add exported isl_union_map_domain_reverse
[isl.git] / isl_map.c
blobfb33f885bf0c03ce797664aa636900454528b01a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <string.h>
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
28 #include <isl_blk.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
34 #include <isl_seq.h>
35 #include <isl/set.h>
36 #include <isl/map.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
39 #include <isl_sort.h>
40 #include "isl_tab.h"
41 #include <isl/vec.h>
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep const isl_basic_map *const_bset_to_bmap(
62 __isl_keep const isl_basic_set *bset)
64 return (const isl_basic_map *) bset;
67 #undef TYPE
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
73 switch (type) {
74 case isl_dim_param: return 1;
75 case isl_dim_in: return 1 + space->nparam;
76 case isl_dim_out: return 1 + space->nparam + space->n_in;
77 default: return 0;
81 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
82 enum isl_dim_type type)
84 if (!bmap)
85 return isl_size_error;
86 switch (type) {
87 case isl_dim_cst: return 1;
88 case isl_dim_param:
89 case isl_dim_in:
90 case isl_dim_out: return isl_space_dim(bmap->dim, type);
91 case isl_dim_div: return bmap->n_div;
92 case isl_dim_all: return isl_basic_map_total_dim(bmap);
93 default: return 0;
97 /* Return the space of "map".
99 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
101 return map ? map->dim : NULL;
104 /* Return the space of "set".
106 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
108 return isl_map_peek_space(set_to_map(set));
111 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
113 return isl_space_dim(isl_map_peek_space(map), type);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
120 return isl_map_dim(map, isl_dim_in);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
127 return isl_map_dim(map, isl_dim_out);
130 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
132 return isl_map_dim(set_to_map(set), type);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_set);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
146 enum isl_dim_type type)
148 isl_space *space;
150 space = isl_basic_map_peek_space(bmap);
151 if (!space)
152 return isl_size_error;
154 switch (type) {
155 case isl_dim_param:
156 case isl_dim_in:
157 case isl_dim_out: return isl_space_offset(space, type);
158 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
159 case isl_dim_cst:
160 default:
161 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
162 "invalid dimension type", return isl_size_error);
166 /* Return the position of the variables of the given type
167 * within the sequence of variables of "bset".
169 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
172 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
175 /* Return the position of the coefficients of the variables of the given type
176 * within the sequence of coefficients of "bmap".
178 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
179 enum isl_dim_type type)
181 switch (type) {
182 case isl_dim_cst: return 0;
183 case isl_dim_param:
184 case isl_dim_in:
185 case isl_dim_out:
186 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
187 default: return 0;
191 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
192 enum isl_dim_type type)
194 return isl_basic_map_offset(bset, type);
197 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
199 return pos(map->dim, type);
202 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
203 enum isl_dim_type type)
205 return isl_basic_map_dim(bset, type);
208 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
210 return isl_basic_set_dim(bset, isl_dim_set);
213 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
215 return isl_basic_set_dim(bset, isl_dim_param);
218 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
220 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
223 isl_size isl_set_n_dim(__isl_keep isl_set *set)
225 return isl_set_dim(set, isl_dim_set);
228 isl_size isl_set_n_param(__isl_keep isl_set *set)
230 return isl_set_dim(set, isl_dim_param);
233 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
235 isl_size dim;
237 if (!bmap)
238 return isl_size_error;
239 dim = isl_space_dim(bmap->dim, isl_dim_all);
240 if (dim < 0)
241 return isl_size_error;
242 return dim + bmap->n_div;
245 /* Return the number of equality constraints in the description of "bmap".
246 * Return isl_size_error on error.
248 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
250 if (!bmap)
251 return isl_size_error;
252 return bmap->n_eq;
255 /* Return the number of equality constraints in the description of "bset".
256 * Return isl_size_error on error.
258 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
260 return isl_basic_map_n_equality(bset_to_bmap(bset));
263 /* Return the number of inequality constraints in the description of "bmap".
264 * Return isl_size_error on error.
266 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
268 if (!bmap)
269 return isl_size_error;
270 return bmap->n_ineq;
273 /* Return the number of inequality constraints in the description of "bset".
274 * Return isl_size_error on error.
276 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
278 return isl_basic_map_n_inequality(bset_to_bmap(bset));
281 /* Do "bmap1" and "bmap2" have the same parameters?
283 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
284 __isl_keep isl_basic_map *bmap2)
286 isl_space *space1, *space2;
288 space1 = isl_basic_map_peek_space(bmap1);
289 space2 = isl_basic_map_peek_space(bmap2);
290 return isl_space_has_equal_params(space1, space2);
293 /* Do "map1" and "map2" have the same parameters?
295 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
296 __isl_keep isl_map *map2)
298 isl_space *space1, *space2;
300 space1 = isl_map_peek_space(map1);
301 space2 = isl_map_peek_space(map2);
302 return isl_space_has_equal_params(space1, space2);
305 /* Do "map" and "set" have the same parameters?
307 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
308 __isl_keep isl_set *set)
310 return isl_map_has_equal_params(map, set_to_map(set));
313 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
315 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
316 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
318 isl_space *bmap_space, *bset_space;
320 bmap_space = isl_basic_map_peek_space(bmap);
321 bset_space = isl_basic_set_peek_space(bset);
322 return isl_space_tuple_is_equal(bmap_space, type,
323 bset_space, isl_dim_set);
326 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
328 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
329 enum isl_dim_type type, __isl_keep isl_set *set)
331 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
334 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
335 __isl_keep isl_set *set)
337 isl_bool m;
338 if (!map || !set)
339 return isl_bool_error;
340 m = isl_map_has_equal_params(map, set_to_map(set));
341 if (m < 0 || !m)
342 return m;
343 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
346 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
347 __isl_keep isl_basic_set *bset)
349 isl_bool m;
350 if (!bmap || !bset)
351 return isl_bool_error;
352 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
353 if (m < 0 || !m)
354 return m;
355 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
358 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
359 __isl_keep isl_set *set)
361 isl_bool m;
362 if (!map || !set)
363 return isl_bool_error;
364 m = isl_map_has_equal_params(map, set_to_map(set));
365 if (m < 0 || !m)
366 return m;
367 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
370 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
371 __isl_keep isl_basic_set *bset)
373 isl_bool m;
374 if (!bmap || !bset)
375 return isl_bool_error;
376 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
377 if (m < 0 || !m)
378 return m;
379 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
382 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
384 return bmap ? bmap->ctx : NULL;
387 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
389 return bset ? bset->ctx : NULL;
392 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
394 return map ? map->ctx : NULL;
397 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
399 return set ? set->ctx : NULL;
402 /* Return the space of "bmap".
404 __isl_keep isl_space *isl_basic_map_peek_space(
405 __isl_keep const isl_basic_map *bmap)
407 return bmap ? bmap->dim : NULL;
410 /* Return the space of "bset".
412 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
414 return isl_basic_map_peek_space(bset_to_bmap(bset));
417 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
419 return isl_space_copy(isl_basic_map_peek_space(bmap));
422 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
424 return isl_basic_map_get_space(bset_to_bmap(bset));
427 /* Return the space of "bmap".
428 * This may be either a copy or the space itself
429 * if there is only one reference to "bmap".
430 * This allows the space to be modified inplace
431 * if both the basic map and its space have only a single reference.
432 * The caller is not allowed to modify "bmap" between this call and
433 * a subsequent call to isl_basic_map_restore_space.
434 * The only exception is that isl_basic_map_free can be called instead.
436 static __isl_give isl_space *isl_basic_map_take_space(
437 __isl_keep isl_basic_map *bmap)
439 isl_space *space;
441 if (!bmap)
442 return NULL;
443 if (bmap->ref != 1)
444 return isl_basic_map_get_space(bmap);
445 space = bmap->dim;
446 bmap->dim = NULL;
447 return space;
450 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
451 * due to a preceding call to isl_basic_map_take_space.
452 * However, in this case, "bmap" only has a single reference and
453 * then the call to isl_basic_map_cow has no effect.
455 static __isl_give isl_basic_map *isl_basic_map_restore_space(
456 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
458 if (!bmap || !space)
459 goto error;
461 if (bmap->dim == space) {
462 isl_space_free(space);
463 return bmap;
466 bmap = isl_basic_map_cow(bmap);
467 if (!bmap)
468 goto error;
469 isl_space_free(bmap->dim);
470 bmap->dim = space;
472 return bmap;
473 error:
474 isl_basic_map_free(bmap);
475 isl_space_free(space);
476 return NULL;
479 /* Extract the divs in "bmap" as a matrix.
481 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
483 int i;
484 isl_ctx *ctx;
485 isl_mat *div;
486 isl_size v_div;
487 unsigned cols;
489 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
490 if (v_div < 0)
491 return NULL;
493 ctx = isl_basic_map_get_ctx(bmap);
494 cols = 1 + 1 + v_div + bmap->n_div;
495 div = isl_mat_alloc(ctx, bmap->n_div, cols);
496 if (!div)
497 return NULL;
499 for (i = 0; i < bmap->n_div; ++i)
500 isl_seq_cpy(div->row[i], bmap->div[i], cols);
502 return div;
505 /* Extract the divs in "bset" as a matrix.
507 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
509 return isl_basic_map_get_divs(bset);
512 __isl_give isl_local_space *isl_basic_map_get_local_space(
513 __isl_keep isl_basic_map *bmap)
515 isl_mat *div;
517 if (!bmap)
518 return NULL;
520 div = isl_basic_map_get_divs(bmap);
521 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
524 __isl_give isl_local_space *isl_basic_set_get_local_space(
525 __isl_keep isl_basic_set *bset)
527 return isl_basic_map_get_local_space(bset);
530 /* For each known div d = floor(f/m), add the constraints
532 * f - m d >= 0
533 * -(f-(m-1)) + m d >= 0
535 * Do not finalize the result.
537 static __isl_give isl_basic_map *add_known_div_constraints(
538 __isl_take isl_basic_map *bmap)
540 int i;
541 isl_size n_div;
543 n_div = isl_basic_map_dim(bmap, isl_dim_div);
544 if (n_div < 0)
545 return isl_basic_map_free(bmap);
546 if (n_div == 0)
547 return bmap;
548 bmap = isl_basic_map_cow(bmap);
549 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
550 if (!bmap)
551 return NULL;
552 for (i = 0; i < n_div; ++i) {
553 if (isl_int_is_zero(bmap->div[i][0]))
554 continue;
555 bmap = isl_basic_map_add_div_constraints(bmap, i);
558 return bmap;
561 __isl_give isl_basic_map *isl_basic_map_from_local_space(
562 __isl_take isl_local_space *ls)
564 int i;
565 isl_size n_div;
566 isl_basic_map *bmap;
568 n_div = isl_local_space_dim(ls, isl_dim_div);
569 if (n_div < 0)
570 ls = isl_local_space_free(ls);
571 if (!ls)
572 return NULL;
574 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
575 n_div, 0, 2 * n_div);
577 for (i = 0; i < n_div; ++i)
578 if (isl_basic_map_alloc_div(bmap) < 0)
579 goto error;
581 for (i = 0; i < n_div; ++i)
582 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
583 bmap = add_known_div_constraints(bmap);
585 isl_local_space_free(ls);
586 return bmap;
587 error:
588 isl_local_space_free(ls);
589 isl_basic_map_free(bmap);
590 return NULL;
593 __isl_give isl_basic_set *isl_basic_set_from_local_space(
594 __isl_take isl_local_space *ls)
596 return isl_basic_map_from_local_space(ls);
599 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
601 return isl_space_copy(isl_map_peek_space(map));
604 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
606 if (!set)
607 return NULL;
608 return isl_space_copy(set->dim);
611 /* Return the space of "map".
612 * This may be either a copy or the space itself
613 * if there is only one reference to "map".
614 * This allows the space to be modified inplace
615 * if both the map and its space have only a single reference.
616 * The caller is not allowed to modify "map" between this call and
617 * a subsequent call to isl_map_restore_space.
618 * The only exception is that isl_map_free can be called instead.
620 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
622 isl_space *space;
624 if (!map)
625 return NULL;
626 if (map->ref != 1)
627 return isl_map_get_space(map);
628 space = map->dim;
629 map->dim = NULL;
630 return space;
633 /* Set the space of "map" to "space", where the space of "map" may be missing
634 * due to a preceding call to isl_map_take_space.
635 * However, in this case, "map" only has a single reference and
636 * then the call to isl_map_cow has no effect.
638 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
639 __isl_take isl_space *space)
641 if (!map || !space)
642 goto error;
644 if (map->dim == space) {
645 isl_space_free(space);
646 return map;
649 map = isl_map_cow(map);
650 if (!map)
651 goto error;
652 isl_space_free(map->dim);
653 map->dim = space;
655 return map;
656 error:
657 isl_map_free(map);
658 isl_space_free(space);
659 return NULL;
662 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
663 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
665 isl_space *space;
667 space = isl_basic_map_take_space(bmap);
668 space = isl_space_set_tuple_name(space, type, s);
669 bmap = isl_basic_map_restore_space(bmap, space);
670 bmap = isl_basic_map_finalize(bmap);
671 return bmap;
674 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
675 __isl_take isl_basic_set *bset, const char *s)
677 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
680 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
681 enum isl_dim_type type)
683 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
686 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
687 enum isl_dim_type type, const char *s)
689 int i;
690 isl_space *space;
692 map = isl_map_cow(map);
693 if (!map)
694 return NULL;
696 for (i = 0; i < map->n; ++i) {
697 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
698 if (!map->p[i])
699 goto error;
702 space = isl_map_take_space(map);
703 space = isl_space_set_tuple_name(space, type, s);
704 map = isl_map_restore_space(map, space);
706 return map;
707 error:
708 isl_map_free(map);
709 return NULL;
712 /* Replace the identifier of the tuple of type "type" by "id".
714 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
715 __isl_take isl_basic_map *bmap,
716 enum isl_dim_type type, __isl_take isl_id *id)
718 isl_space *space;
720 space = isl_basic_map_take_space(bmap);
721 space = isl_space_set_tuple_id(space, type, id);
722 bmap = isl_basic_map_restore_space(bmap, space);
723 bmap = isl_basic_map_finalize(bmap);
724 return bmap;
727 /* Replace the identifier of the tuple by "id".
729 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
730 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
732 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
735 /* Does the input or output tuple have a name?
737 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
739 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
742 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
743 enum isl_dim_type type)
745 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
748 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
749 const char *s)
751 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
752 isl_dim_set, s));
755 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
756 enum isl_dim_type type, __isl_take isl_id *id)
758 isl_space *space;
760 space = isl_map_take_space(map);
761 space = isl_space_set_tuple_id(space, type, id);
762 map = isl_map_restore_space(map, space);
764 return isl_map_reset_space(map, isl_map_get_space(map));
767 /* Replace the identifier of the domain tuple of "map" by "id".
769 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
770 __isl_take isl_id *id)
772 return isl_map_set_tuple_id(map, isl_dim_in, id);
775 /* Replace the identifier of the range tuple of "map" by "id".
777 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
778 __isl_take isl_id *id)
780 return isl_map_set_tuple_id(map, isl_dim_out, id);
783 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
784 __isl_take isl_id *id)
786 return isl_map_set_tuple_id(set, isl_dim_set, id);
789 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
790 enum isl_dim_type type)
792 isl_space *space;
794 space = isl_map_take_space(map);
795 space = isl_space_reset_tuple_id(space, type);
796 map = isl_map_restore_space(map, space);
798 return isl_map_reset_space(map, isl_map_get_space(map));
801 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
803 return isl_map_reset_tuple_id(set, isl_dim_set);
806 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
808 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
811 /* Does the domain tuple of "map" have an identifier?
813 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
815 return isl_map_has_tuple_id(map, isl_dim_in);
818 /* Does the range tuple of "map" have an identifier?
820 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
822 return isl_map_has_tuple_id(map, isl_dim_out);
825 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
826 enum isl_dim_type type)
828 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
831 /* Return the identifier of the domain tuple of "map", assuming it has one.
833 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
835 return isl_map_get_tuple_id(map, isl_dim_in);
838 /* Return the identifier of the range tuple of "map", assuming it has one.
840 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
842 return isl_map_get_tuple_id(map, isl_dim_out);
845 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
847 return isl_map_has_tuple_id(set, isl_dim_set);
850 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
852 return isl_map_get_tuple_id(set, isl_dim_set);
855 /* Does the set tuple have a name?
857 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
859 if (!set)
860 return isl_bool_error;
861 return isl_space_has_tuple_name(set->dim, isl_dim_set);
865 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
867 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
870 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
872 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
875 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
876 enum isl_dim_type type, unsigned pos)
878 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
881 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
882 enum isl_dim_type type, unsigned pos)
884 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
887 /* Does the given dimension have a name?
889 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
890 enum isl_dim_type type, unsigned pos)
892 if (!map)
893 return isl_bool_error;
894 return isl_space_has_dim_name(map->dim, type, pos);
897 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
898 enum isl_dim_type type, unsigned pos)
900 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
903 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
904 enum isl_dim_type type, unsigned pos)
906 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
909 /* Does the given dimension have a name?
911 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
912 enum isl_dim_type type, unsigned pos)
914 if (!set)
915 return isl_bool_error;
916 return isl_space_has_dim_name(set->dim, type, pos);
919 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
920 __isl_take isl_basic_map *bmap,
921 enum isl_dim_type type, unsigned pos, const char *s)
923 isl_space *space;
925 space = isl_basic_map_take_space(bmap);
926 space = isl_space_set_dim_name(space, type, pos, s);
927 bmap = isl_basic_map_restore_space(bmap, space);
928 return isl_basic_map_finalize(bmap);
931 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
932 enum isl_dim_type type, unsigned pos, const char *s)
934 int i;
935 isl_space *space;
937 map = isl_map_cow(map);
938 if (!map)
939 return NULL;
941 for (i = 0; i < map->n; ++i) {
942 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
943 if (!map->p[i])
944 goto error;
947 space = isl_map_take_space(map);
948 space = isl_space_set_dim_name(space, type, pos, s);
949 map = isl_map_restore_space(map, space);
951 return map;
952 error:
953 isl_map_free(map);
954 return NULL;
957 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
958 __isl_take isl_basic_set *bset,
959 enum isl_dim_type type, unsigned pos, const char *s)
961 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
962 type, pos, s));
965 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
966 enum isl_dim_type type, unsigned pos, const char *s)
968 return set_from_map(isl_map_set_dim_name(set_to_map(set),
969 type, pos, s));
972 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
973 enum isl_dim_type type, unsigned pos)
975 if (!bmap)
976 return isl_bool_error;
977 return isl_space_has_dim_id(bmap->dim, type, pos);
980 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
981 enum isl_dim_type type, unsigned pos)
983 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
986 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
987 enum isl_dim_type type, unsigned pos)
989 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
992 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
993 enum isl_dim_type type, unsigned pos)
995 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
998 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
999 enum isl_dim_type type, unsigned pos)
1001 return isl_map_has_dim_id(set, type, pos);
1004 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1005 enum isl_dim_type type, unsigned pos)
1007 return isl_map_get_dim_id(set, type, pos);
1010 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1011 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1013 isl_space *space;
1015 space = isl_map_take_space(map);
1016 space = isl_space_set_dim_id(space, type, pos, id);
1017 map = isl_map_restore_space(map, space);
1019 return isl_map_reset_space(map, isl_map_get_space(map));
1022 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1023 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1025 return isl_map_set_dim_id(set, type, pos, id);
1028 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1029 __isl_keep isl_id *id)
1031 if (!map)
1032 return -1;
1033 return isl_space_find_dim_by_id(map->dim, type, id);
1036 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1037 __isl_keep isl_id *id)
1039 return isl_map_find_dim_by_id(set, type, id);
1042 /* Return the position of the dimension of the given type and name
1043 * in "bmap".
1044 * Return -1 if no such dimension can be found.
1046 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1047 enum isl_dim_type type, const char *name)
1049 if (!bmap)
1050 return -1;
1051 return isl_space_find_dim_by_name(bmap->dim, type, name);
1054 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1055 const char *name)
1057 if (!map)
1058 return -1;
1059 return isl_space_find_dim_by_name(map->dim, type, name);
1062 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1063 const char *name)
1065 return isl_map_find_dim_by_name(set, type, name);
1068 /* Check whether equality i of bset is a pure stride constraint
1069 * on a single dimension, i.e., of the form
1071 * v = k e
1073 * with k a constant and e an existentially quantified variable.
1075 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1077 isl_size nparam;
1078 isl_size d;
1079 isl_size n_div;
1080 int pos1;
1081 int pos2;
1083 nparam = isl_basic_set_dim(bset, isl_dim_param);
1084 d = isl_basic_set_dim(bset, isl_dim_set);
1085 n_div = isl_basic_set_dim(bset, isl_dim_div);
1086 if (nparam < 0 || d < 0 || n_div < 0)
1087 return isl_bool_error;
1089 if (!isl_int_is_zero(bset->eq[i][0]))
1090 return isl_bool_false;
1092 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1093 return isl_bool_false;
1094 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1095 if (pos1 == -1)
1096 return isl_bool_false;
1097 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1098 d - pos1 - 1) != -1)
1099 return isl_bool_false;
1101 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1102 if (pos2 == -1)
1103 return isl_bool_false;
1104 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1105 n_div - pos2 - 1) != -1)
1106 return isl_bool_false;
1107 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1108 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1109 return isl_bool_false;
1111 return isl_bool_true;
1114 /* Reset the user pointer on all identifiers of parameters and tuples
1115 * of the space of "map".
1117 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1119 isl_space *space;
1121 space = isl_map_get_space(map);
1122 space = isl_space_reset_user(space);
1123 map = isl_map_reset_space(map, space);
1125 return map;
1128 /* Reset the user pointer on all identifiers of parameters and tuples
1129 * of the space of "set".
1131 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1133 return isl_map_reset_user(set);
1136 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1138 if (!bmap)
1139 return isl_bool_error;
1140 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1143 /* Has "map" been marked as a rational map?
1144 * In particular, have all basic maps in "map" been marked this way?
1145 * An empty map is not considered to be rational.
1146 * Maps where only some of the basic maps are marked rational
1147 * are not allowed.
1149 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1151 int i;
1152 isl_bool rational;
1154 if (!map)
1155 return isl_bool_error;
1156 if (map->n == 0)
1157 return isl_bool_false;
1158 rational = isl_basic_map_is_rational(map->p[0]);
1159 if (rational < 0)
1160 return rational;
1161 for (i = 1; i < map->n; ++i) {
1162 isl_bool rational_i;
1164 rational_i = isl_basic_map_is_rational(map->p[i]);
1165 if (rational_i < 0)
1166 return rational_i;
1167 if (rational != rational_i)
1168 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1169 "mixed rational and integer basic maps "
1170 "not supported", return isl_bool_error);
1173 return rational;
1176 /* Has "set" been marked as a rational set?
1177 * In particular, have all basic set in "set" been marked this way?
1178 * An empty set is not considered to be rational.
1179 * Sets where only some of the basic sets are marked rational
1180 * are not allowed.
1182 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1184 return isl_map_is_rational(set);
1187 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1189 return isl_basic_map_is_rational(bset);
1192 /* Does "bmap" contain any rational points?
1194 * If "bmap" has an equality for each dimension, equating the dimension
1195 * to an integer constant, then it has no rational points, even if it
1196 * is marked as rational.
1198 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1200 isl_bool has_rational = isl_bool_true;
1201 isl_size total;
1203 if (!bmap)
1204 return isl_bool_error;
1205 if (isl_basic_map_plain_is_empty(bmap))
1206 return isl_bool_false;
1207 if (!isl_basic_map_is_rational(bmap))
1208 return isl_bool_false;
1209 bmap = isl_basic_map_copy(bmap);
1210 bmap = isl_basic_map_implicit_equalities(bmap);
1211 total = isl_basic_map_dim(bmap, isl_dim_all);
1212 if (total < 0)
1213 return isl_bool_error;
1214 if (bmap->n_eq == total) {
1215 int i, j;
1216 for (i = 0; i < bmap->n_eq; ++i) {
1217 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1218 if (j < 0)
1219 break;
1220 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1221 !isl_int_is_negone(bmap->eq[i][1 + j]))
1222 break;
1223 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1224 total - j - 1);
1225 if (j >= 0)
1226 break;
1228 if (i == bmap->n_eq)
1229 has_rational = isl_bool_false;
1231 isl_basic_map_free(bmap);
1233 return has_rational;
1236 /* Does "map" contain any rational points?
1238 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1240 int i;
1241 isl_bool has_rational;
1243 if (!map)
1244 return isl_bool_error;
1245 for (i = 0; i < map->n; ++i) {
1246 has_rational = isl_basic_map_has_rational(map->p[i]);
1247 if (has_rational < 0 || has_rational)
1248 return has_rational;
1250 return isl_bool_false;
1253 /* Does "set" contain any rational points?
1255 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1257 return isl_map_has_rational(set);
1260 /* Is this basic set a parameter domain?
1262 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1264 if (!bset)
1265 return isl_bool_error;
1266 return isl_space_is_params(bset->dim);
1269 /* Is this set a parameter domain?
1271 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1273 if (!set)
1274 return isl_bool_error;
1275 return isl_space_is_params(set->dim);
1278 /* Is this map actually a parameter domain?
1279 * Users should never call this function. Outside of isl,
1280 * a map can never be a parameter domain.
1282 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1284 if (!map)
1285 return isl_bool_error;
1286 return isl_space_is_params(map->dim);
1289 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1290 __isl_take isl_basic_map *bmap, unsigned extra,
1291 unsigned n_eq, unsigned n_ineq)
1293 int i;
1294 isl_space *space = isl_basic_map_peek_space(bmap);
1295 isl_size n_var = isl_space_dim(space, isl_dim_all);
1296 size_t row_size = 1 + n_var + extra;
1298 bmap->ctx = ctx;
1299 isl_ctx_ref(ctx);
1301 if (n_var < 0)
1302 return isl_basic_map_free(bmap);
1304 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1305 if (isl_blk_is_error(bmap->block))
1306 goto error;
1308 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1309 if ((n_ineq + n_eq) && !bmap->ineq)
1310 goto error;
1312 if (extra == 0) {
1313 bmap->block2 = isl_blk_empty();
1314 bmap->div = NULL;
1315 } else {
1316 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1317 if (isl_blk_is_error(bmap->block2))
1318 goto error;
1320 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1321 if (!bmap->div)
1322 goto error;
1325 for (i = 0; i < n_ineq + n_eq; ++i)
1326 bmap->ineq[i] = bmap->block.data + i * row_size;
1328 for (i = 0; i < extra; ++i)
1329 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1331 bmap->ref = 1;
1332 bmap->flags = 0;
1333 bmap->c_size = n_eq + n_ineq;
1334 bmap->eq = bmap->ineq + n_ineq;
1335 bmap->extra = extra;
1336 bmap->n_eq = 0;
1337 bmap->n_ineq = 0;
1338 bmap->n_div = 0;
1339 bmap->sample = NULL;
1341 return bmap;
1342 error:
1343 isl_basic_map_free(bmap);
1344 return NULL;
1347 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1348 unsigned nparam, unsigned dim, unsigned extra,
1349 unsigned n_eq, unsigned n_ineq)
1351 struct isl_basic_map *bmap;
1352 isl_space *space;
1354 space = isl_space_set_alloc(ctx, nparam, dim);
1355 if (!space)
1356 return NULL;
1358 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1359 return bset_from_bmap(bmap);
1362 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1363 unsigned extra, unsigned n_eq, unsigned n_ineq)
1365 struct isl_basic_map *bmap;
1366 if (!space)
1367 return NULL;
1368 isl_assert(space->ctx, space->n_in == 0, goto error);
1369 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1370 return bset_from_bmap(bmap);
1371 error:
1372 isl_space_free(space);
1373 return NULL;
1376 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1377 unsigned extra, unsigned n_eq, unsigned n_ineq)
1379 struct isl_basic_map *bmap;
1381 if (!space)
1382 return NULL;
1383 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1384 if (!bmap)
1385 goto error;
1386 bmap->dim = space;
1388 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1389 error:
1390 isl_space_free(space);
1391 return NULL;
1394 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1395 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1396 unsigned n_eq, unsigned n_ineq)
1398 struct isl_basic_map *bmap;
1399 isl_space *space;
1401 space = isl_space_alloc(ctx, nparam, in, out);
1402 if (!space)
1403 return NULL;
1405 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1406 return bmap;
1409 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1410 __isl_keep isl_basic_map *src)
1412 int i;
1413 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1415 if (!dst || total < 0)
1416 return isl_basic_map_free(dst);
1418 for (i = 0; i < src->n_eq; ++i) {
1419 int j = isl_basic_map_alloc_equality(dst);
1420 if (j < 0)
1421 return isl_basic_map_free(dst);
1422 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1425 for (i = 0; i < src->n_ineq; ++i) {
1426 int j = isl_basic_map_alloc_inequality(dst);
1427 if (j < 0)
1428 return isl_basic_map_free(dst);
1429 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1432 for (i = 0; i < src->n_div; ++i) {
1433 int j = isl_basic_map_alloc_div(dst);
1434 if (j < 0)
1435 return isl_basic_map_free(dst);
1436 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1438 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1439 return dst;
1442 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1444 struct isl_basic_map *dup;
1446 if (!bmap)
1447 return NULL;
1448 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1449 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1450 dup = dup_constraints(dup, bmap);
1451 if (!dup)
1452 return NULL;
1453 dup->flags = bmap->flags;
1454 dup->sample = isl_vec_copy(bmap->sample);
1455 return dup;
1458 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1460 struct isl_basic_map *dup;
1462 dup = isl_basic_map_dup(bset_to_bmap(bset));
1463 return bset_from_bmap(dup);
1466 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1468 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1471 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1473 if (!set)
1474 return NULL;
1476 set->ref++;
1477 return set;
1480 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1482 if (!bmap)
1483 return NULL;
1485 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1486 bmap->ref++;
1487 return bmap;
1489 bmap = isl_basic_map_dup(bmap);
1490 if (bmap)
1491 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1492 return bmap;
1495 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1497 if (!map)
1498 return NULL;
1500 map->ref++;
1501 return map;
1504 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1506 if (!bmap)
1507 return NULL;
1509 if (--bmap->ref > 0)
1510 return NULL;
1512 isl_ctx_deref(bmap->ctx);
1513 free(bmap->div);
1514 isl_blk_free(bmap->ctx, bmap->block2);
1515 free(bmap->ineq);
1516 isl_blk_free(bmap->ctx, bmap->block);
1517 isl_vec_free(bmap->sample);
1518 isl_space_free(bmap->dim);
1519 free(bmap);
1521 return NULL;
1524 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1526 return isl_basic_map_free(bset_to_bmap(bset));
1529 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1531 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1534 /* Check that "bset" does not involve any parameters.
1536 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1538 isl_size nparam;
1540 nparam = isl_basic_set_dim(bset, isl_dim_param);
1541 if (nparam < 0)
1542 return isl_stat_error;
1543 if (nparam != 0)
1544 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1545 "basic set should not have any parameters",
1546 return isl_stat_error);
1547 return isl_stat_ok;
1550 /* Check that "bset" does not involve any local variables.
1552 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1554 isl_size n_div;
1556 n_div = isl_basic_set_dim(bset, isl_dim_div);
1557 if (n_div < 0)
1558 return isl_stat_error;
1559 if (n_div != 0)
1560 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1561 "basic set should not have any local variables",
1562 return isl_stat_error);
1563 return isl_stat_ok;
1566 #undef TYPE
1567 #define TYPE isl_map
1569 #include "isl_check_named_params_templ.c"
1571 #undef TYPE
1572 #define TYPE isl_basic_map
1574 static
1575 #include "isl_check_named_params_templ.c"
1577 /* Check that "bmap1" and "bmap2" have the same parameters,
1578 * reporting an error if they do not.
1580 static isl_stat isl_basic_map_check_equal_params(
1581 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1583 isl_bool match;
1585 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1586 if (match < 0)
1587 return isl_stat_error;
1588 if (!match)
1589 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1590 "parameters don't match", return isl_stat_error);
1591 return isl_stat_ok;
1594 #undef TYPE
1595 #define TYPE isl_map
1597 #include "isl_align_params_bin_templ.c"
1599 #undef SUFFIX
1600 #define SUFFIX set
1601 #undef ARG1
1602 #define ARG1 isl_map
1603 #undef ARG2
1604 #define ARG2 isl_set
1606 #include "isl_align_params_templ.c"
1608 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1609 __isl_keep isl_map *map2,
1610 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1612 isl_bool r;
1614 if (!map1 || !map2)
1615 return isl_bool_error;
1616 if (isl_map_has_equal_params(map1, map2))
1617 return fn(map1, map2);
1618 if (isl_map_check_named_params(map1) < 0)
1619 return isl_bool_error;
1620 if (isl_map_check_named_params(map2) < 0)
1621 return isl_bool_error;
1622 map1 = isl_map_copy(map1);
1623 map2 = isl_map_copy(map2);
1624 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1625 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1626 r = fn(map1, map2);
1627 isl_map_free(map1);
1628 isl_map_free(map2);
1629 return r;
1632 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1634 isl_size total;
1635 struct isl_ctx *ctx;
1637 total = isl_basic_map_dim(bmap, isl_dim_all);
1638 if (total < 0)
1639 return -1;
1640 ctx = bmap->ctx;
1641 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1642 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1643 return -1);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1646 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1647 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1648 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1649 isl_int *t;
1650 int j = isl_basic_map_alloc_inequality(bmap);
1651 if (j < 0)
1652 return -1;
1653 t = bmap->ineq[j];
1654 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1655 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1656 bmap->eq[-1] = t;
1657 bmap->n_eq++;
1658 bmap->n_ineq--;
1659 bmap->eq--;
1660 return 0;
1662 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1663 bmap->extra - bmap->n_div);
1664 return bmap->n_eq++;
1667 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1669 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1672 __isl_give isl_basic_map *isl_basic_map_free_equality(
1673 __isl_take isl_basic_map *bmap, unsigned n)
1675 if (!bmap)
1676 return NULL;
1677 if (n > bmap->n_eq)
1678 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1679 "invalid number of equalities",
1680 isl_basic_map_free(bmap));
1681 bmap->n_eq -= n;
1682 return bmap;
1685 __isl_give isl_basic_set *isl_basic_set_free_equality(
1686 __isl_take isl_basic_set *bset, unsigned n)
1688 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1689 n));
1692 /* Drop the equality constraint at position "pos",
1693 * preserving the order of the other equality constraints.
1695 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1697 isl_int *t;
1698 int r;
1700 if (!bmap)
1701 return -1;
1702 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1704 t = bmap->eq[pos];
1705 bmap->n_eq--;
1706 for (r = pos; r < bmap->n_eq; ++r)
1707 bmap->eq[r] = bmap->eq[r + 1];
1708 bmap->eq[bmap->n_eq] = t;
1710 return 0;
1713 /* Turn inequality "pos" of "bmap" into an equality.
1715 * In particular, we move the inequality in front of the equalities
1716 * and move the last inequality in the position of the moved inequality.
1717 * Note that isl_tab_make_equalities_explicit depends on this particular
1718 * change in the ordering of the constraints.
1720 void isl_basic_map_inequality_to_equality(
1721 __isl_keep isl_basic_map *bmap, unsigned pos)
1723 isl_int *t;
1725 t = bmap->ineq[pos];
1726 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1727 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1728 bmap->eq[-1] = t;
1729 bmap->n_eq++;
1730 bmap->n_ineq--;
1731 bmap->eq--;
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1734 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1735 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1738 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1740 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1743 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1745 isl_size total;
1746 struct isl_ctx *ctx;
1748 total = isl_basic_map_dim(bmap, isl_dim_all);
1749 if (total < 0)
1750 return -1;
1751 ctx = bmap->ctx;
1752 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1755 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1756 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1757 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1758 bmap->extra - bmap->n_div);
1759 return bmap->n_ineq++;
1762 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1764 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1767 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1768 __isl_take isl_basic_map *bmap, unsigned n)
1770 if (!bmap)
1771 return NULL;
1772 if (n > bmap->n_ineq)
1773 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1774 "invalid number of inequalities",
1775 return isl_basic_map_free(bmap));
1776 bmap->n_ineq -= n;
1777 return bmap;
1780 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1781 __isl_take isl_basic_set *bset, unsigned n)
1783 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1784 n));
1787 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1789 isl_int *t;
1790 if (!bmap)
1791 return -1;
1792 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1794 if (pos != bmap->n_ineq - 1) {
1795 t = bmap->ineq[pos];
1796 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1797 bmap->ineq[bmap->n_ineq - 1] = t;
1798 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1800 bmap->n_ineq--;
1801 return 0;
1804 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1806 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1809 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1810 isl_int *eq)
1812 isl_bool empty;
1813 isl_size total;
1814 int k;
1816 empty = isl_basic_map_plain_is_empty(bmap);
1817 if (empty < 0)
1818 return isl_basic_map_free(bmap);
1819 if (empty)
1820 return bmap;
1822 bmap = isl_basic_map_cow(bmap);
1823 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1824 total = isl_basic_map_dim(bmap, isl_dim_all);
1825 if (total < 0)
1826 return isl_basic_map_free(bmap);
1827 k = isl_basic_map_alloc_equality(bmap);
1828 if (k < 0)
1829 goto error;
1830 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1831 return bmap;
1832 error:
1833 isl_basic_map_free(bmap);
1834 return NULL;
1837 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1838 isl_int *eq)
1840 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1843 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1844 isl_int *ineq)
1846 isl_size total;
1847 int k;
1849 bmap = isl_basic_map_cow(bmap);
1850 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1851 total = isl_basic_map_dim(bmap, isl_dim_all);
1852 if (total < 0)
1853 return isl_basic_map_free(bmap);
1854 k = isl_basic_map_alloc_inequality(bmap);
1855 if (k < 0)
1856 goto error;
1857 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1858 return bmap;
1859 error:
1860 isl_basic_map_free(bmap);
1861 return NULL;
1864 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1865 isl_int *ineq)
1867 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1870 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1872 isl_size total;
1874 total = isl_basic_map_dim(bmap, isl_dim_all);
1875 if (total < 0)
1876 return -1;
1877 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1878 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1879 bmap->extra - bmap->n_div);
1880 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1881 return bmap->n_div++;
1884 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1886 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1889 #undef TYPE
1890 #define TYPE isl_basic_map
1891 #include "check_type_range_templ.c"
1893 /* Check that there are "n" dimensions of type "type" starting at "first"
1894 * in "bset".
1896 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1897 enum isl_dim_type type, unsigned first, unsigned n)
1899 return isl_basic_map_check_range(bset_to_bmap(bset),
1900 type, first, n);
1903 /* Insert an extra integer division, prescribed by "div", to "bmap"
1904 * at (integer division) position "pos".
1906 * The integer division is first added at the end and then moved
1907 * into the right position.
1909 __isl_give isl_basic_map *isl_basic_map_insert_div(
1910 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1912 int i, k;
1913 isl_size total;
1915 bmap = isl_basic_map_cow(bmap);
1916 total = isl_basic_map_dim(bmap, isl_dim_all);
1917 if (total < 0 || !div)
1918 return isl_basic_map_free(bmap);
1920 if (div->size != 1 + 1 + total)
1921 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1922 "unexpected size", return isl_basic_map_free(bmap));
1923 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1924 return isl_basic_map_free(bmap);
1926 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1927 k = isl_basic_map_alloc_div(bmap);
1928 if (k < 0)
1929 return isl_basic_map_free(bmap);
1930 isl_seq_cpy(bmap->div[k], div->el, div->size);
1931 isl_int_set_si(bmap->div[k][div->size], 0);
1933 for (i = k; i > pos; --i)
1934 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1936 return bmap;
1939 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1941 if (!bmap)
1942 return isl_stat_error;
1943 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1944 bmap->n_div -= n;
1945 return isl_stat_ok;
1948 static __isl_give isl_basic_map *add_constraints(
1949 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1950 unsigned i_pos, unsigned o_pos)
1952 isl_size total, n_param, n_in, n_out, n_div;
1953 unsigned o_in, o_out;
1954 isl_ctx *ctx;
1955 isl_space *space;
1956 struct isl_dim_map *dim_map;
1958 space = isl_basic_map_peek_space(bmap2);
1959 if (!bmap1 || !space)
1960 goto error;
1962 total = isl_basic_map_dim(bmap1, isl_dim_all);
1963 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1964 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1965 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1966 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1967 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1968 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1969 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1970 goto error;
1971 ctx = isl_basic_map_get_ctx(bmap1);
1972 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1974 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1975 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1976 isl_dim_map_div(dim_map, bmap2, total);
1978 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1979 error:
1980 isl_basic_map_free(bmap1);
1981 isl_basic_map_free(bmap2);
1982 return NULL;
1985 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1986 unsigned extra, unsigned n_eq, unsigned n_ineq)
1988 isl_space *space;
1989 struct isl_basic_map *ext;
1990 unsigned flags;
1991 int dims_ok;
1993 if (!base)
1994 goto error;
1996 dims_ok = base->extra >= base->n_div + extra;
1998 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1999 room_for_ineq(base, n_ineq))
2000 return base;
2002 extra += base->extra;
2003 n_eq += base->n_eq;
2004 n_ineq += base->n_ineq;
2006 space = isl_basic_map_get_space(base);
2007 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2008 if (!ext)
2009 goto error;
2011 if (dims_ok)
2012 ext->sample = isl_vec_copy(base->sample);
2013 flags = base->flags;
2014 ext = add_constraints(ext, base, 0, 0);
2015 if (ext) {
2016 ext->flags = flags;
2017 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2020 return ext;
2022 error:
2023 isl_basic_map_free(base);
2024 return NULL;
2027 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2028 unsigned extra, unsigned n_eq, unsigned n_ineq)
2030 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2031 extra, n_eq, n_ineq));
2034 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2035 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2037 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2040 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2041 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2043 isl_basic_map *bmap = bset_to_bmap(base);
2044 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2045 return bset_from_bmap(bmap);
2048 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2050 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2053 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2055 if (!bmap)
2056 return NULL;
2058 if (bmap->ref > 1) {
2059 bmap->ref--;
2060 bmap = isl_basic_map_dup(bmap);
2062 if (bmap) {
2063 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2064 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2066 return bmap;
2069 /* Clear all cached information in "map", either because it is about
2070 * to be modified or because it is being freed.
2071 * Always return the same pointer that is passed in.
2072 * This is needed for the use in isl_map_free.
2074 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2076 isl_basic_map_free(map->cached_simple_hull[0]);
2077 isl_basic_map_free(map->cached_simple_hull[1]);
2078 map->cached_simple_hull[0] = NULL;
2079 map->cached_simple_hull[1] = NULL;
2080 return map;
2083 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2085 return isl_map_cow(set);
2088 /* Return an isl_map that is equal to "map" and that has only
2089 * a single reference.
2091 * If the original input already has only one reference, then
2092 * simply return it, but clear all cached information, since
2093 * it may be rendered invalid by the operations that will be
2094 * performed on the result.
2096 * Otherwise, create a duplicate (without any cached information).
2098 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2100 if (!map)
2101 return NULL;
2103 if (map->ref == 1)
2104 return clear_caches(map);
2105 map->ref--;
2106 return isl_map_dup(map);
2109 static void swap_vars(struct isl_blk blk, isl_int *a,
2110 unsigned a_len, unsigned b_len)
2112 isl_seq_cpy(blk.data, a+a_len, b_len);
2113 isl_seq_cpy(blk.data+b_len, a, a_len);
2114 isl_seq_cpy(a, blk.data, b_len+a_len);
2117 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2118 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2120 int i;
2121 struct isl_blk blk;
2123 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2124 goto error;
2126 if (n1 == 0 || n2 == 0)
2127 return bmap;
2129 bmap = isl_basic_map_cow(bmap);
2130 if (!bmap)
2131 return NULL;
2133 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2134 if (isl_blk_is_error(blk))
2135 goto error;
2137 for (i = 0; i < bmap->n_eq; ++i)
2138 swap_vars(blk,
2139 bmap->eq[i] + pos, n1, n2);
2141 for (i = 0; i < bmap->n_ineq; ++i)
2142 swap_vars(blk,
2143 bmap->ineq[i] + pos, n1, n2);
2145 for (i = 0; i < bmap->n_div; ++i)
2146 swap_vars(blk,
2147 bmap->div[i]+1 + pos, n1, n2);
2149 isl_blk_free(bmap->ctx, blk);
2151 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2152 bmap = isl_basic_map_gauss(bmap, NULL);
2153 return isl_basic_map_finalize(bmap);
2154 error:
2155 isl_basic_map_free(bmap);
2156 return NULL;
2159 /* The given basic map has turned out to be empty.
2160 * Explicitly mark it as such and change the representation
2161 * to a canonical representation of the empty basic map.
2162 * Since the basic map has conflicting constraints,
2163 * it must have at least one constraint, except perhaps
2164 * if it was already explicitly marked as being empty.
2165 * Do nothing in the latter case, i.e., if it has been marked empty and
2166 * has no constraints.
2168 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2169 __isl_take isl_basic_map *bmap)
2171 int i = 0;
2172 isl_bool empty;
2173 isl_size n;
2174 isl_size total;
2176 n = isl_basic_map_n_constraint(bmap);
2177 empty = isl_basic_map_plain_is_empty(bmap);
2178 if (n < 0 || empty < 0)
2179 return isl_basic_map_free(bmap);
2180 if (n == 0 && empty)
2181 return bmap;
2182 total = isl_basic_map_dim(bmap, isl_dim_all);
2183 if (total < 0)
2184 return isl_basic_map_free(bmap);
2185 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2186 return isl_basic_map_free(bmap);
2187 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2188 if (!bmap)
2189 return NULL;
2190 if (bmap->n_eq > 0) {
2191 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2192 if (!bmap)
2193 return NULL;
2194 } else {
2195 i = isl_basic_map_alloc_equality(bmap);
2196 if (i < 0)
2197 goto error;
2199 isl_int_set_si(bmap->eq[i][0], 1);
2200 isl_seq_clr(bmap->eq[i]+1, total);
2201 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2202 isl_vec_free(bmap->sample);
2203 bmap->sample = NULL;
2204 return isl_basic_map_finalize(bmap);
2205 error:
2206 isl_basic_map_free(bmap);
2207 return NULL;
2210 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2211 __isl_take isl_basic_set *bset)
2213 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2216 __isl_give isl_basic_map *isl_basic_map_set_rational(
2217 __isl_take isl_basic_map *bmap)
2219 if (!bmap)
2220 return NULL;
2222 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2223 return bmap;
2225 bmap = isl_basic_map_cow(bmap);
2226 if (!bmap)
2227 return NULL;
2229 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2231 return isl_basic_map_finalize(bmap);
2234 __isl_give isl_basic_set *isl_basic_set_set_rational(
2235 __isl_take isl_basic_set *bset)
2237 return isl_basic_map_set_rational(bset);
2240 __isl_give isl_basic_set *isl_basic_set_set_integral(
2241 __isl_take isl_basic_set *bset)
2243 if (!bset)
2244 return NULL;
2246 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2247 return bset;
2249 bset = isl_basic_set_cow(bset);
2250 if (!bset)
2251 return NULL;
2253 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2255 return isl_basic_set_finalize(bset);
2258 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2260 int i;
2262 map = isl_map_cow(map);
2263 if (!map)
2264 return NULL;
2265 for (i = 0; i < map->n; ++i) {
2266 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2267 if (!map->p[i])
2268 goto error;
2270 return map;
2271 error:
2272 isl_map_free(map);
2273 return NULL;
2276 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2278 return isl_map_set_rational(set);
2281 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2282 * of "bmap").
2284 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2286 isl_int *t = bmap->div[a];
2287 bmap->div[a] = bmap->div[b];
2288 bmap->div[b] = t;
2291 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2292 * div definitions accordingly.
2294 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2295 int a, int b)
2297 int i;
2298 isl_size off;
2300 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2301 if (off < 0)
2302 return isl_basic_map_free(bmap);
2304 swap_div(bmap, a, b);
2306 for (i = 0; i < bmap->n_eq; ++i)
2307 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2309 for (i = 0; i < bmap->n_ineq; ++i)
2310 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2312 for (i = 0; i < bmap->n_div; ++i)
2313 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2314 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2316 return bmap;
2319 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2321 isl_seq_cpy(c, c + n, rem);
2322 isl_seq_clr(c + rem, n);
2325 /* Drop n dimensions starting at first.
2327 * In principle, this frees up some extra variables as the number
2328 * of columns remains constant, but we would have to extend
2329 * the div array too as the number of rows in this array is assumed
2330 * to be equal to extra.
2332 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2333 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2335 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2338 /* Move "n" divs starting at "first" to the end of the list of divs.
2340 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2341 unsigned first, unsigned n)
2343 isl_int **div;
2344 int i;
2346 if (first + n == bmap->n_div)
2347 return bmap;
2349 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2350 if (!div)
2351 goto error;
2352 for (i = 0; i < n; ++i)
2353 div[i] = bmap->div[first + i];
2354 for (i = 0; i < bmap->n_div - first - n; ++i)
2355 bmap->div[first + i] = bmap->div[first + n + i];
2356 for (i = 0; i < n; ++i)
2357 bmap->div[bmap->n_div - n + i] = div[i];
2358 free(div);
2359 return bmap;
2360 error:
2361 isl_basic_map_free(bmap);
2362 return NULL;
2365 #undef TYPE
2366 #define TYPE isl_map
2367 static
2368 #include "check_type_range_templ.c"
2370 /* Check that there are "n" dimensions of type "type" starting at "first"
2371 * in "set".
2373 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2374 enum isl_dim_type type, unsigned first, unsigned n)
2376 return isl_map_check_range(set_to_map(set), type, first, n);
2379 /* Drop "n" dimensions of type "type" starting at "first".
2380 * Perform the core computation, without cowing or
2381 * simplifying and finalizing the result.
2383 * In principle, this frees up some extra variables as the number
2384 * of columns remains constant, but we would have to extend
2385 * the div array too as the number of rows in this array is assumed
2386 * to be equal to extra.
2388 __isl_give isl_basic_map *isl_basic_map_drop_core(
2389 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2390 unsigned first, unsigned n)
2392 int i;
2393 unsigned offset;
2394 unsigned left;
2395 isl_size total;
2397 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2398 return isl_basic_map_free(bmap);
2400 total = isl_basic_map_dim(bmap, isl_dim_all);
2401 if (total < 0)
2402 return isl_basic_map_free(bmap);
2404 offset = isl_basic_map_offset(bmap, type) + first;
2405 left = total - (offset - 1) - n;
2406 for (i = 0; i < bmap->n_eq; ++i)
2407 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2409 for (i = 0; i < bmap->n_ineq; ++i)
2410 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2412 for (i = 0; i < bmap->n_div; ++i)
2413 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2415 if (type == isl_dim_div) {
2416 bmap = move_divs_last(bmap, first, n);
2417 if (!bmap)
2418 return NULL;
2419 if (isl_basic_map_free_div(bmap, n) < 0)
2420 return isl_basic_map_free(bmap);
2421 } else
2422 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2423 if (!bmap->dim)
2424 return isl_basic_map_free(bmap);
2426 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2427 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2428 return bmap;
2431 /* Drop "n" dimensions of type "type" starting at "first".
2433 * In principle, this frees up some extra variables as the number
2434 * of columns remains constant, but we would have to extend
2435 * the div array too as the number of rows in this array is assumed
2436 * to be equal to extra.
2438 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2439 enum isl_dim_type type, unsigned first, unsigned n)
2441 if (!bmap)
2442 return NULL;
2443 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2444 return bmap;
2446 bmap = isl_basic_map_cow(bmap);
2447 if (!bmap)
2448 return NULL;
2450 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2452 bmap = isl_basic_map_simplify(bmap);
2453 return isl_basic_map_finalize(bmap);
2456 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2457 enum isl_dim_type type, unsigned first, unsigned n)
2459 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2460 type, first, n));
2463 /* No longer consider "map" to be normalized.
2465 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2467 if (!map)
2468 return NULL;
2469 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2470 return map;
2473 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2474 enum isl_dim_type type, unsigned first, unsigned n)
2476 int i;
2477 isl_space *space;
2479 if (isl_map_check_range(map, type, first, n) < 0)
2480 return isl_map_free(map);
2482 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2483 return map;
2484 map = isl_map_cow(map);
2485 if (!map)
2486 goto error;
2488 for (i = 0; i < map->n; ++i) {
2489 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2490 if (!map->p[i])
2491 goto error;
2493 map = isl_map_unmark_normalized(map);
2495 space = isl_map_take_space(map);
2496 space = isl_space_drop_dims(space, type, first, n);
2497 map = isl_map_restore_space(map, space);
2499 return map;
2500 error:
2501 isl_map_free(map);
2502 return NULL;
2505 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2506 enum isl_dim_type type, unsigned first, unsigned n)
2508 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2511 /* Drop the integer division at position "div", which is assumed
2512 * not to appear in any of the constraints or
2513 * in any of the other integer divisions.
2515 * Since the integer division is redundant, there is no need to cow.
2517 __isl_give isl_basic_map *isl_basic_map_drop_div(
2518 __isl_take isl_basic_map *bmap, unsigned div)
2520 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2523 /* Eliminate the specified n dimensions starting at first from the
2524 * constraints, without removing the dimensions from the space.
2525 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2527 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2528 enum isl_dim_type type, unsigned first, unsigned n)
2530 int i;
2532 if (n == 0)
2533 return map;
2535 if (isl_map_check_range(map, type, first, n) < 0)
2536 return isl_map_free(map);
2538 map = isl_map_cow(map);
2539 if (!map)
2540 return NULL;
2542 for (i = 0; i < map->n; ++i) {
2543 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2544 if (!map->p[i])
2545 goto error;
2547 return map;
2548 error:
2549 isl_map_free(map);
2550 return NULL;
2553 /* Eliminate the specified n dimensions starting at first from the
2554 * constraints, without removing the dimensions from the space.
2555 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2557 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2558 enum isl_dim_type type, unsigned first, unsigned n)
2560 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2563 /* Eliminate the specified n dimensions starting at first from the
2564 * constraints, without removing the dimensions from the space.
2565 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2567 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2568 unsigned first, unsigned n)
2570 return isl_set_eliminate(set, isl_dim_set, first, n);
2573 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2574 __isl_take isl_basic_map *bmap)
2576 isl_size v_div;
2578 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2579 if (v_div < 0)
2580 return isl_basic_map_free(bmap);
2581 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2582 if (!bmap)
2583 return NULL;
2584 bmap->n_div = 0;
2585 return isl_basic_map_finalize(bmap);
2588 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2589 __isl_take isl_basic_set *bset)
2591 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2594 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2596 int i;
2598 if (!map)
2599 return NULL;
2600 if (map->n == 0)
2601 return map;
2603 map = isl_map_cow(map);
2604 if (!map)
2605 return NULL;
2607 for (i = 0; i < map->n; ++i) {
2608 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2609 if (!map->p[i])
2610 goto error;
2612 return map;
2613 error:
2614 isl_map_free(map);
2615 return NULL;
2618 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2620 return isl_map_remove_divs(set);
2623 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2624 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2625 unsigned first, unsigned n)
2627 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2628 return isl_basic_map_free(bmap);
2629 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2630 return bmap;
2631 bmap = isl_basic_map_eliminate_vars(bmap,
2632 isl_basic_map_offset(bmap, type) - 1 + first, n);
2633 if (!bmap)
2634 return bmap;
2635 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2636 return bmap;
2637 bmap = isl_basic_map_drop(bmap, type, first, n);
2638 return bmap;
2641 /* Return true if the definition of the given div (recursively) involves
2642 * any of the given variables.
2644 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2645 unsigned first, unsigned n)
2647 int i;
2648 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2650 if (isl_int_is_zero(bmap->div[div][0]))
2651 return isl_bool_false;
2652 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2653 return isl_bool_true;
2655 for (i = bmap->n_div - 1; i >= 0; --i) {
2656 isl_bool involves;
2658 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2659 continue;
2660 involves = div_involves_vars(bmap, i, first, n);
2661 if (involves < 0 || involves)
2662 return involves;
2665 return isl_bool_false;
2668 /* Try and add a lower and/or upper bound on "div" to "bmap"
2669 * based on inequality "i".
2670 * "total" is the total number of variables (excluding the divs).
2671 * "v" is a temporary object that can be used during the calculations.
2672 * If "lb" is set, then a lower bound should be constructed.
2673 * If "ub" is set, then an upper bound should be constructed.
2675 * The calling function has already checked that the inequality does not
2676 * reference "div", but we still need to check that the inequality is
2677 * of the right form. We'll consider the case where we want to construct
2678 * a lower bound. The construction of upper bounds is similar.
2680 * Let "div" be of the form
2682 * q = floor((a + f(x))/d)
2684 * We essentially check if constraint "i" is of the form
2686 * b + f(x) >= 0
2688 * so that we can use it to derive a lower bound on "div".
2689 * However, we allow a slightly more general form
2691 * b + g(x) >= 0
2693 * with the condition that the coefficients of g(x) - f(x) are all
2694 * divisible by d.
2695 * Rewriting this constraint as
2697 * 0 >= -b - g(x)
2699 * adding a + f(x) to both sides and dividing by d, we obtain
2701 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2703 * Taking the floor on both sides, we obtain
2705 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2707 * or
2709 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2711 * In the case of an upper bound, we construct the constraint
2713 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2716 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2717 __isl_take isl_basic_map *bmap, int div, int i,
2718 unsigned total, isl_int v, int lb, int ub)
2720 int j;
2722 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2723 if (lb) {
2724 isl_int_sub(v, bmap->ineq[i][1 + j],
2725 bmap->div[div][1 + 1 + j]);
2726 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2728 if (ub) {
2729 isl_int_add(v, bmap->ineq[i][1 + j],
2730 bmap->div[div][1 + 1 + j]);
2731 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2734 if (!lb && !ub)
2735 return bmap;
2737 bmap = isl_basic_map_cow(bmap);
2738 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2739 if (lb) {
2740 int k = isl_basic_map_alloc_inequality(bmap);
2741 if (k < 0)
2742 goto error;
2743 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2744 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2745 bmap->div[div][1 + j]);
2746 isl_int_cdiv_q(bmap->ineq[k][j],
2747 bmap->ineq[k][j], bmap->div[div][0]);
2749 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2751 if (ub) {
2752 int k = isl_basic_map_alloc_inequality(bmap);
2753 if (k < 0)
2754 goto error;
2755 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2756 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2757 bmap->div[div][1 + j]);
2758 isl_int_fdiv_q(bmap->ineq[k][j],
2759 bmap->ineq[k][j], bmap->div[div][0]);
2761 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2764 return bmap;
2765 error:
2766 isl_basic_map_free(bmap);
2767 return NULL;
2770 /* This function is called right before "div" is eliminated from "bmap"
2771 * using Fourier-Motzkin.
2772 * Look through the constraints of "bmap" for constraints on the argument
2773 * of the integer division and use them to construct constraints on the
2774 * integer division itself. These constraints can then be combined
2775 * during the Fourier-Motzkin elimination.
2776 * Note that it is only useful to introduce lower bounds on "div"
2777 * if "bmap" already contains upper bounds on "div" as the newly
2778 * introduce lower bounds can then be combined with the pre-existing
2779 * upper bounds. Similarly for upper bounds.
2780 * We therefore first check if "bmap" contains any lower and/or upper bounds
2781 * on "div".
2783 * It is interesting to note that the introduction of these constraints
2784 * can indeed lead to more accurate results, even when compared to
2785 * deriving constraints on the argument of "div" from constraints on "div".
2786 * Consider, for example, the set
2788 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2790 * The second constraint can be rewritten as
2792 * 2 * [(-i-2j+3)/4] + k >= 0
2794 * from which we can derive
2796 * -i - 2j + 3 >= -2k
2798 * or
2800 * i + 2j <= 3 + 2k
2802 * Combined with the first constraint, we obtain
2804 * -3 <= 3 + 2k or k >= -3
2806 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2807 * the first constraint, we obtain
2809 * [(i + 2j)/4] >= [-3/4] = -1
2811 * Combining this constraint with the second constraint, we obtain
2813 * k >= -2
2815 static __isl_give isl_basic_map *insert_bounds_on_div(
2816 __isl_take isl_basic_map *bmap, int div)
2818 int i;
2819 int check_lb, check_ub;
2820 isl_int v;
2821 isl_size v_div;
2823 if (!bmap)
2824 return NULL;
2826 if (isl_int_is_zero(bmap->div[div][0]))
2827 return bmap;
2829 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2830 if (v_div < 0)
2831 return isl_basic_map_free(bmap);
2833 check_lb = 0;
2834 check_ub = 0;
2835 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2836 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2837 if (s > 0)
2838 check_ub = 1;
2839 if (s < 0)
2840 check_lb = 1;
2843 if (!check_lb && !check_ub)
2844 return bmap;
2846 isl_int_init(v);
2848 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2849 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2850 continue;
2852 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2853 check_lb, check_ub);
2856 isl_int_clear(v);
2858 return bmap;
2861 /* Remove all divs (recursively) involving any of the given dimensions
2862 * in their definitions.
2864 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2865 __isl_take isl_basic_map *bmap,
2866 enum isl_dim_type type, unsigned first, unsigned n)
2868 int i;
2870 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2871 return isl_basic_map_free(bmap);
2872 first += isl_basic_map_offset(bmap, type);
2874 for (i = bmap->n_div - 1; i >= 0; --i) {
2875 isl_bool involves;
2877 involves = div_involves_vars(bmap, i, first, n);
2878 if (involves < 0)
2879 return isl_basic_map_free(bmap);
2880 if (!involves)
2881 continue;
2882 bmap = insert_bounds_on_div(bmap, i);
2883 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2884 if (!bmap)
2885 return NULL;
2886 i = bmap->n_div;
2889 return bmap;
2892 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2893 __isl_take isl_basic_set *bset,
2894 enum isl_dim_type type, unsigned first, unsigned n)
2896 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2899 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2900 enum isl_dim_type type, unsigned first, unsigned n)
2902 int i;
2904 if (!map)
2905 return NULL;
2906 if (map->n == 0)
2907 return map;
2909 map = isl_map_cow(map);
2910 if (!map)
2911 return NULL;
2913 for (i = 0; i < map->n; ++i) {
2914 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2915 type, first, n);
2916 if (!map->p[i])
2917 goto error;
2919 return map;
2920 error:
2921 isl_map_free(map);
2922 return NULL;
2925 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2926 enum isl_dim_type type, unsigned first, unsigned n)
2928 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2929 type, first, n));
2932 /* Does the description of "bmap" depend on the specified dimensions?
2933 * We also check whether the dimensions appear in any of the div definitions.
2934 * In principle there is no need for this check. If the dimensions appear
2935 * in a div definition, they also appear in the defining constraints of that
2936 * div.
2938 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2939 enum isl_dim_type type, unsigned first, unsigned n)
2941 int i;
2943 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2944 return isl_bool_error;
2946 first += isl_basic_map_offset(bmap, type);
2947 for (i = 0; i < bmap->n_eq; ++i)
2948 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2949 return isl_bool_true;
2950 for (i = 0; i < bmap->n_ineq; ++i)
2951 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2952 return isl_bool_true;
2953 for (i = 0; i < bmap->n_div; ++i) {
2954 if (isl_int_is_zero(bmap->div[i][0]))
2955 continue;
2956 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2957 return isl_bool_true;
2960 return isl_bool_false;
2963 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2964 enum isl_dim_type type, unsigned first, unsigned n)
2966 int i;
2968 if (isl_map_check_range(map, type, first, n) < 0)
2969 return isl_bool_error;
2971 for (i = 0; i < map->n; ++i) {
2972 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2973 type, first, n);
2974 if (involves < 0 || involves)
2975 return involves;
2978 return isl_bool_false;
2981 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2982 enum isl_dim_type type, unsigned first, unsigned n)
2984 return isl_basic_map_involves_dims(bset, type, first, n);
2987 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2988 enum isl_dim_type type, unsigned first, unsigned n)
2990 return isl_map_involves_dims(set, type, first, n);
2993 /* Does "bset" involve any local variables, i.e., integer divisions?
2995 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2997 isl_size n;
2999 n = isl_basic_set_dim(bset, isl_dim_div);
3000 if (n < 0)
3001 return isl_bool_error;
3002 return isl_bool_ok(n > 0);
3005 /* isl_set_every_basic_set callback that checks whether "bset"
3006 * is free of local variables.
3008 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3010 return isl_bool_not(isl_basic_set_involves_locals(bset));
3013 /* Does "set" involve any local variables, i.e., integer divisions?
3015 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3017 isl_bool no_locals;
3019 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3020 return isl_bool_not(no_locals);
3023 /* Drop all constraints in bmap that involve any of the dimensions
3024 * first to first+n-1.
3025 * This function only performs the actual removal of constraints.
3027 * This function should not call finalize since it is used by
3028 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3030 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3031 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3033 int i;
3035 if (n == 0)
3036 return bmap;
3038 bmap = isl_basic_map_cow(bmap);
3040 if (!bmap)
3041 return NULL;
3043 for (i = bmap->n_eq - 1; i >= 0; --i) {
3044 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3045 continue;
3046 if (isl_basic_map_drop_equality(bmap, i) < 0)
3047 return isl_basic_map_free(bmap);
3050 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3051 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3052 continue;
3053 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3054 return isl_basic_map_free(bmap);
3057 return bmap;
3060 /* Drop all constraints in bset that involve any of the dimensions
3061 * first to first+n-1.
3062 * This function only performs the actual removal of constraints.
3064 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3065 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3067 return isl_basic_map_drop_constraints_involving(bset, first, n);
3070 /* Drop all constraints in bmap that do not involve any of the dimensions
3071 * first to first + n - 1 of the given type.
3073 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3074 __isl_take isl_basic_map *bmap,
3075 enum isl_dim_type type, unsigned first, unsigned n)
3077 int i;
3079 if (n == 0) {
3080 isl_space *space = isl_basic_map_get_space(bmap);
3081 isl_basic_map_free(bmap);
3082 return isl_basic_map_universe(space);
3084 bmap = isl_basic_map_cow(bmap);
3085 if (!bmap)
3086 return NULL;
3088 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3089 return isl_basic_map_free(bmap);
3091 first += isl_basic_map_offset(bmap, type) - 1;
3093 for (i = bmap->n_eq - 1; i >= 0; --i) {
3094 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3095 continue;
3096 if (isl_basic_map_drop_equality(bmap, i) < 0)
3097 return isl_basic_map_free(bmap);
3100 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3101 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3102 continue;
3103 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3104 return isl_basic_map_free(bmap);
3107 bmap = isl_basic_map_add_known_div_constraints(bmap);
3108 return bmap;
3111 /* Drop all constraints in bset that do not involve any of the dimensions
3112 * first to first + n - 1 of the given type.
3114 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3115 __isl_take isl_basic_set *bset,
3116 enum isl_dim_type type, unsigned first, unsigned n)
3118 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3119 type, first, n);
3122 /* Drop all constraints in bmap that involve any of the dimensions
3123 * first to first + n - 1 of the given type.
3125 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3126 __isl_take isl_basic_map *bmap,
3127 enum isl_dim_type type, unsigned first, unsigned n)
3129 if (!bmap)
3130 return NULL;
3131 if (n == 0)
3132 return bmap;
3134 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3135 return isl_basic_map_free(bmap);
3137 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3138 first += isl_basic_map_offset(bmap, type) - 1;
3139 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3140 bmap = isl_basic_map_add_known_div_constraints(bmap);
3141 return bmap;
3144 /* Drop all constraints in bset that involve any of the dimensions
3145 * first to first + n - 1 of the given type.
3147 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3148 __isl_take isl_basic_set *bset,
3149 enum isl_dim_type type, unsigned first, unsigned n)
3151 return isl_basic_map_drop_constraints_involving_dims(bset,
3152 type, first, n);
3155 /* Drop constraints from "map" by applying "drop" to each basic map.
3157 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3158 enum isl_dim_type type, unsigned first, unsigned n,
3159 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3160 enum isl_dim_type type, unsigned first, unsigned n))
3162 int i;
3164 if (isl_map_check_range(map, type, first, n) < 0)
3165 return isl_map_free(map);
3167 map = isl_map_cow(map);
3168 if (!map)
3169 return NULL;
3171 for (i = 0; i < map->n; ++i) {
3172 map->p[i] = drop(map->p[i], type, first, n);
3173 if (!map->p[i])
3174 return isl_map_free(map);
3177 if (map->n > 1)
3178 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3180 return map;
3183 /* Drop all constraints in map that involve any of the dimensions
3184 * first to first + n - 1 of the given type.
3186 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3187 __isl_take isl_map *map,
3188 enum isl_dim_type type, unsigned first, unsigned n)
3190 if (n == 0)
3191 return map;
3192 return drop_constraints(map, type, first, n,
3193 &isl_basic_map_drop_constraints_involving_dims);
3196 /* Drop all constraints in "map" that do not involve any of the dimensions
3197 * first to first + n - 1 of the given type.
3199 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3200 __isl_take isl_map *map,
3201 enum isl_dim_type type, unsigned first, unsigned n)
3203 if (n == 0) {
3204 isl_space *space = isl_map_get_space(map);
3205 isl_map_free(map);
3206 return isl_map_universe(space);
3208 return drop_constraints(map, type, first, n,
3209 &isl_basic_map_drop_constraints_not_involving_dims);
3212 /* Drop all constraints in set that involve any of the dimensions
3213 * first to first + n - 1 of the given type.
3215 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3216 __isl_take isl_set *set,
3217 enum isl_dim_type type, unsigned first, unsigned n)
3219 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3222 /* Drop all constraints in "set" that do not involve any of the dimensions
3223 * first to first + n - 1 of the given type.
3225 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3226 __isl_take isl_set *set,
3227 enum isl_dim_type type, unsigned first, unsigned n)
3229 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3232 /* Does local variable "div" of "bmap" have a complete explicit representation?
3233 * Having a complete explicit representation requires not only
3234 * an explicit representation, but also that all local variables
3235 * that appear in this explicit representation in turn have
3236 * a complete explicit representation.
3238 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3240 int i;
3241 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3242 isl_bool marked;
3244 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3245 if (marked < 0 || marked)
3246 return isl_bool_not(marked);
3248 for (i = bmap->n_div - 1; i >= 0; --i) {
3249 isl_bool known;
3251 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3252 continue;
3253 known = isl_basic_map_div_is_known(bmap, i);
3254 if (known < 0 || !known)
3255 return known;
3258 return isl_bool_true;
3261 /* Remove all divs that are unknown or defined in terms of unknown divs.
3263 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3264 __isl_take isl_basic_map *bmap)
3266 int i;
3268 if (!bmap)
3269 return NULL;
3271 for (i = bmap->n_div - 1; i >= 0; --i) {
3272 if (isl_basic_map_div_is_known(bmap, i))
3273 continue;
3274 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3275 if (!bmap)
3276 return NULL;
3277 i = bmap->n_div;
3280 return bmap;
3283 /* Remove all divs that are unknown or defined in terms of unknown divs.
3285 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3286 __isl_take isl_basic_set *bset)
3288 return isl_basic_map_remove_unknown_divs(bset);
3291 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3293 int i;
3295 if (!map)
3296 return NULL;
3297 if (map->n == 0)
3298 return map;
3300 map = isl_map_cow(map);
3301 if (!map)
3302 return NULL;
3304 for (i = 0; i < map->n; ++i) {
3305 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3306 if (!map->p[i])
3307 goto error;
3309 return map;
3310 error:
3311 isl_map_free(map);
3312 return NULL;
3315 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3317 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3320 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3321 __isl_take isl_basic_set *bset,
3322 enum isl_dim_type type, unsigned first, unsigned n)
3324 isl_basic_map *bmap = bset_to_bmap(bset);
3325 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3326 return bset_from_bmap(bmap);
3329 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3330 enum isl_dim_type type, unsigned first, unsigned n)
3332 int i;
3334 if (n == 0)
3335 return map;
3337 map = isl_map_cow(map);
3338 if (isl_map_check_range(map, type, first, n) < 0)
3339 return isl_map_free(map);
3341 for (i = 0; i < map->n; ++i) {
3342 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3343 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3344 if (!map->p[i])
3345 goto error;
3347 map = isl_map_drop(map, type, first, n);
3348 return map;
3349 error:
3350 isl_map_free(map);
3351 return NULL;
3354 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3355 enum isl_dim_type type, unsigned first, unsigned n)
3357 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3358 type, first, n));
3361 /* Project out n inputs starting at first using Fourier-Motzkin */
3362 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3363 unsigned first, unsigned n)
3365 return isl_map_remove_dims(map, isl_dim_in, first, n);
3368 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3369 FILE *out, int indent)
3371 isl_printer *p;
3373 if (!bset) {
3374 fprintf(out, "null basic set\n");
3375 return;
3378 fprintf(out, "%*s", indent, "");
3379 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3380 bset->ref, bset->dim->nparam, bset->dim->n_out,
3381 bset->extra, bset->flags);
3383 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3384 p = isl_printer_set_dump(p, 1);
3385 p = isl_printer_set_indent(p, indent);
3386 p = isl_printer_start_line(p);
3387 p = isl_printer_print_basic_set(p, bset);
3388 p = isl_printer_end_line(p);
3389 isl_printer_free(p);
3392 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3393 FILE *out, int indent)
3395 isl_printer *p;
3397 if (!bmap) {
3398 fprintf(out, "null basic map\n");
3399 return;
3402 fprintf(out, "%*s", indent, "");
3403 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3404 "flags: %x, n_name: %d\n",
3405 bmap->ref,
3406 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3407 bmap->extra, bmap->flags, bmap->dim->n_id);
3409 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3410 p = isl_printer_set_dump(p, 1);
3411 p = isl_printer_set_indent(p, indent);
3412 p = isl_printer_start_line(p);
3413 p = isl_printer_print_basic_map(p, bmap);
3414 p = isl_printer_end_line(p);
3415 isl_printer_free(p);
3418 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3419 unsigned pos)
3421 isl_size total;
3423 total = isl_basic_map_dim(bmap, isl_dim_all);
3424 if (total < 0)
3425 return isl_basic_map_free(bmap);
3426 if (pos >= bmap->n_ineq)
3427 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3428 "invalid position", return isl_basic_map_free(bmap));
3429 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3430 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3431 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3432 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3433 return bmap;
3436 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3437 unsigned flags)
3439 if (isl_space_check_is_set(space) < 0)
3440 goto error;
3441 return isl_map_alloc_space(space, n, flags);
3442 error:
3443 isl_space_free(space);
3444 return NULL;
3447 /* Make sure "map" has room for at least "n" more basic maps.
3449 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3451 int i;
3452 struct isl_map *grown = NULL;
3454 if (!map)
3455 return NULL;
3456 isl_assert(map->ctx, n >= 0, goto error);
3457 if (map->n + n <= map->size)
3458 return map;
3459 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3460 if (!grown)
3461 goto error;
3462 for (i = 0; i < map->n; ++i) {
3463 grown->p[i] = isl_basic_map_copy(map->p[i]);
3464 if (!grown->p[i])
3465 goto error;
3466 grown->n++;
3468 isl_map_free(map);
3469 return grown;
3470 error:
3471 isl_map_free(grown);
3472 isl_map_free(map);
3473 return NULL;
3476 /* Make sure "set" has room for at least "n" more basic sets.
3478 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3480 return set_from_map(isl_map_grow(set_to_map(set), n));
3483 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3485 return isl_map_from_basic_map(bset);
3488 /* This function performs the same operation as isl_set_from_basic_set,
3489 * but is considered as a function on an isl_basic_set when exported.
3491 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3493 return isl_set_from_basic_set(bset);
3496 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3498 struct isl_map *map;
3500 if (!bmap)
3501 return NULL;
3503 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3504 return isl_map_add_basic_map(map, bmap);
3507 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3508 __isl_take isl_basic_set *bset)
3510 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3511 bset_to_bmap(bset)));
3514 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3516 return isl_map_free(set);
3519 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3521 int i;
3523 if (!set) {
3524 fprintf(out, "null set\n");
3525 return;
3528 fprintf(out, "%*s", indent, "");
3529 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3530 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3531 set->flags);
3532 for (i = 0; i < set->n; ++i) {
3533 fprintf(out, "%*s", indent, "");
3534 fprintf(out, "basic set %d:\n", i);
3535 isl_basic_set_print_internal(set->p[i], out, indent+4);
3539 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3541 int i;
3543 if (!map) {
3544 fprintf(out, "null map\n");
3545 return;
3548 fprintf(out, "%*s", indent, "");
3549 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3550 "flags: %x, n_name: %d\n",
3551 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3552 map->dim->n_out, map->flags, map->dim->n_id);
3553 for (i = 0; i < map->n; ++i) {
3554 fprintf(out, "%*s", indent, "");
3555 fprintf(out, "basic map %d:\n", i);
3556 isl_basic_map_print_internal(map->p[i], out, indent+4);
3560 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3562 static isl_stat isl_basic_map_check_compatible_domain(
3563 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3565 isl_bool ok;
3567 ok = isl_basic_map_compatible_domain(bmap, bset);
3568 if (ok < 0)
3569 return isl_stat_error;
3570 if (!ok)
3571 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3572 "incompatible spaces", return isl_stat_error);
3574 return isl_stat_ok;
3577 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3578 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3580 struct isl_basic_map *bmap_domain;
3581 isl_size dim;
3583 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3584 goto error;
3586 dim = isl_basic_set_dim(bset, isl_dim_set);
3587 if (dim < 0)
3588 goto error;
3589 if (dim != 0 &&
3590 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3591 goto error;
3593 bmap = isl_basic_map_cow(bmap);
3594 if (!bmap)
3595 goto error;
3596 bmap = isl_basic_map_extend(bmap,
3597 bset->n_div, bset->n_eq, bset->n_ineq);
3598 bmap_domain = isl_basic_map_from_domain(bset);
3599 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3601 bmap = isl_basic_map_simplify(bmap);
3602 return isl_basic_map_finalize(bmap);
3603 error:
3604 isl_basic_map_free(bmap);
3605 isl_basic_set_free(bset);
3606 return NULL;
3609 /* Check that the space of "bset" is the same as that of the range of "bmap".
3611 static isl_stat isl_basic_map_check_compatible_range(
3612 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3614 isl_bool ok;
3616 ok = isl_basic_map_compatible_range(bmap, bset);
3617 if (ok < 0)
3618 return isl_stat_error;
3619 if (!ok)
3620 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3621 "incompatible spaces", return isl_stat_error);
3623 return isl_stat_ok;
3626 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3627 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3629 struct isl_basic_map *bmap_range;
3630 isl_size dim;
3632 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3633 goto error;
3635 dim = isl_basic_set_dim(bset, isl_dim_set);
3636 if (dim < 0)
3637 goto error;
3638 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3639 goto error;
3641 if (isl_basic_set_plain_is_universe(bset)) {
3642 isl_basic_set_free(bset);
3643 return bmap;
3646 bmap = isl_basic_map_cow(bmap);
3647 if (!bmap)
3648 goto error;
3649 bmap = isl_basic_map_extend(bmap,
3650 bset->n_div, bset->n_eq, bset->n_ineq);
3651 bmap_range = bset_to_bmap(bset);
3652 bmap = add_constraints(bmap, bmap_range, 0, 0);
3654 bmap = isl_basic_map_simplify(bmap);
3655 return isl_basic_map_finalize(bmap);
3656 error:
3657 isl_basic_map_free(bmap);
3658 isl_basic_set_free(bset);
3659 return NULL;
3662 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3663 __isl_keep isl_vec *vec)
3665 int i;
3666 isl_size total;
3667 isl_int s;
3669 total = isl_basic_map_dim(bmap, isl_dim_all);
3670 if (total < 0 || !vec)
3671 return isl_bool_error;
3673 if (1 + total != vec->size)
3674 return isl_bool_false;
3676 isl_int_init(s);
3678 for (i = 0; i < bmap->n_eq; ++i) {
3679 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3680 if (!isl_int_is_zero(s)) {
3681 isl_int_clear(s);
3682 return isl_bool_false;
3686 for (i = 0; i < bmap->n_ineq; ++i) {
3687 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3688 if (isl_int_is_neg(s)) {
3689 isl_int_clear(s);
3690 return isl_bool_false;
3694 isl_int_clear(s);
3696 return isl_bool_true;
3699 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3700 __isl_keep isl_vec *vec)
3702 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3705 __isl_give isl_basic_map *isl_basic_map_intersect(
3706 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3708 struct isl_vec *sample = NULL;
3709 isl_space *space1, *space2;
3710 isl_size dim1, dim2, nparam1, nparam2;
3712 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3713 goto error;
3714 space1 = isl_basic_map_peek_space(bmap1);
3715 space2 = isl_basic_map_peek_space(bmap2);
3716 dim1 = isl_space_dim(space1, isl_dim_all);
3717 dim2 = isl_space_dim(space2, isl_dim_all);
3718 nparam1 = isl_space_dim(space1, isl_dim_param);
3719 nparam2 = isl_space_dim(space2, isl_dim_param);
3720 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3721 goto error;
3722 if (dim1 == nparam1 && dim2 != nparam2)
3723 return isl_basic_map_intersect(bmap2, bmap1);
3725 if (dim2 != nparam2 &&
3726 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3727 goto error;
3729 if (isl_basic_map_plain_is_empty(bmap1)) {
3730 isl_basic_map_free(bmap2);
3731 return bmap1;
3733 if (isl_basic_map_plain_is_empty(bmap2)) {
3734 isl_basic_map_free(bmap1);
3735 return bmap2;
3738 if (bmap1->sample &&
3739 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3740 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3741 sample = isl_vec_copy(bmap1->sample);
3742 else if (bmap2->sample &&
3743 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3744 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3745 sample = isl_vec_copy(bmap2->sample);
3747 bmap1 = isl_basic_map_cow(bmap1);
3748 if (!bmap1)
3749 goto error;
3750 bmap1 = isl_basic_map_extend(bmap1,
3751 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3752 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3754 if (!bmap1)
3755 isl_vec_free(sample);
3756 else if (sample) {
3757 isl_vec_free(bmap1->sample);
3758 bmap1->sample = sample;
3761 bmap1 = isl_basic_map_simplify(bmap1);
3762 return isl_basic_map_finalize(bmap1);
3763 error:
3764 if (sample)
3765 isl_vec_free(sample);
3766 isl_basic_map_free(bmap1);
3767 isl_basic_map_free(bmap2);
3768 return NULL;
3771 __isl_give isl_basic_set *isl_basic_set_intersect(
3772 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3774 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3775 bset_to_bmap(bset2)));
3778 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3779 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3781 return isl_basic_set_intersect(bset1, bset2);
3784 /* Does "map" consist of a single disjunct, without any local variables?
3786 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3788 isl_size n_div;
3790 if (!map)
3791 return isl_bool_error;
3792 if (map->n != 1)
3793 return isl_bool_false;
3794 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3795 if (n_div < 0)
3796 return isl_bool_error;
3797 if (n_div != 0)
3798 return isl_bool_false;
3799 return isl_bool_true;
3802 /* Check that "map" consists of a single disjunct, without any local variables.
3804 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3806 isl_bool ok;
3808 ok = is_convex_no_locals(map);
3809 if (ok < 0)
3810 return isl_stat_error;
3811 if (ok)
3812 return isl_stat_ok;
3814 isl_die(isl_map_get_ctx(map), isl_error_internal,
3815 "unexpectedly not convex or involving local variables",
3816 return isl_stat_error);
3819 /* Special case of isl_map_intersect, where both map1 and map2
3820 * are convex, without any divs and such that either map1 or map2
3821 * contains a single constraint. This constraint is then simply
3822 * added to the other map.
3824 static __isl_give isl_map *map_intersect_add_constraint(
3825 __isl_take isl_map *map1, __isl_take isl_map *map2)
3827 if (check_convex_no_locals(map1) < 0 ||
3828 check_convex_no_locals(map2) < 0)
3829 goto error;
3831 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3832 return isl_map_intersect(map2, map1);
3834 map1 = isl_map_cow(map1);
3835 if (!map1)
3836 goto error;
3837 if (isl_map_plain_is_empty(map1)) {
3838 isl_map_free(map2);
3839 return map1;
3841 if (map2->p[0]->n_eq == 1)
3842 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3843 else
3844 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3845 map2->p[0]->ineq[0]);
3847 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3848 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3849 if (!map1->p[0])
3850 goto error;
3852 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3853 isl_basic_map_free(map1->p[0]);
3854 map1->n = 0;
3857 isl_map_free(map2);
3859 map1 = isl_map_unmark_normalized(map1);
3860 return map1;
3861 error:
3862 isl_map_free(map1);
3863 isl_map_free(map2);
3864 return NULL;
3867 /* map2 may be either a parameter domain or a map living in the same
3868 * space as map1.
3870 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3871 __isl_take isl_map *map2)
3873 unsigned flags = 0;
3874 isl_bool equal;
3875 isl_map *result;
3876 int i, j;
3877 isl_size dim2, nparam2;
3879 if (!map1 || !map2)
3880 goto error;
3882 if ((isl_map_plain_is_empty(map1) ||
3883 isl_map_plain_is_universe(map2)) &&
3884 isl_space_is_equal(map1->dim, map2->dim)) {
3885 isl_map_free(map2);
3886 return map1;
3888 if ((isl_map_plain_is_empty(map2) ||
3889 isl_map_plain_is_universe(map1)) &&
3890 isl_space_is_equal(map1->dim, map2->dim)) {
3891 isl_map_free(map1);
3892 return map2;
3895 if (is_convex_no_locals(map1) == isl_bool_true &&
3896 is_convex_no_locals(map2) == isl_bool_true &&
3897 isl_space_is_equal(map1->dim, map2->dim) &&
3898 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3899 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3900 return map_intersect_add_constraint(map1, map2);
3902 equal = isl_map_plain_is_equal(map1, map2);
3903 if (equal < 0)
3904 goto error;
3905 if (equal) {
3906 isl_map_free(map2);
3907 return map1;
3910 dim2 = isl_map_dim(map2, isl_dim_all);
3911 nparam2 = isl_map_dim(map2, isl_dim_param);
3912 if (dim2 < 0 || nparam2 < 0)
3913 goto error;
3914 if (dim2 != nparam2)
3915 isl_assert(map1->ctx,
3916 isl_space_is_equal(map1->dim, map2->dim), goto error);
3918 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3919 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3920 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3922 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3923 map1->n * map2->n, flags);
3924 if (!result)
3925 goto error;
3926 for (i = 0; i < map1->n; ++i)
3927 for (j = 0; j < map2->n; ++j) {
3928 struct isl_basic_map *part;
3929 part = isl_basic_map_intersect(
3930 isl_basic_map_copy(map1->p[i]),
3931 isl_basic_map_copy(map2->p[j]));
3932 if (isl_basic_map_is_empty(part) < 0)
3933 part = isl_basic_map_free(part);
3934 result = isl_map_add_basic_map(result, part);
3935 if (!result)
3936 goto error;
3938 isl_map_free(map1);
3939 isl_map_free(map2);
3940 return result;
3941 error:
3942 isl_map_free(map1);
3943 isl_map_free(map2);
3944 return NULL;
3947 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3948 __isl_take isl_map *map2)
3950 if (isl_map_check_equal_space(map1, map2) < 0)
3951 goto error;
3952 return map_intersect_internal(map1, map2);
3953 error:
3954 isl_map_free(map1);
3955 isl_map_free(map2);
3956 return NULL;
3959 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3960 __isl_take isl_map *map2)
3962 isl_map_align_params_bin(&map1, &map2);
3963 return map_intersect(map1, map2);
3966 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3967 __isl_take isl_set *set2)
3969 return set_from_map(isl_map_intersect(set_to_map(set1),
3970 set_to_map(set2)));
3973 /* map_intersect_internal accepts intersections
3974 * with parameter domains, so we can just call that function.
3976 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3977 __isl_take isl_set *params)
3979 isl_map_align_params_set(&map, &params);
3980 return map_intersect_internal(map, params);
3983 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3984 __isl_take isl_set *params)
3986 return isl_map_intersect_params(set, params);
3989 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3991 isl_space *space;
3992 unsigned pos;
3993 isl_size n1, n2;
3995 if (!bmap)
3996 return NULL;
3997 bmap = isl_basic_map_cow(bmap);
3998 if (!bmap)
3999 return NULL;
4000 space = isl_space_reverse(isl_space_copy(bmap->dim));
4001 pos = isl_basic_map_offset(bmap, isl_dim_in);
4002 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4003 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4004 if (n1 < 0 || n2 < 0)
4005 bmap = isl_basic_map_free(bmap);
4006 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4007 return isl_basic_map_reset_space(bmap, space);
4010 /* Given a basic map where the tuple of type "type" is a wrapped map,
4011 * swap domain and range of that wrapped map.
4013 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4014 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4016 isl_space *space;
4017 isl_size offset, n1, n2;
4019 space = isl_basic_map_peek_space(bmap);
4020 offset = isl_basic_map_var_offset(bmap, type);
4021 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4022 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4023 if (offset < 0 || n1 < 0 || n2 < 0)
4024 return isl_basic_map_free(bmap);
4026 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4028 space = isl_basic_map_take_space(bmap);
4029 space = isl_space_reverse_wrapped(space, type);
4030 bmap = isl_basic_map_restore_space(bmap, space);
4032 return bmap;
4035 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4036 * (B -> A) -> C.
4038 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4039 __isl_take isl_basic_map *bmap)
4041 isl_space *space;
4043 space = isl_basic_map_peek_space(bmap);
4044 if (isl_space_check_domain_is_wrapping(space) < 0)
4045 return isl_basic_map_free(bmap);
4046 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4048 return bmap;
4051 /* Given a basic map A -> (B -> C), return the corresponding basic map
4052 * A -> (C -> B).
4054 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4055 __isl_take isl_basic_map *bmap)
4057 isl_space *space;
4059 space = isl_basic_map_peek_space(bmap);
4060 if (isl_space_check_range_is_wrapping(space) < 0)
4061 return isl_basic_map_free(bmap);
4062 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4064 return bmap;
4067 /* Given a basic map that is actually a basic set (A -> B),
4068 * return the corresponding basic set (B -> A) as a basic map.
4070 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4071 __isl_take isl_basic_map *bmap)
4073 isl_space *space;
4075 space = isl_basic_map_peek_space(bmap);
4076 if (isl_space_check_is_wrapping(space) < 0)
4077 return isl_basic_map_free(bmap);
4078 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4080 return bmap;
4083 static __isl_give isl_basic_map *basic_map_space_reset(
4084 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4086 isl_space *space;
4088 if (!bmap)
4089 return NULL;
4090 if (!isl_space_is_named_or_nested(bmap->dim, type))
4091 return bmap;
4093 space = isl_basic_map_get_space(bmap);
4094 space = isl_space_reset(space, type);
4095 bmap = isl_basic_map_reset_space(bmap, space);
4096 return bmap;
4099 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4100 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4101 unsigned pos, unsigned n)
4103 isl_bool rational, is_empty;
4104 isl_space *res_space;
4105 struct isl_basic_map *res;
4106 struct isl_dim_map *dim_map;
4107 isl_size total;
4108 unsigned off;
4109 enum isl_dim_type t;
4111 if (n == 0)
4112 return basic_map_space_reset(bmap, type);
4114 is_empty = isl_basic_map_plain_is_empty(bmap);
4115 total = isl_basic_map_dim(bmap, isl_dim_all);
4116 if (is_empty < 0 || total < 0)
4117 return isl_basic_map_free(bmap);
4118 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4119 type, pos, n);
4120 if (!res_space)
4121 return isl_basic_map_free(bmap);
4122 if (is_empty) {
4123 isl_basic_map_free(bmap);
4124 return isl_basic_map_empty(res_space);
4127 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4128 off = 0;
4129 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4130 isl_size dim;
4132 if (t != type) {
4133 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4134 } else {
4135 isl_size size = isl_basic_map_dim(bmap, t);
4136 if (size < 0)
4137 dim_map = isl_dim_map_free(dim_map);
4138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4139 0, pos, off);
4140 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4141 pos, size - pos, off + pos + n);
4143 dim = isl_space_dim(res_space, t);
4144 if (dim < 0)
4145 dim_map = isl_dim_map_free(dim_map);
4146 off += dim;
4148 isl_dim_map_div(dim_map, bmap, off);
4150 res = isl_basic_map_alloc_space(res_space,
4151 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4152 rational = isl_basic_map_is_rational(bmap);
4153 if (rational < 0)
4154 res = isl_basic_map_free(res);
4155 if (rational)
4156 res = isl_basic_map_set_rational(res);
4157 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4158 return isl_basic_map_finalize(res);
4161 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4162 __isl_take isl_basic_set *bset,
4163 enum isl_dim_type type, unsigned pos, unsigned n)
4165 return isl_basic_map_insert_dims(bset, type, pos, n);
4168 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4169 enum isl_dim_type type, unsigned n)
4171 isl_size dim;
4173 dim = isl_basic_map_dim(bmap, type);
4174 if (dim < 0)
4175 return isl_basic_map_free(bmap);
4176 return isl_basic_map_insert_dims(bmap, type, dim, n);
4179 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4180 enum isl_dim_type type, unsigned n)
4182 if (!bset)
4183 return NULL;
4184 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4185 return isl_basic_map_add_dims(bset, type, n);
4186 error:
4187 isl_basic_set_free(bset);
4188 return NULL;
4191 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4192 enum isl_dim_type type)
4194 isl_space *space;
4196 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4197 return map;
4199 space = isl_map_get_space(map);
4200 space = isl_space_reset(space, type);
4201 map = isl_map_reset_space(map, space);
4202 return map;
4205 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4206 enum isl_dim_type type, unsigned pos, unsigned n)
4208 int i;
4209 isl_space *space;
4211 if (n == 0)
4212 return map_space_reset(map, type);
4214 map = isl_map_cow(map);
4215 if (!map)
4216 return NULL;
4218 for (i = 0; i < map->n; ++i) {
4219 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4220 if (!map->p[i])
4221 goto error;
4224 space = isl_map_take_space(map);
4225 space = isl_space_insert_dims(space, type, pos, n);
4226 map = isl_map_restore_space(map, space);
4228 return map;
4229 error:
4230 isl_map_free(map);
4231 return NULL;
4234 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4235 enum isl_dim_type type, unsigned pos, unsigned n)
4237 return isl_map_insert_dims(set, type, pos, n);
4240 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4241 enum isl_dim_type type, unsigned n)
4243 isl_size dim;
4245 dim = isl_map_dim(map, type);
4246 if (dim < 0)
4247 return isl_map_free(map);
4248 return isl_map_insert_dims(map, type, dim, n);
4251 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4252 enum isl_dim_type type, unsigned n)
4254 if (!set)
4255 return NULL;
4256 isl_assert(set->ctx, type != isl_dim_in, goto error);
4257 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4258 error:
4259 isl_set_free(set);
4260 return NULL;
4263 __isl_give isl_basic_map *isl_basic_map_move_dims(
4264 __isl_take isl_basic_map *bmap,
4265 enum isl_dim_type dst_type, unsigned dst_pos,
4266 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4268 isl_space *space;
4269 struct isl_dim_map *dim_map;
4270 struct isl_basic_map *res;
4271 enum isl_dim_type t;
4272 isl_size total;
4273 unsigned off;
4275 if (!bmap)
4276 return NULL;
4277 if (n == 0) {
4278 bmap = isl_basic_map_reset(bmap, src_type);
4279 bmap = isl_basic_map_reset(bmap, dst_type);
4280 return bmap;
4283 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4284 return isl_basic_map_free(bmap);
4286 if (dst_type == src_type && dst_pos == src_pos)
4287 return bmap;
4289 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4291 if (pos(bmap->dim, dst_type) + dst_pos ==
4292 pos(bmap->dim, src_type) + src_pos +
4293 ((src_type < dst_type) ? n : 0)) {
4294 space = isl_basic_map_take_space(bmap);
4295 space = isl_space_move_dims(space, dst_type, dst_pos,
4296 src_type, src_pos, n);
4297 bmap = isl_basic_map_restore_space(bmap, space);
4298 bmap = isl_basic_map_finalize(bmap);
4300 return bmap;
4303 total = isl_basic_map_dim(bmap, isl_dim_all);
4304 if (total < 0)
4305 return isl_basic_map_free(bmap);
4306 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4308 off = 0;
4309 space = isl_basic_map_peek_space(bmap);
4310 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4311 isl_size size = isl_space_dim(space, t);
4312 if (size < 0)
4313 dim_map = isl_dim_map_free(dim_map);
4314 if (t == dst_type) {
4315 isl_dim_map_dim_range(dim_map, space, t,
4316 0, dst_pos, off);
4317 off += dst_pos;
4318 isl_dim_map_dim_range(dim_map, space, src_type,
4319 src_pos, n, off);
4320 off += n;
4321 isl_dim_map_dim_range(dim_map, space, t,
4322 dst_pos, size - dst_pos, off);
4323 off += size - dst_pos;
4324 } else if (t == src_type) {
4325 isl_dim_map_dim_range(dim_map, space, t,
4326 0, src_pos, off);
4327 off += src_pos;
4328 isl_dim_map_dim_range(dim_map, space, t,
4329 src_pos + n, size - src_pos - n, off);
4330 off += size - src_pos - n;
4331 } else {
4332 isl_dim_map_dim(dim_map, space, t, off);
4333 off += size;
4336 isl_dim_map_div(dim_map, bmap, off);
4338 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4339 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4340 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4341 space = isl_basic_map_take_space(bmap);
4342 space = isl_space_move_dims(space, dst_type, dst_pos,
4343 src_type, src_pos, n);
4344 bmap = isl_basic_map_restore_space(bmap, space);
4345 if (!bmap)
4346 goto error;
4348 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4349 bmap = isl_basic_map_gauss(bmap, NULL);
4350 bmap = isl_basic_map_finalize(bmap);
4352 return bmap;
4353 error:
4354 isl_basic_map_free(bmap);
4355 return NULL;
4358 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4359 enum isl_dim_type dst_type, unsigned dst_pos,
4360 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4362 isl_basic_map *bmap = bset_to_bmap(bset);
4363 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4364 src_type, src_pos, n);
4365 return bset_from_bmap(bmap);
4368 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4369 enum isl_dim_type dst_type, unsigned dst_pos,
4370 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4372 if (!set)
4373 return NULL;
4374 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4375 return set_from_map(isl_map_move_dims(set_to_map(set),
4376 dst_type, dst_pos, src_type, src_pos, n));
4377 error:
4378 isl_set_free(set);
4379 return NULL;
4382 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4383 enum isl_dim_type dst_type, unsigned dst_pos,
4384 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4386 int i;
4387 isl_space *space;
4389 if (n == 0) {
4390 map = isl_map_reset(map, src_type);
4391 map = isl_map_reset(map, dst_type);
4392 return map;
4395 if (isl_map_check_range(map, src_type, src_pos, n))
4396 return isl_map_free(map);
4398 if (dst_type == src_type && dst_pos == src_pos)
4399 return map;
4401 isl_assert(map->ctx, dst_type != src_type, goto error);
4403 map = isl_map_cow(map);
4404 if (!map)
4405 return NULL;
4407 for (i = 0; i < map->n; ++i) {
4408 map->p[i] = isl_basic_map_move_dims(map->p[i],
4409 dst_type, dst_pos,
4410 src_type, src_pos, n);
4411 if (!map->p[i])
4412 goto error;
4415 space = isl_map_take_space(map);
4416 space = isl_space_move_dims(space, dst_type, dst_pos,
4417 src_type, src_pos, n);
4418 map = isl_map_restore_space(map, space);
4420 return map;
4421 error:
4422 isl_map_free(map);
4423 return NULL;
4426 /* Move the specified dimensions to the last columns right before
4427 * the divs. Don't change the dimension specification of bmap.
4428 * That's the responsibility of the caller.
4430 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4431 enum isl_dim_type type, unsigned first, unsigned n)
4433 isl_space *space;
4434 struct isl_dim_map *dim_map;
4435 struct isl_basic_map *res;
4436 enum isl_dim_type t;
4437 isl_size total;
4438 unsigned off;
4440 if (!bmap)
4441 return NULL;
4442 if (isl_basic_map_offset(bmap, type) + first + n ==
4443 isl_basic_map_offset(bmap, isl_dim_div))
4444 return bmap;
4446 total = isl_basic_map_dim(bmap, isl_dim_all);
4447 if (total < 0)
4448 return isl_basic_map_free(bmap);
4449 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4451 off = 0;
4452 space = isl_basic_map_peek_space(bmap);
4453 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4454 isl_size size = isl_space_dim(space, t);
4455 if (size < 0)
4456 dim_map = isl_dim_map_free(dim_map);
4457 if (t == type) {
4458 isl_dim_map_dim_range(dim_map, space, t,
4459 0, first, off);
4460 off += first;
4461 isl_dim_map_dim_range(dim_map, space, t,
4462 first, n, total - bmap->n_div - n);
4463 isl_dim_map_dim_range(dim_map, space, t,
4464 first + n, size - (first + n), off);
4465 off += size - (first + n);
4466 } else {
4467 isl_dim_map_dim(dim_map, space, t, off);
4468 off += size;
4471 isl_dim_map_div(dim_map, bmap, off + n);
4473 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4474 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4475 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4476 return res;
4479 /* Insert "n" rows in the divs of "bmap".
4481 * The number of columns is not changed, which means that the last
4482 * dimensions of "bmap" are being reintepreted as the new divs.
4483 * The space of "bmap" is not adjusted, however, which means
4484 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4485 * from the space of "bmap" is the responsibility of the caller.
4487 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4488 int n)
4490 int i;
4491 size_t row_size;
4492 isl_int **new_div;
4493 isl_int *old;
4495 bmap = isl_basic_map_cow(bmap);
4496 if (!bmap)
4497 return NULL;
4499 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4500 old = bmap->block2.data;
4501 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4502 (bmap->extra + n) * (1 + row_size));
4503 if (!bmap->block2.data)
4504 return isl_basic_map_free(bmap);
4505 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4506 if (!new_div)
4507 return isl_basic_map_free(bmap);
4508 for (i = 0; i < n; ++i) {
4509 new_div[i] = bmap->block2.data +
4510 (bmap->extra + i) * (1 + row_size);
4511 isl_seq_clr(new_div[i], 1 + row_size);
4513 for (i = 0; i < bmap->extra; ++i)
4514 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4515 free(bmap->div);
4516 bmap->div = new_div;
4517 bmap->n_div += n;
4518 bmap->extra += n;
4520 return bmap;
4523 /* Drop constraints from "bmap" that only involve the variables
4524 * of "type" in the range [first, first + n] that are not related
4525 * to any of the variables outside that interval.
4526 * These constraints cannot influence the values for the variables
4527 * outside the interval, except in case they cause "bmap" to be empty.
4528 * Only drop the constraints if "bmap" is known to be non-empty.
4530 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4531 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4532 unsigned first, unsigned n)
4534 int i;
4535 int *groups;
4536 isl_size dim, n_div;
4537 isl_bool non_empty;
4539 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4540 if (non_empty < 0)
4541 return isl_basic_map_free(bmap);
4542 if (!non_empty)
4543 return bmap;
4545 dim = isl_basic_map_dim(bmap, isl_dim_all);
4546 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4547 if (dim < 0 || n_div < 0)
4548 return isl_basic_map_free(bmap);
4549 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4550 if (!groups)
4551 return isl_basic_map_free(bmap);
4552 first += isl_basic_map_offset(bmap, type) - 1;
4553 for (i = 0; i < first; ++i)
4554 groups[i] = -1;
4555 for (i = first + n; i < dim - n_div; ++i)
4556 groups[i] = -1;
4558 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4560 return bmap;
4563 /* Turn the n dimensions of type type, starting at first
4564 * into existentially quantified variables.
4566 * If a subset of the projected out variables are unrelated
4567 * to any of the variables that remain, then the constraints
4568 * involving this subset are simply dropped first.
4570 __isl_give isl_basic_map *isl_basic_map_project_out(
4571 __isl_take isl_basic_map *bmap,
4572 enum isl_dim_type type, unsigned first, unsigned n)
4574 isl_bool empty;
4575 isl_space *space;
4577 if (n == 0)
4578 return basic_map_space_reset(bmap, type);
4579 if (type == isl_dim_div)
4580 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4581 "cannot project out existentially quantified variables",
4582 return isl_basic_map_free(bmap));
4584 empty = isl_basic_map_plain_is_empty(bmap);
4585 if (empty < 0)
4586 return isl_basic_map_free(bmap);
4587 if (empty)
4588 bmap = isl_basic_map_set_to_empty(bmap);
4590 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4591 if (!bmap)
4592 return NULL;
4594 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4595 return isl_basic_map_remove_dims(bmap, type, first, n);
4597 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4598 return isl_basic_map_free(bmap);
4600 bmap = move_last(bmap, type, first, n);
4601 bmap = isl_basic_map_cow(bmap);
4602 bmap = insert_div_rows(bmap, n);
4604 space = isl_basic_map_take_space(bmap);
4605 space = isl_space_drop_dims(space, type, first, n);
4606 bmap = isl_basic_map_restore_space(bmap, space);
4607 bmap = isl_basic_map_simplify(bmap);
4608 bmap = isl_basic_map_drop_redundant_divs(bmap);
4609 return isl_basic_map_finalize(bmap);
4612 /* Turn the n dimensions of type type, starting at first
4613 * into existentially quantified variables.
4615 __isl_give isl_basic_set *isl_basic_set_project_out(
4616 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4617 unsigned first, unsigned n)
4619 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4620 type, first, n));
4623 /* Turn the n dimensions of type type, starting at first
4624 * into existentially quantified variables.
4626 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4627 enum isl_dim_type type, unsigned first, unsigned n)
4629 int i;
4630 isl_space *space;
4632 if (n == 0)
4633 return map_space_reset(map, type);
4635 if (isl_map_check_range(map, type, first, n) < 0)
4636 return isl_map_free(map);
4638 map = isl_map_cow(map);
4639 if (!map)
4640 return NULL;
4642 for (i = 0; i < map->n; ++i) {
4643 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4644 if (!map->p[i])
4645 goto error;
4648 if (map->n > 1)
4649 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4650 map = isl_map_unmark_normalized(map);
4652 space = isl_map_take_space(map);
4653 space = isl_space_drop_dims(space, type, first, n);
4654 map = isl_map_restore_space(map, space);
4656 return map;
4657 error:
4658 isl_map_free(map);
4659 return NULL;
4662 #undef TYPE
4663 #define TYPE isl_map
4664 #include "isl_project_out_all_params_templ.c"
4665 #include "isl_project_out_param_templ.c"
4667 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4668 * into existentially quantified variables.
4670 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4671 enum isl_dim_type type, unsigned first, unsigned n)
4673 isl_size dim;
4675 dim = isl_map_dim(map, type);
4676 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4677 return isl_map_free(map);
4678 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4679 map = isl_map_project_out(map, type, 0, first);
4680 return map;
4683 /* Turn the n dimensions of type type, starting at first
4684 * into existentially quantified variables.
4686 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4687 enum isl_dim_type type, unsigned first, unsigned n)
4689 return set_from_map(isl_map_project_out(set_to_map(set),
4690 type, first, n));
4693 /* If "set" involves a parameter with identifier "id",
4694 * then turn it into an existentially quantified variable.
4696 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4697 __isl_take isl_id *id)
4699 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4702 /* If "set" involves any of the parameters with identifiers in "list",
4703 * then turn them into existentially quantified variables.
4705 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4706 __isl_take isl_id_list *list)
4708 isl_map *map;
4710 map = set_to_map(set);
4711 map = isl_map_project_out_param_id_list(map, list);
4712 return set_from_map(map);
4715 /* Project out all parameters from "set" by existentially quantifying
4716 * over them.
4718 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4720 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4723 /* Return a map that projects the elements in "set" onto their
4724 * "n" set dimensions starting at "first".
4725 * "type" should be equal to isl_dim_set.
4727 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4728 enum isl_dim_type type, unsigned first, unsigned n)
4730 int i;
4731 isl_map *map;
4733 if (type != isl_dim_set)
4734 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4735 "only set dimensions can be projected out", goto error);
4736 if (isl_set_check_range(set, type, first, n) < 0)
4737 return isl_set_free(set);
4739 map = isl_map_from_domain(set);
4740 map = isl_map_add_dims(map, isl_dim_out, n);
4741 for (i = 0; i < n; ++i)
4742 map = isl_map_equate(map, isl_dim_in, first + i,
4743 isl_dim_out, i);
4744 return map;
4745 error:
4746 isl_set_free(set);
4747 return NULL;
4750 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4751 unsigned n)
4753 int i, j;
4754 isl_size total;
4756 total = isl_basic_map_dim(bmap, isl_dim_all);
4757 if (total < 0)
4758 return isl_basic_map_free(bmap);
4759 for (i = 0; i < n; ++i) {
4760 j = isl_basic_map_alloc_div(bmap);
4761 if (j < 0)
4762 goto error;
4763 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4765 return bmap;
4766 error:
4767 isl_basic_map_free(bmap);
4768 return NULL;
4771 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4773 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4774 __isl_keep isl_basic_map *bmap2)
4776 isl_space *space1, *space2;
4778 space1 = isl_basic_map_peek_space(bmap1);
4779 space2 = isl_basic_map_peek_space(bmap2);
4780 return isl_space_tuple_is_equal(space1, isl_dim_out,
4781 space2, isl_dim_in);
4784 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4786 static isl_stat isl_basic_map_check_applies_range(
4787 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4789 isl_bool equal;
4791 equal = isl_basic_map_applies_range(bmap1, bmap2);
4792 if (equal < 0)
4793 return isl_stat_error;
4794 if (!equal)
4795 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4796 "spaces don't match", return isl_stat_error);
4797 return isl_stat_ok;
4800 __isl_give isl_basic_map *isl_basic_map_apply_range(
4801 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4803 isl_space *space_result = NULL;
4804 struct isl_basic_map *bmap;
4805 isl_size n_in, n_out, n, nparam;
4806 unsigned total, pos;
4807 struct isl_dim_map *dim_map1, *dim_map2;
4809 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4810 goto error;
4811 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4812 goto error;
4814 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4815 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4816 n = isl_basic_map_dim(bmap1, isl_dim_out);
4817 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4818 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4819 goto error;
4821 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4822 isl_basic_map_get_space(bmap2));
4824 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4825 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4826 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4827 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4828 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4831 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4832 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4833 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4834 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4836 bmap = isl_basic_map_alloc_space(space_result,
4837 bmap1->n_div + bmap2->n_div + n,
4838 bmap1->n_eq + bmap2->n_eq,
4839 bmap1->n_ineq + bmap2->n_ineq);
4840 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4841 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4842 bmap = add_divs(bmap, n);
4843 bmap = isl_basic_map_simplify(bmap);
4844 bmap = isl_basic_map_drop_redundant_divs(bmap);
4845 return isl_basic_map_finalize(bmap);
4846 error:
4847 isl_basic_map_free(bmap1);
4848 isl_basic_map_free(bmap2);
4849 return NULL;
4852 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4853 __isl_take isl_basic_map *bmap)
4855 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4856 goto error;
4858 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4859 bmap));
4860 error:
4861 isl_basic_set_free(bset);
4862 isl_basic_map_free(bmap);
4863 return NULL;
4866 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4867 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4869 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4870 goto error;
4871 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4872 bmap2->dim, isl_dim_in))
4873 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4874 "spaces don't match", goto error);
4876 bmap1 = isl_basic_map_reverse(bmap1);
4877 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4878 return isl_basic_map_reverse(bmap1);
4879 error:
4880 isl_basic_map_free(bmap1);
4881 isl_basic_map_free(bmap2);
4882 return NULL;
4885 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4886 * A \cap B -> f(A) + f(B)
4888 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4889 __isl_take isl_basic_map *bmap2)
4891 isl_size n_in, n_out, nparam;
4892 unsigned total, pos;
4893 struct isl_basic_map *bmap = NULL;
4894 struct isl_dim_map *dim_map1, *dim_map2;
4895 int i;
4897 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4898 goto error;
4900 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4901 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4902 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4903 if (nparam < 0 || n_in < 0 || n_out < 0)
4904 goto error;
4906 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4907 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4908 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4913 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4914 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4915 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4916 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4918 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4919 bmap1->n_div + bmap2->n_div + 2 * n_out,
4920 bmap1->n_eq + bmap2->n_eq + n_out,
4921 bmap1->n_ineq + bmap2->n_ineq);
4922 for (i = 0; i < n_out; ++i) {
4923 int j = isl_basic_map_alloc_equality(bmap);
4924 if (j < 0)
4925 goto error;
4926 isl_seq_clr(bmap->eq[j], 1+total);
4927 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4928 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4929 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4931 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4932 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4933 bmap = add_divs(bmap, 2 * n_out);
4935 bmap = isl_basic_map_simplify(bmap);
4936 return isl_basic_map_finalize(bmap);
4937 error:
4938 isl_basic_map_free(bmap);
4939 isl_basic_map_free(bmap1);
4940 isl_basic_map_free(bmap2);
4941 return NULL;
4944 /* Given two maps A -> f(A) and B -> g(B), construct a map
4945 * A \cap B -> f(A) + f(B)
4947 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4948 __isl_take isl_map *map2)
4950 struct isl_map *result;
4951 int i, j;
4953 if (isl_map_check_equal_space(map1, map2) < 0)
4954 goto error;
4956 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4957 map1->n * map2->n, 0);
4958 if (!result)
4959 goto error;
4960 for (i = 0; i < map1->n; ++i)
4961 for (j = 0; j < map2->n; ++j) {
4962 struct isl_basic_map *part;
4963 part = isl_basic_map_sum(
4964 isl_basic_map_copy(map1->p[i]),
4965 isl_basic_map_copy(map2->p[j]));
4966 if (isl_basic_map_is_empty(part))
4967 isl_basic_map_free(part);
4968 else
4969 result = isl_map_add_basic_map(result, part);
4970 if (!result)
4971 goto error;
4973 isl_map_free(map1);
4974 isl_map_free(map2);
4975 return result;
4976 error:
4977 isl_map_free(map1);
4978 isl_map_free(map2);
4979 return NULL;
4982 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4983 __isl_take isl_set *set2)
4985 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4988 /* Given a basic map A -> f(A), construct A -> -f(A).
4990 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4992 int i, j;
4993 unsigned off;
4994 isl_size n;
4996 bmap = isl_basic_map_cow(bmap);
4997 n = isl_basic_map_dim(bmap, isl_dim_out);
4998 if (n < 0)
4999 return isl_basic_map_free(bmap);
5001 off = isl_basic_map_offset(bmap, isl_dim_out);
5002 for (i = 0; i < bmap->n_eq; ++i)
5003 for (j = 0; j < n; ++j)
5004 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5005 for (i = 0; i < bmap->n_ineq; ++i)
5006 for (j = 0; j < n; ++j)
5007 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5008 for (i = 0; i < bmap->n_div; ++i)
5009 for (j = 0; j < n; ++j)
5010 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5011 bmap = isl_basic_map_gauss(bmap, NULL);
5012 return isl_basic_map_finalize(bmap);
5015 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5017 return isl_basic_map_neg(bset);
5020 /* Given a map A -> f(A), construct A -> -f(A).
5022 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5024 int i;
5026 map = isl_map_cow(map);
5027 if (!map)
5028 return NULL;
5030 for (i = 0; i < map->n; ++i) {
5031 map->p[i] = isl_basic_map_neg(map->p[i]);
5032 if (!map->p[i])
5033 goto error;
5036 return map;
5037 error:
5038 isl_map_free(map);
5039 return NULL;
5042 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5044 return set_from_map(isl_map_neg(set_to_map(set)));
5047 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5048 * A -> floor(f(A)/d).
5050 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5051 isl_int d)
5053 isl_size n_in, n_out, nparam;
5054 unsigned total, pos;
5055 struct isl_basic_map *result = NULL;
5056 struct isl_dim_map *dim_map;
5057 int i;
5059 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5060 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5061 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5062 if (nparam < 0 || n_in < 0 || n_out < 0)
5063 return isl_basic_map_free(bmap);
5065 total = nparam + n_in + n_out + bmap->n_div + n_out;
5066 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5067 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5068 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5069 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5070 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5072 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5073 bmap->n_div + n_out,
5074 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5075 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5076 result = add_divs(result, n_out);
5077 for (i = 0; i < n_out; ++i) {
5078 int j;
5079 j = isl_basic_map_alloc_inequality(result);
5080 if (j < 0)
5081 goto error;
5082 isl_seq_clr(result->ineq[j], 1+total);
5083 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5084 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5085 j = isl_basic_map_alloc_inequality(result);
5086 if (j < 0)
5087 goto error;
5088 isl_seq_clr(result->ineq[j], 1+total);
5089 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5090 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5091 isl_int_sub_ui(result->ineq[j][0], d, 1);
5094 result = isl_basic_map_simplify(result);
5095 return isl_basic_map_finalize(result);
5096 error:
5097 isl_basic_map_free(result);
5098 return NULL;
5101 /* Given a map A -> f(A) and an integer d, construct a map
5102 * A -> floor(f(A)/d).
5104 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5106 int i;
5108 map = isl_map_cow(map);
5109 if (!map)
5110 return NULL;
5112 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5113 for (i = 0; i < map->n; ++i) {
5114 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5115 if (!map->p[i])
5116 goto error;
5118 map = isl_map_unmark_normalized(map);
5120 return map;
5121 error:
5122 isl_map_free(map);
5123 return NULL;
5126 /* Given a map A -> f(A) and an integer d, construct a map
5127 * A -> floor(f(A)/d).
5129 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5130 __isl_take isl_val *d)
5132 if (!map || !d)
5133 goto error;
5134 if (!isl_val_is_int(d))
5135 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5136 "expecting integer denominator", goto error);
5137 map = isl_map_floordiv(map, d->n);
5138 isl_val_free(d);
5139 return map;
5140 error:
5141 isl_map_free(map);
5142 isl_val_free(d);
5143 return NULL;
5146 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5147 unsigned pos)
5149 int i;
5150 isl_size nparam;
5151 isl_size n_in;
5152 isl_size total;
5154 total = isl_basic_map_dim(bmap, isl_dim_all);
5155 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5156 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5157 if (total < 0 || nparam < 0 || n_in < 0)
5158 return isl_basic_map_free(bmap);
5159 i = isl_basic_map_alloc_equality(bmap);
5160 if (i < 0)
5161 goto error;
5162 isl_seq_clr(bmap->eq[i], 1 + total);
5163 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5164 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5165 return isl_basic_map_finalize(bmap);
5166 error:
5167 isl_basic_map_free(bmap);
5168 return NULL;
5171 /* Add a constraint to "bmap" expressing i_pos < o_pos
5173 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5174 unsigned pos)
5176 int i;
5177 isl_size nparam;
5178 isl_size n_in;
5179 isl_size total;
5181 total = isl_basic_map_dim(bmap, isl_dim_all);
5182 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5183 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5184 if (total < 0 || nparam < 0 || n_in < 0)
5185 return isl_basic_map_free(bmap);
5186 i = isl_basic_map_alloc_inequality(bmap);
5187 if (i < 0)
5188 goto error;
5189 isl_seq_clr(bmap->ineq[i], 1 + total);
5190 isl_int_set_si(bmap->ineq[i][0], -1);
5191 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5192 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5193 return isl_basic_map_finalize(bmap);
5194 error:
5195 isl_basic_map_free(bmap);
5196 return NULL;
5199 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5201 static __isl_give isl_basic_map *var_less_or_equal(
5202 __isl_take isl_basic_map *bmap, unsigned pos)
5204 int i;
5205 isl_size nparam;
5206 isl_size n_in;
5207 isl_size total;
5209 total = isl_basic_map_dim(bmap, isl_dim_all);
5210 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5211 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5212 if (total < 0 || nparam < 0 || n_in < 0)
5213 return isl_basic_map_free(bmap);
5214 i = isl_basic_map_alloc_inequality(bmap);
5215 if (i < 0)
5216 goto error;
5217 isl_seq_clr(bmap->ineq[i], 1 + total);
5218 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5219 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5220 return isl_basic_map_finalize(bmap);
5221 error:
5222 isl_basic_map_free(bmap);
5223 return NULL;
5226 /* Add a constraint to "bmap" expressing i_pos > o_pos
5228 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5229 unsigned pos)
5231 int i;
5232 isl_size nparam;
5233 isl_size n_in;
5234 isl_size total;
5236 total = isl_basic_map_dim(bmap, isl_dim_all);
5237 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5238 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5239 if (total < 0 || nparam < 0 || n_in < 0)
5240 return isl_basic_map_free(bmap);
5241 i = isl_basic_map_alloc_inequality(bmap);
5242 if (i < 0)
5243 goto error;
5244 isl_seq_clr(bmap->ineq[i], 1 + total);
5245 isl_int_set_si(bmap->ineq[i][0], -1);
5246 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5247 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5248 return isl_basic_map_finalize(bmap);
5249 error:
5250 isl_basic_map_free(bmap);
5251 return NULL;
5254 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5256 static __isl_give isl_basic_map *var_more_or_equal(
5257 __isl_take isl_basic_map *bmap, unsigned pos)
5259 int i;
5260 isl_size nparam;
5261 isl_size n_in;
5262 isl_size total;
5264 total = isl_basic_map_dim(bmap, isl_dim_all);
5265 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5266 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5267 if (total < 0 || nparam < 0 || n_in < 0)
5268 return isl_basic_map_free(bmap);
5269 i = isl_basic_map_alloc_inequality(bmap);
5270 if (i < 0)
5271 goto error;
5272 isl_seq_clr(bmap->ineq[i], 1 + total);
5273 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5274 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5275 return isl_basic_map_finalize(bmap);
5276 error:
5277 isl_basic_map_free(bmap);
5278 return NULL;
5281 __isl_give isl_basic_map *isl_basic_map_equal(
5282 __isl_take isl_space *space, unsigned n_equal)
5284 int i;
5285 struct isl_basic_map *bmap;
5286 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5287 if (!bmap)
5288 return NULL;
5289 for (i = 0; i < n_equal && bmap; ++i)
5290 bmap = var_equal(bmap, i);
5291 return isl_basic_map_finalize(bmap);
5294 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5296 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5297 unsigned pos)
5299 int i;
5300 struct isl_basic_map *bmap;
5301 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5302 if (!bmap)
5303 return NULL;
5304 for (i = 0; i < pos && bmap; ++i)
5305 bmap = var_equal(bmap, i);
5306 if (bmap)
5307 bmap = var_less(bmap, pos);
5308 return isl_basic_map_finalize(bmap);
5311 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5313 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5314 __isl_take isl_space *space, unsigned pos)
5316 int i;
5317 isl_basic_map *bmap;
5319 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5320 for (i = 0; i < pos; ++i)
5321 bmap = var_equal(bmap, i);
5322 bmap = var_less_or_equal(bmap, pos);
5323 return isl_basic_map_finalize(bmap);
5326 /* Return a relation on "space" expressing i_pos > o_pos
5328 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5329 unsigned pos)
5331 int i;
5332 struct isl_basic_map *bmap;
5333 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5334 if (!bmap)
5335 return NULL;
5336 for (i = 0; i < pos && bmap; ++i)
5337 bmap = var_equal(bmap, i);
5338 if (bmap)
5339 bmap = var_more(bmap, pos);
5340 return isl_basic_map_finalize(bmap);
5343 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5345 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5346 __isl_take isl_space *space, unsigned pos)
5348 int i;
5349 isl_basic_map *bmap;
5351 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5352 for (i = 0; i < pos; ++i)
5353 bmap = var_equal(bmap, i);
5354 bmap = var_more_or_equal(bmap, pos);
5355 return isl_basic_map_finalize(bmap);
5358 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5359 unsigned n, int equal)
5361 struct isl_map *map;
5362 int i;
5364 if (n == 0 && equal)
5365 return isl_map_universe(space);
5367 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5369 for (i = 0; i + 1 < n; ++i)
5370 map = isl_map_add_basic_map(map,
5371 isl_basic_map_less_at(isl_space_copy(space), i));
5372 if (n > 0) {
5373 if (equal)
5374 map = isl_map_add_basic_map(map,
5375 isl_basic_map_less_or_equal_at(space, n - 1));
5376 else
5377 map = isl_map_add_basic_map(map,
5378 isl_basic_map_less_at(space, n - 1));
5379 } else
5380 isl_space_free(space);
5382 return map;
5385 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5387 if (!space)
5388 return NULL;
5389 return map_lex_lte_first(space, space->n_out, equal);
5392 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5393 unsigned n)
5395 return map_lex_lte_first(space, n, 0);
5398 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5399 unsigned n)
5401 return map_lex_lte_first(space, n, 1);
5404 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5406 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5409 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5411 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5414 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5415 unsigned n, int equal)
5417 struct isl_map *map;
5418 int i;
5420 if (n == 0 && equal)
5421 return isl_map_universe(space);
5423 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5425 for (i = 0; i + 1 < n; ++i)
5426 map = isl_map_add_basic_map(map,
5427 isl_basic_map_more_at(isl_space_copy(space), i));
5428 if (n > 0) {
5429 if (equal)
5430 map = isl_map_add_basic_map(map,
5431 isl_basic_map_more_or_equal_at(space, n - 1));
5432 else
5433 map = isl_map_add_basic_map(map,
5434 isl_basic_map_more_at(space, n - 1));
5435 } else
5436 isl_space_free(space);
5438 return map;
5441 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5443 if (!space)
5444 return NULL;
5445 return map_lex_gte_first(space, space->n_out, equal);
5448 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5449 unsigned n)
5451 return map_lex_gte_first(space, n, 0);
5454 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5455 unsigned n)
5457 return map_lex_gte_first(space, n, 1);
5460 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5462 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5465 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5467 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5470 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5471 __isl_take isl_set *set2)
5473 isl_map *map;
5474 map = isl_map_lex_le(isl_set_get_space(set1));
5475 map = isl_map_intersect_domain(map, set1);
5476 map = isl_map_intersect_range(map, set2);
5477 return map;
5480 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5481 __isl_take isl_set *set2)
5483 isl_map *map;
5484 map = isl_map_lex_lt(isl_set_get_space(set1));
5485 map = isl_map_intersect_domain(map, set1);
5486 map = isl_map_intersect_range(map, set2);
5487 return map;
5490 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5491 __isl_take isl_set *set2)
5493 isl_map *map;
5494 map = isl_map_lex_ge(isl_set_get_space(set1));
5495 map = isl_map_intersect_domain(map, set1);
5496 map = isl_map_intersect_range(map, set2);
5497 return map;
5500 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5501 __isl_take isl_set *set2)
5503 isl_map *map;
5504 map = isl_map_lex_gt(isl_set_get_space(set1));
5505 map = isl_map_intersect_domain(map, set1);
5506 map = isl_map_intersect_range(map, set2);
5507 return map;
5510 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5511 __isl_take isl_map *map2)
5513 isl_map *map;
5514 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5515 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5516 map = isl_map_apply_range(map, isl_map_reverse(map2));
5517 return map;
5520 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5521 __isl_take isl_map *map2)
5523 isl_map *map;
5524 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5525 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5526 map = isl_map_apply_range(map, isl_map_reverse(map2));
5527 return map;
5530 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5531 __isl_take isl_map *map2)
5533 isl_map *map;
5534 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5535 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5536 map = isl_map_apply_range(map, isl_map_reverse(map2));
5537 return map;
5540 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5541 __isl_take isl_map *map2)
5543 isl_map *map;
5544 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5545 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5546 map = isl_map_apply_range(map, isl_map_reverse(map2));
5547 return map;
5550 /* For the div d = floor(f/m) at position "div", add the constraint
5552 * f - m d >= 0
5554 static __isl_give isl_basic_map *add_upper_div_constraint(
5555 __isl_take isl_basic_map *bmap, unsigned div)
5557 int i;
5558 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5559 isl_size n_div;
5560 unsigned pos;
5562 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5563 if (v_div < 0 || n_div < 0)
5564 return isl_basic_map_free(bmap);
5565 pos = v_div + div;
5566 i = isl_basic_map_alloc_inequality(bmap);
5567 if (i < 0)
5568 return isl_basic_map_free(bmap);
5569 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5570 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5572 return bmap;
5575 /* For the div d = floor(f/m) at position "div", add the constraint
5577 * -(f-(m-1)) + m d >= 0
5579 static __isl_give isl_basic_map *add_lower_div_constraint(
5580 __isl_take isl_basic_map *bmap, unsigned div)
5582 int i;
5583 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5584 isl_size n_div;
5585 unsigned pos;
5587 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5588 if (v_div < 0 || n_div < 0)
5589 return isl_basic_map_free(bmap);
5590 pos = v_div + div;
5591 i = isl_basic_map_alloc_inequality(bmap);
5592 if (i < 0)
5593 return isl_basic_map_free(bmap);
5594 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5595 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5596 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5597 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5599 return bmap;
5602 /* For the div d = floor(f/m) at position "pos", add the constraints
5604 * f - m d >= 0
5605 * -(f-(m-1)) + m d >= 0
5607 * Note that the second constraint is the negation of
5609 * f - m d >= m
5611 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5612 __isl_take isl_basic_map *bmap, unsigned pos)
5614 bmap = add_upper_div_constraint(bmap, pos);
5615 bmap = add_lower_div_constraint(bmap, pos);
5616 return bmap;
5619 /* For each known div d = floor(f/m), add the constraints
5621 * f - m d >= 0
5622 * -(f-(m-1)) + m d >= 0
5624 * Remove duplicate constraints in case of some these div constraints
5625 * already appear in "bmap".
5627 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5628 __isl_take isl_basic_map *bmap)
5630 isl_size n_div;
5632 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5633 if (n_div < 0)
5634 return isl_basic_map_free(bmap);
5635 if (n_div == 0)
5636 return bmap;
5638 bmap = add_known_div_constraints(bmap);
5639 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5640 bmap = isl_basic_map_finalize(bmap);
5641 return bmap;
5644 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5646 * In particular, if this div is of the form d = floor(f/m),
5647 * then add the constraint
5649 * f - m d >= 0
5651 * if sign < 0 or the constraint
5653 * -(f-(m-1)) + m d >= 0
5655 * if sign > 0.
5657 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5658 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5660 if (sign < 0)
5661 return add_upper_div_constraint(bmap, div);
5662 else
5663 return add_lower_div_constraint(bmap, div);
5666 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5667 __isl_take isl_basic_map *bmap)
5669 isl_space *space;
5671 if (!bmap)
5672 goto error;
5673 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5674 bmap->n_div == 0 &&
5675 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5676 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5677 return bset_from_bmap(bmap);
5678 bmap = isl_basic_map_cow(bmap);
5679 if (!bmap)
5680 return NULL;
5681 space = isl_basic_map_take_space(bmap);
5682 space = isl_space_underlying(space, bmap->n_div);
5683 bmap = isl_basic_map_restore_space(bmap, space);
5684 if (!bmap)
5685 return NULL;
5686 bmap->extra -= bmap->n_div;
5687 bmap->n_div = 0;
5688 bmap = isl_basic_map_finalize(bmap);
5689 return bset_from_bmap(bmap);
5690 error:
5691 isl_basic_map_free(bmap);
5692 return NULL;
5695 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5696 __isl_take isl_basic_set *bset)
5698 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5701 /* Replace each element in "list" by the result of applying
5702 * isl_basic_map_underlying_set to the element.
5704 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5705 __isl_take isl_basic_map_list *list)
5707 int i;
5708 isl_size n;
5710 n = isl_basic_map_list_n_basic_map(list);
5711 if (n < 0)
5712 goto error;
5714 for (i = 0; i < n; ++i) {
5715 isl_basic_map *bmap;
5716 isl_basic_set *bset;
5718 bmap = isl_basic_map_list_get_basic_map(list, i);
5719 bset = isl_basic_set_underlying_set(bmap);
5720 list = isl_basic_set_list_set_basic_set(list, i, bset);
5723 return list;
5724 error:
5725 isl_basic_map_list_free(list);
5726 return NULL;
5729 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5730 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5732 struct isl_basic_map *bmap;
5733 struct isl_ctx *ctx;
5734 isl_size dim, bmap_total;
5735 unsigned total;
5736 int i;
5738 if (!bset || !like)
5739 goto error;
5740 ctx = bset->ctx;
5741 if (isl_basic_set_check_no_params(bset) < 0 ||
5742 isl_basic_set_check_no_locals(bset) < 0)
5743 goto error;
5744 dim = isl_basic_set_dim(bset, isl_dim_set);
5745 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5746 if (dim < 0 || bmap_total < 0)
5747 goto error;
5748 isl_assert(ctx, dim == bmap_total, goto error);
5749 if (like->n_div == 0) {
5750 isl_space *space = isl_basic_map_get_space(like);
5751 isl_basic_map_free(like);
5752 return isl_basic_map_reset_space(bset, space);
5754 bset = isl_basic_set_cow(bset);
5755 if (!bset)
5756 goto error;
5757 total = dim + bset->extra;
5758 bmap = bset_to_bmap(bset);
5759 isl_space_free(isl_basic_map_take_space(bmap));
5760 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5761 if (!bmap)
5762 goto error;
5763 bmap->n_div = like->n_div;
5764 bmap->extra += like->n_div;
5765 if (bmap->extra) {
5766 unsigned ltotal;
5767 isl_int **div;
5768 ltotal = total - bmap->extra + like->extra;
5769 if (ltotal > total)
5770 ltotal = total;
5771 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5772 bmap->extra * (1 + 1 + total));
5773 if (isl_blk_is_error(bmap->block2))
5774 goto error;
5775 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5776 if (!div)
5777 goto error;
5778 bmap->div = div;
5779 for (i = 0; i < bmap->extra; ++i)
5780 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5781 for (i = 0; i < like->n_div; ++i) {
5782 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5783 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5785 bmap = isl_basic_map_add_known_div_constraints(bmap);
5787 isl_basic_map_free(like);
5788 bmap = isl_basic_map_simplify(bmap);
5789 bmap = isl_basic_map_finalize(bmap);
5790 return bmap;
5791 error:
5792 isl_basic_map_free(like);
5793 isl_basic_set_free(bset);
5794 return NULL;
5797 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5798 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5800 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5801 bset_to_bmap(like)));
5804 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5806 int i;
5808 map = isl_map_cow(map);
5809 if (!map)
5810 return NULL;
5811 map->dim = isl_space_cow(map->dim);
5812 if (!map->dim)
5813 goto error;
5815 for (i = 1; i < map->n; ++i)
5816 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5817 goto error);
5818 for (i = 0; i < map->n; ++i) {
5819 map->p[i] = bset_to_bmap(
5820 isl_basic_map_underlying_set(map->p[i]));
5821 if (!map->p[i])
5822 goto error;
5824 if (map->n == 0)
5825 map->dim = isl_space_underlying(map->dim, 0);
5826 else {
5827 isl_space_free(map->dim);
5828 map->dim = isl_space_copy(map->p[0]->dim);
5830 if (!map->dim)
5831 goto error;
5832 return set_from_map(map);
5833 error:
5834 isl_map_free(map);
5835 return NULL;
5838 /* Replace the space of "bmap" by "space".
5840 * If the space of "bmap" is identical to "space" (including the identifiers
5841 * of the input and output dimensions), then simply return the original input.
5843 __isl_give isl_basic_map *isl_basic_map_reset_space(
5844 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5846 isl_bool equal;
5847 isl_space *bmap_space;
5849 bmap_space = isl_basic_map_peek_space(bmap);
5850 equal = isl_space_is_equal(bmap_space, space);
5851 if (equal >= 0 && equal)
5852 equal = isl_space_has_equal_ids(bmap_space, space);
5853 if (equal < 0)
5854 goto error;
5855 if (equal) {
5856 isl_space_free(space);
5857 return bmap;
5859 isl_space_free(isl_basic_map_take_space(bmap));
5860 bmap = isl_basic_map_restore_space(bmap, space);
5862 bmap = isl_basic_map_finalize(bmap);
5864 return bmap;
5865 error:
5866 isl_basic_map_free(bmap);
5867 isl_space_free(space);
5868 return NULL;
5871 __isl_give isl_basic_set *isl_basic_set_reset_space(
5872 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5874 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5875 space));
5878 /* Check that the total dimensions of "map" and "space" are the same.
5880 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5881 __isl_keep isl_space *space)
5883 isl_size dim1, dim2;
5885 dim1 = isl_map_dim(map, isl_dim_all);
5886 dim2 = isl_space_dim(space, isl_dim_all);
5887 if (dim1 < 0 || dim2 < 0)
5888 return isl_stat_error;
5889 if (dim1 == dim2)
5890 return isl_stat_ok;
5891 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5892 "total dimensions do not match", return isl_stat_error);
5895 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5896 __isl_take isl_space *space)
5898 int i;
5900 map = isl_map_cow(map);
5901 if (!map || !space)
5902 goto error;
5904 for (i = 0; i < map->n; ++i) {
5905 map->p[i] = isl_basic_map_reset_space(map->p[i],
5906 isl_space_copy(space));
5907 if (!map->p[i])
5908 goto error;
5910 isl_space_free(isl_map_take_space(map));
5911 map = isl_map_restore_space(map, space);
5913 return map;
5914 error:
5915 isl_map_free(map);
5916 isl_space_free(space);
5917 return NULL;
5920 /* Replace the space of "map" by "space", without modifying
5921 * the dimension of "map".
5923 * If the space of "map" is identical to "space" (including the identifiers
5924 * of the input and output dimensions), then simply return the original input.
5926 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5927 __isl_take isl_space *space)
5929 isl_bool equal;
5930 isl_space *map_space;
5932 map_space = isl_map_peek_space(map);
5933 equal = isl_space_is_equal(map_space, space);
5934 if (equal >= 0 && equal)
5935 equal = isl_space_has_equal_ids(map_space, space);
5936 if (equal < 0)
5937 goto error;
5938 if (equal) {
5939 isl_space_free(space);
5940 return map;
5942 if (check_map_space_equal_total_dim(map, space) < 0)
5943 goto error;
5944 return isl_map_reset_space(map, space);
5945 error:
5946 isl_map_free(map);
5947 isl_space_free(space);
5948 return NULL;
5951 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5952 __isl_take isl_space *space)
5954 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5957 /* Compute the parameter domain of the given basic set.
5959 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5961 isl_bool is_params;
5962 isl_space *space;
5963 isl_size n;
5965 is_params = isl_basic_set_is_params(bset);
5966 if (is_params < 0)
5967 return isl_basic_set_free(bset);
5968 if (is_params)
5969 return bset;
5971 n = isl_basic_set_dim(bset, isl_dim_set);
5972 if (n < 0)
5973 return isl_basic_set_free(bset);
5974 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5975 space = isl_basic_set_get_space(bset);
5976 space = isl_space_params(space);
5977 bset = isl_basic_set_reset_space(bset, space);
5978 return bset;
5981 /* Construct a zero-dimensional basic set with the given parameter domain.
5983 __isl_give isl_basic_set *isl_basic_set_from_params(
5984 __isl_take isl_basic_set *bset)
5986 isl_space *space;
5987 space = isl_basic_set_get_space(bset);
5988 space = isl_space_set_from_params(space);
5989 bset = isl_basic_set_reset_space(bset, space);
5990 return bset;
5993 /* Compute the parameter domain of the given set.
5995 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5997 return isl_map_params(set_to_map(set));
6000 /* Construct a zero-dimensional set with the given parameter domain.
6002 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6004 isl_space *space;
6005 space = isl_set_get_space(set);
6006 space = isl_space_set_from_params(space);
6007 set = isl_set_reset_space(set, space);
6008 return set;
6011 /* Compute the parameter domain of the given map.
6013 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6015 isl_space *space;
6016 isl_size n_in, n_out;
6018 n_in = isl_map_dim(map, isl_dim_in);
6019 n_out = isl_map_dim(map, isl_dim_out);
6020 if (n_in < 0 || n_out < 0)
6021 return isl_map_free(map);
6022 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6023 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6024 space = isl_map_get_space(map);
6025 space = isl_space_params(space);
6026 map = isl_map_reset_space(map, space);
6027 return map;
6030 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6032 isl_space *space;
6033 isl_size n_out;
6035 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6036 if (n_out < 0)
6037 return isl_basic_map_free(bmap);
6038 space = isl_space_domain(isl_basic_map_get_space(bmap));
6040 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6042 return isl_basic_map_reset_space(bmap, space);
6045 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6047 if (!bmap)
6048 return isl_bool_error;
6049 return isl_space_may_be_set(bmap->dim);
6052 /* Is this basic map actually a set?
6053 * Users should never call this function. Outside of isl,
6054 * the type should indicate whether something is a set or a map.
6056 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6058 if (!bmap)
6059 return isl_bool_error;
6060 return isl_space_is_set(bmap->dim);
6063 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6065 isl_bool is_set;
6067 is_set = isl_basic_map_is_set(bmap);
6068 if (is_set < 0)
6069 goto error;
6070 if (is_set)
6071 return bmap;
6072 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6073 error:
6074 isl_basic_map_free(bmap);
6075 return NULL;
6078 __isl_give isl_basic_map *isl_basic_map_domain_map(
6079 __isl_take isl_basic_map *bmap)
6081 int i;
6082 isl_space *space;
6083 isl_basic_map *domain;
6084 isl_size nparam, n_in, n_out;
6086 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6087 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6088 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6089 if (nparam < 0 || n_in < 0 || n_out < 0)
6090 return isl_basic_map_free(bmap);
6092 space = isl_basic_map_get_space(bmap);
6093 space = isl_space_from_range(isl_space_domain(space));
6094 domain = isl_basic_map_universe(space);
6096 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6097 bmap = isl_basic_map_apply_range(bmap, domain);
6098 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6100 for (i = 0; i < n_in; ++i)
6101 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6102 isl_dim_out, i);
6104 bmap = isl_basic_map_gauss(bmap, NULL);
6105 return isl_basic_map_finalize(bmap);
6108 __isl_give isl_basic_map *isl_basic_map_range_map(
6109 __isl_take isl_basic_map *bmap)
6111 int i;
6112 isl_space *space;
6113 isl_basic_map *range;
6114 isl_size nparam, n_in, n_out;
6116 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6117 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6118 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6119 if (nparam < 0 || n_in < 0 || n_out < 0)
6120 return isl_basic_map_free(bmap);
6122 space = isl_basic_map_get_space(bmap);
6123 space = isl_space_from_range(isl_space_range(space));
6124 range = isl_basic_map_universe(space);
6126 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6127 bmap = isl_basic_map_apply_range(bmap, range);
6128 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6130 for (i = 0; i < n_out; ++i)
6131 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6132 isl_dim_out, i);
6134 bmap = isl_basic_map_gauss(bmap, NULL);
6135 return isl_basic_map_finalize(bmap);
6138 int isl_map_may_be_set(__isl_keep isl_map *map)
6140 if (!map)
6141 return -1;
6142 return isl_space_may_be_set(map->dim);
6145 /* Is this map actually a set?
6146 * Users should never call this function. Outside of isl,
6147 * the type should indicate whether something is a set or a map.
6149 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6151 if (!map)
6152 return isl_bool_error;
6153 return isl_space_is_set(map->dim);
6156 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6158 isl_space *space;
6159 isl_size n_in;
6161 n_in = isl_map_dim(map, isl_dim_in);
6162 if (n_in < 0)
6163 return set_from_map(isl_map_free(map));
6164 space = isl_space_range(isl_map_get_space(map));
6166 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6168 return set_from_map(isl_map_reset_space(map, space));
6171 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6172 * to each of its basic maps.
6174 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6175 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6176 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6178 int i;
6179 isl_space *space;
6181 map = isl_map_cow(map);
6182 if (!map)
6183 return NULL;
6185 for (i = 0; i < map->n; ++i) {
6186 map->p[i] = fn_bmap(map->p[i]);
6187 if (!map->p[i])
6188 return isl_map_free(map);
6190 map = isl_map_unmark_normalized(map);
6192 space = isl_map_take_space(map);
6193 space = fn_space(space);
6194 map = isl_map_restore_space(map, space);
6196 return map;
6199 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6201 return isl_map_transform(map, &isl_space_domain_map,
6202 &isl_basic_map_domain_map);
6205 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6207 return isl_map_transform(map, &isl_space_range_map,
6208 &isl_basic_map_range_map);
6211 /* Given a wrapped map of the form A[B -> C],
6212 * return the map A[B -> C] -> B.
6214 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6216 isl_id *id;
6217 isl_map *map;
6219 if (!set)
6220 return NULL;
6221 if (!isl_set_has_tuple_id(set))
6222 return isl_map_domain_map(isl_set_unwrap(set));
6224 id = isl_set_get_tuple_id(set);
6225 map = isl_map_domain_map(isl_set_unwrap(set));
6226 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6228 return map;
6231 __isl_give isl_basic_map *isl_basic_map_from_domain(
6232 __isl_take isl_basic_set *bset)
6234 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6237 __isl_give isl_basic_map *isl_basic_map_from_range(
6238 __isl_take isl_basic_set *bset)
6240 isl_space *space;
6241 space = isl_basic_set_get_space(bset);
6242 space = isl_space_from_range(space);
6243 bset = isl_basic_set_reset_space(bset, space);
6244 return bset_to_bmap(bset);
6247 /* Create a relation with the given set as range.
6248 * The domain of the created relation is a zero-dimensional
6249 * flat anonymous space.
6251 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6253 isl_space *space;
6254 space = isl_set_get_space(set);
6255 space = isl_space_from_range(space);
6256 set = isl_set_reset_space(set, space);
6257 return set_to_map(set);
6260 /* Create a relation with the given set as domain.
6261 * The range of the created relation is a zero-dimensional
6262 * flat anonymous space.
6264 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6266 return isl_map_reverse(isl_map_from_range(set));
6269 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6270 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6272 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6275 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6276 __isl_take isl_set *range)
6278 return isl_map_apply_range(isl_map_reverse(domain), range);
6281 /* Return a newly allocated isl_map with given space and flags and
6282 * room for "n" basic maps.
6283 * Make sure that all cached information is cleared.
6285 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6286 unsigned flags)
6288 struct isl_map *map;
6290 if (!space)
6291 return NULL;
6292 if (n < 0)
6293 isl_die(space->ctx, isl_error_internal,
6294 "negative number of basic maps", goto error);
6295 map = isl_calloc(space->ctx, struct isl_map,
6296 sizeof(struct isl_map) +
6297 (n - 1) * sizeof(struct isl_basic_map *));
6298 if (!map)
6299 goto error;
6301 map->ctx = space->ctx;
6302 isl_ctx_ref(map->ctx);
6303 map->ref = 1;
6304 map->size = n;
6305 map->n = 0;
6306 map->dim = space;
6307 map->flags = flags;
6308 return map;
6309 error:
6310 isl_space_free(space);
6311 return NULL;
6314 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6316 struct isl_basic_map *bmap;
6317 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6318 bmap = isl_basic_map_set_to_empty(bmap);
6319 return bmap;
6322 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6324 struct isl_basic_set *bset;
6325 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6326 bset = isl_basic_set_set_to_empty(bset);
6327 return bset;
6330 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6332 struct isl_basic_map *bmap;
6333 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6334 bmap = isl_basic_map_finalize(bmap);
6335 return bmap;
6338 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6340 struct isl_basic_set *bset;
6341 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6342 bset = isl_basic_set_finalize(bset);
6343 return bset;
6346 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6347 __isl_take isl_space *space)
6349 int i;
6350 isl_size total = isl_space_dim(space, isl_dim_all);
6351 isl_basic_map *bmap;
6353 if (total < 0)
6354 space = isl_space_free(space);
6355 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6356 for (i = 0; i < total; ++i) {
6357 int k = isl_basic_map_alloc_inequality(bmap);
6358 if (k < 0)
6359 goto error;
6360 isl_seq_clr(bmap->ineq[k], 1 + total);
6361 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6363 return bmap;
6364 error:
6365 isl_basic_map_free(bmap);
6366 return NULL;
6369 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6370 __isl_take isl_space *space)
6372 return isl_basic_map_nat_universe(space);
6375 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6377 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6380 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6382 return isl_map_nat_universe(space);
6385 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6387 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6390 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6392 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6395 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6397 struct isl_map *map;
6398 if (!space)
6399 return NULL;
6400 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6401 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6402 return map;
6405 /* This function performs the same operation as isl_map_universe,
6406 * but is considered as a function on an isl_space when exported.
6408 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6410 return isl_map_universe(space);
6413 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6415 struct isl_set *set;
6416 if (!space)
6417 return NULL;
6418 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6419 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6420 return set;
6423 /* This function performs the same operation as isl_set_universe,
6424 * but is considered as a function on an isl_space when exported.
6426 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6428 return isl_set_universe(space);
6431 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6433 int i;
6434 struct isl_map *dup;
6436 if (!map)
6437 return NULL;
6438 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6439 for (i = 0; i < map->n; ++i)
6440 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6441 return dup;
6444 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6445 __isl_take isl_basic_map *bmap)
6447 if (!bmap || !map)
6448 goto error;
6449 if (isl_basic_map_plain_is_empty(bmap)) {
6450 isl_basic_map_free(bmap);
6451 return map;
6453 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6454 goto error;
6455 isl_assert(map->ctx, map->n < map->size, goto error);
6456 map->p[map->n] = bmap;
6457 map->n++;
6458 map = isl_map_unmark_normalized(map);
6459 return map;
6460 error:
6461 if (map)
6462 isl_map_free(map);
6463 if (bmap)
6464 isl_basic_map_free(bmap);
6465 return NULL;
6468 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6470 int i;
6472 if (!map)
6473 return NULL;
6475 if (--map->ref > 0)
6476 return NULL;
6478 clear_caches(map);
6479 isl_ctx_deref(map->ctx);
6480 for (i = 0; i < map->n; ++i)
6481 isl_basic_map_free(map->p[i]);
6482 isl_space_free(map->dim);
6483 free(map);
6485 return NULL;
6488 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6489 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6491 int j;
6492 isl_size total;
6494 total = isl_basic_map_dim(bmap, isl_dim_all);
6495 if (total < 0)
6496 return isl_basic_map_free(bmap);
6498 bmap = isl_basic_map_cow(bmap);
6499 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6500 j = isl_basic_map_alloc_equality(bmap);
6501 if (j < 0)
6502 goto error;
6503 isl_seq_clr(bmap->eq[j] + 1, total);
6504 isl_int_set_si(bmap->eq[j][pos], -1);
6505 isl_int_set_si(bmap->eq[j][0], value);
6506 bmap = isl_basic_map_simplify(bmap);
6507 return isl_basic_map_finalize(bmap);
6508 error:
6509 isl_basic_map_free(bmap);
6510 return NULL;
6513 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6514 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6516 int j;
6517 isl_size total;
6519 total = isl_basic_map_dim(bmap, isl_dim_all);
6520 if (total < 0)
6521 return isl_basic_map_free(bmap);
6523 bmap = isl_basic_map_cow(bmap);
6524 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6525 j = isl_basic_map_alloc_equality(bmap);
6526 if (j < 0)
6527 goto error;
6528 isl_seq_clr(bmap->eq[j] + 1, total);
6529 isl_int_set_si(bmap->eq[j][pos], -1);
6530 isl_int_set(bmap->eq[j][0], value);
6531 bmap = isl_basic_map_simplify(bmap);
6532 return isl_basic_map_finalize(bmap);
6533 error:
6534 isl_basic_map_free(bmap);
6535 return NULL;
6538 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6539 enum isl_dim_type type, unsigned pos, int value)
6541 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6542 return isl_basic_map_free(bmap);
6543 return isl_basic_map_fix_pos_si(bmap,
6544 isl_basic_map_offset(bmap, type) + pos, value);
6547 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6548 enum isl_dim_type type, unsigned pos, isl_int value)
6550 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6551 return isl_basic_map_free(bmap);
6552 return isl_basic_map_fix_pos(bmap,
6553 isl_basic_map_offset(bmap, type) + pos, value);
6556 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6557 * to be equal to "v".
6559 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6560 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6562 if (!bmap || !v)
6563 goto error;
6564 if (!isl_val_is_int(v))
6565 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6566 "expecting integer value", goto error);
6567 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6568 goto error;
6569 pos += isl_basic_map_offset(bmap, type);
6570 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6571 isl_val_free(v);
6572 return bmap;
6573 error:
6574 isl_basic_map_free(bmap);
6575 isl_val_free(v);
6576 return NULL;
6579 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6580 * to be equal to "v".
6582 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6583 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6585 return isl_basic_map_fix_val(bset, type, pos, v);
6588 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6589 enum isl_dim_type type, unsigned pos, int value)
6591 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6592 type, pos, value));
6595 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6596 enum isl_dim_type type, unsigned pos, isl_int value)
6598 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6599 type, pos, value));
6602 /* Remove the basic map at position "i" from "map" if this basic map
6603 * is (obviously) empty.
6605 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6607 isl_bool empty;
6609 if (!map)
6610 return NULL;
6612 empty = isl_basic_map_plain_is_empty(map->p[i]);
6613 if (empty < 0)
6614 return isl_map_free(map);
6615 if (!empty)
6616 return map;
6618 isl_basic_map_free(map->p[i]);
6619 map->n--;
6620 if (i != map->n) {
6621 map->p[i] = map->p[map->n];
6622 map = isl_map_unmark_normalized(map);
6626 return map;
6629 /* Perform "fn" on each basic map of "map", where we may not be holding
6630 * the only reference to "map".
6631 * In particular, "fn" should be a semantics preserving operation
6632 * that we want to apply to all copies of "map". We therefore need
6633 * to be careful not to modify "map" in a way that breaks "map"
6634 * in case anything goes wrong.
6636 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6637 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6639 struct isl_basic_map *bmap;
6640 int i;
6642 if (!map)
6643 return NULL;
6645 for (i = map->n - 1; i >= 0; --i) {
6646 bmap = isl_basic_map_copy(map->p[i]);
6647 bmap = fn(bmap);
6648 if (!bmap)
6649 goto error;
6650 isl_basic_map_free(map->p[i]);
6651 map->p[i] = bmap;
6652 map = remove_if_empty(map, i);
6653 if (!map)
6654 return NULL;
6657 return map;
6658 error:
6659 isl_map_free(map);
6660 return NULL;
6663 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6664 enum isl_dim_type type, unsigned pos, int value)
6666 int i;
6668 map = isl_map_cow(map);
6669 if (isl_map_check_range(map, type, pos, 1) < 0)
6670 return isl_map_free(map);
6671 for (i = map->n - 1; i >= 0; --i) {
6672 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6673 map = remove_if_empty(map, i);
6674 if (!map)
6675 return NULL;
6677 map = isl_map_unmark_normalized(map);
6678 return map;
6681 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6682 enum isl_dim_type type, unsigned pos, int value)
6684 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6687 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6688 enum isl_dim_type type, unsigned pos, isl_int value)
6690 int i;
6692 map = isl_map_cow(map);
6693 if (isl_map_check_range(map, type, pos, 1) < 0)
6694 return isl_map_free(map);
6695 for (i = 0; i < map->n; ++i) {
6696 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6697 if (!map->p[i])
6698 goto error;
6700 map = isl_map_unmark_normalized(map);
6701 return map;
6702 error:
6703 isl_map_free(map);
6704 return NULL;
6707 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6708 enum isl_dim_type type, unsigned pos, isl_int value)
6710 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6713 /* Fix the value of the variable at position "pos" of type "type" of "map"
6714 * to be equal to "v".
6716 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6717 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6719 int i;
6721 map = isl_map_cow(map);
6722 if (!map || !v)
6723 goto error;
6725 if (!isl_val_is_int(v))
6726 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6727 "expecting integer value", goto error);
6728 if (isl_map_check_range(map, type, pos, 1) < 0)
6729 goto error;
6730 for (i = map->n - 1; i >= 0; --i) {
6731 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6732 isl_val_copy(v));
6733 map = remove_if_empty(map, i);
6734 if (!map)
6735 goto error;
6737 map = isl_map_unmark_normalized(map);
6738 isl_val_free(v);
6739 return map;
6740 error:
6741 isl_map_free(map);
6742 isl_val_free(v);
6743 return NULL;
6746 /* Fix the value of the variable at position "pos" of type "type" of "set"
6747 * to be equal to "v".
6749 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6750 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6752 return isl_map_fix_val(set, type, pos, v);
6755 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6756 unsigned input, int value)
6758 return isl_map_fix_si(map, isl_dim_in, input, value);
6761 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6762 int value)
6764 return set_from_map(isl_map_fix_si(set_to_map(set),
6765 isl_dim_set, dim, value));
6768 static __isl_give isl_basic_map *basic_map_bound_si(
6769 __isl_take isl_basic_map *bmap,
6770 enum isl_dim_type type, unsigned pos, int value, int upper)
6772 int j;
6773 isl_size total;
6775 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6776 return isl_basic_map_free(bmap);
6777 total = isl_basic_map_dim(bmap, isl_dim_all);
6778 if (total < 0)
6779 return isl_basic_map_free(bmap);
6780 pos += isl_basic_map_offset(bmap, type);
6781 bmap = isl_basic_map_cow(bmap);
6782 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6783 j = isl_basic_map_alloc_inequality(bmap);
6784 if (j < 0)
6785 goto error;
6786 isl_seq_clr(bmap->ineq[j], 1 + total);
6787 if (upper) {
6788 isl_int_set_si(bmap->ineq[j][pos], -1);
6789 isl_int_set_si(bmap->ineq[j][0], value);
6790 } else {
6791 isl_int_set_si(bmap->ineq[j][pos], 1);
6792 isl_int_set_si(bmap->ineq[j][0], -value);
6794 bmap = isl_basic_map_simplify(bmap);
6795 return isl_basic_map_finalize(bmap);
6796 error:
6797 isl_basic_map_free(bmap);
6798 return NULL;
6801 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6802 __isl_take isl_basic_map *bmap,
6803 enum isl_dim_type type, unsigned pos, int value)
6805 return basic_map_bound_si(bmap, type, pos, value, 0);
6808 /* Constrain the values of the given dimension to be no greater than "value".
6810 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6811 __isl_take isl_basic_map *bmap,
6812 enum isl_dim_type type, unsigned pos, int value)
6814 return basic_map_bound_si(bmap, type, pos, value, 1);
6817 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6818 enum isl_dim_type type, unsigned pos, int value, int upper)
6820 int i;
6822 map = isl_map_cow(map);
6823 if (isl_map_check_range(map, type, pos, 1) < 0)
6824 return isl_map_free(map);
6825 for (i = 0; i < map->n; ++i) {
6826 map->p[i] = basic_map_bound_si(map->p[i],
6827 type, pos, value, upper);
6828 if (!map->p[i])
6829 goto error;
6831 map = isl_map_unmark_normalized(map);
6832 return map;
6833 error:
6834 isl_map_free(map);
6835 return NULL;
6838 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6839 enum isl_dim_type type, unsigned pos, int value)
6841 return map_bound_si(map, type, pos, value, 0);
6844 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6845 enum isl_dim_type type, unsigned pos, int value)
6847 return map_bound_si(map, type, pos, value, 1);
6850 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6851 enum isl_dim_type type, unsigned pos, int value)
6853 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6854 type, pos, value));
6857 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6858 enum isl_dim_type type, unsigned pos, int value)
6860 return isl_map_upper_bound_si(set, type, pos, value);
6863 /* Bound the given variable of "bmap" from below (or above is "upper"
6864 * is set) to "value".
6866 static __isl_give isl_basic_map *basic_map_bound(
6867 __isl_take isl_basic_map *bmap,
6868 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6870 int j;
6871 isl_size total;
6873 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6874 return isl_basic_map_free(bmap);
6875 total = isl_basic_map_dim(bmap, isl_dim_all);
6876 if (total < 0)
6877 return isl_basic_map_free(bmap);
6878 pos += isl_basic_map_offset(bmap, type);
6879 bmap = isl_basic_map_cow(bmap);
6880 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6881 j = isl_basic_map_alloc_inequality(bmap);
6882 if (j < 0)
6883 goto error;
6884 isl_seq_clr(bmap->ineq[j], 1 + total);
6885 if (upper) {
6886 isl_int_set_si(bmap->ineq[j][pos], -1);
6887 isl_int_set(bmap->ineq[j][0], value);
6888 } else {
6889 isl_int_set_si(bmap->ineq[j][pos], 1);
6890 isl_int_neg(bmap->ineq[j][0], value);
6892 bmap = isl_basic_map_simplify(bmap);
6893 return isl_basic_map_finalize(bmap);
6894 error:
6895 isl_basic_map_free(bmap);
6896 return NULL;
6899 /* Bound the given variable of "map" from below (or above is "upper"
6900 * is set) to "value".
6902 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6903 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6905 int i;
6907 map = isl_map_cow(map);
6908 if (isl_map_check_range(map, type, pos, 1) < 0)
6909 return isl_map_free(map);
6910 for (i = map->n - 1; i >= 0; --i) {
6911 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6912 map = remove_if_empty(map, i);
6913 if (!map)
6914 return NULL;
6916 map = isl_map_unmark_normalized(map);
6917 return map;
6920 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6921 enum isl_dim_type type, unsigned pos, isl_int value)
6923 return map_bound(map, type, pos, value, 0);
6926 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6927 enum isl_dim_type type, unsigned pos, isl_int value)
6929 return map_bound(map, type, pos, value, 1);
6932 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6933 enum isl_dim_type type, unsigned pos, isl_int value)
6935 return isl_map_lower_bound(set, type, pos, value);
6938 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6939 enum isl_dim_type type, unsigned pos, isl_int value)
6941 return isl_map_upper_bound(set, type, pos, value);
6944 /* Force the values of the variable at position "pos" of type "type" of "map"
6945 * to be no smaller than "value".
6947 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6948 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6950 if (!value)
6951 goto error;
6952 if (!isl_val_is_int(value))
6953 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6954 "expecting integer value", goto error);
6955 map = isl_map_lower_bound(map, type, pos, value->n);
6956 isl_val_free(value);
6957 return map;
6958 error:
6959 isl_val_free(value);
6960 isl_map_free(map);
6961 return NULL;
6964 /* Force the values of the variable at position "pos" of type "type" of "set"
6965 * to be no smaller than "value".
6967 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6968 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6970 isl_map *map;
6972 map = set_to_map(set);
6973 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6976 /* Force the values of the variable at position "pos" of type "type" of "map"
6977 * to be no greater than "value".
6979 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6980 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6982 if (!value)
6983 goto error;
6984 if (!isl_val_is_int(value))
6985 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6986 "expecting integer value", goto error);
6987 map = isl_map_upper_bound(map, type, pos, value->n);
6988 isl_val_free(value);
6989 return map;
6990 error:
6991 isl_val_free(value);
6992 isl_map_free(map);
6993 return NULL;
6996 /* Force the values of the variable at position "pos" of type "type" of "set"
6997 * to be no greater than "value".
6999 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7000 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7002 isl_map *map;
7004 map = set_to_map(set);
7005 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7008 /* If "mv" has an explicit domain, then intersect the domain of "map"
7009 * with this explicit domain.
7011 * An isl_multi_val object never has an explicit domain,
7012 * so simply return "map".
7014 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7015 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7017 return map;
7020 #undef BASE
7021 #define BASE val
7022 #include "isl_map_bound_templ.c"
7024 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7025 * for each set dimension, by treating the set as a map.
7027 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7028 __isl_take isl_multi_val *bound,
7029 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7030 unsigned pos, __isl_take isl_val *value))
7032 isl_map *map;
7034 map = set_to_map(set);
7035 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7038 #undef BASE
7039 #define BASE pw_aff
7040 #include "isl_map_bound_templ.c"
7042 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7043 * for each set dimension, by converting the set and the bound
7044 * to objects living in a map space.
7046 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7047 __isl_take isl_multi_pw_aff *bound,
7048 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7049 unsigned pos, __isl_take TYPE *value))
7051 isl_map *map;
7053 map = isl_map_from_range(set);
7054 bound = isl_multi_pw_aff_from_range(bound);
7055 map = map_bound_multi_pw_aff(map, bound, set_bound);
7056 return isl_map_range(map);
7059 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7060 * setting a bound on the given output dimension.
7062 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7063 unsigned pos, __isl_take isl_val *v)
7065 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7068 /* Force the values of the set dimensions of "set"
7069 * to be no smaller than the corresponding values in "lower".
7071 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7072 __isl_take isl_multi_val *lower)
7074 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7077 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7078 * setting a bound on the given output dimension.
7080 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7081 unsigned pos, __isl_take isl_val *v)
7083 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7086 /* Force the values of the set dimensions of "set"
7087 * to be no greater than the corresponding values in "upper".
7089 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7090 __isl_take isl_multi_val *upper)
7092 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7095 /* Force the symbolic constant expression "bound"
7096 * to satisfy the relation "order" with respect to
7097 * the output variable at position "pos" of "map".
7099 * Create an affine expression representing the output variable
7100 * in terms of the range and
7101 * compare it using "order" to "bound" (defined on the domain).
7102 * The result is a relation between elements in domain and range that
7103 * can be intersected with "map".
7105 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7106 unsigned pos, __isl_take isl_pw_aff *bound,
7107 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7108 __isl_take isl_pw_aff *pa2))
7110 isl_space *space;
7111 isl_local_space *ls;
7112 isl_pw_aff *var;
7114 space = isl_space_range(isl_map_get_space(map));
7115 ls = isl_local_space_from_space(space);
7116 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7117 map = isl_map_intersect(map, order(bound, var));
7118 return map;
7121 /* Force the values of the output variable at position "pos" of "map"
7122 * to be no smaller than the symbolic constant expression "lower".
7124 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7125 unsigned pos, __isl_take isl_pw_aff *lower)
7127 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7130 /* Force the values of the output variable at position "pos" of "map"
7131 * to be no greater than the symbolic constant expression "upper".
7133 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7134 unsigned pos, __isl_take isl_pw_aff *upper)
7136 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7139 /* Force the values of the set dimensions of "set"
7140 * to be no smaller than the corresponding constant symbolic expressions
7141 * in "lower".
7143 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7144 __isl_take isl_multi_pw_aff *lower)
7146 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7149 /* Force the values of the set dimensions of "set"
7150 * to be no greater than the corresponding constant symbolic expressions
7151 * in "upper".
7153 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7154 __isl_take isl_multi_pw_aff *upper)
7156 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7159 /* Force the values of the output dimensions of "map"
7160 * to be no smaller than the corresponding constant symbolic expressions
7161 * in "lower".
7163 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7164 __isl_take isl_multi_pw_aff *lower)
7166 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7169 /* Force the values of the output dimensions of "map"
7170 * to be no greater than the corresponding constant symbolic expressions
7171 * in "upper".
7173 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7174 __isl_take isl_multi_pw_aff *upper)
7176 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7179 /* Bound the given variable of "bset" from below (or above is "upper"
7180 * is set) to "value".
7182 static __isl_give isl_basic_set *isl_basic_set_bound(
7183 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7184 isl_int value, int upper)
7186 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7187 type, pos, value, upper));
7190 /* Bound the given variable of "bset" from below (or above is "upper"
7191 * is set) to "value".
7193 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7194 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7195 __isl_take isl_val *value, int upper)
7197 if (!value)
7198 goto error;
7199 if (!isl_val_is_int(value))
7200 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7201 "expecting integer value", goto error);
7202 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7203 isl_val_free(value);
7204 return bset;
7205 error:
7206 isl_val_free(value);
7207 isl_basic_set_free(bset);
7208 return NULL;
7211 /* Bound the given variable of "bset" from below to "value".
7213 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7214 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7215 __isl_take isl_val *value)
7217 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7220 /* Bound the given variable of "bset" from above to "value".
7222 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7223 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7224 __isl_take isl_val *value)
7226 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7229 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7231 return isl_map_transform(map, &isl_space_reverse,
7232 &isl_basic_map_reverse);
7235 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7237 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7239 return isl_map_transform(map, &isl_space_domain_reverse,
7240 &isl_basic_map_domain_reverse);
7243 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7245 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7247 return isl_map_transform(map, &isl_space_range_reverse,
7248 &isl_basic_map_range_reverse);
7251 /* Given a set (A -> B), return the corresponding set (B -> A).
7253 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7255 isl_map *map = set_to_map(set);
7257 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7258 &isl_basic_map_set_reverse);
7259 return set_from_map(map);
7262 #undef TYPE
7263 #define TYPE isl_pw_multi_aff
7264 #undef SUFFIX
7265 #define SUFFIX _pw_multi_aff
7266 #undef EMPTY
7267 #define EMPTY isl_pw_multi_aff_empty
7268 #undef ADD
7269 #define ADD isl_pw_multi_aff_union_add
7270 #include "isl_map_lexopt_templ.c"
7272 /* Given a map "map", compute the lexicographically minimal
7273 * (or maximal) image element for each domain element in dom,
7274 * in the form of an isl_pw_multi_aff.
7275 * If "empty" is not NULL, then set *empty to those elements in dom that
7276 * do not have an image element.
7277 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7278 * should be computed over the domain of "map". "empty" is also NULL
7279 * in this case.
7281 * We first compute the lexicographically minimal or maximal element
7282 * in the first basic map. This results in a partial solution "res"
7283 * and a subset "todo" of dom that still need to be handled.
7284 * We then consider each of the remaining maps in "map" and successively
7285 * update both "res" and "todo".
7286 * If "empty" is NULL, then the todo sets are not needed and therefore
7287 * also not computed.
7289 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7290 __isl_take isl_map *map, __isl_take isl_set *dom,
7291 __isl_give isl_set **empty, unsigned flags)
7293 int i;
7294 int full;
7295 isl_pw_multi_aff *res;
7296 isl_set *todo;
7298 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7299 if (!map || (!full && !dom))
7300 goto error;
7302 if (isl_map_plain_is_empty(map)) {
7303 if (empty)
7304 *empty = dom;
7305 else
7306 isl_set_free(dom);
7307 return isl_pw_multi_aff_from_map(map);
7310 res = basic_map_partial_lexopt_pw_multi_aff(
7311 isl_basic_map_copy(map->p[0]),
7312 isl_set_copy(dom), empty, flags);
7314 if (empty)
7315 todo = *empty;
7316 for (i = 1; i < map->n; ++i) {
7317 isl_pw_multi_aff *res_i;
7319 res_i = basic_map_partial_lexopt_pw_multi_aff(
7320 isl_basic_map_copy(map->p[i]),
7321 isl_set_copy(dom), empty, flags);
7323 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7324 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7325 else
7326 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7328 if (empty)
7329 todo = isl_set_intersect(todo, *empty);
7332 isl_set_free(dom);
7333 isl_map_free(map);
7335 if (empty)
7336 *empty = todo;
7338 return res;
7339 error:
7340 if (empty)
7341 *empty = NULL;
7342 isl_set_free(dom);
7343 isl_map_free(map);
7344 return NULL;
7347 #undef TYPE
7348 #define TYPE isl_map
7349 #undef SUFFIX
7350 #define SUFFIX
7351 #undef EMPTY
7352 #define EMPTY isl_map_empty
7353 #undef ADD
7354 #define ADD isl_map_union_disjoint
7355 #include "isl_map_lexopt_templ.c"
7357 /* Given a map "map", compute the lexicographically minimal
7358 * (or maximal) image element for each domain element in "dom",
7359 * in the form of an isl_map.
7360 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7361 * do not have an image element.
7362 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7363 * should be computed over the domain of "map". "empty" is also NULL
7364 * in this case.
7366 * If the input consists of more than one disjunct, then first
7367 * compute the desired result in the form of an isl_pw_multi_aff and
7368 * then convert that into an isl_map.
7370 * This function used to have an explicit implementation in terms
7371 * of isl_maps, but it would continually intersect the domains of
7372 * partial results with the complement of the domain of the next
7373 * partial solution, potentially leading to an explosion in the number
7374 * of disjuncts if there are several disjuncts in the input.
7375 * An even earlier implementation of this function would look for
7376 * better results in the domain of the partial result and for extra
7377 * results in the complement of this domain, which would lead to
7378 * even more splintering.
7380 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7381 __isl_take isl_map *map, __isl_take isl_set *dom,
7382 __isl_give isl_set **empty, unsigned flags)
7384 int full;
7385 struct isl_map *res;
7386 isl_pw_multi_aff *pma;
7388 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7389 if (!map || (!full && !dom))
7390 goto error;
7392 if (isl_map_plain_is_empty(map)) {
7393 if (empty)
7394 *empty = dom;
7395 else
7396 isl_set_free(dom);
7397 return map;
7400 if (map->n == 1) {
7401 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7402 dom, empty, flags);
7403 isl_map_free(map);
7404 return res;
7407 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7408 flags);
7409 return isl_map_from_pw_multi_aff_internal(pma);
7410 error:
7411 if (empty)
7412 *empty = NULL;
7413 isl_set_free(dom);
7414 isl_map_free(map);
7415 return NULL;
7418 __isl_give isl_map *isl_map_partial_lexmax(
7419 __isl_take isl_map *map, __isl_take isl_set *dom,
7420 __isl_give isl_set **empty)
7422 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7425 __isl_give isl_map *isl_map_partial_lexmin(
7426 __isl_take isl_map *map, __isl_take isl_set *dom,
7427 __isl_give isl_set **empty)
7429 return isl_map_partial_lexopt(map, dom, empty, 0);
7432 __isl_give isl_set *isl_set_partial_lexmin(
7433 __isl_take isl_set *set, __isl_take isl_set *dom,
7434 __isl_give isl_set **empty)
7436 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7437 dom, empty));
7440 __isl_give isl_set *isl_set_partial_lexmax(
7441 __isl_take isl_set *set, __isl_take isl_set *dom,
7442 __isl_give isl_set **empty)
7444 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7445 dom, empty));
7448 /* Compute the lexicographic minimum (or maximum if "flags" includes
7449 * ISL_OPT_MAX) of "bset" over its parametric domain.
7451 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7452 unsigned flags)
7454 return isl_basic_map_lexopt(bset, flags);
7457 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7459 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7462 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7464 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7467 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7469 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7472 /* Compute the lexicographic minimum of "bset" over its parametric domain
7473 * for the purpose of quantifier elimination.
7474 * That is, find an explicit representation for all the existentially
7475 * quantified variables in "bset" by computing their lexicographic
7476 * minimum.
7478 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7479 __isl_take isl_basic_set *bset)
7481 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7484 /* Given a basic map with one output dimension, compute the minimum or
7485 * maximum of that dimension as an isl_pw_aff.
7487 * Compute the optimum as a lexicographic optimum over the single
7488 * output dimension and extract the single isl_pw_aff from the result.
7490 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7491 int max)
7493 isl_pw_multi_aff *pma;
7494 isl_pw_aff *pwaff;
7496 bmap = isl_basic_map_copy(bmap);
7497 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7498 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7499 isl_pw_multi_aff_free(pma);
7501 return pwaff;
7504 /* Compute the minimum or maximum of the given output dimension
7505 * as a function of the parameters and the input dimensions,
7506 * but independently of the other output dimensions.
7508 * We first project out the other output dimension and then compute
7509 * the "lexicographic" maximum in each basic map, combining the results
7510 * using isl_pw_aff_union_max.
7512 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7513 int max)
7515 int i;
7516 isl_pw_aff *pwaff;
7517 isl_size n_out;
7519 n_out = isl_map_dim(map, isl_dim_out);
7520 if (n_out < 0)
7521 map = isl_map_free(map);
7522 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7523 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7524 if (!map)
7525 return NULL;
7527 if (map->n == 0) {
7528 isl_space *space = isl_map_get_space(map);
7529 isl_map_free(map);
7530 return isl_pw_aff_empty(space);
7533 pwaff = basic_map_dim_opt(map->p[0], max);
7534 for (i = 1; i < map->n; ++i) {
7535 isl_pw_aff *pwaff_i;
7537 pwaff_i = basic_map_dim_opt(map->p[i], max);
7538 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7541 isl_map_free(map);
7543 return pwaff;
7546 /* Compute the minimum of the given output dimension as a function of the
7547 * parameters and input dimensions, but independently of
7548 * the other output dimensions.
7550 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7552 return map_dim_opt(map, pos, 0);
7555 /* Compute the maximum of the given output dimension as a function of the
7556 * parameters and input dimensions, but independently of
7557 * the other output dimensions.
7559 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7561 return map_dim_opt(map, pos, 1);
7564 /* Compute the minimum or maximum of the given set dimension
7565 * as a function of the parameters,
7566 * but independently of the other set dimensions.
7568 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7569 int max)
7571 return map_dim_opt(set, pos, max);
7574 /* Compute the maximum of the given set dimension as a function of the
7575 * parameters, but independently of the other set dimensions.
7577 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7579 return set_dim_opt(set, pos, 1);
7582 /* Compute the minimum of the given set dimension as a function of the
7583 * parameters, but independently of the other set dimensions.
7585 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7587 return set_dim_opt(set, pos, 0);
7590 /* Apply a preimage specified by "mat" on the parameters of "bset".
7591 * bset is assumed to have only parameters and divs.
7593 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7594 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7596 isl_size nparam;
7598 nparam = isl_basic_set_dim(bset, isl_dim_param);
7599 if (nparam < 0 || !mat)
7600 goto error;
7602 bset->dim = isl_space_cow(bset->dim);
7603 if (!bset->dim)
7604 goto error;
7606 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7608 bset->dim->nparam = 0;
7609 bset->dim->n_out = nparam;
7610 bset = isl_basic_set_preimage(bset, mat);
7611 if (bset) {
7612 bset->dim->nparam = bset->dim->n_out;
7613 bset->dim->n_out = 0;
7615 return bset;
7616 error:
7617 isl_mat_free(mat);
7618 isl_basic_set_free(bset);
7619 return NULL;
7622 /* Apply a preimage specified by "mat" on the parameters of "set".
7623 * set is assumed to have only parameters and divs.
7625 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7626 __isl_take isl_mat *mat)
7628 isl_space *space;
7629 isl_size nparam;
7631 nparam = isl_set_dim(set, isl_dim_param);
7632 if (nparam < 0 || !mat)
7633 goto error;
7635 if (mat->n_row != 1 + nparam)
7636 isl_die(isl_set_get_ctx(set), isl_error_internal,
7637 "unexpected number of rows", goto error);
7639 space = isl_set_get_space(set);
7640 space = isl_space_move_dims(space, isl_dim_set, 0,
7641 isl_dim_param, 0, nparam);
7642 set = isl_set_reset_space(set, space);
7643 set = isl_set_preimage(set, mat);
7644 nparam = isl_set_dim(set, isl_dim_out);
7645 if (nparam < 0)
7646 set = isl_set_free(set);
7647 space = isl_set_get_space(set);
7648 space = isl_space_move_dims(space, isl_dim_param, 0,
7649 isl_dim_out, 0, nparam);
7650 set = isl_set_reset_space(set, space);
7651 return set;
7652 error:
7653 isl_mat_free(mat);
7654 isl_set_free(set);
7655 return NULL;
7658 /* Intersect the basic set "bset" with the affine space specified by the
7659 * equalities in "eq".
7661 static __isl_give isl_basic_set *basic_set_append_equalities(
7662 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7664 int i, k;
7665 unsigned len;
7667 if (!bset || !eq)
7668 goto error;
7670 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7671 if (!bset)
7672 goto error;
7674 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7675 for (i = 0; i < eq->n_row; ++i) {
7676 k = isl_basic_set_alloc_equality(bset);
7677 if (k < 0)
7678 goto error;
7679 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7680 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7682 isl_mat_free(eq);
7684 bset = isl_basic_set_gauss(bset, NULL);
7685 bset = isl_basic_set_finalize(bset);
7687 return bset;
7688 error:
7689 isl_mat_free(eq);
7690 isl_basic_set_free(bset);
7691 return NULL;
7694 /* Intersect the set "set" with the affine space specified by the
7695 * equalities in "eq".
7697 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7698 __isl_take isl_mat *eq)
7700 int i;
7702 if (!set || !eq)
7703 goto error;
7705 for (i = 0; i < set->n; ++i) {
7706 set->p[i] = basic_set_append_equalities(set->p[i],
7707 isl_mat_copy(eq));
7708 if (!set->p[i])
7709 goto error;
7711 isl_mat_free(eq);
7712 return set;
7713 error:
7714 isl_mat_free(eq);
7715 isl_set_free(set);
7716 return NULL;
7719 /* Given a basic set "bset" that only involves parameters and existentially
7720 * quantified variables, return the index of the first equality
7721 * that only involves parameters. If there is no such equality then
7722 * return bset->n_eq.
7724 * This function assumes that isl_basic_set_gauss has been called on "bset".
7726 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7728 int i, j;
7729 isl_size nparam, n_div;
7731 nparam = isl_basic_set_dim(bset, isl_dim_param);
7732 n_div = isl_basic_set_dim(bset, isl_dim_div);
7733 if (nparam < 0 || n_div < 0)
7734 return -1;
7736 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7737 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7738 ++i;
7741 return i;
7744 /* Compute an explicit representation for the existentially quantified
7745 * variables in "bset" by computing the "minimal value" of the set
7746 * variables. Since there are no set variables, the computation of
7747 * the minimal value essentially computes an explicit representation
7748 * of the non-empty part(s) of "bset".
7750 * The input only involves parameters and existentially quantified variables.
7751 * All equalities among parameters have been removed.
7753 * Since the existentially quantified variables in the result are in general
7754 * going to be different from those in the input, we first replace
7755 * them by the minimal number of variables based on their equalities.
7756 * This should simplify the parametric integer programming.
7758 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7760 isl_morph *morph1, *morph2;
7761 isl_set *set;
7762 isl_size n;
7764 if (!bset)
7765 return NULL;
7766 if (bset->n_eq == 0)
7767 return isl_basic_set_lexmin_compute_divs(bset);
7769 morph1 = isl_basic_set_parameter_compression(bset);
7770 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7771 bset = isl_basic_set_lift(bset);
7772 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7773 bset = isl_morph_basic_set(morph2, bset);
7774 n = isl_basic_set_dim(bset, isl_dim_set);
7775 if (n < 0)
7776 bset = isl_basic_set_free(bset);
7777 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7779 set = isl_basic_set_lexmin_compute_divs(bset);
7781 set = isl_morph_set(isl_morph_inverse(morph1), set);
7783 return set;
7786 /* Project the given basic set onto its parameter domain, possibly introducing
7787 * new, explicit, existential variables in the constraints.
7788 * The input has parameters and (possibly implicit) existential variables.
7789 * The output has the same parameters, but only
7790 * explicit existentially quantified variables.
7792 * The actual projection is performed by pip, but pip doesn't seem
7793 * to like equalities very much, so we first remove the equalities
7794 * among the parameters by performing a variable compression on
7795 * the parameters. Afterward, an inverse transformation is performed
7796 * and the equalities among the parameters are inserted back in.
7798 * The variable compression on the parameters may uncover additional
7799 * equalities that were only implicit before. We therefore check
7800 * if there are any new parameter equalities in the result and
7801 * if so recurse. The removal of parameter equalities is required
7802 * for the parameter compression performed by base_compute_divs.
7804 static __isl_give isl_set *parameter_compute_divs(
7805 __isl_take isl_basic_set *bset)
7807 int i;
7808 struct isl_mat *eq;
7809 struct isl_mat *T, *T2;
7810 struct isl_set *set;
7811 isl_size nparam;
7813 bset = isl_basic_set_cow(bset);
7814 if (!bset)
7815 return NULL;
7817 if (bset->n_eq == 0)
7818 return base_compute_divs(bset);
7820 bset = isl_basic_set_gauss(bset, NULL);
7821 if (!bset)
7822 return NULL;
7823 if (isl_basic_set_plain_is_empty(bset))
7824 return isl_set_from_basic_set(bset);
7826 i = first_parameter_equality(bset);
7827 if (i == bset->n_eq)
7828 return base_compute_divs(bset);
7830 nparam = isl_basic_set_dim(bset, isl_dim_param);
7831 if (nparam < 0)
7832 return isl_set_from_basic_set(isl_basic_set_free(bset));
7833 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7834 0, 1 + nparam);
7835 eq = isl_mat_cow(eq);
7836 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7837 if (T && T->n_col == 0) {
7838 isl_mat_free(T);
7839 isl_mat_free(T2);
7840 isl_mat_free(eq);
7841 bset = isl_basic_set_set_to_empty(bset);
7842 return isl_set_from_basic_set(bset);
7844 bset = basic_set_parameter_preimage(bset, T);
7846 i = first_parameter_equality(bset);
7847 if (!bset)
7848 set = NULL;
7849 else if (i == bset->n_eq)
7850 set = base_compute_divs(bset);
7851 else
7852 set = parameter_compute_divs(bset);
7853 set = set_parameter_preimage(set, T2);
7854 set = set_append_equalities(set, eq);
7855 return set;
7858 /* Insert the divs from "ls" before those of "bmap".
7860 * The number of columns is not changed, which means that the last
7861 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7862 * The caller is responsible for removing the same number of dimensions
7863 * from the space of "bmap".
7865 static __isl_give isl_basic_map *insert_divs_from_local_space(
7866 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7868 int i;
7869 isl_size n_div;
7870 int old_n_div;
7872 n_div = isl_local_space_dim(ls, isl_dim_div);
7873 if (n_div < 0)
7874 return isl_basic_map_free(bmap);
7875 if (n_div == 0)
7876 return bmap;
7878 old_n_div = bmap->n_div;
7879 bmap = insert_div_rows(bmap, n_div);
7880 if (!bmap)
7881 return NULL;
7883 for (i = 0; i < n_div; ++i) {
7884 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7885 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7888 return bmap;
7891 /* Replace the space of "bmap" by the space and divs of "ls".
7893 * If "ls" has any divs, then we simplify the result since we may
7894 * have discovered some additional equalities that could simplify
7895 * the div expressions.
7897 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7898 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7900 isl_size n_div;
7902 bmap = isl_basic_map_cow(bmap);
7903 n_div = isl_local_space_dim(ls, isl_dim_div);
7904 if (!bmap || n_div < 0)
7905 goto error;
7907 bmap = insert_divs_from_local_space(bmap, ls);
7908 if (!bmap)
7909 goto error;
7911 isl_space_free(bmap->dim);
7912 bmap->dim = isl_local_space_get_space(ls);
7913 if (!bmap->dim)
7914 goto error;
7916 isl_local_space_free(ls);
7917 if (n_div > 0)
7918 bmap = isl_basic_map_simplify(bmap);
7919 bmap = isl_basic_map_finalize(bmap);
7920 return bmap;
7921 error:
7922 isl_basic_map_free(bmap);
7923 isl_local_space_free(ls);
7924 return NULL;
7927 /* Replace the space of "map" by the space and divs of "ls".
7929 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7930 __isl_take isl_local_space *ls)
7932 int i;
7934 map = isl_map_cow(map);
7935 if (!map || !ls)
7936 goto error;
7938 for (i = 0; i < map->n; ++i) {
7939 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7940 isl_local_space_copy(ls));
7941 if (!map->p[i])
7942 goto error;
7944 isl_space_free(isl_map_take_space(map));
7945 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7947 isl_local_space_free(ls);
7948 return map;
7949 error:
7950 isl_local_space_free(ls);
7951 isl_map_free(map);
7952 return NULL;
7955 /* Compute an explicit representation for the existentially
7956 * quantified variables for which do not know any explicit representation yet.
7958 * We first sort the existentially quantified variables so that the
7959 * existentially quantified variables for which we already have an explicit
7960 * representation are placed before those for which we do not.
7961 * The input dimensions, the output dimensions and the existentially
7962 * quantified variables for which we already have an explicit
7963 * representation are then turned into parameters.
7964 * compute_divs returns a map with the same parameters and
7965 * no input or output dimensions and the dimension specification
7966 * is reset to that of the input, including the existentially quantified
7967 * variables for which we already had an explicit representation.
7969 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7971 struct isl_basic_set *bset;
7972 struct isl_set *set;
7973 struct isl_map *map;
7974 isl_space *space;
7975 isl_local_space *ls;
7976 isl_size nparam;
7977 isl_size n_in;
7978 isl_size n_out;
7979 int n_known;
7980 int i;
7982 bmap = isl_basic_map_sort_divs(bmap);
7983 bmap = isl_basic_map_cow(bmap);
7984 if (!bmap)
7985 return NULL;
7987 n_known = isl_basic_map_first_unknown_div(bmap);
7988 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7989 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7990 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7991 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7992 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7994 space = isl_space_set_alloc(bmap->ctx,
7995 nparam + n_in + n_out + n_known, 0);
7996 if (!space)
7997 goto error;
7999 ls = isl_basic_map_get_local_space(bmap);
8000 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8001 n_known, bmap->n_div - n_known);
8002 if (n_known > 0) {
8003 for (i = n_known; i < bmap->n_div; ++i)
8004 swap_div(bmap, i - n_known, i);
8005 bmap->n_div -= n_known;
8006 bmap->extra -= n_known;
8008 bmap = isl_basic_map_reset_space(bmap, space);
8009 bset = bset_from_bmap(bmap);
8011 set = parameter_compute_divs(bset);
8012 map = set_to_map(set);
8013 map = replace_space_by_local_space(map, ls);
8015 return map;
8016 error:
8017 isl_basic_map_free(bmap);
8018 return NULL;
8021 /* Remove the explicit representation of local variable "div",
8022 * if there is any.
8024 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8025 __isl_take isl_basic_map *bmap, int div)
8027 isl_bool unknown;
8029 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8030 if (unknown < 0)
8031 return isl_basic_map_free(bmap);
8032 if (unknown)
8033 return bmap;
8035 bmap = isl_basic_map_cow(bmap);
8036 if (!bmap)
8037 return NULL;
8038 isl_int_set_si(bmap->div[div][0], 0);
8039 return bmap;
8042 /* Is local variable "div" of "bmap" marked as not having an explicit
8043 * representation?
8044 * Note that even if "div" is not marked in this way and therefore
8045 * has an explicit representation, this representation may still
8046 * depend (indirectly) on other local variables that do not
8047 * have an explicit representation.
8049 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8050 int div)
8052 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8053 return isl_bool_error;
8054 return isl_int_is_zero(bmap->div[div][0]);
8057 /* Return the position of the first local variable that does not
8058 * have an explicit representation.
8059 * Return the total number of local variables if they all have
8060 * an explicit representation.
8061 * Return -1 on error.
8063 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8065 int i;
8067 if (!bmap)
8068 return -1;
8070 for (i = 0; i < bmap->n_div; ++i) {
8071 if (!isl_basic_map_div_is_known(bmap, i))
8072 return i;
8074 return bmap->n_div;
8077 /* Return the position of the first local variable that does not
8078 * have an explicit representation.
8079 * Return the total number of local variables if they all have
8080 * an explicit representation.
8081 * Return -1 on error.
8083 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8085 return isl_basic_map_first_unknown_div(bset);
8088 /* Does "bmap" have an explicit representation for all local variables?
8090 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8092 int first;
8093 isl_size n;
8095 n = isl_basic_map_dim(bmap, isl_dim_div);
8096 first = isl_basic_map_first_unknown_div(bmap);
8097 if (n < 0 || first < 0)
8098 return isl_bool_error;
8099 return first == n;
8102 /* Do all basic maps in "map" have an explicit representation
8103 * for all local variables?
8105 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8107 int i;
8109 if (!map)
8110 return isl_bool_error;
8112 for (i = 0; i < map->n; ++i) {
8113 int known = isl_basic_map_divs_known(map->p[i]);
8114 if (known <= 0)
8115 return known;
8118 return isl_bool_true;
8121 /* If bmap contains any unknown divs, then compute explicit
8122 * expressions for them. However, this computation may be
8123 * quite expensive, so first try to remove divs that aren't
8124 * strictly needed.
8126 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8128 int known;
8129 struct isl_map *map;
8131 known = isl_basic_map_divs_known(bmap);
8132 if (known < 0)
8133 goto error;
8134 if (known)
8135 return isl_map_from_basic_map(bmap);
8137 bmap = isl_basic_map_drop_redundant_divs(bmap);
8139 known = isl_basic_map_divs_known(bmap);
8140 if (known < 0)
8141 goto error;
8142 if (known)
8143 return isl_map_from_basic_map(bmap);
8145 map = compute_divs(bmap);
8146 return map;
8147 error:
8148 isl_basic_map_free(bmap);
8149 return NULL;
8152 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8154 int i;
8155 int known;
8156 struct isl_map *res;
8158 if (!map)
8159 return NULL;
8160 if (map->n == 0)
8161 return map;
8163 known = isl_map_divs_known(map);
8164 if (known < 0) {
8165 isl_map_free(map);
8166 return NULL;
8168 if (known)
8169 return map;
8171 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8172 for (i = 1 ; i < map->n; ++i) {
8173 struct isl_map *r2;
8174 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8175 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8176 res = isl_map_union_disjoint(res, r2);
8177 else
8178 res = isl_map_union(res, r2);
8180 isl_map_free(map);
8182 return res;
8185 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8187 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8190 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8192 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8195 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8197 isl_space *space;
8198 isl_size n_out;
8200 n_out = isl_map_dim(map, isl_dim_out);
8201 if (n_out < 0)
8202 return set_from_map(isl_map_free(map));
8203 space = isl_space_domain(isl_map_get_space(map));
8205 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8207 return set_from_map(isl_map_reset_space(map, space));
8210 /* Return the union of "map1" and "map2", where we assume for now that
8211 * "map1" and "map2" are disjoint. Note that the basic maps inside
8212 * "map1" or "map2" may not be disjoint from each other.
8213 * Also note that this function is also called from isl_map_union,
8214 * which takes care of handling the situation where "map1" and "map2"
8215 * may not be disjoint.
8217 * If one of the inputs is empty, we can simply return the other input.
8218 * Similarly, if one of the inputs is universal, then it is equal to the union.
8220 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8221 __isl_take isl_map *map2)
8223 int i;
8224 unsigned flags = 0;
8225 struct isl_map *map = NULL;
8226 int is_universe;
8228 if (isl_map_check_equal_space(map1, map2) < 0)
8229 goto error;
8231 if (map1->n == 0) {
8232 isl_map_free(map1);
8233 return map2;
8235 if (map2->n == 0) {
8236 isl_map_free(map2);
8237 return map1;
8240 is_universe = isl_map_plain_is_universe(map1);
8241 if (is_universe < 0)
8242 goto error;
8243 if (is_universe) {
8244 isl_map_free(map2);
8245 return map1;
8248 is_universe = isl_map_plain_is_universe(map2);
8249 if (is_universe < 0)
8250 goto error;
8251 if (is_universe) {
8252 isl_map_free(map1);
8253 return map2;
8256 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8257 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8258 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8260 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8261 map1->n + map2->n, flags);
8262 if (!map)
8263 goto error;
8264 for (i = 0; i < map1->n; ++i) {
8265 map = isl_map_add_basic_map(map,
8266 isl_basic_map_copy(map1->p[i]));
8267 if (!map)
8268 goto error;
8270 for (i = 0; i < map2->n; ++i) {
8271 map = isl_map_add_basic_map(map,
8272 isl_basic_map_copy(map2->p[i]));
8273 if (!map)
8274 goto error;
8276 isl_map_free(map1);
8277 isl_map_free(map2);
8278 return map;
8279 error:
8280 isl_map_free(map);
8281 isl_map_free(map1);
8282 isl_map_free(map2);
8283 return NULL;
8286 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8287 * guaranteed to be disjoint by the caller.
8289 * Note that this functions is called from within isl_map_make_disjoint,
8290 * so we have to be careful not to touch the constraints of the inputs
8291 * in any way.
8293 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8294 __isl_take isl_map *map2)
8296 isl_map_align_params_bin(&map1, &map2);
8297 return map_union_disjoint(map1, map2);
8300 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8301 * not be disjoint.
8303 * We currently simply call map_union_disjoint, the internal operation
8304 * of which does not really depend on the inputs being disjoint.
8305 * If the result contains more than one basic map, then we clear
8306 * the disjoint flag since the result may contain basic maps from
8307 * both inputs and these are not guaranteed to be disjoint.
8309 * As a special case, if "map1" and "map2" are obviously equal,
8310 * then we simply return "map1".
8312 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8313 __isl_take isl_map *map2)
8315 int equal;
8317 if (isl_map_align_params_bin(&map1, &map2) < 0)
8318 goto error;
8320 equal = isl_map_plain_is_equal(map1, map2);
8321 if (equal < 0)
8322 goto error;
8323 if (equal) {
8324 isl_map_free(map2);
8325 return map1;
8328 map1 = map_union_disjoint(map1, map2);
8329 if (!map1)
8330 return NULL;
8331 if (map1->n > 1)
8332 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8333 return map1;
8334 error:
8335 isl_map_free(map1);
8336 isl_map_free(map2);
8337 return NULL;
8340 __isl_give isl_set *isl_set_union_disjoint(
8341 __isl_take isl_set *set1, __isl_take isl_set *set2)
8343 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8344 set_to_map(set2)));
8347 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8348 __isl_take isl_set *set2)
8350 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8353 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8354 * the results in a map living in "space".
8356 * "map" and "set" are assumed to be compatible and non-NULL.
8358 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8359 __isl_take isl_space *space, __isl_take isl_set *set,
8360 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8361 __isl_take isl_basic_set *bset))
8363 unsigned flags = 0;
8364 struct isl_map *result;
8365 int i, j;
8367 if (isl_set_plain_is_universe(set)) {
8368 isl_set_free(set);
8369 return isl_map_reset_equal_dim_space(map, space);
8372 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8373 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8374 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8376 result = isl_map_alloc_space(space, map->n * set->n, flags);
8377 for (i = 0; result && i < map->n; ++i)
8378 for (j = 0; j < set->n; ++j) {
8379 result = isl_map_add_basic_map(result,
8380 fn(isl_basic_map_copy(map->p[i]),
8381 isl_basic_set_copy(set->p[j])));
8382 if (!result)
8383 break;
8386 isl_map_free(map);
8387 isl_set_free(set);
8388 return result;
8391 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8392 __isl_take isl_set *set)
8394 isl_bool ok;
8395 isl_space *space;
8397 isl_map_align_params_set(&map, &set);
8398 ok = isl_map_compatible_range(map, set);
8399 if (ok < 0)
8400 goto error;
8401 if (!ok)
8402 isl_die(set->ctx, isl_error_invalid,
8403 "incompatible spaces", goto error);
8405 space = isl_map_get_space(map);
8406 return map_intersect_set(map, space, set,
8407 &isl_basic_map_intersect_range);
8408 error:
8409 isl_map_free(map);
8410 isl_set_free(set);
8411 return NULL;
8414 /* Intersect the domain of "map" with "set".
8416 * If the domain dimensions of "map" do not have any identifiers,
8417 * then copy them over from "set".
8419 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8420 __isl_take isl_set *set)
8422 isl_bool ok;
8423 isl_space *space;
8425 isl_map_align_params_set(&map, &set);
8426 ok = isl_map_compatible_domain(map, set);
8427 if (ok < 0)
8428 goto error;
8429 if (!ok)
8430 isl_die(set->ctx, isl_error_invalid,
8431 "incompatible spaces", goto error);
8433 space = isl_map_get_space(map);
8434 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8435 isl_set_peek_space(set), isl_dim_set);
8436 return map_intersect_set(map, space, set,
8437 &isl_basic_map_intersect_domain);
8438 error:
8439 isl_map_free(map);
8440 isl_set_free(set);
8441 return NULL;
8444 #undef TYPE
8445 #define TYPE isl_map
8446 static
8447 #include "isl_copy_tuple_id_templ.c"
8449 /* Data structure that specifies how isl_map_intersect_factor
8450 * should operate.
8452 * "preserve_type" is the tuple where the factor differs from
8453 * the input map and of which the identifiers needs
8454 * to be preserved explicitly.
8455 * "other_factor" is used to extract the space of the other factor
8456 * from the space of the product ("map").
8457 * "product" is used to combine the given factor and a universe map
8458 * in the space returned by "other_factor" to produce a map
8459 * that lives in the same space as the input map.
8461 struct isl_intersect_factor_control {
8462 enum isl_dim_type preserve_type;
8463 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8464 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8465 __isl_take isl_map *other);
8468 /* Given a map "map" in some product space and a map "factor"
8469 * living in some factor space, return the intersection.
8471 * After aligning the parameters,
8472 * the map "factor" is first extended to a map living in the same space
8473 * as "map" and then a regular intersection is computed.
8475 * Note that the extension is computed as a product, which is anonymous
8476 * by default. If "map" has an identifier on the corresponding tuple,
8477 * then this identifier needs to be set on the product
8478 * before the intersection is computed.
8480 static __isl_give isl_map *isl_map_intersect_factor(
8481 __isl_take isl_map *map, __isl_take isl_map *factor,
8482 struct isl_intersect_factor_control *control)
8484 isl_bool equal;
8485 isl_space *space;
8486 isl_map *other, *product;
8488 equal = isl_map_has_equal_params(map, factor);
8489 if (equal < 0)
8490 goto error;
8491 if (!equal) {
8492 map = isl_map_align_params(map, isl_map_get_space(factor));
8493 factor = isl_map_align_params(factor, isl_map_get_space(map));
8496 space = isl_map_get_space(map);
8497 other = isl_map_universe(control->other_factor(space));
8498 product = control->product(factor, other);
8500 space = isl_map_peek_space(map);
8501 product = isl_map_copy_tuple_id(product, control->preserve_type,
8502 space, control->preserve_type);
8503 return map_intersect(map, product);
8504 error:
8505 isl_map_free(map);
8506 isl_map_free(factor);
8507 return NULL;
8510 /* Return the domain product of "map2" and "map1".
8512 static __isl_give isl_map *isl_map_reverse_domain_product(
8513 __isl_take isl_map *map1, __isl_take isl_map *map2)
8515 return isl_map_domain_product(map2, map1);
8518 /* Return the range product of "map2" and "map1".
8520 static __isl_give isl_map *isl_map_reverse_range_product(
8521 __isl_take isl_map *map1, __isl_take isl_map *map2)
8523 return isl_map_range_product(map2, map1);
8526 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8527 * in the space A -> C, return the intersection.
8529 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8530 __isl_take isl_map *map, __isl_take isl_map *factor)
8532 struct isl_intersect_factor_control control = {
8533 .preserve_type = isl_dim_in,
8534 .other_factor = isl_space_domain_factor_range,
8535 .product = isl_map_domain_product,
8538 return isl_map_intersect_factor(map, factor, &control);
8541 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8542 * in the space B -> C, return the intersection.
8544 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8545 __isl_take isl_map *map, __isl_take isl_map *factor)
8547 struct isl_intersect_factor_control control = {
8548 .preserve_type = isl_dim_in,
8549 .other_factor = isl_space_domain_factor_domain,
8550 .product = isl_map_reverse_domain_product,
8553 return isl_map_intersect_factor(map, factor, &control);
8556 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8557 * in the space A -> B, return the intersection.
8559 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8560 __isl_take isl_map *map, __isl_take isl_map *factor)
8562 struct isl_intersect_factor_control control = {
8563 .preserve_type = isl_dim_out,
8564 .other_factor = isl_space_range_factor_range,
8565 .product = isl_map_range_product,
8568 return isl_map_intersect_factor(map, factor, &control);
8571 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8572 * in the space A -> C, return the intersection.
8574 __isl_give isl_map *isl_map_intersect_range_factor_range(
8575 __isl_take isl_map *map, __isl_take isl_map *factor)
8577 struct isl_intersect_factor_control control = {
8578 .preserve_type = isl_dim_out,
8579 .other_factor = isl_space_range_factor_domain,
8580 .product = isl_map_reverse_range_product,
8583 return isl_map_intersect_factor(map, factor, &control);
8586 /* Given a set "set" in a space [A -> B] and a set "domain"
8587 * in the space A, return the intersection.
8589 * The set "domain" is first extended to a set living in the space
8590 * [A -> B] and then a regular intersection is computed.
8592 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8593 __isl_take isl_set *domain)
8595 struct isl_intersect_factor_control control = {
8596 .preserve_type = isl_dim_set,
8597 .other_factor = isl_space_factor_range,
8598 .product = isl_map_range_product,
8601 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8602 set_to_map(domain), &control));
8605 /* Given a set "set" in a space [A -> B] and a set "range"
8606 * in the space B, return the intersection.
8608 * The set "range" is first extended to a set living in the space
8609 * [A -> B] and then a regular intersection is computed.
8611 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8612 __isl_take isl_set *range)
8614 struct isl_intersect_factor_control control = {
8615 .preserve_type = isl_dim_set,
8616 .other_factor = isl_space_factor_domain,
8617 .product = isl_map_reverse_range_product,
8620 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8621 set_to_map(range), &control));
8624 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8625 * in the space A, return the intersection.
8627 * The set "domain" is extended to a set living in the space [A -> B] and
8628 * the domain of "map" is intersected with this set.
8630 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8631 __isl_take isl_map *map, __isl_take isl_set *domain)
8633 isl_space *space;
8634 isl_set *factor;
8636 isl_map_align_params_set(&map, &domain);
8637 space = isl_map_get_space(map);
8638 space = isl_space_domain_wrapped_range(space);
8639 factor = isl_set_universe(space);
8640 domain = isl_set_product(domain, factor);
8641 return isl_map_intersect_domain(map, domain);
8644 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8645 * in the space B, return the intersection.
8647 * The set "domain" is extended to a set living in the space [B -> C] and
8648 * the range of "map" is intersected with this set.
8650 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8651 __isl_take isl_map *map, __isl_take isl_set *domain)
8653 isl_space *space;
8654 isl_set *factor;
8656 isl_map_align_params_set(&map, &domain);
8657 space = isl_map_get_space(map);
8658 space = isl_space_range_wrapped_range(space);
8659 factor = isl_set_universe(space);
8660 domain = isl_set_product(domain, factor);
8661 return isl_map_intersect_range(map, domain);
8664 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8665 __isl_take isl_map *map2)
8667 if (isl_map_align_params_bin(&map1, &map2) < 0)
8668 goto error;
8669 map1 = isl_map_reverse(map1);
8670 map1 = isl_map_apply_range(map1, map2);
8671 return isl_map_reverse(map1);
8672 error:
8673 isl_map_free(map1);
8674 isl_map_free(map2);
8675 return NULL;
8678 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8679 __isl_take isl_map *map2)
8681 isl_space *space;
8682 struct isl_map *result;
8683 int i, j;
8685 if (isl_map_align_params_bin(&map1, &map2) < 0)
8686 goto error;
8688 space = isl_space_join(isl_space_copy(map1->dim),
8689 isl_space_copy(map2->dim));
8691 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8692 if (!result)
8693 goto error;
8694 for (i = 0; i < map1->n; ++i)
8695 for (j = 0; j < map2->n; ++j) {
8696 result = isl_map_add_basic_map(result,
8697 isl_basic_map_apply_range(
8698 isl_basic_map_copy(map1->p[i]),
8699 isl_basic_map_copy(map2->p[j])));
8700 if (!result)
8701 goto error;
8703 isl_map_free(map1);
8704 isl_map_free(map2);
8705 if (result && result->n <= 1)
8706 ISL_F_SET(result, ISL_MAP_DISJOINT);
8707 return result;
8708 error:
8709 isl_map_free(map1);
8710 isl_map_free(map2);
8711 return NULL;
8714 /* Is "bmap" a transformation, i.e.,
8715 * does it relate elements from the same space.
8717 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8719 isl_space *space;
8721 space = isl_basic_map_peek_space(bmap);
8722 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8725 /* Check that "bmap" is a transformation, i.e.,
8726 * that it relates elements from the same space.
8728 static isl_stat isl_basic_map_check_transformation(
8729 __isl_keep isl_basic_map *bmap)
8731 isl_bool equal;
8733 equal = isl_basic_map_is_transformation(bmap);
8734 if (equal < 0)
8735 return isl_stat_error;
8736 if (!equal)
8737 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8738 "domain and range don't match", return isl_stat_error);
8739 return isl_stat_ok;
8743 * returns range - domain
8745 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8747 isl_space *target_space;
8748 struct isl_basic_set *bset;
8749 isl_size dim;
8750 isl_size nparam;
8751 isl_size total;
8752 int i;
8754 if (isl_basic_map_check_transformation(bmap) < 0)
8755 return isl_basic_map_free(bmap);
8756 dim = isl_basic_map_dim(bmap, isl_dim_in);
8757 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8758 if (dim < 0 || nparam < 0)
8759 goto error;
8760 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8761 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8762 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8763 total = isl_basic_map_dim(bmap, isl_dim_all);
8764 if (total < 0)
8765 bmap = isl_basic_map_free(bmap);
8766 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8767 for (i = 0; i < dim; ++i) {
8768 int j = isl_basic_map_alloc_equality(bmap);
8769 if (j < 0) {
8770 bmap = isl_basic_map_free(bmap);
8771 break;
8773 isl_seq_clr(bmap->eq[j], 1 + total);
8774 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8775 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8776 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8778 bset = isl_basic_map_domain(bmap);
8779 bset = isl_basic_set_reset_space(bset, target_space);
8780 return bset;
8781 error:
8782 isl_basic_map_free(bmap);
8783 return NULL;
8786 /* Is the tuple of type "type1" of "map" the same as
8787 * the tuple of type "type2" of "space"?
8789 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8790 enum isl_dim_type type1, __isl_keep isl_space *space,
8791 enum isl_dim_type type2)
8793 isl_space *map_space;
8795 map_space = isl_map_peek_space(map);
8796 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8799 /* Is the tuple of type "type1" of "map1" the same as
8800 * the tuple of type "type2" of "map2"?
8802 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8803 enum isl_dim_type type1, __isl_keep isl_map *map2,
8804 enum isl_dim_type type2)
8806 isl_space *space1, *space2;
8808 space1 = isl_map_peek_space(map1);
8809 space2 = isl_map_peek_space(map2);
8810 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8813 /* Is the space of "obj" equal to "space", ignoring parameters?
8815 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8816 __isl_keep isl_space *space)
8818 isl_space *map_space;
8820 map_space = isl_map_peek_space(map);
8821 return isl_space_has_equal_tuples(map_space, space);
8824 /* Check that "map" is a transformation, i.e.,
8825 * that it relates elements from the same space.
8827 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8829 isl_bool equal;
8831 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8832 if (equal < 0)
8833 return isl_stat_error;
8834 if (!equal)
8835 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8836 "domain and range don't match", return isl_stat_error);
8837 return isl_stat_ok;
8841 * returns range - domain
8843 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8845 int i;
8846 isl_space *space;
8847 struct isl_set *result;
8849 if (isl_map_check_transformation(map) < 0)
8850 goto error;
8851 space = isl_map_get_space(map);
8852 space = isl_space_domain(space);
8853 result = isl_set_alloc_space(space, map->n, 0);
8854 if (!result)
8855 goto error;
8856 for (i = 0; i < map->n; ++i)
8857 result = isl_set_add_basic_set(result,
8858 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8859 isl_map_free(map);
8860 return result;
8861 error:
8862 isl_map_free(map);
8863 return NULL;
8867 * returns [domain -> range] -> range - domain
8869 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8870 __isl_take isl_basic_map *bmap)
8872 int i, k;
8873 isl_space *space;
8874 isl_basic_map *domain;
8875 isl_size nparam, n;
8876 isl_size total;
8878 if (isl_basic_map_check_transformation(bmap) < 0)
8879 return isl_basic_map_free(bmap);
8881 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8882 n = isl_basic_map_dim(bmap, isl_dim_in);
8883 if (nparam < 0 || n < 0)
8884 return isl_basic_map_free(bmap);
8886 space = isl_basic_map_get_space(bmap);
8887 space = isl_space_from_range(isl_space_domain(space));
8888 domain = isl_basic_map_universe(space);
8890 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8891 bmap = isl_basic_map_apply_range(bmap, domain);
8892 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8894 total = isl_basic_map_dim(bmap, isl_dim_all);
8895 if (total < 0)
8896 return isl_basic_map_free(bmap);
8898 for (i = 0; i < n; ++i) {
8899 k = isl_basic_map_alloc_equality(bmap);
8900 if (k < 0)
8901 goto error;
8902 isl_seq_clr(bmap->eq[k], 1 + total);
8903 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8904 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8905 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8908 bmap = isl_basic_map_gauss(bmap, NULL);
8909 return isl_basic_map_finalize(bmap);
8910 error:
8911 isl_basic_map_free(bmap);
8912 return NULL;
8916 * returns [domain -> range] -> range - domain
8918 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8920 if (isl_map_check_transformation(map) < 0)
8921 return isl_map_free(map);
8923 return isl_map_transform(map, &isl_space_range_map,
8924 &isl_basic_map_deltas_map);
8927 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8929 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8931 isl_space *space;
8932 isl_map *map;
8934 space = isl_space_map_from_set(isl_set_get_space(deltas));
8935 map = isl_map_deltas_map(isl_map_universe(space));
8936 map = isl_map_intersect_range(map, deltas);
8938 return isl_set_unwrap(isl_map_domain(map));
8941 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8943 isl_size n_in, n_out;
8945 n_in = isl_space_dim(space, isl_dim_in);
8946 n_out = isl_space_dim(space, isl_dim_out);
8947 if (n_in < 0 || n_out < 0)
8948 goto error;
8949 if (n_in != n_out)
8950 isl_die(space->ctx, isl_error_invalid,
8951 "number of input and output dimensions needs to be "
8952 "the same", goto error);
8953 return isl_basic_map_equal(space, n_in);
8954 error:
8955 isl_space_free(space);
8956 return NULL;
8959 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8961 return isl_map_from_basic_map(isl_basic_map_identity(space));
8964 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8966 isl_space *space = isl_set_get_space(set);
8967 isl_map *id;
8968 id = isl_map_identity(isl_space_map_from_set(space));
8969 return isl_map_intersect_range(id, set);
8972 /* Construct a basic set with all set dimensions having only non-negative
8973 * values.
8975 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8976 __isl_take isl_space *space)
8978 int i;
8979 isl_size nparam;
8980 isl_size dim;
8981 isl_size total;
8982 struct isl_basic_set *bset;
8984 nparam = isl_space_dim(space, isl_dim_param);
8985 dim = isl_space_dim(space, isl_dim_set);
8986 total = isl_space_dim(space, isl_dim_all);
8987 if (nparam < 0 || dim < 0 || total < 0)
8988 space = isl_space_free(space);
8989 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8990 if (!bset)
8991 return NULL;
8992 for (i = 0; i < dim; ++i) {
8993 int k = isl_basic_set_alloc_inequality(bset);
8994 if (k < 0)
8995 goto error;
8996 isl_seq_clr(bset->ineq[k], 1 + total);
8997 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8999 return bset;
9000 error:
9001 isl_basic_set_free(bset);
9002 return NULL;
9005 /* Construct the half-space x_pos >= 0.
9007 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9008 int pos)
9010 int k;
9011 isl_size total;
9012 isl_basic_set *nonneg;
9014 total = isl_space_dim(space, isl_dim_all);
9015 if (total < 0)
9016 space = isl_space_free(space);
9017 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9018 k = isl_basic_set_alloc_inequality(nonneg);
9019 if (k < 0)
9020 goto error;
9021 isl_seq_clr(nonneg->ineq[k], 1 + total);
9022 isl_int_set_si(nonneg->ineq[k][pos], 1);
9024 return isl_basic_set_finalize(nonneg);
9025 error:
9026 isl_basic_set_free(nonneg);
9027 return NULL;
9030 /* Construct the half-space x_pos <= -1.
9032 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9033 int pos)
9035 int k;
9036 isl_size total;
9037 isl_basic_set *neg;
9039 total = isl_space_dim(space, isl_dim_all);
9040 if (total < 0)
9041 space = isl_space_free(space);
9042 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9043 k = isl_basic_set_alloc_inequality(neg);
9044 if (k < 0)
9045 goto error;
9046 isl_seq_clr(neg->ineq[k], 1 + total);
9047 isl_int_set_si(neg->ineq[k][0], -1);
9048 isl_int_set_si(neg->ineq[k][pos], -1);
9050 return isl_basic_set_finalize(neg);
9051 error:
9052 isl_basic_set_free(neg);
9053 return NULL;
9056 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9057 enum isl_dim_type type, unsigned first, unsigned n)
9059 int i;
9060 unsigned offset;
9061 isl_basic_set *nonneg;
9062 isl_basic_set *neg;
9064 if (n == 0)
9065 return set;
9067 if (isl_set_check_range(set, type, first, n) < 0)
9068 return isl_set_free(set);
9070 offset = pos(set->dim, type);
9071 for (i = 0; i < n; ++i) {
9072 nonneg = nonneg_halfspace(isl_set_get_space(set),
9073 offset + first + i);
9074 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9076 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9079 return set;
9082 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9083 int len,
9084 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9085 void *user)
9087 isl_set *half;
9089 if (!set)
9090 return isl_stat_error;
9091 if (isl_set_plain_is_empty(set)) {
9092 isl_set_free(set);
9093 return isl_stat_ok;
9095 if (first == len)
9096 return fn(set, signs, user);
9098 signs[first] = 1;
9099 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9100 1 + first));
9101 half = isl_set_intersect(half, isl_set_copy(set));
9102 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9103 goto error;
9105 signs[first] = -1;
9106 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9107 1 + first));
9108 half = isl_set_intersect(half, set);
9109 return foreach_orthant(half, signs, first + 1, len, fn, user);
9110 error:
9111 isl_set_free(set);
9112 return isl_stat_error;
9115 /* Call "fn" on the intersections of "set" with each of the orthants
9116 * (except for obviously empty intersections). The orthant is identified
9117 * by the signs array, with each entry having value 1 or -1 according
9118 * to the sign of the corresponding variable.
9120 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9121 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9122 void *user)
9124 isl_size nparam;
9125 isl_size nvar;
9126 int *signs;
9127 isl_stat r;
9129 if (!set)
9130 return isl_stat_error;
9131 if (isl_set_plain_is_empty(set))
9132 return isl_stat_ok;
9134 nparam = isl_set_dim(set, isl_dim_param);
9135 nvar = isl_set_dim(set, isl_dim_set);
9136 if (nparam < 0 || nvar < 0)
9137 return isl_stat_error;
9139 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9141 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9142 fn, user);
9144 free(signs);
9146 return r;
9149 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9151 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9154 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9155 __isl_keep isl_basic_map *bmap2)
9157 isl_bool is_subset;
9158 struct isl_map *map1;
9159 struct isl_map *map2;
9161 if (!bmap1 || !bmap2)
9162 return isl_bool_error;
9164 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9165 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9167 is_subset = isl_map_is_subset(map1, map2);
9169 isl_map_free(map1);
9170 isl_map_free(map2);
9172 return is_subset;
9175 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9176 __isl_keep isl_basic_set *bset2)
9178 return isl_basic_map_is_subset(bset1, bset2);
9181 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9182 __isl_keep isl_basic_map *bmap2)
9184 isl_bool is_subset;
9186 if (!bmap1 || !bmap2)
9187 return isl_bool_error;
9188 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9189 if (is_subset != isl_bool_true)
9190 return is_subset;
9191 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9192 return is_subset;
9195 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9196 __isl_keep isl_basic_set *bset2)
9198 return isl_basic_map_is_equal(
9199 bset_to_bmap(bset1), bset_to_bmap(bset2));
9202 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9204 int i;
9205 int is_empty;
9207 if (!map)
9208 return isl_bool_error;
9209 for (i = 0; i < map->n; ++i) {
9210 is_empty = isl_basic_map_is_empty(map->p[i]);
9211 if (is_empty < 0)
9212 return isl_bool_error;
9213 if (!is_empty)
9214 return isl_bool_false;
9216 return isl_bool_true;
9219 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9221 return map ? map->n == 0 : isl_bool_error;
9224 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9226 return set ? set->n == 0 : isl_bool_error;
9229 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9231 return isl_map_is_empty(set_to_map(set));
9234 #undef TYPE
9235 #define TYPE isl_basic_map
9237 static
9238 #include "isl_type_has_equal_space_bin_templ.c"
9239 #include "isl_type_check_equal_space_templ.c"
9241 /* Check that "bset1" and "bset2" live in the same space,
9242 * reporting an error if they do not.
9244 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9245 __isl_keep isl_basic_set *bset2)
9247 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9248 bset_to_bmap(bset1));
9251 #undef TYPE
9252 #define TYPE isl_map
9254 #include "isl_type_has_equal_space_bin_templ.c"
9255 #include "isl_type_check_equal_space_templ.c"
9256 #include "isl_type_has_space_templ.c"
9258 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9259 __isl_keep isl_set *set2)
9261 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9264 #undef TYPE1
9265 #define TYPE1 isl_map
9266 #undef TYPE2
9267 #define TYPE2 isl_basic_map
9268 #undef TYPE_PAIR
9269 #define TYPE_PAIR isl_map_basic_map
9271 static
9272 #include "isl_type_has_equal_space_templ.c"
9273 #include "isl_type_check_equal_space_templ.c"
9275 /* Check that "set" and "bset" live in the same space,
9276 * reporting an error if they do not.
9278 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9279 __isl_keep isl_basic_set *bset)
9281 return isl_map_basic_map_check_equal_space(set_to_map(set),
9282 bset_to_bmap(bset));
9285 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9287 isl_bool is_subset;
9289 if (!map1 || !map2)
9290 return isl_bool_error;
9291 is_subset = isl_map_is_subset(map1, map2);
9292 if (is_subset != isl_bool_true)
9293 return is_subset;
9294 is_subset = isl_map_is_subset(map2, map1);
9295 return is_subset;
9298 /* Is "map1" equal to "map2"?
9300 * First check if they are obviously equal.
9301 * If not, then perform a more detailed analysis.
9303 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9305 isl_bool equal;
9307 equal = isl_map_plain_is_equal(map1, map2);
9308 if (equal < 0 || equal)
9309 return equal;
9310 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9313 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9314 __isl_keep isl_basic_map *bmap2)
9316 isl_bool is_subset;
9318 if (!bmap1 || !bmap2)
9319 return isl_bool_error;
9320 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9321 if (is_subset != isl_bool_true)
9322 return is_subset;
9323 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9324 return isl_bool_not(is_subset);
9327 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9328 __isl_keep isl_map *map2)
9330 isl_bool is_subset;
9332 if (!map1 || !map2)
9333 return isl_bool_error;
9334 is_subset = isl_map_is_subset(map1, map2);
9335 if (is_subset != isl_bool_true)
9336 return is_subset;
9337 is_subset = isl_map_is_subset(map2, map1);
9338 return isl_bool_not(is_subset);
9341 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9342 __isl_keep isl_set *set2)
9344 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9347 /* Is "bmap" obviously equal to the universe with the same space?
9349 * That is, does it not have any constraints?
9351 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9353 if (!bmap)
9354 return isl_bool_error;
9355 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9358 /* Is "bset" obviously equal to the universe with the same space?
9360 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9362 return isl_basic_map_plain_is_universe(bset);
9365 /* If "c" does not involve any existentially quantified variables,
9366 * then set *univ to false and abort
9368 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9370 isl_bool *univ = user;
9371 isl_size n;
9373 n = isl_constraint_dim(c, isl_dim_div);
9374 if (n < 0)
9375 c = isl_constraint_free(c);
9376 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9377 isl_constraint_free(c);
9378 if (*univ < 0 || !*univ)
9379 return isl_stat_error;
9380 return isl_stat_ok;
9383 /* Is "bmap" equal to the universe with the same space?
9385 * First check if it is obviously equal to the universe.
9386 * If not and if there are any constraints not involving
9387 * existentially quantified variables, then it is certainly
9388 * not equal to the universe.
9389 * Otherwise, check if the universe is a subset of "bmap".
9391 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9393 isl_size n_div;
9394 isl_bool univ;
9395 isl_basic_map *test;
9397 univ = isl_basic_map_plain_is_universe(bmap);
9398 if (univ < 0 || univ)
9399 return univ;
9400 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9401 if (n_div < 0)
9402 return isl_bool_error;
9403 if (n_div == 0)
9404 return isl_bool_false;
9405 univ = isl_bool_true;
9406 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9407 univ)
9408 return isl_bool_error;
9409 if (univ < 0 || !univ)
9410 return univ;
9411 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9412 univ = isl_basic_map_is_subset(test, bmap);
9413 isl_basic_map_free(test);
9414 return univ;
9417 /* Is "bset" equal to the universe with the same space?
9419 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9421 return isl_basic_map_is_universe(bset);
9424 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9426 int i;
9428 if (!map)
9429 return isl_bool_error;
9431 for (i = 0; i < map->n; ++i) {
9432 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9433 if (r < 0 || r)
9434 return r;
9437 return isl_bool_false;
9440 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9442 return isl_map_plain_is_universe(set_to_map(set));
9445 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9447 struct isl_basic_set *bset = NULL;
9448 struct isl_vec *sample = NULL;
9449 isl_bool empty, non_empty;
9451 if (!bmap)
9452 return isl_bool_error;
9454 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9455 return isl_bool_true;
9457 if (isl_basic_map_plain_is_universe(bmap))
9458 return isl_bool_false;
9460 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9461 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9462 copy = isl_basic_map_remove_redundancies(copy);
9463 empty = isl_basic_map_plain_is_empty(copy);
9464 isl_basic_map_free(copy);
9465 return empty;
9468 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9469 if (non_empty < 0)
9470 return isl_bool_error;
9471 if (non_empty)
9472 return isl_bool_false;
9473 isl_vec_free(bmap->sample);
9474 bmap->sample = NULL;
9475 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9476 if (!bset)
9477 return isl_bool_error;
9478 sample = isl_basic_set_sample_vec(bset);
9479 if (!sample)
9480 return isl_bool_error;
9481 empty = sample->size == 0;
9482 isl_vec_free(bmap->sample);
9483 bmap->sample = sample;
9484 if (empty)
9485 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9487 return empty;
9490 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9492 if (!bmap)
9493 return isl_bool_error;
9494 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9497 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9499 if (!bset)
9500 return isl_bool_error;
9501 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9504 /* Is "bmap" known to be non-empty?
9506 * That is, is the cached sample still valid?
9508 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9510 isl_size total;
9512 if (!bmap)
9513 return isl_bool_error;
9514 if (!bmap->sample)
9515 return isl_bool_false;
9516 total = isl_basic_map_dim(bmap, isl_dim_all);
9517 if (total < 0)
9518 return isl_bool_error;
9519 if (bmap->sample->size != 1 + total)
9520 return isl_bool_false;
9521 return isl_basic_map_contains(bmap, bmap->sample);
9524 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9526 return isl_basic_map_is_empty(bset_to_bmap(bset));
9529 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9530 __isl_take isl_basic_map *bmap2)
9532 struct isl_map *map;
9534 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9535 goto error;
9537 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9538 if (!map)
9539 goto error;
9540 map = isl_map_add_basic_map(map, bmap1);
9541 map = isl_map_add_basic_map(map, bmap2);
9542 return map;
9543 error:
9544 isl_basic_map_free(bmap1);
9545 isl_basic_map_free(bmap2);
9546 return NULL;
9549 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9550 __isl_take isl_basic_set *bset2)
9552 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9553 bset_to_bmap(bset2)));
9556 /* Order divs such that any div only depends on previous divs */
9557 __isl_give isl_basic_map *isl_basic_map_order_divs(
9558 __isl_take isl_basic_map *bmap)
9560 int i;
9561 isl_size off;
9563 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9564 if (off < 0)
9565 return isl_basic_map_free(bmap);
9567 for (i = 0; i < bmap->n_div; ++i) {
9568 int pos;
9569 if (isl_int_is_zero(bmap->div[i][0]))
9570 continue;
9571 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9572 bmap->n_div-i);
9573 if (pos == -1)
9574 continue;
9575 if (pos == 0)
9576 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9577 "integer division depends on itself",
9578 return isl_basic_map_free(bmap));
9579 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9580 if (!bmap)
9581 return NULL;
9582 --i;
9584 return bmap;
9587 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9589 int i;
9591 if (!map)
9592 return 0;
9594 for (i = 0; i < map->n; ++i) {
9595 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9596 if (!map->p[i])
9597 goto error;
9600 return map;
9601 error:
9602 isl_map_free(map);
9603 return NULL;
9606 /* Sort the local variables of "bset".
9608 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9609 __isl_take isl_basic_set *bset)
9611 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9614 /* Apply the expansion computed by isl_merge_divs.
9615 * The expansion itself is given by "exp" while the resulting
9616 * list of divs is given by "div".
9618 * Move the integer divisions of "bmap" into the right position
9619 * according to "exp" and then introduce the additional integer
9620 * divisions, adding div constraints.
9621 * The moving should be done first to avoid moving coefficients
9622 * in the definitions of the extra integer divisions.
9624 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9625 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9627 int i, j;
9628 int n_div;
9630 bmap = isl_basic_map_cow(bmap);
9631 if (!bmap || !div)
9632 goto error;
9634 if (div->n_row < bmap->n_div)
9635 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9636 "not an expansion", goto error);
9638 n_div = bmap->n_div;
9639 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9640 2 * (div->n_row - n_div));
9642 for (i = n_div; i < div->n_row; ++i)
9643 if (isl_basic_map_alloc_div(bmap) < 0)
9644 goto error;
9646 for (j = n_div - 1; j >= 0; --j) {
9647 if (exp[j] == j)
9648 break;
9649 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9650 if (!bmap)
9651 goto error;
9653 j = 0;
9654 for (i = 0; i < div->n_row; ++i) {
9655 if (j < n_div && exp[j] == i) {
9656 j++;
9657 } else {
9658 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9659 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9660 continue;
9661 bmap = isl_basic_map_add_div_constraints(bmap, i);
9662 if (!bmap)
9663 goto error;
9667 isl_mat_free(div);
9668 return bmap;
9669 error:
9670 isl_basic_map_free(bmap);
9671 isl_mat_free(div);
9672 return NULL;
9675 /* Apply the expansion computed by isl_merge_divs.
9676 * The expansion itself is given by "exp" while the resulting
9677 * list of divs is given by "div".
9679 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9680 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9682 return isl_basic_map_expand_divs(bset, div, exp);
9685 /* Look for a div in dst that corresponds to the div "div" in src.
9686 * The divs before "div" in src and dst are assumed to be the same.
9688 * Return the position of the corresponding div in dst
9689 * if there is one. Otherwise, return a position beyond the integer divisions.
9690 * Return -1 on error.
9692 static int find_div(__isl_keep isl_basic_map *dst,
9693 __isl_keep isl_basic_map *src, unsigned div)
9695 int i;
9696 isl_size n_div;
9697 isl_size v_div;
9699 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9700 n_div = isl_basic_map_dim(dst, isl_dim_div);
9701 if (n_div < 0 || v_div < 0)
9702 return -1;
9703 isl_assert(dst->ctx, div <= n_div, return -1);
9704 for (i = div; i < n_div; ++i)
9705 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9706 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9707 n_div - div) == -1)
9708 return i;
9709 return n_div;
9712 /* Align the divs of "dst" to those of "src", adding divs from "src"
9713 * if needed. That is, make sure that the first src->n_div divs
9714 * of the result are equal to those of src.
9715 * The integer division of "src" are assumed to be ordered.
9717 * The integer divisions are swapped into the right position
9718 * (possibly after adding them first). This may result
9719 * in the remaining integer divisions appearing in the wrong order,
9720 * i.e., with some integer division appearing before
9721 * some other integer division on which it depends.
9722 * The integer divisions therefore need to be ordered.
9723 * This will not affect the integer divisions aligned to those of "src",
9724 * since "src" is assumed to have ordered integer divisions.
9726 * The result is not finalized as by design it will have redundant
9727 * divs if any divs from "src" were copied.
9729 __isl_give isl_basic_map *isl_basic_map_align_divs(
9730 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9732 int i;
9733 isl_bool known;
9734 int extended;
9735 isl_size v_div;
9736 isl_size dst_n_div;
9738 if (!dst || !src)
9739 return isl_basic_map_free(dst);
9741 if (src->n_div == 0)
9742 return dst;
9744 known = isl_basic_map_divs_known(src);
9745 if (known < 0)
9746 return isl_basic_map_free(dst);
9747 if (!known)
9748 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9749 "some src divs are unknown",
9750 return isl_basic_map_free(dst));
9752 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9753 if (v_div < 0)
9754 return isl_basic_map_free(dst);
9756 extended = 0;
9757 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9758 if (dst_n_div < 0)
9759 dst = isl_basic_map_free(dst);
9760 for (i = 0; i < src->n_div; ++i) {
9761 int j = find_div(dst, src, i);
9762 if (j < 0)
9763 dst = isl_basic_map_free(dst);
9764 if (j == dst_n_div) {
9765 if (!extended) {
9766 int extra = src->n_div - i;
9767 dst = isl_basic_map_cow(dst);
9768 if (!dst)
9769 return isl_basic_map_free(dst);
9770 dst = isl_basic_map_extend(dst,
9771 extra, 0, 2 * extra);
9772 extended = 1;
9774 j = isl_basic_map_alloc_div(dst);
9775 if (j < 0)
9776 return isl_basic_map_free(dst);
9777 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9778 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9779 dst_n_div++;
9780 dst = isl_basic_map_add_div_constraints(dst, j);
9781 if (!dst)
9782 return isl_basic_map_free(dst);
9784 if (j != i)
9785 dst = isl_basic_map_swap_div(dst, i, j);
9786 if (!dst)
9787 return isl_basic_map_free(dst);
9789 return isl_basic_map_order_divs(dst);
9792 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9794 int i;
9796 if (!map)
9797 return NULL;
9798 if (map->n == 0)
9799 return map;
9800 map = isl_map_compute_divs(map);
9801 map = isl_map_order_divs(map);
9802 map = isl_map_cow(map);
9803 if (!map)
9804 return NULL;
9806 for (i = 1; i < map->n; ++i)
9807 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9808 for (i = 1; i < map->n; ++i) {
9809 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9810 if (!map->p[i])
9811 return isl_map_free(map);
9814 map = isl_map_unmark_normalized(map);
9815 return map;
9818 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9820 return isl_map_align_divs_internal(map);
9823 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9825 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9828 /* Align the divs of the basic maps in "map" to those
9829 * of the basic maps in "list", as well as to the other basic maps in "map".
9830 * The elements in "list" are assumed to have known divs.
9832 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9833 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9835 int i;
9836 isl_size n;
9838 n = isl_basic_map_list_n_basic_map(list);
9839 map = isl_map_compute_divs(map);
9840 map = isl_map_cow(map);
9841 if (!map || n < 0)
9842 return isl_map_free(map);
9843 if (map->n == 0)
9844 return map;
9846 for (i = 0; i < n; ++i) {
9847 isl_basic_map *bmap;
9849 bmap = isl_basic_map_list_get_basic_map(list, i);
9850 bmap = isl_basic_map_order_divs(bmap);
9851 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9852 isl_basic_map_free(bmap);
9854 if (!map->p[0])
9855 return isl_map_free(map);
9857 return isl_map_align_divs_internal(map);
9860 /* Align the divs of each element of "list" to those of "bmap".
9861 * Both "bmap" and the elements of "list" are assumed to have known divs.
9863 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9864 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9866 int i;
9867 isl_size n;
9869 n = isl_basic_map_list_n_basic_map(list);
9870 if (n < 0 || !bmap)
9871 return isl_basic_map_list_free(list);
9873 for (i = 0; i < n; ++i) {
9874 isl_basic_map *bmap_i;
9876 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9877 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9878 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9881 return list;
9884 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9885 __isl_take isl_map *map)
9887 isl_bool ok;
9889 isl_map_align_params_set(&map, &set);
9890 ok = isl_map_compatible_domain(map, set);
9891 if (ok < 0)
9892 goto error;
9893 if (!ok)
9894 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9895 "incompatible spaces", goto error);
9896 map = isl_map_intersect_domain(map, set);
9897 set = isl_map_range(map);
9898 return set;
9899 error:
9900 isl_set_free(set);
9901 isl_map_free(map);
9902 return NULL;
9905 /* There is no need to cow as removing empty parts doesn't change
9906 * the meaning of the set.
9908 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9910 int i;
9912 if (!map)
9913 return NULL;
9915 for (i = map->n - 1; i >= 0; --i)
9916 map = remove_if_empty(map, i);
9918 return map;
9921 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9923 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9926 /* Create a binary relation that maps the shared initial "pos" dimensions
9927 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9929 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9930 __isl_keep isl_basic_set *bset2, int pos)
9932 isl_basic_map *bmap1;
9933 isl_basic_map *bmap2;
9935 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9936 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9937 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9938 isl_dim_out, 0, pos);
9939 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9940 isl_dim_out, 0, pos);
9941 return isl_basic_map_range_product(bmap1, bmap2);
9944 /* Given two basic sets bset1 and bset2, compute the maximal difference
9945 * between the values of dimension pos in bset1 and those in bset2
9946 * for any common value of the parameters and dimensions preceding pos.
9948 static enum isl_lp_result basic_set_maximal_difference_at(
9949 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9950 int pos, isl_int *opt)
9952 isl_basic_map *bmap1;
9953 struct isl_ctx *ctx;
9954 struct isl_vec *obj;
9955 isl_size total;
9956 isl_size nparam;
9957 isl_size dim1;
9958 enum isl_lp_result res;
9960 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9961 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9962 if (nparam < 0 || dim1 < 0 || !bset2)
9963 return isl_lp_error;
9965 bmap1 = join_initial(bset1, bset2, pos);
9966 total = isl_basic_map_dim(bmap1, isl_dim_all);
9967 if (total < 0)
9968 return isl_lp_error;
9970 ctx = bmap1->ctx;
9971 obj = isl_vec_alloc(ctx, 1 + total);
9972 if (!obj)
9973 goto error;
9974 isl_seq_clr(obj->block.data, 1 + total);
9975 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9976 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9977 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9978 opt, NULL, NULL);
9979 isl_basic_map_free(bmap1);
9980 isl_vec_free(obj);
9981 return res;
9982 error:
9983 isl_basic_map_free(bmap1);
9984 return isl_lp_error;
9987 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9988 * for any common value of the parameters and dimensions preceding pos
9989 * in both basic sets, the values of dimension pos in bset1 are
9990 * smaller or larger than those in bset2.
9992 * Returns
9993 * 1 if bset1 follows bset2
9994 * -1 if bset1 precedes bset2
9995 * 0 if bset1 and bset2 are incomparable
9996 * -2 if some error occurred.
9998 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9999 __isl_keep isl_basic_set *bset2, int pos)
10001 isl_int opt;
10002 enum isl_lp_result res;
10003 int cmp;
10005 isl_int_init(opt);
10007 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10009 if (res == isl_lp_empty)
10010 cmp = 0;
10011 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10012 res == isl_lp_unbounded)
10013 cmp = 1;
10014 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10015 cmp = -1;
10016 else
10017 cmp = -2;
10019 isl_int_clear(opt);
10020 return cmp;
10023 /* Given two basic sets bset1 and bset2, check whether
10024 * for any common value of the parameters and dimensions preceding pos
10025 * there is a value of dimension pos in bset1 that is larger
10026 * than a value of the same dimension in bset2.
10028 * Return
10029 * 1 if there exists such a pair
10030 * 0 if there is no such pair, but there is a pair of equal values
10031 * -1 otherwise
10032 * -2 if some error occurred.
10034 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10035 __isl_keep isl_basic_set *bset2, int pos)
10037 isl_bool empty;
10038 isl_basic_map *bmap;
10039 isl_size dim1;
10041 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10042 if (dim1 < 0)
10043 return -2;
10044 bmap = join_initial(bset1, bset2, pos);
10045 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10046 isl_dim_out, dim1 - pos);
10047 empty = isl_basic_map_is_empty(bmap);
10048 if (empty < 0)
10049 goto error;
10050 if (empty) {
10051 isl_basic_map_free(bmap);
10052 return -1;
10054 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10055 isl_dim_out, dim1 - pos);
10056 empty = isl_basic_map_is_empty(bmap);
10057 if (empty < 0)
10058 goto error;
10059 isl_basic_map_free(bmap);
10060 if (empty)
10061 return 0;
10062 return 1;
10063 error:
10064 isl_basic_map_free(bmap);
10065 return -2;
10068 /* Given two sets set1 and set2, check whether
10069 * for any common value of the parameters and dimensions preceding pos
10070 * there is a value of dimension pos in set1 that is larger
10071 * than a value of the same dimension in set2.
10073 * Return
10074 * 1 if there exists such a pair
10075 * 0 if there is no such pair, but there is a pair of equal values
10076 * -1 otherwise
10077 * -2 if some error occurred.
10079 int isl_set_follows_at(__isl_keep isl_set *set1,
10080 __isl_keep isl_set *set2, int pos)
10082 int i, j;
10083 int follows = -1;
10085 if (!set1 || !set2)
10086 return -2;
10088 for (i = 0; i < set1->n; ++i)
10089 for (j = 0; j < set2->n; ++j) {
10090 int f;
10091 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10092 if (f == 1 || f == -2)
10093 return f;
10094 if (f > follows)
10095 follows = f;
10098 return follows;
10101 static isl_bool isl_basic_map_plain_has_fixed_var(
10102 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10104 int i;
10105 int d;
10106 isl_size total;
10108 total = isl_basic_map_dim(bmap, isl_dim_all);
10109 if (total < 0)
10110 return isl_bool_error;
10111 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10112 for (; d+1 > pos; --d)
10113 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10114 break;
10115 if (d != pos)
10116 continue;
10117 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10118 return isl_bool_false;
10119 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10120 return isl_bool_false;
10121 if (!isl_int_is_one(bmap->eq[i][1+d]))
10122 return isl_bool_false;
10123 if (val)
10124 isl_int_neg(*val, bmap->eq[i][0]);
10125 return isl_bool_true;
10127 return isl_bool_false;
10130 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10131 unsigned pos, isl_int *val)
10133 int i;
10134 isl_int v;
10135 isl_int tmp;
10136 isl_bool fixed;
10138 if (!map)
10139 return isl_bool_error;
10140 if (map->n == 0)
10141 return isl_bool_false;
10142 if (map->n == 1)
10143 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10144 isl_int_init(v);
10145 isl_int_init(tmp);
10146 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10147 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10148 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10149 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10150 fixed = isl_bool_false;
10152 if (val)
10153 isl_int_set(*val, v);
10154 isl_int_clear(tmp);
10155 isl_int_clear(v);
10156 return fixed;
10159 static isl_bool isl_basic_set_plain_has_fixed_var(
10160 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10162 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10163 pos, val);
10166 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10167 enum isl_dim_type type, unsigned pos, isl_int *val)
10169 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10170 return isl_bool_error;
10171 return isl_basic_map_plain_has_fixed_var(bmap,
10172 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10175 /* If "bmap" obviously lies on a hyperplane where the given dimension
10176 * has a fixed value, then return that value.
10177 * Otherwise return NaN.
10179 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10180 __isl_keep isl_basic_map *bmap,
10181 enum isl_dim_type type, unsigned pos)
10183 isl_ctx *ctx;
10184 isl_val *v;
10185 isl_bool fixed;
10187 if (!bmap)
10188 return NULL;
10189 ctx = isl_basic_map_get_ctx(bmap);
10190 v = isl_val_alloc(ctx);
10191 if (!v)
10192 return NULL;
10193 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10194 if (fixed < 0)
10195 return isl_val_free(v);
10196 if (fixed) {
10197 isl_int_set_si(v->d, 1);
10198 return v;
10200 isl_val_free(v);
10201 return isl_val_nan(ctx);
10204 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10205 enum isl_dim_type type, unsigned pos, isl_int *val)
10207 if (isl_map_check_range(map, type, pos, 1) < 0)
10208 return isl_bool_error;
10209 return isl_map_plain_has_fixed_var(map,
10210 map_offset(map, type) - 1 + pos, val);
10213 /* If "map" obviously lies on a hyperplane where the given dimension
10214 * has a fixed value, then return that value.
10215 * Otherwise return NaN.
10217 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10218 enum isl_dim_type type, unsigned pos)
10220 isl_ctx *ctx;
10221 isl_val *v;
10222 isl_bool fixed;
10224 if (!map)
10225 return NULL;
10226 ctx = isl_map_get_ctx(map);
10227 v = isl_val_alloc(ctx);
10228 if (!v)
10229 return NULL;
10230 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10231 if (fixed < 0)
10232 return isl_val_free(v);
10233 if (fixed) {
10234 isl_int_set_si(v->d, 1);
10235 return v;
10237 isl_val_free(v);
10238 return isl_val_nan(ctx);
10241 /* If "set" obviously lies on a hyperplane where the given dimension
10242 * has a fixed value, then return that value.
10243 * Otherwise return NaN.
10245 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10246 enum isl_dim_type type, unsigned pos)
10248 return isl_map_plain_get_val_if_fixed(set, type, pos);
10251 /* Return a sequence of values in the same space as "set"
10252 * that are equal to the corresponding set dimensions of "set"
10253 * for those set dimensions that obviously lie on a hyperplane
10254 * where the dimension has a fixed value.
10255 * The other elements are set to NaN.
10257 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10258 __isl_keep isl_set *set)
10260 int i;
10261 isl_size n;
10262 isl_space *space;
10263 isl_multi_val *mv;
10265 space = isl_space_drop_all_params(isl_set_get_space(set));
10266 mv = isl_multi_val_alloc(space);
10267 n = isl_multi_val_size(mv);
10268 if (n < 0)
10269 return isl_multi_val_free(mv);
10271 for (i = 0; i < n; ++i) {
10272 isl_val *v;
10274 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10275 mv = isl_multi_val_set_val(mv, i, v);
10278 return mv;
10281 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10282 * then return this fixed value in *val.
10284 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10285 unsigned dim, isl_int *val)
10287 isl_size nparam;
10289 nparam = isl_basic_set_dim(bset, isl_dim_param);
10290 if (nparam < 0)
10291 return isl_bool_error;
10292 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10295 /* Return -1 if the constraint "c1" should be sorted before "c2"
10296 * and 1 if it should be sorted after "c2".
10297 * Return 0 if the two constraints are the same (up to the constant term).
10299 * In particular, if a constraint involves later variables than another
10300 * then it is sorted after this other constraint.
10301 * uset_gist depends on constraints without existentially quantified
10302 * variables sorting first.
10304 * For constraints that have the same latest variable, those
10305 * with the same coefficient for this latest variable (first in absolute value
10306 * and then in actual value) are grouped together.
10307 * This is useful for detecting pairs of constraints that can
10308 * be chained in their printed representation.
10310 * Finally, within a group, constraints are sorted according to
10311 * their coefficients (excluding the constant term).
10313 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10315 isl_int **c1 = (isl_int **) p1;
10316 isl_int **c2 = (isl_int **) p2;
10317 int l1, l2;
10318 unsigned size = *(unsigned *) arg;
10319 int cmp;
10321 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10322 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10324 if (l1 != l2)
10325 return l1 - l2;
10327 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10328 if (cmp != 0)
10329 return cmp;
10330 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10331 if (cmp != 0)
10332 return -cmp;
10334 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10337 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10338 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10339 * and 0 if the two constraints are the same (up to the constant term).
10341 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10342 isl_int *c1, isl_int *c2)
10344 isl_size total;
10345 unsigned size;
10347 total = isl_basic_map_dim(bmap, isl_dim_all);
10348 if (total < 0)
10349 return -2;
10350 size = total;
10351 return sort_constraint_cmp(&c1, &c2, &size);
10354 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10355 __isl_take isl_basic_map *bmap)
10357 isl_size total;
10358 unsigned size;
10360 if (!bmap)
10361 return NULL;
10362 if (bmap->n_ineq == 0)
10363 return bmap;
10364 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10365 return bmap;
10366 total = isl_basic_map_dim(bmap, isl_dim_all);
10367 if (total < 0)
10368 return isl_basic_map_free(bmap);
10369 size = total;
10370 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10371 &sort_constraint_cmp, &size) < 0)
10372 return isl_basic_map_free(bmap);
10373 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10374 return bmap;
10377 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10378 __isl_take isl_basic_set *bset)
10380 isl_basic_map *bmap = bset_to_bmap(bset);
10381 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10384 __isl_give isl_basic_map *isl_basic_map_normalize(
10385 __isl_take isl_basic_map *bmap)
10387 bmap = isl_basic_map_remove_redundancies(bmap);
10388 bmap = isl_basic_map_sort_constraints(bmap);
10389 return bmap;
10391 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10392 __isl_keep isl_basic_map *bmap2)
10394 int i, cmp;
10395 isl_size total;
10396 isl_space *space1, *space2;
10398 if (!bmap1 || !bmap2)
10399 return -1;
10401 if (bmap1 == bmap2)
10402 return 0;
10403 space1 = isl_basic_map_peek_space(bmap1);
10404 space2 = isl_basic_map_peek_space(bmap2);
10405 cmp = isl_space_cmp(space1, space2);
10406 if (cmp)
10407 return cmp;
10408 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10409 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10410 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10411 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10412 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10413 return 0;
10414 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10415 return 1;
10416 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10417 return -1;
10418 if (bmap1->n_eq != bmap2->n_eq)
10419 return bmap1->n_eq - bmap2->n_eq;
10420 if (bmap1->n_ineq != bmap2->n_ineq)
10421 return bmap1->n_ineq - bmap2->n_ineq;
10422 if (bmap1->n_div != bmap2->n_div)
10423 return bmap1->n_div - bmap2->n_div;
10424 total = isl_basic_map_dim(bmap1, isl_dim_all);
10425 if (total < 0)
10426 return -1;
10427 for (i = 0; i < bmap1->n_eq; ++i) {
10428 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10429 if (cmp)
10430 return cmp;
10432 for (i = 0; i < bmap1->n_ineq; ++i) {
10433 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10434 if (cmp)
10435 return cmp;
10437 for (i = 0; i < bmap1->n_div; ++i) {
10438 isl_bool unknown1, unknown2;
10440 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10441 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10442 if (unknown1 < 0 || unknown2 < 0)
10443 return -1;
10444 if (unknown1 && unknown2)
10445 continue;
10446 if (unknown1)
10447 return 1;
10448 if (unknown2)
10449 return -1;
10450 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10451 if (cmp)
10452 return cmp;
10454 return 0;
10457 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10458 __isl_keep isl_basic_set *bset2)
10460 return isl_basic_map_plain_cmp(bset1, bset2);
10463 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10465 int i, cmp;
10467 if (set1 == set2)
10468 return 0;
10469 if (set1->n != set2->n)
10470 return set1->n - set2->n;
10472 for (i = 0; i < set1->n; ++i) {
10473 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10474 if (cmp)
10475 return cmp;
10478 return 0;
10481 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10482 __isl_keep isl_basic_map *bmap2)
10484 if (!bmap1 || !bmap2)
10485 return isl_bool_error;
10486 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10489 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10490 __isl_keep isl_basic_set *bset2)
10492 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10493 bset_to_bmap(bset2));
10496 static int qsort_bmap_cmp(const void *p1, const void *p2)
10498 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10499 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10501 return isl_basic_map_plain_cmp(bmap1, bmap2);
10504 /* Sort the basic maps of "map" and remove duplicate basic maps.
10506 * While removing basic maps, we make sure that the basic maps remain
10507 * sorted because isl_map_normalize expects the basic maps of the result
10508 * to be sorted.
10510 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10512 int i, j;
10514 map = isl_map_remove_empty_parts(map);
10515 if (!map)
10516 return NULL;
10517 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10518 for (i = map->n - 1; i >= 1; --i) {
10519 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10520 continue;
10521 isl_basic_map_free(map->p[i-1]);
10522 for (j = i; j < map->n; ++j)
10523 map->p[j - 1] = map->p[j];
10524 map->n--;
10527 return map;
10530 /* Remove obvious duplicates among the basic maps of "map".
10532 * Unlike isl_map_normalize, this function does not remove redundant
10533 * constraints and only removes duplicates that have exactly the same
10534 * constraints in the input. It does sort the constraints and
10535 * the basic maps to ease the detection of duplicates.
10537 * If "map" has already been normalized or if the basic maps are
10538 * disjoint, then there can be no duplicates.
10540 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10542 int i;
10543 isl_basic_map *bmap;
10545 if (!map)
10546 return NULL;
10547 if (map->n <= 1)
10548 return map;
10549 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10550 return map;
10551 for (i = 0; i < map->n; ++i) {
10552 bmap = isl_basic_map_copy(map->p[i]);
10553 bmap = isl_basic_map_sort_constraints(bmap);
10554 if (!bmap)
10555 return isl_map_free(map);
10556 isl_basic_map_free(map->p[i]);
10557 map->p[i] = bmap;
10560 map = sort_and_remove_duplicates(map);
10561 return map;
10564 /* We normalize in place, but if anything goes wrong we need
10565 * to return NULL, so we need to make sure we don't change the
10566 * meaning of any possible other copies of map.
10568 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10570 int i;
10571 struct isl_basic_map *bmap;
10573 if (!map)
10574 return NULL;
10575 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10576 return map;
10577 for (i = 0; i < map->n; ++i) {
10578 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10579 if (!bmap)
10580 goto error;
10581 isl_basic_map_free(map->p[i]);
10582 map->p[i] = bmap;
10585 map = sort_and_remove_duplicates(map);
10586 if (map)
10587 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10588 return map;
10589 error:
10590 isl_map_free(map);
10591 return NULL;
10594 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10596 return set_from_map(isl_map_normalize(set_to_map(set)));
10599 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10600 __isl_keep isl_map *map2)
10602 int i;
10603 isl_bool equal;
10605 if (!map1 || !map2)
10606 return isl_bool_error;
10608 if (map1 == map2)
10609 return isl_bool_true;
10610 equal = isl_map_has_equal_space(map1, map2);
10611 if (equal < 0 || !equal)
10612 return equal;
10614 map1 = isl_map_copy(map1);
10615 map2 = isl_map_copy(map2);
10616 map1 = isl_map_normalize(map1);
10617 map2 = isl_map_normalize(map2);
10618 if (!map1 || !map2)
10619 goto error;
10620 equal = map1->n == map2->n;
10621 for (i = 0; equal && i < map1->n; ++i) {
10622 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10623 if (equal < 0)
10624 goto error;
10626 isl_map_free(map1);
10627 isl_map_free(map2);
10628 return equal;
10629 error:
10630 isl_map_free(map1);
10631 isl_map_free(map2);
10632 return isl_bool_error;
10635 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10636 __isl_keep isl_set *set2)
10638 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10641 /* Return the basic maps in "map" as a list.
10643 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10644 __isl_keep isl_map *map)
10646 int i;
10647 isl_ctx *ctx;
10648 isl_basic_map_list *list;
10650 if (!map)
10651 return NULL;
10652 ctx = isl_map_get_ctx(map);
10653 list = isl_basic_map_list_alloc(ctx, map->n);
10655 for (i = 0; i < map->n; ++i) {
10656 isl_basic_map *bmap;
10658 bmap = isl_basic_map_copy(map->p[i]);
10659 list = isl_basic_map_list_add(list, bmap);
10662 return list;
10665 /* Return the intersection of the elements in the non-empty list "list".
10666 * All elements are assumed to live in the same space.
10668 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10669 __isl_take isl_basic_map_list *list)
10671 int i;
10672 isl_size n;
10673 isl_basic_map *bmap;
10675 n = isl_basic_map_list_n_basic_map(list);
10676 if (n < 0)
10677 goto error;
10678 if (n < 1)
10679 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10680 "expecting non-empty list", goto error);
10682 bmap = isl_basic_map_list_get_basic_map(list, 0);
10683 for (i = 1; i < n; ++i) {
10684 isl_basic_map *bmap_i;
10686 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10687 bmap = isl_basic_map_intersect(bmap, bmap_i);
10690 isl_basic_map_list_free(list);
10691 return bmap;
10692 error:
10693 isl_basic_map_list_free(list);
10694 return NULL;
10697 /* Return the intersection of the elements in the non-empty list "list".
10698 * All elements are assumed to live in the same space.
10700 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10701 __isl_take isl_basic_set_list *list)
10703 return isl_basic_map_list_intersect(list);
10706 /* Return the union of the elements of "list".
10707 * The list is required to have at least one element.
10709 __isl_give isl_set *isl_basic_set_list_union(
10710 __isl_take isl_basic_set_list *list)
10712 int i;
10713 isl_size n;
10714 isl_space *space;
10715 isl_basic_set *bset;
10716 isl_set *set;
10718 n = isl_basic_set_list_n_basic_set(list);
10719 if (n < 0)
10720 goto error;
10721 if (n < 1)
10722 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10723 "expecting non-empty list", goto error);
10725 bset = isl_basic_set_list_get_basic_set(list, 0);
10726 space = isl_basic_set_get_space(bset);
10727 isl_basic_set_free(bset);
10729 set = isl_set_alloc_space(space, n, 0);
10730 for (i = 0; i < n; ++i) {
10731 bset = isl_basic_set_list_get_basic_set(list, i);
10732 set = isl_set_add_basic_set(set, bset);
10735 isl_basic_set_list_free(list);
10736 return set;
10737 error:
10738 isl_basic_set_list_free(list);
10739 return NULL;
10742 /* Return the union of the elements in the non-empty list "list".
10743 * All elements are assumed to live in the same space.
10745 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10747 int i;
10748 isl_size n;
10749 isl_set *set;
10751 n = isl_set_list_n_set(list);
10752 if (n < 0)
10753 goto error;
10754 if (n < 1)
10755 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10756 "expecting non-empty list", goto error);
10758 set = isl_set_list_get_set(list, 0);
10759 for (i = 1; i < n; ++i) {
10760 isl_set *set_i;
10762 set_i = isl_set_list_get_set(list, i);
10763 set = isl_set_union(set, set_i);
10766 isl_set_list_free(list);
10767 return set;
10768 error:
10769 isl_set_list_free(list);
10770 return NULL;
10773 __isl_give isl_basic_map *isl_basic_map_product(
10774 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10776 isl_space *space_result = NULL;
10777 struct isl_basic_map *bmap;
10778 unsigned in1, in2, out1, out2, nparam, total, pos;
10779 struct isl_dim_map *dim_map1, *dim_map2;
10781 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10782 goto error;
10783 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10784 isl_space_copy(bmap2->dim));
10786 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10787 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10788 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10789 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10790 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10792 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10793 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10794 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10795 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10796 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10797 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10798 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10799 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10800 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10801 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10802 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10804 bmap = isl_basic_map_alloc_space(space_result,
10805 bmap1->n_div + bmap2->n_div,
10806 bmap1->n_eq + bmap2->n_eq,
10807 bmap1->n_ineq + bmap2->n_ineq);
10808 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10809 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10810 bmap = isl_basic_map_simplify(bmap);
10811 return isl_basic_map_finalize(bmap);
10812 error:
10813 isl_basic_map_free(bmap1);
10814 isl_basic_map_free(bmap2);
10815 return NULL;
10818 __isl_give isl_basic_map *isl_basic_map_flat_product(
10819 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10821 isl_basic_map *prod;
10823 prod = isl_basic_map_product(bmap1, bmap2);
10824 prod = isl_basic_map_flatten(prod);
10825 return prod;
10828 __isl_give isl_basic_set *isl_basic_set_flat_product(
10829 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10831 return isl_basic_map_flat_range_product(bset1, bset2);
10834 __isl_give isl_basic_map *isl_basic_map_domain_product(
10835 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10837 isl_space *space1, *space2;
10838 isl_space *space_result = NULL;
10839 isl_basic_map *bmap;
10840 isl_size in1, in2, out, nparam;
10841 unsigned total, pos;
10842 struct isl_dim_map *dim_map1, *dim_map2;
10844 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10845 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10846 out = isl_basic_map_dim(bmap1, isl_dim_out);
10847 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10848 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10849 goto error;
10851 space1 = isl_basic_map_get_space(bmap1);
10852 space2 = isl_basic_map_get_space(bmap2);
10853 space_result = isl_space_domain_product(space1, space2);
10855 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10856 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10857 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10858 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10859 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10862 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10863 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10864 isl_dim_map_div(dim_map1, bmap1, pos += out);
10865 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10867 bmap = isl_basic_map_alloc_space(space_result,
10868 bmap1->n_div + bmap2->n_div,
10869 bmap1->n_eq + bmap2->n_eq,
10870 bmap1->n_ineq + bmap2->n_ineq);
10871 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10872 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10873 bmap = isl_basic_map_simplify(bmap);
10874 return isl_basic_map_finalize(bmap);
10875 error:
10876 isl_basic_map_free(bmap1);
10877 isl_basic_map_free(bmap2);
10878 return NULL;
10881 __isl_give isl_basic_map *isl_basic_map_range_product(
10882 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10884 isl_bool rational;
10885 isl_space *space_result = NULL;
10886 isl_basic_map *bmap;
10887 isl_size in, out1, out2, nparam;
10888 unsigned total, pos;
10889 struct isl_dim_map *dim_map1, *dim_map2;
10891 rational = isl_basic_map_is_rational(bmap1);
10892 if (rational >= 0 && rational)
10893 rational = isl_basic_map_is_rational(bmap2);
10894 in = isl_basic_map_dim(bmap1, isl_dim_in);
10895 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10896 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10897 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10898 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10899 goto error;
10901 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10902 goto error;
10904 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10905 isl_space_copy(bmap2->dim));
10907 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10908 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10909 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10914 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10915 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10916 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10917 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10919 bmap = isl_basic_map_alloc_space(space_result,
10920 bmap1->n_div + bmap2->n_div,
10921 bmap1->n_eq + bmap2->n_eq,
10922 bmap1->n_ineq + bmap2->n_ineq);
10923 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10925 if (rational)
10926 bmap = isl_basic_map_set_rational(bmap);
10927 bmap = isl_basic_map_simplify(bmap);
10928 return isl_basic_map_finalize(bmap);
10929 error:
10930 isl_basic_map_free(bmap1);
10931 isl_basic_map_free(bmap2);
10932 return NULL;
10935 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10936 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10938 isl_basic_map *prod;
10940 prod = isl_basic_map_range_product(bmap1, bmap2);
10941 prod = isl_basic_map_flatten_range(prod);
10942 return prod;
10945 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10946 * and collect the results.
10947 * The result live in the space obtained by calling "space_product"
10948 * on the spaces of "map1" and "map2".
10949 * If "remove_duplicates" is set then the result may contain duplicates
10950 * (even if the inputs do not) and so we try and remove the obvious
10951 * duplicates.
10953 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10954 __isl_take isl_map *map2,
10955 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10956 __isl_take isl_space *right),
10957 __isl_give isl_basic_map *(*basic_map_product)(
10958 __isl_take isl_basic_map *left,
10959 __isl_take isl_basic_map *right),
10960 int remove_duplicates)
10962 unsigned flags = 0;
10963 struct isl_map *result;
10964 int i, j;
10965 isl_bool m;
10967 m = isl_map_has_equal_params(map1, map2);
10968 if (m < 0)
10969 goto error;
10970 if (!m)
10971 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10972 "parameters don't match", goto error);
10974 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10975 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10976 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10978 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10979 isl_space_copy(map2->dim)),
10980 map1->n * map2->n, flags);
10981 if (!result)
10982 goto error;
10983 for (i = 0; i < map1->n; ++i)
10984 for (j = 0; j < map2->n; ++j) {
10985 struct isl_basic_map *part;
10986 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10987 isl_basic_map_copy(map2->p[j]));
10988 if (isl_basic_map_is_empty(part))
10989 isl_basic_map_free(part);
10990 else
10991 result = isl_map_add_basic_map(result, part);
10992 if (!result)
10993 goto error;
10995 if (remove_duplicates)
10996 result = isl_map_remove_obvious_duplicates(result);
10997 isl_map_free(map1);
10998 isl_map_free(map2);
10999 return result;
11000 error:
11001 isl_map_free(map1);
11002 isl_map_free(map2);
11003 return NULL;
11006 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11008 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11009 __isl_take isl_map *map2)
11011 isl_map_align_params_bin(&map1, &map2);
11012 return map_product(map1, map2, &isl_space_product,
11013 &isl_basic_map_product, 0);
11016 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11018 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11019 __isl_take isl_map *map2)
11021 isl_map *prod;
11023 prod = isl_map_product(map1, map2);
11024 prod = isl_map_flatten(prod);
11025 return prod;
11028 /* Given two set A and B, construct its Cartesian product A x B.
11030 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11031 __isl_take isl_set *set2)
11033 return isl_map_range_product(set1, set2);
11036 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11037 __isl_take isl_set *set2)
11039 return isl_map_flat_range_product(set1, set2);
11042 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11044 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11045 __isl_take isl_map *map2)
11047 isl_map_align_params_bin(&map1, &map2);
11048 return map_product(map1, map2, &isl_space_domain_product,
11049 &isl_basic_map_domain_product, 1);
11052 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11054 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11055 __isl_take isl_map *map2)
11057 isl_map_align_params_bin(&map1, &map2);
11058 return map_product(map1, map2, &isl_space_range_product,
11059 &isl_basic_map_range_product, 1);
11062 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11064 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11066 isl_space *space;
11067 isl_size total1, keep1, total2, keep2;
11069 total1 = isl_map_dim(map, isl_dim_in);
11070 total2 = isl_map_dim(map, isl_dim_out);
11071 if (total1 < 0 || total2 < 0)
11072 return isl_map_free(map);
11073 if (!isl_space_domain_is_wrapping(map->dim) ||
11074 !isl_space_range_is_wrapping(map->dim))
11075 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11076 "not a product", return isl_map_free(map));
11078 space = isl_map_get_space(map);
11079 space = isl_space_factor_domain(space);
11080 keep1 = isl_space_dim(space, isl_dim_in);
11081 keep2 = isl_space_dim(space, isl_dim_out);
11082 if (keep1 < 0 || keep2 < 0)
11083 map = isl_map_free(map);
11084 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11085 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11086 map = isl_map_reset_space(map, space);
11088 return map;
11091 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11093 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11095 isl_space *space;
11096 isl_size total1, keep1, total2, keep2;
11098 total1 = isl_map_dim(map, isl_dim_in);
11099 total2 = isl_map_dim(map, isl_dim_out);
11100 if (total1 < 0 || total2 < 0)
11101 return isl_map_free(map);
11102 if (!isl_space_domain_is_wrapping(map->dim) ||
11103 !isl_space_range_is_wrapping(map->dim))
11104 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11105 "not a product", return isl_map_free(map));
11107 space = isl_map_get_space(map);
11108 space = isl_space_factor_range(space);
11109 keep1 = isl_space_dim(space, isl_dim_in);
11110 keep2 = isl_space_dim(space, isl_dim_out);
11111 if (keep1 < 0 || keep2 < 0)
11112 map = isl_map_free(map);
11113 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11114 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11115 map = isl_map_reset_space(map, space);
11117 return map;
11120 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11122 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11124 isl_space *space;
11125 isl_size total, keep;
11127 total = isl_map_dim(map, isl_dim_in);
11128 if (total < 0)
11129 return isl_map_free(map);
11130 if (!isl_space_domain_is_wrapping(map->dim))
11131 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11132 "domain is not a product", return isl_map_free(map));
11134 space = isl_map_get_space(map);
11135 space = isl_space_domain_factor_domain(space);
11136 keep = isl_space_dim(space, isl_dim_in);
11137 if (keep < 0)
11138 map = isl_map_free(map);
11139 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11140 map = isl_map_reset_space(map, space);
11142 return map;
11145 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11147 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11149 isl_space *space;
11150 isl_size total, keep;
11152 total = isl_map_dim(map, isl_dim_in);
11153 if (total < 0)
11154 return isl_map_free(map);
11155 if (!isl_space_domain_is_wrapping(map->dim))
11156 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11157 "domain is not a product", return isl_map_free(map));
11159 space = isl_map_get_space(map);
11160 space = isl_space_domain_factor_range(space);
11161 keep = isl_space_dim(space, isl_dim_in);
11162 if (keep < 0)
11163 map = isl_map_free(map);
11164 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11165 map = isl_map_reset_space(map, space);
11167 return map;
11170 /* Given a map A -> [B -> C], extract the map A -> B.
11172 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11174 isl_space *space;
11175 isl_size total, keep;
11177 total = isl_map_dim(map, isl_dim_out);
11178 if (total < 0)
11179 return isl_map_free(map);
11180 if (!isl_space_range_is_wrapping(map->dim))
11181 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11182 "range is not a product", return isl_map_free(map));
11184 space = isl_map_get_space(map);
11185 space = isl_space_range_factor_domain(space);
11186 keep = isl_space_dim(space, isl_dim_out);
11187 if (keep < 0)
11188 map = isl_map_free(map);
11189 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11190 map = isl_map_reset_space(map, space);
11192 return map;
11195 /* Given a map A -> [B -> C], extract the map A -> C.
11197 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11199 isl_space *space;
11200 isl_size total, keep;
11202 total = isl_map_dim(map, isl_dim_out);
11203 if (total < 0)
11204 return isl_map_free(map);
11205 if (!isl_space_range_is_wrapping(map->dim))
11206 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11207 "range is not a product", return isl_map_free(map));
11209 space = isl_map_get_space(map);
11210 space = isl_space_range_factor_range(space);
11211 keep = isl_space_dim(space, isl_dim_out);
11212 if (keep < 0)
11213 map = isl_map_free(map);
11214 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11215 map = isl_map_reset_space(map, space);
11217 return map;
11220 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11222 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11223 __isl_take isl_map *map2)
11225 isl_map *prod;
11227 prod = isl_map_domain_product(map1, map2);
11228 prod = isl_map_flatten_domain(prod);
11229 return prod;
11232 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11234 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11235 __isl_take isl_map *map2)
11237 isl_map *prod;
11239 prod = isl_map_range_product(map1, map2);
11240 prod = isl_map_flatten_range(prod);
11241 return prod;
11244 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11246 int i;
11247 uint32_t hash = isl_hash_init();
11248 isl_size total;
11250 if (!bmap)
11251 return 0;
11252 bmap = isl_basic_map_copy(bmap);
11253 bmap = isl_basic_map_normalize(bmap);
11254 total = isl_basic_map_dim(bmap, isl_dim_all);
11255 if (total < 0)
11256 return 0;
11257 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11258 for (i = 0; i < bmap->n_eq; ++i) {
11259 uint32_t c_hash;
11260 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11261 isl_hash_hash(hash, c_hash);
11263 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11264 for (i = 0; i < bmap->n_ineq; ++i) {
11265 uint32_t c_hash;
11266 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11267 isl_hash_hash(hash, c_hash);
11269 isl_hash_byte(hash, bmap->n_div & 0xFF);
11270 for (i = 0; i < bmap->n_div; ++i) {
11271 uint32_t c_hash;
11272 if (isl_int_is_zero(bmap->div[i][0]))
11273 continue;
11274 isl_hash_byte(hash, i & 0xFF);
11275 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11276 isl_hash_hash(hash, c_hash);
11278 isl_basic_map_free(bmap);
11279 return hash;
11282 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11284 return isl_basic_map_get_hash(bset_to_bmap(bset));
11287 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11289 int i;
11290 uint32_t hash;
11292 if (!map)
11293 return 0;
11294 map = isl_map_copy(map);
11295 map = isl_map_normalize(map);
11296 if (!map)
11297 return 0;
11299 hash = isl_hash_init();
11300 for (i = 0; i < map->n; ++i) {
11301 uint32_t bmap_hash;
11302 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11303 isl_hash_hash(hash, bmap_hash);
11306 isl_map_free(map);
11308 return hash;
11311 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11313 return isl_map_get_hash(set_to_map(set));
11316 /* Return the number of basic maps in the (current) representation of "map".
11318 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11320 return map ? map->n : isl_size_error;
11323 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11325 return set ? set->n : isl_size_error;
11328 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11329 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11331 int i;
11333 if (!map)
11334 return isl_stat_error;
11336 for (i = 0; i < map->n; ++i)
11337 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11338 return isl_stat_error;
11340 return isl_stat_ok;
11343 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11344 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11346 int i;
11348 if (!set)
11349 return isl_stat_error;
11351 for (i = 0; i < set->n; ++i)
11352 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11353 return isl_stat_error;
11355 return isl_stat_ok;
11358 /* Does "test" succeed on every basic set in "set"?
11360 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11361 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11362 void *user)
11364 int i;
11366 if (!set)
11367 return isl_bool_error;
11369 for (i = 0; i < set->n; ++i) {
11370 isl_bool r;
11372 r = test(set->p[i], user);
11373 if (r < 0 || !r)
11374 return r;
11377 return isl_bool_true;
11380 /* Return a list of basic sets, the union of which is equal to "set".
11382 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11383 __isl_keep isl_set *set)
11385 int i;
11386 isl_basic_set_list *list;
11388 if (!set)
11389 return NULL;
11391 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11392 for (i = 0; i < set->n; ++i) {
11393 isl_basic_set *bset;
11395 bset = isl_basic_set_copy(set->p[i]);
11396 list = isl_basic_set_list_add(list, bset);
11399 return list;
11402 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11404 isl_space *space;
11406 if (!bset)
11407 return NULL;
11409 bset = isl_basic_set_cow(bset);
11410 if (!bset)
11411 return NULL;
11413 space = isl_basic_set_get_space(bset);
11414 space = isl_space_lift(space, bset->n_div);
11415 if (!space)
11416 goto error;
11417 isl_space_free(bset->dim);
11418 bset->dim = space;
11419 bset->extra -= bset->n_div;
11420 bset->n_div = 0;
11422 bset = isl_basic_set_finalize(bset);
11424 return bset;
11425 error:
11426 isl_basic_set_free(bset);
11427 return NULL;
11430 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11432 int i;
11433 isl_space *space;
11434 unsigned n_div;
11436 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11438 if (!set)
11439 return NULL;
11441 set = isl_set_cow(set);
11442 if (!set)
11443 return NULL;
11445 n_div = set->p[0]->n_div;
11446 space = isl_set_get_space(set);
11447 space = isl_space_lift(space, n_div);
11448 if (!space)
11449 goto error;
11450 isl_space_free(set->dim);
11451 set->dim = space;
11453 for (i = 0; i < set->n; ++i) {
11454 set->p[i] = isl_basic_set_lift(set->p[i]);
11455 if (!set->p[i])
11456 goto error;
11459 return set;
11460 error:
11461 isl_set_free(set);
11462 return NULL;
11465 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11467 isl_size dim;
11468 int size = 0;
11470 dim = isl_basic_set_dim(bset, isl_dim_all);
11471 if (dim < 0)
11472 return -1;
11473 size += bset->n_eq * (1 + dim);
11474 size += bset->n_ineq * (1 + dim);
11475 size += bset->n_div * (2 + dim);
11477 return size;
11480 int isl_set_size(__isl_keep isl_set *set)
11482 int i;
11483 int size = 0;
11485 if (!set)
11486 return -1;
11488 for (i = 0; i < set->n; ++i)
11489 size += isl_basic_set_size(set->p[i]);
11491 return size;
11494 /* Check if there is any lower bound (if lower == 0) and/or upper
11495 * bound (if upper == 0) on the specified dim.
11497 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11498 enum isl_dim_type type, unsigned pos, int lower, int upper)
11500 int i;
11502 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11503 return isl_bool_error;
11505 pos += isl_basic_map_offset(bmap, type);
11507 for (i = 0; i < bmap->n_div; ++i) {
11508 if (isl_int_is_zero(bmap->div[i][0]))
11509 continue;
11510 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11511 return isl_bool_true;
11514 for (i = 0; i < bmap->n_eq; ++i)
11515 if (!isl_int_is_zero(bmap->eq[i][pos]))
11516 return isl_bool_true;
11518 for (i = 0; i < bmap->n_ineq; ++i) {
11519 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11520 if (sgn > 0)
11521 lower = 1;
11522 if (sgn < 0)
11523 upper = 1;
11526 return lower && upper;
11529 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11530 enum isl_dim_type type, unsigned pos)
11532 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11535 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11536 enum isl_dim_type type, unsigned pos)
11538 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11541 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11542 enum isl_dim_type type, unsigned pos)
11544 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11547 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11548 enum isl_dim_type type, unsigned pos)
11550 int i;
11552 if (!map)
11553 return isl_bool_error;
11555 for (i = 0; i < map->n; ++i) {
11556 isl_bool bounded;
11557 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11558 if (bounded < 0 || !bounded)
11559 return bounded;
11562 return isl_bool_true;
11565 /* Return true if the specified dim is involved in both an upper bound
11566 * and a lower bound.
11568 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11569 enum isl_dim_type type, unsigned pos)
11571 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11574 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11576 static isl_bool has_any_bound(__isl_keep isl_map *map,
11577 enum isl_dim_type type, unsigned pos,
11578 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11579 enum isl_dim_type type, unsigned pos))
11581 int i;
11583 if (!map)
11584 return isl_bool_error;
11586 for (i = 0; i < map->n; ++i) {
11587 isl_bool bounded;
11588 bounded = fn(map->p[i], type, pos);
11589 if (bounded < 0 || bounded)
11590 return bounded;
11593 return isl_bool_false;
11596 /* Return 1 if the specified dim is involved in any lower bound.
11598 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11599 enum isl_dim_type type, unsigned pos)
11601 return has_any_bound(set, type, pos,
11602 &isl_basic_map_dim_has_lower_bound);
11605 /* Return 1 if the specified dim is involved in any upper bound.
11607 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11608 enum isl_dim_type type, unsigned pos)
11610 return has_any_bound(set, type, pos,
11611 &isl_basic_map_dim_has_upper_bound);
11614 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11616 static isl_bool has_bound(__isl_keep isl_map *map,
11617 enum isl_dim_type type, unsigned pos,
11618 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11619 enum isl_dim_type type, unsigned pos))
11621 int i;
11623 if (!map)
11624 return isl_bool_error;
11626 for (i = 0; i < map->n; ++i) {
11627 isl_bool bounded;
11628 bounded = fn(map->p[i], type, pos);
11629 if (bounded < 0 || !bounded)
11630 return bounded;
11633 return isl_bool_true;
11636 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11638 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11639 enum isl_dim_type type, unsigned pos)
11641 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11644 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11646 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11647 enum isl_dim_type type, unsigned pos)
11649 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11652 /* For each of the "n" variables starting at "first", determine
11653 * the sign of the variable and put the results in the first "n"
11654 * elements of the array "signs".
11655 * Sign
11656 * 1 means that the variable is non-negative
11657 * -1 means that the variable is non-positive
11658 * 0 means the variable attains both positive and negative values.
11660 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11661 unsigned first, unsigned n, int *signs)
11663 isl_vec *bound = NULL;
11664 struct isl_tab *tab = NULL;
11665 struct isl_tab_undo *snap;
11666 int i;
11667 isl_size total;
11669 total = isl_basic_set_dim(bset, isl_dim_all);
11670 if (total < 0 || !signs)
11671 return isl_stat_error;
11673 bound = isl_vec_alloc(bset->ctx, 1 + total);
11674 tab = isl_tab_from_basic_set(bset, 0);
11675 if (!bound || !tab)
11676 goto error;
11678 isl_seq_clr(bound->el, bound->size);
11679 isl_int_set_si(bound->el[0], -1);
11681 snap = isl_tab_snap(tab);
11682 for (i = 0; i < n; ++i) {
11683 int empty;
11685 isl_int_set_si(bound->el[1 + first + i], -1);
11686 if (isl_tab_add_ineq(tab, bound->el) < 0)
11687 goto error;
11688 empty = tab->empty;
11689 isl_int_set_si(bound->el[1 + first + i], 0);
11690 if (isl_tab_rollback(tab, snap) < 0)
11691 goto error;
11693 if (empty) {
11694 signs[i] = 1;
11695 continue;
11698 isl_int_set_si(bound->el[1 + first + i], 1);
11699 if (isl_tab_add_ineq(tab, bound->el) < 0)
11700 goto error;
11701 empty = tab->empty;
11702 isl_int_set_si(bound->el[1 + first + i], 0);
11703 if (isl_tab_rollback(tab, snap) < 0)
11704 goto error;
11706 signs[i] = empty ? -1 : 0;
11709 isl_tab_free(tab);
11710 isl_vec_free(bound);
11711 return isl_stat_ok;
11712 error:
11713 isl_tab_free(tab);
11714 isl_vec_free(bound);
11715 return isl_stat_error;
11718 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11719 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11721 if (!bset || !signs)
11722 return isl_stat_error;
11723 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11724 return isl_stat_error;
11726 first += pos(bset->dim, type) - 1;
11727 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11730 /* Is it possible for the integer division "div" to depend (possibly
11731 * indirectly) on any output dimensions?
11733 * If the div is undefined, then we conservatively assume that it
11734 * may depend on them.
11735 * Otherwise, we check if it actually depends on them or on any integer
11736 * divisions that may depend on them.
11738 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11740 int i;
11741 isl_size n_out, n_div;
11742 unsigned o_out, o_div;
11744 if (isl_int_is_zero(bmap->div[div][0]))
11745 return isl_bool_true;
11747 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11748 if (n_out < 0)
11749 return isl_bool_error;
11750 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11752 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11753 return isl_bool_true;
11755 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11756 if (n_div < 0)
11757 return isl_bool_error;
11758 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11760 for (i = 0; i < n_div; ++i) {
11761 isl_bool may_involve;
11763 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11764 continue;
11765 may_involve = div_may_involve_output(bmap, i);
11766 if (may_involve < 0 || may_involve)
11767 return may_involve;
11770 return isl_bool_false;
11773 /* Return the first integer division of "bmap" in the range
11774 * [first, first + n[ that may depend on any output dimensions and
11775 * that has a non-zero coefficient in "c" (where the first coefficient
11776 * in "c" corresponds to integer division "first").
11778 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11779 isl_int *c, int first, int n)
11781 int k;
11783 if (!bmap)
11784 return -1;
11786 for (k = first; k < first + n; ++k) {
11787 isl_bool may_involve;
11789 if (isl_int_is_zero(c[k]))
11790 continue;
11791 may_involve = div_may_involve_output(bmap, k);
11792 if (may_involve < 0)
11793 return -1;
11794 if (may_involve)
11795 return k;
11798 return first + n;
11801 /* Look for a pair of inequality constraints in "bmap" of the form
11803 * -l + i >= 0 or i >= l
11804 * and
11805 * n + l - i >= 0 or i <= l + n
11807 * with n < "m" and i the output dimension at position "pos".
11808 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11809 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11810 * and earlier output dimensions, as well as integer divisions that do
11811 * not involve any of the output dimensions.
11813 * Return the index of the first inequality constraint or bmap->n_ineq
11814 * if no such pair can be found.
11816 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11817 int pos, isl_int m)
11819 int i, j;
11820 isl_ctx *ctx;
11821 isl_size total;
11822 isl_size n_div, n_out;
11823 unsigned o_div, o_out;
11824 int less;
11826 total = isl_basic_map_dim(bmap, isl_dim_all);
11827 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11828 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11829 if (total < 0 || n_out < 0 || n_div < 0)
11830 return -1;
11832 ctx = isl_basic_map_get_ctx(bmap);
11833 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11834 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11835 for (i = 0; i < bmap->n_ineq; ++i) {
11836 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11837 continue;
11838 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11839 n_out - (pos + 1)) != -1)
11840 continue;
11841 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11842 0, n_div) < n_div)
11843 continue;
11844 for (j = i + 1; j < bmap->n_ineq; ++j) {
11845 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11846 ctx->one))
11847 continue;
11848 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11849 bmap->ineq[j] + 1, total))
11850 continue;
11851 break;
11853 if (j >= bmap->n_ineq)
11854 continue;
11855 isl_int_add(bmap->ineq[i][0],
11856 bmap->ineq[i][0], bmap->ineq[j][0]);
11857 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11858 isl_int_sub(bmap->ineq[i][0],
11859 bmap->ineq[i][0], bmap->ineq[j][0]);
11860 if (!less)
11861 continue;
11862 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11863 return i;
11864 else
11865 return j;
11868 return bmap->n_ineq;
11871 /* Return the index of the equality of "bmap" that defines
11872 * the output dimension "pos" in terms of earlier dimensions.
11873 * The equality may also involve integer divisions, as long
11874 * as those integer divisions are defined in terms of
11875 * parameters or input dimensions.
11876 * In this case, *div is set to the number of integer divisions and
11877 * *ineq is set to the number of inequality constraints (provided
11878 * div and ineq are not NULL).
11880 * The equality may also involve a single integer division involving
11881 * the output dimensions (typically only output dimension "pos") as
11882 * long as the coefficient of output dimension "pos" is 1 or -1 and
11883 * there is a pair of constraints i >= l and i <= l + n, with i referring
11884 * to output dimension "pos", l an expression involving only earlier
11885 * dimensions and n smaller than the coefficient of the integer division
11886 * in the equality. In this case, the output dimension can be defined
11887 * in terms of a modulo expression that does not involve the integer division.
11888 * *div is then set to this single integer division and
11889 * *ineq is set to the index of constraint i >= l.
11891 * Return bmap->n_eq if there is no such equality.
11892 * Return -1 on error.
11894 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11895 int pos, int *div, int *ineq)
11897 int j, k, l;
11898 isl_size n_div, n_out;
11899 unsigned o_div, o_out;
11901 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11902 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11903 if (n_out < 0 || n_div < 0)
11904 return -1;
11906 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11907 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11909 if (ineq)
11910 *ineq = bmap->n_ineq;
11911 if (div)
11912 *div = n_div;
11913 for (j = 0; j < bmap->n_eq; ++j) {
11914 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11915 continue;
11916 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11917 n_out - (pos + 1)) != -1)
11918 continue;
11919 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11920 0, n_div);
11921 if (k >= n_div)
11922 return j;
11923 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11924 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11925 continue;
11926 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11927 k + 1, n_div - (k+1)) < n_div)
11928 continue;
11929 l = find_modulo_constraint_pair(bmap, pos,
11930 bmap->eq[j][o_div + k]);
11931 if (l < 0)
11932 return -1;
11933 if (l >= bmap->n_ineq)
11934 continue;
11935 if (div)
11936 *div = k;
11937 if (ineq)
11938 *ineq = l;
11939 return j;
11942 return bmap->n_eq;
11945 /* Check if the given basic map is obviously single-valued.
11946 * In particular, for each output dimension, check that there is
11947 * an equality that defines the output dimension in terms of
11948 * earlier dimensions.
11950 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11952 int i;
11953 isl_size n_out;
11955 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11956 if (n_out < 0)
11957 return isl_bool_error;
11959 for (i = 0; i < n_out; ++i) {
11960 int eq;
11962 eq = isl_basic_map_output_defining_equality(bmap, i,
11963 NULL, NULL);
11964 if (eq < 0)
11965 return isl_bool_error;
11966 if (eq >= bmap->n_eq)
11967 return isl_bool_false;
11970 return isl_bool_true;
11973 /* Check if the given basic map is single-valued.
11974 * We simply compute
11976 * M \circ M^-1
11978 * and check if the result is a subset of the identity mapping.
11980 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11982 isl_space *space;
11983 isl_basic_map *test;
11984 isl_basic_map *id;
11985 isl_bool sv;
11987 sv = isl_basic_map_plain_is_single_valued(bmap);
11988 if (sv < 0 || sv)
11989 return sv;
11991 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11992 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11994 space = isl_basic_map_get_space(bmap);
11995 space = isl_space_map_from_set(isl_space_range(space));
11996 id = isl_basic_map_identity(space);
11998 sv = isl_basic_map_is_subset(test, id);
12000 isl_basic_map_free(test);
12001 isl_basic_map_free(id);
12003 return sv;
12006 /* Check if the given map is obviously single-valued.
12008 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12010 if (!map)
12011 return isl_bool_error;
12012 if (map->n == 0)
12013 return isl_bool_true;
12014 if (map->n >= 2)
12015 return isl_bool_false;
12017 return isl_basic_map_plain_is_single_valued(map->p[0]);
12020 /* Check if the given map is single-valued.
12021 * We simply compute
12023 * M \circ M^-1
12025 * and check if the result is a subset of the identity mapping.
12027 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12029 isl_space *space;
12030 isl_map *test;
12031 isl_map *id;
12032 isl_bool sv;
12034 sv = isl_map_plain_is_single_valued(map);
12035 if (sv < 0 || sv)
12036 return sv;
12038 test = isl_map_reverse(isl_map_copy(map));
12039 test = isl_map_apply_range(test, isl_map_copy(map));
12041 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12042 id = isl_map_identity(space);
12044 sv = isl_map_is_subset(test, id);
12046 isl_map_free(test);
12047 isl_map_free(id);
12049 return sv;
12052 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12054 isl_bool in;
12056 map = isl_map_copy(map);
12057 map = isl_map_reverse(map);
12058 in = isl_map_is_single_valued(map);
12059 isl_map_free(map);
12061 return in;
12064 /* Check if the given map is obviously injective.
12066 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12068 isl_bool in;
12070 map = isl_map_copy(map);
12071 map = isl_map_reverse(map);
12072 in = isl_map_plain_is_single_valued(map);
12073 isl_map_free(map);
12075 return in;
12078 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12080 isl_bool sv;
12082 sv = isl_map_is_single_valued(map);
12083 if (sv < 0 || !sv)
12084 return sv;
12086 return isl_map_is_injective(map);
12089 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12091 return isl_map_is_single_valued(set_to_map(set));
12094 /* Does "map" only map elements to themselves?
12096 * If the domain and range spaces are different, then "map"
12097 * is considered not to be an identity relation, even if it is empty.
12098 * Otherwise, construct the maximal identity relation and
12099 * check whether "map" is a subset of this relation.
12101 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12103 isl_map *id;
12104 isl_bool equal, is_identity;
12106 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12107 if (equal < 0 || !equal)
12108 return equal;
12110 id = isl_map_identity(isl_map_get_space(map));
12111 is_identity = isl_map_is_subset(map, id);
12112 isl_map_free(id);
12114 return is_identity;
12117 int isl_map_is_translation(__isl_keep isl_map *map)
12119 int ok;
12120 isl_set *delta;
12122 delta = isl_map_deltas(isl_map_copy(map));
12123 ok = isl_set_is_singleton(delta);
12124 isl_set_free(delta);
12126 return ok;
12129 static int unique(isl_int *p, unsigned pos, unsigned len)
12131 if (isl_seq_first_non_zero(p, pos) != -1)
12132 return 0;
12133 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12134 return 0;
12135 return 1;
12138 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12140 int i, j;
12141 isl_size nvar, n_div;
12142 unsigned ovar;
12144 n_div = isl_basic_set_dim(bset, isl_dim_div);
12145 if (n_div < 0)
12146 return isl_bool_error;
12147 if (n_div != 0)
12148 return isl_bool_false;
12150 nvar = isl_basic_set_dim(bset, isl_dim_set);
12151 if (nvar < 0)
12152 return isl_bool_error;
12153 ovar = isl_space_offset(bset->dim, isl_dim_set);
12154 for (j = 0; j < nvar; ++j) {
12155 int lower = 0, upper = 0;
12156 for (i = 0; i < bset->n_eq; ++i) {
12157 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12158 continue;
12159 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12160 return isl_bool_false;
12161 break;
12163 if (i < bset->n_eq)
12164 continue;
12165 for (i = 0; i < bset->n_ineq; ++i) {
12166 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12167 continue;
12168 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12169 return isl_bool_false;
12170 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12171 lower = 1;
12172 else
12173 upper = 1;
12175 if (!lower || !upper)
12176 return isl_bool_false;
12179 return isl_bool_true;
12182 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12184 if (!set)
12185 return isl_bool_error;
12186 if (set->n != 1)
12187 return isl_bool_false;
12189 return isl_basic_set_is_box(set->p[0]);
12192 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12194 if (!bset)
12195 return isl_bool_error;
12197 return isl_space_is_wrapping(bset->dim);
12200 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12202 if (!set)
12203 return isl_bool_error;
12205 return isl_space_is_wrapping(set->dim);
12208 /* Modify the space of "map" through a call to "change".
12209 * If "can_change" is set (not NULL), then first call it to check
12210 * if the modification is allowed, printing the error message "cannot_change"
12211 * if it is not.
12213 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12214 isl_bool (*can_change)(__isl_keep isl_map *map),
12215 const char *cannot_change,
12216 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12218 isl_bool ok;
12219 isl_space *space;
12221 if (!map)
12222 return NULL;
12224 ok = can_change ? can_change(map) : isl_bool_true;
12225 if (ok < 0)
12226 return isl_map_free(map);
12227 if (!ok)
12228 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12229 return isl_map_free(map));
12231 space = change(isl_map_get_space(map));
12232 map = isl_map_reset_space(map, space);
12234 return map;
12237 /* Is the domain of "map" a wrapped relation?
12239 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12241 if (!map)
12242 return isl_bool_error;
12244 return isl_space_domain_is_wrapping(map->dim);
12247 /* Does "map" have a wrapped relation in both domain and range?
12249 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12251 return isl_space_is_product(isl_map_peek_space(map));
12254 /* Is the range of "map" a wrapped relation?
12256 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12258 if (!map)
12259 return isl_bool_error;
12261 return isl_space_range_is_wrapping(map->dim);
12264 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12266 isl_space *space;
12268 space = isl_basic_map_take_space(bmap);
12269 space = isl_space_wrap(space);
12270 bmap = isl_basic_map_restore_space(bmap, space);
12272 bmap = isl_basic_map_finalize(bmap);
12274 return bset_from_bmap(bmap);
12277 /* Given a map A -> B, return the set (A -> B).
12279 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12281 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12284 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12286 bset = isl_basic_set_cow(bset);
12287 if (!bset)
12288 return NULL;
12290 bset->dim = isl_space_unwrap(bset->dim);
12291 if (!bset->dim)
12292 goto error;
12294 bset = isl_basic_set_finalize(bset);
12296 return bset_to_bmap(bset);
12297 error:
12298 isl_basic_set_free(bset);
12299 return NULL;
12302 /* Given a set (A -> B), return the map A -> B.
12303 * Error out if "set" is not of the form (A -> B).
12305 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12307 return isl_map_change_space(set, &isl_set_is_wrapping,
12308 "not a wrapping set", &isl_space_unwrap);
12311 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12312 enum isl_dim_type type)
12314 isl_space *space;
12316 space = isl_basic_map_take_space(bmap);
12317 space = isl_space_reset(space, type);
12318 bmap = isl_basic_map_restore_space(bmap, space);
12320 bmap = isl_basic_map_mark_final(bmap);
12322 return bmap;
12325 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12326 enum isl_dim_type type)
12328 int i;
12329 isl_space *space;
12331 if (!map)
12332 return NULL;
12334 if (!isl_space_is_named_or_nested(map->dim, type))
12335 return map;
12337 map = isl_map_cow(map);
12338 if (!map)
12339 return NULL;
12341 for (i = 0; i < map->n; ++i) {
12342 map->p[i] = isl_basic_map_reset(map->p[i], type);
12343 if (!map->p[i])
12344 goto error;
12347 space = isl_map_take_space(map);
12348 space = isl_space_reset(space, type);
12349 map = isl_map_restore_space(map, space);
12351 return map;
12352 error:
12353 isl_map_free(map);
12354 return NULL;
12357 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12359 isl_space *space;
12361 space = isl_basic_map_take_space(bmap);
12362 space = isl_space_flatten(space);
12363 bmap = isl_basic_map_restore_space(bmap, space);
12365 bmap = isl_basic_map_mark_final(bmap);
12367 return bmap;
12370 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12372 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12375 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12376 __isl_take isl_basic_map *bmap)
12378 isl_space *space;
12380 space = isl_basic_map_take_space(bmap);
12381 space = isl_space_flatten_domain(space);
12382 bmap = isl_basic_map_restore_space(bmap, space);
12384 bmap = isl_basic_map_mark_final(bmap);
12386 return bmap;
12389 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12390 __isl_take isl_basic_map *bmap)
12392 isl_space *space;
12394 space = isl_basic_map_take_space(bmap);
12395 space = isl_space_flatten_range(space);
12396 bmap = isl_basic_map_restore_space(bmap, space);
12398 bmap = isl_basic_map_mark_final(bmap);
12400 return bmap;
12403 /* Remove any internal structure from the spaces of domain and range of "map".
12405 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12407 if (!map)
12408 return NULL;
12410 if (!map->dim->nested[0] && !map->dim->nested[1])
12411 return map;
12413 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12416 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12418 return set_from_map(isl_map_flatten(set_to_map(set)));
12421 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12423 isl_space *space, *flat_space;
12424 isl_map *map;
12426 space = isl_set_get_space(set);
12427 flat_space = isl_space_flatten(isl_space_copy(space));
12428 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12429 flat_space));
12430 map = isl_map_intersect_domain(map, set);
12432 return map;
12435 /* Remove any internal structure from the space of the domain of "map".
12437 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12439 if (!map)
12440 return NULL;
12442 if (!map->dim->nested[0])
12443 return map;
12445 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12448 /* Remove any internal structure from the space of the range of "map".
12450 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12452 if (!map)
12453 return NULL;
12455 if (!map->dim->nested[1])
12456 return map;
12458 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12461 /* Reorder the dimensions of "bmap" according to the given dim_map
12462 * and set the dimension specification to "space" and
12463 * perform Gaussian elimination on the result.
12465 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12466 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12468 isl_basic_map *res;
12469 unsigned flags;
12470 isl_size n_div;
12472 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12473 if (n_div < 0 || !space || !dim_map)
12474 goto error;
12476 flags = bmap->flags;
12477 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12478 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12479 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12480 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12481 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12482 if (res)
12483 res->flags = flags;
12484 res = isl_basic_map_gauss(res, NULL);
12485 res = isl_basic_map_finalize(res);
12486 return res;
12487 error:
12488 isl_dim_map_free(dim_map);
12489 isl_basic_map_free(bmap);
12490 isl_space_free(space);
12491 return NULL;
12494 /* Reorder the dimensions of "map" according to given reordering.
12496 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12497 __isl_take isl_reordering *r)
12499 int i;
12500 struct isl_dim_map *dim_map;
12502 map = isl_map_cow(map);
12503 dim_map = isl_dim_map_from_reordering(r);
12504 if (!map || !r || !dim_map)
12505 goto error;
12507 for (i = 0; i < map->n; ++i) {
12508 struct isl_dim_map *dim_map_i;
12509 isl_space *space;
12511 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12513 space = isl_reordering_get_space(r);
12514 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12516 if (!map->p[i])
12517 goto error;
12520 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12521 map = isl_map_unmark_normalized(map);
12523 isl_reordering_free(r);
12524 isl_dim_map_free(dim_map);
12525 return map;
12526 error:
12527 isl_dim_map_free(dim_map);
12528 isl_map_free(map);
12529 isl_reordering_free(r);
12530 return NULL;
12533 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12534 __isl_take isl_reordering *r)
12536 return set_from_map(isl_map_realign(set_to_map(set), r));
12539 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12540 __isl_take isl_space *model)
12542 isl_ctx *ctx;
12543 isl_bool aligned;
12545 if (!map || !model)
12546 goto error;
12548 ctx = isl_space_get_ctx(model);
12549 if (!isl_space_has_named_params(model))
12550 isl_die(ctx, isl_error_invalid,
12551 "model has unnamed parameters", goto error);
12552 if (isl_map_check_named_params(map) < 0)
12553 goto error;
12554 aligned = isl_map_space_has_equal_params(map, model);
12555 if (aligned < 0)
12556 goto error;
12557 if (!aligned) {
12558 isl_space *space;
12559 isl_reordering *exp;
12561 space = isl_map_peek_space(map);
12562 exp = isl_parameter_alignment_reordering(space, model);
12563 map = isl_map_realign(map, exp);
12566 isl_space_free(model);
12567 return map;
12568 error:
12569 isl_space_free(model);
12570 isl_map_free(map);
12571 return NULL;
12574 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12575 __isl_take isl_space *model)
12577 return isl_map_align_params(set, model);
12580 /* Align the parameters of "bmap" to those of "model", introducing
12581 * additional parameters if needed.
12583 __isl_give isl_basic_map *isl_basic_map_align_params(
12584 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12586 isl_ctx *ctx;
12587 isl_bool equal_params;
12588 isl_space *bmap_space;
12590 if (!bmap || !model)
12591 goto error;
12593 ctx = isl_space_get_ctx(model);
12594 if (!isl_space_has_named_params(model))
12595 isl_die(ctx, isl_error_invalid,
12596 "model has unnamed parameters", goto error);
12597 if (isl_basic_map_check_named_params(bmap) < 0)
12598 goto error;
12599 bmap_space = isl_basic_map_peek_space(bmap);
12600 equal_params = isl_space_has_equal_params(bmap_space, model);
12601 if (equal_params < 0)
12602 goto error;
12603 if (!equal_params) {
12604 isl_reordering *exp;
12605 struct isl_dim_map *dim_map;
12607 exp = isl_parameter_alignment_reordering(bmap_space, model);
12608 dim_map = isl_dim_map_from_reordering(exp);
12609 bmap = isl_basic_map_realign(bmap,
12610 isl_reordering_get_space(exp),
12611 isl_dim_map_extend(dim_map, bmap));
12612 isl_reordering_free(exp);
12613 isl_dim_map_free(dim_map);
12616 isl_space_free(model);
12617 return bmap;
12618 error:
12619 isl_space_free(model);
12620 isl_basic_map_free(bmap);
12621 return NULL;
12624 /* Do "bset" and "space" have the same parameters?
12626 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12627 __isl_keep isl_space *space)
12629 isl_space *bset_space;
12631 bset_space = isl_basic_set_peek_space(bset);
12632 return isl_space_has_equal_params(bset_space, space);
12635 /* Do "map" and "space" have the same parameters?
12637 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12638 __isl_keep isl_space *space)
12640 isl_space *map_space;
12642 map_space = isl_map_peek_space(map);
12643 return isl_space_has_equal_params(map_space, space);
12646 /* Do "set" and "space" have the same parameters?
12648 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12649 __isl_keep isl_space *space)
12651 return isl_map_space_has_equal_params(set_to_map(set), space);
12654 /* Align the parameters of "bset" to those of "model", introducing
12655 * additional parameters if needed.
12657 __isl_give isl_basic_set *isl_basic_set_align_params(
12658 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12660 return isl_basic_map_align_params(bset, model);
12663 #undef TYPE
12664 #define TYPE isl_map
12665 #define isl_map_drop_dims isl_map_drop
12666 #include "isl_drop_unused_params_templ.c"
12668 /* Drop all parameters not referenced by "set".
12670 __isl_give isl_set *isl_set_drop_unused_params(
12671 __isl_take isl_set *set)
12673 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12676 #undef TYPE
12677 #define TYPE isl_basic_map
12678 #define isl_basic_map_drop_dims isl_basic_map_drop
12679 #include "isl_drop_unused_params_templ.c"
12681 /* Drop all parameters not referenced by "bset".
12683 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12684 __isl_take isl_basic_set *bset)
12686 return bset_from_bmap(isl_basic_map_drop_unused_params(
12687 bset_to_bmap(bset)));
12690 /* Given a tuple of identifiers "tuple" in a space that corresponds
12691 * to that of "set", if any of those identifiers appear as parameters
12692 * in "set", then equate those parameters with the corresponding
12693 * set dimensions and project out the parameters.
12694 * The result therefore has no such parameters.
12696 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12697 __isl_keep isl_multi_id *tuple)
12699 int i;
12700 isl_size n;
12701 isl_space *set_space, *tuple_space;
12703 set_space = isl_set_peek_space(set);
12704 tuple_space = isl_multi_id_peek_space(tuple);
12705 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12706 return isl_set_free(set);
12707 n = isl_multi_id_size(tuple);
12708 if (n < 0)
12709 return isl_set_free(set);
12710 for (i = 0; i < n; ++i) {
12711 isl_id *id;
12712 int pos;
12714 id = isl_multi_id_get_at(tuple, i);
12715 if (!id)
12716 return isl_set_free(set);
12717 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12718 isl_id_free(id);
12719 if (pos < 0)
12720 continue;
12721 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12722 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12724 return set;
12727 /* Bind the set dimensions of "set" to parameters with identifiers
12728 * specified by "tuple", living in the same space as "set".
12730 * If no parameters with these identifiers appear in "set" already,
12731 * then the set dimensions are simply reinterpreted as parameters.
12732 * Otherwise, the parameters are first equated to the corresponding
12733 * set dimensions.
12735 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12736 __isl_take isl_multi_id *tuple)
12738 isl_space *space;
12740 set = equate_params(set, tuple);
12741 space = isl_set_get_space(set);
12742 space = isl_space_bind_set(space, tuple);
12743 isl_multi_id_free(tuple);
12744 set = isl_set_reset_space(set, space);
12746 return set;
12749 /* Given a tuple of identifiers "tuple" in a space that corresponds
12750 * to the domain of "map", if any of those identifiers appear as parameters
12751 * in "map", then equate those parameters with the corresponding
12752 * input dimensions and project out the parameters.
12753 * The result therefore has no such parameters.
12755 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12756 __isl_keep isl_multi_id *tuple)
12758 int i;
12759 isl_size n;
12760 isl_space *map_space, *tuple_space;
12762 map_space = isl_map_peek_space(map);
12763 tuple_space = isl_multi_id_peek_space(tuple);
12764 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12765 return isl_map_free(map);
12766 n = isl_multi_id_size(tuple);
12767 if (n < 0)
12768 return isl_map_free(map);
12769 for (i = 0; i < n; ++i) {
12770 isl_id *id;
12771 int pos;
12773 id = isl_multi_id_get_at(tuple, i);
12774 if (!id)
12775 return isl_map_free(map);
12776 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12777 isl_id_free(id);
12778 if (pos < 0)
12779 continue;
12780 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12781 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12783 return map;
12786 /* Bind the input dimensions of "map" to parameters with identifiers
12787 * specified by "tuple", living in the domain space of "map".
12789 * If no parameters with these identifiers appear in "map" already,
12790 * then the input dimensions are simply reinterpreted as parameters.
12791 * Otherwise, the parameters are first equated to the corresponding
12792 * input dimensions.
12794 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12795 __isl_take isl_multi_id *tuple)
12797 isl_space *space;
12798 isl_set *set;
12800 map = map_equate_params(map, tuple);
12801 space = isl_map_get_space(map);
12802 space = isl_space_bind_map_domain(space, tuple);
12803 isl_multi_id_free(tuple);
12804 set = set_from_map(isl_map_reset_space(map, space));
12806 return set;
12809 /* Bind the output dimensions of "map" to parameters with identifiers
12810 * specified by "tuple", living in the range space of "map".
12812 * Since binding is more easily implemented on the domain,
12813 * bind the input dimensions of the inverse of "map".
12815 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12816 __isl_take isl_multi_id *tuple)
12818 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12821 /* Insert a domain corresponding to "tuple"
12822 * into the nullary or unary relation "set".
12823 * The result has an extra initial tuple and is therefore
12824 * either a unary or binary relation.
12825 * Any parameters with identifiers in "tuple" are reinterpreted
12826 * as the corresponding domain dimensions.
12828 static __isl_give isl_map *unbind_params_insert_domain(
12829 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12831 isl_space *space;
12832 isl_reordering *r;
12834 space = isl_set_peek_space(set);
12835 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12836 isl_multi_id_free(tuple);
12838 return isl_map_realign(set_to_map(set), r);
12841 /* Construct a set with "tuple" as domain from the parameter domain "set".
12842 * Any parameters with identifiers in "tuple" are reinterpreted
12843 * as the corresponding set dimensions.
12845 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12846 __isl_take isl_multi_id *tuple)
12848 isl_bool is_params;
12850 is_params = isl_set_is_params(set);
12851 if (is_params < 0)
12852 set = isl_set_free(set);
12853 else if (!is_params)
12854 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12855 "expecting parameter domain", set = isl_set_free(set));
12856 return set_from_map(unbind_params_insert_domain(set, tuple));
12859 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12861 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12863 isl_bool is_params;
12865 is_params = isl_set_is_params(set);
12866 if (is_params < 0)
12867 return isl_stat_error;
12868 else if (is_params)
12869 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12870 "expecting proper set", return isl_stat_error);
12872 return isl_stat_ok;
12875 /* Construct a map with "domain" as domain and "set" as range.
12876 * Any parameters with identifiers in "domain" are reinterpreted
12877 * as the corresponding domain dimensions.
12879 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12880 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12882 if (isl_set_check_is_set(set) < 0)
12883 set = isl_set_free(set);
12884 return unbind_params_insert_domain(set, domain);
12887 /* Construct a map with "domain" as domain and "set" as range.
12889 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12890 __isl_take isl_space *domain)
12892 isl_size dim;
12893 isl_space *space;
12894 isl_map *map;
12896 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12897 domain = isl_space_free(domain);
12898 dim = isl_space_dim(domain, isl_dim_set);
12899 if (dim < 0)
12900 domain = isl_space_free(domain);
12901 space = isl_set_get_space(set);
12902 domain = isl_space_replace_params(domain, space);
12903 space = isl_space_map_from_domain_and_range(domain, space);
12905 map = isl_map_from_range(set);
12906 map = isl_map_add_dims(map, isl_dim_in, dim);
12907 map = isl_map_reset_space(map, space);
12909 return map;
12912 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12913 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12914 enum isl_dim_type c2, enum isl_dim_type c3,
12915 enum isl_dim_type c4, enum isl_dim_type c5)
12917 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12918 struct isl_mat *mat;
12919 int i, j, k;
12920 int pos;
12921 isl_size total;
12923 total = isl_basic_map_dim(bmap, isl_dim_all);
12924 if (total < 0)
12925 return NULL;
12926 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12927 if (!mat)
12928 return NULL;
12929 for (i = 0; i < bmap->n_eq; ++i)
12930 for (j = 0, pos = 0; j < 5; ++j) {
12931 int off = isl_basic_map_offset(bmap, c[j]);
12932 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12933 if (dim < 0)
12934 return isl_mat_free(mat);
12935 for (k = 0; k < dim; ++k) {
12936 isl_int_set(mat->row[i][pos],
12937 bmap->eq[i][off + k]);
12938 ++pos;
12942 return mat;
12945 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12946 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12947 enum isl_dim_type c2, enum isl_dim_type c3,
12948 enum isl_dim_type c4, enum isl_dim_type c5)
12950 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12951 struct isl_mat *mat;
12952 int i, j, k;
12953 int pos;
12954 isl_size total;
12956 total = isl_basic_map_dim(bmap, isl_dim_all);
12957 if (total < 0)
12958 return NULL;
12959 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12960 if (!mat)
12961 return NULL;
12962 for (i = 0; i < bmap->n_ineq; ++i)
12963 for (j = 0, pos = 0; j < 5; ++j) {
12964 int off = isl_basic_map_offset(bmap, c[j]);
12965 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12966 if (dim < 0)
12967 return isl_mat_free(mat);
12968 for (k = 0; k < dim; ++k) {
12969 isl_int_set(mat->row[i][pos],
12970 bmap->ineq[i][off + k]);
12971 ++pos;
12975 return mat;
12978 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12979 __isl_take isl_space *space,
12980 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12981 enum isl_dim_type c2, enum isl_dim_type c3,
12982 enum isl_dim_type c4, enum isl_dim_type c5)
12984 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12985 isl_basic_map *bmap = NULL;
12986 isl_size dim;
12987 unsigned total;
12988 unsigned extra;
12989 int i, j, k, l;
12990 int pos;
12992 dim = isl_space_dim(space, isl_dim_all);
12993 if (dim < 0 || !eq || !ineq)
12994 goto error;
12996 if (eq->n_col != ineq->n_col)
12997 isl_die(space->ctx, isl_error_invalid,
12998 "equalities and inequalities matrices should have "
12999 "same number of columns", goto error);
13001 total = 1 + dim;
13003 if (eq->n_col < total)
13004 isl_die(space->ctx, isl_error_invalid,
13005 "number of columns too small", goto error);
13007 extra = eq->n_col - total;
13009 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13010 eq->n_row, ineq->n_row);
13011 if (!bmap)
13012 goto error;
13013 for (i = 0; i < extra; ++i) {
13014 k = isl_basic_map_alloc_div(bmap);
13015 if (k < 0)
13016 goto error;
13017 isl_int_set_si(bmap->div[k][0], 0);
13019 for (i = 0; i < eq->n_row; ++i) {
13020 l = isl_basic_map_alloc_equality(bmap);
13021 if (l < 0)
13022 goto error;
13023 for (j = 0, pos = 0; j < 5; ++j) {
13024 int off = isl_basic_map_offset(bmap, c[j]);
13025 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13026 if (dim < 0)
13027 goto error;
13028 for (k = 0; k < dim; ++k) {
13029 isl_int_set(bmap->eq[l][off + k],
13030 eq->row[i][pos]);
13031 ++pos;
13035 for (i = 0; i < ineq->n_row; ++i) {
13036 l = isl_basic_map_alloc_inequality(bmap);
13037 if (l < 0)
13038 goto error;
13039 for (j = 0, pos = 0; j < 5; ++j) {
13040 int off = isl_basic_map_offset(bmap, c[j]);
13041 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13042 if (dim < 0)
13043 goto error;
13044 for (k = 0; k < dim; ++k) {
13045 isl_int_set(bmap->ineq[l][off + k],
13046 ineq->row[i][pos]);
13047 ++pos;
13052 isl_space_free(space);
13053 isl_mat_free(eq);
13054 isl_mat_free(ineq);
13056 bmap = isl_basic_map_simplify(bmap);
13057 return isl_basic_map_finalize(bmap);
13058 error:
13059 isl_space_free(space);
13060 isl_mat_free(eq);
13061 isl_mat_free(ineq);
13062 isl_basic_map_free(bmap);
13063 return NULL;
13066 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13067 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13068 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13070 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13071 c1, c2, c3, c4, isl_dim_in);
13074 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13075 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13076 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13078 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13079 c1, c2, c3, c4, isl_dim_in);
13082 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13083 __isl_take isl_space *space,
13084 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13085 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13087 isl_basic_map *bmap;
13088 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13089 c1, c2, c3, c4, isl_dim_in);
13090 return bset_from_bmap(bmap);
13093 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13095 if (!bmap)
13096 return isl_bool_error;
13098 return isl_space_can_zip(bmap->dim);
13101 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13103 if (!map)
13104 return isl_bool_error;
13106 return isl_space_can_zip(map->dim);
13109 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13110 * (A -> C) -> (B -> D).
13112 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13114 unsigned pos;
13115 isl_size n_in;
13116 isl_size n1;
13117 isl_size n2;
13119 if (!bmap)
13120 return NULL;
13122 if (!isl_basic_map_can_zip(bmap))
13123 isl_die(bmap->ctx, isl_error_invalid,
13124 "basic map cannot be zipped", goto error);
13125 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13126 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13127 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13128 if (n_in < 0 || n1 < 0 || n2 < 0)
13129 return isl_basic_map_free(bmap);
13130 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13131 bmap = isl_basic_map_cow(bmap);
13132 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13133 if (!bmap)
13134 return NULL;
13135 bmap->dim = isl_space_zip(bmap->dim);
13136 if (!bmap->dim)
13137 goto error;
13138 bmap = isl_basic_map_mark_final(bmap);
13139 return bmap;
13140 error:
13141 isl_basic_map_free(bmap);
13142 return NULL;
13145 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13146 * (A -> C) -> (B -> D).
13148 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13150 if (!map)
13151 return NULL;
13153 if (!isl_map_can_zip(map))
13154 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13155 goto error);
13157 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13158 error:
13159 isl_map_free(map);
13160 return NULL;
13163 /* Can we apply isl_basic_map_curry to "bmap"?
13164 * That is, does it have a nested relation in its domain?
13166 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13168 if (!bmap)
13169 return isl_bool_error;
13171 return isl_space_can_curry(bmap->dim);
13174 /* Can we apply isl_map_curry to "map"?
13175 * That is, does it have a nested relation in its domain?
13177 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13179 if (!map)
13180 return isl_bool_error;
13182 return isl_space_can_curry(map->dim);
13185 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13186 * A -> (B -> C).
13188 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13191 if (!bmap)
13192 return NULL;
13194 if (!isl_basic_map_can_curry(bmap))
13195 isl_die(bmap->ctx, isl_error_invalid,
13196 "basic map cannot be curried", goto error);
13197 bmap = isl_basic_map_cow(bmap);
13198 if (!bmap)
13199 return NULL;
13200 bmap->dim = isl_space_curry(bmap->dim);
13201 if (!bmap->dim)
13202 goto error;
13203 bmap = isl_basic_map_mark_final(bmap);
13204 return bmap;
13205 error:
13206 isl_basic_map_free(bmap);
13207 return NULL;
13210 /* Given a map (A -> B) -> C, return the corresponding map
13211 * A -> (B -> C).
13213 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13215 return isl_map_change_space(map, &isl_map_can_curry,
13216 "map cannot be curried", &isl_space_curry);
13219 /* Can isl_map_range_curry be applied to "map"?
13220 * That is, does it have a nested relation in its range,
13221 * the domain of which is itself a nested relation?
13223 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13225 if (!map)
13226 return isl_bool_error;
13228 return isl_space_can_range_curry(map->dim);
13231 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13232 * A -> (B -> (C -> D)).
13234 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13236 return isl_map_change_space(map, &isl_map_can_range_curry,
13237 "map range cannot be curried",
13238 &isl_space_range_curry);
13241 /* Can we apply isl_basic_map_uncurry to "bmap"?
13242 * That is, does it have a nested relation in its domain?
13244 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13246 if (!bmap)
13247 return isl_bool_error;
13249 return isl_space_can_uncurry(bmap->dim);
13252 /* Can we apply isl_map_uncurry to "map"?
13253 * That is, does it have a nested relation in its domain?
13255 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13257 if (!map)
13258 return isl_bool_error;
13260 return isl_space_can_uncurry(map->dim);
13263 /* Given a basic map A -> (B -> C), return the corresponding basic map
13264 * (A -> B) -> C.
13266 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13269 if (!bmap)
13270 return NULL;
13272 if (!isl_basic_map_can_uncurry(bmap))
13273 isl_die(bmap->ctx, isl_error_invalid,
13274 "basic map cannot be uncurried",
13275 return isl_basic_map_free(bmap));
13276 bmap = isl_basic_map_cow(bmap);
13277 if (!bmap)
13278 return NULL;
13279 bmap->dim = isl_space_uncurry(bmap->dim);
13280 if (!bmap->dim)
13281 return isl_basic_map_free(bmap);
13282 bmap = isl_basic_map_mark_final(bmap);
13283 return bmap;
13286 /* Given a map A -> (B -> C), return the corresponding map
13287 * (A -> B) -> C.
13289 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13291 return isl_map_change_space(map, &isl_map_can_uncurry,
13292 "map cannot be uncurried", &isl_space_uncurry);
13295 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13296 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13298 return isl_map_equate(set, type1, pos1, type2, pos2);
13301 /* Construct a basic map where the given dimensions are equal to each other.
13303 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13304 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13306 isl_basic_map *bmap = NULL;
13307 int i;
13308 isl_size total;
13310 total = isl_space_dim(space, isl_dim_all);
13311 if (total < 0 ||
13312 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13313 isl_space_check_range(space, type2, pos2, 1) < 0)
13314 goto error;
13316 if (type1 == type2 && pos1 == pos2)
13317 return isl_basic_map_universe(space);
13319 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13320 i = isl_basic_map_alloc_equality(bmap);
13321 if (i < 0)
13322 goto error;
13323 isl_seq_clr(bmap->eq[i], 1 + total);
13324 pos1 += isl_basic_map_offset(bmap, type1);
13325 pos2 += isl_basic_map_offset(bmap, type2);
13326 isl_int_set_si(bmap->eq[i][pos1], -1);
13327 isl_int_set_si(bmap->eq[i][pos2], 1);
13328 bmap = isl_basic_map_finalize(bmap);
13329 isl_space_free(space);
13330 return bmap;
13331 error:
13332 isl_space_free(space);
13333 isl_basic_map_free(bmap);
13334 return NULL;
13337 /* Add a constraint imposing that the given two dimensions are equal.
13339 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13342 isl_basic_map *eq;
13344 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13346 bmap = isl_basic_map_intersect(bmap, eq);
13348 return bmap;
13351 /* Add a constraint imposing that the given two dimensions are equal.
13353 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13354 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13356 isl_basic_map *bmap;
13358 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13360 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13362 return map;
13365 /* Add a constraint imposing that the given two dimensions have opposite values.
13367 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13368 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13370 isl_basic_map *bmap = NULL;
13371 int i;
13372 isl_size total;
13374 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13375 return isl_map_free(map);
13376 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13377 return isl_map_free(map);
13379 total = isl_map_dim(map, isl_dim_all);
13380 if (total < 0)
13381 return isl_map_free(map);
13382 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13383 i = isl_basic_map_alloc_equality(bmap);
13384 if (i < 0)
13385 goto error;
13386 isl_seq_clr(bmap->eq[i], 1 + total);
13387 pos1 += isl_basic_map_offset(bmap, type1);
13388 pos2 += isl_basic_map_offset(bmap, type2);
13389 isl_int_set_si(bmap->eq[i][pos1], 1);
13390 isl_int_set_si(bmap->eq[i][pos2], 1);
13391 bmap = isl_basic_map_finalize(bmap);
13393 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13395 return map;
13396 error:
13397 isl_basic_map_free(bmap);
13398 isl_map_free(map);
13399 return NULL;
13402 /* Construct a constraint imposing that the value of the first dimension is
13403 * greater than or equal to that of the second.
13405 static __isl_give isl_constraint *constraint_order_ge(
13406 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13407 enum isl_dim_type type2, int pos2)
13409 isl_constraint *c;
13411 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13412 isl_space_check_range(space, type2, pos2, 1) < 0)
13413 space = isl_space_free(space);
13414 if (!space)
13415 return NULL;
13417 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13419 if (type1 == type2 && pos1 == pos2)
13420 return c;
13422 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13423 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13425 return c;
13428 /* Add a constraint imposing that the value of the first dimension is
13429 * greater than or equal to that of the second.
13431 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13432 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13434 isl_constraint *c;
13435 isl_space *space;
13437 if (type1 == type2 && pos1 == pos2)
13438 return bmap;
13439 space = isl_basic_map_get_space(bmap);
13440 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13441 bmap = isl_basic_map_add_constraint(bmap, c);
13443 return bmap;
13446 /* Add a constraint imposing that the value of the first dimension is
13447 * greater than or equal to that of the second.
13449 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13450 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13452 isl_constraint *c;
13453 isl_space *space;
13455 if (type1 == type2 && pos1 == pos2)
13456 return map;
13457 space = isl_map_get_space(map);
13458 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13459 map = isl_map_add_constraint(map, c);
13461 return map;
13464 /* Add a constraint imposing that the value of the first dimension is
13465 * less than or equal to that of the second.
13467 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13468 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13470 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13473 /* Construct a basic map where the value of the first dimension is
13474 * greater than that of the second.
13476 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13477 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13479 isl_basic_map *bmap = NULL;
13480 int i;
13481 isl_size total;
13483 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13484 isl_space_check_range(space, type2, pos2, 1) < 0)
13485 goto error;
13487 if (type1 == type2 && pos1 == pos2)
13488 return isl_basic_map_empty(space);
13490 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13491 total = isl_basic_map_dim(bmap, isl_dim_all);
13492 i = isl_basic_map_alloc_inequality(bmap);
13493 if (total < 0 || i < 0)
13494 return isl_basic_map_free(bmap);
13495 isl_seq_clr(bmap->ineq[i], 1 + total);
13496 pos1 += isl_basic_map_offset(bmap, type1);
13497 pos2 += isl_basic_map_offset(bmap, type2);
13498 isl_int_set_si(bmap->ineq[i][pos1], 1);
13499 isl_int_set_si(bmap->ineq[i][pos2], -1);
13500 isl_int_set_si(bmap->ineq[i][0], -1);
13501 bmap = isl_basic_map_finalize(bmap);
13503 return bmap;
13504 error:
13505 isl_space_free(space);
13506 isl_basic_map_free(bmap);
13507 return NULL;
13510 /* Add a constraint imposing that the value of the first dimension is
13511 * greater than that of the second.
13513 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13514 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13516 isl_basic_map *gt;
13518 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13520 bmap = isl_basic_map_intersect(bmap, gt);
13522 return bmap;
13525 /* Add a constraint imposing that the value of the first dimension is
13526 * greater than that of the second.
13528 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13529 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13531 isl_basic_map *bmap;
13533 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13535 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13537 return map;
13540 /* Add a constraint imposing that the value of the first dimension is
13541 * smaller than that of the second.
13543 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13544 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13546 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13549 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13550 int pos)
13552 isl_aff *div;
13553 isl_local_space *ls;
13555 if (!bmap)
13556 return NULL;
13558 if (!isl_basic_map_divs_known(bmap))
13559 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13560 "some divs are unknown", return NULL);
13562 ls = isl_basic_map_get_local_space(bmap);
13563 div = isl_local_space_get_div(ls, pos);
13564 isl_local_space_free(ls);
13566 return div;
13569 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13570 int pos)
13572 return isl_basic_map_get_div(bset, pos);
13575 /* Plug in "subs" for set dimension "pos" of "set".
13577 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13578 unsigned pos, __isl_keep isl_aff *subs)
13580 isl_multi_aff *ma;
13582 if (set && isl_set_plain_is_empty(set))
13583 return set;
13585 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13586 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13587 return isl_set_preimage_multi_aff(set, ma);
13590 /* Check if the range of "ma" is compatible with the domain or range
13591 * (depending on "type") of "bmap".
13593 static isl_stat check_basic_map_compatible_range_multi_aff(
13594 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13595 __isl_keep isl_multi_aff *ma)
13597 isl_bool m;
13598 isl_space *ma_space;
13600 ma_space = isl_multi_aff_get_space(ma);
13602 m = isl_space_has_equal_params(bmap->dim, ma_space);
13603 if (m < 0)
13604 goto error;
13605 if (!m)
13606 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13607 "parameters don't match", goto error);
13608 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13609 if (m < 0)
13610 goto error;
13611 if (!m)
13612 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13613 "spaces don't match", goto error);
13615 isl_space_free(ma_space);
13616 return isl_stat_ok;
13617 error:
13618 isl_space_free(ma_space);
13619 return isl_stat_error;
13622 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13623 * coefficients before the transformed range of dimensions,
13624 * the "n_after" coefficients after the transformed range of dimensions
13625 * and the coefficients of the other divs in "bmap".
13627 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13628 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13630 int i;
13631 isl_size n_param;
13632 isl_size n_set;
13633 isl_local_space *ls;
13635 if (n_div == 0)
13636 return bmap;
13638 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13639 n_param = isl_local_space_dim(ls, isl_dim_param);
13640 n_set = isl_local_space_dim(ls, isl_dim_set);
13641 if (n_param < 0 || n_set < 0)
13642 return isl_basic_map_free(bmap);
13644 for (i = 0; i < n_div; ++i) {
13645 int o_bmap = 0, o_ls = 0;
13647 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13648 o_bmap += 1 + 1 + n_param;
13649 o_ls += 1 + 1 + n_param;
13650 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13651 o_bmap += n_before;
13652 isl_seq_cpy(bmap->div[i] + o_bmap,
13653 ls->div->row[i] + o_ls, n_set);
13654 o_bmap += n_set;
13655 o_ls += n_set;
13656 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13657 o_bmap += n_after;
13658 isl_seq_cpy(bmap->div[i] + o_bmap,
13659 ls->div->row[i] + o_ls, n_div);
13660 o_bmap += n_div;
13661 o_ls += n_div;
13662 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13663 bmap = isl_basic_map_add_div_constraints(bmap, i);
13664 if (!bmap)
13665 goto error;
13668 isl_local_space_free(ls);
13669 return bmap;
13670 error:
13671 isl_local_space_free(ls);
13672 return isl_basic_map_free(bmap);
13675 /* How many stride constraints does "ma" enforce?
13676 * That is, how many of the affine expressions have a denominator
13677 * different from one?
13679 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13681 int i;
13682 int strides = 0;
13684 for (i = 0; i < ma->n; ++i)
13685 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13686 strides++;
13688 return strides;
13691 /* For each affine expression in ma of the form
13693 * x_i = (f_i y + h_i)/m_i
13695 * with m_i different from one, add a constraint to "bmap"
13696 * of the form
13698 * f_i y + h_i = m_i alpha_i
13700 * with alpha_i an additional existentially quantified variable.
13702 * The input variables of "ma" correspond to a subset of the variables
13703 * of "bmap". There are "n_before" variables in "bmap" before this
13704 * subset and "n_after" variables after this subset.
13705 * The integer divisions of the affine expressions in "ma" are assumed
13706 * to have been aligned. There are "n_div_ma" of them and
13707 * they appear first in "bmap", straight after the "n_after" variables.
13709 static __isl_give isl_basic_map *add_ma_strides(
13710 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13711 int n_before, int n_after, int n_div_ma)
13713 int i, k;
13714 int div;
13715 isl_size total;
13716 isl_size n_param;
13717 isl_size n_in;
13719 total = isl_basic_map_dim(bmap, isl_dim_all);
13720 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13721 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13722 if (total < 0 || n_param < 0 || n_in < 0)
13723 return isl_basic_map_free(bmap);
13724 for (i = 0; i < ma->n; ++i) {
13725 int o_bmap = 0, o_ma = 1;
13727 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13728 continue;
13729 div = isl_basic_map_alloc_div(bmap);
13730 k = isl_basic_map_alloc_equality(bmap);
13731 if (div < 0 || k < 0)
13732 goto error;
13733 isl_int_set_si(bmap->div[div][0], 0);
13734 isl_seq_cpy(bmap->eq[k] + o_bmap,
13735 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13736 o_bmap += 1 + n_param;
13737 o_ma += 1 + n_param;
13738 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13739 o_bmap += n_before;
13740 isl_seq_cpy(bmap->eq[k] + o_bmap,
13741 ma->u.p[i]->v->el + o_ma, n_in);
13742 o_bmap += n_in;
13743 o_ma += n_in;
13744 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13745 o_bmap += n_after;
13746 isl_seq_cpy(bmap->eq[k] + o_bmap,
13747 ma->u.p[i]->v->el + o_ma, n_div_ma);
13748 o_bmap += n_div_ma;
13749 o_ma += n_div_ma;
13750 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13751 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13752 total++;
13755 return bmap;
13756 error:
13757 isl_basic_map_free(bmap);
13758 return NULL;
13761 /* Replace the domain or range space (depending on "type) of "space" by "set".
13763 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13764 enum isl_dim_type type, __isl_take isl_space *set)
13766 if (type == isl_dim_in) {
13767 space = isl_space_range(space);
13768 space = isl_space_map_from_domain_and_range(set, space);
13769 } else {
13770 space = isl_space_domain(space);
13771 space = isl_space_map_from_domain_and_range(space, set);
13774 return space;
13777 /* Compute the preimage of the domain or range (depending on "type")
13778 * of "bmap" under the function represented by "ma".
13779 * In other words, plug in "ma" in the domain or range of "bmap".
13780 * The result is a basic map that lives in the same space as "bmap"
13781 * except that the domain or range has been replaced by
13782 * the domain space of "ma".
13784 * If bmap is represented by
13786 * A(p) + S u + B x + T v + C(divs) >= 0,
13788 * where u and x are input and output dimensions if type == isl_dim_out
13789 * while x and v are input and output dimensions if type == isl_dim_in,
13790 * and ma is represented by
13792 * x = D(p) + F(y) + G(divs')
13794 * then the result is
13796 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13798 * The divs in the input set are similarly adjusted.
13799 * In particular
13801 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13803 * becomes
13805 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13806 * B_i G(divs') + c_i(divs))/n_i)
13808 * If bmap is not a rational map and if F(y) involves any denominators
13810 * x_i = (f_i y + h_i)/m_i
13812 * then additional constraints are added to ensure that we only
13813 * map back integer points. That is we enforce
13815 * f_i y + h_i = m_i alpha_i
13817 * with alpha_i an additional existentially quantified variable.
13819 * We first copy over the divs from "ma".
13820 * Then we add the modified constraints and divs from "bmap".
13821 * Finally, we add the stride constraints, if needed.
13823 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13824 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13825 __isl_take isl_multi_aff *ma)
13827 int i, k;
13828 isl_space *space;
13829 isl_basic_map *res = NULL;
13830 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13831 isl_int f, c1, c2, g;
13832 isl_bool rational;
13833 int strides;
13835 isl_int_init(f);
13836 isl_int_init(c1);
13837 isl_int_init(c2);
13838 isl_int_init(g);
13840 ma = isl_multi_aff_align_divs(ma);
13841 if (!bmap || !ma)
13842 goto error;
13843 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13844 goto error;
13846 if (type == isl_dim_in) {
13847 n_before = 0;
13848 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13849 } else {
13850 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13851 n_after = 0;
13853 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13854 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13855 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13856 goto error;
13858 space = isl_multi_aff_get_domain_space(ma);
13859 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13860 rational = isl_basic_map_is_rational(bmap);
13861 strides = rational ? 0 : multi_aff_strides(ma);
13862 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13863 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13864 if (rational)
13865 res = isl_basic_map_set_rational(res);
13867 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13868 if (isl_basic_map_alloc_div(res) < 0)
13869 goto error;
13871 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13872 if (!res)
13873 goto error;
13875 for (i = 0; i < bmap->n_eq; ++i) {
13876 k = isl_basic_map_alloc_equality(res);
13877 if (k < 0)
13878 goto error;
13879 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13880 n_after, n_div_ma, n_div_bmap,
13881 f, c1, c2, g, 0) < 0)
13882 goto error;
13885 for (i = 0; i < bmap->n_ineq; ++i) {
13886 k = isl_basic_map_alloc_inequality(res);
13887 if (k < 0)
13888 goto error;
13889 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13890 n_after, n_div_ma, n_div_bmap,
13891 f, c1, c2, g, 0) < 0)
13892 goto error;
13895 for (i = 0; i < bmap->n_div; ++i) {
13896 if (isl_int_is_zero(bmap->div[i][0])) {
13897 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13898 continue;
13900 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13901 n_before, n_after, n_div_ma, n_div_bmap,
13902 f, c1, c2, g, 1) < 0)
13903 goto error;
13906 if (strides)
13907 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13909 isl_int_clear(f);
13910 isl_int_clear(c1);
13911 isl_int_clear(c2);
13912 isl_int_clear(g);
13913 isl_basic_map_free(bmap);
13914 isl_multi_aff_free(ma);
13915 res = isl_basic_map_simplify(res);
13916 return isl_basic_map_finalize(res);
13917 error:
13918 isl_int_clear(f);
13919 isl_int_clear(c1);
13920 isl_int_clear(c2);
13921 isl_int_clear(g);
13922 isl_basic_map_free(bmap);
13923 isl_multi_aff_free(ma);
13924 isl_basic_map_free(res);
13925 return NULL;
13928 /* Compute the preimage of "bset" under the function represented by "ma".
13929 * In other words, plug in "ma" in "bset". The result is a basic set
13930 * that lives in the domain space of "ma".
13932 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13933 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13935 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13938 /* Compute the preimage of the domain of "bmap" under the function
13939 * represented by "ma".
13940 * In other words, plug in "ma" in the domain of "bmap".
13941 * The result is a basic map that lives in the same space as "bmap"
13942 * except that the domain has been replaced by the domain space of "ma".
13944 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13945 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13947 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13950 /* Compute the preimage of the range of "bmap" under the function
13951 * represented by "ma".
13952 * In other words, plug in "ma" in the range of "bmap".
13953 * The result is a basic map that lives in the same space as "bmap"
13954 * except that the range has been replaced by the domain space of "ma".
13956 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13957 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13959 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13962 /* Check if the range of "ma" is compatible with the domain or range
13963 * (depending on "type") of "map".
13964 * Return isl_stat_error if anything is wrong.
13966 static isl_stat check_map_compatible_range_multi_aff(
13967 __isl_keep isl_map *map, enum isl_dim_type type,
13968 __isl_keep isl_multi_aff *ma)
13970 isl_bool m;
13971 isl_space *ma_space;
13973 ma_space = isl_multi_aff_get_space(ma);
13974 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13975 isl_space_free(ma_space);
13976 if (m < 0)
13977 return isl_stat_error;
13978 if (!m)
13979 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13980 "spaces don't match", return isl_stat_error);
13981 return isl_stat_ok;
13984 /* Compute the preimage of the domain or range (depending on "type")
13985 * of "map" under the function represented by "ma".
13986 * In other words, plug in "ma" in the domain or range of "map".
13987 * The result is a map that lives in the same space as "map"
13988 * except that the domain or range has been replaced by
13989 * the domain space of "ma".
13991 * The parameters are assumed to have been aligned.
13993 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13994 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13996 int i;
13997 isl_space *space;
13999 map = isl_map_cow(map);
14000 ma = isl_multi_aff_align_divs(ma);
14001 if (!map || !ma)
14002 goto error;
14003 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14004 goto error;
14006 for (i = 0; i < map->n; ++i) {
14007 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14008 isl_multi_aff_copy(ma));
14009 if (!map->p[i])
14010 goto error;
14013 space = isl_multi_aff_get_domain_space(ma);
14014 space = isl_space_set(isl_map_get_space(map), type, space);
14016 isl_space_free(isl_map_take_space(map));
14017 map = isl_map_restore_space(map, space);
14018 if (!map)
14019 goto error;
14021 isl_multi_aff_free(ma);
14022 if (map->n > 1)
14023 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14024 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14025 return map;
14026 error:
14027 isl_multi_aff_free(ma);
14028 isl_map_free(map);
14029 return NULL;
14032 /* Compute the preimage of the domain or range (depending on "type")
14033 * of "map" under the function represented by "ma".
14034 * In other words, plug in "ma" in the domain or range of "map".
14035 * The result is a map that lives in the same space as "map"
14036 * except that the domain or range has been replaced by
14037 * the domain space of "ma".
14039 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14040 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14042 isl_bool aligned;
14044 if (!map || !ma)
14045 goto error;
14047 aligned = isl_map_space_has_equal_params(map, ma->space);
14048 if (aligned < 0)
14049 goto error;
14050 if (aligned)
14051 return map_preimage_multi_aff(map, type, ma);
14053 if (isl_map_check_named_params(map) < 0)
14054 goto error;
14055 if (!isl_space_has_named_params(ma->space))
14056 isl_die(map->ctx, isl_error_invalid,
14057 "unaligned unnamed parameters", goto error);
14058 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14059 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14061 return map_preimage_multi_aff(map, type, ma);
14062 error:
14063 isl_multi_aff_free(ma);
14064 return isl_map_free(map);
14067 /* Compute the preimage of "set" under the function represented by "ma".
14068 * In other words, plug in "ma" in "set". The result is a set
14069 * that lives in the domain space of "ma".
14071 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14072 __isl_take isl_multi_aff *ma)
14074 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14077 /* Compute the preimage of the domain of "map" under the function
14078 * represented by "ma".
14079 * In other words, plug in "ma" in the domain of "map".
14080 * The result is a map that lives in the same space as "map"
14081 * except that the domain has been replaced by the domain space of "ma".
14083 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14084 __isl_take isl_multi_aff *ma)
14086 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14089 /* Compute the preimage of the range of "map" under the function
14090 * represented by "ma".
14091 * In other words, plug in "ma" in the range of "map".
14092 * The result is a map that lives in the same space as "map"
14093 * except that the range has been replaced by the domain space of "ma".
14095 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14096 __isl_take isl_multi_aff *ma)
14098 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14101 /* Compute the preimage of "map" under the function represented by "pma".
14102 * In other words, plug in "pma" in the domain or range of "map".
14103 * The result is a map that lives in the same space as "map",
14104 * except that the space of type "type" has been replaced by
14105 * the domain space of "pma".
14107 * The parameters of "map" and "pma" are assumed to have been aligned.
14109 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14110 __isl_take isl_map *map, enum isl_dim_type type,
14111 __isl_take isl_pw_multi_aff *pma)
14113 int i;
14114 isl_map *res;
14116 if (!pma)
14117 goto error;
14119 if (pma->n == 0) {
14120 isl_pw_multi_aff_free(pma);
14121 res = isl_map_empty(isl_map_get_space(map));
14122 isl_map_free(map);
14123 return res;
14126 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14127 isl_multi_aff_copy(pma->p[0].maff));
14128 if (type == isl_dim_in)
14129 res = isl_map_intersect_domain(res,
14130 isl_map_copy(pma->p[0].set));
14131 else
14132 res = isl_map_intersect_range(res,
14133 isl_map_copy(pma->p[0].set));
14135 for (i = 1; i < pma->n; ++i) {
14136 isl_map *res_i;
14138 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14139 isl_multi_aff_copy(pma->p[i].maff));
14140 if (type == isl_dim_in)
14141 res_i = isl_map_intersect_domain(res_i,
14142 isl_map_copy(pma->p[i].set));
14143 else
14144 res_i = isl_map_intersect_range(res_i,
14145 isl_map_copy(pma->p[i].set));
14146 res = isl_map_union(res, res_i);
14149 isl_pw_multi_aff_free(pma);
14150 isl_map_free(map);
14151 return res;
14152 error:
14153 isl_pw_multi_aff_free(pma);
14154 isl_map_free(map);
14155 return NULL;
14158 /* Compute the preimage of "map" under the function represented by "pma".
14159 * In other words, plug in "pma" in the domain or range of "map".
14160 * The result is a map that lives in the same space as "map",
14161 * except that the space of type "type" has been replaced by
14162 * the domain space of "pma".
14164 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14165 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14167 isl_bool aligned;
14169 if (!map || !pma)
14170 goto error;
14172 aligned = isl_map_space_has_equal_params(map, pma->dim);
14173 if (aligned < 0)
14174 goto error;
14175 if (aligned)
14176 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14178 if (isl_map_check_named_params(map) < 0)
14179 goto error;
14180 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14181 goto error;
14182 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14183 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14185 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14186 error:
14187 isl_pw_multi_aff_free(pma);
14188 return isl_map_free(map);
14191 /* Compute the preimage of "set" under the function represented by "pma".
14192 * In other words, plug in "pma" in "set". The result is a set
14193 * that lives in the domain space of "pma".
14195 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14196 __isl_take isl_pw_multi_aff *pma)
14198 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14201 /* Compute the preimage of the domain of "map" under the function
14202 * represented by "pma".
14203 * In other words, plug in "pma" in the domain of "map".
14204 * The result is a map that lives in the same space as "map",
14205 * except that domain space has been replaced by the domain space of "pma".
14207 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14208 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14210 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14213 /* Compute the preimage of the range of "map" under the function
14214 * represented by "pma".
14215 * In other words, plug in "pma" in the range of "map".
14216 * The result is a map that lives in the same space as "map",
14217 * except that range space has been replaced by the domain space of "pma".
14219 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14220 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14222 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14225 /* Compute the preimage of "map" under the function represented by "mpa".
14226 * In other words, plug in "mpa" in the domain or range of "map".
14227 * The result is a map that lives in the same space as "map",
14228 * except that the space of type "type" has been replaced by
14229 * the domain space of "mpa".
14231 * If the map does not involve any constraints that refer to the
14232 * dimensions of the substituted space, then the only possible
14233 * effect of "mpa" on the map is to map the space to a different space.
14234 * We create a separate isl_multi_aff to effectuate this change
14235 * in order to avoid spurious splitting of the map along the pieces
14236 * of "mpa".
14237 * If "mpa" has a non-trivial explicit domain, however,
14238 * then the full substitution should be performed.
14240 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14241 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14243 isl_size n;
14244 isl_bool full;
14245 isl_pw_multi_aff *pma;
14247 n = isl_map_dim(map, type);
14248 if (n < 0 || !mpa)
14249 goto error;
14251 full = isl_map_involves_dims(map, type, 0, n);
14252 if (full >= 0 && !full)
14253 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14254 if (full < 0)
14255 goto error;
14256 if (!full) {
14257 isl_space *space;
14258 isl_multi_aff *ma;
14260 space = isl_multi_pw_aff_get_space(mpa);
14261 isl_multi_pw_aff_free(mpa);
14262 ma = isl_multi_aff_zero(space);
14263 return isl_map_preimage_multi_aff(map, type, ma);
14266 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14267 return isl_map_preimage_pw_multi_aff(map, type, pma);
14268 error:
14269 isl_map_free(map);
14270 isl_multi_pw_aff_free(mpa);
14271 return NULL;
14274 /* Compute the preimage of "map" under the function represented by "mpa".
14275 * In other words, plug in "mpa" in the domain "map".
14276 * The result is a map that lives in the same space as "map",
14277 * except that domain space has been replaced by the domain space of "mpa".
14279 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14280 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14282 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14285 /* Compute the preimage of "set" by the function represented by "mpa".
14286 * In other words, plug in "mpa" in "set".
14288 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14289 __isl_take isl_multi_pw_aff *mpa)
14291 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14294 /* Return a copy of the equality constraints of "bset" as a matrix.
14296 __isl_give isl_mat *isl_basic_set_extract_equalities(
14297 __isl_keep isl_basic_set *bset)
14299 isl_ctx *ctx;
14300 isl_size total;
14302 total = isl_basic_set_dim(bset, isl_dim_all);
14303 if (total < 0)
14304 return NULL;
14306 ctx = isl_basic_set_get_ctx(bset);
14307 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14310 /* Are the "n" "coefficients" starting at "first" of the integer division
14311 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14312 * to each other?
14313 * The "coefficient" at position 0 is the denominator.
14314 * The "coefficient" at position 1 is the constant term.
14316 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14317 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14318 unsigned first, unsigned n)
14320 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14321 return isl_bool_error;
14322 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14323 return isl_bool_error;
14324 return isl_seq_eq(bmap1->div[pos1] + first,
14325 bmap2->div[pos2] + first, n);
14328 /* Are the integer division expressions at position "pos1" in "bmap1" and
14329 * "pos2" in "bmap2" equal to each other, except that the constant terms
14330 * are different?
14332 isl_bool isl_basic_map_equal_div_expr_except_constant(
14333 __isl_keep isl_basic_map *bmap1, int pos1,
14334 __isl_keep isl_basic_map *bmap2, int pos2)
14336 isl_bool equal;
14337 isl_size total, total2;
14339 total = isl_basic_map_dim(bmap1, isl_dim_all);
14340 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14341 if (total < 0 || total2 < 0)
14342 return isl_bool_error;
14343 if (total != total2)
14344 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14345 "incomparable div expressions", return isl_bool_error);
14346 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14347 0, 1);
14348 if (equal < 0 || !equal)
14349 return equal;
14350 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14351 1, 1);
14352 if (equal < 0 || equal)
14353 return isl_bool_not(equal);
14354 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14355 2, total);
14358 /* Replace the numerator of the constant term of the integer division
14359 * expression at position "div" in "bmap" by "value".
14360 * The caller guarantees that this does not change the meaning
14361 * of the input.
14363 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14364 __isl_take isl_basic_map *bmap, int div, int value)
14366 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14367 return isl_basic_map_free(bmap);
14369 isl_int_set_si(bmap->div[div][1], value);
14371 return bmap;
14374 /* Is the point "inner" internal to inequality constraint "ineq"
14375 * of "bset"?
14376 * The point is considered to be internal to the inequality constraint,
14377 * if it strictly lies on the positive side of the inequality constraint,
14378 * or if it lies on the constraint and the constraint is lexico-positive.
14380 static isl_bool is_internal(__isl_keep isl_vec *inner,
14381 __isl_keep isl_basic_set *bset, int ineq)
14383 isl_ctx *ctx;
14384 int pos;
14385 isl_size total;
14387 if (!inner || !bset)
14388 return isl_bool_error;
14390 ctx = isl_basic_set_get_ctx(bset);
14391 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14392 &ctx->normalize_gcd);
14393 if (!isl_int_is_zero(ctx->normalize_gcd))
14394 return isl_int_is_nonneg(ctx->normalize_gcd);
14396 total = isl_basic_set_dim(bset, isl_dim_all);
14397 if (total < 0)
14398 return isl_bool_error;
14399 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14400 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14403 /* Tighten the inequality constraints of "bset" that are outward with respect
14404 * to the point "vec".
14405 * That is, tighten the constraints that are not satisfied by "vec".
14407 * "vec" is a point internal to some superset S of "bset" that is used
14408 * to make the subsets of S disjoint, by tightening one half of the constraints
14409 * that separate two subsets. In particular, the constraints of S
14410 * are all satisfied by "vec" and should not be tightened.
14411 * Of the internal constraints, those that have "vec" on the outside
14412 * are tightened. The shared facet is included in the adjacent subset
14413 * with the opposite constraint.
14414 * For constraints that saturate "vec", this criterion cannot be used
14415 * to determine which of the two sides should be tightened.
14416 * Instead, the sign of the first non-zero coefficient is used
14417 * to make this choice. Note that this second criterion is never used
14418 * on the constraints of S since "vec" is interior to "S".
14420 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14421 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14423 int j;
14425 bset = isl_basic_set_cow(bset);
14426 if (!bset)
14427 return NULL;
14428 for (j = 0; j < bset->n_ineq; ++j) {
14429 isl_bool internal;
14431 internal = is_internal(vec, bset, j);
14432 if (internal < 0)
14433 return isl_basic_set_free(bset);
14434 if (internal)
14435 continue;
14436 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14439 return bset;
14442 /* Replace the variables x of type "type" starting at "first" in "bmap"
14443 * by x' with x = M x' with M the matrix trans.
14444 * That is, replace the corresponding coefficients c by c M.
14446 * The transformation matrix should be a square matrix.
14448 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14449 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14450 __isl_take isl_mat *trans)
14452 unsigned pos;
14454 bmap = isl_basic_map_cow(bmap);
14455 if (!bmap || !trans)
14456 goto error;
14458 if (trans->n_row != trans->n_col)
14459 isl_die(trans->ctx, isl_error_invalid,
14460 "expecting square transformation matrix", goto error);
14461 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14462 goto error;
14464 pos = isl_basic_map_offset(bmap, type) + first;
14466 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14467 isl_mat_copy(trans)) < 0)
14468 goto error;
14469 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14470 isl_mat_copy(trans)) < 0)
14471 goto error;
14472 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14473 isl_mat_copy(trans)) < 0)
14474 goto error;
14476 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14479 isl_mat_free(trans);
14480 return bmap;
14481 error:
14482 isl_mat_free(trans);
14483 isl_basic_map_free(bmap);
14484 return NULL;
14487 /* Replace the variables x of type "type" starting at "first" in "bset"
14488 * by x' with x = M x' with M the matrix trans.
14489 * That is, replace the corresponding coefficients c by c M.
14491 * The transformation matrix should be a square matrix.
14493 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14494 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14495 __isl_take isl_mat *trans)
14497 return isl_basic_map_transform_dims(bset, type, first, trans);