isl_test_cpp17-generic.cc: work around std::optional::value issue in older macOS
[isl.git] / isl_map.c
blobb3f24aed2e017a85a910f102fb6f48ca79617ddf
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 2021-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
23 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
26 #include <string.h>
27 #include <isl_ctx_private.h>
28 #include <isl_map_private.h>
29 #include <isl_blk.h>
30 #include <isl_id_private.h>
31 #include <isl/constraint.h>
32 #include "isl_space_private.h"
33 #include "isl_equalities.h"
34 #include <isl_lp_private.h>
35 #include <isl_seq.h>
36 #include <isl/set.h>
37 #include <isl/map.h>
38 #include <isl_reordering.h>
39 #include "isl_sample.h"
40 #include <isl_sort.h>
41 #include "isl_tab.h"
42 #include <isl/vec.h>
43 #include <isl_mat_private.h>
44 #include <isl_vec_private.h>
45 #include <isl_dim_map.h>
46 #include <isl_local_space_private.h>
47 #include <isl_aff_private.h>
48 #include <isl_options_private.h>
49 #include <isl_morph.h>
50 #include <isl_val_private.h>
51 #include <isl_printer_private.h>
52 #include <isl_maybe_aff.h>
54 #include <bset_to_bmap.c>
55 #include <bset_from_bmap.c>
56 #include <set_to_map.c>
57 #include <set_from_map.c>
59 /* Treat "bset" as a basic map.
60 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
61 * this function performs a redundant cast.
63 static __isl_keep const isl_basic_map *const_bset_to_bmap(
64 __isl_keep const isl_basic_set *bset)
66 return (const isl_basic_map *) bset;
69 #undef TYPE
70 #define TYPE isl_basic_map
71 #include "has_single_reference_templ.c"
73 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
75 switch (type) {
76 case isl_dim_param: return 1;
77 case isl_dim_in: return 1 + space->nparam;
78 case isl_dim_out: return 1 + space->nparam + space->n_in;
79 default: return 0;
83 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
84 enum isl_dim_type type)
86 if (!bmap)
87 return isl_size_error;
88 switch (type) {
89 case isl_dim_cst: return 1;
90 case isl_dim_param:
91 case isl_dim_in:
92 case isl_dim_out: return isl_space_dim(bmap->dim, type);
93 case isl_dim_div: return bmap->n_div;
94 case isl_dim_all: return isl_basic_map_total_dim(bmap);
95 default: return 0;
99 /* Return the space of "map".
101 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
103 return map ? map->dim : NULL;
106 /* Return the space of "set".
108 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
110 return isl_map_peek_space(set_to_map(set));
113 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
115 return isl_space_dim(isl_map_peek_space(map), type);
118 /* Return the dimensionality of the domain (tuple) of the map.
120 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
122 return isl_map_dim(map, isl_dim_in);
125 /* Return the dimensionality of the range (tuple) of the map.
127 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
129 return isl_map_dim(map, isl_dim_out);
132 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
134 return isl_map_dim(set_to_map(set), type);
137 /* Return the dimensionality of the (tuple of the) set.
139 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
141 return isl_set_dim(set, isl_dim_set);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bmap".
147 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
148 enum isl_dim_type type)
150 isl_space *space;
152 space = isl_basic_map_peek_space(bmap);
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_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1758 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1759 bmap->extra - bmap->n_div);
1760 return bmap->n_ineq++;
1763 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1765 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1768 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1769 __isl_take isl_basic_map *bmap, unsigned n)
1771 if (!bmap)
1772 return NULL;
1773 if (n > bmap->n_ineq)
1774 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1775 "invalid number of inequalities",
1776 return isl_basic_map_free(bmap));
1777 bmap->n_ineq -= n;
1778 return bmap;
1781 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1782 __isl_take isl_basic_set *bset, unsigned n)
1784 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1785 n));
1788 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1790 isl_int *t;
1791 if (!bmap)
1792 return -1;
1793 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1795 if (pos != bmap->n_ineq - 1) {
1796 t = bmap->ineq[pos];
1797 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1798 bmap->ineq[bmap->n_ineq - 1] = t;
1799 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1801 bmap->n_ineq--;
1802 return 0;
1805 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1807 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1810 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1811 isl_int *eq)
1813 isl_bool empty;
1814 isl_size total;
1815 int k;
1817 empty = isl_basic_map_plain_is_empty(bmap);
1818 if (empty < 0)
1819 return isl_basic_map_free(bmap);
1820 if (empty)
1821 return bmap;
1823 bmap = isl_basic_map_cow(bmap);
1824 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1825 total = isl_basic_map_dim(bmap, isl_dim_all);
1826 if (total < 0)
1827 return isl_basic_map_free(bmap);
1828 k = isl_basic_map_alloc_equality(bmap);
1829 if (k < 0)
1830 goto error;
1831 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1832 return bmap;
1833 error:
1834 isl_basic_map_free(bmap);
1835 return NULL;
1838 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1839 isl_int *eq)
1841 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1844 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1845 isl_int *ineq)
1847 isl_size total;
1848 int k;
1850 bmap = isl_basic_map_cow(bmap);
1851 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1852 total = isl_basic_map_dim(bmap, isl_dim_all);
1853 if (total < 0)
1854 return isl_basic_map_free(bmap);
1855 k = isl_basic_map_alloc_inequality(bmap);
1856 if (k < 0)
1857 goto error;
1858 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1859 return bmap;
1860 error:
1861 isl_basic_map_free(bmap);
1862 return NULL;
1865 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1866 isl_int *ineq)
1868 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1871 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1873 isl_size total;
1875 total = isl_basic_map_dim(bmap, isl_dim_all);
1876 if (total < 0)
1877 return -1;
1878 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1879 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1880 bmap->extra - bmap->n_div);
1881 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1882 return bmap->n_div++;
1885 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1887 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1890 #undef TYPE
1891 #define TYPE isl_basic_map
1892 #include "check_type_range_templ.c"
1894 /* Check that there are "n" dimensions of type "type" starting at "first"
1895 * in "bset".
1897 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1898 enum isl_dim_type type, unsigned first, unsigned n)
1900 return isl_basic_map_check_range(bset_to_bmap(bset),
1901 type, first, n);
1904 /* Insert an extra integer division, prescribed by "div", to "bmap"
1905 * at (integer division) position "pos".
1907 * The integer division is first added at the end and then moved
1908 * into the right position.
1910 __isl_give isl_basic_map *isl_basic_map_insert_div(
1911 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1913 int i, k;
1914 isl_size total;
1916 bmap = isl_basic_map_cow(bmap);
1917 total = isl_basic_map_dim(bmap, isl_dim_all);
1918 if (total < 0 || !div)
1919 return isl_basic_map_free(bmap);
1921 if (div->size != 1 + 1 + total)
1922 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1923 "unexpected size", return isl_basic_map_free(bmap));
1924 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1925 return isl_basic_map_free(bmap);
1927 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1928 k = isl_basic_map_alloc_div(bmap);
1929 if (k < 0)
1930 return isl_basic_map_free(bmap);
1931 isl_seq_cpy(bmap->div[k], div->el, div->size);
1932 isl_int_set_si(bmap->div[k][div->size], 0);
1934 for (i = k; i > pos; --i)
1935 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1937 return bmap;
1940 /* Insert an extra integer division, prescribed by "div", to "bset"
1941 * at (integer division) position "pos".
1943 __isl_give isl_basic_set *isl_basic_set_insert_div(
1944 __isl_take isl_basic_set *bset, int pos, __isl_keep isl_vec *div)
1946 isl_basic_map *bmap = bset_to_bmap(bset);
1947 bmap = isl_basic_map_insert_div(bmap, pos, div);
1948 return bset_from_bmap(bmap);
1951 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1953 if (!bmap)
1954 return isl_stat_error;
1955 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1956 bmap->n_div -= n;
1957 return isl_stat_ok;
1960 static __isl_give isl_basic_map *add_constraints(
1961 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1962 unsigned i_pos, unsigned o_pos)
1964 isl_size total, n_param, n_in, n_out, n_div;
1965 unsigned o_in, o_out;
1966 isl_ctx *ctx;
1967 isl_space *space;
1968 struct isl_dim_map *dim_map;
1970 space = isl_basic_map_peek_space(bmap2);
1971 if (!bmap1 || !space)
1972 goto error;
1974 total = isl_basic_map_dim(bmap1, isl_dim_all);
1975 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1976 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1977 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1978 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1979 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1980 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1981 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1982 goto error;
1983 ctx = isl_basic_map_get_ctx(bmap1);
1984 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1985 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1986 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1987 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1988 isl_dim_map_div(dim_map, bmap2, total);
1990 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1991 error:
1992 isl_basic_map_free(bmap1);
1993 isl_basic_map_free(bmap2);
1994 return NULL;
1997 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1998 unsigned extra, unsigned n_eq, unsigned n_ineq)
2000 isl_space *space;
2001 struct isl_basic_map *ext;
2002 unsigned flags;
2003 int dims_ok;
2005 if (!base)
2006 goto error;
2008 dims_ok = base->extra >= base->n_div + extra;
2010 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
2011 room_for_ineq(base, n_ineq))
2012 return base;
2014 extra += base->extra;
2015 n_eq += base->n_eq;
2016 n_ineq += base->n_ineq;
2018 space = isl_basic_map_get_space(base);
2019 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2020 if (!ext)
2021 goto error;
2023 if (dims_ok)
2024 ext->sample = isl_vec_copy(base->sample);
2025 flags = base->flags;
2026 ext = add_constraints(ext, base, 0, 0);
2027 if (ext) {
2028 ext->flags = flags;
2029 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2032 return ext;
2034 error:
2035 isl_basic_map_free(base);
2036 return NULL;
2039 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2040 unsigned extra, unsigned n_eq, unsigned n_ineq)
2042 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2043 extra, n_eq, n_ineq));
2046 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2047 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2049 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2052 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2053 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2055 isl_basic_map *bmap = bset_to_bmap(base);
2056 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2057 return bset_from_bmap(bmap);
2060 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2062 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2065 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2067 if (!bmap)
2068 return NULL;
2070 if (bmap->ref > 1) {
2071 bmap->ref--;
2072 bmap = isl_basic_map_dup(bmap);
2074 if (bmap)
2075 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2076 return bmap;
2079 /* Clear all cached information in "map", either because it is about
2080 * to be modified or because it is being freed.
2081 * Always return the same pointer that is passed in.
2082 * This is needed for the use in isl_map_free.
2084 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2086 isl_basic_map_free(map->cached_simple_hull[0]);
2087 isl_basic_map_free(map->cached_simple_hull[1]);
2088 map->cached_simple_hull[0] = NULL;
2089 map->cached_simple_hull[1] = NULL;
2090 return map;
2093 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2095 return isl_map_cow(set);
2098 /* Return an isl_map that is equal to "map" and that has only
2099 * a single reference.
2101 * If the original input already has only one reference, then
2102 * simply return it, but clear all cached information, since
2103 * it may be rendered invalid by the operations that will be
2104 * performed on the result.
2106 * Otherwise, create a duplicate (without any cached information).
2108 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2110 if (!map)
2111 return NULL;
2113 if (map->ref == 1)
2114 return clear_caches(map);
2115 map->ref--;
2116 return isl_map_dup(map);
2119 static void swap_vars(struct isl_blk blk, isl_int *a,
2120 unsigned a_len, unsigned b_len)
2122 isl_seq_cpy(blk.data, a+a_len, b_len);
2123 isl_seq_cpy(blk.data+b_len, a, a_len);
2124 isl_seq_cpy(a, blk.data, b_len+a_len);
2127 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2128 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2130 int i;
2131 struct isl_blk blk;
2133 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2134 goto error;
2136 if (n1 == 0 || n2 == 0)
2137 return bmap;
2139 bmap = isl_basic_map_cow(bmap);
2140 if (!bmap)
2141 return NULL;
2143 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2144 if (isl_blk_is_error(blk))
2145 goto error;
2147 for (i = 0; i < bmap->n_eq; ++i)
2148 swap_vars(blk,
2149 bmap->eq[i] + pos, n1, n2);
2151 for (i = 0; i < bmap->n_ineq; ++i)
2152 swap_vars(blk,
2153 bmap->ineq[i] + pos, n1, n2);
2155 for (i = 0; i < bmap->n_div; ++i)
2156 swap_vars(blk,
2157 bmap->div[i]+1 + pos, n1, n2);
2159 isl_blk_free(bmap->ctx, blk);
2161 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2162 bmap = isl_basic_map_gauss(bmap, NULL);
2163 return isl_basic_map_finalize(bmap);
2164 error:
2165 isl_basic_map_free(bmap);
2166 return NULL;
2169 /* The given basic map has turned out to be empty.
2170 * Explicitly mark it as such and change the representation
2171 * to a canonical representation of the empty basic map.
2172 * Since the basic map has conflicting constraints,
2173 * it must have at least one constraint, except perhaps
2174 * if it was already explicitly marked as being empty.
2175 * Do nothing in the latter case, i.e., if it has been marked empty and
2176 * has no constraints.
2178 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2179 __isl_take isl_basic_map *bmap)
2181 int i = 0;
2182 isl_bool empty;
2183 isl_size n;
2184 isl_size total;
2186 n = isl_basic_map_n_constraint(bmap);
2187 empty = isl_basic_map_plain_is_empty(bmap);
2188 if (n < 0 || empty < 0)
2189 return isl_basic_map_free(bmap);
2190 if (n == 0 && empty)
2191 return bmap;
2192 total = isl_basic_map_dim(bmap, isl_dim_all);
2193 if (total < 0)
2194 return isl_basic_map_free(bmap);
2195 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2196 return isl_basic_map_free(bmap);
2197 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2198 if (!bmap)
2199 return NULL;
2200 if (bmap->n_eq > 0) {
2201 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2202 if (!bmap)
2203 return NULL;
2204 } else {
2205 i = isl_basic_map_alloc_equality(bmap);
2206 if (i < 0)
2207 goto error;
2209 isl_int_set_si(bmap->eq[i][0], 1);
2210 isl_seq_clr(bmap->eq[i]+1, total);
2211 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2212 isl_vec_free(bmap->sample);
2213 bmap->sample = NULL;
2214 return isl_basic_map_finalize(bmap);
2215 error:
2216 isl_basic_map_free(bmap);
2217 return NULL;
2220 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2221 __isl_take isl_basic_set *bset)
2223 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2226 __isl_give isl_basic_map *isl_basic_map_set_rational(
2227 __isl_take isl_basic_map *bmap)
2229 if (!bmap)
2230 return NULL;
2232 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2233 return bmap;
2235 bmap = isl_basic_map_cow(bmap);
2236 if (!bmap)
2237 return NULL;
2239 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2241 return isl_basic_map_finalize(bmap);
2244 __isl_give isl_basic_set *isl_basic_set_set_rational(
2245 __isl_take isl_basic_set *bset)
2247 return isl_basic_map_set_rational(bset);
2250 __isl_give isl_basic_set *isl_basic_set_set_integral(
2251 __isl_take isl_basic_set *bset)
2253 if (!bset)
2254 return NULL;
2256 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2257 return bset;
2259 bset = isl_basic_set_cow(bset);
2260 if (!bset)
2261 return NULL;
2263 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2265 return isl_basic_set_finalize(bset);
2268 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2270 int i;
2272 map = isl_map_cow(map);
2273 if (!map)
2274 return NULL;
2275 for (i = 0; i < map->n; ++i) {
2276 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2277 if (!map->p[i])
2278 goto error;
2280 return map;
2281 error:
2282 isl_map_free(map);
2283 return NULL;
2286 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2288 return isl_map_set_rational(set);
2291 /* Given a constraint "c" that expresses a bound
2292 * on the variable at position "pos" in terms of the first "len" variables
2293 * (other than the variable itself if pos < len), extract this bound
2294 * as a function of those first "len" variables.
2296 * That is, the constraint is of one of the following forms
2298 * -e(...) + m x >= 0
2299 * e(...) - m x >= 0
2300 * -e(...) + m x = 0
2301 * e(...) - m x = 0
2303 * Return (e(...)) / m, with the denominator m in the first position.
2305 static __isl_give isl_vec *extract_bound_from_constraint(isl_ctx *ctx,
2306 isl_int *c, int len, int pos)
2308 isl_vec *v;
2310 v = isl_vec_alloc(ctx, 1 + 1 + len);
2311 if (!v)
2312 return NULL;
2313 if (isl_int_is_pos(c[1 + pos])) {
2314 isl_int_set(v->el[0], c[1 + pos]);
2315 isl_seq_neg(v->el + 1, c, 1 + len);
2316 } else {
2317 isl_int_neg(v->el[0], c[1 + pos]);
2318 isl_seq_cpy(v->el + 1, c, 1 + len);
2320 if (pos < len)
2321 isl_int_set_si(v->el[1 + 1 + pos], 0);
2323 return v;
2326 /* Return the position of the last non-zero coefficient of
2327 * inequality constraint "ineq" of "bmap" or length "len",
2328 * given that the coefficient at position "first" is non-zero,
2329 * or that it is known that there is at least one coefficient
2330 * after "first" that is non-zero.
2332 static int extend_last_non_zero(__isl_keep isl_basic_map *bmap, int ineq,
2333 int first, unsigned len)
2335 int last;
2337 last = isl_seq_last_non_zero(bmap->ineq[ineq] + 1 + first + 1,
2338 len - (first + 1));
2339 if (last < 0)
2340 return first;
2341 else
2342 return first + 1 + last;
2345 /* Do the inequality constraints "i" and "j" of "bmap"
2346 * form a pair of opposite constraints, in the (first) "len" coefficients?
2348 static int is_constraint_pair(__isl_keep isl_basic_map *bmap, int i, int j,
2349 unsigned len)
2351 return isl_seq_is_neg(bmap->ineq[i] + 1, bmap->ineq[j] + 1, len);
2354 /* Given that inequality constraints "i" and "j" of "bmap"
2355 * form a pair of opposite constraints
2357 * f(x) + c1 >= 0
2358 * -f(x) + c2 >= 0
2360 * or
2362 * -c1 <= f(x) <= c2
2364 * do they allow for at most "bound" values in that direction?
2365 * That is, is the sum of their constant terms smaller than "bound"?
2367 * "tmp" is a temporary location that can be used to store the sum.
2369 static int constraint_pair_has_bound(__isl_keep isl_basic_map *bmap,
2370 int i, int j, isl_int bound, isl_int *tmp)
2372 isl_int_add(*tmp, bmap->ineq[i][0], bmap->ineq[j][0]);
2373 return isl_int_abs_lt(*tmp, bound);
2376 /* Return the position of an inequality constraint in "bmap"
2377 * that together with inequality constraint "ineq" forms
2378 * a pair of opposite constraints that allow at most "bound" values
2379 * in their shared direction and that appears before "ineq".
2380 * Return a position beyond the number of inequality constraints
2381 * if no such constraint can be found.
2383 * The constraints of "bmap" are assumed to have been sorted.
2384 * This means that as soon as a constraint is found where the value
2385 * of the last coefficient (in absolute value) is different from that of "ineq",
2386 * no opposite constraint can be found.
2387 * It also means that only the coefficients up to this last coefficient
2388 * need to be compared.
2390 * "pos" is the position of a coefficient that is known to be non-zero.
2391 * If no such position is known a priori, then the value 0 can be passed in.
2392 * "len" is the number of (relevant) coefficients in the constraints.
2393 * "tmp" is a temporary location that can be used to store the sum.
2395 static isl_size find_earlier_constraint_in_pair(__isl_keep isl_basic_map *bmap,
2396 int ineq, int pos, int len, isl_int bound, isl_int *tmp)
2398 int j;
2399 int last;
2400 isl_size n_ineq;
2402 n_ineq = isl_basic_map_n_inequality(bmap);
2403 if (n_ineq < 0)
2404 return isl_size_error;
2406 last = extend_last_non_zero(bmap, ineq, pos, len);
2408 for (j = ineq - 1; j >= 0; --j) {
2409 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + last],
2410 bmap->ineq[j][1 + last]))
2411 return n_ineq;
2412 if (!is_constraint_pair(bmap, ineq, j, last + 1))
2413 continue;
2414 if (constraint_pair_has_bound(bmap, ineq, j, bound, tmp))
2415 return j;
2416 return n_ineq;
2419 return n_ineq;
2422 /* Return the position of an inequality constraint in "bmap"
2423 * that together with inequality constraint "ineq" forms
2424 * a pair of opposite constraints that allow at most "bound" values
2425 * in their shared direction and that appears after "ineq".
2426 * Return a position beyond the number of inequality constraints
2427 * if no such constraint can be found.
2429 * The constraints of "bmap" are assumed to have been sorted.
2430 * This means that as soon as a constraint is found where the value
2431 * of the last coefficient (in absolute value) is different from that of "ineq",
2432 * no opposite constraint can be found.
2433 * It also means that only the coefficients up to this last coefficient
2434 * need to be compared.
2436 * "pos" is the position of a coefficient that is known to be non-zero.
2437 * If no such position is known a priori, then the value 0 can be passed in.
2438 * "len" is the number of (relevant) coefficients in the constraints.
2439 * "tmp" is a temporary location that can be used to store the sum.
2441 static isl_size find_later_constraint_in_pair(__isl_keep isl_basic_map *bmap,
2442 int ineq, int pos, int len, isl_int bound, isl_int *tmp)
2444 int j;
2445 int last;
2446 isl_size n_ineq;
2448 n_ineq = isl_basic_map_n_inequality(bmap);
2449 if (n_ineq < 0)
2450 return isl_size_error;
2452 last = extend_last_non_zero(bmap, ineq, pos, len);
2454 for (j = ineq + 1; j < n_ineq; ++j) {
2455 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + last],
2456 bmap->ineq[j][1 + last]))
2457 return n_ineq;
2458 if (isl_seq_last_non_zero(bmap->ineq[j] + 1 + last + 1,
2459 len - (last + 1)) != -1)
2460 return n_ineq;
2461 if (!is_constraint_pair(bmap, ineq, j, last + 1))
2462 continue;
2463 if (constraint_pair_has_bound(bmap, ineq, j, bound, tmp))
2464 return j;
2465 return n_ineq;
2468 return n_ineq;
2471 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2472 * of "bmap").
2474 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2476 isl_int *t = bmap->div[a];
2477 bmap->div[a] = bmap->div[b];
2478 bmap->div[b] = t;
2481 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2482 * div definitions accordingly.
2484 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2485 int a, int b)
2487 int i;
2488 isl_size off;
2490 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2491 if (off < 0)
2492 return isl_basic_map_free(bmap);
2494 swap_div(bmap, a, b);
2496 for (i = 0; i < bmap->n_eq; ++i)
2497 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2499 for (i = 0; i < bmap->n_ineq; ++i)
2500 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2502 for (i = 0; i < bmap->n_div; ++i)
2503 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2504 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2506 return bmap;
2509 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2511 isl_seq_cpy(c, c + n, rem);
2512 isl_seq_clr(c + rem, n);
2515 /* Drop n dimensions starting at first.
2517 * In principle, this frees up some extra variables as the number
2518 * of columns remains constant, but we would have to extend
2519 * the div array too as the number of rows in this array is assumed
2520 * to be equal to extra.
2522 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2523 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2525 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2528 /* Move "n" divs starting at "first" to the end of the list of divs.
2530 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2531 unsigned first, unsigned n)
2533 isl_int **div;
2534 int i;
2536 if (first + n == bmap->n_div)
2537 return bmap;
2539 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2540 if (!div)
2541 goto error;
2542 for (i = 0; i < n; ++i)
2543 div[i] = bmap->div[first + i];
2544 for (i = 0; i < bmap->n_div - first - n; ++i)
2545 bmap->div[first + i] = bmap->div[first + n + i];
2546 for (i = 0; i < n; ++i)
2547 bmap->div[bmap->n_div - n + i] = div[i];
2548 free(div);
2549 return bmap;
2550 error:
2551 isl_basic_map_free(bmap);
2552 return NULL;
2555 #undef TYPE
2556 #define TYPE isl_map
2557 static
2558 #include "check_type_range_templ.c"
2560 /* Check that there are "n" dimensions of type "type" starting at "first"
2561 * in "set".
2563 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2564 enum isl_dim_type type, unsigned first, unsigned n)
2566 return isl_map_check_range(set_to_map(set), type, first, n);
2569 /* Drop "n" dimensions of type "type" starting at "first".
2570 * Perform the core computation, without cowing or
2571 * simplifying and finalizing the result.
2573 * In principle, this frees up some extra variables as the number
2574 * of columns remains constant, but we would have to extend
2575 * the div array too as the number of rows in this array is assumed
2576 * to be equal to extra.
2578 __isl_give isl_basic_map *isl_basic_map_drop_core(
2579 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2580 unsigned first, unsigned n)
2582 int i;
2583 unsigned offset;
2584 unsigned left;
2585 isl_size total;
2587 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2588 return isl_basic_map_free(bmap);
2590 total = isl_basic_map_dim(bmap, isl_dim_all);
2591 if (total < 0)
2592 return isl_basic_map_free(bmap);
2594 offset = isl_basic_map_offset(bmap, type) + first;
2595 left = total - (offset - 1) - n;
2596 for (i = 0; i < bmap->n_eq; ++i)
2597 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2599 for (i = 0; i < bmap->n_ineq; ++i)
2600 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2602 for (i = 0; i < bmap->n_div; ++i)
2603 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2605 if (type == isl_dim_div) {
2606 bmap = move_divs_last(bmap, first, n);
2607 if (!bmap)
2608 return NULL;
2609 if (isl_basic_map_free_div(bmap, n) < 0)
2610 return isl_basic_map_free(bmap);
2611 } else
2612 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2613 if (!bmap->dim)
2614 return isl_basic_map_free(bmap);
2616 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2617 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2618 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2619 return bmap;
2622 /* Drop "n" dimensions of type "type" starting at "first".
2624 * In principle, this frees up some extra variables as the number
2625 * of columns remains constant, but we would have to extend
2626 * the div array too as the number of rows in this array is assumed
2627 * to be equal to extra.
2629 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2630 enum isl_dim_type type, unsigned first, unsigned n)
2632 if (!bmap)
2633 return NULL;
2634 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2635 return bmap;
2637 bmap = isl_basic_map_cow(bmap);
2638 if (!bmap)
2639 return NULL;
2641 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2643 bmap = isl_basic_map_simplify(bmap);
2644 return isl_basic_map_finalize(bmap);
2647 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2648 enum isl_dim_type type, unsigned first, unsigned n)
2650 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2651 type, first, n));
2654 /* No longer consider "map" to be normalized.
2656 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2658 if (!map)
2659 return NULL;
2660 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2661 return map;
2664 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2665 enum isl_dim_type type, unsigned first, unsigned n)
2667 int i;
2668 isl_space *space;
2670 if (isl_map_check_range(map, type, first, n) < 0)
2671 return isl_map_free(map);
2673 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2674 return map;
2675 map = isl_map_cow(map);
2676 if (!map)
2677 goto error;
2679 for (i = 0; i < map->n; ++i) {
2680 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2681 if (!map->p[i])
2682 goto error;
2684 map = isl_map_unmark_normalized(map);
2686 space = isl_map_take_space(map);
2687 space = isl_space_drop_dims(space, type, first, n);
2688 map = isl_map_restore_space(map, space);
2690 return map;
2691 error:
2692 isl_map_free(map);
2693 return NULL;
2696 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2697 enum isl_dim_type type, unsigned first, unsigned n)
2699 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2702 /* Drop the integer division at position "div", which is assumed
2703 * not to appear in any of the constraints or
2704 * in any of the other integer divisions.
2706 * Since the integer division is redundant, there is no need to cow.
2708 __isl_give isl_basic_map *isl_basic_map_drop_div(
2709 __isl_take isl_basic_map *bmap, unsigned div)
2711 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2714 /* Eliminate the specified n dimensions starting at first from the
2715 * constraints, without removing the dimensions from the space.
2716 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2718 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2719 enum isl_dim_type type, unsigned first, unsigned n)
2721 int i;
2723 if (n == 0)
2724 return map;
2726 if (isl_map_check_range(map, type, first, n) < 0)
2727 return isl_map_free(map);
2729 map = isl_map_cow(map);
2730 if (!map)
2731 return NULL;
2733 for (i = 0; i < map->n; ++i) {
2734 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2735 if (!map->p[i])
2736 goto error;
2738 return map;
2739 error:
2740 isl_map_free(map);
2741 return NULL;
2744 /* Eliminate the specified n dimensions starting at first from the
2745 * constraints, without removing the dimensions from the space.
2746 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2748 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2749 enum isl_dim_type type, unsigned first, unsigned n)
2751 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2754 /* Eliminate the specified n dimensions starting at first from the
2755 * constraints, without removing the dimensions from the space.
2756 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2758 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2759 unsigned first, unsigned n)
2761 return isl_set_eliminate(set, isl_dim_set, first, n);
2764 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2765 __isl_take isl_basic_map *bmap)
2767 isl_size v_div;
2769 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2770 if (v_div < 0)
2771 return isl_basic_map_free(bmap);
2772 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2773 if (!bmap)
2774 return NULL;
2775 bmap->n_div = 0;
2776 return isl_basic_map_finalize(bmap);
2779 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2780 __isl_take isl_basic_set *bset)
2782 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2785 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2787 int i;
2789 if (!map)
2790 return NULL;
2791 if (map->n == 0)
2792 return map;
2794 map = isl_map_cow(map);
2795 if (!map)
2796 return NULL;
2798 for (i = 0; i < map->n; ++i) {
2799 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2800 if (!map->p[i])
2801 goto error;
2803 return map;
2804 error:
2805 isl_map_free(map);
2806 return NULL;
2809 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2811 return isl_map_remove_divs(set);
2814 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2815 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2816 unsigned first, unsigned n)
2818 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2819 return isl_basic_map_free(bmap);
2820 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2821 return bmap;
2822 bmap = isl_basic_map_eliminate_vars(bmap,
2823 isl_basic_map_offset(bmap, type) - 1 + first, n);
2824 if (!bmap)
2825 return bmap;
2826 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2827 return bmap;
2828 bmap = isl_basic_map_drop(bmap, type, first, n);
2829 return bmap;
2832 /* Does the local variable "div" of "bmap" have a known expression
2833 * that involves the "n" variables starting at "first"?
2835 isl_bool isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map *bmap,
2836 int div, unsigned first, unsigned n)
2838 isl_bool unknown;
2840 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
2841 if (unknown < 0 || unknown)
2842 return isl_bool_not(unknown);
2843 if (isl_seq_first_non_zero(bmap->div[div] + 1 + 1 + first, n) >= 0)
2844 return isl_bool_true;
2846 return isl_bool_false;
2849 /* Return true if the definition of the given div (recursively) involves
2850 * any of the given variables.
2852 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2853 unsigned first, unsigned n)
2855 int i;
2856 isl_bool involves;
2857 isl_size n_div, v_div;
2859 involves = isl_basic_map_div_expr_involves_vars(bmap, div, first, n);
2860 if (involves < 0 || involves)
2861 return involves;
2863 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2864 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2865 if (n_div < 0 || v_div < 0)
2866 return isl_bool_error;
2867 for (i = n_div - 1; i >= 0; --i) {
2868 isl_bool involves;
2870 if (isl_int_is_zero(bmap->div[div][1 + 1 + v_div + i]))
2871 continue;
2872 involves = div_involves_vars(bmap, i, first, n);
2873 if (involves < 0 || involves)
2874 return involves;
2877 return isl_bool_false;
2880 /* Does the definition of any integer division involve
2881 * any of the given variables?
2883 isl_bool isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map *bmap,
2884 unsigned first, unsigned n)
2886 int i;
2887 isl_size n_div;
2889 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2890 if (n_div < 0)
2891 return isl_bool_error;
2893 for (i = 0; i < n_div; ++i) {
2894 isl_bool has;
2896 has = isl_basic_map_div_expr_involves_vars(bmap, i, first, n);
2897 if (has < 0 || has)
2898 return has;
2901 return isl_bool_false;
2904 /* Try and add a lower and/or upper bound on "div" to "bmap"
2905 * based on inequality "i".
2906 * "total" is the total number of variables (excluding the divs).
2907 * "v" is a temporary object that can be used during the calculations.
2908 * If "lb" is set, then a lower bound should be constructed.
2909 * If "ub" is set, then an upper bound should be constructed.
2911 * The calling function has already checked that the inequality does not
2912 * reference "div", but we still need to check that the inequality is
2913 * of the right form. We'll consider the case where we want to construct
2914 * a lower bound. The construction of upper bounds is similar.
2916 * Let "div" be of the form
2918 * q = floor((a + f(x))/d)
2920 * We essentially check if constraint "i" is of the form
2922 * b + f(x) >= 0
2924 * so that we can use it to derive a lower bound on "div".
2925 * However, we allow a slightly more general form
2927 * b + g(x) >= 0
2929 * with the condition that the coefficients of g(x) - f(x) are all
2930 * divisible by d.
2931 * Rewriting this constraint as
2933 * 0 >= -b - g(x)
2935 * adding a + f(x) to both sides and dividing by d, we obtain
2937 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2939 * Taking the floor on both sides, we obtain
2941 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2943 * or
2945 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2947 * In the case of an upper bound, we construct the constraint
2949 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2952 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2953 __isl_take isl_basic_map *bmap, int div, int i,
2954 unsigned total, isl_int v, int lb, int ub)
2956 int j;
2958 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2959 if (lb) {
2960 isl_int_sub(v, bmap->ineq[i][1 + j],
2961 bmap->div[div][1 + 1 + j]);
2962 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2964 if (ub) {
2965 isl_int_add(v, bmap->ineq[i][1 + j],
2966 bmap->div[div][1 + 1 + j]);
2967 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2970 if (!lb && !ub)
2971 return bmap;
2973 bmap = isl_basic_map_cow(bmap);
2974 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2975 if (lb) {
2976 int k = isl_basic_map_alloc_inequality(bmap);
2977 if (k < 0)
2978 goto error;
2979 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2980 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2981 bmap->div[div][1 + j]);
2982 isl_int_cdiv_q(bmap->ineq[k][j],
2983 bmap->ineq[k][j], bmap->div[div][0]);
2985 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2987 if (ub) {
2988 int k = isl_basic_map_alloc_inequality(bmap);
2989 if (k < 0)
2990 goto error;
2991 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2992 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2993 bmap->div[div][1 + j]);
2994 isl_int_fdiv_q(bmap->ineq[k][j],
2995 bmap->ineq[k][j], bmap->div[div][0]);
2997 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
3000 return bmap;
3001 error:
3002 isl_basic_map_free(bmap);
3003 return NULL;
3006 /* This function is called right before "div" is eliminated from "bmap"
3007 * using Fourier-Motzkin.
3008 * Look through the constraints of "bmap" for constraints on the argument
3009 * of the integer division and use them to construct constraints on the
3010 * integer division itself. These constraints can then be combined
3011 * during the Fourier-Motzkin elimination.
3012 * Note that it is only useful to introduce lower bounds on "div"
3013 * if "bmap" already contains upper bounds on "div" as the newly
3014 * introduce lower bounds can then be combined with the pre-existing
3015 * upper bounds. Similarly for upper bounds.
3016 * We therefore first check if "bmap" contains any lower and/or upper bounds
3017 * on "div".
3019 * It is interesting to note that the introduction of these constraints
3020 * can indeed lead to more accurate results, even when compared to
3021 * deriving constraints on the argument of "div" from constraints on "div".
3022 * Consider, for example, the set
3024 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
3026 * The second constraint can be rewritten as
3028 * 2 * [(-i-2j+3)/4] + k >= 0
3030 * from which we can derive
3032 * -i - 2j + 3 >= -2k
3034 * or
3036 * i + 2j <= 3 + 2k
3038 * Combined with the first constraint, we obtain
3040 * -3 <= 3 + 2k or k >= -3
3042 * If, on the other hand we derive a constraint on [(i+2j)/4] from
3043 * the first constraint, we obtain
3045 * [(i + 2j)/4] >= [-3/4] = -1
3047 * Combining this constraint with the second constraint, we obtain
3049 * k >= -2
3051 static __isl_give isl_basic_map *insert_bounds_on_div(
3052 __isl_take isl_basic_map *bmap, int div)
3054 int i;
3055 int check_lb, check_ub;
3056 isl_int v;
3057 isl_size v_div;
3059 if (!bmap)
3060 return NULL;
3062 if (isl_int_is_zero(bmap->div[div][0]))
3063 return bmap;
3065 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3066 if (v_div < 0)
3067 return isl_basic_map_free(bmap);
3069 check_lb = 0;
3070 check_ub = 0;
3071 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
3072 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
3073 if (s > 0)
3074 check_ub = 1;
3075 if (s < 0)
3076 check_lb = 1;
3079 if (!check_lb && !check_ub)
3080 return bmap;
3082 isl_int_init(v);
3084 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
3085 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
3086 continue;
3088 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
3089 check_lb, check_ub);
3092 isl_int_clear(v);
3094 return bmap;
3097 /* Remove all divs (recursively) involving any of the given variables
3098 * in their definitions.
3100 static __isl_give isl_basic_map *remove_divs_involving_vars(
3101 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3103 int i;
3105 for (i = bmap->n_div - 1; i >= 0; --i) {
3106 isl_bool involves;
3108 involves = div_involves_vars(bmap, i, first, n);
3109 if (involves < 0)
3110 return isl_basic_map_free(bmap);
3111 if (!involves)
3112 continue;
3113 bmap = insert_bounds_on_div(bmap, i);
3114 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3115 if (!bmap)
3116 return NULL;
3117 i = bmap->n_div;
3120 return bmap;
3123 /* Data structure for communicating data between detect_mod and
3124 * substitute_div_mod.
3126 * "pos" is the position of the variable that is being examined.
3128 * "lower_f" is the index of the constraint
3129 * -f(x) + m i + t m n h(alpha) >= 0
3130 * "lower_g" is the index of the constraint
3131 * -g(x) + i >= 0
3132 * "m" and "n" correspond to the values in the first constraint.
3133 * "sum" is a temporary variable that is used internally inside detect_mod.
3135 struct isl_detect_mod_data {
3136 unsigned pos;
3138 int lower_f;
3139 int lower_g;
3141 isl_int m;
3142 isl_int n;
3144 isl_int sum;
3147 /* Initialize "data".
3149 static void isl_detect_mod_data_init(struct isl_detect_mod_data *data)
3151 isl_int_init(data->m);
3152 isl_int_init(data->n);
3153 isl_int_init(data->sum);
3156 /* Free any memory allocated by "data".
3158 static void isl_detect_mod_data_clear(struct isl_detect_mod_data *data)
3160 isl_int_clear(data->sum);
3161 isl_int_clear(data->n);
3162 isl_int_clear(data->m);
3165 /* Is the variable at position data->pos
3166 * equal to a specific case of a nested modulo?
3168 * In particular, look for two pairs of constraints
3170 * -f(x) + m i + t m n h(alpha) >= 0
3171 * f(x) - m i - t m n h(alpha) + c >= 0
3173 * and
3175 * -g(x) + i >= 0
3176 * g(x) - i + d >= 0
3178 * where f(x) and g(x) are expressions in the other variables,
3179 * excluding local variables,
3180 * h(alpha) is a non-zero expression in the local variable,
3181 * t is +1 or -1,
3182 * c < m, and
3183 * d < n.
3185 * If these pairs of constraints are found,
3186 * then store the constraint index of the first f-constraint in data->lower_f,
3187 * the index of the first g-constraint in data->lower_g and
3188 * the values m and n in data->m and data->n.
3190 * The constraints are assumed to have been sorted,
3191 * which means that the constraints in a pair are close to each other.
3192 * The sorting also means that the f-constraints appear
3193 * after the g-constraints.
3195 static isl_bool detect_mod(__isl_keep isl_basic_map *bmap,
3196 struct isl_detect_mod_data *data)
3198 int i;
3199 isl_size j;
3200 isl_size n_ineq;
3201 isl_size v_div, n_div, total;
3203 n_ineq = isl_basic_map_n_inequality(bmap);
3204 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3205 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3206 total = isl_basic_map_dim(bmap, isl_dim_all);
3207 if (n_ineq < 0 || v_div < 0 || n_div < 0 || total < 0)
3208 return isl_bool_error;
3209 if (n_ineq < 4)
3210 return isl_bool_false;
3212 for (i = n_ineq - 1; i >= 1; --i) {
3213 isl_seq_gcd(bmap->ineq[i] + 1 + v_div, n_div, &data->n);
3214 if (isl_int_is_zero(data->n))
3215 return isl_bool_false;
3216 isl_int_abs(data->m, bmap->ineq[i][1 + data->pos]);
3217 j = find_earlier_constraint_in_pair(bmap, i, data->pos, total,
3218 data->m, &data->sum);
3219 if (j < 0)
3220 return isl_bool_error;
3221 if (j >= n_ineq)
3222 continue;
3223 if (!isl_int_is_divisible_by(data->n, data->m))
3224 continue;
3225 if (isl_int_is_pos(bmap->ineq[i][1 + data->pos]))
3226 data->lower_f = i;
3227 else
3228 data->lower_f = j;
3229 isl_int_divexact(data->n, data->n, data->m);
3230 isl_int_abs(data->n, data->n);
3231 break;
3233 if (i < 1)
3234 return isl_bool_false;
3235 for (i = j - 1; i >= 1; --i) {
3236 int f;
3238 f = isl_seq_first_non_zero(bmap->ineq[i] + 1 + v_div, n_div);
3239 if (f != -1)
3240 continue;
3241 if (!isl_int_is_one(bmap->ineq[i][1 + data->pos]) &&
3242 !isl_int_is_negone(bmap->ineq[i][1 + data->pos]))
3243 continue;
3244 j = find_earlier_constraint_in_pair(bmap, i, data->pos, v_div,
3245 data->m, &data->sum);
3246 if (j < 0)
3247 return isl_bool_error;
3248 if (j >= n_ineq)
3249 continue;
3250 if (isl_int_is_pos(bmap->ineq[i][1 + data->pos]))
3251 data->lower_g = i;
3252 else
3253 data->lower_g = j;
3254 break;
3256 if (i < 1)
3257 return isl_bool_false;
3258 return isl_bool_true;
3261 /* Given that inequality "ineq" of "bset" expresses a lower bound
3262 * on the variable at position "pos" in terms of the other variables,
3263 * extract this lower bound as a function of those other variables,
3264 * excluding any local variables.
3266 static __isl_give isl_aff *extract_lower_bound_aff(
3267 __isl_keep isl_basic_set *bset, int ineq, int pos)
3269 isl_size dim;
3270 isl_ctx *ctx;
3271 isl_vec *v;
3272 isl_space *space;
3273 isl_local_space *ls;
3275 space = isl_basic_set_peek_space(bset);
3276 dim = isl_space_dim(space, isl_dim_all);
3277 if (dim < 0)
3278 return NULL;
3279 ls = isl_local_space_from_space(isl_space_copy(space));
3280 ctx = isl_basic_set_get_ctx(bset);
3281 v = extract_bound_from_constraint(ctx, bset->ineq[ineq], dim, pos);
3282 return isl_aff_alloc_vec(ls, v);
3285 /* Given that there are two pairs of constraints
3287 * -f(x) + m i + t m n h(alpha) >= 0
3288 * f(x) - m i - t m n h(alpha) + c >= 0
3290 * and
3292 * -g(x) + i >= 0
3293 * g(x) - i + d >= 0
3295 * where f(x) and g(x) are expressions in the other variables,
3296 * excluding local variables,
3297 * h(alpha) is a non-zero expression in the local variable,
3298 * t is +1 or -1,
3299 * c < m, and
3300 * d < n,
3301 * derive an expression for i and plug that into "bmap".
3303 * In particular, the first pair of constraints implies
3305 * 0 <= (-f(x) + m i) % (m n) <= c
3306 * 0 <= (m floor(-f(x)/m) + (-f(x)) mod m + m i) % (m n) <= c
3307 * 0 <= (m floor(-f(x)/m) + m i) % (m n) + (-f(x)) mod m <= c
3308 * 0 <= m ((floor(-f(x)/m) + i) % n) + (-f(x)) mod m <= c
3310 * or
3312 * (floor(-f(x)/m) + i) % n = 0
3314 * because c < m, which can be rewritten to
3316 * (floor(-f(x)/m) + g(x) - g(x) + i) % n = 0
3318 * or
3320 * (-g(x) + i) % n = (-floor(-f(x)/m) - g(x)) % n
3322 * Since 0 <= -g(x) + i <= d < n,
3324 * -g(x) + i = (-floor(-f(x)/m) - g(x)) % n
3326 * or
3328 * i = (-floor(-f(x)/m) - g(x)) % n + g(x)
3330 * Note that the lower bounds on i derived from the original constraints are
3332 * i >= f(x)/m
3333 * i >= g(x)
3335 * (ignoring local variables).
3337 * The expression for i is plugged into "bmap", which results
3338 * in a basic map equivalent to "bmap" given i is equal to the expression,
3339 * so an equality constraint is added to ensure the meaning is preserved.
3341 static __isl_give isl_basic_map *substitute_div_mod(
3342 __isl_take isl_basic_map *bmap, unsigned pos,
3343 struct isl_detect_mod_data *data)
3345 isl_ctx *ctx;
3346 isl_basic_set *bset, *eq;
3347 isl_space *space;
3348 isl_aff *g, *f, *aff, *it;
3349 isl_val *v;
3350 isl_size v_in;
3351 isl_multi_aff *ma;
3353 v_in = isl_basic_map_var_offset(bmap, isl_dim_in);
3354 if (v_in < 0)
3355 return isl_basic_map_free(bmap);
3357 ctx = isl_basic_map_get_ctx(bmap);
3358 bset = isl_basic_map_wrap(bmap);
3359 space = isl_basic_set_get_space(bset);
3360 g = extract_lower_bound_aff(bset, data->lower_g, pos);
3361 f = extract_lower_bound_aff(bset, data->lower_f, pos);
3362 ma = isl_space_identity_multi_aff_on_domain(space);
3363 aff = isl_aff_neg(isl_aff_floor(isl_aff_neg(f)));
3364 aff = isl_aff_sub(aff, isl_aff_copy(g));
3365 v = isl_val_int_from_isl_int(ctx, data->n);
3366 aff = isl_aff_mod_val(aff, v);
3367 aff = isl_aff_add(aff, g);
3368 it = isl_multi_aff_get_at(ma, pos - v_in);
3369 eq = isl_aff_eq_basic_set(it, isl_aff_copy(aff));
3370 ma = isl_multi_aff_set_at(ma, pos - v_in, aff);
3371 bset = isl_basic_set_preimage_multi_aff(bset, ma);
3372 bset = isl_basic_set_intersect(bset, eq);
3373 return isl_basic_set_unwrap(bset);
3376 /* Remove all divs (recursively) involving any of the given dimensions
3377 * in their definitions.
3379 * If it is a single input or output dimension that
3380 * should not appear in any integer division expression and
3381 * if there is indeed an integer division expression
3382 * involving that dimension, then check if there are any constraints
3383 * that imply that the given dimension is equal to some expression
3384 * in the other dimensions.
3385 * If so, plug in that expression for the given dimension
3386 * so that the integer division expressions no longer
3387 * involve that dimension.
3389 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
3390 __isl_take isl_basic_map *bmap,
3391 enum isl_dim_type type, unsigned first, unsigned n)
3393 isl_size off;
3394 isl_bool used;
3395 isl_bool found;
3396 struct isl_detect_mod_data data;
3398 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3399 return isl_basic_map_free(bmap);
3400 off = isl_basic_map_var_offset(bmap, type);
3401 if (off < 0)
3402 return isl_basic_map_free(bmap);
3403 first += off;
3405 if (type == isl_dim_param || type == isl_dim_div || n != 1)
3406 return remove_divs_involving_vars(bmap, first, n);
3407 used = isl_basic_map_any_div_involves_vars(bmap, first, n);
3408 if (used < 0)
3409 return isl_basic_map_free(bmap);
3410 if (!used)
3411 return bmap;
3412 bmap = isl_basic_map_sort_constraints(bmap);
3413 isl_detect_mod_data_init(&data);
3414 data.pos = first;
3415 found = detect_mod(bmap, &data);
3416 if (!found)
3417 bmap = remove_divs_involving_vars(bmap, first, n);
3418 else
3419 bmap = substitute_div_mod(bmap, first, &data);
3420 isl_detect_mod_data_clear(&data);
3421 return bmap;
3424 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
3425 __isl_take isl_basic_set *bset,
3426 enum isl_dim_type type, unsigned first, unsigned n)
3428 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
3431 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
3432 enum isl_dim_type type, unsigned first, unsigned n)
3434 int i;
3436 if (!map)
3437 return NULL;
3438 if (map->n == 0)
3439 return map;
3441 map = isl_map_cow(map);
3442 if (!map)
3443 return NULL;
3445 for (i = 0; i < map->n; ++i) {
3446 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
3447 type, first, n);
3448 if (!map->p[i])
3449 goto error;
3451 return map;
3452 error:
3453 isl_map_free(map);
3454 return NULL;
3457 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
3458 enum isl_dim_type type, unsigned first, unsigned n)
3460 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
3461 type, first, n));
3464 /* Does the description of "bmap" depend on the specified dimensions?
3465 * We also check whether the dimensions appear in any of the div definitions.
3466 * In principle there is no need for this check. If the dimensions appear
3467 * in a div definition, they also appear in the defining constraints of that
3468 * div.
3470 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
3471 enum isl_dim_type type, unsigned first, unsigned n)
3473 int i;
3475 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3476 return isl_bool_error;
3478 first += isl_basic_map_offset(bmap, type);
3479 for (i = 0; i < bmap->n_eq; ++i)
3480 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
3481 return isl_bool_true;
3482 for (i = 0; i < bmap->n_ineq; ++i)
3483 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
3484 return isl_bool_true;
3485 for (i = 0; i < bmap->n_div; ++i) {
3486 if (isl_int_is_zero(bmap->div[i][0]))
3487 continue;
3488 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
3489 return isl_bool_true;
3492 return isl_bool_false;
3495 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
3496 enum isl_dim_type type, unsigned first, unsigned n)
3498 int i;
3500 if (isl_map_check_range(map, type, first, n) < 0)
3501 return isl_bool_error;
3503 for (i = 0; i < map->n; ++i) {
3504 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
3505 type, first, n);
3506 if (involves < 0 || involves)
3507 return involves;
3510 return isl_bool_false;
3513 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
3514 enum isl_dim_type type, unsigned first, unsigned n)
3516 return isl_basic_map_involves_dims(bset, type, first, n);
3519 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
3520 enum isl_dim_type type, unsigned first, unsigned n)
3522 return isl_map_involves_dims(set, type, first, n);
3525 /* Does "bset" involve any local variables, i.e., integer divisions?
3527 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
3529 isl_size n;
3531 n = isl_basic_set_dim(bset, isl_dim_div);
3532 if (n < 0)
3533 return isl_bool_error;
3534 return isl_bool_ok(n > 0);
3537 /* isl_set_every_basic_set callback that checks whether "bset"
3538 * is free of local variables.
3540 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3542 return isl_bool_not(isl_basic_set_involves_locals(bset));
3545 /* Does "set" involve any local variables, i.e., integer divisions?
3547 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3549 isl_bool no_locals;
3551 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3552 return isl_bool_not(no_locals);
3555 /* Drop all constraints in bmap that involve any of the dimensions
3556 * first to first+n-1.
3557 * This function only performs the actual removal of constraints.
3559 * This function should not call finalize since it is used by
3560 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3562 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3563 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3565 int i;
3567 if (n == 0)
3568 return bmap;
3570 bmap = isl_basic_map_cow(bmap);
3572 if (!bmap)
3573 return NULL;
3575 for (i = bmap->n_eq - 1; i >= 0; --i) {
3576 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3577 continue;
3578 if (isl_basic_map_drop_equality(bmap, i) < 0)
3579 return isl_basic_map_free(bmap);
3582 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3583 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3584 continue;
3585 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3586 return isl_basic_map_free(bmap);
3589 return bmap;
3592 /* Drop all constraints in bset that involve any of the dimensions
3593 * first to first+n-1.
3594 * This function only performs the actual removal of constraints.
3596 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3597 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3599 return isl_basic_map_drop_constraints_involving(bset, first, n);
3602 /* Drop all constraints in bmap that do not involve any of the dimensions
3603 * first to first + n - 1 of the given type.
3605 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3606 __isl_take isl_basic_map *bmap,
3607 enum isl_dim_type type, unsigned first, unsigned n)
3609 int i;
3611 if (n == 0) {
3612 isl_space *space = isl_basic_map_get_space(bmap);
3613 isl_basic_map_free(bmap);
3614 return isl_basic_map_universe(space);
3616 bmap = isl_basic_map_cow(bmap);
3617 if (!bmap)
3618 return NULL;
3620 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3621 return isl_basic_map_free(bmap);
3623 first += isl_basic_map_offset(bmap, type) - 1;
3625 for (i = bmap->n_eq - 1; i >= 0; --i) {
3626 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3627 continue;
3628 if (isl_basic_map_drop_equality(bmap, i) < 0)
3629 return isl_basic_map_free(bmap);
3632 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3633 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3634 continue;
3635 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3636 return isl_basic_map_free(bmap);
3639 bmap = isl_basic_map_add_known_div_constraints(bmap);
3640 return bmap;
3643 /* Drop all constraints in bset that do not involve any of the dimensions
3644 * first to first + n - 1 of the given type.
3646 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3647 __isl_take isl_basic_set *bset,
3648 enum isl_dim_type type, unsigned first, unsigned n)
3650 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3651 type, first, n);
3654 /* Drop all constraints in bmap that involve any of the dimensions
3655 * first to first + n - 1 of the given type.
3657 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3658 __isl_take isl_basic_map *bmap,
3659 enum isl_dim_type type, unsigned first, unsigned n)
3661 if (!bmap)
3662 return NULL;
3663 if (n == 0)
3664 return bmap;
3666 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3667 return isl_basic_map_free(bmap);
3669 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3670 first += isl_basic_map_offset(bmap, type) - 1;
3671 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3672 bmap = isl_basic_map_add_known_div_constraints(bmap);
3673 return bmap;
3676 /* Drop all constraints in bset that involve any of the dimensions
3677 * first to first + n - 1 of the given type.
3679 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3680 __isl_take isl_basic_set *bset,
3681 enum isl_dim_type type, unsigned first, unsigned n)
3683 return isl_basic_map_drop_constraints_involving_dims(bset,
3684 type, first, n);
3687 /* Drop constraints from "map" by applying "drop" to each basic map.
3689 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3690 enum isl_dim_type type, unsigned first, unsigned n,
3691 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3692 enum isl_dim_type type, unsigned first, unsigned n))
3694 int i;
3696 if (isl_map_check_range(map, type, first, n) < 0)
3697 return isl_map_free(map);
3699 map = isl_map_cow(map);
3700 if (!map)
3701 return NULL;
3703 for (i = 0; i < map->n; ++i) {
3704 map->p[i] = drop(map->p[i], type, first, n);
3705 if (!map->p[i])
3706 return isl_map_free(map);
3709 if (map->n > 1)
3710 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3712 return map;
3715 /* Drop all constraints in map that involve any of the dimensions
3716 * first to first + n - 1 of the given type.
3718 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3719 __isl_take isl_map *map,
3720 enum isl_dim_type type, unsigned first, unsigned n)
3722 if (n == 0)
3723 return map;
3724 return drop_constraints(map, type, first, n,
3725 &isl_basic_map_drop_constraints_involving_dims);
3728 /* Drop all constraints in "map" that do not involve any of the dimensions
3729 * first to first + n - 1 of the given type.
3731 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3732 __isl_take isl_map *map,
3733 enum isl_dim_type type, unsigned first, unsigned n)
3735 if (n == 0) {
3736 isl_space *space = isl_map_get_space(map);
3737 isl_map_free(map);
3738 return isl_map_universe(space);
3740 return drop_constraints(map, type, first, n,
3741 &isl_basic_map_drop_constraints_not_involving_dims);
3744 /* Drop all constraints in set that involve any of the dimensions
3745 * first to first + n - 1 of the given type.
3747 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3748 __isl_take isl_set *set,
3749 enum isl_dim_type type, unsigned first, unsigned n)
3751 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3754 /* Drop all constraints in "set" that do not involve any of the dimensions
3755 * first to first + n - 1 of the given type.
3757 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3758 __isl_take isl_set *set,
3759 enum isl_dim_type type, unsigned first, unsigned n)
3761 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3764 /* Does local variable "div" of "bmap" have a complete explicit representation?
3765 * Having a complete explicit representation requires not only
3766 * an explicit representation, but also that all local variables
3767 * that appear in this explicit representation in turn have
3768 * a complete explicit representation.
3770 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3772 int i;
3773 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3774 isl_bool marked;
3776 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3777 if (marked < 0 || marked)
3778 return isl_bool_not(marked);
3780 for (i = bmap->n_div - 1; i >= 0; --i) {
3781 isl_bool known;
3783 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3784 continue;
3785 known = isl_basic_map_div_is_known(bmap, i);
3786 if (known < 0 || !known)
3787 return known;
3790 return isl_bool_true;
3793 /* Remove all divs that are unknown or defined in terms of unknown divs.
3795 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3796 __isl_take isl_basic_map *bmap)
3798 int i;
3800 if (!bmap)
3801 return NULL;
3803 for (i = bmap->n_div - 1; i >= 0; --i) {
3804 if (isl_basic_map_div_is_known(bmap, i))
3805 continue;
3806 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3807 if (!bmap)
3808 return NULL;
3809 i = bmap->n_div;
3812 return bmap;
3815 /* Remove all divs that are unknown or defined in terms of unknown divs.
3817 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3818 __isl_take isl_basic_set *bset)
3820 return isl_basic_map_remove_unknown_divs(bset);
3823 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3825 int i;
3827 if (!map)
3828 return NULL;
3829 if (map->n == 0)
3830 return map;
3832 map = isl_map_cow(map);
3833 if (!map)
3834 return NULL;
3836 for (i = 0; i < map->n; ++i) {
3837 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3838 if (!map->p[i])
3839 goto error;
3841 return map;
3842 error:
3843 isl_map_free(map);
3844 return NULL;
3847 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3849 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3852 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3853 __isl_take isl_basic_set *bset,
3854 enum isl_dim_type type, unsigned first, unsigned n)
3856 isl_basic_map *bmap = bset_to_bmap(bset);
3857 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3858 return bset_from_bmap(bmap);
3861 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3862 enum isl_dim_type type, unsigned first, unsigned n)
3864 int i;
3866 if (n == 0)
3867 return map;
3869 map = isl_map_cow(map);
3870 if (isl_map_check_range(map, type, first, n) < 0)
3871 return isl_map_free(map);
3873 for (i = 0; i < map->n; ++i) {
3874 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3875 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3876 if (!map->p[i])
3877 goto error;
3879 map = isl_map_drop(map, type, first, n);
3880 return map;
3881 error:
3882 isl_map_free(map);
3883 return NULL;
3886 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3887 enum isl_dim_type type, unsigned first, unsigned n)
3889 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3890 type, first, n));
3893 /* Project out n inputs starting at first using Fourier-Motzkin */
3894 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3895 unsigned first, unsigned n)
3897 return isl_map_remove_dims(map, isl_dim_in, first, n);
3900 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3901 FILE *out, int indent)
3903 isl_printer *p;
3905 if (!bset) {
3906 fprintf(out, "null basic set\n");
3907 return;
3910 fprintf(out, "%*s", indent, "");
3911 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3912 bset->ref, bset->dim->nparam, bset->dim->n_out,
3913 bset->extra, bset->flags);
3915 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3916 p = isl_printer_set_dump(p, 1);
3917 p = isl_printer_set_indent(p, indent);
3918 p = isl_printer_start_line(p);
3919 p = isl_printer_print_basic_set(p, bset);
3920 p = isl_printer_end_line(p);
3921 isl_printer_free(p);
3924 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3925 FILE *out, int indent)
3927 isl_printer *p;
3929 if (!bmap) {
3930 fprintf(out, "null basic map\n");
3931 return;
3934 fprintf(out, "%*s", indent, "");
3935 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3936 "flags: %x, n_name: %d\n",
3937 bmap->ref,
3938 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3939 bmap->extra, bmap->flags, bmap->dim->n_id);
3941 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3942 p = isl_printer_set_dump(p, 1);
3943 p = isl_printer_set_indent(p, indent);
3944 p = isl_printer_start_line(p);
3945 p = isl_printer_print_basic_map(p, bmap);
3946 p = isl_printer_end_line(p);
3947 isl_printer_free(p);
3950 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3951 unsigned pos)
3953 isl_size total;
3955 total = isl_basic_map_dim(bmap, isl_dim_all);
3956 if (total < 0)
3957 return isl_basic_map_free(bmap);
3958 if (pos >= bmap->n_ineq)
3959 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3960 "invalid position", return isl_basic_map_free(bmap));
3961 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3962 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3963 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3964 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3965 return bmap;
3968 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3969 unsigned flags)
3971 if (isl_space_check_is_set(space) < 0)
3972 goto error;
3973 return isl_map_alloc_space(space, n, flags);
3974 error:
3975 isl_space_free(space);
3976 return NULL;
3979 /* Make sure "map" has room for at least "n" more basic maps.
3981 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3983 int i;
3984 struct isl_map *grown = NULL;
3986 if (!map)
3987 return NULL;
3988 isl_assert(map->ctx, n >= 0, goto error);
3989 if (map->n + n <= map->size)
3990 return map;
3991 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3992 if (!grown)
3993 goto error;
3994 for (i = 0; i < map->n; ++i) {
3995 grown->p[i] = isl_basic_map_copy(map->p[i]);
3996 if (!grown->p[i])
3997 goto error;
3998 grown->n++;
4000 isl_map_free(map);
4001 return grown;
4002 error:
4003 isl_map_free(grown);
4004 isl_map_free(map);
4005 return NULL;
4008 /* Make sure "set" has room for at least "n" more basic sets.
4010 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
4012 return set_from_map(isl_map_grow(set_to_map(set), n));
4015 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
4017 return isl_map_from_basic_map(bset);
4020 /* This function performs the same operation as isl_set_from_basic_set,
4021 * but is considered as a function on an isl_basic_set when exported.
4023 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
4025 return isl_set_from_basic_set(bset);
4028 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
4030 struct isl_map *map;
4032 if (!bmap)
4033 return NULL;
4035 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
4036 return isl_map_add_basic_map(map, bmap);
4039 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
4040 __isl_take isl_basic_set *bset)
4042 return set_from_map(isl_map_add_basic_map(set_to_map(set),
4043 bset_to_bmap(bset)));
4046 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
4048 return isl_map_free(set);
4051 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
4053 int i;
4055 if (!set) {
4056 fprintf(out, "null set\n");
4057 return;
4060 fprintf(out, "%*s", indent, "");
4061 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
4062 set->ref, set->n, set->dim->nparam, set->dim->n_out,
4063 set->flags);
4064 for (i = 0; i < set->n; ++i) {
4065 fprintf(out, "%*s", indent, "");
4066 fprintf(out, "basic set %d:\n", i);
4067 isl_basic_set_print_internal(set->p[i], out, indent+4);
4071 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
4073 int i;
4075 if (!map) {
4076 fprintf(out, "null map\n");
4077 return;
4080 fprintf(out, "%*s", indent, "");
4081 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
4082 "flags: %x, n_name: %d\n",
4083 map->ref, map->n, map->dim->nparam, map->dim->n_in,
4084 map->dim->n_out, map->flags, map->dim->n_id);
4085 for (i = 0; i < map->n; ++i) {
4086 fprintf(out, "%*s", indent, "");
4087 fprintf(out, "basic map %d:\n", i);
4088 isl_basic_map_print_internal(map->p[i], out, indent+4);
4092 /* Check that the space of "bset" is the same as that of the domain of "bmap".
4094 static isl_stat isl_basic_map_check_compatible_domain(
4095 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
4097 isl_bool ok;
4099 ok = isl_basic_map_compatible_domain(bmap, bset);
4100 if (ok < 0)
4101 return isl_stat_error;
4102 if (!ok)
4103 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
4104 "incompatible spaces", return isl_stat_error);
4106 return isl_stat_ok;
4109 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
4110 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4112 struct isl_basic_map *bmap_domain;
4113 isl_size dim;
4115 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
4116 goto error;
4118 dim = isl_basic_set_dim(bset, isl_dim_set);
4119 if (dim < 0)
4120 goto error;
4121 if (dim != 0 &&
4122 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4123 goto error;
4125 bmap = isl_basic_map_cow(bmap);
4126 if (!bmap)
4127 goto error;
4128 bmap = isl_basic_map_extend(bmap,
4129 bset->n_div, bset->n_eq, bset->n_ineq);
4130 bmap_domain = isl_basic_map_from_domain(bset);
4131 bmap = add_constraints(bmap, bmap_domain, 0, 0);
4133 bmap = isl_basic_map_simplify(bmap);
4134 return isl_basic_map_finalize(bmap);
4135 error:
4136 isl_basic_map_free(bmap);
4137 isl_basic_set_free(bset);
4138 return NULL;
4141 /* Check that the space of "bset" is the same as that of the range of "bmap".
4143 static isl_stat isl_basic_map_check_compatible_range(
4144 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
4146 isl_bool ok;
4148 ok = isl_basic_map_compatible_range(bmap, bset);
4149 if (ok < 0)
4150 return isl_stat_error;
4151 if (!ok)
4152 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
4153 "incompatible spaces", return isl_stat_error);
4155 return isl_stat_ok;
4158 __isl_give isl_basic_map *isl_basic_map_intersect_range(
4159 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4161 struct isl_basic_map *bmap_range;
4162 isl_size dim;
4164 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
4165 goto error;
4167 dim = isl_basic_set_dim(bset, isl_dim_set);
4168 if (dim < 0)
4169 goto error;
4170 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
4171 goto error;
4173 if (isl_basic_set_plain_is_universe(bset)) {
4174 isl_basic_set_free(bset);
4175 return bmap;
4178 bmap = isl_basic_map_cow(bmap);
4179 if (!bmap)
4180 goto error;
4181 bmap = isl_basic_map_extend(bmap,
4182 bset->n_div, bset->n_eq, bset->n_ineq);
4183 bmap_range = bset_to_bmap(bset);
4184 bmap = add_constraints(bmap, bmap_range, 0, 0);
4186 bmap = isl_basic_map_simplify(bmap);
4187 return isl_basic_map_finalize(bmap);
4188 error:
4189 isl_basic_map_free(bmap);
4190 isl_basic_set_free(bset);
4191 return NULL;
4194 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
4195 __isl_keep isl_vec *vec)
4197 int i;
4198 isl_size total;
4199 isl_int s;
4201 total = isl_basic_map_dim(bmap, isl_dim_all);
4202 if (total < 0 || !vec)
4203 return isl_bool_error;
4205 if (1 + total != vec->size)
4206 return isl_bool_false;
4208 isl_int_init(s);
4210 for (i = 0; i < bmap->n_eq; ++i) {
4211 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
4212 if (!isl_int_is_zero(s)) {
4213 isl_int_clear(s);
4214 return isl_bool_false;
4218 for (i = 0; i < bmap->n_ineq; ++i) {
4219 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
4220 if (isl_int_is_neg(s)) {
4221 isl_int_clear(s);
4222 return isl_bool_false;
4226 isl_int_clear(s);
4228 return isl_bool_true;
4231 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
4232 __isl_keep isl_vec *vec)
4234 return isl_basic_map_contains(bset_to_bmap(bset), vec);
4237 __isl_give isl_basic_map *isl_basic_map_intersect(
4238 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4240 struct isl_vec *sample = NULL;
4241 isl_space *space1, *space2;
4242 isl_size dim1, dim2, nparam1, nparam2;
4244 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4245 goto error;
4246 space1 = isl_basic_map_peek_space(bmap1);
4247 space2 = isl_basic_map_peek_space(bmap2);
4248 dim1 = isl_space_dim(space1, isl_dim_all);
4249 dim2 = isl_space_dim(space2, isl_dim_all);
4250 nparam1 = isl_space_dim(space1, isl_dim_param);
4251 nparam2 = isl_space_dim(space2, isl_dim_param);
4252 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
4253 goto error;
4254 if (dim1 == nparam1 && dim2 != nparam2)
4255 return isl_basic_map_intersect(bmap2, bmap1);
4257 if (dim2 != nparam2 &&
4258 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4259 goto error;
4261 if (isl_basic_map_plain_is_empty(bmap1)) {
4262 isl_basic_map_free(bmap2);
4263 return bmap1;
4265 if (isl_basic_map_plain_is_empty(bmap2)) {
4266 isl_basic_map_free(bmap1);
4267 return bmap2;
4270 if (bmap1->sample &&
4271 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
4272 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
4273 sample = isl_vec_copy(bmap1->sample);
4274 else if (bmap2->sample &&
4275 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
4276 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
4277 sample = isl_vec_copy(bmap2->sample);
4279 bmap1 = isl_basic_map_cow(bmap1);
4280 if (!bmap1)
4281 goto error;
4282 bmap1 = isl_basic_map_extend(bmap1,
4283 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4284 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
4286 if (!bmap1)
4287 isl_vec_free(sample);
4288 else if (sample) {
4289 isl_vec_free(bmap1->sample);
4290 bmap1->sample = sample;
4293 bmap1 = isl_basic_map_simplify(bmap1);
4294 return isl_basic_map_finalize(bmap1);
4295 error:
4296 if (sample)
4297 isl_vec_free(sample);
4298 isl_basic_map_free(bmap1);
4299 isl_basic_map_free(bmap2);
4300 return NULL;
4303 __isl_give isl_basic_set *isl_basic_set_intersect(
4304 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
4306 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
4307 bset_to_bmap(bset2)));
4310 /* Intersect the parameter domain of "bmap" with "bset".
4312 * isl_basic_map_intersect handles this as a special case.
4314 __isl_give isl_basic_map *isl_basic_map_intersect_params(
4315 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4317 return isl_basic_map_intersect(bmap, bset);
4320 __isl_give isl_basic_set *isl_basic_set_intersect_params(
4321 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
4323 isl_basic_map *bmap;
4325 bmap = bset_to_bmap(bset1);
4326 bmap = isl_basic_map_intersect_params(bmap, bset2);
4327 return bset_from_bmap(bmap);
4330 /* Does "map" consist of a single disjunct, without any local variables?
4332 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
4334 isl_size n_div;
4336 if (!map)
4337 return isl_bool_error;
4338 if (map->n != 1)
4339 return isl_bool_false;
4340 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
4341 if (n_div < 0)
4342 return isl_bool_error;
4343 if (n_div != 0)
4344 return isl_bool_false;
4345 return isl_bool_true;
4348 /* Check that "map" consists of a single disjunct, without any local variables.
4350 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
4352 isl_bool ok;
4354 ok = is_convex_no_locals(map);
4355 if (ok < 0)
4356 return isl_stat_error;
4357 if (ok)
4358 return isl_stat_ok;
4360 isl_die(isl_map_get_ctx(map), isl_error_internal,
4361 "unexpectedly not convex or involving local variables",
4362 return isl_stat_error);
4365 /* Special case of isl_map_intersect, where both map1 and map2
4366 * are convex, without any divs and such that either map1 or map2
4367 * contains a single constraint. This constraint is then simply
4368 * added to the other map.
4370 static __isl_give isl_map *map_intersect_add_constraint(
4371 __isl_take isl_map *map1, __isl_take isl_map *map2)
4373 if (check_convex_no_locals(map1) < 0 ||
4374 check_convex_no_locals(map2) < 0)
4375 goto error;
4377 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
4378 return isl_map_intersect(map2, map1);
4380 map1 = isl_map_cow(map1);
4381 if (!map1)
4382 goto error;
4383 if (isl_map_plain_is_empty(map1)) {
4384 isl_map_free(map2);
4385 return map1;
4387 if (map2->p[0]->n_eq == 1)
4388 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
4389 else
4390 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
4391 map2->p[0]->ineq[0]);
4393 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
4394 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
4395 if (!map1->p[0])
4396 goto error;
4398 if (isl_basic_map_plain_is_empty(map1->p[0])) {
4399 isl_basic_map_free(map1->p[0]);
4400 map1->n = 0;
4403 isl_map_free(map2);
4405 map1 = isl_map_unmark_normalized(map1);
4406 return map1;
4407 error:
4408 isl_map_free(map1);
4409 isl_map_free(map2);
4410 return NULL;
4413 /* map2 may be either a parameter domain or a map living in the same
4414 * space as map1.
4416 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
4417 __isl_take isl_map *map2)
4419 unsigned flags = 0;
4420 isl_bool equal;
4421 isl_map *result;
4422 int i, j;
4423 isl_size dim2, nparam2;
4425 if (!map1 || !map2)
4426 goto error;
4428 if ((isl_map_plain_is_empty(map1) ||
4429 isl_map_plain_is_universe(map2)) &&
4430 isl_space_is_equal(map1->dim, map2->dim)) {
4431 isl_map_free(map2);
4432 return map1;
4434 if ((isl_map_plain_is_empty(map2) ||
4435 isl_map_plain_is_universe(map1)) &&
4436 isl_space_is_equal(map1->dim, map2->dim)) {
4437 isl_map_free(map1);
4438 return map2;
4441 if (is_convex_no_locals(map1) == isl_bool_true &&
4442 is_convex_no_locals(map2) == isl_bool_true &&
4443 isl_space_is_equal(map1->dim, map2->dim) &&
4444 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
4445 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
4446 return map_intersect_add_constraint(map1, map2);
4448 equal = isl_map_plain_is_equal(map1, map2);
4449 if (equal < 0)
4450 goto error;
4451 if (equal) {
4452 isl_map_free(map2);
4453 return map1;
4456 dim2 = isl_map_dim(map2, isl_dim_all);
4457 nparam2 = isl_map_dim(map2, isl_dim_param);
4458 if (dim2 < 0 || nparam2 < 0)
4459 goto error;
4460 if (dim2 != nparam2)
4461 isl_assert(map1->ctx,
4462 isl_space_is_equal(map1->dim, map2->dim), goto error);
4464 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4465 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4466 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4468 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4469 map1->n * map2->n, flags);
4470 if (!result)
4471 goto error;
4472 for (i = 0; i < map1->n; ++i)
4473 for (j = 0; j < map2->n; ++j) {
4474 struct isl_basic_map *part;
4475 part = isl_basic_map_intersect(
4476 isl_basic_map_copy(map1->p[i]),
4477 isl_basic_map_copy(map2->p[j]));
4478 if (isl_basic_map_is_empty(part) < 0)
4479 part = isl_basic_map_free(part);
4480 result = isl_map_add_basic_map(result, part);
4481 if (!result)
4482 goto error;
4484 isl_map_free(map1);
4485 isl_map_free(map2);
4486 return result;
4487 error:
4488 isl_map_free(map1);
4489 isl_map_free(map2);
4490 return NULL;
4493 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
4494 __isl_take isl_map *map2)
4496 if (isl_map_check_equal_space(map1, map2) < 0)
4497 goto error;
4498 return map_intersect_internal(map1, map2);
4499 error:
4500 isl_map_free(map1);
4501 isl_map_free(map2);
4502 return NULL;
4505 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
4506 __isl_take isl_map *map2)
4508 isl_map_align_params_bin(&map1, &map2);
4509 return map_intersect(map1, map2);
4512 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
4513 __isl_take isl_set *set2)
4515 return set_from_map(isl_map_intersect(set_to_map(set1),
4516 set_to_map(set2)));
4519 /* map_intersect_internal accepts intersections
4520 * with parameter domains, so we can just call that function.
4522 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
4523 __isl_take isl_set *params)
4525 isl_map_align_params_set(&map, &params);
4526 return map_intersect_internal(map, params);
4529 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4530 __isl_take isl_set *params)
4532 return isl_map_intersect_params(set, params);
4535 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4537 isl_space *space;
4538 unsigned pos;
4539 isl_size n1, n2;
4541 if (!bmap)
4542 return NULL;
4543 bmap = isl_basic_map_cow(bmap);
4544 if (!bmap)
4545 return NULL;
4546 space = isl_space_reverse(isl_space_copy(bmap->dim));
4547 pos = isl_basic_map_offset(bmap, isl_dim_in);
4548 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4549 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4550 if (n1 < 0 || n2 < 0)
4551 bmap = isl_basic_map_free(bmap);
4552 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4553 return isl_basic_map_reset_space(bmap, space);
4556 /* Given a basic map where the tuple of type "type" is a wrapped map,
4557 * swap domain and range of that wrapped map.
4559 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4560 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4562 isl_space *space;
4563 isl_size offset, n1, n2;
4565 space = isl_basic_map_peek_space(bmap);
4566 offset = isl_basic_map_var_offset(bmap, type);
4567 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4568 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4569 if (offset < 0 || n1 < 0 || n2 < 0)
4570 return isl_basic_map_free(bmap);
4572 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4574 space = isl_basic_map_take_space(bmap);
4575 space = isl_space_reverse_wrapped(space, type);
4576 bmap = isl_basic_map_restore_space(bmap, space);
4578 return bmap;
4581 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4582 * (B -> A) -> C.
4584 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4585 __isl_take isl_basic_map *bmap)
4587 isl_space *space;
4589 space = isl_basic_map_peek_space(bmap);
4590 if (isl_space_check_domain_is_wrapping(space) < 0)
4591 return isl_basic_map_free(bmap);
4592 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4594 return bmap;
4597 /* Given a basic map A -> (B -> C), return the corresponding basic map
4598 * A -> (C -> B).
4600 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4601 __isl_take isl_basic_map *bmap)
4603 isl_space *space;
4605 space = isl_basic_map_peek_space(bmap);
4606 if (isl_space_check_range_is_wrapping(space) < 0)
4607 return isl_basic_map_free(bmap);
4608 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4610 return bmap;
4613 /* Given a basic map that is actually a basic set (A -> B),
4614 * return the corresponding basic set (B -> A) as a basic map.
4616 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4617 __isl_take isl_basic_map *bmap)
4619 isl_space *space;
4621 space = isl_basic_map_peek_space(bmap);
4622 if (isl_space_check_is_wrapping(space) < 0)
4623 return isl_basic_map_free(bmap);
4624 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4626 return bmap;
4629 static __isl_give isl_basic_map *basic_map_space_reset(
4630 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4632 isl_space *space;
4634 if (!bmap)
4635 return NULL;
4636 if (!isl_space_is_named_or_nested(bmap->dim, type))
4637 return bmap;
4639 space = isl_basic_map_get_space(bmap);
4640 space = isl_space_reset(space, type);
4641 bmap = isl_basic_map_reset_space(bmap, space);
4642 return bmap;
4645 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4646 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4647 unsigned pos, unsigned n)
4649 isl_bool rational, is_empty;
4650 isl_space *res_space;
4651 struct isl_basic_map *res;
4652 struct isl_dim_map *dim_map;
4653 isl_size total;
4654 unsigned off;
4655 enum isl_dim_type t;
4657 if (n == 0)
4658 return basic_map_space_reset(bmap, type);
4660 is_empty = isl_basic_map_plain_is_empty(bmap);
4661 total = isl_basic_map_dim(bmap, isl_dim_all);
4662 if (is_empty < 0 || total < 0)
4663 return isl_basic_map_free(bmap);
4664 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4665 type, pos, n);
4666 if (!res_space)
4667 return isl_basic_map_free(bmap);
4668 if (is_empty) {
4669 isl_basic_map_free(bmap);
4670 return isl_basic_map_empty(res_space);
4673 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4674 off = 0;
4675 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4676 isl_size dim;
4678 if (t != type) {
4679 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4680 } else {
4681 isl_size size = isl_basic_map_dim(bmap, t);
4682 if (size < 0)
4683 dim_map = isl_dim_map_free(dim_map);
4684 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4685 0, pos, off);
4686 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4687 pos, size - pos, off + pos + n);
4689 dim = isl_space_dim(res_space, t);
4690 if (dim < 0)
4691 dim_map = isl_dim_map_free(dim_map);
4692 off += dim;
4694 isl_dim_map_div(dim_map, bmap, off);
4696 res = isl_basic_map_alloc_space(res_space,
4697 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4698 rational = isl_basic_map_is_rational(bmap);
4699 if (rational < 0)
4700 res = isl_basic_map_free(res);
4701 if (rational)
4702 res = isl_basic_map_set_rational(res);
4703 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4704 return isl_basic_map_finalize(res);
4707 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4708 __isl_take isl_basic_set *bset,
4709 enum isl_dim_type type, unsigned pos, unsigned n)
4711 return isl_basic_map_insert_dims(bset, type, pos, n);
4714 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4715 enum isl_dim_type type, unsigned n)
4717 isl_size dim;
4719 dim = isl_basic_map_dim(bmap, type);
4720 if (dim < 0)
4721 return isl_basic_map_free(bmap);
4722 return isl_basic_map_insert_dims(bmap, type, dim, n);
4725 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4726 enum isl_dim_type type, unsigned n)
4728 if (!bset)
4729 return NULL;
4730 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4731 return isl_basic_map_add_dims(bset, type, n);
4732 error:
4733 isl_basic_set_free(bset);
4734 return NULL;
4737 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4738 enum isl_dim_type type)
4740 isl_space *space;
4742 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4743 return map;
4745 space = isl_map_get_space(map);
4746 space = isl_space_reset(space, type);
4747 map = isl_map_reset_space(map, space);
4748 return map;
4751 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4752 enum isl_dim_type type, unsigned pos, unsigned n)
4754 int i;
4755 isl_space *space;
4757 if (n == 0)
4758 return map_space_reset(map, type);
4760 map = isl_map_cow(map);
4761 if (!map)
4762 return NULL;
4764 for (i = 0; i < map->n; ++i) {
4765 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4766 if (!map->p[i])
4767 goto error;
4770 space = isl_map_take_space(map);
4771 space = isl_space_insert_dims(space, type, pos, n);
4772 map = isl_map_restore_space(map, space);
4774 return map;
4775 error:
4776 isl_map_free(map);
4777 return NULL;
4780 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4781 enum isl_dim_type type, unsigned pos, unsigned n)
4783 return isl_map_insert_dims(set, type, pos, n);
4786 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4787 enum isl_dim_type type, unsigned n)
4789 isl_size dim;
4791 dim = isl_map_dim(map, type);
4792 if (dim < 0)
4793 return isl_map_free(map);
4794 return isl_map_insert_dims(map, type, dim, n);
4797 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4798 enum isl_dim_type type, unsigned n)
4800 if (!set)
4801 return NULL;
4802 isl_assert(set->ctx, type != isl_dim_in, goto error);
4803 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4804 error:
4805 isl_set_free(set);
4806 return NULL;
4809 __isl_give isl_basic_map *isl_basic_map_move_dims(
4810 __isl_take isl_basic_map *bmap,
4811 enum isl_dim_type dst_type, unsigned dst_pos,
4812 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4814 isl_space *space;
4815 struct isl_dim_map *dim_map;
4816 struct isl_basic_map *res;
4817 enum isl_dim_type t;
4818 isl_size total;
4819 unsigned off;
4821 if (!bmap)
4822 return NULL;
4823 if (n == 0) {
4824 bmap = isl_basic_map_reset(bmap, src_type);
4825 bmap = isl_basic_map_reset(bmap, dst_type);
4826 return bmap;
4829 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4830 return isl_basic_map_free(bmap);
4832 if (dst_type == src_type && dst_pos == src_pos)
4833 return bmap;
4835 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4837 if (pos(bmap->dim, dst_type) + dst_pos ==
4838 pos(bmap->dim, src_type) + src_pos +
4839 ((src_type < dst_type) ? n : 0)) {
4840 space = isl_basic_map_take_space(bmap);
4841 space = isl_space_move_dims(space, dst_type, dst_pos,
4842 src_type, src_pos, n);
4843 bmap = isl_basic_map_restore_space(bmap, space);
4844 bmap = isl_basic_map_finalize(bmap);
4846 return bmap;
4849 total = isl_basic_map_dim(bmap, isl_dim_all);
4850 if (total < 0)
4851 return isl_basic_map_free(bmap);
4852 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4854 off = 0;
4855 space = isl_basic_map_peek_space(bmap);
4856 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4857 isl_size size = isl_space_dim(space, t);
4858 if (size < 0)
4859 dim_map = isl_dim_map_free(dim_map);
4860 if (t == dst_type) {
4861 isl_dim_map_dim_range(dim_map, space, t,
4862 0, dst_pos, off);
4863 off += dst_pos;
4864 isl_dim_map_dim_range(dim_map, space, src_type,
4865 src_pos, n, off);
4866 off += n;
4867 isl_dim_map_dim_range(dim_map, space, t,
4868 dst_pos, size - dst_pos, off);
4869 off += size - dst_pos;
4870 } else if (t == src_type) {
4871 isl_dim_map_dim_range(dim_map, space, t,
4872 0, src_pos, off);
4873 off += src_pos;
4874 isl_dim_map_dim_range(dim_map, space, t,
4875 src_pos + n, size - src_pos - n, off);
4876 off += size - src_pos - n;
4877 } else {
4878 isl_dim_map_dim(dim_map, space, t, off);
4879 off += size;
4882 isl_dim_map_div(dim_map, bmap, off);
4884 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4885 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4886 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4887 space = isl_basic_map_take_space(bmap);
4888 space = isl_space_move_dims(space, dst_type, dst_pos,
4889 src_type, src_pos, n);
4890 bmap = isl_basic_map_restore_space(bmap, space);
4891 if (!bmap)
4892 goto error;
4894 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4895 bmap = isl_basic_map_gauss(bmap, NULL);
4896 bmap = isl_basic_map_finalize(bmap);
4898 return bmap;
4899 error:
4900 isl_basic_map_free(bmap);
4901 return NULL;
4904 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4905 enum isl_dim_type dst_type, unsigned dst_pos,
4906 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4908 isl_basic_map *bmap = bset_to_bmap(bset);
4909 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4910 src_type, src_pos, n);
4911 return bset_from_bmap(bmap);
4914 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4915 enum isl_dim_type dst_type, unsigned dst_pos,
4916 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4918 if (!set)
4919 return NULL;
4920 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4921 return set_from_map(isl_map_move_dims(set_to_map(set),
4922 dst_type, dst_pos, src_type, src_pos, n));
4923 error:
4924 isl_set_free(set);
4925 return NULL;
4928 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4929 enum isl_dim_type dst_type, unsigned dst_pos,
4930 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4932 int i;
4933 isl_space *space;
4935 if (n == 0) {
4936 map = isl_map_reset(map, src_type);
4937 map = isl_map_reset(map, dst_type);
4938 return map;
4941 if (isl_map_check_range(map, src_type, src_pos, n))
4942 return isl_map_free(map);
4944 if (dst_type == src_type && dst_pos == src_pos)
4945 return map;
4947 isl_assert(map->ctx, dst_type != src_type, goto error);
4949 map = isl_map_cow(map);
4950 if (!map)
4951 return NULL;
4953 for (i = 0; i < map->n; ++i) {
4954 map->p[i] = isl_basic_map_move_dims(map->p[i],
4955 dst_type, dst_pos,
4956 src_type, src_pos, n);
4957 if (!map->p[i])
4958 goto error;
4961 space = isl_map_take_space(map);
4962 space = isl_space_move_dims(space, dst_type, dst_pos,
4963 src_type, src_pos, n);
4964 map = isl_map_restore_space(map, space);
4966 return map;
4967 error:
4968 isl_map_free(map);
4969 return NULL;
4972 /* Move the specified dimensions to the last columns right before
4973 * the divs. Don't change the dimension specification of bmap.
4974 * That's the responsibility of the caller.
4976 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4977 enum isl_dim_type type, unsigned first, unsigned n)
4979 isl_space *space;
4980 struct isl_dim_map *dim_map;
4981 struct isl_basic_map *res;
4982 enum isl_dim_type t;
4983 isl_size total;
4984 unsigned off;
4986 if (!bmap)
4987 return NULL;
4988 if (isl_basic_map_offset(bmap, type) + first + n ==
4989 isl_basic_map_offset(bmap, isl_dim_div))
4990 return bmap;
4992 total = isl_basic_map_dim(bmap, isl_dim_all);
4993 if (total < 0)
4994 return isl_basic_map_free(bmap);
4995 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4997 off = 0;
4998 space = isl_basic_map_peek_space(bmap);
4999 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
5000 isl_size size = isl_space_dim(space, t);
5001 if (size < 0)
5002 dim_map = isl_dim_map_free(dim_map);
5003 if (t == type) {
5004 isl_dim_map_dim_range(dim_map, space, t,
5005 0, first, off);
5006 off += first;
5007 isl_dim_map_dim_range(dim_map, space, t,
5008 first, n, total - bmap->n_div - n);
5009 isl_dim_map_dim_range(dim_map, space, t,
5010 first + n, size - (first + n), off);
5011 off += size - (first + n);
5012 } else {
5013 isl_dim_map_dim(dim_map, space, t, off);
5014 off += size;
5017 isl_dim_map_div(dim_map, bmap, off + n);
5019 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
5020 bmap->n_div, bmap->n_eq, bmap->n_ineq);
5021 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
5022 return res;
5025 /* Insert "n" rows in the divs of "bmap".
5027 * The number of columns is not changed, which means that the last
5028 * dimensions of "bmap" are being reintepreted as the new divs.
5029 * The space of "bmap" is not adjusted, however, which means
5030 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
5031 * from the space of "bmap" is the responsibility of the caller.
5033 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
5034 int n)
5036 int i;
5037 size_t row_size;
5038 isl_int **new_div;
5039 isl_int *old;
5041 bmap = isl_basic_map_cow(bmap);
5042 if (!bmap)
5043 return NULL;
5045 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
5046 old = bmap->block2.data;
5047 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
5048 (bmap->extra + n) * (1 + row_size));
5049 if (!bmap->block2.data)
5050 return isl_basic_map_free(bmap);
5051 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
5052 if (!new_div)
5053 return isl_basic_map_free(bmap);
5054 for (i = 0; i < n; ++i) {
5055 new_div[i] = bmap->block2.data +
5056 (bmap->extra + i) * (1 + row_size);
5057 isl_seq_clr(new_div[i], 1 + row_size);
5059 for (i = 0; i < bmap->extra; ++i)
5060 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
5061 free(bmap->div);
5062 bmap->div = new_div;
5063 bmap->n_div += n;
5064 bmap->extra += n;
5066 return bmap;
5069 /* Drop constraints from "bmap" that only involve the variables
5070 * of "type" in the range [first, first + n] that are not related
5071 * to any of the variables outside that interval.
5072 * These constraints cannot influence the values for the variables
5073 * outside the interval, except in case they cause "bmap" to be empty.
5074 * Only drop the constraints if "bmap" is known to be non-empty.
5076 static __isl_give isl_basic_map *drop_irrelevant_constraints(
5077 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
5078 unsigned first, unsigned n)
5080 int i;
5081 int *groups;
5082 isl_size dim, n_div;
5083 isl_bool non_empty;
5085 non_empty = isl_basic_map_plain_is_non_empty(bmap);
5086 if (non_empty < 0)
5087 return isl_basic_map_free(bmap);
5088 if (!non_empty)
5089 return bmap;
5091 dim = isl_basic_map_dim(bmap, isl_dim_all);
5092 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5093 if (dim < 0 || n_div < 0)
5094 return isl_basic_map_free(bmap);
5095 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
5096 if (!groups)
5097 return isl_basic_map_free(bmap);
5098 first += isl_basic_map_offset(bmap, type) - 1;
5099 for (i = 0; i < first; ++i)
5100 groups[i] = -1;
5101 for (i = first + n; i < dim - n_div; ++i)
5102 groups[i] = -1;
5104 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
5106 return bmap;
5109 /* Turn the n dimensions of type type, starting at first
5110 * into existentially quantified variables.
5112 * If a subset of the projected out variables are unrelated
5113 * to any of the variables that remain, then the constraints
5114 * involving this subset are simply dropped first.
5116 __isl_give isl_basic_map *isl_basic_map_project_out(
5117 __isl_take isl_basic_map *bmap,
5118 enum isl_dim_type type, unsigned first, unsigned n)
5120 isl_bool empty;
5121 isl_space *space;
5123 if (n == 0)
5124 return basic_map_space_reset(bmap, type);
5125 if (type == isl_dim_div)
5126 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5127 "cannot project out existentially quantified variables",
5128 return isl_basic_map_free(bmap));
5130 empty = isl_basic_map_plain_is_empty(bmap);
5131 if (empty < 0)
5132 return isl_basic_map_free(bmap);
5133 if (empty)
5134 bmap = isl_basic_map_set_to_empty(bmap);
5136 bmap = drop_irrelevant_constraints(bmap, type, first, n);
5137 if (!bmap)
5138 return NULL;
5140 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
5141 return isl_basic_map_remove_dims(bmap, type, first, n);
5143 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
5144 return isl_basic_map_free(bmap);
5146 bmap = move_last(bmap, type, first, n);
5147 bmap = isl_basic_map_cow(bmap);
5148 bmap = insert_div_rows(bmap, n);
5150 space = isl_basic_map_take_space(bmap);
5151 space = isl_space_drop_dims(space, type, first, n);
5152 bmap = isl_basic_map_restore_space(bmap, space);
5153 bmap = isl_basic_map_simplify(bmap);
5154 bmap = isl_basic_map_drop_redundant_divs(bmap);
5155 return isl_basic_map_finalize(bmap);
5158 /* Turn the n dimensions of type type, starting at first
5159 * into existentially quantified variables.
5161 __isl_give isl_basic_set *isl_basic_set_project_out(
5162 __isl_take isl_basic_set *bset, enum isl_dim_type type,
5163 unsigned first, unsigned n)
5165 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
5166 type, first, n));
5169 /* Turn the n dimensions of type type, starting at first
5170 * into existentially quantified variables.
5172 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
5173 enum isl_dim_type type, unsigned first, unsigned n)
5175 int i;
5176 isl_space *space;
5178 if (n == 0)
5179 return map_space_reset(map, type);
5181 if (isl_map_check_range(map, type, first, n) < 0)
5182 return isl_map_free(map);
5184 map = isl_map_cow(map);
5185 if (!map)
5186 return NULL;
5188 for (i = 0; i < map->n; ++i) {
5189 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
5190 if (!map->p[i])
5191 goto error;
5194 if (map->n > 1)
5195 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5196 map = isl_map_unmark_normalized(map);
5198 space = isl_map_take_space(map);
5199 space = isl_space_drop_dims(space, type, first, n);
5200 map = isl_map_restore_space(map, space);
5202 return map;
5203 error:
5204 isl_map_free(map);
5205 return NULL;
5208 #undef TYPE
5209 #define TYPE isl_map
5210 #include "isl_project_out_all_params_templ.c"
5211 #include "isl_project_out_param_templ.c"
5213 /* Turn all the dimensions of type "type", except the "n" starting at "first"
5214 * into existentially quantified variables.
5216 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
5217 enum isl_dim_type type, unsigned first, unsigned n)
5219 isl_size dim;
5221 dim = isl_map_dim(map, type);
5222 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
5223 return isl_map_free(map);
5224 map = isl_map_project_out(map, type, first + n, dim - (first + n));
5225 map = isl_map_project_out(map, type, 0, first);
5226 return map;
5229 /* Turn the n dimensions of type type, starting at first
5230 * into existentially quantified variables.
5232 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
5233 enum isl_dim_type type, unsigned first, unsigned n)
5235 return set_from_map(isl_map_project_out(set_to_map(set),
5236 type, first, n));
5239 /* If "set" involves a parameter with identifier "id",
5240 * then turn it into an existentially quantified variable.
5242 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
5243 __isl_take isl_id *id)
5245 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
5248 /* If "set" involves any of the parameters with identifiers in "list",
5249 * then turn them into existentially quantified variables.
5251 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
5252 __isl_take isl_id_list *list)
5254 isl_map *map;
5256 map = set_to_map(set);
5257 map = isl_map_project_out_param_id_list(map, list);
5258 return set_from_map(map);
5261 /* Project out all parameters from "set" by existentially quantifying
5262 * over them.
5264 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
5266 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
5269 /* Return a map that projects the elements in "set" onto their
5270 * "n" set dimensions starting at "first".
5271 * "type" should be equal to isl_dim_set.
5273 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
5274 enum isl_dim_type type, unsigned first, unsigned n)
5276 int i;
5277 isl_map *map;
5279 if (type != isl_dim_set)
5280 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5281 "only set dimensions can be projected out", goto error);
5282 if (isl_set_check_range(set, type, first, n) < 0)
5283 return isl_set_free(set);
5285 map = isl_map_from_domain(set);
5286 map = isl_map_add_dims(map, isl_dim_out, n);
5287 for (i = 0; i < n; ++i)
5288 map = isl_map_equate(map, isl_dim_in, first + i,
5289 isl_dim_out, i);
5290 return map;
5291 error:
5292 isl_set_free(set);
5293 return NULL;
5296 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
5297 unsigned n)
5299 int i, j;
5300 isl_size total;
5302 total = isl_basic_map_dim(bmap, isl_dim_all);
5303 if (total < 0)
5304 return isl_basic_map_free(bmap);
5305 for (i = 0; i < n; ++i) {
5306 j = isl_basic_map_alloc_div(bmap);
5307 if (j < 0)
5308 goto error;
5309 isl_seq_clr(bmap->div[j], 1 + 1 + total);
5311 return bmap;
5312 error:
5313 isl_basic_map_free(bmap);
5314 return NULL;
5317 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
5319 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
5320 __isl_keep isl_basic_map *bmap2)
5322 isl_space *space1, *space2;
5324 space1 = isl_basic_map_peek_space(bmap1);
5325 space2 = isl_basic_map_peek_space(bmap2);
5326 return isl_space_tuple_is_equal(space1, isl_dim_out,
5327 space2, isl_dim_in);
5330 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
5332 static isl_stat isl_basic_map_check_applies_range(
5333 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
5335 isl_bool equal;
5337 equal = isl_basic_map_applies_range(bmap1, bmap2);
5338 if (equal < 0)
5339 return isl_stat_error;
5340 if (!equal)
5341 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
5342 "spaces don't match", return isl_stat_error);
5343 return isl_stat_ok;
5346 __isl_give isl_basic_map *isl_basic_map_apply_range(
5347 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
5349 isl_space *space_result = NULL;
5350 struct isl_basic_map *bmap;
5351 isl_size n_in, n_out, n, nparam;
5352 unsigned total, pos;
5353 struct isl_dim_map *dim_map1, *dim_map2;
5355 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
5356 goto error;
5357 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
5358 goto error;
5360 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
5361 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
5362 n = isl_basic_map_dim(bmap1, isl_dim_out);
5363 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
5364 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
5365 goto error;
5367 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
5368 isl_basic_map_get_space(bmap2));
5370 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
5371 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5372 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5373 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5374 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5375 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5376 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
5377 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
5378 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5379 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
5380 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
5382 bmap = isl_basic_map_alloc_space(space_result,
5383 bmap1->n_div + bmap2->n_div + n,
5384 bmap1->n_eq + bmap2->n_eq,
5385 bmap1->n_ineq + bmap2->n_ineq);
5386 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
5387 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5388 bmap = add_divs(bmap, n);
5389 bmap = isl_basic_map_simplify(bmap);
5390 bmap = isl_basic_map_drop_redundant_divs(bmap);
5391 return isl_basic_map_finalize(bmap);
5392 error:
5393 isl_basic_map_free(bmap1);
5394 isl_basic_map_free(bmap2);
5395 return NULL;
5398 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
5399 __isl_take isl_basic_map *bmap)
5401 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
5402 goto error;
5404 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
5405 bmap));
5406 error:
5407 isl_basic_set_free(bset);
5408 isl_basic_map_free(bmap);
5409 return NULL;
5412 __isl_give isl_basic_map *isl_basic_map_apply_domain(
5413 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
5415 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
5416 goto error;
5417 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
5418 bmap2->dim, isl_dim_in))
5419 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
5420 "spaces don't match", goto error);
5422 bmap1 = isl_basic_map_reverse(bmap1);
5423 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
5424 return isl_basic_map_reverse(bmap1);
5425 error:
5426 isl_basic_map_free(bmap1);
5427 isl_basic_map_free(bmap2);
5428 return NULL;
5431 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5432 * A \cap B -> f(A) + f(B)
5434 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
5435 __isl_take isl_basic_map *bmap2)
5437 isl_size n_in, n_out, nparam;
5438 unsigned total, pos;
5439 struct isl_basic_map *bmap = NULL;
5440 struct isl_dim_map *dim_map1, *dim_map2;
5441 int i;
5443 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
5444 goto error;
5446 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
5447 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
5448 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
5449 if (nparam < 0 || n_in < 0 || n_out < 0)
5450 goto error;
5452 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
5453 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5454 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
5455 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5456 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
5457 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5458 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
5459 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
5460 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5461 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
5462 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
5464 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
5465 bmap1->n_div + bmap2->n_div + 2 * n_out,
5466 bmap1->n_eq + bmap2->n_eq + n_out,
5467 bmap1->n_ineq + bmap2->n_ineq);
5468 for (i = 0; i < n_out; ++i) {
5469 int j = isl_basic_map_alloc_equality(bmap);
5470 if (j < 0)
5471 goto error;
5472 isl_seq_clr(bmap->eq[j], 1+total);
5473 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
5474 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
5475 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
5477 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
5478 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5479 bmap = add_divs(bmap, 2 * n_out);
5481 bmap = isl_basic_map_simplify(bmap);
5482 return isl_basic_map_finalize(bmap);
5483 error:
5484 isl_basic_map_free(bmap);
5485 isl_basic_map_free(bmap1);
5486 isl_basic_map_free(bmap2);
5487 return NULL;
5490 /* Given two maps A -> f(A) and B -> g(B), construct a map
5491 * A \cap B -> f(A) + f(B)
5493 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
5494 __isl_take isl_map *map2)
5496 struct isl_map *result;
5497 int i, j;
5499 if (isl_map_check_equal_space(map1, map2) < 0)
5500 goto error;
5502 result = isl_map_alloc_space(isl_space_copy(map1->dim),
5503 map1->n * map2->n, 0);
5504 if (!result)
5505 goto error;
5506 for (i = 0; i < map1->n; ++i)
5507 for (j = 0; j < map2->n; ++j) {
5508 struct isl_basic_map *part;
5509 part = isl_basic_map_sum(
5510 isl_basic_map_copy(map1->p[i]),
5511 isl_basic_map_copy(map2->p[j]));
5512 if (isl_basic_map_is_empty(part))
5513 isl_basic_map_free(part);
5514 else
5515 result = isl_map_add_basic_map(result, part);
5516 if (!result)
5517 goto error;
5519 isl_map_free(map1);
5520 isl_map_free(map2);
5521 return result;
5522 error:
5523 isl_map_free(map1);
5524 isl_map_free(map2);
5525 return NULL;
5528 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
5529 __isl_take isl_set *set2)
5531 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5534 /* Given a basic map A -> f(A), construct A -> -f(A).
5536 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5538 int i, j;
5539 unsigned off;
5540 isl_size n;
5542 bmap = isl_basic_map_cow(bmap);
5543 n = isl_basic_map_dim(bmap, isl_dim_out);
5544 if (n < 0)
5545 return isl_basic_map_free(bmap);
5547 off = isl_basic_map_offset(bmap, isl_dim_out);
5548 for (i = 0; i < bmap->n_eq; ++i)
5549 for (j = 0; j < n; ++j)
5550 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5551 for (i = 0; i < bmap->n_ineq; ++i)
5552 for (j = 0; j < n; ++j)
5553 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5554 for (i = 0; i < bmap->n_div; ++i)
5555 for (j = 0; j < n; ++j)
5556 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5557 bmap = isl_basic_map_gauss(bmap, NULL);
5558 return isl_basic_map_finalize(bmap);
5561 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5563 return isl_basic_map_neg(bset);
5566 /* Given a map A -> f(A), construct A -> -f(A).
5568 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5570 int i;
5572 map = isl_map_cow(map);
5573 if (!map)
5574 return NULL;
5576 for (i = 0; i < map->n; ++i) {
5577 map->p[i] = isl_basic_map_neg(map->p[i]);
5578 if (!map->p[i])
5579 goto error;
5582 return map;
5583 error:
5584 isl_map_free(map);
5585 return NULL;
5588 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5590 return set_from_map(isl_map_neg(set_to_map(set)));
5593 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5594 * A -> floor(f(A)/d).
5596 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5597 isl_int d)
5599 isl_size n_in, n_out, nparam;
5600 unsigned total, pos;
5601 struct isl_basic_map *result = NULL;
5602 struct isl_dim_map *dim_map;
5603 int i;
5605 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5606 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5607 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5608 if (nparam < 0 || n_in < 0 || n_out < 0)
5609 return isl_basic_map_free(bmap);
5611 total = nparam + n_in + n_out + bmap->n_div + n_out;
5612 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5613 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5614 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5615 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5616 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5618 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5619 bmap->n_div + n_out,
5620 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5621 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5622 result = add_divs(result, n_out);
5623 for (i = 0; i < n_out; ++i) {
5624 int j;
5625 j = isl_basic_map_alloc_inequality(result);
5626 if (j < 0)
5627 goto error;
5628 isl_seq_clr(result->ineq[j], 1+total);
5629 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5630 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5631 j = isl_basic_map_alloc_inequality(result);
5632 if (j < 0)
5633 goto error;
5634 isl_seq_clr(result->ineq[j], 1+total);
5635 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5636 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5637 isl_int_sub_ui(result->ineq[j][0], d, 1);
5640 result = isl_basic_map_simplify(result);
5641 return isl_basic_map_finalize(result);
5642 error:
5643 isl_basic_map_free(result);
5644 return NULL;
5647 /* Given a map A -> f(A) and an integer d, construct a map
5648 * A -> floor(f(A)/d).
5650 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5652 int i;
5654 map = isl_map_cow(map);
5655 if (!map)
5656 return NULL;
5658 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5659 for (i = 0; i < map->n; ++i) {
5660 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5661 if (!map->p[i])
5662 goto error;
5664 map = isl_map_unmark_normalized(map);
5666 return map;
5667 error:
5668 isl_map_free(map);
5669 return NULL;
5672 /* Given a map A -> f(A) and an integer d, construct a map
5673 * A -> floor(f(A)/d).
5675 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5676 __isl_take isl_val *d)
5678 if (!map || !d)
5679 goto error;
5680 if (!isl_val_is_int(d))
5681 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5682 "expecting integer denominator", goto error);
5683 map = isl_map_floordiv(map, d->n);
5684 isl_val_free(d);
5685 return map;
5686 error:
5687 isl_map_free(map);
5688 isl_val_free(d);
5689 return NULL;
5692 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5693 unsigned pos)
5695 int i;
5696 isl_size nparam;
5697 isl_size n_in;
5698 isl_size total;
5700 total = isl_basic_map_dim(bmap, isl_dim_all);
5701 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5702 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5703 if (total < 0 || nparam < 0 || n_in < 0)
5704 return isl_basic_map_free(bmap);
5705 i = isl_basic_map_alloc_equality(bmap);
5706 if (i < 0)
5707 goto error;
5708 isl_seq_clr(bmap->eq[i], 1 + total);
5709 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5710 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5711 return isl_basic_map_finalize(bmap);
5712 error:
5713 isl_basic_map_free(bmap);
5714 return NULL;
5717 /* Add a constraint to "bmap" expressing i_pos < o_pos
5719 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5720 unsigned pos)
5722 int i;
5723 isl_size nparam;
5724 isl_size n_in;
5725 isl_size total;
5727 total = isl_basic_map_dim(bmap, isl_dim_all);
5728 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5729 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5730 if (total < 0 || nparam < 0 || n_in < 0)
5731 return isl_basic_map_free(bmap);
5732 i = isl_basic_map_alloc_inequality(bmap);
5733 if (i < 0)
5734 goto error;
5735 isl_seq_clr(bmap->ineq[i], 1 + total);
5736 isl_int_set_si(bmap->ineq[i][0], -1);
5737 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5738 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5739 return isl_basic_map_finalize(bmap);
5740 error:
5741 isl_basic_map_free(bmap);
5742 return NULL;
5745 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5747 static __isl_give isl_basic_map *var_less_or_equal(
5748 __isl_take isl_basic_map *bmap, unsigned pos)
5750 int i;
5751 isl_size nparam;
5752 isl_size n_in;
5753 isl_size total;
5755 total = isl_basic_map_dim(bmap, isl_dim_all);
5756 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5757 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5758 if (total < 0 || nparam < 0 || n_in < 0)
5759 return isl_basic_map_free(bmap);
5760 i = isl_basic_map_alloc_inequality(bmap);
5761 if (i < 0)
5762 goto error;
5763 isl_seq_clr(bmap->ineq[i], 1 + total);
5764 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5765 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5766 return isl_basic_map_finalize(bmap);
5767 error:
5768 isl_basic_map_free(bmap);
5769 return NULL;
5772 /* Add a constraint to "bmap" expressing i_pos > o_pos
5774 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5775 unsigned pos)
5777 int i;
5778 isl_size nparam;
5779 isl_size n_in;
5780 isl_size total;
5782 total = isl_basic_map_dim(bmap, isl_dim_all);
5783 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5784 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5785 if (total < 0 || nparam < 0 || n_in < 0)
5786 return isl_basic_map_free(bmap);
5787 i = isl_basic_map_alloc_inequality(bmap);
5788 if (i < 0)
5789 goto error;
5790 isl_seq_clr(bmap->ineq[i], 1 + total);
5791 isl_int_set_si(bmap->ineq[i][0], -1);
5792 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5793 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5794 return isl_basic_map_finalize(bmap);
5795 error:
5796 isl_basic_map_free(bmap);
5797 return NULL;
5800 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5802 static __isl_give isl_basic_map *var_more_or_equal(
5803 __isl_take isl_basic_map *bmap, unsigned pos)
5805 int i;
5806 isl_size nparam;
5807 isl_size n_in;
5808 isl_size total;
5810 total = isl_basic_map_dim(bmap, isl_dim_all);
5811 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5812 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5813 if (total < 0 || nparam < 0 || n_in < 0)
5814 return isl_basic_map_free(bmap);
5815 i = isl_basic_map_alloc_inequality(bmap);
5816 if (i < 0)
5817 goto error;
5818 isl_seq_clr(bmap->ineq[i], 1 + total);
5819 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5820 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5821 return isl_basic_map_finalize(bmap);
5822 error:
5823 isl_basic_map_free(bmap);
5824 return NULL;
5827 __isl_give isl_basic_map *isl_basic_map_equal(
5828 __isl_take isl_space *space, unsigned n_equal)
5830 int i;
5831 struct isl_basic_map *bmap;
5832 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5833 if (!bmap)
5834 return NULL;
5835 for (i = 0; i < n_equal && bmap; ++i)
5836 bmap = var_equal(bmap, i);
5837 return isl_basic_map_finalize(bmap);
5840 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5842 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5843 unsigned pos)
5845 int i;
5846 struct isl_basic_map *bmap;
5847 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5848 if (!bmap)
5849 return NULL;
5850 for (i = 0; i < pos && bmap; ++i)
5851 bmap = var_equal(bmap, i);
5852 if (bmap)
5853 bmap = var_less(bmap, pos);
5854 return isl_basic_map_finalize(bmap);
5857 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5859 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5860 __isl_take isl_space *space, unsigned pos)
5862 int i;
5863 isl_basic_map *bmap;
5865 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5866 for (i = 0; i < pos; ++i)
5867 bmap = var_equal(bmap, i);
5868 bmap = var_less_or_equal(bmap, pos);
5869 return isl_basic_map_finalize(bmap);
5872 /* Return a relation on "space" expressing i_pos > o_pos
5874 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5875 unsigned pos)
5877 int i;
5878 struct isl_basic_map *bmap;
5879 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5880 if (!bmap)
5881 return NULL;
5882 for (i = 0; i < pos && bmap; ++i)
5883 bmap = var_equal(bmap, i);
5884 if (bmap)
5885 bmap = var_more(bmap, pos);
5886 return isl_basic_map_finalize(bmap);
5889 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5891 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5892 __isl_take isl_space *space, unsigned pos)
5894 int i;
5895 isl_basic_map *bmap;
5897 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5898 for (i = 0; i < pos; ++i)
5899 bmap = var_equal(bmap, i);
5900 bmap = var_more_or_equal(bmap, pos);
5901 return isl_basic_map_finalize(bmap);
5904 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5905 unsigned n, int equal)
5907 struct isl_map *map;
5908 int i;
5910 if (n == 0 && equal)
5911 return isl_map_universe(space);
5913 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5915 for (i = 0; i + 1 < n; ++i)
5916 map = isl_map_add_basic_map(map,
5917 isl_basic_map_less_at(isl_space_copy(space), i));
5918 if (n > 0) {
5919 if (equal)
5920 map = isl_map_add_basic_map(map,
5921 isl_basic_map_less_or_equal_at(space, n - 1));
5922 else
5923 map = isl_map_add_basic_map(map,
5924 isl_basic_map_less_at(space, n - 1));
5925 } else
5926 isl_space_free(space);
5928 return map;
5931 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5933 if (!space)
5934 return NULL;
5935 return map_lex_lte_first(space, space->n_out, equal);
5938 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5939 unsigned n)
5941 return map_lex_lte_first(space, n, 0);
5944 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5945 unsigned n)
5947 return map_lex_lte_first(space, n, 1);
5950 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5952 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5955 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5957 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5960 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5961 unsigned n, int equal)
5963 struct isl_map *map;
5964 int i;
5966 if (n == 0 && equal)
5967 return isl_map_universe(space);
5969 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5971 for (i = 0; i + 1 < n; ++i)
5972 map = isl_map_add_basic_map(map,
5973 isl_basic_map_more_at(isl_space_copy(space), i));
5974 if (n > 0) {
5975 if (equal)
5976 map = isl_map_add_basic_map(map,
5977 isl_basic_map_more_or_equal_at(space, n - 1));
5978 else
5979 map = isl_map_add_basic_map(map,
5980 isl_basic_map_more_at(space, n - 1));
5981 } else
5982 isl_space_free(space);
5984 return map;
5987 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5989 if (!space)
5990 return NULL;
5991 return map_lex_gte_first(space, space->n_out, equal);
5994 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5995 unsigned n)
5997 return map_lex_gte_first(space, n, 0);
6000 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
6001 unsigned n)
6003 return map_lex_gte_first(space, n, 1);
6006 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
6008 return map_lex_gte(isl_space_map_from_set(set_space), 0);
6011 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
6013 return map_lex_gte(isl_space_map_from_set(set_space), 1);
6016 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
6017 __isl_take isl_set *set2)
6019 isl_map *map;
6020 map = isl_map_lex_le(isl_set_get_space(set1));
6021 map = isl_map_intersect_domain(map, set1);
6022 map = isl_map_intersect_range(map, set2);
6023 return map;
6026 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
6027 __isl_take isl_set *set2)
6029 isl_map *map;
6030 map = isl_map_lex_lt(isl_set_get_space(set1));
6031 map = isl_map_intersect_domain(map, set1);
6032 map = isl_map_intersect_range(map, set2);
6033 return map;
6036 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
6037 __isl_take isl_set *set2)
6039 isl_map *map;
6040 map = isl_map_lex_ge(isl_set_get_space(set1));
6041 map = isl_map_intersect_domain(map, set1);
6042 map = isl_map_intersect_range(map, set2);
6043 return map;
6046 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
6047 __isl_take isl_set *set2)
6049 isl_map *map;
6050 map = isl_map_lex_gt(isl_set_get_space(set1));
6051 map = isl_map_intersect_domain(map, set1);
6052 map = isl_map_intersect_range(map, set2);
6053 return map;
6056 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
6057 __isl_take isl_map *map2)
6059 isl_map *map;
6060 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
6061 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6062 map = isl_map_apply_range(map, isl_map_reverse(map2));
6063 return map;
6066 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
6067 __isl_take isl_map *map2)
6069 isl_map *map;
6070 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
6071 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6072 map = isl_map_apply_range(map, isl_map_reverse(map2));
6073 return map;
6076 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
6077 __isl_take isl_map *map2)
6079 isl_map *map;
6080 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
6081 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6082 map = isl_map_apply_range(map, isl_map_reverse(map2));
6083 return map;
6086 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
6087 __isl_take isl_map *map2)
6089 isl_map *map;
6090 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
6091 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6092 map = isl_map_apply_range(map, isl_map_reverse(map2));
6093 return map;
6096 /* For the div d = floor(f/m) at position "div", add the constraint
6098 * f - m d >= 0
6100 static __isl_give isl_basic_map *add_upper_div_constraint(
6101 __isl_take isl_basic_map *bmap, unsigned div)
6103 int i;
6104 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
6105 isl_size n_div;
6106 unsigned pos;
6108 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6109 if (v_div < 0 || n_div < 0)
6110 return isl_basic_map_free(bmap);
6111 pos = v_div + div;
6112 i = isl_basic_map_alloc_inequality(bmap);
6113 if (i < 0)
6114 return isl_basic_map_free(bmap);
6115 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
6116 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
6118 return bmap;
6121 /* For the div d = floor(f/m) at position "div", add the constraint
6123 * -(f-(m-1)) + m d >= 0
6125 static __isl_give isl_basic_map *add_lower_div_constraint(
6126 __isl_take isl_basic_map *bmap, unsigned div)
6128 int i;
6129 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
6130 isl_size n_div;
6131 unsigned pos;
6133 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6134 if (v_div < 0 || n_div < 0)
6135 return isl_basic_map_free(bmap);
6136 pos = v_div + div;
6137 i = isl_basic_map_alloc_inequality(bmap);
6138 if (i < 0)
6139 return isl_basic_map_free(bmap);
6140 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
6141 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
6142 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
6143 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
6145 return bmap;
6148 /* For the div d = floor(f/m) at position "pos", add the constraints
6150 * f - m d >= 0
6151 * -(f-(m-1)) + m d >= 0
6153 * Note that the second constraint is the negation of
6155 * f - m d >= m
6157 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
6158 __isl_take isl_basic_map *bmap, unsigned pos)
6160 bmap = add_upper_div_constraint(bmap, pos);
6161 bmap = add_lower_div_constraint(bmap, pos);
6162 return bmap;
6165 /* For the div d = floor(f/m) at position "pos", add the constraints
6167 * f - m d >= 0
6168 * -(f-(m-1)) + m d >= 0
6170 * Note that the second constraint is the negation of
6172 * f - m d >= m
6174 __isl_give isl_basic_set *isl_basic_set_add_div_constraints(
6175 __isl_take isl_basic_set *bset, unsigned pos)
6177 isl_basic_map *bmap = bset_to_bmap(bset);
6178 bmap = isl_basic_map_add_div_constraints(bmap, pos);
6179 return bset_from_bmap(bmap);
6182 /* For each known div d = floor(f/m), add the constraints
6184 * f - m d >= 0
6185 * -(f-(m-1)) + m d >= 0
6187 * Remove duplicate constraints in case of some these div constraints
6188 * already appear in "bmap".
6190 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
6191 __isl_take isl_basic_map *bmap)
6193 isl_size n_div;
6195 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6196 if (n_div < 0)
6197 return isl_basic_map_free(bmap);
6198 if (n_div == 0)
6199 return bmap;
6201 bmap = add_known_div_constraints(bmap);
6202 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
6203 bmap = isl_basic_map_finalize(bmap);
6204 return bmap;
6207 /* Add the div constraint of sign "sign" for div "div" of "bmap".
6209 * In particular, if this div is of the form d = floor(f/m),
6210 * then add the constraint
6212 * f - m d >= 0
6214 * if sign < 0 or the constraint
6216 * -(f-(m-1)) + m d >= 0
6218 * if sign > 0.
6220 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
6221 __isl_take isl_basic_map *bmap, unsigned div, int sign)
6223 if (sign < 0)
6224 return add_upper_div_constraint(bmap, div);
6225 else
6226 return add_lower_div_constraint(bmap, div);
6229 __isl_give isl_basic_set *isl_basic_map_underlying_set(
6230 __isl_take isl_basic_map *bmap)
6232 isl_space *space;
6234 if (!bmap)
6235 goto error;
6236 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
6237 bmap->n_div == 0 &&
6238 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
6239 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
6240 return bset_from_bmap(bmap);
6241 bmap = isl_basic_map_cow(bmap);
6242 if (!bmap)
6243 return NULL;
6244 space = isl_basic_map_take_space(bmap);
6245 space = isl_space_underlying(space, bmap->n_div);
6246 bmap = isl_basic_map_restore_space(bmap, space);
6247 if (!bmap)
6248 return NULL;
6249 bmap->extra -= bmap->n_div;
6250 bmap->n_div = 0;
6251 bmap = isl_basic_map_finalize(bmap);
6252 return bset_from_bmap(bmap);
6253 error:
6254 isl_basic_map_free(bmap);
6255 return NULL;
6258 __isl_give isl_basic_set *isl_basic_set_underlying_set(
6259 __isl_take isl_basic_set *bset)
6261 return isl_basic_map_underlying_set(bset_to_bmap(bset));
6264 /* Replace each element in "list" by the result of applying
6265 * isl_basic_map_underlying_set to the element.
6267 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
6268 __isl_take isl_basic_map_list *list)
6270 int i;
6271 isl_size n;
6273 n = isl_basic_map_list_n_basic_map(list);
6274 if (n < 0)
6275 goto error;
6277 for (i = 0; i < n; ++i) {
6278 isl_basic_map *bmap;
6279 isl_basic_set *bset;
6281 bmap = isl_basic_map_list_get_basic_map(list, i);
6282 bset = isl_basic_set_underlying_set(bmap);
6283 list = isl_basic_set_list_set_basic_set(list, i, bset);
6286 return list;
6287 error:
6288 isl_basic_map_list_free(list);
6289 return NULL;
6292 __isl_give isl_basic_map *isl_basic_map_overlying_set(
6293 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
6295 struct isl_basic_map *bmap;
6296 struct isl_ctx *ctx;
6297 isl_size dim, bmap_total;
6298 unsigned total;
6299 int i;
6301 if (!bset || !like)
6302 goto error;
6303 ctx = bset->ctx;
6304 if (isl_basic_set_check_no_params(bset) < 0 ||
6305 isl_basic_set_check_no_locals(bset) < 0)
6306 goto error;
6307 dim = isl_basic_set_dim(bset, isl_dim_set);
6308 bmap_total = isl_basic_map_dim(like, isl_dim_all);
6309 if (dim < 0 || bmap_total < 0)
6310 goto error;
6311 isl_assert(ctx, dim == bmap_total, goto error);
6312 if (like->n_div == 0) {
6313 isl_space *space = isl_basic_map_get_space(like);
6314 isl_basic_map_free(like);
6315 return isl_basic_map_reset_space(bset, space);
6317 bset = isl_basic_set_cow(bset);
6318 if (!bset)
6319 goto error;
6320 total = dim + bset->extra;
6321 bmap = bset_to_bmap(bset);
6322 isl_space_free(isl_basic_map_take_space(bmap));
6323 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
6324 if (!bmap)
6325 goto error;
6326 bmap->n_div = like->n_div;
6327 bmap->extra += like->n_div;
6328 if (bmap->extra) {
6329 unsigned ltotal;
6330 isl_int **div;
6331 ltotal = total - bmap->extra + like->extra;
6332 if (ltotal > total)
6333 ltotal = total;
6334 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
6335 bmap->extra * (1 + 1 + total));
6336 if (isl_blk_is_error(bmap->block2))
6337 goto error;
6338 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
6339 if (!div)
6340 goto error;
6341 bmap->div = div;
6342 for (i = 0; i < bmap->extra; ++i)
6343 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
6344 for (i = 0; i < like->n_div; ++i) {
6345 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
6346 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
6348 bmap = isl_basic_map_add_known_div_constraints(bmap);
6350 isl_basic_map_free(like);
6351 bmap = isl_basic_map_simplify(bmap);
6352 bmap = isl_basic_map_finalize(bmap);
6353 return bmap;
6354 error:
6355 isl_basic_map_free(like);
6356 isl_basic_set_free(bset);
6357 return NULL;
6360 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
6361 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
6363 return bset_from_bmap(isl_basic_map_overlying_set(bset,
6364 bset_to_bmap(like)));
6367 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
6369 int i;
6371 map = isl_map_cow(map);
6372 if (!map)
6373 return NULL;
6374 map->dim = isl_space_cow(map->dim);
6375 if (!map->dim)
6376 goto error;
6378 for (i = 1; i < map->n; ++i)
6379 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
6380 goto error);
6381 for (i = 0; i < map->n; ++i) {
6382 map->p[i] = bset_to_bmap(
6383 isl_basic_map_underlying_set(map->p[i]));
6384 if (!map->p[i])
6385 goto error;
6387 if (map->n == 0)
6388 map->dim = isl_space_underlying(map->dim, 0);
6389 else {
6390 isl_space_free(map->dim);
6391 map->dim = isl_space_copy(map->p[0]->dim);
6393 if (!map->dim)
6394 goto error;
6395 return set_from_map(map);
6396 error:
6397 isl_map_free(map);
6398 return NULL;
6401 /* Replace the space of "bmap" by "space".
6403 * If the space of "bmap" is identical to "space" (including the identifiers
6404 * of the input and output dimensions), then simply return the original input.
6406 __isl_give isl_basic_map *isl_basic_map_reset_space(
6407 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
6409 isl_bool equal;
6410 isl_space *bmap_space;
6412 bmap_space = isl_basic_map_peek_space(bmap);
6413 equal = isl_space_is_equal(bmap_space, space);
6414 if (equal >= 0 && equal)
6415 equal = isl_space_has_equal_ids(bmap_space, space);
6416 if (equal < 0)
6417 goto error;
6418 if (equal) {
6419 isl_space_free(space);
6420 return bmap;
6422 isl_space_free(isl_basic_map_take_space(bmap));
6423 bmap = isl_basic_map_restore_space(bmap, space);
6425 bmap = isl_basic_map_finalize(bmap);
6427 return bmap;
6428 error:
6429 isl_basic_map_free(bmap);
6430 isl_space_free(space);
6431 return NULL;
6434 __isl_give isl_basic_set *isl_basic_set_reset_space(
6435 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
6437 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
6438 space));
6441 /* Check that the total dimensions of "map" and "space" are the same.
6443 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
6444 __isl_keep isl_space *space)
6446 isl_size dim1, dim2;
6448 dim1 = isl_map_dim(map, isl_dim_all);
6449 dim2 = isl_space_dim(space, isl_dim_all);
6450 if (dim1 < 0 || dim2 < 0)
6451 return isl_stat_error;
6452 if (dim1 == dim2)
6453 return isl_stat_ok;
6454 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6455 "total dimensions do not match", return isl_stat_error);
6458 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
6459 __isl_take isl_space *space)
6461 int i;
6463 map = isl_map_cow(map);
6464 if (!map || !space)
6465 goto error;
6467 for (i = 0; i < map->n; ++i) {
6468 map->p[i] = isl_basic_map_reset_space(map->p[i],
6469 isl_space_copy(space));
6470 if (!map->p[i])
6471 goto error;
6473 isl_space_free(isl_map_take_space(map));
6474 map = isl_map_restore_space(map, space);
6476 return map;
6477 error:
6478 isl_map_free(map);
6479 isl_space_free(space);
6480 return NULL;
6483 /* Replace the space of "map" by "space", without modifying
6484 * the dimension of "map".
6486 * If the space of "map" is identical to "space" (including the identifiers
6487 * of the input and output dimensions), then simply return the original input.
6489 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
6490 __isl_take isl_space *space)
6492 isl_bool equal;
6493 isl_space *map_space;
6495 map_space = isl_map_peek_space(map);
6496 equal = isl_space_is_equal(map_space, space);
6497 if (equal >= 0 && equal)
6498 equal = isl_space_has_equal_ids(map_space, space);
6499 if (equal < 0)
6500 goto error;
6501 if (equal) {
6502 isl_space_free(space);
6503 return map;
6505 if (check_map_space_equal_total_dim(map, space) < 0)
6506 goto error;
6507 return isl_map_reset_space(map, space);
6508 error:
6509 isl_map_free(map);
6510 isl_space_free(space);
6511 return NULL;
6514 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
6515 __isl_take isl_space *space)
6517 return set_from_map(isl_map_reset_space(set_to_map(set), space));
6520 /* Compute the parameter domain of the given basic set.
6522 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
6524 isl_bool is_params;
6525 isl_space *space;
6526 isl_size n;
6528 is_params = isl_basic_set_is_params(bset);
6529 if (is_params < 0)
6530 return isl_basic_set_free(bset);
6531 if (is_params)
6532 return bset;
6534 n = isl_basic_set_dim(bset, isl_dim_set);
6535 if (n < 0)
6536 return isl_basic_set_free(bset);
6537 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6538 space = isl_basic_set_get_space(bset);
6539 space = isl_space_params(space);
6540 bset = isl_basic_set_reset_space(bset, space);
6541 return bset;
6544 /* Construct a zero-dimensional basic set with the given parameter domain.
6546 __isl_give isl_basic_set *isl_basic_set_from_params(
6547 __isl_take isl_basic_set *bset)
6549 isl_space *space;
6550 space = isl_basic_set_get_space(bset);
6551 space = isl_space_set_from_params(space);
6552 bset = isl_basic_set_reset_space(bset, space);
6553 return bset;
6556 /* Compute the parameter domain of the given set.
6558 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6560 return isl_map_params(set_to_map(set));
6563 /* Construct a zero-dimensional set with the given parameter domain.
6565 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6567 isl_space *space;
6568 space = isl_set_get_space(set);
6569 space = isl_space_set_from_params(space);
6570 set = isl_set_reset_space(set, space);
6571 return set;
6574 /* Compute the parameter domain of the given map.
6576 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6578 isl_space *space;
6579 isl_size n_in, n_out;
6581 n_in = isl_map_dim(map, isl_dim_in);
6582 n_out = isl_map_dim(map, isl_dim_out);
6583 if (n_in < 0 || n_out < 0)
6584 return isl_map_free(map);
6585 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6586 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6587 space = isl_map_get_space(map);
6588 space = isl_space_params(space);
6589 map = isl_map_reset_space(map, space);
6590 return map;
6593 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6595 isl_space *space;
6596 isl_size n_out;
6598 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6599 if (n_out < 0)
6600 return isl_basic_map_free(bmap);
6601 space = isl_space_domain(isl_basic_map_get_space(bmap));
6603 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6605 return isl_basic_map_reset_space(bmap, space);
6608 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6610 if (!bmap)
6611 return isl_bool_error;
6612 return isl_space_may_be_set(bmap->dim);
6615 /* Is this basic map actually a set?
6616 * Users should never call this function. Outside of isl,
6617 * the type should indicate whether something is a set or a map.
6619 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6621 if (!bmap)
6622 return isl_bool_error;
6623 return isl_space_is_set(bmap->dim);
6626 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6628 isl_bool is_set;
6630 is_set = isl_basic_map_is_set(bmap);
6631 if (is_set < 0)
6632 goto error;
6633 if (is_set)
6634 return bmap;
6635 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6636 error:
6637 isl_basic_map_free(bmap);
6638 return NULL;
6641 __isl_give isl_basic_map *isl_basic_map_domain_map(
6642 __isl_take isl_basic_map *bmap)
6644 int i;
6645 isl_space *space;
6646 isl_basic_map *domain;
6647 isl_size nparam, n_in, n_out;
6649 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6650 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6651 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6652 if (nparam < 0 || n_in < 0 || n_out < 0)
6653 return isl_basic_map_free(bmap);
6655 space = isl_basic_map_get_space(bmap);
6656 space = isl_space_from_range(isl_space_domain(space));
6657 domain = isl_basic_map_universe(space);
6659 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6660 bmap = isl_basic_map_apply_range(bmap, domain);
6661 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6663 for (i = 0; i < n_in; ++i)
6664 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6665 isl_dim_out, i);
6667 bmap = isl_basic_map_gauss(bmap, NULL);
6668 return isl_basic_map_finalize(bmap);
6671 __isl_give isl_basic_map *isl_basic_map_range_map(
6672 __isl_take isl_basic_map *bmap)
6674 int i;
6675 isl_space *space;
6676 isl_basic_map *range;
6677 isl_size nparam, n_in, n_out;
6679 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6680 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6681 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6682 if (nparam < 0 || n_in < 0 || n_out < 0)
6683 return isl_basic_map_free(bmap);
6685 space = isl_basic_map_get_space(bmap);
6686 space = isl_space_from_range(isl_space_range(space));
6687 range = isl_basic_map_universe(space);
6689 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6690 bmap = isl_basic_map_apply_range(bmap, range);
6691 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6693 for (i = 0; i < n_out; ++i)
6694 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6695 isl_dim_out, i);
6697 bmap = isl_basic_map_gauss(bmap, NULL);
6698 return isl_basic_map_finalize(bmap);
6701 int isl_map_may_be_set(__isl_keep isl_map *map)
6703 if (!map)
6704 return -1;
6705 return isl_space_may_be_set(map->dim);
6708 /* Is this map actually a set?
6709 * Users should never call this function. Outside of isl,
6710 * the type should indicate whether something is a set or a map.
6712 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6714 if (!map)
6715 return isl_bool_error;
6716 return isl_space_is_set(map->dim);
6719 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6721 isl_space *space;
6722 isl_size n_in;
6724 n_in = isl_map_dim(map, isl_dim_in);
6725 if (n_in < 0)
6726 return set_from_map(isl_map_free(map));
6727 space = isl_space_range(isl_map_get_space(map));
6729 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6731 return set_from_map(isl_map_reset_space(map, space));
6734 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6735 * to each of its basic maps.
6737 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6738 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6739 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6741 int i;
6742 isl_space *space;
6744 map = isl_map_cow(map);
6745 if (!map)
6746 return NULL;
6748 for (i = 0; i < map->n; ++i) {
6749 map->p[i] = fn_bmap(map->p[i]);
6750 if (!map->p[i])
6751 return isl_map_free(map);
6753 map = isl_map_unmark_normalized(map);
6755 space = isl_map_take_space(map);
6756 space = fn_space(space);
6757 map = isl_map_restore_space(map, space);
6759 return map;
6762 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6764 return isl_map_transform(map, &isl_space_domain_map,
6765 &isl_basic_map_domain_map);
6768 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6770 return isl_map_transform(map, &isl_space_range_map,
6771 &isl_basic_map_range_map);
6774 /* Given a wrapped map of the form A[B -> C],
6775 * return the map A[B -> C] -> B.
6777 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6779 isl_id *id;
6780 isl_map *map;
6782 if (!set)
6783 return NULL;
6784 if (!isl_set_has_tuple_id(set))
6785 return isl_map_domain_map(isl_set_unwrap(set));
6787 id = isl_set_get_tuple_id(set);
6788 map = isl_map_domain_map(isl_set_unwrap(set));
6789 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6791 return map;
6794 __isl_give isl_basic_map *isl_basic_map_from_domain(
6795 __isl_take isl_basic_set *bset)
6797 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6800 __isl_give isl_basic_map *isl_basic_map_from_range(
6801 __isl_take isl_basic_set *bset)
6803 isl_space *space;
6804 space = isl_basic_set_get_space(bset);
6805 space = isl_space_from_range(space);
6806 bset = isl_basic_set_reset_space(bset, space);
6807 return bset_to_bmap(bset);
6810 /* Create a relation with the given set as range.
6811 * The domain of the created relation is a zero-dimensional
6812 * flat anonymous space.
6814 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6816 isl_space *space;
6817 space = isl_set_get_space(set);
6818 space = isl_space_from_range(space);
6819 set = isl_set_reset_space(set, space);
6820 return set_to_map(set);
6823 /* Create a relation with the given set as domain.
6824 * The range of the created relation is a zero-dimensional
6825 * flat anonymous space.
6827 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6829 return isl_map_reverse(isl_map_from_range(set));
6832 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6833 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6835 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6838 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6839 __isl_take isl_set *range)
6841 return isl_map_apply_range(isl_map_reverse(domain), range);
6844 /* Return a newly allocated isl_map with given space and flags and
6845 * room for "n" basic maps.
6846 * Make sure that all cached information is cleared.
6848 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6849 unsigned flags)
6851 struct isl_map *map;
6853 if (!space)
6854 return NULL;
6855 if (n < 0)
6856 isl_die(space->ctx, isl_error_internal,
6857 "negative number of basic maps", goto error);
6858 map = isl_calloc(space->ctx, struct isl_map,
6859 sizeof(struct isl_map) +
6860 (n - 1) * sizeof(struct isl_basic_map *));
6861 if (!map)
6862 goto error;
6864 map->ctx = space->ctx;
6865 isl_ctx_ref(map->ctx);
6866 map->ref = 1;
6867 map->size = n;
6868 map->n = 0;
6869 map->dim = space;
6870 map->flags = flags;
6871 return map;
6872 error:
6873 isl_space_free(space);
6874 return NULL;
6877 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6879 struct isl_basic_map *bmap;
6880 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6881 bmap = isl_basic_map_set_to_empty(bmap);
6882 return bmap;
6885 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6887 struct isl_basic_set *bset;
6888 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6889 bset = isl_basic_set_set_to_empty(bset);
6890 return bset;
6893 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6895 struct isl_basic_map *bmap;
6896 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6897 bmap = isl_basic_map_finalize(bmap);
6898 return bmap;
6901 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6903 struct isl_basic_set *bset;
6904 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6905 bset = isl_basic_set_finalize(bset);
6906 return bset;
6909 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6910 __isl_take isl_space *space)
6912 int i;
6913 isl_size total = isl_space_dim(space, isl_dim_all);
6914 isl_basic_map *bmap;
6916 if (total < 0)
6917 space = isl_space_free(space);
6918 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6919 for (i = 0; i < total; ++i) {
6920 int k = isl_basic_map_alloc_inequality(bmap);
6921 if (k < 0)
6922 goto error;
6923 isl_seq_clr(bmap->ineq[k], 1 + total);
6924 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6926 return bmap;
6927 error:
6928 isl_basic_map_free(bmap);
6929 return NULL;
6932 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6933 __isl_take isl_space *space)
6935 return isl_basic_map_nat_universe(space);
6938 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6940 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6943 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6945 return isl_map_nat_universe(space);
6948 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6950 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6953 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6955 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6958 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6960 struct isl_map *map;
6961 if (!space)
6962 return NULL;
6963 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6964 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6965 return map;
6968 /* This function performs the same operation as isl_map_universe,
6969 * but is considered as a function on an isl_space when exported.
6971 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6973 return isl_map_universe(space);
6976 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6978 struct isl_set *set;
6979 if (!space)
6980 return NULL;
6981 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6982 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6983 return set;
6986 /* This function performs the same operation as isl_set_universe,
6987 * but is considered as a function on an isl_space when exported.
6989 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6991 return isl_set_universe(space);
6994 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6996 int i;
6997 struct isl_map *dup;
6999 if (!map)
7000 return NULL;
7001 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
7002 for (i = 0; i < map->n; ++i)
7003 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
7004 return dup;
7007 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
7008 __isl_take isl_basic_map *bmap)
7010 if (!bmap || !map)
7011 goto error;
7012 if (isl_basic_map_plain_is_empty(bmap)) {
7013 isl_basic_map_free(bmap);
7014 return map;
7016 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
7017 goto error;
7018 isl_assert(map->ctx, map->n < map->size, goto error);
7019 map->p[map->n] = bmap;
7020 map->n++;
7021 map = isl_map_unmark_normalized(map);
7022 return map;
7023 error:
7024 if (map)
7025 isl_map_free(map);
7026 if (bmap)
7027 isl_basic_map_free(bmap);
7028 return NULL;
7031 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
7033 int i;
7035 if (!map)
7036 return NULL;
7038 if (--map->ref > 0)
7039 return NULL;
7041 clear_caches(map);
7042 isl_ctx_deref(map->ctx);
7043 for (i = 0; i < map->n; ++i)
7044 isl_basic_map_free(map->p[i]);
7045 isl_space_free(map->dim);
7046 free(map);
7048 return NULL;
7051 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
7052 __isl_take isl_basic_map *bmap, unsigned pos, int value)
7054 int j;
7055 isl_size total;
7057 total = isl_basic_map_dim(bmap, isl_dim_all);
7058 if (total < 0)
7059 return isl_basic_map_free(bmap);
7061 bmap = isl_basic_map_cow(bmap);
7062 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
7063 j = isl_basic_map_alloc_equality(bmap);
7064 if (j < 0)
7065 goto error;
7066 isl_seq_clr(bmap->eq[j] + 1, total);
7067 isl_int_set_si(bmap->eq[j][pos], -1);
7068 isl_int_set_si(bmap->eq[j][0], value);
7069 bmap = isl_basic_map_simplify(bmap);
7070 return isl_basic_map_finalize(bmap);
7071 error:
7072 isl_basic_map_free(bmap);
7073 return NULL;
7076 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
7077 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
7079 int j;
7080 isl_size total;
7082 total = isl_basic_map_dim(bmap, isl_dim_all);
7083 if (total < 0)
7084 return isl_basic_map_free(bmap);
7086 bmap = isl_basic_map_cow(bmap);
7087 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
7088 j = isl_basic_map_alloc_equality(bmap);
7089 if (j < 0)
7090 goto error;
7091 isl_seq_clr(bmap->eq[j] + 1, total);
7092 isl_int_set_si(bmap->eq[j][pos], -1);
7093 isl_int_set(bmap->eq[j][0], value);
7094 bmap = isl_basic_map_simplify(bmap);
7095 return isl_basic_map_finalize(bmap);
7096 error:
7097 isl_basic_map_free(bmap);
7098 return NULL;
7101 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
7102 enum isl_dim_type type, unsigned pos, int value)
7104 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7105 return isl_basic_map_free(bmap);
7106 return isl_basic_map_fix_pos_si(bmap,
7107 isl_basic_map_offset(bmap, type) + pos, value);
7110 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
7111 enum isl_dim_type type, unsigned pos, isl_int value)
7113 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7114 return isl_basic_map_free(bmap);
7115 return isl_basic_map_fix_pos(bmap,
7116 isl_basic_map_offset(bmap, type) + pos, value);
7119 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
7120 * to be equal to "v".
7122 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
7123 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7125 if (!bmap || !v)
7126 goto error;
7127 if (!isl_val_is_int(v))
7128 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
7129 "expecting integer value", goto error);
7130 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7131 goto error;
7132 pos += isl_basic_map_offset(bmap, type);
7133 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
7134 isl_val_free(v);
7135 return bmap;
7136 error:
7137 isl_basic_map_free(bmap);
7138 isl_val_free(v);
7139 return NULL;
7142 /* Fix the value of the variable at position "pos" of type "type" of "bset"
7143 * to be equal to "v".
7145 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
7146 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7148 return isl_basic_map_fix_val(bset, type, pos, v);
7151 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
7152 enum isl_dim_type type, unsigned pos, int value)
7154 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
7155 type, pos, value));
7158 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
7159 enum isl_dim_type type, unsigned pos, isl_int value)
7161 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
7162 type, pos, value));
7165 /* Remove the basic map at position "i" from "map" if this basic map
7166 * is (obviously) empty.
7168 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
7170 isl_bool empty;
7172 if (!map)
7173 return NULL;
7175 empty = isl_basic_map_plain_is_empty(map->p[i]);
7176 if (empty < 0)
7177 return isl_map_free(map);
7178 if (!empty)
7179 return map;
7181 isl_basic_map_free(map->p[i]);
7182 map->n--;
7183 if (i != map->n) {
7184 map->p[i] = map->p[map->n];
7185 map = isl_map_unmark_normalized(map);
7189 return map;
7192 /* Perform "fn" on each basic map of "map", where we may not be holding
7193 * the only reference to "map".
7194 * In particular, "fn" should be a semantics preserving operation
7195 * that we want to apply to all copies of "map". We therefore need
7196 * to be careful not to modify "map" in a way that breaks "map"
7197 * in case anything goes wrong.
7199 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
7200 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
7202 struct isl_basic_map *bmap;
7203 int i;
7205 if (!map)
7206 return NULL;
7208 for (i = map->n - 1; i >= 0; --i) {
7209 bmap = isl_basic_map_copy(map->p[i]);
7210 bmap = fn(bmap);
7211 if (!bmap)
7212 goto error;
7213 isl_basic_map_free(map->p[i]);
7214 map->p[i] = bmap;
7215 map = remove_if_empty(map, i);
7216 if (!map)
7217 return NULL;
7220 return map;
7221 error:
7222 isl_map_free(map);
7223 return NULL;
7226 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
7227 enum isl_dim_type type, unsigned pos, int value)
7229 int i;
7231 map = isl_map_cow(map);
7232 if (isl_map_check_range(map, type, pos, 1) < 0)
7233 return isl_map_free(map);
7234 for (i = map->n - 1; i >= 0; --i) {
7235 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
7236 map = remove_if_empty(map, i);
7237 if (!map)
7238 return NULL;
7240 map = isl_map_unmark_normalized(map);
7241 return map;
7244 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
7245 enum isl_dim_type type, unsigned pos, int value)
7247 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
7250 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
7251 enum isl_dim_type type, unsigned pos, isl_int value)
7253 int i;
7255 map = isl_map_cow(map);
7256 if (isl_map_check_range(map, type, pos, 1) < 0)
7257 return isl_map_free(map);
7258 for (i = 0; i < map->n; ++i) {
7259 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
7260 if (!map->p[i])
7261 goto error;
7263 map = isl_map_unmark_normalized(map);
7264 return map;
7265 error:
7266 isl_map_free(map);
7267 return NULL;
7270 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
7271 enum isl_dim_type type, unsigned pos, isl_int value)
7273 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
7276 /* Fix the value of the variable at position "pos" of type "type" of "map"
7277 * to be equal to "v".
7279 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
7280 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7282 int i;
7284 map = isl_map_cow(map);
7285 if (!map || !v)
7286 goto error;
7288 if (!isl_val_is_int(v))
7289 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7290 "expecting integer value", goto error);
7291 if (isl_map_check_range(map, type, pos, 1) < 0)
7292 goto error;
7293 for (i = map->n - 1; i >= 0; --i) {
7294 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
7295 isl_val_copy(v));
7296 map = remove_if_empty(map, i);
7297 if (!map)
7298 goto error;
7300 map = isl_map_unmark_normalized(map);
7301 isl_val_free(v);
7302 return map;
7303 error:
7304 isl_map_free(map);
7305 isl_val_free(v);
7306 return NULL;
7309 /* Fix the value of the variable at position "pos" of type "type" of "set"
7310 * to be equal to "v".
7312 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
7313 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7315 return isl_map_fix_val(set, type, pos, v);
7318 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
7319 unsigned input, int value)
7321 return isl_map_fix_si(map, isl_dim_in, input, value);
7324 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
7325 int value)
7327 return set_from_map(isl_map_fix_si(set_to_map(set),
7328 isl_dim_set, dim, value));
7331 static __isl_give isl_basic_map *basic_map_bound_si(
7332 __isl_take isl_basic_map *bmap,
7333 enum isl_dim_type type, unsigned pos, int value, int upper)
7335 int j;
7336 isl_size total;
7338 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7339 return isl_basic_map_free(bmap);
7340 total = isl_basic_map_dim(bmap, isl_dim_all);
7341 if (total < 0)
7342 return isl_basic_map_free(bmap);
7343 pos += isl_basic_map_offset(bmap, type);
7344 bmap = isl_basic_map_cow(bmap);
7345 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
7346 j = isl_basic_map_alloc_inequality(bmap);
7347 if (j < 0)
7348 goto error;
7349 isl_seq_clr(bmap->ineq[j], 1 + total);
7350 if (upper) {
7351 isl_int_set_si(bmap->ineq[j][pos], -1);
7352 isl_int_set_si(bmap->ineq[j][0], value);
7353 } else {
7354 isl_int_set_si(bmap->ineq[j][pos], 1);
7355 isl_int_set_si(bmap->ineq[j][0], -value);
7357 bmap = isl_basic_map_simplify(bmap);
7358 return isl_basic_map_finalize(bmap);
7359 error:
7360 isl_basic_map_free(bmap);
7361 return NULL;
7364 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
7365 __isl_take isl_basic_map *bmap,
7366 enum isl_dim_type type, unsigned pos, int value)
7368 return basic_map_bound_si(bmap, type, pos, value, 0);
7371 /* Constrain the values of the given dimension to be no greater than "value".
7373 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
7374 __isl_take isl_basic_map *bmap,
7375 enum isl_dim_type type, unsigned pos, int value)
7377 return basic_map_bound_si(bmap, type, pos, value, 1);
7380 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
7381 enum isl_dim_type type, unsigned pos, int value, int upper)
7383 int i;
7385 map = isl_map_cow(map);
7386 if (isl_map_check_range(map, type, pos, 1) < 0)
7387 return isl_map_free(map);
7388 for (i = 0; i < map->n; ++i) {
7389 map->p[i] = basic_map_bound_si(map->p[i],
7390 type, pos, value, upper);
7391 if (!map->p[i])
7392 goto error;
7394 map = isl_map_unmark_normalized(map);
7395 return map;
7396 error:
7397 isl_map_free(map);
7398 return NULL;
7401 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
7402 enum isl_dim_type type, unsigned pos, int value)
7404 return map_bound_si(map, type, pos, value, 0);
7407 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
7408 enum isl_dim_type type, unsigned pos, int value)
7410 return map_bound_si(map, type, pos, value, 1);
7413 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
7414 enum isl_dim_type type, unsigned pos, int value)
7416 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
7417 type, pos, value));
7420 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
7421 enum isl_dim_type type, unsigned pos, int value)
7423 return isl_map_upper_bound_si(set, type, pos, value);
7426 /* Bound the given variable of "bmap" from below (or above is "upper"
7427 * is set) to "value".
7429 static __isl_give isl_basic_map *basic_map_bound(
7430 __isl_take isl_basic_map *bmap,
7431 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7433 int j;
7434 isl_size total;
7436 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7437 return isl_basic_map_free(bmap);
7438 total = isl_basic_map_dim(bmap, isl_dim_all);
7439 if (total < 0)
7440 return isl_basic_map_free(bmap);
7441 pos += isl_basic_map_offset(bmap, type);
7442 bmap = isl_basic_map_cow(bmap);
7443 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
7444 j = isl_basic_map_alloc_inequality(bmap);
7445 if (j < 0)
7446 goto error;
7447 isl_seq_clr(bmap->ineq[j], 1 + total);
7448 if (upper) {
7449 isl_int_set_si(bmap->ineq[j][pos], -1);
7450 isl_int_set(bmap->ineq[j][0], value);
7451 } else {
7452 isl_int_set_si(bmap->ineq[j][pos], 1);
7453 isl_int_neg(bmap->ineq[j][0], value);
7455 bmap = isl_basic_map_simplify(bmap);
7456 return isl_basic_map_finalize(bmap);
7457 error:
7458 isl_basic_map_free(bmap);
7459 return NULL;
7462 /* Bound the given variable of "map" from below (or above is "upper"
7463 * is set) to "value".
7465 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
7466 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7468 int i;
7470 map = isl_map_cow(map);
7471 if (isl_map_check_range(map, type, pos, 1) < 0)
7472 return isl_map_free(map);
7473 for (i = map->n - 1; i >= 0; --i) {
7474 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
7475 map = remove_if_empty(map, i);
7476 if (!map)
7477 return NULL;
7479 map = isl_map_unmark_normalized(map);
7480 return map;
7483 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
7484 enum isl_dim_type type, unsigned pos, isl_int value)
7486 return map_bound(map, type, pos, value, 0);
7489 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
7490 enum isl_dim_type type, unsigned pos, isl_int value)
7492 return map_bound(map, type, pos, value, 1);
7495 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
7496 enum isl_dim_type type, unsigned pos, isl_int value)
7498 return isl_map_lower_bound(set, type, pos, value);
7501 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
7502 enum isl_dim_type type, unsigned pos, isl_int value)
7504 return isl_map_upper_bound(set, type, pos, value);
7507 /* Force the values of the variable at position "pos" of type "type" of "map"
7508 * to be no smaller than "value".
7510 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
7511 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7513 if (!value)
7514 goto error;
7515 if (!isl_val_is_int(value))
7516 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7517 "expecting integer value", goto error);
7518 map = isl_map_lower_bound(map, type, pos, value->n);
7519 isl_val_free(value);
7520 return map;
7521 error:
7522 isl_val_free(value);
7523 isl_map_free(map);
7524 return NULL;
7527 /* Force the values of the variable at position "pos" of type "type" of "set"
7528 * to be no smaller than "value".
7530 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
7531 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7533 isl_map *map;
7535 map = set_to_map(set);
7536 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
7539 /* Force the values of the variable at position "pos" of type "type" of "map"
7540 * to be no greater than "value".
7542 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
7543 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7545 if (!value)
7546 goto error;
7547 if (!isl_val_is_int(value))
7548 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7549 "expecting integer value", goto error);
7550 map = isl_map_upper_bound(map, type, pos, value->n);
7551 isl_val_free(value);
7552 return map;
7553 error:
7554 isl_val_free(value);
7555 isl_map_free(map);
7556 return NULL;
7559 /* Force the values of the variable at position "pos" of type "type" of "set"
7560 * to be no greater than "value".
7562 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7563 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7565 isl_map *map;
7567 map = set_to_map(set);
7568 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7571 /* If "mv" has an explicit domain, then intersect the domain of "map"
7572 * with this explicit domain.
7574 * An isl_multi_val object never has an explicit domain,
7575 * so simply return "map".
7577 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7578 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7580 return map;
7583 #undef BASE
7584 #define BASE val
7585 #include "isl_map_bound_templ.c"
7587 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7588 * for each set dimension, by treating the set as a map.
7590 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7591 __isl_take isl_multi_val *bound,
7592 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7593 unsigned pos, __isl_take isl_val *value))
7595 isl_map *map;
7597 map = set_to_map(set);
7598 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7601 #undef BASE
7602 #define BASE pw_aff
7603 #include "isl_map_bound_templ.c"
7605 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7606 * for each set dimension, by converting the set and the bound
7607 * to objects living in a map space.
7609 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7610 __isl_take isl_multi_pw_aff *bound,
7611 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7612 unsigned pos, __isl_take TYPE *value))
7614 isl_map *map;
7616 map = isl_map_from_range(set);
7617 bound = isl_multi_pw_aff_from_range(bound);
7618 map = map_bound_multi_pw_aff(map, bound, set_bound);
7619 return isl_map_range(map);
7622 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7623 * setting a bound on the given output dimension.
7625 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7626 unsigned pos, __isl_take isl_val *v)
7628 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7631 /* Force the values of the set dimensions of "set"
7632 * to be no smaller than the corresponding values in "lower".
7634 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7635 __isl_take isl_multi_val *lower)
7637 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7640 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7641 * setting a bound on the given output dimension.
7643 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7644 unsigned pos, __isl_take isl_val *v)
7646 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7649 /* Force the values of the set dimensions of "set"
7650 * to be no greater than the corresponding values in "upper".
7652 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7653 __isl_take isl_multi_val *upper)
7655 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7658 /* Force the symbolic constant expression "bound"
7659 * to satisfy the relation "order" with respect to
7660 * the output variable at position "pos" of "map".
7662 * Create an affine expression representing the output variable
7663 * in terms of the range and
7664 * compare it using "order" to "bound" (defined on the domain).
7665 * The result is a relation between elements in domain and range that
7666 * can be intersected with "map".
7668 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7669 unsigned pos, __isl_take isl_pw_aff *bound,
7670 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7671 __isl_take isl_pw_aff *pa2))
7673 isl_space *space;
7674 isl_local_space *ls;
7675 isl_pw_aff *var;
7677 space = isl_space_range(isl_map_get_space(map));
7678 ls = isl_local_space_from_space(space);
7679 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7680 map = isl_map_intersect(map, order(bound, var));
7681 return map;
7684 /* Force the values of the output variable at position "pos" of "map"
7685 * to be no smaller than the symbolic constant expression "lower".
7687 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7688 unsigned pos, __isl_take isl_pw_aff *lower)
7690 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7693 /* Force the values of the output variable at position "pos" of "map"
7694 * to be no greater than the symbolic constant expression "upper".
7696 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7697 unsigned pos, __isl_take isl_pw_aff *upper)
7699 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7702 /* Force the values of the set dimensions of "set"
7703 * to be no smaller than the corresponding constant symbolic expressions
7704 * in "lower".
7706 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7707 __isl_take isl_multi_pw_aff *lower)
7709 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7712 /* Force the values of the set dimensions of "set"
7713 * to be no greater than the corresponding constant symbolic expressions
7714 * in "upper".
7716 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7717 __isl_take isl_multi_pw_aff *upper)
7719 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7722 /* Force the values of the output dimensions of "map"
7723 * to be no smaller than the corresponding constant symbolic expressions
7724 * in "lower".
7726 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7727 __isl_take isl_multi_pw_aff *lower)
7729 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7732 /* Force the values of the output dimensions of "map"
7733 * to be no greater than the corresponding constant symbolic expressions
7734 * in "upper".
7736 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7737 __isl_take isl_multi_pw_aff *upper)
7739 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7742 /* Bound the given variable of "bset" from below (or above is "upper"
7743 * is set) to "value".
7745 static __isl_give isl_basic_set *isl_basic_set_bound(
7746 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7747 isl_int value, int upper)
7749 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7750 type, pos, value, upper));
7753 /* Bound the given variable of "bset" from below (or above is "upper"
7754 * is set) to "value".
7756 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7757 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7758 __isl_take isl_val *value, int upper)
7760 if (!value)
7761 goto error;
7762 if (!isl_val_is_int(value))
7763 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7764 "expecting integer value", goto error);
7765 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7766 isl_val_free(value);
7767 return bset;
7768 error:
7769 isl_val_free(value);
7770 isl_basic_set_free(bset);
7771 return NULL;
7774 /* Bound the given variable of "bset" from below to "value".
7776 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7777 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7778 __isl_take isl_val *value)
7780 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7783 /* Bound the given variable of "bset" from above to "value".
7785 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7786 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7787 __isl_take isl_val *value)
7789 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7792 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7794 return isl_map_transform(map, &isl_space_reverse,
7795 &isl_basic_map_reverse);
7798 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7800 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7802 return isl_map_transform(map, &isl_space_domain_reverse,
7803 &isl_basic_map_domain_reverse);
7806 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7808 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7810 return isl_map_transform(map, &isl_space_range_reverse,
7811 &isl_basic_map_range_reverse);
7814 /* Given a set (A -> B), return the corresponding set (B -> A).
7816 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7818 isl_map *map = set_to_map(set);
7820 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7821 &isl_basic_map_set_reverse);
7822 return set_from_map(map);
7825 #undef TYPE
7826 #define TYPE isl_pw_multi_aff
7827 #undef SUFFIX
7828 #define SUFFIX _pw_multi_aff
7829 #undef EMPTY
7830 #define EMPTY isl_pw_multi_aff_empty
7831 #undef ADD
7832 #define ADD isl_pw_multi_aff_union_add
7833 #include "isl_map_lexopt_templ.c"
7835 /* Given a map "map", compute the lexicographically minimal
7836 * (or maximal) image element for each domain element in dom,
7837 * in the form of an isl_pw_multi_aff.
7838 * If "empty" is not NULL, then set *empty to those elements in dom that
7839 * do not have an image element.
7840 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7841 * should be computed over the domain of "map". "empty" is also NULL
7842 * in this case.
7844 * We first compute the lexicographically minimal or maximal element
7845 * in the first basic map. This results in a partial solution "res"
7846 * and a subset "todo" of dom that still need to be handled.
7847 * We then consider each of the remaining maps in "map" and successively
7848 * update both "res" and "todo".
7849 * If "empty" is NULL, then the todo sets are not needed and therefore
7850 * also not computed.
7852 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7853 __isl_take isl_map *map, __isl_take isl_set *dom,
7854 __isl_give isl_set **empty, unsigned flags)
7856 int i;
7857 int full;
7858 isl_pw_multi_aff *res;
7859 isl_set *todo;
7861 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7862 if (!map || (!full && !dom))
7863 goto error;
7865 if (isl_map_plain_is_empty(map)) {
7866 if (empty)
7867 *empty = dom;
7868 else
7869 isl_set_free(dom);
7870 return isl_pw_multi_aff_from_map(map);
7873 res = basic_map_partial_lexopt_pw_multi_aff(
7874 isl_basic_map_copy(map->p[0]),
7875 isl_set_copy(dom), empty, flags);
7877 if (empty)
7878 todo = *empty;
7879 for (i = 1; i < map->n; ++i) {
7880 isl_pw_multi_aff *res_i;
7882 res_i = basic_map_partial_lexopt_pw_multi_aff(
7883 isl_basic_map_copy(map->p[i]),
7884 isl_set_copy(dom), empty, flags);
7886 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7887 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7888 else
7889 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7891 if (empty)
7892 todo = isl_set_intersect(todo, *empty);
7895 isl_set_free(dom);
7896 isl_map_free(map);
7898 if (empty)
7899 *empty = todo;
7901 return res;
7902 error:
7903 if (empty)
7904 *empty = NULL;
7905 isl_set_free(dom);
7906 isl_map_free(map);
7907 return NULL;
7910 #undef TYPE
7911 #define TYPE isl_map
7912 #undef SUFFIX
7913 #define SUFFIX
7914 #undef EMPTY
7915 #define EMPTY isl_map_empty
7916 #undef ADD
7917 #define ADD isl_map_union_disjoint
7918 #include "isl_map_lexopt_templ.c"
7920 /* Given a map "map", compute the lexicographically minimal
7921 * (or maximal) image element for each domain element in "dom",
7922 * in the form of an isl_map.
7923 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7924 * do not have an image element.
7925 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7926 * should be computed over the domain of "map". "empty" is also NULL
7927 * in this case.
7929 * If the input consists of more than one disjunct, then first
7930 * compute the desired result in the form of an isl_pw_multi_aff and
7931 * then convert that into an isl_map.
7933 * This function used to have an explicit implementation in terms
7934 * of isl_maps, but it would continually intersect the domains of
7935 * partial results with the complement of the domain of the next
7936 * partial solution, potentially leading to an explosion in the number
7937 * of disjuncts if there are several disjuncts in the input.
7938 * An even earlier implementation of this function would look for
7939 * better results in the domain of the partial result and for extra
7940 * results in the complement of this domain, which would lead to
7941 * even more splintering.
7943 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7944 __isl_take isl_map *map, __isl_take isl_set *dom,
7945 __isl_give isl_set **empty, unsigned flags)
7947 int full;
7948 struct isl_map *res;
7949 isl_pw_multi_aff *pma;
7951 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7952 if (!map || (!full && !dom))
7953 goto error;
7955 if (isl_map_plain_is_empty(map)) {
7956 if (empty)
7957 *empty = dom;
7958 else
7959 isl_set_free(dom);
7960 return map;
7963 if (map->n == 1) {
7964 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7965 dom, empty, flags);
7966 isl_map_free(map);
7967 return res;
7970 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7971 flags);
7972 return isl_map_from_pw_multi_aff_internal(pma);
7973 error:
7974 if (empty)
7975 *empty = NULL;
7976 isl_set_free(dom);
7977 isl_map_free(map);
7978 return NULL;
7981 __isl_give isl_map *isl_map_partial_lexmax(
7982 __isl_take isl_map *map, __isl_take isl_set *dom,
7983 __isl_give isl_set **empty)
7985 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7988 __isl_give isl_map *isl_map_partial_lexmin(
7989 __isl_take isl_map *map, __isl_take isl_set *dom,
7990 __isl_give isl_set **empty)
7992 return isl_map_partial_lexopt(map, dom, empty, 0);
7995 __isl_give isl_set *isl_set_partial_lexmin(
7996 __isl_take isl_set *set, __isl_take isl_set *dom,
7997 __isl_give isl_set **empty)
7999 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
8000 dom, empty));
8003 __isl_give isl_set *isl_set_partial_lexmax(
8004 __isl_take isl_set *set, __isl_take isl_set *dom,
8005 __isl_give isl_set **empty)
8007 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
8008 dom, empty));
8011 /* Compute the lexicographic minimum (or maximum if "flags" includes
8012 * ISL_OPT_MAX) of "bset" over its parametric domain.
8014 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
8015 unsigned flags)
8017 return isl_basic_map_lexopt(bset, flags);
8020 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
8022 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
8025 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
8027 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
8030 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
8032 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
8035 /* Compute the lexicographic minimum of "bset" over its parametric domain
8036 * for the purpose of quantifier elimination.
8037 * That is, find an explicit representation for all the existentially
8038 * quantified variables in "bset" by computing their lexicographic
8039 * minimum.
8041 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
8042 __isl_take isl_basic_set *bset)
8044 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
8047 /* Given a basic map with one output dimension, compute the minimum or
8048 * maximum of that dimension as an isl_pw_aff.
8050 * Compute the optimum as a lexicographic optimum over the single
8051 * output dimension and extract the single isl_pw_aff from the result.
8053 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
8054 int max)
8056 isl_pw_multi_aff *pma;
8057 isl_pw_aff *pwaff;
8059 bmap = isl_basic_map_copy(bmap);
8060 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
8061 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
8062 isl_pw_multi_aff_free(pma);
8064 return pwaff;
8067 /* Compute the minimum or maximum of the given output dimension
8068 * as a function of the parameters and the input dimensions,
8069 * but independently of the other output dimensions.
8071 * We first project out the other output dimension and then compute
8072 * the "lexicographic" maximum in each basic map, combining the results
8073 * using isl_pw_aff_union_max.
8075 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
8076 int max)
8078 int i;
8079 isl_pw_aff *pwaff;
8080 isl_size n_out;
8082 n_out = isl_map_dim(map, isl_dim_out);
8083 if (n_out < 0)
8084 map = isl_map_free(map);
8085 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
8086 map = isl_map_project_out(map, isl_dim_out, 0, pos);
8087 if (!map)
8088 return NULL;
8090 if (map->n == 0) {
8091 isl_space *space = isl_map_get_space(map);
8092 isl_map_free(map);
8093 return isl_pw_aff_empty(space);
8096 pwaff = basic_map_dim_opt(map->p[0], max);
8097 for (i = 1; i < map->n; ++i) {
8098 isl_pw_aff *pwaff_i;
8100 pwaff_i = basic_map_dim_opt(map->p[i], max);
8101 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
8104 isl_map_free(map);
8106 return pwaff;
8109 /* Compute the minimum of the given output dimension as a function of the
8110 * parameters and input dimensions, but independently of
8111 * the other output dimensions.
8113 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
8115 return map_dim_opt(map, pos, 0);
8118 /* Compute the maximum of the given output dimension as a function of the
8119 * parameters and input dimensions, but independently of
8120 * the other output dimensions.
8122 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
8124 return map_dim_opt(map, pos, 1);
8127 /* Compute the minimum or maximum of the given set dimension
8128 * as a function of the parameters,
8129 * but independently of the other set dimensions.
8131 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
8132 int max)
8134 return map_dim_opt(set, pos, max);
8137 /* Compute the maximum of the given set dimension as a function of the
8138 * parameters, but independently of the other set dimensions.
8140 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
8142 return set_dim_opt(set, pos, 1);
8145 /* Compute the minimum of the given set dimension as a function of the
8146 * parameters, but independently of the other set dimensions.
8148 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
8150 return set_dim_opt(set, pos, 0);
8153 /* Apply a preimage specified by "mat" on the parameters of "bset".
8154 * bset is assumed to have only parameters and divs.
8156 static __isl_give isl_basic_set *basic_set_parameter_preimage(
8157 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
8159 isl_size nparam;
8161 nparam = isl_basic_set_dim(bset, isl_dim_param);
8162 if (nparam < 0 || !mat)
8163 goto error;
8165 bset->dim = isl_space_cow(bset->dim);
8166 if (!bset->dim)
8167 goto error;
8169 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
8171 bset->dim->nparam = 0;
8172 bset->dim->n_out = nparam;
8173 bset = isl_basic_set_preimage(bset, mat);
8174 if (bset) {
8175 bset->dim->nparam = bset->dim->n_out;
8176 bset->dim->n_out = 0;
8178 return bset;
8179 error:
8180 isl_mat_free(mat);
8181 isl_basic_set_free(bset);
8182 return NULL;
8185 /* Apply a preimage specified by "mat" on the parameters of "set".
8186 * set is assumed to have only parameters and divs.
8188 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
8189 __isl_take isl_mat *mat)
8191 isl_space *space;
8192 isl_size nparam;
8194 nparam = isl_set_dim(set, isl_dim_param);
8195 if (nparam < 0 || !mat)
8196 goto error;
8198 if (mat->n_row != 1 + nparam)
8199 isl_die(isl_set_get_ctx(set), isl_error_internal,
8200 "unexpected number of rows", goto error);
8202 space = isl_set_get_space(set);
8203 space = isl_space_move_dims(space, isl_dim_set, 0,
8204 isl_dim_param, 0, nparam);
8205 set = isl_set_reset_space(set, space);
8206 set = isl_set_preimage(set, mat);
8207 nparam = isl_set_dim(set, isl_dim_out);
8208 if (nparam < 0)
8209 set = isl_set_free(set);
8210 space = isl_set_get_space(set);
8211 space = isl_space_move_dims(space, isl_dim_param, 0,
8212 isl_dim_out, 0, nparam);
8213 set = isl_set_reset_space(set, space);
8214 return set;
8215 error:
8216 isl_mat_free(mat);
8217 isl_set_free(set);
8218 return NULL;
8221 /* Intersect the basic set "bset" with the affine space specified by the
8222 * equalities in "eq".
8224 static __isl_give isl_basic_set *basic_set_append_equalities(
8225 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
8227 int i, k;
8228 unsigned len;
8230 if (!bset || !eq)
8231 goto error;
8233 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
8234 if (!bset)
8235 goto error;
8237 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
8238 for (i = 0; i < eq->n_row; ++i) {
8239 k = isl_basic_set_alloc_equality(bset);
8240 if (k < 0)
8241 goto error;
8242 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
8243 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
8245 isl_mat_free(eq);
8247 bset = isl_basic_set_gauss(bset, NULL);
8248 bset = isl_basic_set_finalize(bset);
8250 return bset;
8251 error:
8252 isl_mat_free(eq);
8253 isl_basic_set_free(bset);
8254 return NULL;
8257 /* Intersect the set "set" with the affine space specified by the
8258 * equalities in "eq".
8260 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
8261 __isl_take isl_mat *eq)
8263 int i;
8265 if (!set || !eq)
8266 goto error;
8268 for (i = 0; i < set->n; ++i) {
8269 set->p[i] = basic_set_append_equalities(set->p[i],
8270 isl_mat_copy(eq));
8271 if (!set->p[i])
8272 goto error;
8274 isl_mat_free(eq);
8275 return set;
8276 error:
8277 isl_mat_free(eq);
8278 isl_set_free(set);
8279 return NULL;
8282 /* Given a basic set "bset" that only involves parameters and existentially
8283 * quantified variables, return the index of the first equality
8284 * that only involves parameters. If there is no such equality then
8285 * return bset->n_eq.
8287 * This function assumes that isl_basic_set_gauss has been called on "bset".
8289 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
8291 int i, j;
8292 isl_size nparam, n_div;
8294 nparam = isl_basic_set_dim(bset, isl_dim_param);
8295 n_div = isl_basic_set_dim(bset, isl_dim_div);
8296 if (nparam < 0 || n_div < 0)
8297 return -1;
8299 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
8300 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
8301 ++i;
8304 return i;
8307 /* Compute an explicit representation for the existentially quantified
8308 * variables in "bset" by computing the "minimal value" of the set
8309 * variables. Since there are no set variables, the computation of
8310 * the minimal value essentially computes an explicit representation
8311 * of the non-empty part(s) of "bset".
8313 * The input only involves parameters and existentially quantified variables.
8314 * All equalities among parameters have been removed.
8316 * Since the existentially quantified variables in the result are in general
8317 * going to be different from those in the input, we first replace
8318 * them by the minimal number of variables based on their equalities.
8319 * This should simplify the parametric integer programming.
8321 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
8323 isl_morph *morph1, *morph2;
8324 isl_set *set;
8325 isl_size n;
8327 if (!bset)
8328 return NULL;
8329 if (bset->n_eq == 0)
8330 return isl_basic_set_lexmin_compute_divs(bset);
8332 morph1 = isl_basic_set_parameter_compression(bset);
8333 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
8334 bset = isl_basic_set_lift(bset);
8335 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
8336 bset = isl_morph_basic_set(morph2, bset);
8337 n = isl_basic_set_dim(bset, isl_dim_set);
8338 if (n < 0)
8339 bset = isl_basic_set_free(bset);
8340 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
8342 set = isl_basic_set_lexmin_compute_divs(bset);
8344 set = isl_morph_set(isl_morph_inverse(morph1), set);
8346 return set;
8349 /* Project the given basic set onto its parameter domain, possibly introducing
8350 * new, explicit, existential variables in the constraints.
8351 * The input has parameters and (possibly implicit) existential variables.
8352 * The output has the same parameters, but only
8353 * explicit existentially quantified variables.
8355 * The actual projection is performed by pip, but pip doesn't seem
8356 * to like equalities very much, so we first remove the equalities
8357 * among the parameters by performing a variable compression on
8358 * the parameters. Afterward, an inverse transformation is performed
8359 * and the equalities among the parameters are inserted back in.
8361 * The variable compression on the parameters may uncover additional
8362 * equalities that were only implicit before. We therefore check
8363 * if there are any new parameter equalities in the result and
8364 * if so recurse. The removal of parameter equalities is required
8365 * for the parameter compression performed by base_compute_divs.
8367 static __isl_give isl_set *parameter_compute_divs(
8368 __isl_take isl_basic_set *bset)
8370 int i;
8371 struct isl_mat *eq;
8372 struct isl_mat *T, *T2;
8373 struct isl_set *set;
8374 isl_size nparam;
8376 bset = isl_basic_set_cow(bset);
8377 if (!bset)
8378 return NULL;
8380 if (bset->n_eq == 0)
8381 return base_compute_divs(bset);
8383 bset = isl_basic_set_gauss(bset, NULL);
8384 if (!bset)
8385 return NULL;
8386 if (isl_basic_set_plain_is_empty(bset))
8387 return isl_set_from_basic_set(bset);
8389 i = first_parameter_equality(bset);
8390 if (i == bset->n_eq)
8391 return base_compute_divs(bset);
8393 nparam = isl_basic_set_dim(bset, isl_dim_param);
8394 if (nparam < 0)
8395 return isl_set_from_basic_set(isl_basic_set_free(bset));
8396 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
8397 0, 1 + nparam);
8398 eq = isl_mat_cow(eq);
8399 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
8400 if (T && T->n_col == 0) {
8401 isl_mat_free(T);
8402 isl_mat_free(T2);
8403 isl_mat_free(eq);
8404 bset = isl_basic_set_set_to_empty(bset);
8405 return isl_set_from_basic_set(bset);
8407 bset = basic_set_parameter_preimage(bset, T);
8409 i = first_parameter_equality(bset);
8410 if (!bset)
8411 set = NULL;
8412 else if (i == bset->n_eq)
8413 set = base_compute_divs(bset);
8414 else
8415 set = parameter_compute_divs(bset);
8416 set = set_parameter_preimage(set, T2);
8417 set = set_append_equalities(set, eq);
8418 return set;
8421 /* Insert the divs from "ls" before those of "bmap".
8423 * The number of columns is not changed, which means that the last
8424 * dimensions of "bmap" are being reintepreted as the divs from "ls".
8425 * The caller is responsible for removing the same number of dimensions
8426 * from the space of "bmap".
8428 static __isl_give isl_basic_map *insert_divs_from_local_space(
8429 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
8431 int i;
8432 isl_size n_div;
8433 int old_n_div;
8435 n_div = isl_local_space_dim(ls, isl_dim_div);
8436 if (n_div < 0)
8437 return isl_basic_map_free(bmap);
8438 if (n_div == 0)
8439 return bmap;
8441 old_n_div = bmap->n_div;
8442 bmap = insert_div_rows(bmap, n_div);
8443 if (!bmap)
8444 return NULL;
8446 for (i = 0; i < n_div; ++i) {
8447 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
8448 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
8451 return bmap;
8454 /* Replace the space of "bmap" by the space and divs of "ls".
8456 * If "ls" has any divs, then we simplify the result since we may
8457 * have discovered some additional equalities that could simplify
8458 * the div expressions.
8460 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
8461 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
8463 isl_size n_div;
8465 bmap = isl_basic_map_cow(bmap);
8466 n_div = isl_local_space_dim(ls, isl_dim_div);
8467 if (!bmap || n_div < 0)
8468 goto error;
8470 bmap = insert_divs_from_local_space(bmap, ls);
8471 if (!bmap)
8472 goto error;
8474 isl_space_free(bmap->dim);
8475 bmap->dim = isl_local_space_get_space(ls);
8476 if (!bmap->dim)
8477 goto error;
8479 isl_local_space_free(ls);
8480 if (n_div > 0)
8481 bmap = isl_basic_map_simplify(bmap);
8482 bmap = isl_basic_map_finalize(bmap);
8483 return bmap;
8484 error:
8485 isl_basic_map_free(bmap);
8486 isl_local_space_free(ls);
8487 return NULL;
8490 /* Replace the space of "map" by the space and divs of "ls".
8492 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
8493 __isl_take isl_local_space *ls)
8495 int i;
8497 map = isl_map_cow(map);
8498 if (!map || !ls)
8499 goto error;
8501 for (i = 0; i < map->n; ++i) {
8502 map->p[i] = basic_replace_space_by_local_space(map->p[i],
8503 isl_local_space_copy(ls));
8504 if (!map->p[i])
8505 goto error;
8507 isl_space_free(isl_map_take_space(map));
8508 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
8510 isl_local_space_free(ls);
8511 return map;
8512 error:
8513 isl_local_space_free(ls);
8514 isl_map_free(map);
8515 return NULL;
8518 /* Compute an explicit representation for the existentially
8519 * quantified variables for which do not know any explicit representation yet.
8521 * We first sort the existentially quantified variables so that the
8522 * existentially quantified variables for which we already have an explicit
8523 * representation are placed before those for which we do not.
8524 * The input dimensions, the output dimensions and the existentially
8525 * quantified variables for which we already have an explicit
8526 * representation are then turned into parameters.
8527 * compute_divs returns a map with the same parameters and
8528 * no input or output dimensions and the dimension specification
8529 * is reset to that of the input, including the existentially quantified
8530 * variables for which we already had an explicit representation.
8532 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
8534 struct isl_basic_set *bset;
8535 struct isl_set *set;
8536 struct isl_map *map;
8537 isl_space *space;
8538 isl_local_space *ls;
8539 isl_size nparam;
8540 isl_size n_in;
8541 isl_size n_out;
8542 int n_known;
8543 int i;
8545 bmap = isl_basic_map_sort_divs(bmap);
8546 bmap = isl_basic_map_cow(bmap);
8547 if (!bmap)
8548 return NULL;
8550 n_known = isl_basic_map_first_unknown_div(bmap);
8551 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8552 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8553 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8554 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8555 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8557 space = isl_space_set_alloc(bmap->ctx,
8558 nparam + n_in + n_out + n_known, 0);
8559 if (!space)
8560 goto error;
8562 ls = isl_basic_map_get_local_space(bmap);
8563 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8564 n_known, bmap->n_div - n_known);
8565 if (n_known > 0) {
8566 for (i = n_known; i < bmap->n_div; ++i)
8567 swap_div(bmap, i - n_known, i);
8568 bmap->n_div -= n_known;
8569 bmap->extra -= n_known;
8571 bmap = isl_basic_map_reset_space(bmap, space);
8572 bset = bset_from_bmap(bmap);
8574 set = parameter_compute_divs(bset);
8575 map = set_to_map(set);
8576 map = replace_space_by_local_space(map, ls);
8578 return map;
8579 error:
8580 isl_basic_map_free(bmap);
8581 return NULL;
8584 /* Is the integer division at position "div" of "bmap" integral?
8585 * That is, does it have denominator 1?
8587 isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
8589 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8590 return isl_bool_error;
8591 return isl_int_is_one(bmap->div[div][0]);
8594 /* Remove the explicit representation of local variable "div",
8595 * if there is any.
8597 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8598 __isl_take isl_basic_map *bmap, int div)
8600 isl_bool unknown;
8602 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8603 if (unknown < 0)
8604 return isl_basic_map_free(bmap);
8605 if (unknown)
8606 return bmap;
8608 bmap = isl_basic_map_cow(bmap);
8609 if (!bmap)
8610 return NULL;
8611 isl_int_set_si(bmap->div[div][0], 0);
8612 return bmap;
8615 /* Is local variable "div" of "bmap" marked as not having an explicit
8616 * representation?
8617 * Note that even if "div" is not marked in this way and therefore
8618 * has an explicit representation, this representation may still
8619 * depend (indirectly) on other local variables that do not
8620 * have an explicit representation.
8622 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8623 int div)
8625 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8626 return isl_bool_error;
8627 return isl_int_is_zero(bmap->div[div][0]);
8630 /* Return the position of the first local variable that does not
8631 * have an explicit representation.
8632 * Return the total number of local variables if they all have
8633 * an explicit representation.
8634 * Return -1 on error.
8636 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8638 int i;
8640 if (!bmap)
8641 return -1;
8643 for (i = 0; i < bmap->n_div; ++i) {
8644 if (!isl_basic_map_div_is_known(bmap, i))
8645 return i;
8647 return bmap->n_div;
8650 /* Return the position of the first local variable that does not
8651 * have an explicit representation.
8652 * Return the total number of local variables if they all have
8653 * an explicit representation.
8654 * Return -1 on error.
8656 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8658 return isl_basic_map_first_unknown_div(bset);
8661 /* Does "bmap" have an explicit representation for all local variables?
8663 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8665 int first;
8666 isl_size n;
8668 n = isl_basic_map_dim(bmap, isl_dim_div);
8669 first = isl_basic_map_first_unknown_div(bmap);
8670 if (n < 0 || first < 0)
8671 return isl_bool_error;
8672 return first == n;
8675 /* Do all basic maps in "map" have an explicit representation
8676 * for all local variables?
8678 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8680 int i;
8682 if (!map)
8683 return isl_bool_error;
8685 for (i = 0; i < map->n; ++i) {
8686 int known = isl_basic_map_divs_known(map->p[i]);
8687 if (known <= 0)
8688 return known;
8691 return isl_bool_true;
8694 /* If bmap contains any unknown divs, then compute explicit
8695 * expressions for them. However, this computation may be
8696 * quite expensive, so first try to remove divs that aren't
8697 * strictly needed.
8699 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8701 int known;
8702 struct isl_map *map;
8704 known = isl_basic_map_divs_known(bmap);
8705 if (known < 0)
8706 goto error;
8707 if (known)
8708 return isl_map_from_basic_map(bmap);
8710 bmap = isl_basic_map_drop_redundant_divs(bmap);
8712 known = isl_basic_map_divs_known(bmap);
8713 if (known < 0)
8714 goto error;
8715 if (known)
8716 return isl_map_from_basic_map(bmap);
8718 map = compute_divs(bmap);
8719 return map;
8720 error:
8721 isl_basic_map_free(bmap);
8722 return NULL;
8725 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8727 int i;
8728 int known;
8729 struct isl_map *res;
8731 if (!map)
8732 return NULL;
8733 if (map->n == 0)
8734 return map;
8736 known = isl_map_divs_known(map);
8737 if (known < 0) {
8738 isl_map_free(map);
8739 return NULL;
8741 if (known)
8742 return map;
8744 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8745 for (i = 1 ; i < map->n; ++i) {
8746 struct isl_map *r2;
8747 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8748 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8749 res = isl_map_union_disjoint(res, r2);
8750 else
8751 res = isl_map_union(res, r2);
8753 isl_map_free(map);
8755 return res;
8758 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8760 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8763 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8765 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8768 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8770 isl_space *space;
8771 isl_size n_out;
8773 n_out = isl_map_dim(map, isl_dim_out);
8774 if (n_out < 0)
8775 return set_from_map(isl_map_free(map));
8776 space = isl_space_domain(isl_map_get_space(map));
8778 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8780 return set_from_map(isl_map_reset_space(map, space));
8783 /* Return the union of "map1" and "map2", where we assume for now that
8784 * "map1" and "map2" are disjoint. Note that the basic maps inside
8785 * "map1" or "map2" may not be disjoint from each other.
8786 * Also note that this function is also called from isl_map_union,
8787 * which takes care of handling the situation where "map1" and "map2"
8788 * may not be disjoint.
8790 * If one of the inputs is empty, we can simply return the other input.
8791 * Similarly, if one of the inputs is universal, then it is equal to the union.
8793 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8794 __isl_take isl_map *map2)
8796 int i;
8797 unsigned flags = 0;
8798 struct isl_map *map = NULL;
8799 int is_universe;
8801 if (isl_map_check_equal_space(map1, map2) < 0)
8802 goto error;
8804 if (map1->n == 0) {
8805 isl_map_free(map1);
8806 return map2;
8808 if (map2->n == 0) {
8809 isl_map_free(map2);
8810 return map1;
8813 is_universe = isl_map_plain_is_universe(map1);
8814 if (is_universe < 0)
8815 goto error;
8816 if (is_universe) {
8817 isl_map_free(map2);
8818 return map1;
8821 is_universe = isl_map_plain_is_universe(map2);
8822 if (is_universe < 0)
8823 goto error;
8824 if (is_universe) {
8825 isl_map_free(map1);
8826 return map2;
8829 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8830 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8831 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8833 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8834 map1->n + map2->n, flags);
8835 if (!map)
8836 goto error;
8837 for (i = 0; i < map1->n; ++i) {
8838 map = isl_map_add_basic_map(map,
8839 isl_basic_map_copy(map1->p[i]));
8840 if (!map)
8841 goto error;
8843 for (i = 0; i < map2->n; ++i) {
8844 map = isl_map_add_basic_map(map,
8845 isl_basic_map_copy(map2->p[i]));
8846 if (!map)
8847 goto error;
8849 isl_map_free(map1);
8850 isl_map_free(map2);
8851 return map;
8852 error:
8853 isl_map_free(map);
8854 isl_map_free(map1);
8855 isl_map_free(map2);
8856 return NULL;
8859 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8860 * guaranteed to be disjoint by the caller.
8862 * Note that this functions is called from within isl_map_make_disjoint,
8863 * so we have to be careful not to touch the constraints of the inputs
8864 * in any way.
8866 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8867 __isl_take isl_map *map2)
8869 isl_map_align_params_bin(&map1, &map2);
8870 return map_union_disjoint(map1, map2);
8873 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8874 * not be disjoint.
8876 * We currently simply call map_union_disjoint, the internal operation
8877 * of which does not really depend on the inputs being disjoint.
8878 * If the result contains more than one basic map, then we clear
8879 * the disjoint flag since the result may contain basic maps from
8880 * both inputs and these are not guaranteed to be disjoint.
8882 * As a special case, if "map1" and "map2" are obviously equal,
8883 * then we simply return "map1".
8885 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8886 __isl_take isl_map *map2)
8888 int equal;
8890 if (isl_map_align_params_bin(&map1, &map2) < 0)
8891 goto error;
8893 equal = isl_map_plain_is_equal(map1, map2);
8894 if (equal < 0)
8895 goto error;
8896 if (equal) {
8897 isl_map_free(map2);
8898 return map1;
8901 map1 = map_union_disjoint(map1, map2);
8902 if (!map1)
8903 return NULL;
8904 if (map1->n > 1)
8905 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8906 return map1;
8907 error:
8908 isl_map_free(map1);
8909 isl_map_free(map2);
8910 return NULL;
8913 __isl_give isl_set *isl_set_union_disjoint(
8914 __isl_take isl_set *set1, __isl_take isl_set *set2)
8916 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8917 set_to_map(set2)));
8920 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8921 __isl_take isl_set *set2)
8923 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8926 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8927 * the results in a map living in "space".
8929 * "map" and "set" are assumed to be compatible and non-NULL.
8931 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8932 __isl_take isl_space *space, __isl_take isl_set *set,
8933 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8934 __isl_take isl_basic_set *bset))
8936 unsigned flags = 0;
8937 struct isl_map *result;
8938 int i, j;
8940 if (isl_set_plain_is_universe(set)) {
8941 isl_set_free(set);
8942 return isl_map_reset_equal_dim_space(map, space);
8945 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8946 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8947 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8949 result = isl_map_alloc_space(space, map->n * set->n, flags);
8950 for (i = 0; result && i < map->n; ++i)
8951 for (j = 0; j < set->n; ++j) {
8952 result = isl_map_add_basic_map(result,
8953 fn(isl_basic_map_copy(map->p[i]),
8954 isl_basic_set_copy(set->p[j])));
8955 if (!result)
8956 break;
8959 isl_map_free(map);
8960 isl_set_free(set);
8961 return result;
8964 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8965 __isl_take isl_set *set)
8967 isl_bool ok;
8968 isl_space *space;
8970 isl_map_align_params_set(&map, &set);
8971 ok = isl_map_compatible_range(map, set);
8972 if (ok < 0)
8973 goto error;
8974 if (!ok)
8975 isl_die(set->ctx, isl_error_invalid,
8976 "incompatible spaces", goto error);
8978 space = isl_map_get_space(map);
8979 return map_intersect_set(map, space, set,
8980 &isl_basic_map_intersect_range);
8981 error:
8982 isl_map_free(map);
8983 isl_set_free(set);
8984 return NULL;
8987 /* Intersect the domain of "map" with "set".
8989 * If the domain dimensions of "map" do not have any identifiers,
8990 * then copy them over from "set".
8992 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8993 __isl_take isl_set *set)
8995 isl_bool ok;
8996 isl_space *space;
8998 isl_map_align_params_set(&map, &set);
8999 ok = isl_map_compatible_domain(map, set);
9000 if (ok < 0)
9001 goto error;
9002 if (!ok)
9003 isl_die(set->ctx, isl_error_invalid,
9004 "incompatible spaces", goto error);
9006 space = isl_map_get_space(map);
9007 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
9008 isl_set_peek_space(set), isl_dim_set);
9009 return map_intersect_set(map, space, set,
9010 &isl_basic_map_intersect_domain);
9011 error:
9012 isl_map_free(map);
9013 isl_set_free(set);
9014 return NULL;
9017 #undef BASE
9018 #define BASE map
9019 static
9020 #include "isl_copy_tuple_id_templ.c"
9022 /* Data structure that specifies how isl_map_intersect_factor
9023 * should operate.
9025 * "preserve_type" is the tuple where the factor differs from
9026 * the input map and of which the identifiers needs
9027 * to be preserved explicitly.
9028 * "other_factor" is used to extract the space of the other factor
9029 * from the space of the product ("map").
9030 * "product" is used to combine the given factor and a universe map
9031 * in the space returned by "other_factor" to produce a map
9032 * that lives in the same space as the input map.
9034 struct isl_intersect_factor_control {
9035 enum isl_dim_type preserve_type;
9036 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
9037 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
9038 __isl_take isl_map *other);
9041 /* Given a map "map" in some product space and a map "factor"
9042 * living in some factor space, return the intersection.
9044 * After aligning the parameters,
9045 * the map "factor" is first extended to a map living in the same space
9046 * as "map" and then a regular intersection is computed.
9048 * Note that the extension is computed as a product, which is anonymous
9049 * by default. If "map" has an identifier on the corresponding tuple,
9050 * then this identifier needs to be set on the product
9051 * before the intersection is computed.
9053 static __isl_give isl_map *isl_map_intersect_factor(
9054 __isl_take isl_map *map, __isl_take isl_map *factor,
9055 struct isl_intersect_factor_control *control)
9057 isl_bool equal;
9058 isl_space *space;
9059 isl_map *other, *product;
9061 equal = isl_map_has_equal_params(map, factor);
9062 if (equal < 0)
9063 goto error;
9064 if (!equal) {
9065 map = isl_map_align_params(map, isl_map_get_space(factor));
9066 factor = isl_map_align_params(factor, isl_map_get_space(map));
9069 space = isl_map_get_space(map);
9070 other = isl_map_universe(control->other_factor(space));
9071 product = control->product(factor, other);
9073 space = isl_map_peek_space(map);
9074 product = isl_map_copy_tuple_id(product, control->preserve_type,
9075 space, control->preserve_type);
9076 return map_intersect(map, product);
9077 error:
9078 isl_map_free(map);
9079 isl_map_free(factor);
9080 return NULL;
9083 /* Return the domain product of "map2" and "map1".
9085 static __isl_give isl_map *isl_map_reverse_domain_product(
9086 __isl_take isl_map *map1, __isl_take isl_map *map2)
9088 return isl_map_domain_product(map2, map1);
9091 /* Return the range product of "map2" and "map1".
9093 static __isl_give isl_map *isl_map_reverse_range_product(
9094 __isl_take isl_map *map1, __isl_take isl_map *map2)
9096 return isl_map_range_product(map2, map1);
9099 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9100 * in the space A -> C, return the intersection.
9102 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
9103 __isl_take isl_map *map, __isl_take isl_map *factor)
9105 struct isl_intersect_factor_control control = {
9106 .preserve_type = isl_dim_in,
9107 .other_factor = isl_space_domain_factor_range,
9108 .product = isl_map_domain_product,
9111 return isl_map_intersect_factor(map, factor, &control);
9114 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9115 * in the space B -> C, return the intersection.
9117 __isl_give isl_map *isl_map_intersect_domain_factor_range(
9118 __isl_take isl_map *map, __isl_take isl_map *factor)
9120 struct isl_intersect_factor_control control = {
9121 .preserve_type = isl_dim_in,
9122 .other_factor = isl_space_domain_factor_domain,
9123 .product = isl_map_reverse_domain_product,
9126 return isl_map_intersect_factor(map, factor, &control);
9129 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9130 * in the space A -> B, return the intersection.
9132 __isl_give isl_map *isl_map_intersect_range_factor_domain(
9133 __isl_take isl_map *map, __isl_take isl_map *factor)
9135 struct isl_intersect_factor_control control = {
9136 .preserve_type = isl_dim_out,
9137 .other_factor = isl_space_range_factor_range,
9138 .product = isl_map_range_product,
9141 return isl_map_intersect_factor(map, factor, &control);
9144 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9145 * in the space A -> C, return the intersection.
9147 __isl_give isl_map *isl_map_intersect_range_factor_range(
9148 __isl_take isl_map *map, __isl_take isl_map *factor)
9150 struct isl_intersect_factor_control control = {
9151 .preserve_type = isl_dim_out,
9152 .other_factor = isl_space_range_factor_domain,
9153 .product = isl_map_reverse_range_product,
9156 return isl_map_intersect_factor(map, factor, &control);
9159 /* Given a set "set" in a space [A -> B] and a set "domain"
9160 * in the space A, return the intersection.
9162 * The set "domain" is first extended to a set living in the space
9163 * [A -> B] and then a regular intersection is computed.
9165 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
9166 __isl_take isl_set *domain)
9168 struct isl_intersect_factor_control control = {
9169 .preserve_type = isl_dim_set,
9170 .other_factor = isl_space_factor_range,
9171 .product = isl_map_range_product,
9174 return set_from_map(isl_map_intersect_factor(set_to_map(set),
9175 set_to_map(domain), &control));
9178 /* Given a set "set" in a space [A -> B] and a set "range"
9179 * in the space B, return the intersection.
9181 * The set "range" is first extended to a set living in the space
9182 * [A -> B] and then a regular intersection is computed.
9184 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
9185 __isl_take isl_set *range)
9187 struct isl_intersect_factor_control control = {
9188 .preserve_type = isl_dim_set,
9189 .other_factor = isl_space_factor_domain,
9190 .product = isl_map_reverse_range_product,
9193 return set_from_map(isl_map_intersect_factor(set_to_map(set),
9194 set_to_map(range), &control));
9197 #undef BASE
9198 #define BASE set
9199 static
9200 #include "isl_copy_tuple_id_templ.c"
9202 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
9203 * in the space A, return the intersection.
9205 * The set "domain" is extended to a set living in the space [A -> B] and
9206 * the domain of "map" is intersected with this set.
9208 * If "map" has an identifier on the domain tuple,
9209 * then this identifier needs to be set on this product
9210 * before the intersection is computed.
9212 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
9213 __isl_take isl_map *map, __isl_take isl_set *domain)
9215 isl_space *space;
9216 isl_set *factor;
9218 isl_map_align_params_set(&map, &domain);
9219 space = isl_map_get_space(map);
9220 space = isl_space_domain_wrapped_range(space);
9221 factor = isl_set_universe(space);
9222 domain = isl_set_product(domain, factor);
9223 space = isl_map_peek_space(map);
9224 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
9225 return isl_map_intersect_domain(map, domain);
9228 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
9229 * in the space B, return the intersection.
9231 * The set "domain" is extended to a set living in the space [B -> C] and
9232 * the range of "map" is intersected with this set.
9234 * If "map" has an identifier on the range tuple,
9235 * then this identifier needs to be set on this product
9236 * before the intersection is computed.
9238 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
9239 __isl_take isl_map *map, __isl_take isl_set *domain)
9241 isl_space *space;
9242 isl_set *factor;
9244 isl_map_align_params_set(&map, &domain);
9245 space = isl_map_get_space(map);
9246 space = isl_space_range_wrapped_range(space);
9247 factor = isl_set_universe(space);
9248 domain = isl_set_product(domain, factor);
9249 space = isl_map_peek_space(map);
9250 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
9251 return isl_map_intersect_range(map, domain);
9254 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
9255 __isl_take isl_map *map2)
9257 if (isl_map_align_params_bin(&map1, &map2) < 0)
9258 goto error;
9259 map1 = isl_map_reverse(map1);
9260 map1 = isl_map_apply_range(map1, map2);
9261 return isl_map_reverse(map1);
9262 error:
9263 isl_map_free(map1);
9264 isl_map_free(map2);
9265 return NULL;
9268 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
9269 __isl_take isl_map *map2)
9271 isl_space *space;
9272 struct isl_map *result;
9273 int i, j;
9275 if (isl_map_align_params_bin(&map1, &map2) < 0)
9276 goto error;
9278 space = isl_space_join(isl_space_copy(map1->dim),
9279 isl_space_copy(map2->dim));
9281 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
9282 if (!result)
9283 goto error;
9284 for (i = 0; i < map1->n; ++i)
9285 for (j = 0; j < map2->n; ++j) {
9286 result = isl_map_add_basic_map(result,
9287 isl_basic_map_apply_range(
9288 isl_basic_map_copy(map1->p[i]),
9289 isl_basic_map_copy(map2->p[j])));
9290 if (!result)
9291 goto error;
9293 isl_map_free(map1);
9294 isl_map_free(map2);
9295 if (result && result->n <= 1)
9296 ISL_F_SET(result, ISL_MAP_DISJOINT);
9297 return result;
9298 error:
9299 isl_map_free(map1);
9300 isl_map_free(map2);
9301 return NULL;
9304 /* Is "bmap" a transformation, i.e.,
9305 * does it relate elements from the same space.
9307 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
9309 isl_space *space;
9311 space = isl_basic_map_peek_space(bmap);
9312 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
9315 /* Check that "bmap" is a transformation, i.e.,
9316 * that it relates elements from the same space.
9318 static isl_stat isl_basic_map_check_transformation(
9319 __isl_keep isl_basic_map *bmap)
9321 isl_bool equal;
9323 equal = isl_basic_map_is_transformation(bmap);
9324 if (equal < 0)
9325 return isl_stat_error;
9326 if (!equal)
9327 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9328 "domain and range don't match", return isl_stat_error);
9329 return isl_stat_ok;
9333 * returns range - domain
9335 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
9337 isl_space *target_space;
9338 struct isl_basic_set *bset;
9339 isl_size dim;
9340 isl_size nparam;
9341 isl_size total;
9342 int i;
9344 if (isl_basic_map_check_transformation(bmap) < 0)
9345 return isl_basic_map_free(bmap);
9346 dim = isl_basic_map_dim(bmap, isl_dim_in);
9347 nparam = isl_basic_map_dim(bmap, isl_dim_param);
9348 if (dim < 0 || nparam < 0)
9349 goto error;
9350 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
9351 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
9352 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
9353 total = isl_basic_map_dim(bmap, isl_dim_all);
9354 if (total < 0)
9355 bmap = isl_basic_map_free(bmap);
9356 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
9357 for (i = 0; i < dim; ++i) {
9358 int j = isl_basic_map_alloc_equality(bmap);
9359 if (j < 0) {
9360 bmap = isl_basic_map_free(bmap);
9361 break;
9363 isl_seq_clr(bmap->eq[j], 1 + total);
9364 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
9365 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
9366 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
9368 bset = isl_basic_map_domain(bmap);
9369 bset = isl_basic_set_reset_space(bset, target_space);
9370 return bset;
9371 error:
9372 isl_basic_map_free(bmap);
9373 return NULL;
9376 /* Is the tuple of type "type1" of "map" the same as
9377 * the tuple of type "type2" of "space"?
9379 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
9380 enum isl_dim_type type1, __isl_keep isl_space *space,
9381 enum isl_dim_type type2)
9383 isl_space *map_space;
9385 map_space = isl_map_peek_space(map);
9386 return isl_space_tuple_is_equal(map_space, type1, space, type2);
9389 /* Is the tuple of type "type1" of "map1" the same as
9390 * the tuple of type "type2" of "map2"?
9392 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
9393 enum isl_dim_type type1, __isl_keep isl_map *map2,
9394 enum isl_dim_type type2)
9396 isl_space *space1, *space2;
9398 space1 = isl_map_peek_space(map1);
9399 space2 = isl_map_peek_space(map2);
9400 return isl_space_tuple_is_equal(space1, type1, space2, type2);
9403 /* Is the space of "obj" equal to "space", ignoring parameters?
9405 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
9406 __isl_keep isl_space *space)
9408 isl_space *map_space;
9410 map_space = isl_map_peek_space(map);
9411 return isl_space_has_equal_tuples(map_space, space);
9414 /* Check that "map" is a transformation, i.e.,
9415 * that it relates elements from the same space.
9417 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
9419 isl_bool equal;
9421 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
9422 if (equal < 0)
9423 return isl_stat_error;
9424 if (!equal)
9425 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9426 "domain and range don't match", return isl_stat_error);
9427 return isl_stat_ok;
9431 * returns range - domain
9433 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
9435 int i;
9436 isl_space *space;
9437 struct isl_set *result;
9439 if (isl_map_check_transformation(map) < 0)
9440 goto error;
9441 space = isl_map_get_space(map);
9442 space = isl_space_domain(space);
9443 result = isl_set_alloc_space(space, map->n, 0);
9444 if (!result)
9445 goto error;
9446 for (i = 0; i < map->n; ++i)
9447 result = isl_set_add_basic_set(result,
9448 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
9449 isl_map_free(map);
9450 return result;
9451 error:
9452 isl_map_free(map);
9453 return NULL;
9457 * returns [domain -> range] -> range - domain
9459 __isl_give isl_basic_map *isl_basic_map_deltas_map(
9460 __isl_take isl_basic_map *bmap)
9462 int i, k;
9463 isl_space *space;
9464 isl_basic_map *domain;
9465 isl_size nparam, n;
9466 isl_size total;
9468 if (isl_basic_map_check_transformation(bmap) < 0)
9469 return isl_basic_map_free(bmap);
9471 nparam = isl_basic_map_dim(bmap, isl_dim_param);
9472 n = isl_basic_map_dim(bmap, isl_dim_in);
9473 if (nparam < 0 || n < 0)
9474 return isl_basic_map_free(bmap);
9476 space = isl_basic_map_get_space(bmap);
9477 space = isl_space_from_range(isl_space_domain(space));
9478 domain = isl_basic_map_universe(space);
9480 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
9481 bmap = isl_basic_map_apply_range(bmap, domain);
9482 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
9484 total = isl_basic_map_dim(bmap, isl_dim_all);
9485 if (total < 0)
9486 return isl_basic_map_free(bmap);
9488 for (i = 0; i < n; ++i) {
9489 k = isl_basic_map_alloc_equality(bmap);
9490 if (k < 0)
9491 goto error;
9492 isl_seq_clr(bmap->eq[k], 1 + total);
9493 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
9494 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
9495 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
9498 bmap = isl_basic_map_gauss(bmap, NULL);
9499 return isl_basic_map_finalize(bmap);
9500 error:
9501 isl_basic_map_free(bmap);
9502 return NULL;
9506 * returns [domain -> range] -> range - domain
9508 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
9510 if (isl_map_check_transformation(map) < 0)
9511 return isl_map_free(map);
9513 return isl_map_transform(map, &isl_space_range_map,
9514 &isl_basic_map_deltas_map);
9517 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9519 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
9521 isl_space *space;
9522 isl_map *map;
9524 space = isl_space_map_from_set(isl_set_get_space(deltas));
9525 map = isl_map_deltas_map(isl_map_universe(space));
9526 map = isl_map_intersect_range(map, deltas);
9528 return isl_set_unwrap(isl_map_domain(map));
9531 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
9533 isl_size n_in, n_out;
9535 n_in = isl_space_dim(space, isl_dim_in);
9536 n_out = isl_space_dim(space, isl_dim_out);
9537 if (n_in < 0 || n_out < 0)
9538 goto error;
9539 if (n_in != n_out)
9540 isl_die(space->ctx, isl_error_invalid,
9541 "number of input and output dimensions needs to be "
9542 "the same", goto error);
9543 return isl_basic_map_equal(space, n_in);
9544 error:
9545 isl_space_free(space);
9546 return NULL;
9549 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
9551 return isl_map_from_basic_map(isl_basic_map_identity(space));
9554 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
9556 isl_space *space = isl_set_get_space(set);
9557 isl_map *id;
9558 id = isl_map_identity(isl_space_map_from_set(space));
9559 return isl_map_intersect_range(id, set);
9562 /* Construct a basic set with all set dimensions having only non-negative
9563 * values.
9565 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9566 __isl_take isl_space *space)
9568 int i;
9569 isl_size nparam;
9570 isl_size dim;
9571 isl_size total;
9572 struct isl_basic_set *bset;
9574 nparam = isl_space_dim(space, isl_dim_param);
9575 dim = isl_space_dim(space, isl_dim_set);
9576 total = isl_space_dim(space, isl_dim_all);
9577 if (nparam < 0 || dim < 0 || total < 0)
9578 space = isl_space_free(space);
9579 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9580 if (!bset)
9581 return NULL;
9582 for (i = 0; i < dim; ++i) {
9583 int k = isl_basic_set_alloc_inequality(bset);
9584 if (k < 0)
9585 goto error;
9586 isl_seq_clr(bset->ineq[k], 1 + total);
9587 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9589 return bset;
9590 error:
9591 isl_basic_set_free(bset);
9592 return NULL;
9595 /* Construct the half-space x_pos >= 0.
9597 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9598 int pos)
9600 int k;
9601 isl_size total;
9602 isl_basic_set *nonneg;
9604 total = isl_space_dim(space, isl_dim_all);
9605 if (total < 0)
9606 space = isl_space_free(space);
9607 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9608 k = isl_basic_set_alloc_inequality(nonneg);
9609 if (k < 0)
9610 goto error;
9611 isl_seq_clr(nonneg->ineq[k], 1 + total);
9612 isl_int_set_si(nonneg->ineq[k][pos], 1);
9614 return isl_basic_set_finalize(nonneg);
9615 error:
9616 isl_basic_set_free(nonneg);
9617 return NULL;
9620 /* Construct the half-space x_pos <= -1.
9622 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9623 int pos)
9625 int k;
9626 isl_size total;
9627 isl_basic_set *neg;
9629 total = isl_space_dim(space, isl_dim_all);
9630 if (total < 0)
9631 space = isl_space_free(space);
9632 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9633 k = isl_basic_set_alloc_inequality(neg);
9634 if (k < 0)
9635 goto error;
9636 isl_seq_clr(neg->ineq[k], 1 + total);
9637 isl_int_set_si(neg->ineq[k][0], -1);
9638 isl_int_set_si(neg->ineq[k][pos], -1);
9640 return isl_basic_set_finalize(neg);
9641 error:
9642 isl_basic_set_free(neg);
9643 return NULL;
9646 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9647 enum isl_dim_type type, unsigned first, unsigned n)
9649 int i;
9650 unsigned offset;
9651 isl_basic_set *nonneg;
9652 isl_basic_set *neg;
9654 if (n == 0)
9655 return set;
9657 if (isl_set_check_range(set, type, first, n) < 0)
9658 return isl_set_free(set);
9660 offset = pos(set->dim, type);
9661 for (i = 0; i < n; ++i) {
9662 nonneg = nonneg_halfspace(isl_set_get_space(set),
9663 offset + first + i);
9664 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9666 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9669 return set;
9672 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9673 int len,
9674 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9675 void *user)
9677 isl_set *half;
9679 if (!set)
9680 return isl_stat_error;
9681 if (isl_set_plain_is_empty(set)) {
9682 isl_set_free(set);
9683 return isl_stat_ok;
9685 if (first == len)
9686 return fn(set, signs, user);
9688 signs[first] = 1;
9689 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9690 1 + first));
9691 half = isl_set_intersect(half, isl_set_copy(set));
9692 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9693 goto error;
9695 signs[first] = -1;
9696 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9697 1 + first));
9698 half = isl_set_intersect(half, set);
9699 return foreach_orthant(half, signs, first + 1, len, fn, user);
9700 error:
9701 isl_set_free(set);
9702 return isl_stat_error;
9705 /* Call "fn" on the intersections of "set" with each of the orthants
9706 * (except for obviously empty intersections). The orthant is identified
9707 * by the signs array, with each entry having value 1 or -1 according
9708 * to the sign of the corresponding variable.
9710 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9711 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9712 void *user)
9714 isl_size nparam;
9715 isl_size nvar;
9716 int *signs;
9717 isl_stat r;
9719 if (!set)
9720 return isl_stat_error;
9721 if (isl_set_plain_is_empty(set))
9722 return isl_stat_ok;
9724 nparam = isl_set_dim(set, isl_dim_param);
9725 nvar = isl_set_dim(set, isl_dim_set);
9726 if (nparam < 0 || nvar < 0)
9727 return isl_stat_error;
9729 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9731 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9732 fn, user);
9734 free(signs);
9736 return r;
9739 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9741 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9744 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9745 __isl_keep isl_basic_map *bmap2)
9747 isl_bool is_subset;
9748 struct isl_map *map1;
9749 struct isl_map *map2;
9751 if (!bmap1 || !bmap2)
9752 return isl_bool_error;
9754 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9755 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9757 is_subset = isl_map_is_subset(map1, map2);
9759 isl_map_free(map1);
9760 isl_map_free(map2);
9762 return is_subset;
9765 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9766 __isl_keep isl_basic_set *bset2)
9768 return isl_basic_map_is_subset(bset1, bset2);
9771 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9772 __isl_keep isl_basic_map *bmap2)
9774 isl_bool is_subset;
9776 if (!bmap1 || !bmap2)
9777 return isl_bool_error;
9778 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9779 if (is_subset != isl_bool_true)
9780 return is_subset;
9781 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9782 return is_subset;
9785 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9786 __isl_keep isl_basic_set *bset2)
9788 return isl_basic_map_is_equal(
9789 bset_to_bmap(bset1), bset_to_bmap(bset2));
9792 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9794 int i;
9795 int is_empty;
9797 if (!map)
9798 return isl_bool_error;
9799 for (i = 0; i < map->n; ++i) {
9800 is_empty = isl_basic_map_is_empty(map->p[i]);
9801 if (is_empty < 0)
9802 return isl_bool_error;
9803 if (!is_empty)
9804 return isl_bool_false;
9806 return isl_bool_true;
9809 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9811 return map ? map->n == 0 : isl_bool_error;
9814 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9816 return set ? set->n == 0 : isl_bool_error;
9819 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9821 return isl_map_is_empty(set_to_map(set));
9824 #undef TYPE
9825 #define TYPE isl_basic_map
9827 static
9828 #include "isl_type_has_equal_space_bin_templ.c"
9829 #include "isl_type_check_equal_space_templ.c"
9831 /* Check that "bset1" and "bset2" live in the same space,
9832 * reporting an error if they do not.
9834 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9835 __isl_keep isl_basic_set *bset2)
9837 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9838 bset_to_bmap(bset1));
9841 #undef TYPE
9842 #define TYPE isl_map
9844 #include "isl_type_has_equal_space_bin_templ.c"
9845 #include "isl_type_check_equal_space_templ.c"
9846 #include "isl_type_has_space_templ.c"
9848 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9849 __isl_keep isl_set *set2)
9851 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9854 #undef TYPE1
9855 #define TYPE1 isl_map
9856 #undef TYPE2
9857 #define TYPE2 isl_basic_map
9858 #undef TYPE_PAIR
9859 #define TYPE_PAIR isl_map_basic_map
9861 static
9862 #include "isl_type_has_equal_space_templ.c"
9863 #include "isl_type_check_equal_space_templ.c"
9865 /* Check that "set" and "bset" live in the same space,
9866 * reporting an error if they do not.
9868 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9869 __isl_keep isl_basic_set *bset)
9871 return isl_map_basic_map_check_equal_space(set_to_map(set),
9872 bset_to_bmap(bset));
9875 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9877 isl_bool is_subset;
9879 if (!map1 || !map2)
9880 return isl_bool_error;
9881 is_subset = isl_map_is_subset(map1, map2);
9882 if (is_subset != isl_bool_true)
9883 return is_subset;
9884 is_subset = isl_map_is_subset(map2, map1);
9885 return is_subset;
9888 /* Is "map1" equal to "map2"?
9890 * First check if they are obviously equal.
9891 * If not, then perform a more detailed analysis.
9893 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9895 isl_bool equal;
9897 equal = isl_map_plain_is_equal(map1, map2);
9898 if (equal < 0 || equal)
9899 return equal;
9900 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9903 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9904 __isl_keep isl_basic_map *bmap2)
9906 isl_bool is_subset;
9908 if (!bmap1 || !bmap2)
9909 return isl_bool_error;
9910 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9911 if (is_subset != isl_bool_true)
9912 return is_subset;
9913 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9914 return isl_bool_not(is_subset);
9917 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9918 __isl_keep isl_map *map2)
9920 isl_bool is_subset;
9922 if (!map1 || !map2)
9923 return isl_bool_error;
9924 is_subset = isl_map_is_subset(map1, map2);
9925 if (is_subset != isl_bool_true)
9926 return is_subset;
9927 is_subset = isl_map_is_subset(map2, map1);
9928 return isl_bool_not(is_subset);
9931 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9932 __isl_keep isl_set *set2)
9934 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9937 /* Is "bmap" obviously equal to the universe with the same space?
9939 * That is, does it not have any constraints?
9941 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9943 if (!bmap)
9944 return isl_bool_error;
9945 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9948 /* Is "bset" obviously equal to the universe with the same space?
9950 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9952 return isl_basic_map_plain_is_universe(bset);
9955 /* If "c" does not involve any existentially quantified variables,
9956 * then set *univ to false and abort
9958 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9960 isl_bool *univ = user;
9961 isl_size n;
9963 n = isl_constraint_dim(c, isl_dim_div);
9964 if (n < 0)
9965 c = isl_constraint_free(c);
9966 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9967 isl_constraint_free(c);
9968 if (*univ < 0 || !*univ)
9969 return isl_stat_error;
9970 return isl_stat_ok;
9973 /* Is "bmap" equal to the universe with the same space?
9975 * First check if it is obviously equal to the universe.
9976 * If not and if there are any constraints not involving
9977 * existentially quantified variables, then it is certainly
9978 * not equal to the universe.
9979 * Otherwise, check if the universe is a subset of "bmap".
9981 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9983 isl_size n_div;
9984 isl_bool univ;
9985 isl_basic_map *test;
9987 univ = isl_basic_map_plain_is_universe(bmap);
9988 if (univ < 0 || univ)
9989 return univ;
9990 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9991 if (n_div < 0)
9992 return isl_bool_error;
9993 if (n_div == 0)
9994 return isl_bool_false;
9995 univ = isl_bool_true;
9996 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9997 univ)
9998 return isl_bool_error;
9999 if (univ < 0 || !univ)
10000 return univ;
10001 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
10002 univ = isl_basic_map_is_subset(test, bmap);
10003 isl_basic_map_free(test);
10004 return univ;
10007 /* Is "bset" equal to the universe with the same space?
10009 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
10011 return isl_basic_map_is_universe(bset);
10014 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
10016 int i;
10018 if (!map)
10019 return isl_bool_error;
10021 for (i = 0; i < map->n; ++i) {
10022 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
10023 if (r < 0 || r)
10024 return r;
10027 return isl_bool_false;
10030 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
10032 return isl_map_plain_is_universe(set_to_map(set));
10035 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
10037 struct isl_basic_set *bset = NULL;
10038 struct isl_vec *sample = NULL;
10039 isl_bool empty, non_empty;
10041 if (!bmap)
10042 return isl_bool_error;
10044 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
10045 return isl_bool_true;
10047 if (isl_basic_map_plain_is_universe(bmap))
10048 return isl_bool_false;
10050 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
10051 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
10052 copy = isl_basic_map_remove_redundancies(copy);
10053 empty = isl_basic_map_plain_is_empty(copy);
10054 isl_basic_map_free(copy);
10055 return empty;
10058 non_empty = isl_basic_map_plain_is_non_empty(bmap);
10059 if (non_empty < 0)
10060 return isl_bool_error;
10061 if (non_empty)
10062 return isl_bool_false;
10063 isl_vec_free(bmap->sample);
10064 bmap->sample = NULL;
10065 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
10066 if (!bset)
10067 return isl_bool_error;
10068 sample = isl_basic_set_sample_vec(bset);
10069 if (!sample)
10070 return isl_bool_error;
10071 empty = sample->size == 0;
10072 isl_vec_free(bmap->sample);
10073 bmap->sample = sample;
10074 if (empty)
10075 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
10077 return empty;
10080 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
10082 if (!bmap)
10083 return isl_bool_error;
10084 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
10087 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
10089 if (!bset)
10090 return isl_bool_error;
10091 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
10094 /* Is "bmap" known to be non-empty?
10096 * That is, is the cached sample still valid?
10098 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
10100 isl_size total;
10102 if (!bmap)
10103 return isl_bool_error;
10104 if (!bmap->sample)
10105 return isl_bool_false;
10106 total = isl_basic_map_dim(bmap, isl_dim_all);
10107 if (total < 0)
10108 return isl_bool_error;
10109 if (bmap->sample->size != 1 + total)
10110 return isl_bool_false;
10111 return isl_basic_map_contains(bmap, bmap->sample);
10114 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
10116 return isl_basic_map_is_empty(bset_to_bmap(bset));
10119 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
10120 __isl_take isl_basic_map *bmap2)
10122 struct isl_map *map;
10124 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
10125 goto error;
10127 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
10128 if (!map)
10129 goto error;
10130 map = isl_map_add_basic_map(map, bmap1);
10131 map = isl_map_add_basic_map(map, bmap2);
10132 return map;
10133 error:
10134 isl_basic_map_free(bmap1);
10135 isl_basic_map_free(bmap2);
10136 return NULL;
10139 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
10140 __isl_take isl_basic_set *bset2)
10142 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
10143 bset_to_bmap(bset2)));
10146 /* Order divs such that any div only depends on previous divs */
10147 __isl_give isl_basic_map *isl_basic_map_order_divs(
10148 __isl_take isl_basic_map *bmap)
10150 int i;
10151 isl_size off;
10153 off = isl_basic_map_var_offset(bmap, isl_dim_div);
10154 if (off < 0)
10155 return isl_basic_map_free(bmap);
10157 for (i = 0; i < bmap->n_div; ++i) {
10158 int pos;
10159 if (isl_int_is_zero(bmap->div[i][0]))
10160 continue;
10161 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
10162 bmap->n_div-i);
10163 if (pos == -1)
10164 continue;
10165 if (pos == 0)
10166 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
10167 "integer division depends on itself",
10168 return isl_basic_map_free(bmap));
10169 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
10170 if (!bmap)
10171 return NULL;
10172 --i;
10174 return bmap;
10177 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
10179 int i;
10181 if (!map)
10182 return 0;
10184 for (i = 0; i < map->n; ++i) {
10185 map->p[i] = isl_basic_map_order_divs(map->p[i]);
10186 if (!map->p[i])
10187 goto error;
10190 return map;
10191 error:
10192 isl_map_free(map);
10193 return NULL;
10196 /* Sort the local variables of "bset".
10198 __isl_give isl_basic_set *isl_basic_set_sort_divs(
10199 __isl_take isl_basic_set *bset)
10201 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
10204 /* Apply the expansion computed by isl_merge_divs.
10205 * The expansion itself is given by "exp" while the resulting
10206 * list of divs is given by "div".
10208 * Move the integer divisions of "bmap" into the right position
10209 * according to "exp" and then introduce the additional integer
10210 * divisions, adding div constraints.
10211 * The moving should be done first to avoid moving coefficients
10212 * in the definitions of the extra integer divisions.
10214 __isl_give isl_basic_map *isl_basic_map_expand_divs(
10215 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
10217 int i, j;
10218 int n_div;
10220 bmap = isl_basic_map_cow(bmap);
10221 if (!bmap || !div)
10222 goto error;
10224 if (div->n_row < bmap->n_div)
10225 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
10226 "not an expansion", goto error);
10228 n_div = bmap->n_div;
10229 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
10230 2 * (div->n_row - n_div));
10232 for (i = n_div; i < div->n_row; ++i)
10233 if (isl_basic_map_alloc_div(bmap) < 0)
10234 goto error;
10236 for (j = n_div - 1; j >= 0; --j) {
10237 if (exp[j] == j)
10238 break;
10239 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
10240 if (!bmap)
10241 goto error;
10243 j = 0;
10244 for (i = 0; i < div->n_row; ++i) {
10245 if (j < n_div && exp[j] == i) {
10246 j++;
10247 } else {
10248 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
10249 if (isl_basic_map_div_is_marked_unknown(bmap, i))
10250 continue;
10251 bmap = isl_basic_map_add_div_constraints(bmap, i);
10252 if (!bmap)
10253 goto error;
10257 isl_mat_free(div);
10258 return bmap;
10259 error:
10260 isl_basic_map_free(bmap);
10261 isl_mat_free(div);
10262 return NULL;
10265 /* Apply the expansion computed by isl_merge_divs.
10266 * The expansion itself is given by "exp" while the resulting
10267 * list of divs is given by "div".
10269 __isl_give isl_basic_set *isl_basic_set_expand_divs(
10270 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
10272 return isl_basic_map_expand_divs(bset, div, exp);
10275 /* Look for a div in dst that corresponds to the div "div" in src.
10276 * The divs before "div" in src and dst are assumed to be the same.
10278 * Return the position of the corresponding div in dst
10279 * if there is one. Otherwise, return a position beyond the integer divisions.
10280 * Return -1 on error.
10282 static int find_div(__isl_keep isl_basic_map *dst,
10283 __isl_keep isl_basic_map *src, unsigned div)
10285 int i;
10286 isl_size n_div;
10287 isl_size v_div;
10289 v_div = isl_basic_map_var_offset(src, isl_dim_div);
10290 n_div = isl_basic_map_dim(dst, isl_dim_div);
10291 if (n_div < 0 || v_div < 0)
10292 return -1;
10293 isl_assert(dst->ctx, div <= n_div, return -1);
10294 for (i = div; i < n_div; ++i)
10295 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
10296 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
10297 n_div - div) == -1)
10298 return i;
10299 return n_div;
10302 /* Align the divs of "dst" to those of "src", adding divs from "src"
10303 * if needed. That is, make sure that the first src->n_div divs
10304 * of the result are equal to those of src.
10305 * The integer division of "src" are assumed to be ordered.
10307 * The integer divisions are swapped into the right position
10308 * (possibly after adding them first). This may result
10309 * in the remaining integer divisions appearing in the wrong order,
10310 * i.e., with some integer division appearing before
10311 * some other integer division on which it depends.
10312 * The integer divisions therefore need to be ordered.
10313 * This will not affect the integer divisions aligned to those of "src",
10314 * since "src" is assumed to have ordered integer divisions.
10316 * The result is not finalized as by design it will have redundant
10317 * divs if any divs from "src" were copied.
10319 __isl_give isl_basic_map *isl_basic_map_align_divs(
10320 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
10322 int i;
10323 isl_bool known;
10324 int extended;
10325 isl_size v_div;
10326 isl_size dst_n_div;
10328 if (!dst || !src)
10329 return isl_basic_map_free(dst);
10331 if (src->n_div == 0)
10332 return dst;
10334 known = isl_basic_map_divs_known(src);
10335 if (known < 0)
10336 return isl_basic_map_free(dst);
10337 if (!known)
10338 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
10339 "some src divs are unknown",
10340 return isl_basic_map_free(dst));
10342 v_div = isl_basic_map_var_offset(src, isl_dim_div);
10343 if (v_div < 0)
10344 return isl_basic_map_free(dst);
10346 extended = 0;
10347 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
10348 if (dst_n_div < 0)
10349 dst = isl_basic_map_free(dst);
10350 for (i = 0; i < src->n_div; ++i) {
10351 int j = find_div(dst, src, i);
10352 if (j < 0)
10353 dst = isl_basic_map_free(dst);
10354 if (j == dst_n_div) {
10355 if (!extended) {
10356 int extra = src->n_div - i;
10357 dst = isl_basic_map_cow(dst);
10358 if (!dst)
10359 return isl_basic_map_free(dst);
10360 dst = isl_basic_map_extend(dst,
10361 extra, 0, 2 * extra);
10362 extended = 1;
10364 j = isl_basic_map_alloc_div(dst);
10365 if (j < 0)
10366 return isl_basic_map_free(dst);
10367 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
10368 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
10369 dst_n_div++;
10370 dst = isl_basic_map_add_div_constraints(dst, j);
10371 if (!dst)
10372 return isl_basic_map_free(dst);
10374 if (j != i)
10375 dst = isl_basic_map_swap_div(dst, i, j);
10376 if (!dst)
10377 return isl_basic_map_free(dst);
10379 return isl_basic_map_order_divs(dst);
10382 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
10384 int i;
10386 if (!map)
10387 return NULL;
10388 if (map->n == 0)
10389 return map;
10390 map = isl_map_compute_divs(map);
10391 map = isl_map_order_divs(map);
10392 map = isl_map_cow(map);
10393 if (!map)
10394 return NULL;
10396 for (i = 1; i < map->n; ++i)
10397 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
10398 for (i = 1; i < map->n; ++i) {
10399 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
10400 if (!map->p[i])
10401 return isl_map_free(map);
10404 map = isl_map_unmark_normalized(map);
10405 return map;
10408 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
10410 return isl_map_align_divs_internal(map);
10413 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
10415 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10418 /* Align the divs of the basic maps in "map" to those
10419 * of the basic maps in "list", as well as to the other basic maps in "map".
10420 * The elements in "list" are assumed to have known divs.
10422 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
10423 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
10425 int i;
10426 isl_size n;
10428 n = isl_basic_map_list_n_basic_map(list);
10429 map = isl_map_compute_divs(map);
10430 map = isl_map_cow(map);
10431 if (!map || n < 0)
10432 return isl_map_free(map);
10433 if (map->n == 0)
10434 return map;
10436 for (i = 0; i < n; ++i) {
10437 isl_basic_map *bmap;
10439 bmap = isl_basic_map_list_get_basic_map(list, i);
10440 bmap = isl_basic_map_order_divs(bmap);
10441 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
10442 isl_basic_map_free(bmap);
10444 if (!map->p[0])
10445 return isl_map_free(map);
10447 return isl_map_align_divs_internal(map);
10450 /* Align the divs of each element of "list" to those of "bmap".
10451 * Both "bmap" and the elements of "list" are assumed to have known divs.
10453 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
10454 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
10456 int i;
10457 isl_size n;
10459 n = isl_basic_map_list_n_basic_map(list);
10460 if (n < 0 || !bmap)
10461 return isl_basic_map_list_free(list);
10463 for (i = 0; i < n; ++i) {
10464 isl_basic_map *bmap_i;
10466 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10467 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
10468 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
10471 return list;
10474 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
10475 __isl_take isl_map *map)
10477 isl_bool ok;
10479 isl_map_align_params_set(&map, &set);
10480 ok = isl_map_compatible_domain(map, set);
10481 if (ok < 0)
10482 goto error;
10483 if (!ok)
10484 isl_die(isl_set_get_ctx(set), isl_error_invalid,
10485 "incompatible spaces", goto error);
10486 map = isl_map_intersect_domain(map, set);
10487 set = isl_map_range(map);
10488 return set;
10489 error:
10490 isl_set_free(set);
10491 isl_map_free(map);
10492 return NULL;
10495 /* There is no need to cow as removing empty parts doesn't change
10496 * the meaning of the set.
10498 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
10500 int i;
10502 if (!map)
10503 return NULL;
10505 for (i = map->n - 1; i >= 0; --i)
10506 map = remove_if_empty(map, i);
10508 return map;
10511 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10513 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10516 /* Create a binary relation that maps the shared initial "pos" dimensions
10517 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10519 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10520 __isl_keep isl_basic_set *bset2, int pos)
10522 isl_basic_map *bmap1;
10523 isl_basic_map *bmap2;
10525 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10526 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10527 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10528 isl_dim_out, 0, pos);
10529 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10530 isl_dim_out, 0, pos);
10531 return isl_basic_map_range_product(bmap1, bmap2);
10534 /* Given two basic sets bset1 and bset2, compute the maximal difference
10535 * between the values of dimension pos in bset1 and those in bset2
10536 * for any common value of the parameters and dimensions preceding pos.
10538 static enum isl_lp_result basic_set_maximal_difference_at(
10539 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10540 int pos, isl_int *opt)
10542 isl_basic_map *bmap1;
10543 struct isl_ctx *ctx;
10544 struct isl_vec *obj;
10545 isl_size total;
10546 isl_size nparam;
10547 isl_size dim1;
10548 enum isl_lp_result res;
10550 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10551 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10552 if (nparam < 0 || dim1 < 0 || !bset2)
10553 return isl_lp_error;
10555 bmap1 = join_initial(bset1, bset2, pos);
10556 total = isl_basic_map_dim(bmap1, isl_dim_all);
10557 if (total < 0)
10558 return isl_lp_error;
10560 ctx = bmap1->ctx;
10561 obj = isl_vec_alloc(ctx, 1 + total);
10562 if (!obj)
10563 goto error;
10564 isl_seq_clr(obj->block.data, 1 + total);
10565 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10566 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10567 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10568 opt, NULL, NULL);
10569 isl_basic_map_free(bmap1);
10570 isl_vec_free(obj);
10571 return res;
10572 error:
10573 isl_basic_map_free(bmap1);
10574 return isl_lp_error;
10577 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10578 * for any common value of the parameters and dimensions preceding pos
10579 * in both basic sets, the values of dimension pos in bset1 are
10580 * smaller or larger than those in bset2.
10582 * Returns
10583 * 1 if bset1 follows bset2
10584 * -1 if bset1 precedes bset2
10585 * 0 if bset1 and bset2 are incomparable
10586 * -2 if some error occurred.
10588 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10589 __isl_keep isl_basic_set *bset2, int pos)
10591 isl_int opt;
10592 enum isl_lp_result res;
10593 int cmp;
10595 isl_int_init(opt);
10597 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10599 if (res == isl_lp_empty)
10600 cmp = 0;
10601 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10602 res == isl_lp_unbounded)
10603 cmp = 1;
10604 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10605 cmp = -1;
10606 else
10607 cmp = -2;
10609 isl_int_clear(opt);
10610 return cmp;
10613 /* Given two basic sets bset1 and bset2, check whether
10614 * for any common value of the parameters and dimensions preceding pos
10615 * there is a value of dimension pos in bset1 that is larger
10616 * than a value of the same dimension in bset2.
10618 * Return
10619 * 1 if there exists such a pair
10620 * 0 if there is no such pair, but there is a pair of equal values
10621 * -1 otherwise
10622 * -2 if some error occurred.
10624 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10625 __isl_keep isl_basic_set *bset2, int pos)
10627 isl_bool empty;
10628 isl_basic_map *bmap;
10629 isl_size dim1;
10631 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10632 if (dim1 < 0)
10633 return -2;
10634 bmap = join_initial(bset1, bset2, pos);
10635 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10636 isl_dim_out, dim1 - pos);
10637 empty = isl_basic_map_is_empty(bmap);
10638 if (empty < 0)
10639 goto error;
10640 if (empty) {
10641 isl_basic_map_free(bmap);
10642 return -1;
10644 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10645 isl_dim_out, dim1 - pos);
10646 empty = isl_basic_map_is_empty(bmap);
10647 if (empty < 0)
10648 goto error;
10649 isl_basic_map_free(bmap);
10650 if (empty)
10651 return 0;
10652 return 1;
10653 error:
10654 isl_basic_map_free(bmap);
10655 return -2;
10658 /* Given two sets set1 and set2, check whether
10659 * for any common value of the parameters and dimensions preceding pos
10660 * there is a value of dimension pos in set1 that is larger
10661 * than a value of the same dimension in set2.
10663 * Return
10664 * 1 if there exists such a pair
10665 * 0 if there is no such pair, but there is a pair of equal values
10666 * -1 otherwise
10667 * -2 if some error occurred.
10669 int isl_set_follows_at(__isl_keep isl_set *set1,
10670 __isl_keep isl_set *set2, int pos)
10672 int i, j;
10673 int follows = -1;
10675 if (!set1 || !set2)
10676 return -2;
10678 for (i = 0; i < set1->n; ++i)
10679 for (j = 0; j < set2->n; ++j) {
10680 int f;
10681 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10682 if (f == 1 || f == -2)
10683 return f;
10684 if (f > follows)
10685 follows = f;
10688 return follows;
10691 static isl_bool isl_basic_map_plain_has_fixed_var(
10692 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10694 int i;
10695 int d;
10696 isl_size total;
10698 total = isl_basic_map_dim(bmap, isl_dim_all);
10699 if (total < 0)
10700 return isl_bool_error;
10701 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10702 for (; d+1 > pos; --d)
10703 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10704 break;
10705 if (d != pos)
10706 continue;
10707 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10708 return isl_bool_false;
10709 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10710 return isl_bool_false;
10711 if (!isl_int_is_one(bmap->eq[i][1+d]))
10712 return isl_bool_false;
10713 if (val)
10714 isl_int_neg(*val, bmap->eq[i][0]);
10715 return isl_bool_true;
10717 return isl_bool_false;
10720 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10721 unsigned pos, isl_int *val)
10723 int i;
10724 isl_int v;
10725 isl_int tmp;
10726 isl_bool fixed;
10728 if (!map)
10729 return isl_bool_error;
10730 if (map->n == 0)
10731 return isl_bool_false;
10732 if (map->n == 1)
10733 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10734 isl_int_init(v);
10735 isl_int_init(tmp);
10736 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10737 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10738 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10739 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10740 fixed = isl_bool_false;
10742 if (val)
10743 isl_int_set(*val, v);
10744 isl_int_clear(tmp);
10745 isl_int_clear(v);
10746 return fixed;
10749 static isl_bool isl_basic_set_plain_has_fixed_var(
10750 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10752 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10753 pos, val);
10756 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10757 enum isl_dim_type type, unsigned pos, isl_int *val)
10759 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10760 return isl_bool_error;
10761 return isl_basic_map_plain_has_fixed_var(bmap,
10762 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10765 /* If "bmap" obviously lies on a hyperplane where the given dimension
10766 * has a fixed value, then return that value.
10767 * Otherwise return NaN.
10769 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10770 __isl_keep isl_basic_map *bmap,
10771 enum isl_dim_type type, unsigned pos)
10773 isl_ctx *ctx;
10774 isl_val *v;
10775 isl_bool fixed;
10777 if (!bmap)
10778 return NULL;
10779 ctx = isl_basic_map_get_ctx(bmap);
10780 v = isl_val_alloc(ctx);
10781 if (!v)
10782 return NULL;
10783 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10784 if (fixed < 0)
10785 return isl_val_free(v);
10786 if (fixed) {
10787 isl_int_set_si(v->d, 1);
10788 return v;
10790 isl_val_free(v);
10791 return isl_val_nan(ctx);
10794 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10795 enum isl_dim_type type, unsigned pos, isl_int *val)
10797 if (isl_map_check_range(map, type, pos, 1) < 0)
10798 return isl_bool_error;
10799 return isl_map_plain_has_fixed_var(map,
10800 map_offset(map, type) - 1 + pos, val);
10803 /* If "map" obviously lies on a hyperplane where the given dimension
10804 * has a fixed value, then return that value.
10805 * Otherwise return NaN.
10807 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10808 enum isl_dim_type type, unsigned pos)
10810 isl_ctx *ctx;
10811 isl_val *v;
10812 isl_bool fixed;
10814 if (!map)
10815 return NULL;
10816 ctx = isl_map_get_ctx(map);
10817 v = isl_val_alloc(ctx);
10818 if (!v)
10819 return NULL;
10820 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10821 if (fixed < 0)
10822 return isl_val_free(v);
10823 if (fixed) {
10824 isl_int_set_si(v->d, 1);
10825 return v;
10827 isl_val_free(v);
10828 return isl_val_nan(ctx);
10831 /* If "set" obviously lies on a hyperplane where the given dimension
10832 * has a fixed value, then return that value.
10833 * Otherwise return NaN.
10835 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10836 enum isl_dim_type type, unsigned pos)
10838 return isl_map_plain_get_val_if_fixed(set, type, pos);
10841 /* Return a sequence of values in the same space as "set"
10842 * that are equal to the corresponding set dimensions of "set"
10843 * for those set dimensions that obviously lie on a hyperplane
10844 * where the dimension has a fixed value.
10845 * The other elements are set to NaN.
10847 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10848 __isl_keep isl_set *set)
10850 int i;
10851 isl_size n;
10852 isl_space *space;
10853 isl_multi_val *mv;
10855 space = isl_space_drop_all_params(isl_set_get_space(set));
10856 mv = isl_multi_val_alloc(space);
10857 n = isl_multi_val_size(mv);
10858 if (n < 0)
10859 return isl_multi_val_free(mv);
10861 for (i = 0; i < n; ++i) {
10862 isl_val *v;
10864 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10865 mv = isl_multi_val_set_val(mv, i, v);
10868 return mv;
10871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10872 * then return this fixed value in *val.
10874 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10875 unsigned dim, isl_int *val)
10877 isl_size nparam;
10879 nparam = isl_basic_set_dim(bset, isl_dim_param);
10880 if (nparam < 0)
10881 return isl_bool_error;
10882 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10885 /* Return -1 if the constraint "c1" should be sorted before "c2"
10886 * and 1 if it should be sorted after "c2".
10887 * Return 0 if the two constraints are the same (up to the constant term).
10889 * In particular, if a constraint involves later variables than another
10890 * then it is sorted after this other constraint.
10891 * uset_gist depends on constraints without existentially quantified
10892 * variables sorting first.
10894 * For constraints that have the same latest variable, those
10895 * with the same coefficient for this latest variable (first in absolute value
10896 * and then in actual value) are grouped together.
10897 * This is useful for detecting pairs of constraints that can
10898 * be chained in their printed representation.
10900 * Finally, within a group, constraints are sorted according to
10901 * their coefficients (excluding the constant term).
10903 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10905 isl_int **c1 = (isl_int **) p1;
10906 isl_int **c2 = (isl_int **) p2;
10907 int l1, l2;
10908 unsigned size = *(unsigned *) arg;
10909 int cmp;
10911 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10912 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10914 if (l1 != l2)
10915 return l1 - l2;
10917 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10918 if (cmp != 0)
10919 return cmp;
10920 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10921 if (cmp != 0)
10922 return -cmp;
10924 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10927 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10928 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10929 * and 0 if the two constraints are the same (up to the constant term).
10931 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10932 isl_int *c1, isl_int *c2)
10934 isl_size total;
10935 unsigned size;
10937 total = isl_basic_map_dim(bmap, isl_dim_all);
10938 if (total < 0)
10939 return -2;
10940 size = total;
10941 return sort_constraint_cmp(&c1, &c2, &size);
10944 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10945 __isl_take isl_basic_map *bmap)
10947 isl_size total;
10948 unsigned size;
10950 if (!bmap)
10951 return NULL;
10952 if (bmap->n_ineq == 0)
10953 return bmap;
10954 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10955 return bmap;
10956 total = isl_basic_map_dim(bmap, isl_dim_all);
10957 if (total < 0)
10958 return isl_basic_map_free(bmap);
10959 size = total;
10960 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10961 &sort_constraint_cmp, &size) < 0)
10962 return isl_basic_map_free(bmap);
10963 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10964 return bmap;
10967 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10968 __isl_take isl_basic_set *bset)
10970 isl_basic_map *bmap = bset_to_bmap(bset);
10971 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10974 __isl_give isl_basic_map *isl_basic_map_normalize(
10975 __isl_take isl_basic_map *bmap)
10977 bmap = isl_basic_map_remove_redundancies(bmap);
10978 bmap = isl_basic_map_sort_constraints(bmap);
10979 return bmap;
10981 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10982 __isl_keep isl_basic_map *bmap2)
10984 int i, cmp;
10985 isl_size total;
10986 isl_space *space1, *space2;
10988 if (!bmap1 || !bmap2)
10989 return -1;
10991 if (bmap1 == bmap2)
10992 return 0;
10993 space1 = isl_basic_map_peek_space(bmap1);
10994 space2 = isl_basic_map_peek_space(bmap2);
10995 cmp = isl_space_cmp(space1, space2);
10996 if (cmp)
10997 return cmp;
10998 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10999 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
11000 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
11001 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
11002 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11003 return 0;
11004 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
11005 return 1;
11006 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11007 return -1;
11008 if (bmap1->n_eq != bmap2->n_eq)
11009 return bmap1->n_eq - bmap2->n_eq;
11010 if (bmap1->n_ineq != bmap2->n_ineq)
11011 return bmap1->n_ineq - bmap2->n_ineq;
11012 if (bmap1->n_div != bmap2->n_div)
11013 return bmap1->n_div - bmap2->n_div;
11014 total = isl_basic_map_dim(bmap1, isl_dim_all);
11015 if (total < 0)
11016 return -1;
11017 for (i = 0; i < bmap1->n_eq; ++i) {
11018 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
11019 if (cmp)
11020 return cmp;
11022 for (i = 0; i < bmap1->n_ineq; ++i) {
11023 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
11024 if (cmp)
11025 return cmp;
11027 for (i = 0; i < bmap1->n_div; ++i) {
11028 isl_bool unknown1, unknown2;
11030 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
11031 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
11032 if (unknown1 < 0 || unknown2 < 0)
11033 return -1;
11034 if (unknown1 && unknown2)
11035 continue;
11036 if (unknown1)
11037 return 1;
11038 if (unknown2)
11039 return -1;
11040 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
11041 if (cmp)
11042 return cmp;
11044 return 0;
11047 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
11048 __isl_keep isl_basic_set *bset2)
11050 return isl_basic_map_plain_cmp(bset1, bset2);
11053 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
11055 int i, cmp;
11057 if (set1 == set2)
11058 return 0;
11059 if (set1->n != set2->n)
11060 return set1->n - set2->n;
11062 for (i = 0; i < set1->n; ++i) {
11063 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
11064 if (cmp)
11065 return cmp;
11068 return 0;
11071 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
11072 __isl_keep isl_basic_map *bmap2)
11074 if (!bmap1 || !bmap2)
11075 return isl_bool_error;
11076 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
11079 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
11080 __isl_keep isl_basic_set *bset2)
11082 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
11083 bset_to_bmap(bset2));
11086 static int qsort_bmap_cmp(const void *p1, const void *p2)
11088 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
11089 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
11091 return isl_basic_map_plain_cmp(bmap1, bmap2);
11094 /* Sort the basic maps of "map" and remove duplicate basic maps.
11096 * While removing basic maps, we make sure that the basic maps remain
11097 * sorted because isl_map_normalize expects the basic maps of the result
11098 * to be sorted.
11100 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
11102 int i, j;
11104 map = isl_map_remove_empty_parts(map);
11105 if (!map)
11106 return NULL;
11107 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
11108 for (i = map->n - 1; i >= 1; --i) {
11109 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
11110 continue;
11111 isl_basic_map_free(map->p[i-1]);
11112 for (j = i; j < map->n; ++j)
11113 map->p[j - 1] = map->p[j];
11114 map->n--;
11117 return map;
11120 /* Remove obvious duplicates among the basic maps of "map".
11122 * Unlike isl_map_normalize, this function does not remove redundant
11123 * constraints and only removes duplicates that have exactly the same
11124 * constraints in the input. It does sort the constraints and
11125 * the basic maps to ease the detection of duplicates.
11127 * If "map" has already been normalized or if the basic maps are
11128 * disjoint, then there can be no duplicates.
11130 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
11132 int i;
11133 isl_basic_map *bmap;
11135 if (!map)
11136 return NULL;
11137 if (map->n <= 1)
11138 return map;
11139 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
11140 return map;
11141 for (i = 0; i < map->n; ++i) {
11142 bmap = isl_basic_map_copy(map->p[i]);
11143 bmap = isl_basic_map_sort_constraints(bmap);
11144 if (!bmap)
11145 return isl_map_free(map);
11146 isl_basic_map_free(map->p[i]);
11147 map->p[i] = bmap;
11150 map = sort_and_remove_duplicates(map);
11151 return map;
11154 /* We normalize in place, but if anything goes wrong we need
11155 * to return NULL, so we need to make sure we don't change the
11156 * meaning of any possible other copies of map.
11158 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
11160 int i;
11161 struct isl_basic_map *bmap;
11163 if (!map)
11164 return NULL;
11165 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
11166 return map;
11167 for (i = 0; i < map->n; ++i) {
11168 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
11169 if (!bmap)
11170 goto error;
11171 isl_basic_map_free(map->p[i]);
11172 map->p[i] = bmap;
11175 map = sort_and_remove_duplicates(map);
11176 if (map)
11177 ISL_F_SET(map, ISL_MAP_NORMALIZED);
11178 return map;
11179 error:
11180 isl_map_free(map);
11181 return NULL;
11184 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
11186 return set_from_map(isl_map_normalize(set_to_map(set)));
11189 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
11190 __isl_keep isl_map *map2)
11192 int i;
11193 isl_bool equal;
11195 if (!map1 || !map2)
11196 return isl_bool_error;
11198 if (map1 == map2)
11199 return isl_bool_true;
11200 equal = isl_map_has_equal_space(map1, map2);
11201 if (equal < 0 || !equal)
11202 return equal;
11204 map1 = isl_map_copy(map1);
11205 map2 = isl_map_copy(map2);
11206 map1 = isl_map_normalize(map1);
11207 map2 = isl_map_normalize(map2);
11208 if (!map1 || !map2)
11209 goto error;
11210 equal = map1->n == map2->n;
11211 for (i = 0; equal && i < map1->n; ++i) {
11212 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
11213 if (equal < 0)
11214 goto error;
11216 isl_map_free(map1);
11217 isl_map_free(map2);
11218 return equal;
11219 error:
11220 isl_map_free(map1);
11221 isl_map_free(map2);
11222 return isl_bool_error;
11225 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
11226 __isl_keep isl_set *set2)
11228 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
11231 /* Return the basic maps in "map" as a list.
11233 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
11234 __isl_keep isl_map *map)
11236 int i;
11237 isl_ctx *ctx;
11238 isl_basic_map_list *list;
11240 if (!map)
11241 return NULL;
11242 ctx = isl_map_get_ctx(map);
11243 list = isl_basic_map_list_alloc(ctx, map->n);
11245 for (i = 0; i < map->n; ++i) {
11246 isl_basic_map *bmap;
11248 bmap = isl_basic_map_copy(map->p[i]);
11249 list = isl_basic_map_list_add(list, bmap);
11252 return list;
11255 /* Return the intersection of the elements in the non-empty list "list".
11256 * All elements are assumed to live in the same space.
11258 __isl_give isl_basic_map *isl_basic_map_list_intersect(
11259 __isl_take isl_basic_map_list *list)
11261 int i;
11262 isl_size n;
11263 isl_basic_map *bmap;
11265 n = isl_basic_map_list_n_basic_map(list);
11266 if (n < 0)
11267 goto error;
11268 if (n < 1)
11269 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
11270 "expecting non-empty list", goto error);
11272 bmap = isl_basic_map_list_get_basic_map(list, 0);
11273 for (i = 1; i < n; ++i) {
11274 isl_basic_map *bmap_i;
11276 bmap_i = isl_basic_map_list_get_basic_map(list, i);
11277 bmap = isl_basic_map_intersect(bmap, bmap_i);
11280 isl_basic_map_list_free(list);
11281 return bmap;
11282 error:
11283 isl_basic_map_list_free(list);
11284 return NULL;
11287 /* Return the intersection of the elements in the non-empty list "list".
11288 * All elements are assumed to live in the same space.
11290 __isl_give isl_basic_set *isl_basic_set_list_intersect(
11291 __isl_take isl_basic_set_list *list)
11293 return isl_basic_map_list_intersect(list);
11296 /* Return the union of the elements of "list".
11297 * The list is required to have at least one element.
11299 __isl_give isl_set *isl_basic_set_list_union(
11300 __isl_take isl_basic_set_list *list)
11302 int i;
11303 isl_size n;
11304 isl_space *space;
11305 isl_basic_set *bset;
11306 isl_set *set;
11308 n = isl_basic_set_list_n_basic_set(list);
11309 if (n < 0)
11310 goto error;
11311 if (n < 1)
11312 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
11313 "expecting non-empty list", goto error);
11315 bset = isl_basic_set_list_get_basic_set(list, 0);
11316 space = isl_basic_set_get_space(bset);
11317 isl_basic_set_free(bset);
11319 set = isl_set_alloc_space(space, n, 0);
11320 for (i = 0; i < n; ++i) {
11321 bset = isl_basic_set_list_get_basic_set(list, i);
11322 set = isl_set_add_basic_set(set, bset);
11325 isl_basic_set_list_free(list);
11326 return set;
11327 error:
11328 isl_basic_set_list_free(list);
11329 return NULL;
11332 /* Return the union of the elements in the non-empty list "list".
11333 * All elements are assumed to live in the same space.
11335 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
11337 int i;
11338 isl_size n;
11339 isl_set *set;
11341 n = isl_set_list_n_set(list);
11342 if (n < 0)
11343 goto error;
11344 if (n < 1)
11345 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
11346 "expecting non-empty list", goto error);
11348 set = isl_set_list_get_set(list, 0);
11349 for (i = 1; i < n; ++i) {
11350 isl_set *set_i;
11352 set_i = isl_set_list_get_set(list, i);
11353 set = isl_set_union(set, set_i);
11356 isl_set_list_free(list);
11357 return set;
11358 error:
11359 isl_set_list_free(list);
11360 return NULL;
11363 __isl_give isl_basic_map *isl_basic_map_product(
11364 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11366 isl_space *space_result = NULL;
11367 struct isl_basic_map *bmap;
11368 unsigned in1, in2, out1, out2, nparam, total, pos;
11369 struct isl_dim_map *dim_map1, *dim_map2;
11371 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11372 goto error;
11373 space_result = isl_space_product(isl_space_copy(bmap1->dim),
11374 isl_space_copy(bmap2->dim));
11376 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11377 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11378 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11379 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11380 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11382 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
11383 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11384 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11385 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11386 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11387 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11388 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11389 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11390 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11391 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11392 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11394 bmap = isl_basic_map_alloc_space(space_result,
11395 bmap1->n_div + bmap2->n_div,
11396 bmap1->n_eq + bmap2->n_eq,
11397 bmap1->n_ineq + bmap2->n_ineq);
11398 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11399 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11400 bmap = isl_basic_map_simplify(bmap);
11401 return isl_basic_map_finalize(bmap);
11402 error:
11403 isl_basic_map_free(bmap1);
11404 isl_basic_map_free(bmap2);
11405 return NULL;
11408 __isl_give isl_basic_map *isl_basic_map_flat_product(
11409 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11411 isl_basic_map *prod;
11413 prod = isl_basic_map_product(bmap1, bmap2);
11414 prod = isl_basic_map_flatten(prod);
11415 return prod;
11418 __isl_give isl_basic_set *isl_basic_set_flat_product(
11419 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
11421 return isl_basic_map_flat_range_product(bset1, bset2);
11424 __isl_give isl_basic_map *isl_basic_map_domain_product(
11425 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11427 isl_space *space1, *space2;
11428 isl_space *space_result = NULL;
11429 isl_basic_map *bmap;
11430 isl_size in1, in2, out, nparam;
11431 unsigned total, pos;
11432 struct isl_dim_map *dim_map1, *dim_map2;
11434 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11435 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11436 out = isl_basic_map_dim(bmap1, isl_dim_out);
11437 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11438 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
11439 goto error;
11441 space1 = isl_basic_map_get_space(bmap1);
11442 space2 = isl_basic_map_get_space(bmap2);
11443 space_result = isl_space_domain_product(space1, space2);
11445 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
11446 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11447 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11448 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11449 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11450 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11451 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11452 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11453 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
11454 isl_dim_map_div(dim_map1, bmap1, pos += out);
11455 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11457 bmap = isl_basic_map_alloc_space(space_result,
11458 bmap1->n_div + bmap2->n_div,
11459 bmap1->n_eq + bmap2->n_eq,
11460 bmap1->n_ineq + bmap2->n_ineq);
11461 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11462 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11463 bmap = isl_basic_map_simplify(bmap);
11464 return isl_basic_map_finalize(bmap);
11465 error:
11466 isl_basic_map_free(bmap1);
11467 isl_basic_map_free(bmap2);
11468 return NULL;
11471 __isl_give isl_basic_map *isl_basic_map_range_product(
11472 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11474 isl_bool rational;
11475 isl_space *space_result = NULL;
11476 isl_basic_map *bmap;
11477 isl_size in, out1, out2, nparam;
11478 unsigned total, pos;
11479 struct isl_dim_map *dim_map1, *dim_map2;
11481 rational = isl_basic_map_is_rational(bmap1);
11482 if (rational >= 0 && rational)
11483 rational = isl_basic_map_is_rational(bmap2);
11484 in = isl_basic_map_dim(bmap1, isl_dim_in);
11485 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11486 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11487 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11488 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
11489 goto error;
11491 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11492 goto error;
11494 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
11495 isl_space_copy(bmap2->dim));
11497 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
11498 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11499 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11500 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11501 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11502 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11503 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11504 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11505 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11506 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11507 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11509 bmap = isl_basic_map_alloc_space(space_result,
11510 bmap1->n_div + bmap2->n_div,
11511 bmap1->n_eq + bmap2->n_eq,
11512 bmap1->n_ineq + bmap2->n_ineq);
11513 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11514 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11515 if (rational)
11516 bmap = isl_basic_map_set_rational(bmap);
11517 bmap = isl_basic_map_simplify(bmap);
11518 return isl_basic_map_finalize(bmap);
11519 error:
11520 isl_basic_map_free(bmap1);
11521 isl_basic_map_free(bmap2);
11522 return NULL;
11525 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11526 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11528 isl_basic_map *prod;
11530 prod = isl_basic_map_range_product(bmap1, bmap2);
11531 prod = isl_basic_map_flatten_range(prod);
11532 return prod;
11535 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11536 * and collect the results.
11537 * The result live in the space obtained by calling "space_product"
11538 * on the spaces of "map1" and "map2".
11539 * If "remove_duplicates" is set then the result may contain duplicates
11540 * (even if the inputs do not) and so we try and remove the obvious
11541 * duplicates.
11543 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11544 __isl_take isl_map *map2,
11545 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11546 __isl_take isl_space *right),
11547 __isl_give isl_basic_map *(*basic_map_product)(
11548 __isl_take isl_basic_map *left,
11549 __isl_take isl_basic_map *right),
11550 int remove_duplicates)
11552 unsigned flags = 0;
11553 struct isl_map *result;
11554 int i, j;
11555 isl_bool m;
11557 m = isl_map_has_equal_params(map1, map2);
11558 if (m < 0)
11559 goto error;
11560 if (!m)
11561 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11562 "parameters don't match", goto error);
11564 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11565 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11566 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11568 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11569 isl_space_copy(map2->dim)),
11570 map1->n * map2->n, flags);
11571 if (!result)
11572 goto error;
11573 for (i = 0; i < map1->n; ++i)
11574 for (j = 0; j < map2->n; ++j) {
11575 struct isl_basic_map *part;
11576 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11577 isl_basic_map_copy(map2->p[j]));
11578 if (isl_basic_map_is_empty(part))
11579 isl_basic_map_free(part);
11580 else
11581 result = isl_map_add_basic_map(result, part);
11582 if (!result)
11583 goto error;
11585 if (remove_duplicates)
11586 result = isl_map_remove_obvious_duplicates(result);
11587 isl_map_free(map1);
11588 isl_map_free(map2);
11589 return result;
11590 error:
11591 isl_map_free(map1);
11592 isl_map_free(map2);
11593 return NULL;
11596 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11598 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11599 __isl_take isl_map *map2)
11601 isl_map_align_params_bin(&map1, &map2);
11602 return map_product(map1, map2, &isl_space_product,
11603 &isl_basic_map_product, 0);
11606 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11608 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11609 __isl_take isl_map *map2)
11611 isl_map *prod;
11613 prod = isl_map_product(map1, map2);
11614 prod = isl_map_flatten(prod);
11615 return prod;
11618 /* Given two set A and B, construct its Cartesian product A x B.
11620 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11621 __isl_take isl_set *set2)
11623 return isl_map_range_product(set1, set2);
11626 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11627 __isl_take isl_set *set2)
11629 return isl_map_flat_range_product(set1, set2);
11632 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11634 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11635 __isl_take isl_map *map2)
11637 isl_map_align_params_bin(&map1, &map2);
11638 return map_product(map1, map2, &isl_space_domain_product,
11639 &isl_basic_map_domain_product, 1);
11642 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11644 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11645 __isl_take isl_map *map2)
11647 isl_map_align_params_bin(&map1, &map2);
11648 return map_product(map1, map2, &isl_space_range_product,
11649 &isl_basic_map_range_product, 1);
11652 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11654 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11656 isl_space *space;
11657 isl_size total1, keep1, total2, keep2;
11659 total1 = isl_map_dim(map, isl_dim_in);
11660 total2 = isl_map_dim(map, isl_dim_out);
11661 if (total1 < 0 || total2 < 0)
11662 return isl_map_free(map);
11663 if (!isl_space_domain_is_wrapping(map->dim) ||
11664 !isl_space_range_is_wrapping(map->dim))
11665 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11666 "not a product", return isl_map_free(map));
11668 space = isl_map_get_space(map);
11669 space = isl_space_factor_domain(space);
11670 keep1 = isl_space_dim(space, isl_dim_in);
11671 keep2 = isl_space_dim(space, isl_dim_out);
11672 if (keep1 < 0 || keep2 < 0)
11673 map = isl_map_free(map);
11674 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11675 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11676 map = isl_map_reset_space(map, space);
11678 return map;
11681 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11683 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11685 isl_space *space;
11686 isl_size total1, keep1, total2, keep2;
11688 total1 = isl_map_dim(map, isl_dim_in);
11689 total2 = isl_map_dim(map, isl_dim_out);
11690 if (total1 < 0 || total2 < 0)
11691 return isl_map_free(map);
11692 if (!isl_space_domain_is_wrapping(map->dim) ||
11693 !isl_space_range_is_wrapping(map->dim))
11694 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11695 "not a product", return isl_map_free(map));
11697 space = isl_map_get_space(map);
11698 space = isl_space_factor_range(space);
11699 keep1 = isl_space_dim(space, isl_dim_in);
11700 keep2 = isl_space_dim(space, isl_dim_out);
11701 if (keep1 < 0 || keep2 < 0)
11702 map = isl_map_free(map);
11703 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11704 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11705 map = isl_map_reset_space(map, space);
11707 return map;
11710 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11712 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11714 isl_space *space;
11715 isl_size total, keep;
11717 total = isl_map_dim(map, isl_dim_in);
11718 if (total < 0)
11719 return isl_map_free(map);
11720 if (!isl_space_domain_is_wrapping(map->dim))
11721 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11722 "domain is not a product", return isl_map_free(map));
11724 space = isl_map_get_space(map);
11725 space = isl_space_domain_factor_domain(space);
11726 keep = isl_space_dim(space, isl_dim_in);
11727 if (keep < 0)
11728 map = isl_map_free(map);
11729 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11730 map = isl_map_reset_space(map, space);
11732 return map;
11735 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11737 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11739 isl_space *space;
11740 isl_size total, keep;
11742 total = isl_map_dim(map, isl_dim_in);
11743 if (total < 0)
11744 return isl_map_free(map);
11745 if (!isl_space_domain_is_wrapping(map->dim))
11746 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11747 "domain is not a product", return isl_map_free(map));
11749 space = isl_map_get_space(map);
11750 space = isl_space_domain_factor_range(space);
11751 keep = isl_space_dim(space, isl_dim_in);
11752 if (keep < 0)
11753 map = isl_map_free(map);
11754 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11755 map = isl_map_reset_space(map, space);
11757 return map;
11760 /* Given a map A -> [B -> C], extract the map A -> B.
11762 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11764 isl_space *space;
11765 isl_size total, keep;
11767 total = isl_map_dim(map, isl_dim_out);
11768 if (total < 0)
11769 return isl_map_free(map);
11770 if (!isl_space_range_is_wrapping(map->dim))
11771 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11772 "range is not a product", return isl_map_free(map));
11774 space = isl_map_get_space(map);
11775 space = isl_space_range_factor_domain(space);
11776 keep = isl_space_dim(space, isl_dim_out);
11777 if (keep < 0)
11778 map = isl_map_free(map);
11779 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11780 map = isl_map_reset_space(map, space);
11782 return map;
11785 /* Given a map A -> [B -> C], extract the map A -> C.
11787 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11789 isl_space *space;
11790 isl_size total, keep;
11792 total = isl_map_dim(map, isl_dim_out);
11793 if (total < 0)
11794 return isl_map_free(map);
11795 if (!isl_space_range_is_wrapping(map->dim))
11796 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11797 "range is not a product", return isl_map_free(map));
11799 space = isl_map_get_space(map);
11800 space = isl_space_range_factor_range(space);
11801 keep = isl_space_dim(space, isl_dim_out);
11802 if (keep < 0)
11803 map = isl_map_free(map);
11804 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11805 map = isl_map_reset_space(map, space);
11807 return map;
11810 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11812 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11813 __isl_take isl_map *map2)
11815 isl_map *prod;
11817 prod = isl_map_domain_product(map1, map2);
11818 prod = isl_map_flatten_domain(prod);
11819 return prod;
11822 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11824 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11825 __isl_take isl_map *map2)
11827 isl_map *prod;
11829 prod = isl_map_range_product(map1, map2);
11830 prod = isl_map_flatten_range(prod);
11831 return prod;
11834 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11836 int i;
11837 uint32_t hash = isl_hash_init();
11838 isl_size total;
11840 if (!bmap)
11841 return 0;
11842 bmap = isl_basic_map_copy(bmap);
11843 bmap = isl_basic_map_normalize(bmap);
11844 total = isl_basic_map_dim(bmap, isl_dim_all);
11845 if (total < 0)
11846 return 0;
11847 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11848 for (i = 0; i < bmap->n_eq; ++i) {
11849 uint32_t c_hash;
11850 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11851 isl_hash_hash(hash, c_hash);
11853 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11854 for (i = 0; i < bmap->n_ineq; ++i) {
11855 uint32_t c_hash;
11856 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11857 isl_hash_hash(hash, c_hash);
11859 isl_hash_byte(hash, bmap->n_div & 0xFF);
11860 for (i = 0; i < bmap->n_div; ++i) {
11861 uint32_t c_hash;
11862 if (isl_int_is_zero(bmap->div[i][0]))
11863 continue;
11864 isl_hash_byte(hash, i & 0xFF);
11865 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11866 isl_hash_hash(hash, c_hash);
11868 isl_basic_map_free(bmap);
11869 return hash;
11872 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11874 return isl_basic_map_get_hash(bset_to_bmap(bset));
11877 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11879 int i;
11880 uint32_t hash;
11882 if (!map)
11883 return 0;
11884 map = isl_map_copy(map);
11885 map = isl_map_normalize(map);
11886 if (!map)
11887 return 0;
11889 hash = isl_hash_init();
11890 for (i = 0; i < map->n; ++i) {
11891 uint32_t bmap_hash;
11892 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11893 isl_hash_hash(hash, bmap_hash);
11896 isl_map_free(map);
11898 return hash;
11901 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11903 return isl_map_get_hash(set_to_map(set));
11906 /* Return the number of basic maps in the (current) representation of "map".
11908 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11910 return map ? map->n : isl_size_error;
11913 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11915 return set ? set->n : isl_size_error;
11918 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11919 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11921 int i;
11923 if (!map)
11924 return isl_stat_error;
11926 for (i = 0; i < map->n; ++i)
11927 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11928 return isl_stat_error;
11930 return isl_stat_ok;
11933 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11934 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11936 int i;
11938 if (!set)
11939 return isl_stat_error;
11941 for (i = 0; i < set->n; ++i)
11942 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11943 return isl_stat_error;
11945 return isl_stat_ok;
11948 /* Does "test" succeed on every basic set in "set"?
11950 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11951 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11952 void *user)
11954 int i;
11956 if (!set)
11957 return isl_bool_error;
11959 for (i = 0; i < set->n; ++i) {
11960 isl_bool r;
11962 r = test(set->p[i], user);
11963 if (r < 0 || !r)
11964 return r;
11967 return isl_bool_true;
11970 /* Return a list of basic sets, the union of which is equal to "set".
11972 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11973 __isl_keep isl_set *set)
11975 int i;
11976 isl_basic_set_list *list;
11978 if (!set)
11979 return NULL;
11981 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11982 for (i = 0; i < set->n; ++i) {
11983 isl_basic_set *bset;
11985 bset = isl_basic_set_copy(set->p[i]);
11986 list = isl_basic_set_list_add(list, bset);
11989 return list;
11992 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11994 isl_space *space;
11996 if (!bset)
11997 return NULL;
11999 bset = isl_basic_set_cow(bset);
12000 if (!bset)
12001 return NULL;
12003 space = isl_basic_set_get_space(bset);
12004 space = isl_space_lift(space, bset->n_div);
12005 if (!space)
12006 goto error;
12007 isl_space_free(bset->dim);
12008 bset->dim = space;
12009 bset->extra -= bset->n_div;
12010 bset->n_div = 0;
12012 bset = isl_basic_set_finalize(bset);
12014 return bset;
12015 error:
12016 isl_basic_set_free(bset);
12017 return NULL;
12020 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
12022 int i;
12023 isl_space *space;
12024 unsigned n_div;
12026 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
12028 if (!set)
12029 return NULL;
12031 set = isl_set_cow(set);
12032 if (!set)
12033 return NULL;
12035 n_div = set->p[0]->n_div;
12036 space = isl_set_get_space(set);
12037 space = isl_space_lift(space, n_div);
12038 if (!space)
12039 goto error;
12040 isl_space_free(set->dim);
12041 set->dim = space;
12043 for (i = 0; i < set->n; ++i) {
12044 set->p[i] = isl_basic_set_lift(set->p[i]);
12045 if (!set->p[i])
12046 goto error;
12049 return set;
12050 error:
12051 isl_set_free(set);
12052 return NULL;
12055 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
12057 isl_size dim;
12058 int size = 0;
12060 dim = isl_basic_set_dim(bset, isl_dim_all);
12061 if (dim < 0)
12062 return -1;
12063 size += bset->n_eq * (1 + dim);
12064 size += bset->n_ineq * (1 + dim);
12065 size += bset->n_div * (2 + dim);
12067 return size;
12070 int isl_set_size(__isl_keep isl_set *set)
12072 int i;
12073 int size = 0;
12075 if (!set)
12076 return -1;
12078 for (i = 0; i < set->n; ++i)
12079 size += isl_basic_set_size(set->p[i]);
12081 return size;
12084 /* Check if there is any lower bound (if lower == 0) and/or upper
12085 * bound (if upper == 0) on the specified dim.
12087 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
12088 enum isl_dim_type type, unsigned pos, int lower, int upper)
12090 int i;
12091 isl_bool involves;
12092 isl_size off;
12094 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
12095 return isl_bool_error;
12097 off = isl_basic_map_var_offset(bmap, type);
12098 if (off < 0)
12099 return isl_bool_error;
12100 involves = isl_basic_map_any_div_involves_vars(bmap, off + pos, 1);
12101 if (involves < 0 || involves)
12102 return involves;
12104 for (i = 0; i < bmap->n_eq; ++i)
12105 if (!isl_int_is_zero(bmap->eq[i][1 + off + pos]))
12106 return isl_bool_true;
12108 for (i = 0; i < bmap->n_ineq; ++i) {
12109 int sgn = isl_int_sgn(bmap->ineq[i][1 + off + pos]);
12110 if (sgn > 0)
12111 lower = 1;
12112 if (sgn < 0)
12113 upper = 1;
12116 return lower && upper;
12119 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
12120 enum isl_dim_type type, unsigned pos)
12122 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
12125 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
12126 enum isl_dim_type type, unsigned pos)
12128 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
12131 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
12132 enum isl_dim_type type, unsigned pos)
12134 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
12137 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
12138 enum isl_dim_type type, unsigned pos)
12140 int i;
12142 if (!map)
12143 return isl_bool_error;
12145 for (i = 0; i < map->n; ++i) {
12146 isl_bool bounded;
12147 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
12148 if (bounded < 0 || !bounded)
12149 return bounded;
12152 return isl_bool_true;
12155 /* Return true if the specified dim is involved in both an upper bound
12156 * and a lower bound.
12158 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
12159 enum isl_dim_type type, unsigned pos)
12161 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
12164 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12166 static isl_bool has_any_bound(__isl_keep isl_map *map,
12167 enum isl_dim_type type, unsigned pos,
12168 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12169 enum isl_dim_type type, unsigned pos))
12171 int i;
12173 if (!map)
12174 return isl_bool_error;
12176 for (i = 0; i < map->n; ++i) {
12177 isl_bool bounded;
12178 bounded = fn(map->p[i], type, pos);
12179 if (bounded < 0 || bounded)
12180 return bounded;
12183 return isl_bool_false;
12186 /* Return 1 if the specified dim is involved in any lower bound.
12188 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
12189 enum isl_dim_type type, unsigned pos)
12191 return has_any_bound(set, type, pos,
12192 &isl_basic_map_dim_has_lower_bound);
12195 /* Return 1 if the specified dim is involved in any upper bound.
12197 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
12198 enum isl_dim_type type, unsigned pos)
12200 return has_any_bound(set, type, pos,
12201 &isl_basic_map_dim_has_upper_bound);
12204 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12206 static isl_bool has_bound(__isl_keep isl_map *map,
12207 enum isl_dim_type type, unsigned pos,
12208 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12209 enum isl_dim_type type, unsigned pos))
12211 int i;
12213 if (!map)
12214 return isl_bool_error;
12216 for (i = 0; i < map->n; ++i) {
12217 isl_bool bounded;
12218 bounded = fn(map->p[i], type, pos);
12219 if (bounded < 0 || !bounded)
12220 return bounded;
12223 return isl_bool_true;
12226 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12228 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
12229 enum isl_dim_type type, unsigned pos)
12231 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
12234 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12236 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
12237 enum isl_dim_type type, unsigned pos)
12239 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
12242 /* For each of the "n" variables starting at "first", determine
12243 * the sign of the variable and put the results in the first "n"
12244 * elements of the array "signs".
12245 * Sign
12246 * 1 means that the variable is non-negative
12247 * -1 means that the variable is non-positive
12248 * 0 means the variable attains both positive and negative values.
12250 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
12251 unsigned first, unsigned n, int *signs)
12253 isl_vec *bound = NULL;
12254 struct isl_tab *tab = NULL;
12255 struct isl_tab_undo *snap;
12256 int i;
12257 isl_size total;
12259 total = isl_basic_set_dim(bset, isl_dim_all);
12260 if (total < 0 || !signs)
12261 return isl_stat_error;
12263 bound = isl_vec_alloc(bset->ctx, 1 + total);
12264 tab = isl_tab_from_basic_set(bset, 0);
12265 if (!bound || !tab)
12266 goto error;
12268 isl_seq_clr(bound->el, bound->size);
12269 isl_int_set_si(bound->el[0], -1);
12271 snap = isl_tab_snap(tab);
12272 for (i = 0; i < n; ++i) {
12273 int empty;
12275 isl_int_set_si(bound->el[1 + first + i], -1);
12276 if (isl_tab_add_ineq(tab, bound->el) < 0)
12277 goto error;
12278 empty = tab->empty;
12279 isl_int_set_si(bound->el[1 + first + i], 0);
12280 if (isl_tab_rollback(tab, snap) < 0)
12281 goto error;
12283 if (empty) {
12284 signs[i] = 1;
12285 continue;
12288 isl_int_set_si(bound->el[1 + first + i], 1);
12289 if (isl_tab_add_ineq(tab, bound->el) < 0)
12290 goto error;
12291 empty = tab->empty;
12292 isl_int_set_si(bound->el[1 + first + i], 0);
12293 if (isl_tab_rollback(tab, snap) < 0)
12294 goto error;
12296 signs[i] = empty ? -1 : 0;
12299 isl_tab_free(tab);
12300 isl_vec_free(bound);
12301 return isl_stat_ok;
12302 error:
12303 isl_tab_free(tab);
12304 isl_vec_free(bound);
12305 return isl_stat_error;
12308 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
12309 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
12311 if (!bset || !signs)
12312 return isl_stat_error;
12313 if (isl_basic_set_check_range(bset, type, first, n) < 0)
12314 return isl_stat_error;
12316 first += pos(bset->dim, type) - 1;
12317 return isl_basic_set_vars_get_sign(bset, first, n, signs);
12320 /* Is it possible for the integer division "div" to depend (possibly
12321 * indirectly) on any output dimensions?
12323 * If the div is undefined, then we conservatively assume that it
12324 * may depend on them.
12325 * Otherwise, we check if it actually depends on them or on any integer
12326 * divisions that may depend on them.
12328 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
12330 int i;
12331 isl_size n_out, n_div;
12332 unsigned o_out, o_div;
12334 if (isl_int_is_zero(bmap->div[div][0]))
12335 return isl_bool_true;
12337 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12338 if (n_out < 0)
12339 return isl_bool_error;
12340 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12342 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
12343 return isl_bool_true;
12345 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12346 if (n_div < 0)
12347 return isl_bool_error;
12348 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12350 for (i = 0; i < n_div; ++i) {
12351 isl_bool may_involve;
12353 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
12354 continue;
12355 may_involve = div_may_involve_output(bmap, i);
12356 if (may_involve < 0 || may_involve)
12357 return may_involve;
12360 return isl_bool_false;
12363 /* Return the first integer division of "bmap" in the range
12364 * [first, first + n[ that may depend on any output dimensions and
12365 * that has a non-zero coefficient in "c" (where the first coefficient
12366 * in "c" corresponds to integer division "first").
12368 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
12369 isl_int *c, int first, int n)
12371 int k;
12373 if (!bmap)
12374 return -1;
12376 for (k = first; k < first + n; ++k) {
12377 isl_bool may_involve;
12379 if (isl_int_is_zero(c[k]))
12380 continue;
12381 may_involve = div_may_involve_output(bmap, k);
12382 if (may_involve < 0)
12383 return -1;
12384 if (may_involve)
12385 return k;
12388 return first + n;
12391 /* Look for a pair of inequality constraints in "bmap" of the form
12393 * -l + i >= 0 or i >= l
12394 * and
12395 * n + l - i >= 0 or i <= l + n
12397 * with n < "m" and i the output dimension at position "pos".
12398 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12399 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12400 * and earlier output dimensions, as well as integer divisions that do
12401 * not involve any of the output dimensions.
12403 * Return the index of the first inequality constraint or bmap->n_ineq
12404 * if no such pair can be found.
12406 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
12407 int pos, isl_int m)
12409 int i, j;
12410 isl_ctx *ctx;
12411 isl_size total;
12412 isl_size n_div, n_out;
12413 unsigned o_div, o_out;
12414 int less;
12416 total = isl_basic_map_dim(bmap, isl_dim_all);
12417 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12418 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12419 if (total < 0 || n_out < 0 || n_div < 0)
12420 return -1;
12422 ctx = isl_basic_map_get_ctx(bmap);
12423 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12424 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12425 for (i = 0; i < bmap->n_ineq; ++i) {
12426 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
12427 continue;
12428 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
12429 n_out - (pos + 1)) != -1)
12430 continue;
12431 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
12432 0, n_div) < n_div)
12433 continue;
12434 for (j = i + 1; j < bmap->n_ineq; ++j) {
12435 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
12436 ctx->one))
12437 continue;
12438 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
12439 bmap->ineq[j] + 1, total))
12440 continue;
12441 break;
12443 if (j >= bmap->n_ineq)
12444 continue;
12445 isl_int_add(bmap->ineq[i][0],
12446 bmap->ineq[i][0], bmap->ineq[j][0]);
12447 less = isl_int_abs_lt(bmap->ineq[i][0], m);
12448 isl_int_sub(bmap->ineq[i][0],
12449 bmap->ineq[i][0], bmap->ineq[j][0]);
12450 if (!less)
12451 continue;
12452 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
12453 return i;
12454 else
12455 return j;
12458 return bmap->n_ineq;
12461 /* Return the index of the equality of "bmap" that defines
12462 * the output dimension "pos" in terms of earlier dimensions.
12463 * The equality may also involve integer divisions, as long
12464 * as those integer divisions are defined in terms of
12465 * parameters or input dimensions.
12466 * In this case, *div is set to the number of integer divisions and
12467 * *ineq is set to the number of inequality constraints (provided
12468 * div and ineq are not NULL).
12470 * The equality may also involve a single integer division involving
12471 * the output dimensions (typically only output dimension "pos") as
12472 * long as the coefficient of output dimension "pos" is 1 or -1 and
12473 * there is a pair of constraints i >= l and i <= l + n, with i referring
12474 * to output dimension "pos", l an expression involving only earlier
12475 * dimensions and n smaller than the coefficient of the integer division
12476 * in the equality. In this case, the output dimension can be defined
12477 * in terms of a modulo expression that does not involve the integer division.
12478 * *div is then set to this single integer division and
12479 * *ineq is set to the index of constraint i >= l.
12481 * Return bmap->n_eq if there is no such equality.
12482 * Return -1 on error.
12484 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
12485 int pos, int *div, int *ineq)
12487 int j, k, l;
12488 isl_size n_div, n_out;
12489 unsigned o_div, o_out;
12491 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12492 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12493 if (n_out < 0 || n_div < 0)
12494 return -1;
12496 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12497 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12499 if (ineq)
12500 *ineq = bmap->n_ineq;
12501 if (div)
12502 *div = n_div;
12503 for (j = 0; j < bmap->n_eq; ++j) {
12504 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12505 continue;
12506 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
12507 n_out - (pos + 1)) != -1)
12508 continue;
12509 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12510 0, n_div);
12511 if (k >= n_div)
12512 return j;
12513 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12514 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12515 continue;
12516 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12517 k + 1, n_div - (k+1)) < n_div)
12518 continue;
12519 l = find_modulo_constraint_pair(bmap, pos,
12520 bmap->eq[j][o_div + k]);
12521 if (l < 0)
12522 return -1;
12523 if (l >= bmap->n_ineq)
12524 continue;
12525 if (div)
12526 *div = k;
12527 if (ineq)
12528 *ineq = l;
12529 return j;
12532 return bmap->n_eq;
12535 /* Check if the given basic map is obviously single-valued.
12536 * In particular, for each output dimension, check that there is
12537 * an equality that defines the output dimension in terms of
12538 * earlier dimensions.
12540 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12542 int i;
12543 isl_size n_out;
12545 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12546 if (n_out < 0)
12547 return isl_bool_error;
12549 for (i = 0; i < n_out; ++i) {
12550 int eq;
12552 eq = isl_basic_map_output_defining_equality(bmap, i,
12553 NULL, NULL);
12554 if (eq < 0)
12555 return isl_bool_error;
12556 if (eq >= bmap->n_eq)
12557 return isl_bool_false;
12560 return isl_bool_true;
12563 /* Check if the given basic map is single-valued.
12564 * We simply compute
12566 * M \circ M^-1
12568 * and check if the result is a subset of the identity mapping.
12570 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12572 isl_space *space;
12573 isl_basic_map *test;
12574 isl_basic_map *id;
12575 isl_bool sv;
12577 sv = isl_basic_map_plain_is_single_valued(bmap);
12578 if (sv < 0 || sv)
12579 return sv;
12581 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12582 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12584 space = isl_basic_map_get_space(bmap);
12585 space = isl_space_map_from_set(isl_space_range(space));
12586 id = isl_basic_map_identity(space);
12588 sv = isl_basic_map_is_subset(test, id);
12590 isl_basic_map_free(test);
12591 isl_basic_map_free(id);
12593 return sv;
12596 /* Check if the given map is obviously single-valued.
12598 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12600 if (!map)
12601 return isl_bool_error;
12602 if (map->n == 0)
12603 return isl_bool_true;
12604 if (map->n >= 2)
12605 return isl_bool_false;
12607 return isl_basic_map_plain_is_single_valued(map->p[0]);
12610 /* Check if the given map is single-valued.
12611 * We simply compute
12613 * M \circ M^-1
12615 * and check if the result is a subset of the identity mapping.
12617 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12619 isl_space *space;
12620 isl_map *test;
12621 isl_map *id;
12622 isl_bool sv;
12624 sv = isl_map_plain_is_single_valued(map);
12625 if (sv < 0 || sv)
12626 return sv;
12628 test = isl_map_reverse(isl_map_copy(map));
12629 test = isl_map_apply_range(test, isl_map_copy(map));
12631 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12632 id = isl_map_identity(space);
12634 sv = isl_map_is_subset(test, id);
12636 isl_map_free(test);
12637 isl_map_free(id);
12639 return sv;
12642 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12644 isl_bool in;
12646 map = isl_map_copy(map);
12647 map = isl_map_reverse(map);
12648 in = isl_map_is_single_valued(map);
12649 isl_map_free(map);
12651 return in;
12654 /* Check if the given map is obviously injective.
12656 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12658 isl_bool in;
12660 map = isl_map_copy(map);
12661 map = isl_map_reverse(map);
12662 in = isl_map_plain_is_single_valued(map);
12663 isl_map_free(map);
12665 return in;
12668 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12670 isl_bool sv;
12672 sv = isl_map_is_single_valued(map);
12673 if (sv < 0 || !sv)
12674 return sv;
12676 return isl_map_is_injective(map);
12679 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12681 return isl_map_is_single_valued(set_to_map(set));
12684 /* Does "map" only map elements to themselves?
12686 * If the domain and range spaces are different, then "map"
12687 * is considered not to be an identity relation, even if it is empty.
12688 * Otherwise, construct the maximal identity relation and
12689 * check whether "map" is a subset of this relation.
12691 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12693 isl_map *id;
12694 isl_bool equal, is_identity;
12696 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12697 if (equal < 0 || !equal)
12698 return equal;
12700 id = isl_map_identity(isl_map_get_space(map));
12701 is_identity = isl_map_is_subset(map, id);
12702 isl_map_free(id);
12704 return is_identity;
12707 int isl_map_is_translation(__isl_keep isl_map *map)
12709 int ok;
12710 isl_set *delta;
12712 delta = isl_map_deltas(isl_map_copy(map));
12713 ok = isl_set_is_singleton(delta);
12714 isl_set_free(delta);
12716 return ok;
12719 static int unique(isl_int *p, unsigned pos, unsigned len)
12721 if (isl_seq_first_non_zero(p, pos) != -1)
12722 return 0;
12723 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12724 return 0;
12725 return 1;
12728 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12730 int i, j;
12731 isl_size nvar, ovar, n_div;
12733 n_div = isl_basic_set_dim(bset, isl_dim_div);
12734 if (n_div < 0)
12735 return isl_bool_error;
12736 if (n_div != 0)
12737 return isl_bool_false;
12739 nvar = isl_basic_set_dim(bset, isl_dim_set);
12740 ovar = isl_space_offset(bset->dim, isl_dim_set);
12741 if (nvar < 0 || ovar < 0)
12742 return isl_bool_error;
12743 for (j = 0; j < nvar; ++j) {
12744 int lower = 0, upper = 0;
12745 for (i = 0; i < bset->n_eq; ++i) {
12746 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12747 continue;
12748 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12749 return isl_bool_false;
12750 break;
12752 if (i < bset->n_eq)
12753 continue;
12754 for (i = 0; i < bset->n_ineq; ++i) {
12755 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12756 continue;
12757 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12758 return isl_bool_false;
12759 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12760 lower = 1;
12761 else
12762 upper = 1;
12764 if (!lower || !upper)
12765 return isl_bool_false;
12768 return isl_bool_true;
12771 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12773 if (!set)
12774 return isl_bool_error;
12775 if (set->n != 1)
12776 return isl_bool_false;
12778 return isl_basic_set_is_box(set->p[0]);
12781 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12783 if (!bset)
12784 return isl_bool_error;
12786 return isl_space_is_wrapping(bset->dim);
12789 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12791 if (!set)
12792 return isl_bool_error;
12794 return isl_space_is_wrapping(set->dim);
12797 /* Modify the space of "map" through a call to "change".
12798 * If "can_change" is set (not NULL), then first call it to check
12799 * if the modification is allowed, printing the error message "cannot_change"
12800 * if it is not.
12802 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12803 isl_bool (*can_change)(__isl_keep isl_map *map),
12804 const char *cannot_change,
12805 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12807 isl_bool ok;
12808 isl_space *space;
12810 if (!map)
12811 return NULL;
12813 ok = can_change ? can_change(map) : isl_bool_true;
12814 if (ok < 0)
12815 return isl_map_free(map);
12816 if (!ok)
12817 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12818 return isl_map_free(map));
12820 space = change(isl_map_get_space(map));
12821 map = isl_map_reset_space(map, space);
12823 return map;
12826 /* Is the domain of "map" a wrapped relation?
12828 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12830 if (!map)
12831 return isl_bool_error;
12833 return isl_space_domain_is_wrapping(map->dim);
12836 /* Does "map" have a wrapped relation in both domain and range?
12838 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12840 return isl_space_is_product(isl_map_peek_space(map));
12843 /* Is the range of "map" a wrapped relation?
12845 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12847 if (!map)
12848 return isl_bool_error;
12850 return isl_space_range_is_wrapping(map->dim);
12853 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12855 isl_space *space;
12857 space = isl_basic_map_take_space(bmap);
12858 space = isl_space_wrap(space);
12859 bmap = isl_basic_map_restore_space(bmap, space);
12861 bmap = isl_basic_map_finalize(bmap);
12863 return bset_from_bmap(bmap);
12866 /* Given a map A -> B, return the set (A -> B).
12868 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12870 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12873 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12875 bset = isl_basic_set_cow(bset);
12876 if (!bset)
12877 return NULL;
12879 bset->dim = isl_space_unwrap(bset->dim);
12880 if (!bset->dim)
12881 goto error;
12883 bset = isl_basic_set_finalize(bset);
12885 return bset_to_bmap(bset);
12886 error:
12887 isl_basic_set_free(bset);
12888 return NULL;
12891 /* Given a set (A -> B), return the map A -> B.
12892 * Error out if "set" is not of the form (A -> B).
12894 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12896 return isl_map_change_space(set, &isl_set_is_wrapping,
12897 "not a wrapping set", &isl_space_unwrap);
12900 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12901 enum isl_dim_type type)
12903 isl_space *space;
12905 space = isl_basic_map_take_space(bmap);
12906 space = isl_space_reset(space, type);
12907 bmap = isl_basic_map_restore_space(bmap, space);
12909 bmap = isl_basic_map_mark_final(bmap);
12911 return bmap;
12914 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12915 enum isl_dim_type type)
12917 int i;
12918 isl_space *space;
12920 if (!map)
12921 return NULL;
12923 if (!isl_space_is_named_or_nested(map->dim, type))
12924 return map;
12926 map = isl_map_cow(map);
12927 if (!map)
12928 return NULL;
12930 for (i = 0; i < map->n; ++i) {
12931 map->p[i] = isl_basic_map_reset(map->p[i], type);
12932 if (!map->p[i])
12933 goto error;
12936 space = isl_map_take_space(map);
12937 space = isl_space_reset(space, type);
12938 map = isl_map_restore_space(map, space);
12940 return map;
12941 error:
12942 isl_map_free(map);
12943 return NULL;
12946 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12948 isl_space *space;
12950 space = isl_basic_map_take_space(bmap);
12951 space = isl_space_flatten(space);
12952 bmap = isl_basic_map_restore_space(bmap, space);
12954 bmap = isl_basic_map_mark_final(bmap);
12956 return bmap;
12959 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12961 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12964 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12965 __isl_take isl_basic_map *bmap)
12967 isl_space *space;
12969 space = isl_basic_map_take_space(bmap);
12970 space = isl_space_flatten_domain(space);
12971 bmap = isl_basic_map_restore_space(bmap, space);
12973 bmap = isl_basic_map_mark_final(bmap);
12975 return bmap;
12978 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12979 __isl_take isl_basic_map *bmap)
12981 isl_space *space;
12983 space = isl_basic_map_take_space(bmap);
12984 space = isl_space_flatten_range(space);
12985 bmap = isl_basic_map_restore_space(bmap, space);
12987 bmap = isl_basic_map_mark_final(bmap);
12989 return bmap;
12992 /* Remove any internal structure from the spaces of domain and range of "map".
12994 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12996 if (!map)
12997 return NULL;
12999 if (!map->dim->nested[0] && !map->dim->nested[1])
13000 return map;
13002 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
13005 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
13007 return set_from_map(isl_map_flatten(set_to_map(set)));
13010 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
13012 isl_space *space, *flat_space;
13013 isl_map *map;
13015 space = isl_set_get_space(set);
13016 flat_space = isl_space_flatten(isl_space_copy(space));
13017 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
13018 flat_space));
13019 map = isl_map_intersect_domain(map, set);
13021 return map;
13024 /* Remove any internal structure from the space of the domain of "map".
13026 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
13028 if (!map)
13029 return NULL;
13031 if (!map->dim->nested[0])
13032 return map;
13034 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
13037 /* Remove any internal structure from the space of the range of "map".
13039 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
13041 if (!map)
13042 return NULL;
13044 if (!map->dim->nested[1])
13045 return map;
13047 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
13050 /* Reorder the dimensions of "bmap" according to the given dim_map
13051 * and set the dimension specification to "space" and
13052 * perform Gaussian elimination on the result.
13054 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
13055 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
13057 isl_basic_map *res;
13058 unsigned flags;
13059 isl_size n_div;
13061 n_div = isl_basic_map_dim(bmap, isl_dim_div);
13062 if (n_div < 0 || !space || !dim_map)
13063 goto error;
13065 flags = bmap->flags;
13066 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
13067 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
13068 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
13069 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
13070 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
13071 if (res)
13072 res->flags = flags;
13073 res = isl_basic_map_gauss(res, NULL);
13074 res = isl_basic_map_finalize(res);
13075 return res;
13076 error:
13077 isl_dim_map_free(dim_map);
13078 isl_basic_map_free(bmap);
13079 isl_space_free(space);
13080 return NULL;
13083 /* Reorder the dimensions of "map" according to given reordering.
13085 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
13086 __isl_take isl_reordering *r)
13088 int i;
13089 struct isl_dim_map *dim_map;
13091 map = isl_map_cow(map);
13092 dim_map = isl_dim_map_from_reordering(r);
13093 if (!map || !r || !dim_map)
13094 goto error;
13096 for (i = 0; i < map->n; ++i) {
13097 struct isl_dim_map *dim_map_i;
13098 isl_space *space;
13100 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
13102 space = isl_reordering_get_space(r);
13103 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
13105 if (!map->p[i])
13106 goto error;
13109 map = isl_map_reset_space(map, isl_reordering_get_space(r));
13110 map = isl_map_unmark_normalized(map);
13112 isl_reordering_free(r);
13113 isl_dim_map_free(dim_map);
13114 return map;
13115 error:
13116 isl_dim_map_free(dim_map);
13117 isl_map_free(map);
13118 isl_reordering_free(r);
13119 return NULL;
13122 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
13123 __isl_take isl_reordering *r)
13125 return set_from_map(isl_map_realign(set_to_map(set), r));
13128 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
13129 __isl_take isl_space *model)
13131 isl_ctx *ctx;
13132 isl_bool aligned;
13134 if (!map || !model)
13135 goto error;
13137 ctx = isl_space_get_ctx(model);
13138 if (!isl_space_has_named_params(model))
13139 isl_die(ctx, isl_error_invalid,
13140 "model has unnamed parameters", goto error);
13141 if (isl_map_check_named_params(map) < 0)
13142 goto error;
13143 aligned = isl_map_space_has_equal_params(map, model);
13144 if (aligned < 0)
13145 goto error;
13146 if (!aligned) {
13147 isl_space *space;
13148 isl_reordering *exp;
13150 space = isl_map_peek_space(map);
13151 exp = isl_parameter_alignment_reordering(space, model);
13152 map = isl_map_realign(map, exp);
13155 isl_space_free(model);
13156 return map;
13157 error:
13158 isl_space_free(model);
13159 isl_map_free(map);
13160 return NULL;
13163 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
13164 __isl_take isl_space *model)
13166 return isl_map_align_params(set, model);
13169 /* Align the parameters of "bmap" to those of "model", introducing
13170 * additional parameters if needed.
13172 __isl_give isl_basic_map *isl_basic_map_align_params(
13173 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
13175 isl_ctx *ctx;
13176 isl_bool equal_params;
13177 isl_space *bmap_space;
13179 if (!bmap || !model)
13180 goto error;
13182 ctx = isl_space_get_ctx(model);
13183 if (!isl_space_has_named_params(model))
13184 isl_die(ctx, isl_error_invalid,
13185 "model has unnamed parameters", goto error);
13186 if (isl_basic_map_check_named_params(bmap) < 0)
13187 goto error;
13188 bmap_space = isl_basic_map_peek_space(bmap);
13189 equal_params = isl_space_has_equal_params(bmap_space, model);
13190 if (equal_params < 0)
13191 goto error;
13192 if (!equal_params) {
13193 isl_reordering *exp;
13194 struct isl_dim_map *dim_map;
13196 exp = isl_parameter_alignment_reordering(bmap_space, model);
13197 dim_map = isl_dim_map_from_reordering(exp);
13198 bmap = isl_basic_map_realign(bmap,
13199 isl_reordering_get_space(exp),
13200 isl_dim_map_extend(dim_map, bmap));
13201 isl_reordering_free(exp);
13202 isl_dim_map_free(dim_map);
13205 isl_space_free(model);
13206 return bmap;
13207 error:
13208 isl_space_free(model);
13209 isl_basic_map_free(bmap);
13210 return NULL;
13213 /* Do "bset" and "space" have the same parameters?
13215 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
13216 __isl_keep isl_space *space)
13218 isl_space *bset_space;
13220 bset_space = isl_basic_set_peek_space(bset);
13221 return isl_space_has_equal_params(bset_space, space);
13224 /* Do "map" and "space" have the same parameters?
13226 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
13227 __isl_keep isl_space *space)
13229 isl_space *map_space;
13231 map_space = isl_map_peek_space(map);
13232 return isl_space_has_equal_params(map_space, space);
13235 /* Do "set" and "space" have the same parameters?
13237 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
13238 __isl_keep isl_space *space)
13240 return isl_map_space_has_equal_params(set_to_map(set), space);
13243 /* Align the parameters of "bset" to those of "model", introducing
13244 * additional parameters if needed.
13246 __isl_give isl_basic_set *isl_basic_set_align_params(
13247 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
13249 return isl_basic_map_align_params(bset, model);
13252 #undef TYPE
13253 #define TYPE isl_map
13254 #define isl_map_drop_dims isl_map_drop
13255 #include "isl_drop_unused_params_templ.c"
13257 /* Drop all parameters not referenced by "set".
13259 __isl_give isl_set *isl_set_drop_unused_params(
13260 __isl_take isl_set *set)
13262 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
13265 #undef TYPE
13266 #define TYPE isl_basic_map
13267 #define isl_basic_map_drop_dims isl_basic_map_drop
13268 #include "isl_drop_unused_params_templ.c"
13270 /* Drop all parameters not referenced by "bset".
13272 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
13273 __isl_take isl_basic_set *bset)
13275 return bset_from_bmap(isl_basic_map_drop_unused_params(
13276 bset_to_bmap(bset)));
13279 /* Given a tuple of identifiers "tuple" in a space that corresponds
13280 * to that of "set", if any of those identifiers appear as parameters
13281 * in "set", then equate those parameters with the corresponding
13282 * set dimensions and project out the parameters.
13283 * The result therefore has no such parameters.
13285 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
13286 __isl_keep isl_multi_id *tuple)
13288 int i;
13289 isl_size n;
13290 isl_space *set_space, *tuple_space;
13292 set_space = isl_set_peek_space(set);
13293 tuple_space = isl_multi_id_peek_space(tuple);
13294 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
13295 return isl_set_free(set);
13296 n = isl_multi_id_size(tuple);
13297 if (n < 0)
13298 return isl_set_free(set);
13299 for (i = 0; i < n; ++i) {
13300 isl_id *id;
13301 int pos;
13303 id = isl_multi_id_get_at(tuple, i);
13304 if (!id)
13305 return isl_set_free(set);
13306 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
13307 isl_id_free(id);
13308 if (pos < 0)
13309 continue;
13310 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
13311 set = isl_set_project_out(set, isl_dim_param, pos, 1);
13313 return set;
13316 /* Bind the set dimensions of "set" to parameters with identifiers
13317 * specified by "tuple", living in the same space as "set".
13319 * If no parameters with these identifiers appear in "set" already,
13320 * then the set dimensions are simply reinterpreted as parameters.
13321 * Otherwise, the parameters are first equated to the corresponding
13322 * set dimensions.
13324 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
13325 __isl_take isl_multi_id *tuple)
13327 isl_space *space;
13329 set = equate_params(set, tuple);
13330 space = isl_set_get_space(set);
13331 space = isl_space_bind_set(space, tuple);
13332 isl_multi_id_free(tuple);
13333 set = isl_set_reset_space(set, space);
13335 return set;
13338 /* Given a tuple of identifiers "tuple" in a space that corresponds
13339 * to the domain of "map", if any of those identifiers appear as parameters
13340 * in "map", then equate those parameters with the corresponding
13341 * input dimensions and project out the parameters.
13342 * The result therefore has no such parameters.
13344 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
13345 __isl_keep isl_multi_id *tuple)
13347 int i;
13348 isl_size n;
13349 isl_space *map_space, *tuple_space;
13351 map_space = isl_map_peek_space(map);
13352 tuple_space = isl_multi_id_peek_space(tuple);
13353 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
13354 return isl_map_free(map);
13355 n = isl_multi_id_size(tuple);
13356 if (n < 0)
13357 return isl_map_free(map);
13358 for (i = 0; i < n; ++i) {
13359 isl_id *id;
13360 int pos;
13362 id = isl_multi_id_get_at(tuple, i);
13363 if (!id)
13364 return isl_map_free(map);
13365 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
13366 isl_id_free(id);
13367 if (pos < 0)
13368 continue;
13369 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
13370 map = isl_map_project_out(map, isl_dim_param, pos, 1);
13372 return map;
13375 /* Bind the input dimensions of "map" to parameters with identifiers
13376 * specified by "tuple", living in the domain space of "map".
13378 * If no parameters with these identifiers appear in "map" already,
13379 * then the input dimensions are simply reinterpreted as parameters.
13380 * Otherwise, the parameters are first equated to the corresponding
13381 * input dimensions.
13383 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
13384 __isl_take isl_multi_id *tuple)
13386 isl_space *space;
13387 isl_set *set;
13389 map = map_equate_params(map, tuple);
13390 space = isl_map_get_space(map);
13391 space = isl_space_bind_map_domain(space, tuple);
13392 isl_multi_id_free(tuple);
13393 set = set_from_map(isl_map_reset_space(map, space));
13395 return set;
13398 /* Bind the output dimensions of "map" to parameters with identifiers
13399 * specified by "tuple", living in the range space of "map".
13401 * Since binding is more easily implemented on the domain,
13402 * bind the input dimensions of the inverse of "map".
13404 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
13405 __isl_take isl_multi_id *tuple)
13407 return isl_map_bind_domain(isl_map_reverse(map), tuple);
13410 /* Insert a domain corresponding to "tuple"
13411 * into the nullary or unary relation "set".
13412 * The result has an extra initial tuple and is therefore
13413 * either a unary or binary relation.
13414 * Any parameters with identifiers in "tuple" are reinterpreted
13415 * as the corresponding domain dimensions.
13417 static __isl_give isl_map *unbind_params_insert_domain(
13418 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
13420 isl_space *space;
13421 isl_reordering *r;
13423 space = isl_set_peek_space(set);
13424 r = isl_reordering_unbind_params_insert_domain(space, tuple);
13425 isl_multi_id_free(tuple);
13427 return isl_map_realign(set_to_map(set), r);
13430 /* Construct a set with "tuple" as domain from the parameter domain "set".
13431 * Any parameters with identifiers in "tuple" are reinterpreted
13432 * as the corresponding set dimensions.
13434 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
13435 __isl_take isl_multi_id *tuple)
13437 isl_bool is_params;
13439 is_params = isl_set_is_params(set);
13440 if (is_params < 0)
13441 set = isl_set_free(set);
13442 else if (!is_params)
13443 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13444 "expecting parameter domain", set = isl_set_free(set));
13445 return set_from_map(unbind_params_insert_domain(set, tuple));
13448 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13450 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
13452 isl_bool is_params;
13454 is_params = isl_set_is_params(set);
13455 if (is_params < 0)
13456 return isl_stat_error;
13457 else if (is_params)
13458 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13459 "expecting proper set", return isl_stat_error);
13461 return isl_stat_ok;
13464 /* Construct a map with "domain" as domain and "set" as range.
13465 * Any parameters with identifiers in "domain" are reinterpreted
13466 * as the corresponding domain dimensions.
13468 __isl_give isl_map *isl_set_unbind_params_insert_domain(
13469 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
13471 if (isl_set_check_is_set(set) < 0)
13472 set = isl_set_free(set);
13473 return unbind_params_insert_domain(set, domain);
13476 /* Construct a map with "domain" as domain and "set" as range.
13478 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
13479 __isl_take isl_space *domain)
13481 isl_size dim;
13482 isl_space *space;
13483 isl_map *map;
13485 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
13486 domain = isl_space_free(domain);
13487 dim = isl_space_dim(domain, isl_dim_set);
13488 if (dim < 0)
13489 domain = isl_space_free(domain);
13490 space = isl_set_get_space(set);
13491 domain = isl_space_replace_params(domain, space);
13492 space = isl_space_map_from_domain_and_range(domain, space);
13494 map = isl_map_from_range(set);
13495 map = isl_map_add_dims(map, isl_dim_in, dim);
13496 map = isl_map_reset_space(map, space);
13498 return map;
13501 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13502 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13503 enum isl_dim_type c2, enum isl_dim_type c3,
13504 enum isl_dim_type c4, enum isl_dim_type c5)
13506 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13507 struct isl_mat *mat;
13508 int i, j, k;
13509 int pos;
13510 isl_size total;
13512 total = isl_basic_map_dim(bmap, isl_dim_all);
13513 if (total < 0)
13514 return NULL;
13515 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13516 if (!mat)
13517 return NULL;
13518 for (i = 0; i < bmap->n_eq; ++i)
13519 for (j = 0, pos = 0; j < 5; ++j) {
13520 int off = isl_basic_map_offset(bmap, c[j]);
13521 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13522 if (dim < 0)
13523 return isl_mat_free(mat);
13524 for (k = 0; k < dim; ++k) {
13525 isl_int_set(mat->row[i][pos],
13526 bmap->eq[i][off + k]);
13527 ++pos;
13531 return mat;
13534 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13535 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13536 enum isl_dim_type c2, enum isl_dim_type c3,
13537 enum isl_dim_type c4, enum isl_dim_type c5)
13539 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13540 struct isl_mat *mat;
13541 int i, j, k;
13542 int pos;
13543 isl_size total;
13545 total = isl_basic_map_dim(bmap, isl_dim_all);
13546 if (total < 0)
13547 return NULL;
13548 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13549 if (!mat)
13550 return NULL;
13551 for (i = 0; i < bmap->n_ineq; ++i)
13552 for (j = 0, pos = 0; j < 5; ++j) {
13553 int off = isl_basic_map_offset(bmap, c[j]);
13554 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13555 if (dim < 0)
13556 return isl_mat_free(mat);
13557 for (k = 0; k < dim; ++k) {
13558 isl_int_set(mat->row[i][pos],
13559 bmap->ineq[i][off + k]);
13560 ++pos;
13564 return mat;
13567 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13568 __isl_take isl_space *space,
13569 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13570 enum isl_dim_type c2, enum isl_dim_type c3,
13571 enum isl_dim_type c4, enum isl_dim_type c5)
13573 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13574 isl_basic_map *bmap = NULL;
13575 isl_size dim;
13576 unsigned total;
13577 unsigned extra;
13578 int i, j, k, l;
13579 int pos;
13581 dim = isl_space_dim(space, isl_dim_all);
13582 if (dim < 0 || !eq || !ineq)
13583 goto error;
13585 if (eq->n_col != ineq->n_col)
13586 isl_die(space->ctx, isl_error_invalid,
13587 "equalities and inequalities matrices should have "
13588 "same number of columns", goto error);
13590 total = 1 + dim;
13592 if (eq->n_col < total)
13593 isl_die(space->ctx, isl_error_invalid,
13594 "number of columns too small", goto error);
13596 extra = eq->n_col - total;
13598 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13599 eq->n_row, ineq->n_row);
13600 if (!bmap)
13601 goto error;
13602 for (i = 0; i < extra; ++i) {
13603 k = isl_basic_map_alloc_div(bmap);
13604 if (k < 0)
13605 goto error;
13606 isl_int_set_si(bmap->div[k][0], 0);
13608 for (i = 0; i < eq->n_row; ++i) {
13609 l = isl_basic_map_alloc_equality(bmap);
13610 if (l < 0)
13611 goto error;
13612 for (j = 0, pos = 0; j < 5; ++j) {
13613 int off = isl_basic_map_offset(bmap, c[j]);
13614 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13615 if (dim < 0)
13616 goto error;
13617 for (k = 0; k < dim; ++k) {
13618 isl_int_set(bmap->eq[l][off + k],
13619 eq->row[i][pos]);
13620 ++pos;
13624 for (i = 0; i < ineq->n_row; ++i) {
13625 l = isl_basic_map_alloc_inequality(bmap);
13626 if (l < 0)
13627 goto error;
13628 for (j = 0, pos = 0; j < 5; ++j) {
13629 int off = isl_basic_map_offset(bmap, c[j]);
13630 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13631 if (dim < 0)
13632 goto error;
13633 for (k = 0; k < dim; ++k) {
13634 isl_int_set(bmap->ineq[l][off + k],
13635 ineq->row[i][pos]);
13636 ++pos;
13641 isl_space_free(space);
13642 isl_mat_free(eq);
13643 isl_mat_free(ineq);
13645 bmap = isl_basic_map_simplify(bmap);
13646 return isl_basic_map_finalize(bmap);
13647 error:
13648 isl_space_free(space);
13649 isl_mat_free(eq);
13650 isl_mat_free(ineq);
13651 isl_basic_map_free(bmap);
13652 return NULL;
13655 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13656 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13657 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13659 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13660 c1, c2, c3, c4, isl_dim_in);
13663 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13664 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13665 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13667 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13668 c1, c2, c3, c4, isl_dim_in);
13671 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13672 __isl_take isl_space *space,
13673 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13674 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13676 isl_basic_map *bmap;
13677 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13678 c1, c2, c3, c4, isl_dim_in);
13679 return bset_from_bmap(bmap);
13682 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13684 if (!bmap)
13685 return isl_bool_error;
13687 return isl_space_can_zip(bmap->dim);
13690 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13692 if (!map)
13693 return isl_bool_error;
13695 return isl_space_can_zip(map->dim);
13698 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13699 * (A -> C) -> (B -> D).
13701 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13703 unsigned pos;
13704 isl_size n_in;
13705 isl_size n1;
13706 isl_size n2;
13708 if (!bmap)
13709 return NULL;
13711 if (!isl_basic_map_can_zip(bmap))
13712 isl_die(bmap->ctx, isl_error_invalid,
13713 "basic map cannot be zipped", goto error);
13714 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13715 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13716 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13717 if (n_in < 0 || n1 < 0 || n2 < 0)
13718 return isl_basic_map_free(bmap);
13719 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13720 bmap = isl_basic_map_cow(bmap);
13721 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13722 if (!bmap)
13723 return NULL;
13724 bmap->dim = isl_space_zip(bmap->dim);
13725 if (!bmap->dim)
13726 goto error;
13727 bmap = isl_basic_map_mark_final(bmap);
13728 return bmap;
13729 error:
13730 isl_basic_map_free(bmap);
13731 return NULL;
13734 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13735 * (A -> C) -> (B -> D).
13737 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13739 if (!map)
13740 return NULL;
13742 if (!isl_map_can_zip(map))
13743 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13744 goto error);
13746 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13747 error:
13748 isl_map_free(map);
13749 return NULL;
13752 /* Can we apply isl_basic_map_curry to "bmap"?
13753 * That is, does it have a nested relation in its domain?
13755 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13757 if (!bmap)
13758 return isl_bool_error;
13760 return isl_space_can_curry(bmap->dim);
13763 /* Can we apply isl_map_curry to "map"?
13764 * That is, does it have a nested relation in its domain?
13766 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13768 if (!map)
13769 return isl_bool_error;
13771 return isl_space_can_curry(map->dim);
13774 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13775 * A -> (B -> C).
13777 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13780 if (!bmap)
13781 return NULL;
13783 if (!isl_basic_map_can_curry(bmap))
13784 isl_die(bmap->ctx, isl_error_invalid,
13785 "basic map cannot be curried", goto error);
13786 bmap = isl_basic_map_cow(bmap);
13787 if (!bmap)
13788 return NULL;
13789 bmap->dim = isl_space_curry(bmap->dim);
13790 if (!bmap->dim)
13791 goto error;
13792 bmap = isl_basic_map_mark_final(bmap);
13793 return bmap;
13794 error:
13795 isl_basic_map_free(bmap);
13796 return NULL;
13799 /* Given a map (A -> B) -> C, return the corresponding map
13800 * A -> (B -> C).
13802 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13804 return isl_map_change_space(map, &isl_map_can_curry,
13805 "map cannot be curried", &isl_space_curry);
13808 /* Can isl_map_range_curry be applied to "map"?
13809 * That is, does it have a nested relation in its range,
13810 * the domain of which is itself a nested relation?
13812 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13814 if (!map)
13815 return isl_bool_error;
13817 return isl_space_can_range_curry(map->dim);
13820 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13821 * A -> (B -> (C -> D)).
13823 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13825 return isl_map_change_space(map, &isl_map_can_range_curry,
13826 "map range cannot be curried",
13827 &isl_space_range_curry);
13830 /* Can we apply isl_basic_map_uncurry to "bmap"?
13831 * That is, does it have a nested relation in its domain?
13833 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13835 if (!bmap)
13836 return isl_bool_error;
13838 return isl_space_can_uncurry(bmap->dim);
13841 /* Can we apply isl_map_uncurry to "map"?
13842 * That is, does it have a nested relation in its domain?
13844 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13846 if (!map)
13847 return isl_bool_error;
13849 return isl_space_can_uncurry(map->dim);
13852 /* Given a basic map A -> (B -> C), return the corresponding basic map
13853 * (A -> B) -> C.
13855 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13858 if (!bmap)
13859 return NULL;
13861 if (!isl_basic_map_can_uncurry(bmap))
13862 isl_die(bmap->ctx, isl_error_invalid,
13863 "basic map cannot be uncurried",
13864 return isl_basic_map_free(bmap));
13865 bmap = isl_basic_map_cow(bmap);
13866 if (!bmap)
13867 return NULL;
13868 bmap->dim = isl_space_uncurry(bmap->dim);
13869 if (!bmap->dim)
13870 return isl_basic_map_free(bmap);
13871 bmap = isl_basic_map_mark_final(bmap);
13872 return bmap;
13875 /* Given a map A -> (B -> C), return the corresponding map
13876 * (A -> B) -> C.
13878 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13880 return isl_map_change_space(map, &isl_map_can_uncurry,
13881 "map cannot be uncurried", &isl_space_uncurry);
13884 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13885 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13887 return isl_map_equate(set, type1, pos1, type2, pos2);
13890 /* Construct a basic map where the given dimensions are equal to each other.
13892 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13893 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13895 isl_basic_map *bmap = NULL;
13896 int i;
13897 isl_size total;
13899 total = isl_space_dim(space, isl_dim_all);
13900 if (total < 0 ||
13901 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13902 isl_space_check_range(space, type2, pos2, 1) < 0)
13903 goto error;
13905 if (type1 == type2 && pos1 == pos2)
13906 return isl_basic_map_universe(space);
13908 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13909 i = isl_basic_map_alloc_equality(bmap);
13910 if (i < 0)
13911 goto error;
13912 isl_seq_clr(bmap->eq[i], 1 + total);
13913 pos1 += isl_basic_map_offset(bmap, type1);
13914 pos2 += isl_basic_map_offset(bmap, type2);
13915 isl_int_set_si(bmap->eq[i][pos1], -1);
13916 isl_int_set_si(bmap->eq[i][pos2], 1);
13917 bmap = isl_basic_map_finalize(bmap);
13918 isl_space_free(space);
13919 return bmap;
13920 error:
13921 isl_space_free(space);
13922 isl_basic_map_free(bmap);
13923 return NULL;
13926 /* Add a constraint imposing that the given two dimensions are equal.
13928 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13929 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13931 isl_basic_map *eq;
13933 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13935 bmap = isl_basic_map_intersect(bmap, eq);
13937 return bmap;
13940 /* Add a constraint imposing that the given two dimensions are equal.
13942 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13943 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13945 isl_basic_map *bmap;
13947 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13949 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13951 return map;
13954 /* Add a constraint imposing that the given two dimensions have opposite values.
13956 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13957 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13959 isl_basic_map *bmap = NULL;
13960 int i;
13961 isl_size total;
13963 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13964 return isl_map_free(map);
13965 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13966 return isl_map_free(map);
13968 total = isl_map_dim(map, isl_dim_all);
13969 if (total < 0)
13970 return isl_map_free(map);
13971 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13972 i = isl_basic_map_alloc_equality(bmap);
13973 if (i < 0)
13974 goto error;
13975 isl_seq_clr(bmap->eq[i], 1 + total);
13976 pos1 += isl_basic_map_offset(bmap, type1);
13977 pos2 += isl_basic_map_offset(bmap, type2);
13978 isl_int_set_si(bmap->eq[i][pos1], 1);
13979 isl_int_set_si(bmap->eq[i][pos2], 1);
13980 bmap = isl_basic_map_finalize(bmap);
13982 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13984 return map;
13985 error:
13986 isl_basic_map_free(bmap);
13987 isl_map_free(map);
13988 return NULL;
13991 /* Construct a constraint imposing that the value of the first dimension is
13992 * greater than or equal to that of the second.
13994 static __isl_give isl_constraint *constraint_order_ge(
13995 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13996 enum isl_dim_type type2, int pos2)
13998 isl_constraint *c;
14000 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14001 isl_space_check_range(space, type2, pos2, 1) < 0)
14002 space = isl_space_free(space);
14003 if (!space)
14004 return NULL;
14006 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
14008 if (type1 == type2 && pos1 == pos2)
14009 return c;
14011 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
14012 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
14014 return c;
14017 /* Add a constraint imposing that the value of the first dimension is
14018 * greater than or equal to that of the second.
14020 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
14021 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14023 isl_constraint *c;
14024 isl_space *space;
14026 if (type1 == type2 && pos1 == pos2)
14027 return bmap;
14028 space = isl_basic_map_get_space(bmap);
14029 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14030 bmap = isl_basic_map_add_constraint(bmap, c);
14032 return bmap;
14035 /* Add a constraint imposing that the value of the first dimension is
14036 * greater than or equal to that of the second.
14038 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
14039 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14041 isl_constraint *c;
14042 isl_space *space;
14044 if (type1 == type2 && pos1 == pos2)
14045 return map;
14046 space = isl_map_get_space(map);
14047 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14048 map = isl_map_add_constraint(map, c);
14050 return map;
14053 /* Add a constraint imposing that the value of the first dimension is
14054 * less than or equal to that of the second.
14056 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
14057 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14059 return isl_map_order_ge(map, type2, pos2, type1, pos1);
14062 /* Construct a basic map where the value of the first dimension is
14063 * greater than that of the second.
14065 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
14066 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14068 isl_basic_map *bmap = NULL;
14069 int i;
14070 isl_size total;
14072 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14073 isl_space_check_range(space, type2, pos2, 1) < 0)
14074 goto error;
14076 if (type1 == type2 && pos1 == pos2)
14077 return isl_basic_map_empty(space);
14079 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
14080 total = isl_basic_map_dim(bmap, isl_dim_all);
14081 i = isl_basic_map_alloc_inequality(bmap);
14082 if (total < 0 || i < 0)
14083 return isl_basic_map_free(bmap);
14084 isl_seq_clr(bmap->ineq[i], 1 + total);
14085 pos1 += isl_basic_map_offset(bmap, type1);
14086 pos2 += isl_basic_map_offset(bmap, type2);
14087 isl_int_set_si(bmap->ineq[i][pos1], 1);
14088 isl_int_set_si(bmap->ineq[i][pos2], -1);
14089 isl_int_set_si(bmap->ineq[i][0], -1);
14090 bmap = isl_basic_map_finalize(bmap);
14092 return bmap;
14093 error:
14094 isl_space_free(space);
14095 isl_basic_map_free(bmap);
14096 return NULL;
14099 /* Add a constraint imposing that the value of the first dimension is
14100 * greater than that of the second.
14102 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
14103 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14105 isl_basic_map *gt;
14107 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
14109 bmap = isl_basic_map_intersect(bmap, gt);
14111 return bmap;
14114 /* Add a constraint imposing that the value of the first dimension is
14115 * greater than that of the second.
14117 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
14118 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14120 isl_basic_map *bmap;
14122 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
14124 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
14126 return map;
14129 /* Add a constraint imposing that the value of the first dimension is
14130 * smaller than that of the second.
14132 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
14133 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14135 return isl_map_order_gt(map, type2, pos2, type1, pos1);
14138 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
14139 int pos)
14141 isl_aff *div;
14142 isl_local_space *ls;
14144 if (!bmap)
14145 return NULL;
14147 if (!isl_basic_map_divs_known(bmap))
14148 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14149 "some divs are unknown", return NULL);
14151 ls = isl_basic_map_get_local_space(bmap);
14152 div = isl_local_space_get_div(ls, pos);
14153 isl_local_space_free(ls);
14155 return div;
14158 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
14159 int pos)
14161 return isl_basic_map_get_div(bset, pos);
14164 /* Plug in "subs" for set dimension "pos" of "set".
14166 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
14167 unsigned pos, __isl_keep isl_aff *subs)
14169 isl_multi_aff *ma;
14171 if (set && isl_set_plain_is_empty(set))
14172 return set;
14174 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
14175 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
14176 return isl_set_preimage_multi_aff(set, ma);
14179 /* Check if the range of "ma" is compatible with the domain or range
14180 * (depending on "type") of "bmap".
14182 static isl_stat check_basic_map_compatible_range_multi_aff(
14183 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
14184 __isl_keep isl_multi_aff *ma)
14186 isl_bool m;
14187 isl_space *ma_space;
14189 ma_space = isl_multi_aff_get_space(ma);
14191 m = isl_space_has_equal_params(bmap->dim, ma_space);
14192 if (m < 0)
14193 goto error;
14194 if (!m)
14195 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14196 "parameters don't match", goto error);
14197 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
14198 if (m < 0)
14199 goto error;
14200 if (!m)
14201 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14202 "spaces don't match", goto error);
14204 isl_space_free(ma_space);
14205 return isl_stat_ok;
14206 error:
14207 isl_space_free(ma_space);
14208 return isl_stat_error;
14211 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14212 * coefficients before the transformed range of dimensions,
14213 * the "n_after" coefficients after the transformed range of dimensions
14214 * and the coefficients of the other divs in "bmap".
14216 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
14217 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
14219 int i;
14220 isl_size n_param;
14221 isl_size n_set;
14222 isl_local_space *ls;
14224 if (n_div == 0)
14225 return bmap;
14227 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
14228 n_param = isl_local_space_dim(ls, isl_dim_param);
14229 n_set = isl_local_space_dim(ls, isl_dim_set);
14230 if (n_param < 0 || n_set < 0)
14231 return isl_basic_map_free(bmap);
14233 for (i = 0; i < n_div; ++i) {
14234 int o_bmap = 0, o_ls = 0;
14236 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
14237 o_bmap += 1 + 1 + n_param;
14238 o_ls += 1 + 1 + n_param;
14239 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
14240 o_bmap += n_before;
14241 isl_seq_cpy(bmap->div[i] + o_bmap,
14242 ls->div->row[i] + o_ls, n_set);
14243 o_bmap += n_set;
14244 o_ls += n_set;
14245 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
14246 o_bmap += n_after;
14247 isl_seq_cpy(bmap->div[i] + o_bmap,
14248 ls->div->row[i] + o_ls, n_div);
14249 o_bmap += n_div;
14250 o_ls += n_div;
14251 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
14252 bmap = isl_basic_map_add_div_constraints(bmap, i);
14253 if (!bmap)
14254 goto error;
14257 isl_local_space_free(ls);
14258 return bmap;
14259 error:
14260 isl_local_space_free(ls);
14261 return isl_basic_map_free(bmap);
14264 /* How many stride constraints does "ma" enforce?
14265 * That is, how many of the affine expressions have a denominator
14266 * different from one?
14268 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
14270 int i;
14271 int strides = 0;
14273 for (i = 0; i < ma->n; ++i)
14274 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
14275 strides++;
14277 return strides;
14280 /* For each affine expression in ma of the form
14282 * x_i = (f_i y + h_i)/m_i
14284 * with m_i different from one, add a constraint to "bmap"
14285 * of the form
14287 * f_i y + h_i = m_i alpha_i
14289 * with alpha_i an additional existentially quantified variable.
14291 * The input variables of "ma" correspond to a subset of the variables
14292 * of "bmap". There are "n_before" variables in "bmap" before this
14293 * subset and "n_after" variables after this subset.
14294 * The integer divisions of the affine expressions in "ma" are assumed
14295 * to have been aligned. There are "n_div_ma" of them and
14296 * they appear first in "bmap", straight after the "n_after" variables.
14298 static __isl_give isl_basic_map *add_ma_strides(
14299 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
14300 int n_before, int n_after, int n_div_ma)
14302 int i, k;
14303 int div;
14304 isl_size total;
14305 isl_size n_param;
14306 isl_size n_in;
14308 total = isl_basic_map_dim(bmap, isl_dim_all);
14309 n_param = isl_multi_aff_dim(ma, isl_dim_param);
14310 n_in = isl_multi_aff_dim(ma, isl_dim_in);
14311 if (total < 0 || n_param < 0 || n_in < 0)
14312 return isl_basic_map_free(bmap);
14313 for (i = 0; i < ma->n; ++i) {
14314 int o_bmap = 0, o_ma = 1;
14316 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
14317 continue;
14318 div = isl_basic_map_alloc_div(bmap);
14319 k = isl_basic_map_alloc_equality(bmap);
14320 if (div < 0 || k < 0)
14321 goto error;
14322 isl_int_set_si(bmap->div[div][0], 0);
14323 isl_seq_cpy(bmap->eq[k] + o_bmap,
14324 ma->u.p[i]->v->el + o_ma, 1 + n_param);
14325 o_bmap += 1 + n_param;
14326 o_ma += 1 + n_param;
14327 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
14328 o_bmap += n_before;
14329 isl_seq_cpy(bmap->eq[k] + o_bmap,
14330 ma->u.p[i]->v->el + o_ma, n_in);
14331 o_bmap += n_in;
14332 o_ma += n_in;
14333 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
14334 o_bmap += n_after;
14335 isl_seq_cpy(bmap->eq[k] + o_bmap,
14336 ma->u.p[i]->v->el + o_ma, n_div_ma);
14337 o_bmap += n_div_ma;
14338 o_ma += n_div_ma;
14339 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
14340 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
14341 total++;
14344 return bmap;
14345 error:
14346 isl_basic_map_free(bmap);
14347 return NULL;
14350 /* Replace the domain or range space (depending on "type) of "space" by "set".
14352 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
14353 enum isl_dim_type type, __isl_take isl_space *set)
14355 if (type == isl_dim_in) {
14356 space = isl_space_range(space);
14357 space = isl_space_map_from_domain_and_range(set, space);
14358 } else {
14359 space = isl_space_domain(space);
14360 space = isl_space_map_from_domain_and_range(space, set);
14363 return space;
14366 /* Compute the preimage of the domain or range (depending on "type")
14367 * of "bmap" under the function represented by "ma".
14368 * In other words, plug in "ma" in the domain or range of "bmap".
14369 * The result is a basic map that lives in the same space as "bmap"
14370 * except that the domain or range has been replaced by
14371 * the domain space of "ma".
14373 * If bmap is represented by
14375 * A(p) + S u + B x + T v + C(divs) >= 0,
14377 * where u and x are input and output dimensions if type == isl_dim_out
14378 * while x and v are input and output dimensions if type == isl_dim_in,
14379 * and ma is represented by
14381 * x = D(p) + F(y) + G(divs')
14383 * then the result is
14385 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14387 * The divs in the input set are similarly adjusted.
14388 * In particular
14390 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14392 * becomes
14394 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14395 * B_i G(divs') + c_i(divs))/n_i)
14397 * If bmap is not a rational map and if F(y) involves any denominators
14399 * x_i = (f_i y + h_i)/m_i
14401 * then additional constraints are added to ensure that we only
14402 * map back integer points. That is we enforce
14404 * f_i y + h_i = m_i alpha_i
14406 * with alpha_i an additional existentially quantified variable.
14408 * We first copy over the divs from "ma".
14409 * Then we add the modified constraints and divs from "bmap".
14410 * Finally, we add the stride constraints, if needed.
14412 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
14413 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
14414 __isl_take isl_multi_aff *ma)
14416 int i, k;
14417 isl_space *space;
14418 isl_basic_map *res = NULL;
14419 isl_size n_before, n_after, n_div_bmap, n_div_ma;
14420 isl_int f, c1, c2, g;
14421 isl_bool rational;
14422 int strides;
14424 isl_int_init(f);
14425 isl_int_init(c1);
14426 isl_int_init(c2);
14427 isl_int_init(g);
14429 ma = isl_multi_aff_align_divs(ma);
14430 if (!bmap || !ma)
14431 goto error;
14432 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
14433 goto error;
14435 if (type == isl_dim_in) {
14436 n_before = 0;
14437 n_after = isl_basic_map_dim(bmap, isl_dim_out);
14438 } else {
14439 n_before = isl_basic_map_dim(bmap, isl_dim_in);
14440 n_after = 0;
14442 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
14443 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
14444 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
14445 goto error;
14447 space = isl_multi_aff_get_domain_space(ma);
14448 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
14449 rational = isl_basic_map_is_rational(bmap);
14450 strides = rational ? 0 : multi_aff_strides(ma);
14451 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
14452 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
14453 if (rational)
14454 res = isl_basic_map_set_rational(res);
14456 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
14457 if (isl_basic_map_alloc_div(res) < 0)
14458 goto error;
14460 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
14461 if (!res)
14462 goto error;
14464 for (i = 0; i < bmap->n_eq; ++i) {
14465 k = isl_basic_map_alloc_equality(res);
14466 if (k < 0)
14467 goto error;
14468 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
14469 n_after, n_div_ma, n_div_bmap,
14470 f, c1, c2, g, 0) < 0)
14471 goto error;
14474 for (i = 0; i < bmap->n_ineq; ++i) {
14475 k = isl_basic_map_alloc_inequality(res);
14476 if (k < 0)
14477 goto error;
14478 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
14479 n_after, n_div_ma, n_div_bmap,
14480 f, c1, c2, g, 0) < 0)
14481 goto error;
14484 for (i = 0; i < bmap->n_div; ++i) {
14485 if (isl_int_is_zero(bmap->div[i][0])) {
14486 isl_int_set_si(res->div[n_div_ma + i][0], 0);
14487 continue;
14489 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
14490 n_before, n_after, n_div_ma, n_div_bmap,
14491 f, c1, c2, g, 1) < 0)
14492 goto error;
14495 if (strides)
14496 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
14498 isl_int_clear(f);
14499 isl_int_clear(c1);
14500 isl_int_clear(c2);
14501 isl_int_clear(g);
14502 isl_basic_map_free(bmap);
14503 isl_multi_aff_free(ma);
14504 res = isl_basic_map_simplify(res);
14505 return isl_basic_map_finalize(res);
14506 error:
14507 isl_int_clear(f);
14508 isl_int_clear(c1);
14509 isl_int_clear(c2);
14510 isl_int_clear(g);
14511 isl_basic_map_free(bmap);
14512 isl_multi_aff_free(ma);
14513 isl_basic_map_free(res);
14514 return NULL;
14517 /* Compute the preimage of "bset" under the function represented by "ma".
14518 * In other words, plug in "ma" in "bset". The result is a basic set
14519 * that lives in the domain space of "ma".
14521 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14522 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14524 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14527 /* Compute the preimage of the domain of "bmap" under the function
14528 * represented by "ma".
14529 * In other words, plug in "ma" in the domain of "bmap".
14530 * The result is a basic map that lives in the same space as "bmap"
14531 * except that the domain has been replaced by the domain space of "ma".
14533 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14534 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14536 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14539 /* Compute the preimage of the range of "bmap" under the function
14540 * represented by "ma".
14541 * In other words, plug in "ma" in the range of "bmap".
14542 * The result is a basic map that lives in the same space as "bmap"
14543 * except that the range has been replaced by the domain space of "ma".
14545 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14546 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14548 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14551 /* Check if the range of "ma" is compatible with the domain or range
14552 * (depending on "type") of "map".
14553 * Return isl_stat_error if anything is wrong.
14555 static isl_stat check_map_compatible_range_multi_aff(
14556 __isl_keep isl_map *map, enum isl_dim_type type,
14557 __isl_keep isl_multi_aff *ma)
14559 isl_bool m;
14560 isl_space *ma_space;
14562 ma_space = isl_multi_aff_get_space(ma);
14563 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14564 isl_space_free(ma_space);
14565 if (m < 0)
14566 return isl_stat_error;
14567 if (!m)
14568 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14569 "spaces don't match", return isl_stat_error);
14570 return isl_stat_ok;
14573 /* Compute the preimage of the domain or range (depending on "type")
14574 * of "map" under the function represented by "ma".
14575 * In other words, plug in "ma" in the domain or range of "map".
14576 * The result is a map that lives in the same space as "map"
14577 * except that the domain or range has been replaced by
14578 * the domain space of "ma".
14580 * The parameters are assumed to have been aligned.
14582 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14583 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14585 int i;
14586 isl_space *space;
14588 map = isl_map_cow(map);
14589 ma = isl_multi_aff_align_divs(ma);
14590 if (!map || !ma)
14591 goto error;
14592 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14593 goto error;
14595 for (i = 0; i < map->n; ++i) {
14596 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14597 isl_multi_aff_copy(ma));
14598 if (!map->p[i])
14599 goto error;
14602 space = isl_multi_aff_get_domain_space(ma);
14603 space = isl_space_set(isl_map_get_space(map), type, space);
14605 isl_space_free(isl_map_take_space(map));
14606 map = isl_map_restore_space(map, space);
14607 if (!map)
14608 goto error;
14610 isl_multi_aff_free(ma);
14611 if (map->n > 1)
14612 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14613 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14614 return map;
14615 error:
14616 isl_multi_aff_free(ma);
14617 isl_map_free(map);
14618 return NULL;
14621 /* Compute the preimage of the domain or range (depending on "type")
14622 * of "map" under the function represented by "ma".
14623 * In other words, plug in "ma" in the domain or range of "map".
14624 * The result is a map that lives in the same space as "map"
14625 * except that the domain or range has been replaced by
14626 * the domain space of "ma".
14628 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14629 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14631 isl_bool aligned;
14633 if (!map || !ma)
14634 goto error;
14636 aligned = isl_map_space_has_equal_params(map, ma->space);
14637 if (aligned < 0)
14638 goto error;
14639 if (aligned)
14640 return map_preimage_multi_aff(map, type, ma);
14642 if (isl_map_check_named_params(map) < 0)
14643 goto error;
14644 if (!isl_space_has_named_params(ma->space))
14645 isl_die(map->ctx, isl_error_invalid,
14646 "unaligned unnamed parameters", goto error);
14647 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14648 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14650 return map_preimage_multi_aff(map, type, ma);
14651 error:
14652 isl_multi_aff_free(ma);
14653 return isl_map_free(map);
14656 /* Compute the preimage of "set" under the function represented by "ma".
14657 * In other words, plug in "ma" in "set". The result is a set
14658 * that lives in the domain space of "ma".
14660 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14661 __isl_take isl_multi_aff *ma)
14663 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14666 /* Compute the preimage of the domain of "map" under the function
14667 * represented by "ma".
14668 * In other words, plug in "ma" in the domain of "map".
14669 * The result is a map that lives in the same space as "map"
14670 * except that the domain has been replaced by the domain space of "ma".
14672 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14673 __isl_take isl_multi_aff *ma)
14675 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14678 /* Compute the preimage of the range of "map" under the function
14679 * represented by "ma".
14680 * In other words, plug in "ma" in the range of "map".
14681 * The result is a map that lives in the same space as "map"
14682 * except that the range has been replaced by the domain space of "ma".
14684 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14685 __isl_take isl_multi_aff *ma)
14687 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14690 /* Compute the preimage of "map" under the function represented by "pma".
14691 * In other words, plug in "pma" in the domain or range of "map".
14692 * The result is a map that lives in the same space as "map",
14693 * except that the space of type "type" has been replaced by
14694 * the domain space of "pma".
14696 * The parameters of "map" and "pma" are assumed to have been aligned.
14698 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14699 __isl_take isl_map *map, enum isl_dim_type type,
14700 __isl_take isl_pw_multi_aff *pma)
14702 int i;
14703 isl_map *res;
14705 if (!pma)
14706 goto error;
14708 if (pma->n == 0) {
14709 isl_space *space;
14711 space = isl_pw_multi_aff_get_domain_space(pma);
14712 isl_pw_multi_aff_free(pma);
14713 space = isl_space_set(isl_map_get_space(map), type, space);
14714 isl_map_free(map);
14715 return isl_map_empty(space);
14718 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14719 isl_multi_aff_copy(pma->p[0].maff));
14720 if (type == isl_dim_in)
14721 res = isl_map_intersect_domain(res,
14722 isl_map_copy(pma->p[0].set));
14723 else
14724 res = isl_map_intersect_range(res,
14725 isl_map_copy(pma->p[0].set));
14727 for (i = 1; i < pma->n; ++i) {
14728 isl_map *res_i;
14730 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14731 isl_multi_aff_copy(pma->p[i].maff));
14732 if (type == isl_dim_in)
14733 res_i = isl_map_intersect_domain(res_i,
14734 isl_map_copy(pma->p[i].set));
14735 else
14736 res_i = isl_map_intersect_range(res_i,
14737 isl_map_copy(pma->p[i].set));
14738 res = isl_map_union(res, res_i);
14741 isl_pw_multi_aff_free(pma);
14742 isl_map_free(map);
14743 return res;
14744 error:
14745 isl_pw_multi_aff_free(pma);
14746 isl_map_free(map);
14747 return NULL;
14750 /* Compute the preimage of "map" under the function represented by "pma".
14751 * In other words, plug in "pma" in the domain or range of "map".
14752 * The result is a map that lives in the same space as "map",
14753 * except that the space of type "type" has been replaced by
14754 * the domain space of "pma".
14756 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14757 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14759 isl_bool aligned;
14761 if (!map || !pma)
14762 goto error;
14764 aligned = isl_map_space_has_equal_params(map, pma->dim);
14765 if (aligned < 0)
14766 goto error;
14767 if (aligned)
14768 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14770 if (isl_map_check_named_params(map) < 0)
14771 goto error;
14772 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14773 goto error;
14774 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14775 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14777 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14778 error:
14779 isl_pw_multi_aff_free(pma);
14780 return isl_map_free(map);
14783 /* Compute the preimage of "set" under the function represented by "pma".
14784 * In other words, plug in "pma" in "set". The result is a set
14785 * that lives in the domain space of "pma".
14787 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14788 __isl_take isl_pw_multi_aff *pma)
14790 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14793 /* Compute the preimage of the domain of "map" under the function
14794 * represented by "pma".
14795 * In other words, plug in "pma" in the domain of "map".
14796 * The result is a map that lives in the same space as "map",
14797 * except that domain space has been replaced by the domain space of "pma".
14799 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14800 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14802 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14805 /* Compute the preimage of the range of "map" under the function
14806 * represented by "pma".
14807 * In other words, plug in "pma" in the range of "map".
14808 * The result is a map that lives in the same space as "map",
14809 * except that range space has been replaced by the domain space of "pma".
14811 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14812 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14814 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14817 /* Compute the preimage of "map" under the function represented by "mpa".
14818 * In other words, plug in "mpa" in the domain or range of "map".
14819 * The result is a map that lives in the same space as "map",
14820 * except that the space of type "type" has been replaced by
14821 * the domain space of "mpa".
14823 * If the map does not involve any constraints that refer to the
14824 * dimensions of the substituted space, then the only possible
14825 * effect of "mpa" on the map is to map the space to a different space.
14826 * We create a separate isl_multi_aff to effectuate this change
14827 * in order to avoid spurious splitting of the map along the pieces
14828 * of "mpa".
14829 * If "mpa" has a non-trivial explicit domain, however,
14830 * then the full substitution should be performed.
14832 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14833 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14835 isl_size n;
14836 isl_bool full;
14837 isl_pw_multi_aff *pma;
14839 n = isl_map_dim(map, type);
14840 if (n < 0 || !mpa)
14841 goto error;
14843 full = isl_map_involves_dims(map, type, 0, n);
14844 if (full >= 0 && !full)
14845 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14846 if (full < 0)
14847 goto error;
14848 if (!full) {
14849 isl_space *space;
14850 isl_multi_aff *ma;
14852 space = isl_multi_pw_aff_get_space(mpa);
14853 isl_multi_pw_aff_free(mpa);
14854 ma = isl_multi_aff_zero(space);
14855 return isl_map_preimage_multi_aff(map, type, ma);
14858 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14859 return isl_map_preimage_pw_multi_aff(map, type, pma);
14860 error:
14861 isl_map_free(map);
14862 isl_multi_pw_aff_free(mpa);
14863 return NULL;
14866 /* Compute the preimage of "map" under the function represented by "mpa".
14867 * In other words, plug in "mpa" in the domain "map".
14868 * The result is a map that lives in the same space as "map",
14869 * except that domain space has been replaced by the domain space of "mpa".
14871 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14872 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14874 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14877 /* Compute the preimage of "set" by the function represented by "mpa".
14878 * In other words, plug in "mpa" in "set".
14880 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14881 __isl_take isl_multi_pw_aff *mpa)
14883 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14886 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14887 * on the output dimension "pos" in terms of the parameters,
14888 * the input dimensions and possibly some integer divisions,
14889 * but not any other output dimensions, extract this upper bound
14890 * as a function of all dimensions (with zero coefficients
14891 * for the output dimensions).
14893 * That is, the inequality is of the form
14895 * e(...) + c - m x >= 0
14897 * where e does not depend on any other output dimensions.
14898 * Return (e(...) + c) / m, with the denominator m in the first position.
14900 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14901 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14903 isl_ctx *ctx;
14904 isl_size v_out, total;
14906 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14907 total = isl_basic_map_dim(bmap, isl_dim_all);
14908 if (v_out < 0 || total < 0)
14909 return NULL;
14910 ctx = isl_basic_map_get_ctx(bmap);
14911 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14912 total, v_out + pos);
14915 /* Is constraint "c" of "bmap" of the form
14917 * e(...) + c1 - m x >= 0
14919 * or
14921 * -e(...) + c2 + m x >= 0
14923 * where m > 1 and e does not involve any other output variables?
14925 * "v_out" is the offset to the output variables.
14926 * "d" is the position of x among the output variables.
14927 * "v_div" is the offset to the local variables.
14928 * "total" is the total number of variables.
14930 * Since the purpose of this function is to use the constraint
14931 * to express the output variable as an integer division,
14932 * do not allow the constraint to involve any local variables
14933 * that do not have an explicit representation.
14935 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14936 isl_int *c, int v_out, int d, int v_div, int total)
14938 int i = 0;
14940 if (isl_int_is_zero(c[1 + v_out + d]))
14941 return isl_bool_false;
14942 if (isl_int_is_one(c[1 + v_out + d]))
14943 return isl_bool_false;
14944 if (isl_int_is_negone(c[1 + v_out + d]))
14945 return isl_bool_false;
14946 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14947 return isl_bool_false;
14948 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14949 v_div - (v_out + d + 1)) != -1)
14950 return isl_bool_false;
14951 for (i = 0; v_div + i < total; ++i) {
14952 isl_bool known, involves;
14954 if (isl_int_is_zero(c[1 + v_div + i]))
14955 continue;
14956 known = isl_basic_map_div_is_known(bmap, i);
14957 if (known < 0 || !known)
14958 return known;
14959 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14960 if (involves < 0 || involves)
14961 return isl_bool_not(involves);
14963 return isl_bool_true;
14966 /* Look for a pair of constraints
14968 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14970 * and
14972 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14974 * that express that the output dimension x at position "pos"
14975 * is some integer division of an expression in terms of the parameters,
14976 * input dimensions and integer divisions.
14977 * If such a pair can be found, then return the index
14978 * of the upper bound constraint, m x <= e(...) + c1.
14979 * Otherwise, return an index beyond the number of constraints.
14981 * The constraints of "bmap" are assumed to have been sorted.
14983 * In order for the constraints above to express an integer division,
14984 * m needs to be greater than 1 and such that
14986 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14988 * In particular, this ensures that
14990 * x = floor((e(...) + c1) / m)
14992 isl_size isl_basic_map_find_output_upper_div_constraint(
14993 __isl_keep isl_basic_map *bmap, int pos)
14995 int i;
14996 isl_size j;
14997 isl_size n_ineq;
14998 isl_size v_out, v_div;
14999 isl_size total;
15000 isl_int sum;
15002 total = isl_basic_map_dim(bmap, isl_dim_all);
15003 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15004 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15005 n_ineq = isl_basic_map_n_inequality(bmap);
15006 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
15007 return isl_size_error;
15009 isl_int_init(sum);
15010 for (i = 0; i < n_ineq; ++i) {
15011 isl_bool potential;
15013 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
15014 v_out, pos, v_div, total);
15015 if (potential < 0)
15016 goto error;
15017 if (!potential)
15018 continue;
15020 j = find_later_constraint_in_pair(bmap, i, v_out + pos, total,
15021 bmap->ineq[i][1 + v_out + pos], &sum);
15022 if (j < 0)
15023 goto error;
15024 if (j < n_ineq)
15025 break;
15027 isl_int_clear(sum);
15029 if (i >= n_ineq)
15030 return n_ineq;
15031 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
15032 return i;
15033 else
15034 return j;
15035 error:
15036 isl_int_clear(sum);
15037 return isl_size_error;
15040 /* Look for a pair of constraints
15042 * -g(x) + i >= 0
15043 * g(x) - i + d >= 0
15045 * in "bmap" on the output dimension at position "pos" such that
15046 * g(x) is an expression in the parameters and input dimensions, and
15047 * d < "n".
15048 * Return the index of the first constraint if such a pair can be found.
15049 * Otherwise, return an index beyond the number of inequality constraints.
15051 * The constraints are assumed to have been sorted.
15053 static isl_size find_output_lower_mod_constraint(__isl_keep isl_basic_map *bmap,
15054 int pos, isl_int n)
15056 int i;
15057 isl_size n_ineq;
15058 isl_size v_out, n_out;
15059 isl_size v_div, n_div;
15060 isl_size j;
15061 isl_int tmp;
15063 n_ineq = isl_basic_map_n_inequality(bmap);
15064 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15065 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15066 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15067 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15068 if (n_ineq < 0 || v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15069 return isl_size_error;
15071 isl_int_init(tmp);
15072 for (i = n_ineq - 1; i >= 0; --i) {
15073 if (!isl_int_is_one(bmap->ineq[i][1 + v_out + pos]) &&
15074 !isl_int_is_negone(bmap->ineq[i][1 + v_out + pos]))
15075 continue;
15076 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_div,
15077 n_div) != -1)
15078 continue;
15079 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_out, pos) != -1)
15080 continue;
15081 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_out + pos + 1,
15082 n_out - (pos + 1)) != -1)
15083 continue;
15084 j = find_earlier_constraint_in_pair(bmap, i, v_out + pos, v_div,
15085 n, &tmp);
15086 if (j >= n_ineq)
15087 continue;
15088 isl_int_clear(tmp);
15089 if (j < 0)
15090 return j;
15091 if (isl_int_is_one(bmap->ineq[i][1 + v_out + pos]))
15092 return i;
15093 return j;
15096 isl_int_clear(tmp);
15097 return n_ineq;
15100 /* Given that equality "eq" of "bset" expresses
15101 * the variable at position "pos" in terms of the other variables,
15102 * extract this expression as a function of those other variables,
15103 * excluding any local variables.
15105 static __isl_give isl_aff *extract_expr_aff(
15106 __isl_keep isl_basic_set *bset, int eq, int pos)
15108 isl_size dim;
15109 isl_ctx *ctx;
15110 isl_vec *v;
15111 isl_space *space;
15112 isl_local_space *ls;
15114 space = isl_basic_set_peek_space(bset);
15115 dim = isl_space_dim(space, isl_dim_all);
15116 if (dim < 0)
15117 return NULL;
15118 ctx = isl_basic_set_get_ctx(bset);
15119 v = extract_bound_from_constraint(ctx, bset->eq[eq], dim, pos);
15120 ls = isl_local_space_from_space(isl_space_copy(space));
15121 return isl_aff_alloc_vec(ls, v);
15124 /* Given the presence in "bmap" of an equality constraint "eq"
15126 * f(x) + t m i + m n h(y) = 0
15128 * and a pair of inequality constraints
15130 * -g(x) + i >= 0 ("lower")
15131 * g(x) - i + d >= 0
15133 * where i is the output dimension at position "pos",
15134 * f(x) and g(x) are expressions in the parameters and
15135 * input dimensions (if any),
15136 * h(y) is an expression in the other output dimensions,
15137 * t is +1 or -1, and
15138 * d < "n",
15139 * return the expression
15141 * (-t f(x)/m - g(x)) mod n + g(x)
15143 * The expression is first constructed in terms of the wrapped space of "bmap".
15144 * Since this expression does not depend on the original output dimensions,
15145 * they can be removed, resulting in an expression in terms
15146 * of the input dimensions.
15147 * If "bmap" is actually a set, then the resulting expression has no domain.
15149 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15150 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15152 static __isl_give isl_aff *construct_mod(__isl_keep isl_basic_map *bmap,
15153 int pos, int eq, int lower, isl_int n)
15155 isl_ctx *ctx;
15156 isl_basic_set *bset;
15157 isl_aff *f, *g, *mod;
15158 isl_val *v;
15159 isl_bool is_set;
15161 is_set = isl_basic_map_is_set(bmap);
15162 if (is_set < 0)
15163 return NULL;
15164 ctx = isl_basic_map_get_ctx(bmap);
15166 if (is_set)
15167 bset = bset_from_bmap(isl_basic_map_copy(bmap));
15168 else
15169 bset = isl_basic_map_wrap(isl_basic_map_copy(bmap));
15170 g = extract_lower_bound_aff(bset, lower, pos);
15171 f = extract_expr_aff(bset, eq, pos);
15172 isl_basic_set_free(bset);
15174 mod = isl_aff_sub(f, isl_aff_copy(g));
15175 v = isl_val_int_from_isl_int(ctx, n);
15176 mod = isl_aff_mod_val(mod, v);
15177 mod = isl_aff_add(mod, g);
15178 if (is_set)
15179 mod = isl_aff_project_domain_on_params(mod);
15180 else
15181 mod = isl_aff_domain_factor_domain(mod);
15183 return mod;
15186 /* Look for a combination of constraints in "bmap" that ensure
15187 * that output dimension "pos" is equal to some modulo expression
15188 * in the parameters and input dimensions and
15189 * return this expression if found.
15191 * The constraints are assumed to have been sorted.
15193 * In particular look for an equality constraint
15195 * f(x) + t m i + m n h(y) = 0
15197 * and a pair of inequality constraints
15199 * -g(x) + i >= 0
15200 * g(x) - i + d >= 0
15202 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15203 * h(y) is an expression in local variables and other output dimensions,
15204 * t is +1 or -1, and
15205 * d < n.
15207 * If such a combination of constraints can be found
15208 * then
15210 * i = -t f(x)/m - t n h(y)
15211 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15213 * and so, because 0 <= i - g(x) <= d < n,
15215 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15216 * = (-t f(x)/m - g(x)) mod n
15218 * That is,
15220 * i = (-t f(x)/m - g(x)) mod n + g(x)
15222 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_mod(
15223 __isl_keep isl_basic_map *bmap, int pos)
15225 int i, j;
15226 isl_size n_eq, n_ineq;
15227 isl_size v_out, n_out;
15228 isl_size v_div, n_div;
15229 isl_size lower;
15230 isl_int gcd;
15231 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15233 n_eq = isl_basic_map_n_equality(bmap);
15234 n_ineq = isl_basic_map_n_inequality(bmap);
15235 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15236 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15237 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15238 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15239 if (n_eq < 0 || n_ineq < 0 ||
15240 v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15241 goto error;
15242 if (n_eq == 0 || n_ineq < 2 || n_out == 1)
15243 return res;
15245 isl_int_init(gcd);
15246 for (i = 0; i < n_eq; ++i) {
15247 if (isl_int_is_zero(bmap->eq[i][1 + v_out + pos]))
15248 continue;
15249 isl_seq_gcd(bmap->eq[i] + 1 + v_div, n_div, &gcd);
15250 for (j = 0; j < n_out; ++j) {
15251 if (j == pos)
15252 continue;
15253 isl_int_gcd(gcd, gcd, bmap->eq[i][1 + v_out + j]);
15255 if (!isl_int_abs_gt(gcd, bmap->eq[i][1 + v_out + pos]))
15256 continue;
15257 if (!isl_int_is_divisible_by(gcd, bmap->eq[i][1 + v_out + pos]))
15258 continue;
15259 isl_int_divexact(gcd, gcd, bmap->eq[i][1 + v_out + pos]);
15260 isl_int_abs(gcd, gcd);
15261 lower = find_output_lower_mod_constraint(bmap, pos, gcd);
15262 if (lower >= n_ineq)
15263 continue;
15264 res.valid = isl_bool_true;
15265 if (lower >= 0)
15266 res.value = construct_mod(bmap, v_out + pos, i,
15267 lower, gcd);
15268 if (!res.value)
15269 res.valid = isl_bool_error;
15270 isl_int_clear(gcd);
15271 return res;
15274 isl_int_clear(gcd);
15275 return res;
15276 error:
15277 res.valid = isl_bool_error;
15278 return res;
15281 /* Return a copy of the equality constraints of "bset" as a matrix.
15283 __isl_give isl_mat *isl_basic_set_extract_equalities(
15284 __isl_keep isl_basic_set *bset)
15286 isl_ctx *ctx;
15287 isl_size total;
15289 total = isl_basic_set_dim(bset, isl_dim_all);
15290 if (total < 0)
15291 return NULL;
15293 ctx = isl_basic_set_get_ctx(bset);
15294 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
15297 /* Are the "n" "coefficients" starting at "first" of the integer division
15298 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15299 * to each other?
15300 * The "coefficient" at position 0 is the denominator.
15301 * The "coefficient" at position 1 is the constant term.
15303 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
15304 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
15305 unsigned first, unsigned n)
15307 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
15308 return isl_bool_error;
15309 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
15310 return isl_bool_error;
15311 return isl_seq_eq(bmap1->div[pos1] + first,
15312 bmap2->div[pos2] + first, n);
15315 /* Are the integer division expressions at position "pos1" in "bmap1" and
15316 * "pos2" in "bmap2" equal to each other, except that the constant terms
15317 * are different?
15319 isl_bool isl_basic_map_equal_div_expr_except_constant(
15320 __isl_keep isl_basic_map *bmap1, int pos1,
15321 __isl_keep isl_basic_map *bmap2, int pos2)
15323 isl_bool equal;
15324 isl_size total, total2;
15326 total = isl_basic_map_dim(bmap1, isl_dim_all);
15327 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
15328 if (total < 0 || total2 < 0)
15329 return isl_bool_error;
15330 if (total != total2)
15331 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
15332 "incomparable div expressions", return isl_bool_error);
15333 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15334 0, 1);
15335 if (equal < 0 || !equal)
15336 return equal;
15337 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15338 1, 1);
15339 if (equal < 0 || equal)
15340 return isl_bool_not(equal);
15341 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15342 2, total);
15345 /* Replace the numerator of the constant term of the integer division
15346 * expression at position "div" in "bmap" by "value".
15347 * The caller guarantees that this does not change the meaning
15348 * of the input.
15350 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
15351 __isl_take isl_basic_map *bmap, int div, int value)
15353 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
15354 return isl_basic_map_free(bmap);
15356 isl_int_set_si(bmap->div[div][1], value);
15358 return bmap;
15361 /* Is the point "inner" internal to inequality constraint "ineq"
15362 * of "bset"?
15363 * The point is considered to be internal to the inequality constraint,
15364 * if it strictly lies on the positive side of the inequality constraint,
15365 * or if it lies on the constraint and the constraint is lexico-positive.
15367 static isl_bool is_internal(__isl_keep isl_vec *inner,
15368 __isl_keep isl_basic_set *bset, int ineq)
15370 isl_ctx *ctx;
15371 int pos;
15372 isl_size total;
15374 if (!inner || !bset)
15375 return isl_bool_error;
15377 ctx = isl_basic_set_get_ctx(bset);
15378 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
15379 &ctx->normalize_gcd);
15380 if (!isl_int_is_zero(ctx->normalize_gcd))
15381 return isl_int_is_nonneg(ctx->normalize_gcd);
15383 total = isl_basic_set_dim(bset, isl_dim_all);
15384 if (total < 0)
15385 return isl_bool_error;
15386 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
15387 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
15390 /* Tighten the inequality constraints of "bset" that are outward with respect
15391 * to the point "vec".
15392 * That is, tighten the constraints that are not satisfied by "vec".
15394 * "vec" is a point internal to some superset S of "bset" that is used
15395 * to make the subsets of S disjoint, by tightening one half of the constraints
15396 * that separate two subsets. In particular, the constraints of S
15397 * are all satisfied by "vec" and should not be tightened.
15398 * Of the internal constraints, those that have "vec" on the outside
15399 * are tightened. The shared facet is included in the adjacent subset
15400 * with the opposite constraint.
15401 * For constraints that saturate "vec", this criterion cannot be used
15402 * to determine which of the two sides should be tightened.
15403 * Instead, the sign of the first non-zero coefficient is used
15404 * to make this choice. Note that this second criterion is never used
15405 * on the constraints of S since "vec" is interior to "S".
15407 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
15408 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
15410 int j;
15412 bset = isl_basic_set_cow(bset);
15413 if (!bset)
15414 return NULL;
15415 for (j = 0; j < bset->n_ineq; ++j) {
15416 isl_bool internal;
15418 internal = is_internal(vec, bset, j);
15419 if (internal < 0)
15420 return isl_basic_set_free(bset);
15421 if (internal)
15422 continue;
15423 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
15426 return bset;
15429 /* Replace the variables x of type "type" starting at "first" in "bmap"
15430 * by x' with x = M x' with M the matrix trans.
15431 * That is, replace the corresponding coefficients c by c M.
15433 * The transformation matrix should be a square matrix.
15435 __isl_give isl_basic_map *isl_basic_map_transform_dims(
15436 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
15437 __isl_take isl_mat *trans)
15439 unsigned pos;
15441 bmap = isl_basic_map_cow(bmap);
15442 if (!bmap || !trans)
15443 goto error;
15445 if (trans->n_row != trans->n_col)
15446 isl_die(trans->ctx, isl_error_invalid,
15447 "expecting square transformation matrix", goto error);
15448 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
15449 goto error;
15451 pos = isl_basic_map_offset(bmap, type) + first;
15453 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
15454 isl_mat_copy(trans)) < 0)
15455 goto error;
15456 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
15457 isl_mat_copy(trans)) < 0)
15458 goto error;
15459 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
15460 isl_mat_copy(trans)) < 0)
15461 goto error;
15463 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
15464 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
15465 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
15467 isl_mat_free(trans);
15468 return bmap;
15469 error:
15470 isl_mat_free(trans);
15471 isl_basic_map_free(bmap);
15472 return NULL;
15475 /* Replace the variables x of type "type" starting at "first" in "bset"
15476 * by x' with x = M x' with M the matrix trans.
15477 * That is, replace the corresponding coefficients c by c M.
15479 * The transformation matrix should be a square matrix.
15481 __isl_give isl_basic_set *isl_basic_set_transform_dims(
15482 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
15483 __isl_take isl_mat *trans)
15485 return isl_basic_map_transform_dims(bset, type, first, trans);