isl_basic_map_remove_divs_involving_dims: extract out remove_divs_involving_vars
[isl.git] / isl_map.c
bloba489ab8b7dfc8c87cc0244817fe3b62fa8b518c2
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_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1756 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1757 bmap->extra - bmap->n_div);
1758 return bmap->n_ineq++;
1761 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1763 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1766 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1767 __isl_take isl_basic_map *bmap, unsigned n)
1769 if (!bmap)
1770 return NULL;
1771 if (n > bmap->n_ineq)
1772 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1773 "invalid number of inequalities",
1774 return isl_basic_map_free(bmap));
1775 bmap->n_ineq -= n;
1776 return bmap;
1779 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1780 __isl_take isl_basic_set *bset, unsigned n)
1782 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1783 n));
1786 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1788 isl_int *t;
1789 if (!bmap)
1790 return -1;
1791 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1793 if (pos != bmap->n_ineq - 1) {
1794 t = bmap->ineq[pos];
1795 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1796 bmap->ineq[bmap->n_ineq - 1] = t;
1797 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1799 bmap->n_ineq--;
1800 return 0;
1803 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1805 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1808 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1809 isl_int *eq)
1811 isl_bool empty;
1812 isl_size total;
1813 int k;
1815 empty = isl_basic_map_plain_is_empty(bmap);
1816 if (empty < 0)
1817 return isl_basic_map_free(bmap);
1818 if (empty)
1819 return bmap;
1821 bmap = isl_basic_map_cow(bmap);
1822 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1823 total = isl_basic_map_dim(bmap, isl_dim_all);
1824 if (total < 0)
1825 return isl_basic_map_free(bmap);
1826 k = isl_basic_map_alloc_equality(bmap);
1827 if (k < 0)
1828 goto error;
1829 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1830 return bmap;
1831 error:
1832 isl_basic_map_free(bmap);
1833 return NULL;
1836 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1837 isl_int *eq)
1839 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1842 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1843 isl_int *ineq)
1845 isl_size total;
1846 int k;
1848 bmap = isl_basic_map_cow(bmap);
1849 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1850 total = isl_basic_map_dim(bmap, isl_dim_all);
1851 if (total < 0)
1852 return isl_basic_map_free(bmap);
1853 k = isl_basic_map_alloc_inequality(bmap);
1854 if (k < 0)
1855 goto error;
1856 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1857 return bmap;
1858 error:
1859 isl_basic_map_free(bmap);
1860 return NULL;
1863 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1864 isl_int *ineq)
1866 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1869 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1871 isl_size total;
1873 total = isl_basic_map_dim(bmap, isl_dim_all);
1874 if (total < 0)
1875 return -1;
1876 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1877 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1878 bmap->extra - bmap->n_div);
1879 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1880 return bmap->n_div++;
1883 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1885 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1888 #undef TYPE
1889 #define TYPE isl_basic_map
1890 #include "check_type_range_templ.c"
1892 /* Check that there are "n" dimensions of type "type" starting at "first"
1893 * in "bset".
1895 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1896 enum isl_dim_type type, unsigned first, unsigned n)
1898 return isl_basic_map_check_range(bset_to_bmap(bset),
1899 type, first, n);
1902 /* Insert an extra integer division, prescribed by "div", to "bmap"
1903 * at (integer division) position "pos".
1905 * The integer division is first added at the end and then moved
1906 * into the right position.
1908 __isl_give isl_basic_map *isl_basic_map_insert_div(
1909 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1911 int i, k;
1912 isl_size total;
1914 bmap = isl_basic_map_cow(bmap);
1915 total = isl_basic_map_dim(bmap, isl_dim_all);
1916 if (total < 0 || !div)
1917 return isl_basic_map_free(bmap);
1919 if (div->size != 1 + 1 + total)
1920 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1921 "unexpected size", return isl_basic_map_free(bmap));
1922 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1923 return isl_basic_map_free(bmap);
1925 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1926 k = isl_basic_map_alloc_div(bmap);
1927 if (k < 0)
1928 return isl_basic_map_free(bmap);
1929 isl_seq_cpy(bmap->div[k], div->el, div->size);
1930 isl_int_set_si(bmap->div[k][div->size], 0);
1932 for (i = k; i > pos; --i)
1933 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1935 return bmap;
1938 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1940 if (!bmap)
1941 return isl_stat_error;
1942 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1943 bmap->n_div -= n;
1944 return isl_stat_ok;
1947 static __isl_give isl_basic_map *add_constraints(
1948 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1949 unsigned i_pos, unsigned o_pos)
1951 isl_size total, n_param, n_in, n_out, n_div;
1952 unsigned o_in, o_out;
1953 isl_ctx *ctx;
1954 isl_space *space;
1955 struct isl_dim_map *dim_map;
1957 space = isl_basic_map_peek_space(bmap2);
1958 if (!bmap1 || !space)
1959 goto error;
1961 total = isl_basic_map_dim(bmap1, isl_dim_all);
1962 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1963 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1964 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1965 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1966 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1967 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1968 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1969 goto error;
1970 ctx = isl_basic_map_get_ctx(bmap1);
1971 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1972 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1974 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1975 isl_dim_map_div(dim_map, bmap2, total);
1977 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1978 error:
1979 isl_basic_map_free(bmap1);
1980 isl_basic_map_free(bmap2);
1981 return NULL;
1984 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1985 unsigned extra, unsigned n_eq, unsigned n_ineq)
1987 isl_space *space;
1988 struct isl_basic_map *ext;
1989 unsigned flags;
1990 int dims_ok;
1992 if (!base)
1993 goto error;
1995 dims_ok = base->extra >= base->n_div + extra;
1997 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1998 room_for_ineq(base, n_ineq))
1999 return base;
2001 extra += base->extra;
2002 n_eq += base->n_eq;
2003 n_ineq += base->n_ineq;
2005 space = isl_basic_map_get_space(base);
2006 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2007 if (!ext)
2008 goto error;
2010 if (dims_ok)
2011 ext->sample = isl_vec_copy(base->sample);
2012 flags = base->flags;
2013 ext = add_constraints(ext, base, 0, 0);
2014 if (ext) {
2015 ext->flags = flags;
2016 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2019 return ext;
2021 error:
2022 isl_basic_map_free(base);
2023 return NULL;
2026 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2027 unsigned extra, unsigned n_eq, unsigned n_ineq)
2029 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2030 extra, n_eq, n_ineq));
2033 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2034 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2036 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2039 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2040 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2042 isl_basic_map *bmap = bset_to_bmap(base);
2043 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2044 return bset_from_bmap(bmap);
2047 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2049 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2052 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2054 if (!bmap)
2055 return NULL;
2057 if (bmap->ref > 1) {
2058 bmap->ref--;
2059 bmap = isl_basic_map_dup(bmap);
2061 if (bmap)
2062 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2063 return bmap;
2066 /* Clear all cached information in "map", either because it is about
2067 * to be modified or because it is being freed.
2068 * Always return the same pointer that is passed in.
2069 * This is needed for the use in isl_map_free.
2071 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2073 isl_basic_map_free(map->cached_simple_hull[0]);
2074 isl_basic_map_free(map->cached_simple_hull[1]);
2075 map->cached_simple_hull[0] = NULL;
2076 map->cached_simple_hull[1] = NULL;
2077 return map;
2080 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2082 return isl_map_cow(set);
2085 /* Return an isl_map that is equal to "map" and that has only
2086 * a single reference.
2088 * If the original input already has only one reference, then
2089 * simply return it, but clear all cached information, since
2090 * it may be rendered invalid by the operations that will be
2091 * performed on the result.
2093 * Otherwise, create a duplicate (without any cached information).
2095 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2097 if (!map)
2098 return NULL;
2100 if (map->ref == 1)
2101 return clear_caches(map);
2102 map->ref--;
2103 return isl_map_dup(map);
2106 static void swap_vars(struct isl_blk blk, isl_int *a,
2107 unsigned a_len, unsigned b_len)
2109 isl_seq_cpy(blk.data, a+a_len, b_len);
2110 isl_seq_cpy(blk.data+b_len, a, a_len);
2111 isl_seq_cpy(a, blk.data, b_len+a_len);
2114 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2115 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2117 int i;
2118 struct isl_blk blk;
2120 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2121 goto error;
2123 if (n1 == 0 || n2 == 0)
2124 return bmap;
2126 bmap = isl_basic_map_cow(bmap);
2127 if (!bmap)
2128 return NULL;
2130 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2131 if (isl_blk_is_error(blk))
2132 goto error;
2134 for (i = 0; i < bmap->n_eq; ++i)
2135 swap_vars(blk,
2136 bmap->eq[i] + pos, n1, n2);
2138 for (i = 0; i < bmap->n_ineq; ++i)
2139 swap_vars(blk,
2140 bmap->ineq[i] + pos, n1, n2);
2142 for (i = 0; i < bmap->n_div; ++i)
2143 swap_vars(blk,
2144 bmap->div[i]+1 + pos, n1, n2);
2146 isl_blk_free(bmap->ctx, blk);
2148 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2149 bmap = isl_basic_map_gauss(bmap, NULL);
2150 return isl_basic_map_finalize(bmap);
2151 error:
2152 isl_basic_map_free(bmap);
2153 return NULL;
2156 /* The given basic map has turned out to be empty.
2157 * Explicitly mark it as such and change the representation
2158 * to a canonical representation of the empty basic map.
2159 * Since the basic map has conflicting constraints,
2160 * it must have at least one constraint, except perhaps
2161 * if it was already explicitly marked as being empty.
2162 * Do nothing in the latter case, i.e., if it has been marked empty and
2163 * has no constraints.
2165 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2166 __isl_take isl_basic_map *bmap)
2168 int i = 0;
2169 isl_bool empty;
2170 isl_size n;
2171 isl_size total;
2173 n = isl_basic_map_n_constraint(bmap);
2174 empty = isl_basic_map_plain_is_empty(bmap);
2175 if (n < 0 || empty < 0)
2176 return isl_basic_map_free(bmap);
2177 if (n == 0 && empty)
2178 return bmap;
2179 total = isl_basic_map_dim(bmap, isl_dim_all);
2180 if (total < 0)
2181 return isl_basic_map_free(bmap);
2182 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2183 return isl_basic_map_free(bmap);
2184 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2185 if (!bmap)
2186 return NULL;
2187 if (bmap->n_eq > 0) {
2188 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2189 if (!bmap)
2190 return NULL;
2191 } else {
2192 i = isl_basic_map_alloc_equality(bmap);
2193 if (i < 0)
2194 goto error;
2196 isl_int_set_si(bmap->eq[i][0], 1);
2197 isl_seq_clr(bmap->eq[i]+1, total);
2198 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2199 isl_vec_free(bmap->sample);
2200 bmap->sample = NULL;
2201 return isl_basic_map_finalize(bmap);
2202 error:
2203 isl_basic_map_free(bmap);
2204 return NULL;
2207 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2208 __isl_take isl_basic_set *bset)
2210 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2213 __isl_give isl_basic_map *isl_basic_map_set_rational(
2214 __isl_take isl_basic_map *bmap)
2216 if (!bmap)
2217 return NULL;
2219 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2220 return bmap;
2222 bmap = isl_basic_map_cow(bmap);
2223 if (!bmap)
2224 return NULL;
2226 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2228 return isl_basic_map_finalize(bmap);
2231 __isl_give isl_basic_set *isl_basic_set_set_rational(
2232 __isl_take isl_basic_set *bset)
2234 return isl_basic_map_set_rational(bset);
2237 __isl_give isl_basic_set *isl_basic_set_set_integral(
2238 __isl_take isl_basic_set *bset)
2240 if (!bset)
2241 return NULL;
2243 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2244 return bset;
2246 bset = isl_basic_set_cow(bset);
2247 if (!bset)
2248 return NULL;
2250 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2252 return isl_basic_set_finalize(bset);
2255 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2257 int i;
2259 map = isl_map_cow(map);
2260 if (!map)
2261 return NULL;
2262 for (i = 0; i < map->n; ++i) {
2263 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2264 if (!map->p[i])
2265 goto error;
2267 return map;
2268 error:
2269 isl_map_free(map);
2270 return NULL;
2273 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2275 return isl_map_set_rational(set);
2278 /* Given a constraint "c" that expresses a bound
2279 * on the variable at position "pos" in terms of the first "len" variables
2280 * (other than the variable itself if pos < len), extract this bound
2281 * as a function of those first "len" variables.
2283 * That is, the constraint is of one of the following forms
2285 * -e(...) + m x >= 0
2286 * e(...) - m x >= 0
2287 * -e(...) + m x = 0
2288 * e(...) - m x = 0
2290 * Return (e(...)) / m, with the denominator m in the first position.
2292 static __isl_give isl_vec *extract_bound_from_constraint(isl_ctx *ctx,
2293 isl_int *c, int len, int pos)
2295 isl_vec *v;
2297 v = isl_vec_alloc(ctx, 1 + 1 + len);
2298 if (!v)
2299 return NULL;
2300 if (isl_int_is_pos(c[1 + pos])) {
2301 isl_int_set(v->el[0], c[1 + pos]);
2302 isl_seq_neg(v->el + 1, c, 1 + len);
2303 } else {
2304 isl_int_neg(v->el[0], c[1 + pos]);
2305 isl_seq_cpy(v->el + 1, c, 1 + len);
2307 if (pos < len)
2308 isl_int_set_si(v->el[1 + 1 + pos], 0);
2310 return v;
2313 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2314 * of "bmap").
2316 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2318 isl_int *t = bmap->div[a];
2319 bmap->div[a] = bmap->div[b];
2320 bmap->div[b] = t;
2323 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2324 * div definitions accordingly.
2326 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2327 int a, int b)
2329 int i;
2330 isl_size off;
2332 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2333 if (off < 0)
2334 return isl_basic_map_free(bmap);
2336 swap_div(bmap, a, b);
2338 for (i = 0; i < bmap->n_eq; ++i)
2339 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2341 for (i = 0; i < bmap->n_ineq; ++i)
2342 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2344 for (i = 0; i < bmap->n_div; ++i)
2345 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2346 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2348 return bmap;
2351 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2353 isl_seq_cpy(c, c + n, rem);
2354 isl_seq_clr(c + rem, n);
2357 /* Drop n dimensions starting at first.
2359 * In principle, this frees up some extra variables as the number
2360 * of columns remains constant, but we would have to extend
2361 * the div array too as the number of rows in this array is assumed
2362 * to be equal to extra.
2364 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2365 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2367 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2370 /* Move "n" divs starting at "first" to the end of the list of divs.
2372 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2373 unsigned first, unsigned n)
2375 isl_int **div;
2376 int i;
2378 if (first + n == bmap->n_div)
2379 return bmap;
2381 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2382 if (!div)
2383 goto error;
2384 for (i = 0; i < n; ++i)
2385 div[i] = bmap->div[first + i];
2386 for (i = 0; i < bmap->n_div - first - n; ++i)
2387 bmap->div[first + i] = bmap->div[first + n + i];
2388 for (i = 0; i < n; ++i)
2389 bmap->div[bmap->n_div - n + i] = div[i];
2390 free(div);
2391 return bmap;
2392 error:
2393 isl_basic_map_free(bmap);
2394 return NULL;
2397 #undef TYPE
2398 #define TYPE isl_map
2399 static
2400 #include "check_type_range_templ.c"
2402 /* Check that there are "n" dimensions of type "type" starting at "first"
2403 * in "set".
2405 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 return isl_map_check_range(set_to_map(set), type, first, n);
2411 /* Drop "n" dimensions of type "type" starting at "first".
2412 * Perform the core computation, without cowing or
2413 * simplifying and finalizing the result.
2415 * In principle, this frees up some extra variables as the number
2416 * of columns remains constant, but we would have to extend
2417 * the div array too as the number of rows in this array is assumed
2418 * to be equal to extra.
2420 __isl_give isl_basic_map *isl_basic_map_drop_core(
2421 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2422 unsigned first, unsigned n)
2424 int i;
2425 unsigned offset;
2426 unsigned left;
2427 isl_size total;
2429 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2430 return isl_basic_map_free(bmap);
2432 total = isl_basic_map_dim(bmap, isl_dim_all);
2433 if (total < 0)
2434 return isl_basic_map_free(bmap);
2436 offset = isl_basic_map_offset(bmap, type) + first;
2437 left = total - (offset - 1) - n;
2438 for (i = 0; i < bmap->n_eq; ++i)
2439 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2441 for (i = 0; i < bmap->n_ineq; ++i)
2442 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2444 for (i = 0; i < bmap->n_div; ++i)
2445 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2447 if (type == isl_dim_div) {
2448 bmap = move_divs_last(bmap, first, n);
2449 if (!bmap)
2450 return NULL;
2451 if (isl_basic_map_free_div(bmap, n) < 0)
2452 return isl_basic_map_free(bmap);
2453 } else
2454 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2455 if (!bmap->dim)
2456 return isl_basic_map_free(bmap);
2458 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2459 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2460 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2461 return bmap;
2464 /* Drop "n" dimensions of type "type" starting at "first".
2466 * In principle, this frees up some extra variables as the number
2467 * of columns remains constant, but we would have to extend
2468 * the div array too as the number of rows in this array is assumed
2469 * to be equal to extra.
2471 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 if (!bmap)
2475 return NULL;
2476 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2477 return bmap;
2479 bmap = isl_basic_map_cow(bmap);
2480 if (!bmap)
2481 return NULL;
2483 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2485 bmap = isl_basic_map_simplify(bmap);
2486 return isl_basic_map_finalize(bmap);
2489 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2490 enum isl_dim_type type, unsigned first, unsigned n)
2492 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2493 type, first, n));
2496 /* No longer consider "map" to be normalized.
2498 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2500 if (!map)
2501 return NULL;
2502 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2503 return map;
2506 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2507 enum isl_dim_type type, unsigned first, unsigned n)
2509 int i;
2510 isl_space *space;
2512 if (isl_map_check_range(map, type, first, n) < 0)
2513 return isl_map_free(map);
2515 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2516 return map;
2517 map = isl_map_cow(map);
2518 if (!map)
2519 goto error;
2521 for (i = 0; i < map->n; ++i) {
2522 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2523 if (!map->p[i])
2524 goto error;
2526 map = isl_map_unmark_normalized(map);
2528 space = isl_map_take_space(map);
2529 space = isl_space_drop_dims(space, type, first, n);
2530 map = isl_map_restore_space(map, space);
2532 return map;
2533 error:
2534 isl_map_free(map);
2535 return NULL;
2538 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2539 enum isl_dim_type type, unsigned first, unsigned n)
2541 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2544 /* Drop the integer division at position "div", which is assumed
2545 * not to appear in any of the constraints or
2546 * in any of the other integer divisions.
2548 * Since the integer division is redundant, there is no need to cow.
2550 __isl_give isl_basic_map *isl_basic_map_drop_div(
2551 __isl_take isl_basic_map *bmap, unsigned div)
2553 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2556 /* Eliminate the specified n dimensions starting at first from the
2557 * constraints, without removing the dimensions from the space.
2558 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2560 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2561 enum isl_dim_type type, unsigned first, unsigned n)
2563 int i;
2565 if (n == 0)
2566 return map;
2568 if (isl_map_check_range(map, type, first, n) < 0)
2569 return isl_map_free(map);
2571 map = isl_map_cow(map);
2572 if (!map)
2573 return NULL;
2575 for (i = 0; i < map->n; ++i) {
2576 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2577 if (!map->p[i])
2578 goto error;
2580 return map;
2581 error:
2582 isl_map_free(map);
2583 return NULL;
2586 /* Eliminate the specified n dimensions starting at first from the
2587 * constraints, without removing the dimensions from the space.
2588 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2590 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2591 enum isl_dim_type type, unsigned first, unsigned n)
2593 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2596 /* Eliminate the specified n dimensions starting at first from the
2597 * constraints, without removing the dimensions from the space.
2598 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2600 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2601 unsigned first, unsigned n)
2603 return isl_set_eliminate(set, isl_dim_set, first, n);
2606 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2607 __isl_take isl_basic_map *bmap)
2609 isl_size v_div;
2611 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2612 if (v_div < 0)
2613 return isl_basic_map_free(bmap);
2614 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2615 if (!bmap)
2616 return NULL;
2617 bmap->n_div = 0;
2618 return isl_basic_map_finalize(bmap);
2621 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2622 __isl_take isl_basic_set *bset)
2624 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2627 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2629 int i;
2631 if (!map)
2632 return NULL;
2633 if (map->n == 0)
2634 return map;
2636 map = isl_map_cow(map);
2637 if (!map)
2638 return NULL;
2640 for (i = 0; i < map->n; ++i) {
2641 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2642 if (!map->p[i])
2643 goto error;
2645 return map;
2646 error:
2647 isl_map_free(map);
2648 return NULL;
2651 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2653 return isl_map_remove_divs(set);
2656 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2657 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2658 unsigned first, unsigned n)
2660 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2661 return isl_basic_map_free(bmap);
2662 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2663 return bmap;
2664 bmap = isl_basic_map_eliminate_vars(bmap,
2665 isl_basic_map_offset(bmap, type) - 1 + first, n);
2666 if (!bmap)
2667 return bmap;
2668 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2669 return bmap;
2670 bmap = isl_basic_map_drop(bmap, type, first, n);
2671 return bmap;
2674 /* Return true if the definition of the given div (recursively) involves
2675 * any of the given variables.
2677 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2678 unsigned first, unsigned n)
2680 int i;
2681 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2683 if (isl_int_is_zero(bmap->div[div][0]))
2684 return isl_bool_false;
2685 if (isl_seq_first_non_zero(bmap->div[div] + 1 + 1 + first, n) >= 0)
2686 return isl_bool_true;
2688 for (i = bmap->n_div - 1; i >= 0; --i) {
2689 isl_bool involves;
2691 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2692 continue;
2693 involves = div_involves_vars(bmap, i, first, n);
2694 if (involves < 0 || involves)
2695 return involves;
2698 return isl_bool_false;
2701 /* Try and add a lower and/or upper bound on "div" to "bmap"
2702 * based on inequality "i".
2703 * "total" is the total number of variables (excluding the divs).
2704 * "v" is a temporary object that can be used during the calculations.
2705 * If "lb" is set, then a lower bound should be constructed.
2706 * If "ub" is set, then an upper bound should be constructed.
2708 * The calling function has already checked that the inequality does not
2709 * reference "div", but we still need to check that the inequality is
2710 * of the right form. We'll consider the case where we want to construct
2711 * a lower bound. The construction of upper bounds is similar.
2713 * Let "div" be of the form
2715 * q = floor((a + f(x))/d)
2717 * We essentially check if constraint "i" is of the form
2719 * b + f(x) >= 0
2721 * so that we can use it to derive a lower bound on "div".
2722 * However, we allow a slightly more general form
2724 * b + g(x) >= 0
2726 * with the condition that the coefficients of g(x) - f(x) are all
2727 * divisible by d.
2728 * Rewriting this constraint as
2730 * 0 >= -b - g(x)
2732 * adding a + f(x) to both sides and dividing by d, we obtain
2734 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2736 * Taking the floor on both sides, we obtain
2738 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2740 * or
2742 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2744 * In the case of an upper bound, we construct the constraint
2746 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2749 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2750 __isl_take isl_basic_map *bmap, int div, int i,
2751 unsigned total, isl_int v, int lb, int ub)
2753 int j;
2755 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2756 if (lb) {
2757 isl_int_sub(v, bmap->ineq[i][1 + j],
2758 bmap->div[div][1 + 1 + j]);
2759 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2761 if (ub) {
2762 isl_int_add(v, bmap->ineq[i][1 + j],
2763 bmap->div[div][1 + 1 + j]);
2764 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2767 if (!lb && !ub)
2768 return bmap;
2770 bmap = isl_basic_map_cow(bmap);
2771 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2772 if (lb) {
2773 int k = isl_basic_map_alloc_inequality(bmap);
2774 if (k < 0)
2775 goto error;
2776 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2777 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2778 bmap->div[div][1 + j]);
2779 isl_int_cdiv_q(bmap->ineq[k][j],
2780 bmap->ineq[k][j], bmap->div[div][0]);
2782 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2784 if (ub) {
2785 int k = isl_basic_map_alloc_inequality(bmap);
2786 if (k < 0)
2787 goto error;
2788 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2789 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2790 bmap->div[div][1 + j]);
2791 isl_int_fdiv_q(bmap->ineq[k][j],
2792 bmap->ineq[k][j], bmap->div[div][0]);
2794 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2797 return bmap;
2798 error:
2799 isl_basic_map_free(bmap);
2800 return NULL;
2803 /* This function is called right before "div" is eliminated from "bmap"
2804 * using Fourier-Motzkin.
2805 * Look through the constraints of "bmap" for constraints on the argument
2806 * of the integer division and use them to construct constraints on the
2807 * integer division itself. These constraints can then be combined
2808 * during the Fourier-Motzkin elimination.
2809 * Note that it is only useful to introduce lower bounds on "div"
2810 * if "bmap" already contains upper bounds on "div" as the newly
2811 * introduce lower bounds can then be combined with the pre-existing
2812 * upper bounds. Similarly for upper bounds.
2813 * We therefore first check if "bmap" contains any lower and/or upper bounds
2814 * on "div".
2816 * It is interesting to note that the introduction of these constraints
2817 * can indeed lead to more accurate results, even when compared to
2818 * deriving constraints on the argument of "div" from constraints on "div".
2819 * Consider, for example, the set
2821 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2823 * The second constraint can be rewritten as
2825 * 2 * [(-i-2j+3)/4] + k >= 0
2827 * from which we can derive
2829 * -i - 2j + 3 >= -2k
2831 * or
2833 * i + 2j <= 3 + 2k
2835 * Combined with the first constraint, we obtain
2837 * -3 <= 3 + 2k or k >= -3
2839 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2840 * the first constraint, we obtain
2842 * [(i + 2j)/4] >= [-3/4] = -1
2844 * Combining this constraint with the second constraint, we obtain
2846 * k >= -2
2848 static __isl_give isl_basic_map *insert_bounds_on_div(
2849 __isl_take isl_basic_map *bmap, int div)
2851 int i;
2852 int check_lb, check_ub;
2853 isl_int v;
2854 isl_size v_div;
2856 if (!bmap)
2857 return NULL;
2859 if (isl_int_is_zero(bmap->div[div][0]))
2860 return bmap;
2862 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2863 if (v_div < 0)
2864 return isl_basic_map_free(bmap);
2866 check_lb = 0;
2867 check_ub = 0;
2868 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2869 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2870 if (s > 0)
2871 check_ub = 1;
2872 if (s < 0)
2873 check_lb = 1;
2876 if (!check_lb && !check_ub)
2877 return bmap;
2879 isl_int_init(v);
2881 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2882 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2883 continue;
2885 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2886 check_lb, check_ub);
2889 isl_int_clear(v);
2891 return bmap;
2894 /* Remove all divs (recursively) involving any of the given variables
2895 * in their definitions.
2897 static __isl_give isl_basic_map *remove_divs_involving_vars(
2898 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2900 int i;
2902 for (i = bmap->n_div - 1; i >= 0; --i) {
2903 isl_bool involves;
2905 involves = div_involves_vars(bmap, i, first, n);
2906 if (involves < 0)
2907 return isl_basic_map_free(bmap);
2908 if (!involves)
2909 continue;
2910 bmap = insert_bounds_on_div(bmap, i);
2911 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2912 if (!bmap)
2913 return NULL;
2914 i = bmap->n_div;
2917 return bmap;
2920 /* Remove all divs (recursively) involving any of the given dimensions
2921 * in their definitions.
2923 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2924 __isl_take isl_basic_map *bmap,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 isl_size off;
2929 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2930 return isl_basic_map_free(bmap);
2931 off = isl_basic_map_var_offset(bmap, type);
2932 if (off < 0)
2933 return isl_basic_map_free(bmap);
2934 first += off;
2936 return remove_divs_involving_vars(bmap, first, n);
2939 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2940 __isl_take isl_basic_set *bset,
2941 enum isl_dim_type type, unsigned first, unsigned n)
2943 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2946 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2947 enum isl_dim_type type, unsigned first, unsigned n)
2949 int i;
2951 if (!map)
2952 return NULL;
2953 if (map->n == 0)
2954 return map;
2956 map = isl_map_cow(map);
2957 if (!map)
2958 return NULL;
2960 for (i = 0; i < map->n; ++i) {
2961 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2962 type, first, n);
2963 if (!map->p[i])
2964 goto error;
2966 return map;
2967 error:
2968 isl_map_free(map);
2969 return NULL;
2972 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2975 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2976 type, first, n));
2979 /* Does the description of "bmap" depend on the specified dimensions?
2980 * We also check whether the dimensions appear in any of the div definitions.
2981 * In principle there is no need for this check. If the dimensions appear
2982 * in a div definition, they also appear in the defining constraints of that
2983 * div.
2985 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2986 enum isl_dim_type type, unsigned first, unsigned n)
2988 int i;
2990 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2991 return isl_bool_error;
2993 first += isl_basic_map_offset(bmap, type);
2994 for (i = 0; i < bmap->n_eq; ++i)
2995 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2996 return isl_bool_true;
2997 for (i = 0; i < bmap->n_ineq; ++i)
2998 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2999 return isl_bool_true;
3000 for (i = 0; i < bmap->n_div; ++i) {
3001 if (isl_int_is_zero(bmap->div[i][0]))
3002 continue;
3003 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
3004 return isl_bool_true;
3007 return isl_bool_false;
3010 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
3011 enum isl_dim_type type, unsigned first, unsigned n)
3013 int i;
3015 if (isl_map_check_range(map, type, first, n) < 0)
3016 return isl_bool_error;
3018 for (i = 0; i < map->n; ++i) {
3019 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
3020 type, first, n);
3021 if (involves < 0 || involves)
3022 return involves;
3025 return isl_bool_false;
3028 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
3029 enum isl_dim_type type, unsigned first, unsigned n)
3031 return isl_basic_map_involves_dims(bset, type, first, n);
3034 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
3035 enum isl_dim_type type, unsigned first, unsigned n)
3037 return isl_map_involves_dims(set, type, first, n);
3040 /* Does "bset" involve any local variables, i.e., integer divisions?
3042 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
3044 isl_size n;
3046 n = isl_basic_set_dim(bset, isl_dim_div);
3047 if (n < 0)
3048 return isl_bool_error;
3049 return isl_bool_ok(n > 0);
3052 /* isl_set_every_basic_set callback that checks whether "bset"
3053 * is free of local variables.
3055 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3057 return isl_bool_not(isl_basic_set_involves_locals(bset));
3060 /* Does "set" involve any local variables, i.e., integer divisions?
3062 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3064 isl_bool no_locals;
3066 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3067 return isl_bool_not(no_locals);
3070 /* Drop all constraints in bmap that involve any of the dimensions
3071 * first to first+n-1.
3072 * This function only performs the actual removal of constraints.
3074 * This function should not call finalize since it is used by
3075 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3077 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3078 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3080 int i;
3082 if (n == 0)
3083 return bmap;
3085 bmap = isl_basic_map_cow(bmap);
3087 if (!bmap)
3088 return NULL;
3090 for (i = bmap->n_eq - 1; i >= 0; --i) {
3091 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3092 continue;
3093 if (isl_basic_map_drop_equality(bmap, i) < 0)
3094 return isl_basic_map_free(bmap);
3097 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3098 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3099 continue;
3100 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3101 return isl_basic_map_free(bmap);
3104 return bmap;
3107 /* Drop all constraints in bset that involve any of the dimensions
3108 * first to first+n-1.
3109 * This function only performs the actual removal of constraints.
3111 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3112 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3114 return isl_basic_map_drop_constraints_involving(bset, first, n);
3117 /* Drop all constraints in bmap that do not involve any of the dimensions
3118 * first to first + n - 1 of the given type.
3120 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3121 __isl_take isl_basic_map *bmap,
3122 enum isl_dim_type type, unsigned first, unsigned n)
3124 int i;
3126 if (n == 0) {
3127 isl_space *space = isl_basic_map_get_space(bmap);
3128 isl_basic_map_free(bmap);
3129 return isl_basic_map_universe(space);
3131 bmap = isl_basic_map_cow(bmap);
3132 if (!bmap)
3133 return NULL;
3135 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3136 return isl_basic_map_free(bmap);
3138 first += isl_basic_map_offset(bmap, type) - 1;
3140 for (i = bmap->n_eq - 1; i >= 0; --i) {
3141 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3142 continue;
3143 if (isl_basic_map_drop_equality(bmap, i) < 0)
3144 return isl_basic_map_free(bmap);
3147 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3148 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3149 continue;
3150 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3151 return isl_basic_map_free(bmap);
3154 bmap = isl_basic_map_add_known_div_constraints(bmap);
3155 return bmap;
3158 /* Drop all constraints in bset that do not involve any of the dimensions
3159 * first to first + n - 1 of the given type.
3161 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3162 __isl_take isl_basic_set *bset,
3163 enum isl_dim_type type, unsigned first, unsigned n)
3165 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3166 type, first, n);
3169 /* Drop all constraints in bmap that involve any of the dimensions
3170 * first to first + n - 1 of the given type.
3172 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3173 __isl_take isl_basic_map *bmap,
3174 enum isl_dim_type type, unsigned first, unsigned n)
3176 if (!bmap)
3177 return NULL;
3178 if (n == 0)
3179 return bmap;
3181 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3182 return isl_basic_map_free(bmap);
3184 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3185 first += isl_basic_map_offset(bmap, type) - 1;
3186 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3187 bmap = isl_basic_map_add_known_div_constraints(bmap);
3188 return bmap;
3191 /* Drop all constraints in bset that involve any of the dimensions
3192 * first to first + n - 1 of the given type.
3194 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3195 __isl_take isl_basic_set *bset,
3196 enum isl_dim_type type, unsigned first, unsigned n)
3198 return isl_basic_map_drop_constraints_involving_dims(bset,
3199 type, first, n);
3202 /* Drop constraints from "map" by applying "drop" to each basic map.
3204 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3205 enum isl_dim_type type, unsigned first, unsigned n,
3206 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3207 enum isl_dim_type type, unsigned first, unsigned n))
3209 int i;
3211 if (isl_map_check_range(map, type, first, n) < 0)
3212 return isl_map_free(map);
3214 map = isl_map_cow(map);
3215 if (!map)
3216 return NULL;
3218 for (i = 0; i < map->n; ++i) {
3219 map->p[i] = drop(map->p[i], type, first, n);
3220 if (!map->p[i])
3221 return isl_map_free(map);
3224 if (map->n > 1)
3225 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3227 return map;
3230 /* Drop all constraints in map that involve any of the dimensions
3231 * first to first + n - 1 of the given type.
3233 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3234 __isl_take isl_map *map,
3235 enum isl_dim_type type, unsigned first, unsigned n)
3237 if (n == 0)
3238 return map;
3239 return drop_constraints(map, type, first, n,
3240 &isl_basic_map_drop_constraints_involving_dims);
3243 /* Drop all constraints in "map" that do not involve any of the dimensions
3244 * first to first + n - 1 of the given type.
3246 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3247 __isl_take isl_map *map,
3248 enum isl_dim_type type, unsigned first, unsigned n)
3250 if (n == 0) {
3251 isl_space *space = isl_map_get_space(map);
3252 isl_map_free(map);
3253 return isl_map_universe(space);
3255 return drop_constraints(map, type, first, n,
3256 &isl_basic_map_drop_constraints_not_involving_dims);
3259 /* Drop all constraints in set that involve any of the dimensions
3260 * first to first + n - 1 of the given type.
3262 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3263 __isl_take isl_set *set,
3264 enum isl_dim_type type, unsigned first, unsigned n)
3266 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3269 /* Drop all constraints in "set" that do not involve any of the dimensions
3270 * first to first + n - 1 of the given type.
3272 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3273 __isl_take isl_set *set,
3274 enum isl_dim_type type, unsigned first, unsigned n)
3276 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3279 /* Does local variable "div" of "bmap" have a complete explicit representation?
3280 * Having a complete explicit representation requires not only
3281 * an explicit representation, but also that all local variables
3282 * that appear in this explicit representation in turn have
3283 * a complete explicit representation.
3285 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3287 int i;
3288 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3289 isl_bool marked;
3291 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3292 if (marked < 0 || marked)
3293 return isl_bool_not(marked);
3295 for (i = bmap->n_div - 1; i >= 0; --i) {
3296 isl_bool known;
3298 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3299 continue;
3300 known = isl_basic_map_div_is_known(bmap, i);
3301 if (known < 0 || !known)
3302 return known;
3305 return isl_bool_true;
3308 /* Remove all divs that are unknown or defined in terms of unknown divs.
3310 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3311 __isl_take isl_basic_map *bmap)
3313 int i;
3315 if (!bmap)
3316 return NULL;
3318 for (i = bmap->n_div - 1; i >= 0; --i) {
3319 if (isl_basic_map_div_is_known(bmap, i))
3320 continue;
3321 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3322 if (!bmap)
3323 return NULL;
3324 i = bmap->n_div;
3327 return bmap;
3330 /* Remove all divs that are unknown or defined in terms of unknown divs.
3332 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3333 __isl_take isl_basic_set *bset)
3335 return isl_basic_map_remove_unknown_divs(bset);
3338 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3340 int i;
3342 if (!map)
3343 return NULL;
3344 if (map->n == 0)
3345 return map;
3347 map = isl_map_cow(map);
3348 if (!map)
3349 return NULL;
3351 for (i = 0; i < map->n; ++i) {
3352 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3353 if (!map->p[i])
3354 goto error;
3356 return map;
3357 error:
3358 isl_map_free(map);
3359 return NULL;
3362 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3364 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3367 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3368 __isl_take isl_basic_set *bset,
3369 enum isl_dim_type type, unsigned first, unsigned n)
3371 isl_basic_map *bmap = bset_to_bmap(bset);
3372 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3373 return bset_from_bmap(bmap);
3376 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3377 enum isl_dim_type type, unsigned first, unsigned n)
3379 int i;
3381 if (n == 0)
3382 return map;
3384 map = isl_map_cow(map);
3385 if (isl_map_check_range(map, type, first, n) < 0)
3386 return isl_map_free(map);
3388 for (i = 0; i < map->n; ++i) {
3389 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3390 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3391 if (!map->p[i])
3392 goto error;
3394 map = isl_map_drop(map, type, first, n);
3395 return map;
3396 error:
3397 isl_map_free(map);
3398 return NULL;
3401 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3402 enum isl_dim_type type, unsigned first, unsigned n)
3404 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3405 type, first, n));
3408 /* Project out n inputs starting at first using Fourier-Motzkin */
3409 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3410 unsigned first, unsigned n)
3412 return isl_map_remove_dims(map, isl_dim_in, first, n);
3415 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3416 FILE *out, int indent)
3418 isl_printer *p;
3420 if (!bset) {
3421 fprintf(out, "null basic set\n");
3422 return;
3425 fprintf(out, "%*s", indent, "");
3426 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3427 bset->ref, bset->dim->nparam, bset->dim->n_out,
3428 bset->extra, bset->flags);
3430 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3431 p = isl_printer_set_dump(p, 1);
3432 p = isl_printer_set_indent(p, indent);
3433 p = isl_printer_start_line(p);
3434 p = isl_printer_print_basic_set(p, bset);
3435 p = isl_printer_end_line(p);
3436 isl_printer_free(p);
3439 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3440 FILE *out, int indent)
3442 isl_printer *p;
3444 if (!bmap) {
3445 fprintf(out, "null basic map\n");
3446 return;
3449 fprintf(out, "%*s", indent, "");
3450 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3451 "flags: %x, n_name: %d\n",
3452 bmap->ref,
3453 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3454 bmap->extra, bmap->flags, bmap->dim->n_id);
3456 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3457 p = isl_printer_set_dump(p, 1);
3458 p = isl_printer_set_indent(p, indent);
3459 p = isl_printer_start_line(p);
3460 p = isl_printer_print_basic_map(p, bmap);
3461 p = isl_printer_end_line(p);
3462 isl_printer_free(p);
3465 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3466 unsigned pos)
3468 isl_size total;
3470 total = isl_basic_map_dim(bmap, isl_dim_all);
3471 if (total < 0)
3472 return isl_basic_map_free(bmap);
3473 if (pos >= bmap->n_ineq)
3474 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3475 "invalid position", return isl_basic_map_free(bmap));
3476 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3477 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3479 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3480 return bmap;
3483 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3484 unsigned flags)
3486 if (isl_space_check_is_set(space) < 0)
3487 goto error;
3488 return isl_map_alloc_space(space, n, flags);
3489 error:
3490 isl_space_free(space);
3491 return NULL;
3494 /* Make sure "map" has room for at least "n" more basic maps.
3496 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3498 int i;
3499 struct isl_map *grown = NULL;
3501 if (!map)
3502 return NULL;
3503 isl_assert(map->ctx, n >= 0, goto error);
3504 if (map->n + n <= map->size)
3505 return map;
3506 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3507 if (!grown)
3508 goto error;
3509 for (i = 0; i < map->n; ++i) {
3510 grown->p[i] = isl_basic_map_copy(map->p[i]);
3511 if (!grown->p[i])
3512 goto error;
3513 grown->n++;
3515 isl_map_free(map);
3516 return grown;
3517 error:
3518 isl_map_free(grown);
3519 isl_map_free(map);
3520 return NULL;
3523 /* Make sure "set" has room for at least "n" more basic sets.
3525 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3527 return set_from_map(isl_map_grow(set_to_map(set), n));
3530 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3532 return isl_map_from_basic_map(bset);
3535 /* This function performs the same operation as isl_set_from_basic_set,
3536 * but is considered as a function on an isl_basic_set when exported.
3538 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3540 return isl_set_from_basic_set(bset);
3543 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3545 struct isl_map *map;
3547 if (!bmap)
3548 return NULL;
3550 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3551 return isl_map_add_basic_map(map, bmap);
3554 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3555 __isl_take isl_basic_set *bset)
3557 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3558 bset_to_bmap(bset)));
3561 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3563 return isl_map_free(set);
3566 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3568 int i;
3570 if (!set) {
3571 fprintf(out, "null set\n");
3572 return;
3575 fprintf(out, "%*s", indent, "");
3576 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3577 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3578 set->flags);
3579 for (i = 0; i < set->n; ++i) {
3580 fprintf(out, "%*s", indent, "");
3581 fprintf(out, "basic set %d:\n", i);
3582 isl_basic_set_print_internal(set->p[i], out, indent+4);
3586 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3588 int i;
3590 if (!map) {
3591 fprintf(out, "null map\n");
3592 return;
3595 fprintf(out, "%*s", indent, "");
3596 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3597 "flags: %x, n_name: %d\n",
3598 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3599 map->dim->n_out, map->flags, map->dim->n_id);
3600 for (i = 0; i < map->n; ++i) {
3601 fprintf(out, "%*s", indent, "");
3602 fprintf(out, "basic map %d:\n", i);
3603 isl_basic_map_print_internal(map->p[i], out, indent+4);
3607 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3609 static isl_stat isl_basic_map_check_compatible_domain(
3610 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3612 isl_bool ok;
3614 ok = isl_basic_map_compatible_domain(bmap, bset);
3615 if (ok < 0)
3616 return isl_stat_error;
3617 if (!ok)
3618 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3619 "incompatible spaces", return isl_stat_error);
3621 return isl_stat_ok;
3624 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3625 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3627 struct isl_basic_map *bmap_domain;
3628 isl_size dim;
3630 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3631 goto error;
3633 dim = isl_basic_set_dim(bset, isl_dim_set);
3634 if (dim < 0)
3635 goto error;
3636 if (dim != 0 &&
3637 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3638 goto error;
3640 bmap = isl_basic_map_cow(bmap);
3641 if (!bmap)
3642 goto error;
3643 bmap = isl_basic_map_extend(bmap,
3644 bset->n_div, bset->n_eq, bset->n_ineq);
3645 bmap_domain = isl_basic_map_from_domain(bset);
3646 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3648 bmap = isl_basic_map_simplify(bmap);
3649 return isl_basic_map_finalize(bmap);
3650 error:
3651 isl_basic_map_free(bmap);
3652 isl_basic_set_free(bset);
3653 return NULL;
3656 /* Check that the space of "bset" is the same as that of the range of "bmap".
3658 static isl_stat isl_basic_map_check_compatible_range(
3659 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3661 isl_bool ok;
3663 ok = isl_basic_map_compatible_range(bmap, bset);
3664 if (ok < 0)
3665 return isl_stat_error;
3666 if (!ok)
3667 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3668 "incompatible spaces", return isl_stat_error);
3670 return isl_stat_ok;
3673 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3674 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3676 struct isl_basic_map *bmap_range;
3677 isl_size dim;
3679 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3680 goto error;
3682 dim = isl_basic_set_dim(bset, isl_dim_set);
3683 if (dim < 0)
3684 goto error;
3685 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3686 goto error;
3688 if (isl_basic_set_plain_is_universe(bset)) {
3689 isl_basic_set_free(bset);
3690 return bmap;
3693 bmap = isl_basic_map_cow(bmap);
3694 if (!bmap)
3695 goto error;
3696 bmap = isl_basic_map_extend(bmap,
3697 bset->n_div, bset->n_eq, bset->n_ineq);
3698 bmap_range = bset_to_bmap(bset);
3699 bmap = add_constraints(bmap, bmap_range, 0, 0);
3701 bmap = isl_basic_map_simplify(bmap);
3702 return isl_basic_map_finalize(bmap);
3703 error:
3704 isl_basic_map_free(bmap);
3705 isl_basic_set_free(bset);
3706 return NULL;
3709 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3710 __isl_keep isl_vec *vec)
3712 int i;
3713 isl_size total;
3714 isl_int s;
3716 total = isl_basic_map_dim(bmap, isl_dim_all);
3717 if (total < 0 || !vec)
3718 return isl_bool_error;
3720 if (1 + total != vec->size)
3721 return isl_bool_false;
3723 isl_int_init(s);
3725 for (i = 0; i < bmap->n_eq; ++i) {
3726 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3727 if (!isl_int_is_zero(s)) {
3728 isl_int_clear(s);
3729 return isl_bool_false;
3733 for (i = 0; i < bmap->n_ineq; ++i) {
3734 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3735 if (isl_int_is_neg(s)) {
3736 isl_int_clear(s);
3737 return isl_bool_false;
3741 isl_int_clear(s);
3743 return isl_bool_true;
3746 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3747 __isl_keep isl_vec *vec)
3749 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3752 __isl_give isl_basic_map *isl_basic_map_intersect(
3753 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3755 struct isl_vec *sample = NULL;
3756 isl_space *space1, *space2;
3757 isl_size dim1, dim2, nparam1, nparam2;
3759 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3760 goto error;
3761 space1 = isl_basic_map_peek_space(bmap1);
3762 space2 = isl_basic_map_peek_space(bmap2);
3763 dim1 = isl_space_dim(space1, isl_dim_all);
3764 dim2 = isl_space_dim(space2, isl_dim_all);
3765 nparam1 = isl_space_dim(space1, isl_dim_param);
3766 nparam2 = isl_space_dim(space2, isl_dim_param);
3767 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3768 goto error;
3769 if (dim1 == nparam1 && dim2 != nparam2)
3770 return isl_basic_map_intersect(bmap2, bmap1);
3772 if (dim2 != nparam2 &&
3773 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3774 goto error;
3776 if (isl_basic_map_plain_is_empty(bmap1)) {
3777 isl_basic_map_free(bmap2);
3778 return bmap1;
3780 if (isl_basic_map_plain_is_empty(bmap2)) {
3781 isl_basic_map_free(bmap1);
3782 return bmap2;
3785 if (bmap1->sample &&
3786 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3787 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3788 sample = isl_vec_copy(bmap1->sample);
3789 else if (bmap2->sample &&
3790 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3791 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3792 sample = isl_vec_copy(bmap2->sample);
3794 bmap1 = isl_basic_map_cow(bmap1);
3795 if (!bmap1)
3796 goto error;
3797 bmap1 = isl_basic_map_extend(bmap1,
3798 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3799 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3801 if (!bmap1)
3802 isl_vec_free(sample);
3803 else if (sample) {
3804 isl_vec_free(bmap1->sample);
3805 bmap1->sample = sample;
3808 bmap1 = isl_basic_map_simplify(bmap1);
3809 return isl_basic_map_finalize(bmap1);
3810 error:
3811 if (sample)
3812 isl_vec_free(sample);
3813 isl_basic_map_free(bmap1);
3814 isl_basic_map_free(bmap2);
3815 return NULL;
3818 __isl_give isl_basic_set *isl_basic_set_intersect(
3819 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3821 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3822 bset_to_bmap(bset2)));
3825 /* Intersect the parameter domain of "bmap" with "bset".
3827 * isl_basic_map_intersect handles this as a special case.
3829 __isl_give isl_basic_map *isl_basic_map_intersect_params(
3830 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3832 return isl_basic_map_intersect(bmap, bset);
3835 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3836 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3838 isl_basic_map *bmap;
3840 bmap = bset_to_bmap(bset1);
3841 bmap = isl_basic_map_intersect_params(bmap, bset2);
3842 return bset_from_bmap(bmap);
3845 /* Does "map" consist of a single disjunct, without any local variables?
3847 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3849 isl_size n_div;
3851 if (!map)
3852 return isl_bool_error;
3853 if (map->n != 1)
3854 return isl_bool_false;
3855 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3856 if (n_div < 0)
3857 return isl_bool_error;
3858 if (n_div != 0)
3859 return isl_bool_false;
3860 return isl_bool_true;
3863 /* Check that "map" consists of a single disjunct, without any local variables.
3865 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3867 isl_bool ok;
3869 ok = is_convex_no_locals(map);
3870 if (ok < 0)
3871 return isl_stat_error;
3872 if (ok)
3873 return isl_stat_ok;
3875 isl_die(isl_map_get_ctx(map), isl_error_internal,
3876 "unexpectedly not convex or involving local variables",
3877 return isl_stat_error);
3880 /* Special case of isl_map_intersect, where both map1 and map2
3881 * are convex, without any divs and such that either map1 or map2
3882 * contains a single constraint. This constraint is then simply
3883 * added to the other map.
3885 static __isl_give isl_map *map_intersect_add_constraint(
3886 __isl_take isl_map *map1, __isl_take isl_map *map2)
3888 if (check_convex_no_locals(map1) < 0 ||
3889 check_convex_no_locals(map2) < 0)
3890 goto error;
3892 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3893 return isl_map_intersect(map2, map1);
3895 map1 = isl_map_cow(map1);
3896 if (!map1)
3897 goto error;
3898 if (isl_map_plain_is_empty(map1)) {
3899 isl_map_free(map2);
3900 return map1;
3902 if (map2->p[0]->n_eq == 1)
3903 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3904 else
3905 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3906 map2->p[0]->ineq[0]);
3908 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3909 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3910 if (!map1->p[0])
3911 goto error;
3913 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3914 isl_basic_map_free(map1->p[0]);
3915 map1->n = 0;
3918 isl_map_free(map2);
3920 map1 = isl_map_unmark_normalized(map1);
3921 return map1;
3922 error:
3923 isl_map_free(map1);
3924 isl_map_free(map2);
3925 return NULL;
3928 /* map2 may be either a parameter domain or a map living in the same
3929 * space as map1.
3931 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3932 __isl_take isl_map *map2)
3934 unsigned flags = 0;
3935 isl_bool equal;
3936 isl_map *result;
3937 int i, j;
3938 isl_size dim2, nparam2;
3940 if (!map1 || !map2)
3941 goto error;
3943 if ((isl_map_plain_is_empty(map1) ||
3944 isl_map_plain_is_universe(map2)) &&
3945 isl_space_is_equal(map1->dim, map2->dim)) {
3946 isl_map_free(map2);
3947 return map1;
3949 if ((isl_map_plain_is_empty(map2) ||
3950 isl_map_plain_is_universe(map1)) &&
3951 isl_space_is_equal(map1->dim, map2->dim)) {
3952 isl_map_free(map1);
3953 return map2;
3956 if (is_convex_no_locals(map1) == isl_bool_true &&
3957 is_convex_no_locals(map2) == isl_bool_true &&
3958 isl_space_is_equal(map1->dim, map2->dim) &&
3959 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3960 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3961 return map_intersect_add_constraint(map1, map2);
3963 equal = isl_map_plain_is_equal(map1, map2);
3964 if (equal < 0)
3965 goto error;
3966 if (equal) {
3967 isl_map_free(map2);
3968 return map1;
3971 dim2 = isl_map_dim(map2, isl_dim_all);
3972 nparam2 = isl_map_dim(map2, isl_dim_param);
3973 if (dim2 < 0 || nparam2 < 0)
3974 goto error;
3975 if (dim2 != nparam2)
3976 isl_assert(map1->ctx,
3977 isl_space_is_equal(map1->dim, map2->dim), goto error);
3979 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3980 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3981 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3983 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3984 map1->n * map2->n, flags);
3985 if (!result)
3986 goto error;
3987 for (i = 0; i < map1->n; ++i)
3988 for (j = 0; j < map2->n; ++j) {
3989 struct isl_basic_map *part;
3990 part = isl_basic_map_intersect(
3991 isl_basic_map_copy(map1->p[i]),
3992 isl_basic_map_copy(map2->p[j]));
3993 if (isl_basic_map_is_empty(part) < 0)
3994 part = isl_basic_map_free(part);
3995 result = isl_map_add_basic_map(result, part);
3996 if (!result)
3997 goto error;
3999 isl_map_free(map1);
4000 isl_map_free(map2);
4001 return result;
4002 error:
4003 isl_map_free(map1);
4004 isl_map_free(map2);
4005 return NULL;
4008 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
4009 __isl_take isl_map *map2)
4011 if (isl_map_check_equal_space(map1, map2) < 0)
4012 goto error;
4013 return map_intersect_internal(map1, map2);
4014 error:
4015 isl_map_free(map1);
4016 isl_map_free(map2);
4017 return NULL;
4020 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
4021 __isl_take isl_map *map2)
4023 isl_map_align_params_bin(&map1, &map2);
4024 return map_intersect(map1, map2);
4027 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
4028 __isl_take isl_set *set2)
4030 return set_from_map(isl_map_intersect(set_to_map(set1),
4031 set_to_map(set2)));
4034 /* map_intersect_internal accepts intersections
4035 * with parameter domains, so we can just call that function.
4037 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
4038 __isl_take isl_set *params)
4040 isl_map_align_params_set(&map, &params);
4041 return map_intersect_internal(map, params);
4044 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4045 __isl_take isl_set *params)
4047 return isl_map_intersect_params(set, params);
4050 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4052 isl_space *space;
4053 unsigned pos;
4054 isl_size n1, n2;
4056 if (!bmap)
4057 return NULL;
4058 bmap = isl_basic_map_cow(bmap);
4059 if (!bmap)
4060 return NULL;
4061 space = isl_space_reverse(isl_space_copy(bmap->dim));
4062 pos = isl_basic_map_offset(bmap, isl_dim_in);
4063 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4064 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4065 if (n1 < 0 || n2 < 0)
4066 bmap = isl_basic_map_free(bmap);
4067 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4068 return isl_basic_map_reset_space(bmap, space);
4071 /* Given a basic map where the tuple of type "type" is a wrapped map,
4072 * swap domain and range of that wrapped map.
4074 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4075 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4077 isl_space *space;
4078 isl_size offset, n1, n2;
4080 space = isl_basic_map_peek_space(bmap);
4081 offset = isl_basic_map_var_offset(bmap, type);
4082 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4083 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4084 if (offset < 0 || n1 < 0 || n2 < 0)
4085 return isl_basic_map_free(bmap);
4087 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4089 space = isl_basic_map_take_space(bmap);
4090 space = isl_space_reverse_wrapped(space, type);
4091 bmap = isl_basic_map_restore_space(bmap, space);
4093 return bmap;
4096 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4097 * (B -> A) -> C.
4099 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4100 __isl_take isl_basic_map *bmap)
4102 isl_space *space;
4104 space = isl_basic_map_peek_space(bmap);
4105 if (isl_space_check_domain_is_wrapping(space) < 0)
4106 return isl_basic_map_free(bmap);
4107 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4109 return bmap;
4112 /* Given a basic map A -> (B -> C), return the corresponding basic map
4113 * A -> (C -> B).
4115 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4116 __isl_take isl_basic_map *bmap)
4118 isl_space *space;
4120 space = isl_basic_map_peek_space(bmap);
4121 if (isl_space_check_range_is_wrapping(space) < 0)
4122 return isl_basic_map_free(bmap);
4123 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4125 return bmap;
4128 /* Given a basic map that is actually a basic set (A -> B),
4129 * return the corresponding basic set (B -> A) as a basic map.
4131 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4132 __isl_take isl_basic_map *bmap)
4134 isl_space *space;
4136 space = isl_basic_map_peek_space(bmap);
4137 if (isl_space_check_is_wrapping(space) < 0)
4138 return isl_basic_map_free(bmap);
4139 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4141 return bmap;
4144 static __isl_give isl_basic_map *basic_map_space_reset(
4145 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4147 isl_space *space;
4149 if (!bmap)
4150 return NULL;
4151 if (!isl_space_is_named_or_nested(bmap->dim, type))
4152 return bmap;
4154 space = isl_basic_map_get_space(bmap);
4155 space = isl_space_reset(space, type);
4156 bmap = isl_basic_map_reset_space(bmap, space);
4157 return bmap;
4160 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4161 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4162 unsigned pos, unsigned n)
4164 isl_bool rational, is_empty;
4165 isl_space *res_space;
4166 struct isl_basic_map *res;
4167 struct isl_dim_map *dim_map;
4168 isl_size total;
4169 unsigned off;
4170 enum isl_dim_type t;
4172 if (n == 0)
4173 return basic_map_space_reset(bmap, type);
4175 is_empty = isl_basic_map_plain_is_empty(bmap);
4176 total = isl_basic_map_dim(bmap, isl_dim_all);
4177 if (is_empty < 0 || total < 0)
4178 return isl_basic_map_free(bmap);
4179 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4180 type, pos, n);
4181 if (!res_space)
4182 return isl_basic_map_free(bmap);
4183 if (is_empty) {
4184 isl_basic_map_free(bmap);
4185 return isl_basic_map_empty(res_space);
4188 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4189 off = 0;
4190 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4191 isl_size dim;
4193 if (t != type) {
4194 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4195 } else {
4196 isl_size size = isl_basic_map_dim(bmap, t);
4197 if (size < 0)
4198 dim_map = isl_dim_map_free(dim_map);
4199 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4200 0, pos, off);
4201 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4202 pos, size - pos, off + pos + n);
4204 dim = isl_space_dim(res_space, t);
4205 if (dim < 0)
4206 dim_map = isl_dim_map_free(dim_map);
4207 off += dim;
4209 isl_dim_map_div(dim_map, bmap, off);
4211 res = isl_basic_map_alloc_space(res_space,
4212 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4213 rational = isl_basic_map_is_rational(bmap);
4214 if (rational < 0)
4215 res = isl_basic_map_free(res);
4216 if (rational)
4217 res = isl_basic_map_set_rational(res);
4218 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4219 return isl_basic_map_finalize(res);
4222 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4223 __isl_take isl_basic_set *bset,
4224 enum isl_dim_type type, unsigned pos, unsigned n)
4226 return isl_basic_map_insert_dims(bset, type, pos, n);
4229 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4230 enum isl_dim_type type, unsigned n)
4232 isl_size dim;
4234 dim = isl_basic_map_dim(bmap, type);
4235 if (dim < 0)
4236 return isl_basic_map_free(bmap);
4237 return isl_basic_map_insert_dims(bmap, type, dim, n);
4240 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4241 enum isl_dim_type type, unsigned n)
4243 if (!bset)
4244 return NULL;
4245 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4246 return isl_basic_map_add_dims(bset, type, n);
4247 error:
4248 isl_basic_set_free(bset);
4249 return NULL;
4252 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4253 enum isl_dim_type type)
4255 isl_space *space;
4257 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4258 return map;
4260 space = isl_map_get_space(map);
4261 space = isl_space_reset(space, type);
4262 map = isl_map_reset_space(map, space);
4263 return map;
4266 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4267 enum isl_dim_type type, unsigned pos, unsigned n)
4269 int i;
4270 isl_space *space;
4272 if (n == 0)
4273 return map_space_reset(map, type);
4275 map = isl_map_cow(map);
4276 if (!map)
4277 return NULL;
4279 for (i = 0; i < map->n; ++i) {
4280 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4281 if (!map->p[i])
4282 goto error;
4285 space = isl_map_take_space(map);
4286 space = isl_space_insert_dims(space, type, pos, n);
4287 map = isl_map_restore_space(map, space);
4289 return map;
4290 error:
4291 isl_map_free(map);
4292 return NULL;
4295 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4296 enum isl_dim_type type, unsigned pos, unsigned n)
4298 return isl_map_insert_dims(set, type, pos, n);
4301 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4302 enum isl_dim_type type, unsigned n)
4304 isl_size dim;
4306 dim = isl_map_dim(map, type);
4307 if (dim < 0)
4308 return isl_map_free(map);
4309 return isl_map_insert_dims(map, type, dim, n);
4312 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4313 enum isl_dim_type type, unsigned n)
4315 if (!set)
4316 return NULL;
4317 isl_assert(set->ctx, type != isl_dim_in, goto error);
4318 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4319 error:
4320 isl_set_free(set);
4321 return NULL;
4324 __isl_give isl_basic_map *isl_basic_map_move_dims(
4325 __isl_take isl_basic_map *bmap,
4326 enum isl_dim_type dst_type, unsigned dst_pos,
4327 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4329 isl_space *space;
4330 struct isl_dim_map *dim_map;
4331 struct isl_basic_map *res;
4332 enum isl_dim_type t;
4333 isl_size total;
4334 unsigned off;
4336 if (!bmap)
4337 return NULL;
4338 if (n == 0) {
4339 bmap = isl_basic_map_reset(bmap, src_type);
4340 bmap = isl_basic_map_reset(bmap, dst_type);
4341 return bmap;
4344 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4345 return isl_basic_map_free(bmap);
4347 if (dst_type == src_type && dst_pos == src_pos)
4348 return bmap;
4350 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4352 if (pos(bmap->dim, dst_type) + dst_pos ==
4353 pos(bmap->dim, src_type) + src_pos +
4354 ((src_type < dst_type) ? n : 0)) {
4355 space = isl_basic_map_take_space(bmap);
4356 space = isl_space_move_dims(space, dst_type, dst_pos,
4357 src_type, src_pos, n);
4358 bmap = isl_basic_map_restore_space(bmap, space);
4359 bmap = isl_basic_map_finalize(bmap);
4361 return bmap;
4364 total = isl_basic_map_dim(bmap, isl_dim_all);
4365 if (total < 0)
4366 return isl_basic_map_free(bmap);
4367 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4369 off = 0;
4370 space = isl_basic_map_peek_space(bmap);
4371 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4372 isl_size size = isl_space_dim(space, t);
4373 if (size < 0)
4374 dim_map = isl_dim_map_free(dim_map);
4375 if (t == dst_type) {
4376 isl_dim_map_dim_range(dim_map, space, t,
4377 0, dst_pos, off);
4378 off += dst_pos;
4379 isl_dim_map_dim_range(dim_map, space, src_type,
4380 src_pos, n, off);
4381 off += n;
4382 isl_dim_map_dim_range(dim_map, space, t,
4383 dst_pos, size - dst_pos, off);
4384 off += size - dst_pos;
4385 } else if (t == src_type) {
4386 isl_dim_map_dim_range(dim_map, space, t,
4387 0, src_pos, off);
4388 off += src_pos;
4389 isl_dim_map_dim_range(dim_map, space, t,
4390 src_pos + n, size - src_pos - n, off);
4391 off += size - src_pos - n;
4392 } else {
4393 isl_dim_map_dim(dim_map, space, t, off);
4394 off += size;
4397 isl_dim_map_div(dim_map, bmap, off);
4399 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4400 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4401 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4402 space = isl_basic_map_take_space(bmap);
4403 space = isl_space_move_dims(space, dst_type, dst_pos,
4404 src_type, src_pos, n);
4405 bmap = isl_basic_map_restore_space(bmap, space);
4406 if (!bmap)
4407 goto error;
4409 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4410 bmap = isl_basic_map_gauss(bmap, NULL);
4411 bmap = isl_basic_map_finalize(bmap);
4413 return bmap;
4414 error:
4415 isl_basic_map_free(bmap);
4416 return NULL;
4419 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4420 enum isl_dim_type dst_type, unsigned dst_pos,
4421 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4423 isl_basic_map *bmap = bset_to_bmap(bset);
4424 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4425 src_type, src_pos, n);
4426 return bset_from_bmap(bmap);
4429 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4430 enum isl_dim_type dst_type, unsigned dst_pos,
4431 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4433 if (!set)
4434 return NULL;
4435 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4436 return set_from_map(isl_map_move_dims(set_to_map(set),
4437 dst_type, dst_pos, src_type, src_pos, n));
4438 error:
4439 isl_set_free(set);
4440 return NULL;
4443 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4444 enum isl_dim_type dst_type, unsigned dst_pos,
4445 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4447 int i;
4448 isl_space *space;
4450 if (n == 0) {
4451 map = isl_map_reset(map, src_type);
4452 map = isl_map_reset(map, dst_type);
4453 return map;
4456 if (isl_map_check_range(map, src_type, src_pos, n))
4457 return isl_map_free(map);
4459 if (dst_type == src_type && dst_pos == src_pos)
4460 return map;
4462 isl_assert(map->ctx, dst_type != src_type, goto error);
4464 map = isl_map_cow(map);
4465 if (!map)
4466 return NULL;
4468 for (i = 0; i < map->n; ++i) {
4469 map->p[i] = isl_basic_map_move_dims(map->p[i],
4470 dst_type, dst_pos,
4471 src_type, src_pos, n);
4472 if (!map->p[i])
4473 goto error;
4476 space = isl_map_take_space(map);
4477 space = isl_space_move_dims(space, dst_type, dst_pos,
4478 src_type, src_pos, n);
4479 map = isl_map_restore_space(map, space);
4481 return map;
4482 error:
4483 isl_map_free(map);
4484 return NULL;
4487 /* Move the specified dimensions to the last columns right before
4488 * the divs. Don't change the dimension specification of bmap.
4489 * That's the responsibility of the caller.
4491 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4492 enum isl_dim_type type, unsigned first, unsigned n)
4494 isl_space *space;
4495 struct isl_dim_map *dim_map;
4496 struct isl_basic_map *res;
4497 enum isl_dim_type t;
4498 isl_size total;
4499 unsigned off;
4501 if (!bmap)
4502 return NULL;
4503 if (isl_basic_map_offset(bmap, type) + first + n ==
4504 isl_basic_map_offset(bmap, isl_dim_div))
4505 return bmap;
4507 total = isl_basic_map_dim(bmap, isl_dim_all);
4508 if (total < 0)
4509 return isl_basic_map_free(bmap);
4510 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4512 off = 0;
4513 space = isl_basic_map_peek_space(bmap);
4514 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4515 isl_size size = isl_space_dim(space, t);
4516 if (size < 0)
4517 dim_map = isl_dim_map_free(dim_map);
4518 if (t == type) {
4519 isl_dim_map_dim_range(dim_map, space, t,
4520 0, first, off);
4521 off += first;
4522 isl_dim_map_dim_range(dim_map, space, t,
4523 first, n, total - bmap->n_div - n);
4524 isl_dim_map_dim_range(dim_map, space, t,
4525 first + n, size - (first + n), off);
4526 off += size - (first + n);
4527 } else {
4528 isl_dim_map_dim(dim_map, space, t, off);
4529 off += size;
4532 isl_dim_map_div(dim_map, bmap, off + n);
4534 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4535 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4536 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4537 return res;
4540 /* Insert "n" rows in the divs of "bmap".
4542 * The number of columns is not changed, which means that the last
4543 * dimensions of "bmap" are being reintepreted as the new divs.
4544 * The space of "bmap" is not adjusted, however, which means
4545 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4546 * from the space of "bmap" is the responsibility of the caller.
4548 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4549 int n)
4551 int i;
4552 size_t row_size;
4553 isl_int **new_div;
4554 isl_int *old;
4556 bmap = isl_basic_map_cow(bmap);
4557 if (!bmap)
4558 return NULL;
4560 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4561 old = bmap->block2.data;
4562 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4563 (bmap->extra + n) * (1 + row_size));
4564 if (!bmap->block2.data)
4565 return isl_basic_map_free(bmap);
4566 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4567 if (!new_div)
4568 return isl_basic_map_free(bmap);
4569 for (i = 0; i < n; ++i) {
4570 new_div[i] = bmap->block2.data +
4571 (bmap->extra + i) * (1 + row_size);
4572 isl_seq_clr(new_div[i], 1 + row_size);
4574 for (i = 0; i < bmap->extra; ++i)
4575 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4576 free(bmap->div);
4577 bmap->div = new_div;
4578 bmap->n_div += n;
4579 bmap->extra += n;
4581 return bmap;
4584 /* Drop constraints from "bmap" that only involve the variables
4585 * of "type" in the range [first, first + n] that are not related
4586 * to any of the variables outside that interval.
4587 * These constraints cannot influence the values for the variables
4588 * outside the interval, except in case they cause "bmap" to be empty.
4589 * Only drop the constraints if "bmap" is known to be non-empty.
4591 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4592 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4593 unsigned first, unsigned n)
4595 int i;
4596 int *groups;
4597 isl_size dim, n_div;
4598 isl_bool non_empty;
4600 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4601 if (non_empty < 0)
4602 return isl_basic_map_free(bmap);
4603 if (!non_empty)
4604 return bmap;
4606 dim = isl_basic_map_dim(bmap, isl_dim_all);
4607 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4608 if (dim < 0 || n_div < 0)
4609 return isl_basic_map_free(bmap);
4610 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4611 if (!groups)
4612 return isl_basic_map_free(bmap);
4613 first += isl_basic_map_offset(bmap, type) - 1;
4614 for (i = 0; i < first; ++i)
4615 groups[i] = -1;
4616 for (i = first + n; i < dim - n_div; ++i)
4617 groups[i] = -1;
4619 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4621 return bmap;
4624 /* Turn the n dimensions of type type, starting at first
4625 * into existentially quantified variables.
4627 * If a subset of the projected out variables are unrelated
4628 * to any of the variables that remain, then the constraints
4629 * involving this subset are simply dropped first.
4631 __isl_give isl_basic_map *isl_basic_map_project_out(
4632 __isl_take isl_basic_map *bmap,
4633 enum isl_dim_type type, unsigned first, unsigned n)
4635 isl_bool empty;
4636 isl_space *space;
4638 if (n == 0)
4639 return basic_map_space_reset(bmap, type);
4640 if (type == isl_dim_div)
4641 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4642 "cannot project out existentially quantified variables",
4643 return isl_basic_map_free(bmap));
4645 empty = isl_basic_map_plain_is_empty(bmap);
4646 if (empty < 0)
4647 return isl_basic_map_free(bmap);
4648 if (empty)
4649 bmap = isl_basic_map_set_to_empty(bmap);
4651 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4652 if (!bmap)
4653 return NULL;
4655 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4656 return isl_basic_map_remove_dims(bmap, type, first, n);
4658 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4659 return isl_basic_map_free(bmap);
4661 bmap = move_last(bmap, type, first, n);
4662 bmap = isl_basic_map_cow(bmap);
4663 bmap = insert_div_rows(bmap, n);
4665 space = isl_basic_map_take_space(bmap);
4666 space = isl_space_drop_dims(space, type, first, n);
4667 bmap = isl_basic_map_restore_space(bmap, space);
4668 bmap = isl_basic_map_simplify(bmap);
4669 bmap = isl_basic_map_drop_redundant_divs(bmap);
4670 return isl_basic_map_finalize(bmap);
4673 /* Turn the n dimensions of type type, starting at first
4674 * into existentially quantified variables.
4676 __isl_give isl_basic_set *isl_basic_set_project_out(
4677 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4678 unsigned first, unsigned n)
4680 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4681 type, first, n));
4684 /* Turn the n dimensions of type type, starting at first
4685 * into existentially quantified variables.
4687 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4688 enum isl_dim_type type, unsigned first, unsigned n)
4690 int i;
4691 isl_space *space;
4693 if (n == 0)
4694 return map_space_reset(map, type);
4696 if (isl_map_check_range(map, type, first, n) < 0)
4697 return isl_map_free(map);
4699 map = isl_map_cow(map);
4700 if (!map)
4701 return NULL;
4703 for (i = 0; i < map->n; ++i) {
4704 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4705 if (!map->p[i])
4706 goto error;
4709 if (map->n > 1)
4710 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4711 map = isl_map_unmark_normalized(map);
4713 space = isl_map_take_space(map);
4714 space = isl_space_drop_dims(space, type, first, n);
4715 map = isl_map_restore_space(map, space);
4717 return map;
4718 error:
4719 isl_map_free(map);
4720 return NULL;
4723 #undef TYPE
4724 #define TYPE isl_map
4725 #include "isl_project_out_all_params_templ.c"
4726 #include "isl_project_out_param_templ.c"
4728 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4729 * into existentially quantified variables.
4731 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4732 enum isl_dim_type type, unsigned first, unsigned n)
4734 isl_size dim;
4736 dim = isl_map_dim(map, type);
4737 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4738 return isl_map_free(map);
4739 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4740 map = isl_map_project_out(map, type, 0, first);
4741 return map;
4744 /* Turn the n dimensions of type type, starting at first
4745 * into existentially quantified variables.
4747 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4748 enum isl_dim_type type, unsigned first, unsigned n)
4750 return set_from_map(isl_map_project_out(set_to_map(set),
4751 type, first, n));
4754 /* If "set" involves a parameter with identifier "id",
4755 * then turn it into an existentially quantified variable.
4757 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4758 __isl_take isl_id *id)
4760 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4763 /* If "set" involves any of the parameters with identifiers in "list",
4764 * then turn them into existentially quantified variables.
4766 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4767 __isl_take isl_id_list *list)
4769 isl_map *map;
4771 map = set_to_map(set);
4772 map = isl_map_project_out_param_id_list(map, list);
4773 return set_from_map(map);
4776 /* Project out all parameters from "set" by existentially quantifying
4777 * over them.
4779 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4781 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4784 /* Return a map that projects the elements in "set" onto their
4785 * "n" set dimensions starting at "first".
4786 * "type" should be equal to isl_dim_set.
4788 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4789 enum isl_dim_type type, unsigned first, unsigned n)
4791 int i;
4792 isl_map *map;
4794 if (type != isl_dim_set)
4795 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4796 "only set dimensions can be projected out", goto error);
4797 if (isl_set_check_range(set, type, first, n) < 0)
4798 return isl_set_free(set);
4800 map = isl_map_from_domain(set);
4801 map = isl_map_add_dims(map, isl_dim_out, n);
4802 for (i = 0; i < n; ++i)
4803 map = isl_map_equate(map, isl_dim_in, first + i,
4804 isl_dim_out, i);
4805 return map;
4806 error:
4807 isl_set_free(set);
4808 return NULL;
4811 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4812 unsigned n)
4814 int i, j;
4815 isl_size total;
4817 total = isl_basic_map_dim(bmap, isl_dim_all);
4818 if (total < 0)
4819 return isl_basic_map_free(bmap);
4820 for (i = 0; i < n; ++i) {
4821 j = isl_basic_map_alloc_div(bmap);
4822 if (j < 0)
4823 goto error;
4824 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4826 return bmap;
4827 error:
4828 isl_basic_map_free(bmap);
4829 return NULL;
4832 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4834 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4835 __isl_keep isl_basic_map *bmap2)
4837 isl_space *space1, *space2;
4839 space1 = isl_basic_map_peek_space(bmap1);
4840 space2 = isl_basic_map_peek_space(bmap2);
4841 return isl_space_tuple_is_equal(space1, isl_dim_out,
4842 space2, isl_dim_in);
4845 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4847 static isl_stat isl_basic_map_check_applies_range(
4848 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4850 isl_bool equal;
4852 equal = isl_basic_map_applies_range(bmap1, bmap2);
4853 if (equal < 0)
4854 return isl_stat_error;
4855 if (!equal)
4856 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4857 "spaces don't match", return isl_stat_error);
4858 return isl_stat_ok;
4861 __isl_give isl_basic_map *isl_basic_map_apply_range(
4862 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4864 isl_space *space_result = NULL;
4865 struct isl_basic_map *bmap;
4866 isl_size n_in, n_out, n, nparam;
4867 unsigned total, pos;
4868 struct isl_dim_map *dim_map1, *dim_map2;
4870 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4871 goto error;
4872 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4873 goto error;
4875 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4876 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4877 n = isl_basic_map_dim(bmap1, isl_dim_out);
4878 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4879 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4880 goto error;
4882 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4883 isl_basic_map_get_space(bmap2));
4885 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4886 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4887 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4888 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4889 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4890 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4891 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4892 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4893 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4894 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4895 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4897 bmap = isl_basic_map_alloc_space(space_result,
4898 bmap1->n_div + bmap2->n_div + n,
4899 bmap1->n_eq + bmap2->n_eq,
4900 bmap1->n_ineq + bmap2->n_ineq);
4901 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4902 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4903 bmap = add_divs(bmap, n);
4904 bmap = isl_basic_map_simplify(bmap);
4905 bmap = isl_basic_map_drop_redundant_divs(bmap);
4906 return isl_basic_map_finalize(bmap);
4907 error:
4908 isl_basic_map_free(bmap1);
4909 isl_basic_map_free(bmap2);
4910 return NULL;
4913 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4914 __isl_take isl_basic_map *bmap)
4916 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4917 goto error;
4919 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4920 bmap));
4921 error:
4922 isl_basic_set_free(bset);
4923 isl_basic_map_free(bmap);
4924 return NULL;
4927 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4928 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4930 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4931 goto error;
4932 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4933 bmap2->dim, isl_dim_in))
4934 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4935 "spaces don't match", goto error);
4937 bmap1 = isl_basic_map_reverse(bmap1);
4938 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4939 return isl_basic_map_reverse(bmap1);
4940 error:
4941 isl_basic_map_free(bmap1);
4942 isl_basic_map_free(bmap2);
4943 return NULL;
4946 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4947 * A \cap B -> f(A) + f(B)
4949 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4950 __isl_take isl_basic_map *bmap2)
4952 isl_size n_in, n_out, nparam;
4953 unsigned total, pos;
4954 struct isl_basic_map *bmap = NULL;
4955 struct isl_dim_map *dim_map1, *dim_map2;
4956 int i;
4958 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4959 goto error;
4961 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4962 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4963 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4964 if (nparam < 0 || n_in < 0 || n_out < 0)
4965 goto error;
4967 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4968 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4969 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4970 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4971 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4972 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4973 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4974 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4975 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4976 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4977 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4979 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4980 bmap1->n_div + bmap2->n_div + 2 * n_out,
4981 bmap1->n_eq + bmap2->n_eq + n_out,
4982 bmap1->n_ineq + bmap2->n_ineq);
4983 for (i = 0; i < n_out; ++i) {
4984 int j = isl_basic_map_alloc_equality(bmap);
4985 if (j < 0)
4986 goto error;
4987 isl_seq_clr(bmap->eq[j], 1+total);
4988 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4989 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4990 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4992 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4993 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4994 bmap = add_divs(bmap, 2 * n_out);
4996 bmap = isl_basic_map_simplify(bmap);
4997 return isl_basic_map_finalize(bmap);
4998 error:
4999 isl_basic_map_free(bmap);
5000 isl_basic_map_free(bmap1);
5001 isl_basic_map_free(bmap2);
5002 return NULL;
5005 /* Given two maps A -> f(A) and B -> g(B), construct a map
5006 * A \cap B -> f(A) + f(B)
5008 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
5009 __isl_take isl_map *map2)
5011 struct isl_map *result;
5012 int i, j;
5014 if (isl_map_check_equal_space(map1, map2) < 0)
5015 goto error;
5017 result = isl_map_alloc_space(isl_space_copy(map1->dim),
5018 map1->n * map2->n, 0);
5019 if (!result)
5020 goto error;
5021 for (i = 0; i < map1->n; ++i)
5022 for (j = 0; j < map2->n; ++j) {
5023 struct isl_basic_map *part;
5024 part = isl_basic_map_sum(
5025 isl_basic_map_copy(map1->p[i]),
5026 isl_basic_map_copy(map2->p[j]));
5027 if (isl_basic_map_is_empty(part))
5028 isl_basic_map_free(part);
5029 else
5030 result = isl_map_add_basic_map(result, part);
5031 if (!result)
5032 goto error;
5034 isl_map_free(map1);
5035 isl_map_free(map2);
5036 return result;
5037 error:
5038 isl_map_free(map1);
5039 isl_map_free(map2);
5040 return NULL;
5043 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
5044 __isl_take isl_set *set2)
5046 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5049 /* Given a basic map A -> f(A), construct A -> -f(A).
5051 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5053 int i, j;
5054 unsigned off;
5055 isl_size n;
5057 bmap = isl_basic_map_cow(bmap);
5058 n = isl_basic_map_dim(bmap, isl_dim_out);
5059 if (n < 0)
5060 return isl_basic_map_free(bmap);
5062 off = isl_basic_map_offset(bmap, isl_dim_out);
5063 for (i = 0; i < bmap->n_eq; ++i)
5064 for (j = 0; j < n; ++j)
5065 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5066 for (i = 0; i < bmap->n_ineq; ++i)
5067 for (j = 0; j < n; ++j)
5068 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5069 for (i = 0; i < bmap->n_div; ++i)
5070 for (j = 0; j < n; ++j)
5071 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5072 bmap = isl_basic_map_gauss(bmap, NULL);
5073 return isl_basic_map_finalize(bmap);
5076 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5078 return isl_basic_map_neg(bset);
5081 /* Given a map A -> f(A), construct A -> -f(A).
5083 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5085 int i;
5087 map = isl_map_cow(map);
5088 if (!map)
5089 return NULL;
5091 for (i = 0; i < map->n; ++i) {
5092 map->p[i] = isl_basic_map_neg(map->p[i]);
5093 if (!map->p[i])
5094 goto error;
5097 return map;
5098 error:
5099 isl_map_free(map);
5100 return NULL;
5103 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5105 return set_from_map(isl_map_neg(set_to_map(set)));
5108 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5109 * A -> floor(f(A)/d).
5111 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5112 isl_int d)
5114 isl_size n_in, n_out, nparam;
5115 unsigned total, pos;
5116 struct isl_basic_map *result = NULL;
5117 struct isl_dim_map *dim_map;
5118 int i;
5120 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5121 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5122 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5123 if (nparam < 0 || n_in < 0 || n_out < 0)
5124 return isl_basic_map_free(bmap);
5126 total = nparam + n_in + n_out + bmap->n_div + n_out;
5127 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5128 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5129 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5130 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5131 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5133 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5134 bmap->n_div + n_out,
5135 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5136 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5137 result = add_divs(result, n_out);
5138 for (i = 0; i < n_out; ++i) {
5139 int j;
5140 j = isl_basic_map_alloc_inequality(result);
5141 if (j < 0)
5142 goto error;
5143 isl_seq_clr(result->ineq[j], 1+total);
5144 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5145 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5146 j = isl_basic_map_alloc_inequality(result);
5147 if (j < 0)
5148 goto error;
5149 isl_seq_clr(result->ineq[j], 1+total);
5150 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5151 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5152 isl_int_sub_ui(result->ineq[j][0], d, 1);
5155 result = isl_basic_map_simplify(result);
5156 return isl_basic_map_finalize(result);
5157 error:
5158 isl_basic_map_free(result);
5159 return NULL;
5162 /* Given a map A -> f(A) and an integer d, construct a map
5163 * A -> floor(f(A)/d).
5165 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5167 int i;
5169 map = isl_map_cow(map);
5170 if (!map)
5171 return NULL;
5173 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5174 for (i = 0; i < map->n; ++i) {
5175 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5176 if (!map->p[i])
5177 goto error;
5179 map = isl_map_unmark_normalized(map);
5181 return map;
5182 error:
5183 isl_map_free(map);
5184 return NULL;
5187 /* Given a map A -> f(A) and an integer d, construct a map
5188 * A -> floor(f(A)/d).
5190 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5191 __isl_take isl_val *d)
5193 if (!map || !d)
5194 goto error;
5195 if (!isl_val_is_int(d))
5196 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5197 "expecting integer denominator", goto error);
5198 map = isl_map_floordiv(map, d->n);
5199 isl_val_free(d);
5200 return map;
5201 error:
5202 isl_map_free(map);
5203 isl_val_free(d);
5204 return NULL;
5207 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5208 unsigned pos)
5210 int i;
5211 isl_size nparam;
5212 isl_size n_in;
5213 isl_size total;
5215 total = isl_basic_map_dim(bmap, isl_dim_all);
5216 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5217 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5218 if (total < 0 || nparam < 0 || n_in < 0)
5219 return isl_basic_map_free(bmap);
5220 i = isl_basic_map_alloc_equality(bmap);
5221 if (i < 0)
5222 goto error;
5223 isl_seq_clr(bmap->eq[i], 1 + total);
5224 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5225 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5226 return isl_basic_map_finalize(bmap);
5227 error:
5228 isl_basic_map_free(bmap);
5229 return NULL;
5232 /* Add a constraint to "bmap" expressing i_pos < o_pos
5234 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5235 unsigned pos)
5237 int i;
5238 isl_size nparam;
5239 isl_size n_in;
5240 isl_size total;
5242 total = isl_basic_map_dim(bmap, isl_dim_all);
5243 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5244 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5245 if (total < 0 || nparam < 0 || n_in < 0)
5246 return isl_basic_map_free(bmap);
5247 i = isl_basic_map_alloc_inequality(bmap);
5248 if (i < 0)
5249 goto error;
5250 isl_seq_clr(bmap->ineq[i], 1 + total);
5251 isl_int_set_si(bmap->ineq[i][0], -1);
5252 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5253 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5254 return isl_basic_map_finalize(bmap);
5255 error:
5256 isl_basic_map_free(bmap);
5257 return NULL;
5260 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5262 static __isl_give isl_basic_map *var_less_or_equal(
5263 __isl_take isl_basic_map *bmap, unsigned pos)
5265 int i;
5266 isl_size nparam;
5267 isl_size n_in;
5268 isl_size total;
5270 total = isl_basic_map_dim(bmap, isl_dim_all);
5271 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5272 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5273 if (total < 0 || nparam < 0 || n_in < 0)
5274 return isl_basic_map_free(bmap);
5275 i = isl_basic_map_alloc_inequality(bmap);
5276 if (i < 0)
5277 goto error;
5278 isl_seq_clr(bmap->ineq[i], 1 + total);
5279 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5280 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5281 return isl_basic_map_finalize(bmap);
5282 error:
5283 isl_basic_map_free(bmap);
5284 return NULL;
5287 /* Add a constraint to "bmap" expressing i_pos > o_pos
5289 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5290 unsigned pos)
5292 int i;
5293 isl_size nparam;
5294 isl_size n_in;
5295 isl_size total;
5297 total = isl_basic_map_dim(bmap, isl_dim_all);
5298 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5299 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5300 if (total < 0 || nparam < 0 || n_in < 0)
5301 return isl_basic_map_free(bmap);
5302 i = isl_basic_map_alloc_inequality(bmap);
5303 if (i < 0)
5304 goto error;
5305 isl_seq_clr(bmap->ineq[i], 1 + total);
5306 isl_int_set_si(bmap->ineq[i][0], -1);
5307 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5308 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5309 return isl_basic_map_finalize(bmap);
5310 error:
5311 isl_basic_map_free(bmap);
5312 return NULL;
5315 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5317 static __isl_give isl_basic_map *var_more_or_equal(
5318 __isl_take isl_basic_map *bmap, unsigned pos)
5320 int i;
5321 isl_size nparam;
5322 isl_size n_in;
5323 isl_size total;
5325 total = isl_basic_map_dim(bmap, isl_dim_all);
5326 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5327 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5328 if (total < 0 || nparam < 0 || n_in < 0)
5329 return isl_basic_map_free(bmap);
5330 i = isl_basic_map_alloc_inequality(bmap);
5331 if (i < 0)
5332 goto error;
5333 isl_seq_clr(bmap->ineq[i], 1 + total);
5334 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5335 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5336 return isl_basic_map_finalize(bmap);
5337 error:
5338 isl_basic_map_free(bmap);
5339 return NULL;
5342 __isl_give isl_basic_map *isl_basic_map_equal(
5343 __isl_take isl_space *space, unsigned n_equal)
5345 int i;
5346 struct isl_basic_map *bmap;
5347 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5348 if (!bmap)
5349 return NULL;
5350 for (i = 0; i < n_equal && bmap; ++i)
5351 bmap = var_equal(bmap, i);
5352 return isl_basic_map_finalize(bmap);
5355 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5357 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5358 unsigned pos)
5360 int i;
5361 struct isl_basic_map *bmap;
5362 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5363 if (!bmap)
5364 return NULL;
5365 for (i = 0; i < pos && bmap; ++i)
5366 bmap = var_equal(bmap, i);
5367 if (bmap)
5368 bmap = var_less(bmap, pos);
5369 return isl_basic_map_finalize(bmap);
5372 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5374 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5375 __isl_take isl_space *space, unsigned pos)
5377 int i;
5378 isl_basic_map *bmap;
5380 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5381 for (i = 0; i < pos; ++i)
5382 bmap = var_equal(bmap, i);
5383 bmap = var_less_or_equal(bmap, pos);
5384 return isl_basic_map_finalize(bmap);
5387 /* Return a relation on "space" expressing i_pos > o_pos
5389 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5390 unsigned pos)
5392 int i;
5393 struct isl_basic_map *bmap;
5394 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5395 if (!bmap)
5396 return NULL;
5397 for (i = 0; i < pos && bmap; ++i)
5398 bmap = var_equal(bmap, i);
5399 if (bmap)
5400 bmap = var_more(bmap, pos);
5401 return isl_basic_map_finalize(bmap);
5404 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5406 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5407 __isl_take isl_space *space, unsigned pos)
5409 int i;
5410 isl_basic_map *bmap;
5412 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5413 for (i = 0; i < pos; ++i)
5414 bmap = var_equal(bmap, i);
5415 bmap = var_more_or_equal(bmap, pos);
5416 return isl_basic_map_finalize(bmap);
5419 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5420 unsigned n, int equal)
5422 struct isl_map *map;
5423 int i;
5425 if (n == 0 && equal)
5426 return isl_map_universe(space);
5428 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5430 for (i = 0; i + 1 < n; ++i)
5431 map = isl_map_add_basic_map(map,
5432 isl_basic_map_less_at(isl_space_copy(space), i));
5433 if (n > 0) {
5434 if (equal)
5435 map = isl_map_add_basic_map(map,
5436 isl_basic_map_less_or_equal_at(space, n - 1));
5437 else
5438 map = isl_map_add_basic_map(map,
5439 isl_basic_map_less_at(space, n - 1));
5440 } else
5441 isl_space_free(space);
5443 return map;
5446 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5448 if (!space)
5449 return NULL;
5450 return map_lex_lte_first(space, space->n_out, equal);
5453 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5454 unsigned n)
5456 return map_lex_lte_first(space, n, 0);
5459 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5460 unsigned n)
5462 return map_lex_lte_first(space, n, 1);
5465 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5467 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5470 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5472 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5475 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5476 unsigned n, int equal)
5478 struct isl_map *map;
5479 int i;
5481 if (n == 0 && equal)
5482 return isl_map_universe(space);
5484 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5486 for (i = 0; i + 1 < n; ++i)
5487 map = isl_map_add_basic_map(map,
5488 isl_basic_map_more_at(isl_space_copy(space), i));
5489 if (n > 0) {
5490 if (equal)
5491 map = isl_map_add_basic_map(map,
5492 isl_basic_map_more_or_equal_at(space, n - 1));
5493 else
5494 map = isl_map_add_basic_map(map,
5495 isl_basic_map_more_at(space, n - 1));
5496 } else
5497 isl_space_free(space);
5499 return map;
5502 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5504 if (!space)
5505 return NULL;
5506 return map_lex_gte_first(space, space->n_out, equal);
5509 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5510 unsigned n)
5512 return map_lex_gte_first(space, n, 0);
5515 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5516 unsigned n)
5518 return map_lex_gte_first(space, n, 1);
5521 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5523 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5526 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5528 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5531 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5532 __isl_take isl_set *set2)
5534 isl_map *map;
5535 map = isl_map_lex_le(isl_set_get_space(set1));
5536 map = isl_map_intersect_domain(map, set1);
5537 map = isl_map_intersect_range(map, set2);
5538 return map;
5541 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5542 __isl_take isl_set *set2)
5544 isl_map *map;
5545 map = isl_map_lex_lt(isl_set_get_space(set1));
5546 map = isl_map_intersect_domain(map, set1);
5547 map = isl_map_intersect_range(map, set2);
5548 return map;
5551 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5552 __isl_take isl_set *set2)
5554 isl_map *map;
5555 map = isl_map_lex_ge(isl_set_get_space(set1));
5556 map = isl_map_intersect_domain(map, set1);
5557 map = isl_map_intersect_range(map, set2);
5558 return map;
5561 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5562 __isl_take isl_set *set2)
5564 isl_map *map;
5565 map = isl_map_lex_gt(isl_set_get_space(set1));
5566 map = isl_map_intersect_domain(map, set1);
5567 map = isl_map_intersect_range(map, set2);
5568 return map;
5571 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5572 __isl_take isl_map *map2)
5574 isl_map *map;
5575 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5576 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5577 map = isl_map_apply_range(map, isl_map_reverse(map2));
5578 return map;
5581 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5582 __isl_take isl_map *map2)
5584 isl_map *map;
5585 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5586 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5587 map = isl_map_apply_range(map, isl_map_reverse(map2));
5588 return map;
5591 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5592 __isl_take isl_map *map2)
5594 isl_map *map;
5595 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5596 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5597 map = isl_map_apply_range(map, isl_map_reverse(map2));
5598 return map;
5601 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5602 __isl_take isl_map *map2)
5604 isl_map *map;
5605 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5606 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5607 map = isl_map_apply_range(map, isl_map_reverse(map2));
5608 return map;
5611 /* For the div d = floor(f/m) at position "div", add the constraint
5613 * f - m d >= 0
5615 static __isl_give isl_basic_map *add_upper_div_constraint(
5616 __isl_take isl_basic_map *bmap, unsigned div)
5618 int i;
5619 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5620 isl_size n_div;
5621 unsigned pos;
5623 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5624 if (v_div < 0 || n_div < 0)
5625 return isl_basic_map_free(bmap);
5626 pos = v_div + div;
5627 i = isl_basic_map_alloc_inequality(bmap);
5628 if (i < 0)
5629 return isl_basic_map_free(bmap);
5630 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5631 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5633 return bmap;
5636 /* For the div d = floor(f/m) at position "div", add the constraint
5638 * -(f-(m-1)) + m d >= 0
5640 static __isl_give isl_basic_map *add_lower_div_constraint(
5641 __isl_take isl_basic_map *bmap, unsigned div)
5643 int i;
5644 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5645 isl_size n_div;
5646 unsigned pos;
5648 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5649 if (v_div < 0 || n_div < 0)
5650 return isl_basic_map_free(bmap);
5651 pos = v_div + div;
5652 i = isl_basic_map_alloc_inequality(bmap);
5653 if (i < 0)
5654 return isl_basic_map_free(bmap);
5655 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5656 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5657 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5658 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5660 return bmap;
5663 /* For the div d = floor(f/m) at position "pos", add the constraints
5665 * f - m d >= 0
5666 * -(f-(m-1)) + m d >= 0
5668 * Note that the second constraint is the negation of
5670 * f - m d >= m
5672 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5673 __isl_take isl_basic_map *bmap, unsigned pos)
5675 bmap = add_upper_div_constraint(bmap, pos);
5676 bmap = add_lower_div_constraint(bmap, pos);
5677 return bmap;
5680 /* For each known div d = floor(f/m), add the constraints
5682 * f - m d >= 0
5683 * -(f-(m-1)) + m d >= 0
5685 * Remove duplicate constraints in case of some these div constraints
5686 * already appear in "bmap".
5688 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5689 __isl_take isl_basic_map *bmap)
5691 isl_size n_div;
5693 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5694 if (n_div < 0)
5695 return isl_basic_map_free(bmap);
5696 if (n_div == 0)
5697 return bmap;
5699 bmap = add_known_div_constraints(bmap);
5700 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5701 bmap = isl_basic_map_finalize(bmap);
5702 return bmap;
5705 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5707 * In particular, if this div is of the form d = floor(f/m),
5708 * then add the constraint
5710 * f - m d >= 0
5712 * if sign < 0 or the constraint
5714 * -(f-(m-1)) + m d >= 0
5716 * if sign > 0.
5718 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5719 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5721 if (sign < 0)
5722 return add_upper_div_constraint(bmap, div);
5723 else
5724 return add_lower_div_constraint(bmap, div);
5727 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5728 __isl_take isl_basic_map *bmap)
5730 isl_space *space;
5732 if (!bmap)
5733 goto error;
5734 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5735 bmap->n_div == 0 &&
5736 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5737 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5738 return bset_from_bmap(bmap);
5739 bmap = isl_basic_map_cow(bmap);
5740 if (!bmap)
5741 return NULL;
5742 space = isl_basic_map_take_space(bmap);
5743 space = isl_space_underlying(space, bmap->n_div);
5744 bmap = isl_basic_map_restore_space(bmap, space);
5745 if (!bmap)
5746 return NULL;
5747 bmap->extra -= bmap->n_div;
5748 bmap->n_div = 0;
5749 bmap = isl_basic_map_finalize(bmap);
5750 return bset_from_bmap(bmap);
5751 error:
5752 isl_basic_map_free(bmap);
5753 return NULL;
5756 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5757 __isl_take isl_basic_set *bset)
5759 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5762 /* Replace each element in "list" by the result of applying
5763 * isl_basic_map_underlying_set to the element.
5765 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5766 __isl_take isl_basic_map_list *list)
5768 int i;
5769 isl_size n;
5771 n = isl_basic_map_list_n_basic_map(list);
5772 if (n < 0)
5773 goto error;
5775 for (i = 0; i < n; ++i) {
5776 isl_basic_map *bmap;
5777 isl_basic_set *bset;
5779 bmap = isl_basic_map_list_get_basic_map(list, i);
5780 bset = isl_basic_set_underlying_set(bmap);
5781 list = isl_basic_set_list_set_basic_set(list, i, bset);
5784 return list;
5785 error:
5786 isl_basic_map_list_free(list);
5787 return NULL;
5790 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5791 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5793 struct isl_basic_map *bmap;
5794 struct isl_ctx *ctx;
5795 isl_size dim, bmap_total;
5796 unsigned total;
5797 int i;
5799 if (!bset || !like)
5800 goto error;
5801 ctx = bset->ctx;
5802 if (isl_basic_set_check_no_params(bset) < 0 ||
5803 isl_basic_set_check_no_locals(bset) < 0)
5804 goto error;
5805 dim = isl_basic_set_dim(bset, isl_dim_set);
5806 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5807 if (dim < 0 || bmap_total < 0)
5808 goto error;
5809 isl_assert(ctx, dim == bmap_total, goto error);
5810 if (like->n_div == 0) {
5811 isl_space *space = isl_basic_map_get_space(like);
5812 isl_basic_map_free(like);
5813 return isl_basic_map_reset_space(bset, space);
5815 bset = isl_basic_set_cow(bset);
5816 if (!bset)
5817 goto error;
5818 total = dim + bset->extra;
5819 bmap = bset_to_bmap(bset);
5820 isl_space_free(isl_basic_map_take_space(bmap));
5821 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5822 if (!bmap)
5823 goto error;
5824 bmap->n_div = like->n_div;
5825 bmap->extra += like->n_div;
5826 if (bmap->extra) {
5827 unsigned ltotal;
5828 isl_int **div;
5829 ltotal = total - bmap->extra + like->extra;
5830 if (ltotal > total)
5831 ltotal = total;
5832 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5833 bmap->extra * (1 + 1 + total));
5834 if (isl_blk_is_error(bmap->block2))
5835 goto error;
5836 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5837 if (!div)
5838 goto error;
5839 bmap->div = div;
5840 for (i = 0; i < bmap->extra; ++i)
5841 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5842 for (i = 0; i < like->n_div; ++i) {
5843 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5844 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5846 bmap = isl_basic_map_add_known_div_constraints(bmap);
5848 isl_basic_map_free(like);
5849 bmap = isl_basic_map_simplify(bmap);
5850 bmap = isl_basic_map_finalize(bmap);
5851 return bmap;
5852 error:
5853 isl_basic_map_free(like);
5854 isl_basic_set_free(bset);
5855 return NULL;
5858 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5859 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5861 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5862 bset_to_bmap(like)));
5865 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5867 int i;
5869 map = isl_map_cow(map);
5870 if (!map)
5871 return NULL;
5872 map->dim = isl_space_cow(map->dim);
5873 if (!map->dim)
5874 goto error;
5876 for (i = 1; i < map->n; ++i)
5877 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5878 goto error);
5879 for (i = 0; i < map->n; ++i) {
5880 map->p[i] = bset_to_bmap(
5881 isl_basic_map_underlying_set(map->p[i]));
5882 if (!map->p[i])
5883 goto error;
5885 if (map->n == 0)
5886 map->dim = isl_space_underlying(map->dim, 0);
5887 else {
5888 isl_space_free(map->dim);
5889 map->dim = isl_space_copy(map->p[0]->dim);
5891 if (!map->dim)
5892 goto error;
5893 return set_from_map(map);
5894 error:
5895 isl_map_free(map);
5896 return NULL;
5899 /* Replace the space of "bmap" by "space".
5901 * If the space of "bmap" is identical to "space" (including the identifiers
5902 * of the input and output dimensions), then simply return the original input.
5904 __isl_give isl_basic_map *isl_basic_map_reset_space(
5905 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5907 isl_bool equal;
5908 isl_space *bmap_space;
5910 bmap_space = isl_basic_map_peek_space(bmap);
5911 equal = isl_space_is_equal(bmap_space, space);
5912 if (equal >= 0 && equal)
5913 equal = isl_space_has_equal_ids(bmap_space, space);
5914 if (equal < 0)
5915 goto error;
5916 if (equal) {
5917 isl_space_free(space);
5918 return bmap;
5920 isl_space_free(isl_basic_map_take_space(bmap));
5921 bmap = isl_basic_map_restore_space(bmap, space);
5923 bmap = isl_basic_map_finalize(bmap);
5925 return bmap;
5926 error:
5927 isl_basic_map_free(bmap);
5928 isl_space_free(space);
5929 return NULL;
5932 __isl_give isl_basic_set *isl_basic_set_reset_space(
5933 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5935 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5936 space));
5939 /* Check that the total dimensions of "map" and "space" are the same.
5941 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5942 __isl_keep isl_space *space)
5944 isl_size dim1, dim2;
5946 dim1 = isl_map_dim(map, isl_dim_all);
5947 dim2 = isl_space_dim(space, isl_dim_all);
5948 if (dim1 < 0 || dim2 < 0)
5949 return isl_stat_error;
5950 if (dim1 == dim2)
5951 return isl_stat_ok;
5952 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5953 "total dimensions do not match", return isl_stat_error);
5956 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5957 __isl_take isl_space *space)
5959 int i;
5961 map = isl_map_cow(map);
5962 if (!map || !space)
5963 goto error;
5965 for (i = 0; i < map->n; ++i) {
5966 map->p[i] = isl_basic_map_reset_space(map->p[i],
5967 isl_space_copy(space));
5968 if (!map->p[i])
5969 goto error;
5971 isl_space_free(isl_map_take_space(map));
5972 map = isl_map_restore_space(map, space);
5974 return map;
5975 error:
5976 isl_map_free(map);
5977 isl_space_free(space);
5978 return NULL;
5981 /* Replace the space of "map" by "space", without modifying
5982 * the dimension of "map".
5984 * If the space of "map" is identical to "space" (including the identifiers
5985 * of the input and output dimensions), then simply return the original input.
5987 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5988 __isl_take isl_space *space)
5990 isl_bool equal;
5991 isl_space *map_space;
5993 map_space = isl_map_peek_space(map);
5994 equal = isl_space_is_equal(map_space, space);
5995 if (equal >= 0 && equal)
5996 equal = isl_space_has_equal_ids(map_space, space);
5997 if (equal < 0)
5998 goto error;
5999 if (equal) {
6000 isl_space_free(space);
6001 return map;
6003 if (check_map_space_equal_total_dim(map, space) < 0)
6004 goto error;
6005 return isl_map_reset_space(map, space);
6006 error:
6007 isl_map_free(map);
6008 isl_space_free(space);
6009 return NULL;
6012 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
6013 __isl_take isl_space *space)
6015 return set_from_map(isl_map_reset_space(set_to_map(set), space));
6018 /* Compute the parameter domain of the given basic set.
6020 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
6022 isl_bool is_params;
6023 isl_space *space;
6024 isl_size n;
6026 is_params = isl_basic_set_is_params(bset);
6027 if (is_params < 0)
6028 return isl_basic_set_free(bset);
6029 if (is_params)
6030 return bset;
6032 n = isl_basic_set_dim(bset, isl_dim_set);
6033 if (n < 0)
6034 return isl_basic_set_free(bset);
6035 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6036 space = isl_basic_set_get_space(bset);
6037 space = isl_space_params(space);
6038 bset = isl_basic_set_reset_space(bset, space);
6039 return bset;
6042 /* Construct a zero-dimensional basic set with the given parameter domain.
6044 __isl_give isl_basic_set *isl_basic_set_from_params(
6045 __isl_take isl_basic_set *bset)
6047 isl_space *space;
6048 space = isl_basic_set_get_space(bset);
6049 space = isl_space_set_from_params(space);
6050 bset = isl_basic_set_reset_space(bset, space);
6051 return bset;
6054 /* Compute the parameter domain of the given set.
6056 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6058 return isl_map_params(set_to_map(set));
6061 /* Construct a zero-dimensional set with the given parameter domain.
6063 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6065 isl_space *space;
6066 space = isl_set_get_space(set);
6067 space = isl_space_set_from_params(space);
6068 set = isl_set_reset_space(set, space);
6069 return set;
6072 /* Compute the parameter domain of the given map.
6074 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6076 isl_space *space;
6077 isl_size n_in, n_out;
6079 n_in = isl_map_dim(map, isl_dim_in);
6080 n_out = isl_map_dim(map, isl_dim_out);
6081 if (n_in < 0 || n_out < 0)
6082 return isl_map_free(map);
6083 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6084 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6085 space = isl_map_get_space(map);
6086 space = isl_space_params(space);
6087 map = isl_map_reset_space(map, space);
6088 return map;
6091 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6093 isl_space *space;
6094 isl_size n_out;
6096 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6097 if (n_out < 0)
6098 return isl_basic_map_free(bmap);
6099 space = isl_space_domain(isl_basic_map_get_space(bmap));
6101 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6103 return isl_basic_map_reset_space(bmap, space);
6106 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6108 if (!bmap)
6109 return isl_bool_error;
6110 return isl_space_may_be_set(bmap->dim);
6113 /* Is this basic map actually a set?
6114 * Users should never call this function. Outside of isl,
6115 * the type should indicate whether something is a set or a map.
6117 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6119 if (!bmap)
6120 return isl_bool_error;
6121 return isl_space_is_set(bmap->dim);
6124 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6126 isl_bool is_set;
6128 is_set = isl_basic_map_is_set(bmap);
6129 if (is_set < 0)
6130 goto error;
6131 if (is_set)
6132 return bmap;
6133 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6134 error:
6135 isl_basic_map_free(bmap);
6136 return NULL;
6139 __isl_give isl_basic_map *isl_basic_map_domain_map(
6140 __isl_take isl_basic_map *bmap)
6142 int i;
6143 isl_space *space;
6144 isl_basic_map *domain;
6145 isl_size nparam, n_in, n_out;
6147 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6148 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6149 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6150 if (nparam < 0 || n_in < 0 || n_out < 0)
6151 return isl_basic_map_free(bmap);
6153 space = isl_basic_map_get_space(bmap);
6154 space = isl_space_from_range(isl_space_domain(space));
6155 domain = isl_basic_map_universe(space);
6157 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6158 bmap = isl_basic_map_apply_range(bmap, domain);
6159 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6161 for (i = 0; i < n_in; ++i)
6162 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6163 isl_dim_out, i);
6165 bmap = isl_basic_map_gauss(bmap, NULL);
6166 return isl_basic_map_finalize(bmap);
6169 __isl_give isl_basic_map *isl_basic_map_range_map(
6170 __isl_take isl_basic_map *bmap)
6172 int i;
6173 isl_space *space;
6174 isl_basic_map *range;
6175 isl_size nparam, n_in, n_out;
6177 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6178 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6179 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6180 if (nparam < 0 || n_in < 0 || n_out < 0)
6181 return isl_basic_map_free(bmap);
6183 space = isl_basic_map_get_space(bmap);
6184 space = isl_space_from_range(isl_space_range(space));
6185 range = isl_basic_map_universe(space);
6187 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6188 bmap = isl_basic_map_apply_range(bmap, range);
6189 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6191 for (i = 0; i < n_out; ++i)
6192 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6193 isl_dim_out, i);
6195 bmap = isl_basic_map_gauss(bmap, NULL);
6196 return isl_basic_map_finalize(bmap);
6199 int isl_map_may_be_set(__isl_keep isl_map *map)
6201 if (!map)
6202 return -1;
6203 return isl_space_may_be_set(map->dim);
6206 /* Is this map actually a set?
6207 * Users should never call this function. Outside of isl,
6208 * the type should indicate whether something is a set or a map.
6210 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6212 if (!map)
6213 return isl_bool_error;
6214 return isl_space_is_set(map->dim);
6217 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6219 isl_space *space;
6220 isl_size n_in;
6222 n_in = isl_map_dim(map, isl_dim_in);
6223 if (n_in < 0)
6224 return set_from_map(isl_map_free(map));
6225 space = isl_space_range(isl_map_get_space(map));
6227 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6229 return set_from_map(isl_map_reset_space(map, space));
6232 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6233 * to each of its basic maps.
6235 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6236 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6237 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6239 int i;
6240 isl_space *space;
6242 map = isl_map_cow(map);
6243 if (!map)
6244 return NULL;
6246 for (i = 0; i < map->n; ++i) {
6247 map->p[i] = fn_bmap(map->p[i]);
6248 if (!map->p[i])
6249 return isl_map_free(map);
6251 map = isl_map_unmark_normalized(map);
6253 space = isl_map_take_space(map);
6254 space = fn_space(space);
6255 map = isl_map_restore_space(map, space);
6257 return map;
6260 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6262 return isl_map_transform(map, &isl_space_domain_map,
6263 &isl_basic_map_domain_map);
6266 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6268 return isl_map_transform(map, &isl_space_range_map,
6269 &isl_basic_map_range_map);
6272 /* Given a wrapped map of the form A[B -> C],
6273 * return the map A[B -> C] -> B.
6275 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6277 isl_id *id;
6278 isl_map *map;
6280 if (!set)
6281 return NULL;
6282 if (!isl_set_has_tuple_id(set))
6283 return isl_map_domain_map(isl_set_unwrap(set));
6285 id = isl_set_get_tuple_id(set);
6286 map = isl_map_domain_map(isl_set_unwrap(set));
6287 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6289 return map;
6292 __isl_give isl_basic_map *isl_basic_map_from_domain(
6293 __isl_take isl_basic_set *bset)
6295 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6298 __isl_give isl_basic_map *isl_basic_map_from_range(
6299 __isl_take isl_basic_set *bset)
6301 isl_space *space;
6302 space = isl_basic_set_get_space(bset);
6303 space = isl_space_from_range(space);
6304 bset = isl_basic_set_reset_space(bset, space);
6305 return bset_to_bmap(bset);
6308 /* Create a relation with the given set as range.
6309 * The domain of the created relation is a zero-dimensional
6310 * flat anonymous space.
6312 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6314 isl_space *space;
6315 space = isl_set_get_space(set);
6316 space = isl_space_from_range(space);
6317 set = isl_set_reset_space(set, space);
6318 return set_to_map(set);
6321 /* Create a relation with the given set as domain.
6322 * The range of the created relation is a zero-dimensional
6323 * flat anonymous space.
6325 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6327 return isl_map_reverse(isl_map_from_range(set));
6330 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6331 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6333 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6336 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6337 __isl_take isl_set *range)
6339 return isl_map_apply_range(isl_map_reverse(domain), range);
6342 /* Return a newly allocated isl_map with given space and flags and
6343 * room for "n" basic maps.
6344 * Make sure that all cached information is cleared.
6346 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6347 unsigned flags)
6349 struct isl_map *map;
6351 if (!space)
6352 return NULL;
6353 if (n < 0)
6354 isl_die(space->ctx, isl_error_internal,
6355 "negative number of basic maps", goto error);
6356 map = isl_calloc(space->ctx, struct isl_map,
6357 sizeof(struct isl_map) +
6358 (n - 1) * sizeof(struct isl_basic_map *));
6359 if (!map)
6360 goto error;
6362 map->ctx = space->ctx;
6363 isl_ctx_ref(map->ctx);
6364 map->ref = 1;
6365 map->size = n;
6366 map->n = 0;
6367 map->dim = space;
6368 map->flags = flags;
6369 return map;
6370 error:
6371 isl_space_free(space);
6372 return NULL;
6375 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6377 struct isl_basic_map *bmap;
6378 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6379 bmap = isl_basic_map_set_to_empty(bmap);
6380 return bmap;
6383 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6385 struct isl_basic_set *bset;
6386 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6387 bset = isl_basic_set_set_to_empty(bset);
6388 return bset;
6391 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6393 struct isl_basic_map *bmap;
6394 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6395 bmap = isl_basic_map_finalize(bmap);
6396 return bmap;
6399 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6401 struct isl_basic_set *bset;
6402 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6403 bset = isl_basic_set_finalize(bset);
6404 return bset;
6407 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6408 __isl_take isl_space *space)
6410 int i;
6411 isl_size total = isl_space_dim(space, isl_dim_all);
6412 isl_basic_map *bmap;
6414 if (total < 0)
6415 space = isl_space_free(space);
6416 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6417 for (i = 0; i < total; ++i) {
6418 int k = isl_basic_map_alloc_inequality(bmap);
6419 if (k < 0)
6420 goto error;
6421 isl_seq_clr(bmap->ineq[k], 1 + total);
6422 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6424 return bmap;
6425 error:
6426 isl_basic_map_free(bmap);
6427 return NULL;
6430 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6431 __isl_take isl_space *space)
6433 return isl_basic_map_nat_universe(space);
6436 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6438 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6441 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6443 return isl_map_nat_universe(space);
6446 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6448 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6451 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6453 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6456 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6458 struct isl_map *map;
6459 if (!space)
6460 return NULL;
6461 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6462 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6463 return map;
6466 /* This function performs the same operation as isl_map_universe,
6467 * but is considered as a function on an isl_space when exported.
6469 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6471 return isl_map_universe(space);
6474 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6476 struct isl_set *set;
6477 if (!space)
6478 return NULL;
6479 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6480 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6481 return set;
6484 /* This function performs the same operation as isl_set_universe,
6485 * but is considered as a function on an isl_space when exported.
6487 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6489 return isl_set_universe(space);
6492 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6494 int i;
6495 struct isl_map *dup;
6497 if (!map)
6498 return NULL;
6499 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6500 for (i = 0; i < map->n; ++i)
6501 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6502 return dup;
6505 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6506 __isl_take isl_basic_map *bmap)
6508 if (!bmap || !map)
6509 goto error;
6510 if (isl_basic_map_plain_is_empty(bmap)) {
6511 isl_basic_map_free(bmap);
6512 return map;
6514 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6515 goto error;
6516 isl_assert(map->ctx, map->n < map->size, goto error);
6517 map->p[map->n] = bmap;
6518 map->n++;
6519 map = isl_map_unmark_normalized(map);
6520 return map;
6521 error:
6522 if (map)
6523 isl_map_free(map);
6524 if (bmap)
6525 isl_basic_map_free(bmap);
6526 return NULL;
6529 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6531 int i;
6533 if (!map)
6534 return NULL;
6536 if (--map->ref > 0)
6537 return NULL;
6539 clear_caches(map);
6540 isl_ctx_deref(map->ctx);
6541 for (i = 0; i < map->n; ++i)
6542 isl_basic_map_free(map->p[i]);
6543 isl_space_free(map->dim);
6544 free(map);
6546 return NULL;
6549 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6550 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6552 int j;
6553 isl_size total;
6555 total = isl_basic_map_dim(bmap, isl_dim_all);
6556 if (total < 0)
6557 return isl_basic_map_free(bmap);
6559 bmap = isl_basic_map_cow(bmap);
6560 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6561 j = isl_basic_map_alloc_equality(bmap);
6562 if (j < 0)
6563 goto error;
6564 isl_seq_clr(bmap->eq[j] + 1, total);
6565 isl_int_set_si(bmap->eq[j][pos], -1);
6566 isl_int_set_si(bmap->eq[j][0], value);
6567 bmap = isl_basic_map_simplify(bmap);
6568 return isl_basic_map_finalize(bmap);
6569 error:
6570 isl_basic_map_free(bmap);
6571 return NULL;
6574 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6575 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6577 int j;
6578 isl_size total;
6580 total = isl_basic_map_dim(bmap, isl_dim_all);
6581 if (total < 0)
6582 return isl_basic_map_free(bmap);
6584 bmap = isl_basic_map_cow(bmap);
6585 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6586 j = isl_basic_map_alloc_equality(bmap);
6587 if (j < 0)
6588 goto error;
6589 isl_seq_clr(bmap->eq[j] + 1, total);
6590 isl_int_set_si(bmap->eq[j][pos], -1);
6591 isl_int_set(bmap->eq[j][0], value);
6592 bmap = isl_basic_map_simplify(bmap);
6593 return isl_basic_map_finalize(bmap);
6594 error:
6595 isl_basic_map_free(bmap);
6596 return NULL;
6599 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6600 enum isl_dim_type type, unsigned pos, int value)
6602 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6603 return isl_basic_map_free(bmap);
6604 return isl_basic_map_fix_pos_si(bmap,
6605 isl_basic_map_offset(bmap, type) + pos, value);
6608 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6609 enum isl_dim_type type, unsigned pos, isl_int value)
6611 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6612 return isl_basic_map_free(bmap);
6613 return isl_basic_map_fix_pos(bmap,
6614 isl_basic_map_offset(bmap, type) + pos, value);
6617 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6618 * to be equal to "v".
6620 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6621 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6623 if (!bmap || !v)
6624 goto error;
6625 if (!isl_val_is_int(v))
6626 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6627 "expecting integer value", goto error);
6628 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6629 goto error;
6630 pos += isl_basic_map_offset(bmap, type);
6631 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6632 isl_val_free(v);
6633 return bmap;
6634 error:
6635 isl_basic_map_free(bmap);
6636 isl_val_free(v);
6637 return NULL;
6640 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6641 * to be equal to "v".
6643 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6644 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6646 return isl_basic_map_fix_val(bset, type, pos, v);
6649 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6650 enum isl_dim_type type, unsigned pos, int value)
6652 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6653 type, pos, value));
6656 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6657 enum isl_dim_type type, unsigned pos, isl_int value)
6659 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6660 type, pos, value));
6663 /* Remove the basic map at position "i" from "map" if this basic map
6664 * is (obviously) empty.
6666 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6668 isl_bool empty;
6670 if (!map)
6671 return NULL;
6673 empty = isl_basic_map_plain_is_empty(map->p[i]);
6674 if (empty < 0)
6675 return isl_map_free(map);
6676 if (!empty)
6677 return map;
6679 isl_basic_map_free(map->p[i]);
6680 map->n--;
6681 if (i != map->n) {
6682 map->p[i] = map->p[map->n];
6683 map = isl_map_unmark_normalized(map);
6687 return map;
6690 /* Perform "fn" on each basic map of "map", where we may not be holding
6691 * the only reference to "map".
6692 * In particular, "fn" should be a semantics preserving operation
6693 * that we want to apply to all copies of "map". We therefore need
6694 * to be careful not to modify "map" in a way that breaks "map"
6695 * in case anything goes wrong.
6697 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6698 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6700 struct isl_basic_map *bmap;
6701 int i;
6703 if (!map)
6704 return NULL;
6706 for (i = map->n - 1; i >= 0; --i) {
6707 bmap = isl_basic_map_copy(map->p[i]);
6708 bmap = fn(bmap);
6709 if (!bmap)
6710 goto error;
6711 isl_basic_map_free(map->p[i]);
6712 map->p[i] = bmap;
6713 map = remove_if_empty(map, i);
6714 if (!map)
6715 return NULL;
6718 return map;
6719 error:
6720 isl_map_free(map);
6721 return NULL;
6724 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6725 enum isl_dim_type type, unsigned pos, int value)
6727 int i;
6729 map = isl_map_cow(map);
6730 if (isl_map_check_range(map, type, pos, 1) < 0)
6731 return isl_map_free(map);
6732 for (i = map->n - 1; i >= 0; --i) {
6733 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6734 map = remove_if_empty(map, i);
6735 if (!map)
6736 return NULL;
6738 map = isl_map_unmark_normalized(map);
6739 return map;
6742 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6743 enum isl_dim_type type, unsigned pos, int value)
6745 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6748 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6749 enum isl_dim_type type, unsigned pos, isl_int value)
6751 int i;
6753 map = isl_map_cow(map);
6754 if (isl_map_check_range(map, type, pos, 1) < 0)
6755 return isl_map_free(map);
6756 for (i = 0; i < map->n; ++i) {
6757 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6758 if (!map->p[i])
6759 goto error;
6761 map = isl_map_unmark_normalized(map);
6762 return map;
6763 error:
6764 isl_map_free(map);
6765 return NULL;
6768 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6769 enum isl_dim_type type, unsigned pos, isl_int value)
6771 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6774 /* Fix the value of the variable at position "pos" of type "type" of "map"
6775 * to be equal to "v".
6777 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6778 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6780 int i;
6782 map = isl_map_cow(map);
6783 if (!map || !v)
6784 goto error;
6786 if (!isl_val_is_int(v))
6787 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6788 "expecting integer value", goto error);
6789 if (isl_map_check_range(map, type, pos, 1) < 0)
6790 goto error;
6791 for (i = map->n - 1; i >= 0; --i) {
6792 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6793 isl_val_copy(v));
6794 map = remove_if_empty(map, i);
6795 if (!map)
6796 goto error;
6798 map = isl_map_unmark_normalized(map);
6799 isl_val_free(v);
6800 return map;
6801 error:
6802 isl_map_free(map);
6803 isl_val_free(v);
6804 return NULL;
6807 /* Fix the value of the variable at position "pos" of type "type" of "set"
6808 * to be equal to "v".
6810 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6811 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6813 return isl_map_fix_val(set, type, pos, v);
6816 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6817 unsigned input, int value)
6819 return isl_map_fix_si(map, isl_dim_in, input, value);
6822 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6823 int value)
6825 return set_from_map(isl_map_fix_si(set_to_map(set),
6826 isl_dim_set, dim, value));
6829 static __isl_give isl_basic_map *basic_map_bound_si(
6830 __isl_take isl_basic_map *bmap,
6831 enum isl_dim_type type, unsigned pos, int value, int upper)
6833 int j;
6834 isl_size total;
6836 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6837 return isl_basic_map_free(bmap);
6838 total = isl_basic_map_dim(bmap, isl_dim_all);
6839 if (total < 0)
6840 return isl_basic_map_free(bmap);
6841 pos += isl_basic_map_offset(bmap, type);
6842 bmap = isl_basic_map_cow(bmap);
6843 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6844 j = isl_basic_map_alloc_inequality(bmap);
6845 if (j < 0)
6846 goto error;
6847 isl_seq_clr(bmap->ineq[j], 1 + total);
6848 if (upper) {
6849 isl_int_set_si(bmap->ineq[j][pos], -1);
6850 isl_int_set_si(bmap->ineq[j][0], value);
6851 } else {
6852 isl_int_set_si(bmap->ineq[j][pos], 1);
6853 isl_int_set_si(bmap->ineq[j][0], -value);
6855 bmap = isl_basic_map_simplify(bmap);
6856 return isl_basic_map_finalize(bmap);
6857 error:
6858 isl_basic_map_free(bmap);
6859 return NULL;
6862 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6863 __isl_take isl_basic_map *bmap,
6864 enum isl_dim_type type, unsigned pos, int value)
6866 return basic_map_bound_si(bmap, type, pos, value, 0);
6869 /* Constrain the values of the given dimension to be no greater than "value".
6871 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6872 __isl_take isl_basic_map *bmap,
6873 enum isl_dim_type type, unsigned pos, int value)
6875 return basic_map_bound_si(bmap, type, pos, value, 1);
6878 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6879 enum isl_dim_type type, unsigned pos, int value, int upper)
6881 int i;
6883 map = isl_map_cow(map);
6884 if (isl_map_check_range(map, type, pos, 1) < 0)
6885 return isl_map_free(map);
6886 for (i = 0; i < map->n; ++i) {
6887 map->p[i] = basic_map_bound_si(map->p[i],
6888 type, pos, value, upper);
6889 if (!map->p[i])
6890 goto error;
6892 map = isl_map_unmark_normalized(map);
6893 return map;
6894 error:
6895 isl_map_free(map);
6896 return NULL;
6899 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6900 enum isl_dim_type type, unsigned pos, int value)
6902 return map_bound_si(map, type, pos, value, 0);
6905 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6906 enum isl_dim_type type, unsigned pos, int value)
6908 return map_bound_si(map, type, pos, value, 1);
6911 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6912 enum isl_dim_type type, unsigned pos, int value)
6914 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6915 type, pos, value));
6918 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6919 enum isl_dim_type type, unsigned pos, int value)
6921 return isl_map_upper_bound_si(set, type, pos, value);
6924 /* Bound the given variable of "bmap" from below (or above is "upper"
6925 * is set) to "value".
6927 static __isl_give isl_basic_map *basic_map_bound(
6928 __isl_take isl_basic_map *bmap,
6929 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6931 int j;
6932 isl_size total;
6934 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6935 return isl_basic_map_free(bmap);
6936 total = isl_basic_map_dim(bmap, isl_dim_all);
6937 if (total < 0)
6938 return isl_basic_map_free(bmap);
6939 pos += isl_basic_map_offset(bmap, type);
6940 bmap = isl_basic_map_cow(bmap);
6941 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6942 j = isl_basic_map_alloc_inequality(bmap);
6943 if (j < 0)
6944 goto error;
6945 isl_seq_clr(bmap->ineq[j], 1 + total);
6946 if (upper) {
6947 isl_int_set_si(bmap->ineq[j][pos], -1);
6948 isl_int_set(bmap->ineq[j][0], value);
6949 } else {
6950 isl_int_set_si(bmap->ineq[j][pos], 1);
6951 isl_int_neg(bmap->ineq[j][0], value);
6953 bmap = isl_basic_map_simplify(bmap);
6954 return isl_basic_map_finalize(bmap);
6955 error:
6956 isl_basic_map_free(bmap);
6957 return NULL;
6960 /* Bound the given variable of "map" from below (or above is "upper"
6961 * is set) to "value".
6963 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6964 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6966 int i;
6968 map = isl_map_cow(map);
6969 if (isl_map_check_range(map, type, pos, 1) < 0)
6970 return isl_map_free(map);
6971 for (i = map->n - 1; i >= 0; --i) {
6972 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6973 map = remove_if_empty(map, i);
6974 if (!map)
6975 return NULL;
6977 map = isl_map_unmark_normalized(map);
6978 return map;
6981 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6982 enum isl_dim_type type, unsigned pos, isl_int value)
6984 return map_bound(map, type, pos, value, 0);
6987 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6988 enum isl_dim_type type, unsigned pos, isl_int value)
6990 return map_bound(map, type, pos, value, 1);
6993 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6994 enum isl_dim_type type, unsigned pos, isl_int value)
6996 return isl_map_lower_bound(set, type, pos, value);
6999 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
7000 enum isl_dim_type type, unsigned pos, isl_int value)
7002 return isl_map_upper_bound(set, type, pos, value);
7005 /* Force the values of the variable at position "pos" of type "type" of "map"
7006 * to be no smaller than "value".
7008 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
7009 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7011 if (!value)
7012 goto error;
7013 if (!isl_val_is_int(value))
7014 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7015 "expecting integer value", goto error);
7016 map = isl_map_lower_bound(map, type, pos, value->n);
7017 isl_val_free(value);
7018 return map;
7019 error:
7020 isl_val_free(value);
7021 isl_map_free(map);
7022 return NULL;
7025 /* Force the values of the variable at position "pos" of type "type" of "set"
7026 * to be no smaller than "value".
7028 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
7029 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7031 isl_map *map;
7033 map = set_to_map(set);
7034 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
7037 /* Force the values of the variable at position "pos" of type "type" of "map"
7038 * to be no greater than "value".
7040 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
7041 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7043 if (!value)
7044 goto error;
7045 if (!isl_val_is_int(value))
7046 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7047 "expecting integer value", goto error);
7048 map = isl_map_upper_bound(map, type, pos, value->n);
7049 isl_val_free(value);
7050 return map;
7051 error:
7052 isl_val_free(value);
7053 isl_map_free(map);
7054 return NULL;
7057 /* Force the values of the variable at position "pos" of type "type" of "set"
7058 * to be no greater than "value".
7060 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7061 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7063 isl_map *map;
7065 map = set_to_map(set);
7066 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7069 /* If "mv" has an explicit domain, then intersect the domain of "map"
7070 * with this explicit domain.
7072 * An isl_multi_val object never has an explicit domain,
7073 * so simply return "map".
7075 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7076 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7078 return map;
7081 #undef BASE
7082 #define BASE val
7083 #include "isl_map_bound_templ.c"
7085 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7086 * for each set dimension, by treating the set as a map.
7088 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7089 __isl_take isl_multi_val *bound,
7090 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7091 unsigned pos, __isl_take isl_val *value))
7093 isl_map *map;
7095 map = set_to_map(set);
7096 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7099 #undef BASE
7100 #define BASE pw_aff
7101 #include "isl_map_bound_templ.c"
7103 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7104 * for each set dimension, by converting the set and the bound
7105 * to objects living in a map space.
7107 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7108 __isl_take isl_multi_pw_aff *bound,
7109 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7110 unsigned pos, __isl_take TYPE *value))
7112 isl_map *map;
7114 map = isl_map_from_range(set);
7115 bound = isl_multi_pw_aff_from_range(bound);
7116 map = map_bound_multi_pw_aff(map, bound, set_bound);
7117 return isl_map_range(map);
7120 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7121 * setting a bound on the given output dimension.
7123 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7124 unsigned pos, __isl_take isl_val *v)
7126 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7129 /* Force the values of the set dimensions of "set"
7130 * to be no smaller than the corresponding values in "lower".
7132 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7133 __isl_take isl_multi_val *lower)
7135 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7138 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7139 * setting a bound on the given output dimension.
7141 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7142 unsigned pos, __isl_take isl_val *v)
7144 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7147 /* Force the values of the set dimensions of "set"
7148 * to be no greater than the corresponding values in "upper".
7150 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7151 __isl_take isl_multi_val *upper)
7153 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7156 /* Force the symbolic constant expression "bound"
7157 * to satisfy the relation "order" with respect to
7158 * the output variable at position "pos" of "map".
7160 * Create an affine expression representing the output variable
7161 * in terms of the range and
7162 * compare it using "order" to "bound" (defined on the domain).
7163 * The result is a relation between elements in domain and range that
7164 * can be intersected with "map".
7166 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7167 unsigned pos, __isl_take isl_pw_aff *bound,
7168 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7169 __isl_take isl_pw_aff *pa2))
7171 isl_space *space;
7172 isl_local_space *ls;
7173 isl_pw_aff *var;
7175 space = isl_space_range(isl_map_get_space(map));
7176 ls = isl_local_space_from_space(space);
7177 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7178 map = isl_map_intersect(map, order(bound, var));
7179 return map;
7182 /* Force the values of the output variable at position "pos" of "map"
7183 * to be no smaller than the symbolic constant expression "lower".
7185 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7186 unsigned pos, __isl_take isl_pw_aff *lower)
7188 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7191 /* Force the values of the output variable at position "pos" of "map"
7192 * to be no greater than the symbolic constant expression "upper".
7194 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7195 unsigned pos, __isl_take isl_pw_aff *upper)
7197 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7200 /* Force the values of the set dimensions of "set"
7201 * to be no smaller than the corresponding constant symbolic expressions
7202 * in "lower".
7204 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7205 __isl_take isl_multi_pw_aff *lower)
7207 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7210 /* Force the values of the set dimensions of "set"
7211 * to be no greater than the corresponding constant symbolic expressions
7212 * in "upper".
7214 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7215 __isl_take isl_multi_pw_aff *upper)
7217 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7220 /* Force the values of the output dimensions of "map"
7221 * to be no smaller than the corresponding constant symbolic expressions
7222 * in "lower".
7224 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7225 __isl_take isl_multi_pw_aff *lower)
7227 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7230 /* Force the values of the output dimensions of "map"
7231 * to be no greater than the corresponding constant symbolic expressions
7232 * in "upper".
7234 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7235 __isl_take isl_multi_pw_aff *upper)
7237 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7240 /* Bound the given variable of "bset" from below (or above is "upper"
7241 * is set) to "value".
7243 static __isl_give isl_basic_set *isl_basic_set_bound(
7244 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7245 isl_int value, int upper)
7247 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7248 type, pos, value, upper));
7251 /* Bound the given variable of "bset" from below (or above is "upper"
7252 * is set) to "value".
7254 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7255 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7256 __isl_take isl_val *value, int upper)
7258 if (!value)
7259 goto error;
7260 if (!isl_val_is_int(value))
7261 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7262 "expecting integer value", goto error);
7263 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7264 isl_val_free(value);
7265 return bset;
7266 error:
7267 isl_val_free(value);
7268 isl_basic_set_free(bset);
7269 return NULL;
7272 /* Bound the given variable of "bset" from below to "value".
7274 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7275 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7276 __isl_take isl_val *value)
7278 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7281 /* Bound the given variable of "bset" from above to "value".
7283 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7284 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7285 __isl_take isl_val *value)
7287 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7290 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7292 return isl_map_transform(map, &isl_space_reverse,
7293 &isl_basic_map_reverse);
7296 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7298 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7300 return isl_map_transform(map, &isl_space_domain_reverse,
7301 &isl_basic_map_domain_reverse);
7304 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7306 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7308 return isl_map_transform(map, &isl_space_range_reverse,
7309 &isl_basic_map_range_reverse);
7312 /* Given a set (A -> B), return the corresponding set (B -> A).
7314 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7316 isl_map *map = set_to_map(set);
7318 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7319 &isl_basic_map_set_reverse);
7320 return set_from_map(map);
7323 #undef TYPE
7324 #define TYPE isl_pw_multi_aff
7325 #undef SUFFIX
7326 #define SUFFIX _pw_multi_aff
7327 #undef EMPTY
7328 #define EMPTY isl_pw_multi_aff_empty
7329 #undef ADD
7330 #define ADD isl_pw_multi_aff_union_add
7331 #include "isl_map_lexopt_templ.c"
7333 /* Given a map "map", compute the lexicographically minimal
7334 * (or maximal) image element for each domain element in dom,
7335 * in the form of an isl_pw_multi_aff.
7336 * If "empty" is not NULL, then set *empty to those elements in dom that
7337 * do not have an image element.
7338 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7339 * should be computed over the domain of "map". "empty" is also NULL
7340 * in this case.
7342 * We first compute the lexicographically minimal or maximal element
7343 * in the first basic map. This results in a partial solution "res"
7344 * and a subset "todo" of dom that still need to be handled.
7345 * We then consider each of the remaining maps in "map" and successively
7346 * update both "res" and "todo".
7347 * If "empty" is NULL, then the todo sets are not needed and therefore
7348 * also not computed.
7350 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7351 __isl_take isl_map *map, __isl_take isl_set *dom,
7352 __isl_give isl_set **empty, unsigned flags)
7354 int i;
7355 int full;
7356 isl_pw_multi_aff *res;
7357 isl_set *todo;
7359 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7360 if (!map || (!full && !dom))
7361 goto error;
7363 if (isl_map_plain_is_empty(map)) {
7364 if (empty)
7365 *empty = dom;
7366 else
7367 isl_set_free(dom);
7368 return isl_pw_multi_aff_from_map(map);
7371 res = basic_map_partial_lexopt_pw_multi_aff(
7372 isl_basic_map_copy(map->p[0]),
7373 isl_set_copy(dom), empty, flags);
7375 if (empty)
7376 todo = *empty;
7377 for (i = 1; i < map->n; ++i) {
7378 isl_pw_multi_aff *res_i;
7380 res_i = basic_map_partial_lexopt_pw_multi_aff(
7381 isl_basic_map_copy(map->p[i]),
7382 isl_set_copy(dom), empty, flags);
7384 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7385 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7386 else
7387 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7389 if (empty)
7390 todo = isl_set_intersect(todo, *empty);
7393 isl_set_free(dom);
7394 isl_map_free(map);
7396 if (empty)
7397 *empty = todo;
7399 return res;
7400 error:
7401 if (empty)
7402 *empty = NULL;
7403 isl_set_free(dom);
7404 isl_map_free(map);
7405 return NULL;
7408 #undef TYPE
7409 #define TYPE isl_map
7410 #undef SUFFIX
7411 #define SUFFIX
7412 #undef EMPTY
7413 #define EMPTY isl_map_empty
7414 #undef ADD
7415 #define ADD isl_map_union_disjoint
7416 #include "isl_map_lexopt_templ.c"
7418 /* Given a map "map", compute the lexicographically minimal
7419 * (or maximal) image element for each domain element in "dom",
7420 * in the form of an isl_map.
7421 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7422 * do not have an image element.
7423 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7424 * should be computed over the domain of "map". "empty" is also NULL
7425 * in this case.
7427 * If the input consists of more than one disjunct, then first
7428 * compute the desired result in the form of an isl_pw_multi_aff and
7429 * then convert that into an isl_map.
7431 * This function used to have an explicit implementation in terms
7432 * of isl_maps, but it would continually intersect the domains of
7433 * partial results with the complement of the domain of the next
7434 * partial solution, potentially leading to an explosion in the number
7435 * of disjuncts if there are several disjuncts in the input.
7436 * An even earlier implementation of this function would look for
7437 * better results in the domain of the partial result and for extra
7438 * results in the complement of this domain, which would lead to
7439 * even more splintering.
7441 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7442 __isl_take isl_map *map, __isl_take isl_set *dom,
7443 __isl_give isl_set **empty, unsigned flags)
7445 int full;
7446 struct isl_map *res;
7447 isl_pw_multi_aff *pma;
7449 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7450 if (!map || (!full && !dom))
7451 goto error;
7453 if (isl_map_plain_is_empty(map)) {
7454 if (empty)
7455 *empty = dom;
7456 else
7457 isl_set_free(dom);
7458 return map;
7461 if (map->n == 1) {
7462 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7463 dom, empty, flags);
7464 isl_map_free(map);
7465 return res;
7468 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7469 flags);
7470 return isl_map_from_pw_multi_aff_internal(pma);
7471 error:
7472 if (empty)
7473 *empty = NULL;
7474 isl_set_free(dom);
7475 isl_map_free(map);
7476 return NULL;
7479 __isl_give isl_map *isl_map_partial_lexmax(
7480 __isl_take isl_map *map, __isl_take isl_set *dom,
7481 __isl_give isl_set **empty)
7483 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7486 __isl_give isl_map *isl_map_partial_lexmin(
7487 __isl_take isl_map *map, __isl_take isl_set *dom,
7488 __isl_give isl_set **empty)
7490 return isl_map_partial_lexopt(map, dom, empty, 0);
7493 __isl_give isl_set *isl_set_partial_lexmin(
7494 __isl_take isl_set *set, __isl_take isl_set *dom,
7495 __isl_give isl_set **empty)
7497 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7498 dom, empty));
7501 __isl_give isl_set *isl_set_partial_lexmax(
7502 __isl_take isl_set *set, __isl_take isl_set *dom,
7503 __isl_give isl_set **empty)
7505 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7506 dom, empty));
7509 /* Compute the lexicographic minimum (or maximum if "flags" includes
7510 * ISL_OPT_MAX) of "bset" over its parametric domain.
7512 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7513 unsigned flags)
7515 return isl_basic_map_lexopt(bset, flags);
7518 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7520 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7523 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7525 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7528 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7530 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7533 /* Compute the lexicographic minimum of "bset" over its parametric domain
7534 * for the purpose of quantifier elimination.
7535 * That is, find an explicit representation for all the existentially
7536 * quantified variables in "bset" by computing their lexicographic
7537 * minimum.
7539 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7540 __isl_take isl_basic_set *bset)
7542 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7545 /* Given a basic map with one output dimension, compute the minimum or
7546 * maximum of that dimension as an isl_pw_aff.
7548 * Compute the optimum as a lexicographic optimum over the single
7549 * output dimension and extract the single isl_pw_aff from the result.
7551 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7552 int max)
7554 isl_pw_multi_aff *pma;
7555 isl_pw_aff *pwaff;
7557 bmap = isl_basic_map_copy(bmap);
7558 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7559 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7560 isl_pw_multi_aff_free(pma);
7562 return pwaff;
7565 /* Compute the minimum or maximum of the given output dimension
7566 * as a function of the parameters and the input dimensions,
7567 * but independently of the other output dimensions.
7569 * We first project out the other output dimension and then compute
7570 * the "lexicographic" maximum in each basic map, combining the results
7571 * using isl_pw_aff_union_max.
7573 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7574 int max)
7576 int i;
7577 isl_pw_aff *pwaff;
7578 isl_size n_out;
7580 n_out = isl_map_dim(map, isl_dim_out);
7581 if (n_out < 0)
7582 map = isl_map_free(map);
7583 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7584 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7585 if (!map)
7586 return NULL;
7588 if (map->n == 0) {
7589 isl_space *space = isl_map_get_space(map);
7590 isl_map_free(map);
7591 return isl_pw_aff_empty(space);
7594 pwaff = basic_map_dim_opt(map->p[0], max);
7595 for (i = 1; i < map->n; ++i) {
7596 isl_pw_aff *pwaff_i;
7598 pwaff_i = basic_map_dim_opt(map->p[i], max);
7599 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7602 isl_map_free(map);
7604 return pwaff;
7607 /* Compute the minimum of the given output dimension as a function of the
7608 * parameters and input dimensions, but independently of
7609 * the other output dimensions.
7611 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7613 return map_dim_opt(map, pos, 0);
7616 /* Compute the maximum of the given output dimension as a function of the
7617 * parameters and input dimensions, but independently of
7618 * the other output dimensions.
7620 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7622 return map_dim_opt(map, pos, 1);
7625 /* Compute the minimum or maximum of the given set dimension
7626 * as a function of the parameters,
7627 * but independently of the other set dimensions.
7629 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7630 int max)
7632 return map_dim_opt(set, pos, max);
7635 /* Compute the maximum of the given set dimension as a function of the
7636 * parameters, but independently of the other set dimensions.
7638 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7640 return set_dim_opt(set, pos, 1);
7643 /* Compute the minimum of the given set dimension as a function of the
7644 * parameters, but independently of the other set dimensions.
7646 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7648 return set_dim_opt(set, pos, 0);
7651 /* Apply a preimage specified by "mat" on the parameters of "bset".
7652 * bset is assumed to have only parameters and divs.
7654 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7655 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7657 isl_size nparam;
7659 nparam = isl_basic_set_dim(bset, isl_dim_param);
7660 if (nparam < 0 || !mat)
7661 goto error;
7663 bset->dim = isl_space_cow(bset->dim);
7664 if (!bset->dim)
7665 goto error;
7667 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7669 bset->dim->nparam = 0;
7670 bset->dim->n_out = nparam;
7671 bset = isl_basic_set_preimage(bset, mat);
7672 if (bset) {
7673 bset->dim->nparam = bset->dim->n_out;
7674 bset->dim->n_out = 0;
7676 return bset;
7677 error:
7678 isl_mat_free(mat);
7679 isl_basic_set_free(bset);
7680 return NULL;
7683 /* Apply a preimage specified by "mat" on the parameters of "set".
7684 * set is assumed to have only parameters and divs.
7686 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7687 __isl_take isl_mat *mat)
7689 isl_space *space;
7690 isl_size nparam;
7692 nparam = isl_set_dim(set, isl_dim_param);
7693 if (nparam < 0 || !mat)
7694 goto error;
7696 if (mat->n_row != 1 + nparam)
7697 isl_die(isl_set_get_ctx(set), isl_error_internal,
7698 "unexpected number of rows", goto error);
7700 space = isl_set_get_space(set);
7701 space = isl_space_move_dims(space, isl_dim_set, 0,
7702 isl_dim_param, 0, nparam);
7703 set = isl_set_reset_space(set, space);
7704 set = isl_set_preimage(set, mat);
7705 nparam = isl_set_dim(set, isl_dim_out);
7706 if (nparam < 0)
7707 set = isl_set_free(set);
7708 space = isl_set_get_space(set);
7709 space = isl_space_move_dims(space, isl_dim_param, 0,
7710 isl_dim_out, 0, nparam);
7711 set = isl_set_reset_space(set, space);
7712 return set;
7713 error:
7714 isl_mat_free(mat);
7715 isl_set_free(set);
7716 return NULL;
7719 /* Intersect the basic set "bset" with the affine space specified by the
7720 * equalities in "eq".
7722 static __isl_give isl_basic_set *basic_set_append_equalities(
7723 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7725 int i, k;
7726 unsigned len;
7728 if (!bset || !eq)
7729 goto error;
7731 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7732 if (!bset)
7733 goto error;
7735 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7736 for (i = 0; i < eq->n_row; ++i) {
7737 k = isl_basic_set_alloc_equality(bset);
7738 if (k < 0)
7739 goto error;
7740 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7741 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7743 isl_mat_free(eq);
7745 bset = isl_basic_set_gauss(bset, NULL);
7746 bset = isl_basic_set_finalize(bset);
7748 return bset;
7749 error:
7750 isl_mat_free(eq);
7751 isl_basic_set_free(bset);
7752 return NULL;
7755 /* Intersect the set "set" with the affine space specified by the
7756 * equalities in "eq".
7758 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7759 __isl_take isl_mat *eq)
7761 int i;
7763 if (!set || !eq)
7764 goto error;
7766 for (i = 0; i < set->n; ++i) {
7767 set->p[i] = basic_set_append_equalities(set->p[i],
7768 isl_mat_copy(eq));
7769 if (!set->p[i])
7770 goto error;
7772 isl_mat_free(eq);
7773 return set;
7774 error:
7775 isl_mat_free(eq);
7776 isl_set_free(set);
7777 return NULL;
7780 /* Given a basic set "bset" that only involves parameters and existentially
7781 * quantified variables, return the index of the first equality
7782 * that only involves parameters. If there is no such equality then
7783 * return bset->n_eq.
7785 * This function assumes that isl_basic_set_gauss has been called on "bset".
7787 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7789 int i, j;
7790 isl_size nparam, n_div;
7792 nparam = isl_basic_set_dim(bset, isl_dim_param);
7793 n_div = isl_basic_set_dim(bset, isl_dim_div);
7794 if (nparam < 0 || n_div < 0)
7795 return -1;
7797 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7798 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7799 ++i;
7802 return i;
7805 /* Compute an explicit representation for the existentially quantified
7806 * variables in "bset" by computing the "minimal value" of the set
7807 * variables. Since there are no set variables, the computation of
7808 * the minimal value essentially computes an explicit representation
7809 * of the non-empty part(s) of "bset".
7811 * The input only involves parameters and existentially quantified variables.
7812 * All equalities among parameters have been removed.
7814 * Since the existentially quantified variables in the result are in general
7815 * going to be different from those in the input, we first replace
7816 * them by the minimal number of variables based on their equalities.
7817 * This should simplify the parametric integer programming.
7819 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7821 isl_morph *morph1, *morph2;
7822 isl_set *set;
7823 isl_size n;
7825 if (!bset)
7826 return NULL;
7827 if (bset->n_eq == 0)
7828 return isl_basic_set_lexmin_compute_divs(bset);
7830 morph1 = isl_basic_set_parameter_compression(bset);
7831 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7832 bset = isl_basic_set_lift(bset);
7833 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7834 bset = isl_morph_basic_set(morph2, bset);
7835 n = isl_basic_set_dim(bset, isl_dim_set);
7836 if (n < 0)
7837 bset = isl_basic_set_free(bset);
7838 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7840 set = isl_basic_set_lexmin_compute_divs(bset);
7842 set = isl_morph_set(isl_morph_inverse(morph1), set);
7844 return set;
7847 /* Project the given basic set onto its parameter domain, possibly introducing
7848 * new, explicit, existential variables in the constraints.
7849 * The input has parameters and (possibly implicit) existential variables.
7850 * The output has the same parameters, but only
7851 * explicit existentially quantified variables.
7853 * The actual projection is performed by pip, but pip doesn't seem
7854 * to like equalities very much, so we first remove the equalities
7855 * among the parameters by performing a variable compression on
7856 * the parameters. Afterward, an inverse transformation is performed
7857 * and the equalities among the parameters are inserted back in.
7859 * The variable compression on the parameters may uncover additional
7860 * equalities that were only implicit before. We therefore check
7861 * if there are any new parameter equalities in the result and
7862 * if so recurse. The removal of parameter equalities is required
7863 * for the parameter compression performed by base_compute_divs.
7865 static __isl_give isl_set *parameter_compute_divs(
7866 __isl_take isl_basic_set *bset)
7868 int i;
7869 struct isl_mat *eq;
7870 struct isl_mat *T, *T2;
7871 struct isl_set *set;
7872 isl_size nparam;
7874 bset = isl_basic_set_cow(bset);
7875 if (!bset)
7876 return NULL;
7878 if (bset->n_eq == 0)
7879 return base_compute_divs(bset);
7881 bset = isl_basic_set_gauss(bset, NULL);
7882 if (!bset)
7883 return NULL;
7884 if (isl_basic_set_plain_is_empty(bset))
7885 return isl_set_from_basic_set(bset);
7887 i = first_parameter_equality(bset);
7888 if (i == bset->n_eq)
7889 return base_compute_divs(bset);
7891 nparam = isl_basic_set_dim(bset, isl_dim_param);
7892 if (nparam < 0)
7893 return isl_set_from_basic_set(isl_basic_set_free(bset));
7894 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7895 0, 1 + nparam);
7896 eq = isl_mat_cow(eq);
7897 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7898 if (T && T->n_col == 0) {
7899 isl_mat_free(T);
7900 isl_mat_free(T2);
7901 isl_mat_free(eq);
7902 bset = isl_basic_set_set_to_empty(bset);
7903 return isl_set_from_basic_set(bset);
7905 bset = basic_set_parameter_preimage(bset, T);
7907 i = first_parameter_equality(bset);
7908 if (!bset)
7909 set = NULL;
7910 else if (i == bset->n_eq)
7911 set = base_compute_divs(bset);
7912 else
7913 set = parameter_compute_divs(bset);
7914 set = set_parameter_preimage(set, T2);
7915 set = set_append_equalities(set, eq);
7916 return set;
7919 /* Insert the divs from "ls" before those of "bmap".
7921 * The number of columns is not changed, which means that the last
7922 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7923 * The caller is responsible for removing the same number of dimensions
7924 * from the space of "bmap".
7926 static __isl_give isl_basic_map *insert_divs_from_local_space(
7927 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7929 int i;
7930 isl_size n_div;
7931 int old_n_div;
7933 n_div = isl_local_space_dim(ls, isl_dim_div);
7934 if (n_div < 0)
7935 return isl_basic_map_free(bmap);
7936 if (n_div == 0)
7937 return bmap;
7939 old_n_div = bmap->n_div;
7940 bmap = insert_div_rows(bmap, n_div);
7941 if (!bmap)
7942 return NULL;
7944 for (i = 0; i < n_div; ++i) {
7945 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7946 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7949 return bmap;
7952 /* Replace the space of "bmap" by the space and divs of "ls".
7954 * If "ls" has any divs, then we simplify the result since we may
7955 * have discovered some additional equalities that could simplify
7956 * the div expressions.
7958 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7959 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7961 isl_size n_div;
7963 bmap = isl_basic_map_cow(bmap);
7964 n_div = isl_local_space_dim(ls, isl_dim_div);
7965 if (!bmap || n_div < 0)
7966 goto error;
7968 bmap = insert_divs_from_local_space(bmap, ls);
7969 if (!bmap)
7970 goto error;
7972 isl_space_free(bmap->dim);
7973 bmap->dim = isl_local_space_get_space(ls);
7974 if (!bmap->dim)
7975 goto error;
7977 isl_local_space_free(ls);
7978 if (n_div > 0)
7979 bmap = isl_basic_map_simplify(bmap);
7980 bmap = isl_basic_map_finalize(bmap);
7981 return bmap;
7982 error:
7983 isl_basic_map_free(bmap);
7984 isl_local_space_free(ls);
7985 return NULL;
7988 /* Replace the space of "map" by the space and divs of "ls".
7990 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7991 __isl_take isl_local_space *ls)
7993 int i;
7995 map = isl_map_cow(map);
7996 if (!map || !ls)
7997 goto error;
7999 for (i = 0; i < map->n; ++i) {
8000 map->p[i] = basic_replace_space_by_local_space(map->p[i],
8001 isl_local_space_copy(ls));
8002 if (!map->p[i])
8003 goto error;
8005 isl_space_free(isl_map_take_space(map));
8006 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
8008 isl_local_space_free(ls);
8009 return map;
8010 error:
8011 isl_local_space_free(ls);
8012 isl_map_free(map);
8013 return NULL;
8016 /* Compute an explicit representation for the existentially
8017 * quantified variables for which do not know any explicit representation yet.
8019 * We first sort the existentially quantified variables so that the
8020 * existentially quantified variables for which we already have an explicit
8021 * representation are placed before those for which we do not.
8022 * The input dimensions, the output dimensions and the existentially
8023 * quantified variables for which we already have an explicit
8024 * representation are then turned into parameters.
8025 * compute_divs returns a map with the same parameters and
8026 * no input or output dimensions and the dimension specification
8027 * is reset to that of the input, including the existentially quantified
8028 * variables for which we already had an explicit representation.
8030 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
8032 struct isl_basic_set *bset;
8033 struct isl_set *set;
8034 struct isl_map *map;
8035 isl_space *space;
8036 isl_local_space *ls;
8037 isl_size nparam;
8038 isl_size n_in;
8039 isl_size n_out;
8040 int n_known;
8041 int i;
8043 bmap = isl_basic_map_sort_divs(bmap);
8044 bmap = isl_basic_map_cow(bmap);
8045 if (!bmap)
8046 return NULL;
8048 n_known = isl_basic_map_first_unknown_div(bmap);
8049 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8050 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8051 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8052 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8053 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8055 space = isl_space_set_alloc(bmap->ctx,
8056 nparam + n_in + n_out + n_known, 0);
8057 if (!space)
8058 goto error;
8060 ls = isl_basic_map_get_local_space(bmap);
8061 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8062 n_known, bmap->n_div - n_known);
8063 if (n_known > 0) {
8064 for (i = n_known; i < bmap->n_div; ++i)
8065 swap_div(bmap, i - n_known, i);
8066 bmap->n_div -= n_known;
8067 bmap->extra -= n_known;
8069 bmap = isl_basic_map_reset_space(bmap, space);
8070 bset = bset_from_bmap(bmap);
8072 set = parameter_compute_divs(bset);
8073 map = set_to_map(set);
8074 map = replace_space_by_local_space(map, ls);
8076 return map;
8077 error:
8078 isl_basic_map_free(bmap);
8079 return NULL;
8082 /* Is the integer division at position "div" of "bmap" integral?
8083 * That is, does it have denominator 1?
8085 isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
8087 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8088 return isl_bool_error;
8089 return isl_int_is_one(bmap->div[div][0]);
8092 /* Remove the explicit representation of local variable "div",
8093 * if there is any.
8095 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8096 __isl_take isl_basic_map *bmap, int div)
8098 isl_bool unknown;
8100 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8101 if (unknown < 0)
8102 return isl_basic_map_free(bmap);
8103 if (unknown)
8104 return bmap;
8106 bmap = isl_basic_map_cow(bmap);
8107 if (!bmap)
8108 return NULL;
8109 isl_int_set_si(bmap->div[div][0], 0);
8110 return bmap;
8113 /* Is local variable "div" of "bmap" marked as not having an explicit
8114 * representation?
8115 * Note that even if "div" is not marked in this way and therefore
8116 * has an explicit representation, this representation may still
8117 * depend (indirectly) on other local variables that do not
8118 * have an explicit representation.
8120 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8121 int div)
8123 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8124 return isl_bool_error;
8125 return isl_int_is_zero(bmap->div[div][0]);
8128 /* Return the position of the first local variable that does not
8129 * have an explicit representation.
8130 * Return the total number of local variables if they all have
8131 * an explicit representation.
8132 * Return -1 on error.
8134 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8136 int i;
8138 if (!bmap)
8139 return -1;
8141 for (i = 0; i < bmap->n_div; ++i) {
8142 if (!isl_basic_map_div_is_known(bmap, i))
8143 return i;
8145 return bmap->n_div;
8148 /* Return the position of the first local variable that does not
8149 * have an explicit representation.
8150 * Return the total number of local variables if they all have
8151 * an explicit representation.
8152 * Return -1 on error.
8154 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8156 return isl_basic_map_first_unknown_div(bset);
8159 /* Does "bmap" have an explicit representation for all local variables?
8161 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8163 int first;
8164 isl_size n;
8166 n = isl_basic_map_dim(bmap, isl_dim_div);
8167 first = isl_basic_map_first_unknown_div(bmap);
8168 if (n < 0 || first < 0)
8169 return isl_bool_error;
8170 return first == n;
8173 /* Do all basic maps in "map" have an explicit representation
8174 * for all local variables?
8176 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8178 int i;
8180 if (!map)
8181 return isl_bool_error;
8183 for (i = 0; i < map->n; ++i) {
8184 int known = isl_basic_map_divs_known(map->p[i]);
8185 if (known <= 0)
8186 return known;
8189 return isl_bool_true;
8192 /* If bmap contains any unknown divs, then compute explicit
8193 * expressions for them. However, this computation may be
8194 * quite expensive, so first try to remove divs that aren't
8195 * strictly needed.
8197 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8199 int known;
8200 struct isl_map *map;
8202 known = isl_basic_map_divs_known(bmap);
8203 if (known < 0)
8204 goto error;
8205 if (known)
8206 return isl_map_from_basic_map(bmap);
8208 bmap = isl_basic_map_drop_redundant_divs(bmap);
8210 known = isl_basic_map_divs_known(bmap);
8211 if (known < 0)
8212 goto error;
8213 if (known)
8214 return isl_map_from_basic_map(bmap);
8216 map = compute_divs(bmap);
8217 return map;
8218 error:
8219 isl_basic_map_free(bmap);
8220 return NULL;
8223 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8225 int i;
8226 int known;
8227 struct isl_map *res;
8229 if (!map)
8230 return NULL;
8231 if (map->n == 0)
8232 return map;
8234 known = isl_map_divs_known(map);
8235 if (known < 0) {
8236 isl_map_free(map);
8237 return NULL;
8239 if (known)
8240 return map;
8242 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8243 for (i = 1 ; i < map->n; ++i) {
8244 struct isl_map *r2;
8245 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8246 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8247 res = isl_map_union_disjoint(res, r2);
8248 else
8249 res = isl_map_union(res, r2);
8251 isl_map_free(map);
8253 return res;
8256 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8258 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8261 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8263 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8266 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8268 isl_space *space;
8269 isl_size n_out;
8271 n_out = isl_map_dim(map, isl_dim_out);
8272 if (n_out < 0)
8273 return set_from_map(isl_map_free(map));
8274 space = isl_space_domain(isl_map_get_space(map));
8276 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8278 return set_from_map(isl_map_reset_space(map, space));
8281 /* Return the union of "map1" and "map2", where we assume for now that
8282 * "map1" and "map2" are disjoint. Note that the basic maps inside
8283 * "map1" or "map2" may not be disjoint from each other.
8284 * Also note that this function is also called from isl_map_union,
8285 * which takes care of handling the situation where "map1" and "map2"
8286 * may not be disjoint.
8288 * If one of the inputs is empty, we can simply return the other input.
8289 * Similarly, if one of the inputs is universal, then it is equal to the union.
8291 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8292 __isl_take isl_map *map2)
8294 int i;
8295 unsigned flags = 0;
8296 struct isl_map *map = NULL;
8297 int is_universe;
8299 if (isl_map_check_equal_space(map1, map2) < 0)
8300 goto error;
8302 if (map1->n == 0) {
8303 isl_map_free(map1);
8304 return map2;
8306 if (map2->n == 0) {
8307 isl_map_free(map2);
8308 return map1;
8311 is_universe = isl_map_plain_is_universe(map1);
8312 if (is_universe < 0)
8313 goto error;
8314 if (is_universe) {
8315 isl_map_free(map2);
8316 return map1;
8319 is_universe = isl_map_plain_is_universe(map2);
8320 if (is_universe < 0)
8321 goto error;
8322 if (is_universe) {
8323 isl_map_free(map1);
8324 return map2;
8327 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8328 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8329 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8331 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8332 map1->n + map2->n, flags);
8333 if (!map)
8334 goto error;
8335 for (i = 0; i < map1->n; ++i) {
8336 map = isl_map_add_basic_map(map,
8337 isl_basic_map_copy(map1->p[i]));
8338 if (!map)
8339 goto error;
8341 for (i = 0; i < map2->n; ++i) {
8342 map = isl_map_add_basic_map(map,
8343 isl_basic_map_copy(map2->p[i]));
8344 if (!map)
8345 goto error;
8347 isl_map_free(map1);
8348 isl_map_free(map2);
8349 return map;
8350 error:
8351 isl_map_free(map);
8352 isl_map_free(map1);
8353 isl_map_free(map2);
8354 return NULL;
8357 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8358 * guaranteed to be disjoint by the caller.
8360 * Note that this functions is called from within isl_map_make_disjoint,
8361 * so we have to be careful not to touch the constraints of the inputs
8362 * in any way.
8364 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8365 __isl_take isl_map *map2)
8367 isl_map_align_params_bin(&map1, &map2);
8368 return map_union_disjoint(map1, map2);
8371 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8372 * not be disjoint.
8374 * We currently simply call map_union_disjoint, the internal operation
8375 * of which does not really depend on the inputs being disjoint.
8376 * If the result contains more than one basic map, then we clear
8377 * the disjoint flag since the result may contain basic maps from
8378 * both inputs and these are not guaranteed to be disjoint.
8380 * As a special case, if "map1" and "map2" are obviously equal,
8381 * then we simply return "map1".
8383 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8384 __isl_take isl_map *map2)
8386 int equal;
8388 if (isl_map_align_params_bin(&map1, &map2) < 0)
8389 goto error;
8391 equal = isl_map_plain_is_equal(map1, map2);
8392 if (equal < 0)
8393 goto error;
8394 if (equal) {
8395 isl_map_free(map2);
8396 return map1;
8399 map1 = map_union_disjoint(map1, map2);
8400 if (!map1)
8401 return NULL;
8402 if (map1->n > 1)
8403 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8404 return map1;
8405 error:
8406 isl_map_free(map1);
8407 isl_map_free(map2);
8408 return NULL;
8411 __isl_give isl_set *isl_set_union_disjoint(
8412 __isl_take isl_set *set1, __isl_take isl_set *set2)
8414 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8415 set_to_map(set2)));
8418 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8419 __isl_take isl_set *set2)
8421 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8424 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8425 * the results in a map living in "space".
8427 * "map" and "set" are assumed to be compatible and non-NULL.
8429 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8430 __isl_take isl_space *space, __isl_take isl_set *set,
8431 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8432 __isl_take isl_basic_set *bset))
8434 unsigned flags = 0;
8435 struct isl_map *result;
8436 int i, j;
8438 if (isl_set_plain_is_universe(set)) {
8439 isl_set_free(set);
8440 return isl_map_reset_equal_dim_space(map, space);
8443 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8444 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8445 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8447 result = isl_map_alloc_space(space, map->n * set->n, flags);
8448 for (i = 0; result && i < map->n; ++i)
8449 for (j = 0; j < set->n; ++j) {
8450 result = isl_map_add_basic_map(result,
8451 fn(isl_basic_map_copy(map->p[i]),
8452 isl_basic_set_copy(set->p[j])));
8453 if (!result)
8454 break;
8457 isl_map_free(map);
8458 isl_set_free(set);
8459 return result;
8462 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8463 __isl_take isl_set *set)
8465 isl_bool ok;
8466 isl_space *space;
8468 isl_map_align_params_set(&map, &set);
8469 ok = isl_map_compatible_range(map, set);
8470 if (ok < 0)
8471 goto error;
8472 if (!ok)
8473 isl_die(set->ctx, isl_error_invalid,
8474 "incompatible spaces", goto error);
8476 space = isl_map_get_space(map);
8477 return map_intersect_set(map, space, set,
8478 &isl_basic_map_intersect_range);
8479 error:
8480 isl_map_free(map);
8481 isl_set_free(set);
8482 return NULL;
8485 /* Intersect the domain of "map" with "set".
8487 * If the domain dimensions of "map" do not have any identifiers,
8488 * then copy them over from "set".
8490 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8491 __isl_take isl_set *set)
8493 isl_bool ok;
8494 isl_space *space;
8496 isl_map_align_params_set(&map, &set);
8497 ok = isl_map_compatible_domain(map, set);
8498 if (ok < 0)
8499 goto error;
8500 if (!ok)
8501 isl_die(set->ctx, isl_error_invalid,
8502 "incompatible spaces", goto error);
8504 space = isl_map_get_space(map);
8505 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8506 isl_set_peek_space(set), isl_dim_set);
8507 return map_intersect_set(map, space, set,
8508 &isl_basic_map_intersect_domain);
8509 error:
8510 isl_map_free(map);
8511 isl_set_free(set);
8512 return NULL;
8515 #undef BASE
8516 #define BASE map
8517 static
8518 #include "isl_copy_tuple_id_templ.c"
8520 /* Data structure that specifies how isl_map_intersect_factor
8521 * should operate.
8523 * "preserve_type" is the tuple where the factor differs from
8524 * the input map and of which the identifiers needs
8525 * to be preserved explicitly.
8526 * "other_factor" is used to extract the space of the other factor
8527 * from the space of the product ("map").
8528 * "product" is used to combine the given factor and a universe map
8529 * in the space returned by "other_factor" to produce a map
8530 * that lives in the same space as the input map.
8532 struct isl_intersect_factor_control {
8533 enum isl_dim_type preserve_type;
8534 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8535 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8536 __isl_take isl_map *other);
8539 /* Given a map "map" in some product space and a map "factor"
8540 * living in some factor space, return the intersection.
8542 * After aligning the parameters,
8543 * the map "factor" is first extended to a map living in the same space
8544 * as "map" and then a regular intersection is computed.
8546 * Note that the extension is computed as a product, which is anonymous
8547 * by default. If "map" has an identifier on the corresponding tuple,
8548 * then this identifier needs to be set on the product
8549 * before the intersection is computed.
8551 static __isl_give isl_map *isl_map_intersect_factor(
8552 __isl_take isl_map *map, __isl_take isl_map *factor,
8553 struct isl_intersect_factor_control *control)
8555 isl_bool equal;
8556 isl_space *space;
8557 isl_map *other, *product;
8559 equal = isl_map_has_equal_params(map, factor);
8560 if (equal < 0)
8561 goto error;
8562 if (!equal) {
8563 map = isl_map_align_params(map, isl_map_get_space(factor));
8564 factor = isl_map_align_params(factor, isl_map_get_space(map));
8567 space = isl_map_get_space(map);
8568 other = isl_map_universe(control->other_factor(space));
8569 product = control->product(factor, other);
8571 space = isl_map_peek_space(map);
8572 product = isl_map_copy_tuple_id(product, control->preserve_type,
8573 space, control->preserve_type);
8574 return map_intersect(map, product);
8575 error:
8576 isl_map_free(map);
8577 isl_map_free(factor);
8578 return NULL;
8581 /* Return the domain product of "map2" and "map1".
8583 static __isl_give isl_map *isl_map_reverse_domain_product(
8584 __isl_take isl_map *map1, __isl_take isl_map *map2)
8586 return isl_map_domain_product(map2, map1);
8589 /* Return the range product of "map2" and "map1".
8591 static __isl_give isl_map *isl_map_reverse_range_product(
8592 __isl_take isl_map *map1, __isl_take isl_map *map2)
8594 return isl_map_range_product(map2, map1);
8597 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8598 * in the space A -> C, return the intersection.
8600 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8601 __isl_take isl_map *map, __isl_take isl_map *factor)
8603 struct isl_intersect_factor_control control = {
8604 .preserve_type = isl_dim_in,
8605 .other_factor = isl_space_domain_factor_range,
8606 .product = isl_map_domain_product,
8609 return isl_map_intersect_factor(map, factor, &control);
8612 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8613 * in the space B -> C, return the intersection.
8615 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8616 __isl_take isl_map *map, __isl_take isl_map *factor)
8618 struct isl_intersect_factor_control control = {
8619 .preserve_type = isl_dim_in,
8620 .other_factor = isl_space_domain_factor_domain,
8621 .product = isl_map_reverse_domain_product,
8624 return isl_map_intersect_factor(map, factor, &control);
8627 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8628 * in the space A -> B, return the intersection.
8630 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8631 __isl_take isl_map *map, __isl_take isl_map *factor)
8633 struct isl_intersect_factor_control control = {
8634 .preserve_type = isl_dim_out,
8635 .other_factor = isl_space_range_factor_range,
8636 .product = isl_map_range_product,
8639 return isl_map_intersect_factor(map, factor, &control);
8642 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8643 * in the space A -> C, return the intersection.
8645 __isl_give isl_map *isl_map_intersect_range_factor_range(
8646 __isl_take isl_map *map, __isl_take isl_map *factor)
8648 struct isl_intersect_factor_control control = {
8649 .preserve_type = isl_dim_out,
8650 .other_factor = isl_space_range_factor_domain,
8651 .product = isl_map_reverse_range_product,
8654 return isl_map_intersect_factor(map, factor, &control);
8657 /* Given a set "set" in a space [A -> B] and a set "domain"
8658 * in the space A, return the intersection.
8660 * The set "domain" is first extended to a set living in the space
8661 * [A -> B] and then a regular intersection is computed.
8663 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8664 __isl_take isl_set *domain)
8666 struct isl_intersect_factor_control control = {
8667 .preserve_type = isl_dim_set,
8668 .other_factor = isl_space_factor_range,
8669 .product = isl_map_range_product,
8672 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8673 set_to_map(domain), &control));
8676 /* Given a set "set" in a space [A -> B] and a set "range"
8677 * in the space B, return the intersection.
8679 * The set "range" is first extended to a set living in the space
8680 * [A -> B] and then a regular intersection is computed.
8682 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8683 __isl_take isl_set *range)
8685 struct isl_intersect_factor_control control = {
8686 .preserve_type = isl_dim_set,
8687 .other_factor = isl_space_factor_domain,
8688 .product = isl_map_reverse_range_product,
8691 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8692 set_to_map(range), &control));
8695 #undef BASE
8696 #define BASE set
8697 static
8698 #include "isl_copy_tuple_id_templ.c"
8700 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8701 * in the space A, return the intersection.
8703 * The set "domain" is extended to a set living in the space [A -> B] and
8704 * the domain of "map" is intersected with this set.
8706 * If "map" has an identifier on the domain tuple,
8707 * then this identifier needs to be set on this product
8708 * before the intersection is computed.
8710 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8711 __isl_take isl_map *map, __isl_take isl_set *domain)
8713 isl_space *space;
8714 isl_set *factor;
8716 isl_map_align_params_set(&map, &domain);
8717 space = isl_map_get_space(map);
8718 space = isl_space_domain_wrapped_range(space);
8719 factor = isl_set_universe(space);
8720 domain = isl_set_product(domain, factor);
8721 space = isl_map_peek_space(map);
8722 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
8723 return isl_map_intersect_domain(map, domain);
8726 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8727 * in the space B, return the intersection.
8729 * The set "domain" is extended to a set living in the space [B -> C] and
8730 * the range of "map" is intersected with this set.
8732 * If "map" has an identifier on the range tuple,
8733 * then this identifier needs to be set on this product
8734 * before the intersection is computed.
8736 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8737 __isl_take isl_map *map, __isl_take isl_set *domain)
8739 isl_space *space;
8740 isl_set *factor;
8742 isl_map_align_params_set(&map, &domain);
8743 space = isl_map_get_space(map);
8744 space = isl_space_range_wrapped_range(space);
8745 factor = isl_set_universe(space);
8746 domain = isl_set_product(domain, factor);
8747 space = isl_map_peek_space(map);
8748 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
8749 return isl_map_intersect_range(map, domain);
8752 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8753 __isl_take isl_map *map2)
8755 if (isl_map_align_params_bin(&map1, &map2) < 0)
8756 goto error;
8757 map1 = isl_map_reverse(map1);
8758 map1 = isl_map_apply_range(map1, map2);
8759 return isl_map_reverse(map1);
8760 error:
8761 isl_map_free(map1);
8762 isl_map_free(map2);
8763 return NULL;
8766 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8767 __isl_take isl_map *map2)
8769 isl_space *space;
8770 struct isl_map *result;
8771 int i, j;
8773 if (isl_map_align_params_bin(&map1, &map2) < 0)
8774 goto error;
8776 space = isl_space_join(isl_space_copy(map1->dim),
8777 isl_space_copy(map2->dim));
8779 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8780 if (!result)
8781 goto error;
8782 for (i = 0; i < map1->n; ++i)
8783 for (j = 0; j < map2->n; ++j) {
8784 result = isl_map_add_basic_map(result,
8785 isl_basic_map_apply_range(
8786 isl_basic_map_copy(map1->p[i]),
8787 isl_basic_map_copy(map2->p[j])));
8788 if (!result)
8789 goto error;
8791 isl_map_free(map1);
8792 isl_map_free(map2);
8793 if (result && result->n <= 1)
8794 ISL_F_SET(result, ISL_MAP_DISJOINT);
8795 return result;
8796 error:
8797 isl_map_free(map1);
8798 isl_map_free(map2);
8799 return NULL;
8802 /* Is "bmap" a transformation, i.e.,
8803 * does it relate elements from the same space.
8805 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8807 isl_space *space;
8809 space = isl_basic_map_peek_space(bmap);
8810 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8813 /* Check that "bmap" is a transformation, i.e.,
8814 * that it relates elements from the same space.
8816 static isl_stat isl_basic_map_check_transformation(
8817 __isl_keep isl_basic_map *bmap)
8819 isl_bool equal;
8821 equal = isl_basic_map_is_transformation(bmap);
8822 if (equal < 0)
8823 return isl_stat_error;
8824 if (!equal)
8825 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8826 "domain and range don't match", return isl_stat_error);
8827 return isl_stat_ok;
8831 * returns range - domain
8833 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8835 isl_space *target_space;
8836 struct isl_basic_set *bset;
8837 isl_size dim;
8838 isl_size nparam;
8839 isl_size total;
8840 int i;
8842 if (isl_basic_map_check_transformation(bmap) < 0)
8843 return isl_basic_map_free(bmap);
8844 dim = isl_basic_map_dim(bmap, isl_dim_in);
8845 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8846 if (dim < 0 || nparam < 0)
8847 goto error;
8848 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8849 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8850 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8851 total = isl_basic_map_dim(bmap, isl_dim_all);
8852 if (total < 0)
8853 bmap = isl_basic_map_free(bmap);
8854 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8855 for (i = 0; i < dim; ++i) {
8856 int j = isl_basic_map_alloc_equality(bmap);
8857 if (j < 0) {
8858 bmap = isl_basic_map_free(bmap);
8859 break;
8861 isl_seq_clr(bmap->eq[j], 1 + total);
8862 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8863 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8864 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8866 bset = isl_basic_map_domain(bmap);
8867 bset = isl_basic_set_reset_space(bset, target_space);
8868 return bset;
8869 error:
8870 isl_basic_map_free(bmap);
8871 return NULL;
8874 /* Is the tuple of type "type1" of "map" the same as
8875 * the tuple of type "type2" of "space"?
8877 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8878 enum isl_dim_type type1, __isl_keep isl_space *space,
8879 enum isl_dim_type type2)
8881 isl_space *map_space;
8883 map_space = isl_map_peek_space(map);
8884 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8887 /* Is the tuple of type "type1" of "map1" the same as
8888 * the tuple of type "type2" of "map2"?
8890 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8891 enum isl_dim_type type1, __isl_keep isl_map *map2,
8892 enum isl_dim_type type2)
8894 isl_space *space1, *space2;
8896 space1 = isl_map_peek_space(map1);
8897 space2 = isl_map_peek_space(map2);
8898 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8901 /* Is the space of "obj" equal to "space", ignoring parameters?
8903 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8904 __isl_keep isl_space *space)
8906 isl_space *map_space;
8908 map_space = isl_map_peek_space(map);
8909 return isl_space_has_equal_tuples(map_space, space);
8912 /* Check that "map" is a transformation, i.e.,
8913 * that it relates elements from the same space.
8915 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8917 isl_bool equal;
8919 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8920 if (equal < 0)
8921 return isl_stat_error;
8922 if (!equal)
8923 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8924 "domain and range don't match", return isl_stat_error);
8925 return isl_stat_ok;
8929 * returns range - domain
8931 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8933 int i;
8934 isl_space *space;
8935 struct isl_set *result;
8937 if (isl_map_check_transformation(map) < 0)
8938 goto error;
8939 space = isl_map_get_space(map);
8940 space = isl_space_domain(space);
8941 result = isl_set_alloc_space(space, map->n, 0);
8942 if (!result)
8943 goto error;
8944 for (i = 0; i < map->n; ++i)
8945 result = isl_set_add_basic_set(result,
8946 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8947 isl_map_free(map);
8948 return result;
8949 error:
8950 isl_map_free(map);
8951 return NULL;
8955 * returns [domain -> range] -> range - domain
8957 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8958 __isl_take isl_basic_map *bmap)
8960 int i, k;
8961 isl_space *space;
8962 isl_basic_map *domain;
8963 isl_size nparam, n;
8964 isl_size total;
8966 if (isl_basic_map_check_transformation(bmap) < 0)
8967 return isl_basic_map_free(bmap);
8969 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8970 n = isl_basic_map_dim(bmap, isl_dim_in);
8971 if (nparam < 0 || n < 0)
8972 return isl_basic_map_free(bmap);
8974 space = isl_basic_map_get_space(bmap);
8975 space = isl_space_from_range(isl_space_domain(space));
8976 domain = isl_basic_map_universe(space);
8978 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8979 bmap = isl_basic_map_apply_range(bmap, domain);
8980 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8982 total = isl_basic_map_dim(bmap, isl_dim_all);
8983 if (total < 0)
8984 return isl_basic_map_free(bmap);
8986 for (i = 0; i < n; ++i) {
8987 k = isl_basic_map_alloc_equality(bmap);
8988 if (k < 0)
8989 goto error;
8990 isl_seq_clr(bmap->eq[k], 1 + total);
8991 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8992 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8993 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8996 bmap = isl_basic_map_gauss(bmap, NULL);
8997 return isl_basic_map_finalize(bmap);
8998 error:
8999 isl_basic_map_free(bmap);
9000 return NULL;
9004 * returns [domain -> range] -> range - domain
9006 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
9008 if (isl_map_check_transformation(map) < 0)
9009 return isl_map_free(map);
9011 return isl_map_transform(map, &isl_space_range_map,
9012 &isl_basic_map_deltas_map);
9015 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9017 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
9019 isl_space *space;
9020 isl_map *map;
9022 space = isl_space_map_from_set(isl_set_get_space(deltas));
9023 map = isl_map_deltas_map(isl_map_universe(space));
9024 map = isl_map_intersect_range(map, deltas);
9026 return isl_set_unwrap(isl_map_domain(map));
9029 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
9031 isl_size n_in, n_out;
9033 n_in = isl_space_dim(space, isl_dim_in);
9034 n_out = isl_space_dim(space, isl_dim_out);
9035 if (n_in < 0 || n_out < 0)
9036 goto error;
9037 if (n_in != n_out)
9038 isl_die(space->ctx, isl_error_invalid,
9039 "number of input and output dimensions needs to be "
9040 "the same", goto error);
9041 return isl_basic_map_equal(space, n_in);
9042 error:
9043 isl_space_free(space);
9044 return NULL;
9047 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
9049 return isl_map_from_basic_map(isl_basic_map_identity(space));
9052 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
9054 isl_space *space = isl_set_get_space(set);
9055 isl_map *id;
9056 id = isl_map_identity(isl_space_map_from_set(space));
9057 return isl_map_intersect_range(id, set);
9060 /* Construct a basic set with all set dimensions having only non-negative
9061 * values.
9063 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9064 __isl_take isl_space *space)
9066 int i;
9067 isl_size nparam;
9068 isl_size dim;
9069 isl_size total;
9070 struct isl_basic_set *bset;
9072 nparam = isl_space_dim(space, isl_dim_param);
9073 dim = isl_space_dim(space, isl_dim_set);
9074 total = isl_space_dim(space, isl_dim_all);
9075 if (nparam < 0 || dim < 0 || total < 0)
9076 space = isl_space_free(space);
9077 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9078 if (!bset)
9079 return NULL;
9080 for (i = 0; i < dim; ++i) {
9081 int k = isl_basic_set_alloc_inequality(bset);
9082 if (k < 0)
9083 goto error;
9084 isl_seq_clr(bset->ineq[k], 1 + total);
9085 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9087 return bset;
9088 error:
9089 isl_basic_set_free(bset);
9090 return NULL;
9093 /* Construct the half-space x_pos >= 0.
9095 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9096 int pos)
9098 int k;
9099 isl_size total;
9100 isl_basic_set *nonneg;
9102 total = isl_space_dim(space, isl_dim_all);
9103 if (total < 0)
9104 space = isl_space_free(space);
9105 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9106 k = isl_basic_set_alloc_inequality(nonneg);
9107 if (k < 0)
9108 goto error;
9109 isl_seq_clr(nonneg->ineq[k], 1 + total);
9110 isl_int_set_si(nonneg->ineq[k][pos], 1);
9112 return isl_basic_set_finalize(nonneg);
9113 error:
9114 isl_basic_set_free(nonneg);
9115 return NULL;
9118 /* Construct the half-space x_pos <= -1.
9120 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9121 int pos)
9123 int k;
9124 isl_size total;
9125 isl_basic_set *neg;
9127 total = isl_space_dim(space, isl_dim_all);
9128 if (total < 0)
9129 space = isl_space_free(space);
9130 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9131 k = isl_basic_set_alloc_inequality(neg);
9132 if (k < 0)
9133 goto error;
9134 isl_seq_clr(neg->ineq[k], 1 + total);
9135 isl_int_set_si(neg->ineq[k][0], -1);
9136 isl_int_set_si(neg->ineq[k][pos], -1);
9138 return isl_basic_set_finalize(neg);
9139 error:
9140 isl_basic_set_free(neg);
9141 return NULL;
9144 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9145 enum isl_dim_type type, unsigned first, unsigned n)
9147 int i;
9148 unsigned offset;
9149 isl_basic_set *nonneg;
9150 isl_basic_set *neg;
9152 if (n == 0)
9153 return set;
9155 if (isl_set_check_range(set, type, first, n) < 0)
9156 return isl_set_free(set);
9158 offset = pos(set->dim, type);
9159 for (i = 0; i < n; ++i) {
9160 nonneg = nonneg_halfspace(isl_set_get_space(set),
9161 offset + first + i);
9162 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9164 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9167 return set;
9170 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9171 int len,
9172 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9173 void *user)
9175 isl_set *half;
9177 if (!set)
9178 return isl_stat_error;
9179 if (isl_set_plain_is_empty(set)) {
9180 isl_set_free(set);
9181 return isl_stat_ok;
9183 if (first == len)
9184 return fn(set, signs, user);
9186 signs[first] = 1;
9187 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9188 1 + first));
9189 half = isl_set_intersect(half, isl_set_copy(set));
9190 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9191 goto error;
9193 signs[first] = -1;
9194 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9195 1 + first));
9196 half = isl_set_intersect(half, set);
9197 return foreach_orthant(half, signs, first + 1, len, fn, user);
9198 error:
9199 isl_set_free(set);
9200 return isl_stat_error;
9203 /* Call "fn" on the intersections of "set" with each of the orthants
9204 * (except for obviously empty intersections). The orthant is identified
9205 * by the signs array, with each entry having value 1 or -1 according
9206 * to the sign of the corresponding variable.
9208 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9209 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9210 void *user)
9212 isl_size nparam;
9213 isl_size nvar;
9214 int *signs;
9215 isl_stat r;
9217 if (!set)
9218 return isl_stat_error;
9219 if (isl_set_plain_is_empty(set))
9220 return isl_stat_ok;
9222 nparam = isl_set_dim(set, isl_dim_param);
9223 nvar = isl_set_dim(set, isl_dim_set);
9224 if (nparam < 0 || nvar < 0)
9225 return isl_stat_error;
9227 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9229 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9230 fn, user);
9232 free(signs);
9234 return r;
9237 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9239 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9242 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9243 __isl_keep isl_basic_map *bmap2)
9245 isl_bool is_subset;
9246 struct isl_map *map1;
9247 struct isl_map *map2;
9249 if (!bmap1 || !bmap2)
9250 return isl_bool_error;
9252 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9253 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9255 is_subset = isl_map_is_subset(map1, map2);
9257 isl_map_free(map1);
9258 isl_map_free(map2);
9260 return is_subset;
9263 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9264 __isl_keep isl_basic_set *bset2)
9266 return isl_basic_map_is_subset(bset1, bset2);
9269 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9270 __isl_keep isl_basic_map *bmap2)
9272 isl_bool is_subset;
9274 if (!bmap1 || !bmap2)
9275 return isl_bool_error;
9276 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9277 if (is_subset != isl_bool_true)
9278 return is_subset;
9279 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9280 return is_subset;
9283 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9284 __isl_keep isl_basic_set *bset2)
9286 return isl_basic_map_is_equal(
9287 bset_to_bmap(bset1), bset_to_bmap(bset2));
9290 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9292 int i;
9293 int is_empty;
9295 if (!map)
9296 return isl_bool_error;
9297 for (i = 0; i < map->n; ++i) {
9298 is_empty = isl_basic_map_is_empty(map->p[i]);
9299 if (is_empty < 0)
9300 return isl_bool_error;
9301 if (!is_empty)
9302 return isl_bool_false;
9304 return isl_bool_true;
9307 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9309 return map ? map->n == 0 : isl_bool_error;
9312 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9314 return set ? set->n == 0 : isl_bool_error;
9317 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9319 return isl_map_is_empty(set_to_map(set));
9322 #undef TYPE
9323 #define TYPE isl_basic_map
9325 static
9326 #include "isl_type_has_equal_space_bin_templ.c"
9327 #include "isl_type_check_equal_space_templ.c"
9329 /* Check that "bset1" and "bset2" live in the same space,
9330 * reporting an error if they do not.
9332 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9333 __isl_keep isl_basic_set *bset2)
9335 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9336 bset_to_bmap(bset1));
9339 #undef TYPE
9340 #define TYPE isl_map
9342 #include "isl_type_has_equal_space_bin_templ.c"
9343 #include "isl_type_check_equal_space_templ.c"
9344 #include "isl_type_has_space_templ.c"
9346 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9347 __isl_keep isl_set *set2)
9349 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9352 #undef TYPE1
9353 #define TYPE1 isl_map
9354 #undef TYPE2
9355 #define TYPE2 isl_basic_map
9356 #undef TYPE_PAIR
9357 #define TYPE_PAIR isl_map_basic_map
9359 static
9360 #include "isl_type_has_equal_space_templ.c"
9361 #include "isl_type_check_equal_space_templ.c"
9363 /* Check that "set" and "bset" live in the same space,
9364 * reporting an error if they do not.
9366 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9367 __isl_keep isl_basic_set *bset)
9369 return isl_map_basic_map_check_equal_space(set_to_map(set),
9370 bset_to_bmap(bset));
9373 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9375 isl_bool is_subset;
9377 if (!map1 || !map2)
9378 return isl_bool_error;
9379 is_subset = isl_map_is_subset(map1, map2);
9380 if (is_subset != isl_bool_true)
9381 return is_subset;
9382 is_subset = isl_map_is_subset(map2, map1);
9383 return is_subset;
9386 /* Is "map1" equal to "map2"?
9388 * First check if they are obviously equal.
9389 * If not, then perform a more detailed analysis.
9391 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9393 isl_bool equal;
9395 equal = isl_map_plain_is_equal(map1, map2);
9396 if (equal < 0 || equal)
9397 return equal;
9398 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9401 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9402 __isl_keep isl_basic_map *bmap2)
9404 isl_bool is_subset;
9406 if (!bmap1 || !bmap2)
9407 return isl_bool_error;
9408 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9409 if (is_subset != isl_bool_true)
9410 return is_subset;
9411 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9412 return isl_bool_not(is_subset);
9415 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9416 __isl_keep isl_map *map2)
9418 isl_bool is_subset;
9420 if (!map1 || !map2)
9421 return isl_bool_error;
9422 is_subset = isl_map_is_subset(map1, map2);
9423 if (is_subset != isl_bool_true)
9424 return is_subset;
9425 is_subset = isl_map_is_subset(map2, map1);
9426 return isl_bool_not(is_subset);
9429 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9430 __isl_keep isl_set *set2)
9432 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9435 /* Is "bmap" obviously equal to the universe with the same space?
9437 * That is, does it not have any constraints?
9439 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9441 if (!bmap)
9442 return isl_bool_error;
9443 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9446 /* Is "bset" obviously equal to the universe with the same space?
9448 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9450 return isl_basic_map_plain_is_universe(bset);
9453 /* If "c" does not involve any existentially quantified variables,
9454 * then set *univ to false and abort
9456 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9458 isl_bool *univ = user;
9459 isl_size n;
9461 n = isl_constraint_dim(c, isl_dim_div);
9462 if (n < 0)
9463 c = isl_constraint_free(c);
9464 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9465 isl_constraint_free(c);
9466 if (*univ < 0 || !*univ)
9467 return isl_stat_error;
9468 return isl_stat_ok;
9471 /* Is "bmap" equal to the universe with the same space?
9473 * First check if it is obviously equal to the universe.
9474 * If not and if there are any constraints not involving
9475 * existentially quantified variables, then it is certainly
9476 * not equal to the universe.
9477 * Otherwise, check if the universe is a subset of "bmap".
9479 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9481 isl_size n_div;
9482 isl_bool univ;
9483 isl_basic_map *test;
9485 univ = isl_basic_map_plain_is_universe(bmap);
9486 if (univ < 0 || univ)
9487 return univ;
9488 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9489 if (n_div < 0)
9490 return isl_bool_error;
9491 if (n_div == 0)
9492 return isl_bool_false;
9493 univ = isl_bool_true;
9494 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9495 univ)
9496 return isl_bool_error;
9497 if (univ < 0 || !univ)
9498 return univ;
9499 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9500 univ = isl_basic_map_is_subset(test, bmap);
9501 isl_basic_map_free(test);
9502 return univ;
9505 /* Is "bset" equal to the universe with the same space?
9507 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9509 return isl_basic_map_is_universe(bset);
9512 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9514 int i;
9516 if (!map)
9517 return isl_bool_error;
9519 for (i = 0; i < map->n; ++i) {
9520 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9521 if (r < 0 || r)
9522 return r;
9525 return isl_bool_false;
9528 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9530 return isl_map_plain_is_universe(set_to_map(set));
9533 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9535 struct isl_basic_set *bset = NULL;
9536 struct isl_vec *sample = NULL;
9537 isl_bool empty, non_empty;
9539 if (!bmap)
9540 return isl_bool_error;
9542 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9543 return isl_bool_true;
9545 if (isl_basic_map_plain_is_universe(bmap))
9546 return isl_bool_false;
9548 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9549 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9550 copy = isl_basic_map_remove_redundancies(copy);
9551 empty = isl_basic_map_plain_is_empty(copy);
9552 isl_basic_map_free(copy);
9553 return empty;
9556 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9557 if (non_empty < 0)
9558 return isl_bool_error;
9559 if (non_empty)
9560 return isl_bool_false;
9561 isl_vec_free(bmap->sample);
9562 bmap->sample = NULL;
9563 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9564 if (!bset)
9565 return isl_bool_error;
9566 sample = isl_basic_set_sample_vec(bset);
9567 if (!sample)
9568 return isl_bool_error;
9569 empty = sample->size == 0;
9570 isl_vec_free(bmap->sample);
9571 bmap->sample = sample;
9572 if (empty)
9573 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9575 return empty;
9578 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9580 if (!bmap)
9581 return isl_bool_error;
9582 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9585 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9587 if (!bset)
9588 return isl_bool_error;
9589 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9592 /* Is "bmap" known to be non-empty?
9594 * That is, is the cached sample still valid?
9596 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9598 isl_size total;
9600 if (!bmap)
9601 return isl_bool_error;
9602 if (!bmap->sample)
9603 return isl_bool_false;
9604 total = isl_basic_map_dim(bmap, isl_dim_all);
9605 if (total < 0)
9606 return isl_bool_error;
9607 if (bmap->sample->size != 1 + total)
9608 return isl_bool_false;
9609 return isl_basic_map_contains(bmap, bmap->sample);
9612 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9614 return isl_basic_map_is_empty(bset_to_bmap(bset));
9617 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9618 __isl_take isl_basic_map *bmap2)
9620 struct isl_map *map;
9622 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9623 goto error;
9625 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9626 if (!map)
9627 goto error;
9628 map = isl_map_add_basic_map(map, bmap1);
9629 map = isl_map_add_basic_map(map, bmap2);
9630 return map;
9631 error:
9632 isl_basic_map_free(bmap1);
9633 isl_basic_map_free(bmap2);
9634 return NULL;
9637 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9638 __isl_take isl_basic_set *bset2)
9640 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9641 bset_to_bmap(bset2)));
9644 /* Order divs such that any div only depends on previous divs */
9645 __isl_give isl_basic_map *isl_basic_map_order_divs(
9646 __isl_take isl_basic_map *bmap)
9648 int i;
9649 isl_size off;
9651 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9652 if (off < 0)
9653 return isl_basic_map_free(bmap);
9655 for (i = 0; i < bmap->n_div; ++i) {
9656 int pos;
9657 if (isl_int_is_zero(bmap->div[i][0]))
9658 continue;
9659 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9660 bmap->n_div-i);
9661 if (pos == -1)
9662 continue;
9663 if (pos == 0)
9664 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9665 "integer division depends on itself",
9666 return isl_basic_map_free(bmap));
9667 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9668 if (!bmap)
9669 return NULL;
9670 --i;
9672 return bmap;
9675 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9677 int i;
9679 if (!map)
9680 return 0;
9682 for (i = 0; i < map->n; ++i) {
9683 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9684 if (!map->p[i])
9685 goto error;
9688 return map;
9689 error:
9690 isl_map_free(map);
9691 return NULL;
9694 /* Sort the local variables of "bset".
9696 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9697 __isl_take isl_basic_set *bset)
9699 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9702 /* Apply the expansion computed by isl_merge_divs.
9703 * The expansion itself is given by "exp" while the resulting
9704 * list of divs is given by "div".
9706 * Move the integer divisions of "bmap" into the right position
9707 * according to "exp" and then introduce the additional integer
9708 * divisions, adding div constraints.
9709 * The moving should be done first to avoid moving coefficients
9710 * in the definitions of the extra integer divisions.
9712 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9713 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9715 int i, j;
9716 int n_div;
9718 bmap = isl_basic_map_cow(bmap);
9719 if (!bmap || !div)
9720 goto error;
9722 if (div->n_row < bmap->n_div)
9723 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9724 "not an expansion", goto error);
9726 n_div = bmap->n_div;
9727 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9728 2 * (div->n_row - n_div));
9730 for (i = n_div; i < div->n_row; ++i)
9731 if (isl_basic_map_alloc_div(bmap) < 0)
9732 goto error;
9734 for (j = n_div - 1; j >= 0; --j) {
9735 if (exp[j] == j)
9736 break;
9737 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9738 if (!bmap)
9739 goto error;
9741 j = 0;
9742 for (i = 0; i < div->n_row; ++i) {
9743 if (j < n_div && exp[j] == i) {
9744 j++;
9745 } else {
9746 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9747 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9748 continue;
9749 bmap = isl_basic_map_add_div_constraints(bmap, i);
9750 if (!bmap)
9751 goto error;
9755 isl_mat_free(div);
9756 return bmap;
9757 error:
9758 isl_basic_map_free(bmap);
9759 isl_mat_free(div);
9760 return NULL;
9763 /* Apply the expansion computed by isl_merge_divs.
9764 * The expansion itself is given by "exp" while the resulting
9765 * list of divs is given by "div".
9767 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9768 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9770 return isl_basic_map_expand_divs(bset, div, exp);
9773 /* Look for a div in dst that corresponds to the div "div" in src.
9774 * The divs before "div" in src and dst are assumed to be the same.
9776 * Return the position of the corresponding div in dst
9777 * if there is one. Otherwise, return a position beyond the integer divisions.
9778 * Return -1 on error.
9780 static int find_div(__isl_keep isl_basic_map *dst,
9781 __isl_keep isl_basic_map *src, unsigned div)
9783 int i;
9784 isl_size n_div;
9785 isl_size v_div;
9787 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9788 n_div = isl_basic_map_dim(dst, isl_dim_div);
9789 if (n_div < 0 || v_div < 0)
9790 return -1;
9791 isl_assert(dst->ctx, div <= n_div, return -1);
9792 for (i = div; i < n_div; ++i)
9793 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9794 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9795 n_div - div) == -1)
9796 return i;
9797 return n_div;
9800 /* Align the divs of "dst" to those of "src", adding divs from "src"
9801 * if needed. That is, make sure that the first src->n_div divs
9802 * of the result are equal to those of src.
9803 * The integer division of "src" are assumed to be ordered.
9805 * The integer divisions are swapped into the right position
9806 * (possibly after adding them first). This may result
9807 * in the remaining integer divisions appearing in the wrong order,
9808 * i.e., with some integer division appearing before
9809 * some other integer division on which it depends.
9810 * The integer divisions therefore need to be ordered.
9811 * This will not affect the integer divisions aligned to those of "src",
9812 * since "src" is assumed to have ordered integer divisions.
9814 * The result is not finalized as by design it will have redundant
9815 * divs if any divs from "src" were copied.
9817 __isl_give isl_basic_map *isl_basic_map_align_divs(
9818 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9820 int i;
9821 isl_bool known;
9822 int extended;
9823 isl_size v_div;
9824 isl_size dst_n_div;
9826 if (!dst || !src)
9827 return isl_basic_map_free(dst);
9829 if (src->n_div == 0)
9830 return dst;
9832 known = isl_basic_map_divs_known(src);
9833 if (known < 0)
9834 return isl_basic_map_free(dst);
9835 if (!known)
9836 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9837 "some src divs are unknown",
9838 return isl_basic_map_free(dst));
9840 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9841 if (v_div < 0)
9842 return isl_basic_map_free(dst);
9844 extended = 0;
9845 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9846 if (dst_n_div < 0)
9847 dst = isl_basic_map_free(dst);
9848 for (i = 0; i < src->n_div; ++i) {
9849 int j = find_div(dst, src, i);
9850 if (j < 0)
9851 dst = isl_basic_map_free(dst);
9852 if (j == dst_n_div) {
9853 if (!extended) {
9854 int extra = src->n_div - i;
9855 dst = isl_basic_map_cow(dst);
9856 if (!dst)
9857 return isl_basic_map_free(dst);
9858 dst = isl_basic_map_extend(dst,
9859 extra, 0, 2 * extra);
9860 extended = 1;
9862 j = isl_basic_map_alloc_div(dst);
9863 if (j < 0)
9864 return isl_basic_map_free(dst);
9865 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9866 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9867 dst_n_div++;
9868 dst = isl_basic_map_add_div_constraints(dst, j);
9869 if (!dst)
9870 return isl_basic_map_free(dst);
9872 if (j != i)
9873 dst = isl_basic_map_swap_div(dst, i, j);
9874 if (!dst)
9875 return isl_basic_map_free(dst);
9877 return isl_basic_map_order_divs(dst);
9880 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9882 int i;
9884 if (!map)
9885 return NULL;
9886 if (map->n == 0)
9887 return map;
9888 map = isl_map_compute_divs(map);
9889 map = isl_map_order_divs(map);
9890 map = isl_map_cow(map);
9891 if (!map)
9892 return NULL;
9894 for (i = 1; i < map->n; ++i)
9895 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9896 for (i = 1; i < map->n; ++i) {
9897 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9898 if (!map->p[i])
9899 return isl_map_free(map);
9902 map = isl_map_unmark_normalized(map);
9903 return map;
9906 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9908 return isl_map_align_divs_internal(map);
9911 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9913 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9916 /* Align the divs of the basic maps in "map" to those
9917 * of the basic maps in "list", as well as to the other basic maps in "map".
9918 * The elements in "list" are assumed to have known divs.
9920 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9921 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9923 int i;
9924 isl_size n;
9926 n = isl_basic_map_list_n_basic_map(list);
9927 map = isl_map_compute_divs(map);
9928 map = isl_map_cow(map);
9929 if (!map || n < 0)
9930 return isl_map_free(map);
9931 if (map->n == 0)
9932 return map;
9934 for (i = 0; i < n; ++i) {
9935 isl_basic_map *bmap;
9937 bmap = isl_basic_map_list_get_basic_map(list, i);
9938 bmap = isl_basic_map_order_divs(bmap);
9939 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9940 isl_basic_map_free(bmap);
9942 if (!map->p[0])
9943 return isl_map_free(map);
9945 return isl_map_align_divs_internal(map);
9948 /* Align the divs of each element of "list" to those of "bmap".
9949 * Both "bmap" and the elements of "list" are assumed to have known divs.
9951 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9952 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9954 int i;
9955 isl_size n;
9957 n = isl_basic_map_list_n_basic_map(list);
9958 if (n < 0 || !bmap)
9959 return isl_basic_map_list_free(list);
9961 for (i = 0; i < n; ++i) {
9962 isl_basic_map *bmap_i;
9964 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9965 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9966 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9969 return list;
9972 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9973 __isl_take isl_map *map)
9975 isl_bool ok;
9977 isl_map_align_params_set(&map, &set);
9978 ok = isl_map_compatible_domain(map, set);
9979 if (ok < 0)
9980 goto error;
9981 if (!ok)
9982 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9983 "incompatible spaces", goto error);
9984 map = isl_map_intersect_domain(map, set);
9985 set = isl_map_range(map);
9986 return set;
9987 error:
9988 isl_set_free(set);
9989 isl_map_free(map);
9990 return NULL;
9993 /* There is no need to cow as removing empty parts doesn't change
9994 * the meaning of the set.
9996 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9998 int i;
10000 if (!map)
10001 return NULL;
10003 for (i = map->n - 1; i >= 0; --i)
10004 map = remove_if_empty(map, i);
10006 return map;
10009 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10011 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10014 /* Create a binary relation that maps the shared initial "pos" dimensions
10015 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10017 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10018 __isl_keep isl_basic_set *bset2, int pos)
10020 isl_basic_map *bmap1;
10021 isl_basic_map *bmap2;
10023 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10024 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10025 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10026 isl_dim_out, 0, pos);
10027 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10028 isl_dim_out, 0, pos);
10029 return isl_basic_map_range_product(bmap1, bmap2);
10032 /* Given two basic sets bset1 and bset2, compute the maximal difference
10033 * between the values of dimension pos in bset1 and those in bset2
10034 * for any common value of the parameters and dimensions preceding pos.
10036 static enum isl_lp_result basic_set_maximal_difference_at(
10037 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10038 int pos, isl_int *opt)
10040 isl_basic_map *bmap1;
10041 struct isl_ctx *ctx;
10042 struct isl_vec *obj;
10043 isl_size total;
10044 isl_size nparam;
10045 isl_size dim1;
10046 enum isl_lp_result res;
10048 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10049 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10050 if (nparam < 0 || dim1 < 0 || !bset2)
10051 return isl_lp_error;
10053 bmap1 = join_initial(bset1, bset2, pos);
10054 total = isl_basic_map_dim(bmap1, isl_dim_all);
10055 if (total < 0)
10056 return isl_lp_error;
10058 ctx = bmap1->ctx;
10059 obj = isl_vec_alloc(ctx, 1 + total);
10060 if (!obj)
10061 goto error;
10062 isl_seq_clr(obj->block.data, 1 + total);
10063 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10064 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10065 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10066 opt, NULL, NULL);
10067 isl_basic_map_free(bmap1);
10068 isl_vec_free(obj);
10069 return res;
10070 error:
10071 isl_basic_map_free(bmap1);
10072 return isl_lp_error;
10075 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10076 * for any common value of the parameters and dimensions preceding pos
10077 * in both basic sets, the values of dimension pos in bset1 are
10078 * smaller or larger than those in bset2.
10080 * Returns
10081 * 1 if bset1 follows bset2
10082 * -1 if bset1 precedes bset2
10083 * 0 if bset1 and bset2 are incomparable
10084 * -2 if some error occurred.
10086 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10087 __isl_keep isl_basic_set *bset2, int pos)
10089 isl_int opt;
10090 enum isl_lp_result res;
10091 int cmp;
10093 isl_int_init(opt);
10095 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10097 if (res == isl_lp_empty)
10098 cmp = 0;
10099 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10100 res == isl_lp_unbounded)
10101 cmp = 1;
10102 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10103 cmp = -1;
10104 else
10105 cmp = -2;
10107 isl_int_clear(opt);
10108 return cmp;
10111 /* Given two basic sets bset1 and bset2, check whether
10112 * for any common value of the parameters and dimensions preceding pos
10113 * there is a value of dimension pos in bset1 that is larger
10114 * than a value of the same dimension in bset2.
10116 * Return
10117 * 1 if there exists such a pair
10118 * 0 if there is no such pair, but there is a pair of equal values
10119 * -1 otherwise
10120 * -2 if some error occurred.
10122 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10123 __isl_keep isl_basic_set *bset2, int pos)
10125 isl_bool empty;
10126 isl_basic_map *bmap;
10127 isl_size dim1;
10129 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10130 if (dim1 < 0)
10131 return -2;
10132 bmap = join_initial(bset1, bset2, pos);
10133 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10134 isl_dim_out, dim1 - pos);
10135 empty = isl_basic_map_is_empty(bmap);
10136 if (empty < 0)
10137 goto error;
10138 if (empty) {
10139 isl_basic_map_free(bmap);
10140 return -1;
10142 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10143 isl_dim_out, dim1 - pos);
10144 empty = isl_basic_map_is_empty(bmap);
10145 if (empty < 0)
10146 goto error;
10147 isl_basic_map_free(bmap);
10148 if (empty)
10149 return 0;
10150 return 1;
10151 error:
10152 isl_basic_map_free(bmap);
10153 return -2;
10156 /* Given two sets set1 and set2, check whether
10157 * for any common value of the parameters and dimensions preceding pos
10158 * there is a value of dimension pos in set1 that is larger
10159 * than a value of the same dimension in set2.
10161 * Return
10162 * 1 if there exists such a pair
10163 * 0 if there is no such pair, but there is a pair of equal values
10164 * -1 otherwise
10165 * -2 if some error occurred.
10167 int isl_set_follows_at(__isl_keep isl_set *set1,
10168 __isl_keep isl_set *set2, int pos)
10170 int i, j;
10171 int follows = -1;
10173 if (!set1 || !set2)
10174 return -2;
10176 for (i = 0; i < set1->n; ++i)
10177 for (j = 0; j < set2->n; ++j) {
10178 int f;
10179 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10180 if (f == 1 || f == -2)
10181 return f;
10182 if (f > follows)
10183 follows = f;
10186 return follows;
10189 static isl_bool isl_basic_map_plain_has_fixed_var(
10190 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10192 int i;
10193 int d;
10194 isl_size total;
10196 total = isl_basic_map_dim(bmap, isl_dim_all);
10197 if (total < 0)
10198 return isl_bool_error;
10199 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10200 for (; d+1 > pos; --d)
10201 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10202 break;
10203 if (d != pos)
10204 continue;
10205 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10206 return isl_bool_false;
10207 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10208 return isl_bool_false;
10209 if (!isl_int_is_one(bmap->eq[i][1+d]))
10210 return isl_bool_false;
10211 if (val)
10212 isl_int_neg(*val, bmap->eq[i][0]);
10213 return isl_bool_true;
10215 return isl_bool_false;
10218 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10219 unsigned pos, isl_int *val)
10221 int i;
10222 isl_int v;
10223 isl_int tmp;
10224 isl_bool fixed;
10226 if (!map)
10227 return isl_bool_error;
10228 if (map->n == 0)
10229 return isl_bool_false;
10230 if (map->n == 1)
10231 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10232 isl_int_init(v);
10233 isl_int_init(tmp);
10234 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10235 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10236 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10237 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10238 fixed = isl_bool_false;
10240 if (val)
10241 isl_int_set(*val, v);
10242 isl_int_clear(tmp);
10243 isl_int_clear(v);
10244 return fixed;
10247 static isl_bool isl_basic_set_plain_has_fixed_var(
10248 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10250 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10251 pos, val);
10254 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10255 enum isl_dim_type type, unsigned pos, isl_int *val)
10257 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10258 return isl_bool_error;
10259 return isl_basic_map_plain_has_fixed_var(bmap,
10260 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10263 /* If "bmap" obviously lies on a hyperplane where the given dimension
10264 * has a fixed value, then return that value.
10265 * Otherwise return NaN.
10267 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10268 __isl_keep isl_basic_map *bmap,
10269 enum isl_dim_type type, unsigned pos)
10271 isl_ctx *ctx;
10272 isl_val *v;
10273 isl_bool fixed;
10275 if (!bmap)
10276 return NULL;
10277 ctx = isl_basic_map_get_ctx(bmap);
10278 v = isl_val_alloc(ctx);
10279 if (!v)
10280 return NULL;
10281 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10282 if (fixed < 0)
10283 return isl_val_free(v);
10284 if (fixed) {
10285 isl_int_set_si(v->d, 1);
10286 return v;
10288 isl_val_free(v);
10289 return isl_val_nan(ctx);
10292 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10293 enum isl_dim_type type, unsigned pos, isl_int *val)
10295 if (isl_map_check_range(map, type, pos, 1) < 0)
10296 return isl_bool_error;
10297 return isl_map_plain_has_fixed_var(map,
10298 map_offset(map, type) - 1 + pos, val);
10301 /* If "map" obviously lies on a hyperplane where the given dimension
10302 * has a fixed value, then return that value.
10303 * Otherwise return NaN.
10305 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10306 enum isl_dim_type type, unsigned pos)
10308 isl_ctx *ctx;
10309 isl_val *v;
10310 isl_bool fixed;
10312 if (!map)
10313 return NULL;
10314 ctx = isl_map_get_ctx(map);
10315 v = isl_val_alloc(ctx);
10316 if (!v)
10317 return NULL;
10318 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10319 if (fixed < 0)
10320 return isl_val_free(v);
10321 if (fixed) {
10322 isl_int_set_si(v->d, 1);
10323 return v;
10325 isl_val_free(v);
10326 return isl_val_nan(ctx);
10329 /* If "set" obviously lies on a hyperplane where the given dimension
10330 * has a fixed value, then return that value.
10331 * Otherwise return NaN.
10333 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10334 enum isl_dim_type type, unsigned pos)
10336 return isl_map_plain_get_val_if_fixed(set, type, pos);
10339 /* Return a sequence of values in the same space as "set"
10340 * that are equal to the corresponding set dimensions of "set"
10341 * for those set dimensions that obviously lie on a hyperplane
10342 * where the dimension has a fixed value.
10343 * The other elements are set to NaN.
10345 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10346 __isl_keep isl_set *set)
10348 int i;
10349 isl_size n;
10350 isl_space *space;
10351 isl_multi_val *mv;
10353 space = isl_space_drop_all_params(isl_set_get_space(set));
10354 mv = isl_multi_val_alloc(space);
10355 n = isl_multi_val_size(mv);
10356 if (n < 0)
10357 return isl_multi_val_free(mv);
10359 for (i = 0; i < n; ++i) {
10360 isl_val *v;
10362 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10363 mv = isl_multi_val_set_val(mv, i, v);
10366 return mv;
10369 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10370 * then return this fixed value in *val.
10372 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10373 unsigned dim, isl_int *val)
10375 isl_size nparam;
10377 nparam = isl_basic_set_dim(bset, isl_dim_param);
10378 if (nparam < 0)
10379 return isl_bool_error;
10380 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10383 /* Return -1 if the constraint "c1" should be sorted before "c2"
10384 * and 1 if it should be sorted after "c2".
10385 * Return 0 if the two constraints are the same (up to the constant term).
10387 * In particular, if a constraint involves later variables than another
10388 * then it is sorted after this other constraint.
10389 * uset_gist depends on constraints without existentially quantified
10390 * variables sorting first.
10392 * For constraints that have the same latest variable, those
10393 * with the same coefficient for this latest variable (first in absolute value
10394 * and then in actual value) are grouped together.
10395 * This is useful for detecting pairs of constraints that can
10396 * be chained in their printed representation.
10398 * Finally, within a group, constraints are sorted according to
10399 * their coefficients (excluding the constant term).
10401 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10403 isl_int **c1 = (isl_int **) p1;
10404 isl_int **c2 = (isl_int **) p2;
10405 int l1, l2;
10406 unsigned size = *(unsigned *) arg;
10407 int cmp;
10409 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10410 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10412 if (l1 != l2)
10413 return l1 - l2;
10415 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10416 if (cmp != 0)
10417 return cmp;
10418 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10419 if (cmp != 0)
10420 return -cmp;
10422 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10425 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10426 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10427 * and 0 if the two constraints are the same (up to the constant term).
10429 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10430 isl_int *c1, isl_int *c2)
10432 isl_size total;
10433 unsigned size;
10435 total = isl_basic_map_dim(bmap, isl_dim_all);
10436 if (total < 0)
10437 return -2;
10438 size = total;
10439 return sort_constraint_cmp(&c1, &c2, &size);
10442 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10443 __isl_take isl_basic_map *bmap)
10445 isl_size total;
10446 unsigned size;
10448 if (!bmap)
10449 return NULL;
10450 if (bmap->n_ineq == 0)
10451 return bmap;
10452 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10453 return bmap;
10454 total = isl_basic_map_dim(bmap, isl_dim_all);
10455 if (total < 0)
10456 return isl_basic_map_free(bmap);
10457 size = total;
10458 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10459 &sort_constraint_cmp, &size) < 0)
10460 return isl_basic_map_free(bmap);
10461 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10462 return bmap;
10465 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10466 __isl_take isl_basic_set *bset)
10468 isl_basic_map *bmap = bset_to_bmap(bset);
10469 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10472 __isl_give isl_basic_map *isl_basic_map_normalize(
10473 __isl_take isl_basic_map *bmap)
10475 bmap = isl_basic_map_remove_redundancies(bmap);
10476 bmap = isl_basic_map_sort_constraints(bmap);
10477 return bmap;
10479 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10480 __isl_keep isl_basic_map *bmap2)
10482 int i, cmp;
10483 isl_size total;
10484 isl_space *space1, *space2;
10486 if (!bmap1 || !bmap2)
10487 return -1;
10489 if (bmap1 == bmap2)
10490 return 0;
10491 space1 = isl_basic_map_peek_space(bmap1);
10492 space2 = isl_basic_map_peek_space(bmap2);
10493 cmp = isl_space_cmp(space1, space2);
10494 if (cmp)
10495 return cmp;
10496 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10497 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10498 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10499 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10500 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10501 return 0;
10502 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10503 return 1;
10504 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10505 return -1;
10506 if (bmap1->n_eq != bmap2->n_eq)
10507 return bmap1->n_eq - bmap2->n_eq;
10508 if (bmap1->n_ineq != bmap2->n_ineq)
10509 return bmap1->n_ineq - bmap2->n_ineq;
10510 if (bmap1->n_div != bmap2->n_div)
10511 return bmap1->n_div - bmap2->n_div;
10512 total = isl_basic_map_dim(bmap1, isl_dim_all);
10513 if (total < 0)
10514 return -1;
10515 for (i = 0; i < bmap1->n_eq; ++i) {
10516 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10517 if (cmp)
10518 return cmp;
10520 for (i = 0; i < bmap1->n_ineq; ++i) {
10521 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10522 if (cmp)
10523 return cmp;
10525 for (i = 0; i < bmap1->n_div; ++i) {
10526 isl_bool unknown1, unknown2;
10528 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10529 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10530 if (unknown1 < 0 || unknown2 < 0)
10531 return -1;
10532 if (unknown1 && unknown2)
10533 continue;
10534 if (unknown1)
10535 return 1;
10536 if (unknown2)
10537 return -1;
10538 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10539 if (cmp)
10540 return cmp;
10542 return 0;
10545 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10546 __isl_keep isl_basic_set *bset2)
10548 return isl_basic_map_plain_cmp(bset1, bset2);
10551 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10553 int i, cmp;
10555 if (set1 == set2)
10556 return 0;
10557 if (set1->n != set2->n)
10558 return set1->n - set2->n;
10560 for (i = 0; i < set1->n; ++i) {
10561 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10562 if (cmp)
10563 return cmp;
10566 return 0;
10569 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10570 __isl_keep isl_basic_map *bmap2)
10572 if (!bmap1 || !bmap2)
10573 return isl_bool_error;
10574 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10577 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10578 __isl_keep isl_basic_set *bset2)
10580 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10581 bset_to_bmap(bset2));
10584 static int qsort_bmap_cmp(const void *p1, const void *p2)
10586 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10587 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10589 return isl_basic_map_plain_cmp(bmap1, bmap2);
10592 /* Sort the basic maps of "map" and remove duplicate basic maps.
10594 * While removing basic maps, we make sure that the basic maps remain
10595 * sorted because isl_map_normalize expects the basic maps of the result
10596 * to be sorted.
10598 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10600 int i, j;
10602 map = isl_map_remove_empty_parts(map);
10603 if (!map)
10604 return NULL;
10605 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10606 for (i = map->n - 1; i >= 1; --i) {
10607 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10608 continue;
10609 isl_basic_map_free(map->p[i-1]);
10610 for (j = i; j < map->n; ++j)
10611 map->p[j - 1] = map->p[j];
10612 map->n--;
10615 return map;
10618 /* Remove obvious duplicates among the basic maps of "map".
10620 * Unlike isl_map_normalize, this function does not remove redundant
10621 * constraints and only removes duplicates that have exactly the same
10622 * constraints in the input. It does sort the constraints and
10623 * the basic maps to ease the detection of duplicates.
10625 * If "map" has already been normalized or if the basic maps are
10626 * disjoint, then there can be no duplicates.
10628 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10630 int i;
10631 isl_basic_map *bmap;
10633 if (!map)
10634 return NULL;
10635 if (map->n <= 1)
10636 return map;
10637 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10638 return map;
10639 for (i = 0; i < map->n; ++i) {
10640 bmap = isl_basic_map_copy(map->p[i]);
10641 bmap = isl_basic_map_sort_constraints(bmap);
10642 if (!bmap)
10643 return isl_map_free(map);
10644 isl_basic_map_free(map->p[i]);
10645 map->p[i] = bmap;
10648 map = sort_and_remove_duplicates(map);
10649 return map;
10652 /* We normalize in place, but if anything goes wrong we need
10653 * to return NULL, so we need to make sure we don't change the
10654 * meaning of any possible other copies of map.
10656 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10658 int i;
10659 struct isl_basic_map *bmap;
10661 if (!map)
10662 return NULL;
10663 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10664 return map;
10665 for (i = 0; i < map->n; ++i) {
10666 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10667 if (!bmap)
10668 goto error;
10669 isl_basic_map_free(map->p[i]);
10670 map->p[i] = bmap;
10673 map = sort_and_remove_duplicates(map);
10674 if (map)
10675 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10676 return map;
10677 error:
10678 isl_map_free(map);
10679 return NULL;
10682 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10684 return set_from_map(isl_map_normalize(set_to_map(set)));
10687 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10688 __isl_keep isl_map *map2)
10690 int i;
10691 isl_bool equal;
10693 if (!map1 || !map2)
10694 return isl_bool_error;
10696 if (map1 == map2)
10697 return isl_bool_true;
10698 equal = isl_map_has_equal_space(map1, map2);
10699 if (equal < 0 || !equal)
10700 return equal;
10702 map1 = isl_map_copy(map1);
10703 map2 = isl_map_copy(map2);
10704 map1 = isl_map_normalize(map1);
10705 map2 = isl_map_normalize(map2);
10706 if (!map1 || !map2)
10707 goto error;
10708 equal = map1->n == map2->n;
10709 for (i = 0; equal && i < map1->n; ++i) {
10710 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10711 if (equal < 0)
10712 goto error;
10714 isl_map_free(map1);
10715 isl_map_free(map2);
10716 return equal;
10717 error:
10718 isl_map_free(map1);
10719 isl_map_free(map2);
10720 return isl_bool_error;
10723 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10724 __isl_keep isl_set *set2)
10726 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10729 /* Return the basic maps in "map" as a list.
10731 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10732 __isl_keep isl_map *map)
10734 int i;
10735 isl_ctx *ctx;
10736 isl_basic_map_list *list;
10738 if (!map)
10739 return NULL;
10740 ctx = isl_map_get_ctx(map);
10741 list = isl_basic_map_list_alloc(ctx, map->n);
10743 for (i = 0; i < map->n; ++i) {
10744 isl_basic_map *bmap;
10746 bmap = isl_basic_map_copy(map->p[i]);
10747 list = isl_basic_map_list_add(list, bmap);
10750 return list;
10753 /* Return the intersection of the elements in the non-empty list "list".
10754 * All elements are assumed to live in the same space.
10756 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10757 __isl_take isl_basic_map_list *list)
10759 int i;
10760 isl_size n;
10761 isl_basic_map *bmap;
10763 n = isl_basic_map_list_n_basic_map(list);
10764 if (n < 0)
10765 goto error;
10766 if (n < 1)
10767 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10768 "expecting non-empty list", goto error);
10770 bmap = isl_basic_map_list_get_basic_map(list, 0);
10771 for (i = 1; i < n; ++i) {
10772 isl_basic_map *bmap_i;
10774 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10775 bmap = isl_basic_map_intersect(bmap, bmap_i);
10778 isl_basic_map_list_free(list);
10779 return bmap;
10780 error:
10781 isl_basic_map_list_free(list);
10782 return NULL;
10785 /* Return the intersection of the elements in the non-empty list "list".
10786 * All elements are assumed to live in the same space.
10788 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10789 __isl_take isl_basic_set_list *list)
10791 return isl_basic_map_list_intersect(list);
10794 /* Return the union of the elements of "list".
10795 * The list is required to have at least one element.
10797 __isl_give isl_set *isl_basic_set_list_union(
10798 __isl_take isl_basic_set_list *list)
10800 int i;
10801 isl_size n;
10802 isl_space *space;
10803 isl_basic_set *bset;
10804 isl_set *set;
10806 n = isl_basic_set_list_n_basic_set(list);
10807 if (n < 0)
10808 goto error;
10809 if (n < 1)
10810 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10811 "expecting non-empty list", goto error);
10813 bset = isl_basic_set_list_get_basic_set(list, 0);
10814 space = isl_basic_set_get_space(bset);
10815 isl_basic_set_free(bset);
10817 set = isl_set_alloc_space(space, n, 0);
10818 for (i = 0; i < n; ++i) {
10819 bset = isl_basic_set_list_get_basic_set(list, i);
10820 set = isl_set_add_basic_set(set, bset);
10823 isl_basic_set_list_free(list);
10824 return set;
10825 error:
10826 isl_basic_set_list_free(list);
10827 return NULL;
10830 /* Return the union of the elements in the non-empty list "list".
10831 * All elements are assumed to live in the same space.
10833 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10835 int i;
10836 isl_size n;
10837 isl_set *set;
10839 n = isl_set_list_n_set(list);
10840 if (n < 0)
10841 goto error;
10842 if (n < 1)
10843 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10844 "expecting non-empty list", goto error);
10846 set = isl_set_list_get_set(list, 0);
10847 for (i = 1; i < n; ++i) {
10848 isl_set *set_i;
10850 set_i = isl_set_list_get_set(list, i);
10851 set = isl_set_union(set, set_i);
10854 isl_set_list_free(list);
10855 return set;
10856 error:
10857 isl_set_list_free(list);
10858 return NULL;
10861 __isl_give isl_basic_map *isl_basic_map_product(
10862 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10864 isl_space *space_result = NULL;
10865 struct isl_basic_map *bmap;
10866 unsigned in1, in2, out1, out2, nparam, total, pos;
10867 struct isl_dim_map *dim_map1, *dim_map2;
10869 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10870 goto error;
10871 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10872 isl_space_copy(bmap2->dim));
10874 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10875 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10876 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10877 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10878 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10880 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10881 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10882 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10883 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10884 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10885 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10886 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10887 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10888 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10889 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10890 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10892 bmap = isl_basic_map_alloc_space(space_result,
10893 bmap1->n_div + bmap2->n_div,
10894 bmap1->n_eq + bmap2->n_eq,
10895 bmap1->n_ineq + bmap2->n_ineq);
10896 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10897 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10898 bmap = isl_basic_map_simplify(bmap);
10899 return isl_basic_map_finalize(bmap);
10900 error:
10901 isl_basic_map_free(bmap1);
10902 isl_basic_map_free(bmap2);
10903 return NULL;
10906 __isl_give isl_basic_map *isl_basic_map_flat_product(
10907 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10909 isl_basic_map *prod;
10911 prod = isl_basic_map_product(bmap1, bmap2);
10912 prod = isl_basic_map_flatten(prod);
10913 return prod;
10916 __isl_give isl_basic_set *isl_basic_set_flat_product(
10917 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10919 return isl_basic_map_flat_range_product(bset1, bset2);
10922 __isl_give isl_basic_map *isl_basic_map_domain_product(
10923 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10925 isl_space *space1, *space2;
10926 isl_space *space_result = NULL;
10927 isl_basic_map *bmap;
10928 isl_size in1, in2, out, nparam;
10929 unsigned total, pos;
10930 struct isl_dim_map *dim_map1, *dim_map2;
10932 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10933 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10934 out = isl_basic_map_dim(bmap1, isl_dim_out);
10935 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10936 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10937 goto error;
10939 space1 = isl_basic_map_get_space(bmap1);
10940 space2 = isl_basic_map_get_space(bmap2);
10941 space_result = isl_space_domain_product(space1, space2);
10943 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10944 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10945 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10948 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10949 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10950 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10951 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10952 isl_dim_map_div(dim_map1, bmap1, pos += out);
10953 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10955 bmap = isl_basic_map_alloc_space(space_result,
10956 bmap1->n_div + bmap2->n_div,
10957 bmap1->n_eq + bmap2->n_eq,
10958 bmap1->n_ineq + bmap2->n_ineq);
10959 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10960 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10961 bmap = isl_basic_map_simplify(bmap);
10962 return isl_basic_map_finalize(bmap);
10963 error:
10964 isl_basic_map_free(bmap1);
10965 isl_basic_map_free(bmap2);
10966 return NULL;
10969 __isl_give isl_basic_map *isl_basic_map_range_product(
10970 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10972 isl_bool rational;
10973 isl_space *space_result = NULL;
10974 isl_basic_map *bmap;
10975 isl_size in, out1, out2, nparam;
10976 unsigned total, pos;
10977 struct isl_dim_map *dim_map1, *dim_map2;
10979 rational = isl_basic_map_is_rational(bmap1);
10980 if (rational >= 0 && rational)
10981 rational = isl_basic_map_is_rational(bmap2);
10982 in = isl_basic_map_dim(bmap1, isl_dim_in);
10983 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10984 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10985 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10986 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10987 goto error;
10989 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10990 goto error;
10992 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10993 isl_space_copy(bmap2->dim));
10995 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10996 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10997 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10998 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10999 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11000 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11001 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11002 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11003 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11004 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11005 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11007 bmap = isl_basic_map_alloc_space(space_result,
11008 bmap1->n_div + bmap2->n_div,
11009 bmap1->n_eq + bmap2->n_eq,
11010 bmap1->n_ineq + bmap2->n_ineq);
11011 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11012 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11013 if (rational)
11014 bmap = isl_basic_map_set_rational(bmap);
11015 bmap = isl_basic_map_simplify(bmap);
11016 return isl_basic_map_finalize(bmap);
11017 error:
11018 isl_basic_map_free(bmap1);
11019 isl_basic_map_free(bmap2);
11020 return NULL;
11023 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11024 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11026 isl_basic_map *prod;
11028 prod = isl_basic_map_range_product(bmap1, bmap2);
11029 prod = isl_basic_map_flatten_range(prod);
11030 return prod;
11033 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11034 * and collect the results.
11035 * The result live in the space obtained by calling "space_product"
11036 * on the spaces of "map1" and "map2".
11037 * If "remove_duplicates" is set then the result may contain duplicates
11038 * (even if the inputs do not) and so we try and remove the obvious
11039 * duplicates.
11041 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11042 __isl_take isl_map *map2,
11043 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11044 __isl_take isl_space *right),
11045 __isl_give isl_basic_map *(*basic_map_product)(
11046 __isl_take isl_basic_map *left,
11047 __isl_take isl_basic_map *right),
11048 int remove_duplicates)
11050 unsigned flags = 0;
11051 struct isl_map *result;
11052 int i, j;
11053 isl_bool m;
11055 m = isl_map_has_equal_params(map1, map2);
11056 if (m < 0)
11057 goto error;
11058 if (!m)
11059 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11060 "parameters don't match", goto error);
11062 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11063 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11064 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11066 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11067 isl_space_copy(map2->dim)),
11068 map1->n * map2->n, flags);
11069 if (!result)
11070 goto error;
11071 for (i = 0; i < map1->n; ++i)
11072 for (j = 0; j < map2->n; ++j) {
11073 struct isl_basic_map *part;
11074 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11075 isl_basic_map_copy(map2->p[j]));
11076 if (isl_basic_map_is_empty(part))
11077 isl_basic_map_free(part);
11078 else
11079 result = isl_map_add_basic_map(result, part);
11080 if (!result)
11081 goto error;
11083 if (remove_duplicates)
11084 result = isl_map_remove_obvious_duplicates(result);
11085 isl_map_free(map1);
11086 isl_map_free(map2);
11087 return result;
11088 error:
11089 isl_map_free(map1);
11090 isl_map_free(map2);
11091 return NULL;
11094 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11096 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11097 __isl_take isl_map *map2)
11099 isl_map_align_params_bin(&map1, &map2);
11100 return map_product(map1, map2, &isl_space_product,
11101 &isl_basic_map_product, 0);
11104 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11106 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11107 __isl_take isl_map *map2)
11109 isl_map *prod;
11111 prod = isl_map_product(map1, map2);
11112 prod = isl_map_flatten(prod);
11113 return prod;
11116 /* Given two set A and B, construct its Cartesian product A x B.
11118 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11119 __isl_take isl_set *set2)
11121 return isl_map_range_product(set1, set2);
11124 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11125 __isl_take isl_set *set2)
11127 return isl_map_flat_range_product(set1, set2);
11130 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11132 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11133 __isl_take isl_map *map2)
11135 isl_map_align_params_bin(&map1, &map2);
11136 return map_product(map1, map2, &isl_space_domain_product,
11137 &isl_basic_map_domain_product, 1);
11140 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11142 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11143 __isl_take isl_map *map2)
11145 isl_map_align_params_bin(&map1, &map2);
11146 return map_product(map1, map2, &isl_space_range_product,
11147 &isl_basic_map_range_product, 1);
11150 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11152 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11154 isl_space *space;
11155 isl_size total1, keep1, total2, keep2;
11157 total1 = isl_map_dim(map, isl_dim_in);
11158 total2 = isl_map_dim(map, isl_dim_out);
11159 if (total1 < 0 || total2 < 0)
11160 return isl_map_free(map);
11161 if (!isl_space_domain_is_wrapping(map->dim) ||
11162 !isl_space_range_is_wrapping(map->dim))
11163 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11164 "not a product", return isl_map_free(map));
11166 space = isl_map_get_space(map);
11167 space = isl_space_factor_domain(space);
11168 keep1 = isl_space_dim(space, isl_dim_in);
11169 keep2 = isl_space_dim(space, isl_dim_out);
11170 if (keep1 < 0 || keep2 < 0)
11171 map = isl_map_free(map);
11172 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11173 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11174 map = isl_map_reset_space(map, space);
11176 return map;
11179 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11181 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11183 isl_space *space;
11184 isl_size total1, keep1, total2, keep2;
11186 total1 = isl_map_dim(map, isl_dim_in);
11187 total2 = isl_map_dim(map, isl_dim_out);
11188 if (total1 < 0 || total2 < 0)
11189 return isl_map_free(map);
11190 if (!isl_space_domain_is_wrapping(map->dim) ||
11191 !isl_space_range_is_wrapping(map->dim))
11192 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11193 "not a product", return isl_map_free(map));
11195 space = isl_map_get_space(map);
11196 space = isl_space_factor_range(space);
11197 keep1 = isl_space_dim(space, isl_dim_in);
11198 keep2 = isl_space_dim(space, isl_dim_out);
11199 if (keep1 < 0 || keep2 < 0)
11200 map = isl_map_free(map);
11201 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11202 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11203 map = isl_map_reset_space(map, space);
11205 return map;
11208 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11210 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11212 isl_space *space;
11213 isl_size total, keep;
11215 total = isl_map_dim(map, isl_dim_in);
11216 if (total < 0)
11217 return isl_map_free(map);
11218 if (!isl_space_domain_is_wrapping(map->dim))
11219 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11220 "domain is not a product", return isl_map_free(map));
11222 space = isl_map_get_space(map);
11223 space = isl_space_domain_factor_domain(space);
11224 keep = isl_space_dim(space, isl_dim_in);
11225 if (keep < 0)
11226 map = isl_map_free(map);
11227 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11228 map = isl_map_reset_space(map, space);
11230 return map;
11233 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11235 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11237 isl_space *space;
11238 isl_size total, keep;
11240 total = isl_map_dim(map, isl_dim_in);
11241 if (total < 0)
11242 return isl_map_free(map);
11243 if (!isl_space_domain_is_wrapping(map->dim))
11244 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11245 "domain is not a product", return isl_map_free(map));
11247 space = isl_map_get_space(map);
11248 space = isl_space_domain_factor_range(space);
11249 keep = isl_space_dim(space, isl_dim_in);
11250 if (keep < 0)
11251 map = isl_map_free(map);
11252 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11253 map = isl_map_reset_space(map, space);
11255 return map;
11258 /* Given a map A -> [B -> C], extract the map A -> B.
11260 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11262 isl_space *space;
11263 isl_size total, keep;
11265 total = isl_map_dim(map, isl_dim_out);
11266 if (total < 0)
11267 return isl_map_free(map);
11268 if (!isl_space_range_is_wrapping(map->dim))
11269 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11270 "range is not a product", return isl_map_free(map));
11272 space = isl_map_get_space(map);
11273 space = isl_space_range_factor_domain(space);
11274 keep = isl_space_dim(space, isl_dim_out);
11275 if (keep < 0)
11276 map = isl_map_free(map);
11277 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11278 map = isl_map_reset_space(map, space);
11280 return map;
11283 /* Given a map A -> [B -> C], extract the map A -> C.
11285 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11287 isl_space *space;
11288 isl_size total, keep;
11290 total = isl_map_dim(map, isl_dim_out);
11291 if (total < 0)
11292 return isl_map_free(map);
11293 if (!isl_space_range_is_wrapping(map->dim))
11294 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11295 "range is not a product", return isl_map_free(map));
11297 space = isl_map_get_space(map);
11298 space = isl_space_range_factor_range(space);
11299 keep = isl_space_dim(space, isl_dim_out);
11300 if (keep < 0)
11301 map = isl_map_free(map);
11302 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11303 map = isl_map_reset_space(map, space);
11305 return map;
11308 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11310 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11311 __isl_take isl_map *map2)
11313 isl_map *prod;
11315 prod = isl_map_domain_product(map1, map2);
11316 prod = isl_map_flatten_domain(prod);
11317 return prod;
11320 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11322 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11323 __isl_take isl_map *map2)
11325 isl_map *prod;
11327 prod = isl_map_range_product(map1, map2);
11328 prod = isl_map_flatten_range(prod);
11329 return prod;
11332 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11334 int i;
11335 uint32_t hash = isl_hash_init();
11336 isl_size total;
11338 if (!bmap)
11339 return 0;
11340 bmap = isl_basic_map_copy(bmap);
11341 bmap = isl_basic_map_normalize(bmap);
11342 total = isl_basic_map_dim(bmap, isl_dim_all);
11343 if (total < 0)
11344 return 0;
11345 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11346 for (i = 0; i < bmap->n_eq; ++i) {
11347 uint32_t c_hash;
11348 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11349 isl_hash_hash(hash, c_hash);
11351 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11352 for (i = 0; i < bmap->n_ineq; ++i) {
11353 uint32_t c_hash;
11354 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11355 isl_hash_hash(hash, c_hash);
11357 isl_hash_byte(hash, bmap->n_div & 0xFF);
11358 for (i = 0; i < bmap->n_div; ++i) {
11359 uint32_t c_hash;
11360 if (isl_int_is_zero(bmap->div[i][0]))
11361 continue;
11362 isl_hash_byte(hash, i & 0xFF);
11363 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11364 isl_hash_hash(hash, c_hash);
11366 isl_basic_map_free(bmap);
11367 return hash;
11370 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11372 return isl_basic_map_get_hash(bset_to_bmap(bset));
11375 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11377 int i;
11378 uint32_t hash;
11380 if (!map)
11381 return 0;
11382 map = isl_map_copy(map);
11383 map = isl_map_normalize(map);
11384 if (!map)
11385 return 0;
11387 hash = isl_hash_init();
11388 for (i = 0; i < map->n; ++i) {
11389 uint32_t bmap_hash;
11390 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11391 isl_hash_hash(hash, bmap_hash);
11394 isl_map_free(map);
11396 return hash;
11399 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11401 return isl_map_get_hash(set_to_map(set));
11404 /* Return the number of basic maps in the (current) representation of "map".
11406 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11408 return map ? map->n : isl_size_error;
11411 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11413 return set ? set->n : isl_size_error;
11416 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11417 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11419 int i;
11421 if (!map)
11422 return isl_stat_error;
11424 for (i = 0; i < map->n; ++i)
11425 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11426 return isl_stat_error;
11428 return isl_stat_ok;
11431 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11432 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11434 int i;
11436 if (!set)
11437 return isl_stat_error;
11439 for (i = 0; i < set->n; ++i)
11440 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11441 return isl_stat_error;
11443 return isl_stat_ok;
11446 /* Does "test" succeed on every basic set in "set"?
11448 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11449 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11450 void *user)
11452 int i;
11454 if (!set)
11455 return isl_bool_error;
11457 for (i = 0; i < set->n; ++i) {
11458 isl_bool r;
11460 r = test(set->p[i], user);
11461 if (r < 0 || !r)
11462 return r;
11465 return isl_bool_true;
11468 /* Return a list of basic sets, the union of which is equal to "set".
11470 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11471 __isl_keep isl_set *set)
11473 int i;
11474 isl_basic_set_list *list;
11476 if (!set)
11477 return NULL;
11479 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11480 for (i = 0; i < set->n; ++i) {
11481 isl_basic_set *bset;
11483 bset = isl_basic_set_copy(set->p[i]);
11484 list = isl_basic_set_list_add(list, bset);
11487 return list;
11490 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11492 isl_space *space;
11494 if (!bset)
11495 return NULL;
11497 bset = isl_basic_set_cow(bset);
11498 if (!bset)
11499 return NULL;
11501 space = isl_basic_set_get_space(bset);
11502 space = isl_space_lift(space, bset->n_div);
11503 if (!space)
11504 goto error;
11505 isl_space_free(bset->dim);
11506 bset->dim = space;
11507 bset->extra -= bset->n_div;
11508 bset->n_div = 0;
11510 bset = isl_basic_set_finalize(bset);
11512 return bset;
11513 error:
11514 isl_basic_set_free(bset);
11515 return NULL;
11518 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11520 int i;
11521 isl_space *space;
11522 unsigned n_div;
11524 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11526 if (!set)
11527 return NULL;
11529 set = isl_set_cow(set);
11530 if (!set)
11531 return NULL;
11533 n_div = set->p[0]->n_div;
11534 space = isl_set_get_space(set);
11535 space = isl_space_lift(space, n_div);
11536 if (!space)
11537 goto error;
11538 isl_space_free(set->dim);
11539 set->dim = space;
11541 for (i = 0; i < set->n; ++i) {
11542 set->p[i] = isl_basic_set_lift(set->p[i]);
11543 if (!set->p[i])
11544 goto error;
11547 return set;
11548 error:
11549 isl_set_free(set);
11550 return NULL;
11553 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11555 isl_size dim;
11556 int size = 0;
11558 dim = isl_basic_set_dim(bset, isl_dim_all);
11559 if (dim < 0)
11560 return -1;
11561 size += bset->n_eq * (1 + dim);
11562 size += bset->n_ineq * (1 + dim);
11563 size += bset->n_div * (2 + dim);
11565 return size;
11568 int isl_set_size(__isl_keep isl_set *set)
11570 int i;
11571 int size = 0;
11573 if (!set)
11574 return -1;
11576 for (i = 0; i < set->n; ++i)
11577 size += isl_basic_set_size(set->p[i]);
11579 return size;
11582 /* Check if there is any lower bound (if lower == 0) and/or upper
11583 * bound (if upper == 0) on the specified dim.
11585 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11586 enum isl_dim_type type, unsigned pos, int lower, int upper)
11588 int i;
11590 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11591 return isl_bool_error;
11593 pos += isl_basic_map_offset(bmap, type);
11595 for (i = 0; i < bmap->n_div; ++i) {
11596 if (isl_int_is_zero(bmap->div[i][0]))
11597 continue;
11598 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11599 return isl_bool_true;
11602 for (i = 0; i < bmap->n_eq; ++i)
11603 if (!isl_int_is_zero(bmap->eq[i][pos]))
11604 return isl_bool_true;
11606 for (i = 0; i < bmap->n_ineq; ++i) {
11607 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11608 if (sgn > 0)
11609 lower = 1;
11610 if (sgn < 0)
11611 upper = 1;
11614 return lower && upper;
11617 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11618 enum isl_dim_type type, unsigned pos)
11620 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11623 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11624 enum isl_dim_type type, unsigned pos)
11626 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11629 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11630 enum isl_dim_type type, unsigned pos)
11632 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11635 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11636 enum isl_dim_type type, unsigned pos)
11638 int i;
11640 if (!map)
11641 return isl_bool_error;
11643 for (i = 0; i < map->n; ++i) {
11644 isl_bool bounded;
11645 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11646 if (bounded < 0 || !bounded)
11647 return bounded;
11650 return isl_bool_true;
11653 /* Return true if the specified dim is involved in both an upper bound
11654 * and a lower bound.
11656 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11657 enum isl_dim_type type, unsigned pos)
11659 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11662 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11664 static isl_bool has_any_bound(__isl_keep isl_map *map,
11665 enum isl_dim_type type, unsigned pos,
11666 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11667 enum isl_dim_type type, unsigned pos))
11669 int i;
11671 if (!map)
11672 return isl_bool_error;
11674 for (i = 0; i < map->n; ++i) {
11675 isl_bool bounded;
11676 bounded = fn(map->p[i], type, pos);
11677 if (bounded < 0 || bounded)
11678 return bounded;
11681 return isl_bool_false;
11684 /* Return 1 if the specified dim is involved in any lower bound.
11686 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11687 enum isl_dim_type type, unsigned pos)
11689 return has_any_bound(set, type, pos,
11690 &isl_basic_map_dim_has_lower_bound);
11693 /* Return 1 if the specified dim is involved in any upper bound.
11695 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11696 enum isl_dim_type type, unsigned pos)
11698 return has_any_bound(set, type, pos,
11699 &isl_basic_map_dim_has_upper_bound);
11702 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11704 static isl_bool has_bound(__isl_keep isl_map *map,
11705 enum isl_dim_type type, unsigned pos,
11706 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11707 enum isl_dim_type type, unsigned pos))
11709 int i;
11711 if (!map)
11712 return isl_bool_error;
11714 for (i = 0; i < map->n; ++i) {
11715 isl_bool bounded;
11716 bounded = fn(map->p[i], type, pos);
11717 if (bounded < 0 || !bounded)
11718 return bounded;
11721 return isl_bool_true;
11724 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11726 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11727 enum isl_dim_type type, unsigned pos)
11729 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11732 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11734 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11735 enum isl_dim_type type, unsigned pos)
11737 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11740 /* For each of the "n" variables starting at "first", determine
11741 * the sign of the variable and put the results in the first "n"
11742 * elements of the array "signs".
11743 * Sign
11744 * 1 means that the variable is non-negative
11745 * -1 means that the variable is non-positive
11746 * 0 means the variable attains both positive and negative values.
11748 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11749 unsigned first, unsigned n, int *signs)
11751 isl_vec *bound = NULL;
11752 struct isl_tab *tab = NULL;
11753 struct isl_tab_undo *snap;
11754 int i;
11755 isl_size total;
11757 total = isl_basic_set_dim(bset, isl_dim_all);
11758 if (total < 0 || !signs)
11759 return isl_stat_error;
11761 bound = isl_vec_alloc(bset->ctx, 1 + total);
11762 tab = isl_tab_from_basic_set(bset, 0);
11763 if (!bound || !tab)
11764 goto error;
11766 isl_seq_clr(bound->el, bound->size);
11767 isl_int_set_si(bound->el[0], -1);
11769 snap = isl_tab_snap(tab);
11770 for (i = 0; i < n; ++i) {
11771 int empty;
11773 isl_int_set_si(bound->el[1 + first + i], -1);
11774 if (isl_tab_add_ineq(tab, bound->el) < 0)
11775 goto error;
11776 empty = tab->empty;
11777 isl_int_set_si(bound->el[1 + first + i], 0);
11778 if (isl_tab_rollback(tab, snap) < 0)
11779 goto error;
11781 if (empty) {
11782 signs[i] = 1;
11783 continue;
11786 isl_int_set_si(bound->el[1 + first + i], 1);
11787 if (isl_tab_add_ineq(tab, bound->el) < 0)
11788 goto error;
11789 empty = tab->empty;
11790 isl_int_set_si(bound->el[1 + first + i], 0);
11791 if (isl_tab_rollback(tab, snap) < 0)
11792 goto error;
11794 signs[i] = empty ? -1 : 0;
11797 isl_tab_free(tab);
11798 isl_vec_free(bound);
11799 return isl_stat_ok;
11800 error:
11801 isl_tab_free(tab);
11802 isl_vec_free(bound);
11803 return isl_stat_error;
11806 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11807 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11809 if (!bset || !signs)
11810 return isl_stat_error;
11811 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11812 return isl_stat_error;
11814 first += pos(bset->dim, type) - 1;
11815 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11818 /* Is it possible for the integer division "div" to depend (possibly
11819 * indirectly) on any output dimensions?
11821 * If the div is undefined, then we conservatively assume that it
11822 * may depend on them.
11823 * Otherwise, we check if it actually depends on them or on any integer
11824 * divisions that may depend on them.
11826 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11828 int i;
11829 isl_size n_out, n_div;
11830 unsigned o_out, o_div;
11832 if (isl_int_is_zero(bmap->div[div][0]))
11833 return isl_bool_true;
11835 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11836 if (n_out < 0)
11837 return isl_bool_error;
11838 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11840 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11841 return isl_bool_true;
11843 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11844 if (n_div < 0)
11845 return isl_bool_error;
11846 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11848 for (i = 0; i < n_div; ++i) {
11849 isl_bool may_involve;
11851 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11852 continue;
11853 may_involve = div_may_involve_output(bmap, i);
11854 if (may_involve < 0 || may_involve)
11855 return may_involve;
11858 return isl_bool_false;
11861 /* Return the first integer division of "bmap" in the range
11862 * [first, first + n[ that may depend on any output dimensions and
11863 * that has a non-zero coefficient in "c" (where the first coefficient
11864 * in "c" corresponds to integer division "first").
11866 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11867 isl_int *c, int first, int n)
11869 int k;
11871 if (!bmap)
11872 return -1;
11874 for (k = first; k < first + n; ++k) {
11875 isl_bool may_involve;
11877 if (isl_int_is_zero(c[k]))
11878 continue;
11879 may_involve = div_may_involve_output(bmap, k);
11880 if (may_involve < 0)
11881 return -1;
11882 if (may_involve)
11883 return k;
11886 return first + n;
11889 /* Look for a pair of inequality constraints in "bmap" of the form
11891 * -l + i >= 0 or i >= l
11892 * and
11893 * n + l - i >= 0 or i <= l + n
11895 * with n < "m" and i the output dimension at position "pos".
11896 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11897 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11898 * and earlier output dimensions, as well as integer divisions that do
11899 * not involve any of the output dimensions.
11901 * Return the index of the first inequality constraint or bmap->n_ineq
11902 * if no such pair can be found.
11904 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11905 int pos, isl_int m)
11907 int i, j;
11908 isl_ctx *ctx;
11909 isl_size total;
11910 isl_size n_div, n_out;
11911 unsigned o_div, o_out;
11912 int less;
11914 total = isl_basic_map_dim(bmap, isl_dim_all);
11915 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11916 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11917 if (total < 0 || n_out < 0 || n_div < 0)
11918 return -1;
11920 ctx = isl_basic_map_get_ctx(bmap);
11921 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11922 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11923 for (i = 0; i < bmap->n_ineq; ++i) {
11924 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11925 continue;
11926 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11927 n_out - (pos + 1)) != -1)
11928 continue;
11929 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11930 0, n_div) < n_div)
11931 continue;
11932 for (j = i + 1; j < bmap->n_ineq; ++j) {
11933 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11934 ctx->one))
11935 continue;
11936 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11937 bmap->ineq[j] + 1, total))
11938 continue;
11939 break;
11941 if (j >= bmap->n_ineq)
11942 continue;
11943 isl_int_add(bmap->ineq[i][0],
11944 bmap->ineq[i][0], bmap->ineq[j][0]);
11945 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11946 isl_int_sub(bmap->ineq[i][0],
11947 bmap->ineq[i][0], bmap->ineq[j][0]);
11948 if (!less)
11949 continue;
11950 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11951 return i;
11952 else
11953 return j;
11956 return bmap->n_ineq;
11959 /* Return the index of the equality of "bmap" that defines
11960 * the output dimension "pos" in terms of earlier dimensions.
11961 * The equality may also involve integer divisions, as long
11962 * as those integer divisions are defined in terms of
11963 * parameters or input dimensions.
11964 * In this case, *div is set to the number of integer divisions and
11965 * *ineq is set to the number of inequality constraints (provided
11966 * div and ineq are not NULL).
11968 * The equality may also involve a single integer division involving
11969 * the output dimensions (typically only output dimension "pos") as
11970 * long as the coefficient of output dimension "pos" is 1 or -1 and
11971 * there is a pair of constraints i >= l and i <= l + n, with i referring
11972 * to output dimension "pos", l an expression involving only earlier
11973 * dimensions and n smaller than the coefficient of the integer division
11974 * in the equality. In this case, the output dimension can be defined
11975 * in terms of a modulo expression that does not involve the integer division.
11976 * *div is then set to this single integer division and
11977 * *ineq is set to the index of constraint i >= l.
11979 * Return bmap->n_eq if there is no such equality.
11980 * Return -1 on error.
11982 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11983 int pos, int *div, int *ineq)
11985 int j, k, l;
11986 isl_size n_div, n_out;
11987 unsigned o_div, o_out;
11989 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11990 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11991 if (n_out < 0 || n_div < 0)
11992 return -1;
11994 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11995 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11997 if (ineq)
11998 *ineq = bmap->n_ineq;
11999 if (div)
12000 *div = n_div;
12001 for (j = 0; j < bmap->n_eq; ++j) {
12002 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12003 continue;
12004 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
12005 n_out - (pos + 1)) != -1)
12006 continue;
12007 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12008 0, n_div);
12009 if (k >= n_div)
12010 return j;
12011 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12012 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12013 continue;
12014 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12015 k + 1, n_div - (k+1)) < n_div)
12016 continue;
12017 l = find_modulo_constraint_pair(bmap, pos,
12018 bmap->eq[j][o_div + k]);
12019 if (l < 0)
12020 return -1;
12021 if (l >= bmap->n_ineq)
12022 continue;
12023 if (div)
12024 *div = k;
12025 if (ineq)
12026 *ineq = l;
12027 return j;
12030 return bmap->n_eq;
12033 /* Check if the given basic map is obviously single-valued.
12034 * In particular, for each output dimension, check that there is
12035 * an equality that defines the output dimension in terms of
12036 * earlier dimensions.
12038 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12040 int i;
12041 isl_size n_out;
12043 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12044 if (n_out < 0)
12045 return isl_bool_error;
12047 for (i = 0; i < n_out; ++i) {
12048 int eq;
12050 eq = isl_basic_map_output_defining_equality(bmap, i,
12051 NULL, NULL);
12052 if (eq < 0)
12053 return isl_bool_error;
12054 if (eq >= bmap->n_eq)
12055 return isl_bool_false;
12058 return isl_bool_true;
12061 /* Check if the given basic map is single-valued.
12062 * We simply compute
12064 * M \circ M^-1
12066 * and check if the result is a subset of the identity mapping.
12068 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12070 isl_space *space;
12071 isl_basic_map *test;
12072 isl_basic_map *id;
12073 isl_bool sv;
12075 sv = isl_basic_map_plain_is_single_valued(bmap);
12076 if (sv < 0 || sv)
12077 return sv;
12079 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12080 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12082 space = isl_basic_map_get_space(bmap);
12083 space = isl_space_map_from_set(isl_space_range(space));
12084 id = isl_basic_map_identity(space);
12086 sv = isl_basic_map_is_subset(test, id);
12088 isl_basic_map_free(test);
12089 isl_basic_map_free(id);
12091 return sv;
12094 /* Check if the given map is obviously single-valued.
12096 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12098 if (!map)
12099 return isl_bool_error;
12100 if (map->n == 0)
12101 return isl_bool_true;
12102 if (map->n >= 2)
12103 return isl_bool_false;
12105 return isl_basic_map_plain_is_single_valued(map->p[0]);
12108 /* Check if the given map is single-valued.
12109 * We simply compute
12111 * M \circ M^-1
12113 * and check if the result is a subset of the identity mapping.
12115 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12117 isl_space *space;
12118 isl_map *test;
12119 isl_map *id;
12120 isl_bool sv;
12122 sv = isl_map_plain_is_single_valued(map);
12123 if (sv < 0 || sv)
12124 return sv;
12126 test = isl_map_reverse(isl_map_copy(map));
12127 test = isl_map_apply_range(test, isl_map_copy(map));
12129 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12130 id = isl_map_identity(space);
12132 sv = isl_map_is_subset(test, id);
12134 isl_map_free(test);
12135 isl_map_free(id);
12137 return sv;
12140 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12142 isl_bool in;
12144 map = isl_map_copy(map);
12145 map = isl_map_reverse(map);
12146 in = isl_map_is_single_valued(map);
12147 isl_map_free(map);
12149 return in;
12152 /* Check if the given map is obviously injective.
12154 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12156 isl_bool in;
12158 map = isl_map_copy(map);
12159 map = isl_map_reverse(map);
12160 in = isl_map_plain_is_single_valued(map);
12161 isl_map_free(map);
12163 return in;
12166 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12168 isl_bool sv;
12170 sv = isl_map_is_single_valued(map);
12171 if (sv < 0 || !sv)
12172 return sv;
12174 return isl_map_is_injective(map);
12177 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12179 return isl_map_is_single_valued(set_to_map(set));
12182 /* Does "map" only map elements to themselves?
12184 * If the domain and range spaces are different, then "map"
12185 * is considered not to be an identity relation, even if it is empty.
12186 * Otherwise, construct the maximal identity relation and
12187 * check whether "map" is a subset of this relation.
12189 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12191 isl_map *id;
12192 isl_bool equal, is_identity;
12194 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12195 if (equal < 0 || !equal)
12196 return equal;
12198 id = isl_map_identity(isl_map_get_space(map));
12199 is_identity = isl_map_is_subset(map, id);
12200 isl_map_free(id);
12202 return is_identity;
12205 int isl_map_is_translation(__isl_keep isl_map *map)
12207 int ok;
12208 isl_set *delta;
12210 delta = isl_map_deltas(isl_map_copy(map));
12211 ok = isl_set_is_singleton(delta);
12212 isl_set_free(delta);
12214 return ok;
12217 static int unique(isl_int *p, unsigned pos, unsigned len)
12219 if (isl_seq_first_non_zero(p, pos) != -1)
12220 return 0;
12221 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12222 return 0;
12223 return 1;
12226 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12228 int i, j;
12229 isl_size nvar, ovar, n_div;
12231 n_div = isl_basic_set_dim(bset, isl_dim_div);
12232 if (n_div < 0)
12233 return isl_bool_error;
12234 if (n_div != 0)
12235 return isl_bool_false;
12237 nvar = isl_basic_set_dim(bset, isl_dim_set);
12238 ovar = isl_space_offset(bset->dim, isl_dim_set);
12239 if (nvar < 0 || ovar < 0)
12240 return isl_bool_error;
12241 for (j = 0; j < nvar; ++j) {
12242 int lower = 0, upper = 0;
12243 for (i = 0; i < bset->n_eq; ++i) {
12244 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12245 continue;
12246 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12247 return isl_bool_false;
12248 break;
12250 if (i < bset->n_eq)
12251 continue;
12252 for (i = 0; i < bset->n_ineq; ++i) {
12253 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12254 continue;
12255 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12256 return isl_bool_false;
12257 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12258 lower = 1;
12259 else
12260 upper = 1;
12262 if (!lower || !upper)
12263 return isl_bool_false;
12266 return isl_bool_true;
12269 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12271 if (!set)
12272 return isl_bool_error;
12273 if (set->n != 1)
12274 return isl_bool_false;
12276 return isl_basic_set_is_box(set->p[0]);
12279 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12281 if (!bset)
12282 return isl_bool_error;
12284 return isl_space_is_wrapping(bset->dim);
12287 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12289 if (!set)
12290 return isl_bool_error;
12292 return isl_space_is_wrapping(set->dim);
12295 /* Modify the space of "map" through a call to "change".
12296 * If "can_change" is set (not NULL), then first call it to check
12297 * if the modification is allowed, printing the error message "cannot_change"
12298 * if it is not.
12300 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12301 isl_bool (*can_change)(__isl_keep isl_map *map),
12302 const char *cannot_change,
12303 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12305 isl_bool ok;
12306 isl_space *space;
12308 if (!map)
12309 return NULL;
12311 ok = can_change ? can_change(map) : isl_bool_true;
12312 if (ok < 0)
12313 return isl_map_free(map);
12314 if (!ok)
12315 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12316 return isl_map_free(map));
12318 space = change(isl_map_get_space(map));
12319 map = isl_map_reset_space(map, space);
12321 return map;
12324 /* Is the domain of "map" a wrapped relation?
12326 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12328 if (!map)
12329 return isl_bool_error;
12331 return isl_space_domain_is_wrapping(map->dim);
12334 /* Does "map" have a wrapped relation in both domain and range?
12336 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12338 return isl_space_is_product(isl_map_peek_space(map));
12341 /* Is the range of "map" a wrapped relation?
12343 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12345 if (!map)
12346 return isl_bool_error;
12348 return isl_space_range_is_wrapping(map->dim);
12351 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12353 isl_space *space;
12355 space = isl_basic_map_take_space(bmap);
12356 space = isl_space_wrap(space);
12357 bmap = isl_basic_map_restore_space(bmap, space);
12359 bmap = isl_basic_map_finalize(bmap);
12361 return bset_from_bmap(bmap);
12364 /* Given a map A -> B, return the set (A -> B).
12366 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12368 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12371 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12373 bset = isl_basic_set_cow(bset);
12374 if (!bset)
12375 return NULL;
12377 bset->dim = isl_space_unwrap(bset->dim);
12378 if (!bset->dim)
12379 goto error;
12381 bset = isl_basic_set_finalize(bset);
12383 return bset_to_bmap(bset);
12384 error:
12385 isl_basic_set_free(bset);
12386 return NULL;
12389 /* Given a set (A -> B), return the map A -> B.
12390 * Error out if "set" is not of the form (A -> B).
12392 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12394 return isl_map_change_space(set, &isl_set_is_wrapping,
12395 "not a wrapping set", &isl_space_unwrap);
12398 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12399 enum isl_dim_type type)
12401 isl_space *space;
12403 space = isl_basic_map_take_space(bmap);
12404 space = isl_space_reset(space, type);
12405 bmap = isl_basic_map_restore_space(bmap, space);
12407 bmap = isl_basic_map_mark_final(bmap);
12409 return bmap;
12412 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12413 enum isl_dim_type type)
12415 int i;
12416 isl_space *space;
12418 if (!map)
12419 return NULL;
12421 if (!isl_space_is_named_or_nested(map->dim, type))
12422 return map;
12424 map = isl_map_cow(map);
12425 if (!map)
12426 return NULL;
12428 for (i = 0; i < map->n; ++i) {
12429 map->p[i] = isl_basic_map_reset(map->p[i], type);
12430 if (!map->p[i])
12431 goto error;
12434 space = isl_map_take_space(map);
12435 space = isl_space_reset(space, type);
12436 map = isl_map_restore_space(map, space);
12438 return map;
12439 error:
12440 isl_map_free(map);
12441 return NULL;
12444 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12446 isl_space *space;
12448 space = isl_basic_map_take_space(bmap);
12449 space = isl_space_flatten(space);
12450 bmap = isl_basic_map_restore_space(bmap, space);
12452 bmap = isl_basic_map_mark_final(bmap);
12454 return bmap;
12457 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12459 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12462 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12463 __isl_take isl_basic_map *bmap)
12465 isl_space *space;
12467 space = isl_basic_map_take_space(bmap);
12468 space = isl_space_flatten_domain(space);
12469 bmap = isl_basic_map_restore_space(bmap, space);
12471 bmap = isl_basic_map_mark_final(bmap);
12473 return bmap;
12476 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12477 __isl_take isl_basic_map *bmap)
12479 isl_space *space;
12481 space = isl_basic_map_take_space(bmap);
12482 space = isl_space_flatten_range(space);
12483 bmap = isl_basic_map_restore_space(bmap, space);
12485 bmap = isl_basic_map_mark_final(bmap);
12487 return bmap;
12490 /* Remove any internal structure from the spaces of domain and range of "map".
12492 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12494 if (!map)
12495 return NULL;
12497 if (!map->dim->nested[0] && !map->dim->nested[1])
12498 return map;
12500 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12503 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12505 return set_from_map(isl_map_flatten(set_to_map(set)));
12508 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12510 isl_space *space, *flat_space;
12511 isl_map *map;
12513 space = isl_set_get_space(set);
12514 flat_space = isl_space_flatten(isl_space_copy(space));
12515 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12516 flat_space));
12517 map = isl_map_intersect_domain(map, set);
12519 return map;
12522 /* Remove any internal structure from the space of the domain of "map".
12524 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12526 if (!map)
12527 return NULL;
12529 if (!map->dim->nested[0])
12530 return map;
12532 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12535 /* Remove any internal structure from the space of the range of "map".
12537 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12539 if (!map)
12540 return NULL;
12542 if (!map->dim->nested[1])
12543 return map;
12545 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12548 /* Reorder the dimensions of "bmap" according to the given dim_map
12549 * and set the dimension specification to "space" and
12550 * perform Gaussian elimination on the result.
12552 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12553 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12555 isl_basic_map *res;
12556 unsigned flags;
12557 isl_size n_div;
12559 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12560 if (n_div < 0 || !space || !dim_map)
12561 goto error;
12563 flags = bmap->flags;
12564 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12565 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12566 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12567 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12568 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12569 if (res)
12570 res->flags = flags;
12571 res = isl_basic_map_gauss(res, NULL);
12572 res = isl_basic_map_finalize(res);
12573 return res;
12574 error:
12575 isl_dim_map_free(dim_map);
12576 isl_basic_map_free(bmap);
12577 isl_space_free(space);
12578 return NULL;
12581 /* Reorder the dimensions of "map" according to given reordering.
12583 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12584 __isl_take isl_reordering *r)
12586 int i;
12587 struct isl_dim_map *dim_map;
12589 map = isl_map_cow(map);
12590 dim_map = isl_dim_map_from_reordering(r);
12591 if (!map || !r || !dim_map)
12592 goto error;
12594 for (i = 0; i < map->n; ++i) {
12595 struct isl_dim_map *dim_map_i;
12596 isl_space *space;
12598 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12600 space = isl_reordering_get_space(r);
12601 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12603 if (!map->p[i])
12604 goto error;
12607 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12608 map = isl_map_unmark_normalized(map);
12610 isl_reordering_free(r);
12611 isl_dim_map_free(dim_map);
12612 return map;
12613 error:
12614 isl_dim_map_free(dim_map);
12615 isl_map_free(map);
12616 isl_reordering_free(r);
12617 return NULL;
12620 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12621 __isl_take isl_reordering *r)
12623 return set_from_map(isl_map_realign(set_to_map(set), r));
12626 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12627 __isl_take isl_space *model)
12629 isl_ctx *ctx;
12630 isl_bool aligned;
12632 if (!map || !model)
12633 goto error;
12635 ctx = isl_space_get_ctx(model);
12636 if (!isl_space_has_named_params(model))
12637 isl_die(ctx, isl_error_invalid,
12638 "model has unnamed parameters", goto error);
12639 if (isl_map_check_named_params(map) < 0)
12640 goto error;
12641 aligned = isl_map_space_has_equal_params(map, model);
12642 if (aligned < 0)
12643 goto error;
12644 if (!aligned) {
12645 isl_space *space;
12646 isl_reordering *exp;
12648 space = isl_map_peek_space(map);
12649 exp = isl_parameter_alignment_reordering(space, model);
12650 map = isl_map_realign(map, exp);
12653 isl_space_free(model);
12654 return map;
12655 error:
12656 isl_space_free(model);
12657 isl_map_free(map);
12658 return NULL;
12661 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12662 __isl_take isl_space *model)
12664 return isl_map_align_params(set, model);
12667 /* Align the parameters of "bmap" to those of "model", introducing
12668 * additional parameters if needed.
12670 __isl_give isl_basic_map *isl_basic_map_align_params(
12671 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12673 isl_ctx *ctx;
12674 isl_bool equal_params;
12675 isl_space *bmap_space;
12677 if (!bmap || !model)
12678 goto error;
12680 ctx = isl_space_get_ctx(model);
12681 if (!isl_space_has_named_params(model))
12682 isl_die(ctx, isl_error_invalid,
12683 "model has unnamed parameters", goto error);
12684 if (isl_basic_map_check_named_params(bmap) < 0)
12685 goto error;
12686 bmap_space = isl_basic_map_peek_space(bmap);
12687 equal_params = isl_space_has_equal_params(bmap_space, model);
12688 if (equal_params < 0)
12689 goto error;
12690 if (!equal_params) {
12691 isl_reordering *exp;
12692 struct isl_dim_map *dim_map;
12694 exp = isl_parameter_alignment_reordering(bmap_space, model);
12695 dim_map = isl_dim_map_from_reordering(exp);
12696 bmap = isl_basic_map_realign(bmap,
12697 isl_reordering_get_space(exp),
12698 isl_dim_map_extend(dim_map, bmap));
12699 isl_reordering_free(exp);
12700 isl_dim_map_free(dim_map);
12703 isl_space_free(model);
12704 return bmap;
12705 error:
12706 isl_space_free(model);
12707 isl_basic_map_free(bmap);
12708 return NULL;
12711 /* Do "bset" and "space" have the same parameters?
12713 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12714 __isl_keep isl_space *space)
12716 isl_space *bset_space;
12718 bset_space = isl_basic_set_peek_space(bset);
12719 return isl_space_has_equal_params(bset_space, space);
12722 /* Do "map" and "space" have the same parameters?
12724 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12725 __isl_keep isl_space *space)
12727 isl_space *map_space;
12729 map_space = isl_map_peek_space(map);
12730 return isl_space_has_equal_params(map_space, space);
12733 /* Do "set" and "space" have the same parameters?
12735 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12736 __isl_keep isl_space *space)
12738 return isl_map_space_has_equal_params(set_to_map(set), space);
12741 /* Align the parameters of "bset" to those of "model", introducing
12742 * additional parameters if needed.
12744 __isl_give isl_basic_set *isl_basic_set_align_params(
12745 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12747 return isl_basic_map_align_params(bset, model);
12750 #undef TYPE
12751 #define TYPE isl_map
12752 #define isl_map_drop_dims isl_map_drop
12753 #include "isl_drop_unused_params_templ.c"
12755 /* Drop all parameters not referenced by "set".
12757 __isl_give isl_set *isl_set_drop_unused_params(
12758 __isl_take isl_set *set)
12760 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12763 #undef TYPE
12764 #define TYPE isl_basic_map
12765 #define isl_basic_map_drop_dims isl_basic_map_drop
12766 #include "isl_drop_unused_params_templ.c"
12768 /* Drop all parameters not referenced by "bset".
12770 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12771 __isl_take isl_basic_set *bset)
12773 return bset_from_bmap(isl_basic_map_drop_unused_params(
12774 bset_to_bmap(bset)));
12777 /* Given a tuple of identifiers "tuple" in a space that corresponds
12778 * to that of "set", if any of those identifiers appear as parameters
12779 * in "set", then equate those parameters with the corresponding
12780 * set dimensions and project out the parameters.
12781 * The result therefore has no such parameters.
12783 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12784 __isl_keep isl_multi_id *tuple)
12786 int i;
12787 isl_size n;
12788 isl_space *set_space, *tuple_space;
12790 set_space = isl_set_peek_space(set);
12791 tuple_space = isl_multi_id_peek_space(tuple);
12792 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12793 return isl_set_free(set);
12794 n = isl_multi_id_size(tuple);
12795 if (n < 0)
12796 return isl_set_free(set);
12797 for (i = 0; i < n; ++i) {
12798 isl_id *id;
12799 int pos;
12801 id = isl_multi_id_get_at(tuple, i);
12802 if (!id)
12803 return isl_set_free(set);
12804 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12805 isl_id_free(id);
12806 if (pos < 0)
12807 continue;
12808 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12809 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12811 return set;
12814 /* Bind the set dimensions of "set" to parameters with identifiers
12815 * specified by "tuple", living in the same space as "set".
12817 * If no parameters with these identifiers appear in "set" already,
12818 * then the set dimensions are simply reinterpreted as parameters.
12819 * Otherwise, the parameters are first equated to the corresponding
12820 * set dimensions.
12822 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12823 __isl_take isl_multi_id *tuple)
12825 isl_space *space;
12827 set = equate_params(set, tuple);
12828 space = isl_set_get_space(set);
12829 space = isl_space_bind_set(space, tuple);
12830 isl_multi_id_free(tuple);
12831 set = isl_set_reset_space(set, space);
12833 return set;
12836 /* Given a tuple of identifiers "tuple" in a space that corresponds
12837 * to the domain of "map", if any of those identifiers appear as parameters
12838 * in "map", then equate those parameters with the corresponding
12839 * input dimensions and project out the parameters.
12840 * The result therefore has no such parameters.
12842 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12843 __isl_keep isl_multi_id *tuple)
12845 int i;
12846 isl_size n;
12847 isl_space *map_space, *tuple_space;
12849 map_space = isl_map_peek_space(map);
12850 tuple_space = isl_multi_id_peek_space(tuple);
12851 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12852 return isl_map_free(map);
12853 n = isl_multi_id_size(tuple);
12854 if (n < 0)
12855 return isl_map_free(map);
12856 for (i = 0; i < n; ++i) {
12857 isl_id *id;
12858 int pos;
12860 id = isl_multi_id_get_at(tuple, i);
12861 if (!id)
12862 return isl_map_free(map);
12863 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12864 isl_id_free(id);
12865 if (pos < 0)
12866 continue;
12867 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12868 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12870 return map;
12873 /* Bind the input dimensions of "map" to parameters with identifiers
12874 * specified by "tuple", living in the domain space of "map".
12876 * If no parameters with these identifiers appear in "map" already,
12877 * then the input dimensions are simply reinterpreted as parameters.
12878 * Otherwise, the parameters are first equated to the corresponding
12879 * input dimensions.
12881 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12882 __isl_take isl_multi_id *tuple)
12884 isl_space *space;
12885 isl_set *set;
12887 map = map_equate_params(map, tuple);
12888 space = isl_map_get_space(map);
12889 space = isl_space_bind_map_domain(space, tuple);
12890 isl_multi_id_free(tuple);
12891 set = set_from_map(isl_map_reset_space(map, space));
12893 return set;
12896 /* Bind the output dimensions of "map" to parameters with identifiers
12897 * specified by "tuple", living in the range space of "map".
12899 * Since binding is more easily implemented on the domain,
12900 * bind the input dimensions of the inverse of "map".
12902 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12903 __isl_take isl_multi_id *tuple)
12905 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12908 /* Insert a domain corresponding to "tuple"
12909 * into the nullary or unary relation "set".
12910 * The result has an extra initial tuple and is therefore
12911 * either a unary or binary relation.
12912 * Any parameters with identifiers in "tuple" are reinterpreted
12913 * as the corresponding domain dimensions.
12915 static __isl_give isl_map *unbind_params_insert_domain(
12916 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12918 isl_space *space;
12919 isl_reordering *r;
12921 space = isl_set_peek_space(set);
12922 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12923 isl_multi_id_free(tuple);
12925 return isl_map_realign(set_to_map(set), r);
12928 /* Construct a set with "tuple" as domain from the parameter domain "set".
12929 * Any parameters with identifiers in "tuple" are reinterpreted
12930 * as the corresponding set dimensions.
12932 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12933 __isl_take isl_multi_id *tuple)
12935 isl_bool is_params;
12937 is_params = isl_set_is_params(set);
12938 if (is_params < 0)
12939 set = isl_set_free(set);
12940 else if (!is_params)
12941 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12942 "expecting parameter domain", set = isl_set_free(set));
12943 return set_from_map(unbind_params_insert_domain(set, tuple));
12946 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12948 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12950 isl_bool is_params;
12952 is_params = isl_set_is_params(set);
12953 if (is_params < 0)
12954 return isl_stat_error;
12955 else if (is_params)
12956 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12957 "expecting proper set", return isl_stat_error);
12959 return isl_stat_ok;
12962 /* Construct a map with "domain" as domain and "set" as range.
12963 * Any parameters with identifiers in "domain" are reinterpreted
12964 * as the corresponding domain dimensions.
12966 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12967 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12969 if (isl_set_check_is_set(set) < 0)
12970 set = isl_set_free(set);
12971 return unbind_params_insert_domain(set, domain);
12974 /* Construct a map with "domain" as domain and "set" as range.
12976 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12977 __isl_take isl_space *domain)
12979 isl_size dim;
12980 isl_space *space;
12981 isl_map *map;
12983 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12984 domain = isl_space_free(domain);
12985 dim = isl_space_dim(domain, isl_dim_set);
12986 if (dim < 0)
12987 domain = isl_space_free(domain);
12988 space = isl_set_get_space(set);
12989 domain = isl_space_replace_params(domain, space);
12990 space = isl_space_map_from_domain_and_range(domain, space);
12992 map = isl_map_from_range(set);
12993 map = isl_map_add_dims(map, isl_dim_in, dim);
12994 map = isl_map_reset_space(map, space);
12996 return map;
12999 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13000 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13001 enum isl_dim_type c2, enum isl_dim_type c3,
13002 enum isl_dim_type c4, enum isl_dim_type c5)
13004 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13005 struct isl_mat *mat;
13006 int i, j, k;
13007 int pos;
13008 isl_size total;
13010 total = isl_basic_map_dim(bmap, isl_dim_all);
13011 if (total < 0)
13012 return NULL;
13013 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13014 if (!mat)
13015 return NULL;
13016 for (i = 0; i < bmap->n_eq; ++i)
13017 for (j = 0, pos = 0; j < 5; ++j) {
13018 int off = isl_basic_map_offset(bmap, c[j]);
13019 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13020 if (dim < 0)
13021 return isl_mat_free(mat);
13022 for (k = 0; k < dim; ++k) {
13023 isl_int_set(mat->row[i][pos],
13024 bmap->eq[i][off + k]);
13025 ++pos;
13029 return mat;
13032 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13033 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13034 enum isl_dim_type c2, enum isl_dim_type c3,
13035 enum isl_dim_type c4, enum isl_dim_type c5)
13037 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13038 struct isl_mat *mat;
13039 int i, j, k;
13040 int pos;
13041 isl_size total;
13043 total = isl_basic_map_dim(bmap, isl_dim_all);
13044 if (total < 0)
13045 return NULL;
13046 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13047 if (!mat)
13048 return NULL;
13049 for (i = 0; i < bmap->n_ineq; ++i)
13050 for (j = 0, pos = 0; j < 5; ++j) {
13051 int off = isl_basic_map_offset(bmap, c[j]);
13052 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13053 if (dim < 0)
13054 return isl_mat_free(mat);
13055 for (k = 0; k < dim; ++k) {
13056 isl_int_set(mat->row[i][pos],
13057 bmap->ineq[i][off + k]);
13058 ++pos;
13062 return mat;
13065 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13066 __isl_take isl_space *space,
13067 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13068 enum isl_dim_type c2, enum isl_dim_type c3,
13069 enum isl_dim_type c4, enum isl_dim_type c5)
13071 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13072 isl_basic_map *bmap = NULL;
13073 isl_size dim;
13074 unsigned total;
13075 unsigned extra;
13076 int i, j, k, l;
13077 int pos;
13079 dim = isl_space_dim(space, isl_dim_all);
13080 if (dim < 0 || !eq || !ineq)
13081 goto error;
13083 if (eq->n_col != ineq->n_col)
13084 isl_die(space->ctx, isl_error_invalid,
13085 "equalities and inequalities matrices should have "
13086 "same number of columns", goto error);
13088 total = 1 + dim;
13090 if (eq->n_col < total)
13091 isl_die(space->ctx, isl_error_invalid,
13092 "number of columns too small", goto error);
13094 extra = eq->n_col - total;
13096 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13097 eq->n_row, ineq->n_row);
13098 if (!bmap)
13099 goto error;
13100 for (i = 0; i < extra; ++i) {
13101 k = isl_basic_map_alloc_div(bmap);
13102 if (k < 0)
13103 goto error;
13104 isl_int_set_si(bmap->div[k][0], 0);
13106 for (i = 0; i < eq->n_row; ++i) {
13107 l = isl_basic_map_alloc_equality(bmap);
13108 if (l < 0)
13109 goto error;
13110 for (j = 0, pos = 0; j < 5; ++j) {
13111 int off = isl_basic_map_offset(bmap, c[j]);
13112 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13113 if (dim < 0)
13114 goto error;
13115 for (k = 0; k < dim; ++k) {
13116 isl_int_set(bmap->eq[l][off + k],
13117 eq->row[i][pos]);
13118 ++pos;
13122 for (i = 0; i < ineq->n_row; ++i) {
13123 l = isl_basic_map_alloc_inequality(bmap);
13124 if (l < 0)
13125 goto error;
13126 for (j = 0, pos = 0; j < 5; ++j) {
13127 int off = isl_basic_map_offset(bmap, c[j]);
13128 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13129 if (dim < 0)
13130 goto error;
13131 for (k = 0; k < dim; ++k) {
13132 isl_int_set(bmap->ineq[l][off + k],
13133 ineq->row[i][pos]);
13134 ++pos;
13139 isl_space_free(space);
13140 isl_mat_free(eq);
13141 isl_mat_free(ineq);
13143 bmap = isl_basic_map_simplify(bmap);
13144 return isl_basic_map_finalize(bmap);
13145 error:
13146 isl_space_free(space);
13147 isl_mat_free(eq);
13148 isl_mat_free(ineq);
13149 isl_basic_map_free(bmap);
13150 return NULL;
13153 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13154 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13155 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13157 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13158 c1, c2, c3, c4, isl_dim_in);
13161 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13162 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13163 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13165 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13166 c1, c2, c3, c4, isl_dim_in);
13169 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13170 __isl_take isl_space *space,
13171 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13172 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13174 isl_basic_map *bmap;
13175 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13176 c1, c2, c3, c4, isl_dim_in);
13177 return bset_from_bmap(bmap);
13180 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13182 if (!bmap)
13183 return isl_bool_error;
13185 return isl_space_can_zip(bmap->dim);
13188 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13190 if (!map)
13191 return isl_bool_error;
13193 return isl_space_can_zip(map->dim);
13196 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13197 * (A -> C) -> (B -> D).
13199 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13201 unsigned pos;
13202 isl_size n_in;
13203 isl_size n1;
13204 isl_size n2;
13206 if (!bmap)
13207 return NULL;
13209 if (!isl_basic_map_can_zip(bmap))
13210 isl_die(bmap->ctx, isl_error_invalid,
13211 "basic map cannot be zipped", goto error);
13212 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13213 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13214 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13215 if (n_in < 0 || n1 < 0 || n2 < 0)
13216 return isl_basic_map_free(bmap);
13217 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13218 bmap = isl_basic_map_cow(bmap);
13219 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13220 if (!bmap)
13221 return NULL;
13222 bmap->dim = isl_space_zip(bmap->dim);
13223 if (!bmap->dim)
13224 goto error;
13225 bmap = isl_basic_map_mark_final(bmap);
13226 return bmap;
13227 error:
13228 isl_basic_map_free(bmap);
13229 return NULL;
13232 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13233 * (A -> C) -> (B -> D).
13235 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13237 if (!map)
13238 return NULL;
13240 if (!isl_map_can_zip(map))
13241 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13242 goto error);
13244 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13245 error:
13246 isl_map_free(map);
13247 return NULL;
13250 /* Can we apply isl_basic_map_curry to "bmap"?
13251 * That is, does it have a nested relation in its domain?
13253 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13255 if (!bmap)
13256 return isl_bool_error;
13258 return isl_space_can_curry(bmap->dim);
13261 /* Can we apply isl_map_curry to "map"?
13262 * That is, does it have a nested relation in its domain?
13264 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13266 if (!map)
13267 return isl_bool_error;
13269 return isl_space_can_curry(map->dim);
13272 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13273 * A -> (B -> C).
13275 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13278 if (!bmap)
13279 return NULL;
13281 if (!isl_basic_map_can_curry(bmap))
13282 isl_die(bmap->ctx, isl_error_invalid,
13283 "basic map cannot be curried", goto error);
13284 bmap = isl_basic_map_cow(bmap);
13285 if (!bmap)
13286 return NULL;
13287 bmap->dim = isl_space_curry(bmap->dim);
13288 if (!bmap->dim)
13289 goto error;
13290 bmap = isl_basic_map_mark_final(bmap);
13291 return bmap;
13292 error:
13293 isl_basic_map_free(bmap);
13294 return NULL;
13297 /* Given a map (A -> B) -> C, return the corresponding map
13298 * A -> (B -> C).
13300 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13302 return isl_map_change_space(map, &isl_map_can_curry,
13303 "map cannot be curried", &isl_space_curry);
13306 /* Can isl_map_range_curry be applied to "map"?
13307 * That is, does it have a nested relation in its range,
13308 * the domain of which is itself a nested relation?
13310 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13312 if (!map)
13313 return isl_bool_error;
13315 return isl_space_can_range_curry(map->dim);
13318 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13319 * A -> (B -> (C -> D)).
13321 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13323 return isl_map_change_space(map, &isl_map_can_range_curry,
13324 "map range cannot be curried",
13325 &isl_space_range_curry);
13328 /* Can we apply isl_basic_map_uncurry to "bmap"?
13329 * That is, does it have a nested relation in its domain?
13331 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13333 if (!bmap)
13334 return isl_bool_error;
13336 return isl_space_can_uncurry(bmap->dim);
13339 /* Can we apply isl_map_uncurry to "map"?
13340 * That is, does it have a nested relation in its domain?
13342 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13344 if (!map)
13345 return isl_bool_error;
13347 return isl_space_can_uncurry(map->dim);
13350 /* Given a basic map A -> (B -> C), return the corresponding basic map
13351 * (A -> B) -> C.
13353 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13356 if (!bmap)
13357 return NULL;
13359 if (!isl_basic_map_can_uncurry(bmap))
13360 isl_die(bmap->ctx, isl_error_invalid,
13361 "basic map cannot be uncurried",
13362 return isl_basic_map_free(bmap));
13363 bmap = isl_basic_map_cow(bmap);
13364 if (!bmap)
13365 return NULL;
13366 bmap->dim = isl_space_uncurry(bmap->dim);
13367 if (!bmap->dim)
13368 return isl_basic_map_free(bmap);
13369 bmap = isl_basic_map_mark_final(bmap);
13370 return bmap;
13373 /* Given a map A -> (B -> C), return the corresponding map
13374 * (A -> B) -> C.
13376 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13378 return isl_map_change_space(map, &isl_map_can_uncurry,
13379 "map cannot be uncurried", &isl_space_uncurry);
13382 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13383 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13385 return isl_map_equate(set, type1, pos1, type2, pos2);
13388 /* Construct a basic map where the given dimensions are equal to each other.
13390 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13391 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13393 isl_basic_map *bmap = NULL;
13394 int i;
13395 isl_size total;
13397 total = isl_space_dim(space, isl_dim_all);
13398 if (total < 0 ||
13399 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13400 isl_space_check_range(space, type2, pos2, 1) < 0)
13401 goto error;
13403 if (type1 == type2 && pos1 == pos2)
13404 return isl_basic_map_universe(space);
13406 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13407 i = isl_basic_map_alloc_equality(bmap);
13408 if (i < 0)
13409 goto error;
13410 isl_seq_clr(bmap->eq[i], 1 + total);
13411 pos1 += isl_basic_map_offset(bmap, type1);
13412 pos2 += isl_basic_map_offset(bmap, type2);
13413 isl_int_set_si(bmap->eq[i][pos1], -1);
13414 isl_int_set_si(bmap->eq[i][pos2], 1);
13415 bmap = isl_basic_map_finalize(bmap);
13416 isl_space_free(space);
13417 return bmap;
13418 error:
13419 isl_space_free(space);
13420 isl_basic_map_free(bmap);
13421 return NULL;
13424 /* Add a constraint imposing that the given two dimensions are equal.
13426 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13427 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13429 isl_basic_map *eq;
13431 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13433 bmap = isl_basic_map_intersect(bmap, eq);
13435 return bmap;
13438 /* Add a constraint imposing that the given two dimensions are equal.
13440 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13441 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13443 isl_basic_map *bmap;
13445 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13447 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13449 return map;
13452 /* Add a constraint imposing that the given two dimensions have opposite values.
13454 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13455 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13457 isl_basic_map *bmap = NULL;
13458 int i;
13459 isl_size total;
13461 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13462 return isl_map_free(map);
13463 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13464 return isl_map_free(map);
13466 total = isl_map_dim(map, isl_dim_all);
13467 if (total < 0)
13468 return isl_map_free(map);
13469 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13470 i = isl_basic_map_alloc_equality(bmap);
13471 if (i < 0)
13472 goto error;
13473 isl_seq_clr(bmap->eq[i], 1 + total);
13474 pos1 += isl_basic_map_offset(bmap, type1);
13475 pos2 += isl_basic_map_offset(bmap, type2);
13476 isl_int_set_si(bmap->eq[i][pos1], 1);
13477 isl_int_set_si(bmap->eq[i][pos2], 1);
13478 bmap = isl_basic_map_finalize(bmap);
13480 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13482 return map;
13483 error:
13484 isl_basic_map_free(bmap);
13485 isl_map_free(map);
13486 return NULL;
13489 /* Construct a constraint imposing that the value of the first dimension is
13490 * greater than or equal to that of the second.
13492 static __isl_give isl_constraint *constraint_order_ge(
13493 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13494 enum isl_dim_type type2, int pos2)
13496 isl_constraint *c;
13498 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13499 isl_space_check_range(space, type2, pos2, 1) < 0)
13500 space = isl_space_free(space);
13501 if (!space)
13502 return NULL;
13504 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13506 if (type1 == type2 && pos1 == pos2)
13507 return c;
13509 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13510 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13512 return c;
13515 /* Add a constraint imposing that the value of the first dimension is
13516 * greater than or equal to that of the second.
13518 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13519 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13521 isl_constraint *c;
13522 isl_space *space;
13524 if (type1 == type2 && pos1 == pos2)
13525 return bmap;
13526 space = isl_basic_map_get_space(bmap);
13527 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13528 bmap = isl_basic_map_add_constraint(bmap, c);
13530 return bmap;
13533 /* Add a constraint imposing that the value of the first dimension is
13534 * greater than or equal to that of the second.
13536 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13537 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13539 isl_constraint *c;
13540 isl_space *space;
13542 if (type1 == type2 && pos1 == pos2)
13543 return map;
13544 space = isl_map_get_space(map);
13545 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13546 map = isl_map_add_constraint(map, c);
13548 return map;
13551 /* Add a constraint imposing that the value of the first dimension is
13552 * less than or equal to that of the second.
13554 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13555 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13557 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13560 /* Construct a basic map where the value of the first dimension is
13561 * greater than that of the second.
13563 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13564 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13566 isl_basic_map *bmap = NULL;
13567 int i;
13568 isl_size total;
13570 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13571 isl_space_check_range(space, type2, pos2, 1) < 0)
13572 goto error;
13574 if (type1 == type2 && pos1 == pos2)
13575 return isl_basic_map_empty(space);
13577 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13578 total = isl_basic_map_dim(bmap, isl_dim_all);
13579 i = isl_basic_map_alloc_inequality(bmap);
13580 if (total < 0 || i < 0)
13581 return isl_basic_map_free(bmap);
13582 isl_seq_clr(bmap->ineq[i], 1 + total);
13583 pos1 += isl_basic_map_offset(bmap, type1);
13584 pos2 += isl_basic_map_offset(bmap, type2);
13585 isl_int_set_si(bmap->ineq[i][pos1], 1);
13586 isl_int_set_si(bmap->ineq[i][pos2], -1);
13587 isl_int_set_si(bmap->ineq[i][0], -1);
13588 bmap = isl_basic_map_finalize(bmap);
13590 return bmap;
13591 error:
13592 isl_space_free(space);
13593 isl_basic_map_free(bmap);
13594 return NULL;
13597 /* Add a constraint imposing that the value of the first dimension is
13598 * greater than that of the second.
13600 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13601 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13603 isl_basic_map *gt;
13605 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13607 bmap = isl_basic_map_intersect(bmap, gt);
13609 return bmap;
13612 /* Add a constraint imposing that the value of the first dimension is
13613 * greater than that of the second.
13615 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13616 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13618 isl_basic_map *bmap;
13620 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13622 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13624 return map;
13627 /* Add a constraint imposing that the value of the first dimension is
13628 * smaller than that of the second.
13630 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13631 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13633 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13636 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13637 int pos)
13639 isl_aff *div;
13640 isl_local_space *ls;
13642 if (!bmap)
13643 return NULL;
13645 if (!isl_basic_map_divs_known(bmap))
13646 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13647 "some divs are unknown", return NULL);
13649 ls = isl_basic_map_get_local_space(bmap);
13650 div = isl_local_space_get_div(ls, pos);
13651 isl_local_space_free(ls);
13653 return div;
13656 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13657 int pos)
13659 return isl_basic_map_get_div(bset, pos);
13662 /* Plug in "subs" for set dimension "pos" of "set".
13664 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13665 unsigned pos, __isl_keep isl_aff *subs)
13667 isl_multi_aff *ma;
13669 if (set && isl_set_plain_is_empty(set))
13670 return set;
13672 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13673 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13674 return isl_set_preimage_multi_aff(set, ma);
13677 /* Check if the range of "ma" is compatible with the domain or range
13678 * (depending on "type") of "bmap".
13680 static isl_stat check_basic_map_compatible_range_multi_aff(
13681 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13682 __isl_keep isl_multi_aff *ma)
13684 isl_bool m;
13685 isl_space *ma_space;
13687 ma_space = isl_multi_aff_get_space(ma);
13689 m = isl_space_has_equal_params(bmap->dim, ma_space);
13690 if (m < 0)
13691 goto error;
13692 if (!m)
13693 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13694 "parameters don't match", goto error);
13695 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13696 if (m < 0)
13697 goto error;
13698 if (!m)
13699 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13700 "spaces don't match", goto error);
13702 isl_space_free(ma_space);
13703 return isl_stat_ok;
13704 error:
13705 isl_space_free(ma_space);
13706 return isl_stat_error;
13709 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13710 * coefficients before the transformed range of dimensions,
13711 * the "n_after" coefficients after the transformed range of dimensions
13712 * and the coefficients of the other divs in "bmap".
13714 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13715 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13717 int i;
13718 isl_size n_param;
13719 isl_size n_set;
13720 isl_local_space *ls;
13722 if (n_div == 0)
13723 return bmap;
13725 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13726 n_param = isl_local_space_dim(ls, isl_dim_param);
13727 n_set = isl_local_space_dim(ls, isl_dim_set);
13728 if (n_param < 0 || n_set < 0)
13729 return isl_basic_map_free(bmap);
13731 for (i = 0; i < n_div; ++i) {
13732 int o_bmap = 0, o_ls = 0;
13734 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13735 o_bmap += 1 + 1 + n_param;
13736 o_ls += 1 + 1 + n_param;
13737 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13738 o_bmap += n_before;
13739 isl_seq_cpy(bmap->div[i] + o_bmap,
13740 ls->div->row[i] + o_ls, n_set);
13741 o_bmap += n_set;
13742 o_ls += n_set;
13743 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13744 o_bmap += n_after;
13745 isl_seq_cpy(bmap->div[i] + o_bmap,
13746 ls->div->row[i] + o_ls, n_div);
13747 o_bmap += n_div;
13748 o_ls += n_div;
13749 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13750 bmap = isl_basic_map_add_div_constraints(bmap, i);
13751 if (!bmap)
13752 goto error;
13755 isl_local_space_free(ls);
13756 return bmap;
13757 error:
13758 isl_local_space_free(ls);
13759 return isl_basic_map_free(bmap);
13762 /* How many stride constraints does "ma" enforce?
13763 * That is, how many of the affine expressions have a denominator
13764 * different from one?
13766 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13768 int i;
13769 int strides = 0;
13771 for (i = 0; i < ma->n; ++i)
13772 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13773 strides++;
13775 return strides;
13778 /* For each affine expression in ma of the form
13780 * x_i = (f_i y + h_i)/m_i
13782 * with m_i different from one, add a constraint to "bmap"
13783 * of the form
13785 * f_i y + h_i = m_i alpha_i
13787 * with alpha_i an additional existentially quantified variable.
13789 * The input variables of "ma" correspond to a subset of the variables
13790 * of "bmap". There are "n_before" variables in "bmap" before this
13791 * subset and "n_after" variables after this subset.
13792 * The integer divisions of the affine expressions in "ma" are assumed
13793 * to have been aligned. There are "n_div_ma" of them and
13794 * they appear first in "bmap", straight after the "n_after" variables.
13796 static __isl_give isl_basic_map *add_ma_strides(
13797 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13798 int n_before, int n_after, int n_div_ma)
13800 int i, k;
13801 int div;
13802 isl_size total;
13803 isl_size n_param;
13804 isl_size n_in;
13806 total = isl_basic_map_dim(bmap, isl_dim_all);
13807 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13808 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13809 if (total < 0 || n_param < 0 || n_in < 0)
13810 return isl_basic_map_free(bmap);
13811 for (i = 0; i < ma->n; ++i) {
13812 int o_bmap = 0, o_ma = 1;
13814 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13815 continue;
13816 div = isl_basic_map_alloc_div(bmap);
13817 k = isl_basic_map_alloc_equality(bmap);
13818 if (div < 0 || k < 0)
13819 goto error;
13820 isl_int_set_si(bmap->div[div][0], 0);
13821 isl_seq_cpy(bmap->eq[k] + o_bmap,
13822 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13823 o_bmap += 1 + n_param;
13824 o_ma += 1 + n_param;
13825 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13826 o_bmap += n_before;
13827 isl_seq_cpy(bmap->eq[k] + o_bmap,
13828 ma->u.p[i]->v->el + o_ma, n_in);
13829 o_bmap += n_in;
13830 o_ma += n_in;
13831 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13832 o_bmap += n_after;
13833 isl_seq_cpy(bmap->eq[k] + o_bmap,
13834 ma->u.p[i]->v->el + o_ma, n_div_ma);
13835 o_bmap += n_div_ma;
13836 o_ma += n_div_ma;
13837 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13838 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13839 total++;
13842 return bmap;
13843 error:
13844 isl_basic_map_free(bmap);
13845 return NULL;
13848 /* Replace the domain or range space (depending on "type) of "space" by "set".
13850 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13851 enum isl_dim_type type, __isl_take isl_space *set)
13853 if (type == isl_dim_in) {
13854 space = isl_space_range(space);
13855 space = isl_space_map_from_domain_and_range(set, space);
13856 } else {
13857 space = isl_space_domain(space);
13858 space = isl_space_map_from_domain_and_range(space, set);
13861 return space;
13864 /* Compute the preimage of the domain or range (depending on "type")
13865 * of "bmap" under the function represented by "ma".
13866 * In other words, plug in "ma" in the domain or range of "bmap".
13867 * The result is a basic map that lives in the same space as "bmap"
13868 * except that the domain or range has been replaced by
13869 * the domain space of "ma".
13871 * If bmap is represented by
13873 * A(p) + S u + B x + T v + C(divs) >= 0,
13875 * where u and x are input and output dimensions if type == isl_dim_out
13876 * while x and v are input and output dimensions if type == isl_dim_in,
13877 * and ma is represented by
13879 * x = D(p) + F(y) + G(divs')
13881 * then the result is
13883 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13885 * The divs in the input set are similarly adjusted.
13886 * In particular
13888 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13890 * becomes
13892 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13893 * B_i G(divs') + c_i(divs))/n_i)
13895 * If bmap is not a rational map and if F(y) involves any denominators
13897 * x_i = (f_i y + h_i)/m_i
13899 * then additional constraints are added to ensure that we only
13900 * map back integer points. That is we enforce
13902 * f_i y + h_i = m_i alpha_i
13904 * with alpha_i an additional existentially quantified variable.
13906 * We first copy over the divs from "ma".
13907 * Then we add the modified constraints and divs from "bmap".
13908 * Finally, we add the stride constraints, if needed.
13910 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13911 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13912 __isl_take isl_multi_aff *ma)
13914 int i, k;
13915 isl_space *space;
13916 isl_basic_map *res = NULL;
13917 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13918 isl_int f, c1, c2, g;
13919 isl_bool rational;
13920 int strides;
13922 isl_int_init(f);
13923 isl_int_init(c1);
13924 isl_int_init(c2);
13925 isl_int_init(g);
13927 ma = isl_multi_aff_align_divs(ma);
13928 if (!bmap || !ma)
13929 goto error;
13930 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13931 goto error;
13933 if (type == isl_dim_in) {
13934 n_before = 0;
13935 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13936 } else {
13937 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13938 n_after = 0;
13940 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13941 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13942 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13943 goto error;
13945 space = isl_multi_aff_get_domain_space(ma);
13946 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13947 rational = isl_basic_map_is_rational(bmap);
13948 strides = rational ? 0 : multi_aff_strides(ma);
13949 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13950 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13951 if (rational)
13952 res = isl_basic_map_set_rational(res);
13954 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13955 if (isl_basic_map_alloc_div(res) < 0)
13956 goto error;
13958 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13959 if (!res)
13960 goto error;
13962 for (i = 0; i < bmap->n_eq; ++i) {
13963 k = isl_basic_map_alloc_equality(res);
13964 if (k < 0)
13965 goto error;
13966 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13967 n_after, n_div_ma, n_div_bmap,
13968 f, c1, c2, g, 0) < 0)
13969 goto error;
13972 for (i = 0; i < bmap->n_ineq; ++i) {
13973 k = isl_basic_map_alloc_inequality(res);
13974 if (k < 0)
13975 goto error;
13976 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13977 n_after, n_div_ma, n_div_bmap,
13978 f, c1, c2, g, 0) < 0)
13979 goto error;
13982 for (i = 0; i < bmap->n_div; ++i) {
13983 if (isl_int_is_zero(bmap->div[i][0])) {
13984 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13985 continue;
13987 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13988 n_before, n_after, n_div_ma, n_div_bmap,
13989 f, c1, c2, g, 1) < 0)
13990 goto error;
13993 if (strides)
13994 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13996 isl_int_clear(f);
13997 isl_int_clear(c1);
13998 isl_int_clear(c2);
13999 isl_int_clear(g);
14000 isl_basic_map_free(bmap);
14001 isl_multi_aff_free(ma);
14002 res = isl_basic_map_simplify(res);
14003 return isl_basic_map_finalize(res);
14004 error:
14005 isl_int_clear(f);
14006 isl_int_clear(c1);
14007 isl_int_clear(c2);
14008 isl_int_clear(g);
14009 isl_basic_map_free(bmap);
14010 isl_multi_aff_free(ma);
14011 isl_basic_map_free(res);
14012 return NULL;
14015 /* Compute the preimage of "bset" under the function represented by "ma".
14016 * In other words, plug in "ma" in "bset". The result is a basic set
14017 * that lives in the domain space of "ma".
14019 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14020 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14022 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14025 /* Compute the preimage of the domain of "bmap" under the function
14026 * represented by "ma".
14027 * In other words, plug in "ma" in the domain of "bmap".
14028 * The result is a basic map that lives in the same space as "bmap"
14029 * except that the domain has been replaced by the domain space of "ma".
14031 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14032 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14034 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14037 /* Compute the preimage of the range of "bmap" under the function
14038 * represented by "ma".
14039 * In other words, plug in "ma" in the range of "bmap".
14040 * The result is a basic map that lives in the same space as "bmap"
14041 * except that the range has been replaced by the domain space of "ma".
14043 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14044 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14046 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14049 /* Check if the range of "ma" is compatible with the domain or range
14050 * (depending on "type") of "map".
14051 * Return isl_stat_error if anything is wrong.
14053 static isl_stat check_map_compatible_range_multi_aff(
14054 __isl_keep isl_map *map, enum isl_dim_type type,
14055 __isl_keep isl_multi_aff *ma)
14057 isl_bool m;
14058 isl_space *ma_space;
14060 ma_space = isl_multi_aff_get_space(ma);
14061 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14062 isl_space_free(ma_space);
14063 if (m < 0)
14064 return isl_stat_error;
14065 if (!m)
14066 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14067 "spaces don't match", return isl_stat_error);
14068 return isl_stat_ok;
14071 /* Compute the preimage of the domain or range (depending on "type")
14072 * of "map" under the function represented by "ma".
14073 * In other words, plug in "ma" in the domain or range of "map".
14074 * The result is a map that lives in the same space as "map"
14075 * except that the domain or range has been replaced by
14076 * the domain space of "ma".
14078 * The parameters are assumed to have been aligned.
14080 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14081 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14083 int i;
14084 isl_space *space;
14086 map = isl_map_cow(map);
14087 ma = isl_multi_aff_align_divs(ma);
14088 if (!map || !ma)
14089 goto error;
14090 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14091 goto error;
14093 for (i = 0; i < map->n; ++i) {
14094 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14095 isl_multi_aff_copy(ma));
14096 if (!map->p[i])
14097 goto error;
14100 space = isl_multi_aff_get_domain_space(ma);
14101 space = isl_space_set(isl_map_get_space(map), type, space);
14103 isl_space_free(isl_map_take_space(map));
14104 map = isl_map_restore_space(map, space);
14105 if (!map)
14106 goto error;
14108 isl_multi_aff_free(ma);
14109 if (map->n > 1)
14110 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14111 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14112 return map;
14113 error:
14114 isl_multi_aff_free(ma);
14115 isl_map_free(map);
14116 return NULL;
14119 /* Compute the preimage of the domain or range (depending on "type")
14120 * of "map" under the function represented by "ma".
14121 * In other words, plug in "ma" in the domain or range of "map".
14122 * The result is a map that lives in the same space as "map"
14123 * except that the domain or range has been replaced by
14124 * the domain space of "ma".
14126 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14127 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14129 isl_bool aligned;
14131 if (!map || !ma)
14132 goto error;
14134 aligned = isl_map_space_has_equal_params(map, ma->space);
14135 if (aligned < 0)
14136 goto error;
14137 if (aligned)
14138 return map_preimage_multi_aff(map, type, ma);
14140 if (isl_map_check_named_params(map) < 0)
14141 goto error;
14142 if (!isl_space_has_named_params(ma->space))
14143 isl_die(map->ctx, isl_error_invalid,
14144 "unaligned unnamed parameters", goto error);
14145 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14146 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14148 return map_preimage_multi_aff(map, type, ma);
14149 error:
14150 isl_multi_aff_free(ma);
14151 return isl_map_free(map);
14154 /* Compute the preimage of "set" under the function represented by "ma".
14155 * In other words, plug in "ma" in "set". The result is a set
14156 * that lives in the domain space of "ma".
14158 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14159 __isl_take isl_multi_aff *ma)
14161 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14164 /* Compute the preimage of the domain of "map" under the function
14165 * represented by "ma".
14166 * In other words, plug in "ma" in the domain of "map".
14167 * The result is a map that lives in the same space as "map"
14168 * except that the domain has been replaced by the domain space of "ma".
14170 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14171 __isl_take isl_multi_aff *ma)
14173 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14176 /* Compute the preimage of the range of "map" under the function
14177 * represented by "ma".
14178 * In other words, plug in "ma" in the range of "map".
14179 * The result is a map that lives in the same space as "map"
14180 * except that the range has been replaced by the domain space of "ma".
14182 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14183 __isl_take isl_multi_aff *ma)
14185 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14188 /* Compute the preimage of "map" under the function represented by "pma".
14189 * In other words, plug in "pma" in the domain or range of "map".
14190 * The result is a map that lives in the same space as "map",
14191 * except that the space of type "type" has been replaced by
14192 * the domain space of "pma".
14194 * The parameters of "map" and "pma" are assumed to have been aligned.
14196 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14197 __isl_take isl_map *map, enum isl_dim_type type,
14198 __isl_take isl_pw_multi_aff *pma)
14200 int i;
14201 isl_map *res;
14203 if (!pma)
14204 goto error;
14206 if (pma->n == 0) {
14207 isl_space *space;
14209 space = isl_pw_multi_aff_get_domain_space(pma);
14210 isl_pw_multi_aff_free(pma);
14211 space = isl_space_set(isl_map_get_space(map), type, space);
14212 isl_map_free(map);
14213 return isl_map_empty(space);
14216 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14217 isl_multi_aff_copy(pma->p[0].maff));
14218 if (type == isl_dim_in)
14219 res = isl_map_intersect_domain(res,
14220 isl_map_copy(pma->p[0].set));
14221 else
14222 res = isl_map_intersect_range(res,
14223 isl_map_copy(pma->p[0].set));
14225 for (i = 1; i < pma->n; ++i) {
14226 isl_map *res_i;
14228 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14229 isl_multi_aff_copy(pma->p[i].maff));
14230 if (type == isl_dim_in)
14231 res_i = isl_map_intersect_domain(res_i,
14232 isl_map_copy(pma->p[i].set));
14233 else
14234 res_i = isl_map_intersect_range(res_i,
14235 isl_map_copy(pma->p[i].set));
14236 res = isl_map_union(res, res_i);
14239 isl_pw_multi_aff_free(pma);
14240 isl_map_free(map);
14241 return res;
14242 error:
14243 isl_pw_multi_aff_free(pma);
14244 isl_map_free(map);
14245 return NULL;
14248 /* Compute the preimage of "map" under the function represented by "pma".
14249 * In other words, plug in "pma" in the domain or range of "map".
14250 * The result is a map that lives in the same space as "map",
14251 * except that the space of type "type" has been replaced by
14252 * the domain space of "pma".
14254 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14255 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14257 isl_bool aligned;
14259 if (!map || !pma)
14260 goto error;
14262 aligned = isl_map_space_has_equal_params(map, pma->dim);
14263 if (aligned < 0)
14264 goto error;
14265 if (aligned)
14266 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14268 if (isl_map_check_named_params(map) < 0)
14269 goto error;
14270 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14271 goto error;
14272 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14273 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14275 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14276 error:
14277 isl_pw_multi_aff_free(pma);
14278 return isl_map_free(map);
14281 /* Compute the preimage of "set" under the function represented by "pma".
14282 * In other words, plug in "pma" in "set". The result is a set
14283 * that lives in the domain space of "pma".
14285 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14286 __isl_take isl_pw_multi_aff *pma)
14288 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14291 /* Compute the preimage of the domain of "map" under the function
14292 * represented by "pma".
14293 * In other words, plug in "pma" in the domain of "map".
14294 * The result is a map that lives in the same space as "map",
14295 * except that domain space has been replaced by the domain space of "pma".
14297 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14298 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14300 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14303 /* Compute the preimage of the range of "map" under the function
14304 * represented by "pma".
14305 * In other words, plug in "pma" in the range of "map".
14306 * The result is a map that lives in the same space as "map",
14307 * except that range space has been replaced by the domain space of "pma".
14309 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14310 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14312 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14315 /* Compute the preimage of "map" under the function represented by "mpa".
14316 * In other words, plug in "mpa" in the domain or range of "map".
14317 * The result is a map that lives in the same space as "map",
14318 * except that the space of type "type" has been replaced by
14319 * the domain space of "mpa".
14321 * If the map does not involve any constraints that refer to the
14322 * dimensions of the substituted space, then the only possible
14323 * effect of "mpa" on the map is to map the space to a different space.
14324 * We create a separate isl_multi_aff to effectuate this change
14325 * in order to avoid spurious splitting of the map along the pieces
14326 * of "mpa".
14327 * If "mpa" has a non-trivial explicit domain, however,
14328 * then the full substitution should be performed.
14330 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14331 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14333 isl_size n;
14334 isl_bool full;
14335 isl_pw_multi_aff *pma;
14337 n = isl_map_dim(map, type);
14338 if (n < 0 || !mpa)
14339 goto error;
14341 full = isl_map_involves_dims(map, type, 0, n);
14342 if (full >= 0 && !full)
14343 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14344 if (full < 0)
14345 goto error;
14346 if (!full) {
14347 isl_space *space;
14348 isl_multi_aff *ma;
14350 space = isl_multi_pw_aff_get_space(mpa);
14351 isl_multi_pw_aff_free(mpa);
14352 ma = isl_multi_aff_zero(space);
14353 return isl_map_preimage_multi_aff(map, type, ma);
14356 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14357 return isl_map_preimage_pw_multi_aff(map, type, pma);
14358 error:
14359 isl_map_free(map);
14360 isl_multi_pw_aff_free(mpa);
14361 return NULL;
14364 /* Compute the preimage of "map" under the function represented by "mpa".
14365 * In other words, plug in "mpa" in the domain "map".
14366 * The result is a map that lives in the same space as "map",
14367 * except that domain space has been replaced by the domain space of "mpa".
14369 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14370 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14372 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14375 /* Compute the preimage of "set" by the function represented by "mpa".
14376 * In other words, plug in "mpa" in "set".
14378 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14379 __isl_take isl_multi_pw_aff *mpa)
14381 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14384 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14385 * on the output dimension "pos" in terms of the parameters,
14386 * the input dimensions and possibly some integer divisions,
14387 * but not any other output dimensions, extract this upper bound
14388 * as a function of all dimensions (with zero coefficients
14389 * for the output dimensions).
14391 * That is, the inequality is of the form
14393 * e(...) + c - m x >= 0
14395 * where e does not depend on any other output dimensions.
14396 * Return (e(...) + c) / m, with the denominator m in the first position.
14398 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14399 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14401 isl_ctx *ctx;
14402 isl_size v_out, total;
14404 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14405 total = isl_basic_map_dim(bmap, isl_dim_all);
14406 if (v_out < 0 || total < 0)
14407 return NULL;
14408 ctx = isl_basic_map_get_ctx(bmap);
14409 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14410 total, v_out + pos);
14413 /* Is constraint "c" of "bmap" of the form
14415 * e(...) + c1 - m x >= 0
14417 * or
14419 * -e(...) + c2 + m x >= 0
14421 * where m > 1 and e does not involve any other output variables?
14423 * "v_out" is the offset to the output variables.
14424 * "d" is the position of x among the output variables.
14425 * "v_div" is the offset to the local variables.
14426 * "total" is the total number of variables.
14428 * Since the purpose of this function is to use the constraint
14429 * to express the output variable as an integer division,
14430 * do not allow the constraint to involve any local variables
14431 * that do not have an explicit representation.
14433 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14434 isl_int *c, int v_out, int d, int v_div, int total)
14436 int i = 0;
14438 if (isl_int_is_zero(c[1 + v_out + d]))
14439 return isl_bool_false;
14440 if (isl_int_is_one(c[1 + v_out + d]))
14441 return isl_bool_false;
14442 if (isl_int_is_negone(c[1 + v_out + d]))
14443 return isl_bool_false;
14444 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14445 return isl_bool_false;
14446 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14447 v_div - (v_out + d + 1)) != -1)
14448 return isl_bool_false;
14449 for (i = 0; v_div + i < total; ++i) {
14450 isl_bool known, involves;
14452 if (isl_int_is_zero(c[1 + v_div + i]))
14453 continue;
14454 known = isl_basic_map_div_is_known(bmap, i);
14455 if (known < 0 || !known)
14456 return known;
14457 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14458 if (involves < 0 || involves)
14459 return isl_bool_not(involves);
14461 return isl_bool_true;
14464 /* Look for a pair of constraints
14466 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14468 * and
14470 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14472 * that express that the output dimension x at position "pos"
14473 * is some integer division of an expression in terms of the parameters,
14474 * input dimensions and integer divisions.
14475 * If such a pair can be found, then return the index
14476 * of the upper bound constraint, m x <= e(...) + c1.
14477 * Otherwise, return an index beyond the number of constraints.
14479 * In order for the constraints above to express an integer division,
14480 * m needs to be greater than 1 and such that
14482 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14484 * In particular, this ensures that
14486 * x = floor((e(...) + c1) / m)
14488 isl_size isl_basic_map_find_output_upper_div_constraint(
14489 __isl_keep isl_basic_map *bmap, int pos)
14491 int i, j;
14492 isl_size n_ineq;
14493 isl_size v_out, v_div;
14494 isl_size total;
14495 isl_int sum;
14497 total = isl_basic_map_dim(bmap, isl_dim_all);
14498 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14499 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
14500 n_ineq = isl_basic_map_n_inequality(bmap);
14501 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
14502 return isl_size_error;
14504 isl_int_init(sum);
14505 for (i = 0; i < n_ineq; ++i) {
14506 isl_bool potential;
14508 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
14509 v_out, pos, v_div, total);
14510 if (potential < 0)
14511 goto error;
14512 if (!potential)
14513 continue;
14514 for (j = i + 1; j < n_ineq; ++j) {
14515 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
14516 bmap->ineq[j] + 1, total))
14517 continue;
14518 isl_int_add(sum, bmap->ineq[i][0], bmap->ineq[j][0]);
14519 if (isl_int_abs_lt(sum, bmap->ineq[i][1 + v_out + pos]))
14520 break;
14522 if (j < n_ineq)
14523 break;
14525 isl_int_clear(sum);
14527 if (i >= n_ineq)
14528 return n_ineq;
14529 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
14530 return i;
14531 else
14532 return j;
14533 error:
14534 isl_int_clear(sum);
14535 return isl_size_error;
14538 /* Return a copy of the equality constraints of "bset" as a matrix.
14540 __isl_give isl_mat *isl_basic_set_extract_equalities(
14541 __isl_keep isl_basic_set *bset)
14543 isl_ctx *ctx;
14544 isl_size total;
14546 total = isl_basic_set_dim(bset, isl_dim_all);
14547 if (total < 0)
14548 return NULL;
14550 ctx = isl_basic_set_get_ctx(bset);
14551 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14554 /* Are the "n" "coefficients" starting at "first" of the integer division
14555 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14556 * to each other?
14557 * The "coefficient" at position 0 is the denominator.
14558 * The "coefficient" at position 1 is the constant term.
14560 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14561 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14562 unsigned first, unsigned n)
14564 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14565 return isl_bool_error;
14566 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14567 return isl_bool_error;
14568 return isl_seq_eq(bmap1->div[pos1] + first,
14569 bmap2->div[pos2] + first, n);
14572 /* Are the integer division expressions at position "pos1" in "bmap1" and
14573 * "pos2" in "bmap2" equal to each other, except that the constant terms
14574 * are different?
14576 isl_bool isl_basic_map_equal_div_expr_except_constant(
14577 __isl_keep isl_basic_map *bmap1, int pos1,
14578 __isl_keep isl_basic_map *bmap2, int pos2)
14580 isl_bool equal;
14581 isl_size total, total2;
14583 total = isl_basic_map_dim(bmap1, isl_dim_all);
14584 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14585 if (total < 0 || total2 < 0)
14586 return isl_bool_error;
14587 if (total != total2)
14588 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14589 "incomparable div expressions", return isl_bool_error);
14590 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14591 0, 1);
14592 if (equal < 0 || !equal)
14593 return equal;
14594 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14595 1, 1);
14596 if (equal < 0 || equal)
14597 return isl_bool_not(equal);
14598 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14599 2, total);
14602 /* Replace the numerator of the constant term of the integer division
14603 * expression at position "div" in "bmap" by "value".
14604 * The caller guarantees that this does not change the meaning
14605 * of the input.
14607 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14608 __isl_take isl_basic_map *bmap, int div, int value)
14610 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14611 return isl_basic_map_free(bmap);
14613 isl_int_set_si(bmap->div[div][1], value);
14615 return bmap;
14618 /* Is the point "inner" internal to inequality constraint "ineq"
14619 * of "bset"?
14620 * The point is considered to be internal to the inequality constraint,
14621 * if it strictly lies on the positive side of the inequality constraint,
14622 * or if it lies on the constraint and the constraint is lexico-positive.
14624 static isl_bool is_internal(__isl_keep isl_vec *inner,
14625 __isl_keep isl_basic_set *bset, int ineq)
14627 isl_ctx *ctx;
14628 int pos;
14629 isl_size total;
14631 if (!inner || !bset)
14632 return isl_bool_error;
14634 ctx = isl_basic_set_get_ctx(bset);
14635 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14636 &ctx->normalize_gcd);
14637 if (!isl_int_is_zero(ctx->normalize_gcd))
14638 return isl_int_is_nonneg(ctx->normalize_gcd);
14640 total = isl_basic_set_dim(bset, isl_dim_all);
14641 if (total < 0)
14642 return isl_bool_error;
14643 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14644 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14647 /* Tighten the inequality constraints of "bset" that are outward with respect
14648 * to the point "vec".
14649 * That is, tighten the constraints that are not satisfied by "vec".
14651 * "vec" is a point internal to some superset S of "bset" that is used
14652 * to make the subsets of S disjoint, by tightening one half of the constraints
14653 * that separate two subsets. In particular, the constraints of S
14654 * are all satisfied by "vec" and should not be tightened.
14655 * Of the internal constraints, those that have "vec" on the outside
14656 * are tightened. The shared facet is included in the adjacent subset
14657 * with the opposite constraint.
14658 * For constraints that saturate "vec", this criterion cannot be used
14659 * to determine which of the two sides should be tightened.
14660 * Instead, the sign of the first non-zero coefficient is used
14661 * to make this choice. Note that this second criterion is never used
14662 * on the constraints of S since "vec" is interior to "S".
14664 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14665 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14667 int j;
14669 bset = isl_basic_set_cow(bset);
14670 if (!bset)
14671 return NULL;
14672 for (j = 0; j < bset->n_ineq; ++j) {
14673 isl_bool internal;
14675 internal = is_internal(vec, bset, j);
14676 if (internal < 0)
14677 return isl_basic_set_free(bset);
14678 if (internal)
14679 continue;
14680 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14683 return bset;
14686 /* Replace the variables x of type "type" starting at "first" in "bmap"
14687 * by x' with x = M x' with M the matrix trans.
14688 * That is, replace the corresponding coefficients c by c M.
14690 * The transformation matrix should be a square matrix.
14692 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14693 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14694 __isl_take isl_mat *trans)
14696 unsigned pos;
14698 bmap = isl_basic_map_cow(bmap);
14699 if (!bmap || !trans)
14700 goto error;
14702 if (trans->n_row != trans->n_col)
14703 isl_die(trans->ctx, isl_error_invalid,
14704 "expecting square transformation matrix", goto error);
14705 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14706 goto error;
14708 pos = isl_basic_map_offset(bmap, type) + first;
14710 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14711 isl_mat_copy(trans)) < 0)
14712 goto error;
14713 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14714 isl_mat_copy(trans)) < 0)
14715 goto error;
14716 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14717 isl_mat_copy(trans)) < 0)
14718 goto error;
14720 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14721 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14722 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
14724 isl_mat_free(trans);
14725 return bmap;
14726 error:
14727 isl_mat_free(trans);
14728 isl_basic_map_free(bmap);
14729 return NULL;
14732 /* Replace the variables x of type "type" starting at "first" in "bset"
14733 * by x' with x = M x' with M the matrix trans.
14734 * That is, replace the corresponding coefficients c by c M.
14736 * The transformation matrix should be a square matrix.
14738 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14739 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14740 __isl_take isl_mat *trans)
14742 return isl_basic_map_transform_dims(bset, type, first, trans);