isl_ast_build_expr.c: check_parallel_or_opposite: extract replace_if_simpler
[isl.git] / isl_map.c
blob53a0af937b9880e7bee3f26cc8b588b639e1d134
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <string.h>
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
28 #include <isl_blk.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
34 #include <isl_seq.h>
35 #include <isl/set.h>
36 #include <isl/map.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
39 #include <isl_sort.h>
40 #include "isl_tab.h"
41 #include <isl/vec.h>
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep const isl_basic_map *const_bset_to_bmap(
62 __isl_keep const isl_basic_set *bset)
64 return (const isl_basic_map *) bset;
67 #undef TYPE
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
73 switch (type) {
74 case isl_dim_param: return 1;
75 case isl_dim_in: return 1 + space->nparam;
76 case isl_dim_out: return 1 + space->nparam + space->n_in;
77 default: return 0;
81 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
82 enum isl_dim_type type)
84 if (!bmap)
85 return isl_size_error;
86 switch (type) {
87 case isl_dim_cst: return 1;
88 case isl_dim_param:
89 case isl_dim_in:
90 case isl_dim_out: return isl_space_dim(bmap->dim, type);
91 case isl_dim_div: return bmap->n_div;
92 case isl_dim_all: return isl_basic_map_total_dim(bmap);
93 default: return 0;
97 /* Return the space of "map".
99 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
101 return map ? map->dim : NULL;
104 /* Return the space of "set".
106 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
108 return isl_map_peek_space(set_to_map(set));
111 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
113 return isl_space_dim(isl_map_peek_space(map), type);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
120 return isl_map_dim(map, isl_dim_in);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
127 return isl_map_dim(map, isl_dim_out);
130 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
132 return isl_map_dim(set_to_map(set), type);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_set);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
146 enum isl_dim_type type)
148 isl_space *space;
150 space = isl_basic_map_peek_space(bmap);
152 switch (type) {
153 case isl_dim_param:
154 case isl_dim_in:
155 case isl_dim_out: return isl_space_offset(space, type);
156 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
157 case isl_dim_cst:
158 default:
159 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
160 "invalid dimension type", return isl_size_error);
164 /* Return the position of the variables of the given type
165 * within the sequence of variables of "bset".
167 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
168 enum isl_dim_type type)
170 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
173 /* Return the position of the coefficients of the variables of the given type
174 * within the sequence of coefficients of "bmap".
176 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
177 enum isl_dim_type type)
179 switch (type) {
180 case isl_dim_cst: return 0;
181 case isl_dim_param:
182 case isl_dim_in:
183 case isl_dim_out:
184 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
185 default: return 0;
189 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
190 enum isl_dim_type type)
192 return isl_basic_map_offset(bset, type);
195 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
197 return pos(map->dim, type);
200 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
201 enum isl_dim_type type)
203 return isl_basic_map_dim(bset, type);
206 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
208 return isl_basic_set_dim(bset, isl_dim_set);
211 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
213 return isl_basic_set_dim(bset, isl_dim_param);
216 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
218 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
221 isl_size isl_set_n_dim(__isl_keep isl_set *set)
223 return isl_set_dim(set, isl_dim_set);
226 isl_size isl_set_n_param(__isl_keep isl_set *set)
228 return isl_set_dim(set, isl_dim_param);
231 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
233 isl_size dim;
235 if (!bmap)
236 return isl_size_error;
237 dim = isl_space_dim(bmap->dim, isl_dim_all);
238 if (dim < 0)
239 return isl_size_error;
240 return dim + bmap->n_div;
243 /* Return the number of equality constraints in the description of "bmap".
244 * Return isl_size_error on error.
246 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
248 if (!bmap)
249 return isl_size_error;
250 return bmap->n_eq;
253 /* Return the number of equality constraints in the description of "bset".
254 * Return isl_size_error on error.
256 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
258 return isl_basic_map_n_equality(bset_to_bmap(bset));
261 /* Return the number of inequality constraints in the description of "bmap".
262 * Return isl_size_error on error.
264 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
266 if (!bmap)
267 return isl_size_error;
268 return bmap->n_ineq;
271 /* Return the number of inequality constraints in the description of "bset".
272 * Return isl_size_error on error.
274 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
276 return isl_basic_map_n_inequality(bset_to_bmap(bset));
279 /* Do "bmap1" and "bmap2" have the same parameters?
281 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
282 __isl_keep isl_basic_map *bmap2)
284 isl_space *space1, *space2;
286 space1 = isl_basic_map_peek_space(bmap1);
287 space2 = isl_basic_map_peek_space(bmap2);
288 return isl_space_has_equal_params(space1, space2);
291 /* Do "map1" and "map2" have the same parameters?
293 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
294 __isl_keep isl_map *map2)
296 isl_space *space1, *space2;
298 space1 = isl_map_peek_space(map1);
299 space2 = isl_map_peek_space(map2);
300 return isl_space_has_equal_params(space1, space2);
303 /* Do "map" and "set" have the same parameters?
305 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
306 __isl_keep isl_set *set)
308 return isl_map_has_equal_params(map, set_to_map(set));
311 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
313 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
314 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
316 isl_space *bmap_space, *bset_space;
318 bmap_space = isl_basic_map_peek_space(bmap);
319 bset_space = isl_basic_set_peek_space(bset);
320 return isl_space_tuple_is_equal(bmap_space, type,
321 bset_space, isl_dim_set);
324 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
326 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
327 enum isl_dim_type type, __isl_keep isl_set *set)
329 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
332 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
333 __isl_keep isl_set *set)
335 isl_bool m;
336 if (!map || !set)
337 return isl_bool_error;
338 m = isl_map_has_equal_params(map, set_to_map(set));
339 if (m < 0 || !m)
340 return m;
341 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
344 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
345 __isl_keep isl_basic_set *bset)
347 isl_bool m;
348 if (!bmap || !bset)
349 return isl_bool_error;
350 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
351 if (m < 0 || !m)
352 return m;
353 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
356 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
357 __isl_keep isl_set *set)
359 isl_bool m;
360 if (!map || !set)
361 return isl_bool_error;
362 m = isl_map_has_equal_params(map, set_to_map(set));
363 if (m < 0 || !m)
364 return m;
365 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
368 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
369 __isl_keep isl_basic_set *bset)
371 isl_bool m;
372 if (!bmap || !bset)
373 return isl_bool_error;
374 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
375 if (m < 0 || !m)
376 return m;
377 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
380 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
382 return bmap ? bmap->ctx : NULL;
385 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
387 return bset ? bset->ctx : NULL;
390 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
392 return map ? map->ctx : NULL;
395 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
397 return set ? set->ctx : NULL;
400 /* Return the space of "bmap".
402 __isl_keep isl_space *isl_basic_map_peek_space(
403 __isl_keep const isl_basic_map *bmap)
405 return bmap ? bmap->dim : NULL;
408 /* Return the space of "bset".
410 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
412 return isl_basic_map_peek_space(bset_to_bmap(bset));
415 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
417 return isl_space_copy(isl_basic_map_peek_space(bmap));
420 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
422 return isl_basic_map_get_space(bset_to_bmap(bset));
425 /* Return the space of "bmap".
426 * This may be either a copy or the space itself
427 * if there is only one reference to "bmap".
428 * This allows the space to be modified inplace
429 * if both the basic map and its space have only a single reference.
430 * The caller is not allowed to modify "bmap" between this call and
431 * a subsequent call to isl_basic_map_restore_space.
432 * The only exception is that isl_basic_map_free can be called instead.
434 static __isl_give isl_space *isl_basic_map_take_space(
435 __isl_keep isl_basic_map *bmap)
437 isl_space *space;
439 if (!bmap)
440 return NULL;
441 if (bmap->ref != 1)
442 return isl_basic_map_get_space(bmap);
443 space = bmap->dim;
444 bmap->dim = NULL;
445 return space;
448 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
449 * due to a preceding call to isl_basic_map_take_space.
450 * However, in this case, "bmap" only has a single reference and
451 * then the call to isl_basic_map_cow has no effect.
453 static __isl_give isl_basic_map *isl_basic_map_restore_space(
454 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
456 if (!bmap || !space)
457 goto error;
459 if (bmap->dim == space) {
460 isl_space_free(space);
461 return bmap;
464 bmap = isl_basic_map_cow(bmap);
465 if (!bmap)
466 goto error;
467 isl_space_free(bmap->dim);
468 bmap->dim = space;
470 return bmap;
471 error:
472 isl_basic_map_free(bmap);
473 isl_space_free(space);
474 return NULL;
477 /* Extract the divs in "bmap" as a matrix.
479 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
481 int i;
482 isl_ctx *ctx;
483 isl_mat *div;
484 isl_size v_div;
485 unsigned cols;
487 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
488 if (v_div < 0)
489 return NULL;
491 ctx = isl_basic_map_get_ctx(bmap);
492 cols = 1 + 1 + v_div + bmap->n_div;
493 div = isl_mat_alloc(ctx, bmap->n_div, cols);
494 if (!div)
495 return NULL;
497 for (i = 0; i < bmap->n_div; ++i)
498 isl_seq_cpy(div->row[i], bmap->div[i], cols);
500 return div;
503 /* Extract the divs in "bset" as a matrix.
505 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
507 return isl_basic_map_get_divs(bset);
510 __isl_give isl_local_space *isl_basic_map_get_local_space(
511 __isl_keep isl_basic_map *bmap)
513 isl_mat *div;
515 if (!bmap)
516 return NULL;
518 div = isl_basic_map_get_divs(bmap);
519 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
522 __isl_give isl_local_space *isl_basic_set_get_local_space(
523 __isl_keep isl_basic_set *bset)
525 return isl_basic_map_get_local_space(bset);
528 /* For each known div d = floor(f/m), add the constraints
530 * f - m d >= 0
531 * -(f-(m-1)) + m d >= 0
533 * Do not finalize the result.
535 static __isl_give isl_basic_map *add_known_div_constraints(
536 __isl_take isl_basic_map *bmap)
538 int i;
539 isl_size n_div;
541 n_div = isl_basic_map_dim(bmap, isl_dim_div);
542 if (n_div < 0)
543 return isl_basic_map_free(bmap);
544 if (n_div == 0)
545 return bmap;
546 bmap = isl_basic_map_cow(bmap);
547 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
548 if (!bmap)
549 return NULL;
550 for (i = 0; i < n_div; ++i) {
551 if (isl_int_is_zero(bmap->div[i][0]))
552 continue;
553 bmap = isl_basic_map_add_div_constraints(bmap, i);
556 return bmap;
559 __isl_give isl_basic_map *isl_basic_map_from_local_space(
560 __isl_take isl_local_space *ls)
562 int i;
563 isl_size n_div;
564 isl_basic_map *bmap;
566 n_div = isl_local_space_dim(ls, isl_dim_div);
567 if (n_div < 0)
568 ls = isl_local_space_free(ls);
569 if (!ls)
570 return NULL;
572 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
573 n_div, 0, 2 * n_div);
575 for (i = 0; i < n_div; ++i)
576 if (isl_basic_map_alloc_div(bmap) < 0)
577 goto error;
579 for (i = 0; i < n_div; ++i)
580 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
581 bmap = add_known_div_constraints(bmap);
583 isl_local_space_free(ls);
584 return bmap;
585 error:
586 isl_local_space_free(ls);
587 isl_basic_map_free(bmap);
588 return NULL;
591 __isl_give isl_basic_set *isl_basic_set_from_local_space(
592 __isl_take isl_local_space *ls)
594 return isl_basic_map_from_local_space(ls);
597 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
599 return isl_space_copy(isl_map_peek_space(map));
602 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
604 if (!set)
605 return NULL;
606 return isl_space_copy(set->dim);
609 /* Return the space of "map".
610 * This may be either a copy or the space itself
611 * if there is only one reference to "map".
612 * This allows the space to be modified inplace
613 * if both the map and its space have only a single reference.
614 * The caller is not allowed to modify "map" between this call and
615 * a subsequent call to isl_map_restore_space.
616 * The only exception is that isl_map_free can be called instead.
618 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
620 isl_space *space;
622 if (!map)
623 return NULL;
624 if (map->ref != 1)
625 return isl_map_get_space(map);
626 space = map->dim;
627 map->dim = NULL;
628 return space;
631 /* Set the space of "map" to "space", where the space of "map" may be missing
632 * due to a preceding call to isl_map_take_space.
633 * However, in this case, "map" only has a single reference and
634 * then the call to isl_map_cow has no effect.
636 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
637 __isl_take isl_space *space)
639 if (!map || !space)
640 goto error;
642 if (map->dim == space) {
643 isl_space_free(space);
644 return map;
647 map = isl_map_cow(map);
648 if (!map)
649 goto error;
650 isl_space_free(map->dim);
651 map->dim = space;
653 return map;
654 error:
655 isl_map_free(map);
656 isl_space_free(space);
657 return NULL;
660 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
661 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
663 isl_space *space;
665 space = isl_basic_map_take_space(bmap);
666 space = isl_space_set_tuple_name(space, type, s);
667 bmap = isl_basic_map_restore_space(bmap, space);
668 bmap = isl_basic_map_finalize(bmap);
669 return bmap;
672 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
673 __isl_take isl_basic_set *bset, const char *s)
675 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
678 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
679 enum isl_dim_type type)
681 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
684 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
685 enum isl_dim_type type, const char *s)
687 int i;
688 isl_space *space;
690 map = isl_map_cow(map);
691 if (!map)
692 return NULL;
694 for (i = 0; i < map->n; ++i) {
695 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
696 if (!map->p[i])
697 goto error;
700 space = isl_map_take_space(map);
701 space = isl_space_set_tuple_name(space, type, s);
702 map = isl_map_restore_space(map, space);
704 return map;
705 error:
706 isl_map_free(map);
707 return NULL;
710 /* Replace the identifier of the tuple of type "type" by "id".
712 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
713 __isl_take isl_basic_map *bmap,
714 enum isl_dim_type type, __isl_take isl_id *id)
716 isl_space *space;
718 space = isl_basic_map_take_space(bmap);
719 space = isl_space_set_tuple_id(space, type, id);
720 bmap = isl_basic_map_restore_space(bmap, space);
721 bmap = isl_basic_map_finalize(bmap);
722 return bmap;
725 /* Replace the identifier of the tuple by "id".
727 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
728 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
730 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
733 /* Does the input or output tuple have a name?
735 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
737 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
740 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
741 enum isl_dim_type type)
743 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
746 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
747 const char *s)
749 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
750 isl_dim_set, s));
753 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
754 enum isl_dim_type type, __isl_take isl_id *id)
756 isl_space *space;
758 space = isl_map_take_space(map);
759 space = isl_space_set_tuple_id(space, type, id);
760 map = isl_map_restore_space(map, space);
762 return isl_map_reset_space(map, isl_map_get_space(map));
765 /* Replace the identifier of the domain tuple of "map" by "id".
767 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
768 __isl_take isl_id *id)
770 return isl_map_set_tuple_id(map, isl_dim_in, id);
773 /* Replace the identifier of the range tuple of "map" by "id".
775 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
776 __isl_take isl_id *id)
778 return isl_map_set_tuple_id(map, isl_dim_out, id);
781 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
782 __isl_take isl_id *id)
784 return isl_map_set_tuple_id(set, isl_dim_set, id);
787 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
788 enum isl_dim_type type)
790 isl_space *space;
792 space = isl_map_take_space(map);
793 space = isl_space_reset_tuple_id(space, type);
794 map = isl_map_restore_space(map, space);
796 return isl_map_reset_space(map, isl_map_get_space(map));
799 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
801 return isl_map_reset_tuple_id(set, isl_dim_set);
804 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
806 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
809 /* Does the domain tuple of "map" have an identifier?
811 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
813 return isl_map_has_tuple_id(map, isl_dim_in);
816 /* Does the range tuple of "map" have an identifier?
818 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
820 return isl_map_has_tuple_id(map, isl_dim_out);
823 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
824 enum isl_dim_type type)
826 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
829 /* Return the identifier of the domain tuple of "map", assuming it has one.
831 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
833 return isl_map_get_tuple_id(map, isl_dim_in);
836 /* Return the identifier of the range tuple of "map", assuming it has one.
838 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
840 return isl_map_get_tuple_id(map, isl_dim_out);
843 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
845 return isl_map_has_tuple_id(set, isl_dim_set);
848 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
850 return isl_map_get_tuple_id(set, isl_dim_set);
853 /* Does the set tuple have a name?
855 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
857 if (!set)
858 return isl_bool_error;
859 return isl_space_has_tuple_name(set->dim, isl_dim_set);
863 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
865 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
868 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
870 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
873 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
874 enum isl_dim_type type, unsigned pos)
876 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
879 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
880 enum isl_dim_type type, unsigned pos)
882 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
885 /* Does the given dimension have a name?
887 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
888 enum isl_dim_type type, unsigned pos)
890 if (!map)
891 return isl_bool_error;
892 return isl_space_has_dim_name(map->dim, type, pos);
895 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
896 enum isl_dim_type type, unsigned pos)
898 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
901 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
902 enum isl_dim_type type, unsigned pos)
904 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
907 /* Does the given dimension have a name?
909 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
910 enum isl_dim_type type, unsigned pos)
912 if (!set)
913 return isl_bool_error;
914 return isl_space_has_dim_name(set->dim, type, pos);
917 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
918 __isl_take isl_basic_map *bmap,
919 enum isl_dim_type type, unsigned pos, const char *s)
921 isl_space *space;
923 space = isl_basic_map_take_space(bmap);
924 space = isl_space_set_dim_name(space, type, pos, s);
925 bmap = isl_basic_map_restore_space(bmap, space);
926 return isl_basic_map_finalize(bmap);
929 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
930 enum isl_dim_type type, unsigned pos, const char *s)
932 int i;
933 isl_space *space;
935 map = isl_map_cow(map);
936 if (!map)
937 return NULL;
939 for (i = 0; i < map->n; ++i) {
940 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
941 if (!map->p[i])
942 goto error;
945 space = isl_map_take_space(map);
946 space = isl_space_set_dim_name(space, type, pos, s);
947 map = isl_map_restore_space(map, space);
949 return map;
950 error:
951 isl_map_free(map);
952 return NULL;
955 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
956 __isl_take isl_basic_set *bset,
957 enum isl_dim_type type, unsigned pos, const char *s)
959 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
960 type, pos, s));
963 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
964 enum isl_dim_type type, unsigned pos, const char *s)
966 return set_from_map(isl_map_set_dim_name(set_to_map(set),
967 type, pos, s));
970 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
971 enum isl_dim_type type, unsigned pos)
973 if (!bmap)
974 return isl_bool_error;
975 return isl_space_has_dim_id(bmap->dim, type, pos);
978 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
979 enum isl_dim_type type, unsigned pos)
981 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
984 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
985 enum isl_dim_type type, unsigned pos)
987 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
990 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
991 enum isl_dim_type type, unsigned pos)
993 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
996 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
997 enum isl_dim_type type, unsigned pos)
999 return isl_map_has_dim_id(set, type, pos);
1002 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1003 enum isl_dim_type type, unsigned pos)
1005 return isl_map_get_dim_id(set, type, pos);
1008 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1009 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1011 isl_space *space;
1013 space = isl_map_take_space(map);
1014 space = isl_space_set_dim_id(space, type, pos, id);
1015 map = isl_map_restore_space(map, space);
1017 return isl_map_reset_space(map, isl_map_get_space(map));
1020 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1021 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1023 return isl_map_set_dim_id(set, type, pos, id);
1026 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1027 __isl_keep isl_id *id)
1029 if (!map)
1030 return -1;
1031 return isl_space_find_dim_by_id(map->dim, type, id);
1034 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1035 __isl_keep isl_id *id)
1037 return isl_map_find_dim_by_id(set, type, id);
1040 /* Return the position of the dimension of the given type and name
1041 * in "bmap".
1042 * Return -1 if no such dimension can be found.
1044 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1045 enum isl_dim_type type, const char *name)
1047 if (!bmap)
1048 return -1;
1049 return isl_space_find_dim_by_name(bmap->dim, type, name);
1052 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1053 const char *name)
1055 if (!map)
1056 return -1;
1057 return isl_space_find_dim_by_name(map->dim, type, name);
1060 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1061 const char *name)
1063 return isl_map_find_dim_by_name(set, type, name);
1066 /* Check whether equality i of bset is a pure stride constraint
1067 * on a single dimension, i.e., of the form
1069 * v = k e
1071 * with k a constant and e an existentially quantified variable.
1073 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1075 isl_size nparam;
1076 isl_size d;
1077 isl_size n_div;
1078 int pos1;
1079 int pos2;
1081 nparam = isl_basic_set_dim(bset, isl_dim_param);
1082 d = isl_basic_set_dim(bset, isl_dim_set);
1083 n_div = isl_basic_set_dim(bset, isl_dim_div);
1084 if (nparam < 0 || d < 0 || n_div < 0)
1085 return isl_bool_error;
1087 if (!isl_int_is_zero(bset->eq[i][0]))
1088 return isl_bool_false;
1090 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1091 return isl_bool_false;
1092 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1093 if (pos1 == -1)
1094 return isl_bool_false;
1095 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1096 d - pos1 - 1) != -1)
1097 return isl_bool_false;
1099 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1100 if (pos2 == -1)
1101 return isl_bool_false;
1102 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1103 n_div - pos2 - 1) != -1)
1104 return isl_bool_false;
1105 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1106 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1107 return isl_bool_false;
1109 return isl_bool_true;
1112 /* Reset the user pointer on all identifiers of parameters and tuples
1113 * of the space of "map".
1115 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1117 isl_space *space;
1119 space = isl_map_get_space(map);
1120 space = isl_space_reset_user(space);
1121 map = isl_map_reset_space(map, space);
1123 return map;
1126 /* Reset the user pointer on all identifiers of parameters and tuples
1127 * of the space of "set".
1129 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1131 return isl_map_reset_user(set);
1134 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1136 if (!bmap)
1137 return isl_bool_error;
1138 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1141 /* Has "map" been marked as a rational map?
1142 * In particular, have all basic maps in "map" been marked this way?
1143 * An empty map is not considered to be rational.
1144 * Maps where only some of the basic maps are marked rational
1145 * are not allowed.
1147 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1149 int i;
1150 isl_bool rational;
1152 if (!map)
1153 return isl_bool_error;
1154 if (map->n == 0)
1155 return isl_bool_false;
1156 rational = isl_basic_map_is_rational(map->p[0]);
1157 if (rational < 0)
1158 return rational;
1159 for (i = 1; i < map->n; ++i) {
1160 isl_bool rational_i;
1162 rational_i = isl_basic_map_is_rational(map->p[i]);
1163 if (rational_i < 0)
1164 return rational_i;
1165 if (rational != rational_i)
1166 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1167 "mixed rational and integer basic maps "
1168 "not supported", return isl_bool_error);
1171 return rational;
1174 /* Has "set" been marked as a rational set?
1175 * In particular, have all basic set in "set" been marked this way?
1176 * An empty set is not considered to be rational.
1177 * Sets where only some of the basic sets are marked rational
1178 * are not allowed.
1180 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1182 return isl_map_is_rational(set);
1185 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1187 return isl_basic_map_is_rational(bset);
1190 /* Does "bmap" contain any rational points?
1192 * If "bmap" has an equality for each dimension, equating the dimension
1193 * to an integer constant, then it has no rational points, even if it
1194 * is marked as rational.
1196 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1198 isl_bool has_rational = isl_bool_true;
1199 isl_size total;
1201 if (!bmap)
1202 return isl_bool_error;
1203 if (isl_basic_map_plain_is_empty(bmap))
1204 return isl_bool_false;
1205 if (!isl_basic_map_is_rational(bmap))
1206 return isl_bool_false;
1207 bmap = isl_basic_map_copy(bmap);
1208 bmap = isl_basic_map_implicit_equalities(bmap);
1209 total = isl_basic_map_dim(bmap, isl_dim_all);
1210 if (total < 0)
1211 return isl_bool_error;
1212 if (bmap->n_eq == total) {
1213 int i, j;
1214 for (i = 0; i < bmap->n_eq; ++i) {
1215 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1216 if (j < 0)
1217 break;
1218 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1219 !isl_int_is_negone(bmap->eq[i][1 + j]))
1220 break;
1221 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1222 total - j - 1);
1223 if (j >= 0)
1224 break;
1226 if (i == bmap->n_eq)
1227 has_rational = isl_bool_false;
1229 isl_basic_map_free(bmap);
1231 return has_rational;
1234 /* Does "map" contain any rational points?
1236 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1238 int i;
1239 isl_bool has_rational;
1241 if (!map)
1242 return isl_bool_error;
1243 for (i = 0; i < map->n; ++i) {
1244 has_rational = isl_basic_map_has_rational(map->p[i]);
1245 if (has_rational < 0 || has_rational)
1246 return has_rational;
1248 return isl_bool_false;
1251 /* Does "set" contain any rational points?
1253 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1255 return isl_map_has_rational(set);
1258 /* Is this basic set a parameter domain?
1260 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1262 if (!bset)
1263 return isl_bool_error;
1264 return isl_space_is_params(bset->dim);
1267 /* Is this set a parameter domain?
1269 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1271 if (!set)
1272 return isl_bool_error;
1273 return isl_space_is_params(set->dim);
1276 /* Is this map actually a parameter domain?
1277 * Users should never call this function. Outside of isl,
1278 * a map can never be a parameter domain.
1280 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1282 if (!map)
1283 return isl_bool_error;
1284 return isl_space_is_params(map->dim);
1287 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1288 __isl_take isl_basic_map *bmap, unsigned extra,
1289 unsigned n_eq, unsigned n_ineq)
1291 int i;
1292 isl_space *space = isl_basic_map_peek_space(bmap);
1293 isl_size n_var = isl_space_dim(space, isl_dim_all);
1294 size_t row_size = 1 + n_var + extra;
1296 bmap->ctx = ctx;
1297 isl_ctx_ref(ctx);
1299 if (n_var < 0)
1300 return isl_basic_map_free(bmap);
1302 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1303 if (isl_blk_is_error(bmap->block))
1304 goto error;
1306 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1307 if ((n_ineq + n_eq) && !bmap->ineq)
1308 goto error;
1310 if (extra == 0) {
1311 bmap->block2 = isl_blk_empty();
1312 bmap->div = NULL;
1313 } else {
1314 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1315 if (isl_blk_is_error(bmap->block2))
1316 goto error;
1318 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1319 if (!bmap->div)
1320 goto error;
1323 for (i = 0; i < n_ineq + n_eq; ++i)
1324 bmap->ineq[i] = bmap->block.data + i * row_size;
1326 for (i = 0; i < extra; ++i)
1327 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1329 bmap->ref = 1;
1330 bmap->flags = 0;
1331 bmap->c_size = n_eq + n_ineq;
1332 bmap->eq = bmap->ineq + n_ineq;
1333 bmap->extra = extra;
1334 bmap->n_eq = 0;
1335 bmap->n_ineq = 0;
1336 bmap->n_div = 0;
1337 bmap->sample = NULL;
1339 return bmap;
1340 error:
1341 isl_basic_map_free(bmap);
1342 return NULL;
1345 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1346 unsigned nparam, unsigned dim, unsigned extra,
1347 unsigned n_eq, unsigned n_ineq)
1349 struct isl_basic_map *bmap;
1350 isl_space *space;
1352 space = isl_space_set_alloc(ctx, nparam, dim);
1353 if (!space)
1354 return NULL;
1356 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1357 return bset_from_bmap(bmap);
1360 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1361 unsigned extra, unsigned n_eq, unsigned n_ineq)
1363 struct isl_basic_map *bmap;
1364 if (!space)
1365 return NULL;
1366 isl_assert(space->ctx, space->n_in == 0, goto error);
1367 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1368 return bset_from_bmap(bmap);
1369 error:
1370 isl_space_free(space);
1371 return NULL;
1374 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1375 unsigned extra, unsigned n_eq, unsigned n_ineq)
1377 struct isl_basic_map *bmap;
1379 if (!space)
1380 return NULL;
1381 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1382 if (!bmap)
1383 goto error;
1384 bmap->dim = space;
1386 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1387 error:
1388 isl_space_free(space);
1389 return NULL;
1392 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1393 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1394 unsigned n_eq, unsigned n_ineq)
1396 struct isl_basic_map *bmap;
1397 isl_space *space;
1399 space = isl_space_alloc(ctx, nparam, in, out);
1400 if (!space)
1401 return NULL;
1403 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1404 return bmap;
1407 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1408 __isl_keep isl_basic_map *src)
1410 int i;
1411 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1413 if (!dst || total < 0)
1414 return isl_basic_map_free(dst);
1416 for (i = 0; i < src->n_eq; ++i) {
1417 int j = isl_basic_map_alloc_equality(dst);
1418 if (j < 0)
1419 return isl_basic_map_free(dst);
1420 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1423 for (i = 0; i < src->n_ineq; ++i) {
1424 int j = isl_basic_map_alloc_inequality(dst);
1425 if (j < 0)
1426 return isl_basic_map_free(dst);
1427 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1430 for (i = 0; i < src->n_div; ++i) {
1431 int j = isl_basic_map_alloc_div(dst);
1432 if (j < 0)
1433 return isl_basic_map_free(dst);
1434 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1436 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1437 return dst;
1440 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1442 struct isl_basic_map *dup;
1444 if (!bmap)
1445 return NULL;
1446 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1447 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1448 dup = dup_constraints(dup, bmap);
1449 if (!dup)
1450 return NULL;
1451 dup->flags = bmap->flags;
1452 dup->sample = isl_vec_copy(bmap->sample);
1453 return dup;
1456 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1458 struct isl_basic_map *dup;
1460 dup = isl_basic_map_dup(bset_to_bmap(bset));
1461 return bset_from_bmap(dup);
1464 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1466 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1469 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1471 if (!set)
1472 return NULL;
1474 set->ref++;
1475 return set;
1478 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1480 if (!bmap)
1481 return NULL;
1483 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1484 bmap->ref++;
1485 return bmap;
1487 bmap = isl_basic_map_dup(bmap);
1488 if (bmap)
1489 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1490 return bmap;
1493 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1495 if (!map)
1496 return NULL;
1498 map->ref++;
1499 return map;
1502 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1504 if (!bmap)
1505 return NULL;
1507 if (--bmap->ref > 0)
1508 return NULL;
1510 isl_ctx_deref(bmap->ctx);
1511 free(bmap->div);
1512 isl_blk_free(bmap->ctx, bmap->block2);
1513 free(bmap->ineq);
1514 isl_blk_free(bmap->ctx, bmap->block);
1515 isl_vec_free(bmap->sample);
1516 isl_space_free(bmap->dim);
1517 free(bmap);
1519 return NULL;
1522 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1524 return isl_basic_map_free(bset_to_bmap(bset));
1527 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1529 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1532 /* Check that "bset" does not involve any parameters.
1534 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1536 isl_size nparam;
1538 nparam = isl_basic_set_dim(bset, isl_dim_param);
1539 if (nparam < 0)
1540 return isl_stat_error;
1541 if (nparam != 0)
1542 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1543 "basic set should not have any parameters",
1544 return isl_stat_error);
1545 return isl_stat_ok;
1548 /* Check that "bset" does not involve any local variables.
1550 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1552 isl_size n_div;
1554 n_div = isl_basic_set_dim(bset, isl_dim_div);
1555 if (n_div < 0)
1556 return isl_stat_error;
1557 if (n_div != 0)
1558 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1559 "basic set should not have any local variables",
1560 return isl_stat_error);
1561 return isl_stat_ok;
1564 #undef TYPE
1565 #define TYPE isl_map
1567 #include "isl_check_named_params_templ.c"
1569 #undef TYPE
1570 #define TYPE isl_basic_map
1572 static
1573 #include "isl_check_named_params_templ.c"
1575 /* Check that "bmap1" and "bmap2" have the same parameters,
1576 * reporting an error if they do not.
1578 static isl_stat isl_basic_map_check_equal_params(
1579 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1581 isl_bool match;
1583 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1584 if (match < 0)
1585 return isl_stat_error;
1586 if (!match)
1587 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1588 "parameters don't match", return isl_stat_error);
1589 return isl_stat_ok;
1592 #undef TYPE
1593 #define TYPE isl_map
1595 #include "isl_align_params_bin_templ.c"
1597 #undef SUFFIX
1598 #define SUFFIX set
1599 #undef ARG1
1600 #define ARG1 isl_map
1601 #undef ARG2
1602 #define ARG2 isl_set
1604 #include "isl_align_params_templ.c"
1606 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1607 __isl_keep isl_map *map2,
1608 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1610 isl_bool r;
1612 if (!map1 || !map2)
1613 return isl_bool_error;
1614 if (isl_map_has_equal_params(map1, map2))
1615 return fn(map1, map2);
1616 if (isl_map_check_named_params(map1) < 0)
1617 return isl_bool_error;
1618 if (isl_map_check_named_params(map2) < 0)
1619 return isl_bool_error;
1620 map1 = isl_map_copy(map1);
1621 map2 = isl_map_copy(map2);
1622 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1623 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1624 r = fn(map1, map2);
1625 isl_map_free(map1);
1626 isl_map_free(map2);
1627 return r;
1630 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1632 isl_size total;
1633 struct isl_ctx *ctx;
1635 total = isl_basic_map_dim(bmap, isl_dim_all);
1636 if (total < 0)
1637 return -1;
1638 ctx = bmap->ctx;
1639 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1640 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1641 return -1);
1642 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1646 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1647 isl_int *t;
1648 int j = isl_basic_map_alloc_inequality(bmap);
1649 if (j < 0)
1650 return -1;
1651 t = bmap->ineq[j];
1652 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1653 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1654 bmap->eq[-1] = t;
1655 bmap->n_eq++;
1656 bmap->n_ineq--;
1657 bmap->eq--;
1658 return 0;
1660 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1661 bmap->extra - bmap->n_div);
1662 return bmap->n_eq++;
1665 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1667 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1670 __isl_give isl_basic_map *isl_basic_map_free_equality(
1671 __isl_take isl_basic_map *bmap, unsigned n)
1673 if (!bmap)
1674 return NULL;
1675 if (n > bmap->n_eq)
1676 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1677 "invalid number of equalities",
1678 isl_basic_map_free(bmap));
1679 bmap->n_eq -= n;
1680 return bmap;
1683 __isl_give isl_basic_set *isl_basic_set_free_equality(
1684 __isl_take isl_basic_set *bset, unsigned n)
1686 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1687 n));
1690 /* Drop the equality constraint at position "pos",
1691 * preserving the order of the other equality constraints.
1693 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1695 isl_int *t;
1696 int r;
1698 if (!bmap)
1699 return -1;
1700 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1702 t = bmap->eq[pos];
1703 bmap->n_eq--;
1704 for (r = pos; r < bmap->n_eq; ++r)
1705 bmap->eq[r] = bmap->eq[r + 1];
1706 bmap->eq[bmap->n_eq] = t;
1708 return 0;
1711 /* Turn inequality "pos" of "bmap" into an equality.
1713 * In particular, we move the inequality in front of the equalities
1714 * and move the last inequality in the position of the moved inequality.
1715 * Note that isl_tab_make_equalities_explicit depends on this particular
1716 * change in the ordering of the constraints.
1718 void isl_basic_map_inequality_to_equality(
1719 __isl_keep isl_basic_map *bmap, unsigned pos)
1721 isl_int *t;
1723 t = bmap->ineq[pos];
1724 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1725 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1726 bmap->eq[-1] = t;
1727 bmap->n_eq++;
1728 bmap->n_ineq--;
1729 bmap->eq--;
1730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1731 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1736 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1738 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1741 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1743 isl_size total;
1744 struct isl_ctx *ctx;
1746 total = isl_basic_map_dim(bmap, isl_dim_all);
1747 if (total < 0)
1748 return -1;
1749 ctx = bmap->ctx;
1750 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1751 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1755 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1756 bmap->extra - bmap->n_div);
1757 return bmap->n_ineq++;
1760 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1762 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1765 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1766 __isl_take isl_basic_map *bmap, unsigned n)
1768 if (!bmap)
1769 return NULL;
1770 if (n > bmap->n_ineq)
1771 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1772 "invalid number of inequalities",
1773 return isl_basic_map_free(bmap));
1774 bmap->n_ineq -= n;
1775 return bmap;
1778 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1779 __isl_take isl_basic_set *bset, unsigned n)
1781 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1782 n));
1785 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1787 isl_int *t;
1788 if (!bmap)
1789 return -1;
1790 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1792 if (pos != bmap->n_ineq - 1) {
1793 t = bmap->ineq[pos];
1794 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1795 bmap->ineq[bmap->n_ineq - 1] = t;
1796 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1798 bmap->n_ineq--;
1799 return 0;
1802 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1804 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1807 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1808 isl_int *eq)
1810 isl_bool empty;
1811 isl_size total;
1812 int k;
1814 empty = isl_basic_map_plain_is_empty(bmap);
1815 if (empty < 0)
1816 return isl_basic_map_free(bmap);
1817 if (empty)
1818 return bmap;
1820 bmap = isl_basic_map_cow(bmap);
1821 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1822 total = isl_basic_map_dim(bmap, isl_dim_all);
1823 if (total < 0)
1824 return isl_basic_map_free(bmap);
1825 k = isl_basic_map_alloc_equality(bmap);
1826 if (k < 0)
1827 goto error;
1828 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1829 return bmap;
1830 error:
1831 isl_basic_map_free(bmap);
1832 return NULL;
1835 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1836 isl_int *eq)
1838 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1841 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1842 isl_int *ineq)
1844 isl_size total;
1845 int k;
1847 bmap = isl_basic_map_cow(bmap);
1848 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1849 total = isl_basic_map_dim(bmap, isl_dim_all);
1850 if (total < 0)
1851 return isl_basic_map_free(bmap);
1852 k = isl_basic_map_alloc_inequality(bmap);
1853 if (k < 0)
1854 goto error;
1855 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1856 return bmap;
1857 error:
1858 isl_basic_map_free(bmap);
1859 return NULL;
1862 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1863 isl_int *ineq)
1865 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1868 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1870 isl_size total;
1872 total = isl_basic_map_dim(bmap, isl_dim_all);
1873 if (total < 0)
1874 return -1;
1875 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1876 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1877 bmap->extra - bmap->n_div);
1878 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1879 return bmap->n_div++;
1882 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1884 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1887 #undef TYPE
1888 #define TYPE isl_basic_map
1889 #include "check_type_range_templ.c"
1891 /* Check that there are "n" dimensions of type "type" starting at "first"
1892 * in "bset".
1894 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1895 enum isl_dim_type type, unsigned first, unsigned n)
1897 return isl_basic_map_check_range(bset_to_bmap(bset),
1898 type, first, n);
1901 /* Insert an extra integer division, prescribed by "div", to "bmap"
1902 * at (integer division) position "pos".
1904 * The integer division is first added at the end and then moved
1905 * into the right position.
1907 __isl_give isl_basic_map *isl_basic_map_insert_div(
1908 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1910 int i, k;
1911 isl_size total;
1913 bmap = isl_basic_map_cow(bmap);
1914 total = isl_basic_map_dim(bmap, isl_dim_all);
1915 if (total < 0 || !div)
1916 return isl_basic_map_free(bmap);
1918 if (div->size != 1 + 1 + total)
1919 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1920 "unexpected size", return isl_basic_map_free(bmap));
1921 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1922 return isl_basic_map_free(bmap);
1924 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1925 k = isl_basic_map_alloc_div(bmap);
1926 if (k < 0)
1927 return isl_basic_map_free(bmap);
1928 isl_seq_cpy(bmap->div[k], div->el, div->size);
1929 isl_int_set_si(bmap->div[k][div->size], 0);
1931 for (i = k; i > pos; --i)
1932 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1934 return bmap;
1937 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1939 if (!bmap)
1940 return isl_stat_error;
1941 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1942 bmap->n_div -= n;
1943 return isl_stat_ok;
1946 static __isl_give isl_basic_map *add_constraints(
1947 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1948 unsigned i_pos, unsigned o_pos)
1950 isl_size total, n_param, n_in, n_out, n_div;
1951 unsigned o_in, o_out;
1952 isl_ctx *ctx;
1953 isl_space *space;
1954 struct isl_dim_map *dim_map;
1956 space = isl_basic_map_peek_space(bmap2);
1957 if (!bmap1 || !space)
1958 goto error;
1960 total = isl_basic_map_dim(bmap1, isl_dim_all);
1961 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1962 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1963 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1964 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1965 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1966 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1967 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1968 goto error;
1969 ctx = isl_basic_map_get_ctx(bmap1);
1970 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1971 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1972 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1974 isl_dim_map_div(dim_map, bmap2, total);
1976 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1977 error:
1978 isl_basic_map_free(bmap1);
1979 isl_basic_map_free(bmap2);
1980 return NULL;
1983 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1984 unsigned extra, unsigned n_eq, unsigned n_ineq)
1986 isl_space *space;
1987 struct isl_basic_map *ext;
1988 unsigned flags;
1989 int dims_ok;
1991 if (!base)
1992 goto error;
1994 dims_ok = base->extra >= base->n_div + extra;
1996 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1997 room_for_ineq(base, n_ineq))
1998 return base;
2000 extra += base->extra;
2001 n_eq += base->n_eq;
2002 n_ineq += base->n_ineq;
2004 space = isl_basic_map_get_space(base);
2005 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2006 if (!ext)
2007 goto error;
2009 if (dims_ok)
2010 ext->sample = isl_vec_copy(base->sample);
2011 flags = base->flags;
2012 ext = add_constraints(ext, base, 0, 0);
2013 if (ext) {
2014 ext->flags = flags;
2015 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2018 return ext;
2020 error:
2021 isl_basic_map_free(base);
2022 return NULL;
2025 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2026 unsigned extra, unsigned n_eq, unsigned n_ineq)
2028 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2029 extra, n_eq, n_ineq));
2032 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2033 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2035 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2038 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2039 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2041 isl_basic_map *bmap = bset_to_bmap(base);
2042 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2043 return bset_from_bmap(bmap);
2046 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2048 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2051 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2053 if (!bmap)
2054 return NULL;
2056 if (bmap->ref > 1) {
2057 bmap->ref--;
2058 bmap = isl_basic_map_dup(bmap);
2060 if (bmap) {
2061 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2062 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2064 return bmap;
2067 /* Clear all cached information in "map", either because it is about
2068 * to be modified or because it is being freed.
2069 * Always return the same pointer that is passed in.
2070 * This is needed for the use in isl_map_free.
2072 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2074 isl_basic_map_free(map->cached_simple_hull[0]);
2075 isl_basic_map_free(map->cached_simple_hull[1]);
2076 map->cached_simple_hull[0] = NULL;
2077 map->cached_simple_hull[1] = NULL;
2078 return map;
2081 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2083 return isl_map_cow(set);
2086 /* Return an isl_map that is equal to "map" and that has only
2087 * a single reference.
2089 * If the original input already has only one reference, then
2090 * simply return it, but clear all cached information, since
2091 * it may be rendered invalid by the operations that will be
2092 * performed on the result.
2094 * Otherwise, create a duplicate (without any cached information).
2096 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2098 if (!map)
2099 return NULL;
2101 if (map->ref == 1)
2102 return clear_caches(map);
2103 map->ref--;
2104 return isl_map_dup(map);
2107 static void swap_vars(struct isl_blk blk, isl_int *a,
2108 unsigned a_len, unsigned b_len)
2110 isl_seq_cpy(blk.data, a+a_len, b_len);
2111 isl_seq_cpy(blk.data+b_len, a, a_len);
2112 isl_seq_cpy(a, blk.data, b_len+a_len);
2115 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2116 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2118 int i;
2119 struct isl_blk blk;
2121 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2122 goto error;
2124 if (n1 == 0 || n2 == 0)
2125 return bmap;
2127 bmap = isl_basic_map_cow(bmap);
2128 if (!bmap)
2129 return NULL;
2131 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2132 if (isl_blk_is_error(blk))
2133 goto error;
2135 for (i = 0; i < bmap->n_eq; ++i)
2136 swap_vars(blk,
2137 bmap->eq[i] + pos, n1, n2);
2139 for (i = 0; i < bmap->n_ineq; ++i)
2140 swap_vars(blk,
2141 bmap->ineq[i] + pos, n1, n2);
2143 for (i = 0; i < bmap->n_div; ++i)
2144 swap_vars(blk,
2145 bmap->div[i]+1 + pos, n1, n2);
2147 isl_blk_free(bmap->ctx, blk);
2149 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2150 bmap = isl_basic_map_gauss(bmap, NULL);
2151 return isl_basic_map_finalize(bmap);
2152 error:
2153 isl_basic_map_free(bmap);
2154 return NULL;
2157 /* The given basic map has turned out to be empty.
2158 * Explicitly mark it as such and change the representation
2159 * to a canonical representation of the empty basic map.
2160 * Since the basic map has conflicting constraints,
2161 * it must have at least one constraint, except perhaps
2162 * if it was already explicitly marked as being empty.
2163 * Do nothing in the latter case, i.e., if it has been marked empty and
2164 * has no constraints.
2166 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2167 __isl_take isl_basic_map *bmap)
2169 int i = 0;
2170 isl_bool empty;
2171 isl_size n;
2172 isl_size total;
2174 n = isl_basic_map_n_constraint(bmap);
2175 empty = isl_basic_map_plain_is_empty(bmap);
2176 if (n < 0 || empty < 0)
2177 return isl_basic_map_free(bmap);
2178 if (n == 0 && empty)
2179 return bmap;
2180 total = isl_basic_map_dim(bmap, isl_dim_all);
2181 if (total < 0)
2182 return isl_basic_map_free(bmap);
2183 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2184 return isl_basic_map_free(bmap);
2185 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2186 if (!bmap)
2187 return NULL;
2188 if (bmap->n_eq > 0) {
2189 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2190 if (!bmap)
2191 return NULL;
2192 } else {
2193 i = isl_basic_map_alloc_equality(bmap);
2194 if (i < 0)
2195 goto error;
2197 isl_int_set_si(bmap->eq[i][0], 1);
2198 isl_seq_clr(bmap->eq[i]+1, total);
2199 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2200 isl_vec_free(bmap->sample);
2201 bmap->sample = NULL;
2202 return isl_basic_map_finalize(bmap);
2203 error:
2204 isl_basic_map_free(bmap);
2205 return NULL;
2208 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2209 __isl_take isl_basic_set *bset)
2211 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2214 __isl_give isl_basic_map *isl_basic_map_set_rational(
2215 __isl_take isl_basic_map *bmap)
2217 if (!bmap)
2218 return NULL;
2220 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2221 return bmap;
2223 bmap = isl_basic_map_cow(bmap);
2224 if (!bmap)
2225 return NULL;
2227 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2229 return isl_basic_map_finalize(bmap);
2232 __isl_give isl_basic_set *isl_basic_set_set_rational(
2233 __isl_take isl_basic_set *bset)
2235 return isl_basic_map_set_rational(bset);
2238 __isl_give isl_basic_set *isl_basic_set_set_integral(
2239 __isl_take isl_basic_set *bset)
2241 if (!bset)
2242 return NULL;
2244 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2245 return bset;
2247 bset = isl_basic_set_cow(bset);
2248 if (!bset)
2249 return NULL;
2251 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2253 return isl_basic_set_finalize(bset);
2256 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2258 int i;
2260 map = isl_map_cow(map);
2261 if (!map)
2262 return NULL;
2263 for (i = 0; i < map->n; ++i) {
2264 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2265 if (!map->p[i])
2266 goto error;
2268 return map;
2269 error:
2270 isl_map_free(map);
2271 return NULL;
2274 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2276 return isl_map_set_rational(set);
2279 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2280 * of "bmap").
2282 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2284 isl_int *t = bmap->div[a];
2285 bmap->div[a] = bmap->div[b];
2286 bmap->div[b] = t;
2289 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2290 * div definitions accordingly.
2292 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2293 int a, int b)
2295 int i;
2296 isl_size off;
2298 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2299 if (off < 0)
2300 return isl_basic_map_free(bmap);
2302 swap_div(bmap, a, b);
2304 for (i = 0; i < bmap->n_eq; ++i)
2305 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2307 for (i = 0; i < bmap->n_ineq; ++i)
2308 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2310 for (i = 0; i < bmap->n_div; ++i)
2311 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2312 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2314 return bmap;
2317 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2319 isl_seq_cpy(c, c + n, rem);
2320 isl_seq_clr(c + rem, n);
2323 /* Drop n dimensions starting at first.
2325 * In principle, this frees up some extra variables as the number
2326 * of columns remains constant, but we would have to extend
2327 * the div array too as the number of rows in this array is assumed
2328 * to be equal to extra.
2330 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2331 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2333 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2336 /* Move "n" divs starting at "first" to the end of the list of divs.
2338 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2339 unsigned first, unsigned n)
2341 isl_int **div;
2342 int i;
2344 if (first + n == bmap->n_div)
2345 return bmap;
2347 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2348 if (!div)
2349 goto error;
2350 for (i = 0; i < n; ++i)
2351 div[i] = bmap->div[first + i];
2352 for (i = 0; i < bmap->n_div - first - n; ++i)
2353 bmap->div[first + i] = bmap->div[first + n + i];
2354 for (i = 0; i < n; ++i)
2355 bmap->div[bmap->n_div - n + i] = div[i];
2356 free(div);
2357 return bmap;
2358 error:
2359 isl_basic_map_free(bmap);
2360 return NULL;
2363 #undef TYPE
2364 #define TYPE isl_map
2365 static
2366 #include "check_type_range_templ.c"
2368 /* Check that there are "n" dimensions of type "type" starting at "first"
2369 * in "set".
2371 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2372 enum isl_dim_type type, unsigned first, unsigned n)
2374 return isl_map_check_range(set_to_map(set), type, first, n);
2377 /* Drop "n" dimensions of type "type" starting at "first".
2378 * Perform the core computation, without cowing or
2379 * simplifying and finalizing the result.
2381 * In principle, this frees up some extra variables as the number
2382 * of columns remains constant, but we would have to extend
2383 * the div array too as the number of rows in this array is assumed
2384 * to be equal to extra.
2386 __isl_give isl_basic_map *isl_basic_map_drop_core(
2387 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2388 unsigned first, unsigned n)
2390 int i;
2391 unsigned offset;
2392 unsigned left;
2393 isl_size total;
2395 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2396 return isl_basic_map_free(bmap);
2398 total = isl_basic_map_dim(bmap, isl_dim_all);
2399 if (total < 0)
2400 return isl_basic_map_free(bmap);
2402 offset = isl_basic_map_offset(bmap, type) + first;
2403 left = total - (offset - 1) - n;
2404 for (i = 0; i < bmap->n_eq; ++i)
2405 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2407 for (i = 0; i < bmap->n_ineq; ++i)
2408 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2410 for (i = 0; i < bmap->n_div; ++i)
2411 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2413 if (type == isl_dim_div) {
2414 bmap = move_divs_last(bmap, first, n);
2415 if (!bmap)
2416 return NULL;
2417 if (isl_basic_map_free_div(bmap, n) < 0)
2418 return isl_basic_map_free(bmap);
2419 } else
2420 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2421 if (!bmap->dim)
2422 return isl_basic_map_free(bmap);
2424 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2425 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2426 return bmap;
2429 /* Drop "n" dimensions of type "type" starting at "first".
2431 * In principle, this frees up some extra variables as the number
2432 * of columns remains constant, but we would have to extend
2433 * the div array too as the number of rows in this array is assumed
2434 * to be equal to extra.
2436 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2437 enum isl_dim_type type, unsigned first, unsigned n)
2439 if (!bmap)
2440 return NULL;
2441 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2442 return bmap;
2444 bmap = isl_basic_map_cow(bmap);
2445 if (!bmap)
2446 return NULL;
2448 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2450 bmap = isl_basic_map_simplify(bmap);
2451 return isl_basic_map_finalize(bmap);
2454 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2455 enum isl_dim_type type, unsigned first, unsigned n)
2457 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2458 type, first, n));
2461 /* No longer consider "map" to be normalized.
2463 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2465 if (!map)
2466 return NULL;
2467 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2468 return map;
2471 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 int i;
2475 isl_space *space;
2477 if (isl_map_check_range(map, type, first, n) < 0)
2478 return isl_map_free(map);
2480 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2481 return map;
2482 map = isl_map_cow(map);
2483 if (!map)
2484 goto error;
2486 for (i = 0; i < map->n; ++i) {
2487 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2488 if (!map->p[i])
2489 goto error;
2491 map = isl_map_unmark_normalized(map);
2493 space = isl_map_take_space(map);
2494 space = isl_space_drop_dims(space, type, first, n);
2495 map = isl_map_restore_space(map, space);
2497 return map;
2498 error:
2499 isl_map_free(map);
2500 return NULL;
2503 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2504 enum isl_dim_type type, unsigned first, unsigned n)
2506 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2509 /* Drop the integer division at position "div", which is assumed
2510 * not to appear in any of the constraints or
2511 * in any of the other integer divisions.
2513 * Since the integer division is redundant, there is no need to cow.
2515 __isl_give isl_basic_map *isl_basic_map_drop_div(
2516 __isl_take isl_basic_map *bmap, unsigned div)
2518 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2521 /* Eliminate the specified n dimensions starting at first from the
2522 * constraints, without removing the dimensions from the space.
2523 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2525 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2526 enum isl_dim_type type, unsigned first, unsigned n)
2528 int i;
2530 if (n == 0)
2531 return map;
2533 if (isl_map_check_range(map, type, first, n) < 0)
2534 return isl_map_free(map);
2536 map = isl_map_cow(map);
2537 if (!map)
2538 return NULL;
2540 for (i = 0; i < map->n; ++i) {
2541 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2542 if (!map->p[i])
2543 goto error;
2545 return map;
2546 error:
2547 isl_map_free(map);
2548 return NULL;
2551 /* Eliminate the specified n dimensions starting at first from the
2552 * constraints, without removing the dimensions from the space.
2553 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2555 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2556 enum isl_dim_type type, unsigned first, unsigned n)
2558 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2561 /* Eliminate the specified n dimensions starting at first from the
2562 * constraints, without removing the dimensions from the space.
2563 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2565 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2566 unsigned first, unsigned n)
2568 return isl_set_eliminate(set, isl_dim_set, first, n);
2571 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2572 __isl_take isl_basic_map *bmap)
2574 isl_size v_div;
2576 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2577 if (v_div < 0)
2578 return isl_basic_map_free(bmap);
2579 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2580 if (!bmap)
2581 return NULL;
2582 bmap->n_div = 0;
2583 return isl_basic_map_finalize(bmap);
2586 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2587 __isl_take isl_basic_set *bset)
2589 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2592 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2594 int i;
2596 if (!map)
2597 return NULL;
2598 if (map->n == 0)
2599 return map;
2601 map = isl_map_cow(map);
2602 if (!map)
2603 return NULL;
2605 for (i = 0; i < map->n; ++i) {
2606 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2607 if (!map->p[i])
2608 goto error;
2610 return map;
2611 error:
2612 isl_map_free(map);
2613 return NULL;
2616 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2618 return isl_map_remove_divs(set);
2621 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2622 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2623 unsigned first, unsigned n)
2625 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2626 return isl_basic_map_free(bmap);
2627 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2628 return bmap;
2629 bmap = isl_basic_map_eliminate_vars(bmap,
2630 isl_basic_map_offset(bmap, type) - 1 + first, n);
2631 if (!bmap)
2632 return bmap;
2633 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2634 return bmap;
2635 bmap = isl_basic_map_drop(bmap, type, first, n);
2636 return bmap;
2639 /* Return true if the definition of the given div (recursively) involves
2640 * any of the given variables.
2642 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2643 unsigned first, unsigned n)
2645 int i;
2646 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2648 if (isl_int_is_zero(bmap->div[div][0]))
2649 return isl_bool_false;
2650 if (isl_seq_first_non_zero(bmap->div[div] + 1 + 1 + first, n) >= 0)
2651 return isl_bool_true;
2653 for (i = bmap->n_div - 1; i >= 0; --i) {
2654 isl_bool involves;
2656 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2657 continue;
2658 involves = div_involves_vars(bmap, i, first, n);
2659 if (involves < 0 || involves)
2660 return involves;
2663 return isl_bool_false;
2666 /* Try and add a lower and/or upper bound on "div" to "bmap"
2667 * based on inequality "i".
2668 * "total" is the total number of variables (excluding the divs).
2669 * "v" is a temporary object that can be used during the calculations.
2670 * If "lb" is set, then a lower bound should be constructed.
2671 * If "ub" is set, then an upper bound should be constructed.
2673 * The calling function has already checked that the inequality does not
2674 * reference "div", but we still need to check that the inequality is
2675 * of the right form. We'll consider the case where we want to construct
2676 * a lower bound. The construction of upper bounds is similar.
2678 * Let "div" be of the form
2680 * q = floor((a + f(x))/d)
2682 * We essentially check if constraint "i" is of the form
2684 * b + f(x) >= 0
2686 * so that we can use it to derive a lower bound on "div".
2687 * However, we allow a slightly more general form
2689 * b + g(x) >= 0
2691 * with the condition that the coefficients of g(x) - f(x) are all
2692 * divisible by d.
2693 * Rewriting this constraint as
2695 * 0 >= -b - g(x)
2697 * adding a + f(x) to both sides and dividing by d, we obtain
2699 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2701 * Taking the floor on both sides, we obtain
2703 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2705 * or
2707 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2709 * In the case of an upper bound, we construct the constraint
2711 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2714 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2715 __isl_take isl_basic_map *bmap, int div, int i,
2716 unsigned total, isl_int v, int lb, int ub)
2718 int j;
2720 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2721 if (lb) {
2722 isl_int_sub(v, bmap->ineq[i][1 + j],
2723 bmap->div[div][1 + 1 + j]);
2724 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2726 if (ub) {
2727 isl_int_add(v, bmap->ineq[i][1 + j],
2728 bmap->div[div][1 + 1 + j]);
2729 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2732 if (!lb && !ub)
2733 return bmap;
2735 bmap = isl_basic_map_cow(bmap);
2736 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2737 if (lb) {
2738 int k = isl_basic_map_alloc_inequality(bmap);
2739 if (k < 0)
2740 goto error;
2741 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2742 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2743 bmap->div[div][1 + j]);
2744 isl_int_cdiv_q(bmap->ineq[k][j],
2745 bmap->ineq[k][j], bmap->div[div][0]);
2747 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2749 if (ub) {
2750 int k = isl_basic_map_alloc_inequality(bmap);
2751 if (k < 0)
2752 goto error;
2753 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2754 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2755 bmap->div[div][1 + j]);
2756 isl_int_fdiv_q(bmap->ineq[k][j],
2757 bmap->ineq[k][j], bmap->div[div][0]);
2759 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2762 return bmap;
2763 error:
2764 isl_basic_map_free(bmap);
2765 return NULL;
2768 /* This function is called right before "div" is eliminated from "bmap"
2769 * using Fourier-Motzkin.
2770 * Look through the constraints of "bmap" for constraints on the argument
2771 * of the integer division and use them to construct constraints on the
2772 * integer division itself. These constraints can then be combined
2773 * during the Fourier-Motzkin elimination.
2774 * Note that it is only useful to introduce lower bounds on "div"
2775 * if "bmap" already contains upper bounds on "div" as the newly
2776 * introduce lower bounds can then be combined with the pre-existing
2777 * upper bounds. Similarly for upper bounds.
2778 * We therefore first check if "bmap" contains any lower and/or upper bounds
2779 * on "div".
2781 * It is interesting to note that the introduction of these constraints
2782 * can indeed lead to more accurate results, even when compared to
2783 * deriving constraints on the argument of "div" from constraints on "div".
2784 * Consider, for example, the set
2786 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2788 * The second constraint can be rewritten as
2790 * 2 * [(-i-2j+3)/4] + k >= 0
2792 * from which we can derive
2794 * -i - 2j + 3 >= -2k
2796 * or
2798 * i + 2j <= 3 + 2k
2800 * Combined with the first constraint, we obtain
2802 * -3 <= 3 + 2k or k >= -3
2804 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2805 * the first constraint, we obtain
2807 * [(i + 2j)/4] >= [-3/4] = -1
2809 * Combining this constraint with the second constraint, we obtain
2811 * k >= -2
2813 static __isl_give isl_basic_map *insert_bounds_on_div(
2814 __isl_take isl_basic_map *bmap, int div)
2816 int i;
2817 int check_lb, check_ub;
2818 isl_int v;
2819 isl_size v_div;
2821 if (!bmap)
2822 return NULL;
2824 if (isl_int_is_zero(bmap->div[div][0]))
2825 return bmap;
2827 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2828 if (v_div < 0)
2829 return isl_basic_map_free(bmap);
2831 check_lb = 0;
2832 check_ub = 0;
2833 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2834 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2835 if (s > 0)
2836 check_ub = 1;
2837 if (s < 0)
2838 check_lb = 1;
2841 if (!check_lb && !check_ub)
2842 return bmap;
2844 isl_int_init(v);
2846 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2847 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2848 continue;
2850 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2851 check_lb, check_ub);
2854 isl_int_clear(v);
2856 return bmap;
2859 /* Remove all divs (recursively) involving any of the given dimensions
2860 * in their definitions.
2862 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2863 __isl_take isl_basic_map *bmap,
2864 enum isl_dim_type type, unsigned first, unsigned n)
2866 int i;
2867 isl_size off;
2869 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2870 return isl_basic_map_free(bmap);
2871 off = isl_basic_map_var_offset(bmap, type);
2872 if (off < 0)
2873 return isl_basic_map_free(bmap);
2874 first += off;
2876 for (i = bmap->n_div - 1; i >= 0; --i) {
2877 isl_bool involves;
2879 involves = div_involves_vars(bmap, i, first, n);
2880 if (involves < 0)
2881 return isl_basic_map_free(bmap);
2882 if (!involves)
2883 continue;
2884 bmap = insert_bounds_on_div(bmap, i);
2885 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2886 if (!bmap)
2887 return NULL;
2888 i = bmap->n_div;
2891 return bmap;
2894 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2895 __isl_take isl_basic_set *bset,
2896 enum isl_dim_type type, unsigned first, unsigned n)
2898 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2901 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2902 enum isl_dim_type type, unsigned first, unsigned n)
2904 int i;
2906 if (!map)
2907 return NULL;
2908 if (map->n == 0)
2909 return map;
2911 map = isl_map_cow(map);
2912 if (!map)
2913 return NULL;
2915 for (i = 0; i < map->n; ++i) {
2916 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2917 type, first, n);
2918 if (!map->p[i])
2919 goto error;
2921 return map;
2922 error:
2923 isl_map_free(map);
2924 return NULL;
2927 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2928 enum isl_dim_type type, unsigned first, unsigned n)
2930 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2931 type, first, n));
2934 /* Does the description of "bmap" depend on the specified dimensions?
2935 * We also check whether the dimensions appear in any of the div definitions.
2936 * In principle there is no need for this check. If the dimensions appear
2937 * in a div definition, they also appear in the defining constraints of that
2938 * div.
2940 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2941 enum isl_dim_type type, unsigned first, unsigned n)
2943 int i;
2945 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2946 return isl_bool_error;
2948 first += isl_basic_map_offset(bmap, type);
2949 for (i = 0; i < bmap->n_eq; ++i)
2950 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2951 return isl_bool_true;
2952 for (i = 0; i < bmap->n_ineq; ++i)
2953 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2954 return isl_bool_true;
2955 for (i = 0; i < bmap->n_div; ++i) {
2956 if (isl_int_is_zero(bmap->div[i][0]))
2957 continue;
2958 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2959 return isl_bool_true;
2962 return isl_bool_false;
2965 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2966 enum isl_dim_type type, unsigned first, unsigned n)
2968 int i;
2970 if (isl_map_check_range(map, type, first, n) < 0)
2971 return isl_bool_error;
2973 for (i = 0; i < map->n; ++i) {
2974 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2975 type, first, n);
2976 if (involves < 0 || involves)
2977 return involves;
2980 return isl_bool_false;
2983 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2984 enum isl_dim_type type, unsigned first, unsigned n)
2986 return isl_basic_map_involves_dims(bset, type, first, n);
2989 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2990 enum isl_dim_type type, unsigned first, unsigned n)
2992 return isl_map_involves_dims(set, type, first, n);
2995 /* Does "bset" involve any local variables, i.e., integer divisions?
2997 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2999 isl_size n;
3001 n = isl_basic_set_dim(bset, isl_dim_div);
3002 if (n < 0)
3003 return isl_bool_error;
3004 return isl_bool_ok(n > 0);
3007 /* isl_set_every_basic_set callback that checks whether "bset"
3008 * is free of local variables.
3010 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3012 return isl_bool_not(isl_basic_set_involves_locals(bset));
3015 /* Does "set" involve any local variables, i.e., integer divisions?
3017 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3019 isl_bool no_locals;
3021 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3022 return isl_bool_not(no_locals);
3025 /* Drop all constraints in bmap that involve any of the dimensions
3026 * first to first+n-1.
3027 * This function only performs the actual removal of constraints.
3029 * This function should not call finalize since it is used by
3030 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3032 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3033 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3035 int i;
3037 if (n == 0)
3038 return bmap;
3040 bmap = isl_basic_map_cow(bmap);
3042 if (!bmap)
3043 return NULL;
3045 for (i = bmap->n_eq - 1; i >= 0; --i) {
3046 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3047 continue;
3048 if (isl_basic_map_drop_equality(bmap, i) < 0)
3049 return isl_basic_map_free(bmap);
3052 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3053 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3054 continue;
3055 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3056 return isl_basic_map_free(bmap);
3059 return bmap;
3062 /* Drop all constraints in bset that involve any of the dimensions
3063 * first to first+n-1.
3064 * This function only performs the actual removal of constraints.
3066 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3067 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3069 return isl_basic_map_drop_constraints_involving(bset, first, n);
3072 /* Drop all constraints in bmap that do not involve any of the dimensions
3073 * first to first + n - 1 of the given type.
3075 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3076 __isl_take isl_basic_map *bmap,
3077 enum isl_dim_type type, unsigned first, unsigned n)
3079 int i;
3081 if (n == 0) {
3082 isl_space *space = isl_basic_map_get_space(bmap);
3083 isl_basic_map_free(bmap);
3084 return isl_basic_map_universe(space);
3086 bmap = isl_basic_map_cow(bmap);
3087 if (!bmap)
3088 return NULL;
3090 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3091 return isl_basic_map_free(bmap);
3093 first += isl_basic_map_offset(bmap, type) - 1;
3095 for (i = bmap->n_eq - 1; i >= 0; --i) {
3096 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3097 continue;
3098 if (isl_basic_map_drop_equality(bmap, i) < 0)
3099 return isl_basic_map_free(bmap);
3102 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3103 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3104 continue;
3105 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3106 return isl_basic_map_free(bmap);
3109 bmap = isl_basic_map_add_known_div_constraints(bmap);
3110 return bmap;
3113 /* Drop all constraints in bset that do not involve any of the dimensions
3114 * first to first + n - 1 of the given type.
3116 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3117 __isl_take isl_basic_set *bset,
3118 enum isl_dim_type type, unsigned first, unsigned n)
3120 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3121 type, first, n);
3124 /* Drop all constraints in bmap that involve any of the dimensions
3125 * first to first + n - 1 of the given type.
3127 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3128 __isl_take isl_basic_map *bmap,
3129 enum isl_dim_type type, unsigned first, unsigned n)
3131 if (!bmap)
3132 return NULL;
3133 if (n == 0)
3134 return bmap;
3136 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3137 return isl_basic_map_free(bmap);
3139 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3140 first += isl_basic_map_offset(bmap, type) - 1;
3141 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3142 bmap = isl_basic_map_add_known_div_constraints(bmap);
3143 return bmap;
3146 /* Drop all constraints in bset that involve any of the dimensions
3147 * first to first + n - 1 of the given type.
3149 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3150 __isl_take isl_basic_set *bset,
3151 enum isl_dim_type type, unsigned first, unsigned n)
3153 return isl_basic_map_drop_constraints_involving_dims(bset,
3154 type, first, n);
3157 /* Drop constraints from "map" by applying "drop" to each basic map.
3159 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3160 enum isl_dim_type type, unsigned first, unsigned n,
3161 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3162 enum isl_dim_type type, unsigned first, unsigned n))
3164 int i;
3166 if (isl_map_check_range(map, type, first, n) < 0)
3167 return isl_map_free(map);
3169 map = isl_map_cow(map);
3170 if (!map)
3171 return NULL;
3173 for (i = 0; i < map->n; ++i) {
3174 map->p[i] = drop(map->p[i], type, first, n);
3175 if (!map->p[i])
3176 return isl_map_free(map);
3179 if (map->n > 1)
3180 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3182 return map;
3185 /* Drop all constraints in map that involve any of the dimensions
3186 * first to first + n - 1 of the given type.
3188 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3189 __isl_take isl_map *map,
3190 enum isl_dim_type type, unsigned first, unsigned n)
3192 if (n == 0)
3193 return map;
3194 return drop_constraints(map, type, first, n,
3195 &isl_basic_map_drop_constraints_involving_dims);
3198 /* Drop all constraints in "map" that do not involve any of the dimensions
3199 * first to first + n - 1 of the given type.
3201 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3202 __isl_take isl_map *map,
3203 enum isl_dim_type type, unsigned first, unsigned n)
3205 if (n == 0) {
3206 isl_space *space = isl_map_get_space(map);
3207 isl_map_free(map);
3208 return isl_map_universe(space);
3210 return drop_constraints(map, type, first, n,
3211 &isl_basic_map_drop_constraints_not_involving_dims);
3214 /* Drop all constraints in set that involve any of the dimensions
3215 * first to first + n - 1 of the given type.
3217 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3218 __isl_take isl_set *set,
3219 enum isl_dim_type type, unsigned first, unsigned n)
3221 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3224 /* Drop all constraints in "set" that do not involve any of the dimensions
3225 * first to first + n - 1 of the given type.
3227 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3228 __isl_take isl_set *set,
3229 enum isl_dim_type type, unsigned first, unsigned n)
3231 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3234 /* Does local variable "div" of "bmap" have a complete explicit representation?
3235 * Having a complete explicit representation requires not only
3236 * an explicit representation, but also that all local variables
3237 * that appear in this explicit representation in turn have
3238 * a complete explicit representation.
3240 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3242 int i;
3243 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3244 isl_bool marked;
3246 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3247 if (marked < 0 || marked)
3248 return isl_bool_not(marked);
3250 for (i = bmap->n_div - 1; i >= 0; --i) {
3251 isl_bool known;
3253 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3254 continue;
3255 known = isl_basic_map_div_is_known(bmap, i);
3256 if (known < 0 || !known)
3257 return known;
3260 return isl_bool_true;
3263 /* Remove all divs that are unknown or defined in terms of unknown divs.
3265 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3266 __isl_take isl_basic_map *bmap)
3268 int i;
3270 if (!bmap)
3271 return NULL;
3273 for (i = bmap->n_div - 1; i >= 0; --i) {
3274 if (isl_basic_map_div_is_known(bmap, i))
3275 continue;
3276 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3277 if (!bmap)
3278 return NULL;
3279 i = bmap->n_div;
3282 return bmap;
3285 /* Remove all divs that are unknown or defined in terms of unknown divs.
3287 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3288 __isl_take isl_basic_set *bset)
3290 return isl_basic_map_remove_unknown_divs(bset);
3293 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3295 int i;
3297 if (!map)
3298 return NULL;
3299 if (map->n == 0)
3300 return map;
3302 map = isl_map_cow(map);
3303 if (!map)
3304 return NULL;
3306 for (i = 0; i < map->n; ++i) {
3307 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3308 if (!map->p[i])
3309 goto error;
3311 return map;
3312 error:
3313 isl_map_free(map);
3314 return NULL;
3317 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3319 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3322 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3323 __isl_take isl_basic_set *bset,
3324 enum isl_dim_type type, unsigned first, unsigned n)
3326 isl_basic_map *bmap = bset_to_bmap(bset);
3327 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3328 return bset_from_bmap(bmap);
3331 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3332 enum isl_dim_type type, unsigned first, unsigned n)
3334 int i;
3336 if (n == 0)
3337 return map;
3339 map = isl_map_cow(map);
3340 if (isl_map_check_range(map, type, first, n) < 0)
3341 return isl_map_free(map);
3343 for (i = 0; i < map->n; ++i) {
3344 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3345 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3346 if (!map->p[i])
3347 goto error;
3349 map = isl_map_drop(map, type, first, n);
3350 return map;
3351 error:
3352 isl_map_free(map);
3353 return NULL;
3356 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3357 enum isl_dim_type type, unsigned first, unsigned n)
3359 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3360 type, first, n));
3363 /* Project out n inputs starting at first using Fourier-Motzkin */
3364 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3365 unsigned first, unsigned n)
3367 return isl_map_remove_dims(map, isl_dim_in, first, n);
3370 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3371 FILE *out, int indent)
3373 isl_printer *p;
3375 if (!bset) {
3376 fprintf(out, "null basic set\n");
3377 return;
3380 fprintf(out, "%*s", indent, "");
3381 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3382 bset->ref, bset->dim->nparam, bset->dim->n_out,
3383 bset->extra, bset->flags);
3385 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3386 p = isl_printer_set_dump(p, 1);
3387 p = isl_printer_set_indent(p, indent);
3388 p = isl_printer_start_line(p);
3389 p = isl_printer_print_basic_set(p, bset);
3390 p = isl_printer_end_line(p);
3391 isl_printer_free(p);
3394 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3395 FILE *out, int indent)
3397 isl_printer *p;
3399 if (!bmap) {
3400 fprintf(out, "null basic map\n");
3401 return;
3404 fprintf(out, "%*s", indent, "");
3405 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3406 "flags: %x, n_name: %d\n",
3407 bmap->ref,
3408 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3409 bmap->extra, bmap->flags, bmap->dim->n_id);
3411 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3412 p = isl_printer_set_dump(p, 1);
3413 p = isl_printer_set_indent(p, indent);
3414 p = isl_printer_start_line(p);
3415 p = isl_printer_print_basic_map(p, bmap);
3416 p = isl_printer_end_line(p);
3417 isl_printer_free(p);
3420 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3421 unsigned pos)
3423 isl_size total;
3425 total = isl_basic_map_dim(bmap, isl_dim_all);
3426 if (total < 0)
3427 return isl_basic_map_free(bmap);
3428 if (pos >= bmap->n_ineq)
3429 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3430 "invalid position", return isl_basic_map_free(bmap));
3431 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3432 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3433 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3434 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3435 return bmap;
3438 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3439 unsigned flags)
3441 if (isl_space_check_is_set(space) < 0)
3442 goto error;
3443 return isl_map_alloc_space(space, n, flags);
3444 error:
3445 isl_space_free(space);
3446 return NULL;
3449 /* Make sure "map" has room for at least "n" more basic maps.
3451 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3453 int i;
3454 struct isl_map *grown = NULL;
3456 if (!map)
3457 return NULL;
3458 isl_assert(map->ctx, n >= 0, goto error);
3459 if (map->n + n <= map->size)
3460 return map;
3461 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3462 if (!grown)
3463 goto error;
3464 for (i = 0; i < map->n; ++i) {
3465 grown->p[i] = isl_basic_map_copy(map->p[i]);
3466 if (!grown->p[i])
3467 goto error;
3468 grown->n++;
3470 isl_map_free(map);
3471 return grown;
3472 error:
3473 isl_map_free(grown);
3474 isl_map_free(map);
3475 return NULL;
3478 /* Make sure "set" has room for at least "n" more basic sets.
3480 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3482 return set_from_map(isl_map_grow(set_to_map(set), n));
3485 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3487 return isl_map_from_basic_map(bset);
3490 /* This function performs the same operation as isl_set_from_basic_set,
3491 * but is considered as a function on an isl_basic_set when exported.
3493 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3495 return isl_set_from_basic_set(bset);
3498 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3500 struct isl_map *map;
3502 if (!bmap)
3503 return NULL;
3505 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3506 return isl_map_add_basic_map(map, bmap);
3509 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3510 __isl_take isl_basic_set *bset)
3512 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3513 bset_to_bmap(bset)));
3516 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3518 return isl_map_free(set);
3521 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3523 int i;
3525 if (!set) {
3526 fprintf(out, "null set\n");
3527 return;
3530 fprintf(out, "%*s", indent, "");
3531 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3532 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3533 set->flags);
3534 for (i = 0; i < set->n; ++i) {
3535 fprintf(out, "%*s", indent, "");
3536 fprintf(out, "basic set %d:\n", i);
3537 isl_basic_set_print_internal(set->p[i], out, indent+4);
3541 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3543 int i;
3545 if (!map) {
3546 fprintf(out, "null map\n");
3547 return;
3550 fprintf(out, "%*s", indent, "");
3551 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3552 "flags: %x, n_name: %d\n",
3553 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3554 map->dim->n_out, map->flags, map->dim->n_id);
3555 for (i = 0; i < map->n; ++i) {
3556 fprintf(out, "%*s", indent, "");
3557 fprintf(out, "basic map %d:\n", i);
3558 isl_basic_map_print_internal(map->p[i], out, indent+4);
3562 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3564 static isl_stat isl_basic_map_check_compatible_domain(
3565 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3567 isl_bool ok;
3569 ok = isl_basic_map_compatible_domain(bmap, bset);
3570 if (ok < 0)
3571 return isl_stat_error;
3572 if (!ok)
3573 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3574 "incompatible spaces", return isl_stat_error);
3576 return isl_stat_ok;
3579 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3580 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3582 struct isl_basic_map *bmap_domain;
3583 isl_size dim;
3585 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3586 goto error;
3588 dim = isl_basic_set_dim(bset, isl_dim_set);
3589 if (dim < 0)
3590 goto error;
3591 if (dim != 0 &&
3592 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3593 goto error;
3595 bmap = isl_basic_map_cow(bmap);
3596 if (!bmap)
3597 goto error;
3598 bmap = isl_basic_map_extend(bmap,
3599 bset->n_div, bset->n_eq, bset->n_ineq);
3600 bmap_domain = isl_basic_map_from_domain(bset);
3601 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3603 bmap = isl_basic_map_simplify(bmap);
3604 return isl_basic_map_finalize(bmap);
3605 error:
3606 isl_basic_map_free(bmap);
3607 isl_basic_set_free(bset);
3608 return NULL;
3611 /* Check that the space of "bset" is the same as that of the range of "bmap".
3613 static isl_stat isl_basic_map_check_compatible_range(
3614 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3616 isl_bool ok;
3618 ok = isl_basic_map_compatible_range(bmap, bset);
3619 if (ok < 0)
3620 return isl_stat_error;
3621 if (!ok)
3622 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3623 "incompatible spaces", return isl_stat_error);
3625 return isl_stat_ok;
3628 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3629 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3631 struct isl_basic_map *bmap_range;
3632 isl_size dim;
3634 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3635 goto error;
3637 dim = isl_basic_set_dim(bset, isl_dim_set);
3638 if (dim < 0)
3639 goto error;
3640 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3641 goto error;
3643 if (isl_basic_set_plain_is_universe(bset)) {
3644 isl_basic_set_free(bset);
3645 return bmap;
3648 bmap = isl_basic_map_cow(bmap);
3649 if (!bmap)
3650 goto error;
3651 bmap = isl_basic_map_extend(bmap,
3652 bset->n_div, bset->n_eq, bset->n_ineq);
3653 bmap_range = bset_to_bmap(bset);
3654 bmap = add_constraints(bmap, bmap_range, 0, 0);
3656 bmap = isl_basic_map_simplify(bmap);
3657 return isl_basic_map_finalize(bmap);
3658 error:
3659 isl_basic_map_free(bmap);
3660 isl_basic_set_free(bset);
3661 return NULL;
3664 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3665 __isl_keep isl_vec *vec)
3667 int i;
3668 isl_size total;
3669 isl_int s;
3671 total = isl_basic_map_dim(bmap, isl_dim_all);
3672 if (total < 0 || !vec)
3673 return isl_bool_error;
3675 if (1 + total != vec->size)
3676 return isl_bool_false;
3678 isl_int_init(s);
3680 for (i = 0; i < bmap->n_eq; ++i) {
3681 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3682 if (!isl_int_is_zero(s)) {
3683 isl_int_clear(s);
3684 return isl_bool_false;
3688 for (i = 0; i < bmap->n_ineq; ++i) {
3689 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3690 if (isl_int_is_neg(s)) {
3691 isl_int_clear(s);
3692 return isl_bool_false;
3696 isl_int_clear(s);
3698 return isl_bool_true;
3701 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3702 __isl_keep isl_vec *vec)
3704 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3707 __isl_give isl_basic_map *isl_basic_map_intersect(
3708 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3710 struct isl_vec *sample = NULL;
3711 isl_space *space1, *space2;
3712 isl_size dim1, dim2, nparam1, nparam2;
3714 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3715 goto error;
3716 space1 = isl_basic_map_peek_space(bmap1);
3717 space2 = isl_basic_map_peek_space(bmap2);
3718 dim1 = isl_space_dim(space1, isl_dim_all);
3719 dim2 = isl_space_dim(space2, isl_dim_all);
3720 nparam1 = isl_space_dim(space1, isl_dim_param);
3721 nparam2 = isl_space_dim(space2, isl_dim_param);
3722 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3723 goto error;
3724 if (dim1 == nparam1 && dim2 != nparam2)
3725 return isl_basic_map_intersect(bmap2, bmap1);
3727 if (dim2 != nparam2 &&
3728 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3729 goto error;
3731 if (isl_basic_map_plain_is_empty(bmap1)) {
3732 isl_basic_map_free(bmap2);
3733 return bmap1;
3735 if (isl_basic_map_plain_is_empty(bmap2)) {
3736 isl_basic_map_free(bmap1);
3737 return bmap2;
3740 if (bmap1->sample &&
3741 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3742 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3743 sample = isl_vec_copy(bmap1->sample);
3744 else if (bmap2->sample &&
3745 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3746 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3747 sample = isl_vec_copy(bmap2->sample);
3749 bmap1 = isl_basic_map_cow(bmap1);
3750 if (!bmap1)
3751 goto error;
3752 bmap1 = isl_basic_map_extend(bmap1,
3753 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3754 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3756 if (!bmap1)
3757 isl_vec_free(sample);
3758 else if (sample) {
3759 isl_vec_free(bmap1->sample);
3760 bmap1->sample = sample;
3763 bmap1 = isl_basic_map_simplify(bmap1);
3764 return isl_basic_map_finalize(bmap1);
3765 error:
3766 if (sample)
3767 isl_vec_free(sample);
3768 isl_basic_map_free(bmap1);
3769 isl_basic_map_free(bmap2);
3770 return NULL;
3773 __isl_give isl_basic_set *isl_basic_set_intersect(
3774 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3776 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3777 bset_to_bmap(bset2)));
3780 /* Intersect the parameter domain of "bmap" with "bset".
3782 * isl_basic_map_intersect handles this as a special case.
3784 __isl_give isl_basic_map *isl_basic_map_intersect_params(
3785 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3787 return isl_basic_map_intersect(bmap, bset);
3790 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3791 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3793 isl_basic_map *bmap;
3795 bmap = bset_to_bmap(bset1);
3796 bmap = isl_basic_map_intersect_params(bmap, bset2);
3797 return bset_from_bmap(bmap);
3800 /* Does "map" consist of a single disjunct, without any local variables?
3802 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3804 isl_size n_div;
3806 if (!map)
3807 return isl_bool_error;
3808 if (map->n != 1)
3809 return isl_bool_false;
3810 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3811 if (n_div < 0)
3812 return isl_bool_error;
3813 if (n_div != 0)
3814 return isl_bool_false;
3815 return isl_bool_true;
3818 /* Check that "map" consists of a single disjunct, without any local variables.
3820 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3822 isl_bool ok;
3824 ok = is_convex_no_locals(map);
3825 if (ok < 0)
3826 return isl_stat_error;
3827 if (ok)
3828 return isl_stat_ok;
3830 isl_die(isl_map_get_ctx(map), isl_error_internal,
3831 "unexpectedly not convex or involving local variables",
3832 return isl_stat_error);
3835 /* Special case of isl_map_intersect, where both map1 and map2
3836 * are convex, without any divs and such that either map1 or map2
3837 * contains a single constraint. This constraint is then simply
3838 * added to the other map.
3840 static __isl_give isl_map *map_intersect_add_constraint(
3841 __isl_take isl_map *map1, __isl_take isl_map *map2)
3843 if (check_convex_no_locals(map1) < 0 ||
3844 check_convex_no_locals(map2) < 0)
3845 goto error;
3847 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3848 return isl_map_intersect(map2, map1);
3850 map1 = isl_map_cow(map1);
3851 if (!map1)
3852 goto error;
3853 if (isl_map_plain_is_empty(map1)) {
3854 isl_map_free(map2);
3855 return map1;
3857 if (map2->p[0]->n_eq == 1)
3858 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3859 else
3860 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3861 map2->p[0]->ineq[0]);
3863 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3864 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3865 if (!map1->p[0])
3866 goto error;
3868 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3869 isl_basic_map_free(map1->p[0]);
3870 map1->n = 0;
3873 isl_map_free(map2);
3875 map1 = isl_map_unmark_normalized(map1);
3876 return map1;
3877 error:
3878 isl_map_free(map1);
3879 isl_map_free(map2);
3880 return NULL;
3883 /* map2 may be either a parameter domain or a map living in the same
3884 * space as map1.
3886 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3887 __isl_take isl_map *map2)
3889 unsigned flags = 0;
3890 isl_bool equal;
3891 isl_map *result;
3892 int i, j;
3893 isl_size dim2, nparam2;
3895 if (!map1 || !map2)
3896 goto error;
3898 if ((isl_map_plain_is_empty(map1) ||
3899 isl_map_plain_is_universe(map2)) &&
3900 isl_space_is_equal(map1->dim, map2->dim)) {
3901 isl_map_free(map2);
3902 return map1;
3904 if ((isl_map_plain_is_empty(map2) ||
3905 isl_map_plain_is_universe(map1)) &&
3906 isl_space_is_equal(map1->dim, map2->dim)) {
3907 isl_map_free(map1);
3908 return map2;
3911 if (is_convex_no_locals(map1) == isl_bool_true &&
3912 is_convex_no_locals(map2) == isl_bool_true &&
3913 isl_space_is_equal(map1->dim, map2->dim) &&
3914 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3915 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3916 return map_intersect_add_constraint(map1, map2);
3918 equal = isl_map_plain_is_equal(map1, map2);
3919 if (equal < 0)
3920 goto error;
3921 if (equal) {
3922 isl_map_free(map2);
3923 return map1;
3926 dim2 = isl_map_dim(map2, isl_dim_all);
3927 nparam2 = isl_map_dim(map2, isl_dim_param);
3928 if (dim2 < 0 || nparam2 < 0)
3929 goto error;
3930 if (dim2 != nparam2)
3931 isl_assert(map1->ctx,
3932 isl_space_is_equal(map1->dim, map2->dim), goto error);
3934 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3935 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3936 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3938 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3939 map1->n * map2->n, flags);
3940 if (!result)
3941 goto error;
3942 for (i = 0; i < map1->n; ++i)
3943 for (j = 0; j < map2->n; ++j) {
3944 struct isl_basic_map *part;
3945 part = isl_basic_map_intersect(
3946 isl_basic_map_copy(map1->p[i]),
3947 isl_basic_map_copy(map2->p[j]));
3948 if (isl_basic_map_is_empty(part) < 0)
3949 part = isl_basic_map_free(part);
3950 result = isl_map_add_basic_map(result, part);
3951 if (!result)
3952 goto error;
3954 isl_map_free(map1);
3955 isl_map_free(map2);
3956 return result;
3957 error:
3958 isl_map_free(map1);
3959 isl_map_free(map2);
3960 return NULL;
3963 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3964 __isl_take isl_map *map2)
3966 if (isl_map_check_equal_space(map1, map2) < 0)
3967 goto error;
3968 return map_intersect_internal(map1, map2);
3969 error:
3970 isl_map_free(map1);
3971 isl_map_free(map2);
3972 return NULL;
3975 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3976 __isl_take isl_map *map2)
3978 isl_map_align_params_bin(&map1, &map2);
3979 return map_intersect(map1, map2);
3982 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3983 __isl_take isl_set *set2)
3985 return set_from_map(isl_map_intersect(set_to_map(set1),
3986 set_to_map(set2)));
3989 /* map_intersect_internal accepts intersections
3990 * with parameter domains, so we can just call that function.
3992 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3993 __isl_take isl_set *params)
3995 isl_map_align_params_set(&map, &params);
3996 return map_intersect_internal(map, params);
3999 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4000 __isl_take isl_set *params)
4002 return isl_map_intersect_params(set, params);
4005 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4007 isl_space *space;
4008 unsigned pos;
4009 isl_size n1, n2;
4011 if (!bmap)
4012 return NULL;
4013 bmap = isl_basic_map_cow(bmap);
4014 if (!bmap)
4015 return NULL;
4016 space = isl_space_reverse(isl_space_copy(bmap->dim));
4017 pos = isl_basic_map_offset(bmap, isl_dim_in);
4018 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4019 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4020 if (n1 < 0 || n2 < 0)
4021 bmap = isl_basic_map_free(bmap);
4022 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4023 return isl_basic_map_reset_space(bmap, space);
4026 /* Given a basic map where the tuple of type "type" is a wrapped map,
4027 * swap domain and range of that wrapped map.
4029 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4030 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4032 isl_space *space;
4033 isl_size offset, n1, n2;
4035 space = isl_basic_map_peek_space(bmap);
4036 offset = isl_basic_map_var_offset(bmap, type);
4037 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4038 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4039 if (offset < 0 || n1 < 0 || n2 < 0)
4040 return isl_basic_map_free(bmap);
4042 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4044 space = isl_basic_map_take_space(bmap);
4045 space = isl_space_reverse_wrapped(space, type);
4046 bmap = isl_basic_map_restore_space(bmap, space);
4048 return bmap;
4051 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4052 * (B -> A) -> C.
4054 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4055 __isl_take isl_basic_map *bmap)
4057 isl_space *space;
4059 space = isl_basic_map_peek_space(bmap);
4060 if (isl_space_check_domain_is_wrapping(space) < 0)
4061 return isl_basic_map_free(bmap);
4062 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4064 return bmap;
4067 /* Given a basic map A -> (B -> C), return the corresponding basic map
4068 * A -> (C -> B).
4070 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4071 __isl_take isl_basic_map *bmap)
4073 isl_space *space;
4075 space = isl_basic_map_peek_space(bmap);
4076 if (isl_space_check_range_is_wrapping(space) < 0)
4077 return isl_basic_map_free(bmap);
4078 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4080 return bmap;
4083 /* Given a basic map that is actually a basic set (A -> B),
4084 * return the corresponding basic set (B -> A) as a basic map.
4086 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4087 __isl_take isl_basic_map *bmap)
4089 isl_space *space;
4091 space = isl_basic_map_peek_space(bmap);
4092 if (isl_space_check_is_wrapping(space) < 0)
4093 return isl_basic_map_free(bmap);
4094 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4096 return bmap;
4099 static __isl_give isl_basic_map *basic_map_space_reset(
4100 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4102 isl_space *space;
4104 if (!bmap)
4105 return NULL;
4106 if (!isl_space_is_named_or_nested(bmap->dim, type))
4107 return bmap;
4109 space = isl_basic_map_get_space(bmap);
4110 space = isl_space_reset(space, type);
4111 bmap = isl_basic_map_reset_space(bmap, space);
4112 return bmap;
4115 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4116 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4117 unsigned pos, unsigned n)
4119 isl_bool rational, is_empty;
4120 isl_space *res_space;
4121 struct isl_basic_map *res;
4122 struct isl_dim_map *dim_map;
4123 isl_size total;
4124 unsigned off;
4125 enum isl_dim_type t;
4127 if (n == 0)
4128 return basic_map_space_reset(bmap, type);
4130 is_empty = isl_basic_map_plain_is_empty(bmap);
4131 total = isl_basic_map_dim(bmap, isl_dim_all);
4132 if (is_empty < 0 || total < 0)
4133 return isl_basic_map_free(bmap);
4134 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4135 type, pos, n);
4136 if (!res_space)
4137 return isl_basic_map_free(bmap);
4138 if (is_empty) {
4139 isl_basic_map_free(bmap);
4140 return isl_basic_map_empty(res_space);
4143 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4144 off = 0;
4145 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4146 isl_size dim;
4148 if (t != type) {
4149 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4150 } else {
4151 isl_size size = isl_basic_map_dim(bmap, t);
4152 if (size < 0)
4153 dim_map = isl_dim_map_free(dim_map);
4154 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4155 0, pos, off);
4156 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4157 pos, size - pos, off + pos + n);
4159 dim = isl_space_dim(res_space, t);
4160 if (dim < 0)
4161 dim_map = isl_dim_map_free(dim_map);
4162 off += dim;
4164 isl_dim_map_div(dim_map, bmap, off);
4166 res = isl_basic_map_alloc_space(res_space,
4167 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4168 rational = isl_basic_map_is_rational(bmap);
4169 if (rational < 0)
4170 res = isl_basic_map_free(res);
4171 if (rational)
4172 res = isl_basic_map_set_rational(res);
4173 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4174 return isl_basic_map_finalize(res);
4177 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4178 __isl_take isl_basic_set *bset,
4179 enum isl_dim_type type, unsigned pos, unsigned n)
4181 return isl_basic_map_insert_dims(bset, type, pos, n);
4184 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4185 enum isl_dim_type type, unsigned n)
4187 isl_size dim;
4189 dim = isl_basic_map_dim(bmap, type);
4190 if (dim < 0)
4191 return isl_basic_map_free(bmap);
4192 return isl_basic_map_insert_dims(bmap, type, dim, n);
4195 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4196 enum isl_dim_type type, unsigned n)
4198 if (!bset)
4199 return NULL;
4200 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4201 return isl_basic_map_add_dims(bset, type, n);
4202 error:
4203 isl_basic_set_free(bset);
4204 return NULL;
4207 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4208 enum isl_dim_type type)
4210 isl_space *space;
4212 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4213 return map;
4215 space = isl_map_get_space(map);
4216 space = isl_space_reset(space, type);
4217 map = isl_map_reset_space(map, space);
4218 return map;
4221 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4222 enum isl_dim_type type, unsigned pos, unsigned n)
4224 int i;
4225 isl_space *space;
4227 if (n == 0)
4228 return map_space_reset(map, type);
4230 map = isl_map_cow(map);
4231 if (!map)
4232 return NULL;
4234 for (i = 0; i < map->n; ++i) {
4235 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4236 if (!map->p[i])
4237 goto error;
4240 space = isl_map_take_space(map);
4241 space = isl_space_insert_dims(space, type, pos, n);
4242 map = isl_map_restore_space(map, space);
4244 return map;
4245 error:
4246 isl_map_free(map);
4247 return NULL;
4250 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4251 enum isl_dim_type type, unsigned pos, unsigned n)
4253 return isl_map_insert_dims(set, type, pos, n);
4256 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4257 enum isl_dim_type type, unsigned n)
4259 isl_size dim;
4261 dim = isl_map_dim(map, type);
4262 if (dim < 0)
4263 return isl_map_free(map);
4264 return isl_map_insert_dims(map, type, dim, n);
4267 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4268 enum isl_dim_type type, unsigned n)
4270 if (!set)
4271 return NULL;
4272 isl_assert(set->ctx, type != isl_dim_in, goto error);
4273 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4274 error:
4275 isl_set_free(set);
4276 return NULL;
4279 __isl_give isl_basic_map *isl_basic_map_move_dims(
4280 __isl_take isl_basic_map *bmap,
4281 enum isl_dim_type dst_type, unsigned dst_pos,
4282 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4284 isl_space *space;
4285 struct isl_dim_map *dim_map;
4286 struct isl_basic_map *res;
4287 enum isl_dim_type t;
4288 isl_size total;
4289 unsigned off;
4291 if (!bmap)
4292 return NULL;
4293 if (n == 0) {
4294 bmap = isl_basic_map_reset(bmap, src_type);
4295 bmap = isl_basic_map_reset(bmap, dst_type);
4296 return bmap;
4299 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4300 return isl_basic_map_free(bmap);
4302 if (dst_type == src_type && dst_pos == src_pos)
4303 return bmap;
4305 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4307 if (pos(bmap->dim, dst_type) + dst_pos ==
4308 pos(bmap->dim, src_type) + src_pos +
4309 ((src_type < dst_type) ? n : 0)) {
4310 space = isl_basic_map_take_space(bmap);
4311 space = isl_space_move_dims(space, dst_type, dst_pos,
4312 src_type, src_pos, n);
4313 bmap = isl_basic_map_restore_space(bmap, space);
4314 bmap = isl_basic_map_finalize(bmap);
4316 return bmap;
4319 total = isl_basic_map_dim(bmap, isl_dim_all);
4320 if (total < 0)
4321 return isl_basic_map_free(bmap);
4322 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4324 off = 0;
4325 space = isl_basic_map_peek_space(bmap);
4326 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4327 isl_size size = isl_space_dim(space, t);
4328 if (size < 0)
4329 dim_map = isl_dim_map_free(dim_map);
4330 if (t == dst_type) {
4331 isl_dim_map_dim_range(dim_map, space, t,
4332 0, dst_pos, off);
4333 off += dst_pos;
4334 isl_dim_map_dim_range(dim_map, space, src_type,
4335 src_pos, n, off);
4336 off += n;
4337 isl_dim_map_dim_range(dim_map, space, t,
4338 dst_pos, size - dst_pos, off);
4339 off += size - dst_pos;
4340 } else if (t == src_type) {
4341 isl_dim_map_dim_range(dim_map, space, t,
4342 0, src_pos, off);
4343 off += src_pos;
4344 isl_dim_map_dim_range(dim_map, space, t,
4345 src_pos + n, size - src_pos - n, off);
4346 off += size - src_pos - n;
4347 } else {
4348 isl_dim_map_dim(dim_map, space, t, off);
4349 off += size;
4352 isl_dim_map_div(dim_map, bmap, off);
4354 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4355 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4356 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4357 space = isl_basic_map_take_space(bmap);
4358 space = isl_space_move_dims(space, dst_type, dst_pos,
4359 src_type, src_pos, n);
4360 bmap = isl_basic_map_restore_space(bmap, space);
4361 if (!bmap)
4362 goto error;
4364 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4365 bmap = isl_basic_map_gauss(bmap, NULL);
4366 bmap = isl_basic_map_finalize(bmap);
4368 return bmap;
4369 error:
4370 isl_basic_map_free(bmap);
4371 return NULL;
4374 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4375 enum isl_dim_type dst_type, unsigned dst_pos,
4376 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4378 isl_basic_map *bmap = bset_to_bmap(bset);
4379 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4380 src_type, src_pos, n);
4381 return bset_from_bmap(bmap);
4384 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4385 enum isl_dim_type dst_type, unsigned dst_pos,
4386 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4388 if (!set)
4389 return NULL;
4390 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4391 return set_from_map(isl_map_move_dims(set_to_map(set),
4392 dst_type, dst_pos, src_type, src_pos, n));
4393 error:
4394 isl_set_free(set);
4395 return NULL;
4398 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4399 enum isl_dim_type dst_type, unsigned dst_pos,
4400 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4402 int i;
4403 isl_space *space;
4405 if (n == 0) {
4406 map = isl_map_reset(map, src_type);
4407 map = isl_map_reset(map, dst_type);
4408 return map;
4411 if (isl_map_check_range(map, src_type, src_pos, n))
4412 return isl_map_free(map);
4414 if (dst_type == src_type && dst_pos == src_pos)
4415 return map;
4417 isl_assert(map->ctx, dst_type != src_type, goto error);
4419 map = isl_map_cow(map);
4420 if (!map)
4421 return NULL;
4423 for (i = 0; i < map->n; ++i) {
4424 map->p[i] = isl_basic_map_move_dims(map->p[i],
4425 dst_type, dst_pos,
4426 src_type, src_pos, n);
4427 if (!map->p[i])
4428 goto error;
4431 space = isl_map_take_space(map);
4432 space = isl_space_move_dims(space, dst_type, dst_pos,
4433 src_type, src_pos, n);
4434 map = isl_map_restore_space(map, space);
4436 return map;
4437 error:
4438 isl_map_free(map);
4439 return NULL;
4442 /* Move the specified dimensions to the last columns right before
4443 * the divs. Don't change the dimension specification of bmap.
4444 * That's the responsibility of the caller.
4446 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4447 enum isl_dim_type type, unsigned first, unsigned n)
4449 isl_space *space;
4450 struct isl_dim_map *dim_map;
4451 struct isl_basic_map *res;
4452 enum isl_dim_type t;
4453 isl_size total;
4454 unsigned off;
4456 if (!bmap)
4457 return NULL;
4458 if (isl_basic_map_offset(bmap, type) + first + n ==
4459 isl_basic_map_offset(bmap, isl_dim_div))
4460 return bmap;
4462 total = isl_basic_map_dim(bmap, isl_dim_all);
4463 if (total < 0)
4464 return isl_basic_map_free(bmap);
4465 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4467 off = 0;
4468 space = isl_basic_map_peek_space(bmap);
4469 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4470 isl_size size = isl_space_dim(space, t);
4471 if (size < 0)
4472 dim_map = isl_dim_map_free(dim_map);
4473 if (t == type) {
4474 isl_dim_map_dim_range(dim_map, space, t,
4475 0, first, off);
4476 off += first;
4477 isl_dim_map_dim_range(dim_map, space, t,
4478 first, n, total - bmap->n_div - n);
4479 isl_dim_map_dim_range(dim_map, space, t,
4480 first + n, size - (first + n), off);
4481 off += size - (first + n);
4482 } else {
4483 isl_dim_map_dim(dim_map, space, t, off);
4484 off += size;
4487 isl_dim_map_div(dim_map, bmap, off + n);
4489 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4490 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4491 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4492 return res;
4495 /* Insert "n" rows in the divs of "bmap".
4497 * The number of columns is not changed, which means that the last
4498 * dimensions of "bmap" are being reintepreted as the new divs.
4499 * The space of "bmap" is not adjusted, however, which means
4500 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4501 * from the space of "bmap" is the responsibility of the caller.
4503 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4504 int n)
4506 int i;
4507 size_t row_size;
4508 isl_int **new_div;
4509 isl_int *old;
4511 bmap = isl_basic_map_cow(bmap);
4512 if (!bmap)
4513 return NULL;
4515 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4516 old = bmap->block2.data;
4517 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4518 (bmap->extra + n) * (1 + row_size));
4519 if (!bmap->block2.data)
4520 return isl_basic_map_free(bmap);
4521 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4522 if (!new_div)
4523 return isl_basic_map_free(bmap);
4524 for (i = 0; i < n; ++i) {
4525 new_div[i] = bmap->block2.data +
4526 (bmap->extra + i) * (1 + row_size);
4527 isl_seq_clr(new_div[i], 1 + row_size);
4529 for (i = 0; i < bmap->extra; ++i)
4530 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4531 free(bmap->div);
4532 bmap->div = new_div;
4533 bmap->n_div += n;
4534 bmap->extra += n;
4536 return bmap;
4539 /* Drop constraints from "bmap" that only involve the variables
4540 * of "type" in the range [first, first + n] that are not related
4541 * to any of the variables outside that interval.
4542 * These constraints cannot influence the values for the variables
4543 * outside the interval, except in case they cause "bmap" to be empty.
4544 * Only drop the constraints if "bmap" is known to be non-empty.
4546 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4547 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4548 unsigned first, unsigned n)
4550 int i;
4551 int *groups;
4552 isl_size dim, n_div;
4553 isl_bool non_empty;
4555 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4556 if (non_empty < 0)
4557 return isl_basic_map_free(bmap);
4558 if (!non_empty)
4559 return bmap;
4561 dim = isl_basic_map_dim(bmap, isl_dim_all);
4562 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4563 if (dim < 0 || n_div < 0)
4564 return isl_basic_map_free(bmap);
4565 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4566 if (!groups)
4567 return isl_basic_map_free(bmap);
4568 first += isl_basic_map_offset(bmap, type) - 1;
4569 for (i = 0; i < first; ++i)
4570 groups[i] = -1;
4571 for (i = first + n; i < dim - n_div; ++i)
4572 groups[i] = -1;
4574 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4576 return bmap;
4579 /* Turn the n dimensions of type type, starting at first
4580 * into existentially quantified variables.
4582 * If a subset of the projected out variables are unrelated
4583 * to any of the variables that remain, then the constraints
4584 * involving this subset are simply dropped first.
4586 __isl_give isl_basic_map *isl_basic_map_project_out(
4587 __isl_take isl_basic_map *bmap,
4588 enum isl_dim_type type, unsigned first, unsigned n)
4590 isl_bool empty;
4591 isl_space *space;
4593 if (n == 0)
4594 return basic_map_space_reset(bmap, type);
4595 if (type == isl_dim_div)
4596 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4597 "cannot project out existentially quantified variables",
4598 return isl_basic_map_free(bmap));
4600 empty = isl_basic_map_plain_is_empty(bmap);
4601 if (empty < 0)
4602 return isl_basic_map_free(bmap);
4603 if (empty)
4604 bmap = isl_basic_map_set_to_empty(bmap);
4606 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4607 if (!bmap)
4608 return NULL;
4610 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4611 return isl_basic_map_remove_dims(bmap, type, first, n);
4613 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4614 return isl_basic_map_free(bmap);
4616 bmap = move_last(bmap, type, first, n);
4617 bmap = isl_basic_map_cow(bmap);
4618 bmap = insert_div_rows(bmap, n);
4620 space = isl_basic_map_take_space(bmap);
4621 space = isl_space_drop_dims(space, type, first, n);
4622 bmap = isl_basic_map_restore_space(bmap, space);
4623 bmap = isl_basic_map_simplify(bmap);
4624 bmap = isl_basic_map_drop_redundant_divs(bmap);
4625 return isl_basic_map_finalize(bmap);
4628 /* Turn the n dimensions of type type, starting at first
4629 * into existentially quantified variables.
4631 __isl_give isl_basic_set *isl_basic_set_project_out(
4632 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4633 unsigned first, unsigned n)
4635 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4636 type, first, n));
4639 /* Turn the n dimensions of type type, starting at first
4640 * into existentially quantified variables.
4642 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4643 enum isl_dim_type type, unsigned first, unsigned n)
4645 int i;
4646 isl_space *space;
4648 if (n == 0)
4649 return map_space_reset(map, type);
4651 if (isl_map_check_range(map, type, first, n) < 0)
4652 return isl_map_free(map);
4654 map = isl_map_cow(map);
4655 if (!map)
4656 return NULL;
4658 for (i = 0; i < map->n; ++i) {
4659 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4660 if (!map->p[i])
4661 goto error;
4664 if (map->n > 1)
4665 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4666 map = isl_map_unmark_normalized(map);
4668 space = isl_map_take_space(map);
4669 space = isl_space_drop_dims(space, type, first, n);
4670 map = isl_map_restore_space(map, space);
4672 return map;
4673 error:
4674 isl_map_free(map);
4675 return NULL;
4678 #undef TYPE
4679 #define TYPE isl_map
4680 #include "isl_project_out_all_params_templ.c"
4681 #include "isl_project_out_param_templ.c"
4683 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4684 * into existentially quantified variables.
4686 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4687 enum isl_dim_type type, unsigned first, unsigned n)
4689 isl_size dim;
4691 dim = isl_map_dim(map, type);
4692 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4693 return isl_map_free(map);
4694 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4695 map = isl_map_project_out(map, type, 0, first);
4696 return map;
4699 /* Turn the n dimensions of type type, starting at first
4700 * into existentially quantified variables.
4702 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4703 enum isl_dim_type type, unsigned first, unsigned n)
4705 return set_from_map(isl_map_project_out(set_to_map(set),
4706 type, first, n));
4709 /* If "set" involves a parameter with identifier "id",
4710 * then turn it into an existentially quantified variable.
4712 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4713 __isl_take isl_id *id)
4715 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4718 /* If "set" involves any of the parameters with identifiers in "list",
4719 * then turn them into existentially quantified variables.
4721 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4722 __isl_take isl_id_list *list)
4724 isl_map *map;
4726 map = set_to_map(set);
4727 map = isl_map_project_out_param_id_list(map, list);
4728 return set_from_map(map);
4731 /* Project out all parameters from "set" by existentially quantifying
4732 * over them.
4734 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4736 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4739 /* Return a map that projects the elements in "set" onto their
4740 * "n" set dimensions starting at "first".
4741 * "type" should be equal to isl_dim_set.
4743 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4744 enum isl_dim_type type, unsigned first, unsigned n)
4746 int i;
4747 isl_map *map;
4749 if (type != isl_dim_set)
4750 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4751 "only set dimensions can be projected out", goto error);
4752 if (isl_set_check_range(set, type, first, n) < 0)
4753 return isl_set_free(set);
4755 map = isl_map_from_domain(set);
4756 map = isl_map_add_dims(map, isl_dim_out, n);
4757 for (i = 0; i < n; ++i)
4758 map = isl_map_equate(map, isl_dim_in, first + i,
4759 isl_dim_out, i);
4760 return map;
4761 error:
4762 isl_set_free(set);
4763 return NULL;
4766 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4767 unsigned n)
4769 int i, j;
4770 isl_size total;
4772 total = isl_basic_map_dim(bmap, isl_dim_all);
4773 if (total < 0)
4774 return isl_basic_map_free(bmap);
4775 for (i = 0; i < n; ++i) {
4776 j = isl_basic_map_alloc_div(bmap);
4777 if (j < 0)
4778 goto error;
4779 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4781 return bmap;
4782 error:
4783 isl_basic_map_free(bmap);
4784 return NULL;
4787 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4789 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4790 __isl_keep isl_basic_map *bmap2)
4792 isl_space *space1, *space2;
4794 space1 = isl_basic_map_peek_space(bmap1);
4795 space2 = isl_basic_map_peek_space(bmap2);
4796 return isl_space_tuple_is_equal(space1, isl_dim_out,
4797 space2, isl_dim_in);
4800 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4802 static isl_stat isl_basic_map_check_applies_range(
4803 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4805 isl_bool equal;
4807 equal = isl_basic_map_applies_range(bmap1, bmap2);
4808 if (equal < 0)
4809 return isl_stat_error;
4810 if (!equal)
4811 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4812 "spaces don't match", return isl_stat_error);
4813 return isl_stat_ok;
4816 __isl_give isl_basic_map *isl_basic_map_apply_range(
4817 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4819 isl_space *space_result = NULL;
4820 struct isl_basic_map *bmap;
4821 isl_size n_in, n_out, n, nparam;
4822 unsigned total, pos;
4823 struct isl_dim_map *dim_map1, *dim_map2;
4825 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4826 goto error;
4827 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4828 goto error;
4830 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4831 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4832 n = isl_basic_map_dim(bmap1, isl_dim_out);
4833 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4834 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4835 goto error;
4837 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4838 isl_basic_map_get_space(bmap2));
4840 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4841 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4842 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4843 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4844 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4847 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4848 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4849 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4850 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4852 bmap = isl_basic_map_alloc_space(space_result,
4853 bmap1->n_div + bmap2->n_div + n,
4854 bmap1->n_eq + bmap2->n_eq,
4855 bmap1->n_ineq + bmap2->n_ineq);
4856 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4857 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4858 bmap = add_divs(bmap, n);
4859 bmap = isl_basic_map_simplify(bmap);
4860 bmap = isl_basic_map_drop_redundant_divs(bmap);
4861 return isl_basic_map_finalize(bmap);
4862 error:
4863 isl_basic_map_free(bmap1);
4864 isl_basic_map_free(bmap2);
4865 return NULL;
4868 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4869 __isl_take isl_basic_map *bmap)
4871 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4872 goto error;
4874 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4875 bmap));
4876 error:
4877 isl_basic_set_free(bset);
4878 isl_basic_map_free(bmap);
4879 return NULL;
4882 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4883 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4885 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4886 goto error;
4887 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4888 bmap2->dim, isl_dim_in))
4889 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4890 "spaces don't match", goto error);
4892 bmap1 = isl_basic_map_reverse(bmap1);
4893 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4894 return isl_basic_map_reverse(bmap1);
4895 error:
4896 isl_basic_map_free(bmap1);
4897 isl_basic_map_free(bmap2);
4898 return NULL;
4901 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4902 * A \cap B -> f(A) + f(B)
4904 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4905 __isl_take isl_basic_map *bmap2)
4907 isl_size n_in, n_out, nparam;
4908 unsigned total, pos;
4909 struct isl_basic_map *bmap = NULL;
4910 struct isl_dim_map *dim_map1, *dim_map2;
4911 int i;
4913 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4914 goto error;
4916 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4917 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4918 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4919 if (nparam < 0 || n_in < 0 || n_out < 0)
4920 goto error;
4922 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4923 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4924 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4925 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4926 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4929 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4930 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4931 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4932 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4934 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4935 bmap1->n_div + bmap2->n_div + 2 * n_out,
4936 bmap1->n_eq + bmap2->n_eq + n_out,
4937 bmap1->n_ineq + bmap2->n_ineq);
4938 for (i = 0; i < n_out; ++i) {
4939 int j = isl_basic_map_alloc_equality(bmap);
4940 if (j < 0)
4941 goto error;
4942 isl_seq_clr(bmap->eq[j], 1+total);
4943 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4944 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4945 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4947 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4948 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4949 bmap = add_divs(bmap, 2 * n_out);
4951 bmap = isl_basic_map_simplify(bmap);
4952 return isl_basic_map_finalize(bmap);
4953 error:
4954 isl_basic_map_free(bmap);
4955 isl_basic_map_free(bmap1);
4956 isl_basic_map_free(bmap2);
4957 return NULL;
4960 /* Given two maps A -> f(A) and B -> g(B), construct a map
4961 * A \cap B -> f(A) + f(B)
4963 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4964 __isl_take isl_map *map2)
4966 struct isl_map *result;
4967 int i, j;
4969 if (isl_map_check_equal_space(map1, map2) < 0)
4970 goto error;
4972 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4973 map1->n * map2->n, 0);
4974 if (!result)
4975 goto error;
4976 for (i = 0; i < map1->n; ++i)
4977 for (j = 0; j < map2->n; ++j) {
4978 struct isl_basic_map *part;
4979 part = isl_basic_map_sum(
4980 isl_basic_map_copy(map1->p[i]),
4981 isl_basic_map_copy(map2->p[j]));
4982 if (isl_basic_map_is_empty(part))
4983 isl_basic_map_free(part);
4984 else
4985 result = isl_map_add_basic_map(result, part);
4986 if (!result)
4987 goto error;
4989 isl_map_free(map1);
4990 isl_map_free(map2);
4991 return result;
4992 error:
4993 isl_map_free(map1);
4994 isl_map_free(map2);
4995 return NULL;
4998 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4999 __isl_take isl_set *set2)
5001 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5004 /* Given a basic map A -> f(A), construct A -> -f(A).
5006 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5008 int i, j;
5009 unsigned off;
5010 isl_size n;
5012 bmap = isl_basic_map_cow(bmap);
5013 n = isl_basic_map_dim(bmap, isl_dim_out);
5014 if (n < 0)
5015 return isl_basic_map_free(bmap);
5017 off = isl_basic_map_offset(bmap, isl_dim_out);
5018 for (i = 0; i < bmap->n_eq; ++i)
5019 for (j = 0; j < n; ++j)
5020 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5021 for (i = 0; i < bmap->n_ineq; ++i)
5022 for (j = 0; j < n; ++j)
5023 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5024 for (i = 0; i < bmap->n_div; ++i)
5025 for (j = 0; j < n; ++j)
5026 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5027 bmap = isl_basic_map_gauss(bmap, NULL);
5028 return isl_basic_map_finalize(bmap);
5031 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5033 return isl_basic_map_neg(bset);
5036 /* Given a map A -> f(A), construct A -> -f(A).
5038 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5040 int i;
5042 map = isl_map_cow(map);
5043 if (!map)
5044 return NULL;
5046 for (i = 0; i < map->n; ++i) {
5047 map->p[i] = isl_basic_map_neg(map->p[i]);
5048 if (!map->p[i])
5049 goto error;
5052 return map;
5053 error:
5054 isl_map_free(map);
5055 return NULL;
5058 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5060 return set_from_map(isl_map_neg(set_to_map(set)));
5063 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5064 * A -> floor(f(A)/d).
5066 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5067 isl_int d)
5069 isl_size n_in, n_out, nparam;
5070 unsigned total, pos;
5071 struct isl_basic_map *result = NULL;
5072 struct isl_dim_map *dim_map;
5073 int i;
5075 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5076 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5077 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5078 if (nparam < 0 || n_in < 0 || n_out < 0)
5079 return isl_basic_map_free(bmap);
5081 total = nparam + n_in + n_out + bmap->n_div + n_out;
5082 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5083 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5084 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5085 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5086 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5088 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5089 bmap->n_div + n_out,
5090 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5091 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5092 result = add_divs(result, n_out);
5093 for (i = 0; i < n_out; ++i) {
5094 int j;
5095 j = isl_basic_map_alloc_inequality(result);
5096 if (j < 0)
5097 goto error;
5098 isl_seq_clr(result->ineq[j], 1+total);
5099 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5100 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5101 j = isl_basic_map_alloc_inequality(result);
5102 if (j < 0)
5103 goto error;
5104 isl_seq_clr(result->ineq[j], 1+total);
5105 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5106 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5107 isl_int_sub_ui(result->ineq[j][0], d, 1);
5110 result = isl_basic_map_simplify(result);
5111 return isl_basic_map_finalize(result);
5112 error:
5113 isl_basic_map_free(result);
5114 return NULL;
5117 /* Given a map A -> f(A) and an integer d, construct a map
5118 * A -> floor(f(A)/d).
5120 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5122 int i;
5124 map = isl_map_cow(map);
5125 if (!map)
5126 return NULL;
5128 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5129 for (i = 0; i < map->n; ++i) {
5130 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5131 if (!map->p[i])
5132 goto error;
5134 map = isl_map_unmark_normalized(map);
5136 return map;
5137 error:
5138 isl_map_free(map);
5139 return NULL;
5142 /* Given a map A -> f(A) and an integer d, construct a map
5143 * A -> floor(f(A)/d).
5145 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5146 __isl_take isl_val *d)
5148 if (!map || !d)
5149 goto error;
5150 if (!isl_val_is_int(d))
5151 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5152 "expecting integer denominator", goto error);
5153 map = isl_map_floordiv(map, d->n);
5154 isl_val_free(d);
5155 return map;
5156 error:
5157 isl_map_free(map);
5158 isl_val_free(d);
5159 return NULL;
5162 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5163 unsigned pos)
5165 int i;
5166 isl_size nparam;
5167 isl_size n_in;
5168 isl_size total;
5170 total = isl_basic_map_dim(bmap, isl_dim_all);
5171 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5172 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5173 if (total < 0 || nparam < 0 || n_in < 0)
5174 return isl_basic_map_free(bmap);
5175 i = isl_basic_map_alloc_equality(bmap);
5176 if (i < 0)
5177 goto error;
5178 isl_seq_clr(bmap->eq[i], 1 + total);
5179 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5180 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5181 return isl_basic_map_finalize(bmap);
5182 error:
5183 isl_basic_map_free(bmap);
5184 return NULL;
5187 /* Add a constraint to "bmap" expressing i_pos < o_pos
5189 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5190 unsigned pos)
5192 int i;
5193 isl_size nparam;
5194 isl_size n_in;
5195 isl_size total;
5197 total = isl_basic_map_dim(bmap, isl_dim_all);
5198 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5199 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5200 if (total < 0 || nparam < 0 || n_in < 0)
5201 return isl_basic_map_free(bmap);
5202 i = isl_basic_map_alloc_inequality(bmap);
5203 if (i < 0)
5204 goto error;
5205 isl_seq_clr(bmap->ineq[i], 1 + total);
5206 isl_int_set_si(bmap->ineq[i][0], -1);
5207 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5208 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5209 return isl_basic_map_finalize(bmap);
5210 error:
5211 isl_basic_map_free(bmap);
5212 return NULL;
5215 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5217 static __isl_give isl_basic_map *var_less_or_equal(
5218 __isl_take isl_basic_map *bmap, unsigned pos)
5220 int i;
5221 isl_size nparam;
5222 isl_size n_in;
5223 isl_size total;
5225 total = isl_basic_map_dim(bmap, isl_dim_all);
5226 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5227 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5228 if (total < 0 || nparam < 0 || n_in < 0)
5229 return isl_basic_map_free(bmap);
5230 i = isl_basic_map_alloc_inequality(bmap);
5231 if (i < 0)
5232 goto error;
5233 isl_seq_clr(bmap->ineq[i], 1 + total);
5234 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5235 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5236 return isl_basic_map_finalize(bmap);
5237 error:
5238 isl_basic_map_free(bmap);
5239 return NULL;
5242 /* Add a constraint to "bmap" expressing i_pos > o_pos
5244 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5245 unsigned pos)
5247 int i;
5248 isl_size nparam;
5249 isl_size n_in;
5250 isl_size total;
5252 total = isl_basic_map_dim(bmap, isl_dim_all);
5253 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5254 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5255 if (total < 0 || nparam < 0 || n_in < 0)
5256 return isl_basic_map_free(bmap);
5257 i = isl_basic_map_alloc_inequality(bmap);
5258 if (i < 0)
5259 goto error;
5260 isl_seq_clr(bmap->ineq[i], 1 + total);
5261 isl_int_set_si(bmap->ineq[i][0], -1);
5262 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5263 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5264 return isl_basic_map_finalize(bmap);
5265 error:
5266 isl_basic_map_free(bmap);
5267 return NULL;
5270 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5272 static __isl_give isl_basic_map *var_more_or_equal(
5273 __isl_take isl_basic_map *bmap, unsigned pos)
5275 int i;
5276 isl_size nparam;
5277 isl_size n_in;
5278 isl_size total;
5280 total = isl_basic_map_dim(bmap, isl_dim_all);
5281 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5282 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5283 if (total < 0 || nparam < 0 || n_in < 0)
5284 return isl_basic_map_free(bmap);
5285 i = isl_basic_map_alloc_inequality(bmap);
5286 if (i < 0)
5287 goto error;
5288 isl_seq_clr(bmap->ineq[i], 1 + total);
5289 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5290 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5291 return isl_basic_map_finalize(bmap);
5292 error:
5293 isl_basic_map_free(bmap);
5294 return NULL;
5297 __isl_give isl_basic_map *isl_basic_map_equal(
5298 __isl_take isl_space *space, unsigned n_equal)
5300 int i;
5301 struct isl_basic_map *bmap;
5302 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5303 if (!bmap)
5304 return NULL;
5305 for (i = 0; i < n_equal && bmap; ++i)
5306 bmap = var_equal(bmap, i);
5307 return isl_basic_map_finalize(bmap);
5310 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5312 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5313 unsigned pos)
5315 int i;
5316 struct isl_basic_map *bmap;
5317 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5318 if (!bmap)
5319 return NULL;
5320 for (i = 0; i < pos && bmap; ++i)
5321 bmap = var_equal(bmap, i);
5322 if (bmap)
5323 bmap = var_less(bmap, pos);
5324 return isl_basic_map_finalize(bmap);
5327 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5329 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5330 __isl_take isl_space *space, unsigned pos)
5332 int i;
5333 isl_basic_map *bmap;
5335 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5336 for (i = 0; i < pos; ++i)
5337 bmap = var_equal(bmap, i);
5338 bmap = var_less_or_equal(bmap, pos);
5339 return isl_basic_map_finalize(bmap);
5342 /* Return a relation on "space" expressing i_pos > o_pos
5344 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5345 unsigned pos)
5347 int i;
5348 struct isl_basic_map *bmap;
5349 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5350 if (!bmap)
5351 return NULL;
5352 for (i = 0; i < pos && bmap; ++i)
5353 bmap = var_equal(bmap, i);
5354 if (bmap)
5355 bmap = var_more(bmap, pos);
5356 return isl_basic_map_finalize(bmap);
5359 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5361 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5362 __isl_take isl_space *space, unsigned pos)
5364 int i;
5365 isl_basic_map *bmap;
5367 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5368 for (i = 0; i < pos; ++i)
5369 bmap = var_equal(bmap, i);
5370 bmap = var_more_or_equal(bmap, pos);
5371 return isl_basic_map_finalize(bmap);
5374 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5375 unsigned n, int equal)
5377 struct isl_map *map;
5378 int i;
5380 if (n == 0 && equal)
5381 return isl_map_universe(space);
5383 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5385 for (i = 0; i + 1 < n; ++i)
5386 map = isl_map_add_basic_map(map,
5387 isl_basic_map_less_at(isl_space_copy(space), i));
5388 if (n > 0) {
5389 if (equal)
5390 map = isl_map_add_basic_map(map,
5391 isl_basic_map_less_or_equal_at(space, n - 1));
5392 else
5393 map = isl_map_add_basic_map(map,
5394 isl_basic_map_less_at(space, n - 1));
5395 } else
5396 isl_space_free(space);
5398 return map;
5401 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5403 if (!space)
5404 return NULL;
5405 return map_lex_lte_first(space, space->n_out, equal);
5408 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5409 unsigned n)
5411 return map_lex_lte_first(space, n, 0);
5414 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5415 unsigned n)
5417 return map_lex_lte_first(space, n, 1);
5420 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5422 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5425 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5427 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5430 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5431 unsigned n, int equal)
5433 struct isl_map *map;
5434 int i;
5436 if (n == 0 && equal)
5437 return isl_map_universe(space);
5439 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5441 for (i = 0; i + 1 < n; ++i)
5442 map = isl_map_add_basic_map(map,
5443 isl_basic_map_more_at(isl_space_copy(space), i));
5444 if (n > 0) {
5445 if (equal)
5446 map = isl_map_add_basic_map(map,
5447 isl_basic_map_more_or_equal_at(space, n - 1));
5448 else
5449 map = isl_map_add_basic_map(map,
5450 isl_basic_map_more_at(space, n - 1));
5451 } else
5452 isl_space_free(space);
5454 return map;
5457 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5459 if (!space)
5460 return NULL;
5461 return map_lex_gte_first(space, space->n_out, equal);
5464 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5465 unsigned n)
5467 return map_lex_gte_first(space, n, 0);
5470 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5471 unsigned n)
5473 return map_lex_gte_first(space, n, 1);
5476 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5478 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5481 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5483 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5486 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5487 __isl_take isl_set *set2)
5489 isl_map *map;
5490 map = isl_map_lex_le(isl_set_get_space(set1));
5491 map = isl_map_intersect_domain(map, set1);
5492 map = isl_map_intersect_range(map, set2);
5493 return map;
5496 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5497 __isl_take isl_set *set2)
5499 isl_map *map;
5500 map = isl_map_lex_lt(isl_set_get_space(set1));
5501 map = isl_map_intersect_domain(map, set1);
5502 map = isl_map_intersect_range(map, set2);
5503 return map;
5506 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5507 __isl_take isl_set *set2)
5509 isl_map *map;
5510 map = isl_map_lex_ge(isl_set_get_space(set1));
5511 map = isl_map_intersect_domain(map, set1);
5512 map = isl_map_intersect_range(map, set2);
5513 return map;
5516 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5517 __isl_take isl_set *set2)
5519 isl_map *map;
5520 map = isl_map_lex_gt(isl_set_get_space(set1));
5521 map = isl_map_intersect_domain(map, set1);
5522 map = isl_map_intersect_range(map, set2);
5523 return map;
5526 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5527 __isl_take isl_map *map2)
5529 isl_map *map;
5530 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5531 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5532 map = isl_map_apply_range(map, isl_map_reverse(map2));
5533 return map;
5536 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5537 __isl_take isl_map *map2)
5539 isl_map *map;
5540 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5541 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5542 map = isl_map_apply_range(map, isl_map_reverse(map2));
5543 return map;
5546 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5547 __isl_take isl_map *map2)
5549 isl_map *map;
5550 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5551 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5552 map = isl_map_apply_range(map, isl_map_reverse(map2));
5553 return map;
5556 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5557 __isl_take isl_map *map2)
5559 isl_map *map;
5560 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5561 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5562 map = isl_map_apply_range(map, isl_map_reverse(map2));
5563 return map;
5566 /* For the div d = floor(f/m) at position "div", add the constraint
5568 * f - m d >= 0
5570 static __isl_give isl_basic_map *add_upper_div_constraint(
5571 __isl_take isl_basic_map *bmap, unsigned div)
5573 int i;
5574 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5575 isl_size n_div;
5576 unsigned pos;
5578 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5579 if (v_div < 0 || n_div < 0)
5580 return isl_basic_map_free(bmap);
5581 pos = v_div + div;
5582 i = isl_basic_map_alloc_inequality(bmap);
5583 if (i < 0)
5584 return isl_basic_map_free(bmap);
5585 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5586 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5588 return bmap;
5591 /* For the div d = floor(f/m) at position "div", add the constraint
5593 * -(f-(m-1)) + m d >= 0
5595 static __isl_give isl_basic_map *add_lower_div_constraint(
5596 __isl_take isl_basic_map *bmap, unsigned div)
5598 int i;
5599 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5600 isl_size n_div;
5601 unsigned pos;
5603 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5604 if (v_div < 0 || n_div < 0)
5605 return isl_basic_map_free(bmap);
5606 pos = v_div + div;
5607 i = isl_basic_map_alloc_inequality(bmap);
5608 if (i < 0)
5609 return isl_basic_map_free(bmap);
5610 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5611 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5612 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5613 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5615 return bmap;
5618 /* For the div d = floor(f/m) at position "pos", add the constraints
5620 * f - m d >= 0
5621 * -(f-(m-1)) + m d >= 0
5623 * Note that the second constraint is the negation of
5625 * f - m d >= m
5627 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5628 __isl_take isl_basic_map *bmap, unsigned pos)
5630 bmap = add_upper_div_constraint(bmap, pos);
5631 bmap = add_lower_div_constraint(bmap, pos);
5632 return bmap;
5635 /* For each known div d = floor(f/m), add the constraints
5637 * f - m d >= 0
5638 * -(f-(m-1)) + m d >= 0
5640 * Remove duplicate constraints in case of some these div constraints
5641 * already appear in "bmap".
5643 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5644 __isl_take isl_basic_map *bmap)
5646 isl_size n_div;
5648 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5649 if (n_div < 0)
5650 return isl_basic_map_free(bmap);
5651 if (n_div == 0)
5652 return bmap;
5654 bmap = add_known_div_constraints(bmap);
5655 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5656 bmap = isl_basic_map_finalize(bmap);
5657 return bmap;
5660 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5662 * In particular, if this div is of the form d = floor(f/m),
5663 * then add the constraint
5665 * f - m d >= 0
5667 * if sign < 0 or the constraint
5669 * -(f-(m-1)) + m d >= 0
5671 * if sign > 0.
5673 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5674 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5676 if (sign < 0)
5677 return add_upper_div_constraint(bmap, div);
5678 else
5679 return add_lower_div_constraint(bmap, div);
5682 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5683 __isl_take isl_basic_map *bmap)
5685 isl_space *space;
5687 if (!bmap)
5688 goto error;
5689 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5690 bmap->n_div == 0 &&
5691 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5692 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5693 return bset_from_bmap(bmap);
5694 bmap = isl_basic_map_cow(bmap);
5695 if (!bmap)
5696 return NULL;
5697 space = isl_basic_map_take_space(bmap);
5698 space = isl_space_underlying(space, bmap->n_div);
5699 bmap = isl_basic_map_restore_space(bmap, space);
5700 if (!bmap)
5701 return NULL;
5702 bmap->extra -= bmap->n_div;
5703 bmap->n_div = 0;
5704 bmap = isl_basic_map_finalize(bmap);
5705 return bset_from_bmap(bmap);
5706 error:
5707 isl_basic_map_free(bmap);
5708 return NULL;
5711 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5712 __isl_take isl_basic_set *bset)
5714 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5717 /* Replace each element in "list" by the result of applying
5718 * isl_basic_map_underlying_set to the element.
5720 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5721 __isl_take isl_basic_map_list *list)
5723 int i;
5724 isl_size n;
5726 n = isl_basic_map_list_n_basic_map(list);
5727 if (n < 0)
5728 goto error;
5730 for (i = 0; i < n; ++i) {
5731 isl_basic_map *bmap;
5732 isl_basic_set *bset;
5734 bmap = isl_basic_map_list_get_basic_map(list, i);
5735 bset = isl_basic_set_underlying_set(bmap);
5736 list = isl_basic_set_list_set_basic_set(list, i, bset);
5739 return list;
5740 error:
5741 isl_basic_map_list_free(list);
5742 return NULL;
5745 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5746 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5748 struct isl_basic_map *bmap;
5749 struct isl_ctx *ctx;
5750 isl_size dim, bmap_total;
5751 unsigned total;
5752 int i;
5754 if (!bset || !like)
5755 goto error;
5756 ctx = bset->ctx;
5757 if (isl_basic_set_check_no_params(bset) < 0 ||
5758 isl_basic_set_check_no_locals(bset) < 0)
5759 goto error;
5760 dim = isl_basic_set_dim(bset, isl_dim_set);
5761 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5762 if (dim < 0 || bmap_total < 0)
5763 goto error;
5764 isl_assert(ctx, dim == bmap_total, goto error);
5765 if (like->n_div == 0) {
5766 isl_space *space = isl_basic_map_get_space(like);
5767 isl_basic_map_free(like);
5768 return isl_basic_map_reset_space(bset, space);
5770 bset = isl_basic_set_cow(bset);
5771 if (!bset)
5772 goto error;
5773 total = dim + bset->extra;
5774 bmap = bset_to_bmap(bset);
5775 isl_space_free(isl_basic_map_take_space(bmap));
5776 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5777 if (!bmap)
5778 goto error;
5779 bmap->n_div = like->n_div;
5780 bmap->extra += like->n_div;
5781 if (bmap->extra) {
5782 unsigned ltotal;
5783 isl_int **div;
5784 ltotal = total - bmap->extra + like->extra;
5785 if (ltotal > total)
5786 ltotal = total;
5787 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5788 bmap->extra * (1 + 1 + total));
5789 if (isl_blk_is_error(bmap->block2))
5790 goto error;
5791 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5792 if (!div)
5793 goto error;
5794 bmap->div = div;
5795 for (i = 0; i < bmap->extra; ++i)
5796 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5797 for (i = 0; i < like->n_div; ++i) {
5798 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5799 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5801 bmap = isl_basic_map_add_known_div_constraints(bmap);
5803 isl_basic_map_free(like);
5804 bmap = isl_basic_map_simplify(bmap);
5805 bmap = isl_basic_map_finalize(bmap);
5806 return bmap;
5807 error:
5808 isl_basic_map_free(like);
5809 isl_basic_set_free(bset);
5810 return NULL;
5813 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5814 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5816 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5817 bset_to_bmap(like)));
5820 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5822 int i;
5824 map = isl_map_cow(map);
5825 if (!map)
5826 return NULL;
5827 map->dim = isl_space_cow(map->dim);
5828 if (!map->dim)
5829 goto error;
5831 for (i = 1; i < map->n; ++i)
5832 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5833 goto error);
5834 for (i = 0; i < map->n; ++i) {
5835 map->p[i] = bset_to_bmap(
5836 isl_basic_map_underlying_set(map->p[i]));
5837 if (!map->p[i])
5838 goto error;
5840 if (map->n == 0)
5841 map->dim = isl_space_underlying(map->dim, 0);
5842 else {
5843 isl_space_free(map->dim);
5844 map->dim = isl_space_copy(map->p[0]->dim);
5846 if (!map->dim)
5847 goto error;
5848 return set_from_map(map);
5849 error:
5850 isl_map_free(map);
5851 return NULL;
5854 /* Replace the space of "bmap" by "space".
5856 * If the space of "bmap" is identical to "space" (including the identifiers
5857 * of the input and output dimensions), then simply return the original input.
5859 __isl_give isl_basic_map *isl_basic_map_reset_space(
5860 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5862 isl_bool equal;
5863 isl_space *bmap_space;
5865 bmap_space = isl_basic_map_peek_space(bmap);
5866 equal = isl_space_is_equal(bmap_space, space);
5867 if (equal >= 0 && equal)
5868 equal = isl_space_has_equal_ids(bmap_space, space);
5869 if (equal < 0)
5870 goto error;
5871 if (equal) {
5872 isl_space_free(space);
5873 return bmap;
5875 isl_space_free(isl_basic_map_take_space(bmap));
5876 bmap = isl_basic_map_restore_space(bmap, space);
5878 bmap = isl_basic_map_finalize(bmap);
5880 return bmap;
5881 error:
5882 isl_basic_map_free(bmap);
5883 isl_space_free(space);
5884 return NULL;
5887 __isl_give isl_basic_set *isl_basic_set_reset_space(
5888 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5890 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5891 space));
5894 /* Check that the total dimensions of "map" and "space" are the same.
5896 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5897 __isl_keep isl_space *space)
5899 isl_size dim1, dim2;
5901 dim1 = isl_map_dim(map, isl_dim_all);
5902 dim2 = isl_space_dim(space, isl_dim_all);
5903 if (dim1 < 0 || dim2 < 0)
5904 return isl_stat_error;
5905 if (dim1 == dim2)
5906 return isl_stat_ok;
5907 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5908 "total dimensions do not match", return isl_stat_error);
5911 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5912 __isl_take isl_space *space)
5914 int i;
5916 map = isl_map_cow(map);
5917 if (!map || !space)
5918 goto error;
5920 for (i = 0; i < map->n; ++i) {
5921 map->p[i] = isl_basic_map_reset_space(map->p[i],
5922 isl_space_copy(space));
5923 if (!map->p[i])
5924 goto error;
5926 isl_space_free(isl_map_take_space(map));
5927 map = isl_map_restore_space(map, space);
5929 return map;
5930 error:
5931 isl_map_free(map);
5932 isl_space_free(space);
5933 return NULL;
5936 /* Replace the space of "map" by "space", without modifying
5937 * the dimension of "map".
5939 * If the space of "map" is identical to "space" (including the identifiers
5940 * of the input and output dimensions), then simply return the original input.
5942 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5943 __isl_take isl_space *space)
5945 isl_bool equal;
5946 isl_space *map_space;
5948 map_space = isl_map_peek_space(map);
5949 equal = isl_space_is_equal(map_space, space);
5950 if (equal >= 0 && equal)
5951 equal = isl_space_has_equal_ids(map_space, space);
5952 if (equal < 0)
5953 goto error;
5954 if (equal) {
5955 isl_space_free(space);
5956 return map;
5958 if (check_map_space_equal_total_dim(map, space) < 0)
5959 goto error;
5960 return isl_map_reset_space(map, space);
5961 error:
5962 isl_map_free(map);
5963 isl_space_free(space);
5964 return NULL;
5967 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5968 __isl_take isl_space *space)
5970 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5973 /* Compute the parameter domain of the given basic set.
5975 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5977 isl_bool is_params;
5978 isl_space *space;
5979 isl_size n;
5981 is_params = isl_basic_set_is_params(bset);
5982 if (is_params < 0)
5983 return isl_basic_set_free(bset);
5984 if (is_params)
5985 return bset;
5987 n = isl_basic_set_dim(bset, isl_dim_set);
5988 if (n < 0)
5989 return isl_basic_set_free(bset);
5990 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5991 space = isl_basic_set_get_space(bset);
5992 space = isl_space_params(space);
5993 bset = isl_basic_set_reset_space(bset, space);
5994 return bset;
5997 /* Construct a zero-dimensional basic set with the given parameter domain.
5999 __isl_give isl_basic_set *isl_basic_set_from_params(
6000 __isl_take isl_basic_set *bset)
6002 isl_space *space;
6003 space = isl_basic_set_get_space(bset);
6004 space = isl_space_set_from_params(space);
6005 bset = isl_basic_set_reset_space(bset, space);
6006 return bset;
6009 /* Compute the parameter domain of the given set.
6011 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6013 return isl_map_params(set_to_map(set));
6016 /* Construct a zero-dimensional set with the given parameter domain.
6018 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6020 isl_space *space;
6021 space = isl_set_get_space(set);
6022 space = isl_space_set_from_params(space);
6023 set = isl_set_reset_space(set, space);
6024 return set;
6027 /* Compute the parameter domain of the given map.
6029 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6031 isl_space *space;
6032 isl_size n_in, n_out;
6034 n_in = isl_map_dim(map, isl_dim_in);
6035 n_out = isl_map_dim(map, isl_dim_out);
6036 if (n_in < 0 || n_out < 0)
6037 return isl_map_free(map);
6038 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6039 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6040 space = isl_map_get_space(map);
6041 space = isl_space_params(space);
6042 map = isl_map_reset_space(map, space);
6043 return map;
6046 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6048 isl_space *space;
6049 isl_size n_out;
6051 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6052 if (n_out < 0)
6053 return isl_basic_map_free(bmap);
6054 space = isl_space_domain(isl_basic_map_get_space(bmap));
6056 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6058 return isl_basic_map_reset_space(bmap, space);
6061 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6063 if (!bmap)
6064 return isl_bool_error;
6065 return isl_space_may_be_set(bmap->dim);
6068 /* Is this basic map actually a set?
6069 * Users should never call this function. Outside of isl,
6070 * the type should indicate whether something is a set or a map.
6072 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6074 if (!bmap)
6075 return isl_bool_error;
6076 return isl_space_is_set(bmap->dim);
6079 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6081 isl_bool is_set;
6083 is_set = isl_basic_map_is_set(bmap);
6084 if (is_set < 0)
6085 goto error;
6086 if (is_set)
6087 return bmap;
6088 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6089 error:
6090 isl_basic_map_free(bmap);
6091 return NULL;
6094 __isl_give isl_basic_map *isl_basic_map_domain_map(
6095 __isl_take isl_basic_map *bmap)
6097 int i;
6098 isl_space *space;
6099 isl_basic_map *domain;
6100 isl_size nparam, n_in, n_out;
6102 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6103 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6104 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6105 if (nparam < 0 || n_in < 0 || n_out < 0)
6106 return isl_basic_map_free(bmap);
6108 space = isl_basic_map_get_space(bmap);
6109 space = isl_space_from_range(isl_space_domain(space));
6110 domain = isl_basic_map_universe(space);
6112 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6113 bmap = isl_basic_map_apply_range(bmap, domain);
6114 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6116 for (i = 0; i < n_in; ++i)
6117 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6118 isl_dim_out, i);
6120 bmap = isl_basic_map_gauss(bmap, NULL);
6121 return isl_basic_map_finalize(bmap);
6124 __isl_give isl_basic_map *isl_basic_map_range_map(
6125 __isl_take isl_basic_map *bmap)
6127 int i;
6128 isl_space *space;
6129 isl_basic_map *range;
6130 isl_size nparam, n_in, n_out;
6132 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6133 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6134 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6135 if (nparam < 0 || n_in < 0 || n_out < 0)
6136 return isl_basic_map_free(bmap);
6138 space = isl_basic_map_get_space(bmap);
6139 space = isl_space_from_range(isl_space_range(space));
6140 range = isl_basic_map_universe(space);
6142 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6143 bmap = isl_basic_map_apply_range(bmap, range);
6144 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6146 for (i = 0; i < n_out; ++i)
6147 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6148 isl_dim_out, i);
6150 bmap = isl_basic_map_gauss(bmap, NULL);
6151 return isl_basic_map_finalize(bmap);
6154 int isl_map_may_be_set(__isl_keep isl_map *map)
6156 if (!map)
6157 return -1;
6158 return isl_space_may_be_set(map->dim);
6161 /* Is this map actually a set?
6162 * Users should never call this function. Outside of isl,
6163 * the type should indicate whether something is a set or a map.
6165 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6167 if (!map)
6168 return isl_bool_error;
6169 return isl_space_is_set(map->dim);
6172 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6174 isl_space *space;
6175 isl_size n_in;
6177 n_in = isl_map_dim(map, isl_dim_in);
6178 if (n_in < 0)
6179 return set_from_map(isl_map_free(map));
6180 space = isl_space_range(isl_map_get_space(map));
6182 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6184 return set_from_map(isl_map_reset_space(map, space));
6187 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6188 * to each of its basic maps.
6190 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6191 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6192 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6194 int i;
6195 isl_space *space;
6197 map = isl_map_cow(map);
6198 if (!map)
6199 return NULL;
6201 for (i = 0; i < map->n; ++i) {
6202 map->p[i] = fn_bmap(map->p[i]);
6203 if (!map->p[i])
6204 return isl_map_free(map);
6206 map = isl_map_unmark_normalized(map);
6208 space = isl_map_take_space(map);
6209 space = fn_space(space);
6210 map = isl_map_restore_space(map, space);
6212 return map;
6215 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6217 return isl_map_transform(map, &isl_space_domain_map,
6218 &isl_basic_map_domain_map);
6221 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6223 return isl_map_transform(map, &isl_space_range_map,
6224 &isl_basic_map_range_map);
6227 /* Given a wrapped map of the form A[B -> C],
6228 * return the map A[B -> C] -> B.
6230 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6232 isl_id *id;
6233 isl_map *map;
6235 if (!set)
6236 return NULL;
6237 if (!isl_set_has_tuple_id(set))
6238 return isl_map_domain_map(isl_set_unwrap(set));
6240 id = isl_set_get_tuple_id(set);
6241 map = isl_map_domain_map(isl_set_unwrap(set));
6242 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6244 return map;
6247 __isl_give isl_basic_map *isl_basic_map_from_domain(
6248 __isl_take isl_basic_set *bset)
6250 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6253 __isl_give isl_basic_map *isl_basic_map_from_range(
6254 __isl_take isl_basic_set *bset)
6256 isl_space *space;
6257 space = isl_basic_set_get_space(bset);
6258 space = isl_space_from_range(space);
6259 bset = isl_basic_set_reset_space(bset, space);
6260 return bset_to_bmap(bset);
6263 /* Create a relation with the given set as range.
6264 * The domain of the created relation is a zero-dimensional
6265 * flat anonymous space.
6267 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6269 isl_space *space;
6270 space = isl_set_get_space(set);
6271 space = isl_space_from_range(space);
6272 set = isl_set_reset_space(set, space);
6273 return set_to_map(set);
6276 /* Create a relation with the given set as domain.
6277 * The range of the created relation is a zero-dimensional
6278 * flat anonymous space.
6280 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6282 return isl_map_reverse(isl_map_from_range(set));
6285 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6286 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6288 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6291 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6292 __isl_take isl_set *range)
6294 return isl_map_apply_range(isl_map_reverse(domain), range);
6297 /* Return a newly allocated isl_map with given space and flags and
6298 * room for "n" basic maps.
6299 * Make sure that all cached information is cleared.
6301 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6302 unsigned flags)
6304 struct isl_map *map;
6306 if (!space)
6307 return NULL;
6308 if (n < 0)
6309 isl_die(space->ctx, isl_error_internal,
6310 "negative number of basic maps", goto error);
6311 map = isl_calloc(space->ctx, struct isl_map,
6312 sizeof(struct isl_map) +
6313 (n - 1) * sizeof(struct isl_basic_map *));
6314 if (!map)
6315 goto error;
6317 map->ctx = space->ctx;
6318 isl_ctx_ref(map->ctx);
6319 map->ref = 1;
6320 map->size = n;
6321 map->n = 0;
6322 map->dim = space;
6323 map->flags = flags;
6324 return map;
6325 error:
6326 isl_space_free(space);
6327 return NULL;
6330 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6332 struct isl_basic_map *bmap;
6333 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6334 bmap = isl_basic_map_set_to_empty(bmap);
6335 return bmap;
6338 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6340 struct isl_basic_set *bset;
6341 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6342 bset = isl_basic_set_set_to_empty(bset);
6343 return bset;
6346 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6348 struct isl_basic_map *bmap;
6349 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6350 bmap = isl_basic_map_finalize(bmap);
6351 return bmap;
6354 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6356 struct isl_basic_set *bset;
6357 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6358 bset = isl_basic_set_finalize(bset);
6359 return bset;
6362 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6363 __isl_take isl_space *space)
6365 int i;
6366 isl_size total = isl_space_dim(space, isl_dim_all);
6367 isl_basic_map *bmap;
6369 if (total < 0)
6370 space = isl_space_free(space);
6371 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6372 for (i = 0; i < total; ++i) {
6373 int k = isl_basic_map_alloc_inequality(bmap);
6374 if (k < 0)
6375 goto error;
6376 isl_seq_clr(bmap->ineq[k], 1 + total);
6377 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6379 return bmap;
6380 error:
6381 isl_basic_map_free(bmap);
6382 return NULL;
6385 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6386 __isl_take isl_space *space)
6388 return isl_basic_map_nat_universe(space);
6391 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6393 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6396 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6398 return isl_map_nat_universe(space);
6401 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6403 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6406 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6408 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6411 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6413 struct isl_map *map;
6414 if (!space)
6415 return NULL;
6416 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6417 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6418 return map;
6421 /* This function performs the same operation as isl_map_universe,
6422 * but is considered as a function on an isl_space when exported.
6424 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6426 return isl_map_universe(space);
6429 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6431 struct isl_set *set;
6432 if (!space)
6433 return NULL;
6434 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6435 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6436 return set;
6439 /* This function performs the same operation as isl_set_universe,
6440 * but is considered as a function on an isl_space when exported.
6442 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6444 return isl_set_universe(space);
6447 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6449 int i;
6450 struct isl_map *dup;
6452 if (!map)
6453 return NULL;
6454 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6455 for (i = 0; i < map->n; ++i)
6456 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6457 return dup;
6460 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6461 __isl_take isl_basic_map *bmap)
6463 if (!bmap || !map)
6464 goto error;
6465 if (isl_basic_map_plain_is_empty(bmap)) {
6466 isl_basic_map_free(bmap);
6467 return map;
6469 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6470 goto error;
6471 isl_assert(map->ctx, map->n < map->size, goto error);
6472 map->p[map->n] = bmap;
6473 map->n++;
6474 map = isl_map_unmark_normalized(map);
6475 return map;
6476 error:
6477 if (map)
6478 isl_map_free(map);
6479 if (bmap)
6480 isl_basic_map_free(bmap);
6481 return NULL;
6484 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6486 int i;
6488 if (!map)
6489 return NULL;
6491 if (--map->ref > 0)
6492 return NULL;
6494 clear_caches(map);
6495 isl_ctx_deref(map->ctx);
6496 for (i = 0; i < map->n; ++i)
6497 isl_basic_map_free(map->p[i]);
6498 isl_space_free(map->dim);
6499 free(map);
6501 return NULL;
6504 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6505 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6507 int j;
6508 isl_size total;
6510 total = isl_basic_map_dim(bmap, isl_dim_all);
6511 if (total < 0)
6512 return isl_basic_map_free(bmap);
6514 bmap = isl_basic_map_cow(bmap);
6515 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6516 j = isl_basic_map_alloc_equality(bmap);
6517 if (j < 0)
6518 goto error;
6519 isl_seq_clr(bmap->eq[j] + 1, total);
6520 isl_int_set_si(bmap->eq[j][pos], -1);
6521 isl_int_set_si(bmap->eq[j][0], value);
6522 bmap = isl_basic_map_simplify(bmap);
6523 return isl_basic_map_finalize(bmap);
6524 error:
6525 isl_basic_map_free(bmap);
6526 return NULL;
6529 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6530 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6532 int j;
6533 isl_size total;
6535 total = isl_basic_map_dim(bmap, isl_dim_all);
6536 if (total < 0)
6537 return isl_basic_map_free(bmap);
6539 bmap = isl_basic_map_cow(bmap);
6540 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6541 j = isl_basic_map_alloc_equality(bmap);
6542 if (j < 0)
6543 goto error;
6544 isl_seq_clr(bmap->eq[j] + 1, total);
6545 isl_int_set_si(bmap->eq[j][pos], -1);
6546 isl_int_set(bmap->eq[j][0], value);
6547 bmap = isl_basic_map_simplify(bmap);
6548 return isl_basic_map_finalize(bmap);
6549 error:
6550 isl_basic_map_free(bmap);
6551 return NULL;
6554 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6555 enum isl_dim_type type, unsigned pos, int value)
6557 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6558 return isl_basic_map_free(bmap);
6559 return isl_basic_map_fix_pos_si(bmap,
6560 isl_basic_map_offset(bmap, type) + pos, value);
6563 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6564 enum isl_dim_type type, unsigned pos, isl_int value)
6566 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6567 return isl_basic_map_free(bmap);
6568 return isl_basic_map_fix_pos(bmap,
6569 isl_basic_map_offset(bmap, type) + pos, value);
6572 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6573 * to be equal to "v".
6575 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6576 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6578 if (!bmap || !v)
6579 goto error;
6580 if (!isl_val_is_int(v))
6581 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6582 "expecting integer value", goto error);
6583 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6584 goto error;
6585 pos += isl_basic_map_offset(bmap, type);
6586 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6587 isl_val_free(v);
6588 return bmap;
6589 error:
6590 isl_basic_map_free(bmap);
6591 isl_val_free(v);
6592 return NULL;
6595 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6596 * to be equal to "v".
6598 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6599 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6601 return isl_basic_map_fix_val(bset, type, pos, v);
6604 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6605 enum isl_dim_type type, unsigned pos, int value)
6607 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6608 type, pos, value));
6611 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6612 enum isl_dim_type type, unsigned pos, isl_int value)
6614 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6615 type, pos, value));
6618 /* Remove the basic map at position "i" from "map" if this basic map
6619 * is (obviously) empty.
6621 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6623 isl_bool empty;
6625 if (!map)
6626 return NULL;
6628 empty = isl_basic_map_plain_is_empty(map->p[i]);
6629 if (empty < 0)
6630 return isl_map_free(map);
6631 if (!empty)
6632 return map;
6634 isl_basic_map_free(map->p[i]);
6635 map->n--;
6636 if (i != map->n) {
6637 map->p[i] = map->p[map->n];
6638 map = isl_map_unmark_normalized(map);
6642 return map;
6645 /* Perform "fn" on each basic map of "map", where we may not be holding
6646 * the only reference to "map".
6647 * In particular, "fn" should be a semantics preserving operation
6648 * that we want to apply to all copies of "map". We therefore need
6649 * to be careful not to modify "map" in a way that breaks "map"
6650 * in case anything goes wrong.
6652 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6653 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6655 struct isl_basic_map *bmap;
6656 int i;
6658 if (!map)
6659 return NULL;
6661 for (i = map->n - 1; i >= 0; --i) {
6662 bmap = isl_basic_map_copy(map->p[i]);
6663 bmap = fn(bmap);
6664 if (!bmap)
6665 goto error;
6666 isl_basic_map_free(map->p[i]);
6667 map->p[i] = bmap;
6668 map = remove_if_empty(map, i);
6669 if (!map)
6670 return NULL;
6673 return map;
6674 error:
6675 isl_map_free(map);
6676 return NULL;
6679 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6680 enum isl_dim_type type, unsigned pos, int value)
6682 int i;
6684 map = isl_map_cow(map);
6685 if (isl_map_check_range(map, type, pos, 1) < 0)
6686 return isl_map_free(map);
6687 for (i = map->n - 1; i >= 0; --i) {
6688 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6689 map = remove_if_empty(map, i);
6690 if (!map)
6691 return NULL;
6693 map = isl_map_unmark_normalized(map);
6694 return map;
6697 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6698 enum isl_dim_type type, unsigned pos, int value)
6700 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6703 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6704 enum isl_dim_type type, unsigned pos, isl_int value)
6706 int i;
6708 map = isl_map_cow(map);
6709 if (isl_map_check_range(map, type, pos, 1) < 0)
6710 return isl_map_free(map);
6711 for (i = 0; i < map->n; ++i) {
6712 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6713 if (!map->p[i])
6714 goto error;
6716 map = isl_map_unmark_normalized(map);
6717 return map;
6718 error:
6719 isl_map_free(map);
6720 return NULL;
6723 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6724 enum isl_dim_type type, unsigned pos, isl_int value)
6726 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6729 /* Fix the value of the variable at position "pos" of type "type" of "map"
6730 * to be equal to "v".
6732 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6733 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6735 int i;
6737 map = isl_map_cow(map);
6738 if (!map || !v)
6739 goto error;
6741 if (!isl_val_is_int(v))
6742 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6743 "expecting integer value", goto error);
6744 if (isl_map_check_range(map, type, pos, 1) < 0)
6745 goto error;
6746 for (i = map->n - 1; i >= 0; --i) {
6747 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6748 isl_val_copy(v));
6749 map = remove_if_empty(map, i);
6750 if (!map)
6751 goto error;
6753 map = isl_map_unmark_normalized(map);
6754 isl_val_free(v);
6755 return map;
6756 error:
6757 isl_map_free(map);
6758 isl_val_free(v);
6759 return NULL;
6762 /* Fix the value of the variable at position "pos" of type "type" of "set"
6763 * to be equal to "v".
6765 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6766 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6768 return isl_map_fix_val(set, type, pos, v);
6771 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6772 unsigned input, int value)
6774 return isl_map_fix_si(map, isl_dim_in, input, value);
6777 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6778 int value)
6780 return set_from_map(isl_map_fix_si(set_to_map(set),
6781 isl_dim_set, dim, value));
6784 static __isl_give isl_basic_map *basic_map_bound_si(
6785 __isl_take isl_basic_map *bmap,
6786 enum isl_dim_type type, unsigned pos, int value, int upper)
6788 int j;
6789 isl_size total;
6791 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6792 return isl_basic_map_free(bmap);
6793 total = isl_basic_map_dim(bmap, isl_dim_all);
6794 if (total < 0)
6795 return isl_basic_map_free(bmap);
6796 pos += isl_basic_map_offset(bmap, type);
6797 bmap = isl_basic_map_cow(bmap);
6798 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6799 j = isl_basic_map_alloc_inequality(bmap);
6800 if (j < 0)
6801 goto error;
6802 isl_seq_clr(bmap->ineq[j], 1 + total);
6803 if (upper) {
6804 isl_int_set_si(bmap->ineq[j][pos], -1);
6805 isl_int_set_si(bmap->ineq[j][0], value);
6806 } else {
6807 isl_int_set_si(bmap->ineq[j][pos], 1);
6808 isl_int_set_si(bmap->ineq[j][0], -value);
6810 bmap = isl_basic_map_simplify(bmap);
6811 return isl_basic_map_finalize(bmap);
6812 error:
6813 isl_basic_map_free(bmap);
6814 return NULL;
6817 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6818 __isl_take isl_basic_map *bmap,
6819 enum isl_dim_type type, unsigned pos, int value)
6821 return basic_map_bound_si(bmap, type, pos, value, 0);
6824 /* Constrain the values of the given dimension to be no greater than "value".
6826 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6827 __isl_take isl_basic_map *bmap,
6828 enum isl_dim_type type, unsigned pos, int value)
6830 return basic_map_bound_si(bmap, type, pos, value, 1);
6833 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6834 enum isl_dim_type type, unsigned pos, int value, int upper)
6836 int i;
6838 map = isl_map_cow(map);
6839 if (isl_map_check_range(map, type, pos, 1) < 0)
6840 return isl_map_free(map);
6841 for (i = 0; i < map->n; ++i) {
6842 map->p[i] = basic_map_bound_si(map->p[i],
6843 type, pos, value, upper);
6844 if (!map->p[i])
6845 goto error;
6847 map = isl_map_unmark_normalized(map);
6848 return map;
6849 error:
6850 isl_map_free(map);
6851 return NULL;
6854 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6855 enum isl_dim_type type, unsigned pos, int value)
6857 return map_bound_si(map, type, pos, value, 0);
6860 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6861 enum isl_dim_type type, unsigned pos, int value)
6863 return map_bound_si(map, type, pos, value, 1);
6866 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6867 enum isl_dim_type type, unsigned pos, int value)
6869 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6870 type, pos, value));
6873 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6874 enum isl_dim_type type, unsigned pos, int value)
6876 return isl_map_upper_bound_si(set, type, pos, value);
6879 /* Bound the given variable of "bmap" from below (or above is "upper"
6880 * is set) to "value".
6882 static __isl_give isl_basic_map *basic_map_bound(
6883 __isl_take isl_basic_map *bmap,
6884 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6886 int j;
6887 isl_size total;
6889 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6890 return isl_basic_map_free(bmap);
6891 total = isl_basic_map_dim(bmap, isl_dim_all);
6892 if (total < 0)
6893 return isl_basic_map_free(bmap);
6894 pos += isl_basic_map_offset(bmap, type);
6895 bmap = isl_basic_map_cow(bmap);
6896 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6897 j = isl_basic_map_alloc_inequality(bmap);
6898 if (j < 0)
6899 goto error;
6900 isl_seq_clr(bmap->ineq[j], 1 + total);
6901 if (upper) {
6902 isl_int_set_si(bmap->ineq[j][pos], -1);
6903 isl_int_set(bmap->ineq[j][0], value);
6904 } else {
6905 isl_int_set_si(bmap->ineq[j][pos], 1);
6906 isl_int_neg(bmap->ineq[j][0], value);
6908 bmap = isl_basic_map_simplify(bmap);
6909 return isl_basic_map_finalize(bmap);
6910 error:
6911 isl_basic_map_free(bmap);
6912 return NULL;
6915 /* Bound the given variable of "map" from below (or above is "upper"
6916 * is set) to "value".
6918 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6919 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6921 int i;
6923 map = isl_map_cow(map);
6924 if (isl_map_check_range(map, type, pos, 1) < 0)
6925 return isl_map_free(map);
6926 for (i = map->n - 1; i >= 0; --i) {
6927 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6928 map = remove_if_empty(map, i);
6929 if (!map)
6930 return NULL;
6932 map = isl_map_unmark_normalized(map);
6933 return map;
6936 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6937 enum isl_dim_type type, unsigned pos, isl_int value)
6939 return map_bound(map, type, pos, value, 0);
6942 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6943 enum isl_dim_type type, unsigned pos, isl_int value)
6945 return map_bound(map, type, pos, value, 1);
6948 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6949 enum isl_dim_type type, unsigned pos, isl_int value)
6951 return isl_map_lower_bound(set, type, pos, value);
6954 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6955 enum isl_dim_type type, unsigned pos, isl_int value)
6957 return isl_map_upper_bound(set, type, pos, value);
6960 /* Force the values of the variable at position "pos" of type "type" of "map"
6961 * to be no smaller than "value".
6963 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6964 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6966 if (!value)
6967 goto error;
6968 if (!isl_val_is_int(value))
6969 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6970 "expecting integer value", goto error);
6971 map = isl_map_lower_bound(map, type, pos, value->n);
6972 isl_val_free(value);
6973 return map;
6974 error:
6975 isl_val_free(value);
6976 isl_map_free(map);
6977 return NULL;
6980 /* Force the values of the variable at position "pos" of type "type" of "set"
6981 * to be no smaller than "value".
6983 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6984 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6986 isl_map *map;
6988 map = set_to_map(set);
6989 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6992 /* Force the values of the variable at position "pos" of type "type" of "map"
6993 * to be no greater than "value".
6995 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6996 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6998 if (!value)
6999 goto error;
7000 if (!isl_val_is_int(value))
7001 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7002 "expecting integer value", goto error);
7003 map = isl_map_upper_bound(map, type, pos, value->n);
7004 isl_val_free(value);
7005 return map;
7006 error:
7007 isl_val_free(value);
7008 isl_map_free(map);
7009 return NULL;
7012 /* Force the values of the variable at position "pos" of type "type" of "set"
7013 * to be no greater than "value".
7015 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7016 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7018 isl_map *map;
7020 map = set_to_map(set);
7021 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7024 /* If "mv" has an explicit domain, then intersect the domain of "map"
7025 * with this explicit domain.
7027 * An isl_multi_val object never has an explicit domain,
7028 * so simply return "map".
7030 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7031 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7033 return map;
7036 #undef BASE
7037 #define BASE val
7038 #include "isl_map_bound_templ.c"
7040 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7041 * for each set dimension, by treating the set as a map.
7043 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7044 __isl_take isl_multi_val *bound,
7045 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7046 unsigned pos, __isl_take isl_val *value))
7048 isl_map *map;
7050 map = set_to_map(set);
7051 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7054 #undef BASE
7055 #define BASE pw_aff
7056 #include "isl_map_bound_templ.c"
7058 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7059 * for each set dimension, by converting the set and the bound
7060 * to objects living in a map space.
7062 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7063 __isl_take isl_multi_pw_aff *bound,
7064 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7065 unsigned pos, __isl_take TYPE *value))
7067 isl_map *map;
7069 map = isl_map_from_range(set);
7070 bound = isl_multi_pw_aff_from_range(bound);
7071 map = map_bound_multi_pw_aff(map, bound, set_bound);
7072 return isl_map_range(map);
7075 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7076 * setting a bound on the given output dimension.
7078 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7079 unsigned pos, __isl_take isl_val *v)
7081 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7084 /* Force the values of the set dimensions of "set"
7085 * to be no smaller than the corresponding values in "lower".
7087 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7088 __isl_take isl_multi_val *lower)
7090 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7093 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7094 * setting a bound on the given output dimension.
7096 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7097 unsigned pos, __isl_take isl_val *v)
7099 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7102 /* Force the values of the set dimensions of "set"
7103 * to be no greater than the corresponding values in "upper".
7105 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7106 __isl_take isl_multi_val *upper)
7108 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7111 /* Force the symbolic constant expression "bound"
7112 * to satisfy the relation "order" with respect to
7113 * the output variable at position "pos" of "map".
7115 * Create an affine expression representing the output variable
7116 * in terms of the range and
7117 * compare it using "order" to "bound" (defined on the domain).
7118 * The result is a relation between elements in domain and range that
7119 * can be intersected with "map".
7121 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7122 unsigned pos, __isl_take isl_pw_aff *bound,
7123 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7124 __isl_take isl_pw_aff *pa2))
7126 isl_space *space;
7127 isl_local_space *ls;
7128 isl_pw_aff *var;
7130 space = isl_space_range(isl_map_get_space(map));
7131 ls = isl_local_space_from_space(space);
7132 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7133 map = isl_map_intersect(map, order(bound, var));
7134 return map;
7137 /* Force the values of the output variable at position "pos" of "map"
7138 * to be no smaller than the symbolic constant expression "lower".
7140 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7141 unsigned pos, __isl_take isl_pw_aff *lower)
7143 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7146 /* Force the values of the output variable at position "pos" of "map"
7147 * to be no greater than the symbolic constant expression "upper".
7149 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7150 unsigned pos, __isl_take isl_pw_aff *upper)
7152 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7155 /* Force the values of the set dimensions of "set"
7156 * to be no smaller than the corresponding constant symbolic expressions
7157 * in "lower".
7159 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7160 __isl_take isl_multi_pw_aff *lower)
7162 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7165 /* Force the values of the set dimensions of "set"
7166 * to be no greater than the corresponding constant symbolic expressions
7167 * in "upper".
7169 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7170 __isl_take isl_multi_pw_aff *upper)
7172 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7175 /* Force the values of the output dimensions of "map"
7176 * to be no smaller than the corresponding constant symbolic expressions
7177 * in "lower".
7179 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7180 __isl_take isl_multi_pw_aff *lower)
7182 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7185 /* Force the values of the output dimensions of "map"
7186 * to be no greater than the corresponding constant symbolic expressions
7187 * in "upper".
7189 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7190 __isl_take isl_multi_pw_aff *upper)
7192 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7195 /* Bound the given variable of "bset" from below (or above is "upper"
7196 * is set) to "value".
7198 static __isl_give isl_basic_set *isl_basic_set_bound(
7199 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7200 isl_int value, int upper)
7202 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7203 type, pos, value, upper));
7206 /* Bound the given variable of "bset" from below (or above is "upper"
7207 * is set) to "value".
7209 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7210 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7211 __isl_take isl_val *value, int upper)
7213 if (!value)
7214 goto error;
7215 if (!isl_val_is_int(value))
7216 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7217 "expecting integer value", goto error);
7218 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7219 isl_val_free(value);
7220 return bset;
7221 error:
7222 isl_val_free(value);
7223 isl_basic_set_free(bset);
7224 return NULL;
7227 /* Bound the given variable of "bset" from below to "value".
7229 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7230 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7231 __isl_take isl_val *value)
7233 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7236 /* Bound the given variable of "bset" from above to "value".
7238 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7239 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7240 __isl_take isl_val *value)
7242 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7245 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7247 return isl_map_transform(map, &isl_space_reverse,
7248 &isl_basic_map_reverse);
7251 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7253 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7255 return isl_map_transform(map, &isl_space_domain_reverse,
7256 &isl_basic_map_domain_reverse);
7259 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7261 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7263 return isl_map_transform(map, &isl_space_range_reverse,
7264 &isl_basic_map_range_reverse);
7267 /* Given a set (A -> B), return the corresponding set (B -> A).
7269 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7271 isl_map *map = set_to_map(set);
7273 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7274 &isl_basic_map_set_reverse);
7275 return set_from_map(map);
7278 #undef TYPE
7279 #define TYPE isl_pw_multi_aff
7280 #undef SUFFIX
7281 #define SUFFIX _pw_multi_aff
7282 #undef EMPTY
7283 #define EMPTY isl_pw_multi_aff_empty
7284 #undef ADD
7285 #define ADD isl_pw_multi_aff_union_add
7286 #include "isl_map_lexopt_templ.c"
7288 /* Given a map "map", compute the lexicographically minimal
7289 * (or maximal) image element for each domain element in dom,
7290 * in the form of an isl_pw_multi_aff.
7291 * If "empty" is not NULL, then set *empty to those elements in dom that
7292 * do not have an image element.
7293 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7294 * should be computed over the domain of "map". "empty" is also NULL
7295 * in this case.
7297 * We first compute the lexicographically minimal or maximal element
7298 * in the first basic map. This results in a partial solution "res"
7299 * and a subset "todo" of dom that still need to be handled.
7300 * We then consider each of the remaining maps in "map" and successively
7301 * update both "res" and "todo".
7302 * If "empty" is NULL, then the todo sets are not needed and therefore
7303 * also not computed.
7305 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7306 __isl_take isl_map *map, __isl_take isl_set *dom,
7307 __isl_give isl_set **empty, unsigned flags)
7309 int i;
7310 int full;
7311 isl_pw_multi_aff *res;
7312 isl_set *todo;
7314 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7315 if (!map || (!full && !dom))
7316 goto error;
7318 if (isl_map_plain_is_empty(map)) {
7319 if (empty)
7320 *empty = dom;
7321 else
7322 isl_set_free(dom);
7323 return isl_pw_multi_aff_from_map(map);
7326 res = basic_map_partial_lexopt_pw_multi_aff(
7327 isl_basic_map_copy(map->p[0]),
7328 isl_set_copy(dom), empty, flags);
7330 if (empty)
7331 todo = *empty;
7332 for (i = 1; i < map->n; ++i) {
7333 isl_pw_multi_aff *res_i;
7335 res_i = basic_map_partial_lexopt_pw_multi_aff(
7336 isl_basic_map_copy(map->p[i]),
7337 isl_set_copy(dom), empty, flags);
7339 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7340 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7341 else
7342 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7344 if (empty)
7345 todo = isl_set_intersect(todo, *empty);
7348 isl_set_free(dom);
7349 isl_map_free(map);
7351 if (empty)
7352 *empty = todo;
7354 return res;
7355 error:
7356 if (empty)
7357 *empty = NULL;
7358 isl_set_free(dom);
7359 isl_map_free(map);
7360 return NULL;
7363 #undef TYPE
7364 #define TYPE isl_map
7365 #undef SUFFIX
7366 #define SUFFIX
7367 #undef EMPTY
7368 #define EMPTY isl_map_empty
7369 #undef ADD
7370 #define ADD isl_map_union_disjoint
7371 #include "isl_map_lexopt_templ.c"
7373 /* Given a map "map", compute the lexicographically minimal
7374 * (or maximal) image element for each domain element in "dom",
7375 * in the form of an isl_map.
7376 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7377 * do not have an image element.
7378 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7379 * should be computed over the domain of "map". "empty" is also NULL
7380 * in this case.
7382 * If the input consists of more than one disjunct, then first
7383 * compute the desired result in the form of an isl_pw_multi_aff and
7384 * then convert that into an isl_map.
7386 * This function used to have an explicit implementation in terms
7387 * of isl_maps, but it would continually intersect the domains of
7388 * partial results with the complement of the domain of the next
7389 * partial solution, potentially leading to an explosion in the number
7390 * of disjuncts if there are several disjuncts in the input.
7391 * An even earlier implementation of this function would look for
7392 * better results in the domain of the partial result and for extra
7393 * results in the complement of this domain, which would lead to
7394 * even more splintering.
7396 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7397 __isl_take isl_map *map, __isl_take isl_set *dom,
7398 __isl_give isl_set **empty, unsigned flags)
7400 int full;
7401 struct isl_map *res;
7402 isl_pw_multi_aff *pma;
7404 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7405 if (!map || (!full && !dom))
7406 goto error;
7408 if (isl_map_plain_is_empty(map)) {
7409 if (empty)
7410 *empty = dom;
7411 else
7412 isl_set_free(dom);
7413 return map;
7416 if (map->n == 1) {
7417 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7418 dom, empty, flags);
7419 isl_map_free(map);
7420 return res;
7423 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7424 flags);
7425 return isl_map_from_pw_multi_aff_internal(pma);
7426 error:
7427 if (empty)
7428 *empty = NULL;
7429 isl_set_free(dom);
7430 isl_map_free(map);
7431 return NULL;
7434 __isl_give isl_map *isl_map_partial_lexmax(
7435 __isl_take isl_map *map, __isl_take isl_set *dom,
7436 __isl_give isl_set **empty)
7438 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7441 __isl_give isl_map *isl_map_partial_lexmin(
7442 __isl_take isl_map *map, __isl_take isl_set *dom,
7443 __isl_give isl_set **empty)
7445 return isl_map_partial_lexopt(map, dom, empty, 0);
7448 __isl_give isl_set *isl_set_partial_lexmin(
7449 __isl_take isl_set *set, __isl_take isl_set *dom,
7450 __isl_give isl_set **empty)
7452 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7453 dom, empty));
7456 __isl_give isl_set *isl_set_partial_lexmax(
7457 __isl_take isl_set *set, __isl_take isl_set *dom,
7458 __isl_give isl_set **empty)
7460 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7461 dom, empty));
7464 /* Compute the lexicographic minimum (or maximum if "flags" includes
7465 * ISL_OPT_MAX) of "bset" over its parametric domain.
7467 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7468 unsigned flags)
7470 return isl_basic_map_lexopt(bset, flags);
7473 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7475 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7478 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7480 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7483 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7485 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7488 /* Compute the lexicographic minimum of "bset" over its parametric domain
7489 * for the purpose of quantifier elimination.
7490 * That is, find an explicit representation for all the existentially
7491 * quantified variables in "bset" by computing their lexicographic
7492 * minimum.
7494 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7495 __isl_take isl_basic_set *bset)
7497 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7500 /* Given a basic map with one output dimension, compute the minimum or
7501 * maximum of that dimension as an isl_pw_aff.
7503 * Compute the optimum as a lexicographic optimum over the single
7504 * output dimension and extract the single isl_pw_aff from the result.
7506 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7507 int max)
7509 isl_pw_multi_aff *pma;
7510 isl_pw_aff *pwaff;
7512 bmap = isl_basic_map_copy(bmap);
7513 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7514 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7515 isl_pw_multi_aff_free(pma);
7517 return pwaff;
7520 /* Compute the minimum or maximum of the given output dimension
7521 * as a function of the parameters and the input dimensions,
7522 * but independently of the other output dimensions.
7524 * We first project out the other output dimension and then compute
7525 * the "lexicographic" maximum in each basic map, combining the results
7526 * using isl_pw_aff_union_max.
7528 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7529 int max)
7531 int i;
7532 isl_pw_aff *pwaff;
7533 isl_size n_out;
7535 n_out = isl_map_dim(map, isl_dim_out);
7536 if (n_out < 0)
7537 map = isl_map_free(map);
7538 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7539 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7540 if (!map)
7541 return NULL;
7543 if (map->n == 0) {
7544 isl_space *space = isl_map_get_space(map);
7545 isl_map_free(map);
7546 return isl_pw_aff_empty(space);
7549 pwaff = basic_map_dim_opt(map->p[0], max);
7550 for (i = 1; i < map->n; ++i) {
7551 isl_pw_aff *pwaff_i;
7553 pwaff_i = basic_map_dim_opt(map->p[i], max);
7554 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7557 isl_map_free(map);
7559 return pwaff;
7562 /* Compute the minimum of the given output dimension as a function of the
7563 * parameters and input dimensions, but independently of
7564 * the other output dimensions.
7566 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7568 return map_dim_opt(map, pos, 0);
7571 /* Compute the maximum of the given output dimension as a function of the
7572 * parameters and input dimensions, but independently of
7573 * the other output dimensions.
7575 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7577 return map_dim_opt(map, pos, 1);
7580 /* Compute the minimum or maximum of the given set dimension
7581 * as a function of the parameters,
7582 * but independently of the other set dimensions.
7584 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7585 int max)
7587 return map_dim_opt(set, pos, max);
7590 /* Compute the maximum of the given set dimension as a function of the
7591 * parameters, but independently of the other set dimensions.
7593 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7595 return set_dim_opt(set, pos, 1);
7598 /* Compute the minimum of the given set dimension as a function of the
7599 * parameters, but independently of the other set dimensions.
7601 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7603 return set_dim_opt(set, pos, 0);
7606 /* Apply a preimage specified by "mat" on the parameters of "bset".
7607 * bset is assumed to have only parameters and divs.
7609 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7610 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7612 isl_size nparam;
7614 nparam = isl_basic_set_dim(bset, isl_dim_param);
7615 if (nparam < 0 || !mat)
7616 goto error;
7618 bset->dim = isl_space_cow(bset->dim);
7619 if (!bset->dim)
7620 goto error;
7622 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7624 bset->dim->nparam = 0;
7625 bset->dim->n_out = nparam;
7626 bset = isl_basic_set_preimage(bset, mat);
7627 if (bset) {
7628 bset->dim->nparam = bset->dim->n_out;
7629 bset->dim->n_out = 0;
7631 return bset;
7632 error:
7633 isl_mat_free(mat);
7634 isl_basic_set_free(bset);
7635 return NULL;
7638 /* Apply a preimage specified by "mat" on the parameters of "set".
7639 * set is assumed to have only parameters and divs.
7641 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7642 __isl_take isl_mat *mat)
7644 isl_space *space;
7645 isl_size nparam;
7647 nparam = isl_set_dim(set, isl_dim_param);
7648 if (nparam < 0 || !mat)
7649 goto error;
7651 if (mat->n_row != 1 + nparam)
7652 isl_die(isl_set_get_ctx(set), isl_error_internal,
7653 "unexpected number of rows", goto error);
7655 space = isl_set_get_space(set);
7656 space = isl_space_move_dims(space, isl_dim_set, 0,
7657 isl_dim_param, 0, nparam);
7658 set = isl_set_reset_space(set, space);
7659 set = isl_set_preimage(set, mat);
7660 nparam = isl_set_dim(set, isl_dim_out);
7661 if (nparam < 0)
7662 set = isl_set_free(set);
7663 space = isl_set_get_space(set);
7664 space = isl_space_move_dims(space, isl_dim_param, 0,
7665 isl_dim_out, 0, nparam);
7666 set = isl_set_reset_space(set, space);
7667 return set;
7668 error:
7669 isl_mat_free(mat);
7670 isl_set_free(set);
7671 return NULL;
7674 /* Intersect the basic set "bset" with the affine space specified by the
7675 * equalities in "eq".
7677 static __isl_give isl_basic_set *basic_set_append_equalities(
7678 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7680 int i, k;
7681 unsigned len;
7683 if (!bset || !eq)
7684 goto error;
7686 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7687 if (!bset)
7688 goto error;
7690 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7691 for (i = 0; i < eq->n_row; ++i) {
7692 k = isl_basic_set_alloc_equality(bset);
7693 if (k < 0)
7694 goto error;
7695 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7696 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7698 isl_mat_free(eq);
7700 bset = isl_basic_set_gauss(bset, NULL);
7701 bset = isl_basic_set_finalize(bset);
7703 return bset;
7704 error:
7705 isl_mat_free(eq);
7706 isl_basic_set_free(bset);
7707 return NULL;
7710 /* Intersect the set "set" with the affine space specified by the
7711 * equalities in "eq".
7713 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7714 __isl_take isl_mat *eq)
7716 int i;
7718 if (!set || !eq)
7719 goto error;
7721 for (i = 0; i < set->n; ++i) {
7722 set->p[i] = basic_set_append_equalities(set->p[i],
7723 isl_mat_copy(eq));
7724 if (!set->p[i])
7725 goto error;
7727 isl_mat_free(eq);
7728 return set;
7729 error:
7730 isl_mat_free(eq);
7731 isl_set_free(set);
7732 return NULL;
7735 /* Given a basic set "bset" that only involves parameters and existentially
7736 * quantified variables, return the index of the first equality
7737 * that only involves parameters. If there is no such equality then
7738 * return bset->n_eq.
7740 * This function assumes that isl_basic_set_gauss has been called on "bset".
7742 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7744 int i, j;
7745 isl_size nparam, n_div;
7747 nparam = isl_basic_set_dim(bset, isl_dim_param);
7748 n_div = isl_basic_set_dim(bset, isl_dim_div);
7749 if (nparam < 0 || n_div < 0)
7750 return -1;
7752 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7753 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7754 ++i;
7757 return i;
7760 /* Compute an explicit representation for the existentially quantified
7761 * variables in "bset" by computing the "minimal value" of the set
7762 * variables. Since there are no set variables, the computation of
7763 * the minimal value essentially computes an explicit representation
7764 * of the non-empty part(s) of "bset".
7766 * The input only involves parameters and existentially quantified variables.
7767 * All equalities among parameters have been removed.
7769 * Since the existentially quantified variables in the result are in general
7770 * going to be different from those in the input, we first replace
7771 * them by the minimal number of variables based on their equalities.
7772 * This should simplify the parametric integer programming.
7774 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7776 isl_morph *morph1, *morph2;
7777 isl_set *set;
7778 isl_size n;
7780 if (!bset)
7781 return NULL;
7782 if (bset->n_eq == 0)
7783 return isl_basic_set_lexmin_compute_divs(bset);
7785 morph1 = isl_basic_set_parameter_compression(bset);
7786 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7787 bset = isl_basic_set_lift(bset);
7788 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7789 bset = isl_morph_basic_set(morph2, bset);
7790 n = isl_basic_set_dim(bset, isl_dim_set);
7791 if (n < 0)
7792 bset = isl_basic_set_free(bset);
7793 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7795 set = isl_basic_set_lexmin_compute_divs(bset);
7797 set = isl_morph_set(isl_morph_inverse(morph1), set);
7799 return set;
7802 /* Project the given basic set onto its parameter domain, possibly introducing
7803 * new, explicit, existential variables in the constraints.
7804 * The input has parameters and (possibly implicit) existential variables.
7805 * The output has the same parameters, but only
7806 * explicit existentially quantified variables.
7808 * The actual projection is performed by pip, but pip doesn't seem
7809 * to like equalities very much, so we first remove the equalities
7810 * among the parameters by performing a variable compression on
7811 * the parameters. Afterward, an inverse transformation is performed
7812 * and the equalities among the parameters are inserted back in.
7814 * The variable compression on the parameters may uncover additional
7815 * equalities that were only implicit before. We therefore check
7816 * if there are any new parameter equalities in the result and
7817 * if so recurse. The removal of parameter equalities is required
7818 * for the parameter compression performed by base_compute_divs.
7820 static __isl_give isl_set *parameter_compute_divs(
7821 __isl_take isl_basic_set *bset)
7823 int i;
7824 struct isl_mat *eq;
7825 struct isl_mat *T, *T2;
7826 struct isl_set *set;
7827 isl_size nparam;
7829 bset = isl_basic_set_cow(bset);
7830 if (!bset)
7831 return NULL;
7833 if (bset->n_eq == 0)
7834 return base_compute_divs(bset);
7836 bset = isl_basic_set_gauss(bset, NULL);
7837 if (!bset)
7838 return NULL;
7839 if (isl_basic_set_plain_is_empty(bset))
7840 return isl_set_from_basic_set(bset);
7842 i = first_parameter_equality(bset);
7843 if (i == bset->n_eq)
7844 return base_compute_divs(bset);
7846 nparam = isl_basic_set_dim(bset, isl_dim_param);
7847 if (nparam < 0)
7848 return isl_set_from_basic_set(isl_basic_set_free(bset));
7849 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7850 0, 1 + nparam);
7851 eq = isl_mat_cow(eq);
7852 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7853 if (T && T->n_col == 0) {
7854 isl_mat_free(T);
7855 isl_mat_free(T2);
7856 isl_mat_free(eq);
7857 bset = isl_basic_set_set_to_empty(bset);
7858 return isl_set_from_basic_set(bset);
7860 bset = basic_set_parameter_preimage(bset, T);
7862 i = first_parameter_equality(bset);
7863 if (!bset)
7864 set = NULL;
7865 else if (i == bset->n_eq)
7866 set = base_compute_divs(bset);
7867 else
7868 set = parameter_compute_divs(bset);
7869 set = set_parameter_preimage(set, T2);
7870 set = set_append_equalities(set, eq);
7871 return set;
7874 /* Insert the divs from "ls" before those of "bmap".
7876 * The number of columns is not changed, which means that the last
7877 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7878 * The caller is responsible for removing the same number of dimensions
7879 * from the space of "bmap".
7881 static __isl_give isl_basic_map *insert_divs_from_local_space(
7882 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7884 int i;
7885 isl_size n_div;
7886 int old_n_div;
7888 n_div = isl_local_space_dim(ls, isl_dim_div);
7889 if (n_div < 0)
7890 return isl_basic_map_free(bmap);
7891 if (n_div == 0)
7892 return bmap;
7894 old_n_div = bmap->n_div;
7895 bmap = insert_div_rows(bmap, n_div);
7896 if (!bmap)
7897 return NULL;
7899 for (i = 0; i < n_div; ++i) {
7900 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7901 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7904 return bmap;
7907 /* Replace the space of "bmap" by the space and divs of "ls".
7909 * If "ls" has any divs, then we simplify the result since we may
7910 * have discovered some additional equalities that could simplify
7911 * the div expressions.
7913 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7914 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7916 isl_size n_div;
7918 bmap = isl_basic_map_cow(bmap);
7919 n_div = isl_local_space_dim(ls, isl_dim_div);
7920 if (!bmap || n_div < 0)
7921 goto error;
7923 bmap = insert_divs_from_local_space(bmap, ls);
7924 if (!bmap)
7925 goto error;
7927 isl_space_free(bmap->dim);
7928 bmap->dim = isl_local_space_get_space(ls);
7929 if (!bmap->dim)
7930 goto error;
7932 isl_local_space_free(ls);
7933 if (n_div > 0)
7934 bmap = isl_basic_map_simplify(bmap);
7935 bmap = isl_basic_map_finalize(bmap);
7936 return bmap;
7937 error:
7938 isl_basic_map_free(bmap);
7939 isl_local_space_free(ls);
7940 return NULL;
7943 /* Replace the space of "map" by the space and divs of "ls".
7945 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7946 __isl_take isl_local_space *ls)
7948 int i;
7950 map = isl_map_cow(map);
7951 if (!map || !ls)
7952 goto error;
7954 for (i = 0; i < map->n; ++i) {
7955 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7956 isl_local_space_copy(ls));
7957 if (!map->p[i])
7958 goto error;
7960 isl_space_free(isl_map_take_space(map));
7961 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7963 isl_local_space_free(ls);
7964 return map;
7965 error:
7966 isl_local_space_free(ls);
7967 isl_map_free(map);
7968 return NULL;
7971 /* Compute an explicit representation for the existentially
7972 * quantified variables for which do not know any explicit representation yet.
7974 * We first sort the existentially quantified variables so that the
7975 * existentially quantified variables for which we already have an explicit
7976 * representation are placed before those for which we do not.
7977 * The input dimensions, the output dimensions and the existentially
7978 * quantified variables for which we already have an explicit
7979 * representation are then turned into parameters.
7980 * compute_divs returns a map with the same parameters and
7981 * no input or output dimensions and the dimension specification
7982 * is reset to that of the input, including the existentially quantified
7983 * variables for which we already had an explicit representation.
7985 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7987 struct isl_basic_set *bset;
7988 struct isl_set *set;
7989 struct isl_map *map;
7990 isl_space *space;
7991 isl_local_space *ls;
7992 isl_size nparam;
7993 isl_size n_in;
7994 isl_size n_out;
7995 int n_known;
7996 int i;
7998 bmap = isl_basic_map_sort_divs(bmap);
7999 bmap = isl_basic_map_cow(bmap);
8000 if (!bmap)
8001 return NULL;
8003 n_known = isl_basic_map_first_unknown_div(bmap);
8004 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8005 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8006 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8007 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8008 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8010 space = isl_space_set_alloc(bmap->ctx,
8011 nparam + n_in + n_out + n_known, 0);
8012 if (!space)
8013 goto error;
8015 ls = isl_basic_map_get_local_space(bmap);
8016 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8017 n_known, bmap->n_div - n_known);
8018 if (n_known > 0) {
8019 for (i = n_known; i < bmap->n_div; ++i)
8020 swap_div(bmap, i - n_known, i);
8021 bmap->n_div -= n_known;
8022 bmap->extra -= n_known;
8024 bmap = isl_basic_map_reset_space(bmap, space);
8025 bset = bset_from_bmap(bmap);
8027 set = parameter_compute_divs(bset);
8028 map = set_to_map(set);
8029 map = replace_space_by_local_space(map, ls);
8031 return map;
8032 error:
8033 isl_basic_map_free(bmap);
8034 return NULL;
8037 /* Remove the explicit representation of local variable "div",
8038 * if there is any.
8040 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8041 __isl_take isl_basic_map *bmap, int div)
8043 isl_bool unknown;
8045 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8046 if (unknown < 0)
8047 return isl_basic_map_free(bmap);
8048 if (unknown)
8049 return bmap;
8051 bmap = isl_basic_map_cow(bmap);
8052 if (!bmap)
8053 return NULL;
8054 isl_int_set_si(bmap->div[div][0], 0);
8055 return bmap;
8058 /* Is local variable "div" of "bmap" marked as not having an explicit
8059 * representation?
8060 * Note that even if "div" is not marked in this way and therefore
8061 * has an explicit representation, this representation may still
8062 * depend (indirectly) on other local variables that do not
8063 * have an explicit representation.
8065 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8066 int div)
8068 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8069 return isl_bool_error;
8070 return isl_int_is_zero(bmap->div[div][0]);
8073 /* Return the position of the first local variable that does not
8074 * have an explicit representation.
8075 * Return the total number of local variables if they all have
8076 * an explicit representation.
8077 * Return -1 on error.
8079 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8081 int i;
8083 if (!bmap)
8084 return -1;
8086 for (i = 0; i < bmap->n_div; ++i) {
8087 if (!isl_basic_map_div_is_known(bmap, i))
8088 return i;
8090 return bmap->n_div;
8093 /* Return the position of the first local variable that does not
8094 * have an explicit representation.
8095 * Return the total number of local variables if they all have
8096 * an explicit representation.
8097 * Return -1 on error.
8099 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8101 return isl_basic_map_first_unknown_div(bset);
8104 /* Does "bmap" have an explicit representation for all local variables?
8106 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8108 int first;
8109 isl_size n;
8111 n = isl_basic_map_dim(bmap, isl_dim_div);
8112 first = isl_basic_map_first_unknown_div(bmap);
8113 if (n < 0 || first < 0)
8114 return isl_bool_error;
8115 return first == n;
8118 /* Do all basic maps in "map" have an explicit representation
8119 * for all local variables?
8121 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8123 int i;
8125 if (!map)
8126 return isl_bool_error;
8128 for (i = 0; i < map->n; ++i) {
8129 int known = isl_basic_map_divs_known(map->p[i]);
8130 if (known <= 0)
8131 return known;
8134 return isl_bool_true;
8137 /* If bmap contains any unknown divs, then compute explicit
8138 * expressions for them. However, this computation may be
8139 * quite expensive, so first try to remove divs that aren't
8140 * strictly needed.
8142 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8144 int known;
8145 struct isl_map *map;
8147 known = isl_basic_map_divs_known(bmap);
8148 if (known < 0)
8149 goto error;
8150 if (known)
8151 return isl_map_from_basic_map(bmap);
8153 bmap = isl_basic_map_drop_redundant_divs(bmap);
8155 known = isl_basic_map_divs_known(bmap);
8156 if (known < 0)
8157 goto error;
8158 if (known)
8159 return isl_map_from_basic_map(bmap);
8161 map = compute_divs(bmap);
8162 return map;
8163 error:
8164 isl_basic_map_free(bmap);
8165 return NULL;
8168 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8170 int i;
8171 int known;
8172 struct isl_map *res;
8174 if (!map)
8175 return NULL;
8176 if (map->n == 0)
8177 return map;
8179 known = isl_map_divs_known(map);
8180 if (known < 0) {
8181 isl_map_free(map);
8182 return NULL;
8184 if (known)
8185 return map;
8187 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8188 for (i = 1 ; i < map->n; ++i) {
8189 struct isl_map *r2;
8190 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8191 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8192 res = isl_map_union_disjoint(res, r2);
8193 else
8194 res = isl_map_union(res, r2);
8196 isl_map_free(map);
8198 return res;
8201 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8203 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8206 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8208 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8211 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8213 isl_space *space;
8214 isl_size n_out;
8216 n_out = isl_map_dim(map, isl_dim_out);
8217 if (n_out < 0)
8218 return set_from_map(isl_map_free(map));
8219 space = isl_space_domain(isl_map_get_space(map));
8221 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8223 return set_from_map(isl_map_reset_space(map, space));
8226 /* Return the union of "map1" and "map2", where we assume for now that
8227 * "map1" and "map2" are disjoint. Note that the basic maps inside
8228 * "map1" or "map2" may not be disjoint from each other.
8229 * Also note that this function is also called from isl_map_union,
8230 * which takes care of handling the situation where "map1" and "map2"
8231 * may not be disjoint.
8233 * If one of the inputs is empty, we can simply return the other input.
8234 * Similarly, if one of the inputs is universal, then it is equal to the union.
8236 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8237 __isl_take isl_map *map2)
8239 int i;
8240 unsigned flags = 0;
8241 struct isl_map *map = NULL;
8242 int is_universe;
8244 if (isl_map_check_equal_space(map1, map2) < 0)
8245 goto error;
8247 if (map1->n == 0) {
8248 isl_map_free(map1);
8249 return map2;
8251 if (map2->n == 0) {
8252 isl_map_free(map2);
8253 return map1;
8256 is_universe = isl_map_plain_is_universe(map1);
8257 if (is_universe < 0)
8258 goto error;
8259 if (is_universe) {
8260 isl_map_free(map2);
8261 return map1;
8264 is_universe = isl_map_plain_is_universe(map2);
8265 if (is_universe < 0)
8266 goto error;
8267 if (is_universe) {
8268 isl_map_free(map1);
8269 return map2;
8272 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8273 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8274 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8276 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8277 map1->n + map2->n, flags);
8278 if (!map)
8279 goto error;
8280 for (i = 0; i < map1->n; ++i) {
8281 map = isl_map_add_basic_map(map,
8282 isl_basic_map_copy(map1->p[i]));
8283 if (!map)
8284 goto error;
8286 for (i = 0; i < map2->n; ++i) {
8287 map = isl_map_add_basic_map(map,
8288 isl_basic_map_copy(map2->p[i]));
8289 if (!map)
8290 goto error;
8292 isl_map_free(map1);
8293 isl_map_free(map2);
8294 return map;
8295 error:
8296 isl_map_free(map);
8297 isl_map_free(map1);
8298 isl_map_free(map2);
8299 return NULL;
8302 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8303 * guaranteed to be disjoint by the caller.
8305 * Note that this functions is called from within isl_map_make_disjoint,
8306 * so we have to be careful not to touch the constraints of the inputs
8307 * in any way.
8309 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8310 __isl_take isl_map *map2)
8312 isl_map_align_params_bin(&map1, &map2);
8313 return map_union_disjoint(map1, map2);
8316 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8317 * not be disjoint.
8319 * We currently simply call map_union_disjoint, the internal operation
8320 * of which does not really depend on the inputs being disjoint.
8321 * If the result contains more than one basic map, then we clear
8322 * the disjoint flag since the result may contain basic maps from
8323 * both inputs and these are not guaranteed to be disjoint.
8325 * As a special case, if "map1" and "map2" are obviously equal,
8326 * then we simply return "map1".
8328 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8329 __isl_take isl_map *map2)
8331 int equal;
8333 if (isl_map_align_params_bin(&map1, &map2) < 0)
8334 goto error;
8336 equal = isl_map_plain_is_equal(map1, map2);
8337 if (equal < 0)
8338 goto error;
8339 if (equal) {
8340 isl_map_free(map2);
8341 return map1;
8344 map1 = map_union_disjoint(map1, map2);
8345 if (!map1)
8346 return NULL;
8347 if (map1->n > 1)
8348 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8349 return map1;
8350 error:
8351 isl_map_free(map1);
8352 isl_map_free(map2);
8353 return NULL;
8356 __isl_give isl_set *isl_set_union_disjoint(
8357 __isl_take isl_set *set1, __isl_take isl_set *set2)
8359 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8360 set_to_map(set2)));
8363 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8364 __isl_take isl_set *set2)
8366 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8369 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8370 * the results in a map living in "space".
8372 * "map" and "set" are assumed to be compatible and non-NULL.
8374 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8375 __isl_take isl_space *space, __isl_take isl_set *set,
8376 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8377 __isl_take isl_basic_set *bset))
8379 unsigned flags = 0;
8380 struct isl_map *result;
8381 int i, j;
8383 if (isl_set_plain_is_universe(set)) {
8384 isl_set_free(set);
8385 return isl_map_reset_equal_dim_space(map, space);
8388 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8389 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8390 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8392 result = isl_map_alloc_space(space, map->n * set->n, flags);
8393 for (i = 0; result && i < map->n; ++i)
8394 for (j = 0; j < set->n; ++j) {
8395 result = isl_map_add_basic_map(result,
8396 fn(isl_basic_map_copy(map->p[i]),
8397 isl_basic_set_copy(set->p[j])));
8398 if (!result)
8399 break;
8402 isl_map_free(map);
8403 isl_set_free(set);
8404 return result;
8407 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8408 __isl_take isl_set *set)
8410 isl_bool ok;
8411 isl_space *space;
8413 isl_map_align_params_set(&map, &set);
8414 ok = isl_map_compatible_range(map, set);
8415 if (ok < 0)
8416 goto error;
8417 if (!ok)
8418 isl_die(set->ctx, isl_error_invalid,
8419 "incompatible spaces", goto error);
8421 space = isl_map_get_space(map);
8422 return map_intersect_set(map, space, set,
8423 &isl_basic_map_intersect_range);
8424 error:
8425 isl_map_free(map);
8426 isl_set_free(set);
8427 return NULL;
8430 /* Intersect the domain of "map" with "set".
8432 * If the domain dimensions of "map" do not have any identifiers,
8433 * then copy them over from "set".
8435 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8436 __isl_take isl_set *set)
8438 isl_bool ok;
8439 isl_space *space;
8441 isl_map_align_params_set(&map, &set);
8442 ok = isl_map_compatible_domain(map, set);
8443 if (ok < 0)
8444 goto error;
8445 if (!ok)
8446 isl_die(set->ctx, isl_error_invalid,
8447 "incompatible spaces", goto error);
8449 space = isl_map_get_space(map);
8450 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8451 isl_set_peek_space(set), isl_dim_set);
8452 return map_intersect_set(map, space, set,
8453 &isl_basic_map_intersect_domain);
8454 error:
8455 isl_map_free(map);
8456 isl_set_free(set);
8457 return NULL;
8460 #undef BASE
8461 #define BASE map
8462 static
8463 #include "isl_copy_tuple_id_templ.c"
8465 /* Data structure that specifies how isl_map_intersect_factor
8466 * should operate.
8468 * "preserve_type" is the tuple where the factor differs from
8469 * the input map and of which the identifiers needs
8470 * to be preserved explicitly.
8471 * "other_factor" is used to extract the space of the other factor
8472 * from the space of the product ("map").
8473 * "product" is used to combine the given factor and a universe map
8474 * in the space returned by "other_factor" to produce a map
8475 * that lives in the same space as the input map.
8477 struct isl_intersect_factor_control {
8478 enum isl_dim_type preserve_type;
8479 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8480 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8481 __isl_take isl_map *other);
8484 /* Given a map "map" in some product space and a map "factor"
8485 * living in some factor space, return the intersection.
8487 * After aligning the parameters,
8488 * the map "factor" is first extended to a map living in the same space
8489 * as "map" and then a regular intersection is computed.
8491 * Note that the extension is computed as a product, which is anonymous
8492 * by default. If "map" has an identifier on the corresponding tuple,
8493 * then this identifier needs to be set on the product
8494 * before the intersection is computed.
8496 static __isl_give isl_map *isl_map_intersect_factor(
8497 __isl_take isl_map *map, __isl_take isl_map *factor,
8498 struct isl_intersect_factor_control *control)
8500 isl_bool equal;
8501 isl_space *space;
8502 isl_map *other, *product;
8504 equal = isl_map_has_equal_params(map, factor);
8505 if (equal < 0)
8506 goto error;
8507 if (!equal) {
8508 map = isl_map_align_params(map, isl_map_get_space(factor));
8509 factor = isl_map_align_params(factor, isl_map_get_space(map));
8512 space = isl_map_get_space(map);
8513 other = isl_map_universe(control->other_factor(space));
8514 product = control->product(factor, other);
8516 space = isl_map_peek_space(map);
8517 product = isl_map_copy_tuple_id(product, control->preserve_type,
8518 space, control->preserve_type);
8519 return map_intersect(map, product);
8520 error:
8521 isl_map_free(map);
8522 isl_map_free(factor);
8523 return NULL;
8526 /* Return the domain product of "map2" and "map1".
8528 static __isl_give isl_map *isl_map_reverse_domain_product(
8529 __isl_take isl_map *map1, __isl_take isl_map *map2)
8531 return isl_map_domain_product(map2, map1);
8534 /* Return the range product of "map2" and "map1".
8536 static __isl_give isl_map *isl_map_reverse_range_product(
8537 __isl_take isl_map *map1, __isl_take isl_map *map2)
8539 return isl_map_range_product(map2, map1);
8542 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8543 * in the space A -> C, return the intersection.
8545 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8546 __isl_take isl_map *map, __isl_take isl_map *factor)
8548 struct isl_intersect_factor_control control = {
8549 .preserve_type = isl_dim_in,
8550 .other_factor = isl_space_domain_factor_range,
8551 .product = isl_map_domain_product,
8554 return isl_map_intersect_factor(map, factor, &control);
8557 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8558 * in the space B -> C, return the intersection.
8560 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8561 __isl_take isl_map *map, __isl_take isl_map *factor)
8563 struct isl_intersect_factor_control control = {
8564 .preserve_type = isl_dim_in,
8565 .other_factor = isl_space_domain_factor_domain,
8566 .product = isl_map_reverse_domain_product,
8569 return isl_map_intersect_factor(map, factor, &control);
8572 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8573 * in the space A -> B, return the intersection.
8575 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8576 __isl_take isl_map *map, __isl_take isl_map *factor)
8578 struct isl_intersect_factor_control control = {
8579 .preserve_type = isl_dim_out,
8580 .other_factor = isl_space_range_factor_range,
8581 .product = isl_map_range_product,
8584 return isl_map_intersect_factor(map, factor, &control);
8587 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8588 * in the space A -> C, return the intersection.
8590 __isl_give isl_map *isl_map_intersect_range_factor_range(
8591 __isl_take isl_map *map, __isl_take isl_map *factor)
8593 struct isl_intersect_factor_control control = {
8594 .preserve_type = isl_dim_out,
8595 .other_factor = isl_space_range_factor_domain,
8596 .product = isl_map_reverse_range_product,
8599 return isl_map_intersect_factor(map, factor, &control);
8602 /* Given a set "set" in a space [A -> B] and a set "domain"
8603 * in the space A, return the intersection.
8605 * The set "domain" is first extended to a set living in the space
8606 * [A -> B] and then a regular intersection is computed.
8608 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8609 __isl_take isl_set *domain)
8611 struct isl_intersect_factor_control control = {
8612 .preserve_type = isl_dim_set,
8613 .other_factor = isl_space_factor_range,
8614 .product = isl_map_range_product,
8617 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8618 set_to_map(domain), &control));
8621 /* Given a set "set" in a space [A -> B] and a set "range"
8622 * in the space B, return the intersection.
8624 * The set "range" is first extended to a set living in the space
8625 * [A -> B] and then a regular intersection is computed.
8627 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8628 __isl_take isl_set *range)
8630 struct isl_intersect_factor_control control = {
8631 .preserve_type = isl_dim_set,
8632 .other_factor = isl_space_factor_domain,
8633 .product = isl_map_reverse_range_product,
8636 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8637 set_to_map(range), &control));
8640 #undef BASE
8641 #define BASE set
8642 static
8643 #include "isl_copy_tuple_id_templ.c"
8645 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8646 * in the space A, return the intersection.
8648 * The set "domain" is extended to a set living in the space [A -> B] and
8649 * the domain of "map" is intersected with this set.
8651 * If "map" has an identifier on the domain tuple,
8652 * then this identifier needs to be set on this product
8653 * before the intersection is computed.
8655 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8656 __isl_take isl_map *map, __isl_take isl_set *domain)
8658 isl_space *space;
8659 isl_set *factor;
8661 isl_map_align_params_set(&map, &domain);
8662 space = isl_map_get_space(map);
8663 space = isl_space_domain_wrapped_range(space);
8664 factor = isl_set_universe(space);
8665 domain = isl_set_product(domain, factor);
8666 space = isl_map_peek_space(map);
8667 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
8668 return isl_map_intersect_domain(map, domain);
8671 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8672 * in the space B, return the intersection.
8674 * The set "domain" is extended to a set living in the space [B -> C] and
8675 * the range of "map" is intersected with this set.
8677 * If "map" has an identifier on the range tuple,
8678 * then this identifier needs to be set on this product
8679 * before the intersection is computed.
8681 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8682 __isl_take isl_map *map, __isl_take isl_set *domain)
8684 isl_space *space;
8685 isl_set *factor;
8687 isl_map_align_params_set(&map, &domain);
8688 space = isl_map_get_space(map);
8689 space = isl_space_range_wrapped_range(space);
8690 factor = isl_set_universe(space);
8691 domain = isl_set_product(domain, factor);
8692 space = isl_map_peek_space(map);
8693 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
8694 return isl_map_intersect_range(map, domain);
8697 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8698 __isl_take isl_map *map2)
8700 if (isl_map_align_params_bin(&map1, &map2) < 0)
8701 goto error;
8702 map1 = isl_map_reverse(map1);
8703 map1 = isl_map_apply_range(map1, map2);
8704 return isl_map_reverse(map1);
8705 error:
8706 isl_map_free(map1);
8707 isl_map_free(map2);
8708 return NULL;
8711 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8712 __isl_take isl_map *map2)
8714 isl_space *space;
8715 struct isl_map *result;
8716 int i, j;
8718 if (isl_map_align_params_bin(&map1, &map2) < 0)
8719 goto error;
8721 space = isl_space_join(isl_space_copy(map1->dim),
8722 isl_space_copy(map2->dim));
8724 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8725 if (!result)
8726 goto error;
8727 for (i = 0; i < map1->n; ++i)
8728 for (j = 0; j < map2->n; ++j) {
8729 result = isl_map_add_basic_map(result,
8730 isl_basic_map_apply_range(
8731 isl_basic_map_copy(map1->p[i]),
8732 isl_basic_map_copy(map2->p[j])));
8733 if (!result)
8734 goto error;
8736 isl_map_free(map1);
8737 isl_map_free(map2);
8738 if (result && result->n <= 1)
8739 ISL_F_SET(result, ISL_MAP_DISJOINT);
8740 return result;
8741 error:
8742 isl_map_free(map1);
8743 isl_map_free(map2);
8744 return NULL;
8747 /* Is "bmap" a transformation, i.e.,
8748 * does it relate elements from the same space.
8750 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8752 isl_space *space;
8754 space = isl_basic_map_peek_space(bmap);
8755 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8758 /* Check that "bmap" is a transformation, i.e.,
8759 * that it relates elements from the same space.
8761 static isl_stat isl_basic_map_check_transformation(
8762 __isl_keep isl_basic_map *bmap)
8764 isl_bool equal;
8766 equal = isl_basic_map_is_transformation(bmap);
8767 if (equal < 0)
8768 return isl_stat_error;
8769 if (!equal)
8770 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8771 "domain and range don't match", return isl_stat_error);
8772 return isl_stat_ok;
8776 * returns range - domain
8778 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8780 isl_space *target_space;
8781 struct isl_basic_set *bset;
8782 isl_size dim;
8783 isl_size nparam;
8784 isl_size total;
8785 int i;
8787 if (isl_basic_map_check_transformation(bmap) < 0)
8788 return isl_basic_map_free(bmap);
8789 dim = isl_basic_map_dim(bmap, isl_dim_in);
8790 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8791 if (dim < 0 || nparam < 0)
8792 goto error;
8793 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8794 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8795 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8796 total = isl_basic_map_dim(bmap, isl_dim_all);
8797 if (total < 0)
8798 bmap = isl_basic_map_free(bmap);
8799 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8800 for (i = 0; i < dim; ++i) {
8801 int j = isl_basic_map_alloc_equality(bmap);
8802 if (j < 0) {
8803 bmap = isl_basic_map_free(bmap);
8804 break;
8806 isl_seq_clr(bmap->eq[j], 1 + total);
8807 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8808 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8809 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8811 bset = isl_basic_map_domain(bmap);
8812 bset = isl_basic_set_reset_space(bset, target_space);
8813 return bset;
8814 error:
8815 isl_basic_map_free(bmap);
8816 return NULL;
8819 /* Is the tuple of type "type1" of "map" the same as
8820 * the tuple of type "type2" of "space"?
8822 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8823 enum isl_dim_type type1, __isl_keep isl_space *space,
8824 enum isl_dim_type type2)
8826 isl_space *map_space;
8828 map_space = isl_map_peek_space(map);
8829 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8832 /* Is the tuple of type "type1" of "map1" the same as
8833 * the tuple of type "type2" of "map2"?
8835 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8836 enum isl_dim_type type1, __isl_keep isl_map *map2,
8837 enum isl_dim_type type2)
8839 isl_space *space1, *space2;
8841 space1 = isl_map_peek_space(map1);
8842 space2 = isl_map_peek_space(map2);
8843 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8846 /* Is the space of "obj" equal to "space", ignoring parameters?
8848 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8849 __isl_keep isl_space *space)
8851 isl_space *map_space;
8853 map_space = isl_map_peek_space(map);
8854 return isl_space_has_equal_tuples(map_space, space);
8857 /* Check that "map" is a transformation, i.e.,
8858 * that it relates elements from the same space.
8860 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8862 isl_bool equal;
8864 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8865 if (equal < 0)
8866 return isl_stat_error;
8867 if (!equal)
8868 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8869 "domain and range don't match", return isl_stat_error);
8870 return isl_stat_ok;
8874 * returns range - domain
8876 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8878 int i;
8879 isl_space *space;
8880 struct isl_set *result;
8882 if (isl_map_check_transformation(map) < 0)
8883 goto error;
8884 space = isl_map_get_space(map);
8885 space = isl_space_domain(space);
8886 result = isl_set_alloc_space(space, map->n, 0);
8887 if (!result)
8888 goto error;
8889 for (i = 0; i < map->n; ++i)
8890 result = isl_set_add_basic_set(result,
8891 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8892 isl_map_free(map);
8893 return result;
8894 error:
8895 isl_map_free(map);
8896 return NULL;
8900 * returns [domain -> range] -> range - domain
8902 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8903 __isl_take isl_basic_map *bmap)
8905 int i, k;
8906 isl_space *space;
8907 isl_basic_map *domain;
8908 isl_size nparam, n;
8909 isl_size total;
8911 if (isl_basic_map_check_transformation(bmap) < 0)
8912 return isl_basic_map_free(bmap);
8914 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8915 n = isl_basic_map_dim(bmap, isl_dim_in);
8916 if (nparam < 0 || n < 0)
8917 return isl_basic_map_free(bmap);
8919 space = isl_basic_map_get_space(bmap);
8920 space = isl_space_from_range(isl_space_domain(space));
8921 domain = isl_basic_map_universe(space);
8923 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8924 bmap = isl_basic_map_apply_range(bmap, domain);
8925 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8927 total = isl_basic_map_dim(bmap, isl_dim_all);
8928 if (total < 0)
8929 return isl_basic_map_free(bmap);
8931 for (i = 0; i < n; ++i) {
8932 k = isl_basic_map_alloc_equality(bmap);
8933 if (k < 0)
8934 goto error;
8935 isl_seq_clr(bmap->eq[k], 1 + total);
8936 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8937 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8938 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8941 bmap = isl_basic_map_gauss(bmap, NULL);
8942 return isl_basic_map_finalize(bmap);
8943 error:
8944 isl_basic_map_free(bmap);
8945 return NULL;
8949 * returns [domain -> range] -> range - domain
8951 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8953 if (isl_map_check_transformation(map) < 0)
8954 return isl_map_free(map);
8956 return isl_map_transform(map, &isl_space_range_map,
8957 &isl_basic_map_deltas_map);
8960 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8962 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8964 isl_space *space;
8965 isl_map *map;
8967 space = isl_space_map_from_set(isl_set_get_space(deltas));
8968 map = isl_map_deltas_map(isl_map_universe(space));
8969 map = isl_map_intersect_range(map, deltas);
8971 return isl_set_unwrap(isl_map_domain(map));
8974 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8976 isl_size n_in, n_out;
8978 n_in = isl_space_dim(space, isl_dim_in);
8979 n_out = isl_space_dim(space, isl_dim_out);
8980 if (n_in < 0 || n_out < 0)
8981 goto error;
8982 if (n_in != n_out)
8983 isl_die(space->ctx, isl_error_invalid,
8984 "number of input and output dimensions needs to be "
8985 "the same", goto error);
8986 return isl_basic_map_equal(space, n_in);
8987 error:
8988 isl_space_free(space);
8989 return NULL;
8992 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8994 return isl_map_from_basic_map(isl_basic_map_identity(space));
8997 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8999 isl_space *space = isl_set_get_space(set);
9000 isl_map *id;
9001 id = isl_map_identity(isl_space_map_from_set(space));
9002 return isl_map_intersect_range(id, set);
9005 /* Construct a basic set with all set dimensions having only non-negative
9006 * values.
9008 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9009 __isl_take isl_space *space)
9011 int i;
9012 isl_size nparam;
9013 isl_size dim;
9014 isl_size total;
9015 struct isl_basic_set *bset;
9017 nparam = isl_space_dim(space, isl_dim_param);
9018 dim = isl_space_dim(space, isl_dim_set);
9019 total = isl_space_dim(space, isl_dim_all);
9020 if (nparam < 0 || dim < 0 || total < 0)
9021 space = isl_space_free(space);
9022 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9023 if (!bset)
9024 return NULL;
9025 for (i = 0; i < dim; ++i) {
9026 int k = isl_basic_set_alloc_inequality(bset);
9027 if (k < 0)
9028 goto error;
9029 isl_seq_clr(bset->ineq[k], 1 + total);
9030 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9032 return bset;
9033 error:
9034 isl_basic_set_free(bset);
9035 return NULL;
9038 /* Construct the half-space x_pos >= 0.
9040 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9041 int pos)
9043 int k;
9044 isl_size total;
9045 isl_basic_set *nonneg;
9047 total = isl_space_dim(space, isl_dim_all);
9048 if (total < 0)
9049 space = isl_space_free(space);
9050 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9051 k = isl_basic_set_alloc_inequality(nonneg);
9052 if (k < 0)
9053 goto error;
9054 isl_seq_clr(nonneg->ineq[k], 1 + total);
9055 isl_int_set_si(nonneg->ineq[k][pos], 1);
9057 return isl_basic_set_finalize(nonneg);
9058 error:
9059 isl_basic_set_free(nonneg);
9060 return NULL;
9063 /* Construct the half-space x_pos <= -1.
9065 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9066 int pos)
9068 int k;
9069 isl_size total;
9070 isl_basic_set *neg;
9072 total = isl_space_dim(space, isl_dim_all);
9073 if (total < 0)
9074 space = isl_space_free(space);
9075 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9076 k = isl_basic_set_alloc_inequality(neg);
9077 if (k < 0)
9078 goto error;
9079 isl_seq_clr(neg->ineq[k], 1 + total);
9080 isl_int_set_si(neg->ineq[k][0], -1);
9081 isl_int_set_si(neg->ineq[k][pos], -1);
9083 return isl_basic_set_finalize(neg);
9084 error:
9085 isl_basic_set_free(neg);
9086 return NULL;
9089 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9090 enum isl_dim_type type, unsigned first, unsigned n)
9092 int i;
9093 unsigned offset;
9094 isl_basic_set *nonneg;
9095 isl_basic_set *neg;
9097 if (n == 0)
9098 return set;
9100 if (isl_set_check_range(set, type, first, n) < 0)
9101 return isl_set_free(set);
9103 offset = pos(set->dim, type);
9104 for (i = 0; i < n; ++i) {
9105 nonneg = nonneg_halfspace(isl_set_get_space(set),
9106 offset + first + i);
9107 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9109 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9112 return set;
9115 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9116 int len,
9117 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9118 void *user)
9120 isl_set *half;
9122 if (!set)
9123 return isl_stat_error;
9124 if (isl_set_plain_is_empty(set)) {
9125 isl_set_free(set);
9126 return isl_stat_ok;
9128 if (first == len)
9129 return fn(set, signs, user);
9131 signs[first] = 1;
9132 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9133 1 + first));
9134 half = isl_set_intersect(half, isl_set_copy(set));
9135 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9136 goto error;
9138 signs[first] = -1;
9139 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9140 1 + first));
9141 half = isl_set_intersect(half, set);
9142 return foreach_orthant(half, signs, first + 1, len, fn, user);
9143 error:
9144 isl_set_free(set);
9145 return isl_stat_error;
9148 /* Call "fn" on the intersections of "set" with each of the orthants
9149 * (except for obviously empty intersections). The orthant is identified
9150 * by the signs array, with each entry having value 1 or -1 according
9151 * to the sign of the corresponding variable.
9153 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9154 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9155 void *user)
9157 isl_size nparam;
9158 isl_size nvar;
9159 int *signs;
9160 isl_stat r;
9162 if (!set)
9163 return isl_stat_error;
9164 if (isl_set_plain_is_empty(set))
9165 return isl_stat_ok;
9167 nparam = isl_set_dim(set, isl_dim_param);
9168 nvar = isl_set_dim(set, isl_dim_set);
9169 if (nparam < 0 || nvar < 0)
9170 return isl_stat_error;
9172 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9174 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9175 fn, user);
9177 free(signs);
9179 return r;
9182 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9184 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9187 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9188 __isl_keep isl_basic_map *bmap2)
9190 isl_bool is_subset;
9191 struct isl_map *map1;
9192 struct isl_map *map2;
9194 if (!bmap1 || !bmap2)
9195 return isl_bool_error;
9197 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9198 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9200 is_subset = isl_map_is_subset(map1, map2);
9202 isl_map_free(map1);
9203 isl_map_free(map2);
9205 return is_subset;
9208 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9209 __isl_keep isl_basic_set *bset2)
9211 return isl_basic_map_is_subset(bset1, bset2);
9214 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9215 __isl_keep isl_basic_map *bmap2)
9217 isl_bool is_subset;
9219 if (!bmap1 || !bmap2)
9220 return isl_bool_error;
9221 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9222 if (is_subset != isl_bool_true)
9223 return is_subset;
9224 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9225 return is_subset;
9228 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9229 __isl_keep isl_basic_set *bset2)
9231 return isl_basic_map_is_equal(
9232 bset_to_bmap(bset1), bset_to_bmap(bset2));
9235 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9237 int i;
9238 int is_empty;
9240 if (!map)
9241 return isl_bool_error;
9242 for (i = 0; i < map->n; ++i) {
9243 is_empty = isl_basic_map_is_empty(map->p[i]);
9244 if (is_empty < 0)
9245 return isl_bool_error;
9246 if (!is_empty)
9247 return isl_bool_false;
9249 return isl_bool_true;
9252 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9254 return map ? map->n == 0 : isl_bool_error;
9257 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9259 return set ? set->n == 0 : isl_bool_error;
9262 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9264 return isl_map_is_empty(set_to_map(set));
9267 #undef TYPE
9268 #define TYPE isl_basic_map
9270 static
9271 #include "isl_type_has_equal_space_bin_templ.c"
9272 #include "isl_type_check_equal_space_templ.c"
9274 /* Check that "bset1" and "bset2" live in the same space,
9275 * reporting an error if they do not.
9277 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9278 __isl_keep isl_basic_set *bset2)
9280 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9281 bset_to_bmap(bset1));
9284 #undef TYPE
9285 #define TYPE isl_map
9287 #include "isl_type_has_equal_space_bin_templ.c"
9288 #include "isl_type_check_equal_space_templ.c"
9289 #include "isl_type_has_space_templ.c"
9291 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9292 __isl_keep isl_set *set2)
9294 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9297 #undef TYPE1
9298 #define TYPE1 isl_map
9299 #undef TYPE2
9300 #define TYPE2 isl_basic_map
9301 #undef TYPE_PAIR
9302 #define TYPE_PAIR isl_map_basic_map
9304 static
9305 #include "isl_type_has_equal_space_templ.c"
9306 #include "isl_type_check_equal_space_templ.c"
9308 /* Check that "set" and "bset" live in the same space,
9309 * reporting an error if they do not.
9311 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9312 __isl_keep isl_basic_set *bset)
9314 return isl_map_basic_map_check_equal_space(set_to_map(set),
9315 bset_to_bmap(bset));
9318 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9320 isl_bool is_subset;
9322 if (!map1 || !map2)
9323 return isl_bool_error;
9324 is_subset = isl_map_is_subset(map1, map2);
9325 if (is_subset != isl_bool_true)
9326 return is_subset;
9327 is_subset = isl_map_is_subset(map2, map1);
9328 return is_subset;
9331 /* Is "map1" equal to "map2"?
9333 * First check if they are obviously equal.
9334 * If not, then perform a more detailed analysis.
9336 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9338 isl_bool equal;
9340 equal = isl_map_plain_is_equal(map1, map2);
9341 if (equal < 0 || equal)
9342 return equal;
9343 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9346 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9347 __isl_keep isl_basic_map *bmap2)
9349 isl_bool is_subset;
9351 if (!bmap1 || !bmap2)
9352 return isl_bool_error;
9353 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9354 if (is_subset != isl_bool_true)
9355 return is_subset;
9356 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9357 return isl_bool_not(is_subset);
9360 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9361 __isl_keep isl_map *map2)
9363 isl_bool is_subset;
9365 if (!map1 || !map2)
9366 return isl_bool_error;
9367 is_subset = isl_map_is_subset(map1, map2);
9368 if (is_subset != isl_bool_true)
9369 return is_subset;
9370 is_subset = isl_map_is_subset(map2, map1);
9371 return isl_bool_not(is_subset);
9374 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9375 __isl_keep isl_set *set2)
9377 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9380 /* Is "bmap" obviously equal to the universe with the same space?
9382 * That is, does it not have any constraints?
9384 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9386 if (!bmap)
9387 return isl_bool_error;
9388 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9391 /* Is "bset" obviously equal to the universe with the same space?
9393 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9395 return isl_basic_map_plain_is_universe(bset);
9398 /* If "c" does not involve any existentially quantified variables,
9399 * then set *univ to false and abort
9401 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9403 isl_bool *univ = user;
9404 isl_size n;
9406 n = isl_constraint_dim(c, isl_dim_div);
9407 if (n < 0)
9408 c = isl_constraint_free(c);
9409 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9410 isl_constraint_free(c);
9411 if (*univ < 0 || !*univ)
9412 return isl_stat_error;
9413 return isl_stat_ok;
9416 /* Is "bmap" equal to the universe with the same space?
9418 * First check if it is obviously equal to the universe.
9419 * If not and if there are any constraints not involving
9420 * existentially quantified variables, then it is certainly
9421 * not equal to the universe.
9422 * Otherwise, check if the universe is a subset of "bmap".
9424 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9426 isl_size n_div;
9427 isl_bool univ;
9428 isl_basic_map *test;
9430 univ = isl_basic_map_plain_is_universe(bmap);
9431 if (univ < 0 || univ)
9432 return univ;
9433 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9434 if (n_div < 0)
9435 return isl_bool_error;
9436 if (n_div == 0)
9437 return isl_bool_false;
9438 univ = isl_bool_true;
9439 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9440 univ)
9441 return isl_bool_error;
9442 if (univ < 0 || !univ)
9443 return univ;
9444 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9445 univ = isl_basic_map_is_subset(test, bmap);
9446 isl_basic_map_free(test);
9447 return univ;
9450 /* Is "bset" equal to the universe with the same space?
9452 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9454 return isl_basic_map_is_universe(bset);
9457 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9459 int i;
9461 if (!map)
9462 return isl_bool_error;
9464 for (i = 0; i < map->n; ++i) {
9465 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9466 if (r < 0 || r)
9467 return r;
9470 return isl_bool_false;
9473 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9475 return isl_map_plain_is_universe(set_to_map(set));
9478 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9480 struct isl_basic_set *bset = NULL;
9481 struct isl_vec *sample = NULL;
9482 isl_bool empty, non_empty;
9484 if (!bmap)
9485 return isl_bool_error;
9487 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9488 return isl_bool_true;
9490 if (isl_basic_map_plain_is_universe(bmap))
9491 return isl_bool_false;
9493 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9494 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9495 copy = isl_basic_map_remove_redundancies(copy);
9496 empty = isl_basic_map_plain_is_empty(copy);
9497 isl_basic_map_free(copy);
9498 return empty;
9501 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9502 if (non_empty < 0)
9503 return isl_bool_error;
9504 if (non_empty)
9505 return isl_bool_false;
9506 isl_vec_free(bmap->sample);
9507 bmap->sample = NULL;
9508 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9509 if (!bset)
9510 return isl_bool_error;
9511 sample = isl_basic_set_sample_vec(bset);
9512 if (!sample)
9513 return isl_bool_error;
9514 empty = sample->size == 0;
9515 isl_vec_free(bmap->sample);
9516 bmap->sample = sample;
9517 if (empty)
9518 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9520 return empty;
9523 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9525 if (!bmap)
9526 return isl_bool_error;
9527 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9530 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9532 if (!bset)
9533 return isl_bool_error;
9534 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9537 /* Is "bmap" known to be non-empty?
9539 * That is, is the cached sample still valid?
9541 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9543 isl_size total;
9545 if (!bmap)
9546 return isl_bool_error;
9547 if (!bmap->sample)
9548 return isl_bool_false;
9549 total = isl_basic_map_dim(bmap, isl_dim_all);
9550 if (total < 0)
9551 return isl_bool_error;
9552 if (bmap->sample->size != 1 + total)
9553 return isl_bool_false;
9554 return isl_basic_map_contains(bmap, bmap->sample);
9557 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9559 return isl_basic_map_is_empty(bset_to_bmap(bset));
9562 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9563 __isl_take isl_basic_map *bmap2)
9565 struct isl_map *map;
9567 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9568 goto error;
9570 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9571 if (!map)
9572 goto error;
9573 map = isl_map_add_basic_map(map, bmap1);
9574 map = isl_map_add_basic_map(map, bmap2);
9575 return map;
9576 error:
9577 isl_basic_map_free(bmap1);
9578 isl_basic_map_free(bmap2);
9579 return NULL;
9582 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9583 __isl_take isl_basic_set *bset2)
9585 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9586 bset_to_bmap(bset2)));
9589 /* Order divs such that any div only depends on previous divs */
9590 __isl_give isl_basic_map *isl_basic_map_order_divs(
9591 __isl_take isl_basic_map *bmap)
9593 int i;
9594 isl_size off;
9596 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9597 if (off < 0)
9598 return isl_basic_map_free(bmap);
9600 for (i = 0; i < bmap->n_div; ++i) {
9601 int pos;
9602 if (isl_int_is_zero(bmap->div[i][0]))
9603 continue;
9604 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9605 bmap->n_div-i);
9606 if (pos == -1)
9607 continue;
9608 if (pos == 0)
9609 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9610 "integer division depends on itself",
9611 return isl_basic_map_free(bmap));
9612 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9613 if (!bmap)
9614 return NULL;
9615 --i;
9617 return bmap;
9620 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9622 int i;
9624 if (!map)
9625 return 0;
9627 for (i = 0; i < map->n; ++i) {
9628 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9629 if (!map->p[i])
9630 goto error;
9633 return map;
9634 error:
9635 isl_map_free(map);
9636 return NULL;
9639 /* Sort the local variables of "bset".
9641 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9642 __isl_take isl_basic_set *bset)
9644 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9647 /* Apply the expansion computed by isl_merge_divs.
9648 * The expansion itself is given by "exp" while the resulting
9649 * list of divs is given by "div".
9651 * Move the integer divisions of "bmap" into the right position
9652 * according to "exp" and then introduce the additional integer
9653 * divisions, adding div constraints.
9654 * The moving should be done first to avoid moving coefficients
9655 * in the definitions of the extra integer divisions.
9657 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9658 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9660 int i, j;
9661 int n_div;
9663 bmap = isl_basic_map_cow(bmap);
9664 if (!bmap || !div)
9665 goto error;
9667 if (div->n_row < bmap->n_div)
9668 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9669 "not an expansion", goto error);
9671 n_div = bmap->n_div;
9672 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9673 2 * (div->n_row - n_div));
9675 for (i = n_div; i < div->n_row; ++i)
9676 if (isl_basic_map_alloc_div(bmap) < 0)
9677 goto error;
9679 for (j = n_div - 1; j >= 0; --j) {
9680 if (exp[j] == j)
9681 break;
9682 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9683 if (!bmap)
9684 goto error;
9686 j = 0;
9687 for (i = 0; i < div->n_row; ++i) {
9688 if (j < n_div && exp[j] == i) {
9689 j++;
9690 } else {
9691 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9692 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9693 continue;
9694 bmap = isl_basic_map_add_div_constraints(bmap, i);
9695 if (!bmap)
9696 goto error;
9700 isl_mat_free(div);
9701 return bmap;
9702 error:
9703 isl_basic_map_free(bmap);
9704 isl_mat_free(div);
9705 return NULL;
9708 /* Apply the expansion computed by isl_merge_divs.
9709 * The expansion itself is given by "exp" while the resulting
9710 * list of divs is given by "div".
9712 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9713 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9715 return isl_basic_map_expand_divs(bset, div, exp);
9718 /* Look for a div in dst that corresponds to the div "div" in src.
9719 * The divs before "div" in src and dst are assumed to be the same.
9721 * Return the position of the corresponding div in dst
9722 * if there is one. Otherwise, return a position beyond the integer divisions.
9723 * Return -1 on error.
9725 static int find_div(__isl_keep isl_basic_map *dst,
9726 __isl_keep isl_basic_map *src, unsigned div)
9728 int i;
9729 isl_size n_div;
9730 isl_size v_div;
9732 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9733 n_div = isl_basic_map_dim(dst, isl_dim_div);
9734 if (n_div < 0 || v_div < 0)
9735 return -1;
9736 isl_assert(dst->ctx, div <= n_div, return -1);
9737 for (i = div; i < n_div; ++i)
9738 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9739 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9740 n_div - div) == -1)
9741 return i;
9742 return n_div;
9745 /* Align the divs of "dst" to those of "src", adding divs from "src"
9746 * if needed. That is, make sure that the first src->n_div divs
9747 * of the result are equal to those of src.
9748 * The integer division of "src" are assumed to be ordered.
9750 * The integer divisions are swapped into the right position
9751 * (possibly after adding them first). This may result
9752 * in the remaining integer divisions appearing in the wrong order,
9753 * i.e., with some integer division appearing before
9754 * some other integer division on which it depends.
9755 * The integer divisions therefore need to be ordered.
9756 * This will not affect the integer divisions aligned to those of "src",
9757 * since "src" is assumed to have ordered integer divisions.
9759 * The result is not finalized as by design it will have redundant
9760 * divs if any divs from "src" were copied.
9762 __isl_give isl_basic_map *isl_basic_map_align_divs(
9763 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9765 int i;
9766 isl_bool known;
9767 int extended;
9768 isl_size v_div;
9769 isl_size dst_n_div;
9771 if (!dst || !src)
9772 return isl_basic_map_free(dst);
9774 if (src->n_div == 0)
9775 return dst;
9777 known = isl_basic_map_divs_known(src);
9778 if (known < 0)
9779 return isl_basic_map_free(dst);
9780 if (!known)
9781 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9782 "some src divs are unknown",
9783 return isl_basic_map_free(dst));
9785 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9786 if (v_div < 0)
9787 return isl_basic_map_free(dst);
9789 extended = 0;
9790 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9791 if (dst_n_div < 0)
9792 dst = isl_basic_map_free(dst);
9793 for (i = 0; i < src->n_div; ++i) {
9794 int j = find_div(dst, src, i);
9795 if (j < 0)
9796 dst = isl_basic_map_free(dst);
9797 if (j == dst_n_div) {
9798 if (!extended) {
9799 int extra = src->n_div - i;
9800 dst = isl_basic_map_cow(dst);
9801 if (!dst)
9802 return isl_basic_map_free(dst);
9803 dst = isl_basic_map_extend(dst,
9804 extra, 0, 2 * extra);
9805 extended = 1;
9807 j = isl_basic_map_alloc_div(dst);
9808 if (j < 0)
9809 return isl_basic_map_free(dst);
9810 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9811 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9812 dst_n_div++;
9813 dst = isl_basic_map_add_div_constraints(dst, j);
9814 if (!dst)
9815 return isl_basic_map_free(dst);
9817 if (j != i)
9818 dst = isl_basic_map_swap_div(dst, i, j);
9819 if (!dst)
9820 return isl_basic_map_free(dst);
9822 return isl_basic_map_order_divs(dst);
9825 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9827 int i;
9829 if (!map)
9830 return NULL;
9831 if (map->n == 0)
9832 return map;
9833 map = isl_map_compute_divs(map);
9834 map = isl_map_order_divs(map);
9835 map = isl_map_cow(map);
9836 if (!map)
9837 return NULL;
9839 for (i = 1; i < map->n; ++i)
9840 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9841 for (i = 1; i < map->n; ++i) {
9842 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9843 if (!map->p[i])
9844 return isl_map_free(map);
9847 map = isl_map_unmark_normalized(map);
9848 return map;
9851 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9853 return isl_map_align_divs_internal(map);
9856 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9858 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9861 /* Align the divs of the basic maps in "map" to those
9862 * of the basic maps in "list", as well as to the other basic maps in "map".
9863 * The elements in "list" are assumed to have known divs.
9865 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9866 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9868 int i;
9869 isl_size n;
9871 n = isl_basic_map_list_n_basic_map(list);
9872 map = isl_map_compute_divs(map);
9873 map = isl_map_cow(map);
9874 if (!map || n < 0)
9875 return isl_map_free(map);
9876 if (map->n == 0)
9877 return map;
9879 for (i = 0; i < n; ++i) {
9880 isl_basic_map *bmap;
9882 bmap = isl_basic_map_list_get_basic_map(list, i);
9883 bmap = isl_basic_map_order_divs(bmap);
9884 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9885 isl_basic_map_free(bmap);
9887 if (!map->p[0])
9888 return isl_map_free(map);
9890 return isl_map_align_divs_internal(map);
9893 /* Align the divs of each element of "list" to those of "bmap".
9894 * Both "bmap" and the elements of "list" are assumed to have known divs.
9896 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9897 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9899 int i;
9900 isl_size n;
9902 n = isl_basic_map_list_n_basic_map(list);
9903 if (n < 0 || !bmap)
9904 return isl_basic_map_list_free(list);
9906 for (i = 0; i < n; ++i) {
9907 isl_basic_map *bmap_i;
9909 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9910 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9911 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9914 return list;
9917 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9918 __isl_take isl_map *map)
9920 isl_bool ok;
9922 isl_map_align_params_set(&map, &set);
9923 ok = isl_map_compatible_domain(map, set);
9924 if (ok < 0)
9925 goto error;
9926 if (!ok)
9927 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9928 "incompatible spaces", goto error);
9929 map = isl_map_intersect_domain(map, set);
9930 set = isl_map_range(map);
9931 return set;
9932 error:
9933 isl_set_free(set);
9934 isl_map_free(map);
9935 return NULL;
9938 /* There is no need to cow as removing empty parts doesn't change
9939 * the meaning of the set.
9941 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9943 int i;
9945 if (!map)
9946 return NULL;
9948 for (i = map->n - 1; i >= 0; --i)
9949 map = remove_if_empty(map, i);
9951 return map;
9954 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9956 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9959 /* Create a binary relation that maps the shared initial "pos" dimensions
9960 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9962 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9963 __isl_keep isl_basic_set *bset2, int pos)
9965 isl_basic_map *bmap1;
9966 isl_basic_map *bmap2;
9968 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9969 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9970 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9971 isl_dim_out, 0, pos);
9972 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9973 isl_dim_out, 0, pos);
9974 return isl_basic_map_range_product(bmap1, bmap2);
9977 /* Given two basic sets bset1 and bset2, compute the maximal difference
9978 * between the values of dimension pos in bset1 and those in bset2
9979 * for any common value of the parameters and dimensions preceding pos.
9981 static enum isl_lp_result basic_set_maximal_difference_at(
9982 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9983 int pos, isl_int *opt)
9985 isl_basic_map *bmap1;
9986 struct isl_ctx *ctx;
9987 struct isl_vec *obj;
9988 isl_size total;
9989 isl_size nparam;
9990 isl_size dim1;
9991 enum isl_lp_result res;
9993 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9994 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9995 if (nparam < 0 || dim1 < 0 || !bset2)
9996 return isl_lp_error;
9998 bmap1 = join_initial(bset1, bset2, pos);
9999 total = isl_basic_map_dim(bmap1, isl_dim_all);
10000 if (total < 0)
10001 return isl_lp_error;
10003 ctx = bmap1->ctx;
10004 obj = isl_vec_alloc(ctx, 1 + total);
10005 if (!obj)
10006 goto error;
10007 isl_seq_clr(obj->block.data, 1 + total);
10008 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10009 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10010 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10011 opt, NULL, NULL);
10012 isl_basic_map_free(bmap1);
10013 isl_vec_free(obj);
10014 return res;
10015 error:
10016 isl_basic_map_free(bmap1);
10017 return isl_lp_error;
10020 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10021 * for any common value of the parameters and dimensions preceding pos
10022 * in both basic sets, the values of dimension pos in bset1 are
10023 * smaller or larger than those in bset2.
10025 * Returns
10026 * 1 if bset1 follows bset2
10027 * -1 if bset1 precedes bset2
10028 * 0 if bset1 and bset2 are incomparable
10029 * -2 if some error occurred.
10031 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10032 __isl_keep isl_basic_set *bset2, int pos)
10034 isl_int opt;
10035 enum isl_lp_result res;
10036 int cmp;
10038 isl_int_init(opt);
10040 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10042 if (res == isl_lp_empty)
10043 cmp = 0;
10044 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10045 res == isl_lp_unbounded)
10046 cmp = 1;
10047 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10048 cmp = -1;
10049 else
10050 cmp = -2;
10052 isl_int_clear(opt);
10053 return cmp;
10056 /* Given two basic sets bset1 and bset2, check whether
10057 * for any common value of the parameters and dimensions preceding pos
10058 * there is a value of dimension pos in bset1 that is larger
10059 * than a value of the same dimension in bset2.
10061 * Return
10062 * 1 if there exists such a pair
10063 * 0 if there is no such pair, but there is a pair of equal values
10064 * -1 otherwise
10065 * -2 if some error occurred.
10067 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10068 __isl_keep isl_basic_set *bset2, int pos)
10070 isl_bool empty;
10071 isl_basic_map *bmap;
10072 isl_size dim1;
10074 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10075 if (dim1 < 0)
10076 return -2;
10077 bmap = join_initial(bset1, bset2, pos);
10078 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10079 isl_dim_out, dim1 - pos);
10080 empty = isl_basic_map_is_empty(bmap);
10081 if (empty < 0)
10082 goto error;
10083 if (empty) {
10084 isl_basic_map_free(bmap);
10085 return -1;
10087 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10088 isl_dim_out, dim1 - pos);
10089 empty = isl_basic_map_is_empty(bmap);
10090 if (empty < 0)
10091 goto error;
10092 isl_basic_map_free(bmap);
10093 if (empty)
10094 return 0;
10095 return 1;
10096 error:
10097 isl_basic_map_free(bmap);
10098 return -2;
10101 /* Given two sets set1 and set2, check whether
10102 * for any common value of the parameters and dimensions preceding pos
10103 * there is a value of dimension pos in set1 that is larger
10104 * than a value of the same dimension in set2.
10106 * Return
10107 * 1 if there exists such a pair
10108 * 0 if there is no such pair, but there is a pair of equal values
10109 * -1 otherwise
10110 * -2 if some error occurred.
10112 int isl_set_follows_at(__isl_keep isl_set *set1,
10113 __isl_keep isl_set *set2, int pos)
10115 int i, j;
10116 int follows = -1;
10118 if (!set1 || !set2)
10119 return -2;
10121 for (i = 0; i < set1->n; ++i)
10122 for (j = 0; j < set2->n; ++j) {
10123 int f;
10124 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10125 if (f == 1 || f == -2)
10126 return f;
10127 if (f > follows)
10128 follows = f;
10131 return follows;
10134 static isl_bool isl_basic_map_plain_has_fixed_var(
10135 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10137 int i;
10138 int d;
10139 isl_size total;
10141 total = isl_basic_map_dim(bmap, isl_dim_all);
10142 if (total < 0)
10143 return isl_bool_error;
10144 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10145 for (; d+1 > pos; --d)
10146 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10147 break;
10148 if (d != pos)
10149 continue;
10150 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10151 return isl_bool_false;
10152 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10153 return isl_bool_false;
10154 if (!isl_int_is_one(bmap->eq[i][1+d]))
10155 return isl_bool_false;
10156 if (val)
10157 isl_int_neg(*val, bmap->eq[i][0]);
10158 return isl_bool_true;
10160 return isl_bool_false;
10163 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10164 unsigned pos, isl_int *val)
10166 int i;
10167 isl_int v;
10168 isl_int tmp;
10169 isl_bool fixed;
10171 if (!map)
10172 return isl_bool_error;
10173 if (map->n == 0)
10174 return isl_bool_false;
10175 if (map->n == 1)
10176 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10177 isl_int_init(v);
10178 isl_int_init(tmp);
10179 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10180 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10181 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10182 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10183 fixed = isl_bool_false;
10185 if (val)
10186 isl_int_set(*val, v);
10187 isl_int_clear(tmp);
10188 isl_int_clear(v);
10189 return fixed;
10192 static isl_bool isl_basic_set_plain_has_fixed_var(
10193 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10195 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10196 pos, val);
10199 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10200 enum isl_dim_type type, unsigned pos, isl_int *val)
10202 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10203 return isl_bool_error;
10204 return isl_basic_map_plain_has_fixed_var(bmap,
10205 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10208 /* If "bmap" obviously lies on a hyperplane where the given dimension
10209 * has a fixed value, then return that value.
10210 * Otherwise return NaN.
10212 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10213 __isl_keep isl_basic_map *bmap,
10214 enum isl_dim_type type, unsigned pos)
10216 isl_ctx *ctx;
10217 isl_val *v;
10218 isl_bool fixed;
10220 if (!bmap)
10221 return NULL;
10222 ctx = isl_basic_map_get_ctx(bmap);
10223 v = isl_val_alloc(ctx);
10224 if (!v)
10225 return NULL;
10226 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10227 if (fixed < 0)
10228 return isl_val_free(v);
10229 if (fixed) {
10230 isl_int_set_si(v->d, 1);
10231 return v;
10233 isl_val_free(v);
10234 return isl_val_nan(ctx);
10237 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10238 enum isl_dim_type type, unsigned pos, isl_int *val)
10240 if (isl_map_check_range(map, type, pos, 1) < 0)
10241 return isl_bool_error;
10242 return isl_map_plain_has_fixed_var(map,
10243 map_offset(map, type) - 1 + pos, val);
10246 /* If "map" obviously lies on a hyperplane where the given dimension
10247 * has a fixed value, then return that value.
10248 * Otherwise return NaN.
10250 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10251 enum isl_dim_type type, unsigned pos)
10253 isl_ctx *ctx;
10254 isl_val *v;
10255 isl_bool fixed;
10257 if (!map)
10258 return NULL;
10259 ctx = isl_map_get_ctx(map);
10260 v = isl_val_alloc(ctx);
10261 if (!v)
10262 return NULL;
10263 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10264 if (fixed < 0)
10265 return isl_val_free(v);
10266 if (fixed) {
10267 isl_int_set_si(v->d, 1);
10268 return v;
10270 isl_val_free(v);
10271 return isl_val_nan(ctx);
10274 /* If "set" obviously lies on a hyperplane where the given dimension
10275 * has a fixed value, then return that value.
10276 * Otherwise return NaN.
10278 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10279 enum isl_dim_type type, unsigned pos)
10281 return isl_map_plain_get_val_if_fixed(set, type, pos);
10284 /* Return a sequence of values in the same space as "set"
10285 * that are equal to the corresponding set dimensions of "set"
10286 * for those set dimensions that obviously lie on a hyperplane
10287 * where the dimension has a fixed value.
10288 * The other elements are set to NaN.
10290 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10291 __isl_keep isl_set *set)
10293 int i;
10294 isl_size n;
10295 isl_space *space;
10296 isl_multi_val *mv;
10298 space = isl_space_drop_all_params(isl_set_get_space(set));
10299 mv = isl_multi_val_alloc(space);
10300 n = isl_multi_val_size(mv);
10301 if (n < 0)
10302 return isl_multi_val_free(mv);
10304 for (i = 0; i < n; ++i) {
10305 isl_val *v;
10307 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10308 mv = isl_multi_val_set_val(mv, i, v);
10311 return mv;
10314 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10315 * then return this fixed value in *val.
10317 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10318 unsigned dim, isl_int *val)
10320 isl_size nparam;
10322 nparam = isl_basic_set_dim(bset, isl_dim_param);
10323 if (nparam < 0)
10324 return isl_bool_error;
10325 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10328 /* Return -1 if the constraint "c1" should be sorted before "c2"
10329 * and 1 if it should be sorted after "c2".
10330 * Return 0 if the two constraints are the same (up to the constant term).
10332 * In particular, if a constraint involves later variables than another
10333 * then it is sorted after this other constraint.
10334 * uset_gist depends on constraints without existentially quantified
10335 * variables sorting first.
10337 * For constraints that have the same latest variable, those
10338 * with the same coefficient for this latest variable (first in absolute value
10339 * and then in actual value) are grouped together.
10340 * This is useful for detecting pairs of constraints that can
10341 * be chained in their printed representation.
10343 * Finally, within a group, constraints are sorted according to
10344 * their coefficients (excluding the constant term).
10346 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10348 isl_int **c1 = (isl_int **) p1;
10349 isl_int **c2 = (isl_int **) p2;
10350 int l1, l2;
10351 unsigned size = *(unsigned *) arg;
10352 int cmp;
10354 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10355 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10357 if (l1 != l2)
10358 return l1 - l2;
10360 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10361 if (cmp != 0)
10362 return cmp;
10363 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10364 if (cmp != 0)
10365 return -cmp;
10367 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10370 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10371 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10372 * and 0 if the two constraints are the same (up to the constant term).
10374 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10375 isl_int *c1, isl_int *c2)
10377 isl_size total;
10378 unsigned size;
10380 total = isl_basic_map_dim(bmap, isl_dim_all);
10381 if (total < 0)
10382 return -2;
10383 size = total;
10384 return sort_constraint_cmp(&c1, &c2, &size);
10387 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10388 __isl_take isl_basic_map *bmap)
10390 isl_size total;
10391 unsigned size;
10393 if (!bmap)
10394 return NULL;
10395 if (bmap->n_ineq == 0)
10396 return bmap;
10397 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10398 return bmap;
10399 total = isl_basic_map_dim(bmap, isl_dim_all);
10400 if (total < 0)
10401 return isl_basic_map_free(bmap);
10402 size = total;
10403 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10404 &sort_constraint_cmp, &size) < 0)
10405 return isl_basic_map_free(bmap);
10406 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10407 return bmap;
10410 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10411 __isl_take isl_basic_set *bset)
10413 isl_basic_map *bmap = bset_to_bmap(bset);
10414 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10417 __isl_give isl_basic_map *isl_basic_map_normalize(
10418 __isl_take isl_basic_map *bmap)
10420 bmap = isl_basic_map_remove_redundancies(bmap);
10421 bmap = isl_basic_map_sort_constraints(bmap);
10422 return bmap;
10424 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10425 __isl_keep isl_basic_map *bmap2)
10427 int i, cmp;
10428 isl_size total;
10429 isl_space *space1, *space2;
10431 if (!bmap1 || !bmap2)
10432 return -1;
10434 if (bmap1 == bmap2)
10435 return 0;
10436 space1 = isl_basic_map_peek_space(bmap1);
10437 space2 = isl_basic_map_peek_space(bmap2);
10438 cmp = isl_space_cmp(space1, space2);
10439 if (cmp)
10440 return cmp;
10441 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10442 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10443 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10444 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10445 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10446 return 0;
10447 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10448 return 1;
10449 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10450 return -1;
10451 if (bmap1->n_eq != bmap2->n_eq)
10452 return bmap1->n_eq - bmap2->n_eq;
10453 if (bmap1->n_ineq != bmap2->n_ineq)
10454 return bmap1->n_ineq - bmap2->n_ineq;
10455 if (bmap1->n_div != bmap2->n_div)
10456 return bmap1->n_div - bmap2->n_div;
10457 total = isl_basic_map_dim(bmap1, isl_dim_all);
10458 if (total < 0)
10459 return -1;
10460 for (i = 0; i < bmap1->n_eq; ++i) {
10461 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10462 if (cmp)
10463 return cmp;
10465 for (i = 0; i < bmap1->n_ineq; ++i) {
10466 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10467 if (cmp)
10468 return cmp;
10470 for (i = 0; i < bmap1->n_div; ++i) {
10471 isl_bool unknown1, unknown2;
10473 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10474 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10475 if (unknown1 < 0 || unknown2 < 0)
10476 return -1;
10477 if (unknown1 && unknown2)
10478 continue;
10479 if (unknown1)
10480 return 1;
10481 if (unknown2)
10482 return -1;
10483 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10484 if (cmp)
10485 return cmp;
10487 return 0;
10490 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10491 __isl_keep isl_basic_set *bset2)
10493 return isl_basic_map_plain_cmp(bset1, bset2);
10496 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10498 int i, cmp;
10500 if (set1 == set2)
10501 return 0;
10502 if (set1->n != set2->n)
10503 return set1->n - set2->n;
10505 for (i = 0; i < set1->n; ++i) {
10506 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10507 if (cmp)
10508 return cmp;
10511 return 0;
10514 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10515 __isl_keep isl_basic_map *bmap2)
10517 if (!bmap1 || !bmap2)
10518 return isl_bool_error;
10519 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10522 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10523 __isl_keep isl_basic_set *bset2)
10525 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10526 bset_to_bmap(bset2));
10529 static int qsort_bmap_cmp(const void *p1, const void *p2)
10531 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10532 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10534 return isl_basic_map_plain_cmp(bmap1, bmap2);
10537 /* Sort the basic maps of "map" and remove duplicate basic maps.
10539 * While removing basic maps, we make sure that the basic maps remain
10540 * sorted because isl_map_normalize expects the basic maps of the result
10541 * to be sorted.
10543 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10545 int i, j;
10547 map = isl_map_remove_empty_parts(map);
10548 if (!map)
10549 return NULL;
10550 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10551 for (i = map->n - 1; i >= 1; --i) {
10552 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10553 continue;
10554 isl_basic_map_free(map->p[i-1]);
10555 for (j = i; j < map->n; ++j)
10556 map->p[j - 1] = map->p[j];
10557 map->n--;
10560 return map;
10563 /* Remove obvious duplicates among the basic maps of "map".
10565 * Unlike isl_map_normalize, this function does not remove redundant
10566 * constraints and only removes duplicates that have exactly the same
10567 * constraints in the input. It does sort the constraints and
10568 * the basic maps to ease the detection of duplicates.
10570 * If "map" has already been normalized or if the basic maps are
10571 * disjoint, then there can be no duplicates.
10573 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10575 int i;
10576 isl_basic_map *bmap;
10578 if (!map)
10579 return NULL;
10580 if (map->n <= 1)
10581 return map;
10582 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10583 return map;
10584 for (i = 0; i < map->n; ++i) {
10585 bmap = isl_basic_map_copy(map->p[i]);
10586 bmap = isl_basic_map_sort_constraints(bmap);
10587 if (!bmap)
10588 return isl_map_free(map);
10589 isl_basic_map_free(map->p[i]);
10590 map->p[i] = bmap;
10593 map = sort_and_remove_duplicates(map);
10594 return map;
10597 /* We normalize in place, but if anything goes wrong we need
10598 * to return NULL, so we need to make sure we don't change the
10599 * meaning of any possible other copies of map.
10601 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10603 int i;
10604 struct isl_basic_map *bmap;
10606 if (!map)
10607 return NULL;
10608 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10609 return map;
10610 for (i = 0; i < map->n; ++i) {
10611 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10612 if (!bmap)
10613 goto error;
10614 isl_basic_map_free(map->p[i]);
10615 map->p[i] = bmap;
10618 map = sort_and_remove_duplicates(map);
10619 if (map)
10620 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10621 return map;
10622 error:
10623 isl_map_free(map);
10624 return NULL;
10627 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10629 return set_from_map(isl_map_normalize(set_to_map(set)));
10632 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10633 __isl_keep isl_map *map2)
10635 int i;
10636 isl_bool equal;
10638 if (!map1 || !map2)
10639 return isl_bool_error;
10641 if (map1 == map2)
10642 return isl_bool_true;
10643 equal = isl_map_has_equal_space(map1, map2);
10644 if (equal < 0 || !equal)
10645 return equal;
10647 map1 = isl_map_copy(map1);
10648 map2 = isl_map_copy(map2);
10649 map1 = isl_map_normalize(map1);
10650 map2 = isl_map_normalize(map2);
10651 if (!map1 || !map2)
10652 goto error;
10653 equal = map1->n == map2->n;
10654 for (i = 0; equal && i < map1->n; ++i) {
10655 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10656 if (equal < 0)
10657 goto error;
10659 isl_map_free(map1);
10660 isl_map_free(map2);
10661 return equal;
10662 error:
10663 isl_map_free(map1);
10664 isl_map_free(map2);
10665 return isl_bool_error;
10668 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10669 __isl_keep isl_set *set2)
10671 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10674 /* Return the basic maps in "map" as a list.
10676 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10677 __isl_keep isl_map *map)
10679 int i;
10680 isl_ctx *ctx;
10681 isl_basic_map_list *list;
10683 if (!map)
10684 return NULL;
10685 ctx = isl_map_get_ctx(map);
10686 list = isl_basic_map_list_alloc(ctx, map->n);
10688 for (i = 0; i < map->n; ++i) {
10689 isl_basic_map *bmap;
10691 bmap = isl_basic_map_copy(map->p[i]);
10692 list = isl_basic_map_list_add(list, bmap);
10695 return list;
10698 /* Return the intersection of the elements in the non-empty list "list".
10699 * All elements are assumed to live in the same space.
10701 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10702 __isl_take isl_basic_map_list *list)
10704 int i;
10705 isl_size n;
10706 isl_basic_map *bmap;
10708 n = isl_basic_map_list_n_basic_map(list);
10709 if (n < 0)
10710 goto error;
10711 if (n < 1)
10712 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10713 "expecting non-empty list", goto error);
10715 bmap = isl_basic_map_list_get_basic_map(list, 0);
10716 for (i = 1; i < n; ++i) {
10717 isl_basic_map *bmap_i;
10719 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10720 bmap = isl_basic_map_intersect(bmap, bmap_i);
10723 isl_basic_map_list_free(list);
10724 return bmap;
10725 error:
10726 isl_basic_map_list_free(list);
10727 return NULL;
10730 /* Return the intersection of the elements in the non-empty list "list".
10731 * All elements are assumed to live in the same space.
10733 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10734 __isl_take isl_basic_set_list *list)
10736 return isl_basic_map_list_intersect(list);
10739 /* Return the union of the elements of "list".
10740 * The list is required to have at least one element.
10742 __isl_give isl_set *isl_basic_set_list_union(
10743 __isl_take isl_basic_set_list *list)
10745 int i;
10746 isl_size n;
10747 isl_space *space;
10748 isl_basic_set *bset;
10749 isl_set *set;
10751 n = isl_basic_set_list_n_basic_set(list);
10752 if (n < 0)
10753 goto error;
10754 if (n < 1)
10755 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10756 "expecting non-empty list", goto error);
10758 bset = isl_basic_set_list_get_basic_set(list, 0);
10759 space = isl_basic_set_get_space(bset);
10760 isl_basic_set_free(bset);
10762 set = isl_set_alloc_space(space, n, 0);
10763 for (i = 0; i < n; ++i) {
10764 bset = isl_basic_set_list_get_basic_set(list, i);
10765 set = isl_set_add_basic_set(set, bset);
10768 isl_basic_set_list_free(list);
10769 return set;
10770 error:
10771 isl_basic_set_list_free(list);
10772 return NULL;
10775 /* Return the union of the elements in the non-empty list "list".
10776 * All elements are assumed to live in the same space.
10778 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10780 int i;
10781 isl_size n;
10782 isl_set *set;
10784 n = isl_set_list_n_set(list);
10785 if (n < 0)
10786 goto error;
10787 if (n < 1)
10788 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10789 "expecting non-empty list", goto error);
10791 set = isl_set_list_get_set(list, 0);
10792 for (i = 1; i < n; ++i) {
10793 isl_set *set_i;
10795 set_i = isl_set_list_get_set(list, i);
10796 set = isl_set_union(set, set_i);
10799 isl_set_list_free(list);
10800 return set;
10801 error:
10802 isl_set_list_free(list);
10803 return NULL;
10806 __isl_give isl_basic_map *isl_basic_map_product(
10807 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10809 isl_space *space_result = NULL;
10810 struct isl_basic_map *bmap;
10811 unsigned in1, in2, out1, out2, nparam, total, pos;
10812 struct isl_dim_map *dim_map1, *dim_map2;
10814 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10815 goto error;
10816 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10817 isl_space_copy(bmap2->dim));
10819 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10820 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10821 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10822 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10823 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10825 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10826 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10827 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10828 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10829 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10830 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10831 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10832 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10833 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10834 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10835 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10837 bmap = isl_basic_map_alloc_space(space_result,
10838 bmap1->n_div + bmap2->n_div,
10839 bmap1->n_eq + bmap2->n_eq,
10840 bmap1->n_ineq + bmap2->n_ineq);
10841 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10842 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10843 bmap = isl_basic_map_simplify(bmap);
10844 return isl_basic_map_finalize(bmap);
10845 error:
10846 isl_basic_map_free(bmap1);
10847 isl_basic_map_free(bmap2);
10848 return NULL;
10851 __isl_give isl_basic_map *isl_basic_map_flat_product(
10852 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10854 isl_basic_map *prod;
10856 prod = isl_basic_map_product(bmap1, bmap2);
10857 prod = isl_basic_map_flatten(prod);
10858 return prod;
10861 __isl_give isl_basic_set *isl_basic_set_flat_product(
10862 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10864 return isl_basic_map_flat_range_product(bset1, bset2);
10867 __isl_give isl_basic_map *isl_basic_map_domain_product(
10868 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10870 isl_space *space1, *space2;
10871 isl_space *space_result = NULL;
10872 isl_basic_map *bmap;
10873 isl_size in1, in2, out, nparam;
10874 unsigned total, pos;
10875 struct isl_dim_map *dim_map1, *dim_map2;
10877 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10878 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10879 out = isl_basic_map_dim(bmap1, isl_dim_out);
10880 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10881 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10882 goto error;
10884 space1 = isl_basic_map_get_space(bmap1);
10885 space2 = isl_basic_map_get_space(bmap2);
10886 space_result = isl_space_domain_product(space1, space2);
10888 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10889 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10890 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10895 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10896 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10897 isl_dim_map_div(dim_map1, bmap1, pos += out);
10898 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10900 bmap = isl_basic_map_alloc_space(space_result,
10901 bmap1->n_div + bmap2->n_div,
10902 bmap1->n_eq + bmap2->n_eq,
10903 bmap1->n_ineq + bmap2->n_ineq);
10904 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10905 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10906 bmap = isl_basic_map_simplify(bmap);
10907 return isl_basic_map_finalize(bmap);
10908 error:
10909 isl_basic_map_free(bmap1);
10910 isl_basic_map_free(bmap2);
10911 return NULL;
10914 __isl_give isl_basic_map *isl_basic_map_range_product(
10915 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10917 isl_bool rational;
10918 isl_space *space_result = NULL;
10919 isl_basic_map *bmap;
10920 isl_size in, out1, out2, nparam;
10921 unsigned total, pos;
10922 struct isl_dim_map *dim_map1, *dim_map2;
10924 rational = isl_basic_map_is_rational(bmap1);
10925 if (rational >= 0 && rational)
10926 rational = isl_basic_map_is_rational(bmap2);
10927 in = isl_basic_map_dim(bmap1, isl_dim_in);
10928 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10929 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10930 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10931 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10932 goto error;
10934 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10935 goto error;
10937 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10938 isl_space_copy(bmap2->dim));
10940 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10941 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10942 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10943 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10944 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10949 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10950 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10952 bmap = isl_basic_map_alloc_space(space_result,
10953 bmap1->n_div + bmap2->n_div,
10954 bmap1->n_eq + bmap2->n_eq,
10955 bmap1->n_ineq + bmap2->n_ineq);
10956 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10958 if (rational)
10959 bmap = isl_basic_map_set_rational(bmap);
10960 bmap = isl_basic_map_simplify(bmap);
10961 return isl_basic_map_finalize(bmap);
10962 error:
10963 isl_basic_map_free(bmap1);
10964 isl_basic_map_free(bmap2);
10965 return NULL;
10968 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10969 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10971 isl_basic_map *prod;
10973 prod = isl_basic_map_range_product(bmap1, bmap2);
10974 prod = isl_basic_map_flatten_range(prod);
10975 return prod;
10978 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10979 * and collect the results.
10980 * The result live in the space obtained by calling "space_product"
10981 * on the spaces of "map1" and "map2".
10982 * If "remove_duplicates" is set then the result may contain duplicates
10983 * (even if the inputs do not) and so we try and remove the obvious
10984 * duplicates.
10986 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10987 __isl_take isl_map *map2,
10988 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10989 __isl_take isl_space *right),
10990 __isl_give isl_basic_map *(*basic_map_product)(
10991 __isl_take isl_basic_map *left,
10992 __isl_take isl_basic_map *right),
10993 int remove_duplicates)
10995 unsigned flags = 0;
10996 struct isl_map *result;
10997 int i, j;
10998 isl_bool m;
11000 m = isl_map_has_equal_params(map1, map2);
11001 if (m < 0)
11002 goto error;
11003 if (!m)
11004 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11005 "parameters don't match", goto error);
11007 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11008 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11009 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11011 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11012 isl_space_copy(map2->dim)),
11013 map1->n * map2->n, flags);
11014 if (!result)
11015 goto error;
11016 for (i = 0; i < map1->n; ++i)
11017 for (j = 0; j < map2->n; ++j) {
11018 struct isl_basic_map *part;
11019 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11020 isl_basic_map_copy(map2->p[j]));
11021 if (isl_basic_map_is_empty(part))
11022 isl_basic_map_free(part);
11023 else
11024 result = isl_map_add_basic_map(result, part);
11025 if (!result)
11026 goto error;
11028 if (remove_duplicates)
11029 result = isl_map_remove_obvious_duplicates(result);
11030 isl_map_free(map1);
11031 isl_map_free(map2);
11032 return result;
11033 error:
11034 isl_map_free(map1);
11035 isl_map_free(map2);
11036 return NULL;
11039 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11041 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11042 __isl_take isl_map *map2)
11044 isl_map_align_params_bin(&map1, &map2);
11045 return map_product(map1, map2, &isl_space_product,
11046 &isl_basic_map_product, 0);
11049 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11051 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11052 __isl_take isl_map *map2)
11054 isl_map *prod;
11056 prod = isl_map_product(map1, map2);
11057 prod = isl_map_flatten(prod);
11058 return prod;
11061 /* Given two set A and B, construct its Cartesian product A x B.
11063 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11064 __isl_take isl_set *set2)
11066 return isl_map_range_product(set1, set2);
11069 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11070 __isl_take isl_set *set2)
11072 return isl_map_flat_range_product(set1, set2);
11075 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11077 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11078 __isl_take isl_map *map2)
11080 isl_map_align_params_bin(&map1, &map2);
11081 return map_product(map1, map2, &isl_space_domain_product,
11082 &isl_basic_map_domain_product, 1);
11085 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11087 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11088 __isl_take isl_map *map2)
11090 isl_map_align_params_bin(&map1, &map2);
11091 return map_product(map1, map2, &isl_space_range_product,
11092 &isl_basic_map_range_product, 1);
11095 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11097 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11099 isl_space *space;
11100 isl_size total1, keep1, total2, keep2;
11102 total1 = isl_map_dim(map, isl_dim_in);
11103 total2 = isl_map_dim(map, isl_dim_out);
11104 if (total1 < 0 || total2 < 0)
11105 return isl_map_free(map);
11106 if (!isl_space_domain_is_wrapping(map->dim) ||
11107 !isl_space_range_is_wrapping(map->dim))
11108 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11109 "not a product", return isl_map_free(map));
11111 space = isl_map_get_space(map);
11112 space = isl_space_factor_domain(space);
11113 keep1 = isl_space_dim(space, isl_dim_in);
11114 keep2 = isl_space_dim(space, isl_dim_out);
11115 if (keep1 < 0 || keep2 < 0)
11116 map = isl_map_free(map);
11117 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11118 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11119 map = isl_map_reset_space(map, space);
11121 return map;
11124 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11126 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11128 isl_space *space;
11129 isl_size total1, keep1, total2, keep2;
11131 total1 = isl_map_dim(map, isl_dim_in);
11132 total2 = isl_map_dim(map, isl_dim_out);
11133 if (total1 < 0 || total2 < 0)
11134 return isl_map_free(map);
11135 if (!isl_space_domain_is_wrapping(map->dim) ||
11136 !isl_space_range_is_wrapping(map->dim))
11137 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11138 "not a product", return isl_map_free(map));
11140 space = isl_map_get_space(map);
11141 space = isl_space_factor_range(space);
11142 keep1 = isl_space_dim(space, isl_dim_in);
11143 keep2 = isl_space_dim(space, isl_dim_out);
11144 if (keep1 < 0 || keep2 < 0)
11145 map = isl_map_free(map);
11146 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11147 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11148 map = isl_map_reset_space(map, space);
11150 return map;
11153 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11155 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11157 isl_space *space;
11158 isl_size total, keep;
11160 total = isl_map_dim(map, isl_dim_in);
11161 if (total < 0)
11162 return isl_map_free(map);
11163 if (!isl_space_domain_is_wrapping(map->dim))
11164 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11165 "domain is not a product", return isl_map_free(map));
11167 space = isl_map_get_space(map);
11168 space = isl_space_domain_factor_domain(space);
11169 keep = isl_space_dim(space, isl_dim_in);
11170 if (keep < 0)
11171 map = isl_map_free(map);
11172 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11173 map = isl_map_reset_space(map, space);
11175 return map;
11178 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11180 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11182 isl_space *space;
11183 isl_size total, keep;
11185 total = isl_map_dim(map, isl_dim_in);
11186 if (total < 0)
11187 return isl_map_free(map);
11188 if (!isl_space_domain_is_wrapping(map->dim))
11189 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11190 "domain is not a product", return isl_map_free(map));
11192 space = isl_map_get_space(map);
11193 space = isl_space_domain_factor_range(space);
11194 keep = isl_space_dim(space, isl_dim_in);
11195 if (keep < 0)
11196 map = isl_map_free(map);
11197 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11198 map = isl_map_reset_space(map, space);
11200 return map;
11203 /* Given a map A -> [B -> C], extract the map A -> B.
11205 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11207 isl_space *space;
11208 isl_size total, keep;
11210 total = isl_map_dim(map, isl_dim_out);
11211 if (total < 0)
11212 return isl_map_free(map);
11213 if (!isl_space_range_is_wrapping(map->dim))
11214 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11215 "range is not a product", return isl_map_free(map));
11217 space = isl_map_get_space(map);
11218 space = isl_space_range_factor_domain(space);
11219 keep = isl_space_dim(space, isl_dim_out);
11220 if (keep < 0)
11221 map = isl_map_free(map);
11222 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11223 map = isl_map_reset_space(map, space);
11225 return map;
11228 /* Given a map A -> [B -> C], extract the map A -> C.
11230 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11232 isl_space *space;
11233 isl_size total, keep;
11235 total = isl_map_dim(map, isl_dim_out);
11236 if (total < 0)
11237 return isl_map_free(map);
11238 if (!isl_space_range_is_wrapping(map->dim))
11239 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11240 "range is not a product", return isl_map_free(map));
11242 space = isl_map_get_space(map);
11243 space = isl_space_range_factor_range(space);
11244 keep = isl_space_dim(space, isl_dim_out);
11245 if (keep < 0)
11246 map = isl_map_free(map);
11247 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11248 map = isl_map_reset_space(map, space);
11250 return map;
11253 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11255 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11256 __isl_take isl_map *map2)
11258 isl_map *prod;
11260 prod = isl_map_domain_product(map1, map2);
11261 prod = isl_map_flatten_domain(prod);
11262 return prod;
11265 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11267 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11268 __isl_take isl_map *map2)
11270 isl_map *prod;
11272 prod = isl_map_range_product(map1, map2);
11273 prod = isl_map_flatten_range(prod);
11274 return prod;
11277 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11279 int i;
11280 uint32_t hash = isl_hash_init();
11281 isl_size total;
11283 if (!bmap)
11284 return 0;
11285 bmap = isl_basic_map_copy(bmap);
11286 bmap = isl_basic_map_normalize(bmap);
11287 total = isl_basic_map_dim(bmap, isl_dim_all);
11288 if (total < 0)
11289 return 0;
11290 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11291 for (i = 0; i < bmap->n_eq; ++i) {
11292 uint32_t c_hash;
11293 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11294 isl_hash_hash(hash, c_hash);
11296 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11297 for (i = 0; i < bmap->n_ineq; ++i) {
11298 uint32_t c_hash;
11299 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11300 isl_hash_hash(hash, c_hash);
11302 isl_hash_byte(hash, bmap->n_div & 0xFF);
11303 for (i = 0; i < bmap->n_div; ++i) {
11304 uint32_t c_hash;
11305 if (isl_int_is_zero(bmap->div[i][0]))
11306 continue;
11307 isl_hash_byte(hash, i & 0xFF);
11308 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11309 isl_hash_hash(hash, c_hash);
11311 isl_basic_map_free(bmap);
11312 return hash;
11315 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11317 return isl_basic_map_get_hash(bset_to_bmap(bset));
11320 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11322 int i;
11323 uint32_t hash;
11325 if (!map)
11326 return 0;
11327 map = isl_map_copy(map);
11328 map = isl_map_normalize(map);
11329 if (!map)
11330 return 0;
11332 hash = isl_hash_init();
11333 for (i = 0; i < map->n; ++i) {
11334 uint32_t bmap_hash;
11335 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11336 isl_hash_hash(hash, bmap_hash);
11339 isl_map_free(map);
11341 return hash;
11344 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11346 return isl_map_get_hash(set_to_map(set));
11349 /* Return the number of basic maps in the (current) representation of "map".
11351 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11353 return map ? map->n : isl_size_error;
11356 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11358 return set ? set->n : isl_size_error;
11361 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11362 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11364 int i;
11366 if (!map)
11367 return isl_stat_error;
11369 for (i = 0; i < map->n; ++i)
11370 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11371 return isl_stat_error;
11373 return isl_stat_ok;
11376 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11377 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11379 int i;
11381 if (!set)
11382 return isl_stat_error;
11384 for (i = 0; i < set->n; ++i)
11385 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11386 return isl_stat_error;
11388 return isl_stat_ok;
11391 /* Does "test" succeed on every basic set in "set"?
11393 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11394 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11395 void *user)
11397 int i;
11399 if (!set)
11400 return isl_bool_error;
11402 for (i = 0; i < set->n; ++i) {
11403 isl_bool r;
11405 r = test(set->p[i], user);
11406 if (r < 0 || !r)
11407 return r;
11410 return isl_bool_true;
11413 /* Return a list of basic sets, the union of which is equal to "set".
11415 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11416 __isl_keep isl_set *set)
11418 int i;
11419 isl_basic_set_list *list;
11421 if (!set)
11422 return NULL;
11424 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11425 for (i = 0; i < set->n; ++i) {
11426 isl_basic_set *bset;
11428 bset = isl_basic_set_copy(set->p[i]);
11429 list = isl_basic_set_list_add(list, bset);
11432 return list;
11435 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11437 isl_space *space;
11439 if (!bset)
11440 return NULL;
11442 bset = isl_basic_set_cow(bset);
11443 if (!bset)
11444 return NULL;
11446 space = isl_basic_set_get_space(bset);
11447 space = isl_space_lift(space, bset->n_div);
11448 if (!space)
11449 goto error;
11450 isl_space_free(bset->dim);
11451 bset->dim = space;
11452 bset->extra -= bset->n_div;
11453 bset->n_div = 0;
11455 bset = isl_basic_set_finalize(bset);
11457 return bset;
11458 error:
11459 isl_basic_set_free(bset);
11460 return NULL;
11463 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11465 int i;
11466 isl_space *space;
11467 unsigned n_div;
11469 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11471 if (!set)
11472 return NULL;
11474 set = isl_set_cow(set);
11475 if (!set)
11476 return NULL;
11478 n_div = set->p[0]->n_div;
11479 space = isl_set_get_space(set);
11480 space = isl_space_lift(space, n_div);
11481 if (!space)
11482 goto error;
11483 isl_space_free(set->dim);
11484 set->dim = space;
11486 for (i = 0; i < set->n; ++i) {
11487 set->p[i] = isl_basic_set_lift(set->p[i]);
11488 if (!set->p[i])
11489 goto error;
11492 return set;
11493 error:
11494 isl_set_free(set);
11495 return NULL;
11498 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11500 isl_size dim;
11501 int size = 0;
11503 dim = isl_basic_set_dim(bset, isl_dim_all);
11504 if (dim < 0)
11505 return -1;
11506 size += bset->n_eq * (1 + dim);
11507 size += bset->n_ineq * (1 + dim);
11508 size += bset->n_div * (2 + dim);
11510 return size;
11513 int isl_set_size(__isl_keep isl_set *set)
11515 int i;
11516 int size = 0;
11518 if (!set)
11519 return -1;
11521 for (i = 0; i < set->n; ++i)
11522 size += isl_basic_set_size(set->p[i]);
11524 return size;
11527 /* Check if there is any lower bound (if lower == 0) and/or upper
11528 * bound (if upper == 0) on the specified dim.
11530 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11531 enum isl_dim_type type, unsigned pos, int lower, int upper)
11533 int i;
11535 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11536 return isl_bool_error;
11538 pos += isl_basic_map_offset(bmap, type);
11540 for (i = 0; i < bmap->n_div; ++i) {
11541 if (isl_int_is_zero(bmap->div[i][0]))
11542 continue;
11543 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11544 return isl_bool_true;
11547 for (i = 0; i < bmap->n_eq; ++i)
11548 if (!isl_int_is_zero(bmap->eq[i][pos]))
11549 return isl_bool_true;
11551 for (i = 0; i < bmap->n_ineq; ++i) {
11552 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11553 if (sgn > 0)
11554 lower = 1;
11555 if (sgn < 0)
11556 upper = 1;
11559 return lower && upper;
11562 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11563 enum isl_dim_type type, unsigned pos)
11565 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11568 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11569 enum isl_dim_type type, unsigned pos)
11571 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11574 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11575 enum isl_dim_type type, unsigned pos)
11577 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11580 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11581 enum isl_dim_type type, unsigned pos)
11583 int i;
11585 if (!map)
11586 return isl_bool_error;
11588 for (i = 0; i < map->n; ++i) {
11589 isl_bool bounded;
11590 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11591 if (bounded < 0 || !bounded)
11592 return bounded;
11595 return isl_bool_true;
11598 /* Return true if the specified dim is involved in both an upper bound
11599 * and a lower bound.
11601 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11602 enum isl_dim_type type, unsigned pos)
11604 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11607 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11609 static isl_bool has_any_bound(__isl_keep isl_map *map,
11610 enum isl_dim_type type, unsigned pos,
11611 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11612 enum isl_dim_type type, unsigned pos))
11614 int i;
11616 if (!map)
11617 return isl_bool_error;
11619 for (i = 0; i < map->n; ++i) {
11620 isl_bool bounded;
11621 bounded = fn(map->p[i], type, pos);
11622 if (bounded < 0 || bounded)
11623 return bounded;
11626 return isl_bool_false;
11629 /* Return 1 if the specified dim is involved in any lower bound.
11631 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11632 enum isl_dim_type type, unsigned pos)
11634 return has_any_bound(set, type, pos,
11635 &isl_basic_map_dim_has_lower_bound);
11638 /* Return 1 if the specified dim is involved in any upper bound.
11640 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11641 enum isl_dim_type type, unsigned pos)
11643 return has_any_bound(set, type, pos,
11644 &isl_basic_map_dim_has_upper_bound);
11647 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11649 static isl_bool has_bound(__isl_keep isl_map *map,
11650 enum isl_dim_type type, unsigned pos,
11651 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11652 enum isl_dim_type type, unsigned pos))
11654 int i;
11656 if (!map)
11657 return isl_bool_error;
11659 for (i = 0; i < map->n; ++i) {
11660 isl_bool bounded;
11661 bounded = fn(map->p[i], type, pos);
11662 if (bounded < 0 || !bounded)
11663 return bounded;
11666 return isl_bool_true;
11669 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11671 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11672 enum isl_dim_type type, unsigned pos)
11674 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11677 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11679 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11680 enum isl_dim_type type, unsigned pos)
11682 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11685 /* For each of the "n" variables starting at "first", determine
11686 * the sign of the variable and put the results in the first "n"
11687 * elements of the array "signs".
11688 * Sign
11689 * 1 means that the variable is non-negative
11690 * -1 means that the variable is non-positive
11691 * 0 means the variable attains both positive and negative values.
11693 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11694 unsigned first, unsigned n, int *signs)
11696 isl_vec *bound = NULL;
11697 struct isl_tab *tab = NULL;
11698 struct isl_tab_undo *snap;
11699 int i;
11700 isl_size total;
11702 total = isl_basic_set_dim(bset, isl_dim_all);
11703 if (total < 0 || !signs)
11704 return isl_stat_error;
11706 bound = isl_vec_alloc(bset->ctx, 1 + total);
11707 tab = isl_tab_from_basic_set(bset, 0);
11708 if (!bound || !tab)
11709 goto error;
11711 isl_seq_clr(bound->el, bound->size);
11712 isl_int_set_si(bound->el[0], -1);
11714 snap = isl_tab_snap(tab);
11715 for (i = 0; i < n; ++i) {
11716 int empty;
11718 isl_int_set_si(bound->el[1 + first + i], -1);
11719 if (isl_tab_add_ineq(tab, bound->el) < 0)
11720 goto error;
11721 empty = tab->empty;
11722 isl_int_set_si(bound->el[1 + first + i], 0);
11723 if (isl_tab_rollback(tab, snap) < 0)
11724 goto error;
11726 if (empty) {
11727 signs[i] = 1;
11728 continue;
11731 isl_int_set_si(bound->el[1 + first + i], 1);
11732 if (isl_tab_add_ineq(tab, bound->el) < 0)
11733 goto error;
11734 empty = tab->empty;
11735 isl_int_set_si(bound->el[1 + first + i], 0);
11736 if (isl_tab_rollback(tab, snap) < 0)
11737 goto error;
11739 signs[i] = empty ? -1 : 0;
11742 isl_tab_free(tab);
11743 isl_vec_free(bound);
11744 return isl_stat_ok;
11745 error:
11746 isl_tab_free(tab);
11747 isl_vec_free(bound);
11748 return isl_stat_error;
11751 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11752 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11754 if (!bset || !signs)
11755 return isl_stat_error;
11756 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11757 return isl_stat_error;
11759 first += pos(bset->dim, type) - 1;
11760 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11763 /* Is it possible for the integer division "div" to depend (possibly
11764 * indirectly) on any output dimensions?
11766 * If the div is undefined, then we conservatively assume that it
11767 * may depend on them.
11768 * Otherwise, we check if it actually depends on them or on any integer
11769 * divisions that may depend on them.
11771 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11773 int i;
11774 isl_size n_out, n_div;
11775 unsigned o_out, o_div;
11777 if (isl_int_is_zero(bmap->div[div][0]))
11778 return isl_bool_true;
11780 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11781 if (n_out < 0)
11782 return isl_bool_error;
11783 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11785 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11786 return isl_bool_true;
11788 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11789 if (n_div < 0)
11790 return isl_bool_error;
11791 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11793 for (i = 0; i < n_div; ++i) {
11794 isl_bool may_involve;
11796 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11797 continue;
11798 may_involve = div_may_involve_output(bmap, i);
11799 if (may_involve < 0 || may_involve)
11800 return may_involve;
11803 return isl_bool_false;
11806 /* Return the first integer division of "bmap" in the range
11807 * [first, first + n[ that may depend on any output dimensions and
11808 * that has a non-zero coefficient in "c" (where the first coefficient
11809 * in "c" corresponds to integer division "first").
11811 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11812 isl_int *c, int first, int n)
11814 int k;
11816 if (!bmap)
11817 return -1;
11819 for (k = first; k < first + n; ++k) {
11820 isl_bool may_involve;
11822 if (isl_int_is_zero(c[k]))
11823 continue;
11824 may_involve = div_may_involve_output(bmap, k);
11825 if (may_involve < 0)
11826 return -1;
11827 if (may_involve)
11828 return k;
11831 return first + n;
11834 /* Look for a pair of inequality constraints in "bmap" of the form
11836 * -l + i >= 0 or i >= l
11837 * and
11838 * n + l - i >= 0 or i <= l + n
11840 * with n < "m" and i the output dimension at position "pos".
11841 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11842 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11843 * and earlier output dimensions, as well as integer divisions that do
11844 * not involve any of the output dimensions.
11846 * Return the index of the first inequality constraint or bmap->n_ineq
11847 * if no such pair can be found.
11849 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11850 int pos, isl_int m)
11852 int i, j;
11853 isl_ctx *ctx;
11854 isl_size total;
11855 isl_size n_div, n_out;
11856 unsigned o_div, o_out;
11857 int less;
11859 total = isl_basic_map_dim(bmap, isl_dim_all);
11860 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11861 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11862 if (total < 0 || n_out < 0 || n_div < 0)
11863 return -1;
11865 ctx = isl_basic_map_get_ctx(bmap);
11866 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11867 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11868 for (i = 0; i < bmap->n_ineq; ++i) {
11869 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11870 continue;
11871 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11872 n_out - (pos + 1)) != -1)
11873 continue;
11874 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11875 0, n_div) < n_div)
11876 continue;
11877 for (j = i + 1; j < bmap->n_ineq; ++j) {
11878 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11879 ctx->one))
11880 continue;
11881 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11882 bmap->ineq[j] + 1, total))
11883 continue;
11884 break;
11886 if (j >= bmap->n_ineq)
11887 continue;
11888 isl_int_add(bmap->ineq[i][0],
11889 bmap->ineq[i][0], bmap->ineq[j][0]);
11890 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11891 isl_int_sub(bmap->ineq[i][0],
11892 bmap->ineq[i][0], bmap->ineq[j][0]);
11893 if (!less)
11894 continue;
11895 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11896 return i;
11897 else
11898 return j;
11901 return bmap->n_ineq;
11904 /* Return the index of the equality of "bmap" that defines
11905 * the output dimension "pos" in terms of earlier dimensions.
11906 * The equality may also involve integer divisions, as long
11907 * as those integer divisions are defined in terms of
11908 * parameters or input dimensions.
11909 * In this case, *div is set to the number of integer divisions and
11910 * *ineq is set to the number of inequality constraints (provided
11911 * div and ineq are not NULL).
11913 * The equality may also involve a single integer division involving
11914 * the output dimensions (typically only output dimension "pos") as
11915 * long as the coefficient of output dimension "pos" is 1 or -1 and
11916 * there is a pair of constraints i >= l and i <= l + n, with i referring
11917 * to output dimension "pos", l an expression involving only earlier
11918 * dimensions and n smaller than the coefficient of the integer division
11919 * in the equality. In this case, the output dimension can be defined
11920 * in terms of a modulo expression that does not involve the integer division.
11921 * *div is then set to this single integer division and
11922 * *ineq is set to the index of constraint i >= l.
11924 * Return bmap->n_eq if there is no such equality.
11925 * Return -1 on error.
11927 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11928 int pos, int *div, int *ineq)
11930 int j, k, l;
11931 isl_size n_div, n_out;
11932 unsigned o_div, o_out;
11934 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11935 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11936 if (n_out < 0 || n_div < 0)
11937 return -1;
11939 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11940 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11942 if (ineq)
11943 *ineq = bmap->n_ineq;
11944 if (div)
11945 *div = n_div;
11946 for (j = 0; j < bmap->n_eq; ++j) {
11947 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11948 continue;
11949 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11950 n_out - (pos + 1)) != -1)
11951 continue;
11952 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11953 0, n_div);
11954 if (k >= n_div)
11955 return j;
11956 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11957 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11958 continue;
11959 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11960 k + 1, n_div - (k+1)) < n_div)
11961 continue;
11962 l = find_modulo_constraint_pair(bmap, pos,
11963 bmap->eq[j][o_div + k]);
11964 if (l < 0)
11965 return -1;
11966 if (l >= bmap->n_ineq)
11967 continue;
11968 if (div)
11969 *div = k;
11970 if (ineq)
11971 *ineq = l;
11972 return j;
11975 return bmap->n_eq;
11978 /* Check if the given basic map is obviously single-valued.
11979 * In particular, for each output dimension, check that there is
11980 * an equality that defines the output dimension in terms of
11981 * earlier dimensions.
11983 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11985 int i;
11986 isl_size n_out;
11988 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11989 if (n_out < 0)
11990 return isl_bool_error;
11992 for (i = 0; i < n_out; ++i) {
11993 int eq;
11995 eq = isl_basic_map_output_defining_equality(bmap, i,
11996 NULL, NULL);
11997 if (eq < 0)
11998 return isl_bool_error;
11999 if (eq >= bmap->n_eq)
12000 return isl_bool_false;
12003 return isl_bool_true;
12006 /* Check if the given basic map is single-valued.
12007 * We simply compute
12009 * M \circ M^-1
12011 * and check if the result is a subset of the identity mapping.
12013 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12015 isl_space *space;
12016 isl_basic_map *test;
12017 isl_basic_map *id;
12018 isl_bool sv;
12020 sv = isl_basic_map_plain_is_single_valued(bmap);
12021 if (sv < 0 || sv)
12022 return sv;
12024 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12025 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12027 space = isl_basic_map_get_space(bmap);
12028 space = isl_space_map_from_set(isl_space_range(space));
12029 id = isl_basic_map_identity(space);
12031 sv = isl_basic_map_is_subset(test, id);
12033 isl_basic_map_free(test);
12034 isl_basic_map_free(id);
12036 return sv;
12039 /* Check if the given map is obviously single-valued.
12041 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12043 if (!map)
12044 return isl_bool_error;
12045 if (map->n == 0)
12046 return isl_bool_true;
12047 if (map->n >= 2)
12048 return isl_bool_false;
12050 return isl_basic_map_plain_is_single_valued(map->p[0]);
12053 /* Check if the given map is single-valued.
12054 * We simply compute
12056 * M \circ M^-1
12058 * and check if the result is a subset of the identity mapping.
12060 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12062 isl_space *space;
12063 isl_map *test;
12064 isl_map *id;
12065 isl_bool sv;
12067 sv = isl_map_plain_is_single_valued(map);
12068 if (sv < 0 || sv)
12069 return sv;
12071 test = isl_map_reverse(isl_map_copy(map));
12072 test = isl_map_apply_range(test, isl_map_copy(map));
12074 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12075 id = isl_map_identity(space);
12077 sv = isl_map_is_subset(test, id);
12079 isl_map_free(test);
12080 isl_map_free(id);
12082 return sv;
12085 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12087 isl_bool in;
12089 map = isl_map_copy(map);
12090 map = isl_map_reverse(map);
12091 in = isl_map_is_single_valued(map);
12092 isl_map_free(map);
12094 return in;
12097 /* Check if the given map is obviously injective.
12099 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12101 isl_bool in;
12103 map = isl_map_copy(map);
12104 map = isl_map_reverse(map);
12105 in = isl_map_plain_is_single_valued(map);
12106 isl_map_free(map);
12108 return in;
12111 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12113 isl_bool sv;
12115 sv = isl_map_is_single_valued(map);
12116 if (sv < 0 || !sv)
12117 return sv;
12119 return isl_map_is_injective(map);
12122 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12124 return isl_map_is_single_valued(set_to_map(set));
12127 /* Does "map" only map elements to themselves?
12129 * If the domain and range spaces are different, then "map"
12130 * is considered not to be an identity relation, even if it is empty.
12131 * Otherwise, construct the maximal identity relation and
12132 * check whether "map" is a subset of this relation.
12134 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12136 isl_map *id;
12137 isl_bool equal, is_identity;
12139 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12140 if (equal < 0 || !equal)
12141 return equal;
12143 id = isl_map_identity(isl_map_get_space(map));
12144 is_identity = isl_map_is_subset(map, id);
12145 isl_map_free(id);
12147 return is_identity;
12150 int isl_map_is_translation(__isl_keep isl_map *map)
12152 int ok;
12153 isl_set *delta;
12155 delta = isl_map_deltas(isl_map_copy(map));
12156 ok = isl_set_is_singleton(delta);
12157 isl_set_free(delta);
12159 return ok;
12162 static int unique(isl_int *p, unsigned pos, unsigned len)
12164 if (isl_seq_first_non_zero(p, pos) != -1)
12165 return 0;
12166 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12167 return 0;
12168 return 1;
12171 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12173 int i, j;
12174 isl_size nvar, ovar, n_div;
12176 n_div = isl_basic_set_dim(bset, isl_dim_div);
12177 if (n_div < 0)
12178 return isl_bool_error;
12179 if (n_div != 0)
12180 return isl_bool_false;
12182 nvar = isl_basic_set_dim(bset, isl_dim_set);
12183 ovar = isl_space_offset(bset->dim, isl_dim_set);
12184 if (nvar < 0 || ovar < 0)
12185 return isl_bool_error;
12186 for (j = 0; j < nvar; ++j) {
12187 int lower = 0, upper = 0;
12188 for (i = 0; i < bset->n_eq; ++i) {
12189 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12190 continue;
12191 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12192 return isl_bool_false;
12193 break;
12195 if (i < bset->n_eq)
12196 continue;
12197 for (i = 0; i < bset->n_ineq; ++i) {
12198 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12199 continue;
12200 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12201 return isl_bool_false;
12202 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12203 lower = 1;
12204 else
12205 upper = 1;
12207 if (!lower || !upper)
12208 return isl_bool_false;
12211 return isl_bool_true;
12214 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12216 if (!set)
12217 return isl_bool_error;
12218 if (set->n != 1)
12219 return isl_bool_false;
12221 return isl_basic_set_is_box(set->p[0]);
12224 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12226 if (!bset)
12227 return isl_bool_error;
12229 return isl_space_is_wrapping(bset->dim);
12232 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12234 if (!set)
12235 return isl_bool_error;
12237 return isl_space_is_wrapping(set->dim);
12240 /* Modify the space of "map" through a call to "change".
12241 * If "can_change" is set (not NULL), then first call it to check
12242 * if the modification is allowed, printing the error message "cannot_change"
12243 * if it is not.
12245 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12246 isl_bool (*can_change)(__isl_keep isl_map *map),
12247 const char *cannot_change,
12248 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12250 isl_bool ok;
12251 isl_space *space;
12253 if (!map)
12254 return NULL;
12256 ok = can_change ? can_change(map) : isl_bool_true;
12257 if (ok < 0)
12258 return isl_map_free(map);
12259 if (!ok)
12260 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12261 return isl_map_free(map));
12263 space = change(isl_map_get_space(map));
12264 map = isl_map_reset_space(map, space);
12266 return map;
12269 /* Is the domain of "map" a wrapped relation?
12271 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12273 if (!map)
12274 return isl_bool_error;
12276 return isl_space_domain_is_wrapping(map->dim);
12279 /* Does "map" have a wrapped relation in both domain and range?
12281 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12283 return isl_space_is_product(isl_map_peek_space(map));
12286 /* Is the range of "map" a wrapped relation?
12288 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12290 if (!map)
12291 return isl_bool_error;
12293 return isl_space_range_is_wrapping(map->dim);
12296 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12298 isl_space *space;
12300 space = isl_basic_map_take_space(bmap);
12301 space = isl_space_wrap(space);
12302 bmap = isl_basic_map_restore_space(bmap, space);
12304 bmap = isl_basic_map_finalize(bmap);
12306 return bset_from_bmap(bmap);
12309 /* Given a map A -> B, return the set (A -> B).
12311 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12313 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12316 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12318 bset = isl_basic_set_cow(bset);
12319 if (!bset)
12320 return NULL;
12322 bset->dim = isl_space_unwrap(bset->dim);
12323 if (!bset->dim)
12324 goto error;
12326 bset = isl_basic_set_finalize(bset);
12328 return bset_to_bmap(bset);
12329 error:
12330 isl_basic_set_free(bset);
12331 return NULL;
12334 /* Given a set (A -> B), return the map A -> B.
12335 * Error out if "set" is not of the form (A -> B).
12337 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12339 return isl_map_change_space(set, &isl_set_is_wrapping,
12340 "not a wrapping set", &isl_space_unwrap);
12343 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12344 enum isl_dim_type type)
12346 isl_space *space;
12348 space = isl_basic_map_take_space(bmap);
12349 space = isl_space_reset(space, type);
12350 bmap = isl_basic_map_restore_space(bmap, space);
12352 bmap = isl_basic_map_mark_final(bmap);
12354 return bmap;
12357 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12358 enum isl_dim_type type)
12360 int i;
12361 isl_space *space;
12363 if (!map)
12364 return NULL;
12366 if (!isl_space_is_named_or_nested(map->dim, type))
12367 return map;
12369 map = isl_map_cow(map);
12370 if (!map)
12371 return NULL;
12373 for (i = 0; i < map->n; ++i) {
12374 map->p[i] = isl_basic_map_reset(map->p[i], type);
12375 if (!map->p[i])
12376 goto error;
12379 space = isl_map_take_space(map);
12380 space = isl_space_reset(space, type);
12381 map = isl_map_restore_space(map, space);
12383 return map;
12384 error:
12385 isl_map_free(map);
12386 return NULL;
12389 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12391 isl_space *space;
12393 space = isl_basic_map_take_space(bmap);
12394 space = isl_space_flatten(space);
12395 bmap = isl_basic_map_restore_space(bmap, space);
12397 bmap = isl_basic_map_mark_final(bmap);
12399 return bmap;
12402 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12404 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12407 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12408 __isl_take isl_basic_map *bmap)
12410 isl_space *space;
12412 space = isl_basic_map_take_space(bmap);
12413 space = isl_space_flatten_domain(space);
12414 bmap = isl_basic_map_restore_space(bmap, space);
12416 bmap = isl_basic_map_mark_final(bmap);
12418 return bmap;
12421 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12422 __isl_take isl_basic_map *bmap)
12424 isl_space *space;
12426 space = isl_basic_map_take_space(bmap);
12427 space = isl_space_flatten_range(space);
12428 bmap = isl_basic_map_restore_space(bmap, space);
12430 bmap = isl_basic_map_mark_final(bmap);
12432 return bmap;
12435 /* Remove any internal structure from the spaces of domain and range of "map".
12437 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12439 if (!map)
12440 return NULL;
12442 if (!map->dim->nested[0] && !map->dim->nested[1])
12443 return map;
12445 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12448 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12450 return set_from_map(isl_map_flatten(set_to_map(set)));
12453 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12455 isl_space *space, *flat_space;
12456 isl_map *map;
12458 space = isl_set_get_space(set);
12459 flat_space = isl_space_flatten(isl_space_copy(space));
12460 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12461 flat_space));
12462 map = isl_map_intersect_domain(map, set);
12464 return map;
12467 /* Remove any internal structure from the space of the domain of "map".
12469 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12471 if (!map)
12472 return NULL;
12474 if (!map->dim->nested[0])
12475 return map;
12477 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12480 /* Remove any internal structure from the space of the range of "map".
12482 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12484 if (!map)
12485 return NULL;
12487 if (!map->dim->nested[1])
12488 return map;
12490 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12493 /* Reorder the dimensions of "bmap" according to the given dim_map
12494 * and set the dimension specification to "space" and
12495 * perform Gaussian elimination on the result.
12497 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12498 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12500 isl_basic_map *res;
12501 unsigned flags;
12502 isl_size n_div;
12504 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12505 if (n_div < 0 || !space || !dim_map)
12506 goto error;
12508 flags = bmap->flags;
12509 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12510 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12511 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12512 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12513 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12514 if (res)
12515 res->flags = flags;
12516 res = isl_basic_map_gauss(res, NULL);
12517 res = isl_basic_map_finalize(res);
12518 return res;
12519 error:
12520 isl_dim_map_free(dim_map);
12521 isl_basic_map_free(bmap);
12522 isl_space_free(space);
12523 return NULL;
12526 /* Reorder the dimensions of "map" according to given reordering.
12528 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12529 __isl_take isl_reordering *r)
12531 int i;
12532 struct isl_dim_map *dim_map;
12534 map = isl_map_cow(map);
12535 dim_map = isl_dim_map_from_reordering(r);
12536 if (!map || !r || !dim_map)
12537 goto error;
12539 for (i = 0; i < map->n; ++i) {
12540 struct isl_dim_map *dim_map_i;
12541 isl_space *space;
12543 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12545 space = isl_reordering_get_space(r);
12546 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12548 if (!map->p[i])
12549 goto error;
12552 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12553 map = isl_map_unmark_normalized(map);
12555 isl_reordering_free(r);
12556 isl_dim_map_free(dim_map);
12557 return map;
12558 error:
12559 isl_dim_map_free(dim_map);
12560 isl_map_free(map);
12561 isl_reordering_free(r);
12562 return NULL;
12565 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12566 __isl_take isl_reordering *r)
12568 return set_from_map(isl_map_realign(set_to_map(set), r));
12571 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12572 __isl_take isl_space *model)
12574 isl_ctx *ctx;
12575 isl_bool aligned;
12577 if (!map || !model)
12578 goto error;
12580 ctx = isl_space_get_ctx(model);
12581 if (!isl_space_has_named_params(model))
12582 isl_die(ctx, isl_error_invalid,
12583 "model has unnamed parameters", goto error);
12584 if (isl_map_check_named_params(map) < 0)
12585 goto error;
12586 aligned = isl_map_space_has_equal_params(map, model);
12587 if (aligned < 0)
12588 goto error;
12589 if (!aligned) {
12590 isl_space *space;
12591 isl_reordering *exp;
12593 space = isl_map_peek_space(map);
12594 exp = isl_parameter_alignment_reordering(space, model);
12595 map = isl_map_realign(map, exp);
12598 isl_space_free(model);
12599 return map;
12600 error:
12601 isl_space_free(model);
12602 isl_map_free(map);
12603 return NULL;
12606 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12607 __isl_take isl_space *model)
12609 return isl_map_align_params(set, model);
12612 /* Align the parameters of "bmap" to those of "model", introducing
12613 * additional parameters if needed.
12615 __isl_give isl_basic_map *isl_basic_map_align_params(
12616 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12618 isl_ctx *ctx;
12619 isl_bool equal_params;
12620 isl_space *bmap_space;
12622 if (!bmap || !model)
12623 goto error;
12625 ctx = isl_space_get_ctx(model);
12626 if (!isl_space_has_named_params(model))
12627 isl_die(ctx, isl_error_invalid,
12628 "model has unnamed parameters", goto error);
12629 if (isl_basic_map_check_named_params(bmap) < 0)
12630 goto error;
12631 bmap_space = isl_basic_map_peek_space(bmap);
12632 equal_params = isl_space_has_equal_params(bmap_space, model);
12633 if (equal_params < 0)
12634 goto error;
12635 if (!equal_params) {
12636 isl_reordering *exp;
12637 struct isl_dim_map *dim_map;
12639 exp = isl_parameter_alignment_reordering(bmap_space, model);
12640 dim_map = isl_dim_map_from_reordering(exp);
12641 bmap = isl_basic_map_realign(bmap,
12642 isl_reordering_get_space(exp),
12643 isl_dim_map_extend(dim_map, bmap));
12644 isl_reordering_free(exp);
12645 isl_dim_map_free(dim_map);
12648 isl_space_free(model);
12649 return bmap;
12650 error:
12651 isl_space_free(model);
12652 isl_basic_map_free(bmap);
12653 return NULL;
12656 /* Do "bset" and "space" have the same parameters?
12658 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12659 __isl_keep isl_space *space)
12661 isl_space *bset_space;
12663 bset_space = isl_basic_set_peek_space(bset);
12664 return isl_space_has_equal_params(bset_space, space);
12667 /* Do "map" and "space" have the same parameters?
12669 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12670 __isl_keep isl_space *space)
12672 isl_space *map_space;
12674 map_space = isl_map_peek_space(map);
12675 return isl_space_has_equal_params(map_space, space);
12678 /* Do "set" and "space" have the same parameters?
12680 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12681 __isl_keep isl_space *space)
12683 return isl_map_space_has_equal_params(set_to_map(set), space);
12686 /* Align the parameters of "bset" to those of "model", introducing
12687 * additional parameters if needed.
12689 __isl_give isl_basic_set *isl_basic_set_align_params(
12690 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12692 return isl_basic_map_align_params(bset, model);
12695 #undef TYPE
12696 #define TYPE isl_map
12697 #define isl_map_drop_dims isl_map_drop
12698 #include "isl_drop_unused_params_templ.c"
12700 /* Drop all parameters not referenced by "set".
12702 __isl_give isl_set *isl_set_drop_unused_params(
12703 __isl_take isl_set *set)
12705 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12708 #undef TYPE
12709 #define TYPE isl_basic_map
12710 #define isl_basic_map_drop_dims isl_basic_map_drop
12711 #include "isl_drop_unused_params_templ.c"
12713 /* Drop all parameters not referenced by "bset".
12715 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12716 __isl_take isl_basic_set *bset)
12718 return bset_from_bmap(isl_basic_map_drop_unused_params(
12719 bset_to_bmap(bset)));
12722 /* Given a tuple of identifiers "tuple" in a space that corresponds
12723 * to that of "set", if any of those identifiers appear as parameters
12724 * in "set", then equate those parameters with the corresponding
12725 * set dimensions and project out the parameters.
12726 * The result therefore has no such parameters.
12728 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12729 __isl_keep isl_multi_id *tuple)
12731 int i;
12732 isl_size n;
12733 isl_space *set_space, *tuple_space;
12735 set_space = isl_set_peek_space(set);
12736 tuple_space = isl_multi_id_peek_space(tuple);
12737 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12738 return isl_set_free(set);
12739 n = isl_multi_id_size(tuple);
12740 if (n < 0)
12741 return isl_set_free(set);
12742 for (i = 0; i < n; ++i) {
12743 isl_id *id;
12744 int pos;
12746 id = isl_multi_id_get_at(tuple, i);
12747 if (!id)
12748 return isl_set_free(set);
12749 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12750 isl_id_free(id);
12751 if (pos < 0)
12752 continue;
12753 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12754 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12756 return set;
12759 /* Bind the set dimensions of "set" to parameters with identifiers
12760 * specified by "tuple", living in the same space as "set".
12762 * If no parameters with these identifiers appear in "set" already,
12763 * then the set dimensions are simply reinterpreted as parameters.
12764 * Otherwise, the parameters are first equated to the corresponding
12765 * set dimensions.
12767 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12768 __isl_take isl_multi_id *tuple)
12770 isl_space *space;
12772 set = equate_params(set, tuple);
12773 space = isl_set_get_space(set);
12774 space = isl_space_bind_set(space, tuple);
12775 isl_multi_id_free(tuple);
12776 set = isl_set_reset_space(set, space);
12778 return set;
12781 /* Given a tuple of identifiers "tuple" in a space that corresponds
12782 * to the domain of "map", if any of those identifiers appear as parameters
12783 * in "map", then equate those parameters with the corresponding
12784 * input dimensions and project out the parameters.
12785 * The result therefore has no such parameters.
12787 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12788 __isl_keep isl_multi_id *tuple)
12790 int i;
12791 isl_size n;
12792 isl_space *map_space, *tuple_space;
12794 map_space = isl_map_peek_space(map);
12795 tuple_space = isl_multi_id_peek_space(tuple);
12796 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12797 return isl_map_free(map);
12798 n = isl_multi_id_size(tuple);
12799 if (n < 0)
12800 return isl_map_free(map);
12801 for (i = 0; i < n; ++i) {
12802 isl_id *id;
12803 int pos;
12805 id = isl_multi_id_get_at(tuple, i);
12806 if (!id)
12807 return isl_map_free(map);
12808 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12809 isl_id_free(id);
12810 if (pos < 0)
12811 continue;
12812 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12813 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12815 return map;
12818 /* Bind the input dimensions of "map" to parameters with identifiers
12819 * specified by "tuple", living in the domain space of "map".
12821 * If no parameters with these identifiers appear in "map" already,
12822 * then the input dimensions are simply reinterpreted as parameters.
12823 * Otherwise, the parameters are first equated to the corresponding
12824 * input dimensions.
12826 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12827 __isl_take isl_multi_id *tuple)
12829 isl_space *space;
12830 isl_set *set;
12832 map = map_equate_params(map, tuple);
12833 space = isl_map_get_space(map);
12834 space = isl_space_bind_map_domain(space, tuple);
12835 isl_multi_id_free(tuple);
12836 set = set_from_map(isl_map_reset_space(map, space));
12838 return set;
12841 /* Bind the output dimensions of "map" to parameters with identifiers
12842 * specified by "tuple", living in the range space of "map".
12844 * Since binding is more easily implemented on the domain,
12845 * bind the input dimensions of the inverse of "map".
12847 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12848 __isl_take isl_multi_id *tuple)
12850 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12853 /* Insert a domain corresponding to "tuple"
12854 * into the nullary or unary relation "set".
12855 * The result has an extra initial tuple and is therefore
12856 * either a unary or binary relation.
12857 * Any parameters with identifiers in "tuple" are reinterpreted
12858 * as the corresponding domain dimensions.
12860 static __isl_give isl_map *unbind_params_insert_domain(
12861 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12863 isl_space *space;
12864 isl_reordering *r;
12866 space = isl_set_peek_space(set);
12867 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12868 isl_multi_id_free(tuple);
12870 return isl_map_realign(set_to_map(set), r);
12873 /* Construct a set with "tuple" as domain from the parameter domain "set".
12874 * Any parameters with identifiers in "tuple" are reinterpreted
12875 * as the corresponding set dimensions.
12877 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12878 __isl_take isl_multi_id *tuple)
12880 isl_bool is_params;
12882 is_params = isl_set_is_params(set);
12883 if (is_params < 0)
12884 set = isl_set_free(set);
12885 else if (!is_params)
12886 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12887 "expecting parameter domain", set = isl_set_free(set));
12888 return set_from_map(unbind_params_insert_domain(set, tuple));
12891 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12893 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12895 isl_bool is_params;
12897 is_params = isl_set_is_params(set);
12898 if (is_params < 0)
12899 return isl_stat_error;
12900 else if (is_params)
12901 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12902 "expecting proper set", return isl_stat_error);
12904 return isl_stat_ok;
12907 /* Construct a map with "domain" as domain and "set" as range.
12908 * Any parameters with identifiers in "domain" are reinterpreted
12909 * as the corresponding domain dimensions.
12911 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12912 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12914 if (isl_set_check_is_set(set) < 0)
12915 set = isl_set_free(set);
12916 return unbind_params_insert_domain(set, domain);
12919 /* Construct a map with "domain" as domain and "set" as range.
12921 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12922 __isl_take isl_space *domain)
12924 isl_size dim;
12925 isl_space *space;
12926 isl_map *map;
12928 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12929 domain = isl_space_free(domain);
12930 dim = isl_space_dim(domain, isl_dim_set);
12931 if (dim < 0)
12932 domain = isl_space_free(domain);
12933 space = isl_set_get_space(set);
12934 domain = isl_space_replace_params(domain, space);
12935 space = isl_space_map_from_domain_and_range(domain, space);
12937 map = isl_map_from_range(set);
12938 map = isl_map_add_dims(map, isl_dim_in, dim);
12939 map = isl_map_reset_space(map, space);
12941 return map;
12944 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12945 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12946 enum isl_dim_type c2, enum isl_dim_type c3,
12947 enum isl_dim_type c4, enum isl_dim_type c5)
12949 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12950 struct isl_mat *mat;
12951 int i, j, k;
12952 int pos;
12953 isl_size total;
12955 total = isl_basic_map_dim(bmap, isl_dim_all);
12956 if (total < 0)
12957 return NULL;
12958 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12959 if (!mat)
12960 return NULL;
12961 for (i = 0; i < bmap->n_eq; ++i)
12962 for (j = 0, pos = 0; j < 5; ++j) {
12963 int off = isl_basic_map_offset(bmap, c[j]);
12964 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12965 if (dim < 0)
12966 return isl_mat_free(mat);
12967 for (k = 0; k < dim; ++k) {
12968 isl_int_set(mat->row[i][pos],
12969 bmap->eq[i][off + k]);
12970 ++pos;
12974 return mat;
12977 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12978 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12979 enum isl_dim_type c2, enum isl_dim_type c3,
12980 enum isl_dim_type c4, enum isl_dim_type c5)
12982 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12983 struct isl_mat *mat;
12984 int i, j, k;
12985 int pos;
12986 isl_size total;
12988 total = isl_basic_map_dim(bmap, isl_dim_all);
12989 if (total < 0)
12990 return NULL;
12991 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12992 if (!mat)
12993 return NULL;
12994 for (i = 0; i < bmap->n_ineq; ++i)
12995 for (j = 0, pos = 0; j < 5; ++j) {
12996 int off = isl_basic_map_offset(bmap, c[j]);
12997 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12998 if (dim < 0)
12999 return isl_mat_free(mat);
13000 for (k = 0; k < dim; ++k) {
13001 isl_int_set(mat->row[i][pos],
13002 bmap->ineq[i][off + k]);
13003 ++pos;
13007 return mat;
13010 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13011 __isl_take isl_space *space,
13012 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13013 enum isl_dim_type c2, enum isl_dim_type c3,
13014 enum isl_dim_type c4, enum isl_dim_type c5)
13016 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13017 isl_basic_map *bmap = NULL;
13018 isl_size dim;
13019 unsigned total;
13020 unsigned extra;
13021 int i, j, k, l;
13022 int pos;
13024 dim = isl_space_dim(space, isl_dim_all);
13025 if (dim < 0 || !eq || !ineq)
13026 goto error;
13028 if (eq->n_col != ineq->n_col)
13029 isl_die(space->ctx, isl_error_invalid,
13030 "equalities and inequalities matrices should have "
13031 "same number of columns", goto error);
13033 total = 1 + dim;
13035 if (eq->n_col < total)
13036 isl_die(space->ctx, isl_error_invalid,
13037 "number of columns too small", goto error);
13039 extra = eq->n_col - total;
13041 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13042 eq->n_row, ineq->n_row);
13043 if (!bmap)
13044 goto error;
13045 for (i = 0; i < extra; ++i) {
13046 k = isl_basic_map_alloc_div(bmap);
13047 if (k < 0)
13048 goto error;
13049 isl_int_set_si(bmap->div[k][0], 0);
13051 for (i = 0; i < eq->n_row; ++i) {
13052 l = isl_basic_map_alloc_equality(bmap);
13053 if (l < 0)
13054 goto error;
13055 for (j = 0, pos = 0; j < 5; ++j) {
13056 int off = isl_basic_map_offset(bmap, c[j]);
13057 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13058 if (dim < 0)
13059 goto error;
13060 for (k = 0; k < dim; ++k) {
13061 isl_int_set(bmap->eq[l][off + k],
13062 eq->row[i][pos]);
13063 ++pos;
13067 for (i = 0; i < ineq->n_row; ++i) {
13068 l = isl_basic_map_alloc_inequality(bmap);
13069 if (l < 0)
13070 goto error;
13071 for (j = 0, pos = 0; j < 5; ++j) {
13072 int off = isl_basic_map_offset(bmap, c[j]);
13073 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13074 if (dim < 0)
13075 goto error;
13076 for (k = 0; k < dim; ++k) {
13077 isl_int_set(bmap->ineq[l][off + k],
13078 ineq->row[i][pos]);
13079 ++pos;
13084 isl_space_free(space);
13085 isl_mat_free(eq);
13086 isl_mat_free(ineq);
13088 bmap = isl_basic_map_simplify(bmap);
13089 return isl_basic_map_finalize(bmap);
13090 error:
13091 isl_space_free(space);
13092 isl_mat_free(eq);
13093 isl_mat_free(ineq);
13094 isl_basic_map_free(bmap);
13095 return NULL;
13098 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13099 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13100 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13102 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13103 c1, c2, c3, c4, isl_dim_in);
13106 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13107 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13108 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13110 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13111 c1, c2, c3, c4, isl_dim_in);
13114 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13115 __isl_take isl_space *space,
13116 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13117 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13119 isl_basic_map *bmap;
13120 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13121 c1, c2, c3, c4, isl_dim_in);
13122 return bset_from_bmap(bmap);
13125 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13127 if (!bmap)
13128 return isl_bool_error;
13130 return isl_space_can_zip(bmap->dim);
13133 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13135 if (!map)
13136 return isl_bool_error;
13138 return isl_space_can_zip(map->dim);
13141 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13142 * (A -> C) -> (B -> D).
13144 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13146 unsigned pos;
13147 isl_size n_in;
13148 isl_size n1;
13149 isl_size n2;
13151 if (!bmap)
13152 return NULL;
13154 if (!isl_basic_map_can_zip(bmap))
13155 isl_die(bmap->ctx, isl_error_invalid,
13156 "basic map cannot be zipped", goto error);
13157 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13158 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13159 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13160 if (n_in < 0 || n1 < 0 || n2 < 0)
13161 return isl_basic_map_free(bmap);
13162 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13163 bmap = isl_basic_map_cow(bmap);
13164 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13165 if (!bmap)
13166 return NULL;
13167 bmap->dim = isl_space_zip(bmap->dim);
13168 if (!bmap->dim)
13169 goto error;
13170 bmap = isl_basic_map_mark_final(bmap);
13171 return bmap;
13172 error:
13173 isl_basic_map_free(bmap);
13174 return NULL;
13177 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13178 * (A -> C) -> (B -> D).
13180 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13182 if (!map)
13183 return NULL;
13185 if (!isl_map_can_zip(map))
13186 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13187 goto error);
13189 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13190 error:
13191 isl_map_free(map);
13192 return NULL;
13195 /* Can we apply isl_basic_map_curry to "bmap"?
13196 * That is, does it have a nested relation in its domain?
13198 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13200 if (!bmap)
13201 return isl_bool_error;
13203 return isl_space_can_curry(bmap->dim);
13206 /* Can we apply isl_map_curry to "map"?
13207 * That is, does it have a nested relation in its domain?
13209 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13211 if (!map)
13212 return isl_bool_error;
13214 return isl_space_can_curry(map->dim);
13217 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13218 * A -> (B -> C).
13220 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13223 if (!bmap)
13224 return NULL;
13226 if (!isl_basic_map_can_curry(bmap))
13227 isl_die(bmap->ctx, isl_error_invalid,
13228 "basic map cannot be curried", goto error);
13229 bmap = isl_basic_map_cow(bmap);
13230 if (!bmap)
13231 return NULL;
13232 bmap->dim = isl_space_curry(bmap->dim);
13233 if (!bmap->dim)
13234 goto error;
13235 bmap = isl_basic_map_mark_final(bmap);
13236 return bmap;
13237 error:
13238 isl_basic_map_free(bmap);
13239 return NULL;
13242 /* Given a map (A -> B) -> C, return the corresponding map
13243 * A -> (B -> C).
13245 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13247 return isl_map_change_space(map, &isl_map_can_curry,
13248 "map cannot be curried", &isl_space_curry);
13251 /* Can isl_map_range_curry be applied to "map"?
13252 * That is, does it have a nested relation in its range,
13253 * the domain of which is itself a nested relation?
13255 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13257 if (!map)
13258 return isl_bool_error;
13260 return isl_space_can_range_curry(map->dim);
13263 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13264 * A -> (B -> (C -> D)).
13266 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13268 return isl_map_change_space(map, &isl_map_can_range_curry,
13269 "map range cannot be curried",
13270 &isl_space_range_curry);
13273 /* Can we apply isl_basic_map_uncurry to "bmap"?
13274 * That is, does it have a nested relation in its domain?
13276 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13278 if (!bmap)
13279 return isl_bool_error;
13281 return isl_space_can_uncurry(bmap->dim);
13284 /* Can we apply isl_map_uncurry to "map"?
13285 * That is, does it have a nested relation in its domain?
13287 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13289 if (!map)
13290 return isl_bool_error;
13292 return isl_space_can_uncurry(map->dim);
13295 /* Given a basic map A -> (B -> C), return the corresponding basic map
13296 * (A -> B) -> C.
13298 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13301 if (!bmap)
13302 return NULL;
13304 if (!isl_basic_map_can_uncurry(bmap))
13305 isl_die(bmap->ctx, isl_error_invalid,
13306 "basic map cannot be uncurried",
13307 return isl_basic_map_free(bmap));
13308 bmap = isl_basic_map_cow(bmap);
13309 if (!bmap)
13310 return NULL;
13311 bmap->dim = isl_space_uncurry(bmap->dim);
13312 if (!bmap->dim)
13313 return isl_basic_map_free(bmap);
13314 bmap = isl_basic_map_mark_final(bmap);
13315 return bmap;
13318 /* Given a map A -> (B -> C), return the corresponding map
13319 * (A -> B) -> C.
13321 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13323 return isl_map_change_space(map, &isl_map_can_uncurry,
13324 "map cannot be uncurried", &isl_space_uncurry);
13327 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13328 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13330 return isl_map_equate(set, type1, pos1, type2, pos2);
13333 /* Construct a basic map where the given dimensions are equal to each other.
13335 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13336 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13338 isl_basic_map *bmap = NULL;
13339 int i;
13340 isl_size total;
13342 total = isl_space_dim(space, isl_dim_all);
13343 if (total < 0 ||
13344 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13345 isl_space_check_range(space, type2, pos2, 1) < 0)
13346 goto error;
13348 if (type1 == type2 && pos1 == pos2)
13349 return isl_basic_map_universe(space);
13351 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13352 i = isl_basic_map_alloc_equality(bmap);
13353 if (i < 0)
13354 goto error;
13355 isl_seq_clr(bmap->eq[i], 1 + total);
13356 pos1 += isl_basic_map_offset(bmap, type1);
13357 pos2 += isl_basic_map_offset(bmap, type2);
13358 isl_int_set_si(bmap->eq[i][pos1], -1);
13359 isl_int_set_si(bmap->eq[i][pos2], 1);
13360 bmap = isl_basic_map_finalize(bmap);
13361 isl_space_free(space);
13362 return bmap;
13363 error:
13364 isl_space_free(space);
13365 isl_basic_map_free(bmap);
13366 return NULL;
13369 /* Add a constraint imposing that the given two dimensions are equal.
13371 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13372 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13374 isl_basic_map *eq;
13376 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13378 bmap = isl_basic_map_intersect(bmap, eq);
13380 return bmap;
13383 /* Add a constraint imposing that the given two dimensions are equal.
13385 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13386 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13388 isl_basic_map *bmap;
13390 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13392 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13394 return map;
13397 /* Add a constraint imposing that the given two dimensions have opposite values.
13399 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13400 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13402 isl_basic_map *bmap = NULL;
13403 int i;
13404 isl_size total;
13406 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13407 return isl_map_free(map);
13408 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13409 return isl_map_free(map);
13411 total = isl_map_dim(map, isl_dim_all);
13412 if (total < 0)
13413 return isl_map_free(map);
13414 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13415 i = isl_basic_map_alloc_equality(bmap);
13416 if (i < 0)
13417 goto error;
13418 isl_seq_clr(bmap->eq[i], 1 + total);
13419 pos1 += isl_basic_map_offset(bmap, type1);
13420 pos2 += isl_basic_map_offset(bmap, type2);
13421 isl_int_set_si(bmap->eq[i][pos1], 1);
13422 isl_int_set_si(bmap->eq[i][pos2], 1);
13423 bmap = isl_basic_map_finalize(bmap);
13425 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13427 return map;
13428 error:
13429 isl_basic_map_free(bmap);
13430 isl_map_free(map);
13431 return NULL;
13434 /* Construct a constraint imposing that the value of the first dimension is
13435 * greater than or equal to that of the second.
13437 static __isl_give isl_constraint *constraint_order_ge(
13438 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13439 enum isl_dim_type type2, int pos2)
13441 isl_constraint *c;
13443 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13444 isl_space_check_range(space, type2, pos2, 1) < 0)
13445 space = isl_space_free(space);
13446 if (!space)
13447 return NULL;
13449 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13451 if (type1 == type2 && pos1 == pos2)
13452 return c;
13454 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13455 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13457 return c;
13460 /* Add a constraint imposing that the value of the first dimension is
13461 * greater than or equal to that of the second.
13463 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13464 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13466 isl_constraint *c;
13467 isl_space *space;
13469 if (type1 == type2 && pos1 == pos2)
13470 return bmap;
13471 space = isl_basic_map_get_space(bmap);
13472 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13473 bmap = isl_basic_map_add_constraint(bmap, c);
13475 return bmap;
13478 /* Add a constraint imposing that the value of the first dimension is
13479 * greater than or equal to that of the second.
13481 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13482 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13484 isl_constraint *c;
13485 isl_space *space;
13487 if (type1 == type2 && pos1 == pos2)
13488 return map;
13489 space = isl_map_get_space(map);
13490 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13491 map = isl_map_add_constraint(map, c);
13493 return map;
13496 /* Add a constraint imposing that the value of the first dimension is
13497 * less than or equal to that of the second.
13499 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13500 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13502 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13505 /* Construct a basic map where the value of the first dimension is
13506 * greater than that of the second.
13508 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13509 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13511 isl_basic_map *bmap = NULL;
13512 int i;
13513 isl_size total;
13515 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13516 isl_space_check_range(space, type2, pos2, 1) < 0)
13517 goto error;
13519 if (type1 == type2 && pos1 == pos2)
13520 return isl_basic_map_empty(space);
13522 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13523 total = isl_basic_map_dim(bmap, isl_dim_all);
13524 i = isl_basic_map_alloc_inequality(bmap);
13525 if (total < 0 || i < 0)
13526 return isl_basic_map_free(bmap);
13527 isl_seq_clr(bmap->ineq[i], 1 + total);
13528 pos1 += isl_basic_map_offset(bmap, type1);
13529 pos2 += isl_basic_map_offset(bmap, type2);
13530 isl_int_set_si(bmap->ineq[i][pos1], 1);
13531 isl_int_set_si(bmap->ineq[i][pos2], -1);
13532 isl_int_set_si(bmap->ineq[i][0], -1);
13533 bmap = isl_basic_map_finalize(bmap);
13535 return bmap;
13536 error:
13537 isl_space_free(space);
13538 isl_basic_map_free(bmap);
13539 return NULL;
13542 /* Add a constraint imposing that the value of the first dimension is
13543 * greater than that of the second.
13545 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13546 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13548 isl_basic_map *gt;
13550 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13552 bmap = isl_basic_map_intersect(bmap, gt);
13554 return bmap;
13557 /* Add a constraint imposing that the value of the first dimension is
13558 * greater than that of the second.
13560 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13561 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13563 isl_basic_map *bmap;
13565 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13567 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13569 return map;
13572 /* Add a constraint imposing that the value of the first dimension is
13573 * smaller than that of the second.
13575 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13576 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13578 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13581 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13582 int pos)
13584 isl_aff *div;
13585 isl_local_space *ls;
13587 if (!bmap)
13588 return NULL;
13590 if (!isl_basic_map_divs_known(bmap))
13591 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13592 "some divs are unknown", return NULL);
13594 ls = isl_basic_map_get_local_space(bmap);
13595 div = isl_local_space_get_div(ls, pos);
13596 isl_local_space_free(ls);
13598 return div;
13601 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13602 int pos)
13604 return isl_basic_map_get_div(bset, pos);
13607 /* Plug in "subs" for set dimension "pos" of "set".
13609 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13610 unsigned pos, __isl_keep isl_aff *subs)
13612 isl_multi_aff *ma;
13614 if (set && isl_set_plain_is_empty(set))
13615 return set;
13617 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13618 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13619 return isl_set_preimage_multi_aff(set, ma);
13622 /* Check if the range of "ma" is compatible with the domain or range
13623 * (depending on "type") of "bmap".
13625 static isl_stat check_basic_map_compatible_range_multi_aff(
13626 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13627 __isl_keep isl_multi_aff *ma)
13629 isl_bool m;
13630 isl_space *ma_space;
13632 ma_space = isl_multi_aff_get_space(ma);
13634 m = isl_space_has_equal_params(bmap->dim, ma_space);
13635 if (m < 0)
13636 goto error;
13637 if (!m)
13638 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13639 "parameters don't match", goto error);
13640 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13641 if (m < 0)
13642 goto error;
13643 if (!m)
13644 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13645 "spaces don't match", goto error);
13647 isl_space_free(ma_space);
13648 return isl_stat_ok;
13649 error:
13650 isl_space_free(ma_space);
13651 return isl_stat_error;
13654 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13655 * coefficients before the transformed range of dimensions,
13656 * the "n_after" coefficients after the transformed range of dimensions
13657 * and the coefficients of the other divs in "bmap".
13659 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13660 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13662 int i;
13663 isl_size n_param;
13664 isl_size n_set;
13665 isl_local_space *ls;
13667 if (n_div == 0)
13668 return bmap;
13670 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13671 n_param = isl_local_space_dim(ls, isl_dim_param);
13672 n_set = isl_local_space_dim(ls, isl_dim_set);
13673 if (n_param < 0 || n_set < 0)
13674 return isl_basic_map_free(bmap);
13676 for (i = 0; i < n_div; ++i) {
13677 int o_bmap = 0, o_ls = 0;
13679 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13680 o_bmap += 1 + 1 + n_param;
13681 o_ls += 1 + 1 + n_param;
13682 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13683 o_bmap += n_before;
13684 isl_seq_cpy(bmap->div[i] + o_bmap,
13685 ls->div->row[i] + o_ls, n_set);
13686 o_bmap += n_set;
13687 o_ls += n_set;
13688 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13689 o_bmap += n_after;
13690 isl_seq_cpy(bmap->div[i] + o_bmap,
13691 ls->div->row[i] + o_ls, n_div);
13692 o_bmap += n_div;
13693 o_ls += n_div;
13694 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13695 bmap = isl_basic_map_add_div_constraints(bmap, i);
13696 if (!bmap)
13697 goto error;
13700 isl_local_space_free(ls);
13701 return bmap;
13702 error:
13703 isl_local_space_free(ls);
13704 return isl_basic_map_free(bmap);
13707 /* How many stride constraints does "ma" enforce?
13708 * That is, how many of the affine expressions have a denominator
13709 * different from one?
13711 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13713 int i;
13714 int strides = 0;
13716 for (i = 0; i < ma->n; ++i)
13717 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13718 strides++;
13720 return strides;
13723 /* For each affine expression in ma of the form
13725 * x_i = (f_i y + h_i)/m_i
13727 * with m_i different from one, add a constraint to "bmap"
13728 * of the form
13730 * f_i y + h_i = m_i alpha_i
13732 * with alpha_i an additional existentially quantified variable.
13734 * The input variables of "ma" correspond to a subset of the variables
13735 * of "bmap". There are "n_before" variables in "bmap" before this
13736 * subset and "n_after" variables after this subset.
13737 * The integer divisions of the affine expressions in "ma" are assumed
13738 * to have been aligned. There are "n_div_ma" of them and
13739 * they appear first in "bmap", straight after the "n_after" variables.
13741 static __isl_give isl_basic_map *add_ma_strides(
13742 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13743 int n_before, int n_after, int n_div_ma)
13745 int i, k;
13746 int div;
13747 isl_size total;
13748 isl_size n_param;
13749 isl_size n_in;
13751 total = isl_basic_map_dim(bmap, isl_dim_all);
13752 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13753 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13754 if (total < 0 || n_param < 0 || n_in < 0)
13755 return isl_basic_map_free(bmap);
13756 for (i = 0; i < ma->n; ++i) {
13757 int o_bmap = 0, o_ma = 1;
13759 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13760 continue;
13761 div = isl_basic_map_alloc_div(bmap);
13762 k = isl_basic_map_alloc_equality(bmap);
13763 if (div < 0 || k < 0)
13764 goto error;
13765 isl_int_set_si(bmap->div[div][0], 0);
13766 isl_seq_cpy(bmap->eq[k] + o_bmap,
13767 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13768 o_bmap += 1 + n_param;
13769 o_ma += 1 + n_param;
13770 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13771 o_bmap += n_before;
13772 isl_seq_cpy(bmap->eq[k] + o_bmap,
13773 ma->u.p[i]->v->el + o_ma, n_in);
13774 o_bmap += n_in;
13775 o_ma += n_in;
13776 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13777 o_bmap += n_after;
13778 isl_seq_cpy(bmap->eq[k] + o_bmap,
13779 ma->u.p[i]->v->el + o_ma, n_div_ma);
13780 o_bmap += n_div_ma;
13781 o_ma += n_div_ma;
13782 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13783 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13784 total++;
13787 return bmap;
13788 error:
13789 isl_basic_map_free(bmap);
13790 return NULL;
13793 /* Replace the domain or range space (depending on "type) of "space" by "set".
13795 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13796 enum isl_dim_type type, __isl_take isl_space *set)
13798 if (type == isl_dim_in) {
13799 space = isl_space_range(space);
13800 space = isl_space_map_from_domain_and_range(set, space);
13801 } else {
13802 space = isl_space_domain(space);
13803 space = isl_space_map_from_domain_and_range(space, set);
13806 return space;
13809 /* Compute the preimage of the domain or range (depending on "type")
13810 * of "bmap" under the function represented by "ma".
13811 * In other words, plug in "ma" in the domain or range of "bmap".
13812 * The result is a basic map that lives in the same space as "bmap"
13813 * except that the domain or range has been replaced by
13814 * the domain space of "ma".
13816 * If bmap is represented by
13818 * A(p) + S u + B x + T v + C(divs) >= 0,
13820 * where u and x are input and output dimensions if type == isl_dim_out
13821 * while x and v are input and output dimensions if type == isl_dim_in,
13822 * and ma is represented by
13824 * x = D(p) + F(y) + G(divs')
13826 * then the result is
13828 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13830 * The divs in the input set are similarly adjusted.
13831 * In particular
13833 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13835 * becomes
13837 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13838 * B_i G(divs') + c_i(divs))/n_i)
13840 * If bmap is not a rational map and if F(y) involves any denominators
13842 * x_i = (f_i y + h_i)/m_i
13844 * then additional constraints are added to ensure that we only
13845 * map back integer points. That is we enforce
13847 * f_i y + h_i = m_i alpha_i
13849 * with alpha_i an additional existentially quantified variable.
13851 * We first copy over the divs from "ma".
13852 * Then we add the modified constraints and divs from "bmap".
13853 * Finally, we add the stride constraints, if needed.
13855 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13856 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13857 __isl_take isl_multi_aff *ma)
13859 int i, k;
13860 isl_space *space;
13861 isl_basic_map *res = NULL;
13862 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13863 isl_int f, c1, c2, g;
13864 isl_bool rational;
13865 int strides;
13867 isl_int_init(f);
13868 isl_int_init(c1);
13869 isl_int_init(c2);
13870 isl_int_init(g);
13872 ma = isl_multi_aff_align_divs(ma);
13873 if (!bmap || !ma)
13874 goto error;
13875 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13876 goto error;
13878 if (type == isl_dim_in) {
13879 n_before = 0;
13880 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13881 } else {
13882 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13883 n_after = 0;
13885 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13886 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13887 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13888 goto error;
13890 space = isl_multi_aff_get_domain_space(ma);
13891 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13892 rational = isl_basic_map_is_rational(bmap);
13893 strides = rational ? 0 : multi_aff_strides(ma);
13894 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13895 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13896 if (rational)
13897 res = isl_basic_map_set_rational(res);
13899 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13900 if (isl_basic_map_alloc_div(res) < 0)
13901 goto error;
13903 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13904 if (!res)
13905 goto error;
13907 for (i = 0; i < bmap->n_eq; ++i) {
13908 k = isl_basic_map_alloc_equality(res);
13909 if (k < 0)
13910 goto error;
13911 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13912 n_after, n_div_ma, n_div_bmap,
13913 f, c1, c2, g, 0) < 0)
13914 goto error;
13917 for (i = 0; i < bmap->n_ineq; ++i) {
13918 k = isl_basic_map_alloc_inequality(res);
13919 if (k < 0)
13920 goto error;
13921 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13922 n_after, n_div_ma, n_div_bmap,
13923 f, c1, c2, g, 0) < 0)
13924 goto error;
13927 for (i = 0; i < bmap->n_div; ++i) {
13928 if (isl_int_is_zero(bmap->div[i][0])) {
13929 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13930 continue;
13932 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13933 n_before, n_after, n_div_ma, n_div_bmap,
13934 f, c1, c2, g, 1) < 0)
13935 goto error;
13938 if (strides)
13939 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13941 isl_int_clear(f);
13942 isl_int_clear(c1);
13943 isl_int_clear(c2);
13944 isl_int_clear(g);
13945 isl_basic_map_free(bmap);
13946 isl_multi_aff_free(ma);
13947 res = isl_basic_map_simplify(res);
13948 return isl_basic_map_finalize(res);
13949 error:
13950 isl_int_clear(f);
13951 isl_int_clear(c1);
13952 isl_int_clear(c2);
13953 isl_int_clear(g);
13954 isl_basic_map_free(bmap);
13955 isl_multi_aff_free(ma);
13956 isl_basic_map_free(res);
13957 return NULL;
13960 /* Compute the preimage of "bset" under the function represented by "ma".
13961 * In other words, plug in "ma" in "bset". The result is a basic set
13962 * that lives in the domain space of "ma".
13964 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13965 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13967 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13970 /* Compute the preimage of the domain of "bmap" under the function
13971 * represented by "ma".
13972 * In other words, plug in "ma" in the domain of "bmap".
13973 * The result is a basic map that lives in the same space as "bmap"
13974 * except that the domain has been replaced by the domain space of "ma".
13976 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13977 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13979 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13982 /* Compute the preimage of the range of "bmap" under the function
13983 * represented by "ma".
13984 * In other words, plug in "ma" in the range of "bmap".
13985 * The result is a basic map that lives in the same space as "bmap"
13986 * except that the range has been replaced by the domain space of "ma".
13988 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13989 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13991 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13994 /* Check if the range of "ma" is compatible with the domain or range
13995 * (depending on "type") of "map".
13996 * Return isl_stat_error if anything is wrong.
13998 static isl_stat check_map_compatible_range_multi_aff(
13999 __isl_keep isl_map *map, enum isl_dim_type type,
14000 __isl_keep isl_multi_aff *ma)
14002 isl_bool m;
14003 isl_space *ma_space;
14005 ma_space = isl_multi_aff_get_space(ma);
14006 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14007 isl_space_free(ma_space);
14008 if (m < 0)
14009 return isl_stat_error;
14010 if (!m)
14011 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14012 "spaces don't match", return isl_stat_error);
14013 return isl_stat_ok;
14016 /* Compute the preimage of the domain or range (depending on "type")
14017 * of "map" under the function represented by "ma".
14018 * In other words, plug in "ma" in the domain or range of "map".
14019 * The result is a map that lives in the same space as "map"
14020 * except that the domain or range has been replaced by
14021 * the domain space of "ma".
14023 * The parameters are assumed to have been aligned.
14025 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14026 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14028 int i;
14029 isl_space *space;
14031 map = isl_map_cow(map);
14032 ma = isl_multi_aff_align_divs(ma);
14033 if (!map || !ma)
14034 goto error;
14035 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14036 goto error;
14038 for (i = 0; i < map->n; ++i) {
14039 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14040 isl_multi_aff_copy(ma));
14041 if (!map->p[i])
14042 goto error;
14045 space = isl_multi_aff_get_domain_space(ma);
14046 space = isl_space_set(isl_map_get_space(map), type, space);
14048 isl_space_free(isl_map_take_space(map));
14049 map = isl_map_restore_space(map, space);
14050 if (!map)
14051 goto error;
14053 isl_multi_aff_free(ma);
14054 if (map->n > 1)
14055 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14056 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14057 return map;
14058 error:
14059 isl_multi_aff_free(ma);
14060 isl_map_free(map);
14061 return NULL;
14064 /* Compute the preimage of the domain or range (depending on "type")
14065 * of "map" under the function represented by "ma".
14066 * In other words, plug in "ma" in the domain or range of "map".
14067 * The result is a map that lives in the same space as "map"
14068 * except that the domain or range has been replaced by
14069 * the domain space of "ma".
14071 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14072 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14074 isl_bool aligned;
14076 if (!map || !ma)
14077 goto error;
14079 aligned = isl_map_space_has_equal_params(map, ma->space);
14080 if (aligned < 0)
14081 goto error;
14082 if (aligned)
14083 return map_preimage_multi_aff(map, type, ma);
14085 if (isl_map_check_named_params(map) < 0)
14086 goto error;
14087 if (!isl_space_has_named_params(ma->space))
14088 isl_die(map->ctx, isl_error_invalid,
14089 "unaligned unnamed parameters", goto error);
14090 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14091 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14093 return map_preimage_multi_aff(map, type, ma);
14094 error:
14095 isl_multi_aff_free(ma);
14096 return isl_map_free(map);
14099 /* Compute the preimage of "set" under the function represented by "ma".
14100 * In other words, plug in "ma" in "set". The result is a set
14101 * that lives in the domain space of "ma".
14103 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14104 __isl_take isl_multi_aff *ma)
14106 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14109 /* Compute the preimage of the domain of "map" under the function
14110 * represented by "ma".
14111 * In other words, plug in "ma" in the domain of "map".
14112 * The result is a map that lives in the same space as "map"
14113 * except that the domain has been replaced by the domain space of "ma".
14115 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14116 __isl_take isl_multi_aff *ma)
14118 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14121 /* Compute the preimage of the range of "map" under the function
14122 * represented by "ma".
14123 * In other words, plug in "ma" in the range of "map".
14124 * The result is a map that lives in the same space as "map"
14125 * except that the range has been replaced by the domain space of "ma".
14127 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14128 __isl_take isl_multi_aff *ma)
14130 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14133 /* Compute the preimage of "map" under the function represented by "pma".
14134 * In other words, plug in "pma" in the domain or range of "map".
14135 * The result is a map that lives in the same space as "map",
14136 * except that the space of type "type" has been replaced by
14137 * the domain space of "pma".
14139 * The parameters of "map" and "pma" are assumed to have been aligned.
14141 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14142 __isl_take isl_map *map, enum isl_dim_type type,
14143 __isl_take isl_pw_multi_aff *pma)
14145 int i;
14146 isl_map *res;
14148 if (!pma)
14149 goto error;
14151 if (pma->n == 0) {
14152 isl_space *space;
14154 space = isl_pw_multi_aff_get_domain_space(pma);
14155 isl_pw_multi_aff_free(pma);
14156 space = isl_space_set(isl_map_get_space(map), type, space);
14157 isl_map_free(map);
14158 return isl_map_empty(space);
14161 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14162 isl_multi_aff_copy(pma->p[0].maff));
14163 if (type == isl_dim_in)
14164 res = isl_map_intersect_domain(res,
14165 isl_map_copy(pma->p[0].set));
14166 else
14167 res = isl_map_intersect_range(res,
14168 isl_map_copy(pma->p[0].set));
14170 for (i = 1; i < pma->n; ++i) {
14171 isl_map *res_i;
14173 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14174 isl_multi_aff_copy(pma->p[i].maff));
14175 if (type == isl_dim_in)
14176 res_i = isl_map_intersect_domain(res_i,
14177 isl_map_copy(pma->p[i].set));
14178 else
14179 res_i = isl_map_intersect_range(res_i,
14180 isl_map_copy(pma->p[i].set));
14181 res = isl_map_union(res, res_i);
14184 isl_pw_multi_aff_free(pma);
14185 isl_map_free(map);
14186 return res;
14187 error:
14188 isl_pw_multi_aff_free(pma);
14189 isl_map_free(map);
14190 return NULL;
14193 /* Compute the preimage of "map" under the function represented by "pma".
14194 * In other words, plug in "pma" in the domain or range of "map".
14195 * The result is a map that lives in the same space as "map",
14196 * except that the space of type "type" has been replaced by
14197 * the domain space of "pma".
14199 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14200 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14202 isl_bool aligned;
14204 if (!map || !pma)
14205 goto error;
14207 aligned = isl_map_space_has_equal_params(map, pma->dim);
14208 if (aligned < 0)
14209 goto error;
14210 if (aligned)
14211 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14213 if (isl_map_check_named_params(map) < 0)
14214 goto error;
14215 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14216 goto error;
14217 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14218 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14220 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14221 error:
14222 isl_pw_multi_aff_free(pma);
14223 return isl_map_free(map);
14226 /* Compute the preimage of "set" under the function represented by "pma".
14227 * In other words, plug in "pma" in "set". The result is a set
14228 * that lives in the domain space of "pma".
14230 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14231 __isl_take isl_pw_multi_aff *pma)
14233 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14236 /* Compute the preimage of the domain of "map" under the function
14237 * represented by "pma".
14238 * In other words, plug in "pma" in the domain of "map".
14239 * The result is a map that lives in the same space as "map",
14240 * except that domain space has been replaced by the domain space of "pma".
14242 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14243 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14245 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14248 /* Compute the preimage of the range of "map" under the function
14249 * represented by "pma".
14250 * In other words, plug in "pma" in the range of "map".
14251 * The result is a map that lives in the same space as "map",
14252 * except that range space has been replaced by the domain space of "pma".
14254 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14255 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14257 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14260 /* Compute the preimage of "map" under the function represented by "mpa".
14261 * In other words, plug in "mpa" in the domain or range of "map".
14262 * The result is a map that lives in the same space as "map",
14263 * except that the space of type "type" has been replaced by
14264 * the domain space of "mpa".
14266 * If the map does not involve any constraints that refer to the
14267 * dimensions of the substituted space, then the only possible
14268 * effect of "mpa" on the map is to map the space to a different space.
14269 * We create a separate isl_multi_aff to effectuate this change
14270 * in order to avoid spurious splitting of the map along the pieces
14271 * of "mpa".
14272 * If "mpa" has a non-trivial explicit domain, however,
14273 * then the full substitution should be performed.
14275 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14276 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14278 isl_size n;
14279 isl_bool full;
14280 isl_pw_multi_aff *pma;
14282 n = isl_map_dim(map, type);
14283 if (n < 0 || !mpa)
14284 goto error;
14286 full = isl_map_involves_dims(map, type, 0, n);
14287 if (full >= 0 && !full)
14288 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14289 if (full < 0)
14290 goto error;
14291 if (!full) {
14292 isl_space *space;
14293 isl_multi_aff *ma;
14295 space = isl_multi_pw_aff_get_space(mpa);
14296 isl_multi_pw_aff_free(mpa);
14297 ma = isl_multi_aff_zero(space);
14298 return isl_map_preimage_multi_aff(map, type, ma);
14301 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14302 return isl_map_preimage_pw_multi_aff(map, type, pma);
14303 error:
14304 isl_map_free(map);
14305 isl_multi_pw_aff_free(mpa);
14306 return NULL;
14309 /* Compute the preimage of "map" under the function represented by "mpa".
14310 * In other words, plug in "mpa" in the domain "map".
14311 * The result is a map that lives in the same space as "map",
14312 * except that domain space has been replaced by the domain space of "mpa".
14314 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14315 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14317 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14320 /* Compute the preimage of "set" by the function represented by "mpa".
14321 * In other words, plug in "mpa" in "set".
14323 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14324 __isl_take isl_multi_pw_aff *mpa)
14326 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14329 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14330 * on the output dimension "pos" in terms of the parameters,
14331 * the input dimensions and possibly some integer divisions,
14332 * but not any other output dimensions, extract this upper bound
14333 * as a function of all dimensions (with zero coefficients
14334 * for the output dimensions).
14336 * That is, the inequality is of the form
14338 * e(...) + c - m x >= 0
14340 * where e does not depend on any other output dimensions.
14341 * Return (e(...) + c) / m, with the denominator m in the first position.
14343 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14344 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14346 isl_ctx *ctx;
14347 isl_vec *v;
14348 isl_size v_out, total;
14350 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14351 total = isl_basic_map_dim(bmap, isl_dim_all);
14352 if (v_out < 0 || total < 0)
14353 return NULL;
14354 ctx = isl_basic_map_get_ctx(bmap);
14355 v = isl_vec_alloc(ctx, 1 + 1 + total);
14356 if (!v)
14357 return NULL;
14358 isl_int_neg(v->el[0], bmap->ineq[ineq][1 + v_out + pos]);
14359 isl_seq_cpy(v->el + 1, bmap->ineq[ineq], 1 + total);
14360 isl_int_set_si(v->el[1 + 1 + v_out + pos], 0);
14362 return v;
14365 /* Is constraint "c" of "bmap" of the form
14367 * e(...) + c1 - m x >= 0
14369 * or
14371 * -e(...) + c2 + m x >= 0
14373 * where m > 1 and e does not involve any other output variables?
14375 * "v_out" is the offset to the output variables.
14376 * "d" is the position of x among the output variables.
14377 * "v_div" is the offset to the local variables.
14378 * "total" is the total number of variables.
14380 * Since the purpose of this function is to use the constraint
14381 * to express the output variable as an integer division,
14382 * do not allow the constraint to involve any local variables
14383 * that do not have an explicit representation.
14385 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14386 isl_int *c, int v_out, int d, int v_div, int total)
14388 int i = 0;
14390 if (isl_int_is_zero(c[1 + v_out + d]))
14391 return isl_bool_false;
14392 if (isl_int_is_one(c[1 + v_out + d]))
14393 return isl_bool_false;
14394 if (isl_int_is_negone(c[1 + v_out + d]))
14395 return isl_bool_false;
14396 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14397 return isl_bool_false;
14398 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14399 v_div - (v_out + d + 1)) != -1)
14400 return isl_bool_false;
14401 for (i = 0; v_div + i < total; ++i) {
14402 isl_bool known, involves;
14404 if (isl_int_is_zero(c[1 + v_div + i]))
14405 continue;
14406 known = isl_basic_map_div_is_known(bmap, i);
14407 if (known < 0 || !known)
14408 return known;
14409 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14410 if (involves < 0 || involves)
14411 return isl_bool_not(involves);
14413 return isl_bool_true;
14416 /* Look for a pair of constraints
14418 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14420 * and
14422 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14424 * that express that the output dimension x at position "pos"
14425 * is some integer division of an expression in terms of the parameters,
14426 * input dimensions and integer divisions.
14427 * If such a pair can be found, then return the index
14428 * of the upper bound constraint, m x <= e(...) + c1.
14429 * Otherwise, return an index beyond the number of constraints.
14431 * In order for the constraints above to express an integer division,
14432 * m needs to be greater than 1 and such that
14434 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14436 * In particular, this ensures that
14438 * x = floor((e(...) + c1) / m)
14440 isl_size isl_basic_map_find_output_upper_div_constraint(
14441 __isl_keep isl_basic_map *bmap, int pos)
14443 int i, j;
14444 isl_size n_ineq;
14445 isl_size v_out, v_div;
14446 isl_size total;
14447 isl_int sum;
14449 total = isl_basic_map_dim(bmap, isl_dim_all);
14450 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14451 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
14452 n_ineq = isl_basic_map_n_inequality(bmap);
14453 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
14454 return isl_size_error;
14456 isl_int_init(sum);
14457 for (i = 0; i < n_ineq; ++i) {
14458 isl_bool potential;
14460 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
14461 v_out, pos, v_div, total);
14462 if (potential < 0)
14463 goto error;
14464 if (!potential)
14465 continue;
14466 for (j = i + 1; j < n_ineq; ++j) {
14467 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
14468 bmap->ineq[j] + 1, total))
14469 continue;
14470 isl_int_add(sum, bmap->ineq[i][0], bmap->ineq[j][0]);
14471 if (isl_int_abs_lt(sum, bmap->ineq[i][1 + v_out + pos]))
14472 break;
14474 if (j < n_ineq)
14475 break;
14477 isl_int_clear(sum);
14479 if (i >= n_ineq)
14480 return n_ineq;
14481 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
14482 return i;
14483 else
14484 return j;
14485 error:
14486 isl_int_clear(sum);
14487 return isl_size_error;
14490 /* Return a copy of the equality constraints of "bset" as a matrix.
14492 __isl_give isl_mat *isl_basic_set_extract_equalities(
14493 __isl_keep isl_basic_set *bset)
14495 isl_ctx *ctx;
14496 isl_size total;
14498 total = isl_basic_set_dim(bset, isl_dim_all);
14499 if (total < 0)
14500 return NULL;
14502 ctx = isl_basic_set_get_ctx(bset);
14503 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14506 /* Are the "n" "coefficients" starting at "first" of the integer division
14507 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14508 * to each other?
14509 * The "coefficient" at position 0 is the denominator.
14510 * The "coefficient" at position 1 is the constant term.
14512 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14513 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14514 unsigned first, unsigned n)
14516 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14517 return isl_bool_error;
14518 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14519 return isl_bool_error;
14520 return isl_seq_eq(bmap1->div[pos1] + first,
14521 bmap2->div[pos2] + first, n);
14524 /* Are the integer division expressions at position "pos1" in "bmap1" and
14525 * "pos2" in "bmap2" equal to each other, except that the constant terms
14526 * are different?
14528 isl_bool isl_basic_map_equal_div_expr_except_constant(
14529 __isl_keep isl_basic_map *bmap1, int pos1,
14530 __isl_keep isl_basic_map *bmap2, int pos2)
14532 isl_bool equal;
14533 isl_size total, total2;
14535 total = isl_basic_map_dim(bmap1, isl_dim_all);
14536 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14537 if (total < 0 || total2 < 0)
14538 return isl_bool_error;
14539 if (total != total2)
14540 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14541 "incomparable div expressions", return isl_bool_error);
14542 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14543 0, 1);
14544 if (equal < 0 || !equal)
14545 return equal;
14546 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14547 1, 1);
14548 if (equal < 0 || equal)
14549 return isl_bool_not(equal);
14550 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14551 2, total);
14554 /* Replace the numerator of the constant term of the integer division
14555 * expression at position "div" in "bmap" by "value".
14556 * The caller guarantees that this does not change the meaning
14557 * of the input.
14559 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14560 __isl_take isl_basic_map *bmap, int div, int value)
14562 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14563 return isl_basic_map_free(bmap);
14565 isl_int_set_si(bmap->div[div][1], value);
14567 return bmap;
14570 /* Is the point "inner" internal to inequality constraint "ineq"
14571 * of "bset"?
14572 * The point is considered to be internal to the inequality constraint,
14573 * if it strictly lies on the positive side of the inequality constraint,
14574 * or if it lies on the constraint and the constraint is lexico-positive.
14576 static isl_bool is_internal(__isl_keep isl_vec *inner,
14577 __isl_keep isl_basic_set *bset, int ineq)
14579 isl_ctx *ctx;
14580 int pos;
14581 isl_size total;
14583 if (!inner || !bset)
14584 return isl_bool_error;
14586 ctx = isl_basic_set_get_ctx(bset);
14587 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14588 &ctx->normalize_gcd);
14589 if (!isl_int_is_zero(ctx->normalize_gcd))
14590 return isl_int_is_nonneg(ctx->normalize_gcd);
14592 total = isl_basic_set_dim(bset, isl_dim_all);
14593 if (total < 0)
14594 return isl_bool_error;
14595 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14596 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14599 /* Tighten the inequality constraints of "bset" that are outward with respect
14600 * to the point "vec".
14601 * That is, tighten the constraints that are not satisfied by "vec".
14603 * "vec" is a point internal to some superset S of "bset" that is used
14604 * to make the subsets of S disjoint, by tightening one half of the constraints
14605 * that separate two subsets. In particular, the constraints of S
14606 * are all satisfied by "vec" and should not be tightened.
14607 * Of the internal constraints, those that have "vec" on the outside
14608 * are tightened. The shared facet is included in the adjacent subset
14609 * with the opposite constraint.
14610 * For constraints that saturate "vec", this criterion cannot be used
14611 * to determine which of the two sides should be tightened.
14612 * Instead, the sign of the first non-zero coefficient is used
14613 * to make this choice. Note that this second criterion is never used
14614 * on the constraints of S since "vec" is interior to "S".
14616 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14617 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14619 int j;
14621 bset = isl_basic_set_cow(bset);
14622 if (!bset)
14623 return NULL;
14624 for (j = 0; j < bset->n_ineq; ++j) {
14625 isl_bool internal;
14627 internal = is_internal(vec, bset, j);
14628 if (internal < 0)
14629 return isl_basic_set_free(bset);
14630 if (internal)
14631 continue;
14632 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14635 return bset;
14638 /* Replace the variables x of type "type" starting at "first" in "bmap"
14639 * by x' with x = M x' with M the matrix trans.
14640 * That is, replace the corresponding coefficients c by c M.
14642 * The transformation matrix should be a square matrix.
14644 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14645 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14646 __isl_take isl_mat *trans)
14648 unsigned pos;
14650 bmap = isl_basic_map_cow(bmap);
14651 if (!bmap || !trans)
14652 goto error;
14654 if (trans->n_row != trans->n_col)
14655 isl_die(trans->ctx, isl_error_invalid,
14656 "expecting square transformation matrix", goto error);
14657 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14658 goto error;
14660 pos = isl_basic_map_offset(bmap, type) + first;
14662 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14663 isl_mat_copy(trans)) < 0)
14664 goto error;
14665 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14666 isl_mat_copy(trans)) < 0)
14667 goto error;
14668 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14669 isl_mat_copy(trans)) < 0)
14670 goto error;
14672 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14673 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14675 isl_mat_free(trans);
14676 return bmap;
14677 error:
14678 isl_mat_free(trans);
14679 isl_basic_map_free(bmap);
14680 return NULL;
14683 /* Replace the variables x of type "type" starting at "first" in "bset"
14684 * by x' with x = M x' with M the matrix trans.
14685 * That is, replace the corresponding coefficients c by c M.
14687 * The transformation matrix should be a square matrix.
14689 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14690 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14691 __isl_take isl_mat *trans)
14693 return isl_basic_map_transform_dims(bset, type, first, trans);