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)
495 bmap = isl_basic_map_cow(bmap);
498 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
503 isl_basic_map_free(bmap);
507 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
508 enum isl_dim_type type, unsigned pos, const char *s)
512 map = isl_map_cow(map);
516 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
520 for (i = 0; i < map->n; ++i) {
521 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
532 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
533 __isl_take isl_basic_set *bset,
534 enum isl_dim_type type, unsigned pos, const char *s)
536 return (isl_basic_set *)isl_basic_map_set_dim_name(
537 (isl_basic_map *)bset, type, pos, s);
540 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
541 enum isl_dim_type type, unsigned pos, const char *s)
543 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
546 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
547 enum isl_dim_type type, unsigned pos)
549 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
552 int isl_map_has_dim_id(__isl_keep isl_map *map,
553 enum isl_dim_type type, unsigned pos)
555 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
558 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
559 enum isl_dim_type type, unsigned pos)
561 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
564 int isl_set_has_dim_id(__isl_keep isl_set *set,
565 enum isl_dim_type type, unsigned pos)
567 return isl_map_has_dim_id(set, type, pos);
570 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
571 enum isl_dim_type type, unsigned pos)
573 return isl_map_get_dim_id(set, type, pos);
576 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
577 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
579 map = isl_map_cow(map);
581 return isl_id_free(id);
583 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
585 return isl_map_reset_space(map, isl_space_copy(map->dim));
588 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
589 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
591 return isl_map_set_dim_id(set, type, pos, id);
594 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
595 __isl_keep isl_id *id)
599 return isl_space_find_dim_by_id(map->dim, type, id);
602 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
603 __isl_keep isl_id *id)
605 return isl_map_find_dim_by_id(set, type, id);
608 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
613 return isl_space_find_dim_by_name(map->dim, type, name);
616 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
620 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
623 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
625 return isl_basic_map_is_rational(bset);
628 /* Is this basic set a parameter domain?
630 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
634 return isl_space_is_params(bset->dim);
637 /* Is this set a parameter domain?
639 int isl_set_is_params(__isl_keep isl_set *set)
643 return isl_space_is_params(set->dim);
646 /* Is this map actually a parameter domain?
647 * Users should never call this function. Outside of isl,
648 * a map can never be a parameter domain.
650 int isl_map_is_params(__isl_keep isl_map *map)
654 return isl_space_is_params(map->dim);
657 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
658 struct isl_basic_map *bmap, unsigned extra,
659 unsigned n_eq, unsigned n_ineq)
662 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
667 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
668 if (isl_blk_is_error(bmap->block))
671 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
676 bmap->block2 = isl_blk_empty();
679 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
680 if (isl_blk_is_error(bmap->block2))
683 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
688 for (i = 0; i < n_ineq + n_eq; ++i)
689 bmap->ineq[i] = bmap->block.data + i * row_size;
691 for (i = 0; i < extra; ++i)
692 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
696 bmap->c_size = n_eq + n_ineq;
697 bmap->eq = bmap->ineq + n_ineq;
706 isl_basic_map_free(bmap);
710 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
711 unsigned nparam, unsigned dim, unsigned extra,
712 unsigned n_eq, unsigned n_ineq)
714 struct isl_basic_map *bmap;
717 space = isl_space_set_alloc(ctx, nparam, dim);
721 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
722 return (struct isl_basic_set *)bmap;
725 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
726 unsigned extra, unsigned n_eq, unsigned n_ineq)
728 struct isl_basic_map *bmap;
731 isl_assert(dim->ctx, dim->n_in == 0, goto error);
732 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
733 return (struct isl_basic_set *)bmap;
739 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
740 unsigned extra, unsigned n_eq, unsigned n_ineq)
742 struct isl_basic_map *bmap;
746 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
751 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
757 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
758 unsigned nparam, unsigned in, unsigned out, unsigned extra,
759 unsigned n_eq, unsigned n_ineq)
761 struct isl_basic_map *bmap;
764 dim = isl_space_alloc(ctx, nparam, in, out);
768 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
772 static void dup_constraints(
773 struct isl_basic_map *dst, struct isl_basic_map *src)
776 unsigned total = isl_basic_map_total_dim(src);
778 for (i = 0; i < src->n_eq; ++i) {
779 int j = isl_basic_map_alloc_equality(dst);
780 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
783 for (i = 0; i < src->n_ineq; ++i) {
784 int j = isl_basic_map_alloc_inequality(dst);
785 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
788 for (i = 0; i < src->n_div; ++i) {
789 int j = isl_basic_map_alloc_div(dst);
790 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
792 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
795 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
797 struct isl_basic_map *dup;
801 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
802 bmap->n_div, bmap->n_eq, bmap->n_ineq);
805 dup_constraints(dup, bmap);
806 dup->flags = bmap->flags;
807 dup->sample = isl_vec_copy(bmap->sample);
811 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
813 struct isl_basic_map *dup;
815 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
816 return (struct isl_basic_set *)dup;
819 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
824 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
828 return isl_basic_set_dup(bset);
831 struct isl_set *isl_set_copy(struct isl_set *set)
840 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
845 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
849 bmap = isl_basic_map_dup(bmap);
851 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
855 struct isl_map *isl_map_copy(struct isl_map *map)
864 void isl_basic_map_free(struct isl_basic_map *bmap)
872 isl_ctx_deref(bmap->ctx);
874 isl_blk_free(bmap->ctx, bmap->block2);
876 isl_blk_free(bmap->ctx, bmap->block);
877 isl_vec_free(bmap->sample);
878 isl_space_free(bmap->dim);
882 void isl_basic_set_free(struct isl_basic_set *bset)
884 isl_basic_map_free((struct isl_basic_map *)bset);
887 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
889 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
892 __isl_give isl_map *isl_map_align_params_map_map_and(
893 __isl_take isl_map *map1, __isl_take isl_map *map2,
894 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
895 __isl_take isl_map *map2))
899 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
900 return fn(map1, map2);
901 if (!isl_space_has_named_params(map1->dim) ||
902 !isl_space_has_named_params(map2->dim))
903 isl_die(map1->ctx, isl_error_invalid,
904 "unaligned unnamed parameters", goto error);
905 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
906 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
907 return fn(map1, map2);
914 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
915 __isl_keep isl_map *map2,
916 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
922 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
923 return fn(map1, map2);
924 if (!isl_space_has_named_params(map1->dim) ||
925 !isl_space_has_named_params(map2->dim))
926 isl_die(map1->ctx, isl_error_invalid,
927 "unaligned unnamed parameters", return -1);
928 map1 = isl_map_copy(map1);
929 map2 = isl_map_copy(map2);
930 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
931 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
938 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
944 isl_assert(ctx, room_for_con(bmap, 1), return -1);
945 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
950 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
951 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
952 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
954 int j = isl_basic_map_alloc_inequality(bmap);
958 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
959 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
966 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
967 bmap->extra - bmap->n_div);
971 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
973 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
976 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
980 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
985 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
987 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
990 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
995 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
997 if (pos != bmap->n_eq - 1) {
999 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1000 bmap->eq[bmap->n_eq - 1] = t;
1006 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1008 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1011 void isl_basic_map_inequality_to_equality(
1012 struct isl_basic_map *bmap, unsigned pos)
1016 t = bmap->ineq[pos];
1017 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1018 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1023 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1025 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1026 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1029 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1031 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1034 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1036 struct isl_ctx *ctx;
1040 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1043 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1044 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1045 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1046 1 + isl_basic_map_total_dim(bmap),
1047 bmap->extra - bmap->n_div);
1048 return bmap->n_ineq++;
1051 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1053 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1056 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1060 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1065 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1067 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1070 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1075 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1077 if (pos != bmap->n_ineq - 1) {
1078 t = bmap->ineq[pos];
1079 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1080 bmap->ineq[bmap->n_ineq - 1] = t;
1081 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1087 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1089 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1092 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1097 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1100 k = isl_basic_map_alloc_equality(bmap);
1103 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1106 isl_basic_map_free(bmap);
1110 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1113 return (isl_basic_set *)
1114 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1117 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1122 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1125 k = isl_basic_map_alloc_inequality(bmap);
1128 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1131 isl_basic_map_free(bmap);
1135 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1138 return (isl_basic_set *)
1139 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1142 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1146 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1147 isl_seq_clr(bmap->div[bmap->n_div] +
1148 1 + 1 + isl_basic_map_total_dim(bmap),
1149 bmap->extra - bmap->n_div);
1150 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1151 return bmap->n_div++;
1154 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1156 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1159 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1163 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1168 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1170 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1173 /* Copy constraint from src to dst, putting the vars of src at offset
1174 * dim_off in dst and the divs of src at offset div_off in dst.
1175 * If both sets are actually map, then dim_off applies to the input
1178 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1179 struct isl_basic_map *src_map, isl_int *src,
1180 unsigned in_off, unsigned out_off, unsigned div_off)
1182 unsigned src_nparam = isl_basic_map_n_param(src_map);
1183 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1184 unsigned src_in = isl_basic_map_n_in(src_map);
1185 unsigned dst_in = isl_basic_map_n_in(dst_map);
1186 unsigned src_out = isl_basic_map_n_out(src_map);
1187 unsigned dst_out = isl_basic_map_n_out(dst_map);
1188 isl_int_set(dst[0], src[0]);
1189 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1190 if (dst_nparam > src_nparam)
1191 isl_seq_clr(dst+1+src_nparam,
1192 dst_nparam - src_nparam);
1193 isl_seq_clr(dst+1+dst_nparam, in_off);
1194 isl_seq_cpy(dst+1+dst_nparam+in_off,
1196 isl_min(dst_in-in_off, src_in));
1197 if (dst_in-in_off > src_in)
1198 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1199 dst_in - in_off - src_in);
1200 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1201 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1202 src+1+src_nparam+src_in,
1203 isl_min(dst_out-out_off, src_out));
1204 if (dst_out-out_off > src_out)
1205 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1206 dst_out - out_off - src_out);
1207 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1208 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1209 src+1+src_nparam+src_in+src_out,
1210 isl_min(dst_map->extra-div_off, src_map->n_div));
1211 if (dst_map->n_div-div_off > src_map->n_div)
1212 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1213 div_off+src_map->n_div,
1214 dst_map->n_div - div_off - src_map->n_div);
1217 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1218 struct isl_basic_map *src_map, isl_int *src,
1219 unsigned in_off, unsigned out_off, unsigned div_off)
1221 isl_int_set(dst[0], src[0]);
1222 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1225 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1226 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1231 if (!bmap1 || !bmap2)
1234 div_off = bmap1->n_div;
1236 for (i = 0; i < bmap2->n_eq; ++i) {
1237 int i1 = isl_basic_map_alloc_equality(bmap1);
1240 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1241 i_pos, o_pos, div_off);
1244 for (i = 0; i < bmap2->n_ineq; ++i) {
1245 int i1 = isl_basic_map_alloc_inequality(bmap1);
1248 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1249 i_pos, o_pos, div_off);
1252 for (i = 0; i < bmap2->n_div; ++i) {
1253 int i1 = isl_basic_map_alloc_div(bmap1);
1256 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1257 i_pos, o_pos, div_off);
1260 isl_basic_map_free(bmap2);
1265 isl_basic_map_free(bmap1);
1266 isl_basic_map_free(bmap2);
1270 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1271 struct isl_basic_set *bset2, unsigned pos)
1273 return (struct isl_basic_set *)
1274 add_constraints((struct isl_basic_map *)bset1,
1275 (struct isl_basic_map *)bset2, 0, pos);
1278 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1279 __isl_take isl_space *dim, unsigned extra,
1280 unsigned n_eq, unsigned n_ineq)
1282 struct isl_basic_map *ext;
1292 dims_ok = isl_space_is_equal(base->dim, dim) &&
1293 base->extra >= base->n_div + extra;
1295 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1296 room_for_ineq(base, n_ineq)) {
1297 isl_space_free(dim);
1301 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1302 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1303 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1304 extra += base->extra;
1306 n_ineq += base->n_ineq;
1308 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1314 ext->sample = isl_vec_copy(base->sample);
1315 flags = base->flags;
1316 ext = add_constraints(ext, base, 0, 0);
1319 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1325 isl_space_free(dim);
1326 isl_basic_map_free(base);
1330 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1331 __isl_take isl_space *dim, unsigned extra,
1332 unsigned n_eq, unsigned n_ineq)
1334 return (struct isl_basic_set *)
1335 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1336 extra, n_eq, n_ineq);
1339 struct isl_basic_map *isl_basic_map_extend_constraints(
1340 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1344 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1348 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1349 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1350 unsigned n_eq, unsigned n_ineq)
1352 struct isl_basic_map *bmap;
1357 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1361 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1364 isl_basic_map_free(base);
1368 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1369 unsigned nparam, unsigned dim, unsigned extra,
1370 unsigned n_eq, unsigned n_ineq)
1372 return (struct isl_basic_set *)
1373 isl_basic_map_extend((struct isl_basic_map *)base,
1374 nparam, 0, dim, extra, n_eq, n_ineq);
1377 struct isl_basic_set *isl_basic_set_extend_constraints(
1378 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1380 return (struct isl_basic_set *)
1381 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1385 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1387 return (struct isl_basic_set *)
1388 isl_basic_map_cow((struct isl_basic_map *)bset);
1391 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1396 if (bmap->ref > 1) {
1398 bmap = isl_basic_map_dup(bmap);
1401 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1405 struct isl_set *isl_set_cow(struct isl_set *set)
1413 return isl_set_dup(set);
1416 struct isl_map *isl_map_cow(struct isl_map *map)
1424 return isl_map_dup(map);
1427 static void swap_vars(struct isl_blk blk, isl_int *a,
1428 unsigned a_len, unsigned b_len)
1430 isl_seq_cpy(blk.data, a+a_len, b_len);
1431 isl_seq_cpy(blk.data+b_len, a, a_len);
1432 isl_seq_cpy(a, blk.data, b_len+a_len);
1435 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1436 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1444 isl_assert(bmap->ctx,
1445 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1447 if (n1 == 0 || n2 == 0)
1450 bmap = isl_basic_map_cow(bmap);
1454 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1455 if (isl_blk_is_error(blk))
1458 for (i = 0; i < bmap->n_eq; ++i)
1460 bmap->eq[i] + pos, n1, n2);
1462 for (i = 0; i < bmap->n_ineq; ++i)
1464 bmap->ineq[i] + pos, n1, n2);
1466 for (i = 0; i < bmap->n_div; ++i)
1468 bmap->div[i]+1 + pos, n1, n2);
1470 isl_blk_free(bmap->ctx, blk);
1472 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1473 bmap = isl_basic_map_gauss(bmap, NULL);
1474 return isl_basic_map_finalize(bmap);
1476 isl_basic_map_free(bmap);
1480 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1481 __isl_take isl_basic_set *bset, unsigned n)
1486 nparam = isl_basic_set_n_param(bset);
1487 dim = isl_basic_set_n_dim(bset);
1488 isl_assert(bset->ctx, n <= dim, goto error);
1490 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1492 isl_basic_set_free(bset);
1496 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1502 total = isl_basic_map_total_dim(bmap);
1503 isl_basic_map_free_div(bmap, bmap->n_div);
1504 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1506 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1508 i = isl_basic_map_alloc_equality(bmap);
1512 isl_int_set_si(bmap->eq[i][0], 1);
1513 isl_seq_clr(bmap->eq[i]+1, total);
1514 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1515 isl_vec_free(bmap->sample);
1516 bmap->sample = NULL;
1517 return isl_basic_map_finalize(bmap);
1519 isl_basic_map_free(bmap);
1523 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1525 return (struct isl_basic_set *)
1526 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1529 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1532 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1533 isl_int *t = bmap->div[a];
1534 bmap->div[a] = bmap->div[b];
1537 for (i = 0; i < bmap->n_eq; ++i)
1538 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1540 for (i = 0; i < bmap->n_ineq; ++i)
1541 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1543 for (i = 0; i < bmap->n_div; ++i)
1544 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1548 /* Eliminate the specified n dimensions starting at first from the
1549 * constraints using Fourier-Motzkin. The dimensions themselves
1552 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1553 enum isl_dim_type type, unsigned first, unsigned n)
1562 map = isl_map_cow(map);
1566 for (i = 0; i < map->n; ++i) {
1567 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1577 /* Eliminate the specified n dimensions starting at first from the
1578 * constraints using Fourier-Motzkin. The dimensions themselves
1581 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1582 enum isl_dim_type type, unsigned first, unsigned n)
1584 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1587 /* Eliminate the specified n dimensions starting at first from the
1588 * constraints using Fourier-Motzkin. The dimensions themselves
1591 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1592 unsigned first, unsigned n)
1594 return isl_set_eliminate(set, isl_dim_set, first, n);
1597 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1598 __isl_take isl_basic_map *bmap)
1602 bmap = isl_basic_map_eliminate_vars(bmap,
1603 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1607 return isl_basic_map_finalize(bmap);
1610 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1611 __isl_take isl_basic_set *bset)
1613 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1614 (struct isl_basic_map *)bset);
1617 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1626 map = isl_map_cow(map);
1630 for (i = 0; i < map->n; ++i) {
1631 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1641 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1643 return isl_map_remove_divs(set);
1646 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1647 enum isl_dim_type type, unsigned first, unsigned n)
1651 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1653 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1655 bmap = isl_basic_map_eliminate_vars(bmap,
1656 isl_basic_map_offset(bmap, type) - 1 + first, n);
1659 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1661 bmap = isl_basic_map_drop(bmap, type, first, n);
1664 isl_basic_map_free(bmap);
1668 /* Return true if the definition of the given div (recursively) involves
1669 * any of the given variables.
1671 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1672 unsigned first, unsigned n)
1675 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1677 if (isl_int_is_zero(bmap->div[div][0]))
1679 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1682 for (i = bmap->n_div - 1; i >= 0; --i) {
1683 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1685 if (div_involves_vars(bmap, i, first, n))
1692 /* Remove all divs (recursively) involving any of the given dimensions
1693 * in their definitions.
1695 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1696 __isl_take isl_basic_map *bmap,
1697 enum isl_dim_type type, unsigned first, unsigned n)
1703 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1705 first += isl_basic_map_offset(bmap, type);
1707 for (i = bmap->n_div - 1; i >= 0; --i) {
1708 if (!div_involves_vars(bmap, i, first, n))
1710 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1718 isl_basic_map_free(bmap);
1722 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1723 enum isl_dim_type type, unsigned first, unsigned n)
1732 map = isl_map_cow(map);
1736 for (i = 0; i < map->n; ++i) {
1737 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1748 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1751 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1755 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1756 enum isl_dim_type type, unsigned first, unsigned n)
1763 if (first + n > isl_basic_map_dim(bmap, type))
1764 isl_die(bmap->ctx, isl_error_invalid,
1765 "index out of bounds", return -1);
1767 first += isl_basic_map_offset(bmap, type);
1768 for (i = 0; i < bmap->n_eq; ++i)
1769 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1771 for (i = 0; i < bmap->n_ineq; ++i)
1772 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1778 int isl_map_involves_dims(__isl_keep isl_map *map,
1779 enum isl_dim_type type, unsigned first, unsigned n)
1786 if (first + n > isl_map_dim(map, type))
1787 isl_die(map->ctx, isl_error_invalid,
1788 "index out of bounds", return -1);
1790 for (i = 0; i < map->n; ++i) {
1791 int involves = isl_basic_map_involves_dims(map->p[i],
1793 if (involves < 0 || involves)
1800 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1803 return isl_basic_map_involves_dims(bset, type, first, n);
1806 int isl_set_involves_dims(__isl_keep isl_set *set,
1807 enum isl_dim_type type, unsigned first, unsigned n)
1809 return isl_map_involves_dims(set, type, first, n);
1812 /* Return true if the definition of the given div is unknown or depends
1815 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1818 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1820 if (isl_int_is_zero(bmap->div[div][0]))
1823 for (i = bmap->n_div - 1; i >= 0; --i) {
1824 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1826 if (div_is_unknown(bmap, i))
1833 /* Remove all divs that are unknown or defined in terms of unknown divs.
1835 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1836 __isl_take isl_basic_map *bmap)
1843 for (i = bmap->n_div - 1; i >= 0; --i) {
1844 if (!div_is_unknown(bmap, i))
1846 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1852 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1861 map = isl_map_cow(map);
1865 for (i = 0; i < map->n; ++i) {
1866 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1876 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1878 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1881 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1882 __isl_take isl_basic_set *bset,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 return (isl_basic_set *)
1886 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1889 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1890 enum isl_dim_type type, unsigned first, unsigned n)
1897 map = isl_map_cow(map);
1900 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1902 for (i = 0; i < map->n; ++i) {
1903 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1904 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1908 map = isl_map_drop(map, type, first, n);
1915 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1916 enum isl_dim_type type, unsigned first, unsigned n)
1918 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1921 /* Project out n inputs starting at first using Fourier-Motzkin */
1922 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1923 unsigned first, unsigned n)
1925 return isl_map_remove_dims(map, isl_dim_in, first, n);
1928 static void dump_term(struct isl_basic_map *bmap,
1929 isl_int c, int pos, FILE *out)
1932 unsigned in = isl_basic_map_n_in(bmap);
1933 unsigned dim = in + isl_basic_map_n_out(bmap);
1934 unsigned nparam = isl_basic_map_n_param(bmap);
1936 isl_int_print(out, c, 0);
1938 if (!isl_int_is_one(c))
1939 isl_int_print(out, c, 0);
1940 if (pos < 1 + nparam) {
1941 name = isl_space_get_dim_name(bmap->dim,
1942 isl_dim_param, pos - 1);
1944 fprintf(out, "%s", name);
1946 fprintf(out, "p%d", pos - 1);
1947 } else if (pos < 1 + nparam + in)
1948 fprintf(out, "i%d", pos - 1 - nparam);
1949 else if (pos < 1 + nparam + dim)
1950 fprintf(out, "o%d", pos - 1 - nparam - in);
1952 fprintf(out, "e%d", pos - 1 - nparam - dim);
1956 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1957 int sign, FILE *out)
1961 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1965 for (i = 0, first = 1; i < len; ++i) {
1966 if (isl_int_sgn(c[i]) * sign <= 0)
1969 fprintf(out, " + ");
1971 isl_int_abs(v, c[i]);
1972 dump_term(bmap, v, i, out);
1979 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1980 const char *op, FILE *out, int indent)
1984 fprintf(out, "%*s", indent, "");
1986 dump_constraint_sign(bmap, c, 1, out);
1987 fprintf(out, " %s ", op);
1988 dump_constraint_sign(bmap, c, -1, out);
1992 for (i = bmap->n_div; i < bmap->extra; ++i) {
1993 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1995 fprintf(out, "%*s", indent, "");
1996 fprintf(out, "ERROR: unused div coefficient not zero\n");
2001 static void dump_constraints(struct isl_basic_map *bmap,
2002 isl_int **c, unsigned n,
2003 const char *op, FILE *out, int indent)
2007 for (i = 0; i < n; ++i)
2008 dump_constraint(bmap, c[i], op, out, indent);
2011 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2015 unsigned total = isl_basic_map_total_dim(bmap);
2017 for (j = 0; j < 1 + total; ++j) {
2018 if (isl_int_is_zero(exp[j]))
2020 if (!first && isl_int_is_pos(exp[j]))
2022 dump_term(bmap, exp[j], j, out);
2027 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2031 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2032 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2034 for (i = 0; i < bmap->n_div; ++i) {
2035 fprintf(out, "%*s", indent, "");
2036 fprintf(out, "e%d = [(", i);
2037 dump_affine(bmap, bmap->div[i]+1, out);
2039 isl_int_print(out, bmap->div[i][0], 0);
2040 fprintf(out, "]\n");
2044 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2045 FILE *out, int indent)
2048 fprintf(out, "null basic set\n");
2052 fprintf(out, "%*s", indent, "");
2053 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2054 bset->ref, bset->dim->nparam, bset->dim->n_out,
2055 bset->extra, bset->flags);
2056 dump((struct isl_basic_map *)bset, out, indent);
2059 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2060 FILE *out, int indent)
2063 fprintf(out, "null basic map\n");
2067 fprintf(out, "%*s", indent, "");
2068 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2069 "flags: %x, n_name: %d\n",
2071 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2072 bmap->extra, bmap->flags, bmap->dim->n_id);
2073 dump(bmap, out, indent);
2076 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2081 total = isl_basic_map_total_dim(bmap);
2082 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2083 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2084 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2089 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2092 struct isl_set *set;
2096 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2097 isl_assert(dim->ctx, n >= 0, goto error);
2098 set = isl_alloc(dim->ctx, struct isl_set,
2099 sizeof(struct isl_set) +
2100 (n - 1) * sizeof(struct isl_basic_set *));
2104 set->ctx = dim->ctx;
2105 isl_ctx_ref(set->ctx);
2113 isl_space_free(dim);
2117 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2118 unsigned nparam, unsigned dim, int n, unsigned flags)
2120 struct isl_set *set;
2123 dims = isl_space_alloc(ctx, nparam, 0, dim);
2127 set = isl_set_alloc_space(dims, n, flags);
2131 /* Make sure "map" has room for at least "n" more basic maps.
2133 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2136 struct isl_map *grown = NULL;
2140 isl_assert(map->ctx, n >= 0, goto error);
2141 if (map->n + n <= map->size)
2143 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2146 for (i = 0; i < map->n; ++i) {
2147 grown->p[i] = isl_basic_map_copy(map->p[i]);
2155 isl_map_free(grown);
2160 /* Make sure "set" has room for at least "n" more basic sets.
2162 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2164 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2167 struct isl_set *isl_set_dup(struct isl_set *set)
2170 struct isl_set *dup;
2175 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2178 for (i = 0; i < set->n; ++i)
2179 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2183 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2185 return isl_map_from_basic_map(bset);
2188 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2190 struct isl_map *map;
2195 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2196 return isl_map_add_basic_map(map, bmap);
2199 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2200 __isl_take isl_basic_set *bset)
2202 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2203 (struct isl_basic_map *)bset);
2206 void isl_set_free(struct isl_set *set)
2216 isl_ctx_deref(set->ctx);
2217 for (i = 0; i < set->n; ++i)
2218 isl_basic_set_free(set->p[i]);
2219 isl_space_free(set->dim);
2223 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2228 fprintf(out, "null set\n");
2232 fprintf(out, "%*s", indent, "");
2233 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2234 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2236 for (i = 0; i < set->n; ++i) {
2237 fprintf(out, "%*s", indent, "");
2238 fprintf(out, "basic set %d:\n", i);
2239 isl_basic_set_print_internal(set->p[i], out, indent+4);
2243 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2248 fprintf(out, "null map\n");
2252 fprintf(out, "%*s", indent, "");
2253 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2254 "flags: %x, n_name: %d\n",
2255 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2256 map->dim->n_out, map->flags, map->dim->n_id);
2257 for (i = 0; i < map->n; ++i) {
2258 fprintf(out, "%*s", indent, "");
2259 fprintf(out, "basic map %d:\n", i);
2260 isl_basic_map_print_internal(map->p[i], out, indent+4);
2264 struct isl_basic_map *isl_basic_map_intersect_domain(
2265 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2267 struct isl_basic_map *bmap_domain;
2272 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2273 bset->dim, isl_dim_param), goto error);
2275 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2276 isl_assert(bset->ctx,
2277 isl_basic_map_compatible_domain(bmap, bset), goto error);
2279 bmap = isl_basic_map_cow(bmap);
2282 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2283 bset->n_div, bset->n_eq, bset->n_ineq);
2284 bmap_domain = isl_basic_map_from_domain(bset);
2285 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2287 bmap = isl_basic_map_simplify(bmap);
2288 return isl_basic_map_finalize(bmap);
2290 isl_basic_map_free(bmap);
2291 isl_basic_set_free(bset);
2295 struct isl_basic_map *isl_basic_map_intersect_range(
2296 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2298 struct isl_basic_map *bmap_range;
2303 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2304 bset->dim, isl_dim_param), goto error);
2306 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2307 isl_assert(bset->ctx,
2308 isl_basic_map_compatible_range(bmap, bset), goto error);
2310 if (isl_basic_set_is_universe(bset)) {
2311 isl_basic_set_free(bset);
2315 bmap = isl_basic_map_cow(bmap);
2318 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2319 bset->n_div, bset->n_eq, bset->n_ineq);
2320 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2321 bmap = add_constraints(bmap, bmap_range, 0, 0);
2323 bmap = isl_basic_map_simplify(bmap);
2324 return isl_basic_map_finalize(bmap);
2326 isl_basic_map_free(bmap);
2327 isl_basic_set_free(bset);
2331 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2337 total = 1 + isl_basic_map_total_dim(bmap);
2338 if (total != vec->size)
2343 for (i = 0; i < bmap->n_eq; ++i) {
2344 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2345 if (!isl_int_is_zero(s)) {
2351 for (i = 0; i < bmap->n_ineq; ++i) {
2352 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2353 if (isl_int_is_neg(s)) {
2364 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2366 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2369 struct isl_basic_map *isl_basic_map_intersect(
2370 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2372 struct isl_vec *sample = NULL;
2374 if (!bmap1 || !bmap2)
2377 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2378 bmap2->dim, isl_dim_param), goto error);
2379 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2380 isl_space_dim(bmap1->dim, isl_dim_param) &&
2381 isl_space_dim(bmap2->dim, isl_dim_all) !=
2382 isl_space_dim(bmap2->dim, isl_dim_param))
2383 return isl_basic_map_intersect(bmap2, bmap1);
2385 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2386 isl_space_dim(bmap2->dim, isl_dim_param))
2387 isl_assert(bmap1->ctx,
2388 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2390 if (bmap1->sample &&
2391 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2392 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2393 sample = isl_vec_copy(bmap1->sample);
2394 else if (bmap2->sample &&
2395 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2396 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2397 sample = isl_vec_copy(bmap2->sample);
2399 bmap1 = isl_basic_map_cow(bmap1);
2402 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2403 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2404 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2407 isl_vec_free(sample);
2409 isl_vec_free(bmap1->sample);
2410 bmap1->sample = sample;
2413 bmap1 = isl_basic_map_simplify(bmap1);
2414 return isl_basic_map_finalize(bmap1);
2417 isl_vec_free(sample);
2418 isl_basic_map_free(bmap1);
2419 isl_basic_map_free(bmap2);
2423 struct isl_basic_set *isl_basic_set_intersect(
2424 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2426 return (struct isl_basic_set *)
2427 isl_basic_map_intersect(
2428 (struct isl_basic_map *)bset1,
2429 (struct isl_basic_map *)bset2);
2432 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2433 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2435 return isl_basic_set_intersect(bset1, bset2);
2438 /* Special case of isl_map_intersect, where both map1 and map2
2439 * are convex, without any divs and such that either map1 or map2
2440 * contains a single constraint. This constraint is then simply
2441 * added to the other map.
2443 static __isl_give isl_map *map_intersect_add_constraint(
2444 __isl_take isl_map *map1, __isl_take isl_map *map2)
2446 isl_assert(map1->ctx, map1->n == 1, goto error);
2447 isl_assert(map2->ctx, map1->n == 1, goto error);
2448 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2449 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2451 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2452 return isl_map_intersect(map2, map1);
2454 isl_assert(map2->ctx,
2455 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2457 map1 = isl_map_cow(map1);
2460 if (isl_map_plain_is_empty(map1)) {
2464 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2465 if (map2->p[0]->n_eq == 1)
2466 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2468 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2469 map2->p[0]->ineq[0]);
2471 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2472 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2476 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2477 isl_basic_map_free(map1->p[0]);
2490 /* map2 may be either a parameter domain or a map living in the same
2493 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2494 __isl_take isl_map *map2)
2497 struct isl_map *result;
2503 if (isl_map_plain_is_empty(map1) &&
2504 isl_space_is_equal(map1->dim, map2->dim)) {
2508 if (isl_map_plain_is_empty(map2) &&
2509 isl_space_is_equal(map1->dim, map2->dim)) {
2514 if (map1->n == 1 && map2->n == 1 &&
2515 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2516 isl_space_is_equal(map1->dim, map2->dim) &&
2517 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2518 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2519 return map_intersect_add_constraint(map1, map2);
2521 if (isl_space_dim(map2->dim, isl_dim_all) !=
2522 isl_space_dim(map2->dim, isl_dim_param))
2523 isl_assert(map1->ctx,
2524 isl_space_is_equal(map1->dim, map2->dim), goto error);
2526 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2527 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2528 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2530 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2531 map1->n * map2->n, flags);
2534 for (i = 0; i < map1->n; ++i)
2535 for (j = 0; j < map2->n; ++j) {
2536 struct isl_basic_map *part;
2537 part = isl_basic_map_intersect(
2538 isl_basic_map_copy(map1->p[i]),
2539 isl_basic_map_copy(map2->p[j]));
2540 if (isl_basic_map_is_empty(part))
2541 isl_basic_map_free(part);
2543 result = isl_map_add_basic_map(result, part);
2556 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2557 __isl_take isl_map *map2)
2561 if (!isl_space_is_equal(map1->dim, map2->dim))
2562 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2563 "spaces don't match", goto error);
2564 return map_intersect_internal(map1, map2);
2571 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2572 __isl_take isl_map *map2)
2574 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2577 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2579 return (struct isl_set *)
2580 isl_map_intersect((struct isl_map *)set1,
2581 (struct isl_map *)set2);
2584 /* map_intersect_internal accepts intersections
2585 * with parameter domains, so we can just call that function.
2587 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2588 __isl_take isl_set *params)
2590 return map_intersect_internal(map, params);
2593 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2594 __isl_take isl_map *map2)
2596 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2599 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2600 __isl_take isl_set *params)
2602 return isl_map_intersect_params(set, params);
2605 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2608 struct isl_basic_set *bset;
2613 bmap = isl_basic_map_cow(bmap);
2616 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2617 in = isl_basic_map_n_in(bmap);
2618 bset = isl_basic_set_from_basic_map(bmap);
2619 bset = isl_basic_set_swap_vars(bset, in);
2620 return isl_basic_map_from_basic_set(bset, dim);
2623 static __isl_give isl_basic_map *basic_map_space_reset(
2624 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2628 if (!isl_space_is_named_or_nested(bmap->dim, type))
2631 space = isl_basic_map_get_space(bmap);
2632 space = isl_space_reset(space, type);
2633 bmap = isl_basic_map_reset_space(bmap, space);
2637 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2638 enum isl_dim_type type, unsigned pos, unsigned n)
2641 struct isl_basic_map *res;
2642 struct isl_dim_map *dim_map;
2643 unsigned total, off;
2644 enum isl_dim_type t;
2647 return basic_map_space_reset(bmap, type);
2652 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2654 total = isl_basic_map_total_dim(bmap) + n;
2655 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2657 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2659 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2661 unsigned size = isl_basic_map_dim(bmap, t);
2662 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2664 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2665 pos, size - pos, off + pos + n);
2667 off += isl_space_dim(res_dim, t);
2669 isl_dim_map_div(dim_map, bmap, off);
2671 res = isl_basic_map_alloc_space(res_dim,
2672 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2673 if (isl_basic_map_is_rational(bmap))
2674 res = isl_basic_map_set_rational(res);
2675 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2676 return isl_basic_map_finalize(res);
2679 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2680 enum isl_dim_type type, unsigned n)
2684 return isl_basic_map_insert(bmap, type,
2685 isl_basic_map_dim(bmap, type), n);
2688 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2689 enum isl_dim_type type, unsigned n)
2693 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2694 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2696 isl_basic_set_free(bset);
2700 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2701 enum isl_dim_type type)
2705 if (!isl_space_is_named_or_nested(map->dim, type))
2708 space = isl_map_get_space(map);
2709 space = isl_space_reset(space, type);
2710 map = isl_map_reset_space(map, space);
2714 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2715 enum isl_dim_type type, unsigned pos, unsigned n)
2720 return map_space_reset(map, type);
2722 map = isl_map_cow(map);
2726 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2730 for (i = 0; i < map->n; ++i) {
2731 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2742 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2743 enum isl_dim_type type, unsigned pos, unsigned n)
2745 return isl_map_insert_dims(set, type, pos, n);
2748 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2749 enum isl_dim_type type, unsigned n)
2753 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2756 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2757 enum isl_dim_type type, unsigned n)
2761 isl_assert(set->ctx, type != isl_dim_in, goto error);
2762 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2768 __isl_give isl_basic_map *isl_basic_map_move_dims(
2769 __isl_take isl_basic_map *bmap,
2770 enum isl_dim_type dst_type, unsigned dst_pos,
2771 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2773 struct isl_dim_map *dim_map;
2774 struct isl_basic_map *res;
2775 enum isl_dim_type t;
2776 unsigned total, off;
2783 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2786 if (dst_type == src_type && dst_pos == src_pos)
2789 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2791 if (pos(bmap->dim, dst_type) + dst_pos ==
2792 pos(bmap->dim, src_type) + src_pos +
2793 ((src_type < dst_type) ? n : 0)) {
2794 bmap = isl_basic_map_cow(bmap);
2798 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2799 src_type, src_pos, n);
2803 bmap = isl_basic_map_finalize(bmap);
2808 total = isl_basic_map_total_dim(bmap);
2809 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2812 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2813 unsigned size = isl_space_dim(bmap->dim, t);
2814 if (t == dst_type) {
2815 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2818 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2821 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2822 dst_pos, size - dst_pos, off);
2823 off += size - dst_pos;
2824 } else if (t == src_type) {
2825 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2828 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2829 src_pos + n, size - src_pos - n, off);
2830 off += size - src_pos - n;
2832 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2836 isl_dim_map_div(dim_map, bmap, off);
2838 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2839 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2840 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2842 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2843 src_type, src_pos, n);
2847 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2848 bmap = isl_basic_map_gauss(bmap, NULL);
2849 bmap = isl_basic_map_finalize(bmap);
2853 isl_basic_map_free(bmap);
2857 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2858 enum isl_dim_type dst_type, unsigned dst_pos,
2859 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2861 return (isl_basic_set *)isl_basic_map_move_dims(
2862 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2865 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2866 enum isl_dim_type dst_type, unsigned dst_pos,
2867 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2871 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2872 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2873 src_type, src_pos, n);
2879 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2880 enum isl_dim_type dst_type, unsigned dst_pos,
2881 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2890 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2893 if (dst_type == src_type && dst_pos == src_pos)
2896 isl_assert(map->ctx, dst_type != src_type, goto error);
2898 map = isl_map_cow(map);
2902 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2906 for (i = 0; i < map->n; ++i) {
2907 map->p[i] = isl_basic_map_move_dims(map->p[i],
2909 src_type, src_pos, n);
2920 /* Move the specified dimensions to the last columns right before
2921 * the divs. Don't change the dimension specification of bmap.
2922 * That's the responsibility of the caller.
2924 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 struct isl_dim_map *dim_map;
2928 struct isl_basic_map *res;
2929 enum isl_dim_type t;
2930 unsigned total, off;
2934 if (pos(bmap->dim, type) + first + n ==
2935 1 + isl_space_dim(bmap->dim, isl_dim_all))
2938 total = isl_basic_map_total_dim(bmap);
2939 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2942 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2943 unsigned size = isl_space_dim(bmap->dim, t);
2945 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2948 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2949 first, n, total - bmap->n_div - n);
2950 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2951 first + n, size - (first + n), off);
2952 off += size - (first + n);
2954 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2958 isl_dim_map_div(dim_map, bmap, off + n);
2960 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2961 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2962 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2966 /* Turn the n dimensions of type type, starting at first
2967 * into existentially quantified variables.
2969 __isl_give isl_basic_map *isl_basic_map_project_out(
2970 __isl_take isl_basic_map *bmap,
2971 enum isl_dim_type type, unsigned first, unsigned n)
2984 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2985 return isl_basic_map_remove_dims(bmap, type, first, n);
2987 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2990 bmap = move_last(bmap, type, first, n);
2991 bmap = isl_basic_map_cow(bmap);
2995 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2996 old = bmap->block2.data;
2997 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2998 (bmap->extra + n) * (1 + row_size));
2999 if (!bmap->block2.data)
3001 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3004 for (i = 0; i < n; ++i) {
3005 new_div[i] = bmap->block2.data +
3006 (bmap->extra + i) * (1 + row_size);
3007 isl_seq_clr(new_div[i], 1 + row_size);
3009 for (i = 0; i < bmap->extra; ++i)
3010 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3012 bmap->div = new_div;
3016 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3019 bmap = isl_basic_map_simplify(bmap);
3020 bmap = isl_basic_map_drop_redundant_divs(bmap);
3021 return isl_basic_map_finalize(bmap);
3023 isl_basic_map_free(bmap);
3027 /* Turn the n dimensions of type type, starting at first
3028 * into existentially quantified variables.
3030 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3031 enum isl_dim_type type, unsigned first, unsigned n)
3033 return (isl_basic_set *)isl_basic_map_project_out(
3034 (isl_basic_map *)bset, type, first, n);
3037 /* Turn the n dimensions of type type, starting at first
3038 * into existentially quantified variables.
3040 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3041 enum isl_dim_type type, unsigned first, unsigned n)
3051 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3053 map = isl_map_cow(map);
3057 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3061 for (i = 0; i < map->n; ++i) {
3062 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3073 /* Turn the n dimensions of type type, starting at first
3074 * into existentially quantified variables.
3076 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3077 enum isl_dim_type type, unsigned first, unsigned n)
3079 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3082 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3086 for (i = 0; i < n; ++i) {
3087 j = isl_basic_map_alloc_div(bmap);
3090 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3094 isl_basic_map_free(bmap);
3098 struct isl_basic_map *isl_basic_map_apply_range(
3099 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3101 isl_space *dim_result = NULL;
3102 struct isl_basic_map *bmap;
3103 unsigned n_in, n_out, n, nparam, total, pos;
3104 struct isl_dim_map *dim_map1, *dim_map2;
3106 if (!bmap1 || !bmap2)
3109 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3110 isl_space_copy(bmap2->dim));
3112 n_in = isl_basic_map_n_in(bmap1);
3113 n_out = isl_basic_map_n_out(bmap2);
3114 n = isl_basic_map_n_out(bmap1);
3115 nparam = isl_basic_map_n_param(bmap1);
3117 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3118 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3119 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3120 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3121 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3122 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3123 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3124 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3125 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3126 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3127 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3129 bmap = isl_basic_map_alloc_space(dim_result,
3130 bmap1->n_div + bmap2->n_div + n,
3131 bmap1->n_eq + bmap2->n_eq,
3132 bmap1->n_ineq + bmap2->n_ineq);
3133 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3134 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3135 bmap = add_divs(bmap, n);
3136 bmap = isl_basic_map_simplify(bmap);
3137 bmap = isl_basic_map_drop_redundant_divs(bmap);
3138 return isl_basic_map_finalize(bmap);
3140 isl_basic_map_free(bmap1);
3141 isl_basic_map_free(bmap2);
3145 struct isl_basic_set *isl_basic_set_apply(
3146 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3151 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3154 return (struct isl_basic_set *)
3155 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3157 isl_basic_set_free(bset);
3158 isl_basic_map_free(bmap);
3162 struct isl_basic_map *isl_basic_map_apply_domain(
3163 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3165 if (!bmap1 || !bmap2)
3168 isl_assert(bmap1->ctx,
3169 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3170 isl_assert(bmap1->ctx,
3171 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3174 bmap1 = isl_basic_map_reverse(bmap1);
3175 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3176 return isl_basic_map_reverse(bmap1);
3178 isl_basic_map_free(bmap1);
3179 isl_basic_map_free(bmap2);
3183 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3184 * A \cap B -> f(A) + f(B)
3186 struct isl_basic_map *isl_basic_map_sum(
3187 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3189 unsigned n_in, n_out, nparam, total, pos;
3190 struct isl_basic_map *bmap = NULL;
3191 struct isl_dim_map *dim_map1, *dim_map2;
3194 if (!bmap1 || !bmap2)
3197 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3200 nparam = isl_basic_map_n_param(bmap1);
3201 n_in = isl_basic_map_n_in(bmap1);
3202 n_out = isl_basic_map_n_out(bmap1);
3204 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3205 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3206 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3207 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3208 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3209 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3210 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3211 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3212 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3213 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3214 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3216 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3217 bmap1->n_div + bmap2->n_div + 2 * n_out,
3218 bmap1->n_eq + bmap2->n_eq + n_out,
3219 bmap1->n_ineq + bmap2->n_ineq);
3220 for (i = 0; i < n_out; ++i) {
3221 int j = isl_basic_map_alloc_equality(bmap);
3224 isl_seq_clr(bmap->eq[j], 1+total);
3225 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3226 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3227 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3229 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3230 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3231 bmap = add_divs(bmap, 2 * n_out);
3233 bmap = isl_basic_map_simplify(bmap);
3234 return isl_basic_map_finalize(bmap);
3236 isl_basic_map_free(bmap);
3237 isl_basic_map_free(bmap1);
3238 isl_basic_map_free(bmap2);
3242 /* Given two maps A -> f(A) and B -> g(B), construct a map
3243 * A \cap B -> f(A) + f(B)
3245 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3247 struct isl_map *result;
3253 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3255 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3256 map1->n * map2->n, 0);
3259 for (i = 0; i < map1->n; ++i)
3260 for (j = 0; j < map2->n; ++j) {
3261 struct isl_basic_map *part;
3262 part = isl_basic_map_sum(
3263 isl_basic_map_copy(map1->p[i]),
3264 isl_basic_map_copy(map2->p[j]));
3265 if (isl_basic_map_is_empty(part))
3266 isl_basic_map_free(part);
3268 result = isl_map_add_basic_map(result, part);
3281 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3282 __isl_take isl_set *set2)
3284 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3287 /* Given a basic map A -> f(A), construct A -> -f(A).
3289 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3294 bmap = isl_basic_map_cow(bmap);
3298 n = isl_basic_map_dim(bmap, isl_dim_out);
3299 off = isl_basic_map_offset(bmap, isl_dim_out);
3300 for (i = 0; i < bmap->n_eq; ++i)
3301 for (j = 0; j < n; ++j)
3302 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3303 for (i = 0; i < bmap->n_ineq; ++i)
3304 for (j = 0; j < n; ++j)
3305 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3306 for (i = 0; i < bmap->n_div; ++i)
3307 for (j = 0; j < n; ++j)
3308 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3309 bmap = isl_basic_map_gauss(bmap, NULL);
3310 return isl_basic_map_finalize(bmap);
3313 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3315 return isl_basic_map_neg(bset);
3318 /* Given a map A -> f(A), construct A -> -f(A).
3320 struct isl_map *isl_map_neg(struct isl_map *map)
3324 map = isl_map_cow(map);
3328 for (i = 0; i < map->n; ++i) {
3329 map->p[i] = isl_basic_map_neg(map->p[i]);
3340 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3342 return (isl_set *)isl_map_neg((isl_map *)set);
3345 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3346 * A -> floor(f(A)/d).
3348 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3351 unsigned n_in, n_out, nparam, total, pos;
3352 struct isl_basic_map *result = NULL;
3353 struct isl_dim_map *dim_map;
3359 nparam = isl_basic_map_n_param(bmap);
3360 n_in = isl_basic_map_n_in(bmap);
3361 n_out = isl_basic_map_n_out(bmap);
3363 total = nparam + n_in + n_out + bmap->n_div + n_out;
3364 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3365 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3366 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3367 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3368 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3370 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3371 bmap->n_div + n_out,
3372 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3373 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3374 result = add_divs(result, n_out);
3375 for (i = 0; i < n_out; ++i) {
3377 j = isl_basic_map_alloc_inequality(result);
3380 isl_seq_clr(result->ineq[j], 1+total);
3381 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3382 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3383 j = isl_basic_map_alloc_inequality(result);
3386 isl_seq_clr(result->ineq[j], 1+total);
3387 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3388 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3389 isl_int_sub_ui(result->ineq[j][0], d, 1);
3392 result = isl_basic_map_simplify(result);
3393 return isl_basic_map_finalize(result);
3395 isl_basic_map_free(result);
3399 /* Given a map A -> f(A) and an integer d, construct a map
3400 * A -> floor(f(A)/d).
3402 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3406 map = isl_map_cow(map);
3410 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3411 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3412 for (i = 0; i < map->n; ++i) {
3413 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3424 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3430 i = isl_basic_map_alloc_equality(bmap);
3433 nparam = isl_basic_map_n_param(bmap);
3434 n_in = isl_basic_map_n_in(bmap);
3435 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3436 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3437 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3438 return isl_basic_map_finalize(bmap);
3440 isl_basic_map_free(bmap);
3444 /* Add a constraints to "bmap" expressing i_pos < o_pos
3446 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3452 i = isl_basic_map_alloc_inequality(bmap);
3455 nparam = isl_basic_map_n_param(bmap);
3456 n_in = isl_basic_map_n_in(bmap);
3457 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3458 isl_int_set_si(bmap->ineq[i][0], -1);
3459 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3460 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3461 return isl_basic_map_finalize(bmap);
3463 isl_basic_map_free(bmap);
3467 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3469 static __isl_give isl_basic_map *var_less_or_equal(
3470 __isl_take isl_basic_map *bmap, unsigned pos)
3476 i = isl_basic_map_alloc_inequality(bmap);
3479 nparam = isl_basic_map_n_param(bmap);
3480 n_in = isl_basic_map_n_in(bmap);
3481 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3482 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3483 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3484 return isl_basic_map_finalize(bmap);
3486 isl_basic_map_free(bmap);
3490 /* Add a constraints to "bmap" expressing i_pos > o_pos
3492 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3498 i = isl_basic_map_alloc_inequality(bmap);
3501 nparam = isl_basic_map_n_param(bmap);
3502 n_in = isl_basic_map_n_in(bmap);
3503 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3504 isl_int_set_si(bmap->ineq[i][0], -1);
3505 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3506 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3507 return isl_basic_map_finalize(bmap);
3509 isl_basic_map_free(bmap);
3513 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3515 static __isl_give isl_basic_map *var_more_or_equal(
3516 __isl_take isl_basic_map *bmap, unsigned pos)
3522 i = isl_basic_map_alloc_inequality(bmap);
3525 nparam = isl_basic_map_n_param(bmap);
3526 n_in = isl_basic_map_n_in(bmap);
3527 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3528 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3529 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3530 return isl_basic_map_finalize(bmap);
3532 isl_basic_map_free(bmap);
3536 __isl_give isl_basic_map *isl_basic_map_equal(
3537 __isl_take isl_space *dim, unsigned n_equal)
3540 struct isl_basic_map *bmap;
3541 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3544 for (i = 0; i < n_equal && bmap; ++i)
3545 bmap = var_equal(bmap, i);
3546 return isl_basic_map_finalize(bmap);
3549 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3551 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3555 struct isl_basic_map *bmap;
3556 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3559 for (i = 0; i < pos && bmap; ++i)
3560 bmap = var_equal(bmap, i);
3562 bmap = var_less(bmap, pos);
3563 return isl_basic_map_finalize(bmap);
3566 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3568 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3569 __isl_take isl_space *dim, unsigned pos)
3572 isl_basic_map *bmap;
3574 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3575 for (i = 0; i < pos; ++i)
3576 bmap = var_equal(bmap, i);
3577 bmap = var_less_or_equal(bmap, pos);
3578 return isl_basic_map_finalize(bmap);
3581 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3583 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3587 struct isl_basic_map *bmap;
3588 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3591 for (i = 0; i < pos && bmap; ++i)
3592 bmap = var_equal(bmap, i);
3594 bmap = var_more(bmap, pos);
3595 return isl_basic_map_finalize(bmap);
3598 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3600 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3601 __isl_take isl_space *dim, unsigned pos)
3604 isl_basic_map *bmap;
3606 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3607 for (i = 0; i < pos; ++i)
3608 bmap = var_equal(bmap, i);
3609 bmap = var_more_or_equal(bmap, pos);
3610 return isl_basic_map_finalize(bmap);
3613 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3614 unsigned n, int equal)
3616 struct isl_map *map;
3619 if (n == 0 && equal)
3620 return isl_map_universe(dims);
3622 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3624 for (i = 0; i + 1 < n; ++i)
3625 map = isl_map_add_basic_map(map,
3626 isl_basic_map_less_at(isl_space_copy(dims), i));
3629 map = isl_map_add_basic_map(map,
3630 isl_basic_map_less_or_equal_at(dims, n - 1));
3632 map = isl_map_add_basic_map(map,
3633 isl_basic_map_less_at(dims, n - 1));
3635 isl_space_free(dims);
3640 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3644 return map_lex_lte_first(dims, dims->n_out, equal);
3647 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3649 return map_lex_lte_first(dim, n, 0);
3652 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3654 return map_lex_lte_first(dim, n, 1);
3657 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3659 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3662 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3664 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3667 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3668 unsigned n, int equal)
3670 struct isl_map *map;
3673 if (n == 0 && equal)
3674 return isl_map_universe(dims);
3676 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3678 for (i = 0; i + 1 < n; ++i)
3679 map = isl_map_add_basic_map(map,
3680 isl_basic_map_more_at(isl_space_copy(dims), i));
3683 map = isl_map_add_basic_map(map,
3684 isl_basic_map_more_or_equal_at(dims, n - 1));
3686 map = isl_map_add_basic_map(map,
3687 isl_basic_map_more_at(dims, n - 1));
3689 isl_space_free(dims);
3694 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3698 return map_lex_gte_first(dims, dims->n_out, equal);
3701 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3703 return map_lex_gte_first(dim, n, 0);
3706 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3708 return map_lex_gte_first(dim, n, 1);
3711 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3713 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3716 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3718 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3721 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3722 __isl_take isl_set *set2)
3725 map = isl_map_lex_le(isl_set_get_space(set1));
3726 map = isl_map_intersect_domain(map, set1);
3727 map = isl_map_intersect_range(map, set2);
3731 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3732 __isl_take isl_set *set2)
3735 map = isl_map_lex_lt(isl_set_get_space(set1));
3736 map = isl_map_intersect_domain(map, set1);
3737 map = isl_map_intersect_range(map, set2);
3741 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3742 __isl_take isl_set *set2)
3745 map = isl_map_lex_ge(isl_set_get_space(set1));
3746 map = isl_map_intersect_domain(map, set1);
3747 map = isl_map_intersect_range(map, set2);
3751 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3752 __isl_take isl_set *set2)
3755 map = isl_map_lex_gt(isl_set_get_space(set1));
3756 map = isl_map_intersect_domain(map, set1);
3757 map = isl_map_intersect_range(map, set2);
3761 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3762 __isl_take isl_map *map2)
3765 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3766 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3767 map = isl_map_apply_range(map, isl_map_reverse(map2));
3771 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3772 __isl_take isl_map *map2)
3775 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3776 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3777 map = isl_map_apply_range(map, isl_map_reverse(map2));
3781 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3782 __isl_take isl_map *map2)
3785 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3786 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3787 map = isl_map_apply_range(map, isl_map_reverse(map2));
3791 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3792 __isl_take isl_map *map2)
3795 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3796 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3797 map = isl_map_apply_range(map, isl_map_reverse(map2));
3801 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3802 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3804 struct isl_basic_map *bmap;
3806 bset = isl_basic_set_cow(bset);
3810 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3811 isl_space_free(bset->dim);
3812 bmap = (struct isl_basic_map *) bset;
3814 return isl_basic_map_finalize(bmap);
3816 isl_basic_set_free(bset);
3817 isl_space_free(dim);
3821 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3825 if (bmap->dim->n_in == 0)
3826 return (struct isl_basic_set *)bmap;
3827 bmap = isl_basic_map_cow(bmap);
3830 bmap->dim = isl_space_as_set_space(bmap->dim);
3833 bmap = isl_basic_map_finalize(bmap);
3834 return (struct isl_basic_set *)bmap;
3836 isl_basic_map_free(bmap);
3840 /* For a div d = floor(f/m), add the constraints
3843 * -(f-(n-1)) + m d >= 0
3845 * Note that the second constraint is the negation of
3849 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3850 unsigned pos, isl_int *div)
3853 unsigned total = isl_basic_map_total_dim(bmap);
3855 i = isl_basic_map_alloc_inequality(bmap);
3858 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3859 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3861 j = isl_basic_map_alloc_inequality(bmap);
3864 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3865 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3866 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3870 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3871 unsigned pos, isl_int *div)
3873 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3877 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3879 unsigned total = isl_basic_map_total_dim(bmap);
3880 unsigned div_pos = total - bmap->n_div + div;
3882 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3886 struct isl_basic_set *isl_basic_map_underlying_set(
3887 struct isl_basic_map *bmap)
3891 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3893 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3894 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3895 return (struct isl_basic_set *)bmap;
3896 bmap = isl_basic_map_cow(bmap);
3899 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3902 bmap->extra -= bmap->n_div;
3904 bmap = isl_basic_map_finalize(bmap);
3905 return (struct isl_basic_set *)bmap;
3910 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3911 __isl_take isl_basic_set *bset)
3913 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3916 struct isl_basic_map *isl_basic_map_overlying_set(
3917 struct isl_basic_set *bset, struct isl_basic_map *like)
3919 struct isl_basic_map *bmap;
3920 struct isl_ctx *ctx;
3927 isl_assert(ctx, bset->n_div == 0, goto error);
3928 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3929 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3931 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3932 isl_basic_map_free(like);
3933 return (struct isl_basic_map *)bset;
3935 bset = isl_basic_set_cow(bset);
3938 total = bset->dim->n_out + bset->extra;
3939 bmap = (struct isl_basic_map *)bset;
3940 isl_space_free(bmap->dim);
3941 bmap->dim = isl_space_copy(like->dim);
3944 bmap->n_div = like->n_div;
3945 bmap->extra += like->n_div;
3949 ltotal = total - bmap->extra + like->extra;
3952 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3953 bmap->extra * (1 + 1 + total));
3954 if (isl_blk_is_error(bmap->block2))
3956 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3960 for (i = 0; i < bmap->extra; ++i)
3961 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3962 for (i = 0; i < like->n_div; ++i) {
3963 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3964 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3966 bmap = isl_basic_map_extend_constraints(bmap,
3967 0, 2 * like->n_div);
3968 for (i = 0; i < like->n_div; ++i) {
3969 if (isl_int_is_zero(bmap->div[i][0]))
3971 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3975 isl_basic_map_free(like);
3976 bmap = isl_basic_map_simplify(bmap);
3977 bmap = isl_basic_map_finalize(bmap);
3980 isl_basic_map_free(like);
3981 isl_basic_set_free(bset);
3985 struct isl_basic_set *isl_basic_set_from_underlying_set(
3986 struct isl_basic_set *bset, struct isl_basic_set *like)
3988 return (struct isl_basic_set *)
3989 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3992 struct isl_set *isl_set_from_underlying_set(
3993 struct isl_set *set, struct isl_basic_set *like)
3999 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4001 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4002 isl_basic_set_free(like);
4005 set = isl_set_cow(set);
4008 for (i = 0; i < set->n; ++i) {
4009 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4010 isl_basic_set_copy(like));
4014 isl_space_free(set->dim);
4015 set->dim = isl_space_copy(like->dim);
4018 isl_basic_set_free(like);
4021 isl_basic_set_free(like);
4026 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4030 map = isl_map_cow(map);
4033 map->dim = isl_space_cow(map->dim);
4037 for (i = 1; i < map->n; ++i)
4038 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4040 for (i = 0; i < map->n; ++i) {
4041 map->p[i] = (struct isl_basic_map *)
4042 isl_basic_map_underlying_set(map->p[i]);
4047 map->dim = isl_space_underlying(map->dim, 0);
4049 isl_space_free(map->dim);
4050 map->dim = isl_space_copy(map->p[0]->dim);
4054 return (struct isl_set *)map;
4060 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4062 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4065 __isl_give isl_basic_map *isl_basic_map_reset_space(
4066 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4068 bmap = isl_basic_map_cow(bmap);
4072 isl_space_free(bmap->dim);
4075 bmap = isl_basic_map_finalize(bmap);
4079 isl_basic_map_free(bmap);
4080 isl_space_free(dim);
4084 __isl_give isl_basic_set *isl_basic_set_reset_space(
4085 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4087 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4091 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4092 __isl_take isl_space *dim)
4096 map = isl_map_cow(map);
4100 for (i = 0; i < map->n; ++i) {
4101 map->p[i] = isl_basic_map_reset_space(map->p[i],
4102 isl_space_copy(dim));
4106 isl_space_free(map->dim);
4112 isl_space_free(dim);
4116 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4117 __isl_take isl_space *dim)
4119 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4122 /* Compute the parameter domain of the given basic set.
4124 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4129 if (isl_basic_set_is_params(bset))
4132 n = isl_basic_set_dim(bset, isl_dim_set);
4133 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4134 space = isl_basic_set_get_space(bset);
4135 space = isl_space_params(space);
4136 bset = isl_basic_set_reset_space(bset, space);
4140 /* Compute the parameter domain of the given set.
4142 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4147 if (isl_set_is_params(set))
4150 n = isl_set_dim(set, isl_dim_set);
4151 set = isl_set_project_out(set, isl_dim_set, 0, n);
4152 space = isl_set_get_space(set);
4153 space = isl_space_params(space);
4154 set = isl_set_reset_space(set, space);
4158 /* Construct a zero-dimensional set with the given parameter domain.
4160 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4163 space = isl_set_get_space(set);
4164 space = isl_space_set_from_params(space);
4165 set = isl_set_reset_space(set, space);
4169 /* Compute the parameter domain of the given map.
4171 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4176 n = isl_map_dim(map, isl_dim_in);
4177 map = isl_map_project_out(map, isl_dim_in, 0, n);
4178 n = isl_map_dim(map, isl_dim_out);
4179 map = isl_map_project_out(map, isl_dim_out, 0, n);
4180 space = isl_map_get_space(map);
4181 space = isl_space_params(space);
4182 map = isl_map_reset_space(map, space);
4186 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4189 struct isl_basic_set *domain;
4195 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4197 n_in = isl_basic_map_n_in(bmap);
4198 n_out = isl_basic_map_n_out(bmap);
4199 domain = isl_basic_set_from_basic_map(bmap);
4200 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4202 domain = isl_basic_set_reset_space(domain, dim);
4207 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4211 return isl_space_may_be_set(bmap->dim);
4214 /* Is this basic map actually a set?
4215 * Users should never call this function. Outside of isl,
4216 * the type should indicate whether something is a set or a map.
4218 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4222 return isl_space_is_set(bmap->dim);
4225 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4229 if (isl_basic_map_is_set(bmap))
4231 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4234 __isl_give isl_basic_map *isl_basic_map_domain_map(
4235 __isl_take isl_basic_map *bmap)
4239 isl_basic_map *domain;
4240 int nparam, n_in, n_out;
4243 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4244 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4245 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4247 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4248 domain = isl_basic_map_universe(dim);
4250 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4251 bmap = isl_basic_map_apply_range(bmap, domain);
4252 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4254 total = isl_basic_map_total_dim(bmap);
4256 for (i = 0; i < n_in; ++i) {
4257 k = isl_basic_map_alloc_equality(bmap);
4260 isl_seq_clr(bmap->eq[k], 1 + total);
4261 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4262 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4265 bmap = isl_basic_map_gauss(bmap, NULL);
4266 return isl_basic_map_finalize(bmap);
4268 isl_basic_map_free(bmap);
4272 __isl_give isl_basic_map *isl_basic_map_range_map(
4273 __isl_take isl_basic_map *bmap)
4277 isl_basic_map *range;
4278 int nparam, n_in, n_out;
4281 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4282 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4283 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4285 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4286 range = isl_basic_map_universe(dim);
4288 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4289 bmap = isl_basic_map_apply_range(bmap, range);
4290 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4292 total = isl_basic_map_total_dim(bmap);
4294 for (i = 0; i < n_out; ++i) {
4295 k = isl_basic_map_alloc_equality(bmap);
4298 isl_seq_clr(bmap->eq[k], 1 + total);
4299 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4300 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4303 bmap = isl_basic_map_gauss(bmap, NULL);
4304 return isl_basic_map_finalize(bmap);
4306 isl_basic_map_free(bmap);
4310 int isl_map_may_be_set(__isl_keep isl_map *map)
4314 return isl_space_may_be_set(map->dim);
4317 /* Is this map actually a set?
4318 * Users should never call this function. Outside of isl,
4319 * the type should indicate whether something is a set or a map.
4321 int isl_map_is_set(__isl_keep isl_map *map)
4325 return isl_space_is_set(map->dim);
4328 struct isl_set *isl_map_range(struct isl_map *map)
4331 struct isl_set *set;
4335 if (isl_map_is_set(map))
4336 return (isl_set *)map;
4338 map = isl_map_cow(map);
4342 set = (struct isl_set *) map;
4343 set->dim = isl_space_range(set->dim);
4346 for (i = 0; i < map->n; ++i) {
4347 set->p[i] = isl_basic_map_range(map->p[i]);
4351 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4352 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4359 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4362 isl_space *domain_dim;
4364 map = isl_map_cow(map);
4368 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4369 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4370 map->dim = isl_space_join(map->dim, domain_dim);
4373 for (i = 0; i < map->n; ++i) {
4374 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4378 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4379 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4386 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4389 isl_space *range_dim;
4391 map = isl_map_cow(map);
4395 range_dim = isl_space_range(isl_map_get_space(map));
4396 range_dim = isl_space_from_range(range_dim);
4397 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4398 map->dim = isl_space_join(map->dim, range_dim);
4401 for (i = 0; i < map->n; ++i) {
4402 map->p[i] = isl_basic_map_range_map(map->p[i]);
4406 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4407 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4414 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4415 __isl_take isl_space *dim)
4418 struct isl_map *map = NULL;
4420 set = isl_set_cow(set);
4423 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4424 map = (struct isl_map *)set;
4425 for (i = 0; i < set->n; ++i) {
4426 map->p[i] = isl_basic_map_from_basic_set(
4427 set->p[i], isl_space_copy(dim));
4431 isl_space_free(map->dim);
4435 isl_space_free(dim);
4440 __isl_give isl_basic_map *isl_basic_map_from_domain(
4441 __isl_take isl_basic_set *bset)
4443 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4446 __isl_give isl_basic_map *isl_basic_map_from_range(
4447 __isl_take isl_basic_set *bset)
4450 space = isl_basic_set_get_space(bset);
4451 space = isl_space_from_range(space);
4452 bset = isl_basic_set_reset_space(bset, space);
4453 return (isl_basic_map *)bset;
4456 struct isl_map *isl_map_from_range(struct isl_set *set)
4459 space = isl_set_get_space(set);
4460 space = isl_space_from_range(space);
4461 set = isl_set_reset_space(set, space);
4462 return (struct isl_map *)set;
4465 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4467 return isl_map_reverse(isl_map_from_range(set));
4470 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4471 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4473 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4476 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4477 __isl_take isl_set *range)
4479 return isl_map_apply_range(isl_map_reverse(domain), range);
4482 struct isl_set *isl_set_from_map(struct isl_map *map)
4485 struct isl_set *set = NULL;
4489 map = isl_map_cow(map);
4492 map->dim = isl_space_as_set_space(map->dim);
4495 set = (struct isl_set *)map;
4496 for (i = 0; i < map->n; ++i) {
4497 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4507 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4510 struct isl_map *map;
4515 isl_die(dim->ctx, isl_error_internal,
4516 "negative number of basic maps", goto error);
4517 map = isl_alloc(dim->ctx, struct isl_map,
4518 sizeof(struct isl_map) +
4519 (n - 1) * sizeof(struct isl_basic_map *));
4523 map->ctx = dim->ctx;
4524 isl_ctx_ref(map->ctx);
4532 isl_space_free(dim);
4536 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4537 unsigned nparam, unsigned in, unsigned out, int n,
4540 struct isl_map *map;
4543 dims = isl_space_alloc(ctx, nparam, in, out);
4547 map = isl_map_alloc_space(dims, n, flags);
4551 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4553 struct isl_basic_map *bmap;
4554 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4555 bmap = isl_basic_map_set_to_empty(bmap);
4559 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4561 struct isl_basic_set *bset;
4562 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4563 bset = isl_basic_set_set_to_empty(bset);
4567 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4569 struct isl_basic_map *bmap;
4572 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4573 bmap = isl_basic_map_set_to_empty(bmap);
4577 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4579 struct isl_basic_map *bmap;
4582 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4583 bmap = isl_basic_map_set_to_empty(bmap);
4587 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4589 struct isl_basic_set *bset;
4592 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4593 bset = isl_basic_set_set_to_empty(bset);
4597 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4599 struct isl_basic_map *bmap;
4600 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4601 bmap = isl_basic_map_finalize(bmap);
4605 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4607 struct isl_basic_set *bset;
4608 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4609 bset = isl_basic_set_finalize(bset);
4613 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4616 unsigned total = isl_space_dim(dim, isl_dim_all);
4617 isl_basic_map *bmap;
4619 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4620 for (i = 0; i < total; ++i) {
4621 int k = isl_basic_map_alloc_inequality(bmap);
4624 isl_seq_clr(bmap->ineq[k], 1 + total);
4625 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4629 isl_basic_map_free(bmap);
4633 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4635 return isl_basic_map_nat_universe(dim);
4638 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4640 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4643 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4645 return isl_map_nat_universe(dim);
4648 __isl_give isl_basic_map *isl_basic_map_universe_like(
4649 __isl_keep isl_basic_map *model)
4653 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4656 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4660 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4663 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4664 __isl_keep isl_set *model)
4668 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4671 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4673 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4676 struct isl_map *isl_map_empty_like(struct isl_map *model)
4680 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4683 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4687 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4690 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4692 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4695 struct isl_set *isl_set_empty_like(struct isl_set *model)
4699 return isl_set_empty(isl_space_copy(model->dim));
4702 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4704 struct isl_map *map;
4707 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4708 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4712 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4714 struct isl_set *set;
4717 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4718 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4722 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4726 return isl_set_universe(isl_space_copy(model->dim));
4729 struct isl_map *isl_map_dup(struct isl_map *map)
4732 struct isl_map *dup;
4736 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4737 for (i = 0; i < map->n; ++i)
4738 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4742 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4743 __isl_take isl_basic_map *bmap)
4747 if (isl_basic_map_plain_is_empty(bmap)) {
4748 isl_basic_map_free(bmap);
4751 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4752 isl_assert(map->ctx, map->n < map->size, goto error);
4753 map->p[map->n] = bmap;
4755 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4761 isl_basic_map_free(bmap);
4765 void isl_map_free(struct isl_map *map)
4775 isl_ctx_deref(map->ctx);
4776 for (i = 0; i < map->n; ++i)
4777 isl_basic_map_free(map->p[i]);
4778 isl_space_free(map->dim);
4782 struct isl_map *isl_map_extend(struct isl_map *base,
4783 unsigned nparam, unsigned n_in, unsigned n_out)
4787 base = isl_map_cow(base);
4791 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4794 for (i = 0; i < base->n; ++i) {
4795 base->p[i] = isl_basic_map_extend_space(base->p[i],
4796 isl_space_copy(base->dim), 0, 0, 0);
4806 struct isl_set *isl_set_extend(struct isl_set *base,
4807 unsigned nparam, unsigned dim)
4809 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4813 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4814 struct isl_basic_map *bmap, unsigned pos, int value)
4818 bmap = isl_basic_map_cow(bmap);
4819 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4820 j = isl_basic_map_alloc_equality(bmap);
4823 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4824 isl_int_set_si(bmap->eq[j][pos], -1);
4825 isl_int_set_si(bmap->eq[j][0], value);
4826 bmap = isl_basic_map_simplify(bmap);
4827 return isl_basic_map_finalize(bmap);
4829 isl_basic_map_free(bmap);
4833 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4834 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4838 bmap = isl_basic_map_cow(bmap);
4839 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4840 j = isl_basic_map_alloc_equality(bmap);
4843 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4844 isl_int_set_si(bmap->eq[j][pos], -1);
4845 isl_int_set(bmap->eq[j][0], value);
4846 bmap = isl_basic_map_simplify(bmap);
4847 return isl_basic_map_finalize(bmap);
4849 isl_basic_map_free(bmap);
4853 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4854 enum isl_dim_type type, unsigned pos, int value)
4858 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4859 return isl_basic_map_fix_pos_si(bmap,
4860 isl_basic_map_offset(bmap, type) + pos, value);
4862 isl_basic_map_free(bmap);
4866 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4867 enum isl_dim_type type, unsigned pos, isl_int value)
4871 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4872 return isl_basic_map_fix_pos(bmap,
4873 isl_basic_map_offset(bmap, type) + pos, value);
4875 isl_basic_map_free(bmap);
4879 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4880 enum isl_dim_type type, unsigned pos, int value)
4882 return (struct isl_basic_set *)
4883 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4887 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4888 enum isl_dim_type type, unsigned pos, isl_int value)
4890 return (struct isl_basic_set *)
4891 isl_basic_map_fix((struct isl_basic_map *)bset,
4895 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4896 unsigned input, int value)
4898 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4901 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4902 unsigned dim, int value)
4904 return (struct isl_basic_set *)
4905 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4906 isl_dim_set, dim, value);
4909 struct isl_map *isl_map_fix_si(struct isl_map *map,
4910 enum isl_dim_type type, unsigned pos, int value)
4914 map = isl_map_cow(map);
4918 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4919 for (i = 0; i < map->n; ++i) {
4920 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4924 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4931 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4932 enum isl_dim_type type, unsigned pos, int value)
4934 return (struct isl_set *)
4935 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4938 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4939 enum isl_dim_type type, unsigned pos, isl_int value)
4943 map = isl_map_cow(map);
4947 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4948 for (i = 0; i < map->n; ++i) {
4949 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4953 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4960 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4961 enum isl_dim_type type, unsigned pos, isl_int value)
4963 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4966 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4967 unsigned input, int value)
4969 return isl_map_fix_si(map, isl_dim_in, input, value);
4972 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4974 return (struct isl_set *)
4975 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4978 static __isl_give isl_basic_map *basic_map_bound_si(
4979 __isl_take isl_basic_map *bmap,
4980 enum isl_dim_type type, unsigned pos, int value, int upper)
4986 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4987 pos += isl_basic_map_offset(bmap, type);
4988 bmap = isl_basic_map_cow(bmap);
4989 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4990 j = isl_basic_map_alloc_inequality(bmap);
4993 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4995 isl_int_set_si(bmap->ineq[j][pos], -1);
4996 isl_int_set_si(bmap->ineq[j][0], value);
4998 isl_int_set_si(bmap->ineq[j][pos], 1);
4999 isl_int_set_si(bmap->ineq[j][0], -value);
5001 bmap = isl_basic_map_simplify(bmap);
5002 return isl_basic_map_finalize(bmap);
5004 isl_basic_map_free(bmap);
5008 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5009 __isl_take isl_basic_map *bmap,
5010 enum isl_dim_type type, unsigned pos, int value)
5012 return basic_map_bound_si(bmap, type, pos, value, 0);
5015 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5016 unsigned dim, isl_int value)
5020 bset = isl_basic_set_cow(bset);
5021 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5022 j = isl_basic_set_alloc_inequality(bset);
5025 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5026 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5027 isl_int_neg(bset->ineq[j][0], value);
5028 bset = isl_basic_set_simplify(bset);
5029 return isl_basic_set_finalize(bset);
5031 isl_basic_set_free(bset);
5035 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5036 enum isl_dim_type type, unsigned pos, int value, int upper)
5040 map = isl_map_cow(map);
5044 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5045 for (i = 0; i < map->n; ++i) {
5046 map->p[i] = basic_map_bound_si(map->p[i],
5047 type, pos, value, upper);
5051 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5058 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5059 enum isl_dim_type type, unsigned pos, int value)
5061 return map_bound_si(map, type, pos, value, 0);
5064 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5065 enum isl_dim_type type, unsigned pos, int value)
5067 return map_bound_si(map, type, pos, value, 1);
5070 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5071 enum isl_dim_type type, unsigned pos, int value)
5073 return (struct isl_set *)
5074 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5077 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5078 enum isl_dim_type type, unsigned pos, int value)
5080 return isl_map_upper_bound_si(set, type, pos, value);
5083 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5088 set = isl_set_cow(set);
5092 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5093 for (i = 0; i < set->n; ++i) {
5094 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5104 struct isl_map *isl_map_reverse(struct isl_map *map)
5108 map = isl_map_cow(map);
5112 map->dim = isl_space_reverse(map->dim);
5115 for (i = 0; i < map->n; ++i) {
5116 map->p[i] = isl_basic_map_reverse(map->p[i]);
5120 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5127 static struct isl_map *isl_basic_map_partial_lexopt(
5128 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5129 struct isl_set **empty, int max)
5133 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5134 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5136 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5138 isl_basic_map_free(bmap);
5139 isl_basic_set_free(dom);
5145 struct isl_map *isl_basic_map_partial_lexmax(
5146 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5147 struct isl_set **empty)
5149 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5152 struct isl_map *isl_basic_map_partial_lexmin(
5153 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5154 struct isl_set **empty)
5156 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5159 struct isl_set *isl_basic_set_partial_lexmin(
5160 struct isl_basic_set *bset, struct isl_basic_set *dom,
5161 struct isl_set **empty)
5163 return (struct isl_set *)
5164 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5168 struct isl_set *isl_basic_set_partial_lexmax(
5169 struct isl_basic_set *bset, struct isl_basic_set *dom,
5170 struct isl_set **empty)
5172 return (struct isl_set *)
5173 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5177 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5178 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5179 __isl_give isl_set **empty)
5181 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5184 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5185 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5186 __isl_give isl_set **empty)
5188 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5191 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5192 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5193 __isl_give isl_set **empty)
5195 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5198 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5199 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5200 __isl_give isl_set **empty)
5202 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5205 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5206 __isl_take isl_basic_map *bmap, int max)
5208 isl_basic_set *dom = NULL;
5209 isl_space *dom_space;
5213 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5214 dom = isl_basic_set_universe(dom_space);
5215 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5217 isl_basic_map_free(bmap);
5221 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5222 __isl_take isl_basic_map *bmap)
5224 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5227 /* Given a basic map "bmap", compute the lexicographically minimal
5228 * (or maximal) image element for each domain element in dom.
5229 * Set *empty to those elements in dom that do not have an image element.
5231 * We first make sure the basic sets in dom are disjoint and then
5232 * simply collect the results over each of the basic sets separately.
5233 * We could probably improve the efficiency a bit by moving the union
5234 * domain down into the parametric integer programming.
5236 static __isl_give isl_map *basic_map_partial_lexopt(
5237 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5238 __isl_give isl_set **empty, int max)
5241 struct isl_map *res;
5243 dom = isl_set_make_disjoint(dom);
5247 if (isl_set_plain_is_empty(dom)) {
5248 res = isl_map_empty_like_basic_map(bmap);
5249 *empty = isl_set_empty_like(dom);
5251 isl_basic_map_free(bmap);
5255 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5256 isl_basic_set_copy(dom->p[0]), empty, max);
5258 for (i = 1; i < dom->n; ++i) {
5259 struct isl_map *res_i;
5260 struct isl_set *empty_i;
5262 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5263 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5265 res = isl_map_union_disjoint(res, res_i);
5266 *empty = isl_set_union_disjoint(*empty, empty_i);
5270 isl_basic_map_free(bmap);
5275 isl_basic_map_free(bmap);
5279 /* Given a map "map", compute the lexicographically minimal
5280 * (or maximal) image element for each domain element in dom.
5281 * Set *empty to those elements in dom that do not have an image element.
5283 * We first compute the lexicographically minimal or maximal element
5284 * in the first basic map. This results in a partial solution "res"
5285 * and a subset "todo" of dom that still need to be handled.
5286 * We then consider each of the remaining maps in "map" and successively
5287 * improve both "res" and "todo".
5289 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5290 * Assume we are computing the lexicographical maximum.
5291 * We first compute the lexicographically maximal element in basic map i.
5292 * This results in a partial solution res_i and a subset todo_i.
5293 * Then we combine these results with those obtain for the first k basic maps
5294 * to obtain a result that is valid for the first k+1 basic maps.
5295 * In particular, the set where there is no solution is the set where
5296 * there is no solution for the first k basic maps and also no solution
5297 * for the ith basic map, i.e.,
5299 * todo^i = todo^k * todo_i
5301 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5302 * solutions, arbitrarily breaking ties in favor of res^k.
5303 * That is, when res^k(a) >= res_i(a), we pick res^k and
5304 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5305 * the lexicographic order.)
5306 * In practice, we compute
5308 * res^k * (res_i . "<=")
5312 * res_i * (res^k . "<")
5314 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5315 * where only one of res^k and res_i provides a solution and we simply pick
5322 * Note that we only compute these intersections when dom(res^k) intersects
5323 * dom(res_i). Otherwise, the only effect of these intersections is to
5324 * potentially break up res^k and res_i into smaller pieces.
5325 * We want to avoid such splintering as much as possible.
5326 * In fact, an earlier implementation of this function would look for
5327 * better results in the domain of res^k and for extra results in todo^k,
5328 * but this would always result in a splintering according to todo^k,
5329 * even when the domain of basic map i is disjoint from the domains of
5330 * the previous basic maps.
5332 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5333 __isl_take isl_map *map, __isl_take isl_set *dom,
5334 __isl_give isl_set **empty, int max)
5337 struct isl_map *res;
5338 struct isl_set *todo;
5343 if (isl_map_plain_is_empty(map)) {
5351 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5352 isl_set_copy(dom), &todo, max);
5354 for (i = 1; i < map->n; ++i) {
5358 isl_space *dim = isl_space_range(isl_map_get_space(res));
5360 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5361 isl_set_copy(dom), &todo_i, max);
5364 lt = isl_map_lex_lt(isl_space_copy(dim));
5365 le = isl_map_lex_le(dim);
5367 lt = isl_map_lex_gt(isl_space_copy(dim));
5368 le = isl_map_lex_ge(dim);
5370 lt = isl_map_apply_range(isl_map_copy(res), lt);
5371 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5372 le = isl_map_apply_range(isl_map_copy(res_i), le);
5373 le = isl_map_intersect(le, isl_map_copy(res));
5375 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5376 res = isl_map_intersect_domain(res,
5377 isl_set_copy(todo_i));
5378 res_i = isl_map_intersect_domain(res_i,
5379 isl_set_copy(todo));
5382 res = isl_map_union_disjoint(res, res_i);
5383 res = isl_map_union_disjoint(res, lt);
5384 res = isl_map_union_disjoint(res, le);
5386 todo = isl_set_intersect(todo, todo_i);
5406 /* Given a map "map", compute the lexicographically minimal
5407 * (or maximal) image element for each domain element in dom.
5408 * Set *empty to those elements in dom that do not have an image element.
5410 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5412 static __isl_give isl_map *isl_map_partial_lexopt(
5413 __isl_take isl_map *map, __isl_take isl_set *dom,
5414 __isl_give isl_set **empty, int max)
5418 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5419 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5420 if (!isl_space_has_named_params(map->dim) ||
5421 !isl_space_has_named_params(dom->dim))
5422 isl_die(map->ctx, isl_error_invalid,
5423 "unaligned unnamed parameters", goto error);
5424 map = isl_map_align_params(map, isl_map_get_space(dom));
5425 dom = isl_map_align_params(dom, isl_map_get_space(map));
5426 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5435 __isl_give isl_map *isl_map_partial_lexmax(
5436 __isl_take isl_map *map, __isl_take isl_set *dom,
5437 __isl_give isl_set **empty)
5439 return isl_map_partial_lexopt(map, dom, empty, 1);
5442 __isl_give isl_map *isl_map_partial_lexmin(
5443 __isl_take isl_map *map, __isl_take isl_set *dom,
5444 __isl_give isl_set **empty)
5446 return isl_map_partial_lexopt(map, dom, empty, 0);
5449 __isl_give isl_set *isl_set_partial_lexmin(
5450 __isl_take isl_set *set, __isl_take isl_set *dom,
5451 __isl_give isl_set **empty)
5453 return (struct isl_set *)
5454 isl_map_partial_lexmin((struct isl_map *)set,
5458 __isl_give isl_set *isl_set_partial_lexmax(
5459 __isl_take isl_set *set, __isl_take isl_set *dom,
5460 __isl_give isl_set **empty)
5462 return (struct isl_set *)
5463 isl_map_partial_lexmax((struct isl_map *)set,
5467 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5469 struct isl_basic_set *dom = NULL;
5474 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5475 dom = isl_basic_set_universe(dom_dim);
5476 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5478 isl_basic_map_free(bmap);
5482 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5484 return isl_basic_map_lexopt(bmap, 0);
5487 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5489 return isl_basic_map_lexopt(bmap, 1);
5492 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5494 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5497 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5499 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5502 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5504 struct isl_set *dom = NULL;
5509 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5510 dom = isl_set_universe(dom_dim);
5511 return isl_map_partial_lexopt(map, dom, NULL, max);
5517 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5519 return isl_map_lexopt(map, 0);
5522 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5524 return isl_map_lexopt(map, 1);
5527 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5529 return (isl_set *)isl_map_lexmin((isl_map *)set);
5532 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5534 return (isl_set *)isl_map_lexmax((isl_map *)set);
5537 /* Extract the first and only affine expression from list
5538 * and then add it to *pwaff with the given dom.
5539 * This domain is known to be disjoint from other domains
5540 * because of the way isl_basic_map_foreach_lexmax works.
5542 static int update_dim_opt(__isl_take isl_basic_set *dom,
5543 __isl_take isl_aff_list *list, void *user)
5545 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5547 isl_pw_aff **pwaff = user;
5548 isl_pw_aff *pwaff_i;
5550 if (isl_aff_list_n_aff(list) != 1)
5551 isl_die(ctx, isl_error_internal,
5552 "expecting single element list", goto error);
5554 aff = isl_aff_list_get_aff(list, 0);
5555 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5557 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5559 isl_aff_list_free(list);
5563 isl_basic_set_free(dom);
5564 isl_aff_list_free(list);
5568 /* Given a basic map with one output dimension, compute the minimum or
5569 * maximum of that dimension as an isl_pw_aff.
5571 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5572 * call update_dim_opt on each leaf of the result.
5574 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5577 isl_space *dim = isl_basic_map_get_space(bmap);
5581 dim = isl_space_from_domain(isl_space_domain(dim));
5582 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5583 pwaff = isl_pw_aff_empty(dim);
5585 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5587 return isl_pw_aff_free(pwaff);
5592 /* Compute the minimum or maximum of the given output dimension
5593 * as a function of the parameters and the input dimensions,
5594 * but independently of the other output dimensions.
5596 * We first project out the other output dimension and then compute
5597 * the "lexicographic" maximum in each basic map, combining the results
5598 * using isl_pw_aff_union_max.
5600 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5607 n_out = isl_map_dim(map, isl_dim_out);
5608 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5609 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5614 isl_space *dim = isl_map_get_space(map);
5615 dim = isl_space_domain(isl_space_from_range(dim));
5617 return isl_pw_aff_empty(dim);
5620 pwaff = basic_map_dim_opt(map->p[0], max);
5621 for (i = 1; i < map->n; ++i) {
5622 isl_pw_aff *pwaff_i;
5624 pwaff_i = basic_map_dim_opt(map->p[i], max);
5625 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5633 /* Compute the maximum of the given output dimension as a function of the
5634 * parameters and input dimensions, but independently of
5635 * the other output dimensions.
5637 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5639 return map_dim_opt(map, pos, 1);
5642 /* Compute the minimum or maximum of the given set dimension
5643 * as a function of the parameters,
5644 * but independently of the other set dimensions.
5646 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5649 return map_dim_opt(set, pos, max);
5652 /* Compute the maximum of the given set dimension as a function of the
5653 * parameters, but independently of the other set dimensions.
5655 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5657 return set_dim_opt(set, pos, 1);
5660 /* Compute the minimum of the given set dimension as a function of the
5661 * parameters, but independently of the other set dimensions.
5663 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5665 return set_dim_opt(set, pos, 0);
5668 /* Apply a preimage specified by "mat" on the parameters of "bset".
5669 * bset is assumed to have only parameters and divs.
5671 static struct isl_basic_set *basic_set_parameter_preimage(
5672 struct isl_basic_set *bset, struct isl_mat *mat)
5679 bset->dim = isl_space_cow(bset->dim);
5683 nparam = isl_basic_set_dim(bset, isl_dim_param);
5685 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5687 bset->dim->nparam = 0;
5688 bset->dim->n_out = nparam;
5689 bset = isl_basic_set_preimage(bset, mat);
5691 bset->dim->nparam = bset->dim->n_out;
5692 bset->dim->n_out = 0;
5697 isl_basic_set_free(bset);
5701 /* Apply a preimage specified by "mat" on the parameters of "set".
5702 * set is assumed to have only parameters and divs.
5704 static struct isl_set *set_parameter_preimage(
5705 struct isl_set *set, struct isl_mat *mat)
5707 isl_space *dim = NULL;
5713 dim = isl_space_copy(set->dim);
5714 dim = isl_space_cow(dim);
5718 nparam = isl_set_dim(set, isl_dim_param);
5720 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5723 dim->n_out = nparam;
5724 isl_set_reset_space(set, dim);
5725 set = isl_set_preimage(set, mat);
5728 dim = isl_space_copy(set->dim);
5729 dim = isl_space_cow(dim);
5732 dim->nparam = dim->n_out;
5734 isl_set_reset_space(set, dim);
5737 isl_space_free(dim);
5744 /* Intersect the basic set "bset" with the affine space specified by the
5745 * equalities in "eq".
5747 static struct isl_basic_set *basic_set_append_equalities(
5748 struct isl_basic_set *bset, struct isl_mat *eq)
5756 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5761 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5762 for (i = 0; i < eq->n_row; ++i) {
5763 k = isl_basic_set_alloc_equality(bset);
5766 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5767 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5771 bset = isl_basic_set_gauss(bset, NULL);
5772 bset = isl_basic_set_finalize(bset);
5777 isl_basic_set_free(bset);
5781 /* Intersect the set "set" with the affine space specified by the
5782 * equalities in "eq".
5784 static struct isl_set *set_append_equalities(struct isl_set *set,
5792 for (i = 0; i < set->n; ++i) {
5793 set->p[i] = basic_set_append_equalities(set->p[i],
5806 /* Project the given basic set onto its parameter domain, possibly introducing
5807 * new, explicit, existential variables in the constraints.
5808 * The input has parameters and (possibly implicit) existential variables.
5809 * The output has the same parameters, but only
5810 * explicit existentially quantified variables.
5812 * The actual projection is performed by pip, but pip doesn't seem
5813 * to like equalities very much, so we first remove the equalities
5814 * among the parameters by performing a variable compression on
5815 * the parameters. Afterward, an inverse transformation is performed
5816 * and the equalities among the parameters are inserted back in.
5818 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5822 struct isl_mat *T, *T2;
5823 struct isl_set *set;
5824 unsigned nparam, n_div;
5826 bset = isl_basic_set_cow(bset);
5830 if (bset->n_eq == 0)
5831 return isl_basic_set_lexmin(bset);
5833 isl_basic_set_gauss(bset, NULL);
5835 nparam = isl_basic_set_dim(bset, isl_dim_param);
5836 n_div = isl_basic_set_dim(bset, isl_dim_div);
5838 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5839 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5842 if (i == bset->n_eq)
5843 return isl_basic_set_lexmin(bset);
5845 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5847 eq = isl_mat_cow(eq);
5848 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5849 if (T && T->n_col == 0) {
5853 bset = isl_basic_set_set_to_empty(bset);
5854 return isl_set_from_basic_set(bset);
5856 bset = basic_set_parameter_preimage(bset, T);
5858 set = isl_basic_set_lexmin(bset);
5859 set = set_parameter_preimage(set, T2);
5860 set = set_append_equalities(set, eq);
5864 /* Compute an explicit representation for all the existentially
5865 * quantified variables.
5866 * The input and output dimensions are first turned into parameters.
5867 * compute_divs then returns a map with the same parameters and
5868 * no input or output dimensions and the dimension specification
5869 * is reset to that of the input.
5871 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5873 struct isl_basic_set *bset;
5874 struct isl_set *set;
5875 struct isl_map *map;
5876 isl_space *dim, *orig_dim = NULL;
5881 bmap = isl_basic_map_cow(bmap);
5885 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5886 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5887 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5888 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5892 orig_dim = bmap->dim;
5894 bset = (struct isl_basic_set *)bmap;
5896 set = parameter_compute_divs(bset);
5897 map = (struct isl_map *)set;
5898 map = isl_map_reset_space(map, orig_dim);
5902 isl_basic_map_free(bmap);
5906 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5914 off = isl_space_dim(bmap->dim, isl_dim_all);
5915 for (i = 0; i < bmap->n_div; ++i) {
5916 if (isl_int_is_zero(bmap->div[i][0]))
5918 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5924 static int map_divs_known(__isl_keep isl_map *map)
5931 for (i = 0; i < map->n; ++i) {
5932 int known = isl_basic_map_divs_known(map->p[i]);
5940 /* If bmap contains any unknown divs, then compute explicit
5941 * expressions for them. However, this computation may be
5942 * quite expensive, so first try to remove divs that aren't
5945 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5948 struct isl_map *map;
5950 known = isl_basic_map_divs_known(bmap);
5954 return isl_map_from_basic_map(bmap);
5956 bmap = isl_basic_map_drop_redundant_divs(bmap);
5958 known = isl_basic_map_divs_known(bmap);
5962 return isl_map_from_basic_map(bmap);
5964 map = compute_divs(bmap);
5967 isl_basic_map_free(bmap);
5971 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5975 struct isl_map *res;
5982 known = map_divs_known(map);
5990 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5991 for (i = 1 ; i < map->n; ++i) {
5993 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5994 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5995 res = isl_map_union_disjoint(res, r2);
5997 res = isl_map_union(res, r2);
6004 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6006 return (struct isl_set *)
6007 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6010 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6012 return (struct isl_set *)
6013 isl_map_compute_divs((struct isl_map *)set);
6016 struct isl_set *isl_map_domain(struct isl_map *map)
6019 struct isl_set *set;
6024 map = isl_map_cow(map);
6028 set = (struct isl_set *)map;
6029 set->dim = isl_space_domain(set->dim);
6032 for (i = 0; i < map->n; ++i) {
6033 set->p[i] = isl_basic_map_domain(map->p[i]);
6037 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6038 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6045 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6046 __isl_take isl_map *map2)
6050 struct isl_map *map = NULL;
6064 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6066 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6067 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6068 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6070 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6071 map1->n + map2->n, flags);
6074 for (i = 0; i < map1->n; ++i) {
6075 map = isl_map_add_basic_map(map,
6076 isl_basic_map_copy(map1->p[i]));
6080 for (i = 0; i < map2->n; ++i) {
6081 map = isl_map_add_basic_map(map,
6082 isl_basic_map_copy(map2->p[i]));
6096 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6097 __isl_take isl_map *map2)
6099 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6102 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6104 map1 = isl_map_union_disjoint(map1, map2);
6108 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6112 struct isl_set *isl_set_union_disjoint(
6113 struct isl_set *set1, struct isl_set *set2)
6115 return (struct isl_set *)
6116 isl_map_union_disjoint(
6117 (struct isl_map *)set1, (struct isl_map *)set2);
6120 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6122 return (struct isl_set *)
6123 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6126 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6127 __isl_take isl_set *set)
6130 struct isl_map *result;
6136 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6137 isl_die(set->ctx, isl_error_invalid,
6138 "parameters don't match", goto error);
6140 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6141 !isl_map_compatible_range(map, set))
6142 isl_die(set->ctx, isl_error_invalid,
6143 "incompatible spaces", goto error);
6145 if (isl_set_plain_is_universe(set)) {
6150 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6151 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6152 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6154 result = isl_map_alloc_space(isl_space_copy(map->dim),
6155 map->n * set->n, flags);
6158 for (i = 0; i < map->n; ++i)
6159 for (j = 0; j < set->n; ++j) {
6160 result = isl_map_add_basic_map(result,
6161 isl_basic_map_intersect_range(
6162 isl_basic_map_copy(map->p[i]),
6163 isl_basic_set_copy(set->p[j])));
6176 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6177 __isl_take isl_set *set)
6179 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6182 struct isl_map *isl_map_intersect_domain(
6183 struct isl_map *map, struct isl_set *set)
6185 return isl_map_reverse(
6186 isl_map_intersect_range(isl_map_reverse(map), set));
6189 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6190 __isl_take isl_map *map2)
6194 map1 = isl_map_reverse(map1);
6195 map1 = isl_map_apply_range(map1, map2);
6196 return isl_map_reverse(map1);
6203 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6204 __isl_take isl_map *map2)
6206 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6209 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6210 __isl_take isl_map *map2)
6212 isl_space *dim_result;
6213 struct isl_map *result;
6219 dim_result = isl_space_join(isl_space_copy(map1->dim),
6220 isl_space_copy(map2->dim));
6222 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6225 for (i = 0; i < map1->n; ++i)
6226 for (j = 0; j < map2->n; ++j) {
6227 result = isl_map_add_basic_map(result,
6228 isl_basic_map_apply_range(
6229 isl_basic_map_copy(map1->p[i]),
6230 isl_basic_map_copy(map2->p[j])));
6236 if (result && result->n <= 1)
6237 ISL_F_SET(result, ISL_MAP_DISJOINT);
6245 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6246 __isl_take isl_map *map2)
6248 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6252 * returns range - domain
6254 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6256 isl_space *dims, *target_dim;
6257 struct isl_basic_set *bset;
6264 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6265 bmap->dim, isl_dim_out),
6267 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6268 dim = isl_basic_map_n_in(bmap);
6269 nparam = isl_basic_map_n_param(bmap);
6270 bset = isl_basic_set_from_basic_map(bmap);
6271 bset = isl_basic_set_cow(bset);
6272 dims = isl_basic_set_get_space(bset);
6273 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6274 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6275 bset = isl_basic_set_swap_vars(bset, 2*dim);
6276 for (i = 0; i < dim; ++i) {
6277 int j = isl_basic_map_alloc_equality(
6278 (struct isl_basic_map *)bset);
6281 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6282 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6283 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6284 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6286 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6287 bset = isl_basic_set_reset_space(bset, target_dim);
6290 isl_basic_map_free(bmap);
6295 * returns range - domain
6297 struct isl_set *isl_map_deltas(struct isl_map *map)
6301 struct isl_set *result;
6306 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6307 map->dim, isl_dim_out),
6309 dim = isl_map_get_space(map);
6310 dim = isl_space_domain(dim);
6311 result = isl_set_alloc_space(dim, map->n, 0);
6314 for (i = 0; i < map->n; ++i)
6315 result = isl_set_add_basic_set(result,
6316 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6325 * returns [domain -> range] -> range - domain
6327 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6328 __isl_take isl_basic_map *bmap)
6332 isl_basic_map *domain;
6336 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6337 isl_die(bmap->ctx, isl_error_invalid,
6338 "domain and range don't match", goto error);
6340 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6341 n = isl_basic_map_dim(bmap, isl_dim_in);
6343 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6344 domain = isl_basic_map_universe(dim);
6346 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6347 bmap = isl_basic_map_apply_range(bmap, domain);
6348 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6350 total = isl_basic_map_total_dim(bmap);
6352 for (i = 0; i < n; ++i) {
6353 k = isl_basic_map_alloc_equality(bmap);
6356 isl_seq_clr(bmap->eq[k], 1 + total);
6357 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6358 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6359 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6362 bmap = isl_basic_map_gauss(bmap, NULL);
6363 return isl_basic_map_finalize(bmap);
6365 isl_basic_map_free(bmap);
6370 * returns [domain -> range] -> range - domain
6372 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6375 isl_space *domain_dim;
6380 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6381 isl_die(map->ctx, isl_error_invalid,
6382 "domain and range don't match", goto error);
6384 map = isl_map_cow(map);
6388 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6389 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6390 map->dim = isl_space_join(map->dim, domain_dim);
6393 for (i = 0; i < map->n; ++i) {
6394 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6398 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6405 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6407 struct isl_basic_map *bmap;
6415 nparam = dims->nparam;
6417 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6421 for (i = 0; i < dim; ++i) {
6422 int j = isl_basic_map_alloc_equality(bmap);
6425 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6426 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6427 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6429 return isl_basic_map_finalize(bmap);
6431 isl_basic_map_free(bmap);
6435 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6439 if (dim->n_in != dim->n_out)
6440 isl_die(dim->ctx, isl_error_invalid,
6441 "number of input and output dimensions needs to be "
6442 "the same", goto error);
6443 return basic_map_identity(dim);
6445 isl_space_free(dim);
6449 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6451 if (!model || !model->dim)
6453 return isl_basic_map_identity(isl_space_copy(model->dim));
6456 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6458 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6461 struct isl_map *isl_map_identity_like(struct isl_map *model)
6463 if (!model || !model->dim)
6465 return isl_map_identity(isl_space_copy(model->dim));
6468 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6470 if (!model || !model->dim)
6472 return isl_map_identity(isl_space_copy(model->dim));
6475 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6477 isl_space *dim = isl_set_get_space(set);
6479 id = isl_map_identity(isl_space_map_from_set(dim));
6480 return isl_map_intersect_range(id, set);
6483 /* Construct a basic set with all set dimensions having only non-negative
6486 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6487 __isl_take isl_space *space)
6492 struct isl_basic_set *bset;
6496 nparam = space->nparam;
6498 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6501 for (i = 0; i < dim; ++i) {
6502 int k = isl_basic_set_alloc_inequality(bset);
6505 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6506 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6510 isl_basic_set_free(bset);
6514 /* Construct the half-space x_pos >= 0.
6516 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6520 isl_basic_set *nonneg;
6522 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6523 k = isl_basic_set_alloc_inequality(nonneg);
6526 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6527 isl_int_set_si(nonneg->ineq[k][pos], 1);
6529 return isl_basic_set_finalize(nonneg);
6531 isl_basic_set_free(nonneg);
6535 /* Construct the half-space x_pos <= -1.
6537 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6542 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6543 k = isl_basic_set_alloc_inequality(neg);
6546 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6547 isl_int_set_si(neg->ineq[k][0], -1);
6548 isl_int_set_si(neg->ineq[k][pos], -1);
6550 return isl_basic_set_finalize(neg);
6552 isl_basic_set_free(neg);
6556 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6557 enum isl_dim_type type, unsigned first, unsigned n)
6560 isl_basic_set *nonneg;
6568 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6570 for (i = 0; i < n; ++i) {
6571 nonneg = nonneg_halfspace(isl_set_get_space(set),
6572 pos(set->dim, type) + first + i);
6573 neg = neg_halfspace(isl_set_get_space(set),
6574 pos(set->dim, type) + first + i);
6576 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6585 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6586 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6593 if (isl_set_plain_is_empty(set)) {
6598 return fn(set, signs, user);
6601 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6603 half = isl_set_intersect(half, isl_set_copy(set));
6604 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6608 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6610 half = isl_set_intersect(half, set);
6611 return foreach_orthant(half, signs, first + 1, len, fn, user);
6617 /* Call "fn" on the intersections of "set" with each of the orthants
6618 * (except for obviously empty intersections). The orthant is identified
6619 * by the signs array, with each entry having value 1 or -1 according
6620 * to the sign of the corresponding variable.
6622 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6623 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6633 if (isl_set_plain_is_empty(set))
6636 nparam = isl_set_dim(set, isl_dim_param);
6637 nvar = isl_set_dim(set, isl_dim_set);
6639 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6641 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6649 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6651 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6654 int isl_basic_map_is_subset(
6655 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6658 struct isl_map *map1;
6659 struct isl_map *map2;
6661 if (!bmap1 || !bmap2)
6664 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6665 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6667 is_subset = isl_map_is_subset(map1, map2);
6675 int isl_basic_map_is_equal(
6676 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6680 if (!bmap1 || !bmap2)
6682 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6685 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6689 int isl_basic_set_is_equal(
6690 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6692 return isl_basic_map_is_equal(
6693 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6696 int isl_map_is_empty(struct isl_map *map)
6703 for (i = 0; i < map->n; ++i) {
6704 is_empty = isl_basic_map_is_empty(map->p[i]);
6713 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6715 return map ? map->n == 0 : -1;
6718 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6720 return isl_map_plain_is_empty(map);
6723 int isl_set_plain_is_empty(struct isl_set *set)
6725 return set ? set->n == 0 : -1;
6728 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6730 return isl_set_plain_is_empty(set);
6733 int isl_set_is_empty(struct isl_set *set)
6735 return isl_map_is_empty((struct isl_map *)set);
6738 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6743 return isl_space_is_equal(map1->dim, map2->dim);
6746 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6751 return isl_space_is_equal(set1->dim, set2->dim);
6754 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6760 is_subset = isl_map_is_subset(map1, map2);
6763 is_subset = isl_map_is_subset(map2, map1);
6767 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6769 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6772 int isl_basic_map_is_strict_subset(
6773 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6777 if (!bmap1 || !bmap2)
6779 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6782 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6783 if (is_subset == -1)
6788 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6794 is_subset = isl_map_is_subset(map1, map2);
6797 is_subset = isl_map_is_subset(map2, map1);
6798 if (is_subset == -1)
6803 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6805 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6808 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6812 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6815 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6819 return bset->n_eq == 0 && bset->n_ineq == 0;
6822 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6829 for (i = 0; i < map->n; ++i) {
6830 int r = isl_basic_map_is_universe(map->p[i]);
6838 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6840 return isl_map_plain_is_universe((isl_map *) set);
6843 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6845 return isl_set_plain_is_universe(set);
6848 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6850 struct isl_basic_set *bset = NULL;
6851 struct isl_vec *sample = NULL;
6858 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6861 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6862 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6863 copy = isl_basic_map_remove_redundancies(copy);
6864 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6865 isl_basic_map_free(copy);
6869 total = 1 + isl_basic_map_total_dim(bmap);
6870 if (bmap->sample && bmap->sample->size == total) {
6871 int contains = isl_basic_map_contains(bmap, bmap->sample);
6877 isl_vec_free(bmap->sample);
6878 bmap->sample = NULL;
6879 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6882 sample = isl_basic_set_sample_vec(bset);
6885 empty = sample->size == 0;
6886 isl_vec_free(bmap->sample);
6887 bmap->sample = sample;
6889 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6894 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6898 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6901 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6903 return isl_basic_map_plain_is_empty(bmap);
6906 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6910 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6913 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6915 return isl_basic_set_plain_is_empty(bset);
6918 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6920 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6923 struct isl_map *isl_basic_map_union(
6924 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6926 struct isl_map *map;
6927 if (!bmap1 || !bmap2)
6930 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6932 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6935 map = isl_map_add_basic_map(map, bmap1);
6936 map = isl_map_add_basic_map(map, bmap2);
6939 isl_basic_map_free(bmap1);
6940 isl_basic_map_free(bmap2);
6944 struct isl_set *isl_basic_set_union(
6945 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6947 return (struct isl_set *)isl_basic_map_union(
6948 (struct isl_basic_map *)bset1,
6949 (struct isl_basic_map *)bset2);
6952 /* Order divs such that any div only depends on previous divs */
6953 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6961 off = isl_space_dim(bmap->dim, isl_dim_all);
6963 for (i = 0; i < bmap->n_div; ++i) {
6965 if (isl_int_is_zero(bmap->div[i][0]))
6967 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6971 isl_basic_map_swap_div(bmap, i, i + pos);
6977 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6979 return (struct isl_basic_set *)
6980 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6983 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6990 for (i = 0; i < map->n; ++i) {
6991 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7002 /* Apply the expansion computed by isl_merge_divs.
7003 * The expansion itself is given by "exp" while the resulting
7004 * list of divs is given by "div".
7006 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7007 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7012 bset = isl_basic_set_cow(bset);
7016 if (div->n_row < bset->n_div)
7017 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7018 "not an expansion", goto error);
7020 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7021 div->n_row - bset->n_div, 0,
7022 2 * (div->n_row - bset->n_div));
7024 n_div = bset->n_div;
7025 for (i = n_div; i < div->n_row; ++i)
7026 if (isl_basic_set_alloc_div(bset) < 0)
7030 for (i = div->n_row - 1; i >= 0; --i) {
7031 if (j >= 0 && exp[j] == i) {
7033 isl_basic_map_swap_div(bset, i, j);
7036 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7037 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7045 isl_basic_set_free(bset);
7050 /* Look for a div in dst that corresponds to the div "div" in src.
7051 * The divs before "div" in src and dst are assumed to be the same.
7053 * Returns -1 if no corresponding div was found and the position
7054 * of the corresponding div in dst otherwise.
7056 static int find_div(struct isl_basic_map *dst,
7057 struct isl_basic_map *src, unsigned div)
7061 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7063 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7064 for (i = div; i < dst->n_div; ++i)
7065 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7066 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7067 dst->n_div - div) == -1)
7072 struct isl_basic_map *isl_basic_map_align_divs(
7073 struct isl_basic_map *dst, struct isl_basic_map *src)
7076 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7081 if (src->n_div == 0)
7084 for (i = 0; i < src->n_div; ++i)
7085 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7087 src = isl_basic_map_order_divs(src);
7088 dst = isl_basic_map_cow(dst);
7089 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7090 src->n_div, 0, 2 * src->n_div);
7093 for (i = 0; i < src->n_div; ++i) {
7094 int j = find_div(dst, src, i);
7096 j = isl_basic_map_alloc_div(dst);
7099 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7100 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7101 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7105 isl_basic_map_swap_div(dst, i, j);
7109 isl_basic_map_free(dst);
7113 struct isl_basic_set *isl_basic_set_align_divs(
7114 struct isl_basic_set *dst, struct isl_basic_set *src)
7116 return (struct isl_basic_set *)isl_basic_map_align_divs(
7117 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7120 struct isl_map *isl_map_align_divs(struct isl_map *map)
7128 map = isl_map_compute_divs(map);
7129 map = isl_map_cow(map);
7133 for (i = 1; i < map->n; ++i)
7134 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7135 for (i = 1; i < map->n; ++i)
7136 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7138 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7142 struct isl_set *isl_set_align_divs(struct isl_set *set)
7144 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7147 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7148 __isl_take isl_map *map)
7152 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7153 map = isl_map_intersect_domain(map, set);
7154 set = isl_map_range(map);
7162 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7163 __isl_take isl_map *map)
7165 return isl_map_align_params_map_map_and(set, map, &set_apply);
7168 /* There is no need to cow as removing empty parts doesn't change
7169 * the meaning of the set.
7171 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7178 for (i = map->n-1; i >= 0; --i) {
7179 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7181 isl_basic_map_free(map->p[i]);
7182 if (i != map->n-1) {
7183 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7184 map->p[i] = map->p[map->n-1];
7192 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7194 return (struct isl_set *)
7195 isl_map_remove_empty_parts((struct isl_map *)set);
7198 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7200 struct isl_basic_map *bmap;
7201 if (!map || map->n == 0)
7203 bmap = map->p[map->n-1];
7204 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7205 return isl_basic_map_copy(bmap);
7208 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7210 return (struct isl_basic_set *)
7211 isl_map_copy_basic_map((struct isl_map *)set);
7214 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7215 __isl_keep isl_basic_map *bmap)
7221 for (i = map->n-1; i >= 0; --i) {
7222 if (map->p[i] != bmap)
7224 map = isl_map_cow(map);
7227 isl_basic_map_free(map->p[i]);
7228 if (i != map->n-1) {
7229 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7230 map->p[i] = map->p[map->n-1];
7241 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7242 struct isl_basic_set *bset)
7244 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7245 (struct isl_basic_map *)bset);
7248 /* Given two basic sets bset1 and bset2, compute the maximal difference
7249 * between the values of dimension pos in bset1 and those in bset2
7250 * for any common value of the parameters and dimensions preceding pos.
7252 static enum isl_lp_result basic_set_maximal_difference_at(
7253 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7254 int pos, isl_int *opt)
7257 struct isl_basic_map *bmap1 = NULL;
7258 struct isl_basic_map *bmap2 = NULL;
7259 struct isl_ctx *ctx;
7260 struct isl_vec *obj;
7263 unsigned dim1, dim2;
7264 enum isl_lp_result res;
7266 if (!bset1 || !bset2)
7267 return isl_lp_error;
7269 nparam = isl_basic_set_n_param(bset1);
7270 dim1 = isl_basic_set_n_dim(bset1);
7271 dim2 = isl_basic_set_n_dim(bset2);
7272 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7273 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7274 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7275 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7276 if (!bmap1 || !bmap2)
7278 bmap1 = isl_basic_map_cow(bmap1);
7279 bmap1 = isl_basic_map_extend(bmap1, nparam,
7280 pos, (dim1 - pos) + (dim2 - pos),
7281 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7282 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7285 total = isl_basic_map_total_dim(bmap1);
7287 obj = isl_vec_alloc(ctx, 1 + total);
7288 isl_seq_clr(obj->block.data, 1 + total);
7289 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7290 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7293 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7295 isl_basic_map_free(bmap1);
7299 isl_basic_map_free(bmap1);
7300 isl_basic_map_free(bmap2);
7301 return isl_lp_error;
7304 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7305 * for any common value of the parameters and dimensions preceding pos
7306 * in both basic sets, the values of dimension pos in bset1 are
7307 * smaller or larger than those in bset2.
7310 * 1 if bset1 follows bset2
7311 * -1 if bset1 precedes bset2
7312 * 0 if bset1 and bset2 are incomparable
7313 * -2 if some error occurred.
7315 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7316 struct isl_basic_set *bset2, int pos)
7319 enum isl_lp_result res;
7324 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7326 if (res == isl_lp_empty)
7328 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7329 res == isl_lp_unbounded)
7331 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7340 /* Given two basic sets bset1 and bset2, check whether
7341 * for any common value of the parameters and dimensions preceding pos
7342 * there is a value of dimension pos in bset1 that is larger
7343 * than a value of the same dimension in bset2.
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_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7352 __isl_keep isl_basic_set *bset2, int pos)
7355 enum isl_lp_result res;
7360 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7362 if (res == isl_lp_empty)
7364 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7365 res == isl_lp_unbounded)
7367 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7369 else if (res == isl_lp_ok)
7378 /* Given two sets set1 and set2, check whether
7379 * for any common value of the parameters and dimensions preceding pos
7380 * there is a value of dimension pos in set1 that is larger
7381 * than a value of the same dimension in set2.
7384 * 1 if there exists such a pair
7385 * 0 if there is no such pair, but there is a pair of equal values
7387 * -2 if some error occurred.
7389 int isl_set_follows_at(__isl_keep isl_set *set1,
7390 __isl_keep isl_set *set2, int pos)
7398 for (i = 0; i < set1->n; ++i)
7399 for (j = 0; j < set2->n; ++j) {
7401 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7402 if (f == 1 || f == -2)
7411 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7412 unsigned pos, isl_int *val)
7420 total = isl_basic_map_total_dim(bmap);
7421 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7422 for (; d+1 > pos; --d)
7423 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7427 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7429 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7431 if (!isl_int_is_one(bmap->eq[i][1+d]))
7434 isl_int_neg(*val, bmap->eq[i][0]);
7440 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7441 unsigned pos, isl_int *val)
7453 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7456 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7457 for (i = 1; fixed == 1 && i < map->n; ++i) {
7458 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7459 if (fixed == 1 && isl_int_ne(tmp, v))
7463 isl_int_set(*val, v);
7469 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7470 unsigned pos, isl_int *val)
7472 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7476 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7479 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7482 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7483 enum isl_dim_type type, unsigned pos, isl_int *val)
7485 if (pos >= isl_basic_map_dim(bmap, type))
7487 return isl_basic_map_plain_has_fixed_var(bmap,
7488 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7491 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7492 enum isl_dim_type type, unsigned pos, isl_int *val)
7494 if (pos >= isl_map_dim(map, type))
7496 return isl_map_plain_has_fixed_var(map,
7497 map_offset(map, type) - 1 + pos, val);
7500 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7501 enum isl_dim_type type, unsigned pos, isl_int *val)
7503 return isl_map_plain_is_fixed(set, type, pos, val);
7506 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7507 enum isl_dim_type type, unsigned pos, isl_int *val)
7509 return isl_map_plain_is_fixed(map, type, pos, val);
7512 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7513 * then return this fixed value in *val.
7515 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7516 unsigned dim, isl_int *val)
7518 return isl_basic_set_plain_has_fixed_var(bset,
7519 isl_basic_set_n_param(bset) + dim, val);
7522 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7523 * then return this fixed value in *val.
7525 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7526 unsigned dim, isl_int *val)
7528 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7531 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7532 unsigned dim, isl_int *val)
7534 return isl_set_plain_dim_is_fixed(set, dim, val);
7537 /* Check if input variable in has fixed value and if so and if val is not NULL,
7538 * then return this fixed value in *val.
7540 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7541 unsigned in, isl_int *val)
7543 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7546 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7547 * and if val is not NULL, then return this lower bound in *val.
7549 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7550 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7552 int i, i_eq = -1, i_ineq = -1;
7559 total = isl_basic_set_total_dim(bset);
7560 nparam = isl_basic_set_n_param(bset);
7561 for (i = 0; i < bset->n_eq; ++i) {
7562 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7568 for (i = 0; i < bset->n_ineq; ++i) {
7569 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7571 if (i_eq != -1 || i_ineq != -1)
7575 if (i_eq == -1 && i_ineq == -1)
7577 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7578 /* The coefficient should always be one due to normalization. */
7579 if (!isl_int_is_one(c[1+nparam+dim]))
7581 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7583 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7584 total - nparam - dim - 1) != -1)
7587 isl_int_neg(*val, c[0]);
7591 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7592 unsigned dim, isl_int *val)
7604 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7608 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7610 for (i = 1; fixed == 1 && i < set->n; ++i) {
7611 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7613 if (fixed == 1 && isl_int_ne(tmp, v))
7617 isl_int_set(*val, v);
7628 /* uset_gist depends on constraints without existentially quantified
7629 * variables sorting first.
7631 static int qsort_constraint_cmp(const void *p1, const void *p2)
7633 const struct constraint *c1 = (const struct constraint *)p1;
7634 const struct constraint *c2 = (const struct constraint *)p2;
7636 unsigned size = isl_min(c1->size, c2->size);
7638 l1 = isl_seq_last_non_zero(c1->c, size);
7639 l2 = isl_seq_last_non_zero(c2->c, size);
7644 return isl_seq_cmp(c1->c, c2->c, size);
7647 static struct isl_basic_map *isl_basic_map_sort_constraints(
7648 struct isl_basic_map *bmap)
7651 struct constraint *c;
7656 total = isl_basic_map_total_dim(bmap);
7657 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7660 for (i = 0; i < bmap->n_ineq; ++i) {
7662 c[i].c = bmap->ineq[i];
7664 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7665 for (i = 0; i < bmap->n_ineq; ++i)
7666 bmap->ineq[i] = c[i].c;
7670 isl_basic_map_free(bmap);
7674 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7675 __isl_take isl_basic_set *bset)
7677 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7678 (struct isl_basic_map *)bset);
7681 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7685 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7687 bmap = isl_basic_map_remove_redundancies(bmap);
7688 bmap = isl_basic_map_sort_constraints(bmap);
7689 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7693 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7695 return (struct isl_basic_set *)isl_basic_map_normalize(
7696 (struct isl_basic_map *)bset);
7699 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7700 const __isl_keep isl_basic_map *bmap2)
7707 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7708 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7709 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7710 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7711 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7712 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7713 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7714 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7715 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7716 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7717 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7719 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7721 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7723 if (bmap1->n_eq != bmap2->n_eq)
7724 return bmap1->n_eq - bmap2->n_eq;
7725 if (bmap1->n_ineq != bmap2->n_ineq)
7726 return bmap1->n_ineq - bmap2->n_ineq;
7727 if (bmap1->n_div != bmap2->n_div)
7728 return bmap1->n_div - bmap2->n_div;
7729 total = isl_basic_map_total_dim(bmap1);
7730 for (i = 0; i < bmap1->n_eq; ++i) {
7731 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7735 for (i = 0; i < bmap1->n_ineq; ++i) {
7736 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7740 for (i = 0; i < bmap1->n_div; ++i) {
7741 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7748 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7749 const __isl_keep isl_basic_set *bset2)
7751 return isl_basic_map_plain_cmp(bset1, bset2);
7754 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7755 const __isl_keep isl_set *set2)
7761 if (set1->n != set2->n)
7762 return set1->n - set2->n;
7764 for (i = 0; i < set1->n; ++i) {
7765 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7773 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7774 __isl_keep isl_basic_map *bmap2)
7776 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7779 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7780 __isl_keep isl_basic_set *bset2)
7782 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7783 (isl_basic_map *)bset2);
7786 static int qsort_bmap_cmp(const void *p1, const void *p2)
7788 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7789 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7791 return isl_basic_map_plain_cmp(bmap1, bmap2);
7794 /* We normalize in place, but if anything goes wrong we need
7795 * to return NULL, so we need to make sure we don't change the
7796 * meaning of any possible other copies of map.
7798 struct isl_map *isl_map_normalize(struct isl_map *map)
7801 struct isl_basic_map *bmap;
7805 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7807 for (i = 0; i < map->n; ++i) {
7808 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7811 isl_basic_map_free(map->p[i]);
7814 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7815 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7816 map = isl_map_remove_empty_parts(map);
7819 for (i = map->n - 1; i >= 1; --i) {
7820 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7822 isl_basic_map_free(map->p[i-1]);
7823 for (j = i; j < map->n; ++j)
7824 map->p[j-1] = map->p[j];
7834 struct isl_set *isl_set_normalize(struct isl_set *set)
7836 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7839 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7849 if (!isl_space_is_equal(map1->dim, map2->dim))
7852 map1 = isl_map_copy(map1);
7853 map2 = isl_map_copy(map2);
7854 map1 = isl_map_normalize(map1);
7855 map2 = isl_map_normalize(map2);
7858 equal = map1->n == map2->n;
7859 for (i = 0; equal && i < map1->n; ++i) {
7860 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7873 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7875 return isl_map_plain_is_equal(map1, map2);
7878 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7880 return isl_map_plain_is_equal((struct isl_map *)set1,
7881 (struct isl_map *)set2);
7884 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7886 return isl_set_plain_is_equal(set1, set2);
7889 /* Return an interval that ranges from min to max (inclusive)
7891 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7892 isl_int min, isl_int max)
7895 struct isl_basic_set *bset = NULL;
7897 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7901 k = isl_basic_set_alloc_inequality(bset);
7904 isl_int_set_si(bset->ineq[k][1], 1);
7905 isl_int_neg(bset->ineq[k][0], min);
7907 k = isl_basic_set_alloc_inequality(bset);
7910 isl_int_set_si(bset->ineq[k][1], -1);
7911 isl_int_set(bset->ineq[k][0], max);
7915 isl_basic_set_free(bset);
7919 /* Return the Cartesian product of the basic sets in list (in the given order).
7921 __isl_give isl_basic_set *isl_basic_set_list_product(
7922 __isl_take struct isl_basic_set_list *list)
7930 struct isl_basic_set *product = NULL;
7934 isl_assert(list->ctx, list->n > 0, goto error);
7935 isl_assert(list->ctx, list->p[0], goto error);
7936 nparam = isl_basic_set_n_param(list->p[0]);
7937 dim = isl_basic_set_n_dim(list->p[0]);
7938 extra = list->p[0]->n_div;
7939 n_eq = list->p[0]->n_eq;
7940 n_ineq = list->p[0]->n_ineq;
7941 for (i = 1; i < list->n; ++i) {
7942 isl_assert(list->ctx, list->p[i], goto error);
7943 isl_assert(list->ctx,
7944 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7945 dim += isl_basic_set_n_dim(list->p[i]);
7946 extra += list->p[i]->n_div;
7947 n_eq += list->p[i]->n_eq;
7948 n_ineq += list->p[i]->n_ineq;
7950 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7955 for (i = 0; i < list->n; ++i) {
7956 isl_basic_set_add_constraints(product,
7957 isl_basic_set_copy(list->p[i]), dim);
7958 dim += isl_basic_set_n_dim(list->p[i]);
7960 isl_basic_set_list_free(list);
7963 isl_basic_set_free(product);
7964 isl_basic_set_list_free(list);
7968 struct isl_basic_map *isl_basic_map_product(
7969 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7971 isl_space *dim_result = NULL;
7972 struct isl_basic_map *bmap;
7973 unsigned in1, in2, out1, out2, nparam, total, pos;
7974 struct isl_dim_map *dim_map1, *dim_map2;
7976 if (!bmap1 || !bmap2)
7979 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7980 bmap2->dim, isl_dim_param), goto error);
7981 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7982 isl_space_copy(bmap2->dim));
7984 in1 = isl_basic_map_n_in(bmap1);
7985 in2 = isl_basic_map_n_in(bmap2);
7986 out1 = isl_basic_map_n_out(bmap1);
7987 out2 = isl_basic_map_n_out(bmap2);
7988 nparam = isl_basic_map_n_param(bmap1);
7990 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7991 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7992 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7993 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7994 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7995 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7996 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7997 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7998 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7999 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8000 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8002 bmap = isl_basic_map_alloc_space(dim_result,
8003 bmap1->n_div + bmap2->n_div,
8004 bmap1->n_eq + bmap2->n_eq,
8005 bmap1->n_ineq + bmap2->n_ineq);
8006 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8007 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8008 bmap = isl_basic_map_simplify(bmap);
8009 return isl_basic_map_finalize(bmap);
8011 isl_basic_map_free(bmap1);
8012 isl_basic_map_free(bmap2);
8016 __isl_give isl_basic_map *isl_basic_map_flat_product(
8017 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8019 isl_basic_map *prod;
8021 prod = isl_basic_map_product(bmap1, bmap2);
8022 prod = isl_basic_map_flatten(prod);
8026 __isl_give isl_basic_set *isl_basic_set_flat_product(
8027 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8029 return isl_basic_map_flat_range_product(bset1, bset2);
8032 __isl_give isl_basic_map *isl_basic_map_domain_product(
8033 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8035 isl_space *space_result = NULL;
8036 isl_basic_map *bmap;
8037 unsigned in1, in2, out, nparam, total, pos;
8038 struct isl_dim_map *dim_map1, *dim_map2;
8040 if (!bmap1 || !bmap2)
8043 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8044 isl_space_copy(bmap2->dim));
8046 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8047 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8048 out = isl_basic_map_dim(bmap1, isl_dim_out);
8049 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8051 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8052 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8053 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8054 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8055 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8056 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8057 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8058 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8059 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8060 isl_dim_map_div(dim_map1, bmap1, pos += out);
8061 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8063 bmap = isl_basic_map_alloc_space(space_result,
8064 bmap1->n_div + bmap2->n_div,
8065 bmap1->n_eq + bmap2->n_eq,
8066 bmap1->n_ineq + bmap2->n_ineq);
8067 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8068 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8069 bmap = isl_basic_map_simplify(bmap);
8070 return isl_basic_map_finalize(bmap);
8072 isl_basic_map_free(bmap1);
8073 isl_basic_map_free(bmap2);
8077 __isl_give isl_basic_map *isl_basic_map_range_product(
8078 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8080 isl_space *dim_result = NULL;
8081 isl_basic_map *bmap;
8082 unsigned in, out1, out2, nparam, total, pos;
8083 struct isl_dim_map *dim_map1, *dim_map2;
8085 if (!bmap1 || !bmap2)
8088 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8089 isl_space_copy(bmap2->dim));
8091 in = isl_basic_map_dim(bmap1, isl_dim_in);
8092 out1 = isl_basic_map_n_out(bmap1);
8093 out2 = isl_basic_map_n_out(bmap2);
8094 nparam = isl_basic_map_n_param(bmap1);
8096 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8097 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8098 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8099 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8100 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8101 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8102 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8103 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8104 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8105 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8106 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8108 bmap = isl_basic_map_alloc_space(dim_result,
8109 bmap1->n_div + bmap2->n_div,
8110 bmap1->n_eq + bmap2->n_eq,
8111 bmap1->n_ineq + bmap2->n_ineq);
8112 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8113 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8114 bmap = isl_basic_map_simplify(bmap);
8115 return isl_basic_map_finalize(bmap);
8117 isl_basic_map_free(bmap1);
8118 isl_basic_map_free(bmap2);
8122 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8123 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8125 isl_basic_map *prod;
8127 prod = isl_basic_map_range_product(bmap1, bmap2);
8128 prod = isl_basic_map_flatten_range(prod);
8132 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8133 __isl_take isl_map *map2,
8134 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8135 __isl_take isl_space *right),
8136 __isl_give isl_basic_map *(*basic_map_product)(
8137 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8140 struct isl_map *result;
8146 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8147 map2->dim, isl_dim_param), goto error);
8149 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8150 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8151 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8153 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8154 isl_space_copy(map2->dim)),
8155 map1->n * map2->n, flags);
8158 for (i = 0; i < map1->n; ++i)
8159 for (j = 0; j < map2->n; ++j) {
8160 struct isl_basic_map *part;
8161 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8162 isl_basic_map_copy(map2->p[j]));
8163 if (isl_basic_map_is_empty(part))
8164 isl_basic_map_free(part);
8166 result = isl_map_add_basic_map(result, part);
8179 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8181 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8182 __isl_take isl_map *map2)
8184 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8187 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8188 __isl_take isl_map *map2)
8190 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8193 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8195 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8196 __isl_take isl_map *map2)
8200 prod = isl_map_product(map1, map2);
8201 prod = isl_map_flatten(prod);
8205 /* Given two set A and B, construct its Cartesian product A x B.
8207 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8209 return isl_map_range_product(set1, set2);
8212 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8213 __isl_take isl_set *set2)
8215 return isl_map_flat_range_product(set1, set2);
8218 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8220 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8221 __isl_take isl_map *map2)
8223 return map_product(map1, map2, &isl_space_domain_product,
8224 &isl_basic_map_domain_product);
8227 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8229 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8230 __isl_take isl_map *map2)
8232 return map_product(map1, map2, &isl_space_range_product,
8233 &isl_basic_map_range_product);
8236 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8237 __isl_take isl_map *map2)
8239 return isl_map_align_params_map_map_and(map1, map2,
8240 &map_domain_product_aligned);
8243 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8244 __isl_take isl_map *map2)
8246 return isl_map_align_params_map_map_and(map1, map2,
8247 &map_range_product_aligned);
8250 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8252 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8253 __isl_take isl_map *map2)
8257 prod = isl_map_domain_product(map1, map2);
8258 prod = isl_map_flatten_domain(prod);
8262 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8264 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8265 __isl_take isl_map *map2)
8269 prod = isl_map_range_product(map1, map2);
8270 prod = isl_map_flatten_range(prod);
8274 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8277 uint32_t hash = isl_hash_init();
8282 bmap = isl_basic_map_copy(bmap);
8283 bmap = isl_basic_map_normalize(bmap);
8286 total = isl_basic_map_total_dim(bmap);
8287 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8288 for (i = 0; i < bmap->n_eq; ++i) {
8290 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8291 isl_hash_hash(hash, c_hash);
8293 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8294 for (i = 0; i < bmap->n_ineq; ++i) {
8296 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8297 isl_hash_hash(hash, c_hash);
8299 isl_hash_byte(hash, bmap->n_div & 0xFF);
8300 for (i = 0; i < bmap->n_div; ++i) {
8302 if (isl_int_is_zero(bmap->div[i][0]))
8304 isl_hash_byte(hash, i & 0xFF);
8305 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8306 isl_hash_hash(hash, c_hash);
8308 isl_basic_map_free(bmap);
8312 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8314 return isl_basic_map_get_hash((isl_basic_map *)bset);
8317 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8324 map = isl_map_copy(map);
8325 map = isl_map_normalize(map);
8329 hash = isl_hash_init();
8330 for (i = 0; i < map->n; ++i) {
8332 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8333 isl_hash_hash(hash, bmap_hash);
8341 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8343 return isl_map_get_hash((isl_map *)set);
8346 /* Check if the value for dimension dim is completely determined
8347 * by the values of the other parameters and variables.
8348 * That is, check if dimension dim is involved in an equality.
8350 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8357 nparam = isl_basic_set_n_param(bset);
8358 for (i = 0; i < bset->n_eq; ++i)
8359 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8364 /* Check if the value for dimension dim is completely determined
8365 * by the values of the other parameters and variables.
8366 * That is, check if dimension dim is involved in an equality
8367 * for each of the subsets.
8369 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8375 for (i = 0; i < set->n; ++i) {
8377 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8384 int isl_set_n_basic_set(__isl_keep isl_set *set)
8386 return set ? set->n : 0;
8389 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8390 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8397 for (i = 0; i < map->n; ++i)
8398 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8404 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8405 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8412 for (i = 0; i < set->n; ++i)
8413 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8419 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8426 bset = isl_basic_set_cow(bset);
8430 dim = isl_basic_set_get_space(bset);
8431 dim = isl_space_lift(dim, bset->n_div);
8434 isl_space_free(bset->dim);
8436 bset->extra -= bset->n_div;
8439 bset = isl_basic_set_finalize(bset);
8443 isl_basic_set_free(bset);
8447 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8453 set = isl_set_align_divs(set);
8458 set = isl_set_cow(set);
8462 n_div = set->p[0]->n_div;
8463 dim = isl_set_get_space(set);
8464 dim = isl_space_lift(dim, n_div);
8467 isl_space_free(set->dim);
8470 for (i = 0; i < set->n; ++i) {
8471 set->p[i] = isl_basic_set_lift(set->p[i]);
8482 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8485 struct isl_basic_map *bmap;
8492 set = isl_set_align_divs(set);
8497 dim = isl_set_get_space(set);
8498 if (set->n == 0 || set->p[0]->n_div == 0) {
8500 return isl_map_identity(isl_space_map_from_set(dim));
8503 n_div = set->p[0]->n_div;
8504 dim = isl_space_map_from_set(dim);
8505 n_param = isl_space_dim(dim, isl_dim_param);
8506 n_set = isl_space_dim(dim, isl_dim_in);
8507 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8508 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8509 for (i = 0; i < n_set; ++i)
8510 bmap = var_equal(bmap, i);
8512 total = n_param + n_set + n_set + n_div;
8513 for (i = 0; i < n_div; ++i) {
8514 k = isl_basic_map_alloc_inequality(bmap);
8517 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8518 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8519 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8520 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8521 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8522 set->p[0]->div[i][0]);
8524 l = isl_basic_map_alloc_inequality(bmap);
8527 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8528 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8529 set->p[0]->div[i][0]);
8530 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8534 bmap = isl_basic_map_simplify(bmap);
8535 bmap = isl_basic_map_finalize(bmap);
8536 return isl_map_from_basic_map(bmap);
8539 isl_basic_map_free(bmap);
8543 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8551 dim = isl_basic_set_total_dim(bset);
8552 size += bset->n_eq * (1 + dim);
8553 size += bset->n_ineq * (1 + dim);
8554 size += bset->n_div * (2 + dim);
8559 int isl_set_size(__isl_keep isl_set *set)
8567 for (i = 0; i < set->n; ++i)
8568 size += isl_basic_set_size(set->p[i]);
8573 /* Check if there is any lower bound (if lower == 0) and/or upper
8574 * bound (if upper == 0) on the specified dim.
8576 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8577 enum isl_dim_type type, unsigned pos, int lower, int upper)
8584 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8586 pos += isl_basic_map_offset(bmap, type);
8588 for (i = 0; i < bmap->n_div; ++i) {
8589 if (isl_int_is_zero(bmap->div[i][0]))
8591 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8595 for (i = 0; i < bmap->n_eq; ++i)
8596 if (!isl_int_is_zero(bmap->eq[i][pos]))
8599 for (i = 0; i < bmap->n_ineq; ++i) {
8600 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8607 return lower && upper;
8610 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8611 enum isl_dim_type type, unsigned pos)
8613 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8616 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8617 enum isl_dim_type type, unsigned pos)
8619 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8622 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8623 enum isl_dim_type type, unsigned pos)
8625 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8628 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8629 enum isl_dim_type type, unsigned pos)
8636 for (i = 0; i < map->n; ++i) {
8638 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8639 if (bounded < 0 || !bounded)
8646 /* Return 1 if the specified dim is involved in both an upper bound
8647 * and a lower bound.
8649 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8650 enum isl_dim_type type, unsigned pos)
8652 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8655 static int has_bound(__isl_keep isl_map *map,
8656 enum isl_dim_type type, unsigned pos,
8657 int (*fn)(__isl_keep isl_basic_map *bmap,
8658 enum isl_dim_type type, unsigned pos))
8665 for (i = 0; i < map->n; ++i) {
8667 bounded = fn(map->p[i], type, pos);
8668 if (bounded < 0 || bounded)
8675 /* Return 1 if the specified dim is involved in any lower bound.
8677 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8678 enum isl_dim_type type, unsigned pos)
8680 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8683 /* Return 1 if the specified dim is involved in any upper bound.
8685 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8686 enum isl_dim_type type, unsigned pos)
8688 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8691 /* For each of the "n" variables starting at "first", determine
8692 * the sign of the variable and put the results in the first "n"
8693 * elements of the array "signs".
8695 * 1 means that the variable is non-negative
8696 * -1 means that the variable is non-positive
8697 * 0 means the variable attains both positive and negative values.
8699 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8700 unsigned first, unsigned n, int *signs)
8702 isl_vec *bound = NULL;
8703 struct isl_tab *tab = NULL;
8704 struct isl_tab_undo *snap;
8707 if (!bset || !signs)
8710 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8711 tab = isl_tab_from_basic_set(bset);
8715 isl_seq_clr(bound->el, bound->size);
8716 isl_int_set_si(bound->el[0], -1);
8718 snap = isl_tab_snap(tab);
8719 for (i = 0; i < n; ++i) {
8722 isl_int_set_si(bound->el[1 + first + i], -1);
8723 if (isl_tab_add_ineq(tab, bound->el) < 0)
8726 isl_int_set_si(bound->el[1 + first + i], 0);
8727 if (isl_tab_rollback(tab, snap) < 0)
8735 isl_int_set_si(bound->el[1 + first + i], 1);
8736 if (isl_tab_add_ineq(tab, bound->el) < 0)
8739 isl_int_set_si(bound->el[1 + first + i], 0);
8740 if (isl_tab_rollback(tab, snap) < 0)
8743 signs[i] = empty ? -1 : 0;
8747 isl_vec_free(bound);
8751 isl_vec_free(bound);
8755 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8756 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8758 if (!bset || !signs)
8760 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8763 first += pos(bset->dim, type) - 1;
8764 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8767 /* Check if the given basic map is obviously single-valued.
8768 * In particular, for each output dimension, check that there is
8769 * an equality that defines the output dimension in terms of
8770 * earlier dimensions.
8772 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8782 total = 1 + isl_basic_map_total_dim(bmap);
8783 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8784 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8786 for (i = 0; i < n_out; ++i) {
8787 for (j = 0; j < bmap->n_eq; ++j) {
8788 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8790 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8791 total - (o_out + i + 1)) == -1)
8794 if (j >= bmap->n_eq)
8801 /* Check if the given map is obviously single-valued.
8803 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8812 return isl_basic_map_plain_is_single_valued(map->p[0]);
8815 /* Check if the given map is single-valued.
8820 * and check if the result is a subset of the identity mapping.
8822 int isl_map_is_single_valued(__isl_keep isl_map *map)
8829 sv = isl_map_plain_is_single_valued(map);
8833 test = isl_map_reverse(isl_map_copy(map));
8834 test = isl_map_apply_range(test, isl_map_copy(map));
8836 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8837 id = isl_map_identity(dim);
8839 sv = isl_map_is_subset(test, id);
8847 int isl_map_is_injective(__isl_keep isl_map *map)
8851 map = isl_map_copy(map);
8852 map = isl_map_reverse(map);
8853 in = isl_map_is_single_valued(map);
8859 /* Check if the given map is obviously injective.
8861 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8865 map = isl_map_copy(map);
8866 map = isl_map_reverse(map);
8867 in = isl_map_plain_is_single_valued(map);
8873 int isl_map_is_bijective(__isl_keep isl_map *map)
8877 sv = isl_map_is_single_valued(map);
8881 return isl_map_is_injective(map);
8884 int isl_set_is_singleton(__isl_keep isl_set *set)
8886 return isl_map_is_single_valued((isl_map *)set);
8889 int isl_map_is_translation(__isl_keep isl_map *map)
8894 delta = isl_map_deltas(isl_map_copy(map));
8895 ok = isl_set_is_singleton(delta);
8896 isl_set_free(delta);
8901 static int unique(isl_int *p, unsigned pos, unsigned len)
8903 if (isl_seq_first_non_zero(p, pos) != -1)
8905 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8910 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8919 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8922 nvar = isl_basic_set_dim(bset, isl_dim_set);
8923 ovar = isl_space_offset(bset->dim, isl_dim_set);
8924 for (j = 0; j < nvar; ++j) {
8925 int lower = 0, upper = 0;
8926 for (i = 0; i < bset->n_eq; ++i) {
8927 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8929 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8935 for (i = 0; i < bset->n_ineq; ++i) {
8936 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8938 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8940 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8945 if (!lower || !upper)
8952 int isl_set_is_box(__isl_keep isl_set *set)
8959 return isl_basic_set_is_box(set->p[0]);
8962 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8967 return isl_space_is_wrapping(bset->dim);
8970 int isl_set_is_wrapping(__isl_keep isl_set *set)
8975 return isl_space_is_wrapping(set->dim);
8978 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8980 bmap = isl_basic_map_cow(bmap);
8984 bmap->dim = isl_space_wrap(bmap->dim);
8988 bmap = isl_basic_map_finalize(bmap);
8990 return (isl_basic_set *)bmap;
8992 isl_basic_map_free(bmap);
8996 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9000 map = isl_map_cow(map);
9004 for (i = 0; i < map->n; ++i) {
9005 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9009 map->dim = isl_space_wrap(map->dim);
9013 return (isl_set *)map;
9019 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9021 bset = isl_basic_set_cow(bset);
9025 bset->dim = isl_space_unwrap(bset->dim);
9029 bset = isl_basic_set_finalize(bset);
9031 return (isl_basic_map *)bset;
9033 isl_basic_set_free(bset);
9037 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9044 if (!isl_set_is_wrapping(set))
9045 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9048 set = isl_set_cow(set);
9052 for (i = 0; i < set->n; ++i) {
9053 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9058 set->dim = isl_space_unwrap(set->dim);
9062 return (isl_map *)set;
9068 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9069 enum isl_dim_type type)
9074 if (!isl_space_is_named_or_nested(bmap->dim, type))
9077 bmap = isl_basic_map_cow(bmap);
9081 bmap->dim = isl_space_reset(bmap->dim, type);
9085 bmap = isl_basic_map_finalize(bmap);
9089 isl_basic_map_free(bmap);
9093 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9094 enum isl_dim_type type)
9101 if (!isl_space_is_named_or_nested(map->dim, type))
9104 map = isl_map_cow(map);
9108 for (i = 0; i < map->n; ++i) {
9109 map->p[i] = isl_basic_map_reset(map->p[i], type);
9113 map->dim = isl_space_reset(map->dim, type);
9123 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9128 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9131 bmap = isl_basic_map_cow(bmap);
9135 bmap->dim = isl_space_flatten(bmap->dim);
9139 bmap = isl_basic_map_finalize(bmap);
9143 isl_basic_map_free(bmap);
9147 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9149 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9152 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9153 __isl_take isl_basic_map *bmap)
9158 if (!bmap->dim->nested[0])
9161 bmap = isl_basic_map_cow(bmap);
9165 bmap->dim = isl_space_flatten_domain(bmap->dim);
9169 bmap = isl_basic_map_finalize(bmap);
9173 isl_basic_map_free(bmap);
9177 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9178 __isl_take isl_basic_map *bmap)
9183 if (!bmap->dim->nested[1])
9186 bmap = isl_basic_map_cow(bmap);
9190 bmap->dim = isl_space_flatten_range(bmap->dim);
9194 bmap = isl_basic_map_finalize(bmap);
9198 isl_basic_map_free(bmap);
9202 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9209 if (!map->dim->nested[0] && !map->dim->nested[1])
9212 map = isl_map_cow(map);
9216 for (i = 0; i < map->n; ++i) {
9217 map->p[i] = isl_basic_map_flatten(map->p[i]);
9221 map->dim = isl_space_flatten(map->dim);
9231 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9233 return (isl_set *)isl_map_flatten((isl_map *)set);
9236 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9238 isl_space *dim, *flat_dim;
9241 dim = isl_set_get_space(set);
9242 flat_dim = isl_space_flatten(isl_space_copy(dim));
9243 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9244 map = isl_map_intersect_domain(map, set);
9249 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9256 if (!map->dim->nested[0])
9259 map = isl_map_cow(map);
9263 for (i = 0; i < map->n; ++i) {
9264 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9268 map->dim = isl_space_flatten_domain(map->dim);
9278 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9285 if (!map->dim->nested[1])
9288 map = isl_map_cow(map);
9292 for (i = 0; i < map->n; ++i) {
9293 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9297 map->dim = isl_space_flatten_range(map->dim);
9307 /* Reorder the dimensions of "bmap" according to the given dim_map
9308 * and set the dimension specification to "dim".
9310 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9311 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9315 bmap = isl_basic_map_cow(bmap);
9316 if (!bmap || !dim || !dim_map)
9319 res = isl_basic_map_alloc_space(dim,
9320 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9321 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9322 res = isl_basic_map_finalize(res);
9326 isl_basic_map_free(bmap);
9327 isl_space_free(dim);
9331 /* Reorder the dimensions of "map" according to given reordering.
9333 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9334 __isl_take isl_reordering *r)
9337 struct isl_dim_map *dim_map;
9339 map = isl_map_cow(map);
9340 dim_map = isl_dim_map_from_reordering(r);
9341 if (!map || !r || !dim_map)
9344 for (i = 0; i < map->n; ++i) {
9345 struct isl_dim_map *dim_map_i;
9347 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9349 map->p[i] = isl_basic_map_realign(map->p[i],
9350 isl_space_copy(r->dim), dim_map_i);
9356 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9358 isl_reordering_free(r);
9364 isl_reordering_free(r);
9368 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9369 __isl_take isl_reordering *r)
9371 return (isl_set *)isl_map_realign((isl_map *)set, r);
9374 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9375 __isl_take isl_space *model)
9382 ctx = isl_space_get_ctx(model);
9383 if (!isl_space_has_named_params(model))
9384 isl_die(ctx, isl_error_invalid,
9385 "model has unnamed parameters", goto error);
9386 if (!isl_space_has_named_params(map->dim))
9387 isl_die(ctx, isl_error_invalid,
9388 "relation has unnamed parameters", goto error);
9389 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9390 isl_reordering *exp;
9392 model = isl_space_drop_dims(model, isl_dim_in,
9393 0, isl_space_dim(model, isl_dim_in));
9394 model = isl_space_drop_dims(model, isl_dim_out,
9395 0, isl_space_dim(model, isl_dim_out));
9396 exp = isl_parameter_alignment_reordering(map->dim, model);
9397 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9398 map = isl_map_realign(map, exp);
9401 isl_space_free(model);
9404 isl_space_free(model);
9409 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9410 __isl_take isl_space *model)
9412 return isl_map_align_params(set, model);
9415 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9416 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9417 enum isl_dim_type c2, enum isl_dim_type c3,
9418 enum isl_dim_type c4, enum isl_dim_type c5)
9420 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9421 struct isl_mat *mat;
9427 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9428 isl_basic_map_total_dim(bmap) + 1);
9431 for (i = 0; i < bmap->n_eq; ++i)
9432 for (j = 0, pos = 0; j < 5; ++j) {
9433 int off = isl_basic_map_offset(bmap, c[j]);
9434 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9435 isl_int_set(mat->row[i][pos],
9436 bmap->eq[i][off + k]);
9444 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9445 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9446 enum isl_dim_type c2, enum isl_dim_type c3,
9447 enum isl_dim_type c4, enum isl_dim_type c5)
9449 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9450 struct isl_mat *mat;
9456 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9457 isl_basic_map_total_dim(bmap) + 1);
9460 for (i = 0; i < bmap->n_ineq; ++i)
9461 for (j = 0, pos = 0; j < 5; ++j) {
9462 int off = isl_basic_map_offset(bmap, c[j]);
9463 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9464 isl_int_set(mat->row[i][pos],
9465 bmap->ineq[i][off + k]);
9473 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9474 __isl_take isl_space *dim,
9475 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9476 enum isl_dim_type c2, enum isl_dim_type c3,
9477 enum isl_dim_type c4, enum isl_dim_type c5)
9479 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9480 isl_basic_map *bmap;
9486 if (!dim || !eq || !ineq)
9489 if (eq->n_col != ineq->n_col)
9490 isl_die(dim->ctx, isl_error_invalid,
9491 "equalities and inequalities matrices should have "
9492 "same number of columns", goto error);
9494 total = 1 + isl_space_dim(dim, isl_dim_all);
9496 if (eq->n_col < total)
9497 isl_die(dim->ctx, isl_error_invalid,
9498 "number of columns too small", goto error);
9500 extra = eq->n_col - total;
9502 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9503 eq->n_row, ineq->n_row);
9506 for (i = 0; i < extra; ++i) {
9507 k = isl_basic_map_alloc_div(bmap);
9510 isl_int_set_si(bmap->div[k][0], 0);
9512 for (i = 0; i < eq->n_row; ++i) {
9513 l = isl_basic_map_alloc_equality(bmap);
9516 for (j = 0, pos = 0; j < 5; ++j) {
9517 int off = isl_basic_map_offset(bmap, c[j]);
9518 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9519 isl_int_set(bmap->eq[l][off + k],
9525 for (i = 0; i < ineq->n_row; ++i) {
9526 l = isl_basic_map_alloc_inequality(bmap);
9529 for (j = 0, pos = 0; j < 5; ++j) {
9530 int off = isl_basic_map_offset(bmap, c[j]);
9531 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9532 isl_int_set(bmap->ineq[l][off + k],
9539 isl_space_free(dim);
9545 isl_space_free(dim);
9551 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9552 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9553 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9555 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9556 c1, c2, c3, c4, isl_dim_in);
9559 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9560 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9561 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9563 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9564 c1, c2, c3, c4, isl_dim_in);
9567 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9568 __isl_take isl_space *dim,
9569 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9570 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9572 return (isl_basic_set*)
9573 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9574 c1, c2, c3, c4, isl_dim_in);
9577 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9582 return isl_space_can_zip(bmap->dim);
9585 int isl_map_can_zip(__isl_keep isl_map *map)
9590 return isl_space_can_zip(map->dim);
9593 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9594 * (A -> C) -> (B -> D).
9596 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9605 if (!isl_basic_map_can_zip(bmap))
9606 isl_die(bmap->ctx, isl_error_invalid,
9607 "basic map cannot be zipped", goto error);
9608 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9609 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9610 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9611 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9612 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9615 bmap->dim = isl_space_zip(bmap->dim);
9620 isl_basic_map_free(bmap);
9624 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9625 * (A -> C) -> (B -> D).
9627 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9634 if (!isl_map_can_zip(map))
9635 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9638 map = isl_map_cow(map);
9642 for (i = 0; i < map->n; ++i) {
9643 map->p[i] = isl_basic_map_zip(map->p[i]);
9648 map->dim = isl_space_zip(map->dim);
9658 /* Construct a basic map mapping the domain of the affine expression
9659 * to a one-dimensional range prescribed by the affine expression.
9661 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9665 isl_local_space *ls;
9666 isl_basic_map *bmap;
9671 ls = isl_aff_get_local_space(aff);
9672 bmap = isl_basic_map_from_local_space(ls);
9673 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9674 k = isl_basic_map_alloc_equality(bmap);
9678 pos = isl_basic_map_offset(bmap, isl_dim_out);
9679 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9680 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9681 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9682 aff->v->size - (pos + 1));
9685 bmap = isl_basic_map_finalize(bmap);
9689 isl_basic_map_free(bmap);
9693 /* Construct a basic map mapping the domain the multi-affine expression
9694 * to its range, with each dimension in the range equated to the
9695 * corresponding affine expression.
9697 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9698 __isl_take isl_multi_aff *maff)
9702 isl_basic_map *bmap;
9707 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9708 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9709 "invalid space", return isl_multi_aff_free(maff));
9711 space = isl_space_domain(isl_multi_aff_get_space(maff));
9712 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9714 for (i = 0; i < maff->n; ++i) {
9716 isl_basic_map *bmap_i;
9718 aff = isl_aff_copy(maff->p[i]);
9719 bmap_i = isl_basic_map_from_aff(aff);
9721 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9724 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9726 isl_multi_aff_free(maff);
9730 /* Construct a basic map mapping a domain in the given space to
9731 * to an n-dimensional range, with n the number of elements in the list,
9732 * where each coordinate in the range is prescribed by the
9733 * corresponding affine expression.
9734 * The domains of all affine expressions in the list are assumed to match
9737 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9738 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9742 isl_basic_map *bmap;
9747 dim = isl_space_from_domain(domain_dim);
9748 bmap = isl_basic_map_universe(dim);
9750 for (i = 0; i < list->n; ++i) {
9752 isl_basic_map *bmap_i;
9754 aff = isl_aff_copy(list->p[i]);
9755 bmap_i = isl_basic_map_from_aff(aff);
9757 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9760 isl_aff_list_free(list);
9764 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9765 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9767 return isl_map_equate(set, type1, pos1, type2, pos2);
9770 /* Add a constraint imposing that the given two dimensions are equal.
9772 __isl_give isl_map *isl_map_equate(__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 /* Add a constraint imposing that the given two dimensions have opposite values.
9810 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9811 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9813 isl_basic_map *bmap = NULL;
9819 if (pos1 >= isl_map_dim(map, type1))
9820 isl_die(map->ctx, isl_error_invalid,
9821 "index out of bounds", goto error);
9822 if (pos2 >= isl_map_dim(map, type2))
9823 isl_die(map->ctx, isl_error_invalid,
9824 "index out of bounds", goto error);
9826 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9827 i = isl_basic_map_alloc_equality(bmap);
9830 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9831 pos1 += isl_basic_map_offset(bmap, type1);
9832 pos2 += isl_basic_map_offset(bmap, type2);
9833 isl_int_set_si(bmap->eq[i][pos1], 1);
9834 isl_int_set_si(bmap->eq[i][pos2], 1);
9835 bmap = isl_basic_map_finalize(bmap);
9837 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9841 isl_basic_map_free(bmap);
9846 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9850 isl_local_space *ls;
9855 if (!isl_basic_map_divs_known(bmap))
9856 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9857 "some divs are unknown", return NULL);
9859 ls = isl_basic_map_get_local_space(bmap);
9860 div = isl_local_space_get_div(ls, pos);
9861 isl_local_space_free(ls);
9866 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9869 return isl_basic_map_get_div(bset, pos);
9872 /* Plug in "subs" for dimension "type", "pos" of "bset".
9874 * Let i be the dimension to replace and let "subs" be of the form
9878 * Any integer division with a non-zero coefficient for i,
9880 * floor((a i + g)/m)
9884 * floor((a f + d g)/(m d))
9886 * Constraints of the form
9894 __isl_give isl_basic_set *isl_basic_set_substitute(
9895 __isl_take isl_basic_set *bset,
9896 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9902 if (bset && isl_basic_set_plain_is_empty(bset))
9905 bset = isl_basic_set_cow(bset);
9909 ctx = isl_basic_set_get_ctx(bset);
9910 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9911 isl_die(ctx, isl_error_invalid,
9912 "spaces don't match", goto error);
9913 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9914 isl_die(ctx, isl_error_unsupported,
9915 "cannot handle divs yet", goto error);
9917 pos += isl_basic_set_offset(bset, type);
9921 for (i = 0; i < bset->n_eq; ++i) {
9922 if (isl_int_is_zero(bset->eq[i][pos]))
9924 isl_int_set(v, bset->eq[i][pos]);
9925 isl_int_set_si(bset->eq[i][pos], 0);
9926 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9927 v, subs->v->el + 1, subs->v->size - 1);
9930 for (i = 0; i < bset->n_ineq; ++i) {
9931 if (isl_int_is_zero(bset->ineq[i][pos]))
9933 isl_int_set(v, bset->ineq[i][pos]);
9934 isl_int_set_si(bset->ineq[i][pos], 0);
9935 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9936 v, subs->v->el + 1, subs->v->size - 1);
9939 for (i = 0; i < bset->n_div; ++i) {
9940 if (isl_int_is_zero(bset->div[i][1 + pos]))
9942 isl_int_set(v, bset->div[i][1 + pos]);
9943 isl_int_set_si(bset->div[i][1 + pos], 0);
9944 isl_seq_combine(bset->div[i] + 1,
9945 subs->v->el[0], bset->div[i] + 1,
9946 v, subs->v->el + 1, subs->v->size - 1);
9947 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9952 bset = isl_basic_set_simplify(bset);
9953 return isl_basic_set_finalize(bset);
9955 isl_basic_set_free(bset);
9959 /* Plug in "subs" for dimension "type", "pos" of "set".
9961 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9962 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9966 if (set && isl_set_plain_is_empty(set))
9969 set = isl_set_cow(set);
9973 for (i = set->n - 1; i >= 0; --i) {
9974 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9977 if (isl_basic_set_plain_is_empty(set->p[i])) {
9978 isl_basic_set_free(set->p[i]);
9979 if (i != set->n - 1)
9980 set->p[i] = set->p[set->n - 1];