isl_basic_map_find_output_upper_div_constraint: extract constraint_pair_has_bound
[isl.git] / isl_map.c
blobc08bc507a2556dd7db59d1afeaee17369fff67ea
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 /* Do the inequality constraints "i" and "j" of "bmap"
2314 * form a pair of opposite constraints, in the (first) "len" coefficients?
2316 static int is_constraint_pair(__isl_keep isl_basic_map *bmap, int i, int j,
2317 unsigned len)
2319 return isl_seq_is_neg(bmap->ineq[i] + 1, bmap->ineq[j] + 1, len);
2322 /* Given that inequality constraints "i" and "j" of "bmap"
2323 * form a pair of opposite constraints
2325 * f(x) + c1 >= 0
2326 * -f(x) + c2 >= 0
2328 * or
2330 * -c1 <= f(x) <= c2
2332 * do they allow for at most "bound" values in that direction?
2333 * That is, is the sum of their constant terms smaller than "bound"?
2335 * "tmp" is a temporary location that can be used to store the sum.
2337 static int constraint_pair_has_bound(__isl_keep isl_basic_map *bmap,
2338 int i, int j, isl_int bound, isl_int *tmp)
2340 isl_int_add(*tmp, bmap->ineq[i][0], bmap->ineq[j][0]);
2341 return isl_int_abs_lt(*tmp, bound);
2344 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2345 * of "bmap").
2347 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2349 isl_int *t = bmap->div[a];
2350 bmap->div[a] = bmap->div[b];
2351 bmap->div[b] = t;
2354 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2355 * div definitions accordingly.
2357 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2358 int a, int b)
2360 int i;
2361 isl_size off;
2363 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2364 if (off < 0)
2365 return isl_basic_map_free(bmap);
2367 swap_div(bmap, a, b);
2369 for (i = 0; i < bmap->n_eq; ++i)
2370 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2372 for (i = 0; i < bmap->n_ineq; ++i)
2373 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2375 for (i = 0; i < bmap->n_div; ++i)
2376 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2377 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2379 return bmap;
2382 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2384 isl_seq_cpy(c, c + n, rem);
2385 isl_seq_clr(c + rem, n);
2388 /* Drop n dimensions starting at first.
2390 * In principle, this frees up some extra variables as the number
2391 * of columns remains constant, but we would have to extend
2392 * the div array too as the number of rows in this array is assumed
2393 * to be equal to extra.
2395 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2396 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2398 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2401 /* Move "n" divs starting at "first" to the end of the list of divs.
2403 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2404 unsigned first, unsigned n)
2406 isl_int **div;
2407 int i;
2409 if (first + n == bmap->n_div)
2410 return bmap;
2412 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2413 if (!div)
2414 goto error;
2415 for (i = 0; i < n; ++i)
2416 div[i] = bmap->div[first + i];
2417 for (i = 0; i < bmap->n_div - first - n; ++i)
2418 bmap->div[first + i] = bmap->div[first + n + i];
2419 for (i = 0; i < n; ++i)
2420 bmap->div[bmap->n_div - n + i] = div[i];
2421 free(div);
2422 return bmap;
2423 error:
2424 isl_basic_map_free(bmap);
2425 return NULL;
2428 #undef TYPE
2429 #define TYPE isl_map
2430 static
2431 #include "check_type_range_templ.c"
2433 /* Check that there are "n" dimensions of type "type" starting at "first"
2434 * in "set".
2436 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2437 enum isl_dim_type type, unsigned first, unsigned n)
2439 return isl_map_check_range(set_to_map(set), type, first, n);
2442 /* Drop "n" dimensions of type "type" starting at "first".
2443 * Perform the core computation, without cowing or
2444 * simplifying and finalizing the result.
2446 * In principle, this frees up some extra variables as the number
2447 * of columns remains constant, but we would have to extend
2448 * the div array too as the number of rows in this array is assumed
2449 * to be equal to extra.
2451 __isl_give isl_basic_map *isl_basic_map_drop_core(
2452 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2453 unsigned first, unsigned n)
2455 int i;
2456 unsigned offset;
2457 unsigned left;
2458 isl_size total;
2460 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2461 return isl_basic_map_free(bmap);
2463 total = isl_basic_map_dim(bmap, isl_dim_all);
2464 if (total < 0)
2465 return isl_basic_map_free(bmap);
2467 offset = isl_basic_map_offset(bmap, type) + first;
2468 left = total - (offset - 1) - n;
2469 for (i = 0; i < bmap->n_eq; ++i)
2470 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2472 for (i = 0; i < bmap->n_ineq; ++i)
2473 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2475 for (i = 0; i < bmap->n_div; ++i)
2476 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2478 if (type == isl_dim_div) {
2479 bmap = move_divs_last(bmap, first, n);
2480 if (!bmap)
2481 return NULL;
2482 if (isl_basic_map_free_div(bmap, n) < 0)
2483 return isl_basic_map_free(bmap);
2484 } else
2485 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2486 if (!bmap->dim)
2487 return isl_basic_map_free(bmap);
2489 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2490 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2491 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2492 return bmap;
2495 /* Drop "n" dimensions of type "type" starting at "first".
2497 * In principle, this frees up some extra variables as the number
2498 * of columns remains constant, but we would have to extend
2499 * the div array too as the number of rows in this array is assumed
2500 * to be equal to extra.
2502 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2503 enum isl_dim_type type, unsigned first, unsigned n)
2505 if (!bmap)
2506 return NULL;
2507 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2508 return bmap;
2510 bmap = isl_basic_map_cow(bmap);
2511 if (!bmap)
2512 return NULL;
2514 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2516 bmap = isl_basic_map_simplify(bmap);
2517 return isl_basic_map_finalize(bmap);
2520 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2521 enum isl_dim_type type, unsigned first, unsigned n)
2523 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2524 type, first, n));
2527 /* No longer consider "map" to be normalized.
2529 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2531 if (!map)
2532 return NULL;
2533 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2534 return map;
2537 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2538 enum isl_dim_type type, unsigned first, unsigned n)
2540 int i;
2541 isl_space *space;
2543 if (isl_map_check_range(map, type, first, n) < 0)
2544 return isl_map_free(map);
2546 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2547 return map;
2548 map = isl_map_cow(map);
2549 if (!map)
2550 goto error;
2552 for (i = 0; i < map->n; ++i) {
2553 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2554 if (!map->p[i])
2555 goto error;
2557 map = isl_map_unmark_normalized(map);
2559 space = isl_map_take_space(map);
2560 space = isl_space_drop_dims(space, type, first, n);
2561 map = isl_map_restore_space(map, space);
2563 return map;
2564 error:
2565 isl_map_free(map);
2566 return NULL;
2569 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2570 enum isl_dim_type type, unsigned first, unsigned n)
2572 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2575 /* Drop the integer division at position "div", which is assumed
2576 * not to appear in any of the constraints or
2577 * in any of the other integer divisions.
2579 * Since the integer division is redundant, there is no need to cow.
2581 __isl_give isl_basic_map *isl_basic_map_drop_div(
2582 __isl_take isl_basic_map *bmap, unsigned div)
2584 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2587 /* Eliminate the specified n dimensions starting at first from the
2588 * constraints, without removing the dimensions from the space.
2589 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2591 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2592 enum isl_dim_type type, unsigned first, unsigned n)
2594 int i;
2596 if (n == 0)
2597 return map;
2599 if (isl_map_check_range(map, type, first, n) < 0)
2600 return isl_map_free(map);
2602 map = isl_map_cow(map);
2603 if (!map)
2604 return NULL;
2606 for (i = 0; i < map->n; ++i) {
2607 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2608 if (!map->p[i])
2609 goto error;
2611 return map;
2612 error:
2613 isl_map_free(map);
2614 return NULL;
2617 /* Eliminate the specified n dimensions starting at first from the
2618 * constraints, without removing the dimensions from the space.
2619 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2621 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2622 enum isl_dim_type type, unsigned first, unsigned n)
2624 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2627 /* Eliminate the specified n dimensions starting at first from the
2628 * constraints, without removing the dimensions from the space.
2629 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2631 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2632 unsigned first, unsigned n)
2634 return isl_set_eliminate(set, isl_dim_set, first, n);
2637 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2638 __isl_take isl_basic_map *bmap)
2640 isl_size v_div;
2642 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2643 if (v_div < 0)
2644 return isl_basic_map_free(bmap);
2645 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2646 if (!bmap)
2647 return NULL;
2648 bmap->n_div = 0;
2649 return isl_basic_map_finalize(bmap);
2652 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2653 __isl_take isl_basic_set *bset)
2655 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2658 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2660 int i;
2662 if (!map)
2663 return NULL;
2664 if (map->n == 0)
2665 return map;
2667 map = isl_map_cow(map);
2668 if (!map)
2669 return NULL;
2671 for (i = 0; i < map->n; ++i) {
2672 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2673 if (!map->p[i])
2674 goto error;
2676 return map;
2677 error:
2678 isl_map_free(map);
2679 return NULL;
2682 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2684 return isl_map_remove_divs(set);
2687 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2688 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2689 unsigned first, unsigned n)
2691 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2692 return isl_basic_map_free(bmap);
2693 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2694 return bmap;
2695 bmap = isl_basic_map_eliminate_vars(bmap,
2696 isl_basic_map_offset(bmap, type) - 1 + first, n);
2697 if (!bmap)
2698 return bmap;
2699 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2700 return bmap;
2701 bmap = isl_basic_map_drop(bmap, type, first, n);
2702 return bmap;
2705 /* Does the local variable "div" of "bmap" have a known expression
2706 * that involves the "n" variables starting at "first"?
2708 isl_bool isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map *bmap,
2709 int div, unsigned first, unsigned n)
2711 isl_bool unknown;
2713 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
2714 if (unknown < 0 || unknown)
2715 return isl_bool_not(unknown);
2716 if (isl_seq_first_non_zero(bmap->div[div] + 1 + 1 + first, n) >= 0)
2717 return isl_bool_true;
2719 return isl_bool_false;
2722 /* Return true if the definition of the given div (recursively) involves
2723 * any of the given variables.
2725 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2726 unsigned first, unsigned n)
2728 int i;
2729 isl_bool involves;
2730 isl_size n_div, v_div;
2732 involves = isl_basic_map_div_expr_involves_vars(bmap, div, first, n);
2733 if (involves < 0 || involves)
2734 return involves;
2736 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2737 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2738 if (n_div < 0 || v_div < 0)
2739 return isl_bool_error;
2740 for (i = n_div - 1; i >= 0; --i) {
2741 isl_bool involves;
2743 if (isl_int_is_zero(bmap->div[div][1 + 1 + v_div + i]))
2744 continue;
2745 involves = div_involves_vars(bmap, i, first, n);
2746 if (involves < 0 || involves)
2747 return involves;
2750 return isl_bool_false;
2753 /* Does the definition of any integer division involve
2754 * any of the given variables?
2756 isl_bool isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map *bmap,
2757 unsigned first, unsigned n)
2759 int i;
2760 isl_size n_div;
2762 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2763 if (n_div < 0)
2764 return isl_bool_error;
2766 for (i = 0; i < n_div; ++i) {
2767 isl_bool has;
2769 has = isl_basic_map_div_expr_involves_vars(bmap, i, first, n);
2770 if (has < 0 || has)
2771 return has;
2774 return isl_bool_false;
2777 /* Try and add a lower and/or upper bound on "div" to "bmap"
2778 * based on inequality "i".
2779 * "total" is the total number of variables (excluding the divs).
2780 * "v" is a temporary object that can be used during the calculations.
2781 * If "lb" is set, then a lower bound should be constructed.
2782 * If "ub" is set, then an upper bound should be constructed.
2784 * The calling function has already checked that the inequality does not
2785 * reference "div", but we still need to check that the inequality is
2786 * of the right form. We'll consider the case where we want to construct
2787 * a lower bound. The construction of upper bounds is similar.
2789 * Let "div" be of the form
2791 * q = floor((a + f(x))/d)
2793 * We essentially check if constraint "i" is of the form
2795 * b + f(x) >= 0
2797 * so that we can use it to derive a lower bound on "div".
2798 * However, we allow a slightly more general form
2800 * b + g(x) >= 0
2802 * with the condition that the coefficients of g(x) - f(x) are all
2803 * divisible by d.
2804 * Rewriting this constraint as
2806 * 0 >= -b - g(x)
2808 * adding a + f(x) to both sides and dividing by d, we obtain
2810 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2812 * Taking the floor on both sides, we obtain
2814 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2816 * or
2818 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2820 * In the case of an upper bound, we construct the constraint
2822 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2825 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2826 __isl_take isl_basic_map *bmap, int div, int i,
2827 unsigned total, isl_int v, int lb, int ub)
2829 int j;
2831 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2832 if (lb) {
2833 isl_int_sub(v, bmap->ineq[i][1 + j],
2834 bmap->div[div][1 + 1 + j]);
2835 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2837 if (ub) {
2838 isl_int_add(v, bmap->ineq[i][1 + j],
2839 bmap->div[div][1 + 1 + j]);
2840 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2843 if (!lb && !ub)
2844 return bmap;
2846 bmap = isl_basic_map_cow(bmap);
2847 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2848 if (lb) {
2849 int k = isl_basic_map_alloc_inequality(bmap);
2850 if (k < 0)
2851 goto error;
2852 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2853 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2854 bmap->div[div][1 + j]);
2855 isl_int_cdiv_q(bmap->ineq[k][j],
2856 bmap->ineq[k][j], bmap->div[div][0]);
2858 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2860 if (ub) {
2861 int k = isl_basic_map_alloc_inequality(bmap);
2862 if (k < 0)
2863 goto error;
2864 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2865 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2866 bmap->div[div][1 + j]);
2867 isl_int_fdiv_q(bmap->ineq[k][j],
2868 bmap->ineq[k][j], bmap->div[div][0]);
2870 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2873 return bmap;
2874 error:
2875 isl_basic_map_free(bmap);
2876 return NULL;
2879 /* This function is called right before "div" is eliminated from "bmap"
2880 * using Fourier-Motzkin.
2881 * Look through the constraints of "bmap" for constraints on the argument
2882 * of the integer division and use them to construct constraints on the
2883 * integer division itself. These constraints can then be combined
2884 * during the Fourier-Motzkin elimination.
2885 * Note that it is only useful to introduce lower bounds on "div"
2886 * if "bmap" already contains upper bounds on "div" as the newly
2887 * introduce lower bounds can then be combined with the pre-existing
2888 * upper bounds. Similarly for upper bounds.
2889 * We therefore first check if "bmap" contains any lower and/or upper bounds
2890 * on "div".
2892 * It is interesting to note that the introduction of these constraints
2893 * can indeed lead to more accurate results, even when compared to
2894 * deriving constraints on the argument of "div" from constraints on "div".
2895 * Consider, for example, the set
2897 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2899 * The second constraint can be rewritten as
2901 * 2 * [(-i-2j+3)/4] + k >= 0
2903 * from which we can derive
2905 * -i - 2j + 3 >= -2k
2907 * or
2909 * i + 2j <= 3 + 2k
2911 * Combined with the first constraint, we obtain
2913 * -3 <= 3 + 2k or k >= -3
2915 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2916 * the first constraint, we obtain
2918 * [(i + 2j)/4] >= [-3/4] = -1
2920 * Combining this constraint with the second constraint, we obtain
2922 * k >= -2
2924 static __isl_give isl_basic_map *insert_bounds_on_div(
2925 __isl_take isl_basic_map *bmap, int div)
2927 int i;
2928 int check_lb, check_ub;
2929 isl_int v;
2930 isl_size v_div;
2932 if (!bmap)
2933 return NULL;
2935 if (isl_int_is_zero(bmap->div[div][0]))
2936 return bmap;
2938 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2939 if (v_div < 0)
2940 return isl_basic_map_free(bmap);
2942 check_lb = 0;
2943 check_ub = 0;
2944 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2945 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2946 if (s > 0)
2947 check_ub = 1;
2948 if (s < 0)
2949 check_lb = 1;
2952 if (!check_lb && !check_ub)
2953 return bmap;
2955 isl_int_init(v);
2957 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2958 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2959 continue;
2961 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2962 check_lb, check_ub);
2965 isl_int_clear(v);
2967 return bmap;
2970 /* Remove all divs (recursively) involving any of the given variables
2971 * in their definitions.
2973 static __isl_give isl_basic_map *remove_divs_involving_vars(
2974 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2976 int i;
2978 for (i = bmap->n_div - 1; i >= 0; --i) {
2979 isl_bool involves;
2981 involves = div_involves_vars(bmap, i, first, n);
2982 if (involves < 0)
2983 return isl_basic_map_free(bmap);
2984 if (!involves)
2985 continue;
2986 bmap = insert_bounds_on_div(bmap, i);
2987 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2988 if (!bmap)
2989 return NULL;
2990 i = bmap->n_div;
2993 return bmap;
2996 /* Remove all divs (recursively) involving any of the given dimensions
2997 * in their definitions.
2999 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
3000 __isl_take isl_basic_map *bmap,
3001 enum isl_dim_type type, unsigned first, unsigned n)
3003 isl_size off;
3005 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3006 return isl_basic_map_free(bmap);
3007 off = isl_basic_map_var_offset(bmap, type);
3008 if (off < 0)
3009 return isl_basic_map_free(bmap);
3010 first += off;
3012 return remove_divs_involving_vars(bmap, first, n);
3015 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
3016 __isl_take isl_basic_set *bset,
3017 enum isl_dim_type type, unsigned first, unsigned n)
3019 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
3022 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
3023 enum isl_dim_type type, unsigned first, unsigned n)
3025 int i;
3027 if (!map)
3028 return NULL;
3029 if (map->n == 0)
3030 return map;
3032 map = isl_map_cow(map);
3033 if (!map)
3034 return NULL;
3036 for (i = 0; i < map->n; ++i) {
3037 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
3038 type, first, n);
3039 if (!map->p[i])
3040 goto error;
3042 return map;
3043 error:
3044 isl_map_free(map);
3045 return NULL;
3048 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
3049 enum isl_dim_type type, unsigned first, unsigned n)
3051 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
3052 type, first, n));
3055 /* Does the description of "bmap" depend on the specified dimensions?
3056 * We also check whether the dimensions appear in any of the div definitions.
3057 * In principle there is no need for this check. If the dimensions appear
3058 * in a div definition, they also appear in the defining constraints of that
3059 * div.
3061 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
3062 enum isl_dim_type type, unsigned first, unsigned n)
3064 int i;
3066 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3067 return isl_bool_error;
3069 first += isl_basic_map_offset(bmap, type);
3070 for (i = 0; i < bmap->n_eq; ++i)
3071 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
3072 return isl_bool_true;
3073 for (i = 0; i < bmap->n_ineq; ++i)
3074 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
3075 return isl_bool_true;
3076 for (i = 0; i < bmap->n_div; ++i) {
3077 if (isl_int_is_zero(bmap->div[i][0]))
3078 continue;
3079 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
3080 return isl_bool_true;
3083 return isl_bool_false;
3086 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
3087 enum isl_dim_type type, unsigned first, unsigned n)
3089 int i;
3091 if (isl_map_check_range(map, type, first, n) < 0)
3092 return isl_bool_error;
3094 for (i = 0; i < map->n; ++i) {
3095 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
3096 type, first, n);
3097 if (involves < 0 || involves)
3098 return involves;
3101 return isl_bool_false;
3104 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
3105 enum isl_dim_type type, unsigned first, unsigned n)
3107 return isl_basic_map_involves_dims(bset, type, first, n);
3110 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
3111 enum isl_dim_type type, unsigned first, unsigned n)
3113 return isl_map_involves_dims(set, type, first, n);
3116 /* Does "bset" involve any local variables, i.e., integer divisions?
3118 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
3120 isl_size n;
3122 n = isl_basic_set_dim(bset, isl_dim_div);
3123 if (n < 0)
3124 return isl_bool_error;
3125 return isl_bool_ok(n > 0);
3128 /* isl_set_every_basic_set callback that checks whether "bset"
3129 * is free of local variables.
3131 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3133 return isl_bool_not(isl_basic_set_involves_locals(bset));
3136 /* Does "set" involve any local variables, i.e., integer divisions?
3138 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3140 isl_bool no_locals;
3142 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3143 return isl_bool_not(no_locals);
3146 /* Drop all constraints in bmap that involve any of the dimensions
3147 * first to first+n-1.
3148 * This function only performs the actual removal of constraints.
3150 * This function should not call finalize since it is used by
3151 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3153 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3154 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3156 int i;
3158 if (n == 0)
3159 return bmap;
3161 bmap = isl_basic_map_cow(bmap);
3163 if (!bmap)
3164 return NULL;
3166 for (i = bmap->n_eq - 1; i >= 0; --i) {
3167 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3168 continue;
3169 if (isl_basic_map_drop_equality(bmap, i) < 0)
3170 return isl_basic_map_free(bmap);
3173 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3174 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3175 continue;
3176 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3177 return isl_basic_map_free(bmap);
3180 return bmap;
3183 /* Drop all constraints in bset that involve any of the dimensions
3184 * first to first+n-1.
3185 * This function only performs the actual removal of constraints.
3187 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3188 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3190 return isl_basic_map_drop_constraints_involving(bset, first, n);
3193 /* Drop all constraints in bmap that do not involve any of the dimensions
3194 * first to first + n - 1 of the given type.
3196 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3197 __isl_take isl_basic_map *bmap,
3198 enum isl_dim_type type, unsigned first, unsigned n)
3200 int i;
3202 if (n == 0) {
3203 isl_space *space = isl_basic_map_get_space(bmap);
3204 isl_basic_map_free(bmap);
3205 return isl_basic_map_universe(space);
3207 bmap = isl_basic_map_cow(bmap);
3208 if (!bmap)
3209 return NULL;
3211 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3212 return isl_basic_map_free(bmap);
3214 first += isl_basic_map_offset(bmap, type) - 1;
3216 for (i = bmap->n_eq - 1; i >= 0; --i) {
3217 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3218 continue;
3219 if (isl_basic_map_drop_equality(bmap, i) < 0)
3220 return isl_basic_map_free(bmap);
3223 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3224 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3225 continue;
3226 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3227 return isl_basic_map_free(bmap);
3230 bmap = isl_basic_map_add_known_div_constraints(bmap);
3231 return bmap;
3234 /* Drop all constraints in bset that do not involve any of the dimensions
3235 * first to first + n - 1 of the given type.
3237 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3238 __isl_take isl_basic_set *bset,
3239 enum isl_dim_type type, unsigned first, unsigned n)
3241 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3242 type, first, n);
3245 /* Drop all constraints in bmap that involve any of the dimensions
3246 * first to first + n - 1 of the given type.
3248 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3249 __isl_take isl_basic_map *bmap,
3250 enum isl_dim_type type, unsigned first, unsigned n)
3252 if (!bmap)
3253 return NULL;
3254 if (n == 0)
3255 return bmap;
3257 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3258 return isl_basic_map_free(bmap);
3260 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3261 first += isl_basic_map_offset(bmap, type) - 1;
3262 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3263 bmap = isl_basic_map_add_known_div_constraints(bmap);
3264 return bmap;
3267 /* Drop all constraints in bset that involve any of the dimensions
3268 * first to first + n - 1 of the given type.
3270 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3271 __isl_take isl_basic_set *bset,
3272 enum isl_dim_type type, unsigned first, unsigned n)
3274 return isl_basic_map_drop_constraints_involving_dims(bset,
3275 type, first, n);
3278 /* Drop constraints from "map" by applying "drop" to each basic map.
3280 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3281 enum isl_dim_type type, unsigned first, unsigned n,
3282 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3283 enum isl_dim_type type, unsigned first, unsigned n))
3285 int i;
3287 if (isl_map_check_range(map, type, first, n) < 0)
3288 return isl_map_free(map);
3290 map = isl_map_cow(map);
3291 if (!map)
3292 return NULL;
3294 for (i = 0; i < map->n; ++i) {
3295 map->p[i] = drop(map->p[i], type, first, n);
3296 if (!map->p[i])
3297 return isl_map_free(map);
3300 if (map->n > 1)
3301 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3303 return map;
3306 /* Drop all constraints in map that involve any of the dimensions
3307 * first to first + n - 1 of the given type.
3309 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3310 __isl_take isl_map *map,
3311 enum isl_dim_type type, unsigned first, unsigned n)
3313 if (n == 0)
3314 return map;
3315 return drop_constraints(map, type, first, n,
3316 &isl_basic_map_drop_constraints_involving_dims);
3319 /* Drop all constraints in "map" that do not involve any of the dimensions
3320 * first to first + n - 1 of the given type.
3322 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3323 __isl_take isl_map *map,
3324 enum isl_dim_type type, unsigned first, unsigned n)
3326 if (n == 0) {
3327 isl_space *space = isl_map_get_space(map);
3328 isl_map_free(map);
3329 return isl_map_universe(space);
3331 return drop_constraints(map, type, first, n,
3332 &isl_basic_map_drop_constraints_not_involving_dims);
3335 /* Drop all constraints in set that involve any of the dimensions
3336 * first to first + n - 1 of the given type.
3338 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3339 __isl_take isl_set *set,
3340 enum isl_dim_type type, unsigned first, unsigned n)
3342 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3345 /* Drop all constraints in "set" that do not involve any of the dimensions
3346 * first to first + n - 1 of the given type.
3348 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3349 __isl_take isl_set *set,
3350 enum isl_dim_type type, unsigned first, unsigned n)
3352 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3355 /* Does local variable "div" of "bmap" have a complete explicit representation?
3356 * Having a complete explicit representation requires not only
3357 * an explicit representation, but also that all local variables
3358 * that appear in this explicit representation in turn have
3359 * a complete explicit representation.
3361 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3363 int i;
3364 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3365 isl_bool marked;
3367 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3368 if (marked < 0 || marked)
3369 return isl_bool_not(marked);
3371 for (i = bmap->n_div - 1; i >= 0; --i) {
3372 isl_bool known;
3374 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3375 continue;
3376 known = isl_basic_map_div_is_known(bmap, i);
3377 if (known < 0 || !known)
3378 return known;
3381 return isl_bool_true;
3384 /* Remove all divs that are unknown or defined in terms of unknown divs.
3386 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3387 __isl_take isl_basic_map *bmap)
3389 int i;
3391 if (!bmap)
3392 return NULL;
3394 for (i = bmap->n_div - 1; i >= 0; --i) {
3395 if (isl_basic_map_div_is_known(bmap, i))
3396 continue;
3397 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3398 if (!bmap)
3399 return NULL;
3400 i = bmap->n_div;
3403 return bmap;
3406 /* Remove all divs that are unknown or defined in terms of unknown divs.
3408 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3409 __isl_take isl_basic_set *bset)
3411 return isl_basic_map_remove_unknown_divs(bset);
3414 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3416 int i;
3418 if (!map)
3419 return NULL;
3420 if (map->n == 0)
3421 return map;
3423 map = isl_map_cow(map);
3424 if (!map)
3425 return NULL;
3427 for (i = 0; i < map->n; ++i) {
3428 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3429 if (!map->p[i])
3430 goto error;
3432 return map;
3433 error:
3434 isl_map_free(map);
3435 return NULL;
3438 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3440 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3443 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3444 __isl_take isl_basic_set *bset,
3445 enum isl_dim_type type, unsigned first, unsigned n)
3447 isl_basic_map *bmap = bset_to_bmap(bset);
3448 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3449 return bset_from_bmap(bmap);
3452 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3453 enum isl_dim_type type, unsigned first, unsigned n)
3455 int i;
3457 if (n == 0)
3458 return map;
3460 map = isl_map_cow(map);
3461 if (isl_map_check_range(map, type, first, n) < 0)
3462 return isl_map_free(map);
3464 for (i = 0; i < map->n; ++i) {
3465 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3466 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3467 if (!map->p[i])
3468 goto error;
3470 map = isl_map_drop(map, type, first, n);
3471 return map;
3472 error:
3473 isl_map_free(map);
3474 return NULL;
3477 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3478 enum isl_dim_type type, unsigned first, unsigned n)
3480 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3481 type, first, n));
3484 /* Project out n inputs starting at first using Fourier-Motzkin */
3485 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3486 unsigned first, unsigned n)
3488 return isl_map_remove_dims(map, isl_dim_in, first, n);
3491 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3492 FILE *out, int indent)
3494 isl_printer *p;
3496 if (!bset) {
3497 fprintf(out, "null basic set\n");
3498 return;
3501 fprintf(out, "%*s", indent, "");
3502 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3503 bset->ref, bset->dim->nparam, bset->dim->n_out,
3504 bset->extra, bset->flags);
3506 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3507 p = isl_printer_set_dump(p, 1);
3508 p = isl_printer_set_indent(p, indent);
3509 p = isl_printer_start_line(p);
3510 p = isl_printer_print_basic_set(p, bset);
3511 p = isl_printer_end_line(p);
3512 isl_printer_free(p);
3515 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3516 FILE *out, int indent)
3518 isl_printer *p;
3520 if (!bmap) {
3521 fprintf(out, "null basic map\n");
3522 return;
3525 fprintf(out, "%*s", indent, "");
3526 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3527 "flags: %x, n_name: %d\n",
3528 bmap->ref,
3529 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3530 bmap->extra, bmap->flags, bmap->dim->n_id);
3532 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3533 p = isl_printer_set_dump(p, 1);
3534 p = isl_printer_set_indent(p, indent);
3535 p = isl_printer_start_line(p);
3536 p = isl_printer_print_basic_map(p, bmap);
3537 p = isl_printer_end_line(p);
3538 isl_printer_free(p);
3541 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3542 unsigned pos)
3544 isl_size total;
3546 total = isl_basic_map_dim(bmap, isl_dim_all);
3547 if (total < 0)
3548 return isl_basic_map_free(bmap);
3549 if (pos >= bmap->n_ineq)
3550 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3551 "invalid position", return isl_basic_map_free(bmap));
3552 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3553 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3554 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3555 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3556 return bmap;
3559 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3560 unsigned flags)
3562 if (isl_space_check_is_set(space) < 0)
3563 goto error;
3564 return isl_map_alloc_space(space, n, flags);
3565 error:
3566 isl_space_free(space);
3567 return NULL;
3570 /* Make sure "map" has room for at least "n" more basic maps.
3572 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3574 int i;
3575 struct isl_map *grown = NULL;
3577 if (!map)
3578 return NULL;
3579 isl_assert(map->ctx, n >= 0, goto error);
3580 if (map->n + n <= map->size)
3581 return map;
3582 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3583 if (!grown)
3584 goto error;
3585 for (i = 0; i < map->n; ++i) {
3586 grown->p[i] = isl_basic_map_copy(map->p[i]);
3587 if (!grown->p[i])
3588 goto error;
3589 grown->n++;
3591 isl_map_free(map);
3592 return grown;
3593 error:
3594 isl_map_free(grown);
3595 isl_map_free(map);
3596 return NULL;
3599 /* Make sure "set" has room for at least "n" more basic sets.
3601 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3603 return set_from_map(isl_map_grow(set_to_map(set), n));
3606 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3608 return isl_map_from_basic_map(bset);
3611 /* This function performs the same operation as isl_set_from_basic_set,
3612 * but is considered as a function on an isl_basic_set when exported.
3614 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3616 return isl_set_from_basic_set(bset);
3619 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3621 struct isl_map *map;
3623 if (!bmap)
3624 return NULL;
3626 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3627 return isl_map_add_basic_map(map, bmap);
3630 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3631 __isl_take isl_basic_set *bset)
3633 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3634 bset_to_bmap(bset)));
3637 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3639 return isl_map_free(set);
3642 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3644 int i;
3646 if (!set) {
3647 fprintf(out, "null set\n");
3648 return;
3651 fprintf(out, "%*s", indent, "");
3652 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3653 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3654 set->flags);
3655 for (i = 0; i < set->n; ++i) {
3656 fprintf(out, "%*s", indent, "");
3657 fprintf(out, "basic set %d:\n", i);
3658 isl_basic_set_print_internal(set->p[i], out, indent+4);
3662 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3664 int i;
3666 if (!map) {
3667 fprintf(out, "null map\n");
3668 return;
3671 fprintf(out, "%*s", indent, "");
3672 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3673 "flags: %x, n_name: %d\n",
3674 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3675 map->dim->n_out, map->flags, map->dim->n_id);
3676 for (i = 0; i < map->n; ++i) {
3677 fprintf(out, "%*s", indent, "");
3678 fprintf(out, "basic map %d:\n", i);
3679 isl_basic_map_print_internal(map->p[i], out, indent+4);
3683 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3685 static isl_stat isl_basic_map_check_compatible_domain(
3686 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3688 isl_bool ok;
3690 ok = isl_basic_map_compatible_domain(bmap, bset);
3691 if (ok < 0)
3692 return isl_stat_error;
3693 if (!ok)
3694 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3695 "incompatible spaces", return isl_stat_error);
3697 return isl_stat_ok;
3700 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3701 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3703 struct isl_basic_map *bmap_domain;
3704 isl_size dim;
3706 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3707 goto error;
3709 dim = isl_basic_set_dim(bset, isl_dim_set);
3710 if (dim < 0)
3711 goto error;
3712 if (dim != 0 &&
3713 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3714 goto error;
3716 bmap = isl_basic_map_cow(bmap);
3717 if (!bmap)
3718 goto error;
3719 bmap = isl_basic_map_extend(bmap,
3720 bset->n_div, bset->n_eq, bset->n_ineq);
3721 bmap_domain = isl_basic_map_from_domain(bset);
3722 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3724 bmap = isl_basic_map_simplify(bmap);
3725 return isl_basic_map_finalize(bmap);
3726 error:
3727 isl_basic_map_free(bmap);
3728 isl_basic_set_free(bset);
3729 return NULL;
3732 /* Check that the space of "bset" is the same as that of the range of "bmap".
3734 static isl_stat isl_basic_map_check_compatible_range(
3735 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3737 isl_bool ok;
3739 ok = isl_basic_map_compatible_range(bmap, bset);
3740 if (ok < 0)
3741 return isl_stat_error;
3742 if (!ok)
3743 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3744 "incompatible spaces", return isl_stat_error);
3746 return isl_stat_ok;
3749 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3750 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3752 struct isl_basic_map *bmap_range;
3753 isl_size dim;
3755 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3756 goto error;
3758 dim = isl_basic_set_dim(bset, isl_dim_set);
3759 if (dim < 0)
3760 goto error;
3761 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3762 goto error;
3764 if (isl_basic_set_plain_is_universe(bset)) {
3765 isl_basic_set_free(bset);
3766 return bmap;
3769 bmap = isl_basic_map_cow(bmap);
3770 if (!bmap)
3771 goto error;
3772 bmap = isl_basic_map_extend(bmap,
3773 bset->n_div, bset->n_eq, bset->n_ineq);
3774 bmap_range = bset_to_bmap(bset);
3775 bmap = add_constraints(bmap, bmap_range, 0, 0);
3777 bmap = isl_basic_map_simplify(bmap);
3778 return isl_basic_map_finalize(bmap);
3779 error:
3780 isl_basic_map_free(bmap);
3781 isl_basic_set_free(bset);
3782 return NULL;
3785 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3786 __isl_keep isl_vec *vec)
3788 int i;
3789 isl_size total;
3790 isl_int s;
3792 total = isl_basic_map_dim(bmap, isl_dim_all);
3793 if (total < 0 || !vec)
3794 return isl_bool_error;
3796 if (1 + total != vec->size)
3797 return isl_bool_false;
3799 isl_int_init(s);
3801 for (i = 0; i < bmap->n_eq; ++i) {
3802 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3803 if (!isl_int_is_zero(s)) {
3804 isl_int_clear(s);
3805 return isl_bool_false;
3809 for (i = 0; i < bmap->n_ineq; ++i) {
3810 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3811 if (isl_int_is_neg(s)) {
3812 isl_int_clear(s);
3813 return isl_bool_false;
3817 isl_int_clear(s);
3819 return isl_bool_true;
3822 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3823 __isl_keep isl_vec *vec)
3825 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3828 __isl_give isl_basic_map *isl_basic_map_intersect(
3829 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3831 struct isl_vec *sample = NULL;
3832 isl_space *space1, *space2;
3833 isl_size dim1, dim2, nparam1, nparam2;
3835 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3836 goto error;
3837 space1 = isl_basic_map_peek_space(bmap1);
3838 space2 = isl_basic_map_peek_space(bmap2);
3839 dim1 = isl_space_dim(space1, isl_dim_all);
3840 dim2 = isl_space_dim(space2, isl_dim_all);
3841 nparam1 = isl_space_dim(space1, isl_dim_param);
3842 nparam2 = isl_space_dim(space2, isl_dim_param);
3843 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3844 goto error;
3845 if (dim1 == nparam1 && dim2 != nparam2)
3846 return isl_basic_map_intersect(bmap2, bmap1);
3848 if (dim2 != nparam2 &&
3849 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3850 goto error;
3852 if (isl_basic_map_plain_is_empty(bmap1)) {
3853 isl_basic_map_free(bmap2);
3854 return bmap1;
3856 if (isl_basic_map_plain_is_empty(bmap2)) {
3857 isl_basic_map_free(bmap1);
3858 return bmap2;
3861 if (bmap1->sample &&
3862 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3863 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3864 sample = isl_vec_copy(bmap1->sample);
3865 else if (bmap2->sample &&
3866 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3867 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3868 sample = isl_vec_copy(bmap2->sample);
3870 bmap1 = isl_basic_map_cow(bmap1);
3871 if (!bmap1)
3872 goto error;
3873 bmap1 = isl_basic_map_extend(bmap1,
3874 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3875 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3877 if (!bmap1)
3878 isl_vec_free(sample);
3879 else if (sample) {
3880 isl_vec_free(bmap1->sample);
3881 bmap1->sample = sample;
3884 bmap1 = isl_basic_map_simplify(bmap1);
3885 return isl_basic_map_finalize(bmap1);
3886 error:
3887 if (sample)
3888 isl_vec_free(sample);
3889 isl_basic_map_free(bmap1);
3890 isl_basic_map_free(bmap2);
3891 return NULL;
3894 __isl_give isl_basic_set *isl_basic_set_intersect(
3895 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3897 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3898 bset_to_bmap(bset2)));
3901 /* Intersect the parameter domain of "bmap" with "bset".
3903 * isl_basic_map_intersect handles this as a special case.
3905 __isl_give isl_basic_map *isl_basic_map_intersect_params(
3906 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3908 return isl_basic_map_intersect(bmap, bset);
3911 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3912 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3914 isl_basic_map *bmap;
3916 bmap = bset_to_bmap(bset1);
3917 bmap = isl_basic_map_intersect_params(bmap, bset2);
3918 return bset_from_bmap(bmap);
3921 /* Does "map" consist of a single disjunct, without any local variables?
3923 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3925 isl_size n_div;
3927 if (!map)
3928 return isl_bool_error;
3929 if (map->n != 1)
3930 return isl_bool_false;
3931 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3932 if (n_div < 0)
3933 return isl_bool_error;
3934 if (n_div != 0)
3935 return isl_bool_false;
3936 return isl_bool_true;
3939 /* Check that "map" consists of a single disjunct, without any local variables.
3941 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3943 isl_bool ok;
3945 ok = is_convex_no_locals(map);
3946 if (ok < 0)
3947 return isl_stat_error;
3948 if (ok)
3949 return isl_stat_ok;
3951 isl_die(isl_map_get_ctx(map), isl_error_internal,
3952 "unexpectedly not convex or involving local variables",
3953 return isl_stat_error);
3956 /* Special case of isl_map_intersect, where both map1 and map2
3957 * are convex, without any divs and such that either map1 or map2
3958 * contains a single constraint. This constraint is then simply
3959 * added to the other map.
3961 static __isl_give isl_map *map_intersect_add_constraint(
3962 __isl_take isl_map *map1, __isl_take isl_map *map2)
3964 if (check_convex_no_locals(map1) < 0 ||
3965 check_convex_no_locals(map2) < 0)
3966 goto error;
3968 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3969 return isl_map_intersect(map2, map1);
3971 map1 = isl_map_cow(map1);
3972 if (!map1)
3973 goto error;
3974 if (isl_map_plain_is_empty(map1)) {
3975 isl_map_free(map2);
3976 return map1;
3978 if (map2->p[0]->n_eq == 1)
3979 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3980 else
3981 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3982 map2->p[0]->ineq[0]);
3984 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3985 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3986 if (!map1->p[0])
3987 goto error;
3989 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3990 isl_basic_map_free(map1->p[0]);
3991 map1->n = 0;
3994 isl_map_free(map2);
3996 map1 = isl_map_unmark_normalized(map1);
3997 return map1;
3998 error:
3999 isl_map_free(map1);
4000 isl_map_free(map2);
4001 return NULL;
4004 /* map2 may be either a parameter domain or a map living in the same
4005 * space as map1.
4007 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
4008 __isl_take isl_map *map2)
4010 unsigned flags = 0;
4011 isl_bool equal;
4012 isl_map *result;
4013 int i, j;
4014 isl_size dim2, nparam2;
4016 if (!map1 || !map2)
4017 goto error;
4019 if ((isl_map_plain_is_empty(map1) ||
4020 isl_map_plain_is_universe(map2)) &&
4021 isl_space_is_equal(map1->dim, map2->dim)) {
4022 isl_map_free(map2);
4023 return map1;
4025 if ((isl_map_plain_is_empty(map2) ||
4026 isl_map_plain_is_universe(map1)) &&
4027 isl_space_is_equal(map1->dim, map2->dim)) {
4028 isl_map_free(map1);
4029 return map2;
4032 if (is_convex_no_locals(map1) == isl_bool_true &&
4033 is_convex_no_locals(map2) == isl_bool_true &&
4034 isl_space_is_equal(map1->dim, map2->dim) &&
4035 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
4036 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
4037 return map_intersect_add_constraint(map1, map2);
4039 equal = isl_map_plain_is_equal(map1, map2);
4040 if (equal < 0)
4041 goto error;
4042 if (equal) {
4043 isl_map_free(map2);
4044 return map1;
4047 dim2 = isl_map_dim(map2, isl_dim_all);
4048 nparam2 = isl_map_dim(map2, isl_dim_param);
4049 if (dim2 < 0 || nparam2 < 0)
4050 goto error;
4051 if (dim2 != nparam2)
4052 isl_assert(map1->ctx,
4053 isl_space_is_equal(map1->dim, map2->dim), goto error);
4055 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4056 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4057 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4059 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4060 map1->n * map2->n, flags);
4061 if (!result)
4062 goto error;
4063 for (i = 0; i < map1->n; ++i)
4064 for (j = 0; j < map2->n; ++j) {
4065 struct isl_basic_map *part;
4066 part = isl_basic_map_intersect(
4067 isl_basic_map_copy(map1->p[i]),
4068 isl_basic_map_copy(map2->p[j]));
4069 if (isl_basic_map_is_empty(part) < 0)
4070 part = isl_basic_map_free(part);
4071 result = isl_map_add_basic_map(result, part);
4072 if (!result)
4073 goto error;
4075 isl_map_free(map1);
4076 isl_map_free(map2);
4077 return result;
4078 error:
4079 isl_map_free(map1);
4080 isl_map_free(map2);
4081 return NULL;
4084 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
4085 __isl_take isl_map *map2)
4087 if (isl_map_check_equal_space(map1, map2) < 0)
4088 goto error;
4089 return map_intersect_internal(map1, map2);
4090 error:
4091 isl_map_free(map1);
4092 isl_map_free(map2);
4093 return NULL;
4096 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
4097 __isl_take isl_map *map2)
4099 isl_map_align_params_bin(&map1, &map2);
4100 return map_intersect(map1, map2);
4103 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
4104 __isl_take isl_set *set2)
4106 return set_from_map(isl_map_intersect(set_to_map(set1),
4107 set_to_map(set2)));
4110 /* map_intersect_internal accepts intersections
4111 * with parameter domains, so we can just call that function.
4113 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
4114 __isl_take isl_set *params)
4116 isl_map_align_params_set(&map, &params);
4117 return map_intersect_internal(map, params);
4120 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4121 __isl_take isl_set *params)
4123 return isl_map_intersect_params(set, params);
4126 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4128 isl_space *space;
4129 unsigned pos;
4130 isl_size n1, n2;
4132 if (!bmap)
4133 return NULL;
4134 bmap = isl_basic_map_cow(bmap);
4135 if (!bmap)
4136 return NULL;
4137 space = isl_space_reverse(isl_space_copy(bmap->dim));
4138 pos = isl_basic_map_offset(bmap, isl_dim_in);
4139 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4140 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4141 if (n1 < 0 || n2 < 0)
4142 bmap = isl_basic_map_free(bmap);
4143 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4144 return isl_basic_map_reset_space(bmap, space);
4147 /* Given a basic map where the tuple of type "type" is a wrapped map,
4148 * swap domain and range of that wrapped map.
4150 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4151 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4153 isl_space *space;
4154 isl_size offset, n1, n2;
4156 space = isl_basic_map_peek_space(bmap);
4157 offset = isl_basic_map_var_offset(bmap, type);
4158 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4159 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4160 if (offset < 0 || n1 < 0 || n2 < 0)
4161 return isl_basic_map_free(bmap);
4163 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4165 space = isl_basic_map_take_space(bmap);
4166 space = isl_space_reverse_wrapped(space, type);
4167 bmap = isl_basic_map_restore_space(bmap, space);
4169 return bmap;
4172 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4173 * (B -> A) -> C.
4175 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4176 __isl_take isl_basic_map *bmap)
4178 isl_space *space;
4180 space = isl_basic_map_peek_space(bmap);
4181 if (isl_space_check_domain_is_wrapping(space) < 0)
4182 return isl_basic_map_free(bmap);
4183 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4185 return bmap;
4188 /* Given a basic map A -> (B -> C), return the corresponding basic map
4189 * A -> (C -> B).
4191 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4192 __isl_take isl_basic_map *bmap)
4194 isl_space *space;
4196 space = isl_basic_map_peek_space(bmap);
4197 if (isl_space_check_range_is_wrapping(space) < 0)
4198 return isl_basic_map_free(bmap);
4199 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4201 return bmap;
4204 /* Given a basic map that is actually a basic set (A -> B),
4205 * return the corresponding basic set (B -> A) as a basic map.
4207 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4208 __isl_take isl_basic_map *bmap)
4210 isl_space *space;
4212 space = isl_basic_map_peek_space(bmap);
4213 if (isl_space_check_is_wrapping(space) < 0)
4214 return isl_basic_map_free(bmap);
4215 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4217 return bmap;
4220 static __isl_give isl_basic_map *basic_map_space_reset(
4221 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4223 isl_space *space;
4225 if (!bmap)
4226 return NULL;
4227 if (!isl_space_is_named_or_nested(bmap->dim, type))
4228 return bmap;
4230 space = isl_basic_map_get_space(bmap);
4231 space = isl_space_reset(space, type);
4232 bmap = isl_basic_map_reset_space(bmap, space);
4233 return bmap;
4236 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4237 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4238 unsigned pos, unsigned n)
4240 isl_bool rational, is_empty;
4241 isl_space *res_space;
4242 struct isl_basic_map *res;
4243 struct isl_dim_map *dim_map;
4244 isl_size total;
4245 unsigned off;
4246 enum isl_dim_type t;
4248 if (n == 0)
4249 return basic_map_space_reset(bmap, type);
4251 is_empty = isl_basic_map_plain_is_empty(bmap);
4252 total = isl_basic_map_dim(bmap, isl_dim_all);
4253 if (is_empty < 0 || total < 0)
4254 return isl_basic_map_free(bmap);
4255 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4256 type, pos, n);
4257 if (!res_space)
4258 return isl_basic_map_free(bmap);
4259 if (is_empty) {
4260 isl_basic_map_free(bmap);
4261 return isl_basic_map_empty(res_space);
4264 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4265 off = 0;
4266 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4267 isl_size dim;
4269 if (t != type) {
4270 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4271 } else {
4272 isl_size size = isl_basic_map_dim(bmap, t);
4273 if (size < 0)
4274 dim_map = isl_dim_map_free(dim_map);
4275 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4276 0, pos, off);
4277 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4278 pos, size - pos, off + pos + n);
4280 dim = isl_space_dim(res_space, t);
4281 if (dim < 0)
4282 dim_map = isl_dim_map_free(dim_map);
4283 off += dim;
4285 isl_dim_map_div(dim_map, bmap, off);
4287 res = isl_basic_map_alloc_space(res_space,
4288 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4289 rational = isl_basic_map_is_rational(bmap);
4290 if (rational < 0)
4291 res = isl_basic_map_free(res);
4292 if (rational)
4293 res = isl_basic_map_set_rational(res);
4294 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4295 return isl_basic_map_finalize(res);
4298 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4299 __isl_take isl_basic_set *bset,
4300 enum isl_dim_type type, unsigned pos, unsigned n)
4302 return isl_basic_map_insert_dims(bset, type, pos, n);
4305 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4306 enum isl_dim_type type, unsigned n)
4308 isl_size dim;
4310 dim = isl_basic_map_dim(bmap, type);
4311 if (dim < 0)
4312 return isl_basic_map_free(bmap);
4313 return isl_basic_map_insert_dims(bmap, type, dim, n);
4316 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4317 enum isl_dim_type type, unsigned n)
4319 if (!bset)
4320 return NULL;
4321 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4322 return isl_basic_map_add_dims(bset, type, n);
4323 error:
4324 isl_basic_set_free(bset);
4325 return NULL;
4328 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4329 enum isl_dim_type type)
4331 isl_space *space;
4333 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4334 return map;
4336 space = isl_map_get_space(map);
4337 space = isl_space_reset(space, type);
4338 map = isl_map_reset_space(map, space);
4339 return map;
4342 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4343 enum isl_dim_type type, unsigned pos, unsigned n)
4345 int i;
4346 isl_space *space;
4348 if (n == 0)
4349 return map_space_reset(map, type);
4351 map = isl_map_cow(map);
4352 if (!map)
4353 return NULL;
4355 for (i = 0; i < map->n; ++i) {
4356 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4357 if (!map->p[i])
4358 goto error;
4361 space = isl_map_take_space(map);
4362 space = isl_space_insert_dims(space, type, pos, n);
4363 map = isl_map_restore_space(map, space);
4365 return map;
4366 error:
4367 isl_map_free(map);
4368 return NULL;
4371 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4372 enum isl_dim_type type, unsigned pos, unsigned n)
4374 return isl_map_insert_dims(set, type, pos, n);
4377 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4378 enum isl_dim_type type, unsigned n)
4380 isl_size dim;
4382 dim = isl_map_dim(map, type);
4383 if (dim < 0)
4384 return isl_map_free(map);
4385 return isl_map_insert_dims(map, type, dim, n);
4388 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4389 enum isl_dim_type type, unsigned n)
4391 if (!set)
4392 return NULL;
4393 isl_assert(set->ctx, type != isl_dim_in, goto error);
4394 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4395 error:
4396 isl_set_free(set);
4397 return NULL;
4400 __isl_give isl_basic_map *isl_basic_map_move_dims(
4401 __isl_take isl_basic_map *bmap,
4402 enum isl_dim_type dst_type, unsigned dst_pos,
4403 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4405 isl_space *space;
4406 struct isl_dim_map *dim_map;
4407 struct isl_basic_map *res;
4408 enum isl_dim_type t;
4409 isl_size total;
4410 unsigned off;
4412 if (!bmap)
4413 return NULL;
4414 if (n == 0) {
4415 bmap = isl_basic_map_reset(bmap, src_type);
4416 bmap = isl_basic_map_reset(bmap, dst_type);
4417 return bmap;
4420 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4421 return isl_basic_map_free(bmap);
4423 if (dst_type == src_type && dst_pos == src_pos)
4424 return bmap;
4426 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4428 if (pos(bmap->dim, dst_type) + dst_pos ==
4429 pos(bmap->dim, src_type) + src_pos +
4430 ((src_type < dst_type) ? n : 0)) {
4431 space = isl_basic_map_take_space(bmap);
4432 space = isl_space_move_dims(space, dst_type, dst_pos,
4433 src_type, src_pos, n);
4434 bmap = isl_basic_map_restore_space(bmap, space);
4435 bmap = isl_basic_map_finalize(bmap);
4437 return bmap;
4440 total = isl_basic_map_dim(bmap, isl_dim_all);
4441 if (total < 0)
4442 return isl_basic_map_free(bmap);
4443 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4445 off = 0;
4446 space = isl_basic_map_peek_space(bmap);
4447 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4448 isl_size size = isl_space_dim(space, t);
4449 if (size < 0)
4450 dim_map = isl_dim_map_free(dim_map);
4451 if (t == dst_type) {
4452 isl_dim_map_dim_range(dim_map, space, t,
4453 0, dst_pos, off);
4454 off += dst_pos;
4455 isl_dim_map_dim_range(dim_map, space, src_type,
4456 src_pos, n, off);
4457 off += n;
4458 isl_dim_map_dim_range(dim_map, space, t,
4459 dst_pos, size - dst_pos, off);
4460 off += size - dst_pos;
4461 } else if (t == src_type) {
4462 isl_dim_map_dim_range(dim_map, space, t,
4463 0, src_pos, off);
4464 off += src_pos;
4465 isl_dim_map_dim_range(dim_map, space, t,
4466 src_pos + n, size - src_pos - n, off);
4467 off += size - src_pos - n;
4468 } else {
4469 isl_dim_map_dim(dim_map, space, t, off);
4470 off += size;
4473 isl_dim_map_div(dim_map, bmap, off);
4475 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4476 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4477 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4478 space = isl_basic_map_take_space(bmap);
4479 space = isl_space_move_dims(space, dst_type, dst_pos,
4480 src_type, src_pos, n);
4481 bmap = isl_basic_map_restore_space(bmap, space);
4482 if (!bmap)
4483 goto error;
4485 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4486 bmap = isl_basic_map_gauss(bmap, NULL);
4487 bmap = isl_basic_map_finalize(bmap);
4489 return bmap;
4490 error:
4491 isl_basic_map_free(bmap);
4492 return NULL;
4495 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4496 enum isl_dim_type dst_type, unsigned dst_pos,
4497 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4499 isl_basic_map *bmap = bset_to_bmap(bset);
4500 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4501 src_type, src_pos, n);
4502 return bset_from_bmap(bmap);
4505 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4506 enum isl_dim_type dst_type, unsigned dst_pos,
4507 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4509 if (!set)
4510 return NULL;
4511 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4512 return set_from_map(isl_map_move_dims(set_to_map(set),
4513 dst_type, dst_pos, src_type, src_pos, n));
4514 error:
4515 isl_set_free(set);
4516 return NULL;
4519 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4520 enum isl_dim_type dst_type, unsigned dst_pos,
4521 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4523 int i;
4524 isl_space *space;
4526 if (n == 0) {
4527 map = isl_map_reset(map, src_type);
4528 map = isl_map_reset(map, dst_type);
4529 return map;
4532 if (isl_map_check_range(map, src_type, src_pos, n))
4533 return isl_map_free(map);
4535 if (dst_type == src_type && dst_pos == src_pos)
4536 return map;
4538 isl_assert(map->ctx, dst_type != src_type, goto error);
4540 map = isl_map_cow(map);
4541 if (!map)
4542 return NULL;
4544 for (i = 0; i < map->n; ++i) {
4545 map->p[i] = isl_basic_map_move_dims(map->p[i],
4546 dst_type, dst_pos,
4547 src_type, src_pos, n);
4548 if (!map->p[i])
4549 goto error;
4552 space = isl_map_take_space(map);
4553 space = isl_space_move_dims(space, dst_type, dst_pos,
4554 src_type, src_pos, n);
4555 map = isl_map_restore_space(map, space);
4557 return map;
4558 error:
4559 isl_map_free(map);
4560 return NULL;
4563 /* Move the specified dimensions to the last columns right before
4564 * the divs. Don't change the dimension specification of bmap.
4565 * That's the responsibility of the caller.
4567 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4568 enum isl_dim_type type, unsigned first, unsigned n)
4570 isl_space *space;
4571 struct isl_dim_map *dim_map;
4572 struct isl_basic_map *res;
4573 enum isl_dim_type t;
4574 isl_size total;
4575 unsigned off;
4577 if (!bmap)
4578 return NULL;
4579 if (isl_basic_map_offset(bmap, type) + first + n ==
4580 isl_basic_map_offset(bmap, isl_dim_div))
4581 return bmap;
4583 total = isl_basic_map_dim(bmap, isl_dim_all);
4584 if (total < 0)
4585 return isl_basic_map_free(bmap);
4586 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4588 off = 0;
4589 space = isl_basic_map_peek_space(bmap);
4590 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4591 isl_size size = isl_space_dim(space, t);
4592 if (size < 0)
4593 dim_map = isl_dim_map_free(dim_map);
4594 if (t == type) {
4595 isl_dim_map_dim_range(dim_map, space, t,
4596 0, first, off);
4597 off += first;
4598 isl_dim_map_dim_range(dim_map, space, t,
4599 first, n, total - bmap->n_div - n);
4600 isl_dim_map_dim_range(dim_map, space, t,
4601 first + n, size - (first + n), off);
4602 off += size - (first + n);
4603 } else {
4604 isl_dim_map_dim(dim_map, space, t, off);
4605 off += size;
4608 isl_dim_map_div(dim_map, bmap, off + n);
4610 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4611 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4612 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4613 return res;
4616 /* Insert "n" rows in the divs of "bmap".
4618 * The number of columns is not changed, which means that the last
4619 * dimensions of "bmap" are being reintepreted as the new divs.
4620 * The space of "bmap" is not adjusted, however, which means
4621 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4622 * from the space of "bmap" is the responsibility of the caller.
4624 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4625 int n)
4627 int i;
4628 size_t row_size;
4629 isl_int **new_div;
4630 isl_int *old;
4632 bmap = isl_basic_map_cow(bmap);
4633 if (!bmap)
4634 return NULL;
4636 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4637 old = bmap->block2.data;
4638 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4639 (bmap->extra + n) * (1 + row_size));
4640 if (!bmap->block2.data)
4641 return isl_basic_map_free(bmap);
4642 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4643 if (!new_div)
4644 return isl_basic_map_free(bmap);
4645 for (i = 0; i < n; ++i) {
4646 new_div[i] = bmap->block2.data +
4647 (bmap->extra + i) * (1 + row_size);
4648 isl_seq_clr(new_div[i], 1 + row_size);
4650 for (i = 0; i < bmap->extra; ++i)
4651 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4652 free(bmap->div);
4653 bmap->div = new_div;
4654 bmap->n_div += n;
4655 bmap->extra += n;
4657 return bmap;
4660 /* Drop constraints from "bmap" that only involve the variables
4661 * of "type" in the range [first, first + n] that are not related
4662 * to any of the variables outside that interval.
4663 * These constraints cannot influence the values for the variables
4664 * outside the interval, except in case they cause "bmap" to be empty.
4665 * Only drop the constraints if "bmap" is known to be non-empty.
4667 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4668 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4669 unsigned first, unsigned n)
4671 int i;
4672 int *groups;
4673 isl_size dim, n_div;
4674 isl_bool non_empty;
4676 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4677 if (non_empty < 0)
4678 return isl_basic_map_free(bmap);
4679 if (!non_empty)
4680 return bmap;
4682 dim = isl_basic_map_dim(bmap, isl_dim_all);
4683 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4684 if (dim < 0 || n_div < 0)
4685 return isl_basic_map_free(bmap);
4686 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4687 if (!groups)
4688 return isl_basic_map_free(bmap);
4689 first += isl_basic_map_offset(bmap, type) - 1;
4690 for (i = 0; i < first; ++i)
4691 groups[i] = -1;
4692 for (i = first + n; i < dim - n_div; ++i)
4693 groups[i] = -1;
4695 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4697 return bmap;
4700 /* Turn the n dimensions of type type, starting at first
4701 * into existentially quantified variables.
4703 * If a subset of the projected out variables are unrelated
4704 * to any of the variables that remain, then the constraints
4705 * involving this subset are simply dropped first.
4707 __isl_give isl_basic_map *isl_basic_map_project_out(
4708 __isl_take isl_basic_map *bmap,
4709 enum isl_dim_type type, unsigned first, unsigned n)
4711 isl_bool empty;
4712 isl_space *space;
4714 if (n == 0)
4715 return basic_map_space_reset(bmap, type);
4716 if (type == isl_dim_div)
4717 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4718 "cannot project out existentially quantified variables",
4719 return isl_basic_map_free(bmap));
4721 empty = isl_basic_map_plain_is_empty(bmap);
4722 if (empty < 0)
4723 return isl_basic_map_free(bmap);
4724 if (empty)
4725 bmap = isl_basic_map_set_to_empty(bmap);
4727 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4728 if (!bmap)
4729 return NULL;
4731 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4732 return isl_basic_map_remove_dims(bmap, type, first, n);
4734 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4735 return isl_basic_map_free(bmap);
4737 bmap = move_last(bmap, type, first, n);
4738 bmap = isl_basic_map_cow(bmap);
4739 bmap = insert_div_rows(bmap, n);
4741 space = isl_basic_map_take_space(bmap);
4742 space = isl_space_drop_dims(space, type, first, n);
4743 bmap = isl_basic_map_restore_space(bmap, space);
4744 bmap = isl_basic_map_simplify(bmap);
4745 bmap = isl_basic_map_drop_redundant_divs(bmap);
4746 return isl_basic_map_finalize(bmap);
4749 /* Turn the n dimensions of type type, starting at first
4750 * into existentially quantified variables.
4752 __isl_give isl_basic_set *isl_basic_set_project_out(
4753 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4754 unsigned first, unsigned n)
4756 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4757 type, first, n));
4760 /* Turn the n dimensions of type type, starting at first
4761 * into existentially quantified variables.
4763 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4764 enum isl_dim_type type, unsigned first, unsigned n)
4766 int i;
4767 isl_space *space;
4769 if (n == 0)
4770 return map_space_reset(map, type);
4772 if (isl_map_check_range(map, type, first, n) < 0)
4773 return isl_map_free(map);
4775 map = isl_map_cow(map);
4776 if (!map)
4777 return NULL;
4779 for (i = 0; i < map->n; ++i) {
4780 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4781 if (!map->p[i])
4782 goto error;
4785 if (map->n > 1)
4786 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4787 map = isl_map_unmark_normalized(map);
4789 space = isl_map_take_space(map);
4790 space = isl_space_drop_dims(space, type, first, n);
4791 map = isl_map_restore_space(map, space);
4793 return map;
4794 error:
4795 isl_map_free(map);
4796 return NULL;
4799 #undef TYPE
4800 #define TYPE isl_map
4801 #include "isl_project_out_all_params_templ.c"
4802 #include "isl_project_out_param_templ.c"
4804 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4805 * into existentially quantified variables.
4807 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4808 enum isl_dim_type type, unsigned first, unsigned n)
4810 isl_size dim;
4812 dim = isl_map_dim(map, type);
4813 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4814 return isl_map_free(map);
4815 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4816 map = isl_map_project_out(map, type, 0, first);
4817 return map;
4820 /* Turn the n dimensions of type type, starting at first
4821 * into existentially quantified variables.
4823 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4824 enum isl_dim_type type, unsigned first, unsigned n)
4826 return set_from_map(isl_map_project_out(set_to_map(set),
4827 type, first, n));
4830 /* If "set" involves a parameter with identifier "id",
4831 * then turn it into an existentially quantified variable.
4833 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4834 __isl_take isl_id *id)
4836 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4839 /* If "set" involves any of the parameters with identifiers in "list",
4840 * then turn them into existentially quantified variables.
4842 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4843 __isl_take isl_id_list *list)
4845 isl_map *map;
4847 map = set_to_map(set);
4848 map = isl_map_project_out_param_id_list(map, list);
4849 return set_from_map(map);
4852 /* Project out all parameters from "set" by existentially quantifying
4853 * over them.
4855 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4857 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4860 /* Return a map that projects the elements in "set" onto their
4861 * "n" set dimensions starting at "first".
4862 * "type" should be equal to isl_dim_set.
4864 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4865 enum isl_dim_type type, unsigned first, unsigned n)
4867 int i;
4868 isl_map *map;
4870 if (type != isl_dim_set)
4871 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4872 "only set dimensions can be projected out", goto error);
4873 if (isl_set_check_range(set, type, first, n) < 0)
4874 return isl_set_free(set);
4876 map = isl_map_from_domain(set);
4877 map = isl_map_add_dims(map, isl_dim_out, n);
4878 for (i = 0; i < n; ++i)
4879 map = isl_map_equate(map, isl_dim_in, first + i,
4880 isl_dim_out, i);
4881 return map;
4882 error:
4883 isl_set_free(set);
4884 return NULL;
4887 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4888 unsigned n)
4890 int i, j;
4891 isl_size total;
4893 total = isl_basic_map_dim(bmap, isl_dim_all);
4894 if (total < 0)
4895 return isl_basic_map_free(bmap);
4896 for (i = 0; i < n; ++i) {
4897 j = isl_basic_map_alloc_div(bmap);
4898 if (j < 0)
4899 goto error;
4900 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4902 return bmap;
4903 error:
4904 isl_basic_map_free(bmap);
4905 return NULL;
4908 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4910 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4911 __isl_keep isl_basic_map *bmap2)
4913 isl_space *space1, *space2;
4915 space1 = isl_basic_map_peek_space(bmap1);
4916 space2 = isl_basic_map_peek_space(bmap2);
4917 return isl_space_tuple_is_equal(space1, isl_dim_out,
4918 space2, isl_dim_in);
4921 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4923 static isl_stat isl_basic_map_check_applies_range(
4924 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4926 isl_bool equal;
4928 equal = isl_basic_map_applies_range(bmap1, bmap2);
4929 if (equal < 0)
4930 return isl_stat_error;
4931 if (!equal)
4932 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4933 "spaces don't match", return isl_stat_error);
4934 return isl_stat_ok;
4937 __isl_give isl_basic_map *isl_basic_map_apply_range(
4938 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4940 isl_space *space_result = NULL;
4941 struct isl_basic_map *bmap;
4942 isl_size n_in, n_out, n, nparam;
4943 unsigned total, pos;
4944 struct isl_dim_map *dim_map1, *dim_map2;
4946 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4947 goto error;
4948 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4949 goto error;
4951 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4952 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4953 n = isl_basic_map_dim(bmap1, isl_dim_out);
4954 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4955 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4956 goto error;
4958 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4959 isl_basic_map_get_space(bmap2));
4961 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4962 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4963 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4964 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4965 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4966 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4967 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4968 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4969 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4970 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4971 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4973 bmap = isl_basic_map_alloc_space(space_result,
4974 bmap1->n_div + bmap2->n_div + n,
4975 bmap1->n_eq + bmap2->n_eq,
4976 bmap1->n_ineq + bmap2->n_ineq);
4977 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4978 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4979 bmap = add_divs(bmap, n);
4980 bmap = isl_basic_map_simplify(bmap);
4981 bmap = isl_basic_map_drop_redundant_divs(bmap);
4982 return isl_basic_map_finalize(bmap);
4983 error:
4984 isl_basic_map_free(bmap1);
4985 isl_basic_map_free(bmap2);
4986 return NULL;
4989 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4990 __isl_take isl_basic_map *bmap)
4992 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4993 goto error;
4995 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4996 bmap));
4997 error:
4998 isl_basic_set_free(bset);
4999 isl_basic_map_free(bmap);
5000 return NULL;
5003 __isl_give isl_basic_map *isl_basic_map_apply_domain(
5004 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
5006 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
5007 goto error;
5008 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
5009 bmap2->dim, isl_dim_in))
5010 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
5011 "spaces don't match", goto error);
5013 bmap1 = isl_basic_map_reverse(bmap1);
5014 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
5015 return isl_basic_map_reverse(bmap1);
5016 error:
5017 isl_basic_map_free(bmap1);
5018 isl_basic_map_free(bmap2);
5019 return NULL;
5022 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5023 * A \cap B -> f(A) + f(B)
5025 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
5026 __isl_take isl_basic_map *bmap2)
5028 isl_size n_in, n_out, nparam;
5029 unsigned total, pos;
5030 struct isl_basic_map *bmap = NULL;
5031 struct isl_dim_map *dim_map1, *dim_map2;
5032 int i;
5034 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
5035 goto error;
5037 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
5038 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
5039 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
5040 if (nparam < 0 || n_in < 0 || n_out < 0)
5041 goto error;
5043 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
5044 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5045 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
5046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
5048 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5049 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
5050 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
5051 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5052 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
5053 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
5055 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
5056 bmap1->n_div + bmap2->n_div + 2 * n_out,
5057 bmap1->n_eq + bmap2->n_eq + n_out,
5058 bmap1->n_ineq + bmap2->n_ineq);
5059 for (i = 0; i < n_out; ++i) {
5060 int j = isl_basic_map_alloc_equality(bmap);
5061 if (j < 0)
5062 goto error;
5063 isl_seq_clr(bmap->eq[j], 1+total);
5064 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
5065 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
5066 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
5068 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
5069 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5070 bmap = add_divs(bmap, 2 * n_out);
5072 bmap = isl_basic_map_simplify(bmap);
5073 return isl_basic_map_finalize(bmap);
5074 error:
5075 isl_basic_map_free(bmap);
5076 isl_basic_map_free(bmap1);
5077 isl_basic_map_free(bmap2);
5078 return NULL;
5081 /* Given two maps A -> f(A) and B -> g(B), construct a map
5082 * A \cap B -> f(A) + f(B)
5084 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
5085 __isl_take isl_map *map2)
5087 struct isl_map *result;
5088 int i, j;
5090 if (isl_map_check_equal_space(map1, map2) < 0)
5091 goto error;
5093 result = isl_map_alloc_space(isl_space_copy(map1->dim),
5094 map1->n * map2->n, 0);
5095 if (!result)
5096 goto error;
5097 for (i = 0; i < map1->n; ++i)
5098 for (j = 0; j < map2->n; ++j) {
5099 struct isl_basic_map *part;
5100 part = isl_basic_map_sum(
5101 isl_basic_map_copy(map1->p[i]),
5102 isl_basic_map_copy(map2->p[j]));
5103 if (isl_basic_map_is_empty(part))
5104 isl_basic_map_free(part);
5105 else
5106 result = isl_map_add_basic_map(result, part);
5107 if (!result)
5108 goto error;
5110 isl_map_free(map1);
5111 isl_map_free(map2);
5112 return result;
5113 error:
5114 isl_map_free(map1);
5115 isl_map_free(map2);
5116 return NULL;
5119 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
5120 __isl_take isl_set *set2)
5122 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5125 /* Given a basic map A -> f(A), construct A -> -f(A).
5127 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5129 int i, j;
5130 unsigned off;
5131 isl_size n;
5133 bmap = isl_basic_map_cow(bmap);
5134 n = isl_basic_map_dim(bmap, isl_dim_out);
5135 if (n < 0)
5136 return isl_basic_map_free(bmap);
5138 off = isl_basic_map_offset(bmap, isl_dim_out);
5139 for (i = 0; i < bmap->n_eq; ++i)
5140 for (j = 0; j < n; ++j)
5141 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5142 for (i = 0; i < bmap->n_ineq; ++i)
5143 for (j = 0; j < n; ++j)
5144 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5145 for (i = 0; i < bmap->n_div; ++i)
5146 for (j = 0; j < n; ++j)
5147 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5148 bmap = isl_basic_map_gauss(bmap, NULL);
5149 return isl_basic_map_finalize(bmap);
5152 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5154 return isl_basic_map_neg(bset);
5157 /* Given a map A -> f(A), construct A -> -f(A).
5159 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5161 int i;
5163 map = isl_map_cow(map);
5164 if (!map)
5165 return NULL;
5167 for (i = 0; i < map->n; ++i) {
5168 map->p[i] = isl_basic_map_neg(map->p[i]);
5169 if (!map->p[i])
5170 goto error;
5173 return map;
5174 error:
5175 isl_map_free(map);
5176 return NULL;
5179 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5181 return set_from_map(isl_map_neg(set_to_map(set)));
5184 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5185 * A -> floor(f(A)/d).
5187 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5188 isl_int d)
5190 isl_size n_in, n_out, nparam;
5191 unsigned total, pos;
5192 struct isl_basic_map *result = NULL;
5193 struct isl_dim_map *dim_map;
5194 int i;
5196 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5197 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5198 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5199 if (nparam < 0 || n_in < 0 || n_out < 0)
5200 return isl_basic_map_free(bmap);
5202 total = nparam + n_in + n_out + bmap->n_div + n_out;
5203 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5204 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5205 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5206 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5207 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5209 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5210 bmap->n_div + n_out,
5211 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5212 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5213 result = add_divs(result, n_out);
5214 for (i = 0; i < n_out; ++i) {
5215 int j;
5216 j = isl_basic_map_alloc_inequality(result);
5217 if (j < 0)
5218 goto error;
5219 isl_seq_clr(result->ineq[j], 1+total);
5220 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5221 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5222 j = isl_basic_map_alloc_inequality(result);
5223 if (j < 0)
5224 goto error;
5225 isl_seq_clr(result->ineq[j], 1+total);
5226 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5227 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5228 isl_int_sub_ui(result->ineq[j][0], d, 1);
5231 result = isl_basic_map_simplify(result);
5232 return isl_basic_map_finalize(result);
5233 error:
5234 isl_basic_map_free(result);
5235 return NULL;
5238 /* Given a map A -> f(A) and an integer d, construct a map
5239 * A -> floor(f(A)/d).
5241 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5243 int i;
5245 map = isl_map_cow(map);
5246 if (!map)
5247 return NULL;
5249 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5250 for (i = 0; i < map->n; ++i) {
5251 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5252 if (!map->p[i])
5253 goto error;
5255 map = isl_map_unmark_normalized(map);
5257 return map;
5258 error:
5259 isl_map_free(map);
5260 return NULL;
5263 /* Given a map A -> f(A) and an integer d, construct a map
5264 * A -> floor(f(A)/d).
5266 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5267 __isl_take isl_val *d)
5269 if (!map || !d)
5270 goto error;
5271 if (!isl_val_is_int(d))
5272 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5273 "expecting integer denominator", goto error);
5274 map = isl_map_floordiv(map, d->n);
5275 isl_val_free(d);
5276 return map;
5277 error:
5278 isl_map_free(map);
5279 isl_val_free(d);
5280 return NULL;
5283 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5284 unsigned pos)
5286 int i;
5287 isl_size nparam;
5288 isl_size n_in;
5289 isl_size total;
5291 total = isl_basic_map_dim(bmap, isl_dim_all);
5292 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5293 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5294 if (total < 0 || nparam < 0 || n_in < 0)
5295 return isl_basic_map_free(bmap);
5296 i = isl_basic_map_alloc_equality(bmap);
5297 if (i < 0)
5298 goto error;
5299 isl_seq_clr(bmap->eq[i], 1 + total);
5300 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5301 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5302 return isl_basic_map_finalize(bmap);
5303 error:
5304 isl_basic_map_free(bmap);
5305 return NULL;
5308 /* Add a constraint to "bmap" expressing i_pos < o_pos
5310 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5311 unsigned pos)
5313 int i;
5314 isl_size nparam;
5315 isl_size n_in;
5316 isl_size total;
5318 total = isl_basic_map_dim(bmap, isl_dim_all);
5319 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5320 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5321 if (total < 0 || nparam < 0 || n_in < 0)
5322 return isl_basic_map_free(bmap);
5323 i = isl_basic_map_alloc_inequality(bmap);
5324 if (i < 0)
5325 goto error;
5326 isl_seq_clr(bmap->ineq[i], 1 + total);
5327 isl_int_set_si(bmap->ineq[i][0], -1);
5328 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5329 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5330 return isl_basic_map_finalize(bmap);
5331 error:
5332 isl_basic_map_free(bmap);
5333 return NULL;
5336 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5338 static __isl_give isl_basic_map *var_less_or_equal(
5339 __isl_take isl_basic_map *bmap, unsigned pos)
5341 int i;
5342 isl_size nparam;
5343 isl_size n_in;
5344 isl_size total;
5346 total = isl_basic_map_dim(bmap, isl_dim_all);
5347 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5348 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5349 if (total < 0 || nparam < 0 || n_in < 0)
5350 return isl_basic_map_free(bmap);
5351 i = isl_basic_map_alloc_inequality(bmap);
5352 if (i < 0)
5353 goto error;
5354 isl_seq_clr(bmap->ineq[i], 1 + total);
5355 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5356 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5357 return isl_basic_map_finalize(bmap);
5358 error:
5359 isl_basic_map_free(bmap);
5360 return NULL;
5363 /* Add a constraint to "bmap" expressing i_pos > o_pos
5365 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5366 unsigned pos)
5368 int i;
5369 isl_size nparam;
5370 isl_size n_in;
5371 isl_size total;
5373 total = isl_basic_map_dim(bmap, isl_dim_all);
5374 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5375 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5376 if (total < 0 || nparam < 0 || n_in < 0)
5377 return isl_basic_map_free(bmap);
5378 i = isl_basic_map_alloc_inequality(bmap);
5379 if (i < 0)
5380 goto error;
5381 isl_seq_clr(bmap->ineq[i], 1 + total);
5382 isl_int_set_si(bmap->ineq[i][0], -1);
5383 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5384 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5385 return isl_basic_map_finalize(bmap);
5386 error:
5387 isl_basic_map_free(bmap);
5388 return NULL;
5391 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5393 static __isl_give isl_basic_map *var_more_or_equal(
5394 __isl_take isl_basic_map *bmap, unsigned pos)
5396 int i;
5397 isl_size nparam;
5398 isl_size n_in;
5399 isl_size total;
5401 total = isl_basic_map_dim(bmap, isl_dim_all);
5402 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5403 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5404 if (total < 0 || nparam < 0 || n_in < 0)
5405 return isl_basic_map_free(bmap);
5406 i = isl_basic_map_alloc_inequality(bmap);
5407 if (i < 0)
5408 goto error;
5409 isl_seq_clr(bmap->ineq[i], 1 + total);
5410 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5411 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5412 return isl_basic_map_finalize(bmap);
5413 error:
5414 isl_basic_map_free(bmap);
5415 return NULL;
5418 __isl_give isl_basic_map *isl_basic_map_equal(
5419 __isl_take isl_space *space, unsigned n_equal)
5421 int i;
5422 struct isl_basic_map *bmap;
5423 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5424 if (!bmap)
5425 return NULL;
5426 for (i = 0; i < n_equal && bmap; ++i)
5427 bmap = var_equal(bmap, i);
5428 return isl_basic_map_finalize(bmap);
5431 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5433 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5434 unsigned pos)
5436 int i;
5437 struct isl_basic_map *bmap;
5438 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5439 if (!bmap)
5440 return NULL;
5441 for (i = 0; i < pos && bmap; ++i)
5442 bmap = var_equal(bmap, i);
5443 if (bmap)
5444 bmap = var_less(bmap, pos);
5445 return isl_basic_map_finalize(bmap);
5448 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5450 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5451 __isl_take isl_space *space, unsigned pos)
5453 int i;
5454 isl_basic_map *bmap;
5456 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5457 for (i = 0; i < pos; ++i)
5458 bmap = var_equal(bmap, i);
5459 bmap = var_less_or_equal(bmap, pos);
5460 return isl_basic_map_finalize(bmap);
5463 /* Return a relation on "space" expressing i_pos > o_pos
5465 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5466 unsigned pos)
5468 int i;
5469 struct isl_basic_map *bmap;
5470 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5471 if (!bmap)
5472 return NULL;
5473 for (i = 0; i < pos && bmap; ++i)
5474 bmap = var_equal(bmap, i);
5475 if (bmap)
5476 bmap = var_more(bmap, pos);
5477 return isl_basic_map_finalize(bmap);
5480 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5482 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5483 __isl_take isl_space *space, unsigned pos)
5485 int i;
5486 isl_basic_map *bmap;
5488 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5489 for (i = 0; i < pos; ++i)
5490 bmap = var_equal(bmap, i);
5491 bmap = var_more_or_equal(bmap, pos);
5492 return isl_basic_map_finalize(bmap);
5495 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5496 unsigned n, int equal)
5498 struct isl_map *map;
5499 int i;
5501 if (n == 0 && equal)
5502 return isl_map_universe(space);
5504 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5506 for (i = 0; i + 1 < n; ++i)
5507 map = isl_map_add_basic_map(map,
5508 isl_basic_map_less_at(isl_space_copy(space), i));
5509 if (n > 0) {
5510 if (equal)
5511 map = isl_map_add_basic_map(map,
5512 isl_basic_map_less_or_equal_at(space, n - 1));
5513 else
5514 map = isl_map_add_basic_map(map,
5515 isl_basic_map_less_at(space, n - 1));
5516 } else
5517 isl_space_free(space);
5519 return map;
5522 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5524 if (!space)
5525 return NULL;
5526 return map_lex_lte_first(space, space->n_out, equal);
5529 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5530 unsigned n)
5532 return map_lex_lte_first(space, n, 0);
5535 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5536 unsigned n)
5538 return map_lex_lte_first(space, n, 1);
5541 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5543 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5546 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5548 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5551 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5552 unsigned n, int equal)
5554 struct isl_map *map;
5555 int i;
5557 if (n == 0 && equal)
5558 return isl_map_universe(space);
5560 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5562 for (i = 0; i + 1 < n; ++i)
5563 map = isl_map_add_basic_map(map,
5564 isl_basic_map_more_at(isl_space_copy(space), i));
5565 if (n > 0) {
5566 if (equal)
5567 map = isl_map_add_basic_map(map,
5568 isl_basic_map_more_or_equal_at(space, n - 1));
5569 else
5570 map = isl_map_add_basic_map(map,
5571 isl_basic_map_more_at(space, n - 1));
5572 } else
5573 isl_space_free(space);
5575 return map;
5578 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5580 if (!space)
5581 return NULL;
5582 return map_lex_gte_first(space, space->n_out, equal);
5585 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5586 unsigned n)
5588 return map_lex_gte_first(space, n, 0);
5591 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5592 unsigned n)
5594 return map_lex_gte_first(space, n, 1);
5597 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5599 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5602 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5604 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5607 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5608 __isl_take isl_set *set2)
5610 isl_map *map;
5611 map = isl_map_lex_le(isl_set_get_space(set1));
5612 map = isl_map_intersect_domain(map, set1);
5613 map = isl_map_intersect_range(map, set2);
5614 return map;
5617 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5618 __isl_take isl_set *set2)
5620 isl_map *map;
5621 map = isl_map_lex_lt(isl_set_get_space(set1));
5622 map = isl_map_intersect_domain(map, set1);
5623 map = isl_map_intersect_range(map, set2);
5624 return map;
5627 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5628 __isl_take isl_set *set2)
5630 isl_map *map;
5631 map = isl_map_lex_ge(isl_set_get_space(set1));
5632 map = isl_map_intersect_domain(map, set1);
5633 map = isl_map_intersect_range(map, set2);
5634 return map;
5637 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5638 __isl_take isl_set *set2)
5640 isl_map *map;
5641 map = isl_map_lex_gt(isl_set_get_space(set1));
5642 map = isl_map_intersect_domain(map, set1);
5643 map = isl_map_intersect_range(map, set2);
5644 return map;
5647 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5648 __isl_take isl_map *map2)
5650 isl_map *map;
5651 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5652 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5653 map = isl_map_apply_range(map, isl_map_reverse(map2));
5654 return map;
5657 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5658 __isl_take isl_map *map2)
5660 isl_map *map;
5661 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5662 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5663 map = isl_map_apply_range(map, isl_map_reverse(map2));
5664 return map;
5667 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5668 __isl_take isl_map *map2)
5670 isl_map *map;
5671 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5672 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5673 map = isl_map_apply_range(map, isl_map_reverse(map2));
5674 return map;
5677 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5678 __isl_take isl_map *map2)
5680 isl_map *map;
5681 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5682 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5683 map = isl_map_apply_range(map, isl_map_reverse(map2));
5684 return map;
5687 /* For the div d = floor(f/m) at position "div", add the constraint
5689 * f - m d >= 0
5691 static __isl_give isl_basic_map *add_upper_div_constraint(
5692 __isl_take isl_basic_map *bmap, unsigned div)
5694 int i;
5695 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5696 isl_size n_div;
5697 unsigned pos;
5699 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5700 if (v_div < 0 || n_div < 0)
5701 return isl_basic_map_free(bmap);
5702 pos = v_div + div;
5703 i = isl_basic_map_alloc_inequality(bmap);
5704 if (i < 0)
5705 return isl_basic_map_free(bmap);
5706 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5707 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5709 return bmap;
5712 /* For the div d = floor(f/m) at position "div", add the constraint
5714 * -(f-(m-1)) + m d >= 0
5716 static __isl_give isl_basic_map *add_lower_div_constraint(
5717 __isl_take isl_basic_map *bmap, unsigned div)
5719 int i;
5720 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5721 isl_size n_div;
5722 unsigned pos;
5724 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5725 if (v_div < 0 || n_div < 0)
5726 return isl_basic_map_free(bmap);
5727 pos = v_div + div;
5728 i = isl_basic_map_alloc_inequality(bmap);
5729 if (i < 0)
5730 return isl_basic_map_free(bmap);
5731 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5732 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5733 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5734 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5736 return bmap;
5739 /* For the div d = floor(f/m) at position "pos", add the constraints
5741 * f - m d >= 0
5742 * -(f-(m-1)) + m d >= 0
5744 * Note that the second constraint is the negation of
5746 * f - m d >= m
5748 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5749 __isl_take isl_basic_map *bmap, unsigned pos)
5751 bmap = add_upper_div_constraint(bmap, pos);
5752 bmap = add_lower_div_constraint(bmap, pos);
5753 return bmap;
5756 /* For each known div d = floor(f/m), add the constraints
5758 * f - m d >= 0
5759 * -(f-(m-1)) + m d >= 0
5761 * Remove duplicate constraints in case of some these div constraints
5762 * already appear in "bmap".
5764 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5765 __isl_take isl_basic_map *bmap)
5767 isl_size n_div;
5769 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5770 if (n_div < 0)
5771 return isl_basic_map_free(bmap);
5772 if (n_div == 0)
5773 return bmap;
5775 bmap = add_known_div_constraints(bmap);
5776 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5777 bmap = isl_basic_map_finalize(bmap);
5778 return bmap;
5781 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5783 * In particular, if this div is of the form d = floor(f/m),
5784 * then add the constraint
5786 * f - m d >= 0
5788 * if sign < 0 or the constraint
5790 * -(f-(m-1)) + m d >= 0
5792 * if sign > 0.
5794 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5795 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5797 if (sign < 0)
5798 return add_upper_div_constraint(bmap, div);
5799 else
5800 return add_lower_div_constraint(bmap, div);
5803 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5804 __isl_take isl_basic_map *bmap)
5806 isl_space *space;
5808 if (!bmap)
5809 goto error;
5810 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5811 bmap->n_div == 0 &&
5812 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5813 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5814 return bset_from_bmap(bmap);
5815 bmap = isl_basic_map_cow(bmap);
5816 if (!bmap)
5817 return NULL;
5818 space = isl_basic_map_take_space(bmap);
5819 space = isl_space_underlying(space, bmap->n_div);
5820 bmap = isl_basic_map_restore_space(bmap, space);
5821 if (!bmap)
5822 return NULL;
5823 bmap->extra -= bmap->n_div;
5824 bmap->n_div = 0;
5825 bmap = isl_basic_map_finalize(bmap);
5826 return bset_from_bmap(bmap);
5827 error:
5828 isl_basic_map_free(bmap);
5829 return NULL;
5832 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5833 __isl_take isl_basic_set *bset)
5835 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5838 /* Replace each element in "list" by the result of applying
5839 * isl_basic_map_underlying_set to the element.
5841 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5842 __isl_take isl_basic_map_list *list)
5844 int i;
5845 isl_size n;
5847 n = isl_basic_map_list_n_basic_map(list);
5848 if (n < 0)
5849 goto error;
5851 for (i = 0; i < n; ++i) {
5852 isl_basic_map *bmap;
5853 isl_basic_set *bset;
5855 bmap = isl_basic_map_list_get_basic_map(list, i);
5856 bset = isl_basic_set_underlying_set(bmap);
5857 list = isl_basic_set_list_set_basic_set(list, i, bset);
5860 return list;
5861 error:
5862 isl_basic_map_list_free(list);
5863 return NULL;
5866 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5867 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5869 struct isl_basic_map *bmap;
5870 struct isl_ctx *ctx;
5871 isl_size dim, bmap_total;
5872 unsigned total;
5873 int i;
5875 if (!bset || !like)
5876 goto error;
5877 ctx = bset->ctx;
5878 if (isl_basic_set_check_no_params(bset) < 0 ||
5879 isl_basic_set_check_no_locals(bset) < 0)
5880 goto error;
5881 dim = isl_basic_set_dim(bset, isl_dim_set);
5882 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5883 if (dim < 0 || bmap_total < 0)
5884 goto error;
5885 isl_assert(ctx, dim == bmap_total, goto error);
5886 if (like->n_div == 0) {
5887 isl_space *space = isl_basic_map_get_space(like);
5888 isl_basic_map_free(like);
5889 return isl_basic_map_reset_space(bset, space);
5891 bset = isl_basic_set_cow(bset);
5892 if (!bset)
5893 goto error;
5894 total = dim + bset->extra;
5895 bmap = bset_to_bmap(bset);
5896 isl_space_free(isl_basic_map_take_space(bmap));
5897 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5898 if (!bmap)
5899 goto error;
5900 bmap->n_div = like->n_div;
5901 bmap->extra += like->n_div;
5902 if (bmap->extra) {
5903 unsigned ltotal;
5904 isl_int **div;
5905 ltotal = total - bmap->extra + like->extra;
5906 if (ltotal > total)
5907 ltotal = total;
5908 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5909 bmap->extra * (1 + 1 + total));
5910 if (isl_blk_is_error(bmap->block2))
5911 goto error;
5912 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5913 if (!div)
5914 goto error;
5915 bmap->div = div;
5916 for (i = 0; i < bmap->extra; ++i)
5917 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5918 for (i = 0; i < like->n_div; ++i) {
5919 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5920 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5922 bmap = isl_basic_map_add_known_div_constraints(bmap);
5924 isl_basic_map_free(like);
5925 bmap = isl_basic_map_simplify(bmap);
5926 bmap = isl_basic_map_finalize(bmap);
5927 return bmap;
5928 error:
5929 isl_basic_map_free(like);
5930 isl_basic_set_free(bset);
5931 return NULL;
5934 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5935 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5937 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5938 bset_to_bmap(like)));
5941 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5943 int i;
5945 map = isl_map_cow(map);
5946 if (!map)
5947 return NULL;
5948 map->dim = isl_space_cow(map->dim);
5949 if (!map->dim)
5950 goto error;
5952 for (i = 1; i < map->n; ++i)
5953 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5954 goto error);
5955 for (i = 0; i < map->n; ++i) {
5956 map->p[i] = bset_to_bmap(
5957 isl_basic_map_underlying_set(map->p[i]));
5958 if (!map->p[i])
5959 goto error;
5961 if (map->n == 0)
5962 map->dim = isl_space_underlying(map->dim, 0);
5963 else {
5964 isl_space_free(map->dim);
5965 map->dim = isl_space_copy(map->p[0]->dim);
5967 if (!map->dim)
5968 goto error;
5969 return set_from_map(map);
5970 error:
5971 isl_map_free(map);
5972 return NULL;
5975 /* Replace the space of "bmap" by "space".
5977 * If the space of "bmap" is identical to "space" (including the identifiers
5978 * of the input and output dimensions), then simply return the original input.
5980 __isl_give isl_basic_map *isl_basic_map_reset_space(
5981 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5983 isl_bool equal;
5984 isl_space *bmap_space;
5986 bmap_space = isl_basic_map_peek_space(bmap);
5987 equal = isl_space_is_equal(bmap_space, space);
5988 if (equal >= 0 && equal)
5989 equal = isl_space_has_equal_ids(bmap_space, space);
5990 if (equal < 0)
5991 goto error;
5992 if (equal) {
5993 isl_space_free(space);
5994 return bmap;
5996 isl_space_free(isl_basic_map_take_space(bmap));
5997 bmap = isl_basic_map_restore_space(bmap, space);
5999 bmap = isl_basic_map_finalize(bmap);
6001 return bmap;
6002 error:
6003 isl_basic_map_free(bmap);
6004 isl_space_free(space);
6005 return NULL;
6008 __isl_give isl_basic_set *isl_basic_set_reset_space(
6009 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
6011 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
6012 space));
6015 /* Check that the total dimensions of "map" and "space" are the same.
6017 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
6018 __isl_keep isl_space *space)
6020 isl_size dim1, dim2;
6022 dim1 = isl_map_dim(map, isl_dim_all);
6023 dim2 = isl_space_dim(space, isl_dim_all);
6024 if (dim1 < 0 || dim2 < 0)
6025 return isl_stat_error;
6026 if (dim1 == dim2)
6027 return isl_stat_ok;
6028 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6029 "total dimensions do not match", return isl_stat_error);
6032 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
6033 __isl_take isl_space *space)
6035 int i;
6037 map = isl_map_cow(map);
6038 if (!map || !space)
6039 goto error;
6041 for (i = 0; i < map->n; ++i) {
6042 map->p[i] = isl_basic_map_reset_space(map->p[i],
6043 isl_space_copy(space));
6044 if (!map->p[i])
6045 goto error;
6047 isl_space_free(isl_map_take_space(map));
6048 map = isl_map_restore_space(map, space);
6050 return map;
6051 error:
6052 isl_map_free(map);
6053 isl_space_free(space);
6054 return NULL;
6057 /* Replace the space of "map" by "space", without modifying
6058 * the dimension of "map".
6060 * If the space of "map" is identical to "space" (including the identifiers
6061 * of the input and output dimensions), then simply return the original input.
6063 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
6064 __isl_take isl_space *space)
6066 isl_bool equal;
6067 isl_space *map_space;
6069 map_space = isl_map_peek_space(map);
6070 equal = isl_space_is_equal(map_space, space);
6071 if (equal >= 0 && equal)
6072 equal = isl_space_has_equal_ids(map_space, space);
6073 if (equal < 0)
6074 goto error;
6075 if (equal) {
6076 isl_space_free(space);
6077 return map;
6079 if (check_map_space_equal_total_dim(map, space) < 0)
6080 goto error;
6081 return isl_map_reset_space(map, space);
6082 error:
6083 isl_map_free(map);
6084 isl_space_free(space);
6085 return NULL;
6088 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
6089 __isl_take isl_space *space)
6091 return set_from_map(isl_map_reset_space(set_to_map(set), space));
6094 /* Compute the parameter domain of the given basic set.
6096 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
6098 isl_bool is_params;
6099 isl_space *space;
6100 isl_size n;
6102 is_params = isl_basic_set_is_params(bset);
6103 if (is_params < 0)
6104 return isl_basic_set_free(bset);
6105 if (is_params)
6106 return bset;
6108 n = isl_basic_set_dim(bset, isl_dim_set);
6109 if (n < 0)
6110 return isl_basic_set_free(bset);
6111 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6112 space = isl_basic_set_get_space(bset);
6113 space = isl_space_params(space);
6114 bset = isl_basic_set_reset_space(bset, space);
6115 return bset;
6118 /* Construct a zero-dimensional basic set with the given parameter domain.
6120 __isl_give isl_basic_set *isl_basic_set_from_params(
6121 __isl_take isl_basic_set *bset)
6123 isl_space *space;
6124 space = isl_basic_set_get_space(bset);
6125 space = isl_space_set_from_params(space);
6126 bset = isl_basic_set_reset_space(bset, space);
6127 return bset;
6130 /* Compute the parameter domain of the given set.
6132 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6134 return isl_map_params(set_to_map(set));
6137 /* Construct a zero-dimensional set with the given parameter domain.
6139 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6141 isl_space *space;
6142 space = isl_set_get_space(set);
6143 space = isl_space_set_from_params(space);
6144 set = isl_set_reset_space(set, space);
6145 return set;
6148 /* Compute the parameter domain of the given map.
6150 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6152 isl_space *space;
6153 isl_size n_in, n_out;
6155 n_in = isl_map_dim(map, isl_dim_in);
6156 n_out = isl_map_dim(map, isl_dim_out);
6157 if (n_in < 0 || n_out < 0)
6158 return isl_map_free(map);
6159 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6160 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6161 space = isl_map_get_space(map);
6162 space = isl_space_params(space);
6163 map = isl_map_reset_space(map, space);
6164 return map;
6167 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6169 isl_space *space;
6170 isl_size n_out;
6172 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6173 if (n_out < 0)
6174 return isl_basic_map_free(bmap);
6175 space = isl_space_domain(isl_basic_map_get_space(bmap));
6177 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6179 return isl_basic_map_reset_space(bmap, space);
6182 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6184 if (!bmap)
6185 return isl_bool_error;
6186 return isl_space_may_be_set(bmap->dim);
6189 /* Is this basic map actually a set?
6190 * Users should never call this function. Outside of isl,
6191 * the type should indicate whether something is a set or a map.
6193 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6195 if (!bmap)
6196 return isl_bool_error;
6197 return isl_space_is_set(bmap->dim);
6200 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6202 isl_bool is_set;
6204 is_set = isl_basic_map_is_set(bmap);
6205 if (is_set < 0)
6206 goto error;
6207 if (is_set)
6208 return bmap;
6209 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6210 error:
6211 isl_basic_map_free(bmap);
6212 return NULL;
6215 __isl_give isl_basic_map *isl_basic_map_domain_map(
6216 __isl_take isl_basic_map *bmap)
6218 int i;
6219 isl_space *space;
6220 isl_basic_map *domain;
6221 isl_size nparam, n_in, n_out;
6223 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6224 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6225 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6226 if (nparam < 0 || n_in < 0 || n_out < 0)
6227 return isl_basic_map_free(bmap);
6229 space = isl_basic_map_get_space(bmap);
6230 space = isl_space_from_range(isl_space_domain(space));
6231 domain = isl_basic_map_universe(space);
6233 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6234 bmap = isl_basic_map_apply_range(bmap, domain);
6235 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6237 for (i = 0; i < n_in; ++i)
6238 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6239 isl_dim_out, i);
6241 bmap = isl_basic_map_gauss(bmap, NULL);
6242 return isl_basic_map_finalize(bmap);
6245 __isl_give isl_basic_map *isl_basic_map_range_map(
6246 __isl_take isl_basic_map *bmap)
6248 int i;
6249 isl_space *space;
6250 isl_basic_map *range;
6251 isl_size nparam, n_in, n_out;
6253 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6254 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6255 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6256 if (nparam < 0 || n_in < 0 || n_out < 0)
6257 return isl_basic_map_free(bmap);
6259 space = isl_basic_map_get_space(bmap);
6260 space = isl_space_from_range(isl_space_range(space));
6261 range = isl_basic_map_universe(space);
6263 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6264 bmap = isl_basic_map_apply_range(bmap, range);
6265 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6267 for (i = 0; i < n_out; ++i)
6268 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6269 isl_dim_out, i);
6271 bmap = isl_basic_map_gauss(bmap, NULL);
6272 return isl_basic_map_finalize(bmap);
6275 int isl_map_may_be_set(__isl_keep isl_map *map)
6277 if (!map)
6278 return -1;
6279 return isl_space_may_be_set(map->dim);
6282 /* Is this map actually a set?
6283 * Users should never call this function. Outside of isl,
6284 * the type should indicate whether something is a set or a map.
6286 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6288 if (!map)
6289 return isl_bool_error;
6290 return isl_space_is_set(map->dim);
6293 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6295 isl_space *space;
6296 isl_size n_in;
6298 n_in = isl_map_dim(map, isl_dim_in);
6299 if (n_in < 0)
6300 return set_from_map(isl_map_free(map));
6301 space = isl_space_range(isl_map_get_space(map));
6303 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6305 return set_from_map(isl_map_reset_space(map, space));
6308 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6309 * to each of its basic maps.
6311 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6312 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6313 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6315 int i;
6316 isl_space *space;
6318 map = isl_map_cow(map);
6319 if (!map)
6320 return NULL;
6322 for (i = 0; i < map->n; ++i) {
6323 map->p[i] = fn_bmap(map->p[i]);
6324 if (!map->p[i])
6325 return isl_map_free(map);
6327 map = isl_map_unmark_normalized(map);
6329 space = isl_map_take_space(map);
6330 space = fn_space(space);
6331 map = isl_map_restore_space(map, space);
6333 return map;
6336 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6338 return isl_map_transform(map, &isl_space_domain_map,
6339 &isl_basic_map_domain_map);
6342 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6344 return isl_map_transform(map, &isl_space_range_map,
6345 &isl_basic_map_range_map);
6348 /* Given a wrapped map of the form A[B -> C],
6349 * return the map A[B -> C] -> B.
6351 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6353 isl_id *id;
6354 isl_map *map;
6356 if (!set)
6357 return NULL;
6358 if (!isl_set_has_tuple_id(set))
6359 return isl_map_domain_map(isl_set_unwrap(set));
6361 id = isl_set_get_tuple_id(set);
6362 map = isl_map_domain_map(isl_set_unwrap(set));
6363 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6365 return map;
6368 __isl_give isl_basic_map *isl_basic_map_from_domain(
6369 __isl_take isl_basic_set *bset)
6371 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6374 __isl_give isl_basic_map *isl_basic_map_from_range(
6375 __isl_take isl_basic_set *bset)
6377 isl_space *space;
6378 space = isl_basic_set_get_space(bset);
6379 space = isl_space_from_range(space);
6380 bset = isl_basic_set_reset_space(bset, space);
6381 return bset_to_bmap(bset);
6384 /* Create a relation with the given set as range.
6385 * The domain of the created relation is a zero-dimensional
6386 * flat anonymous space.
6388 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6390 isl_space *space;
6391 space = isl_set_get_space(set);
6392 space = isl_space_from_range(space);
6393 set = isl_set_reset_space(set, space);
6394 return set_to_map(set);
6397 /* Create a relation with the given set as domain.
6398 * The range of the created relation is a zero-dimensional
6399 * flat anonymous space.
6401 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6403 return isl_map_reverse(isl_map_from_range(set));
6406 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6407 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6409 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6412 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6413 __isl_take isl_set *range)
6415 return isl_map_apply_range(isl_map_reverse(domain), range);
6418 /* Return a newly allocated isl_map with given space and flags and
6419 * room for "n" basic maps.
6420 * Make sure that all cached information is cleared.
6422 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6423 unsigned flags)
6425 struct isl_map *map;
6427 if (!space)
6428 return NULL;
6429 if (n < 0)
6430 isl_die(space->ctx, isl_error_internal,
6431 "negative number of basic maps", goto error);
6432 map = isl_calloc(space->ctx, struct isl_map,
6433 sizeof(struct isl_map) +
6434 (n - 1) * sizeof(struct isl_basic_map *));
6435 if (!map)
6436 goto error;
6438 map->ctx = space->ctx;
6439 isl_ctx_ref(map->ctx);
6440 map->ref = 1;
6441 map->size = n;
6442 map->n = 0;
6443 map->dim = space;
6444 map->flags = flags;
6445 return map;
6446 error:
6447 isl_space_free(space);
6448 return NULL;
6451 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6453 struct isl_basic_map *bmap;
6454 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6455 bmap = isl_basic_map_set_to_empty(bmap);
6456 return bmap;
6459 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6461 struct isl_basic_set *bset;
6462 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6463 bset = isl_basic_set_set_to_empty(bset);
6464 return bset;
6467 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6469 struct isl_basic_map *bmap;
6470 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6471 bmap = isl_basic_map_finalize(bmap);
6472 return bmap;
6475 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6477 struct isl_basic_set *bset;
6478 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6479 bset = isl_basic_set_finalize(bset);
6480 return bset;
6483 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6484 __isl_take isl_space *space)
6486 int i;
6487 isl_size total = isl_space_dim(space, isl_dim_all);
6488 isl_basic_map *bmap;
6490 if (total < 0)
6491 space = isl_space_free(space);
6492 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6493 for (i = 0; i < total; ++i) {
6494 int k = isl_basic_map_alloc_inequality(bmap);
6495 if (k < 0)
6496 goto error;
6497 isl_seq_clr(bmap->ineq[k], 1 + total);
6498 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6500 return bmap;
6501 error:
6502 isl_basic_map_free(bmap);
6503 return NULL;
6506 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6507 __isl_take isl_space *space)
6509 return isl_basic_map_nat_universe(space);
6512 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6514 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6517 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6519 return isl_map_nat_universe(space);
6522 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6524 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6527 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6529 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6532 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6534 struct isl_map *map;
6535 if (!space)
6536 return NULL;
6537 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6538 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6539 return map;
6542 /* This function performs the same operation as isl_map_universe,
6543 * but is considered as a function on an isl_space when exported.
6545 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6547 return isl_map_universe(space);
6550 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6552 struct isl_set *set;
6553 if (!space)
6554 return NULL;
6555 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6556 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6557 return set;
6560 /* This function performs the same operation as isl_set_universe,
6561 * but is considered as a function on an isl_space when exported.
6563 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6565 return isl_set_universe(space);
6568 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6570 int i;
6571 struct isl_map *dup;
6573 if (!map)
6574 return NULL;
6575 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6576 for (i = 0; i < map->n; ++i)
6577 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6578 return dup;
6581 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6582 __isl_take isl_basic_map *bmap)
6584 if (!bmap || !map)
6585 goto error;
6586 if (isl_basic_map_plain_is_empty(bmap)) {
6587 isl_basic_map_free(bmap);
6588 return map;
6590 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6591 goto error;
6592 isl_assert(map->ctx, map->n < map->size, goto error);
6593 map->p[map->n] = bmap;
6594 map->n++;
6595 map = isl_map_unmark_normalized(map);
6596 return map;
6597 error:
6598 if (map)
6599 isl_map_free(map);
6600 if (bmap)
6601 isl_basic_map_free(bmap);
6602 return NULL;
6605 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6607 int i;
6609 if (!map)
6610 return NULL;
6612 if (--map->ref > 0)
6613 return NULL;
6615 clear_caches(map);
6616 isl_ctx_deref(map->ctx);
6617 for (i = 0; i < map->n; ++i)
6618 isl_basic_map_free(map->p[i]);
6619 isl_space_free(map->dim);
6620 free(map);
6622 return NULL;
6625 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6626 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6628 int j;
6629 isl_size total;
6631 total = isl_basic_map_dim(bmap, isl_dim_all);
6632 if (total < 0)
6633 return isl_basic_map_free(bmap);
6635 bmap = isl_basic_map_cow(bmap);
6636 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6637 j = isl_basic_map_alloc_equality(bmap);
6638 if (j < 0)
6639 goto error;
6640 isl_seq_clr(bmap->eq[j] + 1, total);
6641 isl_int_set_si(bmap->eq[j][pos], -1);
6642 isl_int_set_si(bmap->eq[j][0], value);
6643 bmap = isl_basic_map_simplify(bmap);
6644 return isl_basic_map_finalize(bmap);
6645 error:
6646 isl_basic_map_free(bmap);
6647 return NULL;
6650 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6651 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6653 int j;
6654 isl_size total;
6656 total = isl_basic_map_dim(bmap, isl_dim_all);
6657 if (total < 0)
6658 return isl_basic_map_free(bmap);
6660 bmap = isl_basic_map_cow(bmap);
6661 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6662 j = isl_basic_map_alloc_equality(bmap);
6663 if (j < 0)
6664 goto error;
6665 isl_seq_clr(bmap->eq[j] + 1, total);
6666 isl_int_set_si(bmap->eq[j][pos], -1);
6667 isl_int_set(bmap->eq[j][0], value);
6668 bmap = isl_basic_map_simplify(bmap);
6669 return isl_basic_map_finalize(bmap);
6670 error:
6671 isl_basic_map_free(bmap);
6672 return NULL;
6675 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6676 enum isl_dim_type type, unsigned pos, int value)
6678 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6679 return isl_basic_map_free(bmap);
6680 return isl_basic_map_fix_pos_si(bmap,
6681 isl_basic_map_offset(bmap, type) + pos, value);
6684 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6685 enum isl_dim_type type, unsigned pos, isl_int value)
6687 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6688 return isl_basic_map_free(bmap);
6689 return isl_basic_map_fix_pos(bmap,
6690 isl_basic_map_offset(bmap, type) + pos, value);
6693 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6694 * to be equal to "v".
6696 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6697 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6699 if (!bmap || !v)
6700 goto error;
6701 if (!isl_val_is_int(v))
6702 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6703 "expecting integer value", goto error);
6704 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6705 goto error;
6706 pos += isl_basic_map_offset(bmap, type);
6707 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6708 isl_val_free(v);
6709 return bmap;
6710 error:
6711 isl_basic_map_free(bmap);
6712 isl_val_free(v);
6713 return NULL;
6716 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6717 * to be equal to "v".
6719 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6720 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6722 return isl_basic_map_fix_val(bset, type, pos, v);
6725 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6726 enum isl_dim_type type, unsigned pos, int value)
6728 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6729 type, pos, value));
6732 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6733 enum isl_dim_type type, unsigned pos, isl_int value)
6735 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6736 type, pos, value));
6739 /* Remove the basic map at position "i" from "map" if this basic map
6740 * is (obviously) empty.
6742 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6744 isl_bool empty;
6746 if (!map)
6747 return NULL;
6749 empty = isl_basic_map_plain_is_empty(map->p[i]);
6750 if (empty < 0)
6751 return isl_map_free(map);
6752 if (!empty)
6753 return map;
6755 isl_basic_map_free(map->p[i]);
6756 map->n--;
6757 if (i != map->n) {
6758 map->p[i] = map->p[map->n];
6759 map = isl_map_unmark_normalized(map);
6763 return map;
6766 /* Perform "fn" on each basic map of "map", where we may not be holding
6767 * the only reference to "map".
6768 * In particular, "fn" should be a semantics preserving operation
6769 * that we want to apply to all copies of "map". We therefore need
6770 * to be careful not to modify "map" in a way that breaks "map"
6771 * in case anything goes wrong.
6773 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6774 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6776 struct isl_basic_map *bmap;
6777 int i;
6779 if (!map)
6780 return NULL;
6782 for (i = map->n - 1; i >= 0; --i) {
6783 bmap = isl_basic_map_copy(map->p[i]);
6784 bmap = fn(bmap);
6785 if (!bmap)
6786 goto error;
6787 isl_basic_map_free(map->p[i]);
6788 map->p[i] = bmap;
6789 map = remove_if_empty(map, i);
6790 if (!map)
6791 return NULL;
6794 return map;
6795 error:
6796 isl_map_free(map);
6797 return NULL;
6800 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6801 enum isl_dim_type type, unsigned pos, int value)
6803 int i;
6805 map = isl_map_cow(map);
6806 if (isl_map_check_range(map, type, pos, 1) < 0)
6807 return isl_map_free(map);
6808 for (i = map->n - 1; i >= 0; --i) {
6809 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6810 map = remove_if_empty(map, i);
6811 if (!map)
6812 return NULL;
6814 map = isl_map_unmark_normalized(map);
6815 return map;
6818 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6819 enum isl_dim_type type, unsigned pos, int value)
6821 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6824 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6825 enum isl_dim_type type, unsigned pos, isl_int value)
6827 int i;
6829 map = isl_map_cow(map);
6830 if (isl_map_check_range(map, type, pos, 1) < 0)
6831 return isl_map_free(map);
6832 for (i = 0; i < map->n; ++i) {
6833 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6834 if (!map->p[i])
6835 goto error;
6837 map = isl_map_unmark_normalized(map);
6838 return map;
6839 error:
6840 isl_map_free(map);
6841 return NULL;
6844 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6845 enum isl_dim_type type, unsigned pos, isl_int value)
6847 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6850 /* Fix the value of the variable at position "pos" of type "type" of "map"
6851 * to be equal to "v".
6853 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6854 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6856 int i;
6858 map = isl_map_cow(map);
6859 if (!map || !v)
6860 goto error;
6862 if (!isl_val_is_int(v))
6863 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6864 "expecting integer value", goto error);
6865 if (isl_map_check_range(map, type, pos, 1) < 0)
6866 goto error;
6867 for (i = map->n - 1; i >= 0; --i) {
6868 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6869 isl_val_copy(v));
6870 map = remove_if_empty(map, i);
6871 if (!map)
6872 goto error;
6874 map = isl_map_unmark_normalized(map);
6875 isl_val_free(v);
6876 return map;
6877 error:
6878 isl_map_free(map);
6879 isl_val_free(v);
6880 return NULL;
6883 /* Fix the value of the variable at position "pos" of type "type" of "set"
6884 * to be equal to "v".
6886 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6887 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6889 return isl_map_fix_val(set, type, pos, v);
6892 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6893 unsigned input, int value)
6895 return isl_map_fix_si(map, isl_dim_in, input, value);
6898 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6899 int value)
6901 return set_from_map(isl_map_fix_si(set_to_map(set),
6902 isl_dim_set, dim, value));
6905 static __isl_give isl_basic_map *basic_map_bound_si(
6906 __isl_take isl_basic_map *bmap,
6907 enum isl_dim_type type, unsigned pos, int value, int upper)
6909 int j;
6910 isl_size total;
6912 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6913 return isl_basic_map_free(bmap);
6914 total = isl_basic_map_dim(bmap, isl_dim_all);
6915 if (total < 0)
6916 return isl_basic_map_free(bmap);
6917 pos += isl_basic_map_offset(bmap, type);
6918 bmap = isl_basic_map_cow(bmap);
6919 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6920 j = isl_basic_map_alloc_inequality(bmap);
6921 if (j < 0)
6922 goto error;
6923 isl_seq_clr(bmap->ineq[j], 1 + total);
6924 if (upper) {
6925 isl_int_set_si(bmap->ineq[j][pos], -1);
6926 isl_int_set_si(bmap->ineq[j][0], value);
6927 } else {
6928 isl_int_set_si(bmap->ineq[j][pos], 1);
6929 isl_int_set_si(bmap->ineq[j][0], -value);
6931 bmap = isl_basic_map_simplify(bmap);
6932 return isl_basic_map_finalize(bmap);
6933 error:
6934 isl_basic_map_free(bmap);
6935 return NULL;
6938 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6939 __isl_take isl_basic_map *bmap,
6940 enum isl_dim_type type, unsigned pos, int value)
6942 return basic_map_bound_si(bmap, type, pos, value, 0);
6945 /* Constrain the values of the given dimension to be no greater than "value".
6947 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6948 __isl_take isl_basic_map *bmap,
6949 enum isl_dim_type type, unsigned pos, int value)
6951 return basic_map_bound_si(bmap, type, pos, value, 1);
6954 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6955 enum isl_dim_type type, unsigned pos, int value, int upper)
6957 int i;
6959 map = isl_map_cow(map);
6960 if (isl_map_check_range(map, type, pos, 1) < 0)
6961 return isl_map_free(map);
6962 for (i = 0; i < map->n; ++i) {
6963 map->p[i] = basic_map_bound_si(map->p[i],
6964 type, pos, value, upper);
6965 if (!map->p[i])
6966 goto error;
6968 map = isl_map_unmark_normalized(map);
6969 return map;
6970 error:
6971 isl_map_free(map);
6972 return NULL;
6975 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6976 enum isl_dim_type type, unsigned pos, int value)
6978 return map_bound_si(map, type, pos, value, 0);
6981 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6982 enum isl_dim_type type, unsigned pos, int value)
6984 return map_bound_si(map, type, pos, value, 1);
6987 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6988 enum isl_dim_type type, unsigned pos, int value)
6990 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6991 type, pos, value));
6994 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6995 enum isl_dim_type type, unsigned pos, int value)
6997 return isl_map_upper_bound_si(set, type, pos, value);
7000 /* Bound the given variable of "bmap" from below (or above is "upper"
7001 * is set) to "value".
7003 static __isl_give isl_basic_map *basic_map_bound(
7004 __isl_take isl_basic_map *bmap,
7005 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7007 int j;
7008 isl_size total;
7010 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7011 return isl_basic_map_free(bmap);
7012 total = isl_basic_map_dim(bmap, isl_dim_all);
7013 if (total < 0)
7014 return isl_basic_map_free(bmap);
7015 pos += isl_basic_map_offset(bmap, type);
7016 bmap = isl_basic_map_cow(bmap);
7017 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
7018 j = isl_basic_map_alloc_inequality(bmap);
7019 if (j < 0)
7020 goto error;
7021 isl_seq_clr(bmap->ineq[j], 1 + total);
7022 if (upper) {
7023 isl_int_set_si(bmap->ineq[j][pos], -1);
7024 isl_int_set(bmap->ineq[j][0], value);
7025 } else {
7026 isl_int_set_si(bmap->ineq[j][pos], 1);
7027 isl_int_neg(bmap->ineq[j][0], value);
7029 bmap = isl_basic_map_simplify(bmap);
7030 return isl_basic_map_finalize(bmap);
7031 error:
7032 isl_basic_map_free(bmap);
7033 return NULL;
7036 /* Bound the given variable of "map" from below (or above is "upper"
7037 * is set) to "value".
7039 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
7040 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7042 int i;
7044 map = isl_map_cow(map);
7045 if (isl_map_check_range(map, type, pos, 1) < 0)
7046 return isl_map_free(map);
7047 for (i = map->n - 1; i >= 0; --i) {
7048 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
7049 map = remove_if_empty(map, i);
7050 if (!map)
7051 return NULL;
7053 map = isl_map_unmark_normalized(map);
7054 return map;
7057 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
7058 enum isl_dim_type type, unsigned pos, isl_int value)
7060 return map_bound(map, type, pos, value, 0);
7063 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
7064 enum isl_dim_type type, unsigned pos, isl_int value)
7066 return map_bound(map, type, pos, value, 1);
7069 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
7070 enum isl_dim_type type, unsigned pos, isl_int value)
7072 return isl_map_lower_bound(set, type, pos, value);
7075 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
7076 enum isl_dim_type type, unsigned pos, isl_int value)
7078 return isl_map_upper_bound(set, type, pos, value);
7081 /* Force the values of the variable at position "pos" of type "type" of "map"
7082 * to be no smaller than "value".
7084 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
7085 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7087 if (!value)
7088 goto error;
7089 if (!isl_val_is_int(value))
7090 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7091 "expecting integer value", goto error);
7092 map = isl_map_lower_bound(map, type, pos, value->n);
7093 isl_val_free(value);
7094 return map;
7095 error:
7096 isl_val_free(value);
7097 isl_map_free(map);
7098 return NULL;
7101 /* Force the values of the variable at position "pos" of type "type" of "set"
7102 * to be no smaller than "value".
7104 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
7105 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7107 isl_map *map;
7109 map = set_to_map(set);
7110 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
7113 /* Force the values of the variable at position "pos" of type "type" of "map"
7114 * to be no greater than "value".
7116 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
7117 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7119 if (!value)
7120 goto error;
7121 if (!isl_val_is_int(value))
7122 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7123 "expecting integer value", goto error);
7124 map = isl_map_upper_bound(map, type, pos, value->n);
7125 isl_val_free(value);
7126 return map;
7127 error:
7128 isl_val_free(value);
7129 isl_map_free(map);
7130 return NULL;
7133 /* Force the values of the variable at position "pos" of type "type" of "set"
7134 * to be no greater than "value".
7136 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7137 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7139 isl_map *map;
7141 map = set_to_map(set);
7142 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7145 /* If "mv" has an explicit domain, then intersect the domain of "map"
7146 * with this explicit domain.
7148 * An isl_multi_val object never has an explicit domain,
7149 * so simply return "map".
7151 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7152 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7154 return map;
7157 #undef BASE
7158 #define BASE val
7159 #include "isl_map_bound_templ.c"
7161 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7162 * for each set dimension, by treating the set as a map.
7164 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7165 __isl_take isl_multi_val *bound,
7166 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7167 unsigned pos, __isl_take isl_val *value))
7169 isl_map *map;
7171 map = set_to_map(set);
7172 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7175 #undef BASE
7176 #define BASE pw_aff
7177 #include "isl_map_bound_templ.c"
7179 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7180 * for each set dimension, by converting the set and the bound
7181 * to objects living in a map space.
7183 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7184 __isl_take isl_multi_pw_aff *bound,
7185 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7186 unsigned pos, __isl_take TYPE *value))
7188 isl_map *map;
7190 map = isl_map_from_range(set);
7191 bound = isl_multi_pw_aff_from_range(bound);
7192 map = map_bound_multi_pw_aff(map, bound, set_bound);
7193 return isl_map_range(map);
7196 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7197 * setting a bound on the given output dimension.
7199 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7200 unsigned pos, __isl_take isl_val *v)
7202 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7205 /* Force the values of the set dimensions of "set"
7206 * to be no smaller than the corresponding values in "lower".
7208 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7209 __isl_take isl_multi_val *lower)
7211 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7214 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7215 * setting a bound on the given output dimension.
7217 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7218 unsigned pos, __isl_take isl_val *v)
7220 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7223 /* Force the values of the set dimensions of "set"
7224 * to be no greater than the corresponding values in "upper".
7226 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7227 __isl_take isl_multi_val *upper)
7229 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7232 /* Force the symbolic constant expression "bound"
7233 * to satisfy the relation "order" with respect to
7234 * the output variable at position "pos" of "map".
7236 * Create an affine expression representing the output variable
7237 * in terms of the range and
7238 * compare it using "order" to "bound" (defined on the domain).
7239 * The result is a relation between elements in domain and range that
7240 * can be intersected with "map".
7242 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7243 unsigned pos, __isl_take isl_pw_aff *bound,
7244 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7245 __isl_take isl_pw_aff *pa2))
7247 isl_space *space;
7248 isl_local_space *ls;
7249 isl_pw_aff *var;
7251 space = isl_space_range(isl_map_get_space(map));
7252 ls = isl_local_space_from_space(space);
7253 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7254 map = isl_map_intersect(map, order(bound, var));
7255 return map;
7258 /* Force the values of the output variable at position "pos" of "map"
7259 * to be no smaller than the symbolic constant expression "lower".
7261 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7262 unsigned pos, __isl_take isl_pw_aff *lower)
7264 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7267 /* Force the values of the output variable at position "pos" of "map"
7268 * to be no greater than the symbolic constant expression "upper".
7270 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7271 unsigned pos, __isl_take isl_pw_aff *upper)
7273 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7276 /* Force the values of the set dimensions of "set"
7277 * to be no smaller than the corresponding constant symbolic expressions
7278 * in "lower".
7280 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7281 __isl_take isl_multi_pw_aff *lower)
7283 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7286 /* Force the values of the set dimensions of "set"
7287 * to be no greater than the corresponding constant symbolic expressions
7288 * in "upper".
7290 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7291 __isl_take isl_multi_pw_aff *upper)
7293 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7296 /* Force the values of the output dimensions of "map"
7297 * to be no smaller than the corresponding constant symbolic expressions
7298 * in "lower".
7300 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7301 __isl_take isl_multi_pw_aff *lower)
7303 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7306 /* Force the values of the output dimensions of "map"
7307 * to be no greater than the corresponding constant symbolic expressions
7308 * in "upper".
7310 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7311 __isl_take isl_multi_pw_aff *upper)
7313 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7316 /* Bound the given variable of "bset" from below (or above is "upper"
7317 * is set) to "value".
7319 static __isl_give isl_basic_set *isl_basic_set_bound(
7320 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7321 isl_int value, int upper)
7323 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7324 type, pos, value, upper));
7327 /* Bound the given variable of "bset" from below (or above is "upper"
7328 * is set) to "value".
7330 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7331 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7332 __isl_take isl_val *value, int upper)
7334 if (!value)
7335 goto error;
7336 if (!isl_val_is_int(value))
7337 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7338 "expecting integer value", goto error);
7339 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7340 isl_val_free(value);
7341 return bset;
7342 error:
7343 isl_val_free(value);
7344 isl_basic_set_free(bset);
7345 return NULL;
7348 /* Bound the given variable of "bset" from below to "value".
7350 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7351 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7352 __isl_take isl_val *value)
7354 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7357 /* Bound the given variable of "bset" from above to "value".
7359 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7360 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7361 __isl_take isl_val *value)
7363 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7366 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7368 return isl_map_transform(map, &isl_space_reverse,
7369 &isl_basic_map_reverse);
7372 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7374 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7376 return isl_map_transform(map, &isl_space_domain_reverse,
7377 &isl_basic_map_domain_reverse);
7380 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7382 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7384 return isl_map_transform(map, &isl_space_range_reverse,
7385 &isl_basic_map_range_reverse);
7388 /* Given a set (A -> B), return the corresponding set (B -> A).
7390 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7392 isl_map *map = set_to_map(set);
7394 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7395 &isl_basic_map_set_reverse);
7396 return set_from_map(map);
7399 #undef TYPE
7400 #define TYPE isl_pw_multi_aff
7401 #undef SUFFIX
7402 #define SUFFIX _pw_multi_aff
7403 #undef EMPTY
7404 #define EMPTY isl_pw_multi_aff_empty
7405 #undef ADD
7406 #define ADD isl_pw_multi_aff_union_add
7407 #include "isl_map_lexopt_templ.c"
7409 /* Given a map "map", compute the lexicographically minimal
7410 * (or maximal) image element for each domain element in dom,
7411 * in the form of an isl_pw_multi_aff.
7412 * If "empty" is not NULL, then set *empty to those elements in dom that
7413 * do not have an image element.
7414 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7415 * should be computed over the domain of "map". "empty" is also NULL
7416 * in this case.
7418 * We first compute the lexicographically minimal or maximal element
7419 * in the first basic map. This results in a partial solution "res"
7420 * and a subset "todo" of dom that still need to be handled.
7421 * We then consider each of the remaining maps in "map" and successively
7422 * update both "res" and "todo".
7423 * If "empty" is NULL, then the todo sets are not needed and therefore
7424 * also not computed.
7426 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7427 __isl_take isl_map *map, __isl_take isl_set *dom,
7428 __isl_give isl_set **empty, unsigned flags)
7430 int i;
7431 int full;
7432 isl_pw_multi_aff *res;
7433 isl_set *todo;
7435 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7436 if (!map || (!full && !dom))
7437 goto error;
7439 if (isl_map_plain_is_empty(map)) {
7440 if (empty)
7441 *empty = dom;
7442 else
7443 isl_set_free(dom);
7444 return isl_pw_multi_aff_from_map(map);
7447 res = basic_map_partial_lexopt_pw_multi_aff(
7448 isl_basic_map_copy(map->p[0]),
7449 isl_set_copy(dom), empty, flags);
7451 if (empty)
7452 todo = *empty;
7453 for (i = 1; i < map->n; ++i) {
7454 isl_pw_multi_aff *res_i;
7456 res_i = basic_map_partial_lexopt_pw_multi_aff(
7457 isl_basic_map_copy(map->p[i]),
7458 isl_set_copy(dom), empty, flags);
7460 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7461 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7462 else
7463 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7465 if (empty)
7466 todo = isl_set_intersect(todo, *empty);
7469 isl_set_free(dom);
7470 isl_map_free(map);
7472 if (empty)
7473 *empty = todo;
7475 return res;
7476 error:
7477 if (empty)
7478 *empty = NULL;
7479 isl_set_free(dom);
7480 isl_map_free(map);
7481 return NULL;
7484 #undef TYPE
7485 #define TYPE isl_map
7486 #undef SUFFIX
7487 #define SUFFIX
7488 #undef EMPTY
7489 #define EMPTY isl_map_empty
7490 #undef ADD
7491 #define ADD isl_map_union_disjoint
7492 #include "isl_map_lexopt_templ.c"
7494 /* Given a map "map", compute the lexicographically minimal
7495 * (or maximal) image element for each domain element in "dom",
7496 * in the form of an isl_map.
7497 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7498 * do not have an image element.
7499 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7500 * should be computed over the domain of "map". "empty" is also NULL
7501 * in this case.
7503 * If the input consists of more than one disjunct, then first
7504 * compute the desired result in the form of an isl_pw_multi_aff and
7505 * then convert that into an isl_map.
7507 * This function used to have an explicit implementation in terms
7508 * of isl_maps, but it would continually intersect the domains of
7509 * partial results with the complement of the domain of the next
7510 * partial solution, potentially leading to an explosion in the number
7511 * of disjuncts if there are several disjuncts in the input.
7512 * An even earlier implementation of this function would look for
7513 * better results in the domain of the partial result and for extra
7514 * results in the complement of this domain, which would lead to
7515 * even more splintering.
7517 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7518 __isl_take isl_map *map, __isl_take isl_set *dom,
7519 __isl_give isl_set **empty, unsigned flags)
7521 int full;
7522 struct isl_map *res;
7523 isl_pw_multi_aff *pma;
7525 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7526 if (!map || (!full && !dom))
7527 goto error;
7529 if (isl_map_plain_is_empty(map)) {
7530 if (empty)
7531 *empty = dom;
7532 else
7533 isl_set_free(dom);
7534 return map;
7537 if (map->n == 1) {
7538 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7539 dom, empty, flags);
7540 isl_map_free(map);
7541 return res;
7544 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7545 flags);
7546 return isl_map_from_pw_multi_aff_internal(pma);
7547 error:
7548 if (empty)
7549 *empty = NULL;
7550 isl_set_free(dom);
7551 isl_map_free(map);
7552 return NULL;
7555 __isl_give isl_map *isl_map_partial_lexmax(
7556 __isl_take isl_map *map, __isl_take isl_set *dom,
7557 __isl_give isl_set **empty)
7559 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7562 __isl_give isl_map *isl_map_partial_lexmin(
7563 __isl_take isl_map *map, __isl_take isl_set *dom,
7564 __isl_give isl_set **empty)
7566 return isl_map_partial_lexopt(map, dom, empty, 0);
7569 __isl_give isl_set *isl_set_partial_lexmin(
7570 __isl_take isl_set *set, __isl_take isl_set *dom,
7571 __isl_give isl_set **empty)
7573 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7574 dom, empty));
7577 __isl_give isl_set *isl_set_partial_lexmax(
7578 __isl_take isl_set *set, __isl_take isl_set *dom,
7579 __isl_give isl_set **empty)
7581 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7582 dom, empty));
7585 /* Compute the lexicographic minimum (or maximum if "flags" includes
7586 * ISL_OPT_MAX) of "bset" over its parametric domain.
7588 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7589 unsigned flags)
7591 return isl_basic_map_lexopt(bset, flags);
7594 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7596 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7599 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7601 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7604 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7606 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7609 /* Compute the lexicographic minimum of "bset" over its parametric domain
7610 * for the purpose of quantifier elimination.
7611 * That is, find an explicit representation for all the existentially
7612 * quantified variables in "bset" by computing their lexicographic
7613 * minimum.
7615 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7616 __isl_take isl_basic_set *bset)
7618 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7621 /* Given a basic map with one output dimension, compute the minimum or
7622 * maximum of that dimension as an isl_pw_aff.
7624 * Compute the optimum as a lexicographic optimum over the single
7625 * output dimension and extract the single isl_pw_aff from the result.
7627 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7628 int max)
7630 isl_pw_multi_aff *pma;
7631 isl_pw_aff *pwaff;
7633 bmap = isl_basic_map_copy(bmap);
7634 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7635 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7636 isl_pw_multi_aff_free(pma);
7638 return pwaff;
7641 /* Compute the minimum or maximum of the given output dimension
7642 * as a function of the parameters and the input dimensions,
7643 * but independently of the other output dimensions.
7645 * We first project out the other output dimension and then compute
7646 * the "lexicographic" maximum in each basic map, combining the results
7647 * using isl_pw_aff_union_max.
7649 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7650 int max)
7652 int i;
7653 isl_pw_aff *pwaff;
7654 isl_size n_out;
7656 n_out = isl_map_dim(map, isl_dim_out);
7657 if (n_out < 0)
7658 map = isl_map_free(map);
7659 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7660 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7661 if (!map)
7662 return NULL;
7664 if (map->n == 0) {
7665 isl_space *space = isl_map_get_space(map);
7666 isl_map_free(map);
7667 return isl_pw_aff_empty(space);
7670 pwaff = basic_map_dim_opt(map->p[0], max);
7671 for (i = 1; i < map->n; ++i) {
7672 isl_pw_aff *pwaff_i;
7674 pwaff_i = basic_map_dim_opt(map->p[i], max);
7675 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7678 isl_map_free(map);
7680 return pwaff;
7683 /* Compute the minimum of the given output dimension as a function of the
7684 * parameters and input dimensions, but independently of
7685 * the other output dimensions.
7687 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7689 return map_dim_opt(map, pos, 0);
7692 /* Compute the maximum of the given output dimension as a function of the
7693 * parameters and input dimensions, but independently of
7694 * the other output dimensions.
7696 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7698 return map_dim_opt(map, pos, 1);
7701 /* Compute the minimum or maximum of the given set dimension
7702 * as a function of the parameters,
7703 * but independently of the other set dimensions.
7705 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7706 int max)
7708 return map_dim_opt(set, pos, max);
7711 /* Compute the maximum of the given set dimension as a function of the
7712 * parameters, but independently of the other set dimensions.
7714 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7716 return set_dim_opt(set, pos, 1);
7719 /* Compute the minimum of the given set dimension as a function of the
7720 * parameters, but independently of the other set dimensions.
7722 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7724 return set_dim_opt(set, pos, 0);
7727 /* Apply a preimage specified by "mat" on the parameters of "bset".
7728 * bset is assumed to have only parameters and divs.
7730 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7731 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7733 isl_size nparam;
7735 nparam = isl_basic_set_dim(bset, isl_dim_param);
7736 if (nparam < 0 || !mat)
7737 goto error;
7739 bset->dim = isl_space_cow(bset->dim);
7740 if (!bset->dim)
7741 goto error;
7743 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7745 bset->dim->nparam = 0;
7746 bset->dim->n_out = nparam;
7747 bset = isl_basic_set_preimage(bset, mat);
7748 if (bset) {
7749 bset->dim->nparam = bset->dim->n_out;
7750 bset->dim->n_out = 0;
7752 return bset;
7753 error:
7754 isl_mat_free(mat);
7755 isl_basic_set_free(bset);
7756 return NULL;
7759 /* Apply a preimage specified by "mat" on the parameters of "set".
7760 * set is assumed to have only parameters and divs.
7762 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7763 __isl_take isl_mat *mat)
7765 isl_space *space;
7766 isl_size nparam;
7768 nparam = isl_set_dim(set, isl_dim_param);
7769 if (nparam < 0 || !mat)
7770 goto error;
7772 if (mat->n_row != 1 + nparam)
7773 isl_die(isl_set_get_ctx(set), isl_error_internal,
7774 "unexpected number of rows", goto error);
7776 space = isl_set_get_space(set);
7777 space = isl_space_move_dims(space, isl_dim_set, 0,
7778 isl_dim_param, 0, nparam);
7779 set = isl_set_reset_space(set, space);
7780 set = isl_set_preimage(set, mat);
7781 nparam = isl_set_dim(set, isl_dim_out);
7782 if (nparam < 0)
7783 set = isl_set_free(set);
7784 space = isl_set_get_space(set);
7785 space = isl_space_move_dims(space, isl_dim_param, 0,
7786 isl_dim_out, 0, nparam);
7787 set = isl_set_reset_space(set, space);
7788 return set;
7789 error:
7790 isl_mat_free(mat);
7791 isl_set_free(set);
7792 return NULL;
7795 /* Intersect the basic set "bset" with the affine space specified by the
7796 * equalities in "eq".
7798 static __isl_give isl_basic_set *basic_set_append_equalities(
7799 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7801 int i, k;
7802 unsigned len;
7804 if (!bset || !eq)
7805 goto error;
7807 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7808 if (!bset)
7809 goto error;
7811 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7812 for (i = 0; i < eq->n_row; ++i) {
7813 k = isl_basic_set_alloc_equality(bset);
7814 if (k < 0)
7815 goto error;
7816 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7817 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7819 isl_mat_free(eq);
7821 bset = isl_basic_set_gauss(bset, NULL);
7822 bset = isl_basic_set_finalize(bset);
7824 return bset;
7825 error:
7826 isl_mat_free(eq);
7827 isl_basic_set_free(bset);
7828 return NULL;
7831 /* Intersect the set "set" with the affine space specified by the
7832 * equalities in "eq".
7834 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7835 __isl_take isl_mat *eq)
7837 int i;
7839 if (!set || !eq)
7840 goto error;
7842 for (i = 0; i < set->n; ++i) {
7843 set->p[i] = basic_set_append_equalities(set->p[i],
7844 isl_mat_copy(eq));
7845 if (!set->p[i])
7846 goto error;
7848 isl_mat_free(eq);
7849 return set;
7850 error:
7851 isl_mat_free(eq);
7852 isl_set_free(set);
7853 return NULL;
7856 /* Given a basic set "bset" that only involves parameters and existentially
7857 * quantified variables, return the index of the first equality
7858 * that only involves parameters. If there is no such equality then
7859 * return bset->n_eq.
7861 * This function assumes that isl_basic_set_gauss has been called on "bset".
7863 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7865 int i, j;
7866 isl_size nparam, n_div;
7868 nparam = isl_basic_set_dim(bset, isl_dim_param);
7869 n_div = isl_basic_set_dim(bset, isl_dim_div);
7870 if (nparam < 0 || n_div < 0)
7871 return -1;
7873 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7874 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7875 ++i;
7878 return i;
7881 /* Compute an explicit representation for the existentially quantified
7882 * variables in "bset" by computing the "minimal value" of the set
7883 * variables. Since there are no set variables, the computation of
7884 * the minimal value essentially computes an explicit representation
7885 * of the non-empty part(s) of "bset".
7887 * The input only involves parameters and existentially quantified variables.
7888 * All equalities among parameters have been removed.
7890 * Since the existentially quantified variables in the result are in general
7891 * going to be different from those in the input, we first replace
7892 * them by the minimal number of variables based on their equalities.
7893 * This should simplify the parametric integer programming.
7895 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7897 isl_morph *morph1, *morph2;
7898 isl_set *set;
7899 isl_size n;
7901 if (!bset)
7902 return NULL;
7903 if (bset->n_eq == 0)
7904 return isl_basic_set_lexmin_compute_divs(bset);
7906 morph1 = isl_basic_set_parameter_compression(bset);
7907 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7908 bset = isl_basic_set_lift(bset);
7909 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7910 bset = isl_morph_basic_set(morph2, bset);
7911 n = isl_basic_set_dim(bset, isl_dim_set);
7912 if (n < 0)
7913 bset = isl_basic_set_free(bset);
7914 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7916 set = isl_basic_set_lexmin_compute_divs(bset);
7918 set = isl_morph_set(isl_morph_inverse(morph1), set);
7920 return set;
7923 /* Project the given basic set onto its parameter domain, possibly introducing
7924 * new, explicit, existential variables in the constraints.
7925 * The input has parameters and (possibly implicit) existential variables.
7926 * The output has the same parameters, but only
7927 * explicit existentially quantified variables.
7929 * The actual projection is performed by pip, but pip doesn't seem
7930 * to like equalities very much, so we first remove the equalities
7931 * among the parameters by performing a variable compression on
7932 * the parameters. Afterward, an inverse transformation is performed
7933 * and the equalities among the parameters are inserted back in.
7935 * The variable compression on the parameters may uncover additional
7936 * equalities that were only implicit before. We therefore check
7937 * if there are any new parameter equalities in the result and
7938 * if so recurse. The removal of parameter equalities is required
7939 * for the parameter compression performed by base_compute_divs.
7941 static __isl_give isl_set *parameter_compute_divs(
7942 __isl_take isl_basic_set *bset)
7944 int i;
7945 struct isl_mat *eq;
7946 struct isl_mat *T, *T2;
7947 struct isl_set *set;
7948 isl_size nparam;
7950 bset = isl_basic_set_cow(bset);
7951 if (!bset)
7952 return NULL;
7954 if (bset->n_eq == 0)
7955 return base_compute_divs(bset);
7957 bset = isl_basic_set_gauss(bset, NULL);
7958 if (!bset)
7959 return NULL;
7960 if (isl_basic_set_plain_is_empty(bset))
7961 return isl_set_from_basic_set(bset);
7963 i = first_parameter_equality(bset);
7964 if (i == bset->n_eq)
7965 return base_compute_divs(bset);
7967 nparam = isl_basic_set_dim(bset, isl_dim_param);
7968 if (nparam < 0)
7969 return isl_set_from_basic_set(isl_basic_set_free(bset));
7970 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7971 0, 1 + nparam);
7972 eq = isl_mat_cow(eq);
7973 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7974 if (T && T->n_col == 0) {
7975 isl_mat_free(T);
7976 isl_mat_free(T2);
7977 isl_mat_free(eq);
7978 bset = isl_basic_set_set_to_empty(bset);
7979 return isl_set_from_basic_set(bset);
7981 bset = basic_set_parameter_preimage(bset, T);
7983 i = first_parameter_equality(bset);
7984 if (!bset)
7985 set = NULL;
7986 else if (i == bset->n_eq)
7987 set = base_compute_divs(bset);
7988 else
7989 set = parameter_compute_divs(bset);
7990 set = set_parameter_preimage(set, T2);
7991 set = set_append_equalities(set, eq);
7992 return set;
7995 /* Insert the divs from "ls" before those of "bmap".
7997 * The number of columns is not changed, which means that the last
7998 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7999 * The caller is responsible for removing the same number of dimensions
8000 * from the space of "bmap".
8002 static __isl_give isl_basic_map *insert_divs_from_local_space(
8003 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
8005 int i;
8006 isl_size n_div;
8007 int old_n_div;
8009 n_div = isl_local_space_dim(ls, isl_dim_div);
8010 if (n_div < 0)
8011 return isl_basic_map_free(bmap);
8012 if (n_div == 0)
8013 return bmap;
8015 old_n_div = bmap->n_div;
8016 bmap = insert_div_rows(bmap, n_div);
8017 if (!bmap)
8018 return NULL;
8020 for (i = 0; i < n_div; ++i) {
8021 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
8022 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
8025 return bmap;
8028 /* Replace the space of "bmap" by the space and divs of "ls".
8030 * If "ls" has any divs, then we simplify the result since we may
8031 * have discovered some additional equalities that could simplify
8032 * the div expressions.
8034 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
8035 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
8037 isl_size n_div;
8039 bmap = isl_basic_map_cow(bmap);
8040 n_div = isl_local_space_dim(ls, isl_dim_div);
8041 if (!bmap || n_div < 0)
8042 goto error;
8044 bmap = insert_divs_from_local_space(bmap, ls);
8045 if (!bmap)
8046 goto error;
8048 isl_space_free(bmap->dim);
8049 bmap->dim = isl_local_space_get_space(ls);
8050 if (!bmap->dim)
8051 goto error;
8053 isl_local_space_free(ls);
8054 if (n_div > 0)
8055 bmap = isl_basic_map_simplify(bmap);
8056 bmap = isl_basic_map_finalize(bmap);
8057 return bmap;
8058 error:
8059 isl_basic_map_free(bmap);
8060 isl_local_space_free(ls);
8061 return NULL;
8064 /* Replace the space of "map" by the space and divs of "ls".
8066 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
8067 __isl_take isl_local_space *ls)
8069 int i;
8071 map = isl_map_cow(map);
8072 if (!map || !ls)
8073 goto error;
8075 for (i = 0; i < map->n; ++i) {
8076 map->p[i] = basic_replace_space_by_local_space(map->p[i],
8077 isl_local_space_copy(ls));
8078 if (!map->p[i])
8079 goto error;
8081 isl_space_free(isl_map_take_space(map));
8082 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
8084 isl_local_space_free(ls);
8085 return map;
8086 error:
8087 isl_local_space_free(ls);
8088 isl_map_free(map);
8089 return NULL;
8092 /* Compute an explicit representation for the existentially
8093 * quantified variables for which do not know any explicit representation yet.
8095 * We first sort the existentially quantified variables so that the
8096 * existentially quantified variables for which we already have an explicit
8097 * representation are placed before those for which we do not.
8098 * The input dimensions, the output dimensions and the existentially
8099 * quantified variables for which we already have an explicit
8100 * representation are then turned into parameters.
8101 * compute_divs returns a map with the same parameters and
8102 * no input or output dimensions and the dimension specification
8103 * is reset to that of the input, including the existentially quantified
8104 * variables for which we already had an explicit representation.
8106 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
8108 struct isl_basic_set *bset;
8109 struct isl_set *set;
8110 struct isl_map *map;
8111 isl_space *space;
8112 isl_local_space *ls;
8113 isl_size nparam;
8114 isl_size n_in;
8115 isl_size n_out;
8116 int n_known;
8117 int i;
8119 bmap = isl_basic_map_sort_divs(bmap);
8120 bmap = isl_basic_map_cow(bmap);
8121 if (!bmap)
8122 return NULL;
8124 n_known = isl_basic_map_first_unknown_div(bmap);
8125 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8126 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8127 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8128 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8129 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8131 space = isl_space_set_alloc(bmap->ctx,
8132 nparam + n_in + n_out + n_known, 0);
8133 if (!space)
8134 goto error;
8136 ls = isl_basic_map_get_local_space(bmap);
8137 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8138 n_known, bmap->n_div - n_known);
8139 if (n_known > 0) {
8140 for (i = n_known; i < bmap->n_div; ++i)
8141 swap_div(bmap, i - n_known, i);
8142 bmap->n_div -= n_known;
8143 bmap->extra -= n_known;
8145 bmap = isl_basic_map_reset_space(bmap, space);
8146 bset = bset_from_bmap(bmap);
8148 set = parameter_compute_divs(bset);
8149 map = set_to_map(set);
8150 map = replace_space_by_local_space(map, ls);
8152 return map;
8153 error:
8154 isl_basic_map_free(bmap);
8155 return NULL;
8158 /* Is the integer division at position "div" of "bmap" integral?
8159 * That is, does it have denominator 1?
8161 isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
8163 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8164 return isl_bool_error;
8165 return isl_int_is_one(bmap->div[div][0]);
8168 /* Remove the explicit representation of local variable "div",
8169 * if there is any.
8171 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8172 __isl_take isl_basic_map *bmap, int div)
8174 isl_bool unknown;
8176 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8177 if (unknown < 0)
8178 return isl_basic_map_free(bmap);
8179 if (unknown)
8180 return bmap;
8182 bmap = isl_basic_map_cow(bmap);
8183 if (!bmap)
8184 return NULL;
8185 isl_int_set_si(bmap->div[div][0], 0);
8186 return bmap;
8189 /* Is local variable "div" of "bmap" marked as not having an explicit
8190 * representation?
8191 * Note that even if "div" is not marked in this way and therefore
8192 * has an explicit representation, this representation may still
8193 * depend (indirectly) on other local variables that do not
8194 * have an explicit representation.
8196 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8197 int div)
8199 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8200 return isl_bool_error;
8201 return isl_int_is_zero(bmap->div[div][0]);
8204 /* Return the position of the first local variable that does not
8205 * have an explicit representation.
8206 * Return the total number of local variables if they all have
8207 * an explicit representation.
8208 * Return -1 on error.
8210 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8212 int i;
8214 if (!bmap)
8215 return -1;
8217 for (i = 0; i < bmap->n_div; ++i) {
8218 if (!isl_basic_map_div_is_known(bmap, i))
8219 return i;
8221 return bmap->n_div;
8224 /* Return the position of the first local variable that does not
8225 * have an explicit representation.
8226 * Return the total number of local variables if they all have
8227 * an explicit representation.
8228 * Return -1 on error.
8230 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8232 return isl_basic_map_first_unknown_div(bset);
8235 /* Does "bmap" have an explicit representation for all local variables?
8237 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8239 int first;
8240 isl_size n;
8242 n = isl_basic_map_dim(bmap, isl_dim_div);
8243 first = isl_basic_map_first_unknown_div(bmap);
8244 if (n < 0 || first < 0)
8245 return isl_bool_error;
8246 return first == n;
8249 /* Do all basic maps in "map" have an explicit representation
8250 * for all local variables?
8252 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8254 int i;
8256 if (!map)
8257 return isl_bool_error;
8259 for (i = 0; i < map->n; ++i) {
8260 int known = isl_basic_map_divs_known(map->p[i]);
8261 if (known <= 0)
8262 return known;
8265 return isl_bool_true;
8268 /* If bmap contains any unknown divs, then compute explicit
8269 * expressions for them. However, this computation may be
8270 * quite expensive, so first try to remove divs that aren't
8271 * strictly needed.
8273 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8275 int known;
8276 struct isl_map *map;
8278 known = isl_basic_map_divs_known(bmap);
8279 if (known < 0)
8280 goto error;
8281 if (known)
8282 return isl_map_from_basic_map(bmap);
8284 bmap = isl_basic_map_drop_redundant_divs(bmap);
8286 known = isl_basic_map_divs_known(bmap);
8287 if (known < 0)
8288 goto error;
8289 if (known)
8290 return isl_map_from_basic_map(bmap);
8292 map = compute_divs(bmap);
8293 return map;
8294 error:
8295 isl_basic_map_free(bmap);
8296 return NULL;
8299 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8301 int i;
8302 int known;
8303 struct isl_map *res;
8305 if (!map)
8306 return NULL;
8307 if (map->n == 0)
8308 return map;
8310 known = isl_map_divs_known(map);
8311 if (known < 0) {
8312 isl_map_free(map);
8313 return NULL;
8315 if (known)
8316 return map;
8318 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8319 for (i = 1 ; i < map->n; ++i) {
8320 struct isl_map *r2;
8321 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8322 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8323 res = isl_map_union_disjoint(res, r2);
8324 else
8325 res = isl_map_union(res, r2);
8327 isl_map_free(map);
8329 return res;
8332 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8334 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8337 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8339 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8342 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8344 isl_space *space;
8345 isl_size n_out;
8347 n_out = isl_map_dim(map, isl_dim_out);
8348 if (n_out < 0)
8349 return set_from_map(isl_map_free(map));
8350 space = isl_space_domain(isl_map_get_space(map));
8352 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8354 return set_from_map(isl_map_reset_space(map, space));
8357 /* Return the union of "map1" and "map2", where we assume for now that
8358 * "map1" and "map2" are disjoint. Note that the basic maps inside
8359 * "map1" or "map2" may not be disjoint from each other.
8360 * Also note that this function is also called from isl_map_union,
8361 * which takes care of handling the situation where "map1" and "map2"
8362 * may not be disjoint.
8364 * If one of the inputs is empty, we can simply return the other input.
8365 * Similarly, if one of the inputs is universal, then it is equal to the union.
8367 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8368 __isl_take isl_map *map2)
8370 int i;
8371 unsigned flags = 0;
8372 struct isl_map *map = NULL;
8373 int is_universe;
8375 if (isl_map_check_equal_space(map1, map2) < 0)
8376 goto error;
8378 if (map1->n == 0) {
8379 isl_map_free(map1);
8380 return map2;
8382 if (map2->n == 0) {
8383 isl_map_free(map2);
8384 return map1;
8387 is_universe = isl_map_plain_is_universe(map1);
8388 if (is_universe < 0)
8389 goto error;
8390 if (is_universe) {
8391 isl_map_free(map2);
8392 return map1;
8395 is_universe = isl_map_plain_is_universe(map2);
8396 if (is_universe < 0)
8397 goto error;
8398 if (is_universe) {
8399 isl_map_free(map1);
8400 return map2;
8403 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8404 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8405 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8407 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8408 map1->n + map2->n, flags);
8409 if (!map)
8410 goto error;
8411 for (i = 0; i < map1->n; ++i) {
8412 map = isl_map_add_basic_map(map,
8413 isl_basic_map_copy(map1->p[i]));
8414 if (!map)
8415 goto error;
8417 for (i = 0; i < map2->n; ++i) {
8418 map = isl_map_add_basic_map(map,
8419 isl_basic_map_copy(map2->p[i]));
8420 if (!map)
8421 goto error;
8423 isl_map_free(map1);
8424 isl_map_free(map2);
8425 return map;
8426 error:
8427 isl_map_free(map);
8428 isl_map_free(map1);
8429 isl_map_free(map2);
8430 return NULL;
8433 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8434 * guaranteed to be disjoint by the caller.
8436 * Note that this functions is called from within isl_map_make_disjoint,
8437 * so we have to be careful not to touch the constraints of the inputs
8438 * in any way.
8440 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8441 __isl_take isl_map *map2)
8443 isl_map_align_params_bin(&map1, &map2);
8444 return map_union_disjoint(map1, map2);
8447 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8448 * not be disjoint.
8450 * We currently simply call map_union_disjoint, the internal operation
8451 * of which does not really depend on the inputs being disjoint.
8452 * If the result contains more than one basic map, then we clear
8453 * the disjoint flag since the result may contain basic maps from
8454 * both inputs and these are not guaranteed to be disjoint.
8456 * As a special case, if "map1" and "map2" are obviously equal,
8457 * then we simply return "map1".
8459 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8460 __isl_take isl_map *map2)
8462 int equal;
8464 if (isl_map_align_params_bin(&map1, &map2) < 0)
8465 goto error;
8467 equal = isl_map_plain_is_equal(map1, map2);
8468 if (equal < 0)
8469 goto error;
8470 if (equal) {
8471 isl_map_free(map2);
8472 return map1;
8475 map1 = map_union_disjoint(map1, map2);
8476 if (!map1)
8477 return NULL;
8478 if (map1->n > 1)
8479 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8480 return map1;
8481 error:
8482 isl_map_free(map1);
8483 isl_map_free(map2);
8484 return NULL;
8487 __isl_give isl_set *isl_set_union_disjoint(
8488 __isl_take isl_set *set1, __isl_take isl_set *set2)
8490 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8491 set_to_map(set2)));
8494 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8495 __isl_take isl_set *set2)
8497 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8500 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8501 * the results in a map living in "space".
8503 * "map" and "set" are assumed to be compatible and non-NULL.
8505 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8506 __isl_take isl_space *space, __isl_take isl_set *set,
8507 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8508 __isl_take isl_basic_set *bset))
8510 unsigned flags = 0;
8511 struct isl_map *result;
8512 int i, j;
8514 if (isl_set_plain_is_universe(set)) {
8515 isl_set_free(set);
8516 return isl_map_reset_equal_dim_space(map, space);
8519 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8520 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8521 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8523 result = isl_map_alloc_space(space, map->n * set->n, flags);
8524 for (i = 0; result && i < map->n; ++i)
8525 for (j = 0; j < set->n; ++j) {
8526 result = isl_map_add_basic_map(result,
8527 fn(isl_basic_map_copy(map->p[i]),
8528 isl_basic_set_copy(set->p[j])));
8529 if (!result)
8530 break;
8533 isl_map_free(map);
8534 isl_set_free(set);
8535 return result;
8538 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8539 __isl_take isl_set *set)
8541 isl_bool ok;
8542 isl_space *space;
8544 isl_map_align_params_set(&map, &set);
8545 ok = isl_map_compatible_range(map, set);
8546 if (ok < 0)
8547 goto error;
8548 if (!ok)
8549 isl_die(set->ctx, isl_error_invalid,
8550 "incompatible spaces", goto error);
8552 space = isl_map_get_space(map);
8553 return map_intersect_set(map, space, set,
8554 &isl_basic_map_intersect_range);
8555 error:
8556 isl_map_free(map);
8557 isl_set_free(set);
8558 return NULL;
8561 /* Intersect the domain of "map" with "set".
8563 * If the domain dimensions of "map" do not have any identifiers,
8564 * then copy them over from "set".
8566 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8567 __isl_take isl_set *set)
8569 isl_bool ok;
8570 isl_space *space;
8572 isl_map_align_params_set(&map, &set);
8573 ok = isl_map_compatible_domain(map, set);
8574 if (ok < 0)
8575 goto error;
8576 if (!ok)
8577 isl_die(set->ctx, isl_error_invalid,
8578 "incompatible spaces", goto error);
8580 space = isl_map_get_space(map);
8581 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8582 isl_set_peek_space(set), isl_dim_set);
8583 return map_intersect_set(map, space, set,
8584 &isl_basic_map_intersect_domain);
8585 error:
8586 isl_map_free(map);
8587 isl_set_free(set);
8588 return NULL;
8591 #undef BASE
8592 #define BASE map
8593 static
8594 #include "isl_copy_tuple_id_templ.c"
8596 /* Data structure that specifies how isl_map_intersect_factor
8597 * should operate.
8599 * "preserve_type" is the tuple where the factor differs from
8600 * the input map and of which the identifiers needs
8601 * to be preserved explicitly.
8602 * "other_factor" is used to extract the space of the other factor
8603 * from the space of the product ("map").
8604 * "product" is used to combine the given factor and a universe map
8605 * in the space returned by "other_factor" to produce a map
8606 * that lives in the same space as the input map.
8608 struct isl_intersect_factor_control {
8609 enum isl_dim_type preserve_type;
8610 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8611 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8612 __isl_take isl_map *other);
8615 /* Given a map "map" in some product space and a map "factor"
8616 * living in some factor space, return the intersection.
8618 * After aligning the parameters,
8619 * the map "factor" is first extended to a map living in the same space
8620 * as "map" and then a regular intersection is computed.
8622 * Note that the extension is computed as a product, which is anonymous
8623 * by default. If "map" has an identifier on the corresponding tuple,
8624 * then this identifier needs to be set on the product
8625 * before the intersection is computed.
8627 static __isl_give isl_map *isl_map_intersect_factor(
8628 __isl_take isl_map *map, __isl_take isl_map *factor,
8629 struct isl_intersect_factor_control *control)
8631 isl_bool equal;
8632 isl_space *space;
8633 isl_map *other, *product;
8635 equal = isl_map_has_equal_params(map, factor);
8636 if (equal < 0)
8637 goto error;
8638 if (!equal) {
8639 map = isl_map_align_params(map, isl_map_get_space(factor));
8640 factor = isl_map_align_params(factor, isl_map_get_space(map));
8643 space = isl_map_get_space(map);
8644 other = isl_map_universe(control->other_factor(space));
8645 product = control->product(factor, other);
8647 space = isl_map_peek_space(map);
8648 product = isl_map_copy_tuple_id(product, control->preserve_type,
8649 space, control->preserve_type);
8650 return map_intersect(map, product);
8651 error:
8652 isl_map_free(map);
8653 isl_map_free(factor);
8654 return NULL;
8657 /* Return the domain product of "map2" and "map1".
8659 static __isl_give isl_map *isl_map_reverse_domain_product(
8660 __isl_take isl_map *map1, __isl_take isl_map *map2)
8662 return isl_map_domain_product(map2, map1);
8665 /* Return the range product of "map2" and "map1".
8667 static __isl_give isl_map *isl_map_reverse_range_product(
8668 __isl_take isl_map *map1, __isl_take isl_map *map2)
8670 return isl_map_range_product(map2, map1);
8673 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8674 * in the space A -> C, return the intersection.
8676 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8677 __isl_take isl_map *map, __isl_take isl_map *factor)
8679 struct isl_intersect_factor_control control = {
8680 .preserve_type = isl_dim_in,
8681 .other_factor = isl_space_domain_factor_range,
8682 .product = isl_map_domain_product,
8685 return isl_map_intersect_factor(map, factor, &control);
8688 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8689 * in the space B -> C, return the intersection.
8691 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8692 __isl_take isl_map *map, __isl_take isl_map *factor)
8694 struct isl_intersect_factor_control control = {
8695 .preserve_type = isl_dim_in,
8696 .other_factor = isl_space_domain_factor_domain,
8697 .product = isl_map_reverse_domain_product,
8700 return isl_map_intersect_factor(map, factor, &control);
8703 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8704 * in the space A -> B, return the intersection.
8706 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8707 __isl_take isl_map *map, __isl_take isl_map *factor)
8709 struct isl_intersect_factor_control control = {
8710 .preserve_type = isl_dim_out,
8711 .other_factor = isl_space_range_factor_range,
8712 .product = isl_map_range_product,
8715 return isl_map_intersect_factor(map, factor, &control);
8718 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8719 * in the space A -> C, return the intersection.
8721 __isl_give isl_map *isl_map_intersect_range_factor_range(
8722 __isl_take isl_map *map, __isl_take isl_map *factor)
8724 struct isl_intersect_factor_control control = {
8725 .preserve_type = isl_dim_out,
8726 .other_factor = isl_space_range_factor_domain,
8727 .product = isl_map_reverse_range_product,
8730 return isl_map_intersect_factor(map, factor, &control);
8733 /* Given a set "set" in a space [A -> B] and a set "domain"
8734 * in the space A, return the intersection.
8736 * The set "domain" is first extended to a set living in the space
8737 * [A -> B] and then a regular intersection is computed.
8739 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8740 __isl_take isl_set *domain)
8742 struct isl_intersect_factor_control control = {
8743 .preserve_type = isl_dim_set,
8744 .other_factor = isl_space_factor_range,
8745 .product = isl_map_range_product,
8748 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8749 set_to_map(domain), &control));
8752 /* Given a set "set" in a space [A -> B] and a set "range"
8753 * in the space B, return the intersection.
8755 * The set "range" is first extended to a set living in the space
8756 * [A -> B] and then a regular intersection is computed.
8758 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8759 __isl_take isl_set *range)
8761 struct isl_intersect_factor_control control = {
8762 .preserve_type = isl_dim_set,
8763 .other_factor = isl_space_factor_domain,
8764 .product = isl_map_reverse_range_product,
8767 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8768 set_to_map(range), &control));
8771 #undef BASE
8772 #define BASE set
8773 static
8774 #include "isl_copy_tuple_id_templ.c"
8776 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8777 * in the space A, return the intersection.
8779 * The set "domain" is extended to a set living in the space [A -> B] and
8780 * the domain of "map" is intersected with this set.
8782 * If "map" has an identifier on the domain tuple,
8783 * then this identifier needs to be set on this product
8784 * before the intersection is computed.
8786 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8787 __isl_take isl_map *map, __isl_take isl_set *domain)
8789 isl_space *space;
8790 isl_set *factor;
8792 isl_map_align_params_set(&map, &domain);
8793 space = isl_map_get_space(map);
8794 space = isl_space_domain_wrapped_range(space);
8795 factor = isl_set_universe(space);
8796 domain = isl_set_product(domain, factor);
8797 space = isl_map_peek_space(map);
8798 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
8799 return isl_map_intersect_domain(map, domain);
8802 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8803 * in the space B, return the intersection.
8805 * The set "domain" is extended to a set living in the space [B -> C] and
8806 * the range of "map" is intersected with this set.
8808 * If "map" has an identifier on the range tuple,
8809 * then this identifier needs to be set on this product
8810 * before the intersection is computed.
8812 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8813 __isl_take isl_map *map, __isl_take isl_set *domain)
8815 isl_space *space;
8816 isl_set *factor;
8818 isl_map_align_params_set(&map, &domain);
8819 space = isl_map_get_space(map);
8820 space = isl_space_range_wrapped_range(space);
8821 factor = isl_set_universe(space);
8822 domain = isl_set_product(domain, factor);
8823 space = isl_map_peek_space(map);
8824 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
8825 return isl_map_intersect_range(map, domain);
8828 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8829 __isl_take isl_map *map2)
8831 if (isl_map_align_params_bin(&map1, &map2) < 0)
8832 goto error;
8833 map1 = isl_map_reverse(map1);
8834 map1 = isl_map_apply_range(map1, map2);
8835 return isl_map_reverse(map1);
8836 error:
8837 isl_map_free(map1);
8838 isl_map_free(map2);
8839 return NULL;
8842 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8843 __isl_take isl_map *map2)
8845 isl_space *space;
8846 struct isl_map *result;
8847 int i, j;
8849 if (isl_map_align_params_bin(&map1, &map2) < 0)
8850 goto error;
8852 space = isl_space_join(isl_space_copy(map1->dim),
8853 isl_space_copy(map2->dim));
8855 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8856 if (!result)
8857 goto error;
8858 for (i = 0; i < map1->n; ++i)
8859 for (j = 0; j < map2->n; ++j) {
8860 result = isl_map_add_basic_map(result,
8861 isl_basic_map_apply_range(
8862 isl_basic_map_copy(map1->p[i]),
8863 isl_basic_map_copy(map2->p[j])));
8864 if (!result)
8865 goto error;
8867 isl_map_free(map1);
8868 isl_map_free(map2);
8869 if (result && result->n <= 1)
8870 ISL_F_SET(result, ISL_MAP_DISJOINT);
8871 return result;
8872 error:
8873 isl_map_free(map1);
8874 isl_map_free(map2);
8875 return NULL;
8878 /* Is "bmap" a transformation, i.e.,
8879 * does it relate elements from the same space.
8881 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8883 isl_space *space;
8885 space = isl_basic_map_peek_space(bmap);
8886 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8889 /* Check that "bmap" is a transformation, i.e.,
8890 * that it relates elements from the same space.
8892 static isl_stat isl_basic_map_check_transformation(
8893 __isl_keep isl_basic_map *bmap)
8895 isl_bool equal;
8897 equal = isl_basic_map_is_transformation(bmap);
8898 if (equal < 0)
8899 return isl_stat_error;
8900 if (!equal)
8901 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8902 "domain and range don't match", return isl_stat_error);
8903 return isl_stat_ok;
8907 * returns range - domain
8909 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8911 isl_space *target_space;
8912 struct isl_basic_set *bset;
8913 isl_size dim;
8914 isl_size nparam;
8915 isl_size total;
8916 int i;
8918 if (isl_basic_map_check_transformation(bmap) < 0)
8919 return isl_basic_map_free(bmap);
8920 dim = isl_basic_map_dim(bmap, isl_dim_in);
8921 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8922 if (dim < 0 || nparam < 0)
8923 goto error;
8924 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8925 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8926 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8927 total = isl_basic_map_dim(bmap, isl_dim_all);
8928 if (total < 0)
8929 bmap = isl_basic_map_free(bmap);
8930 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8931 for (i = 0; i < dim; ++i) {
8932 int j = isl_basic_map_alloc_equality(bmap);
8933 if (j < 0) {
8934 bmap = isl_basic_map_free(bmap);
8935 break;
8937 isl_seq_clr(bmap->eq[j], 1 + total);
8938 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8939 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8940 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8942 bset = isl_basic_map_domain(bmap);
8943 bset = isl_basic_set_reset_space(bset, target_space);
8944 return bset;
8945 error:
8946 isl_basic_map_free(bmap);
8947 return NULL;
8950 /* Is the tuple of type "type1" of "map" the same as
8951 * the tuple of type "type2" of "space"?
8953 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8954 enum isl_dim_type type1, __isl_keep isl_space *space,
8955 enum isl_dim_type type2)
8957 isl_space *map_space;
8959 map_space = isl_map_peek_space(map);
8960 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8963 /* Is the tuple of type "type1" of "map1" the same as
8964 * the tuple of type "type2" of "map2"?
8966 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8967 enum isl_dim_type type1, __isl_keep isl_map *map2,
8968 enum isl_dim_type type2)
8970 isl_space *space1, *space2;
8972 space1 = isl_map_peek_space(map1);
8973 space2 = isl_map_peek_space(map2);
8974 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8977 /* Is the space of "obj" equal to "space", ignoring parameters?
8979 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8980 __isl_keep isl_space *space)
8982 isl_space *map_space;
8984 map_space = isl_map_peek_space(map);
8985 return isl_space_has_equal_tuples(map_space, space);
8988 /* Check that "map" is a transformation, i.e.,
8989 * that it relates elements from the same space.
8991 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8993 isl_bool equal;
8995 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8996 if (equal < 0)
8997 return isl_stat_error;
8998 if (!equal)
8999 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9000 "domain and range don't match", return isl_stat_error);
9001 return isl_stat_ok;
9005 * returns range - domain
9007 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
9009 int i;
9010 isl_space *space;
9011 struct isl_set *result;
9013 if (isl_map_check_transformation(map) < 0)
9014 goto error;
9015 space = isl_map_get_space(map);
9016 space = isl_space_domain(space);
9017 result = isl_set_alloc_space(space, map->n, 0);
9018 if (!result)
9019 goto error;
9020 for (i = 0; i < map->n; ++i)
9021 result = isl_set_add_basic_set(result,
9022 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
9023 isl_map_free(map);
9024 return result;
9025 error:
9026 isl_map_free(map);
9027 return NULL;
9031 * returns [domain -> range] -> range - domain
9033 __isl_give isl_basic_map *isl_basic_map_deltas_map(
9034 __isl_take isl_basic_map *bmap)
9036 int i, k;
9037 isl_space *space;
9038 isl_basic_map *domain;
9039 isl_size nparam, n;
9040 isl_size total;
9042 if (isl_basic_map_check_transformation(bmap) < 0)
9043 return isl_basic_map_free(bmap);
9045 nparam = isl_basic_map_dim(bmap, isl_dim_param);
9046 n = isl_basic_map_dim(bmap, isl_dim_in);
9047 if (nparam < 0 || n < 0)
9048 return isl_basic_map_free(bmap);
9050 space = isl_basic_map_get_space(bmap);
9051 space = isl_space_from_range(isl_space_domain(space));
9052 domain = isl_basic_map_universe(space);
9054 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
9055 bmap = isl_basic_map_apply_range(bmap, domain);
9056 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
9058 total = isl_basic_map_dim(bmap, isl_dim_all);
9059 if (total < 0)
9060 return isl_basic_map_free(bmap);
9062 for (i = 0; i < n; ++i) {
9063 k = isl_basic_map_alloc_equality(bmap);
9064 if (k < 0)
9065 goto error;
9066 isl_seq_clr(bmap->eq[k], 1 + total);
9067 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
9068 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
9069 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
9072 bmap = isl_basic_map_gauss(bmap, NULL);
9073 return isl_basic_map_finalize(bmap);
9074 error:
9075 isl_basic_map_free(bmap);
9076 return NULL;
9080 * returns [domain -> range] -> range - domain
9082 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
9084 if (isl_map_check_transformation(map) < 0)
9085 return isl_map_free(map);
9087 return isl_map_transform(map, &isl_space_range_map,
9088 &isl_basic_map_deltas_map);
9091 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9093 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
9095 isl_space *space;
9096 isl_map *map;
9098 space = isl_space_map_from_set(isl_set_get_space(deltas));
9099 map = isl_map_deltas_map(isl_map_universe(space));
9100 map = isl_map_intersect_range(map, deltas);
9102 return isl_set_unwrap(isl_map_domain(map));
9105 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
9107 isl_size n_in, n_out;
9109 n_in = isl_space_dim(space, isl_dim_in);
9110 n_out = isl_space_dim(space, isl_dim_out);
9111 if (n_in < 0 || n_out < 0)
9112 goto error;
9113 if (n_in != n_out)
9114 isl_die(space->ctx, isl_error_invalid,
9115 "number of input and output dimensions needs to be "
9116 "the same", goto error);
9117 return isl_basic_map_equal(space, n_in);
9118 error:
9119 isl_space_free(space);
9120 return NULL;
9123 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
9125 return isl_map_from_basic_map(isl_basic_map_identity(space));
9128 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
9130 isl_space *space = isl_set_get_space(set);
9131 isl_map *id;
9132 id = isl_map_identity(isl_space_map_from_set(space));
9133 return isl_map_intersect_range(id, set);
9136 /* Construct a basic set with all set dimensions having only non-negative
9137 * values.
9139 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9140 __isl_take isl_space *space)
9142 int i;
9143 isl_size nparam;
9144 isl_size dim;
9145 isl_size total;
9146 struct isl_basic_set *bset;
9148 nparam = isl_space_dim(space, isl_dim_param);
9149 dim = isl_space_dim(space, isl_dim_set);
9150 total = isl_space_dim(space, isl_dim_all);
9151 if (nparam < 0 || dim < 0 || total < 0)
9152 space = isl_space_free(space);
9153 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9154 if (!bset)
9155 return NULL;
9156 for (i = 0; i < dim; ++i) {
9157 int k = isl_basic_set_alloc_inequality(bset);
9158 if (k < 0)
9159 goto error;
9160 isl_seq_clr(bset->ineq[k], 1 + total);
9161 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9163 return bset;
9164 error:
9165 isl_basic_set_free(bset);
9166 return NULL;
9169 /* Construct the half-space x_pos >= 0.
9171 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9172 int pos)
9174 int k;
9175 isl_size total;
9176 isl_basic_set *nonneg;
9178 total = isl_space_dim(space, isl_dim_all);
9179 if (total < 0)
9180 space = isl_space_free(space);
9181 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9182 k = isl_basic_set_alloc_inequality(nonneg);
9183 if (k < 0)
9184 goto error;
9185 isl_seq_clr(nonneg->ineq[k], 1 + total);
9186 isl_int_set_si(nonneg->ineq[k][pos], 1);
9188 return isl_basic_set_finalize(nonneg);
9189 error:
9190 isl_basic_set_free(nonneg);
9191 return NULL;
9194 /* Construct the half-space x_pos <= -1.
9196 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9197 int pos)
9199 int k;
9200 isl_size total;
9201 isl_basic_set *neg;
9203 total = isl_space_dim(space, isl_dim_all);
9204 if (total < 0)
9205 space = isl_space_free(space);
9206 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9207 k = isl_basic_set_alloc_inequality(neg);
9208 if (k < 0)
9209 goto error;
9210 isl_seq_clr(neg->ineq[k], 1 + total);
9211 isl_int_set_si(neg->ineq[k][0], -1);
9212 isl_int_set_si(neg->ineq[k][pos], -1);
9214 return isl_basic_set_finalize(neg);
9215 error:
9216 isl_basic_set_free(neg);
9217 return NULL;
9220 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9221 enum isl_dim_type type, unsigned first, unsigned n)
9223 int i;
9224 unsigned offset;
9225 isl_basic_set *nonneg;
9226 isl_basic_set *neg;
9228 if (n == 0)
9229 return set;
9231 if (isl_set_check_range(set, type, first, n) < 0)
9232 return isl_set_free(set);
9234 offset = pos(set->dim, type);
9235 for (i = 0; i < n; ++i) {
9236 nonneg = nonneg_halfspace(isl_set_get_space(set),
9237 offset + first + i);
9238 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9240 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9243 return set;
9246 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9247 int len,
9248 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9249 void *user)
9251 isl_set *half;
9253 if (!set)
9254 return isl_stat_error;
9255 if (isl_set_plain_is_empty(set)) {
9256 isl_set_free(set);
9257 return isl_stat_ok;
9259 if (first == len)
9260 return fn(set, signs, user);
9262 signs[first] = 1;
9263 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9264 1 + first));
9265 half = isl_set_intersect(half, isl_set_copy(set));
9266 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9267 goto error;
9269 signs[first] = -1;
9270 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9271 1 + first));
9272 half = isl_set_intersect(half, set);
9273 return foreach_orthant(half, signs, first + 1, len, fn, user);
9274 error:
9275 isl_set_free(set);
9276 return isl_stat_error;
9279 /* Call "fn" on the intersections of "set" with each of the orthants
9280 * (except for obviously empty intersections). The orthant is identified
9281 * by the signs array, with each entry having value 1 or -1 according
9282 * to the sign of the corresponding variable.
9284 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9285 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9286 void *user)
9288 isl_size nparam;
9289 isl_size nvar;
9290 int *signs;
9291 isl_stat r;
9293 if (!set)
9294 return isl_stat_error;
9295 if (isl_set_plain_is_empty(set))
9296 return isl_stat_ok;
9298 nparam = isl_set_dim(set, isl_dim_param);
9299 nvar = isl_set_dim(set, isl_dim_set);
9300 if (nparam < 0 || nvar < 0)
9301 return isl_stat_error;
9303 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9305 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9306 fn, user);
9308 free(signs);
9310 return r;
9313 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9315 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9318 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9319 __isl_keep isl_basic_map *bmap2)
9321 isl_bool is_subset;
9322 struct isl_map *map1;
9323 struct isl_map *map2;
9325 if (!bmap1 || !bmap2)
9326 return isl_bool_error;
9328 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9329 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9331 is_subset = isl_map_is_subset(map1, map2);
9333 isl_map_free(map1);
9334 isl_map_free(map2);
9336 return is_subset;
9339 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9340 __isl_keep isl_basic_set *bset2)
9342 return isl_basic_map_is_subset(bset1, bset2);
9345 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9346 __isl_keep isl_basic_map *bmap2)
9348 isl_bool is_subset;
9350 if (!bmap1 || !bmap2)
9351 return isl_bool_error;
9352 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9353 if (is_subset != isl_bool_true)
9354 return is_subset;
9355 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9356 return is_subset;
9359 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9360 __isl_keep isl_basic_set *bset2)
9362 return isl_basic_map_is_equal(
9363 bset_to_bmap(bset1), bset_to_bmap(bset2));
9366 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9368 int i;
9369 int is_empty;
9371 if (!map)
9372 return isl_bool_error;
9373 for (i = 0; i < map->n; ++i) {
9374 is_empty = isl_basic_map_is_empty(map->p[i]);
9375 if (is_empty < 0)
9376 return isl_bool_error;
9377 if (!is_empty)
9378 return isl_bool_false;
9380 return isl_bool_true;
9383 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9385 return map ? map->n == 0 : isl_bool_error;
9388 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9390 return set ? set->n == 0 : isl_bool_error;
9393 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9395 return isl_map_is_empty(set_to_map(set));
9398 #undef TYPE
9399 #define TYPE isl_basic_map
9401 static
9402 #include "isl_type_has_equal_space_bin_templ.c"
9403 #include "isl_type_check_equal_space_templ.c"
9405 /* Check that "bset1" and "bset2" live in the same space,
9406 * reporting an error if they do not.
9408 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9409 __isl_keep isl_basic_set *bset2)
9411 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9412 bset_to_bmap(bset1));
9415 #undef TYPE
9416 #define TYPE isl_map
9418 #include "isl_type_has_equal_space_bin_templ.c"
9419 #include "isl_type_check_equal_space_templ.c"
9420 #include "isl_type_has_space_templ.c"
9422 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9423 __isl_keep isl_set *set2)
9425 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9428 #undef TYPE1
9429 #define TYPE1 isl_map
9430 #undef TYPE2
9431 #define TYPE2 isl_basic_map
9432 #undef TYPE_PAIR
9433 #define TYPE_PAIR isl_map_basic_map
9435 static
9436 #include "isl_type_has_equal_space_templ.c"
9437 #include "isl_type_check_equal_space_templ.c"
9439 /* Check that "set" and "bset" live in the same space,
9440 * reporting an error if they do not.
9442 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9443 __isl_keep isl_basic_set *bset)
9445 return isl_map_basic_map_check_equal_space(set_to_map(set),
9446 bset_to_bmap(bset));
9449 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9451 isl_bool is_subset;
9453 if (!map1 || !map2)
9454 return isl_bool_error;
9455 is_subset = isl_map_is_subset(map1, map2);
9456 if (is_subset != isl_bool_true)
9457 return is_subset;
9458 is_subset = isl_map_is_subset(map2, map1);
9459 return is_subset;
9462 /* Is "map1" equal to "map2"?
9464 * First check if they are obviously equal.
9465 * If not, then perform a more detailed analysis.
9467 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9469 isl_bool equal;
9471 equal = isl_map_plain_is_equal(map1, map2);
9472 if (equal < 0 || equal)
9473 return equal;
9474 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9477 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9478 __isl_keep isl_basic_map *bmap2)
9480 isl_bool is_subset;
9482 if (!bmap1 || !bmap2)
9483 return isl_bool_error;
9484 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9485 if (is_subset != isl_bool_true)
9486 return is_subset;
9487 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9488 return isl_bool_not(is_subset);
9491 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9492 __isl_keep isl_map *map2)
9494 isl_bool is_subset;
9496 if (!map1 || !map2)
9497 return isl_bool_error;
9498 is_subset = isl_map_is_subset(map1, map2);
9499 if (is_subset != isl_bool_true)
9500 return is_subset;
9501 is_subset = isl_map_is_subset(map2, map1);
9502 return isl_bool_not(is_subset);
9505 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9506 __isl_keep isl_set *set2)
9508 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9511 /* Is "bmap" obviously equal to the universe with the same space?
9513 * That is, does it not have any constraints?
9515 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9517 if (!bmap)
9518 return isl_bool_error;
9519 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9522 /* Is "bset" obviously equal to the universe with the same space?
9524 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9526 return isl_basic_map_plain_is_universe(bset);
9529 /* If "c" does not involve any existentially quantified variables,
9530 * then set *univ to false and abort
9532 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9534 isl_bool *univ = user;
9535 isl_size n;
9537 n = isl_constraint_dim(c, isl_dim_div);
9538 if (n < 0)
9539 c = isl_constraint_free(c);
9540 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9541 isl_constraint_free(c);
9542 if (*univ < 0 || !*univ)
9543 return isl_stat_error;
9544 return isl_stat_ok;
9547 /* Is "bmap" equal to the universe with the same space?
9549 * First check if it is obviously equal to the universe.
9550 * If not and if there are any constraints not involving
9551 * existentially quantified variables, then it is certainly
9552 * not equal to the universe.
9553 * Otherwise, check if the universe is a subset of "bmap".
9555 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9557 isl_size n_div;
9558 isl_bool univ;
9559 isl_basic_map *test;
9561 univ = isl_basic_map_plain_is_universe(bmap);
9562 if (univ < 0 || univ)
9563 return univ;
9564 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9565 if (n_div < 0)
9566 return isl_bool_error;
9567 if (n_div == 0)
9568 return isl_bool_false;
9569 univ = isl_bool_true;
9570 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9571 univ)
9572 return isl_bool_error;
9573 if (univ < 0 || !univ)
9574 return univ;
9575 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9576 univ = isl_basic_map_is_subset(test, bmap);
9577 isl_basic_map_free(test);
9578 return univ;
9581 /* Is "bset" equal to the universe with the same space?
9583 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9585 return isl_basic_map_is_universe(bset);
9588 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9590 int i;
9592 if (!map)
9593 return isl_bool_error;
9595 for (i = 0; i < map->n; ++i) {
9596 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9597 if (r < 0 || r)
9598 return r;
9601 return isl_bool_false;
9604 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9606 return isl_map_plain_is_universe(set_to_map(set));
9609 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9611 struct isl_basic_set *bset = NULL;
9612 struct isl_vec *sample = NULL;
9613 isl_bool empty, non_empty;
9615 if (!bmap)
9616 return isl_bool_error;
9618 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9619 return isl_bool_true;
9621 if (isl_basic_map_plain_is_universe(bmap))
9622 return isl_bool_false;
9624 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9625 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9626 copy = isl_basic_map_remove_redundancies(copy);
9627 empty = isl_basic_map_plain_is_empty(copy);
9628 isl_basic_map_free(copy);
9629 return empty;
9632 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9633 if (non_empty < 0)
9634 return isl_bool_error;
9635 if (non_empty)
9636 return isl_bool_false;
9637 isl_vec_free(bmap->sample);
9638 bmap->sample = NULL;
9639 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9640 if (!bset)
9641 return isl_bool_error;
9642 sample = isl_basic_set_sample_vec(bset);
9643 if (!sample)
9644 return isl_bool_error;
9645 empty = sample->size == 0;
9646 isl_vec_free(bmap->sample);
9647 bmap->sample = sample;
9648 if (empty)
9649 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9651 return empty;
9654 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9656 if (!bmap)
9657 return isl_bool_error;
9658 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9661 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9663 if (!bset)
9664 return isl_bool_error;
9665 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9668 /* Is "bmap" known to be non-empty?
9670 * That is, is the cached sample still valid?
9672 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9674 isl_size total;
9676 if (!bmap)
9677 return isl_bool_error;
9678 if (!bmap->sample)
9679 return isl_bool_false;
9680 total = isl_basic_map_dim(bmap, isl_dim_all);
9681 if (total < 0)
9682 return isl_bool_error;
9683 if (bmap->sample->size != 1 + total)
9684 return isl_bool_false;
9685 return isl_basic_map_contains(bmap, bmap->sample);
9688 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9690 return isl_basic_map_is_empty(bset_to_bmap(bset));
9693 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9694 __isl_take isl_basic_map *bmap2)
9696 struct isl_map *map;
9698 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9699 goto error;
9701 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9702 if (!map)
9703 goto error;
9704 map = isl_map_add_basic_map(map, bmap1);
9705 map = isl_map_add_basic_map(map, bmap2);
9706 return map;
9707 error:
9708 isl_basic_map_free(bmap1);
9709 isl_basic_map_free(bmap2);
9710 return NULL;
9713 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9714 __isl_take isl_basic_set *bset2)
9716 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9717 bset_to_bmap(bset2)));
9720 /* Order divs such that any div only depends on previous divs */
9721 __isl_give isl_basic_map *isl_basic_map_order_divs(
9722 __isl_take isl_basic_map *bmap)
9724 int i;
9725 isl_size off;
9727 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9728 if (off < 0)
9729 return isl_basic_map_free(bmap);
9731 for (i = 0; i < bmap->n_div; ++i) {
9732 int pos;
9733 if (isl_int_is_zero(bmap->div[i][0]))
9734 continue;
9735 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9736 bmap->n_div-i);
9737 if (pos == -1)
9738 continue;
9739 if (pos == 0)
9740 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9741 "integer division depends on itself",
9742 return isl_basic_map_free(bmap));
9743 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9744 if (!bmap)
9745 return NULL;
9746 --i;
9748 return bmap;
9751 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9753 int i;
9755 if (!map)
9756 return 0;
9758 for (i = 0; i < map->n; ++i) {
9759 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9760 if (!map->p[i])
9761 goto error;
9764 return map;
9765 error:
9766 isl_map_free(map);
9767 return NULL;
9770 /* Sort the local variables of "bset".
9772 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9773 __isl_take isl_basic_set *bset)
9775 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9778 /* Apply the expansion computed by isl_merge_divs.
9779 * The expansion itself is given by "exp" while the resulting
9780 * list of divs is given by "div".
9782 * Move the integer divisions of "bmap" into the right position
9783 * according to "exp" and then introduce the additional integer
9784 * divisions, adding div constraints.
9785 * The moving should be done first to avoid moving coefficients
9786 * in the definitions of the extra integer divisions.
9788 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9789 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9791 int i, j;
9792 int n_div;
9794 bmap = isl_basic_map_cow(bmap);
9795 if (!bmap || !div)
9796 goto error;
9798 if (div->n_row < bmap->n_div)
9799 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9800 "not an expansion", goto error);
9802 n_div = bmap->n_div;
9803 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9804 2 * (div->n_row - n_div));
9806 for (i = n_div; i < div->n_row; ++i)
9807 if (isl_basic_map_alloc_div(bmap) < 0)
9808 goto error;
9810 for (j = n_div - 1; j >= 0; --j) {
9811 if (exp[j] == j)
9812 break;
9813 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9814 if (!bmap)
9815 goto error;
9817 j = 0;
9818 for (i = 0; i < div->n_row; ++i) {
9819 if (j < n_div && exp[j] == i) {
9820 j++;
9821 } else {
9822 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9823 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9824 continue;
9825 bmap = isl_basic_map_add_div_constraints(bmap, i);
9826 if (!bmap)
9827 goto error;
9831 isl_mat_free(div);
9832 return bmap;
9833 error:
9834 isl_basic_map_free(bmap);
9835 isl_mat_free(div);
9836 return NULL;
9839 /* Apply the expansion computed by isl_merge_divs.
9840 * The expansion itself is given by "exp" while the resulting
9841 * list of divs is given by "div".
9843 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9844 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9846 return isl_basic_map_expand_divs(bset, div, exp);
9849 /* Look for a div in dst that corresponds to the div "div" in src.
9850 * The divs before "div" in src and dst are assumed to be the same.
9852 * Return the position of the corresponding div in dst
9853 * if there is one. Otherwise, return a position beyond the integer divisions.
9854 * Return -1 on error.
9856 static int find_div(__isl_keep isl_basic_map *dst,
9857 __isl_keep isl_basic_map *src, unsigned div)
9859 int i;
9860 isl_size n_div;
9861 isl_size v_div;
9863 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9864 n_div = isl_basic_map_dim(dst, isl_dim_div);
9865 if (n_div < 0 || v_div < 0)
9866 return -1;
9867 isl_assert(dst->ctx, div <= n_div, return -1);
9868 for (i = div; i < n_div; ++i)
9869 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9870 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9871 n_div - div) == -1)
9872 return i;
9873 return n_div;
9876 /* Align the divs of "dst" to those of "src", adding divs from "src"
9877 * if needed. That is, make sure that the first src->n_div divs
9878 * of the result are equal to those of src.
9879 * The integer division of "src" are assumed to be ordered.
9881 * The integer divisions are swapped into the right position
9882 * (possibly after adding them first). This may result
9883 * in the remaining integer divisions appearing in the wrong order,
9884 * i.e., with some integer division appearing before
9885 * some other integer division on which it depends.
9886 * The integer divisions therefore need to be ordered.
9887 * This will not affect the integer divisions aligned to those of "src",
9888 * since "src" is assumed to have ordered integer divisions.
9890 * The result is not finalized as by design it will have redundant
9891 * divs if any divs from "src" were copied.
9893 __isl_give isl_basic_map *isl_basic_map_align_divs(
9894 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9896 int i;
9897 isl_bool known;
9898 int extended;
9899 isl_size v_div;
9900 isl_size dst_n_div;
9902 if (!dst || !src)
9903 return isl_basic_map_free(dst);
9905 if (src->n_div == 0)
9906 return dst;
9908 known = isl_basic_map_divs_known(src);
9909 if (known < 0)
9910 return isl_basic_map_free(dst);
9911 if (!known)
9912 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9913 "some src divs are unknown",
9914 return isl_basic_map_free(dst));
9916 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9917 if (v_div < 0)
9918 return isl_basic_map_free(dst);
9920 extended = 0;
9921 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9922 if (dst_n_div < 0)
9923 dst = isl_basic_map_free(dst);
9924 for (i = 0; i < src->n_div; ++i) {
9925 int j = find_div(dst, src, i);
9926 if (j < 0)
9927 dst = isl_basic_map_free(dst);
9928 if (j == dst_n_div) {
9929 if (!extended) {
9930 int extra = src->n_div - i;
9931 dst = isl_basic_map_cow(dst);
9932 if (!dst)
9933 return isl_basic_map_free(dst);
9934 dst = isl_basic_map_extend(dst,
9935 extra, 0, 2 * extra);
9936 extended = 1;
9938 j = isl_basic_map_alloc_div(dst);
9939 if (j < 0)
9940 return isl_basic_map_free(dst);
9941 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9942 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9943 dst_n_div++;
9944 dst = isl_basic_map_add_div_constraints(dst, j);
9945 if (!dst)
9946 return isl_basic_map_free(dst);
9948 if (j != i)
9949 dst = isl_basic_map_swap_div(dst, i, j);
9950 if (!dst)
9951 return isl_basic_map_free(dst);
9953 return isl_basic_map_order_divs(dst);
9956 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9958 int i;
9960 if (!map)
9961 return NULL;
9962 if (map->n == 0)
9963 return map;
9964 map = isl_map_compute_divs(map);
9965 map = isl_map_order_divs(map);
9966 map = isl_map_cow(map);
9967 if (!map)
9968 return NULL;
9970 for (i = 1; i < map->n; ++i)
9971 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9972 for (i = 1; i < map->n; ++i) {
9973 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9974 if (!map->p[i])
9975 return isl_map_free(map);
9978 map = isl_map_unmark_normalized(map);
9979 return map;
9982 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9984 return isl_map_align_divs_internal(map);
9987 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9989 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9992 /* Align the divs of the basic maps in "map" to those
9993 * of the basic maps in "list", as well as to the other basic maps in "map".
9994 * The elements in "list" are assumed to have known divs.
9996 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9997 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9999 int i;
10000 isl_size n;
10002 n = isl_basic_map_list_n_basic_map(list);
10003 map = isl_map_compute_divs(map);
10004 map = isl_map_cow(map);
10005 if (!map || n < 0)
10006 return isl_map_free(map);
10007 if (map->n == 0)
10008 return map;
10010 for (i = 0; i < n; ++i) {
10011 isl_basic_map *bmap;
10013 bmap = isl_basic_map_list_get_basic_map(list, i);
10014 bmap = isl_basic_map_order_divs(bmap);
10015 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
10016 isl_basic_map_free(bmap);
10018 if (!map->p[0])
10019 return isl_map_free(map);
10021 return isl_map_align_divs_internal(map);
10024 /* Align the divs of each element of "list" to those of "bmap".
10025 * Both "bmap" and the elements of "list" are assumed to have known divs.
10027 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
10028 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
10030 int i;
10031 isl_size n;
10033 n = isl_basic_map_list_n_basic_map(list);
10034 if (n < 0 || !bmap)
10035 return isl_basic_map_list_free(list);
10037 for (i = 0; i < n; ++i) {
10038 isl_basic_map *bmap_i;
10040 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10041 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
10042 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
10045 return list;
10048 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
10049 __isl_take isl_map *map)
10051 isl_bool ok;
10053 isl_map_align_params_set(&map, &set);
10054 ok = isl_map_compatible_domain(map, set);
10055 if (ok < 0)
10056 goto error;
10057 if (!ok)
10058 isl_die(isl_set_get_ctx(set), isl_error_invalid,
10059 "incompatible spaces", goto error);
10060 map = isl_map_intersect_domain(map, set);
10061 set = isl_map_range(map);
10062 return set;
10063 error:
10064 isl_set_free(set);
10065 isl_map_free(map);
10066 return NULL;
10069 /* There is no need to cow as removing empty parts doesn't change
10070 * the meaning of the set.
10072 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
10074 int i;
10076 if (!map)
10077 return NULL;
10079 for (i = map->n - 1; i >= 0; --i)
10080 map = remove_if_empty(map, i);
10082 return map;
10085 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10087 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10090 /* Create a binary relation that maps the shared initial "pos" dimensions
10091 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10093 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10094 __isl_keep isl_basic_set *bset2, int pos)
10096 isl_basic_map *bmap1;
10097 isl_basic_map *bmap2;
10099 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10100 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10101 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10102 isl_dim_out, 0, pos);
10103 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10104 isl_dim_out, 0, pos);
10105 return isl_basic_map_range_product(bmap1, bmap2);
10108 /* Given two basic sets bset1 and bset2, compute the maximal difference
10109 * between the values of dimension pos in bset1 and those in bset2
10110 * for any common value of the parameters and dimensions preceding pos.
10112 static enum isl_lp_result basic_set_maximal_difference_at(
10113 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10114 int pos, isl_int *opt)
10116 isl_basic_map *bmap1;
10117 struct isl_ctx *ctx;
10118 struct isl_vec *obj;
10119 isl_size total;
10120 isl_size nparam;
10121 isl_size dim1;
10122 enum isl_lp_result res;
10124 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10125 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10126 if (nparam < 0 || dim1 < 0 || !bset2)
10127 return isl_lp_error;
10129 bmap1 = join_initial(bset1, bset2, pos);
10130 total = isl_basic_map_dim(bmap1, isl_dim_all);
10131 if (total < 0)
10132 return isl_lp_error;
10134 ctx = bmap1->ctx;
10135 obj = isl_vec_alloc(ctx, 1 + total);
10136 if (!obj)
10137 goto error;
10138 isl_seq_clr(obj->block.data, 1 + total);
10139 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10140 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10141 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10142 opt, NULL, NULL);
10143 isl_basic_map_free(bmap1);
10144 isl_vec_free(obj);
10145 return res;
10146 error:
10147 isl_basic_map_free(bmap1);
10148 return isl_lp_error;
10151 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10152 * for any common value of the parameters and dimensions preceding pos
10153 * in both basic sets, the values of dimension pos in bset1 are
10154 * smaller or larger than those in bset2.
10156 * Returns
10157 * 1 if bset1 follows bset2
10158 * -1 if bset1 precedes bset2
10159 * 0 if bset1 and bset2 are incomparable
10160 * -2 if some error occurred.
10162 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10163 __isl_keep isl_basic_set *bset2, int pos)
10165 isl_int opt;
10166 enum isl_lp_result res;
10167 int cmp;
10169 isl_int_init(opt);
10171 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10173 if (res == isl_lp_empty)
10174 cmp = 0;
10175 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10176 res == isl_lp_unbounded)
10177 cmp = 1;
10178 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10179 cmp = -1;
10180 else
10181 cmp = -2;
10183 isl_int_clear(opt);
10184 return cmp;
10187 /* Given two basic sets bset1 and bset2, check whether
10188 * for any common value of the parameters and dimensions preceding pos
10189 * there is a value of dimension pos in bset1 that is larger
10190 * than a value of the same dimension in bset2.
10192 * Return
10193 * 1 if there exists such a pair
10194 * 0 if there is no such pair, but there is a pair of equal values
10195 * -1 otherwise
10196 * -2 if some error occurred.
10198 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10199 __isl_keep isl_basic_set *bset2, int pos)
10201 isl_bool empty;
10202 isl_basic_map *bmap;
10203 isl_size dim1;
10205 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10206 if (dim1 < 0)
10207 return -2;
10208 bmap = join_initial(bset1, bset2, pos);
10209 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10210 isl_dim_out, dim1 - pos);
10211 empty = isl_basic_map_is_empty(bmap);
10212 if (empty < 0)
10213 goto error;
10214 if (empty) {
10215 isl_basic_map_free(bmap);
10216 return -1;
10218 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10219 isl_dim_out, dim1 - pos);
10220 empty = isl_basic_map_is_empty(bmap);
10221 if (empty < 0)
10222 goto error;
10223 isl_basic_map_free(bmap);
10224 if (empty)
10225 return 0;
10226 return 1;
10227 error:
10228 isl_basic_map_free(bmap);
10229 return -2;
10232 /* Given two sets set1 and set2, check whether
10233 * for any common value of the parameters and dimensions preceding pos
10234 * there is a value of dimension pos in set1 that is larger
10235 * than a value of the same dimension in set2.
10237 * Return
10238 * 1 if there exists such a pair
10239 * 0 if there is no such pair, but there is a pair of equal values
10240 * -1 otherwise
10241 * -2 if some error occurred.
10243 int isl_set_follows_at(__isl_keep isl_set *set1,
10244 __isl_keep isl_set *set2, int pos)
10246 int i, j;
10247 int follows = -1;
10249 if (!set1 || !set2)
10250 return -2;
10252 for (i = 0; i < set1->n; ++i)
10253 for (j = 0; j < set2->n; ++j) {
10254 int f;
10255 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10256 if (f == 1 || f == -2)
10257 return f;
10258 if (f > follows)
10259 follows = f;
10262 return follows;
10265 static isl_bool isl_basic_map_plain_has_fixed_var(
10266 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10268 int i;
10269 int d;
10270 isl_size total;
10272 total = isl_basic_map_dim(bmap, isl_dim_all);
10273 if (total < 0)
10274 return isl_bool_error;
10275 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10276 for (; d+1 > pos; --d)
10277 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10278 break;
10279 if (d != pos)
10280 continue;
10281 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10282 return isl_bool_false;
10283 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10284 return isl_bool_false;
10285 if (!isl_int_is_one(bmap->eq[i][1+d]))
10286 return isl_bool_false;
10287 if (val)
10288 isl_int_neg(*val, bmap->eq[i][0]);
10289 return isl_bool_true;
10291 return isl_bool_false;
10294 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10295 unsigned pos, isl_int *val)
10297 int i;
10298 isl_int v;
10299 isl_int tmp;
10300 isl_bool fixed;
10302 if (!map)
10303 return isl_bool_error;
10304 if (map->n == 0)
10305 return isl_bool_false;
10306 if (map->n == 1)
10307 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10308 isl_int_init(v);
10309 isl_int_init(tmp);
10310 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10311 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10312 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10313 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10314 fixed = isl_bool_false;
10316 if (val)
10317 isl_int_set(*val, v);
10318 isl_int_clear(tmp);
10319 isl_int_clear(v);
10320 return fixed;
10323 static isl_bool isl_basic_set_plain_has_fixed_var(
10324 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10326 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10327 pos, val);
10330 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10331 enum isl_dim_type type, unsigned pos, isl_int *val)
10333 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10334 return isl_bool_error;
10335 return isl_basic_map_plain_has_fixed_var(bmap,
10336 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10339 /* If "bmap" obviously lies on a hyperplane where the given dimension
10340 * has a fixed value, then return that value.
10341 * Otherwise return NaN.
10343 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10344 __isl_keep isl_basic_map *bmap,
10345 enum isl_dim_type type, unsigned pos)
10347 isl_ctx *ctx;
10348 isl_val *v;
10349 isl_bool fixed;
10351 if (!bmap)
10352 return NULL;
10353 ctx = isl_basic_map_get_ctx(bmap);
10354 v = isl_val_alloc(ctx);
10355 if (!v)
10356 return NULL;
10357 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10358 if (fixed < 0)
10359 return isl_val_free(v);
10360 if (fixed) {
10361 isl_int_set_si(v->d, 1);
10362 return v;
10364 isl_val_free(v);
10365 return isl_val_nan(ctx);
10368 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10369 enum isl_dim_type type, unsigned pos, isl_int *val)
10371 if (isl_map_check_range(map, type, pos, 1) < 0)
10372 return isl_bool_error;
10373 return isl_map_plain_has_fixed_var(map,
10374 map_offset(map, type) - 1 + pos, val);
10377 /* If "map" obviously lies on a hyperplane where the given dimension
10378 * has a fixed value, then return that value.
10379 * Otherwise return NaN.
10381 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10382 enum isl_dim_type type, unsigned pos)
10384 isl_ctx *ctx;
10385 isl_val *v;
10386 isl_bool fixed;
10388 if (!map)
10389 return NULL;
10390 ctx = isl_map_get_ctx(map);
10391 v = isl_val_alloc(ctx);
10392 if (!v)
10393 return NULL;
10394 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10395 if (fixed < 0)
10396 return isl_val_free(v);
10397 if (fixed) {
10398 isl_int_set_si(v->d, 1);
10399 return v;
10401 isl_val_free(v);
10402 return isl_val_nan(ctx);
10405 /* If "set" obviously lies on a hyperplane where the given dimension
10406 * has a fixed value, then return that value.
10407 * Otherwise return NaN.
10409 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10410 enum isl_dim_type type, unsigned pos)
10412 return isl_map_plain_get_val_if_fixed(set, type, pos);
10415 /* Return a sequence of values in the same space as "set"
10416 * that are equal to the corresponding set dimensions of "set"
10417 * for those set dimensions that obviously lie on a hyperplane
10418 * where the dimension has a fixed value.
10419 * The other elements are set to NaN.
10421 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10422 __isl_keep isl_set *set)
10424 int i;
10425 isl_size n;
10426 isl_space *space;
10427 isl_multi_val *mv;
10429 space = isl_space_drop_all_params(isl_set_get_space(set));
10430 mv = isl_multi_val_alloc(space);
10431 n = isl_multi_val_size(mv);
10432 if (n < 0)
10433 return isl_multi_val_free(mv);
10435 for (i = 0; i < n; ++i) {
10436 isl_val *v;
10438 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10439 mv = isl_multi_val_set_val(mv, i, v);
10442 return mv;
10445 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10446 * then return this fixed value in *val.
10448 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10449 unsigned dim, isl_int *val)
10451 isl_size nparam;
10453 nparam = isl_basic_set_dim(bset, isl_dim_param);
10454 if (nparam < 0)
10455 return isl_bool_error;
10456 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10459 /* Return -1 if the constraint "c1" should be sorted before "c2"
10460 * and 1 if it should be sorted after "c2".
10461 * Return 0 if the two constraints are the same (up to the constant term).
10463 * In particular, if a constraint involves later variables than another
10464 * then it is sorted after this other constraint.
10465 * uset_gist depends on constraints without existentially quantified
10466 * variables sorting first.
10468 * For constraints that have the same latest variable, those
10469 * with the same coefficient for this latest variable (first in absolute value
10470 * and then in actual value) are grouped together.
10471 * This is useful for detecting pairs of constraints that can
10472 * be chained in their printed representation.
10474 * Finally, within a group, constraints are sorted according to
10475 * their coefficients (excluding the constant term).
10477 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10479 isl_int **c1 = (isl_int **) p1;
10480 isl_int **c2 = (isl_int **) p2;
10481 int l1, l2;
10482 unsigned size = *(unsigned *) arg;
10483 int cmp;
10485 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10486 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10488 if (l1 != l2)
10489 return l1 - l2;
10491 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10492 if (cmp != 0)
10493 return cmp;
10494 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10495 if (cmp != 0)
10496 return -cmp;
10498 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10501 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10502 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10503 * and 0 if the two constraints are the same (up to the constant term).
10505 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10506 isl_int *c1, isl_int *c2)
10508 isl_size total;
10509 unsigned size;
10511 total = isl_basic_map_dim(bmap, isl_dim_all);
10512 if (total < 0)
10513 return -2;
10514 size = total;
10515 return sort_constraint_cmp(&c1, &c2, &size);
10518 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10519 __isl_take isl_basic_map *bmap)
10521 isl_size total;
10522 unsigned size;
10524 if (!bmap)
10525 return NULL;
10526 if (bmap->n_ineq == 0)
10527 return bmap;
10528 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10529 return bmap;
10530 total = isl_basic_map_dim(bmap, isl_dim_all);
10531 if (total < 0)
10532 return isl_basic_map_free(bmap);
10533 size = total;
10534 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10535 &sort_constraint_cmp, &size) < 0)
10536 return isl_basic_map_free(bmap);
10537 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10538 return bmap;
10541 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10542 __isl_take isl_basic_set *bset)
10544 isl_basic_map *bmap = bset_to_bmap(bset);
10545 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10548 __isl_give isl_basic_map *isl_basic_map_normalize(
10549 __isl_take isl_basic_map *bmap)
10551 bmap = isl_basic_map_remove_redundancies(bmap);
10552 bmap = isl_basic_map_sort_constraints(bmap);
10553 return bmap;
10555 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10556 __isl_keep isl_basic_map *bmap2)
10558 int i, cmp;
10559 isl_size total;
10560 isl_space *space1, *space2;
10562 if (!bmap1 || !bmap2)
10563 return -1;
10565 if (bmap1 == bmap2)
10566 return 0;
10567 space1 = isl_basic_map_peek_space(bmap1);
10568 space2 = isl_basic_map_peek_space(bmap2);
10569 cmp = isl_space_cmp(space1, space2);
10570 if (cmp)
10571 return cmp;
10572 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10573 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10574 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10575 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10576 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10577 return 0;
10578 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10579 return 1;
10580 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10581 return -1;
10582 if (bmap1->n_eq != bmap2->n_eq)
10583 return bmap1->n_eq - bmap2->n_eq;
10584 if (bmap1->n_ineq != bmap2->n_ineq)
10585 return bmap1->n_ineq - bmap2->n_ineq;
10586 if (bmap1->n_div != bmap2->n_div)
10587 return bmap1->n_div - bmap2->n_div;
10588 total = isl_basic_map_dim(bmap1, isl_dim_all);
10589 if (total < 0)
10590 return -1;
10591 for (i = 0; i < bmap1->n_eq; ++i) {
10592 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10593 if (cmp)
10594 return cmp;
10596 for (i = 0; i < bmap1->n_ineq; ++i) {
10597 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10598 if (cmp)
10599 return cmp;
10601 for (i = 0; i < bmap1->n_div; ++i) {
10602 isl_bool unknown1, unknown2;
10604 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10605 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10606 if (unknown1 < 0 || unknown2 < 0)
10607 return -1;
10608 if (unknown1 && unknown2)
10609 continue;
10610 if (unknown1)
10611 return 1;
10612 if (unknown2)
10613 return -1;
10614 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10615 if (cmp)
10616 return cmp;
10618 return 0;
10621 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10622 __isl_keep isl_basic_set *bset2)
10624 return isl_basic_map_plain_cmp(bset1, bset2);
10627 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10629 int i, cmp;
10631 if (set1 == set2)
10632 return 0;
10633 if (set1->n != set2->n)
10634 return set1->n - set2->n;
10636 for (i = 0; i < set1->n; ++i) {
10637 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10638 if (cmp)
10639 return cmp;
10642 return 0;
10645 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10646 __isl_keep isl_basic_map *bmap2)
10648 if (!bmap1 || !bmap2)
10649 return isl_bool_error;
10650 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10653 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10654 __isl_keep isl_basic_set *bset2)
10656 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10657 bset_to_bmap(bset2));
10660 static int qsort_bmap_cmp(const void *p1, const void *p2)
10662 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10663 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10665 return isl_basic_map_plain_cmp(bmap1, bmap2);
10668 /* Sort the basic maps of "map" and remove duplicate basic maps.
10670 * While removing basic maps, we make sure that the basic maps remain
10671 * sorted because isl_map_normalize expects the basic maps of the result
10672 * to be sorted.
10674 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10676 int i, j;
10678 map = isl_map_remove_empty_parts(map);
10679 if (!map)
10680 return NULL;
10681 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10682 for (i = map->n - 1; i >= 1; --i) {
10683 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10684 continue;
10685 isl_basic_map_free(map->p[i-1]);
10686 for (j = i; j < map->n; ++j)
10687 map->p[j - 1] = map->p[j];
10688 map->n--;
10691 return map;
10694 /* Remove obvious duplicates among the basic maps of "map".
10696 * Unlike isl_map_normalize, this function does not remove redundant
10697 * constraints and only removes duplicates that have exactly the same
10698 * constraints in the input. It does sort the constraints and
10699 * the basic maps to ease the detection of duplicates.
10701 * If "map" has already been normalized or if the basic maps are
10702 * disjoint, then there can be no duplicates.
10704 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10706 int i;
10707 isl_basic_map *bmap;
10709 if (!map)
10710 return NULL;
10711 if (map->n <= 1)
10712 return map;
10713 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10714 return map;
10715 for (i = 0; i < map->n; ++i) {
10716 bmap = isl_basic_map_copy(map->p[i]);
10717 bmap = isl_basic_map_sort_constraints(bmap);
10718 if (!bmap)
10719 return isl_map_free(map);
10720 isl_basic_map_free(map->p[i]);
10721 map->p[i] = bmap;
10724 map = sort_and_remove_duplicates(map);
10725 return map;
10728 /* We normalize in place, but if anything goes wrong we need
10729 * to return NULL, so we need to make sure we don't change the
10730 * meaning of any possible other copies of map.
10732 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10734 int i;
10735 struct isl_basic_map *bmap;
10737 if (!map)
10738 return NULL;
10739 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10740 return map;
10741 for (i = 0; i < map->n; ++i) {
10742 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10743 if (!bmap)
10744 goto error;
10745 isl_basic_map_free(map->p[i]);
10746 map->p[i] = bmap;
10749 map = sort_and_remove_duplicates(map);
10750 if (map)
10751 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10752 return map;
10753 error:
10754 isl_map_free(map);
10755 return NULL;
10758 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10760 return set_from_map(isl_map_normalize(set_to_map(set)));
10763 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10764 __isl_keep isl_map *map2)
10766 int i;
10767 isl_bool equal;
10769 if (!map1 || !map2)
10770 return isl_bool_error;
10772 if (map1 == map2)
10773 return isl_bool_true;
10774 equal = isl_map_has_equal_space(map1, map2);
10775 if (equal < 0 || !equal)
10776 return equal;
10778 map1 = isl_map_copy(map1);
10779 map2 = isl_map_copy(map2);
10780 map1 = isl_map_normalize(map1);
10781 map2 = isl_map_normalize(map2);
10782 if (!map1 || !map2)
10783 goto error;
10784 equal = map1->n == map2->n;
10785 for (i = 0; equal && i < map1->n; ++i) {
10786 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10787 if (equal < 0)
10788 goto error;
10790 isl_map_free(map1);
10791 isl_map_free(map2);
10792 return equal;
10793 error:
10794 isl_map_free(map1);
10795 isl_map_free(map2);
10796 return isl_bool_error;
10799 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10800 __isl_keep isl_set *set2)
10802 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10805 /* Return the basic maps in "map" as a list.
10807 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10808 __isl_keep isl_map *map)
10810 int i;
10811 isl_ctx *ctx;
10812 isl_basic_map_list *list;
10814 if (!map)
10815 return NULL;
10816 ctx = isl_map_get_ctx(map);
10817 list = isl_basic_map_list_alloc(ctx, map->n);
10819 for (i = 0; i < map->n; ++i) {
10820 isl_basic_map *bmap;
10822 bmap = isl_basic_map_copy(map->p[i]);
10823 list = isl_basic_map_list_add(list, bmap);
10826 return list;
10829 /* Return the intersection of the elements in the non-empty list "list".
10830 * All elements are assumed to live in the same space.
10832 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10833 __isl_take isl_basic_map_list *list)
10835 int i;
10836 isl_size n;
10837 isl_basic_map *bmap;
10839 n = isl_basic_map_list_n_basic_map(list);
10840 if (n < 0)
10841 goto error;
10842 if (n < 1)
10843 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10844 "expecting non-empty list", goto error);
10846 bmap = isl_basic_map_list_get_basic_map(list, 0);
10847 for (i = 1; i < n; ++i) {
10848 isl_basic_map *bmap_i;
10850 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10851 bmap = isl_basic_map_intersect(bmap, bmap_i);
10854 isl_basic_map_list_free(list);
10855 return bmap;
10856 error:
10857 isl_basic_map_list_free(list);
10858 return NULL;
10861 /* Return the intersection of the elements in the non-empty list "list".
10862 * All elements are assumed to live in the same space.
10864 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10865 __isl_take isl_basic_set_list *list)
10867 return isl_basic_map_list_intersect(list);
10870 /* Return the union of the elements of "list".
10871 * The list is required to have at least one element.
10873 __isl_give isl_set *isl_basic_set_list_union(
10874 __isl_take isl_basic_set_list *list)
10876 int i;
10877 isl_size n;
10878 isl_space *space;
10879 isl_basic_set *bset;
10880 isl_set *set;
10882 n = isl_basic_set_list_n_basic_set(list);
10883 if (n < 0)
10884 goto error;
10885 if (n < 1)
10886 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10887 "expecting non-empty list", goto error);
10889 bset = isl_basic_set_list_get_basic_set(list, 0);
10890 space = isl_basic_set_get_space(bset);
10891 isl_basic_set_free(bset);
10893 set = isl_set_alloc_space(space, n, 0);
10894 for (i = 0; i < n; ++i) {
10895 bset = isl_basic_set_list_get_basic_set(list, i);
10896 set = isl_set_add_basic_set(set, bset);
10899 isl_basic_set_list_free(list);
10900 return set;
10901 error:
10902 isl_basic_set_list_free(list);
10903 return NULL;
10906 /* Return the union of the elements in the non-empty list "list".
10907 * All elements are assumed to live in the same space.
10909 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10911 int i;
10912 isl_size n;
10913 isl_set *set;
10915 n = isl_set_list_n_set(list);
10916 if (n < 0)
10917 goto error;
10918 if (n < 1)
10919 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10920 "expecting non-empty list", goto error);
10922 set = isl_set_list_get_set(list, 0);
10923 for (i = 1; i < n; ++i) {
10924 isl_set *set_i;
10926 set_i = isl_set_list_get_set(list, i);
10927 set = isl_set_union(set, set_i);
10930 isl_set_list_free(list);
10931 return set;
10932 error:
10933 isl_set_list_free(list);
10934 return NULL;
10937 __isl_give isl_basic_map *isl_basic_map_product(
10938 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10940 isl_space *space_result = NULL;
10941 struct isl_basic_map *bmap;
10942 unsigned in1, in2, out1, out2, nparam, total, pos;
10943 struct isl_dim_map *dim_map1, *dim_map2;
10945 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10946 goto error;
10947 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10948 isl_space_copy(bmap2->dim));
10950 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10951 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10952 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10953 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10954 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10956 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10957 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10958 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10959 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10960 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10961 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10962 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10965 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10966 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10968 bmap = isl_basic_map_alloc_space(space_result,
10969 bmap1->n_div + bmap2->n_div,
10970 bmap1->n_eq + bmap2->n_eq,
10971 bmap1->n_ineq + bmap2->n_ineq);
10972 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10973 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10974 bmap = isl_basic_map_simplify(bmap);
10975 return isl_basic_map_finalize(bmap);
10976 error:
10977 isl_basic_map_free(bmap1);
10978 isl_basic_map_free(bmap2);
10979 return NULL;
10982 __isl_give isl_basic_map *isl_basic_map_flat_product(
10983 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10985 isl_basic_map *prod;
10987 prod = isl_basic_map_product(bmap1, bmap2);
10988 prod = isl_basic_map_flatten(prod);
10989 return prod;
10992 __isl_give isl_basic_set *isl_basic_set_flat_product(
10993 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10995 return isl_basic_map_flat_range_product(bset1, bset2);
10998 __isl_give isl_basic_map *isl_basic_map_domain_product(
10999 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11001 isl_space *space1, *space2;
11002 isl_space *space_result = NULL;
11003 isl_basic_map *bmap;
11004 isl_size in1, in2, out, nparam;
11005 unsigned total, pos;
11006 struct isl_dim_map *dim_map1, *dim_map2;
11008 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11009 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11010 out = isl_basic_map_dim(bmap1, isl_dim_out);
11011 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11012 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
11013 goto error;
11015 space1 = isl_basic_map_get_space(bmap1);
11016 space2 = isl_basic_map_get_space(bmap2);
11017 space_result = isl_space_domain_product(space1, space2);
11019 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
11020 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11021 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11022 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11023 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11024 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11025 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11026 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11027 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
11028 isl_dim_map_div(dim_map1, bmap1, pos += out);
11029 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11031 bmap = isl_basic_map_alloc_space(space_result,
11032 bmap1->n_div + bmap2->n_div,
11033 bmap1->n_eq + bmap2->n_eq,
11034 bmap1->n_ineq + bmap2->n_ineq);
11035 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11036 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11037 bmap = isl_basic_map_simplify(bmap);
11038 return isl_basic_map_finalize(bmap);
11039 error:
11040 isl_basic_map_free(bmap1);
11041 isl_basic_map_free(bmap2);
11042 return NULL;
11045 __isl_give isl_basic_map *isl_basic_map_range_product(
11046 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11048 isl_bool rational;
11049 isl_space *space_result = NULL;
11050 isl_basic_map *bmap;
11051 isl_size in, out1, out2, nparam;
11052 unsigned total, pos;
11053 struct isl_dim_map *dim_map1, *dim_map2;
11055 rational = isl_basic_map_is_rational(bmap1);
11056 if (rational >= 0 && rational)
11057 rational = isl_basic_map_is_rational(bmap2);
11058 in = isl_basic_map_dim(bmap1, isl_dim_in);
11059 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11060 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11061 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11062 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
11063 goto error;
11065 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11066 goto error;
11068 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
11069 isl_space_copy(bmap2->dim));
11071 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
11072 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11073 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11074 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11075 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11076 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11077 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11078 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11079 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11080 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11081 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11083 bmap = isl_basic_map_alloc_space(space_result,
11084 bmap1->n_div + bmap2->n_div,
11085 bmap1->n_eq + bmap2->n_eq,
11086 bmap1->n_ineq + bmap2->n_ineq);
11087 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11088 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11089 if (rational)
11090 bmap = isl_basic_map_set_rational(bmap);
11091 bmap = isl_basic_map_simplify(bmap);
11092 return isl_basic_map_finalize(bmap);
11093 error:
11094 isl_basic_map_free(bmap1);
11095 isl_basic_map_free(bmap2);
11096 return NULL;
11099 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11100 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11102 isl_basic_map *prod;
11104 prod = isl_basic_map_range_product(bmap1, bmap2);
11105 prod = isl_basic_map_flatten_range(prod);
11106 return prod;
11109 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11110 * and collect the results.
11111 * The result live in the space obtained by calling "space_product"
11112 * on the spaces of "map1" and "map2".
11113 * If "remove_duplicates" is set then the result may contain duplicates
11114 * (even if the inputs do not) and so we try and remove the obvious
11115 * duplicates.
11117 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11118 __isl_take isl_map *map2,
11119 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11120 __isl_take isl_space *right),
11121 __isl_give isl_basic_map *(*basic_map_product)(
11122 __isl_take isl_basic_map *left,
11123 __isl_take isl_basic_map *right),
11124 int remove_duplicates)
11126 unsigned flags = 0;
11127 struct isl_map *result;
11128 int i, j;
11129 isl_bool m;
11131 m = isl_map_has_equal_params(map1, map2);
11132 if (m < 0)
11133 goto error;
11134 if (!m)
11135 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11136 "parameters don't match", goto error);
11138 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11139 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11140 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11142 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11143 isl_space_copy(map2->dim)),
11144 map1->n * map2->n, flags);
11145 if (!result)
11146 goto error;
11147 for (i = 0; i < map1->n; ++i)
11148 for (j = 0; j < map2->n; ++j) {
11149 struct isl_basic_map *part;
11150 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11151 isl_basic_map_copy(map2->p[j]));
11152 if (isl_basic_map_is_empty(part))
11153 isl_basic_map_free(part);
11154 else
11155 result = isl_map_add_basic_map(result, part);
11156 if (!result)
11157 goto error;
11159 if (remove_duplicates)
11160 result = isl_map_remove_obvious_duplicates(result);
11161 isl_map_free(map1);
11162 isl_map_free(map2);
11163 return result;
11164 error:
11165 isl_map_free(map1);
11166 isl_map_free(map2);
11167 return NULL;
11170 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11172 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11173 __isl_take isl_map *map2)
11175 isl_map_align_params_bin(&map1, &map2);
11176 return map_product(map1, map2, &isl_space_product,
11177 &isl_basic_map_product, 0);
11180 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11182 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11183 __isl_take isl_map *map2)
11185 isl_map *prod;
11187 prod = isl_map_product(map1, map2);
11188 prod = isl_map_flatten(prod);
11189 return prod;
11192 /* Given two set A and B, construct its Cartesian product A x B.
11194 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11195 __isl_take isl_set *set2)
11197 return isl_map_range_product(set1, set2);
11200 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11201 __isl_take isl_set *set2)
11203 return isl_map_flat_range_product(set1, set2);
11206 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11208 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11209 __isl_take isl_map *map2)
11211 isl_map_align_params_bin(&map1, &map2);
11212 return map_product(map1, map2, &isl_space_domain_product,
11213 &isl_basic_map_domain_product, 1);
11216 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11218 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11219 __isl_take isl_map *map2)
11221 isl_map_align_params_bin(&map1, &map2);
11222 return map_product(map1, map2, &isl_space_range_product,
11223 &isl_basic_map_range_product, 1);
11226 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11228 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11230 isl_space *space;
11231 isl_size total1, keep1, total2, keep2;
11233 total1 = isl_map_dim(map, isl_dim_in);
11234 total2 = isl_map_dim(map, isl_dim_out);
11235 if (total1 < 0 || total2 < 0)
11236 return isl_map_free(map);
11237 if (!isl_space_domain_is_wrapping(map->dim) ||
11238 !isl_space_range_is_wrapping(map->dim))
11239 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11240 "not a product", return isl_map_free(map));
11242 space = isl_map_get_space(map);
11243 space = isl_space_factor_domain(space);
11244 keep1 = isl_space_dim(space, isl_dim_in);
11245 keep2 = isl_space_dim(space, isl_dim_out);
11246 if (keep1 < 0 || keep2 < 0)
11247 map = isl_map_free(map);
11248 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11249 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11250 map = isl_map_reset_space(map, space);
11252 return map;
11255 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11257 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11259 isl_space *space;
11260 isl_size total1, keep1, total2, keep2;
11262 total1 = isl_map_dim(map, isl_dim_in);
11263 total2 = isl_map_dim(map, isl_dim_out);
11264 if (total1 < 0 || total2 < 0)
11265 return isl_map_free(map);
11266 if (!isl_space_domain_is_wrapping(map->dim) ||
11267 !isl_space_range_is_wrapping(map->dim))
11268 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11269 "not a product", return isl_map_free(map));
11271 space = isl_map_get_space(map);
11272 space = isl_space_factor_range(space);
11273 keep1 = isl_space_dim(space, isl_dim_in);
11274 keep2 = isl_space_dim(space, isl_dim_out);
11275 if (keep1 < 0 || keep2 < 0)
11276 map = isl_map_free(map);
11277 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11278 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11279 map = isl_map_reset_space(map, space);
11281 return map;
11284 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11286 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11288 isl_space *space;
11289 isl_size total, keep;
11291 total = isl_map_dim(map, isl_dim_in);
11292 if (total < 0)
11293 return isl_map_free(map);
11294 if (!isl_space_domain_is_wrapping(map->dim))
11295 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11296 "domain is not a product", return isl_map_free(map));
11298 space = isl_map_get_space(map);
11299 space = isl_space_domain_factor_domain(space);
11300 keep = isl_space_dim(space, isl_dim_in);
11301 if (keep < 0)
11302 map = isl_map_free(map);
11303 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11304 map = isl_map_reset_space(map, space);
11306 return map;
11309 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11311 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11313 isl_space *space;
11314 isl_size total, keep;
11316 total = isl_map_dim(map, isl_dim_in);
11317 if (total < 0)
11318 return isl_map_free(map);
11319 if (!isl_space_domain_is_wrapping(map->dim))
11320 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11321 "domain is not a product", return isl_map_free(map));
11323 space = isl_map_get_space(map);
11324 space = isl_space_domain_factor_range(space);
11325 keep = isl_space_dim(space, isl_dim_in);
11326 if (keep < 0)
11327 map = isl_map_free(map);
11328 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11329 map = isl_map_reset_space(map, space);
11331 return map;
11334 /* Given a map A -> [B -> C], extract the map A -> B.
11336 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11338 isl_space *space;
11339 isl_size total, keep;
11341 total = isl_map_dim(map, isl_dim_out);
11342 if (total < 0)
11343 return isl_map_free(map);
11344 if (!isl_space_range_is_wrapping(map->dim))
11345 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11346 "range is not a product", return isl_map_free(map));
11348 space = isl_map_get_space(map);
11349 space = isl_space_range_factor_domain(space);
11350 keep = isl_space_dim(space, isl_dim_out);
11351 if (keep < 0)
11352 map = isl_map_free(map);
11353 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11354 map = isl_map_reset_space(map, space);
11356 return map;
11359 /* Given a map A -> [B -> C], extract the map A -> C.
11361 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11363 isl_space *space;
11364 isl_size total, keep;
11366 total = isl_map_dim(map, isl_dim_out);
11367 if (total < 0)
11368 return isl_map_free(map);
11369 if (!isl_space_range_is_wrapping(map->dim))
11370 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11371 "range is not a product", return isl_map_free(map));
11373 space = isl_map_get_space(map);
11374 space = isl_space_range_factor_range(space);
11375 keep = isl_space_dim(space, isl_dim_out);
11376 if (keep < 0)
11377 map = isl_map_free(map);
11378 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11379 map = isl_map_reset_space(map, space);
11381 return map;
11384 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11386 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11387 __isl_take isl_map *map2)
11389 isl_map *prod;
11391 prod = isl_map_domain_product(map1, map2);
11392 prod = isl_map_flatten_domain(prod);
11393 return prod;
11396 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11398 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11399 __isl_take isl_map *map2)
11401 isl_map *prod;
11403 prod = isl_map_range_product(map1, map2);
11404 prod = isl_map_flatten_range(prod);
11405 return prod;
11408 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11410 int i;
11411 uint32_t hash = isl_hash_init();
11412 isl_size total;
11414 if (!bmap)
11415 return 0;
11416 bmap = isl_basic_map_copy(bmap);
11417 bmap = isl_basic_map_normalize(bmap);
11418 total = isl_basic_map_dim(bmap, isl_dim_all);
11419 if (total < 0)
11420 return 0;
11421 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11422 for (i = 0; i < bmap->n_eq; ++i) {
11423 uint32_t c_hash;
11424 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11425 isl_hash_hash(hash, c_hash);
11427 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11428 for (i = 0; i < bmap->n_ineq; ++i) {
11429 uint32_t c_hash;
11430 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11431 isl_hash_hash(hash, c_hash);
11433 isl_hash_byte(hash, bmap->n_div & 0xFF);
11434 for (i = 0; i < bmap->n_div; ++i) {
11435 uint32_t c_hash;
11436 if (isl_int_is_zero(bmap->div[i][0]))
11437 continue;
11438 isl_hash_byte(hash, i & 0xFF);
11439 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11440 isl_hash_hash(hash, c_hash);
11442 isl_basic_map_free(bmap);
11443 return hash;
11446 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11448 return isl_basic_map_get_hash(bset_to_bmap(bset));
11451 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11453 int i;
11454 uint32_t hash;
11456 if (!map)
11457 return 0;
11458 map = isl_map_copy(map);
11459 map = isl_map_normalize(map);
11460 if (!map)
11461 return 0;
11463 hash = isl_hash_init();
11464 for (i = 0; i < map->n; ++i) {
11465 uint32_t bmap_hash;
11466 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11467 isl_hash_hash(hash, bmap_hash);
11470 isl_map_free(map);
11472 return hash;
11475 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11477 return isl_map_get_hash(set_to_map(set));
11480 /* Return the number of basic maps in the (current) representation of "map".
11482 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11484 return map ? map->n : isl_size_error;
11487 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11489 return set ? set->n : isl_size_error;
11492 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11493 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11495 int i;
11497 if (!map)
11498 return isl_stat_error;
11500 for (i = 0; i < map->n; ++i)
11501 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11502 return isl_stat_error;
11504 return isl_stat_ok;
11507 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11508 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11510 int i;
11512 if (!set)
11513 return isl_stat_error;
11515 for (i = 0; i < set->n; ++i)
11516 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11517 return isl_stat_error;
11519 return isl_stat_ok;
11522 /* Does "test" succeed on every basic set in "set"?
11524 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11525 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11526 void *user)
11528 int i;
11530 if (!set)
11531 return isl_bool_error;
11533 for (i = 0; i < set->n; ++i) {
11534 isl_bool r;
11536 r = test(set->p[i], user);
11537 if (r < 0 || !r)
11538 return r;
11541 return isl_bool_true;
11544 /* Return a list of basic sets, the union of which is equal to "set".
11546 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11547 __isl_keep isl_set *set)
11549 int i;
11550 isl_basic_set_list *list;
11552 if (!set)
11553 return NULL;
11555 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11556 for (i = 0; i < set->n; ++i) {
11557 isl_basic_set *bset;
11559 bset = isl_basic_set_copy(set->p[i]);
11560 list = isl_basic_set_list_add(list, bset);
11563 return list;
11566 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11568 isl_space *space;
11570 if (!bset)
11571 return NULL;
11573 bset = isl_basic_set_cow(bset);
11574 if (!bset)
11575 return NULL;
11577 space = isl_basic_set_get_space(bset);
11578 space = isl_space_lift(space, bset->n_div);
11579 if (!space)
11580 goto error;
11581 isl_space_free(bset->dim);
11582 bset->dim = space;
11583 bset->extra -= bset->n_div;
11584 bset->n_div = 0;
11586 bset = isl_basic_set_finalize(bset);
11588 return bset;
11589 error:
11590 isl_basic_set_free(bset);
11591 return NULL;
11594 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11596 int i;
11597 isl_space *space;
11598 unsigned n_div;
11600 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11602 if (!set)
11603 return NULL;
11605 set = isl_set_cow(set);
11606 if (!set)
11607 return NULL;
11609 n_div = set->p[0]->n_div;
11610 space = isl_set_get_space(set);
11611 space = isl_space_lift(space, n_div);
11612 if (!space)
11613 goto error;
11614 isl_space_free(set->dim);
11615 set->dim = space;
11617 for (i = 0; i < set->n; ++i) {
11618 set->p[i] = isl_basic_set_lift(set->p[i]);
11619 if (!set->p[i])
11620 goto error;
11623 return set;
11624 error:
11625 isl_set_free(set);
11626 return NULL;
11629 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11631 isl_size dim;
11632 int size = 0;
11634 dim = isl_basic_set_dim(bset, isl_dim_all);
11635 if (dim < 0)
11636 return -1;
11637 size += bset->n_eq * (1 + dim);
11638 size += bset->n_ineq * (1 + dim);
11639 size += bset->n_div * (2 + dim);
11641 return size;
11644 int isl_set_size(__isl_keep isl_set *set)
11646 int i;
11647 int size = 0;
11649 if (!set)
11650 return -1;
11652 for (i = 0; i < set->n; ++i)
11653 size += isl_basic_set_size(set->p[i]);
11655 return size;
11658 /* Check if there is any lower bound (if lower == 0) and/or upper
11659 * bound (if upper == 0) on the specified dim.
11661 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11662 enum isl_dim_type type, unsigned pos, int lower, int upper)
11664 int i;
11665 isl_bool involves;
11666 isl_size off;
11668 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11669 return isl_bool_error;
11671 off = isl_basic_map_var_offset(bmap, type);
11672 if (off < 0)
11673 return isl_bool_error;
11674 involves = isl_basic_map_any_div_involves_vars(bmap, off + pos, 1);
11675 if (involves < 0 || involves)
11676 return involves;
11678 for (i = 0; i < bmap->n_eq; ++i)
11679 if (!isl_int_is_zero(bmap->eq[i][1 + off + pos]))
11680 return isl_bool_true;
11682 for (i = 0; i < bmap->n_ineq; ++i) {
11683 int sgn = isl_int_sgn(bmap->ineq[i][1 + off + pos]);
11684 if (sgn > 0)
11685 lower = 1;
11686 if (sgn < 0)
11687 upper = 1;
11690 return lower && upper;
11693 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11694 enum isl_dim_type type, unsigned pos)
11696 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11699 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11700 enum isl_dim_type type, unsigned pos)
11702 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11705 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11706 enum isl_dim_type type, unsigned pos)
11708 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11711 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11712 enum isl_dim_type type, unsigned pos)
11714 int i;
11716 if (!map)
11717 return isl_bool_error;
11719 for (i = 0; i < map->n; ++i) {
11720 isl_bool bounded;
11721 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11722 if (bounded < 0 || !bounded)
11723 return bounded;
11726 return isl_bool_true;
11729 /* Return true if the specified dim is involved in both an upper bound
11730 * and a lower bound.
11732 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11733 enum isl_dim_type type, unsigned pos)
11735 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11738 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11740 static isl_bool has_any_bound(__isl_keep isl_map *map,
11741 enum isl_dim_type type, unsigned pos,
11742 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11743 enum isl_dim_type type, unsigned pos))
11745 int i;
11747 if (!map)
11748 return isl_bool_error;
11750 for (i = 0; i < map->n; ++i) {
11751 isl_bool bounded;
11752 bounded = fn(map->p[i], type, pos);
11753 if (bounded < 0 || bounded)
11754 return bounded;
11757 return isl_bool_false;
11760 /* Return 1 if the specified dim is involved in any lower bound.
11762 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11763 enum isl_dim_type type, unsigned pos)
11765 return has_any_bound(set, type, pos,
11766 &isl_basic_map_dim_has_lower_bound);
11769 /* Return 1 if the specified dim is involved in any upper bound.
11771 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11772 enum isl_dim_type type, unsigned pos)
11774 return has_any_bound(set, type, pos,
11775 &isl_basic_map_dim_has_upper_bound);
11778 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11780 static isl_bool has_bound(__isl_keep isl_map *map,
11781 enum isl_dim_type type, unsigned pos,
11782 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11783 enum isl_dim_type type, unsigned pos))
11785 int i;
11787 if (!map)
11788 return isl_bool_error;
11790 for (i = 0; i < map->n; ++i) {
11791 isl_bool bounded;
11792 bounded = fn(map->p[i], type, pos);
11793 if (bounded < 0 || !bounded)
11794 return bounded;
11797 return isl_bool_true;
11800 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11802 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11803 enum isl_dim_type type, unsigned pos)
11805 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11808 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11810 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11811 enum isl_dim_type type, unsigned pos)
11813 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11816 /* For each of the "n" variables starting at "first", determine
11817 * the sign of the variable and put the results in the first "n"
11818 * elements of the array "signs".
11819 * Sign
11820 * 1 means that the variable is non-negative
11821 * -1 means that the variable is non-positive
11822 * 0 means the variable attains both positive and negative values.
11824 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11825 unsigned first, unsigned n, int *signs)
11827 isl_vec *bound = NULL;
11828 struct isl_tab *tab = NULL;
11829 struct isl_tab_undo *snap;
11830 int i;
11831 isl_size total;
11833 total = isl_basic_set_dim(bset, isl_dim_all);
11834 if (total < 0 || !signs)
11835 return isl_stat_error;
11837 bound = isl_vec_alloc(bset->ctx, 1 + total);
11838 tab = isl_tab_from_basic_set(bset, 0);
11839 if (!bound || !tab)
11840 goto error;
11842 isl_seq_clr(bound->el, bound->size);
11843 isl_int_set_si(bound->el[0], -1);
11845 snap = isl_tab_snap(tab);
11846 for (i = 0; i < n; ++i) {
11847 int empty;
11849 isl_int_set_si(bound->el[1 + first + i], -1);
11850 if (isl_tab_add_ineq(tab, bound->el) < 0)
11851 goto error;
11852 empty = tab->empty;
11853 isl_int_set_si(bound->el[1 + first + i], 0);
11854 if (isl_tab_rollback(tab, snap) < 0)
11855 goto error;
11857 if (empty) {
11858 signs[i] = 1;
11859 continue;
11862 isl_int_set_si(bound->el[1 + first + i], 1);
11863 if (isl_tab_add_ineq(tab, bound->el) < 0)
11864 goto error;
11865 empty = tab->empty;
11866 isl_int_set_si(bound->el[1 + first + i], 0);
11867 if (isl_tab_rollback(tab, snap) < 0)
11868 goto error;
11870 signs[i] = empty ? -1 : 0;
11873 isl_tab_free(tab);
11874 isl_vec_free(bound);
11875 return isl_stat_ok;
11876 error:
11877 isl_tab_free(tab);
11878 isl_vec_free(bound);
11879 return isl_stat_error;
11882 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11883 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11885 if (!bset || !signs)
11886 return isl_stat_error;
11887 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11888 return isl_stat_error;
11890 first += pos(bset->dim, type) - 1;
11891 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11894 /* Is it possible for the integer division "div" to depend (possibly
11895 * indirectly) on any output dimensions?
11897 * If the div is undefined, then we conservatively assume that it
11898 * may depend on them.
11899 * Otherwise, we check if it actually depends on them or on any integer
11900 * divisions that may depend on them.
11902 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11904 int i;
11905 isl_size n_out, n_div;
11906 unsigned o_out, o_div;
11908 if (isl_int_is_zero(bmap->div[div][0]))
11909 return isl_bool_true;
11911 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11912 if (n_out < 0)
11913 return isl_bool_error;
11914 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11916 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11917 return isl_bool_true;
11919 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11920 if (n_div < 0)
11921 return isl_bool_error;
11922 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11924 for (i = 0; i < n_div; ++i) {
11925 isl_bool may_involve;
11927 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11928 continue;
11929 may_involve = div_may_involve_output(bmap, i);
11930 if (may_involve < 0 || may_involve)
11931 return may_involve;
11934 return isl_bool_false;
11937 /* Return the first integer division of "bmap" in the range
11938 * [first, first + n[ that may depend on any output dimensions and
11939 * that has a non-zero coefficient in "c" (where the first coefficient
11940 * in "c" corresponds to integer division "first").
11942 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11943 isl_int *c, int first, int n)
11945 int k;
11947 if (!bmap)
11948 return -1;
11950 for (k = first; k < first + n; ++k) {
11951 isl_bool may_involve;
11953 if (isl_int_is_zero(c[k]))
11954 continue;
11955 may_involve = div_may_involve_output(bmap, k);
11956 if (may_involve < 0)
11957 return -1;
11958 if (may_involve)
11959 return k;
11962 return first + n;
11965 /* Look for a pair of inequality constraints in "bmap" of the form
11967 * -l + i >= 0 or i >= l
11968 * and
11969 * n + l - i >= 0 or i <= l + n
11971 * with n < "m" and i the output dimension at position "pos".
11972 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11973 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11974 * and earlier output dimensions, as well as integer divisions that do
11975 * not involve any of the output dimensions.
11977 * Return the index of the first inequality constraint or bmap->n_ineq
11978 * if no such pair can be found.
11980 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11981 int pos, isl_int m)
11983 int i, j;
11984 isl_ctx *ctx;
11985 isl_size total;
11986 isl_size n_div, n_out;
11987 unsigned o_div, o_out;
11988 int less;
11990 total = isl_basic_map_dim(bmap, isl_dim_all);
11991 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11992 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11993 if (total < 0 || n_out < 0 || n_div < 0)
11994 return -1;
11996 ctx = isl_basic_map_get_ctx(bmap);
11997 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11998 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11999 for (i = 0; i < bmap->n_ineq; ++i) {
12000 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
12001 continue;
12002 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
12003 n_out - (pos + 1)) != -1)
12004 continue;
12005 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
12006 0, n_div) < n_div)
12007 continue;
12008 for (j = i + 1; j < bmap->n_ineq; ++j) {
12009 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
12010 ctx->one))
12011 continue;
12012 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
12013 bmap->ineq[j] + 1, total))
12014 continue;
12015 break;
12017 if (j >= bmap->n_ineq)
12018 continue;
12019 isl_int_add(bmap->ineq[i][0],
12020 bmap->ineq[i][0], bmap->ineq[j][0]);
12021 less = isl_int_abs_lt(bmap->ineq[i][0], m);
12022 isl_int_sub(bmap->ineq[i][0],
12023 bmap->ineq[i][0], bmap->ineq[j][0]);
12024 if (!less)
12025 continue;
12026 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
12027 return i;
12028 else
12029 return j;
12032 return bmap->n_ineq;
12035 /* Return the index of the equality of "bmap" that defines
12036 * the output dimension "pos" in terms of earlier dimensions.
12037 * The equality may also involve integer divisions, as long
12038 * as those integer divisions are defined in terms of
12039 * parameters or input dimensions.
12040 * In this case, *div is set to the number of integer divisions and
12041 * *ineq is set to the number of inequality constraints (provided
12042 * div and ineq are not NULL).
12044 * The equality may also involve a single integer division involving
12045 * the output dimensions (typically only output dimension "pos") as
12046 * long as the coefficient of output dimension "pos" is 1 or -1 and
12047 * there is a pair of constraints i >= l and i <= l + n, with i referring
12048 * to output dimension "pos", l an expression involving only earlier
12049 * dimensions and n smaller than the coefficient of the integer division
12050 * in the equality. In this case, the output dimension can be defined
12051 * in terms of a modulo expression that does not involve the integer division.
12052 * *div is then set to this single integer division and
12053 * *ineq is set to the index of constraint i >= l.
12055 * Return bmap->n_eq if there is no such equality.
12056 * Return -1 on error.
12058 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
12059 int pos, int *div, int *ineq)
12061 int j, k, l;
12062 isl_size n_div, n_out;
12063 unsigned o_div, o_out;
12065 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12066 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12067 if (n_out < 0 || n_div < 0)
12068 return -1;
12070 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12071 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12073 if (ineq)
12074 *ineq = bmap->n_ineq;
12075 if (div)
12076 *div = n_div;
12077 for (j = 0; j < bmap->n_eq; ++j) {
12078 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12079 continue;
12080 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
12081 n_out - (pos + 1)) != -1)
12082 continue;
12083 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12084 0, n_div);
12085 if (k >= n_div)
12086 return j;
12087 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12088 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12089 continue;
12090 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12091 k + 1, n_div - (k+1)) < n_div)
12092 continue;
12093 l = find_modulo_constraint_pair(bmap, pos,
12094 bmap->eq[j][o_div + k]);
12095 if (l < 0)
12096 return -1;
12097 if (l >= bmap->n_ineq)
12098 continue;
12099 if (div)
12100 *div = k;
12101 if (ineq)
12102 *ineq = l;
12103 return j;
12106 return bmap->n_eq;
12109 /* Check if the given basic map is obviously single-valued.
12110 * In particular, for each output dimension, check that there is
12111 * an equality that defines the output dimension in terms of
12112 * earlier dimensions.
12114 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12116 int i;
12117 isl_size n_out;
12119 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12120 if (n_out < 0)
12121 return isl_bool_error;
12123 for (i = 0; i < n_out; ++i) {
12124 int eq;
12126 eq = isl_basic_map_output_defining_equality(bmap, i,
12127 NULL, NULL);
12128 if (eq < 0)
12129 return isl_bool_error;
12130 if (eq >= bmap->n_eq)
12131 return isl_bool_false;
12134 return isl_bool_true;
12137 /* Check if the given basic map is single-valued.
12138 * We simply compute
12140 * M \circ M^-1
12142 * and check if the result is a subset of the identity mapping.
12144 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12146 isl_space *space;
12147 isl_basic_map *test;
12148 isl_basic_map *id;
12149 isl_bool sv;
12151 sv = isl_basic_map_plain_is_single_valued(bmap);
12152 if (sv < 0 || sv)
12153 return sv;
12155 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12156 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12158 space = isl_basic_map_get_space(bmap);
12159 space = isl_space_map_from_set(isl_space_range(space));
12160 id = isl_basic_map_identity(space);
12162 sv = isl_basic_map_is_subset(test, id);
12164 isl_basic_map_free(test);
12165 isl_basic_map_free(id);
12167 return sv;
12170 /* Check if the given map is obviously single-valued.
12172 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12174 if (!map)
12175 return isl_bool_error;
12176 if (map->n == 0)
12177 return isl_bool_true;
12178 if (map->n >= 2)
12179 return isl_bool_false;
12181 return isl_basic_map_plain_is_single_valued(map->p[0]);
12184 /* Check if the given map is single-valued.
12185 * We simply compute
12187 * M \circ M^-1
12189 * and check if the result is a subset of the identity mapping.
12191 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12193 isl_space *space;
12194 isl_map *test;
12195 isl_map *id;
12196 isl_bool sv;
12198 sv = isl_map_plain_is_single_valued(map);
12199 if (sv < 0 || sv)
12200 return sv;
12202 test = isl_map_reverse(isl_map_copy(map));
12203 test = isl_map_apply_range(test, isl_map_copy(map));
12205 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12206 id = isl_map_identity(space);
12208 sv = isl_map_is_subset(test, id);
12210 isl_map_free(test);
12211 isl_map_free(id);
12213 return sv;
12216 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12218 isl_bool in;
12220 map = isl_map_copy(map);
12221 map = isl_map_reverse(map);
12222 in = isl_map_is_single_valued(map);
12223 isl_map_free(map);
12225 return in;
12228 /* Check if the given map is obviously injective.
12230 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12232 isl_bool in;
12234 map = isl_map_copy(map);
12235 map = isl_map_reverse(map);
12236 in = isl_map_plain_is_single_valued(map);
12237 isl_map_free(map);
12239 return in;
12242 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12244 isl_bool sv;
12246 sv = isl_map_is_single_valued(map);
12247 if (sv < 0 || !sv)
12248 return sv;
12250 return isl_map_is_injective(map);
12253 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12255 return isl_map_is_single_valued(set_to_map(set));
12258 /* Does "map" only map elements to themselves?
12260 * If the domain and range spaces are different, then "map"
12261 * is considered not to be an identity relation, even if it is empty.
12262 * Otherwise, construct the maximal identity relation and
12263 * check whether "map" is a subset of this relation.
12265 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12267 isl_map *id;
12268 isl_bool equal, is_identity;
12270 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12271 if (equal < 0 || !equal)
12272 return equal;
12274 id = isl_map_identity(isl_map_get_space(map));
12275 is_identity = isl_map_is_subset(map, id);
12276 isl_map_free(id);
12278 return is_identity;
12281 int isl_map_is_translation(__isl_keep isl_map *map)
12283 int ok;
12284 isl_set *delta;
12286 delta = isl_map_deltas(isl_map_copy(map));
12287 ok = isl_set_is_singleton(delta);
12288 isl_set_free(delta);
12290 return ok;
12293 static int unique(isl_int *p, unsigned pos, unsigned len)
12295 if (isl_seq_first_non_zero(p, pos) != -1)
12296 return 0;
12297 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12298 return 0;
12299 return 1;
12302 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12304 int i, j;
12305 isl_size nvar, ovar, n_div;
12307 n_div = isl_basic_set_dim(bset, isl_dim_div);
12308 if (n_div < 0)
12309 return isl_bool_error;
12310 if (n_div != 0)
12311 return isl_bool_false;
12313 nvar = isl_basic_set_dim(bset, isl_dim_set);
12314 ovar = isl_space_offset(bset->dim, isl_dim_set);
12315 if (nvar < 0 || ovar < 0)
12316 return isl_bool_error;
12317 for (j = 0; j < nvar; ++j) {
12318 int lower = 0, upper = 0;
12319 for (i = 0; i < bset->n_eq; ++i) {
12320 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12321 continue;
12322 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12323 return isl_bool_false;
12324 break;
12326 if (i < bset->n_eq)
12327 continue;
12328 for (i = 0; i < bset->n_ineq; ++i) {
12329 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12330 continue;
12331 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12332 return isl_bool_false;
12333 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12334 lower = 1;
12335 else
12336 upper = 1;
12338 if (!lower || !upper)
12339 return isl_bool_false;
12342 return isl_bool_true;
12345 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12347 if (!set)
12348 return isl_bool_error;
12349 if (set->n != 1)
12350 return isl_bool_false;
12352 return isl_basic_set_is_box(set->p[0]);
12355 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12357 if (!bset)
12358 return isl_bool_error;
12360 return isl_space_is_wrapping(bset->dim);
12363 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12365 if (!set)
12366 return isl_bool_error;
12368 return isl_space_is_wrapping(set->dim);
12371 /* Modify the space of "map" through a call to "change".
12372 * If "can_change" is set (not NULL), then first call it to check
12373 * if the modification is allowed, printing the error message "cannot_change"
12374 * if it is not.
12376 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12377 isl_bool (*can_change)(__isl_keep isl_map *map),
12378 const char *cannot_change,
12379 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12381 isl_bool ok;
12382 isl_space *space;
12384 if (!map)
12385 return NULL;
12387 ok = can_change ? can_change(map) : isl_bool_true;
12388 if (ok < 0)
12389 return isl_map_free(map);
12390 if (!ok)
12391 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12392 return isl_map_free(map));
12394 space = change(isl_map_get_space(map));
12395 map = isl_map_reset_space(map, space);
12397 return map;
12400 /* Is the domain of "map" a wrapped relation?
12402 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12404 if (!map)
12405 return isl_bool_error;
12407 return isl_space_domain_is_wrapping(map->dim);
12410 /* Does "map" have a wrapped relation in both domain and range?
12412 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12414 return isl_space_is_product(isl_map_peek_space(map));
12417 /* Is the range of "map" a wrapped relation?
12419 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12421 if (!map)
12422 return isl_bool_error;
12424 return isl_space_range_is_wrapping(map->dim);
12427 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12429 isl_space *space;
12431 space = isl_basic_map_take_space(bmap);
12432 space = isl_space_wrap(space);
12433 bmap = isl_basic_map_restore_space(bmap, space);
12435 bmap = isl_basic_map_finalize(bmap);
12437 return bset_from_bmap(bmap);
12440 /* Given a map A -> B, return the set (A -> B).
12442 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12444 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12447 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12449 bset = isl_basic_set_cow(bset);
12450 if (!bset)
12451 return NULL;
12453 bset->dim = isl_space_unwrap(bset->dim);
12454 if (!bset->dim)
12455 goto error;
12457 bset = isl_basic_set_finalize(bset);
12459 return bset_to_bmap(bset);
12460 error:
12461 isl_basic_set_free(bset);
12462 return NULL;
12465 /* Given a set (A -> B), return the map A -> B.
12466 * Error out if "set" is not of the form (A -> B).
12468 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12470 return isl_map_change_space(set, &isl_set_is_wrapping,
12471 "not a wrapping set", &isl_space_unwrap);
12474 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12475 enum isl_dim_type type)
12477 isl_space *space;
12479 space = isl_basic_map_take_space(bmap);
12480 space = isl_space_reset(space, type);
12481 bmap = isl_basic_map_restore_space(bmap, space);
12483 bmap = isl_basic_map_mark_final(bmap);
12485 return bmap;
12488 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12489 enum isl_dim_type type)
12491 int i;
12492 isl_space *space;
12494 if (!map)
12495 return NULL;
12497 if (!isl_space_is_named_or_nested(map->dim, type))
12498 return map;
12500 map = isl_map_cow(map);
12501 if (!map)
12502 return NULL;
12504 for (i = 0; i < map->n; ++i) {
12505 map->p[i] = isl_basic_map_reset(map->p[i], type);
12506 if (!map->p[i])
12507 goto error;
12510 space = isl_map_take_space(map);
12511 space = isl_space_reset(space, type);
12512 map = isl_map_restore_space(map, space);
12514 return map;
12515 error:
12516 isl_map_free(map);
12517 return NULL;
12520 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12522 isl_space *space;
12524 space = isl_basic_map_take_space(bmap);
12525 space = isl_space_flatten(space);
12526 bmap = isl_basic_map_restore_space(bmap, space);
12528 bmap = isl_basic_map_mark_final(bmap);
12530 return bmap;
12533 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12535 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12538 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12539 __isl_take isl_basic_map *bmap)
12541 isl_space *space;
12543 space = isl_basic_map_take_space(bmap);
12544 space = isl_space_flatten_domain(space);
12545 bmap = isl_basic_map_restore_space(bmap, space);
12547 bmap = isl_basic_map_mark_final(bmap);
12549 return bmap;
12552 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12553 __isl_take isl_basic_map *bmap)
12555 isl_space *space;
12557 space = isl_basic_map_take_space(bmap);
12558 space = isl_space_flatten_range(space);
12559 bmap = isl_basic_map_restore_space(bmap, space);
12561 bmap = isl_basic_map_mark_final(bmap);
12563 return bmap;
12566 /* Remove any internal structure from the spaces of domain and range of "map".
12568 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12570 if (!map)
12571 return NULL;
12573 if (!map->dim->nested[0] && !map->dim->nested[1])
12574 return map;
12576 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12579 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12581 return set_from_map(isl_map_flatten(set_to_map(set)));
12584 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12586 isl_space *space, *flat_space;
12587 isl_map *map;
12589 space = isl_set_get_space(set);
12590 flat_space = isl_space_flatten(isl_space_copy(space));
12591 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12592 flat_space));
12593 map = isl_map_intersect_domain(map, set);
12595 return map;
12598 /* Remove any internal structure from the space of the domain of "map".
12600 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12602 if (!map)
12603 return NULL;
12605 if (!map->dim->nested[0])
12606 return map;
12608 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12611 /* Remove any internal structure from the space of the range of "map".
12613 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12615 if (!map)
12616 return NULL;
12618 if (!map->dim->nested[1])
12619 return map;
12621 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12624 /* Reorder the dimensions of "bmap" according to the given dim_map
12625 * and set the dimension specification to "space" and
12626 * perform Gaussian elimination on the result.
12628 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12629 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12631 isl_basic_map *res;
12632 unsigned flags;
12633 isl_size n_div;
12635 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12636 if (n_div < 0 || !space || !dim_map)
12637 goto error;
12639 flags = bmap->flags;
12640 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12641 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12642 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12643 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12644 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12645 if (res)
12646 res->flags = flags;
12647 res = isl_basic_map_gauss(res, NULL);
12648 res = isl_basic_map_finalize(res);
12649 return res;
12650 error:
12651 isl_dim_map_free(dim_map);
12652 isl_basic_map_free(bmap);
12653 isl_space_free(space);
12654 return NULL;
12657 /* Reorder the dimensions of "map" according to given reordering.
12659 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12660 __isl_take isl_reordering *r)
12662 int i;
12663 struct isl_dim_map *dim_map;
12665 map = isl_map_cow(map);
12666 dim_map = isl_dim_map_from_reordering(r);
12667 if (!map || !r || !dim_map)
12668 goto error;
12670 for (i = 0; i < map->n; ++i) {
12671 struct isl_dim_map *dim_map_i;
12672 isl_space *space;
12674 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12676 space = isl_reordering_get_space(r);
12677 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12679 if (!map->p[i])
12680 goto error;
12683 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12684 map = isl_map_unmark_normalized(map);
12686 isl_reordering_free(r);
12687 isl_dim_map_free(dim_map);
12688 return map;
12689 error:
12690 isl_dim_map_free(dim_map);
12691 isl_map_free(map);
12692 isl_reordering_free(r);
12693 return NULL;
12696 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12697 __isl_take isl_reordering *r)
12699 return set_from_map(isl_map_realign(set_to_map(set), r));
12702 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12703 __isl_take isl_space *model)
12705 isl_ctx *ctx;
12706 isl_bool aligned;
12708 if (!map || !model)
12709 goto error;
12711 ctx = isl_space_get_ctx(model);
12712 if (!isl_space_has_named_params(model))
12713 isl_die(ctx, isl_error_invalid,
12714 "model has unnamed parameters", goto error);
12715 if (isl_map_check_named_params(map) < 0)
12716 goto error;
12717 aligned = isl_map_space_has_equal_params(map, model);
12718 if (aligned < 0)
12719 goto error;
12720 if (!aligned) {
12721 isl_space *space;
12722 isl_reordering *exp;
12724 space = isl_map_peek_space(map);
12725 exp = isl_parameter_alignment_reordering(space, model);
12726 map = isl_map_realign(map, exp);
12729 isl_space_free(model);
12730 return map;
12731 error:
12732 isl_space_free(model);
12733 isl_map_free(map);
12734 return NULL;
12737 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12738 __isl_take isl_space *model)
12740 return isl_map_align_params(set, model);
12743 /* Align the parameters of "bmap" to those of "model", introducing
12744 * additional parameters if needed.
12746 __isl_give isl_basic_map *isl_basic_map_align_params(
12747 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12749 isl_ctx *ctx;
12750 isl_bool equal_params;
12751 isl_space *bmap_space;
12753 if (!bmap || !model)
12754 goto error;
12756 ctx = isl_space_get_ctx(model);
12757 if (!isl_space_has_named_params(model))
12758 isl_die(ctx, isl_error_invalid,
12759 "model has unnamed parameters", goto error);
12760 if (isl_basic_map_check_named_params(bmap) < 0)
12761 goto error;
12762 bmap_space = isl_basic_map_peek_space(bmap);
12763 equal_params = isl_space_has_equal_params(bmap_space, model);
12764 if (equal_params < 0)
12765 goto error;
12766 if (!equal_params) {
12767 isl_reordering *exp;
12768 struct isl_dim_map *dim_map;
12770 exp = isl_parameter_alignment_reordering(bmap_space, model);
12771 dim_map = isl_dim_map_from_reordering(exp);
12772 bmap = isl_basic_map_realign(bmap,
12773 isl_reordering_get_space(exp),
12774 isl_dim_map_extend(dim_map, bmap));
12775 isl_reordering_free(exp);
12776 isl_dim_map_free(dim_map);
12779 isl_space_free(model);
12780 return bmap;
12781 error:
12782 isl_space_free(model);
12783 isl_basic_map_free(bmap);
12784 return NULL;
12787 /* Do "bset" and "space" have the same parameters?
12789 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12790 __isl_keep isl_space *space)
12792 isl_space *bset_space;
12794 bset_space = isl_basic_set_peek_space(bset);
12795 return isl_space_has_equal_params(bset_space, space);
12798 /* Do "map" and "space" have the same parameters?
12800 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12801 __isl_keep isl_space *space)
12803 isl_space *map_space;
12805 map_space = isl_map_peek_space(map);
12806 return isl_space_has_equal_params(map_space, space);
12809 /* Do "set" and "space" have the same parameters?
12811 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12812 __isl_keep isl_space *space)
12814 return isl_map_space_has_equal_params(set_to_map(set), space);
12817 /* Align the parameters of "bset" to those of "model", introducing
12818 * additional parameters if needed.
12820 __isl_give isl_basic_set *isl_basic_set_align_params(
12821 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12823 return isl_basic_map_align_params(bset, model);
12826 #undef TYPE
12827 #define TYPE isl_map
12828 #define isl_map_drop_dims isl_map_drop
12829 #include "isl_drop_unused_params_templ.c"
12831 /* Drop all parameters not referenced by "set".
12833 __isl_give isl_set *isl_set_drop_unused_params(
12834 __isl_take isl_set *set)
12836 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12839 #undef TYPE
12840 #define TYPE isl_basic_map
12841 #define isl_basic_map_drop_dims isl_basic_map_drop
12842 #include "isl_drop_unused_params_templ.c"
12844 /* Drop all parameters not referenced by "bset".
12846 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12847 __isl_take isl_basic_set *bset)
12849 return bset_from_bmap(isl_basic_map_drop_unused_params(
12850 bset_to_bmap(bset)));
12853 /* Given a tuple of identifiers "tuple" in a space that corresponds
12854 * to that of "set", if any of those identifiers appear as parameters
12855 * in "set", then equate those parameters with the corresponding
12856 * set dimensions and project out the parameters.
12857 * The result therefore has no such parameters.
12859 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12860 __isl_keep isl_multi_id *tuple)
12862 int i;
12863 isl_size n;
12864 isl_space *set_space, *tuple_space;
12866 set_space = isl_set_peek_space(set);
12867 tuple_space = isl_multi_id_peek_space(tuple);
12868 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12869 return isl_set_free(set);
12870 n = isl_multi_id_size(tuple);
12871 if (n < 0)
12872 return isl_set_free(set);
12873 for (i = 0; i < n; ++i) {
12874 isl_id *id;
12875 int pos;
12877 id = isl_multi_id_get_at(tuple, i);
12878 if (!id)
12879 return isl_set_free(set);
12880 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12881 isl_id_free(id);
12882 if (pos < 0)
12883 continue;
12884 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12885 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12887 return set;
12890 /* Bind the set dimensions of "set" to parameters with identifiers
12891 * specified by "tuple", living in the same space as "set".
12893 * If no parameters with these identifiers appear in "set" already,
12894 * then the set dimensions are simply reinterpreted as parameters.
12895 * Otherwise, the parameters are first equated to the corresponding
12896 * set dimensions.
12898 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12899 __isl_take isl_multi_id *tuple)
12901 isl_space *space;
12903 set = equate_params(set, tuple);
12904 space = isl_set_get_space(set);
12905 space = isl_space_bind_set(space, tuple);
12906 isl_multi_id_free(tuple);
12907 set = isl_set_reset_space(set, space);
12909 return set;
12912 /* Given a tuple of identifiers "tuple" in a space that corresponds
12913 * to the domain of "map", if any of those identifiers appear as parameters
12914 * in "map", then equate those parameters with the corresponding
12915 * input dimensions and project out the parameters.
12916 * The result therefore has no such parameters.
12918 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12919 __isl_keep isl_multi_id *tuple)
12921 int i;
12922 isl_size n;
12923 isl_space *map_space, *tuple_space;
12925 map_space = isl_map_peek_space(map);
12926 tuple_space = isl_multi_id_peek_space(tuple);
12927 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12928 return isl_map_free(map);
12929 n = isl_multi_id_size(tuple);
12930 if (n < 0)
12931 return isl_map_free(map);
12932 for (i = 0; i < n; ++i) {
12933 isl_id *id;
12934 int pos;
12936 id = isl_multi_id_get_at(tuple, i);
12937 if (!id)
12938 return isl_map_free(map);
12939 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12940 isl_id_free(id);
12941 if (pos < 0)
12942 continue;
12943 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12944 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12946 return map;
12949 /* Bind the input dimensions of "map" to parameters with identifiers
12950 * specified by "tuple", living in the domain space of "map".
12952 * If no parameters with these identifiers appear in "map" already,
12953 * then the input dimensions are simply reinterpreted as parameters.
12954 * Otherwise, the parameters are first equated to the corresponding
12955 * input dimensions.
12957 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12958 __isl_take isl_multi_id *tuple)
12960 isl_space *space;
12961 isl_set *set;
12963 map = map_equate_params(map, tuple);
12964 space = isl_map_get_space(map);
12965 space = isl_space_bind_map_domain(space, tuple);
12966 isl_multi_id_free(tuple);
12967 set = set_from_map(isl_map_reset_space(map, space));
12969 return set;
12972 /* Bind the output dimensions of "map" to parameters with identifiers
12973 * specified by "tuple", living in the range space of "map".
12975 * Since binding is more easily implemented on the domain,
12976 * bind the input dimensions of the inverse of "map".
12978 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12979 __isl_take isl_multi_id *tuple)
12981 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12984 /* Insert a domain corresponding to "tuple"
12985 * into the nullary or unary relation "set".
12986 * The result has an extra initial tuple and is therefore
12987 * either a unary or binary relation.
12988 * Any parameters with identifiers in "tuple" are reinterpreted
12989 * as the corresponding domain dimensions.
12991 static __isl_give isl_map *unbind_params_insert_domain(
12992 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12994 isl_space *space;
12995 isl_reordering *r;
12997 space = isl_set_peek_space(set);
12998 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12999 isl_multi_id_free(tuple);
13001 return isl_map_realign(set_to_map(set), r);
13004 /* Construct a set with "tuple" as domain from the parameter domain "set".
13005 * Any parameters with identifiers in "tuple" are reinterpreted
13006 * as the corresponding set dimensions.
13008 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
13009 __isl_take isl_multi_id *tuple)
13011 isl_bool is_params;
13013 is_params = isl_set_is_params(set);
13014 if (is_params < 0)
13015 set = isl_set_free(set);
13016 else if (!is_params)
13017 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13018 "expecting parameter domain", set = isl_set_free(set));
13019 return set_from_map(unbind_params_insert_domain(set, tuple));
13022 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13024 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
13026 isl_bool is_params;
13028 is_params = isl_set_is_params(set);
13029 if (is_params < 0)
13030 return isl_stat_error;
13031 else if (is_params)
13032 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13033 "expecting proper set", return isl_stat_error);
13035 return isl_stat_ok;
13038 /* Construct a map with "domain" as domain and "set" as range.
13039 * Any parameters with identifiers in "domain" are reinterpreted
13040 * as the corresponding domain dimensions.
13042 __isl_give isl_map *isl_set_unbind_params_insert_domain(
13043 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
13045 if (isl_set_check_is_set(set) < 0)
13046 set = isl_set_free(set);
13047 return unbind_params_insert_domain(set, domain);
13050 /* Construct a map with "domain" as domain and "set" as range.
13052 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
13053 __isl_take isl_space *domain)
13055 isl_size dim;
13056 isl_space *space;
13057 isl_map *map;
13059 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
13060 domain = isl_space_free(domain);
13061 dim = isl_space_dim(domain, isl_dim_set);
13062 if (dim < 0)
13063 domain = isl_space_free(domain);
13064 space = isl_set_get_space(set);
13065 domain = isl_space_replace_params(domain, space);
13066 space = isl_space_map_from_domain_and_range(domain, space);
13068 map = isl_map_from_range(set);
13069 map = isl_map_add_dims(map, isl_dim_in, dim);
13070 map = isl_map_reset_space(map, space);
13072 return map;
13075 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13076 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13077 enum isl_dim_type c2, enum isl_dim_type c3,
13078 enum isl_dim_type c4, enum isl_dim_type c5)
13080 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13081 struct isl_mat *mat;
13082 int i, j, k;
13083 int pos;
13084 isl_size total;
13086 total = isl_basic_map_dim(bmap, isl_dim_all);
13087 if (total < 0)
13088 return NULL;
13089 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13090 if (!mat)
13091 return NULL;
13092 for (i = 0; i < bmap->n_eq; ++i)
13093 for (j = 0, pos = 0; j < 5; ++j) {
13094 int off = isl_basic_map_offset(bmap, c[j]);
13095 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13096 if (dim < 0)
13097 return isl_mat_free(mat);
13098 for (k = 0; k < dim; ++k) {
13099 isl_int_set(mat->row[i][pos],
13100 bmap->eq[i][off + k]);
13101 ++pos;
13105 return mat;
13108 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13109 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13110 enum isl_dim_type c2, enum isl_dim_type c3,
13111 enum isl_dim_type c4, enum isl_dim_type c5)
13113 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13114 struct isl_mat *mat;
13115 int i, j, k;
13116 int pos;
13117 isl_size total;
13119 total = isl_basic_map_dim(bmap, isl_dim_all);
13120 if (total < 0)
13121 return NULL;
13122 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13123 if (!mat)
13124 return NULL;
13125 for (i = 0; i < bmap->n_ineq; ++i)
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 return isl_mat_free(mat);
13131 for (k = 0; k < dim; ++k) {
13132 isl_int_set(mat->row[i][pos],
13133 bmap->ineq[i][off + k]);
13134 ++pos;
13138 return mat;
13141 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13142 __isl_take isl_space *space,
13143 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13144 enum isl_dim_type c2, enum isl_dim_type c3,
13145 enum isl_dim_type c4, enum isl_dim_type c5)
13147 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13148 isl_basic_map *bmap = NULL;
13149 isl_size dim;
13150 unsigned total;
13151 unsigned extra;
13152 int i, j, k, l;
13153 int pos;
13155 dim = isl_space_dim(space, isl_dim_all);
13156 if (dim < 0 || !eq || !ineq)
13157 goto error;
13159 if (eq->n_col != ineq->n_col)
13160 isl_die(space->ctx, isl_error_invalid,
13161 "equalities and inequalities matrices should have "
13162 "same number of columns", goto error);
13164 total = 1 + dim;
13166 if (eq->n_col < total)
13167 isl_die(space->ctx, isl_error_invalid,
13168 "number of columns too small", goto error);
13170 extra = eq->n_col - total;
13172 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13173 eq->n_row, ineq->n_row);
13174 if (!bmap)
13175 goto error;
13176 for (i = 0; i < extra; ++i) {
13177 k = isl_basic_map_alloc_div(bmap);
13178 if (k < 0)
13179 goto error;
13180 isl_int_set_si(bmap->div[k][0], 0);
13182 for (i = 0; i < eq->n_row; ++i) {
13183 l = isl_basic_map_alloc_equality(bmap);
13184 if (l < 0)
13185 goto error;
13186 for (j = 0, pos = 0; j < 5; ++j) {
13187 int off = isl_basic_map_offset(bmap, c[j]);
13188 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13189 if (dim < 0)
13190 goto error;
13191 for (k = 0; k < dim; ++k) {
13192 isl_int_set(bmap->eq[l][off + k],
13193 eq->row[i][pos]);
13194 ++pos;
13198 for (i = 0; i < ineq->n_row; ++i) {
13199 l = isl_basic_map_alloc_inequality(bmap);
13200 if (l < 0)
13201 goto error;
13202 for (j = 0, pos = 0; j < 5; ++j) {
13203 int off = isl_basic_map_offset(bmap, c[j]);
13204 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13205 if (dim < 0)
13206 goto error;
13207 for (k = 0; k < dim; ++k) {
13208 isl_int_set(bmap->ineq[l][off + k],
13209 ineq->row[i][pos]);
13210 ++pos;
13215 isl_space_free(space);
13216 isl_mat_free(eq);
13217 isl_mat_free(ineq);
13219 bmap = isl_basic_map_simplify(bmap);
13220 return isl_basic_map_finalize(bmap);
13221 error:
13222 isl_space_free(space);
13223 isl_mat_free(eq);
13224 isl_mat_free(ineq);
13225 isl_basic_map_free(bmap);
13226 return NULL;
13229 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13230 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13231 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13233 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13234 c1, c2, c3, c4, isl_dim_in);
13237 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13238 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13239 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13241 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13242 c1, c2, c3, c4, isl_dim_in);
13245 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13246 __isl_take isl_space *space,
13247 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13248 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13250 isl_basic_map *bmap;
13251 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13252 c1, c2, c3, c4, isl_dim_in);
13253 return bset_from_bmap(bmap);
13256 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13258 if (!bmap)
13259 return isl_bool_error;
13261 return isl_space_can_zip(bmap->dim);
13264 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13266 if (!map)
13267 return isl_bool_error;
13269 return isl_space_can_zip(map->dim);
13272 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13273 * (A -> C) -> (B -> D).
13275 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13277 unsigned pos;
13278 isl_size n_in;
13279 isl_size n1;
13280 isl_size n2;
13282 if (!bmap)
13283 return NULL;
13285 if (!isl_basic_map_can_zip(bmap))
13286 isl_die(bmap->ctx, isl_error_invalid,
13287 "basic map cannot be zipped", goto error);
13288 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13289 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13290 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13291 if (n_in < 0 || n1 < 0 || n2 < 0)
13292 return isl_basic_map_free(bmap);
13293 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13294 bmap = isl_basic_map_cow(bmap);
13295 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13296 if (!bmap)
13297 return NULL;
13298 bmap->dim = isl_space_zip(bmap->dim);
13299 if (!bmap->dim)
13300 goto error;
13301 bmap = isl_basic_map_mark_final(bmap);
13302 return bmap;
13303 error:
13304 isl_basic_map_free(bmap);
13305 return NULL;
13308 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13309 * (A -> C) -> (B -> D).
13311 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13313 if (!map)
13314 return NULL;
13316 if (!isl_map_can_zip(map))
13317 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13318 goto error);
13320 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13321 error:
13322 isl_map_free(map);
13323 return NULL;
13326 /* Can we apply isl_basic_map_curry to "bmap"?
13327 * That is, does it have a nested relation in its domain?
13329 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13331 if (!bmap)
13332 return isl_bool_error;
13334 return isl_space_can_curry(bmap->dim);
13337 /* Can we apply isl_map_curry to "map"?
13338 * That is, does it have a nested relation in its domain?
13340 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13342 if (!map)
13343 return isl_bool_error;
13345 return isl_space_can_curry(map->dim);
13348 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13349 * A -> (B -> C).
13351 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13354 if (!bmap)
13355 return NULL;
13357 if (!isl_basic_map_can_curry(bmap))
13358 isl_die(bmap->ctx, isl_error_invalid,
13359 "basic map cannot be curried", goto error);
13360 bmap = isl_basic_map_cow(bmap);
13361 if (!bmap)
13362 return NULL;
13363 bmap->dim = isl_space_curry(bmap->dim);
13364 if (!bmap->dim)
13365 goto error;
13366 bmap = isl_basic_map_mark_final(bmap);
13367 return bmap;
13368 error:
13369 isl_basic_map_free(bmap);
13370 return NULL;
13373 /* Given a map (A -> B) -> C, return the corresponding map
13374 * A -> (B -> C).
13376 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13378 return isl_map_change_space(map, &isl_map_can_curry,
13379 "map cannot be curried", &isl_space_curry);
13382 /* Can isl_map_range_curry be applied to "map"?
13383 * That is, does it have a nested relation in its range,
13384 * the domain of which is itself a nested relation?
13386 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13388 if (!map)
13389 return isl_bool_error;
13391 return isl_space_can_range_curry(map->dim);
13394 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13395 * A -> (B -> (C -> D)).
13397 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13399 return isl_map_change_space(map, &isl_map_can_range_curry,
13400 "map range cannot be curried",
13401 &isl_space_range_curry);
13404 /* Can we apply isl_basic_map_uncurry to "bmap"?
13405 * That is, does it have a nested relation in its domain?
13407 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13409 if (!bmap)
13410 return isl_bool_error;
13412 return isl_space_can_uncurry(bmap->dim);
13415 /* Can we apply isl_map_uncurry to "map"?
13416 * That is, does it have a nested relation in its domain?
13418 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13420 if (!map)
13421 return isl_bool_error;
13423 return isl_space_can_uncurry(map->dim);
13426 /* Given a basic map A -> (B -> C), return the corresponding basic map
13427 * (A -> B) -> C.
13429 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13432 if (!bmap)
13433 return NULL;
13435 if (!isl_basic_map_can_uncurry(bmap))
13436 isl_die(bmap->ctx, isl_error_invalid,
13437 "basic map cannot be uncurried",
13438 return isl_basic_map_free(bmap));
13439 bmap = isl_basic_map_cow(bmap);
13440 if (!bmap)
13441 return NULL;
13442 bmap->dim = isl_space_uncurry(bmap->dim);
13443 if (!bmap->dim)
13444 return isl_basic_map_free(bmap);
13445 bmap = isl_basic_map_mark_final(bmap);
13446 return bmap;
13449 /* Given a map A -> (B -> C), return the corresponding map
13450 * (A -> B) -> C.
13452 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13454 return isl_map_change_space(map, &isl_map_can_uncurry,
13455 "map cannot be uncurried", &isl_space_uncurry);
13458 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13459 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13461 return isl_map_equate(set, type1, pos1, type2, pos2);
13464 /* Construct a basic map where the given dimensions are equal to each other.
13466 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13467 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13469 isl_basic_map *bmap = NULL;
13470 int i;
13471 isl_size total;
13473 total = isl_space_dim(space, isl_dim_all);
13474 if (total < 0 ||
13475 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13476 isl_space_check_range(space, type2, pos2, 1) < 0)
13477 goto error;
13479 if (type1 == type2 && pos1 == pos2)
13480 return isl_basic_map_universe(space);
13482 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13483 i = isl_basic_map_alloc_equality(bmap);
13484 if (i < 0)
13485 goto error;
13486 isl_seq_clr(bmap->eq[i], 1 + total);
13487 pos1 += isl_basic_map_offset(bmap, type1);
13488 pos2 += isl_basic_map_offset(bmap, type2);
13489 isl_int_set_si(bmap->eq[i][pos1], -1);
13490 isl_int_set_si(bmap->eq[i][pos2], 1);
13491 bmap = isl_basic_map_finalize(bmap);
13492 isl_space_free(space);
13493 return bmap;
13494 error:
13495 isl_space_free(space);
13496 isl_basic_map_free(bmap);
13497 return NULL;
13500 /* Add a constraint imposing that the given two dimensions are equal.
13502 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13503 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13505 isl_basic_map *eq;
13507 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13509 bmap = isl_basic_map_intersect(bmap, eq);
13511 return bmap;
13514 /* Add a constraint imposing that the given two dimensions are equal.
13516 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13517 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13519 isl_basic_map *bmap;
13521 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13523 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13525 return map;
13528 /* Add a constraint imposing that the given two dimensions have opposite values.
13530 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13531 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13533 isl_basic_map *bmap = NULL;
13534 int i;
13535 isl_size total;
13537 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13538 return isl_map_free(map);
13539 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13540 return isl_map_free(map);
13542 total = isl_map_dim(map, isl_dim_all);
13543 if (total < 0)
13544 return isl_map_free(map);
13545 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13546 i = isl_basic_map_alloc_equality(bmap);
13547 if (i < 0)
13548 goto error;
13549 isl_seq_clr(bmap->eq[i], 1 + total);
13550 pos1 += isl_basic_map_offset(bmap, type1);
13551 pos2 += isl_basic_map_offset(bmap, type2);
13552 isl_int_set_si(bmap->eq[i][pos1], 1);
13553 isl_int_set_si(bmap->eq[i][pos2], 1);
13554 bmap = isl_basic_map_finalize(bmap);
13556 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13558 return map;
13559 error:
13560 isl_basic_map_free(bmap);
13561 isl_map_free(map);
13562 return NULL;
13565 /* Construct a constraint imposing that the value of the first dimension is
13566 * greater than or equal to that of the second.
13568 static __isl_give isl_constraint *constraint_order_ge(
13569 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13570 enum isl_dim_type type2, int pos2)
13572 isl_constraint *c;
13574 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13575 isl_space_check_range(space, type2, pos2, 1) < 0)
13576 space = isl_space_free(space);
13577 if (!space)
13578 return NULL;
13580 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13582 if (type1 == type2 && pos1 == pos2)
13583 return c;
13585 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13586 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13588 return c;
13591 /* Add a constraint imposing that the value of the first dimension is
13592 * greater than or equal to that of the second.
13594 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13595 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13597 isl_constraint *c;
13598 isl_space *space;
13600 if (type1 == type2 && pos1 == pos2)
13601 return bmap;
13602 space = isl_basic_map_get_space(bmap);
13603 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13604 bmap = isl_basic_map_add_constraint(bmap, c);
13606 return bmap;
13609 /* Add a constraint imposing that the value of the first dimension is
13610 * greater than or equal to that of the second.
13612 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13613 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13615 isl_constraint *c;
13616 isl_space *space;
13618 if (type1 == type2 && pos1 == pos2)
13619 return map;
13620 space = isl_map_get_space(map);
13621 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13622 map = isl_map_add_constraint(map, c);
13624 return map;
13627 /* Add a constraint imposing that the value of the first dimension is
13628 * less than or equal to that of the second.
13630 __isl_give isl_map *isl_map_order_le(__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_ge(map, type2, pos2, type1, pos1);
13636 /* Construct a basic map where the value of the first dimension is
13637 * greater than that of the second.
13639 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13640 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13642 isl_basic_map *bmap = NULL;
13643 int i;
13644 isl_size total;
13646 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13647 isl_space_check_range(space, type2, pos2, 1) < 0)
13648 goto error;
13650 if (type1 == type2 && pos1 == pos2)
13651 return isl_basic_map_empty(space);
13653 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13654 total = isl_basic_map_dim(bmap, isl_dim_all);
13655 i = isl_basic_map_alloc_inequality(bmap);
13656 if (total < 0 || i < 0)
13657 return isl_basic_map_free(bmap);
13658 isl_seq_clr(bmap->ineq[i], 1 + total);
13659 pos1 += isl_basic_map_offset(bmap, type1);
13660 pos2 += isl_basic_map_offset(bmap, type2);
13661 isl_int_set_si(bmap->ineq[i][pos1], 1);
13662 isl_int_set_si(bmap->ineq[i][pos2], -1);
13663 isl_int_set_si(bmap->ineq[i][0], -1);
13664 bmap = isl_basic_map_finalize(bmap);
13666 return bmap;
13667 error:
13668 isl_space_free(space);
13669 isl_basic_map_free(bmap);
13670 return NULL;
13673 /* Add a constraint imposing that the value of the first dimension is
13674 * greater than that of the second.
13676 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13677 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13679 isl_basic_map *gt;
13681 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13683 bmap = isl_basic_map_intersect(bmap, gt);
13685 return bmap;
13688 /* Add a constraint imposing that the value of the first dimension is
13689 * greater than that of the second.
13691 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13692 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13694 isl_basic_map *bmap;
13696 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13698 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13700 return map;
13703 /* Add a constraint imposing that the value of the first dimension is
13704 * smaller than that of the second.
13706 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13707 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13709 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13712 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13713 int pos)
13715 isl_aff *div;
13716 isl_local_space *ls;
13718 if (!bmap)
13719 return NULL;
13721 if (!isl_basic_map_divs_known(bmap))
13722 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13723 "some divs are unknown", return NULL);
13725 ls = isl_basic_map_get_local_space(bmap);
13726 div = isl_local_space_get_div(ls, pos);
13727 isl_local_space_free(ls);
13729 return div;
13732 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13733 int pos)
13735 return isl_basic_map_get_div(bset, pos);
13738 /* Plug in "subs" for set dimension "pos" of "set".
13740 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13741 unsigned pos, __isl_keep isl_aff *subs)
13743 isl_multi_aff *ma;
13745 if (set && isl_set_plain_is_empty(set))
13746 return set;
13748 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13749 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13750 return isl_set_preimage_multi_aff(set, ma);
13753 /* Check if the range of "ma" is compatible with the domain or range
13754 * (depending on "type") of "bmap".
13756 static isl_stat check_basic_map_compatible_range_multi_aff(
13757 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13758 __isl_keep isl_multi_aff *ma)
13760 isl_bool m;
13761 isl_space *ma_space;
13763 ma_space = isl_multi_aff_get_space(ma);
13765 m = isl_space_has_equal_params(bmap->dim, ma_space);
13766 if (m < 0)
13767 goto error;
13768 if (!m)
13769 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13770 "parameters don't match", goto error);
13771 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13772 if (m < 0)
13773 goto error;
13774 if (!m)
13775 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13776 "spaces don't match", goto error);
13778 isl_space_free(ma_space);
13779 return isl_stat_ok;
13780 error:
13781 isl_space_free(ma_space);
13782 return isl_stat_error;
13785 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13786 * coefficients before the transformed range of dimensions,
13787 * the "n_after" coefficients after the transformed range of dimensions
13788 * and the coefficients of the other divs in "bmap".
13790 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13791 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13793 int i;
13794 isl_size n_param;
13795 isl_size n_set;
13796 isl_local_space *ls;
13798 if (n_div == 0)
13799 return bmap;
13801 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13802 n_param = isl_local_space_dim(ls, isl_dim_param);
13803 n_set = isl_local_space_dim(ls, isl_dim_set);
13804 if (n_param < 0 || n_set < 0)
13805 return isl_basic_map_free(bmap);
13807 for (i = 0; i < n_div; ++i) {
13808 int o_bmap = 0, o_ls = 0;
13810 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13811 o_bmap += 1 + 1 + n_param;
13812 o_ls += 1 + 1 + n_param;
13813 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13814 o_bmap += n_before;
13815 isl_seq_cpy(bmap->div[i] + o_bmap,
13816 ls->div->row[i] + o_ls, n_set);
13817 o_bmap += n_set;
13818 o_ls += n_set;
13819 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13820 o_bmap += n_after;
13821 isl_seq_cpy(bmap->div[i] + o_bmap,
13822 ls->div->row[i] + o_ls, n_div);
13823 o_bmap += n_div;
13824 o_ls += n_div;
13825 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13826 bmap = isl_basic_map_add_div_constraints(bmap, i);
13827 if (!bmap)
13828 goto error;
13831 isl_local_space_free(ls);
13832 return bmap;
13833 error:
13834 isl_local_space_free(ls);
13835 return isl_basic_map_free(bmap);
13838 /* How many stride constraints does "ma" enforce?
13839 * That is, how many of the affine expressions have a denominator
13840 * different from one?
13842 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13844 int i;
13845 int strides = 0;
13847 for (i = 0; i < ma->n; ++i)
13848 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13849 strides++;
13851 return strides;
13854 /* For each affine expression in ma of the form
13856 * x_i = (f_i y + h_i)/m_i
13858 * with m_i different from one, add a constraint to "bmap"
13859 * of the form
13861 * f_i y + h_i = m_i alpha_i
13863 * with alpha_i an additional existentially quantified variable.
13865 * The input variables of "ma" correspond to a subset of the variables
13866 * of "bmap". There are "n_before" variables in "bmap" before this
13867 * subset and "n_after" variables after this subset.
13868 * The integer divisions of the affine expressions in "ma" are assumed
13869 * to have been aligned. There are "n_div_ma" of them and
13870 * they appear first in "bmap", straight after the "n_after" variables.
13872 static __isl_give isl_basic_map *add_ma_strides(
13873 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13874 int n_before, int n_after, int n_div_ma)
13876 int i, k;
13877 int div;
13878 isl_size total;
13879 isl_size n_param;
13880 isl_size n_in;
13882 total = isl_basic_map_dim(bmap, isl_dim_all);
13883 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13884 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13885 if (total < 0 || n_param < 0 || n_in < 0)
13886 return isl_basic_map_free(bmap);
13887 for (i = 0; i < ma->n; ++i) {
13888 int o_bmap = 0, o_ma = 1;
13890 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13891 continue;
13892 div = isl_basic_map_alloc_div(bmap);
13893 k = isl_basic_map_alloc_equality(bmap);
13894 if (div < 0 || k < 0)
13895 goto error;
13896 isl_int_set_si(bmap->div[div][0], 0);
13897 isl_seq_cpy(bmap->eq[k] + o_bmap,
13898 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13899 o_bmap += 1 + n_param;
13900 o_ma += 1 + n_param;
13901 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13902 o_bmap += n_before;
13903 isl_seq_cpy(bmap->eq[k] + o_bmap,
13904 ma->u.p[i]->v->el + o_ma, n_in);
13905 o_bmap += n_in;
13906 o_ma += n_in;
13907 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13908 o_bmap += n_after;
13909 isl_seq_cpy(bmap->eq[k] + o_bmap,
13910 ma->u.p[i]->v->el + o_ma, n_div_ma);
13911 o_bmap += n_div_ma;
13912 o_ma += n_div_ma;
13913 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13914 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13915 total++;
13918 return bmap;
13919 error:
13920 isl_basic_map_free(bmap);
13921 return NULL;
13924 /* Replace the domain or range space (depending on "type) of "space" by "set".
13926 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13927 enum isl_dim_type type, __isl_take isl_space *set)
13929 if (type == isl_dim_in) {
13930 space = isl_space_range(space);
13931 space = isl_space_map_from_domain_and_range(set, space);
13932 } else {
13933 space = isl_space_domain(space);
13934 space = isl_space_map_from_domain_and_range(space, set);
13937 return space;
13940 /* Compute the preimage of the domain or range (depending on "type")
13941 * of "bmap" under the function represented by "ma".
13942 * In other words, plug in "ma" in the domain or range of "bmap".
13943 * The result is a basic map that lives in the same space as "bmap"
13944 * except that the domain or range has been replaced by
13945 * the domain space of "ma".
13947 * If bmap is represented by
13949 * A(p) + S u + B x + T v + C(divs) >= 0,
13951 * where u and x are input and output dimensions if type == isl_dim_out
13952 * while x and v are input and output dimensions if type == isl_dim_in,
13953 * and ma is represented by
13955 * x = D(p) + F(y) + G(divs')
13957 * then the result is
13959 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13961 * The divs in the input set are similarly adjusted.
13962 * In particular
13964 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13966 * becomes
13968 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13969 * B_i G(divs') + c_i(divs))/n_i)
13971 * If bmap is not a rational map and if F(y) involves any denominators
13973 * x_i = (f_i y + h_i)/m_i
13975 * then additional constraints are added to ensure that we only
13976 * map back integer points. That is we enforce
13978 * f_i y + h_i = m_i alpha_i
13980 * with alpha_i an additional existentially quantified variable.
13982 * We first copy over the divs from "ma".
13983 * Then we add the modified constraints and divs from "bmap".
13984 * Finally, we add the stride constraints, if needed.
13986 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13987 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13988 __isl_take isl_multi_aff *ma)
13990 int i, k;
13991 isl_space *space;
13992 isl_basic_map *res = NULL;
13993 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13994 isl_int f, c1, c2, g;
13995 isl_bool rational;
13996 int strides;
13998 isl_int_init(f);
13999 isl_int_init(c1);
14000 isl_int_init(c2);
14001 isl_int_init(g);
14003 ma = isl_multi_aff_align_divs(ma);
14004 if (!bmap || !ma)
14005 goto error;
14006 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
14007 goto error;
14009 if (type == isl_dim_in) {
14010 n_before = 0;
14011 n_after = isl_basic_map_dim(bmap, isl_dim_out);
14012 } else {
14013 n_before = isl_basic_map_dim(bmap, isl_dim_in);
14014 n_after = 0;
14016 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
14017 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
14018 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
14019 goto error;
14021 space = isl_multi_aff_get_domain_space(ma);
14022 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
14023 rational = isl_basic_map_is_rational(bmap);
14024 strides = rational ? 0 : multi_aff_strides(ma);
14025 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
14026 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
14027 if (rational)
14028 res = isl_basic_map_set_rational(res);
14030 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
14031 if (isl_basic_map_alloc_div(res) < 0)
14032 goto error;
14034 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
14035 if (!res)
14036 goto error;
14038 for (i = 0; i < bmap->n_eq; ++i) {
14039 k = isl_basic_map_alloc_equality(res);
14040 if (k < 0)
14041 goto error;
14042 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
14043 n_after, n_div_ma, n_div_bmap,
14044 f, c1, c2, g, 0) < 0)
14045 goto error;
14048 for (i = 0; i < bmap->n_ineq; ++i) {
14049 k = isl_basic_map_alloc_inequality(res);
14050 if (k < 0)
14051 goto error;
14052 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
14053 n_after, n_div_ma, n_div_bmap,
14054 f, c1, c2, g, 0) < 0)
14055 goto error;
14058 for (i = 0; i < bmap->n_div; ++i) {
14059 if (isl_int_is_zero(bmap->div[i][0])) {
14060 isl_int_set_si(res->div[n_div_ma + i][0], 0);
14061 continue;
14063 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
14064 n_before, n_after, n_div_ma, n_div_bmap,
14065 f, c1, c2, g, 1) < 0)
14066 goto error;
14069 if (strides)
14070 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
14072 isl_int_clear(f);
14073 isl_int_clear(c1);
14074 isl_int_clear(c2);
14075 isl_int_clear(g);
14076 isl_basic_map_free(bmap);
14077 isl_multi_aff_free(ma);
14078 res = isl_basic_map_simplify(res);
14079 return isl_basic_map_finalize(res);
14080 error:
14081 isl_int_clear(f);
14082 isl_int_clear(c1);
14083 isl_int_clear(c2);
14084 isl_int_clear(g);
14085 isl_basic_map_free(bmap);
14086 isl_multi_aff_free(ma);
14087 isl_basic_map_free(res);
14088 return NULL;
14091 /* Compute the preimage of "bset" under the function represented by "ma".
14092 * In other words, plug in "ma" in "bset". The result is a basic set
14093 * that lives in the domain space of "ma".
14095 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14096 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14098 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14101 /* Compute the preimage of the domain of "bmap" under the function
14102 * represented by "ma".
14103 * In other words, plug in "ma" in the domain of "bmap".
14104 * The result is a basic map that lives in the same space as "bmap"
14105 * except that the domain has been replaced by the domain space of "ma".
14107 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14108 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14110 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14113 /* Compute the preimage of the range of "bmap" under the function
14114 * represented by "ma".
14115 * In other words, plug in "ma" in the range of "bmap".
14116 * The result is a basic map that lives in the same space as "bmap"
14117 * except that the range has been replaced by the domain space of "ma".
14119 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14120 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14122 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14125 /* Check if the range of "ma" is compatible with the domain or range
14126 * (depending on "type") of "map".
14127 * Return isl_stat_error if anything is wrong.
14129 static isl_stat check_map_compatible_range_multi_aff(
14130 __isl_keep isl_map *map, enum isl_dim_type type,
14131 __isl_keep isl_multi_aff *ma)
14133 isl_bool m;
14134 isl_space *ma_space;
14136 ma_space = isl_multi_aff_get_space(ma);
14137 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14138 isl_space_free(ma_space);
14139 if (m < 0)
14140 return isl_stat_error;
14141 if (!m)
14142 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14143 "spaces don't match", return isl_stat_error);
14144 return isl_stat_ok;
14147 /* Compute the preimage of the domain or range (depending on "type")
14148 * of "map" under the function represented by "ma".
14149 * In other words, plug in "ma" in the domain or range of "map".
14150 * The result is a map that lives in the same space as "map"
14151 * except that the domain or range has been replaced by
14152 * the domain space of "ma".
14154 * The parameters are assumed to have been aligned.
14156 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14157 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14159 int i;
14160 isl_space *space;
14162 map = isl_map_cow(map);
14163 ma = isl_multi_aff_align_divs(ma);
14164 if (!map || !ma)
14165 goto error;
14166 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14167 goto error;
14169 for (i = 0; i < map->n; ++i) {
14170 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14171 isl_multi_aff_copy(ma));
14172 if (!map->p[i])
14173 goto error;
14176 space = isl_multi_aff_get_domain_space(ma);
14177 space = isl_space_set(isl_map_get_space(map), type, space);
14179 isl_space_free(isl_map_take_space(map));
14180 map = isl_map_restore_space(map, space);
14181 if (!map)
14182 goto error;
14184 isl_multi_aff_free(ma);
14185 if (map->n > 1)
14186 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14187 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14188 return map;
14189 error:
14190 isl_multi_aff_free(ma);
14191 isl_map_free(map);
14192 return NULL;
14195 /* Compute the preimage of the domain or range (depending on "type")
14196 * of "map" under the function represented by "ma".
14197 * In other words, plug in "ma" in the domain or range of "map".
14198 * The result is a map that lives in the same space as "map"
14199 * except that the domain or range has been replaced by
14200 * the domain space of "ma".
14202 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14203 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14205 isl_bool aligned;
14207 if (!map || !ma)
14208 goto error;
14210 aligned = isl_map_space_has_equal_params(map, ma->space);
14211 if (aligned < 0)
14212 goto error;
14213 if (aligned)
14214 return map_preimage_multi_aff(map, type, ma);
14216 if (isl_map_check_named_params(map) < 0)
14217 goto error;
14218 if (!isl_space_has_named_params(ma->space))
14219 isl_die(map->ctx, isl_error_invalid,
14220 "unaligned unnamed parameters", goto error);
14221 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14222 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14224 return map_preimage_multi_aff(map, type, ma);
14225 error:
14226 isl_multi_aff_free(ma);
14227 return isl_map_free(map);
14230 /* Compute the preimage of "set" under the function represented by "ma".
14231 * In other words, plug in "ma" in "set". The result is a set
14232 * that lives in the domain space of "ma".
14234 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14235 __isl_take isl_multi_aff *ma)
14237 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14240 /* Compute the preimage of the domain of "map" under the function
14241 * represented by "ma".
14242 * In other words, plug in "ma" in the domain of "map".
14243 * The result is a map that lives in the same space as "map"
14244 * except that the domain has been replaced by the domain space of "ma".
14246 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14247 __isl_take isl_multi_aff *ma)
14249 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14252 /* Compute the preimage of the range of "map" under the function
14253 * represented by "ma".
14254 * In other words, plug in "ma" in the range of "map".
14255 * The result is a map that lives in the same space as "map"
14256 * except that the range has been replaced by the domain space of "ma".
14258 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14259 __isl_take isl_multi_aff *ma)
14261 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14264 /* Compute the preimage of "map" under the function represented by "pma".
14265 * In other words, plug in "pma" in the domain or range of "map".
14266 * The result is a map that lives in the same space as "map",
14267 * except that the space of type "type" has been replaced by
14268 * the domain space of "pma".
14270 * The parameters of "map" and "pma" are assumed to have been aligned.
14272 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14273 __isl_take isl_map *map, enum isl_dim_type type,
14274 __isl_take isl_pw_multi_aff *pma)
14276 int i;
14277 isl_map *res;
14279 if (!pma)
14280 goto error;
14282 if (pma->n == 0) {
14283 isl_space *space;
14285 space = isl_pw_multi_aff_get_domain_space(pma);
14286 isl_pw_multi_aff_free(pma);
14287 space = isl_space_set(isl_map_get_space(map), type, space);
14288 isl_map_free(map);
14289 return isl_map_empty(space);
14292 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14293 isl_multi_aff_copy(pma->p[0].maff));
14294 if (type == isl_dim_in)
14295 res = isl_map_intersect_domain(res,
14296 isl_map_copy(pma->p[0].set));
14297 else
14298 res = isl_map_intersect_range(res,
14299 isl_map_copy(pma->p[0].set));
14301 for (i = 1; i < pma->n; ++i) {
14302 isl_map *res_i;
14304 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14305 isl_multi_aff_copy(pma->p[i].maff));
14306 if (type == isl_dim_in)
14307 res_i = isl_map_intersect_domain(res_i,
14308 isl_map_copy(pma->p[i].set));
14309 else
14310 res_i = isl_map_intersect_range(res_i,
14311 isl_map_copy(pma->p[i].set));
14312 res = isl_map_union(res, res_i);
14315 isl_pw_multi_aff_free(pma);
14316 isl_map_free(map);
14317 return res;
14318 error:
14319 isl_pw_multi_aff_free(pma);
14320 isl_map_free(map);
14321 return NULL;
14324 /* Compute the preimage of "map" under the function represented by "pma".
14325 * In other words, plug in "pma" in the domain or range of "map".
14326 * The result is a map that lives in the same space as "map",
14327 * except that the space of type "type" has been replaced by
14328 * the domain space of "pma".
14330 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14331 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14333 isl_bool aligned;
14335 if (!map || !pma)
14336 goto error;
14338 aligned = isl_map_space_has_equal_params(map, pma->dim);
14339 if (aligned < 0)
14340 goto error;
14341 if (aligned)
14342 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14344 if (isl_map_check_named_params(map) < 0)
14345 goto error;
14346 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14347 goto error;
14348 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14349 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14351 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14352 error:
14353 isl_pw_multi_aff_free(pma);
14354 return isl_map_free(map);
14357 /* Compute the preimage of "set" under the function represented by "pma".
14358 * In other words, plug in "pma" in "set". The result is a set
14359 * that lives in the domain space of "pma".
14361 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14362 __isl_take isl_pw_multi_aff *pma)
14364 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14367 /* Compute the preimage of the domain of "map" under the function
14368 * represented by "pma".
14369 * In other words, plug in "pma" in the domain of "map".
14370 * The result is a map that lives in the same space as "map",
14371 * except that domain space has been replaced by the domain space of "pma".
14373 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14374 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14376 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14379 /* Compute the preimage of the range of "map" under the function
14380 * represented by "pma".
14381 * In other words, plug in "pma" in the range of "map".
14382 * The result is a map that lives in the same space as "map",
14383 * except that range space has been replaced by the domain space of "pma".
14385 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14386 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14388 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14391 /* Compute the preimage of "map" under the function represented by "mpa".
14392 * In other words, plug in "mpa" in the domain or range of "map".
14393 * The result is a map that lives in the same space as "map",
14394 * except that the space of type "type" has been replaced by
14395 * the domain space of "mpa".
14397 * If the map does not involve any constraints that refer to the
14398 * dimensions of the substituted space, then the only possible
14399 * effect of "mpa" on the map is to map the space to a different space.
14400 * We create a separate isl_multi_aff to effectuate this change
14401 * in order to avoid spurious splitting of the map along the pieces
14402 * of "mpa".
14403 * If "mpa" has a non-trivial explicit domain, however,
14404 * then the full substitution should be performed.
14406 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14407 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14409 isl_size n;
14410 isl_bool full;
14411 isl_pw_multi_aff *pma;
14413 n = isl_map_dim(map, type);
14414 if (n < 0 || !mpa)
14415 goto error;
14417 full = isl_map_involves_dims(map, type, 0, n);
14418 if (full >= 0 && !full)
14419 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14420 if (full < 0)
14421 goto error;
14422 if (!full) {
14423 isl_space *space;
14424 isl_multi_aff *ma;
14426 space = isl_multi_pw_aff_get_space(mpa);
14427 isl_multi_pw_aff_free(mpa);
14428 ma = isl_multi_aff_zero(space);
14429 return isl_map_preimage_multi_aff(map, type, ma);
14432 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14433 return isl_map_preimage_pw_multi_aff(map, type, pma);
14434 error:
14435 isl_map_free(map);
14436 isl_multi_pw_aff_free(mpa);
14437 return NULL;
14440 /* Compute the preimage of "map" under the function represented by "mpa".
14441 * In other words, plug in "mpa" in the domain "map".
14442 * The result is a map that lives in the same space as "map",
14443 * except that domain space has been replaced by the domain space of "mpa".
14445 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14446 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14448 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14451 /* Compute the preimage of "set" by the function represented by "mpa".
14452 * In other words, plug in "mpa" in "set".
14454 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14455 __isl_take isl_multi_pw_aff *mpa)
14457 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14460 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14461 * on the output dimension "pos" in terms of the parameters,
14462 * the input dimensions and possibly some integer divisions,
14463 * but not any other output dimensions, extract this upper bound
14464 * as a function of all dimensions (with zero coefficients
14465 * for the output dimensions).
14467 * That is, the inequality is of the form
14469 * e(...) + c - m x >= 0
14471 * where e does not depend on any other output dimensions.
14472 * Return (e(...) + c) / m, with the denominator m in the first position.
14474 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14475 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14477 isl_ctx *ctx;
14478 isl_size v_out, total;
14480 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14481 total = isl_basic_map_dim(bmap, isl_dim_all);
14482 if (v_out < 0 || total < 0)
14483 return NULL;
14484 ctx = isl_basic_map_get_ctx(bmap);
14485 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14486 total, v_out + pos);
14489 /* Is constraint "c" of "bmap" of the form
14491 * e(...) + c1 - m x >= 0
14493 * or
14495 * -e(...) + c2 + m x >= 0
14497 * where m > 1 and e does not involve any other output variables?
14499 * "v_out" is the offset to the output variables.
14500 * "d" is the position of x among the output variables.
14501 * "v_div" is the offset to the local variables.
14502 * "total" is the total number of variables.
14504 * Since the purpose of this function is to use the constraint
14505 * to express the output variable as an integer division,
14506 * do not allow the constraint to involve any local variables
14507 * that do not have an explicit representation.
14509 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14510 isl_int *c, int v_out, int d, int v_div, int total)
14512 int i = 0;
14514 if (isl_int_is_zero(c[1 + v_out + d]))
14515 return isl_bool_false;
14516 if (isl_int_is_one(c[1 + v_out + d]))
14517 return isl_bool_false;
14518 if (isl_int_is_negone(c[1 + v_out + d]))
14519 return isl_bool_false;
14520 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14521 return isl_bool_false;
14522 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14523 v_div - (v_out + d + 1)) != -1)
14524 return isl_bool_false;
14525 for (i = 0; v_div + i < total; ++i) {
14526 isl_bool known, involves;
14528 if (isl_int_is_zero(c[1 + v_div + i]))
14529 continue;
14530 known = isl_basic_map_div_is_known(bmap, i);
14531 if (known < 0 || !known)
14532 return known;
14533 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14534 if (involves < 0 || involves)
14535 return isl_bool_not(involves);
14537 return isl_bool_true;
14540 /* Look for a pair of constraints
14542 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14544 * and
14546 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14548 * that express that the output dimension x at position "pos"
14549 * is some integer division of an expression in terms of the parameters,
14550 * input dimensions and integer divisions.
14551 * If such a pair can be found, then return the index
14552 * of the upper bound constraint, m x <= e(...) + c1.
14553 * Otherwise, return an index beyond the number of constraints.
14555 * In order for the constraints above to express an integer division,
14556 * m needs to be greater than 1 and such that
14558 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14560 * In particular, this ensures that
14562 * x = floor((e(...) + c1) / m)
14564 isl_size isl_basic_map_find_output_upper_div_constraint(
14565 __isl_keep isl_basic_map *bmap, int pos)
14567 int i, j;
14568 isl_size n_ineq;
14569 isl_size v_out, v_div;
14570 isl_size total;
14571 isl_int sum;
14573 total = isl_basic_map_dim(bmap, isl_dim_all);
14574 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14575 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
14576 n_ineq = isl_basic_map_n_inequality(bmap);
14577 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
14578 return isl_size_error;
14580 isl_int_init(sum);
14581 for (i = 0; i < n_ineq; ++i) {
14582 isl_bool potential;
14584 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
14585 v_out, pos, v_div, total);
14586 if (potential < 0)
14587 goto error;
14588 if (!potential)
14589 continue;
14590 for (j = i + 1; j < n_ineq; ++j) {
14591 if (!is_constraint_pair(bmap, i, j, total))
14592 continue;
14593 if (constraint_pair_has_bound(bmap, i, j,
14594 bmap->ineq[i][1 + v_out + pos], &sum))
14595 break;
14597 if (j < n_ineq)
14598 break;
14600 isl_int_clear(sum);
14602 if (i >= n_ineq)
14603 return n_ineq;
14604 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
14605 return i;
14606 else
14607 return j;
14608 error:
14609 isl_int_clear(sum);
14610 return isl_size_error;
14613 /* Return a copy of the equality constraints of "bset" as a matrix.
14615 __isl_give isl_mat *isl_basic_set_extract_equalities(
14616 __isl_keep isl_basic_set *bset)
14618 isl_ctx *ctx;
14619 isl_size total;
14621 total = isl_basic_set_dim(bset, isl_dim_all);
14622 if (total < 0)
14623 return NULL;
14625 ctx = isl_basic_set_get_ctx(bset);
14626 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14629 /* Are the "n" "coefficients" starting at "first" of the integer division
14630 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14631 * to each other?
14632 * The "coefficient" at position 0 is the denominator.
14633 * The "coefficient" at position 1 is the constant term.
14635 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14636 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14637 unsigned first, unsigned n)
14639 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14640 return isl_bool_error;
14641 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14642 return isl_bool_error;
14643 return isl_seq_eq(bmap1->div[pos1] + first,
14644 bmap2->div[pos2] + first, n);
14647 /* Are the integer division expressions at position "pos1" in "bmap1" and
14648 * "pos2" in "bmap2" equal to each other, except that the constant terms
14649 * are different?
14651 isl_bool isl_basic_map_equal_div_expr_except_constant(
14652 __isl_keep isl_basic_map *bmap1, int pos1,
14653 __isl_keep isl_basic_map *bmap2, int pos2)
14655 isl_bool equal;
14656 isl_size total, total2;
14658 total = isl_basic_map_dim(bmap1, isl_dim_all);
14659 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14660 if (total < 0 || total2 < 0)
14661 return isl_bool_error;
14662 if (total != total2)
14663 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14664 "incomparable div expressions", return isl_bool_error);
14665 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14666 0, 1);
14667 if (equal < 0 || !equal)
14668 return equal;
14669 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14670 1, 1);
14671 if (equal < 0 || equal)
14672 return isl_bool_not(equal);
14673 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14674 2, total);
14677 /* Replace the numerator of the constant term of the integer division
14678 * expression at position "div" in "bmap" by "value".
14679 * The caller guarantees that this does not change the meaning
14680 * of the input.
14682 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14683 __isl_take isl_basic_map *bmap, int div, int value)
14685 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14686 return isl_basic_map_free(bmap);
14688 isl_int_set_si(bmap->div[div][1], value);
14690 return bmap;
14693 /* Is the point "inner" internal to inequality constraint "ineq"
14694 * of "bset"?
14695 * The point is considered to be internal to the inequality constraint,
14696 * if it strictly lies on the positive side of the inequality constraint,
14697 * or if it lies on the constraint and the constraint is lexico-positive.
14699 static isl_bool is_internal(__isl_keep isl_vec *inner,
14700 __isl_keep isl_basic_set *bset, int ineq)
14702 isl_ctx *ctx;
14703 int pos;
14704 isl_size total;
14706 if (!inner || !bset)
14707 return isl_bool_error;
14709 ctx = isl_basic_set_get_ctx(bset);
14710 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14711 &ctx->normalize_gcd);
14712 if (!isl_int_is_zero(ctx->normalize_gcd))
14713 return isl_int_is_nonneg(ctx->normalize_gcd);
14715 total = isl_basic_set_dim(bset, isl_dim_all);
14716 if (total < 0)
14717 return isl_bool_error;
14718 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14719 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14722 /* Tighten the inequality constraints of "bset" that are outward with respect
14723 * to the point "vec".
14724 * That is, tighten the constraints that are not satisfied by "vec".
14726 * "vec" is a point internal to some superset S of "bset" that is used
14727 * to make the subsets of S disjoint, by tightening one half of the constraints
14728 * that separate two subsets. In particular, the constraints of S
14729 * are all satisfied by "vec" and should not be tightened.
14730 * Of the internal constraints, those that have "vec" on the outside
14731 * are tightened. The shared facet is included in the adjacent subset
14732 * with the opposite constraint.
14733 * For constraints that saturate "vec", this criterion cannot be used
14734 * to determine which of the two sides should be tightened.
14735 * Instead, the sign of the first non-zero coefficient is used
14736 * to make this choice. Note that this second criterion is never used
14737 * on the constraints of S since "vec" is interior to "S".
14739 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14740 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14742 int j;
14744 bset = isl_basic_set_cow(bset);
14745 if (!bset)
14746 return NULL;
14747 for (j = 0; j < bset->n_ineq; ++j) {
14748 isl_bool internal;
14750 internal = is_internal(vec, bset, j);
14751 if (internal < 0)
14752 return isl_basic_set_free(bset);
14753 if (internal)
14754 continue;
14755 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14758 return bset;
14761 /* Replace the variables x of type "type" starting at "first" in "bmap"
14762 * by x' with x = M x' with M the matrix trans.
14763 * That is, replace the corresponding coefficients c by c M.
14765 * The transformation matrix should be a square matrix.
14767 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14768 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14769 __isl_take isl_mat *trans)
14771 unsigned pos;
14773 bmap = isl_basic_map_cow(bmap);
14774 if (!bmap || !trans)
14775 goto error;
14777 if (trans->n_row != trans->n_col)
14778 isl_die(trans->ctx, isl_error_invalid,
14779 "expecting square transformation matrix", goto error);
14780 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14781 goto error;
14783 pos = isl_basic_map_offset(bmap, type) + first;
14785 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14786 isl_mat_copy(trans)) < 0)
14787 goto error;
14788 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14789 isl_mat_copy(trans)) < 0)
14790 goto error;
14791 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14792 isl_mat_copy(trans)) < 0)
14793 goto error;
14795 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14796 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14797 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
14799 isl_mat_free(trans);
14800 return bmap;
14801 error:
14802 isl_mat_free(trans);
14803 isl_basic_map_free(bmap);
14804 return NULL;
14807 /* Replace the variables x of type "type" starting at "first" in "bset"
14808 * by x' with x = M x' with M the matrix trans.
14809 * That is, replace the corresponding coefficients c by c M.
14811 * The transformation matrix should be a square matrix.
14813 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14814 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14815 __isl_take isl_mat *trans)
14817 return isl_basic_map_transform_dims(bset, type, first, trans);