isl_ast_expr_from_*: use isl_val
[isl.git] / isl_map.c
blob164e075d142f0a31d392b267ae276e609d8a735c
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.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_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
38 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
40 switch (type) {
41 case isl_dim_param: return dim->nparam;
42 case isl_dim_in: return dim->n_in;
43 case isl_dim_out: return dim->n_out;
44 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
45 default: return 0;
49 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
51 switch (type) {
52 case isl_dim_param: return 1;
53 case isl_dim_in: return 1 + dim->nparam;
54 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 default: return 0;
59 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
60 enum isl_dim_type type)
62 if (!bmap)
63 return 0;
64 switch (type) {
65 case isl_dim_cst: return 1;
66 case isl_dim_param:
67 case isl_dim_in:
68 case isl_dim_out: return isl_space_dim(bmap->dim, type);
69 case isl_dim_div: return bmap->n_div;
70 case isl_dim_all: return isl_basic_map_total_dim(bmap);
71 default: return 0;
75 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
77 return map ? n(map->dim, type) : 0;
80 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
82 return set ? n(set->dim, type) : 0;
85 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
86 enum isl_dim_type type)
88 isl_space *dim = bmap->dim;
89 switch (type) {
90 case isl_dim_cst: return 0;
91 case isl_dim_param: return 1;
92 case isl_dim_in: return 1 + dim->nparam;
93 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
94 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
95 default: return 0;
99 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
100 enum isl_dim_type type)
102 return isl_basic_map_offset(bset, type);
105 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
107 return pos(map->dim, type);
110 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
111 enum isl_dim_type type)
113 return isl_basic_map_dim(bset, type);
116 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
118 return isl_basic_set_dim(bset, isl_dim_set);
121 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
123 return isl_basic_set_dim(bset, isl_dim_param);
126 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
128 if (!bset)
129 return 0;
130 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
133 unsigned isl_set_n_dim(__isl_keep isl_set *set)
135 return isl_set_dim(set, isl_dim_set);
138 unsigned isl_set_n_param(__isl_keep isl_set *set)
140 return isl_set_dim(set, isl_dim_param);
143 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
145 return bmap ? bmap->dim->n_in : 0;
148 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->n_out : 0;
153 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
155 return bmap ? bmap->dim->nparam : 0;
158 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
160 return bmap ? bmap->n_div : 0;
163 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
165 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
168 unsigned isl_map_n_in(const struct isl_map *map)
170 return map ? map->dim->n_in : 0;
173 unsigned isl_map_n_out(const struct isl_map *map)
175 return map ? map->dim->n_out : 0;
178 unsigned isl_map_n_param(const struct isl_map *map)
180 return map ? map->dim->nparam : 0;
183 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
185 int m;
186 if (!map || !set)
187 return -1;
188 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
189 if (m < 0 || !m)
190 return m;
191 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
194 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
195 struct isl_basic_set *bset)
197 int m;
198 if (!bmap || !bset)
199 return -1;
200 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
201 if (m < 0 || !m)
202 return m;
203 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
206 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
208 int m;
209 if (!map || !set)
210 return -1;
211 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
212 if (m < 0 || !m)
213 return m;
214 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
217 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
218 struct isl_basic_set *bset)
220 int m;
221 if (!bmap || !bset)
222 return -1;
223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
224 if (m < 0 || !m)
225 return m;
226 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
229 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
231 return bmap ? bmap->ctx : NULL;
234 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
236 return bset ? bset->ctx : NULL;
239 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
241 return map ? map->ctx : NULL;
244 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
246 return set ? set->ctx : NULL;
249 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return NULL;
253 return isl_space_copy(bmap->dim);
256 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
258 if (!bset)
259 return NULL;
260 return isl_space_copy(bset->dim);
263 /* Extract the divs in "bmap" as a matrix.
265 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
267 int i;
268 isl_ctx *ctx;
269 isl_mat *div;
270 unsigned total;
271 unsigned cols;
273 if (!bmap)
274 return NULL;
276 ctx = isl_basic_map_get_ctx(bmap);
277 total = isl_space_dim(bmap->dim, isl_dim_all);
278 cols = 1 + 1 + total + bmap->n_div;
279 div = isl_mat_alloc(ctx, bmap->n_div, cols);
280 if (!div)
281 return NULL;
283 for (i = 0; i < bmap->n_div; ++i)
284 isl_seq_cpy(div->row[i], bmap->div[i], cols);
286 return div;
289 /* Extract the divs in "bset" as a matrix.
291 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
293 return isl_basic_map_get_divs(bset);
296 __isl_give isl_local_space *isl_basic_map_get_local_space(
297 __isl_keep isl_basic_map *bmap)
299 isl_mat *div;
301 if (!bmap)
302 return NULL;
304 div = isl_basic_map_get_divs(bmap);
305 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
308 __isl_give isl_local_space *isl_basic_set_get_local_space(
309 __isl_keep isl_basic_set *bset)
311 return isl_basic_map_get_local_space(bset);
314 __isl_give isl_basic_map *isl_basic_map_from_local_space(
315 __isl_take isl_local_space *ls)
317 int i;
318 int n_div;
319 isl_basic_map *bmap;
321 if (!ls)
322 return NULL;
324 n_div = isl_local_space_dim(ls, isl_dim_div);
325 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
326 n_div, 0, 2 * n_div);
328 for (i = 0; i < n_div; ++i)
329 if (isl_basic_map_alloc_div(bmap) < 0)
330 goto error;
332 for (i = 0; i < n_div; ++i) {
333 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
334 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
335 goto error;
338 isl_local_space_free(ls);
339 return bmap;
340 error:
341 isl_local_space_free(ls);
342 isl_basic_map_free(bmap);
343 return NULL;
346 __isl_give isl_basic_set *isl_basic_set_from_local_space(
347 __isl_take isl_local_space *ls)
349 return isl_basic_map_from_local_space(ls);
352 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
354 if (!map)
355 return NULL;
356 return isl_space_copy(map->dim);
359 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
361 if (!set)
362 return NULL;
363 return isl_space_copy(set->dim);
366 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
367 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
369 bmap = isl_basic_map_cow(bmap);
370 if (!bmap)
371 return NULL;
372 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
373 if (!bmap->dim)
374 goto error;
375 bmap = isl_basic_map_finalize(bmap);
376 return bmap;
377 error:
378 isl_basic_map_free(bmap);
379 return NULL;
382 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
383 __isl_take isl_basic_set *bset, const char *s)
385 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
388 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
389 enum isl_dim_type type)
391 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
394 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
395 enum isl_dim_type type, const char *s)
397 int i;
399 map = isl_map_cow(map);
400 if (!map)
401 return NULL;
403 map->dim = isl_space_set_tuple_name(map->dim, type, s);
404 if (!map->dim)
405 goto error;
407 for (i = 0; i < map->n; ++i) {
408 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
409 if (!map->p[i])
410 goto error;
413 return map;
414 error:
415 isl_map_free(map);
416 return NULL;
419 /* Does the input or output tuple have a name?
421 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
423 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
426 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
427 enum isl_dim_type type)
429 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
432 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
433 const char *s)
435 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
438 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
439 enum isl_dim_type type, __isl_take isl_id *id)
441 map = isl_map_cow(map);
442 if (!map)
443 return isl_id_free(id);
445 map->dim = isl_space_set_tuple_id(map->dim, type, id);
447 return isl_map_reset_space(map, isl_space_copy(map->dim));
450 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
451 __isl_take isl_id *id)
453 return isl_map_set_tuple_id(set, isl_dim_set, id);
456 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
457 enum isl_dim_type type)
459 map = isl_map_cow(map);
460 if (!map)
461 return NULL;
463 map->dim = isl_space_reset_tuple_id(map->dim, type);
465 return isl_map_reset_space(map, isl_space_copy(map->dim));
468 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
470 return isl_map_reset_tuple_id(set, isl_dim_set);
473 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
475 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
478 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
479 enum isl_dim_type type)
481 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
484 int isl_set_has_tuple_id(__isl_keep isl_set *set)
486 return isl_map_has_tuple_id(set, isl_dim_set);
489 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
491 return isl_map_get_tuple_id(set, isl_dim_set);
494 /* Does the set tuple have a name?
496 int isl_set_has_tuple_name(__isl_keep isl_set *set)
498 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
502 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
504 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
507 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
509 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
512 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
513 enum isl_dim_type type, unsigned pos)
515 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
518 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
519 enum isl_dim_type type, unsigned pos)
521 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
524 /* Does the given dimension have a name?
526 int isl_map_has_dim_name(__isl_keep isl_map *map,
527 enum isl_dim_type type, unsigned pos)
529 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
532 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
533 enum isl_dim_type type, unsigned pos)
535 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
538 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
539 enum isl_dim_type type, unsigned pos)
541 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
544 /* Does the given dimension have a name?
546 int isl_set_has_dim_name(__isl_keep isl_set *set,
547 enum isl_dim_type type, unsigned pos)
549 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
552 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
553 __isl_take isl_basic_map *bmap,
554 enum isl_dim_type type, unsigned pos, const char *s)
556 bmap = isl_basic_map_cow(bmap);
557 if (!bmap)
558 return NULL;
559 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
560 if (!bmap->dim)
561 goto error;
562 return isl_basic_map_finalize(bmap);
563 error:
564 isl_basic_map_free(bmap);
565 return NULL;
568 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
569 enum isl_dim_type type, unsigned pos, const char *s)
571 int i;
573 map = isl_map_cow(map);
574 if (!map)
575 return NULL;
577 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
578 if (!map->dim)
579 goto error;
581 for (i = 0; i < map->n; ++i) {
582 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
583 if (!map->p[i])
584 goto error;
587 return map;
588 error:
589 isl_map_free(map);
590 return NULL;
593 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
594 __isl_take isl_basic_set *bset,
595 enum isl_dim_type type, unsigned pos, const char *s)
597 return (isl_basic_set *)isl_basic_map_set_dim_name(
598 (isl_basic_map *)bset, type, pos, s);
601 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
602 enum isl_dim_type type, unsigned pos, const char *s)
604 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
607 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
608 enum isl_dim_type type, unsigned pos)
610 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
613 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
614 enum isl_dim_type type, unsigned pos)
616 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
619 int isl_map_has_dim_id(__isl_keep isl_map *map,
620 enum isl_dim_type type, unsigned pos)
622 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
625 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
626 enum isl_dim_type type, unsigned pos)
628 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
631 int isl_set_has_dim_id(__isl_keep isl_set *set,
632 enum isl_dim_type type, unsigned pos)
634 return isl_map_has_dim_id(set, type, pos);
637 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
638 enum isl_dim_type type, unsigned pos)
640 return isl_map_get_dim_id(set, type, pos);
643 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
644 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
646 map = isl_map_cow(map);
647 if (!map)
648 return isl_id_free(id);
650 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
652 return isl_map_reset_space(map, isl_space_copy(map->dim));
655 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
656 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
658 return isl_map_set_dim_id(set, type, pos, id);
661 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
662 __isl_keep isl_id *id)
664 if (!map)
665 return -1;
666 return isl_space_find_dim_by_id(map->dim, type, id);
669 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
670 __isl_keep isl_id *id)
672 return isl_map_find_dim_by_id(set, type, id);
675 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
676 const char *name)
678 if (!map)
679 return -1;
680 return isl_space_find_dim_by_name(map->dim, type, name);
683 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
684 const char *name)
686 return isl_map_find_dim_by_name(set, type, name);
689 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
691 if (!bmap)
692 return -1;
693 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
696 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
698 return isl_basic_map_is_rational(bset);
701 /* Does "bmap" contain any rational points?
703 * If "bmap" has an equality for each dimension, equating the dimension
704 * to an integer constant, then it has no rational points, even if it
705 * is marked as rational.
707 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
709 int has_rational = 1;
710 unsigned total;
712 if (!bmap)
713 return -1;
714 if (isl_basic_map_plain_is_empty(bmap))
715 return 0;
716 if (!isl_basic_map_is_rational(bmap))
717 return 0;
718 bmap = isl_basic_map_copy(bmap);
719 bmap = isl_basic_map_implicit_equalities(bmap);
720 if (!bmap)
721 return -1;
722 total = isl_basic_map_total_dim(bmap);
723 if (bmap->n_eq == total) {
724 int i, j;
725 for (i = 0; i < bmap->n_eq; ++i) {
726 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
727 if (j < 0)
728 break;
729 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
730 !isl_int_is_negone(bmap->eq[i][1 + j]))
731 break;
732 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
733 total - j - 1);
734 if (j >= 0)
735 break;
737 if (i == bmap->n_eq)
738 has_rational = 0;
740 isl_basic_map_free(bmap);
742 return has_rational;
745 /* Does "map" contain any rational points?
747 int isl_map_has_rational(__isl_keep isl_map *map)
749 int i;
750 int has_rational;
752 if (!map)
753 return -1;
754 for (i = 0; i < map->n; ++i) {
755 has_rational = isl_basic_map_has_rational(map->p[i]);
756 if (has_rational < 0)
757 return -1;
758 if (has_rational)
759 return 1;
761 return 0;
764 /* Does "set" contain any rational points?
766 int isl_set_has_rational(__isl_keep isl_set *set)
768 return isl_map_has_rational(set);
771 /* Is this basic set a parameter domain?
773 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
775 if (!bset)
776 return -1;
777 return isl_space_is_params(bset->dim);
780 /* Is this set a parameter domain?
782 int isl_set_is_params(__isl_keep isl_set *set)
784 if (!set)
785 return -1;
786 return isl_space_is_params(set->dim);
789 /* Is this map actually a parameter domain?
790 * Users should never call this function. Outside of isl,
791 * a map can never be a parameter domain.
793 int isl_map_is_params(__isl_keep isl_map *map)
795 if (!map)
796 return -1;
797 return isl_space_is_params(map->dim);
800 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
801 struct isl_basic_map *bmap, unsigned extra,
802 unsigned n_eq, unsigned n_ineq)
804 int i;
805 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
807 bmap->ctx = ctx;
808 isl_ctx_ref(ctx);
810 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
811 if (isl_blk_is_error(bmap->block))
812 goto error;
814 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
815 if (!bmap->ineq)
816 goto error;
818 if (extra == 0) {
819 bmap->block2 = isl_blk_empty();
820 bmap->div = NULL;
821 } else {
822 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
823 if (isl_blk_is_error(bmap->block2))
824 goto error;
826 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
827 if (!bmap->div)
828 goto error;
831 for (i = 0; i < n_ineq + n_eq; ++i)
832 bmap->ineq[i] = bmap->block.data + i * row_size;
834 for (i = 0; i < extra; ++i)
835 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
837 bmap->ref = 1;
838 bmap->flags = 0;
839 bmap->c_size = n_eq + n_ineq;
840 bmap->eq = bmap->ineq + n_ineq;
841 bmap->extra = extra;
842 bmap->n_eq = 0;
843 bmap->n_ineq = 0;
844 bmap->n_div = 0;
845 bmap->sample = NULL;
847 return bmap;
848 error:
849 isl_basic_map_free(bmap);
850 return NULL;
853 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
854 unsigned nparam, unsigned dim, unsigned extra,
855 unsigned n_eq, unsigned n_ineq)
857 struct isl_basic_map *bmap;
858 isl_space *space;
860 space = isl_space_set_alloc(ctx, nparam, dim);
861 if (!space)
862 return NULL;
864 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
865 return (struct isl_basic_set *)bmap;
868 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
869 unsigned extra, unsigned n_eq, unsigned n_ineq)
871 struct isl_basic_map *bmap;
872 if (!dim)
873 return NULL;
874 isl_assert(dim->ctx, dim->n_in == 0, goto error);
875 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
876 return (struct isl_basic_set *)bmap;
877 error:
878 isl_space_free(dim);
879 return NULL;
882 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
883 unsigned extra, unsigned n_eq, unsigned n_ineq)
885 struct isl_basic_map *bmap;
887 if (!dim)
888 return NULL;
889 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
890 if (!bmap)
891 goto error;
892 bmap->dim = dim;
894 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
895 error:
896 isl_space_free(dim);
897 return NULL;
900 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
901 unsigned nparam, unsigned in, unsigned out, unsigned extra,
902 unsigned n_eq, unsigned n_ineq)
904 struct isl_basic_map *bmap;
905 isl_space *dim;
907 dim = isl_space_alloc(ctx, nparam, in, out);
908 if (!dim)
909 return NULL;
911 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
912 return bmap;
915 static void dup_constraints(
916 struct isl_basic_map *dst, struct isl_basic_map *src)
918 int i;
919 unsigned total = isl_basic_map_total_dim(src);
921 for (i = 0; i < src->n_eq; ++i) {
922 int j = isl_basic_map_alloc_equality(dst);
923 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
926 for (i = 0; i < src->n_ineq; ++i) {
927 int j = isl_basic_map_alloc_inequality(dst);
928 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
931 for (i = 0; i < src->n_div; ++i) {
932 int j = isl_basic_map_alloc_div(dst);
933 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
935 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
938 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
940 struct isl_basic_map *dup;
942 if (!bmap)
943 return NULL;
944 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
945 bmap->n_div, bmap->n_eq, bmap->n_ineq);
946 if (!dup)
947 return NULL;
948 dup_constraints(dup, bmap);
949 dup->flags = bmap->flags;
950 dup->sample = isl_vec_copy(bmap->sample);
951 return dup;
954 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
956 struct isl_basic_map *dup;
958 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
959 return (struct isl_basic_set *)dup;
962 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
964 if (!bset)
965 return NULL;
967 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
968 bset->ref++;
969 return bset;
971 return isl_basic_set_dup(bset);
974 struct isl_set *isl_set_copy(struct isl_set *set)
976 if (!set)
977 return NULL;
979 set->ref++;
980 return set;
983 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
985 if (!bmap)
986 return NULL;
988 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
989 bmap->ref++;
990 return bmap;
992 bmap = isl_basic_map_dup(bmap);
993 if (bmap)
994 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
995 return bmap;
998 struct isl_map *isl_map_copy(struct isl_map *map)
1000 if (!map)
1001 return NULL;
1003 map->ref++;
1004 return map;
1007 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1009 if (!bmap)
1010 return NULL;
1012 if (--bmap->ref > 0)
1013 return NULL;
1015 isl_ctx_deref(bmap->ctx);
1016 free(bmap->div);
1017 isl_blk_free(bmap->ctx, bmap->block2);
1018 free(bmap->ineq);
1019 isl_blk_free(bmap->ctx, bmap->block);
1020 isl_vec_free(bmap->sample);
1021 isl_space_free(bmap->dim);
1022 free(bmap);
1024 return NULL;
1027 void *isl_basic_set_free(struct isl_basic_set *bset)
1029 return isl_basic_map_free((struct isl_basic_map *)bset);
1032 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1034 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1037 __isl_give isl_map *isl_map_align_params_map_map_and(
1038 __isl_take isl_map *map1, __isl_take isl_map *map2,
1039 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1040 __isl_take isl_map *map2))
1042 if (!map1 || !map2)
1043 goto error;
1044 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1045 return fn(map1, map2);
1046 if (!isl_space_has_named_params(map1->dim) ||
1047 !isl_space_has_named_params(map2->dim))
1048 isl_die(map1->ctx, isl_error_invalid,
1049 "unaligned unnamed parameters", goto error);
1050 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1051 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1052 return fn(map1, map2);
1053 error:
1054 isl_map_free(map1);
1055 isl_map_free(map2);
1056 return NULL;
1059 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1060 __isl_keep isl_map *map2,
1061 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1063 int r;
1065 if (!map1 || !map2)
1066 return -1;
1067 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1068 return fn(map1, map2);
1069 if (!isl_space_has_named_params(map1->dim) ||
1070 !isl_space_has_named_params(map2->dim))
1071 isl_die(map1->ctx, isl_error_invalid,
1072 "unaligned unnamed parameters", return -1);
1073 map1 = isl_map_copy(map1);
1074 map2 = isl_map_copy(map2);
1075 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1076 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1077 r = fn(map1, map2);
1078 isl_map_free(map1);
1079 isl_map_free(map2);
1080 return r;
1083 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1085 struct isl_ctx *ctx;
1086 if (!bmap)
1087 return -1;
1088 ctx = bmap->ctx;
1089 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1090 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1091 return -1);
1092 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1093 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1094 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1095 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1096 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1097 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1098 isl_int *t;
1099 int j = isl_basic_map_alloc_inequality(bmap);
1100 if (j < 0)
1101 return -1;
1102 t = bmap->ineq[j];
1103 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1104 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1105 bmap->eq[-1] = t;
1106 bmap->n_eq++;
1107 bmap->n_ineq--;
1108 bmap->eq--;
1109 return 0;
1111 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1112 bmap->extra - bmap->n_div);
1113 return bmap->n_eq++;
1116 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1118 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1121 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1123 if (!bmap)
1124 return -1;
1125 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1126 bmap->n_eq -= n;
1127 return 0;
1130 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1132 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1135 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1137 isl_int *t;
1138 if (!bmap)
1139 return -1;
1140 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1142 if (pos != bmap->n_eq - 1) {
1143 t = bmap->eq[pos];
1144 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1145 bmap->eq[bmap->n_eq - 1] = t;
1147 bmap->n_eq--;
1148 return 0;
1151 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1153 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1156 /* Turn inequality "pos" of "bmap" into an equality.
1158 * In particular, we move the inequality in front of the equalities
1159 * and move the last inequality in the position of the moved inequality.
1160 * Note that isl_tab_make_equalities_explicit depends on this particular
1161 * change in the ordering of the constraints.
1163 void isl_basic_map_inequality_to_equality(
1164 struct isl_basic_map *bmap, unsigned pos)
1166 isl_int *t;
1168 t = bmap->ineq[pos];
1169 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1170 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1171 bmap->eq[-1] = t;
1172 bmap->n_eq++;
1173 bmap->n_ineq--;
1174 bmap->eq--;
1175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1176 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1177 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1181 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1183 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1186 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1188 struct isl_ctx *ctx;
1189 if (!bmap)
1190 return -1;
1191 ctx = bmap->ctx;
1192 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1193 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1194 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1195 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1196 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1197 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1198 1 + isl_basic_map_total_dim(bmap),
1199 bmap->extra - bmap->n_div);
1200 return bmap->n_ineq++;
1203 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1205 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1208 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1210 if (!bmap)
1211 return -1;
1212 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1213 bmap->n_ineq -= n;
1214 return 0;
1217 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1219 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1222 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1224 isl_int *t;
1225 if (!bmap)
1226 return -1;
1227 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1229 if (pos != bmap->n_ineq - 1) {
1230 t = bmap->ineq[pos];
1231 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1232 bmap->ineq[bmap->n_ineq - 1] = t;
1233 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1235 bmap->n_ineq--;
1236 return 0;
1239 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1241 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1244 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1245 isl_int *eq)
1247 int k;
1249 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1250 if (!bmap)
1251 return NULL;
1252 k = isl_basic_map_alloc_equality(bmap);
1253 if (k < 0)
1254 goto error;
1255 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1256 return bmap;
1257 error:
1258 isl_basic_map_free(bmap);
1259 return NULL;
1262 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1263 isl_int *eq)
1265 return (isl_basic_set *)
1266 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1269 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1270 isl_int *ineq)
1272 int k;
1274 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1275 if (!bmap)
1276 return NULL;
1277 k = isl_basic_map_alloc_inequality(bmap);
1278 if (k < 0)
1279 goto error;
1280 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1281 return bmap;
1282 error:
1283 isl_basic_map_free(bmap);
1284 return NULL;
1287 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1288 isl_int *ineq)
1290 return (isl_basic_set *)
1291 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1294 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1296 if (!bmap)
1297 return -1;
1298 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1299 isl_seq_clr(bmap->div[bmap->n_div] +
1300 1 + 1 + isl_basic_map_total_dim(bmap),
1301 bmap->extra - bmap->n_div);
1302 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1303 return bmap->n_div++;
1306 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1308 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1311 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1313 if (!bmap)
1314 return -1;
1315 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1316 bmap->n_div -= n;
1317 return 0;
1320 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1322 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1325 /* Copy constraint from src to dst, putting the vars of src at offset
1326 * dim_off in dst and the divs of src at offset div_off in dst.
1327 * If both sets are actually map, then dim_off applies to the input
1328 * variables.
1330 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1331 struct isl_basic_map *src_map, isl_int *src,
1332 unsigned in_off, unsigned out_off, unsigned div_off)
1334 unsigned src_nparam = isl_basic_map_n_param(src_map);
1335 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1336 unsigned src_in = isl_basic_map_n_in(src_map);
1337 unsigned dst_in = isl_basic_map_n_in(dst_map);
1338 unsigned src_out = isl_basic_map_n_out(src_map);
1339 unsigned dst_out = isl_basic_map_n_out(dst_map);
1340 isl_int_set(dst[0], src[0]);
1341 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1342 if (dst_nparam > src_nparam)
1343 isl_seq_clr(dst+1+src_nparam,
1344 dst_nparam - src_nparam);
1345 isl_seq_clr(dst+1+dst_nparam, in_off);
1346 isl_seq_cpy(dst+1+dst_nparam+in_off,
1347 src+1+src_nparam,
1348 isl_min(dst_in-in_off, src_in));
1349 if (dst_in-in_off > src_in)
1350 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1351 dst_in - in_off - src_in);
1352 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1353 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1354 src+1+src_nparam+src_in,
1355 isl_min(dst_out-out_off, src_out));
1356 if (dst_out-out_off > src_out)
1357 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1358 dst_out - out_off - src_out);
1359 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1360 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1361 src+1+src_nparam+src_in+src_out,
1362 isl_min(dst_map->extra-div_off, src_map->n_div));
1363 if (dst_map->n_div-div_off > src_map->n_div)
1364 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1365 div_off+src_map->n_div,
1366 dst_map->n_div - div_off - src_map->n_div);
1369 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1370 struct isl_basic_map *src_map, isl_int *src,
1371 unsigned in_off, unsigned out_off, unsigned div_off)
1373 isl_int_set(dst[0], src[0]);
1374 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1377 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1378 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1380 int i;
1381 unsigned div_off;
1383 if (!bmap1 || !bmap2)
1384 goto error;
1386 div_off = bmap1->n_div;
1388 for (i = 0; i < bmap2->n_eq; ++i) {
1389 int i1 = isl_basic_map_alloc_equality(bmap1);
1390 if (i1 < 0)
1391 goto error;
1392 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1393 i_pos, o_pos, div_off);
1396 for (i = 0; i < bmap2->n_ineq; ++i) {
1397 int i1 = isl_basic_map_alloc_inequality(bmap1);
1398 if (i1 < 0)
1399 goto error;
1400 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1401 i_pos, o_pos, div_off);
1404 for (i = 0; i < bmap2->n_div; ++i) {
1405 int i1 = isl_basic_map_alloc_div(bmap1);
1406 if (i1 < 0)
1407 goto error;
1408 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1409 i_pos, o_pos, div_off);
1412 isl_basic_map_free(bmap2);
1414 return bmap1;
1416 error:
1417 isl_basic_map_free(bmap1);
1418 isl_basic_map_free(bmap2);
1419 return NULL;
1422 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1423 struct isl_basic_set *bset2, unsigned pos)
1425 return (struct isl_basic_set *)
1426 add_constraints((struct isl_basic_map *)bset1,
1427 (struct isl_basic_map *)bset2, 0, pos);
1430 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1431 __isl_take isl_space *dim, unsigned extra,
1432 unsigned n_eq, unsigned n_ineq)
1434 struct isl_basic_map *ext;
1435 unsigned flags;
1436 int dims_ok;
1438 if (!dim)
1439 goto error;
1441 if (!base)
1442 goto error;
1444 dims_ok = isl_space_is_equal(base->dim, dim) &&
1445 base->extra >= base->n_div + extra;
1447 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1448 room_for_ineq(base, n_ineq)) {
1449 isl_space_free(dim);
1450 return base;
1453 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1454 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1455 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1456 extra += base->extra;
1457 n_eq += base->n_eq;
1458 n_ineq += base->n_ineq;
1460 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1461 dim = NULL;
1462 if (!ext)
1463 goto error;
1465 if (dims_ok)
1466 ext->sample = isl_vec_copy(base->sample);
1467 flags = base->flags;
1468 ext = add_constraints(ext, base, 0, 0);
1469 if (ext) {
1470 ext->flags = flags;
1471 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1474 return ext;
1476 error:
1477 isl_space_free(dim);
1478 isl_basic_map_free(base);
1479 return NULL;
1482 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1483 __isl_take isl_space *dim, unsigned extra,
1484 unsigned n_eq, unsigned n_ineq)
1486 return (struct isl_basic_set *)
1487 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1488 extra, n_eq, n_ineq);
1491 struct isl_basic_map *isl_basic_map_extend_constraints(
1492 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1494 if (!base)
1495 return NULL;
1496 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1497 0, n_eq, n_ineq);
1500 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1501 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1502 unsigned n_eq, unsigned n_ineq)
1504 struct isl_basic_map *bmap;
1505 isl_space *dim;
1507 if (!base)
1508 return NULL;
1509 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1510 if (!dim)
1511 goto error;
1513 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1514 return bmap;
1515 error:
1516 isl_basic_map_free(base);
1517 return NULL;
1520 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1521 unsigned nparam, unsigned dim, unsigned extra,
1522 unsigned n_eq, unsigned n_ineq)
1524 return (struct isl_basic_set *)
1525 isl_basic_map_extend((struct isl_basic_map *)base,
1526 nparam, 0, dim, extra, n_eq, n_ineq);
1529 struct isl_basic_set *isl_basic_set_extend_constraints(
1530 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1532 return (struct isl_basic_set *)
1533 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1534 n_eq, n_ineq);
1537 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1539 return (struct isl_basic_set *)
1540 isl_basic_map_cow((struct isl_basic_map *)bset);
1543 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1545 if (!bmap)
1546 return NULL;
1548 if (bmap->ref > 1) {
1549 bmap->ref--;
1550 bmap = isl_basic_map_dup(bmap);
1552 if (bmap)
1553 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1554 return bmap;
1557 struct isl_set *isl_set_cow(struct isl_set *set)
1559 if (!set)
1560 return NULL;
1562 if (set->ref == 1)
1563 return set;
1564 set->ref--;
1565 return isl_set_dup(set);
1568 struct isl_map *isl_map_cow(struct isl_map *map)
1570 if (!map)
1571 return NULL;
1573 if (map->ref == 1)
1574 return map;
1575 map->ref--;
1576 return isl_map_dup(map);
1579 static void swap_vars(struct isl_blk blk, isl_int *a,
1580 unsigned a_len, unsigned b_len)
1582 isl_seq_cpy(blk.data, a+a_len, b_len);
1583 isl_seq_cpy(blk.data+b_len, a, a_len);
1584 isl_seq_cpy(a, blk.data, b_len+a_len);
1587 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1588 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1590 int i;
1591 struct isl_blk blk;
1593 if (!bmap)
1594 goto error;
1596 isl_assert(bmap->ctx,
1597 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1599 if (n1 == 0 || n2 == 0)
1600 return bmap;
1602 bmap = isl_basic_map_cow(bmap);
1603 if (!bmap)
1604 return NULL;
1606 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1607 if (isl_blk_is_error(blk))
1608 goto error;
1610 for (i = 0; i < bmap->n_eq; ++i)
1611 swap_vars(blk,
1612 bmap->eq[i] + pos, n1, n2);
1614 for (i = 0; i < bmap->n_ineq; ++i)
1615 swap_vars(blk,
1616 bmap->ineq[i] + pos, n1, n2);
1618 for (i = 0; i < bmap->n_div; ++i)
1619 swap_vars(blk,
1620 bmap->div[i]+1 + pos, n1, n2);
1622 isl_blk_free(bmap->ctx, blk);
1624 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1625 bmap = isl_basic_map_gauss(bmap, NULL);
1626 return isl_basic_map_finalize(bmap);
1627 error:
1628 isl_basic_map_free(bmap);
1629 return NULL;
1632 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1633 __isl_take isl_basic_set *bset, unsigned n)
1635 unsigned dim;
1636 unsigned nparam;
1638 if (!bset)
1639 return NULL;
1641 nparam = isl_basic_set_n_param(bset);
1642 dim = isl_basic_set_n_dim(bset);
1643 isl_assert(bset->ctx, n <= dim, goto error);
1645 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1646 error:
1647 isl_basic_set_free(bset);
1648 return NULL;
1651 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1653 int i = 0;
1654 unsigned total;
1655 if (!bmap)
1656 goto error;
1657 total = isl_basic_map_total_dim(bmap);
1658 isl_basic_map_free_div(bmap, bmap->n_div);
1659 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1660 if (bmap->n_eq > 0)
1661 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1662 else {
1663 i = isl_basic_map_alloc_equality(bmap);
1664 if (i < 0)
1665 goto error;
1667 isl_int_set_si(bmap->eq[i][0], 1);
1668 isl_seq_clr(bmap->eq[i]+1, total);
1669 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1670 isl_vec_free(bmap->sample);
1671 bmap->sample = NULL;
1672 return isl_basic_map_finalize(bmap);
1673 error:
1674 isl_basic_map_free(bmap);
1675 return NULL;
1678 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1680 return (struct isl_basic_set *)
1681 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1684 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1685 * of "bmap").
1687 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1689 isl_int *t = bmap->div[a];
1690 bmap->div[a] = bmap->div[b];
1691 bmap->div[b] = t;
1694 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1695 * div definitions accordingly.
1697 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1699 int i;
1700 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1702 swap_div(bmap, a, b);
1704 for (i = 0; i < bmap->n_eq; ++i)
1705 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1707 for (i = 0; i < bmap->n_ineq; ++i)
1708 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1710 for (i = 0; i < bmap->n_div; ++i)
1711 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1712 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1715 /* Eliminate the specified n dimensions starting at first from the
1716 * constraints, without removing the dimensions from the space.
1717 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1719 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1720 enum isl_dim_type type, unsigned first, unsigned n)
1722 int i;
1724 if (!map)
1725 return NULL;
1726 if (n == 0)
1727 return map;
1729 if (first + n > isl_map_dim(map, type) || first + n < first)
1730 isl_die(map->ctx, isl_error_invalid,
1731 "index out of bounds", goto error);
1733 map = isl_map_cow(map);
1734 if (!map)
1735 return NULL;
1737 for (i = 0; i < map->n; ++i) {
1738 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1739 if (!map->p[i])
1740 goto error;
1742 return map;
1743 error:
1744 isl_map_free(map);
1745 return NULL;
1748 /* Eliminate the specified n dimensions starting at first from the
1749 * constraints, without removing the dimensions from the space.
1750 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1752 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1753 enum isl_dim_type type, unsigned first, unsigned n)
1755 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1758 /* Eliminate the specified n dimensions starting at first from the
1759 * constraints, without removing the dimensions from the space.
1760 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1762 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1763 unsigned first, unsigned n)
1765 return isl_set_eliminate(set, isl_dim_set, first, n);
1768 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1769 __isl_take isl_basic_map *bmap)
1771 if (!bmap)
1772 return NULL;
1773 bmap = isl_basic_map_eliminate_vars(bmap,
1774 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1775 if (!bmap)
1776 return NULL;
1777 bmap->n_div = 0;
1778 return isl_basic_map_finalize(bmap);
1781 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1782 __isl_take isl_basic_set *bset)
1784 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1785 (struct isl_basic_map *)bset);
1788 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1790 int i;
1792 if (!map)
1793 return NULL;
1794 if (map->n == 0)
1795 return map;
1797 map = isl_map_cow(map);
1798 if (!map)
1799 return NULL;
1801 for (i = 0; i < map->n; ++i) {
1802 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1803 if (!map->p[i])
1804 goto error;
1806 return map;
1807 error:
1808 isl_map_free(map);
1809 return NULL;
1812 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1814 return isl_map_remove_divs(set);
1817 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1818 enum isl_dim_type type, unsigned first, unsigned n)
1820 if (!bmap)
1821 return NULL;
1822 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1823 goto error);
1824 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1825 return bmap;
1826 bmap = isl_basic_map_eliminate_vars(bmap,
1827 isl_basic_map_offset(bmap, type) - 1 + first, n);
1828 if (!bmap)
1829 return bmap;
1830 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1831 return bmap;
1832 bmap = isl_basic_map_drop(bmap, type, first, n);
1833 return bmap;
1834 error:
1835 isl_basic_map_free(bmap);
1836 return NULL;
1839 /* Return true if the definition of the given div (recursively) involves
1840 * any of the given variables.
1842 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1843 unsigned first, unsigned n)
1845 int i;
1846 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1848 if (isl_int_is_zero(bmap->div[div][0]))
1849 return 0;
1850 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1851 return 1;
1853 for (i = bmap->n_div - 1; i >= 0; --i) {
1854 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1855 continue;
1856 if (div_involves_vars(bmap, i, first, n))
1857 return 1;
1860 return 0;
1863 /* Try and add a lower and/or upper bound on "div" to "bmap"
1864 * based on inequality "i".
1865 * "total" is the total number of variables (excluding the divs).
1866 * "v" is a temporary object that can be used during the calculations.
1867 * If "lb" is set, then a lower bound should be constructed.
1868 * If "ub" is set, then an upper bound should be constructed.
1870 * The calling function has already checked that the inequality does not
1871 * reference "div", but we still need to check that the inequality is
1872 * of the right form. We'll consider the case where we want to construct
1873 * a lower bound. The construction of upper bounds is similar.
1875 * Let "div" be of the form
1877 * q = floor((a + f(x))/d)
1879 * We essentially check if constraint "i" is of the form
1881 * b + f(x) >= 0
1883 * so that we can use it to derive a lower bound on "div".
1884 * However, we allow a slightly more general form
1886 * b + g(x) >= 0
1888 * with the condition that the coefficients of g(x) - f(x) are all
1889 * divisible by d.
1890 * Rewriting this constraint as
1892 * 0 >= -b - g(x)
1894 * adding a + f(x) to both sides and dividing by d, we obtain
1896 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1898 * Taking the floor on both sides, we obtain
1900 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1902 * or
1904 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1906 * In the case of an upper bound, we construct the constraint
1908 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1911 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1912 __isl_take isl_basic_map *bmap, int div, int i,
1913 unsigned total, isl_int v, int lb, int ub)
1915 int j;
1917 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1918 if (lb) {
1919 isl_int_sub(v, bmap->ineq[i][1 + j],
1920 bmap->div[div][1 + 1 + j]);
1921 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1923 if (ub) {
1924 isl_int_add(v, bmap->ineq[i][1 + j],
1925 bmap->div[div][1 + 1 + j]);
1926 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1929 if (!lb && !ub)
1930 return bmap;
1932 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1933 if (lb) {
1934 int k = isl_basic_map_alloc_inequality(bmap);
1935 if (k < 0)
1936 goto error;
1937 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1938 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1939 bmap->div[div][1 + j]);
1940 isl_int_cdiv_q(bmap->ineq[k][j],
1941 bmap->ineq[k][j], bmap->div[div][0]);
1943 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1945 if (ub) {
1946 int k = isl_basic_map_alloc_inequality(bmap);
1947 if (k < 0)
1948 goto error;
1949 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1950 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1951 bmap->div[div][1 + j]);
1952 isl_int_fdiv_q(bmap->ineq[k][j],
1953 bmap->ineq[k][j], bmap->div[div][0]);
1955 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1958 return bmap;
1959 error:
1960 isl_basic_map_free(bmap);
1961 return NULL;
1964 /* This function is called right before "div" is eliminated from "bmap"
1965 * using Fourier-Motzkin.
1966 * Look through the constraints of "bmap" for constraints on the argument
1967 * of the integer division and use them to construct constraints on the
1968 * integer division itself. These constraints can then be combined
1969 * during the Fourier-Motzkin elimination.
1970 * Note that it is only useful to introduce lower bounds on "div"
1971 * if "bmap" already contains upper bounds on "div" as the newly
1972 * introduce lower bounds can then be combined with the pre-existing
1973 * upper bounds. Similarly for upper bounds.
1974 * We therefore first check if "bmap" contains any lower and/or upper bounds
1975 * on "div".
1977 * It is interesting to note that the introduction of these constraints
1978 * can indeed lead to more accurate results, even when compared to
1979 * deriving constraints on the argument of "div" from constraints on "div".
1980 * Consider, for example, the set
1982 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1984 * The second constraint can be rewritten as
1986 * 2 * [(-i-2j+3)/4] + k >= 0
1988 * from which we can derive
1990 * -i - 2j + 3 >= -2k
1992 * or
1994 * i + 2j <= 3 + 2k
1996 * Combined with the first constraint, we obtain
1998 * -3 <= 3 + 2k or k >= -3
2000 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2001 * the first constraint, we obtain
2003 * [(i + 2j)/4] >= [-3/4] = -1
2005 * Combining this constraint with the second constraint, we obtain
2007 * k >= -2
2009 static __isl_give isl_basic_map *insert_bounds_on_div(
2010 __isl_take isl_basic_map *bmap, int div)
2012 int i;
2013 int check_lb, check_ub;
2014 isl_int v;
2015 unsigned total;
2017 if (!bmap)
2018 return NULL;
2020 if (isl_int_is_zero(bmap->div[div][0]))
2021 return bmap;
2023 total = isl_space_dim(bmap->dim, isl_dim_all);
2025 check_lb = 0;
2026 check_ub = 0;
2027 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2028 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2029 if (s > 0)
2030 check_ub = 1;
2031 if (s < 0)
2032 check_lb = 1;
2035 if (!check_lb && !check_ub)
2036 return bmap;
2038 isl_int_init(v);
2040 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2041 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2042 continue;
2044 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2045 check_lb, check_ub);
2048 isl_int_clear(v);
2050 return bmap;
2053 /* Remove all divs (recursively) involving any of the given dimensions
2054 * in their definitions.
2056 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2057 __isl_take isl_basic_map *bmap,
2058 enum isl_dim_type type, unsigned first, unsigned n)
2060 int i;
2062 if (!bmap)
2063 return NULL;
2064 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2065 goto error);
2066 first += isl_basic_map_offset(bmap, type);
2068 for (i = bmap->n_div - 1; i >= 0; --i) {
2069 if (!div_involves_vars(bmap, i, first, n))
2070 continue;
2071 bmap = insert_bounds_on_div(bmap, i);
2072 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2073 if (!bmap)
2074 return NULL;
2075 i = bmap->n_div;
2078 return bmap;
2079 error:
2080 isl_basic_map_free(bmap);
2081 return NULL;
2084 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2085 __isl_take isl_basic_set *bset,
2086 enum isl_dim_type type, unsigned first, unsigned n)
2088 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2091 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2092 enum isl_dim_type type, unsigned first, unsigned n)
2094 int i;
2096 if (!map)
2097 return NULL;
2098 if (map->n == 0)
2099 return map;
2101 map = isl_map_cow(map);
2102 if (!map)
2103 return NULL;
2105 for (i = 0; i < map->n; ++i) {
2106 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2107 type, first, n);
2108 if (!map->p[i])
2109 goto error;
2111 return map;
2112 error:
2113 isl_map_free(map);
2114 return NULL;
2117 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2118 enum isl_dim_type type, unsigned first, unsigned n)
2120 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2121 type, first, n);
2124 /* Does the desciption of "bmap" depend on the specified dimensions?
2125 * We also check whether the dimensions appear in any of the div definitions.
2126 * In principle there is no need for this check. If the dimensions appear
2127 * in a div definition, they also appear in the defining constraints of that
2128 * div.
2130 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2131 enum isl_dim_type type, unsigned first, unsigned n)
2133 int i;
2135 if (!bmap)
2136 return -1;
2138 if (first + n > isl_basic_map_dim(bmap, type))
2139 isl_die(bmap->ctx, isl_error_invalid,
2140 "index out of bounds", return -1);
2142 first += isl_basic_map_offset(bmap, type);
2143 for (i = 0; i < bmap->n_eq; ++i)
2144 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2145 return 1;
2146 for (i = 0; i < bmap->n_ineq; ++i)
2147 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2148 return 1;
2149 for (i = 0; i < bmap->n_div; ++i) {
2150 if (isl_int_is_zero(bmap->div[i][0]))
2151 continue;
2152 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2153 return 1;
2156 return 0;
2159 int isl_map_involves_dims(__isl_keep isl_map *map,
2160 enum isl_dim_type type, unsigned first, unsigned n)
2162 int i;
2164 if (!map)
2165 return -1;
2167 if (first + n > isl_map_dim(map, type))
2168 isl_die(map->ctx, isl_error_invalid,
2169 "index out of bounds", return -1);
2171 for (i = 0; i < map->n; ++i) {
2172 int involves = isl_basic_map_involves_dims(map->p[i],
2173 type, first, n);
2174 if (involves < 0 || involves)
2175 return involves;
2178 return 0;
2181 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2182 enum isl_dim_type type, unsigned first, unsigned n)
2184 return isl_basic_map_involves_dims(bset, type, first, n);
2187 int isl_set_involves_dims(__isl_keep isl_set *set,
2188 enum isl_dim_type type, unsigned first, unsigned n)
2190 return isl_map_involves_dims(set, type, first, n);
2193 /* Return true if the definition of the given div is unknown or depends
2194 * on unknown divs.
2196 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2198 int i;
2199 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2201 if (isl_int_is_zero(bmap->div[div][0]))
2202 return 1;
2204 for (i = bmap->n_div - 1; i >= 0; --i) {
2205 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2206 continue;
2207 if (div_is_unknown(bmap, i))
2208 return 1;
2211 return 0;
2214 /* Remove all divs that are unknown or defined in terms of unknown divs.
2216 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2217 __isl_take isl_basic_map *bmap)
2219 int i;
2221 if (!bmap)
2222 return NULL;
2224 for (i = bmap->n_div - 1; i >= 0; --i) {
2225 if (!div_is_unknown(bmap, i))
2226 continue;
2227 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2228 if (!bmap)
2229 return NULL;
2230 i = bmap->n_div;
2233 return bmap;
2236 /* Remove all divs that are unknown or defined in terms of unknown divs.
2238 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2239 __isl_take isl_basic_set *bset)
2241 return isl_basic_map_remove_unknown_divs(bset);
2244 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2246 int i;
2248 if (!map)
2249 return NULL;
2250 if (map->n == 0)
2251 return map;
2253 map = isl_map_cow(map);
2254 if (!map)
2255 return NULL;
2257 for (i = 0; i < map->n; ++i) {
2258 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2259 if (!map->p[i])
2260 goto error;
2262 return map;
2263 error:
2264 isl_map_free(map);
2265 return NULL;
2268 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2270 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2273 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2274 __isl_take isl_basic_set *bset,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2277 return (isl_basic_set *)
2278 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2281 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2282 enum isl_dim_type type, unsigned first, unsigned n)
2284 int i;
2286 if (n == 0)
2287 return map;
2289 map = isl_map_cow(map);
2290 if (!map)
2291 return NULL;
2292 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2294 for (i = 0; i < map->n; ++i) {
2295 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2296 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2297 if (!map->p[i])
2298 goto error;
2300 map = isl_map_drop(map, type, first, n);
2301 return map;
2302 error:
2303 isl_map_free(map);
2304 return NULL;
2307 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2308 enum isl_dim_type type, unsigned first, unsigned n)
2310 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2313 /* Project out n inputs starting at first using Fourier-Motzkin */
2314 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2315 unsigned first, unsigned n)
2317 return isl_map_remove_dims(map, isl_dim_in, first, n);
2320 static void dump_term(struct isl_basic_map *bmap,
2321 isl_int c, int pos, FILE *out)
2323 const char *name;
2324 unsigned in = isl_basic_map_n_in(bmap);
2325 unsigned dim = in + isl_basic_map_n_out(bmap);
2326 unsigned nparam = isl_basic_map_n_param(bmap);
2327 if (!pos)
2328 isl_int_print(out, c, 0);
2329 else {
2330 if (!isl_int_is_one(c))
2331 isl_int_print(out, c, 0);
2332 if (pos < 1 + nparam) {
2333 name = isl_space_get_dim_name(bmap->dim,
2334 isl_dim_param, pos - 1);
2335 if (name)
2336 fprintf(out, "%s", name);
2337 else
2338 fprintf(out, "p%d", pos - 1);
2339 } else if (pos < 1 + nparam + in)
2340 fprintf(out, "i%d", pos - 1 - nparam);
2341 else if (pos < 1 + nparam + dim)
2342 fprintf(out, "o%d", pos - 1 - nparam - in);
2343 else
2344 fprintf(out, "e%d", pos - 1 - nparam - dim);
2348 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2349 int sign, FILE *out)
2351 int i;
2352 int first;
2353 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2354 isl_int v;
2356 isl_int_init(v);
2357 for (i = 0, first = 1; i < len; ++i) {
2358 if (isl_int_sgn(c[i]) * sign <= 0)
2359 continue;
2360 if (!first)
2361 fprintf(out, " + ");
2362 first = 0;
2363 isl_int_abs(v, c[i]);
2364 dump_term(bmap, v, i, out);
2366 isl_int_clear(v);
2367 if (first)
2368 fprintf(out, "0");
2371 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2372 const char *op, FILE *out, int indent)
2374 int i;
2376 fprintf(out, "%*s", indent, "");
2378 dump_constraint_sign(bmap, c, 1, out);
2379 fprintf(out, " %s ", op);
2380 dump_constraint_sign(bmap, c, -1, out);
2382 fprintf(out, "\n");
2384 for (i = bmap->n_div; i < bmap->extra; ++i) {
2385 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2386 continue;
2387 fprintf(out, "%*s", indent, "");
2388 fprintf(out, "ERROR: unused div coefficient not zero\n");
2389 abort();
2393 static void dump_constraints(struct isl_basic_map *bmap,
2394 isl_int **c, unsigned n,
2395 const char *op, FILE *out, int indent)
2397 int i;
2399 for (i = 0; i < n; ++i)
2400 dump_constraint(bmap, c[i], op, out, indent);
2403 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2405 int j;
2406 int first = 1;
2407 unsigned total = isl_basic_map_total_dim(bmap);
2409 for (j = 0; j < 1 + total; ++j) {
2410 if (isl_int_is_zero(exp[j]))
2411 continue;
2412 if (!first && isl_int_is_pos(exp[j]))
2413 fprintf(out, "+");
2414 dump_term(bmap, exp[j], j, out);
2415 first = 0;
2419 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2421 int i;
2423 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2424 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2426 for (i = 0; i < bmap->n_div; ++i) {
2427 fprintf(out, "%*s", indent, "");
2428 fprintf(out, "e%d = [(", i);
2429 dump_affine(bmap, bmap->div[i]+1, out);
2430 fprintf(out, ")/");
2431 isl_int_print(out, bmap->div[i][0], 0);
2432 fprintf(out, "]\n");
2436 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2437 FILE *out, int indent)
2439 if (!bset) {
2440 fprintf(out, "null basic set\n");
2441 return;
2444 fprintf(out, "%*s", indent, "");
2445 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2446 bset->ref, bset->dim->nparam, bset->dim->n_out,
2447 bset->extra, bset->flags);
2448 dump((struct isl_basic_map *)bset, out, indent);
2451 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2452 FILE *out, int indent)
2454 if (!bmap) {
2455 fprintf(out, "null basic map\n");
2456 return;
2459 fprintf(out, "%*s", indent, "");
2460 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2461 "flags: %x, n_name: %d\n",
2462 bmap->ref,
2463 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2464 bmap->extra, bmap->flags, bmap->dim->n_id);
2465 dump(bmap, out, indent);
2468 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2470 unsigned total;
2471 if (!bmap)
2472 return -1;
2473 total = isl_basic_map_total_dim(bmap);
2474 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2475 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2476 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2478 return 0;
2481 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2482 unsigned flags)
2484 struct isl_set *set;
2486 if (!dim)
2487 return NULL;
2488 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2489 isl_assert(dim->ctx, n >= 0, goto error);
2490 set = isl_alloc(dim->ctx, struct isl_set,
2491 sizeof(struct isl_set) +
2492 (n - 1) * sizeof(struct isl_basic_set *));
2493 if (!set)
2494 goto error;
2496 set->ctx = dim->ctx;
2497 isl_ctx_ref(set->ctx);
2498 set->ref = 1;
2499 set->size = n;
2500 set->n = 0;
2501 set->dim = dim;
2502 set->flags = flags;
2503 return set;
2504 error:
2505 isl_space_free(dim);
2506 return NULL;
2509 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2510 unsigned nparam, unsigned dim, int n, unsigned flags)
2512 struct isl_set *set;
2513 isl_space *dims;
2515 dims = isl_space_alloc(ctx, nparam, 0, dim);
2516 if (!dims)
2517 return NULL;
2519 set = isl_set_alloc_space(dims, n, flags);
2520 return set;
2523 /* Make sure "map" has room for at least "n" more basic maps.
2525 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2527 int i;
2528 struct isl_map *grown = NULL;
2530 if (!map)
2531 return NULL;
2532 isl_assert(map->ctx, n >= 0, goto error);
2533 if (map->n + n <= map->size)
2534 return map;
2535 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2536 if (!grown)
2537 goto error;
2538 for (i = 0; i < map->n; ++i) {
2539 grown->p[i] = isl_basic_map_copy(map->p[i]);
2540 if (!grown->p[i])
2541 goto error;
2542 grown->n++;
2544 isl_map_free(map);
2545 return grown;
2546 error:
2547 isl_map_free(grown);
2548 isl_map_free(map);
2549 return NULL;
2552 /* Make sure "set" has room for at least "n" more basic sets.
2554 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2556 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2559 struct isl_set *isl_set_dup(struct isl_set *set)
2561 int i;
2562 struct isl_set *dup;
2564 if (!set)
2565 return NULL;
2567 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2568 if (!dup)
2569 return NULL;
2570 for (i = 0; i < set->n; ++i)
2571 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2572 return dup;
2575 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2577 return isl_map_from_basic_map(bset);
2580 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2582 struct isl_map *map;
2584 if (!bmap)
2585 return NULL;
2587 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2588 return isl_map_add_basic_map(map, bmap);
2591 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2592 __isl_take isl_basic_set *bset)
2594 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2595 (struct isl_basic_map *)bset);
2598 void *isl_set_free(__isl_take isl_set *set)
2600 int i;
2602 if (!set)
2603 return NULL;
2605 if (--set->ref > 0)
2606 return NULL;
2608 isl_ctx_deref(set->ctx);
2609 for (i = 0; i < set->n; ++i)
2610 isl_basic_set_free(set->p[i]);
2611 isl_space_free(set->dim);
2612 free(set);
2614 return NULL;
2617 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2619 int i;
2621 if (!set) {
2622 fprintf(out, "null set\n");
2623 return;
2626 fprintf(out, "%*s", indent, "");
2627 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2628 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2629 set->flags);
2630 for (i = 0; i < set->n; ++i) {
2631 fprintf(out, "%*s", indent, "");
2632 fprintf(out, "basic set %d:\n", i);
2633 isl_basic_set_print_internal(set->p[i], out, indent+4);
2637 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2639 int i;
2641 if (!map) {
2642 fprintf(out, "null map\n");
2643 return;
2646 fprintf(out, "%*s", indent, "");
2647 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2648 "flags: %x, n_name: %d\n",
2649 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2650 map->dim->n_out, map->flags, map->dim->n_id);
2651 for (i = 0; i < map->n; ++i) {
2652 fprintf(out, "%*s", indent, "");
2653 fprintf(out, "basic map %d:\n", i);
2654 isl_basic_map_print_internal(map->p[i], out, indent+4);
2658 struct isl_basic_map *isl_basic_map_intersect_domain(
2659 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2661 struct isl_basic_map *bmap_domain;
2663 if (!bmap || !bset)
2664 goto error;
2666 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2667 bset->dim, isl_dim_param), goto error);
2669 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2670 isl_assert(bset->ctx,
2671 isl_basic_map_compatible_domain(bmap, bset), goto error);
2673 bmap = isl_basic_map_cow(bmap);
2674 if (!bmap)
2675 goto error;
2676 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2677 bset->n_div, bset->n_eq, bset->n_ineq);
2678 bmap_domain = isl_basic_map_from_domain(bset);
2679 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2681 bmap = isl_basic_map_simplify(bmap);
2682 return isl_basic_map_finalize(bmap);
2683 error:
2684 isl_basic_map_free(bmap);
2685 isl_basic_set_free(bset);
2686 return NULL;
2689 struct isl_basic_map *isl_basic_map_intersect_range(
2690 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2692 struct isl_basic_map *bmap_range;
2694 if (!bmap || !bset)
2695 goto error;
2697 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2698 bset->dim, isl_dim_param), goto error);
2700 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2701 isl_assert(bset->ctx,
2702 isl_basic_map_compatible_range(bmap, bset), goto error);
2704 if (isl_basic_set_is_universe(bset)) {
2705 isl_basic_set_free(bset);
2706 return bmap;
2709 bmap = isl_basic_map_cow(bmap);
2710 if (!bmap)
2711 goto error;
2712 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2713 bset->n_div, bset->n_eq, bset->n_ineq);
2714 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2715 bmap = add_constraints(bmap, bmap_range, 0, 0);
2717 bmap = isl_basic_map_simplify(bmap);
2718 return isl_basic_map_finalize(bmap);
2719 error:
2720 isl_basic_map_free(bmap);
2721 isl_basic_set_free(bset);
2722 return NULL;
2725 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2727 int i;
2728 unsigned total;
2729 isl_int s;
2731 total = 1 + isl_basic_map_total_dim(bmap);
2732 if (total != vec->size)
2733 return -1;
2735 isl_int_init(s);
2737 for (i = 0; i < bmap->n_eq; ++i) {
2738 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2739 if (!isl_int_is_zero(s)) {
2740 isl_int_clear(s);
2741 return 0;
2745 for (i = 0; i < bmap->n_ineq; ++i) {
2746 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2747 if (isl_int_is_neg(s)) {
2748 isl_int_clear(s);
2749 return 0;
2753 isl_int_clear(s);
2755 return 1;
2758 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2760 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2763 struct isl_basic_map *isl_basic_map_intersect(
2764 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2766 struct isl_vec *sample = NULL;
2768 if (!bmap1 || !bmap2)
2769 goto error;
2771 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2772 bmap2->dim, isl_dim_param), goto error);
2773 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2774 isl_space_dim(bmap1->dim, isl_dim_param) &&
2775 isl_space_dim(bmap2->dim, isl_dim_all) !=
2776 isl_space_dim(bmap2->dim, isl_dim_param))
2777 return isl_basic_map_intersect(bmap2, bmap1);
2779 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2780 isl_space_dim(bmap2->dim, isl_dim_param))
2781 isl_assert(bmap1->ctx,
2782 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2784 if (bmap1->sample &&
2785 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2786 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2787 sample = isl_vec_copy(bmap1->sample);
2788 else if (bmap2->sample &&
2789 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2790 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2791 sample = isl_vec_copy(bmap2->sample);
2793 bmap1 = isl_basic_map_cow(bmap1);
2794 if (!bmap1)
2795 goto error;
2796 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2797 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2798 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2800 if (!bmap1)
2801 isl_vec_free(sample);
2802 else if (sample) {
2803 isl_vec_free(bmap1->sample);
2804 bmap1->sample = sample;
2807 bmap1 = isl_basic_map_simplify(bmap1);
2808 return isl_basic_map_finalize(bmap1);
2809 error:
2810 if (sample)
2811 isl_vec_free(sample);
2812 isl_basic_map_free(bmap1);
2813 isl_basic_map_free(bmap2);
2814 return NULL;
2817 struct isl_basic_set *isl_basic_set_intersect(
2818 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2820 return (struct isl_basic_set *)
2821 isl_basic_map_intersect(
2822 (struct isl_basic_map *)bset1,
2823 (struct isl_basic_map *)bset2);
2826 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2827 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2829 return isl_basic_set_intersect(bset1, bset2);
2832 /* Special case of isl_map_intersect, where both map1 and map2
2833 * are convex, without any divs and such that either map1 or map2
2834 * contains a single constraint. This constraint is then simply
2835 * added to the other map.
2837 static __isl_give isl_map *map_intersect_add_constraint(
2838 __isl_take isl_map *map1, __isl_take isl_map *map2)
2840 isl_assert(map1->ctx, map1->n == 1, goto error);
2841 isl_assert(map2->ctx, map1->n == 1, goto error);
2842 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2843 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2845 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2846 return isl_map_intersect(map2, map1);
2848 isl_assert(map2->ctx,
2849 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2851 map1 = isl_map_cow(map1);
2852 if (!map1)
2853 goto error;
2854 if (isl_map_plain_is_empty(map1)) {
2855 isl_map_free(map2);
2856 return map1;
2858 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2859 if (map2->p[0]->n_eq == 1)
2860 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2861 else
2862 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2863 map2->p[0]->ineq[0]);
2865 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2866 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2867 if (!map1->p[0])
2868 goto error;
2870 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2871 isl_basic_map_free(map1->p[0]);
2872 map1->n = 0;
2875 isl_map_free(map2);
2877 return map1;
2878 error:
2879 isl_map_free(map1);
2880 isl_map_free(map2);
2881 return NULL;
2884 /* map2 may be either a parameter domain or a map living in the same
2885 * space as map1.
2887 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2888 __isl_take isl_map *map2)
2890 unsigned flags = 0;
2891 isl_map *result;
2892 int i, j;
2894 if (!map1 || !map2)
2895 goto error;
2897 if ((isl_map_plain_is_empty(map1) ||
2898 isl_map_plain_is_universe(map2)) &&
2899 isl_space_is_equal(map1->dim, map2->dim)) {
2900 isl_map_free(map2);
2901 return map1;
2903 if ((isl_map_plain_is_empty(map2) ||
2904 isl_map_plain_is_universe(map1)) &&
2905 isl_space_is_equal(map1->dim, map2->dim)) {
2906 isl_map_free(map1);
2907 return map2;
2910 if (map1->n == 1 && map2->n == 1 &&
2911 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2912 isl_space_is_equal(map1->dim, map2->dim) &&
2913 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2914 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2915 return map_intersect_add_constraint(map1, map2);
2917 if (isl_space_dim(map2->dim, isl_dim_all) !=
2918 isl_space_dim(map2->dim, isl_dim_param))
2919 isl_assert(map1->ctx,
2920 isl_space_is_equal(map1->dim, map2->dim), goto error);
2922 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2923 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2924 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2926 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2927 map1->n * map2->n, flags);
2928 if (!result)
2929 goto error;
2930 for (i = 0; i < map1->n; ++i)
2931 for (j = 0; j < map2->n; ++j) {
2932 struct isl_basic_map *part;
2933 part = isl_basic_map_intersect(
2934 isl_basic_map_copy(map1->p[i]),
2935 isl_basic_map_copy(map2->p[j]));
2936 if (isl_basic_map_is_empty(part) < 0)
2937 part = isl_basic_map_free(part);
2938 result = isl_map_add_basic_map(result, part);
2939 if (!result)
2940 goto error;
2942 isl_map_free(map1);
2943 isl_map_free(map2);
2944 return result;
2945 error:
2946 isl_map_free(map1);
2947 isl_map_free(map2);
2948 return NULL;
2951 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2952 __isl_take isl_map *map2)
2954 if (!map1 || !map2)
2955 goto error;
2956 if (!isl_space_is_equal(map1->dim, map2->dim))
2957 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2958 "spaces don't match", goto error);
2959 return map_intersect_internal(map1, map2);
2960 error:
2961 isl_map_free(map1);
2962 isl_map_free(map2);
2963 return NULL;
2966 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2967 __isl_take isl_map *map2)
2969 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2972 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2974 return (struct isl_set *)
2975 isl_map_intersect((struct isl_map *)set1,
2976 (struct isl_map *)set2);
2979 /* map_intersect_internal accepts intersections
2980 * with parameter domains, so we can just call that function.
2982 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2983 __isl_take isl_set *params)
2985 return map_intersect_internal(map, params);
2988 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2989 __isl_take isl_map *map2)
2991 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2994 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2995 __isl_take isl_set *params)
2997 return isl_map_intersect_params(set, params);
3000 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3002 isl_space *dim;
3003 struct isl_basic_set *bset;
3004 unsigned in;
3006 if (!bmap)
3007 return NULL;
3008 bmap = isl_basic_map_cow(bmap);
3009 if (!bmap)
3010 return NULL;
3011 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3012 in = isl_basic_map_n_in(bmap);
3013 bset = isl_basic_set_from_basic_map(bmap);
3014 bset = isl_basic_set_swap_vars(bset, in);
3015 return isl_basic_map_from_basic_set(bset, dim);
3018 static __isl_give isl_basic_map *basic_map_space_reset(
3019 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3021 isl_space *space;
3023 if (!bmap)
3024 return NULL;
3025 if (!isl_space_is_named_or_nested(bmap->dim, type))
3026 return bmap;
3028 space = isl_basic_map_get_space(bmap);
3029 space = isl_space_reset(space, type);
3030 bmap = isl_basic_map_reset_space(bmap, space);
3031 return bmap;
3034 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3035 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3036 unsigned pos, unsigned n)
3038 isl_space *res_dim;
3039 struct isl_basic_map *res;
3040 struct isl_dim_map *dim_map;
3041 unsigned total, off;
3042 enum isl_dim_type t;
3044 if (n == 0)
3045 return basic_map_space_reset(bmap, type);
3047 if (!bmap)
3048 return NULL;
3050 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3052 total = isl_basic_map_total_dim(bmap) + n;
3053 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3054 off = 0;
3055 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3056 if (t != type) {
3057 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3058 } else {
3059 unsigned size = isl_basic_map_dim(bmap, t);
3060 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3061 0, pos, off);
3062 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3063 pos, size - pos, off + pos + n);
3065 off += isl_space_dim(res_dim, t);
3067 isl_dim_map_div(dim_map, bmap, off);
3069 res = isl_basic_map_alloc_space(res_dim,
3070 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3071 if (isl_basic_map_is_rational(bmap))
3072 res = isl_basic_map_set_rational(res);
3073 if (isl_basic_map_plain_is_empty(bmap)) {
3074 isl_basic_map_free(bmap);
3075 free(dim_map);
3076 return isl_basic_map_set_to_empty(res);
3078 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3079 return isl_basic_map_finalize(res);
3082 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3083 __isl_take isl_basic_set *bset,
3084 enum isl_dim_type type, unsigned pos, unsigned n)
3086 return isl_basic_map_insert_dims(bset, type, pos, n);
3089 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3090 enum isl_dim_type type, unsigned n)
3092 if (!bmap)
3093 return NULL;
3094 return isl_basic_map_insert_dims(bmap, type,
3095 isl_basic_map_dim(bmap, type), n);
3098 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3099 enum isl_dim_type type, unsigned n)
3101 if (!bset)
3102 return NULL;
3103 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3104 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3105 error:
3106 isl_basic_set_free(bset);
3107 return NULL;
3110 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3111 enum isl_dim_type type)
3113 isl_space *space;
3115 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3116 return map;
3118 space = isl_map_get_space(map);
3119 space = isl_space_reset(space, type);
3120 map = isl_map_reset_space(map, space);
3121 return map;
3124 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3125 enum isl_dim_type type, unsigned pos, unsigned n)
3127 int i;
3129 if (n == 0)
3130 return map_space_reset(map, type);
3132 map = isl_map_cow(map);
3133 if (!map)
3134 return NULL;
3136 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3137 if (!map->dim)
3138 goto error;
3140 for (i = 0; i < map->n; ++i) {
3141 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3142 if (!map->p[i])
3143 goto error;
3146 return map;
3147 error:
3148 isl_map_free(map);
3149 return NULL;
3152 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3153 enum isl_dim_type type, unsigned pos, unsigned n)
3155 return isl_map_insert_dims(set, type, pos, n);
3158 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3159 enum isl_dim_type type, unsigned n)
3161 if (!map)
3162 return NULL;
3163 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3166 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3167 enum isl_dim_type type, unsigned n)
3169 if (!set)
3170 return NULL;
3171 isl_assert(set->ctx, type != isl_dim_in, goto error);
3172 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3173 error:
3174 isl_set_free(set);
3175 return NULL;
3178 __isl_give isl_basic_map *isl_basic_map_move_dims(
3179 __isl_take isl_basic_map *bmap,
3180 enum isl_dim_type dst_type, unsigned dst_pos,
3181 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3183 struct isl_dim_map *dim_map;
3184 struct isl_basic_map *res;
3185 enum isl_dim_type t;
3186 unsigned total, off;
3188 if (!bmap)
3189 return NULL;
3190 if (n == 0)
3191 return bmap;
3193 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3194 goto error);
3196 if (dst_type == src_type && dst_pos == src_pos)
3197 return bmap;
3199 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3201 if (pos(bmap->dim, dst_type) + dst_pos ==
3202 pos(bmap->dim, src_type) + src_pos +
3203 ((src_type < dst_type) ? n : 0)) {
3204 bmap = isl_basic_map_cow(bmap);
3205 if (!bmap)
3206 return NULL;
3208 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3209 src_type, src_pos, n);
3210 if (!bmap->dim)
3211 goto error;
3213 bmap = isl_basic_map_finalize(bmap);
3215 return bmap;
3218 total = isl_basic_map_total_dim(bmap);
3219 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3221 off = 0;
3222 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3223 unsigned size = isl_space_dim(bmap->dim, t);
3224 if (t == dst_type) {
3225 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3226 0, dst_pos, off);
3227 off += dst_pos;
3228 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3229 src_pos, n, off);
3230 off += n;
3231 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3232 dst_pos, size - dst_pos, off);
3233 off += size - dst_pos;
3234 } else if (t == src_type) {
3235 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3236 0, src_pos, off);
3237 off += src_pos;
3238 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3239 src_pos + n, size - src_pos - n, off);
3240 off += size - src_pos - n;
3241 } else {
3242 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3243 off += size;
3246 isl_dim_map_div(dim_map, bmap, off);
3248 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3249 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3250 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3251 if (!bmap)
3252 goto error;
3254 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3255 src_type, src_pos, n);
3256 if (!bmap->dim)
3257 goto error;
3259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3260 bmap = isl_basic_map_gauss(bmap, NULL);
3261 bmap = isl_basic_map_finalize(bmap);
3263 return bmap;
3264 error:
3265 isl_basic_map_free(bmap);
3266 return NULL;
3269 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3270 enum isl_dim_type dst_type, unsigned dst_pos,
3271 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3273 return (isl_basic_set *)isl_basic_map_move_dims(
3274 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3277 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3278 enum isl_dim_type dst_type, unsigned dst_pos,
3279 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3281 if (!set)
3282 return NULL;
3283 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3284 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3285 src_type, src_pos, n);
3286 error:
3287 isl_set_free(set);
3288 return NULL;
3291 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3292 enum isl_dim_type dst_type, unsigned dst_pos,
3293 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3295 int i;
3297 if (!map)
3298 return NULL;
3299 if (n == 0)
3300 return map;
3302 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3303 goto error);
3305 if (dst_type == src_type && dst_pos == src_pos)
3306 return map;
3308 isl_assert(map->ctx, dst_type != src_type, goto error);
3310 map = isl_map_cow(map);
3311 if (!map)
3312 return NULL;
3314 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3315 if (!map->dim)
3316 goto error;
3318 for (i = 0; i < map->n; ++i) {
3319 map->p[i] = isl_basic_map_move_dims(map->p[i],
3320 dst_type, dst_pos,
3321 src_type, src_pos, n);
3322 if (!map->p[i])
3323 goto error;
3326 return map;
3327 error:
3328 isl_map_free(map);
3329 return NULL;
3332 /* Move the specified dimensions to the last columns right before
3333 * the divs. Don't change the dimension specification of bmap.
3334 * That's the responsibility of the caller.
3336 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3337 enum isl_dim_type type, unsigned first, unsigned n)
3339 struct isl_dim_map *dim_map;
3340 struct isl_basic_map *res;
3341 enum isl_dim_type t;
3342 unsigned total, off;
3344 if (!bmap)
3345 return NULL;
3346 if (pos(bmap->dim, type) + first + n ==
3347 1 + isl_space_dim(bmap->dim, isl_dim_all))
3348 return bmap;
3350 total = isl_basic_map_total_dim(bmap);
3351 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3353 off = 0;
3354 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3355 unsigned size = isl_space_dim(bmap->dim, t);
3356 if (t == type) {
3357 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3358 0, first, off);
3359 off += first;
3360 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3361 first, n, total - bmap->n_div - n);
3362 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3363 first + n, size - (first + n), off);
3364 off += size - (first + n);
3365 } else {
3366 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3367 off += size;
3370 isl_dim_map_div(dim_map, bmap, off + n);
3372 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3373 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3374 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3375 return res;
3378 /* Insert "n" rows in the divs of "bmap".
3380 * The number of columns is not changed, which means that the last
3381 * dimensions of "bmap" are being reintepreted as the new divs.
3382 * The space of "bmap" is not adjusted, however, which means
3383 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3384 * from the space of "bmap" is the responsibility of the caller.
3386 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3387 int n)
3389 int i;
3390 size_t row_size;
3391 isl_int **new_div;
3392 isl_int *old;
3394 bmap = isl_basic_map_cow(bmap);
3395 if (!bmap)
3396 return NULL;
3398 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3399 old = bmap->block2.data;
3400 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3401 (bmap->extra + n) * (1 + row_size));
3402 if (!bmap->block2.data)
3403 return isl_basic_map_free(bmap);
3404 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3405 if (!new_div)
3406 return isl_basic_map_free(bmap);
3407 for (i = 0; i < n; ++i) {
3408 new_div[i] = bmap->block2.data +
3409 (bmap->extra + i) * (1 + row_size);
3410 isl_seq_clr(new_div[i], 1 + row_size);
3412 for (i = 0; i < bmap->extra; ++i)
3413 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3414 free(bmap->div);
3415 bmap->div = new_div;
3416 bmap->n_div += n;
3417 bmap->extra += n;
3419 return bmap;
3422 /* Turn the n dimensions of type type, starting at first
3423 * into existentially quantified variables.
3425 __isl_give isl_basic_map *isl_basic_map_project_out(
3426 __isl_take isl_basic_map *bmap,
3427 enum isl_dim_type type, unsigned first, unsigned n)
3429 if (n == 0)
3430 return basic_map_space_reset(bmap, type);
3432 if (!bmap)
3433 return NULL;
3435 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3436 return isl_basic_map_remove_dims(bmap, type, first, n);
3438 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3439 goto error);
3441 bmap = move_last(bmap, type, first, n);
3442 bmap = isl_basic_map_cow(bmap);
3443 bmap = insert_div_rows(bmap, n);
3444 if (!bmap)
3445 return NULL;
3447 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3448 if (!bmap->dim)
3449 goto error;
3450 bmap = isl_basic_map_simplify(bmap);
3451 bmap = isl_basic_map_drop_redundant_divs(bmap);
3452 return isl_basic_map_finalize(bmap);
3453 error:
3454 isl_basic_map_free(bmap);
3455 return NULL;
3458 /* Turn the n dimensions of type type, starting at first
3459 * into existentially quantified variables.
3461 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3462 enum isl_dim_type type, unsigned first, unsigned n)
3464 return (isl_basic_set *)isl_basic_map_project_out(
3465 (isl_basic_map *)bset, type, first, n);
3468 /* Turn the n dimensions of type type, starting at first
3469 * into existentially quantified variables.
3471 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3472 enum isl_dim_type type, unsigned first, unsigned n)
3474 int i;
3476 if (!map)
3477 return NULL;
3479 if (n == 0)
3480 return map_space_reset(map, type);
3482 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3484 map = isl_map_cow(map);
3485 if (!map)
3486 return NULL;
3488 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3489 if (!map->dim)
3490 goto error;
3492 for (i = 0; i < map->n; ++i) {
3493 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3494 if (!map->p[i])
3495 goto error;
3498 return map;
3499 error:
3500 isl_map_free(map);
3501 return NULL;
3504 /* Turn the n dimensions of type type, starting at first
3505 * into existentially quantified variables.
3507 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3508 enum isl_dim_type type, unsigned first, unsigned n)
3510 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3513 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3515 int i, j;
3517 for (i = 0; i < n; ++i) {
3518 j = isl_basic_map_alloc_div(bmap);
3519 if (j < 0)
3520 goto error;
3521 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3523 return bmap;
3524 error:
3525 isl_basic_map_free(bmap);
3526 return NULL;
3529 struct isl_basic_map *isl_basic_map_apply_range(
3530 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3532 isl_space *dim_result = NULL;
3533 struct isl_basic_map *bmap;
3534 unsigned n_in, n_out, n, nparam, total, pos;
3535 struct isl_dim_map *dim_map1, *dim_map2;
3537 if (!bmap1 || !bmap2)
3538 goto error;
3540 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3541 isl_space_copy(bmap2->dim));
3543 n_in = isl_basic_map_n_in(bmap1);
3544 n_out = isl_basic_map_n_out(bmap2);
3545 n = isl_basic_map_n_out(bmap1);
3546 nparam = isl_basic_map_n_param(bmap1);
3548 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3549 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3550 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3551 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3552 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3553 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3554 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3555 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3556 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3557 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3558 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3560 bmap = isl_basic_map_alloc_space(dim_result,
3561 bmap1->n_div + bmap2->n_div + n,
3562 bmap1->n_eq + bmap2->n_eq,
3563 bmap1->n_ineq + bmap2->n_ineq);
3564 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3565 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3566 bmap = add_divs(bmap, n);
3567 bmap = isl_basic_map_simplify(bmap);
3568 bmap = isl_basic_map_drop_redundant_divs(bmap);
3569 return isl_basic_map_finalize(bmap);
3570 error:
3571 isl_basic_map_free(bmap1);
3572 isl_basic_map_free(bmap2);
3573 return NULL;
3576 struct isl_basic_set *isl_basic_set_apply(
3577 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3579 if (!bset || !bmap)
3580 goto error;
3582 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3583 goto error);
3585 return (struct isl_basic_set *)
3586 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3587 error:
3588 isl_basic_set_free(bset);
3589 isl_basic_map_free(bmap);
3590 return NULL;
3593 struct isl_basic_map *isl_basic_map_apply_domain(
3594 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3596 if (!bmap1 || !bmap2)
3597 goto error;
3599 isl_assert(bmap1->ctx,
3600 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3601 isl_assert(bmap1->ctx,
3602 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3603 goto error);
3605 bmap1 = isl_basic_map_reverse(bmap1);
3606 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3607 return isl_basic_map_reverse(bmap1);
3608 error:
3609 isl_basic_map_free(bmap1);
3610 isl_basic_map_free(bmap2);
3611 return NULL;
3614 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3615 * A \cap B -> f(A) + f(B)
3617 struct isl_basic_map *isl_basic_map_sum(
3618 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3620 unsigned n_in, n_out, nparam, total, pos;
3621 struct isl_basic_map *bmap = NULL;
3622 struct isl_dim_map *dim_map1, *dim_map2;
3623 int i;
3625 if (!bmap1 || !bmap2)
3626 goto error;
3628 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3629 goto error);
3631 nparam = isl_basic_map_n_param(bmap1);
3632 n_in = isl_basic_map_n_in(bmap1);
3633 n_out = isl_basic_map_n_out(bmap1);
3635 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3636 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3637 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3638 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3639 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3640 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3641 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3642 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3643 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3644 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3645 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3647 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3648 bmap1->n_div + bmap2->n_div + 2 * n_out,
3649 bmap1->n_eq + bmap2->n_eq + n_out,
3650 bmap1->n_ineq + bmap2->n_ineq);
3651 for (i = 0; i < n_out; ++i) {
3652 int j = isl_basic_map_alloc_equality(bmap);
3653 if (j < 0)
3654 goto error;
3655 isl_seq_clr(bmap->eq[j], 1+total);
3656 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3657 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3658 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3660 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3661 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3662 bmap = add_divs(bmap, 2 * n_out);
3664 bmap = isl_basic_map_simplify(bmap);
3665 return isl_basic_map_finalize(bmap);
3666 error:
3667 isl_basic_map_free(bmap);
3668 isl_basic_map_free(bmap1);
3669 isl_basic_map_free(bmap2);
3670 return NULL;
3673 /* Given two maps A -> f(A) and B -> g(B), construct a map
3674 * A \cap B -> f(A) + f(B)
3676 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3678 struct isl_map *result;
3679 int i, j;
3681 if (!map1 || !map2)
3682 goto error;
3684 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3686 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3687 map1->n * map2->n, 0);
3688 if (!result)
3689 goto error;
3690 for (i = 0; i < map1->n; ++i)
3691 for (j = 0; j < map2->n; ++j) {
3692 struct isl_basic_map *part;
3693 part = isl_basic_map_sum(
3694 isl_basic_map_copy(map1->p[i]),
3695 isl_basic_map_copy(map2->p[j]));
3696 if (isl_basic_map_is_empty(part))
3697 isl_basic_map_free(part);
3698 else
3699 result = isl_map_add_basic_map(result, part);
3700 if (!result)
3701 goto error;
3703 isl_map_free(map1);
3704 isl_map_free(map2);
3705 return result;
3706 error:
3707 isl_map_free(map1);
3708 isl_map_free(map2);
3709 return NULL;
3712 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3713 __isl_take isl_set *set2)
3715 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3718 /* Given a basic map A -> f(A), construct A -> -f(A).
3720 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3722 int i, j;
3723 unsigned off, n;
3725 bmap = isl_basic_map_cow(bmap);
3726 if (!bmap)
3727 return NULL;
3729 n = isl_basic_map_dim(bmap, isl_dim_out);
3730 off = isl_basic_map_offset(bmap, isl_dim_out);
3731 for (i = 0; i < bmap->n_eq; ++i)
3732 for (j = 0; j < n; ++j)
3733 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3734 for (i = 0; i < bmap->n_ineq; ++i)
3735 for (j = 0; j < n; ++j)
3736 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3737 for (i = 0; i < bmap->n_div; ++i)
3738 for (j = 0; j < n; ++j)
3739 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3740 bmap = isl_basic_map_gauss(bmap, NULL);
3741 return isl_basic_map_finalize(bmap);
3744 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3746 return isl_basic_map_neg(bset);
3749 /* Given a map A -> f(A), construct A -> -f(A).
3751 struct isl_map *isl_map_neg(struct isl_map *map)
3753 int i;
3755 map = isl_map_cow(map);
3756 if (!map)
3757 return NULL;
3759 for (i = 0; i < map->n; ++i) {
3760 map->p[i] = isl_basic_map_neg(map->p[i]);
3761 if (!map->p[i])
3762 goto error;
3765 return map;
3766 error:
3767 isl_map_free(map);
3768 return NULL;
3771 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3773 return (isl_set *)isl_map_neg((isl_map *)set);
3776 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3777 * A -> floor(f(A)/d).
3779 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3780 isl_int d)
3782 unsigned n_in, n_out, nparam, total, pos;
3783 struct isl_basic_map *result = NULL;
3784 struct isl_dim_map *dim_map;
3785 int i;
3787 if (!bmap)
3788 return NULL;
3790 nparam = isl_basic_map_n_param(bmap);
3791 n_in = isl_basic_map_n_in(bmap);
3792 n_out = isl_basic_map_n_out(bmap);
3794 total = nparam + n_in + n_out + bmap->n_div + n_out;
3795 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3796 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3797 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3798 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3799 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3801 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3802 bmap->n_div + n_out,
3803 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3804 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3805 result = add_divs(result, n_out);
3806 for (i = 0; i < n_out; ++i) {
3807 int j;
3808 j = isl_basic_map_alloc_inequality(result);
3809 if (j < 0)
3810 goto error;
3811 isl_seq_clr(result->ineq[j], 1+total);
3812 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3813 isl_int_set_si(result->ineq[j][1+pos+i], 1);
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_set(result->ineq[j][1+nparam+n_in+i], d);
3819 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3820 isl_int_sub_ui(result->ineq[j][0], d, 1);
3823 result = isl_basic_map_simplify(result);
3824 return isl_basic_map_finalize(result);
3825 error:
3826 isl_basic_map_free(result);
3827 return NULL;
3830 /* Given a map A -> f(A) and an integer d, construct a map
3831 * A -> floor(f(A)/d).
3833 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3835 int i;
3837 map = isl_map_cow(map);
3838 if (!map)
3839 return NULL;
3841 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3842 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3843 for (i = 0; i < map->n; ++i) {
3844 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3845 if (!map->p[i])
3846 goto error;
3849 return map;
3850 error:
3851 isl_map_free(map);
3852 return NULL;
3855 /* Given a map A -> f(A) and an integer d, construct a map
3856 * A -> floor(f(A)/d).
3858 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3859 __isl_take isl_val *d)
3861 if (!map || !d)
3862 goto error;
3863 if (!isl_val_is_int(d))
3864 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3865 "expecting integer denominator", goto error);
3866 map = isl_map_floordiv(map, d->n);
3867 isl_val_free(d);
3868 return map;
3869 error:
3870 isl_map_free(map);
3871 isl_val_free(d);
3872 return NULL;
3875 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3877 int i;
3878 unsigned nparam;
3879 unsigned n_in;
3881 i = isl_basic_map_alloc_equality(bmap);
3882 if (i < 0)
3883 goto error;
3884 nparam = isl_basic_map_n_param(bmap);
3885 n_in = isl_basic_map_n_in(bmap);
3886 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3887 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3888 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3889 return isl_basic_map_finalize(bmap);
3890 error:
3891 isl_basic_map_free(bmap);
3892 return NULL;
3895 /* Add a constraints to "bmap" expressing i_pos < o_pos
3897 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3899 int i;
3900 unsigned nparam;
3901 unsigned n_in;
3903 i = isl_basic_map_alloc_inequality(bmap);
3904 if (i < 0)
3905 goto error;
3906 nparam = isl_basic_map_n_param(bmap);
3907 n_in = isl_basic_map_n_in(bmap);
3908 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3909 isl_int_set_si(bmap->ineq[i][0], -1);
3910 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3911 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3912 return isl_basic_map_finalize(bmap);
3913 error:
3914 isl_basic_map_free(bmap);
3915 return NULL;
3918 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3920 static __isl_give isl_basic_map *var_less_or_equal(
3921 __isl_take isl_basic_map *bmap, unsigned pos)
3923 int i;
3924 unsigned nparam;
3925 unsigned n_in;
3927 i = isl_basic_map_alloc_inequality(bmap);
3928 if (i < 0)
3929 goto error;
3930 nparam = isl_basic_map_n_param(bmap);
3931 n_in = isl_basic_map_n_in(bmap);
3932 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3933 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3934 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3935 return isl_basic_map_finalize(bmap);
3936 error:
3937 isl_basic_map_free(bmap);
3938 return NULL;
3941 /* Add a constraints to "bmap" expressing i_pos > o_pos
3943 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3945 int i;
3946 unsigned nparam;
3947 unsigned n_in;
3949 i = isl_basic_map_alloc_inequality(bmap);
3950 if (i < 0)
3951 goto error;
3952 nparam = isl_basic_map_n_param(bmap);
3953 n_in = isl_basic_map_n_in(bmap);
3954 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3955 isl_int_set_si(bmap->ineq[i][0], -1);
3956 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3957 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3958 return isl_basic_map_finalize(bmap);
3959 error:
3960 isl_basic_map_free(bmap);
3961 return NULL;
3964 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3966 static __isl_give isl_basic_map *var_more_or_equal(
3967 __isl_take isl_basic_map *bmap, unsigned pos)
3969 int i;
3970 unsigned nparam;
3971 unsigned n_in;
3973 i = isl_basic_map_alloc_inequality(bmap);
3974 if (i < 0)
3975 goto error;
3976 nparam = isl_basic_map_n_param(bmap);
3977 n_in = isl_basic_map_n_in(bmap);
3978 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3979 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3980 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3981 return isl_basic_map_finalize(bmap);
3982 error:
3983 isl_basic_map_free(bmap);
3984 return NULL;
3987 __isl_give isl_basic_map *isl_basic_map_equal(
3988 __isl_take isl_space *dim, unsigned n_equal)
3990 int i;
3991 struct isl_basic_map *bmap;
3992 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3993 if (!bmap)
3994 return NULL;
3995 for (i = 0; i < n_equal && bmap; ++i)
3996 bmap = var_equal(bmap, i);
3997 return isl_basic_map_finalize(bmap);
4000 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4002 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4003 unsigned pos)
4005 int i;
4006 struct isl_basic_map *bmap;
4007 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4008 if (!bmap)
4009 return NULL;
4010 for (i = 0; i < pos && bmap; ++i)
4011 bmap = var_equal(bmap, i);
4012 if (bmap)
4013 bmap = var_less(bmap, pos);
4014 return isl_basic_map_finalize(bmap);
4017 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4019 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4020 __isl_take isl_space *dim, unsigned pos)
4022 int i;
4023 isl_basic_map *bmap;
4025 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4026 for (i = 0; i < pos; ++i)
4027 bmap = var_equal(bmap, i);
4028 bmap = var_less_or_equal(bmap, pos);
4029 return isl_basic_map_finalize(bmap);
4032 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4034 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4035 unsigned pos)
4037 int i;
4038 struct isl_basic_map *bmap;
4039 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4040 if (!bmap)
4041 return NULL;
4042 for (i = 0; i < pos && bmap; ++i)
4043 bmap = var_equal(bmap, i);
4044 if (bmap)
4045 bmap = var_more(bmap, pos);
4046 return isl_basic_map_finalize(bmap);
4049 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4051 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4052 __isl_take isl_space *dim, unsigned pos)
4054 int i;
4055 isl_basic_map *bmap;
4057 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4058 for (i = 0; i < pos; ++i)
4059 bmap = var_equal(bmap, i);
4060 bmap = var_more_or_equal(bmap, pos);
4061 return isl_basic_map_finalize(bmap);
4064 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4065 unsigned n, int equal)
4067 struct isl_map *map;
4068 int i;
4070 if (n == 0 && equal)
4071 return isl_map_universe(dims);
4073 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4075 for (i = 0; i + 1 < n; ++i)
4076 map = isl_map_add_basic_map(map,
4077 isl_basic_map_less_at(isl_space_copy(dims), i));
4078 if (n > 0) {
4079 if (equal)
4080 map = isl_map_add_basic_map(map,
4081 isl_basic_map_less_or_equal_at(dims, n - 1));
4082 else
4083 map = isl_map_add_basic_map(map,
4084 isl_basic_map_less_at(dims, n - 1));
4085 } else
4086 isl_space_free(dims);
4088 return map;
4091 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4093 if (!dims)
4094 return NULL;
4095 return map_lex_lte_first(dims, dims->n_out, equal);
4098 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4100 return map_lex_lte_first(dim, n, 0);
4103 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4105 return map_lex_lte_first(dim, n, 1);
4108 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4110 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4113 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4115 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4118 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4119 unsigned n, int equal)
4121 struct isl_map *map;
4122 int i;
4124 if (n == 0 && equal)
4125 return isl_map_universe(dims);
4127 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4129 for (i = 0; i + 1 < n; ++i)
4130 map = isl_map_add_basic_map(map,
4131 isl_basic_map_more_at(isl_space_copy(dims), i));
4132 if (n > 0) {
4133 if (equal)
4134 map = isl_map_add_basic_map(map,
4135 isl_basic_map_more_or_equal_at(dims, n - 1));
4136 else
4137 map = isl_map_add_basic_map(map,
4138 isl_basic_map_more_at(dims, n - 1));
4139 } else
4140 isl_space_free(dims);
4142 return map;
4145 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4147 if (!dims)
4148 return NULL;
4149 return map_lex_gte_first(dims, dims->n_out, equal);
4152 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4154 return map_lex_gte_first(dim, n, 0);
4157 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4159 return map_lex_gte_first(dim, n, 1);
4162 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4164 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4167 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4169 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4172 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4173 __isl_take isl_set *set2)
4175 isl_map *map;
4176 map = isl_map_lex_le(isl_set_get_space(set1));
4177 map = isl_map_intersect_domain(map, set1);
4178 map = isl_map_intersect_range(map, set2);
4179 return map;
4182 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4183 __isl_take isl_set *set2)
4185 isl_map *map;
4186 map = isl_map_lex_lt(isl_set_get_space(set1));
4187 map = isl_map_intersect_domain(map, set1);
4188 map = isl_map_intersect_range(map, set2);
4189 return map;
4192 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4193 __isl_take isl_set *set2)
4195 isl_map *map;
4196 map = isl_map_lex_ge(isl_set_get_space(set1));
4197 map = isl_map_intersect_domain(map, set1);
4198 map = isl_map_intersect_range(map, set2);
4199 return map;
4202 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4203 __isl_take isl_set *set2)
4205 isl_map *map;
4206 map = isl_map_lex_gt(isl_set_get_space(set1));
4207 map = isl_map_intersect_domain(map, set1);
4208 map = isl_map_intersect_range(map, set2);
4209 return map;
4212 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4213 __isl_take isl_map *map2)
4215 isl_map *map;
4216 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4217 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4218 map = isl_map_apply_range(map, isl_map_reverse(map2));
4219 return map;
4222 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4223 __isl_take isl_map *map2)
4225 isl_map *map;
4226 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4227 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4228 map = isl_map_apply_range(map, isl_map_reverse(map2));
4229 return map;
4232 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4233 __isl_take isl_map *map2)
4235 isl_map *map;
4236 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4237 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4238 map = isl_map_apply_range(map, isl_map_reverse(map2));
4239 return map;
4242 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4243 __isl_take isl_map *map2)
4245 isl_map *map;
4246 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4247 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4248 map = isl_map_apply_range(map, isl_map_reverse(map2));
4249 return map;
4252 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4253 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4255 struct isl_basic_map *bmap;
4257 bset = isl_basic_set_cow(bset);
4258 if (!bset || !dim)
4259 goto error;
4261 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4262 isl_space_free(bset->dim);
4263 bmap = (struct isl_basic_map *) bset;
4264 bmap->dim = dim;
4265 return isl_basic_map_finalize(bmap);
4266 error:
4267 isl_basic_set_free(bset);
4268 isl_space_free(dim);
4269 return NULL;
4272 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4274 if (!bmap)
4275 goto error;
4276 if (bmap->dim->n_in == 0)
4277 return (struct isl_basic_set *)bmap;
4278 bmap = isl_basic_map_cow(bmap);
4279 if (!bmap)
4280 goto error;
4281 bmap->dim = isl_space_as_set_space(bmap->dim);
4282 if (!bmap->dim)
4283 goto error;
4284 bmap = isl_basic_map_finalize(bmap);
4285 return (struct isl_basic_set *)bmap;
4286 error:
4287 isl_basic_map_free(bmap);
4288 return NULL;
4291 /* For a div d = floor(f/m), add the constraints
4293 * f - m d >= 0
4294 * -(f-(n-1)) + m d >= 0
4296 * Note that the second constraint is the negation of
4298 * f - m d >= n
4300 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4301 unsigned pos, isl_int *div)
4303 int i, j;
4304 unsigned total = isl_basic_map_total_dim(bmap);
4306 i = isl_basic_map_alloc_inequality(bmap);
4307 if (i < 0)
4308 return -1;
4309 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4310 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4312 j = isl_basic_map_alloc_inequality(bmap);
4313 if (j < 0)
4314 return -1;
4315 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4316 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4317 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4318 return j;
4321 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4322 unsigned pos, isl_int *div)
4324 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4325 pos, div);
4328 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4330 unsigned total = isl_basic_map_total_dim(bmap);
4331 unsigned div_pos = total - bmap->n_div + div;
4333 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4334 bmap->div[div]);
4337 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4339 return isl_basic_map_add_div_constraints(bset, div);
4342 struct isl_basic_set *isl_basic_map_underlying_set(
4343 struct isl_basic_map *bmap)
4345 if (!bmap)
4346 goto error;
4347 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4348 bmap->n_div == 0 &&
4349 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4350 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4351 return (struct isl_basic_set *)bmap;
4352 bmap = isl_basic_map_cow(bmap);
4353 if (!bmap)
4354 goto error;
4355 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4356 if (!bmap->dim)
4357 goto error;
4358 bmap->extra -= bmap->n_div;
4359 bmap->n_div = 0;
4360 bmap = isl_basic_map_finalize(bmap);
4361 return (struct isl_basic_set *)bmap;
4362 error:
4363 isl_basic_map_free(bmap);
4364 return NULL;
4367 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4368 __isl_take isl_basic_set *bset)
4370 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4373 struct isl_basic_map *isl_basic_map_overlying_set(
4374 struct isl_basic_set *bset, struct isl_basic_map *like)
4376 struct isl_basic_map *bmap;
4377 struct isl_ctx *ctx;
4378 unsigned total;
4379 int i;
4381 if (!bset || !like)
4382 goto error;
4383 ctx = bset->ctx;
4384 isl_assert(ctx, bset->n_div == 0, goto error);
4385 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4386 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4387 goto error);
4388 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4389 isl_basic_map_free(like);
4390 return (struct isl_basic_map *)bset;
4392 bset = isl_basic_set_cow(bset);
4393 if (!bset)
4394 goto error;
4395 total = bset->dim->n_out + bset->extra;
4396 bmap = (struct isl_basic_map *)bset;
4397 isl_space_free(bmap->dim);
4398 bmap->dim = isl_space_copy(like->dim);
4399 if (!bmap->dim)
4400 goto error;
4401 bmap->n_div = like->n_div;
4402 bmap->extra += like->n_div;
4403 if (bmap->extra) {
4404 unsigned ltotal;
4405 isl_int **div;
4406 ltotal = total - bmap->extra + like->extra;
4407 if (ltotal > total)
4408 ltotal = total;
4409 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4410 bmap->extra * (1 + 1 + total));
4411 if (isl_blk_is_error(bmap->block2))
4412 goto error;
4413 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4414 if (!div)
4415 goto error;
4416 bmap->div = div;
4417 for (i = 0; i < bmap->extra; ++i)
4418 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4419 for (i = 0; i < like->n_div; ++i) {
4420 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4421 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4423 bmap = isl_basic_map_extend_constraints(bmap,
4424 0, 2 * like->n_div);
4425 for (i = 0; i < like->n_div; ++i) {
4426 if (!bmap)
4427 break;
4428 if (isl_int_is_zero(bmap->div[i][0]))
4429 continue;
4430 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4431 bmap = isl_basic_map_free(bmap);
4434 isl_basic_map_free(like);
4435 bmap = isl_basic_map_simplify(bmap);
4436 bmap = isl_basic_map_finalize(bmap);
4437 return bmap;
4438 error:
4439 isl_basic_map_free(like);
4440 isl_basic_set_free(bset);
4441 return NULL;
4444 struct isl_basic_set *isl_basic_set_from_underlying_set(
4445 struct isl_basic_set *bset, struct isl_basic_set *like)
4447 return (struct isl_basic_set *)
4448 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4451 struct isl_set *isl_set_from_underlying_set(
4452 struct isl_set *set, struct isl_basic_set *like)
4454 int i;
4456 if (!set || !like)
4457 goto error;
4458 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4459 goto error);
4460 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4461 isl_basic_set_free(like);
4462 return set;
4464 set = isl_set_cow(set);
4465 if (!set)
4466 goto error;
4467 for (i = 0; i < set->n; ++i) {
4468 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4469 isl_basic_set_copy(like));
4470 if (!set->p[i])
4471 goto error;
4473 isl_space_free(set->dim);
4474 set->dim = isl_space_copy(like->dim);
4475 if (!set->dim)
4476 goto error;
4477 isl_basic_set_free(like);
4478 return set;
4479 error:
4480 isl_basic_set_free(like);
4481 isl_set_free(set);
4482 return NULL;
4485 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4487 int i;
4489 map = isl_map_cow(map);
4490 if (!map)
4491 return NULL;
4492 map->dim = isl_space_cow(map->dim);
4493 if (!map->dim)
4494 goto error;
4496 for (i = 1; i < map->n; ++i)
4497 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4498 goto error);
4499 for (i = 0; i < map->n; ++i) {
4500 map->p[i] = (struct isl_basic_map *)
4501 isl_basic_map_underlying_set(map->p[i]);
4502 if (!map->p[i])
4503 goto error;
4505 if (map->n == 0)
4506 map->dim = isl_space_underlying(map->dim, 0);
4507 else {
4508 isl_space_free(map->dim);
4509 map->dim = isl_space_copy(map->p[0]->dim);
4511 if (!map->dim)
4512 goto error;
4513 return (struct isl_set *)map;
4514 error:
4515 isl_map_free(map);
4516 return NULL;
4519 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4521 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4524 __isl_give isl_basic_map *isl_basic_map_reset_space(
4525 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4527 bmap = isl_basic_map_cow(bmap);
4528 if (!bmap || !dim)
4529 goto error;
4531 isl_space_free(bmap->dim);
4532 bmap->dim = dim;
4534 bmap = isl_basic_map_finalize(bmap);
4536 return bmap;
4537 error:
4538 isl_basic_map_free(bmap);
4539 isl_space_free(dim);
4540 return NULL;
4543 __isl_give isl_basic_set *isl_basic_set_reset_space(
4544 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4546 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4547 dim);
4550 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4551 __isl_take isl_space *dim)
4553 int i;
4555 map = isl_map_cow(map);
4556 if (!map || !dim)
4557 goto error;
4559 for (i = 0; i < map->n; ++i) {
4560 map->p[i] = isl_basic_map_reset_space(map->p[i],
4561 isl_space_copy(dim));
4562 if (!map->p[i])
4563 goto error;
4565 isl_space_free(map->dim);
4566 map->dim = dim;
4568 return map;
4569 error:
4570 isl_map_free(map);
4571 isl_space_free(dim);
4572 return NULL;
4575 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4576 __isl_take isl_space *dim)
4578 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4581 /* Compute the parameter domain of the given basic set.
4583 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4585 isl_space *space;
4586 unsigned n;
4588 if (isl_basic_set_is_params(bset))
4589 return bset;
4591 n = isl_basic_set_dim(bset, isl_dim_set);
4592 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4593 space = isl_basic_set_get_space(bset);
4594 space = isl_space_params(space);
4595 bset = isl_basic_set_reset_space(bset, space);
4596 return bset;
4599 /* Construct a zero-dimensional basic set with the given parameter domain.
4601 __isl_give isl_basic_set *isl_basic_set_from_params(
4602 __isl_take isl_basic_set *bset)
4604 isl_space *space;
4605 space = isl_basic_set_get_space(bset);
4606 space = isl_space_set_from_params(space);
4607 bset = isl_basic_set_reset_space(bset, space);
4608 return bset;
4611 /* Compute the parameter domain of the given set.
4613 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4615 isl_space *space;
4616 unsigned n;
4618 if (isl_set_is_params(set))
4619 return set;
4621 n = isl_set_dim(set, isl_dim_set);
4622 set = isl_set_project_out(set, isl_dim_set, 0, n);
4623 space = isl_set_get_space(set);
4624 space = isl_space_params(space);
4625 set = isl_set_reset_space(set, space);
4626 return set;
4629 /* Construct a zero-dimensional set with the given parameter domain.
4631 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4633 isl_space *space;
4634 space = isl_set_get_space(set);
4635 space = isl_space_set_from_params(space);
4636 set = isl_set_reset_space(set, space);
4637 return set;
4640 /* Compute the parameter domain of the given map.
4642 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4644 isl_space *space;
4645 unsigned n;
4647 n = isl_map_dim(map, isl_dim_in);
4648 map = isl_map_project_out(map, isl_dim_in, 0, n);
4649 n = isl_map_dim(map, isl_dim_out);
4650 map = isl_map_project_out(map, isl_dim_out, 0, n);
4651 space = isl_map_get_space(map);
4652 space = isl_space_params(space);
4653 map = isl_map_reset_space(map, space);
4654 return map;
4657 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4659 isl_space *dim;
4660 struct isl_basic_set *domain;
4661 unsigned n_in;
4662 unsigned n_out;
4664 if (!bmap)
4665 return NULL;
4666 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4668 n_in = isl_basic_map_n_in(bmap);
4669 n_out = isl_basic_map_n_out(bmap);
4670 domain = isl_basic_set_from_basic_map(bmap);
4671 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4673 domain = isl_basic_set_reset_space(domain, dim);
4675 return domain;
4678 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4680 if (!bmap)
4681 return -1;
4682 return isl_space_may_be_set(bmap->dim);
4685 /* Is this basic map actually a set?
4686 * Users should never call this function. Outside of isl,
4687 * the type should indicate whether something is a set or a map.
4689 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4691 if (!bmap)
4692 return -1;
4693 return isl_space_is_set(bmap->dim);
4696 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4698 if (!bmap)
4699 return NULL;
4700 if (isl_basic_map_is_set(bmap))
4701 return bmap;
4702 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4705 __isl_give isl_basic_map *isl_basic_map_domain_map(
4706 __isl_take isl_basic_map *bmap)
4708 int i, k;
4709 isl_space *dim;
4710 isl_basic_map *domain;
4711 int nparam, n_in, n_out;
4712 unsigned total;
4714 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4715 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4716 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4718 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4719 domain = isl_basic_map_universe(dim);
4721 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4722 bmap = isl_basic_map_apply_range(bmap, domain);
4723 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4725 total = isl_basic_map_total_dim(bmap);
4727 for (i = 0; i < n_in; ++i) {
4728 k = isl_basic_map_alloc_equality(bmap);
4729 if (k < 0)
4730 goto error;
4731 isl_seq_clr(bmap->eq[k], 1 + total);
4732 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4733 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4736 bmap = isl_basic_map_gauss(bmap, NULL);
4737 return isl_basic_map_finalize(bmap);
4738 error:
4739 isl_basic_map_free(bmap);
4740 return NULL;
4743 __isl_give isl_basic_map *isl_basic_map_range_map(
4744 __isl_take isl_basic_map *bmap)
4746 int i, k;
4747 isl_space *dim;
4748 isl_basic_map *range;
4749 int nparam, n_in, n_out;
4750 unsigned total;
4752 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4753 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4754 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4756 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4757 range = isl_basic_map_universe(dim);
4759 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4760 bmap = isl_basic_map_apply_range(bmap, range);
4761 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4763 total = isl_basic_map_total_dim(bmap);
4765 for (i = 0; i < n_out; ++i) {
4766 k = isl_basic_map_alloc_equality(bmap);
4767 if (k < 0)
4768 goto error;
4769 isl_seq_clr(bmap->eq[k], 1 + total);
4770 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4771 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4774 bmap = isl_basic_map_gauss(bmap, NULL);
4775 return isl_basic_map_finalize(bmap);
4776 error:
4777 isl_basic_map_free(bmap);
4778 return NULL;
4781 int isl_map_may_be_set(__isl_keep isl_map *map)
4783 if (!map)
4784 return -1;
4785 return isl_space_may_be_set(map->dim);
4788 /* Is this map actually a set?
4789 * Users should never call this function. Outside of isl,
4790 * the type should indicate whether something is a set or a map.
4792 int isl_map_is_set(__isl_keep isl_map *map)
4794 if (!map)
4795 return -1;
4796 return isl_space_is_set(map->dim);
4799 struct isl_set *isl_map_range(struct isl_map *map)
4801 int i;
4802 struct isl_set *set;
4804 if (!map)
4805 goto error;
4806 if (isl_map_is_set(map))
4807 return (isl_set *)map;
4809 map = isl_map_cow(map);
4810 if (!map)
4811 goto error;
4813 set = (struct isl_set *) map;
4814 set->dim = isl_space_range(set->dim);
4815 if (!set->dim)
4816 goto error;
4817 for (i = 0; i < map->n; ++i) {
4818 set->p[i] = isl_basic_map_range(map->p[i]);
4819 if (!set->p[i])
4820 goto error;
4822 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4823 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4824 return set;
4825 error:
4826 isl_map_free(map);
4827 return NULL;
4830 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4832 int i;
4833 isl_space *domain_dim;
4835 map = isl_map_cow(map);
4836 if (!map)
4837 return NULL;
4839 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4840 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4841 map->dim = isl_space_join(map->dim, domain_dim);
4842 if (!map->dim)
4843 goto error;
4844 for (i = 0; i < map->n; ++i) {
4845 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4846 if (!map->p[i])
4847 goto error;
4849 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4850 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4851 return map;
4852 error:
4853 isl_map_free(map);
4854 return NULL;
4857 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4859 int i;
4860 isl_space *range_dim;
4862 map = isl_map_cow(map);
4863 if (!map)
4864 return NULL;
4866 range_dim = isl_space_range(isl_map_get_space(map));
4867 range_dim = isl_space_from_range(range_dim);
4868 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4869 map->dim = isl_space_join(map->dim, range_dim);
4870 if (!map->dim)
4871 goto error;
4872 for (i = 0; i < map->n; ++i) {
4873 map->p[i] = isl_basic_map_range_map(map->p[i]);
4874 if (!map->p[i])
4875 goto error;
4877 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4878 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4879 return map;
4880 error:
4881 isl_map_free(map);
4882 return NULL;
4885 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4886 __isl_take isl_space *dim)
4888 int i;
4889 struct isl_map *map = NULL;
4891 set = isl_set_cow(set);
4892 if (!set || !dim)
4893 goto error;
4894 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4895 map = (struct isl_map *)set;
4896 for (i = 0; i < set->n; ++i) {
4897 map->p[i] = isl_basic_map_from_basic_set(
4898 set->p[i], isl_space_copy(dim));
4899 if (!map->p[i])
4900 goto error;
4902 isl_space_free(map->dim);
4903 map->dim = dim;
4904 return map;
4905 error:
4906 isl_space_free(dim);
4907 isl_set_free(set);
4908 return NULL;
4911 __isl_give isl_basic_map *isl_basic_map_from_domain(
4912 __isl_take isl_basic_set *bset)
4914 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4917 __isl_give isl_basic_map *isl_basic_map_from_range(
4918 __isl_take isl_basic_set *bset)
4920 isl_space *space;
4921 space = isl_basic_set_get_space(bset);
4922 space = isl_space_from_range(space);
4923 bset = isl_basic_set_reset_space(bset, space);
4924 return (isl_basic_map *)bset;
4927 struct isl_map *isl_map_from_range(struct isl_set *set)
4929 isl_space *space;
4930 space = isl_set_get_space(set);
4931 space = isl_space_from_range(space);
4932 set = isl_set_reset_space(set, space);
4933 return (struct isl_map *)set;
4936 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4938 return isl_map_reverse(isl_map_from_range(set));
4941 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4942 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4944 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4947 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4948 __isl_take isl_set *range)
4950 return isl_map_apply_range(isl_map_reverse(domain), range);
4953 struct isl_set *isl_set_from_map(struct isl_map *map)
4955 int i;
4956 struct isl_set *set = NULL;
4958 if (!map)
4959 return NULL;
4960 map = isl_map_cow(map);
4961 if (!map)
4962 return NULL;
4963 map->dim = isl_space_as_set_space(map->dim);
4964 if (!map->dim)
4965 goto error;
4966 set = (struct isl_set *)map;
4967 for (i = 0; i < map->n; ++i) {
4968 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4969 if (!set->p[i])
4970 goto error;
4972 return set;
4973 error:
4974 isl_map_free(map);
4975 return NULL;
4978 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4979 unsigned flags)
4981 struct isl_map *map;
4983 if (!dim)
4984 return NULL;
4985 if (n < 0)
4986 isl_die(dim->ctx, isl_error_internal,
4987 "negative number of basic maps", goto error);
4988 map = isl_alloc(dim->ctx, struct isl_map,
4989 sizeof(struct isl_map) +
4990 (n - 1) * sizeof(struct isl_basic_map *));
4991 if (!map)
4992 goto error;
4994 map->ctx = dim->ctx;
4995 isl_ctx_ref(map->ctx);
4996 map->ref = 1;
4997 map->size = n;
4998 map->n = 0;
4999 map->dim = dim;
5000 map->flags = flags;
5001 return map;
5002 error:
5003 isl_space_free(dim);
5004 return NULL;
5007 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5008 unsigned nparam, unsigned in, unsigned out, int n,
5009 unsigned flags)
5011 struct isl_map *map;
5012 isl_space *dims;
5014 dims = isl_space_alloc(ctx, nparam, in, out);
5015 if (!dims)
5016 return NULL;
5018 map = isl_map_alloc_space(dims, n, flags);
5019 return map;
5022 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5024 struct isl_basic_map *bmap;
5025 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5026 bmap = isl_basic_map_set_to_empty(bmap);
5027 return bmap;
5030 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5032 struct isl_basic_set *bset;
5033 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5034 bset = isl_basic_set_set_to_empty(bset);
5035 return bset;
5038 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5040 struct isl_basic_map *bmap;
5041 if (!model)
5042 return NULL;
5043 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5044 bmap = isl_basic_map_set_to_empty(bmap);
5045 return bmap;
5048 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5050 struct isl_basic_map *bmap;
5051 if (!model)
5052 return NULL;
5053 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5054 bmap = isl_basic_map_set_to_empty(bmap);
5055 return bmap;
5058 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5060 struct isl_basic_set *bset;
5061 if (!model)
5062 return NULL;
5063 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5064 bset = isl_basic_set_set_to_empty(bset);
5065 return bset;
5068 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5070 struct isl_basic_map *bmap;
5071 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5072 bmap = isl_basic_map_finalize(bmap);
5073 return bmap;
5076 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5078 struct isl_basic_set *bset;
5079 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5080 bset = isl_basic_set_finalize(bset);
5081 return bset;
5084 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5086 int i;
5087 unsigned total = isl_space_dim(dim, isl_dim_all);
5088 isl_basic_map *bmap;
5090 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5091 for (i = 0; i < total; ++i) {
5092 int k = isl_basic_map_alloc_inequality(bmap);
5093 if (k < 0)
5094 goto error;
5095 isl_seq_clr(bmap->ineq[k], 1 + total);
5096 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5098 return bmap;
5099 error:
5100 isl_basic_map_free(bmap);
5101 return NULL;
5104 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5106 return isl_basic_map_nat_universe(dim);
5109 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5111 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5114 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5116 return isl_map_nat_universe(dim);
5119 __isl_give isl_basic_map *isl_basic_map_universe_like(
5120 __isl_keep isl_basic_map *model)
5122 if (!model)
5123 return NULL;
5124 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5127 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5129 if (!model)
5130 return NULL;
5131 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5134 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5135 __isl_keep isl_set *model)
5137 if (!model)
5138 return NULL;
5139 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5142 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5144 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5147 struct isl_map *isl_map_empty_like(struct isl_map *model)
5149 if (!model)
5150 return NULL;
5151 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5154 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5156 if (!model)
5157 return NULL;
5158 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5161 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5163 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5166 struct isl_set *isl_set_empty_like(struct isl_set *model)
5168 if (!model)
5169 return NULL;
5170 return isl_set_empty(isl_space_copy(model->dim));
5173 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5175 struct isl_map *map;
5176 if (!dim)
5177 return NULL;
5178 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5179 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5180 return map;
5183 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5185 struct isl_set *set;
5186 if (!dim)
5187 return NULL;
5188 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5189 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5190 return set;
5193 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5195 if (!model)
5196 return NULL;
5197 return isl_set_universe(isl_space_copy(model->dim));
5200 struct isl_map *isl_map_dup(struct isl_map *map)
5202 int i;
5203 struct isl_map *dup;
5205 if (!map)
5206 return NULL;
5207 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5208 for (i = 0; i < map->n; ++i)
5209 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5210 return dup;
5213 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5214 __isl_take isl_basic_map *bmap)
5216 if (!bmap || !map)
5217 goto error;
5218 if (isl_basic_map_plain_is_empty(bmap)) {
5219 isl_basic_map_free(bmap);
5220 return map;
5222 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5223 isl_assert(map->ctx, map->n < map->size, goto error);
5224 map->p[map->n] = bmap;
5225 map->n++;
5226 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5227 return map;
5228 error:
5229 if (map)
5230 isl_map_free(map);
5231 if (bmap)
5232 isl_basic_map_free(bmap);
5233 return NULL;
5236 void *isl_map_free(struct isl_map *map)
5238 int i;
5240 if (!map)
5241 return NULL;
5243 if (--map->ref > 0)
5244 return NULL;
5246 isl_ctx_deref(map->ctx);
5247 for (i = 0; i < map->n; ++i)
5248 isl_basic_map_free(map->p[i]);
5249 isl_space_free(map->dim);
5250 free(map);
5252 return NULL;
5255 struct isl_map *isl_map_extend(struct isl_map *base,
5256 unsigned nparam, unsigned n_in, unsigned n_out)
5258 int i;
5260 base = isl_map_cow(base);
5261 if (!base)
5262 return NULL;
5264 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5265 if (!base->dim)
5266 goto error;
5267 for (i = 0; i < base->n; ++i) {
5268 base->p[i] = isl_basic_map_extend_space(base->p[i],
5269 isl_space_copy(base->dim), 0, 0, 0);
5270 if (!base->p[i])
5271 goto error;
5273 return base;
5274 error:
5275 isl_map_free(base);
5276 return NULL;
5279 struct isl_set *isl_set_extend(struct isl_set *base,
5280 unsigned nparam, unsigned dim)
5282 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5283 nparam, 0, dim);
5286 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5287 struct isl_basic_map *bmap, unsigned pos, int value)
5289 int j;
5291 bmap = isl_basic_map_cow(bmap);
5292 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5293 j = isl_basic_map_alloc_equality(bmap);
5294 if (j < 0)
5295 goto error;
5296 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5297 isl_int_set_si(bmap->eq[j][pos], -1);
5298 isl_int_set_si(bmap->eq[j][0], value);
5299 bmap = isl_basic_map_simplify(bmap);
5300 return isl_basic_map_finalize(bmap);
5301 error:
5302 isl_basic_map_free(bmap);
5303 return NULL;
5306 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5307 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5309 int j;
5311 bmap = isl_basic_map_cow(bmap);
5312 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5313 j = isl_basic_map_alloc_equality(bmap);
5314 if (j < 0)
5315 goto error;
5316 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5317 isl_int_set_si(bmap->eq[j][pos], -1);
5318 isl_int_set(bmap->eq[j][0], value);
5319 bmap = isl_basic_map_simplify(bmap);
5320 return isl_basic_map_finalize(bmap);
5321 error:
5322 isl_basic_map_free(bmap);
5323 return NULL;
5326 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5327 enum isl_dim_type type, unsigned pos, int value)
5329 if (!bmap)
5330 return NULL;
5331 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5332 return isl_basic_map_fix_pos_si(bmap,
5333 isl_basic_map_offset(bmap, type) + pos, value);
5334 error:
5335 isl_basic_map_free(bmap);
5336 return NULL;
5339 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5340 enum isl_dim_type type, unsigned pos, isl_int value)
5342 if (!bmap)
5343 return NULL;
5344 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5345 return isl_basic_map_fix_pos(bmap,
5346 isl_basic_map_offset(bmap, type) + pos, value);
5347 error:
5348 isl_basic_map_free(bmap);
5349 return NULL;
5352 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5353 * to be equal to "v".
5355 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5356 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5358 if (!bmap || !v)
5359 goto error;
5360 if (!isl_val_is_int(v))
5361 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5362 "expecting integer value", goto error);
5363 if (pos >= isl_basic_map_dim(bmap, type))
5364 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5365 "index out of bounds", goto error);
5366 pos += isl_basic_map_offset(bmap, type);
5367 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5368 isl_val_free(v);
5369 return bmap;
5370 error:
5371 isl_basic_map_free(bmap);
5372 isl_val_free(v);
5373 return NULL;
5376 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5377 * to be equal to "v".
5379 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5380 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5382 return isl_basic_map_fix_val(bset, type, pos, v);
5385 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5386 enum isl_dim_type type, unsigned pos, int value)
5388 return (struct isl_basic_set *)
5389 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5390 type, pos, value);
5393 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5394 enum isl_dim_type type, unsigned pos, isl_int value)
5396 return (struct isl_basic_set *)
5397 isl_basic_map_fix((struct isl_basic_map *)bset,
5398 type, pos, value);
5401 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5402 unsigned input, int value)
5404 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5407 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5408 unsigned dim, int value)
5410 return (struct isl_basic_set *)
5411 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5412 isl_dim_set, dim, value);
5415 static int remove_if_empty(__isl_keep isl_map *map, int i)
5417 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5419 if (empty < 0)
5420 return -1;
5421 if (!empty)
5422 return 0;
5424 isl_basic_map_free(map->p[i]);
5425 if (i != map->n - 1) {
5426 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5427 map->p[i] = map->p[map->n - 1];
5429 map->n--;
5431 return 0;
5434 /* Perform "fn" on each basic map of "map", where we may not be holding
5435 * the only reference to "map".
5436 * In particular, "fn" should be a semantics preserving operation
5437 * that we want to apply to all copies of "map". We therefore need
5438 * to be careful not to modify "map" in a way that breaks "map"
5439 * in case anything goes wrong.
5441 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5442 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5444 struct isl_basic_map *bmap;
5445 int i;
5447 if (!map)
5448 return NULL;
5450 for (i = map->n - 1; i >= 0; --i) {
5451 bmap = isl_basic_map_copy(map->p[i]);
5452 bmap = fn(bmap);
5453 if (!bmap)
5454 goto error;
5455 isl_basic_map_free(map->p[i]);
5456 map->p[i] = bmap;
5457 if (remove_if_empty(map, i) < 0)
5458 goto error;
5461 return map;
5462 error:
5463 isl_map_free(map);
5464 return NULL;
5467 struct isl_map *isl_map_fix_si(struct isl_map *map,
5468 enum isl_dim_type type, unsigned pos, int value)
5470 int i;
5472 map = isl_map_cow(map);
5473 if (!map)
5474 return NULL;
5476 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5477 for (i = map->n - 1; i >= 0; --i) {
5478 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5479 if (remove_if_empty(map, i) < 0)
5480 goto error;
5482 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5483 return map;
5484 error:
5485 isl_map_free(map);
5486 return NULL;
5489 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5490 enum isl_dim_type type, unsigned pos, int value)
5492 return (struct isl_set *)
5493 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5496 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5497 enum isl_dim_type type, unsigned pos, isl_int value)
5499 int i;
5501 map = isl_map_cow(map);
5502 if (!map)
5503 return NULL;
5505 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5506 for (i = 0; i < map->n; ++i) {
5507 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5508 if (!map->p[i])
5509 goto error;
5511 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5512 return map;
5513 error:
5514 isl_map_free(map);
5515 return NULL;
5518 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5519 enum isl_dim_type type, unsigned pos, isl_int value)
5521 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5524 /* Fix the value of the variable at position "pos" of type "type" of "map"
5525 * to be equal to "v".
5527 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5528 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5530 int i;
5532 map = isl_map_cow(map);
5533 if (!map || !v)
5534 goto error;
5536 if (!isl_val_is_int(v))
5537 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5538 "expecting integer value", goto error);
5539 if (pos >= isl_map_dim(map, type))
5540 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5541 "index out of bounds", goto error);
5542 for (i = map->n - 1; i >= 0; --i) {
5543 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5544 isl_val_copy(v));
5545 if (remove_if_empty(map, i) < 0)
5546 goto error;
5548 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5549 isl_val_free(v);
5550 return map;
5551 error:
5552 isl_map_free(map);
5553 isl_val_free(v);
5554 return NULL;
5557 /* Fix the value of the variable at position "pos" of type "type" of "set"
5558 * to be equal to "v".
5560 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5561 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5563 return isl_map_fix_val(set, type, pos, v);
5566 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5567 unsigned input, int value)
5569 return isl_map_fix_si(map, isl_dim_in, input, value);
5572 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5574 return (struct isl_set *)
5575 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5578 static __isl_give isl_basic_map *basic_map_bound_si(
5579 __isl_take isl_basic_map *bmap,
5580 enum isl_dim_type type, unsigned pos, int value, int upper)
5582 int j;
5584 if (!bmap)
5585 return NULL;
5586 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5587 pos += isl_basic_map_offset(bmap, type);
5588 bmap = isl_basic_map_cow(bmap);
5589 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5590 j = isl_basic_map_alloc_inequality(bmap);
5591 if (j < 0)
5592 goto error;
5593 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5594 if (upper) {
5595 isl_int_set_si(bmap->ineq[j][pos], -1);
5596 isl_int_set_si(bmap->ineq[j][0], value);
5597 } else {
5598 isl_int_set_si(bmap->ineq[j][pos], 1);
5599 isl_int_set_si(bmap->ineq[j][0], -value);
5601 bmap = isl_basic_map_simplify(bmap);
5602 return isl_basic_map_finalize(bmap);
5603 error:
5604 isl_basic_map_free(bmap);
5605 return NULL;
5608 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5609 __isl_take isl_basic_map *bmap,
5610 enum isl_dim_type type, unsigned pos, int value)
5612 return basic_map_bound_si(bmap, type, pos, value, 0);
5615 /* Constrain the values of the given dimension to be no greater than "value".
5617 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5618 __isl_take isl_basic_map *bmap,
5619 enum isl_dim_type type, unsigned pos, int value)
5621 return basic_map_bound_si(bmap, type, pos, value, 1);
5624 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5625 unsigned dim, isl_int value)
5627 int j;
5629 bset = isl_basic_set_cow(bset);
5630 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5631 j = isl_basic_set_alloc_inequality(bset);
5632 if (j < 0)
5633 goto error;
5634 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5635 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5636 isl_int_neg(bset->ineq[j][0], value);
5637 bset = isl_basic_set_simplify(bset);
5638 return isl_basic_set_finalize(bset);
5639 error:
5640 isl_basic_set_free(bset);
5641 return NULL;
5644 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5645 enum isl_dim_type type, unsigned pos, int value, int upper)
5647 int i;
5649 map = isl_map_cow(map);
5650 if (!map)
5651 return NULL;
5653 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5654 for (i = 0; i < map->n; ++i) {
5655 map->p[i] = basic_map_bound_si(map->p[i],
5656 type, pos, value, upper);
5657 if (!map->p[i])
5658 goto error;
5660 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5661 return map;
5662 error:
5663 isl_map_free(map);
5664 return NULL;
5667 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5668 enum isl_dim_type type, unsigned pos, int value)
5670 return map_bound_si(map, type, pos, value, 0);
5673 __isl_give isl_map *isl_map_upper_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, 1);
5679 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5680 enum isl_dim_type type, unsigned pos, int value)
5682 return (struct isl_set *)
5683 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5686 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5687 enum isl_dim_type type, unsigned pos, int value)
5689 return isl_map_upper_bound_si(set, type, pos, value);
5692 /* Bound the given variable of "bmap" from below (or above is "upper"
5693 * is set) to "value".
5695 static __isl_give isl_basic_map *basic_map_bound(
5696 __isl_take isl_basic_map *bmap,
5697 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5699 int j;
5701 if (!bmap)
5702 return NULL;
5703 if (pos >= isl_basic_map_dim(bmap, type))
5704 isl_die(bmap->ctx, isl_error_invalid,
5705 "index out of bounds", goto error);
5706 pos += isl_basic_map_offset(bmap, type);
5707 bmap = isl_basic_map_cow(bmap);
5708 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5709 j = isl_basic_map_alloc_inequality(bmap);
5710 if (j < 0)
5711 goto error;
5712 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5713 if (upper) {
5714 isl_int_set_si(bmap->ineq[j][pos], -1);
5715 isl_int_set(bmap->ineq[j][0], value);
5716 } else {
5717 isl_int_set_si(bmap->ineq[j][pos], 1);
5718 isl_int_neg(bmap->ineq[j][0], value);
5720 bmap = isl_basic_map_simplify(bmap);
5721 return isl_basic_map_finalize(bmap);
5722 error:
5723 isl_basic_map_free(bmap);
5724 return NULL;
5727 /* Bound the given variable of "map" from below (or above is "upper"
5728 * is set) to "value".
5730 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5731 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5733 int i;
5735 map = isl_map_cow(map);
5736 if (!map)
5737 return NULL;
5739 if (pos >= isl_map_dim(map, type))
5740 isl_die(map->ctx, isl_error_invalid,
5741 "index out of bounds", goto error);
5742 for (i = map->n - 1; i >= 0; --i) {
5743 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5744 if (remove_if_empty(map, i) < 0)
5745 goto error;
5747 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5748 return map;
5749 error:
5750 isl_map_free(map);
5751 return NULL;
5754 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5755 enum isl_dim_type type, unsigned pos, isl_int value)
5757 return map_bound(map, type, pos, value, 0);
5760 __isl_give isl_map *isl_map_upper_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, 1);
5766 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5767 enum isl_dim_type type, unsigned pos, isl_int value)
5769 return isl_map_lower_bound(set, type, pos, value);
5772 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5773 enum isl_dim_type type, unsigned pos, isl_int value)
5775 return isl_map_upper_bound(set, type, pos, value);
5778 /* Force the values of the variable at position "pos" of type "type" of "set"
5779 * to be no smaller than "value".
5781 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5782 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5784 if (!value)
5785 goto error;
5786 if (!isl_val_is_int(value))
5787 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5788 "expecting integer value", goto error);
5789 set = isl_set_lower_bound(set, type, pos, value->n);
5790 isl_val_free(value);
5791 return set;
5792 error:
5793 isl_val_free(value);
5794 isl_set_free(set);
5795 return NULL;
5798 /* Force the values of the variable at position "pos" of type "type" of "set"
5799 * to be no greater than "value".
5801 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5802 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5804 if (!value)
5805 goto error;
5806 if (!isl_val_is_int(value))
5807 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5808 "expecting integer value", goto error);
5809 set = isl_set_upper_bound(set, type, pos, value->n);
5810 isl_val_free(value);
5811 return set;
5812 error:
5813 isl_val_free(value);
5814 isl_set_free(set);
5815 return NULL;
5818 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5819 isl_int value)
5821 int i;
5823 set = isl_set_cow(set);
5824 if (!set)
5825 return NULL;
5827 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5828 for (i = 0; i < set->n; ++i) {
5829 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5830 if (!set->p[i])
5831 goto error;
5833 return set;
5834 error:
5835 isl_set_free(set);
5836 return NULL;
5839 struct isl_map *isl_map_reverse(struct isl_map *map)
5841 int i;
5843 map = isl_map_cow(map);
5844 if (!map)
5845 return NULL;
5847 map->dim = isl_space_reverse(map->dim);
5848 if (!map->dim)
5849 goto error;
5850 for (i = 0; i < map->n; ++i) {
5851 map->p[i] = isl_basic_map_reverse(map->p[i]);
5852 if (!map->p[i])
5853 goto error;
5855 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5856 return map;
5857 error:
5858 isl_map_free(map);
5859 return NULL;
5862 static struct isl_map *isl_basic_map_partial_lexopt(
5863 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5864 struct isl_set **empty, int max)
5866 if (!bmap)
5867 goto error;
5868 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5869 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5870 else
5871 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5872 error:
5873 isl_basic_map_free(bmap);
5874 isl_basic_set_free(dom);
5875 if (empty)
5876 *empty = NULL;
5877 return NULL;
5880 struct isl_map *isl_basic_map_partial_lexmax(
5881 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5882 struct isl_set **empty)
5884 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5887 struct isl_map *isl_basic_map_partial_lexmin(
5888 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5889 struct isl_set **empty)
5891 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5894 struct isl_set *isl_basic_set_partial_lexmin(
5895 struct isl_basic_set *bset, struct isl_basic_set *dom,
5896 struct isl_set **empty)
5898 return (struct isl_set *)
5899 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5900 dom, empty);
5903 struct isl_set *isl_basic_set_partial_lexmax(
5904 struct isl_basic_set *bset, struct isl_basic_set *dom,
5905 struct isl_set **empty)
5907 return (struct isl_set *)
5908 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5909 dom, empty);
5912 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5913 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5914 __isl_give isl_set **empty)
5916 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5919 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5920 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5921 __isl_give isl_set **empty)
5923 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5926 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5927 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5928 __isl_give isl_set **empty)
5930 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5933 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5934 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5935 __isl_give isl_set **empty)
5937 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5940 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5941 __isl_take isl_basic_map *bmap, int max)
5943 isl_basic_set *dom = NULL;
5944 isl_space *dom_space;
5946 if (!bmap)
5947 goto error;
5948 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5949 dom = isl_basic_set_universe(dom_space);
5950 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5951 error:
5952 isl_basic_map_free(bmap);
5953 return NULL;
5956 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5957 __isl_take isl_basic_map *bmap)
5959 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5962 #undef TYPE
5963 #define TYPE isl_pw_multi_aff
5964 #undef SUFFIX
5965 #define SUFFIX _pw_multi_aff
5966 #undef EMPTY
5967 #define EMPTY isl_pw_multi_aff_empty
5968 #undef ADD
5969 #define ADD isl_pw_multi_aff_union_add
5970 #include "isl_map_lexopt_templ.c"
5972 /* Given a map "map", compute the lexicographically minimal
5973 * (or maximal) image element for each domain element in dom,
5974 * in the form of an isl_pw_multi_aff.
5975 * Set *empty to those elements in dom that do not have an image element.
5977 * We first compute the lexicographically minimal or maximal element
5978 * in the first basic map. This results in a partial solution "res"
5979 * and a subset "todo" of dom that still need to be handled.
5980 * We then consider each of the remaining maps in "map" and successively
5981 * update both "res" and "todo".
5983 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5984 __isl_take isl_map *map, __isl_take isl_set *dom,
5985 __isl_give isl_set **empty, int max)
5987 int i;
5988 isl_pw_multi_aff *res;
5989 isl_set *todo;
5991 if (!map || !dom)
5992 goto error;
5994 if (isl_map_plain_is_empty(map)) {
5995 if (empty)
5996 *empty = dom;
5997 else
5998 isl_set_free(dom);
5999 return isl_pw_multi_aff_from_map(map);
6002 res = basic_map_partial_lexopt_pw_multi_aff(
6003 isl_basic_map_copy(map->p[0]),
6004 isl_set_copy(dom), &todo, max);
6006 for (i = 1; i < map->n; ++i) {
6007 isl_pw_multi_aff *res_i;
6008 isl_set *todo_i;
6010 res_i = basic_map_partial_lexopt_pw_multi_aff(
6011 isl_basic_map_copy(map->p[i]),
6012 isl_set_copy(dom), &todo_i, max);
6014 if (max)
6015 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6016 else
6017 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6019 todo = isl_set_intersect(todo, todo_i);
6022 isl_set_free(dom);
6023 isl_map_free(map);
6025 if (empty)
6026 *empty = todo;
6027 else
6028 isl_set_free(todo);
6030 return res;
6031 error:
6032 if (empty)
6033 *empty = NULL;
6034 isl_set_free(dom);
6035 isl_map_free(map);
6036 return NULL;
6039 #undef TYPE
6040 #define TYPE isl_map
6041 #undef SUFFIX
6042 #define SUFFIX
6043 #undef EMPTY
6044 #define EMPTY isl_map_empty
6045 #undef ADD
6046 #define ADD isl_map_union_disjoint
6047 #include "isl_map_lexopt_templ.c"
6049 /* Given a map "map", compute the lexicographically minimal
6050 * (or maximal) image element for each domain element in dom.
6051 * Set *empty to those elements in dom that do not have an image element.
6053 * We first compute the lexicographically minimal or maximal element
6054 * in the first basic map. This results in a partial solution "res"
6055 * and a subset "todo" of dom that still need to be handled.
6056 * We then consider each of the remaining maps in "map" and successively
6057 * update both "res" and "todo".
6059 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6060 * Assume we are computing the lexicographical maximum.
6061 * We first compute the lexicographically maximal element in basic map i.
6062 * This results in a partial solution res_i and a subset todo_i.
6063 * Then we combine these results with those obtain for the first k basic maps
6064 * to obtain a result that is valid for the first k+1 basic maps.
6065 * In particular, the set where there is no solution is the set where
6066 * there is no solution for the first k basic maps and also no solution
6067 * for the ith basic map, i.e.,
6069 * todo^i = todo^k * todo_i
6071 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6072 * solutions, arbitrarily breaking ties in favor of res^k.
6073 * That is, when res^k(a) >= res_i(a), we pick res^k and
6074 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6075 * the lexicographic order.)
6076 * In practice, we compute
6078 * res^k * (res_i . "<=")
6080 * and
6082 * res_i * (res^k . "<")
6084 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6085 * where only one of res^k and res_i provides a solution and we simply pick
6086 * that one, i.e.,
6088 * res^k * todo_i
6089 * and
6090 * res_i * todo^k
6092 * Note that we only compute these intersections when dom(res^k) intersects
6093 * dom(res_i). Otherwise, the only effect of these intersections is to
6094 * potentially break up res^k and res_i into smaller pieces.
6095 * We want to avoid such splintering as much as possible.
6096 * In fact, an earlier implementation of this function would look for
6097 * better results in the domain of res^k and for extra results in todo^k,
6098 * but this would always result in a splintering according to todo^k,
6099 * even when the domain of basic map i is disjoint from the domains of
6100 * the previous basic maps.
6102 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6103 __isl_take isl_map *map, __isl_take isl_set *dom,
6104 __isl_give isl_set **empty, int max)
6106 int i;
6107 struct isl_map *res;
6108 struct isl_set *todo;
6110 if (!map || !dom)
6111 goto error;
6113 if (isl_map_plain_is_empty(map)) {
6114 if (empty)
6115 *empty = dom;
6116 else
6117 isl_set_free(dom);
6118 return map;
6121 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6122 isl_set_copy(dom), &todo, max);
6124 for (i = 1; i < map->n; ++i) {
6125 isl_map *lt, *le;
6126 isl_map *res_i;
6127 isl_set *todo_i;
6128 isl_space *dim = isl_space_range(isl_map_get_space(res));
6130 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6131 isl_set_copy(dom), &todo_i, max);
6133 if (max) {
6134 lt = isl_map_lex_lt(isl_space_copy(dim));
6135 le = isl_map_lex_le(dim);
6136 } else {
6137 lt = isl_map_lex_gt(isl_space_copy(dim));
6138 le = isl_map_lex_ge(dim);
6140 lt = isl_map_apply_range(isl_map_copy(res), lt);
6141 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6142 le = isl_map_apply_range(isl_map_copy(res_i), le);
6143 le = isl_map_intersect(le, isl_map_copy(res));
6145 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6146 res = isl_map_intersect_domain(res,
6147 isl_set_copy(todo_i));
6148 res_i = isl_map_intersect_domain(res_i,
6149 isl_set_copy(todo));
6152 res = isl_map_union_disjoint(res, res_i);
6153 res = isl_map_union_disjoint(res, lt);
6154 res = isl_map_union_disjoint(res, le);
6156 todo = isl_set_intersect(todo, todo_i);
6159 isl_set_free(dom);
6160 isl_map_free(map);
6162 if (empty)
6163 *empty = todo;
6164 else
6165 isl_set_free(todo);
6167 return res;
6168 error:
6169 if (empty)
6170 *empty = NULL;
6171 isl_set_free(dom);
6172 isl_map_free(map);
6173 return NULL;
6176 __isl_give isl_map *isl_map_partial_lexmax(
6177 __isl_take isl_map *map, __isl_take isl_set *dom,
6178 __isl_give isl_set **empty)
6180 return isl_map_partial_lexopt(map, dom, empty, 1);
6183 __isl_give isl_map *isl_map_partial_lexmin(
6184 __isl_take isl_map *map, __isl_take isl_set *dom,
6185 __isl_give isl_set **empty)
6187 return isl_map_partial_lexopt(map, dom, empty, 0);
6190 __isl_give isl_set *isl_set_partial_lexmin(
6191 __isl_take isl_set *set, __isl_take isl_set *dom,
6192 __isl_give isl_set **empty)
6194 return (struct isl_set *)
6195 isl_map_partial_lexmin((struct isl_map *)set,
6196 dom, empty);
6199 __isl_give isl_set *isl_set_partial_lexmax(
6200 __isl_take isl_set *set, __isl_take isl_set *dom,
6201 __isl_give isl_set **empty)
6203 return (struct isl_set *)
6204 isl_map_partial_lexmax((struct isl_map *)set,
6205 dom, empty);
6208 /* Compute the lexicographic minimum (or maximum if "max" is set)
6209 * of "bmap" over its domain.
6211 * Since we are not interested in the part of the domain space where
6212 * there is no solution, we initialize the domain to those constraints
6213 * of "bmap" that only involve the parameters and the input dimensions.
6214 * This relieves the parametric programming engine from detecting those
6215 * inequalities and transferring them to the context. More importantly,
6216 * it ensures that those inequalities are transferred first and not
6217 * intermixed with inequalities that actually split the domain.
6219 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6221 int n_div;
6222 int n_out;
6223 isl_basic_map *copy;
6224 isl_basic_set *dom;
6226 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6227 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6228 copy = isl_basic_map_copy(bmap);
6229 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6230 isl_dim_div, 0, n_div);
6231 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6232 isl_dim_out, 0, n_out);
6233 dom = isl_basic_map_domain(copy);
6234 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6237 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6239 return isl_basic_map_lexopt(bmap, 0);
6242 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6244 return isl_basic_map_lexopt(bmap, 1);
6247 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6249 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6252 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6254 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6257 /* Extract the first and only affine expression from list
6258 * and then add it to *pwaff with the given dom.
6259 * This domain is known to be disjoint from other domains
6260 * because of the way isl_basic_map_foreach_lexmax works.
6262 static int update_dim_opt(__isl_take isl_basic_set *dom,
6263 __isl_take isl_aff_list *list, void *user)
6265 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6266 isl_aff *aff;
6267 isl_pw_aff **pwaff = user;
6268 isl_pw_aff *pwaff_i;
6270 if (!list)
6271 goto error;
6272 if (isl_aff_list_n_aff(list) != 1)
6273 isl_die(ctx, isl_error_internal,
6274 "expecting single element list", goto error);
6276 aff = isl_aff_list_get_aff(list, 0);
6277 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6279 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6281 isl_aff_list_free(list);
6283 return 0;
6284 error:
6285 isl_basic_set_free(dom);
6286 isl_aff_list_free(list);
6287 return -1;
6290 /* Given a basic map with one output dimension, compute the minimum or
6291 * maximum of that dimension as an isl_pw_aff.
6293 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6294 * call update_dim_opt on each leaf of the result.
6296 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6297 int max)
6299 isl_space *dim = isl_basic_map_get_space(bmap);
6300 isl_pw_aff *pwaff;
6301 int r;
6303 dim = isl_space_from_domain(isl_space_domain(dim));
6304 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6305 pwaff = isl_pw_aff_empty(dim);
6307 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6308 if (r < 0)
6309 return isl_pw_aff_free(pwaff);
6311 return pwaff;
6314 /* Compute the minimum or maximum of the given output dimension
6315 * as a function of the parameters and the input dimensions,
6316 * but independently of the other output dimensions.
6318 * We first project out the other output dimension and then compute
6319 * the "lexicographic" maximum in each basic map, combining the results
6320 * using isl_pw_aff_union_max.
6322 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6323 int max)
6325 int i;
6326 isl_pw_aff *pwaff;
6327 unsigned n_out;
6329 n_out = isl_map_dim(map, isl_dim_out);
6330 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6331 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6332 if (!map)
6333 return NULL;
6335 if (map->n == 0) {
6336 isl_space *dim = isl_map_get_space(map);
6337 dim = isl_space_domain(isl_space_from_range(dim));
6338 isl_map_free(map);
6339 return isl_pw_aff_empty(dim);
6342 pwaff = basic_map_dim_opt(map->p[0], max);
6343 for (i = 1; i < map->n; ++i) {
6344 isl_pw_aff *pwaff_i;
6346 pwaff_i = basic_map_dim_opt(map->p[i], max);
6347 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6350 isl_map_free(map);
6352 return pwaff;
6355 /* Compute the maximum of the given output dimension as a function of the
6356 * parameters and input dimensions, but independently of
6357 * the other output dimensions.
6359 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6361 return map_dim_opt(map, pos, 1);
6364 /* Compute the minimum or maximum of the given set dimension
6365 * as a function of the parameters,
6366 * but independently of the other set dimensions.
6368 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6369 int max)
6371 return map_dim_opt(set, pos, max);
6374 /* Compute the maximum of the given set dimension as a function of the
6375 * parameters, but independently of the other set dimensions.
6377 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6379 return set_dim_opt(set, pos, 1);
6382 /* Compute the minimum of the given set dimension as a function of the
6383 * parameters, but independently of the other set dimensions.
6385 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6387 return set_dim_opt(set, pos, 0);
6390 /* Apply a preimage specified by "mat" on the parameters of "bset".
6391 * bset is assumed to have only parameters and divs.
6393 static struct isl_basic_set *basic_set_parameter_preimage(
6394 struct isl_basic_set *bset, struct isl_mat *mat)
6396 unsigned nparam;
6398 if (!bset || !mat)
6399 goto error;
6401 bset->dim = isl_space_cow(bset->dim);
6402 if (!bset->dim)
6403 goto error;
6405 nparam = isl_basic_set_dim(bset, isl_dim_param);
6407 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6409 bset->dim->nparam = 0;
6410 bset->dim->n_out = nparam;
6411 bset = isl_basic_set_preimage(bset, mat);
6412 if (bset) {
6413 bset->dim->nparam = bset->dim->n_out;
6414 bset->dim->n_out = 0;
6416 return bset;
6417 error:
6418 isl_mat_free(mat);
6419 isl_basic_set_free(bset);
6420 return NULL;
6423 /* Apply a preimage specified by "mat" on the parameters of "set".
6424 * set is assumed to have only parameters and divs.
6426 static struct isl_set *set_parameter_preimage(
6427 struct isl_set *set, struct isl_mat *mat)
6429 isl_space *dim = NULL;
6430 unsigned nparam;
6432 if (!set || !mat)
6433 goto error;
6435 dim = isl_space_copy(set->dim);
6436 dim = isl_space_cow(dim);
6437 if (!dim)
6438 goto error;
6440 nparam = isl_set_dim(set, isl_dim_param);
6442 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6444 dim->nparam = 0;
6445 dim->n_out = nparam;
6446 isl_set_reset_space(set, dim);
6447 set = isl_set_preimage(set, mat);
6448 if (!set)
6449 goto error2;
6450 dim = isl_space_copy(set->dim);
6451 dim = isl_space_cow(dim);
6452 if (!dim)
6453 goto error2;
6454 dim->nparam = dim->n_out;
6455 dim->n_out = 0;
6456 isl_set_reset_space(set, dim);
6457 return set;
6458 error:
6459 isl_space_free(dim);
6460 isl_mat_free(mat);
6461 error2:
6462 isl_set_free(set);
6463 return NULL;
6466 /* Intersect the basic set "bset" with the affine space specified by the
6467 * equalities in "eq".
6469 static struct isl_basic_set *basic_set_append_equalities(
6470 struct isl_basic_set *bset, struct isl_mat *eq)
6472 int i, k;
6473 unsigned len;
6475 if (!bset || !eq)
6476 goto error;
6478 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6479 eq->n_row, 0);
6480 if (!bset)
6481 goto error;
6483 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6484 for (i = 0; i < eq->n_row; ++i) {
6485 k = isl_basic_set_alloc_equality(bset);
6486 if (k < 0)
6487 goto error;
6488 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6489 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6491 isl_mat_free(eq);
6493 bset = isl_basic_set_gauss(bset, NULL);
6494 bset = isl_basic_set_finalize(bset);
6496 return bset;
6497 error:
6498 isl_mat_free(eq);
6499 isl_basic_set_free(bset);
6500 return NULL;
6503 /* Intersect the set "set" with the affine space specified by the
6504 * equalities in "eq".
6506 static struct isl_set *set_append_equalities(struct isl_set *set,
6507 struct isl_mat *eq)
6509 int i;
6511 if (!set || !eq)
6512 goto error;
6514 for (i = 0; i < set->n; ++i) {
6515 set->p[i] = basic_set_append_equalities(set->p[i],
6516 isl_mat_copy(eq));
6517 if (!set->p[i])
6518 goto error;
6520 isl_mat_free(eq);
6521 return set;
6522 error:
6523 isl_mat_free(eq);
6524 isl_set_free(set);
6525 return NULL;
6528 /* Given a basic set "bset" that only involves parameters and existentially
6529 * quantified variables, return the index of the first equality
6530 * that only involves parameters. If there is no such equality then
6531 * return bset->n_eq.
6533 * This function assumes that isl_basic_set_gauss has been called on "bset".
6535 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6537 int i, j;
6538 unsigned nparam, n_div;
6540 if (!bset)
6541 return -1;
6543 nparam = isl_basic_set_dim(bset, isl_dim_param);
6544 n_div = isl_basic_set_dim(bset, isl_dim_div);
6546 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6547 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6548 ++i;
6551 return i;
6554 /* Compute an explicit representation for the existentially quantified
6555 * variables in "bset" by computing the "minimal value" of the set
6556 * variables. Since there are no set variables, the computation of
6557 * the minimal value essentially computes an explicit representation
6558 * of the non-empty part(s) of "bset".
6560 * The input only involves parameters and existentially quantified variables.
6561 * All equalities among parameters have been removed.
6563 * Since the existentially quantified variables in the result are in general
6564 * going to be different from those in the input, we first replace
6565 * them by the minimal number of variables based on their equalities.
6566 * This should simplify the parametric integer programming.
6568 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6570 isl_morph *morph1, *morph2;
6571 isl_set *set;
6572 unsigned n;
6574 if (!bset)
6575 return NULL;
6576 if (bset->n_eq == 0)
6577 return isl_basic_set_lexmin(bset);
6579 morph1 = isl_basic_set_parameter_compression(bset);
6580 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6581 bset = isl_basic_set_lift(bset);
6582 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6583 bset = isl_morph_basic_set(morph2, bset);
6584 n = isl_basic_set_dim(bset, isl_dim_set);
6585 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6587 set = isl_basic_set_lexmin(bset);
6589 set = isl_morph_set(isl_morph_inverse(morph1), set);
6591 return set;
6594 /* Project the given basic set onto its parameter domain, possibly introducing
6595 * new, explicit, existential variables in the constraints.
6596 * The input has parameters and (possibly implicit) existential variables.
6597 * The output has the same parameters, but only
6598 * explicit existentially quantified variables.
6600 * The actual projection is performed by pip, but pip doesn't seem
6601 * to like equalities very much, so we first remove the equalities
6602 * among the parameters by performing a variable compression on
6603 * the parameters. Afterward, an inverse transformation is performed
6604 * and the equalities among the parameters are inserted back in.
6606 * The variable compression on the parameters may uncover additional
6607 * equalities that were only implicit before. We therefore check
6608 * if there are any new parameter equalities in the result and
6609 * if so recurse. The removal of parameter equalities is required
6610 * for the parameter compression performed by base_compute_divs.
6612 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6614 int i;
6615 struct isl_mat *eq;
6616 struct isl_mat *T, *T2;
6617 struct isl_set *set;
6618 unsigned nparam;
6620 bset = isl_basic_set_cow(bset);
6621 if (!bset)
6622 return NULL;
6624 if (bset->n_eq == 0)
6625 return base_compute_divs(bset);
6627 bset = isl_basic_set_gauss(bset, NULL);
6628 if (!bset)
6629 return NULL;
6630 if (isl_basic_set_plain_is_empty(bset))
6631 return isl_set_from_basic_set(bset);
6633 i = first_parameter_equality(bset);
6634 if (i == bset->n_eq)
6635 return base_compute_divs(bset);
6637 nparam = isl_basic_set_dim(bset, isl_dim_param);
6638 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6639 0, 1 + nparam);
6640 eq = isl_mat_cow(eq);
6641 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6642 if (T && T->n_col == 0) {
6643 isl_mat_free(T);
6644 isl_mat_free(T2);
6645 isl_mat_free(eq);
6646 bset = isl_basic_set_set_to_empty(bset);
6647 return isl_set_from_basic_set(bset);
6649 bset = basic_set_parameter_preimage(bset, T);
6651 i = first_parameter_equality(bset);
6652 if (!bset)
6653 set = NULL;
6654 else if (i == bset->n_eq)
6655 set = base_compute_divs(bset);
6656 else
6657 set = parameter_compute_divs(bset);
6658 set = set_parameter_preimage(set, T2);
6659 set = set_append_equalities(set, eq);
6660 return set;
6663 /* Insert the divs from "ls" before those of "bmap".
6665 * The number of columns is not changed, which means that the last
6666 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6667 * The caller is responsible for removing the same number of dimensions
6668 * from the space of "bmap".
6670 static __isl_give isl_basic_map *insert_divs_from_local_space(
6671 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6673 int i;
6674 int n_div;
6675 int old_n_div;
6677 n_div = isl_local_space_dim(ls, isl_dim_div);
6678 if (n_div == 0)
6679 return bmap;
6681 old_n_div = bmap->n_div;
6682 bmap = insert_div_rows(bmap, n_div);
6683 if (!bmap)
6684 return NULL;
6686 for (i = 0; i < n_div; ++i) {
6687 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6688 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6691 return bmap;
6694 /* Replace the space of "bmap" by the space and divs of "ls".
6696 * If "ls" has any divs, then we simplify the result since we may
6697 * have discovered some additional equalities that could simplify
6698 * the div expressions.
6700 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6701 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6703 int n_div;
6705 bmap = isl_basic_map_cow(bmap);
6706 if (!bmap || !ls)
6707 goto error;
6709 n_div = isl_local_space_dim(ls, isl_dim_div);
6710 bmap = insert_divs_from_local_space(bmap, ls);
6711 if (!bmap)
6712 goto error;
6714 isl_space_free(bmap->dim);
6715 bmap->dim = isl_local_space_get_space(ls);
6716 if (!bmap->dim)
6717 goto error;
6719 isl_local_space_free(ls);
6720 if (n_div > 0)
6721 bmap = isl_basic_map_simplify(bmap);
6722 bmap = isl_basic_map_finalize(bmap);
6723 return bmap;
6724 error:
6725 isl_basic_map_free(bmap);
6726 isl_local_space_free(ls);
6727 return NULL;
6730 /* Replace the space of "map" by the space and divs of "ls".
6732 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6733 __isl_take isl_local_space *ls)
6735 int i;
6737 map = isl_map_cow(map);
6738 if (!map || !ls)
6739 goto error;
6741 for (i = 0; i < map->n; ++i) {
6742 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6743 isl_local_space_copy(ls));
6744 if (!map->p[i])
6745 goto error;
6747 isl_space_free(map->dim);
6748 map->dim = isl_local_space_get_space(ls);
6749 if (!map->dim)
6750 goto error;
6752 isl_local_space_free(ls);
6753 return map;
6754 error:
6755 isl_local_space_free(ls);
6756 isl_map_free(map);
6757 return NULL;
6760 /* Compute an explicit representation for the existentially
6761 * quantified variables for which do not know any explicit representation yet.
6763 * We first sort the existentially quantified variables so that the
6764 * existentially quantified variables for which we already have an explicit
6765 * representation are placed before those for which we do not.
6766 * The input dimensions, the output dimensions and the existentially
6767 * quantified variables for which we already have an explicit
6768 * representation are then turned into parameters.
6769 * compute_divs returns a map with the same parameters and
6770 * no input or output dimensions and the dimension specification
6771 * is reset to that of the input, including the existentially quantified
6772 * variables for which we already had an explicit representation.
6774 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6776 struct isl_basic_set *bset;
6777 struct isl_set *set;
6778 struct isl_map *map;
6779 isl_space *dim;
6780 isl_local_space *ls;
6781 unsigned nparam;
6782 unsigned n_in;
6783 unsigned n_out;
6784 unsigned n_known;
6785 int i;
6787 bmap = isl_basic_map_sort_divs(bmap);
6788 bmap = isl_basic_map_cow(bmap);
6789 if (!bmap)
6790 return NULL;
6792 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6793 if (isl_int_is_zero(bmap->div[n_known][0]))
6794 break;
6796 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6797 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6798 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6799 dim = isl_space_set_alloc(bmap->ctx,
6800 nparam + n_in + n_out + n_known, 0);
6801 if (!dim)
6802 goto error;
6804 ls = isl_basic_map_get_local_space(bmap);
6805 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6806 n_known, bmap->n_div - n_known);
6807 if (n_known > 0) {
6808 for (i = n_known; i < bmap->n_div; ++i)
6809 swap_div(bmap, i - n_known, i);
6810 bmap->n_div -= n_known;
6811 bmap->extra -= n_known;
6813 bmap = isl_basic_map_reset_space(bmap, dim);
6814 bset = (struct isl_basic_set *)bmap;
6816 set = parameter_compute_divs(bset);
6817 map = (struct isl_map *)set;
6818 map = replace_space_by_local_space(map, ls);
6820 return map;
6821 error:
6822 isl_basic_map_free(bmap);
6823 return NULL;
6826 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6828 int i;
6829 unsigned off;
6831 if (!bmap)
6832 return -1;
6834 off = isl_space_dim(bmap->dim, isl_dim_all);
6835 for (i = 0; i < bmap->n_div; ++i) {
6836 if (isl_int_is_zero(bmap->div[i][0]))
6837 return 0;
6838 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6839 return -1);
6841 return 1;
6844 static int map_divs_known(__isl_keep isl_map *map)
6846 int i;
6848 if (!map)
6849 return -1;
6851 for (i = 0; i < map->n; ++i) {
6852 int known = isl_basic_map_divs_known(map->p[i]);
6853 if (known <= 0)
6854 return known;
6857 return 1;
6860 /* If bmap contains any unknown divs, then compute explicit
6861 * expressions for them. However, this computation may be
6862 * quite expensive, so first try to remove divs that aren't
6863 * strictly needed.
6865 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6867 int known;
6868 struct isl_map *map;
6870 known = isl_basic_map_divs_known(bmap);
6871 if (known < 0)
6872 goto error;
6873 if (known)
6874 return isl_map_from_basic_map(bmap);
6876 bmap = isl_basic_map_drop_redundant_divs(bmap);
6878 known = isl_basic_map_divs_known(bmap);
6879 if (known < 0)
6880 goto error;
6881 if (known)
6882 return isl_map_from_basic_map(bmap);
6884 map = compute_divs(bmap);
6885 return map;
6886 error:
6887 isl_basic_map_free(bmap);
6888 return NULL;
6891 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6893 int i;
6894 int known;
6895 struct isl_map *res;
6897 if (!map)
6898 return NULL;
6899 if (map->n == 0)
6900 return map;
6902 known = map_divs_known(map);
6903 if (known < 0) {
6904 isl_map_free(map);
6905 return NULL;
6907 if (known)
6908 return map;
6910 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6911 for (i = 1 ; i < map->n; ++i) {
6912 struct isl_map *r2;
6913 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6914 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6915 res = isl_map_union_disjoint(res, r2);
6916 else
6917 res = isl_map_union(res, r2);
6919 isl_map_free(map);
6921 return res;
6924 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6926 return (struct isl_set *)
6927 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6930 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6932 return (struct isl_set *)
6933 isl_map_compute_divs((struct isl_map *)set);
6936 struct isl_set *isl_map_domain(struct isl_map *map)
6938 int i;
6939 struct isl_set *set;
6941 if (!map)
6942 goto error;
6944 map = isl_map_cow(map);
6945 if (!map)
6946 return NULL;
6948 set = (struct isl_set *)map;
6949 set->dim = isl_space_domain(set->dim);
6950 if (!set->dim)
6951 goto error;
6952 for (i = 0; i < map->n; ++i) {
6953 set->p[i] = isl_basic_map_domain(map->p[i]);
6954 if (!set->p[i])
6955 goto error;
6957 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6958 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6959 return set;
6960 error:
6961 isl_map_free(map);
6962 return NULL;
6965 /* Return the union of "map1" and "map2", where we assume for now that
6966 * "map1" and "map2" are disjoint. Note that the basic maps inside
6967 * "map1" or "map2" may not be disjoint from each other.
6968 * Also note that this function is also called from isl_map_union,
6969 * which takes care of handling the situation where "map1" and "map2"
6970 * may not be disjoint.
6972 * If one of the inputs is empty, we can simply return the other input.
6973 * Similarly, if one of the inputs is universal, then it is equal to the union.
6975 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6976 __isl_take isl_map *map2)
6978 int i;
6979 unsigned flags = 0;
6980 struct isl_map *map = NULL;
6981 int is_universe;
6983 if (!map1 || !map2)
6984 goto error;
6986 if (map1->n == 0) {
6987 isl_map_free(map1);
6988 return map2;
6990 if (map2->n == 0) {
6991 isl_map_free(map2);
6992 return map1;
6995 is_universe = isl_map_plain_is_universe(map1);
6996 if (is_universe < 0)
6997 goto error;
6998 if (is_universe) {
6999 isl_map_free(map2);
7000 return map1;
7003 is_universe = isl_map_plain_is_universe(map2);
7004 if (is_universe < 0)
7005 goto error;
7006 if (is_universe) {
7007 isl_map_free(map1);
7008 return map2;
7011 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
7013 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7014 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7015 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7017 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7018 map1->n + map2->n, flags);
7019 if (!map)
7020 goto error;
7021 for (i = 0; i < map1->n; ++i) {
7022 map = isl_map_add_basic_map(map,
7023 isl_basic_map_copy(map1->p[i]));
7024 if (!map)
7025 goto error;
7027 for (i = 0; i < map2->n; ++i) {
7028 map = isl_map_add_basic_map(map,
7029 isl_basic_map_copy(map2->p[i]));
7030 if (!map)
7031 goto error;
7033 isl_map_free(map1);
7034 isl_map_free(map2);
7035 return map;
7036 error:
7037 isl_map_free(map);
7038 isl_map_free(map1);
7039 isl_map_free(map2);
7040 return NULL;
7043 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7044 __isl_take isl_map *map2)
7046 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7049 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7051 map1 = isl_map_union_disjoint(map1, map2);
7052 if (!map1)
7053 return NULL;
7054 if (map1->n > 1)
7055 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7056 return map1;
7059 struct isl_set *isl_set_union_disjoint(
7060 struct isl_set *set1, struct isl_set *set2)
7062 return (struct isl_set *)
7063 isl_map_union_disjoint(
7064 (struct isl_map *)set1, (struct isl_map *)set2);
7067 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7069 return (struct isl_set *)
7070 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7073 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7074 * the results.
7076 * "map" and "set" are assumed to be compatible and non-NULL.
7078 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7079 __isl_take isl_set *set,
7080 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7081 __isl_take isl_basic_set *bset))
7083 unsigned flags = 0;
7084 struct isl_map *result;
7085 int i, j;
7087 if (isl_set_plain_is_universe(set)) {
7088 isl_set_free(set);
7089 return map;
7092 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7093 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7094 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7096 result = isl_map_alloc_space(isl_space_copy(map->dim),
7097 map->n * set->n, flags);
7098 for (i = 0; result && i < map->n; ++i)
7099 for (j = 0; j < set->n; ++j) {
7100 result = isl_map_add_basic_map(result,
7101 fn(isl_basic_map_copy(map->p[i]),
7102 isl_basic_set_copy(set->p[j])));
7103 if (!result)
7104 break;
7107 isl_map_free(map);
7108 isl_set_free(set);
7109 return result;
7112 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7113 __isl_take isl_set *set)
7115 if (!map || !set)
7116 goto error;
7118 if (!isl_map_compatible_range(map, set))
7119 isl_die(set->ctx, isl_error_invalid,
7120 "incompatible spaces", goto error);
7122 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7123 error:
7124 isl_map_free(map);
7125 isl_set_free(set);
7126 return NULL;
7129 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7130 __isl_take isl_set *set)
7132 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7135 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7136 __isl_take isl_set *set)
7138 if (!map || !set)
7139 goto error;
7141 if (!isl_map_compatible_domain(map, set))
7142 isl_die(set->ctx, isl_error_invalid,
7143 "incompatible spaces", goto error);
7145 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7146 error:
7147 isl_map_free(map);
7148 isl_set_free(set);
7149 return NULL;
7152 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7153 __isl_take isl_set *set)
7155 return isl_map_align_params_map_map_and(map, set,
7156 &map_intersect_domain);
7159 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7160 __isl_take isl_map *map2)
7162 if (!map1 || !map2)
7163 goto error;
7164 map1 = isl_map_reverse(map1);
7165 map1 = isl_map_apply_range(map1, map2);
7166 return isl_map_reverse(map1);
7167 error:
7168 isl_map_free(map1);
7169 isl_map_free(map2);
7170 return NULL;
7173 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7174 __isl_take isl_map *map2)
7176 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7179 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7180 __isl_take isl_map *map2)
7182 isl_space *dim_result;
7183 struct isl_map *result;
7184 int i, j;
7186 if (!map1 || !map2)
7187 goto error;
7189 dim_result = isl_space_join(isl_space_copy(map1->dim),
7190 isl_space_copy(map2->dim));
7192 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7193 if (!result)
7194 goto error;
7195 for (i = 0; i < map1->n; ++i)
7196 for (j = 0; j < map2->n; ++j) {
7197 result = isl_map_add_basic_map(result,
7198 isl_basic_map_apply_range(
7199 isl_basic_map_copy(map1->p[i]),
7200 isl_basic_map_copy(map2->p[j])));
7201 if (!result)
7202 goto error;
7204 isl_map_free(map1);
7205 isl_map_free(map2);
7206 if (result && result->n <= 1)
7207 ISL_F_SET(result, ISL_MAP_DISJOINT);
7208 return result;
7209 error:
7210 isl_map_free(map1);
7211 isl_map_free(map2);
7212 return NULL;
7215 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7216 __isl_take isl_map *map2)
7218 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7222 * returns range - domain
7224 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7226 isl_space *dims, *target_dim;
7227 struct isl_basic_set *bset;
7228 unsigned dim;
7229 unsigned nparam;
7230 int i;
7232 if (!bmap)
7233 goto error;
7234 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7235 bmap->dim, isl_dim_out),
7236 goto error);
7237 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7238 dim = isl_basic_map_n_in(bmap);
7239 nparam = isl_basic_map_n_param(bmap);
7240 bset = isl_basic_set_from_basic_map(bmap);
7241 bset = isl_basic_set_cow(bset);
7242 dims = isl_basic_set_get_space(bset);
7243 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7244 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7245 bset = isl_basic_set_swap_vars(bset, 2*dim);
7246 for (i = 0; i < dim; ++i) {
7247 int j = isl_basic_map_alloc_equality(
7248 (struct isl_basic_map *)bset);
7249 if (j < 0) {
7250 bset = isl_basic_set_free(bset);
7251 break;
7253 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7254 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7255 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7256 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7258 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7259 bset = isl_basic_set_reset_space(bset, target_dim);
7260 return bset;
7261 error:
7262 isl_basic_map_free(bmap);
7263 return NULL;
7267 * returns range - domain
7269 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7271 int i;
7272 isl_space *dim;
7273 struct isl_set *result;
7275 if (!map)
7276 return NULL;
7278 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7279 map->dim, isl_dim_out),
7280 goto error);
7281 dim = isl_map_get_space(map);
7282 dim = isl_space_domain(dim);
7283 result = isl_set_alloc_space(dim, map->n, 0);
7284 if (!result)
7285 goto error;
7286 for (i = 0; i < map->n; ++i)
7287 result = isl_set_add_basic_set(result,
7288 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7289 isl_map_free(map);
7290 return result;
7291 error:
7292 isl_map_free(map);
7293 return NULL;
7297 * returns [domain -> range] -> range - domain
7299 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7300 __isl_take isl_basic_map *bmap)
7302 int i, k;
7303 isl_space *dim;
7304 isl_basic_map *domain;
7305 int nparam, n;
7306 unsigned total;
7308 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7309 isl_die(bmap->ctx, isl_error_invalid,
7310 "domain and range don't match", goto error);
7312 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7313 n = isl_basic_map_dim(bmap, isl_dim_in);
7315 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7316 domain = isl_basic_map_universe(dim);
7318 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7319 bmap = isl_basic_map_apply_range(bmap, domain);
7320 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7322 total = isl_basic_map_total_dim(bmap);
7324 for (i = 0; i < n; ++i) {
7325 k = isl_basic_map_alloc_equality(bmap);
7326 if (k < 0)
7327 goto error;
7328 isl_seq_clr(bmap->eq[k], 1 + total);
7329 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7330 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7331 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7334 bmap = isl_basic_map_gauss(bmap, NULL);
7335 return isl_basic_map_finalize(bmap);
7336 error:
7337 isl_basic_map_free(bmap);
7338 return NULL;
7342 * returns [domain -> range] -> range - domain
7344 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7346 int i;
7347 isl_space *domain_dim;
7349 if (!map)
7350 return NULL;
7352 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7353 isl_die(map->ctx, isl_error_invalid,
7354 "domain and range don't match", goto error);
7356 map = isl_map_cow(map);
7357 if (!map)
7358 return NULL;
7360 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7361 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7362 map->dim = isl_space_join(map->dim, domain_dim);
7363 if (!map->dim)
7364 goto error;
7365 for (i = 0; i < map->n; ++i) {
7366 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7367 if (!map->p[i])
7368 goto error;
7370 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7371 return map;
7372 error:
7373 isl_map_free(map);
7374 return NULL;
7377 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7379 struct isl_basic_map *bmap;
7380 unsigned nparam;
7381 unsigned dim;
7382 int i;
7384 if (!dims)
7385 return NULL;
7387 nparam = dims->nparam;
7388 dim = dims->n_out;
7389 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7390 if (!bmap)
7391 goto error;
7393 for (i = 0; i < dim; ++i) {
7394 int j = isl_basic_map_alloc_equality(bmap);
7395 if (j < 0)
7396 goto error;
7397 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7398 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7399 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7401 return isl_basic_map_finalize(bmap);
7402 error:
7403 isl_basic_map_free(bmap);
7404 return NULL;
7407 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7409 if (!dim)
7410 return NULL;
7411 if (dim->n_in != dim->n_out)
7412 isl_die(dim->ctx, isl_error_invalid,
7413 "number of input and output dimensions needs to be "
7414 "the same", goto error);
7415 return basic_map_identity(dim);
7416 error:
7417 isl_space_free(dim);
7418 return NULL;
7421 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7423 if (!model || !model->dim)
7424 return NULL;
7425 return isl_basic_map_identity(isl_space_copy(model->dim));
7428 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7430 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7433 struct isl_map *isl_map_identity_like(struct isl_map *model)
7435 if (!model || !model->dim)
7436 return NULL;
7437 return isl_map_identity(isl_space_copy(model->dim));
7440 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7442 if (!model || !model->dim)
7443 return NULL;
7444 return isl_map_identity(isl_space_copy(model->dim));
7447 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7449 isl_space *dim = isl_set_get_space(set);
7450 isl_map *id;
7451 id = isl_map_identity(isl_space_map_from_set(dim));
7452 return isl_map_intersect_range(id, set);
7455 /* Construct a basic set with all set dimensions having only non-negative
7456 * values.
7458 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7459 __isl_take isl_space *space)
7461 int i;
7462 unsigned nparam;
7463 unsigned dim;
7464 struct isl_basic_set *bset;
7466 if (!space)
7467 return NULL;
7468 nparam = space->nparam;
7469 dim = space->n_out;
7470 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7471 if (!bset)
7472 return NULL;
7473 for (i = 0; i < dim; ++i) {
7474 int k = isl_basic_set_alloc_inequality(bset);
7475 if (k < 0)
7476 goto error;
7477 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7478 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7480 return bset;
7481 error:
7482 isl_basic_set_free(bset);
7483 return NULL;
7486 /* Construct the half-space x_pos >= 0.
7488 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7489 int pos)
7491 int k;
7492 isl_basic_set *nonneg;
7494 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7495 k = isl_basic_set_alloc_inequality(nonneg);
7496 if (k < 0)
7497 goto error;
7498 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7499 isl_int_set_si(nonneg->ineq[k][pos], 1);
7501 return isl_basic_set_finalize(nonneg);
7502 error:
7503 isl_basic_set_free(nonneg);
7504 return NULL;
7507 /* Construct the half-space x_pos <= -1.
7509 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7511 int k;
7512 isl_basic_set *neg;
7514 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7515 k = isl_basic_set_alloc_inequality(neg);
7516 if (k < 0)
7517 goto error;
7518 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7519 isl_int_set_si(neg->ineq[k][0], -1);
7520 isl_int_set_si(neg->ineq[k][pos], -1);
7522 return isl_basic_set_finalize(neg);
7523 error:
7524 isl_basic_set_free(neg);
7525 return NULL;
7528 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7529 enum isl_dim_type type, unsigned first, unsigned n)
7531 int i;
7532 isl_basic_set *nonneg;
7533 isl_basic_set *neg;
7535 if (!set)
7536 return NULL;
7537 if (n == 0)
7538 return set;
7540 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7542 for (i = 0; i < n; ++i) {
7543 nonneg = nonneg_halfspace(isl_set_get_space(set),
7544 pos(set->dim, type) + first + i);
7545 neg = neg_halfspace(isl_set_get_space(set),
7546 pos(set->dim, type) + first + i);
7548 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7551 return set;
7552 error:
7553 isl_set_free(set);
7554 return NULL;
7557 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7558 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7559 void *user)
7561 isl_set *half;
7563 if (!set)
7564 return -1;
7565 if (isl_set_plain_is_empty(set)) {
7566 isl_set_free(set);
7567 return 0;
7569 if (first == len)
7570 return fn(set, signs, user);
7572 signs[first] = 1;
7573 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7574 1 + first));
7575 half = isl_set_intersect(half, isl_set_copy(set));
7576 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7577 goto error;
7579 signs[first] = -1;
7580 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7581 1 + first));
7582 half = isl_set_intersect(half, set);
7583 return foreach_orthant(half, signs, first + 1, len, fn, user);
7584 error:
7585 isl_set_free(set);
7586 return -1;
7589 /* Call "fn" on the intersections of "set" with each of the orthants
7590 * (except for obviously empty intersections). The orthant is identified
7591 * by the signs array, with each entry having value 1 or -1 according
7592 * to the sign of the corresponding variable.
7594 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7595 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7596 void *user)
7598 unsigned nparam;
7599 unsigned nvar;
7600 int *signs;
7601 int r;
7603 if (!set)
7604 return -1;
7605 if (isl_set_plain_is_empty(set))
7606 return 0;
7608 nparam = isl_set_dim(set, isl_dim_param);
7609 nvar = isl_set_dim(set, isl_dim_set);
7611 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7613 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7614 fn, user);
7616 free(signs);
7618 return r;
7621 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7623 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7626 int isl_basic_map_is_subset(
7627 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7629 int is_subset;
7630 struct isl_map *map1;
7631 struct isl_map *map2;
7633 if (!bmap1 || !bmap2)
7634 return -1;
7636 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7637 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7639 is_subset = isl_map_is_subset(map1, map2);
7641 isl_map_free(map1);
7642 isl_map_free(map2);
7644 return is_subset;
7647 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7648 __isl_keep isl_basic_set *bset2)
7650 return isl_basic_map_is_subset(bset1, bset2);
7653 int isl_basic_map_is_equal(
7654 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7656 int is_subset;
7658 if (!bmap1 || !bmap2)
7659 return -1;
7660 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7661 if (is_subset != 1)
7662 return is_subset;
7663 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7664 return is_subset;
7667 int isl_basic_set_is_equal(
7668 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7670 return isl_basic_map_is_equal(
7671 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7674 int isl_map_is_empty(struct isl_map *map)
7676 int i;
7677 int is_empty;
7679 if (!map)
7680 return -1;
7681 for (i = 0; i < map->n; ++i) {
7682 is_empty = isl_basic_map_is_empty(map->p[i]);
7683 if (is_empty < 0)
7684 return -1;
7685 if (!is_empty)
7686 return 0;
7688 return 1;
7691 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7693 return map ? map->n == 0 : -1;
7696 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7698 return isl_map_plain_is_empty(map);
7701 int isl_set_plain_is_empty(struct isl_set *set)
7703 return set ? set->n == 0 : -1;
7706 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7708 return isl_set_plain_is_empty(set);
7711 int isl_set_is_empty(struct isl_set *set)
7713 return isl_map_is_empty((struct isl_map *)set);
7716 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7718 if (!map1 || !map2)
7719 return -1;
7721 return isl_space_is_equal(map1->dim, map2->dim);
7724 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7726 if (!set1 || !set2)
7727 return -1;
7729 return isl_space_is_equal(set1->dim, set2->dim);
7732 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7734 int is_subset;
7736 if (!map1 || !map2)
7737 return -1;
7738 is_subset = isl_map_is_subset(map1, map2);
7739 if (is_subset != 1)
7740 return is_subset;
7741 is_subset = isl_map_is_subset(map2, map1);
7742 return is_subset;
7745 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7747 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7750 int isl_basic_map_is_strict_subset(
7751 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7753 int is_subset;
7755 if (!bmap1 || !bmap2)
7756 return -1;
7757 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7758 if (is_subset != 1)
7759 return is_subset;
7760 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7761 if (is_subset == -1)
7762 return is_subset;
7763 return !is_subset;
7766 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7768 int is_subset;
7770 if (!map1 || !map2)
7771 return -1;
7772 is_subset = isl_map_is_subset(map1, map2);
7773 if (is_subset != 1)
7774 return is_subset;
7775 is_subset = isl_map_is_subset(map2, map1);
7776 if (is_subset == -1)
7777 return is_subset;
7778 return !is_subset;
7781 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7783 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7786 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7788 if (!bmap)
7789 return -1;
7790 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7793 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7795 if (!bset)
7796 return -1;
7797 return bset->n_eq == 0 && bset->n_ineq == 0;
7800 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7802 int i;
7804 if (!map)
7805 return -1;
7807 for (i = 0; i < map->n; ++i) {
7808 int r = isl_basic_map_is_universe(map->p[i]);
7809 if (r < 0 || r)
7810 return r;
7813 return 0;
7816 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7818 return isl_map_plain_is_universe((isl_map *) set);
7821 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7823 return isl_set_plain_is_universe(set);
7826 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7828 struct isl_basic_set *bset = NULL;
7829 struct isl_vec *sample = NULL;
7830 int empty;
7831 unsigned total;
7833 if (!bmap)
7834 return -1;
7836 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7837 return 1;
7839 if (isl_basic_map_is_universe(bmap))
7840 return 0;
7842 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7843 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7844 copy = isl_basic_map_remove_redundancies(copy);
7845 empty = isl_basic_map_plain_is_empty(copy);
7846 isl_basic_map_free(copy);
7847 return empty;
7850 total = 1 + isl_basic_map_total_dim(bmap);
7851 if (bmap->sample && bmap->sample->size == total) {
7852 int contains = isl_basic_map_contains(bmap, bmap->sample);
7853 if (contains < 0)
7854 return -1;
7855 if (contains)
7856 return 0;
7858 isl_vec_free(bmap->sample);
7859 bmap->sample = NULL;
7860 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7861 if (!bset)
7862 return -1;
7863 sample = isl_basic_set_sample_vec(bset);
7864 if (!sample)
7865 return -1;
7866 empty = sample->size == 0;
7867 isl_vec_free(bmap->sample);
7868 bmap->sample = sample;
7869 if (empty)
7870 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7872 return empty;
7875 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7877 if (!bmap)
7878 return -1;
7879 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7882 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7884 return isl_basic_map_plain_is_empty(bmap);
7887 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7889 if (!bset)
7890 return -1;
7891 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7894 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7896 return isl_basic_set_plain_is_empty(bset);
7899 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7901 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7904 struct isl_map *isl_basic_map_union(
7905 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7907 struct isl_map *map;
7908 if (!bmap1 || !bmap2)
7909 goto error;
7911 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7913 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7914 if (!map)
7915 goto error;
7916 map = isl_map_add_basic_map(map, bmap1);
7917 map = isl_map_add_basic_map(map, bmap2);
7918 return map;
7919 error:
7920 isl_basic_map_free(bmap1);
7921 isl_basic_map_free(bmap2);
7922 return NULL;
7925 struct isl_set *isl_basic_set_union(
7926 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7928 return (struct isl_set *)isl_basic_map_union(
7929 (struct isl_basic_map *)bset1,
7930 (struct isl_basic_map *)bset2);
7933 /* Order divs such that any div only depends on previous divs */
7934 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7936 int i;
7937 unsigned off;
7939 if (!bmap)
7940 return NULL;
7942 off = isl_space_dim(bmap->dim, isl_dim_all);
7944 for (i = 0; i < bmap->n_div; ++i) {
7945 int pos;
7946 if (isl_int_is_zero(bmap->div[i][0]))
7947 continue;
7948 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7949 bmap->n_div-i);
7950 if (pos == -1)
7951 continue;
7952 isl_basic_map_swap_div(bmap, i, i + pos);
7953 --i;
7955 return bmap;
7958 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7960 return (struct isl_basic_set *)
7961 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7964 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7966 int i;
7968 if (!map)
7969 return 0;
7971 for (i = 0; i < map->n; ++i) {
7972 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7973 if (!map->p[i])
7974 goto error;
7977 return map;
7978 error:
7979 isl_map_free(map);
7980 return NULL;
7983 /* Apply the expansion computed by isl_merge_divs.
7984 * The expansion itself is given by "exp" while the resulting
7985 * list of divs is given by "div".
7987 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7988 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7990 int i, j;
7991 int n_div;
7993 bset = isl_basic_set_cow(bset);
7994 if (!bset || !div)
7995 goto error;
7997 if (div->n_row < bset->n_div)
7998 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7999 "not an expansion", goto error);
8001 n_div = bset->n_div;
8002 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8003 div->n_row - n_div, 0,
8004 2 * (div->n_row - n_div));
8006 for (i = n_div; i < div->n_row; ++i)
8007 if (isl_basic_set_alloc_div(bset) < 0)
8008 goto error;
8010 j = n_div - 1;
8011 for (i = div->n_row - 1; i >= 0; --i) {
8012 if (j >= 0 && exp[j] == i) {
8013 if (i != j)
8014 isl_basic_map_swap_div(bset, i, j);
8015 j--;
8016 } else {
8017 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8018 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8019 goto error;
8023 isl_mat_free(div);
8024 return bset;
8025 error:
8026 isl_basic_set_free(bset);
8027 isl_mat_free(div);
8028 return NULL;
8031 /* Look for a div in dst that corresponds to the div "div" in src.
8032 * The divs before "div" in src and dst are assumed to be the same.
8034 * Returns -1 if no corresponding div was found and the position
8035 * of the corresponding div in dst otherwise.
8037 static int find_div(struct isl_basic_map *dst,
8038 struct isl_basic_map *src, unsigned div)
8040 int i;
8042 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8044 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8045 for (i = div; i < dst->n_div; ++i)
8046 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8047 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8048 dst->n_div - div) == -1)
8049 return i;
8050 return -1;
8053 struct isl_basic_map *isl_basic_map_align_divs(
8054 struct isl_basic_map *dst, struct isl_basic_map *src)
8056 int i;
8057 unsigned total;
8059 if (!dst || !src)
8060 goto error;
8062 if (src->n_div == 0)
8063 return dst;
8065 for (i = 0; i < src->n_div; ++i)
8066 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8068 src = isl_basic_map_order_divs(src);
8069 dst = isl_basic_map_cow(dst);
8070 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8071 src->n_div, 0, 2 * src->n_div);
8072 if (!dst)
8073 return NULL;
8074 total = isl_space_dim(src->dim, isl_dim_all);
8075 for (i = 0; i < src->n_div; ++i) {
8076 int j = find_div(dst, src, i);
8077 if (j < 0) {
8078 j = isl_basic_map_alloc_div(dst);
8079 if (j < 0)
8080 goto error;
8081 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8082 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8083 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8084 goto error;
8086 if (j != i)
8087 isl_basic_map_swap_div(dst, i, j);
8089 return dst;
8090 error:
8091 isl_basic_map_free(dst);
8092 return NULL;
8095 struct isl_basic_set *isl_basic_set_align_divs(
8096 struct isl_basic_set *dst, struct isl_basic_set *src)
8098 return (struct isl_basic_set *)isl_basic_map_align_divs(
8099 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8102 struct isl_map *isl_map_align_divs(struct isl_map *map)
8104 int i;
8106 if (!map)
8107 return NULL;
8108 if (map->n == 0)
8109 return map;
8110 map = isl_map_compute_divs(map);
8111 map = isl_map_cow(map);
8112 if (!map)
8113 return NULL;
8115 for (i = 1; i < map->n; ++i)
8116 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8117 for (i = 1; i < map->n; ++i)
8118 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8120 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8121 return map;
8124 struct isl_set *isl_set_align_divs(struct isl_set *set)
8126 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8129 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8130 __isl_take isl_map *map)
8132 if (!set || !map)
8133 goto error;
8134 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8135 map = isl_map_intersect_domain(map, set);
8136 set = isl_map_range(map);
8137 return set;
8138 error:
8139 isl_set_free(set);
8140 isl_map_free(map);
8141 return NULL;
8144 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8145 __isl_take isl_map *map)
8147 return isl_map_align_params_map_map_and(set, map, &set_apply);
8150 /* There is no need to cow as removing empty parts doesn't change
8151 * the meaning of the set.
8153 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8155 int i;
8157 if (!map)
8158 return NULL;
8160 for (i = map->n - 1; i >= 0; --i)
8161 remove_if_empty(map, i);
8163 return map;
8166 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8168 return (struct isl_set *)
8169 isl_map_remove_empty_parts((struct isl_map *)set);
8172 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8174 struct isl_basic_map *bmap;
8175 if (!map || map->n == 0)
8176 return NULL;
8177 bmap = map->p[map->n-1];
8178 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8179 return isl_basic_map_copy(bmap);
8182 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8184 return (struct isl_basic_set *)
8185 isl_map_copy_basic_map((struct isl_map *)set);
8188 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8189 __isl_keep isl_basic_map *bmap)
8191 int i;
8193 if (!map || !bmap)
8194 goto error;
8195 for (i = map->n-1; i >= 0; --i) {
8196 if (map->p[i] != bmap)
8197 continue;
8198 map = isl_map_cow(map);
8199 if (!map)
8200 goto error;
8201 isl_basic_map_free(map->p[i]);
8202 if (i != map->n-1) {
8203 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8204 map->p[i] = map->p[map->n-1];
8206 map->n--;
8207 return map;
8209 return map;
8210 error:
8211 isl_map_free(map);
8212 return NULL;
8215 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8216 struct isl_basic_set *bset)
8218 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8219 (struct isl_basic_map *)bset);
8222 /* Given two basic sets bset1 and bset2, compute the maximal difference
8223 * between the values of dimension pos in bset1 and those in bset2
8224 * for any common value of the parameters and dimensions preceding pos.
8226 static enum isl_lp_result basic_set_maximal_difference_at(
8227 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8228 int pos, isl_int *opt)
8230 isl_space *dims;
8231 struct isl_basic_map *bmap1 = NULL;
8232 struct isl_basic_map *bmap2 = NULL;
8233 struct isl_ctx *ctx;
8234 struct isl_vec *obj;
8235 unsigned total;
8236 unsigned nparam;
8237 unsigned dim1, dim2;
8238 enum isl_lp_result res;
8240 if (!bset1 || !bset2)
8241 return isl_lp_error;
8243 nparam = isl_basic_set_n_param(bset1);
8244 dim1 = isl_basic_set_n_dim(bset1);
8245 dim2 = isl_basic_set_n_dim(bset2);
8246 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8247 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8248 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8249 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8250 if (!bmap1 || !bmap2)
8251 goto error;
8252 bmap1 = isl_basic_map_cow(bmap1);
8253 bmap1 = isl_basic_map_extend(bmap1, nparam,
8254 pos, (dim1 - pos) + (dim2 - pos),
8255 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8256 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8257 if (!bmap1)
8258 goto error;
8259 total = isl_basic_map_total_dim(bmap1);
8260 ctx = bmap1->ctx;
8261 obj = isl_vec_alloc(ctx, 1 + total);
8262 if (!obj)
8263 goto error2;
8264 isl_seq_clr(obj->block.data, 1 + total);
8265 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8266 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8267 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8268 opt, NULL, NULL);
8269 isl_basic_map_free(bmap1);
8270 isl_vec_free(obj);
8271 return res;
8272 error:
8273 isl_basic_map_free(bmap2);
8274 error2:
8275 isl_basic_map_free(bmap1);
8276 return isl_lp_error;
8279 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8280 * for any common value of the parameters and dimensions preceding pos
8281 * in both basic sets, the values of dimension pos in bset1 are
8282 * smaller or larger than those in bset2.
8284 * Returns
8285 * 1 if bset1 follows bset2
8286 * -1 if bset1 precedes bset2
8287 * 0 if bset1 and bset2 are incomparable
8288 * -2 if some error occurred.
8290 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8291 struct isl_basic_set *bset2, int pos)
8293 isl_int opt;
8294 enum isl_lp_result res;
8295 int cmp;
8297 isl_int_init(opt);
8299 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8301 if (res == isl_lp_empty)
8302 cmp = 0;
8303 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8304 res == isl_lp_unbounded)
8305 cmp = 1;
8306 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8307 cmp = -1;
8308 else
8309 cmp = -2;
8311 isl_int_clear(opt);
8312 return cmp;
8315 /* Given two basic sets bset1 and bset2, check whether
8316 * for any common value of the parameters and dimensions preceding pos
8317 * there is a value of dimension pos in bset1 that is larger
8318 * than a value of the same dimension in bset2.
8320 * Return
8321 * 1 if there exists such a pair
8322 * 0 if there is no such pair, but there is a pair of equal values
8323 * -1 otherwise
8324 * -2 if some error occurred.
8326 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8327 __isl_keep isl_basic_set *bset2, int pos)
8329 isl_int opt;
8330 enum isl_lp_result res;
8331 int cmp;
8333 isl_int_init(opt);
8335 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8337 if (res == isl_lp_empty)
8338 cmp = -1;
8339 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8340 res == isl_lp_unbounded)
8341 cmp = 1;
8342 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8343 cmp = -1;
8344 else if (res == isl_lp_ok)
8345 cmp = 0;
8346 else
8347 cmp = -2;
8349 isl_int_clear(opt);
8350 return cmp;
8353 /* Given two sets set1 and set2, check whether
8354 * for any common value of the parameters and dimensions preceding pos
8355 * there is a value of dimension pos in set1 that is larger
8356 * than a value of the same dimension in set2.
8358 * Return
8359 * 1 if there exists such a pair
8360 * 0 if there is no such pair, but there is a pair of equal values
8361 * -1 otherwise
8362 * -2 if some error occurred.
8364 int isl_set_follows_at(__isl_keep isl_set *set1,
8365 __isl_keep isl_set *set2, int pos)
8367 int i, j;
8368 int follows = -1;
8370 if (!set1 || !set2)
8371 return -2;
8373 for (i = 0; i < set1->n; ++i)
8374 for (j = 0; j < set2->n; ++j) {
8375 int f;
8376 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8377 if (f == 1 || f == -2)
8378 return f;
8379 if (f > follows)
8380 follows = f;
8383 return follows;
8386 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8387 unsigned pos, isl_int *val)
8389 int i;
8390 int d;
8391 unsigned total;
8393 if (!bmap)
8394 return -1;
8395 total = isl_basic_map_total_dim(bmap);
8396 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8397 for (; d+1 > pos; --d)
8398 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8399 break;
8400 if (d != pos)
8401 continue;
8402 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8403 return 0;
8404 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8405 return 0;
8406 if (!isl_int_is_one(bmap->eq[i][1+d]))
8407 return 0;
8408 if (val)
8409 isl_int_neg(*val, bmap->eq[i][0]);
8410 return 1;
8412 return 0;
8415 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8416 unsigned pos, isl_int *val)
8418 int i;
8419 isl_int v;
8420 isl_int tmp;
8421 int fixed;
8423 if (!map)
8424 return -1;
8425 if (map->n == 0)
8426 return 0;
8427 if (map->n == 1)
8428 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8429 isl_int_init(v);
8430 isl_int_init(tmp);
8431 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8432 for (i = 1; fixed == 1 && i < map->n; ++i) {
8433 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8434 if (fixed == 1 && isl_int_ne(tmp, v))
8435 fixed = 0;
8437 if (val)
8438 isl_int_set(*val, v);
8439 isl_int_clear(tmp);
8440 isl_int_clear(v);
8441 return fixed;
8444 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8445 unsigned pos, isl_int *val)
8447 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8448 pos, val);
8451 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8452 isl_int *val)
8454 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8457 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8458 enum isl_dim_type type, unsigned pos, isl_int *val)
8460 if (pos >= isl_basic_map_dim(bmap, type))
8461 return -1;
8462 return isl_basic_map_plain_has_fixed_var(bmap,
8463 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8466 /* If "bmap" obviously lies on a hyperplane where the given dimension
8467 * has a fixed value, then return that value.
8468 * Otherwise return NaN.
8470 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8471 __isl_keep isl_basic_map *bmap,
8472 enum isl_dim_type type, unsigned pos)
8474 isl_ctx *ctx;
8475 isl_val *v;
8476 int fixed;
8478 if (!bmap)
8479 return NULL;
8480 ctx = isl_basic_map_get_ctx(bmap);
8481 v = isl_val_alloc(ctx);
8482 if (!v)
8483 return NULL;
8484 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8485 if (fixed < 0)
8486 return isl_val_free(v);
8487 if (fixed) {
8488 isl_int_set_si(v->d, 1);
8489 return v;
8491 isl_val_free(v);
8492 return isl_val_nan(ctx);
8495 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8496 enum isl_dim_type type, unsigned pos, isl_int *val)
8498 if (pos >= isl_map_dim(map, type))
8499 return -1;
8500 return isl_map_plain_has_fixed_var(map,
8501 map_offset(map, type) - 1 + pos, val);
8504 /* If "map" obviously lies on a hyperplane where the given dimension
8505 * has a fixed value, then return that value.
8506 * Otherwise return NaN.
8508 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8509 enum isl_dim_type type, unsigned pos)
8511 isl_ctx *ctx;
8512 isl_val *v;
8513 int fixed;
8515 if (!map)
8516 return NULL;
8517 ctx = isl_map_get_ctx(map);
8518 v = isl_val_alloc(ctx);
8519 if (!v)
8520 return NULL;
8521 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8522 if (fixed < 0)
8523 return isl_val_free(v);
8524 if (fixed) {
8525 isl_int_set_si(v->d, 1);
8526 return v;
8528 isl_val_free(v);
8529 return isl_val_nan(ctx);
8532 /* If "set" obviously lies on a hyperplane where the given dimension
8533 * has a fixed value, then return that value.
8534 * Otherwise return NaN.
8536 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8537 enum isl_dim_type type, unsigned pos)
8539 return isl_map_plain_get_val_if_fixed(set, type, pos);
8542 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8543 enum isl_dim_type type, unsigned pos, isl_int *val)
8545 return isl_map_plain_is_fixed(set, type, pos, val);
8548 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8549 enum isl_dim_type type, unsigned pos, isl_int *val)
8551 return isl_map_plain_is_fixed(map, type, pos, val);
8554 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8555 * then return this fixed value in *val.
8557 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8558 unsigned dim, isl_int *val)
8560 return isl_basic_set_plain_has_fixed_var(bset,
8561 isl_basic_set_n_param(bset) + dim, val);
8564 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8565 * then return this fixed value in *val.
8567 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8568 unsigned dim, isl_int *val)
8570 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8573 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8574 unsigned dim, isl_int *val)
8576 return isl_set_plain_dim_is_fixed(set, dim, val);
8579 /* Check if input variable in has fixed value and if so and if val is not NULL,
8580 * then return this fixed value in *val.
8582 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8583 unsigned in, isl_int *val)
8585 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8588 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8589 * and if val is not NULL, then return this lower bound in *val.
8591 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8592 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8594 int i, i_eq = -1, i_ineq = -1;
8595 isl_int *c;
8596 unsigned total;
8597 unsigned nparam;
8599 if (!bset)
8600 return -1;
8601 total = isl_basic_set_total_dim(bset);
8602 nparam = isl_basic_set_n_param(bset);
8603 for (i = 0; i < bset->n_eq; ++i) {
8604 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8605 continue;
8606 if (i_eq != -1)
8607 return 0;
8608 i_eq = i;
8610 for (i = 0; i < bset->n_ineq; ++i) {
8611 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8612 continue;
8613 if (i_eq != -1 || i_ineq != -1)
8614 return 0;
8615 i_ineq = i;
8617 if (i_eq == -1 && i_ineq == -1)
8618 return 0;
8619 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8620 /* The coefficient should always be one due to normalization. */
8621 if (!isl_int_is_one(c[1+nparam+dim]))
8622 return 0;
8623 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8624 return 0;
8625 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8626 total - nparam - dim - 1) != -1)
8627 return 0;
8628 if (val)
8629 isl_int_neg(*val, c[0]);
8630 return 1;
8633 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8634 unsigned dim, isl_int *val)
8636 int i;
8637 isl_int v;
8638 isl_int tmp;
8639 int fixed;
8641 if (!set)
8642 return -1;
8643 if (set->n == 0)
8644 return 0;
8645 if (set->n == 1)
8646 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8647 dim, val);
8648 isl_int_init(v);
8649 isl_int_init(tmp);
8650 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8651 dim, &v);
8652 for (i = 1; fixed == 1 && i < set->n; ++i) {
8653 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8654 dim, &tmp);
8655 if (fixed == 1 && isl_int_ne(tmp, v))
8656 fixed = 0;
8658 if (val)
8659 isl_int_set(*val, v);
8660 isl_int_clear(tmp);
8661 isl_int_clear(v);
8662 return fixed;
8665 struct constraint {
8666 unsigned size;
8667 isl_int *c;
8670 /* uset_gist depends on constraints without existentially quantified
8671 * variables sorting first.
8673 static int qsort_constraint_cmp(const void *p1, const void *p2)
8675 const struct constraint *c1 = (const struct constraint *)p1;
8676 const struct constraint *c2 = (const struct constraint *)p2;
8677 int l1, l2;
8678 unsigned size = isl_min(c1->size, c2->size);
8680 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8681 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8683 if (l1 != l2)
8684 return l1 - l2;
8686 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8689 static struct isl_basic_map *isl_basic_map_sort_constraints(
8690 struct isl_basic_map *bmap)
8692 int i;
8693 struct constraint *c;
8694 unsigned total;
8696 if (!bmap)
8697 return NULL;
8698 total = isl_basic_map_total_dim(bmap);
8699 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8700 if (!c)
8701 goto error;
8702 for (i = 0; i < bmap->n_ineq; ++i) {
8703 c[i].size = total;
8704 c[i].c = bmap->ineq[i];
8706 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8707 for (i = 0; i < bmap->n_ineq; ++i)
8708 bmap->ineq[i] = c[i].c;
8709 free(c);
8710 return bmap;
8711 error:
8712 isl_basic_map_free(bmap);
8713 return NULL;
8716 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8717 __isl_take isl_basic_set *bset)
8719 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8720 (struct isl_basic_map *)bset);
8723 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8725 if (!bmap)
8726 return NULL;
8727 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8728 return bmap;
8729 bmap = isl_basic_map_remove_redundancies(bmap);
8730 bmap = isl_basic_map_sort_constraints(bmap);
8731 if (bmap)
8732 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8733 return bmap;
8736 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8738 return (struct isl_basic_set *)isl_basic_map_normalize(
8739 (struct isl_basic_map *)bset);
8742 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8743 const __isl_keep isl_basic_map *bmap2)
8745 int i, cmp;
8746 unsigned total;
8748 if (bmap1 == bmap2)
8749 return 0;
8750 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8751 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8752 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8753 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8754 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8755 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8756 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8757 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8758 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8759 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8760 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8761 return 0;
8762 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8763 return 1;
8764 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8765 return -1;
8766 if (bmap1->n_eq != bmap2->n_eq)
8767 return bmap1->n_eq - bmap2->n_eq;
8768 if (bmap1->n_ineq != bmap2->n_ineq)
8769 return bmap1->n_ineq - bmap2->n_ineq;
8770 if (bmap1->n_div != bmap2->n_div)
8771 return bmap1->n_div - bmap2->n_div;
8772 total = isl_basic_map_total_dim(bmap1);
8773 for (i = 0; i < bmap1->n_eq; ++i) {
8774 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8775 if (cmp)
8776 return cmp;
8778 for (i = 0; i < bmap1->n_ineq; ++i) {
8779 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8780 if (cmp)
8781 return cmp;
8783 for (i = 0; i < bmap1->n_div; ++i) {
8784 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8785 if (cmp)
8786 return cmp;
8788 return 0;
8791 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8792 const __isl_keep isl_basic_set *bset2)
8794 return isl_basic_map_plain_cmp(bset1, bset2);
8797 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8799 int i, cmp;
8801 if (set1 == set2)
8802 return 0;
8803 if (set1->n != set2->n)
8804 return set1->n - set2->n;
8806 for (i = 0; i < set1->n; ++i) {
8807 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8808 if (cmp)
8809 return cmp;
8812 return 0;
8815 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8816 __isl_keep isl_basic_map *bmap2)
8818 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8821 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8822 __isl_keep isl_basic_set *bset2)
8824 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8825 (isl_basic_map *)bset2);
8828 static int qsort_bmap_cmp(const void *p1, const void *p2)
8830 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8831 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8833 return isl_basic_map_plain_cmp(bmap1, bmap2);
8836 /* We normalize in place, but if anything goes wrong we need
8837 * to return NULL, so we need to make sure we don't change the
8838 * meaning of any possible other copies of map.
8840 struct isl_map *isl_map_normalize(struct isl_map *map)
8842 int i, j;
8843 struct isl_basic_map *bmap;
8845 if (!map)
8846 return NULL;
8847 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8848 return map;
8849 for (i = 0; i < map->n; ++i) {
8850 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8851 if (!bmap)
8852 goto error;
8853 isl_basic_map_free(map->p[i]);
8854 map->p[i] = bmap;
8856 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8857 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8858 map = isl_map_remove_empty_parts(map);
8859 if (!map)
8860 return NULL;
8861 for (i = map->n - 1; i >= 1; --i) {
8862 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8863 continue;
8864 isl_basic_map_free(map->p[i-1]);
8865 for (j = i; j < map->n; ++j)
8866 map->p[j-1] = map->p[j];
8867 map->n--;
8869 return map;
8870 error:
8871 isl_map_free(map);
8872 return NULL;
8876 struct isl_set *isl_set_normalize(struct isl_set *set)
8878 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8881 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8883 int i;
8884 int equal;
8886 if (!map1 || !map2)
8887 return -1;
8889 if (map1 == map2)
8890 return 1;
8891 if (!isl_space_is_equal(map1->dim, map2->dim))
8892 return 0;
8894 map1 = isl_map_copy(map1);
8895 map2 = isl_map_copy(map2);
8896 map1 = isl_map_normalize(map1);
8897 map2 = isl_map_normalize(map2);
8898 if (!map1 || !map2)
8899 goto error;
8900 equal = map1->n == map2->n;
8901 for (i = 0; equal && i < map1->n; ++i) {
8902 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8903 if (equal < 0)
8904 goto error;
8906 isl_map_free(map1);
8907 isl_map_free(map2);
8908 return equal;
8909 error:
8910 isl_map_free(map1);
8911 isl_map_free(map2);
8912 return -1;
8915 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8917 return isl_map_plain_is_equal(map1, map2);
8920 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8922 return isl_map_plain_is_equal((struct isl_map *)set1,
8923 (struct isl_map *)set2);
8926 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8928 return isl_set_plain_is_equal(set1, set2);
8931 /* Return an interval that ranges from min to max (inclusive)
8933 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8934 isl_int min, isl_int max)
8936 int k;
8937 struct isl_basic_set *bset = NULL;
8939 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8940 if (!bset)
8941 goto error;
8943 k = isl_basic_set_alloc_inequality(bset);
8944 if (k < 0)
8945 goto error;
8946 isl_int_set_si(bset->ineq[k][1], 1);
8947 isl_int_neg(bset->ineq[k][0], min);
8949 k = isl_basic_set_alloc_inequality(bset);
8950 if (k < 0)
8951 goto error;
8952 isl_int_set_si(bset->ineq[k][1], -1);
8953 isl_int_set(bset->ineq[k][0], max);
8955 return bset;
8956 error:
8957 isl_basic_set_free(bset);
8958 return NULL;
8961 /* Return the Cartesian product of the basic sets in list (in the given order).
8963 __isl_give isl_basic_set *isl_basic_set_list_product(
8964 __isl_take struct isl_basic_set_list *list)
8966 int i;
8967 unsigned dim;
8968 unsigned nparam;
8969 unsigned extra;
8970 unsigned n_eq;
8971 unsigned n_ineq;
8972 struct isl_basic_set *product = NULL;
8974 if (!list)
8975 goto error;
8976 isl_assert(list->ctx, list->n > 0, goto error);
8977 isl_assert(list->ctx, list->p[0], goto error);
8978 nparam = isl_basic_set_n_param(list->p[0]);
8979 dim = isl_basic_set_n_dim(list->p[0]);
8980 extra = list->p[0]->n_div;
8981 n_eq = list->p[0]->n_eq;
8982 n_ineq = list->p[0]->n_ineq;
8983 for (i = 1; i < list->n; ++i) {
8984 isl_assert(list->ctx, list->p[i], goto error);
8985 isl_assert(list->ctx,
8986 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8987 dim += isl_basic_set_n_dim(list->p[i]);
8988 extra += list->p[i]->n_div;
8989 n_eq += list->p[i]->n_eq;
8990 n_ineq += list->p[i]->n_ineq;
8992 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8993 n_eq, n_ineq);
8994 if (!product)
8995 goto error;
8996 dim = 0;
8997 for (i = 0; i < list->n; ++i) {
8998 isl_basic_set_add_constraints(product,
8999 isl_basic_set_copy(list->p[i]), dim);
9000 dim += isl_basic_set_n_dim(list->p[i]);
9002 isl_basic_set_list_free(list);
9003 return product;
9004 error:
9005 isl_basic_set_free(product);
9006 isl_basic_set_list_free(list);
9007 return NULL;
9010 struct isl_basic_map *isl_basic_map_product(
9011 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9013 isl_space *dim_result = NULL;
9014 struct isl_basic_map *bmap;
9015 unsigned in1, in2, out1, out2, nparam, total, pos;
9016 struct isl_dim_map *dim_map1, *dim_map2;
9018 if (!bmap1 || !bmap2)
9019 goto error;
9021 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9022 bmap2->dim, isl_dim_param), goto error);
9023 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9024 isl_space_copy(bmap2->dim));
9026 in1 = isl_basic_map_n_in(bmap1);
9027 in2 = isl_basic_map_n_in(bmap2);
9028 out1 = isl_basic_map_n_out(bmap1);
9029 out2 = isl_basic_map_n_out(bmap2);
9030 nparam = isl_basic_map_n_param(bmap1);
9032 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9033 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9034 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9035 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9036 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9037 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9038 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9039 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9040 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9041 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9042 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9044 bmap = isl_basic_map_alloc_space(dim_result,
9045 bmap1->n_div + bmap2->n_div,
9046 bmap1->n_eq + bmap2->n_eq,
9047 bmap1->n_ineq + bmap2->n_ineq);
9048 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9049 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9050 bmap = isl_basic_map_simplify(bmap);
9051 return isl_basic_map_finalize(bmap);
9052 error:
9053 isl_basic_map_free(bmap1);
9054 isl_basic_map_free(bmap2);
9055 return NULL;
9058 __isl_give isl_basic_map *isl_basic_map_flat_product(
9059 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9061 isl_basic_map *prod;
9063 prod = isl_basic_map_product(bmap1, bmap2);
9064 prod = isl_basic_map_flatten(prod);
9065 return prod;
9068 __isl_give isl_basic_set *isl_basic_set_flat_product(
9069 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9071 return isl_basic_map_flat_range_product(bset1, bset2);
9074 __isl_give isl_basic_map *isl_basic_map_domain_product(
9075 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9077 isl_space *space_result = NULL;
9078 isl_basic_map *bmap;
9079 unsigned in1, in2, out, nparam, total, pos;
9080 struct isl_dim_map *dim_map1, *dim_map2;
9082 if (!bmap1 || !bmap2)
9083 goto error;
9085 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9086 isl_space_copy(bmap2->dim));
9088 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9089 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9090 out = isl_basic_map_dim(bmap1, isl_dim_out);
9091 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9093 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9094 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9095 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9096 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9097 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9098 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9099 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9100 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9101 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9102 isl_dim_map_div(dim_map1, bmap1, pos += out);
9103 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9105 bmap = isl_basic_map_alloc_space(space_result,
9106 bmap1->n_div + bmap2->n_div,
9107 bmap1->n_eq + bmap2->n_eq,
9108 bmap1->n_ineq + bmap2->n_ineq);
9109 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9110 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9111 bmap = isl_basic_map_simplify(bmap);
9112 return isl_basic_map_finalize(bmap);
9113 error:
9114 isl_basic_map_free(bmap1);
9115 isl_basic_map_free(bmap2);
9116 return NULL;
9119 __isl_give isl_basic_map *isl_basic_map_range_product(
9120 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9122 isl_space *dim_result = NULL;
9123 isl_basic_map *bmap;
9124 unsigned in, out1, out2, nparam, total, pos;
9125 struct isl_dim_map *dim_map1, *dim_map2;
9127 if (!bmap1 || !bmap2)
9128 goto error;
9130 if (!isl_space_match(bmap1->dim, isl_dim_param,
9131 bmap2->dim, isl_dim_param))
9132 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9133 "parameters don't match", goto error);
9135 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9136 isl_space_copy(bmap2->dim));
9138 in = isl_basic_map_dim(bmap1, isl_dim_in);
9139 out1 = isl_basic_map_n_out(bmap1);
9140 out2 = isl_basic_map_n_out(bmap2);
9141 nparam = isl_basic_map_n_param(bmap1);
9143 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9144 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9145 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9146 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9147 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9148 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9149 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9150 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9151 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9152 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9153 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9155 bmap = isl_basic_map_alloc_space(dim_result,
9156 bmap1->n_div + bmap2->n_div,
9157 bmap1->n_eq + bmap2->n_eq,
9158 bmap1->n_ineq + bmap2->n_ineq);
9159 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9160 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9161 bmap = isl_basic_map_simplify(bmap);
9162 return isl_basic_map_finalize(bmap);
9163 error:
9164 isl_basic_map_free(bmap1);
9165 isl_basic_map_free(bmap2);
9166 return NULL;
9169 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9170 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9172 isl_basic_map *prod;
9174 prod = isl_basic_map_range_product(bmap1, bmap2);
9175 prod = isl_basic_map_flatten_range(prod);
9176 return prod;
9179 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9180 __isl_take isl_map *map2,
9181 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9182 __isl_take isl_space *right),
9183 __isl_give isl_basic_map *(*basic_map_product)(
9184 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9186 unsigned flags = 0;
9187 struct isl_map *result;
9188 int i, j;
9190 if (!map1 || !map2)
9191 goto error;
9193 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9194 map2->dim, isl_dim_param), goto error);
9196 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9197 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9198 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9200 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9201 isl_space_copy(map2->dim)),
9202 map1->n * map2->n, flags);
9203 if (!result)
9204 goto error;
9205 for (i = 0; i < map1->n; ++i)
9206 for (j = 0; j < map2->n; ++j) {
9207 struct isl_basic_map *part;
9208 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9209 isl_basic_map_copy(map2->p[j]));
9210 if (isl_basic_map_is_empty(part))
9211 isl_basic_map_free(part);
9212 else
9213 result = isl_map_add_basic_map(result, part);
9214 if (!result)
9215 goto error;
9217 isl_map_free(map1);
9218 isl_map_free(map2);
9219 return result;
9220 error:
9221 isl_map_free(map1);
9222 isl_map_free(map2);
9223 return NULL;
9226 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9228 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9229 __isl_take isl_map *map2)
9231 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9234 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9235 __isl_take isl_map *map2)
9237 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9240 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9242 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9243 __isl_take isl_map *map2)
9245 isl_map *prod;
9247 prod = isl_map_product(map1, map2);
9248 prod = isl_map_flatten(prod);
9249 return prod;
9252 /* Given two set A and B, construct its Cartesian product A x B.
9254 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9256 return isl_map_range_product(set1, set2);
9259 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9260 __isl_take isl_set *set2)
9262 return isl_map_flat_range_product(set1, set2);
9265 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9267 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9268 __isl_take isl_map *map2)
9270 return map_product(map1, map2, &isl_space_domain_product,
9271 &isl_basic_map_domain_product);
9274 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9276 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9277 __isl_take isl_map *map2)
9279 return map_product(map1, map2, &isl_space_range_product,
9280 &isl_basic_map_range_product);
9283 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9284 __isl_take isl_map *map2)
9286 return isl_map_align_params_map_map_and(map1, map2,
9287 &map_domain_product_aligned);
9290 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9291 __isl_take isl_map *map2)
9293 return isl_map_align_params_map_map_and(map1, map2,
9294 &map_range_product_aligned);
9297 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9299 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9300 __isl_take isl_map *map2)
9302 isl_map *prod;
9304 prod = isl_map_domain_product(map1, map2);
9305 prod = isl_map_flatten_domain(prod);
9306 return prod;
9309 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9311 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9312 __isl_take isl_map *map2)
9314 isl_map *prod;
9316 prod = isl_map_range_product(map1, map2);
9317 prod = isl_map_flatten_range(prod);
9318 return prod;
9321 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9323 int i;
9324 uint32_t hash = isl_hash_init();
9325 unsigned total;
9327 if (!bmap)
9328 return 0;
9329 bmap = isl_basic_map_copy(bmap);
9330 bmap = isl_basic_map_normalize(bmap);
9331 if (!bmap)
9332 return 0;
9333 total = isl_basic_map_total_dim(bmap);
9334 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9335 for (i = 0; i < bmap->n_eq; ++i) {
9336 uint32_t c_hash;
9337 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9338 isl_hash_hash(hash, c_hash);
9340 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9341 for (i = 0; i < bmap->n_ineq; ++i) {
9342 uint32_t c_hash;
9343 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9344 isl_hash_hash(hash, c_hash);
9346 isl_hash_byte(hash, bmap->n_div & 0xFF);
9347 for (i = 0; i < bmap->n_div; ++i) {
9348 uint32_t c_hash;
9349 if (isl_int_is_zero(bmap->div[i][0]))
9350 continue;
9351 isl_hash_byte(hash, i & 0xFF);
9352 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9353 isl_hash_hash(hash, c_hash);
9355 isl_basic_map_free(bmap);
9356 return hash;
9359 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9361 return isl_basic_map_get_hash((isl_basic_map *)bset);
9364 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9366 int i;
9367 uint32_t hash;
9369 if (!map)
9370 return 0;
9371 map = isl_map_copy(map);
9372 map = isl_map_normalize(map);
9373 if (!map)
9374 return 0;
9376 hash = isl_hash_init();
9377 for (i = 0; i < map->n; ++i) {
9378 uint32_t bmap_hash;
9379 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9380 isl_hash_hash(hash, bmap_hash);
9383 isl_map_free(map);
9385 return hash;
9388 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9390 return isl_map_get_hash((isl_map *)set);
9393 /* Check if the value for dimension dim is completely determined
9394 * by the values of the other parameters and variables.
9395 * That is, check if dimension dim is involved in an equality.
9397 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9399 int i;
9400 unsigned nparam;
9402 if (!bset)
9403 return -1;
9404 nparam = isl_basic_set_n_param(bset);
9405 for (i = 0; i < bset->n_eq; ++i)
9406 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9407 return 1;
9408 return 0;
9411 /* Check if the value for dimension dim is completely determined
9412 * by the values of the other parameters and variables.
9413 * That is, check if dimension dim is involved in an equality
9414 * for each of the subsets.
9416 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9418 int i;
9420 if (!set)
9421 return -1;
9422 for (i = 0; i < set->n; ++i) {
9423 int unique;
9424 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9425 if (unique != 1)
9426 return unique;
9428 return 1;
9431 int isl_set_n_basic_set(__isl_keep isl_set *set)
9433 return set ? set->n : 0;
9436 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9437 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9439 int i;
9441 if (!map)
9442 return -1;
9444 for (i = 0; i < map->n; ++i)
9445 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9446 return -1;
9448 return 0;
9451 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9452 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9454 int i;
9456 if (!set)
9457 return -1;
9459 for (i = 0; i < set->n; ++i)
9460 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9461 return -1;
9463 return 0;
9466 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9468 isl_space *dim;
9470 if (!bset)
9471 return NULL;
9473 bset = isl_basic_set_cow(bset);
9474 if (!bset)
9475 return NULL;
9477 dim = isl_basic_set_get_space(bset);
9478 dim = isl_space_lift(dim, bset->n_div);
9479 if (!dim)
9480 goto error;
9481 isl_space_free(bset->dim);
9482 bset->dim = dim;
9483 bset->extra -= bset->n_div;
9484 bset->n_div = 0;
9486 bset = isl_basic_set_finalize(bset);
9488 return bset;
9489 error:
9490 isl_basic_set_free(bset);
9491 return NULL;
9494 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9496 int i;
9497 isl_space *dim;
9498 unsigned n_div;
9500 set = isl_set_align_divs(set);
9502 if (!set)
9503 return NULL;
9505 set = isl_set_cow(set);
9506 if (!set)
9507 return NULL;
9509 n_div = set->p[0]->n_div;
9510 dim = isl_set_get_space(set);
9511 dim = isl_space_lift(dim, n_div);
9512 if (!dim)
9513 goto error;
9514 isl_space_free(set->dim);
9515 set->dim = dim;
9517 for (i = 0; i < set->n; ++i) {
9518 set->p[i] = isl_basic_set_lift(set->p[i]);
9519 if (!set->p[i])
9520 goto error;
9523 return set;
9524 error:
9525 isl_set_free(set);
9526 return NULL;
9529 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9531 isl_space *dim;
9532 struct isl_basic_map *bmap;
9533 unsigned n_set;
9534 unsigned n_div;
9535 unsigned n_param;
9536 unsigned total;
9537 int i, k, l;
9539 set = isl_set_align_divs(set);
9541 if (!set)
9542 return NULL;
9544 dim = isl_set_get_space(set);
9545 if (set->n == 0 || set->p[0]->n_div == 0) {
9546 isl_set_free(set);
9547 return isl_map_identity(isl_space_map_from_set(dim));
9550 n_div = set->p[0]->n_div;
9551 dim = isl_space_map_from_set(dim);
9552 n_param = isl_space_dim(dim, isl_dim_param);
9553 n_set = isl_space_dim(dim, isl_dim_in);
9554 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9555 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9556 for (i = 0; i < n_set; ++i)
9557 bmap = var_equal(bmap, i);
9559 total = n_param + n_set + n_set + n_div;
9560 for (i = 0; i < n_div; ++i) {
9561 k = isl_basic_map_alloc_inequality(bmap);
9562 if (k < 0)
9563 goto error;
9564 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9565 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9566 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9567 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9568 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9569 set->p[0]->div[i][0]);
9571 l = isl_basic_map_alloc_inequality(bmap);
9572 if (l < 0)
9573 goto error;
9574 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9575 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9576 set->p[0]->div[i][0]);
9577 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9580 isl_set_free(set);
9581 bmap = isl_basic_map_simplify(bmap);
9582 bmap = isl_basic_map_finalize(bmap);
9583 return isl_map_from_basic_map(bmap);
9584 error:
9585 isl_set_free(set);
9586 isl_basic_map_free(bmap);
9587 return NULL;
9590 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9592 unsigned dim;
9593 int size = 0;
9595 if (!bset)
9596 return -1;
9598 dim = isl_basic_set_total_dim(bset);
9599 size += bset->n_eq * (1 + dim);
9600 size += bset->n_ineq * (1 + dim);
9601 size += bset->n_div * (2 + dim);
9603 return size;
9606 int isl_set_size(__isl_keep isl_set *set)
9608 int i;
9609 int size = 0;
9611 if (!set)
9612 return -1;
9614 for (i = 0; i < set->n; ++i)
9615 size += isl_basic_set_size(set->p[i]);
9617 return size;
9620 /* Check if there is any lower bound (if lower == 0) and/or upper
9621 * bound (if upper == 0) on the specified dim.
9623 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9624 enum isl_dim_type type, unsigned pos, int lower, int upper)
9626 int i;
9628 if (!bmap)
9629 return -1;
9631 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9633 pos += isl_basic_map_offset(bmap, type);
9635 for (i = 0; i < bmap->n_div; ++i) {
9636 if (isl_int_is_zero(bmap->div[i][0]))
9637 continue;
9638 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9639 return 1;
9642 for (i = 0; i < bmap->n_eq; ++i)
9643 if (!isl_int_is_zero(bmap->eq[i][pos]))
9644 return 1;
9646 for (i = 0; i < bmap->n_ineq; ++i) {
9647 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9648 if (sgn > 0)
9649 lower = 1;
9650 if (sgn < 0)
9651 upper = 1;
9654 return lower && upper;
9657 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9658 enum isl_dim_type type, unsigned pos)
9660 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9663 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9664 enum isl_dim_type type, unsigned pos)
9666 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9669 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9670 enum isl_dim_type type, unsigned pos)
9672 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9675 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9676 enum isl_dim_type type, unsigned pos)
9678 int i;
9680 if (!map)
9681 return -1;
9683 for (i = 0; i < map->n; ++i) {
9684 int bounded;
9685 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9686 if (bounded < 0 || !bounded)
9687 return bounded;
9690 return 1;
9693 /* Return 1 if the specified dim is involved in both an upper bound
9694 * and a lower bound.
9696 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9697 enum isl_dim_type type, unsigned pos)
9699 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9702 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9704 static int has_any_bound(__isl_keep isl_map *map,
9705 enum isl_dim_type type, unsigned pos,
9706 int (*fn)(__isl_keep isl_basic_map *bmap,
9707 enum isl_dim_type type, unsigned pos))
9709 int i;
9711 if (!map)
9712 return -1;
9714 for (i = 0; i < map->n; ++i) {
9715 int bounded;
9716 bounded = fn(map->p[i], type, pos);
9717 if (bounded < 0 || bounded)
9718 return bounded;
9721 return 0;
9724 /* Return 1 if the specified dim is involved in any lower bound.
9726 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9727 enum isl_dim_type type, unsigned pos)
9729 return has_any_bound(set, type, pos,
9730 &isl_basic_map_dim_has_lower_bound);
9733 /* Return 1 if the specified dim is involved in any upper bound.
9735 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9736 enum isl_dim_type type, unsigned pos)
9738 return has_any_bound(set, type, pos,
9739 &isl_basic_map_dim_has_upper_bound);
9742 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9744 static int has_bound(__isl_keep isl_map *map,
9745 enum isl_dim_type type, unsigned pos,
9746 int (*fn)(__isl_keep isl_basic_map *bmap,
9747 enum isl_dim_type type, unsigned pos))
9749 int i;
9751 if (!map)
9752 return -1;
9754 for (i = 0; i < map->n; ++i) {
9755 int bounded;
9756 bounded = fn(map->p[i], type, pos);
9757 if (bounded < 0 || !bounded)
9758 return bounded;
9761 return 1;
9764 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9766 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9767 enum isl_dim_type type, unsigned pos)
9769 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9772 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9774 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9775 enum isl_dim_type type, unsigned pos)
9777 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9780 /* For each of the "n" variables starting at "first", determine
9781 * the sign of the variable and put the results in the first "n"
9782 * elements of the array "signs".
9783 * Sign
9784 * 1 means that the variable is non-negative
9785 * -1 means that the variable is non-positive
9786 * 0 means the variable attains both positive and negative values.
9788 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9789 unsigned first, unsigned n, int *signs)
9791 isl_vec *bound = NULL;
9792 struct isl_tab *tab = NULL;
9793 struct isl_tab_undo *snap;
9794 int i;
9796 if (!bset || !signs)
9797 return -1;
9799 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9800 tab = isl_tab_from_basic_set(bset, 0);
9801 if (!bound || !tab)
9802 goto error;
9804 isl_seq_clr(bound->el, bound->size);
9805 isl_int_set_si(bound->el[0], -1);
9807 snap = isl_tab_snap(tab);
9808 for (i = 0; i < n; ++i) {
9809 int empty;
9811 isl_int_set_si(bound->el[1 + first + i], -1);
9812 if (isl_tab_add_ineq(tab, bound->el) < 0)
9813 goto error;
9814 empty = tab->empty;
9815 isl_int_set_si(bound->el[1 + first + i], 0);
9816 if (isl_tab_rollback(tab, snap) < 0)
9817 goto error;
9819 if (empty) {
9820 signs[i] = 1;
9821 continue;
9824 isl_int_set_si(bound->el[1 + first + i], 1);
9825 if (isl_tab_add_ineq(tab, bound->el) < 0)
9826 goto error;
9827 empty = tab->empty;
9828 isl_int_set_si(bound->el[1 + first + i], 0);
9829 if (isl_tab_rollback(tab, snap) < 0)
9830 goto error;
9832 signs[i] = empty ? -1 : 0;
9835 isl_tab_free(tab);
9836 isl_vec_free(bound);
9837 return 0;
9838 error:
9839 isl_tab_free(tab);
9840 isl_vec_free(bound);
9841 return -1;
9844 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9845 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9847 if (!bset || !signs)
9848 return -1;
9849 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9850 return -1);
9852 first += pos(bset->dim, type) - 1;
9853 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9856 /* Check if the given basic map is obviously single-valued.
9857 * In particular, for each output dimension, check that there is
9858 * an equality that defines the output dimension in terms of
9859 * earlier dimensions.
9861 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9863 int i, j;
9864 unsigned total;
9865 unsigned n_out;
9866 unsigned o_out;
9868 if (!bmap)
9869 return -1;
9871 total = 1 + isl_basic_map_total_dim(bmap);
9872 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9873 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9875 for (i = 0; i < n_out; ++i) {
9876 for (j = 0; j < bmap->n_eq; ++j) {
9877 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9878 continue;
9879 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9880 total - (o_out + i + 1)) == -1)
9881 break;
9883 if (j >= bmap->n_eq)
9884 return 0;
9887 return 1;
9890 /* Check if the given basic map is single-valued.
9891 * We simply compute
9893 * M \circ M^-1
9895 * and check if the result is a subset of the identity mapping.
9897 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9899 isl_space *space;
9900 isl_basic_map *test;
9901 isl_basic_map *id;
9902 int sv;
9904 sv = isl_basic_map_plain_is_single_valued(bmap);
9905 if (sv < 0 || sv)
9906 return sv;
9908 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9909 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9911 space = isl_basic_map_get_space(bmap);
9912 space = isl_space_map_from_set(isl_space_range(space));
9913 id = isl_basic_map_identity(space);
9915 sv = isl_basic_map_is_subset(test, id);
9917 isl_basic_map_free(test);
9918 isl_basic_map_free(id);
9920 return sv;
9923 /* Check if the given map is obviously single-valued.
9925 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9927 if (!map)
9928 return -1;
9929 if (map->n == 0)
9930 return 1;
9931 if (map->n >= 2)
9932 return 0;
9934 return isl_basic_map_plain_is_single_valued(map->p[0]);
9937 /* Check if the given map is single-valued.
9938 * We simply compute
9940 * M \circ M^-1
9942 * and check if the result is a subset of the identity mapping.
9944 int isl_map_is_single_valued(__isl_keep isl_map *map)
9946 isl_space *dim;
9947 isl_map *test;
9948 isl_map *id;
9949 int sv;
9951 sv = isl_map_plain_is_single_valued(map);
9952 if (sv < 0 || sv)
9953 return sv;
9955 test = isl_map_reverse(isl_map_copy(map));
9956 test = isl_map_apply_range(test, isl_map_copy(map));
9958 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9959 id = isl_map_identity(dim);
9961 sv = isl_map_is_subset(test, id);
9963 isl_map_free(test);
9964 isl_map_free(id);
9966 return sv;
9969 int isl_map_is_injective(__isl_keep isl_map *map)
9971 int in;
9973 map = isl_map_copy(map);
9974 map = isl_map_reverse(map);
9975 in = isl_map_is_single_valued(map);
9976 isl_map_free(map);
9978 return in;
9981 /* Check if the given map is obviously injective.
9983 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9985 int in;
9987 map = isl_map_copy(map);
9988 map = isl_map_reverse(map);
9989 in = isl_map_plain_is_single_valued(map);
9990 isl_map_free(map);
9992 return in;
9995 int isl_map_is_bijective(__isl_keep isl_map *map)
9997 int sv;
9999 sv = isl_map_is_single_valued(map);
10000 if (sv < 0 || !sv)
10001 return sv;
10003 return isl_map_is_injective(map);
10006 int isl_set_is_singleton(__isl_keep isl_set *set)
10008 return isl_map_is_single_valued((isl_map *)set);
10011 int isl_map_is_translation(__isl_keep isl_map *map)
10013 int ok;
10014 isl_set *delta;
10016 delta = isl_map_deltas(isl_map_copy(map));
10017 ok = isl_set_is_singleton(delta);
10018 isl_set_free(delta);
10020 return ok;
10023 static int unique(isl_int *p, unsigned pos, unsigned len)
10025 if (isl_seq_first_non_zero(p, pos) != -1)
10026 return 0;
10027 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10028 return 0;
10029 return 1;
10032 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10034 int i, j;
10035 unsigned nvar;
10036 unsigned ovar;
10038 if (!bset)
10039 return -1;
10041 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10042 return 0;
10044 nvar = isl_basic_set_dim(bset, isl_dim_set);
10045 ovar = isl_space_offset(bset->dim, isl_dim_set);
10046 for (j = 0; j < nvar; ++j) {
10047 int lower = 0, upper = 0;
10048 for (i = 0; i < bset->n_eq; ++i) {
10049 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10050 continue;
10051 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10052 return 0;
10053 break;
10055 if (i < bset->n_eq)
10056 continue;
10057 for (i = 0; i < bset->n_ineq; ++i) {
10058 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10059 continue;
10060 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10061 return 0;
10062 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10063 lower = 1;
10064 else
10065 upper = 1;
10067 if (!lower || !upper)
10068 return 0;
10071 return 1;
10074 int isl_set_is_box(__isl_keep isl_set *set)
10076 if (!set)
10077 return -1;
10078 if (set->n != 1)
10079 return 0;
10081 return isl_basic_set_is_box(set->p[0]);
10084 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10086 if (!bset)
10087 return -1;
10089 return isl_space_is_wrapping(bset->dim);
10092 int isl_set_is_wrapping(__isl_keep isl_set *set)
10094 if (!set)
10095 return -1;
10097 return isl_space_is_wrapping(set->dim);
10100 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10102 bmap = isl_basic_map_cow(bmap);
10103 if (!bmap)
10104 return NULL;
10106 bmap->dim = isl_space_wrap(bmap->dim);
10107 if (!bmap->dim)
10108 goto error;
10110 bmap = isl_basic_map_finalize(bmap);
10112 return (isl_basic_set *)bmap;
10113 error:
10114 isl_basic_map_free(bmap);
10115 return NULL;
10118 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10120 int i;
10122 map = isl_map_cow(map);
10123 if (!map)
10124 return NULL;
10126 for (i = 0; i < map->n; ++i) {
10127 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10128 if (!map->p[i])
10129 goto error;
10131 map->dim = isl_space_wrap(map->dim);
10132 if (!map->dim)
10133 goto error;
10135 return (isl_set *)map;
10136 error:
10137 isl_map_free(map);
10138 return NULL;
10141 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10143 bset = isl_basic_set_cow(bset);
10144 if (!bset)
10145 return NULL;
10147 bset->dim = isl_space_unwrap(bset->dim);
10148 if (!bset->dim)
10149 goto error;
10151 bset = isl_basic_set_finalize(bset);
10153 return (isl_basic_map *)bset;
10154 error:
10155 isl_basic_set_free(bset);
10156 return NULL;
10159 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10161 int i;
10163 if (!set)
10164 return NULL;
10166 if (!isl_set_is_wrapping(set))
10167 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10168 goto error);
10170 set = isl_set_cow(set);
10171 if (!set)
10172 return NULL;
10174 for (i = 0; i < set->n; ++i) {
10175 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10176 if (!set->p[i])
10177 goto error;
10180 set->dim = isl_space_unwrap(set->dim);
10181 if (!set->dim)
10182 goto error;
10184 return (isl_map *)set;
10185 error:
10186 isl_set_free(set);
10187 return NULL;
10190 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10191 enum isl_dim_type type)
10193 if (!bmap)
10194 return NULL;
10196 if (!isl_space_is_named_or_nested(bmap->dim, type))
10197 return bmap;
10199 bmap = isl_basic_map_cow(bmap);
10200 if (!bmap)
10201 return NULL;
10203 bmap->dim = isl_space_reset(bmap->dim, type);
10204 if (!bmap->dim)
10205 goto error;
10207 bmap = isl_basic_map_finalize(bmap);
10209 return bmap;
10210 error:
10211 isl_basic_map_free(bmap);
10212 return NULL;
10215 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10216 enum isl_dim_type type)
10218 int i;
10220 if (!map)
10221 return NULL;
10223 if (!isl_space_is_named_or_nested(map->dim, type))
10224 return map;
10226 map = isl_map_cow(map);
10227 if (!map)
10228 return NULL;
10230 for (i = 0; i < map->n; ++i) {
10231 map->p[i] = isl_basic_map_reset(map->p[i], type);
10232 if (!map->p[i])
10233 goto error;
10235 map->dim = isl_space_reset(map->dim, type);
10236 if (!map->dim)
10237 goto error;
10239 return map;
10240 error:
10241 isl_map_free(map);
10242 return NULL;
10245 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10247 if (!bmap)
10248 return NULL;
10250 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10251 return bmap;
10253 bmap = isl_basic_map_cow(bmap);
10254 if (!bmap)
10255 return NULL;
10257 bmap->dim = isl_space_flatten(bmap->dim);
10258 if (!bmap->dim)
10259 goto error;
10261 bmap = isl_basic_map_finalize(bmap);
10263 return bmap;
10264 error:
10265 isl_basic_map_free(bmap);
10266 return NULL;
10269 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10271 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10274 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10275 __isl_take isl_basic_map *bmap)
10277 if (!bmap)
10278 return NULL;
10280 if (!bmap->dim->nested[0])
10281 return bmap;
10283 bmap = isl_basic_map_cow(bmap);
10284 if (!bmap)
10285 return NULL;
10287 bmap->dim = isl_space_flatten_domain(bmap->dim);
10288 if (!bmap->dim)
10289 goto error;
10291 bmap = isl_basic_map_finalize(bmap);
10293 return bmap;
10294 error:
10295 isl_basic_map_free(bmap);
10296 return NULL;
10299 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10300 __isl_take isl_basic_map *bmap)
10302 if (!bmap)
10303 return NULL;
10305 if (!bmap->dim->nested[1])
10306 return bmap;
10308 bmap = isl_basic_map_cow(bmap);
10309 if (!bmap)
10310 return NULL;
10312 bmap->dim = isl_space_flatten_range(bmap->dim);
10313 if (!bmap->dim)
10314 goto error;
10316 bmap = isl_basic_map_finalize(bmap);
10318 return bmap;
10319 error:
10320 isl_basic_map_free(bmap);
10321 return NULL;
10324 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10326 int i;
10328 if (!map)
10329 return NULL;
10331 if (!map->dim->nested[0] && !map->dim->nested[1])
10332 return map;
10334 map = isl_map_cow(map);
10335 if (!map)
10336 return NULL;
10338 for (i = 0; i < map->n; ++i) {
10339 map->p[i] = isl_basic_map_flatten(map->p[i]);
10340 if (!map->p[i])
10341 goto error;
10343 map->dim = isl_space_flatten(map->dim);
10344 if (!map->dim)
10345 goto error;
10347 return map;
10348 error:
10349 isl_map_free(map);
10350 return NULL;
10353 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10355 return (isl_set *)isl_map_flatten((isl_map *)set);
10358 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10360 isl_space *dim, *flat_dim;
10361 isl_map *map;
10363 dim = isl_set_get_space(set);
10364 flat_dim = isl_space_flatten(isl_space_copy(dim));
10365 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10366 map = isl_map_intersect_domain(map, set);
10368 return map;
10371 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10373 int i;
10375 if (!map)
10376 return NULL;
10378 if (!map->dim->nested[0])
10379 return map;
10381 map = isl_map_cow(map);
10382 if (!map)
10383 return NULL;
10385 for (i = 0; i < map->n; ++i) {
10386 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10387 if (!map->p[i])
10388 goto error;
10390 map->dim = isl_space_flatten_domain(map->dim);
10391 if (!map->dim)
10392 goto error;
10394 return map;
10395 error:
10396 isl_map_free(map);
10397 return NULL;
10400 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10402 int i;
10404 if (!map)
10405 return NULL;
10407 if (!map->dim->nested[1])
10408 return map;
10410 map = isl_map_cow(map);
10411 if (!map)
10412 return NULL;
10414 for (i = 0; i < map->n; ++i) {
10415 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10416 if (!map->p[i])
10417 goto error;
10419 map->dim = isl_space_flatten_range(map->dim);
10420 if (!map->dim)
10421 goto error;
10423 return map;
10424 error:
10425 isl_map_free(map);
10426 return NULL;
10429 /* Reorder the dimensions of "bmap" according to the given dim_map
10430 * and set the dimension specification to "dim".
10432 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10433 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10435 isl_basic_map *res;
10436 unsigned flags;
10438 bmap = isl_basic_map_cow(bmap);
10439 if (!bmap || !dim || !dim_map)
10440 goto error;
10442 flags = bmap->flags;
10443 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10444 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10445 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10446 res = isl_basic_map_alloc_space(dim,
10447 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10448 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10449 if (res)
10450 res->flags = flags;
10451 res = isl_basic_map_finalize(res);
10452 return res;
10453 error:
10454 free(dim_map);
10455 isl_basic_map_free(bmap);
10456 isl_space_free(dim);
10457 return NULL;
10460 /* Reorder the dimensions of "map" according to given reordering.
10462 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10463 __isl_take isl_reordering *r)
10465 int i;
10466 struct isl_dim_map *dim_map;
10468 map = isl_map_cow(map);
10469 dim_map = isl_dim_map_from_reordering(r);
10470 if (!map || !r || !dim_map)
10471 goto error;
10473 for (i = 0; i < map->n; ++i) {
10474 struct isl_dim_map *dim_map_i;
10476 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10478 map->p[i] = isl_basic_map_realign(map->p[i],
10479 isl_space_copy(r->dim), dim_map_i);
10481 if (!map->p[i])
10482 goto error;
10485 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10487 isl_reordering_free(r);
10488 free(dim_map);
10489 return map;
10490 error:
10491 free(dim_map);
10492 isl_map_free(map);
10493 isl_reordering_free(r);
10494 return NULL;
10497 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10498 __isl_take isl_reordering *r)
10500 return (isl_set *)isl_map_realign((isl_map *)set, r);
10503 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10504 __isl_take isl_space *model)
10506 isl_ctx *ctx;
10508 if (!map || !model)
10509 goto error;
10511 ctx = isl_space_get_ctx(model);
10512 if (!isl_space_has_named_params(model))
10513 isl_die(ctx, isl_error_invalid,
10514 "model has unnamed parameters", goto error);
10515 if (!isl_space_has_named_params(map->dim))
10516 isl_die(ctx, isl_error_invalid,
10517 "relation has unnamed parameters", goto error);
10518 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10519 isl_reordering *exp;
10521 model = isl_space_drop_dims(model, isl_dim_in,
10522 0, isl_space_dim(model, isl_dim_in));
10523 model = isl_space_drop_dims(model, isl_dim_out,
10524 0, isl_space_dim(model, isl_dim_out));
10525 exp = isl_parameter_alignment_reordering(map->dim, model);
10526 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10527 map = isl_map_realign(map, exp);
10530 isl_space_free(model);
10531 return map;
10532 error:
10533 isl_space_free(model);
10534 isl_map_free(map);
10535 return NULL;
10538 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10539 __isl_take isl_space *model)
10541 return isl_map_align_params(set, model);
10544 /* Align the parameters of "bmap" to those of "model", introducing
10545 * additional parameters if needed.
10547 __isl_give isl_basic_map *isl_basic_map_align_params(
10548 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10550 isl_ctx *ctx;
10552 if (!bmap || !model)
10553 goto error;
10555 ctx = isl_space_get_ctx(model);
10556 if (!isl_space_has_named_params(model))
10557 isl_die(ctx, isl_error_invalid,
10558 "model has unnamed parameters", goto error);
10559 if (!isl_space_has_named_params(bmap->dim))
10560 isl_die(ctx, isl_error_invalid,
10561 "relation has unnamed parameters", goto error);
10562 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10563 isl_reordering *exp;
10564 struct isl_dim_map *dim_map;
10566 model = isl_space_drop_dims(model, isl_dim_in,
10567 0, isl_space_dim(model, isl_dim_in));
10568 model = isl_space_drop_dims(model, isl_dim_out,
10569 0, isl_space_dim(model, isl_dim_out));
10570 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10571 exp = isl_reordering_extend_space(exp,
10572 isl_basic_map_get_space(bmap));
10573 dim_map = isl_dim_map_from_reordering(exp);
10574 bmap = isl_basic_map_realign(bmap,
10575 exp ? isl_space_copy(exp->dim) : NULL,
10576 isl_dim_map_extend(dim_map, bmap));
10577 isl_reordering_free(exp);
10578 free(dim_map);
10581 isl_space_free(model);
10582 return bmap;
10583 error:
10584 isl_space_free(model);
10585 isl_basic_map_free(bmap);
10586 return NULL;
10589 /* Align the parameters of "bset" to those of "model", introducing
10590 * additional parameters if needed.
10592 __isl_give isl_basic_set *isl_basic_set_align_params(
10593 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10595 return isl_basic_map_align_params(bset, model);
10598 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10599 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10600 enum isl_dim_type c2, enum isl_dim_type c3,
10601 enum isl_dim_type c4, enum isl_dim_type c5)
10603 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10604 struct isl_mat *mat;
10605 int i, j, k;
10606 int pos;
10608 if (!bmap)
10609 return NULL;
10610 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10611 isl_basic_map_total_dim(bmap) + 1);
10612 if (!mat)
10613 return NULL;
10614 for (i = 0; i < bmap->n_eq; ++i)
10615 for (j = 0, pos = 0; j < 5; ++j) {
10616 int off = isl_basic_map_offset(bmap, c[j]);
10617 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10618 isl_int_set(mat->row[i][pos],
10619 bmap->eq[i][off + k]);
10620 ++pos;
10624 return mat;
10627 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10628 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10629 enum isl_dim_type c2, enum isl_dim_type c3,
10630 enum isl_dim_type c4, enum isl_dim_type c5)
10632 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10633 struct isl_mat *mat;
10634 int i, j, k;
10635 int pos;
10637 if (!bmap)
10638 return NULL;
10639 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10640 isl_basic_map_total_dim(bmap) + 1);
10641 if (!mat)
10642 return NULL;
10643 for (i = 0; i < bmap->n_ineq; ++i)
10644 for (j = 0, pos = 0; j < 5; ++j) {
10645 int off = isl_basic_map_offset(bmap, c[j]);
10646 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10647 isl_int_set(mat->row[i][pos],
10648 bmap->ineq[i][off + k]);
10649 ++pos;
10653 return mat;
10656 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10657 __isl_take isl_space *dim,
10658 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10659 enum isl_dim_type c2, enum isl_dim_type c3,
10660 enum isl_dim_type c4, enum isl_dim_type c5)
10662 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10663 isl_basic_map *bmap;
10664 unsigned total;
10665 unsigned extra;
10666 int i, j, k, l;
10667 int pos;
10669 if (!dim || !eq || !ineq)
10670 goto error;
10672 if (eq->n_col != ineq->n_col)
10673 isl_die(dim->ctx, isl_error_invalid,
10674 "equalities and inequalities matrices should have "
10675 "same number of columns", goto error);
10677 total = 1 + isl_space_dim(dim, isl_dim_all);
10679 if (eq->n_col < total)
10680 isl_die(dim->ctx, isl_error_invalid,
10681 "number of columns too small", goto error);
10683 extra = eq->n_col - total;
10685 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10686 eq->n_row, ineq->n_row);
10687 if (!bmap)
10688 goto error;
10689 for (i = 0; i < extra; ++i) {
10690 k = isl_basic_map_alloc_div(bmap);
10691 if (k < 0)
10692 goto error;
10693 isl_int_set_si(bmap->div[k][0], 0);
10695 for (i = 0; i < eq->n_row; ++i) {
10696 l = isl_basic_map_alloc_equality(bmap);
10697 if (l < 0)
10698 goto error;
10699 for (j = 0, pos = 0; j < 5; ++j) {
10700 int off = isl_basic_map_offset(bmap, c[j]);
10701 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10702 isl_int_set(bmap->eq[l][off + k],
10703 eq->row[i][pos]);
10704 ++pos;
10708 for (i = 0; i < ineq->n_row; ++i) {
10709 l = isl_basic_map_alloc_inequality(bmap);
10710 if (l < 0)
10711 goto error;
10712 for (j = 0, pos = 0; j < 5; ++j) {
10713 int off = isl_basic_map_offset(bmap, c[j]);
10714 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10715 isl_int_set(bmap->ineq[l][off + k],
10716 ineq->row[i][pos]);
10717 ++pos;
10722 isl_space_free(dim);
10723 isl_mat_free(eq);
10724 isl_mat_free(ineq);
10726 bmap = isl_basic_map_simplify(bmap);
10727 return isl_basic_map_finalize(bmap);
10728 error:
10729 isl_space_free(dim);
10730 isl_mat_free(eq);
10731 isl_mat_free(ineq);
10732 return NULL;
10735 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10736 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10737 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10739 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10740 c1, c2, c3, c4, isl_dim_in);
10743 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10744 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10745 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10747 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10748 c1, c2, c3, c4, isl_dim_in);
10751 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10752 __isl_take isl_space *dim,
10753 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10754 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10756 return (isl_basic_set*)
10757 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10758 c1, c2, c3, c4, isl_dim_in);
10761 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10763 if (!bmap)
10764 return -1;
10766 return isl_space_can_zip(bmap->dim);
10769 int isl_map_can_zip(__isl_keep isl_map *map)
10771 if (!map)
10772 return -1;
10774 return isl_space_can_zip(map->dim);
10777 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10778 * (A -> C) -> (B -> D).
10780 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10782 unsigned pos;
10783 unsigned n1;
10784 unsigned n2;
10786 if (!bmap)
10787 return NULL;
10789 if (!isl_basic_map_can_zip(bmap))
10790 isl_die(bmap->ctx, isl_error_invalid,
10791 "basic map cannot be zipped", goto error);
10792 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10793 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10794 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10795 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10796 bmap = isl_basic_map_cow(bmap);
10797 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10798 if (!bmap)
10799 return NULL;
10800 bmap->dim = isl_space_zip(bmap->dim);
10801 if (!bmap->dim)
10802 goto error;
10803 return bmap;
10804 error:
10805 isl_basic_map_free(bmap);
10806 return NULL;
10809 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10810 * (A -> C) -> (B -> D).
10812 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10814 int i;
10816 if (!map)
10817 return NULL;
10819 if (!isl_map_can_zip(map))
10820 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10821 goto error);
10823 map = isl_map_cow(map);
10824 if (!map)
10825 return NULL;
10827 for (i = 0; i < map->n; ++i) {
10828 map->p[i] = isl_basic_map_zip(map->p[i]);
10829 if (!map->p[i])
10830 goto error;
10833 map->dim = isl_space_zip(map->dim);
10834 if (!map->dim)
10835 goto error;
10837 return map;
10838 error:
10839 isl_map_free(map);
10840 return NULL;
10843 /* Can we apply isl_basic_map_curry to "bmap"?
10844 * That is, does it have a nested relation in its domain?
10846 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10848 if (!bmap)
10849 return -1;
10851 return isl_space_can_curry(bmap->dim);
10854 /* Can we apply isl_map_curry to "map"?
10855 * That is, does it have a nested relation in its domain?
10857 int isl_map_can_curry(__isl_keep isl_map *map)
10859 if (!map)
10860 return -1;
10862 return isl_space_can_curry(map->dim);
10865 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10866 * A -> (B -> C).
10868 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10871 if (!bmap)
10872 return NULL;
10874 if (!isl_basic_map_can_curry(bmap))
10875 isl_die(bmap->ctx, isl_error_invalid,
10876 "basic map cannot be curried", goto error);
10877 bmap = isl_basic_map_cow(bmap);
10878 if (!bmap)
10879 return NULL;
10880 bmap->dim = isl_space_curry(bmap->dim);
10881 if (!bmap->dim)
10882 goto error;
10883 return bmap;
10884 error:
10885 isl_basic_map_free(bmap);
10886 return NULL;
10889 /* Given a map (A -> B) -> C, return the corresponding map
10890 * A -> (B -> C).
10892 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10894 int i;
10896 if (!map)
10897 return NULL;
10899 if (!isl_map_can_curry(map))
10900 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10901 goto error);
10903 map = isl_map_cow(map);
10904 if (!map)
10905 return NULL;
10907 for (i = 0; i < map->n; ++i) {
10908 map->p[i] = isl_basic_map_curry(map->p[i]);
10909 if (!map->p[i])
10910 goto error;
10913 map->dim = isl_space_curry(map->dim);
10914 if (!map->dim)
10915 goto error;
10917 return map;
10918 error:
10919 isl_map_free(map);
10920 return NULL;
10923 /* Can we apply isl_basic_map_uncurry to "bmap"?
10924 * That is, does it have a nested relation in its domain?
10926 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10928 if (!bmap)
10929 return -1;
10931 return isl_space_can_uncurry(bmap->dim);
10934 /* Can we apply isl_map_uncurry to "map"?
10935 * That is, does it have a nested relation in its domain?
10937 int isl_map_can_uncurry(__isl_keep isl_map *map)
10939 if (!map)
10940 return -1;
10942 return isl_space_can_uncurry(map->dim);
10945 /* Given a basic map A -> (B -> C), return the corresponding basic map
10946 * (A -> B) -> C.
10948 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10951 if (!bmap)
10952 return NULL;
10954 if (!isl_basic_map_can_uncurry(bmap))
10955 isl_die(bmap->ctx, isl_error_invalid,
10956 "basic map cannot be uncurried",
10957 return isl_basic_map_free(bmap));
10958 bmap = isl_basic_map_cow(bmap);
10959 if (!bmap)
10960 return NULL;
10961 bmap->dim = isl_space_uncurry(bmap->dim);
10962 if (!bmap->dim)
10963 return isl_basic_map_free(bmap);
10964 return bmap;
10967 /* Given a map A -> (B -> C), return the corresponding map
10968 * (A -> B) -> C.
10970 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10972 int i;
10974 if (!map)
10975 return NULL;
10977 if (!isl_map_can_uncurry(map))
10978 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10979 return isl_map_free(map));
10981 map = isl_map_cow(map);
10982 if (!map)
10983 return NULL;
10985 for (i = 0; i < map->n; ++i) {
10986 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10987 if (!map->p[i])
10988 return isl_map_free(map);
10991 map->dim = isl_space_uncurry(map->dim);
10992 if (!map->dim)
10993 return isl_map_free(map);
10995 return map;
10998 /* Construct a basic map mapping the domain of the affine expression
10999 * to a one-dimensional range prescribed by the affine expression.
11001 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11003 int k;
11004 int pos;
11005 isl_local_space *ls;
11006 isl_basic_map *bmap;
11008 if (!aff)
11009 return NULL;
11011 ls = isl_aff_get_local_space(aff);
11012 bmap = isl_basic_map_from_local_space(ls);
11013 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11014 k = isl_basic_map_alloc_equality(bmap);
11015 if (k < 0)
11016 goto error;
11018 pos = isl_basic_map_offset(bmap, isl_dim_out);
11019 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11020 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11021 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11022 aff->v->size - (pos + 1));
11024 isl_aff_free(aff);
11025 bmap = isl_basic_map_finalize(bmap);
11026 return bmap;
11027 error:
11028 isl_aff_free(aff);
11029 isl_basic_map_free(bmap);
11030 return NULL;
11033 /* Construct a map mapping the domain of the affine expression
11034 * to a one-dimensional range prescribed by the affine expression.
11036 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11038 isl_basic_map *bmap;
11040 bmap = isl_basic_map_from_aff(aff);
11041 return isl_map_from_basic_map(bmap);
11044 /* Construct a basic map mapping the domain the multi-affine expression
11045 * to its range, with each dimension in the range equated to the
11046 * corresponding affine expression.
11048 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11049 __isl_take isl_multi_aff *maff)
11051 int i;
11052 isl_space *space;
11053 isl_basic_map *bmap;
11055 if (!maff)
11056 return NULL;
11058 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11059 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11060 "invalid space", return isl_multi_aff_free(maff));
11062 space = isl_space_domain(isl_multi_aff_get_space(maff));
11063 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11065 for (i = 0; i < maff->n; ++i) {
11066 isl_aff *aff;
11067 isl_basic_map *bmap_i;
11069 aff = isl_aff_copy(maff->p[i]);
11070 bmap_i = isl_basic_map_from_aff(aff);
11072 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11075 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11077 isl_multi_aff_free(maff);
11078 return bmap;
11081 /* Construct a map mapping the domain the multi-affine expression
11082 * to its range, with each dimension in the range equated to the
11083 * corresponding affine expression.
11085 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11087 isl_basic_map *bmap;
11089 bmap = isl_basic_map_from_multi_aff(maff);
11090 return isl_map_from_basic_map(bmap);
11093 /* Construct a basic map mapping a domain in the given space to
11094 * to an n-dimensional range, with n the number of elements in the list,
11095 * where each coordinate in the range is prescribed by the
11096 * corresponding affine expression.
11097 * The domains of all affine expressions in the list are assumed to match
11098 * domain_dim.
11100 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11101 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11103 int i;
11104 isl_space *dim;
11105 isl_basic_map *bmap;
11107 if (!list)
11108 return NULL;
11110 dim = isl_space_from_domain(domain_dim);
11111 bmap = isl_basic_map_universe(dim);
11113 for (i = 0; i < list->n; ++i) {
11114 isl_aff *aff;
11115 isl_basic_map *bmap_i;
11117 aff = isl_aff_copy(list->p[i]);
11118 bmap_i = isl_basic_map_from_aff(aff);
11120 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11123 isl_aff_list_free(list);
11124 return bmap;
11127 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11128 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11130 return isl_map_equate(set, type1, pos1, type2, pos2);
11133 /* Construct a basic map where the given dimensions are equal to each other.
11135 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11136 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11138 isl_basic_map *bmap = NULL;
11139 int i;
11141 if (!space)
11142 return NULL;
11144 if (pos1 >= isl_space_dim(space, type1))
11145 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11146 "index out of bounds", goto error);
11147 if (pos2 >= isl_space_dim(space, type2))
11148 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11149 "index out of bounds", goto error);
11151 if (type1 == type2 && pos1 == pos2)
11152 return isl_basic_map_universe(space);
11154 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11155 i = isl_basic_map_alloc_equality(bmap);
11156 if (i < 0)
11157 goto error;
11158 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11159 pos1 += isl_basic_map_offset(bmap, type1);
11160 pos2 += isl_basic_map_offset(bmap, type2);
11161 isl_int_set_si(bmap->eq[i][pos1], -1);
11162 isl_int_set_si(bmap->eq[i][pos2], 1);
11163 bmap = isl_basic_map_finalize(bmap);
11164 isl_space_free(space);
11165 return bmap;
11166 error:
11167 isl_space_free(space);
11168 isl_basic_map_free(bmap);
11169 return NULL;
11172 /* Add a constraint imposing that the given two dimensions are equal.
11174 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11175 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11177 isl_basic_map *eq;
11179 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11181 bmap = isl_basic_map_intersect(bmap, eq);
11183 return bmap;
11186 /* Add a constraint imposing that the given two dimensions are equal.
11188 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11189 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11191 isl_basic_map *bmap;
11193 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11195 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11197 return map;
11200 /* Add a constraint imposing that the given two dimensions have opposite values.
11202 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11203 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11205 isl_basic_map *bmap = NULL;
11206 int i;
11208 if (!map)
11209 return NULL;
11211 if (pos1 >= isl_map_dim(map, type1))
11212 isl_die(map->ctx, isl_error_invalid,
11213 "index out of bounds", goto error);
11214 if (pos2 >= isl_map_dim(map, type2))
11215 isl_die(map->ctx, isl_error_invalid,
11216 "index out of bounds", goto error);
11218 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11219 i = isl_basic_map_alloc_equality(bmap);
11220 if (i < 0)
11221 goto error;
11222 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11223 pos1 += isl_basic_map_offset(bmap, type1);
11224 pos2 += isl_basic_map_offset(bmap, type2);
11225 isl_int_set_si(bmap->eq[i][pos1], 1);
11226 isl_int_set_si(bmap->eq[i][pos2], 1);
11227 bmap = isl_basic_map_finalize(bmap);
11229 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11231 return map;
11232 error:
11233 isl_basic_map_free(bmap);
11234 isl_map_free(map);
11235 return NULL;
11238 /* Add a constraint imposing that the value of the first dimension is
11239 * greater than or equal to that of the second.
11241 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11244 isl_constraint *c;
11245 isl_local_space *ls;
11247 if (!bmap)
11248 return NULL;
11250 if (pos1 >= isl_basic_map_dim(bmap, type1))
11251 isl_die(bmap->ctx, isl_error_invalid,
11252 "index out of bounds", return isl_basic_map_free(bmap));
11253 if (pos2 >= isl_basic_map_dim(bmap, type2))
11254 isl_die(bmap->ctx, isl_error_invalid,
11255 "index out of bounds", return isl_basic_map_free(bmap));
11257 if (type1 == type2 && pos1 == pos2)
11258 return bmap;
11260 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11261 c = isl_inequality_alloc(ls);
11262 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11263 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11264 bmap = isl_basic_map_add_constraint(bmap, c);
11266 return bmap;
11269 /* Construct a basic map where the value of the first dimension is
11270 * greater than that of the second.
11272 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11273 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11275 isl_basic_map *bmap = NULL;
11276 int i;
11278 if (!space)
11279 return NULL;
11281 if (pos1 >= isl_space_dim(space, type1))
11282 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11283 "index out of bounds", goto error);
11284 if (pos2 >= isl_space_dim(space, type2))
11285 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11286 "index out of bounds", goto error);
11288 if (type1 == type2 && pos1 == pos2)
11289 return isl_basic_map_empty(space);
11291 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11292 i = isl_basic_map_alloc_inequality(bmap);
11293 if (i < 0)
11294 goto error;
11295 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11296 pos1 += isl_basic_map_offset(bmap, type1);
11297 pos2 += isl_basic_map_offset(bmap, type2);
11298 isl_int_set_si(bmap->ineq[i][pos1], 1);
11299 isl_int_set_si(bmap->ineq[i][pos2], -1);
11300 isl_int_set_si(bmap->ineq[i][0], -1);
11301 bmap = isl_basic_map_finalize(bmap);
11303 return bmap;
11304 error:
11305 isl_space_free(space);
11306 isl_basic_map_free(bmap);
11307 return NULL;
11310 /* Add a constraint imposing that the value of the first dimension is
11311 * greater than that of the second.
11313 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11314 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11316 isl_basic_map *gt;
11318 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11320 bmap = isl_basic_map_intersect(bmap, gt);
11322 return bmap;
11325 /* Add a constraint imposing that the value of the first dimension is
11326 * greater than that of the second.
11328 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11329 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11331 isl_basic_map *bmap;
11333 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11335 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11337 return map;
11340 /* Add a constraint imposing that the value of the first dimension is
11341 * smaller than that of the second.
11343 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11344 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11346 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11349 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11350 int pos)
11352 isl_aff *div;
11353 isl_local_space *ls;
11355 if (!bmap)
11356 return NULL;
11358 if (!isl_basic_map_divs_known(bmap))
11359 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11360 "some divs are unknown", return NULL);
11362 ls = isl_basic_map_get_local_space(bmap);
11363 div = isl_local_space_get_div(ls, pos);
11364 isl_local_space_free(ls);
11366 return div;
11369 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11370 int pos)
11372 return isl_basic_map_get_div(bset, pos);
11375 /* Plug in "subs" for dimension "type", "pos" of "bset".
11377 * Let i be the dimension to replace and let "subs" be of the form
11379 * f/d
11381 * Any integer division with a non-zero coefficient for i,
11383 * floor((a i + g)/m)
11385 * is replaced by
11387 * floor((a f + d g)/(m d))
11389 * Constraints of the form
11391 * a i + g
11393 * are replaced by
11395 * a f + d g
11397 * We currently require that "subs" is an integral expression.
11398 * Handling rational expressions may require us to add stride constraints
11399 * as we do in isl_basic_set_preimage_multi_aff.
11401 __isl_give isl_basic_set *isl_basic_set_substitute(
11402 __isl_take isl_basic_set *bset,
11403 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11405 int i;
11406 isl_int v;
11407 isl_ctx *ctx;
11409 if (bset && isl_basic_set_plain_is_empty(bset))
11410 return bset;
11412 bset = isl_basic_set_cow(bset);
11413 if (!bset || !subs)
11414 goto error;
11416 ctx = isl_basic_set_get_ctx(bset);
11417 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11418 isl_die(ctx, isl_error_invalid,
11419 "spaces don't match", goto error);
11420 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11421 isl_die(ctx, isl_error_unsupported,
11422 "cannot handle divs yet", goto error);
11423 if (!isl_int_is_one(subs->v->el[0]))
11424 isl_die(ctx, isl_error_invalid,
11425 "can only substitute integer expressions", goto error);
11427 pos += isl_basic_set_offset(bset, type);
11429 isl_int_init(v);
11431 for (i = 0; i < bset->n_eq; ++i) {
11432 if (isl_int_is_zero(bset->eq[i][pos]))
11433 continue;
11434 isl_int_set(v, bset->eq[i][pos]);
11435 isl_int_set_si(bset->eq[i][pos], 0);
11436 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11437 v, subs->v->el + 1, subs->v->size - 1);
11440 for (i = 0; i < bset->n_ineq; ++i) {
11441 if (isl_int_is_zero(bset->ineq[i][pos]))
11442 continue;
11443 isl_int_set(v, bset->ineq[i][pos]);
11444 isl_int_set_si(bset->ineq[i][pos], 0);
11445 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11446 v, subs->v->el + 1, subs->v->size - 1);
11449 for (i = 0; i < bset->n_div; ++i) {
11450 if (isl_int_is_zero(bset->div[i][1 + pos]))
11451 continue;
11452 isl_int_set(v, bset->div[i][1 + pos]);
11453 isl_int_set_si(bset->div[i][1 + pos], 0);
11454 isl_seq_combine(bset->div[i] + 1,
11455 subs->v->el[0], bset->div[i] + 1,
11456 v, subs->v->el + 1, subs->v->size - 1);
11457 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11460 isl_int_clear(v);
11462 bset = isl_basic_set_simplify(bset);
11463 return isl_basic_set_finalize(bset);
11464 error:
11465 isl_basic_set_free(bset);
11466 return NULL;
11469 /* Plug in "subs" for dimension "type", "pos" of "set".
11471 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11472 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11474 int i;
11476 if (set && isl_set_plain_is_empty(set))
11477 return set;
11479 set = isl_set_cow(set);
11480 if (!set || !subs)
11481 goto error;
11483 for (i = set->n - 1; i >= 0; --i) {
11484 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11485 if (remove_if_empty(set, i) < 0)
11486 goto error;
11489 return set;
11490 error:
11491 isl_set_free(set);
11492 return NULL;
11495 /* Check if the range of "ma" is compatible with the domain or range
11496 * (depending on "type") of "bmap".
11497 * Return -1 if anything is wrong.
11499 static int check_basic_map_compatible_range_multi_aff(
11500 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11501 __isl_keep isl_multi_aff *ma)
11503 int m;
11504 isl_space *ma_space;
11506 ma_space = isl_multi_aff_get_space(ma);
11507 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11508 isl_space_free(ma_space);
11509 if (m >= 0 && !m)
11510 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11511 "spaces don't match", return -1);
11512 return m;
11515 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11516 * coefficients before the transformed range of dimensions,
11517 * the "n_after" coefficients after the transformed range of dimensions
11518 * and the coefficients of the other divs in "bmap".
11520 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11521 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11523 int i;
11524 int n_param;
11525 int n_set;
11526 isl_local_space *ls;
11528 if (n_div == 0)
11529 return 0;
11531 ls = isl_aff_get_domain_local_space(ma->p[0]);
11532 if (!ls)
11533 return -1;
11535 n_param = isl_local_space_dim(ls, isl_dim_param);
11536 n_set = isl_local_space_dim(ls, isl_dim_set);
11537 for (i = 0; i < n_div; ++i) {
11538 int o_bmap = 0, o_ls = 0;
11540 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11541 o_bmap += 1 + 1 + n_param;
11542 o_ls += 1 + 1 + n_param;
11543 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11544 o_bmap += n_before;
11545 isl_seq_cpy(bmap->div[i] + o_bmap,
11546 ls->div->row[i] + o_ls, n_set);
11547 o_bmap += n_set;
11548 o_ls += n_set;
11549 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11550 o_bmap += n_after;
11551 isl_seq_cpy(bmap->div[i] + o_bmap,
11552 ls->div->row[i] + o_ls, n_div);
11553 o_bmap += n_div;
11554 o_ls += n_div;
11555 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11556 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11557 goto error;
11560 isl_local_space_free(ls);
11561 return 0;
11562 error:
11563 isl_local_space_free(ls);
11564 return -1;
11567 /* How many stride constraints does "ma" enforce?
11568 * That is, how many of the affine expressions have a denominator
11569 * different from one?
11571 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11573 int i;
11574 int strides = 0;
11576 for (i = 0; i < ma->n; ++i)
11577 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11578 strides++;
11580 return strides;
11583 /* For each affine expression in ma of the form
11585 * x_i = (f_i y + h_i)/m_i
11587 * with m_i different from one, add a constraint to "bmap"
11588 * of the form
11590 * f_i y + h_i = m_i alpha_i
11592 * with alpha_i an additional existentially quantified variable.
11594 static __isl_give isl_basic_map *add_ma_strides(
11595 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11596 int n_before, int n_after)
11598 int i, k;
11599 int div;
11600 int total;
11601 int n_param;
11602 int n_in;
11603 int n_div;
11605 total = isl_basic_map_total_dim(bmap);
11606 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11607 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11608 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11609 for (i = 0; i < ma->n; ++i) {
11610 int o_bmap = 0, o_ma = 1;
11612 if (isl_int_is_one(ma->p[i]->v->el[0]))
11613 continue;
11614 div = isl_basic_map_alloc_div(bmap);
11615 k = isl_basic_map_alloc_equality(bmap);
11616 if (div < 0 || k < 0)
11617 goto error;
11618 isl_int_set_si(bmap->div[div][0], 0);
11619 isl_seq_cpy(bmap->eq[k] + o_bmap,
11620 ma->p[i]->v->el + o_ma, 1 + n_param);
11621 o_bmap += 1 + n_param;
11622 o_ma += 1 + n_param;
11623 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11624 o_bmap += n_before;
11625 isl_seq_cpy(bmap->eq[k] + o_bmap,
11626 ma->p[i]->v->el + o_ma, n_in);
11627 o_bmap += n_in;
11628 o_ma += n_in;
11629 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11630 o_bmap += n_after;
11631 isl_seq_cpy(bmap->eq[k] + o_bmap,
11632 ma->p[i]->v->el + o_ma, n_div);
11633 o_bmap += n_div;
11634 o_ma += n_div;
11635 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11636 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11637 total++;
11640 return bmap;
11641 error:
11642 isl_basic_map_free(bmap);
11643 return NULL;
11646 /* Replace the domain or range space (depending on "type) of "space" by "set".
11648 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11649 enum isl_dim_type type, __isl_take isl_space *set)
11651 if (type == isl_dim_in) {
11652 space = isl_space_range(space);
11653 space = isl_space_map_from_domain_and_range(set, space);
11654 } else {
11655 space = isl_space_domain(space);
11656 space = isl_space_map_from_domain_and_range(space, set);
11659 return space;
11662 /* Compute the preimage of the domain or range (depending on "type")
11663 * of "bmap" under the function represented by "ma".
11664 * In other words, plug in "ma" in the domain or range of "bmap".
11665 * The result is a basic map that lives in the same space as "bmap"
11666 * except that the domain or range has been replaced by
11667 * the domain space of "ma".
11669 * If bmap is represented by
11671 * A(p) + S u + B x + T v + C(divs) >= 0,
11673 * where u and x are input and output dimensions if type == isl_dim_out
11674 * while x and v are input and output dimensions if type == isl_dim_in,
11675 * and ma is represented by
11677 * x = D(p) + F(y) + G(divs')
11679 * then the result is
11681 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11683 * The divs in the input set are similarly adjusted.
11684 * In particular
11686 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11688 * becomes
11690 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11691 * B_i G(divs') + c_i(divs))/n_i)
11693 * If bmap is not a rational map and if F(y) involves any denominators
11695 * x_i = (f_i y + h_i)/m_i
11697 * then additional constraints are added to ensure that we only
11698 * map back integer points. That is we enforce
11700 * f_i y + h_i = m_i alpha_i
11702 * with alpha_i an additional existentially quantified variable.
11704 * We first copy over the divs from "ma".
11705 * Then we add the modified constraints and divs from "bmap".
11706 * Finally, we add the stride constraints, if needed.
11708 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11709 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11710 __isl_take isl_multi_aff *ma)
11712 int i, k;
11713 isl_space *space;
11714 isl_basic_map *res = NULL;
11715 int n_before, n_after, n_div_bmap, n_div_ma;
11716 isl_int f, c1, c2, g;
11717 int rational, strides;
11719 isl_int_init(f);
11720 isl_int_init(c1);
11721 isl_int_init(c2);
11722 isl_int_init(g);
11724 ma = isl_multi_aff_align_divs(ma);
11725 if (!bmap || !ma)
11726 goto error;
11727 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11728 goto error;
11730 if (type == isl_dim_in) {
11731 n_before = 0;
11732 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11733 } else {
11734 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11735 n_after = 0;
11737 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11738 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11740 space = isl_multi_aff_get_domain_space(ma);
11741 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11742 rational = isl_basic_map_is_rational(bmap);
11743 strides = rational ? 0 : multi_aff_strides(ma);
11744 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11745 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11746 if (rational)
11747 res = isl_basic_map_set_rational(res);
11749 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11750 if (isl_basic_map_alloc_div(res) < 0)
11751 goto error;
11753 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11754 goto error;
11756 for (i = 0; i < bmap->n_eq; ++i) {
11757 k = isl_basic_map_alloc_equality(res);
11758 if (k < 0)
11759 goto error;
11760 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11761 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11764 for (i = 0; i < bmap->n_ineq; ++i) {
11765 k = isl_basic_map_alloc_inequality(res);
11766 if (k < 0)
11767 goto error;
11768 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11769 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11772 for (i = 0; i < bmap->n_div; ++i) {
11773 if (isl_int_is_zero(bmap->div[i][0])) {
11774 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11775 continue;
11777 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11778 n_before, n_after, n_div_ma, n_div_bmap,
11779 f, c1, c2, g, 1);
11782 if (strides)
11783 res = add_ma_strides(res, ma, n_before, n_after);
11785 isl_int_clear(f);
11786 isl_int_clear(c1);
11787 isl_int_clear(c2);
11788 isl_int_clear(g);
11789 isl_basic_map_free(bmap);
11790 isl_multi_aff_free(ma);
11791 res = isl_basic_set_simplify(res);
11792 return isl_basic_map_finalize(res);
11793 error:
11794 isl_int_clear(f);
11795 isl_int_clear(c1);
11796 isl_int_clear(c2);
11797 isl_int_clear(g);
11798 isl_basic_map_free(bmap);
11799 isl_multi_aff_free(ma);
11800 isl_basic_map_free(res);
11801 return NULL;
11804 /* Compute the preimage of "bset" under the function represented by "ma".
11805 * In other words, plug in "ma" in "bset". The result is a basic set
11806 * that lives in the domain space of "ma".
11808 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11809 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11811 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11814 /* Check if the range of "ma" is compatible with the domain or range
11815 * (depending on "type") of "map".
11816 * Return -1 if anything is wrong.
11818 static int check_map_compatible_range_multi_aff(
11819 __isl_keep isl_map *map, enum isl_dim_type type,
11820 __isl_keep isl_multi_aff *ma)
11822 int m;
11823 isl_space *ma_space;
11825 ma_space = isl_multi_aff_get_space(ma);
11826 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11827 isl_space_free(ma_space);
11828 if (m >= 0 && !m)
11829 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11830 "spaces don't match", return -1);
11831 return m;
11834 /* Compute the preimage of the domain or range (depending on "type")
11835 * of "map" under the function represented by "ma".
11836 * In other words, plug in "ma" in the domain or range of "map".
11837 * The result is a map that lives in the same space as "map"
11838 * except that the domain or range has been replaced by
11839 * the domain space of "ma".
11841 * The parameters are assumed to have been aligned.
11843 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11844 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11846 int i;
11847 isl_space *space;
11849 map = isl_map_cow(map);
11850 ma = isl_multi_aff_align_divs(ma);
11851 if (!map || !ma)
11852 goto error;
11853 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11854 goto error;
11856 for (i = 0; i < map->n; ++i) {
11857 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11858 isl_multi_aff_copy(ma));
11859 if (!map->p[i])
11860 goto error;
11863 space = isl_multi_aff_get_domain_space(ma);
11864 space = isl_space_set(isl_map_get_space(map), type, space);
11866 isl_space_free(map->dim);
11867 map->dim = space;
11868 if (!map->dim)
11869 goto error;
11871 isl_multi_aff_free(ma);
11872 if (map->n > 1)
11873 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11874 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11875 return map;
11876 error:
11877 isl_multi_aff_free(ma);
11878 isl_map_free(map);
11879 return NULL;
11882 /* Compute the preimage of the domain or range (depending on "type")
11883 * of "map" under the function represented by "ma".
11884 * In other words, plug in "ma" in the domain or range of "map".
11885 * The result is a map that lives in the same space as "map"
11886 * except that the domain or range has been replaced by
11887 * the domain space of "ma".
11889 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11890 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11892 if (!map || !ma)
11893 goto error;
11895 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11896 return map_preimage_multi_aff(map, type, ma);
11898 if (!isl_space_has_named_params(map->dim) ||
11899 !isl_space_has_named_params(ma->space))
11900 isl_die(map->ctx, isl_error_invalid,
11901 "unaligned unnamed parameters", goto error);
11902 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11903 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11905 return map_preimage_multi_aff(map, type, ma);
11906 error:
11907 isl_multi_aff_free(ma);
11908 return isl_map_free(map);
11911 /* Compute the preimage of "set" under the function represented by "ma".
11912 * In other words, plug in "ma" "set". The result is a set
11913 * that lives in the domain space of "ma".
11915 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11916 __isl_take isl_multi_aff *ma)
11918 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11921 /* Compute the preimage of the domain of "map" under the function
11922 * represented by "ma".
11923 * In other words, plug in "ma" in the domain of "map".
11924 * The result is a map that lives in the same space as "map"
11925 * except that the domain has been replaced by the domain space of "ma".
11927 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11928 __isl_take isl_multi_aff *ma)
11930 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11933 /* Compute the preimage of "set" under the function represented by "pma".
11934 * In other words, plug in "pma" in "set. The result is a set
11935 * that lives in the domain space of "pma".
11937 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11938 __isl_take isl_pw_multi_aff *pma)
11940 int i;
11941 isl_set *res;
11943 if (!pma)
11944 goto error;
11946 if (pma->n == 0) {
11947 isl_pw_multi_aff_free(pma);
11948 res = isl_set_empty(isl_set_get_space(set));
11949 isl_set_free(set);
11950 return res;
11953 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11954 isl_multi_aff_copy(pma->p[0].maff));
11955 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11957 for (i = 1; i < pma->n; ++i) {
11958 isl_set *res_i;
11960 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11961 isl_multi_aff_copy(pma->p[i].maff));
11962 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11963 res = isl_set_union(res, res_i);
11966 isl_pw_multi_aff_free(pma);
11967 isl_set_free(set);
11968 return res;
11969 error:
11970 isl_pw_multi_aff_free(pma);
11971 isl_set_free(set);
11972 return NULL;
11975 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11976 __isl_take isl_pw_multi_aff *pma)
11978 if (!set || !pma)
11979 goto error;
11981 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11982 return set_preimage_pw_multi_aff(set, pma);
11984 if (!isl_space_has_named_params(set->dim) ||
11985 !isl_space_has_named_params(pma->dim))
11986 isl_die(set->ctx, isl_error_invalid,
11987 "unaligned unnamed parameters", goto error);
11988 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11989 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11991 return set_preimage_pw_multi_aff(set, pma);
11992 error:
11993 isl_pw_multi_aff_free(pma);
11994 return isl_set_free(set);