isl_tab_basic_map_partial_lexopt*: separate out intersection
[isl.git] / isl_map.c
blobcd5d437cd649bba1c93e5f3394f5f6753c7e3a74
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 isl_size_error on error.
10282 static isl_size 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 isl_size_error;
10293 isl_assert(dst->ctx, div <= n_div, return isl_size_error);
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 local variables of "dst" to those of "src",
10303 * adding local variables from "src" if needed.
10304 * That is, make sure that the first src->n_div local variables
10305 * of the result correspond to those of src.
10306 * For any integer division that is copied to "dst",
10307 * the defining constraints are also introduced to "dst".
10308 * For an existentially quantified variable (without an explicit definition)
10309 * only an unconstrained existentially quantified variable
10310 * in the same positions is introduced.
10311 * The integer division of "src" are assumed to be ordered.
10313 * The integer divisions are swapped into the right position
10314 * (possibly after adding them first). This may result
10315 * in the remaining integer divisions appearing in the wrong order,
10316 * i.e., with some integer division appearing before
10317 * some other integer division on which it depends.
10318 * The integer divisions therefore need to be ordered.
10319 * This will not affect the integer divisions aligned to those of "src",
10320 * since "src" is assumed to have ordered integer divisions.
10322 * The result is not finalized as by design it will have redundant
10323 * divs if any divs from "src" were copied.
10325 __isl_give isl_basic_map *isl_basic_map_align_divs(
10326 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
10328 int i;
10329 int extended;
10330 isl_size v_div;
10331 isl_size dst_n_div, src_n_div;
10333 src_n_div = isl_basic_map_dim(src, isl_dim_div);
10334 if (!dst || src_n_div < 0)
10335 return isl_basic_map_free(dst);
10337 if (src_n_div == 0)
10338 return dst;
10340 v_div = isl_basic_map_var_offset(src, isl_dim_div);
10341 if (v_div < 0)
10342 return isl_basic_map_free(dst);
10344 extended = 0;
10345 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
10346 if (dst_n_div < 0)
10347 dst = isl_basic_map_free(dst);
10348 for (i = 0; i < src_n_div; ++i) {
10349 isl_bool known;
10350 isl_size j;
10352 known = isl_basic_map_div_is_known(src, i);
10353 if (known < 0)
10354 return isl_basic_map_free(dst);
10355 j = known ? find_div(dst, src, i) : dst_n_div;
10356 if (j < 0)
10357 dst = isl_basic_map_free(dst);
10358 if (j == dst_n_div) {
10359 if (!extended) {
10360 int extra = src_n_div - i;
10361 dst = isl_basic_map_cow(dst);
10362 if (!dst)
10363 return isl_basic_map_free(dst);
10364 dst = isl_basic_map_extend(dst,
10365 extra, 0, 2 * extra);
10366 extended = 1;
10368 j = isl_basic_map_alloc_div(dst);
10369 if (j < 0)
10370 return isl_basic_map_free(dst);
10371 dst_n_div++;
10372 if (!known)
10373 continue;
10374 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
10375 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst_n_div - i);
10376 dst = isl_basic_map_add_div_constraints(dst, j);
10377 if (!dst)
10378 return isl_basic_map_free(dst);
10380 if (j != i)
10381 dst = isl_basic_map_swap_div(dst, i, j);
10382 if (!dst)
10383 return isl_basic_map_free(dst);
10385 return isl_basic_map_order_divs(dst);
10388 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
10390 int i;
10392 if (!map)
10393 return NULL;
10394 if (map->n == 0)
10395 return map;
10396 map = isl_map_compute_divs(map);
10397 map = isl_map_order_divs(map);
10398 map = isl_map_cow(map);
10399 if (!map)
10400 return NULL;
10402 for (i = 1; i < map->n; ++i)
10403 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
10404 for (i = 1; i < map->n; ++i) {
10405 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
10406 if (!map->p[i])
10407 return isl_map_free(map);
10410 map = isl_map_unmark_normalized(map);
10411 return map;
10414 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
10416 return isl_map_align_divs_internal(map);
10419 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
10421 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10424 /* Align the divs of the basic maps in "map" to those
10425 * of the basic maps in "list", as well as to the other basic maps in "map".
10426 * The elements in "list" are assumed to have known divs.
10428 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
10429 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
10431 int i;
10432 isl_size n;
10434 n = isl_basic_map_list_n_basic_map(list);
10435 map = isl_map_compute_divs(map);
10436 map = isl_map_cow(map);
10437 if (!map || n < 0)
10438 return isl_map_free(map);
10439 if (map->n == 0)
10440 return map;
10442 for (i = 0; i < n; ++i) {
10443 isl_basic_map *bmap;
10445 bmap = isl_basic_map_list_get_basic_map(list, i);
10446 bmap = isl_basic_map_order_divs(bmap);
10447 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
10448 isl_basic_map_free(bmap);
10450 if (!map->p[0])
10451 return isl_map_free(map);
10453 return isl_map_align_divs_internal(map);
10456 /* Align the divs of each element of "list" to those of "bmap".
10457 * Both "bmap" and the elements of "list" are assumed to have known divs.
10459 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
10460 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
10462 int i;
10463 isl_size n;
10465 n = isl_basic_map_list_n_basic_map(list);
10466 if (n < 0 || !bmap)
10467 return isl_basic_map_list_free(list);
10469 for (i = 0; i < n; ++i) {
10470 isl_basic_map *bmap_i;
10472 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10473 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
10474 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
10477 return list;
10480 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
10481 __isl_take isl_map *map)
10483 isl_bool ok;
10485 isl_map_align_params_set(&map, &set);
10486 ok = isl_map_compatible_domain(map, set);
10487 if (ok < 0)
10488 goto error;
10489 if (!ok)
10490 isl_die(isl_set_get_ctx(set), isl_error_invalid,
10491 "incompatible spaces", goto error);
10492 map = isl_map_intersect_domain(map, set);
10493 set = isl_map_range(map);
10494 return set;
10495 error:
10496 isl_set_free(set);
10497 isl_map_free(map);
10498 return NULL;
10501 /* There is no need to cow as removing empty parts doesn't change
10502 * the meaning of the set.
10504 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
10506 int i;
10508 if (!map)
10509 return NULL;
10511 for (i = map->n - 1; i >= 0; --i)
10512 map = remove_if_empty(map, i);
10514 return map;
10517 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10519 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10522 /* Create a binary relation that maps the shared initial "pos" dimensions
10523 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10525 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10526 __isl_keep isl_basic_set *bset2, int pos)
10528 isl_basic_map *bmap1;
10529 isl_basic_map *bmap2;
10531 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10532 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10533 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10534 isl_dim_out, 0, pos);
10535 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10536 isl_dim_out, 0, pos);
10537 return isl_basic_map_range_product(bmap1, bmap2);
10540 /* Given two basic sets bset1 and bset2, compute the maximal difference
10541 * between the values of dimension pos in bset1 and those in bset2
10542 * for any common value of the parameters and dimensions preceding pos.
10544 static enum isl_lp_result basic_set_maximal_difference_at(
10545 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10546 int pos, isl_int *opt)
10548 isl_basic_map *bmap1;
10549 struct isl_ctx *ctx;
10550 struct isl_vec *obj;
10551 isl_size total;
10552 isl_size nparam;
10553 isl_size dim1;
10554 enum isl_lp_result res;
10556 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10557 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10558 if (nparam < 0 || dim1 < 0 || !bset2)
10559 return isl_lp_error;
10561 bmap1 = join_initial(bset1, bset2, pos);
10562 total = isl_basic_map_dim(bmap1, isl_dim_all);
10563 if (total < 0)
10564 return isl_lp_error;
10566 ctx = bmap1->ctx;
10567 obj = isl_vec_alloc(ctx, 1 + total);
10568 if (!obj)
10569 goto error;
10570 isl_seq_clr(obj->block.data, 1 + total);
10571 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10572 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10573 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10574 opt, NULL, NULL);
10575 isl_basic_map_free(bmap1);
10576 isl_vec_free(obj);
10577 return res;
10578 error:
10579 isl_basic_map_free(bmap1);
10580 return isl_lp_error;
10583 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10584 * for any common value of the parameters and dimensions preceding pos
10585 * in both basic sets, the values of dimension pos in bset1 are
10586 * smaller or larger than those in bset2.
10588 * Returns
10589 * 1 if bset1 follows bset2
10590 * -1 if bset1 precedes bset2
10591 * 0 if bset1 and bset2 are incomparable
10592 * -2 if some error occurred.
10594 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10595 __isl_keep isl_basic_set *bset2, int pos)
10597 isl_int opt;
10598 enum isl_lp_result res;
10599 int cmp;
10601 isl_int_init(opt);
10603 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10605 if (res == isl_lp_empty)
10606 cmp = 0;
10607 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10608 res == isl_lp_unbounded)
10609 cmp = 1;
10610 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10611 cmp = -1;
10612 else
10613 cmp = -2;
10615 isl_int_clear(opt);
10616 return cmp;
10619 /* Given two basic sets bset1 and bset2, check whether
10620 * for any common value of the parameters and dimensions preceding pos
10621 * there is a value of dimension pos in bset1 that is larger
10622 * than a value of the same dimension in bset2.
10624 * Return
10625 * 1 if there exists such a pair
10626 * 0 if there is no such pair, but there is a pair of equal values
10627 * -1 otherwise
10628 * -2 if some error occurred.
10630 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10631 __isl_keep isl_basic_set *bset2, int pos)
10633 isl_bool empty;
10634 isl_basic_map *bmap;
10635 isl_size dim1;
10637 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10638 if (dim1 < 0)
10639 return -2;
10640 bmap = join_initial(bset1, bset2, pos);
10641 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10642 isl_dim_out, dim1 - pos);
10643 empty = isl_basic_map_is_empty(bmap);
10644 if (empty < 0)
10645 goto error;
10646 if (empty) {
10647 isl_basic_map_free(bmap);
10648 return -1;
10650 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10651 isl_dim_out, dim1 - pos);
10652 empty = isl_basic_map_is_empty(bmap);
10653 if (empty < 0)
10654 goto error;
10655 isl_basic_map_free(bmap);
10656 if (empty)
10657 return 0;
10658 return 1;
10659 error:
10660 isl_basic_map_free(bmap);
10661 return -2;
10664 /* Given two sets set1 and set2, check whether
10665 * for any common value of the parameters and dimensions preceding pos
10666 * there is a value of dimension pos in set1 that is larger
10667 * than a value of the same dimension in set2.
10669 * Return
10670 * 1 if there exists such a pair
10671 * 0 if there is no such pair, but there is a pair of equal values
10672 * -1 otherwise
10673 * -2 if some error occurred.
10675 int isl_set_follows_at(__isl_keep isl_set *set1,
10676 __isl_keep isl_set *set2, int pos)
10678 int i, j;
10679 int follows = -1;
10681 if (!set1 || !set2)
10682 return -2;
10684 for (i = 0; i < set1->n; ++i)
10685 for (j = 0; j < set2->n; ++j) {
10686 int f;
10687 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10688 if (f == 1 || f == -2)
10689 return f;
10690 if (f > follows)
10691 follows = f;
10694 return follows;
10697 static isl_bool isl_basic_map_plain_has_fixed_var(
10698 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10700 int i;
10701 int d;
10702 isl_size total;
10704 total = isl_basic_map_dim(bmap, isl_dim_all);
10705 if (total < 0)
10706 return isl_bool_error;
10707 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10708 for (; d+1 > pos; --d)
10709 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10710 break;
10711 if (d != pos)
10712 continue;
10713 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10714 return isl_bool_false;
10715 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10716 return isl_bool_false;
10717 if (!isl_int_is_one(bmap->eq[i][1+d]))
10718 return isl_bool_false;
10719 if (val)
10720 isl_int_neg(*val, bmap->eq[i][0]);
10721 return isl_bool_true;
10723 return isl_bool_false;
10726 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10727 unsigned pos, isl_int *val)
10729 int i;
10730 isl_int v;
10731 isl_int tmp;
10732 isl_bool fixed;
10734 if (!map)
10735 return isl_bool_error;
10736 if (map->n == 0)
10737 return isl_bool_false;
10738 if (map->n == 1)
10739 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10740 isl_int_init(v);
10741 isl_int_init(tmp);
10742 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10743 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10744 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10745 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10746 fixed = isl_bool_false;
10748 if (val)
10749 isl_int_set(*val, v);
10750 isl_int_clear(tmp);
10751 isl_int_clear(v);
10752 return fixed;
10755 static isl_bool isl_basic_set_plain_has_fixed_var(
10756 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10758 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10759 pos, val);
10762 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10763 enum isl_dim_type type, unsigned pos, isl_int *val)
10765 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10766 return isl_bool_error;
10767 return isl_basic_map_plain_has_fixed_var(bmap,
10768 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10771 /* If "bmap" obviously lies on a hyperplane where the given dimension
10772 * has a fixed value, then return that value.
10773 * Otherwise return NaN.
10775 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10776 __isl_keep isl_basic_map *bmap,
10777 enum isl_dim_type type, unsigned pos)
10779 isl_ctx *ctx;
10780 isl_val *v;
10781 isl_bool fixed;
10783 if (!bmap)
10784 return NULL;
10785 ctx = isl_basic_map_get_ctx(bmap);
10786 v = isl_val_alloc(ctx);
10787 if (!v)
10788 return NULL;
10789 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10790 if (fixed < 0)
10791 return isl_val_free(v);
10792 if (fixed) {
10793 isl_int_set_si(v->d, 1);
10794 return v;
10796 isl_val_free(v);
10797 return isl_val_nan(ctx);
10800 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10801 enum isl_dim_type type, unsigned pos, isl_int *val)
10803 if (isl_map_check_range(map, type, pos, 1) < 0)
10804 return isl_bool_error;
10805 return isl_map_plain_has_fixed_var(map,
10806 map_offset(map, type) - 1 + pos, val);
10809 /* If "map" obviously lies on a hyperplane where the given dimension
10810 * has a fixed value, then return that value.
10811 * Otherwise return NaN.
10813 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10814 enum isl_dim_type type, unsigned pos)
10816 isl_ctx *ctx;
10817 isl_val *v;
10818 isl_bool fixed;
10820 if (!map)
10821 return NULL;
10822 ctx = isl_map_get_ctx(map);
10823 v = isl_val_alloc(ctx);
10824 if (!v)
10825 return NULL;
10826 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10827 if (fixed < 0)
10828 return isl_val_free(v);
10829 if (fixed) {
10830 isl_int_set_si(v->d, 1);
10831 return v;
10833 isl_val_free(v);
10834 return isl_val_nan(ctx);
10837 /* If "set" obviously lies on a hyperplane where the given dimension
10838 * has a fixed value, then return that value.
10839 * Otherwise return NaN.
10841 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10842 enum isl_dim_type type, unsigned pos)
10844 return isl_map_plain_get_val_if_fixed(set, type, pos);
10847 /* Return a sequence of values in the same space as "set"
10848 * that are equal to the corresponding set dimensions of "set"
10849 * for those set dimensions that obviously lie on a hyperplane
10850 * where the dimension has a fixed value.
10851 * The other elements are set to NaN.
10853 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10854 __isl_keep isl_set *set)
10856 int i;
10857 isl_size n;
10858 isl_space *space;
10859 isl_multi_val *mv;
10861 space = isl_space_drop_all_params(isl_set_get_space(set));
10862 mv = isl_multi_val_alloc(space);
10863 n = isl_multi_val_size(mv);
10864 if (n < 0)
10865 return isl_multi_val_free(mv);
10867 for (i = 0; i < n; ++i) {
10868 isl_val *v;
10870 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10871 mv = isl_multi_val_set_val(mv, i, v);
10874 return mv;
10877 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10878 * then return this fixed value in *val.
10880 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10881 unsigned dim, isl_int *val)
10883 isl_size nparam;
10885 nparam = isl_basic_set_dim(bset, isl_dim_param);
10886 if (nparam < 0)
10887 return isl_bool_error;
10888 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10891 /* Return -1 if the constraint "c1" should be sorted before "c2"
10892 * and 1 if it should be sorted after "c2".
10893 * Return 0 if the two constraints are the same (up to the constant term).
10895 * In particular, if a constraint involves later variables than another
10896 * then it is sorted after this other constraint.
10897 * uset_gist depends on constraints without existentially quantified
10898 * variables sorting first.
10900 * For constraints that have the same latest variable, those
10901 * with the same coefficient for this latest variable (first in absolute value
10902 * and then in actual value) are grouped together.
10903 * This is useful for detecting pairs of constraints that can
10904 * be chained in their printed representation.
10906 * Finally, within a group, constraints are sorted according to
10907 * their coefficients (excluding the constant term).
10909 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10911 isl_int **c1 = (isl_int **) p1;
10912 isl_int **c2 = (isl_int **) p2;
10913 int l1, l2;
10914 unsigned size = *(unsigned *) arg;
10915 int cmp;
10917 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10918 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10920 if (l1 != l2)
10921 return l1 - l2;
10923 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10924 if (cmp != 0)
10925 return cmp;
10926 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10927 if (cmp != 0)
10928 return -cmp;
10930 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10933 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10934 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10935 * and 0 if the two constraints are the same (up to the constant term).
10937 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10938 isl_int *c1, isl_int *c2)
10940 isl_size total;
10941 unsigned size;
10943 total = isl_basic_map_dim(bmap, isl_dim_all);
10944 if (total < 0)
10945 return -2;
10946 size = total;
10947 return sort_constraint_cmp(&c1, &c2, &size);
10950 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10951 __isl_take isl_basic_map *bmap)
10953 isl_size total;
10954 unsigned size;
10956 if (!bmap)
10957 return NULL;
10958 if (bmap->n_ineq == 0)
10959 return bmap;
10960 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10961 return bmap;
10962 total = isl_basic_map_dim(bmap, isl_dim_all);
10963 if (total < 0)
10964 return isl_basic_map_free(bmap);
10965 size = total;
10966 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10967 &sort_constraint_cmp, &size) < 0)
10968 return isl_basic_map_free(bmap);
10969 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10970 return bmap;
10973 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10974 __isl_take isl_basic_set *bset)
10976 isl_basic_map *bmap = bset_to_bmap(bset);
10977 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10980 __isl_give isl_basic_map *isl_basic_map_normalize(
10981 __isl_take isl_basic_map *bmap)
10983 bmap = isl_basic_map_remove_redundancies(bmap);
10984 bmap = isl_basic_map_sort_constraints(bmap);
10985 return bmap;
10987 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10988 __isl_keep isl_basic_map *bmap2)
10990 int i, cmp;
10991 isl_size total;
10992 isl_space *space1, *space2;
10994 if (!bmap1 || !bmap2)
10995 return -1;
10997 if (bmap1 == bmap2)
10998 return 0;
10999 space1 = isl_basic_map_peek_space(bmap1);
11000 space2 = isl_basic_map_peek_space(bmap2);
11001 cmp = isl_space_cmp(space1, space2);
11002 if (cmp)
11003 return cmp;
11004 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
11005 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
11006 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
11007 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
11008 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11009 return 0;
11010 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
11011 return 1;
11012 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11013 return -1;
11014 if (bmap1->n_eq != bmap2->n_eq)
11015 return bmap1->n_eq - bmap2->n_eq;
11016 if (bmap1->n_ineq != bmap2->n_ineq)
11017 return bmap1->n_ineq - bmap2->n_ineq;
11018 if (bmap1->n_div != bmap2->n_div)
11019 return bmap1->n_div - bmap2->n_div;
11020 total = isl_basic_map_dim(bmap1, isl_dim_all);
11021 if (total < 0)
11022 return -1;
11023 for (i = 0; i < bmap1->n_eq; ++i) {
11024 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
11025 if (cmp)
11026 return cmp;
11028 for (i = 0; i < bmap1->n_ineq; ++i) {
11029 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
11030 if (cmp)
11031 return cmp;
11033 for (i = 0; i < bmap1->n_div; ++i) {
11034 isl_bool unknown1, unknown2;
11036 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
11037 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
11038 if (unknown1 < 0 || unknown2 < 0)
11039 return -1;
11040 if (unknown1 && unknown2)
11041 continue;
11042 if (unknown1)
11043 return 1;
11044 if (unknown2)
11045 return -1;
11046 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
11047 if (cmp)
11048 return cmp;
11050 return 0;
11053 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
11054 __isl_keep isl_basic_set *bset2)
11056 return isl_basic_map_plain_cmp(bset1, bset2);
11059 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
11061 int i, cmp;
11063 if (set1 == set2)
11064 return 0;
11065 if (set1->n != set2->n)
11066 return set1->n - set2->n;
11068 for (i = 0; i < set1->n; ++i) {
11069 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
11070 if (cmp)
11071 return cmp;
11074 return 0;
11077 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
11078 __isl_keep isl_basic_map *bmap2)
11080 if (!bmap1 || !bmap2)
11081 return isl_bool_error;
11082 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
11085 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
11086 __isl_keep isl_basic_set *bset2)
11088 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
11089 bset_to_bmap(bset2));
11092 static int qsort_bmap_cmp(const void *p1, const void *p2)
11094 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
11095 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
11097 return isl_basic_map_plain_cmp(bmap1, bmap2);
11100 /* Sort the basic maps of "map" and remove duplicate basic maps.
11102 * While removing basic maps, we make sure that the basic maps remain
11103 * sorted because isl_map_normalize expects the basic maps of the result
11104 * to be sorted.
11106 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
11108 int i, j;
11110 map = isl_map_remove_empty_parts(map);
11111 if (!map)
11112 return NULL;
11113 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
11114 for (i = map->n - 1; i >= 1; --i) {
11115 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
11116 continue;
11117 isl_basic_map_free(map->p[i-1]);
11118 for (j = i; j < map->n; ++j)
11119 map->p[j - 1] = map->p[j];
11120 map->n--;
11123 return map;
11126 /* Remove obvious duplicates among the basic maps of "map".
11128 * Unlike isl_map_normalize, this function does not remove redundant
11129 * constraints and only removes duplicates that have exactly the same
11130 * constraints in the input. It does sort the constraints and
11131 * the basic maps to ease the detection of duplicates.
11133 * If "map" has already been normalized or if the basic maps are
11134 * disjoint, then there can be no duplicates.
11136 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
11138 int i;
11139 isl_basic_map *bmap;
11141 if (!map)
11142 return NULL;
11143 if (map->n <= 1)
11144 return map;
11145 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
11146 return map;
11147 for (i = 0; i < map->n; ++i) {
11148 bmap = isl_basic_map_copy(map->p[i]);
11149 bmap = isl_basic_map_sort_constraints(bmap);
11150 if (!bmap)
11151 return isl_map_free(map);
11152 isl_basic_map_free(map->p[i]);
11153 map->p[i] = bmap;
11156 map = sort_and_remove_duplicates(map);
11157 return map;
11160 /* We normalize in place, but if anything goes wrong we need
11161 * to return NULL, so we need to make sure we don't change the
11162 * meaning of any possible other copies of map.
11164 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
11166 int i;
11167 struct isl_basic_map *bmap;
11169 if (!map)
11170 return NULL;
11171 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
11172 return map;
11173 for (i = 0; i < map->n; ++i) {
11174 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
11175 if (!bmap)
11176 goto error;
11177 isl_basic_map_free(map->p[i]);
11178 map->p[i] = bmap;
11181 map = sort_and_remove_duplicates(map);
11182 if (map)
11183 ISL_F_SET(map, ISL_MAP_NORMALIZED);
11184 return map;
11185 error:
11186 isl_map_free(map);
11187 return NULL;
11190 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
11192 return set_from_map(isl_map_normalize(set_to_map(set)));
11195 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
11196 __isl_keep isl_map *map2)
11198 int i;
11199 isl_bool equal;
11201 if (!map1 || !map2)
11202 return isl_bool_error;
11204 if (map1 == map2)
11205 return isl_bool_true;
11206 equal = isl_map_has_equal_space(map1, map2);
11207 if (equal < 0 || !equal)
11208 return equal;
11210 map1 = isl_map_copy(map1);
11211 map2 = isl_map_copy(map2);
11212 map1 = isl_map_normalize(map1);
11213 map2 = isl_map_normalize(map2);
11214 if (!map1 || !map2)
11215 goto error;
11216 equal = map1->n == map2->n;
11217 for (i = 0; equal && i < map1->n; ++i) {
11218 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
11219 if (equal < 0)
11220 goto error;
11222 isl_map_free(map1);
11223 isl_map_free(map2);
11224 return equal;
11225 error:
11226 isl_map_free(map1);
11227 isl_map_free(map2);
11228 return isl_bool_error;
11231 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
11232 __isl_keep isl_set *set2)
11234 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
11237 /* Return the basic maps in "map" as a list.
11239 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
11240 __isl_keep isl_map *map)
11242 int i;
11243 isl_ctx *ctx;
11244 isl_basic_map_list *list;
11246 if (!map)
11247 return NULL;
11248 ctx = isl_map_get_ctx(map);
11249 list = isl_basic_map_list_alloc(ctx, map->n);
11251 for (i = 0; i < map->n; ++i) {
11252 isl_basic_map *bmap;
11254 bmap = isl_basic_map_copy(map->p[i]);
11255 list = isl_basic_map_list_add(list, bmap);
11258 return list;
11261 /* Return the intersection of the elements in the non-empty list "list".
11262 * All elements are assumed to live in the same space.
11264 __isl_give isl_basic_map *isl_basic_map_list_intersect(
11265 __isl_take isl_basic_map_list *list)
11267 int i;
11268 isl_size n;
11269 isl_basic_map *bmap;
11271 n = isl_basic_map_list_n_basic_map(list);
11272 if (n < 0)
11273 goto error;
11274 if (n < 1)
11275 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
11276 "expecting non-empty list", goto error);
11278 bmap = isl_basic_map_list_get_basic_map(list, 0);
11279 for (i = 1; i < n; ++i) {
11280 isl_basic_map *bmap_i;
11282 bmap_i = isl_basic_map_list_get_basic_map(list, i);
11283 bmap = isl_basic_map_intersect(bmap, bmap_i);
11286 isl_basic_map_list_free(list);
11287 return bmap;
11288 error:
11289 isl_basic_map_list_free(list);
11290 return NULL;
11293 /* Return the intersection of the elements in the non-empty list "list".
11294 * All elements are assumed to live in the same space.
11296 __isl_give isl_basic_set *isl_basic_set_list_intersect(
11297 __isl_take isl_basic_set_list *list)
11299 return isl_basic_map_list_intersect(list);
11302 /* Return the union of the elements of "list".
11303 * The list is required to have at least one element.
11305 __isl_give isl_set *isl_basic_set_list_union(
11306 __isl_take isl_basic_set_list *list)
11308 int i;
11309 isl_size n;
11310 isl_space *space;
11311 isl_basic_set *bset;
11312 isl_set *set;
11314 n = isl_basic_set_list_n_basic_set(list);
11315 if (n < 0)
11316 goto error;
11317 if (n < 1)
11318 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
11319 "expecting non-empty list", goto error);
11321 bset = isl_basic_set_list_get_basic_set(list, 0);
11322 space = isl_basic_set_get_space(bset);
11323 isl_basic_set_free(bset);
11325 set = isl_set_alloc_space(space, n, 0);
11326 for (i = 0; i < n; ++i) {
11327 bset = isl_basic_set_list_get_basic_set(list, i);
11328 set = isl_set_add_basic_set(set, bset);
11331 isl_basic_set_list_free(list);
11332 return set;
11333 error:
11334 isl_basic_set_list_free(list);
11335 return NULL;
11338 /* Return the union of the elements in the non-empty list "list".
11339 * All elements are assumed to live in the same space.
11341 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
11343 int i;
11344 isl_size n;
11345 isl_set *set;
11347 n = isl_set_list_n_set(list);
11348 if (n < 0)
11349 goto error;
11350 if (n < 1)
11351 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
11352 "expecting non-empty list", goto error);
11354 set = isl_set_list_get_set(list, 0);
11355 for (i = 1; i < n; ++i) {
11356 isl_set *set_i;
11358 set_i = isl_set_list_get_set(list, i);
11359 set = isl_set_union(set, set_i);
11362 isl_set_list_free(list);
11363 return set;
11364 error:
11365 isl_set_list_free(list);
11366 return NULL;
11369 __isl_give isl_basic_map *isl_basic_map_product(
11370 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11372 isl_space *space_result = NULL;
11373 struct isl_basic_map *bmap;
11374 unsigned in1, in2, out1, out2, nparam, total, pos;
11375 struct isl_dim_map *dim_map1, *dim_map2;
11377 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11378 goto error;
11379 space_result = isl_space_product(isl_space_copy(bmap1->dim),
11380 isl_space_copy(bmap2->dim));
11382 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11383 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11384 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11385 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11386 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11388 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
11389 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11390 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11391 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11392 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11393 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11394 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11395 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11396 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11397 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11398 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11400 bmap = isl_basic_map_alloc_space(space_result,
11401 bmap1->n_div + bmap2->n_div,
11402 bmap1->n_eq + bmap2->n_eq,
11403 bmap1->n_ineq + bmap2->n_ineq);
11404 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11405 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11406 bmap = isl_basic_map_simplify(bmap);
11407 return isl_basic_map_finalize(bmap);
11408 error:
11409 isl_basic_map_free(bmap1);
11410 isl_basic_map_free(bmap2);
11411 return NULL;
11414 __isl_give isl_basic_map *isl_basic_map_flat_product(
11415 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11417 isl_basic_map *prod;
11419 prod = isl_basic_map_product(bmap1, bmap2);
11420 prod = isl_basic_map_flatten(prod);
11421 return prod;
11424 __isl_give isl_basic_set *isl_basic_set_flat_product(
11425 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
11427 return isl_basic_map_flat_range_product(bset1, bset2);
11430 __isl_give isl_basic_map *isl_basic_map_domain_product(
11431 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11433 isl_space *space1, *space2;
11434 isl_space *space_result = NULL;
11435 isl_basic_map *bmap;
11436 isl_size in1, in2, out, nparam;
11437 unsigned total, pos;
11438 struct isl_dim_map *dim_map1, *dim_map2;
11440 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11441 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11442 out = isl_basic_map_dim(bmap1, isl_dim_out);
11443 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11444 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
11445 goto error;
11447 space1 = isl_basic_map_get_space(bmap1);
11448 space2 = isl_basic_map_get_space(bmap2);
11449 space_result = isl_space_domain_product(space1, space2);
11451 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
11452 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11453 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11454 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11455 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11456 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11457 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11458 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11459 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
11460 isl_dim_map_div(dim_map1, bmap1, pos += out);
11461 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11463 bmap = isl_basic_map_alloc_space(space_result,
11464 bmap1->n_div + bmap2->n_div,
11465 bmap1->n_eq + bmap2->n_eq,
11466 bmap1->n_ineq + bmap2->n_ineq);
11467 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11468 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11469 bmap = isl_basic_map_simplify(bmap);
11470 return isl_basic_map_finalize(bmap);
11471 error:
11472 isl_basic_map_free(bmap1);
11473 isl_basic_map_free(bmap2);
11474 return NULL;
11477 __isl_give isl_basic_map *isl_basic_map_range_product(
11478 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11480 isl_bool rational;
11481 isl_space *space_result = NULL;
11482 isl_basic_map *bmap;
11483 isl_size in, out1, out2, nparam;
11484 unsigned total, pos;
11485 struct isl_dim_map *dim_map1, *dim_map2;
11487 rational = isl_basic_map_is_rational(bmap1);
11488 if (rational >= 0 && rational)
11489 rational = isl_basic_map_is_rational(bmap2);
11490 in = isl_basic_map_dim(bmap1, isl_dim_in);
11491 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11492 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11493 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11494 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
11495 goto error;
11497 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11498 goto error;
11500 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
11501 isl_space_copy(bmap2->dim));
11503 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
11504 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11505 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11506 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11507 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11508 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11509 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11510 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11511 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11512 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11513 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11515 bmap = isl_basic_map_alloc_space(space_result,
11516 bmap1->n_div + bmap2->n_div,
11517 bmap1->n_eq + bmap2->n_eq,
11518 bmap1->n_ineq + bmap2->n_ineq);
11519 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11520 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11521 if (rational)
11522 bmap = isl_basic_map_set_rational(bmap);
11523 bmap = isl_basic_map_simplify(bmap);
11524 return isl_basic_map_finalize(bmap);
11525 error:
11526 isl_basic_map_free(bmap1);
11527 isl_basic_map_free(bmap2);
11528 return NULL;
11531 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11532 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11534 isl_basic_map *prod;
11536 prod = isl_basic_map_range_product(bmap1, bmap2);
11537 prod = isl_basic_map_flatten_range(prod);
11538 return prod;
11541 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11542 * and collect the results.
11543 * The result live in the space obtained by calling "space_product"
11544 * on the spaces of "map1" and "map2".
11545 * If "remove_duplicates" is set then the result may contain duplicates
11546 * (even if the inputs do not) and so we try and remove the obvious
11547 * duplicates.
11549 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11550 __isl_take isl_map *map2,
11551 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11552 __isl_take isl_space *right),
11553 __isl_give isl_basic_map *(*basic_map_product)(
11554 __isl_take isl_basic_map *left,
11555 __isl_take isl_basic_map *right),
11556 int remove_duplicates)
11558 unsigned flags = 0;
11559 struct isl_map *result;
11560 int i, j;
11561 isl_bool m;
11563 m = isl_map_has_equal_params(map1, map2);
11564 if (m < 0)
11565 goto error;
11566 if (!m)
11567 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11568 "parameters don't match", goto error);
11570 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11571 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11572 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11574 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11575 isl_space_copy(map2->dim)),
11576 map1->n * map2->n, flags);
11577 if (!result)
11578 goto error;
11579 for (i = 0; i < map1->n; ++i)
11580 for (j = 0; j < map2->n; ++j) {
11581 struct isl_basic_map *part;
11582 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11583 isl_basic_map_copy(map2->p[j]));
11584 if (isl_basic_map_is_empty(part))
11585 isl_basic_map_free(part);
11586 else
11587 result = isl_map_add_basic_map(result, part);
11588 if (!result)
11589 goto error;
11591 if (remove_duplicates)
11592 result = isl_map_remove_obvious_duplicates(result);
11593 isl_map_free(map1);
11594 isl_map_free(map2);
11595 return result;
11596 error:
11597 isl_map_free(map1);
11598 isl_map_free(map2);
11599 return NULL;
11602 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11604 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11605 __isl_take isl_map *map2)
11607 isl_map_align_params_bin(&map1, &map2);
11608 return map_product(map1, map2, &isl_space_product,
11609 &isl_basic_map_product, 0);
11612 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11614 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11615 __isl_take isl_map *map2)
11617 isl_map *prod;
11619 prod = isl_map_product(map1, map2);
11620 prod = isl_map_flatten(prod);
11621 return prod;
11624 /* Given two set A and B, construct its Cartesian product A x B.
11626 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11627 __isl_take isl_set *set2)
11629 return isl_map_range_product(set1, set2);
11632 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11633 __isl_take isl_set *set2)
11635 return isl_map_flat_range_product(set1, set2);
11638 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11640 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11641 __isl_take isl_map *map2)
11643 isl_map_align_params_bin(&map1, &map2);
11644 return map_product(map1, map2, &isl_space_domain_product,
11645 &isl_basic_map_domain_product, 1);
11648 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11650 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11651 __isl_take isl_map *map2)
11653 isl_map_align_params_bin(&map1, &map2);
11654 return map_product(map1, map2, &isl_space_range_product,
11655 &isl_basic_map_range_product, 1);
11658 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11660 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11662 isl_space *space;
11663 isl_size total1, keep1, total2, keep2;
11665 total1 = isl_map_dim(map, isl_dim_in);
11666 total2 = isl_map_dim(map, isl_dim_out);
11667 if (total1 < 0 || total2 < 0)
11668 return isl_map_free(map);
11669 if (!isl_space_domain_is_wrapping(map->dim) ||
11670 !isl_space_range_is_wrapping(map->dim))
11671 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11672 "not a product", return isl_map_free(map));
11674 space = isl_map_get_space(map);
11675 space = isl_space_factor_domain(space);
11676 keep1 = isl_space_dim(space, isl_dim_in);
11677 keep2 = isl_space_dim(space, isl_dim_out);
11678 if (keep1 < 0 || keep2 < 0)
11679 map = isl_map_free(map);
11680 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11681 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11682 map = isl_map_reset_space(map, space);
11684 return map;
11687 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11689 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11691 isl_space *space;
11692 isl_size total1, keep1, total2, keep2;
11694 total1 = isl_map_dim(map, isl_dim_in);
11695 total2 = isl_map_dim(map, isl_dim_out);
11696 if (total1 < 0 || total2 < 0)
11697 return isl_map_free(map);
11698 if (!isl_space_domain_is_wrapping(map->dim) ||
11699 !isl_space_range_is_wrapping(map->dim))
11700 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11701 "not a product", return isl_map_free(map));
11703 space = isl_map_get_space(map);
11704 space = isl_space_factor_range(space);
11705 keep1 = isl_space_dim(space, isl_dim_in);
11706 keep2 = isl_space_dim(space, isl_dim_out);
11707 if (keep1 < 0 || keep2 < 0)
11708 map = isl_map_free(map);
11709 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11710 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11711 map = isl_map_reset_space(map, space);
11713 return map;
11716 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11718 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11720 isl_space *space;
11721 isl_size total, keep;
11723 total = isl_map_dim(map, isl_dim_in);
11724 if (total < 0)
11725 return isl_map_free(map);
11726 if (!isl_space_domain_is_wrapping(map->dim))
11727 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11728 "domain is not a product", return isl_map_free(map));
11730 space = isl_map_get_space(map);
11731 space = isl_space_domain_factor_domain(space);
11732 keep = isl_space_dim(space, isl_dim_in);
11733 if (keep < 0)
11734 map = isl_map_free(map);
11735 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11736 map = isl_map_reset_space(map, space);
11738 return map;
11741 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11743 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11745 isl_space *space;
11746 isl_size total, keep;
11748 total = isl_map_dim(map, isl_dim_in);
11749 if (total < 0)
11750 return isl_map_free(map);
11751 if (!isl_space_domain_is_wrapping(map->dim))
11752 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11753 "domain is not a product", return isl_map_free(map));
11755 space = isl_map_get_space(map);
11756 space = isl_space_domain_factor_range(space);
11757 keep = isl_space_dim(space, isl_dim_in);
11758 if (keep < 0)
11759 map = isl_map_free(map);
11760 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11761 map = isl_map_reset_space(map, space);
11763 return map;
11766 /* Given a map A -> [B -> C], extract the map A -> B.
11768 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11770 isl_space *space;
11771 isl_size total, keep;
11773 total = isl_map_dim(map, isl_dim_out);
11774 if (total < 0)
11775 return isl_map_free(map);
11776 if (!isl_space_range_is_wrapping(map->dim))
11777 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11778 "range is not a product", return isl_map_free(map));
11780 space = isl_map_get_space(map);
11781 space = isl_space_range_factor_domain(space);
11782 keep = isl_space_dim(space, isl_dim_out);
11783 if (keep < 0)
11784 map = isl_map_free(map);
11785 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11786 map = isl_map_reset_space(map, space);
11788 return map;
11791 /* Given a map A -> [B -> C], extract the map A -> C.
11793 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11795 isl_space *space;
11796 isl_size total, keep;
11798 total = isl_map_dim(map, isl_dim_out);
11799 if (total < 0)
11800 return isl_map_free(map);
11801 if (!isl_space_range_is_wrapping(map->dim))
11802 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11803 "range is not a product", return isl_map_free(map));
11805 space = isl_map_get_space(map);
11806 space = isl_space_range_factor_range(space);
11807 keep = isl_space_dim(space, isl_dim_out);
11808 if (keep < 0)
11809 map = isl_map_free(map);
11810 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11811 map = isl_map_reset_space(map, space);
11813 return map;
11816 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11818 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11819 __isl_take isl_map *map2)
11821 isl_map *prod;
11823 prod = isl_map_domain_product(map1, map2);
11824 prod = isl_map_flatten_domain(prod);
11825 return prod;
11828 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11830 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11831 __isl_take isl_map *map2)
11833 isl_map *prod;
11835 prod = isl_map_range_product(map1, map2);
11836 prod = isl_map_flatten_range(prod);
11837 return prod;
11840 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11842 int i;
11843 uint32_t hash = isl_hash_init();
11844 isl_size total;
11846 if (!bmap)
11847 return 0;
11848 bmap = isl_basic_map_copy(bmap);
11849 bmap = isl_basic_map_normalize(bmap);
11850 total = isl_basic_map_dim(bmap, isl_dim_all);
11851 if (total < 0)
11852 return 0;
11853 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11854 for (i = 0; i < bmap->n_eq; ++i) {
11855 uint32_t c_hash;
11856 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11857 isl_hash_hash(hash, c_hash);
11859 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11860 for (i = 0; i < bmap->n_ineq; ++i) {
11861 uint32_t c_hash;
11862 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11863 isl_hash_hash(hash, c_hash);
11865 isl_hash_byte(hash, bmap->n_div & 0xFF);
11866 for (i = 0; i < bmap->n_div; ++i) {
11867 uint32_t c_hash;
11868 if (isl_int_is_zero(bmap->div[i][0]))
11869 continue;
11870 isl_hash_byte(hash, i & 0xFF);
11871 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11872 isl_hash_hash(hash, c_hash);
11874 isl_basic_map_free(bmap);
11875 return hash;
11878 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11880 return isl_basic_map_get_hash(bset_to_bmap(bset));
11883 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11885 int i;
11886 uint32_t hash;
11888 if (!map)
11889 return 0;
11890 map = isl_map_copy(map);
11891 map = isl_map_normalize(map);
11892 if (!map)
11893 return 0;
11895 hash = isl_hash_init();
11896 for (i = 0; i < map->n; ++i) {
11897 uint32_t bmap_hash;
11898 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11899 isl_hash_hash(hash, bmap_hash);
11902 isl_map_free(map);
11904 return hash;
11907 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11909 return isl_map_get_hash(set_to_map(set));
11912 /* Return the number of basic maps in the (current) representation of "map".
11914 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11916 return map ? map->n : isl_size_error;
11919 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11921 return set ? set->n : isl_size_error;
11924 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11925 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11927 int i;
11929 if (!map)
11930 return isl_stat_error;
11932 for (i = 0; i < map->n; ++i)
11933 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11934 return isl_stat_error;
11936 return isl_stat_ok;
11939 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11940 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11942 int i;
11944 if (!set)
11945 return isl_stat_error;
11947 for (i = 0; i < set->n; ++i)
11948 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11949 return isl_stat_error;
11951 return isl_stat_ok;
11954 /* Does "test" succeed on every basic set in "set"?
11956 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11957 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11958 void *user)
11960 int i;
11962 if (!set)
11963 return isl_bool_error;
11965 for (i = 0; i < set->n; ++i) {
11966 isl_bool r;
11968 r = test(set->p[i], user);
11969 if (r < 0 || !r)
11970 return r;
11973 return isl_bool_true;
11976 /* Return a list of basic sets, the union of which is equal to "set".
11978 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11979 __isl_keep isl_set *set)
11981 int i;
11982 isl_basic_set_list *list;
11984 if (!set)
11985 return NULL;
11987 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11988 for (i = 0; i < set->n; ++i) {
11989 isl_basic_set *bset;
11991 bset = isl_basic_set_copy(set->p[i]);
11992 list = isl_basic_set_list_add(list, bset);
11995 return list;
11998 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
12000 isl_space *space;
12002 if (!bset)
12003 return NULL;
12005 bset = isl_basic_set_cow(bset);
12006 if (!bset)
12007 return NULL;
12009 space = isl_basic_set_get_space(bset);
12010 space = isl_space_lift(space, bset->n_div);
12011 if (!space)
12012 goto error;
12013 isl_space_free(bset->dim);
12014 bset->dim = space;
12015 bset->extra -= bset->n_div;
12016 bset->n_div = 0;
12018 bset = isl_basic_set_finalize(bset);
12020 return bset;
12021 error:
12022 isl_basic_set_free(bset);
12023 return NULL;
12026 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
12028 int i;
12029 isl_space *space;
12030 unsigned n_div;
12032 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
12034 if (!set)
12035 return NULL;
12037 set = isl_set_cow(set);
12038 if (!set)
12039 return NULL;
12041 n_div = set->p[0]->n_div;
12042 space = isl_set_get_space(set);
12043 space = isl_space_lift(space, n_div);
12044 if (!space)
12045 goto error;
12046 isl_space_free(set->dim);
12047 set->dim = space;
12049 for (i = 0; i < set->n; ++i) {
12050 set->p[i] = isl_basic_set_lift(set->p[i]);
12051 if (!set->p[i])
12052 goto error;
12055 return set;
12056 error:
12057 isl_set_free(set);
12058 return NULL;
12061 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
12063 isl_size dim;
12064 int size = 0;
12066 dim = isl_basic_set_dim(bset, isl_dim_all);
12067 if (dim < 0)
12068 return -1;
12069 size += bset->n_eq * (1 + dim);
12070 size += bset->n_ineq * (1 + dim);
12071 size += bset->n_div * (2 + dim);
12073 return size;
12076 int isl_set_size(__isl_keep isl_set *set)
12078 int i;
12079 int size = 0;
12081 if (!set)
12082 return -1;
12084 for (i = 0; i < set->n; ++i)
12085 size += isl_basic_set_size(set->p[i]);
12087 return size;
12090 /* Check if there is any lower bound (if lower == 0) and/or upper
12091 * bound (if upper == 0) on the specified dim.
12093 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
12094 enum isl_dim_type type, unsigned pos, int lower, int upper)
12096 int i;
12097 isl_bool involves;
12098 isl_size off;
12100 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
12101 return isl_bool_error;
12103 off = isl_basic_map_var_offset(bmap, type);
12104 if (off < 0)
12105 return isl_bool_error;
12106 involves = isl_basic_map_any_div_involves_vars(bmap, off + pos, 1);
12107 if (involves < 0 || involves)
12108 return involves;
12110 for (i = 0; i < bmap->n_eq; ++i)
12111 if (!isl_int_is_zero(bmap->eq[i][1 + off + pos]))
12112 return isl_bool_true;
12114 for (i = 0; i < bmap->n_ineq; ++i) {
12115 int sgn = isl_int_sgn(bmap->ineq[i][1 + off + pos]);
12116 if (sgn > 0)
12117 lower = 1;
12118 if (sgn < 0)
12119 upper = 1;
12122 return lower && upper;
12125 isl_bool isl_basic_map_dim_is_bounded(__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, 0);
12131 isl_bool isl_basic_map_dim_has_lower_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, 0, 1);
12137 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
12138 enum isl_dim_type type, unsigned pos)
12140 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
12143 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
12144 enum isl_dim_type type, unsigned pos)
12146 int i;
12148 if (!map)
12149 return isl_bool_error;
12151 for (i = 0; i < map->n; ++i) {
12152 isl_bool bounded;
12153 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
12154 if (bounded < 0 || !bounded)
12155 return bounded;
12158 return isl_bool_true;
12161 /* Return true if the specified dim is involved in both an upper bound
12162 * and a lower bound.
12164 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
12165 enum isl_dim_type type, unsigned pos)
12167 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
12170 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12172 static isl_bool has_any_bound(__isl_keep isl_map *map,
12173 enum isl_dim_type type, unsigned pos,
12174 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12175 enum isl_dim_type type, unsigned pos))
12177 int i;
12179 if (!map)
12180 return isl_bool_error;
12182 for (i = 0; i < map->n; ++i) {
12183 isl_bool bounded;
12184 bounded = fn(map->p[i], type, pos);
12185 if (bounded < 0 || bounded)
12186 return bounded;
12189 return isl_bool_false;
12192 /* Return 1 if the specified dim is involved in any lower bound.
12194 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
12195 enum isl_dim_type type, unsigned pos)
12197 return has_any_bound(set, type, pos,
12198 &isl_basic_map_dim_has_lower_bound);
12201 /* Return 1 if the specified dim is involved in any upper bound.
12203 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
12204 enum isl_dim_type type, unsigned pos)
12206 return has_any_bound(set, type, pos,
12207 &isl_basic_map_dim_has_upper_bound);
12210 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12212 static isl_bool has_bound(__isl_keep isl_map *map,
12213 enum isl_dim_type type, unsigned pos,
12214 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12215 enum isl_dim_type type, unsigned pos))
12217 int i;
12219 if (!map)
12220 return isl_bool_error;
12222 for (i = 0; i < map->n; ++i) {
12223 isl_bool bounded;
12224 bounded = fn(map->p[i], type, pos);
12225 if (bounded < 0 || !bounded)
12226 return bounded;
12229 return isl_bool_true;
12232 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12234 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
12235 enum isl_dim_type type, unsigned pos)
12237 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
12240 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12242 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
12243 enum isl_dim_type type, unsigned pos)
12245 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
12248 /* For each of the "n" variables starting at "first", determine
12249 * the sign of the variable and put the results in the first "n"
12250 * elements of the array "signs".
12251 * Sign
12252 * 1 means that the variable is non-negative
12253 * -1 means that the variable is non-positive
12254 * 0 means the variable attains both positive and negative values.
12256 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
12257 unsigned first, unsigned n, int *signs)
12259 isl_vec *bound = NULL;
12260 struct isl_tab *tab = NULL;
12261 struct isl_tab_undo *snap;
12262 int i;
12263 isl_size total;
12265 total = isl_basic_set_dim(bset, isl_dim_all);
12266 if (total < 0 || !signs)
12267 return isl_stat_error;
12269 bound = isl_vec_alloc(bset->ctx, 1 + total);
12270 tab = isl_tab_from_basic_set(bset, 0);
12271 if (!bound || !tab)
12272 goto error;
12274 isl_seq_clr(bound->el, bound->size);
12275 isl_int_set_si(bound->el[0], -1);
12277 snap = isl_tab_snap(tab);
12278 for (i = 0; i < n; ++i) {
12279 int empty;
12281 isl_int_set_si(bound->el[1 + first + i], -1);
12282 if (isl_tab_add_ineq(tab, bound->el) < 0)
12283 goto error;
12284 empty = tab->empty;
12285 isl_int_set_si(bound->el[1 + first + i], 0);
12286 if (isl_tab_rollback(tab, snap) < 0)
12287 goto error;
12289 if (empty) {
12290 signs[i] = 1;
12291 continue;
12294 isl_int_set_si(bound->el[1 + first + i], 1);
12295 if (isl_tab_add_ineq(tab, bound->el) < 0)
12296 goto error;
12297 empty = tab->empty;
12298 isl_int_set_si(bound->el[1 + first + i], 0);
12299 if (isl_tab_rollback(tab, snap) < 0)
12300 goto error;
12302 signs[i] = empty ? -1 : 0;
12305 isl_tab_free(tab);
12306 isl_vec_free(bound);
12307 return isl_stat_ok;
12308 error:
12309 isl_tab_free(tab);
12310 isl_vec_free(bound);
12311 return isl_stat_error;
12314 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
12315 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
12317 if (!bset || !signs)
12318 return isl_stat_error;
12319 if (isl_basic_set_check_range(bset, type, first, n) < 0)
12320 return isl_stat_error;
12322 first += pos(bset->dim, type) - 1;
12323 return isl_basic_set_vars_get_sign(bset, first, n, signs);
12326 /* Is it possible for the integer division "div" to depend (possibly
12327 * indirectly) on any output dimensions?
12329 * If the div is undefined, then we conservatively assume that it
12330 * may depend on them.
12331 * Otherwise, we check if it actually depends on them or on any integer
12332 * divisions that may depend on them.
12334 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
12336 int i;
12337 isl_size n_out, n_div;
12338 unsigned o_out, o_div;
12340 if (isl_int_is_zero(bmap->div[div][0]))
12341 return isl_bool_true;
12343 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12344 if (n_out < 0)
12345 return isl_bool_error;
12346 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12348 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
12349 return isl_bool_true;
12351 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12352 if (n_div < 0)
12353 return isl_bool_error;
12354 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12356 for (i = 0; i < n_div; ++i) {
12357 isl_bool may_involve;
12359 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
12360 continue;
12361 may_involve = div_may_involve_output(bmap, i);
12362 if (may_involve < 0 || may_involve)
12363 return may_involve;
12366 return isl_bool_false;
12369 /* Return the first integer division of "bmap" in the range
12370 * [first, first + n[ that may depend on any output dimensions and
12371 * that has a non-zero coefficient in "c" (where the first coefficient
12372 * in "c" corresponds to integer division "first").
12374 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
12375 isl_int *c, int first, int n)
12377 int k;
12379 if (!bmap)
12380 return -1;
12382 for (k = first; k < first + n; ++k) {
12383 isl_bool may_involve;
12385 if (isl_int_is_zero(c[k]))
12386 continue;
12387 may_involve = div_may_involve_output(bmap, k);
12388 if (may_involve < 0)
12389 return -1;
12390 if (may_involve)
12391 return k;
12394 return first + n;
12397 /* Look for a pair of inequality constraints in "bmap" of the form
12399 * -l + i >= 0 or i >= l
12400 * and
12401 * n + l - i >= 0 or i <= l + n
12403 * with n < "m" and i the output dimension at position "pos".
12404 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12405 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12406 * and earlier output dimensions, as well as integer divisions that do
12407 * not involve any of the output dimensions.
12409 * Return the index of the first inequality constraint or bmap->n_ineq
12410 * if no such pair can be found.
12412 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
12413 int pos, isl_int m)
12415 int i, j;
12416 isl_ctx *ctx;
12417 isl_size total;
12418 isl_size n_div, n_out;
12419 unsigned o_div, o_out;
12420 int less;
12422 total = isl_basic_map_dim(bmap, isl_dim_all);
12423 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12424 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12425 if (total < 0 || n_out < 0 || n_div < 0)
12426 return -1;
12428 ctx = isl_basic_map_get_ctx(bmap);
12429 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12430 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12431 for (i = 0; i < bmap->n_ineq; ++i) {
12432 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
12433 continue;
12434 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
12435 n_out - (pos + 1)) != -1)
12436 continue;
12437 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
12438 0, n_div) < n_div)
12439 continue;
12440 for (j = i + 1; j < bmap->n_ineq; ++j) {
12441 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
12442 ctx->one))
12443 continue;
12444 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
12445 bmap->ineq[j] + 1, total))
12446 continue;
12447 break;
12449 if (j >= bmap->n_ineq)
12450 continue;
12451 isl_int_add(bmap->ineq[i][0],
12452 bmap->ineq[i][0], bmap->ineq[j][0]);
12453 less = isl_int_abs_lt(bmap->ineq[i][0], m);
12454 isl_int_sub(bmap->ineq[i][0],
12455 bmap->ineq[i][0], bmap->ineq[j][0]);
12456 if (!less)
12457 continue;
12458 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
12459 return i;
12460 else
12461 return j;
12464 return bmap->n_ineq;
12467 /* Return the index of the equality of "bmap" that defines
12468 * the output dimension "pos" in terms of earlier dimensions.
12469 * The equality may also involve integer divisions, as long
12470 * as those integer divisions are defined in terms of
12471 * parameters or input dimensions.
12472 * In this case, *div is set to the number of integer divisions and
12473 * *ineq is set to the number of inequality constraints (provided
12474 * div and ineq are not NULL).
12476 * The equality may also involve a single integer division involving
12477 * the output dimensions (typically only output dimension "pos") as
12478 * long as the coefficient of output dimension "pos" is 1 or -1 and
12479 * there is a pair of constraints i >= l and i <= l + n, with i referring
12480 * to output dimension "pos", l an expression involving only earlier
12481 * dimensions and n smaller than the coefficient of the integer division
12482 * in the equality. In this case, the output dimension can be defined
12483 * in terms of a modulo expression that does not involve the integer division.
12484 * *div is then set to this single integer division and
12485 * *ineq is set to the index of constraint i >= l.
12487 * Return bmap->n_eq if there is no such equality.
12488 * Return -1 on error.
12490 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
12491 int pos, int *div, int *ineq)
12493 int j, k, l;
12494 isl_size n_div, n_out;
12495 unsigned o_div, o_out;
12497 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12498 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12499 if (n_out < 0 || n_div < 0)
12500 return -1;
12502 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12503 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12505 if (ineq)
12506 *ineq = bmap->n_ineq;
12507 if (div)
12508 *div = n_div;
12509 for (j = 0; j < bmap->n_eq; ++j) {
12510 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12511 continue;
12512 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
12513 n_out - (pos + 1)) != -1)
12514 continue;
12515 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12516 0, n_div);
12517 if (k >= n_div)
12518 return j;
12519 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12520 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12521 continue;
12522 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12523 k + 1, n_div - (k+1)) < n_div)
12524 continue;
12525 l = find_modulo_constraint_pair(bmap, pos,
12526 bmap->eq[j][o_div + k]);
12527 if (l < 0)
12528 return -1;
12529 if (l >= bmap->n_ineq)
12530 continue;
12531 if (div)
12532 *div = k;
12533 if (ineq)
12534 *ineq = l;
12535 return j;
12538 return bmap->n_eq;
12541 /* Check if the given basic map is obviously single-valued.
12542 * In particular, for each output dimension, check that there is
12543 * an equality that defines the output dimension in terms of
12544 * earlier dimensions.
12546 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12548 int i;
12549 isl_size n_out;
12551 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12552 if (n_out < 0)
12553 return isl_bool_error;
12555 for (i = 0; i < n_out; ++i) {
12556 int eq;
12558 eq = isl_basic_map_output_defining_equality(bmap, i,
12559 NULL, NULL);
12560 if (eq < 0)
12561 return isl_bool_error;
12562 if (eq >= bmap->n_eq)
12563 return isl_bool_false;
12566 return isl_bool_true;
12569 /* Check if the given basic map is single-valued.
12570 * We simply compute
12572 * M \circ M^-1
12574 * and check if the result is a subset of the identity mapping.
12576 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12578 isl_space *space;
12579 isl_basic_map *test;
12580 isl_basic_map *id;
12581 isl_bool sv;
12583 sv = isl_basic_map_plain_is_single_valued(bmap);
12584 if (sv < 0 || sv)
12585 return sv;
12587 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12588 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12590 space = isl_basic_map_get_space(bmap);
12591 space = isl_space_map_from_set(isl_space_range(space));
12592 id = isl_basic_map_identity(space);
12594 sv = isl_basic_map_is_subset(test, id);
12596 isl_basic_map_free(test);
12597 isl_basic_map_free(id);
12599 return sv;
12602 /* Check if the given map is obviously single-valued.
12604 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12606 if (!map)
12607 return isl_bool_error;
12608 if (map->n == 0)
12609 return isl_bool_true;
12610 if (map->n >= 2)
12611 return isl_bool_false;
12613 return isl_basic_map_plain_is_single_valued(map->p[0]);
12616 /* Check if the given map is single-valued.
12617 * We simply compute
12619 * M \circ M^-1
12621 * and check if the result is a subset of the identity mapping.
12623 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12625 isl_space *space;
12626 isl_map *test;
12627 isl_map *id;
12628 isl_bool sv;
12630 sv = isl_map_plain_is_single_valued(map);
12631 if (sv < 0 || sv)
12632 return sv;
12634 test = isl_map_reverse(isl_map_copy(map));
12635 test = isl_map_apply_range(test, isl_map_copy(map));
12637 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12638 id = isl_map_identity(space);
12640 sv = isl_map_is_subset(test, id);
12642 isl_map_free(test);
12643 isl_map_free(id);
12645 return sv;
12648 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12650 isl_bool in;
12652 map = isl_map_copy(map);
12653 map = isl_map_reverse(map);
12654 in = isl_map_is_single_valued(map);
12655 isl_map_free(map);
12657 return in;
12660 /* Check if the given map is obviously injective.
12662 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12664 isl_bool in;
12666 map = isl_map_copy(map);
12667 map = isl_map_reverse(map);
12668 in = isl_map_plain_is_single_valued(map);
12669 isl_map_free(map);
12671 return in;
12674 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12676 isl_bool sv;
12678 sv = isl_map_is_single_valued(map);
12679 if (sv < 0 || !sv)
12680 return sv;
12682 return isl_map_is_injective(map);
12685 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12687 return isl_map_is_single_valued(set_to_map(set));
12690 /* Does "map" only map elements to themselves?
12692 * If the domain and range spaces are different, then "map"
12693 * is considered not to be an identity relation, even if it is empty.
12694 * Otherwise, construct the maximal identity relation and
12695 * check whether "map" is a subset of this relation.
12697 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12699 isl_map *id;
12700 isl_bool equal, is_identity;
12702 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12703 if (equal < 0 || !equal)
12704 return equal;
12706 id = isl_map_identity(isl_map_get_space(map));
12707 is_identity = isl_map_is_subset(map, id);
12708 isl_map_free(id);
12710 return is_identity;
12713 int isl_map_is_translation(__isl_keep isl_map *map)
12715 int ok;
12716 isl_set *delta;
12718 delta = isl_map_deltas(isl_map_copy(map));
12719 ok = isl_set_is_singleton(delta);
12720 isl_set_free(delta);
12722 return ok;
12725 static int unique(isl_int *p, unsigned pos, unsigned len)
12727 if (isl_seq_first_non_zero(p, pos) != -1)
12728 return 0;
12729 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12730 return 0;
12731 return 1;
12734 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12736 int i, j;
12737 isl_size nvar, ovar, n_div;
12739 n_div = isl_basic_set_dim(bset, isl_dim_div);
12740 if (n_div < 0)
12741 return isl_bool_error;
12742 if (n_div != 0)
12743 return isl_bool_false;
12745 nvar = isl_basic_set_dim(bset, isl_dim_set);
12746 ovar = isl_space_offset(bset->dim, isl_dim_set);
12747 if (nvar < 0 || ovar < 0)
12748 return isl_bool_error;
12749 for (j = 0; j < nvar; ++j) {
12750 int lower = 0, upper = 0;
12751 for (i = 0; i < bset->n_eq; ++i) {
12752 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12753 continue;
12754 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12755 return isl_bool_false;
12756 break;
12758 if (i < bset->n_eq)
12759 continue;
12760 for (i = 0; i < bset->n_ineq; ++i) {
12761 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12762 continue;
12763 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12764 return isl_bool_false;
12765 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12766 lower = 1;
12767 else
12768 upper = 1;
12770 if (!lower || !upper)
12771 return isl_bool_false;
12774 return isl_bool_true;
12777 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12779 if (!set)
12780 return isl_bool_error;
12781 if (set->n != 1)
12782 return isl_bool_false;
12784 return isl_basic_set_is_box(set->p[0]);
12787 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12789 if (!bset)
12790 return isl_bool_error;
12792 return isl_space_is_wrapping(bset->dim);
12795 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12797 if (!set)
12798 return isl_bool_error;
12800 return isl_space_is_wrapping(set->dim);
12803 /* Modify the space of "map" through a call to "change".
12804 * If "can_change" is set (not NULL), then first call it to check
12805 * if the modification is allowed, printing the error message "cannot_change"
12806 * if it is not.
12808 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12809 isl_bool (*can_change)(__isl_keep isl_map *map),
12810 const char *cannot_change,
12811 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12813 isl_bool ok;
12814 isl_space *space;
12816 if (!map)
12817 return NULL;
12819 ok = can_change ? can_change(map) : isl_bool_true;
12820 if (ok < 0)
12821 return isl_map_free(map);
12822 if (!ok)
12823 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12824 return isl_map_free(map));
12826 space = change(isl_map_get_space(map));
12827 map = isl_map_reset_space(map, space);
12829 return map;
12832 /* Is the domain of "map" a wrapped relation?
12834 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12836 if (!map)
12837 return isl_bool_error;
12839 return isl_space_domain_is_wrapping(map->dim);
12842 /* Does "map" have a wrapped relation in both domain and range?
12844 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12846 return isl_space_is_product(isl_map_peek_space(map));
12849 /* Is the range of "map" a wrapped relation?
12851 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12853 if (!map)
12854 return isl_bool_error;
12856 return isl_space_range_is_wrapping(map->dim);
12859 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12861 isl_space *space;
12863 space = isl_basic_map_take_space(bmap);
12864 space = isl_space_wrap(space);
12865 bmap = isl_basic_map_restore_space(bmap, space);
12867 bmap = isl_basic_map_finalize(bmap);
12869 return bset_from_bmap(bmap);
12872 /* Given a map A -> B, return the set (A -> B).
12874 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12876 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12879 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12881 bset = isl_basic_set_cow(bset);
12882 if (!bset)
12883 return NULL;
12885 bset->dim = isl_space_unwrap(bset->dim);
12886 if (!bset->dim)
12887 goto error;
12889 bset = isl_basic_set_finalize(bset);
12891 return bset_to_bmap(bset);
12892 error:
12893 isl_basic_set_free(bset);
12894 return NULL;
12897 /* Given a set (A -> B), return the map A -> B.
12898 * Error out if "set" is not of the form (A -> B).
12900 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12902 return isl_map_change_space(set, &isl_set_is_wrapping,
12903 "not a wrapping set", &isl_space_unwrap);
12906 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12907 enum isl_dim_type type)
12909 isl_space *space;
12911 space = isl_basic_map_take_space(bmap);
12912 space = isl_space_reset(space, type);
12913 bmap = isl_basic_map_restore_space(bmap, space);
12915 bmap = isl_basic_map_mark_final(bmap);
12917 return bmap;
12920 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12921 enum isl_dim_type type)
12923 int i;
12924 isl_space *space;
12926 if (!map)
12927 return NULL;
12929 if (!isl_space_is_named_or_nested(map->dim, type))
12930 return map;
12932 map = isl_map_cow(map);
12933 if (!map)
12934 return NULL;
12936 for (i = 0; i < map->n; ++i) {
12937 map->p[i] = isl_basic_map_reset(map->p[i], type);
12938 if (!map->p[i])
12939 goto error;
12942 space = isl_map_take_space(map);
12943 space = isl_space_reset(space, type);
12944 map = isl_map_restore_space(map, space);
12946 return map;
12947 error:
12948 isl_map_free(map);
12949 return NULL;
12952 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12954 isl_space *space;
12956 space = isl_basic_map_take_space(bmap);
12957 space = isl_space_flatten(space);
12958 bmap = isl_basic_map_restore_space(bmap, space);
12960 bmap = isl_basic_map_mark_final(bmap);
12962 return bmap;
12965 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12967 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12970 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12971 __isl_take isl_basic_map *bmap)
12973 isl_space *space;
12975 space = isl_basic_map_take_space(bmap);
12976 space = isl_space_flatten_domain(space);
12977 bmap = isl_basic_map_restore_space(bmap, space);
12979 bmap = isl_basic_map_mark_final(bmap);
12981 return bmap;
12984 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12985 __isl_take isl_basic_map *bmap)
12987 isl_space *space;
12989 space = isl_basic_map_take_space(bmap);
12990 space = isl_space_flatten_range(space);
12991 bmap = isl_basic_map_restore_space(bmap, space);
12993 bmap = isl_basic_map_mark_final(bmap);
12995 return bmap;
12998 /* Remove any internal structure from the spaces of domain and range of "map".
13000 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
13002 if (!map)
13003 return NULL;
13005 if (!map->dim->nested[0] && !map->dim->nested[1])
13006 return map;
13008 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
13011 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
13013 return set_from_map(isl_map_flatten(set_to_map(set)));
13016 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
13018 isl_space *space, *flat_space;
13019 isl_map *map;
13021 space = isl_set_get_space(set);
13022 flat_space = isl_space_flatten(isl_space_copy(space));
13023 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
13024 flat_space));
13025 map = isl_map_intersect_domain(map, set);
13027 return map;
13030 /* Remove any internal structure from the space of the domain of "map".
13032 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
13034 if (!map)
13035 return NULL;
13037 if (!map->dim->nested[0])
13038 return map;
13040 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
13043 /* Remove any internal structure from the space of the range of "map".
13045 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
13047 if (!map)
13048 return NULL;
13050 if (!map->dim->nested[1])
13051 return map;
13053 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
13056 /* Reorder the dimensions of "bmap" according to the given dim_map
13057 * and set the dimension specification to "space" and
13058 * perform Gaussian elimination on the result.
13060 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
13061 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
13063 isl_basic_map *res;
13064 unsigned flags;
13065 isl_size n_div;
13067 n_div = isl_basic_map_dim(bmap, isl_dim_div);
13068 if (n_div < 0 || !space || !dim_map)
13069 goto error;
13071 flags = bmap->flags;
13072 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
13073 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
13074 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
13075 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
13076 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
13077 if (res)
13078 res->flags = flags;
13079 res = isl_basic_map_gauss(res, NULL);
13080 res = isl_basic_map_finalize(res);
13081 return res;
13082 error:
13083 isl_dim_map_free(dim_map);
13084 isl_basic_map_free(bmap);
13085 isl_space_free(space);
13086 return NULL;
13089 /* Reorder the dimensions of "map" according to given reordering.
13091 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
13092 __isl_take isl_reordering *r)
13094 int i;
13095 struct isl_dim_map *dim_map;
13097 map = isl_map_cow(map);
13098 dim_map = isl_dim_map_from_reordering(r);
13099 if (!map || !r || !dim_map)
13100 goto error;
13102 for (i = 0; i < map->n; ++i) {
13103 struct isl_dim_map *dim_map_i;
13104 isl_space *space;
13106 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
13108 space = isl_reordering_get_space(r);
13109 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
13111 if (!map->p[i])
13112 goto error;
13115 map = isl_map_reset_space(map, isl_reordering_get_space(r));
13116 map = isl_map_unmark_normalized(map);
13118 isl_reordering_free(r);
13119 isl_dim_map_free(dim_map);
13120 return map;
13121 error:
13122 isl_dim_map_free(dim_map);
13123 isl_map_free(map);
13124 isl_reordering_free(r);
13125 return NULL;
13128 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
13129 __isl_take isl_reordering *r)
13131 return set_from_map(isl_map_realign(set_to_map(set), r));
13134 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
13135 __isl_take isl_space *model)
13137 isl_ctx *ctx;
13138 isl_bool aligned;
13140 if (!map || !model)
13141 goto error;
13143 ctx = isl_space_get_ctx(model);
13144 if (!isl_space_has_named_params(model))
13145 isl_die(ctx, isl_error_invalid,
13146 "model has unnamed parameters", goto error);
13147 if (isl_map_check_named_params(map) < 0)
13148 goto error;
13149 aligned = isl_map_space_has_equal_params(map, model);
13150 if (aligned < 0)
13151 goto error;
13152 if (!aligned) {
13153 isl_space *space;
13154 isl_reordering *exp;
13156 space = isl_map_peek_space(map);
13157 exp = isl_parameter_alignment_reordering(space, model);
13158 map = isl_map_realign(map, exp);
13161 isl_space_free(model);
13162 return map;
13163 error:
13164 isl_space_free(model);
13165 isl_map_free(map);
13166 return NULL;
13169 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
13170 __isl_take isl_space *model)
13172 return isl_map_align_params(set, model);
13175 /* Align the parameters of "bmap" to those of "model", introducing
13176 * additional parameters if needed.
13178 __isl_give isl_basic_map *isl_basic_map_align_params(
13179 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
13181 isl_ctx *ctx;
13182 isl_bool equal_params;
13183 isl_space *bmap_space;
13185 if (!bmap || !model)
13186 goto error;
13188 ctx = isl_space_get_ctx(model);
13189 if (!isl_space_has_named_params(model))
13190 isl_die(ctx, isl_error_invalid,
13191 "model has unnamed parameters", goto error);
13192 if (isl_basic_map_check_named_params(bmap) < 0)
13193 goto error;
13194 bmap_space = isl_basic_map_peek_space(bmap);
13195 equal_params = isl_space_has_equal_params(bmap_space, model);
13196 if (equal_params < 0)
13197 goto error;
13198 if (!equal_params) {
13199 isl_reordering *exp;
13200 struct isl_dim_map *dim_map;
13202 exp = isl_parameter_alignment_reordering(bmap_space, model);
13203 dim_map = isl_dim_map_from_reordering(exp);
13204 bmap = isl_basic_map_realign(bmap,
13205 isl_reordering_get_space(exp),
13206 isl_dim_map_extend(dim_map, bmap));
13207 isl_reordering_free(exp);
13208 isl_dim_map_free(dim_map);
13211 isl_space_free(model);
13212 return bmap;
13213 error:
13214 isl_space_free(model);
13215 isl_basic_map_free(bmap);
13216 return NULL;
13219 /* Do "bset" and "space" have the same parameters?
13221 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
13222 __isl_keep isl_space *space)
13224 isl_space *bset_space;
13226 bset_space = isl_basic_set_peek_space(bset);
13227 return isl_space_has_equal_params(bset_space, space);
13230 /* Do "map" and "space" have the same parameters?
13232 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
13233 __isl_keep isl_space *space)
13235 isl_space *map_space;
13237 map_space = isl_map_peek_space(map);
13238 return isl_space_has_equal_params(map_space, space);
13241 /* Do "set" and "space" have the same parameters?
13243 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
13244 __isl_keep isl_space *space)
13246 return isl_map_space_has_equal_params(set_to_map(set), space);
13249 /* Align the parameters of "bset" to those of "model", introducing
13250 * additional parameters if needed.
13252 __isl_give isl_basic_set *isl_basic_set_align_params(
13253 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
13255 return isl_basic_map_align_params(bset, model);
13258 #undef TYPE
13259 #define TYPE isl_map
13260 #define isl_map_drop_dims isl_map_drop
13261 #include "isl_drop_unused_params_templ.c"
13263 /* Drop all parameters not referenced by "set".
13265 __isl_give isl_set *isl_set_drop_unused_params(
13266 __isl_take isl_set *set)
13268 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
13271 #undef TYPE
13272 #define TYPE isl_basic_map
13273 #define isl_basic_map_drop_dims isl_basic_map_drop
13274 #include "isl_drop_unused_params_templ.c"
13276 /* Drop all parameters not referenced by "bset".
13278 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
13279 __isl_take isl_basic_set *bset)
13281 return bset_from_bmap(isl_basic_map_drop_unused_params(
13282 bset_to_bmap(bset)));
13285 /* Given a tuple of identifiers "tuple" in a space that corresponds
13286 * to that of "set", if any of those identifiers appear as parameters
13287 * in "set", then equate those parameters with the corresponding
13288 * set dimensions and project out the parameters.
13289 * The result therefore has no such parameters.
13291 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
13292 __isl_keep isl_multi_id *tuple)
13294 int i;
13295 isl_size n;
13296 isl_space *set_space, *tuple_space;
13298 set_space = isl_set_peek_space(set);
13299 tuple_space = isl_multi_id_peek_space(tuple);
13300 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
13301 return isl_set_free(set);
13302 n = isl_multi_id_size(tuple);
13303 if (n < 0)
13304 return isl_set_free(set);
13305 for (i = 0; i < n; ++i) {
13306 isl_id *id;
13307 int pos;
13309 id = isl_multi_id_get_at(tuple, i);
13310 if (!id)
13311 return isl_set_free(set);
13312 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
13313 isl_id_free(id);
13314 if (pos < 0)
13315 continue;
13316 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
13317 set = isl_set_project_out(set, isl_dim_param, pos, 1);
13319 return set;
13322 /* Bind the set dimensions of "set" to parameters with identifiers
13323 * specified by "tuple", living in the same space as "set".
13325 * If no parameters with these identifiers appear in "set" already,
13326 * then the set dimensions are simply reinterpreted as parameters.
13327 * Otherwise, the parameters are first equated to the corresponding
13328 * set dimensions.
13330 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
13331 __isl_take isl_multi_id *tuple)
13333 isl_space *space;
13335 set = equate_params(set, tuple);
13336 space = isl_set_get_space(set);
13337 space = isl_space_bind_set(space, tuple);
13338 isl_multi_id_free(tuple);
13339 set = isl_set_reset_space(set, space);
13341 return set;
13344 /* Given a tuple of identifiers "tuple" in a space that corresponds
13345 * to the domain of "map", if any of those identifiers appear as parameters
13346 * in "map", then equate those parameters with the corresponding
13347 * input dimensions and project out the parameters.
13348 * The result therefore has no such parameters.
13350 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
13351 __isl_keep isl_multi_id *tuple)
13353 int i;
13354 isl_size n;
13355 isl_space *map_space, *tuple_space;
13357 map_space = isl_map_peek_space(map);
13358 tuple_space = isl_multi_id_peek_space(tuple);
13359 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
13360 return isl_map_free(map);
13361 n = isl_multi_id_size(tuple);
13362 if (n < 0)
13363 return isl_map_free(map);
13364 for (i = 0; i < n; ++i) {
13365 isl_id *id;
13366 int pos;
13368 id = isl_multi_id_get_at(tuple, i);
13369 if (!id)
13370 return isl_map_free(map);
13371 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
13372 isl_id_free(id);
13373 if (pos < 0)
13374 continue;
13375 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
13376 map = isl_map_project_out(map, isl_dim_param, pos, 1);
13378 return map;
13381 /* Bind the input dimensions of "map" to parameters with identifiers
13382 * specified by "tuple", living in the domain space of "map".
13384 * If no parameters with these identifiers appear in "map" already,
13385 * then the input dimensions are simply reinterpreted as parameters.
13386 * Otherwise, the parameters are first equated to the corresponding
13387 * input dimensions.
13389 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
13390 __isl_take isl_multi_id *tuple)
13392 isl_space *space;
13393 isl_set *set;
13395 map = map_equate_params(map, tuple);
13396 space = isl_map_get_space(map);
13397 space = isl_space_bind_map_domain(space, tuple);
13398 isl_multi_id_free(tuple);
13399 set = set_from_map(isl_map_reset_space(map, space));
13401 return set;
13404 /* Bind the output dimensions of "map" to parameters with identifiers
13405 * specified by "tuple", living in the range space of "map".
13407 * Since binding is more easily implemented on the domain,
13408 * bind the input dimensions of the inverse of "map".
13410 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
13411 __isl_take isl_multi_id *tuple)
13413 return isl_map_bind_domain(isl_map_reverse(map), tuple);
13416 /* Insert a domain corresponding to "tuple"
13417 * into the nullary or unary relation "set".
13418 * The result has an extra initial tuple and is therefore
13419 * either a unary or binary relation.
13420 * Any parameters with identifiers in "tuple" are reinterpreted
13421 * as the corresponding domain dimensions.
13423 static __isl_give isl_map *unbind_params_insert_domain(
13424 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
13426 isl_space *space;
13427 isl_reordering *r;
13429 space = isl_set_peek_space(set);
13430 r = isl_reordering_unbind_params_insert_domain(space, tuple);
13431 isl_multi_id_free(tuple);
13433 return isl_map_realign(set_to_map(set), r);
13436 /* Construct a set with "tuple" as domain from the parameter domain "set".
13437 * Any parameters with identifiers in "tuple" are reinterpreted
13438 * as the corresponding set dimensions.
13440 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
13441 __isl_take isl_multi_id *tuple)
13443 isl_bool is_params;
13445 is_params = isl_set_is_params(set);
13446 if (is_params < 0)
13447 set = isl_set_free(set);
13448 else if (!is_params)
13449 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13450 "expecting parameter domain", set = isl_set_free(set));
13451 return set_from_map(unbind_params_insert_domain(set, tuple));
13454 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13456 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
13458 isl_bool is_params;
13460 is_params = isl_set_is_params(set);
13461 if (is_params < 0)
13462 return isl_stat_error;
13463 else if (is_params)
13464 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13465 "expecting proper set", return isl_stat_error);
13467 return isl_stat_ok;
13470 /* Construct a map with "domain" as domain and "set" as range.
13471 * Any parameters with identifiers in "domain" are reinterpreted
13472 * as the corresponding domain dimensions.
13474 __isl_give isl_map *isl_set_unbind_params_insert_domain(
13475 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
13477 if (isl_set_check_is_set(set) < 0)
13478 set = isl_set_free(set);
13479 return unbind_params_insert_domain(set, domain);
13482 /* Construct a map with "domain" as domain and "set" as range.
13484 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
13485 __isl_take isl_space *domain)
13487 isl_size dim;
13488 isl_space *space;
13489 isl_map *map;
13491 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
13492 domain = isl_space_free(domain);
13493 dim = isl_space_dim(domain, isl_dim_set);
13494 if (dim < 0)
13495 domain = isl_space_free(domain);
13496 space = isl_set_get_space(set);
13497 domain = isl_space_replace_params(domain, space);
13498 space = isl_space_map_from_domain_and_range(domain, space);
13500 map = isl_map_from_range(set);
13501 map = isl_map_add_dims(map, isl_dim_in, dim);
13502 map = isl_map_reset_space(map, space);
13504 return map;
13507 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13508 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13509 enum isl_dim_type c2, enum isl_dim_type c3,
13510 enum isl_dim_type c4, enum isl_dim_type c5)
13512 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13513 struct isl_mat *mat;
13514 int i, j, k;
13515 int pos;
13516 isl_size total;
13518 total = isl_basic_map_dim(bmap, isl_dim_all);
13519 if (total < 0)
13520 return NULL;
13521 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13522 if (!mat)
13523 return NULL;
13524 for (i = 0; i < bmap->n_eq; ++i)
13525 for (j = 0, pos = 0; j < 5; ++j) {
13526 int off = isl_basic_map_offset(bmap, c[j]);
13527 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13528 if (dim < 0)
13529 return isl_mat_free(mat);
13530 for (k = 0; k < dim; ++k) {
13531 isl_int_set(mat->row[i][pos],
13532 bmap->eq[i][off + k]);
13533 ++pos;
13537 return mat;
13540 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13541 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13542 enum isl_dim_type c2, enum isl_dim_type c3,
13543 enum isl_dim_type c4, enum isl_dim_type c5)
13545 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13546 struct isl_mat *mat;
13547 int i, j, k;
13548 int pos;
13549 isl_size total;
13551 total = isl_basic_map_dim(bmap, isl_dim_all);
13552 if (total < 0)
13553 return NULL;
13554 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13555 if (!mat)
13556 return NULL;
13557 for (i = 0; i < bmap->n_ineq; ++i)
13558 for (j = 0, pos = 0; j < 5; ++j) {
13559 int off = isl_basic_map_offset(bmap, c[j]);
13560 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13561 if (dim < 0)
13562 return isl_mat_free(mat);
13563 for (k = 0; k < dim; ++k) {
13564 isl_int_set(mat->row[i][pos],
13565 bmap->ineq[i][off + k]);
13566 ++pos;
13570 return mat;
13573 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13574 __isl_take isl_space *space,
13575 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13576 enum isl_dim_type c2, enum isl_dim_type c3,
13577 enum isl_dim_type c4, enum isl_dim_type c5)
13579 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13580 isl_basic_map *bmap = NULL;
13581 isl_size dim;
13582 unsigned total;
13583 unsigned extra;
13584 int i, j, k, l;
13585 int pos;
13587 dim = isl_space_dim(space, isl_dim_all);
13588 if (dim < 0 || !eq || !ineq)
13589 goto error;
13591 if (eq->n_col != ineq->n_col)
13592 isl_die(space->ctx, isl_error_invalid,
13593 "equalities and inequalities matrices should have "
13594 "same number of columns", goto error);
13596 total = 1 + dim;
13598 if (eq->n_col < total)
13599 isl_die(space->ctx, isl_error_invalid,
13600 "number of columns too small", goto error);
13602 extra = eq->n_col - total;
13604 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13605 eq->n_row, ineq->n_row);
13606 if (!bmap)
13607 goto error;
13608 for (i = 0; i < extra; ++i) {
13609 k = isl_basic_map_alloc_div(bmap);
13610 if (k < 0)
13611 goto error;
13612 isl_int_set_si(bmap->div[k][0], 0);
13614 for (i = 0; i < eq->n_row; ++i) {
13615 l = isl_basic_map_alloc_equality(bmap);
13616 if (l < 0)
13617 goto error;
13618 for (j = 0, pos = 0; j < 5; ++j) {
13619 int off = isl_basic_map_offset(bmap, c[j]);
13620 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13621 if (dim < 0)
13622 goto error;
13623 for (k = 0; k < dim; ++k) {
13624 isl_int_set(bmap->eq[l][off + k],
13625 eq->row[i][pos]);
13626 ++pos;
13630 for (i = 0; i < ineq->n_row; ++i) {
13631 l = isl_basic_map_alloc_inequality(bmap);
13632 if (l < 0)
13633 goto error;
13634 for (j = 0, pos = 0; j < 5; ++j) {
13635 int off = isl_basic_map_offset(bmap, c[j]);
13636 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13637 if (dim < 0)
13638 goto error;
13639 for (k = 0; k < dim; ++k) {
13640 isl_int_set(bmap->ineq[l][off + k],
13641 ineq->row[i][pos]);
13642 ++pos;
13647 isl_space_free(space);
13648 isl_mat_free(eq);
13649 isl_mat_free(ineq);
13651 bmap = isl_basic_map_simplify(bmap);
13652 return isl_basic_map_finalize(bmap);
13653 error:
13654 isl_space_free(space);
13655 isl_mat_free(eq);
13656 isl_mat_free(ineq);
13657 isl_basic_map_free(bmap);
13658 return NULL;
13661 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13662 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13663 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13665 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13666 c1, c2, c3, c4, isl_dim_in);
13669 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13670 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13671 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13673 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13674 c1, c2, c3, c4, isl_dim_in);
13677 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13678 __isl_take isl_space *space,
13679 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13680 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13682 isl_basic_map *bmap;
13683 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13684 c1, c2, c3, c4, isl_dim_in);
13685 return bset_from_bmap(bmap);
13688 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13690 if (!bmap)
13691 return isl_bool_error;
13693 return isl_space_can_zip(bmap->dim);
13696 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13698 if (!map)
13699 return isl_bool_error;
13701 return isl_space_can_zip(map->dim);
13704 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13705 * (A -> C) -> (B -> D).
13707 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13709 unsigned pos;
13710 isl_size n_in;
13711 isl_size n1;
13712 isl_size n2;
13714 if (!bmap)
13715 return NULL;
13717 if (!isl_basic_map_can_zip(bmap))
13718 isl_die(bmap->ctx, isl_error_invalid,
13719 "basic map cannot be zipped", goto error);
13720 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13721 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13722 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13723 if (n_in < 0 || n1 < 0 || n2 < 0)
13724 return isl_basic_map_free(bmap);
13725 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13726 bmap = isl_basic_map_cow(bmap);
13727 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13728 if (!bmap)
13729 return NULL;
13730 bmap->dim = isl_space_zip(bmap->dim);
13731 if (!bmap->dim)
13732 goto error;
13733 bmap = isl_basic_map_mark_final(bmap);
13734 return bmap;
13735 error:
13736 isl_basic_map_free(bmap);
13737 return NULL;
13740 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13741 * (A -> C) -> (B -> D).
13743 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13745 if (!map)
13746 return NULL;
13748 if (!isl_map_can_zip(map))
13749 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13750 goto error);
13752 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13753 error:
13754 isl_map_free(map);
13755 return NULL;
13758 /* Can we apply isl_basic_map_curry to "bmap"?
13759 * That is, does it have a nested relation in its domain?
13761 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13763 if (!bmap)
13764 return isl_bool_error;
13766 return isl_space_can_curry(bmap->dim);
13769 /* Can we apply isl_map_curry to "map"?
13770 * That is, does it have a nested relation in its domain?
13772 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13774 if (!map)
13775 return isl_bool_error;
13777 return isl_space_can_curry(map->dim);
13780 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13781 * A -> (B -> C).
13783 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13786 if (!bmap)
13787 return NULL;
13789 if (!isl_basic_map_can_curry(bmap))
13790 isl_die(bmap->ctx, isl_error_invalid,
13791 "basic map cannot be curried", goto error);
13792 bmap = isl_basic_map_cow(bmap);
13793 if (!bmap)
13794 return NULL;
13795 bmap->dim = isl_space_curry(bmap->dim);
13796 if (!bmap->dim)
13797 goto error;
13798 bmap = isl_basic_map_mark_final(bmap);
13799 return bmap;
13800 error:
13801 isl_basic_map_free(bmap);
13802 return NULL;
13805 /* Given a map (A -> B) -> C, return the corresponding map
13806 * A -> (B -> C).
13808 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13810 return isl_map_change_space(map, &isl_map_can_curry,
13811 "map cannot be curried", &isl_space_curry);
13814 /* Can isl_map_range_curry be applied to "map"?
13815 * That is, does it have a nested relation in its range,
13816 * the domain of which is itself a nested relation?
13818 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13820 if (!map)
13821 return isl_bool_error;
13823 return isl_space_can_range_curry(map->dim);
13826 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13827 * A -> (B -> (C -> D)).
13829 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13831 return isl_map_change_space(map, &isl_map_can_range_curry,
13832 "map range cannot be curried",
13833 &isl_space_range_curry);
13836 /* Can we apply isl_basic_map_uncurry to "bmap"?
13837 * That is, does it have a nested relation in its domain?
13839 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13841 if (!bmap)
13842 return isl_bool_error;
13844 return isl_space_can_uncurry(bmap->dim);
13847 /* Can we apply isl_map_uncurry to "map"?
13848 * That is, does it have a nested relation in its domain?
13850 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13852 if (!map)
13853 return isl_bool_error;
13855 return isl_space_can_uncurry(map->dim);
13858 /* Given a basic map A -> (B -> C), return the corresponding basic map
13859 * (A -> B) -> C.
13861 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13864 if (!bmap)
13865 return NULL;
13867 if (!isl_basic_map_can_uncurry(bmap))
13868 isl_die(bmap->ctx, isl_error_invalid,
13869 "basic map cannot be uncurried",
13870 return isl_basic_map_free(bmap));
13871 bmap = isl_basic_map_cow(bmap);
13872 if (!bmap)
13873 return NULL;
13874 bmap->dim = isl_space_uncurry(bmap->dim);
13875 if (!bmap->dim)
13876 return isl_basic_map_free(bmap);
13877 bmap = isl_basic_map_mark_final(bmap);
13878 return bmap;
13881 /* Given a map A -> (B -> C), return the corresponding map
13882 * (A -> B) -> C.
13884 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13886 return isl_map_change_space(map, &isl_map_can_uncurry,
13887 "map cannot be uncurried", &isl_space_uncurry);
13890 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13891 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13893 return isl_map_equate(set, type1, pos1, type2, pos2);
13896 /* Construct a basic map where the given dimensions are equal to each other.
13898 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13899 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13901 isl_basic_map *bmap = NULL;
13902 int i;
13903 isl_size total;
13905 total = isl_space_dim(space, isl_dim_all);
13906 if (total < 0 ||
13907 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13908 isl_space_check_range(space, type2, pos2, 1) < 0)
13909 goto error;
13911 if (type1 == type2 && pos1 == pos2)
13912 return isl_basic_map_universe(space);
13914 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13915 i = isl_basic_map_alloc_equality(bmap);
13916 if (i < 0)
13917 goto error;
13918 isl_seq_clr(bmap->eq[i], 1 + total);
13919 pos1 += isl_basic_map_offset(bmap, type1);
13920 pos2 += isl_basic_map_offset(bmap, type2);
13921 isl_int_set_si(bmap->eq[i][pos1], -1);
13922 isl_int_set_si(bmap->eq[i][pos2], 1);
13923 bmap = isl_basic_map_finalize(bmap);
13924 isl_space_free(space);
13925 return bmap;
13926 error:
13927 isl_space_free(space);
13928 isl_basic_map_free(bmap);
13929 return NULL;
13932 /* Add a constraint imposing that the given two dimensions are equal.
13934 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13935 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13937 isl_basic_map *eq;
13939 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13941 bmap = isl_basic_map_intersect(bmap, eq);
13943 return bmap;
13946 /* Add a constraint imposing that the given two dimensions are equal.
13948 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13949 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13951 isl_basic_map *bmap;
13953 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13955 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13957 return map;
13960 /* Add a constraint imposing that the given two dimensions have opposite values.
13962 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13963 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13965 isl_basic_map *bmap = NULL;
13966 int i;
13967 isl_size total;
13969 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13970 return isl_map_free(map);
13971 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13972 return isl_map_free(map);
13974 total = isl_map_dim(map, isl_dim_all);
13975 if (total < 0)
13976 return isl_map_free(map);
13977 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13978 i = isl_basic_map_alloc_equality(bmap);
13979 if (i < 0)
13980 goto error;
13981 isl_seq_clr(bmap->eq[i], 1 + total);
13982 pos1 += isl_basic_map_offset(bmap, type1);
13983 pos2 += isl_basic_map_offset(bmap, type2);
13984 isl_int_set_si(bmap->eq[i][pos1], 1);
13985 isl_int_set_si(bmap->eq[i][pos2], 1);
13986 bmap = isl_basic_map_finalize(bmap);
13988 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13990 return map;
13991 error:
13992 isl_basic_map_free(bmap);
13993 isl_map_free(map);
13994 return NULL;
13997 /* Construct a constraint imposing that the value of the first dimension is
13998 * greater than or equal to that of the second.
14000 static __isl_give isl_constraint *constraint_order_ge(
14001 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
14002 enum isl_dim_type type2, int pos2)
14004 isl_constraint *c;
14006 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14007 isl_space_check_range(space, type2, pos2, 1) < 0)
14008 space = isl_space_free(space);
14009 if (!space)
14010 return NULL;
14012 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
14014 if (type1 == type2 && pos1 == pos2)
14015 return c;
14017 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
14018 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
14020 return c;
14023 /* Add a constraint imposing that the value of the first dimension is
14024 * greater than or equal to that of the second.
14026 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
14027 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14029 isl_constraint *c;
14030 isl_space *space;
14032 if (type1 == type2 && pos1 == pos2)
14033 return bmap;
14034 space = isl_basic_map_get_space(bmap);
14035 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14036 bmap = isl_basic_map_add_constraint(bmap, c);
14038 return bmap;
14041 /* Add a constraint imposing that the value of the first dimension is
14042 * greater than or equal to that of the second.
14044 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
14045 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14047 isl_constraint *c;
14048 isl_space *space;
14050 if (type1 == type2 && pos1 == pos2)
14051 return map;
14052 space = isl_map_get_space(map);
14053 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14054 map = isl_map_add_constraint(map, c);
14056 return map;
14059 /* Add a constraint imposing that the value of the first dimension is
14060 * less than or equal to that of the second.
14062 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
14063 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14065 return isl_map_order_ge(map, type2, pos2, type1, pos1);
14068 /* Construct a basic map where the value of the first dimension is
14069 * greater than that of the second.
14071 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
14072 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14074 isl_basic_map *bmap = NULL;
14075 int i;
14076 isl_size total;
14078 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14079 isl_space_check_range(space, type2, pos2, 1) < 0)
14080 goto error;
14082 if (type1 == type2 && pos1 == pos2)
14083 return isl_basic_map_empty(space);
14085 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
14086 total = isl_basic_map_dim(bmap, isl_dim_all);
14087 i = isl_basic_map_alloc_inequality(bmap);
14088 if (total < 0 || i < 0)
14089 return isl_basic_map_free(bmap);
14090 isl_seq_clr(bmap->ineq[i], 1 + total);
14091 pos1 += isl_basic_map_offset(bmap, type1);
14092 pos2 += isl_basic_map_offset(bmap, type2);
14093 isl_int_set_si(bmap->ineq[i][pos1], 1);
14094 isl_int_set_si(bmap->ineq[i][pos2], -1);
14095 isl_int_set_si(bmap->ineq[i][0], -1);
14096 bmap = isl_basic_map_finalize(bmap);
14098 return bmap;
14099 error:
14100 isl_space_free(space);
14101 isl_basic_map_free(bmap);
14102 return NULL;
14105 /* Add a constraint imposing that the value of the first dimension is
14106 * greater than that of the second.
14108 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
14109 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14111 isl_basic_map *gt;
14113 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
14115 bmap = isl_basic_map_intersect(bmap, gt);
14117 return bmap;
14120 /* Add a constraint imposing that the value of the first dimension is
14121 * greater than that of the second.
14123 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
14124 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14126 isl_basic_map *bmap;
14128 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
14130 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
14132 return map;
14135 /* Add a constraint imposing that the value of the first dimension is
14136 * smaller than that of the second.
14138 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
14139 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14141 return isl_map_order_gt(map, type2, pos2, type1, pos1);
14144 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
14145 int pos)
14147 isl_aff *div;
14148 isl_local_space *ls;
14150 if (!bmap)
14151 return NULL;
14153 if (!isl_basic_map_divs_known(bmap))
14154 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14155 "some divs are unknown", return NULL);
14157 ls = isl_basic_map_get_local_space(bmap);
14158 div = isl_local_space_get_div(ls, pos);
14159 isl_local_space_free(ls);
14161 return div;
14164 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
14165 int pos)
14167 return isl_basic_map_get_div(bset, pos);
14170 /* Plug in "subs" for set dimension "pos" of "set".
14172 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
14173 unsigned pos, __isl_keep isl_aff *subs)
14175 isl_multi_aff *ma;
14177 if (set && isl_set_plain_is_empty(set))
14178 return set;
14180 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
14181 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
14182 return isl_set_preimage_multi_aff(set, ma);
14185 /* Check if the range of "ma" is compatible with the domain or range
14186 * (depending on "type") of "bmap".
14188 static isl_stat check_basic_map_compatible_range_multi_aff(
14189 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
14190 __isl_keep isl_multi_aff *ma)
14192 isl_bool m;
14193 isl_space *ma_space;
14195 ma_space = isl_multi_aff_get_space(ma);
14197 m = isl_space_has_equal_params(bmap->dim, ma_space);
14198 if (m < 0)
14199 goto error;
14200 if (!m)
14201 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14202 "parameters don't match", goto error);
14203 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
14204 if (m < 0)
14205 goto error;
14206 if (!m)
14207 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14208 "spaces don't match", goto error);
14210 isl_space_free(ma_space);
14211 return isl_stat_ok;
14212 error:
14213 isl_space_free(ma_space);
14214 return isl_stat_error;
14217 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14218 * coefficients before the transformed range of dimensions,
14219 * the "n_after" coefficients after the transformed range of dimensions
14220 * and the coefficients of the other divs in "bmap".
14222 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
14223 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
14225 int i;
14226 isl_size n_param;
14227 isl_size n_set;
14228 isl_local_space *ls;
14230 if (n_div == 0)
14231 return bmap;
14233 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
14234 n_param = isl_local_space_dim(ls, isl_dim_param);
14235 n_set = isl_local_space_dim(ls, isl_dim_set);
14236 if (n_param < 0 || n_set < 0)
14237 return isl_basic_map_free(bmap);
14239 for (i = 0; i < n_div; ++i) {
14240 int o_bmap = 0, o_ls = 0;
14242 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
14243 o_bmap += 1 + 1 + n_param;
14244 o_ls += 1 + 1 + n_param;
14245 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
14246 o_bmap += n_before;
14247 isl_seq_cpy(bmap->div[i] + o_bmap,
14248 ls->div->row[i] + o_ls, n_set);
14249 o_bmap += n_set;
14250 o_ls += n_set;
14251 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
14252 o_bmap += n_after;
14253 isl_seq_cpy(bmap->div[i] + o_bmap,
14254 ls->div->row[i] + o_ls, n_div);
14255 o_bmap += n_div;
14256 o_ls += n_div;
14257 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
14258 bmap = isl_basic_map_add_div_constraints(bmap, i);
14259 if (!bmap)
14260 goto error;
14263 isl_local_space_free(ls);
14264 return bmap;
14265 error:
14266 isl_local_space_free(ls);
14267 return isl_basic_map_free(bmap);
14270 /* How many stride constraints does "ma" enforce?
14271 * That is, how many of the affine expressions have a denominator
14272 * different from one?
14274 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
14276 int i;
14277 int strides = 0;
14279 for (i = 0; i < ma->n; ++i)
14280 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
14281 strides++;
14283 return strides;
14286 /* For each affine expression in ma of the form
14288 * x_i = (f_i y + h_i)/m_i
14290 * with m_i different from one, add a constraint to "bmap"
14291 * of the form
14293 * f_i y + h_i = m_i alpha_i
14295 * with alpha_i an additional existentially quantified variable.
14297 * The input variables of "ma" correspond to a subset of the variables
14298 * of "bmap". There are "n_before" variables in "bmap" before this
14299 * subset and "n_after" variables after this subset.
14300 * The integer divisions of the affine expressions in "ma" are assumed
14301 * to have been aligned. There are "n_div_ma" of them and
14302 * they appear first in "bmap", straight after the "n_after" variables.
14304 static __isl_give isl_basic_map *add_ma_strides(
14305 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
14306 int n_before, int n_after, int n_div_ma)
14308 int i, k;
14309 int div;
14310 isl_size total;
14311 isl_size n_param;
14312 isl_size n_in;
14314 total = isl_basic_map_dim(bmap, isl_dim_all);
14315 n_param = isl_multi_aff_dim(ma, isl_dim_param);
14316 n_in = isl_multi_aff_dim(ma, isl_dim_in);
14317 if (total < 0 || n_param < 0 || n_in < 0)
14318 return isl_basic_map_free(bmap);
14319 for (i = 0; i < ma->n; ++i) {
14320 int o_bmap = 0, o_ma = 1;
14322 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
14323 continue;
14324 div = isl_basic_map_alloc_div(bmap);
14325 k = isl_basic_map_alloc_equality(bmap);
14326 if (div < 0 || k < 0)
14327 goto error;
14328 isl_int_set_si(bmap->div[div][0], 0);
14329 isl_seq_cpy(bmap->eq[k] + o_bmap,
14330 ma->u.p[i]->v->el + o_ma, 1 + n_param);
14331 o_bmap += 1 + n_param;
14332 o_ma += 1 + n_param;
14333 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
14334 o_bmap += n_before;
14335 isl_seq_cpy(bmap->eq[k] + o_bmap,
14336 ma->u.p[i]->v->el + o_ma, n_in);
14337 o_bmap += n_in;
14338 o_ma += n_in;
14339 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
14340 o_bmap += n_after;
14341 isl_seq_cpy(bmap->eq[k] + o_bmap,
14342 ma->u.p[i]->v->el + o_ma, n_div_ma);
14343 o_bmap += n_div_ma;
14344 o_ma += n_div_ma;
14345 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
14346 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
14347 total++;
14350 return bmap;
14351 error:
14352 isl_basic_map_free(bmap);
14353 return NULL;
14356 /* Replace the domain or range space (depending on "type) of "space" by "set".
14358 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
14359 enum isl_dim_type type, __isl_take isl_space *set)
14361 if (type == isl_dim_in) {
14362 space = isl_space_range(space);
14363 space = isl_space_map_from_domain_and_range(set, space);
14364 } else {
14365 space = isl_space_domain(space);
14366 space = isl_space_map_from_domain_and_range(space, set);
14369 return space;
14372 /* Compute the preimage of the domain or range (depending on "type")
14373 * of "bmap" under the function represented by "ma".
14374 * In other words, plug in "ma" in the domain or range of "bmap".
14375 * The result is a basic map that lives in the same space as "bmap"
14376 * except that the domain or range has been replaced by
14377 * the domain space of "ma".
14379 * If bmap is represented by
14381 * A(p) + S u + B x + T v + C(divs) >= 0,
14383 * where u and x are input and output dimensions if type == isl_dim_out
14384 * while x and v are input and output dimensions if type == isl_dim_in,
14385 * and ma is represented by
14387 * x = D(p) + F(y) + G(divs')
14389 * then the result is
14391 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14393 * The divs in the input set are similarly adjusted.
14394 * In particular
14396 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14398 * becomes
14400 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14401 * B_i G(divs') + c_i(divs))/n_i)
14403 * If bmap is not a rational map and if F(y) involves any denominators
14405 * x_i = (f_i y + h_i)/m_i
14407 * then additional constraints are added to ensure that we only
14408 * map back integer points. That is we enforce
14410 * f_i y + h_i = m_i alpha_i
14412 * with alpha_i an additional existentially quantified variable.
14414 * We first copy over the divs from "ma".
14415 * Then we add the modified constraints and divs from "bmap".
14416 * Finally, we add the stride constraints, if needed.
14418 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
14419 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
14420 __isl_take isl_multi_aff *ma)
14422 int i, k;
14423 isl_space *space;
14424 isl_basic_map *res = NULL;
14425 isl_size n_before, n_after, n_div_bmap, n_div_ma;
14426 isl_int f, c1, c2, g;
14427 isl_bool rational;
14428 int strides;
14430 isl_int_init(f);
14431 isl_int_init(c1);
14432 isl_int_init(c2);
14433 isl_int_init(g);
14435 ma = isl_multi_aff_align_divs(ma);
14436 if (!bmap || !ma)
14437 goto error;
14438 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
14439 goto error;
14441 if (type == isl_dim_in) {
14442 n_before = 0;
14443 n_after = isl_basic_map_dim(bmap, isl_dim_out);
14444 } else {
14445 n_before = isl_basic_map_dim(bmap, isl_dim_in);
14446 n_after = 0;
14448 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
14449 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
14450 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
14451 goto error;
14453 space = isl_multi_aff_get_domain_space(ma);
14454 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
14455 rational = isl_basic_map_is_rational(bmap);
14456 strides = rational ? 0 : multi_aff_strides(ma);
14457 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
14458 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
14459 if (rational)
14460 res = isl_basic_map_set_rational(res);
14462 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
14463 if (isl_basic_map_alloc_div(res) < 0)
14464 goto error;
14466 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
14467 if (!res)
14468 goto error;
14470 for (i = 0; i < bmap->n_eq; ++i) {
14471 k = isl_basic_map_alloc_equality(res);
14472 if (k < 0)
14473 goto error;
14474 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
14475 n_after, n_div_ma, n_div_bmap,
14476 f, c1, c2, g, 0) < 0)
14477 goto error;
14480 for (i = 0; i < bmap->n_ineq; ++i) {
14481 k = isl_basic_map_alloc_inequality(res);
14482 if (k < 0)
14483 goto error;
14484 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
14485 n_after, n_div_ma, n_div_bmap,
14486 f, c1, c2, g, 0) < 0)
14487 goto error;
14490 for (i = 0; i < bmap->n_div; ++i) {
14491 if (isl_int_is_zero(bmap->div[i][0])) {
14492 isl_int_set_si(res->div[n_div_ma + i][0], 0);
14493 continue;
14495 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
14496 n_before, n_after, n_div_ma, n_div_bmap,
14497 f, c1, c2, g, 1) < 0)
14498 goto error;
14501 if (strides)
14502 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
14504 isl_int_clear(f);
14505 isl_int_clear(c1);
14506 isl_int_clear(c2);
14507 isl_int_clear(g);
14508 isl_basic_map_free(bmap);
14509 isl_multi_aff_free(ma);
14510 res = isl_basic_map_simplify(res);
14511 return isl_basic_map_finalize(res);
14512 error:
14513 isl_int_clear(f);
14514 isl_int_clear(c1);
14515 isl_int_clear(c2);
14516 isl_int_clear(g);
14517 isl_basic_map_free(bmap);
14518 isl_multi_aff_free(ma);
14519 isl_basic_map_free(res);
14520 return NULL;
14523 /* Compute the preimage of "bset" under the function represented by "ma".
14524 * In other words, plug in "ma" in "bset". The result is a basic set
14525 * that lives in the domain space of "ma".
14527 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14528 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14530 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14533 /* Compute the preimage of the domain of "bmap" under the function
14534 * represented by "ma".
14535 * In other words, plug in "ma" in the domain of "bmap".
14536 * The result is a basic map that lives in the same space as "bmap"
14537 * except that the domain has been replaced by the domain space of "ma".
14539 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14540 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14542 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14545 /* Compute the preimage of the range of "bmap" under the function
14546 * represented by "ma".
14547 * In other words, plug in "ma" in the range of "bmap".
14548 * The result is a basic map that lives in the same space as "bmap"
14549 * except that the range has been replaced by the domain space of "ma".
14551 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14552 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14554 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14557 /* Check if the range of "ma" is compatible with the domain or range
14558 * (depending on "type") of "map".
14559 * Return isl_stat_error if anything is wrong.
14561 static isl_stat check_map_compatible_range_multi_aff(
14562 __isl_keep isl_map *map, enum isl_dim_type type,
14563 __isl_keep isl_multi_aff *ma)
14565 isl_bool m;
14566 isl_space *ma_space;
14568 ma_space = isl_multi_aff_get_space(ma);
14569 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14570 isl_space_free(ma_space);
14571 if (m < 0)
14572 return isl_stat_error;
14573 if (!m)
14574 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14575 "spaces don't match", return isl_stat_error);
14576 return isl_stat_ok;
14579 /* Compute the preimage of the domain or range (depending on "type")
14580 * of "map" under the function represented by "ma".
14581 * In other words, plug in "ma" in the domain or range of "map".
14582 * The result is a map that lives in the same space as "map"
14583 * except that the domain or range has been replaced by
14584 * the domain space of "ma".
14586 * The parameters are assumed to have been aligned.
14588 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14589 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14591 int i;
14592 isl_space *space;
14594 map = isl_map_cow(map);
14595 ma = isl_multi_aff_align_divs(ma);
14596 if (!map || !ma)
14597 goto error;
14598 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14599 goto error;
14601 for (i = 0; i < map->n; ++i) {
14602 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14603 isl_multi_aff_copy(ma));
14604 if (!map->p[i])
14605 goto error;
14608 space = isl_multi_aff_get_domain_space(ma);
14609 space = isl_space_set(isl_map_get_space(map), type, space);
14611 isl_space_free(isl_map_take_space(map));
14612 map = isl_map_restore_space(map, space);
14613 if (!map)
14614 goto error;
14616 isl_multi_aff_free(ma);
14617 if (map->n > 1)
14618 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14619 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14620 return map;
14621 error:
14622 isl_multi_aff_free(ma);
14623 isl_map_free(map);
14624 return NULL;
14627 /* Compute the preimage of the domain or range (depending on "type")
14628 * of "map" under the function represented by "ma".
14629 * In other words, plug in "ma" in the domain or range of "map".
14630 * The result is a map that lives in the same space as "map"
14631 * except that the domain or range has been replaced by
14632 * the domain space of "ma".
14634 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14635 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14637 isl_bool aligned;
14639 if (!map || !ma)
14640 goto error;
14642 aligned = isl_map_space_has_equal_params(map, ma->space);
14643 if (aligned < 0)
14644 goto error;
14645 if (aligned)
14646 return map_preimage_multi_aff(map, type, ma);
14648 if (isl_map_check_named_params(map) < 0)
14649 goto error;
14650 if (!isl_space_has_named_params(ma->space))
14651 isl_die(map->ctx, isl_error_invalid,
14652 "unaligned unnamed parameters", goto error);
14653 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14654 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14656 return map_preimage_multi_aff(map, type, ma);
14657 error:
14658 isl_multi_aff_free(ma);
14659 return isl_map_free(map);
14662 /* Compute the preimage of "set" under the function represented by "ma".
14663 * In other words, plug in "ma" in "set". The result is a set
14664 * that lives in the domain space of "ma".
14666 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14667 __isl_take isl_multi_aff *ma)
14669 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14672 /* Compute the preimage of the domain of "map" under the function
14673 * represented by "ma".
14674 * In other words, plug in "ma" in the domain of "map".
14675 * The result is a map that lives in the same space as "map"
14676 * except that the domain has been replaced by the domain space of "ma".
14678 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14679 __isl_take isl_multi_aff *ma)
14681 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14684 /* Compute the preimage of the range of "map" under the function
14685 * represented by "ma".
14686 * In other words, plug in "ma" in the range of "map".
14687 * The result is a map that lives in the same space as "map"
14688 * except that the range has been replaced by the domain space of "ma".
14690 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14691 __isl_take isl_multi_aff *ma)
14693 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14696 /* Compute the preimage of "map" under the function represented by "pma".
14697 * In other words, plug in "pma" in the domain or range of "map".
14698 * The result is a map that lives in the same space as "map",
14699 * except that the space of type "type" has been replaced by
14700 * the domain space of "pma".
14702 * The parameters of "map" and "pma" are assumed to have been aligned.
14704 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14705 __isl_take isl_map *map, enum isl_dim_type type,
14706 __isl_take isl_pw_multi_aff *pma)
14708 int i;
14709 isl_map *res;
14711 if (!pma)
14712 goto error;
14714 if (pma->n == 0) {
14715 isl_space *space;
14717 space = isl_pw_multi_aff_get_domain_space(pma);
14718 isl_pw_multi_aff_free(pma);
14719 space = isl_space_set(isl_map_get_space(map), type, space);
14720 isl_map_free(map);
14721 return isl_map_empty(space);
14724 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14725 isl_multi_aff_copy(pma->p[0].maff));
14726 if (type == isl_dim_in)
14727 res = isl_map_intersect_domain(res,
14728 isl_map_copy(pma->p[0].set));
14729 else
14730 res = isl_map_intersect_range(res,
14731 isl_map_copy(pma->p[0].set));
14733 for (i = 1; i < pma->n; ++i) {
14734 isl_map *res_i;
14736 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14737 isl_multi_aff_copy(pma->p[i].maff));
14738 if (type == isl_dim_in)
14739 res_i = isl_map_intersect_domain(res_i,
14740 isl_map_copy(pma->p[i].set));
14741 else
14742 res_i = isl_map_intersect_range(res_i,
14743 isl_map_copy(pma->p[i].set));
14744 res = isl_map_union(res, res_i);
14747 isl_pw_multi_aff_free(pma);
14748 isl_map_free(map);
14749 return res;
14750 error:
14751 isl_pw_multi_aff_free(pma);
14752 isl_map_free(map);
14753 return NULL;
14756 /* Compute the preimage of "map" under the function represented by "pma".
14757 * In other words, plug in "pma" in the domain or range of "map".
14758 * The result is a map that lives in the same space as "map",
14759 * except that the space of type "type" has been replaced by
14760 * the domain space of "pma".
14762 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14763 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14765 isl_bool aligned;
14767 if (!map || !pma)
14768 goto error;
14770 aligned = isl_map_space_has_equal_params(map, pma->dim);
14771 if (aligned < 0)
14772 goto error;
14773 if (aligned)
14774 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14776 if (isl_map_check_named_params(map) < 0)
14777 goto error;
14778 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14779 goto error;
14780 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14781 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14783 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14784 error:
14785 isl_pw_multi_aff_free(pma);
14786 return isl_map_free(map);
14789 /* Compute the preimage of "set" under the function represented by "pma".
14790 * In other words, plug in "pma" in "set". The result is a set
14791 * that lives in the domain space of "pma".
14793 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14794 __isl_take isl_pw_multi_aff *pma)
14796 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14799 /* Compute the preimage of the domain of "map" under the function
14800 * represented by "pma".
14801 * In other words, plug in "pma" in the domain of "map".
14802 * The result is a map that lives in the same space as "map",
14803 * except that domain space has been replaced by the domain space of "pma".
14805 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14806 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14808 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14811 /* Compute the preimage of the range of "map" under the function
14812 * represented by "pma".
14813 * In other words, plug in "pma" in the range of "map".
14814 * The result is a map that lives in the same space as "map",
14815 * except that range space has been replaced by the domain space of "pma".
14817 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14818 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14820 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14823 /* Compute the preimage of "map" under the function represented by "mpa".
14824 * In other words, plug in "mpa" in the domain or range of "map".
14825 * The result is a map that lives in the same space as "map",
14826 * except that the space of type "type" has been replaced by
14827 * the domain space of "mpa".
14829 * If the map does not involve any constraints that refer to the
14830 * dimensions of the substituted space, then the only possible
14831 * effect of "mpa" on the map is to map the space to a different space.
14832 * We create a separate isl_multi_aff to effectuate this change
14833 * in order to avoid spurious splitting of the map along the pieces
14834 * of "mpa".
14835 * If "mpa" has a non-trivial explicit domain, however,
14836 * then the full substitution should be performed.
14838 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14839 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14841 isl_size n;
14842 isl_bool full;
14843 isl_pw_multi_aff *pma;
14845 n = isl_map_dim(map, type);
14846 if (n < 0 || !mpa)
14847 goto error;
14849 full = isl_map_involves_dims(map, type, 0, n);
14850 if (full >= 0 && !full)
14851 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14852 if (full < 0)
14853 goto error;
14854 if (!full) {
14855 isl_space *space;
14856 isl_multi_aff *ma;
14858 space = isl_multi_pw_aff_get_space(mpa);
14859 isl_multi_pw_aff_free(mpa);
14860 ma = isl_multi_aff_zero(space);
14861 return isl_map_preimage_multi_aff(map, type, ma);
14864 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14865 return isl_map_preimage_pw_multi_aff(map, type, pma);
14866 error:
14867 isl_map_free(map);
14868 isl_multi_pw_aff_free(mpa);
14869 return NULL;
14872 /* Compute the preimage of "map" under the function represented by "mpa".
14873 * In other words, plug in "mpa" in the domain "map".
14874 * The result is a map that lives in the same space as "map",
14875 * except that domain space has been replaced by the domain space of "mpa".
14877 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14878 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14880 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14883 /* Compute the preimage of "set" by the function represented by "mpa".
14884 * In other words, plug in "mpa" in "set".
14886 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14887 __isl_take isl_multi_pw_aff *mpa)
14889 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14892 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14893 * on the output dimension "pos" in terms of the parameters,
14894 * the input dimensions and possibly some integer divisions,
14895 * but not any other output dimensions, extract this upper bound
14896 * as a function of all dimensions (with zero coefficients
14897 * for the output dimensions).
14899 * That is, the inequality is of the form
14901 * e(...) + c - m x >= 0
14903 * where e does not depend on any other output dimensions.
14904 * Return (e(...) + c) / m, with the denominator m in the first position.
14906 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14907 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14909 isl_ctx *ctx;
14910 isl_size v_out, total;
14912 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14913 total = isl_basic_map_dim(bmap, isl_dim_all);
14914 if (v_out < 0 || total < 0)
14915 return NULL;
14916 ctx = isl_basic_map_get_ctx(bmap);
14917 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14918 total, v_out + pos);
14921 /* Is constraint "c" of "bmap" of the form
14923 * e(...) + c1 - m x >= 0
14925 * or
14927 * -e(...) + c2 + m x >= 0
14929 * where m > 1 and e does not involve any other output variables?
14931 * "v_out" is the offset to the output variables.
14932 * "d" is the position of x among the output variables.
14933 * "v_div" is the offset to the local variables.
14934 * "total" is the total number of variables.
14936 * Since the purpose of this function is to use the constraint
14937 * to express the output variable as an integer division,
14938 * do not allow the constraint to involve any local variables
14939 * that do not have an explicit representation.
14941 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14942 isl_int *c, int v_out, int d, int v_div, int total)
14944 int i = 0;
14946 if (isl_int_is_zero(c[1 + v_out + d]))
14947 return isl_bool_false;
14948 if (isl_int_is_one(c[1 + v_out + d]))
14949 return isl_bool_false;
14950 if (isl_int_is_negone(c[1 + v_out + d]))
14951 return isl_bool_false;
14952 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14953 return isl_bool_false;
14954 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14955 v_div - (v_out + d + 1)) != -1)
14956 return isl_bool_false;
14957 for (i = 0; v_div + i < total; ++i) {
14958 isl_bool known, involves;
14960 if (isl_int_is_zero(c[1 + v_div + i]))
14961 continue;
14962 known = isl_basic_map_div_is_known(bmap, i);
14963 if (known < 0 || !known)
14964 return known;
14965 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14966 if (involves < 0 || involves)
14967 return isl_bool_not(involves);
14969 return isl_bool_true;
14972 /* Look for a pair of constraints
14974 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14976 * and
14978 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14980 * that express that the output dimension x at position "pos"
14981 * is some integer division of an expression in terms of the parameters,
14982 * input dimensions and integer divisions.
14983 * If such a pair can be found, then return the index
14984 * of the upper bound constraint, m x <= e(...) + c1.
14985 * Otherwise, return an index beyond the number of constraints.
14987 * The constraints of "bmap" are assumed to have been sorted.
14989 * In order for the constraints above to express an integer division,
14990 * m needs to be greater than 1 and such that
14992 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14994 * In particular, this ensures that
14996 * x = floor((e(...) + c1) / m)
14998 isl_size isl_basic_map_find_output_upper_div_constraint(
14999 __isl_keep isl_basic_map *bmap, int pos)
15001 int i;
15002 isl_size j;
15003 isl_size n_ineq;
15004 isl_size v_out, v_div;
15005 isl_size total;
15006 isl_int sum;
15008 total = isl_basic_map_dim(bmap, isl_dim_all);
15009 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15010 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15011 n_ineq = isl_basic_map_n_inequality(bmap);
15012 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
15013 return isl_size_error;
15015 isl_int_init(sum);
15016 for (i = 0; i < n_ineq; ++i) {
15017 isl_bool potential;
15019 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
15020 v_out, pos, v_div, total);
15021 if (potential < 0)
15022 goto error;
15023 if (!potential)
15024 continue;
15026 j = find_later_constraint_in_pair(bmap, i, v_out + pos, total,
15027 bmap->ineq[i][1 + v_out + pos], &sum);
15028 if (j < 0)
15029 goto error;
15030 if (j < n_ineq)
15031 break;
15033 isl_int_clear(sum);
15035 if (i >= n_ineq)
15036 return n_ineq;
15037 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
15038 return i;
15039 else
15040 return j;
15041 error:
15042 isl_int_clear(sum);
15043 return isl_size_error;
15046 /* Look for a pair of constraints
15048 * -g(x) + i >= 0
15049 * g(x) - i + d >= 0
15051 * in "bmap" on the output dimension at position "pos" such that
15052 * g(x) is an expression in the parameters and input dimensions, and
15053 * d < "n".
15054 * Return the index of the first constraint if such a pair can be found.
15055 * Otherwise, return an index beyond the number of inequality constraints.
15057 * The constraints are assumed to have been sorted.
15059 static isl_size find_output_lower_mod_constraint(__isl_keep isl_basic_map *bmap,
15060 int pos, isl_int n)
15062 int i;
15063 isl_size n_ineq;
15064 isl_size v_out, n_out;
15065 isl_size v_div, n_div;
15066 isl_size j;
15067 isl_int tmp;
15069 n_ineq = isl_basic_map_n_inequality(bmap);
15070 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15071 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15072 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15073 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15074 if (n_ineq < 0 || v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15075 return isl_size_error;
15077 isl_int_init(tmp);
15078 for (i = n_ineq - 1; i >= 0; --i) {
15079 if (!isl_int_is_one(bmap->ineq[i][1 + v_out + pos]) &&
15080 !isl_int_is_negone(bmap->ineq[i][1 + v_out + pos]))
15081 continue;
15082 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_div,
15083 n_div) != -1)
15084 continue;
15085 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_out, pos) != -1)
15086 continue;
15087 if (isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_out + pos + 1,
15088 n_out - (pos + 1)) != -1)
15089 continue;
15090 j = find_earlier_constraint_in_pair(bmap, i, v_out + pos, v_div,
15091 n, &tmp);
15092 if (j >= n_ineq)
15093 continue;
15094 isl_int_clear(tmp);
15095 if (j < 0)
15096 return j;
15097 if (isl_int_is_one(bmap->ineq[i][1 + v_out + pos]))
15098 return i;
15099 return j;
15102 isl_int_clear(tmp);
15103 return n_ineq;
15106 /* Given that equality "eq" of "bset" expresses
15107 * the variable at position "pos" in terms of the other variables,
15108 * extract this expression as a function of those other variables,
15109 * excluding any local variables.
15111 static __isl_give isl_aff *extract_expr_aff(
15112 __isl_keep isl_basic_set *bset, int eq, int pos)
15114 isl_size dim;
15115 isl_ctx *ctx;
15116 isl_vec *v;
15117 isl_space *space;
15118 isl_local_space *ls;
15120 space = isl_basic_set_peek_space(bset);
15121 dim = isl_space_dim(space, isl_dim_all);
15122 if (dim < 0)
15123 return NULL;
15124 ctx = isl_basic_set_get_ctx(bset);
15125 v = extract_bound_from_constraint(ctx, bset->eq[eq], dim, pos);
15126 ls = isl_local_space_from_space(isl_space_copy(space));
15127 return isl_aff_alloc_vec(ls, v);
15130 /* Given the presence in "bmap" of an equality constraint "eq"
15132 * f(x) + t m i + m n h(y) = 0
15134 * and a pair of inequality constraints
15136 * -g(x) + i >= 0 ("lower")
15137 * g(x) - i + d >= 0
15139 * where i is the output dimension at position "pos",
15140 * f(x) and g(x) are expressions in the parameters and
15141 * input dimensions (if any),
15142 * h(y) is an expression in the other output dimensions,
15143 * t is +1 or -1, and
15144 * d < "n",
15145 * return the expression
15147 * (-t f(x)/m - g(x)) mod n + g(x)
15149 * The expression is first constructed in terms of the wrapped space of "bmap".
15150 * Since this expression does not depend on the original output dimensions,
15151 * they can be removed, resulting in an expression in terms
15152 * of the input dimensions.
15153 * If "bmap" is actually a set, then the resulting expression has no domain.
15155 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15156 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15158 static __isl_give isl_aff *construct_mod(__isl_keep isl_basic_map *bmap,
15159 int pos, int eq, int lower, isl_int n)
15161 isl_ctx *ctx;
15162 isl_basic_set *bset;
15163 isl_aff *f, *g, *mod;
15164 isl_val *v;
15165 isl_bool is_set;
15167 is_set = isl_basic_map_is_set(bmap);
15168 if (is_set < 0)
15169 return NULL;
15170 ctx = isl_basic_map_get_ctx(bmap);
15172 if (is_set)
15173 bset = bset_from_bmap(isl_basic_map_copy(bmap));
15174 else
15175 bset = isl_basic_map_wrap(isl_basic_map_copy(bmap));
15176 g = extract_lower_bound_aff(bset, lower, pos);
15177 f = extract_expr_aff(bset, eq, pos);
15178 isl_basic_set_free(bset);
15180 mod = isl_aff_sub(f, isl_aff_copy(g));
15181 v = isl_val_int_from_isl_int(ctx, n);
15182 mod = isl_aff_mod_val(mod, v);
15183 mod = isl_aff_add(mod, g);
15184 if (is_set)
15185 mod = isl_aff_project_domain_on_params(mod);
15186 else
15187 mod = isl_aff_domain_factor_domain(mod);
15189 return mod;
15192 /* Look for a combination of constraints in "bmap" that ensure
15193 * that output dimension "pos" is equal to some modulo expression
15194 * in the parameters and input dimensions and
15195 * return this expression if found.
15197 * The constraints are assumed to have been sorted.
15199 * In particular look for an equality constraint
15201 * f(x) + t m i + m n h(y) = 0
15203 * and a pair of inequality constraints
15205 * -g(x) + i >= 0
15206 * g(x) - i + d >= 0
15208 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15209 * h(y) is an expression in local variables and other output dimensions,
15210 * t is +1 or -1, and
15211 * d < n.
15213 * If such a combination of constraints can be found
15214 * then
15216 * i = -t f(x)/m - t n h(y)
15217 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15219 * and so, because 0 <= i - g(x) <= d < n,
15221 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15222 * = (-t f(x)/m - g(x)) mod n
15224 * That is,
15226 * i = (-t f(x)/m - g(x)) mod n + g(x)
15228 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_mod(
15229 __isl_keep isl_basic_map *bmap, int pos)
15231 int i, j;
15232 isl_size n_eq, n_ineq;
15233 isl_size v_out, n_out;
15234 isl_size v_div, n_div;
15235 isl_size lower;
15236 isl_int gcd;
15237 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15239 n_eq = isl_basic_map_n_equality(bmap);
15240 n_ineq = isl_basic_map_n_inequality(bmap);
15241 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15242 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15243 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15244 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15245 if (n_eq < 0 || n_ineq < 0 ||
15246 v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15247 goto error;
15248 if (n_eq == 0 || n_ineq < 2 || n_out == 1)
15249 return res;
15251 isl_int_init(gcd);
15252 for (i = 0; i < n_eq; ++i) {
15253 if (isl_int_is_zero(bmap->eq[i][1 + v_out + pos]))
15254 continue;
15255 isl_seq_gcd(bmap->eq[i] + 1 + v_div, n_div, &gcd);
15256 for (j = 0; j < n_out; ++j) {
15257 if (j == pos)
15258 continue;
15259 isl_int_gcd(gcd, gcd, bmap->eq[i][1 + v_out + j]);
15261 if (!isl_int_abs_gt(gcd, bmap->eq[i][1 + v_out + pos]))
15262 continue;
15263 if (!isl_int_is_divisible_by(gcd, bmap->eq[i][1 + v_out + pos]))
15264 continue;
15265 isl_int_divexact(gcd, gcd, bmap->eq[i][1 + v_out + pos]);
15266 isl_int_abs(gcd, gcd);
15267 lower = find_output_lower_mod_constraint(bmap, pos, gcd);
15268 if (lower >= n_ineq)
15269 continue;
15270 res.valid = isl_bool_true;
15271 if (lower >= 0)
15272 res.value = construct_mod(bmap, v_out + pos, i,
15273 lower, gcd);
15274 if (!res.value)
15275 res.valid = isl_bool_error;
15276 isl_int_clear(gcd);
15277 return res;
15280 isl_int_clear(gcd);
15281 return res;
15282 error:
15283 res.valid = isl_bool_error;
15284 return res;
15287 /* Construct an isl_aff from the given domain local space "ls" and
15288 * coefficients "v", where the local space may involve
15289 * local variables without a known expression, as long as these
15290 * do not have a non-zero coefficient in "v".
15291 * These need to be pruned away first since an isl_aff cannot
15292 * reference any local variables without a known expression.
15293 * For simplicity, remove all local variables that have a zero coefficient and
15294 * that are not used in other local variables with a non-zero coefficient.
15296 static __isl_give isl_aff *isl_aff_alloc_vec_prune(
15297 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
15299 int i;
15300 isl_size n_div, v_div;
15302 n_div = isl_local_space_dim(ls, isl_dim_div);
15303 v_div = isl_local_space_var_offset(ls, isl_dim_div);
15304 if (n_div < 0 || v_div < 0 || !v)
15305 goto error;
15306 for (i = n_div - 1; i >= 0; --i) {
15307 isl_bool involves;
15309 if (!isl_int_is_zero(v->el[1 + 1 + v_div + i]))
15310 continue;
15311 involves = isl_local_space_involves_dims(ls, isl_dim_div, i, 1);
15312 if (involves < 0)
15313 goto error;
15314 if (involves)
15315 continue;
15316 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
15317 v = isl_vec_drop_els(v, 1 + 1 + v_div + i, 1);
15318 if (!v)
15319 goto error;
15322 return isl_aff_alloc_vec(ls, v);
15323 error:
15324 isl_local_space_free(ls);
15325 isl_vec_free(v);
15326 return NULL;
15329 /* Look for a pair of constraints in "bmap" that ensure
15330 * that output dimension "pos" is equal to some integer division expression
15331 * in the parameters and input dimensions and
15332 * return this expression if found.
15334 * In particular, looks for a pair of constraints
15336 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
15338 * and
15340 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
15342 * where m > 1 and e only depends on parameters and input dimensions,
15343 * and such that
15345 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15347 * If such a pair of constraints can be found
15348 * then
15350 * x = floor((e(...) + c1) / m)
15352 * with e(...) an expression that does not involve any other output dimensions.
15354 * Note that we know that
15356 * c1 + c2 >= 1
15358 * If c1 + c2 were 0, then we would have detected an equality during
15359 * simplification. If c1 + c2 were negative, then we would have detected
15360 * a contradiction.
15362 * The constraint defining the integer division is guaranteed not to involve
15363 * any local variables without a known expression, but such local variables
15364 * may appear in other constraints. They therefore need to be removed
15365 * during the construction of the affine expression.
15367 static __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_div(
15368 __isl_keep isl_basic_map *bmap, int pos)
15370 isl_size i;
15371 isl_size n_ineq;
15372 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15373 isl_local_space *ls;
15374 isl_aff *aff;
15375 isl_vec *v;
15376 isl_bool is_set;
15378 n_ineq = isl_basic_map_n_inequality(bmap);
15379 if (n_ineq < 0)
15380 goto error;
15382 i = isl_basic_map_find_output_upper_div_constraint(bmap, pos);
15383 if (i < 0)
15384 goto error;
15385 if (i >= n_ineq)
15386 return res;
15388 is_set = isl_basic_map_is_set(bmap);
15389 if (is_set < 0)
15390 bmap = isl_basic_map_free(bmap);
15392 ls = isl_basic_map_get_local_space(bmap);
15393 if (!is_set)
15394 ls = isl_local_space_wrap(ls);
15395 v = isl_basic_map_inequality_extract_output_upper_bound(bmap, i, pos);
15397 aff = isl_aff_alloc_vec_prune(ls, v);
15398 aff = isl_aff_floor(aff);
15400 if (is_set)
15401 aff = isl_aff_project_domain_on_params(aff);
15402 else
15403 aff = isl_aff_domain_factor_domain(aff);
15405 res.valid = isl_bool_true;
15406 res.value = aff;
15407 return res;
15408 error:
15409 res.valid = isl_bool_error;
15410 return res;
15413 /* Look for a combination of constraints in "bmap" that ensure
15414 * that output dimension "pos" is equal to some integer division or
15415 * modulo expression in the parameters and input dimensions and
15416 * return this expression if found.
15418 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_div_mod(
15419 __isl_keep isl_basic_map *bmap, int pos)
15421 isl_maybe_isl_aff div;
15423 div = isl_basic_map_try_find_output_div(bmap, pos);
15424 if (div.valid < 0 || div.valid)
15425 return div;
15426 return isl_basic_map_try_find_output_mod(bmap, pos);
15429 /* Look for a combination of constraints in "bmap" that ensure
15430 * that any output dimension is equal to some integer division or
15431 * modulo expression in the parameters and input dimensions and
15432 * return this expression if found.
15433 * The position of the output dimension (if any) is returned in "pos".
15435 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_any_output_div_mod(
15436 __isl_keep isl_basic_map *bmap, int *pos)
15438 isl_size dim;
15439 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15441 dim = isl_basic_map_dim(bmap, isl_dim_out);
15442 if (dim < 0)
15443 goto error;
15445 for (*pos = 0; *pos < dim; ++*pos) {
15446 res = isl_basic_map_try_find_output_div_mod(bmap, *pos);
15447 if (res.valid < 0 || res.valid)
15448 return res;
15451 return res;
15452 error:
15453 res.valid = isl_bool_error;
15454 return res;
15457 /* Return a copy of the equality constraints of "bset" as a matrix.
15459 __isl_give isl_mat *isl_basic_set_extract_equalities(
15460 __isl_keep isl_basic_set *bset)
15462 isl_ctx *ctx;
15463 isl_size total;
15465 total = isl_basic_set_dim(bset, isl_dim_all);
15466 if (total < 0)
15467 return NULL;
15469 ctx = isl_basic_set_get_ctx(bset);
15470 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
15473 /* Are the "n" "coefficients" starting at "first" of the integer division
15474 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15475 * to each other?
15476 * The "coefficient" at position 0 is the denominator.
15477 * The "coefficient" at position 1 is the constant term.
15479 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
15480 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
15481 unsigned first, unsigned n)
15483 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
15484 return isl_bool_error;
15485 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
15486 return isl_bool_error;
15487 return isl_seq_eq(bmap1->div[pos1] + first,
15488 bmap2->div[pos2] + first, n);
15491 /* Are the integer division expressions at position "pos1" in "bmap1" and
15492 * "pos2" in "bmap2" equal to each other, except that the constant terms
15493 * are different?
15495 isl_bool isl_basic_map_equal_div_expr_except_constant(
15496 __isl_keep isl_basic_map *bmap1, int pos1,
15497 __isl_keep isl_basic_map *bmap2, int pos2)
15499 isl_bool equal;
15500 isl_size total, total2;
15502 total = isl_basic_map_dim(bmap1, isl_dim_all);
15503 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
15504 if (total < 0 || total2 < 0)
15505 return isl_bool_error;
15506 if (total != total2)
15507 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
15508 "incomparable div expressions", return isl_bool_error);
15509 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15510 0, 1);
15511 if (equal < 0 || !equal)
15512 return equal;
15513 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15514 1, 1);
15515 if (equal < 0 || equal)
15516 return isl_bool_not(equal);
15517 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15518 2, total);
15521 /* Replace the numerator of the constant term of the integer division
15522 * expression at position "div" in "bmap" by "value".
15523 * The caller guarantees that this does not change the meaning
15524 * of the input.
15526 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
15527 __isl_take isl_basic_map *bmap, int div, int value)
15529 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
15530 return isl_basic_map_free(bmap);
15532 isl_int_set_si(bmap->div[div][1], value);
15534 return bmap;
15537 /* Is the point "inner" internal to inequality constraint "ineq"
15538 * of "bset"?
15539 * The point is considered to be internal to the inequality constraint,
15540 * if it strictly lies on the positive side of the inequality constraint,
15541 * or if it lies on the constraint and the constraint is lexico-positive.
15543 static isl_bool is_internal(__isl_keep isl_vec *inner,
15544 __isl_keep isl_basic_set *bset, int ineq)
15546 isl_ctx *ctx;
15547 int pos;
15548 isl_size total;
15550 if (!inner || !bset)
15551 return isl_bool_error;
15553 ctx = isl_basic_set_get_ctx(bset);
15554 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
15555 &ctx->normalize_gcd);
15556 if (!isl_int_is_zero(ctx->normalize_gcd))
15557 return isl_int_is_nonneg(ctx->normalize_gcd);
15559 total = isl_basic_set_dim(bset, isl_dim_all);
15560 if (total < 0)
15561 return isl_bool_error;
15562 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
15563 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
15566 /* Tighten the inequality constraints of "bset" that are outward with respect
15567 * to the point "vec".
15568 * That is, tighten the constraints that are not satisfied by "vec".
15570 * "vec" is a point internal to some superset S of "bset" that is used
15571 * to make the subsets of S disjoint, by tightening one half of the constraints
15572 * that separate two subsets. In particular, the constraints of S
15573 * are all satisfied by "vec" and should not be tightened.
15574 * Of the internal constraints, those that have "vec" on the outside
15575 * are tightened. The shared facet is included in the adjacent subset
15576 * with the opposite constraint.
15577 * For constraints that saturate "vec", this criterion cannot be used
15578 * to determine which of the two sides should be tightened.
15579 * Instead, the sign of the first non-zero coefficient is used
15580 * to make this choice. Note that this second criterion is never used
15581 * on the constraints of S since "vec" is interior to "S".
15583 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
15584 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
15586 int j;
15588 bset = isl_basic_set_cow(bset);
15589 if (!bset)
15590 return NULL;
15591 for (j = 0; j < bset->n_ineq; ++j) {
15592 isl_bool internal;
15594 internal = is_internal(vec, bset, j);
15595 if (internal < 0)
15596 return isl_basic_set_free(bset);
15597 if (internal)
15598 continue;
15599 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
15602 return bset;
15605 /* Replace the variables x of type "type" starting at "first" in "bmap"
15606 * by x' with x = M x' with M the matrix trans.
15607 * That is, replace the corresponding coefficients c by c M.
15609 * The transformation matrix should be a square matrix.
15611 __isl_give isl_basic_map *isl_basic_map_transform_dims(
15612 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
15613 __isl_take isl_mat *trans)
15615 unsigned pos;
15617 bmap = isl_basic_map_cow(bmap);
15618 if (!bmap || !trans)
15619 goto error;
15621 if (trans->n_row != trans->n_col)
15622 isl_die(trans->ctx, isl_error_invalid,
15623 "expecting square transformation matrix", goto error);
15624 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
15625 goto error;
15627 pos = isl_basic_map_offset(bmap, type) + first;
15629 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
15630 isl_mat_copy(trans)) < 0)
15631 goto error;
15632 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
15633 isl_mat_copy(trans)) < 0)
15634 goto error;
15635 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
15636 isl_mat_copy(trans)) < 0)
15637 goto error;
15639 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
15640 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
15641 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
15643 isl_mat_free(trans);
15644 return bmap;
15645 error:
15646 isl_mat_free(trans);
15647 isl_basic_map_free(bmap);
15648 return NULL;
15651 /* Replace the variables x of type "type" starting at "first" in "bset"
15652 * by x' with x = M x' with M the matrix trans.
15653 * That is, replace the corresponding coefficients c by c M.
15655 * The transformation matrix should be a square matrix.
15657 __isl_give isl_basic_set *isl_basic_set_transform_dims(
15658 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
15659 __isl_take isl_mat *trans)
15661 return isl_basic_map_transform_dims(bset, type, first, trans);