isl_tab_pip.c: fix typo in comment
[isl.git] / isl_map.c
blob613dbb32e9d973efd914bfd85aa2eeebaab1bb35
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>
34 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
36 switch (type) {
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
41 default: return 0;
45 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
51 default: return 0;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
56 enum isl_dim_type type)
58 if (!bmap)
59 return 0;
60 switch (type) {
61 case isl_dim_cst: return 1;
62 case isl_dim_param:
63 case isl_dim_in:
64 case isl_dim_out: return isl_space_dim(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
67 default: return 0;
71 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 isl_space *dim = bmap->dim;
85 switch (type) {
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
91 default: return 0;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim(bset, type);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
127 unsigned isl_set_n_dim(__isl_keep isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map ? map->dim->n_in : 0;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map ? map->dim->n_out : 0;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map ? map->dim->nparam : 0;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
179 int m;
180 if (!map || !set)
181 return -1;
182 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183 if (m < 0 || !m)
184 return m;
185 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
191 int m;
192 if (!bmap || !bset)
193 return -1;
194 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
202 int m;
203 if (!map || !set)
204 return -1;
205 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 int m;
215 if (!bmap || !bset)
216 return -1;
217 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
245 if (!bmap)
246 return NULL;
247 return isl_space_copy(bmap->dim);
250 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
252 if (!bset)
253 return NULL;
254 return isl_space_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
260 int i;
261 isl_local_space *ls;
262 unsigned total;
264 if (!bmap)
265 return NULL;
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
269 if (!ls)
270 return NULL;
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
275 return ls;
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
287 int i;
288 int n_div;
289 isl_basic_map *bmap;
291 if (!ls)
292 return NULL;
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
300 goto error;
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305 goto error;
308 isl_local_space_free(ls);
309 return bmap;
310 error:
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
324 if (!map)
325 return NULL;
326 return isl_space_copy(map->dim);
329 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
331 if (!set)
332 return NULL;
333 return isl_space_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
340 if (!bmap)
341 return NULL;
342 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
343 if (!bmap->dim)
344 goto error;
345 bmap = isl_basic_map_finalize(bmap);
346 return bmap;
347 error:
348 isl_basic_map_free(bmap);
349 return NULL;
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
367 int i;
369 map = isl_map_cow(map);
370 if (!map)
371 return NULL;
373 map->dim = isl_space_set_tuple_name(map->dim, type, s);
374 if (!map->dim)
375 goto error;
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
379 if (!map->p[i])
380 goto error;
383 return map;
384 error:
385 isl_map_free(map);
386 return NULL;
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
396 const char *s)
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
405 if (!map)
406 return isl_id_free(id);
408 map->dim = isl_space_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_space(map, isl_space_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
423 if (!map)
424 return NULL;
426 map->dim = isl_space_reset_tuple_id(map->dim, type);
428 return isl_map_reset_space(map, isl_space_copy(map->dim));
431 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
433 return isl_map_reset_tuple_id(set, isl_dim_set);
436 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
438 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
441 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
442 enum isl_dim_type type)
444 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
447 int isl_set_has_tuple_id(__isl_keep isl_set *set)
449 return isl_map_has_tuple_id(set, isl_dim_set);
452 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
454 return isl_map_get_tuple_id(set, isl_dim_set);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
459 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
464 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
468 enum isl_dim_type type, unsigned pos)
470 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
474 enum isl_dim_type type, unsigned pos)
476 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
479 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
480 enum isl_dim_type type, unsigned pos)
482 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
485 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
486 enum isl_dim_type type, unsigned pos)
488 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
491 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map *bmap,
493 enum isl_dim_type type, unsigned pos, const char *s)
495 if (!bmap)
496 return NULL;
497 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
498 if (!bmap->dim)
499 goto error;
500 return bmap;
501 error:
502 isl_basic_map_free(bmap);
503 return NULL;
506 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
507 enum isl_dim_type type, unsigned pos, const char *s)
509 int i;
511 if (!map)
512 return NULL;
514 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
515 if (!map->dim)
516 goto error;
518 for (i = 0; i < map->n; ++i) {
519 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
520 if (!map->p[i])
521 goto error;
524 return map;
525 error:
526 isl_map_free(map);
527 return NULL;
530 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set *bset,
532 enum isl_dim_type type, unsigned pos, const char *s)
534 return (isl_basic_set *)isl_basic_map_set_dim_name(
535 (isl_basic_map *)bset, type, pos, s);
538 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
544 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
545 enum isl_dim_type type, unsigned pos)
547 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
550 int isl_map_has_dim_id(__isl_keep isl_map *map,
551 enum isl_dim_type type, unsigned pos)
553 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
556 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
557 enum isl_dim_type type, unsigned pos)
559 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
562 int isl_set_has_dim_id(__isl_keep isl_set *set,
563 enum isl_dim_type type, unsigned pos)
565 return isl_map_has_dim_id(set, type, pos);
568 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
569 enum isl_dim_type type, unsigned pos)
571 return isl_map_get_dim_id(set, type, pos);
574 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
575 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
577 map = isl_map_cow(map);
578 if (!map)
579 return isl_id_free(id);
581 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
583 return isl_map_reset_space(map, isl_space_copy(map->dim));
586 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
587 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
589 return isl_map_set_dim_id(set, type, pos, id);
592 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
593 __isl_keep isl_id *id)
595 if (!map)
596 return -1;
597 return isl_space_find_dim_by_id(map->dim, type, id);
600 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
601 __isl_keep isl_id *id)
603 return isl_map_find_dim_by_id(set, type, id);
606 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
607 const char *name)
609 if (!map)
610 return -1;
611 return isl_space_find_dim_by_name(map->dim, type, name);
614 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
616 if (!bmap)
617 return -1;
618 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
621 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
623 return isl_basic_map_is_rational(bset);
626 /* Is this basic set a parameter domain?
628 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
630 if (!bset)
631 return -1;
632 return isl_space_is_params(bset->dim);
635 /* Is this set a parameter domain?
637 int isl_set_is_params(__isl_keep isl_set *set)
639 if (!set)
640 return -1;
641 return isl_space_is_params(set->dim);
644 /* Is this map actually a parameter domain?
645 * Users should never call this function. Outside of isl,
646 * a map can never be a parameter domain.
648 int isl_map_is_params(__isl_keep isl_map *map)
650 if (!map)
651 return -1;
652 return isl_space_is_params(map->dim);
655 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
656 struct isl_basic_map *bmap, unsigned extra,
657 unsigned n_eq, unsigned n_ineq)
659 int i;
660 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
662 bmap->ctx = ctx;
663 isl_ctx_ref(ctx);
665 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
666 if (isl_blk_is_error(bmap->block))
667 goto error;
669 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
670 if (!bmap->ineq)
671 goto error;
673 if (extra == 0) {
674 bmap->block2 = isl_blk_empty();
675 bmap->div = NULL;
676 } else {
677 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
678 if (isl_blk_is_error(bmap->block2))
679 goto error;
681 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
682 if (!bmap->div)
683 goto error;
686 for (i = 0; i < n_ineq + n_eq; ++i)
687 bmap->ineq[i] = bmap->block.data + i * row_size;
689 for (i = 0; i < extra; ++i)
690 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
692 bmap->ref = 1;
693 bmap->flags = 0;
694 bmap->c_size = n_eq + n_ineq;
695 bmap->eq = bmap->ineq + n_ineq;
696 bmap->extra = extra;
697 bmap->n_eq = 0;
698 bmap->n_ineq = 0;
699 bmap->n_div = 0;
700 bmap->sample = NULL;
702 return bmap;
703 error:
704 isl_basic_map_free(bmap);
705 return NULL;
708 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
709 unsigned nparam, unsigned dim, unsigned extra,
710 unsigned n_eq, unsigned n_ineq)
712 struct isl_basic_map *bmap;
713 isl_space *space;
715 space = isl_space_set_alloc(ctx, nparam, dim);
716 if (!space)
717 return NULL;
719 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
720 return (struct isl_basic_set *)bmap;
723 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
724 unsigned extra, unsigned n_eq, unsigned n_ineq)
726 struct isl_basic_map *bmap;
727 if (!dim)
728 return NULL;
729 isl_assert(dim->ctx, dim->n_in == 0, goto error);
730 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
731 return (struct isl_basic_set *)bmap;
732 error:
733 isl_space_free(dim);
734 return NULL;
737 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
738 unsigned extra, unsigned n_eq, unsigned n_ineq)
740 struct isl_basic_map *bmap;
742 if (!dim)
743 return NULL;
744 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
745 if (!bmap)
746 goto error;
747 bmap->dim = dim;
749 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
750 error:
751 isl_space_free(dim);
752 return NULL;
755 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
756 unsigned nparam, unsigned in, unsigned out, unsigned extra,
757 unsigned n_eq, unsigned n_ineq)
759 struct isl_basic_map *bmap;
760 isl_space *dim;
762 dim = isl_space_alloc(ctx, nparam, in, out);
763 if (!dim)
764 return NULL;
766 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
767 return bmap;
770 static void dup_constraints(
771 struct isl_basic_map *dst, struct isl_basic_map *src)
773 int i;
774 unsigned total = isl_basic_map_total_dim(src);
776 for (i = 0; i < src->n_eq; ++i) {
777 int j = isl_basic_map_alloc_equality(dst);
778 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
781 for (i = 0; i < src->n_ineq; ++i) {
782 int j = isl_basic_map_alloc_inequality(dst);
783 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
786 for (i = 0; i < src->n_div; ++i) {
787 int j = isl_basic_map_alloc_div(dst);
788 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
790 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
793 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
795 struct isl_basic_map *dup;
797 if (!bmap)
798 return NULL;
799 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
800 bmap->n_div, bmap->n_eq, bmap->n_ineq);
801 if (!dup)
802 return NULL;
803 dup_constraints(dup, bmap);
804 dup->flags = bmap->flags;
805 dup->sample = isl_vec_copy(bmap->sample);
806 return dup;
809 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
811 struct isl_basic_map *dup;
813 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
814 return (struct isl_basic_set *)dup;
817 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
819 if (!bset)
820 return NULL;
822 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
823 bset->ref++;
824 return bset;
826 return isl_basic_set_dup(bset);
829 struct isl_set *isl_set_copy(struct isl_set *set)
831 if (!set)
832 return NULL;
834 set->ref++;
835 return set;
838 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
840 if (!bmap)
841 return NULL;
843 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
844 bmap->ref++;
845 return bmap;
847 bmap = isl_basic_map_dup(bmap);
848 if (bmap)
849 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
850 return bmap;
853 struct isl_map *isl_map_copy(struct isl_map *map)
855 if (!map)
856 return NULL;
858 map->ref++;
859 return map;
862 void isl_basic_map_free(struct isl_basic_map *bmap)
864 if (!bmap)
865 return;
867 if (--bmap->ref > 0)
868 return;
870 isl_ctx_deref(bmap->ctx);
871 free(bmap->div);
872 isl_blk_free(bmap->ctx, bmap->block2);
873 free(bmap->ineq);
874 isl_blk_free(bmap->ctx, bmap->block);
875 isl_vec_free(bmap->sample);
876 isl_space_free(bmap->dim);
877 free(bmap);
880 void isl_basic_set_free(struct isl_basic_set *bset)
882 isl_basic_map_free((struct isl_basic_map *)bset);
885 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
887 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
890 __isl_give isl_map *isl_map_align_params_map_map_and(
891 __isl_take isl_map *map1, __isl_take isl_map *map2,
892 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
893 __isl_take isl_map *map2))
895 if (!map1 || !map2)
896 goto error;
897 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
898 return fn(map1, map2);
899 if (!isl_space_has_named_params(map1->dim) ||
900 !isl_space_has_named_params(map2->dim))
901 isl_die(map1->ctx, isl_error_invalid,
902 "unaligned unnamed parameters", goto error);
903 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
904 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
905 return fn(map1, map2);
906 error:
907 isl_map_free(map1);
908 isl_map_free(map2);
909 return NULL;
912 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
913 __isl_keep isl_map *map2,
914 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
916 int r;
918 if (!map1 || !map2)
919 return -1;
920 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
921 return fn(map1, map2);
922 if (!isl_space_has_named_params(map1->dim) ||
923 !isl_space_has_named_params(map2->dim))
924 isl_die(map1->ctx, isl_error_invalid,
925 "unaligned unnamed parameters", return -1);
926 map1 = isl_map_copy(map1);
927 map2 = isl_map_copy(map2);
928 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
929 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
930 r = fn(map1, map2);
931 isl_map_free(map1);
932 isl_map_free(map2);
933 return r;
936 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
938 struct isl_ctx *ctx;
939 if (!bmap)
940 return -1;
941 ctx = bmap->ctx;
942 isl_assert(ctx, room_for_con(bmap, 1), return -1);
943 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
944 return -1);
945 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
946 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
950 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
951 isl_int *t;
952 int j = isl_basic_map_alloc_inequality(bmap);
953 if (j < 0)
954 return -1;
955 t = bmap->ineq[j];
956 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
957 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
958 bmap->eq[-1] = t;
959 bmap->n_eq++;
960 bmap->n_ineq--;
961 bmap->eq--;
962 return 0;
964 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
965 bmap->extra - bmap->n_div);
966 return bmap->n_eq++;
969 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
971 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
974 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
976 if (!bmap)
977 return -1;
978 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
979 bmap->n_eq -= n;
980 return 0;
983 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
985 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
988 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
990 isl_int *t;
991 if (!bmap)
992 return -1;
993 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
995 if (pos != bmap->n_eq - 1) {
996 t = bmap->eq[pos];
997 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
998 bmap->eq[bmap->n_eq - 1] = t;
1000 bmap->n_eq--;
1001 return 0;
1004 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1006 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1009 void isl_basic_map_inequality_to_equality(
1010 struct isl_basic_map *bmap, unsigned pos)
1012 isl_int *t;
1014 t = bmap->ineq[pos];
1015 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1016 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1017 bmap->eq[-1] = t;
1018 bmap->n_eq++;
1019 bmap->n_ineq--;
1020 bmap->eq--;
1021 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1022 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1023 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1027 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1029 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1032 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1034 struct isl_ctx *ctx;
1035 if (!bmap)
1036 return -1;
1037 ctx = bmap->ctx;
1038 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1039 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1040 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1043 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1044 1 + isl_basic_map_total_dim(bmap),
1045 bmap->extra - bmap->n_div);
1046 return bmap->n_ineq++;
1049 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1051 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1054 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1056 if (!bmap)
1057 return -1;
1058 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1059 bmap->n_ineq -= n;
1060 return 0;
1063 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1065 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1068 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1070 isl_int *t;
1071 if (!bmap)
1072 return -1;
1073 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1075 if (pos != bmap->n_ineq - 1) {
1076 t = bmap->ineq[pos];
1077 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1078 bmap->ineq[bmap->n_ineq - 1] = t;
1079 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1081 bmap->n_ineq--;
1082 return 0;
1085 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1087 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1090 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1091 isl_int *eq)
1093 int k;
1095 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1096 if (!bmap)
1097 return NULL;
1098 k = isl_basic_map_alloc_equality(bmap);
1099 if (k < 0)
1100 goto error;
1101 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1102 return bmap;
1103 error:
1104 isl_basic_map_free(bmap);
1105 return NULL;
1108 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1109 isl_int *eq)
1111 return (isl_basic_set *)
1112 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1115 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1116 isl_int *ineq)
1118 int k;
1120 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1121 if (!bmap)
1122 return NULL;
1123 k = isl_basic_map_alloc_inequality(bmap);
1124 if (k < 0)
1125 goto error;
1126 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1127 return bmap;
1128 error:
1129 isl_basic_map_free(bmap);
1130 return NULL;
1133 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1134 isl_int *ineq)
1136 return (isl_basic_set *)
1137 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1140 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1142 if (!bmap)
1143 return -1;
1144 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1145 isl_seq_clr(bmap->div[bmap->n_div] +
1146 1 + 1 + isl_basic_map_total_dim(bmap),
1147 bmap->extra - bmap->n_div);
1148 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1149 return bmap->n_div++;
1152 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1154 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1157 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1159 if (!bmap)
1160 return -1;
1161 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1162 bmap->n_div -= n;
1163 return 0;
1166 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1168 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1171 /* Copy constraint from src to dst, putting the vars of src at offset
1172 * dim_off in dst and the divs of src at offset div_off in dst.
1173 * If both sets are actually map, then dim_off applies to the input
1174 * variables.
1176 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1177 struct isl_basic_map *src_map, isl_int *src,
1178 unsigned in_off, unsigned out_off, unsigned div_off)
1180 unsigned src_nparam = isl_basic_map_n_param(src_map);
1181 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1182 unsigned src_in = isl_basic_map_n_in(src_map);
1183 unsigned dst_in = isl_basic_map_n_in(dst_map);
1184 unsigned src_out = isl_basic_map_n_out(src_map);
1185 unsigned dst_out = isl_basic_map_n_out(dst_map);
1186 isl_int_set(dst[0], src[0]);
1187 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1188 if (dst_nparam > src_nparam)
1189 isl_seq_clr(dst+1+src_nparam,
1190 dst_nparam - src_nparam);
1191 isl_seq_clr(dst+1+dst_nparam, in_off);
1192 isl_seq_cpy(dst+1+dst_nparam+in_off,
1193 src+1+src_nparam,
1194 isl_min(dst_in-in_off, src_in));
1195 if (dst_in-in_off > src_in)
1196 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1197 dst_in - in_off - src_in);
1198 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1199 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1200 src+1+src_nparam+src_in,
1201 isl_min(dst_out-out_off, src_out));
1202 if (dst_out-out_off > src_out)
1203 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1204 dst_out - out_off - src_out);
1205 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1206 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1207 src+1+src_nparam+src_in+src_out,
1208 isl_min(dst_map->extra-div_off, src_map->n_div));
1209 if (dst_map->n_div-div_off > src_map->n_div)
1210 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1211 div_off+src_map->n_div,
1212 dst_map->n_div - div_off - src_map->n_div);
1215 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1216 struct isl_basic_map *src_map, isl_int *src,
1217 unsigned in_off, unsigned out_off, unsigned div_off)
1219 isl_int_set(dst[0], src[0]);
1220 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1223 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1224 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1226 int i;
1227 unsigned div_off;
1229 if (!bmap1 || !bmap2)
1230 goto error;
1232 div_off = bmap1->n_div;
1234 for (i = 0; i < bmap2->n_eq; ++i) {
1235 int i1 = isl_basic_map_alloc_equality(bmap1);
1236 if (i1 < 0)
1237 goto error;
1238 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1239 i_pos, o_pos, div_off);
1242 for (i = 0; i < bmap2->n_ineq; ++i) {
1243 int i1 = isl_basic_map_alloc_inequality(bmap1);
1244 if (i1 < 0)
1245 goto error;
1246 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1247 i_pos, o_pos, div_off);
1250 for (i = 0; i < bmap2->n_div; ++i) {
1251 int i1 = isl_basic_map_alloc_div(bmap1);
1252 if (i1 < 0)
1253 goto error;
1254 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1255 i_pos, o_pos, div_off);
1258 isl_basic_map_free(bmap2);
1260 return bmap1;
1262 error:
1263 isl_basic_map_free(bmap1);
1264 isl_basic_map_free(bmap2);
1265 return NULL;
1268 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1269 struct isl_basic_set *bset2, unsigned pos)
1271 return (struct isl_basic_set *)
1272 add_constraints((struct isl_basic_map *)bset1,
1273 (struct isl_basic_map *)bset2, 0, pos);
1276 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1277 __isl_take isl_space *dim, unsigned extra,
1278 unsigned n_eq, unsigned n_ineq)
1280 struct isl_basic_map *ext;
1281 unsigned flags;
1282 int dims_ok;
1284 if (!dim)
1285 goto error;
1287 if (!base)
1288 goto error;
1290 dims_ok = isl_space_is_equal(base->dim, dim) &&
1291 base->extra >= base->n_div + extra;
1293 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1294 room_for_ineq(base, n_ineq)) {
1295 isl_space_free(dim);
1296 return base;
1299 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1300 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1301 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1302 extra += base->extra;
1303 n_eq += base->n_eq;
1304 n_ineq += base->n_ineq;
1306 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1307 dim = NULL;
1308 if (!ext)
1309 goto error;
1311 if (dims_ok)
1312 ext->sample = isl_vec_copy(base->sample);
1313 flags = base->flags;
1314 ext = add_constraints(ext, base, 0, 0);
1315 if (ext) {
1316 ext->flags = flags;
1317 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1320 return ext;
1322 error:
1323 isl_space_free(dim);
1324 isl_basic_map_free(base);
1325 return NULL;
1328 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1329 __isl_take isl_space *dim, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 return (struct isl_basic_set *)
1333 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1334 extra, n_eq, n_ineq);
1337 struct isl_basic_map *isl_basic_map_extend_constraints(
1338 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1340 if (!base)
1341 return NULL;
1342 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1343 0, n_eq, n_ineq);
1346 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1347 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1348 unsigned n_eq, unsigned n_ineq)
1350 struct isl_basic_map *bmap;
1351 isl_space *dim;
1353 if (!base)
1354 return NULL;
1355 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1356 if (!dim)
1357 goto error;
1359 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1360 return bmap;
1361 error:
1362 isl_basic_map_free(base);
1363 return NULL;
1366 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1367 unsigned nparam, unsigned dim, unsigned extra,
1368 unsigned n_eq, unsigned n_ineq)
1370 return (struct isl_basic_set *)
1371 isl_basic_map_extend((struct isl_basic_map *)base,
1372 nparam, 0, dim, extra, n_eq, n_ineq);
1375 struct isl_basic_set *isl_basic_set_extend_constraints(
1376 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1378 return (struct isl_basic_set *)
1379 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1380 n_eq, n_ineq);
1383 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1385 return (struct isl_basic_set *)
1386 isl_basic_map_cow((struct isl_basic_map *)bset);
1389 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1391 if (!bmap)
1392 return NULL;
1394 if (bmap->ref > 1) {
1395 bmap->ref--;
1396 bmap = isl_basic_map_dup(bmap);
1398 if (bmap)
1399 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1400 return bmap;
1403 struct isl_set *isl_set_cow(struct isl_set *set)
1405 if (!set)
1406 return NULL;
1408 if (set->ref == 1)
1409 return set;
1410 set->ref--;
1411 return isl_set_dup(set);
1414 struct isl_map *isl_map_cow(struct isl_map *map)
1416 if (!map)
1417 return NULL;
1419 if (map->ref == 1)
1420 return map;
1421 map->ref--;
1422 return isl_map_dup(map);
1425 static void swap_vars(struct isl_blk blk, isl_int *a,
1426 unsigned a_len, unsigned b_len)
1428 isl_seq_cpy(blk.data, a+a_len, b_len);
1429 isl_seq_cpy(blk.data+b_len, a, a_len);
1430 isl_seq_cpy(a, blk.data, b_len+a_len);
1433 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1434 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1436 int i;
1437 struct isl_blk blk;
1439 if (!bmap)
1440 goto error;
1442 isl_assert(bmap->ctx,
1443 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1445 if (n1 == 0 || n2 == 0)
1446 return bmap;
1448 bmap = isl_basic_map_cow(bmap);
1449 if (!bmap)
1450 return NULL;
1452 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1453 if (isl_blk_is_error(blk))
1454 goto error;
1456 for (i = 0; i < bmap->n_eq; ++i)
1457 swap_vars(blk,
1458 bmap->eq[i] + pos, n1, n2);
1460 for (i = 0; i < bmap->n_ineq; ++i)
1461 swap_vars(blk,
1462 bmap->ineq[i] + pos, n1, n2);
1464 for (i = 0; i < bmap->n_div; ++i)
1465 swap_vars(blk,
1466 bmap->div[i]+1 + pos, n1, n2);
1468 isl_blk_free(bmap->ctx, blk);
1470 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1471 bmap = isl_basic_map_gauss(bmap, NULL);
1472 return isl_basic_map_finalize(bmap);
1473 error:
1474 isl_basic_map_free(bmap);
1475 return NULL;
1478 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1479 __isl_take isl_basic_set *bset, unsigned n)
1481 unsigned dim;
1482 unsigned nparam;
1484 nparam = isl_basic_set_n_param(bset);
1485 dim = isl_basic_set_n_dim(bset);
1486 isl_assert(bset->ctx, n <= dim, goto error);
1488 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1489 error:
1490 isl_basic_set_free(bset);
1491 return NULL;
1494 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1496 int i = 0;
1497 unsigned total;
1498 if (!bmap)
1499 goto error;
1500 total = isl_basic_map_total_dim(bmap);
1501 isl_basic_map_free_div(bmap, bmap->n_div);
1502 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1503 if (bmap->n_eq > 0)
1504 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1505 else {
1506 i = isl_basic_map_alloc_equality(bmap);
1507 if (i < 0)
1508 goto error;
1510 isl_int_set_si(bmap->eq[i][0], 1);
1511 isl_seq_clr(bmap->eq[i]+1, total);
1512 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1513 isl_vec_free(bmap->sample);
1514 bmap->sample = NULL;
1515 return isl_basic_map_finalize(bmap);
1516 error:
1517 isl_basic_map_free(bmap);
1518 return NULL;
1521 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1523 return (struct isl_basic_set *)
1524 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1527 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1529 int i;
1530 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1531 isl_int *t = bmap->div[a];
1532 bmap->div[a] = bmap->div[b];
1533 bmap->div[b] = t;
1535 for (i = 0; i < bmap->n_eq; ++i)
1536 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1538 for (i = 0; i < bmap->n_ineq; ++i)
1539 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1541 for (i = 0; i < bmap->n_div; ++i)
1542 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1543 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1546 /* Eliminate the specified n dimensions starting at first from the
1547 * constraints using Fourier-Motzkin. The dimensions themselves
1548 * are not removed.
1550 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1551 enum isl_dim_type type, unsigned first, unsigned n)
1553 int i;
1555 if (!map)
1556 return NULL;
1557 if (n == 0)
1558 return map;
1560 map = isl_map_cow(map);
1561 if (!map)
1562 return NULL;
1564 for (i = 0; i < map->n; ++i) {
1565 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1566 if (!map->p[i])
1567 goto error;
1569 return map;
1570 error:
1571 isl_map_free(map);
1572 return NULL;
1575 /* Eliminate the specified n dimensions starting at first from the
1576 * constraints using Fourier-Motzkin. The dimensions themselves
1577 * are not removed.
1579 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1580 enum isl_dim_type type, unsigned first, unsigned n)
1582 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1585 /* Eliminate the specified n dimensions starting at first from the
1586 * constraints using Fourier-Motzkin. The dimensions themselves
1587 * are not removed.
1589 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1590 unsigned first, unsigned n)
1592 return isl_set_eliminate(set, isl_dim_set, first, n);
1595 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1596 __isl_take isl_basic_map *bmap)
1598 if (!bmap)
1599 return NULL;
1600 bmap = isl_basic_map_eliminate_vars(bmap,
1601 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1602 if (!bmap)
1603 return NULL;
1604 bmap->n_div = 0;
1605 return isl_basic_map_finalize(bmap);
1608 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1609 __isl_take isl_basic_set *bset)
1611 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1612 (struct isl_basic_map *)bset);
1615 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1617 int i;
1619 if (!map)
1620 return NULL;
1621 if (map->n == 0)
1622 return map;
1624 map = isl_map_cow(map);
1625 if (!map)
1626 return NULL;
1628 for (i = 0; i < map->n; ++i) {
1629 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1630 if (!map->p[i])
1631 goto error;
1633 return map;
1634 error:
1635 isl_map_free(map);
1636 return NULL;
1639 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1641 return isl_map_remove_divs(set);
1644 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1645 enum isl_dim_type type, unsigned first, unsigned n)
1647 if (!bmap)
1648 return NULL;
1649 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1650 goto error);
1651 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1652 return bmap;
1653 bmap = isl_basic_map_eliminate_vars(bmap,
1654 isl_basic_map_offset(bmap, type) - 1 + first, n);
1655 if (!bmap)
1656 return bmap;
1657 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1658 return bmap;
1659 bmap = isl_basic_map_drop(bmap, type, first, n);
1660 return bmap;
1661 error:
1662 isl_basic_map_free(bmap);
1663 return NULL;
1666 /* Return true if the definition of the given div (recursively) involves
1667 * any of the given variables.
1669 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1670 unsigned first, unsigned n)
1672 int i;
1673 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1675 if (isl_int_is_zero(bmap->div[div][0]))
1676 return 0;
1677 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1678 return 1;
1680 for (i = bmap->n_div - 1; i >= 0; --i) {
1681 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1682 continue;
1683 if (div_involves_vars(bmap, i, first, n))
1684 return 1;
1687 return 0;
1690 /* Remove all divs (recursively) involving any of the given dimensions
1691 * in their definitions.
1693 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1694 __isl_take isl_basic_map *bmap,
1695 enum isl_dim_type type, unsigned first, unsigned n)
1697 int i;
1699 if (!bmap)
1700 return NULL;
1701 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1702 goto error);
1703 first += isl_basic_map_offset(bmap, type);
1705 for (i = bmap->n_div - 1; i >= 0; --i) {
1706 if (!div_involves_vars(bmap, i, first, n))
1707 continue;
1708 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1709 if (!bmap)
1710 return NULL;
1711 i = bmap->n_div;
1714 return bmap;
1715 error:
1716 isl_basic_map_free(bmap);
1717 return NULL;
1720 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1721 enum isl_dim_type type, unsigned first, unsigned n)
1723 int i;
1725 if (!map)
1726 return NULL;
1727 if (map->n == 0)
1728 return map;
1730 map = isl_map_cow(map);
1731 if (!map)
1732 return NULL;
1734 for (i = 0; i < map->n; ++i) {
1735 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1736 type, first, n);
1737 if (!map->p[i])
1738 goto error;
1740 return map;
1741 error:
1742 isl_map_free(map);
1743 return NULL;
1746 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1747 enum isl_dim_type type, unsigned first, unsigned n)
1749 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1750 type, first, n);
1753 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1754 enum isl_dim_type type, unsigned first, unsigned n)
1756 int i;
1758 if (!bmap)
1759 return -1;
1761 if (first + n > isl_basic_map_dim(bmap, type))
1762 isl_die(bmap->ctx, isl_error_invalid,
1763 "index out of bounds", return -1);
1765 first += isl_basic_map_offset(bmap, type);
1766 for (i = 0; i < bmap->n_eq; ++i)
1767 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1768 return 1;
1769 for (i = 0; i < bmap->n_ineq; ++i)
1770 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1771 return 1;
1773 return 0;
1776 int isl_map_involves_dims(__isl_keep isl_map *map,
1777 enum isl_dim_type type, unsigned first, unsigned n)
1779 int i;
1781 if (!map)
1782 return -1;
1784 if (first + n > isl_map_dim(map, type))
1785 isl_die(map->ctx, isl_error_invalid,
1786 "index out of bounds", return -1);
1788 for (i = 0; i < map->n; ++i) {
1789 int involves = isl_basic_map_involves_dims(map->p[i],
1790 type, first, n);
1791 if (involves < 0 || involves)
1792 return involves;
1795 return 0;
1798 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1799 enum isl_dim_type type, unsigned first, unsigned n)
1801 return isl_basic_map_involves_dims(bset, type, first, n);
1804 int isl_set_involves_dims(__isl_keep isl_set *set,
1805 enum isl_dim_type type, unsigned first, unsigned n)
1807 return isl_map_involves_dims(set, type, first, n);
1810 /* Return true if the definition of the given div is unknown or depends
1811 * on unknown divs.
1813 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1815 int i;
1816 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1818 if (isl_int_is_zero(bmap->div[div][0]))
1819 return 1;
1821 for (i = bmap->n_div - 1; i >= 0; --i) {
1822 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1823 continue;
1824 if (div_is_unknown(bmap, i))
1825 return 1;
1828 return 0;
1831 /* Remove all divs that are unknown or defined in terms of unknown divs.
1833 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1834 __isl_take isl_basic_map *bmap)
1836 int i;
1838 if (!bmap)
1839 return NULL;
1841 for (i = bmap->n_div - 1; i >= 0; --i) {
1842 if (!div_is_unknown(bmap, i))
1843 continue;
1844 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1847 return bmap;
1850 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1852 int i;
1854 if (!map)
1855 return NULL;
1856 if (map->n == 0)
1857 return map;
1859 map = isl_map_cow(map);
1860 if (!map)
1861 return NULL;
1863 for (i = 0; i < map->n; ++i) {
1864 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1865 if (!map->p[i])
1866 goto error;
1868 return map;
1869 error:
1870 isl_map_free(map);
1871 return NULL;
1874 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1876 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1879 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1880 __isl_take isl_basic_set *bset,
1881 enum isl_dim_type type, unsigned first, unsigned n)
1883 return (isl_basic_set *)
1884 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1887 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1888 enum isl_dim_type type, unsigned first, unsigned n)
1890 int i;
1892 if (n == 0)
1893 return map;
1895 map = isl_map_cow(map);
1896 if (!map)
1897 return NULL;
1898 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1902 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1903 if (!map->p[i])
1904 goto error;
1906 map = isl_map_drop(map, type, first, n);
1907 return map;
1908 error:
1909 isl_map_free(map);
1910 return NULL;
1913 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1914 enum isl_dim_type type, unsigned first, unsigned n)
1916 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1919 /* Project out n inputs starting at first using Fourier-Motzkin */
1920 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1921 unsigned first, unsigned n)
1923 return isl_map_remove_dims(map, isl_dim_in, first, n);
1926 static void dump_term(struct isl_basic_map *bmap,
1927 isl_int c, int pos, FILE *out)
1929 const char *name;
1930 unsigned in = isl_basic_map_n_in(bmap);
1931 unsigned dim = in + isl_basic_map_n_out(bmap);
1932 unsigned nparam = isl_basic_map_n_param(bmap);
1933 if (!pos)
1934 isl_int_print(out, c, 0);
1935 else {
1936 if (!isl_int_is_one(c))
1937 isl_int_print(out, c, 0);
1938 if (pos < 1 + nparam) {
1939 name = isl_space_get_dim_name(bmap->dim,
1940 isl_dim_param, pos - 1);
1941 if (name)
1942 fprintf(out, "%s", name);
1943 else
1944 fprintf(out, "p%d", pos - 1);
1945 } else if (pos < 1 + nparam + in)
1946 fprintf(out, "i%d", pos - 1 - nparam);
1947 else if (pos < 1 + nparam + dim)
1948 fprintf(out, "o%d", pos - 1 - nparam - in);
1949 else
1950 fprintf(out, "e%d", pos - 1 - nparam - dim);
1954 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1955 int sign, FILE *out)
1957 int i;
1958 int first;
1959 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1960 isl_int v;
1962 isl_int_init(v);
1963 for (i = 0, first = 1; i < len; ++i) {
1964 if (isl_int_sgn(c[i]) * sign <= 0)
1965 continue;
1966 if (!first)
1967 fprintf(out, " + ");
1968 first = 0;
1969 isl_int_abs(v, c[i]);
1970 dump_term(bmap, v, i, out);
1972 isl_int_clear(v);
1973 if (first)
1974 fprintf(out, "0");
1977 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1978 const char *op, FILE *out, int indent)
1980 int i;
1982 fprintf(out, "%*s", indent, "");
1984 dump_constraint_sign(bmap, c, 1, out);
1985 fprintf(out, " %s ", op);
1986 dump_constraint_sign(bmap, c, -1, out);
1988 fprintf(out, "\n");
1990 for (i = bmap->n_div; i < bmap->extra; ++i) {
1991 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1992 continue;
1993 fprintf(out, "%*s", indent, "");
1994 fprintf(out, "ERROR: unused div coefficient not zero\n");
1995 abort();
1999 static void dump_constraints(struct isl_basic_map *bmap,
2000 isl_int **c, unsigned n,
2001 const char *op, FILE *out, int indent)
2003 int i;
2005 for (i = 0; i < n; ++i)
2006 dump_constraint(bmap, c[i], op, out, indent);
2009 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2011 int j;
2012 int first = 1;
2013 unsigned total = isl_basic_map_total_dim(bmap);
2015 for (j = 0; j < 1 + total; ++j) {
2016 if (isl_int_is_zero(exp[j]))
2017 continue;
2018 if (!first && isl_int_is_pos(exp[j]))
2019 fprintf(out, "+");
2020 dump_term(bmap, exp[j], j, out);
2021 first = 0;
2025 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2027 int i;
2029 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2030 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2032 for (i = 0; i < bmap->n_div; ++i) {
2033 fprintf(out, "%*s", indent, "");
2034 fprintf(out, "e%d = [(", i);
2035 dump_affine(bmap, bmap->div[i]+1, out);
2036 fprintf(out, ")/");
2037 isl_int_print(out, bmap->div[i][0], 0);
2038 fprintf(out, "]\n");
2042 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2043 FILE *out, int indent)
2045 if (!bset) {
2046 fprintf(out, "null basic set\n");
2047 return;
2050 fprintf(out, "%*s", indent, "");
2051 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2052 bset->ref, bset->dim->nparam, bset->dim->n_out,
2053 bset->extra, bset->flags);
2054 dump((struct isl_basic_map *)bset, out, indent);
2057 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2058 FILE *out, int indent)
2060 if (!bmap) {
2061 fprintf(out, "null basic map\n");
2062 return;
2065 fprintf(out, "%*s", indent, "");
2066 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2067 "flags: %x, n_name: %d\n",
2068 bmap->ref,
2069 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2070 bmap->extra, bmap->flags, bmap->dim->n_id);
2071 dump(bmap, out, indent);
2074 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2076 unsigned total;
2077 if (!bmap)
2078 return -1;
2079 total = isl_basic_map_total_dim(bmap);
2080 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2081 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2082 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2083 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2084 return 0;
2087 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2088 unsigned flags)
2090 struct isl_set *set;
2092 if (!dim)
2093 return NULL;
2094 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2095 isl_assert(dim->ctx, n >= 0, goto error);
2096 set = isl_alloc(dim->ctx, struct isl_set,
2097 sizeof(struct isl_set) +
2098 (n - 1) * sizeof(struct isl_basic_set *));
2099 if (!set)
2100 goto error;
2102 set->ctx = dim->ctx;
2103 isl_ctx_ref(set->ctx);
2104 set->ref = 1;
2105 set->size = n;
2106 set->n = 0;
2107 set->dim = dim;
2108 set->flags = flags;
2109 return set;
2110 error:
2111 isl_space_free(dim);
2112 return NULL;
2115 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2116 unsigned nparam, unsigned dim, int n, unsigned flags)
2118 struct isl_set *set;
2119 isl_space *dims;
2121 dims = isl_space_alloc(ctx, nparam, 0, dim);
2122 if (!dims)
2123 return NULL;
2125 set = isl_set_alloc_space(dims, n, flags);
2126 return set;
2129 /* Make sure "map" has room for at least "n" more basic maps.
2131 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2133 int i;
2134 struct isl_map *grown = NULL;
2136 if (!map)
2137 return NULL;
2138 isl_assert(map->ctx, n >= 0, goto error);
2139 if (map->n + n <= map->size)
2140 return map;
2141 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2142 if (!grown)
2143 goto error;
2144 for (i = 0; i < map->n; ++i) {
2145 grown->p[i] = isl_basic_map_copy(map->p[i]);
2146 if (!grown->p[i])
2147 goto error;
2148 grown->n++;
2150 isl_map_free(map);
2151 return grown;
2152 error:
2153 isl_map_free(grown);
2154 isl_map_free(map);
2155 return NULL;
2158 /* Make sure "set" has room for at least "n" more basic sets.
2160 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2162 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2165 struct isl_set *isl_set_dup(struct isl_set *set)
2167 int i;
2168 struct isl_set *dup;
2170 if (!set)
2171 return NULL;
2173 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2174 if (!dup)
2175 return NULL;
2176 for (i = 0; i < set->n; ++i)
2177 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2178 return dup;
2181 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2183 return isl_map_from_basic_map(bset);
2186 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2188 struct isl_map *map;
2190 if (!bmap)
2191 return NULL;
2193 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2194 return isl_map_add_basic_map(map, bmap);
2197 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2198 __isl_take isl_basic_set *bset)
2200 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2201 (struct isl_basic_map *)bset);
2204 void isl_set_free(struct isl_set *set)
2206 int i;
2208 if (!set)
2209 return;
2211 if (--set->ref > 0)
2212 return;
2214 isl_ctx_deref(set->ctx);
2215 for (i = 0; i < set->n; ++i)
2216 isl_basic_set_free(set->p[i]);
2217 isl_space_free(set->dim);
2218 free(set);
2221 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2223 int i;
2225 if (!set) {
2226 fprintf(out, "null set\n");
2227 return;
2230 fprintf(out, "%*s", indent, "");
2231 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2232 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2233 set->flags);
2234 for (i = 0; i < set->n; ++i) {
2235 fprintf(out, "%*s", indent, "");
2236 fprintf(out, "basic set %d:\n", i);
2237 isl_basic_set_print_internal(set->p[i], out, indent+4);
2241 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2243 int i;
2245 if (!map) {
2246 fprintf(out, "null map\n");
2247 return;
2250 fprintf(out, "%*s", indent, "");
2251 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2252 "flags: %x, n_name: %d\n",
2253 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2254 map->dim->n_out, map->flags, map->dim->n_id);
2255 for (i = 0; i < map->n; ++i) {
2256 fprintf(out, "%*s", indent, "");
2257 fprintf(out, "basic map %d:\n", i);
2258 isl_basic_map_print_internal(map->p[i], out, indent+4);
2262 struct isl_basic_map *isl_basic_map_intersect_domain(
2263 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2265 struct isl_basic_map *bmap_domain;
2267 if (!bmap || !bset)
2268 goto error;
2270 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2271 bset->dim, isl_dim_param), goto error);
2273 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2274 isl_assert(bset->ctx,
2275 isl_basic_map_compatible_domain(bmap, bset), goto error);
2277 bmap = isl_basic_map_cow(bmap);
2278 if (!bmap)
2279 goto error;
2280 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2281 bset->n_div, bset->n_eq, bset->n_ineq);
2282 bmap_domain = isl_basic_map_from_domain(bset);
2283 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2285 bmap = isl_basic_map_simplify(bmap);
2286 return isl_basic_map_finalize(bmap);
2287 error:
2288 isl_basic_map_free(bmap);
2289 isl_basic_set_free(bset);
2290 return NULL;
2293 struct isl_basic_map *isl_basic_map_intersect_range(
2294 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2296 struct isl_basic_map *bmap_range;
2298 if (!bmap || !bset)
2299 goto error;
2301 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2302 bset->dim, isl_dim_param), goto error);
2304 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2305 isl_assert(bset->ctx,
2306 isl_basic_map_compatible_range(bmap, bset), goto error);
2308 if (isl_basic_set_is_universe(bset)) {
2309 isl_basic_set_free(bset);
2310 return bmap;
2313 bmap = isl_basic_map_cow(bmap);
2314 if (!bmap)
2315 goto error;
2316 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2317 bset->n_div, bset->n_eq, bset->n_ineq);
2318 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2319 bmap = add_constraints(bmap, bmap_range, 0, 0);
2321 bmap = isl_basic_map_simplify(bmap);
2322 return isl_basic_map_finalize(bmap);
2323 error:
2324 isl_basic_map_free(bmap);
2325 isl_basic_set_free(bset);
2326 return NULL;
2329 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2331 int i;
2332 unsigned total;
2333 isl_int s;
2335 total = 1 + isl_basic_map_total_dim(bmap);
2336 if (total != vec->size)
2337 return -1;
2339 isl_int_init(s);
2341 for (i = 0; i < bmap->n_eq; ++i) {
2342 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2343 if (!isl_int_is_zero(s)) {
2344 isl_int_clear(s);
2345 return 0;
2349 for (i = 0; i < bmap->n_ineq; ++i) {
2350 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2351 if (isl_int_is_neg(s)) {
2352 isl_int_clear(s);
2353 return 0;
2357 isl_int_clear(s);
2359 return 1;
2362 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2364 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2367 struct isl_basic_map *isl_basic_map_intersect(
2368 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2370 struct isl_vec *sample = NULL;
2372 if (!bmap1 || !bmap2)
2373 goto error;
2375 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2376 bmap2->dim, isl_dim_param), goto error);
2377 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2378 isl_space_dim(bmap1->dim, isl_dim_param) &&
2379 isl_space_dim(bmap2->dim, isl_dim_all) !=
2380 isl_space_dim(bmap2->dim, isl_dim_param))
2381 return isl_basic_map_intersect(bmap2, bmap1);
2383 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2384 isl_space_dim(bmap2->dim, isl_dim_param))
2385 isl_assert(bmap1->ctx,
2386 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2388 if (bmap1->sample &&
2389 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2390 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2391 sample = isl_vec_copy(bmap1->sample);
2392 else if (bmap2->sample &&
2393 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2394 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2395 sample = isl_vec_copy(bmap2->sample);
2397 bmap1 = isl_basic_map_cow(bmap1);
2398 if (!bmap1)
2399 goto error;
2400 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2401 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2402 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2404 if (!bmap1)
2405 isl_vec_free(sample);
2406 else if (sample) {
2407 isl_vec_free(bmap1->sample);
2408 bmap1->sample = sample;
2411 bmap1 = isl_basic_map_simplify(bmap1);
2412 return isl_basic_map_finalize(bmap1);
2413 error:
2414 if (sample)
2415 isl_vec_free(sample);
2416 isl_basic_map_free(bmap1);
2417 isl_basic_map_free(bmap2);
2418 return NULL;
2421 struct isl_basic_set *isl_basic_set_intersect(
2422 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2424 return (struct isl_basic_set *)
2425 isl_basic_map_intersect(
2426 (struct isl_basic_map *)bset1,
2427 (struct isl_basic_map *)bset2);
2430 /* Special case of isl_map_intersect, where both map1 and map2
2431 * are convex, without any divs and such that either map1 or map2
2432 * contains a single constraint. This constraint is then simply
2433 * added to the other map.
2435 static __isl_give isl_map *map_intersect_add_constraint(
2436 __isl_take isl_map *map1, __isl_take isl_map *map2)
2438 isl_assert(map1->ctx, map1->n == 1, goto error);
2439 isl_assert(map2->ctx, map1->n == 1, goto error);
2440 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2441 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2443 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2444 return isl_map_intersect(map2, map1);
2446 isl_assert(map2->ctx,
2447 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2449 map1 = isl_map_cow(map1);
2450 if (!map1)
2451 goto error;
2452 if (isl_map_plain_is_empty(map1)) {
2453 isl_map_free(map2);
2454 return map1;
2456 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2457 if (map2->p[0]->n_eq == 1)
2458 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2459 else
2460 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2461 map2->p[0]->ineq[0]);
2463 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2464 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2465 if (!map1->p[0])
2466 goto error;
2468 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2469 isl_basic_map_free(map1->p[0]);
2470 map1->n = 0;
2473 isl_map_free(map2);
2475 return map1;
2476 error:
2477 isl_map_free(map1);
2478 isl_map_free(map2);
2479 return NULL;
2482 /* map2 may be either a parameter domain or a map living in the same
2483 * space as map1.
2485 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2486 __isl_take isl_map *map2)
2488 unsigned flags = 0;
2489 struct isl_map *result;
2490 int i, j;
2492 if (!map1 || !map2)
2493 goto error;
2495 if (isl_map_plain_is_empty(map1) &&
2496 isl_space_is_equal(map1->dim, map2->dim)) {
2497 isl_map_free(map2);
2498 return map1;
2500 if (isl_map_plain_is_empty(map2) &&
2501 isl_space_is_equal(map1->dim, map2->dim)) {
2502 isl_map_free(map1);
2503 return map2;
2506 if (map1->n == 1 && map2->n == 1 &&
2507 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2508 isl_space_is_equal(map1->dim, map2->dim) &&
2509 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2510 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2511 return map_intersect_add_constraint(map1, map2);
2513 if (isl_space_dim(map2->dim, isl_dim_all) !=
2514 isl_space_dim(map2->dim, isl_dim_param))
2515 isl_assert(map1->ctx,
2516 isl_space_is_equal(map1->dim, map2->dim), goto error);
2518 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2519 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2520 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2522 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2523 map1->n * map2->n, flags);
2524 if (!result)
2525 goto error;
2526 for (i = 0; i < map1->n; ++i)
2527 for (j = 0; j < map2->n; ++j) {
2528 struct isl_basic_map *part;
2529 part = isl_basic_map_intersect(
2530 isl_basic_map_copy(map1->p[i]),
2531 isl_basic_map_copy(map2->p[j]));
2532 if (isl_basic_map_is_empty(part))
2533 isl_basic_map_free(part);
2534 else
2535 result = isl_map_add_basic_map(result, part);
2536 if (!result)
2537 goto error;
2539 isl_map_free(map1);
2540 isl_map_free(map2);
2541 return result;
2542 error:
2543 isl_map_free(map1);
2544 isl_map_free(map2);
2545 return NULL;
2548 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2549 __isl_take isl_map *map2)
2551 if (!map1 || !map2)
2552 goto error;
2553 if (!isl_space_is_equal(map1->dim, map2->dim))
2554 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2555 "spaces don't match", goto error);
2556 return map_intersect_internal(map1, map2);
2557 error:
2558 isl_map_free(map1);
2559 isl_map_free(map2);
2560 return NULL;
2563 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2564 __isl_take isl_map *map2)
2566 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2569 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2571 return (struct isl_set *)
2572 isl_map_intersect((struct isl_map *)set1,
2573 (struct isl_map *)set2);
2576 /* map_intersect_internal accepts intersections
2577 * with parameter domains, so we can just call that function.
2579 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2580 __isl_take isl_set *params)
2582 return map_intersect_internal(map, params);
2585 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2586 __isl_take isl_map *map2)
2588 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2591 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2592 __isl_take isl_set *params)
2594 return isl_map_intersect_params(set, params);
2597 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2599 isl_space *dim;
2600 struct isl_basic_set *bset;
2601 unsigned in;
2603 if (!bmap)
2604 return NULL;
2605 bmap = isl_basic_map_cow(bmap);
2606 if (!bmap)
2607 return NULL;
2608 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2609 in = isl_basic_map_n_in(bmap);
2610 bset = isl_basic_set_from_basic_map(bmap);
2611 bset = isl_basic_set_swap_vars(bset, in);
2612 return isl_basic_map_from_basic_set(bset, dim);
2615 static __isl_give isl_basic_map *basic_map_space_reset(
2616 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2618 isl_space *space;
2620 if (!isl_space_is_named_or_nested(bmap->dim, type))
2621 return bmap;
2623 space = isl_basic_map_get_space(bmap);
2624 space = isl_space_reset(space, type);
2625 bmap = isl_basic_map_reset_space(bmap, space);
2626 return bmap;
2629 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2630 enum isl_dim_type type, unsigned pos, unsigned n)
2632 isl_space *res_dim;
2633 struct isl_basic_map *res;
2634 struct isl_dim_map *dim_map;
2635 unsigned total, off;
2636 enum isl_dim_type t;
2638 if (n == 0)
2639 return basic_map_space_reset(bmap, type);
2641 if (!bmap)
2642 return NULL;
2644 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2646 total = isl_basic_map_total_dim(bmap) + n;
2647 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2648 off = 0;
2649 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2650 if (t != type) {
2651 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2652 } else {
2653 unsigned size = isl_basic_map_dim(bmap, t);
2654 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2655 0, pos, off);
2656 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2657 pos, size - pos, off + pos + n);
2659 off += isl_space_dim(res_dim, t);
2661 isl_dim_map_div(dim_map, bmap, off);
2663 res = isl_basic_map_alloc_space(res_dim,
2664 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2665 if (isl_basic_map_is_rational(bmap))
2666 res = isl_basic_map_set_rational(res);
2667 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2668 return isl_basic_map_finalize(res);
2671 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2672 enum isl_dim_type type, unsigned n)
2674 if (!bmap)
2675 return NULL;
2676 return isl_basic_map_insert(bmap, type,
2677 isl_basic_map_dim(bmap, type), n);
2680 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2681 enum isl_dim_type type, unsigned n)
2683 if (!bset)
2684 return NULL;
2685 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2686 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2687 error:
2688 isl_basic_set_free(bset);
2689 return NULL;
2692 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2693 enum isl_dim_type type)
2695 isl_space *space;
2697 if (!isl_space_is_named_or_nested(map->dim, type))
2698 return map;
2700 space = isl_map_get_space(map);
2701 space = isl_space_reset(space, type);
2702 map = isl_map_reset_space(map, space);
2703 return map;
2706 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2707 enum isl_dim_type type, unsigned pos, unsigned n)
2709 int i;
2711 if (n == 0)
2712 return map_space_reset(map, type);
2714 map = isl_map_cow(map);
2715 if (!map)
2716 return NULL;
2718 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2719 if (!map->dim)
2720 goto error;
2722 for (i = 0; i < map->n; ++i) {
2723 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2724 if (!map->p[i])
2725 goto error;
2728 return map;
2729 error:
2730 isl_map_free(map);
2731 return NULL;
2734 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2735 enum isl_dim_type type, unsigned pos, unsigned n)
2737 return isl_map_insert_dims(set, type, pos, n);
2740 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2741 enum isl_dim_type type, unsigned n)
2743 if (!map)
2744 return NULL;
2745 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2748 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2749 enum isl_dim_type type, unsigned n)
2751 if (!set)
2752 return NULL;
2753 isl_assert(set->ctx, type != isl_dim_in, goto error);
2754 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2755 error:
2756 isl_set_free(set);
2757 return NULL;
2760 __isl_give isl_basic_map *isl_basic_map_move_dims(
2761 __isl_take isl_basic_map *bmap,
2762 enum isl_dim_type dst_type, unsigned dst_pos,
2763 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2765 struct isl_dim_map *dim_map;
2766 struct isl_basic_map *res;
2767 enum isl_dim_type t;
2768 unsigned total, off;
2770 if (!bmap)
2771 return NULL;
2772 if (n == 0)
2773 return bmap;
2775 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2776 goto error);
2778 if (dst_type == src_type && dst_pos == src_pos)
2779 return bmap;
2781 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2783 if (pos(bmap->dim, dst_type) + dst_pos ==
2784 pos(bmap->dim, src_type) + src_pos +
2785 ((src_type < dst_type) ? n : 0)) {
2786 bmap = isl_basic_map_cow(bmap);
2787 if (!bmap)
2788 return NULL;
2790 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2791 src_type, src_pos, n);
2792 if (!bmap->dim)
2793 goto error;
2795 bmap = isl_basic_map_finalize(bmap);
2797 return bmap;
2800 total = isl_basic_map_total_dim(bmap);
2801 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2803 off = 0;
2804 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2805 unsigned size = isl_space_dim(bmap->dim, t);
2806 if (t == dst_type) {
2807 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2808 0, dst_pos, off);
2809 off += dst_pos;
2810 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2811 src_pos, n, off);
2812 off += n;
2813 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2814 dst_pos, size - dst_pos, off);
2815 off += size - dst_pos;
2816 } else if (t == src_type) {
2817 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2818 0, src_pos, off);
2819 off += src_pos;
2820 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2821 src_pos + n, size - src_pos - n, off);
2822 off += size - src_pos - n;
2823 } else {
2824 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2825 off += size;
2828 isl_dim_map_div(dim_map, bmap, off);
2830 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2831 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2832 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2834 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2835 src_type, src_pos, n);
2836 if (!bmap->dim)
2837 goto error;
2839 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2840 bmap = isl_basic_map_gauss(bmap, NULL);
2841 bmap = isl_basic_map_finalize(bmap);
2843 return bmap;
2844 error:
2845 isl_basic_map_free(bmap);
2846 return NULL;
2849 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2850 enum isl_dim_type dst_type, unsigned dst_pos,
2851 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2853 return (isl_basic_set *)isl_basic_map_move_dims(
2854 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2857 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2858 enum isl_dim_type dst_type, unsigned dst_pos,
2859 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2861 if (!set)
2862 return NULL;
2863 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2864 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2865 src_type, src_pos, n);
2866 error:
2867 isl_set_free(set);
2868 return NULL;
2871 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2872 enum isl_dim_type dst_type, unsigned dst_pos,
2873 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2875 int i;
2877 if (!map)
2878 return NULL;
2879 if (n == 0)
2880 return map;
2882 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2883 goto error);
2885 if (dst_type == src_type && dst_pos == src_pos)
2886 return map;
2888 isl_assert(map->ctx, dst_type != src_type, goto error);
2890 map = isl_map_cow(map);
2891 if (!map)
2892 return NULL;
2894 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2895 if (!map->dim)
2896 goto error;
2898 for (i = 0; i < map->n; ++i) {
2899 map->p[i] = isl_basic_map_move_dims(map->p[i],
2900 dst_type, dst_pos,
2901 src_type, src_pos, n);
2902 if (!map->p[i])
2903 goto error;
2906 return map;
2907 error:
2908 isl_map_free(map);
2909 return NULL;
2912 /* Move the specified dimensions to the last columns right before
2913 * the divs. Don't change the dimension specification of bmap.
2914 * That's the responsibility of the caller.
2916 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2917 enum isl_dim_type type, unsigned first, unsigned n)
2919 struct isl_dim_map *dim_map;
2920 struct isl_basic_map *res;
2921 enum isl_dim_type t;
2922 unsigned total, off;
2924 if (!bmap)
2925 return NULL;
2926 if (pos(bmap->dim, type) + first + n ==
2927 1 + isl_space_dim(bmap->dim, isl_dim_all))
2928 return bmap;
2930 total = isl_basic_map_total_dim(bmap);
2931 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2933 off = 0;
2934 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2935 unsigned size = isl_space_dim(bmap->dim, t);
2936 if (t == type) {
2937 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2938 0, first, off);
2939 off += first;
2940 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2941 first, n, total - bmap->n_div - n);
2942 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2943 first + n, size - (first + n), off);
2944 off += size - (first + n);
2945 } else {
2946 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2947 off += size;
2950 isl_dim_map_div(dim_map, bmap, off + n);
2952 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2953 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2954 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2955 return res;
2958 /* Turn the n dimensions of type type, starting at first
2959 * into existentially quantified variables.
2961 __isl_give isl_basic_map *isl_basic_map_project_out(
2962 __isl_take isl_basic_map *bmap,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2965 int i;
2966 size_t row_size;
2967 isl_int **new_div;
2968 isl_int *old;
2970 if (n == 0)
2971 return bmap;
2973 if (!bmap)
2974 return NULL;
2976 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2977 return isl_basic_map_remove_dims(bmap, type, first, n);
2979 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2980 goto error);
2982 bmap = move_last(bmap, type, first, n);
2983 bmap = isl_basic_map_cow(bmap);
2984 if (!bmap)
2985 return NULL;
2987 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2988 old = bmap->block2.data;
2989 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2990 (bmap->extra + n) * (1 + row_size));
2991 if (!bmap->block2.data)
2992 goto error;
2993 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2994 if (!new_div)
2995 goto error;
2996 for (i = 0; i < n; ++i) {
2997 new_div[i] = bmap->block2.data +
2998 (bmap->extra + i) * (1 + row_size);
2999 isl_seq_clr(new_div[i], 1 + row_size);
3001 for (i = 0; i < bmap->extra; ++i)
3002 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3003 free(bmap->div);
3004 bmap->div = new_div;
3005 bmap->n_div += n;
3006 bmap->extra += n;
3008 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3009 if (!bmap->dim)
3010 goto error;
3011 bmap = isl_basic_map_simplify(bmap);
3012 bmap = isl_basic_map_drop_redundant_divs(bmap);
3013 return isl_basic_map_finalize(bmap);
3014 error:
3015 isl_basic_map_free(bmap);
3016 return NULL;
3019 /* Turn the n dimensions of type type, starting at first
3020 * into existentially quantified variables.
3022 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3023 enum isl_dim_type type, unsigned first, unsigned n)
3025 return (isl_basic_set *)isl_basic_map_project_out(
3026 (isl_basic_map *)bset, type, first, n);
3029 /* Turn the n dimensions of type type, starting at first
3030 * into existentially quantified variables.
3032 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3033 enum isl_dim_type type, unsigned first, unsigned n)
3035 int i;
3037 if (!map)
3038 return NULL;
3040 if (n == 0)
3041 return map;
3043 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3045 map = isl_map_cow(map);
3046 if (!map)
3047 return NULL;
3049 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3050 if (!map->dim)
3051 goto error;
3053 for (i = 0; i < map->n; ++i) {
3054 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3055 if (!map->p[i])
3056 goto error;
3059 return map;
3060 error:
3061 isl_map_free(map);
3062 return NULL;
3065 /* Turn the n dimensions of type type, starting at first
3066 * into existentially quantified variables.
3068 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3069 enum isl_dim_type type, unsigned first, unsigned n)
3071 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3074 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3076 int i, j;
3078 for (i = 0; i < n; ++i) {
3079 j = isl_basic_map_alloc_div(bmap);
3080 if (j < 0)
3081 goto error;
3082 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3084 return bmap;
3085 error:
3086 isl_basic_map_free(bmap);
3087 return NULL;
3090 struct isl_basic_map *isl_basic_map_apply_range(
3091 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3093 isl_space *dim_result = NULL;
3094 struct isl_basic_map *bmap;
3095 unsigned n_in, n_out, n, nparam, total, pos;
3096 struct isl_dim_map *dim_map1, *dim_map2;
3098 if (!bmap1 || !bmap2)
3099 goto error;
3101 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3102 isl_space_copy(bmap2->dim));
3104 n_in = isl_basic_map_n_in(bmap1);
3105 n_out = isl_basic_map_n_out(bmap2);
3106 n = isl_basic_map_n_out(bmap1);
3107 nparam = isl_basic_map_n_param(bmap1);
3109 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3110 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3111 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3112 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3113 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3114 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3115 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3116 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3117 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3118 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3119 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3121 bmap = isl_basic_map_alloc_space(dim_result,
3122 bmap1->n_div + bmap2->n_div + n,
3123 bmap1->n_eq + bmap2->n_eq,
3124 bmap1->n_ineq + bmap2->n_ineq);
3125 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3126 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3127 bmap = add_divs(bmap, n);
3128 bmap = isl_basic_map_simplify(bmap);
3129 bmap = isl_basic_map_drop_redundant_divs(bmap);
3130 return isl_basic_map_finalize(bmap);
3131 error:
3132 isl_basic_map_free(bmap1);
3133 isl_basic_map_free(bmap2);
3134 return NULL;
3137 struct isl_basic_set *isl_basic_set_apply(
3138 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3140 if (!bset || !bmap)
3141 goto error;
3143 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3144 goto error);
3146 return (struct isl_basic_set *)
3147 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3148 error:
3149 isl_basic_set_free(bset);
3150 isl_basic_map_free(bmap);
3151 return NULL;
3154 struct isl_basic_map *isl_basic_map_apply_domain(
3155 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3157 if (!bmap1 || !bmap2)
3158 goto error;
3160 isl_assert(bmap1->ctx,
3161 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3162 isl_assert(bmap1->ctx,
3163 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3164 goto error);
3166 bmap1 = isl_basic_map_reverse(bmap1);
3167 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3168 return isl_basic_map_reverse(bmap1);
3169 error:
3170 isl_basic_map_free(bmap1);
3171 isl_basic_map_free(bmap2);
3172 return NULL;
3175 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3176 * A \cap B -> f(A) + f(B)
3178 struct isl_basic_map *isl_basic_map_sum(
3179 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3181 unsigned n_in, n_out, nparam, total, pos;
3182 struct isl_basic_map *bmap = NULL;
3183 struct isl_dim_map *dim_map1, *dim_map2;
3184 int i;
3186 if (!bmap1 || !bmap2)
3187 goto error;
3189 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3190 goto error);
3192 nparam = isl_basic_map_n_param(bmap1);
3193 n_in = isl_basic_map_n_in(bmap1);
3194 n_out = isl_basic_map_n_out(bmap1);
3196 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3197 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3198 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3199 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3200 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3201 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3202 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3203 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3204 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3205 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3206 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3208 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3209 bmap1->n_div + bmap2->n_div + 2 * n_out,
3210 bmap1->n_eq + bmap2->n_eq + n_out,
3211 bmap1->n_ineq + bmap2->n_ineq);
3212 for (i = 0; i < n_out; ++i) {
3213 int j = isl_basic_map_alloc_equality(bmap);
3214 if (j < 0)
3215 goto error;
3216 isl_seq_clr(bmap->eq[j], 1+total);
3217 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3218 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3219 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3221 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3222 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3223 bmap = add_divs(bmap, 2 * n_out);
3225 bmap = isl_basic_map_simplify(bmap);
3226 return isl_basic_map_finalize(bmap);
3227 error:
3228 isl_basic_map_free(bmap);
3229 isl_basic_map_free(bmap1);
3230 isl_basic_map_free(bmap2);
3231 return NULL;
3234 /* Given two maps A -> f(A) and B -> g(B), construct a map
3235 * A \cap B -> f(A) + f(B)
3237 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3239 struct isl_map *result;
3240 int i, j;
3242 if (!map1 || !map2)
3243 goto error;
3245 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3247 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3248 map1->n * map2->n, 0);
3249 if (!result)
3250 goto error;
3251 for (i = 0; i < map1->n; ++i)
3252 for (j = 0; j < map2->n; ++j) {
3253 struct isl_basic_map *part;
3254 part = isl_basic_map_sum(
3255 isl_basic_map_copy(map1->p[i]),
3256 isl_basic_map_copy(map2->p[j]));
3257 if (isl_basic_map_is_empty(part))
3258 isl_basic_map_free(part);
3259 else
3260 result = isl_map_add_basic_map(result, part);
3261 if (!result)
3262 goto error;
3264 isl_map_free(map1);
3265 isl_map_free(map2);
3266 return result;
3267 error:
3268 isl_map_free(map1);
3269 isl_map_free(map2);
3270 return NULL;
3273 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3274 __isl_take isl_set *set2)
3276 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3279 /* Given a basic map A -> f(A), construct A -> -f(A).
3281 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3283 int i, j;
3284 unsigned off, n;
3286 bmap = isl_basic_map_cow(bmap);
3287 if (!bmap)
3288 return NULL;
3290 n = isl_basic_map_dim(bmap, isl_dim_out);
3291 off = isl_basic_map_offset(bmap, isl_dim_out);
3292 for (i = 0; i < bmap->n_eq; ++i)
3293 for (j = 0; j < n; ++j)
3294 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3295 for (i = 0; i < bmap->n_ineq; ++i)
3296 for (j = 0; j < n; ++j)
3297 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3298 for (i = 0; i < bmap->n_div; ++i)
3299 for (j = 0; j < n; ++j)
3300 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3301 bmap = isl_basic_map_gauss(bmap, NULL);
3302 return isl_basic_map_finalize(bmap);
3305 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3307 return isl_basic_map_neg(bset);
3310 /* Given a map A -> f(A), construct A -> -f(A).
3312 struct isl_map *isl_map_neg(struct isl_map *map)
3314 int i;
3316 map = isl_map_cow(map);
3317 if (!map)
3318 return NULL;
3320 for (i = 0; i < map->n; ++i) {
3321 map->p[i] = isl_basic_map_neg(map->p[i]);
3322 if (!map->p[i])
3323 goto error;
3326 return map;
3327 error:
3328 isl_map_free(map);
3329 return NULL;
3332 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3334 return (isl_set *)isl_map_neg((isl_map *)set);
3337 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3338 * A -> floor(f(A)/d).
3340 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3341 isl_int d)
3343 unsigned n_in, n_out, nparam, total, pos;
3344 struct isl_basic_map *result = NULL;
3345 struct isl_dim_map *dim_map;
3346 int i;
3348 if (!bmap)
3349 return NULL;
3351 nparam = isl_basic_map_n_param(bmap);
3352 n_in = isl_basic_map_n_in(bmap);
3353 n_out = isl_basic_map_n_out(bmap);
3355 total = nparam + n_in + n_out + bmap->n_div + n_out;
3356 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3357 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3358 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3359 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3360 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3362 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3363 bmap->n_div + n_out,
3364 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3365 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3366 result = add_divs(result, n_out);
3367 for (i = 0; i < n_out; ++i) {
3368 int j;
3369 j = isl_basic_map_alloc_inequality(result);
3370 if (j < 0)
3371 goto error;
3372 isl_seq_clr(result->ineq[j], 1+total);
3373 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3374 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3375 j = isl_basic_map_alloc_inequality(result);
3376 if (j < 0)
3377 goto error;
3378 isl_seq_clr(result->ineq[j], 1+total);
3379 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3380 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3381 isl_int_sub_ui(result->ineq[j][0], d, 1);
3384 result = isl_basic_map_simplify(result);
3385 return isl_basic_map_finalize(result);
3386 error:
3387 isl_basic_map_free(result);
3388 return NULL;
3391 /* Given a map A -> f(A) and an integer d, construct a map
3392 * A -> floor(f(A)/d).
3394 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3396 int i;
3398 map = isl_map_cow(map);
3399 if (!map)
3400 return NULL;
3402 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3403 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3404 for (i = 0; i < map->n; ++i) {
3405 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3406 if (!map->p[i])
3407 goto error;
3410 return map;
3411 error:
3412 isl_map_free(map);
3413 return NULL;
3416 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3418 int i;
3419 unsigned nparam;
3420 unsigned n_in;
3422 i = isl_basic_map_alloc_equality(bmap);
3423 if (i < 0)
3424 goto error;
3425 nparam = isl_basic_map_n_param(bmap);
3426 n_in = isl_basic_map_n_in(bmap);
3427 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3428 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3429 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3430 return isl_basic_map_finalize(bmap);
3431 error:
3432 isl_basic_map_free(bmap);
3433 return NULL;
3436 /* Add a constraints to "bmap" expressing i_pos < o_pos
3438 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3440 int i;
3441 unsigned nparam;
3442 unsigned n_in;
3444 i = isl_basic_map_alloc_inequality(bmap);
3445 if (i < 0)
3446 goto error;
3447 nparam = isl_basic_map_n_param(bmap);
3448 n_in = isl_basic_map_n_in(bmap);
3449 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3450 isl_int_set_si(bmap->ineq[i][0], -1);
3451 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3452 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3453 return isl_basic_map_finalize(bmap);
3454 error:
3455 isl_basic_map_free(bmap);
3456 return NULL;
3459 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3461 static __isl_give isl_basic_map *var_less_or_equal(
3462 __isl_take isl_basic_map *bmap, unsigned pos)
3464 int i;
3465 unsigned nparam;
3466 unsigned n_in;
3468 i = isl_basic_map_alloc_inequality(bmap);
3469 if (i < 0)
3470 goto error;
3471 nparam = isl_basic_map_n_param(bmap);
3472 n_in = isl_basic_map_n_in(bmap);
3473 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3474 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3475 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3476 return isl_basic_map_finalize(bmap);
3477 error:
3478 isl_basic_map_free(bmap);
3479 return NULL;
3482 /* Add a constraints to "bmap" expressing i_pos > o_pos
3484 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3486 int i;
3487 unsigned nparam;
3488 unsigned n_in;
3490 i = isl_basic_map_alloc_inequality(bmap);
3491 if (i < 0)
3492 goto error;
3493 nparam = isl_basic_map_n_param(bmap);
3494 n_in = isl_basic_map_n_in(bmap);
3495 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3496 isl_int_set_si(bmap->ineq[i][0], -1);
3497 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3498 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3499 return isl_basic_map_finalize(bmap);
3500 error:
3501 isl_basic_map_free(bmap);
3502 return NULL;
3505 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3507 static __isl_give isl_basic_map *var_more_or_equal(
3508 __isl_take isl_basic_map *bmap, unsigned pos)
3510 int i;
3511 unsigned nparam;
3512 unsigned n_in;
3514 i = isl_basic_map_alloc_inequality(bmap);
3515 if (i < 0)
3516 goto error;
3517 nparam = isl_basic_map_n_param(bmap);
3518 n_in = isl_basic_map_n_in(bmap);
3519 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3520 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3521 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3522 return isl_basic_map_finalize(bmap);
3523 error:
3524 isl_basic_map_free(bmap);
3525 return NULL;
3528 __isl_give isl_basic_map *isl_basic_map_equal(
3529 __isl_take isl_space *dim, unsigned n_equal)
3531 int i;
3532 struct isl_basic_map *bmap;
3533 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3534 if (!bmap)
3535 return NULL;
3536 for (i = 0; i < n_equal && bmap; ++i)
3537 bmap = var_equal(bmap, i);
3538 return isl_basic_map_finalize(bmap);
3541 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3543 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3544 unsigned pos)
3546 int i;
3547 struct isl_basic_map *bmap;
3548 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3549 if (!bmap)
3550 return NULL;
3551 for (i = 0; i < pos && bmap; ++i)
3552 bmap = var_equal(bmap, i);
3553 if (bmap)
3554 bmap = var_less(bmap, pos);
3555 return isl_basic_map_finalize(bmap);
3558 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3560 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3561 __isl_take isl_space *dim, unsigned pos)
3563 int i;
3564 isl_basic_map *bmap;
3566 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3567 for (i = 0; i < pos; ++i)
3568 bmap = var_equal(bmap, i);
3569 bmap = var_less_or_equal(bmap, pos);
3570 return isl_basic_map_finalize(bmap);
3573 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3575 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3576 unsigned pos)
3578 int i;
3579 struct isl_basic_map *bmap;
3580 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3581 if (!bmap)
3582 return NULL;
3583 for (i = 0; i < pos && bmap; ++i)
3584 bmap = var_equal(bmap, i);
3585 if (bmap)
3586 bmap = var_more(bmap, pos);
3587 return isl_basic_map_finalize(bmap);
3590 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3592 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3593 __isl_take isl_space *dim, unsigned pos)
3595 int i;
3596 isl_basic_map *bmap;
3598 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3599 for (i = 0; i < pos; ++i)
3600 bmap = var_equal(bmap, i);
3601 bmap = var_more_or_equal(bmap, pos);
3602 return isl_basic_map_finalize(bmap);
3605 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3606 unsigned n, int equal)
3608 struct isl_map *map;
3609 int i;
3611 if (n == 0 && equal)
3612 return isl_map_universe(dims);
3614 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3616 for (i = 0; i + 1 < n; ++i)
3617 map = isl_map_add_basic_map(map,
3618 isl_basic_map_less_at(isl_space_copy(dims), i));
3619 if (n > 0) {
3620 if (equal)
3621 map = isl_map_add_basic_map(map,
3622 isl_basic_map_less_or_equal_at(dims, n - 1));
3623 else
3624 map = isl_map_add_basic_map(map,
3625 isl_basic_map_less_at(dims, n - 1));
3626 } else
3627 isl_space_free(dims);
3629 return map;
3632 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3634 if (!dims)
3635 return NULL;
3636 return map_lex_lte_first(dims, dims->n_out, equal);
3639 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3641 return map_lex_lte_first(dim, n, 0);
3644 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3646 return map_lex_lte_first(dim, n, 1);
3649 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3651 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3654 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3656 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3659 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3660 unsigned n, int equal)
3662 struct isl_map *map;
3663 int i;
3665 if (n == 0 && equal)
3666 return isl_map_universe(dims);
3668 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3670 for (i = 0; i + 1 < n; ++i)
3671 map = isl_map_add_basic_map(map,
3672 isl_basic_map_more_at(isl_space_copy(dims), i));
3673 if (n > 0) {
3674 if (equal)
3675 map = isl_map_add_basic_map(map,
3676 isl_basic_map_more_or_equal_at(dims, n - 1));
3677 else
3678 map = isl_map_add_basic_map(map,
3679 isl_basic_map_more_at(dims, n - 1));
3680 } else
3681 isl_space_free(dims);
3683 return map;
3686 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3688 if (!dims)
3689 return NULL;
3690 return map_lex_gte_first(dims, dims->n_out, equal);
3693 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3695 return map_lex_gte_first(dim, n, 0);
3698 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3700 return map_lex_gte_first(dim, n, 1);
3703 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3705 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3708 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3710 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3713 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3714 __isl_take isl_set *set2)
3716 isl_map *map;
3717 map = isl_map_lex_le(isl_set_get_space(set1));
3718 map = isl_map_intersect_domain(map, set1);
3719 map = isl_map_intersect_range(map, set2);
3720 return map;
3723 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3724 __isl_take isl_set *set2)
3726 isl_map *map;
3727 map = isl_map_lex_lt(isl_set_get_space(set1));
3728 map = isl_map_intersect_domain(map, set1);
3729 map = isl_map_intersect_range(map, set2);
3730 return map;
3733 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3734 __isl_take isl_set *set2)
3736 isl_map *map;
3737 map = isl_map_lex_ge(isl_set_get_space(set1));
3738 map = isl_map_intersect_domain(map, set1);
3739 map = isl_map_intersect_range(map, set2);
3740 return map;
3743 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3744 __isl_take isl_set *set2)
3746 isl_map *map;
3747 map = isl_map_lex_gt(isl_set_get_space(set1));
3748 map = isl_map_intersect_domain(map, set1);
3749 map = isl_map_intersect_range(map, set2);
3750 return map;
3753 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3754 __isl_take isl_map *map2)
3756 isl_map *map;
3757 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3758 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3759 map = isl_map_apply_range(map, isl_map_reverse(map2));
3760 return map;
3763 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3766 isl_map *map;
3767 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3768 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3769 map = isl_map_apply_range(map, isl_map_reverse(map2));
3770 return map;
3773 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3774 __isl_take isl_map *map2)
3776 isl_map *map;
3777 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3778 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3779 map = isl_map_apply_range(map, isl_map_reverse(map2));
3780 return map;
3783 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3784 __isl_take isl_map *map2)
3786 isl_map *map;
3787 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3788 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3789 map = isl_map_apply_range(map, isl_map_reverse(map2));
3790 return map;
3793 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3794 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3796 struct isl_basic_map *bmap;
3798 bset = isl_basic_set_cow(bset);
3799 if (!bset || !dim)
3800 goto error;
3802 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3803 isl_space_free(bset->dim);
3804 bmap = (struct isl_basic_map *) bset;
3805 bmap->dim = dim;
3806 return isl_basic_map_finalize(bmap);
3807 error:
3808 isl_basic_set_free(bset);
3809 isl_space_free(dim);
3810 return NULL;
3813 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3815 if (!bmap)
3816 goto error;
3817 if (bmap->dim->n_in == 0)
3818 return (struct isl_basic_set *)bmap;
3819 bmap = isl_basic_map_cow(bmap);
3820 if (!bmap)
3821 goto error;
3822 bmap->dim = isl_space_as_set_space(bmap->dim);
3823 if (!bmap->dim)
3824 goto error;
3825 bmap = isl_basic_map_finalize(bmap);
3826 return (struct isl_basic_set *)bmap;
3827 error:
3828 isl_basic_map_free(bmap);
3829 return NULL;
3832 /* For a div d = floor(f/m), add the constraints
3834 * f - m d >= 0
3835 * -(f-(n-1)) + m d >= 0
3837 * Note that the second constraint is the negation of
3839 * f - m d >= n
3841 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3842 unsigned pos, isl_int *div)
3844 int i, j;
3845 unsigned total = isl_basic_map_total_dim(bmap);
3847 i = isl_basic_map_alloc_inequality(bmap);
3848 if (i < 0)
3849 return -1;
3850 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3851 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3853 j = isl_basic_map_alloc_inequality(bmap);
3854 if (j < 0)
3855 return -1;
3856 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3857 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3858 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3859 return j;
3862 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3863 unsigned pos, isl_int *div)
3865 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3866 pos, div);
3869 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3871 unsigned total = isl_basic_map_total_dim(bmap);
3872 unsigned div_pos = total - bmap->n_div + div;
3874 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3875 bmap->div[div]);
3878 struct isl_basic_set *isl_basic_map_underlying_set(
3879 struct isl_basic_map *bmap)
3881 if (!bmap)
3882 goto error;
3883 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3884 bmap->n_div == 0 &&
3885 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3886 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3887 return (struct isl_basic_set *)bmap;
3888 bmap = isl_basic_map_cow(bmap);
3889 if (!bmap)
3890 goto error;
3891 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3892 if (!bmap->dim)
3893 goto error;
3894 bmap->extra -= bmap->n_div;
3895 bmap->n_div = 0;
3896 bmap = isl_basic_map_finalize(bmap);
3897 return (struct isl_basic_set *)bmap;
3898 error:
3899 return NULL;
3902 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3903 __isl_take isl_basic_set *bset)
3905 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3908 struct isl_basic_map *isl_basic_map_overlying_set(
3909 struct isl_basic_set *bset, struct isl_basic_map *like)
3911 struct isl_basic_map *bmap;
3912 struct isl_ctx *ctx;
3913 unsigned total;
3914 int i;
3916 if (!bset || !like)
3917 goto error;
3918 ctx = bset->ctx;
3919 isl_assert(ctx, bset->n_div == 0, goto error);
3920 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3921 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3922 goto error);
3923 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3924 isl_basic_map_free(like);
3925 return (struct isl_basic_map *)bset;
3927 bset = isl_basic_set_cow(bset);
3928 if (!bset)
3929 goto error;
3930 total = bset->dim->n_out + bset->extra;
3931 bmap = (struct isl_basic_map *)bset;
3932 isl_space_free(bmap->dim);
3933 bmap->dim = isl_space_copy(like->dim);
3934 if (!bmap->dim)
3935 goto error;
3936 bmap->n_div = like->n_div;
3937 bmap->extra += like->n_div;
3938 if (bmap->extra) {
3939 unsigned ltotal;
3940 isl_int **div;
3941 ltotal = total - bmap->extra + like->extra;
3942 if (ltotal > total)
3943 ltotal = total;
3944 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3945 bmap->extra * (1 + 1 + total));
3946 if (isl_blk_is_error(bmap->block2))
3947 goto error;
3948 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3949 if (!div)
3950 goto error;
3951 bmap->div = div;
3952 for (i = 0; i < bmap->extra; ++i)
3953 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3954 for (i = 0; i < like->n_div; ++i) {
3955 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3956 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3958 bmap = isl_basic_map_extend_constraints(bmap,
3959 0, 2 * like->n_div);
3960 for (i = 0; i < like->n_div; ++i) {
3961 if (isl_int_is_zero(bmap->div[i][0]))
3962 continue;
3963 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3964 goto error;
3967 isl_basic_map_free(like);
3968 bmap = isl_basic_map_simplify(bmap);
3969 bmap = isl_basic_map_finalize(bmap);
3970 return bmap;
3971 error:
3972 isl_basic_map_free(like);
3973 isl_basic_set_free(bset);
3974 return NULL;
3977 struct isl_basic_set *isl_basic_set_from_underlying_set(
3978 struct isl_basic_set *bset, struct isl_basic_set *like)
3980 return (struct isl_basic_set *)
3981 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3984 struct isl_set *isl_set_from_underlying_set(
3985 struct isl_set *set, struct isl_basic_set *like)
3987 int i;
3989 if (!set || !like)
3990 goto error;
3991 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3992 goto error);
3993 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3994 isl_basic_set_free(like);
3995 return set;
3997 set = isl_set_cow(set);
3998 if (!set)
3999 goto error;
4000 for (i = 0; i < set->n; ++i) {
4001 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4002 isl_basic_set_copy(like));
4003 if (!set->p[i])
4004 goto error;
4006 isl_space_free(set->dim);
4007 set->dim = isl_space_copy(like->dim);
4008 if (!set->dim)
4009 goto error;
4010 isl_basic_set_free(like);
4011 return set;
4012 error:
4013 isl_basic_set_free(like);
4014 isl_set_free(set);
4015 return NULL;
4018 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4020 int i;
4022 map = isl_map_cow(map);
4023 if (!map)
4024 return NULL;
4025 map->dim = isl_space_cow(map->dim);
4026 if (!map->dim)
4027 goto error;
4029 for (i = 1; i < map->n; ++i)
4030 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4031 goto error);
4032 for (i = 0; i < map->n; ++i) {
4033 map->p[i] = (struct isl_basic_map *)
4034 isl_basic_map_underlying_set(map->p[i]);
4035 if (!map->p[i])
4036 goto error;
4038 if (map->n == 0)
4039 map->dim = isl_space_underlying(map->dim, 0);
4040 else {
4041 isl_space_free(map->dim);
4042 map->dim = isl_space_copy(map->p[0]->dim);
4044 if (!map->dim)
4045 goto error;
4046 return (struct isl_set *)map;
4047 error:
4048 isl_map_free(map);
4049 return NULL;
4052 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4054 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4057 __isl_give isl_basic_map *isl_basic_map_reset_space(
4058 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4060 bmap = isl_basic_map_cow(bmap);
4061 if (!bmap || !dim)
4062 goto error;
4064 isl_space_free(bmap->dim);
4065 bmap->dim = dim;
4067 bmap = isl_basic_map_finalize(bmap);
4069 return bmap;
4070 error:
4071 isl_basic_map_free(bmap);
4072 isl_space_free(dim);
4073 return NULL;
4076 __isl_give isl_basic_set *isl_basic_set_reset_space(
4077 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4079 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4080 dim);
4083 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4084 __isl_take isl_space *dim)
4086 int i;
4088 map = isl_map_cow(map);
4089 if (!map || !dim)
4090 goto error;
4092 for (i = 0; i < map->n; ++i) {
4093 map->p[i] = isl_basic_map_reset_space(map->p[i],
4094 isl_space_copy(dim));
4095 if (!map->p[i])
4096 goto error;
4098 isl_space_free(map->dim);
4099 map->dim = dim;
4101 return map;
4102 error:
4103 isl_map_free(map);
4104 isl_space_free(dim);
4105 return NULL;
4108 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4109 __isl_take isl_space *dim)
4111 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4114 /* Compute the parameter domain of the given basic set.
4116 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4118 isl_space *space;
4119 unsigned n;
4121 if (isl_basic_set_is_params(bset))
4122 return bset;
4124 n = isl_basic_set_dim(bset, isl_dim_set);
4125 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4126 space = isl_basic_set_get_space(bset);
4127 space = isl_space_params(space);
4128 bset = isl_basic_set_reset_space(bset, space);
4129 return bset;
4132 /* Compute the parameter domain of the given set.
4134 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4136 isl_space *space;
4137 unsigned n;
4139 if (isl_set_is_params(set))
4140 return set;
4142 n = isl_set_dim(set, isl_dim_set);
4143 set = isl_set_project_out(set, isl_dim_set, 0, n);
4144 space = isl_set_get_space(set);
4145 space = isl_space_params(space);
4146 set = isl_set_reset_space(set, space);
4147 return set;
4150 /* Construct a zero-dimensional set with the given parameter domain.
4152 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4154 isl_space *space;
4155 space = isl_set_get_space(set);
4156 space = isl_space_set_from_params(space);
4157 set = isl_set_reset_space(set, space);
4158 return set;
4161 /* Compute the parameter domain of the given map.
4163 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4165 isl_space *space;
4166 unsigned n;
4168 n = isl_map_dim(map, isl_dim_in);
4169 map = isl_map_project_out(map, isl_dim_in, 0, n);
4170 n = isl_map_dim(map, isl_dim_out);
4171 map = isl_map_project_out(map, isl_dim_out, 0, n);
4172 space = isl_map_get_space(map);
4173 space = isl_space_params(space);
4174 map = isl_map_reset_space(map, space);
4175 return map;
4178 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4180 isl_space *dim;
4181 struct isl_basic_set *domain;
4182 unsigned n_in;
4183 unsigned n_out;
4185 if (!bmap)
4186 return NULL;
4187 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4189 n_in = isl_basic_map_n_in(bmap);
4190 n_out = isl_basic_map_n_out(bmap);
4191 domain = isl_basic_set_from_basic_map(bmap);
4192 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4194 domain = isl_basic_set_reset_space(domain, dim);
4196 return domain;
4199 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4201 if (!bmap)
4202 return -1;
4203 return isl_space_may_be_set(bmap->dim);
4206 /* Is this basic map actually a set?
4207 * Users should never call this function. Outside of isl,
4208 * the type should indicate whether something is a set or a map.
4210 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4212 if (!bmap)
4213 return -1;
4214 return isl_space_is_set(bmap->dim);
4217 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4219 if (!bmap)
4220 return NULL;
4221 if (isl_basic_map_is_set(bmap))
4222 return bmap;
4223 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4226 __isl_give isl_basic_map *isl_basic_map_domain_map(
4227 __isl_take isl_basic_map *bmap)
4229 int i, k;
4230 isl_space *dim;
4231 isl_basic_map *domain;
4232 int nparam, n_in, n_out;
4233 unsigned total;
4235 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4236 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4237 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4239 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4240 domain = isl_basic_map_universe(dim);
4242 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4243 bmap = isl_basic_map_apply_range(bmap, domain);
4244 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4246 total = isl_basic_map_total_dim(bmap);
4248 for (i = 0; i < n_in; ++i) {
4249 k = isl_basic_map_alloc_equality(bmap);
4250 if (k < 0)
4251 goto error;
4252 isl_seq_clr(bmap->eq[k], 1 + total);
4253 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4254 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4257 bmap = isl_basic_map_gauss(bmap, NULL);
4258 return isl_basic_map_finalize(bmap);
4259 error:
4260 isl_basic_map_free(bmap);
4261 return NULL;
4264 __isl_give isl_basic_map *isl_basic_map_range_map(
4265 __isl_take isl_basic_map *bmap)
4267 int i, k;
4268 isl_space *dim;
4269 isl_basic_map *range;
4270 int nparam, n_in, n_out;
4271 unsigned total;
4273 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4274 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4275 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4277 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4278 range = isl_basic_map_universe(dim);
4280 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4281 bmap = isl_basic_map_apply_range(bmap, range);
4282 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4284 total = isl_basic_map_total_dim(bmap);
4286 for (i = 0; i < n_out; ++i) {
4287 k = isl_basic_map_alloc_equality(bmap);
4288 if (k < 0)
4289 goto error;
4290 isl_seq_clr(bmap->eq[k], 1 + total);
4291 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4292 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4295 bmap = isl_basic_map_gauss(bmap, NULL);
4296 return isl_basic_map_finalize(bmap);
4297 error:
4298 isl_basic_map_free(bmap);
4299 return NULL;
4302 int isl_map_may_be_set(__isl_keep isl_map *map)
4304 if (!map)
4305 return -1;
4306 return isl_space_may_be_set(map->dim);
4309 /* Is this map actually a set?
4310 * Users should never call this function. Outside of isl,
4311 * the type should indicate whether something is a set or a map.
4313 int isl_map_is_set(__isl_keep isl_map *map)
4315 if (!map)
4316 return -1;
4317 return isl_space_is_set(map->dim);
4320 struct isl_set *isl_map_range(struct isl_map *map)
4322 int i;
4323 struct isl_set *set;
4325 if (!map)
4326 goto error;
4327 if (isl_map_is_set(map))
4328 return (isl_set *)map;
4330 map = isl_map_cow(map);
4331 if (!map)
4332 goto error;
4334 set = (struct isl_set *) map;
4335 set->dim = isl_space_range(set->dim);
4336 if (!set->dim)
4337 goto error;
4338 for (i = 0; i < map->n; ++i) {
4339 set->p[i] = isl_basic_map_range(map->p[i]);
4340 if (!set->p[i])
4341 goto error;
4343 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4344 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4345 return set;
4346 error:
4347 isl_map_free(map);
4348 return NULL;
4351 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4353 int i;
4354 isl_space *domain_dim;
4356 map = isl_map_cow(map);
4357 if (!map)
4358 return NULL;
4360 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4361 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4362 map->dim = isl_space_join(map->dim, domain_dim);
4363 if (!map->dim)
4364 goto error;
4365 for (i = 0; i < map->n; ++i) {
4366 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4367 if (!map->p[i])
4368 goto error;
4370 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4372 return map;
4373 error:
4374 isl_map_free(map);
4375 return NULL;
4378 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4380 int i;
4381 isl_space *range_dim;
4383 map = isl_map_cow(map);
4384 if (!map)
4385 return NULL;
4387 range_dim = isl_space_range(isl_map_get_space(map));
4388 range_dim = isl_space_from_range(range_dim);
4389 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4390 map->dim = isl_space_join(map->dim, range_dim);
4391 if (!map->dim)
4392 goto error;
4393 for (i = 0; i < map->n; ++i) {
4394 map->p[i] = isl_basic_map_range_map(map->p[i]);
4395 if (!map->p[i])
4396 goto error;
4398 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4399 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4400 return map;
4401 error:
4402 isl_map_free(map);
4403 return NULL;
4406 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4407 __isl_take isl_space *dim)
4409 int i;
4410 struct isl_map *map = NULL;
4412 set = isl_set_cow(set);
4413 if (!set || !dim)
4414 goto error;
4415 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4416 map = (struct isl_map *)set;
4417 for (i = 0; i < set->n; ++i) {
4418 map->p[i] = isl_basic_map_from_basic_set(
4419 set->p[i], isl_space_copy(dim));
4420 if (!map->p[i])
4421 goto error;
4423 isl_space_free(map->dim);
4424 map->dim = dim;
4425 return map;
4426 error:
4427 isl_space_free(dim);
4428 isl_set_free(set);
4429 return NULL;
4432 __isl_give isl_basic_map *isl_basic_map_from_domain(
4433 __isl_take isl_basic_set *bset)
4435 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4438 __isl_give isl_basic_map *isl_basic_map_from_range(
4439 __isl_take isl_basic_set *bset)
4441 isl_space *space;
4442 space = isl_basic_set_get_space(bset);
4443 space = isl_space_from_range(space);
4444 bset = isl_basic_set_reset_space(bset, space);
4445 return (isl_basic_map *)bset;
4448 struct isl_map *isl_map_from_range(struct isl_set *set)
4450 isl_space *space;
4451 space = isl_set_get_space(set);
4452 space = isl_space_from_range(space);
4453 set = isl_set_reset_space(set, space);
4454 return (struct isl_map *)set;
4457 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4459 return isl_map_reverse(isl_map_from_range(set));
4462 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4463 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4465 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4468 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4469 __isl_take isl_set *range)
4471 return isl_map_apply_range(isl_map_reverse(domain), range);
4474 struct isl_set *isl_set_from_map(struct isl_map *map)
4476 int i;
4477 struct isl_set *set = NULL;
4479 if (!map)
4480 return NULL;
4481 map = isl_map_cow(map);
4482 if (!map)
4483 return NULL;
4484 map->dim = isl_space_as_set_space(map->dim);
4485 if (!map->dim)
4486 goto error;
4487 set = (struct isl_set *)map;
4488 for (i = 0; i < map->n; ++i) {
4489 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4490 if (!set->p[i])
4491 goto error;
4493 return set;
4494 error:
4495 isl_map_free(map);
4496 return NULL;
4499 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4500 unsigned flags)
4502 struct isl_map *map;
4504 if (!dim)
4505 return NULL;
4506 isl_assert(dim->ctx, n >= 0, return NULL);
4507 map = isl_alloc(dim->ctx, struct isl_map,
4508 sizeof(struct isl_map) +
4509 (n - 1) * sizeof(struct isl_basic_map *));
4510 if (!map)
4511 goto error;
4513 map->ctx = dim->ctx;
4514 isl_ctx_ref(map->ctx);
4515 map->ref = 1;
4516 map->size = n;
4517 map->n = 0;
4518 map->dim = dim;
4519 map->flags = flags;
4520 return map;
4521 error:
4522 isl_space_free(dim);
4523 return NULL;
4526 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4527 unsigned nparam, unsigned in, unsigned out, int n,
4528 unsigned flags)
4530 struct isl_map *map;
4531 isl_space *dims;
4533 dims = isl_space_alloc(ctx, nparam, in, out);
4534 if (!dims)
4535 return NULL;
4537 map = isl_map_alloc_space(dims, n, flags);
4538 return map;
4541 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4543 struct isl_basic_map *bmap;
4544 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4545 bmap = isl_basic_map_set_to_empty(bmap);
4546 return bmap;
4549 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4551 struct isl_basic_set *bset;
4552 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4553 bset = isl_basic_set_set_to_empty(bset);
4554 return bset;
4557 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4559 struct isl_basic_map *bmap;
4560 if (!model)
4561 return NULL;
4562 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4563 bmap = isl_basic_map_set_to_empty(bmap);
4564 return bmap;
4567 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4569 struct isl_basic_map *bmap;
4570 if (!model)
4571 return NULL;
4572 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4573 bmap = isl_basic_map_set_to_empty(bmap);
4574 return bmap;
4577 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4579 struct isl_basic_set *bset;
4580 if (!model)
4581 return NULL;
4582 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4583 bset = isl_basic_set_set_to_empty(bset);
4584 return bset;
4587 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4589 struct isl_basic_map *bmap;
4590 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4591 bmap = isl_basic_map_finalize(bmap);
4592 return bmap;
4595 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4597 struct isl_basic_set *bset;
4598 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4599 bset = isl_basic_set_finalize(bset);
4600 return bset;
4603 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4605 int i;
4606 unsigned total = isl_space_dim(dim, isl_dim_all);
4607 isl_basic_map *bmap;
4609 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4610 for (i = 0; i < total; ++i) {
4611 int k = isl_basic_map_alloc_inequality(bmap);
4612 if (k < 0)
4613 goto error;
4614 isl_seq_clr(bmap->ineq[k], 1 + total);
4615 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4617 return bmap;
4618 error:
4619 isl_basic_map_free(bmap);
4620 return NULL;
4623 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4625 return isl_basic_map_nat_universe(dim);
4628 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4630 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4633 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4635 return isl_map_nat_universe(dim);
4638 __isl_give isl_basic_map *isl_basic_map_universe_like(
4639 __isl_keep isl_basic_map *model)
4641 if (!model)
4642 return NULL;
4643 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4646 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4648 if (!model)
4649 return NULL;
4650 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4653 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4654 __isl_keep isl_set *model)
4656 if (!model)
4657 return NULL;
4658 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4661 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4663 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4666 struct isl_map *isl_map_empty_like(struct isl_map *model)
4668 if (!model)
4669 return NULL;
4670 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4673 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4675 if (!model)
4676 return NULL;
4677 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4680 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4682 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4685 struct isl_set *isl_set_empty_like(struct isl_set *model)
4687 if (!model)
4688 return NULL;
4689 return isl_set_empty(isl_space_copy(model->dim));
4692 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4694 struct isl_map *map;
4695 if (!dim)
4696 return NULL;
4697 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4698 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4699 return map;
4702 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4704 struct isl_set *set;
4705 if (!dim)
4706 return NULL;
4707 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4708 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4709 return set;
4712 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4714 if (!model)
4715 return NULL;
4716 return isl_set_universe(isl_space_copy(model->dim));
4719 struct isl_map *isl_map_dup(struct isl_map *map)
4721 int i;
4722 struct isl_map *dup;
4724 if (!map)
4725 return NULL;
4726 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4727 for (i = 0; i < map->n; ++i)
4728 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4729 return dup;
4732 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4733 __isl_take isl_basic_map *bmap)
4735 if (!bmap || !map)
4736 goto error;
4737 if (isl_basic_map_plain_is_empty(bmap)) {
4738 isl_basic_map_free(bmap);
4739 return map;
4741 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4742 isl_assert(map->ctx, map->n < map->size, goto error);
4743 map->p[map->n] = bmap;
4744 map->n++;
4745 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4746 return map;
4747 error:
4748 if (map)
4749 isl_map_free(map);
4750 if (bmap)
4751 isl_basic_map_free(bmap);
4752 return NULL;
4755 void isl_map_free(struct isl_map *map)
4757 int i;
4759 if (!map)
4760 return;
4762 if (--map->ref > 0)
4763 return;
4765 isl_ctx_deref(map->ctx);
4766 for (i = 0; i < map->n; ++i)
4767 isl_basic_map_free(map->p[i]);
4768 isl_space_free(map->dim);
4769 free(map);
4772 struct isl_map *isl_map_extend(struct isl_map *base,
4773 unsigned nparam, unsigned n_in, unsigned n_out)
4775 int i;
4777 base = isl_map_cow(base);
4778 if (!base)
4779 return NULL;
4781 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4782 if (!base->dim)
4783 goto error;
4784 for (i = 0; i < base->n; ++i) {
4785 base->p[i] = isl_basic_map_extend_space(base->p[i],
4786 isl_space_copy(base->dim), 0, 0, 0);
4787 if (!base->p[i])
4788 goto error;
4790 return base;
4791 error:
4792 isl_map_free(base);
4793 return NULL;
4796 struct isl_set *isl_set_extend(struct isl_set *base,
4797 unsigned nparam, unsigned dim)
4799 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4800 nparam, 0, dim);
4803 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4804 struct isl_basic_map *bmap, unsigned pos, int value)
4806 int j;
4808 bmap = isl_basic_map_cow(bmap);
4809 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4810 j = isl_basic_map_alloc_equality(bmap);
4811 if (j < 0)
4812 goto error;
4813 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4814 isl_int_set_si(bmap->eq[j][pos], -1);
4815 isl_int_set_si(bmap->eq[j][0], value);
4816 bmap = isl_basic_map_simplify(bmap);
4817 return isl_basic_map_finalize(bmap);
4818 error:
4819 isl_basic_map_free(bmap);
4820 return NULL;
4823 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4824 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4826 int j;
4828 bmap = isl_basic_map_cow(bmap);
4829 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4830 j = isl_basic_map_alloc_equality(bmap);
4831 if (j < 0)
4832 goto error;
4833 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4834 isl_int_set_si(bmap->eq[j][pos], -1);
4835 isl_int_set(bmap->eq[j][0], value);
4836 bmap = isl_basic_map_simplify(bmap);
4837 return isl_basic_map_finalize(bmap);
4838 error:
4839 isl_basic_map_free(bmap);
4840 return NULL;
4843 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4844 enum isl_dim_type type, unsigned pos, int value)
4846 if (!bmap)
4847 return NULL;
4848 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4849 return isl_basic_map_fix_pos_si(bmap,
4850 isl_basic_map_offset(bmap, type) + pos, value);
4851 error:
4852 isl_basic_map_free(bmap);
4853 return NULL;
4856 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4857 enum isl_dim_type type, unsigned pos, isl_int value)
4859 if (!bmap)
4860 return NULL;
4861 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4862 return isl_basic_map_fix_pos(bmap,
4863 isl_basic_map_offset(bmap, type) + pos, value);
4864 error:
4865 isl_basic_map_free(bmap);
4866 return NULL;
4869 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4870 enum isl_dim_type type, unsigned pos, int value)
4872 return (struct isl_basic_set *)
4873 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4874 type, pos, value);
4877 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4878 enum isl_dim_type type, unsigned pos, isl_int value)
4880 return (struct isl_basic_set *)
4881 isl_basic_map_fix((struct isl_basic_map *)bset,
4882 type, pos, value);
4885 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4886 unsigned input, int value)
4888 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4891 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4892 unsigned dim, int value)
4894 return (struct isl_basic_set *)
4895 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4896 isl_dim_set, dim, value);
4899 struct isl_map *isl_map_fix_si(struct isl_map *map,
4900 enum isl_dim_type type, unsigned pos, int value)
4902 int i;
4904 map = isl_map_cow(map);
4905 if (!map)
4906 return NULL;
4908 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4909 for (i = 0; i < map->n; ++i) {
4910 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4911 if (!map->p[i])
4912 goto error;
4914 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4915 return map;
4916 error:
4917 isl_map_free(map);
4918 return NULL;
4921 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4922 enum isl_dim_type type, unsigned pos, int value)
4924 return (struct isl_set *)
4925 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4928 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4929 enum isl_dim_type type, unsigned pos, isl_int value)
4931 int i;
4933 map = isl_map_cow(map);
4934 if (!map)
4935 return NULL;
4937 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4938 for (i = 0; i < map->n; ++i) {
4939 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4940 if (!map->p[i])
4941 goto error;
4943 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4944 return map;
4945 error:
4946 isl_map_free(map);
4947 return NULL;
4950 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4951 enum isl_dim_type type, unsigned pos, isl_int value)
4953 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4956 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4957 unsigned input, int value)
4959 return isl_map_fix_si(map, isl_dim_in, input, value);
4962 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4964 return (struct isl_set *)
4965 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4968 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4969 __isl_take isl_basic_map *bmap,
4970 enum isl_dim_type type, unsigned pos, int value)
4972 int j;
4974 if (!bmap)
4975 return NULL;
4976 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4977 pos += isl_basic_map_offset(bmap, type);
4978 bmap = isl_basic_map_cow(bmap);
4979 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4980 j = isl_basic_map_alloc_inequality(bmap);
4981 if (j < 0)
4982 goto error;
4983 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4984 isl_int_set_si(bmap->ineq[j][pos], 1);
4985 isl_int_set_si(bmap->ineq[j][0], -value);
4986 bmap = isl_basic_map_simplify(bmap);
4987 return isl_basic_map_finalize(bmap);
4988 error:
4989 isl_basic_map_free(bmap);
4990 return NULL;
4993 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4994 unsigned dim, isl_int value)
4996 int j;
4998 bset = isl_basic_set_cow(bset);
4999 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5000 j = isl_basic_set_alloc_inequality(bset);
5001 if (j < 0)
5002 goto error;
5003 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5004 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5005 isl_int_neg(bset->ineq[j][0], value);
5006 bset = isl_basic_set_simplify(bset);
5007 return isl_basic_set_finalize(bset);
5008 error:
5009 isl_basic_set_free(bset);
5010 return NULL;
5013 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5014 enum isl_dim_type type, unsigned pos, int value)
5016 int i;
5018 map = isl_map_cow(map);
5019 if (!map)
5020 return NULL;
5022 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5023 for (i = 0; i < map->n; ++i) {
5024 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
5025 type, pos, value);
5026 if (!map->p[i])
5027 goto error;
5029 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5030 return map;
5031 error:
5032 isl_map_free(map);
5033 return NULL;
5036 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5037 enum isl_dim_type type, unsigned pos, int value)
5039 return (struct isl_set *)
5040 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5043 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5044 isl_int value)
5046 int i;
5048 set = isl_set_cow(set);
5049 if (!set)
5050 return NULL;
5052 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5053 for (i = 0; i < set->n; ++i) {
5054 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5055 if (!set->p[i])
5056 goto error;
5058 return set;
5059 error:
5060 isl_set_free(set);
5061 return NULL;
5064 struct isl_map *isl_map_reverse(struct isl_map *map)
5066 int i;
5068 map = isl_map_cow(map);
5069 if (!map)
5070 return NULL;
5072 map->dim = isl_space_reverse(map->dim);
5073 if (!map->dim)
5074 goto error;
5075 for (i = 0; i < map->n; ++i) {
5076 map->p[i] = isl_basic_map_reverse(map->p[i]);
5077 if (!map->p[i])
5078 goto error;
5080 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5081 return map;
5082 error:
5083 isl_map_free(map);
5084 return NULL;
5087 static struct isl_map *isl_basic_map_partial_lexopt(
5088 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5089 struct isl_set **empty, int max)
5091 if (!bmap)
5092 goto error;
5093 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5094 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5095 else
5096 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5097 error:
5098 isl_basic_map_free(bmap);
5099 isl_basic_set_free(dom);
5100 if (empty)
5101 *empty = NULL;
5102 return NULL;
5105 struct isl_map *isl_basic_map_partial_lexmax(
5106 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5107 struct isl_set **empty)
5109 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5112 struct isl_map *isl_basic_map_partial_lexmin(
5113 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5114 struct isl_set **empty)
5116 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5119 struct isl_set *isl_basic_set_partial_lexmin(
5120 struct isl_basic_set *bset, struct isl_basic_set *dom,
5121 struct isl_set **empty)
5123 return (struct isl_set *)
5124 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5125 dom, empty);
5128 struct isl_set *isl_basic_set_partial_lexmax(
5129 struct isl_basic_set *bset, struct isl_basic_set *dom,
5130 struct isl_set **empty)
5132 return (struct isl_set *)
5133 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5134 dom, empty);
5137 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5138 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5139 __isl_give isl_set **empty)
5141 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5144 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5145 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5146 __isl_give isl_set **empty)
5148 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5151 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5152 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5153 __isl_give isl_set **empty)
5155 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5158 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5159 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5160 __isl_give isl_set **empty)
5162 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5165 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5166 __isl_take isl_basic_map *bmap, int max)
5168 isl_basic_set *dom = NULL;
5169 isl_space *dom_space;
5171 if (!bmap)
5172 goto error;
5173 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5174 dom = isl_basic_set_universe(dom_space);
5175 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5176 error:
5177 isl_basic_map_free(bmap);
5178 return NULL;
5181 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5182 __isl_take isl_basic_map *bmap)
5184 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5187 /* Given a basic map "bmap", compute the lexicographically minimal
5188 * (or maximal) image element for each domain element in dom.
5189 * Set *empty to those elements in dom that do not have an image element.
5191 * We first make sure the basic sets in dom are disjoint and then
5192 * simply collect the results over each of the basic sets separately.
5193 * We could probably improve the efficiency a bit by moving the union
5194 * domain down into the parametric integer programming.
5196 static __isl_give isl_map *basic_map_partial_lexopt(
5197 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5198 __isl_give isl_set **empty, int max)
5200 int i;
5201 struct isl_map *res;
5203 dom = isl_set_make_disjoint(dom);
5204 if (!dom)
5205 goto error;
5207 if (isl_set_plain_is_empty(dom)) {
5208 res = isl_map_empty_like_basic_map(bmap);
5209 *empty = isl_set_empty_like(dom);
5210 isl_set_free(dom);
5211 isl_basic_map_free(bmap);
5212 return res;
5215 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5216 isl_basic_set_copy(dom->p[0]), empty, max);
5218 for (i = 1; i < dom->n; ++i) {
5219 struct isl_map *res_i;
5220 struct isl_set *empty_i;
5222 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5223 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5225 res = isl_map_union_disjoint(res, res_i);
5226 *empty = isl_set_union_disjoint(*empty, empty_i);
5229 isl_set_free(dom);
5230 isl_basic_map_free(bmap);
5231 return res;
5232 error:
5233 *empty = NULL;
5234 isl_set_free(dom);
5235 isl_basic_map_free(bmap);
5236 return NULL;
5239 /* Given a map "map", compute the lexicographically minimal
5240 * (or maximal) image element for each domain element in dom.
5241 * Set *empty to those elements in dom that do not have an image element.
5243 * We first compute the lexicographically minimal or maximal element
5244 * in the first basic map. This results in a partial solution "res"
5245 * and a subset "todo" of dom that still need to be handled.
5246 * We then consider each of the remaining maps in "map" and successively
5247 * improve both "res" and "todo".
5249 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5250 * Assume we are computing the lexicographical maximum.
5251 * We first compute the lexicographically maximal element in basic map i.
5252 * This results in a partial solution res_i and a subset todo_i.
5253 * Then we combine these results with those obtain for the first k basic maps
5254 * to obtain a result that is valid for the first k+1 basic maps.
5255 * In particular, the set where there is no solution is the set where
5256 * there is no solution for the first k basic maps and also no solution
5257 * for the ith basic map, i.e.,
5259 * todo^i = todo^k * todo_i
5261 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5262 * solutions, arbitrarily breaking ties in favor of res^k.
5263 * That is, when res^k(a) >= res_i(a), we pick res^k and
5264 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5265 * the lexicographic order.)
5266 * In practice, we compute
5268 * res^k * (res_i . "<=")
5270 * and
5272 * res_i * (res^k . "<")
5274 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5275 * where only one of res^k and res_i provides a solution and we simply pick
5276 * that one, i.e.,
5278 * res^k * todo_i
5279 * and
5280 * res_i * todo^k
5282 * Note that we only compute these intersections when dom(res^k) intersects
5283 * dom(res_i). Otherwise, the only effect of these intersections is to
5284 * potentially break up res^k and res_i into smaller pieces.
5285 * We want to avoid such splintering as much as possible.
5286 * In fact, an earlier implementation of this function would look for
5287 * better results in the domain of res^k and for extra results in todo^k,
5288 * but this would always result in a splintering according to todo^k,
5289 * even when the domain of basic map i is disjoint from the domains of
5290 * the previous basic maps.
5292 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5293 __isl_take isl_map *map, __isl_take isl_set *dom,
5294 __isl_give isl_set **empty, int max)
5296 int i;
5297 struct isl_map *res;
5298 struct isl_set *todo;
5300 if (!map || !dom)
5301 goto error;
5303 if (isl_map_plain_is_empty(map)) {
5304 if (empty)
5305 *empty = dom;
5306 else
5307 isl_set_free(dom);
5308 return map;
5311 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5312 isl_set_copy(dom), &todo, max);
5314 for (i = 1; i < map->n; ++i) {
5315 isl_map *lt, *le;
5316 isl_map *res_i;
5317 isl_set *todo_i;
5318 isl_space *dim = isl_space_range(isl_map_get_space(res));
5320 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5321 isl_set_copy(dom), &todo_i, max);
5323 if (max) {
5324 lt = isl_map_lex_lt(isl_space_copy(dim));
5325 le = isl_map_lex_le(dim);
5326 } else {
5327 lt = isl_map_lex_gt(isl_space_copy(dim));
5328 le = isl_map_lex_ge(dim);
5330 lt = isl_map_apply_range(isl_map_copy(res), lt);
5331 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5332 le = isl_map_apply_range(isl_map_copy(res_i), le);
5333 le = isl_map_intersect(le, isl_map_copy(res));
5335 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5336 res = isl_map_intersect_domain(res,
5337 isl_set_copy(todo_i));
5338 res_i = isl_map_intersect_domain(res_i,
5339 isl_set_copy(todo));
5342 res = isl_map_union_disjoint(res, res_i);
5343 res = isl_map_union_disjoint(res, lt);
5344 res = isl_map_union_disjoint(res, le);
5346 todo = isl_set_intersect(todo, todo_i);
5349 isl_set_free(dom);
5350 isl_map_free(map);
5352 if (empty)
5353 *empty = todo;
5354 else
5355 isl_set_free(todo);
5357 return res;
5358 error:
5359 if (empty)
5360 *empty = NULL;
5361 isl_set_free(dom);
5362 isl_map_free(map);
5363 return NULL;
5366 /* Given a map "map", compute the lexicographically minimal
5367 * (or maximal) image element for each domain element in dom.
5368 * Set *empty to those elements in dom that do not have an image element.
5370 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5372 static __isl_give isl_map *isl_map_partial_lexopt(
5373 __isl_take isl_map *map, __isl_take isl_set *dom,
5374 __isl_give isl_set **empty, int max)
5376 if (!map || !dom)
5377 goto error;
5378 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5379 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5380 if (!isl_space_has_named_params(map->dim) ||
5381 !isl_space_has_named_params(dom->dim))
5382 isl_die(map->ctx, isl_error_invalid,
5383 "unaligned unnamed parameters", goto error);
5384 map = isl_map_align_params(map, isl_map_get_space(dom));
5385 dom = isl_map_align_params(dom, isl_map_get_space(map));
5386 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5387 error:
5388 if (empty)
5389 *empty = NULL;
5390 isl_set_free(dom);
5391 isl_map_free(map);
5392 return NULL;
5395 __isl_give isl_map *isl_map_partial_lexmax(
5396 __isl_take isl_map *map, __isl_take isl_set *dom,
5397 __isl_give isl_set **empty)
5399 return isl_map_partial_lexopt(map, dom, empty, 1);
5402 __isl_give isl_map *isl_map_partial_lexmin(
5403 __isl_take isl_map *map, __isl_take isl_set *dom,
5404 __isl_give isl_set **empty)
5406 return isl_map_partial_lexopt(map, dom, empty, 0);
5409 __isl_give isl_set *isl_set_partial_lexmin(
5410 __isl_take isl_set *set, __isl_take isl_set *dom,
5411 __isl_give isl_set **empty)
5413 return (struct isl_set *)
5414 isl_map_partial_lexmin((struct isl_map *)set,
5415 dom, empty);
5418 __isl_give isl_set *isl_set_partial_lexmax(
5419 __isl_take isl_set *set, __isl_take isl_set *dom,
5420 __isl_give isl_set **empty)
5422 return (struct isl_set *)
5423 isl_map_partial_lexmax((struct isl_map *)set,
5424 dom, empty);
5427 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5429 struct isl_basic_set *dom = NULL;
5430 isl_space *dom_dim;
5432 if (!bmap)
5433 goto error;
5434 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5435 dom = isl_basic_set_universe(dom_dim);
5436 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5437 error:
5438 isl_basic_map_free(bmap);
5439 return NULL;
5442 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5444 return isl_basic_map_lexopt(bmap, 0);
5447 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5449 return isl_basic_map_lexopt(bmap, 1);
5452 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5454 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5457 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5459 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5462 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5464 struct isl_set *dom = NULL;
5465 isl_space *dom_dim;
5467 if (!map)
5468 goto error;
5469 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5470 dom = isl_set_universe(dom_dim);
5471 return isl_map_partial_lexopt(map, dom, NULL, max);
5472 error:
5473 isl_map_free(map);
5474 return NULL;
5477 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5479 return isl_map_lexopt(map, 0);
5482 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5484 return isl_map_lexopt(map, 1);
5487 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5489 return (isl_set *)isl_map_lexmin((isl_map *)set);
5492 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5494 return (isl_set *)isl_map_lexmax((isl_map *)set);
5497 /* Extract the first and only affine expression from list
5498 * and then add it to *pwaff with the given dom.
5499 * This domain is known to be disjoint from other domains
5500 * because of the way isl_basic_map_foreach_lexmax works.
5502 static int update_dim_opt(__isl_take isl_basic_set *dom,
5503 __isl_take isl_aff_list *list, void *user)
5505 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5506 isl_aff *aff;
5507 isl_pw_aff **pwaff = user;
5508 isl_pw_aff *pwaff_i;
5510 if (isl_aff_list_n_aff(list) != 1)
5511 isl_die(ctx, isl_error_internal,
5512 "expecting single element list", goto error);
5514 aff = isl_aff_list_get_aff(list, 0);
5515 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5517 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5519 isl_aff_list_free(list);
5521 return 0;
5522 error:
5523 isl_basic_set_free(dom);
5524 isl_aff_list_free(list);
5525 return -1;
5528 /* Given a basic map with one output dimension, compute the minimum or
5529 * maximum of that dimension as an isl_pw_aff.
5531 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5532 * call update_dim_opt on each leaf of the result.
5534 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5535 int max)
5537 isl_space *dim = isl_basic_map_get_space(bmap);
5538 isl_pw_aff *pwaff;
5539 int r;
5541 dim = isl_space_from_domain(isl_space_domain(dim));
5542 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5543 pwaff = isl_pw_aff_empty(dim);
5545 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5546 if (r < 0)
5547 return isl_pw_aff_free(pwaff);
5549 return pwaff;
5552 /* Compute the minimum or maximum of the given output dimension
5553 * as a function of the parameters and the input dimensions,
5554 * but independently of the other output dimensions.
5556 * We first project out the other output dimension and then compute
5557 * the "lexicographic" maximum in each basic map, combining the results
5558 * using isl_pw_aff_union_max.
5560 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5561 int max)
5563 int i;
5564 isl_pw_aff *pwaff;
5565 unsigned n_out;
5567 n_out = isl_map_dim(map, isl_dim_out);
5568 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5569 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5570 if (!map)
5571 return NULL;
5573 if (map->n == 0) {
5574 isl_space *dim = isl_map_get_space(map);
5575 dim = isl_space_domain(isl_space_from_range(dim));
5576 isl_map_free(map);
5577 return isl_pw_aff_empty(dim);
5580 pwaff = basic_map_dim_opt(map->p[0], max);
5581 for (i = 1; i < map->n; ++i) {
5582 isl_pw_aff *pwaff_i;
5584 pwaff_i = basic_map_dim_opt(map->p[i], max);
5585 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5588 isl_map_free(map);
5590 return pwaff;
5593 /* Compute the maximum of the given output dimension as a function of the
5594 * parameters and input dimensions, but independently of
5595 * the other output dimensions.
5597 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5599 return map_dim_opt(map, pos, 1);
5602 /* Compute the minimum or maximum of the given set dimension
5603 * as a function of the parameters,
5604 * but independently of the other set dimensions.
5606 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5607 int max)
5609 return map_dim_opt(set, pos, max);
5612 /* Compute the maximum of the given set dimension as a function of the
5613 * parameters, but independently of the other set dimensions.
5615 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5617 return set_dim_opt(set, pos, 1);
5620 /* Compute the minimum of the given set dimension as a function of the
5621 * parameters, but independently of the other set dimensions.
5623 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5625 return set_dim_opt(set, pos, 0);
5628 /* Apply a preimage specified by "mat" on the parameters of "bset".
5629 * bset is assumed to have only parameters and divs.
5631 static struct isl_basic_set *basic_set_parameter_preimage(
5632 struct isl_basic_set *bset, struct isl_mat *mat)
5634 unsigned nparam;
5636 if (!bset || !mat)
5637 goto error;
5639 bset->dim = isl_space_cow(bset->dim);
5640 if (!bset->dim)
5641 goto error;
5643 nparam = isl_basic_set_dim(bset, isl_dim_param);
5645 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5647 bset->dim->nparam = 0;
5648 bset->dim->n_out = nparam;
5649 bset = isl_basic_set_preimage(bset, mat);
5650 if (bset) {
5651 bset->dim->nparam = bset->dim->n_out;
5652 bset->dim->n_out = 0;
5654 return bset;
5655 error:
5656 isl_mat_free(mat);
5657 isl_basic_set_free(bset);
5658 return NULL;
5661 /* Apply a preimage specified by "mat" on the parameters of "set".
5662 * set is assumed to have only parameters and divs.
5664 static struct isl_set *set_parameter_preimage(
5665 struct isl_set *set, struct isl_mat *mat)
5667 isl_space *dim = NULL;
5668 unsigned nparam;
5670 if (!set || !mat)
5671 goto error;
5673 dim = isl_space_copy(set->dim);
5674 dim = isl_space_cow(dim);
5675 if (!dim)
5676 goto error;
5678 nparam = isl_set_dim(set, isl_dim_param);
5680 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5682 dim->nparam = 0;
5683 dim->n_out = nparam;
5684 isl_set_reset_space(set, dim);
5685 set = isl_set_preimage(set, mat);
5686 if (!set)
5687 goto error2;
5688 dim = isl_space_copy(set->dim);
5689 dim = isl_space_cow(dim);
5690 if (!dim)
5691 goto error2;
5692 dim->nparam = dim->n_out;
5693 dim->n_out = 0;
5694 isl_set_reset_space(set, dim);
5695 return set;
5696 error:
5697 isl_space_free(dim);
5698 isl_mat_free(mat);
5699 error2:
5700 isl_set_free(set);
5701 return NULL;
5704 /* Intersect the basic set "bset" with the affine space specified by the
5705 * equalities in "eq".
5707 static struct isl_basic_set *basic_set_append_equalities(
5708 struct isl_basic_set *bset, struct isl_mat *eq)
5710 int i, k;
5711 unsigned len;
5713 if (!bset || !eq)
5714 goto error;
5716 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5717 eq->n_row, 0);
5718 if (!bset)
5719 goto error;
5721 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5722 for (i = 0; i < eq->n_row; ++i) {
5723 k = isl_basic_set_alloc_equality(bset);
5724 if (k < 0)
5725 goto error;
5726 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5727 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5729 isl_mat_free(eq);
5731 bset = isl_basic_set_gauss(bset, NULL);
5732 bset = isl_basic_set_finalize(bset);
5734 return bset;
5735 error:
5736 isl_mat_free(eq);
5737 isl_basic_set_free(bset);
5738 return NULL;
5741 /* Intersect the set "set" with the affine space specified by the
5742 * equalities in "eq".
5744 static struct isl_set *set_append_equalities(struct isl_set *set,
5745 struct isl_mat *eq)
5747 int i;
5749 if (!set || !eq)
5750 goto error;
5752 for (i = 0; i < set->n; ++i) {
5753 set->p[i] = basic_set_append_equalities(set->p[i],
5754 isl_mat_copy(eq));
5755 if (!set->p[i])
5756 goto error;
5758 isl_mat_free(eq);
5759 return set;
5760 error:
5761 isl_mat_free(eq);
5762 isl_set_free(set);
5763 return NULL;
5766 /* Project the given basic set onto its parameter domain, possibly introducing
5767 * new, explicit, existential variables in the constraints.
5768 * The input has parameters and (possibly implicit) existential variables.
5769 * The output has the same parameters, but only
5770 * explicit existentially quantified variables.
5772 * The actual projection is performed by pip, but pip doesn't seem
5773 * to like equalities very much, so we first remove the equalities
5774 * among the parameters by performing a variable compression on
5775 * the parameters. Afterward, an inverse transformation is performed
5776 * and the equalities among the parameters are inserted back in.
5778 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5780 int i, j;
5781 struct isl_mat *eq;
5782 struct isl_mat *T, *T2;
5783 struct isl_set *set;
5784 unsigned nparam, n_div;
5786 bset = isl_basic_set_cow(bset);
5787 if (!bset)
5788 return NULL;
5790 if (bset->n_eq == 0)
5791 return isl_basic_set_lexmin(bset);
5793 isl_basic_set_gauss(bset, NULL);
5795 nparam = isl_basic_set_dim(bset, isl_dim_param);
5796 n_div = isl_basic_set_dim(bset, isl_dim_div);
5798 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5799 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5800 ++i;
5802 if (i == bset->n_eq)
5803 return isl_basic_set_lexmin(bset);
5805 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5806 0, 1 + nparam);
5807 eq = isl_mat_cow(eq);
5808 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5809 if (T && T->n_col == 0) {
5810 isl_mat_free(T);
5811 isl_mat_free(T2);
5812 isl_mat_free(eq);
5813 bset = isl_basic_set_set_to_empty(bset);
5814 return isl_set_from_basic_set(bset);
5816 bset = basic_set_parameter_preimage(bset, T);
5818 set = isl_basic_set_lexmin(bset);
5819 set = set_parameter_preimage(set, T2);
5820 set = set_append_equalities(set, eq);
5821 return set;
5824 /* Compute an explicit representation for all the existentially
5825 * quantified variables.
5826 * The input and output dimensions are first turned into parameters.
5827 * compute_divs then returns a map with the same parameters and
5828 * no input or output dimensions and the dimension specification
5829 * is reset to that of the input.
5831 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5833 struct isl_basic_set *bset;
5834 struct isl_set *set;
5835 struct isl_map *map;
5836 isl_space *dim, *orig_dim = NULL;
5837 unsigned nparam;
5838 unsigned n_in;
5839 unsigned n_out;
5841 bmap = isl_basic_map_cow(bmap);
5842 if (!bmap)
5843 return NULL;
5845 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5846 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5847 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5848 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5849 if (!dim)
5850 goto error;
5852 orig_dim = bmap->dim;
5853 bmap->dim = dim;
5854 bset = (struct isl_basic_set *)bmap;
5856 set = parameter_compute_divs(bset);
5857 map = (struct isl_map *)set;
5858 map = isl_map_reset_space(map, orig_dim);
5860 return map;
5861 error:
5862 isl_basic_map_free(bmap);
5863 return NULL;
5866 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5868 int i;
5869 unsigned off;
5871 if (!bmap)
5872 return -1;
5874 off = isl_space_dim(bmap->dim, isl_dim_all);
5875 for (i = 0; i < bmap->n_div; ++i) {
5876 if (isl_int_is_zero(bmap->div[i][0]))
5877 return 0;
5878 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5879 return -1);
5881 return 1;
5884 static int map_divs_known(__isl_keep isl_map *map)
5886 int i;
5888 if (!map)
5889 return -1;
5891 for (i = 0; i < map->n; ++i) {
5892 int known = isl_basic_map_divs_known(map->p[i]);
5893 if (known <= 0)
5894 return known;
5897 return 1;
5900 /* If bmap contains any unknown divs, then compute explicit
5901 * expressions for them. However, this computation may be
5902 * quite expensive, so first try to remove divs that aren't
5903 * strictly needed.
5905 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5907 int known;
5908 struct isl_map *map;
5910 known = isl_basic_map_divs_known(bmap);
5911 if (known < 0)
5912 goto error;
5913 if (known)
5914 return isl_map_from_basic_map(bmap);
5916 bmap = isl_basic_map_drop_redundant_divs(bmap);
5918 known = isl_basic_map_divs_known(bmap);
5919 if (known < 0)
5920 goto error;
5921 if (known)
5922 return isl_map_from_basic_map(bmap);
5924 map = compute_divs(bmap);
5925 return map;
5926 error:
5927 isl_basic_map_free(bmap);
5928 return NULL;
5931 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5933 int i;
5934 int known;
5935 struct isl_map *res;
5937 if (!map)
5938 return NULL;
5939 if (map->n == 0)
5940 return map;
5942 known = map_divs_known(map);
5943 if (known < 0) {
5944 isl_map_free(map);
5945 return NULL;
5947 if (known)
5948 return map;
5950 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5951 for (i = 1 ; i < map->n; ++i) {
5952 struct isl_map *r2;
5953 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5954 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5955 res = isl_map_union_disjoint(res, r2);
5956 else
5957 res = isl_map_union(res, r2);
5959 isl_map_free(map);
5961 return res;
5964 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5966 return (struct isl_set *)
5967 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5970 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5972 return (struct isl_set *)
5973 isl_map_compute_divs((struct isl_map *)set);
5976 struct isl_set *isl_map_domain(struct isl_map *map)
5978 int i;
5979 struct isl_set *set;
5981 if (!map)
5982 goto error;
5984 map = isl_map_cow(map);
5985 if (!map)
5986 return NULL;
5988 set = (struct isl_set *)map;
5989 set->dim = isl_space_domain(set->dim);
5990 if (!set->dim)
5991 goto error;
5992 for (i = 0; i < map->n; ++i) {
5993 set->p[i] = isl_basic_map_domain(map->p[i]);
5994 if (!set->p[i])
5995 goto error;
5997 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5998 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5999 return set;
6000 error:
6001 isl_map_free(map);
6002 return NULL;
6005 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6006 __isl_take isl_map *map2)
6008 int i;
6009 unsigned flags = 0;
6010 struct isl_map *map = NULL;
6012 if (!map1 || !map2)
6013 goto error;
6015 if (map1->n == 0) {
6016 isl_map_free(map1);
6017 return map2;
6019 if (map2->n == 0) {
6020 isl_map_free(map2);
6021 return map1;
6024 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6026 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6027 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6028 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6030 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6031 map1->n + map2->n, flags);
6032 if (!map)
6033 goto error;
6034 for (i = 0; i < map1->n; ++i) {
6035 map = isl_map_add_basic_map(map,
6036 isl_basic_map_copy(map1->p[i]));
6037 if (!map)
6038 goto error;
6040 for (i = 0; i < map2->n; ++i) {
6041 map = isl_map_add_basic_map(map,
6042 isl_basic_map_copy(map2->p[i]));
6043 if (!map)
6044 goto error;
6046 isl_map_free(map1);
6047 isl_map_free(map2);
6048 return map;
6049 error:
6050 isl_map_free(map);
6051 isl_map_free(map1);
6052 isl_map_free(map2);
6053 return NULL;
6056 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6057 __isl_take isl_map *map2)
6059 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6062 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6064 map1 = isl_map_union_disjoint(map1, map2);
6065 if (!map1)
6066 return NULL;
6067 if (map1->n > 1)
6068 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6069 return map1;
6072 struct isl_set *isl_set_union_disjoint(
6073 struct isl_set *set1, struct isl_set *set2)
6075 return (struct isl_set *)
6076 isl_map_union_disjoint(
6077 (struct isl_map *)set1, (struct isl_map *)set2);
6080 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6082 return (struct isl_set *)
6083 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6086 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6087 __isl_take isl_set *set)
6089 unsigned flags = 0;
6090 struct isl_map *result;
6091 int i, j;
6093 if (!map || !set)
6094 goto error;
6096 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6097 isl_die(set->ctx, isl_error_invalid,
6098 "parameters don't match", goto error);
6100 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6101 !isl_map_compatible_range(map, set))
6102 isl_die(set->ctx, isl_error_invalid,
6103 "incompatible spaces", goto error);
6105 if (isl_set_plain_is_universe(set)) {
6106 isl_set_free(set);
6107 return map;
6110 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6111 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6112 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6114 result = isl_map_alloc_space(isl_space_copy(map->dim),
6115 map->n * set->n, flags);
6116 if (!result)
6117 goto error;
6118 for (i = 0; i < map->n; ++i)
6119 for (j = 0; j < set->n; ++j) {
6120 result = isl_map_add_basic_map(result,
6121 isl_basic_map_intersect_range(
6122 isl_basic_map_copy(map->p[i]),
6123 isl_basic_set_copy(set->p[j])));
6124 if (!result)
6125 goto error;
6127 isl_map_free(map);
6128 isl_set_free(set);
6129 return result;
6130 error:
6131 isl_map_free(map);
6132 isl_set_free(set);
6133 return NULL;
6136 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6137 __isl_take isl_set *set)
6139 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6142 struct isl_map *isl_map_intersect_domain(
6143 struct isl_map *map, struct isl_set *set)
6145 return isl_map_reverse(
6146 isl_map_intersect_range(isl_map_reverse(map), set));
6149 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6150 __isl_take isl_map *map2)
6152 if (!map1 || !map2)
6153 goto error;
6154 map1 = isl_map_reverse(map1);
6155 map1 = isl_map_apply_range(map1, map2);
6156 return isl_map_reverse(map1);
6157 error:
6158 isl_map_free(map1);
6159 isl_map_free(map2);
6160 return NULL;
6163 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6164 __isl_take isl_map *map2)
6166 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6169 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6170 __isl_take isl_map *map2)
6172 isl_space *dim_result;
6173 struct isl_map *result;
6174 int i, j;
6176 if (!map1 || !map2)
6177 goto error;
6179 dim_result = isl_space_join(isl_space_copy(map1->dim),
6180 isl_space_copy(map2->dim));
6182 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6183 if (!result)
6184 goto error;
6185 for (i = 0; i < map1->n; ++i)
6186 for (j = 0; j < map2->n; ++j) {
6187 result = isl_map_add_basic_map(result,
6188 isl_basic_map_apply_range(
6189 isl_basic_map_copy(map1->p[i]),
6190 isl_basic_map_copy(map2->p[j])));
6191 if (!result)
6192 goto error;
6194 isl_map_free(map1);
6195 isl_map_free(map2);
6196 if (result && result->n <= 1)
6197 ISL_F_SET(result, ISL_MAP_DISJOINT);
6198 return result;
6199 error:
6200 isl_map_free(map1);
6201 isl_map_free(map2);
6202 return NULL;
6205 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6206 __isl_take isl_map *map2)
6208 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6212 * returns range - domain
6214 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6216 isl_space *dims, *target_dim;
6217 struct isl_basic_set *bset;
6218 unsigned dim;
6219 unsigned nparam;
6220 int i;
6222 if (!bmap)
6223 goto error;
6224 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6225 bmap->dim, isl_dim_out),
6226 goto error);
6227 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6228 dim = isl_basic_map_n_in(bmap);
6229 nparam = isl_basic_map_n_param(bmap);
6230 bset = isl_basic_set_from_basic_map(bmap);
6231 bset = isl_basic_set_cow(bset);
6232 dims = isl_basic_set_get_space(bset);
6233 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6234 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6235 bset = isl_basic_set_swap_vars(bset, 2*dim);
6236 for (i = 0; i < dim; ++i) {
6237 int j = isl_basic_map_alloc_equality(
6238 (struct isl_basic_map *)bset);
6239 if (j < 0)
6240 goto error;
6241 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6242 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6243 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6244 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6246 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6247 bset = isl_basic_set_reset_space(bset, target_dim);
6248 return bset;
6249 error:
6250 isl_basic_map_free(bmap);
6251 return NULL;
6255 * returns range - domain
6257 struct isl_set *isl_map_deltas(struct isl_map *map)
6259 int i;
6260 isl_space *dim;
6261 struct isl_set *result;
6263 if (!map)
6264 return NULL;
6266 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6267 map->dim, isl_dim_out),
6268 goto error);
6269 dim = isl_map_get_space(map);
6270 dim = isl_space_domain(dim);
6271 result = isl_set_alloc_space(dim, map->n, 0);
6272 if (!result)
6273 goto error;
6274 for (i = 0; i < map->n; ++i)
6275 result = isl_set_add_basic_set(result,
6276 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6277 isl_map_free(map);
6278 return result;
6279 error:
6280 isl_map_free(map);
6281 return NULL;
6285 * returns [domain -> range] -> range - domain
6287 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6288 __isl_take isl_basic_map *bmap)
6290 int i, k;
6291 isl_space *dim;
6292 isl_basic_map *domain;
6293 int nparam, n;
6294 unsigned total;
6296 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6297 isl_die(bmap->ctx, isl_error_invalid,
6298 "domain and range don't match", goto error);
6300 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6301 n = isl_basic_map_dim(bmap, isl_dim_in);
6303 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6304 domain = isl_basic_map_universe(dim);
6306 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6307 bmap = isl_basic_map_apply_range(bmap, domain);
6308 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6310 total = isl_basic_map_total_dim(bmap);
6312 for (i = 0; i < n; ++i) {
6313 k = isl_basic_map_alloc_equality(bmap);
6314 if (k < 0)
6315 goto error;
6316 isl_seq_clr(bmap->eq[k], 1 + total);
6317 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6318 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6319 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6322 bmap = isl_basic_map_gauss(bmap, NULL);
6323 return isl_basic_map_finalize(bmap);
6324 error:
6325 isl_basic_map_free(bmap);
6326 return NULL;
6330 * returns [domain -> range] -> range - domain
6332 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6334 int i;
6335 isl_space *domain_dim;
6337 if (!map)
6338 return NULL;
6340 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6341 isl_die(map->ctx, isl_error_invalid,
6342 "domain and range don't match", goto error);
6344 map = isl_map_cow(map);
6345 if (!map)
6346 return NULL;
6348 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6349 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6350 map->dim = isl_space_join(map->dim, domain_dim);
6351 if (!map->dim)
6352 goto error;
6353 for (i = 0; i < map->n; ++i) {
6354 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6355 if (!map->p[i])
6356 goto error;
6358 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6359 return map;
6360 error:
6361 isl_map_free(map);
6362 return NULL;
6365 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6367 struct isl_basic_map *bmap;
6368 unsigned nparam;
6369 unsigned dim;
6370 int i;
6372 if (!dims)
6373 return NULL;
6375 nparam = dims->nparam;
6376 dim = dims->n_out;
6377 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6378 if (!bmap)
6379 goto error;
6381 for (i = 0; i < dim; ++i) {
6382 int j = isl_basic_map_alloc_equality(bmap);
6383 if (j < 0)
6384 goto error;
6385 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6386 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6387 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6389 return isl_basic_map_finalize(bmap);
6390 error:
6391 isl_basic_map_free(bmap);
6392 return NULL;
6395 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6397 if (!dim)
6398 return NULL;
6399 if (dim->n_in != dim->n_out)
6400 isl_die(dim->ctx, isl_error_invalid,
6401 "number of input and output dimensions needs to be "
6402 "the same", goto error);
6403 return basic_map_identity(dim);
6404 error:
6405 isl_space_free(dim);
6406 return NULL;
6409 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6411 if (!model || !model->dim)
6412 return NULL;
6413 return isl_basic_map_identity(isl_space_copy(model->dim));
6416 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6418 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6421 struct isl_map *isl_map_identity_like(struct isl_map *model)
6423 if (!model || !model->dim)
6424 return NULL;
6425 return isl_map_identity(isl_space_copy(model->dim));
6428 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6430 if (!model || !model->dim)
6431 return NULL;
6432 return isl_map_identity(isl_space_copy(model->dim));
6435 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6437 isl_space *dim = isl_set_get_space(set);
6438 isl_map *id;
6439 id = isl_map_identity(isl_space_map_from_set(dim));
6440 return isl_map_intersect_range(id, set);
6443 /* Construct a basic set with all set dimensions having only non-negative
6444 * values.
6446 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6447 __isl_take isl_space *space)
6449 int i;
6450 unsigned nparam;
6451 unsigned dim;
6452 struct isl_basic_set *bset;
6454 if (!space)
6455 return NULL;
6456 nparam = space->nparam;
6457 dim = space->n_out;
6458 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6459 if (!bset)
6460 return NULL;
6461 for (i = 0; i < dim; ++i) {
6462 int k = isl_basic_set_alloc_inequality(bset);
6463 if (k < 0)
6464 goto error;
6465 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6466 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6468 return bset;
6469 error:
6470 isl_basic_set_free(bset);
6471 return NULL;
6474 /* Construct the half-space x_pos >= 0.
6476 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6477 int pos)
6479 int k;
6480 isl_basic_set *nonneg;
6482 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6483 k = isl_basic_set_alloc_inequality(nonneg);
6484 if (k < 0)
6485 goto error;
6486 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6487 isl_int_set_si(nonneg->ineq[k][pos], 1);
6489 return isl_basic_set_finalize(nonneg);
6490 error:
6491 isl_basic_set_free(nonneg);
6492 return NULL;
6495 /* Construct the half-space x_pos <= -1.
6497 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6499 int k;
6500 isl_basic_set *neg;
6502 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6503 k = isl_basic_set_alloc_inequality(neg);
6504 if (k < 0)
6505 goto error;
6506 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6507 isl_int_set_si(neg->ineq[k][0], -1);
6508 isl_int_set_si(neg->ineq[k][pos], -1);
6510 return isl_basic_set_finalize(neg);
6511 error:
6512 isl_basic_set_free(neg);
6513 return NULL;
6516 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6517 enum isl_dim_type type, unsigned first, unsigned n)
6519 int i;
6520 isl_basic_set *nonneg;
6521 isl_basic_set *neg;
6523 if (!set)
6524 return NULL;
6525 if (n == 0)
6526 return set;
6528 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6530 for (i = 0; i < n; ++i) {
6531 nonneg = nonneg_halfspace(isl_set_get_space(set),
6532 pos(set->dim, type) + first + i);
6533 neg = neg_halfspace(isl_set_get_space(set),
6534 pos(set->dim, type) + first + i);
6536 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6539 return set;
6540 error:
6541 isl_set_free(set);
6542 return NULL;
6545 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6546 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6547 void *user)
6549 isl_set *half;
6551 if (!set)
6552 return -1;
6553 if (isl_set_plain_is_empty(set)) {
6554 isl_set_free(set);
6555 return 0;
6557 if (first == len)
6558 return fn(set, signs, user);
6560 signs[first] = 1;
6561 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6562 1 + first));
6563 half = isl_set_intersect(half, isl_set_copy(set));
6564 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6565 goto error;
6567 signs[first] = -1;
6568 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6569 1 + first));
6570 half = isl_set_intersect(half, set);
6571 return foreach_orthant(half, signs, first + 1, len, fn, user);
6572 error:
6573 isl_set_free(set);
6574 return -1;
6577 /* Call "fn" on the intersections of "set" with each of the orthants
6578 * (except for obviously empty intersections). The orthant is identified
6579 * by the signs array, with each entry having value 1 or -1 according
6580 * to the sign of the corresponding variable.
6582 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6583 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6584 void *user)
6586 unsigned nparam;
6587 unsigned nvar;
6588 int *signs;
6589 int r;
6591 if (!set)
6592 return -1;
6593 if (isl_set_plain_is_empty(set))
6594 return 0;
6596 nparam = isl_set_dim(set, isl_dim_param);
6597 nvar = isl_set_dim(set, isl_dim_set);
6599 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6601 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6602 fn, user);
6604 free(signs);
6606 return r;
6609 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6611 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6614 int isl_basic_map_is_subset(
6615 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6617 int is_subset;
6618 struct isl_map *map1;
6619 struct isl_map *map2;
6621 if (!bmap1 || !bmap2)
6622 return -1;
6624 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6625 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6627 is_subset = isl_map_is_subset(map1, map2);
6629 isl_map_free(map1);
6630 isl_map_free(map2);
6632 return is_subset;
6635 int isl_basic_map_is_equal(
6636 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6638 int is_subset;
6640 if (!bmap1 || !bmap2)
6641 return -1;
6642 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6643 if (is_subset != 1)
6644 return is_subset;
6645 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6646 return is_subset;
6649 int isl_basic_set_is_equal(
6650 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6652 return isl_basic_map_is_equal(
6653 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6656 int isl_map_is_empty(struct isl_map *map)
6658 int i;
6659 int is_empty;
6661 if (!map)
6662 return -1;
6663 for (i = 0; i < map->n; ++i) {
6664 is_empty = isl_basic_map_is_empty(map->p[i]);
6665 if (is_empty < 0)
6666 return -1;
6667 if (!is_empty)
6668 return 0;
6670 return 1;
6673 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6675 return map ? map->n == 0 : -1;
6678 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6680 return isl_map_plain_is_empty(map);
6683 int isl_set_plain_is_empty(struct isl_set *set)
6685 return set ? set->n == 0 : -1;
6688 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6690 return isl_set_plain_is_empty(set);
6693 int isl_set_is_empty(struct isl_set *set)
6695 return isl_map_is_empty((struct isl_map *)set);
6698 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6700 if (!map1 || !map2)
6701 return -1;
6703 return isl_space_is_equal(map1->dim, map2->dim);
6706 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6708 if (!set1 || !set2)
6709 return -1;
6711 return isl_space_is_equal(set1->dim, set2->dim);
6714 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6716 int is_subset;
6718 if (!map1 || !map2)
6719 return -1;
6720 is_subset = isl_map_is_subset(map1, map2);
6721 if (is_subset != 1)
6722 return is_subset;
6723 is_subset = isl_map_is_subset(map2, map1);
6724 return is_subset;
6727 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6729 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6732 int isl_basic_map_is_strict_subset(
6733 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6735 int is_subset;
6737 if (!bmap1 || !bmap2)
6738 return -1;
6739 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6740 if (is_subset != 1)
6741 return is_subset;
6742 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6743 if (is_subset == -1)
6744 return is_subset;
6745 return !is_subset;
6748 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6750 int is_subset;
6752 if (!map1 || !map2)
6753 return -1;
6754 is_subset = isl_map_is_subset(map1, map2);
6755 if (is_subset != 1)
6756 return is_subset;
6757 is_subset = isl_map_is_subset(map2, map1);
6758 if (is_subset == -1)
6759 return is_subset;
6760 return !is_subset;
6763 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6765 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6768 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6770 if (!bmap)
6771 return -1;
6772 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6775 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6777 if (!bset)
6778 return -1;
6779 return bset->n_eq == 0 && bset->n_ineq == 0;
6782 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6784 int i;
6786 if (!map)
6787 return -1;
6789 for (i = 0; i < map->n; ++i) {
6790 int r = isl_basic_map_is_universe(map->p[i]);
6791 if (r < 0 || r)
6792 return r;
6795 return 0;
6798 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6800 return isl_map_plain_is_universe((isl_map *) set);
6803 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6805 return isl_set_plain_is_universe(set);
6808 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6810 struct isl_basic_set *bset = NULL;
6811 struct isl_vec *sample = NULL;
6812 int empty;
6813 unsigned total;
6815 if (!bmap)
6816 return -1;
6818 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6819 return 1;
6821 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6822 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6823 copy = isl_basic_map_remove_redundancies(copy);
6824 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6825 isl_basic_map_free(copy);
6826 return empty;
6829 total = 1 + isl_basic_map_total_dim(bmap);
6830 if (bmap->sample && bmap->sample->size == total) {
6831 int contains = isl_basic_map_contains(bmap, bmap->sample);
6832 if (contains < 0)
6833 return -1;
6834 if (contains)
6835 return 0;
6837 isl_vec_free(bmap->sample);
6838 bmap->sample = NULL;
6839 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6840 if (!bset)
6841 return -1;
6842 sample = isl_basic_set_sample_vec(bset);
6843 if (!sample)
6844 return -1;
6845 empty = sample->size == 0;
6846 isl_vec_free(bmap->sample);
6847 bmap->sample = sample;
6848 if (empty)
6849 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6851 return empty;
6854 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6856 if (!bmap)
6857 return -1;
6858 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6861 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6863 return isl_basic_map_plain_is_empty(bmap);
6866 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6868 if (!bset)
6869 return -1;
6870 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6873 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6875 return isl_basic_set_plain_is_empty(bset);
6878 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6880 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6883 struct isl_map *isl_basic_map_union(
6884 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6886 struct isl_map *map;
6887 if (!bmap1 || !bmap2)
6888 return NULL;
6890 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6892 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6893 if (!map)
6894 goto error;
6895 map = isl_map_add_basic_map(map, bmap1);
6896 map = isl_map_add_basic_map(map, bmap2);
6897 return map;
6898 error:
6899 isl_basic_map_free(bmap1);
6900 isl_basic_map_free(bmap2);
6901 return NULL;
6904 struct isl_set *isl_basic_set_union(
6905 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6907 return (struct isl_set *)isl_basic_map_union(
6908 (struct isl_basic_map *)bset1,
6909 (struct isl_basic_map *)bset2);
6912 /* Order divs such that any div only depends on previous divs */
6913 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6915 int i;
6916 unsigned off;
6918 if (!bmap)
6919 return NULL;
6921 off = isl_space_dim(bmap->dim, isl_dim_all);
6923 for (i = 0; i < bmap->n_div; ++i) {
6924 int pos;
6925 if (isl_int_is_zero(bmap->div[i][0]))
6926 continue;
6927 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6928 bmap->n_div-i);
6929 if (pos == -1)
6930 continue;
6931 isl_basic_map_swap_div(bmap, i, i + pos);
6932 --i;
6934 return bmap;
6937 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6939 return (struct isl_basic_set *)
6940 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6943 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6945 int i;
6947 if (!map)
6948 return 0;
6950 for (i = 0; i < map->n; ++i) {
6951 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6952 if (!map->p[i])
6953 goto error;
6956 return map;
6957 error:
6958 isl_map_free(map);
6959 return NULL;
6962 /* Apply the expansion computed by isl_merge_divs.
6963 * The expansion itself is given by "exp" while the resulting
6964 * list of divs is given by "div".
6966 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6967 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6969 int i, j;
6970 int n_div;
6972 bset = isl_basic_set_cow(bset);
6973 if (!bset || !div)
6974 goto error;
6976 if (div->n_row < bset->n_div)
6977 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6978 "not an expansion", goto error);
6980 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6981 div->n_row - bset->n_div, 0,
6982 2 * (div->n_row - bset->n_div));
6984 n_div = bset->n_div;
6985 for (i = n_div; i < div->n_row; ++i)
6986 if (isl_basic_set_alloc_div(bset) < 0)
6987 goto error;
6989 j = n_div - 1;
6990 for (i = div->n_row - 1; i >= 0; --i) {
6991 if (j >= 0 && exp[j] == i) {
6992 if (i != j)
6993 isl_basic_map_swap_div(bset, i, j);
6994 j--;
6995 } else {
6996 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6997 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6998 goto error;
7002 isl_mat_free(div);
7003 return bset;
7004 error:
7005 isl_basic_set_free(bset);
7006 isl_mat_free(div);
7007 return NULL;
7010 /* Look for a div in dst that corresponds to the div "div" in src.
7011 * The divs before "div" in src and dst are assumed to be the same.
7013 * Returns -1 if no corresponding div was found and the position
7014 * of the corresponding div in dst otherwise.
7016 static int find_div(struct isl_basic_map *dst,
7017 struct isl_basic_map *src, unsigned div)
7019 int i;
7021 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7023 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7024 for (i = div; i < dst->n_div; ++i)
7025 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7026 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7027 dst->n_div - div) == -1)
7028 return i;
7029 return -1;
7032 struct isl_basic_map *isl_basic_map_align_divs(
7033 struct isl_basic_map *dst, struct isl_basic_map *src)
7035 int i;
7036 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7038 if (!dst || !src)
7039 goto error;
7041 if (src->n_div == 0)
7042 return dst;
7044 for (i = 0; i < src->n_div; ++i)
7045 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7047 src = isl_basic_map_order_divs(src);
7048 dst = isl_basic_map_cow(dst);
7049 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7050 src->n_div, 0, 2 * src->n_div);
7051 if (!dst)
7052 return NULL;
7053 for (i = 0; i < src->n_div; ++i) {
7054 int j = find_div(dst, src, i);
7055 if (j < 0) {
7056 j = isl_basic_map_alloc_div(dst);
7057 if (j < 0)
7058 goto error;
7059 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7060 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7061 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7062 goto error;
7064 if (j != i)
7065 isl_basic_map_swap_div(dst, i, j);
7067 return dst;
7068 error:
7069 isl_basic_map_free(dst);
7070 return NULL;
7073 struct isl_basic_set *isl_basic_set_align_divs(
7074 struct isl_basic_set *dst, struct isl_basic_set *src)
7076 return (struct isl_basic_set *)isl_basic_map_align_divs(
7077 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7080 struct isl_map *isl_map_align_divs(struct isl_map *map)
7082 int i;
7084 if (!map)
7085 return NULL;
7086 if (map->n == 0)
7087 return map;
7088 map = isl_map_compute_divs(map);
7089 map = isl_map_cow(map);
7090 if (!map)
7091 return NULL;
7093 for (i = 1; i < map->n; ++i)
7094 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7095 for (i = 1; i < map->n; ++i)
7096 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7098 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7099 return map;
7102 struct isl_set *isl_set_align_divs(struct isl_set *set)
7104 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7107 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7108 __isl_take isl_map *map)
7110 if (!set || !map)
7111 goto error;
7112 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7113 map = isl_map_intersect_domain(map, set);
7114 set = isl_map_range(map);
7115 return set;
7116 error:
7117 isl_set_free(set);
7118 isl_map_free(map);
7119 return NULL;
7122 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7123 __isl_take isl_map *map)
7125 return isl_map_align_params_map_map_and(set, map, &set_apply);
7128 /* There is no need to cow as removing empty parts doesn't change
7129 * the meaning of the set.
7131 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7133 int i;
7135 if (!map)
7136 return NULL;
7138 for (i = map->n-1; i >= 0; --i) {
7139 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7140 continue;
7141 isl_basic_map_free(map->p[i]);
7142 if (i != map->n-1) {
7143 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7144 map->p[i] = map->p[map->n-1];
7146 map->n--;
7149 return map;
7152 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7154 return (struct isl_set *)
7155 isl_map_remove_empty_parts((struct isl_map *)set);
7158 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7160 struct isl_basic_map *bmap;
7161 if (!map || map->n == 0)
7162 return NULL;
7163 bmap = map->p[map->n-1];
7164 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7165 return isl_basic_map_copy(bmap);
7168 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7170 return (struct isl_basic_set *)
7171 isl_map_copy_basic_map((struct isl_map *)set);
7174 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7175 __isl_keep isl_basic_map *bmap)
7177 int i;
7179 if (!map || !bmap)
7180 goto error;
7181 for (i = map->n-1; i >= 0; --i) {
7182 if (map->p[i] != bmap)
7183 continue;
7184 map = isl_map_cow(map);
7185 if (!map)
7186 goto error;
7187 isl_basic_map_free(map->p[i]);
7188 if (i != map->n-1) {
7189 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7190 map->p[i] = map->p[map->n-1];
7192 map->n--;
7193 return map;
7195 return map;
7196 error:
7197 isl_map_free(map);
7198 return NULL;
7201 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7202 struct isl_basic_set *bset)
7204 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7205 (struct isl_basic_map *)bset);
7208 /* Given two basic sets bset1 and bset2, compute the maximal difference
7209 * between the values of dimension pos in bset1 and those in bset2
7210 * for any common value of the parameters and dimensions preceding pos.
7212 static enum isl_lp_result basic_set_maximal_difference_at(
7213 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7214 int pos, isl_int *opt)
7216 isl_space *dims;
7217 struct isl_basic_map *bmap1 = NULL;
7218 struct isl_basic_map *bmap2 = NULL;
7219 struct isl_ctx *ctx;
7220 struct isl_vec *obj;
7221 unsigned total;
7222 unsigned nparam;
7223 unsigned dim1, dim2;
7224 enum isl_lp_result res;
7226 if (!bset1 || !bset2)
7227 return isl_lp_error;
7229 nparam = isl_basic_set_n_param(bset1);
7230 dim1 = isl_basic_set_n_dim(bset1);
7231 dim2 = isl_basic_set_n_dim(bset2);
7232 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7233 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7234 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7235 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7236 if (!bmap1 || !bmap2)
7237 goto error;
7238 bmap1 = isl_basic_map_cow(bmap1);
7239 bmap1 = isl_basic_map_extend(bmap1, nparam,
7240 pos, (dim1 - pos) + (dim2 - pos),
7241 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7242 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7243 if (!bmap1)
7244 goto error;
7245 total = isl_basic_map_total_dim(bmap1);
7246 ctx = bmap1->ctx;
7247 obj = isl_vec_alloc(ctx, 1 + total);
7248 isl_seq_clr(obj->block.data, 1 + total);
7249 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7250 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7251 if (!obj)
7252 goto error;
7253 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7254 opt, NULL, NULL);
7255 isl_basic_map_free(bmap1);
7256 isl_vec_free(obj);
7257 return res;
7258 error:
7259 isl_basic_map_free(bmap1);
7260 isl_basic_map_free(bmap2);
7261 return isl_lp_error;
7264 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7265 * for any common value of the parameters and dimensions preceding pos
7266 * in both basic sets, the values of dimension pos in bset1 are
7267 * smaller or larger than those in bset2.
7269 * Returns
7270 * 1 if bset1 follows bset2
7271 * -1 if bset1 precedes bset2
7272 * 0 if bset1 and bset2 are incomparable
7273 * -2 if some error occurred.
7275 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7276 struct isl_basic_set *bset2, int pos)
7278 isl_int opt;
7279 enum isl_lp_result res;
7280 int cmp;
7282 isl_int_init(opt);
7284 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7286 if (res == isl_lp_empty)
7287 cmp = 0;
7288 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7289 res == isl_lp_unbounded)
7290 cmp = 1;
7291 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7292 cmp = -1;
7293 else
7294 cmp = -2;
7296 isl_int_clear(opt);
7297 return cmp;
7300 /* Given two basic sets bset1 and bset2, check whether
7301 * for any common value of the parameters and dimensions preceding pos
7302 * there is a value of dimension pos in bset1 that is larger
7303 * than a value of the same dimension in bset2.
7305 * Return
7306 * 1 if there exists such a pair
7307 * 0 if there is no such pair, but there is a pair of equal values
7308 * -1 otherwise
7309 * -2 if some error occurred.
7311 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7312 __isl_keep isl_basic_set *bset2, int pos)
7314 isl_int opt;
7315 enum isl_lp_result res;
7316 int cmp;
7318 isl_int_init(opt);
7320 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7322 if (res == isl_lp_empty)
7323 cmp = -1;
7324 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7325 res == isl_lp_unbounded)
7326 cmp = 1;
7327 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7328 cmp = -1;
7329 else if (res == isl_lp_ok)
7330 cmp = 0;
7331 else
7332 cmp = -2;
7334 isl_int_clear(opt);
7335 return cmp;
7338 /* Given two sets set1 and set2, check whether
7339 * for any common value of the parameters and dimensions preceding pos
7340 * there is a value of dimension pos in set1 that is larger
7341 * than a value of the same dimension in set2.
7343 * Return
7344 * 1 if there exists such a pair
7345 * 0 if there is no such pair, but there is a pair of equal values
7346 * -1 otherwise
7347 * -2 if some error occurred.
7349 int isl_set_follows_at(__isl_keep isl_set *set1,
7350 __isl_keep isl_set *set2, int pos)
7352 int i, j;
7353 int follows = -1;
7355 if (!set1 || !set2)
7356 return -2;
7358 for (i = 0; i < set1->n; ++i)
7359 for (j = 0; j < set2->n; ++j) {
7360 int f;
7361 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7362 if (f == 1 || f == -2)
7363 return f;
7364 if (f > follows)
7365 follows = f;
7368 return follows;
7371 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7372 unsigned pos, isl_int *val)
7374 int i;
7375 int d;
7376 unsigned total;
7378 if (!bmap)
7379 return -1;
7380 total = isl_basic_map_total_dim(bmap);
7381 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7382 for (; d+1 > pos; --d)
7383 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7384 break;
7385 if (d != pos)
7386 continue;
7387 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7388 return 0;
7389 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7390 return 0;
7391 if (!isl_int_is_one(bmap->eq[i][1+d]))
7392 return 0;
7393 if (val)
7394 isl_int_neg(*val, bmap->eq[i][0]);
7395 return 1;
7397 return 0;
7400 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7401 unsigned pos, isl_int *val)
7403 int i;
7404 isl_int v;
7405 isl_int tmp;
7406 int fixed;
7408 if (!map)
7409 return -1;
7410 if (map->n == 0)
7411 return 0;
7412 if (map->n == 1)
7413 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7414 isl_int_init(v);
7415 isl_int_init(tmp);
7416 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7417 for (i = 1; fixed == 1 && i < map->n; ++i) {
7418 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7419 if (fixed == 1 && isl_int_ne(tmp, v))
7420 fixed = 0;
7422 if (val)
7423 isl_int_set(*val, v);
7424 isl_int_clear(tmp);
7425 isl_int_clear(v);
7426 return fixed;
7429 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7430 unsigned pos, isl_int *val)
7432 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7433 pos, val);
7436 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7437 isl_int *val)
7439 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7442 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7443 enum isl_dim_type type, unsigned pos, isl_int *val)
7445 if (pos >= isl_basic_map_dim(bmap, type))
7446 return -1;
7447 return isl_basic_map_plain_has_fixed_var(bmap,
7448 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7451 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7452 enum isl_dim_type type, unsigned pos, isl_int *val)
7454 if (pos >= isl_map_dim(map, type))
7455 return -1;
7456 return isl_map_plain_has_fixed_var(map,
7457 map_offset(map, type) - 1 + pos, val);
7460 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7461 enum isl_dim_type type, unsigned pos, isl_int *val)
7463 return isl_map_plain_is_fixed(set, type, pos, val);
7466 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7467 enum isl_dim_type type, unsigned pos, isl_int *val)
7469 return isl_map_plain_is_fixed(map, type, pos, val);
7472 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7473 * then return this fixed value in *val.
7475 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7476 unsigned dim, isl_int *val)
7478 return isl_basic_set_plain_has_fixed_var(bset,
7479 isl_basic_set_n_param(bset) + dim, val);
7482 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7483 * then return this fixed value in *val.
7485 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7486 unsigned dim, isl_int *val)
7488 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7491 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7492 unsigned dim, isl_int *val)
7494 return isl_set_plain_dim_is_fixed(set, dim, val);
7497 /* Check if input variable in has fixed value and if so and if val is not NULL,
7498 * then return this fixed value in *val.
7500 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7501 unsigned in, isl_int *val)
7503 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7506 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7507 * and if val is not NULL, then return this lower bound in *val.
7509 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7510 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7512 int i, i_eq = -1, i_ineq = -1;
7513 isl_int *c;
7514 unsigned total;
7515 unsigned nparam;
7517 if (!bset)
7518 return -1;
7519 total = isl_basic_set_total_dim(bset);
7520 nparam = isl_basic_set_n_param(bset);
7521 for (i = 0; i < bset->n_eq; ++i) {
7522 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7523 continue;
7524 if (i_eq != -1)
7525 return 0;
7526 i_eq = i;
7528 for (i = 0; i < bset->n_ineq; ++i) {
7529 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7530 continue;
7531 if (i_eq != -1 || i_ineq != -1)
7532 return 0;
7533 i_ineq = i;
7535 if (i_eq == -1 && i_ineq == -1)
7536 return 0;
7537 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7538 /* The coefficient should always be one due to normalization. */
7539 if (!isl_int_is_one(c[1+nparam+dim]))
7540 return 0;
7541 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7542 return 0;
7543 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7544 total - nparam - dim - 1) != -1)
7545 return 0;
7546 if (val)
7547 isl_int_neg(*val, c[0]);
7548 return 1;
7551 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7552 unsigned dim, isl_int *val)
7554 int i;
7555 isl_int v;
7556 isl_int tmp;
7557 int fixed;
7559 if (!set)
7560 return -1;
7561 if (set->n == 0)
7562 return 0;
7563 if (set->n == 1)
7564 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7565 dim, val);
7566 isl_int_init(v);
7567 isl_int_init(tmp);
7568 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7569 dim, &v);
7570 for (i = 1; fixed == 1 && i < set->n; ++i) {
7571 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7572 dim, &tmp);
7573 if (fixed == 1 && isl_int_ne(tmp, v))
7574 fixed = 0;
7576 if (val)
7577 isl_int_set(*val, v);
7578 isl_int_clear(tmp);
7579 isl_int_clear(v);
7580 return fixed;
7583 struct constraint {
7584 unsigned size;
7585 isl_int *c;
7588 /* uset_gist depends on constraints without existentially quantified
7589 * variables sorting first.
7591 static int qsort_constraint_cmp(const void *p1, const void *p2)
7593 const struct constraint *c1 = (const struct constraint *)p1;
7594 const struct constraint *c2 = (const struct constraint *)p2;
7595 int l1, l2;
7596 unsigned size = isl_min(c1->size, c2->size);
7598 l1 = isl_seq_last_non_zero(c1->c, size);
7599 l2 = isl_seq_last_non_zero(c2->c, size);
7601 if (l1 != l2)
7602 return l1 - l2;
7604 return isl_seq_cmp(c1->c, c2->c, size);
7607 static struct isl_basic_map *isl_basic_map_sort_constraints(
7608 struct isl_basic_map *bmap)
7610 int i;
7611 struct constraint *c;
7612 unsigned total;
7614 if (!bmap)
7615 return NULL;
7616 total = isl_basic_map_total_dim(bmap);
7617 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7618 if (!c)
7619 goto error;
7620 for (i = 0; i < bmap->n_ineq; ++i) {
7621 c[i].size = total;
7622 c[i].c = bmap->ineq[i];
7624 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7625 for (i = 0; i < bmap->n_ineq; ++i)
7626 bmap->ineq[i] = c[i].c;
7627 free(c);
7628 return bmap;
7629 error:
7630 isl_basic_map_free(bmap);
7631 return NULL;
7634 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7635 __isl_take isl_basic_set *bset)
7637 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7638 (struct isl_basic_map *)bset);
7641 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7643 if (!bmap)
7644 return NULL;
7645 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7646 return bmap;
7647 bmap = isl_basic_map_remove_redundancies(bmap);
7648 bmap = isl_basic_map_sort_constraints(bmap);
7649 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7650 return bmap;
7653 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7655 return (struct isl_basic_set *)isl_basic_map_normalize(
7656 (struct isl_basic_map *)bset);
7659 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7660 const __isl_keep isl_basic_map *bmap2)
7662 int i, cmp;
7663 unsigned total;
7665 if (bmap1 == bmap2)
7666 return 0;
7667 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7668 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7669 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7670 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7671 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7672 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7673 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7674 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7675 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7676 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7677 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7678 return 0;
7679 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7680 return 1;
7681 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7682 return -1;
7683 if (bmap1->n_eq != bmap2->n_eq)
7684 return bmap1->n_eq - bmap2->n_eq;
7685 if (bmap1->n_ineq != bmap2->n_ineq)
7686 return bmap1->n_ineq - bmap2->n_ineq;
7687 if (bmap1->n_div != bmap2->n_div)
7688 return bmap1->n_div - bmap2->n_div;
7689 total = isl_basic_map_total_dim(bmap1);
7690 for (i = 0; i < bmap1->n_eq; ++i) {
7691 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7692 if (cmp)
7693 return cmp;
7695 for (i = 0; i < bmap1->n_ineq; ++i) {
7696 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7697 if (cmp)
7698 return cmp;
7700 for (i = 0; i < bmap1->n_div; ++i) {
7701 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7702 if (cmp)
7703 return cmp;
7705 return 0;
7708 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7709 const __isl_keep isl_basic_set *bset2)
7711 return isl_basic_map_plain_cmp(bset1, bset2);
7714 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7715 const __isl_keep isl_set *set2)
7717 int i, cmp;
7719 if (set1 == set2)
7720 return 0;
7721 if (set1->n != set2->n)
7722 return set1->n - set2->n;
7724 for (i = 0; i < set1->n; ++i) {
7725 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7726 if (cmp)
7727 return cmp;
7730 return 0;
7733 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7734 __isl_keep isl_basic_map *bmap2)
7736 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7739 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7740 __isl_keep isl_basic_set *bset2)
7742 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7743 (isl_basic_map *)bset2);
7746 static int qsort_bmap_cmp(const void *p1, const void *p2)
7748 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7749 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7751 return isl_basic_map_plain_cmp(bmap1, bmap2);
7754 /* We normalize in place, but if anything goes wrong we need
7755 * to return NULL, so we need to make sure we don't change the
7756 * meaning of any possible other copies of map.
7758 struct isl_map *isl_map_normalize(struct isl_map *map)
7760 int i, j;
7761 struct isl_basic_map *bmap;
7763 if (!map)
7764 return NULL;
7765 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7766 return map;
7767 for (i = 0; i < map->n; ++i) {
7768 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7769 if (!bmap)
7770 goto error;
7771 isl_basic_map_free(map->p[i]);
7772 map->p[i] = bmap;
7774 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7775 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7776 map = isl_map_remove_empty_parts(map);
7777 if (!map)
7778 return NULL;
7779 for (i = map->n - 1; i >= 1; --i) {
7780 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7781 continue;
7782 isl_basic_map_free(map->p[i-1]);
7783 for (j = i; j < map->n; ++j)
7784 map->p[j-1] = map->p[j];
7785 map->n--;
7787 return map;
7788 error:
7789 isl_map_free(map);
7790 return NULL;
7794 struct isl_set *isl_set_normalize(struct isl_set *set)
7796 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7799 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7801 int i;
7802 int equal;
7804 if (!map1 || !map2)
7805 return -1;
7807 if (map1 == map2)
7808 return 1;
7809 if (!isl_space_is_equal(map1->dim, map2->dim))
7810 return 0;
7812 map1 = isl_map_copy(map1);
7813 map2 = isl_map_copy(map2);
7814 map1 = isl_map_normalize(map1);
7815 map2 = isl_map_normalize(map2);
7816 if (!map1 || !map2)
7817 goto error;
7818 equal = map1->n == map2->n;
7819 for (i = 0; equal && i < map1->n; ++i) {
7820 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7821 if (equal < 0)
7822 goto error;
7824 isl_map_free(map1);
7825 isl_map_free(map2);
7826 return equal;
7827 error:
7828 isl_map_free(map1);
7829 isl_map_free(map2);
7830 return -1;
7833 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7835 return isl_map_plain_is_equal(map1, map2);
7838 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7840 return isl_map_plain_is_equal((struct isl_map *)set1,
7841 (struct isl_map *)set2);
7844 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7846 return isl_set_plain_is_equal(set1, set2);
7849 /* Return an interval that ranges from min to max (inclusive)
7851 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7852 isl_int min, isl_int max)
7854 int k;
7855 struct isl_basic_set *bset = NULL;
7857 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7858 if (!bset)
7859 goto error;
7861 k = isl_basic_set_alloc_inequality(bset);
7862 if (k < 0)
7863 goto error;
7864 isl_int_set_si(bset->ineq[k][1], 1);
7865 isl_int_neg(bset->ineq[k][0], min);
7867 k = isl_basic_set_alloc_inequality(bset);
7868 if (k < 0)
7869 goto error;
7870 isl_int_set_si(bset->ineq[k][1], -1);
7871 isl_int_set(bset->ineq[k][0], max);
7873 return bset;
7874 error:
7875 isl_basic_set_free(bset);
7876 return NULL;
7879 /* Return the Cartesian product of the basic sets in list (in the given order).
7881 __isl_give isl_basic_set *isl_basic_set_list_product(
7882 __isl_take struct isl_basic_set_list *list)
7884 int i;
7885 unsigned dim;
7886 unsigned nparam;
7887 unsigned extra;
7888 unsigned n_eq;
7889 unsigned n_ineq;
7890 struct isl_basic_set *product = NULL;
7892 if (!list)
7893 goto error;
7894 isl_assert(list->ctx, list->n > 0, goto error);
7895 isl_assert(list->ctx, list->p[0], goto error);
7896 nparam = isl_basic_set_n_param(list->p[0]);
7897 dim = isl_basic_set_n_dim(list->p[0]);
7898 extra = list->p[0]->n_div;
7899 n_eq = list->p[0]->n_eq;
7900 n_ineq = list->p[0]->n_ineq;
7901 for (i = 1; i < list->n; ++i) {
7902 isl_assert(list->ctx, list->p[i], goto error);
7903 isl_assert(list->ctx,
7904 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7905 dim += isl_basic_set_n_dim(list->p[i]);
7906 extra += list->p[i]->n_div;
7907 n_eq += list->p[i]->n_eq;
7908 n_ineq += list->p[i]->n_ineq;
7910 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7911 n_eq, n_ineq);
7912 if (!product)
7913 goto error;
7914 dim = 0;
7915 for (i = 0; i < list->n; ++i) {
7916 isl_basic_set_add_constraints(product,
7917 isl_basic_set_copy(list->p[i]), dim);
7918 dim += isl_basic_set_n_dim(list->p[i]);
7920 isl_basic_set_list_free(list);
7921 return product;
7922 error:
7923 isl_basic_set_free(product);
7924 isl_basic_set_list_free(list);
7925 return NULL;
7928 struct isl_basic_map *isl_basic_map_product(
7929 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7931 isl_space *dim_result = NULL;
7932 struct isl_basic_map *bmap;
7933 unsigned in1, in2, out1, out2, nparam, total, pos;
7934 struct isl_dim_map *dim_map1, *dim_map2;
7936 if (!bmap1 || !bmap2)
7937 goto error;
7939 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7940 bmap2->dim, isl_dim_param), goto error);
7941 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7942 isl_space_copy(bmap2->dim));
7944 in1 = isl_basic_map_n_in(bmap1);
7945 in2 = isl_basic_map_n_in(bmap2);
7946 out1 = isl_basic_map_n_out(bmap1);
7947 out2 = isl_basic_map_n_out(bmap2);
7948 nparam = isl_basic_map_n_param(bmap1);
7950 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7951 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7952 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7953 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7954 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7955 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7956 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7957 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7958 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7959 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7960 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7962 bmap = isl_basic_map_alloc_space(dim_result,
7963 bmap1->n_div + bmap2->n_div,
7964 bmap1->n_eq + bmap2->n_eq,
7965 bmap1->n_ineq + bmap2->n_ineq);
7966 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7967 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7968 bmap = isl_basic_map_simplify(bmap);
7969 return isl_basic_map_finalize(bmap);
7970 error:
7971 isl_basic_map_free(bmap1);
7972 isl_basic_map_free(bmap2);
7973 return NULL;
7976 __isl_give isl_basic_map *isl_basic_map_flat_product(
7977 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7979 isl_basic_map *prod;
7981 prod = isl_basic_map_product(bmap1, bmap2);
7982 prod = isl_basic_map_flatten(prod);
7983 return prod;
7986 __isl_give isl_basic_set *isl_basic_set_flat_product(
7987 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7989 return isl_basic_map_flat_range_product(bset1, bset2);
7992 __isl_give isl_basic_map *isl_basic_map_domain_product(
7993 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7995 isl_space *space_result = NULL;
7996 isl_basic_map *bmap;
7997 unsigned in1, in2, out, nparam, total, pos;
7998 struct isl_dim_map *dim_map1, *dim_map2;
8000 if (!bmap1 || !bmap2)
8001 goto error;
8003 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8004 isl_space_copy(bmap2->dim));
8006 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8007 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8008 out = isl_basic_map_dim(bmap1, isl_dim_out);
8009 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8011 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8012 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8013 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8014 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8015 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8016 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8017 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8018 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8019 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8020 isl_dim_map_div(dim_map1, bmap1, pos += out);
8021 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8023 bmap = isl_basic_map_alloc_space(space_result,
8024 bmap1->n_div + bmap2->n_div,
8025 bmap1->n_eq + bmap2->n_eq,
8026 bmap1->n_ineq + bmap2->n_ineq);
8027 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8028 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8029 bmap = isl_basic_map_simplify(bmap);
8030 return isl_basic_map_finalize(bmap);
8031 error:
8032 isl_basic_map_free(bmap1);
8033 isl_basic_map_free(bmap2);
8034 return NULL;
8037 __isl_give isl_basic_map *isl_basic_map_range_product(
8038 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8040 isl_space *dim_result = NULL;
8041 isl_basic_map *bmap;
8042 unsigned in, out1, out2, nparam, total, pos;
8043 struct isl_dim_map *dim_map1, *dim_map2;
8045 if (!bmap1 || !bmap2)
8046 goto error;
8048 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8049 isl_space_copy(bmap2->dim));
8051 in = isl_basic_map_dim(bmap1, isl_dim_in);
8052 out1 = isl_basic_map_n_out(bmap1);
8053 out2 = isl_basic_map_n_out(bmap2);
8054 nparam = isl_basic_map_n_param(bmap1);
8056 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8057 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8058 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8059 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8060 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8061 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8062 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8063 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8064 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8065 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8066 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8068 bmap = isl_basic_map_alloc_space(dim_result,
8069 bmap1->n_div + bmap2->n_div,
8070 bmap1->n_eq + bmap2->n_eq,
8071 bmap1->n_ineq + bmap2->n_ineq);
8072 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8073 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8074 bmap = isl_basic_map_simplify(bmap);
8075 return isl_basic_map_finalize(bmap);
8076 error:
8077 isl_basic_map_free(bmap1);
8078 isl_basic_map_free(bmap2);
8079 return NULL;
8082 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8083 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8085 isl_basic_map *prod;
8087 prod = isl_basic_map_range_product(bmap1, bmap2);
8088 prod = isl_basic_map_flatten_range(prod);
8089 return prod;
8092 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8093 __isl_take isl_map *map2,
8094 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8095 __isl_take isl_space *right),
8096 __isl_give isl_basic_map *(*basic_map_product)(
8097 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8099 unsigned flags = 0;
8100 struct isl_map *result;
8101 int i, j;
8103 if (!map1 || !map2)
8104 goto error;
8106 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8107 map2->dim, isl_dim_param), goto error);
8109 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8110 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8111 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8113 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8114 isl_space_copy(map2->dim)),
8115 map1->n * map2->n, flags);
8116 if (!result)
8117 goto error;
8118 for (i = 0; i < map1->n; ++i)
8119 for (j = 0; j < map2->n; ++j) {
8120 struct isl_basic_map *part;
8121 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8122 isl_basic_map_copy(map2->p[j]));
8123 if (isl_basic_map_is_empty(part))
8124 isl_basic_map_free(part);
8125 else
8126 result = isl_map_add_basic_map(result, part);
8127 if (!result)
8128 goto error;
8130 isl_map_free(map1);
8131 isl_map_free(map2);
8132 return result;
8133 error:
8134 isl_map_free(map1);
8135 isl_map_free(map2);
8136 return NULL;
8139 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8141 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8142 __isl_take isl_map *map2)
8144 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8147 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8148 __isl_take isl_map *map2)
8150 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8153 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8155 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8156 __isl_take isl_map *map2)
8158 isl_map *prod;
8160 prod = isl_map_product(map1, map2);
8161 prod = isl_map_flatten(prod);
8162 return prod;
8165 /* Given two set A and B, construct its Cartesian product A x B.
8167 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8169 return isl_map_range_product(set1, set2);
8172 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8173 __isl_take isl_set *set2)
8175 return isl_map_flat_range_product(set1, set2);
8178 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8180 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8181 __isl_take isl_map *map2)
8183 return map_product(map1, map2, &isl_space_domain_product,
8184 &isl_basic_map_domain_product);
8187 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8189 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8190 __isl_take isl_map *map2)
8192 return map_product(map1, map2, &isl_space_range_product,
8193 &isl_basic_map_range_product);
8196 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8197 __isl_take isl_map *map2)
8199 return isl_map_align_params_map_map_and(map1, map2,
8200 &map_domain_product_aligned);
8203 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8204 __isl_take isl_map *map2)
8206 return isl_map_align_params_map_map_and(map1, map2,
8207 &map_range_product_aligned);
8210 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8212 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8213 __isl_take isl_map *map2)
8215 isl_map *prod;
8217 prod = isl_map_domain_product(map1, map2);
8218 prod = isl_map_flatten_domain(prod);
8219 return prod;
8222 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8224 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8225 __isl_take isl_map *map2)
8227 isl_map *prod;
8229 prod = isl_map_range_product(map1, map2);
8230 prod = isl_map_flatten_range(prod);
8231 return prod;
8234 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8236 int i;
8237 uint32_t hash = isl_hash_init();
8238 unsigned total;
8240 if (!bmap)
8241 return 0;
8242 bmap = isl_basic_map_copy(bmap);
8243 bmap = isl_basic_map_normalize(bmap);
8244 if (!bmap)
8245 return 0;
8246 total = isl_basic_map_total_dim(bmap);
8247 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8248 for (i = 0; i < bmap->n_eq; ++i) {
8249 uint32_t c_hash;
8250 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8251 isl_hash_hash(hash, c_hash);
8253 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8254 for (i = 0; i < bmap->n_ineq; ++i) {
8255 uint32_t c_hash;
8256 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8257 isl_hash_hash(hash, c_hash);
8259 isl_hash_byte(hash, bmap->n_div & 0xFF);
8260 for (i = 0; i < bmap->n_div; ++i) {
8261 uint32_t c_hash;
8262 if (isl_int_is_zero(bmap->div[i][0]))
8263 continue;
8264 isl_hash_byte(hash, i & 0xFF);
8265 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8266 isl_hash_hash(hash, c_hash);
8268 isl_basic_map_free(bmap);
8269 return hash;
8272 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8274 return isl_basic_map_get_hash((isl_basic_map *)bset);
8277 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8279 int i;
8280 uint32_t hash;
8282 if (!map)
8283 return 0;
8284 map = isl_map_copy(map);
8285 map = isl_map_normalize(map);
8286 if (!map)
8287 return 0;
8289 hash = isl_hash_init();
8290 for (i = 0; i < map->n; ++i) {
8291 uint32_t bmap_hash;
8292 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8293 isl_hash_hash(hash, bmap_hash);
8296 isl_map_free(map);
8298 return hash;
8301 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8303 return isl_map_get_hash((isl_map *)set);
8306 /* Check if the value for dimension dim is completely determined
8307 * by the values of the other parameters and variables.
8308 * That is, check if dimension dim is involved in an equality.
8310 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8312 int i;
8313 unsigned nparam;
8315 if (!bset)
8316 return -1;
8317 nparam = isl_basic_set_n_param(bset);
8318 for (i = 0; i < bset->n_eq; ++i)
8319 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8320 return 1;
8321 return 0;
8324 /* Check if the value for dimension dim is completely determined
8325 * by the values of the other parameters and variables.
8326 * That is, check if dimension dim is involved in an equality
8327 * for each of the subsets.
8329 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8331 int i;
8333 if (!set)
8334 return -1;
8335 for (i = 0; i < set->n; ++i) {
8336 int unique;
8337 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8338 if (unique != 1)
8339 return unique;
8341 return 1;
8344 int isl_set_n_basic_set(__isl_keep isl_set *set)
8346 return set ? set->n : 0;
8349 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8350 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8352 int i;
8354 if (!map)
8355 return -1;
8357 for (i = 0; i < map->n; ++i)
8358 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8359 return -1;
8361 return 0;
8364 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8365 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8367 int i;
8369 if (!set)
8370 return -1;
8372 for (i = 0; i < set->n; ++i)
8373 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8374 return -1;
8376 return 0;
8379 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8381 isl_space *dim;
8383 if (!bset)
8384 return NULL;
8386 bset = isl_basic_set_cow(bset);
8387 if (!bset)
8388 return NULL;
8390 dim = isl_basic_set_get_space(bset);
8391 dim = isl_space_lift(dim, bset->n_div);
8392 if (!dim)
8393 goto error;
8394 isl_space_free(bset->dim);
8395 bset->dim = dim;
8396 bset->extra -= bset->n_div;
8397 bset->n_div = 0;
8399 bset = isl_basic_set_finalize(bset);
8401 return bset;
8402 error:
8403 isl_basic_set_free(bset);
8404 return NULL;
8407 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8409 int i;
8410 isl_space *dim;
8411 unsigned n_div;
8413 set = isl_set_align_divs(set);
8415 if (!set)
8416 return NULL;
8418 set = isl_set_cow(set);
8419 if (!set)
8420 return NULL;
8422 n_div = set->p[0]->n_div;
8423 dim = isl_set_get_space(set);
8424 dim = isl_space_lift(dim, n_div);
8425 if (!dim)
8426 goto error;
8427 isl_space_free(set->dim);
8428 set->dim = dim;
8430 for (i = 0; i < set->n; ++i) {
8431 set->p[i] = isl_basic_set_lift(set->p[i]);
8432 if (!set->p[i])
8433 goto error;
8436 return set;
8437 error:
8438 isl_set_free(set);
8439 return NULL;
8442 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8444 isl_space *dim;
8445 struct isl_basic_map *bmap;
8446 unsigned n_set;
8447 unsigned n_div;
8448 unsigned n_param;
8449 unsigned total;
8450 int i, k, l;
8452 set = isl_set_align_divs(set);
8454 if (!set)
8455 return NULL;
8457 dim = isl_set_get_space(set);
8458 if (set->n == 0 || set->p[0]->n_div == 0) {
8459 isl_set_free(set);
8460 return isl_map_identity(isl_space_map_from_set(dim));
8463 n_div = set->p[0]->n_div;
8464 dim = isl_space_map_from_set(dim);
8465 n_param = isl_space_dim(dim, isl_dim_param);
8466 n_set = isl_space_dim(dim, isl_dim_in);
8467 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8468 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8469 for (i = 0; i < n_set; ++i)
8470 bmap = var_equal(bmap, i);
8472 total = n_param + n_set + n_set + n_div;
8473 for (i = 0; i < n_div; ++i) {
8474 k = isl_basic_map_alloc_inequality(bmap);
8475 if (k < 0)
8476 goto error;
8477 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8478 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8479 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8480 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8481 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8482 set->p[0]->div[i][0]);
8484 l = isl_basic_map_alloc_inequality(bmap);
8485 if (l < 0)
8486 goto error;
8487 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8488 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8489 set->p[0]->div[i][0]);
8490 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8493 isl_set_free(set);
8494 bmap = isl_basic_map_simplify(bmap);
8495 bmap = isl_basic_map_finalize(bmap);
8496 return isl_map_from_basic_map(bmap);
8497 error:
8498 isl_set_free(set);
8499 isl_basic_map_free(bmap);
8500 return NULL;
8503 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8505 unsigned dim;
8506 int size = 0;
8508 if (!bset)
8509 return -1;
8511 dim = isl_basic_set_total_dim(bset);
8512 size += bset->n_eq * (1 + dim);
8513 size += bset->n_ineq * (1 + dim);
8514 size += bset->n_div * (2 + dim);
8516 return size;
8519 int isl_set_size(__isl_keep isl_set *set)
8521 int i;
8522 int size = 0;
8524 if (!set)
8525 return -1;
8527 for (i = 0; i < set->n; ++i)
8528 size += isl_basic_set_size(set->p[i]);
8530 return size;
8533 /* Check if there is any lower bound (if lower == 0) and/or upper
8534 * bound (if upper == 0) on the specified dim.
8536 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8537 enum isl_dim_type type, unsigned pos, int lower, int upper)
8539 int i;
8541 if (!bmap)
8542 return -1;
8544 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8546 pos += isl_basic_map_offset(bmap, type);
8548 for (i = 0; i < bmap->n_div; ++i) {
8549 if (isl_int_is_zero(bmap->div[i][0]))
8550 continue;
8551 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8552 return 1;
8555 for (i = 0; i < bmap->n_eq; ++i)
8556 if (!isl_int_is_zero(bmap->eq[i][pos]))
8557 return 1;
8559 for (i = 0; i < bmap->n_ineq; ++i) {
8560 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8561 if (sgn > 0)
8562 lower = 1;
8563 if (sgn < 0)
8564 upper = 1;
8567 return lower && upper;
8570 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8571 enum isl_dim_type type, unsigned pos)
8573 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8576 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8577 enum isl_dim_type type, unsigned pos)
8579 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8582 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8583 enum isl_dim_type type, unsigned pos)
8585 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8588 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8589 enum isl_dim_type type, unsigned pos)
8591 int i;
8593 if (!map)
8594 return -1;
8596 for (i = 0; i < map->n; ++i) {
8597 int bounded;
8598 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8599 if (bounded < 0 || !bounded)
8600 return bounded;
8603 return 1;
8606 /* Return 1 if the specified dim is involved in both an upper bound
8607 * and a lower bound.
8609 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8610 enum isl_dim_type type, unsigned pos)
8612 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8615 static int has_bound(__isl_keep isl_map *map,
8616 enum isl_dim_type type, unsigned pos,
8617 int (*fn)(__isl_keep isl_basic_map *bmap,
8618 enum isl_dim_type type, unsigned pos))
8620 int i;
8622 if (!map)
8623 return -1;
8625 for (i = 0; i < map->n; ++i) {
8626 int bounded;
8627 bounded = fn(map->p[i], type, pos);
8628 if (bounded < 0 || bounded)
8629 return bounded;
8632 return 0;
8635 /* Return 1 if the specified dim is involved in any lower bound.
8637 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8638 enum isl_dim_type type, unsigned pos)
8640 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8643 /* Return 1 if the specified dim is involved in any upper bound.
8645 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8646 enum isl_dim_type type, unsigned pos)
8648 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8651 /* For each of the "n" variables starting at "first", determine
8652 * the sign of the variable and put the results in the first "n"
8653 * elements of the array "signs".
8654 * Sign
8655 * 1 means that the variable is non-negative
8656 * -1 means that the variable is non-positive
8657 * 0 means the variable attains both positive and negative values.
8659 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8660 unsigned first, unsigned n, int *signs)
8662 isl_vec *bound = NULL;
8663 struct isl_tab *tab = NULL;
8664 struct isl_tab_undo *snap;
8665 int i;
8667 if (!bset || !signs)
8668 return -1;
8670 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8671 tab = isl_tab_from_basic_set(bset);
8672 if (!bound || !tab)
8673 goto error;
8675 isl_seq_clr(bound->el, bound->size);
8676 isl_int_set_si(bound->el[0], -1);
8678 snap = isl_tab_snap(tab);
8679 for (i = 0; i < n; ++i) {
8680 int empty;
8682 isl_int_set_si(bound->el[1 + first + i], -1);
8683 if (isl_tab_add_ineq(tab, bound->el) < 0)
8684 goto error;
8685 empty = tab->empty;
8686 isl_int_set_si(bound->el[1 + first + i], 0);
8687 if (isl_tab_rollback(tab, snap) < 0)
8688 goto error;
8690 if (empty) {
8691 signs[i] = 1;
8692 continue;
8695 isl_int_set_si(bound->el[1 + first + i], 1);
8696 if (isl_tab_add_ineq(tab, bound->el) < 0)
8697 goto error;
8698 empty = tab->empty;
8699 isl_int_set_si(bound->el[1 + first + i], 0);
8700 if (isl_tab_rollback(tab, snap) < 0)
8701 goto error;
8703 signs[i] = empty ? -1 : 0;
8706 isl_tab_free(tab);
8707 isl_vec_free(bound);
8708 return 0;
8709 error:
8710 isl_tab_free(tab);
8711 isl_vec_free(bound);
8712 return -1;
8715 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8716 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8718 if (!bset || !signs)
8719 return -1;
8720 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8721 return -1);
8723 first += pos(bset->dim, type) - 1;
8724 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8727 /* Check if the given basic map is obviously single-valued.
8728 * In particular, for each output dimension, check that there is
8729 * an equality that defines the output dimension in terms of
8730 * earlier dimensions.
8732 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8734 int i, j;
8735 unsigned total;
8736 unsigned n_out;
8737 unsigned o_out;
8739 if (!bmap)
8740 return -1;
8742 total = 1 + isl_basic_map_total_dim(bmap);
8743 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8744 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8746 for (i = 0; i < n_out; ++i) {
8747 for (j = 0; j < bmap->n_eq; ++j) {
8748 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8749 continue;
8750 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8751 total - (o_out + i + 1)) == -1)
8752 break;
8754 if (j >= bmap->n_eq)
8755 return 0;
8758 return 1;
8761 /* Check if the given map is obviously single-valued.
8763 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8765 if (!map)
8766 return -1;
8767 if (map->n == 0)
8768 return 1;
8769 if (map->n >= 2)
8770 return 0;
8772 return isl_basic_map_plain_is_single_valued(map->p[0]);
8775 /* Check if the given map is single-valued.
8776 * We simply compute
8778 * M \circ M^-1
8780 * and check if the result is a subset of the identity mapping.
8782 int isl_map_is_single_valued(__isl_keep isl_map *map)
8784 isl_space *dim;
8785 isl_map *test;
8786 isl_map *id;
8787 int sv;
8789 sv = isl_map_plain_is_single_valued(map);
8790 if (sv < 0 || sv)
8791 return sv;
8793 test = isl_map_reverse(isl_map_copy(map));
8794 test = isl_map_apply_range(test, isl_map_copy(map));
8796 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8797 id = isl_map_identity(dim);
8799 sv = isl_map_is_subset(test, id);
8801 isl_map_free(test);
8802 isl_map_free(id);
8804 return sv;
8807 int isl_map_is_injective(__isl_keep isl_map *map)
8809 int in;
8811 map = isl_map_copy(map);
8812 map = isl_map_reverse(map);
8813 in = isl_map_is_single_valued(map);
8814 isl_map_free(map);
8816 return in;
8819 /* Check if the given map is obviously injective.
8821 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8823 int in;
8825 map = isl_map_copy(map);
8826 map = isl_map_reverse(map);
8827 in = isl_map_plain_is_single_valued(map);
8828 isl_map_free(map);
8830 return in;
8833 int isl_map_is_bijective(__isl_keep isl_map *map)
8835 int sv;
8837 sv = isl_map_is_single_valued(map);
8838 if (sv < 0 || !sv)
8839 return sv;
8841 return isl_map_is_injective(map);
8844 int isl_set_is_singleton(__isl_keep isl_set *set)
8846 return isl_map_is_single_valued((isl_map *)set);
8849 int isl_map_is_translation(__isl_keep isl_map *map)
8851 int ok;
8852 isl_set *delta;
8854 delta = isl_map_deltas(isl_map_copy(map));
8855 ok = isl_set_is_singleton(delta);
8856 isl_set_free(delta);
8858 return ok;
8861 static int unique(isl_int *p, unsigned pos, unsigned len)
8863 if (isl_seq_first_non_zero(p, pos) != -1)
8864 return 0;
8865 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8866 return 0;
8867 return 1;
8870 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8872 int i, j;
8873 unsigned nvar;
8874 unsigned ovar;
8876 if (!bset)
8877 return -1;
8879 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8880 return 0;
8882 nvar = isl_basic_set_dim(bset, isl_dim_set);
8883 ovar = isl_space_offset(bset->dim, isl_dim_set);
8884 for (j = 0; j < nvar; ++j) {
8885 int lower = 0, upper = 0;
8886 for (i = 0; i < bset->n_eq; ++i) {
8887 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8888 continue;
8889 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8890 return 0;
8891 break;
8893 if (i < bset->n_eq)
8894 continue;
8895 for (i = 0; i < bset->n_ineq; ++i) {
8896 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8897 continue;
8898 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8899 return 0;
8900 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8901 lower = 1;
8902 else
8903 upper = 1;
8905 if (!lower || !upper)
8906 return 0;
8909 return 1;
8912 int isl_set_is_box(__isl_keep isl_set *set)
8914 if (!set)
8915 return -1;
8916 if (set->n != 1)
8917 return 0;
8919 return isl_basic_set_is_box(set->p[0]);
8922 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8924 if (!bset)
8925 return -1;
8927 return isl_space_is_wrapping(bset->dim);
8930 int isl_set_is_wrapping(__isl_keep isl_set *set)
8932 if (!set)
8933 return -1;
8935 return isl_space_is_wrapping(set->dim);
8938 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8940 bmap = isl_basic_map_cow(bmap);
8941 if (!bmap)
8942 return NULL;
8944 bmap->dim = isl_space_wrap(bmap->dim);
8945 if (!bmap->dim)
8946 goto error;
8948 bmap = isl_basic_map_finalize(bmap);
8950 return (isl_basic_set *)bmap;
8951 error:
8952 isl_basic_map_free(bmap);
8953 return NULL;
8956 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8958 int i;
8960 map = isl_map_cow(map);
8961 if (!map)
8962 return NULL;
8964 for (i = 0; i < map->n; ++i) {
8965 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8966 if (!map->p[i])
8967 goto error;
8969 map->dim = isl_space_wrap(map->dim);
8970 if (!map->dim)
8971 goto error;
8973 return (isl_set *)map;
8974 error:
8975 isl_map_free(map);
8976 return NULL;
8979 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8981 bset = isl_basic_set_cow(bset);
8982 if (!bset)
8983 return NULL;
8985 bset->dim = isl_space_unwrap(bset->dim);
8986 if (!bset->dim)
8987 goto error;
8989 bset = isl_basic_set_finalize(bset);
8991 return (isl_basic_map *)bset;
8992 error:
8993 isl_basic_set_free(bset);
8994 return NULL;
8997 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8999 int i;
9001 if (!set)
9002 return NULL;
9004 if (!isl_set_is_wrapping(set))
9005 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9006 goto error);
9008 set = isl_set_cow(set);
9009 if (!set)
9010 return NULL;
9012 for (i = 0; i < set->n; ++i) {
9013 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9014 if (!set->p[i])
9015 goto error;
9018 set->dim = isl_space_unwrap(set->dim);
9019 if (!set->dim)
9020 goto error;
9022 return (isl_map *)set;
9023 error:
9024 isl_set_free(set);
9025 return NULL;
9028 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9029 enum isl_dim_type type)
9031 if (!bmap)
9032 return NULL;
9034 if (!isl_space_is_named_or_nested(bmap->dim, type))
9035 return bmap;
9037 bmap = isl_basic_map_cow(bmap);
9038 if (!bmap)
9039 return NULL;
9041 bmap->dim = isl_space_reset(bmap->dim, type);
9042 if (!bmap->dim)
9043 goto error;
9045 bmap = isl_basic_map_finalize(bmap);
9047 return bmap;
9048 error:
9049 isl_basic_map_free(bmap);
9050 return NULL;
9053 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9054 enum isl_dim_type type)
9056 int i;
9058 if (!map)
9059 return NULL;
9061 if (!isl_space_is_named_or_nested(map->dim, type))
9062 return map;
9064 map = isl_map_cow(map);
9065 if (!map)
9066 return NULL;
9068 for (i = 0; i < map->n; ++i) {
9069 map->p[i] = isl_basic_map_reset(map->p[i], type);
9070 if (!map->p[i])
9071 goto error;
9073 map->dim = isl_space_reset(map->dim, type);
9074 if (!map->dim)
9075 goto error;
9077 return map;
9078 error:
9079 isl_map_free(map);
9080 return NULL;
9083 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9085 if (!bmap)
9086 return NULL;
9088 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9089 return bmap;
9091 bmap = isl_basic_map_cow(bmap);
9092 if (!bmap)
9093 return NULL;
9095 bmap->dim = isl_space_flatten(bmap->dim);
9096 if (!bmap->dim)
9097 goto error;
9099 bmap = isl_basic_map_finalize(bmap);
9101 return bmap;
9102 error:
9103 isl_basic_map_free(bmap);
9104 return NULL;
9107 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9109 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9112 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9113 __isl_take isl_basic_map *bmap)
9115 if (!bmap)
9116 return NULL;
9118 if (!bmap->dim->nested[0])
9119 return bmap;
9121 bmap = isl_basic_map_cow(bmap);
9122 if (!bmap)
9123 return NULL;
9125 bmap->dim = isl_space_flatten_domain(bmap->dim);
9126 if (!bmap->dim)
9127 goto error;
9129 bmap = isl_basic_map_finalize(bmap);
9131 return bmap;
9132 error:
9133 isl_basic_map_free(bmap);
9134 return NULL;
9137 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9138 __isl_take isl_basic_map *bmap)
9140 if (!bmap)
9141 return NULL;
9143 if (!bmap->dim->nested[1])
9144 return bmap;
9146 bmap = isl_basic_map_cow(bmap);
9147 if (!bmap)
9148 return NULL;
9150 bmap->dim = isl_space_flatten_range(bmap->dim);
9151 if (!bmap->dim)
9152 goto error;
9154 bmap = isl_basic_map_finalize(bmap);
9156 return bmap;
9157 error:
9158 isl_basic_map_free(bmap);
9159 return NULL;
9162 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9164 int i;
9166 if (!map)
9167 return NULL;
9169 if (!map->dim->nested[0] && !map->dim->nested[1])
9170 return map;
9172 map = isl_map_cow(map);
9173 if (!map)
9174 return NULL;
9176 for (i = 0; i < map->n; ++i) {
9177 map->p[i] = isl_basic_map_flatten(map->p[i]);
9178 if (!map->p[i])
9179 goto error;
9181 map->dim = isl_space_flatten(map->dim);
9182 if (!map->dim)
9183 goto error;
9185 return map;
9186 error:
9187 isl_map_free(map);
9188 return NULL;
9191 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9193 return (isl_set *)isl_map_flatten((isl_map *)set);
9196 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9198 isl_space *dim, *flat_dim;
9199 isl_map *map;
9201 dim = isl_set_get_space(set);
9202 flat_dim = isl_space_flatten(isl_space_copy(dim));
9203 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9204 map = isl_map_intersect_domain(map, set);
9206 return map;
9209 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9211 int i;
9213 if (!map)
9214 return NULL;
9216 if (!map->dim->nested[0])
9217 return map;
9219 map = isl_map_cow(map);
9220 if (!map)
9221 return NULL;
9223 for (i = 0; i < map->n; ++i) {
9224 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9225 if (!map->p[i])
9226 goto error;
9228 map->dim = isl_space_flatten_domain(map->dim);
9229 if (!map->dim)
9230 goto error;
9232 return map;
9233 error:
9234 isl_map_free(map);
9235 return NULL;
9238 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9240 int i;
9242 if (!map)
9243 return NULL;
9245 if (!map->dim->nested[1])
9246 return map;
9248 map = isl_map_cow(map);
9249 if (!map)
9250 return NULL;
9252 for (i = 0; i < map->n; ++i) {
9253 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9254 if (!map->p[i])
9255 goto error;
9257 map->dim = isl_space_flatten_range(map->dim);
9258 if (!map->dim)
9259 goto error;
9261 return map;
9262 error:
9263 isl_map_free(map);
9264 return NULL;
9267 /* Reorder the dimensions of "bmap" according to the given dim_map
9268 * and set the dimension specification to "dim".
9270 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9271 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9273 isl_basic_map *res;
9275 bmap = isl_basic_map_cow(bmap);
9276 if (!bmap || !dim || !dim_map)
9277 goto error;
9279 res = isl_basic_map_alloc_space(dim,
9280 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9281 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9282 res = isl_basic_map_finalize(res);
9283 return res;
9284 error:
9285 free(dim_map);
9286 isl_basic_map_free(bmap);
9287 isl_space_free(dim);
9288 return NULL;
9291 /* Reorder the dimensions of "map" according to given reordering.
9293 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9294 __isl_take isl_reordering *r)
9296 int i;
9297 struct isl_dim_map *dim_map;
9299 map = isl_map_cow(map);
9300 dim_map = isl_dim_map_from_reordering(r);
9301 if (!map || !r || !dim_map)
9302 goto error;
9304 for (i = 0; i < map->n; ++i) {
9305 struct isl_dim_map *dim_map_i;
9307 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9309 map->p[i] = isl_basic_map_realign(map->p[i],
9310 isl_space_copy(r->dim), dim_map_i);
9312 if (!map->p[i])
9313 goto error;
9316 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9318 isl_reordering_free(r);
9319 free(dim_map);
9320 return map;
9321 error:
9322 free(dim_map);
9323 isl_map_free(map);
9324 isl_reordering_free(r);
9325 return NULL;
9328 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9329 __isl_take isl_reordering *r)
9331 return (isl_set *)isl_map_realign((isl_map *)set, r);
9334 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9335 __isl_take isl_space *model)
9337 isl_ctx *ctx;
9339 if (!map || !model)
9340 goto error;
9342 ctx = isl_space_get_ctx(model);
9343 if (!isl_space_has_named_params(model))
9344 isl_die(ctx, isl_error_invalid,
9345 "model has unnamed parameters", goto error);
9346 if (!isl_space_has_named_params(map->dim))
9347 isl_die(ctx, isl_error_invalid,
9348 "relation has unnamed parameters", goto error);
9349 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9350 isl_reordering *exp;
9352 model = isl_space_drop_dims(model, isl_dim_in,
9353 0, isl_space_dim(model, isl_dim_in));
9354 model = isl_space_drop_dims(model, isl_dim_out,
9355 0, isl_space_dim(model, isl_dim_out));
9356 exp = isl_parameter_alignment_reordering(map->dim, model);
9357 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9358 map = isl_map_realign(map, exp);
9361 isl_space_free(model);
9362 return map;
9363 error:
9364 isl_space_free(model);
9365 isl_map_free(map);
9366 return NULL;
9369 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9370 __isl_take isl_space *model)
9372 return isl_map_align_params(set, model);
9375 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9376 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9377 enum isl_dim_type c2, enum isl_dim_type c3,
9378 enum isl_dim_type c4, enum isl_dim_type c5)
9380 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9381 struct isl_mat *mat;
9382 int i, j, k;
9383 int pos;
9385 if (!bmap)
9386 return NULL;
9387 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9388 isl_basic_map_total_dim(bmap) + 1);
9389 if (!mat)
9390 return NULL;
9391 for (i = 0; i < bmap->n_eq; ++i)
9392 for (j = 0, pos = 0; j < 5; ++j) {
9393 int off = isl_basic_map_offset(bmap, c[j]);
9394 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9395 isl_int_set(mat->row[i][pos],
9396 bmap->eq[i][off + k]);
9397 ++pos;
9401 return mat;
9404 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9405 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9406 enum isl_dim_type c2, enum isl_dim_type c3,
9407 enum isl_dim_type c4, enum isl_dim_type c5)
9409 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9410 struct isl_mat *mat;
9411 int i, j, k;
9412 int pos;
9414 if (!bmap)
9415 return NULL;
9416 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9417 isl_basic_map_total_dim(bmap) + 1);
9418 if (!mat)
9419 return NULL;
9420 for (i = 0; i < bmap->n_ineq; ++i)
9421 for (j = 0, pos = 0; j < 5; ++j) {
9422 int off = isl_basic_map_offset(bmap, c[j]);
9423 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9424 isl_int_set(mat->row[i][pos],
9425 bmap->ineq[i][off + k]);
9426 ++pos;
9430 return mat;
9433 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9434 __isl_take isl_space *dim,
9435 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9436 enum isl_dim_type c2, enum isl_dim_type c3,
9437 enum isl_dim_type c4, enum isl_dim_type c5)
9439 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9440 isl_basic_map *bmap;
9441 unsigned total;
9442 unsigned extra;
9443 int i, j, k, l;
9444 int pos;
9446 if (!dim || !eq || !ineq)
9447 goto error;
9449 if (eq->n_col != ineq->n_col)
9450 isl_die(dim->ctx, isl_error_invalid,
9451 "equalities and inequalities matrices should have "
9452 "same number of columns", goto error);
9454 total = 1 + isl_space_dim(dim, isl_dim_all);
9456 if (eq->n_col < total)
9457 isl_die(dim->ctx, isl_error_invalid,
9458 "number of columns too small", goto error);
9460 extra = eq->n_col - total;
9462 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9463 eq->n_row, ineq->n_row);
9464 if (!bmap)
9465 goto error;
9466 for (i = 0; i < extra; ++i) {
9467 k = isl_basic_map_alloc_div(bmap);
9468 if (k < 0)
9469 goto error;
9470 isl_int_set_si(bmap->div[k][0], 0);
9472 for (i = 0; i < eq->n_row; ++i) {
9473 l = isl_basic_map_alloc_equality(bmap);
9474 if (l < 0)
9475 goto error;
9476 for (j = 0, pos = 0; j < 5; ++j) {
9477 int off = isl_basic_map_offset(bmap, c[j]);
9478 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9479 isl_int_set(bmap->eq[l][off + k],
9480 eq->row[i][pos]);
9481 ++pos;
9485 for (i = 0; i < ineq->n_row; ++i) {
9486 l = isl_basic_map_alloc_inequality(bmap);
9487 if (l < 0)
9488 goto error;
9489 for (j = 0, pos = 0; j < 5; ++j) {
9490 int off = isl_basic_map_offset(bmap, c[j]);
9491 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9492 isl_int_set(bmap->ineq[l][off + k],
9493 ineq->row[i][pos]);
9494 ++pos;
9499 isl_space_free(dim);
9500 isl_mat_free(eq);
9501 isl_mat_free(ineq);
9503 return bmap;
9504 error:
9505 isl_space_free(dim);
9506 isl_mat_free(eq);
9507 isl_mat_free(ineq);
9508 return NULL;
9511 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9512 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9513 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9515 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9516 c1, c2, c3, c4, isl_dim_in);
9519 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9520 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9521 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9523 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9524 c1, c2, c3, c4, isl_dim_in);
9527 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9528 __isl_take isl_space *dim,
9529 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9530 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9532 return (isl_basic_set*)
9533 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9534 c1, c2, c3, c4, isl_dim_in);
9537 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9539 if (!bmap)
9540 return -1;
9542 return isl_space_can_zip(bmap->dim);
9545 int isl_map_can_zip(__isl_keep isl_map *map)
9547 if (!map)
9548 return -1;
9550 return isl_space_can_zip(map->dim);
9553 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9554 * (A -> C) -> (B -> D).
9556 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9558 unsigned pos;
9559 unsigned n1;
9560 unsigned n2;
9562 if (!bmap)
9563 return NULL;
9565 if (!isl_basic_map_can_zip(bmap))
9566 isl_die(bmap->ctx, isl_error_invalid,
9567 "basic map cannot be zipped", goto error);
9568 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9569 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9570 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9571 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9572 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9573 if (!bmap)
9574 return NULL;
9575 bmap->dim = isl_space_zip(bmap->dim);
9576 if (!bmap->dim)
9577 goto error;
9578 return bmap;
9579 error:
9580 isl_basic_map_free(bmap);
9581 return NULL;
9584 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9585 * (A -> C) -> (B -> D).
9587 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9589 int i;
9591 if (!map)
9592 return NULL;
9594 if (!isl_map_can_zip(map))
9595 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9596 goto error);
9598 map = isl_map_cow(map);
9599 if (!map)
9600 return NULL;
9602 for (i = 0; i < map->n; ++i) {
9603 map->p[i] = isl_basic_map_zip(map->p[i]);
9604 if (!map->p[i])
9605 goto error;
9608 map->dim = isl_space_zip(map->dim);
9609 if (!map->dim)
9610 goto error;
9612 return map;
9613 error:
9614 isl_map_free(map);
9615 return NULL;
9618 /* Construct a basic map mapping the domain of the affine expression
9619 * to a one-dimensional range prescribed by the affine expression.
9621 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9623 int k;
9624 int pos;
9625 isl_local_space *ls;
9626 isl_basic_map *bmap;
9628 if (!aff)
9629 return NULL;
9631 ls = isl_aff_get_local_space(aff);
9632 bmap = isl_basic_map_from_local_space(ls);
9633 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9634 k = isl_basic_map_alloc_equality(bmap);
9635 if (k < 0)
9636 goto error;
9638 pos = isl_basic_map_offset(bmap, isl_dim_out);
9639 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9640 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9641 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9642 aff->v->size - (pos + 1));
9644 isl_aff_free(aff);
9645 bmap = isl_basic_map_finalize(bmap);
9646 return bmap;
9647 error:
9648 isl_aff_free(aff);
9649 isl_basic_map_free(bmap);
9650 return NULL;
9653 /* Construct a basic map mapping the domain the multi-affine expression
9654 * to its range, with each dimension in the range equated to the
9655 * corresponding affine expression.
9657 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9658 __isl_take isl_multi_aff *maff)
9660 int i;
9661 isl_space *space;
9662 isl_basic_map *bmap;
9664 if (!maff)
9665 return NULL;
9667 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9668 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9669 "invalid space", return isl_multi_aff_free(maff));
9671 space = isl_space_domain(isl_multi_aff_get_space(maff));
9672 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9674 for (i = 0; i < maff->n; ++i) {
9675 isl_aff *aff;
9676 isl_basic_map *bmap_i;
9678 aff = isl_aff_copy(maff->p[i]);
9679 bmap_i = isl_basic_map_from_aff(aff);
9681 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9684 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9686 isl_multi_aff_free(maff);
9687 return bmap;
9690 /* Construct a basic map mapping a domain in the given space to
9691 * to an n-dimensional range, with n the number of elements in the list,
9692 * where each coordinate in the range is prescribed by the
9693 * corresponding affine expression.
9694 * The domains of all affine expressions in the list are assumed to match
9695 * domain_dim.
9697 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9698 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9700 int i;
9701 isl_space *dim;
9702 isl_basic_map *bmap;
9704 if (!list)
9705 return NULL;
9707 dim = isl_space_from_domain(domain_dim);
9708 bmap = isl_basic_map_universe(dim);
9710 for (i = 0; i < list->n; ++i) {
9711 isl_aff *aff;
9712 isl_basic_map *bmap_i;
9714 aff = isl_aff_copy(list->p[i]);
9715 bmap_i = isl_basic_map_from_aff(aff);
9717 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9720 isl_aff_list_free(list);
9721 return bmap;
9724 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9725 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9727 return isl_map_equate(set, type1, pos1, type2, pos2);
9730 /* Add a constraint imposing that the given two dimensions are equal.
9732 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9733 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9735 isl_basic_map *bmap = NULL;
9736 int i;
9738 if (!map)
9739 return NULL;
9741 if (pos1 >= isl_map_dim(map, type1))
9742 isl_die(map->ctx, isl_error_invalid,
9743 "index out of bounds", goto error);
9744 if (pos2 >= isl_map_dim(map, type2))
9745 isl_die(map->ctx, isl_error_invalid,
9746 "index out of bounds", goto error);
9748 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9749 i = isl_basic_map_alloc_equality(bmap);
9750 if (i < 0)
9751 goto error;
9752 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9753 pos1 += isl_basic_map_offset(bmap, type1);
9754 pos2 += isl_basic_map_offset(bmap, type2);
9755 isl_int_set_si(bmap->eq[i][pos1], -1);
9756 isl_int_set_si(bmap->eq[i][pos2], 1);
9757 bmap = isl_basic_map_finalize(bmap);
9759 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9761 return map;
9762 error:
9763 isl_basic_map_free(bmap);
9764 isl_map_free(map);
9765 return NULL;
9768 /* Add a constraint imposing that the given two dimensions have opposite values.
9770 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9771 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9773 isl_basic_map *bmap = NULL;
9774 int i;
9776 if (!map)
9777 return NULL;
9779 if (pos1 >= isl_map_dim(map, type1))
9780 isl_die(map->ctx, isl_error_invalid,
9781 "index out of bounds", goto error);
9782 if (pos2 >= isl_map_dim(map, type2))
9783 isl_die(map->ctx, isl_error_invalid,
9784 "index out of bounds", goto error);
9786 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9787 i = isl_basic_map_alloc_equality(bmap);
9788 if (i < 0)
9789 goto error;
9790 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9791 pos1 += isl_basic_map_offset(bmap, type1);
9792 pos2 += isl_basic_map_offset(bmap, type2);
9793 isl_int_set_si(bmap->eq[i][pos1], 1);
9794 isl_int_set_si(bmap->eq[i][pos2], 1);
9795 bmap = isl_basic_map_finalize(bmap);
9797 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9799 return map;
9800 error:
9801 isl_basic_map_free(bmap);
9802 isl_map_free(map);
9803 return NULL;
9806 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9807 int pos)
9809 isl_aff *div;
9810 isl_local_space *ls;
9812 if (!bmap)
9813 return NULL;
9815 if (!isl_basic_map_divs_known(bmap))
9816 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9817 "some divs are unknown", return NULL);
9819 ls = isl_basic_map_get_local_space(bmap);
9820 div = isl_local_space_get_div(ls, pos);
9821 isl_local_space_free(ls);
9823 return div;
9826 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9827 int pos)
9829 return isl_basic_map_get_div(bset, pos);
9832 /* Plug in "subs" for dimension "type", "pos" of "bset".
9834 * Let i be the dimension to replace and let "subs" be of the form
9836 * f/d
9838 * Any integer division with a non-zero coefficient for i,
9840 * floor((a i + g)/m)
9842 * is replaced by
9844 * floor((a f + d g)/(m d))
9846 * Constraints of the form
9848 * a i + g
9850 * are replaced by
9852 * a f + d g
9854 __isl_give isl_basic_set *isl_basic_set_substitute(
9855 __isl_take isl_basic_set *bset,
9856 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9858 int i;
9859 isl_int v;
9860 isl_ctx *ctx;
9862 if (bset && isl_basic_set_plain_is_empty(bset))
9863 return bset;
9865 bset = isl_basic_set_cow(bset);
9866 if (!bset || !subs)
9867 goto error;
9869 ctx = isl_basic_set_get_ctx(bset);
9870 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9871 isl_die(ctx, isl_error_invalid,
9872 "spaces don't match", goto error);
9873 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9874 isl_die(ctx, isl_error_unsupported,
9875 "cannot handle divs yet", goto error);
9877 pos += isl_basic_set_offset(bset, type);
9879 isl_int_init(v);
9881 for (i = 0; i < bset->n_eq; ++i) {
9882 if (isl_int_is_zero(bset->eq[i][pos]))
9883 continue;
9884 isl_int_set(v, bset->eq[i][pos]);
9885 isl_int_set_si(bset->eq[i][pos], 0);
9886 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9887 v, subs->v->el + 1, subs->v->size - 1);
9890 for (i = 0; i < bset->n_ineq; ++i) {
9891 if (isl_int_is_zero(bset->ineq[i][pos]))
9892 continue;
9893 isl_int_set(v, bset->ineq[i][pos]);
9894 isl_int_set_si(bset->ineq[i][pos], 0);
9895 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9896 v, subs->v->el + 1, subs->v->size - 1);
9899 for (i = 0; i < bset->n_div; ++i) {
9900 if (isl_int_is_zero(bset->div[i][1 + pos]))
9901 continue;
9902 isl_int_set(v, bset->div[i][1 + pos]);
9903 isl_int_set_si(bset->div[i][1 + pos], 0);
9904 isl_seq_combine(bset->div[i] + 1,
9905 subs->v->el[0], bset->div[i] + 1,
9906 v, subs->v->el + 1, subs->v->size - 1);
9907 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9910 isl_int_clear(v);
9912 bset = isl_basic_set_simplify(bset);
9913 return isl_basic_set_finalize(bset);
9914 error:
9915 isl_basic_set_free(bset);
9916 return NULL;
9919 /* Plug in "subs" for dimension "type", "pos" of "set".
9921 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9922 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9924 int i;
9926 if (set && isl_set_plain_is_empty(set))
9927 return set;
9929 set = isl_set_cow(set);
9930 if (!set || !subs)
9931 goto error;
9933 for (i = set->n - 1; i >= 0; --i) {
9934 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9935 if (!set->p[i])
9936 goto error;
9937 if (isl_basic_set_plain_is_empty(set->p[i])) {
9938 isl_basic_set_free(set->p[i]);
9939 if (i != set->n - 1)
9940 set->p[i] = set->p[set->n - 1];
9941 set->n--;
9945 return set;
9946 error:
9947 isl_set_free(set);
9948 return NULL;