deprecate isl_int
[isl.git] / isl_map.c
blob96f9104dd9f19112b988b265e96e114917f0dfb5
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl_blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
22 #include <isl_seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
31 #include <isl_vec_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
36 #include <isl_morph.h>
37 #include <isl_val_private.h>
38 #include <isl/deprecated/map_int.h>
39 #include <isl/deprecated/set_int.h>
41 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
43 switch (type) {
44 case isl_dim_param: return dim->nparam;
45 case isl_dim_in: return dim->n_in;
46 case isl_dim_out: return dim->n_out;
47 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
48 default: return 0;
52 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
54 switch (type) {
55 case isl_dim_param: return 1;
56 case isl_dim_in: return 1 + dim->nparam;
57 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
58 default: return 0;
62 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
63 enum isl_dim_type type)
65 if (!bmap)
66 return 0;
67 switch (type) {
68 case isl_dim_cst: return 1;
69 case isl_dim_param:
70 case isl_dim_in:
71 case isl_dim_out: return isl_space_dim(bmap->dim, type);
72 case isl_dim_div: return bmap->n_div;
73 case isl_dim_all: return isl_basic_map_total_dim(bmap);
74 default: return 0;
78 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
80 return map ? n(map->dim, type) : 0;
83 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
85 return set ? n(set->dim, type) : 0;
88 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
89 enum isl_dim_type type)
91 isl_space *dim = bmap->dim;
92 switch (type) {
93 case isl_dim_cst: return 0;
94 case isl_dim_param: return 1;
95 case isl_dim_in: return 1 + dim->nparam;
96 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
97 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
98 default: return 0;
102 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
103 enum isl_dim_type type)
105 return isl_basic_map_offset(bset, type);
108 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
110 return pos(map->dim, type);
113 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
114 enum isl_dim_type type)
116 return isl_basic_map_dim(bset, type);
119 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
121 return isl_basic_set_dim(bset, isl_dim_set);
124 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
126 return isl_basic_set_dim(bset, isl_dim_param);
129 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
131 if (!bset)
132 return 0;
133 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
136 unsigned isl_set_n_dim(__isl_keep isl_set *set)
138 return isl_set_dim(set, isl_dim_set);
141 unsigned isl_set_n_param(__isl_keep isl_set *set)
143 return isl_set_dim(set, isl_dim_param);
146 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
148 return bmap ? bmap->dim->n_in : 0;
151 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
153 return bmap ? bmap->dim->n_out : 0;
156 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
158 return bmap ? bmap->dim->nparam : 0;
161 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
163 return bmap ? bmap->n_div : 0;
166 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
168 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
171 unsigned isl_map_n_in(const struct isl_map *map)
173 return map ? map->dim->n_in : 0;
176 unsigned isl_map_n_out(const struct isl_map *map)
178 return map ? map->dim->n_out : 0;
181 unsigned isl_map_n_param(const struct isl_map *map)
183 return map ? map->dim->nparam : 0;
186 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
188 int m;
189 if (!map || !set)
190 return -1;
191 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
192 if (m < 0 || !m)
193 return m;
194 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
197 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
198 struct isl_basic_set *bset)
200 int m;
201 if (!bmap || !bset)
202 return -1;
203 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
204 if (m < 0 || !m)
205 return m;
206 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
209 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
211 int m;
212 if (!map || !set)
213 return -1;
214 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
215 if (m < 0 || !m)
216 return m;
217 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
220 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
221 struct isl_basic_set *bset)
223 int m;
224 if (!bmap || !bset)
225 return -1;
226 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
227 if (m < 0 || !m)
228 return m;
229 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
232 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
234 return bmap ? bmap->ctx : NULL;
237 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
239 return bset ? bset->ctx : NULL;
242 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
244 return map ? map->ctx : NULL;
247 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
249 return set ? set->ctx : NULL;
252 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
254 if (!bmap)
255 return NULL;
256 return isl_space_copy(bmap->dim);
259 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
261 if (!bset)
262 return NULL;
263 return isl_space_copy(bset->dim);
266 /* Extract the divs in "bmap" as a matrix.
268 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
270 int i;
271 isl_ctx *ctx;
272 isl_mat *div;
273 unsigned total;
274 unsigned cols;
276 if (!bmap)
277 return NULL;
279 ctx = isl_basic_map_get_ctx(bmap);
280 total = isl_space_dim(bmap->dim, isl_dim_all);
281 cols = 1 + 1 + total + bmap->n_div;
282 div = isl_mat_alloc(ctx, bmap->n_div, cols);
283 if (!div)
284 return NULL;
286 for (i = 0; i < bmap->n_div; ++i)
287 isl_seq_cpy(div->row[i], bmap->div[i], cols);
289 return div;
292 /* Extract the divs in "bset" as a matrix.
294 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
296 return isl_basic_map_get_divs(bset);
299 __isl_give isl_local_space *isl_basic_map_get_local_space(
300 __isl_keep isl_basic_map *bmap)
302 isl_mat *div;
304 if (!bmap)
305 return NULL;
307 div = isl_basic_map_get_divs(bmap);
308 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
311 __isl_give isl_local_space *isl_basic_set_get_local_space(
312 __isl_keep isl_basic_set *bset)
314 return isl_basic_map_get_local_space(bset);
317 __isl_give isl_basic_map *isl_basic_map_from_local_space(
318 __isl_take isl_local_space *ls)
320 int i;
321 int n_div;
322 isl_basic_map *bmap;
324 if (!ls)
325 return NULL;
327 n_div = isl_local_space_dim(ls, isl_dim_div);
328 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
329 n_div, 0, 2 * n_div);
331 for (i = 0; i < n_div; ++i)
332 if (isl_basic_map_alloc_div(bmap) < 0)
333 goto error;
335 for (i = 0; i < n_div; ++i) {
336 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
337 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
338 goto error;
341 isl_local_space_free(ls);
342 return bmap;
343 error:
344 isl_local_space_free(ls);
345 isl_basic_map_free(bmap);
346 return NULL;
349 __isl_give isl_basic_set *isl_basic_set_from_local_space(
350 __isl_take isl_local_space *ls)
352 return isl_basic_map_from_local_space(ls);
355 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
357 if (!map)
358 return NULL;
359 return isl_space_copy(map->dim);
362 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
364 if (!set)
365 return NULL;
366 return isl_space_copy(set->dim);
369 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
370 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
372 bmap = isl_basic_map_cow(bmap);
373 if (!bmap)
374 return NULL;
375 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
376 if (!bmap->dim)
377 goto error;
378 bmap = isl_basic_map_finalize(bmap);
379 return bmap;
380 error:
381 isl_basic_map_free(bmap);
382 return NULL;
385 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
386 __isl_take isl_basic_set *bset, const char *s)
388 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
391 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
392 enum isl_dim_type type)
394 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
397 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
398 enum isl_dim_type type, const char *s)
400 int i;
402 map = isl_map_cow(map);
403 if (!map)
404 return NULL;
406 map->dim = isl_space_set_tuple_name(map->dim, type, s);
407 if (!map->dim)
408 goto error;
410 for (i = 0; i < map->n; ++i) {
411 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
412 if (!map->p[i])
413 goto error;
416 return map;
417 error:
418 isl_map_free(map);
419 return NULL;
422 /* Does the input or output tuple have a name?
424 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
426 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
429 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
430 enum isl_dim_type type)
432 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
435 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
436 const char *s)
438 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
441 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
442 enum isl_dim_type type, __isl_take isl_id *id)
444 map = isl_map_cow(map);
445 if (!map)
446 return isl_id_free(id);
448 map->dim = isl_space_set_tuple_id(map->dim, type, id);
450 return isl_map_reset_space(map, isl_space_copy(map->dim));
453 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
454 __isl_take isl_id *id)
456 return isl_map_set_tuple_id(set, isl_dim_set, id);
459 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
460 enum isl_dim_type type)
462 map = isl_map_cow(map);
463 if (!map)
464 return NULL;
466 map->dim = isl_space_reset_tuple_id(map->dim, type);
468 return isl_map_reset_space(map, isl_space_copy(map->dim));
471 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
473 return isl_map_reset_tuple_id(set, isl_dim_set);
476 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
478 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
481 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
482 enum isl_dim_type type)
484 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
487 int isl_set_has_tuple_id(__isl_keep isl_set *set)
489 return isl_map_has_tuple_id(set, isl_dim_set);
492 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
494 return isl_map_get_tuple_id(set, isl_dim_set);
497 /* Does the set tuple have a name?
499 int isl_set_has_tuple_name(__isl_keep isl_set *set)
501 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
505 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
507 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
510 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
512 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
515 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
516 enum isl_dim_type type, unsigned pos)
518 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
521 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
522 enum isl_dim_type type, unsigned pos)
524 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
527 /* Does the given dimension have a name?
529 int isl_map_has_dim_name(__isl_keep isl_map *map,
530 enum isl_dim_type type, unsigned pos)
532 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
535 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
536 enum isl_dim_type type, unsigned pos)
538 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
541 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
542 enum isl_dim_type type, unsigned pos)
544 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
547 /* Does the given dimension have a name?
549 int isl_set_has_dim_name(__isl_keep isl_set *set,
550 enum isl_dim_type type, unsigned pos)
552 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
555 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
556 __isl_take isl_basic_map *bmap,
557 enum isl_dim_type type, unsigned pos, const char *s)
559 bmap = isl_basic_map_cow(bmap);
560 if (!bmap)
561 return NULL;
562 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
563 if (!bmap->dim)
564 goto error;
565 return isl_basic_map_finalize(bmap);
566 error:
567 isl_basic_map_free(bmap);
568 return NULL;
571 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
572 enum isl_dim_type type, unsigned pos, const char *s)
574 int i;
576 map = isl_map_cow(map);
577 if (!map)
578 return NULL;
580 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
581 if (!map->dim)
582 goto error;
584 for (i = 0; i < map->n; ++i) {
585 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
586 if (!map->p[i])
587 goto error;
590 return map;
591 error:
592 isl_map_free(map);
593 return NULL;
596 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
597 __isl_take isl_basic_set *bset,
598 enum isl_dim_type type, unsigned pos, const char *s)
600 return (isl_basic_set *)isl_basic_map_set_dim_name(
601 (isl_basic_map *)bset, type, pos, s);
604 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
605 enum isl_dim_type type, unsigned pos, const char *s)
607 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
610 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
611 enum isl_dim_type type, unsigned pos)
613 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
616 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
617 enum isl_dim_type type, unsigned pos)
619 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
622 int isl_map_has_dim_id(__isl_keep isl_map *map,
623 enum isl_dim_type type, unsigned pos)
625 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
628 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
629 enum isl_dim_type type, unsigned pos)
631 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
634 int isl_set_has_dim_id(__isl_keep isl_set *set,
635 enum isl_dim_type type, unsigned pos)
637 return isl_map_has_dim_id(set, type, pos);
640 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
641 enum isl_dim_type type, unsigned pos)
643 return isl_map_get_dim_id(set, type, pos);
646 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
647 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
649 map = isl_map_cow(map);
650 if (!map)
651 return isl_id_free(id);
653 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
655 return isl_map_reset_space(map, isl_space_copy(map->dim));
658 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
659 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
661 return isl_map_set_dim_id(set, type, pos, id);
664 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
665 __isl_keep isl_id *id)
667 if (!map)
668 return -1;
669 return isl_space_find_dim_by_id(map->dim, type, id);
672 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
673 __isl_keep isl_id *id)
675 return isl_map_find_dim_by_id(set, type, id);
678 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
679 const char *name)
681 if (!map)
682 return -1;
683 return isl_space_find_dim_by_name(map->dim, type, name);
686 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
687 const char *name)
689 return isl_map_find_dim_by_name(set, type, name);
692 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
694 if (!bmap)
695 return -1;
696 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
699 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
701 return isl_basic_map_is_rational(bset);
704 /* Does "bmap" contain any rational points?
706 * If "bmap" has an equality for each dimension, equating the dimension
707 * to an integer constant, then it has no rational points, even if it
708 * is marked as rational.
710 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
712 int has_rational = 1;
713 unsigned total;
715 if (!bmap)
716 return -1;
717 if (isl_basic_map_plain_is_empty(bmap))
718 return 0;
719 if (!isl_basic_map_is_rational(bmap))
720 return 0;
721 bmap = isl_basic_map_copy(bmap);
722 bmap = isl_basic_map_implicit_equalities(bmap);
723 if (!bmap)
724 return -1;
725 total = isl_basic_map_total_dim(bmap);
726 if (bmap->n_eq == total) {
727 int i, j;
728 for (i = 0; i < bmap->n_eq; ++i) {
729 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
730 if (j < 0)
731 break;
732 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
733 !isl_int_is_negone(bmap->eq[i][1 + j]))
734 break;
735 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
736 total - j - 1);
737 if (j >= 0)
738 break;
740 if (i == bmap->n_eq)
741 has_rational = 0;
743 isl_basic_map_free(bmap);
745 return has_rational;
748 /* Does "map" contain any rational points?
750 int isl_map_has_rational(__isl_keep isl_map *map)
752 int i;
753 int has_rational;
755 if (!map)
756 return -1;
757 for (i = 0; i < map->n; ++i) {
758 has_rational = isl_basic_map_has_rational(map->p[i]);
759 if (has_rational < 0)
760 return -1;
761 if (has_rational)
762 return 1;
764 return 0;
767 /* Does "set" contain any rational points?
769 int isl_set_has_rational(__isl_keep isl_set *set)
771 return isl_map_has_rational(set);
774 /* Is this basic set a parameter domain?
776 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
778 if (!bset)
779 return -1;
780 return isl_space_is_params(bset->dim);
783 /* Is this set a parameter domain?
785 int isl_set_is_params(__isl_keep isl_set *set)
787 if (!set)
788 return -1;
789 return isl_space_is_params(set->dim);
792 /* Is this map actually a parameter domain?
793 * Users should never call this function. Outside of isl,
794 * a map can never be a parameter domain.
796 int isl_map_is_params(__isl_keep isl_map *map)
798 if (!map)
799 return -1;
800 return isl_space_is_params(map->dim);
803 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
804 struct isl_basic_map *bmap, unsigned extra,
805 unsigned n_eq, unsigned n_ineq)
807 int i;
808 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
810 bmap->ctx = ctx;
811 isl_ctx_ref(ctx);
813 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
814 if (isl_blk_is_error(bmap->block))
815 goto error;
817 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
818 if (!bmap->ineq)
819 goto error;
821 if (extra == 0) {
822 bmap->block2 = isl_blk_empty();
823 bmap->div = NULL;
824 } else {
825 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
826 if (isl_blk_is_error(bmap->block2))
827 goto error;
829 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
830 if (!bmap->div)
831 goto error;
834 for (i = 0; i < n_ineq + n_eq; ++i)
835 bmap->ineq[i] = bmap->block.data + i * row_size;
837 for (i = 0; i < extra; ++i)
838 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
840 bmap->ref = 1;
841 bmap->flags = 0;
842 bmap->c_size = n_eq + n_ineq;
843 bmap->eq = bmap->ineq + n_ineq;
844 bmap->extra = extra;
845 bmap->n_eq = 0;
846 bmap->n_ineq = 0;
847 bmap->n_div = 0;
848 bmap->sample = NULL;
850 return bmap;
851 error:
852 isl_basic_map_free(bmap);
853 return NULL;
856 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
857 unsigned nparam, unsigned dim, unsigned extra,
858 unsigned n_eq, unsigned n_ineq)
860 struct isl_basic_map *bmap;
861 isl_space *space;
863 space = isl_space_set_alloc(ctx, nparam, dim);
864 if (!space)
865 return NULL;
867 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
868 return (struct isl_basic_set *)bmap;
871 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
872 unsigned extra, unsigned n_eq, unsigned n_ineq)
874 struct isl_basic_map *bmap;
875 if (!dim)
876 return NULL;
877 isl_assert(dim->ctx, dim->n_in == 0, goto error);
878 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
879 return (struct isl_basic_set *)bmap;
880 error:
881 isl_space_free(dim);
882 return NULL;
885 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
886 unsigned extra, unsigned n_eq, unsigned n_ineq)
888 struct isl_basic_map *bmap;
890 if (!dim)
891 return NULL;
892 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
893 if (!bmap)
894 goto error;
895 bmap->dim = dim;
897 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
898 error:
899 isl_space_free(dim);
900 return NULL;
903 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
904 unsigned nparam, unsigned in, unsigned out, unsigned extra,
905 unsigned n_eq, unsigned n_ineq)
907 struct isl_basic_map *bmap;
908 isl_space *dim;
910 dim = isl_space_alloc(ctx, nparam, in, out);
911 if (!dim)
912 return NULL;
914 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
915 return bmap;
918 static void dup_constraints(
919 struct isl_basic_map *dst, struct isl_basic_map *src)
921 int i;
922 unsigned total = isl_basic_map_total_dim(src);
924 for (i = 0; i < src->n_eq; ++i) {
925 int j = isl_basic_map_alloc_equality(dst);
926 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
929 for (i = 0; i < src->n_ineq; ++i) {
930 int j = isl_basic_map_alloc_inequality(dst);
931 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
934 for (i = 0; i < src->n_div; ++i) {
935 int j = isl_basic_map_alloc_div(dst);
936 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
938 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
941 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
943 struct isl_basic_map *dup;
945 if (!bmap)
946 return NULL;
947 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
948 bmap->n_div, bmap->n_eq, bmap->n_ineq);
949 if (!dup)
950 return NULL;
951 dup_constraints(dup, bmap);
952 dup->flags = bmap->flags;
953 dup->sample = isl_vec_copy(bmap->sample);
954 return dup;
957 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
959 struct isl_basic_map *dup;
961 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
962 return (struct isl_basic_set *)dup;
965 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
967 if (!bset)
968 return NULL;
970 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
971 bset->ref++;
972 return bset;
974 return isl_basic_set_dup(bset);
977 struct isl_set *isl_set_copy(struct isl_set *set)
979 if (!set)
980 return NULL;
982 set->ref++;
983 return set;
986 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
988 if (!bmap)
989 return NULL;
991 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
992 bmap->ref++;
993 return bmap;
995 bmap = isl_basic_map_dup(bmap);
996 if (bmap)
997 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
998 return bmap;
1001 struct isl_map *isl_map_copy(struct isl_map *map)
1003 if (!map)
1004 return NULL;
1006 map->ref++;
1007 return map;
1010 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1012 if (!bmap)
1013 return NULL;
1015 if (--bmap->ref > 0)
1016 return NULL;
1018 isl_ctx_deref(bmap->ctx);
1019 free(bmap->div);
1020 isl_blk_free(bmap->ctx, bmap->block2);
1021 free(bmap->ineq);
1022 isl_blk_free(bmap->ctx, bmap->block);
1023 isl_vec_free(bmap->sample);
1024 isl_space_free(bmap->dim);
1025 free(bmap);
1027 return NULL;
1030 void *isl_basic_set_free(struct isl_basic_set *bset)
1032 return isl_basic_map_free((struct isl_basic_map *)bset);
1035 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1037 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1040 __isl_give isl_map *isl_map_align_params_map_map_and(
1041 __isl_take isl_map *map1, __isl_take isl_map *map2,
1042 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1043 __isl_take isl_map *map2))
1045 if (!map1 || !map2)
1046 goto error;
1047 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1048 return fn(map1, map2);
1049 if (!isl_space_has_named_params(map1->dim) ||
1050 !isl_space_has_named_params(map2->dim))
1051 isl_die(map1->ctx, isl_error_invalid,
1052 "unaligned unnamed parameters", goto error);
1053 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1054 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1055 return fn(map1, map2);
1056 error:
1057 isl_map_free(map1);
1058 isl_map_free(map2);
1059 return NULL;
1062 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1063 __isl_keep isl_map *map2,
1064 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1066 int r;
1068 if (!map1 || !map2)
1069 return -1;
1070 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1071 return fn(map1, map2);
1072 if (!isl_space_has_named_params(map1->dim) ||
1073 !isl_space_has_named_params(map2->dim))
1074 isl_die(map1->ctx, isl_error_invalid,
1075 "unaligned unnamed parameters", return -1);
1076 map1 = isl_map_copy(map1);
1077 map2 = isl_map_copy(map2);
1078 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1079 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1080 r = fn(map1, map2);
1081 isl_map_free(map1);
1082 isl_map_free(map2);
1083 return r;
1086 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1088 struct isl_ctx *ctx;
1089 if (!bmap)
1090 return -1;
1091 ctx = bmap->ctx;
1092 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1093 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1094 return -1);
1095 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1096 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1097 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1098 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1099 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1100 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1101 isl_int *t;
1102 int j = isl_basic_map_alloc_inequality(bmap);
1103 if (j < 0)
1104 return -1;
1105 t = bmap->ineq[j];
1106 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1107 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1108 bmap->eq[-1] = t;
1109 bmap->n_eq++;
1110 bmap->n_ineq--;
1111 bmap->eq--;
1112 return 0;
1114 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1115 bmap->extra - bmap->n_div);
1116 return bmap->n_eq++;
1119 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1121 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1124 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1126 if (!bmap)
1127 return -1;
1128 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1129 bmap->n_eq -= n;
1130 return 0;
1133 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1135 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1138 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1140 isl_int *t;
1141 if (!bmap)
1142 return -1;
1143 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1145 if (pos != bmap->n_eq - 1) {
1146 t = bmap->eq[pos];
1147 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1148 bmap->eq[bmap->n_eq - 1] = t;
1150 bmap->n_eq--;
1151 return 0;
1154 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1156 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1159 /* Turn inequality "pos" of "bmap" into an equality.
1161 * In particular, we move the inequality in front of the equalities
1162 * and move the last inequality in the position of the moved inequality.
1163 * Note that isl_tab_make_equalities_explicit depends on this particular
1164 * change in the ordering of the constraints.
1166 void isl_basic_map_inequality_to_equality(
1167 struct isl_basic_map *bmap, unsigned pos)
1169 isl_int *t;
1171 t = bmap->ineq[pos];
1172 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1173 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1174 bmap->eq[-1] = t;
1175 bmap->n_eq++;
1176 bmap->n_ineq--;
1177 bmap->eq--;
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1179 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1180 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1181 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1184 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1186 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1189 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1191 struct isl_ctx *ctx;
1192 if (!bmap)
1193 return -1;
1194 ctx = bmap->ctx;
1195 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1196 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1197 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1198 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1199 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1200 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1201 1 + isl_basic_map_total_dim(bmap),
1202 bmap->extra - bmap->n_div);
1203 return bmap->n_ineq++;
1206 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1208 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1211 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1213 if (!bmap)
1214 return -1;
1215 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1216 bmap->n_ineq -= n;
1217 return 0;
1220 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1222 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1225 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1227 isl_int *t;
1228 if (!bmap)
1229 return -1;
1230 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1232 if (pos != bmap->n_ineq - 1) {
1233 t = bmap->ineq[pos];
1234 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1235 bmap->ineq[bmap->n_ineq - 1] = t;
1236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1238 bmap->n_ineq--;
1239 return 0;
1242 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1244 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1247 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1248 isl_int *eq)
1250 int k;
1252 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1253 if (!bmap)
1254 return NULL;
1255 k = isl_basic_map_alloc_equality(bmap);
1256 if (k < 0)
1257 goto error;
1258 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1259 return bmap;
1260 error:
1261 isl_basic_map_free(bmap);
1262 return NULL;
1265 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1266 isl_int *eq)
1268 return (isl_basic_set *)
1269 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1272 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1273 isl_int *ineq)
1275 int k;
1277 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1278 if (!bmap)
1279 return NULL;
1280 k = isl_basic_map_alloc_inequality(bmap);
1281 if (k < 0)
1282 goto error;
1283 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1284 return bmap;
1285 error:
1286 isl_basic_map_free(bmap);
1287 return NULL;
1290 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1291 isl_int *ineq)
1293 return (isl_basic_set *)
1294 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1297 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1299 if (!bmap)
1300 return -1;
1301 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1302 isl_seq_clr(bmap->div[bmap->n_div] +
1303 1 + 1 + isl_basic_map_total_dim(bmap),
1304 bmap->extra - bmap->n_div);
1305 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1306 return bmap->n_div++;
1309 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1311 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1314 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1316 if (!bmap)
1317 return -1;
1318 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1319 bmap->n_div -= n;
1320 return 0;
1323 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1325 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1328 /* Copy constraint from src to dst, putting the vars of src at offset
1329 * dim_off in dst and the divs of src at offset div_off in dst.
1330 * If both sets are actually map, then dim_off applies to the input
1331 * variables.
1333 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1334 struct isl_basic_map *src_map, isl_int *src,
1335 unsigned in_off, unsigned out_off, unsigned div_off)
1337 unsigned src_nparam = isl_basic_map_n_param(src_map);
1338 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1339 unsigned src_in = isl_basic_map_n_in(src_map);
1340 unsigned dst_in = isl_basic_map_n_in(dst_map);
1341 unsigned src_out = isl_basic_map_n_out(src_map);
1342 unsigned dst_out = isl_basic_map_n_out(dst_map);
1343 isl_int_set(dst[0], src[0]);
1344 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1345 if (dst_nparam > src_nparam)
1346 isl_seq_clr(dst+1+src_nparam,
1347 dst_nparam - src_nparam);
1348 isl_seq_clr(dst+1+dst_nparam, in_off);
1349 isl_seq_cpy(dst+1+dst_nparam+in_off,
1350 src+1+src_nparam,
1351 isl_min(dst_in-in_off, src_in));
1352 if (dst_in-in_off > src_in)
1353 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1354 dst_in - in_off - src_in);
1355 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1356 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1357 src+1+src_nparam+src_in,
1358 isl_min(dst_out-out_off, src_out));
1359 if (dst_out-out_off > src_out)
1360 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1361 dst_out - out_off - src_out);
1362 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1363 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1364 src+1+src_nparam+src_in+src_out,
1365 isl_min(dst_map->extra-div_off, src_map->n_div));
1366 if (dst_map->n_div-div_off > src_map->n_div)
1367 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1368 div_off+src_map->n_div,
1369 dst_map->n_div - div_off - src_map->n_div);
1372 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1373 struct isl_basic_map *src_map, isl_int *src,
1374 unsigned in_off, unsigned out_off, unsigned div_off)
1376 isl_int_set(dst[0], src[0]);
1377 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1380 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1381 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1383 int i;
1384 unsigned div_off;
1386 if (!bmap1 || !bmap2)
1387 goto error;
1389 div_off = bmap1->n_div;
1391 for (i = 0; i < bmap2->n_eq; ++i) {
1392 int i1 = isl_basic_map_alloc_equality(bmap1);
1393 if (i1 < 0)
1394 goto error;
1395 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1396 i_pos, o_pos, div_off);
1399 for (i = 0; i < bmap2->n_ineq; ++i) {
1400 int i1 = isl_basic_map_alloc_inequality(bmap1);
1401 if (i1 < 0)
1402 goto error;
1403 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1404 i_pos, o_pos, div_off);
1407 for (i = 0; i < bmap2->n_div; ++i) {
1408 int i1 = isl_basic_map_alloc_div(bmap1);
1409 if (i1 < 0)
1410 goto error;
1411 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1412 i_pos, o_pos, div_off);
1415 isl_basic_map_free(bmap2);
1417 return bmap1;
1419 error:
1420 isl_basic_map_free(bmap1);
1421 isl_basic_map_free(bmap2);
1422 return NULL;
1425 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1426 struct isl_basic_set *bset2, unsigned pos)
1428 return (struct isl_basic_set *)
1429 add_constraints((struct isl_basic_map *)bset1,
1430 (struct isl_basic_map *)bset2, 0, pos);
1433 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1434 __isl_take isl_space *dim, unsigned extra,
1435 unsigned n_eq, unsigned n_ineq)
1437 struct isl_basic_map *ext;
1438 unsigned flags;
1439 int dims_ok;
1441 if (!dim)
1442 goto error;
1444 if (!base)
1445 goto error;
1447 dims_ok = isl_space_is_equal(base->dim, dim) &&
1448 base->extra >= base->n_div + extra;
1450 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1451 room_for_ineq(base, n_ineq)) {
1452 isl_space_free(dim);
1453 return base;
1456 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1457 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1458 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1459 extra += base->extra;
1460 n_eq += base->n_eq;
1461 n_ineq += base->n_ineq;
1463 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1464 dim = NULL;
1465 if (!ext)
1466 goto error;
1468 if (dims_ok)
1469 ext->sample = isl_vec_copy(base->sample);
1470 flags = base->flags;
1471 ext = add_constraints(ext, base, 0, 0);
1472 if (ext) {
1473 ext->flags = flags;
1474 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1477 return ext;
1479 error:
1480 isl_space_free(dim);
1481 isl_basic_map_free(base);
1482 return NULL;
1485 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1486 __isl_take isl_space *dim, unsigned extra,
1487 unsigned n_eq, unsigned n_ineq)
1489 return (struct isl_basic_set *)
1490 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1491 extra, n_eq, n_ineq);
1494 struct isl_basic_map *isl_basic_map_extend_constraints(
1495 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1497 if (!base)
1498 return NULL;
1499 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1500 0, n_eq, n_ineq);
1503 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1504 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1505 unsigned n_eq, unsigned n_ineq)
1507 struct isl_basic_map *bmap;
1508 isl_space *dim;
1510 if (!base)
1511 return NULL;
1512 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1513 if (!dim)
1514 goto error;
1516 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1517 return bmap;
1518 error:
1519 isl_basic_map_free(base);
1520 return NULL;
1523 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1524 unsigned nparam, unsigned dim, unsigned extra,
1525 unsigned n_eq, unsigned n_ineq)
1527 return (struct isl_basic_set *)
1528 isl_basic_map_extend((struct isl_basic_map *)base,
1529 nparam, 0, dim, extra, n_eq, n_ineq);
1532 struct isl_basic_set *isl_basic_set_extend_constraints(
1533 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1535 return (struct isl_basic_set *)
1536 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1537 n_eq, n_ineq);
1540 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1542 return (struct isl_basic_set *)
1543 isl_basic_map_cow((struct isl_basic_map *)bset);
1546 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1548 if (!bmap)
1549 return NULL;
1551 if (bmap->ref > 1) {
1552 bmap->ref--;
1553 bmap = isl_basic_map_dup(bmap);
1555 if (bmap)
1556 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1557 return bmap;
1560 struct isl_set *isl_set_cow(struct isl_set *set)
1562 if (!set)
1563 return NULL;
1565 if (set->ref == 1)
1566 return set;
1567 set->ref--;
1568 return isl_set_dup(set);
1571 struct isl_map *isl_map_cow(struct isl_map *map)
1573 if (!map)
1574 return NULL;
1576 if (map->ref == 1)
1577 return map;
1578 map->ref--;
1579 return isl_map_dup(map);
1582 static void swap_vars(struct isl_blk blk, isl_int *a,
1583 unsigned a_len, unsigned b_len)
1585 isl_seq_cpy(blk.data, a+a_len, b_len);
1586 isl_seq_cpy(blk.data+b_len, a, a_len);
1587 isl_seq_cpy(a, blk.data, b_len+a_len);
1590 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1591 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1593 int i;
1594 struct isl_blk blk;
1596 if (!bmap)
1597 goto error;
1599 isl_assert(bmap->ctx,
1600 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1602 if (n1 == 0 || n2 == 0)
1603 return bmap;
1605 bmap = isl_basic_map_cow(bmap);
1606 if (!bmap)
1607 return NULL;
1609 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1610 if (isl_blk_is_error(blk))
1611 goto error;
1613 for (i = 0; i < bmap->n_eq; ++i)
1614 swap_vars(blk,
1615 bmap->eq[i] + pos, n1, n2);
1617 for (i = 0; i < bmap->n_ineq; ++i)
1618 swap_vars(blk,
1619 bmap->ineq[i] + pos, n1, n2);
1621 for (i = 0; i < bmap->n_div; ++i)
1622 swap_vars(blk,
1623 bmap->div[i]+1 + pos, n1, n2);
1625 isl_blk_free(bmap->ctx, blk);
1627 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1628 bmap = isl_basic_map_gauss(bmap, NULL);
1629 return isl_basic_map_finalize(bmap);
1630 error:
1631 isl_basic_map_free(bmap);
1632 return NULL;
1635 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1636 __isl_take isl_basic_set *bset, unsigned n)
1638 unsigned dim;
1639 unsigned nparam;
1641 if (!bset)
1642 return NULL;
1644 nparam = isl_basic_set_n_param(bset);
1645 dim = isl_basic_set_n_dim(bset);
1646 isl_assert(bset->ctx, n <= dim, goto error);
1648 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1649 error:
1650 isl_basic_set_free(bset);
1651 return NULL;
1654 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1656 int i = 0;
1657 unsigned total;
1658 if (!bmap)
1659 goto error;
1660 total = isl_basic_map_total_dim(bmap);
1661 isl_basic_map_free_div(bmap, bmap->n_div);
1662 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1663 if (bmap->n_eq > 0)
1664 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1665 else {
1666 i = isl_basic_map_alloc_equality(bmap);
1667 if (i < 0)
1668 goto error;
1670 isl_int_set_si(bmap->eq[i][0], 1);
1671 isl_seq_clr(bmap->eq[i]+1, total);
1672 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1673 isl_vec_free(bmap->sample);
1674 bmap->sample = NULL;
1675 return isl_basic_map_finalize(bmap);
1676 error:
1677 isl_basic_map_free(bmap);
1678 return NULL;
1681 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1683 return (struct isl_basic_set *)
1684 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1687 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1688 * of "bmap").
1690 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1692 isl_int *t = bmap->div[a];
1693 bmap->div[a] = bmap->div[b];
1694 bmap->div[b] = t;
1697 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1698 * div definitions accordingly.
1700 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1702 int i;
1703 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1705 swap_div(bmap, a, b);
1707 for (i = 0; i < bmap->n_eq; ++i)
1708 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1710 for (i = 0; i < bmap->n_ineq; ++i)
1711 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1713 for (i = 0; i < bmap->n_div; ++i)
1714 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1715 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1718 /* Eliminate the specified n dimensions starting at first from the
1719 * constraints, without removing the dimensions from the space.
1720 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1722 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1723 enum isl_dim_type type, unsigned first, unsigned n)
1725 int i;
1727 if (!map)
1728 return NULL;
1729 if (n == 0)
1730 return map;
1732 if (first + n > isl_map_dim(map, type) || first + n < first)
1733 isl_die(map->ctx, isl_error_invalid,
1734 "index out of bounds", goto error);
1736 map = isl_map_cow(map);
1737 if (!map)
1738 return NULL;
1740 for (i = 0; i < map->n; ++i) {
1741 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1742 if (!map->p[i])
1743 goto error;
1745 return map;
1746 error:
1747 isl_map_free(map);
1748 return NULL;
1751 /* Eliminate the specified n dimensions starting at first from the
1752 * constraints, without removing the dimensions from the space.
1753 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1755 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1756 enum isl_dim_type type, unsigned first, unsigned n)
1758 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1761 /* Eliminate the specified n dimensions starting at first from the
1762 * constraints, without removing the dimensions from the space.
1763 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1765 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1766 unsigned first, unsigned n)
1768 return isl_set_eliminate(set, isl_dim_set, first, n);
1771 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1772 __isl_take isl_basic_map *bmap)
1774 if (!bmap)
1775 return NULL;
1776 bmap = isl_basic_map_eliminate_vars(bmap,
1777 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1778 if (!bmap)
1779 return NULL;
1780 bmap->n_div = 0;
1781 return isl_basic_map_finalize(bmap);
1784 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1785 __isl_take isl_basic_set *bset)
1787 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1788 (struct isl_basic_map *)bset);
1791 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1793 int i;
1795 if (!map)
1796 return NULL;
1797 if (map->n == 0)
1798 return map;
1800 map = isl_map_cow(map);
1801 if (!map)
1802 return NULL;
1804 for (i = 0; i < map->n; ++i) {
1805 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1806 if (!map->p[i])
1807 goto error;
1809 return map;
1810 error:
1811 isl_map_free(map);
1812 return NULL;
1815 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1817 return isl_map_remove_divs(set);
1820 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1821 enum isl_dim_type type, unsigned first, unsigned n)
1823 if (!bmap)
1824 return NULL;
1825 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1826 goto error);
1827 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1828 return bmap;
1829 bmap = isl_basic_map_eliminate_vars(bmap,
1830 isl_basic_map_offset(bmap, type) - 1 + first, n);
1831 if (!bmap)
1832 return bmap;
1833 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1834 return bmap;
1835 bmap = isl_basic_map_drop(bmap, type, first, n);
1836 return bmap;
1837 error:
1838 isl_basic_map_free(bmap);
1839 return NULL;
1842 /* Return true if the definition of the given div (recursively) involves
1843 * any of the given variables.
1845 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1846 unsigned first, unsigned n)
1848 int i;
1849 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1851 if (isl_int_is_zero(bmap->div[div][0]))
1852 return 0;
1853 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1854 return 1;
1856 for (i = bmap->n_div - 1; i >= 0; --i) {
1857 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1858 continue;
1859 if (div_involves_vars(bmap, i, first, n))
1860 return 1;
1863 return 0;
1866 /* Try and add a lower and/or upper bound on "div" to "bmap"
1867 * based on inequality "i".
1868 * "total" is the total number of variables (excluding the divs).
1869 * "v" is a temporary object that can be used during the calculations.
1870 * If "lb" is set, then a lower bound should be constructed.
1871 * If "ub" is set, then an upper bound should be constructed.
1873 * The calling function has already checked that the inequality does not
1874 * reference "div", but we still need to check that the inequality is
1875 * of the right form. We'll consider the case where we want to construct
1876 * a lower bound. The construction of upper bounds is similar.
1878 * Let "div" be of the form
1880 * q = floor((a + f(x))/d)
1882 * We essentially check if constraint "i" is of the form
1884 * b + f(x) >= 0
1886 * so that we can use it to derive a lower bound on "div".
1887 * However, we allow a slightly more general form
1889 * b + g(x) >= 0
1891 * with the condition that the coefficients of g(x) - f(x) are all
1892 * divisible by d.
1893 * Rewriting this constraint as
1895 * 0 >= -b - g(x)
1897 * adding a + f(x) to both sides and dividing by d, we obtain
1899 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1901 * Taking the floor on both sides, we obtain
1903 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1905 * or
1907 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1909 * In the case of an upper bound, we construct the constraint
1911 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1914 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1915 __isl_take isl_basic_map *bmap, int div, int i,
1916 unsigned total, isl_int v, int lb, int ub)
1918 int j;
1920 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1921 if (lb) {
1922 isl_int_sub(v, bmap->ineq[i][1 + j],
1923 bmap->div[div][1 + 1 + j]);
1924 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1926 if (ub) {
1927 isl_int_add(v, bmap->ineq[i][1 + j],
1928 bmap->div[div][1 + 1 + j]);
1929 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1932 if (!lb && !ub)
1933 return bmap;
1935 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1936 if (lb) {
1937 int k = isl_basic_map_alloc_inequality(bmap);
1938 if (k < 0)
1939 goto error;
1940 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1941 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1942 bmap->div[div][1 + j]);
1943 isl_int_cdiv_q(bmap->ineq[k][j],
1944 bmap->ineq[k][j], bmap->div[div][0]);
1946 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1948 if (ub) {
1949 int k = isl_basic_map_alloc_inequality(bmap);
1950 if (k < 0)
1951 goto error;
1952 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1953 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1954 bmap->div[div][1 + j]);
1955 isl_int_fdiv_q(bmap->ineq[k][j],
1956 bmap->ineq[k][j], bmap->div[div][0]);
1958 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1961 return bmap;
1962 error:
1963 isl_basic_map_free(bmap);
1964 return NULL;
1967 /* This function is called right before "div" is eliminated from "bmap"
1968 * using Fourier-Motzkin.
1969 * Look through the constraints of "bmap" for constraints on the argument
1970 * of the integer division and use them to construct constraints on the
1971 * integer division itself. These constraints can then be combined
1972 * during the Fourier-Motzkin elimination.
1973 * Note that it is only useful to introduce lower bounds on "div"
1974 * if "bmap" already contains upper bounds on "div" as the newly
1975 * introduce lower bounds can then be combined with the pre-existing
1976 * upper bounds. Similarly for upper bounds.
1977 * We therefore first check if "bmap" contains any lower and/or upper bounds
1978 * on "div".
1980 * It is interesting to note that the introduction of these constraints
1981 * can indeed lead to more accurate results, even when compared to
1982 * deriving constraints on the argument of "div" from constraints on "div".
1983 * Consider, for example, the set
1985 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1987 * The second constraint can be rewritten as
1989 * 2 * [(-i-2j+3)/4] + k >= 0
1991 * from which we can derive
1993 * -i - 2j + 3 >= -2k
1995 * or
1997 * i + 2j <= 3 + 2k
1999 * Combined with the first constraint, we obtain
2001 * -3 <= 3 + 2k or k >= -3
2003 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2004 * the first constraint, we obtain
2006 * [(i + 2j)/4] >= [-3/4] = -1
2008 * Combining this constraint with the second constraint, we obtain
2010 * k >= -2
2012 static __isl_give isl_basic_map *insert_bounds_on_div(
2013 __isl_take isl_basic_map *bmap, int div)
2015 int i;
2016 int check_lb, check_ub;
2017 isl_int v;
2018 unsigned total;
2020 if (!bmap)
2021 return NULL;
2023 if (isl_int_is_zero(bmap->div[div][0]))
2024 return bmap;
2026 total = isl_space_dim(bmap->dim, isl_dim_all);
2028 check_lb = 0;
2029 check_ub = 0;
2030 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2031 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2032 if (s > 0)
2033 check_ub = 1;
2034 if (s < 0)
2035 check_lb = 1;
2038 if (!check_lb && !check_ub)
2039 return bmap;
2041 isl_int_init(v);
2043 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2044 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2045 continue;
2047 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2048 check_lb, check_ub);
2051 isl_int_clear(v);
2053 return bmap;
2056 /* Remove all divs (recursively) involving any of the given dimensions
2057 * in their definitions.
2059 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2060 __isl_take isl_basic_map *bmap,
2061 enum isl_dim_type type, unsigned first, unsigned n)
2063 int i;
2065 if (!bmap)
2066 return NULL;
2067 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2068 goto error);
2069 first += isl_basic_map_offset(bmap, type);
2071 for (i = bmap->n_div - 1; i >= 0; --i) {
2072 if (!div_involves_vars(bmap, i, first, n))
2073 continue;
2074 bmap = insert_bounds_on_div(bmap, i);
2075 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2076 if (!bmap)
2077 return NULL;
2078 i = bmap->n_div;
2081 return bmap;
2082 error:
2083 isl_basic_map_free(bmap);
2084 return NULL;
2087 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2088 __isl_take isl_basic_set *bset,
2089 enum isl_dim_type type, unsigned first, unsigned n)
2091 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2094 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2095 enum isl_dim_type type, unsigned first, unsigned n)
2097 int i;
2099 if (!map)
2100 return NULL;
2101 if (map->n == 0)
2102 return map;
2104 map = isl_map_cow(map);
2105 if (!map)
2106 return NULL;
2108 for (i = 0; i < map->n; ++i) {
2109 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2110 type, first, n);
2111 if (!map->p[i])
2112 goto error;
2114 return map;
2115 error:
2116 isl_map_free(map);
2117 return NULL;
2120 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2121 enum isl_dim_type type, unsigned first, unsigned n)
2123 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2124 type, first, n);
2127 /* Does the desciption of "bmap" depend on the specified dimensions?
2128 * We also check whether the dimensions appear in any of the div definitions.
2129 * In principle there is no need for this check. If the dimensions appear
2130 * in a div definition, they also appear in the defining constraints of that
2131 * div.
2133 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2134 enum isl_dim_type type, unsigned first, unsigned n)
2136 int i;
2138 if (!bmap)
2139 return -1;
2141 if (first + n > isl_basic_map_dim(bmap, type))
2142 isl_die(bmap->ctx, isl_error_invalid,
2143 "index out of bounds", return -1);
2145 first += isl_basic_map_offset(bmap, type);
2146 for (i = 0; i < bmap->n_eq; ++i)
2147 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2148 return 1;
2149 for (i = 0; i < bmap->n_ineq; ++i)
2150 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2151 return 1;
2152 for (i = 0; i < bmap->n_div; ++i) {
2153 if (isl_int_is_zero(bmap->div[i][0]))
2154 continue;
2155 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2156 return 1;
2159 return 0;
2162 int isl_map_involves_dims(__isl_keep isl_map *map,
2163 enum isl_dim_type type, unsigned first, unsigned n)
2165 int i;
2167 if (!map)
2168 return -1;
2170 if (first + n > isl_map_dim(map, type))
2171 isl_die(map->ctx, isl_error_invalid,
2172 "index out of bounds", return -1);
2174 for (i = 0; i < map->n; ++i) {
2175 int involves = isl_basic_map_involves_dims(map->p[i],
2176 type, first, n);
2177 if (involves < 0 || involves)
2178 return involves;
2181 return 0;
2184 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2185 enum isl_dim_type type, unsigned first, unsigned n)
2187 return isl_basic_map_involves_dims(bset, type, first, n);
2190 int isl_set_involves_dims(__isl_keep isl_set *set,
2191 enum isl_dim_type type, unsigned first, unsigned n)
2193 return isl_map_involves_dims(set, type, first, n);
2196 /* Return true if the definition of the given div is unknown or depends
2197 * on unknown divs.
2199 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2201 int i;
2202 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2204 if (isl_int_is_zero(bmap->div[div][0]))
2205 return 1;
2207 for (i = bmap->n_div - 1; i >= 0; --i) {
2208 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2209 continue;
2210 if (div_is_unknown(bmap, i))
2211 return 1;
2214 return 0;
2217 /* Remove all divs that are unknown or defined in terms of unknown divs.
2219 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2220 __isl_take isl_basic_map *bmap)
2222 int i;
2224 if (!bmap)
2225 return NULL;
2227 for (i = bmap->n_div - 1; i >= 0; --i) {
2228 if (!div_is_unknown(bmap, i))
2229 continue;
2230 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2231 if (!bmap)
2232 return NULL;
2233 i = bmap->n_div;
2236 return bmap;
2239 /* Remove all divs that are unknown or defined in terms of unknown divs.
2241 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2242 __isl_take isl_basic_set *bset)
2244 return isl_basic_map_remove_unknown_divs(bset);
2247 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2249 int i;
2251 if (!map)
2252 return NULL;
2253 if (map->n == 0)
2254 return map;
2256 map = isl_map_cow(map);
2257 if (!map)
2258 return NULL;
2260 for (i = 0; i < map->n; ++i) {
2261 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2262 if (!map->p[i])
2263 goto error;
2265 return map;
2266 error:
2267 isl_map_free(map);
2268 return NULL;
2271 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2273 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2276 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2277 __isl_take isl_basic_set *bset,
2278 enum isl_dim_type type, unsigned first, unsigned n)
2280 return (isl_basic_set *)
2281 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2284 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2285 enum isl_dim_type type, unsigned first, unsigned n)
2287 int i;
2289 if (n == 0)
2290 return map;
2292 map = isl_map_cow(map);
2293 if (!map)
2294 return NULL;
2295 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2297 for (i = 0; i < map->n; ++i) {
2298 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2299 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2300 if (!map->p[i])
2301 goto error;
2303 map = isl_map_drop(map, type, first, n);
2304 return map;
2305 error:
2306 isl_map_free(map);
2307 return NULL;
2310 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2311 enum isl_dim_type type, unsigned first, unsigned n)
2313 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2316 /* Project out n inputs starting at first using Fourier-Motzkin */
2317 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2318 unsigned first, unsigned n)
2320 return isl_map_remove_dims(map, isl_dim_in, first, n);
2323 static void dump_term(struct isl_basic_map *bmap,
2324 isl_int c, int pos, FILE *out)
2326 const char *name;
2327 unsigned in = isl_basic_map_n_in(bmap);
2328 unsigned dim = in + isl_basic_map_n_out(bmap);
2329 unsigned nparam = isl_basic_map_n_param(bmap);
2330 if (!pos)
2331 isl_int_print(out, c, 0);
2332 else {
2333 if (!isl_int_is_one(c))
2334 isl_int_print(out, c, 0);
2335 if (pos < 1 + nparam) {
2336 name = isl_space_get_dim_name(bmap->dim,
2337 isl_dim_param, pos - 1);
2338 if (name)
2339 fprintf(out, "%s", name);
2340 else
2341 fprintf(out, "p%d", pos - 1);
2342 } else if (pos < 1 + nparam + in)
2343 fprintf(out, "i%d", pos - 1 - nparam);
2344 else if (pos < 1 + nparam + dim)
2345 fprintf(out, "o%d", pos - 1 - nparam - in);
2346 else
2347 fprintf(out, "e%d", pos - 1 - nparam - dim);
2351 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2352 int sign, FILE *out)
2354 int i;
2355 int first;
2356 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2357 isl_int v;
2359 isl_int_init(v);
2360 for (i = 0, first = 1; i < len; ++i) {
2361 if (isl_int_sgn(c[i]) * sign <= 0)
2362 continue;
2363 if (!first)
2364 fprintf(out, " + ");
2365 first = 0;
2366 isl_int_abs(v, c[i]);
2367 dump_term(bmap, v, i, out);
2369 isl_int_clear(v);
2370 if (first)
2371 fprintf(out, "0");
2374 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2375 const char *op, FILE *out, int indent)
2377 int i;
2379 fprintf(out, "%*s", indent, "");
2381 dump_constraint_sign(bmap, c, 1, out);
2382 fprintf(out, " %s ", op);
2383 dump_constraint_sign(bmap, c, -1, out);
2385 fprintf(out, "\n");
2387 for (i = bmap->n_div; i < bmap->extra; ++i) {
2388 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2389 continue;
2390 fprintf(out, "%*s", indent, "");
2391 fprintf(out, "ERROR: unused div coefficient not zero\n");
2392 abort();
2396 static void dump_constraints(struct isl_basic_map *bmap,
2397 isl_int **c, unsigned n,
2398 const char *op, FILE *out, int indent)
2400 int i;
2402 for (i = 0; i < n; ++i)
2403 dump_constraint(bmap, c[i], op, out, indent);
2406 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2408 int j;
2409 int first = 1;
2410 unsigned total = isl_basic_map_total_dim(bmap);
2412 for (j = 0; j < 1 + total; ++j) {
2413 if (isl_int_is_zero(exp[j]))
2414 continue;
2415 if (!first && isl_int_is_pos(exp[j]))
2416 fprintf(out, "+");
2417 dump_term(bmap, exp[j], j, out);
2418 first = 0;
2422 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2424 int i;
2426 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2427 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2429 for (i = 0; i < bmap->n_div; ++i) {
2430 fprintf(out, "%*s", indent, "");
2431 fprintf(out, "e%d = [(", i);
2432 dump_affine(bmap, bmap->div[i]+1, out);
2433 fprintf(out, ")/");
2434 isl_int_print(out, bmap->div[i][0], 0);
2435 fprintf(out, "]\n");
2439 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2440 FILE *out, int indent)
2442 if (!bset) {
2443 fprintf(out, "null basic set\n");
2444 return;
2447 fprintf(out, "%*s", indent, "");
2448 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2449 bset->ref, bset->dim->nparam, bset->dim->n_out,
2450 bset->extra, bset->flags);
2451 dump((struct isl_basic_map *)bset, out, indent);
2454 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2455 FILE *out, int indent)
2457 if (!bmap) {
2458 fprintf(out, "null basic map\n");
2459 return;
2462 fprintf(out, "%*s", indent, "");
2463 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2464 "flags: %x, n_name: %d\n",
2465 bmap->ref,
2466 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2467 bmap->extra, bmap->flags, bmap->dim->n_id);
2468 dump(bmap, out, indent);
2471 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2473 unsigned total;
2474 if (!bmap)
2475 return -1;
2476 total = isl_basic_map_total_dim(bmap);
2477 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2478 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2479 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2480 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2481 return 0;
2484 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2485 unsigned flags)
2487 struct isl_set *set;
2489 if (!dim)
2490 return NULL;
2491 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2492 isl_assert(dim->ctx, n >= 0, goto error);
2493 set = isl_alloc(dim->ctx, struct isl_set,
2494 sizeof(struct isl_set) +
2495 (n - 1) * sizeof(struct isl_basic_set *));
2496 if (!set)
2497 goto error;
2499 set->ctx = dim->ctx;
2500 isl_ctx_ref(set->ctx);
2501 set->ref = 1;
2502 set->size = n;
2503 set->n = 0;
2504 set->dim = dim;
2505 set->flags = flags;
2506 return set;
2507 error:
2508 isl_space_free(dim);
2509 return NULL;
2512 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2513 unsigned nparam, unsigned dim, int n, unsigned flags)
2515 struct isl_set *set;
2516 isl_space *dims;
2518 dims = isl_space_alloc(ctx, nparam, 0, dim);
2519 if (!dims)
2520 return NULL;
2522 set = isl_set_alloc_space(dims, n, flags);
2523 return set;
2526 /* Make sure "map" has room for at least "n" more basic maps.
2528 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2530 int i;
2531 struct isl_map *grown = NULL;
2533 if (!map)
2534 return NULL;
2535 isl_assert(map->ctx, n >= 0, goto error);
2536 if (map->n + n <= map->size)
2537 return map;
2538 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2539 if (!grown)
2540 goto error;
2541 for (i = 0; i < map->n; ++i) {
2542 grown->p[i] = isl_basic_map_copy(map->p[i]);
2543 if (!grown->p[i])
2544 goto error;
2545 grown->n++;
2547 isl_map_free(map);
2548 return grown;
2549 error:
2550 isl_map_free(grown);
2551 isl_map_free(map);
2552 return NULL;
2555 /* Make sure "set" has room for at least "n" more basic sets.
2557 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2559 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2562 struct isl_set *isl_set_dup(struct isl_set *set)
2564 int i;
2565 struct isl_set *dup;
2567 if (!set)
2568 return NULL;
2570 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2571 if (!dup)
2572 return NULL;
2573 for (i = 0; i < set->n; ++i)
2574 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2575 return dup;
2578 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2580 return isl_map_from_basic_map(bset);
2583 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2585 struct isl_map *map;
2587 if (!bmap)
2588 return NULL;
2590 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2591 return isl_map_add_basic_map(map, bmap);
2594 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2595 __isl_take isl_basic_set *bset)
2597 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2598 (struct isl_basic_map *)bset);
2601 void *isl_set_free(__isl_take isl_set *set)
2603 int i;
2605 if (!set)
2606 return NULL;
2608 if (--set->ref > 0)
2609 return NULL;
2611 isl_ctx_deref(set->ctx);
2612 for (i = 0; i < set->n; ++i)
2613 isl_basic_set_free(set->p[i]);
2614 isl_space_free(set->dim);
2615 free(set);
2617 return NULL;
2620 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2622 int i;
2624 if (!set) {
2625 fprintf(out, "null set\n");
2626 return;
2629 fprintf(out, "%*s", indent, "");
2630 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2631 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2632 set->flags);
2633 for (i = 0; i < set->n; ++i) {
2634 fprintf(out, "%*s", indent, "");
2635 fprintf(out, "basic set %d:\n", i);
2636 isl_basic_set_print_internal(set->p[i], out, indent+4);
2640 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2642 int i;
2644 if (!map) {
2645 fprintf(out, "null map\n");
2646 return;
2649 fprintf(out, "%*s", indent, "");
2650 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2651 "flags: %x, n_name: %d\n",
2652 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2653 map->dim->n_out, map->flags, map->dim->n_id);
2654 for (i = 0; i < map->n; ++i) {
2655 fprintf(out, "%*s", indent, "");
2656 fprintf(out, "basic map %d:\n", i);
2657 isl_basic_map_print_internal(map->p[i], out, indent+4);
2661 struct isl_basic_map *isl_basic_map_intersect_domain(
2662 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2664 struct isl_basic_map *bmap_domain;
2666 if (!bmap || !bset)
2667 goto error;
2669 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2670 bset->dim, isl_dim_param), goto error);
2672 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2673 isl_assert(bset->ctx,
2674 isl_basic_map_compatible_domain(bmap, bset), goto error);
2676 bmap = isl_basic_map_cow(bmap);
2677 if (!bmap)
2678 goto error;
2679 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2680 bset->n_div, bset->n_eq, bset->n_ineq);
2681 bmap_domain = isl_basic_map_from_domain(bset);
2682 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2684 bmap = isl_basic_map_simplify(bmap);
2685 return isl_basic_map_finalize(bmap);
2686 error:
2687 isl_basic_map_free(bmap);
2688 isl_basic_set_free(bset);
2689 return NULL;
2692 struct isl_basic_map *isl_basic_map_intersect_range(
2693 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2695 struct isl_basic_map *bmap_range;
2697 if (!bmap || !bset)
2698 goto error;
2700 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2701 bset->dim, isl_dim_param), goto error);
2703 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2704 isl_assert(bset->ctx,
2705 isl_basic_map_compatible_range(bmap, bset), goto error);
2707 if (isl_basic_set_is_universe(bset)) {
2708 isl_basic_set_free(bset);
2709 return bmap;
2712 bmap = isl_basic_map_cow(bmap);
2713 if (!bmap)
2714 goto error;
2715 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2716 bset->n_div, bset->n_eq, bset->n_ineq);
2717 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2718 bmap = add_constraints(bmap, bmap_range, 0, 0);
2720 bmap = isl_basic_map_simplify(bmap);
2721 return isl_basic_map_finalize(bmap);
2722 error:
2723 isl_basic_map_free(bmap);
2724 isl_basic_set_free(bset);
2725 return NULL;
2728 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2730 int i;
2731 unsigned total;
2732 isl_int s;
2734 if (!bmap || !vec)
2735 return -1;
2737 total = 1 + isl_basic_map_total_dim(bmap);
2738 if (total != vec->size)
2739 return -1;
2741 isl_int_init(s);
2743 for (i = 0; i < bmap->n_eq; ++i) {
2744 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2745 if (!isl_int_is_zero(s)) {
2746 isl_int_clear(s);
2747 return 0;
2751 for (i = 0; i < bmap->n_ineq; ++i) {
2752 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2753 if (isl_int_is_neg(s)) {
2754 isl_int_clear(s);
2755 return 0;
2759 isl_int_clear(s);
2761 return 1;
2764 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2766 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2769 struct isl_basic_map *isl_basic_map_intersect(
2770 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2772 struct isl_vec *sample = NULL;
2774 if (!bmap1 || !bmap2)
2775 goto error;
2777 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2778 bmap2->dim, isl_dim_param), goto error);
2779 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2780 isl_space_dim(bmap1->dim, isl_dim_param) &&
2781 isl_space_dim(bmap2->dim, isl_dim_all) !=
2782 isl_space_dim(bmap2->dim, isl_dim_param))
2783 return isl_basic_map_intersect(bmap2, bmap1);
2785 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2786 isl_space_dim(bmap2->dim, isl_dim_param))
2787 isl_assert(bmap1->ctx,
2788 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2790 if (bmap1->sample &&
2791 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2792 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2793 sample = isl_vec_copy(bmap1->sample);
2794 else if (bmap2->sample &&
2795 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2796 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2797 sample = isl_vec_copy(bmap2->sample);
2799 bmap1 = isl_basic_map_cow(bmap1);
2800 if (!bmap1)
2801 goto error;
2802 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2803 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2804 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2806 if (!bmap1)
2807 isl_vec_free(sample);
2808 else if (sample) {
2809 isl_vec_free(bmap1->sample);
2810 bmap1->sample = sample;
2813 bmap1 = isl_basic_map_simplify(bmap1);
2814 return isl_basic_map_finalize(bmap1);
2815 error:
2816 if (sample)
2817 isl_vec_free(sample);
2818 isl_basic_map_free(bmap1);
2819 isl_basic_map_free(bmap2);
2820 return NULL;
2823 struct isl_basic_set *isl_basic_set_intersect(
2824 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2826 return (struct isl_basic_set *)
2827 isl_basic_map_intersect(
2828 (struct isl_basic_map *)bset1,
2829 (struct isl_basic_map *)bset2);
2832 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2833 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2835 return isl_basic_set_intersect(bset1, bset2);
2838 /* Special case of isl_map_intersect, where both map1 and map2
2839 * are convex, without any divs and such that either map1 or map2
2840 * contains a single constraint. This constraint is then simply
2841 * added to the other map.
2843 static __isl_give isl_map *map_intersect_add_constraint(
2844 __isl_take isl_map *map1, __isl_take isl_map *map2)
2846 isl_assert(map1->ctx, map1->n == 1, goto error);
2847 isl_assert(map2->ctx, map1->n == 1, goto error);
2848 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2849 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2851 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2852 return isl_map_intersect(map2, map1);
2854 isl_assert(map2->ctx,
2855 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2857 map1 = isl_map_cow(map1);
2858 if (!map1)
2859 goto error;
2860 if (isl_map_plain_is_empty(map1)) {
2861 isl_map_free(map2);
2862 return map1;
2864 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2865 if (map2->p[0]->n_eq == 1)
2866 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2867 else
2868 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2869 map2->p[0]->ineq[0]);
2871 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2872 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2873 if (!map1->p[0])
2874 goto error;
2876 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2877 isl_basic_map_free(map1->p[0]);
2878 map1->n = 0;
2881 isl_map_free(map2);
2883 return map1;
2884 error:
2885 isl_map_free(map1);
2886 isl_map_free(map2);
2887 return NULL;
2890 /* map2 may be either a parameter domain or a map living in the same
2891 * space as map1.
2893 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2894 __isl_take isl_map *map2)
2896 unsigned flags = 0;
2897 isl_map *result;
2898 int i, j;
2900 if (!map1 || !map2)
2901 goto error;
2903 if ((isl_map_plain_is_empty(map1) ||
2904 isl_map_plain_is_universe(map2)) &&
2905 isl_space_is_equal(map1->dim, map2->dim)) {
2906 isl_map_free(map2);
2907 return map1;
2909 if ((isl_map_plain_is_empty(map2) ||
2910 isl_map_plain_is_universe(map1)) &&
2911 isl_space_is_equal(map1->dim, map2->dim)) {
2912 isl_map_free(map1);
2913 return map2;
2916 if (map1->n == 1 && map2->n == 1 &&
2917 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2918 isl_space_is_equal(map1->dim, map2->dim) &&
2919 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2920 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2921 return map_intersect_add_constraint(map1, map2);
2923 if (isl_space_dim(map2->dim, isl_dim_all) !=
2924 isl_space_dim(map2->dim, isl_dim_param))
2925 isl_assert(map1->ctx,
2926 isl_space_is_equal(map1->dim, map2->dim), goto error);
2928 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2929 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2930 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2932 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2933 map1->n * map2->n, flags);
2934 if (!result)
2935 goto error;
2936 for (i = 0; i < map1->n; ++i)
2937 for (j = 0; j < map2->n; ++j) {
2938 struct isl_basic_map *part;
2939 part = isl_basic_map_intersect(
2940 isl_basic_map_copy(map1->p[i]),
2941 isl_basic_map_copy(map2->p[j]));
2942 if (isl_basic_map_is_empty(part) < 0)
2943 part = isl_basic_map_free(part);
2944 result = isl_map_add_basic_map(result, part);
2945 if (!result)
2946 goto error;
2948 isl_map_free(map1);
2949 isl_map_free(map2);
2950 return result;
2951 error:
2952 isl_map_free(map1);
2953 isl_map_free(map2);
2954 return NULL;
2957 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2958 __isl_take isl_map *map2)
2960 if (!map1 || !map2)
2961 goto error;
2962 if (!isl_space_is_equal(map1->dim, map2->dim))
2963 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2964 "spaces don't match", goto error);
2965 return map_intersect_internal(map1, map2);
2966 error:
2967 isl_map_free(map1);
2968 isl_map_free(map2);
2969 return NULL;
2972 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2973 __isl_take isl_map *map2)
2975 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2978 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2980 return (struct isl_set *)
2981 isl_map_intersect((struct isl_map *)set1,
2982 (struct isl_map *)set2);
2985 /* map_intersect_internal accepts intersections
2986 * with parameter domains, so we can just call that function.
2988 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2989 __isl_take isl_set *params)
2991 return map_intersect_internal(map, params);
2994 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2995 __isl_take isl_map *map2)
2997 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3000 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3001 __isl_take isl_set *params)
3003 return isl_map_intersect_params(set, params);
3006 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3008 isl_space *dim;
3009 struct isl_basic_set *bset;
3010 unsigned in;
3012 if (!bmap)
3013 return NULL;
3014 bmap = isl_basic_map_cow(bmap);
3015 if (!bmap)
3016 return NULL;
3017 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3018 in = isl_basic_map_n_in(bmap);
3019 bset = isl_basic_set_from_basic_map(bmap);
3020 bset = isl_basic_set_swap_vars(bset, in);
3021 return isl_basic_map_from_basic_set(bset, dim);
3024 static __isl_give isl_basic_map *basic_map_space_reset(
3025 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3027 isl_space *space;
3029 if (!bmap)
3030 return NULL;
3031 if (!isl_space_is_named_or_nested(bmap->dim, type))
3032 return bmap;
3034 space = isl_basic_map_get_space(bmap);
3035 space = isl_space_reset(space, type);
3036 bmap = isl_basic_map_reset_space(bmap, space);
3037 return bmap;
3040 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3041 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3042 unsigned pos, unsigned n)
3044 isl_space *res_dim;
3045 struct isl_basic_map *res;
3046 struct isl_dim_map *dim_map;
3047 unsigned total, off;
3048 enum isl_dim_type t;
3050 if (n == 0)
3051 return basic_map_space_reset(bmap, type);
3053 if (!bmap)
3054 return NULL;
3056 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3058 total = isl_basic_map_total_dim(bmap) + n;
3059 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3060 off = 0;
3061 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3062 if (t != type) {
3063 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3064 } else {
3065 unsigned size = isl_basic_map_dim(bmap, t);
3066 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3067 0, pos, off);
3068 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3069 pos, size - pos, off + pos + n);
3071 off += isl_space_dim(res_dim, t);
3073 isl_dim_map_div(dim_map, bmap, off);
3075 res = isl_basic_map_alloc_space(res_dim,
3076 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3077 if (isl_basic_map_is_rational(bmap))
3078 res = isl_basic_map_set_rational(res);
3079 if (isl_basic_map_plain_is_empty(bmap)) {
3080 isl_basic_map_free(bmap);
3081 free(dim_map);
3082 return isl_basic_map_set_to_empty(res);
3084 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3085 return isl_basic_map_finalize(res);
3088 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3089 __isl_take isl_basic_set *bset,
3090 enum isl_dim_type type, unsigned pos, unsigned n)
3092 return isl_basic_map_insert_dims(bset, type, pos, n);
3095 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3096 enum isl_dim_type type, unsigned n)
3098 if (!bmap)
3099 return NULL;
3100 return isl_basic_map_insert_dims(bmap, type,
3101 isl_basic_map_dim(bmap, type), n);
3104 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3105 enum isl_dim_type type, unsigned n)
3107 if (!bset)
3108 return NULL;
3109 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3110 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3111 error:
3112 isl_basic_set_free(bset);
3113 return NULL;
3116 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3117 enum isl_dim_type type)
3119 isl_space *space;
3121 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3122 return map;
3124 space = isl_map_get_space(map);
3125 space = isl_space_reset(space, type);
3126 map = isl_map_reset_space(map, space);
3127 return map;
3130 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3131 enum isl_dim_type type, unsigned pos, unsigned n)
3133 int i;
3135 if (n == 0)
3136 return map_space_reset(map, type);
3138 map = isl_map_cow(map);
3139 if (!map)
3140 return NULL;
3142 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3143 if (!map->dim)
3144 goto error;
3146 for (i = 0; i < map->n; ++i) {
3147 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3148 if (!map->p[i])
3149 goto error;
3152 return map;
3153 error:
3154 isl_map_free(map);
3155 return NULL;
3158 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3159 enum isl_dim_type type, unsigned pos, unsigned n)
3161 return isl_map_insert_dims(set, type, pos, n);
3164 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3165 enum isl_dim_type type, unsigned n)
3167 if (!map)
3168 return NULL;
3169 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3172 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3173 enum isl_dim_type type, unsigned n)
3175 if (!set)
3176 return NULL;
3177 isl_assert(set->ctx, type != isl_dim_in, goto error);
3178 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3179 error:
3180 isl_set_free(set);
3181 return NULL;
3184 __isl_give isl_basic_map *isl_basic_map_move_dims(
3185 __isl_take isl_basic_map *bmap,
3186 enum isl_dim_type dst_type, unsigned dst_pos,
3187 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3189 struct isl_dim_map *dim_map;
3190 struct isl_basic_map *res;
3191 enum isl_dim_type t;
3192 unsigned total, off;
3194 if (!bmap)
3195 return NULL;
3196 if (n == 0)
3197 return bmap;
3199 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3200 goto error);
3202 if (dst_type == src_type && dst_pos == src_pos)
3203 return bmap;
3205 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3207 if (pos(bmap->dim, dst_type) + dst_pos ==
3208 pos(bmap->dim, src_type) + src_pos +
3209 ((src_type < dst_type) ? n : 0)) {
3210 bmap = isl_basic_map_cow(bmap);
3211 if (!bmap)
3212 return NULL;
3214 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3215 src_type, src_pos, n);
3216 if (!bmap->dim)
3217 goto error;
3219 bmap = isl_basic_map_finalize(bmap);
3221 return bmap;
3224 total = isl_basic_map_total_dim(bmap);
3225 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3227 off = 0;
3228 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3229 unsigned size = isl_space_dim(bmap->dim, t);
3230 if (t == dst_type) {
3231 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3232 0, dst_pos, off);
3233 off += dst_pos;
3234 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3235 src_pos, n, off);
3236 off += n;
3237 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3238 dst_pos, size - dst_pos, off);
3239 off += size - dst_pos;
3240 } else if (t == src_type) {
3241 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3242 0, src_pos, off);
3243 off += src_pos;
3244 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3245 src_pos + n, size - src_pos - n, off);
3246 off += size - src_pos - n;
3247 } else {
3248 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3249 off += size;
3252 isl_dim_map_div(dim_map, bmap, off);
3254 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3255 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3256 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3257 if (!bmap)
3258 goto error;
3260 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3261 src_type, src_pos, n);
3262 if (!bmap->dim)
3263 goto error;
3265 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3266 bmap = isl_basic_map_gauss(bmap, NULL);
3267 bmap = isl_basic_map_finalize(bmap);
3269 return bmap;
3270 error:
3271 isl_basic_map_free(bmap);
3272 return NULL;
3275 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3276 enum isl_dim_type dst_type, unsigned dst_pos,
3277 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3279 return (isl_basic_set *)isl_basic_map_move_dims(
3280 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3283 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3284 enum isl_dim_type dst_type, unsigned dst_pos,
3285 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3287 if (!set)
3288 return NULL;
3289 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3290 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3291 src_type, src_pos, n);
3292 error:
3293 isl_set_free(set);
3294 return NULL;
3297 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3298 enum isl_dim_type dst_type, unsigned dst_pos,
3299 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3301 int i;
3303 if (!map)
3304 return NULL;
3305 if (n == 0)
3306 return map;
3308 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3309 goto error);
3311 if (dst_type == src_type && dst_pos == src_pos)
3312 return map;
3314 isl_assert(map->ctx, dst_type != src_type, goto error);
3316 map = isl_map_cow(map);
3317 if (!map)
3318 return NULL;
3320 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3321 if (!map->dim)
3322 goto error;
3324 for (i = 0; i < map->n; ++i) {
3325 map->p[i] = isl_basic_map_move_dims(map->p[i],
3326 dst_type, dst_pos,
3327 src_type, src_pos, n);
3328 if (!map->p[i])
3329 goto error;
3332 return map;
3333 error:
3334 isl_map_free(map);
3335 return NULL;
3338 /* Move the specified dimensions to the last columns right before
3339 * the divs. Don't change the dimension specification of bmap.
3340 * That's the responsibility of the caller.
3342 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3343 enum isl_dim_type type, unsigned first, unsigned n)
3345 struct isl_dim_map *dim_map;
3346 struct isl_basic_map *res;
3347 enum isl_dim_type t;
3348 unsigned total, off;
3350 if (!bmap)
3351 return NULL;
3352 if (pos(bmap->dim, type) + first + n ==
3353 1 + isl_space_dim(bmap->dim, isl_dim_all))
3354 return bmap;
3356 total = isl_basic_map_total_dim(bmap);
3357 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3359 off = 0;
3360 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3361 unsigned size = isl_space_dim(bmap->dim, t);
3362 if (t == type) {
3363 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3364 0, first, off);
3365 off += first;
3366 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3367 first, n, total - bmap->n_div - n);
3368 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3369 first + n, size - (first + n), off);
3370 off += size - (first + n);
3371 } else {
3372 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3373 off += size;
3376 isl_dim_map_div(dim_map, bmap, off + n);
3378 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3379 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3380 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3381 return res;
3384 /* Insert "n" rows in the divs of "bmap".
3386 * The number of columns is not changed, which means that the last
3387 * dimensions of "bmap" are being reintepreted as the new divs.
3388 * The space of "bmap" is not adjusted, however, which means
3389 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3390 * from the space of "bmap" is the responsibility of the caller.
3392 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3393 int n)
3395 int i;
3396 size_t row_size;
3397 isl_int **new_div;
3398 isl_int *old;
3400 bmap = isl_basic_map_cow(bmap);
3401 if (!bmap)
3402 return NULL;
3404 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3405 old = bmap->block2.data;
3406 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3407 (bmap->extra + n) * (1 + row_size));
3408 if (!bmap->block2.data)
3409 return isl_basic_map_free(bmap);
3410 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3411 if (!new_div)
3412 return isl_basic_map_free(bmap);
3413 for (i = 0; i < n; ++i) {
3414 new_div[i] = bmap->block2.data +
3415 (bmap->extra + i) * (1 + row_size);
3416 isl_seq_clr(new_div[i], 1 + row_size);
3418 for (i = 0; i < bmap->extra; ++i)
3419 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3420 free(bmap->div);
3421 bmap->div = new_div;
3422 bmap->n_div += n;
3423 bmap->extra += n;
3425 return bmap;
3428 /* Turn the n dimensions of type type, starting at first
3429 * into existentially quantified variables.
3431 __isl_give isl_basic_map *isl_basic_map_project_out(
3432 __isl_take isl_basic_map *bmap,
3433 enum isl_dim_type type, unsigned first, unsigned n)
3435 if (n == 0)
3436 return basic_map_space_reset(bmap, type);
3438 if (!bmap)
3439 return NULL;
3441 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3442 return isl_basic_map_remove_dims(bmap, type, first, n);
3444 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3445 goto error);
3447 bmap = move_last(bmap, type, first, n);
3448 bmap = isl_basic_map_cow(bmap);
3449 bmap = insert_div_rows(bmap, n);
3450 if (!bmap)
3451 return NULL;
3453 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3454 if (!bmap->dim)
3455 goto error;
3456 bmap = isl_basic_map_simplify(bmap);
3457 bmap = isl_basic_map_drop_redundant_divs(bmap);
3458 return isl_basic_map_finalize(bmap);
3459 error:
3460 isl_basic_map_free(bmap);
3461 return NULL;
3464 /* Turn the n dimensions of type type, starting at first
3465 * into existentially quantified variables.
3467 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3468 enum isl_dim_type type, unsigned first, unsigned n)
3470 return (isl_basic_set *)isl_basic_map_project_out(
3471 (isl_basic_map *)bset, type, first, n);
3474 /* Turn the n dimensions of type type, starting at first
3475 * into existentially quantified variables.
3477 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3478 enum isl_dim_type type, unsigned first, unsigned n)
3480 int i;
3482 if (!map)
3483 return NULL;
3485 if (n == 0)
3486 return map_space_reset(map, type);
3488 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3490 map = isl_map_cow(map);
3491 if (!map)
3492 return NULL;
3494 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3495 if (!map->dim)
3496 goto error;
3498 for (i = 0; i < map->n; ++i) {
3499 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3500 if (!map->p[i])
3501 goto error;
3504 return map;
3505 error:
3506 isl_map_free(map);
3507 return NULL;
3510 /* Turn the n dimensions of type type, starting at first
3511 * into existentially quantified variables.
3513 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3514 enum isl_dim_type type, unsigned first, unsigned n)
3516 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3519 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3521 int i, j;
3523 for (i = 0; i < n; ++i) {
3524 j = isl_basic_map_alloc_div(bmap);
3525 if (j < 0)
3526 goto error;
3527 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3529 return bmap;
3530 error:
3531 isl_basic_map_free(bmap);
3532 return NULL;
3535 struct isl_basic_map *isl_basic_map_apply_range(
3536 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3538 isl_space *dim_result = NULL;
3539 struct isl_basic_map *bmap;
3540 unsigned n_in, n_out, n, nparam, total, pos;
3541 struct isl_dim_map *dim_map1, *dim_map2;
3543 if (!bmap1 || !bmap2)
3544 goto error;
3546 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3547 isl_space_copy(bmap2->dim));
3549 n_in = isl_basic_map_n_in(bmap1);
3550 n_out = isl_basic_map_n_out(bmap2);
3551 n = isl_basic_map_n_out(bmap1);
3552 nparam = isl_basic_map_n_param(bmap1);
3554 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3555 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3556 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3557 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3558 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3559 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3560 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3561 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3562 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3563 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3564 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3566 bmap = isl_basic_map_alloc_space(dim_result,
3567 bmap1->n_div + bmap2->n_div + n,
3568 bmap1->n_eq + bmap2->n_eq,
3569 bmap1->n_ineq + bmap2->n_ineq);
3570 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3571 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3572 bmap = add_divs(bmap, n);
3573 bmap = isl_basic_map_simplify(bmap);
3574 bmap = isl_basic_map_drop_redundant_divs(bmap);
3575 return isl_basic_map_finalize(bmap);
3576 error:
3577 isl_basic_map_free(bmap1);
3578 isl_basic_map_free(bmap2);
3579 return NULL;
3582 struct isl_basic_set *isl_basic_set_apply(
3583 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3585 if (!bset || !bmap)
3586 goto error;
3588 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3589 goto error);
3591 return (struct isl_basic_set *)
3592 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3593 error:
3594 isl_basic_set_free(bset);
3595 isl_basic_map_free(bmap);
3596 return NULL;
3599 struct isl_basic_map *isl_basic_map_apply_domain(
3600 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3602 if (!bmap1 || !bmap2)
3603 goto error;
3605 isl_assert(bmap1->ctx,
3606 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3607 isl_assert(bmap1->ctx,
3608 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3609 goto error);
3611 bmap1 = isl_basic_map_reverse(bmap1);
3612 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3613 return isl_basic_map_reverse(bmap1);
3614 error:
3615 isl_basic_map_free(bmap1);
3616 isl_basic_map_free(bmap2);
3617 return NULL;
3620 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3621 * A \cap B -> f(A) + f(B)
3623 struct isl_basic_map *isl_basic_map_sum(
3624 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3626 unsigned n_in, n_out, nparam, total, pos;
3627 struct isl_basic_map *bmap = NULL;
3628 struct isl_dim_map *dim_map1, *dim_map2;
3629 int i;
3631 if (!bmap1 || !bmap2)
3632 goto error;
3634 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3635 goto error);
3637 nparam = isl_basic_map_n_param(bmap1);
3638 n_in = isl_basic_map_n_in(bmap1);
3639 n_out = isl_basic_map_n_out(bmap1);
3641 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3642 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3643 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3644 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3645 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3646 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3647 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3648 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3649 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3650 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3651 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3653 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3654 bmap1->n_div + bmap2->n_div + 2 * n_out,
3655 bmap1->n_eq + bmap2->n_eq + n_out,
3656 bmap1->n_ineq + bmap2->n_ineq);
3657 for (i = 0; i < n_out; ++i) {
3658 int j = isl_basic_map_alloc_equality(bmap);
3659 if (j < 0)
3660 goto error;
3661 isl_seq_clr(bmap->eq[j], 1+total);
3662 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3663 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3664 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3666 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3667 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3668 bmap = add_divs(bmap, 2 * n_out);
3670 bmap = isl_basic_map_simplify(bmap);
3671 return isl_basic_map_finalize(bmap);
3672 error:
3673 isl_basic_map_free(bmap);
3674 isl_basic_map_free(bmap1);
3675 isl_basic_map_free(bmap2);
3676 return NULL;
3679 /* Given two maps A -> f(A) and B -> g(B), construct a map
3680 * A \cap B -> f(A) + f(B)
3682 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3684 struct isl_map *result;
3685 int i, j;
3687 if (!map1 || !map2)
3688 goto error;
3690 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3692 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3693 map1->n * map2->n, 0);
3694 if (!result)
3695 goto error;
3696 for (i = 0; i < map1->n; ++i)
3697 for (j = 0; j < map2->n; ++j) {
3698 struct isl_basic_map *part;
3699 part = isl_basic_map_sum(
3700 isl_basic_map_copy(map1->p[i]),
3701 isl_basic_map_copy(map2->p[j]));
3702 if (isl_basic_map_is_empty(part))
3703 isl_basic_map_free(part);
3704 else
3705 result = isl_map_add_basic_map(result, part);
3706 if (!result)
3707 goto error;
3709 isl_map_free(map1);
3710 isl_map_free(map2);
3711 return result;
3712 error:
3713 isl_map_free(map1);
3714 isl_map_free(map2);
3715 return NULL;
3718 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3719 __isl_take isl_set *set2)
3721 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3724 /* Given a basic map A -> f(A), construct A -> -f(A).
3726 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3728 int i, j;
3729 unsigned off, n;
3731 bmap = isl_basic_map_cow(bmap);
3732 if (!bmap)
3733 return NULL;
3735 n = isl_basic_map_dim(bmap, isl_dim_out);
3736 off = isl_basic_map_offset(bmap, isl_dim_out);
3737 for (i = 0; i < bmap->n_eq; ++i)
3738 for (j = 0; j < n; ++j)
3739 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3740 for (i = 0; i < bmap->n_ineq; ++i)
3741 for (j = 0; j < n; ++j)
3742 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3743 for (i = 0; i < bmap->n_div; ++i)
3744 for (j = 0; j < n; ++j)
3745 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3746 bmap = isl_basic_map_gauss(bmap, NULL);
3747 return isl_basic_map_finalize(bmap);
3750 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3752 return isl_basic_map_neg(bset);
3755 /* Given a map A -> f(A), construct A -> -f(A).
3757 struct isl_map *isl_map_neg(struct isl_map *map)
3759 int i;
3761 map = isl_map_cow(map);
3762 if (!map)
3763 return NULL;
3765 for (i = 0; i < map->n; ++i) {
3766 map->p[i] = isl_basic_map_neg(map->p[i]);
3767 if (!map->p[i])
3768 goto error;
3771 return map;
3772 error:
3773 isl_map_free(map);
3774 return NULL;
3777 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3779 return (isl_set *)isl_map_neg((isl_map *)set);
3782 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3783 * A -> floor(f(A)/d).
3785 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3786 isl_int d)
3788 unsigned n_in, n_out, nparam, total, pos;
3789 struct isl_basic_map *result = NULL;
3790 struct isl_dim_map *dim_map;
3791 int i;
3793 if (!bmap)
3794 return NULL;
3796 nparam = isl_basic_map_n_param(bmap);
3797 n_in = isl_basic_map_n_in(bmap);
3798 n_out = isl_basic_map_n_out(bmap);
3800 total = nparam + n_in + n_out + bmap->n_div + n_out;
3801 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3802 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3803 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3804 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3805 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3807 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3808 bmap->n_div + n_out,
3809 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3810 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3811 result = add_divs(result, n_out);
3812 for (i = 0; i < n_out; ++i) {
3813 int j;
3814 j = isl_basic_map_alloc_inequality(result);
3815 if (j < 0)
3816 goto error;
3817 isl_seq_clr(result->ineq[j], 1+total);
3818 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3819 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3820 j = isl_basic_map_alloc_inequality(result);
3821 if (j < 0)
3822 goto error;
3823 isl_seq_clr(result->ineq[j], 1+total);
3824 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3825 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3826 isl_int_sub_ui(result->ineq[j][0], d, 1);
3829 result = isl_basic_map_simplify(result);
3830 return isl_basic_map_finalize(result);
3831 error:
3832 isl_basic_map_free(result);
3833 return NULL;
3836 /* Given a map A -> f(A) and an integer d, construct a map
3837 * A -> floor(f(A)/d).
3839 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3841 int i;
3843 map = isl_map_cow(map);
3844 if (!map)
3845 return NULL;
3847 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3848 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3849 for (i = 0; i < map->n; ++i) {
3850 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3851 if (!map->p[i])
3852 goto error;
3855 return map;
3856 error:
3857 isl_map_free(map);
3858 return NULL;
3861 /* Given a map A -> f(A) and an integer d, construct a map
3862 * A -> floor(f(A)/d).
3864 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3865 __isl_take isl_val *d)
3867 if (!map || !d)
3868 goto error;
3869 if (!isl_val_is_int(d))
3870 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3871 "expecting integer denominator", goto error);
3872 map = isl_map_floordiv(map, d->n);
3873 isl_val_free(d);
3874 return map;
3875 error:
3876 isl_map_free(map);
3877 isl_val_free(d);
3878 return NULL;
3881 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3883 int i;
3884 unsigned nparam;
3885 unsigned n_in;
3887 i = isl_basic_map_alloc_equality(bmap);
3888 if (i < 0)
3889 goto error;
3890 nparam = isl_basic_map_n_param(bmap);
3891 n_in = isl_basic_map_n_in(bmap);
3892 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3893 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3894 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3895 return isl_basic_map_finalize(bmap);
3896 error:
3897 isl_basic_map_free(bmap);
3898 return NULL;
3901 /* Add a constraints to "bmap" expressing i_pos < o_pos
3903 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3905 int i;
3906 unsigned nparam;
3907 unsigned n_in;
3909 i = isl_basic_map_alloc_inequality(bmap);
3910 if (i < 0)
3911 goto error;
3912 nparam = isl_basic_map_n_param(bmap);
3913 n_in = isl_basic_map_n_in(bmap);
3914 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3915 isl_int_set_si(bmap->ineq[i][0], -1);
3916 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3917 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3918 return isl_basic_map_finalize(bmap);
3919 error:
3920 isl_basic_map_free(bmap);
3921 return NULL;
3924 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3926 static __isl_give isl_basic_map *var_less_or_equal(
3927 __isl_take isl_basic_map *bmap, unsigned pos)
3929 int i;
3930 unsigned nparam;
3931 unsigned n_in;
3933 i = isl_basic_map_alloc_inequality(bmap);
3934 if (i < 0)
3935 goto error;
3936 nparam = isl_basic_map_n_param(bmap);
3937 n_in = isl_basic_map_n_in(bmap);
3938 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3939 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3940 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3941 return isl_basic_map_finalize(bmap);
3942 error:
3943 isl_basic_map_free(bmap);
3944 return NULL;
3947 /* Add a constraints to "bmap" expressing i_pos > o_pos
3949 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3951 int i;
3952 unsigned nparam;
3953 unsigned n_in;
3955 i = isl_basic_map_alloc_inequality(bmap);
3956 if (i < 0)
3957 goto error;
3958 nparam = isl_basic_map_n_param(bmap);
3959 n_in = isl_basic_map_n_in(bmap);
3960 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3961 isl_int_set_si(bmap->ineq[i][0], -1);
3962 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3963 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3964 return isl_basic_map_finalize(bmap);
3965 error:
3966 isl_basic_map_free(bmap);
3967 return NULL;
3970 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3972 static __isl_give isl_basic_map *var_more_or_equal(
3973 __isl_take isl_basic_map *bmap, unsigned pos)
3975 int i;
3976 unsigned nparam;
3977 unsigned n_in;
3979 i = isl_basic_map_alloc_inequality(bmap);
3980 if (i < 0)
3981 goto error;
3982 nparam = isl_basic_map_n_param(bmap);
3983 n_in = isl_basic_map_n_in(bmap);
3984 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3985 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3986 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3987 return isl_basic_map_finalize(bmap);
3988 error:
3989 isl_basic_map_free(bmap);
3990 return NULL;
3993 __isl_give isl_basic_map *isl_basic_map_equal(
3994 __isl_take isl_space *dim, unsigned n_equal)
3996 int i;
3997 struct isl_basic_map *bmap;
3998 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3999 if (!bmap)
4000 return NULL;
4001 for (i = 0; i < n_equal && bmap; ++i)
4002 bmap = var_equal(bmap, i);
4003 return isl_basic_map_finalize(bmap);
4006 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4008 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4009 unsigned pos)
4011 int i;
4012 struct isl_basic_map *bmap;
4013 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4014 if (!bmap)
4015 return NULL;
4016 for (i = 0; i < pos && bmap; ++i)
4017 bmap = var_equal(bmap, i);
4018 if (bmap)
4019 bmap = var_less(bmap, pos);
4020 return isl_basic_map_finalize(bmap);
4023 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4025 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4026 __isl_take isl_space *dim, unsigned pos)
4028 int i;
4029 isl_basic_map *bmap;
4031 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4032 for (i = 0; i < pos; ++i)
4033 bmap = var_equal(bmap, i);
4034 bmap = var_less_or_equal(bmap, pos);
4035 return isl_basic_map_finalize(bmap);
4038 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4040 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4041 unsigned pos)
4043 int i;
4044 struct isl_basic_map *bmap;
4045 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4046 if (!bmap)
4047 return NULL;
4048 for (i = 0; i < pos && bmap; ++i)
4049 bmap = var_equal(bmap, i);
4050 if (bmap)
4051 bmap = var_more(bmap, pos);
4052 return isl_basic_map_finalize(bmap);
4055 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4057 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4058 __isl_take isl_space *dim, unsigned pos)
4060 int i;
4061 isl_basic_map *bmap;
4063 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4064 for (i = 0; i < pos; ++i)
4065 bmap = var_equal(bmap, i);
4066 bmap = var_more_or_equal(bmap, pos);
4067 return isl_basic_map_finalize(bmap);
4070 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4071 unsigned n, int equal)
4073 struct isl_map *map;
4074 int i;
4076 if (n == 0 && equal)
4077 return isl_map_universe(dims);
4079 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4081 for (i = 0; i + 1 < n; ++i)
4082 map = isl_map_add_basic_map(map,
4083 isl_basic_map_less_at(isl_space_copy(dims), i));
4084 if (n > 0) {
4085 if (equal)
4086 map = isl_map_add_basic_map(map,
4087 isl_basic_map_less_or_equal_at(dims, n - 1));
4088 else
4089 map = isl_map_add_basic_map(map,
4090 isl_basic_map_less_at(dims, n - 1));
4091 } else
4092 isl_space_free(dims);
4094 return map;
4097 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4099 if (!dims)
4100 return NULL;
4101 return map_lex_lte_first(dims, dims->n_out, equal);
4104 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4106 return map_lex_lte_first(dim, n, 0);
4109 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4111 return map_lex_lte_first(dim, n, 1);
4114 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4116 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4119 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4121 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4124 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4125 unsigned n, int equal)
4127 struct isl_map *map;
4128 int i;
4130 if (n == 0 && equal)
4131 return isl_map_universe(dims);
4133 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4135 for (i = 0; i + 1 < n; ++i)
4136 map = isl_map_add_basic_map(map,
4137 isl_basic_map_more_at(isl_space_copy(dims), i));
4138 if (n > 0) {
4139 if (equal)
4140 map = isl_map_add_basic_map(map,
4141 isl_basic_map_more_or_equal_at(dims, n - 1));
4142 else
4143 map = isl_map_add_basic_map(map,
4144 isl_basic_map_more_at(dims, n - 1));
4145 } else
4146 isl_space_free(dims);
4148 return map;
4151 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4153 if (!dims)
4154 return NULL;
4155 return map_lex_gte_first(dims, dims->n_out, equal);
4158 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4160 return map_lex_gte_first(dim, n, 0);
4163 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4165 return map_lex_gte_first(dim, n, 1);
4168 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4170 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4173 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4175 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4178 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4179 __isl_take isl_set *set2)
4181 isl_map *map;
4182 map = isl_map_lex_le(isl_set_get_space(set1));
4183 map = isl_map_intersect_domain(map, set1);
4184 map = isl_map_intersect_range(map, set2);
4185 return map;
4188 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4189 __isl_take isl_set *set2)
4191 isl_map *map;
4192 map = isl_map_lex_lt(isl_set_get_space(set1));
4193 map = isl_map_intersect_domain(map, set1);
4194 map = isl_map_intersect_range(map, set2);
4195 return map;
4198 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4199 __isl_take isl_set *set2)
4201 isl_map *map;
4202 map = isl_map_lex_ge(isl_set_get_space(set1));
4203 map = isl_map_intersect_domain(map, set1);
4204 map = isl_map_intersect_range(map, set2);
4205 return map;
4208 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4209 __isl_take isl_set *set2)
4211 isl_map *map;
4212 map = isl_map_lex_gt(isl_set_get_space(set1));
4213 map = isl_map_intersect_domain(map, set1);
4214 map = isl_map_intersect_range(map, set2);
4215 return map;
4218 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4219 __isl_take isl_map *map2)
4221 isl_map *map;
4222 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4223 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4224 map = isl_map_apply_range(map, isl_map_reverse(map2));
4225 return map;
4228 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4229 __isl_take isl_map *map2)
4231 isl_map *map;
4232 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4233 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4234 map = isl_map_apply_range(map, isl_map_reverse(map2));
4235 return map;
4238 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4239 __isl_take isl_map *map2)
4241 isl_map *map;
4242 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4243 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4244 map = isl_map_apply_range(map, isl_map_reverse(map2));
4245 return map;
4248 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4249 __isl_take isl_map *map2)
4251 isl_map *map;
4252 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4253 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4254 map = isl_map_apply_range(map, isl_map_reverse(map2));
4255 return map;
4258 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4259 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4261 struct isl_basic_map *bmap;
4263 bset = isl_basic_set_cow(bset);
4264 if (!bset || !dim)
4265 goto error;
4267 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4268 isl_space_free(bset->dim);
4269 bmap = (struct isl_basic_map *) bset;
4270 bmap->dim = dim;
4271 return isl_basic_map_finalize(bmap);
4272 error:
4273 isl_basic_set_free(bset);
4274 isl_space_free(dim);
4275 return NULL;
4278 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4280 if (!bmap)
4281 goto error;
4282 if (bmap->dim->n_in == 0)
4283 return (struct isl_basic_set *)bmap;
4284 bmap = isl_basic_map_cow(bmap);
4285 if (!bmap)
4286 goto error;
4287 bmap->dim = isl_space_as_set_space(bmap->dim);
4288 if (!bmap->dim)
4289 goto error;
4290 bmap = isl_basic_map_finalize(bmap);
4291 return (struct isl_basic_set *)bmap;
4292 error:
4293 isl_basic_map_free(bmap);
4294 return NULL;
4297 /* For a div d = floor(f/m), add the constraints
4299 * f - m d >= 0
4300 * -(f-(n-1)) + m d >= 0
4302 * Note that the second constraint is the negation of
4304 * f - m d >= n
4306 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4307 unsigned pos, isl_int *div)
4309 int i, j;
4310 unsigned total = isl_basic_map_total_dim(bmap);
4312 i = isl_basic_map_alloc_inequality(bmap);
4313 if (i < 0)
4314 return -1;
4315 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4316 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4318 j = isl_basic_map_alloc_inequality(bmap);
4319 if (j < 0)
4320 return -1;
4321 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4322 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4323 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4324 return j;
4327 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4328 unsigned pos, isl_int *div)
4330 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4331 pos, div);
4334 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4336 unsigned total = isl_basic_map_total_dim(bmap);
4337 unsigned div_pos = total - bmap->n_div + div;
4339 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4340 bmap->div[div]);
4343 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4345 return isl_basic_map_add_div_constraints(bset, div);
4348 struct isl_basic_set *isl_basic_map_underlying_set(
4349 struct isl_basic_map *bmap)
4351 if (!bmap)
4352 goto error;
4353 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4354 bmap->n_div == 0 &&
4355 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4356 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4357 return (struct isl_basic_set *)bmap;
4358 bmap = isl_basic_map_cow(bmap);
4359 if (!bmap)
4360 goto error;
4361 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4362 if (!bmap->dim)
4363 goto error;
4364 bmap->extra -= bmap->n_div;
4365 bmap->n_div = 0;
4366 bmap = isl_basic_map_finalize(bmap);
4367 return (struct isl_basic_set *)bmap;
4368 error:
4369 isl_basic_map_free(bmap);
4370 return NULL;
4373 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4374 __isl_take isl_basic_set *bset)
4376 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4379 struct isl_basic_map *isl_basic_map_overlying_set(
4380 struct isl_basic_set *bset, struct isl_basic_map *like)
4382 struct isl_basic_map *bmap;
4383 struct isl_ctx *ctx;
4384 unsigned total;
4385 int i;
4387 if (!bset || !like)
4388 goto error;
4389 ctx = bset->ctx;
4390 isl_assert(ctx, bset->n_div == 0, goto error);
4391 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4392 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4393 goto error);
4394 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4395 isl_basic_map_free(like);
4396 return (struct isl_basic_map *)bset;
4398 bset = isl_basic_set_cow(bset);
4399 if (!bset)
4400 goto error;
4401 total = bset->dim->n_out + bset->extra;
4402 bmap = (struct isl_basic_map *)bset;
4403 isl_space_free(bmap->dim);
4404 bmap->dim = isl_space_copy(like->dim);
4405 if (!bmap->dim)
4406 goto error;
4407 bmap->n_div = like->n_div;
4408 bmap->extra += like->n_div;
4409 if (bmap->extra) {
4410 unsigned ltotal;
4411 isl_int **div;
4412 ltotal = total - bmap->extra + like->extra;
4413 if (ltotal > total)
4414 ltotal = total;
4415 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4416 bmap->extra * (1 + 1 + total));
4417 if (isl_blk_is_error(bmap->block2))
4418 goto error;
4419 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4420 if (!div)
4421 goto error;
4422 bmap->div = div;
4423 for (i = 0; i < bmap->extra; ++i)
4424 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4425 for (i = 0; i < like->n_div; ++i) {
4426 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4427 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4429 bmap = isl_basic_map_extend_constraints(bmap,
4430 0, 2 * like->n_div);
4431 for (i = 0; i < like->n_div; ++i) {
4432 if (!bmap)
4433 break;
4434 if (isl_int_is_zero(bmap->div[i][0]))
4435 continue;
4436 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4437 bmap = isl_basic_map_free(bmap);
4440 isl_basic_map_free(like);
4441 bmap = isl_basic_map_simplify(bmap);
4442 bmap = isl_basic_map_finalize(bmap);
4443 return bmap;
4444 error:
4445 isl_basic_map_free(like);
4446 isl_basic_set_free(bset);
4447 return NULL;
4450 struct isl_basic_set *isl_basic_set_from_underlying_set(
4451 struct isl_basic_set *bset, struct isl_basic_set *like)
4453 return (struct isl_basic_set *)
4454 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4457 struct isl_set *isl_set_from_underlying_set(
4458 struct isl_set *set, struct isl_basic_set *like)
4460 int i;
4462 if (!set || !like)
4463 goto error;
4464 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4465 goto error);
4466 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4467 isl_basic_set_free(like);
4468 return set;
4470 set = isl_set_cow(set);
4471 if (!set)
4472 goto error;
4473 for (i = 0; i < set->n; ++i) {
4474 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4475 isl_basic_set_copy(like));
4476 if (!set->p[i])
4477 goto error;
4479 isl_space_free(set->dim);
4480 set->dim = isl_space_copy(like->dim);
4481 if (!set->dim)
4482 goto error;
4483 isl_basic_set_free(like);
4484 return set;
4485 error:
4486 isl_basic_set_free(like);
4487 isl_set_free(set);
4488 return NULL;
4491 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4493 int i;
4495 map = isl_map_cow(map);
4496 if (!map)
4497 return NULL;
4498 map->dim = isl_space_cow(map->dim);
4499 if (!map->dim)
4500 goto error;
4502 for (i = 1; i < map->n; ++i)
4503 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4504 goto error);
4505 for (i = 0; i < map->n; ++i) {
4506 map->p[i] = (struct isl_basic_map *)
4507 isl_basic_map_underlying_set(map->p[i]);
4508 if (!map->p[i])
4509 goto error;
4511 if (map->n == 0)
4512 map->dim = isl_space_underlying(map->dim, 0);
4513 else {
4514 isl_space_free(map->dim);
4515 map->dim = isl_space_copy(map->p[0]->dim);
4517 if (!map->dim)
4518 goto error;
4519 return (struct isl_set *)map;
4520 error:
4521 isl_map_free(map);
4522 return NULL;
4525 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4527 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4530 __isl_give isl_basic_map *isl_basic_map_reset_space(
4531 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4533 bmap = isl_basic_map_cow(bmap);
4534 if (!bmap || !dim)
4535 goto error;
4537 isl_space_free(bmap->dim);
4538 bmap->dim = dim;
4540 bmap = isl_basic_map_finalize(bmap);
4542 return bmap;
4543 error:
4544 isl_basic_map_free(bmap);
4545 isl_space_free(dim);
4546 return NULL;
4549 __isl_give isl_basic_set *isl_basic_set_reset_space(
4550 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4552 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4553 dim);
4556 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4557 __isl_take isl_space *dim)
4559 int i;
4561 map = isl_map_cow(map);
4562 if (!map || !dim)
4563 goto error;
4565 for (i = 0; i < map->n; ++i) {
4566 map->p[i] = isl_basic_map_reset_space(map->p[i],
4567 isl_space_copy(dim));
4568 if (!map->p[i])
4569 goto error;
4571 isl_space_free(map->dim);
4572 map->dim = dim;
4574 return map;
4575 error:
4576 isl_map_free(map);
4577 isl_space_free(dim);
4578 return NULL;
4581 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4582 __isl_take isl_space *dim)
4584 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4587 /* Compute the parameter domain of the given basic set.
4589 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4591 isl_space *space;
4592 unsigned n;
4594 if (isl_basic_set_is_params(bset))
4595 return bset;
4597 n = isl_basic_set_dim(bset, isl_dim_set);
4598 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4599 space = isl_basic_set_get_space(bset);
4600 space = isl_space_params(space);
4601 bset = isl_basic_set_reset_space(bset, space);
4602 return bset;
4605 /* Construct a zero-dimensional basic set with the given parameter domain.
4607 __isl_give isl_basic_set *isl_basic_set_from_params(
4608 __isl_take isl_basic_set *bset)
4610 isl_space *space;
4611 space = isl_basic_set_get_space(bset);
4612 space = isl_space_set_from_params(space);
4613 bset = isl_basic_set_reset_space(bset, space);
4614 return bset;
4617 /* Compute the parameter domain of the given set.
4619 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4621 isl_space *space;
4622 unsigned n;
4624 if (isl_set_is_params(set))
4625 return set;
4627 n = isl_set_dim(set, isl_dim_set);
4628 set = isl_set_project_out(set, isl_dim_set, 0, n);
4629 space = isl_set_get_space(set);
4630 space = isl_space_params(space);
4631 set = isl_set_reset_space(set, space);
4632 return set;
4635 /* Construct a zero-dimensional set with the given parameter domain.
4637 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4639 isl_space *space;
4640 space = isl_set_get_space(set);
4641 space = isl_space_set_from_params(space);
4642 set = isl_set_reset_space(set, space);
4643 return set;
4646 /* Compute the parameter domain of the given map.
4648 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4650 isl_space *space;
4651 unsigned n;
4653 n = isl_map_dim(map, isl_dim_in);
4654 map = isl_map_project_out(map, isl_dim_in, 0, n);
4655 n = isl_map_dim(map, isl_dim_out);
4656 map = isl_map_project_out(map, isl_dim_out, 0, n);
4657 space = isl_map_get_space(map);
4658 space = isl_space_params(space);
4659 map = isl_map_reset_space(map, space);
4660 return map;
4663 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4665 isl_space *dim;
4666 struct isl_basic_set *domain;
4667 unsigned n_in;
4668 unsigned n_out;
4670 if (!bmap)
4671 return NULL;
4672 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4674 n_in = isl_basic_map_n_in(bmap);
4675 n_out = isl_basic_map_n_out(bmap);
4676 domain = isl_basic_set_from_basic_map(bmap);
4677 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4679 domain = isl_basic_set_reset_space(domain, dim);
4681 return domain;
4684 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4686 if (!bmap)
4687 return -1;
4688 return isl_space_may_be_set(bmap->dim);
4691 /* Is this basic map actually a set?
4692 * Users should never call this function. Outside of isl,
4693 * the type should indicate whether something is a set or a map.
4695 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4697 if (!bmap)
4698 return -1;
4699 return isl_space_is_set(bmap->dim);
4702 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4704 if (!bmap)
4705 return NULL;
4706 if (isl_basic_map_is_set(bmap))
4707 return bmap;
4708 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4711 __isl_give isl_basic_map *isl_basic_map_domain_map(
4712 __isl_take isl_basic_map *bmap)
4714 int i, k;
4715 isl_space *dim;
4716 isl_basic_map *domain;
4717 int nparam, n_in, n_out;
4718 unsigned total;
4720 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4721 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4724 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4725 domain = isl_basic_map_universe(dim);
4727 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4728 bmap = isl_basic_map_apply_range(bmap, domain);
4729 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4731 total = isl_basic_map_total_dim(bmap);
4733 for (i = 0; i < n_in; ++i) {
4734 k = isl_basic_map_alloc_equality(bmap);
4735 if (k < 0)
4736 goto error;
4737 isl_seq_clr(bmap->eq[k], 1 + total);
4738 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4739 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4742 bmap = isl_basic_map_gauss(bmap, NULL);
4743 return isl_basic_map_finalize(bmap);
4744 error:
4745 isl_basic_map_free(bmap);
4746 return NULL;
4749 __isl_give isl_basic_map *isl_basic_map_range_map(
4750 __isl_take isl_basic_map *bmap)
4752 int i, k;
4753 isl_space *dim;
4754 isl_basic_map *range;
4755 int nparam, n_in, n_out;
4756 unsigned total;
4758 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4759 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4760 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4762 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4763 range = isl_basic_map_universe(dim);
4765 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4766 bmap = isl_basic_map_apply_range(bmap, range);
4767 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4769 total = isl_basic_map_total_dim(bmap);
4771 for (i = 0; i < n_out; ++i) {
4772 k = isl_basic_map_alloc_equality(bmap);
4773 if (k < 0)
4774 goto error;
4775 isl_seq_clr(bmap->eq[k], 1 + total);
4776 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4777 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4780 bmap = isl_basic_map_gauss(bmap, NULL);
4781 return isl_basic_map_finalize(bmap);
4782 error:
4783 isl_basic_map_free(bmap);
4784 return NULL;
4787 int isl_map_may_be_set(__isl_keep isl_map *map)
4789 if (!map)
4790 return -1;
4791 return isl_space_may_be_set(map->dim);
4794 /* Is this map actually a set?
4795 * Users should never call this function. Outside of isl,
4796 * the type should indicate whether something is a set or a map.
4798 int isl_map_is_set(__isl_keep isl_map *map)
4800 if (!map)
4801 return -1;
4802 return isl_space_is_set(map->dim);
4805 struct isl_set *isl_map_range(struct isl_map *map)
4807 int i;
4808 struct isl_set *set;
4810 if (!map)
4811 goto error;
4812 if (isl_map_is_set(map))
4813 return (isl_set *)map;
4815 map = isl_map_cow(map);
4816 if (!map)
4817 goto error;
4819 set = (struct isl_set *) map;
4820 set->dim = isl_space_range(set->dim);
4821 if (!set->dim)
4822 goto error;
4823 for (i = 0; i < map->n; ++i) {
4824 set->p[i] = isl_basic_map_range(map->p[i]);
4825 if (!set->p[i])
4826 goto error;
4828 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4829 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4830 return set;
4831 error:
4832 isl_map_free(map);
4833 return NULL;
4836 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4838 int i;
4839 isl_space *domain_dim;
4841 map = isl_map_cow(map);
4842 if (!map)
4843 return NULL;
4845 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4846 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4847 map->dim = isl_space_join(map->dim, domain_dim);
4848 if (!map->dim)
4849 goto error;
4850 for (i = 0; i < map->n; ++i) {
4851 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4852 if (!map->p[i])
4853 goto error;
4855 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4856 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4857 return map;
4858 error:
4859 isl_map_free(map);
4860 return NULL;
4863 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4865 int i;
4866 isl_space *range_dim;
4868 map = isl_map_cow(map);
4869 if (!map)
4870 return NULL;
4872 range_dim = isl_space_range(isl_map_get_space(map));
4873 range_dim = isl_space_from_range(range_dim);
4874 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4875 map->dim = isl_space_join(map->dim, range_dim);
4876 if (!map->dim)
4877 goto error;
4878 for (i = 0; i < map->n; ++i) {
4879 map->p[i] = isl_basic_map_range_map(map->p[i]);
4880 if (!map->p[i])
4881 goto error;
4883 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4884 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4885 return map;
4886 error:
4887 isl_map_free(map);
4888 return NULL;
4891 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4892 __isl_take isl_space *dim)
4894 int i;
4895 struct isl_map *map = NULL;
4897 set = isl_set_cow(set);
4898 if (!set || !dim)
4899 goto error;
4900 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4901 map = (struct isl_map *)set;
4902 for (i = 0; i < set->n; ++i) {
4903 map->p[i] = isl_basic_map_from_basic_set(
4904 set->p[i], isl_space_copy(dim));
4905 if (!map->p[i])
4906 goto error;
4908 isl_space_free(map->dim);
4909 map->dim = dim;
4910 return map;
4911 error:
4912 isl_space_free(dim);
4913 isl_set_free(set);
4914 return NULL;
4917 __isl_give isl_basic_map *isl_basic_map_from_domain(
4918 __isl_take isl_basic_set *bset)
4920 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4923 __isl_give isl_basic_map *isl_basic_map_from_range(
4924 __isl_take isl_basic_set *bset)
4926 isl_space *space;
4927 space = isl_basic_set_get_space(bset);
4928 space = isl_space_from_range(space);
4929 bset = isl_basic_set_reset_space(bset, space);
4930 return (isl_basic_map *)bset;
4933 struct isl_map *isl_map_from_range(struct isl_set *set)
4935 isl_space *space;
4936 space = isl_set_get_space(set);
4937 space = isl_space_from_range(space);
4938 set = isl_set_reset_space(set, space);
4939 return (struct isl_map *)set;
4942 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4944 return isl_map_reverse(isl_map_from_range(set));
4947 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4948 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4950 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4953 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4954 __isl_take isl_set *range)
4956 return isl_map_apply_range(isl_map_reverse(domain), range);
4959 struct isl_set *isl_set_from_map(struct isl_map *map)
4961 int i;
4962 struct isl_set *set = NULL;
4964 if (!map)
4965 return NULL;
4966 map = isl_map_cow(map);
4967 if (!map)
4968 return NULL;
4969 map->dim = isl_space_as_set_space(map->dim);
4970 if (!map->dim)
4971 goto error;
4972 set = (struct isl_set *)map;
4973 for (i = 0; i < map->n; ++i) {
4974 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4975 if (!set->p[i])
4976 goto error;
4978 return set;
4979 error:
4980 isl_map_free(map);
4981 return NULL;
4984 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4985 unsigned flags)
4987 struct isl_map *map;
4989 if (!dim)
4990 return NULL;
4991 if (n < 0)
4992 isl_die(dim->ctx, isl_error_internal,
4993 "negative number of basic maps", goto error);
4994 map = isl_alloc(dim->ctx, struct isl_map,
4995 sizeof(struct isl_map) +
4996 (n - 1) * sizeof(struct isl_basic_map *));
4997 if (!map)
4998 goto error;
5000 map->ctx = dim->ctx;
5001 isl_ctx_ref(map->ctx);
5002 map->ref = 1;
5003 map->size = n;
5004 map->n = 0;
5005 map->dim = dim;
5006 map->flags = flags;
5007 return map;
5008 error:
5009 isl_space_free(dim);
5010 return NULL;
5013 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5014 unsigned nparam, unsigned in, unsigned out, int n,
5015 unsigned flags)
5017 struct isl_map *map;
5018 isl_space *dims;
5020 dims = isl_space_alloc(ctx, nparam, in, out);
5021 if (!dims)
5022 return NULL;
5024 map = isl_map_alloc_space(dims, n, flags);
5025 return map;
5028 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5030 struct isl_basic_map *bmap;
5031 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5032 bmap = isl_basic_map_set_to_empty(bmap);
5033 return bmap;
5036 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5038 struct isl_basic_set *bset;
5039 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5040 bset = isl_basic_set_set_to_empty(bset);
5041 return bset;
5044 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5046 struct isl_basic_map *bmap;
5047 if (!model)
5048 return NULL;
5049 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5050 bmap = isl_basic_map_set_to_empty(bmap);
5051 return bmap;
5054 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5056 struct isl_basic_map *bmap;
5057 if (!model)
5058 return NULL;
5059 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5060 bmap = isl_basic_map_set_to_empty(bmap);
5061 return bmap;
5064 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5066 struct isl_basic_set *bset;
5067 if (!model)
5068 return NULL;
5069 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5070 bset = isl_basic_set_set_to_empty(bset);
5071 return bset;
5074 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5076 struct isl_basic_map *bmap;
5077 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5078 bmap = isl_basic_map_finalize(bmap);
5079 return bmap;
5082 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5084 struct isl_basic_set *bset;
5085 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5086 bset = isl_basic_set_finalize(bset);
5087 return bset;
5090 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5092 int i;
5093 unsigned total = isl_space_dim(dim, isl_dim_all);
5094 isl_basic_map *bmap;
5096 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5097 for (i = 0; i < total; ++i) {
5098 int k = isl_basic_map_alloc_inequality(bmap);
5099 if (k < 0)
5100 goto error;
5101 isl_seq_clr(bmap->ineq[k], 1 + total);
5102 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5104 return bmap;
5105 error:
5106 isl_basic_map_free(bmap);
5107 return NULL;
5110 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5112 return isl_basic_map_nat_universe(dim);
5115 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5117 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5120 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5122 return isl_map_nat_universe(dim);
5125 __isl_give isl_basic_map *isl_basic_map_universe_like(
5126 __isl_keep isl_basic_map *model)
5128 if (!model)
5129 return NULL;
5130 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5133 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5135 if (!model)
5136 return NULL;
5137 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5140 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5141 __isl_keep isl_set *model)
5143 if (!model)
5144 return NULL;
5145 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5148 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5150 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5153 struct isl_map *isl_map_empty_like(struct isl_map *model)
5155 if (!model)
5156 return NULL;
5157 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5160 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5162 if (!model)
5163 return NULL;
5164 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5167 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5169 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5172 struct isl_set *isl_set_empty_like(struct isl_set *model)
5174 if (!model)
5175 return NULL;
5176 return isl_set_empty(isl_space_copy(model->dim));
5179 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5181 struct isl_map *map;
5182 if (!dim)
5183 return NULL;
5184 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5185 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5186 return map;
5189 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5191 struct isl_set *set;
5192 if (!dim)
5193 return NULL;
5194 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5195 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5196 return set;
5199 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5201 if (!model)
5202 return NULL;
5203 return isl_set_universe(isl_space_copy(model->dim));
5206 struct isl_map *isl_map_dup(struct isl_map *map)
5208 int i;
5209 struct isl_map *dup;
5211 if (!map)
5212 return NULL;
5213 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5214 for (i = 0; i < map->n; ++i)
5215 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5216 return dup;
5219 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5220 __isl_take isl_basic_map *bmap)
5222 if (!bmap || !map)
5223 goto error;
5224 if (isl_basic_map_plain_is_empty(bmap)) {
5225 isl_basic_map_free(bmap);
5226 return map;
5228 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5229 isl_assert(map->ctx, map->n < map->size, goto error);
5230 map->p[map->n] = bmap;
5231 map->n++;
5232 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5233 return map;
5234 error:
5235 if (map)
5236 isl_map_free(map);
5237 if (bmap)
5238 isl_basic_map_free(bmap);
5239 return NULL;
5242 void *isl_map_free(struct isl_map *map)
5244 int i;
5246 if (!map)
5247 return NULL;
5249 if (--map->ref > 0)
5250 return NULL;
5252 isl_ctx_deref(map->ctx);
5253 for (i = 0; i < map->n; ++i)
5254 isl_basic_map_free(map->p[i]);
5255 isl_space_free(map->dim);
5256 free(map);
5258 return NULL;
5261 struct isl_map *isl_map_extend(struct isl_map *base,
5262 unsigned nparam, unsigned n_in, unsigned n_out)
5264 int i;
5266 base = isl_map_cow(base);
5267 if (!base)
5268 return NULL;
5270 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5271 if (!base->dim)
5272 goto error;
5273 for (i = 0; i < base->n; ++i) {
5274 base->p[i] = isl_basic_map_extend_space(base->p[i],
5275 isl_space_copy(base->dim), 0, 0, 0);
5276 if (!base->p[i])
5277 goto error;
5279 return base;
5280 error:
5281 isl_map_free(base);
5282 return NULL;
5285 struct isl_set *isl_set_extend(struct isl_set *base,
5286 unsigned nparam, unsigned dim)
5288 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5289 nparam, 0, dim);
5292 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5293 struct isl_basic_map *bmap, unsigned pos, int value)
5295 int j;
5297 bmap = isl_basic_map_cow(bmap);
5298 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5299 j = isl_basic_map_alloc_equality(bmap);
5300 if (j < 0)
5301 goto error;
5302 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5303 isl_int_set_si(bmap->eq[j][pos], -1);
5304 isl_int_set_si(bmap->eq[j][0], value);
5305 bmap = isl_basic_map_simplify(bmap);
5306 return isl_basic_map_finalize(bmap);
5307 error:
5308 isl_basic_map_free(bmap);
5309 return NULL;
5312 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5313 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5315 int j;
5317 bmap = isl_basic_map_cow(bmap);
5318 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5319 j = isl_basic_map_alloc_equality(bmap);
5320 if (j < 0)
5321 goto error;
5322 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5323 isl_int_set_si(bmap->eq[j][pos], -1);
5324 isl_int_set(bmap->eq[j][0], value);
5325 bmap = isl_basic_map_simplify(bmap);
5326 return isl_basic_map_finalize(bmap);
5327 error:
5328 isl_basic_map_free(bmap);
5329 return NULL;
5332 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5333 enum isl_dim_type type, unsigned pos, int value)
5335 if (!bmap)
5336 return NULL;
5337 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5338 return isl_basic_map_fix_pos_si(bmap,
5339 isl_basic_map_offset(bmap, type) + pos, value);
5340 error:
5341 isl_basic_map_free(bmap);
5342 return NULL;
5345 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5346 enum isl_dim_type type, unsigned pos, isl_int value)
5348 if (!bmap)
5349 return NULL;
5350 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5351 return isl_basic_map_fix_pos(bmap,
5352 isl_basic_map_offset(bmap, type) + pos, value);
5353 error:
5354 isl_basic_map_free(bmap);
5355 return NULL;
5358 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5359 * to be equal to "v".
5361 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5362 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5364 if (!bmap || !v)
5365 goto error;
5366 if (!isl_val_is_int(v))
5367 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5368 "expecting integer value", goto error);
5369 if (pos >= isl_basic_map_dim(bmap, type))
5370 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5371 "index out of bounds", goto error);
5372 pos += isl_basic_map_offset(bmap, type);
5373 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5374 isl_val_free(v);
5375 return bmap;
5376 error:
5377 isl_basic_map_free(bmap);
5378 isl_val_free(v);
5379 return NULL;
5382 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5383 * to be equal to "v".
5385 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5386 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5388 return isl_basic_map_fix_val(bset, type, pos, v);
5391 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5392 enum isl_dim_type type, unsigned pos, int value)
5394 return (struct isl_basic_set *)
5395 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5396 type, pos, value);
5399 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5400 enum isl_dim_type type, unsigned pos, isl_int value)
5402 return (struct isl_basic_set *)
5403 isl_basic_map_fix((struct isl_basic_map *)bset,
5404 type, pos, value);
5407 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5408 unsigned input, int value)
5410 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5413 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5414 unsigned dim, int value)
5416 return (struct isl_basic_set *)
5417 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5418 isl_dim_set, dim, value);
5421 static int remove_if_empty(__isl_keep isl_map *map, int i)
5423 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5425 if (empty < 0)
5426 return -1;
5427 if (!empty)
5428 return 0;
5430 isl_basic_map_free(map->p[i]);
5431 if (i != map->n - 1) {
5432 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5433 map->p[i] = map->p[map->n - 1];
5435 map->n--;
5437 return 0;
5440 /* Perform "fn" on each basic map of "map", where we may not be holding
5441 * the only reference to "map".
5442 * In particular, "fn" should be a semantics preserving operation
5443 * that we want to apply to all copies of "map". We therefore need
5444 * to be careful not to modify "map" in a way that breaks "map"
5445 * in case anything goes wrong.
5447 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5448 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5450 struct isl_basic_map *bmap;
5451 int i;
5453 if (!map)
5454 return NULL;
5456 for (i = map->n - 1; i >= 0; --i) {
5457 bmap = isl_basic_map_copy(map->p[i]);
5458 bmap = fn(bmap);
5459 if (!bmap)
5460 goto error;
5461 isl_basic_map_free(map->p[i]);
5462 map->p[i] = bmap;
5463 if (remove_if_empty(map, i) < 0)
5464 goto error;
5467 return map;
5468 error:
5469 isl_map_free(map);
5470 return NULL;
5473 struct isl_map *isl_map_fix_si(struct isl_map *map,
5474 enum isl_dim_type type, unsigned pos, int value)
5476 int i;
5478 map = isl_map_cow(map);
5479 if (!map)
5480 return NULL;
5482 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5483 for (i = map->n - 1; i >= 0; --i) {
5484 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5485 if (remove_if_empty(map, i) < 0)
5486 goto error;
5488 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5489 return map;
5490 error:
5491 isl_map_free(map);
5492 return NULL;
5495 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5496 enum isl_dim_type type, unsigned pos, int value)
5498 return (struct isl_set *)
5499 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5502 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5503 enum isl_dim_type type, unsigned pos, isl_int value)
5505 int i;
5507 map = isl_map_cow(map);
5508 if (!map)
5509 return NULL;
5511 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5512 for (i = 0; i < map->n; ++i) {
5513 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5514 if (!map->p[i])
5515 goto error;
5517 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5518 return map;
5519 error:
5520 isl_map_free(map);
5521 return NULL;
5524 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5525 enum isl_dim_type type, unsigned pos, isl_int value)
5527 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5530 /* Fix the value of the variable at position "pos" of type "type" of "map"
5531 * to be equal to "v".
5533 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5534 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5536 int i;
5538 map = isl_map_cow(map);
5539 if (!map || !v)
5540 goto error;
5542 if (!isl_val_is_int(v))
5543 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5544 "expecting integer value", goto error);
5545 if (pos >= isl_map_dim(map, type))
5546 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5547 "index out of bounds", goto error);
5548 for (i = map->n - 1; i >= 0; --i) {
5549 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5550 isl_val_copy(v));
5551 if (remove_if_empty(map, i) < 0)
5552 goto error;
5554 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5555 isl_val_free(v);
5556 return map;
5557 error:
5558 isl_map_free(map);
5559 isl_val_free(v);
5560 return NULL;
5563 /* Fix the value of the variable at position "pos" of type "type" of "set"
5564 * to be equal to "v".
5566 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5567 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5569 return isl_map_fix_val(set, type, pos, v);
5572 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5573 unsigned input, int value)
5575 return isl_map_fix_si(map, isl_dim_in, input, value);
5578 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5580 return (struct isl_set *)
5581 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5584 static __isl_give isl_basic_map *basic_map_bound_si(
5585 __isl_take isl_basic_map *bmap,
5586 enum isl_dim_type type, unsigned pos, int value, int upper)
5588 int j;
5590 if (!bmap)
5591 return NULL;
5592 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5593 pos += isl_basic_map_offset(bmap, type);
5594 bmap = isl_basic_map_cow(bmap);
5595 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5596 j = isl_basic_map_alloc_inequality(bmap);
5597 if (j < 0)
5598 goto error;
5599 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5600 if (upper) {
5601 isl_int_set_si(bmap->ineq[j][pos], -1);
5602 isl_int_set_si(bmap->ineq[j][0], value);
5603 } else {
5604 isl_int_set_si(bmap->ineq[j][pos], 1);
5605 isl_int_set_si(bmap->ineq[j][0], -value);
5607 bmap = isl_basic_map_simplify(bmap);
5608 return isl_basic_map_finalize(bmap);
5609 error:
5610 isl_basic_map_free(bmap);
5611 return NULL;
5614 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5615 __isl_take isl_basic_map *bmap,
5616 enum isl_dim_type type, unsigned pos, int value)
5618 return basic_map_bound_si(bmap, type, pos, value, 0);
5621 /* Constrain the values of the given dimension to be no greater than "value".
5623 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5624 __isl_take isl_basic_map *bmap,
5625 enum isl_dim_type type, unsigned pos, int value)
5627 return basic_map_bound_si(bmap, type, pos, value, 1);
5630 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5631 unsigned dim, isl_int value)
5633 int j;
5635 bset = isl_basic_set_cow(bset);
5636 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5637 j = isl_basic_set_alloc_inequality(bset);
5638 if (j < 0)
5639 goto error;
5640 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5641 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5642 isl_int_neg(bset->ineq[j][0], value);
5643 bset = isl_basic_set_simplify(bset);
5644 return isl_basic_set_finalize(bset);
5645 error:
5646 isl_basic_set_free(bset);
5647 return NULL;
5650 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5651 enum isl_dim_type type, unsigned pos, int value, int upper)
5653 int i;
5655 map = isl_map_cow(map);
5656 if (!map)
5657 return NULL;
5659 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5660 for (i = 0; i < map->n; ++i) {
5661 map->p[i] = basic_map_bound_si(map->p[i],
5662 type, pos, value, upper);
5663 if (!map->p[i])
5664 goto error;
5666 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5667 return map;
5668 error:
5669 isl_map_free(map);
5670 return NULL;
5673 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5674 enum isl_dim_type type, unsigned pos, int value)
5676 return map_bound_si(map, type, pos, value, 0);
5679 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5680 enum isl_dim_type type, unsigned pos, int value)
5682 return map_bound_si(map, type, pos, value, 1);
5685 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5686 enum isl_dim_type type, unsigned pos, int value)
5688 return (struct isl_set *)
5689 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5692 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5693 enum isl_dim_type type, unsigned pos, int value)
5695 return isl_map_upper_bound_si(set, type, pos, value);
5698 /* Bound the given variable of "bmap" from below (or above is "upper"
5699 * is set) to "value".
5701 static __isl_give isl_basic_map *basic_map_bound(
5702 __isl_take isl_basic_map *bmap,
5703 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5705 int j;
5707 if (!bmap)
5708 return NULL;
5709 if (pos >= isl_basic_map_dim(bmap, type))
5710 isl_die(bmap->ctx, isl_error_invalid,
5711 "index out of bounds", goto error);
5712 pos += isl_basic_map_offset(bmap, type);
5713 bmap = isl_basic_map_cow(bmap);
5714 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5715 j = isl_basic_map_alloc_inequality(bmap);
5716 if (j < 0)
5717 goto error;
5718 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5719 if (upper) {
5720 isl_int_set_si(bmap->ineq[j][pos], -1);
5721 isl_int_set(bmap->ineq[j][0], value);
5722 } else {
5723 isl_int_set_si(bmap->ineq[j][pos], 1);
5724 isl_int_neg(bmap->ineq[j][0], value);
5726 bmap = isl_basic_map_simplify(bmap);
5727 return isl_basic_map_finalize(bmap);
5728 error:
5729 isl_basic_map_free(bmap);
5730 return NULL;
5733 /* Bound the given variable of "map" from below (or above is "upper"
5734 * is set) to "value".
5736 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5737 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5739 int i;
5741 map = isl_map_cow(map);
5742 if (!map)
5743 return NULL;
5745 if (pos >= isl_map_dim(map, type))
5746 isl_die(map->ctx, isl_error_invalid,
5747 "index out of bounds", goto error);
5748 for (i = map->n - 1; i >= 0; --i) {
5749 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5750 if (remove_if_empty(map, i) < 0)
5751 goto error;
5753 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5754 return map;
5755 error:
5756 isl_map_free(map);
5757 return NULL;
5760 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5761 enum isl_dim_type type, unsigned pos, isl_int value)
5763 return map_bound(map, type, pos, value, 0);
5766 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5767 enum isl_dim_type type, unsigned pos, isl_int value)
5769 return map_bound(map, type, pos, value, 1);
5772 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5773 enum isl_dim_type type, unsigned pos, isl_int value)
5775 return isl_map_lower_bound(set, type, pos, value);
5778 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5779 enum isl_dim_type type, unsigned pos, isl_int value)
5781 return isl_map_upper_bound(set, type, pos, value);
5784 /* Force the values of the variable at position "pos" of type "type" of "set"
5785 * to be no smaller than "value".
5787 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5788 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5790 if (!value)
5791 goto error;
5792 if (!isl_val_is_int(value))
5793 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5794 "expecting integer value", goto error);
5795 set = isl_set_lower_bound(set, type, pos, value->n);
5796 isl_val_free(value);
5797 return set;
5798 error:
5799 isl_val_free(value);
5800 isl_set_free(set);
5801 return NULL;
5804 /* Force the values of the variable at position "pos" of type "type" of "set"
5805 * to be no greater than "value".
5807 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5808 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5810 if (!value)
5811 goto error;
5812 if (!isl_val_is_int(value))
5813 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5814 "expecting integer value", goto error);
5815 set = isl_set_upper_bound(set, type, pos, value->n);
5816 isl_val_free(value);
5817 return set;
5818 error:
5819 isl_val_free(value);
5820 isl_set_free(set);
5821 return NULL;
5824 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5825 isl_int value)
5827 int i;
5829 set = isl_set_cow(set);
5830 if (!set)
5831 return NULL;
5833 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5834 for (i = 0; i < set->n; ++i) {
5835 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5836 if (!set->p[i])
5837 goto error;
5839 return set;
5840 error:
5841 isl_set_free(set);
5842 return NULL;
5845 struct isl_map *isl_map_reverse(struct isl_map *map)
5847 int i;
5849 map = isl_map_cow(map);
5850 if (!map)
5851 return NULL;
5853 map->dim = isl_space_reverse(map->dim);
5854 if (!map->dim)
5855 goto error;
5856 for (i = 0; i < map->n; ++i) {
5857 map->p[i] = isl_basic_map_reverse(map->p[i]);
5858 if (!map->p[i])
5859 goto error;
5861 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5862 return map;
5863 error:
5864 isl_map_free(map);
5865 return NULL;
5868 static struct isl_map *isl_basic_map_partial_lexopt(
5869 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5870 struct isl_set **empty, int max)
5872 if (!bmap)
5873 goto error;
5874 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5875 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5876 else
5877 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5878 error:
5879 isl_basic_map_free(bmap);
5880 isl_basic_set_free(dom);
5881 if (empty)
5882 *empty = NULL;
5883 return NULL;
5886 struct isl_map *isl_basic_map_partial_lexmax(
5887 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5888 struct isl_set **empty)
5890 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5893 struct isl_map *isl_basic_map_partial_lexmin(
5894 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5895 struct isl_set **empty)
5897 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5900 struct isl_set *isl_basic_set_partial_lexmin(
5901 struct isl_basic_set *bset, struct isl_basic_set *dom,
5902 struct isl_set **empty)
5904 return (struct isl_set *)
5905 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5906 dom, empty);
5909 struct isl_set *isl_basic_set_partial_lexmax(
5910 struct isl_basic_set *bset, struct isl_basic_set *dom,
5911 struct isl_set **empty)
5913 return (struct isl_set *)
5914 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5915 dom, empty);
5918 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5919 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5920 __isl_give isl_set **empty)
5922 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5925 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5926 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5927 __isl_give isl_set **empty)
5929 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5932 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5933 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5934 __isl_give isl_set **empty)
5936 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5939 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5940 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5941 __isl_give isl_set **empty)
5943 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5946 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5947 __isl_take isl_basic_map *bmap, int max)
5949 isl_basic_set *dom = NULL;
5950 isl_space *dom_space;
5952 if (!bmap)
5953 goto error;
5954 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5955 dom = isl_basic_set_universe(dom_space);
5956 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5957 error:
5958 isl_basic_map_free(bmap);
5959 return NULL;
5962 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5963 __isl_take isl_basic_map *bmap)
5965 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5968 #undef TYPE
5969 #define TYPE isl_pw_multi_aff
5970 #undef SUFFIX
5971 #define SUFFIX _pw_multi_aff
5972 #undef EMPTY
5973 #define EMPTY isl_pw_multi_aff_empty
5974 #undef ADD
5975 #define ADD isl_pw_multi_aff_union_add
5976 #include "isl_map_lexopt_templ.c"
5978 /* Given a map "map", compute the lexicographically minimal
5979 * (or maximal) image element for each domain element in dom,
5980 * in the form of an isl_pw_multi_aff.
5981 * Set *empty to those elements in dom that do not have an image element.
5983 * We first compute the lexicographically minimal or maximal element
5984 * in the first basic map. This results in a partial solution "res"
5985 * and a subset "todo" of dom that still need to be handled.
5986 * We then consider each of the remaining maps in "map" and successively
5987 * update both "res" and "todo".
5989 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5990 __isl_take isl_map *map, __isl_take isl_set *dom,
5991 __isl_give isl_set **empty, int max)
5993 int i;
5994 isl_pw_multi_aff *res;
5995 isl_set *todo;
5997 if (!map || !dom)
5998 goto error;
6000 if (isl_map_plain_is_empty(map)) {
6001 if (empty)
6002 *empty = dom;
6003 else
6004 isl_set_free(dom);
6005 return isl_pw_multi_aff_from_map(map);
6008 res = basic_map_partial_lexopt_pw_multi_aff(
6009 isl_basic_map_copy(map->p[0]),
6010 isl_set_copy(dom), &todo, max);
6012 for (i = 1; i < map->n; ++i) {
6013 isl_pw_multi_aff *res_i;
6014 isl_set *todo_i;
6016 res_i = basic_map_partial_lexopt_pw_multi_aff(
6017 isl_basic_map_copy(map->p[i]),
6018 isl_set_copy(dom), &todo_i, max);
6020 if (max)
6021 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6022 else
6023 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6025 todo = isl_set_intersect(todo, todo_i);
6028 isl_set_free(dom);
6029 isl_map_free(map);
6031 if (empty)
6032 *empty = todo;
6033 else
6034 isl_set_free(todo);
6036 return res;
6037 error:
6038 if (empty)
6039 *empty = NULL;
6040 isl_set_free(dom);
6041 isl_map_free(map);
6042 return NULL;
6045 #undef TYPE
6046 #define TYPE isl_map
6047 #undef SUFFIX
6048 #define SUFFIX
6049 #undef EMPTY
6050 #define EMPTY isl_map_empty
6051 #undef ADD
6052 #define ADD isl_map_union_disjoint
6053 #include "isl_map_lexopt_templ.c"
6055 /* Given a map "map", compute the lexicographically minimal
6056 * (or maximal) image element for each domain element in dom.
6057 * Set *empty to those elements in dom that do not have an image element.
6059 * We first compute the lexicographically minimal or maximal element
6060 * in the first basic map. This results in a partial solution "res"
6061 * and a subset "todo" of dom that still need to be handled.
6062 * We then consider each of the remaining maps in "map" and successively
6063 * update both "res" and "todo".
6065 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6066 * Assume we are computing the lexicographical maximum.
6067 * We first compute the lexicographically maximal element in basic map i.
6068 * This results in a partial solution res_i and a subset todo_i.
6069 * Then we combine these results with those obtain for the first k basic maps
6070 * to obtain a result that is valid for the first k+1 basic maps.
6071 * In particular, the set where there is no solution is the set where
6072 * there is no solution for the first k basic maps and also no solution
6073 * for the ith basic map, i.e.,
6075 * todo^i = todo^k * todo_i
6077 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6078 * solutions, arbitrarily breaking ties in favor of res^k.
6079 * That is, when res^k(a) >= res_i(a), we pick res^k and
6080 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6081 * the lexicographic order.)
6082 * In practice, we compute
6084 * res^k * (res_i . "<=")
6086 * and
6088 * res_i * (res^k . "<")
6090 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6091 * where only one of res^k and res_i provides a solution and we simply pick
6092 * that one, i.e.,
6094 * res^k * todo_i
6095 * and
6096 * res_i * todo^k
6098 * Note that we only compute these intersections when dom(res^k) intersects
6099 * dom(res_i). Otherwise, the only effect of these intersections is to
6100 * potentially break up res^k and res_i into smaller pieces.
6101 * We want to avoid such splintering as much as possible.
6102 * In fact, an earlier implementation of this function would look for
6103 * better results in the domain of res^k and for extra results in todo^k,
6104 * but this would always result in a splintering according to todo^k,
6105 * even when the domain of basic map i is disjoint from the domains of
6106 * the previous basic maps.
6108 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6109 __isl_take isl_map *map, __isl_take isl_set *dom,
6110 __isl_give isl_set **empty, int max)
6112 int i;
6113 struct isl_map *res;
6114 struct isl_set *todo;
6116 if (!map || !dom)
6117 goto error;
6119 if (isl_map_plain_is_empty(map)) {
6120 if (empty)
6121 *empty = dom;
6122 else
6123 isl_set_free(dom);
6124 return map;
6127 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6128 isl_set_copy(dom), &todo, max);
6130 for (i = 1; i < map->n; ++i) {
6131 isl_map *lt, *le;
6132 isl_map *res_i;
6133 isl_set *todo_i;
6134 isl_space *dim = isl_space_range(isl_map_get_space(res));
6136 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6137 isl_set_copy(dom), &todo_i, max);
6139 if (max) {
6140 lt = isl_map_lex_lt(isl_space_copy(dim));
6141 le = isl_map_lex_le(dim);
6142 } else {
6143 lt = isl_map_lex_gt(isl_space_copy(dim));
6144 le = isl_map_lex_ge(dim);
6146 lt = isl_map_apply_range(isl_map_copy(res), lt);
6147 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6148 le = isl_map_apply_range(isl_map_copy(res_i), le);
6149 le = isl_map_intersect(le, isl_map_copy(res));
6151 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6152 res = isl_map_intersect_domain(res,
6153 isl_set_copy(todo_i));
6154 res_i = isl_map_intersect_domain(res_i,
6155 isl_set_copy(todo));
6158 res = isl_map_union_disjoint(res, res_i);
6159 res = isl_map_union_disjoint(res, lt);
6160 res = isl_map_union_disjoint(res, le);
6162 todo = isl_set_intersect(todo, todo_i);
6165 isl_set_free(dom);
6166 isl_map_free(map);
6168 if (empty)
6169 *empty = todo;
6170 else
6171 isl_set_free(todo);
6173 return res;
6174 error:
6175 if (empty)
6176 *empty = NULL;
6177 isl_set_free(dom);
6178 isl_map_free(map);
6179 return NULL;
6182 __isl_give isl_map *isl_map_partial_lexmax(
6183 __isl_take isl_map *map, __isl_take isl_set *dom,
6184 __isl_give isl_set **empty)
6186 return isl_map_partial_lexopt(map, dom, empty, 1);
6189 __isl_give isl_map *isl_map_partial_lexmin(
6190 __isl_take isl_map *map, __isl_take isl_set *dom,
6191 __isl_give isl_set **empty)
6193 return isl_map_partial_lexopt(map, dom, empty, 0);
6196 __isl_give isl_set *isl_set_partial_lexmin(
6197 __isl_take isl_set *set, __isl_take isl_set *dom,
6198 __isl_give isl_set **empty)
6200 return (struct isl_set *)
6201 isl_map_partial_lexmin((struct isl_map *)set,
6202 dom, empty);
6205 __isl_give isl_set *isl_set_partial_lexmax(
6206 __isl_take isl_set *set, __isl_take isl_set *dom,
6207 __isl_give isl_set **empty)
6209 return (struct isl_set *)
6210 isl_map_partial_lexmax((struct isl_map *)set,
6211 dom, empty);
6214 /* Compute the lexicographic minimum (or maximum if "max" is set)
6215 * of "bmap" over its domain.
6217 * Since we are not interested in the part of the domain space where
6218 * there is no solution, we initialize the domain to those constraints
6219 * of "bmap" that only involve the parameters and the input dimensions.
6220 * This relieves the parametric programming engine from detecting those
6221 * inequalities and transferring them to the context. More importantly,
6222 * it ensures that those inequalities are transferred first and not
6223 * intermixed with inequalities that actually split the domain.
6225 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6227 int n_div;
6228 int n_out;
6229 isl_basic_map *copy;
6230 isl_basic_set *dom;
6232 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6233 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6234 copy = isl_basic_map_copy(bmap);
6235 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6236 isl_dim_div, 0, n_div);
6237 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6238 isl_dim_out, 0, n_out);
6239 dom = isl_basic_map_domain(copy);
6240 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6243 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6245 return isl_basic_map_lexopt(bmap, 0);
6248 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6250 return isl_basic_map_lexopt(bmap, 1);
6253 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6255 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6258 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6260 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6263 /* Extract the first and only affine expression from list
6264 * and then add it to *pwaff with the given dom.
6265 * This domain is known to be disjoint from other domains
6266 * because of the way isl_basic_map_foreach_lexmax works.
6268 static int update_dim_opt(__isl_take isl_basic_set *dom,
6269 __isl_take isl_aff_list *list, void *user)
6271 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6272 isl_aff *aff;
6273 isl_pw_aff **pwaff = user;
6274 isl_pw_aff *pwaff_i;
6276 if (!list)
6277 goto error;
6278 if (isl_aff_list_n_aff(list) != 1)
6279 isl_die(ctx, isl_error_internal,
6280 "expecting single element list", goto error);
6282 aff = isl_aff_list_get_aff(list, 0);
6283 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6285 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6287 isl_aff_list_free(list);
6289 return 0;
6290 error:
6291 isl_basic_set_free(dom);
6292 isl_aff_list_free(list);
6293 return -1;
6296 /* Given a basic map with one output dimension, compute the minimum or
6297 * maximum of that dimension as an isl_pw_aff.
6299 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6300 * call update_dim_opt on each leaf of the result.
6302 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6303 int max)
6305 isl_space *dim = isl_basic_map_get_space(bmap);
6306 isl_pw_aff *pwaff;
6307 int r;
6309 dim = isl_space_from_domain(isl_space_domain(dim));
6310 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6311 pwaff = isl_pw_aff_empty(dim);
6313 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6314 if (r < 0)
6315 return isl_pw_aff_free(pwaff);
6317 return pwaff;
6320 /* Compute the minimum or maximum of the given output dimension
6321 * as a function of the parameters and the input dimensions,
6322 * but independently of the other output dimensions.
6324 * We first project out the other output dimension and then compute
6325 * the "lexicographic" maximum in each basic map, combining the results
6326 * using isl_pw_aff_union_max.
6328 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6329 int max)
6331 int i;
6332 isl_pw_aff *pwaff;
6333 unsigned n_out;
6335 n_out = isl_map_dim(map, isl_dim_out);
6336 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6337 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6338 if (!map)
6339 return NULL;
6341 if (map->n == 0) {
6342 isl_space *dim = isl_map_get_space(map);
6343 dim = isl_space_domain(isl_space_from_range(dim));
6344 isl_map_free(map);
6345 return isl_pw_aff_empty(dim);
6348 pwaff = basic_map_dim_opt(map->p[0], max);
6349 for (i = 1; i < map->n; ++i) {
6350 isl_pw_aff *pwaff_i;
6352 pwaff_i = basic_map_dim_opt(map->p[i], max);
6353 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6356 isl_map_free(map);
6358 return pwaff;
6361 /* Compute the maximum of the given output dimension as a function of the
6362 * parameters and input dimensions, but independently of
6363 * the other output dimensions.
6365 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6367 return map_dim_opt(map, pos, 1);
6370 /* Compute the minimum or maximum of the given set dimension
6371 * as a function of the parameters,
6372 * but independently of the other set dimensions.
6374 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6375 int max)
6377 return map_dim_opt(set, pos, max);
6380 /* Compute the maximum of the given set dimension as a function of the
6381 * parameters, but independently of the other set dimensions.
6383 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6385 return set_dim_opt(set, pos, 1);
6388 /* Compute the minimum of the given set dimension as a function of the
6389 * parameters, but independently of the other set dimensions.
6391 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6393 return set_dim_opt(set, pos, 0);
6396 /* Apply a preimage specified by "mat" on the parameters of "bset".
6397 * bset is assumed to have only parameters and divs.
6399 static struct isl_basic_set *basic_set_parameter_preimage(
6400 struct isl_basic_set *bset, struct isl_mat *mat)
6402 unsigned nparam;
6404 if (!bset || !mat)
6405 goto error;
6407 bset->dim = isl_space_cow(bset->dim);
6408 if (!bset->dim)
6409 goto error;
6411 nparam = isl_basic_set_dim(bset, isl_dim_param);
6413 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6415 bset->dim->nparam = 0;
6416 bset->dim->n_out = nparam;
6417 bset = isl_basic_set_preimage(bset, mat);
6418 if (bset) {
6419 bset->dim->nparam = bset->dim->n_out;
6420 bset->dim->n_out = 0;
6422 return bset;
6423 error:
6424 isl_mat_free(mat);
6425 isl_basic_set_free(bset);
6426 return NULL;
6429 /* Apply a preimage specified by "mat" on the parameters of "set".
6430 * set is assumed to have only parameters and divs.
6432 static struct isl_set *set_parameter_preimage(
6433 struct isl_set *set, struct isl_mat *mat)
6435 isl_space *dim = NULL;
6436 unsigned nparam;
6438 if (!set || !mat)
6439 goto error;
6441 dim = isl_space_copy(set->dim);
6442 dim = isl_space_cow(dim);
6443 if (!dim)
6444 goto error;
6446 nparam = isl_set_dim(set, isl_dim_param);
6448 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6450 dim->nparam = 0;
6451 dim->n_out = nparam;
6452 isl_set_reset_space(set, dim);
6453 set = isl_set_preimage(set, mat);
6454 if (!set)
6455 goto error2;
6456 dim = isl_space_copy(set->dim);
6457 dim = isl_space_cow(dim);
6458 if (!dim)
6459 goto error2;
6460 dim->nparam = dim->n_out;
6461 dim->n_out = 0;
6462 isl_set_reset_space(set, dim);
6463 return set;
6464 error:
6465 isl_space_free(dim);
6466 isl_mat_free(mat);
6467 error2:
6468 isl_set_free(set);
6469 return NULL;
6472 /* Intersect the basic set "bset" with the affine space specified by the
6473 * equalities in "eq".
6475 static struct isl_basic_set *basic_set_append_equalities(
6476 struct isl_basic_set *bset, struct isl_mat *eq)
6478 int i, k;
6479 unsigned len;
6481 if (!bset || !eq)
6482 goto error;
6484 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6485 eq->n_row, 0);
6486 if (!bset)
6487 goto error;
6489 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6490 for (i = 0; i < eq->n_row; ++i) {
6491 k = isl_basic_set_alloc_equality(bset);
6492 if (k < 0)
6493 goto error;
6494 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6495 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6497 isl_mat_free(eq);
6499 bset = isl_basic_set_gauss(bset, NULL);
6500 bset = isl_basic_set_finalize(bset);
6502 return bset;
6503 error:
6504 isl_mat_free(eq);
6505 isl_basic_set_free(bset);
6506 return NULL;
6509 /* Intersect the set "set" with the affine space specified by the
6510 * equalities in "eq".
6512 static struct isl_set *set_append_equalities(struct isl_set *set,
6513 struct isl_mat *eq)
6515 int i;
6517 if (!set || !eq)
6518 goto error;
6520 for (i = 0; i < set->n; ++i) {
6521 set->p[i] = basic_set_append_equalities(set->p[i],
6522 isl_mat_copy(eq));
6523 if (!set->p[i])
6524 goto error;
6526 isl_mat_free(eq);
6527 return set;
6528 error:
6529 isl_mat_free(eq);
6530 isl_set_free(set);
6531 return NULL;
6534 /* Given a basic set "bset" that only involves parameters and existentially
6535 * quantified variables, return the index of the first equality
6536 * that only involves parameters. If there is no such equality then
6537 * return bset->n_eq.
6539 * This function assumes that isl_basic_set_gauss has been called on "bset".
6541 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6543 int i, j;
6544 unsigned nparam, n_div;
6546 if (!bset)
6547 return -1;
6549 nparam = isl_basic_set_dim(bset, isl_dim_param);
6550 n_div = isl_basic_set_dim(bset, isl_dim_div);
6552 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6553 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6554 ++i;
6557 return i;
6560 /* Compute an explicit representation for the existentially quantified
6561 * variables in "bset" by computing the "minimal value" of the set
6562 * variables. Since there are no set variables, the computation of
6563 * the minimal value essentially computes an explicit representation
6564 * of the non-empty part(s) of "bset".
6566 * The input only involves parameters and existentially quantified variables.
6567 * All equalities among parameters have been removed.
6569 * Since the existentially quantified variables in the result are in general
6570 * going to be different from those in the input, we first replace
6571 * them by the minimal number of variables based on their equalities.
6572 * This should simplify the parametric integer programming.
6574 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6576 isl_morph *morph1, *morph2;
6577 isl_set *set;
6578 unsigned n;
6580 if (!bset)
6581 return NULL;
6582 if (bset->n_eq == 0)
6583 return isl_basic_set_lexmin(bset);
6585 morph1 = isl_basic_set_parameter_compression(bset);
6586 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6587 bset = isl_basic_set_lift(bset);
6588 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6589 bset = isl_morph_basic_set(morph2, bset);
6590 n = isl_basic_set_dim(bset, isl_dim_set);
6591 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6593 set = isl_basic_set_lexmin(bset);
6595 set = isl_morph_set(isl_morph_inverse(morph1), set);
6597 return set;
6600 /* Project the given basic set onto its parameter domain, possibly introducing
6601 * new, explicit, existential variables in the constraints.
6602 * The input has parameters and (possibly implicit) existential variables.
6603 * The output has the same parameters, but only
6604 * explicit existentially quantified variables.
6606 * The actual projection is performed by pip, but pip doesn't seem
6607 * to like equalities very much, so we first remove the equalities
6608 * among the parameters by performing a variable compression on
6609 * the parameters. Afterward, an inverse transformation is performed
6610 * and the equalities among the parameters are inserted back in.
6612 * The variable compression on the parameters may uncover additional
6613 * equalities that were only implicit before. We therefore check
6614 * if there are any new parameter equalities in the result and
6615 * if so recurse. The removal of parameter equalities is required
6616 * for the parameter compression performed by base_compute_divs.
6618 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6620 int i;
6621 struct isl_mat *eq;
6622 struct isl_mat *T, *T2;
6623 struct isl_set *set;
6624 unsigned nparam;
6626 bset = isl_basic_set_cow(bset);
6627 if (!bset)
6628 return NULL;
6630 if (bset->n_eq == 0)
6631 return base_compute_divs(bset);
6633 bset = isl_basic_set_gauss(bset, NULL);
6634 if (!bset)
6635 return NULL;
6636 if (isl_basic_set_plain_is_empty(bset))
6637 return isl_set_from_basic_set(bset);
6639 i = first_parameter_equality(bset);
6640 if (i == bset->n_eq)
6641 return base_compute_divs(bset);
6643 nparam = isl_basic_set_dim(bset, isl_dim_param);
6644 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6645 0, 1 + nparam);
6646 eq = isl_mat_cow(eq);
6647 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6648 if (T && T->n_col == 0) {
6649 isl_mat_free(T);
6650 isl_mat_free(T2);
6651 isl_mat_free(eq);
6652 bset = isl_basic_set_set_to_empty(bset);
6653 return isl_set_from_basic_set(bset);
6655 bset = basic_set_parameter_preimage(bset, T);
6657 i = first_parameter_equality(bset);
6658 if (!bset)
6659 set = NULL;
6660 else if (i == bset->n_eq)
6661 set = base_compute_divs(bset);
6662 else
6663 set = parameter_compute_divs(bset);
6664 set = set_parameter_preimage(set, T2);
6665 set = set_append_equalities(set, eq);
6666 return set;
6669 /* Insert the divs from "ls" before those of "bmap".
6671 * The number of columns is not changed, which means that the last
6672 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6673 * The caller is responsible for removing the same number of dimensions
6674 * from the space of "bmap".
6676 static __isl_give isl_basic_map *insert_divs_from_local_space(
6677 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6679 int i;
6680 int n_div;
6681 int old_n_div;
6683 n_div = isl_local_space_dim(ls, isl_dim_div);
6684 if (n_div == 0)
6685 return bmap;
6687 old_n_div = bmap->n_div;
6688 bmap = insert_div_rows(bmap, n_div);
6689 if (!bmap)
6690 return NULL;
6692 for (i = 0; i < n_div; ++i) {
6693 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6694 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6697 return bmap;
6700 /* Replace the space of "bmap" by the space and divs of "ls".
6702 * If "ls" has any divs, then we simplify the result since we may
6703 * have discovered some additional equalities that could simplify
6704 * the div expressions.
6706 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6707 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6709 int n_div;
6711 bmap = isl_basic_map_cow(bmap);
6712 if (!bmap || !ls)
6713 goto error;
6715 n_div = isl_local_space_dim(ls, isl_dim_div);
6716 bmap = insert_divs_from_local_space(bmap, ls);
6717 if (!bmap)
6718 goto error;
6720 isl_space_free(bmap->dim);
6721 bmap->dim = isl_local_space_get_space(ls);
6722 if (!bmap->dim)
6723 goto error;
6725 isl_local_space_free(ls);
6726 if (n_div > 0)
6727 bmap = isl_basic_map_simplify(bmap);
6728 bmap = isl_basic_map_finalize(bmap);
6729 return bmap;
6730 error:
6731 isl_basic_map_free(bmap);
6732 isl_local_space_free(ls);
6733 return NULL;
6736 /* Replace the space of "map" by the space and divs of "ls".
6738 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6739 __isl_take isl_local_space *ls)
6741 int i;
6743 map = isl_map_cow(map);
6744 if (!map || !ls)
6745 goto error;
6747 for (i = 0; i < map->n; ++i) {
6748 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6749 isl_local_space_copy(ls));
6750 if (!map->p[i])
6751 goto error;
6753 isl_space_free(map->dim);
6754 map->dim = isl_local_space_get_space(ls);
6755 if (!map->dim)
6756 goto error;
6758 isl_local_space_free(ls);
6759 return map;
6760 error:
6761 isl_local_space_free(ls);
6762 isl_map_free(map);
6763 return NULL;
6766 /* Compute an explicit representation for the existentially
6767 * quantified variables for which do not know any explicit representation yet.
6769 * We first sort the existentially quantified variables so that the
6770 * existentially quantified variables for which we already have an explicit
6771 * representation are placed before those for which we do not.
6772 * The input dimensions, the output dimensions and the existentially
6773 * quantified variables for which we already have an explicit
6774 * representation are then turned into parameters.
6775 * compute_divs returns a map with the same parameters and
6776 * no input or output dimensions and the dimension specification
6777 * is reset to that of the input, including the existentially quantified
6778 * variables for which we already had an explicit representation.
6780 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6782 struct isl_basic_set *bset;
6783 struct isl_set *set;
6784 struct isl_map *map;
6785 isl_space *dim;
6786 isl_local_space *ls;
6787 unsigned nparam;
6788 unsigned n_in;
6789 unsigned n_out;
6790 unsigned n_known;
6791 int i;
6793 bmap = isl_basic_map_sort_divs(bmap);
6794 bmap = isl_basic_map_cow(bmap);
6795 if (!bmap)
6796 return NULL;
6798 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6799 if (isl_int_is_zero(bmap->div[n_known][0]))
6800 break;
6802 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6803 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6804 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6805 dim = isl_space_set_alloc(bmap->ctx,
6806 nparam + n_in + n_out + n_known, 0);
6807 if (!dim)
6808 goto error;
6810 ls = isl_basic_map_get_local_space(bmap);
6811 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6812 n_known, bmap->n_div - n_known);
6813 if (n_known > 0) {
6814 for (i = n_known; i < bmap->n_div; ++i)
6815 swap_div(bmap, i - n_known, i);
6816 bmap->n_div -= n_known;
6817 bmap->extra -= n_known;
6819 bmap = isl_basic_map_reset_space(bmap, dim);
6820 bset = (struct isl_basic_set *)bmap;
6822 set = parameter_compute_divs(bset);
6823 map = (struct isl_map *)set;
6824 map = replace_space_by_local_space(map, ls);
6826 return map;
6827 error:
6828 isl_basic_map_free(bmap);
6829 return NULL;
6832 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6834 int i;
6835 unsigned off;
6837 if (!bmap)
6838 return -1;
6840 off = isl_space_dim(bmap->dim, isl_dim_all);
6841 for (i = 0; i < bmap->n_div; ++i) {
6842 if (isl_int_is_zero(bmap->div[i][0]))
6843 return 0;
6844 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6845 return -1);
6847 return 1;
6850 static int map_divs_known(__isl_keep isl_map *map)
6852 int i;
6854 if (!map)
6855 return -1;
6857 for (i = 0; i < map->n; ++i) {
6858 int known = isl_basic_map_divs_known(map->p[i]);
6859 if (known <= 0)
6860 return known;
6863 return 1;
6866 /* If bmap contains any unknown divs, then compute explicit
6867 * expressions for them. However, this computation may be
6868 * quite expensive, so first try to remove divs that aren't
6869 * strictly needed.
6871 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6873 int known;
6874 struct isl_map *map;
6876 known = isl_basic_map_divs_known(bmap);
6877 if (known < 0)
6878 goto error;
6879 if (known)
6880 return isl_map_from_basic_map(bmap);
6882 bmap = isl_basic_map_drop_redundant_divs(bmap);
6884 known = isl_basic_map_divs_known(bmap);
6885 if (known < 0)
6886 goto error;
6887 if (known)
6888 return isl_map_from_basic_map(bmap);
6890 map = compute_divs(bmap);
6891 return map;
6892 error:
6893 isl_basic_map_free(bmap);
6894 return NULL;
6897 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6899 int i;
6900 int known;
6901 struct isl_map *res;
6903 if (!map)
6904 return NULL;
6905 if (map->n == 0)
6906 return map;
6908 known = map_divs_known(map);
6909 if (known < 0) {
6910 isl_map_free(map);
6911 return NULL;
6913 if (known)
6914 return map;
6916 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6917 for (i = 1 ; i < map->n; ++i) {
6918 struct isl_map *r2;
6919 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6920 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6921 res = isl_map_union_disjoint(res, r2);
6922 else
6923 res = isl_map_union(res, r2);
6925 isl_map_free(map);
6927 return res;
6930 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6932 return (struct isl_set *)
6933 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6936 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6938 return (struct isl_set *)
6939 isl_map_compute_divs((struct isl_map *)set);
6942 struct isl_set *isl_map_domain(struct isl_map *map)
6944 int i;
6945 struct isl_set *set;
6947 if (!map)
6948 goto error;
6950 map = isl_map_cow(map);
6951 if (!map)
6952 return NULL;
6954 set = (struct isl_set *)map;
6955 set->dim = isl_space_domain(set->dim);
6956 if (!set->dim)
6957 goto error;
6958 for (i = 0; i < map->n; ++i) {
6959 set->p[i] = isl_basic_map_domain(map->p[i]);
6960 if (!set->p[i])
6961 goto error;
6963 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6964 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6965 return set;
6966 error:
6967 isl_map_free(map);
6968 return NULL;
6971 /* Return the union of "map1" and "map2", where we assume for now that
6972 * "map1" and "map2" are disjoint. Note that the basic maps inside
6973 * "map1" or "map2" may not be disjoint from each other.
6974 * Also note that this function is also called from isl_map_union,
6975 * which takes care of handling the situation where "map1" and "map2"
6976 * may not be disjoint.
6978 * If one of the inputs is empty, we can simply return the other input.
6979 * Similarly, if one of the inputs is universal, then it is equal to the union.
6981 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6982 __isl_take isl_map *map2)
6984 int i;
6985 unsigned flags = 0;
6986 struct isl_map *map = NULL;
6987 int is_universe;
6989 if (!map1 || !map2)
6990 goto error;
6992 if (map1->n == 0) {
6993 isl_map_free(map1);
6994 return map2;
6996 if (map2->n == 0) {
6997 isl_map_free(map2);
6998 return map1;
7001 is_universe = isl_map_plain_is_universe(map1);
7002 if (is_universe < 0)
7003 goto error;
7004 if (is_universe) {
7005 isl_map_free(map2);
7006 return map1;
7009 is_universe = isl_map_plain_is_universe(map2);
7010 if (is_universe < 0)
7011 goto error;
7012 if (is_universe) {
7013 isl_map_free(map1);
7014 return map2;
7017 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7019 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7020 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7021 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7023 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7024 map1->n + map2->n, flags);
7025 if (!map)
7026 goto error;
7027 for (i = 0; i < map1->n; ++i) {
7028 map = isl_map_add_basic_map(map,
7029 isl_basic_map_copy(map1->p[i]));
7030 if (!map)
7031 goto error;
7033 for (i = 0; i < map2->n; ++i) {
7034 map = isl_map_add_basic_map(map,
7035 isl_basic_map_copy(map2->p[i]));
7036 if (!map)
7037 goto error;
7039 isl_map_free(map1);
7040 isl_map_free(map2);
7041 return map;
7042 error:
7043 isl_map_free(map);
7044 isl_map_free(map1);
7045 isl_map_free(map2);
7046 return NULL;
7049 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7050 __isl_take isl_map *map2)
7052 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7055 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7057 map1 = isl_map_union_disjoint(map1, map2);
7058 if (!map1)
7059 return NULL;
7060 if (map1->n > 1)
7061 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7062 return map1;
7065 struct isl_set *isl_set_union_disjoint(
7066 struct isl_set *set1, struct isl_set *set2)
7068 return (struct isl_set *)
7069 isl_map_union_disjoint(
7070 (struct isl_map *)set1, (struct isl_map *)set2);
7073 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7075 return (struct isl_set *)
7076 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7079 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7080 * the results.
7082 * "map" and "set" are assumed to be compatible and non-NULL.
7084 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7085 __isl_take isl_set *set,
7086 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7087 __isl_take isl_basic_set *bset))
7089 unsigned flags = 0;
7090 struct isl_map *result;
7091 int i, j;
7093 if (isl_set_plain_is_universe(set)) {
7094 isl_set_free(set);
7095 return map;
7098 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7099 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7100 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7102 result = isl_map_alloc_space(isl_space_copy(map->dim),
7103 map->n * set->n, flags);
7104 for (i = 0; result && i < map->n; ++i)
7105 for (j = 0; j < set->n; ++j) {
7106 result = isl_map_add_basic_map(result,
7107 fn(isl_basic_map_copy(map->p[i]),
7108 isl_basic_set_copy(set->p[j])));
7109 if (!result)
7110 break;
7113 isl_map_free(map);
7114 isl_set_free(set);
7115 return result;
7118 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7119 __isl_take isl_set *set)
7121 if (!map || !set)
7122 goto error;
7124 if (!isl_map_compatible_range(map, set))
7125 isl_die(set->ctx, isl_error_invalid,
7126 "incompatible spaces", goto error);
7128 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7129 error:
7130 isl_map_free(map);
7131 isl_set_free(set);
7132 return NULL;
7135 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7136 __isl_take isl_set *set)
7138 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7141 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7142 __isl_take isl_set *set)
7144 if (!map || !set)
7145 goto error;
7147 if (!isl_map_compatible_domain(map, set))
7148 isl_die(set->ctx, isl_error_invalid,
7149 "incompatible spaces", goto error);
7151 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7152 error:
7153 isl_map_free(map);
7154 isl_set_free(set);
7155 return NULL;
7158 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7159 __isl_take isl_set *set)
7161 return isl_map_align_params_map_map_and(map, set,
7162 &map_intersect_domain);
7165 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7166 __isl_take isl_map *map2)
7168 if (!map1 || !map2)
7169 goto error;
7170 map1 = isl_map_reverse(map1);
7171 map1 = isl_map_apply_range(map1, map2);
7172 return isl_map_reverse(map1);
7173 error:
7174 isl_map_free(map1);
7175 isl_map_free(map2);
7176 return NULL;
7179 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7180 __isl_take isl_map *map2)
7182 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7185 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7186 __isl_take isl_map *map2)
7188 isl_space *dim_result;
7189 struct isl_map *result;
7190 int i, j;
7192 if (!map1 || !map2)
7193 goto error;
7195 dim_result = isl_space_join(isl_space_copy(map1->dim),
7196 isl_space_copy(map2->dim));
7198 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7199 if (!result)
7200 goto error;
7201 for (i = 0; i < map1->n; ++i)
7202 for (j = 0; j < map2->n; ++j) {
7203 result = isl_map_add_basic_map(result,
7204 isl_basic_map_apply_range(
7205 isl_basic_map_copy(map1->p[i]),
7206 isl_basic_map_copy(map2->p[j])));
7207 if (!result)
7208 goto error;
7210 isl_map_free(map1);
7211 isl_map_free(map2);
7212 if (result && result->n <= 1)
7213 ISL_F_SET(result, ISL_MAP_DISJOINT);
7214 return result;
7215 error:
7216 isl_map_free(map1);
7217 isl_map_free(map2);
7218 return NULL;
7221 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7222 __isl_take isl_map *map2)
7224 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7228 * returns range - domain
7230 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7232 isl_space *dims, *target_dim;
7233 struct isl_basic_set *bset;
7234 unsigned dim;
7235 unsigned nparam;
7236 int i;
7238 if (!bmap)
7239 goto error;
7240 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7241 bmap->dim, isl_dim_out),
7242 goto error);
7243 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7244 dim = isl_basic_map_n_in(bmap);
7245 nparam = isl_basic_map_n_param(bmap);
7246 bset = isl_basic_set_from_basic_map(bmap);
7247 bset = isl_basic_set_cow(bset);
7248 dims = isl_basic_set_get_space(bset);
7249 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7250 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7251 bset = isl_basic_set_swap_vars(bset, 2*dim);
7252 for (i = 0; i < dim; ++i) {
7253 int j = isl_basic_map_alloc_equality(
7254 (struct isl_basic_map *)bset);
7255 if (j < 0) {
7256 bset = isl_basic_set_free(bset);
7257 break;
7259 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7260 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7261 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7262 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7264 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7265 bset = isl_basic_set_reset_space(bset, target_dim);
7266 return bset;
7267 error:
7268 isl_basic_map_free(bmap);
7269 return NULL;
7273 * returns range - domain
7275 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7277 int i;
7278 isl_space *dim;
7279 struct isl_set *result;
7281 if (!map)
7282 return NULL;
7284 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7285 map->dim, isl_dim_out),
7286 goto error);
7287 dim = isl_map_get_space(map);
7288 dim = isl_space_domain(dim);
7289 result = isl_set_alloc_space(dim, map->n, 0);
7290 if (!result)
7291 goto error;
7292 for (i = 0; i < map->n; ++i)
7293 result = isl_set_add_basic_set(result,
7294 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7295 isl_map_free(map);
7296 return result;
7297 error:
7298 isl_map_free(map);
7299 return NULL;
7303 * returns [domain -> range] -> range - domain
7305 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7306 __isl_take isl_basic_map *bmap)
7308 int i, k;
7309 isl_space *dim;
7310 isl_basic_map *domain;
7311 int nparam, n;
7312 unsigned total;
7314 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7315 isl_die(bmap->ctx, isl_error_invalid,
7316 "domain and range don't match", goto error);
7318 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7319 n = isl_basic_map_dim(bmap, isl_dim_in);
7321 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7322 domain = isl_basic_map_universe(dim);
7324 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7325 bmap = isl_basic_map_apply_range(bmap, domain);
7326 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7328 total = isl_basic_map_total_dim(bmap);
7330 for (i = 0; i < n; ++i) {
7331 k = isl_basic_map_alloc_equality(bmap);
7332 if (k < 0)
7333 goto error;
7334 isl_seq_clr(bmap->eq[k], 1 + total);
7335 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7336 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7337 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7340 bmap = isl_basic_map_gauss(bmap, NULL);
7341 return isl_basic_map_finalize(bmap);
7342 error:
7343 isl_basic_map_free(bmap);
7344 return NULL;
7348 * returns [domain -> range] -> range - domain
7350 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7352 int i;
7353 isl_space *domain_dim;
7355 if (!map)
7356 return NULL;
7358 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7359 isl_die(map->ctx, isl_error_invalid,
7360 "domain and range don't match", goto error);
7362 map = isl_map_cow(map);
7363 if (!map)
7364 return NULL;
7366 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7367 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7368 map->dim = isl_space_join(map->dim, domain_dim);
7369 if (!map->dim)
7370 goto error;
7371 for (i = 0; i < map->n; ++i) {
7372 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7373 if (!map->p[i])
7374 goto error;
7376 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7377 return map;
7378 error:
7379 isl_map_free(map);
7380 return NULL;
7383 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7385 struct isl_basic_map *bmap;
7386 unsigned nparam;
7387 unsigned dim;
7388 int i;
7390 if (!dims)
7391 return NULL;
7393 nparam = dims->nparam;
7394 dim = dims->n_out;
7395 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7396 if (!bmap)
7397 goto error;
7399 for (i = 0; i < dim; ++i) {
7400 int j = isl_basic_map_alloc_equality(bmap);
7401 if (j < 0)
7402 goto error;
7403 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7404 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7405 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7407 return isl_basic_map_finalize(bmap);
7408 error:
7409 isl_basic_map_free(bmap);
7410 return NULL;
7413 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7415 if (!dim)
7416 return NULL;
7417 if (dim->n_in != dim->n_out)
7418 isl_die(dim->ctx, isl_error_invalid,
7419 "number of input and output dimensions needs to be "
7420 "the same", goto error);
7421 return basic_map_identity(dim);
7422 error:
7423 isl_space_free(dim);
7424 return NULL;
7427 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7429 if (!model || !model->dim)
7430 return NULL;
7431 return isl_basic_map_identity(isl_space_copy(model->dim));
7434 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7436 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7439 struct isl_map *isl_map_identity_like(struct isl_map *model)
7441 if (!model || !model->dim)
7442 return NULL;
7443 return isl_map_identity(isl_space_copy(model->dim));
7446 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7448 if (!model || !model->dim)
7449 return NULL;
7450 return isl_map_identity(isl_space_copy(model->dim));
7453 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7455 isl_space *dim = isl_set_get_space(set);
7456 isl_map *id;
7457 id = isl_map_identity(isl_space_map_from_set(dim));
7458 return isl_map_intersect_range(id, set);
7461 /* Construct a basic set with all set dimensions having only non-negative
7462 * values.
7464 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7465 __isl_take isl_space *space)
7467 int i;
7468 unsigned nparam;
7469 unsigned dim;
7470 struct isl_basic_set *bset;
7472 if (!space)
7473 return NULL;
7474 nparam = space->nparam;
7475 dim = space->n_out;
7476 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7477 if (!bset)
7478 return NULL;
7479 for (i = 0; i < dim; ++i) {
7480 int k = isl_basic_set_alloc_inequality(bset);
7481 if (k < 0)
7482 goto error;
7483 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7484 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7486 return bset;
7487 error:
7488 isl_basic_set_free(bset);
7489 return NULL;
7492 /* Construct the half-space x_pos >= 0.
7494 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7495 int pos)
7497 int k;
7498 isl_basic_set *nonneg;
7500 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7501 k = isl_basic_set_alloc_inequality(nonneg);
7502 if (k < 0)
7503 goto error;
7504 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7505 isl_int_set_si(nonneg->ineq[k][pos], 1);
7507 return isl_basic_set_finalize(nonneg);
7508 error:
7509 isl_basic_set_free(nonneg);
7510 return NULL;
7513 /* Construct the half-space x_pos <= -1.
7515 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7517 int k;
7518 isl_basic_set *neg;
7520 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7521 k = isl_basic_set_alloc_inequality(neg);
7522 if (k < 0)
7523 goto error;
7524 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7525 isl_int_set_si(neg->ineq[k][0], -1);
7526 isl_int_set_si(neg->ineq[k][pos], -1);
7528 return isl_basic_set_finalize(neg);
7529 error:
7530 isl_basic_set_free(neg);
7531 return NULL;
7534 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7535 enum isl_dim_type type, unsigned first, unsigned n)
7537 int i;
7538 isl_basic_set *nonneg;
7539 isl_basic_set *neg;
7541 if (!set)
7542 return NULL;
7543 if (n == 0)
7544 return set;
7546 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7548 for (i = 0; i < n; ++i) {
7549 nonneg = nonneg_halfspace(isl_set_get_space(set),
7550 pos(set->dim, type) + first + i);
7551 neg = neg_halfspace(isl_set_get_space(set),
7552 pos(set->dim, type) + first + i);
7554 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7557 return set;
7558 error:
7559 isl_set_free(set);
7560 return NULL;
7563 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7564 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7565 void *user)
7567 isl_set *half;
7569 if (!set)
7570 return -1;
7571 if (isl_set_plain_is_empty(set)) {
7572 isl_set_free(set);
7573 return 0;
7575 if (first == len)
7576 return fn(set, signs, user);
7578 signs[first] = 1;
7579 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7580 1 + first));
7581 half = isl_set_intersect(half, isl_set_copy(set));
7582 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7583 goto error;
7585 signs[first] = -1;
7586 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7587 1 + first));
7588 half = isl_set_intersect(half, set);
7589 return foreach_orthant(half, signs, first + 1, len, fn, user);
7590 error:
7591 isl_set_free(set);
7592 return -1;
7595 /* Call "fn" on the intersections of "set" with each of the orthants
7596 * (except for obviously empty intersections). The orthant is identified
7597 * by the signs array, with each entry having value 1 or -1 according
7598 * to the sign of the corresponding variable.
7600 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7601 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7602 void *user)
7604 unsigned nparam;
7605 unsigned nvar;
7606 int *signs;
7607 int r;
7609 if (!set)
7610 return -1;
7611 if (isl_set_plain_is_empty(set))
7612 return 0;
7614 nparam = isl_set_dim(set, isl_dim_param);
7615 nvar = isl_set_dim(set, isl_dim_set);
7617 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7619 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7620 fn, user);
7622 free(signs);
7624 return r;
7627 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7629 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7632 int isl_basic_map_is_subset(
7633 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7635 int is_subset;
7636 struct isl_map *map1;
7637 struct isl_map *map2;
7639 if (!bmap1 || !bmap2)
7640 return -1;
7642 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7643 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7645 is_subset = isl_map_is_subset(map1, map2);
7647 isl_map_free(map1);
7648 isl_map_free(map2);
7650 return is_subset;
7653 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7654 __isl_keep isl_basic_set *bset2)
7656 return isl_basic_map_is_subset(bset1, bset2);
7659 int isl_basic_map_is_equal(
7660 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7662 int is_subset;
7664 if (!bmap1 || !bmap2)
7665 return -1;
7666 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7667 if (is_subset != 1)
7668 return is_subset;
7669 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7670 return is_subset;
7673 int isl_basic_set_is_equal(
7674 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7676 return isl_basic_map_is_equal(
7677 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7680 int isl_map_is_empty(struct isl_map *map)
7682 int i;
7683 int is_empty;
7685 if (!map)
7686 return -1;
7687 for (i = 0; i < map->n; ++i) {
7688 is_empty = isl_basic_map_is_empty(map->p[i]);
7689 if (is_empty < 0)
7690 return -1;
7691 if (!is_empty)
7692 return 0;
7694 return 1;
7697 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7699 return map ? map->n == 0 : -1;
7702 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7704 return isl_map_plain_is_empty(map);
7707 int isl_set_plain_is_empty(struct isl_set *set)
7709 return set ? set->n == 0 : -1;
7712 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7714 return isl_set_plain_is_empty(set);
7717 int isl_set_is_empty(struct isl_set *set)
7719 return isl_map_is_empty((struct isl_map *)set);
7722 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7724 if (!map1 || !map2)
7725 return -1;
7727 return isl_space_is_equal(map1->dim, map2->dim);
7730 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7732 if (!set1 || !set2)
7733 return -1;
7735 return isl_space_is_equal(set1->dim, set2->dim);
7738 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7740 int is_subset;
7742 if (!map1 || !map2)
7743 return -1;
7744 is_subset = isl_map_is_subset(map1, map2);
7745 if (is_subset != 1)
7746 return is_subset;
7747 is_subset = isl_map_is_subset(map2, map1);
7748 return is_subset;
7751 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7753 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7756 int isl_basic_map_is_strict_subset(
7757 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7759 int is_subset;
7761 if (!bmap1 || !bmap2)
7762 return -1;
7763 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7764 if (is_subset != 1)
7765 return is_subset;
7766 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7767 if (is_subset == -1)
7768 return is_subset;
7769 return !is_subset;
7772 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7774 int is_subset;
7776 if (!map1 || !map2)
7777 return -1;
7778 is_subset = isl_map_is_subset(map1, map2);
7779 if (is_subset != 1)
7780 return is_subset;
7781 is_subset = isl_map_is_subset(map2, map1);
7782 if (is_subset == -1)
7783 return is_subset;
7784 return !is_subset;
7787 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7789 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7792 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7794 if (!bmap)
7795 return -1;
7796 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7799 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7801 if (!bset)
7802 return -1;
7803 return bset->n_eq == 0 && bset->n_ineq == 0;
7806 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7808 int i;
7810 if (!map)
7811 return -1;
7813 for (i = 0; i < map->n; ++i) {
7814 int r = isl_basic_map_is_universe(map->p[i]);
7815 if (r < 0 || r)
7816 return r;
7819 return 0;
7822 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7824 return isl_map_plain_is_universe((isl_map *) set);
7827 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7829 return isl_set_plain_is_universe(set);
7832 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7834 struct isl_basic_set *bset = NULL;
7835 struct isl_vec *sample = NULL;
7836 int empty;
7837 unsigned total;
7839 if (!bmap)
7840 return -1;
7842 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7843 return 1;
7845 if (isl_basic_map_is_universe(bmap))
7846 return 0;
7848 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7849 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7850 copy = isl_basic_map_remove_redundancies(copy);
7851 empty = isl_basic_map_plain_is_empty(copy);
7852 isl_basic_map_free(copy);
7853 return empty;
7856 total = 1 + isl_basic_map_total_dim(bmap);
7857 if (bmap->sample && bmap->sample->size == total) {
7858 int contains = isl_basic_map_contains(bmap, bmap->sample);
7859 if (contains < 0)
7860 return -1;
7861 if (contains)
7862 return 0;
7864 isl_vec_free(bmap->sample);
7865 bmap->sample = NULL;
7866 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7867 if (!bset)
7868 return -1;
7869 sample = isl_basic_set_sample_vec(bset);
7870 if (!sample)
7871 return -1;
7872 empty = sample->size == 0;
7873 isl_vec_free(bmap->sample);
7874 bmap->sample = sample;
7875 if (empty)
7876 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7878 return empty;
7881 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7883 if (!bmap)
7884 return -1;
7885 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7888 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7890 return isl_basic_map_plain_is_empty(bmap);
7893 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7895 if (!bset)
7896 return -1;
7897 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7900 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7902 return isl_basic_set_plain_is_empty(bset);
7905 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7907 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7910 struct isl_map *isl_basic_map_union(
7911 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7913 struct isl_map *map;
7914 if (!bmap1 || !bmap2)
7915 goto error;
7917 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7919 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7920 if (!map)
7921 goto error;
7922 map = isl_map_add_basic_map(map, bmap1);
7923 map = isl_map_add_basic_map(map, bmap2);
7924 return map;
7925 error:
7926 isl_basic_map_free(bmap1);
7927 isl_basic_map_free(bmap2);
7928 return NULL;
7931 struct isl_set *isl_basic_set_union(
7932 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7934 return (struct isl_set *)isl_basic_map_union(
7935 (struct isl_basic_map *)bset1,
7936 (struct isl_basic_map *)bset2);
7939 /* Order divs such that any div only depends on previous divs */
7940 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7942 int i;
7943 unsigned off;
7945 if (!bmap)
7946 return NULL;
7948 off = isl_space_dim(bmap->dim, isl_dim_all);
7950 for (i = 0; i < bmap->n_div; ++i) {
7951 int pos;
7952 if (isl_int_is_zero(bmap->div[i][0]))
7953 continue;
7954 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7955 bmap->n_div-i);
7956 if (pos == -1)
7957 continue;
7958 isl_basic_map_swap_div(bmap, i, i + pos);
7959 --i;
7961 return bmap;
7964 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7966 return (struct isl_basic_set *)
7967 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7970 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7972 int i;
7974 if (!map)
7975 return 0;
7977 for (i = 0; i < map->n; ++i) {
7978 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7979 if (!map->p[i])
7980 goto error;
7983 return map;
7984 error:
7985 isl_map_free(map);
7986 return NULL;
7989 /* Apply the expansion computed by isl_merge_divs.
7990 * The expansion itself is given by "exp" while the resulting
7991 * list of divs is given by "div".
7993 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7994 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7996 int i, j;
7997 int n_div;
7999 bset = isl_basic_set_cow(bset);
8000 if (!bset || !div)
8001 goto error;
8003 if (div->n_row < bset->n_div)
8004 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8005 "not an expansion", goto error);
8007 n_div = bset->n_div;
8008 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8009 div->n_row - n_div, 0,
8010 2 * (div->n_row - n_div));
8012 for (i = n_div; i < div->n_row; ++i)
8013 if (isl_basic_set_alloc_div(bset) < 0)
8014 goto error;
8016 j = n_div - 1;
8017 for (i = div->n_row - 1; i >= 0; --i) {
8018 if (j >= 0 && exp[j] == i) {
8019 if (i != j)
8020 isl_basic_map_swap_div(bset, i, j);
8021 j--;
8022 } else {
8023 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8024 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8025 goto error;
8029 isl_mat_free(div);
8030 return bset;
8031 error:
8032 isl_basic_set_free(bset);
8033 isl_mat_free(div);
8034 return NULL;
8037 /* Look for a div in dst that corresponds to the div "div" in src.
8038 * The divs before "div" in src and dst are assumed to be the same.
8040 * Returns -1 if no corresponding div was found and the position
8041 * of the corresponding div in dst otherwise.
8043 static int find_div(struct isl_basic_map *dst,
8044 struct isl_basic_map *src, unsigned div)
8046 int i;
8048 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8050 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8051 for (i = div; i < dst->n_div; ++i)
8052 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8053 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8054 dst->n_div - div) == -1)
8055 return i;
8056 return -1;
8059 struct isl_basic_map *isl_basic_map_align_divs(
8060 struct isl_basic_map *dst, struct isl_basic_map *src)
8062 int i;
8063 unsigned total;
8065 if (!dst || !src)
8066 goto error;
8068 if (src->n_div == 0)
8069 return dst;
8071 for (i = 0; i < src->n_div; ++i)
8072 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8074 src = isl_basic_map_order_divs(src);
8075 dst = isl_basic_map_cow(dst);
8076 if (!dst)
8077 return NULL;
8078 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8079 src->n_div, 0, 2 * src->n_div);
8080 if (!dst)
8081 return NULL;
8082 total = isl_space_dim(src->dim, isl_dim_all);
8083 for (i = 0; i < src->n_div; ++i) {
8084 int j = find_div(dst, src, i);
8085 if (j < 0) {
8086 j = isl_basic_map_alloc_div(dst);
8087 if (j < 0)
8088 goto error;
8089 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8090 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8091 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8092 goto error;
8094 if (j != i)
8095 isl_basic_map_swap_div(dst, i, j);
8097 return dst;
8098 error:
8099 isl_basic_map_free(dst);
8100 return NULL;
8103 struct isl_basic_set *isl_basic_set_align_divs(
8104 struct isl_basic_set *dst, struct isl_basic_set *src)
8106 return (struct isl_basic_set *)isl_basic_map_align_divs(
8107 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8110 struct isl_map *isl_map_align_divs(struct isl_map *map)
8112 int i;
8114 if (!map)
8115 return NULL;
8116 if (map->n == 0)
8117 return map;
8118 map = isl_map_compute_divs(map);
8119 map = isl_map_cow(map);
8120 if (!map)
8121 return NULL;
8123 for (i = 1; i < map->n; ++i)
8124 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8125 for (i = 1; i < map->n; ++i) {
8126 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8127 if (!map->p[i])
8128 return isl_map_free(map);
8131 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8132 return map;
8135 struct isl_set *isl_set_align_divs(struct isl_set *set)
8137 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8140 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8141 __isl_take isl_map *map)
8143 if (!set || !map)
8144 goto error;
8145 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8146 map = isl_map_intersect_domain(map, set);
8147 set = isl_map_range(map);
8148 return set;
8149 error:
8150 isl_set_free(set);
8151 isl_map_free(map);
8152 return NULL;
8155 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8156 __isl_take isl_map *map)
8158 return isl_map_align_params_map_map_and(set, map, &set_apply);
8161 /* There is no need to cow as removing empty parts doesn't change
8162 * the meaning of the set.
8164 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8166 int i;
8168 if (!map)
8169 return NULL;
8171 for (i = map->n - 1; i >= 0; --i)
8172 remove_if_empty(map, i);
8174 return map;
8177 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8179 return (struct isl_set *)
8180 isl_map_remove_empty_parts((struct isl_map *)set);
8183 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8185 struct isl_basic_map *bmap;
8186 if (!map || map->n == 0)
8187 return NULL;
8188 bmap = map->p[map->n-1];
8189 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8190 return isl_basic_map_copy(bmap);
8193 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8195 return (struct isl_basic_set *)
8196 isl_map_copy_basic_map((struct isl_map *)set);
8199 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8200 __isl_keep isl_basic_map *bmap)
8202 int i;
8204 if (!map || !bmap)
8205 goto error;
8206 for (i = map->n-1; i >= 0; --i) {
8207 if (map->p[i] != bmap)
8208 continue;
8209 map = isl_map_cow(map);
8210 if (!map)
8211 goto error;
8212 isl_basic_map_free(map->p[i]);
8213 if (i != map->n-1) {
8214 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8215 map->p[i] = map->p[map->n-1];
8217 map->n--;
8218 return map;
8220 return map;
8221 error:
8222 isl_map_free(map);
8223 return NULL;
8226 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8227 struct isl_basic_set *bset)
8229 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8230 (struct isl_basic_map *)bset);
8233 /* Given two basic sets bset1 and bset2, compute the maximal difference
8234 * between the values of dimension pos in bset1 and those in bset2
8235 * for any common value of the parameters and dimensions preceding pos.
8237 static enum isl_lp_result basic_set_maximal_difference_at(
8238 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8239 int pos, isl_int *opt)
8241 isl_space *dims;
8242 struct isl_basic_map *bmap1 = NULL;
8243 struct isl_basic_map *bmap2 = NULL;
8244 struct isl_ctx *ctx;
8245 struct isl_vec *obj;
8246 unsigned total;
8247 unsigned nparam;
8248 unsigned dim1, dim2;
8249 enum isl_lp_result res;
8251 if (!bset1 || !bset2)
8252 return isl_lp_error;
8254 nparam = isl_basic_set_n_param(bset1);
8255 dim1 = isl_basic_set_n_dim(bset1);
8256 dim2 = isl_basic_set_n_dim(bset2);
8257 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8258 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8259 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8260 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8261 if (!bmap1 || !bmap2)
8262 goto error;
8263 bmap1 = isl_basic_map_cow(bmap1);
8264 bmap1 = isl_basic_map_extend(bmap1, nparam,
8265 pos, (dim1 - pos) + (dim2 - pos),
8266 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8267 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8268 if (!bmap1)
8269 goto error;
8270 total = isl_basic_map_total_dim(bmap1);
8271 ctx = bmap1->ctx;
8272 obj = isl_vec_alloc(ctx, 1 + total);
8273 if (!obj)
8274 goto error2;
8275 isl_seq_clr(obj->block.data, 1 + total);
8276 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8277 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8278 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8279 opt, NULL, NULL);
8280 isl_basic_map_free(bmap1);
8281 isl_vec_free(obj);
8282 return res;
8283 error:
8284 isl_basic_map_free(bmap2);
8285 error2:
8286 isl_basic_map_free(bmap1);
8287 return isl_lp_error;
8290 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8291 * for any common value of the parameters and dimensions preceding pos
8292 * in both basic sets, the values of dimension pos in bset1 are
8293 * smaller or larger than those in bset2.
8295 * Returns
8296 * 1 if bset1 follows bset2
8297 * -1 if bset1 precedes bset2
8298 * 0 if bset1 and bset2 are incomparable
8299 * -2 if some error occurred.
8301 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8302 struct isl_basic_set *bset2, int pos)
8304 isl_int opt;
8305 enum isl_lp_result res;
8306 int cmp;
8308 isl_int_init(opt);
8310 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8312 if (res == isl_lp_empty)
8313 cmp = 0;
8314 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8315 res == isl_lp_unbounded)
8316 cmp = 1;
8317 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8318 cmp = -1;
8319 else
8320 cmp = -2;
8322 isl_int_clear(opt);
8323 return cmp;
8326 /* Given two basic sets bset1 and bset2, check whether
8327 * for any common value of the parameters and dimensions preceding pos
8328 * there is a value of dimension pos in bset1 that is larger
8329 * than a value of the same dimension in bset2.
8331 * Return
8332 * 1 if there exists such a pair
8333 * 0 if there is no such pair, but there is a pair of equal values
8334 * -1 otherwise
8335 * -2 if some error occurred.
8337 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8338 __isl_keep isl_basic_set *bset2, int pos)
8340 isl_int opt;
8341 enum isl_lp_result res;
8342 int cmp;
8344 isl_int_init(opt);
8346 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8348 if (res == isl_lp_empty)
8349 cmp = -1;
8350 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8351 res == isl_lp_unbounded)
8352 cmp = 1;
8353 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8354 cmp = -1;
8355 else if (res == isl_lp_ok)
8356 cmp = 0;
8357 else
8358 cmp = -2;
8360 isl_int_clear(opt);
8361 return cmp;
8364 /* Given two sets set1 and set2, check whether
8365 * for any common value of the parameters and dimensions preceding pos
8366 * there is a value of dimension pos in set1 that is larger
8367 * than a value of the same dimension in set2.
8369 * Return
8370 * 1 if there exists such a pair
8371 * 0 if there is no such pair, but there is a pair of equal values
8372 * -1 otherwise
8373 * -2 if some error occurred.
8375 int isl_set_follows_at(__isl_keep isl_set *set1,
8376 __isl_keep isl_set *set2, int pos)
8378 int i, j;
8379 int follows = -1;
8381 if (!set1 || !set2)
8382 return -2;
8384 for (i = 0; i < set1->n; ++i)
8385 for (j = 0; j < set2->n; ++j) {
8386 int f;
8387 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8388 if (f == 1 || f == -2)
8389 return f;
8390 if (f > follows)
8391 follows = f;
8394 return follows;
8397 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8398 unsigned pos, isl_int *val)
8400 int i;
8401 int d;
8402 unsigned total;
8404 if (!bmap)
8405 return -1;
8406 total = isl_basic_map_total_dim(bmap);
8407 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8408 for (; d+1 > pos; --d)
8409 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8410 break;
8411 if (d != pos)
8412 continue;
8413 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8414 return 0;
8415 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8416 return 0;
8417 if (!isl_int_is_one(bmap->eq[i][1+d]))
8418 return 0;
8419 if (val)
8420 isl_int_neg(*val, bmap->eq[i][0]);
8421 return 1;
8423 return 0;
8426 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8427 unsigned pos, isl_int *val)
8429 int i;
8430 isl_int v;
8431 isl_int tmp;
8432 int fixed;
8434 if (!map)
8435 return -1;
8436 if (map->n == 0)
8437 return 0;
8438 if (map->n == 1)
8439 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8440 isl_int_init(v);
8441 isl_int_init(tmp);
8442 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8443 for (i = 1; fixed == 1 && i < map->n; ++i) {
8444 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8445 if (fixed == 1 && isl_int_ne(tmp, v))
8446 fixed = 0;
8448 if (val)
8449 isl_int_set(*val, v);
8450 isl_int_clear(tmp);
8451 isl_int_clear(v);
8452 return fixed;
8455 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8456 unsigned pos, isl_int *val)
8458 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8459 pos, val);
8462 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8463 isl_int *val)
8465 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8468 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8469 enum isl_dim_type type, unsigned pos, isl_int *val)
8471 if (pos >= isl_basic_map_dim(bmap, type))
8472 return -1;
8473 return isl_basic_map_plain_has_fixed_var(bmap,
8474 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8477 /* If "bmap" obviously lies on a hyperplane where the given dimension
8478 * has a fixed value, then return that value.
8479 * Otherwise return NaN.
8481 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8482 __isl_keep isl_basic_map *bmap,
8483 enum isl_dim_type type, unsigned pos)
8485 isl_ctx *ctx;
8486 isl_val *v;
8487 int fixed;
8489 if (!bmap)
8490 return NULL;
8491 ctx = isl_basic_map_get_ctx(bmap);
8492 v = isl_val_alloc(ctx);
8493 if (!v)
8494 return NULL;
8495 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8496 if (fixed < 0)
8497 return isl_val_free(v);
8498 if (fixed) {
8499 isl_int_set_si(v->d, 1);
8500 return v;
8502 isl_val_free(v);
8503 return isl_val_nan(ctx);
8506 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8507 enum isl_dim_type type, unsigned pos, isl_int *val)
8509 if (pos >= isl_map_dim(map, type))
8510 return -1;
8511 return isl_map_plain_has_fixed_var(map,
8512 map_offset(map, type) - 1 + pos, val);
8515 /* If "map" obviously lies on a hyperplane where the given dimension
8516 * has a fixed value, then return that value.
8517 * Otherwise return NaN.
8519 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8520 enum isl_dim_type type, unsigned pos)
8522 isl_ctx *ctx;
8523 isl_val *v;
8524 int fixed;
8526 if (!map)
8527 return NULL;
8528 ctx = isl_map_get_ctx(map);
8529 v = isl_val_alloc(ctx);
8530 if (!v)
8531 return NULL;
8532 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8533 if (fixed < 0)
8534 return isl_val_free(v);
8535 if (fixed) {
8536 isl_int_set_si(v->d, 1);
8537 return v;
8539 isl_val_free(v);
8540 return isl_val_nan(ctx);
8543 /* If "set" obviously lies on a hyperplane where the given dimension
8544 * has a fixed value, then return that value.
8545 * Otherwise return NaN.
8547 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8548 enum isl_dim_type type, unsigned pos)
8550 return isl_map_plain_get_val_if_fixed(set, type, pos);
8553 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8554 enum isl_dim_type type, unsigned pos, isl_int *val)
8556 return isl_map_plain_is_fixed(set, type, pos, val);
8559 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8560 enum isl_dim_type type, unsigned pos, isl_int *val)
8562 return isl_map_plain_is_fixed(map, type, pos, val);
8565 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8566 * then return this fixed value in *val.
8568 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8569 unsigned dim, isl_int *val)
8571 return isl_basic_set_plain_has_fixed_var(bset,
8572 isl_basic_set_n_param(bset) + dim, val);
8575 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8576 * then return this fixed value in *val.
8578 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8579 unsigned dim, isl_int *val)
8581 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8584 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8585 unsigned dim, isl_int *val)
8587 return isl_set_plain_dim_is_fixed(set, dim, val);
8590 /* Check if input variable in has fixed value and if so and if val is not NULL,
8591 * then return this fixed value in *val.
8593 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8594 unsigned in, isl_int *val)
8596 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8599 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8600 * and if val is not NULL, then return this lower bound in *val.
8602 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8603 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8605 int i, i_eq = -1, i_ineq = -1;
8606 isl_int *c;
8607 unsigned total;
8608 unsigned nparam;
8610 if (!bset)
8611 return -1;
8612 total = isl_basic_set_total_dim(bset);
8613 nparam = isl_basic_set_n_param(bset);
8614 for (i = 0; i < bset->n_eq; ++i) {
8615 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8616 continue;
8617 if (i_eq != -1)
8618 return 0;
8619 i_eq = i;
8621 for (i = 0; i < bset->n_ineq; ++i) {
8622 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8623 continue;
8624 if (i_eq != -1 || i_ineq != -1)
8625 return 0;
8626 i_ineq = i;
8628 if (i_eq == -1 && i_ineq == -1)
8629 return 0;
8630 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8631 /* The coefficient should always be one due to normalization. */
8632 if (!isl_int_is_one(c[1+nparam+dim]))
8633 return 0;
8634 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8635 return 0;
8636 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8637 total - nparam - dim - 1) != -1)
8638 return 0;
8639 if (val)
8640 isl_int_neg(*val, c[0]);
8641 return 1;
8644 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8645 unsigned dim, isl_int *val)
8647 int i;
8648 isl_int v;
8649 isl_int tmp;
8650 int fixed;
8652 if (!set)
8653 return -1;
8654 if (set->n == 0)
8655 return 0;
8656 if (set->n == 1)
8657 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8658 dim, val);
8659 isl_int_init(v);
8660 isl_int_init(tmp);
8661 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8662 dim, &v);
8663 for (i = 1; fixed == 1 && i < set->n; ++i) {
8664 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8665 dim, &tmp);
8666 if (fixed == 1 && isl_int_ne(tmp, v))
8667 fixed = 0;
8669 if (val)
8670 isl_int_set(*val, v);
8671 isl_int_clear(tmp);
8672 isl_int_clear(v);
8673 return fixed;
8676 struct constraint {
8677 unsigned size;
8678 isl_int *c;
8681 /* uset_gist depends on constraints without existentially quantified
8682 * variables sorting first.
8684 static int qsort_constraint_cmp(const void *p1, const void *p2)
8686 const struct constraint *c1 = (const struct constraint *)p1;
8687 const struct constraint *c2 = (const struct constraint *)p2;
8688 int l1, l2;
8689 unsigned size = isl_min(c1->size, c2->size);
8691 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8692 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8694 if (l1 != l2)
8695 return l1 - l2;
8697 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8700 static struct isl_basic_map *isl_basic_map_sort_constraints(
8701 struct isl_basic_map *bmap)
8703 int i;
8704 struct constraint *c;
8705 unsigned total;
8707 if (!bmap)
8708 return NULL;
8709 total = isl_basic_map_total_dim(bmap);
8710 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8711 if (!c)
8712 goto error;
8713 for (i = 0; i < bmap->n_ineq; ++i) {
8714 c[i].size = total;
8715 c[i].c = bmap->ineq[i];
8717 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8718 for (i = 0; i < bmap->n_ineq; ++i)
8719 bmap->ineq[i] = c[i].c;
8720 free(c);
8721 return bmap;
8722 error:
8723 isl_basic_map_free(bmap);
8724 return NULL;
8727 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8728 __isl_take isl_basic_set *bset)
8730 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8731 (struct isl_basic_map *)bset);
8734 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8736 if (!bmap)
8737 return NULL;
8738 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8739 return bmap;
8740 bmap = isl_basic_map_remove_redundancies(bmap);
8741 bmap = isl_basic_map_sort_constraints(bmap);
8742 if (bmap)
8743 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8744 return bmap;
8747 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8749 return (struct isl_basic_set *)isl_basic_map_normalize(
8750 (struct isl_basic_map *)bset);
8753 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8754 const __isl_keep isl_basic_map *bmap2)
8756 int i, cmp;
8757 unsigned total;
8759 if (bmap1 == bmap2)
8760 return 0;
8761 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8762 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8763 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8764 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8765 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8766 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8767 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8768 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8769 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8770 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8771 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8772 return 0;
8773 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8774 return 1;
8775 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8776 return -1;
8777 if (bmap1->n_eq != bmap2->n_eq)
8778 return bmap1->n_eq - bmap2->n_eq;
8779 if (bmap1->n_ineq != bmap2->n_ineq)
8780 return bmap1->n_ineq - bmap2->n_ineq;
8781 if (bmap1->n_div != bmap2->n_div)
8782 return bmap1->n_div - bmap2->n_div;
8783 total = isl_basic_map_total_dim(bmap1);
8784 for (i = 0; i < bmap1->n_eq; ++i) {
8785 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8786 if (cmp)
8787 return cmp;
8789 for (i = 0; i < bmap1->n_ineq; ++i) {
8790 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8791 if (cmp)
8792 return cmp;
8794 for (i = 0; i < bmap1->n_div; ++i) {
8795 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8796 if (cmp)
8797 return cmp;
8799 return 0;
8802 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8803 const __isl_keep isl_basic_set *bset2)
8805 return isl_basic_map_plain_cmp(bset1, bset2);
8808 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8810 int i, cmp;
8812 if (set1 == set2)
8813 return 0;
8814 if (set1->n != set2->n)
8815 return set1->n - set2->n;
8817 for (i = 0; i < set1->n; ++i) {
8818 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8819 if (cmp)
8820 return cmp;
8823 return 0;
8826 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8827 __isl_keep isl_basic_map *bmap2)
8829 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8832 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8833 __isl_keep isl_basic_set *bset2)
8835 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8836 (isl_basic_map *)bset2);
8839 static int qsort_bmap_cmp(const void *p1, const void *p2)
8841 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8842 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8844 return isl_basic_map_plain_cmp(bmap1, bmap2);
8847 /* We normalize in place, but if anything goes wrong we need
8848 * to return NULL, so we need to make sure we don't change the
8849 * meaning of any possible other copies of map.
8851 struct isl_map *isl_map_normalize(struct isl_map *map)
8853 int i, j;
8854 struct isl_basic_map *bmap;
8856 if (!map)
8857 return NULL;
8858 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8859 return map;
8860 for (i = 0; i < map->n; ++i) {
8861 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8862 if (!bmap)
8863 goto error;
8864 isl_basic_map_free(map->p[i]);
8865 map->p[i] = bmap;
8867 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8868 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8869 map = isl_map_remove_empty_parts(map);
8870 if (!map)
8871 return NULL;
8872 for (i = map->n - 1; i >= 1; --i) {
8873 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8874 continue;
8875 isl_basic_map_free(map->p[i-1]);
8876 for (j = i; j < map->n; ++j)
8877 map->p[j-1] = map->p[j];
8878 map->n--;
8880 return map;
8881 error:
8882 isl_map_free(map);
8883 return NULL;
8887 struct isl_set *isl_set_normalize(struct isl_set *set)
8889 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8892 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8894 int i;
8895 int equal;
8897 if (!map1 || !map2)
8898 return -1;
8900 if (map1 == map2)
8901 return 1;
8902 if (!isl_space_is_equal(map1->dim, map2->dim))
8903 return 0;
8905 map1 = isl_map_copy(map1);
8906 map2 = isl_map_copy(map2);
8907 map1 = isl_map_normalize(map1);
8908 map2 = isl_map_normalize(map2);
8909 if (!map1 || !map2)
8910 goto error;
8911 equal = map1->n == map2->n;
8912 for (i = 0; equal && i < map1->n; ++i) {
8913 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8914 if (equal < 0)
8915 goto error;
8917 isl_map_free(map1);
8918 isl_map_free(map2);
8919 return equal;
8920 error:
8921 isl_map_free(map1);
8922 isl_map_free(map2);
8923 return -1;
8926 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8928 return isl_map_plain_is_equal(map1, map2);
8931 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8933 return isl_map_plain_is_equal((struct isl_map *)set1,
8934 (struct isl_map *)set2);
8937 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8939 return isl_set_plain_is_equal(set1, set2);
8942 /* Return an interval that ranges from min to max (inclusive)
8944 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8945 isl_int min, isl_int max)
8947 int k;
8948 struct isl_basic_set *bset = NULL;
8950 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8951 if (!bset)
8952 goto error;
8954 k = isl_basic_set_alloc_inequality(bset);
8955 if (k < 0)
8956 goto error;
8957 isl_int_set_si(bset->ineq[k][1], 1);
8958 isl_int_neg(bset->ineq[k][0], min);
8960 k = isl_basic_set_alloc_inequality(bset);
8961 if (k < 0)
8962 goto error;
8963 isl_int_set_si(bset->ineq[k][1], -1);
8964 isl_int_set(bset->ineq[k][0], max);
8966 return bset;
8967 error:
8968 isl_basic_set_free(bset);
8969 return NULL;
8972 /* Return the Cartesian product of the basic sets in list (in the given order).
8974 __isl_give isl_basic_set *isl_basic_set_list_product(
8975 __isl_take struct isl_basic_set_list *list)
8977 int i;
8978 unsigned dim;
8979 unsigned nparam;
8980 unsigned extra;
8981 unsigned n_eq;
8982 unsigned n_ineq;
8983 struct isl_basic_set *product = NULL;
8985 if (!list)
8986 goto error;
8987 isl_assert(list->ctx, list->n > 0, goto error);
8988 isl_assert(list->ctx, list->p[0], goto error);
8989 nparam = isl_basic_set_n_param(list->p[0]);
8990 dim = isl_basic_set_n_dim(list->p[0]);
8991 extra = list->p[0]->n_div;
8992 n_eq = list->p[0]->n_eq;
8993 n_ineq = list->p[0]->n_ineq;
8994 for (i = 1; i < list->n; ++i) {
8995 isl_assert(list->ctx, list->p[i], goto error);
8996 isl_assert(list->ctx,
8997 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8998 dim += isl_basic_set_n_dim(list->p[i]);
8999 extra += list->p[i]->n_div;
9000 n_eq += list->p[i]->n_eq;
9001 n_ineq += list->p[i]->n_ineq;
9003 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9004 n_eq, n_ineq);
9005 if (!product)
9006 goto error;
9007 dim = 0;
9008 for (i = 0; i < list->n; ++i) {
9009 isl_basic_set_add_constraints(product,
9010 isl_basic_set_copy(list->p[i]), dim);
9011 dim += isl_basic_set_n_dim(list->p[i]);
9013 isl_basic_set_list_free(list);
9014 return product;
9015 error:
9016 isl_basic_set_free(product);
9017 isl_basic_set_list_free(list);
9018 return NULL;
9021 struct isl_basic_map *isl_basic_map_product(
9022 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9024 isl_space *dim_result = NULL;
9025 struct isl_basic_map *bmap;
9026 unsigned in1, in2, out1, out2, nparam, total, pos;
9027 struct isl_dim_map *dim_map1, *dim_map2;
9029 if (!bmap1 || !bmap2)
9030 goto error;
9032 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9033 bmap2->dim, isl_dim_param), goto error);
9034 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9035 isl_space_copy(bmap2->dim));
9037 in1 = isl_basic_map_n_in(bmap1);
9038 in2 = isl_basic_map_n_in(bmap2);
9039 out1 = isl_basic_map_n_out(bmap1);
9040 out2 = isl_basic_map_n_out(bmap2);
9041 nparam = isl_basic_map_n_param(bmap1);
9043 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9044 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9045 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9048 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9049 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9052 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9053 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9055 bmap = isl_basic_map_alloc_space(dim_result,
9056 bmap1->n_div + bmap2->n_div,
9057 bmap1->n_eq + bmap2->n_eq,
9058 bmap1->n_ineq + bmap2->n_ineq);
9059 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9060 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9061 bmap = isl_basic_map_simplify(bmap);
9062 return isl_basic_map_finalize(bmap);
9063 error:
9064 isl_basic_map_free(bmap1);
9065 isl_basic_map_free(bmap2);
9066 return NULL;
9069 __isl_give isl_basic_map *isl_basic_map_flat_product(
9070 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9072 isl_basic_map *prod;
9074 prod = isl_basic_map_product(bmap1, bmap2);
9075 prod = isl_basic_map_flatten(prod);
9076 return prod;
9079 __isl_give isl_basic_set *isl_basic_set_flat_product(
9080 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9082 return isl_basic_map_flat_range_product(bset1, bset2);
9085 __isl_give isl_basic_map *isl_basic_map_domain_product(
9086 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9088 isl_space *space_result = NULL;
9089 isl_basic_map *bmap;
9090 unsigned in1, in2, out, nparam, total, pos;
9091 struct isl_dim_map *dim_map1, *dim_map2;
9093 if (!bmap1 || !bmap2)
9094 goto error;
9096 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9097 isl_space_copy(bmap2->dim));
9099 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9100 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9101 out = isl_basic_map_dim(bmap1, isl_dim_out);
9102 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9104 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9105 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9106 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9107 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9108 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9109 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9110 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9111 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9112 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9113 isl_dim_map_div(dim_map1, bmap1, pos += out);
9114 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9116 bmap = isl_basic_map_alloc_space(space_result,
9117 bmap1->n_div + bmap2->n_div,
9118 bmap1->n_eq + bmap2->n_eq,
9119 bmap1->n_ineq + bmap2->n_ineq);
9120 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9121 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9122 bmap = isl_basic_map_simplify(bmap);
9123 return isl_basic_map_finalize(bmap);
9124 error:
9125 isl_basic_map_free(bmap1);
9126 isl_basic_map_free(bmap2);
9127 return NULL;
9130 __isl_give isl_basic_map *isl_basic_map_range_product(
9131 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9133 isl_space *dim_result = NULL;
9134 isl_basic_map *bmap;
9135 unsigned in, out1, out2, nparam, total, pos;
9136 struct isl_dim_map *dim_map1, *dim_map2;
9138 if (!bmap1 || !bmap2)
9139 goto error;
9141 if (!isl_space_match(bmap1->dim, isl_dim_param,
9142 bmap2->dim, isl_dim_param))
9143 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9144 "parameters don't match", goto error);
9146 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9147 isl_space_copy(bmap2->dim));
9149 in = isl_basic_map_dim(bmap1, isl_dim_in);
9150 out1 = isl_basic_map_n_out(bmap1);
9151 out2 = isl_basic_map_n_out(bmap2);
9152 nparam = isl_basic_map_n_param(bmap1);
9154 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9155 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9156 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9157 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9158 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9159 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9160 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9161 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9162 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9163 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9164 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9166 bmap = isl_basic_map_alloc_space(dim_result,
9167 bmap1->n_div + bmap2->n_div,
9168 bmap1->n_eq + bmap2->n_eq,
9169 bmap1->n_ineq + bmap2->n_ineq);
9170 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9171 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9172 bmap = isl_basic_map_simplify(bmap);
9173 return isl_basic_map_finalize(bmap);
9174 error:
9175 isl_basic_map_free(bmap1);
9176 isl_basic_map_free(bmap2);
9177 return NULL;
9180 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9181 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9183 isl_basic_map *prod;
9185 prod = isl_basic_map_range_product(bmap1, bmap2);
9186 prod = isl_basic_map_flatten_range(prod);
9187 return prod;
9190 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9191 __isl_take isl_map *map2,
9192 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9193 __isl_take isl_space *right),
9194 __isl_give isl_basic_map *(*basic_map_product)(
9195 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9197 unsigned flags = 0;
9198 struct isl_map *result;
9199 int i, j;
9201 if (!map1 || !map2)
9202 goto error;
9204 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9205 map2->dim, isl_dim_param), goto error);
9207 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9208 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9209 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9211 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9212 isl_space_copy(map2->dim)),
9213 map1->n * map2->n, flags);
9214 if (!result)
9215 goto error;
9216 for (i = 0; i < map1->n; ++i)
9217 for (j = 0; j < map2->n; ++j) {
9218 struct isl_basic_map *part;
9219 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9220 isl_basic_map_copy(map2->p[j]));
9221 if (isl_basic_map_is_empty(part))
9222 isl_basic_map_free(part);
9223 else
9224 result = isl_map_add_basic_map(result, part);
9225 if (!result)
9226 goto error;
9228 isl_map_free(map1);
9229 isl_map_free(map2);
9230 return result;
9231 error:
9232 isl_map_free(map1);
9233 isl_map_free(map2);
9234 return NULL;
9237 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9239 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9240 __isl_take isl_map *map2)
9242 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9245 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9246 __isl_take isl_map *map2)
9248 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9251 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9253 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9254 __isl_take isl_map *map2)
9256 isl_map *prod;
9258 prod = isl_map_product(map1, map2);
9259 prod = isl_map_flatten(prod);
9260 return prod;
9263 /* Given two set A and B, construct its Cartesian product A x B.
9265 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9267 return isl_map_range_product(set1, set2);
9270 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9271 __isl_take isl_set *set2)
9273 return isl_map_flat_range_product(set1, set2);
9276 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9278 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9279 __isl_take isl_map *map2)
9281 return map_product(map1, map2, &isl_space_domain_product,
9282 &isl_basic_map_domain_product);
9285 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9287 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9288 __isl_take isl_map *map2)
9290 return map_product(map1, map2, &isl_space_range_product,
9291 &isl_basic_map_range_product);
9294 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9295 __isl_take isl_map *map2)
9297 return isl_map_align_params_map_map_and(map1, map2,
9298 &map_domain_product_aligned);
9301 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9302 __isl_take isl_map *map2)
9304 return isl_map_align_params_map_map_and(map1, map2,
9305 &map_range_product_aligned);
9308 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9310 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9311 __isl_take isl_map *map2)
9313 isl_map *prod;
9315 prod = isl_map_domain_product(map1, map2);
9316 prod = isl_map_flatten_domain(prod);
9317 return prod;
9320 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9322 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9323 __isl_take isl_map *map2)
9325 isl_map *prod;
9327 prod = isl_map_range_product(map1, map2);
9328 prod = isl_map_flatten_range(prod);
9329 return prod;
9332 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9334 int i;
9335 uint32_t hash = isl_hash_init();
9336 unsigned total;
9338 if (!bmap)
9339 return 0;
9340 bmap = isl_basic_map_copy(bmap);
9341 bmap = isl_basic_map_normalize(bmap);
9342 if (!bmap)
9343 return 0;
9344 total = isl_basic_map_total_dim(bmap);
9345 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9346 for (i = 0; i < bmap->n_eq; ++i) {
9347 uint32_t c_hash;
9348 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9349 isl_hash_hash(hash, c_hash);
9351 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9352 for (i = 0; i < bmap->n_ineq; ++i) {
9353 uint32_t c_hash;
9354 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9355 isl_hash_hash(hash, c_hash);
9357 isl_hash_byte(hash, bmap->n_div & 0xFF);
9358 for (i = 0; i < bmap->n_div; ++i) {
9359 uint32_t c_hash;
9360 if (isl_int_is_zero(bmap->div[i][0]))
9361 continue;
9362 isl_hash_byte(hash, i & 0xFF);
9363 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9364 isl_hash_hash(hash, c_hash);
9366 isl_basic_map_free(bmap);
9367 return hash;
9370 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9372 return isl_basic_map_get_hash((isl_basic_map *)bset);
9375 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9377 int i;
9378 uint32_t hash;
9380 if (!map)
9381 return 0;
9382 map = isl_map_copy(map);
9383 map = isl_map_normalize(map);
9384 if (!map)
9385 return 0;
9387 hash = isl_hash_init();
9388 for (i = 0; i < map->n; ++i) {
9389 uint32_t bmap_hash;
9390 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9391 isl_hash_hash(hash, bmap_hash);
9394 isl_map_free(map);
9396 return hash;
9399 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9401 return isl_map_get_hash((isl_map *)set);
9404 /* Check if the value for dimension dim is completely determined
9405 * by the values of the other parameters and variables.
9406 * That is, check if dimension dim is involved in an equality.
9408 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9410 int i;
9411 unsigned nparam;
9413 if (!bset)
9414 return -1;
9415 nparam = isl_basic_set_n_param(bset);
9416 for (i = 0; i < bset->n_eq; ++i)
9417 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9418 return 1;
9419 return 0;
9422 /* Check if the value for dimension dim is completely determined
9423 * by the values of the other parameters and variables.
9424 * That is, check if dimension dim is involved in an equality
9425 * for each of the subsets.
9427 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9429 int i;
9431 if (!set)
9432 return -1;
9433 for (i = 0; i < set->n; ++i) {
9434 int unique;
9435 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9436 if (unique != 1)
9437 return unique;
9439 return 1;
9442 int isl_set_n_basic_set(__isl_keep isl_set *set)
9444 return set ? set->n : 0;
9447 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9448 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9450 int i;
9452 if (!map)
9453 return -1;
9455 for (i = 0; i < map->n; ++i)
9456 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9457 return -1;
9459 return 0;
9462 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9463 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9465 int i;
9467 if (!set)
9468 return -1;
9470 for (i = 0; i < set->n; ++i)
9471 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9472 return -1;
9474 return 0;
9477 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9479 isl_space *dim;
9481 if (!bset)
9482 return NULL;
9484 bset = isl_basic_set_cow(bset);
9485 if (!bset)
9486 return NULL;
9488 dim = isl_basic_set_get_space(bset);
9489 dim = isl_space_lift(dim, bset->n_div);
9490 if (!dim)
9491 goto error;
9492 isl_space_free(bset->dim);
9493 bset->dim = dim;
9494 bset->extra -= bset->n_div;
9495 bset->n_div = 0;
9497 bset = isl_basic_set_finalize(bset);
9499 return bset;
9500 error:
9501 isl_basic_set_free(bset);
9502 return NULL;
9505 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9507 int i;
9508 isl_space *dim;
9509 unsigned n_div;
9511 set = isl_set_align_divs(set);
9513 if (!set)
9514 return NULL;
9516 set = isl_set_cow(set);
9517 if (!set)
9518 return NULL;
9520 n_div = set->p[0]->n_div;
9521 dim = isl_set_get_space(set);
9522 dim = isl_space_lift(dim, n_div);
9523 if (!dim)
9524 goto error;
9525 isl_space_free(set->dim);
9526 set->dim = dim;
9528 for (i = 0; i < set->n; ++i) {
9529 set->p[i] = isl_basic_set_lift(set->p[i]);
9530 if (!set->p[i])
9531 goto error;
9534 return set;
9535 error:
9536 isl_set_free(set);
9537 return NULL;
9540 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9542 isl_space *dim;
9543 struct isl_basic_map *bmap;
9544 unsigned n_set;
9545 unsigned n_div;
9546 unsigned n_param;
9547 unsigned total;
9548 int i, k, l;
9550 set = isl_set_align_divs(set);
9552 if (!set)
9553 return NULL;
9555 dim = isl_set_get_space(set);
9556 if (set->n == 0 || set->p[0]->n_div == 0) {
9557 isl_set_free(set);
9558 return isl_map_identity(isl_space_map_from_set(dim));
9561 n_div = set->p[0]->n_div;
9562 dim = isl_space_map_from_set(dim);
9563 n_param = isl_space_dim(dim, isl_dim_param);
9564 n_set = isl_space_dim(dim, isl_dim_in);
9565 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9566 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9567 for (i = 0; i < n_set; ++i)
9568 bmap = var_equal(bmap, i);
9570 total = n_param + n_set + n_set + n_div;
9571 for (i = 0; i < n_div; ++i) {
9572 k = isl_basic_map_alloc_inequality(bmap);
9573 if (k < 0)
9574 goto error;
9575 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9576 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9577 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9578 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9579 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9580 set->p[0]->div[i][0]);
9582 l = isl_basic_map_alloc_inequality(bmap);
9583 if (l < 0)
9584 goto error;
9585 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9586 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9587 set->p[0]->div[i][0]);
9588 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9591 isl_set_free(set);
9592 bmap = isl_basic_map_simplify(bmap);
9593 bmap = isl_basic_map_finalize(bmap);
9594 return isl_map_from_basic_map(bmap);
9595 error:
9596 isl_set_free(set);
9597 isl_basic_map_free(bmap);
9598 return NULL;
9601 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9603 unsigned dim;
9604 int size = 0;
9606 if (!bset)
9607 return -1;
9609 dim = isl_basic_set_total_dim(bset);
9610 size += bset->n_eq * (1 + dim);
9611 size += bset->n_ineq * (1 + dim);
9612 size += bset->n_div * (2 + dim);
9614 return size;
9617 int isl_set_size(__isl_keep isl_set *set)
9619 int i;
9620 int size = 0;
9622 if (!set)
9623 return -1;
9625 for (i = 0; i < set->n; ++i)
9626 size += isl_basic_set_size(set->p[i]);
9628 return size;
9631 /* Check if there is any lower bound (if lower == 0) and/or upper
9632 * bound (if upper == 0) on the specified dim.
9634 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9635 enum isl_dim_type type, unsigned pos, int lower, int upper)
9637 int i;
9639 if (!bmap)
9640 return -1;
9642 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9644 pos += isl_basic_map_offset(bmap, type);
9646 for (i = 0; i < bmap->n_div; ++i) {
9647 if (isl_int_is_zero(bmap->div[i][0]))
9648 continue;
9649 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9650 return 1;
9653 for (i = 0; i < bmap->n_eq; ++i)
9654 if (!isl_int_is_zero(bmap->eq[i][pos]))
9655 return 1;
9657 for (i = 0; i < bmap->n_ineq; ++i) {
9658 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9659 if (sgn > 0)
9660 lower = 1;
9661 if (sgn < 0)
9662 upper = 1;
9665 return lower && upper;
9668 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9669 enum isl_dim_type type, unsigned pos)
9671 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9674 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9675 enum isl_dim_type type, unsigned pos)
9677 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9680 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9681 enum isl_dim_type type, unsigned pos)
9683 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9686 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9687 enum isl_dim_type type, unsigned pos)
9689 int i;
9691 if (!map)
9692 return -1;
9694 for (i = 0; i < map->n; ++i) {
9695 int bounded;
9696 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9697 if (bounded < 0 || !bounded)
9698 return bounded;
9701 return 1;
9704 /* Return 1 if the specified dim is involved in both an upper bound
9705 * and a lower bound.
9707 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9708 enum isl_dim_type type, unsigned pos)
9710 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9713 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9715 static int has_any_bound(__isl_keep isl_map *map,
9716 enum isl_dim_type type, unsigned pos,
9717 int (*fn)(__isl_keep isl_basic_map *bmap,
9718 enum isl_dim_type type, unsigned pos))
9720 int i;
9722 if (!map)
9723 return -1;
9725 for (i = 0; i < map->n; ++i) {
9726 int bounded;
9727 bounded = fn(map->p[i], type, pos);
9728 if (bounded < 0 || bounded)
9729 return bounded;
9732 return 0;
9735 /* Return 1 if the specified dim is involved in any lower bound.
9737 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9738 enum isl_dim_type type, unsigned pos)
9740 return has_any_bound(set, type, pos,
9741 &isl_basic_map_dim_has_lower_bound);
9744 /* Return 1 if the specified dim is involved in any upper bound.
9746 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9747 enum isl_dim_type type, unsigned pos)
9749 return has_any_bound(set, type, pos,
9750 &isl_basic_map_dim_has_upper_bound);
9753 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9755 static int has_bound(__isl_keep isl_map *map,
9756 enum isl_dim_type type, unsigned pos,
9757 int (*fn)(__isl_keep isl_basic_map *bmap,
9758 enum isl_dim_type type, unsigned pos))
9760 int i;
9762 if (!map)
9763 return -1;
9765 for (i = 0; i < map->n; ++i) {
9766 int bounded;
9767 bounded = fn(map->p[i], type, pos);
9768 if (bounded < 0 || !bounded)
9769 return bounded;
9772 return 1;
9775 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9777 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9778 enum isl_dim_type type, unsigned pos)
9780 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9783 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9785 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9786 enum isl_dim_type type, unsigned pos)
9788 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9791 /* For each of the "n" variables starting at "first", determine
9792 * the sign of the variable and put the results in the first "n"
9793 * elements of the array "signs".
9794 * Sign
9795 * 1 means that the variable is non-negative
9796 * -1 means that the variable is non-positive
9797 * 0 means the variable attains both positive and negative values.
9799 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9800 unsigned first, unsigned n, int *signs)
9802 isl_vec *bound = NULL;
9803 struct isl_tab *tab = NULL;
9804 struct isl_tab_undo *snap;
9805 int i;
9807 if (!bset || !signs)
9808 return -1;
9810 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9811 tab = isl_tab_from_basic_set(bset, 0);
9812 if (!bound || !tab)
9813 goto error;
9815 isl_seq_clr(bound->el, bound->size);
9816 isl_int_set_si(bound->el[0], -1);
9818 snap = isl_tab_snap(tab);
9819 for (i = 0; i < n; ++i) {
9820 int empty;
9822 isl_int_set_si(bound->el[1 + first + i], -1);
9823 if (isl_tab_add_ineq(tab, bound->el) < 0)
9824 goto error;
9825 empty = tab->empty;
9826 isl_int_set_si(bound->el[1 + first + i], 0);
9827 if (isl_tab_rollback(tab, snap) < 0)
9828 goto error;
9830 if (empty) {
9831 signs[i] = 1;
9832 continue;
9835 isl_int_set_si(bound->el[1 + first + i], 1);
9836 if (isl_tab_add_ineq(tab, bound->el) < 0)
9837 goto error;
9838 empty = tab->empty;
9839 isl_int_set_si(bound->el[1 + first + i], 0);
9840 if (isl_tab_rollback(tab, snap) < 0)
9841 goto error;
9843 signs[i] = empty ? -1 : 0;
9846 isl_tab_free(tab);
9847 isl_vec_free(bound);
9848 return 0;
9849 error:
9850 isl_tab_free(tab);
9851 isl_vec_free(bound);
9852 return -1;
9855 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9856 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9858 if (!bset || !signs)
9859 return -1;
9860 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9861 return -1);
9863 first += pos(bset->dim, type) - 1;
9864 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9867 /* Check if the given basic map is obviously single-valued.
9868 * In particular, for each output dimension, check that there is
9869 * an equality that defines the output dimension in terms of
9870 * earlier dimensions.
9872 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9874 int i, j;
9875 unsigned total;
9876 unsigned n_out;
9877 unsigned o_out;
9879 if (!bmap)
9880 return -1;
9882 total = 1 + isl_basic_map_total_dim(bmap);
9883 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9884 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9886 for (i = 0; i < n_out; ++i) {
9887 for (j = 0; j < bmap->n_eq; ++j) {
9888 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9889 continue;
9890 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9891 total - (o_out + i + 1)) == -1)
9892 break;
9894 if (j >= bmap->n_eq)
9895 return 0;
9898 return 1;
9901 /* Check if the given basic map is single-valued.
9902 * We simply compute
9904 * M \circ M^-1
9906 * and check if the result is a subset of the identity mapping.
9908 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9910 isl_space *space;
9911 isl_basic_map *test;
9912 isl_basic_map *id;
9913 int sv;
9915 sv = isl_basic_map_plain_is_single_valued(bmap);
9916 if (sv < 0 || sv)
9917 return sv;
9919 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9920 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9922 space = isl_basic_map_get_space(bmap);
9923 space = isl_space_map_from_set(isl_space_range(space));
9924 id = isl_basic_map_identity(space);
9926 sv = isl_basic_map_is_subset(test, id);
9928 isl_basic_map_free(test);
9929 isl_basic_map_free(id);
9931 return sv;
9934 /* Check if the given map is obviously single-valued.
9936 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9938 if (!map)
9939 return -1;
9940 if (map->n == 0)
9941 return 1;
9942 if (map->n >= 2)
9943 return 0;
9945 return isl_basic_map_plain_is_single_valued(map->p[0]);
9948 /* Check if the given map is single-valued.
9949 * We simply compute
9951 * M \circ M^-1
9953 * and check if the result is a subset of the identity mapping.
9955 int isl_map_is_single_valued(__isl_keep isl_map *map)
9957 isl_space *dim;
9958 isl_map *test;
9959 isl_map *id;
9960 int sv;
9962 sv = isl_map_plain_is_single_valued(map);
9963 if (sv < 0 || sv)
9964 return sv;
9966 test = isl_map_reverse(isl_map_copy(map));
9967 test = isl_map_apply_range(test, isl_map_copy(map));
9969 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9970 id = isl_map_identity(dim);
9972 sv = isl_map_is_subset(test, id);
9974 isl_map_free(test);
9975 isl_map_free(id);
9977 return sv;
9980 int isl_map_is_injective(__isl_keep isl_map *map)
9982 int in;
9984 map = isl_map_copy(map);
9985 map = isl_map_reverse(map);
9986 in = isl_map_is_single_valued(map);
9987 isl_map_free(map);
9989 return in;
9992 /* Check if the given map is obviously injective.
9994 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9996 int in;
9998 map = isl_map_copy(map);
9999 map = isl_map_reverse(map);
10000 in = isl_map_plain_is_single_valued(map);
10001 isl_map_free(map);
10003 return in;
10006 int isl_map_is_bijective(__isl_keep isl_map *map)
10008 int sv;
10010 sv = isl_map_is_single_valued(map);
10011 if (sv < 0 || !sv)
10012 return sv;
10014 return isl_map_is_injective(map);
10017 int isl_set_is_singleton(__isl_keep isl_set *set)
10019 return isl_map_is_single_valued((isl_map *)set);
10022 int isl_map_is_translation(__isl_keep isl_map *map)
10024 int ok;
10025 isl_set *delta;
10027 delta = isl_map_deltas(isl_map_copy(map));
10028 ok = isl_set_is_singleton(delta);
10029 isl_set_free(delta);
10031 return ok;
10034 static int unique(isl_int *p, unsigned pos, unsigned len)
10036 if (isl_seq_first_non_zero(p, pos) != -1)
10037 return 0;
10038 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10039 return 0;
10040 return 1;
10043 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10045 int i, j;
10046 unsigned nvar;
10047 unsigned ovar;
10049 if (!bset)
10050 return -1;
10052 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10053 return 0;
10055 nvar = isl_basic_set_dim(bset, isl_dim_set);
10056 ovar = isl_space_offset(bset->dim, isl_dim_set);
10057 for (j = 0; j < nvar; ++j) {
10058 int lower = 0, upper = 0;
10059 for (i = 0; i < bset->n_eq; ++i) {
10060 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10061 continue;
10062 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10063 return 0;
10064 break;
10066 if (i < bset->n_eq)
10067 continue;
10068 for (i = 0; i < bset->n_ineq; ++i) {
10069 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10070 continue;
10071 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10072 return 0;
10073 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10074 lower = 1;
10075 else
10076 upper = 1;
10078 if (!lower || !upper)
10079 return 0;
10082 return 1;
10085 int isl_set_is_box(__isl_keep isl_set *set)
10087 if (!set)
10088 return -1;
10089 if (set->n != 1)
10090 return 0;
10092 return isl_basic_set_is_box(set->p[0]);
10095 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10097 if (!bset)
10098 return -1;
10100 return isl_space_is_wrapping(bset->dim);
10103 int isl_set_is_wrapping(__isl_keep isl_set *set)
10105 if (!set)
10106 return -1;
10108 return isl_space_is_wrapping(set->dim);
10111 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10113 bmap = isl_basic_map_cow(bmap);
10114 if (!bmap)
10115 return NULL;
10117 bmap->dim = isl_space_wrap(bmap->dim);
10118 if (!bmap->dim)
10119 goto error;
10121 bmap = isl_basic_map_finalize(bmap);
10123 return (isl_basic_set *)bmap;
10124 error:
10125 isl_basic_map_free(bmap);
10126 return NULL;
10129 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10131 int i;
10133 map = isl_map_cow(map);
10134 if (!map)
10135 return NULL;
10137 for (i = 0; i < map->n; ++i) {
10138 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10139 if (!map->p[i])
10140 goto error;
10142 map->dim = isl_space_wrap(map->dim);
10143 if (!map->dim)
10144 goto error;
10146 return (isl_set *)map;
10147 error:
10148 isl_map_free(map);
10149 return NULL;
10152 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10154 bset = isl_basic_set_cow(bset);
10155 if (!bset)
10156 return NULL;
10158 bset->dim = isl_space_unwrap(bset->dim);
10159 if (!bset->dim)
10160 goto error;
10162 bset = isl_basic_set_finalize(bset);
10164 return (isl_basic_map *)bset;
10165 error:
10166 isl_basic_set_free(bset);
10167 return NULL;
10170 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10172 int i;
10174 if (!set)
10175 return NULL;
10177 if (!isl_set_is_wrapping(set))
10178 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10179 goto error);
10181 set = isl_set_cow(set);
10182 if (!set)
10183 return NULL;
10185 for (i = 0; i < set->n; ++i) {
10186 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10187 if (!set->p[i])
10188 goto error;
10191 set->dim = isl_space_unwrap(set->dim);
10192 if (!set->dim)
10193 goto error;
10195 return (isl_map *)set;
10196 error:
10197 isl_set_free(set);
10198 return NULL;
10201 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10202 enum isl_dim_type type)
10204 if (!bmap)
10205 return NULL;
10207 if (!isl_space_is_named_or_nested(bmap->dim, type))
10208 return bmap;
10210 bmap = isl_basic_map_cow(bmap);
10211 if (!bmap)
10212 return NULL;
10214 bmap->dim = isl_space_reset(bmap->dim, type);
10215 if (!bmap->dim)
10216 goto error;
10218 bmap = isl_basic_map_finalize(bmap);
10220 return bmap;
10221 error:
10222 isl_basic_map_free(bmap);
10223 return NULL;
10226 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10227 enum isl_dim_type type)
10229 int i;
10231 if (!map)
10232 return NULL;
10234 if (!isl_space_is_named_or_nested(map->dim, type))
10235 return map;
10237 map = isl_map_cow(map);
10238 if (!map)
10239 return NULL;
10241 for (i = 0; i < map->n; ++i) {
10242 map->p[i] = isl_basic_map_reset(map->p[i], type);
10243 if (!map->p[i])
10244 goto error;
10246 map->dim = isl_space_reset(map->dim, type);
10247 if (!map->dim)
10248 goto error;
10250 return map;
10251 error:
10252 isl_map_free(map);
10253 return NULL;
10256 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10258 if (!bmap)
10259 return NULL;
10261 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10262 return bmap;
10264 bmap = isl_basic_map_cow(bmap);
10265 if (!bmap)
10266 return NULL;
10268 bmap->dim = isl_space_flatten(bmap->dim);
10269 if (!bmap->dim)
10270 goto error;
10272 bmap = isl_basic_map_finalize(bmap);
10274 return bmap;
10275 error:
10276 isl_basic_map_free(bmap);
10277 return NULL;
10280 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10282 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10285 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10286 __isl_take isl_basic_map *bmap)
10288 if (!bmap)
10289 return NULL;
10291 if (!bmap->dim->nested[0])
10292 return bmap;
10294 bmap = isl_basic_map_cow(bmap);
10295 if (!bmap)
10296 return NULL;
10298 bmap->dim = isl_space_flatten_domain(bmap->dim);
10299 if (!bmap->dim)
10300 goto error;
10302 bmap = isl_basic_map_finalize(bmap);
10304 return bmap;
10305 error:
10306 isl_basic_map_free(bmap);
10307 return NULL;
10310 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10311 __isl_take isl_basic_map *bmap)
10313 if (!bmap)
10314 return NULL;
10316 if (!bmap->dim->nested[1])
10317 return bmap;
10319 bmap = isl_basic_map_cow(bmap);
10320 if (!bmap)
10321 return NULL;
10323 bmap->dim = isl_space_flatten_range(bmap->dim);
10324 if (!bmap->dim)
10325 goto error;
10327 bmap = isl_basic_map_finalize(bmap);
10329 return bmap;
10330 error:
10331 isl_basic_map_free(bmap);
10332 return NULL;
10335 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10337 int i;
10339 if (!map)
10340 return NULL;
10342 if (!map->dim->nested[0] && !map->dim->nested[1])
10343 return map;
10345 map = isl_map_cow(map);
10346 if (!map)
10347 return NULL;
10349 for (i = 0; i < map->n; ++i) {
10350 map->p[i] = isl_basic_map_flatten(map->p[i]);
10351 if (!map->p[i])
10352 goto error;
10354 map->dim = isl_space_flatten(map->dim);
10355 if (!map->dim)
10356 goto error;
10358 return map;
10359 error:
10360 isl_map_free(map);
10361 return NULL;
10364 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10366 return (isl_set *)isl_map_flatten((isl_map *)set);
10369 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10371 isl_space *dim, *flat_dim;
10372 isl_map *map;
10374 dim = isl_set_get_space(set);
10375 flat_dim = isl_space_flatten(isl_space_copy(dim));
10376 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10377 map = isl_map_intersect_domain(map, set);
10379 return map;
10382 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10384 int i;
10386 if (!map)
10387 return NULL;
10389 if (!map->dim->nested[0])
10390 return map;
10392 map = isl_map_cow(map);
10393 if (!map)
10394 return NULL;
10396 for (i = 0; i < map->n; ++i) {
10397 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10398 if (!map->p[i])
10399 goto error;
10401 map->dim = isl_space_flatten_domain(map->dim);
10402 if (!map->dim)
10403 goto error;
10405 return map;
10406 error:
10407 isl_map_free(map);
10408 return NULL;
10411 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10413 int i;
10415 if (!map)
10416 return NULL;
10418 if (!map->dim->nested[1])
10419 return map;
10421 map = isl_map_cow(map);
10422 if (!map)
10423 return NULL;
10425 for (i = 0; i < map->n; ++i) {
10426 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10427 if (!map->p[i])
10428 goto error;
10430 map->dim = isl_space_flatten_range(map->dim);
10431 if (!map->dim)
10432 goto error;
10434 return map;
10435 error:
10436 isl_map_free(map);
10437 return NULL;
10440 /* Reorder the dimensions of "bmap" according to the given dim_map
10441 * and set the dimension specification to "dim".
10443 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10444 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10446 isl_basic_map *res;
10447 unsigned flags;
10449 bmap = isl_basic_map_cow(bmap);
10450 if (!bmap || !dim || !dim_map)
10451 goto error;
10453 flags = bmap->flags;
10454 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10455 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10456 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10457 res = isl_basic_map_alloc_space(dim,
10458 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10459 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10460 if (res)
10461 res->flags = flags;
10462 res = isl_basic_map_finalize(res);
10463 return res;
10464 error:
10465 free(dim_map);
10466 isl_basic_map_free(bmap);
10467 isl_space_free(dim);
10468 return NULL;
10471 /* Reorder the dimensions of "map" according to given reordering.
10473 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10474 __isl_take isl_reordering *r)
10476 int i;
10477 struct isl_dim_map *dim_map;
10479 map = isl_map_cow(map);
10480 dim_map = isl_dim_map_from_reordering(r);
10481 if (!map || !r || !dim_map)
10482 goto error;
10484 for (i = 0; i < map->n; ++i) {
10485 struct isl_dim_map *dim_map_i;
10487 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10489 map->p[i] = isl_basic_map_realign(map->p[i],
10490 isl_space_copy(r->dim), dim_map_i);
10492 if (!map->p[i])
10493 goto error;
10496 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10498 isl_reordering_free(r);
10499 free(dim_map);
10500 return map;
10501 error:
10502 free(dim_map);
10503 isl_map_free(map);
10504 isl_reordering_free(r);
10505 return NULL;
10508 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10509 __isl_take isl_reordering *r)
10511 return (isl_set *)isl_map_realign((isl_map *)set, r);
10514 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10515 __isl_take isl_space *model)
10517 isl_ctx *ctx;
10519 if (!map || !model)
10520 goto error;
10522 ctx = isl_space_get_ctx(model);
10523 if (!isl_space_has_named_params(model))
10524 isl_die(ctx, isl_error_invalid,
10525 "model has unnamed parameters", goto error);
10526 if (!isl_space_has_named_params(map->dim))
10527 isl_die(ctx, isl_error_invalid,
10528 "relation has unnamed parameters", goto error);
10529 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10530 isl_reordering *exp;
10532 model = isl_space_drop_dims(model, isl_dim_in,
10533 0, isl_space_dim(model, isl_dim_in));
10534 model = isl_space_drop_dims(model, isl_dim_out,
10535 0, isl_space_dim(model, isl_dim_out));
10536 exp = isl_parameter_alignment_reordering(map->dim, model);
10537 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10538 map = isl_map_realign(map, exp);
10541 isl_space_free(model);
10542 return map;
10543 error:
10544 isl_space_free(model);
10545 isl_map_free(map);
10546 return NULL;
10549 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10550 __isl_take isl_space *model)
10552 return isl_map_align_params(set, model);
10555 /* Align the parameters of "bmap" to those of "model", introducing
10556 * additional parameters if needed.
10558 __isl_give isl_basic_map *isl_basic_map_align_params(
10559 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10561 isl_ctx *ctx;
10563 if (!bmap || !model)
10564 goto error;
10566 ctx = isl_space_get_ctx(model);
10567 if (!isl_space_has_named_params(model))
10568 isl_die(ctx, isl_error_invalid,
10569 "model has unnamed parameters", goto error);
10570 if (!isl_space_has_named_params(bmap->dim))
10571 isl_die(ctx, isl_error_invalid,
10572 "relation has unnamed parameters", goto error);
10573 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10574 isl_reordering *exp;
10575 struct isl_dim_map *dim_map;
10577 model = isl_space_drop_dims(model, isl_dim_in,
10578 0, isl_space_dim(model, isl_dim_in));
10579 model = isl_space_drop_dims(model, isl_dim_out,
10580 0, isl_space_dim(model, isl_dim_out));
10581 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10582 exp = isl_reordering_extend_space(exp,
10583 isl_basic_map_get_space(bmap));
10584 dim_map = isl_dim_map_from_reordering(exp);
10585 bmap = isl_basic_map_realign(bmap,
10586 exp ? isl_space_copy(exp->dim) : NULL,
10587 isl_dim_map_extend(dim_map, bmap));
10588 isl_reordering_free(exp);
10589 free(dim_map);
10592 isl_space_free(model);
10593 return bmap;
10594 error:
10595 isl_space_free(model);
10596 isl_basic_map_free(bmap);
10597 return NULL;
10600 /* Align the parameters of "bset" to those of "model", introducing
10601 * additional parameters if needed.
10603 __isl_give isl_basic_set *isl_basic_set_align_params(
10604 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10606 return isl_basic_map_align_params(bset, model);
10609 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10610 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10611 enum isl_dim_type c2, enum isl_dim_type c3,
10612 enum isl_dim_type c4, enum isl_dim_type c5)
10614 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10615 struct isl_mat *mat;
10616 int i, j, k;
10617 int pos;
10619 if (!bmap)
10620 return NULL;
10621 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10622 isl_basic_map_total_dim(bmap) + 1);
10623 if (!mat)
10624 return NULL;
10625 for (i = 0; i < bmap->n_eq; ++i)
10626 for (j = 0, pos = 0; j < 5; ++j) {
10627 int off = isl_basic_map_offset(bmap, c[j]);
10628 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10629 isl_int_set(mat->row[i][pos],
10630 bmap->eq[i][off + k]);
10631 ++pos;
10635 return mat;
10638 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10639 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10640 enum isl_dim_type c2, enum isl_dim_type c3,
10641 enum isl_dim_type c4, enum isl_dim_type c5)
10643 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10644 struct isl_mat *mat;
10645 int i, j, k;
10646 int pos;
10648 if (!bmap)
10649 return NULL;
10650 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10651 isl_basic_map_total_dim(bmap) + 1);
10652 if (!mat)
10653 return NULL;
10654 for (i = 0; i < bmap->n_ineq; ++i)
10655 for (j = 0, pos = 0; j < 5; ++j) {
10656 int off = isl_basic_map_offset(bmap, c[j]);
10657 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10658 isl_int_set(mat->row[i][pos],
10659 bmap->ineq[i][off + k]);
10660 ++pos;
10664 return mat;
10667 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10668 __isl_take isl_space *dim,
10669 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10670 enum isl_dim_type c2, enum isl_dim_type c3,
10671 enum isl_dim_type c4, enum isl_dim_type c5)
10673 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10674 isl_basic_map *bmap;
10675 unsigned total;
10676 unsigned extra;
10677 int i, j, k, l;
10678 int pos;
10680 if (!dim || !eq || !ineq)
10681 goto error;
10683 if (eq->n_col != ineq->n_col)
10684 isl_die(dim->ctx, isl_error_invalid,
10685 "equalities and inequalities matrices should have "
10686 "same number of columns", goto error);
10688 total = 1 + isl_space_dim(dim, isl_dim_all);
10690 if (eq->n_col < total)
10691 isl_die(dim->ctx, isl_error_invalid,
10692 "number of columns too small", goto error);
10694 extra = eq->n_col - total;
10696 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10697 eq->n_row, ineq->n_row);
10698 if (!bmap)
10699 goto error;
10700 for (i = 0; i < extra; ++i) {
10701 k = isl_basic_map_alloc_div(bmap);
10702 if (k < 0)
10703 goto error;
10704 isl_int_set_si(bmap->div[k][0], 0);
10706 for (i = 0; i < eq->n_row; ++i) {
10707 l = isl_basic_map_alloc_equality(bmap);
10708 if (l < 0)
10709 goto error;
10710 for (j = 0, pos = 0; j < 5; ++j) {
10711 int off = isl_basic_map_offset(bmap, c[j]);
10712 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10713 isl_int_set(bmap->eq[l][off + k],
10714 eq->row[i][pos]);
10715 ++pos;
10719 for (i = 0; i < ineq->n_row; ++i) {
10720 l = isl_basic_map_alloc_inequality(bmap);
10721 if (l < 0)
10722 goto error;
10723 for (j = 0, pos = 0; j < 5; ++j) {
10724 int off = isl_basic_map_offset(bmap, c[j]);
10725 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10726 isl_int_set(bmap->ineq[l][off + k],
10727 ineq->row[i][pos]);
10728 ++pos;
10733 isl_space_free(dim);
10734 isl_mat_free(eq);
10735 isl_mat_free(ineq);
10737 bmap = isl_basic_map_simplify(bmap);
10738 return isl_basic_map_finalize(bmap);
10739 error:
10740 isl_space_free(dim);
10741 isl_mat_free(eq);
10742 isl_mat_free(ineq);
10743 return NULL;
10746 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10747 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10748 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10750 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10751 c1, c2, c3, c4, isl_dim_in);
10754 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10755 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10756 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10758 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10759 c1, c2, c3, c4, isl_dim_in);
10762 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10763 __isl_take isl_space *dim,
10764 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10765 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10767 return (isl_basic_set*)
10768 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10769 c1, c2, c3, c4, isl_dim_in);
10772 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10774 if (!bmap)
10775 return -1;
10777 return isl_space_can_zip(bmap->dim);
10780 int isl_map_can_zip(__isl_keep isl_map *map)
10782 if (!map)
10783 return -1;
10785 return isl_space_can_zip(map->dim);
10788 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10789 * (A -> C) -> (B -> D).
10791 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10793 unsigned pos;
10794 unsigned n1;
10795 unsigned n2;
10797 if (!bmap)
10798 return NULL;
10800 if (!isl_basic_map_can_zip(bmap))
10801 isl_die(bmap->ctx, isl_error_invalid,
10802 "basic map cannot be zipped", goto error);
10803 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10804 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10805 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10806 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10807 bmap = isl_basic_map_cow(bmap);
10808 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10809 if (!bmap)
10810 return NULL;
10811 bmap->dim = isl_space_zip(bmap->dim);
10812 if (!bmap->dim)
10813 goto error;
10814 return bmap;
10815 error:
10816 isl_basic_map_free(bmap);
10817 return NULL;
10820 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10821 * (A -> C) -> (B -> D).
10823 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10825 int i;
10827 if (!map)
10828 return NULL;
10830 if (!isl_map_can_zip(map))
10831 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10832 goto error);
10834 map = isl_map_cow(map);
10835 if (!map)
10836 return NULL;
10838 for (i = 0; i < map->n; ++i) {
10839 map->p[i] = isl_basic_map_zip(map->p[i]);
10840 if (!map->p[i])
10841 goto error;
10844 map->dim = isl_space_zip(map->dim);
10845 if (!map->dim)
10846 goto error;
10848 return map;
10849 error:
10850 isl_map_free(map);
10851 return NULL;
10854 /* Can we apply isl_basic_map_curry to "bmap"?
10855 * That is, does it have a nested relation in its domain?
10857 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10859 if (!bmap)
10860 return -1;
10862 return isl_space_can_curry(bmap->dim);
10865 /* Can we apply isl_map_curry to "map"?
10866 * That is, does it have a nested relation in its domain?
10868 int isl_map_can_curry(__isl_keep isl_map *map)
10870 if (!map)
10871 return -1;
10873 return isl_space_can_curry(map->dim);
10876 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10877 * A -> (B -> C).
10879 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10882 if (!bmap)
10883 return NULL;
10885 if (!isl_basic_map_can_curry(bmap))
10886 isl_die(bmap->ctx, isl_error_invalid,
10887 "basic map cannot be curried", goto error);
10888 bmap = isl_basic_map_cow(bmap);
10889 if (!bmap)
10890 return NULL;
10891 bmap->dim = isl_space_curry(bmap->dim);
10892 if (!bmap->dim)
10893 goto error;
10894 return bmap;
10895 error:
10896 isl_basic_map_free(bmap);
10897 return NULL;
10900 /* Given a map (A -> B) -> C, return the corresponding map
10901 * A -> (B -> C).
10903 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10905 int i;
10907 if (!map)
10908 return NULL;
10910 if (!isl_map_can_curry(map))
10911 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10912 goto error);
10914 map = isl_map_cow(map);
10915 if (!map)
10916 return NULL;
10918 for (i = 0; i < map->n; ++i) {
10919 map->p[i] = isl_basic_map_curry(map->p[i]);
10920 if (!map->p[i])
10921 goto error;
10924 map->dim = isl_space_curry(map->dim);
10925 if (!map->dim)
10926 goto error;
10928 return map;
10929 error:
10930 isl_map_free(map);
10931 return NULL;
10934 /* Can we apply isl_basic_map_uncurry to "bmap"?
10935 * That is, does it have a nested relation in its domain?
10937 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10939 if (!bmap)
10940 return -1;
10942 return isl_space_can_uncurry(bmap->dim);
10945 /* Can we apply isl_map_uncurry to "map"?
10946 * That is, does it have a nested relation in its domain?
10948 int isl_map_can_uncurry(__isl_keep isl_map *map)
10950 if (!map)
10951 return -1;
10953 return isl_space_can_uncurry(map->dim);
10956 /* Given a basic map A -> (B -> C), return the corresponding basic map
10957 * (A -> B) -> C.
10959 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10962 if (!bmap)
10963 return NULL;
10965 if (!isl_basic_map_can_uncurry(bmap))
10966 isl_die(bmap->ctx, isl_error_invalid,
10967 "basic map cannot be uncurried",
10968 return isl_basic_map_free(bmap));
10969 bmap = isl_basic_map_cow(bmap);
10970 if (!bmap)
10971 return NULL;
10972 bmap->dim = isl_space_uncurry(bmap->dim);
10973 if (!bmap->dim)
10974 return isl_basic_map_free(bmap);
10975 return bmap;
10978 /* Given a map A -> (B -> C), return the corresponding map
10979 * (A -> B) -> C.
10981 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10983 int i;
10985 if (!map)
10986 return NULL;
10988 if (!isl_map_can_uncurry(map))
10989 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10990 return isl_map_free(map));
10992 map = isl_map_cow(map);
10993 if (!map)
10994 return NULL;
10996 for (i = 0; i < map->n; ++i) {
10997 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10998 if (!map->p[i])
10999 return isl_map_free(map);
11002 map->dim = isl_space_uncurry(map->dim);
11003 if (!map->dim)
11004 return isl_map_free(map);
11006 return map;
11009 /* Construct a basic map mapping the domain of the affine expression
11010 * to a one-dimensional range prescribed by the affine expression.
11012 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11014 int k;
11015 int pos;
11016 isl_local_space *ls;
11017 isl_basic_map *bmap;
11019 if (!aff)
11020 return NULL;
11022 ls = isl_aff_get_local_space(aff);
11023 bmap = isl_basic_map_from_local_space(ls);
11024 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11025 k = isl_basic_map_alloc_equality(bmap);
11026 if (k < 0)
11027 goto error;
11029 pos = isl_basic_map_offset(bmap, isl_dim_out);
11030 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11031 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11032 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11033 aff->v->size - (pos + 1));
11035 isl_aff_free(aff);
11036 bmap = isl_basic_map_finalize(bmap);
11037 return bmap;
11038 error:
11039 isl_aff_free(aff);
11040 isl_basic_map_free(bmap);
11041 return NULL;
11044 /* Construct a map mapping the domain of the affine expression
11045 * to a one-dimensional range prescribed by the affine expression.
11047 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11049 isl_basic_map *bmap;
11051 bmap = isl_basic_map_from_aff(aff);
11052 return isl_map_from_basic_map(bmap);
11055 /* Construct a basic map mapping the domain the multi-affine expression
11056 * to its range, with each dimension in the range equated to the
11057 * corresponding affine expression.
11059 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11060 __isl_take isl_multi_aff *maff)
11062 int i;
11063 isl_space *space;
11064 isl_basic_map *bmap;
11066 if (!maff)
11067 return NULL;
11069 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11070 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11071 "invalid space", return isl_multi_aff_free(maff));
11073 space = isl_space_domain(isl_multi_aff_get_space(maff));
11074 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11076 for (i = 0; i < maff->n; ++i) {
11077 isl_aff *aff;
11078 isl_basic_map *bmap_i;
11080 aff = isl_aff_copy(maff->p[i]);
11081 bmap_i = isl_basic_map_from_aff(aff);
11083 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11086 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11088 isl_multi_aff_free(maff);
11089 return bmap;
11092 /* Construct a map mapping the domain the multi-affine expression
11093 * to its range, with each dimension in the range equated to the
11094 * corresponding affine expression.
11096 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11098 isl_basic_map *bmap;
11100 bmap = isl_basic_map_from_multi_aff(maff);
11101 return isl_map_from_basic_map(bmap);
11104 /* Construct a basic map mapping a domain in the given space to
11105 * to an n-dimensional range, with n the number of elements in the list,
11106 * where each coordinate in the range is prescribed by the
11107 * corresponding affine expression.
11108 * The domains of all affine expressions in the list are assumed to match
11109 * domain_dim.
11111 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11112 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11114 int i;
11115 isl_space *dim;
11116 isl_basic_map *bmap;
11118 if (!list)
11119 return NULL;
11121 dim = isl_space_from_domain(domain_dim);
11122 bmap = isl_basic_map_universe(dim);
11124 for (i = 0; i < list->n; ++i) {
11125 isl_aff *aff;
11126 isl_basic_map *bmap_i;
11128 aff = isl_aff_copy(list->p[i]);
11129 bmap_i = isl_basic_map_from_aff(aff);
11131 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11134 isl_aff_list_free(list);
11135 return bmap;
11138 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11139 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11141 return isl_map_equate(set, type1, pos1, type2, pos2);
11144 /* Construct a basic map where the given dimensions are equal to each other.
11146 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11147 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11149 isl_basic_map *bmap = NULL;
11150 int i;
11152 if (!space)
11153 return NULL;
11155 if (pos1 >= isl_space_dim(space, type1))
11156 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11157 "index out of bounds", goto error);
11158 if (pos2 >= isl_space_dim(space, type2))
11159 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11160 "index out of bounds", goto error);
11162 if (type1 == type2 && pos1 == pos2)
11163 return isl_basic_map_universe(space);
11165 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11166 i = isl_basic_map_alloc_equality(bmap);
11167 if (i < 0)
11168 goto error;
11169 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11170 pos1 += isl_basic_map_offset(bmap, type1);
11171 pos2 += isl_basic_map_offset(bmap, type2);
11172 isl_int_set_si(bmap->eq[i][pos1], -1);
11173 isl_int_set_si(bmap->eq[i][pos2], 1);
11174 bmap = isl_basic_map_finalize(bmap);
11175 isl_space_free(space);
11176 return bmap;
11177 error:
11178 isl_space_free(space);
11179 isl_basic_map_free(bmap);
11180 return NULL;
11183 /* Add a constraint imposing that the given two dimensions are equal.
11185 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11186 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11188 isl_basic_map *eq;
11190 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11192 bmap = isl_basic_map_intersect(bmap, eq);
11194 return bmap;
11197 /* Add a constraint imposing that the given two dimensions are equal.
11199 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11200 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11202 isl_basic_map *bmap;
11204 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11206 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11208 return map;
11211 /* Add a constraint imposing that the given two dimensions have opposite values.
11213 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11214 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11216 isl_basic_map *bmap = NULL;
11217 int i;
11219 if (!map)
11220 return NULL;
11222 if (pos1 >= isl_map_dim(map, type1))
11223 isl_die(map->ctx, isl_error_invalid,
11224 "index out of bounds", goto error);
11225 if (pos2 >= isl_map_dim(map, type2))
11226 isl_die(map->ctx, isl_error_invalid,
11227 "index out of bounds", goto error);
11229 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11230 i = isl_basic_map_alloc_equality(bmap);
11231 if (i < 0)
11232 goto error;
11233 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11234 pos1 += isl_basic_map_offset(bmap, type1);
11235 pos2 += isl_basic_map_offset(bmap, type2);
11236 isl_int_set_si(bmap->eq[i][pos1], 1);
11237 isl_int_set_si(bmap->eq[i][pos2], 1);
11238 bmap = isl_basic_map_finalize(bmap);
11240 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11242 return map;
11243 error:
11244 isl_basic_map_free(bmap);
11245 isl_map_free(map);
11246 return NULL;
11249 /* Add a constraint imposing that the value of the first dimension is
11250 * greater than or equal to that of the second.
11252 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11253 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11255 isl_constraint *c;
11256 isl_local_space *ls;
11258 if (!bmap)
11259 return NULL;
11261 if (pos1 >= isl_basic_map_dim(bmap, type1))
11262 isl_die(bmap->ctx, isl_error_invalid,
11263 "index out of bounds", return isl_basic_map_free(bmap));
11264 if (pos2 >= isl_basic_map_dim(bmap, type2))
11265 isl_die(bmap->ctx, isl_error_invalid,
11266 "index out of bounds", return isl_basic_map_free(bmap));
11268 if (type1 == type2 && pos1 == pos2)
11269 return bmap;
11271 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11272 c = isl_inequality_alloc(ls);
11273 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11274 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11275 bmap = isl_basic_map_add_constraint(bmap, c);
11277 return bmap;
11280 /* Construct a basic map where the value of the first dimension is
11281 * greater than that of the second.
11283 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11284 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11286 isl_basic_map *bmap = NULL;
11287 int i;
11289 if (!space)
11290 return NULL;
11292 if (pos1 >= isl_space_dim(space, type1))
11293 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11294 "index out of bounds", goto error);
11295 if (pos2 >= isl_space_dim(space, type2))
11296 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11297 "index out of bounds", goto error);
11299 if (type1 == type2 && pos1 == pos2)
11300 return isl_basic_map_empty(space);
11302 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11303 i = isl_basic_map_alloc_inequality(bmap);
11304 if (i < 0)
11305 goto error;
11306 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11307 pos1 += isl_basic_map_offset(bmap, type1);
11308 pos2 += isl_basic_map_offset(bmap, type2);
11309 isl_int_set_si(bmap->ineq[i][pos1], 1);
11310 isl_int_set_si(bmap->ineq[i][pos2], -1);
11311 isl_int_set_si(bmap->ineq[i][0], -1);
11312 bmap = isl_basic_map_finalize(bmap);
11314 return bmap;
11315 error:
11316 isl_space_free(space);
11317 isl_basic_map_free(bmap);
11318 return NULL;
11321 /* Add a constraint imposing that the value of the first dimension is
11322 * greater than that of the second.
11324 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11325 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11327 isl_basic_map *gt;
11329 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11331 bmap = isl_basic_map_intersect(bmap, gt);
11333 return bmap;
11336 /* Add a constraint imposing that the value of the first dimension is
11337 * greater than that of the second.
11339 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11342 isl_basic_map *bmap;
11344 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11346 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11348 return map;
11351 /* Add a constraint imposing that the value of the first dimension is
11352 * smaller than that of the second.
11354 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11355 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11357 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11360 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11361 int pos)
11363 isl_aff *div;
11364 isl_local_space *ls;
11366 if (!bmap)
11367 return NULL;
11369 if (!isl_basic_map_divs_known(bmap))
11370 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11371 "some divs are unknown", return NULL);
11373 ls = isl_basic_map_get_local_space(bmap);
11374 div = isl_local_space_get_div(ls, pos);
11375 isl_local_space_free(ls);
11377 return div;
11380 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11381 int pos)
11383 return isl_basic_map_get_div(bset, pos);
11386 /* Plug in "subs" for dimension "type", "pos" of "bset".
11388 * Let i be the dimension to replace and let "subs" be of the form
11390 * f/d
11392 * Any integer division with a non-zero coefficient for i,
11394 * floor((a i + g)/m)
11396 * is replaced by
11398 * floor((a f + d g)/(m d))
11400 * Constraints of the form
11402 * a i + g
11404 * are replaced by
11406 * a f + d g
11408 * We currently require that "subs" is an integral expression.
11409 * Handling rational expressions may require us to add stride constraints
11410 * as we do in isl_basic_set_preimage_multi_aff.
11412 __isl_give isl_basic_set *isl_basic_set_substitute(
11413 __isl_take isl_basic_set *bset,
11414 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11416 int i;
11417 isl_int v;
11418 isl_ctx *ctx;
11420 if (bset && isl_basic_set_plain_is_empty(bset))
11421 return bset;
11423 bset = isl_basic_set_cow(bset);
11424 if (!bset || !subs)
11425 goto error;
11427 ctx = isl_basic_set_get_ctx(bset);
11428 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11429 isl_die(ctx, isl_error_invalid,
11430 "spaces don't match", goto error);
11431 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11432 isl_die(ctx, isl_error_unsupported,
11433 "cannot handle divs yet", goto error);
11434 if (!isl_int_is_one(subs->v->el[0]))
11435 isl_die(ctx, isl_error_invalid,
11436 "can only substitute integer expressions", goto error);
11438 pos += isl_basic_set_offset(bset, type);
11440 isl_int_init(v);
11442 for (i = 0; i < bset->n_eq; ++i) {
11443 if (isl_int_is_zero(bset->eq[i][pos]))
11444 continue;
11445 isl_int_set(v, bset->eq[i][pos]);
11446 isl_int_set_si(bset->eq[i][pos], 0);
11447 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11448 v, subs->v->el + 1, subs->v->size - 1);
11451 for (i = 0; i < bset->n_ineq; ++i) {
11452 if (isl_int_is_zero(bset->ineq[i][pos]))
11453 continue;
11454 isl_int_set(v, bset->ineq[i][pos]);
11455 isl_int_set_si(bset->ineq[i][pos], 0);
11456 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11457 v, subs->v->el + 1, subs->v->size - 1);
11460 for (i = 0; i < bset->n_div; ++i) {
11461 if (isl_int_is_zero(bset->div[i][1 + pos]))
11462 continue;
11463 isl_int_set(v, bset->div[i][1 + pos]);
11464 isl_int_set_si(bset->div[i][1 + pos], 0);
11465 isl_seq_combine(bset->div[i] + 1,
11466 subs->v->el[0], bset->div[i] + 1,
11467 v, subs->v->el + 1, subs->v->size - 1);
11468 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11471 isl_int_clear(v);
11473 bset = isl_basic_set_simplify(bset);
11474 return isl_basic_set_finalize(bset);
11475 error:
11476 isl_basic_set_free(bset);
11477 return NULL;
11480 /* Plug in "subs" for dimension "type", "pos" of "set".
11482 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11483 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11485 int i;
11487 if (set && isl_set_plain_is_empty(set))
11488 return set;
11490 set = isl_set_cow(set);
11491 if (!set || !subs)
11492 goto error;
11494 for (i = set->n - 1; i >= 0; --i) {
11495 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11496 if (remove_if_empty(set, i) < 0)
11497 goto error;
11500 return set;
11501 error:
11502 isl_set_free(set);
11503 return NULL;
11506 /* Check if the range of "ma" is compatible with the domain or range
11507 * (depending on "type") of "bmap".
11508 * Return -1 if anything is wrong.
11510 static int check_basic_map_compatible_range_multi_aff(
11511 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11512 __isl_keep isl_multi_aff *ma)
11514 int m;
11515 isl_space *ma_space;
11517 ma_space = isl_multi_aff_get_space(ma);
11518 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11519 isl_space_free(ma_space);
11520 if (m >= 0 && !m)
11521 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11522 "spaces don't match", return -1);
11523 return m;
11526 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11527 * coefficients before the transformed range of dimensions,
11528 * the "n_after" coefficients after the transformed range of dimensions
11529 * and the coefficients of the other divs in "bmap".
11531 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11532 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11534 int i;
11535 int n_param;
11536 int n_set;
11537 isl_local_space *ls;
11539 if (n_div == 0)
11540 return 0;
11542 ls = isl_aff_get_domain_local_space(ma->p[0]);
11543 if (!ls)
11544 return -1;
11546 n_param = isl_local_space_dim(ls, isl_dim_param);
11547 n_set = isl_local_space_dim(ls, isl_dim_set);
11548 for (i = 0; i < n_div; ++i) {
11549 int o_bmap = 0, o_ls = 0;
11551 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11552 o_bmap += 1 + 1 + n_param;
11553 o_ls += 1 + 1 + n_param;
11554 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11555 o_bmap += n_before;
11556 isl_seq_cpy(bmap->div[i] + o_bmap,
11557 ls->div->row[i] + o_ls, n_set);
11558 o_bmap += n_set;
11559 o_ls += n_set;
11560 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11561 o_bmap += n_after;
11562 isl_seq_cpy(bmap->div[i] + o_bmap,
11563 ls->div->row[i] + o_ls, n_div);
11564 o_bmap += n_div;
11565 o_ls += n_div;
11566 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11567 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11568 goto error;
11571 isl_local_space_free(ls);
11572 return 0;
11573 error:
11574 isl_local_space_free(ls);
11575 return -1;
11578 /* How many stride constraints does "ma" enforce?
11579 * That is, how many of the affine expressions have a denominator
11580 * different from one?
11582 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11584 int i;
11585 int strides = 0;
11587 for (i = 0; i < ma->n; ++i)
11588 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11589 strides++;
11591 return strides;
11594 /* For each affine expression in ma of the form
11596 * x_i = (f_i y + h_i)/m_i
11598 * with m_i different from one, add a constraint to "bmap"
11599 * of the form
11601 * f_i y + h_i = m_i alpha_i
11603 * with alpha_i an additional existentially quantified variable.
11605 static __isl_give isl_basic_map *add_ma_strides(
11606 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11607 int n_before, int n_after)
11609 int i, k;
11610 int div;
11611 int total;
11612 int n_param;
11613 int n_in;
11614 int n_div;
11616 total = isl_basic_map_total_dim(bmap);
11617 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11618 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11619 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11620 for (i = 0; i < ma->n; ++i) {
11621 int o_bmap = 0, o_ma = 1;
11623 if (isl_int_is_one(ma->p[i]->v->el[0]))
11624 continue;
11625 div = isl_basic_map_alloc_div(bmap);
11626 k = isl_basic_map_alloc_equality(bmap);
11627 if (div < 0 || k < 0)
11628 goto error;
11629 isl_int_set_si(bmap->div[div][0], 0);
11630 isl_seq_cpy(bmap->eq[k] + o_bmap,
11631 ma->p[i]->v->el + o_ma, 1 + n_param);
11632 o_bmap += 1 + n_param;
11633 o_ma += 1 + n_param;
11634 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11635 o_bmap += n_before;
11636 isl_seq_cpy(bmap->eq[k] + o_bmap,
11637 ma->p[i]->v->el + o_ma, n_in);
11638 o_bmap += n_in;
11639 o_ma += n_in;
11640 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11641 o_bmap += n_after;
11642 isl_seq_cpy(bmap->eq[k] + o_bmap,
11643 ma->p[i]->v->el + o_ma, n_div);
11644 o_bmap += n_div;
11645 o_ma += n_div;
11646 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11647 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11648 total++;
11651 return bmap;
11652 error:
11653 isl_basic_map_free(bmap);
11654 return NULL;
11657 /* Replace the domain or range space (depending on "type) of "space" by "set".
11659 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11660 enum isl_dim_type type, __isl_take isl_space *set)
11662 if (type == isl_dim_in) {
11663 space = isl_space_range(space);
11664 space = isl_space_map_from_domain_and_range(set, space);
11665 } else {
11666 space = isl_space_domain(space);
11667 space = isl_space_map_from_domain_and_range(space, set);
11670 return space;
11673 /* Compute the preimage of the domain or range (depending on "type")
11674 * of "bmap" under the function represented by "ma".
11675 * In other words, plug in "ma" in the domain or range of "bmap".
11676 * The result is a basic map that lives in the same space as "bmap"
11677 * except that the domain or range has been replaced by
11678 * the domain space of "ma".
11680 * If bmap is represented by
11682 * A(p) + S u + B x + T v + C(divs) >= 0,
11684 * where u and x are input and output dimensions if type == isl_dim_out
11685 * while x and v are input and output dimensions if type == isl_dim_in,
11686 * and ma is represented by
11688 * x = D(p) + F(y) + G(divs')
11690 * then the result is
11692 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11694 * The divs in the input set are similarly adjusted.
11695 * In particular
11697 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11699 * becomes
11701 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11702 * B_i G(divs') + c_i(divs))/n_i)
11704 * If bmap is not a rational map and if F(y) involves any denominators
11706 * x_i = (f_i y + h_i)/m_i
11708 * then additional constraints are added to ensure that we only
11709 * map back integer points. That is we enforce
11711 * f_i y + h_i = m_i alpha_i
11713 * with alpha_i an additional existentially quantified variable.
11715 * We first copy over the divs from "ma".
11716 * Then we add the modified constraints and divs from "bmap".
11717 * Finally, we add the stride constraints, if needed.
11719 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11720 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11721 __isl_take isl_multi_aff *ma)
11723 int i, k;
11724 isl_space *space;
11725 isl_basic_map *res = NULL;
11726 int n_before, n_after, n_div_bmap, n_div_ma;
11727 isl_int f, c1, c2, g;
11728 int rational, strides;
11730 isl_int_init(f);
11731 isl_int_init(c1);
11732 isl_int_init(c2);
11733 isl_int_init(g);
11735 ma = isl_multi_aff_align_divs(ma);
11736 if (!bmap || !ma)
11737 goto error;
11738 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11739 goto error;
11741 if (type == isl_dim_in) {
11742 n_before = 0;
11743 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11744 } else {
11745 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11746 n_after = 0;
11748 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11749 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11751 space = isl_multi_aff_get_domain_space(ma);
11752 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11753 rational = isl_basic_map_is_rational(bmap);
11754 strides = rational ? 0 : multi_aff_strides(ma);
11755 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11756 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11757 if (rational)
11758 res = isl_basic_map_set_rational(res);
11760 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11761 if (isl_basic_map_alloc_div(res) < 0)
11762 goto error;
11764 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11765 goto error;
11767 for (i = 0; i < bmap->n_eq; ++i) {
11768 k = isl_basic_map_alloc_equality(res);
11769 if (k < 0)
11770 goto error;
11771 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11772 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11775 for (i = 0; i < bmap->n_ineq; ++i) {
11776 k = isl_basic_map_alloc_inequality(res);
11777 if (k < 0)
11778 goto error;
11779 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11780 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11783 for (i = 0; i < bmap->n_div; ++i) {
11784 if (isl_int_is_zero(bmap->div[i][0])) {
11785 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11786 continue;
11788 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11789 n_before, n_after, n_div_ma, n_div_bmap,
11790 f, c1, c2, g, 1);
11793 if (strides)
11794 res = add_ma_strides(res, ma, n_before, n_after);
11796 isl_int_clear(f);
11797 isl_int_clear(c1);
11798 isl_int_clear(c2);
11799 isl_int_clear(g);
11800 isl_basic_map_free(bmap);
11801 isl_multi_aff_free(ma);
11802 res = isl_basic_set_simplify(res);
11803 return isl_basic_map_finalize(res);
11804 error:
11805 isl_int_clear(f);
11806 isl_int_clear(c1);
11807 isl_int_clear(c2);
11808 isl_int_clear(g);
11809 isl_basic_map_free(bmap);
11810 isl_multi_aff_free(ma);
11811 isl_basic_map_free(res);
11812 return NULL;
11815 /* Compute the preimage of "bset" under the function represented by "ma".
11816 * In other words, plug in "ma" in "bset". The result is a basic set
11817 * that lives in the domain space of "ma".
11819 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11820 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11822 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11825 /* Check if the range of "ma" is compatible with the domain or range
11826 * (depending on "type") of "map".
11827 * Return -1 if anything is wrong.
11829 static int check_map_compatible_range_multi_aff(
11830 __isl_keep isl_map *map, enum isl_dim_type type,
11831 __isl_keep isl_multi_aff *ma)
11833 int m;
11834 isl_space *ma_space;
11836 ma_space = isl_multi_aff_get_space(ma);
11837 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11838 isl_space_free(ma_space);
11839 if (m >= 0 && !m)
11840 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11841 "spaces don't match", return -1);
11842 return m;
11845 /* Compute the preimage of the domain or range (depending on "type")
11846 * of "map" under the function represented by "ma".
11847 * In other words, plug in "ma" in the domain or range of "map".
11848 * The result is a map that lives in the same space as "map"
11849 * except that the domain or range has been replaced by
11850 * the domain space of "ma".
11852 * The parameters are assumed to have been aligned.
11854 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11855 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11857 int i;
11858 isl_space *space;
11860 map = isl_map_cow(map);
11861 ma = isl_multi_aff_align_divs(ma);
11862 if (!map || !ma)
11863 goto error;
11864 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11865 goto error;
11867 for (i = 0; i < map->n; ++i) {
11868 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11869 isl_multi_aff_copy(ma));
11870 if (!map->p[i])
11871 goto error;
11874 space = isl_multi_aff_get_domain_space(ma);
11875 space = isl_space_set(isl_map_get_space(map), type, space);
11877 isl_space_free(map->dim);
11878 map->dim = space;
11879 if (!map->dim)
11880 goto error;
11882 isl_multi_aff_free(ma);
11883 if (map->n > 1)
11884 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11885 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11886 return map;
11887 error:
11888 isl_multi_aff_free(ma);
11889 isl_map_free(map);
11890 return NULL;
11893 /* Compute the preimage of the domain or range (depending on "type")
11894 * of "map" under the function represented by "ma".
11895 * In other words, plug in "ma" in the domain or range of "map".
11896 * The result is a map that lives in the same space as "map"
11897 * except that the domain or range has been replaced by
11898 * the domain space of "ma".
11900 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11901 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11903 if (!map || !ma)
11904 goto error;
11906 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11907 return map_preimage_multi_aff(map, type, ma);
11909 if (!isl_space_has_named_params(map->dim) ||
11910 !isl_space_has_named_params(ma->space))
11911 isl_die(map->ctx, isl_error_invalid,
11912 "unaligned unnamed parameters", goto error);
11913 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11914 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11916 return map_preimage_multi_aff(map, type, ma);
11917 error:
11918 isl_multi_aff_free(ma);
11919 return isl_map_free(map);
11922 /* Compute the preimage of "set" under the function represented by "ma".
11923 * In other words, plug in "ma" "set". The result is a set
11924 * that lives in the domain space of "ma".
11926 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11927 __isl_take isl_multi_aff *ma)
11929 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11932 /* Compute the preimage of the domain of "map" under the function
11933 * represented by "ma".
11934 * In other words, plug in "ma" in the domain of "map".
11935 * The result is a map that lives in the same space as "map"
11936 * except that the domain has been replaced by the domain space of "ma".
11938 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11939 __isl_take isl_multi_aff *ma)
11941 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11944 /* Compute the preimage of "set" under the function represented by "pma".
11945 * In other words, plug in "pma" in "set. The result is a set
11946 * that lives in the domain space of "pma".
11948 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11949 __isl_take isl_pw_multi_aff *pma)
11951 int i;
11952 isl_set *res;
11954 if (!pma)
11955 goto error;
11957 if (pma->n == 0) {
11958 isl_pw_multi_aff_free(pma);
11959 res = isl_set_empty(isl_set_get_space(set));
11960 isl_set_free(set);
11961 return res;
11964 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11965 isl_multi_aff_copy(pma->p[0].maff));
11966 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11968 for (i = 1; i < pma->n; ++i) {
11969 isl_set *res_i;
11971 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11972 isl_multi_aff_copy(pma->p[i].maff));
11973 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11974 res = isl_set_union(res, res_i);
11977 isl_pw_multi_aff_free(pma);
11978 isl_set_free(set);
11979 return res;
11980 error:
11981 isl_pw_multi_aff_free(pma);
11982 isl_set_free(set);
11983 return NULL;
11986 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11987 __isl_take isl_pw_multi_aff *pma)
11989 if (!set || !pma)
11990 goto error;
11992 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11993 return set_preimage_pw_multi_aff(set, pma);
11995 if (!isl_space_has_named_params(set->dim) ||
11996 !isl_space_has_named_params(pma->dim))
11997 isl_die(set->ctx, isl_error_invalid,
11998 "unaligned unnamed parameters", goto error);
11999 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
12000 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
12002 return set_preimage_pw_multi_aff(set, pma);
12003 error:
12004 isl_pw_multi_aff_free(pma);
12005 return isl_set_free(set);