Merge branch 'maint'
[isl.git] / isl_map.c
blob26eb7d26505eeb8b8471cb5b6f563eb40078ba13
1 /*
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
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.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>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
37 switch (type) {
38 case isl_dim_param: return dim->nparam;
39 case isl_dim_in: return dim->n_in;
40 case isl_dim_out: return dim->n_out;
41 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
42 default: return 0;
46 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
52 default: return 0;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
57 enum isl_dim_type type)
59 if (!bmap)
60 return 0;
61 switch (type) {
62 case isl_dim_cst: return 1;
63 case isl_dim_param:
64 case isl_dim_in:
65 case isl_dim_out: return isl_space_dim(bmap->dim, type);
66 case isl_dim_div: return bmap->n_div;
67 case isl_dim_all: return isl_basic_map_total_dim(bmap);
68 default: return 0;
72 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
74 return map ? n(map->dim, type) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
79 return set ? n(set->dim, type) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
83 enum isl_dim_type type)
85 isl_space *dim = bmap->dim;
86 switch (type) {
87 case isl_dim_cst: return 0;
88 case isl_dim_param: return 1;
89 case isl_dim_in: return 1 + dim->nparam;
90 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
91 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
92 default: return 0;
96 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
97 enum isl_dim_type type)
99 return isl_basic_map_offset(bset, type);
102 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
104 return pos(map->dim, type);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_dim(bset, type);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
115 return isl_basic_set_dim(bset, isl_dim_set);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_param);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
125 if (!bset)
126 return 0;
127 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
130 unsigned isl_set_n_dim(__isl_keep isl_set *set)
132 return isl_set_dim(set, isl_dim_set);
135 unsigned isl_set_n_param(__isl_keep isl_set *set)
137 return isl_set_dim(set, isl_dim_param);
140 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
142 return bmap ? bmap->dim->n_in : 0;
145 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
147 return bmap ? bmap->dim->n_out : 0;
150 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->nparam : 0;
155 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
157 return bmap ? bmap->n_div : 0;
160 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
162 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
165 unsigned isl_map_n_in(const struct isl_map *map)
167 return map ? map->dim->n_in : 0;
170 unsigned isl_map_n_out(const struct isl_map *map)
172 return map ? map->dim->n_out : 0;
175 unsigned isl_map_n_param(const struct isl_map *map)
177 return map ? map->dim->nparam : 0;
180 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
182 int m;
183 if (!map || !set)
184 return -1;
185 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
186 if (m < 0 || !m)
187 return m;
188 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
191 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
192 struct isl_basic_set *bset)
194 int m;
195 if (!bmap || !bset)
196 return -1;
197 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
198 if (m < 0 || !m)
199 return m;
200 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
203 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
205 int m;
206 if (!map || !set)
207 return -1;
208 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
214 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
215 struct isl_basic_set *bset)
217 int m;
218 if (!bmap || !bset)
219 return -1;
220 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
226 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
228 return bmap ? bmap->ctx : NULL;
231 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
233 return bset ? bset->ctx : NULL;
236 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
238 return map ? map->ctx : NULL;
241 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
243 return set ? set->ctx : NULL;
246 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
248 if (!bmap)
249 return NULL;
250 return isl_space_copy(bmap->dim);
253 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
255 if (!bset)
256 return NULL;
257 return isl_space_copy(bset->dim);
260 /* Extract the divs in "bmap" as a matrix.
262 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
264 int i;
265 isl_ctx *ctx;
266 isl_mat *div;
267 unsigned total;
268 unsigned cols;
270 if (!bmap)
271 return NULL;
273 ctx = isl_basic_map_get_ctx(bmap);
274 total = isl_space_dim(bmap->dim, isl_dim_all);
275 cols = 1 + 1 + total + bmap->n_div;
276 div = isl_mat_alloc(ctx, bmap->n_div, cols);
277 if (!div)
278 return NULL;
280 for (i = 0; i < bmap->n_div; ++i)
281 isl_seq_cpy(div->row[i], bmap->div[i], cols);
283 return div;
286 __isl_give isl_local_space *isl_basic_map_get_local_space(
287 __isl_keep isl_basic_map *bmap)
289 isl_mat *div;
291 if (!bmap)
292 return NULL;
294 div = isl_basic_map_get_divs(bmap);
295 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
298 __isl_give isl_local_space *isl_basic_set_get_local_space(
299 __isl_keep isl_basic_set *bset)
301 return isl_basic_map_get_local_space(bset);
304 __isl_give isl_basic_map *isl_basic_map_from_local_space(
305 __isl_take isl_local_space *ls)
307 int i;
308 int n_div;
309 isl_basic_map *bmap;
311 if (!ls)
312 return NULL;
314 n_div = isl_local_space_dim(ls, isl_dim_div);
315 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
316 n_div, 0, 2 * n_div);
318 for (i = 0; i < n_div; ++i)
319 if (isl_basic_map_alloc_div(bmap) < 0)
320 goto error;
322 for (i = 0; i < n_div; ++i) {
323 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
324 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
325 goto error;
328 isl_local_space_free(ls);
329 return bmap;
330 error:
331 isl_local_space_free(ls);
332 isl_basic_map_free(bmap);
333 return NULL;
336 __isl_give isl_basic_set *isl_basic_set_from_local_space(
337 __isl_take isl_local_space *ls)
339 return isl_basic_map_from_local_space(ls);
342 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
344 if (!map)
345 return NULL;
346 return isl_space_copy(map->dim);
349 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
351 if (!set)
352 return NULL;
353 return isl_space_copy(set->dim);
356 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
357 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
359 bmap = isl_basic_map_cow(bmap);
360 if (!bmap)
361 return NULL;
362 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
363 if (!bmap->dim)
364 goto error;
365 bmap = isl_basic_map_finalize(bmap);
366 return bmap;
367 error:
368 isl_basic_map_free(bmap);
369 return NULL;
372 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
373 __isl_take isl_basic_set *bset, const char *s)
375 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
378 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
379 enum isl_dim_type type)
381 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
384 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
385 enum isl_dim_type type, const char *s)
387 int i;
389 map = isl_map_cow(map);
390 if (!map)
391 return NULL;
393 map->dim = isl_space_set_tuple_name(map->dim, type, s);
394 if (!map->dim)
395 goto error;
397 for (i = 0; i < map->n; ++i) {
398 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
399 if (!map->p[i])
400 goto error;
403 return map;
404 error:
405 isl_map_free(map);
406 return NULL;
409 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
410 enum isl_dim_type type)
412 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
415 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
416 const char *s)
418 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
421 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
422 enum isl_dim_type type, __isl_take isl_id *id)
424 map = isl_map_cow(map);
425 if (!map)
426 return isl_id_free(id);
428 map->dim = isl_space_set_tuple_id(map->dim, type, id);
430 return isl_map_reset_space(map, isl_space_copy(map->dim));
433 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
434 __isl_take isl_id *id)
436 return isl_map_set_tuple_id(set, isl_dim_set, id);
439 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
440 enum isl_dim_type type)
442 map = isl_map_cow(map);
443 if (!map)
444 return NULL;
446 map->dim = isl_space_reset_tuple_id(map->dim, type);
448 return isl_map_reset_space(map, isl_space_copy(map->dim));
451 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
453 return isl_map_reset_tuple_id(set, isl_dim_set);
456 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
458 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
461 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
462 enum isl_dim_type type)
464 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
467 int isl_set_has_tuple_id(__isl_keep isl_set *set)
469 return isl_map_has_tuple_id(set, isl_dim_set);
472 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
474 return isl_map_get_tuple_id(set, isl_dim_set);
477 /* Does the set tuple have a name?
479 int isl_set_has_tuple_name(__isl_keep isl_set *set)
481 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
485 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
487 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
490 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
492 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
495 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
496 enum isl_dim_type type, unsigned pos)
498 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
501 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
502 enum isl_dim_type type, unsigned pos)
504 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
507 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
508 enum isl_dim_type type, unsigned pos)
510 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
513 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
514 enum isl_dim_type type, unsigned pos)
516 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
519 /* Does the given dimension have a name?
521 int isl_set_has_dim_name(__isl_keep isl_set *set,
522 enum isl_dim_type type, unsigned pos)
524 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
527 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
528 __isl_take isl_basic_map *bmap,
529 enum isl_dim_type type, unsigned pos, const char *s)
531 bmap = isl_basic_map_cow(bmap);
532 if (!bmap)
533 return NULL;
534 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
535 if (!bmap->dim)
536 goto error;
537 return isl_basic_map_finalize(bmap);
538 error:
539 isl_basic_map_free(bmap);
540 return NULL;
543 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
544 enum isl_dim_type type, unsigned pos, const char *s)
546 int i;
548 map = isl_map_cow(map);
549 if (!map)
550 return NULL;
552 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
553 if (!map->dim)
554 goto error;
556 for (i = 0; i < map->n; ++i) {
557 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
558 if (!map->p[i])
559 goto error;
562 return map;
563 error:
564 isl_map_free(map);
565 return NULL;
568 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
569 __isl_take isl_basic_set *bset,
570 enum isl_dim_type type, unsigned pos, const char *s)
572 return (isl_basic_set *)isl_basic_map_set_dim_name(
573 (isl_basic_map *)bset, type, pos, s);
576 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
577 enum isl_dim_type type, unsigned pos, const char *s)
579 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
582 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
583 enum isl_dim_type type, unsigned pos)
585 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
588 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
589 enum isl_dim_type type, unsigned pos)
591 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
594 int isl_map_has_dim_id(__isl_keep isl_map *map,
595 enum isl_dim_type type, unsigned pos)
597 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
600 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
601 enum isl_dim_type type, unsigned pos)
603 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
606 int isl_set_has_dim_id(__isl_keep isl_set *set,
607 enum isl_dim_type type, unsigned pos)
609 return isl_map_has_dim_id(set, type, pos);
612 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
613 enum isl_dim_type type, unsigned pos)
615 return isl_map_get_dim_id(set, type, pos);
618 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
619 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
621 map = isl_map_cow(map);
622 if (!map)
623 return isl_id_free(id);
625 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
627 return isl_map_reset_space(map, isl_space_copy(map->dim));
630 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
631 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
633 return isl_map_set_dim_id(set, type, pos, id);
636 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
637 __isl_keep isl_id *id)
639 if (!map)
640 return -1;
641 return isl_space_find_dim_by_id(map->dim, type, id);
644 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
645 __isl_keep isl_id *id)
647 return isl_map_find_dim_by_id(set, type, id);
650 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
651 const char *name)
653 if (!map)
654 return -1;
655 return isl_space_find_dim_by_name(map->dim, type, name);
658 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
659 const char *name)
661 return isl_map_find_dim_by_name(set, type, name);
664 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
666 if (!bmap)
667 return -1;
668 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
671 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
673 return isl_basic_map_is_rational(bset);
676 /* Is this basic set a parameter domain?
678 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
680 if (!bset)
681 return -1;
682 return isl_space_is_params(bset->dim);
685 /* Is this set a parameter domain?
687 int isl_set_is_params(__isl_keep isl_set *set)
689 if (!set)
690 return -1;
691 return isl_space_is_params(set->dim);
694 /* Is this map actually a parameter domain?
695 * Users should never call this function. Outside of isl,
696 * a map can never be a parameter domain.
698 int isl_map_is_params(__isl_keep isl_map *map)
700 if (!map)
701 return -1;
702 return isl_space_is_params(map->dim);
705 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
706 struct isl_basic_map *bmap, unsigned extra,
707 unsigned n_eq, unsigned n_ineq)
709 int i;
710 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
712 bmap->ctx = ctx;
713 isl_ctx_ref(ctx);
715 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
716 if (isl_blk_is_error(bmap->block))
717 goto error;
719 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
720 if (!bmap->ineq)
721 goto error;
723 if (extra == 0) {
724 bmap->block2 = isl_blk_empty();
725 bmap->div = NULL;
726 } else {
727 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
728 if (isl_blk_is_error(bmap->block2))
729 goto error;
731 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
732 if (!bmap->div)
733 goto error;
736 for (i = 0; i < n_ineq + n_eq; ++i)
737 bmap->ineq[i] = bmap->block.data + i * row_size;
739 for (i = 0; i < extra; ++i)
740 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
742 bmap->ref = 1;
743 bmap->flags = 0;
744 bmap->c_size = n_eq + n_ineq;
745 bmap->eq = bmap->ineq + n_ineq;
746 bmap->extra = extra;
747 bmap->n_eq = 0;
748 bmap->n_ineq = 0;
749 bmap->n_div = 0;
750 bmap->sample = NULL;
752 return bmap;
753 error:
754 isl_basic_map_free(bmap);
755 return NULL;
758 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
759 unsigned nparam, unsigned dim, unsigned extra,
760 unsigned n_eq, unsigned n_ineq)
762 struct isl_basic_map *bmap;
763 isl_space *space;
765 space = isl_space_set_alloc(ctx, nparam, dim);
766 if (!space)
767 return NULL;
769 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
770 return (struct isl_basic_set *)bmap;
773 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
774 unsigned extra, unsigned n_eq, unsigned n_ineq)
776 struct isl_basic_map *bmap;
777 if (!dim)
778 return NULL;
779 isl_assert(dim->ctx, dim->n_in == 0, goto error);
780 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
781 return (struct isl_basic_set *)bmap;
782 error:
783 isl_space_free(dim);
784 return NULL;
787 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
788 unsigned extra, unsigned n_eq, unsigned n_ineq)
790 struct isl_basic_map *bmap;
792 if (!dim)
793 return NULL;
794 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
795 if (!bmap)
796 goto error;
797 bmap->dim = dim;
799 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
800 error:
801 isl_space_free(dim);
802 return NULL;
805 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
806 unsigned nparam, unsigned in, unsigned out, unsigned extra,
807 unsigned n_eq, unsigned n_ineq)
809 struct isl_basic_map *bmap;
810 isl_space *dim;
812 dim = isl_space_alloc(ctx, nparam, in, out);
813 if (!dim)
814 return NULL;
816 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
817 return bmap;
820 static void dup_constraints(
821 struct isl_basic_map *dst, struct isl_basic_map *src)
823 int i;
824 unsigned total = isl_basic_map_total_dim(src);
826 for (i = 0; i < src->n_eq; ++i) {
827 int j = isl_basic_map_alloc_equality(dst);
828 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
831 for (i = 0; i < src->n_ineq; ++i) {
832 int j = isl_basic_map_alloc_inequality(dst);
833 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
836 for (i = 0; i < src->n_div; ++i) {
837 int j = isl_basic_map_alloc_div(dst);
838 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
840 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
843 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
845 struct isl_basic_map *dup;
847 if (!bmap)
848 return NULL;
849 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
850 bmap->n_div, bmap->n_eq, bmap->n_ineq);
851 if (!dup)
852 return NULL;
853 dup_constraints(dup, bmap);
854 dup->flags = bmap->flags;
855 dup->sample = isl_vec_copy(bmap->sample);
856 return dup;
859 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
861 struct isl_basic_map *dup;
863 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
864 return (struct isl_basic_set *)dup;
867 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
869 if (!bset)
870 return NULL;
872 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
873 bset->ref++;
874 return bset;
876 return isl_basic_set_dup(bset);
879 struct isl_set *isl_set_copy(struct isl_set *set)
881 if (!set)
882 return NULL;
884 set->ref++;
885 return set;
888 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
890 if (!bmap)
891 return NULL;
893 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
894 bmap->ref++;
895 return bmap;
897 bmap = isl_basic_map_dup(bmap);
898 if (bmap)
899 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
900 return bmap;
903 struct isl_map *isl_map_copy(struct isl_map *map)
905 if (!map)
906 return NULL;
908 map->ref++;
909 return map;
912 void isl_basic_map_free(struct isl_basic_map *bmap)
914 if (!bmap)
915 return;
917 if (--bmap->ref > 0)
918 return;
920 isl_ctx_deref(bmap->ctx);
921 free(bmap->div);
922 isl_blk_free(bmap->ctx, bmap->block2);
923 free(bmap->ineq);
924 isl_blk_free(bmap->ctx, bmap->block);
925 isl_vec_free(bmap->sample);
926 isl_space_free(bmap->dim);
927 free(bmap);
930 void isl_basic_set_free(struct isl_basic_set *bset)
932 isl_basic_map_free((struct isl_basic_map *)bset);
935 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
937 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
940 __isl_give isl_map *isl_map_align_params_map_map_and(
941 __isl_take isl_map *map1, __isl_take isl_map *map2,
942 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
943 __isl_take isl_map *map2))
945 if (!map1 || !map2)
946 goto error;
947 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
948 return fn(map1, map2);
949 if (!isl_space_has_named_params(map1->dim) ||
950 !isl_space_has_named_params(map2->dim))
951 isl_die(map1->ctx, isl_error_invalid,
952 "unaligned unnamed parameters", goto error);
953 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
954 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
955 return fn(map1, map2);
956 error:
957 isl_map_free(map1);
958 isl_map_free(map2);
959 return NULL;
962 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
963 __isl_keep isl_map *map2,
964 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
966 int r;
968 if (!map1 || !map2)
969 return -1;
970 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
971 return fn(map1, map2);
972 if (!isl_space_has_named_params(map1->dim) ||
973 !isl_space_has_named_params(map2->dim))
974 isl_die(map1->ctx, isl_error_invalid,
975 "unaligned unnamed parameters", return -1);
976 map1 = isl_map_copy(map1);
977 map2 = isl_map_copy(map2);
978 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
979 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
980 r = fn(map1, map2);
981 isl_map_free(map1);
982 isl_map_free(map2);
983 return r;
986 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
988 struct isl_ctx *ctx;
989 if (!bmap)
990 return -1;
991 ctx = bmap->ctx;
992 isl_assert(ctx, room_for_con(bmap, 1), return -1);
993 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
994 return -1);
995 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
996 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
997 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
998 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
999 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1000 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1001 isl_int *t;
1002 int j = isl_basic_map_alloc_inequality(bmap);
1003 if (j < 0)
1004 return -1;
1005 t = bmap->ineq[j];
1006 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1007 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1008 bmap->eq[-1] = t;
1009 bmap->n_eq++;
1010 bmap->n_ineq--;
1011 bmap->eq--;
1012 return 0;
1014 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1015 bmap->extra - bmap->n_div);
1016 return bmap->n_eq++;
1019 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1021 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1024 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1026 if (!bmap)
1027 return -1;
1028 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1029 bmap->n_eq -= n;
1030 return 0;
1033 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1035 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1038 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1040 isl_int *t;
1041 if (!bmap)
1042 return -1;
1043 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1045 if (pos != bmap->n_eq - 1) {
1046 t = bmap->eq[pos];
1047 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1048 bmap->eq[bmap->n_eq - 1] = t;
1050 bmap->n_eq--;
1051 return 0;
1054 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1056 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1059 void isl_basic_map_inequality_to_equality(
1060 struct isl_basic_map *bmap, unsigned pos)
1062 isl_int *t;
1064 t = bmap->ineq[pos];
1065 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1066 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1067 bmap->eq[-1] = t;
1068 bmap->n_eq++;
1069 bmap->n_ineq--;
1070 bmap->eq--;
1071 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1072 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1073 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1074 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1077 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1079 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1082 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1084 struct isl_ctx *ctx;
1085 if (!bmap)
1086 return -1;
1087 ctx = bmap->ctx;
1088 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1089 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1090 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1091 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1092 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1093 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1094 1 + isl_basic_map_total_dim(bmap),
1095 bmap->extra - bmap->n_div);
1096 return bmap->n_ineq++;
1099 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1101 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1104 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1106 if (!bmap)
1107 return -1;
1108 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1109 bmap->n_ineq -= n;
1110 return 0;
1113 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1115 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1118 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1120 isl_int *t;
1121 if (!bmap)
1122 return -1;
1123 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1125 if (pos != bmap->n_ineq - 1) {
1126 t = bmap->ineq[pos];
1127 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1128 bmap->ineq[bmap->n_ineq - 1] = t;
1129 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1131 bmap->n_ineq--;
1132 return 0;
1135 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1137 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1140 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1141 isl_int *eq)
1143 int k;
1145 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1146 if (!bmap)
1147 return NULL;
1148 k = isl_basic_map_alloc_equality(bmap);
1149 if (k < 0)
1150 goto error;
1151 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1152 return bmap;
1153 error:
1154 isl_basic_map_free(bmap);
1155 return NULL;
1158 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1159 isl_int *eq)
1161 return (isl_basic_set *)
1162 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1165 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1166 isl_int *ineq)
1168 int k;
1170 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1171 if (!bmap)
1172 return NULL;
1173 k = isl_basic_map_alloc_inequality(bmap);
1174 if (k < 0)
1175 goto error;
1176 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1177 return bmap;
1178 error:
1179 isl_basic_map_free(bmap);
1180 return NULL;
1183 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1184 isl_int *ineq)
1186 return (isl_basic_set *)
1187 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1190 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1192 if (!bmap)
1193 return -1;
1194 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1195 isl_seq_clr(bmap->div[bmap->n_div] +
1196 1 + 1 + isl_basic_map_total_dim(bmap),
1197 bmap->extra - bmap->n_div);
1198 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1199 return bmap->n_div++;
1202 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1204 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1207 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1209 if (!bmap)
1210 return -1;
1211 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1212 bmap->n_div -= n;
1213 return 0;
1216 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1218 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1221 /* Copy constraint from src to dst, putting the vars of src at offset
1222 * dim_off in dst and the divs of src at offset div_off in dst.
1223 * If both sets are actually map, then dim_off applies to the input
1224 * variables.
1226 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1227 struct isl_basic_map *src_map, isl_int *src,
1228 unsigned in_off, unsigned out_off, unsigned div_off)
1230 unsigned src_nparam = isl_basic_map_n_param(src_map);
1231 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1232 unsigned src_in = isl_basic_map_n_in(src_map);
1233 unsigned dst_in = isl_basic_map_n_in(dst_map);
1234 unsigned src_out = isl_basic_map_n_out(src_map);
1235 unsigned dst_out = isl_basic_map_n_out(dst_map);
1236 isl_int_set(dst[0], src[0]);
1237 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1238 if (dst_nparam > src_nparam)
1239 isl_seq_clr(dst+1+src_nparam,
1240 dst_nparam - src_nparam);
1241 isl_seq_clr(dst+1+dst_nparam, in_off);
1242 isl_seq_cpy(dst+1+dst_nparam+in_off,
1243 src+1+src_nparam,
1244 isl_min(dst_in-in_off, src_in));
1245 if (dst_in-in_off > src_in)
1246 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1247 dst_in - in_off - src_in);
1248 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1249 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1250 src+1+src_nparam+src_in,
1251 isl_min(dst_out-out_off, src_out));
1252 if (dst_out-out_off > src_out)
1253 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1254 dst_out - out_off - src_out);
1255 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1256 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1257 src+1+src_nparam+src_in+src_out,
1258 isl_min(dst_map->extra-div_off, src_map->n_div));
1259 if (dst_map->n_div-div_off > src_map->n_div)
1260 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1261 div_off+src_map->n_div,
1262 dst_map->n_div - div_off - src_map->n_div);
1265 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1266 struct isl_basic_map *src_map, isl_int *src,
1267 unsigned in_off, unsigned out_off, unsigned div_off)
1269 isl_int_set(dst[0], src[0]);
1270 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1273 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1274 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1276 int i;
1277 unsigned div_off;
1279 if (!bmap1 || !bmap2)
1280 goto error;
1282 div_off = bmap1->n_div;
1284 for (i = 0; i < bmap2->n_eq; ++i) {
1285 int i1 = isl_basic_map_alloc_equality(bmap1);
1286 if (i1 < 0)
1287 goto error;
1288 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1289 i_pos, o_pos, div_off);
1292 for (i = 0; i < bmap2->n_ineq; ++i) {
1293 int i1 = isl_basic_map_alloc_inequality(bmap1);
1294 if (i1 < 0)
1295 goto error;
1296 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1297 i_pos, o_pos, div_off);
1300 for (i = 0; i < bmap2->n_div; ++i) {
1301 int i1 = isl_basic_map_alloc_div(bmap1);
1302 if (i1 < 0)
1303 goto error;
1304 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1305 i_pos, o_pos, div_off);
1308 isl_basic_map_free(bmap2);
1310 return bmap1;
1312 error:
1313 isl_basic_map_free(bmap1);
1314 isl_basic_map_free(bmap2);
1315 return NULL;
1318 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1319 struct isl_basic_set *bset2, unsigned pos)
1321 return (struct isl_basic_set *)
1322 add_constraints((struct isl_basic_map *)bset1,
1323 (struct isl_basic_map *)bset2, 0, pos);
1326 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1327 __isl_take isl_space *dim, unsigned extra,
1328 unsigned n_eq, unsigned n_ineq)
1330 struct isl_basic_map *ext;
1331 unsigned flags;
1332 int dims_ok;
1334 if (!dim)
1335 goto error;
1337 if (!base)
1338 goto error;
1340 dims_ok = isl_space_is_equal(base->dim, dim) &&
1341 base->extra >= base->n_div + extra;
1343 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1344 room_for_ineq(base, n_ineq)) {
1345 isl_space_free(dim);
1346 return base;
1349 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1350 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1351 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1352 extra += base->extra;
1353 n_eq += base->n_eq;
1354 n_ineq += base->n_ineq;
1356 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1357 dim = NULL;
1358 if (!ext)
1359 goto error;
1361 if (dims_ok)
1362 ext->sample = isl_vec_copy(base->sample);
1363 flags = base->flags;
1364 ext = add_constraints(ext, base, 0, 0);
1365 if (ext) {
1366 ext->flags = flags;
1367 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1370 return ext;
1372 error:
1373 isl_space_free(dim);
1374 isl_basic_map_free(base);
1375 return NULL;
1378 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1379 __isl_take isl_space *dim, unsigned extra,
1380 unsigned n_eq, unsigned n_ineq)
1382 return (struct isl_basic_set *)
1383 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1384 extra, n_eq, n_ineq);
1387 struct isl_basic_map *isl_basic_map_extend_constraints(
1388 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1390 if (!base)
1391 return NULL;
1392 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1393 0, n_eq, n_ineq);
1396 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1397 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1398 unsigned n_eq, unsigned n_ineq)
1400 struct isl_basic_map *bmap;
1401 isl_space *dim;
1403 if (!base)
1404 return NULL;
1405 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1406 if (!dim)
1407 goto error;
1409 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1410 return bmap;
1411 error:
1412 isl_basic_map_free(base);
1413 return NULL;
1416 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1417 unsigned nparam, unsigned dim, unsigned extra,
1418 unsigned n_eq, unsigned n_ineq)
1420 return (struct isl_basic_set *)
1421 isl_basic_map_extend((struct isl_basic_map *)base,
1422 nparam, 0, dim, extra, n_eq, n_ineq);
1425 struct isl_basic_set *isl_basic_set_extend_constraints(
1426 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1428 return (struct isl_basic_set *)
1429 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1430 n_eq, n_ineq);
1433 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1435 return (struct isl_basic_set *)
1436 isl_basic_map_cow((struct isl_basic_map *)bset);
1439 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1441 if (!bmap)
1442 return NULL;
1444 if (bmap->ref > 1) {
1445 bmap->ref--;
1446 bmap = isl_basic_map_dup(bmap);
1448 if (bmap)
1449 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1450 return bmap;
1453 struct isl_set *isl_set_cow(struct isl_set *set)
1455 if (!set)
1456 return NULL;
1458 if (set->ref == 1)
1459 return set;
1460 set->ref--;
1461 return isl_set_dup(set);
1464 struct isl_map *isl_map_cow(struct isl_map *map)
1466 if (!map)
1467 return NULL;
1469 if (map->ref == 1)
1470 return map;
1471 map->ref--;
1472 return isl_map_dup(map);
1475 static void swap_vars(struct isl_blk blk, isl_int *a,
1476 unsigned a_len, unsigned b_len)
1478 isl_seq_cpy(blk.data, a+a_len, b_len);
1479 isl_seq_cpy(blk.data+b_len, a, a_len);
1480 isl_seq_cpy(a, blk.data, b_len+a_len);
1483 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1484 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1486 int i;
1487 struct isl_blk blk;
1489 if (!bmap)
1490 goto error;
1492 isl_assert(bmap->ctx,
1493 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1495 if (n1 == 0 || n2 == 0)
1496 return bmap;
1498 bmap = isl_basic_map_cow(bmap);
1499 if (!bmap)
1500 return NULL;
1502 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1503 if (isl_blk_is_error(blk))
1504 goto error;
1506 for (i = 0; i < bmap->n_eq; ++i)
1507 swap_vars(blk,
1508 bmap->eq[i] + pos, n1, n2);
1510 for (i = 0; i < bmap->n_ineq; ++i)
1511 swap_vars(blk,
1512 bmap->ineq[i] + pos, n1, n2);
1514 for (i = 0; i < bmap->n_div; ++i)
1515 swap_vars(blk,
1516 bmap->div[i]+1 + pos, n1, n2);
1518 isl_blk_free(bmap->ctx, blk);
1520 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1521 bmap = isl_basic_map_gauss(bmap, NULL);
1522 return isl_basic_map_finalize(bmap);
1523 error:
1524 isl_basic_map_free(bmap);
1525 return NULL;
1528 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1529 __isl_take isl_basic_set *bset, unsigned n)
1531 unsigned dim;
1532 unsigned nparam;
1534 nparam = isl_basic_set_n_param(bset);
1535 dim = isl_basic_set_n_dim(bset);
1536 isl_assert(bset->ctx, n <= dim, goto error);
1538 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1539 error:
1540 isl_basic_set_free(bset);
1541 return NULL;
1544 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1546 int i = 0;
1547 unsigned total;
1548 if (!bmap)
1549 goto error;
1550 total = isl_basic_map_total_dim(bmap);
1551 isl_basic_map_free_div(bmap, bmap->n_div);
1552 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1553 if (bmap->n_eq > 0)
1554 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1555 else {
1556 i = isl_basic_map_alloc_equality(bmap);
1557 if (i < 0)
1558 goto error;
1560 isl_int_set_si(bmap->eq[i][0], 1);
1561 isl_seq_clr(bmap->eq[i]+1, total);
1562 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1563 isl_vec_free(bmap->sample);
1564 bmap->sample = NULL;
1565 return isl_basic_map_finalize(bmap);
1566 error:
1567 isl_basic_map_free(bmap);
1568 return NULL;
1571 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1573 return (struct isl_basic_set *)
1574 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1577 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1579 int i;
1580 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1581 isl_int *t = bmap->div[a];
1582 bmap->div[a] = bmap->div[b];
1583 bmap->div[b] = t;
1585 for (i = 0; i < bmap->n_eq; ++i)
1586 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1588 for (i = 0; i < bmap->n_ineq; ++i)
1589 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1591 for (i = 0; i < bmap->n_div; ++i)
1592 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1596 /* Eliminate the specified n dimensions starting at first from the
1597 * constraints, without removing the dimensions from the space.
1598 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1600 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1601 enum isl_dim_type type, unsigned first, unsigned n)
1603 int i;
1605 if (!map)
1606 return NULL;
1607 if (n == 0)
1608 return map;
1610 if (first + n > isl_map_dim(map, type) || first + n < first)
1611 isl_die(map->ctx, isl_error_invalid,
1612 "index out of bounds", goto error);
1614 map = isl_map_cow(map);
1615 if (!map)
1616 return NULL;
1618 for (i = 0; i < map->n; ++i) {
1619 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1620 if (!map->p[i])
1621 goto error;
1623 return map;
1624 error:
1625 isl_map_free(map);
1626 return NULL;
1629 /* Eliminate the specified n dimensions starting at first from the
1630 * constraints, without removing the dimensions from the space.
1631 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1633 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1634 enum isl_dim_type type, unsigned first, unsigned n)
1636 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1639 /* Eliminate the specified n dimensions starting at first from the
1640 * constraints, without removing the dimensions from the space.
1641 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1643 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1644 unsigned first, unsigned n)
1646 return isl_set_eliminate(set, isl_dim_set, first, n);
1649 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1650 __isl_take isl_basic_map *bmap)
1652 if (!bmap)
1653 return NULL;
1654 bmap = isl_basic_map_eliminate_vars(bmap,
1655 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1656 if (!bmap)
1657 return NULL;
1658 bmap->n_div = 0;
1659 return isl_basic_map_finalize(bmap);
1662 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1663 __isl_take isl_basic_set *bset)
1665 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1666 (struct isl_basic_map *)bset);
1669 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1671 int i;
1673 if (!map)
1674 return NULL;
1675 if (map->n == 0)
1676 return map;
1678 map = isl_map_cow(map);
1679 if (!map)
1680 return NULL;
1682 for (i = 0; i < map->n; ++i) {
1683 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1684 if (!map->p[i])
1685 goto error;
1687 return map;
1688 error:
1689 isl_map_free(map);
1690 return NULL;
1693 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1695 return isl_map_remove_divs(set);
1698 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1699 enum isl_dim_type type, unsigned first, unsigned n)
1701 if (!bmap)
1702 return NULL;
1703 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1704 goto error);
1705 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1706 return bmap;
1707 bmap = isl_basic_map_eliminate_vars(bmap,
1708 isl_basic_map_offset(bmap, type) - 1 + first, n);
1709 if (!bmap)
1710 return bmap;
1711 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1712 return bmap;
1713 bmap = isl_basic_map_drop(bmap, type, first, n);
1714 return bmap;
1715 error:
1716 isl_basic_map_free(bmap);
1717 return NULL;
1720 /* Return true if the definition of the given div (recursively) involves
1721 * any of the given variables.
1723 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1724 unsigned first, unsigned n)
1726 int i;
1727 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1729 if (isl_int_is_zero(bmap->div[div][0]))
1730 return 0;
1731 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1732 return 1;
1734 for (i = bmap->n_div - 1; i >= 0; --i) {
1735 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1736 continue;
1737 if (div_involves_vars(bmap, i, first, n))
1738 return 1;
1741 return 0;
1744 /* Remove all divs (recursively) involving any of the given dimensions
1745 * in their definitions.
1747 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1748 __isl_take isl_basic_map *bmap,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1751 int i;
1753 if (!bmap)
1754 return NULL;
1755 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1756 goto error);
1757 first += isl_basic_map_offset(bmap, type);
1759 for (i = bmap->n_div - 1; i >= 0; --i) {
1760 if (!div_involves_vars(bmap, i, first, n))
1761 continue;
1762 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1763 if (!bmap)
1764 return NULL;
1765 i = bmap->n_div;
1768 return bmap;
1769 error:
1770 isl_basic_map_free(bmap);
1771 return NULL;
1774 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1775 enum isl_dim_type type, unsigned first, unsigned n)
1777 int i;
1779 if (!map)
1780 return NULL;
1781 if (map->n == 0)
1782 return map;
1784 map = isl_map_cow(map);
1785 if (!map)
1786 return NULL;
1788 for (i = 0; i < map->n; ++i) {
1789 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1790 type, first, n);
1791 if (!map->p[i])
1792 goto error;
1794 return map;
1795 error:
1796 isl_map_free(map);
1797 return NULL;
1800 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1803 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1804 type, first, n);
1807 /* Does the desciption of "bmap" depend on the specified dimensions?
1808 * We also check whether the dimensions appear in any of the div definitions.
1809 * In principle there is no need for this check. If the dimensions appear
1810 * in a div definition, they also appear in the defining constraints of that
1811 * div.
1813 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1814 enum isl_dim_type type, unsigned first, unsigned n)
1816 int i;
1818 if (!bmap)
1819 return -1;
1821 if (first + n > isl_basic_map_dim(bmap, type))
1822 isl_die(bmap->ctx, isl_error_invalid,
1823 "index out of bounds", return -1);
1825 first += isl_basic_map_offset(bmap, type);
1826 for (i = 0; i < bmap->n_eq; ++i)
1827 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1828 return 1;
1829 for (i = 0; i < bmap->n_ineq; ++i)
1830 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1831 return 1;
1832 for (i = 0; i < bmap->n_div; ++i) {
1833 if (isl_int_is_zero(bmap->div[i][0]))
1834 continue;
1835 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
1836 return 1;
1839 return 0;
1842 int isl_map_involves_dims(__isl_keep isl_map *map,
1843 enum isl_dim_type type, unsigned first, unsigned n)
1845 int i;
1847 if (!map)
1848 return -1;
1850 if (first + n > isl_map_dim(map, type))
1851 isl_die(map->ctx, isl_error_invalid,
1852 "index out of bounds", return -1);
1854 for (i = 0; i < map->n; ++i) {
1855 int involves = isl_basic_map_involves_dims(map->p[i],
1856 type, first, n);
1857 if (involves < 0 || involves)
1858 return involves;
1861 return 0;
1864 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1865 enum isl_dim_type type, unsigned first, unsigned n)
1867 return isl_basic_map_involves_dims(bset, type, first, n);
1870 int isl_set_involves_dims(__isl_keep isl_set *set,
1871 enum isl_dim_type type, unsigned first, unsigned n)
1873 return isl_map_involves_dims(set, type, first, n);
1876 /* Return true if the definition of the given div is unknown or depends
1877 * on unknown divs.
1879 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1881 int i;
1882 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1884 if (isl_int_is_zero(bmap->div[div][0]))
1885 return 1;
1887 for (i = bmap->n_div - 1; i >= 0; --i) {
1888 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1889 continue;
1890 if (div_is_unknown(bmap, i))
1891 return 1;
1894 return 0;
1897 /* Remove all divs that are unknown or defined in terms of unknown divs.
1899 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1900 __isl_take isl_basic_map *bmap)
1902 int i;
1904 if (!bmap)
1905 return NULL;
1907 for (i = bmap->n_div - 1; i >= 0; --i) {
1908 if (!div_is_unknown(bmap, i))
1909 continue;
1910 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1911 if (!bmap)
1912 return NULL;
1913 i = bmap->n_div;
1916 return bmap;
1919 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1921 int i;
1923 if (!map)
1924 return NULL;
1925 if (map->n == 0)
1926 return map;
1928 map = isl_map_cow(map);
1929 if (!map)
1930 return NULL;
1932 for (i = 0; i < map->n; ++i) {
1933 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1934 if (!map->p[i])
1935 goto error;
1937 return map;
1938 error:
1939 isl_map_free(map);
1940 return NULL;
1943 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1945 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1948 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1949 __isl_take isl_basic_set *bset,
1950 enum isl_dim_type type, unsigned first, unsigned n)
1952 return (isl_basic_set *)
1953 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1956 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1957 enum isl_dim_type type, unsigned first, unsigned n)
1959 int i;
1961 if (n == 0)
1962 return map;
1964 map = isl_map_cow(map);
1965 if (!map)
1966 return NULL;
1967 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1969 for (i = 0; i < map->n; ++i) {
1970 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1971 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1972 if (!map->p[i])
1973 goto error;
1975 map = isl_map_drop(map, type, first, n);
1976 return map;
1977 error:
1978 isl_map_free(map);
1979 return NULL;
1982 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1983 enum isl_dim_type type, unsigned first, unsigned n)
1985 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1988 /* Project out n inputs starting at first using Fourier-Motzkin */
1989 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1990 unsigned first, unsigned n)
1992 return isl_map_remove_dims(map, isl_dim_in, first, n);
1995 static void dump_term(struct isl_basic_map *bmap,
1996 isl_int c, int pos, FILE *out)
1998 const char *name;
1999 unsigned in = isl_basic_map_n_in(bmap);
2000 unsigned dim = in + isl_basic_map_n_out(bmap);
2001 unsigned nparam = isl_basic_map_n_param(bmap);
2002 if (!pos)
2003 isl_int_print(out, c, 0);
2004 else {
2005 if (!isl_int_is_one(c))
2006 isl_int_print(out, c, 0);
2007 if (pos < 1 + nparam) {
2008 name = isl_space_get_dim_name(bmap->dim,
2009 isl_dim_param, pos - 1);
2010 if (name)
2011 fprintf(out, "%s", name);
2012 else
2013 fprintf(out, "p%d", pos - 1);
2014 } else if (pos < 1 + nparam + in)
2015 fprintf(out, "i%d", pos - 1 - nparam);
2016 else if (pos < 1 + nparam + dim)
2017 fprintf(out, "o%d", pos - 1 - nparam - in);
2018 else
2019 fprintf(out, "e%d", pos - 1 - nparam - dim);
2023 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2024 int sign, FILE *out)
2026 int i;
2027 int first;
2028 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2029 isl_int v;
2031 isl_int_init(v);
2032 for (i = 0, first = 1; i < len; ++i) {
2033 if (isl_int_sgn(c[i]) * sign <= 0)
2034 continue;
2035 if (!first)
2036 fprintf(out, " + ");
2037 first = 0;
2038 isl_int_abs(v, c[i]);
2039 dump_term(bmap, v, i, out);
2041 isl_int_clear(v);
2042 if (first)
2043 fprintf(out, "0");
2046 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2047 const char *op, FILE *out, int indent)
2049 int i;
2051 fprintf(out, "%*s", indent, "");
2053 dump_constraint_sign(bmap, c, 1, out);
2054 fprintf(out, " %s ", op);
2055 dump_constraint_sign(bmap, c, -1, out);
2057 fprintf(out, "\n");
2059 for (i = bmap->n_div; i < bmap->extra; ++i) {
2060 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2061 continue;
2062 fprintf(out, "%*s", indent, "");
2063 fprintf(out, "ERROR: unused div coefficient not zero\n");
2064 abort();
2068 static void dump_constraints(struct isl_basic_map *bmap,
2069 isl_int **c, unsigned n,
2070 const char *op, FILE *out, int indent)
2072 int i;
2074 for (i = 0; i < n; ++i)
2075 dump_constraint(bmap, c[i], op, out, indent);
2078 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2080 int j;
2081 int first = 1;
2082 unsigned total = isl_basic_map_total_dim(bmap);
2084 for (j = 0; j < 1 + total; ++j) {
2085 if (isl_int_is_zero(exp[j]))
2086 continue;
2087 if (!first && isl_int_is_pos(exp[j]))
2088 fprintf(out, "+");
2089 dump_term(bmap, exp[j], j, out);
2090 first = 0;
2094 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2096 int i;
2098 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2099 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2101 for (i = 0; i < bmap->n_div; ++i) {
2102 fprintf(out, "%*s", indent, "");
2103 fprintf(out, "e%d = [(", i);
2104 dump_affine(bmap, bmap->div[i]+1, out);
2105 fprintf(out, ")/");
2106 isl_int_print(out, bmap->div[i][0], 0);
2107 fprintf(out, "]\n");
2111 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2112 FILE *out, int indent)
2114 if (!bset) {
2115 fprintf(out, "null basic set\n");
2116 return;
2119 fprintf(out, "%*s", indent, "");
2120 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2121 bset->ref, bset->dim->nparam, bset->dim->n_out,
2122 bset->extra, bset->flags);
2123 dump((struct isl_basic_map *)bset, out, indent);
2126 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2127 FILE *out, int indent)
2129 if (!bmap) {
2130 fprintf(out, "null basic map\n");
2131 return;
2134 fprintf(out, "%*s", indent, "");
2135 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2136 "flags: %x, n_name: %d\n",
2137 bmap->ref,
2138 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2139 bmap->extra, bmap->flags, bmap->dim->n_id);
2140 dump(bmap, out, indent);
2143 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2145 unsigned total;
2146 if (!bmap)
2147 return -1;
2148 total = isl_basic_map_total_dim(bmap);
2149 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2150 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2151 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2152 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2153 return 0;
2156 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2157 unsigned flags)
2159 struct isl_set *set;
2161 if (!dim)
2162 return NULL;
2163 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2164 isl_assert(dim->ctx, n >= 0, goto error);
2165 set = isl_alloc(dim->ctx, struct isl_set,
2166 sizeof(struct isl_set) +
2167 (n - 1) * sizeof(struct isl_basic_set *));
2168 if (!set)
2169 goto error;
2171 set->ctx = dim->ctx;
2172 isl_ctx_ref(set->ctx);
2173 set->ref = 1;
2174 set->size = n;
2175 set->n = 0;
2176 set->dim = dim;
2177 set->flags = flags;
2178 return set;
2179 error:
2180 isl_space_free(dim);
2181 return NULL;
2184 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2185 unsigned nparam, unsigned dim, int n, unsigned flags)
2187 struct isl_set *set;
2188 isl_space *dims;
2190 dims = isl_space_alloc(ctx, nparam, 0, dim);
2191 if (!dims)
2192 return NULL;
2194 set = isl_set_alloc_space(dims, n, flags);
2195 return set;
2198 /* Make sure "map" has room for at least "n" more basic maps.
2200 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2202 int i;
2203 struct isl_map *grown = NULL;
2205 if (!map)
2206 return NULL;
2207 isl_assert(map->ctx, n >= 0, goto error);
2208 if (map->n + n <= map->size)
2209 return map;
2210 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2211 if (!grown)
2212 goto error;
2213 for (i = 0; i < map->n; ++i) {
2214 grown->p[i] = isl_basic_map_copy(map->p[i]);
2215 if (!grown->p[i])
2216 goto error;
2217 grown->n++;
2219 isl_map_free(map);
2220 return grown;
2221 error:
2222 isl_map_free(grown);
2223 isl_map_free(map);
2224 return NULL;
2227 /* Make sure "set" has room for at least "n" more basic sets.
2229 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2231 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2234 struct isl_set *isl_set_dup(struct isl_set *set)
2236 int i;
2237 struct isl_set *dup;
2239 if (!set)
2240 return NULL;
2242 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2243 if (!dup)
2244 return NULL;
2245 for (i = 0; i < set->n; ++i)
2246 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2247 return dup;
2250 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2252 return isl_map_from_basic_map(bset);
2255 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2257 struct isl_map *map;
2259 if (!bmap)
2260 return NULL;
2262 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2263 return isl_map_add_basic_map(map, bmap);
2266 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2267 __isl_take isl_basic_set *bset)
2269 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2270 (struct isl_basic_map *)bset);
2273 void isl_set_free(struct isl_set *set)
2275 int i;
2277 if (!set)
2278 return;
2280 if (--set->ref > 0)
2281 return;
2283 isl_ctx_deref(set->ctx);
2284 for (i = 0; i < set->n; ++i)
2285 isl_basic_set_free(set->p[i]);
2286 isl_space_free(set->dim);
2287 free(set);
2290 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2292 int i;
2294 if (!set) {
2295 fprintf(out, "null set\n");
2296 return;
2299 fprintf(out, "%*s", indent, "");
2300 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2301 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2302 set->flags);
2303 for (i = 0; i < set->n; ++i) {
2304 fprintf(out, "%*s", indent, "");
2305 fprintf(out, "basic set %d:\n", i);
2306 isl_basic_set_print_internal(set->p[i], out, indent+4);
2310 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2312 int i;
2314 if (!map) {
2315 fprintf(out, "null map\n");
2316 return;
2319 fprintf(out, "%*s", indent, "");
2320 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2321 "flags: %x, n_name: %d\n",
2322 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2323 map->dim->n_out, map->flags, map->dim->n_id);
2324 for (i = 0; i < map->n; ++i) {
2325 fprintf(out, "%*s", indent, "");
2326 fprintf(out, "basic map %d:\n", i);
2327 isl_basic_map_print_internal(map->p[i], out, indent+4);
2331 struct isl_basic_map *isl_basic_map_intersect_domain(
2332 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2334 struct isl_basic_map *bmap_domain;
2336 if (!bmap || !bset)
2337 goto error;
2339 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2340 bset->dim, isl_dim_param), goto error);
2342 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2343 isl_assert(bset->ctx,
2344 isl_basic_map_compatible_domain(bmap, bset), goto error);
2346 bmap = isl_basic_map_cow(bmap);
2347 if (!bmap)
2348 goto error;
2349 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2350 bset->n_div, bset->n_eq, bset->n_ineq);
2351 bmap_domain = isl_basic_map_from_domain(bset);
2352 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2354 bmap = isl_basic_map_simplify(bmap);
2355 return isl_basic_map_finalize(bmap);
2356 error:
2357 isl_basic_map_free(bmap);
2358 isl_basic_set_free(bset);
2359 return NULL;
2362 struct isl_basic_map *isl_basic_map_intersect_range(
2363 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2365 struct isl_basic_map *bmap_range;
2367 if (!bmap || !bset)
2368 goto error;
2370 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2371 bset->dim, isl_dim_param), goto error);
2373 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2374 isl_assert(bset->ctx,
2375 isl_basic_map_compatible_range(bmap, bset), goto error);
2377 if (isl_basic_set_is_universe(bset)) {
2378 isl_basic_set_free(bset);
2379 return bmap;
2382 bmap = isl_basic_map_cow(bmap);
2383 if (!bmap)
2384 goto error;
2385 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2386 bset->n_div, bset->n_eq, bset->n_ineq);
2387 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2388 bmap = add_constraints(bmap, bmap_range, 0, 0);
2390 bmap = isl_basic_map_simplify(bmap);
2391 return isl_basic_map_finalize(bmap);
2392 error:
2393 isl_basic_map_free(bmap);
2394 isl_basic_set_free(bset);
2395 return NULL;
2398 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2400 int i;
2401 unsigned total;
2402 isl_int s;
2404 total = 1 + isl_basic_map_total_dim(bmap);
2405 if (total != vec->size)
2406 return -1;
2408 isl_int_init(s);
2410 for (i = 0; i < bmap->n_eq; ++i) {
2411 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2412 if (!isl_int_is_zero(s)) {
2413 isl_int_clear(s);
2414 return 0;
2418 for (i = 0; i < bmap->n_ineq; ++i) {
2419 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2420 if (isl_int_is_neg(s)) {
2421 isl_int_clear(s);
2422 return 0;
2426 isl_int_clear(s);
2428 return 1;
2431 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2433 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2436 struct isl_basic_map *isl_basic_map_intersect(
2437 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2439 struct isl_vec *sample = NULL;
2441 if (!bmap1 || !bmap2)
2442 goto error;
2444 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2445 bmap2->dim, isl_dim_param), goto error);
2446 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2447 isl_space_dim(bmap1->dim, isl_dim_param) &&
2448 isl_space_dim(bmap2->dim, isl_dim_all) !=
2449 isl_space_dim(bmap2->dim, isl_dim_param))
2450 return isl_basic_map_intersect(bmap2, bmap1);
2452 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2453 isl_space_dim(bmap2->dim, isl_dim_param))
2454 isl_assert(bmap1->ctx,
2455 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2457 if (bmap1->sample &&
2458 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2459 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2460 sample = isl_vec_copy(bmap1->sample);
2461 else if (bmap2->sample &&
2462 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2463 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2464 sample = isl_vec_copy(bmap2->sample);
2466 bmap1 = isl_basic_map_cow(bmap1);
2467 if (!bmap1)
2468 goto error;
2469 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2470 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2471 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2473 if (!bmap1)
2474 isl_vec_free(sample);
2475 else if (sample) {
2476 isl_vec_free(bmap1->sample);
2477 bmap1->sample = sample;
2480 bmap1 = isl_basic_map_simplify(bmap1);
2481 return isl_basic_map_finalize(bmap1);
2482 error:
2483 if (sample)
2484 isl_vec_free(sample);
2485 isl_basic_map_free(bmap1);
2486 isl_basic_map_free(bmap2);
2487 return NULL;
2490 struct isl_basic_set *isl_basic_set_intersect(
2491 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2493 return (struct isl_basic_set *)
2494 isl_basic_map_intersect(
2495 (struct isl_basic_map *)bset1,
2496 (struct isl_basic_map *)bset2);
2499 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2500 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2502 return isl_basic_set_intersect(bset1, bset2);
2505 /* Special case of isl_map_intersect, where both map1 and map2
2506 * are convex, without any divs and such that either map1 or map2
2507 * contains a single constraint. This constraint is then simply
2508 * added to the other map.
2510 static __isl_give isl_map *map_intersect_add_constraint(
2511 __isl_take isl_map *map1, __isl_take isl_map *map2)
2513 isl_assert(map1->ctx, map1->n == 1, goto error);
2514 isl_assert(map2->ctx, map1->n == 1, goto error);
2515 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2516 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2518 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2519 return isl_map_intersect(map2, map1);
2521 isl_assert(map2->ctx,
2522 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2524 map1 = isl_map_cow(map1);
2525 if (!map1)
2526 goto error;
2527 if (isl_map_plain_is_empty(map1)) {
2528 isl_map_free(map2);
2529 return map1;
2531 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2532 if (map2->p[0]->n_eq == 1)
2533 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2534 else
2535 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2536 map2->p[0]->ineq[0]);
2538 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2539 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2540 if (!map1->p[0])
2541 goto error;
2543 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2544 isl_basic_map_free(map1->p[0]);
2545 map1->n = 0;
2548 isl_map_free(map2);
2550 return map1;
2551 error:
2552 isl_map_free(map1);
2553 isl_map_free(map2);
2554 return NULL;
2557 /* map2 may be either a parameter domain or a map living in the same
2558 * space as map1.
2560 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2561 __isl_take isl_map *map2)
2563 unsigned flags = 0;
2564 struct isl_map *result;
2565 int i, j;
2567 if (!map1 || !map2)
2568 goto error;
2570 if (isl_map_plain_is_empty(map1) &&
2571 isl_space_is_equal(map1->dim, map2->dim)) {
2572 isl_map_free(map2);
2573 return map1;
2575 if (isl_map_plain_is_empty(map2) &&
2576 isl_space_is_equal(map1->dim, map2->dim)) {
2577 isl_map_free(map1);
2578 return map2;
2581 if (map1->n == 1 && map2->n == 1 &&
2582 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2583 isl_space_is_equal(map1->dim, map2->dim) &&
2584 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2585 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2586 return map_intersect_add_constraint(map1, map2);
2588 if (isl_space_dim(map2->dim, isl_dim_all) !=
2589 isl_space_dim(map2->dim, isl_dim_param))
2590 isl_assert(map1->ctx,
2591 isl_space_is_equal(map1->dim, map2->dim), goto error);
2593 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2594 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2595 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2597 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2598 map1->n * map2->n, flags);
2599 if (!result)
2600 goto error;
2601 for (i = 0; i < map1->n; ++i)
2602 for (j = 0; j < map2->n; ++j) {
2603 struct isl_basic_map *part;
2604 part = isl_basic_map_intersect(
2605 isl_basic_map_copy(map1->p[i]),
2606 isl_basic_map_copy(map2->p[j]));
2607 if (isl_basic_map_is_empty(part))
2608 isl_basic_map_free(part);
2609 else
2610 result = isl_map_add_basic_map(result, part);
2611 if (!result)
2612 goto error;
2614 isl_map_free(map1);
2615 isl_map_free(map2);
2616 return result;
2617 error:
2618 isl_map_free(map1);
2619 isl_map_free(map2);
2620 return NULL;
2623 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2624 __isl_take isl_map *map2)
2626 if (!map1 || !map2)
2627 goto error;
2628 if (!isl_space_is_equal(map1->dim, map2->dim))
2629 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2630 "spaces don't match", goto error);
2631 return map_intersect_internal(map1, map2);
2632 error:
2633 isl_map_free(map1);
2634 isl_map_free(map2);
2635 return NULL;
2638 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2639 __isl_take isl_map *map2)
2641 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2644 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2646 return (struct isl_set *)
2647 isl_map_intersect((struct isl_map *)set1,
2648 (struct isl_map *)set2);
2651 /* map_intersect_internal accepts intersections
2652 * with parameter domains, so we can just call that function.
2654 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2655 __isl_take isl_set *params)
2657 return map_intersect_internal(map, params);
2660 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2661 __isl_take isl_map *map2)
2663 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2666 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2667 __isl_take isl_set *params)
2669 return isl_map_intersect_params(set, params);
2672 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2674 isl_space *dim;
2675 struct isl_basic_set *bset;
2676 unsigned in;
2678 if (!bmap)
2679 return NULL;
2680 bmap = isl_basic_map_cow(bmap);
2681 if (!bmap)
2682 return NULL;
2683 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2684 in = isl_basic_map_n_in(bmap);
2685 bset = isl_basic_set_from_basic_map(bmap);
2686 bset = isl_basic_set_swap_vars(bset, in);
2687 return isl_basic_map_from_basic_set(bset, dim);
2690 static __isl_give isl_basic_map *basic_map_space_reset(
2691 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2693 isl_space *space;
2695 if (!isl_space_is_named_or_nested(bmap->dim, type))
2696 return bmap;
2698 space = isl_basic_map_get_space(bmap);
2699 space = isl_space_reset(space, type);
2700 bmap = isl_basic_map_reset_space(bmap, space);
2701 return bmap;
2704 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2705 enum isl_dim_type type, unsigned pos, unsigned n)
2707 isl_space *res_dim;
2708 struct isl_basic_map *res;
2709 struct isl_dim_map *dim_map;
2710 unsigned total, off;
2711 enum isl_dim_type t;
2713 if (n == 0)
2714 return basic_map_space_reset(bmap, type);
2716 if (!bmap)
2717 return NULL;
2719 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2721 total = isl_basic_map_total_dim(bmap) + n;
2722 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2723 off = 0;
2724 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2725 if (t != type) {
2726 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2727 } else {
2728 unsigned size = isl_basic_map_dim(bmap, t);
2729 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2730 0, pos, off);
2731 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2732 pos, size - pos, off + pos + n);
2734 off += isl_space_dim(res_dim, t);
2736 isl_dim_map_div(dim_map, bmap, off);
2738 res = isl_basic_map_alloc_space(res_dim,
2739 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2740 if (isl_basic_map_is_rational(bmap))
2741 res = isl_basic_map_set_rational(res);
2742 if (isl_basic_map_plain_is_empty(bmap)) {
2743 isl_basic_map_free(bmap);
2744 return isl_basic_map_set_to_empty(res);
2746 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2747 return isl_basic_map_finalize(res);
2750 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2751 enum isl_dim_type type, unsigned n)
2753 if (!bmap)
2754 return NULL;
2755 return isl_basic_map_insert(bmap, type,
2756 isl_basic_map_dim(bmap, type), n);
2759 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2760 enum isl_dim_type type, unsigned n)
2762 if (!bset)
2763 return NULL;
2764 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2765 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2766 error:
2767 isl_basic_set_free(bset);
2768 return NULL;
2771 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2772 enum isl_dim_type type)
2774 isl_space *space;
2776 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2777 return map;
2779 space = isl_map_get_space(map);
2780 space = isl_space_reset(space, type);
2781 map = isl_map_reset_space(map, space);
2782 return map;
2785 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2786 enum isl_dim_type type, unsigned pos, unsigned n)
2788 int i;
2790 if (n == 0)
2791 return map_space_reset(map, type);
2793 map = isl_map_cow(map);
2794 if (!map)
2795 return NULL;
2797 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2798 if (!map->dim)
2799 goto error;
2801 for (i = 0; i < map->n; ++i) {
2802 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2803 if (!map->p[i])
2804 goto error;
2807 return map;
2808 error:
2809 isl_map_free(map);
2810 return NULL;
2813 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2814 enum isl_dim_type type, unsigned pos, unsigned n)
2816 return isl_map_insert_dims(set, type, pos, n);
2819 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2820 enum isl_dim_type type, unsigned n)
2822 if (!map)
2823 return NULL;
2824 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2827 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2828 enum isl_dim_type type, unsigned n)
2830 if (!set)
2831 return NULL;
2832 isl_assert(set->ctx, type != isl_dim_in, goto error);
2833 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2834 error:
2835 isl_set_free(set);
2836 return NULL;
2839 __isl_give isl_basic_map *isl_basic_map_move_dims(
2840 __isl_take isl_basic_map *bmap,
2841 enum isl_dim_type dst_type, unsigned dst_pos,
2842 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2844 struct isl_dim_map *dim_map;
2845 struct isl_basic_map *res;
2846 enum isl_dim_type t;
2847 unsigned total, off;
2849 if (!bmap)
2850 return NULL;
2851 if (n == 0)
2852 return bmap;
2854 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2855 goto error);
2857 if (dst_type == src_type && dst_pos == src_pos)
2858 return bmap;
2860 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2862 if (pos(bmap->dim, dst_type) + dst_pos ==
2863 pos(bmap->dim, src_type) + src_pos +
2864 ((src_type < dst_type) ? n : 0)) {
2865 bmap = isl_basic_map_cow(bmap);
2866 if (!bmap)
2867 return NULL;
2869 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2870 src_type, src_pos, n);
2871 if (!bmap->dim)
2872 goto error;
2874 bmap = isl_basic_map_finalize(bmap);
2876 return bmap;
2879 total = isl_basic_map_total_dim(bmap);
2880 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2882 off = 0;
2883 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2884 unsigned size = isl_space_dim(bmap->dim, t);
2885 if (t == dst_type) {
2886 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2887 0, dst_pos, off);
2888 off += dst_pos;
2889 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2890 src_pos, n, off);
2891 off += n;
2892 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2893 dst_pos, size - dst_pos, off);
2894 off += size - dst_pos;
2895 } else if (t == src_type) {
2896 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2897 0, src_pos, off);
2898 off += src_pos;
2899 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2900 src_pos + n, size - src_pos - n, off);
2901 off += size - src_pos - n;
2902 } else {
2903 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2904 off += size;
2907 isl_dim_map_div(dim_map, bmap, off);
2909 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2910 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2911 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2913 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2914 src_type, src_pos, n);
2915 if (!bmap->dim)
2916 goto error;
2918 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2919 bmap = isl_basic_map_gauss(bmap, NULL);
2920 bmap = isl_basic_map_finalize(bmap);
2922 return bmap;
2923 error:
2924 isl_basic_map_free(bmap);
2925 return NULL;
2928 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2929 enum isl_dim_type dst_type, unsigned dst_pos,
2930 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2932 return (isl_basic_set *)isl_basic_map_move_dims(
2933 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2936 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2937 enum isl_dim_type dst_type, unsigned dst_pos,
2938 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2940 if (!set)
2941 return NULL;
2942 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2943 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2944 src_type, src_pos, n);
2945 error:
2946 isl_set_free(set);
2947 return NULL;
2950 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2951 enum isl_dim_type dst_type, unsigned dst_pos,
2952 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2954 int i;
2956 if (!map)
2957 return NULL;
2958 if (n == 0)
2959 return map;
2961 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2962 goto error);
2964 if (dst_type == src_type && dst_pos == src_pos)
2965 return map;
2967 isl_assert(map->ctx, dst_type != src_type, goto error);
2969 map = isl_map_cow(map);
2970 if (!map)
2971 return NULL;
2973 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2974 if (!map->dim)
2975 goto error;
2977 for (i = 0; i < map->n; ++i) {
2978 map->p[i] = isl_basic_map_move_dims(map->p[i],
2979 dst_type, dst_pos,
2980 src_type, src_pos, n);
2981 if (!map->p[i])
2982 goto error;
2985 return map;
2986 error:
2987 isl_map_free(map);
2988 return NULL;
2991 /* Move the specified dimensions to the last columns right before
2992 * the divs. Don't change the dimension specification of bmap.
2993 * That's the responsibility of the caller.
2995 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2996 enum isl_dim_type type, unsigned first, unsigned n)
2998 struct isl_dim_map *dim_map;
2999 struct isl_basic_map *res;
3000 enum isl_dim_type t;
3001 unsigned total, off;
3003 if (!bmap)
3004 return NULL;
3005 if (pos(bmap->dim, type) + first + n ==
3006 1 + isl_space_dim(bmap->dim, isl_dim_all))
3007 return bmap;
3009 total = isl_basic_map_total_dim(bmap);
3010 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3012 off = 0;
3013 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3014 unsigned size = isl_space_dim(bmap->dim, t);
3015 if (t == type) {
3016 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3017 0, first, off);
3018 off += first;
3019 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3020 first, n, total - bmap->n_div - n);
3021 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3022 first + n, size - (first + n), off);
3023 off += size - (first + n);
3024 } else {
3025 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3026 off += size;
3029 isl_dim_map_div(dim_map, bmap, off + n);
3031 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3032 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3033 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3034 return res;
3037 /* Turn the n dimensions of type type, starting at first
3038 * into existentially quantified variables.
3040 __isl_give isl_basic_map *isl_basic_map_project_out(
3041 __isl_take isl_basic_map *bmap,
3042 enum isl_dim_type type, unsigned first, unsigned n)
3044 int i;
3045 size_t row_size;
3046 isl_int **new_div;
3047 isl_int *old;
3049 if (n == 0)
3050 return basic_map_space_reset(bmap, type);
3052 if (!bmap)
3053 return NULL;
3055 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3056 return isl_basic_map_remove_dims(bmap, type, first, n);
3058 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3059 goto error);
3061 bmap = move_last(bmap, type, first, n);
3062 bmap = isl_basic_map_cow(bmap);
3063 if (!bmap)
3064 return NULL;
3066 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3067 old = bmap->block2.data;
3068 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3069 (bmap->extra + n) * (1 + row_size));
3070 if (!bmap->block2.data)
3071 goto error;
3072 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3073 if (!new_div)
3074 goto error;
3075 for (i = 0; i < n; ++i) {
3076 new_div[i] = bmap->block2.data +
3077 (bmap->extra + i) * (1 + row_size);
3078 isl_seq_clr(new_div[i], 1 + row_size);
3080 for (i = 0; i < bmap->extra; ++i)
3081 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3082 free(bmap->div);
3083 bmap->div = new_div;
3084 bmap->n_div += n;
3085 bmap->extra += n;
3087 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3088 if (!bmap->dim)
3089 goto error;
3090 bmap = isl_basic_map_simplify(bmap);
3091 bmap = isl_basic_map_drop_redundant_divs(bmap);
3092 return isl_basic_map_finalize(bmap);
3093 error:
3094 isl_basic_map_free(bmap);
3095 return NULL;
3098 /* Turn the n dimensions of type type, starting at first
3099 * into existentially quantified variables.
3101 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3102 enum isl_dim_type type, unsigned first, unsigned n)
3104 return (isl_basic_set *)isl_basic_map_project_out(
3105 (isl_basic_map *)bset, type, first, n);
3108 /* Turn the n dimensions of type type, starting at first
3109 * into existentially quantified variables.
3111 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3112 enum isl_dim_type type, unsigned first, unsigned n)
3114 int i;
3116 if (!map)
3117 return NULL;
3119 if (n == 0)
3120 return map_space_reset(map, type);
3122 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3124 map = isl_map_cow(map);
3125 if (!map)
3126 return NULL;
3128 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3129 if (!map->dim)
3130 goto error;
3132 for (i = 0; i < map->n; ++i) {
3133 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3134 if (!map->p[i])
3135 goto error;
3138 return map;
3139 error:
3140 isl_map_free(map);
3141 return NULL;
3144 /* Turn the n dimensions of type type, starting at first
3145 * into existentially quantified variables.
3147 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3148 enum isl_dim_type type, unsigned first, unsigned n)
3150 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3153 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3155 int i, j;
3157 for (i = 0; i < n; ++i) {
3158 j = isl_basic_map_alloc_div(bmap);
3159 if (j < 0)
3160 goto error;
3161 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3163 return bmap;
3164 error:
3165 isl_basic_map_free(bmap);
3166 return NULL;
3169 struct isl_basic_map *isl_basic_map_apply_range(
3170 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3172 isl_space *dim_result = NULL;
3173 struct isl_basic_map *bmap;
3174 unsigned n_in, n_out, n, nparam, total, pos;
3175 struct isl_dim_map *dim_map1, *dim_map2;
3177 if (!bmap1 || !bmap2)
3178 goto error;
3180 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3181 isl_space_copy(bmap2->dim));
3183 n_in = isl_basic_map_n_in(bmap1);
3184 n_out = isl_basic_map_n_out(bmap2);
3185 n = isl_basic_map_n_out(bmap1);
3186 nparam = isl_basic_map_n_param(bmap1);
3188 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3189 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3190 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3191 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3192 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3193 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3194 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3195 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3196 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3197 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3198 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3200 bmap = isl_basic_map_alloc_space(dim_result,
3201 bmap1->n_div + bmap2->n_div + n,
3202 bmap1->n_eq + bmap2->n_eq,
3203 bmap1->n_ineq + bmap2->n_ineq);
3204 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3205 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3206 bmap = add_divs(bmap, n);
3207 bmap = isl_basic_map_simplify(bmap);
3208 bmap = isl_basic_map_drop_redundant_divs(bmap);
3209 return isl_basic_map_finalize(bmap);
3210 error:
3211 isl_basic_map_free(bmap1);
3212 isl_basic_map_free(bmap2);
3213 return NULL;
3216 struct isl_basic_set *isl_basic_set_apply(
3217 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3219 if (!bset || !bmap)
3220 goto error;
3222 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3223 goto error);
3225 return (struct isl_basic_set *)
3226 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3227 error:
3228 isl_basic_set_free(bset);
3229 isl_basic_map_free(bmap);
3230 return NULL;
3233 struct isl_basic_map *isl_basic_map_apply_domain(
3234 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3236 if (!bmap1 || !bmap2)
3237 goto error;
3239 isl_assert(bmap1->ctx,
3240 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3241 isl_assert(bmap1->ctx,
3242 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3243 goto error);
3245 bmap1 = isl_basic_map_reverse(bmap1);
3246 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3247 return isl_basic_map_reverse(bmap1);
3248 error:
3249 isl_basic_map_free(bmap1);
3250 isl_basic_map_free(bmap2);
3251 return NULL;
3254 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3255 * A \cap B -> f(A) + f(B)
3257 struct isl_basic_map *isl_basic_map_sum(
3258 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3260 unsigned n_in, n_out, nparam, total, pos;
3261 struct isl_basic_map *bmap = NULL;
3262 struct isl_dim_map *dim_map1, *dim_map2;
3263 int i;
3265 if (!bmap1 || !bmap2)
3266 goto error;
3268 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3269 goto error);
3271 nparam = isl_basic_map_n_param(bmap1);
3272 n_in = isl_basic_map_n_in(bmap1);
3273 n_out = isl_basic_map_n_out(bmap1);
3275 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3276 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3277 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3278 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3279 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3280 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3281 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3282 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3283 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3284 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3285 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3287 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3288 bmap1->n_div + bmap2->n_div + 2 * n_out,
3289 bmap1->n_eq + bmap2->n_eq + n_out,
3290 bmap1->n_ineq + bmap2->n_ineq);
3291 for (i = 0; i < n_out; ++i) {
3292 int j = isl_basic_map_alloc_equality(bmap);
3293 if (j < 0)
3294 goto error;
3295 isl_seq_clr(bmap->eq[j], 1+total);
3296 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3297 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3298 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3300 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3301 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3302 bmap = add_divs(bmap, 2 * n_out);
3304 bmap = isl_basic_map_simplify(bmap);
3305 return isl_basic_map_finalize(bmap);
3306 error:
3307 isl_basic_map_free(bmap);
3308 isl_basic_map_free(bmap1);
3309 isl_basic_map_free(bmap2);
3310 return NULL;
3313 /* Given two maps A -> f(A) and B -> g(B), construct a map
3314 * A \cap B -> f(A) + f(B)
3316 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3318 struct isl_map *result;
3319 int i, j;
3321 if (!map1 || !map2)
3322 goto error;
3324 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3326 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3327 map1->n * map2->n, 0);
3328 if (!result)
3329 goto error;
3330 for (i = 0; i < map1->n; ++i)
3331 for (j = 0; j < map2->n; ++j) {
3332 struct isl_basic_map *part;
3333 part = isl_basic_map_sum(
3334 isl_basic_map_copy(map1->p[i]),
3335 isl_basic_map_copy(map2->p[j]));
3336 if (isl_basic_map_is_empty(part))
3337 isl_basic_map_free(part);
3338 else
3339 result = isl_map_add_basic_map(result, part);
3340 if (!result)
3341 goto error;
3343 isl_map_free(map1);
3344 isl_map_free(map2);
3345 return result;
3346 error:
3347 isl_map_free(map1);
3348 isl_map_free(map2);
3349 return NULL;
3352 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3353 __isl_take isl_set *set2)
3355 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3358 /* Given a basic map A -> f(A), construct A -> -f(A).
3360 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3362 int i, j;
3363 unsigned off, n;
3365 bmap = isl_basic_map_cow(bmap);
3366 if (!bmap)
3367 return NULL;
3369 n = isl_basic_map_dim(bmap, isl_dim_out);
3370 off = isl_basic_map_offset(bmap, isl_dim_out);
3371 for (i = 0; i < bmap->n_eq; ++i)
3372 for (j = 0; j < n; ++j)
3373 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3374 for (i = 0; i < bmap->n_ineq; ++i)
3375 for (j = 0; j < n; ++j)
3376 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3377 for (i = 0; i < bmap->n_div; ++i)
3378 for (j = 0; j < n; ++j)
3379 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3380 bmap = isl_basic_map_gauss(bmap, NULL);
3381 return isl_basic_map_finalize(bmap);
3384 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3386 return isl_basic_map_neg(bset);
3389 /* Given a map A -> f(A), construct A -> -f(A).
3391 struct isl_map *isl_map_neg(struct isl_map *map)
3393 int i;
3395 map = isl_map_cow(map);
3396 if (!map)
3397 return NULL;
3399 for (i = 0; i < map->n; ++i) {
3400 map->p[i] = isl_basic_map_neg(map->p[i]);
3401 if (!map->p[i])
3402 goto error;
3405 return map;
3406 error:
3407 isl_map_free(map);
3408 return NULL;
3411 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3413 return (isl_set *)isl_map_neg((isl_map *)set);
3416 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3417 * A -> floor(f(A)/d).
3419 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3420 isl_int d)
3422 unsigned n_in, n_out, nparam, total, pos;
3423 struct isl_basic_map *result = NULL;
3424 struct isl_dim_map *dim_map;
3425 int i;
3427 if (!bmap)
3428 return NULL;
3430 nparam = isl_basic_map_n_param(bmap);
3431 n_in = isl_basic_map_n_in(bmap);
3432 n_out = isl_basic_map_n_out(bmap);
3434 total = nparam + n_in + n_out + bmap->n_div + n_out;
3435 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3436 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3437 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3438 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3439 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3441 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3442 bmap->n_div + n_out,
3443 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3444 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3445 result = add_divs(result, n_out);
3446 for (i = 0; i < n_out; ++i) {
3447 int j;
3448 j = isl_basic_map_alloc_inequality(result);
3449 if (j < 0)
3450 goto error;
3451 isl_seq_clr(result->ineq[j], 1+total);
3452 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3453 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3454 j = isl_basic_map_alloc_inequality(result);
3455 if (j < 0)
3456 goto error;
3457 isl_seq_clr(result->ineq[j], 1+total);
3458 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3459 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3460 isl_int_sub_ui(result->ineq[j][0], d, 1);
3463 result = isl_basic_map_simplify(result);
3464 return isl_basic_map_finalize(result);
3465 error:
3466 isl_basic_map_free(result);
3467 return NULL;
3470 /* Given a map A -> f(A) and an integer d, construct a map
3471 * A -> floor(f(A)/d).
3473 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3475 int i;
3477 map = isl_map_cow(map);
3478 if (!map)
3479 return NULL;
3481 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3482 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3483 for (i = 0; i < map->n; ++i) {
3484 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3485 if (!map->p[i])
3486 goto error;
3489 return map;
3490 error:
3491 isl_map_free(map);
3492 return NULL;
3495 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3497 int i;
3498 unsigned nparam;
3499 unsigned n_in;
3501 i = isl_basic_map_alloc_equality(bmap);
3502 if (i < 0)
3503 goto error;
3504 nparam = isl_basic_map_n_param(bmap);
3505 n_in = isl_basic_map_n_in(bmap);
3506 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3507 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3508 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3509 return isl_basic_map_finalize(bmap);
3510 error:
3511 isl_basic_map_free(bmap);
3512 return NULL;
3515 /* Add a constraints to "bmap" expressing i_pos < o_pos
3517 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3519 int i;
3520 unsigned nparam;
3521 unsigned n_in;
3523 i = isl_basic_map_alloc_inequality(bmap);
3524 if (i < 0)
3525 goto error;
3526 nparam = isl_basic_map_n_param(bmap);
3527 n_in = isl_basic_map_n_in(bmap);
3528 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3529 isl_int_set_si(bmap->ineq[i][0], -1);
3530 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3531 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3532 return isl_basic_map_finalize(bmap);
3533 error:
3534 isl_basic_map_free(bmap);
3535 return NULL;
3538 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3540 static __isl_give isl_basic_map *var_less_or_equal(
3541 __isl_take isl_basic_map *bmap, unsigned pos)
3543 int i;
3544 unsigned nparam;
3545 unsigned n_in;
3547 i = isl_basic_map_alloc_inequality(bmap);
3548 if (i < 0)
3549 goto error;
3550 nparam = isl_basic_map_n_param(bmap);
3551 n_in = isl_basic_map_n_in(bmap);
3552 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3553 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3554 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3555 return isl_basic_map_finalize(bmap);
3556 error:
3557 isl_basic_map_free(bmap);
3558 return NULL;
3561 /* Add a constraints to "bmap" expressing i_pos > o_pos
3563 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3565 int i;
3566 unsigned nparam;
3567 unsigned n_in;
3569 i = isl_basic_map_alloc_inequality(bmap);
3570 if (i < 0)
3571 goto error;
3572 nparam = isl_basic_map_n_param(bmap);
3573 n_in = isl_basic_map_n_in(bmap);
3574 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3575 isl_int_set_si(bmap->ineq[i][0], -1);
3576 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3577 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3578 return isl_basic_map_finalize(bmap);
3579 error:
3580 isl_basic_map_free(bmap);
3581 return NULL;
3584 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3586 static __isl_give isl_basic_map *var_more_or_equal(
3587 __isl_take isl_basic_map *bmap, unsigned pos)
3589 int i;
3590 unsigned nparam;
3591 unsigned n_in;
3593 i = isl_basic_map_alloc_inequality(bmap);
3594 if (i < 0)
3595 goto error;
3596 nparam = isl_basic_map_n_param(bmap);
3597 n_in = isl_basic_map_n_in(bmap);
3598 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3599 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3600 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3601 return isl_basic_map_finalize(bmap);
3602 error:
3603 isl_basic_map_free(bmap);
3604 return NULL;
3607 __isl_give isl_basic_map *isl_basic_map_equal(
3608 __isl_take isl_space *dim, unsigned n_equal)
3610 int i;
3611 struct isl_basic_map *bmap;
3612 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3613 if (!bmap)
3614 return NULL;
3615 for (i = 0; i < n_equal && bmap; ++i)
3616 bmap = var_equal(bmap, i);
3617 return isl_basic_map_finalize(bmap);
3620 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3622 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3623 unsigned pos)
3625 int i;
3626 struct isl_basic_map *bmap;
3627 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3628 if (!bmap)
3629 return NULL;
3630 for (i = 0; i < pos && bmap; ++i)
3631 bmap = var_equal(bmap, i);
3632 if (bmap)
3633 bmap = var_less(bmap, pos);
3634 return isl_basic_map_finalize(bmap);
3637 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3639 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3640 __isl_take isl_space *dim, unsigned pos)
3642 int i;
3643 isl_basic_map *bmap;
3645 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3646 for (i = 0; i < pos; ++i)
3647 bmap = var_equal(bmap, i);
3648 bmap = var_less_or_equal(bmap, pos);
3649 return isl_basic_map_finalize(bmap);
3652 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3654 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3655 unsigned pos)
3657 int i;
3658 struct isl_basic_map *bmap;
3659 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3660 if (!bmap)
3661 return NULL;
3662 for (i = 0; i < pos && bmap; ++i)
3663 bmap = var_equal(bmap, i);
3664 if (bmap)
3665 bmap = var_more(bmap, pos);
3666 return isl_basic_map_finalize(bmap);
3669 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3671 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3672 __isl_take isl_space *dim, unsigned pos)
3674 int i;
3675 isl_basic_map *bmap;
3677 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3678 for (i = 0; i < pos; ++i)
3679 bmap = var_equal(bmap, i);
3680 bmap = var_more_or_equal(bmap, pos);
3681 return isl_basic_map_finalize(bmap);
3684 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3685 unsigned n, int equal)
3687 struct isl_map *map;
3688 int i;
3690 if (n == 0 && equal)
3691 return isl_map_universe(dims);
3693 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3695 for (i = 0; i + 1 < n; ++i)
3696 map = isl_map_add_basic_map(map,
3697 isl_basic_map_less_at(isl_space_copy(dims), i));
3698 if (n > 0) {
3699 if (equal)
3700 map = isl_map_add_basic_map(map,
3701 isl_basic_map_less_or_equal_at(dims, n - 1));
3702 else
3703 map = isl_map_add_basic_map(map,
3704 isl_basic_map_less_at(dims, n - 1));
3705 } else
3706 isl_space_free(dims);
3708 return map;
3711 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3713 if (!dims)
3714 return NULL;
3715 return map_lex_lte_first(dims, dims->n_out, equal);
3718 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3720 return map_lex_lte_first(dim, n, 0);
3723 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3725 return map_lex_lte_first(dim, n, 1);
3728 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3730 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3733 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3735 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3738 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3739 unsigned n, int equal)
3741 struct isl_map *map;
3742 int i;
3744 if (n == 0 && equal)
3745 return isl_map_universe(dims);
3747 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3749 for (i = 0; i + 1 < n; ++i)
3750 map = isl_map_add_basic_map(map,
3751 isl_basic_map_more_at(isl_space_copy(dims), i));
3752 if (n > 0) {
3753 if (equal)
3754 map = isl_map_add_basic_map(map,
3755 isl_basic_map_more_or_equal_at(dims, n - 1));
3756 else
3757 map = isl_map_add_basic_map(map,
3758 isl_basic_map_more_at(dims, n - 1));
3759 } else
3760 isl_space_free(dims);
3762 return map;
3765 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3767 if (!dims)
3768 return NULL;
3769 return map_lex_gte_first(dims, dims->n_out, equal);
3772 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3774 return map_lex_gte_first(dim, n, 0);
3777 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3779 return map_lex_gte_first(dim, n, 1);
3782 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3784 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3787 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3789 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3792 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3793 __isl_take isl_set *set2)
3795 isl_map *map;
3796 map = isl_map_lex_le(isl_set_get_space(set1));
3797 map = isl_map_intersect_domain(map, set1);
3798 map = isl_map_intersect_range(map, set2);
3799 return map;
3802 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3803 __isl_take isl_set *set2)
3805 isl_map *map;
3806 map = isl_map_lex_lt(isl_set_get_space(set1));
3807 map = isl_map_intersect_domain(map, set1);
3808 map = isl_map_intersect_range(map, set2);
3809 return map;
3812 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3813 __isl_take isl_set *set2)
3815 isl_map *map;
3816 map = isl_map_lex_ge(isl_set_get_space(set1));
3817 map = isl_map_intersect_domain(map, set1);
3818 map = isl_map_intersect_range(map, set2);
3819 return map;
3822 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3823 __isl_take isl_set *set2)
3825 isl_map *map;
3826 map = isl_map_lex_gt(isl_set_get_space(set1));
3827 map = isl_map_intersect_domain(map, set1);
3828 map = isl_map_intersect_range(map, set2);
3829 return map;
3832 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3833 __isl_take isl_map *map2)
3835 isl_map *map;
3836 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3837 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3838 map = isl_map_apply_range(map, isl_map_reverse(map2));
3839 return map;
3842 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3843 __isl_take isl_map *map2)
3845 isl_map *map;
3846 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3847 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3848 map = isl_map_apply_range(map, isl_map_reverse(map2));
3849 return map;
3852 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3853 __isl_take isl_map *map2)
3855 isl_map *map;
3856 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3857 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3858 map = isl_map_apply_range(map, isl_map_reverse(map2));
3859 return map;
3862 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3863 __isl_take isl_map *map2)
3865 isl_map *map;
3866 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3867 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3868 map = isl_map_apply_range(map, isl_map_reverse(map2));
3869 return map;
3872 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3873 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3875 struct isl_basic_map *bmap;
3877 bset = isl_basic_set_cow(bset);
3878 if (!bset || !dim)
3879 goto error;
3881 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3882 isl_space_free(bset->dim);
3883 bmap = (struct isl_basic_map *) bset;
3884 bmap->dim = dim;
3885 return isl_basic_map_finalize(bmap);
3886 error:
3887 isl_basic_set_free(bset);
3888 isl_space_free(dim);
3889 return NULL;
3892 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3894 if (!bmap)
3895 goto error;
3896 if (bmap->dim->n_in == 0)
3897 return (struct isl_basic_set *)bmap;
3898 bmap = isl_basic_map_cow(bmap);
3899 if (!bmap)
3900 goto error;
3901 bmap->dim = isl_space_as_set_space(bmap->dim);
3902 if (!bmap->dim)
3903 goto error;
3904 bmap = isl_basic_map_finalize(bmap);
3905 return (struct isl_basic_set *)bmap;
3906 error:
3907 isl_basic_map_free(bmap);
3908 return NULL;
3911 /* For a div d = floor(f/m), add the constraints
3913 * f - m d >= 0
3914 * -(f-(n-1)) + m d >= 0
3916 * Note that the second constraint is the negation of
3918 * f - m d >= n
3920 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3921 unsigned pos, isl_int *div)
3923 int i, j;
3924 unsigned total = isl_basic_map_total_dim(bmap);
3926 i = isl_basic_map_alloc_inequality(bmap);
3927 if (i < 0)
3928 return -1;
3929 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3930 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3932 j = isl_basic_map_alloc_inequality(bmap);
3933 if (j < 0)
3934 return -1;
3935 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3936 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3937 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3938 return j;
3941 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3942 unsigned pos, isl_int *div)
3944 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3945 pos, div);
3948 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3950 unsigned total = isl_basic_map_total_dim(bmap);
3951 unsigned div_pos = total - bmap->n_div + div;
3953 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3954 bmap->div[div]);
3957 struct isl_basic_set *isl_basic_map_underlying_set(
3958 struct isl_basic_map *bmap)
3960 if (!bmap)
3961 goto error;
3962 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3963 bmap->n_div == 0 &&
3964 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3965 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3966 return (struct isl_basic_set *)bmap;
3967 bmap = isl_basic_map_cow(bmap);
3968 if (!bmap)
3969 goto error;
3970 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3971 if (!bmap->dim)
3972 goto error;
3973 bmap->extra -= bmap->n_div;
3974 bmap->n_div = 0;
3975 bmap = isl_basic_map_finalize(bmap);
3976 return (struct isl_basic_set *)bmap;
3977 error:
3978 return NULL;
3981 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3982 __isl_take isl_basic_set *bset)
3984 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3987 struct isl_basic_map *isl_basic_map_overlying_set(
3988 struct isl_basic_set *bset, struct isl_basic_map *like)
3990 struct isl_basic_map *bmap;
3991 struct isl_ctx *ctx;
3992 unsigned total;
3993 int i;
3995 if (!bset || !like)
3996 goto error;
3997 ctx = bset->ctx;
3998 isl_assert(ctx, bset->n_div == 0, goto error);
3999 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4000 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4001 goto error);
4002 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4003 isl_basic_map_free(like);
4004 return (struct isl_basic_map *)bset;
4006 bset = isl_basic_set_cow(bset);
4007 if (!bset)
4008 goto error;
4009 total = bset->dim->n_out + bset->extra;
4010 bmap = (struct isl_basic_map *)bset;
4011 isl_space_free(bmap->dim);
4012 bmap->dim = isl_space_copy(like->dim);
4013 if (!bmap->dim)
4014 goto error;
4015 bmap->n_div = like->n_div;
4016 bmap->extra += like->n_div;
4017 if (bmap->extra) {
4018 unsigned ltotal;
4019 isl_int **div;
4020 ltotal = total - bmap->extra + like->extra;
4021 if (ltotal > total)
4022 ltotal = total;
4023 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4024 bmap->extra * (1 + 1 + total));
4025 if (isl_blk_is_error(bmap->block2))
4026 goto error;
4027 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4028 if (!div)
4029 goto error;
4030 bmap->div = div;
4031 for (i = 0; i < bmap->extra; ++i)
4032 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4033 for (i = 0; i < like->n_div; ++i) {
4034 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4035 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4037 bmap = isl_basic_map_extend_constraints(bmap,
4038 0, 2 * like->n_div);
4039 for (i = 0; i < like->n_div; ++i) {
4040 if (isl_int_is_zero(bmap->div[i][0]))
4041 continue;
4042 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4043 goto error;
4046 isl_basic_map_free(like);
4047 bmap = isl_basic_map_simplify(bmap);
4048 bmap = isl_basic_map_finalize(bmap);
4049 return bmap;
4050 error:
4051 isl_basic_map_free(like);
4052 isl_basic_set_free(bset);
4053 return NULL;
4056 struct isl_basic_set *isl_basic_set_from_underlying_set(
4057 struct isl_basic_set *bset, struct isl_basic_set *like)
4059 return (struct isl_basic_set *)
4060 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4063 struct isl_set *isl_set_from_underlying_set(
4064 struct isl_set *set, struct isl_basic_set *like)
4066 int i;
4068 if (!set || !like)
4069 goto error;
4070 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4071 goto error);
4072 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4073 isl_basic_set_free(like);
4074 return set;
4076 set = isl_set_cow(set);
4077 if (!set)
4078 goto error;
4079 for (i = 0; i < set->n; ++i) {
4080 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4081 isl_basic_set_copy(like));
4082 if (!set->p[i])
4083 goto error;
4085 isl_space_free(set->dim);
4086 set->dim = isl_space_copy(like->dim);
4087 if (!set->dim)
4088 goto error;
4089 isl_basic_set_free(like);
4090 return set;
4091 error:
4092 isl_basic_set_free(like);
4093 isl_set_free(set);
4094 return NULL;
4097 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4099 int i;
4101 map = isl_map_cow(map);
4102 if (!map)
4103 return NULL;
4104 map->dim = isl_space_cow(map->dim);
4105 if (!map->dim)
4106 goto error;
4108 for (i = 1; i < map->n; ++i)
4109 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4110 goto error);
4111 for (i = 0; i < map->n; ++i) {
4112 map->p[i] = (struct isl_basic_map *)
4113 isl_basic_map_underlying_set(map->p[i]);
4114 if (!map->p[i])
4115 goto error;
4117 if (map->n == 0)
4118 map->dim = isl_space_underlying(map->dim, 0);
4119 else {
4120 isl_space_free(map->dim);
4121 map->dim = isl_space_copy(map->p[0]->dim);
4123 if (!map->dim)
4124 goto error;
4125 return (struct isl_set *)map;
4126 error:
4127 isl_map_free(map);
4128 return NULL;
4131 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4133 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4136 __isl_give isl_basic_map *isl_basic_map_reset_space(
4137 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4139 bmap = isl_basic_map_cow(bmap);
4140 if (!bmap || !dim)
4141 goto error;
4143 isl_space_free(bmap->dim);
4144 bmap->dim = dim;
4146 bmap = isl_basic_map_finalize(bmap);
4148 return bmap;
4149 error:
4150 isl_basic_map_free(bmap);
4151 isl_space_free(dim);
4152 return NULL;
4155 __isl_give isl_basic_set *isl_basic_set_reset_space(
4156 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4158 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4159 dim);
4162 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4163 __isl_take isl_space *dim)
4165 int i;
4167 map = isl_map_cow(map);
4168 if (!map || !dim)
4169 goto error;
4171 for (i = 0; i < map->n; ++i) {
4172 map->p[i] = isl_basic_map_reset_space(map->p[i],
4173 isl_space_copy(dim));
4174 if (!map->p[i])
4175 goto error;
4177 isl_space_free(map->dim);
4178 map->dim = dim;
4180 return map;
4181 error:
4182 isl_map_free(map);
4183 isl_space_free(dim);
4184 return NULL;
4187 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4188 __isl_take isl_space *dim)
4190 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4193 /* Compute the parameter domain of the given basic set.
4195 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4197 isl_space *space;
4198 unsigned n;
4200 if (isl_basic_set_is_params(bset))
4201 return bset;
4203 n = isl_basic_set_dim(bset, isl_dim_set);
4204 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4205 space = isl_basic_set_get_space(bset);
4206 space = isl_space_params(space);
4207 bset = isl_basic_set_reset_space(bset, space);
4208 return bset;
4211 /* Compute the parameter domain of the given set.
4213 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4215 isl_space *space;
4216 unsigned n;
4218 if (isl_set_is_params(set))
4219 return set;
4221 n = isl_set_dim(set, isl_dim_set);
4222 set = isl_set_project_out(set, isl_dim_set, 0, n);
4223 space = isl_set_get_space(set);
4224 space = isl_space_params(space);
4225 set = isl_set_reset_space(set, space);
4226 return set;
4229 /* Construct a zero-dimensional set with the given parameter domain.
4231 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4233 isl_space *space;
4234 space = isl_set_get_space(set);
4235 space = isl_space_set_from_params(space);
4236 set = isl_set_reset_space(set, space);
4237 return set;
4240 /* Compute the parameter domain of the given map.
4242 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4244 isl_space *space;
4245 unsigned n;
4247 n = isl_map_dim(map, isl_dim_in);
4248 map = isl_map_project_out(map, isl_dim_in, 0, n);
4249 n = isl_map_dim(map, isl_dim_out);
4250 map = isl_map_project_out(map, isl_dim_out, 0, n);
4251 space = isl_map_get_space(map);
4252 space = isl_space_params(space);
4253 map = isl_map_reset_space(map, space);
4254 return map;
4257 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4259 isl_space *dim;
4260 struct isl_basic_set *domain;
4261 unsigned n_in;
4262 unsigned n_out;
4264 if (!bmap)
4265 return NULL;
4266 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4268 n_in = isl_basic_map_n_in(bmap);
4269 n_out = isl_basic_map_n_out(bmap);
4270 domain = isl_basic_set_from_basic_map(bmap);
4271 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4273 domain = isl_basic_set_reset_space(domain, dim);
4275 return domain;
4278 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4280 if (!bmap)
4281 return -1;
4282 return isl_space_may_be_set(bmap->dim);
4285 /* Is this basic map actually a set?
4286 * Users should never call this function. Outside of isl,
4287 * the type should indicate whether something is a set or a map.
4289 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4291 if (!bmap)
4292 return -1;
4293 return isl_space_is_set(bmap->dim);
4296 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4298 if (!bmap)
4299 return NULL;
4300 if (isl_basic_map_is_set(bmap))
4301 return bmap;
4302 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4305 __isl_give isl_basic_map *isl_basic_map_domain_map(
4306 __isl_take isl_basic_map *bmap)
4308 int i, k;
4309 isl_space *dim;
4310 isl_basic_map *domain;
4311 int nparam, n_in, n_out;
4312 unsigned total;
4314 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4315 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4316 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4318 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4319 domain = isl_basic_map_universe(dim);
4321 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4322 bmap = isl_basic_map_apply_range(bmap, domain);
4323 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4325 total = isl_basic_map_total_dim(bmap);
4327 for (i = 0; i < n_in; ++i) {
4328 k = isl_basic_map_alloc_equality(bmap);
4329 if (k < 0)
4330 goto error;
4331 isl_seq_clr(bmap->eq[k], 1 + total);
4332 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4333 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4336 bmap = isl_basic_map_gauss(bmap, NULL);
4337 return isl_basic_map_finalize(bmap);
4338 error:
4339 isl_basic_map_free(bmap);
4340 return NULL;
4343 __isl_give isl_basic_map *isl_basic_map_range_map(
4344 __isl_take isl_basic_map *bmap)
4346 int i, k;
4347 isl_space *dim;
4348 isl_basic_map *range;
4349 int nparam, n_in, n_out;
4350 unsigned total;
4352 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4353 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4354 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4356 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4357 range = isl_basic_map_universe(dim);
4359 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4360 bmap = isl_basic_map_apply_range(bmap, range);
4361 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4363 total = isl_basic_map_total_dim(bmap);
4365 for (i = 0; i < n_out; ++i) {
4366 k = isl_basic_map_alloc_equality(bmap);
4367 if (k < 0)
4368 goto error;
4369 isl_seq_clr(bmap->eq[k], 1 + total);
4370 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4371 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4374 bmap = isl_basic_map_gauss(bmap, NULL);
4375 return isl_basic_map_finalize(bmap);
4376 error:
4377 isl_basic_map_free(bmap);
4378 return NULL;
4381 int isl_map_may_be_set(__isl_keep isl_map *map)
4383 if (!map)
4384 return -1;
4385 return isl_space_may_be_set(map->dim);
4388 /* Is this map actually a set?
4389 * Users should never call this function. Outside of isl,
4390 * the type should indicate whether something is a set or a map.
4392 int isl_map_is_set(__isl_keep isl_map *map)
4394 if (!map)
4395 return -1;
4396 return isl_space_is_set(map->dim);
4399 struct isl_set *isl_map_range(struct isl_map *map)
4401 int i;
4402 struct isl_set *set;
4404 if (!map)
4405 goto error;
4406 if (isl_map_is_set(map))
4407 return (isl_set *)map;
4409 map = isl_map_cow(map);
4410 if (!map)
4411 goto error;
4413 set = (struct isl_set *) map;
4414 set->dim = isl_space_range(set->dim);
4415 if (!set->dim)
4416 goto error;
4417 for (i = 0; i < map->n; ++i) {
4418 set->p[i] = isl_basic_map_range(map->p[i]);
4419 if (!set->p[i])
4420 goto error;
4422 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4423 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4424 return set;
4425 error:
4426 isl_map_free(map);
4427 return NULL;
4430 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4432 int i;
4433 isl_space *domain_dim;
4435 map = isl_map_cow(map);
4436 if (!map)
4437 return NULL;
4439 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4440 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4441 map->dim = isl_space_join(map->dim, domain_dim);
4442 if (!map->dim)
4443 goto error;
4444 for (i = 0; i < map->n; ++i) {
4445 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4446 if (!map->p[i])
4447 goto error;
4449 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4450 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4451 return map;
4452 error:
4453 isl_map_free(map);
4454 return NULL;
4457 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4459 int i;
4460 isl_space *range_dim;
4462 map = isl_map_cow(map);
4463 if (!map)
4464 return NULL;
4466 range_dim = isl_space_range(isl_map_get_space(map));
4467 range_dim = isl_space_from_range(range_dim);
4468 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4469 map->dim = isl_space_join(map->dim, range_dim);
4470 if (!map->dim)
4471 goto error;
4472 for (i = 0; i < map->n; ++i) {
4473 map->p[i] = isl_basic_map_range_map(map->p[i]);
4474 if (!map->p[i])
4475 goto error;
4477 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4478 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4479 return map;
4480 error:
4481 isl_map_free(map);
4482 return NULL;
4485 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4486 __isl_take isl_space *dim)
4488 int i;
4489 struct isl_map *map = NULL;
4491 set = isl_set_cow(set);
4492 if (!set || !dim)
4493 goto error;
4494 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4495 map = (struct isl_map *)set;
4496 for (i = 0; i < set->n; ++i) {
4497 map->p[i] = isl_basic_map_from_basic_set(
4498 set->p[i], isl_space_copy(dim));
4499 if (!map->p[i])
4500 goto error;
4502 isl_space_free(map->dim);
4503 map->dim = dim;
4504 return map;
4505 error:
4506 isl_space_free(dim);
4507 isl_set_free(set);
4508 return NULL;
4511 __isl_give isl_basic_map *isl_basic_map_from_domain(
4512 __isl_take isl_basic_set *bset)
4514 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4517 __isl_give isl_basic_map *isl_basic_map_from_range(
4518 __isl_take isl_basic_set *bset)
4520 isl_space *space;
4521 space = isl_basic_set_get_space(bset);
4522 space = isl_space_from_range(space);
4523 bset = isl_basic_set_reset_space(bset, space);
4524 return (isl_basic_map *)bset;
4527 struct isl_map *isl_map_from_range(struct isl_set *set)
4529 isl_space *space;
4530 space = isl_set_get_space(set);
4531 space = isl_space_from_range(space);
4532 set = isl_set_reset_space(set, space);
4533 return (struct isl_map *)set;
4536 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4538 return isl_map_reverse(isl_map_from_range(set));
4541 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4542 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4544 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4547 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4548 __isl_take isl_set *range)
4550 return isl_map_apply_range(isl_map_reverse(domain), range);
4553 struct isl_set *isl_set_from_map(struct isl_map *map)
4555 int i;
4556 struct isl_set *set = NULL;
4558 if (!map)
4559 return NULL;
4560 map = isl_map_cow(map);
4561 if (!map)
4562 return NULL;
4563 map->dim = isl_space_as_set_space(map->dim);
4564 if (!map->dim)
4565 goto error;
4566 set = (struct isl_set *)map;
4567 for (i = 0; i < map->n; ++i) {
4568 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4569 if (!set->p[i])
4570 goto error;
4572 return set;
4573 error:
4574 isl_map_free(map);
4575 return NULL;
4578 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4579 unsigned flags)
4581 struct isl_map *map;
4583 if (!dim)
4584 return NULL;
4585 if (n < 0)
4586 isl_die(dim->ctx, isl_error_internal,
4587 "negative number of basic maps", goto error);
4588 map = isl_alloc(dim->ctx, struct isl_map,
4589 sizeof(struct isl_map) +
4590 (n - 1) * sizeof(struct isl_basic_map *));
4591 if (!map)
4592 goto error;
4594 map->ctx = dim->ctx;
4595 isl_ctx_ref(map->ctx);
4596 map->ref = 1;
4597 map->size = n;
4598 map->n = 0;
4599 map->dim = dim;
4600 map->flags = flags;
4601 return map;
4602 error:
4603 isl_space_free(dim);
4604 return NULL;
4607 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4608 unsigned nparam, unsigned in, unsigned out, int n,
4609 unsigned flags)
4611 struct isl_map *map;
4612 isl_space *dims;
4614 dims = isl_space_alloc(ctx, nparam, in, out);
4615 if (!dims)
4616 return NULL;
4618 map = isl_map_alloc_space(dims, n, flags);
4619 return map;
4622 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4624 struct isl_basic_map *bmap;
4625 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4626 bmap = isl_basic_map_set_to_empty(bmap);
4627 return bmap;
4630 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4632 struct isl_basic_set *bset;
4633 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4634 bset = isl_basic_set_set_to_empty(bset);
4635 return bset;
4638 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4640 struct isl_basic_map *bmap;
4641 if (!model)
4642 return NULL;
4643 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4644 bmap = isl_basic_map_set_to_empty(bmap);
4645 return bmap;
4648 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4650 struct isl_basic_map *bmap;
4651 if (!model)
4652 return NULL;
4653 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4654 bmap = isl_basic_map_set_to_empty(bmap);
4655 return bmap;
4658 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4660 struct isl_basic_set *bset;
4661 if (!model)
4662 return NULL;
4663 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4664 bset = isl_basic_set_set_to_empty(bset);
4665 return bset;
4668 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4670 struct isl_basic_map *bmap;
4671 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4672 bmap = isl_basic_map_finalize(bmap);
4673 return bmap;
4676 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4678 struct isl_basic_set *bset;
4679 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4680 bset = isl_basic_set_finalize(bset);
4681 return bset;
4684 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4686 int i;
4687 unsigned total = isl_space_dim(dim, isl_dim_all);
4688 isl_basic_map *bmap;
4690 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4691 for (i = 0; i < total; ++i) {
4692 int k = isl_basic_map_alloc_inequality(bmap);
4693 if (k < 0)
4694 goto error;
4695 isl_seq_clr(bmap->ineq[k], 1 + total);
4696 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4698 return bmap;
4699 error:
4700 isl_basic_map_free(bmap);
4701 return NULL;
4704 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4706 return isl_basic_map_nat_universe(dim);
4709 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4711 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4714 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4716 return isl_map_nat_universe(dim);
4719 __isl_give isl_basic_map *isl_basic_map_universe_like(
4720 __isl_keep isl_basic_map *model)
4722 if (!model)
4723 return NULL;
4724 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4727 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4729 if (!model)
4730 return NULL;
4731 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4734 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4735 __isl_keep isl_set *model)
4737 if (!model)
4738 return NULL;
4739 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4742 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4744 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4747 struct isl_map *isl_map_empty_like(struct isl_map *model)
4749 if (!model)
4750 return NULL;
4751 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4754 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4756 if (!model)
4757 return NULL;
4758 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4761 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4763 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4766 struct isl_set *isl_set_empty_like(struct isl_set *model)
4768 if (!model)
4769 return NULL;
4770 return isl_set_empty(isl_space_copy(model->dim));
4773 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4775 struct isl_map *map;
4776 if (!dim)
4777 return NULL;
4778 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4779 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4780 return map;
4783 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4785 struct isl_set *set;
4786 if (!dim)
4787 return NULL;
4788 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4789 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4790 return set;
4793 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4795 if (!model)
4796 return NULL;
4797 return isl_set_universe(isl_space_copy(model->dim));
4800 struct isl_map *isl_map_dup(struct isl_map *map)
4802 int i;
4803 struct isl_map *dup;
4805 if (!map)
4806 return NULL;
4807 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4808 for (i = 0; i < map->n; ++i)
4809 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4810 return dup;
4813 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4814 __isl_take isl_basic_map *bmap)
4816 if (!bmap || !map)
4817 goto error;
4818 if (isl_basic_map_plain_is_empty(bmap)) {
4819 isl_basic_map_free(bmap);
4820 return map;
4822 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4823 isl_assert(map->ctx, map->n < map->size, goto error);
4824 map->p[map->n] = bmap;
4825 map->n++;
4826 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4827 return map;
4828 error:
4829 if (map)
4830 isl_map_free(map);
4831 if (bmap)
4832 isl_basic_map_free(bmap);
4833 return NULL;
4836 void isl_map_free(struct isl_map *map)
4838 int i;
4840 if (!map)
4841 return;
4843 if (--map->ref > 0)
4844 return;
4846 isl_ctx_deref(map->ctx);
4847 for (i = 0; i < map->n; ++i)
4848 isl_basic_map_free(map->p[i]);
4849 isl_space_free(map->dim);
4850 free(map);
4853 struct isl_map *isl_map_extend(struct isl_map *base,
4854 unsigned nparam, unsigned n_in, unsigned n_out)
4856 int i;
4858 base = isl_map_cow(base);
4859 if (!base)
4860 return NULL;
4862 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4863 if (!base->dim)
4864 goto error;
4865 for (i = 0; i < base->n; ++i) {
4866 base->p[i] = isl_basic_map_extend_space(base->p[i],
4867 isl_space_copy(base->dim), 0, 0, 0);
4868 if (!base->p[i])
4869 goto error;
4871 return base;
4872 error:
4873 isl_map_free(base);
4874 return NULL;
4877 struct isl_set *isl_set_extend(struct isl_set *base,
4878 unsigned nparam, unsigned dim)
4880 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4881 nparam, 0, dim);
4884 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4885 struct isl_basic_map *bmap, unsigned pos, int value)
4887 int j;
4889 bmap = isl_basic_map_cow(bmap);
4890 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4891 j = isl_basic_map_alloc_equality(bmap);
4892 if (j < 0)
4893 goto error;
4894 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4895 isl_int_set_si(bmap->eq[j][pos], -1);
4896 isl_int_set_si(bmap->eq[j][0], value);
4897 bmap = isl_basic_map_simplify(bmap);
4898 return isl_basic_map_finalize(bmap);
4899 error:
4900 isl_basic_map_free(bmap);
4901 return NULL;
4904 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4905 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4907 int j;
4909 bmap = isl_basic_map_cow(bmap);
4910 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4911 j = isl_basic_map_alloc_equality(bmap);
4912 if (j < 0)
4913 goto error;
4914 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4915 isl_int_set_si(bmap->eq[j][pos], -1);
4916 isl_int_set(bmap->eq[j][0], value);
4917 bmap = isl_basic_map_simplify(bmap);
4918 return isl_basic_map_finalize(bmap);
4919 error:
4920 isl_basic_map_free(bmap);
4921 return NULL;
4924 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4925 enum isl_dim_type type, unsigned pos, int value)
4927 if (!bmap)
4928 return NULL;
4929 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4930 return isl_basic_map_fix_pos_si(bmap,
4931 isl_basic_map_offset(bmap, type) + pos, value);
4932 error:
4933 isl_basic_map_free(bmap);
4934 return NULL;
4937 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4938 enum isl_dim_type type, unsigned pos, isl_int value)
4940 if (!bmap)
4941 return NULL;
4942 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4943 return isl_basic_map_fix_pos(bmap,
4944 isl_basic_map_offset(bmap, type) + pos, value);
4945 error:
4946 isl_basic_map_free(bmap);
4947 return NULL;
4950 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4951 enum isl_dim_type type, unsigned pos, int value)
4953 return (struct isl_basic_set *)
4954 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4955 type, pos, value);
4958 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4959 enum isl_dim_type type, unsigned pos, isl_int value)
4961 return (struct isl_basic_set *)
4962 isl_basic_map_fix((struct isl_basic_map *)bset,
4963 type, pos, value);
4966 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4967 unsigned input, int value)
4969 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4972 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4973 unsigned dim, int value)
4975 return (struct isl_basic_set *)
4976 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4977 isl_dim_set, dim, value);
4980 static int remove_if_empty(__isl_keep isl_map *map, int i)
4982 int empty = isl_basic_map_plain_is_empty(map->p[i]);
4984 if (empty < 0)
4985 return -1;
4986 if (!empty)
4987 return 0;
4989 isl_basic_map_free(map->p[i]);
4990 if (i != map->n - 1) {
4991 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4992 map->p[i] = map->p[map->n - 1];
4994 map->n--;
4996 return 0;
4999 struct isl_map *isl_map_fix_si(struct isl_map *map,
5000 enum isl_dim_type type, unsigned pos, int value)
5002 int i;
5004 map = isl_map_cow(map);
5005 if (!map)
5006 return NULL;
5008 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5009 for (i = map->n - 1; i >= 0; --i) {
5010 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5011 if (remove_if_empty(map, i) < 0)
5012 goto error;
5014 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5015 return map;
5016 error:
5017 isl_map_free(map);
5018 return NULL;
5021 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5022 enum isl_dim_type type, unsigned pos, int value)
5024 return (struct isl_set *)
5025 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5028 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5029 enum isl_dim_type type, unsigned pos, isl_int value)
5031 int i;
5033 map = isl_map_cow(map);
5034 if (!map)
5035 return NULL;
5037 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5038 for (i = 0; i < map->n; ++i) {
5039 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5040 if (!map->p[i])
5041 goto error;
5043 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5044 return map;
5045 error:
5046 isl_map_free(map);
5047 return NULL;
5050 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5051 enum isl_dim_type type, unsigned pos, isl_int value)
5053 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5056 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5057 unsigned input, int value)
5059 return isl_map_fix_si(map, isl_dim_in, input, value);
5062 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5064 return (struct isl_set *)
5065 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5068 static __isl_give isl_basic_map *basic_map_bound_si(
5069 __isl_take isl_basic_map *bmap,
5070 enum isl_dim_type type, unsigned pos, int value, int upper)
5072 int j;
5074 if (!bmap)
5075 return NULL;
5076 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5077 pos += isl_basic_map_offset(bmap, type);
5078 bmap = isl_basic_map_cow(bmap);
5079 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5080 j = isl_basic_map_alloc_inequality(bmap);
5081 if (j < 0)
5082 goto error;
5083 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5084 if (upper) {
5085 isl_int_set_si(bmap->ineq[j][pos], -1);
5086 isl_int_set_si(bmap->ineq[j][0], value);
5087 } else {
5088 isl_int_set_si(bmap->ineq[j][pos], 1);
5089 isl_int_set_si(bmap->ineq[j][0], -value);
5091 bmap = isl_basic_map_simplify(bmap);
5092 return isl_basic_map_finalize(bmap);
5093 error:
5094 isl_basic_map_free(bmap);
5095 return NULL;
5098 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5099 __isl_take isl_basic_map *bmap,
5100 enum isl_dim_type type, unsigned pos, int value)
5102 return basic_map_bound_si(bmap, type, pos, value, 0);
5105 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5106 unsigned dim, isl_int value)
5108 int j;
5110 bset = isl_basic_set_cow(bset);
5111 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5112 j = isl_basic_set_alloc_inequality(bset);
5113 if (j < 0)
5114 goto error;
5115 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5116 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5117 isl_int_neg(bset->ineq[j][0], value);
5118 bset = isl_basic_set_simplify(bset);
5119 return isl_basic_set_finalize(bset);
5120 error:
5121 isl_basic_set_free(bset);
5122 return NULL;
5125 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5126 enum isl_dim_type type, unsigned pos, int value, int upper)
5128 int i;
5130 map = isl_map_cow(map);
5131 if (!map)
5132 return NULL;
5134 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5135 for (i = 0; i < map->n; ++i) {
5136 map->p[i] = basic_map_bound_si(map->p[i],
5137 type, pos, value, upper);
5138 if (!map->p[i])
5139 goto error;
5141 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5142 return map;
5143 error:
5144 isl_map_free(map);
5145 return NULL;
5148 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5149 enum isl_dim_type type, unsigned pos, int value)
5151 return map_bound_si(map, type, pos, value, 0);
5154 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5155 enum isl_dim_type type, unsigned pos, int value)
5157 return map_bound_si(map, type, pos, value, 1);
5160 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5161 enum isl_dim_type type, unsigned pos, int value)
5163 return (struct isl_set *)
5164 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5167 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5168 enum isl_dim_type type, unsigned pos, int value)
5170 return isl_map_upper_bound_si(set, type, pos, value);
5173 /* Bound the given variable of "bmap" from below (or above is "upper"
5174 * is set) to "value".
5176 static __isl_give isl_basic_map *basic_map_bound(
5177 __isl_take isl_basic_map *bmap,
5178 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5180 int j;
5182 if (!bmap)
5183 return NULL;
5184 if (pos >= isl_basic_map_dim(bmap, type))
5185 isl_die(bmap->ctx, isl_error_invalid,
5186 "index out of bounds", goto error);
5187 pos += isl_basic_map_offset(bmap, type);
5188 bmap = isl_basic_map_cow(bmap);
5189 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5190 j = isl_basic_map_alloc_inequality(bmap);
5191 if (j < 0)
5192 goto error;
5193 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5194 if (upper) {
5195 isl_int_set_si(bmap->ineq[j][pos], -1);
5196 isl_int_set(bmap->ineq[j][0], value);
5197 } else {
5198 isl_int_set_si(bmap->ineq[j][pos], 1);
5199 isl_int_neg(bmap->ineq[j][0], value);
5201 bmap = isl_basic_map_simplify(bmap);
5202 return isl_basic_map_finalize(bmap);
5203 error:
5204 isl_basic_map_free(bmap);
5205 return NULL;
5208 /* Bound the given variable of "map" from below (or above is "upper"
5209 * is set) to "value".
5211 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5212 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5214 int i;
5216 map = isl_map_cow(map);
5217 if (!map)
5218 return NULL;
5220 if (pos >= isl_map_dim(map, type))
5221 isl_die(map->ctx, isl_error_invalid,
5222 "index out of bounds", goto error);
5223 for (i = map->n - 1; i >= 0; --i) {
5224 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5225 if (remove_if_empty(map, i) < 0)
5226 goto error;
5228 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5229 return map;
5230 error:
5231 isl_map_free(map);
5232 return NULL;
5235 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5236 enum isl_dim_type type, unsigned pos, isl_int value)
5238 return map_bound(map, type, pos, value, 0);
5241 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5242 enum isl_dim_type type, unsigned pos, isl_int value)
5244 return map_bound(map, type, pos, value, 1);
5247 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5248 enum isl_dim_type type, unsigned pos, isl_int value)
5250 return isl_map_lower_bound(set, type, pos, value);
5253 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5254 enum isl_dim_type type, unsigned pos, isl_int value)
5256 return isl_map_upper_bound(set, type, pos, value);
5259 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5260 isl_int value)
5262 int i;
5264 set = isl_set_cow(set);
5265 if (!set)
5266 return NULL;
5268 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5269 for (i = 0; i < set->n; ++i) {
5270 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5271 if (!set->p[i])
5272 goto error;
5274 return set;
5275 error:
5276 isl_set_free(set);
5277 return NULL;
5280 struct isl_map *isl_map_reverse(struct isl_map *map)
5282 int i;
5284 map = isl_map_cow(map);
5285 if (!map)
5286 return NULL;
5288 map->dim = isl_space_reverse(map->dim);
5289 if (!map->dim)
5290 goto error;
5291 for (i = 0; i < map->n; ++i) {
5292 map->p[i] = isl_basic_map_reverse(map->p[i]);
5293 if (!map->p[i])
5294 goto error;
5296 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5297 return map;
5298 error:
5299 isl_map_free(map);
5300 return NULL;
5303 static struct isl_map *isl_basic_map_partial_lexopt(
5304 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5305 struct isl_set **empty, int max)
5307 if (!bmap)
5308 goto error;
5309 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5310 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5311 else
5312 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5313 error:
5314 isl_basic_map_free(bmap);
5315 isl_basic_set_free(dom);
5316 if (empty)
5317 *empty = NULL;
5318 return NULL;
5321 struct isl_map *isl_basic_map_partial_lexmax(
5322 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5323 struct isl_set **empty)
5325 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5328 struct isl_map *isl_basic_map_partial_lexmin(
5329 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5330 struct isl_set **empty)
5332 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5335 struct isl_set *isl_basic_set_partial_lexmin(
5336 struct isl_basic_set *bset, struct isl_basic_set *dom,
5337 struct isl_set **empty)
5339 return (struct isl_set *)
5340 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5341 dom, empty);
5344 struct isl_set *isl_basic_set_partial_lexmax(
5345 struct isl_basic_set *bset, struct isl_basic_set *dom,
5346 struct isl_set **empty)
5348 return (struct isl_set *)
5349 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5350 dom, empty);
5353 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5354 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5355 __isl_give isl_set **empty)
5357 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5360 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5361 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5362 __isl_give isl_set **empty)
5364 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5367 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5368 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5369 __isl_give isl_set **empty)
5371 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5374 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5375 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5376 __isl_give isl_set **empty)
5378 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5381 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5382 __isl_take isl_basic_map *bmap, int max)
5384 isl_basic_set *dom = NULL;
5385 isl_space *dom_space;
5387 if (!bmap)
5388 goto error;
5389 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5390 dom = isl_basic_set_universe(dom_space);
5391 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5392 error:
5393 isl_basic_map_free(bmap);
5394 return NULL;
5397 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5398 __isl_take isl_basic_map *bmap)
5400 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5403 /* Given a basic map "bmap", compute the lexicographically minimal
5404 * (or maximal) image element for each domain element in dom.
5405 * Set *empty to those elements in dom that do not have an image element.
5407 * We first make sure the basic sets in dom are disjoint and then
5408 * simply collect the results over each of the basic sets separately.
5409 * We could probably improve the efficiency a bit by moving the union
5410 * domain down into the parametric integer programming.
5412 static __isl_give isl_map *basic_map_partial_lexopt(
5413 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5414 __isl_give isl_set **empty, int max)
5416 int i;
5417 struct isl_map *res;
5419 dom = isl_set_make_disjoint(dom);
5420 if (!dom)
5421 goto error;
5423 if (isl_set_plain_is_empty(dom)) {
5424 res = isl_map_empty_like_basic_map(bmap);
5425 *empty = isl_set_empty_like(dom);
5426 isl_set_free(dom);
5427 isl_basic_map_free(bmap);
5428 return res;
5431 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5432 isl_basic_set_copy(dom->p[0]), empty, max);
5434 for (i = 1; i < dom->n; ++i) {
5435 struct isl_map *res_i;
5436 struct isl_set *empty_i;
5438 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5439 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5441 res = isl_map_union_disjoint(res, res_i);
5442 *empty = isl_set_union_disjoint(*empty, empty_i);
5445 isl_set_free(dom);
5446 isl_basic_map_free(bmap);
5447 return res;
5448 error:
5449 *empty = NULL;
5450 isl_set_free(dom);
5451 isl_basic_map_free(bmap);
5452 return NULL;
5455 /* Given a map "map", compute the lexicographically minimal
5456 * (or maximal) image element for each domain element in dom.
5457 * Set *empty to those elements in dom that do not have an image element.
5459 * We first compute the lexicographically minimal or maximal element
5460 * in the first basic map. This results in a partial solution "res"
5461 * and a subset "todo" of dom that still need to be handled.
5462 * We then consider each of the remaining maps in "map" and successively
5463 * improve both "res" and "todo".
5465 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5466 * Assume we are computing the lexicographical maximum.
5467 * We first compute the lexicographically maximal element in basic map i.
5468 * This results in a partial solution res_i and a subset todo_i.
5469 * Then we combine these results with those obtain for the first k basic maps
5470 * to obtain a result that is valid for the first k+1 basic maps.
5471 * In particular, the set where there is no solution is the set where
5472 * there is no solution for the first k basic maps and also no solution
5473 * for the ith basic map, i.e.,
5475 * todo^i = todo^k * todo_i
5477 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5478 * solutions, arbitrarily breaking ties in favor of res^k.
5479 * That is, when res^k(a) >= res_i(a), we pick res^k and
5480 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5481 * the lexicographic order.)
5482 * In practice, we compute
5484 * res^k * (res_i . "<=")
5486 * and
5488 * res_i * (res^k . "<")
5490 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5491 * where only one of res^k and res_i provides a solution and we simply pick
5492 * that one, i.e.,
5494 * res^k * todo_i
5495 * and
5496 * res_i * todo^k
5498 * Note that we only compute these intersections when dom(res^k) intersects
5499 * dom(res_i). Otherwise, the only effect of these intersections is to
5500 * potentially break up res^k and res_i into smaller pieces.
5501 * We want to avoid such splintering as much as possible.
5502 * In fact, an earlier implementation of this function would look for
5503 * better results in the domain of res^k and for extra results in todo^k,
5504 * but this would always result in a splintering according to todo^k,
5505 * even when the domain of basic map i is disjoint from the domains of
5506 * the previous basic maps.
5508 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5509 __isl_take isl_map *map, __isl_take isl_set *dom,
5510 __isl_give isl_set **empty, int max)
5512 int i;
5513 struct isl_map *res;
5514 struct isl_set *todo;
5516 if (!map || !dom)
5517 goto error;
5519 if (isl_map_plain_is_empty(map)) {
5520 if (empty)
5521 *empty = dom;
5522 else
5523 isl_set_free(dom);
5524 return map;
5527 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5528 isl_set_copy(dom), &todo, max);
5530 for (i = 1; i < map->n; ++i) {
5531 isl_map *lt, *le;
5532 isl_map *res_i;
5533 isl_set *todo_i;
5534 isl_space *dim = isl_space_range(isl_map_get_space(res));
5536 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5537 isl_set_copy(dom), &todo_i, max);
5539 if (max) {
5540 lt = isl_map_lex_lt(isl_space_copy(dim));
5541 le = isl_map_lex_le(dim);
5542 } else {
5543 lt = isl_map_lex_gt(isl_space_copy(dim));
5544 le = isl_map_lex_ge(dim);
5546 lt = isl_map_apply_range(isl_map_copy(res), lt);
5547 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5548 le = isl_map_apply_range(isl_map_copy(res_i), le);
5549 le = isl_map_intersect(le, isl_map_copy(res));
5551 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5552 res = isl_map_intersect_domain(res,
5553 isl_set_copy(todo_i));
5554 res_i = isl_map_intersect_domain(res_i,
5555 isl_set_copy(todo));
5558 res = isl_map_union_disjoint(res, res_i);
5559 res = isl_map_union_disjoint(res, lt);
5560 res = isl_map_union_disjoint(res, le);
5562 todo = isl_set_intersect(todo, todo_i);
5565 isl_set_free(dom);
5566 isl_map_free(map);
5568 if (empty)
5569 *empty = todo;
5570 else
5571 isl_set_free(todo);
5573 return res;
5574 error:
5575 if (empty)
5576 *empty = NULL;
5577 isl_set_free(dom);
5578 isl_map_free(map);
5579 return NULL;
5582 /* Given a map "map", compute the lexicographically minimal
5583 * (or maximal) image element for each domain element in dom.
5584 * Set *empty to those elements in dom that do not have an image element.
5586 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5588 static __isl_give isl_map *isl_map_partial_lexopt(
5589 __isl_take isl_map *map, __isl_take isl_set *dom,
5590 __isl_give isl_set **empty, int max)
5592 if (!map || !dom)
5593 goto error;
5594 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5595 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5596 if (!isl_space_has_named_params(map->dim) ||
5597 !isl_space_has_named_params(dom->dim))
5598 isl_die(map->ctx, isl_error_invalid,
5599 "unaligned unnamed parameters", goto error);
5600 map = isl_map_align_params(map, isl_map_get_space(dom));
5601 dom = isl_map_align_params(dom, isl_map_get_space(map));
5602 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5603 error:
5604 if (empty)
5605 *empty = NULL;
5606 isl_set_free(dom);
5607 isl_map_free(map);
5608 return NULL;
5611 __isl_give isl_map *isl_map_partial_lexmax(
5612 __isl_take isl_map *map, __isl_take isl_set *dom,
5613 __isl_give isl_set **empty)
5615 return isl_map_partial_lexopt(map, dom, empty, 1);
5618 __isl_give isl_map *isl_map_partial_lexmin(
5619 __isl_take isl_map *map, __isl_take isl_set *dom,
5620 __isl_give isl_set **empty)
5622 return isl_map_partial_lexopt(map, dom, empty, 0);
5625 __isl_give isl_set *isl_set_partial_lexmin(
5626 __isl_take isl_set *set, __isl_take isl_set *dom,
5627 __isl_give isl_set **empty)
5629 return (struct isl_set *)
5630 isl_map_partial_lexmin((struct isl_map *)set,
5631 dom, empty);
5634 __isl_give isl_set *isl_set_partial_lexmax(
5635 __isl_take isl_set *set, __isl_take isl_set *dom,
5636 __isl_give isl_set **empty)
5638 return (struct isl_set *)
5639 isl_map_partial_lexmax((struct isl_map *)set,
5640 dom, empty);
5643 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5645 struct isl_basic_set *dom = NULL;
5646 isl_space *dom_dim;
5648 if (!bmap)
5649 goto error;
5650 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5651 dom = isl_basic_set_universe(dom_dim);
5652 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5653 error:
5654 isl_basic_map_free(bmap);
5655 return NULL;
5658 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5660 return isl_basic_map_lexopt(bmap, 0);
5663 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5665 return isl_basic_map_lexopt(bmap, 1);
5668 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5670 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5673 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5675 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5678 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5680 struct isl_set *dom = NULL;
5681 isl_space *dom_dim;
5683 if (!map)
5684 goto error;
5685 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5686 dom = isl_set_universe(dom_dim);
5687 return isl_map_partial_lexopt(map, dom, NULL, max);
5688 error:
5689 isl_map_free(map);
5690 return NULL;
5693 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5695 return isl_map_lexopt(map, 0);
5698 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5700 return isl_map_lexopt(map, 1);
5703 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5705 return (isl_set *)isl_map_lexmin((isl_map *)set);
5708 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5710 return (isl_set *)isl_map_lexmax((isl_map *)set);
5713 /* Extract the first and only affine expression from list
5714 * and then add it to *pwaff with the given dom.
5715 * This domain is known to be disjoint from other domains
5716 * because of the way isl_basic_map_foreach_lexmax works.
5718 static int update_dim_opt(__isl_take isl_basic_set *dom,
5719 __isl_take isl_aff_list *list, void *user)
5721 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5722 isl_aff *aff;
5723 isl_pw_aff **pwaff = user;
5724 isl_pw_aff *pwaff_i;
5726 if (isl_aff_list_n_aff(list) != 1)
5727 isl_die(ctx, isl_error_internal,
5728 "expecting single element list", goto error);
5730 aff = isl_aff_list_get_aff(list, 0);
5731 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5733 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5735 isl_aff_list_free(list);
5737 return 0;
5738 error:
5739 isl_basic_set_free(dom);
5740 isl_aff_list_free(list);
5741 return -1;
5744 /* Given a basic map with one output dimension, compute the minimum or
5745 * maximum of that dimension as an isl_pw_aff.
5747 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5748 * call update_dim_opt on each leaf of the result.
5750 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5751 int max)
5753 isl_space *dim = isl_basic_map_get_space(bmap);
5754 isl_pw_aff *pwaff;
5755 int r;
5757 dim = isl_space_from_domain(isl_space_domain(dim));
5758 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5759 pwaff = isl_pw_aff_empty(dim);
5761 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5762 if (r < 0)
5763 return isl_pw_aff_free(pwaff);
5765 return pwaff;
5768 /* Compute the minimum or maximum of the given output dimension
5769 * as a function of the parameters and the input dimensions,
5770 * but independently of the other output dimensions.
5772 * We first project out the other output dimension and then compute
5773 * the "lexicographic" maximum in each basic map, combining the results
5774 * using isl_pw_aff_union_max.
5776 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5777 int max)
5779 int i;
5780 isl_pw_aff *pwaff;
5781 unsigned n_out;
5783 n_out = isl_map_dim(map, isl_dim_out);
5784 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5785 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5786 if (!map)
5787 return NULL;
5789 if (map->n == 0) {
5790 isl_space *dim = isl_map_get_space(map);
5791 dim = isl_space_domain(isl_space_from_range(dim));
5792 isl_map_free(map);
5793 return isl_pw_aff_empty(dim);
5796 pwaff = basic_map_dim_opt(map->p[0], max);
5797 for (i = 1; i < map->n; ++i) {
5798 isl_pw_aff *pwaff_i;
5800 pwaff_i = basic_map_dim_opt(map->p[i], max);
5801 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5804 isl_map_free(map);
5806 return pwaff;
5809 /* Compute the maximum of the given output dimension as a function of the
5810 * parameters and input dimensions, but independently of
5811 * the other output dimensions.
5813 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5815 return map_dim_opt(map, pos, 1);
5818 /* Compute the minimum or maximum of the given set dimension
5819 * as a function of the parameters,
5820 * but independently of the other set dimensions.
5822 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5823 int max)
5825 return map_dim_opt(set, pos, max);
5828 /* Compute the maximum of the given set dimension as a function of the
5829 * parameters, but independently of the other set dimensions.
5831 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5833 return set_dim_opt(set, pos, 1);
5836 /* Compute the minimum of the given set dimension as a function of the
5837 * parameters, but independently of the other set dimensions.
5839 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5841 return set_dim_opt(set, pos, 0);
5844 /* Apply a preimage specified by "mat" on the parameters of "bset".
5845 * bset is assumed to have only parameters and divs.
5847 static struct isl_basic_set *basic_set_parameter_preimage(
5848 struct isl_basic_set *bset, struct isl_mat *mat)
5850 unsigned nparam;
5852 if (!bset || !mat)
5853 goto error;
5855 bset->dim = isl_space_cow(bset->dim);
5856 if (!bset->dim)
5857 goto error;
5859 nparam = isl_basic_set_dim(bset, isl_dim_param);
5861 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5863 bset->dim->nparam = 0;
5864 bset->dim->n_out = nparam;
5865 bset = isl_basic_set_preimage(bset, mat);
5866 if (bset) {
5867 bset->dim->nparam = bset->dim->n_out;
5868 bset->dim->n_out = 0;
5870 return bset;
5871 error:
5872 isl_mat_free(mat);
5873 isl_basic_set_free(bset);
5874 return NULL;
5877 /* Apply a preimage specified by "mat" on the parameters of "set".
5878 * set is assumed to have only parameters and divs.
5880 static struct isl_set *set_parameter_preimage(
5881 struct isl_set *set, struct isl_mat *mat)
5883 isl_space *dim = NULL;
5884 unsigned nparam;
5886 if (!set || !mat)
5887 goto error;
5889 dim = isl_space_copy(set->dim);
5890 dim = isl_space_cow(dim);
5891 if (!dim)
5892 goto error;
5894 nparam = isl_set_dim(set, isl_dim_param);
5896 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5898 dim->nparam = 0;
5899 dim->n_out = nparam;
5900 isl_set_reset_space(set, dim);
5901 set = isl_set_preimage(set, mat);
5902 if (!set)
5903 goto error2;
5904 dim = isl_space_copy(set->dim);
5905 dim = isl_space_cow(dim);
5906 if (!dim)
5907 goto error2;
5908 dim->nparam = dim->n_out;
5909 dim->n_out = 0;
5910 isl_set_reset_space(set, dim);
5911 return set;
5912 error:
5913 isl_space_free(dim);
5914 isl_mat_free(mat);
5915 error2:
5916 isl_set_free(set);
5917 return NULL;
5920 /* Intersect the basic set "bset" with the affine space specified by the
5921 * equalities in "eq".
5923 static struct isl_basic_set *basic_set_append_equalities(
5924 struct isl_basic_set *bset, struct isl_mat *eq)
5926 int i, k;
5927 unsigned len;
5929 if (!bset || !eq)
5930 goto error;
5932 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5933 eq->n_row, 0);
5934 if (!bset)
5935 goto error;
5937 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5938 for (i = 0; i < eq->n_row; ++i) {
5939 k = isl_basic_set_alloc_equality(bset);
5940 if (k < 0)
5941 goto error;
5942 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5943 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5945 isl_mat_free(eq);
5947 bset = isl_basic_set_gauss(bset, NULL);
5948 bset = isl_basic_set_finalize(bset);
5950 return bset;
5951 error:
5952 isl_mat_free(eq);
5953 isl_basic_set_free(bset);
5954 return NULL;
5957 /* Intersect the set "set" with the affine space specified by the
5958 * equalities in "eq".
5960 static struct isl_set *set_append_equalities(struct isl_set *set,
5961 struct isl_mat *eq)
5963 int i;
5965 if (!set || !eq)
5966 goto error;
5968 for (i = 0; i < set->n; ++i) {
5969 set->p[i] = basic_set_append_equalities(set->p[i],
5970 isl_mat_copy(eq));
5971 if (!set->p[i])
5972 goto error;
5974 isl_mat_free(eq);
5975 return set;
5976 error:
5977 isl_mat_free(eq);
5978 isl_set_free(set);
5979 return NULL;
5982 /* Project the given basic set onto its parameter domain, possibly introducing
5983 * new, explicit, existential variables in the constraints.
5984 * The input has parameters and (possibly implicit) existential variables.
5985 * The output has the same parameters, but only
5986 * explicit existentially quantified variables.
5988 * The actual projection is performed by pip, but pip doesn't seem
5989 * to like equalities very much, so we first remove the equalities
5990 * among the parameters by performing a variable compression on
5991 * the parameters. Afterward, an inverse transformation is performed
5992 * and the equalities among the parameters are inserted back in.
5994 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5996 int i, j;
5997 struct isl_mat *eq;
5998 struct isl_mat *T, *T2;
5999 struct isl_set *set;
6000 unsigned nparam, n_div;
6002 bset = isl_basic_set_cow(bset);
6003 if (!bset)
6004 return NULL;
6006 if (bset->n_eq == 0)
6007 return isl_basic_set_lexmin(bset);
6009 isl_basic_set_gauss(bset, NULL);
6011 nparam = isl_basic_set_dim(bset, isl_dim_param);
6012 n_div = isl_basic_set_dim(bset, isl_dim_div);
6014 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6015 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6016 ++i;
6018 if (i == bset->n_eq)
6019 return isl_basic_set_lexmin(bset);
6021 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6022 0, 1 + nparam);
6023 eq = isl_mat_cow(eq);
6024 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6025 if (T && T->n_col == 0) {
6026 isl_mat_free(T);
6027 isl_mat_free(T2);
6028 isl_mat_free(eq);
6029 bset = isl_basic_set_set_to_empty(bset);
6030 return isl_set_from_basic_set(bset);
6032 bset = basic_set_parameter_preimage(bset, T);
6034 set = isl_basic_set_lexmin(bset);
6035 set = set_parameter_preimage(set, T2);
6036 set = set_append_equalities(set, eq);
6037 return set;
6040 /* Compute an explicit representation for all the existentially
6041 * quantified variables.
6042 * The input and output dimensions are first turned into parameters.
6043 * compute_divs then returns a map with the same parameters and
6044 * no input or output dimensions and the dimension specification
6045 * is reset to that of the input.
6047 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6049 struct isl_basic_set *bset;
6050 struct isl_set *set;
6051 struct isl_map *map;
6052 isl_space *dim, *orig_dim = NULL;
6053 unsigned nparam;
6054 unsigned n_in;
6055 unsigned n_out;
6057 bmap = isl_basic_map_cow(bmap);
6058 if (!bmap)
6059 return NULL;
6061 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6062 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6063 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6064 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6065 if (!dim)
6066 goto error;
6068 orig_dim = bmap->dim;
6069 bmap->dim = dim;
6070 bset = (struct isl_basic_set *)bmap;
6072 set = parameter_compute_divs(bset);
6073 map = (struct isl_map *)set;
6074 map = isl_map_reset_space(map, orig_dim);
6076 return map;
6077 error:
6078 isl_basic_map_free(bmap);
6079 return NULL;
6082 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6084 int i;
6085 unsigned off;
6087 if (!bmap)
6088 return -1;
6090 off = isl_space_dim(bmap->dim, isl_dim_all);
6091 for (i = 0; i < bmap->n_div; ++i) {
6092 if (isl_int_is_zero(bmap->div[i][0]))
6093 return 0;
6094 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6095 return -1);
6097 return 1;
6100 static int map_divs_known(__isl_keep isl_map *map)
6102 int i;
6104 if (!map)
6105 return -1;
6107 for (i = 0; i < map->n; ++i) {
6108 int known = isl_basic_map_divs_known(map->p[i]);
6109 if (known <= 0)
6110 return known;
6113 return 1;
6116 /* If bmap contains any unknown divs, then compute explicit
6117 * expressions for them. However, this computation may be
6118 * quite expensive, so first try to remove divs that aren't
6119 * strictly needed.
6121 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6123 int known;
6124 struct isl_map *map;
6126 known = isl_basic_map_divs_known(bmap);
6127 if (known < 0)
6128 goto error;
6129 if (known)
6130 return isl_map_from_basic_map(bmap);
6132 bmap = isl_basic_map_drop_redundant_divs(bmap);
6134 known = isl_basic_map_divs_known(bmap);
6135 if (known < 0)
6136 goto error;
6137 if (known)
6138 return isl_map_from_basic_map(bmap);
6140 map = compute_divs(bmap);
6141 return map;
6142 error:
6143 isl_basic_map_free(bmap);
6144 return NULL;
6147 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6149 int i;
6150 int known;
6151 struct isl_map *res;
6153 if (!map)
6154 return NULL;
6155 if (map->n == 0)
6156 return map;
6158 known = map_divs_known(map);
6159 if (known < 0) {
6160 isl_map_free(map);
6161 return NULL;
6163 if (known)
6164 return map;
6166 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6167 for (i = 1 ; i < map->n; ++i) {
6168 struct isl_map *r2;
6169 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6170 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6171 res = isl_map_union_disjoint(res, r2);
6172 else
6173 res = isl_map_union(res, r2);
6175 isl_map_free(map);
6177 return res;
6180 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6182 return (struct isl_set *)
6183 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6186 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6188 return (struct isl_set *)
6189 isl_map_compute_divs((struct isl_map *)set);
6192 struct isl_set *isl_map_domain(struct isl_map *map)
6194 int i;
6195 struct isl_set *set;
6197 if (!map)
6198 goto error;
6200 map = isl_map_cow(map);
6201 if (!map)
6202 return NULL;
6204 set = (struct isl_set *)map;
6205 set->dim = isl_space_domain(set->dim);
6206 if (!set->dim)
6207 goto error;
6208 for (i = 0; i < map->n; ++i) {
6209 set->p[i] = isl_basic_map_domain(map->p[i]);
6210 if (!set->p[i])
6211 goto error;
6213 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6214 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6215 return set;
6216 error:
6217 isl_map_free(map);
6218 return NULL;
6221 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6222 __isl_take isl_map *map2)
6224 int i;
6225 unsigned flags = 0;
6226 struct isl_map *map = NULL;
6228 if (!map1 || !map2)
6229 goto error;
6231 if (map1->n == 0) {
6232 isl_map_free(map1);
6233 return map2;
6235 if (map2->n == 0) {
6236 isl_map_free(map2);
6237 return map1;
6240 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6242 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6243 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6244 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6246 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6247 map1->n + map2->n, flags);
6248 if (!map)
6249 goto error;
6250 for (i = 0; i < map1->n; ++i) {
6251 map = isl_map_add_basic_map(map,
6252 isl_basic_map_copy(map1->p[i]));
6253 if (!map)
6254 goto error;
6256 for (i = 0; i < map2->n; ++i) {
6257 map = isl_map_add_basic_map(map,
6258 isl_basic_map_copy(map2->p[i]));
6259 if (!map)
6260 goto error;
6262 isl_map_free(map1);
6263 isl_map_free(map2);
6264 return map;
6265 error:
6266 isl_map_free(map);
6267 isl_map_free(map1);
6268 isl_map_free(map2);
6269 return NULL;
6272 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6273 __isl_take isl_map *map2)
6275 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6278 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6280 map1 = isl_map_union_disjoint(map1, map2);
6281 if (!map1)
6282 return NULL;
6283 if (map1->n > 1)
6284 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6285 return map1;
6288 struct isl_set *isl_set_union_disjoint(
6289 struct isl_set *set1, struct isl_set *set2)
6291 return (struct isl_set *)
6292 isl_map_union_disjoint(
6293 (struct isl_map *)set1, (struct isl_map *)set2);
6296 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6298 return (struct isl_set *)
6299 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6302 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6303 __isl_take isl_set *set)
6305 unsigned flags = 0;
6306 struct isl_map *result;
6307 int i, j;
6309 if (!map || !set)
6310 goto error;
6312 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6313 isl_die(set->ctx, isl_error_invalid,
6314 "parameters don't match", goto error);
6316 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6317 !isl_map_compatible_range(map, set))
6318 isl_die(set->ctx, isl_error_invalid,
6319 "incompatible spaces", goto error);
6321 if (isl_set_plain_is_universe(set)) {
6322 isl_set_free(set);
6323 return map;
6326 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6327 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6328 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6330 result = isl_map_alloc_space(isl_space_copy(map->dim),
6331 map->n * set->n, flags);
6332 if (!result)
6333 goto error;
6334 for (i = 0; i < map->n; ++i)
6335 for (j = 0; j < set->n; ++j) {
6336 result = isl_map_add_basic_map(result,
6337 isl_basic_map_intersect_range(
6338 isl_basic_map_copy(map->p[i]),
6339 isl_basic_set_copy(set->p[j])));
6340 if (!result)
6341 goto error;
6343 isl_map_free(map);
6344 isl_set_free(set);
6345 return result;
6346 error:
6347 isl_map_free(map);
6348 isl_set_free(set);
6349 return NULL;
6352 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6353 __isl_take isl_set *set)
6355 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6358 struct isl_map *isl_map_intersect_domain(
6359 struct isl_map *map, struct isl_set *set)
6361 return isl_map_reverse(
6362 isl_map_intersect_range(isl_map_reverse(map), set));
6365 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6366 __isl_take isl_map *map2)
6368 if (!map1 || !map2)
6369 goto error;
6370 map1 = isl_map_reverse(map1);
6371 map1 = isl_map_apply_range(map1, map2);
6372 return isl_map_reverse(map1);
6373 error:
6374 isl_map_free(map1);
6375 isl_map_free(map2);
6376 return NULL;
6379 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6380 __isl_take isl_map *map2)
6382 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6385 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6386 __isl_take isl_map *map2)
6388 isl_space *dim_result;
6389 struct isl_map *result;
6390 int i, j;
6392 if (!map1 || !map2)
6393 goto error;
6395 dim_result = isl_space_join(isl_space_copy(map1->dim),
6396 isl_space_copy(map2->dim));
6398 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6399 if (!result)
6400 goto error;
6401 for (i = 0; i < map1->n; ++i)
6402 for (j = 0; j < map2->n; ++j) {
6403 result = isl_map_add_basic_map(result,
6404 isl_basic_map_apply_range(
6405 isl_basic_map_copy(map1->p[i]),
6406 isl_basic_map_copy(map2->p[j])));
6407 if (!result)
6408 goto error;
6410 isl_map_free(map1);
6411 isl_map_free(map2);
6412 if (result && result->n <= 1)
6413 ISL_F_SET(result, ISL_MAP_DISJOINT);
6414 return result;
6415 error:
6416 isl_map_free(map1);
6417 isl_map_free(map2);
6418 return NULL;
6421 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6422 __isl_take isl_map *map2)
6424 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6428 * returns range - domain
6430 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6432 isl_space *dims, *target_dim;
6433 struct isl_basic_set *bset;
6434 unsigned dim;
6435 unsigned nparam;
6436 int i;
6438 if (!bmap)
6439 goto error;
6440 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6441 bmap->dim, isl_dim_out),
6442 goto error);
6443 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6444 dim = isl_basic_map_n_in(bmap);
6445 nparam = isl_basic_map_n_param(bmap);
6446 bset = isl_basic_set_from_basic_map(bmap);
6447 bset = isl_basic_set_cow(bset);
6448 dims = isl_basic_set_get_space(bset);
6449 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6450 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6451 bset = isl_basic_set_swap_vars(bset, 2*dim);
6452 for (i = 0; i < dim; ++i) {
6453 int j = isl_basic_map_alloc_equality(
6454 (struct isl_basic_map *)bset);
6455 if (j < 0)
6456 goto error;
6457 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6458 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6459 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6460 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6462 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6463 bset = isl_basic_set_reset_space(bset, target_dim);
6464 return bset;
6465 error:
6466 isl_basic_map_free(bmap);
6467 return NULL;
6471 * returns range - domain
6473 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6475 int i;
6476 isl_space *dim;
6477 struct isl_set *result;
6479 if (!map)
6480 return NULL;
6482 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6483 map->dim, isl_dim_out),
6484 goto error);
6485 dim = isl_map_get_space(map);
6486 dim = isl_space_domain(dim);
6487 result = isl_set_alloc_space(dim, map->n, 0);
6488 if (!result)
6489 goto error;
6490 for (i = 0; i < map->n; ++i)
6491 result = isl_set_add_basic_set(result,
6492 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6493 isl_map_free(map);
6494 return result;
6495 error:
6496 isl_map_free(map);
6497 return NULL;
6501 * returns [domain -> range] -> range - domain
6503 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6504 __isl_take isl_basic_map *bmap)
6506 int i, k;
6507 isl_space *dim;
6508 isl_basic_map *domain;
6509 int nparam, n;
6510 unsigned total;
6512 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6513 isl_die(bmap->ctx, isl_error_invalid,
6514 "domain and range don't match", goto error);
6516 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6517 n = isl_basic_map_dim(bmap, isl_dim_in);
6519 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6520 domain = isl_basic_map_universe(dim);
6522 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6523 bmap = isl_basic_map_apply_range(bmap, domain);
6524 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6526 total = isl_basic_map_total_dim(bmap);
6528 for (i = 0; i < n; ++i) {
6529 k = isl_basic_map_alloc_equality(bmap);
6530 if (k < 0)
6531 goto error;
6532 isl_seq_clr(bmap->eq[k], 1 + total);
6533 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6534 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6535 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6538 bmap = isl_basic_map_gauss(bmap, NULL);
6539 return isl_basic_map_finalize(bmap);
6540 error:
6541 isl_basic_map_free(bmap);
6542 return NULL;
6546 * returns [domain -> range] -> range - domain
6548 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6550 int i;
6551 isl_space *domain_dim;
6553 if (!map)
6554 return NULL;
6556 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6557 isl_die(map->ctx, isl_error_invalid,
6558 "domain and range don't match", goto error);
6560 map = isl_map_cow(map);
6561 if (!map)
6562 return NULL;
6564 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6565 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6566 map->dim = isl_space_join(map->dim, domain_dim);
6567 if (!map->dim)
6568 goto error;
6569 for (i = 0; i < map->n; ++i) {
6570 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6571 if (!map->p[i])
6572 goto error;
6574 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6575 return map;
6576 error:
6577 isl_map_free(map);
6578 return NULL;
6581 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6583 struct isl_basic_map *bmap;
6584 unsigned nparam;
6585 unsigned dim;
6586 int i;
6588 if (!dims)
6589 return NULL;
6591 nparam = dims->nparam;
6592 dim = dims->n_out;
6593 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6594 if (!bmap)
6595 goto error;
6597 for (i = 0; i < dim; ++i) {
6598 int j = isl_basic_map_alloc_equality(bmap);
6599 if (j < 0)
6600 goto error;
6601 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6602 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6603 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6605 return isl_basic_map_finalize(bmap);
6606 error:
6607 isl_basic_map_free(bmap);
6608 return NULL;
6611 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6613 if (!dim)
6614 return NULL;
6615 if (dim->n_in != dim->n_out)
6616 isl_die(dim->ctx, isl_error_invalid,
6617 "number of input and output dimensions needs to be "
6618 "the same", goto error);
6619 return basic_map_identity(dim);
6620 error:
6621 isl_space_free(dim);
6622 return NULL;
6625 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6627 if (!model || !model->dim)
6628 return NULL;
6629 return isl_basic_map_identity(isl_space_copy(model->dim));
6632 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6634 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6637 struct isl_map *isl_map_identity_like(struct isl_map *model)
6639 if (!model || !model->dim)
6640 return NULL;
6641 return isl_map_identity(isl_space_copy(model->dim));
6644 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6646 if (!model || !model->dim)
6647 return NULL;
6648 return isl_map_identity(isl_space_copy(model->dim));
6651 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6653 isl_space *dim = isl_set_get_space(set);
6654 isl_map *id;
6655 id = isl_map_identity(isl_space_map_from_set(dim));
6656 return isl_map_intersect_range(id, set);
6659 /* Construct a basic set with all set dimensions having only non-negative
6660 * values.
6662 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6663 __isl_take isl_space *space)
6665 int i;
6666 unsigned nparam;
6667 unsigned dim;
6668 struct isl_basic_set *bset;
6670 if (!space)
6671 return NULL;
6672 nparam = space->nparam;
6673 dim = space->n_out;
6674 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6675 if (!bset)
6676 return NULL;
6677 for (i = 0; i < dim; ++i) {
6678 int k = isl_basic_set_alloc_inequality(bset);
6679 if (k < 0)
6680 goto error;
6681 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6682 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6684 return bset;
6685 error:
6686 isl_basic_set_free(bset);
6687 return NULL;
6690 /* Construct the half-space x_pos >= 0.
6692 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6693 int pos)
6695 int k;
6696 isl_basic_set *nonneg;
6698 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6699 k = isl_basic_set_alloc_inequality(nonneg);
6700 if (k < 0)
6701 goto error;
6702 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6703 isl_int_set_si(nonneg->ineq[k][pos], 1);
6705 return isl_basic_set_finalize(nonneg);
6706 error:
6707 isl_basic_set_free(nonneg);
6708 return NULL;
6711 /* Construct the half-space x_pos <= -1.
6713 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6715 int k;
6716 isl_basic_set *neg;
6718 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6719 k = isl_basic_set_alloc_inequality(neg);
6720 if (k < 0)
6721 goto error;
6722 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6723 isl_int_set_si(neg->ineq[k][0], -1);
6724 isl_int_set_si(neg->ineq[k][pos], -1);
6726 return isl_basic_set_finalize(neg);
6727 error:
6728 isl_basic_set_free(neg);
6729 return NULL;
6732 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6733 enum isl_dim_type type, unsigned first, unsigned n)
6735 int i;
6736 isl_basic_set *nonneg;
6737 isl_basic_set *neg;
6739 if (!set)
6740 return NULL;
6741 if (n == 0)
6742 return set;
6744 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6746 for (i = 0; i < n; ++i) {
6747 nonneg = nonneg_halfspace(isl_set_get_space(set),
6748 pos(set->dim, type) + first + i);
6749 neg = neg_halfspace(isl_set_get_space(set),
6750 pos(set->dim, type) + first + i);
6752 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6755 return set;
6756 error:
6757 isl_set_free(set);
6758 return NULL;
6761 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6762 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6763 void *user)
6765 isl_set *half;
6767 if (!set)
6768 return -1;
6769 if (isl_set_plain_is_empty(set)) {
6770 isl_set_free(set);
6771 return 0;
6773 if (first == len)
6774 return fn(set, signs, user);
6776 signs[first] = 1;
6777 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6778 1 + first));
6779 half = isl_set_intersect(half, isl_set_copy(set));
6780 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6781 goto error;
6783 signs[first] = -1;
6784 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6785 1 + first));
6786 half = isl_set_intersect(half, set);
6787 return foreach_orthant(half, signs, first + 1, len, fn, user);
6788 error:
6789 isl_set_free(set);
6790 return -1;
6793 /* Call "fn" on the intersections of "set" with each of the orthants
6794 * (except for obviously empty intersections). The orthant is identified
6795 * by the signs array, with each entry having value 1 or -1 according
6796 * to the sign of the corresponding variable.
6798 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6799 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6800 void *user)
6802 unsigned nparam;
6803 unsigned nvar;
6804 int *signs;
6805 int r;
6807 if (!set)
6808 return -1;
6809 if (isl_set_plain_is_empty(set))
6810 return 0;
6812 nparam = isl_set_dim(set, isl_dim_param);
6813 nvar = isl_set_dim(set, isl_dim_set);
6815 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6817 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6818 fn, user);
6820 free(signs);
6822 return r;
6825 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6827 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6830 int isl_basic_map_is_subset(
6831 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6833 int is_subset;
6834 struct isl_map *map1;
6835 struct isl_map *map2;
6837 if (!bmap1 || !bmap2)
6838 return -1;
6840 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6841 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6843 is_subset = isl_map_is_subset(map1, map2);
6845 isl_map_free(map1);
6846 isl_map_free(map2);
6848 return is_subset;
6851 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
6852 __isl_keep isl_basic_set *bset2)
6854 return isl_basic_map_is_subset(bset1, bset2);
6857 int isl_basic_map_is_equal(
6858 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6860 int is_subset;
6862 if (!bmap1 || !bmap2)
6863 return -1;
6864 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6865 if (is_subset != 1)
6866 return is_subset;
6867 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6868 return is_subset;
6871 int isl_basic_set_is_equal(
6872 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6874 return isl_basic_map_is_equal(
6875 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6878 int isl_map_is_empty(struct isl_map *map)
6880 int i;
6881 int is_empty;
6883 if (!map)
6884 return -1;
6885 for (i = 0; i < map->n; ++i) {
6886 is_empty = isl_basic_map_is_empty(map->p[i]);
6887 if (is_empty < 0)
6888 return -1;
6889 if (!is_empty)
6890 return 0;
6892 return 1;
6895 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6897 return map ? map->n == 0 : -1;
6900 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6902 return isl_map_plain_is_empty(map);
6905 int isl_set_plain_is_empty(struct isl_set *set)
6907 return set ? set->n == 0 : -1;
6910 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6912 return isl_set_plain_is_empty(set);
6915 int isl_set_is_empty(struct isl_set *set)
6917 return isl_map_is_empty((struct isl_map *)set);
6920 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6922 if (!map1 || !map2)
6923 return -1;
6925 return isl_space_is_equal(map1->dim, map2->dim);
6928 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6930 if (!set1 || !set2)
6931 return -1;
6933 return isl_space_is_equal(set1->dim, set2->dim);
6936 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6938 int is_subset;
6940 if (!map1 || !map2)
6941 return -1;
6942 is_subset = isl_map_is_subset(map1, map2);
6943 if (is_subset != 1)
6944 return is_subset;
6945 is_subset = isl_map_is_subset(map2, map1);
6946 return is_subset;
6949 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6951 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6954 int isl_basic_map_is_strict_subset(
6955 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6957 int is_subset;
6959 if (!bmap1 || !bmap2)
6960 return -1;
6961 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6962 if (is_subset != 1)
6963 return is_subset;
6964 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6965 if (is_subset == -1)
6966 return is_subset;
6967 return !is_subset;
6970 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6972 int is_subset;
6974 if (!map1 || !map2)
6975 return -1;
6976 is_subset = isl_map_is_subset(map1, map2);
6977 if (is_subset != 1)
6978 return is_subset;
6979 is_subset = isl_map_is_subset(map2, map1);
6980 if (is_subset == -1)
6981 return is_subset;
6982 return !is_subset;
6985 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6987 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6990 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6992 if (!bmap)
6993 return -1;
6994 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6997 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6999 if (!bset)
7000 return -1;
7001 return bset->n_eq == 0 && bset->n_ineq == 0;
7004 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7006 int i;
7008 if (!map)
7009 return -1;
7011 for (i = 0; i < map->n; ++i) {
7012 int r = isl_basic_map_is_universe(map->p[i]);
7013 if (r < 0 || r)
7014 return r;
7017 return 0;
7020 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7022 return isl_map_plain_is_universe((isl_map *) set);
7025 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7027 return isl_set_plain_is_universe(set);
7030 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7032 struct isl_basic_set *bset = NULL;
7033 struct isl_vec *sample = NULL;
7034 int empty;
7035 unsigned total;
7037 if (!bmap)
7038 return -1;
7040 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7041 return 1;
7043 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7044 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7045 copy = isl_basic_map_remove_redundancies(copy);
7046 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7047 isl_basic_map_free(copy);
7048 return empty;
7051 total = 1 + isl_basic_map_total_dim(bmap);
7052 if (bmap->sample && bmap->sample->size == total) {
7053 int contains = isl_basic_map_contains(bmap, bmap->sample);
7054 if (contains < 0)
7055 return -1;
7056 if (contains)
7057 return 0;
7059 isl_vec_free(bmap->sample);
7060 bmap->sample = NULL;
7061 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7062 if (!bset)
7063 return -1;
7064 sample = isl_basic_set_sample_vec(bset);
7065 if (!sample)
7066 return -1;
7067 empty = sample->size == 0;
7068 isl_vec_free(bmap->sample);
7069 bmap->sample = sample;
7070 if (empty)
7071 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7073 return empty;
7076 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7078 if (!bmap)
7079 return -1;
7080 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7083 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7085 return isl_basic_map_plain_is_empty(bmap);
7088 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7090 if (!bset)
7091 return -1;
7092 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7095 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7097 return isl_basic_set_plain_is_empty(bset);
7100 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7102 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7105 struct isl_map *isl_basic_map_union(
7106 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7108 struct isl_map *map;
7109 if (!bmap1 || !bmap2)
7110 goto error;
7112 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7114 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7115 if (!map)
7116 goto error;
7117 map = isl_map_add_basic_map(map, bmap1);
7118 map = isl_map_add_basic_map(map, bmap2);
7119 return map;
7120 error:
7121 isl_basic_map_free(bmap1);
7122 isl_basic_map_free(bmap2);
7123 return NULL;
7126 struct isl_set *isl_basic_set_union(
7127 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7129 return (struct isl_set *)isl_basic_map_union(
7130 (struct isl_basic_map *)bset1,
7131 (struct isl_basic_map *)bset2);
7134 /* Order divs such that any div only depends on previous divs */
7135 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7137 int i;
7138 unsigned off;
7140 if (!bmap)
7141 return NULL;
7143 off = isl_space_dim(bmap->dim, isl_dim_all);
7145 for (i = 0; i < bmap->n_div; ++i) {
7146 int pos;
7147 if (isl_int_is_zero(bmap->div[i][0]))
7148 continue;
7149 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7150 bmap->n_div-i);
7151 if (pos == -1)
7152 continue;
7153 isl_basic_map_swap_div(bmap, i, i + pos);
7154 --i;
7156 return bmap;
7159 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7161 return (struct isl_basic_set *)
7162 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7165 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7167 int i;
7169 if (!map)
7170 return 0;
7172 for (i = 0; i < map->n; ++i) {
7173 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7174 if (!map->p[i])
7175 goto error;
7178 return map;
7179 error:
7180 isl_map_free(map);
7181 return NULL;
7184 /* Apply the expansion computed by isl_merge_divs.
7185 * The expansion itself is given by "exp" while the resulting
7186 * list of divs is given by "div".
7188 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7189 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7191 int i, j;
7192 int n_div;
7194 bset = isl_basic_set_cow(bset);
7195 if (!bset || !div)
7196 goto error;
7198 if (div->n_row < bset->n_div)
7199 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7200 "not an expansion", goto error);
7202 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7203 div->n_row - bset->n_div, 0,
7204 2 * (div->n_row - bset->n_div));
7206 n_div = bset->n_div;
7207 for (i = n_div; i < div->n_row; ++i)
7208 if (isl_basic_set_alloc_div(bset) < 0)
7209 goto error;
7211 j = n_div - 1;
7212 for (i = div->n_row - 1; i >= 0; --i) {
7213 if (j >= 0 && exp[j] == i) {
7214 if (i != j)
7215 isl_basic_map_swap_div(bset, i, j);
7216 j--;
7217 } else {
7218 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7219 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7220 goto error;
7224 isl_mat_free(div);
7225 return bset;
7226 error:
7227 isl_basic_set_free(bset);
7228 isl_mat_free(div);
7229 return NULL;
7232 /* Look for a div in dst that corresponds to the div "div" in src.
7233 * The divs before "div" in src and dst are assumed to be the same.
7235 * Returns -1 if no corresponding div was found and the position
7236 * of the corresponding div in dst otherwise.
7238 static int find_div(struct isl_basic_map *dst,
7239 struct isl_basic_map *src, unsigned div)
7241 int i;
7243 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7245 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7246 for (i = div; i < dst->n_div; ++i)
7247 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7248 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7249 dst->n_div - div) == -1)
7250 return i;
7251 return -1;
7254 struct isl_basic_map *isl_basic_map_align_divs(
7255 struct isl_basic_map *dst, struct isl_basic_map *src)
7257 int i;
7258 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7260 if (!dst || !src)
7261 goto error;
7263 if (src->n_div == 0)
7264 return dst;
7266 for (i = 0; i < src->n_div; ++i)
7267 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7269 src = isl_basic_map_order_divs(src);
7270 dst = isl_basic_map_cow(dst);
7271 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7272 src->n_div, 0, 2 * src->n_div);
7273 if (!dst)
7274 return NULL;
7275 for (i = 0; i < src->n_div; ++i) {
7276 int j = find_div(dst, src, i);
7277 if (j < 0) {
7278 j = isl_basic_map_alloc_div(dst);
7279 if (j < 0)
7280 goto error;
7281 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7282 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7283 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7284 goto error;
7286 if (j != i)
7287 isl_basic_map_swap_div(dst, i, j);
7289 return dst;
7290 error:
7291 isl_basic_map_free(dst);
7292 return NULL;
7295 struct isl_basic_set *isl_basic_set_align_divs(
7296 struct isl_basic_set *dst, struct isl_basic_set *src)
7298 return (struct isl_basic_set *)isl_basic_map_align_divs(
7299 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7302 struct isl_map *isl_map_align_divs(struct isl_map *map)
7304 int i;
7306 if (!map)
7307 return NULL;
7308 if (map->n == 0)
7309 return map;
7310 map = isl_map_compute_divs(map);
7311 map = isl_map_cow(map);
7312 if (!map)
7313 return NULL;
7315 for (i = 1; i < map->n; ++i)
7316 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7317 for (i = 1; i < map->n; ++i)
7318 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7320 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7321 return map;
7324 struct isl_set *isl_set_align_divs(struct isl_set *set)
7326 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7329 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7330 __isl_take isl_map *map)
7332 if (!set || !map)
7333 goto error;
7334 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7335 map = isl_map_intersect_domain(map, set);
7336 set = isl_map_range(map);
7337 return set;
7338 error:
7339 isl_set_free(set);
7340 isl_map_free(map);
7341 return NULL;
7344 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7345 __isl_take isl_map *map)
7347 return isl_map_align_params_map_map_and(set, map, &set_apply);
7350 /* There is no need to cow as removing empty parts doesn't change
7351 * the meaning of the set.
7353 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7355 int i;
7357 if (!map)
7358 return NULL;
7360 for (i = map->n - 1; i >= 0; --i)
7361 remove_if_empty(map, i);
7363 return map;
7366 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7368 return (struct isl_set *)
7369 isl_map_remove_empty_parts((struct isl_map *)set);
7372 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7374 struct isl_basic_map *bmap;
7375 if (!map || map->n == 0)
7376 return NULL;
7377 bmap = map->p[map->n-1];
7378 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7379 return isl_basic_map_copy(bmap);
7382 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7384 return (struct isl_basic_set *)
7385 isl_map_copy_basic_map((struct isl_map *)set);
7388 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7389 __isl_keep isl_basic_map *bmap)
7391 int i;
7393 if (!map || !bmap)
7394 goto error;
7395 for (i = map->n-1; i >= 0; --i) {
7396 if (map->p[i] != bmap)
7397 continue;
7398 map = isl_map_cow(map);
7399 if (!map)
7400 goto error;
7401 isl_basic_map_free(map->p[i]);
7402 if (i != map->n-1) {
7403 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7404 map->p[i] = map->p[map->n-1];
7406 map->n--;
7407 return map;
7409 return map;
7410 error:
7411 isl_map_free(map);
7412 return NULL;
7415 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7416 struct isl_basic_set *bset)
7418 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7419 (struct isl_basic_map *)bset);
7422 /* Given two basic sets bset1 and bset2, compute the maximal difference
7423 * between the values of dimension pos in bset1 and those in bset2
7424 * for any common value of the parameters and dimensions preceding pos.
7426 static enum isl_lp_result basic_set_maximal_difference_at(
7427 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7428 int pos, isl_int *opt)
7430 isl_space *dims;
7431 struct isl_basic_map *bmap1 = NULL;
7432 struct isl_basic_map *bmap2 = NULL;
7433 struct isl_ctx *ctx;
7434 struct isl_vec *obj;
7435 unsigned total;
7436 unsigned nparam;
7437 unsigned dim1, dim2;
7438 enum isl_lp_result res;
7440 if (!bset1 || !bset2)
7441 return isl_lp_error;
7443 nparam = isl_basic_set_n_param(bset1);
7444 dim1 = isl_basic_set_n_dim(bset1);
7445 dim2 = isl_basic_set_n_dim(bset2);
7446 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7447 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7448 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7449 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7450 if (!bmap1 || !bmap2)
7451 goto error;
7452 bmap1 = isl_basic_map_cow(bmap1);
7453 bmap1 = isl_basic_map_extend(bmap1, nparam,
7454 pos, (dim1 - pos) + (dim2 - pos),
7455 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7456 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7457 if (!bmap1)
7458 goto error;
7459 total = isl_basic_map_total_dim(bmap1);
7460 ctx = bmap1->ctx;
7461 obj = isl_vec_alloc(ctx, 1 + total);
7462 isl_seq_clr(obj->block.data, 1 + total);
7463 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7464 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7465 if (!obj)
7466 goto error;
7467 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7468 opt, NULL, NULL);
7469 isl_basic_map_free(bmap1);
7470 isl_vec_free(obj);
7471 return res;
7472 error:
7473 isl_basic_map_free(bmap1);
7474 isl_basic_map_free(bmap2);
7475 return isl_lp_error;
7478 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7479 * for any common value of the parameters and dimensions preceding pos
7480 * in both basic sets, the values of dimension pos in bset1 are
7481 * smaller or larger than those in bset2.
7483 * Returns
7484 * 1 if bset1 follows bset2
7485 * -1 if bset1 precedes bset2
7486 * 0 if bset1 and bset2 are incomparable
7487 * -2 if some error occurred.
7489 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7490 struct isl_basic_set *bset2, int pos)
7492 isl_int opt;
7493 enum isl_lp_result res;
7494 int cmp;
7496 isl_int_init(opt);
7498 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7500 if (res == isl_lp_empty)
7501 cmp = 0;
7502 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7503 res == isl_lp_unbounded)
7504 cmp = 1;
7505 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7506 cmp = -1;
7507 else
7508 cmp = -2;
7510 isl_int_clear(opt);
7511 return cmp;
7514 /* Given two basic sets bset1 and bset2, check whether
7515 * for any common value of the parameters and dimensions preceding pos
7516 * there is a value of dimension pos in bset1 that is larger
7517 * than a value of the same dimension in bset2.
7519 * Return
7520 * 1 if there exists such a pair
7521 * 0 if there is no such pair, but there is a pair of equal values
7522 * -1 otherwise
7523 * -2 if some error occurred.
7525 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7526 __isl_keep isl_basic_set *bset2, int pos)
7528 isl_int opt;
7529 enum isl_lp_result res;
7530 int cmp;
7532 isl_int_init(opt);
7534 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7536 if (res == isl_lp_empty)
7537 cmp = -1;
7538 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7539 res == isl_lp_unbounded)
7540 cmp = 1;
7541 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7542 cmp = -1;
7543 else if (res == isl_lp_ok)
7544 cmp = 0;
7545 else
7546 cmp = -2;
7548 isl_int_clear(opt);
7549 return cmp;
7552 /* Given two sets set1 and set2, check whether
7553 * for any common value of the parameters and dimensions preceding pos
7554 * there is a value of dimension pos in set1 that is larger
7555 * than a value of the same dimension in set2.
7557 * Return
7558 * 1 if there exists such a pair
7559 * 0 if there is no such pair, but there is a pair of equal values
7560 * -1 otherwise
7561 * -2 if some error occurred.
7563 int isl_set_follows_at(__isl_keep isl_set *set1,
7564 __isl_keep isl_set *set2, int pos)
7566 int i, j;
7567 int follows = -1;
7569 if (!set1 || !set2)
7570 return -2;
7572 for (i = 0; i < set1->n; ++i)
7573 for (j = 0; j < set2->n; ++j) {
7574 int f;
7575 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7576 if (f == 1 || f == -2)
7577 return f;
7578 if (f > follows)
7579 follows = f;
7582 return follows;
7585 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7586 unsigned pos, isl_int *val)
7588 int i;
7589 int d;
7590 unsigned total;
7592 if (!bmap)
7593 return -1;
7594 total = isl_basic_map_total_dim(bmap);
7595 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7596 for (; d+1 > pos; --d)
7597 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7598 break;
7599 if (d != pos)
7600 continue;
7601 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7602 return 0;
7603 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7604 return 0;
7605 if (!isl_int_is_one(bmap->eq[i][1+d]))
7606 return 0;
7607 if (val)
7608 isl_int_neg(*val, bmap->eq[i][0]);
7609 return 1;
7611 return 0;
7614 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7615 unsigned pos, isl_int *val)
7617 int i;
7618 isl_int v;
7619 isl_int tmp;
7620 int fixed;
7622 if (!map)
7623 return -1;
7624 if (map->n == 0)
7625 return 0;
7626 if (map->n == 1)
7627 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7628 isl_int_init(v);
7629 isl_int_init(tmp);
7630 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7631 for (i = 1; fixed == 1 && i < map->n; ++i) {
7632 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7633 if (fixed == 1 && isl_int_ne(tmp, v))
7634 fixed = 0;
7636 if (val)
7637 isl_int_set(*val, v);
7638 isl_int_clear(tmp);
7639 isl_int_clear(v);
7640 return fixed;
7643 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7644 unsigned pos, isl_int *val)
7646 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7647 pos, val);
7650 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7651 isl_int *val)
7653 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7656 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7657 enum isl_dim_type type, unsigned pos, isl_int *val)
7659 if (pos >= isl_basic_map_dim(bmap, type))
7660 return -1;
7661 return isl_basic_map_plain_has_fixed_var(bmap,
7662 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7665 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7666 enum isl_dim_type type, unsigned pos, isl_int *val)
7668 if (pos >= isl_map_dim(map, type))
7669 return -1;
7670 return isl_map_plain_has_fixed_var(map,
7671 map_offset(map, type) - 1 + pos, val);
7674 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7675 enum isl_dim_type type, unsigned pos, isl_int *val)
7677 return isl_map_plain_is_fixed(set, type, pos, val);
7680 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7681 enum isl_dim_type type, unsigned pos, isl_int *val)
7683 return isl_map_plain_is_fixed(map, type, pos, val);
7686 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7687 * then return this fixed value in *val.
7689 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7690 unsigned dim, isl_int *val)
7692 return isl_basic_set_plain_has_fixed_var(bset,
7693 isl_basic_set_n_param(bset) + dim, val);
7696 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7697 * then return this fixed value in *val.
7699 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7700 unsigned dim, isl_int *val)
7702 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7705 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7706 unsigned dim, isl_int *val)
7708 return isl_set_plain_dim_is_fixed(set, dim, val);
7711 /* Check if input variable in has fixed value and if so and if val is not NULL,
7712 * then return this fixed value in *val.
7714 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7715 unsigned in, isl_int *val)
7717 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7720 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7721 * and if val is not NULL, then return this lower bound in *val.
7723 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7724 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7726 int i, i_eq = -1, i_ineq = -1;
7727 isl_int *c;
7728 unsigned total;
7729 unsigned nparam;
7731 if (!bset)
7732 return -1;
7733 total = isl_basic_set_total_dim(bset);
7734 nparam = isl_basic_set_n_param(bset);
7735 for (i = 0; i < bset->n_eq; ++i) {
7736 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7737 continue;
7738 if (i_eq != -1)
7739 return 0;
7740 i_eq = i;
7742 for (i = 0; i < bset->n_ineq; ++i) {
7743 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7744 continue;
7745 if (i_eq != -1 || i_ineq != -1)
7746 return 0;
7747 i_ineq = i;
7749 if (i_eq == -1 && i_ineq == -1)
7750 return 0;
7751 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7752 /* The coefficient should always be one due to normalization. */
7753 if (!isl_int_is_one(c[1+nparam+dim]))
7754 return 0;
7755 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7756 return 0;
7757 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7758 total - nparam - dim - 1) != -1)
7759 return 0;
7760 if (val)
7761 isl_int_neg(*val, c[0]);
7762 return 1;
7765 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7766 unsigned dim, isl_int *val)
7768 int i;
7769 isl_int v;
7770 isl_int tmp;
7771 int fixed;
7773 if (!set)
7774 return -1;
7775 if (set->n == 0)
7776 return 0;
7777 if (set->n == 1)
7778 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7779 dim, val);
7780 isl_int_init(v);
7781 isl_int_init(tmp);
7782 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7783 dim, &v);
7784 for (i = 1; fixed == 1 && i < set->n; ++i) {
7785 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7786 dim, &tmp);
7787 if (fixed == 1 && isl_int_ne(tmp, v))
7788 fixed = 0;
7790 if (val)
7791 isl_int_set(*val, v);
7792 isl_int_clear(tmp);
7793 isl_int_clear(v);
7794 return fixed;
7797 struct constraint {
7798 unsigned size;
7799 isl_int *c;
7802 /* uset_gist depends on constraints without existentially quantified
7803 * variables sorting first.
7805 static int qsort_constraint_cmp(const void *p1, const void *p2)
7807 const struct constraint *c1 = (const struct constraint *)p1;
7808 const struct constraint *c2 = (const struct constraint *)p2;
7809 int l1, l2;
7810 unsigned size = isl_min(c1->size, c2->size);
7812 l1 = isl_seq_last_non_zero(c1->c, size);
7813 l2 = isl_seq_last_non_zero(c2->c, size);
7815 if (l1 != l2)
7816 return l1 - l2;
7818 return isl_seq_cmp(c1->c, c2->c, size);
7821 static struct isl_basic_map *isl_basic_map_sort_constraints(
7822 struct isl_basic_map *bmap)
7824 int i;
7825 struct constraint *c;
7826 unsigned total;
7828 if (!bmap)
7829 return NULL;
7830 total = isl_basic_map_total_dim(bmap);
7831 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7832 if (!c)
7833 goto error;
7834 for (i = 0; i < bmap->n_ineq; ++i) {
7835 c[i].size = total;
7836 c[i].c = bmap->ineq[i];
7838 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7839 for (i = 0; i < bmap->n_ineq; ++i)
7840 bmap->ineq[i] = c[i].c;
7841 free(c);
7842 return bmap;
7843 error:
7844 isl_basic_map_free(bmap);
7845 return NULL;
7848 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7849 __isl_take isl_basic_set *bset)
7851 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7852 (struct isl_basic_map *)bset);
7855 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7857 if (!bmap)
7858 return NULL;
7859 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7860 return bmap;
7861 bmap = isl_basic_map_remove_redundancies(bmap);
7862 bmap = isl_basic_map_sort_constraints(bmap);
7863 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7864 return bmap;
7867 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7869 return (struct isl_basic_set *)isl_basic_map_normalize(
7870 (struct isl_basic_map *)bset);
7873 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7874 const __isl_keep isl_basic_map *bmap2)
7876 int i, cmp;
7877 unsigned total;
7879 if (bmap1 == bmap2)
7880 return 0;
7881 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7882 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7883 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7884 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7885 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7886 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7887 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7888 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7889 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7890 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7891 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7892 return 0;
7893 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7894 return 1;
7895 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7896 return -1;
7897 if (bmap1->n_eq != bmap2->n_eq)
7898 return bmap1->n_eq - bmap2->n_eq;
7899 if (bmap1->n_ineq != bmap2->n_ineq)
7900 return bmap1->n_ineq - bmap2->n_ineq;
7901 if (bmap1->n_div != bmap2->n_div)
7902 return bmap1->n_div - bmap2->n_div;
7903 total = isl_basic_map_total_dim(bmap1);
7904 for (i = 0; i < bmap1->n_eq; ++i) {
7905 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7906 if (cmp)
7907 return cmp;
7909 for (i = 0; i < bmap1->n_ineq; ++i) {
7910 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7911 if (cmp)
7912 return cmp;
7914 for (i = 0; i < bmap1->n_div; ++i) {
7915 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7916 if (cmp)
7917 return cmp;
7919 return 0;
7922 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7923 const __isl_keep isl_basic_set *bset2)
7925 return isl_basic_map_plain_cmp(bset1, bset2);
7928 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7929 const __isl_keep isl_set *set2)
7931 int i, cmp;
7933 if (set1 == set2)
7934 return 0;
7935 if (set1->n != set2->n)
7936 return set1->n - set2->n;
7938 for (i = 0; i < set1->n; ++i) {
7939 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7940 if (cmp)
7941 return cmp;
7944 return 0;
7947 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7948 __isl_keep isl_basic_map *bmap2)
7950 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7953 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7954 __isl_keep isl_basic_set *bset2)
7956 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7957 (isl_basic_map *)bset2);
7960 static int qsort_bmap_cmp(const void *p1, const void *p2)
7962 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7963 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7965 return isl_basic_map_plain_cmp(bmap1, bmap2);
7968 /* We normalize in place, but if anything goes wrong we need
7969 * to return NULL, so we need to make sure we don't change the
7970 * meaning of any possible other copies of map.
7972 struct isl_map *isl_map_normalize(struct isl_map *map)
7974 int i, j;
7975 struct isl_basic_map *bmap;
7977 if (!map)
7978 return NULL;
7979 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7980 return map;
7981 for (i = 0; i < map->n; ++i) {
7982 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7983 if (!bmap)
7984 goto error;
7985 isl_basic_map_free(map->p[i]);
7986 map->p[i] = bmap;
7988 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7989 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7990 map = isl_map_remove_empty_parts(map);
7991 if (!map)
7992 return NULL;
7993 for (i = map->n - 1; i >= 1; --i) {
7994 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7995 continue;
7996 isl_basic_map_free(map->p[i-1]);
7997 for (j = i; j < map->n; ++j)
7998 map->p[j-1] = map->p[j];
7999 map->n--;
8001 return map;
8002 error:
8003 isl_map_free(map);
8004 return NULL;
8008 struct isl_set *isl_set_normalize(struct isl_set *set)
8010 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8013 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8015 int i;
8016 int equal;
8018 if (!map1 || !map2)
8019 return -1;
8021 if (map1 == map2)
8022 return 1;
8023 if (!isl_space_is_equal(map1->dim, map2->dim))
8024 return 0;
8026 map1 = isl_map_copy(map1);
8027 map2 = isl_map_copy(map2);
8028 map1 = isl_map_normalize(map1);
8029 map2 = isl_map_normalize(map2);
8030 if (!map1 || !map2)
8031 goto error;
8032 equal = map1->n == map2->n;
8033 for (i = 0; equal && i < map1->n; ++i) {
8034 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8035 if (equal < 0)
8036 goto error;
8038 isl_map_free(map1);
8039 isl_map_free(map2);
8040 return equal;
8041 error:
8042 isl_map_free(map1);
8043 isl_map_free(map2);
8044 return -1;
8047 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8049 return isl_map_plain_is_equal(map1, map2);
8052 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8054 return isl_map_plain_is_equal((struct isl_map *)set1,
8055 (struct isl_map *)set2);
8058 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8060 return isl_set_plain_is_equal(set1, set2);
8063 /* Return an interval that ranges from min to max (inclusive)
8065 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8066 isl_int min, isl_int max)
8068 int k;
8069 struct isl_basic_set *bset = NULL;
8071 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8072 if (!bset)
8073 goto error;
8075 k = isl_basic_set_alloc_inequality(bset);
8076 if (k < 0)
8077 goto error;
8078 isl_int_set_si(bset->ineq[k][1], 1);
8079 isl_int_neg(bset->ineq[k][0], min);
8081 k = isl_basic_set_alloc_inequality(bset);
8082 if (k < 0)
8083 goto error;
8084 isl_int_set_si(bset->ineq[k][1], -1);
8085 isl_int_set(bset->ineq[k][0], max);
8087 return bset;
8088 error:
8089 isl_basic_set_free(bset);
8090 return NULL;
8093 /* Return the Cartesian product of the basic sets in list (in the given order).
8095 __isl_give isl_basic_set *isl_basic_set_list_product(
8096 __isl_take struct isl_basic_set_list *list)
8098 int i;
8099 unsigned dim;
8100 unsigned nparam;
8101 unsigned extra;
8102 unsigned n_eq;
8103 unsigned n_ineq;
8104 struct isl_basic_set *product = NULL;
8106 if (!list)
8107 goto error;
8108 isl_assert(list->ctx, list->n > 0, goto error);
8109 isl_assert(list->ctx, list->p[0], goto error);
8110 nparam = isl_basic_set_n_param(list->p[0]);
8111 dim = isl_basic_set_n_dim(list->p[0]);
8112 extra = list->p[0]->n_div;
8113 n_eq = list->p[0]->n_eq;
8114 n_ineq = list->p[0]->n_ineq;
8115 for (i = 1; i < list->n; ++i) {
8116 isl_assert(list->ctx, list->p[i], goto error);
8117 isl_assert(list->ctx,
8118 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8119 dim += isl_basic_set_n_dim(list->p[i]);
8120 extra += list->p[i]->n_div;
8121 n_eq += list->p[i]->n_eq;
8122 n_ineq += list->p[i]->n_ineq;
8124 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8125 n_eq, n_ineq);
8126 if (!product)
8127 goto error;
8128 dim = 0;
8129 for (i = 0; i < list->n; ++i) {
8130 isl_basic_set_add_constraints(product,
8131 isl_basic_set_copy(list->p[i]), dim);
8132 dim += isl_basic_set_n_dim(list->p[i]);
8134 isl_basic_set_list_free(list);
8135 return product;
8136 error:
8137 isl_basic_set_free(product);
8138 isl_basic_set_list_free(list);
8139 return NULL;
8142 struct isl_basic_map *isl_basic_map_product(
8143 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8145 isl_space *dim_result = NULL;
8146 struct isl_basic_map *bmap;
8147 unsigned in1, in2, out1, out2, nparam, total, pos;
8148 struct isl_dim_map *dim_map1, *dim_map2;
8150 if (!bmap1 || !bmap2)
8151 goto error;
8153 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8154 bmap2->dim, isl_dim_param), goto error);
8155 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8156 isl_space_copy(bmap2->dim));
8158 in1 = isl_basic_map_n_in(bmap1);
8159 in2 = isl_basic_map_n_in(bmap2);
8160 out1 = isl_basic_map_n_out(bmap1);
8161 out2 = isl_basic_map_n_out(bmap2);
8162 nparam = isl_basic_map_n_param(bmap1);
8164 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8165 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8166 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8167 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8168 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8169 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8170 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8171 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8172 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8173 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8174 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8176 bmap = isl_basic_map_alloc_space(dim_result,
8177 bmap1->n_div + bmap2->n_div,
8178 bmap1->n_eq + bmap2->n_eq,
8179 bmap1->n_ineq + bmap2->n_ineq);
8180 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8181 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8182 bmap = isl_basic_map_simplify(bmap);
8183 return isl_basic_map_finalize(bmap);
8184 error:
8185 isl_basic_map_free(bmap1);
8186 isl_basic_map_free(bmap2);
8187 return NULL;
8190 __isl_give isl_basic_map *isl_basic_map_flat_product(
8191 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8193 isl_basic_map *prod;
8195 prod = isl_basic_map_product(bmap1, bmap2);
8196 prod = isl_basic_map_flatten(prod);
8197 return prod;
8200 __isl_give isl_basic_set *isl_basic_set_flat_product(
8201 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8203 return isl_basic_map_flat_range_product(bset1, bset2);
8206 __isl_give isl_basic_map *isl_basic_map_domain_product(
8207 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8209 isl_space *space_result = NULL;
8210 isl_basic_map *bmap;
8211 unsigned in1, in2, out, nparam, total, pos;
8212 struct isl_dim_map *dim_map1, *dim_map2;
8214 if (!bmap1 || !bmap2)
8215 goto error;
8217 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8218 isl_space_copy(bmap2->dim));
8220 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8221 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8222 out = isl_basic_map_dim(bmap1, isl_dim_out);
8223 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8225 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8226 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8227 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8228 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8229 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8230 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8231 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8232 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8233 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8234 isl_dim_map_div(dim_map1, bmap1, pos += out);
8235 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8237 bmap = isl_basic_map_alloc_space(space_result,
8238 bmap1->n_div + bmap2->n_div,
8239 bmap1->n_eq + bmap2->n_eq,
8240 bmap1->n_ineq + bmap2->n_ineq);
8241 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8242 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8243 bmap = isl_basic_map_simplify(bmap);
8244 return isl_basic_map_finalize(bmap);
8245 error:
8246 isl_basic_map_free(bmap1);
8247 isl_basic_map_free(bmap2);
8248 return NULL;
8251 __isl_give isl_basic_map *isl_basic_map_range_product(
8252 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8254 isl_space *dim_result = NULL;
8255 isl_basic_map *bmap;
8256 unsigned in, out1, out2, nparam, total, pos;
8257 struct isl_dim_map *dim_map1, *dim_map2;
8259 if (!bmap1 || !bmap2)
8260 goto error;
8262 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8263 isl_space_copy(bmap2->dim));
8265 in = isl_basic_map_dim(bmap1, isl_dim_in);
8266 out1 = isl_basic_map_n_out(bmap1);
8267 out2 = isl_basic_map_n_out(bmap2);
8268 nparam = isl_basic_map_n_param(bmap1);
8270 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8271 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8272 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8273 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8274 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8275 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8276 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8277 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8278 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8279 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8280 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8282 bmap = isl_basic_map_alloc_space(dim_result,
8283 bmap1->n_div + bmap2->n_div,
8284 bmap1->n_eq + bmap2->n_eq,
8285 bmap1->n_ineq + bmap2->n_ineq);
8286 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8287 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8288 bmap = isl_basic_map_simplify(bmap);
8289 return isl_basic_map_finalize(bmap);
8290 error:
8291 isl_basic_map_free(bmap1);
8292 isl_basic_map_free(bmap2);
8293 return NULL;
8296 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8297 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8299 isl_basic_map *prod;
8301 prod = isl_basic_map_range_product(bmap1, bmap2);
8302 prod = isl_basic_map_flatten_range(prod);
8303 return prod;
8306 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8307 __isl_take isl_map *map2,
8308 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8309 __isl_take isl_space *right),
8310 __isl_give isl_basic_map *(*basic_map_product)(
8311 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8313 unsigned flags = 0;
8314 struct isl_map *result;
8315 int i, j;
8317 if (!map1 || !map2)
8318 goto error;
8320 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8321 map2->dim, isl_dim_param), goto error);
8323 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8324 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8325 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8327 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8328 isl_space_copy(map2->dim)),
8329 map1->n * map2->n, flags);
8330 if (!result)
8331 goto error;
8332 for (i = 0; i < map1->n; ++i)
8333 for (j = 0; j < map2->n; ++j) {
8334 struct isl_basic_map *part;
8335 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8336 isl_basic_map_copy(map2->p[j]));
8337 if (isl_basic_map_is_empty(part))
8338 isl_basic_map_free(part);
8339 else
8340 result = isl_map_add_basic_map(result, part);
8341 if (!result)
8342 goto error;
8344 isl_map_free(map1);
8345 isl_map_free(map2);
8346 return result;
8347 error:
8348 isl_map_free(map1);
8349 isl_map_free(map2);
8350 return NULL;
8353 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8355 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8356 __isl_take isl_map *map2)
8358 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8361 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8362 __isl_take isl_map *map2)
8364 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8367 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8369 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8370 __isl_take isl_map *map2)
8372 isl_map *prod;
8374 prod = isl_map_product(map1, map2);
8375 prod = isl_map_flatten(prod);
8376 return prod;
8379 /* Given two set A and B, construct its Cartesian product A x B.
8381 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8383 return isl_map_range_product(set1, set2);
8386 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8387 __isl_take isl_set *set2)
8389 return isl_map_flat_range_product(set1, set2);
8392 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8394 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8395 __isl_take isl_map *map2)
8397 return map_product(map1, map2, &isl_space_domain_product,
8398 &isl_basic_map_domain_product);
8401 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8403 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8404 __isl_take isl_map *map2)
8406 return map_product(map1, map2, &isl_space_range_product,
8407 &isl_basic_map_range_product);
8410 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8411 __isl_take isl_map *map2)
8413 return isl_map_align_params_map_map_and(map1, map2,
8414 &map_domain_product_aligned);
8417 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8418 __isl_take isl_map *map2)
8420 return isl_map_align_params_map_map_and(map1, map2,
8421 &map_range_product_aligned);
8424 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8426 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8427 __isl_take isl_map *map2)
8429 isl_map *prod;
8431 prod = isl_map_domain_product(map1, map2);
8432 prod = isl_map_flatten_domain(prod);
8433 return prod;
8436 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8438 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8439 __isl_take isl_map *map2)
8441 isl_map *prod;
8443 prod = isl_map_range_product(map1, map2);
8444 prod = isl_map_flatten_range(prod);
8445 return prod;
8448 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8450 int i;
8451 uint32_t hash = isl_hash_init();
8452 unsigned total;
8454 if (!bmap)
8455 return 0;
8456 bmap = isl_basic_map_copy(bmap);
8457 bmap = isl_basic_map_normalize(bmap);
8458 if (!bmap)
8459 return 0;
8460 total = isl_basic_map_total_dim(bmap);
8461 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8462 for (i = 0; i < bmap->n_eq; ++i) {
8463 uint32_t c_hash;
8464 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8465 isl_hash_hash(hash, c_hash);
8467 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8468 for (i = 0; i < bmap->n_ineq; ++i) {
8469 uint32_t c_hash;
8470 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8471 isl_hash_hash(hash, c_hash);
8473 isl_hash_byte(hash, bmap->n_div & 0xFF);
8474 for (i = 0; i < bmap->n_div; ++i) {
8475 uint32_t c_hash;
8476 if (isl_int_is_zero(bmap->div[i][0]))
8477 continue;
8478 isl_hash_byte(hash, i & 0xFF);
8479 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8480 isl_hash_hash(hash, c_hash);
8482 isl_basic_map_free(bmap);
8483 return hash;
8486 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8488 return isl_basic_map_get_hash((isl_basic_map *)bset);
8491 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8493 int i;
8494 uint32_t hash;
8496 if (!map)
8497 return 0;
8498 map = isl_map_copy(map);
8499 map = isl_map_normalize(map);
8500 if (!map)
8501 return 0;
8503 hash = isl_hash_init();
8504 for (i = 0; i < map->n; ++i) {
8505 uint32_t bmap_hash;
8506 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8507 isl_hash_hash(hash, bmap_hash);
8510 isl_map_free(map);
8512 return hash;
8515 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8517 return isl_map_get_hash((isl_map *)set);
8520 /* Check if the value for dimension dim is completely determined
8521 * by the values of the other parameters and variables.
8522 * That is, check if dimension dim is involved in an equality.
8524 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8526 int i;
8527 unsigned nparam;
8529 if (!bset)
8530 return -1;
8531 nparam = isl_basic_set_n_param(bset);
8532 for (i = 0; i < bset->n_eq; ++i)
8533 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8534 return 1;
8535 return 0;
8538 /* Check if the value for dimension dim is completely determined
8539 * by the values of the other parameters and variables.
8540 * That is, check if dimension dim is involved in an equality
8541 * for each of the subsets.
8543 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8545 int i;
8547 if (!set)
8548 return -1;
8549 for (i = 0; i < set->n; ++i) {
8550 int unique;
8551 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8552 if (unique != 1)
8553 return unique;
8555 return 1;
8558 int isl_set_n_basic_set(__isl_keep isl_set *set)
8560 return set ? set->n : 0;
8563 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8564 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8566 int i;
8568 if (!map)
8569 return -1;
8571 for (i = 0; i < map->n; ++i)
8572 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8573 return -1;
8575 return 0;
8578 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8579 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8581 int i;
8583 if (!set)
8584 return -1;
8586 for (i = 0; i < set->n; ++i)
8587 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8588 return -1;
8590 return 0;
8593 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8595 isl_space *dim;
8597 if (!bset)
8598 return NULL;
8600 bset = isl_basic_set_cow(bset);
8601 if (!bset)
8602 return NULL;
8604 dim = isl_basic_set_get_space(bset);
8605 dim = isl_space_lift(dim, bset->n_div);
8606 if (!dim)
8607 goto error;
8608 isl_space_free(bset->dim);
8609 bset->dim = dim;
8610 bset->extra -= bset->n_div;
8611 bset->n_div = 0;
8613 bset = isl_basic_set_finalize(bset);
8615 return bset;
8616 error:
8617 isl_basic_set_free(bset);
8618 return NULL;
8621 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8623 int i;
8624 isl_space *dim;
8625 unsigned n_div;
8627 set = isl_set_align_divs(set);
8629 if (!set)
8630 return NULL;
8632 set = isl_set_cow(set);
8633 if (!set)
8634 return NULL;
8636 n_div = set->p[0]->n_div;
8637 dim = isl_set_get_space(set);
8638 dim = isl_space_lift(dim, n_div);
8639 if (!dim)
8640 goto error;
8641 isl_space_free(set->dim);
8642 set->dim = dim;
8644 for (i = 0; i < set->n; ++i) {
8645 set->p[i] = isl_basic_set_lift(set->p[i]);
8646 if (!set->p[i])
8647 goto error;
8650 return set;
8651 error:
8652 isl_set_free(set);
8653 return NULL;
8656 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8658 isl_space *dim;
8659 struct isl_basic_map *bmap;
8660 unsigned n_set;
8661 unsigned n_div;
8662 unsigned n_param;
8663 unsigned total;
8664 int i, k, l;
8666 set = isl_set_align_divs(set);
8668 if (!set)
8669 return NULL;
8671 dim = isl_set_get_space(set);
8672 if (set->n == 0 || set->p[0]->n_div == 0) {
8673 isl_set_free(set);
8674 return isl_map_identity(isl_space_map_from_set(dim));
8677 n_div = set->p[0]->n_div;
8678 dim = isl_space_map_from_set(dim);
8679 n_param = isl_space_dim(dim, isl_dim_param);
8680 n_set = isl_space_dim(dim, isl_dim_in);
8681 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8682 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8683 for (i = 0; i < n_set; ++i)
8684 bmap = var_equal(bmap, i);
8686 total = n_param + n_set + n_set + n_div;
8687 for (i = 0; i < n_div; ++i) {
8688 k = isl_basic_map_alloc_inequality(bmap);
8689 if (k < 0)
8690 goto error;
8691 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8692 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8693 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8694 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8695 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8696 set->p[0]->div[i][0]);
8698 l = isl_basic_map_alloc_inequality(bmap);
8699 if (l < 0)
8700 goto error;
8701 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8702 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8703 set->p[0]->div[i][0]);
8704 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8707 isl_set_free(set);
8708 bmap = isl_basic_map_simplify(bmap);
8709 bmap = isl_basic_map_finalize(bmap);
8710 return isl_map_from_basic_map(bmap);
8711 error:
8712 isl_set_free(set);
8713 isl_basic_map_free(bmap);
8714 return NULL;
8717 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8719 unsigned dim;
8720 int size = 0;
8722 if (!bset)
8723 return -1;
8725 dim = isl_basic_set_total_dim(bset);
8726 size += bset->n_eq * (1 + dim);
8727 size += bset->n_ineq * (1 + dim);
8728 size += bset->n_div * (2 + dim);
8730 return size;
8733 int isl_set_size(__isl_keep isl_set *set)
8735 int i;
8736 int size = 0;
8738 if (!set)
8739 return -1;
8741 for (i = 0; i < set->n; ++i)
8742 size += isl_basic_set_size(set->p[i]);
8744 return size;
8747 /* Check if there is any lower bound (if lower == 0) and/or upper
8748 * bound (if upper == 0) on the specified dim.
8750 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8751 enum isl_dim_type type, unsigned pos, int lower, int upper)
8753 int i;
8755 if (!bmap)
8756 return -1;
8758 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8760 pos += isl_basic_map_offset(bmap, type);
8762 for (i = 0; i < bmap->n_div; ++i) {
8763 if (isl_int_is_zero(bmap->div[i][0]))
8764 continue;
8765 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8766 return 1;
8769 for (i = 0; i < bmap->n_eq; ++i)
8770 if (!isl_int_is_zero(bmap->eq[i][pos]))
8771 return 1;
8773 for (i = 0; i < bmap->n_ineq; ++i) {
8774 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8775 if (sgn > 0)
8776 lower = 1;
8777 if (sgn < 0)
8778 upper = 1;
8781 return lower && upper;
8784 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8785 enum isl_dim_type type, unsigned pos)
8787 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8790 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8791 enum isl_dim_type type, unsigned pos)
8793 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8796 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8797 enum isl_dim_type type, unsigned pos)
8799 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8802 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8803 enum isl_dim_type type, unsigned pos)
8805 int i;
8807 if (!map)
8808 return -1;
8810 for (i = 0; i < map->n; ++i) {
8811 int bounded;
8812 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8813 if (bounded < 0 || !bounded)
8814 return bounded;
8817 return 1;
8820 /* Return 1 if the specified dim is involved in both an upper bound
8821 * and a lower bound.
8823 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8824 enum isl_dim_type type, unsigned pos)
8826 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8829 static int has_bound(__isl_keep isl_map *map,
8830 enum isl_dim_type type, unsigned pos,
8831 int (*fn)(__isl_keep isl_basic_map *bmap,
8832 enum isl_dim_type type, unsigned pos))
8834 int i;
8836 if (!map)
8837 return -1;
8839 for (i = 0; i < map->n; ++i) {
8840 int bounded;
8841 bounded = fn(map->p[i], type, pos);
8842 if (bounded < 0 || bounded)
8843 return bounded;
8846 return 0;
8849 /* Return 1 if the specified dim is involved in any lower bound.
8851 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8852 enum isl_dim_type type, unsigned pos)
8854 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8857 /* Return 1 if the specified dim is involved in any upper bound.
8859 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8860 enum isl_dim_type type, unsigned pos)
8862 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8865 /* For each of the "n" variables starting at "first", determine
8866 * the sign of the variable and put the results in the first "n"
8867 * elements of the array "signs".
8868 * Sign
8869 * 1 means that the variable is non-negative
8870 * -1 means that the variable is non-positive
8871 * 0 means the variable attains both positive and negative values.
8873 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8874 unsigned first, unsigned n, int *signs)
8876 isl_vec *bound = NULL;
8877 struct isl_tab *tab = NULL;
8878 struct isl_tab_undo *snap;
8879 int i;
8881 if (!bset || !signs)
8882 return -1;
8884 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8885 tab = isl_tab_from_basic_set(bset, 0);
8886 if (!bound || !tab)
8887 goto error;
8889 isl_seq_clr(bound->el, bound->size);
8890 isl_int_set_si(bound->el[0], -1);
8892 snap = isl_tab_snap(tab);
8893 for (i = 0; i < n; ++i) {
8894 int empty;
8896 isl_int_set_si(bound->el[1 + first + i], -1);
8897 if (isl_tab_add_ineq(tab, bound->el) < 0)
8898 goto error;
8899 empty = tab->empty;
8900 isl_int_set_si(bound->el[1 + first + i], 0);
8901 if (isl_tab_rollback(tab, snap) < 0)
8902 goto error;
8904 if (empty) {
8905 signs[i] = 1;
8906 continue;
8909 isl_int_set_si(bound->el[1 + first + i], 1);
8910 if (isl_tab_add_ineq(tab, bound->el) < 0)
8911 goto error;
8912 empty = tab->empty;
8913 isl_int_set_si(bound->el[1 + first + i], 0);
8914 if (isl_tab_rollback(tab, snap) < 0)
8915 goto error;
8917 signs[i] = empty ? -1 : 0;
8920 isl_tab_free(tab);
8921 isl_vec_free(bound);
8922 return 0;
8923 error:
8924 isl_tab_free(tab);
8925 isl_vec_free(bound);
8926 return -1;
8929 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8930 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8932 if (!bset || !signs)
8933 return -1;
8934 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8935 return -1);
8937 first += pos(bset->dim, type) - 1;
8938 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8941 /* Check if the given basic map is obviously single-valued.
8942 * In particular, for each output dimension, check that there is
8943 * an equality that defines the output dimension in terms of
8944 * earlier dimensions.
8946 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8948 int i, j;
8949 unsigned total;
8950 unsigned n_out;
8951 unsigned o_out;
8953 if (!bmap)
8954 return -1;
8956 total = 1 + isl_basic_map_total_dim(bmap);
8957 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8958 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8960 for (i = 0; i < n_out; ++i) {
8961 for (j = 0; j < bmap->n_eq; ++j) {
8962 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8963 continue;
8964 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8965 total - (o_out + i + 1)) == -1)
8966 break;
8968 if (j >= bmap->n_eq)
8969 return 0;
8972 return 1;
8975 /* Check if the given basic map is single-valued.
8976 * We simply compute
8978 * M \circ M^-1
8980 * and check if the result is a subset of the identity mapping.
8982 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
8984 isl_space *space;
8985 isl_basic_map *test;
8986 isl_basic_map *id;
8987 int sv;
8989 sv = isl_basic_map_plain_is_single_valued(bmap);
8990 if (sv < 0 || sv)
8991 return sv;
8993 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
8994 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
8996 space = isl_basic_map_get_space(bmap);
8997 space = isl_space_map_from_set(isl_space_range(space));
8998 id = isl_basic_map_identity(space);
9000 sv = isl_basic_map_is_subset(test, id);
9002 isl_basic_map_free(test);
9003 isl_basic_map_free(id);
9005 return sv;
9008 /* Check if the given map is obviously single-valued.
9010 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9012 if (!map)
9013 return -1;
9014 if (map->n == 0)
9015 return 1;
9016 if (map->n >= 2)
9017 return 0;
9019 return isl_basic_map_plain_is_single_valued(map->p[0]);
9022 /* Check if the given map is single-valued.
9023 * We simply compute
9025 * M \circ M^-1
9027 * and check if the result is a subset of the identity mapping.
9029 int isl_map_is_single_valued(__isl_keep isl_map *map)
9031 isl_space *dim;
9032 isl_map *test;
9033 isl_map *id;
9034 int sv;
9036 sv = isl_map_plain_is_single_valued(map);
9037 if (sv < 0 || sv)
9038 return sv;
9040 test = isl_map_reverse(isl_map_copy(map));
9041 test = isl_map_apply_range(test, isl_map_copy(map));
9043 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9044 id = isl_map_identity(dim);
9046 sv = isl_map_is_subset(test, id);
9048 isl_map_free(test);
9049 isl_map_free(id);
9051 return sv;
9054 int isl_map_is_injective(__isl_keep isl_map *map)
9056 int in;
9058 map = isl_map_copy(map);
9059 map = isl_map_reverse(map);
9060 in = isl_map_is_single_valued(map);
9061 isl_map_free(map);
9063 return in;
9066 /* Check if the given map is obviously injective.
9068 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9070 int in;
9072 map = isl_map_copy(map);
9073 map = isl_map_reverse(map);
9074 in = isl_map_plain_is_single_valued(map);
9075 isl_map_free(map);
9077 return in;
9080 int isl_map_is_bijective(__isl_keep isl_map *map)
9082 int sv;
9084 sv = isl_map_is_single_valued(map);
9085 if (sv < 0 || !sv)
9086 return sv;
9088 return isl_map_is_injective(map);
9091 int isl_set_is_singleton(__isl_keep isl_set *set)
9093 return isl_map_is_single_valued((isl_map *)set);
9096 int isl_map_is_translation(__isl_keep isl_map *map)
9098 int ok;
9099 isl_set *delta;
9101 delta = isl_map_deltas(isl_map_copy(map));
9102 ok = isl_set_is_singleton(delta);
9103 isl_set_free(delta);
9105 return ok;
9108 static int unique(isl_int *p, unsigned pos, unsigned len)
9110 if (isl_seq_first_non_zero(p, pos) != -1)
9111 return 0;
9112 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9113 return 0;
9114 return 1;
9117 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9119 int i, j;
9120 unsigned nvar;
9121 unsigned ovar;
9123 if (!bset)
9124 return -1;
9126 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9127 return 0;
9129 nvar = isl_basic_set_dim(bset, isl_dim_set);
9130 ovar = isl_space_offset(bset->dim, isl_dim_set);
9131 for (j = 0; j < nvar; ++j) {
9132 int lower = 0, upper = 0;
9133 for (i = 0; i < bset->n_eq; ++i) {
9134 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9135 continue;
9136 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9137 return 0;
9138 break;
9140 if (i < bset->n_eq)
9141 continue;
9142 for (i = 0; i < bset->n_ineq; ++i) {
9143 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9144 continue;
9145 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9146 return 0;
9147 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9148 lower = 1;
9149 else
9150 upper = 1;
9152 if (!lower || !upper)
9153 return 0;
9156 return 1;
9159 int isl_set_is_box(__isl_keep isl_set *set)
9161 if (!set)
9162 return -1;
9163 if (set->n != 1)
9164 return 0;
9166 return isl_basic_set_is_box(set->p[0]);
9169 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9171 if (!bset)
9172 return -1;
9174 return isl_space_is_wrapping(bset->dim);
9177 int isl_set_is_wrapping(__isl_keep isl_set *set)
9179 if (!set)
9180 return -1;
9182 return isl_space_is_wrapping(set->dim);
9185 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9187 bmap = isl_basic_map_cow(bmap);
9188 if (!bmap)
9189 return NULL;
9191 bmap->dim = isl_space_wrap(bmap->dim);
9192 if (!bmap->dim)
9193 goto error;
9195 bmap = isl_basic_map_finalize(bmap);
9197 return (isl_basic_set *)bmap;
9198 error:
9199 isl_basic_map_free(bmap);
9200 return NULL;
9203 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9205 int i;
9207 map = isl_map_cow(map);
9208 if (!map)
9209 return NULL;
9211 for (i = 0; i < map->n; ++i) {
9212 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9213 if (!map->p[i])
9214 goto error;
9216 map->dim = isl_space_wrap(map->dim);
9217 if (!map->dim)
9218 goto error;
9220 return (isl_set *)map;
9221 error:
9222 isl_map_free(map);
9223 return NULL;
9226 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9228 bset = isl_basic_set_cow(bset);
9229 if (!bset)
9230 return NULL;
9232 bset->dim = isl_space_unwrap(bset->dim);
9233 if (!bset->dim)
9234 goto error;
9236 bset = isl_basic_set_finalize(bset);
9238 return (isl_basic_map *)bset;
9239 error:
9240 isl_basic_set_free(bset);
9241 return NULL;
9244 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9246 int i;
9248 if (!set)
9249 return NULL;
9251 if (!isl_set_is_wrapping(set))
9252 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9253 goto error);
9255 set = isl_set_cow(set);
9256 if (!set)
9257 return NULL;
9259 for (i = 0; i < set->n; ++i) {
9260 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9261 if (!set->p[i])
9262 goto error;
9265 set->dim = isl_space_unwrap(set->dim);
9266 if (!set->dim)
9267 goto error;
9269 return (isl_map *)set;
9270 error:
9271 isl_set_free(set);
9272 return NULL;
9275 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9276 enum isl_dim_type type)
9278 if (!bmap)
9279 return NULL;
9281 if (!isl_space_is_named_or_nested(bmap->dim, type))
9282 return bmap;
9284 bmap = isl_basic_map_cow(bmap);
9285 if (!bmap)
9286 return NULL;
9288 bmap->dim = isl_space_reset(bmap->dim, type);
9289 if (!bmap->dim)
9290 goto error;
9292 bmap = isl_basic_map_finalize(bmap);
9294 return bmap;
9295 error:
9296 isl_basic_map_free(bmap);
9297 return NULL;
9300 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9301 enum isl_dim_type type)
9303 int i;
9305 if (!map)
9306 return NULL;
9308 if (!isl_space_is_named_or_nested(map->dim, type))
9309 return map;
9311 map = isl_map_cow(map);
9312 if (!map)
9313 return NULL;
9315 for (i = 0; i < map->n; ++i) {
9316 map->p[i] = isl_basic_map_reset(map->p[i], type);
9317 if (!map->p[i])
9318 goto error;
9320 map->dim = isl_space_reset(map->dim, type);
9321 if (!map->dim)
9322 goto error;
9324 return map;
9325 error:
9326 isl_map_free(map);
9327 return NULL;
9330 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9332 if (!bmap)
9333 return NULL;
9335 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9336 return bmap;
9338 bmap = isl_basic_map_cow(bmap);
9339 if (!bmap)
9340 return NULL;
9342 bmap->dim = isl_space_flatten(bmap->dim);
9343 if (!bmap->dim)
9344 goto error;
9346 bmap = isl_basic_map_finalize(bmap);
9348 return bmap;
9349 error:
9350 isl_basic_map_free(bmap);
9351 return NULL;
9354 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9356 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9359 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9360 __isl_take isl_basic_map *bmap)
9362 if (!bmap)
9363 return NULL;
9365 if (!bmap->dim->nested[0])
9366 return bmap;
9368 bmap = isl_basic_map_cow(bmap);
9369 if (!bmap)
9370 return NULL;
9372 bmap->dim = isl_space_flatten_domain(bmap->dim);
9373 if (!bmap->dim)
9374 goto error;
9376 bmap = isl_basic_map_finalize(bmap);
9378 return bmap;
9379 error:
9380 isl_basic_map_free(bmap);
9381 return NULL;
9384 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9385 __isl_take isl_basic_map *bmap)
9387 if (!bmap)
9388 return NULL;
9390 if (!bmap->dim->nested[1])
9391 return bmap;
9393 bmap = isl_basic_map_cow(bmap);
9394 if (!bmap)
9395 return NULL;
9397 bmap->dim = isl_space_flatten_range(bmap->dim);
9398 if (!bmap->dim)
9399 goto error;
9401 bmap = isl_basic_map_finalize(bmap);
9403 return bmap;
9404 error:
9405 isl_basic_map_free(bmap);
9406 return NULL;
9409 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9411 int i;
9413 if (!map)
9414 return NULL;
9416 if (!map->dim->nested[0] && !map->dim->nested[1])
9417 return map;
9419 map = isl_map_cow(map);
9420 if (!map)
9421 return NULL;
9423 for (i = 0; i < map->n; ++i) {
9424 map->p[i] = isl_basic_map_flatten(map->p[i]);
9425 if (!map->p[i])
9426 goto error;
9428 map->dim = isl_space_flatten(map->dim);
9429 if (!map->dim)
9430 goto error;
9432 return map;
9433 error:
9434 isl_map_free(map);
9435 return NULL;
9438 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9440 return (isl_set *)isl_map_flatten((isl_map *)set);
9443 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9445 isl_space *dim, *flat_dim;
9446 isl_map *map;
9448 dim = isl_set_get_space(set);
9449 flat_dim = isl_space_flatten(isl_space_copy(dim));
9450 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9451 map = isl_map_intersect_domain(map, set);
9453 return map;
9456 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9458 int i;
9460 if (!map)
9461 return NULL;
9463 if (!map->dim->nested[0])
9464 return map;
9466 map = isl_map_cow(map);
9467 if (!map)
9468 return NULL;
9470 for (i = 0; i < map->n; ++i) {
9471 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9472 if (!map->p[i])
9473 goto error;
9475 map->dim = isl_space_flatten_domain(map->dim);
9476 if (!map->dim)
9477 goto error;
9479 return map;
9480 error:
9481 isl_map_free(map);
9482 return NULL;
9485 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9487 int i;
9489 if (!map)
9490 return NULL;
9492 if (!map->dim->nested[1])
9493 return map;
9495 map = isl_map_cow(map);
9496 if (!map)
9497 return NULL;
9499 for (i = 0; i < map->n; ++i) {
9500 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9501 if (!map->p[i])
9502 goto error;
9504 map->dim = isl_space_flatten_range(map->dim);
9505 if (!map->dim)
9506 goto error;
9508 return map;
9509 error:
9510 isl_map_free(map);
9511 return NULL;
9514 /* Reorder the dimensions of "bmap" according to the given dim_map
9515 * and set the dimension specification to "dim".
9517 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9518 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9520 isl_basic_map *res;
9521 unsigned flags;
9523 bmap = isl_basic_map_cow(bmap);
9524 if (!bmap || !dim || !dim_map)
9525 goto error;
9527 flags = bmap->flags;
9528 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9529 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9530 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9531 res = isl_basic_map_alloc_space(dim,
9532 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9533 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9534 if (res)
9535 res->flags = flags;
9536 res = isl_basic_map_finalize(res);
9537 return res;
9538 error:
9539 free(dim_map);
9540 isl_basic_map_free(bmap);
9541 isl_space_free(dim);
9542 return NULL;
9545 /* Reorder the dimensions of "map" according to given reordering.
9547 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9548 __isl_take isl_reordering *r)
9550 int i;
9551 struct isl_dim_map *dim_map;
9553 map = isl_map_cow(map);
9554 dim_map = isl_dim_map_from_reordering(r);
9555 if (!map || !r || !dim_map)
9556 goto error;
9558 for (i = 0; i < map->n; ++i) {
9559 struct isl_dim_map *dim_map_i;
9561 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9563 map->p[i] = isl_basic_map_realign(map->p[i],
9564 isl_space_copy(r->dim), dim_map_i);
9566 if (!map->p[i])
9567 goto error;
9570 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9572 isl_reordering_free(r);
9573 free(dim_map);
9574 return map;
9575 error:
9576 free(dim_map);
9577 isl_map_free(map);
9578 isl_reordering_free(r);
9579 return NULL;
9582 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9583 __isl_take isl_reordering *r)
9585 return (isl_set *)isl_map_realign((isl_map *)set, r);
9588 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9589 __isl_take isl_space *model)
9591 isl_ctx *ctx;
9593 if (!map || !model)
9594 goto error;
9596 ctx = isl_space_get_ctx(model);
9597 if (!isl_space_has_named_params(model))
9598 isl_die(ctx, isl_error_invalid,
9599 "model has unnamed parameters", goto error);
9600 if (!isl_space_has_named_params(map->dim))
9601 isl_die(ctx, isl_error_invalid,
9602 "relation has unnamed parameters", goto error);
9603 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9604 isl_reordering *exp;
9606 model = isl_space_drop_dims(model, isl_dim_in,
9607 0, isl_space_dim(model, isl_dim_in));
9608 model = isl_space_drop_dims(model, isl_dim_out,
9609 0, isl_space_dim(model, isl_dim_out));
9610 exp = isl_parameter_alignment_reordering(map->dim, model);
9611 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9612 map = isl_map_realign(map, exp);
9615 isl_space_free(model);
9616 return map;
9617 error:
9618 isl_space_free(model);
9619 isl_map_free(map);
9620 return NULL;
9623 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9624 __isl_take isl_space *model)
9626 return isl_map_align_params(set, model);
9629 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9630 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9631 enum isl_dim_type c2, enum isl_dim_type c3,
9632 enum isl_dim_type c4, enum isl_dim_type c5)
9634 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9635 struct isl_mat *mat;
9636 int i, j, k;
9637 int pos;
9639 if (!bmap)
9640 return NULL;
9641 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9642 isl_basic_map_total_dim(bmap) + 1);
9643 if (!mat)
9644 return NULL;
9645 for (i = 0; i < bmap->n_eq; ++i)
9646 for (j = 0, pos = 0; j < 5; ++j) {
9647 int off = isl_basic_map_offset(bmap, c[j]);
9648 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9649 isl_int_set(mat->row[i][pos],
9650 bmap->eq[i][off + k]);
9651 ++pos;
9655 return mat;
9658 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9659 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9660 enum isl_dim_type c2, enum isl_dim_type c3,
9661 enum isl_dim_type c4, enum isl_dim_type c5)
9663 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9664 struct isl_mat *mat;
9665 int i, j, k;
9666 int pos;
9668 if (!bmap)
9669 return NULL;
9670 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9671 isl_basic_map_total_dim(bmap) + 1);
9672 if (!mat)
9673 return NULL;
9674 for (i = 0; i < bmap->n_ineq; ++i)
9675 for (j = 0, pos = 0; j < 5; ++j) {
9676 int off = isl_basic_map_offset(bmap, c[j]);
9677 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9678 isl_int_set(mat->row[i][pos],
9679 bmap->ineq[i][off + k]);
9680 ++pos;
9684 return mat;
9687 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9688 __isl_take isl_space *dim,
9689 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9690 enum isl_dim_type c2, enum isl_dim_type c3,
9691 enum isl_dim_type c4, enum isl_dim_type c5)
9693 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9694 isl_basic_map *bmap;
9695 unsigned total;
9696 unsigned extra;
9697 int i, j, k, l;
9698 int pos;
9700 if (!dim || !eq || !ineq)
9701 goto error;
9703 if (eq->n_col != ineq->n_col)
9704 isl_die(dim->ctx, isl_error_invalid,
9705 "equalities and inequalities matrices should have "
9706 "same number of columns", goto error);
9708 total = 1 + isl_space_dim(dim, isl_dim_all);
9710 if (eq->n_col < total)
9711 isl_die(dim->ctx, isl_error_invalid,
9712 "number of columns too small", goto error);
9714 extra = eq->n_col - total;
9716 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9717 eq->n_row, ineq->n_row);
9718 if (!bmap)
9719 goto error;
9720 for (i = 0; i < extra; ++i) {
9721 k = isl_basic_map_alloc_div(bmap);
9722 if (k < 0)
9723 goto error;
9724 isl_int_set_si(bmap->div[k][0], 0);
9726 for (i = 0; i < eq->n_row; ++i) {
9727 l = isl_basic_map_alloc_equality(bmap);
9728 if (l < 0)
9729 goto error;
9730 for (j = 0, pos = 0; j < 5; ++j) {
9731 int off = isl_basic_map_offset(bmap, c[j]);
9732 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9733 isl_int_set(bmap->eq[l][off + k],
9734 eq->row[i][pos]);
9735 ++pos;
9739 for (i = 0; i < ineq->n_row; ++i) {
9740 l = isl_basic_map_alloc_inequality(bmap);
9741 if (l < 0)
9742 goto error;
9743 for (j = 0, pos = 0; j < 5; ++j) {
9744 int off = isl_basic_map_offset(bmap, c[j]);
9745 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9746 isl_int_set(bmap->ineq[l][off + k],
9747 ineq->row[i][pos]);
9748 ++pos;
9753 isl_space_free(dim);
9754 isl_mat_free(eq);
9755 isl_mat_free(ineq);
9757 return bmap;
9758 error:
9759 isl_space_free(dim);
9760 isl_mat_free(eq);
9761 isl_mat_free(ineq);
9762 return NULL;
9765 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9766 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9767 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9769 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9770 c1, c2, c3, c4, isl_dim_in);
9773 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9774 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9775 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9777 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9778 c1, c2, c3, c4, isl_dim_in);
9781 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9782 __isl_take isl_space *dim,
9783 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9784 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9786 return (isl_basic_set*)
9787 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9788 c1, c2, c3, c4, isl_dim_in);
9791 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9793 if (!bmap)
9794 return -1;
9796 return isl_space_can_zip(bmap->dim);
9799 int isl_map_can_zip(__isl_keep isl_map *map)
9801 if (!map)
9802 return -1;
9804 return isl_space_can_zip(map->dim);
9807 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9808 * (A -> C) -> (B -> D).
9810 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9812 unsigned pos;
9813 unsigned n1;
9814 unsigned n2;
9816 if (!bmap)
9817 return NULL;
9819 if (!isl_basic_map_can_zip(bmap))
9820 isl_die(bmap->ctx, isl_error_invalid,
9821 "basic map cannot be zipped", goto error);
9822 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9823 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9824 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9825 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9826 bmap = isl_basic_map_cow(bmap);
9827 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9828 if (!bmap)
9829 return NULL;
9830 bmap->dim = isl_space_zip(bmap->dim);
9831 if (!bmap->dim)
9832 goto error;
9833 return bmap;
9834 error:
9835 isl_basic_map_free(bmap);
9836 return NULL;
9839 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9840 * (A -> C) -> (B -> D).
9842 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9844 int i;
9846 if (!map)
9847 return NULL;
9849 if (!isl_map_can_zip(map))
9850 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9851 goto error);
9853 map = isl_map_cow(map);
9854 if (!map)
9855 return NULL;
9857 for (i = 0; i < map->n; ++i) {
9858 map->p[i] = isl_basic_map_zip(map->p[i]);
9859 if (!map->p[i])
9860 goto error;
9863 map->dim = isl_space_zip(map->dim);
9864 if (!map->dim)
9865 goto error;
9867 return map;
9868 error:
9869 isl_map_free(map);
9870 return NULL;
9873 /* Can we apply isl_basic_map_curry to "bmap"?
9874 * That is, does it have a nested relation in its domain?
9876 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
9878 if (!bmap)
9879 return -1;
9881 return isl_space_can_curry(bmap->dim);
9884 /* Can we apply isl_map_curry to "map"?
9885 * That is, does it have a nested relation in its domain?
9887 int isl_map_can_curry(__isl_keep isl_map *map)
9889 if (!map)
9890 return -1;
9892 return isl_space_can_curry(map->dim);
9895 /* Given a basic map (A -> B) -> C, return the corresponding basic map
9896 * A -> (B -> C).
9898 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
9901 if (!bmap)
9902 return NULL;
9904 if (!isl_basic_map_can_curry(bmap))
9905 isl_die(bmap->ctx, isl_error_invalid,
9906 "basic map cannot be curried", goto error);
9907 bmap->dim = isl_space_curry(bmap->dim);
9908 if (!bmap->dim)
9909 goto error;
9910 return bmap;
9911 error:
9912 isl_basic_map_free(bmap);
9913 return NULL;
9916 /* Given a map (A -> B) -> C, return the corresponding map
9917 * A -> (B -> C).
9919 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
9921 int i;
9923 if (!map)
9924 return NULL;
9926 if (!isl_map_can_curry(map))
9927 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
9928 goto error);
9930 map = isl_map_cow(map);
9931 if (!map)
9932 return NULL;
9934 for (i = 0; i < map->n; ++i) {
9935 map->p[i] = isl_basic_map_curry(map->p[i]);
9936 if (!map->p[i])
9937 goto error;
9940 map->dim = isl_space_curry(map->dim);
9941 if (!map->dim)
9942 goto error;
9944 return map;
9945 error:
9946 isl_map_free(map);
9947 return NULL;
9950 /* Construct a basic map mapping the domain of the affine expression
9951 * to a one-dimensional range prescribed by the affine expression.
9953 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9955 int k;
9956 int pos;
9957 isl_local_space *ls;
9958 isl_basic_map *bmap;
9960 if (!aff)
9961 return NULL;
9963 ls = isl_aff_get_local_space(aff);
9964 bmap = isl_basic_map_from_local_space(ls);
9965 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9966 k = isl_basic_map_alloc_equality(bmap);
9967 if (k < 0)
9968 goto error;
9970 pos = isl_basic_map_offset(bmap, isl_dim_out);
9971 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9972 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9973 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9974 aff->v->size - (pos + 1));
9976 isl_aff_free(aff);
9977 bmap = isl_basic_map_finalize(bmap);
9978 return bmap;
9979 error:
9980 isl_aff_free(aff);
9981 isl_basic_map_free(bmap);
9982 return NULL;
9985 /* Construct a map mapping the domain of the affine expression
9986 * to a one-dimensional range prescribed by the affine expression.
9988 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
9990 isl_basic_map *bmap;
9992 bmap = isl_basic_map_from_aff(aff);
9993 return isl_map_from_basic_map(bmap);
9996 /* Construct a basic map mapping the domain the multi-affine expression
9997 * to its range, with each dimension in the range equated to the
9998 * corresponding affine expression.
10000 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10001 __isl_take isl_multi_aff *maff)
10003 int i;
10004 isl_space *space;
10005 isl_basic_map *bmap;
10007 if (!maff)
10008 return NULL;
10010 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10011 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10012 "invalid space", return isl_multi_aff_free(maff));
10014 space = isl_space_domain(isl_multi_aff_get_space(maff));
10015 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10017 for (i = 0; i < maff->n; ++i) {
10018 isl_aff *aff;
10019 isl_basic_map *bmap_i;
10021 aff = isl_aff_copy(maff->p[i]);
10022 bmap_i = isl_basic_map_from_aff(aff);
10024 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10027 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10029 isl_multi_aff_free(maff);
10030 return bmap;
10033 /* Construct a map mapping the domain the multi-affine expression
10034 * to its range, with each dimension in the range equated to the
10035 * corresponding affine expression.
10037 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10039 isl_basic_map *bmap;
10041 bmap = isl_basic_map_from_multi_aff(maff);
10042 return isl_map_from_basic_map(bmap);
10045 /* Construct a basic map mapping a domain in the given space to
10046 * to an n-dimensional range, with n the number of elements in the list,
10047 * where each coordinate in the range is prescribed by the
10048 * corresponding affine expression.
10049 * The domains of all affine expressions in the list are assumed to match
10050 * domain_dim.
10052 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10053 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10055 int i;
10056 isl_space *dim;
10057 isl_basic_map *bmap;
10059 if (!list)
10060 return NULL;
10062 dim = isl_space_from_domain(domain_dim);
10063 bmap = isl_basic_map_universe(dim);
10065 for (i = 0; i < list->n; ++i) {
10066 isl_aff *aff;
10067 isl_basic_map *bmap_i;
10069 aff = isl_aff_copy(list->p[i]);
10070 bmap_i = isl_basic_map_from_aff(aff);
10072 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10075 isl_aff_list_free(list);
10076 return bmap;
10079 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10080 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10082 return isl_map_equate(set, type1, pos1, type2, pos2);
10085 /* Construct a basic map where the given dimensions are equal to each other.
10087 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10088 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10090 isl_basic_map *bmap = NULL;
10091 int i;
10093 if (!space)
10094 return NULL;
10096 if (pos1 >= isl_space_dim(space, type1))
10097 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10098 "index out of bounds", goto error);
10099 if (pos2 >= isl_space_dim(space, type2))
10100 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10101 "index out of bounds", goto error);
10103 if (type1 == type2 && pos1 == pos2)
10104 return isl_basic_map_universe(space);
10106 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10107 i = isl_basic_map_alloc_equality(bmap);
10108 if (i < 0)
10109 goto error;
10110 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10111 pos1 += isl_basic_map_offset(bmap, type1);
10112 pos2 += isl_basic_map_offset(bmap, type2);
10113 isl_int_set_si(bmap->eq[i][pos1], -1);
10114 isl_int_set_si(bmap->eq[i][pos2], 1);
10115 bmap = isl_basic_map_finalize(bmap);
10116 isl_space_free(space);
10117 return bmap;
10118 error:
10119 isl_space_free(space);
10120 isl_basic_map_free(bmap);
10121 return NULL;
10124 /* Add a constraint imposing that the given two dimensions are equal.
10126 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10127 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10129 isl_basic_map *eq;
10131 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10133 bmap = isl_basic_map_intersect(bmap, eq);
10135 return bmap;
10138 /* Add a constraint imposing that the given two dimensions are equal.
10140 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10141 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10143 isl_basic_map *bmap;
10145 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10147 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10149 return map;
10152 /* Add a constraint imposing that the given two dimensions have opposite values.
10154 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10155 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10157 isl_basic_map *bmap = NULL;
10158 int i;
10160 if (!map)
10161 return NULL;
10163 if (pos1 >= isl_map_dim(map, type1))
10164 isl_die(map->ctx, isl_error_invalid,
10165 "index out of bounds", goto error);
10166 if (pos2 >= isl_map_dim(map, type2))
10167 isl_die(map->ctx, isl_error_invalid,
10168 "index out of bounds", goto error);
10170 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10171 i = isl_basic_map_alloc_equality(bmap);
10172 if (i < 0)
10173 goto error;
10174 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10175 pos1 += isl_basic_map_offset(bmap, type1);
10176 pos2 += isl_basic_map_offset(bmap, type2);
10177 isl_int_set_si(bmap->eq[i][pos1], 1);
10178 isl_int_set_si(bmap->eq[i][pos2], 1);
10179 bmap = isl_basic_map_finalize(bmap);
10181 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10183 return map;
10184 error:
10185 isl_basic_map_free(bmap);
10186 isl_map_free(map);
10187 return NULL;
10190 /* Add a constraint imposing that the value of the first dimension is
10191 * greater than that of the second.
10193 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10194 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10196 isl_basic_map *bmap = NULL;
10197 int i;
10199 if (!map)
10200 return NULL;
10202 if (pos1 >= isl_map_dim(map, type1))
10203 isl_die(map->ctx, isl_error_invalid,
10204 "index out of bounds", goto error);
10205 if (pos2 >= isl_map_dim(map, type2))
10206 isl_die(map->ctx, isl_error_invalid,
10207 "index out of bounds", goto error);
10209 if (type1 == type2 && pos1 == pos2) {
10210 isl_space *space = isl_map_get_space(map);
10211 isl_map_free(map);
10212 return isl_map_empty(space);
10215 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10216 i = isl_basic_map_alloc_inequality(bmap);
10217 if (i < 0)
10218 goto error;
10219 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10220 pos1 += isl_basic_map_offset(bmap, type1);
10221 pos2 += isl_basic_map_offset(bmap, type2);
10222 isl_int_set_si(bmap->ineq[i][pos1], 1);
10223 isl_int_set_si(bmap->ineq[i][pos2], -1);
10224 isl_int_set_si(bmap->ineq[i][0], -1);
10225 bmap = isl_basic_map_finalize(bmap);
10227 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10229 return map;
10230 error:
10231 isl_basic_map_free(bmap);
10232 isl_map_free(map);
10233 return NULL;
10236 /* Add a constraint imposing that the value of the first dimension is
10237 * smaller than that of the second.
10239 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10240 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10242 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10245 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10246 int pos)
10248 isl_aff *div;
10249 isl_local_space *ls;
10251 if (!bmap)
10252 return NULL;
10254 if (!isl_basic_map_divs_known(bmap))
10255 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10256 "some divs are unknown", return NULL);
10258 ls = isl_basic_map_get_local_space(bmap);
10259 div = isl_local_space_get_div(ls, pos);
10260 isl_local_space_free(ls);
10262 return div;
10265 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10266 int pos)
10268 return isl_basic_map_get_div(bset, pos);
10271 /* Plug in "subs" for dimension "type", "pos" of "bset".
10273 * Let i be the dimension to replace and let "subs" be of the form
10275 * f/d
10277 * Any integer division with a non-zero coefficient for i,
10279 * floor((a i + g)/m)
10281 * is replaced by
10283 * floor((a f + d g)/(m d))
10285 * Constraints of the form
10287 * a i + g
10289 * are replaced by
10291 * a f + d g
10293 __isl_give isl_basic_set *isl_basic_set_substitute(
10294 __isl_take isl_basic_set *bset,
10295 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10297 int i;
10298 isl_int v;
10299 isl_ctx *ctx;
10301 if (bset && isl_basic_set_plain_is_empty(bset))
10302 return bset;
10304 bset = isl_basic_set_cow(bset);
10305 if (!bset || !subs)
10306 goto error;
10308 ctx = isl_basic_set_get_ctx(bset);
10309 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10310 isl_die(ctx, isl_error_invalid,
10311 "spaces don't match", goto error);
10312 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10313 isl_die(ctx, isl_error_unsupported,
10314 "cannot handle divs yet", goto error);
10316 pos += isl_basic_set_offset(bset, type);
10318 isl_int_init(v);
10320 for (i = 0; i < bset->n_eq; ++i) {
10321 if (isl_int_is_zero(bset->eq[i][pos]))
10322 continue;
10323 isl_int_set(v, bset->eq[i][pos]);
10324 isl_int_set_si(bset->eq[i][pos], 0);
10325 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10326 v, subs->v->el + 1, subs->v->size - 1);
10329 for (i = 0; i < bset->n_ineq; ++i) {
10330 if (isl_int_is_zero(bset->ineq[i][pos]))
10331 continue;
10332 isl_int_set(v, bset->ineq[i][pos]);
10333 isl_int_set_si(bset->ineq[i][pos], 0);
10334 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10335 v, subs->v->el + 1, subs->v->size - 1);
10338 for (i = 0; i < bset->n_div; ++i) {
10339 if (isl_int_is_zero(bset->div[i][1 + pos]))
10340 continue;
10341 isl_int_set(v, bset->div[i][1 + pos]);
10342 isl_int_set_si(bset->div[i][1 + pos], 0);
10343 isl_seq_combine(bset->div[i] + 1,
10344 subs->v->el[0], bset->div[i] + 1,
10345 v, subs->v->el + 1, subs->v->size - 1);
10346 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10349 isl_int_clear(v);
10351 bset = isl_basic_set_simplify(bset);
10352 return isl_basic_set_finalize(bset);
10353 error:
10354 isl_basic_set_free(bset);
10355 return NULL;
10358 /* Plug in "subs" for dimension "type", "pos" of "set".
10360 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10361 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10363 int i;
10365 if (set && isl_set_plain_is_empty(set))
10366 return set;
10368 set = isl_set_cow(set);
10369 if (!set || !subs)
10370 goto error;
10372 for (i = set->n - 1; i >= 0; --i) {
10373 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10374 if (remove_if_empty(set, i) < 0)
10375 goto error;
10378 return set;
10379 error:
10380 isl_set_free(set);
10381 return NULL;