isl_map.c: div_involves_vars: use isl_basic_map_var_offset
[isl.git] / isl_map.c
blob6d1aaa901e58f261ed52915449e6357a4d90ef25
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <string.h>
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
28 #include <isl_blk.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
34 #include <isl_seq.h>
35 #include <isl/set.h>
36 #include <isl/map.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
39 #include <isl_sort.h>
40 #include "isl_tab.h"
41 #include <isl/vec.h>
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep const isl_basic_map *const_bset_to_bmap(
62 __isl_keep const isl_basic_set *bset)
64 return (const isl_basic_map *) bset;
67 #undef TYPE
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
73 switch (type) {
74 case isl_dim_param: return 1;
75 case isl_dim_in: return 1 + space->nparam;
76 case isl_dim_out: return 1 + space->nparam + space->n_in;
77 default: return 0;
81 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
82 enum isl_dim_type type)
84 if (!bmap)
85 return isl_size_error;
86 switch (type) {
87 case isl_dim_cst: return 1;
88 case isl_dim_param:
89 case isl_dim_in:
90 case isl_dim_out: return isl_space_dim(bmap->dim, type);
91 case isl_dim_div: return bmap->n_div;
92 case isl_dim_all: return isl_basic_map_total_dim(bmap);
93 default: return 0;
97 /* Return the space of "map".
99 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
101 return map ? map->dim : NULL;
104 /* Return the space of "set".
106 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
108 return isl_map_peek_space(set_to_map(set));
111 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
113 return isl_space_dim(isl_map_peek_space(map), type);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
120 return isl_map_dim(map, isl_dim_in);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
127 return isl_map_dim(map, isl_dim_out);
130 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
132 return isl_map_dim(set_to_map(set), type);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_set);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
146 enum isl_dim_type type)
148 isl_space *space;
150 space = isl_basic_map_peek_space(bmap);
152 switch (type) {
153 case isl_dim_param:
154 case isl_dim_in:
155 case isl_dim_out: return isl_space_offset(space, type);
156 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
157 case isl_dim_cst:
158 default:
159 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
160 "invalid dimension type", return isl_size_error);
164 /* Return the position of the variables of the given type
165 * within the sequence of variables of "bset".
167 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
168 enum isl_dim_type type)
170 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
173 /* Return the position of the coefficients of the variables of the given type
174 * within the sequence of coefficients of "bmap".
176 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
177 enum isl_dim_type type)
179 switch (type) {
180 case isl_dim_cst: return 0;
181 case isl_dim_param:
182 case isl_dim_in:
183 case isl_dim_out:
184 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
185 default: return 0;
189 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
190 enum isl_dim_type type)
192 return isl_basic_map_offset(bset, type);
195 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
197 return pos(map->dim, type);
200 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
201 enum isl_dim_type type)
203 return isl_basic_map_dim(bset, type);
206 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
208 return isl_basic_set_dim(bset, isl_dim_set);
211 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
213 return isl_basic_set_dim(bset, isl_dim_param);
216 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
218 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
221 isl_size isl_set_n_dim(__isl_keep isl_set *set)
223 return isl_set_dim(set, isl_dim_set);
226 isl_size isl_set_n_param(__isl_keep isl_set *set)
228 return isl_set_dim(set, isl_dim_param);
231 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
233 isl_size dim;
235 if (!bmap)
236 return isl_size_error;
237 dim = isl_space_dim(bmap->dim, isl_dim_all);
238 if (dim < 0)
239 return isl_size_error;
240 return dim + bmap->n_div;
243 /* Return the number of equality constraints in the description of "bmap".
244 * Return isl_size_error on error.
246 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
248 if (!bmap)
249 return isl_size_error;
250 return bmap->n_eq;
253 /* Return the number of equality constraints in the description of "bset".
254 * Return isl_size_error on error.
256 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
258 return isl_basic_map_n_equality(bset_to_bmap(bset));
261 /* Return the number of inequality constraints in the description of "bmap".
262 * Return isl_size_error on error.
264 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
266 if (!bmap)
267 return isl_size_error;
268 return bmap->n_ineq;
271 /* Return the number of inequality constraints in the description of "bset".
272 * Return isl_size_error on error.
274 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
276 return isl_basic_map_n_inequality(bset_to_bmap(bset));
279 /* Do "bmap1" and "bmap2" have the same parameters?
281 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
282 __isl_keep isl_basic_map *bmap2)
284 isl_space *space1, *space2;
286 space1 = isl_basic_map_peek_space(bmap1);
287 space2 = isl_basic_map_peek_space(bmap2);
288 return isl_space_has_equal_params(space1, space2);
291 /* Do "map1" and "map2" have the same parameters?
293 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
294 __isl_keep isl_map *map2)
296 isl_space *space1, *space2;
298 space1 = isl_map_peek_space(map1);
299 space2 = isl_map_peek_space(map2);
300 return isl_space_has_equal_params(space1, space2);
303 /* Do "map" and "set" have the same parameters?
305 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
306 __isl_keep isl_set *set)
308 return isl_map_has_equal_params(map, set_to_map(set));
311 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
313 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
314 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
316 isl_space *bmap_space, *bset_space;
318 bmap_space = isl_basic_map_peek_space(bmap);
319 bset_space = isl_basic_set_peek_space(bset);
320 return isl_space_tuple_is_equal(bmap_space, type,
321 bset_space, isl_dim_set);
324 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
326 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
327 enum isl_dim_type type, __isl_keep isl_set *set)
329 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
332 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
333 __isl_keep isl_set *set)
335 isl_bool m;
336 if (!map || !set)
337 return isl_bool_error;
338 m = isl_map_has_equal_params(map, set_to_map(set));
339 if (m < 0 || !m)
340 return m;
341 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
344 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
345 __isl_keep isl_basic_set *bset)
347 isl_bool m;
348 if (!bmap || !bset)
349 return isl_bool_error;
350 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
351 if (m < 0 || !m)
352 return m;
353 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
356 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
357 __isl_keep isl_set *set)
359 isl_bool m;
360 if (!map || !set)
361 return isl_bool_error;
362 m = isl_map_has_equal_params(map, set_to_map(set));
363 if (m < 0 || !m)
364 return m;
365 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
368 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
369 __isl_keep isl_basic_set *bset)
371 isl_bool m;
372 if (!bmap || !bset)
373 return isl_bool_error;
374 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
375 if (m < 0 || !m)
376 return m;
377 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
380 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
382 return bmap ? bmap->ctx : NULL;
385 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
387 return bset ? bset->ctx : NULL;
390 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
392 return map ? map->ctx : NULL;
395 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
397 return set ? set->ctx : NULL;
400 /* Return the space of "bmap".
402 __isl_keep isl_space *isl_basic_map_peek_space(
403 __isl_keep const isl_basic_map *bmap)
405 return bmap ? bmap->dim : NULL;
408 /* Return the space of "bset".
410 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
412 return isl_basic_map_peek_space(bset_to_bmap(bset));
415 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
417 return isl_space_copy(isl_basic_map_peek_space(bmap));
420 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
422 return isl_basic_map_get_space(bset_to_bmap(bset));
425 /* Return the space of "bmap".
426 * This may be either a copy or the space itself
427 * if there is only one reference to "bmap".
428 * This allows the space to be modified inplace
429 * if both the basic map and its space have only a single reference.
430 * The caller is not allowed to modify "bmap" between this call and
431 * a subsequent call to isl_basic_map_restore_space.
432 * The only exception is that isl_basic_map_free can be called instead.
434 static __isl_give isl_space *isl_basic_map_take_space(
435 __isl_keep isl_basic_map *bmap)
437 isl_space *space;
439 if (!bmap)
440 return NULL;
441 if (bmap->ref != 1)
442 return isl_basic_map_get_space(bmap);
443 space = bmap->dim;
444 bmap->dim = NULL;
445 return space;
448 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
449 * due to a preceding call to isl_basic_map_take_space.
450 * However, in this case, "bmap" only has a single reference and
451 * then the call to isl_basic_map_cow has no effect.
453 static __isl_give isl_basic_map *isl_basic_map_restore_space(
454 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
456 if (!bmap || !space)
457 goto error;
459 if (bmap->dim == space) {
460 isl_space_free(space);
461 return bmap;
464 bmap = isl_basic_map_cow(bmap);
465 if (!bmap)
466 goto error;
467 isl_space_free(bmap->dim);
468 bmap->dim = space;
470 return bmap;
471 error:
472 isl_basic_map_free(bmap);
473 isl_space_free(space);
474 return NULL;
477 /* Extract the divs in "bmap" as a matrix.
479 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
481 int i;
482 isl_ctx *ctx;
483 isl_mat *div;
484 isl_size v_div;
485 unsigned cols;
487 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
488 if (v_div < 0)
489 return NULL;
491 ctx = isl_basic_map_get_ctx(bmap);
492 cols = 1 + 1 + v_div + bmap->n_div;
493 div = isl_mat_alloc(ctx, bmap->n_div, cols);
494 if (!div)
495 return NULL;
497 for (i = 0; i < bmap->n_div; ++i)
498 isl_seq_cpy(div->row[i], bmap->div[i], cols);
500 return div;
503 /* Extract the divs in "bset" as a matrix.
505 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
507 return isl_basic_map_get_divs(bset);
510 __isl_give isl_local_space *isl_basic_map_get_local_space(
511 __isl_keep isl_basic_map *bmap)
513 isl_mat *div;
515 if (!bmap)
516 return NULL;
518 div = isl_basic_map_get_divs(bmap);
519 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
522 __isl_give isl_local_space *isl_basic_set_get_local_space(
523 __isl_keep isl_basic_set *bset)
525 return isl_basic_map_get_local_space(bset);
528 /* For each known div d = floor(f/m), add the constraints
530 * f - m d >= 0
531 * -(f-(m-1)) + m d >= 0
533 * Do not finalize the result.
535 static __isl_give isl_basic_map *add_known_div_constraints(
536 __isl_take isl_basic_map *bmap)
538 int i;
539 isl_size n_div;
541 n_div = isl_basic_map_dim(bmap, isl_dim_div);
542 if (n_div < 0)
543 return isl_basic_map_free(bmap);
544 if (n_div == 0)
545 return bmap;
546 bmap = isl_basic_map_cow(bmap);
547 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
548 if (!bmap)
549 return NULL;
550 for (i = 0; i < n_div; ++i) {
551 if (isl_int_is_zero(bmap->div[i][0]))
552 continue;
553 bmap = isl_basic_map_add_div_constraints(bmap, i);
556 return bmap;
559 __isl_give isl_basic_map *isl_basic_map_from_local_space(
560 __isl_take isl_local_space *ls)
562 int i;
563 isl_size n_div;
564 isl_basic_map *bmap;
566 n_div = isl_local_space_dim(ls, isl_dim_div);
567 if (n_div < 0)
568 ls = isl_local_space_free(ls);
569 if (!ls)
570 return NULL;
572 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
573 n_div, 0, 2 * n_div);
575 for (i = 0; i < n_div; ++i)
576 if (isl_basic_map_alloc_div(bmap) < 0)
577 goto error;
579 for (i = 0; i < n_div; ++i)
580 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
581 bmap = add_known_div_constraints(bmap);
583 isl_local_space_free(ls);
584 return bmap;
585 error:
586 isl_local_space_free(ls);
587 isl_basic_map_free(bmap);
588 return NULL;
591 __isl_give isl_basic_set *isl_basic_set_from_local_space(
592 __isl_take isl_local_space *ls)
594 return isl_basic_map_from_local_space(ls);
597 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
599 return isl_space_copy(isl_map_peek_space(map));
602 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
604 if (!set)
605 return NULL;
606 return isl_space_copy(set->dim);
609 /* Return the space of "map".
610 * This may be either a copy or the space itself
611 * if there is only one reference to "map".
612 * This allows the space to be modified inplace
613 * if both the map and its space have only a single reference.
614 * The caller is not allowed to modify "map" between this call and
615 * a subsequent call to isl_map_restore_space.
616 * The only exception is that isl_map_free can be called instead.
618 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
620 isl_space *space;
622 if (!map)
623 return NULL;
624 if (map->ref != 1)
625 return isl_map_get_space(map);
626 space = map->dim;
627 map->dim = NULL;
628 return space;
631 /* Set the space of "map" to "space", where the space of "map" may be missing
632 * due to a preceding call to isl_map_take_space.
633 * However, in this case, "map" only has a single reference and
634 * then the call to isl_map_cow has no effect.
636 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
637 __isl_take isl_space *space)
639 if (!map || !space)
640 goto error;
642 if (map->dim == space) {
643 isl_space_free(space);
644 return map;
647 map = isl_map_cow(map);
648 if (!map)
649 goto error;
650 isl_space_free(map->dim);
651 map->dim = space;
653 return map;
654 error:
655 isl_map_free(map);
656 isl_space_free(space);
657 return NULL;
660 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
661 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
663 isl_space *space;
665 space = isl_basic_map_take_space(bmap);
666 space = isl_space_set_tuple_name(space, type, s);
667 bmap = isl_basic_map_restore_space(bmap, space);
668 bmap = isl_basic_map_finalize(bmap);
669 return bmap;
672 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
673 __isl_take isl_basic_set *bset, const char *s)
675 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
678 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
679 enum isl_dim_type type)
681 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
684 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
685 enum isl_dim_type type, const char *s)
687 int i;
688 isl_space *space;
690 map = isl_map_cow(map);
691 if (!map)
692 return NULL;
694 for (i = 0; i < map->n; ++i) {
695 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
696 if (!map->p[i])
697 goto error;
700 space = isl_map_take_space(map);
701 space = isl_space_set_tuple_name(space, type, s);
702 map = isl_map_restore_space(map, space);
704 return map;
705 error:
706 isl_map_free(map);
707 return NULL;
710 /* Replace the identifier of the tuple of type "type" by "id".
712 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
713 __isl_take isl_basic_map *bmap,
714 enum isl_dim_type type, __isl_take isl_id *id)
716 isl_space *space;
718 space = isl_basic_map_take_space(bmap);
719 space = isl_space_set_tuple_id(space, type, id);
720 bmap = isl_basic_map_restore_space(bmap, space);
721 bmap = isl_basic_map_finalize(bmap);
722 return bmap;
725 /* Replace the identifier of the tuple by "id".
727 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
728 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
730 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
733 /* Does the input or output tuple have a name?
735 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
737 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
740 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
741 enum isl_dim_type type)
743 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
746 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
747 const char *s)
749 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
750 isl_dim_set, s));
753 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
754 enum isl_dim_type type, __isl_take isl_id *id)
756 isl_space *space;
758 space = isl_map_take_space(map);
759 space = isl_space_set_tuple_id(space, type, id);
760 map = isl_map_restore_space(map, space);
762 return isl_map_reset_space(map, isl_map_get_space(map));
765 /* Replace the identifier of the domain tuple of "map" by "id".
767 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
768 __isl_take isl_id *id)
770 return isl_map_set_tuple_id(map, isl_dim_in, id);
773 /* Replace the identifier of the range tuple of "map" by "id".
775 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
776 __isl_take isl_id *id)
778 return isl_map_set_tuple_id(map, isl_dim_out, id);
781 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
782 __isl_take isl_id *id)
784 return isl_map_set_tuple_id(set, isl_dim_set, id);
787 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
788 enum isl_dim_type type)
790 isl_space *space;
792 space = isl_map_take_space(map);
793 space = isl_space_reset_tuple_id(space, type);
794 map = isl_map_restore_space(map, space);
796 return isl_map_reset_space(map, isl_map_get_space(map));
799 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
801 return isl_map_reset_tuple_id(set, isl_dim_set);
804 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
806 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
809 /* Does the domain tuple of "map" have an identifier?
811 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
813 return isl_map_has_tuple_id(map, isl_dim_in);
816 /* Does the range tuple of "map" have an identifier?
818 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
820 return isl_map_has_tuple_id(map, isl_dim_out);
823 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
824 enum isl_dim_type type)
826 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
829 /* Return the identifier of the domain tuple of "map", assuming it has one.
831 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
833 return isl_map_get_tuple_id(map, isl_dim_in);
836 /* Return the identifier of the range tuple of "map", assuming it has one.
838 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
840 return isl_map_get_tuple_id(map, isl_dim_out);
843 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
845 return isl_map_has_tuple_id(set, isl_dim_set);
848 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
850 return isl_map_get_tuple_id(set, isl_dim_set);
853 /* Does the set tuple have a name?
855 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
857 if (!set)
858 return isl_bool_error;
859 return isl_space_has_tuple_name(set->dim, isl_dim_set);
863 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
865 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
868 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
870 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
873 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
874 enum isl_dim_type type, unsigned pos)
876 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
879 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
880 enum isl_dim_type type, unsigned pos)
882 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
885 /* Does the given dimension have a name?
887 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
888 enum isl_dim_type type, unsigned pos)
890 if (!map)
891 return isl_bool_error;
892 return isl_space_has_dim_name(map->dim, type, pos);
895 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
896 enum isl_dim_type type, unsigned pos)
898 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
901 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
902 enum isl_dim_type type, unsigned pos)
904 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
907 /* Does the given dimension have a name?
909 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
910 enum isl_dim_type type, unsigned pos)
912 if (!set)
913 return isl_bool_error;
914 return isl_space_has_dim_name(set->dim, type, pos);
917 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
918 __isl_take isl_basic_map *bmap,
919 enum isl_dim_type type, unsigned pos, const char *s)
921 isl_space *space;
923 space = isl_basic_map_take_space(bmap);
924 space = isl_space_set_dim_name(space, type, pos, s);
925 bmap = isl_basic_map_restore_space(bmap, space);
926 return isl_basic_map_finalize(bmap);
929 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
930 enum isl_dim_type type, unsigned pos, const char *s)
932 int i;
933 isl_space *space;
935 map = isl_map_cow(map);
936 if (!map)
937 return NULL;
939 for (i = 0; i < map->n; ++i) {
940 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
941 if (!map->p[i])
942 goto error;
945 space = isl_map_take_space(map);
946 space = isl_space_set_dim_name(space, type, pos, s);
947 map = isl_map_restore_space(map, space);
949 return map;
950 error:
951 isl_map_free(map);
952 return NULL;
955 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
956 __isl_take isl_basic_set *bset,
957 enum isl_dim_type type, unsigned pos, const char *s)
959 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
960 type, pos, s));
963 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
964 enum isl_dim_type type, unsigned pos, const char *s)
966 return set_from_map(isl_map_set_dim_name(set_to_map(set),
967 type, pos, s));
970 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
971 enum isl_dim_type type, unsigned pos)
973 if (!bmap)
974 return isl_bool_error;
975 return isl_space_has_dim_id(bmap->dim, type, pos);
978 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
979 enum isl_dim_type type, unsigned pos)
981 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
984 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
985 enum isl_dim_type type, unsigned pos)
987 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
990 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
991 enum isl_dim_type type, unsigned pos)
993 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
996 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
997 enum isl_dim_type type, unsigned pos)
999 return isl_map_has_dim_id(set, type, pos);
1002 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1003 enum isl_dim_type type, unsigned pos)
1005 return isl_map_get_dim_id(set, type, pos);
1008 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1009 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1011 isl_space *space;
1013 space = isl_map_take_space(map);
1014 space = isl_space_set_dim_id(space, type, pos, id);
1015 map = isl_map_restore_space(map, space);
1017 return isl_map_reset_space(map, isl_map_get_space(map));
1020 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1021 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1023 return isl_map_set_dim_id(set, type, pos, id);
1026 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1027 __isl_keep isl_id *id)
1029 if (!map)
1030 return -1;
1031 return isl_space_find_dim_by_id(map->dim, type, id);
1034 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1035 __isl_keep isl_id *id)
1037 return isl_map_find_dim_by_id(set, type, id);
1040 /* Return the position of the dimension of the given type and name
1041 * in "bmap".
1042 * Return -1 if no such dimension can be found.
1044 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1045 enum isl_dim_type type, const char *name)
1047 if (!bmap)
1048 return -1;
1049 return isl_space_find_dim_by_name(bmap->dim, type, name);
1052 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1053 const char *name)
1055 if (!map)
1056 return -1;
1057 return isl_space_find_dim_by_name(map->dim, type, name);
1060 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1061 const char *name)
1063 return isl_map_find_dim_by_name(set, type, name);
1066 /* Check whether equality i of bset is a pure stride constraint
1067 * on a single dimension, i.e., of the form
1069 * v = k e
1071 * with k a constant and e an existentially quantified variable.
1073 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1075 isl_size nparam;
1076 isl_size d;
1077 isl_size n_div;
1078 int pos1;
1079 int pos2;
1081 nparam = isl_basic_set_dim(bset, isl_dim_param);
1082 d = isl_basic_set_dim(bset, isl_dim_set);
1083 n_div = isl_basic_set_dim(bset, isl_dim_div);
1084 if (nparam < 0 || d < 0 || n_div < 0)
1085 return isl_bool_error;
1087 if (!isl_int_is_zero(bset->eq[i][0]))
1088 return isl_bool_false;
1090 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1091 return isl_bool_false;
1092 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1093 if (pos1 == -1)
1094 return isl_bool_false;
1095 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1096 d - pos1 - 1) != -1)
1097 return isl_bool_false;
1099 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1100 if (pos2 == -1)
1101 return isl_bool_false;
1102 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1103 n_div - pos2 - 1) != -1)
1104 return isl_bool_false;
1105 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1106 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1107 return isl_bool_false;
1109 return isl_bool_true;
1112 /* Reset the user pointer on all identifiers of parameters and tuples
1113 * of the space of "map".
1115 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1117 isl_space *space;
1119 space = isl_map_get_space(map);
1120 space = isl_space_reset_user(space);
1121 map = isl_map_reset_space(map, space);
1123 return map;
1126 /* Reset the user pointer on all identifiers of parameters and tuples
1127 * of the space of "set".
1129 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1131 return isl_map_reset_user(set);
1134 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1136 if (!bmap)
1137 return isl_bool_error;
1138 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1141 /* Has "map" been marked as a rational map?
1142 * In particular, have all basic maps in "map" been marked this way?
1143 * An empty map is not considered to be rational.
1144 * Maps where only some of the basic maps are marked rational
1145 * are not allowed.
1147 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1149 int i;
1150 isl_bool rational;
1152 if (!map)
1153 return isl_bool_error;
1154 if (map->n == 0)
1155 return isl_bool_false;
1156 rational = isl_basic_map_is_rational(map->p[0]);
1157 if (rational < 0)
1158 return rational;
1159 for (i = 1; i < map->n; ++i) {
1160 isl_bool rational_i;
1162 rational_i = isl_basic_map_is_rational(map->p[i]);
1163 if (rational_i < 0)
1164 return rational_i;
1165 if (rational != rational_i)
1166 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1167 "mixed rational and integer basic maps "
1168 "not supported", return isl_bool_error);
1171 return rational;
1174 /* Has "set" been marked as a rational set?
1175 * In particular, have all basic set in "set" been marked this way?
1176 * An empty set is not considered to be rational.
1177 * Sets where only some of the basic sets are marked rational
1178 * are not allowed.
1180 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1182 return isl_map_is_rational(set);
1185 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1187 return isl_basic_map_is_rational(bset);
1190 /* Does "bmap" contain any rational points?
1192 * If "bmap" has an equality for each dimension, equating the dimension
1193 * to an integer constant, then it has no rational points, even if it
1194 * is marked as rational.
1196 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1198 isl_bool has_rational = isl_bool_true;
1199 isl_size total;
1201 if (!bmap)
1202 return isl_bool_error;
1203 if (isl_basic_map_plain_is_empty(bmap))
1204 return isl_bool_false;
1205 if (!isl_basic_map_is_rational(bmap))
1206 return isl_bool_false;
1207 bmap = isl_basic_map_copy(bmap);
1208 bmap = isl_basic_map_implicit_equalities(bmap);
1209 total = isl_basic_map_dim(bmap, isl_dim_all);
1210 if (total < 0)
1211 return isl_bool_error;
1212 if (bmap->n_eq == total) {
1213 int i, j;
1214 for (i = 0; i < bmap->n_eq; ++i) {
1215 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1216 if (j < 0)
1217 break;
1218 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1219 !isl_int_is_negone(bmap->eq[i][1 + j]))
1220 break;
1221 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1222 total - j - 1);
1223 if (j >= 0)
1224 break;
1226 if (i == bmap->n_eq)
1227 has_rational = isl_bool_false;
1229 isl_basic_map_free(bmap);
1231 return has_rational;
1234 /* Does "map" contain any rational points?
1236 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1238 int i;
1239 isl_bool has_rational;
1241 if (!map)
1242 return isl_bool_error;
1243 for (i = 0; i < map->n; ++i) {
1244 has_rational = isl_basic_map_has_rational(map->p[i]);
1245 if (has_rational < 0 || has_rational)
1246 return has_rational;
1248 return isl_bool_false;
1251 /* Does "set" contain any rational points?
1253 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1255 return isl_map_has_rational(set);
1258 /* Is this basic set a parameter domain?
1260 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1262 if (!bset)
1263 return isl_bool_error;
1264 return isl_space_is_params(bset->dim);
1267 /* Is this set a parameter domain?
1269 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1271 if (!set)
1272 return isl_bool_error;
1273 return isl_space_is_params(set->dim);
1276 /* Is this map actually a parameter domain?
1277 * Users should never call this function. Outside of isl,
1278 * a map can never be a parameter domain.
1280 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1282 if (!map)
1283 return isl_bool_error;
1284 return isl_space_is_params(map->dim);
1287 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1288 __isl_take isl_basic_map *bmap, unsigned extra,
1289 unsigned n_eq, unsigned n_ineq)
1291 int i;
1292 isl_space *space = isl_basic_map_peek_space(bmap);
1293 isl_size n_var = isl_space_dim(space, isl_dim_all);
1294 size_t row_size = 1 + n_var + extra;
1296 bmap->ctx = ctx;
1297 isl_ctx_ref(ctx);
1299 if (n_var < 0)
1300 return isl_basic_map_free(bmap);
1302 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1303 if (isl_blk_is_error(bmap->block))
1304 goto error;
1306 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1307 if ((n_ineq + n_eq) && !bmap->ineq)
1308 goto error;
1310 if (extra == 0) {
1311 bmap->block2 = isl_blk_empty();
1312 bmap->div = NULL;
1313 } else {
1314 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1315 if (isl_blk_is_error(bmap->block2))
1316 goto error;
1318 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1319 if (!bmap->div)
1320 goto error;
1323 for (i = 0; i < n_ineq + n_eq; ++i)
1324 bmap->ineq[i] = bmap->block.data + i * row_size;
1326 for (i = 0; i < extra; ++i)
1327 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1329 bmap->ref = 1;
1330 bmap->flags = 0;
1331 bmap->c_size = n_eq + n_ineq;
1332 bmap->eq = bmap->ineq + n_ineq;
1333 bmap->extra = extra;
1334 bmap->n_eq = 0;
1335 bmap->n_ineq = 0;
1336 bmap->n_div = 0;
1337 bmap->sample = NULL;
1339 return bmap;
1340 error:
1341 isl_basic_map_free(bmap);
1342 return NULL;
1345 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1346 unsigned nparam, unsigned dim, unsigned extra,
1347 unsigned n_eq, unsigned n_ineq)
1349 struct isl_basic_map *bmap;
1350 isl_space *space;
1352 space = isl_space_set_alloc(ctx, nparam, dim);
1353 if (!space)
1354 return NULL;
1356 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1357 return bset_from_bmap(bmap);
1360 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1361 unsigned extra, unsigned n_eq, unsigned n_ineq)
1363 struct isl_basic_map *bmap;
1364 if (!space)
1365 return NULL;
1366 isl_assert(space->ctx, space->n_in == 0, goto error);
1367 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1368 return bset_from_bmap(bmap);
1369 error:
1370 isl_space_free(space);
1371 return NULL;
1374 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1375 unsigned extra, unsigned n_eq, unsigned n_ineq)
1377 struct isl_basic_map *bmap;
1379 if (!space)
1380 return NULL;
1381 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1382 if (!bmap)
1383 goto error;
1384 bmap->dim = space;
1386 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1387 error:
1388 isl_space_free(space);
1389 return NULL;
1392 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1393 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1394 unsigned n_eq, unsigned n_ineq)
1396 struct isl_basic_map *bmap;
1397 isl_space *space;
1399 space = isl_space_alloc(ctx, nparam, in, out);
1400 if (!space)
1401 return NULL;
1403 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1404 return bmap;
1407 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1408 __isl_keep isl_basic_map *src)
1410 int i;
1411 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1413 if (!dst || total < 0)
1414 return isl_basic_map_free(dst);
1416 for (i = 0; i < src->n_eq; ++i) {
1417 int j = isl_basic_map_alloc_equality(dst);
1418 if (j < 0)
1419 return isl_basic_map_free(dst);
1420 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1423 for (i = 0; i < src->n_ineq; ++i) {
1424 int j = isl_basic_map_alloc_inequality(dst);
1425 if (j < 0)
1426 return isl_basic_map_free(dst);
1427 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1430 for (i = 0; i < src->n_div; ++i) {
1431 int j = isl_basic_map_alloc_div(dst);
1432 if (j < 0)
1433 return isl_basic_map_free(dst);
1434 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1436 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1437 return dst;
1440 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1442 struct isl_basic_map *dup;
1444 if (!bmap)
1445 return NULL;
1446 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1447 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1448 dup = dup_constraints(dup, bmap);
1449 if (!dup)
1450 return NULL;
1451 dup->flags = bmap->flags;
1452 dup->sample = isl_vec_copy(bmap->sample);
1453 return dup;
1456 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1458 struct isl_basic_map *dup;
1460 dup = isl_basic_map_dup(bset_to_bmap(bset));
1461 return bset_from_bmap(dup);
1464 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1466 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1469 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1471 if (!set)
1472 return NULL;
1474 set->ref++;
1475 return set;
1478 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1480 if (!bmap)
1481 return NULL;
1483 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1484 bmap->ref++;
1485 return bmap;
1487 bmap = isl_basic_map_dup(bmap);
1488 if (bmap)
1489 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1490 return bmap;
1493 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1495 if (!map)
1496 return NULL;
1498 map->ref++;
1499 return map;
1502 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1504 if (!bmap)
1505 return NULL;
1507 if (--bmap->ref > 0)
1508 return NULL;
1510 isl_ctx_deref(bmap->ctx);
1511 free(bmap->div);
1512 isl_blk_free(bmap->ctx, bmap->block2);
1513 free(bmap->ineq);
1514 isl_blk_free(bmap->ctx, bmap->block);
1515 isl_vec_free(bmap->sample);
1516 isl_space_free(bmap->dim);
1517 free(bmap);
1519 return NULL;
1522 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1524 return isl_basic_map_free(bset_to_bmap(bset));
1527 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1529 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1532 /* Check that "bset" does not involve any parameters.
1534 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1536 isl_size nparam;
1538 nparam = isl_basic_set_dim(bset, isl_dim_param);
1539 if (nparam < 0)
1540 return isl_stat_error;
1541 if (nparam != 0)
1542 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1543 "basic set should not have any parameters",
1544 return isl_stat_error);
1545 return isl_stat_ok;
1548 /* Check that "bset" does not involve any local variables.
1550 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1552 isl_size n_div;
1554 n_div = isl_basic_set_dim(bset, isl_dim_div);
1555 if (n_div < 0)
1556 return isl_stat_error;
1557 if (n_div != 0)
1558 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1559 "basic set should not have any local variables",
1560 return isl_stat_error);
1561 return isl_stat_ok;
1564 #undef TYPE
1565 #define TYPE isl_map
1567 #include "isl_check_named_params_templ.c"
1569 #undef TYPE
1570 #define TYPE isl_basic_map
1572 static
1573 #include "isl_check_named_params_templ.c"
1575 /* Check that "bmap1" and "bmap2" have the same parameters,
1576 * reporting an error if they do not.
1578 static isl_stat isl_basic_map_check_equal_params(
1579 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1581 isl_bool match;
1583 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1584 if (match < 0)
1585 return isl_stat_error;
1586 if (!match)
1587 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1588 "parameters don't match", return isl_stat_error);
1589 return isl_stat_ok;
1592 #undef TYPE
1593 #define TYPE isl_map
1595 #include "isl_align_params_bin_templ.c"
1597 #undef SUFFIX
1598 #define SUFFIX set
1599 #undef ARG1
1600 #define ARG1 isl_map
1601 #undef ARG2
1602 #define ARG2 isl_set
1604 #include "isl_align_params_templ.c"
1606 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1607 __isl_keep isl_map *map2,
1608 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1610 isl_bool r;
1612 if (!map1 || !map2)
1613 return isl_bool_error;
1614 if (isl_map_has_equal_params(map1, map2))
1615 return fn(map1, map2);
1616 if (isl_map_check_named_params(map1) < 0)
1617 return isl_bool_error;
1618 if (isl_map_check_named_params(map2) < 0)
1619 return isl_bool_error;
1620 map1 = isl_map_copy(map1);
1621 map2 = isl_map_copy(map2);
1622 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1623 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1624 r = fn(map1, map2);
1625 isl_map_free(map1);
1626 isl_map_free(map2);
1627 return r;
1630 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1632 isl_size total;
1633 struct isl_ctx *ctx;
1635 total = isl_basic_map_dim(bmap, isl_dim_all);
1636 if (total < 0)
1637 return -1;
1638 ctx = bmap->ctx;
1639 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1640 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1641 return -1);
1642 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1646 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1647 isl_int *t;
1648 int j = isl_basic_map_alloc_inequality(bmap);
1649 if (j < 0)
1650 return -1;
1651 t = bmap->ineq[j];
1652 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1653 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1654 bmap->eq[-1] = t;
1655 bmap->n_eq++;
1656 bmap->n_ineq--;
1657 bmap->eq--;
1658 return 0;
1660 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1661 bmap->extra - bmap->n_div);
1662 return bmap->n_eq++;
1665 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1667 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1670 __isl_give isl_basic_map *isl_basic_map_free_equality(
1671 __isl_take isl_basic_map *bmap, unsigned n)
1673 if (!bmap)
1674 return NULL;
1675 if (n > bmap->n_eq)
1676 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1677 "invalid number of equalities",
1678 isl_basic_map_free(bmap));
1679 bmap->n_eq -= n;
1680 return bmap;
1683 __isl_give isl_basic_set *isl_basic_set_free_equality(
1684 __isl_take isl_basic_set *bset, unsigned n)
1686 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1687 n));
1690 /* Drop the equality constraint at position "pos",
1691 * preserving the order of the other equality constraints.
1693 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1695 isl_int *t;
1696 int r;
1698 if (!bmap)
1699 return -1;
1700 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1702 t = bmap->eq[pos];
1703 bmap->n_eq--;
1704 for (r = pos; r < bmap->n_eq; ++r)
1705 bmap->eq[r] = bmap->eq[r + 1];
1706 bmap->eq[bmap->n_eq] = t;
1708 return 0;
1711 /* Turn inequality "pos" of "bmap" into an equality.
1713 * In particular, we move the inequality in front of the equalities
1714 * and move the last inequality in the position of the moved inequality.
1715 * Note that isl_tab_make_equalities_explicit depends on this particular
1716 * change in the ordering of the constraints.
1718 void isl_basic_map_inequality_to_equality(
1719 __isl_keep isl_basic_map *bmap, unsigned pos)
1721 isl_int *t;
1723 t = bmap->ineq[pos];
1724 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1725 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1726 bmap->eq[-1] = t;
1727 bmap->n_eq++;
1728 bmap->n_ineq--;
1729 bmap->eq--;
1730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1731 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1736 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1738 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1741 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1743 isl_size total;
1744 struct isl_ctx *ctx;
1746 total = isl_basic_map_dim(bmap, isl_dim_all);
1747 if (total < 0)
1748 return -1;
1749 ctx = bmap->ctx;
1750 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1751 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1755 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1756 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1757 bmap->extra - bmap->n_div);
1758 return bmap->n_ineq++;
1761 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1763 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1766 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1767 __isl_take isl_basic_map *bmap, unsigned n)
1769 if (!bmap)
1770 return NULL;
1771 if (n > bmap->n_ineq)
1772 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1773 "invalid number of inequalities",
1774 return isl_basic_map_free(bmap));
1775 bmap->n_ineq -= n;
1776 return bmap;
1779 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1780 __isl_take isl_basic_set *bset, unsigned n)
1782 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1783 n));
1786 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1788 isl_int *t;
1789 if (!bmap)
1790 return -1;
1791 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1793 if (pos != bmap->n_ineq - 1) {
1794 t = bmap->ineq[pos];
1795 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1796 bmap->ineq[bmap->n_ineq - 1] = t;
1797 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1799 bmap->n_ineq--;
1800 return 0;
1803 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1805 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1808 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1809 isl_int *eq)
1811 isl_bool empty;
1812 isl_size total;
1813 int k;
1815 empty = isl_basic_map_plain_is_empty(bmap);
1816 if (empty < 0)
1817 return isl_basic_map_free(bmap);
1818 if (empty)
1819 return bmap;
1821 bmap = isl_basic_map_cow(bmap);
1822 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1823 total = isl_basic_map_dim(bmap, isl_dim_all);
1824 if (total < 0)
1825 return isl_basic_map_free(bmap);
1826 k = isl_basic_map_alloc_equality(bmap);
1827 if (k < 0)
1828 goto error;
1829 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1830 return bmap;
1831 error:
1832 isl_basic_map_free(bmap);
1833 return NULL;
1836 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1837 isl_int *eq)
1839 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1842 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1843 isl_int *ineq)
1845 isl_size total;
1846 int k;
1848 bmap = isl_basic_map_cow(bmap);
1849 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1850 total = isl_basic_map_dim(bmap, isl_dim_all);
1851 if (total < 0)
1852 return isl_basic_map_free(bmap);
1853 k = isl_basic_map_alloc_inequality(bmap);
1854 if (k < 0)
1855 goto error;
1856 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1857 return bmap;
1858 error:
1859 isl_basic_map_free(bmap);
1860 return NULL;
1863 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1864 isl_int *ineq)
1866 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1869 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1871 isl_size total;
1873 total = isl_basic_map_dim(bmap, isl_dim_all);
1874 if (total < 0)
1875 return -1;
1876 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1877 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1878 bmap->extra - bmap->n_div);
1879 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1880 return bmap->n_div++;
1883 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1885 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1888 #undef TYPE
1889 #define TYPE isl_basic_map
1890 #include "check_type_range_templ.c"
1892 /* Check that there are "n" dimensions of type "type" starting at "first"
1893 * in "bset".
1895 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1896 enum isl_dim_type type, unsigned first, unsigned n)
1898 return isl_basic_map_check_range(bset_to_bmap(bset),
1899 type, first, n);
1902 /* Insert an extra integer division, prescribed by "div", to "bmap"
1903 * at (integer division) position "pos".
1905 * The integer division is first added at the end and then moved
1906 * into the right position.
1908 __isl_give isl_basic_map *isl_basic_map_insert_div(
1909 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1911 int i, k;
1912 isl_size total;
1914 bmap = isl_basic_map_cow(bmap);
1915 total = isl_basic_map_dim(bmap, isl_dim_all);
1916 if (total < 0 || !div)
1917 return isl_basic_map_free(bmap);
1919 if (div->size != 1 + 1 + total)
1920 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1921 "unexpected size", return isl_basic_map_free(bmap));
1922 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1923 return isl_basic_map_free(bmap);
1925 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1926 k = isl_basic_map_alloc_div(bmap);
1927 if (k < 0)
1928 return isl_basic_map_free(bmap);
1929 isl_seq_cpy(bmap->div[k], div->el, div->size);
1930 isl_int_set_si(bmap->div[k][div->size], 0);
1932 for (i = k; i > pos; --i)
1933 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1935 return bmap;
1938 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1940 if (!bmap)
1941 return isl_stat_error;
1942 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1943 bmap->n_div -= n;
1944 return isl_stat_ok;
1947 static __isl_give isl_basic_map *add_constraints(
1948 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1949 unsigned i_pos, unsigned o_pos)
1951 isl_size total, n_param, n_in, n_out, n_div;
1952 unsigned o_in, o_out;
1953 isl_ctx *ctx;
1954 isl_space *space;
1955 struct isl_dim_map *dim_map;
1957 space = isl_basic_map_peek_space(bmap2);
1958 if (!bmap1 || !space)
1959 goto error;
1961 total = isl_basic_map_dim(bmap1, isl_dim_all);
1962 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1963 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1964 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1965 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1966 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1967 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1968 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1969 goto error;
1970 ctx = isl_basic_map_get_ctx(bmap1);
1971 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1972 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1974 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1975 isl_dim_map_div(dim_map, bmap2, total);
1977 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1978 error:
1979 isl_basic_map_free(bmap1);
1980 isl_basic_map_free(bmap2);
1981 return NULL;
1984 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1985 unsigned extra, unsigned n_eq, unsigned n_ineq)
1987 isl_space *space;
1988 struct isl_basic_map *ext;
1989 unsigned flags;
1990 int dims_ok;
1992 if (!base)
1993 goto error;
1995 dims_ok = base->extra >= base->n_div + extra;
1997 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1998 room_for_ineq(base, n_ineq))
1999 return base;
2001 extra += base->extra;
2002 n_eq += base->n_eq;
2003 n_ineq += base->n_ineq;
2005 space = isl_basic_map_get_space(base);
2006 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2007 if (!ext)
2008 goto error;
2010 if (dims_ok)
2011 ext->sample = isl_vec_copy(base->sample);
2012 flags = base->flags;
2013 ext = add_constraints(ext, base, 0, 0);
2014 if (ext) {
2015 ext->flags = flags;
2016 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2019 return ext;
2021 error:
2022 isl_basic_map_free(base);
2023 return NULL;
2026 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2027 unsigned extra, unsigned n_eq, unsigned n_ineq)
2029 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2030 extra, n_eq, n_ineq));
2033 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2034 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2036 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2039 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2040 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2042 isl_basic_map *bmap = bset_to_bmap(base);
2043 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2044 return bset_from_bmap(bmap);
2047 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2049 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2052 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2054 if (!bmap)
2055 return NULL;
2057 if (bmap->ref > 1) {
2058 bmap->ref--;
2059 bmap = isl_basic_map_dup(bmap);
2061 if (bmap)
2062 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2063 return bmap;
2066 /* Clear all cached information in "map", either because it is about
2067 * to be modified or because it is being freed.
2068 * Always return the same pointer that is passed in.
2069 * This is needed for the use in isl_map_free.
2071 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2073 isl_basic_map_free(map->cached_simple_hull[0]);
2074 isl_basic_map_free(map->cached_simple_hull[1]);
2075 map->cached_simple_hull[0] = NULL;
2076 map->cached_simple_hull[1] = NULL;
2077 return map;
2080 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2082 return isl_map_cow(set);
2085 /* Return an isl_map that is equal to "map" and that has only
2086 * a single reference.
2088 * If the original input already has only one reference, then
2089 * simply return it, but clear all cached information, since
2090 * it may be rendered invalid by the operations that will be
2091 * performed on the result.
2093 * Otherwise, create a duplicate (without any cached information).
2095 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2097 if (!map)
2098 return NULL;
2100 if (map->ref == 1)
2101 return clear_caches(map);
2102 map->ref--;
2103 return isl_map_dup(map);
2106 static void swap_vars(struct isl_blk blk, isl_int *a,
2107 unsigned a_len, unsigned b_len)
2109 isl_seq_cpy(blk.data, a+a_len, b_len);
2110 isl_seq_cpy(blk.data+b_len, a, a_len);
2111 isl_seq_cpy(a, blk.data, b_len+a_len);
2114 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2115 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2117 int i;
2118 struct isl_blk blk;
2120 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2121 goto error;
2123 if (n1 == 0 || n2 == 0)
2124 return bmap;
2126 bmap = isl_basic_map_cow(bmap);
2127 if (!bmap)
2128 return NULL;
2130 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2131 if (isl_blk_is_error(blk))
2132 goto error;
2134 for (i = 0; i < bmap->n_eq; ++i)
2135 swap_vars(blk,
2136 bmap->eq[i] + pos, n1, n2);
2138 for (i = 0; i < bmap->n_ineq; ++i)
2139 swap_vars(blk,
2140 bmap->ineq[i] + pos, n1, n2);
2142 for (i = 0; i < bmap->n_div; ++i)
2143 swap_vars(blk,
2144 bmap->div[i]+1 + pos, n1, n2);
2146 isl_blk_free(bmap->ctx, blk);
2148 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2149 bmap = isl_basic_map_gauss(bmap, NULL);
2150 return isl_basic_map_finalize(bmap);
2151 error:
2152 isl_basic_map_free(bmap);
2153 return NULL;
2156 /* The given basic map has turned out to be empty.
2157 * Explicitly mark it as such and change the representation
2158 * to a canonical representation of the empty basic map.
2159 * Since the basic map has conflicting constraints,
2160 * it must have at least one constraint, except perhaps
2161 * if it was already explicitly marked as being empty.
2162 * Do nothing in the latter case, i.e., if it has been marked empty and
2163 * has no constraints.
2165 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2166 __isl_take isl_basic_map *bmap)
2168 int i = 0;
2169 isl_bool empty;
2170 isl_size n;
2171 isl_size total;
2173 n = isl_basic_map_n_constraint(bmap);
2174 empty = isl_basic_map_plain_is_empty(bmap);
2175 if (n < 0 || empty < 0)
2176 return isl_basic_map_free(bmap);
2177 if (n == 0 && empty)
2178 return bmap;
2179 total = isl_basic_map_dim(bmap, isl_dim_all);
2180 if (total < 0)
2181 return isl_basic_map_free(bmap);
2182 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2183 return isl_basic_map_free(bmap);
2184 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2185 if (!bmap)
2186 return NULL;
2187 if (bmap->n_eq > 0) {
2188 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2189 if (!bmap)
2190 return NULL;
2191 } else {
2192 i = isl_basic_map_alloc_equality(bmap);
2193 if (i < 0)
2194 goto error;
2196 isl_int_set_si(bmap->eq[i][0], 1);
2197 isl_seq_clr(bmap->eq[i]+1, total);
2198 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2199 isl_vec_free(bmap->sample);
2200 bmap->sample = NULL;
2201 return isl_basic_map_finalize(bmap);
2202 error:
2203 isl_basic_map_free(bmap);
2204 return NULL;
2207 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2208 __isl_take isl_basic_set *bset)
2210 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2213 __isl_give isl_basic_map *isl_basic_map_set_rational(
2214 __isl_take isl_basic_map *bmap)
2216 if (!bmap)
2217 return NULL;
2219 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2220 return bmap;
2222 bmap = isl_basic_map_cow(bmap);
2223 if (!bmap)
2224 return NULL;
2226 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2228 return isl_basic_map_finalize(bmap);
2231 __isl_give isl_basic_set *isl_basic_set_set_rational(
2232 __isl_take isl_basic_set *bset)
2234 return isl_basic_map_set_rational(bset);
2237 __isl_give isl_basic_set *isl_basic_set_set_integral(
2238 __isl_take isl_basic_set *bset)
2240 if (!bset)
2241 return NULL;
2243 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2244 return bset;
2246 bset = isl_basic_set_cow(bset);
2247 if (!bset)
2248 return NULL;
2250 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2252 return isl_basic_set_finalize(bset);
2255 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2257 int i;
2259 map = isl_map_cow(map);
2260 if (!map)
2261 return NULL;
2262 for (i = 0; i < map->n; ++i) {
2263 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2264 if (!map->p[i])
2265 goto error;
2267 return map;
2268 error:
2269 isl_map_free(map);
2270 return NULL;
2273 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2275 return isl_map_set_rational(set);
2278 /* Given a constraint "c" that expresses a bound
2279 * on the variable at position "pos" in terms of the first "len" variables
2280 * (other than the variable itself if pos < len), extract this bound
2281 * as a function of those first "len" variables.
2283 * That is, the constraint is of one of the following forms
2285 * -e(...) + m x >= 0
2286 * e(...) - m x >= 0
2287 * -e(...) + m x = 0
2288 * e(...) - m x = 0
2290 * Return (e(...)) / m, with the denominator m in the first position.
2292 static __isl_give isl_vec *extract_bound_from_constraint(isl_ctx *ctx,
2293 isl_int *c, int len, int pos)
2295 isl_vec *v;
2297 v = isl_vec_alloc(ctx, 1 + 1 + len);
2298 if (!v)
2299 return NULL;
2300 if (isl_int_is_pos(c[1 + pos])) {
2301 isl_int_set(v->el[0], c[1 + pos]);
2302 isl_seq_neg(v->el + 1, c, 1 + len);
2303 } else {
2304 isl_int_neg(v->el[0], c[1 + pos]);
2305 isl_seq_cpy(v->el + 1, c, 1 + len);
2307 if (pos < len)
2308 isl_int_set_si(v->el[1 + 1 + pos], 0);
2310 return v;
2313 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2314 * of "bmap").
2316 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2318 isl_int *t = bmap->div[a];
2319 bmap->div[a] = bmap->div[b];
2320 bmap->div[b] = t;
2323 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2324 * div definitions accordingly.
2326 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2327 int a, int b)
2329 int i;
2330 isl_size off;
2332 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2333 if (off < 0)
2334 return isl_basic_map_free(bmap);
2336 swap_div(bmap, a, b);
2338 for (i = 0; i < bmap->n_eq; ++i)
2339 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2341 for (i = 0; i < bmap->n_ineq; ++i)
2342 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2344 for (i = 0; i < bmap->n_div; ++i)
2345 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2346 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2348 return bmap;
2351 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2353 isl_seq_cpy(c, c + n, rem);
2354 isl_seq_clr(c + rem, n);
2357 /* Drop n dimensions starting at first.
2359 * In principle, this frees up some extra variables as the number
2360 * of columns remains constant, but we would have to extend
2361 * the div array too as the number of rows in this array is assumed
2362 * to be equal to extra.
2364 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2365 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2367 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2370 /* Move "n" divs starting at "first" to the end of the list of divs.
2372 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2373 unsigned first, unsigned n)
2375 isl_int **div;
2376 int i;
2378 if (first + n == bmap->n_div)
2379 return bmap;
2381 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2382 if (!div)
2383 goto error;
2384 for (i = 0; i < n; ++i)
2385 div[i] = bmap->div[first + i];
2386 for (i = 0; i < bmap->n_div - first - n; ++i)
2387 bmap->div[first + i] = bmap->div[first + n + i];
2388 for (i = 0; i < n; ++i)
2389 bmap->div[bmap->n_div - n + i] = div[i];
2390 free(div);
2391 return bmap;
2392 error:
2393 isl_basic_map_free(bmap);
2394 return NULL;
2397 #undef TYPE
2398 #define TYPE isl_map
2399 static
2400 #include "check_type_range_templ.c"
2402 /* Check that there are "n" dimensions of type "type" starting at "first"
2403 * in "set".
2405 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 return isl_map_check_range(set_to_map(set), type, first, n);
2411 /* Drop "n" dimensions of type "type" starting at "first".
2412 * Perform the core computation, without cowing or
2413 * simplifying and finalizing the result.
2415 * In principle, this frees up some extra variables as the number
2416 * of columns remains constant, but we would have to extend
2417 * the div array too as the number of rows in this array is assumed
2418 * to be equal to extra.
2420 __isl_give isl_basic_map *isl_basic_map_drop_core(
2421 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2422 unsigned first, unsigned n)
2424 int i;
2425 unsigned offset;
2426 unsigned left;
2427 isl_size total;
2429 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2430 return isl_basic_map_free(bmap);
2432 total = isl_basic_map_dim(bmap, isl_dim_all);
2433 if (total < 0)
2434 return isl_basic_map_free(bmap);
2436 offset = isl_basic_map_offset(bmap, type) + first;
2437 left = total - (offset - 1) - n;
2438 for (i = 0; i < bmap->n_eq; ++i)
2439 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2441 for (i = 0; i < bmap->n_ineq; ++i)
2442 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2444 for (i = 0; i < bmap->n_div; ++i)
2445 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2447 if (type == isl_dim_div) {
2448 bmap = move_divs_last(bmap, first, n);
2449 if (!bmap)
2450 return NULL;
2451 if (isl_basic_map_free_div(bmap, n) < 0)
2452 return isl_basic_map_free(bmap);
2453 } else
2454 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2455 if (!bmap->dim)
2456 return isl_basic_map_free(bmap);
2458 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2459 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2460 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2461 return bmap;
2464 /* Drop "n" dimensions of type "type" starting at "first".
2466 * In principle, this frees up some extra variables as the number
2467 * of columns remains constant, but we would have to extend
2468 * the div array too as the number of rows in this array is assumed
2469 * to be equal to extra.
2471 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 if (!bmap)
2475 return NULL;
2476 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2477 return bmap;
2479 bmap = isl_basic_map_cow(bmap);
2480 if (!bmap)
2481 return NULL;
2483 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2485 bmap = isl_basic_map_simplify(bmap);
2486 return isl_basic_map_finalize(bmap);
2489 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2490 enum isl_dim_type type, unsigned first, unsigned n)
2492 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2493 type, first, n));
2496 /* No longer consider "map" to be normalized.
2498 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2500 if (!map)
2501 return NULL;
2502 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2503 return map;
2506 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2507 enum isl_dim_type type, unsigned first, unsigned n)
2509 int i;
2510 isl_space *space;
2512 if (isl_map_check_range(map, type, first, n) < 0)
2513 return isl_map_free(map);
2515 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2516 return map;
2517 map = isl_map_cow(map);
2518 if (!map)
2519 goto error;
2521 for (i = 0; i < map->n; ++i) {
2522 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2523 if (!map->p[i])
2524 goto error;
2526 map = isl_map_unmark_normalized(map);
2528 space = isl_map_take_space(map);
2529 space = isl_space_drop_dims(space, type, first, n);
2530 map = isl_map_restore_space(map, space);
2532 return map;
2533 error:
2534 isl_map_free(map);
2535 return NULL;
2538 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2539 enum isl_dim_type type, unsigned first, unsigned n)
2541 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2544 /* Drop the integer division at position "div", which is assumed
2545 * not to appear in any of the constraints or
2546 * in any of the other integer divisions.
2548 * Since the integer division is redundant, there is no need to cow.
2550 __isl_give isl_basic_map *isl_basic_map_drop_div(
2551 __isl_take isl_basic_map *bmap, unsigned div)
2553 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2556 /* Eliminate the specified n dimensions starting at first from the
2557 * constraints, without removing the dimensions from the space.
2558 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2560 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2561 enum isl_dim_type type, unsigned first, unsigned n)
2563 int i;
2565 if (n == 0)
2566 return map;
2568 if (isl_map_check_range(map, type, first, n) < 0)
2569 return isl_map_free(map);
2571 map = isl_map_cow(map);
2572 if (!map)
2573 return NULL;
2575 for (i = 0; i < map->n; ++i) {
2576 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2577 if (!map->p[i])
2578 goto error;
2580 return map;
2581 error:
2582 isl_map_free(map);
2583 return NULL;
2586 /* Eliminate the specified n dimensions starting at first from the
2587 * constraints, without removing the dimensions from the space.
2588 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2590 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2591 enum isl_dim_type type, unsigned first, unsigned n)
2593 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2596 /* Eliminate the specified n dimensions starting at first from the
2597 * constraints, without removing the dimensions from the space.
2598 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2600 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2601 unsigned first, unsigned n)
2603 return isl_set_eliminate(set, isl_dim_set, first, n);
2606 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2607 __isl_take isl_basic_map *bmap)
2609 isl_size v_div;
2611 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2612 if (v_div < 0)
2613 return isl_basic_map_free(bmap);
2614 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2615 if (!bmap)
2616 return NULL;
2617 bmap->n_div = 0;
2618 return isl_basic_map_finalize(bmap);
2621 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2622 __isl_take isl_basic_set *bset)
2624 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2627 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2629 int i;
2631 if (!map)
2632 return NULL;
2633 if (map->n == 0)
2634 return map;
2636 map = isl_map_cow(map);
2637 if (!map)
2638 return NULL;
2640 for (i = 0; i < map->n; ++i) {
2641 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2642 if (!map->p[i])
2643 goto error;
2645 return map;
2646 error:
2647 isl_map_free(map);
2648 return NULL;
2651 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2653 return isl_map_remove_divs(set);
2656 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2657 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2658 unsigned first, unsigned n)
2660 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2661 return isl_basic_map_free(bmap);
2662 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2663 return bmap;
2664 bmap = isl_basic_map_eliminate_vars(bmap,
2665 isl_basic_map_offset(bmap, type) - 1 + first, n);
2666 if (!bmap)
2667 return bmap;
2668 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2669 return bmap;
2670 bmap = isl_basic_map_drop(bmap, type, first, n);
2671 return bmap;
2674 /* Return true if the definition of the given div (recursively) involves
2675 * any of the given variables.
2677 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2678 unsigned first, unsigned n)
2680 int i;
2681 isl_bool unknown;
2682 isl_size n_div, v_div;
2684 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
2685 if (unknown < 0 || unknown)
2686 return isl_bool_not(unknown);
2687 if (isl_seq_first_non_zero(bmap->div[div] + 1 + 1 + first, n) >= 0)
2688 return isl_bool_true;
2690 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2691 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2692 if (n_div < 0 || v_div < 0)
2693 return isl_bool_error;
2694 for (i = n_div - 1; i >= 0; --i) {
2695 isl_bool involves;
2697 if (isl_int_is_zero(bmap->div[div][1 + 1 + v_div + i]))
2698 continue;
2699 involves = div_involves_vars(bmap, i, first, n);
2700 if (involves < 0 || involves)
2701 return involves;
2704 return isl_bool_false;
2707 /* Try and add a lower and/or upper bound on "div" to "bmap"
2708 * based on inequality "i".
2709 * "total" is the total number of variables (excluding the divs).
2710 * "v" is a temporary object that can be used during the calculations.
2711 * If "lb" is set, then a lower bound should be constructed.
2712 * If "ub" is set, then an upper bound should be constructed.
2714 * The calling function has already checked that the inequality does not
2715 * reference "div", but we still need to check that the inequality is
2716 * of the right form. We'll consider the case where we want to construct
2717 * a lower bound. The construction of upper bounds is similar.
2719 * Let "div" be of the form
2721 * q = floor((a + f(x))/d)
2723 * We essentially check if constraint "i" is of the form
2725 * b + f(x) >= 0
2727 * so that we can use it to derive a lower bound on "div".
2728 * However, we allow a slightly more general form
2730 * b + g(x) >= 0
2732 * with the condition that the coefficients of g(x) - f(x) are all
2733 * divisible by d.
2734 * Rewriting this constraint as
2736 * 0 >= -b - g(x)
2738 * adding a + f(x) to both sides and dividing by d, we obtain
2740 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2742 * Taking the floor on both sides, we obtain
2744 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2746 * or
2748 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2750 * In the case of an upper bound, we construct the constraint
2752 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2755 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2756 __isl_take isl_basic_map *bmap, int div, int i,
2757 unsigned total, isl_int v, int lb, int ub)
2759 int j;
2761 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2762 if (lb) {
2763 isl_int_sub(v, bmap->ineq[i][1 + j],
2764 bmap->div[div][1 + 1 + j]);
2765 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2767 if (ub) {
2768 isl_int_add(v, bmap->ineq[i][1 + j],
2769 bmap->div[div][1 + 1 + j]);
2770 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2773 if (!lb && !ub)
2774 return bmap;
2776 bmap = isl_basic_map_cow(bmap);
2777 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2778 if (lb) {
2779 int k = isl_basic_map_alloc_inequality(bmap);
2780 if (k < 0)
2781 goto error;
2782 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2783 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2784 bmap->div[div][1 + j]);
2785 isl_int_cdiv_q(bmap->ineq[k][j],
2786 bmap->ineq[k][j], bmap->div[div][0]);
2788 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2790 if (ub) {
2791 int k = isl_basic_map_alloc_inequality(bmap);
2792 if (k < 0)
2793 goto error;
2794 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2795 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2796 bmap->div[div][1 + j]);
2797 isl_int_fdiv_q(bmap->ineq[k][j],
2798 bmap->ineq[k][j], bmap->div[div][0]);
2800 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2803 return bmap;
2804 error:
2805 isl_basic_map_free(bmap);
2806 return NULL;
2809 /* This function is called right before "div" is eliminated from "bmap"
2810 * using Fourier-Motzkin.
2811 * Look through the constraints of "bmap" for constraints on the argument
2812 * of the integer division and use them to construct constraints on the
2813 * integer division itself. These constraints can then be combined
2814 * during the Fourier-Motzkin elimination.
2815 * Note that it is only useful to introduce lower bounds on "div"
2816 * if "bmap" already contains upper bounds on "div" as the newly
2817 * introduce lower bounds can then be combined with the pre-existing
2818 * upper bounds. Similarly for upper bounds.
2819 * We therefore first check if "bmap" contains any lower and/or upper bounds
2820 * on "div".
2822 * It is interesting to note that the introduction of these constraints
2823 * can indeed lead to more accurate results, even when compared to
2824 * deriving constraints on the argument of "div" from constraints on "div".
2825 * Consider, for example, the set
2827 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2829 * The second constraint can be rewritten as
2831 * 2 * [(-i-2j+3)/4] + k >= 0
2833 * from which we can derive
2835 * -i - 2j + 3 >= -2k
2837 * or
2839 * i + 2j <= 3 + 2k
2841 * Combined with the first constraint, we obtain
2843 * -3 <= 3 + 2k or k >= -3
2845 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2846 * the first constraint, we obtain
2848 * [(i + 2j)/4] >= [-3/4] = -1
2850 * Combining this constraint with the second constraint, we obtain
2852 * k >= -2
2854 static __isl_give isl_basic_map *insert_bounds_on_div(
2855 __isl_take isl_basic_map *bmap, int div)
2857 int i;
2858 int check_lb, check_ub;
2859 isl_int v;
2860 isl_size v_div;
2862 if (!bmap)
2863 return NULL;
2865 if (isl_int_is_zero(bmap->div[div][0]))
2866 return bmap;
2868 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2869 if (v_div < 0)
2870 return isl_basic_map_free(bmap);
2872 check_lb = 0;
2873 check_ub = 0;
2874 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2875 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2876 if (s > 0)
2877 check_ub = 1;
2878 if (s < 0)
2879 check_lb = 1;
2882 if (!check_lb && !check_ub)
2883 return bmap;
2885 isl_int_init(v);
2887 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2888 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2889 continue;
2891 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2892 check_lb, check_ub);
2895 isl_int_clear(v);
2897 return bmap;
2900 /* Remove all divs (recursively) involving any of the given variables
2901 * in their definitions.
2903 static __isl_give isl_basic_map *remove_divs_involving_vars(
2904 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2906 int i;
2908 for (i = bmap->n_div - 1; i >= 0; --i) {
2909 isl_bool involves;
2911 involves = div_involves_vars(bmap, i, first, n);
2912 if (involves < 0)
2913 return isl_basic_map_free(bmap);
2914 if (!involves)
2915 continue;
2916 bmap = insert_bounds_on_div(bmap, i);
2917 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2918 if (!bmap)
2919 return NULL;
2920 i = bmap->n_div;
2923 return bmap;
2926 /* Remove all divs (recursively) involving any of the given dimensions
2927 * in their definitions.
2929 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2930 __isl_take isl_basic_map *bmap,
2931 enum isl_dim_type type, unsigned first, unsigned n)
2933 isl_size off;
2935 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2936 return isl_basic_map_free(bmap);
2937 off = isl_basic_map_var_offset(bmap, type);
2938 if (off < 0)
2939 return isl_basic_map_free(bmap);
2940 first += off;
2942 return remove_divs_involving_vars(bmap, first, n);
2945 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2946 __isl_take isl_basic_set *bset,
2947 enum isl_dim_type type, unsigned first, unsigned n)
2949 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2952 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2953 enum isl_dim_type type, unsigned first, unsigned n)
2955 int i;
2957 if (!map)
2958 return NULL;
2959 if (map->n == 0)
2960 return map;
2962 map = isl_map_cow(map);
2963 if (!map)
2964 return NULL;
2966 for (i = 0; i < map->n; ++i) {
2967 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2968 type, first, n);
2969 if (!map->p[i])
2970 goto error;
2972 return map;
2973 error:
2974 isl_map_free(map);
2975 return NULL;
2978 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2979 enum isl_dim_type type, unsigned first, unsigned n)
2981 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2982 type, first, n));
2985 /* Does the description of "bmap" depend on the specified dimensions?
2986 * We also check whether the dimensions appear in any of the div definitions.
2987 * In principle there is no need for this check. If the dimensions appear
2988 * in a div definition, they also appear in the defining constraints of that
2989 * div.
2991 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2992 enum isl_dim_type type, unsigned first, unsigned n)
2994 int i;
2996 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2997 return isl_bool_error;
2999 first += isl_basic_map_offset(bmap, type);
3000 for (i = 0; i < bmap->n_eq; ++i)
3001 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
3002 return isl_bool_true;
3003 for (i = 0; i < bmap->n_ineq; ++i)
3004 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
3005 return isl_bool_true;
3006 for (i = 0; i < bmap->n_div; ++i) {
3007 if (isl_int_is_zero(bmap->div[i][0]))
3008 continue;
3009 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
3010 return isl_bool_true;
3013 return isl_bool_false;
3016 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
3017 enum isl_dim_type type, unsigned first, unsigned n)
3019 int i;
3021 if (isl_map_check_range(map, type, first, n) < 0)
3022 return isl_bool_error;
3024 for (i = 0; i < map->n; ++i) {
3025 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
3026 type, first, n);
3027 if (involves < 0 || involves)
3028 return involves;
3031 return isl_bool_false;
3034 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
3035 enum isl_dim_type type, unsigned first, unsigned n)
3037 return isl_basic_map_involves_dims(bset, type, first, n);
3040 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
3041 enum isl_dim_type type, unsigned first, unsigned n)
3043 return isl_map_involves_dims(set, type, first, n);
3046 /* Does "bset" involve any local variables, i.e., integer divisions?
3048 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
3050 isl_size n;
3052 n = isl_basic_set_dim(bset, isl_dim_div);
3053 if (n < 0)
3054 return isl_bool_error;
3055 return isl_bool_ok(n > 0);
3058 /* isl_set_every_basic_set callback that checks whether "bset"
3059 * is free of local variables.
3061 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3063 return isl_bool_not(isl_basic_set_involves_locals(bset));
3066 /* Does "set" involve any local variables, i.e., integer divisions?
3068 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3070 isl_bool no_locals;
3072 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3073 return isl_bool_not(no_locals);
3076 /* Drop all constraints in bmap that involve any of the dimensions
3077 * first to first+n-1.
3078 * This function only performs the actual removal of constraints.
3080 * This function should not call finalize since it is used by
3081 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3083 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3084 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3086 int i;
3088 if (n == 0)
3089 return bmap;
3091 bmap = isl_basic_map_cow(bmap);
3093 if (!bmap)
3094 return NULL;
3096 for (i = bmap->n_eq - 1; i >= 0; --i) {
3097 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3098 continue;
3099 if (isl_basic_map_drop_equality(bmap, i) < 0)
3100 return isl_basic_map_free(bmap);
3103 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3104 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3105 continue;
3106 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3107 return isl_basic_map_free(bmap);
3110 return bmap;
3113 /* Drop all constraints in bset that involve any of the dimensions
3114 * first to first+n-1.
3115 * This function only performs the actual removal of constraints.
3117 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3118 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3120 return isl_basic_map_drop_constraints_involving(bset, first, n);
3123 /* Drop all constraints in bmap that do not involve any of the dimensions
3124 * first to first + n - 1 of the given type.
3126 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3127 __isl_take isl_basic_map *bmap,
3128 enum isl_dim_type type, unsigned first, unsigned n)
3130 int i;
3132 if (n == 0) {
3133 isl_space *space = isl_basic_map_get_space(bmap);
3134 isl_basic_map_free(bmap);
3135 return isl_basic_map_universe(space);
3137 bmap = isl_basic_map_cow(bmap);
3138 if (!bmap)
3139 return NULL;
3141 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3142 return isl_basic_map_free(bmap);
3144 first += isl_basic_map_offset(bmap, type) - 1;
3146 for (i = bmap->n_eq - 1; i >= 0; --i) {
3147 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3148 continue;
3149 if (isl_basic_map_drop_equality(bmap, i) < 0)
3150 return isl_basic_map_free(bmap);
3153 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3154 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3155 continue;
3156 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3157 return isl_basic_map_free(bmap);
3160 bmap = isl_basic_map_add_known_div_constraints(bmap);
3161 return bmap;
3164 /* Drop all constraints in bset that do not involve any of the dimensions
3165 * first to first + n - 1 of the given type.
3167 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3168 __isl_take isl_basic_set *bset,
3169 enum isl_dim_type type, unsigned first, unsigned n)
3171 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3172 type, first, n);
3175 /* Drop all constraints in bmap that involve any of the dimensions
3176 * first to first + n - 1 of the given type.
3178 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3179 __isl_take isl_basic_map *bmap,
3180 enum isl_dim_type type, unsigned first, unsigned n)
3182 if (!bmap)
3183 return NULL;
3184 if (n == 0)
3185 return bmap;
3187 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3188 return isl_basic_map_free(bmap);
3190 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3191 first += isl_basic_map_offset(bmap, type) - 1;
3192 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3193 bmap = isl_basic_map_add_known_div_constraints(bmap);
3194 return bmap;
3197 /* Drop all constraints in bset that involve any of the dimensions
3198 * first to first + n - 1 of the given type.
3200 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3201 __isl_take isl_basic_set *bset,
3202 enum isl_dim_type type, unsigned first, unsigned n)
3204 return isl_basic_map_drop_constraints_involving_dims(bset,
3205 type, first, n);
3208 /* Drop constraints from "map" by applying "drop" to each basic map.
3210 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3211 enum isl_dim_type type, unsigned first, unsigned n,
3212 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3213 enum isl_dim_type type, unsigned first, unsigned n))
3215 int i;
3217 if (isl_map_check_range(map, type, first, n) < 0)
3218 return isl_map_free(map);
3220 map = isl_map_cow(map);
3221 if (!map)
3222 return NULL;
3224 for (i = 0; i < map->n; ++i) {
3225 map->p[i] = drop(map->p[i], type, first, n);
3226 if (!map->p[i])
3227 return isl_map_free(map);
3230 if (map->n > 1)
3231 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3233 return map;
3236 /* Drop all constraints in map that involve any of the dimensions
3237 * first to first + n - 1 of the given type.
3239 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3240 __isl_take isl_map *map,
3241 enum isl_dim_type type, unsigned first, unsigned n)
3243 if (n == 0)
3244 return map;
3245 return drop_constraints(map, type, first, n,
3246 &isl_basic_map_drop_constraints_involving_dims);
3249 /* Drop all constraints in "map" that do not involve any of the dimensions
3250 * first to first + n - 1 of the given type.
3252 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3253 __isl_take isl_map *map,
3254 enum isl_dim_type type, unsigned first, unsigned n)
3256 if (n == 0) {
3257 isl_space *space = isl_map_get_space(map);
3258 isl_map_free(map);
3259 return isl_map_universe(space);
3261 return drop_constraints(map, type, first, n,
3262 &isl_basic_map_drop_constraints_not_involving_dims);
3265 /* Drop all constraints in set that involve any of the dimensions
3266 * first to first + n - 1 of the given type.
3268 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3269 __isl_take isl_set *set,
3270 enum isl_dim_type type, unsigned first, unsigned n)
3272 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3275 /* Drop all constraints in "set" that do not involve any of the dimensions
3276 * first to first + n - 1 of the given type.
3278 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3279 __isl_take isl_set *set,
3280 enum isl_dim_type type, unsigned first, unsigned n)
3282 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3285 /* Does local variable "div" of "bmap" have a complete explicit representation?
3286 * Having a complete explicit representation requires not only
3287 * an explicit representation, but also that all local variables
3288 * that appear in this explicit representation in turn have
3289 * a complete explicit representation.
3291 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3293 int i;
3294 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3295 isl_bool marked;
3297 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3298 if (marked < 0 || marked)
3299 return isl_bool_not(marked);
3301 for (i = bmap->n_div - 1; i >= 0; --i) {
3302 isl_bool known;
3304 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3305 continue;
3306 known = isl_basic_map_div_is_known(bmap, i);
3307 if (known < 0 || !known)
3308 return known;
3311 return isl_bool_true;
3314 /* Remove all divs that are unknown or defined in terms of unknown divs.
3316 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3317 __isl_take isl_basic_map *bmap)
3319 int i;
3321 if (!bmap)
3322 return NULL;
3324 for (i = bmap->n_div - 1; i >= 0; --i) {
3325 if (isl_basic_map_div_is_known(bmap, i))
3326 continue;
3327 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3328 if (!bmap)
3329 return NULL;
3330 i = bmap->n_div;
3333 return bmap;
3336 /* Remove all divs that are unknown or defined in terms of unknown divs.
3338 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3339 __isl_take isl_basic_set *bset)
3341 return isl_basic_map_remove_unknown_divs(bset);
3344 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3346 int i;
3348 if (!map)
3349 return NULL;
3350 if (map->n == 0)
3351 return map;
3353 map = isl_map_cow(map);
3354 if (!map)
3355 return NULL;
3357 for (i = 0; i < map->n; ++i) {
3358 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3359 if (!map->p[i])
3360 goto error;
3362 return map;
3363 error:
3364 isl_map_free(map);
3365 return NULL;
3368 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3370 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3373 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3374 __isl_take isl_basic_set *bset,
3375 enum isl_dim_type type, unsigned first, unsigned n)
3377 isl_basic_map *bmap = bset_to_bmap(bset);
3378 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3379 return bset_from_bmap(bmap);
3382 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3383 enum isl_dim_type type, unsigned first, unsigned n)
3385 int i;
3387 if (n == 0)
3388 return map;
3390 map = isl_map_cow(map);
3391 if (isl_map_check_range(map, type, first, n) < 0)
3392 return isl_map_free(map);
3394 for (i = 0; i < map->n; ++i) {
3395 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3396 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3397 if (!map->p[i])
3398 goto error;
3400 map = isl_map_drop(map, type, first, n);
3401 return map;
3402 error:
3403 isl_map_free(map);
3404 return NULL;
3407 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3408 enum isl_dim_type type, unsigned first, unsigned n)
3410 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3411 type, first, n));
3414 /* Project out n inputs starting at first using Fourier-Motzkin */
3415 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3416 unsigned first, unsigned n)
3418 return isl_map_remove_dims(map, isl_dim_in, first, n);
3421 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3422 FILE *out, int indent)
3424 isl_printer *p;
3426 if (!bset) {
3427 fprintf(out, "null basic set\n");
3428 return;
3431 fprintf(out, "%*s", indent, "");
3432 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3433 bset->ref, bset->dim->nparam, bset->dim->n_out,
3434 bset->extra, bset->flags);
3436 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3437 p = isl_printer_set_dump(p, 1);
3438 p = isl_printer_set_indent(p, indent);
3439 p = isl_printer_start_line(p);
3440 p = isl_printer_print_basic_set(p, bset);
3441 p = isl_printer_end_line(p);
3442 isl_printer_free(p);
3445 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3446 FILE *out, int indent)
3448 isl_printer *p;
3450 if (!bmap) {
3451 fprintf(out, "null basic map\n");
3452 return;
3455 fprintf(out, "%*s", indent, "");
3456 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3457 "flags: %x, n_name: %d\n",
3458 bmap->ref,
3459 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3460 bmap->extra, bmap->flags, bmap->dim->n_id);
3462 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3463 p = isl_printer_set_dump(p, 1);
3464 p = isl_printer_set_indent(p, indent);
3465 p = isl_printer_start_line(p);
3466 p = isl_printer_print_basic_map(p, bmap);
3467 p = isl_printer_end_line(p);
3468 isl_printer_free(p);
3471 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3472 unsigned pos)
3474 isl_size total;
3476 total = isl_basic_map_dim(bmap, isl_dim_all);
3477 if (total < 0)
3478 return isl_basic_map_free(bmap);
3479 if (pos >= bmap->n_ineq)
3480 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3481 "invalid position", return isl_basic_map_free(bmap));
3482 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3483 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3484 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3485 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3486 return bmap;
3489 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3490 unsigned flags)
3492 if (isl_space_check_is_set(space) < 0)
3493 goto error;
3494 return isl_map_alloc_space(space, n, flags);
3495 error:
3496 isl_space_free(space);
3497 return NULL;
3500 /* Make sure "map" has room for at least "n" more basic maps.
3502 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3504 int i;
3505 struct isl_map *grown = NULL;
3507 if (!map)
3508 return NULL;
3509 isl_assert(map->ctx, n >= 0, goto error);
3510 if (map->n + n <= map->size)
3511 return map;
3512 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3513 if (!grown)
3514 goto error;
3515 for (i = 0; i < map->n; ++i) {
3516 grown->p[i] = isl_basic_map_copy(map->p[i]);
3517 if (!grown->p[i])
3518 goto error;
3519 grown->n++;
3521 isl_map_free(map);
3522 return grown;
3523 error:
3524 isl_map_free(grown);
3525 isl_map_free(map);
3526 return NULL;
3529 /* Make sure "set" has room for at least "n" more basic sets.
3531 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3533 return set_from_map(isl_map_grow(set_to_map(set), n));
3536 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3538 return isl_map_from_basic_map(bset);
3541 /* This function performs the same operation as isl_set_from_basic_set,
3542 * but is considered as a function on an isl_basic_set when exported.
3544 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3546 return isl_set_from_basic_set(bset);
3549 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3551 struct isl_map *map;
3553 if (!bmap)
3554 return NULL;
3556 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3557 return isl_map_add_basic_map(map, bmap);
3560 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3561 __isl_take isl_basic_set *bset)
3563 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3564 bset_to_bmap(bset)));
3567 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3569 return isl_map_free(set);
3572 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3574 int i;
3576 if (!set) {
3577 fprintf(out, "null set\n");
3578 return;
3581 fprintf(out, "%*s", indent, "");
3582 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3583 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3584 set->flags);
3585 for (i = 0; i < set->n; ++i) {
3586 fprintf(out, "%*s", indent, "");
3587 fprintf(out, "basic set %d:\n", i);
3588 isl_basic_set_print_internal(set->p[i], out, indent+4);
3592 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3594 int i;
3596 if (!map) {
3597 fprintf(out, "null map\n");
3598 return;
3601 fprintf(out, "%*s", indent, "");
3602 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3603 "flags: %x, n_name: %d\n",
3604 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3605 map->dim->n_out, map->flags, map->dim->n_id);
3606 for (i = 0; i < map->n; ++i) {
3607 fprintf(out, "%*s", indent, "");
3608 fprintf(out, "basic map %d:\n", i);
3609 isl_basic_map_print_internal(map->p[i], out, indent+4);
3613 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3615 static isl_stat isl_basic_map_check_compatible_domain(
3616 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3618 isl_bool ok;
3620 ok = isl_basic_map_compatible_domain(bmap, bset);
3621 if (ok < 0)
3622 return isl_stat_error;
3623 if (!ok)
3624 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3625 "incompatible spaces", return isl_stat_error);
3627 return isl_stat_ok;
3630 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3631 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3633 struct isl_basic_map *bmap_domain;
3634 isl_size dim;
3636 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3637 goto error;
3639 dim = isl_basic_set_dim(bset, isl_dim_set);
3640 if (dim < 0)
3641 goto error;
3642 if (dim != 0 &&
3643 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3644 goto error;
3646 bmap = isl_basic_map_cow(bmap);
3647 if (!bmap)
3648 goto error;
3649 bmap = isl_basic_map_extend(bmap,
3650 bset->n_div, bset->n_eq, bset->n_ineq);
3651 bmap_domain = isl_basic_map_from_domain(bset);
3652 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3654 bmap = isl_basic_map_simplify(bmap);
3655 return isl_basic_map_finalize(bmap);
3656 error:
3657 isl_basic_map_free(bmap);
3658 isl_basic_set_free(bset);
3659 return NULL;
3662 /* Check that the space of "bset" is the same as that of the range of "bmap".
3664 static isl_stat isl_basic_map_check_compatible_range(
3665 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3667 isl_bool ok;
3669 ok = isl_basic_map_compatible_range(bmap, bset);
3670 if (ok < 0)
3671 return isl_stat_error;
3672 if (!ok)
3673 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3674 "incompatible spaces", return isl_stat_error);
3676 return isl_stat_ok;
3679 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3680 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3682 struct isl_basic_map *bmap_range;
3683 isl_size dim;
3685 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3686 goto error;
3688 dim = isl_basic_set_dim(bset, isl_dim_set);
3689 if (dim < 0)
3690 goto error;
3691 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3692 goto error;
3694 if (isl_basic_set_plain_is_universe(bset)) {
3695 isl_basic_set_free(bset);
3696 return bmap;
3699 bmap = isl_basic_map_cow(bmap);
3700 if (!bmap)
3701 goto error;
3702 bmap = isl_basic_map_extend(bmap,
3703 bset->n_div, bset->n_eq, bset->n_ineq);
3704 bmap_range = bset_to_bmap(bset);
3705 bmap = add_constraints(bmap, bmap_range, 0, 0);
3707 bmap = isl_basic_map_simplify(bmap);
3708 return isl_basic_map_finalize(bmap);
3709 error:
3710 isl_basic_map_free(bmap);
3711 isl_basic_set_free(bset);
3712 return NULL;
3715 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3716 __isl_keep isl_vec *vec)
3718 int i;
3719 isl_size total;
3720 isl_int s;
3722 total = isl_basic_map_dim(bmap, isl_dim_all);
3723 if (total < 0 || !vec)
3724 return isl_bool_error;
3726 if (1 + total != vec->size)
3727 return isl_bool_false;
3729 isl_int_init(s);
3731 for (i = 0; i < bmap->n_eq; ++i) {
3732 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3733 if (!isl_int_is_zero(s)) {
3734 isl_int_clear(s);
3735 return isl_bool_false;
3739 for (i = 0; i < bmap->n_ineq; ++i) {
3740 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3741 if (isl_int_is_neg(s)) {
3742 isl_int_clear(s);
3743 return isl_bool_false;
3747 isl_int_clear(s);
3749 return isl_bool_true;
3752 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3753 __isl_keep isl_vec *vec)
3755 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3758 __isl_give isl_basic_map *isl_basic_map_intersect(
3759 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3761 struct isl_vec *sample = NULL;
3762 isl_space *space1, *space2;
3763 isl_size dim1, dim2, nparam1, nparam2;
3765 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3766 goto error;
3767 space1 = isl_basic_map_peek_space(bmap1);
3768 space2 = isl_basic_map_peek_space(bmap2);
3769 dim1 = isl_space_dim(space1, isl_dim_all);
3770 dim2 = isl_space_dim(space2, isl_dim_all);
3771 nparam1 = isl_space_dim(space1, isl_dim_param);
3772 nparam2 = isl_space_dim(space2, isl_dim_param);
3773 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3774 goto error;
3775 if (dim1 == nparam1 && dim2 != nparam2)
3776 return isl_basic_map_intersect(bmap2, bmap1);
3778 if (dim2 != nparam2 &&
3779 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3780 goto error;
3782 if (isl_basic_map_plain_is_empty(bmap1)) {
3783 isl_basic_map_free(bmap2);
3784 return bmap1;
3786 if (isl_basic_map_plain_is_empty(bmap2)) {
3787 isl_basic_map_free(bmap1);
3788 return bmap2;
3791 if (bmap1->sample &&
3792 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3793 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3794 sample = isl_vec_copy(bmap1->sample);
3795 else if (bmap2->sample &&
3796 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3797 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3798 sample = isl_vec_copy(bmap2->sample);
3800 bmap1 = isl_basic_map_cow(bmap1);
3801 if (!bmap1)
3802 goto error;
3803 bmap1 = isl_basic_map_extend(bmap1,
3804 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3805 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3807 if (!bmap1)
3808 isl_vec_free(sample);
3809 else if (sample) {
3810 isl_vec_free(bmap1->sample);
3811 bmap1->sample = sample;
3814 bmap1 = isl_basic_map_simplify(bmap1);
3815 return isl_basic_map_finalize(bmap1);
3816 error:
3817 if (sample)
3818 isl_vec_free(sample);
3819 isl_basic_map_free(bmap1);
3820 isl_basic_map_free(bmap2);
3821 return NULL;
3824 __isl_give isl_basic_set *isl_basic_set_intersect(
3825 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3827 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3828 bset_to_bmap(bset2)));
3831 /* Intersect the parameter domain of "bmap" with "bset".
3833 * isl_basic_map_intersect handles this as a special case.
3835 __isl_give isl_basic_map *isl_basic_map_intersect_params(
3836 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3838 return isl_basic_map_intersect(bmap, bset);
3841 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3842 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3844 isl_basic_map *bmap;
3846 bmap = bset_to_bmap(bset1);
3847 bmap = isl_basic_map_intersect_params(bmap, bset2);
3848 return bset_from_bmap(bmap);
3851 /* Does "map" consist of a single disjunct, without any local variables?
3853 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3855 isl_size n_div;
3857 if (!map)
3858 return isl_bool_error;
3859 if (map->n != 1)
3860 return isl_bool_false;
3861 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3862 if (n_div < 0)
3863 return isl_bool_error;
3864 if (n_div != 0)
3865 return isl_bool_false;
3866 return isl_bool_true;
3869 /* Check that "map" consists of a single disjunct, without any local variables.
3871 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3873 isl_bool ok;
3875 ok = is_convex_no_locals(map);
3876 if (ok < 0)
3877 return isl_stat_error;
3878 if (ok)
3879 return isl_stat_ok;
3881 isl_die(isl_map_get_ctx(map), isl_error_internal,
3882 "unexpectedly not convex or involving local variables",
3883 return isl_stat_error);
3886 /* Special case of isl_map_intersect, where both map1 and map2
3887 * are convex, without any divs and such that either map1 or map2
3888 * contains a single constraint. This constraint is then simply
3889 * added to the other map.
3891 static __isl_give isl_map *map_intersect_add_constraint(
3892 __isl_take isl_map *map1, __isl_take isl_map *map2)
3894 if (check_convex_no_locals(map1) < 0 ||
3895 check_convex_no_locals(map2) < 0)
3896 goto error;
3898 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3899 return isl_map_intersect(map2, map1);
3901 map1 = isl_map_cow(map1);
3902 if (!map1)
3903 goto error;
3904 if (isl_map_plain_is_empty(map1)) {
3905 isl_map_free(map2);
3906 return map1;
3908 if (map2->p[0]->n_eq == 1)
3909 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3910 else
3911 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3912 map2->p[0]->ineq[0]);
3914 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3915 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3916 if (!map1->p[0])
3917 goto error;
3919 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3920 isl_basic_map_free(map1->p[0]);
3921 map1->n = 0;
3924 isl_map_free(map2);
3926 map1 = isl_map_unmark_normalized(map1);
3927 return map1;
3928 error:
3929 isl_map_free(map1);
3930 isl_map_free(map2);
3931 return NULL;
3934 /* map2 may be either a parameter domain or a map living in the same
3935 * space as map1.
3937 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3938 __isl_take isl_map *map2)
3940 unsigned flags = 0;
3941 isl_bool equal;
3942 isl_map *result;
3943 int i, j;
3944 isl_size dim2, nparam2;
3946 if (!map1 || !map2)
3947 goto error;
3949 if ((isl_map_plain_is_empty(map1) ||
3950 isl_map_plain_is_universe(map2)) &&
3951 isl_space_is_equal(map1->dim, map2->dim)) {
3952 isl_map_free(map2);
3953 return map1;
3955 if ((isl_map_plain_is_empty(map2) ||
3956 isl_map_plain_is_universe(map1)) &&
3957 isl_space_is_equal(map1->dim, map2->dim)) {
3958 isl_map_free(map1);
3959 return map2;
3962 if (is_convex_no_locals(map1) == isl_bool_true &&
3963 is_convex_no_locals(map2) == isl_bool_true &&
3964 isl_space_is_equal(map1->dim, map2->dim) &&
3965 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3966 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3967 return map_intersect_add_constraint(map1, map2);
3969 equal = isl_map_plain_is_equal(map1, map2);
3970 if (equal < 0)
3971 goto error;
3972 if (equal) {
3973 isl_map_free(map2);
3974 return map1;
3977 dim2 = isl_map_dim(map2, isl_dim_all);
3978 nparam2 = isl_map_dim(map2, isl_dim_param);
3979 if (dim2 < 0 || nparam2 < 0)
3980 goto error;
3981 if (dim2 != nparam2)
3982 isl_assert(map1->ctx,
3983 isl_space_is_equal(map1->dim, map2->dim), goto error);
3985 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3986 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3987 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3989 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3990 map1->n * map2->n, flags);
3991 if (!result)
3992 goto error;
3993 for (i = 0; i < map1->n; ++i)
3994 for (j = 0; j < map2->n; ++j) {
3995 struct isl_basic_map *part;
3996 part = isl_basic_map_intersect(
3997 isl_basic_map_copy(map1->p[i]),
3998 isl_basic_map_copy(map2->p[j]));
3999 if (isl_basic_map_is_empty(part) < 0)
4000 part = isl_basic_map_free(part);
4001 result = isl_map_add_basic_map(result, part);
4002 if (!result)
4003 goto error;
4005 isl_map_free(map1);
4006 isl_map_free(map2);
4007 return result;
4008 error:
4009 isl_map_free(map1);
4010 isl_map_free(map2);
4011 return NULL;
4014 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
4015 __isl_take isl_map *map2)
4017 if (isl_map_check_equal_space(map1, map2) < 0)
4018 goto error;
4019 return map_intersect_internal(map1, map2);
4020 error:
4021 isl_map_free(map1);
4022 isl_map_free(map2);
4023 return NULL;
4026 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
4027 __isl_take isl_map *map2)
4029 isl_map_align_params_bin(&map1, &map2);
4030 return map_intersect(map1, map2);
4033 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
4034 __isl_take isl_set *set2)
4036 return set_from_map(isl_map_intersect(set_to_map(set1),
4037 set_to_map(set2)));
4040 /* map_intersect_internal accepts intersections
4041 * with parameter domains, so we can just call that function.
4043 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
4044 __isl_take isl_set *params)
4046 isl_map_align_params_set(&map, &params);
4047 return map_intersect_internal(map, params);
4050 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4051 __isl_take isl_set *params)
4053 return isl_map_intersect_params(set, params);
4056 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4058 isl_space *space;
4059 unsigned pos;
4060 isl_size n1, n2;
4062 if (!bmap)
4063 return NULL;
4064 bmap = isl_basic_map_cow(bmap);
4065 if (!bmap)
4066 return NULL;
4067 space = isl_space_reverse(isl_space_copy(bmap->dim));
4068 pos = isl_basic_map_offset(bmap, isl_dim_in);
4069 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4070 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4071 if (n1 < 0 || n2 < 0)
4072 bmap = isl_basic_map_free(bmap);
4073 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4074 return isl_basic_map_reset_space(bmap, space);
4077 /* Given a basic map where the tuple of type "type" is a wrapped map,
4078 * swap domain and range of that wrapped map.
4080 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4081 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4083 isl_space *space;
4084 isl_size offset, n1, n2;
4086 space = isl_basic_map_peek_space(bmap);
4087 offset = isl_basic_map_var_offset(bmap, type);
4088 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4089 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4090 if (offset < 0 || n1 < 0 || n2 < 0)
4091 return isl_basic_map_free(bmap);
4093 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4095 space = isl_basic_map_take_space(bmap);
4096 space = isl_space_reverse_wrapped(space, type);
4097 bmap = isl_basic_map_restore_space(bmap, space);
4099 return bmap;
4102 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4103 * (B -> A) -> C.
4105 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4106 __isl_take isl_basic_map *bmap)
4108 isl_space *space;
4110 space = isl_basic_map_peek_space(bmap);
4111 if (isl_space_check_domain_is_wrapping(space) < 0)
4112 return isl_basic_map_free(bmap);
4113 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4115 return bmap;
4118 /* Given a basic map A -> (B -> C), return the corresponding basic map
4119 * A -> (C -> B).
4121 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4122 __isl_take isl_basic_map *bmap)
4124 isl_space *space;
4126 space = isl_basic_map_peek_space(bmap);
4127 if (isl_space_check_range_is_wrapping(space) < 0)
4128 return isl_basic_map_free(bmap);
4129 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4131 return bmap;
4134 /* Given a basic map that is actually a basic set (A -> B),
4135 * return the corresponding basic set (B -> A) as a basic map.
4137 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4138 __isl_take isl_basic_map *bmap)
4140 isl_space *space;
4142 space = isl_basic_map_peek_space(bmap);
4143 if (isl_space_check_is_wrapping(space) < 0)
4144 return isl_basic_map_free(bmap);
4145 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4147 return bmap;
4150 static __isl_give isl_basic_map *basic_map_space_reset(
4151 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4153 isl_space *space;
4155 if (!bmap)
4156 return NULL;
4157 if (!isl_space_is_named_or_nested(bmap->dim, type))
4158 return bmap;
4160 space = isl_basic_map_get_space(bmap);
4161 space = isl_space_reset(space, type);
4162 bmap = isl_basic_map_reset_space(bmap, space);
4163 return bmap;
4166 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4167 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4168 unsigned pos, unsigned n)
4170 isl_bool rational, is_empty;
4171 isl_space *res_space;
4172 struct isl_basic_map *res;
4173 struct isl_dim_map *dim_map;
4174 isl_size total;
4175 unsigned off;
4176 enum isl_dim_type t;
4178 if (n == 0)
4179 return basic_map_space_reset(bmap, type);
4181 is_empty = isl_basic_map_plain_is_empty(bmap);
4182 total = isl_basic_map_dim(bmap, isl_dim_all);
4183 if (is_empty < 0 || total < 0)
4184 return isl_basic_map_free(bmap);
4185 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4186 type, pos, n);
4187 if (!res_space)
4188 return isl_basic_map_free(bmap);
4189 if (is_empty) {
4190 isl_basic_map_free(bmap);
4191 return isl_basic_map_empty(res_space);
4194 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4195 off = 0;
4196 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4197 isl_size dim;
4199 if (t != type) {
4200 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4201 } else {
4202 isl_size size = isl_basic_map_dim(bmap, t);
4203 if (size < 0)
4204 dim_map = isl_dim_map_free(dim_map);
4205 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4206 0, pos, off);
4207 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4208 pos, size - pos, off + pos + n);
4210 dim = isl_space_dim(res_space, t);
4211 if (dim < 0)
4212 dim_map = isl_dim_map_free(dim_map);
4213 off += dim;
4215 isl_dim_map_div(dim_map, bmap, off);
4217 res = isl_basic_map_alloc_space(res_space,
4218 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4219 rational = isl_basic_map_is_rational(bmap);
4220 if (rational < 0)
4221 res = isl_basic_map_free(res);
4222 if (rational)
4223 res = isl_basic_map_set_rational(res);
4224 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4225 return isl_basic_map_finalize(res);
4228 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4229 __isl_take isl_basic_set *bset,
4230 enum isl_dim_type type, unsigned pos, unsigned n)
4232 return isl_basic_map_insert_dims(bset, type, pos, n);
4235 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4236 enum isl_dim_type type, unsigned n)
4238 isl_size dim;
4240 dim = isl_basic_map_dim(bmap, type);
4241 if (dim < 0)
4242 return isl_basic_map_free(bmap);
4243 return isl_basic_map_insert_dims(bmap, type, dim, n);
4246 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4247 enum isl_dim_type type, unsigned n)
4249 if (!bset)
4250 return NULL;
4251 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4252 return isl_basic_map_add_dims(bset, type, n);
4253 error:
4254 isl_basic_set_free(bset);
4255 return NULL;
4258 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4259 enum isl_dim_type type)
4261 isl_space *space;
4263 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4264 return map;
4266 space = isl_map_get_space(map);
4267 space = isl_space_reset(space, type);
4268 map = isl_map_reset_space(map, space);
4269 return map;
4272 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4273 enum isl_dim_type type, unsigned pos, unsigned n)
4275 int i;
4276 isl_space *space;
4278 if (n == 0)
4279 return map_space_reset(map, type);
4281 map = isl_map_cow(map);
4282 if (!map)
4283 return NULL;
4285 for (i = 0; i < map->n; ++i) {
4286 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4287 if (!map->p[i])
4288 goto error;
4291 space = isl_map_take_space(map);
4292 space = isl_space_insert_dims(space, type, pos, n);
4293 map = isl_map_restore_space(map, space);
4295 return map;
4296 error:
4297 isl_map_free(map);
4298 return NULL;
4301 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4302 enum isl_dim_type type, unsigned pos, unsigned n)
4304 return isl_map_insert_dims(set, type, pos, n);
4307 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4308 enum isl_dim_type type, unsigned n)
4310 isl_size dim;
4312 dim = isl_map_dim(map, type);
4313 if (dim < 0)
4314 return isl_map_free(map);
4315 return isl_map_insert_dims(map, type, dim, n);
4318 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4319 enum isl_dim_type type, unsigned n)
4321 if (!set)
4322 return NULL;
4323 isl_assert(set->ctx, type != isl_dim_in, goto error);
4324 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4325 error:
4326 isl_set_free(set);
4327 return NULL;
4330 __isl_give isl_basic_map *isl_basic_map_move_dims(
4331 __isl_take isl_basic_map *bmap,
4332 enum isl_dim_type dst_type, unsigned dst_pos,
4333 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4335 isl_space *space;
4336 struct isl_dim_map *dim_map;
4337 struct isl_basic_map *res;
4338 enum isl_dim_type t;
4339 isl_size total;
4340 unsigned off;
4342 if (!bmap)
4343 return NULL;
4344 if (n == 0) {
4345 bmap = isl_basic_map_reset(bmap, src_type);
4346 bmap = isl_basic_map_reset(bmap, dst_type);
4347 return bmap;
4350 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4351 return isl_basic_map_free(bmap);
4353 if (dst_type == src_type && dst_pos == src_pos)
4354 return bmap;
4356 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4358 if (pos(bmap->dim, dst_type) + dst_pos ==
4359 pos(bmap->dim, src_type) + src_pos +
4360 ((src_type < dst_type) ? n : 0)) {
4361 space = isl_basic_map_take_space(bmap);
4362 space = isl_space_move_dims(space, dst_type, dst_pos,
4363 src_type, src_pos, n);
4364 bmap = isl_basic_map_restore_space(bmap, space);
4365 bmap = isl_basic_map_finalize(bmap);
4367 return bmap;
4370 total = isl_basic_map_dim(bmap, isl_dim_all);
4371 if (total < 0)
4372 return isl_basic_map_free(bmap);
4373 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4375 off = 0;
4376 space = isl_basic_map_peek_space(bmap);
4377 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4378 isl_size size = isl_space_dim(space, t);
4379 if (size < 0)
4380 dim_map = isl_dim_map_free(dim_map);
4381 if (t == dst_type) {
4382 isl_dim_map_dim_range(dim_map, space, t,
4383 0, dst_pos, off);
4384 off += dst_pos;
4385 isl_dim_map_dim_range(dim_map, space, src_type,
4386 src_pos, n, off);
4387 off += n;
4388 isl_dim_map_dim_range(dim_map, space, t,
4389 dst_pos, size - dst_pos, off);
4390 off += size - dst_pos;
4391 } else if (t == src_type) {
4392 isl_dim_map_dim_range(dim_map, space, t,
4393 0, src_pos, off);
4394 off += src_pos;
4395 isl_dim_map_dim_range(dim_map, space, t,
4396 src_pos + n, size - src_pos - n, off);
4397 off += size - src_pos - n;
4398 } else {
4399 isl_dim_map_dim(dim_map, space, t, off);
4400 off += size;
4403 isl_dim_map_div(dim_map, bmap, off);
4405 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4406 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4407 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4408 space = isl_basic_map_take_space(bmap);
4409 space = isl_space_move_dims(space, dst_type, dst_pos,
4410 src_type, src_pos, n);
4411 bmap = isl_basic_map_restore_space(bmap, space);
4412 if (!bmap)
4413 goto error;
4415 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4416 bmap = isl_basic_map_gauss(bmap, NULL);
4417 bmap = isl_basic_map_finalize(bmap);
4419 return bmap;
4420 error:
4421 isl_basic_map_free(bmap);
4422 return NULL;
4425 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4426 enum isl_dim_type dst_type, unsigned dst_pos,
4427 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4429 isl_basic_map *bmap = bset_to_bmap(bset);
4430 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4431 src_type, src_pos, n);
4432 return bset_from_bmap(bmap);
4435 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4436 enum isl_dim_type dst_type, unsigned dst_pos,
4437 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4439 if (!set)
4440 return NULL;
4441 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4442 return set_from_map(isl_map_move_dims(set_to_map(set),
4443 dst_type, dst_pos, src_type, src_pos, n));
4444 error:
4445 isl_set_free(set);
4446 return NULL;
4449 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4450 enum isl_dim_type dst_type, unsigned dst_pos,
4451 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4453 int i;
4454 isl_space *space;
4456 if (n == 0) {
4457 map = isl_map_reset(map, src_type);
4458 map = isl_map_reset(map, dst_type);
4459 return map;
4462 if (isl_map_check_range(map, src_type, src_pos, n))
4463 return isl_map_free(map);
4465 if (dst_type == src_type && dst_pos == src_pos)
4466 return map;
4468 isl_assert(map->ctx, dst_type != src_type, goto error);
4470 map = isl_map_cow(map);
4471 if (!map)
4472 return NULL;
4474 for (i = 0; i < map->n; ++i) {
4475 map->p[i] = isl_basic_map_move_dims(map->p[i],
4476 dst_type, dst_pos,
4477 src_type, src_pos, n);
4478 if (!map->p[i])
4479 goto error;
4482 space = isl_map_take_space(map);
4483 space = isl_space_move_dims(space, dst_type, dst_pos,
4484 src_type, src_pos, n);
4485 map = isl_map_restore_space(map, space);
4487 return map;
4488 error:
4489 isl_map_free(map);
4490 return NULL;
4493 /* Move the specified dimensions to the last columns right before
4494 * the divs. Don't change the dimension specification of bmap.
4495 * That's the responsibility of the caller.
4497 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4498 enum isl_dim_type type, unsigned first, unsigned n)
4500 isl_space *space;
4501 struct isl_dim_map *dim_map;
4502 struct isl_basic_map *res;
4503 enum isl_dim_type t;
4504 isl_size total;
4505 unsigned off;
4507 if (!bmap)
4508 return NULL;
4509 if (isl_basic_map_offset(bmap, type) + first + n ==
4510 isl_basic_map_offset(bmap, isl_dim_div))
4511 return bmap;
4513 total = isl_basic_map_dim(bmap, isl_dim_all);
4514 if (total < 0)
4515 return isl_basic_map_free(bmap);
4516 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4518 off = 0;
4519 space = isl_basic_map_peek_space(bmap);
4520 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4521 isl_size size = isl_space_dim(space, t);
4522 if (size < 0)
4523 dim_map = isl_dim_map_free(dim_map);
4524 if (t == type) {
4525 isl_dim_map_dim_range(dim_map, space, t,
4526 0, first, off);
4527 off += first;
4528 isl_dim_map_dim_range(dim_map, space, t,
4529 first, n, total - bmap->n_div - n);
4530 isl_dim_map_dim_range(dim_map, space, t,
4531 first + n, size - (first + n), off);
4532 off += size - (first + n);
4533 } else {
4534 isl_dim_map_dim(dim_map, space, t, off);
4535 off += size;
4538 isl_dim_map_div(dim_map, bmap, off + n);
4540 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4541 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4542 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4543 return res;
4546 /* Insert "n" rows in the divs of "bmap".
4548 * The number of columns is not changed, which means that the last
4549 * dimensions of "bmap" are being reintepreted as the new divs.
4550 * The space of "bmap" is not adjusted, however, which means
4551 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4552 * from the space of "bmap" is the responsibility of the caller.
4554 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4555 int n)
4557 int i;
4558 size_t row_size;
4559 isl_int **new_div;
4560 isl_int *old;
4562 bmap = isl_basic_map_cow(bmap);
4563 if (!bmap)
4564 return NULL;
4566 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4567 old = bmap->block2.data;
4568 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4569 (bmap->extra + n) * (1 + row_size));
4570 if (!bmap->block2.data)
4571 return isl_basic_map_free(bmap);
4572 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4573 if (!new_div)
4574 return isl_basic_map_free(bmap);
4575 for (i = 0; i < n; ++i) {
4576 new_div[i] = bmap->block2.data +
4577 (bmap->extra + i) * (1 + row_size);
4578 isl_seq_clr(new_div[i], 1 + row_size);
4580 for (i = 0; i < bmap->extra; ++i)
4581 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4582 free(bmap->div);
4583 bmap->div = new_div;
4584 bmap->n_div += n;
4585 bmap->extra += n;
4587 return bmap;
4590 /* Drop constraints from "bmap" that only involve the variables
4591 * of "type" in the range [first, first + n] that are not related
4592 * to any of the variables outside that interval.
4593 * These constraints cannot influence the values for the variables
4594 * outside the interval, except in case they cause "bmap" to be empty.
4595 * Only drop the constraints if "bmap" is known to be non-empty.
4597 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4598 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4599 unsigned first, unsigned n)
4601 int i;
4602 int *groups;
4603 isl_size dim, n_div;
4604 isl_bool non_empty;
4606 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4607 if (non_empty < 0)
4608 return isl_basic_map_free(bmap);
4609 if (!non_empty)
4610 return bmap;
4612 dim = isl_basic_map_dim(bmap, isl_dim_all);
4613 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4614 if (dim < 0 || n_div < 0)
4615 return isl_basic_map_free(bmap);
4616 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4617 if (!groups)
4618 return isl_basic_map_free(bmap);
4619 first += isl_basic_map_offset(bmap, type) - 1;
4620 for (i = 0; i < first; ++i)
4621 groups[i] = -1;
4622 for (i = first + n; i < dim - n_div; ++i)
4623 groups[i] = -1;
4625 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4627 return bmap;
4630 /* Turn the n dimensions of type type, starting at first
4631 * into existentially quantified variables.
4633 * If a subset of the projected out variables are unrelated
4634 * to any of the variables that remain, then the constraints
4635 * involving this subset are simply dropped first.
4637 __isl_give isl_basic_map *isl_basic_map_project_out(
4638 __isl_take isl_basic_map *bmap,
4639 enum isl_dim_type type, unsigned first, unsigned n)
4641 isl_bool empty;
4642 isl_space *space;
4644 if (n == 0)
4645 return basic_map_space_reset(bmap, type);
4646 if (type == isl_dim_div)
4647 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4648 "cannot project out existentially quantified variables",
4649 return isl_basic_map_free(bmap));
4651 empty = isl_basic_map_plain_is_empty(bmap);
4652 if (empty < 0)
4653 return isl_basic_map_free(bmap);
4654 if (empty)
4655 bmap = isl_basic_map_set_to_empty(bmap);
4657 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4658 if (!bmap)
4659 return NULL;
4661 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4662 return isl_basic_map_remove_dims(bmap, type, first, n);
4664 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4665 return isl_basic_map_free(bmap);
4667 bmap = move_last(bmap, type, first, n);
4668 bmap = isl_basic_map_cow(bmap);
4669 bmap = insert_div_rows(bmap, n);
4671 space = isl_basic_map_take_space(bmap);
4672 space = isl_space_drop_dims(space, type, first, n);
4673 bmap = isl_basic_map_restore_space(bmap, space);
4674 bmap = isl_basic_map_simplify(bmap);
4675 bmap = isl_basic_map_drop_redundant_divs(bmap);
4676 return isl_basic_map_finalize(bmap);
4679 /* Turn the n dimensions of type type, starting at first
4680 * into existentially quantified variables.
4682 __isl_give isl_basic_set *isl_basic_set_project_out(
4683 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4684 unsigned first, unsigned n)
4686 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4687 type, first, n));
4690 /* Turn the n dimensions of type type, starting at first
4691 * into existentially quantified variables.
4693 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4694 enum isl_dim_type type, unsigned first, unsigned n)
4696 int i;
4697 isl_space *space;
4699 if (n == 0)
4700 return map_space_reset(map, type);
4702 if (isl_map_check_range(map, type, first, n) < 0)
4703 return isl_map_free(map);
4705 map = isl_map_cow(map);
4706 if (!map)
4707 return NULL;
4709 for (i = 0; i < map->n; ++i) {
4710 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4711 if (!map->p[i])
4712 goto error;
4715 if (map->n > 1)
4716 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4717 map = isl_map_unmark_normalized(map);
4719 space = isl_map_take_space(map);
4720 space = isl_space_drop_dims(space, type, first, n);
4721 map = isl_map_restore_space(map, space);
4723 return map;
4724 error:
4725 isl_map_free(map);
4726 return NULL;
4729 #undef TYPE
4730 #define TYPE isl_map
4731 #include "isl_project_out_all_params_templ.c"
4732 #include "isl_project_out_param_templ.c"
4734 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4735 * into existentially quantified variables.
4737 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4738 enum isl_dim_type type, unsigned first, unsigned n)
4740 isl_size dim;
4742 dim = isl_map_dim(map, type);
4743 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4744 return isl_map_free(map);
4745 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4746 map = isl_map_project_out(map, type, 0, first);
4747 return map;
4750 /* Turn the n dimensions of type type, starting at first
4751 * into existentially quantified variables.
4753 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4754 enum isl_dim_type type, unsigned first, unsigned n)
4756 return set_from_map(isl_map_project_out(set_to_map(set),
4757 type, first, n));
4760 /* If "set" involves a parameter with identifier "id",
4761 * then turn it into an existentially quantified variable.
4763 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4764 __isl_take isl_id *id)
4766 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4769 /* If "set" involves any of the parameters with identifiers in "list",
4770 * then turn them into existentially quantified variables.
4772 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4773 __isl_take isl_id_list *list)
4775 isl_map *map;
4777 map = set_to_map(set);
4778 map = isl_map_project_out_param_id_list(map, list);
4779 return set_from_map(map);
4782 /* Project out all parameters from "set" by existentially quantifying
4783 * over them.
4785 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4787 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4790 /* Return a map that projects the elements in "set" onto their
4791 * "n" set dimensions starting at "first".
4792 * "type" should be equal to isl_dim_set.
4794 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4795 enum isl_dim_type type, unsigned first, unsigned n)
4797 int i;
4798 isl_map *map;
4800 if (type != isl_dim_set)
4801 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4802 "only set dimensions can be projected out", goto error);
4803 if (isl_set_check_range(set, type, first, n) < 0)
4804 return isl_set_free(set);
4806 map = isl_map_from_domain(set);
4807 map = isl_map_add_dims(map, isl_dim_out, n);
4808 for (i = 0; i < n; ++i)
4809 map = isl_map_equate(map, isl_dim_in, first + i,
4810 isl_dim_out, i);
4811 return map;
4812 error:
4813 isl_set_free(set);
4814 return NULL;
4817 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4818 unsigned n)
4820 int i, j;
4821 isl_size total;
4823 total = isl_basic_map_dim(bmap, isl_dim_all);
4824 if (total < 0)
4825 return isl_basic_map_free(bmap);
4826 for (i = 0; i < n; ++i) {
4827 j = isl_basic_map_alloc_div(bmap);
4828 if (j < 0)
4829 goto error;
4830 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4832 return bmap;
4833 error:
4834 isl_basic_map_free(bmap);
4835 return NULL;
4838 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4840 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4841 __isl_keep isl_basic_map *bmap2)
4843 isl_space *space1, *space2;
4845 space1 = isl_basic_map_peek_space(bmap1);
4846 space2 = isl_basic_map_peek_space(bmap2);
4847 return isl_space_tuple_is_equal(space1, isl_dim_out,
4848 space2, isl_dim_in);
4851 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4853 static isl_stat isl_basic_map_check_applies_range(
4854 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4856 isl_bool equal;
4858 equal = isl_basic_map_applies_range(bmap1, bmap2);
4859 if (equal < 0)
4860 return isl_stat_error;
4861 if (!equal)
4862 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4863 "spaces don't match", return isl_stat_error);
4864 return isl_stat_ok;
4867 __isl_give isl_basic_map *isl_basic_map_apply_range(
4868 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4870 isl_space *space_result = NULL;
4871 struct isl_basic_map *bmap;
4872 isl_size n_in, n_out, n, nparam;
4873 unsigned total, pos;
4874 struct isl_dim_map *dim_map1, *dim_map2;
4876 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4877 goto error;
4878 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4879 goto error;
4881 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4882 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4883 n = isl_basic_map_dim(bmap1, isl_dim_out);
4884 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4885 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4886 goto error;
4888 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4889 isl_basic_map_get_space(bmap2));
4891 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4892 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4893 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4894 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4895 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4896 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4897 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4898 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4899 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4900 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4901 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4903 bmap = isl_basic_map_alloc_space(space_result,
4904 bmap1->n_div + bmap2->n_div + n,
4905 bmap1->n_eq + bmap2->n_eq,
4906 bmap1->n_ineq + bmap2->n_ineq);
4907 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4908 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4909 bmap = add_divs(bmap, n);
4910 bmap = isl_basic_map_simplify(bmap);
4911 bmap = isl_basic_map_drop_redundant_divs(bmap);
4912 return isl_basic_map_finalize(bmap);
4913 error:
4914 isl_basic_map_free(bmap1);
4915 isl_basic_map_free(bmap2);
4916 return NULL;
4919 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4920 __isl_take isl_basic_map *bmap)
4922 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4923 goto error;
4925 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4926 bmap));
4927 error:
4928 isl_basic_set_free(bset);
4929 isl_basic_map_free(bmap);
4930 return NULL;
4933 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4934 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4936 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4937 goto error;
4938 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4939 bmap2->dim, isl_dim_in))
4940 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4941 "spaces don't match", goto error);
4943 bmap1 = isl_basic_map_reverse(bmap1);
4944 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4945 return isl_basic_map_reverse(bmap1);
4946 error:
4947 isl_basic_map_free(bmap1);
4948 isl_basic_map_free(bmap2);
4949 return NULL;
4952 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4953 * A \cap B -> f(A) + f(B)
4955 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4956 __isl_take isl_basic_map *bmap2)
4958 isl_size n_in, n_out, nparam;
4959 unsigned total, pos;
4960 struct isl_basic_map *bmap = NULL;
4961 struct isl_dim_map *dim_map1, *dim_map2;
4962 int i;
4964 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4965 goto error;
4967 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4968 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4969 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4970 if (nparam < 0 || n_in < 0 || n_out < 0)
4971 goto error;
4973 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4974 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4975 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4976 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4977 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4978 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4979 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4980 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4981 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4982 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4983 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4985 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4986 bmap1->n_div + bmap2->n_div + 2 * n_out,
4987 bmap1->n_eq + bmap2->n_eq + n_out,
4988 bmap1->n_ineq + bmap2->n_ineq);
4989 for (i = 0; i < n_out; ++i) {
4990 int j = isl_basic_map_alloc_equality(bmap);
4991 if (j < 0)
4992 goto error;
4993 isl_seq_clr(bmap->eq[j], 1+total);
4994 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4995 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4996 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4998 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4999 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5000 bmap = add_divs(bmap, 2 * n_out);
5002 bmap = isl_basic_map_simplify(bmap);
5003 return isl_basic_map_finalize(bmap);
5004 error:
5005 isl_basic_map_free(bmap);
5006 isl_basic_map_free(bmap1);
5007 isl_basic_map_free(bmap2);
5008 return NULL;
5011 /* Given two maps A -> f(A) and B -> g(B), construct a map
5012 * A \cap B -> f(A) + f(B)
5014 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
5015 __isl_take isl_map *map2)
5017 struct isl_map *result;
5018 int i, j;
5020 if (isl_map_check_equal_space(map1, map2) < 0)
5021 goto error;
5023 result = isl_map_alloc_space(isl_space_copy(map1->dim),
5024 map1->n * map2->n, 0);
5025 if (!result)
5026 goto error;
5027 for (i = 0; i < map1->n; ++i)
5028 for (j = 0; j < map2->n; ++j) {
5029 struct isl_basic_map *part;
5030 part = isl_basic_map_sum(
5031 isl_basic_map_copy(map1->p[i]),
5032 isl_basic_map_copy(map2->p[j]));
5033 if (isl_basic_map_is_empty(part))
5034 isl_basic_map_free(part);
5035 else
5036 result = isl_map_add_basic_map(result, part);
5037 if (!result)
5038 goto error;
5040 isl_map_free(map1);
5041 isl_map_free(map2);
5042 return result;
5043 error:
5044 isl_map_free(map1);
5045 isl_map_free(map2);
5046 return NULL;
5049 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
5050 __isl_take isl_set *set2)
5052 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5055 /* Given a basic map A -> f(A), construct A -> -f(A).
5057 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5059 int i, j;
5060 unsigned off;
5061 isl_size n;
5063 bmap = isl_basic_map_cow(bmap);
5064 n = isl_basic_map_dim(bmap, isl_dim_out);
5065 if (n < 0)
5066 return isl_basic_map_free(bmap);
5068 off = isl_basic_map_offset(bmap, isl_dim_out);
5069 for (i = 0; i < bmap->n_eq; ++i)
5070 for (j = 0; j < n; ++j)
5071 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5072 for (i = 0; i < bmap->n_ineq; ++i)
5073 for (j = 0; j < n; ++j)
5074 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5075 for (i = 0; i < bmap->n_div; ++i)
5076 for (j = 0; j < n; ++j)
5077 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5078 bmap = isl_basic_map_gauss(bmap, NULL);
5079 return isl_basic_map_finalize(bmap);
5082 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5084 return isl_basic_map_neg(bset);
5087 /* Given a map A -> f(A), construct A -> -f(A).
5089 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5091 int i;
5093 map = isl_map_cow(map);
5094 if (!map)
5095 return NULL;
5097 for (i = 0; i < map->n; ++i) {
5098 map->p[i] = isl_basic_map_neg(map->p[i]);
5099 if (!map->p[i])
5100 goto error;
5103 return map;
5104 error:
5105 isl_map_free(map);
5106 return NULL;
5109 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5111 return set_from_map(isl_map_neg(set_to_map(set)));
5114 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5115 * A -> floor(f(A)/d).
5117 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5118 isl_int d)
5120 isl_size n_in, n_out, nparam;
5121 unsigned total, pos;
5122 struct isl_basic_map *result = NULL;
5123 struct isl_dim_map *dim_map;
5124 int i;
5126 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5127 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5128 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5129 if (nparam < 0 || n_in < 0 || n_out < 0)
5130 return isl_basic_map_free(bmap);
5132 total = nparam + n_in + n_out + bmap->n_div + n_out;
5133 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5134 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5135 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5136 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5137 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5139 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5140 bmap->n_div + n_out,
5141 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5142 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5143 result = add_divs(result, n_out);
5144 for (i = 0; i < n_out; ++i) {
5145 int j;
5146 j = isl_basic_map_alloc_inequality(result);
5147 if (j < 0)
5148 goto error;
5149 isl_seq_clr(result->ineq[j], 1+total);
5150 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5151 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5152 j = isl_basic_map_alloc_inequality(result);
5153 if (j < 0)
5154 goto error;
5155 isl_seq_clr(result->ineq[j], 1+total);
5156 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5157 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5158 isl_int_sub_ui(result->ineq[j][0], d, 1);
5161 result = isl_basic_map_simplify(result);
5162 return isl_basic_map_finalize(result);
5163 error:
5164 isl_basic_map_free(result);
5165 return NULL;
5168 /* Given a map A -> f(A) and an integer d, construct a map
5169 * A -> floor(f(A)/d).
5171 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5173 int i;
5175 map = isl_map_cow(map);
5176 if (!map)
5177 return NULL;
5179 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5180 for (i = 0; i < map->n; ++i) {
5181 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5182 if (!map->p[i])
5183 goto error;
5185 map = isl_map_unmark_normalized(map);
5187 return map;
5188 error:
5189 isl_map_free(map);
5190 return NULL;
5193 /* Given a map A -> f(A) and an integer d, construct a map
5194 * A -> floor(f(A)/d).
5196 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5197 __isl_take isl_val *d)
5199 if (!map || !d)
5200 goto error;
5201 if (!isl_val_is_int(d))
5202 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5203 "expecting integer denominator", goto error);
5204 map = isl_map_floordiv(map, d->n);
5205 isl_val_free(d);
5206 return map;
5207 error:
5208 isl_map_free(map);
5209 isl_val_free(d);
5210 return NULL;
5213 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5214 unsigned pos)
5216 int i;
5217 isl_size nparam;
5218 isl_size n_in;
5219 isl_size total;
5221 total = isl_basic_map_dim(bmap, isl_dim_all);
5222 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5223 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5224 if (total < 0 || nparam < 0 || n_in < 0)
5225 return isl_basic_map_free(bmap);
5226 i = isl_basic_map_alloc_equality(bmap);
5227 if (i < 0)
5228 goto error;
5229 isl_seq_clr(bmap->eq[i], 1 + total);
5230 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5231 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5232 return isl_basic_map_finalize(bmap);
5233 error:
5234 isl_basic_map_free(bmap);
5235 return NULL;
5238 /* Add a constraint to "bmap" expressing i_pos < o_pos
5240 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5241 unsigned pos)
5243 int i;
5244 isl_size nparam;
5245 isl_size n_in;
5246 isl_size total;
5248 total = isl_basic_map_dim(bmap, isl_dim_all);
5249 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5250 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5251 if (total < 0 || nparam < 0 || n_in < 0)
5252 return isl_basic_map_free(bmap);
5253 i = isl_basic_map_alloc_inequality(bmap);
5254 if (i < 0)
5255 goto error;
5256 isl_seq_clr(bmap->ineq[i], 1 + total);
5257 isl_int_set_si(bmap->ineq[i][0], -1);
5258 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5259 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5260 return isl_basic_map_finalize(bmap);
5261 error:
5262 isl_basic_map_free(bmap);
5263 return NULL;
5266 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5268 static __isl_give isl_basic_map *var_less_or_equal(
5269 __isl_take isl_basic_map *bmap, unsigned pos)
5271 int i;
5272 isl_size nparam;
5273 isl_size n_in;
5274 isl_size total;
5276 total = isl_basic_map_dim(bmap, isl_dim_all);
5277 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5278 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5279 if (total < 0 || nparam < 0 || n_in < 0)
5280 return isl_basic_map_free(bmap);
5281 i = isl_basic_map_alloc_inequality(bmap);
5282 if (i < 0)
5283 goto error;
5284 isl_seq_clr(bmap->ineq[i], 1 + total);
5285 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5286 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5287 return isl_basic_map_finalize(bmap);
5288 error:
5289 isl_basic_map_free(bmap);
5290 return NULL;
5293 /* Add a constraint to "bmap" expressing i_pos > o_pos
5295 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5296 unsigned pos)
5298 int i;
5299 isl_size nparam;
5300 isl_size n_in;
5301 isl_size total;
5303 total = isl_basic_map_dim(bmap, isl_dim_all);
5304 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5305 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5306 if (total < 0 || nparam < 0 || n_in < 0)
5307 return isl_basic_map_free(bmap);
5308 i = isl_basic_map_alloc_inequality(bmap);
5309 if (i < 0)
5310 goto error;
5311 isl_seq_clr(bmap->ineq[i], 1 + total);
5312 isl_int_set_si(bmap->ineq[i][0], -1);
5313 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5314 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5315 return isl_basic_map_finalize(bmap);
5316 error:
5317 isl_basic_map_free(bmap);
5318 return NULL;
5321 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5323 static __isl_give isl_basic_map *var_more_or_equal(
5324 __isl_take isl_basic_map *bmap, unsigned pos)
5326 int i;
5327 isl_size nparam;
5328 isl_size n_in;
5329 isl_size total;
5331 total = isl_basic_map_dim(bmap, isl_dim_all);
5332 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5333 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5334 if (total < 0 || nparam < 0 || n_in < 0)
5335 return isl_basic_map_free(bmap);
5336 i = isl_basic_map_alloc_inequality(bmap);
5337 if (i < 0)
5338 goto error;
5339 isl_seq_clr(bmap->ineq[i], 1 + total);
5340 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5341 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5342 return isl_basic_map_finalize(bmap);
5343 error:
5344 isl_basic_map_free(bmap);
5345 return NULL;
5348 __isl_give isl_basic_map *isl_basic_map_equal(
5349 __isl_take isl_space *space, unsigned n_equal)
5351 int i;
5352 struct isl_basic_map *bmap;
5353 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5354 if (!bmap)
5355 return NULL;
5356 for (i = 0; i < n_equal && bmap; ++i)
5357 bmap = var_equal(bmap, i);
5358 return isl_basic_map_finalize(bmap);
5361 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5363 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5364 unsigned pos)
5366 int i;
5367 struct isl_basic_map *bmap;
5368 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5369 if (!bmap)
5370 return NULL;
5371 for (i = 0; i < pos && bmap; ++i)
5372 bmap = var_equal(bmap, i);
5373 if (bmap)
5374 bmap = var_less(bmap, pos);
5375 return isl_basic_map_finalize(bmap);
5378 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5380 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5381 __isl_take isl_space *space, unsigned pos)
5383 int i;
5384 isl_basic_map *bmap;
5386 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5387 for (i = 0; i < pos; ++i)
5388 bmap = var_equal(bmap, i);
5389 bmap = var_less_or_equal(bmap, pos);
5390 return isl_basic_map_finalize(bmap);
5393 /* Return a relation on "space" expressing i_pos > o_pos
5395 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5396 unsigned pos)
5398 int i;
5399 struct isl_basic_map *bmap;
5400 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5401 if (!bmap)
5402 return NULL;
5403 for (i = 0; i < pos && bmap; ++i)
5404 bmap = var_equal(bmap, i);
5405 if (bmap)
5406 bmap = var_more(bmap, pos);
5407 return isl_basic_map_finalize(bmap);
5410 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5412 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5413 __isl_take isl_space *space, unsigned pos)
5415 int i;
5416 isl_basic_map *bmap;
5418 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5419 for (i = 0; i < pos; ++i)
5420 bmap = var_equal(bmap, i);
5421 bmap = var_more_or_equal(bmap, pos);
5422 return isl_basic_map_finalize(bmap);
5425 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5426 unsigned n, int equal)
5428 struct isl_map *map;
5429 int i;
5431 if (n == 0 && equal)
5432 return isl_map_universe(space);
5434 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5436 for (i = 0; i + 1 < n; ++i)
5437 map = isl_map_add_basic_map(map,
5438 isl_basic_map_less_at(isl_space_copy(space), i));
5439 if (n > 0) {
5440 if (equal)
5441 map = isl_map_add_basic_map(map,
5442 isl_basic_map_less_or_equal_at(space, n - 1));
5443 else
5444 map = isl_map_add_basic_map(map,
5445 isl_basic_map_less_at(space, n - 1));
5446 } else
5447 isl_space_free(space);
5449 return map;
5452 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5454 if (!space)
5455 return NULL;
5456 return map_lex_lte_first(space, space->n_out, equal);
5459 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5460 unsigned n)
5462 return map_lex_lte_first(space, n, 0);
5465 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5466 unsigned n)
5468 return map_lex_lte_first(space, n, 1);
5471 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5473 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5476 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5478 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5481 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5482 unsigned n, int equal)
5484 struct isl_map *map;
5485 int i;
5487 if (n == 0 && equal)
5488 return isl_map_universe(space);
5490 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5492 for (i = 0; i + 1 < n; ++i)
5493 map = isl_map_add_basic_map(map,
5494 isl_basic_map_more_at(isl_space_copy(space), i));
5495 if (n > 0) {
5496 if (equal)
5497 map = isl_map_add_basic_map(map,
5498 isl_basic_map_more_or_equal_at(space, n - 1));
5499 else
5500 map = isl_map_add_basic_map(map,
5501 isl_basic_map_more_at(space, n - 1));
5502 } else
5503 isl_space_free(space);
5505 return map;
5508 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5510 if (!space)
5511 return NULL;
5512 return map_lex_gte_first(space, space->n_out, equal);
5515 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5516 unsigned n)
5518 return map_lex_gte_first(space, n, 0);
5521 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5522 unsigned n)
5524 return map_lex_gte_first(space, n, 1);
5527 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5529 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5532 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5534 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5537 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5538 __isl_take isl_set *set2)
5540 isl_map *map;
5541 map = isl_map_lex_le(isl_set_get_space(set1));
5542 map = isl_map_intersect_domain(map, set1);
5543 map = isl_map_intersect_range(map, set2);
5544 return map;
5547 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5548 __isl_take isl_set *set2)
5550 isl_map *map;
5551 map = isl_map_lex_lt(isl_set_get_space(set1));
5552 map = isl_map_intersect_domain(map, set1);
5553 map = isl_map_intersect_range(map, set2);
5554 return map;
5557 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5558 __isl_take isl_set *set2)
5560 isl_map *map;
5561 map = isl_map_lex_ge(isl_set_get_space(set1));
5562 map = isl_map_intersect_domain(map, set1);
5563 map = isl_map_intersect_range(map, set2);
5564 return map;
5567 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5568 __isl_take isl_set *set2)
5570 isl_map *map;
5571 map = isl_map_lex_gt(isl_set_get_space(set1));
5572 map = isl_map_intersect_domain(map, set1);
5573 map = isl_map_intersect_range(map, set2);
5574 return map;
5577 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5578 __isl_take isl_map *map2)
5580 isl_map *map;
5581 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5582 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5583 map = isl_map_apply_range(map, isl_map_reverse(map2));
5584 return map;
5587 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5588 __isl_take isl_map *map2)
5590 isl_map *map;
5591 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5592 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5593 map = isl_map_apply_range(map, isl_map_reverse(map2));
5594 return map;
5597 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5598 __isl_take isl_map *map2)
5600 isl_map *map;
5601 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5602 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5603 map = isl_map_apply_range(map, isl_map_reverse(map2));
5604 return map;
5607 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5608 __isl_take isl_map *map2)
5610 isl_map *map;
5611 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5612 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5613 map = isl_map_apply_range(map, isl_map_reverse(map2));
5614 return map;
5617 /* For the div d = floor(f/m) at position "div", add the constraint
5619 * f - m d >= 0
5621 static __isl_give isl_basic_map *add_upper_div_constraint(
5622 __isl_take isl_basic_map *bmap, unsigned div)
5624 int i;
5625 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5626 isl_size n_div;
5627 unsigned pos;
5629 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5630 if (v_div < 0 || n_div < 0)
5631 return isl_basic_map_free(bmap);
5632 pos = v_div + div;
5633 i = isl_basic_map_alloc_inequality(bmap);
5634 if (i < 0)
5635 return isl_basic_map_free(bmap);
5636 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5637 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5639 return bmap;
5642 /* For the div d = floor(f/m) at position "div", add the constraint
5644 * -(f-(m-1)) + m d >= 0
5646 static __isl_give isl_basic_map *add_lower_div_constraint(
5647 __isl_take isl_basic_map *bmap, unsigned div)
5649 int i;
5650 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5651 isl_size n_div;
5652 unsigned pos;
5654 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5655 if (v_div < 0 || n_div < 0)
5656 return isl_basic_map_free(bmap);
5657 pos = v_div + div;
5658 i = isl_basic_map_alloc_inequality(bmap);
5659 if (i < 0)
5660 return isl_basic_map_free(bmap);
5661 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5662 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5663 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5664 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5666 return bmap;
5669 /* For the div d = floor(f/m) at position "pos", add the constraints
5671 * f - m d >= 0
5672 * -(f-(m-1)) + m d >= 0
5674 * Note that the second constraint is the negation of
5676 * f - m d >= m
5678 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5679 __isl_take isl_basic_map *bmap, unsigned pos)
5681 bmap = add_upper_div_constraint(bmap, pos);
5682 bmap = add_lower_div_constraint(bmap, pos);
5683 return bmap;
5686 /* For each known div d = floor(f/m), add the constraints
5688 * f - m d >= 0
5689 * -(f-(m-1)) + m d >= 0
5691 * Remove duplicate constraints in case of some these div constraints
5692 * already appear in "bmap".
5694 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5695 __isl_take isl_basic_map *bmap)
5697 isl_size n_div;
5699 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5700 if (n_div < 0)
5701 return isl_basic_map_free(bmap);
5702 if (n_div == 0)
5703 return bmap;
5705 bmap = add_known_div_constraints(bmap);
5706 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5707 bmap = isl_basic_map_finalize(bmap);
5708 return bmap;
5711 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5713 * In particular, if this div is of the form d = floor(f/m),
5714 * then add the constraint
5716 * f - m d >= 0
5718 * if sign < 0 or the constraint
5720 * -(f-(m-1)) + m d >= 0
5722 * if sign > 0.
5724 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5725 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5727 if (sign < 0)
5728 return add_upper_div_constraint(bmap, div);
5729 else
5730 return add_lower_div_constraint(bmap, div);
5733 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5734 __isl_take isl_basic_map *bmap)
5736 isl_space *space;
5738 if (!bmap)
5739 goto error;
5740 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5741 bmap->n_div == 0 &&
5742 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5743 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5744 return bset_from_bmap(bmap);
5745 bmap = isl_basic_map_cow(bmap);
5746 if (!bmap)
5747 return NULL;
5748 space = isl_basic_map_take_space(bmap);
5749 space = isl_space_underlying(space, bmap->n_div);
5750 bmap = isl_basic_map_restore_space(bmap, space);
5751 if (!bmap)
5752 return NULL;
5753 bmap->extra -= bmap->n_div;
5754 bmap->n_div = 0;
5755 bmap = isl_basic_map_finalize(bmap);
5756 return bset_from_bmap(bmap);
5757 error:
5758 isl_basic_map_free(bmap);
5759 return NULL;
5762 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5763 __isl_take isl_basic_set *bset)
5765 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5768 /* Replace each element in "list" by the result of applying
5769 * isl_basic_map_underlying_set to the element.
5771 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5772 __isl_take isl_basic_map_list *list)
5774 int i;
5775 isl_size n;
5777 n = isl_basic_map_list_n_basic_map(list);
5778 if (n < 0)
5779 goto error;
5781 for (i = 0; i < n; ++i) {
5782 isl_basic_map *bmap;
5783 isl_basic_set *bset;
5785 bmap = isl_basic_map_list_get_basic_map(list, i);
5786 bset = isl_basic_set_underlying_set(bmap);
5787 list = isl_basic_set_list_set_basic_set(list, i, bset);
5790 return list;
5791 error:
5792 isl_basic_map_list_free(list);
5793 return NULL;
5796 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5797 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5799 struct isl_basic_map *bmap;
5800 struct isl_ctx *ctx;
5801 isl_size dim, bmap_total;
5802 unsigned total;
5803 int i;
5805 if (!bset || !like)
5806 goto error;
5807 ctx = bset->ctx;
5808 if (isl_basic_set_check_no_params(bset) < 0 ||
5809 isl_basic_set_check_no_locals(bset) < 0)
5810 goto error;
5811 dim = isl_basic_set_dim(bset, isl_dim_set);
5812 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5813 if (dim < 0 || bmap_total < 0)
5814 goto error;
5815 isl_assert(ctx, dim == bmap_total, goto error);
5816 if (like->n_div == 0) {
5817 isl_space *space = isl_basic_map_get_space(like);
5818 isl_basic_map_free(like);
5819 return isl_basic_map_reset_space(bset, space);
5821 bset = isl_basic_set_cow(bset);
5822 if (!bset)
5823 goto error;
5824 total = dim + bset->extra;
5825 bmap = bset_to_bmap(bset);
5826 isl_space_free(isl_basic_map_take_space(bmap));
5827 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5828 if (!bmap)
5829 goto error;
5830 bmap->n_div = like->n_div;
5831 bmap->extra += like->n_div;
5832 if (bmap->extra) {
5833 unsigned ltotal;
5834 isl_int **div;
5835 ltotal = total - bmap->extra + like->extra;
5836 if (ltotal > total)
5837 ltotal = total;
5838 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5839 bmap->extra * (1 + 1 + total));
5840 if (isl_blk_is_error(bmap->block2))
5841 goto error;
5842 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5843 if (!div)
5844 goto error;
5845 bmap->div = div;
5846 for (i = 0; i < bmap->extra; ++i)
5847 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5848 for (i = 0; i < like->n_div; ++i) {
5849 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5850 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5852 bmap = isl_basic_map_add_known_div_constraints(bmap);
5854 isl_basic_map_free(like);
5855 bmap = isl_basic_map_simplify(bmap);
5856 bmap = isl_basic_map_finalize(bmap);
5857 return bmap;
5858 error:
5859 isl_basic_map_free(like);
5860 isl_basic_set_free(bset);
5861 return NULL;
5864 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5865 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5867 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5868 bset_to_bmap(like)));
5871 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5873 int i;
5875 map = isl_map_cow(map);
5876 if (!map)
5877 return NULL;
5878 map->dim = isl_space_cow(map->dim);
5879 if (!map->dim)
5880 goto error;
5882 for (i = 1; i < map->n; ++i)
5883 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5884 goto error);
5885 for (i = 0; i < map->n; ++i) {
5886 map->p[i] = bset_to_bmap(
5887 isl_basic_map_underlying_set(map->p[i]));
5888 if (!map->p[i])
5889 goto error;
5891 if (map->n == 0)
5892 map->dim = isl_space_underlying(map->dim, 0);
5893 else {
5894 isl_space_free(map->dim);
5895 map->dim = isl_space_copy(map->p[0]->dim);
5897 if (!map->dim)
5898 goto error;
5899 return set_from_map(map);
5900 error:
5901 isl_map_free(map);
5902 return NULL;
5905 /* Replace the space of "bmap" by "space".
5907 * If the space of "bmap" is identical to "space" (including the identifiers
5908 * of the input and output dimensions), then simply return the original input.
5910 __isl_give isl_basic_map *isl_basic_map_reset_space(
5911 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5913 isl_bool equal;
5914 isl_space *bmap_space;
5916 bmap_space = isl_basic_map_peek_space(bmap);
5917 equal = isl_space_is_equal(bmap_space, space);
5918 if (equal >= 0 && equal)
5919 equal = isl_space_has_equal_ids(bmap_space, space);
5920 if (equal < 0)
5921 goto error;
5922 if (equal) {
5923 isl_space_free(space);
5924 return bmap;
5926 isl_space_free(isl_basic_map_take_space(bmap));
5927 bmap = isl_basic_map_restore_space(bmap, space);
5929 bmap = isl_basic_map_finalize(bmap);
5931 return bmap;
5932 error:
5933 isl_basic_map_free(bmap);
5934 isl_space_free(space);
5935 return NULL;
5938 __isl_give isl_basic_set *isl_basic_set_reset_space(
5939 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5941 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5942 space));
5945 /* Check that the total dimensions of "map" and "space" are the same.
5947 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5948 __isl_keep isl_space *space)
5950 isl_size dim1, dim2;
5952 dim1 = isl_map_dim(map, isl_dim_all);
5953 dim2 = isl_space_dim(space, isl_dim_all);
5954 if (dim1 < 0 || dim2 < 0)
5955 return isl_stat_error;
5956 if (dim1 == dim2)
5957 return isl_stat_ok;
5958 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5959 "total dimensions do not match", return isl_stat_error);
5962 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5963 __isl_take isl_space *space)
5965 int i;
5967 map = isl_map_cow(map);
5968 if (!map || !space)
5969 goto error;
5971 for (i = 0; i < map->n; ++i) {
5972 map->p[i] = isl_basic_map_reset_space(map->p[i],
5973 isl_space_copy(space));
5974 if (!map->p[i])
5975 goto error;
5977 isl_space_free(isl_map_take_space(map));
5978 map = isl_map_restore_space(map, space);
5980 return map;
5981 error:
5982 isl_map_free(map);
5983 isl_space_free(space);
5984 return NULL;
5987 /* Replace the space of "map" by "space", without modifying
5988 * the dimension of "map".
5990 * If the space of "map" is identical to "space" (including the identifiers
5991 * of the input and output dimensions), then simply return the original input.
5993 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5994 __isl_take isl_space *space)
5996 isl_bool equal;
5997 isl_space *map_space;
5999 map_space = isl_map_peek_space(map);
6000 equal = isl_space_is_equal(map_space, space);
6001 if (equal >= 0 && equal)
6002 equal = isl_space_has_equal_ids(map_space, space);
6003 if (equal < 0)
6004 goto error;
6005 if (equal) {
6006 isl_space_free(space);
6007 return map;
6009 if (check_map_space_equal_total_dim(map, space) < 0)
6010 goto error;
6011 return isl_map_reset_space(map, space);
6012 error:
6013 isl_map_free(map);
6014 isl_space_free(space);
6015 return NULL;
6018 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
6019 __isl_take isl_space *space)
6021 return set_from_map(isl_map_reset_space(set_to_map(set), space));
6024 /* Compute the parameter domain of the given basic set.
6026 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
6028 isl_bool is_params;
6029 isl_space *space;
6030 isl_size n;
6032 is_params = isl_basic_set_is_params(bset);
6033 if (is_params < 0)
6034 return isl_basic_set_free(bset);
6035 if (is_params)
6036 return bset;
6038 n = isl_basic_set_dim(bset, isl_dim_set);
6039 if (n < 0)
6040 return isl_basic_set_free(bset);
6041 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6042 space = isl_basic_set_get_space(bset);
6043 space = isl_space_params(space);
6044 bset = isl_basic_set_reset_space(bset, space);
6045 return bset;
6048 /* Construct a zero-dimensional basic set with the given parameter domain.
6050 __isl_give isl_basic_set *isl_basic_set_from_params(
6051 __isl_take isl_basic_set *bset)
6053 isl_space *space;
6054 space = isl_basic_set_get_space(bset);
6055 space = isl_space_set_from_params(space);
6056 bset = isl_basic_set_reset_space(bset, space);
6057 return bset;
6060 /* Compute the parameter domain of the given set.
6062 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6064 return isl_map_params(set_to_map(set));
6067 /* Construct a zero-dimensional set with the given parameter domain.
6069 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6071 isl_space *space;
6072 space = isl_set_get_space(set);
6073 space = isl_space_set_from_params(space);
6074 set = isl_set_reset_space(set, space);
6075 return set;
6078 /* Compute the parameter domain of the given map.
6080 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6082 isl_space *space;
6083 isl_size n_in, n_out;
6085 n_in = isl_map_dim(map, isl_dim_in);
6086 n_out = isl_map_dim(map, isl_dim_out);
6087 if (n_in < 0 || n_out < 0)
6088 return isl_map_free(map);
6089 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6090 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6091 space = isl_map_get_space(map);
6092 space = isl_space_params(space);
6093 map = isl_map_reset_space(map, space);
6094 return map;
6097 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6099 isl_space *space;
6100 isl_size n_out;
6102 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6103 if (n_out < 0)
6104 return isl_basic_map_free(bmap);
6105 space = isl_space_domain(isl_basic_map_get_space(bmap));
6107 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6109 return isl_basic_map_reset_space(bmap, space);
6112 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6114 if (!bmap)
6115 return isl_bool_error;
6116 return isl_space_may_be_set(bmap->dim);
6119 /* Is this basic map actually a set?
6120 * Users should never call this function. Outside of isl,
6121 * the type should indicate whether something is a set or a map.
6123 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6125 if (!bmap)
6126 return isl_bool_error;
6127 return isl_space_is_set(bmap->dim);
6130 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6132 isl_bool is_set;
6134 is_set = isl_basic_map_is_set(bmap);
6135 if (is_set < 0)
6136 goto error;
6137 if (is_set)
6138 return bmap;
6139 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6140 error:
6141 isl_basic_map_free(bmap);
6142 return NULL;
6145 __isl_give isl_basic_map *isl_basic_map_domain_map(
6146 __isl_take isl_basic_map *bmap)
6148 int i;
6149 isl_space *space;
6150 isl_basic_map *domain;
6151 isl_size nparam, n_in, n_out;
6153 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6154 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6155 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6156 if (nparam < 0 || n_in < 0 || n_out < 0)
6157 return isl_basic_map_free(bmap);
6159 space = isl_basic_map_get_space(bmap);
6160 space = isl_space_from_range(isl_space_domain(space));
6161 domain = isl_basic_map_universe(space);
6163 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6164 bmap = isl_basic_map_apply_range(bmap, domain);
6165 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6167 for (i = 0; i < n_in; ++i)
6168 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6169 isl_dim_out, i);
6171 bmap = isl_basic_map_gauss(bmap, NULL);
6172 return isl_basic_map_finalize(bmap);
6175 __isl_give isl_basic_map *isl_basic_map_range_map(
6176 __isl_take isl_basic_map *bmap)
6178 int i;
6179 isl_space *space;
6180 isl_basic_map *range;
6181 isl_size nparam, n_in, n_out;
6183 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6184 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6185 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6186 if (nparam < 0 || n_in < 0 || n_out < 0)
6187 return isl_basic_map_free(bmap);
6189 space = isl_basic_map_get_space(bmap);
6190 space = isl_space_from_range(isl_space_range(space));
6191 range = isl_basic_map_universe(space);
6193 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6194 bmap = isl_basic_map_apply_range(bmap, range);
6195 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6197 for (i = 0; i < n_out; ++i)
6198 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6199 isl_dim_out, i);
6201 bmap = isl_basic_map_gauss(bmap, NULL);
6202 return isl_basic_map_finalize(bmap);
6205 int isl_map_may_be_set(__isl_keep isl_map *map)
6207 if (!map)
6208 return -1;
6209 return isl_space_may_be_set(map->dim);
6212 /* Is this map actually a set?
6213 * Users should never call this function. Outside of isl,
6214 * the type should indicate whether something is a set or a map.
6216 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6218 if (!map)
6219 return isl_bool_error;
6220 return isl_space_is_set(map->dim);
6223 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6225 isl_space *space;
6226 isl_size n_in;
6228 n_in = isl_map_dim(map, isl_dim_in);
6229 if (n_in < 0)
6230 return set_from_map(isl_map_free(map));
6231 space = isl_space_range(isl_map_get_space(map));
6233 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6235 return set_from_map(isl_map_reset_space(map, space));
6238 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6239 * to each of its basic maps.
6241 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6242 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6243 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6245 int i;
6246 isl_space *space;
6248 map = isl_map_cow(map);
6249 if (!map)
6250 return NULL;
6252 for (i = 0; i < map->n; ++i) {
6253 map->p[i] = fn_bmap(map->p[i]);
6254 if (!map->p[i])
6255 return isl_map_free(map);
6257 map = isl_map_unmark_normalized(map);
6259 space = isl_map_take_space(map);
6260 space = fn_space(space);
6261 map = isl_map_restore_space(map, space);
6263 return map;
6266 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6268 return isl_map_transform(map, &isl_space_domain_map,
6269 &isl_basic_map_domain_map);
6272 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6274 return isl_map_transform(map, &isl_space_range_map,
6275 &isl_basic_map_range_map);
6278 /* Given a wrapped map of the form A[B -> C],
6279 * return the map A[B -> C] -> B.
6281 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6283 isl_id *id;
6284 isl_map *map;
6286 if (!set)
6287 return NULL;
6288 if (!isl_set_has_tuple_id(set))
6289 return isl_map_domain_map(isl_set_unwrap(set));
6291 id = isl_set_get_tuple_id(set);
6292 map = isl_map_domain_map(isl_set_unwrap(set));
6293 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6295 return map;
6298 __isl_give isl_basic_map *isl_basic_map_from_domain(
6299 __isl_take isl_basic_set *bset)
6301 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6304 __isl_give isl_basic_map *isl_basic_map_from_range(
6305 __isl_take isl_basic_set *bset)
6307 isl_space *space;
6308 space = isl_basic_set_get_space(bset);
6309 space = isl_space_from_range(space);
6310 bset = isl_basic_set_reset_space(bset, space);
6311 return bset_to_bmap(bset);
6314 /* Create a relation with the given set as range.
6315 * The domain of the created relation is a zero-dimensional
6316 * flat anonymous space.
6318 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6320 isl_space *space;
6321 space = isl_set_get_space(set);
6322 space = isl_space_from_range(space);
6323 set = isl_set_reset_space(set, space);
6324 return set_to_map(set);
6327 /* Create a relation with the given set as domain.
6328 * The range of the created relation is a zero-dimensional
6329 * flat anonymous space.
6331 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6333 return isl_map_reverse(isl_map_from_range(set));
6336 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6337 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6339 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6342 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6343 __isl_take isl_set *range)
6345 return isl_map_apply_range(isl_map_reverse(domain), range);
6348 /* Return a newly allocated isl_map with given space and flags and
6349 * room for "n" basic maps.
6350 * Make sure that all cached information is cleared.
6352 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6353 unsigned flags)
6355 struct isl_map *map;
6357 if (!space)
6358 return NULL;
6359 if (n < 0)
6360 isl_die(space->ctx, isl_error_internal,
6361 "negative number of basic maps", goto error);
6362 map = isl_calloc(space->ctx, struct isl_map,
6363 sizeof(struct isl_map) +
6364 (n - 1) * sizeof(struct isl_basic_map *));
6365 if (!map)
6366 goto error;
6368 map->ctx = space->ctx;
6369 isl_ctx_ref(map->ctx);
6370 map->ref = 1;
6371 map->size = n;
6372 map->n = 0;
6373 map->dim = space;
6374 map->flags = flags;
6375 return map;
6376 error:
6377 isl_space_free(space);
6378 return NULL;
6381 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6383 struct isl_basic_map *bmap;
6384 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6385 bmap = isl_basic_map_set_to_empty(bmap);
6386 return bmap;
6389 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6391 struct isl_basic_set *bset;
6392 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6393 bset = isl_basic_set_set_to_empty(bset);
6394 return bset;
6397 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6399 struct isl_basic_map *bmap;
6400 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6401 bmap = isl_basic_map_finalize(bmap);
6402 return bmap;
6405 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6407 struct isl_basic_set *bset;
6408 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6409 bset = isl_basic_set_finalize(bset);
6410 return bset;
6413 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6414 __isl_take isl_space *space)
6416 int i;
6417 isl_size total = isl_space_dim(space, isl_dim_all);
6418 isl_basic_map *bmap;
6420 if (total < 0)
6421 space = isl_space_free(space);
6422 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6423 for (i = 0; i < total; ++i) {
6424 int k = isl_basic_map_alloc_inequality(bmap);
6425 if (k < 0)
6426 goto error;
6427 isl_seq_clr(bmap->ineq[k], 1 + total);
6428 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6430 return bmap;
6431 error:
6432 isl_basic_map_free(bmap);
6433 return NULL;
6436 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6437 __isl_take isl_space *space)
6439 return isl_basic_map_nat_universe(space);
6442 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6444 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6447 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6449 return isl_map_nat_universe(space);
6452 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6454 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6457 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6459 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6462 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6464 struct isl_map *map;
6465 if (!space)
6466 return NULL;
6467 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6468 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6469 return map;
6472 /* This function performs the same operation as isl_map_universe,
6473 * but is considered as a function on an isl_space when exported.
6475 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6477 return isl_map_universe(space);
6480 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6482 struct isl_set *set;
6483 if (!space)
6484 return NULL;
6485 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6486 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6487 return set;
6490 /* This function performs the same operation as isl_set_universe,
6491 * but is considered as a function on an isl_space when exported.
6493 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6495 return isl_set_universe(space);
6498 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6500 int i;
6501 struct isl_map *dup;
6503 if (!map)
6504 return NULL;
6505 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6506 for (i = 0; i < map->n; ++i)
6507 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6508 return dup;
6511 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6512 __isl_take isl_basic_map *bmap)
6514 if (!bmap || !map)
6515 goto error;
6516 if (isl_basic_map_plain_is_empty(bmap)) {
6517 isl_basic_map_free(bmap);
6518 return map;
6520 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6521 goto error;
6522 isl_assert(map->ctx, map->n < map->size, goto error);
6523 map->p[map->n] = bmap;
6524 map->n++;
6525 map = isl_map_unmark_normalized(map);
6526 return map;
6527 error:
6528 if (map)
6529 isl_map_free(map);
6530 if (bmap)
6531 isl_basic_map_free(bmap);
6532 return NULL;
6535 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6537 int i;
6539 if (!map)
6540 return NULL;
6542 if (--map->ref > 0)
6543 return NULL;
6545 clear_caches(map);
6546 isl_ctx_deref(map->ctx);
6547 for (i = 0; i < map->n; ++i)
6548 isl_basic_map_free(map->p[i]);
6549 isl_space_free(map->dim);
6550 free(map);
6552 return NULL;
6555 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6556 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6558 int j;
6559 isl_size total;
6561 total = isl_basic_map_dim(bmap, isl_dim_all);
6562 if (total < 0)
6563 return isl_basic_map_free(bmap);
6565 bmap = isl_basic_map_cow(bmap);
6566 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6567 j = isl_basic_map_alloc_equality(bmap);
6568 if (j < 0)
6569 goto error;
6570 isl_seq_clr(bmap->eq[j] + 1, total);
6571 isl_int_set_si(bmap->eq[j][pos], -1);
6572 isl_int_set_si(bmap->eq[j][0], value);
6573 bmap = isl_basic_map_simplify(bmap);
6574 return isl_basic_map_finalize(bmap);
6575 error:
6576 isl_basic_map_free(bmap);
6577 return NULL;
6580 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6581 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6583 int j;
6584 isl_size total;
6586 total = isl_basic_map_dim(bmap, isl_dim_all);
6587 if (total < 0)
6588 return isl_basic_map_free(bmap);
6590 bmap = isl_basic_map_cow(bmap);
6591 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6592 j = isl_basic_map_alloc_equality(bmap);
6593 if (j < 0)
6594 goto error;
6595 isl_seq_clr(bmap->eq[j] + 1, total);
6596 isl_int_set_si(bmap->eq[j][pos], -1);
6597 isl_int_set(bmap->eq[j][0], value);
6598 bmap = isl_basic_map_simplify(bmap);
6599 return isl_basic_map_finalize(bmap);
6600 error:
6601 isl_basic_map_free(bmap);
6602 return NULL;
6605 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6606 enum isl_dim_type type, unsigned pos, int value)
6608 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6609 return isl_basic_map_free(bmap);
6610 return isl_basic_map_fix_pos_si(bmap,
6611 isl_basic_map_offset(bmap, type) + pos, value);
6614 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6615 enum isl_dim_type type, unsigned pos, isl_int value)
6617 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6618 return isl_basic_map_free(bmap);
6619 return isl_basic_map_fix_pos(bmap,
6620 isl_basic_map_offset(bmap, type) + pos, value);
6623 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6624 * to be equal to "v".
6626 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6627 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6629 if (!bmap || !v)
6630 goto error;
6631 if (!isl_val_is_int(v))
6632 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6633 "expecting integer value", goto error);
6634 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6635 goto error;
6636 pos += isl_basic_map_offset(bmap, type);
6637 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6638 isl_val_free(v);
6639 return bmap;
6640 error:
6641 isl_basic_map_free(bmap);
6642 isl_val_free(v);
6643 return NULL;
6646 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6647 * to be equal to "v".
6649 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6650 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6652 return isl_basic_map_fix_val(bset, type, pos, v);
6655 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6656 enum isl_dim_type type, unsigned pos, int value)
6658 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6659 type, pos, value));
6662 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6663 enum isl_dim_type type, unsigned pos, isl_int value)
6665 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6666 type, pos, value));
6669 /* Remove the basic map at position "i" from "map" if this basic map
6670 * is (obviously) empty.
6672 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6674 isl_bool empty;
6676 if (!map)
6677 return NULL;
6679 empty = isl_basic_map_plain_is_empty(map->p[i]);
6680 if (empty < 0)
6681 return isl_map_free(map);
6682 if (!empty)
6683 return map;
6685 isl_basic_map_free(map->p[i]);
6686 map->n--;
6687 if (i != map->n) {
6688 map->p[i] = map->p[map->n];
6689 map = isl_map_unmark_normalized(map);
6693 return map;
6696 /* Perform "fn" on each basic map of "map", where we may not be holding
6697 * the only reference to "map".
6698 * In particular, "fn" should be a semantics preserving operation
6699 * that we want to apply to all copies of "map". We therefore need
6700 * to be careful not to modify "map" in a way that breaks "map"
6701 * in case anything goes wrong.
6703 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6704 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6706 struct isl_basic_map *bmap;
6707 int i;
6709 if (!map)
6710 return NULL;
6712 for (i = map->n - 1; i >= 0; --i) {
6713 bmap = isl_basic_map_copy(map->p[i]);
6714 bmap = fn(bmap);
6715 if (!bmap)
6716 goto error;
6717 isl_basic_map_free(map->p[i]);
6718 map->p[i] = bmap;
6719 map = remove_if_empty(map, i);
6720 if (!map)
6721 return NULL;
6724 return map;
6725 error:
6726 isl_map_free(map);
6727 return NULL;
6730 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6731 enum isl_dim_type type, unsigned pos, int value)
6733 int i;
6735 map = isl_map_cow(map);
6736 if (isl_map_check_range(map, type, pos, 1) < 0)
6737 return isl_map_free(map);
6738 for (i = map->n - 1; i >= 0; --i) {
6739 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6740 map = remove_if_empty(map, i);
6741 if (!map)
6742 return NULL;
6744 map = isl_map_unmark_normalized(map);
6745 return map;
6748 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6749 enum isl_dim_type type, unsigned pos, int value)
6751 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6754 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6755 enum isl_dim_type type, unsigned pos, isl_int value)
6757 int i;
6759 map = isl_map_cow(map);
6760 if (isl_map_check_range(map, type, pos, 1) < 0)
6761 return isl_map_free(map);
6762 for (i = 0; i < map->n; ++i) {
6763 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6764 if (!map->p[i])
6765 goto error;
6767 map = isl_map_unmark_normalized(map);
6768 return map;
6769 error:
6770 isl_map_free(map);
6771 return NULL;
6774 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6775 enum isl_dim_type type, unsigned pos, isl_int value)
6777 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6780 /* Fix the value of the variable at position "pos" of type "type" of "map"
6781 * to be equal to "v".
6783 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6784 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6786 int i;
6788 map = isl_map_cow(map);
6789 if (!map || !v)
6790 goto error;
6792 if (!isl_val_is_int(v))
6793 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6794 "expecting integer value", goto error);
6795 if (isl_map_check_range(map, type, pos, 1) < 0)
6796 goto error;
6797 for (i = map->n - 1; i >= 0; --i) {
6798 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6799 isl_val_copy(v));
6800 map = remove_if_empty(map, i);
6801 if (!map)
6802 goto error;
6804 map = isl_map_unmark_normalized(map);
6805 isl_val_free(v);
6806 return map;
6807 error:
6808 isl_map_free(map);
6809 isl_val_free(v);
6810 return NULL;
6813 /* Fix the value of the variable at position "pos" of type "type" of "set"
6814 * to be equal to "v".
6816 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6817 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6819 return isl_map_fix_val(set, type, pos, v);
6822 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6823 unsigned input, int value)
6825 return isl_map_fix_si(map, isl_dim_in, input, value);
6828 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6829 int value)
6831 return set_from_map(isl_map_fix_si(set_to_map(set),
6832 isl_dim_set, dim, value));
6835 static __isl_give isl_basic_map *basic_map_bound_si(
6836 __isl_take isl_basic_map *bmap,
6837 enum isl_dim_type type, unsigned pos, int value, int upper)
6839 int j;
6840 isl_size total;
6842 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6843 return isl_basic_map_free(bmap);
6844 total = isl_basic_map_dim(bmap, isl_dim_all);
6845 if (total < 0)
6846 return isl_basic_map_free(bmap);
6847 pos += isl_basic_map_offset(bmap, type);
6848 bmap = isl_basic_map_cow(bmap);
6849 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6850 j = isl_basic_map_alloc_inequality(bmap);
6851 if (j < 0)
6852 goto error;
6853 isl_seq_clr(bmap->ineq[j], 1 + total);
6854 if (upper) {
6855 isl_int_set_si(bmap->ineq[j][pos], -1);
6856 isl_int_set_si(bmap->ineq[j][0], value);
6857 } else {
6858 isl_int_set_si(bmap->ineq[j][pos], 1);
6859 isl_int_set_si(bmap->ineq[j][0], -value);
6861 bmap = isl_basic_map_simplify(bmap);
6862 return isl_basic_map_finalize(bmap);
6863 error:
6864 isl_basic_map_free(bmap);
6865 return NULL;
6868 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6869 __isl_take isl_basic_map *bmap,
6870 enum isl_dim_type type, unsigned pos, int value)
6872 return basic_map_bound_si(bmap, type, pos, value, 0);
6875 /* Constrain the values of the given dimension to be no greater than "value".
6877 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6878 __isl_take isl_basic_map *bmap,
6879 enum isl_dim_type type, unsigned pos, int value)
6881 return basic_map_bound_si(bmap, type, pos, value, 1);
6884 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6885 enum isl_dim_type type, unsigned pos, int value, int upper)
6887 int i;
6889 map = isl_map_cow(map);
6890 if (isl_map_check_range(map, type, pos, 1) < 0)
6891 return isl_map_free(map);
6892 for (i = 0; i < map->n; ++i) {
6893 map->p[i] = basic_map_bound_si(map->p[i],
6894 type, pos, value, upper);
6895 if (!map->p[i])
6896 goto error;
6898 map = isl_map_unmark_normalized(map);
6899 return map;
6900 error:
6901 isl_map_free(map);
6902 return NULL;
6905 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6906 enum isl_dim_type type, unsigned pos, int value)
6908 return map_bound_si(map, type, pos, value, 0);
6911 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6912 enum isl_dim_type type, unsigned pos, int value)
6914 return map_bound_si(map, type, pos, value, 1);
6917 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6918 enum isl_dim_type type, unsigned pos, int value)
6920 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6921 type, pos, value));
6924 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6925 enum isl_dim_type type, unsigned pos, int value)
6927 return isl_map_upper_bound_si(set, type, pos, value);
6930 /* Bound the given variable of "bmap" from below (or above is "upper"
6931 * is set) to "value".
6933 static __isl_give isl_basic_map *basic_map_bound(
6934 __isl_take isl_basic_map *bmap,
6935 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6937 int j;
6938 isl_size total;
6940 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6941 return isl_basic_map_free(bmap);
6942 total = isl_basic_map_dim(bmap, isl_dim_all);
6943 if (total < 0)
6944 return isl_basic_map_free(bmap);
6945 pos += isl_basic_map_offset(bmap, type);
6946 bmap = isl_basic_map_cow(bmap);
6947 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6948 j = isl_basic_map_alloc_inequality(bmap);
6949 if (j < 0)
6950 goto error;
6951 isl_seq_clr(bmap->ineq[j], 1 + total);
6952 if (upper) {
6953 isl_int_set_si(bmap->ineq[j][pos], -1);
6954 isl_int_set(bmap->ineq[j][0], value);
6955 } else {
6956 isl_int_set_si(bmap->ineq[j][pos], 1);
6957 isl_int_neg(bmap->ineq[j][0], value);
6959 bmap = isl_basic_map_simplify(bmap);
6960 return isl_basic_map_finalize(bmap);
6961 error:
6962 isl_basic_map_free(bmap);
6963 return NULL;
6966 /* Bound the given variable of "map" from below (or above is "upper"
6967 * is set) to "value".
6969 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6970 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6972 int i;
6974 map = isl_map_cow(map);
6975 if (isl_map_check_range(map, type, pos, 1) < 0)
6976 return isl_map_free(map);
6977 for (i = map->n - 1; i >= 0; --i) {
6978 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6979 map = remove_if_empty(map, i);
6980 if (!map)
6981 return NULL;
6983 map = isl_map_unmark_normalized(map);
6984 return map;
6987 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6988 enum isl_dim_type type, unsigned pos, isl_int value)
6990 return map_bound(map, type, pos, value, 0);
6993 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6994 enum isl_dim_type type, unsigned pos, isl_int value)
6996 return map_bound(map, type, pos, value, 1);
6999 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
7000 enum isl_dim_type type, unsigned pos, isl_int value)
7002 return isl_map_lower_bound(set, type, pos, value);
7005 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
7006 enum isl_dim_type type, unsigned pos, isl_int value)
7008 return isl_map_upper_bound(set, type, pos, value);
7011 /* Force the values of the variable at position "pos" of type "type" of "map"
7012 * to be no smaller than "value".
7014 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
7015 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7017 if (!value)
7018 goto error;
7019 if (!isl_val_is_int(value))
7020 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7021 "expecting integer value", goto error);
7022 map = isl_map_lower_bound(map, type, pos, value->n);
7023 isl_val_free(value);
7024 return map;
7025 error:
7026 isl_val_free(value);
7027 isl_map_free(map);
7028 return NULL;
7031 /* Force the values of the variable at position "pos" of type "type" of "set"
7032 * to be no smaller than "value".
7034 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
7035 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7037 isl_map *map;
7039 map = set_to_map(set);
7040 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
7043 /* Force the values of the variable at position "pos" of type "type" of "map"
7044 * to be no greater than "value".
7046 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
7047 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7049 if (!value)
7050 goto error;
7051 if (!isl_val_is_int(value))
7052 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7053 "expecting integer value", goto error);
7054 map = isl_map_upper_bound(map, type, pos, value->n);
7055 isl_val_free(value);
7056 return map;
7057 error:
7058 isl_val_free(value);
7059 isl_map_free(map);
7060 return NULL;
7063 /* Force the values of the variable at position "pos" of type "type" of "set"
7064 * to be no greater than "value".
7066 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7067 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7069 isl_map *map;
7071 map = set_to_map(set);
7072 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7075 /* If "mv" has an explicit domain, then intersect the domain of "map"
7076 * with this explicit domain.
7078 * An isl_multi_val object never has an explicit domain,
7079 * so simply return "map".
7081 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7082 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7084 return map;
7087 #undef BASE
7088 #define BASE val
7089 #include "isl_map_bound_templ.c"
7091 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7092 * for each set dimension, by treating the set as a map.
7094 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7095 __isl_take isl_multi_val *bound,
7096 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7097 unsigned pos, __isl_take isl_val *value))
7099 isl_map *map;
7101 map = set_to_map(set);
7102 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7105 #undef BASE
7106 #define BASE pw_aff
7107 #include "isl_map_bound_templ.c"
7109 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7110 * for each set dimension, by converting the set and the bound
7111 * to objects living in a map space.
7113 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7114 __isl_take isl_multi_pw_aff *bound,
7115 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7116 unsigned pos, __isl_take TYPE *value))
7118 isl_map *map;
7120 map = isl_map_from_range(set);
7121 bound = isl_multi_pw_aff_from_range(bound);
7122 map = map_bound_multi_pw_aff(map, bound, set_bound);
7123 return isl_map_range(map);
7126 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7127 * setting a bound on the given output dimension.
7129 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7130 unsigned pos, __isl_take isl_val *v)
7132 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7135 /* Force the values of the set dimensions of "set"
7136 * to be no smaller than the corresponding values in "lower".
7138 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7139 __isl_take isl_multi_val *lower)
7141 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7144 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7145 * setting a bound on the given output dimension.
7147 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7148 unsigned pos, __isl_take isl_val *v)
7150 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7153 /* Force the values of the set dimensions of "set"
7154 * to be no greater than the corresponding values in "upper".
7156 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7157 __isl_take isl_multi_val *upper)
7159 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7162 /* Force the symbolic constant expression "bound"
7163 * to satisfy the relation "order" with respect to
7164 * the output variable at position "pos" of "map".
7166 * Create an affine expression representing the output variable
7167 * in terms of the range and
7168 * compare it using "order" to "bound" (defined on the domain).
7169 * The result is a relation between elements in domain and range that
7170 * can be intersected with "map".
7172 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7173 unsigned pos, __isl_take isl_pw_aff *bound,
7174 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7175 __isl_take isl_pw_aff *pa2))
7177 isl_space *space;
7178 isl_local_space *ls;
7179 isl_pw_aff *var;
7181 space = isl_space_range(isl_map_get_space(map));
7182 ls = isl_local_space_from_space(space);
7183 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7184 map = isl_map_intersect(map, order(bound, var));
7185 return map;
7188 /* Force the values of the output variable at position "pos" of "map"
7189 * to be no smaller than the symbolic constant expression "lower".
7191 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7192 unsigned pos, __isl_take isl_pw_aff *lower)
7194 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7197 /* Force the values of the output variable at position "pos" of "map"
7198 * to be no greater than the symbolic constant expression "upper".
7200 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7201 unsigned pos, __isl_take isl_pw_aff *upper)
7203 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7206 /* Force the values of the set dimensions of "set"
7207 * to be no smaller than the corresponding constant symbolic expressions
7208 * in "lower".
7210 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7211 __isl_take isl_multi_pw_aff *lower)
7213 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7216 /* Force the values of the set dimensions of "set"
7217 * to be no greater than the corresponding constant symbolic expressions
7218 * in "upper".
7220 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7221 __isl_take isl_multi_pw_aff *upper)
7223 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7226 /* Force the values of the output dimensions of "map"
7227 * to be no smaller than the corresponding constant symbolic expressions
7228 * in "lower".
7230 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7231 __isl_take isl_multi_pw_aff *lower)
7233 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7236 /* Force the values of the output dimensions of "map"
7237 * to be no greater than the corresponding constant symbolic expressions
7238 * in "upper".
7240 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7241 __isl_take isl_multi_pw_aff *upper)
7243 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7246 /* Bound the given variable of "bset" from below (or above is "upper"
7247 * is set) to "value".
7249 static __isl_give isl_basic_set *isl_basic_set_bound(
7250 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7251 isl_int value, int upper)
7253 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7254 type, pos, value, upper));
7257 /* Bound the given variable of "bset" from below (or above is "upper"
7258 * is set) to "value".
7260 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7261 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7262 __isl_take isl_val *value, int upper)
7264 if (!value)
7265 goto error;
7266 if (!isl_val_is_int(value))
7267 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7268 "expecting integer value", goto error);
7269 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7270 isl_val_free(value);
7271 return bset;
7272 error:
7273 isl_val_free(value);
7274 isl_basic_set_free(bset);
7275 return NULL;
7278 /* Bound the given variable of "bset" from below to "value".
7280 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7281 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7282 __isl_take isl_val *value)
7284 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7287 /* Bound the given variable of "bset" from above to "value".
7289 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7290 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7291 __isl_take isl_val *value)
7293 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7296 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7298 return isl_map_transform(map, &isl_space_reverse,
7299 &isl_basic_map_reverse);
7302 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7304 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7306 return isl_map_transform(map, &isl_space_domain_reverse,
7307 &isl_basic_map_domain_reverse);
7310 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7312 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7314 return isl_map_transform(map, &isl_space_range_reverse,
7315 &isl_basic_map_range_reverse);
7318 /* Given a set (A -> B), return the corresponding set (B -> A).
7320 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7322 isl_map *map = set_to_map(set);
7324 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7325 &isl_basic_map_set_reverse);
7326 return set_from_map(map);
7329 #undef TYPE
7330 #define TYPE isl_pw_multi_aff
7331 #undef SUFFIX
7332 #define SUFFIX _pw_multi_aff
7333 #undef EMPTY
7334 #define EMPTY isl_pw_multi_aff_empty
7335 #undef ADD
7336 #define ADD isl_pw_multi_aff_union_add
7337 #include "isl_map_lexopt_templ.c"
7339 /* Given a map "map", compute the lexicographically minimal
7340 * (or maximal) image element for each domain element in dom,
7341 * in the form of an isl_pw_multi_aff.
7342 * If "empty" is not NULL, then set *empty to those elements in dom that
7343 * do not have an image element.
7344 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7345 * should be computed over the domain of "map". "empty" is also NULL
7346 * in this case.
7348 * We first compute the lexicographically minimal or maximal element
7349 * in the first basic map. This results in a partial solution "res"
7350 * and a subset "todo" of dom that still need to be handled.
7351 * We then consider each of the remaining maps in "map" and successively
7352 * update both "res" and "todo".
7353 * If "empty" is NULL, then the todo sets are not needed and therefore
7354 * also not computed.
7356 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7357 __isl_take isl_map *map, __isl_take isl_set *dom,
7358 __isl_give isl_set **empty, unsigned flags)
7360 int i;
7361 int full;
7362 isl_pw_multi_aff *res;
7363 isl_set *todo;
7365 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7366 if (!map || (!full && !dom))
7367 goto error;
7369 if (isl_map_plain_is_empty(map)) {
7370 if (empty)
7371 *empty = dom;
7372 else
7373 isl_set_free(dom);
7374 return isl_pw_multi_aff_from_map(map);
7377 res = basic_map_partial_lexopt_pw_multi_aff(
7378 isl_basic_map_copy(map->p[0]),
7379 isl_set_copy(dom), empty, flags);
7381 if (empty)
7382 todo = *empty;
7383 for (i = 1; i < map->n; ++i) {
7384 isl_pw_multi_aff *res_i;
7386 res_i = basic_map_partial_lexopt_pw_multi_aff(
7387 isl_basic_map_copy(map->p[i]),
7388 isl_set_copy(dom), empty, flags);
7390 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7391 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7392 else
7393 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7395 if (empty)
7396 todo = isl_set_intersect(todo, *empty);
7399 isl_set_free(dom);
7400 isl_map_free(map);
7402 if (empty)
7403 *empty = todo;
7405 return res;
7406 error:
7407 if (empty)
7408 *empty = NULL;
7409 isl_set_free(dom);
7410 isl_map_free(map);
7411 return NULL;
7414 #undef TYPE
7415 #define TYPE isl_map
7416 #undef SUFFIX
7417 #define SUFFIX
7418 #undef EMPTY
7419 #define EMPTY isl_map_empty
7420 #undef ADD
7421 #define ADD isl_map_union_disjoint
7422 #include "isl_map_lexopt_templ.c"
7424 /* Given a map "map", compute the lexicographically minimal
7425 * (or maximal) image element for each domain element in "dom",
7426 * in the form of an isl_map.
7427 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7428 * do not have an image element.
7429 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7430 * should be computed over the domain of "map". "empty" is also NULL
7431 * in this case.
7433 * If the input consists of more than one disjunct, then first
7434 * compute the desired result in the form of an isl_pw_multi_aff and
7435 * then convert that into an isl_map.
7437 * This function used to have an explicit implementation in terms
7438 * of isl_maps, but it would continually intersect the domains of
7439 * partial results with the complement of the domain of the next
7440 * partial solution, potentially leading to an explosion in the number
7441 * of disjuncts if there are several disjuncts in the input.
7442 * An even earlier implementation of this function would look for
7443 * better results in the domain of the partial result and for extra
7444 * results in the complement of this domain, which would lead to
7445 * even more splintering.
7447 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7448 __isl_take isl_map *map, __isl_take isl_set *dom,
7449 __isl_give isl_set **empty, unsigned flags)
7451 int full;
7452 struct isl_map *res;
7453 isl_pw_multi_aff *pma;
7455 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7456 if (!map || (!full && !dom))
7457 goto error;
7459 if (isl_map_plain_is_empty(map)) {
7460 if (empty)
7461 *empty = dom;
7462 else
7463 isl_set_free(dom);
7464 return map;
7467 if (map->n == 1) {
7468 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7469 dom, empty, flags);
7470 isl_map_free(map);
7471 return res;
7474 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7475 flags);
7476 return isl_map_from_pw_multi_aff_internal(pma);
7477 error:
7478 if (empty)
7479 *empty = NULL;
7480 isl_set_free(dom);
7481 isl_map_free(map);
7482 return NULL;
7485 __isl_give isl_map *isl_map_partial_lexmax(
7486 __isl_take isl_map *map, __isl_take isl_set *dom,
7487 __isl_give isl_set **empty)
7489 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7492 __isl_give isl_map *isl_map_partial_lexmin(
7493 __isl_take isl_map *map, __isl_take isl_set *dom,
7494 __isl_give isl_set **empty)
7496 return isl_map_partial_lexopt(map, dom, empty, 0);
7499 __isl_give isl_set *isl_set_partial_lexmin(
7500 __isl_take isl_set *set, __isl_take isl_set *dom,
7501 __isl_give isl_set **empty)
7503 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7504 dom, empty));
7507 __isl_give isl_set *isl_set_partial_lexmax(
7508 __isl_take isl_set *set, __isl_take isl_set *dom,
7509 __isl_give isl_set **empty)
7511 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7512 dom, empty));
7515 /* Compute the lexicographic minimum (or maximum if "flags" includes
7516 * ISL_OPT_MAX) of "bset" over its parametric domain.
7518 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7519 unsigned flags)
7521 return isl_basic_map_lexopt(bset, flags);
7524 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7526 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7529 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7531 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7534 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7536 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7539 /* Compute the lexicographic minimum of "bset" over its parametric domain
7540 * for the purpose of quantifier elimination.
7541 * That is, find an explicit representation for all the existentially
7542 * quantified variables in "bset" by computing their lexicographic
7543 * minimum.
7545 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7546 __isl_take isl_basic_set *bset)
7548 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7551 /* Given a basic map with one output dimension, compute the minimum or
7552 * maximum of that dimension as an isl_pw_aff.
7554 * Compute the optimum as a lexicographic optimum over the single
7555 * output dimension and extract the single isl_pw_aff from the result.
7557 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7558 int max)
7560 isl_pw_multi_aff *pma;
7561 isl_pw_aff *pwaff;
7563 bmap = isl_basic_map_copy(bmap);
7564 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7565 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7566 isl_pw_multi_aff_free(pma);
7568 return pwaff;
7571 /* Compute the minimum or maximum of the given output dimension
7572 * as a function of the parameters and the input dimensions,
7573 * but independently of the other output dimensions.
7575 * We first project out the other output dimension and then compute
7576 * the "lexicographic" maximum in each basic map, combining the results
7577 * using isl_pw_aff_union_max.
7579 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7580 int max)
7582 int i;
7583 isl_pw_aff *pwaff;
7584 isl_size n_out;
7586 n_out = isl_map_dim(map, isl_dim_out);
7587 if (n_out < 0)
7588 map = isl_map_free(map);
7589 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7590 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7591 if (!map)
7592 return NULL;
7594 if (map->n == 0) {
7595 isl_space *space = isl_map_get_space(map);
7596 isl_map_free(map);
7597 return isl_pw_aff_empty(space);
7600 pwaff = basic_map_dim_opt(map->p[0], max);
7601 for (i = 1; i < map->n; ++i) {
7602 isl_pw_aff *pwaff_i;
7604 pwaff_i = basic_map_dim_opt(map->p[i], max);
7605 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7608 isl_map_free(map);
7610 return pwaff;
7613 /* Compute the minimum of the given output dimension as a function of the
7614 * parameters and input dimensions, but independently of
7615 * the other output dimensions.
7617 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7619 return map_dim_opt(map, pos, 0);
7622 /* Compute the maximum of the given output dimension as a function of the
7623 * parameters and input dimensions, but independently of
7624 * the other output dimensions.
7626 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7628 return map_dim_opt(map, pos, 1);
7631 /* Compute the minimum or maximum of the given set dimension
7632 * as a function of the parameters,
7633 * but independently of the other set dimensions.
7635 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7636 int max)
7638 return map_dim_opt(set, pos, max);
7641 /* Compute the maximum of the given set dimension as a function of the
7642 * parameters, but independently of the other set dimensions.
7644 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7646 return set_dim_opt(set, pos, 1);
7649 /* Compute the minimum of the given set dimension as a function of the
7650 * parameters, but independently of the other set dimensions.
7652 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7654 return set_dim_opt(set, pos, 0);
7657 /* Apply a preimage specified by "mat" on the parameters of "bset".
7658 * bset is assumed to have only parameters and divs.
7660 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7661 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7663 isl_size nparam;
7665 nparam = isl_basic_set_dim(bset, isl_dim_param);
7666 if (nparam < 0 || !mat)
7667 goto error;
7669 bset->dim = isl_space_cow(bset->dim);
7670 if (!bset->dim)
7671 goto error;
7673 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7675 bset->dim->nparam = 0;
7676 bset->dim->n_out = nparam;
7677 bset = isl_basic_set_preimage(bset, mat);
7678 if (bset) {
7679 bset->dim->nparam = bset->dim->n_out;
7680 bset->dim->n_out = 0;
7682 return bset;
7683 error:
7684 isl_mat_free(mat);
7685 isl_basic_set_free(bset);
7686 return NULL;
7689 /* Apply a preimage specified by "mat" on the parameters of "set".
7690 * set is assumed to have only parameters and divs.
7692 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7693 __isl_take isl_mat *mat)
7695 isl_space *space;
7696 isl_size nparam;
7698 nparam = isl_set_dim(set, isl_dim_param);
7699 if (nparam < 0 || !mat)
7700 goto error;
7702 if (mat->n_row != 1 + nparam)
7703 isl_die(isl_set_get_ctx(set), isl_error_internal,
7704 "unexpected number of rows", goto error);
7706 space = isl_set_get_space(set);
7707 space = isl_space_move_dims(space, isl_dim_set, 0,
7708 isl_dim_param, 0, nparam);
7709 set = isl_set_reset_space(set, space);
7710 set = isl_set_preimage(set, mat);
7711 nparam = isl_set_dim(set, isl_dim_out);
7712 if (nparam < 0)
7713 set = isl_set_free(set);
7714 space = isl_set_get_space(set);
7715 space = isl_space_move_dims(space, isl_dim_param, 0,
7716 isl_dim_out, 0, nparam);
7717 set = isl_set_reset_space(set, space);
7718 return set;
7719 error:
7720 isl_mat_free(mat);
7721 isl_set_free(set);
7722 return NULL;
7725 /* Intersect the basic set "bset" with the affine space specified by the
7726 * equalities in "eq".
7728 static __isl_give isl_basic_set *basic_set_append_equalities(
7729 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7731 int i, k;
7732 unsigned len;
7734 if (!bset || !eq)
7735 goto error;
7737 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7738 if (!bset)
7739 goto error;
7741 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7742 for (i = 0; i < eq->n_row; ++i) {
7743 k = isl_basic_set_alloc_equality(bset);
7744 if (k < 0)
7745 goto error;
7746 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7747 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7749 isl_mat_free(eq);
7751 bset = isl_basic_set_gauss(bset, NULL);
7752 bset = isl_basic_set_finalize(bset);
7754 return bset;
7755 error:
7756 isl_mat_free(eq);
7757 isl_basic_set_free(bset);
7758 return NULL;
7761 /* Intersect the set "set" with the affine space specified by the
7762 * equalities in "eq".
7764 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7765 __isl_take isl_mat *eq)
7767 int i;
7769 if (!set || !eq)
7770 goto error;
7772 for (i = 0; i < set->n; ++i) {
7773 set->p[i] = basic_set_append_equalities(set->p[i],
7774 isl_mat_copy(eq));
7775 if (!set->p[i])
7776 goto error;
7778 isl_mat_free(eq);
7779 return set;
7780 error:
7781 isl_mat_free(eq);
7782 isl_set_free(set);
7783 return NULL;
7786 /* Given a basic set "bset" that only involves parameters and existentially
7787 * quantified variables, return the index of the first equality
7788 * that only involves parameters. If there is no such equality then
7789 * return bset->n_eq.
7791 * This function assumes that isl_basic_set_gauss has been called on "bset".
7793 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7795 int i, j;
7796 isl_size nparam, n_div;
7798 nparam = isl_basic_set_dim(bset, isl_dim_param);
7799 n_div = isl_basic_set_dim(bset, isl_dim_div);
7800 if (nparam < 0 || n_div < 0)
7801 return -1;
7803 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7804 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7805 ++i;
7808 return i;
7811 /* Compute an explicit representation for the existentially quantified
7812 * variables in "bset" by computing the "minimal value" of the set
7813 * variables. Since there are no set variables, the computation of
7814 * the minimal value essentially computes an explicit representation
7815 * of the non-empty part(s) of "bset".
7817 * The input only involves parameters and existentially quantified variables.
7818 * All equalities among parameters have been removed.
7820 * Since the existentially quantified variables in the result are in general
7821 * going to be different from those in the input, we first replace
7822 * them by the minimal number of variables based on their equalities.
7823 * This should simplify the parametric integer programming.
7825 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7827 isl_morph *morph1, *morph2;
7828 isl_set *set;
7829 isl_size n;
7831 if (!bset)
7832 return NULL;
7833 if (bset->n_eq == 0)
7834 return isl_basic_set_lexmin_compute_divs(bset);
7836 morph1 = isl_basic_set_parameter_compression(bset);
7837 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7838 bset = isl_basic_set_lift(bset);
7839 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7840 bset = isl_morph_basic_set(morph2, bset);
7841 n = isl_basic_set_dim(bset, isl_dim_set);
7842 if (n < 0)
7843 bset = isl_basic_set_free(bset);
7844 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7846 set = isl_basic_set_lexmin_compute_divs(bset);
7848 set = isl_morph_set(isl_morph_inverse(morph1), set);
7850 return set;
7853 /* Project the given basic set onto its parameter domain, possibly introducing
7854 * new, explicit, existential variables in the constraints.
7855 * The input has parameters and (possibly implicit) existential variables.
7856 * The output has the same parameters, but only
7857 * explicit existentially quantified variables.
7859 * The actual projection is performed by pip, but pip doesn't seem
7860 * to like equalities very much, so we first remove the equalities
7861 * among the parameters by performing a variable compression on
7862 * the parameters. Afterward, an inverse transformation is performed
7863 * and the equalities among the parameters are inserted back in.
7865 * The variable compression on the parameters may uncover additional
7866 * equalities that were only implicit before. We therefore check
7867 * if there are any new parameter equalities in the result and
7868 * if so recurse. The removal of parameter equalities is required
7869 * for the parameter compression performed by base_compute_divs.
7871 static __isl_give isl_set *parameter_compute_divs(
7872 __isl_take isl_basic_set *bset)
7874 int i;
7875 struct isl_mat *eq;
7876 struct isl_mat *T, *T2;
7877 struct isl_set *set;
7878 isl_size nparam;
7880 bset = isl_basic_set_cow(bset);
7881 if (!bset)
7882 return NULL;
7884 if (bset->n_eq == 0)
7885 return base_compute_divs(bset);
7887 bset = isl_basic_set_gauss(bset, NULL);
7888 if (!bset)
7889 return NULL;
7890 if (isl_basic_set_plain_is_empty(bset))
7891 return isl_set_from_basic_set(bset);
7893 i = first_parameter_equality(bset);
7894 if (i == bset->n_eq)
7895 return base_compute_divs(bset);
7897 nparam = isl_basic_set_dim(bset, isl_dim_param);
7898 if (nparam < 0)
7899 return isl_set_from_basic_set(isl_basic_set_free(bset));
7900 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7901 0, 1 + nparam);
7902 eq = isl_mat_cow(eq);
7903 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7904 if (T && T->n_col == 0) {
7905 isl_mat_free(T);
7906 isl_mat_free(T2);
7907 isl_mat_free(eq);
7908 bset = isl_basic_set_set_to_empty(bset);
7909 return isl_set_from_basic_set(bset);
7911 bset = basic_set_parameter_preimage(bset, T);
7913 i = first_parameter_equality(bset);
7914 if (!bset)
7915 set = NULL;
7916 else if (i == bset->n_eq)
7917 set = base_compute_divs(bset);
7918 else
7919 set = parameter_compute_divs(bset);
7920 set = set_parameter_preimage(set, T2);
7921 set = set_append_equalities(set, eq);
7922 return set;
7925 /* Insert the divs from "ls" before those of "bmap".
7927 * The number of columns is not changed, which means that the last
7928 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7929 * The caller is responsible for removing the same number of dimensions
7930 * from the space of "bmap".
7932 static __isl_give isl_basic_map *insert_divs_from_local_space(
7933 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7935 int i;
7936 isl_size n_div;
7937 int old_n_div;
7939 n_div = isl_local_space_dim(ls, isl_dim_div);
7940 if (n_div < 0)
7941 return isl_basic_map_free(bmap);
7942 if (n_div == 0)
7943 return bmap;
7945 old_n_div = bmap->n_div;
7946 bmap = insert_div_rows(bmap, n_div);
7947 if (!bmap)
7948 return NULL;
7950 for (i = 0; i < n_div; ++i) {
7951 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7952 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7955 return bmap;
7958 /* Replace the space of "bmap" by the space and divs of "ls".
7960 * If "ls" has any divs, then we simplify the result since we may
7961 * have discovered some additional equalities that could simplify
7962 * the div expressions.
7964 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7965 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7967 isl_size n_div;
7969 bmap = isl_basic_map_cow(bmap);
7970 n_div = isl_local_space_dim(ls, isl_dim_div);
7971 if (!bmap || n_div < 0)
7972 goto error;
7974 bmap = insert_divs_from_local_space(bmap, ls);
7975 if (!bmap)
7976 goto error;
7978 isl_space_free(bmap->dim);
7979 bmap->dim = isl_local_space_get_space(ls);
7980 if (!bmap->dim)
7981 goto error;
7983 isl_local_space_free(ls);
7984 if (n_div > 0)
7985 bmap = isl_basic_map_simplify(bmap);
7986 bmap = isl_basic_map_finalize(bmap);
7987 return bmap;
7988 error:
7989 isl_basic_map_free(bmap);
7990 isl_local_space_free(ls);
7991 return NULL;
7994 /* Replace the space of "map" by the space and divs of "ls".
7996 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7997 __isl_take isl_local_space *ls)
7999 int i;
8001 map = isl_map_cow(map);
8002 if (!map || !ls)
8003 goto error;
8005 for (i = 0; i < map->n; ++i) {
8006 map->p[i] = basic_replace_space_by_local_space(map->p[i],
8007 isl_local_space_copy(ls));
8008 if (!map->p[i])
8009 goto error;
8011 isl_space_free(isl_map_take_space(map));
8012 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
8014 isl_local_space_free(ls);
8015 return map;
8016 error:
8017 isl_local_space_free(ls);
8018 isl_map_free(map);
8019 return NULL;
8022 /* Compute an explicit representation for the existentially
8023 * quantified variables for which do not know any explicit representation yet.
8025 * We first sort the existentially quantified variables so that the
8026 * existentially quantified variables for which we already have an explicit
8027 * representation are placed before those for which we do not.
8028 * The input dimensions, the output dimensions and the existentially
8029 * quantified variables for which we already have an explicit
8030 * representation are then turned into parameters.
8031 * compute_divs returns a map with the same parameters and
8032 * no input or output dimensions and the dimension specification
8033 * is reset to that of the input, including the existentially quantified
8034 * variables for which we already had an explicit representation.
8036 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
8038 struct isl_basic_set *bset;
8039 struct isl_set *set;
8040 struct isl_map *map;
8041 isl_space *space;
8042 isl_local_space *ls;
8043 isl_size nparam;
8044 isl_size n_in;
8045 isl_size n_out;
8046 int n_known;
8047 int i;
8049 bmap = isl_basic_map_sort_divs(bmap);
8050 bmap = isl_basic_map_cow(bmap);
8051 if (!bmap)
8052 return NULL;
8054 n_known = isl_basic_map_first_unknown_div(bmap);
8055 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8056 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8057 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8058 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8059 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8061 space = isl_space_set_alloc(bmap->ctx,
8062 nparam + n_in + n_out + n_known, 0);
8063 if (!space)
8064 goto error;
8066 ls = isl_basic_map_get_local_space(bmap);
8067 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8068 n_known, bmap->n_div - n_known);
8069 if (n_known > 0) {
8070 for (i = n_known; i < bmap->n_div; ++i)
8071 swap_div(bmap, i - n_known, i);
8072 bmap->n_div -= n_known;
8073 bmap->extra -= n_known;
8075 bmap = isl_basic_map_reset_space(bmap, space);
8076 bset = bset_from_bmap(bmap);
8078 set = parameter_compute_divs(bset);
8079 map = set_to_map(set);
8080 map = replace_space_by_local_space(map, ls);
8082 return map;
8083 error:
8084 isl_basic_map_free(bmap);
8085 return NULL;
8088 /* Is the integer division at position "div" of "bmap" integral?
8089 * That is, does it have denominator 1?
8091 isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
8093 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8094 return isl_bool_error;
8095 return isl_int_is_one(bmap->div[div][0]);
8098 /* Remove the explicit representation of local variable "div",
8099 * if there is any.
8101 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8102 __isl_take isl_basic_map *bmap, int div)
8104 isl_bool unknown;
8106 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8107 if (unknown < 0)
8108 return isl_basic_map_free(bmap);
8109 if (unknown)
8110 return bmap;
8112 bmap = isl_basic_map_cow(bmap);
8113 if (!bmap)
8114 return NULL;
8115 isl_int_set_si(bmap->div[div][0], 0);
8116 return bmap;
8119 /* Is local variable "div" of "bmap" marked as not having an explicit
8120 * representation?
8121 * Note that even if "div" is not marked in this way and therefore
8122 * has an explicit representation, this representation may still
8123 * depend (indirectly) on other local variables that do not
8124 * have an explicit representation.
8126 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8127 int div)
8129 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8130 return isl_bool_error;
8131 return isl_int_is_zero(bmap->div[div][0]);
8134 /* Return the position of the first local variable that does not
8135 * have an explicit representation.
8136 * Return the total number of local variables if they all have
8137 * an explicit representation.
8138 * Return -1 on error.
8140 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8142 int i;
8144 if (!bmap)
8145 return -1;
8147 for (i = 0; i < bmap->n_div; ++i) {
8148 if (!isl_basic_map_div_is_known(bmap, i))
8149 return i;
8151 return bmap->n_div;
8154 /* Return the position of the first local variable that does not
8155 * have an explicit representation.
8156 * Return the total number of local variables if they all have
8157 * an explicit representation.
8158 * Return -1 on error.
8160 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8162 return isl_basic_map_first_unknown_div(bset);
8165 /* Does "bmap" have an explicit representation for all local variables?
8167 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8169 int first;
8170 isl_size n;
8172 n = isl_basic_map_dim(bmap, isl_dim_div);
8173 first = isl_basic_map_first_unknown_div(bmap);
8174 if (n < 0 || first < 0)
8175 return isl_bool_error;
8176 return first == n;
8179 /* Do all basic maps in "map" have an explicit representation
8180 * for all local variables?
8182 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8184 int i;
8186 if (!map)
8187 return isl_bool_error;
8189 for (i = 0; i < map->n; ++i) {
8190 int known = isl_basic_map_divs_known(map->p[i]);
8191 if (known <= 0)
8192 return known;
8195 return isl_bool_true;
8198 /* If bmap contains any unknown divs, then compute explicit
8199 * expressions for them. However, this computation may be
8200 * quite expensive, so first try to remove divs that aren't
8201 * strictly needed.
8203 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8205 int known;
8206 struct isl_map *map;
8208 known = isl_basic_map_divs_known(bmap);
8209 if (known < 0)
8210 goto error;
8211 if (known)
8212 return isl_map_from_basic_map(bmap);
8214 bmap = isl_basic_map_drop_redundant_divs(bmap);
8216 known = isl_basic_map_divs_known(bmap);
8217 if (known < 0)
8218 goto error;
8219 if (known)
8220 return isl_map_from_basic_map(bmap);
8222 map = compute_divs(bmap);
8223 return map;
8224 error:
8225 isl_basic_map_free(bmap);
8226 return NULL;
8229 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8231 int i;
8232 int known;
8233 struct isl_map *res;
8235 if (!map)
8236 return NULL;
8237 if (map->n == 0)
8238 return map;
8240 known = isl_map_divs_known(map);
8241 if (known < 0) {
8242 isl_map_free(map);
8243 return NULL;
8245 if (known)
8246 return map;
8248 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8249 for (i = 1 ; i < map->n; ++i) {
8250 struct isl_map *r2;
8251 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8252 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8253 res = isl_map_union_disjoint(res, r2);
8254 else
8255 res = isl_map_union(res, r2);
8257 isl_map_free(map);
8259 return res;
8262 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8264 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8267 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8269 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8272 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8274 isl_space *space;
8275 isl_size n_out;
8277 n_out = isl_map_dim(map, isl_dim_out);
8278 if (n_out < 0)
8279 return set_from_map(isl_map_free(map));
8280 space = isl_space_domain(isl_map_get_space(map));
8282 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8284 return set_from_map(isl_map_reset_space(map, space));
8287 /* Return the union of "map1" and "map2", where we assume for now that
8288 * "map1" and "map2" are disjoint. Note that the basic maps inside
8289 * "map1" or "map2" may not be disjoint from each other.
8290 * Also note that this function is also called from isl_map_union,
8291 * which takes care of handling the situation where "map1" and "map2"
8292 * may not be disjoint.
8294 * If one of the inputs is empty, we can simply return the other input.
8295 * Similarly, if one of the inputs is universal, then it is equal to the union.
8297 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8298 __isl_take isl_map *map2)
8300 int i;
8301 unsigned flags = 0;
8302 struct isl_map *map = NULL;
8303 int is_universe;
8305 if (isl_map_check_equal_space(map1, map2) < 0)
8306 goto error;
8308 if (map1->n == 0) {
8309 isl_map_free(map1);
8310 return map2;
8312 if (map2->n == 0) {
8313 isl_map_free(map2);
8314 return map1;
8317 is_universe = isl_map_plain_is_universe(map1);
8318 if (is_universe < 0)
8319 goto error;
8320 if (is_universe) {
8321 isl_map_free(map2);
8322 return map1;
8325 is_universe = isl_map_plain_is_universe(map2);
8326 if (is_universe < 0)
8327 goto error;
8328 if (is_universe) {
8329 isl_map_free(map1);
8330 return map2;
8333 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8334 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8335 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8337 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8338 map1->n + map2->n, flags);
8339 if (!map)
8340 goto error;
8341 for (i = 0; i < map1->n; ++i) {
8342 map = isl_map_add_basic_map(map,
8343 isl_basic_map_copy(map1->p[i]));
8344 if (!map)
8345 goto error;
8347 for (i = 0; i < map2->n; ++i) {
8348 map = isl_map_add_basic_map(map,
8349 isl_basic_map_copy(map2->p[i]));
8350 if (!map)
8351 goto error;
8353 isl_map_free(map1);
8354 isl_map_free(map2);
8355 return map;
8356 error:
8357 isl_map_free(map);
8358 isl_map_free(map1);
8359 isl_map_free(map2);
8360 return NULL;
8363 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8364 * guaranteed to be disjoint by the caller.
8366 * Note that this functions is called from within isl_map_make_disjoint,
8367 * so we have to be careful not to touch the constraints of the inputs
8368 * in any way.
8370 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8371 __isl_take isl_map *map2)
8373 isl_map_align_params_bin(&map1, &map2);
8374 return map_union_disjoint(map1, map2);
8377 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8378 * not be disjoint.
8380 * We currently simply call map_union_disjoint, the internal operation
8381 * of which does not really depend on the inputs being disjoint.
8382 * If the result contains more than one basic map, then we clear
8383 * the disjoint flag since the result may contain basic maps from
8384 * both inputs and these are not guaranteed to be disjoint.
8386 * As a special case, if "map1" and "map2" are obviously equal,
8387 * then we simply return "map1".
8389 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8390 __isl_take isl_map *map2)
8392 int equal;
8394 if (isl_map_align_params_bin(&map1, &map2) < 0)
8395 goto error;
8397 equal = isl_map_plain_is_equal(map1, map2);
8398 if (equal < 0)
8399 goto error;
8400 if (equal) {
8401 isl_map_free(map2);
8402 return map1;
8405 map1 = map_union_disjoint(map1, map2);
8406 if (!map1)
8407 return NULL;
8408 if (map1->n > 1)
8409 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8410 return map1;
8411 error:
8412 isl_map_free(map1);
8413 isl_map_free(map2);
8414 return NULL;
8417 __isl_give isl_set *isl_set_union_disjoint(
8418 __isl_take isl_set *set1, __isl_take isl_set *set2)
8420 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8421 set_to_map(set2)));
8424 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8425 __isl_take isl_set *set2)
8427 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8430 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8431 * the results in a map living in "space".
8433 * "map" and "set" are assumed to be compatible and non-NULL.
8435 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8436 __isl_take isl_space *space, __isl_take isl_set *set,
8437 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8438 __isl_take isl_basic_set *bset))
8440 unsigned flags = 0;
8441 struct isl_map *result;
8442 int i, j;
8444 if (isl_set_plain_is_universe(set)) {
8445 isl_set_free(set);
8446 return isl_map_reset_equal_dim_space(map, space);
8449 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8450 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8451 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8453 result = isl_map_alloc_space(space, map->n * set->n, flags);
8454 for (i = 0; result && i < map->n; ++i)
8455 for (j = 0; j < set->n; ++j) {
8456 result = isl_map_add_basic_map(result,
8457 fn(isl_basic_map_copy(map->p[i]),
8458 isl_basic_set_copy(set->p[j])));
8459 if (!result)
8460 break;
8463 isl_map_free(map);
8464 isl_set_free(set);
8465 return result;
8468 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8469 __isl_take isl_set *set)
8471 isl_bool ok;
8472 isl_space *space;
8474 isl_map_align_params_set(&map, &set);
8475 ok = isl_map_compatible_range(map, set);
8476 if (ok < 0)
8477 goto error;
8478 if (!ok)
8479 isl_die(set->ctx, isl_error_invalid,
8480 "incompatible spaces", goto error);
8482 space = isl_map_get_space(map);
8483 return map_intersect_set(map, space, set,
8484 &isl_basic_map_intersect_range);
8485 error:
8486 isl_map_free(map);
8487 isl_set_free(set);
8488 return NULL;
8491 /* Intersect the domain of "map" with "set".
8493 * If the domain dimensions of "map" do not have any identifiers,
8494 * then copy them over from "set".
8496 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8497 __isl_take isl_set *set)
8499 isl_bool ok;
8500 isl_space *space;
8502 isl_map_align_params_set(&map, &set);
8503 ok = isl_map_compatible_domain(map, set);
8504 if (ok < 0)
8505 goto error;
8506 if (!ok)
8507 isl_die(set->ctx, isl_error_invalid,
8508 "incompatible spaces", goto error);
8510 space = isl_map_get_space(map);
8511 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8512 isl_set_peek_space(set), isl_dim_set);
8513 return map_intersect_set(map, space, set,
8514 &isl_basic_map_intersect_domain);
8515 error:
8516 isl_map_free(map);
8517 isl_set_free(set);
8518 return NULL;
8521 #undef BASE
8522 #define BASE map
8523 static
8524 #include "isl_copy_tuple_id_templ.c"
8526 /* Data structure that specifies how isl_map_intersect_factor
8527 * should operate.
8529 * "preserve_type" is the tuple where the factor differs from
8530 * the input map and of which the identifiers needs
8531 * to be preserved explicitly.
8532 * "other_factor" is used to extract the space of the other factor
8533 * from the space of the product ("map").
8534 * "product" is used to combine the given factor and a universe map
8535 * in the space returned by "other_factor" to produce a map
8536 * that lives in the same space as the input map.
8538 struct isl_intersect_factor_control {
8539 enum isl_dim_type preserve_type;
8540 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8541 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8542 __isl_take isl_map *other);
8545 /* Given a map "map" in some product space and a map "factor"
8546 * living in some factor space, return the intersection.
8548 * After aligning the parameters,
8549 * the map "factor" is first extended to a map living in the same space
8550 * as "map" and then a regular intersection is computed.
8552 * Note that the extension is computed as a product, which is anonymous
8553 * by default. If "map" has an identifier on the corresponding tuple,
8554 * then this identifier needs to be set on the product
8555 * before the intersection is computed.
8557 static __isl_give isl_map *isl_map_intersect_factor(
8558 __isl_take isl_map *map, __isl_take isl_map *factor,
8559 struct isl_intersect_factor_control *control)
8561 isl_bool equal;
8562 isl_space *space;
8563 isl_map *other, *product;
8565 equal = isl_map_has_equal_params(map, factor);
8566 if (equal < 0)
8567 goto error;
8568 if (!equal) {
8569 map = isl_map_align_params(map, isl_map_get_space(factor));
8570 factor = isl_map_align_params(factor, isl_map_get_space(map));
8573 space = isl_map_get_space(map);
8574 other = isl_map_universe(control->other_factor(space));
8575 product = control->product(factor, other);
8577 space = isl_map_peek_space(map);
8578 product = isl_map_copy_tuple_id(product, control->preserve_type,
8579 space, control->preserve_type);
8580 return map_intersect(map, product);
8581 error:
8582 isl_map_free(map);
8583 isl_map_free(factor);
8584 return NULL;
8587 /* Return the domain product of "map2" and "map1".
8589 static __isl_give isl_map *isl_map_reverse_domain_product(
8590 __isl_take isl_map *map1, __isl_take isl_map *map2)
8592 return isl_map_domain_product(map2, map1);
8595 /* Return the range product of "map2" and "map1".
8597 static __isl_give isl_map *isl_map_reverse_range_product(
8598 __isl_take isl_map *map1, __isl_take isl_map *map2)
8600 return isl_map_range_product(map2, map1);
8603 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8604 * in the space A -> C, return the intersection.
8606 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8607 __isl_take isl_map *map, __isl_take isl_map *factor)
8609 struct isl_intersect_factor_control control = {
8610 .preserve_type = isl_dim_in,
8611 .other_factor = isl_space_domain_factor_range,
8612 .product = isl_map_domain_product,
8615 return isl_map_intersect_factor(map, factor, &control);
8618 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8619 * in the space B -> C, return the intersection.
8621 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8622 __isl_take isl_map *map, __isl_take isl_map *factor)
8624 struct isl_intersect_factor_control control = {
8625 .preserve_type = isl_dim_in,
8626 .other_factor = isl_space_domain_factor_domain,
8627 .product = isl_map_reverse_domain_product,
8630 return isl_map_intersect_factor(map, factor, &control);
8633 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8634 * in the space A -> B, return the intersection.
8636 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8637 __isl_take isl_map *map, __isl_take isl_map *factor)
8639 struct isl_intersect_factor_control control = {
8640 .preserve_type = isl_dim_out,
8641 .other_factor = isl_space_range_factor_range,
8642 .product = isl_map_range_product,
8645 return isl_map_intersect_factor(map, factor, &control);
8648 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8649 * in the space A -> C, return the intersection.
8651 __isl_give isl_map *isl_map_intersect_range_factor_range(
8652 __isl_take isl_map *map, __isl_take isl_map *factor)
8654 struct isl_intersect_factor_control control = {
8655 .preserve_type = isl_dim_out,
8656 .other_factor = isl_space_range_factor_domain,
8657 .product = isl_map_reverse_range_product,
8660 return isl_map_intersect_factor(map, factor, &control);
8663 /* Given a set "set" in a space [A -> B] and a set "domain"
8664 * in the space A, return the intersection.
8666 * The set "domain" is first extended to a set living in the space
8667 * [A -> B] and then a regular intersection is computed.
8669 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8670 __isl_take isl_set *domain)
8672 struct isl_intersect_factor_control control = {
8673 .preserve_type = isl_dim_set,
8674 .other_factor = isl_space_factor_range,
8675 .product = isl_map_range_product,
8678 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8679 set_to_map(domain), &control));
8682 /* Given a set "set" in a space [A -> B] and a set "range"
8683 * in the space B, return the intersection.
8685 * The set "range" is first extended to a set living in the space
8686 * [A -> B] and then a regular intersection is computed.
8688 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8689 __isl_take isl_set *range)
8691 struct isl_intersect_factor_control control = {
8692 .preserve_type = isl_dim_set,
8693 .other_factor = isl_space_factor_domain,
8694 .product = isl_map_reverse_range_product,
8697 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8698 set_to_map(range), &control));
8701 #undef BASE
8702 #define BASE set
8703 static
8704 #include "isl_copy_tuple_id_templ.c"
8706 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8707 * in the space A, return the intersection.
8709 * The set "domain" is extended to a set living in the space [A -> B] and
8710 * the domain of "map" is intersected with this set.
8712 * If "map" has an identifier on the domain tuple,
8713 * then this identifier needs to be set on this product
8714 * before the intersection is computed.
8716 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8717 __isl_take isl_map *map, __isl_take isl_set *domain)
8719 isl_space *space;
8720 isl_set *factor;
8722 isl_map_align_params_set(&map, &domain);
8723 space = isl_map_get_space(map);
8724 space = isl_space_domain_wrapped_range(space);
8725 factor = isl_set_universe(space);
8726 domain = isl_set_product(domain, factor);
8727 space = isl_map_peek_space(map);
8728 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
8729 return isl_map_intersect_domain(map, domain);
8732 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8733 * in the space B, return the intersection.
8735 * The set "domain" is extended to a set living in the space [B -> C] and
8736 * the range of "map" is intersected with this set.
8738 * If "map" has an identifier on the range tuple,
8739 * then this identifier needs to be set on this product
8740 * before the intersection is computed.
8742 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8743 __isl_take isl_map *map, __isl_take isl_set *domain)
8745 isl_space *space;
8746 isl_set *factor;
8748 isl_map_align_params_set(&map, &domain);
8749 space = isl_map_get_space(map);
8750 space = isl_space_range_wrapped_range(space);
8751 factor = isl_set_universe(space);
8752 domain = isl_set_product(domain, factor);
8753 space = isl_map_peek_space(map);
8754 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
8755 return isl_map_intersect_range(map, domain);
8758 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8759 __isl_take isl_map *map2)
8761 if (isl_map_align_params_bin(&map1, &map2) < 0)
8762 goto error;
8763 map1 = isl_map_reverse(map1);
8764 map1 = isl_map_apply_range(map1, map2);
8765 return isl_map_reverse(map1);
8766 error:
8767 isl_map_free(map1);
8768 isl_map_free(map2);
8769 return NULL;
8772 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8773 __isl_take isl_map *map2)
8775 isl_space *space;
8776 struct isl_map *result;
8777 int i, j;
8779 if (isl_map_align_params_bin(&map1, &map2) < 0)
8780 goto error;
8782 space = isl_space_join(isl_space_copy(map1->dim),
8783 isl_space_copy(map2->dim));
8785 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8786 if (!result)
8787 goto error;
8788 for (i = 0; i < map1->n; ++i)
8789 for (j = 0; j < map2->n; ++j) {
8790 result = isl_map_add_basic_map(result,
8791 isl_basic_map_apply_range(
8792 isl_basic_map_copy(map1->p[i]),
8793 isl_basic_map_copy(map2->p[j])));
8794 if (!result)
8795 goto error;
8797 isl_map_free(map1);
8798 isl_map_free(map2);
8799 if (result && result->n <= 1)
8800 ISL_F_SET(result, ISL_MAP_DISJOINT);
8801 return result;
8802 error:
8803 isl_map_free(map1);
8804 isl_map_free(map2);
8805 return NULL;
8808 /* Is "bmap" a transformation, i.e.,
8809 * does it relate elements from the same space.
8811 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8813 isl_space *space;
8815 space = isl_basic_map_peek_space(bmap);
8816 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8819 /* Check that "bmap" is a transformation, i.e.,
8820 * that it relates elements from the same space.
8822 static isl_stat isl_basic_map_check_transformation(
8823 __isl_keep isl_basic_map *bmap)
8825 isl_bool equal;
8827 equal = isl_basic_map_is_transformation(bmap);
8828 if (equal < 0)
8829 return isl_stat_error;
8830 if (!equal)
8831 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8832 "domain and range don't match", return isl_stat_error);
8833 return isl_stat_ok;
8837 * returns range - domain
8839 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8841 isl_space *target_space;
8842 struct isl_basic_set *bset;
8843 isl_size dim;
8844 isl_size nparam;
8845 isl_size total;
8846 int i;
8848 if (isl_basic_map_check_transformation(bmap) < 0)
8849 return isl_basic_map_free(bmap);
8850 dim = isl_basic_map_dim(bmap, isl_dim_in);
8851 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8852 if (dim < 0 || nparam < 0)
8853 goto error;
8854 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8855 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8856 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8857 total = isl_basic_map_dim(bmap, isl_dim_all);
8858 if (total < 0)
8859 bmap = isl_basic_map_free(bmap);
8860 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8861 for (i = 0; i < dim; ++i) {
8862 int j = isl_basic_map_alloc_equality(bmap);
8863 if (j < 0) {
8864 bmap = isl_basic_map_free(bmap);
8865 break;
8867 isl_seq_clr(bmap->eq[j], 1 + total);
8868 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8869 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8870 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8872 bset = isl_basic_map_domain(bmap);
8873 bset = isl_basic_set_reset_space(bset, target_space);
8874 return bset;
8875 error:
8876 isl_basic_map_free(bmap);
8877 return NULL;
8880 /* Is the tuple of type "type1" of "map" the same as
8881 * the tuple of type "type2" of "space"?
8883 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8884 enum isl_dim_type type1, __isl_keep isl_space *space,
8885 enum isl_dim_type type2)
8887 isl_space *map_space;
8889 map_space = isl_map_peek_space(map);
8890 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8893 /* Is the tuple of type "type1" of "map1" the same as
8894 * the tuple of type "type2" of "map2"?
8896 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8897 enum isl_dim_type type1, __isl_keep isl_map *map2,
8898 enum isl_dim_type type2)
8900 isl_space *space1, *space2;
8902 space1 = isl_map_peek_space(map1);
8903 space2 = isl_map_peek_space(map2);
8904 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8907 /* Is the space of "obj" equal to "space", ignoring parameters?
8909 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8910 __isl_keep isl_space *space)
8912 isl_space *map_space;
8914 map_space = isl_map_peek_space(map);
8915 return isl_space_has_equal_tuples(map_space, space);
8918 /* Check that "map" is a transformation, i.e.,
8919 * that it relates elements from the same space.
8921 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8923 isl_bool equal;
8925 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8926 if (equal < 0)
8927 return isl_stat_error;
8928 if (!equal)
8929 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8930 "domain and range don't match", return isl_stat_error);
8931 return isl_stat_ok;
8935 * returns range - domain
8937 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8939 int i;
8940 isl_space *space;
8941 struct isl_set *result;
8943 if (isl_map_check_transformation(map) < 0)
8944 goto error;
8945 space = isl_map_get_space(map);
8946 space = isl_space_domain(space);
8947 result = isl_set_alloc_space(space, map->n, 0);
8948 if (!result)
8949 goto error;
8950 for (i = 0; i < map->n; ++i)
8951 result = isl_set_add_basic_set(result,
8952 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8953 isl_map_free(map);
8954 return result;
8955 error:
8956 isl_map_free(map);
8957 return NULL;
8961 * returns [domain -> range] -> range - domain
8963 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8964 __isl_take isl_basic_map *bmap)
8966 int i, k;
8967 isl_space *space;
8968 isl_basic_map *domain;
8969 isl_size nparam, n;
8970 isl_size total;
8972 if (isl_basic_map_check_transformation(bmap) < 0)
8973 return isl_basic_map_free(bmap);
8975 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8976 n = isl_basic_map_dim(bmap, isl_dim_in);
8977 if (nparam < 0 || n < 0)
8978 return isl_basic_map_free(bmap);
8980 space = isl_basic_map_get_space(bmap);
8981 space = isl_space_from_range(isl_space_domain(space));
8982 domain = isl_basic_map_universe(space);
8984 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8985 bmap = isl_basic_map_apply_range(bmap, domain);
8986 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8988 total = isl_basic_map_dim(bmap, isl_dim_all);
8989 if (total < 0)
8990 return isl_basic_map_free(bmap);
8992 for (i = 0; i < n; ++i) {
8993 k = isl_basic_map_alloc_equality(bmap);
8994 if (k < 0)
8995 goto error;
8996 isl_seq_clr(bmap->eq[k], 1 + total);
8997 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8998 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8999 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
9002 bmap = isl_basic_map_gauss(bmap, NULL);
9003 return isl_basic_map_finalize(bmap);
9004 error:
9005 isl_basic_map_free(bmap);
9006 return NULL;
9010 * returns [domain -> range] -> range - domain
9012 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
9014 if (isl_map_check_transformation(map) < 0)
9015 return isl_map_free(map);
9017 return isl_map_transform(map, &isl_space_range_map,
9018 &isl_basic_map_deltas_map);
9021 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9023 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
9025 isl_space *space;
9026 isl_map *map;
9028 space = isl_space_map_from_set(isl_set_get_space(deltas));
9029 map = isl_map_deltas_map(isl_map_universe(space));
9030 map = isl_map_intersect_range(map, deltas);
9032 return isl_set_unwrap(isl_map_domain(map));
9035 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
9037 isl_size n_in, n_out;
9039 n_in = isl_space_dim(space, isl_dim_in);
9040 n_out = isl_space_dim(space, isl_dim_out);
9041 if (n_in < 0 || n_out < 0)
9042 goto error;
9043 if (n_in != n_out)
9044 isl_die(space->ctx, isl_error_invalid,
9045 "number of input and output dimensions needs to be "
9046 "the same", goto error);
9047 return isl_basic_map_equal(space, n_in);
9048 error:
9049 isl_space_free(space);
9050 return NULL;
9053 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
9055 return isl_map_from_basic_map(isl_basic_map_identity(space));
9058 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
9060 isl_space *space = isl_set_get_space(set);
9061 isl_map *id;
9062 id = isl_map_identity(isl_space_map_from_set(space));
9063 return isl_map_intersect_range(id, set);
9066 /* Construct a basic set with all set dimensions having only non-negative
9067 * values.
9069 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9070 __isl_take isl_space *space)
9072 int i;
9073 isl_size nparam;
9074 isl_size dim;
9075 isl_size total;
9076 struct isl_basic_set *bset;
9078 nparam = isl_space_dim(space, isl_dim_param);
9079 dim = isl_space_dim(space, isl_dim_set);
9080 total = isl_space_dim(space, isl_dim_all);
9081 if (nparam < 0 || dim < 0 || total < 0)
9082 space = isl_space_free(space);
9083 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9084 if (!bset)
9085 return NULL;
9086 for (i = 0; i < dim; ++i) {
9087 int k = isl_basic_set_alloc_inequality(bset);
9088 if (k < 0)
9089 goto error;
9090 isl_seq_clr(bset->ineq[k], 1 + total);
9091 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9093 return bset;
9094 error:
9095 isl_basic_set_free(bset);
9096 return NULL;
9099 /* Construct the half-space x_pos >= 0.
9101 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9102 int pos)
9104 int k;
9105 isl_size total;
9106 isl_basic_set *nonneg;
9108 total = isl_space_dim(space, isl_dim_all);
9109 if (total < 0)
9110 space = isl_space_free(space);
9111 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9112 k = isl_basic_set_alloc_inequality(nonneg);
9113 if (k < 0)
9114 goto error;
9115 isl_seq_clr(nonneg->ineq[k], 1 + total);
9116 isl_int_set_si(nonneg->ineq[k][pos], 1);
9118 return isl_basic_set_finalize(nonneg);
9119 error:
9120 isl_basic_set_free(nonneg);
9121 return NULL;
9124 /* Construct the half-space x_pos <= -1.
9126 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9127 int pos)
9129 int k;
9130 isl_size total;
9131 isl_basic_set *neg;
9133 total = isl_space_dim(space, isl_dim_all);
9134 if (total < 0)
9135 space = isl_space_free(space);
9136 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9137 k = isl_basic_set_alloc_inequality(neg);
9138 if (k < 0)
9139 goto error;
9140 isl_seq_clr(neg->ineq[k], 1 + total);
9141 isl_int_set_si(neg->ineq[k][0], -1);
9142 isl_int_set_si(neg->ineq[k][pos], -1);
9144 return isl_basic_set_finalize(neg);
9145 error:
9146 isl_basic_set_free(neg);
9147 return NULL;
9150 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9151 enum isl_dim_type type, unsigned first, unsigned n)
9153 int i;
9154 unsigned offset;
9155 isl_basic_set *nonneg;
9156 isl_basic_set *neg;
9158 if (n == 0)
9159 return set;
9161 if (isl_set_check_range(set, type, first, n) < 0)
9162 return isl_set_free(set);
9164 offset = pos(set->dim, type);
9165 for (i = 0; i < n; ++i) {
9166 nonneg = nonneg_halfspace(isl_set_get_space(set),
9167 offset + first + i);
9168 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9170 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9173 return set;
9176 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9177 int len,
9178 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9179 void *user)
9181 isl_set *half;
9183 if (!set)
9184 return isl_stat_error;
9185 if (isl_set_plain_is_empty(set)) {
9186 isl_set_free(set);
9187 return isl_stat_ok;
9189 if (first == len)
9190 return fn(set, signs, user);
9192 signs[first] = 1;
9193 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9194 1 + first));
9195 half = isl_set_intersect(half, isl_set_copy(set));
9196 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9197 goto error;
9199 signs[first] = -1;
9200 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9201 1 + first));
9202 half = isl_set_intersect(half, set);
9203 return foreach_orthant(half, signs, first + 1, len, fn, user);
9204 error:
9205 isl_set_free(set);
9206 return isl_stat_error;
9209 /* Call "fn" on the intersections of "set" with each of the orthants
9210 * (except for obviously empty intersections). The orthant is identified
9211 * by the signs array, with each entry having value 1 or -1 according
9212 * to the sign of the corresponding variable.
9214 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9215 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9216 void *user)
9218 isl_size nparam;
9219 isl_size nvar;
9220 int *signs;
9221 isl_stat r;
9223 if (!set)
9224 return isl_stat_error;
9225 if (isl_set_plain_is_empty(set))
9226 return isl_stat_ok;
9228 nparam = isl_set_dim(set, isl_dim_param);
9229 nvar = isl_set_dim(set, isl_dim_set);
9230 if (nparam < 0 || nvar < 0)
9231 return isl_stat_error;
9233 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9235 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9236 fn, user);
9238 free(signs);
9240 return r;
9243 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9245 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9248 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9249 __isl_keep isl_basic_map *bmap2)
9251 isl_bool is_subset;
9252 struct isl_map *map1;
9253 struct isl_map *map2;
9255 if (!bmap1 || !bmap2)
9256 return isl_bool_error;
9258 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9259 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9261 is_subset = isl_map_is_subset(map1, map2);
9263 isl_map_free(map1);
9264 isl_map_free(map2);
9266 return is_subset;
9269 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9270 __isl_keep isl_basic_set *bset2)
9272 return isl_basic_map_is_subset(bset1, bset2);
9275 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9276 __isl_keep isl_basic_map *bmap2)
9278 isl_bool is_subset;
9280 if (!bmap1 || !bmap2)
9281 return isl_bool_error;
9282 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9283 if (is_subset != isl_bool_true)
9284 return is_subset;
9285 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9286 return is_subset;
9289 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9290 __isl_keep isl_basic_set *bset2)
9292 return isl_basic_map_is_equal(
9293 bset_to_bmap(bset1), bset_to_bmap(bset2));
9296 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9298 int i;
9299 int is_empty;
9301 if (!map)
9302 return isl_bool_error;
9303 for (i = 0; i < map->n; ++i) {
9304 is_empty = isl_basic_map_is_empty(map->p[i]);
9305 if (is_empty < 0)
9306 return isl_bool_error;
9307 if (!is_empty)
9308 return isl_bool_false;
9310 return isl_bool_true;
9313 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9315 return map ? map->n == 0 : isl_bool_error;
9318 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9320 return set ? set->n == 0 : isl_bool_error;
9323 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9325 return isl_map_is_empty(set_to_map(set));
9328 #undef TYPE
9329 #define TYPE isl_basic_map
9331 static
9332 #include "isl_type_has_equal_space_bin_templ.c"
9333 #include "isl_type_check_equal_space_templ.c"
9335 /* Check that "bset1" and "bset2" live in the same space,
9336 * reporting an error if they do not.
9338 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9339 __isl_keep isl_basic_set *bset2)
9341 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9342 bset_to_bmap(bset1));
9345 #undef TYPE
9346 #define TYPE isl_map
9348 #include "isl_type_has_equal_space_bin_templ.c"
9349 #include "isl_type_check_equal_space_templ.c"
9350 #include "isl_type_has_space_templ.c"
9352 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9353 __isl_keep isl_set *set2)
9355 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9358 #undef TYPE1
9359 #define TYPE1 isl_map
9360 #undef TYPE2
9361 #define TYPE2 isl_basic_map
9362 #undef TYPE_PAIR
9363 #define TYPE_PAIR isl_map_basic_map
9365 static
9366 #include "isl_type_has_equal_space_templ.c"
9367 #include "isl_type_check_equal_space_templ.c"
9369 /* Check that "set" and "bset" live in the same space,
9370 * reporting an error if they do not.
9372 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9373 __isl_keep isl_basic_set *bset)
9375 return isl_map_basic_map_check_equal_space(set_to_map(set),
9376 bset_to_bmap(bset));
9379 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9381 isl_bool is_subset;
9383 if (!map1 || !map2)
9384 return isl_bool_error;
9385 is_subset = isl_map_is_subset(map1, map2);
9386 if (is_subset != isl_bool_true)
9387 return is_subset;
9388 is_subset = isl_map_is_subset(map2, map1);
9389 return is_subset;
9392 /* Is "map1" equal to "map2"?
9394 * First check if they are obviously equal.
9395 * If not, then perform a more detailed analysis.
9397 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9399 isl_bool equal;
9401 equal = isl_map_plain_is_equal(map1, map2);
9402 if (equal < 0 || equal)
9403 return equal;
9404 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9407 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9408 __isl_keep isl_basic_map *bmap2)
9410 isl_bool is_subset;
9412 if (!bmap1 || !bmap2)
9413 return isl_bool_error;
9414 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9415 if (is_subset != isl_bool_true)
9416 return is_subset;
9417 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9418 return isl_bool_not(is_subset);
9421 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9422 __isl_keep isl_map *map2)
9424 isl_bool is_subset;
9426 if (!map1 || !map2)
9427 return isl_bool_error;
9428 is_subset = isl_map_is_subset(map1, map2);
9429 if (is_subset != isl_bool_true)
9430 return is_subset;
9431 is_subset = isl_map_is_subset(map2, map1);
9432 return isl_bool_not(is_subset);
9435 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9436 __isl_keep isl_set *set2)
9438 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9441 /* Is "bmap" obviously equal to the universe with the same space?
9443 * That is, does it not have any constraints?
9445 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9447 if (!bmap)
9448 return isl_bool_error;
9449 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9452 /* Is "bset" obviously equal to the universe with the same space?
9454 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9456 return isl_basic_map_plain_is_universe(bset);
9459 /* If "c" does not involve any existentially quantified variables,
9460 * then set *univ to false and abort
9462 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9464 isl_bool *univ = user;
9465 isl_size n;
9467 n = isl_constraint_dim(c, isl_dim_div);
9468 if (n < 0)
9469 c = isl_constraint_free(c);
9470 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9471 isl_constraint_free(c);
9472 if (*univ < 0 || !*univ)
9473 return isl_stat_error;
9474 return isl_stat_ok;
9477 /* Is "bmap" equal to the universe with the same space?
9479 * First check if it is obviously equal to the universe.
9480 * If not and if there are any constraints not involving
9481 * existentially quantified variables, then it is certainly
9482 * not equal to the universe.
9483 * Otherwise, check if the universe is a subset of "bmap".
9485 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9487 isl_size n_div;
9488 isl_bool univ;
9489 isl_basic_map *test;
9491 univ = isl_basic_map_plain_is_universe(bmap);
9492 if (univ < 0 || univ)
9493 return univ;
9494 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9495 if (n_div < 0)
9496 return isl_bool_error;
9497 if (n_div == 0)
9498 return isl_bool_false;
9499 univ = isl_bool_true;
9500 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9501 univ)
9502 return isl_bool_error;
9503 if (univ < 0 || !univ)
9504 return univ;
9505 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9506 univ = isl_basic_map_is_subset(test, bmap);
9507 isl_basic_map_free(test);
9508 return univ;
9511 /* Is "bset" equal to the universe with the same space?
9513 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9515 return isl_basic_map_is_universe(bset);
9518 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9520 int i;
9522 if (!map)
9523 return isl_bool_error;
9525 for (i = 0; i < map->n; ++i) {
9526 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9527 if (r < 0 || r)
9528 return r;
9531 return isl_bool_false;
9534 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9536 return isl_map_plain_is_universe(set_to_map(set));
9539 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9541 struct isl_basic_set *bset = NULL;
9542 struct isl_vec *sample = NULL;
9543 isl_bool empty, non_empty;
9545 if (!bmap)
9546 return isl_bool_error;
9548 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9549 return isl_bool_true;
9551 if (isl_basic_map_plain_is_universe(bmap))
9552 return isl_bool_false;
9554 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9555 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9556 copy = isl_basic_map_remove_redundancies(copy);
9557 empty = isl_basic_map_plain_is_empty(copy);
9558 isl_basic_map_free(copy);
9559 return empty;
9562 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9563 if (non_empty < 0)
9564 return isl_bool_error;
9565 if (non_empty)
9566 return isl_bool_false;
9567 isl_vec_free(bmap->sample);
9568 bmap->sample = NULL;
9569 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9570 if (!bset)
9571 return isl_bool_error;
9572 sample = isl_basic_set_sample_vec(bset);
9573 if (!sample)
9574 return isl_bool_error;
9575 empty = sample->size == 0;
9576 isl_vec_free(bmap->sample);
9577 bmap->sample = sample;
9578 if (empty)
9579 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9581 return empty;
9584 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9586 if (!bmap)
9587 return isl_bool_error;
9588 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9591 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9593 if (!bset)
9594 return isl_bool_error;
9595 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9598 /* Is "bmap" known to be non-empty?
9600 * That is, is the cached sample still valid?
9602 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9604 isl_size total;
9606 if (!bmap)
9607 return isl_bool_error;
9608 if (!bmap->sample)
9609 return isl_bool_false;
9610 total = isl_basic_map_dim(bmap, isl_dim_all);
9611 if (total < 0)
9612 return isl_bool_error;
9613 if (bmap->sample->size != 1 + total)
9614 return isl_bool_false;
9615 return isl_basic_map_contains(bmap, bmap->sample);
9618 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9620 return isl_basic_map_is_empty(bset_to_bmap(bset));
9623 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9624 __isl_take isl_basic_map *bmap2)
9626 struct isl_map *map;
9628 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9629 goto error;
9631 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9632 if (!map)
9633 goto error;
9634 map = isl_map_add_basic_map(map, bmap1);
9635 map = isl_map_add_basic_map(map, bmap2);
9636 return map;
9637 error:
9638 isl_basic_map_free(bmap1);
9639 isl_basic_map_free(bmap2);
9640 return NULL;
9643 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9644 __isl_take isl_basic_set *bset2)
9646 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9647 bset_to_bmap(bset2)));
9650 /* Order divs such that any div only depends on previous divs */
9651 __isl_give isl_basic_map *isl_basic_map_order_divs(
9652 __isl_take isl_basic_map *bmap)
9654 int i;
9655 isl_size off;
9657 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9658 if (off < 0)
9659 return isl_basic_map_free(bmap);
9661 for (i = 0; i < bmap->n_div; ++i) {
9662 int pos;
9663 if (isl_int_is_zero(bmap->div[i][0]))
9664 continue;
9665 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9666 bmap->n_div-i);
9667 if (pos == -1)
9668 continue;
9669 if (pos == 0)
9670 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9671 "integer division depends on itself",
9672 return isl_basic_map_free(bmap));
9673 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9674 if (!bmap)
9675 return NULL;
9676 --i;
9678 return bmap;
9681 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9683 int i;
9685 if (!map)
9686 return 0;
9688 for (i = 0; i < map->n; ++i) {
9689 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9690 if (!map->p[i])
9691 goto error;
9694 return map;
9695 error:
9696 isl_map_free(map);
9697 return NULL;
9700 /* Sort the local variables of "bset".
9702 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9703 __isl_take isl_basic_set *bset)
9705 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9708 /* Apply the expansion computed by isl_merge_divs.
9709 * The expansion itself is given by "exp" while the resulting
9710 * list of divs is given by "div".
9712 * Move the integer divisions of "bmap" into the right position
9713 * according to "exp" and then introduce the additional integer
9714 * divisions, adding div constraints.
9715 * The moving should be done first to avoid moving coefficients
9716 * in the definitions of the extra integer divisions.
9718 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9719 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9721 int i, j;
9722 int n_div;
9724 bmap = isl_basic_map_cow(bmap);
9725 if (!bmap || !div)
9726 goto error;
9728 if (div->n_row < bmap->n_div)
9729 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9730 "not an expansion", goto error);
9732 n_div = bmap->n_div;
9733 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9734 2 * (div->n_row - n_div));
9736 for (i = n_div; i < div->n_row; ++i)
9737 if (isl_basic_map_alloc_div(bmap) < 0)
9738 goto error;
9740 for (j = n_div - 1; j >= 0; --j) {
9741 if (exp[j] == j)
9742 break;
9743 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9744 if (!bmap)
9745 goto error;
9747 j = 0;
9748 for (i = 0; i < div->n_row; ++i) {
9749 if (j < n_div && exp[j] == i) {
9750 j++;
9751 } else {
9752 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9753 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9754 continue;
9755 bmap = isl_basic_map_add_div_constraints(bmap, i);
9756 if (!bmap)
9757 goto error;
9761 isl_mat_free(div);
9762 return bmap;
9763 error:
9764 isl_basic_map_free(bmap);
9765 isl_mat_free(div);
9766 return NULL;
9769 /* Apply the expansion computed by isl_merge_divs.
9770 * The expansion itself is given by "exp" while the resulting
9771 * list of divs is given by "div".
9773 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9774 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9776 return isl_basic_map_expand_divs(bset, div, exp);
9779 /* Look for a div in dst that corresponds to the div "div" in src.
9780 * The divs before "div" in src and dst are assumed to be the same.
9782 * Return the position of the corresponding div in dst
9783 * if there is one. Otherwise, return a position beyond the integer divisions.
9784 * Return -1 on error.
9786 static int find_div(__isl_keep isl_basic_map *dst,
9787 __isl_keep isl_basic_map *src, unsigned div)
9789 int i;
9790 isl_size n_div;
9791 isl_size v_div;
9793 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9794 n_div = isl_basic_map_dim(dst, isl_dim_div);
9795 if (n_div < 0 || v_div < 0)
9796 return -1;
9797 isl_assert(dst->ctx, div <= n_div, return -1);
9798 for (i = div; i < n_div; ++i)
9799 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9800 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9801 n_div - div) == -1)
9802 return i;
9803 return n_div;
9806 /* Align the divs of "dst" to those of "src", adding divs from "src"
9807 * if needed. That is, make sure that the first src->n_div divs
9808 * of the result are equal to those of src.
9809 * The integer division of "src" are assumed to be ordered.
9811 * The integer divisions are swapped into the right position
9812 * (possibly after adding them first). This may result
9813 * in the remaining integer divisions appearing in the wrong order,
9814 * i.e., with some integer division appearing before
9815 * some other integer division on which it depends.
9816 * The integer divisions therefore need to be ordered.
9817 * This will not affect the integer divisions aligned to those of "src",
9818 * since "src" is assumed to have ordered integer divisions.
9820 * The result is not finalized as by design it will have redundant
9821 * divs if any divs from "src" were copied.
9823 __isl_give isl_basic_map *isl_basic_map_align_divs(
9824 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9826 int i;
9827 isl_bool known;
9828 int extended;
9829 isl_size v_div;
9830 isl_size dst_n_div;
9832 if (!dst || !src)
9833 return isl_basic_map_free(dst);
9835 if (src->n_div == 0)
9836 return dst;
9838 known = isl_basic_map_divs_known(src);
9839 if (known < 0)
9840 return isl_basic_map_free(dst);
9841 if (!known)
9842 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9843 "some src divs are unknown",
9844 return isl_basic_map_free(dst));
9846 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9847 if (v_div < 0)
9848 return isl_basic_map_free(dst);
9850 extended = 0;
9851 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9852 if (dst_n_div < 0)
9853 dst = isl_basic_map_free(dst);
9854 for (i = 0; i < src->n_div; ++i) {
9855 int j = find_div(dst, src, i);
9856 if (j < 0)
9857 dst = isl_basic_map_free(dst);
9858 if (j == dst_n_div) {
9859 if (!extended) {
9860 int extra = src->n_div - i;
9861 dst = isl_basic_map_cow(dst);
9862 if (!dst)
9863 return isl_basic_map_free(dst);
9864 dst = isl_basic_map_extend(dst,
9865 extra, 0, 2 * extra);
9866 extended = 1;
9868 j = isl_basic_map_alloc_div(dst);
9869 if (j < 0)
9870 return isl_basic_map_free(dst);
9871 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9872 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9873 dst_n_div++;
9874 dst = isl_basic_map_add_div_constraints(dst, j);
9875 if (!dst)
9876 return isl_basic_map_free(dst);
9878 if (j != i)
9879 dst = isl_basic_map_swap_div(dst, i, j);
9880 if (!dst)
9881 return isl_basic_map_free(dst);
9883 return isl_basic_map_order_divs(dst);
9886 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9888 int i;
9890 if (!map)
9891 return NULL;
9892 if (map->n == 0)
9893 return map;
9894 map = isl_map_compute_divs(map);
9895 map = isl_map_order_divs(map);
9896 map = isl_map_cow(map);
9897 if (!map)
9898 return NULL;
9900 for (i = 1; i < map->n; ++i)
9901 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9902 for (i = 1; i < map->n; ++i) {
9903 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9904 if (!map->p[i])
9905 return isl_map_free(map);
9908 map = isl_map_unmark_normalized(map);
9909 return map;
9912 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9914 return isl_map_align_divs_internal(map);
9917 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9919 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9922 /* Align the divs of the basic maps in "map" to those
9923 * of the basic maps in "list", as well as to the other basic maps in "map".
9924 * The elements in "list" are assumed to have known divs.
9926 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9927 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9929 int i;
9930 isl_size n;
9932 n = isl_basic_map_list_n_basic_map(list);
9933 map = isl_map_compute_divs(map);
9934 map = isl_map_cow(map);
9935 if (!map || n < 0)
9936 return isl_map_free(map);
9937 if (map->n == 0)
9938 return map;
9940 for (i = 0; i < n; ++i) {
9941 isl_basic_map *bmap;
9943 bmap = isl_basic_map_list_get_basic_map(list, i);
9944 bmap = isl_basic_map_order_divs(bmap);
9945 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9946 isl_basic_map_free(bmap);
9948 if (!map->p[0])
9949 return isl_map_free(map);
9951 return isl_map_align_divs_internal(map);
9954 /* Align the divs of each element of "list" to those of "bmap".
9955 * Both "bmap" and the elements of "list" are assumed to have known divs.
9957 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9958 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9960 int i;
9961 isl_size n;
9963 n = isl_basic_map_list_n_basic_map(list);
9964 if (n < 0 || !bmap)
9965 return isl_basic_map_list_free(list);
9967 for (i = 0; i < n; ++i) {
9968 isl_basic_map *bmap_i;
9970 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9971 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9972 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9975 return list;
9978 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9979 __isl_take isl_map *map)
9981 isl_bool ok;
9983 isl_map_align_params_set(&map, &set);
9984 ok = isl_map_compatible_domain(map, set);
9985 if (ok < 0)
9986 goto error;
9987 if (!ok)
9988 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9989 "incompatible spaces", goto error);
9990 map = isl_map_intersect_domain(map, set);
9991 set = isl_map_range(map);
9992 return set;
9993 error:
9994 isl_set_free(set);
9995 isl_map_free(map);
9996 return NULL;
9999 /* There is no need to cow as removing empty parts doesn't change
10000 * the meaning of the set.
10002 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
10004 int i;
10006 if (!map)
10007 return NULL;
10009 for (i = map->n - 1; i >= 0; --i)
10010 map = remove_if_empty(map, i);
10012 return map;
10015 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10017 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10020 /* Create a binary relation that maps the shared initial "pos" dimensions
10021 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10023 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10024 __isl_keep isl_basic_set *bset2, int pos)
10026 isl_basic_map *bmap1;
10027 isl_basic_map *bmap2;
10029 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10030 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10031 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10032 isl_dim_out, 0, pos);
10033 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10034 isl_dim_out, 0, pos);
10035 return isl_basic_map_range_product(bmap1, bmap2);
10038 /* Given two basic sets bset1 and bset2, compute the maximal difference
10039 * between the values of dimension pos in bset1 and those in bset2
10040 * for any common value of the parameters and dimensions preceding pos.
10042 static enum isl_lp_result basic_set_maximal_difference_at(
10043 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10044 int pos, isl_int *opt)
10046 isl_basic_map *bmap1;
10047 struct isl_ctx *ctx;
10048 struct isl_vec *obj;
10049 isl_size total;
10050 isl_size nparam;
10051 isl_size dim1;
10052 enum isl_lp_result res;
10054 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10055 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10056 if (nparam < 0 || dim1 < 0 || !bset2)
10057 return isl_lp_error;
10059 bmap1 = join_initial(bset1, bset2, pos);
10060 total = isl_basic_map_dim(bmap1, isl_dim_all);
10061 if (total < 0)
10062 return isl_lp_error;
10064 ctx = bmap1->ctx;
10065 obj = isl_vec_alloc(ctx, 1 + total);
10066 if (!obj)
10067 goto error;
10068 isl_seq_clr(obj->block.data, 1 + total);
10069 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10070 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10071 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10072 opt, NULL, NULL);
10073 isl_basic_map_free(bmap1);
10074 isl_vec_free(obj);
10075 return res;
10076 error:
10077 isl_basic_map_free(bmap1);
10078 return isl_lp_error;
10081 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10082 * for any common value of the parameters and dimensions preceding pos
10083 * in both basic sets, the values of dimension pos in bset1 are
10084 * smaller or larger than those in bset2.
10086 * Returns
10087 * 1 if bset1 follows bset2
10088 * -1 if bset1 precedes bset2
10089 * 0 if bset1 and bset2 are incomparable
10090 * -2 if some error occurred.
10092 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10093 __isl_keep isl_basic_set *bset2, int pos)
10095 isl_int opt;
10096 enum isl_lp_result res;
10097 int cmp;
10099 isl_int_init(opt);
10101 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10103 if (res == isl_lp_empty)
10104 cmp = 0;
10105 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10106 res == isl_lp_unbounded)
10107 cmp = 1;
10108 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10109 cmp = -1;
10110 else
10111 cmp = -2;
10113 isl_int_clear(opt);
10114 return cmp;
10117 /* Given two basic sets bset1 and bset2, check whether
10118 * for any common value of the parameters and dimensions preceding pos
10119 * there is a value of dimension pos in bset1 that is larger
10120 * than a value of the same dimension in bset2.
10122 * Return
10123 * 1 if there exists such a pair
10124 * 0 if there is no such pair, but there is a pair of equal values
10125 * -1 otherwise
10126 * -2 if some error occurred.
10128 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10129 __isl_keep isl_basic_set *bset2, int pos)
10131 isl_bool empty;
10132 isl_basic_map *bmap;
10133 isl_size dim1;
10135 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10136 if (dim1 < 0)
10137 return -2;
10138 bmap = join_initial(bset1, bset2, pos);
10139 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10140 isl_dim_out, dim1 - pos);
10141 empty = isl_basic_map_is_empty(bmap);
10142 if (empty < 0)
10143 goto error;
10144 if (empty) {
10145 isl_basic_map_free(bmap);
10146 return -1;
10148 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10149 isl_dim_out, dim1 - pos);
10150 empty = isl_basic_map_is_empty(bmap);
10151 if (empty < 0)
10152 goto error;
10153 isl_basic_map_free(bmap);
10154 if (empty)
10155 return 0;
10156 return 1;
10157 error:
10158 isl_basic_map_free(bmap);
10159 return -2;
10162 /* Given two sets set1 and set2, check whether
10163 * for any common value of the parameters and dimensions preceding pos
10164 * there is a value of dimension pos in set1 that is larger
10165 * than a value of the same dimension in set2.
10167 * Return
10168 * 1 if there exists such a pair
10169 * 0 if there is no such pair, but there is a pair of equal values
10170 * -1 otherwise
10171 * -2 if some error occurred.
10173 int isl_set_follows_at(__isl_keep isl_set *set1,
10174 __isl_keep isl_set *set2, int pos)
10176 int i, j;
10177 int follows = -1;
10179 if (!set1 || !set2)
10180 return -2;
10182 for (i = 0; i < set1->n; ++i)
10183 for (j = 0; j < set2->n; ++j) {
10184 int f;
10185 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10186 if (f == 1 || f == -2)
10187 return f;
10188 if (f > follows)
10189 follows = f;
10192 return follows;
10195 static isl_bool isl_basic_map_plain_has_fixed_var(
10196 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10198 int i;
10199 int d;
10200 isl_size total;
10202 total = isl_basic_map_dim(bmap, isl_dim_all);
10203 if (total < 0)
10204 return isl_bool_error;
10205 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10206 for (; d+1 > pos; --d)
10207 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10208 break;
10209 if (d != pos)
10210 continue;
10211 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10212 return isl_bool_false;
10213 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10214 return isl_bool_false;
10215 if (!isl_int_is_one(bmap->eq[i][1+d]))
10216 return isl_bool_false;
10217 if (val)
10218 isl_int_neg(*val, bmap->eq[i][0]);
10219 return isl_bool_true;
10221 return isl_bool_false;
10224 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10225 unsigned pos, isl_int *val)
10227 int i;
10228 isl_int v;
10229 isl_int tmp;
10230 isl_bool fixed;
10232 if (!map)
10233 return isl_bool_error;
10234 if (map->n == 0)
10235 return isl_bool_false;
10236 if (map->n == 1)
10237 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10238 isl_int_init(v);
10239 isl_int_init(tmp);
10240 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10241 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10242 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10243 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10244 fixed = isl_bool_false;
10246 if (val)
10247 isl_int_set(*val, v);
10248 isl_int_clear(tmp);
10249 isl_int_clear(v);
10250 return fixed;
10253 static isl_bool isl_basic_set_plain_has_fixed_var(
10254 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10256 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10257 pos, val);
10260 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10261 enum isl_dim_type type, unsigned pos, isl_int *val)
10263 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10264 return isl_bool_error;
10265 return isl_basic_map_plain_has_fixed_var(bmap,
10266 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10269 /* If "bmap" obviously lies on a hyperplane where the given dimension
10270 * has a fixed value, then return that value.
10271 * Otherwise return NaN.
10273 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10274 __isl_keep isl_basic_map *bmap,
10275 enum isl_dim_type type, unsigned pos)
10277 isl_ctx *ctx;
10278 isl_val *v;
10279 isl_bool fixed;
10281 if (!bmap)
10282 return NULL;
10283 ctx = isl_basic_map_get_ctx(bmap);
10284 v = isl_val_alloc(ctx);
10285 if (!v)
10286 return NULL;
10287 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10288 if (fixed < 0)
10289 return isl_val_free(v);
10290 if (fixed) {
10291 isl_int_set_si(v->d, 1);
10292 return v;
10294 isl_val_free(v);
10295 return isl_val_nan(ctx);
10298 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10299 enum isl_dim_type type, unsigned pos, isl_int *val)
10301 if (isl_map_check_range(map, type, pos, 1) < 0)
10302 return isl_bool_error;
10303 return isl_map_plain_has_fixed_var(map,
10304 map_offset(map, type) - 1 + pos, val);
10307 /* If "map" obviously lies on a hyperplane where the given dimension
10308 * has a fixed value, then return that value.
10309 * Otherwise return NaN.
10311 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10312 enum isl_dim_type type, unsigned pos)
10314 isl_ctx *ctx;
10315 isl_val *v;
10316 isl_bool fixed;
10318 if (!map)
10319 return NULL;
10320 ctx = isl_map_get_ctx(map);
10321 v = isl_val_alloc(ctx);
10322 if (!v)
10323 return NULL;
10324 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10325 if (fixed < 0)
10326 return isl_val_free(v);
10327 if (fixed) {
10328 isl_int_set_si(v->d, 1);
10329 return v;
10331 isl_val_free(v);
10332 return isl_val_nan(ctx);
10335 /* If "set" obviously lies on a hyperplane where the given dimension
10336 * has a fixed value, then return that value.
10337 * Otherwise return NaN.
10339 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10340 enum isl_dim_type type, unsigned pos)
10342 return isl_map_plain_get_val_if_fixed(set, type, pos);
10345 /* Return a sequence of values in the same space as "set"
10346 * that are equal to the corresponding set dimensions of "set"
10347 * for those set dimensions that obviously lie on a hyperplane
10348 * where the dimension has a fixed value.
10349 * The other elements are set to NaN.
10351 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10352 __isl_keep isl_set *set)
10354 int i;
10355 isl_size n;
10356 isl_space *space;
10357 isl_multi_val *mv;
10359 space = isl_space_drop_all_params(isl_set_get_space(set));
10360 mv = isl_multi_val_alloc(space);
10361 n = isl_multi_val_size(mv);
10362 if (n < 0)
10363 return isl_multi_val_free(mv);
10365 for (i = 0; i < n; ++i) {
10366 isl_val *v;
10368 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10369 mv = isl_multi_val_set_val(mv, i, v);
10372 return mv;
10375 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10376 * then return this fixed value in *val.
10378 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10379 unsigned dim, isl_int *val)
10381 isl_size nparam;
10383 nparam = isl_basic_set_dim(bset, isl_dim_param);
10384 if (nparam < 0)
10385 return isl_bool_error;
10386 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10389 /* Return -1 if the constraint "c1" should be sorted before "c2"
10390 * and 1 if it should be sorted after "c2".
10391 * Return 0 if the two constraints are the same (up to the constant term).
10393 * In particular, if a constraint involves later variables than another
10394 * then it is sorted after this other constraint.
10395 * uset_gist depends on constraints without existentially quantified
10396 * variables sorting first.
10398 * For constraints that have the same latest variable, those
10399 * with the same coefficient for this latest variable (first in absolute value
10400 * and then in actual value) are grouped together.
10401 * This is useful for detecting pairs of constraints that can
10402 * be chained in their printed representation.
10404 * Finally, within a group, constraints are sorted according to
10405 * their coefficients (excluding the constant term).
10407 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10409 isl_int **c1 = (isl_int **) p1;
10410 isl_int **c2 = (isl_int **) p2;
10411 int l1, l2;
10412 unsigned size = *(unsigned *) arg;
10413 int cmp;
10415 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10416 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10418 if (l1 != l2)
10419 return l1 - l2;
10421 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10422 if (cmp != 0)
10423 return cmp;
10424 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10425 if (cmp != 0)
10426 return -cmp;
10428 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10431 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10432 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10433 * and 0 if the two constraints are the same (up to the constant term).
10435 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10436 isl_int *c1, isl_int *c2)
10438 isl_size total;
10439 unsigned size;
10441 total = isl_basic_map_dim(bmap, isl_dim_all);
10442 if (total < 0)
10443 return -2;
10444 size = total;
10445 return sort_constraint_cmp(&c1, &c2, &size);
10448 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10449 __isl_take isl_basic_map *bmap)
10451 isl_size total;
10452 unsigned size;
10454 if (!bmap)
10455 return NULL;
10456 if (bmap->n_ineq == 0)
10457 return bmap;
10458 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10459 return bmap;
10460 total = isl_basic_map_dim(bmap, isl_dim_all);
10461 if (total < 0)
10462 return isl_basic_map_free(bmap);
10463 size = total;
10464 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10465 &sort_constraint_cmp, &size) < 0)
10466 return isl_basic_map_free(bmap);
10467 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10468 return bmap;
10471 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10472 __isl_take isl_basic_set *bset)
10474 isl_basic_map *bmap = bset_to_bmap(bset);
10475 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10478 __isl_give isl_basic_map *isl_basic_map_normalize(
10479 __isl_take isl_basic_map *bmap)
10481 bmap = isl_basic_map_remove_redundancies(bmap);
10482 bmap = isl_basic_map_sort_constraints(bmap);
10483 return bmap;
10485 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10486 __isl_keep isl_basic_map *bmap2)
10488 int i, cmp;
10489 isl_size total;
10490 isl_space *space1, *space2;
10492 if (!bmap1 || !bmap2)
10493 return -1;
10495 if (bmap1 == bmap2)
10496 return 0;
10497 space1 = isl_basic_map_peek_space(bmap1);
10498 space2 = isl_basic_map_peek_space(bmap2);
10499 cmp = isl_space_cmp(space1, space2);
10500 if (cmp)
10501 return cmp;
10502 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10503 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10504 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10505 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10506 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10507 return 0;
10508 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10509 return 1;
10510 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10511 return -1;
10512 if (bmap1->n_eq != bmap2->n_eq)
10513 return bmap1->n_eq - bmap2->n_eq;
10514 if (bmap1->n_ineq != bmap2->n_ineq)
10515 return bmap1->n_ineq - bmap2->n_ineq;
10516 if (bmap1->n_div != bmap2->n_div)
10517 return bmap1->n_div - bmap2->n_div;
10518 total = isl_basic_map_dim(bmap1, isl_dim_all);
10519 if (total < 0)
10520 return -1;
10521 for (i = 0; i < bmap1->n_eq; ++i) {
10522 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10523 if (cmp)
10524 return cmp;
10526 for (i = 0; i < bmap1->n_ineq; ++i) {
10527 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10528 if (cmp)
10529 return cmp;
10531 for (i = 0; i < bmap1->n_div; ++i) {
10532 isl_bool unknown1, unknown2;
10534 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10535 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10536 if (unknown1 < 0 || unknown2 < 0)
10537 return -1;
10538 if (unknown1 && unknown2)
10539 continue;
10540 if (unknown1)
10541 return 1;
10542 if (unknown2)
10543 return -1;
10544 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10545 if (cmp)
10546 return cmp;
10548 return 0;
10551 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10552 __isl_keep isl_basic_set *bset2)
10554 return isl_basic_map_plain_cmp(bset1, bset2);
10557 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10559 int i, cmp;
10561 if (set1 == set2)
10562 return 0;
10563 if (set1->n != set2->n)
10564 return set1->n - set2->n;
10566 for (i = 0; i < set1->n; ++i) {
10567 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10568 if (cmp)
10569 return cmp;
10572 return 0;
10575 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10576 __isl_keep isl_basic_map *bmap2)
10578 if (!bmap1 || !bmap2)
10579 return isl_bool_error;
10580 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10583 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10584 __isl_keep isl_basic_set *bset2)
10586 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10587 bset_to_bmap(bset2));
10590 static int qsort_bmap_cmp(const void *p1, const void *p2)
10592 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10593 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10595 return isl_basic_map_plain_cmp(bmap1, bmap2);
10598 /* Sort the basic maps of "map" and remove duplicate basic maps.
10600 * While removing basic maps, we make sure that the basic maps remain
10601 * sorted because isl_map_normalize expects the basic maps of the result
10602 * to be sorted.
10604 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10606 int i, j;
10608 map = isl_map_remove_empty_parts(map);
10609 if (!map)
10610 return NULL;
10611 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10612 for (i = map->n - 1; i >= 1; --i) {
10613 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10614 continue;
10615 isl_basic_map_free(map->p[i-1]);
10616 for (j = i; j < map->n; ++j)
10617 map->p[j - 1] = map->p[j];
10618 map->n--;
10621 return map;
10624 /* Remove obvious duplicates among the basic maps of "map".
10626 * Unlike isl_map_normalize, this function does not remove redundant
10627 * constraints and only removes duplicates that have exactly the same
10628 * constraints in the input. It does sort the constraints and
10629 * the basic maps to ease the detection of duplicates.
10631 * If "map" has already been normalized or if the basic maps are
10632 * disjoint, then there can be no duplicates.
10634 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10636 int i;
10637 isl_basic_map *bmap;
10639 if (!map)
10640 return NULL;
10641 if (map->n <= 1)
10642 return map;
10643 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10644 return map;
10645 for (i = 0; i < map->n; ++i) {
10646 bmap = isl_basic_map_copy(map->p[i]);
10647 bmap = isl_basic_map_sort_constraints(bmap);
10648 if (!bmap)
10649 return isl_map_free(map);
10650 isl_basic_map_free(map->p[i]);
10651 map->p[i] = bmap;
10654 map = sort_and_remove_duplicates(map);
10655 return map;
10658 /* We normalize in place, but if anything goes wrong we need
10659 * to return NULL, so we need to make sure we don't change the
10660 * meaning of any possible other copies of map.
10662 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10664 int i;
10665 struct isl_basic_map *bmap;
10667 if (!map)
10668 return NULL;
10669 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10670 return map;
10671 for (i = 0; i < map->n; ++i) {
10672 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10673 if (!bmap)
10674 goto error;
10675 isl_basic_map_free(map->p[i]);
10676 map->p[i] = bmap;
10679 map = sort_and_remove_duplicates(map);
10680 if (map)
10681 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10682 return map;
10683 error:
10684 isl_map_free(map);
10685 return NULL;
10688 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10690 return set_from_map(isl_map_normalize(set_to_map(set)));
10693 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10694 __isl_keep isl_map *map2)
10696 int i;
10697 isl_bool equal;
10699 if (!map1 || !map2)
10700 return isl_bool_error;
10702 if (map1 == map2)
10703 return isl_bool_true;
10704 equal = isl_map_has_equal_space(map1, map2);
10705 if (equal < 0 || !equal)
10706 return equal;
10708 map1 = isl_map_copy(map1);
10709 map2 = isl_map_copy(map2);
10710 map1 = isl_map_normalize(map1);
10711 map2 = isl_map_normalize(map2);
10712 if (!map1 || !map2)
10713 goto error;
10714 equal = map1->n == map2->n;
10715 for (i = 0; equal && i < map1->n; ++i) {
10716 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10717 if (equal < 0)
10718 goto error;
10720 isl_map_free(map1);
10721 isl_map_free(map2);
10722 return equal;
10723 error:
10724 isl_map_free(map1);
10725 isl_map_free(map2);
10726 return isl_bool_error;
10729 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10730 __isl_keep isl_set *set2)
10732 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10735 /* Return the basic maps in "map" as a list.
10737 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10738 __isl_keep isl_map *map)
10740 int i;
10741 isl_ctx *ctx;
10742 isl_basic_map_list *list;
10744 if (!map)
10745 return NULL;
10746 ctx = isl_map_get_ctx(map);
10747 list = isl_basic_map_list_alloc(ctx, map->n);
10749 for (i = 0; i < map->n; ++i) {
10750 isl_basic_map *bmap;
10752 bmap = isl_basic_map_copy(map->p[i]);
10753 list = isl_basic_map_list_add(list, bmap);
10756 return list;
10759 /* Return the intersection of the elements in the non-empty list "list".
10760 * All elements are assumed to live in the same space.
10762 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10763 __isl_take isl_basic_map_list *list)
10765 int i;
10766 isl_size n;
10767 isl_basic_map *bmap;
10769 n = isl_basic_map_list_n_basic_map(list);
10770 if (n < 0)
10771 goto error;
10772 if (n < 1)
10773 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10774 "expecting non-empty list", goto error);
10776 bmap = isl_basic_map_list_get_basic_map(list, 0);
10777 for (i = 1; i < n; ++i) {
10778 isl_basic_map *bmap_i;
10780 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10781 bmap = isl_basic_map_intersect(bmap, bmap_i);
10784 isl_basic_map_list_free(list);
10785 return bmap;
10786 error:
10787 isl_basic_map_list_free(list);
10788 return NULL;
10791 /* Return the intersection of the elements in the non-empty list "list".
10792 * All elements are assumed to live in the same space.
10794 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10795 __isl_take isl_basic_set_list *list)
10797 return isl_basic_map_list_intersect(list);
10800 /* Return the union of the elements of "list".
10801 * The list is required to have at least one element.
10803 __isl_give isl_set *isl_basic_set_list_union(
10804 __isl_take isl_basic_set_list *list)
10806 int i;
10807 isl_size n;
10808 isl_space *space;
10809 isl_basic_set *bset;
10810 isl_set *set;
10812 n = isl_basic_set_list_n_basic_set(list);
10813 if (n < 0)
10814 goto error;
10815 if (n < 1)
10816 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10817 "expecting non-empty list", goto error);
10819 bset = isl_basic_set_list_get_basic_set(list, 0);
10820 space = isl_basic_set_get_space(bset);
10821 isl_basic_set_free(bset);
10823 set = isl_set_alloc_space(space, n, 0);
10824 for (i = 0; i < n; ++i) {
10825 bset = isl_basic_set_list_get_basic_set(list, i);
10826 set = isl_set_add_basic_set(set, bset);
10829 isl_basic_set_list_free(list);
10830 return set;
10831 error:
10832 isl_basic_set_list_free(list);
10833 return NULL;
10836 /* Return the union of the elements in the non-empty list "list".
10837 * All elements are assumed to live in the same space.
10839 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10841 int i;
10842 isl_size n;
10843 isl_set *set;
10845 n = isl_set_list_n_set(list);
10846 if (n < 0)
10847 goto error;
10848 if (n < 1)
10849 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10850 "expecting non-empty list", goto error);
10852 set = isl_set_list_get_set(list, 0);
10853 for (i = 1; i < n; ++i) {
10854 isl_set *set_i;
10856 set_i = isl_set_list_get_set(list, i);
10857 set = isl_set_union(set, set_i);
10860 isl_set_list_free(list);
10861 return set;
10862 error:
10863 isl_set_list_free(list);
10864 return NULL;
10867 __isl_give isl_basic_map *isl_basic_map_product(
10868 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10870 isl_space *space_result = NULL;
10871 struct isl_basic_map *bmap;
10872 unsigned in1, in2, out1, out2, nparam, total, pos;
10873 struct isl_dim_map *dim_map1, *dim_map2;
10875 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10876 goto error;
10877 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10878 isl_space_copy(bmap2->dim));
10880 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10881 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10882 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10883 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10884 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10886 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10887 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10888 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10889 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10890 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10895 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10896 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10898 bmap = isl_basic_map_alloc_space(space_result,
10899 bmap1->n_div + bmap2->n_div,
10900 bmap1->n_eq + bmap2->n_eq,
10901 bmap1->n_ineq + bmap2->n_ineq);
10902 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10903 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10904 bmap = isl_basic_map_simplify(bmap);
10905 return isl_basic_map_finalize(bmap);
10906 error:
10907 isl_basic_map_free(bmap1);
10908 isl_basic_map_free(bmap2);
10909 return NULL;
10912 __isl_give isl_basic_map *isl_basic_map_flat_product(
10913 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10915 isl_basic_map *prod;
10917 prod = isl_basic_map_product(bmap1, bmap2);
10918 prod = isl_basic_map_flatten(prod);
10919 return prod;
10922 __isl_give isl_basic_set *isl_basic_set_flat_product(
10923 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10925 return isl_basic_map_flat_range_product(bset1, bset2);
10928 __isl_give isl_basic_map *isl_basic_map_domain_product(
10929 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10931 isl_space *space1, *space2;
10932 isl_space *space_result = NULL;
10933 isl_basic_map *bmap;
10934 isl_size in1, in2, out, nparam;
10935 unsigned total, pos;
10936 struct isl_dim_map *dim_map1, *dim_map2;
10938 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10939 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10940 out = isl_basic_map_dim(bmap1, isl_dim_out);
10941 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10942 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10943 goto error;
10945 space1 = isl_basic_map_get_space(bmap1);
10946 space2 = isl_basic_map_get_space(bmap2);
10947 space_result = isl_space_domain_product(space1, space2);
10949 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10950 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10951 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10952 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10953 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10954 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10955 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10956 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10957 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10958 isl_dim_map_div(dim_map1, bmap1, pos += out);
10959 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10961 bmap = isl_basic_map_alloc_space(space_result,
10962 bmap1->n_div + bmap2->n_div,
10963 bmap1->n_eq + bmap2->n_eq,
10964 bmap1->n_ineq + bmap2->n_ineq);
10965 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10966 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10967 bmap = isl_basic_map_simplify(bmap);
10968 return isl_basic_map_finalize(bmap);
10969 error:
10970 isl_basic_map_free(bmap1);
10971 isl_basic_map_free(bmap2);
10972 return NULL;
10975 __isl_give isl_basic_map *isl_basic_map_range_product(
10976 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10978 isl_bool rational;
10979 isl_space *space_result = NULL;
10980 isl_basic_map *bmap;
10981 isl_size in, out1, out2, nparam;
10982 unsigned total, pos;
10983 struct isl_dim_map *dim_map1, *dim_map2;
10985 rational = isl_basic_map_is_rational(bmap1);
10986 if (rational >= 0 && rational)
10987 rational = isl_basic_map_is_rational(bmap2);
10988 in = isl_basic_map_dim(bmap1, isl_dim_in);
10989 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10990 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10991 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10992 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10993 goto error;
10995 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10996 goto error;
10998 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10999 isl_space_copy(bmap2->dim));
11001 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
11002 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11003 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11004 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11005 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11006 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11007 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11008 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11009 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11010 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11011 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11013 bmap = isl_basic_map_alloc_space(space_result,
11014 bmap1->n_div + bmap2->n_div,
11015 bmap1->n_eq + bmap2->n_eq,
11016 bmap1->n_ineq + bmap2->n_ineq);
11017 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11018 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11019 if (rational)
11020 bmap = isl_basic_map_set_rational(bmap);
11021 bmap = isl_basic_map_simplify(bmap);
11022 return isl_basic_map_finalize(bmap);
11023 error:
11024 isl_basic_map_free(bmap1);
11025 isl_basic_map_free(bmap2);
11026 return NULL;
11029 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11030 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11032 isl_basic_map *prod;
11034 prod = isl_basic_map_range_product(bmap1, bmap2);
11035 prod = isl_basic_map_flatten_range(prod);
11036 return prod;
11039 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11040 * and collect the results.
11041 * The result live in the space obtained by calling "space_product"
11042 * on the spaces of "map1" and "map2".
11043 * If "remove_duplicates" is set then the result may contain duplicates
11044 * (even if the inputs do not) and so we try and remove the obvious
11045 * duplicates.
11047 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11048 __isl_take isl_map *map2,
11049 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11050 __isl_take isl_space *right),
11051 __isl_give isl_basic_map *(*basic_map_product)(
11052 __isl_take isl_basic_map *left,
11053 __isl_take isl_basic_map *right),
11054 int remove_duplicates)
11056 unsigned flags = 0;
11057 struct isl_map *result;
11058 int i, j;
11059 isl_bool m;
11061 m = isl_map_has_equal_params(map1, map2);
11062 if (m < 0)
11063 goto error;
11064 if (!m)
11065 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11066 "parameters don't match", goto error);
11068 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11069 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11070 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11072 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11073 isl_space_copy(map2->dim)),
11074 map1->n * map2->n, flags);
11075 if (!result)
11076 goto error;
11077 for (i = 0; i < map1->n; ++i)
11078 for (j = 0; j < map2->n; ++j) {
11079 struct isl_basic_map *part;
11080 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11081 isl_basic_map_copy(map2->p[j]));
11082 if (isl_basic_map_is_empty(part))
11083 isl_basic_map_free(part);
11084 else
11085 result = isl_map_add_basic_map(result, part);
11086 if (!result)
11087 goto error;
11089 if (remove_duplicates)
11090 result = isl_map_remove_obvious_duplicates(result);
11091 isl_map_free(map1);
11092 isl_map_free(map2);
11093 return result;
11094 error:
11095 isl_map_free(map1);
11096 isl_map_free(map2);
11097 return NULL;
11100 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11102 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11103 __isl_take isl_map *map2)
11105 isl_map_align_params_bin(&map1, &map2);
11106 return map_product(map1, map2, &isl_space_product,
11107 &isl_basic_map_product, 0);
11110 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11112 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11113 __isl_take isl_map *map2)
11115 isl_map *prod;
11117 prod = isl_map_product(map1, map2);
11118 prod = isl_map_flatten(prod);
11119 return prod;
11122 /* Given two set A and B, construct its Cartesian product A x B.
11124 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11125 __isl_take isl_set *set2)
11127 return isl_map_range_product(set1, set2);
11130 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11131 __isl_take isl_set *set2)
11133 return isl_map_flat_range_product(set1, set2);
11136 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11138 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11139 __isl_take isl_map *map2)
11141 isl_map_align_params_bin(&map1, &map2);
11142 return map_product(map1, map2, &isl_space_domain_product,
11143 &isl_basic_map_domain_product, 1);
11146 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11148 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11149 __isl_take isl_map *map2)
11151 isl_map_align_params_bin(&map1, &map2);
11152 return map_product(map1, map2, &isl_space_range_product,
11153 &isl_basic_map_range_product, 1);
11156 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11158 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11160 isl_space *space;
11161 isl_size total1, keep1, total2, keep2;
11163 total1 = isl_map_dim(map, isl_dim_in);
11164 total2 = isl_map_dim(map, isl_dim_out);
11165 if (total1 < 0 || total2 < 0)
11166 return isl_map_free(map);
11167 if (!isl_space_domain_is_wrapping(map->dim) ||
11168 !isl_space_range_is_wrapping(map->dim))
11169 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11170 "not a product", return isl_map_free(map));
11172 space = isl_map_get_space(map);
11173 space = isl_space_factor_domain(space);
11174 keep1 = isl_space_dim(space, isl_dim_in);
11175 keep2 = isl_space_dim(space, isl_dim_out);
11176 if (keep1 < 0 || keep2 < 0)
11177 map = isl_map_free(map);
11178 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11179 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11180 map = isl_map_reset_space(map, space);
11182 return map;
11185 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11187 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11189 isl_space *space;
11190 isl_size total1, keep1, total2, keep2;
11192 total1 = isl_map_dim(map, isl_dim_in);
11193 total2 = isl_map_dim(map, isl_dim_out);
11194 if (total1 < 0 || total2 < 0)
11195 return isl_map_free(map);
11196 if (!isl_space_domain_is_wrapping(map->dim) ||
11197 !isl_space_range_is_wrapping(map->dim))
11198 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11199 "not a product", return isl_map_free(map));
11201 space = isl_map_get_space(map);
11202 space = isl_space_factor_range(space);
11203 keep1 = isl_space_dim(space, isl_dim_in);
11204 keep2 = isl_space_dim(space, isl_dim_out);
11205 if (keep1 < 0 || keep2 < 0)
11206 map = isl_map_free(map);
11207 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11208 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11209 map = isl_map_reset_space(map, space);
11211 return map;
11214 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11216 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11218 isl_space *space;
11219 isl_size total, keep;
11221 total = isl_map_dim(map, isl_dim_in);
11222 if (total < 0)
11223 return isl_map_free(map);
11224 if (!isl_space_domain_is_wrapping(map->dim))
11225 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11226 "domain is not a product", return isl_map_free(map));
11228 space = isl_map_get_space(map);
11229 space = isl_space_domain_factor_domain(space);
11230 keep = isl_space_dim(space, isl_dim_in);
11231 if (keep < 0)
11232 map = isl_map_free(map);
11233 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11234 map = isl_map_reset_space(map, space);
11236 return map;
11239 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11241 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11243 isl_space *space;
11244 isl_size total, keep;
11246 total = isl_map_dim(map, isl_dim_in);
11247 if (total < 0)
11248 return isl_map_free(map);
11249 if (!isl_space_domain_is_wrapping(map->dim))
11250 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11251 "domain is not a product", return isl_map_free(map));
11253 space = isl_map_get_space(map);
11254 space = isl_space_domain_factor_range(space);
11255 keep = isl_space_dim(space, isl_dim_in);
11256 if (keep < 0)
11257 map = isl_map_free(map);
11258 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11259 map = isl_map_reset_space(map, space);
11261 return map;
11264 /* Given a map A -> [B -> C], extract the map A -> B.
11266 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11268 isl_space *space;
11269 isl_size total, keep;
11271 total = isl_map_dim(map, isl_dim_out);
11272 if (total < 0)
11273 return isl_map_free(map);
11274 if (!isl_space_range_is_wrapping(map->dim))
11275 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11276 "range is not a product", return isl_map_free(map));
11278 space = isl_map_get_space(map);
11279 space = isl_space_range_factor_domain(space);
11280 keep = isl_space_dim(space, isl_dim_out);
11281 if (keep < 0)
11282 map = isl_map_free(map);
11283 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11284 map = isl_map_reset_space(map, space);
11286 return map;
11289 /* Given a map A -> [B -> C], extract the map A -> C.
11291 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11293 isl_space *space;
11294 isl_size total, keep;
11296 total = isl_map_dim(map, isl_dim_out);
11297 if (total < 0)
11298 return isl_map_free(map);
11299 if (!isl_space_range_is_wrapping(map->dim))
11300 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11301 "range is not a product", return isl_map_free(map));
11303 space = isl_map_get_space(map);
11304 space = isl_space_range_factor_range(space);
11305 keep = isl_space_dim(space, isl_dim_out);
11306 if (keep < 0)
11307 map = isl_map_free(map);
11308 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11309 map = isl_map_reset_space(map, space);
11311 return map;
11314 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11316 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11317 __isl_take isl_map *map2)
11319 isl_map *prod;
11321 prod = isl_map_domain_product(map1, map2);
11322 prod = isl_map_flatten_domain(prod);
11323 return prod;
11326 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11328 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11329 __isl_take isl_map *map2)
11331 isl_map *prod;
11333 prod = isl_map_range_product(map1, map2);
11334 prod = isl_map_flatten_range(prod);
11335 return prod;
11338 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11340 int i;
11341 uint32_t hash = isl_hash_init();
11342 isl_size total;
11344 if (!bmap)
11345 return 0;
11346 bmap = isl_basic_map_copy(bmap);
11347 bmap = isl_basic_map_normalize(bmap);
11348 total = isl_basic_map_dim(bmap, isl_dim_all);
11349 if (total < 0)
11350 return 0;
11351 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11352 for (i = 0; i < bmap->n_eq; ++i) {
11353 uint32_t c_hash;
11354 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11355 isl_hash_hash(hash, c_hash);
11357 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11358 for (i = 0; i < bmap->n_ineq; ++i) {
11359 uint32_t c_hash;
11360 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11361 isl_hash_hash(hash, c_hash);
11363 isl_hash_byte(hash, bmap->n_div & 0xFF);
11364 for (i = 0; i < bmap->n_div; ++i) {
11365 uint32_t c_hash;
11366 if (isl_int_is_zero(bmap->div[i][0]))
11367 continue;
11368 isl_hash_byte(hash, i & 0xFF);
11369 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11370 isl_hash_hash(hash, c_hash);
11372 isl_basic_map_free(bmap);
11373 return hash;
11376 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11378 return isl_basic_map_get_hash(bset_to_bmap(bset));
11381 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11383 int i;
11384 uint32_t hash;
11386 if (!map)
11387 return 0;
11388 map = isl_map_copy(map);
11389 map = isl_map_normalize(map);
11390 if (!map)
11391 return 0;
11393 hash = isl_hash_init();
11394 for (i = 0; i < map->n; ++i) {
11395 uint32_t bmap_hash;
11396 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11397 isl_hash_hash(hash, bmap_hash);
11400 isl_map_free(map);
11402 return hash;
11405 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11407 return isl_map_get_hash(set_to_map(set));
11410 /* Return the number of basic maps in the (current) representation of "map".
11412 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11414 return map ? map->n : isl_size_error;
11417 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11419 return set ? set->n : isl_size_error;
11422 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11423 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11425 int i;
11427 if (!map)
11428 return isl_stat_error;
11430 for (i = 0; i < map->n; ++i)
11431 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11432 return isl_stat_error;
11434 return isl_stat_ok;
11437 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11438 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11440 int i;
11442 if (!set)
11443 return isl_stat_error;
11445 for (i = 0; i < set->n; ++i)
11446 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11447 return isl_stat_error;
11449 return isl_stat_ok;
11452 /* Does "test" succeed on every basic set in "set"?
11454 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11455 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11456 void *user)
11458 int i;
11460 if (!set)
11461 return isl_bool_error;
11463 for (i = 0; i < set->n; ++i) {
11464 isl_bool r;
11466 r = test(set->p[i], user);
11467 if (r < 0 || !r)
11468 return r;
11471 return isl_bool_true;
11474 /* Return a list of basic sets, the union of which is equal to "set".
11476 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11477 __isl_keep isl_set *set)
11479 int i;
11480 isl_basic_set_list *list;
11482 if (!set)
11483 return NULL;
11485 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11486 for (i = 0; i < set->n; ++i) {
11487 isl_basic_set *bset;
11489 bset = isl_basic_set_copy(set->p[i]);
11490 list = isl_basic_set_list_add(list, bset);
11493 return list;
11496 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11498 isl_space *space;
11500 if (!bset)
11501 return NULL;
11503 bset = isl_basic_set_cow(bset);
11504 if (!bset)
11505 return NULL;
11507 space = isl_basic_set_get_space(bset);
11508 space = isl_space_lift(space, bset->n_div);
11509 if (!space)
11510 goto error;
11511 isl_space_free(bset->dim);
11512 bset->dim = space;
11513 bset->extra -= bset->n_div;
11514 bset->n_div = 0;
11516 bset = isl_basic_set_finalize(bset);
11518 return bset;
11519 error:
11520 isl_basic_set_free(bset);
11521 return NULL;
11524 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11526 int i;
11527 isl_space *space;
11528 unsigned n_div;
11530 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11532 if (!set)
11533 return NULL;
11535 set = isl_set_cow(set);
11536 if (!set)
11537 return NULL;
11539 n_div = set->p[0]->n_div;
11540 space = isl_set_get_space(set);
11541 space = isl_space_lift(space, n_div);
11542 if (!space)
11543 goto error;
11544 isl_space_free(set->dim);
11545 set->dim = space;
11547 for (i = 0; i < set->n; ++i) {
11548 set->p[i] = isl_basic_set_lift(set->p[i]);
11549 if (!set->p[i])
11550 goto error;
11553 return set;
11554 error:
11555 isl_set_free(set);
11556 return NULL;
11559 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11561 isl_size dim;
11562 int size = 0;
11564 dim = isl_basic_set_dim(bset, isl_dim_all);
11565 if (dim < 0)
11566 return -1;
11567 size += bset->n_eq * (1 + dim);
11568 size += bset->n_ineq * (1 + dim);
11569 size += bset->n_div * (2 + dim);
11571 return size;
11574 int isl_set_size(__isl_keep isl_set *set)
11576 int i;
11577 int size = 0;
11579 if (!set)
11580 return -1;
11582 for (i = 0; i < set->n; ++i)
11583 size += isl_basic_set_size(set->p[i]);
11585 return size;
11588 /* Check if there is any lower bound (if lower == 0) and/or upper
11589 * bound (if upper == 0) on the specified dim.
11591 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11592 enum isl_dim_type type, unsigned pos, int lower, int upper)
11594 int i;
11596 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11597 return isl_bool_error;
11599 pos += isl_basic_map_offset(bmap, type);
11601 for (i = 0; i < bmap->n_div; ++i) {
11602 if (isl_int_is_zero(bmap->div[i][0]))
11603 continue;
11604 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11605 return isl_bool_true;
11608 for (i = 0; i < bmap->n_eq; ++i)
11609 if (!isl_int_is_zero(bmap->eq[i][pos]))
11610 return isl_bool_true;
11612 for (i = 0; i < bmap->n_ineq; ++i) {
11613 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11614 if (sgn > 0)
11615 lower = 1;
11616 if (sgn < 0)
11617 upper = 1;
11620 return lower && upper;
11623 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11624 enum isl_dim_type type, unsigned pos)
11626 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11629 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11630 enum isl_dim_type type, unsigned pos)
11632 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11635 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11636 enum isl_dim_type type, unsigned pos)
11638 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11641 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11642 enum isl_dim_type type, unsigned pos)
11644 int i;
11646 if (!map)
11647 return isl_bool_error;
11649 for (i = 0; i < map->n; ++i) {
11650 isl_bool bounded;
11651 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11652 if (bounded < 0 || !bounded)
11653 return bounded;
11656 return isl_bool_true;
11659 /* Return true if the specified dim is involved in both an upper bound
11660 * and a lower bound.
11662 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11663 enum isl_dim_type type, unsigned pos)
11665 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11668 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11670 static isl_bool has_any_bound(__isl_keep isl_map *map,
11671 enum isl_dim_type type, unsigned pos,
11672 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11673 enum isl_dim_type type, unsigned pos))
11675 int i;
11677 if (!map)
11678 return isl_bool_error;
11680 for (i = 0; i < map->n; ++i) {
11681 isl_bool bounded;
11682 bounded = fn(map->p[i], type, pos);
11683 if (bounded < 0 || bounded)
11684 return bounded;
11687 return isl_bool_false;
11690 /* Return 1 if the specified dim is involved in any lower bound.
11692 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11693 enum isl_dim_type type, unsigned pos)
11695 return has_any_bound(set, type, pos,
11696 &isl_basic_map_dim_has_lower_bound);
11699 /* Return 1 if the specified dim is involved in any upper bound.
11701 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11702 enum isl_dim_type type, unsigned pos)
11704 return has_any_bound(set, type, pos,
11705 &isl_basic_map_dim_has_upper_bound);
11708 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11710 static isl_bool has_bound(__isl_keep isl_map *map,
11711 enum isl_dim_type type, unsigned pos,
11712 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11713 enum isl_dim_type type, unsigned pos))
11715 int i;
11717 if (!map)
11718 return isl_bool_error;
11720 for (i = 0; i < map->n; ++i) {
11721 isl_bool bounded;
11722 bounded = fn(map->p[i], type, pos);
11723 if (bounded < 0 || !bounded)
11724 return bounded;
11727 return isl_bool_true;
11730 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11732 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11733 enum isl_dim_type type, unsigned pos)
11735 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11738 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11740 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11741 enum isl_dim_type type, unsigned pos)
11743 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11746 /* For each of the "n" variables starting at "first", determine
11747 * the sign of the variable and put the results in the first "n"
11748 * elements of the array "signs".
11749 * Sign
11750 * 1 means that the variable is non-negative
11751 * -1 means that the variable is non-positive
11752 * 0 means the variable attains both positive and negative values.
11754 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11755 unsigned first, unsigned n, int *signs)
11757 isl_vec *bound = NULL;
11758 struct isl_tab *tab = NULL;
11759 struct isl_tab_undo *snap;
11760 int i;
11761 isl_size total;
11763 total = isl_basic_set_dim(bset, isl_dim_all);
11764 if (total < 0 || !signs)
11765 return isl_stat_error;
11767 bound = isl_vec_alloc(bset->ctx, 1 + total);
11768 tab = isl_tab_from_basic_set(bset, 0);
11769 if (!bound || !tab)
11770 goto error;
11772 isl_seq_clr(bound->el, bound->size);
11773 isl_int_set_si(bound->el[0], -1);
11775 snap = isl_tab_snap(tab);
11776 for (i = 0; i < n; ++i) {
11777 int empty;
11779 isl_int_set_si(bound->el[1 + first + i], -1);
11780 if (isl_tab_add_ineq(tab, bound->el) < 0)
11781 goto error;
11782 empty = tab->empty;
11783 isl_int_set_si(bound->el[1 + first + i], 0);
11784 if (isl_tab_rollback(tab, snap) < 0)
11785 goto error;
11787 if (empty) {
11788 signs[i] = 1;
11789 continue;
11792 isl_int_set_si(bound->el[1 + first + i], 1);
11793 if (isl_tab_add_ineq(tab, bound->el) < 0)
11794 goto error;
11795 empty = tab->empty;
11796 isl_int_set_si(bound->el[1 + first + i], 0);
11797 if (isl_tab_rollback(tab, snap) < 0)
11798 goto error;
11800 signs[i] = empty ? -1 : 0;
11803 isl_tab_free(tab);
11804 isl_vec_free(bound);
11805 return isl_stat_ok;
11806 error:
11807 isl_tab_free(tab);
11808 isl_vec_free(bound);
11809 return isl_stat_error;
11812 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11813 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11815 if (!bset || !signs)
11816 return isl_stat_error;
11817 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11818 return isl_stat_error;
11820 first += pos(bset->dim, type) - 1;
11821 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11824 /* Is it possible for the integer division "div" to depend (possibly
11825 * indirectly) on any output dimensions?
11827 * If the div is undefined, then we conservatively assume that it
11828 * may depend on them.
11829 * Otherwise, we check if it actually depends on them or on any integer
11830 * divisions that may depend on them.
11832 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11834 int i;
11835 isl_size n_out, n_div;
11836 unsigned o_out, o_div;
11838 if (isl_int_is_zero(bmap->div[div][0]))
11839 return isl_bool_true;
11841 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11842 if (n_out < 0)
11843 return isl_bool_error;
11844 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11846 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11847 return isl_bool_true;
11849 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11850 if (n_div < 0)
11851 return isl_bool_error;
11852 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11854 for (i = 0; i < n_div; ++i) {
11855 isl_bool may_involve;
11857 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11858 continue;
11859 may_involve = div_may_involve_output(bmap, i);
11860 if (may_involve < 0 || may_involve)
11861 return may_involve;
11864 return isl_bool_false;
11867 /* Return the first integer division of "bmap" in the range
11868 * [first, first + n[ that may depend on any output dimensions and
11869 * that has a non-zero coefficient in "c" (where the first coefficient
11870 * in "c" corresponds to integer division "first").
11872 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11873 isl_int *c, int first, int n)
11875 int k;
11877 if (!bmap)
11878 return -1;
11880 for (k = first; k < first + n; ++k) {
11881 isl_bool may_involve;
11883 if (isl_int_is_zero(c[k]))
11884 continue;
11885 may_involve = div_may_involve_output(bmap, k);
11886 if (may_involve < 0)
11887 return -1;
11888 if (may_involve)
11889 return k;
11892 return first + n;
11895 /* Look for a pair of inequality constraints in "bmap" of the form
11897 * -l + i >= 0 or i >= l
11898 * and
11899 * n + l - i >= 0 or i <= l + n
11901 * with n < "m" and i the output dimension at position "pos".
11902 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11903 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11904 * and earlier output dimensions, as well as integer divisions that do
11905 * not involve any of the output dimensions.
11907 * Return the index of the first inequality constraint or bmap->n_ineq
11908 * if no such pair can be found.
11910 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11911 int pos, isl_int m)
11913 int i, j;
11914 isl_ctx *ctx;
11915 isl_size total;
11916 isl_size n_div, n_out;
11917 unsigned o_div, o_out;
11918 int less;
11920 total = isl_basic_map_dim(bmap, isl_dim_all);
11921 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11922 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11923 if (total < 0 || n_out < 0 || n_div < 0)
11924 return -1;
11926 ctx = isl_basic_map_get_ctx(bmap);
11927 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11928 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11929 for (i = 0; i < bmap->n_ineq; ++i) {
11930 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11931 continue;
11932 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11933 n_out - (pos + 1)) != -1)
11934 continue;
11935 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11936 0, n_div) < n_div)
11937 continue;
11938 for (j = i + 1; j < bmap->n_ineq; ++j) {
11939 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11940 ctx->one))
11941 continue;
11942 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11943 bmap->ineq[j] + 1, total))
11944 continue;
11945 break;
11947 if (j >= bmap->n_ineq)
11948 continue;
11949 isl_int_add(bmap->ineq[i][0],
11950 bmap->ineq[i][0], bmap->ineq[j][0]);
11951 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11952 isl_int_sub(bmap->ineq[i][0],
11953 bmap->ineq[i][0], bmap->ineq[j][0]);
11954 if (!less)
11955 continue;
11956 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11957 return i;
11958 else
11959 return j;
11962 return bmap->n_ineq;
11965 /* Return the index of the equality of "bmap" that defines
11966 * the output dimension "pos" in terms of earlier dimensions.
11967 * The equality may also involve integer divisions, as long
11968 * as those integer divisions are defined in terms of
11969 * parameters or input dimensions.
11970 * In this case, *div is set to the number of integer divisions and
11971 * *ineq is set to the number of inequality constraints (provided
11972 * div and ineq are not NULL).
11974 * The equality may also involve a single integer division involving
11975 * the output dimensions (typically only output dimension "pos") as
11976 * long as the coefficient of output dimension "pos" is 1 or -1 and
11977 * there is a pair of constraints i >= l and i <= l + n, with i referring
11978 * to output dimension "pos", l an expression involving only earlier
11979 * dimensions and n smaller than the coefficient of the integer division
11980 * in the equality. In this case, the output dimension can be defined
11981 * in terms of a modulo expression that does not involve the integer division.
11982 * *div is then set to this single integer division and
11983 * *ineq is set to the index of constraint i >= l.
11985 * Return bmap->n_eq if there is no such equality.
11986 * Return -1 on error.
11988 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11989 int pos, int *div, int *ineq)
11991 int j, k, l;
11992 isl_size n_div, n_out;
11993 unsigned o_div, o_out;
11995 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11996 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11997 if (n_out < 0 || n_div < 0)
11998 return -1;
12000 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12001 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12003 if (ineq)
12004 *ineq = bmap->n_ineq;
12005 if (div)
12006 *div = n_div;
12007 for (j = 0; j < bmap->n_eq; ++j) {
12008 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12009 continue;
12010 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
12011 n_out - (pos + 1)) != -1)
12012 continue;
12013 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12014 0, n_div);
12015 if (k >= n_div)
12016 return j;
12017 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12018 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12019 continue;
12020 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12021 k + 1, n_div - (k+1)) < n_div)
12022 continue;
12023 l = find_modulo_constraint_pair(bmap, pos,
12024 bmap->eq[j][o_div + k]);
12025 if (l < 0)
12026 return -1;
12027 if (l >= bmap->n_ineq)
12028 continue;
12029 if (div)
12030 *div = k;
12031 if (ineq)
12032 *ineq = l;
12033 return j;
12036 return bmap->n_eq;
12039 /* Check if the given basic map is obviously single-valued.
12040 * In particular, for each output dimension, check that there is
12041 * an equality that defines the output dimension in terms of
12042 * earlier dimensions.
12044 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12046 int i;
12047 isl_size n_out;
12049 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12050 if (n_out < 0)
12051 return isl_bool_error;
12053 for (i = 0; i < n_out; ++i) {
12054 int eq;
12056 eq = isl_basic_map_output_defining_equality(bmap, i,
12057 NULL, NULL);
12058 if (eq < 0)
12059 return isl_bool_error;
12060 if (eq >= bmap->n_eq)
12061 return isl_bool_false;
12064 return isl_bool_true;
12067 /* Check if the given basic map is single-valued.
12068 * We simply compute
12070 * M \circ M^-1
12072 * and check if the result is a subset of the identity mapping.
12074 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12076 isl_space *space;
12077 isl_basic_map *test;
12078 isl_basic_map *id;
12079 isl_bool sv;
12081 sv = isl_basic_map_plain_is_single_valued(bmap);
12082 if (sv < 0 || sv)
12083 return sv;
12085 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12086 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12088 space = isl_basic_map_get_space(bmap);
12089 space = isl_space_map_from_set(isl_space_range(space));
12090 id = isl_basic_map_identity(space);
12092 sv = isl_basic_map_is_subset(test, id);
12094 isl_basic_map_free(test);
12095 isl_basic_map_free(id);
12097 return sv;
12100 /* Check if the given map is obviously single-valued.
12102 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12104 if (!map)
12105 return isl_bool_error;
12106 if (map->n == 0)
12107 return isl_bool_true;
12108 if (map->n >= 2)
12109 return isl_bool_false;
12111 return isl_basic_map_plain_is_single_valued(map->p[0]);
12114 /* Check if the given map is single-valued.
12115 * We simply compute
12117 * M \circ M^-1
12119 * and check if the result is a subset of the identity mapping.
12121 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12123 isl_space *space;
12124 isl_map *test;
12125 isl_map *id;
12126 isl_bool sv;
12128 sv = isl_map_plain_is_single_valued(map);
12129 if (sv < 0 || sv)
12130 return sv;
12132 test = isl_map_reverse(isl_map_copy(map));
12133 test = isl_map_apply_range(test, isl_map_copy(map));
12135 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12136 id = isl_map_identity(space);
12138 sv = isl_map_is_subset(test, id);
12140 isl_map_free(test);
12141 isl_map_free(id);
12143 return sv;
12146 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12148 isl_bool in;
12150 map = isl_map_copy(map);
12151 map = isl_map_reverse(map);
12152 in = isl_map_is_single_valued(map);
12153 isl_map_free(map);
12155 return in;
12158 /* Check if the given map is obviously injective.
12160 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12162 isl_bool in;
12164 map = isl_map_copy(map);
12165 map = isl_map_reverse(map);
12166 in = isl_map_plain_is_single_valued(map);
12167 isl_map_free(map);
12169 return in;
12172 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12174 isl_bool sv;
12176 sv = isl_map_is_single_valued(map);
12177 if (sv < 0 || !sv)
12178 return sv;
12180 return isl_map_is_injective(map);
12183 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12185 return isl_map_is_single_valued(set_to_map(set));
12188 /* Does "map" only map elements to themselves?
12190 * If the domain and range spaces are different, then "map"
12191 * is considered not to be an identity relation, even if it is empty.
12192 * Otherwise, construct the maximal identity relation and
12193 * check whether "map" is a subset of this relation.
12195 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12197 isl_map *id;
12198 isl_bool equal, is_identity;
12200 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12201 if (equal < 0 || !equal)
12202 return equal;
12204 id = isl_map_identity(isl_map_get_space(map));
12205 is_identity = isl_map_is_subset(map, id);
12206 isl_map_free(id);
12208 return is_identity;
12211 int isl_map_is_translation(__isl_keep isl_map *map)
12213 int ok;
12214 isl_set *delta;
12216 delta = isl_map_deltas(isl_map_copy(map));
12217 ok = isl_set_is_singleton(delta);
12218 isl_set_free(delta);
12220 return ok;
12223 static int unique(isl_int *p, unsigned pos, unsigned len)
12225 if (isl_seq_first_non_zero(p, pos) != -1)
12226 return 0;
12227 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12228 return 0;
12229 return 1;
12232 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12234 int i, j;
12235 isl_size nvar, ovar, n_div;
12237 n_div = isl_basic_set_dim(bset, isl_dim_div);
12238 if (n_div < 0)
12239 return isl_bool_error;
12240 if (n_div != 0)
12241 return isl_bool_false;
12243 nvar = isl_basic_set_dim(bset, isl_dim_set);
12244 ovar = isl_space_offset(bset->dim, isl_dim_set);
12245 if (nvar < 0 || ovar < 0)
12246 return isl_bool_error;
12247 for (j = 0; j < nvar; ++j) {
12248 int lower = 0, upper = 0;
12249 for (i = 0; i < bset->n_eq; ++i) {
12250 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12251 continue;
12252 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12253 return isl_bool_false;
12254 break;
12256 if (i < bset->n_eq)
12257 continue;
12258 for (i = 0; i < bset->n_ineq; ++i) {
12259 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12260 continue;
12261 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12262 return isl_bool_false;
12263 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12264 lower = 1;
12265 else
12266 upper = 1;
12268 if (!lower || !upper)
12269 return isl_bool_false;
12272 return isl_bool_true;
12275 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12277 if (!set)
12278 return isl_bool_error;
12279 if (set->n != 1)
12280 return isl_bool_false;
12282 return isl_basic_set_is_box(set->p[0]);
12285 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12287 if (!bset)
12288 return isl_bool_error;
12290 return isl_space_is_wrapping(bset->dim);
12293 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12295 if (!set)
12296 return isl_bool_error;
12298 return isl_space_is_wrapping(set->dim);
12301 /* Modify the space of "map" through a call to "change".
12302 * If "can_change" is set (not NULL), then first call it to check
12303 * if the modification is allowed, printing the error message "cannot_change"
12304 * if it is not.
12306 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12307 isl_bool (*can_change)(__isl_keep isl_map *map),
12308 const char *cannot_change,
12309 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12311 isl_bool ok;
12312 isl_space *space;
12314 if (!map)
12315 return NULL;
12317 ok = can_change ? can_change(map) : isl_bool_true;
12318 if (ok < 0)
12319 return isl_map_free(map);
12320 if (!ok)
12321 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12322 return isl_map_free(map));
12324 space = change(isl_map_get_space(map));
12325 map = isl_map_reset_space(map, space);
12327 return map;
12330 /* Is the domain of "map" a wrapped relation?
12332 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12334 if (!map)
12335 return isl_bool_error;
12337 return isl_space_domain_is_wrapping(map->dim);
12340 /* Does "map" have a wrapped relation in both domain and range?
12342 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12344 return isl_space_is_product(isl_map_peek_space(map));
12347 /* Is the range of "map" a wrapped relation?
12349 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12351 if (!map)
12352 return isl_bool_error;
12354 return isl_space_range_is_wrapping(map->dim);
12357 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12359 isl_space *space;
12361 space = isl_basic_map_take_space(bmap);
12362 space = isl_space_wrap(space);
12363 bmap = isl_basic_map_restore_space(bmap, space);
12365 bmap = isl_basic_map_finalize(bmap);
12367 return bset_from_bmap(bmap);
12370 /* Given a map A -> B, return the set (A -> B).
12372 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12374 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12377 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12379 bset = isl_basic_set_cow(bset);
12380 if (!bset)
12381 return NULL;
12383 bset->dim = isl_space_unwrap(bset->dim);
12384 if (!bset->dim)
12385 goto error;
12387 bset = isl_basic_set_finalize(bset);
12389 return bset_to_bmap(bset);
12390 error:
12391 isl_basic_set_free(bset);
12392 return NULL;
12395 /* Given a set (A -> B), return the map A -> B.
12396 * Error out if "set" is not of the form (A -> B).
12398 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12400 return isl_map_change_space(set, &isl_set_is_wrapping,
12401 "not a wrapping set", &isl_space_unwrap);
12404 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12405 enum isl_dim_type type)
12407 isl_space *space;
12409 space = isl_basic_map_take_space(bmap);
12410 space = isl_space_reset(space, type);
12411 bmap = isl_basic_map_restore_space(bmap, space);
12413 bmap = isl_basic_map_mark_final(bmap);
12415 return bmap;
12418 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12419 enum isl_dim_type type)
12421 int i;
12422 isl_space *space;
12424 if (!map)
12425 return NULL;
12427 if (!isl_space_is_named_or_nested(map->dim, type))
12428 return map;
12430 map = isl_map_cow(map);
12431 if (!map)
12432 return NULL;
12434 for (i = 0; i < map->n; ++i) {
12435 map->p[i] = isl_basic_map_reset(map->p[i], type);
12436 if (!map->p[i])
12437 goto error;
12440 space = isl_map_take_space(map);
12441 space = isl_space_reset(space, type);
12442 map = isl_map_restore_space(map, space);
12444 return map;
12445 error:
12446 isl_map_free(map);
12447 return NULL;
12450 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12452 isl_space *space;
12454 space = isl_basic_map_take_space(bmap);
12455 space = isl_space_flatten(space);
12456 bmap = isl_basic_map_restore_space(bmap, space);
12458 bmap = isl_basic_map_mark_final(bmap);
12460 return bmap;
12463 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12465 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12468 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12469 __isl_take isl_basic_map *bmap)
12471 isl_space *space;
12473 space = isl_basic_map_take_space(bmap);
12474 space = isl_space_flatten_domain(space);
12475 bmap = isl_basic_map_restore_space(bmap, space);
12477 bmap = isl_basic_map_mark_final(bmap);
12479 return bmap;
12482 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12483 __isl_take isl_basic_map *bmap)
12485 isl_space *space;
12487 space = isl_basic_map_take_space(bmap);
12488 space = isl_space_flatten_range(space);
12489 bmap = isl_basic_map_restore_space(bmap, space);
12491 bmap = isl_basic_map_mark_final(bmap);
12493 return bmap;
12496 /* Remove any internal structure from the spaces of domain and range of "map".
12498 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12500 if (!map)
12501 return NULL;
12503 if (!map->dim->nested[0] && !map->dim->nested[1])
12504 return map;
12506 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12509 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12511 return set_from_map(isl_map_flatten(set_to_map(set)));
12514 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12516 isl_space *space, *flat_space;
12517 isl_map *map;
12519 space = isl_set_get_space(set);
12520 flat_space = isl_space_flatten(isl_space_copy(space));
12521 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12522 flat_space));
12523 map = isl_map_intersect_domain(map, set);
12525 return map;
12528 /* Remove any internal structure from the space of the domain of "map".
12530 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12532 if (!map)
12533 return NULL;
12535 if (!map->dim->nested[0])
12536 return map;
12538 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12541 /* Remove any internal structure from the space of the range of "map".
12543 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12545 if (!map)
12546 return NULL;
12548 if (!map->dim->nested[1])
12549 return map;
12551 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12554 /* Reorder the dimensions of "bmap" according to the given dim_map
12555 * and set the dimension specification to "space" and
12556 * perform Gaussian elimination on the result.
12558 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12559 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12561 isl_basic_map *res;
12562 unsigned flags;
12563 isl_size n_div;
12565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12566 if (n_div < 0 || !space || !dim_map)
12567 goto error;
12569 flags = bmap->flags;
12570 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12571 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12572 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12573 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12574 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12575 if (res)
12576 res->flags = flags;
12577 res = isl_basic_map_gauss(res, NULL);
12578 res = isl_basic_map_finalize(res);
12579 return res;
12580 error:
12581 isl_dim_map_free(dim_map);
12582 isl_basic_map_free(bmap);
12583 isl_space_free(space);
12584 return NULL;
12587 /* Reorder the dimensions of "map" according to given reordering.
12589 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12590 __isl_take isl_reordering *r)
12592 int i;
12593 struct isl_dim_map *dim_map;
12595 map = isl_map_cow(map);
12596 dim_map = isl_dim_map_from_reordering(r);
12597 if (!map || !r || !dim_map)
12598 goto error;
12600 for (i = 0; i < map->n; ++i) {
12601 struct isl_dim_map *dim_map_i;
12602 isl_space *space;
12604 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12606 space = isl_reordering_get_space(r);
12607 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12609 if (!map->p[i])
12610 goto error;
12613 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12614 map = isl_map_unmark_normalized(map);
12616 isl_reordering_free(r);
12617 isl_dim_map_free(dim_map);
12618 return map;
12619 error:
12620 isl_dim_map_free(dim_map);
12621 isl_map_free(map);
12622 isl_reordering_free(r);
12623 return NULL;
12626 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12627 __isl_take isl_reordering *r)
12629 return set_from_map(isl_map_realign(set_to_map(set), r));
12632 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12633 __isl_take isl_space *model)
12635 isl_ctx *ctx;
12636 isl_bool aligned;
12638 if (!map || !model)
12639 goto error;
12641 ctx = isl_space_get_ctx(model);
12642 if (!isl_space_has_named_params(model))
12643 isl_die(ctx, isl_error_invalid,
12644 "model has unnamed parameters", goto error);
12645 if (isl_map_check_named_params(map) < 0)
12646 goto error;
12647 aligned = isl_map_space_has_equal_params(map, model);
12648 if (aligned < 0)
12649 goto error;
12650 if (!aligned) {
12651 isl_space *space;
12652 isl_reordering *exp;
12654 space = isl_map_peek_space(map);
12655 exp = isl_parameter_alignment_reordering(space, model);
12656 map = isl_map_realign(map, exp);
12659 isl_space_free(model);
12660 return map;
12661 error:
12662 isl_space_free(model);
12663 isl_map_free(map);
12664 return NULL;
12667 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12668 __isl_take isl_space *model)
12670 return isl_map_align_params(set, model);
12673 /* Align the parameters of "bmap" to those of "model", introducing
12674 * additional parameters if needed.
12676 __isl_give isl_basic_map *isl_basic_map_align_params(
12677 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12679 isl_ctx *ctx;
12680 isl_bool equal_params;
12681 isl_space *bmap_space;
12683 if (!bmap || !model)
12684 goto error;
12686 ctx = isl_space_get_ctx(model);
12687 if (!isl_space_has_named_params(model))
12688 isl_die(ctx, isl_error_invalid,
12689 "model has unnamed parameters", goto error);
12690 if (isl_basic_map_check_named_params(bmap) < 0)
12691 goto error;
12692 bmap_space = isl_basic_map_peek_space(bmap);
12693 equal_params = isl_space_has_equal_params(bmap_space, model);
12694 if (equal_params < 0)
12695 goto error;
12696 if (!equal_params) {
12697 isl_reordering *exp;
12698 struct isl_dim_map *dim_map;
12700 exp = isl_parameter_alignment_reordering(bmap_space, model);
12701 dim_map = isl_dim_map_from_reordering(exp);
12702 bmap = isl_basic_map_realign(bmap,
12703 isl_reordering_get_space(exp),
12704 isl_dim_map_extend(dim_map, bmap));
12705 isl_reordering_free(exp);
12706 isl_dim_map_free(dim_map);
12709 isl_space_free(model);
12710 return bmap;
12711 error:
12712 isl_space_free(model);
12713 isl_basic_map_free(bmap);
12714 return NULL;
12717 /* Do "bset" and "space" have the same parameters?
12719 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12720 __isl_keep isl_space *space)
12722 isl_space *bset_space;
12724 bset_space = isl_basic_set_peek_space(bset);
12725 return isl_space_has_equal_params(bset_space, space);
12728 /* Do "map" and "space" have the same parameters?
12730 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12731 __isl_keep isl_space *space)
12733 isl_space *map_space;
12735 map_space = isl_map_peek_space(map);
12736 return isl_space_has_equal_params(map_space, space);
12739 /* Do "set" and "space" have the same parameters?
12741 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12742 __isl_keep isl_space *space)
12744 return isl_map_space_has_equal_params(set_to_map(set), space);
12747 /* Align the parameters of "bset" to those of "model", introducing
12748 * additional parameters if needed.
12750 __isl_give isl_basic_set *isl_basic_set_align_params(
12751 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12753 return isl_basic_map_align_params(bset, model);
12756 #undef TYPE
12757 #define TYPE isl_map
12758 #define isl_map_drop_dims isl_map_drop
12759 #include "isl_drop_unused_params_templ.c"
12761 /* Drop all parameters not referenced by "set".
12763 __isl_give isl_set *isl_set_drop_unused_params(
12764 __isl_take isl_set *set)
12766 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12769 #undef TYPE
12770 #define TYPE isl_basic_map
12771 #define isl_basic_map_drop_dims isl_basic_map_drop
12772 #include "isl_drop_unused_params_templ.c"
12774 /* Drop all parameters not referenced by "bset".
12776 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12777 __isl_take isl_basic_set *bset)
12779 return bset_from_bmap(isl_basic_map_drop_unused_params(
12780 bset_to_bmap(bset)));
12783 /* Given a tuple of identifiers "tuple" in a space that corresponds
12784 * to that of "set", if any of those identifiers appear as parameters
12785 * in "set", then equate those parameters with the corresponding
12786 * set dimensions and project out the parameters.
12787 * The result therefore has no such parameters.
12789 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12790 __isl_keep isl_multi_id *tuple)
12792 int i;
12793 isl_size n;
12794 isl_space *set_space, *tuple_space;
12796 set_space = isl_set_peek_space(set);
12797 tuple_space = isl_multi_id_peek_space(tuple);
12798 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12799 return isl_set_free(set);
12800 n = isl_multi_id_size(tuple);
12801 if (n < 0)
12802 return isl_set_free(set);
12803 for (i = 0; i < n; ++i) {
12804 isl_id *id;
12805 int pos;
12807 id = isl_multi_id_get_at(tuple, i);
12808 if (!id)
12809 return isl_set_free(set);
12810 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12811 isl_id_free(id);
12812 if (pos < 0)
12813 continue;
12814 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12815 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12817 return set;
12820 /* Bind the set dimensions of "set" to parameters with identifiers
12821 * specified by "tuple", living in the same space as "set".
12823 * If no parameters with these identifiers appear in "set" already,
12824 * then the set dimensions are simply reinterpreted as parameters.
12825 * Otherwise, the parameters are first equated to the corresponding
12826 * set dimensions.
12828 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12829 __isl_take isl_multi_id *tuple)
12831 isl_space *space;
12833 set = equate_params(set, tuple);
12834 space = isl_set_get_space(set);
12835 space = isl_space_bind_set(space, tuple);
12836 isl_multi_id_free(tuple);
12837 set = isl_set_reset_space(set, space);
12839 return set;
12842 /* Given a tuple of identifiers "tuple" in a space that corresponds
12843 * to the domain of "map", if any of those identifiers appear as parameters
12844 * in "map", then equate those parameters with the corresponding
12845 * input dimensions and project out the parameters.
12846 * The result therefore has no such parameters.
12848 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12849 __isl_keep isl_multi_id *tuple)
12851 int i;
12852 isl_size n;
12853 isl_space *map_space, *tuple_space;
12855 map_space = isl_map_peek_space(map);
12856 tuple_space = isl_multi_id_peek_space(tuple);
12857 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12858 return isl_map_free(map);
12859 n = isl_multi_id_size(tuple);
12860 if (n < 0)
12861 return isl_map_free(map);
12862 for (i = 0; i < n; ++i) {
12863 isl_id *id;
12864 int pos;
12866 id = isl_multi_id_get_at(tuple, i);
12867 if (!id)
12868 return isl_map_free(map);
12869 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12870 isl_id_free(id);
12871 if (pos < 0)
12872 continue;
12873 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12874 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12876 return map;
12879 /* Bind the input dimensions of "map" to parameters with identifiers
12880 * specified by "tuple", living in the domain space of "map".
12882 * If no parameters with these identifiers appear in "map" already,
12883 * then the input dimensions are simply reinterpreted as parameters.
12884 * Otherwise, the parameters are first equated to the corresponding
12885 * input dimensions.
12887 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12888 __isl_take isl_multi_id *tuple)
12890 isl_space *space;
12891 isl_set *set;
12893 map = map_equate_params(map, tuple);
12894 space = isl_map_get_space(map);
12895 space = isl_space_bind_map_domain(space, tuple);
12896 isl_multi_id_free(tuple);
12897 set = set_from_map(isl_map_reset_space(map, space));
12899 return set;
12902 /* Bind the output dimensions of "map" to parameters with identifiers
12903 * specified by "tuple", living in the range space of "map".
12905 * Since binding is more easily implemented on the domain,
12906 * bind the input dimensions of the inverse of "map".
12908 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12909 __isl_take isl_multi_id *tuple)
12911 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12914 /* Insert a domain corresponding to "tuple"
12915 * into the nullary or unary relation "set".
12916 * The result has an extra initial tuple and is therefore
12917 * either a unary or binary relation.
12918 * Any parameters with identifiers in "tuple" are reinterpreted
12919 * as the corresponding domain dimensions.
12921 static __isl_give isl_map *unbind_params_insert_domain(
12922 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12924 isl_space *space;
12925 isl_reordering *r;
12927 space = isl_set_peek_space(set);
12928 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12929 isl_multi_id_free(tuple);
12931 return isl_map_realign(set_to_map(set), r);
12934 /* Construct a set with "tuple" as domain from the parameter domain "set".
12935 * Any parameters with identifiers in "tuple" are reinterpreted
12936 * as the corresponding set dimensions.
12938 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12939 __isl_take isl_multi_id *tuple)
12941 isl_bool is_params;
12943 is_params = isl_set_is_params(set);
12944 if (is_params < 0)
12945 set = isl_set_free(set);
12946 else if (!is_params)
12947 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12948 "expecting parameter domain", set = isl_set_free(set));
12949 return set_from_map(unbind_params_insert_domain(set, tuple));
12952 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12954 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12956 isl_bool is_params;
12958 is_params = isl_set_is_params(set);
12959 if (is_params < 0)
12960 return isl_stat_error;
12961 else if (is_params)
12962 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12963 "expecting proper set", return isl_stat_error);
12965 return isl_stat_ok;
12968 /* Construct a map with "domain" as domain and "set" as range.
12969 * Any parameters with identifiers in "domain" are reinterpreted
12970 * as the corresponding domain dimensions.
12972 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12973 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12975 if (isl_set_check_is_set(set) < 0)
12976 set = isl_set_free(set);
12977 return unbind_params_insert_domain(set, domain);
12980 /* Construct a map with "domain" as domain and "set" as range.
12982 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12983 __isl_take isl_space *domain)
12985 isl_size dim;
12986 isl_space *space;
12987 isl_map *map;
12989 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12990 domain = isl_space_free(domain);
12991 dim = isl_space_dim(domain, isl_dim_set);
12992 if (dim < 0)
12993 domain = isl_space_free(domain);
12994 space = isl_set_get_space(set);
12995 domain = isl_space_replace_params(domain, space);
12996 space = isl_space_map_from_domain_and_range(domain, space);
12998 map = isl_map_from_range(set);
12999 map = isl_map_add_dims(map, isl_dim_in, dim);
13000 map = isl_map_reset_space(map, space);
13002 return map;
13005 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13006 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13007 enum isl_dim_type c2, enum isl_dim_type c3,
13008 enum isl_dim_type c4, enum isl_dim_type c5)
13010 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13011 struct isl_mat *mat;
13012 int i, j, k;
13013 int pos;
13014 isl_size total;
13016 total = isl_basic_map_dim(bmap, isl_dim_all);
13017 if (total < 0)
13018 return NULL;
13019 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13020 if (!mat)
13021 return NULL;
13022 for (i = 0; i < bmap->n_eq; ++i)
13023 for (j = 0, pos = 0; j < 5; ++j) {
13024 int off = isl_basic_map_offset(bmap, c[j]);
13025 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13026 if (dim < 0)
13027 return isl_mat_free(mat);
13028 for (k = 0; k < dim; ++k) {
13029 isl_int_set(mat->row[i][pos],
13030 bmap->eq[i][off + k]);
13031 ++pos;
13035 return mat;
13038 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13039 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13040 enum isl_dim_type c2, enum isl_dim_type c3,
13041 enum isl_dim_type c4, enum isl_dim_type c5)
13043 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13044 struct isl_mat *mat;
13045 int i, j, k;
13046 int pos;
13047 isl_size total;
13049 total = isl_basic_map_dim(bmap, isl_dim_all);
13050 if (total < 0)
13051 return NULL;
13052 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13053 if (!mat)
13054 return NULL;
13055 for (i = 0; i < bmap->n_ineq; ++i)
13056 for (j = 0, pos = 0; j < 5; ++j) {
13057 int off = isl_basic_map_offset(bmap, c[j]);
13058 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13059 if (dim < 0)
13060 return isl_mat_free(mat);
13061 for (k = 0; k < dim; ++k) {
13062 isl_int_set(mat->row[i][pos],
13063 bmap->ineq[i][off + k]);
13064 ++pos;
13068 return mat;
13071 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13072 __isl_take isl_space *space,
13073 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13074 enum isl_dim_type c2, enum isl_dim_type c3,
13075 enum isl_dim_type c4, enum isl_dim_type c5)
13077 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13078 isl_basic_map *bmap = NULL;
13079 isl_size dim;
13080 unsigned total;
13081 unsigned extra;
13082 int i, j, k, l;
13083 int pos;
13085 dim = isl_space_dim(space, isl_dim_all);
13086 if (dim < 0 || !eq || !ineq)
13087 goto error;
13089 if (eq->n_col != ineq->n_col)
13090 isl_die(space->ctx, isl_error_invalid,
13091 "equalities and inequalities matrices should have "
13092 "same number of columns", goto error);
13094 total = 1 + dim;
13096 if (eq->n_col < total)
13097 isl_die(space->ctx, isl_error_invalid,
13098 "number of columns too small", goto error);
13100 extra = eq->n_col - total;
13102 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13103 eq->n_row, ineq->n_row);
13104 if (!bmap)
13105 goto error;
13106 for (i = 0; i < extra; ++i) {
13107 k = isl_basic_map_alloc_div(bmap);
13108 if (k < 0)
13109 goto error;
13110 isl_int_set_si(bmap->div[k][0], 0);
13112 for (i = 0; i < eq->n_row; ++i) {
13113 l = isl_basic_map_alloc_equality(bmap);
13114 if (l < 0)
13115 goto error;
13116 for (j = 0, pos = 0; j < 5; ++j) {
13117 int off = isl_basic_map_offset(bmap, c[j]);
13118 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13119 if (dim < 0)
13120 goto error;
13121 for (k = 0; k < dim; ++k) {
13122 isl_int_set(bmap->eq[l][off + k],
13123 eq->row[i][pos]);
13124 ++pos;
13128 for (i = 0; i < ineq->n_row; ++i) {
13129 l = isl_basic_map_alloc_inequality(bmap);
13130 if (l < 0)
13131 goto error;
13132 for (j = 0, pos = 0; j < 5; ++j) {
13133 int off = isl_basic_map_offset(bmap, c[j]);
13134 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13135 if (dim < 0)
13136 goto error;
13137 for (k = 0; k < dim; ++k) {
13138 isl_int_set(bmap->ineq[l][off + k],
13139 ineq->row[i][pos]);
13140 ++pos;
13145 isl_space_free(space);
13146 isl_mat_free(eq);
13147 isl_mat_free(ineq);
13149 bmap = isl_basic_map_simplify(bmap);
13150 return isl_basic_map_finalize(bmap);
13151 error:
13152 isl_space_free(space);
13153 isl_mat_free(eq);
13154 isl_mat_free(ineq);
13155 isl_basic_map_free(bmap);
13156 return NULL;
13159 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13160 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13161 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13163 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13164 c1, c2, c3, c4, isl_dim_in);
13167 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13168 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13169 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13171 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13172 c1, c2, c3, c4, isl_dim_in);
13175 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13176 __isl_take isl_space *space,
13177 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13178 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13180 isl_basic_map *bmap;
13181 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13182 c1, c2, c3, c4, isl_dim_in);
13183 return bset_from_bmap(bmap);
13186 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13188 if (!bmap)
13189 return isl_bool_error;
13191 return isl_space_can_zip(bmap->dim);
13194 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13196 if (!map)
13197 return isl_bool_error;
13199 return isl_space_can_zip(map->dim);
13202 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13203 * (A -> C) -> (B -> D).
13205 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13207 unsigned pos;
13208 isl_size n_in;
13209 isl_size n1;
13210 isl_size n2;
13212 if (!bmap)
13213 return NULL;
13215 if (!isl_basic_map_can_zip(bmap))
13216 isl_die(bmap->ctx, isl_error_invalid,
13217 "basic map cannot be zipped", goto error);
13218 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13219 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13220 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13221 if (n_in < 0 || n1 < 0 || n2 < 0)
13222 return isl_basic_map_free(bmap);
13223 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13224 bmap = isl_basic_map_cow(bmap);
13225 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13226 if (!bmap)
13227 return NULL;
13228 bmap->dim = isl_space_zip(bmap->dim);
13229 if (!bmap->dim)
13230 goto error;
13231 bmap = isl_basic_map_mark_final(bmap);
13232 return bmap;
13233 error:
13234 isl_basic_map_free(bmap);
13235 return NULL;
13238 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13239 * (A -> C) -> (B -> D).
13241 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13243 if (!map)
13244 return NULL;
13246 if (!isl_map_can_zip(map))
13247 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13248 goto error);
13250 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13251 error:
13252 isl_map_free(map);
13253 return NULL;
13256 /* Can we apply isl_basic_map_curry to "bmap"?
13257 * That is, does it have a nested relation in its domain?
13259 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13261 if (!bmap)
13262 return isl_bool_error;
13264 return isl_space_can_curry(bmap->dim);
13267 /* Can we apply isl_map_curry to "map"?
13268 * That is, does it have a nested relation in its domain?
13270 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13272 if (!map)
13273 return isl_bool_error;
13275 return isl_space_can_curry(map->dim);
13278 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13279 * A -> (B -> C).
13281 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13284 if (!bmap)
13285 return NULL;
13287 if (!isl_basic_map_can_curry(bmap))
13288 isl_die(bmap->ctx, isl_error_invalid,
13289 "basic map cannot be curried", goto error);
13290 bmap = isl_basic_map_cow(bmap);
13291 if (!bmap)
13292 return NULL;
13293 bmap->dim = isl_space_curry(bmap->dim);
13294 if (!bmap->dim)
13295 goto error;
13296 bmap = isl_basic_map_mark_final(bmap);
13297 return bmap;
13298 error:
13299 isl_basic_map_free(bmap);
13300 return NULL;
13303 /* Given a map (A -> B) -> C, return the corresponding map
13304 * A -> (B -> C).
13306 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13308 return isl_map_change_space(map, &isl_map_can_curry,
13309 "map cannot be curried", &isl_space_curry);
13312 /* Can isl_map_range_curry be applied to "map"?
13313 * That is, does it have a nested relation in its range,
13314 * the domain of which is itself a nested relation?
13316 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13318 if (!map)
13319 return isl_bool_error;
13321 return isl_space_can_range_curry(map->dim);
13324 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13325 * A -> (B -> (C -> D)).
13327 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13329 return isl_map_change_space(map, &isl_map_can_range_curry,
13330 "map range cannot be curried",
13331 &isl_space_range_curry);
13334 /* Can we apply isl_basic_map_uncurry to "bmap"?
13335 * That is, does it have a nested relation in its domain?
13337 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13339 if (!bmap)
13340 return isl_bool_error;
13342 return isl_space_can_uncurry(bmap->dim);
13345 /* Can we apply isl_map_uncurry to "map"?
13346 * That is, does it have a nested relation in its domain?
13348 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13350 if (!map)
13351 return isl_bool_error;
13353 return isl_space_can_uncurry(map->dim);
13356 /* Given a basic map A -> (B -> C), return the corresponding basic map
13357 * (A -> B) -> C.
13359 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13362 if (!bmap)
13363 return NULL;
13365 if (!isl_basic_map_can_uncurry(bmap))
13366 isl_die(bmap->ctx, isl_error_invalid,
13367 "basic map cannot be uncurried",
13368 return isl_basic_map_free(bmap));
13369 bmap = isl_basic_map_cow(bmap);
13370 if (!bmap)
13371 return NULL;
13372 bmap->dim = isl_space_uncurry(bmap->dim);
13373 if (!bmap->dim)
13374 return isl_basic_map_free(bmap);
13375 bmap = isl_basic_map_mark_final(bmap);
13376 return bmap;
13379 /* Given a map A -> (B -> C), return the corresponding map
13380 * (A -> B) -> C.
13382 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13384 return isl_map_change_space(map, &isl_map_can_uncurry,
13385 "map cannot be uncurried", &isl_space_uncurry);
13388 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13389 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13391 return isl_map_equate(set, type1, pos1, type2, pos2);
13394 /* Construct a basic map where the given dimensions are equal to each other.
13396 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13399 isl_basic_map *bmap = NULL;
13400 int i;
13401 isl_size total;
13403 total = isl_space_dim(space, isl_dim_all);
13404 if (total < 0 ||
13405 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13406 isl_space_check_range(space, type2, pos2, 1) < 0)
13407 goto error;
13409 if (type1 == type2 && pos1 == pos2)
13410 return isl_basic_map_universe(space);
13412 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13413 i = isl_basic_map_alloc_equality(bmap);
13414 if (i < 0)
13415 goto error;
13416 isl_seq_clr(bmap->eq[i], 1 + total);
13417 pos1 += isl_basic_map_offset(bmap, type1);
13418 pos2 += isl_basic_map_offset(bmap, type2);
13419 isl_int_set_si(bmap->eq[i][pos1], -1);
13420 isl_int_set_si(bmap->eq[i][pos2], 1);
13421 bmap = isl_basic_map_finalize(bmap);
13422 isl_space_free(space);
13423 return bmap;
13424 error:
13425 isl_space_free(space);
13426 isl_basic_map_free(bmap);
13427 return NULL;
13430 /* Add a constraint imposing that the given two dimensions are equal.
13432 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13433 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13435 isl_basic_map *eq;
13437 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13439 bmap = isl_basic_map_intersect(bmap, eq);
13441 return bmap;
13444 /* Add a constraint imposing that the given two dimensions are equal.
13446 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13447 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13449 isl_basic_map *bmap;
13451 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13453 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13455 return map;
13458 /* Add a constraint imposing that the given two dimensions have opposite values.
13460 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13461 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13463 isl_basic_map *bmap = NULL;
13464 int i;
13465 isl_size total;
13467 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13468 return isl_map_free(map);
13469 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13470 return isl_map_free(map);
13472 total = isl_map_dim(map, isl_dim_all);
13473 if (total < 0)
13474 return isl_map_free(map);
13475 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13476 i = isl_basic_map_alloc_equality(bmap);
13477 if (i < 0)
13478 goto error;
13479 isl_seq_clr(bmap->eq[i], 1 + total);
13480 pos1 += isl_basic_map_offset(bmap, type1);
13481 pos2 += isl_basic_map_offset(bmap, type2);
13482 isl_int_set_si(bmap->eq[i][pos1], 1);
13483 isl_int_set_si(bmap->eq[i][pos2], 1);
13484 bmap = isl_basic_map_finalize(bmap);
13486 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13488 return map;
13489 error:
13490 isl_basic_map_free(bmap);
13491 isl_map_free(map);
13492 return NULL;
13495 /* Construct a constraint imposing that the value of the first dimension is
13496 * greater than or equal to that of the second.
13498 static __isl_give isl_constraint *constraint_order_ge(
13499 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13500 enum isl_dim_type type2, int pos2)
13502 isl_constraint *c;
13504 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13505 isl_space_check_range(space, type2, pos2, 1) < 0)
13506 space = isl_space_free(space);
13507 if (!space)
13508 return NULL;
13510 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13512 if (type1 == type2 && pos1 == pos2)
13513 return c;
13515 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13516 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13518 return c;
13521 /* Add a constraint imposing that the value of the first dimension is
13522 * greater than or equal to that of the second.
13524 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13525 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13527 isl_constraint *c;
13528 isl_space *space;
13530 if (type1 == type2 && pos1 == pos2)
13531 return bmap;
13532 space = isl_basic_map_get_space(bmap);
13533 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13534 bmap = isl_basic_map_add_constraint(bmap, c);
13536 return bmap;
13539 /* Add a constraint imposing that the value of the first dimension is
13540 * greater than or equal to that of the second.
13542 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13543 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13545 isl_constraint *c;
13546 isl_space *space;
13548 if (type1 == type2 && pos1 == pos2)
13549 return map;
13550 space = isl_map_get_space(map);
13551 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13552 map = isl_map_add_constraint(map, c);
13554 return map;
13557 /* Add a constraint imposing that the value of the first dimension is
13558 * less than or equal to that of the second.
13560 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13561 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13563 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13566 /* Construct a basic map where the value of the first dimension is
13567 * greater than that of the second.
13569 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13570 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13572 isl_basic_map *bmap = NULL;
13573 int i;
13574 isl_size total;
13576 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13577 isl_space_check_range(space, type2, pos2, 1) < 0)
13578 goto error;
13580 if (type1 == type2 && pos1 == pos2)
13581 return isl_basic_map_empty(space);
13583 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13584 total = isl_basic_map_dim(bmap, isl_dim_all);
13585 i = isl_basic_map_alloc_inequality(bmap);
13586 if (total < 0 || i < 0)
13587 return isl_basic_map_free(bmap);
13588 isl_seq_clr(bmap->ineq[i], 1 + total);
13589 pos1 += isl_basic_map_offset(bmap, type1);
13590 pos2 += isl_basic_map_offset(bmap, type2);
13591 isl_int_set_si(bmap->ineq[i][pos1], 1);
13592 isl_int_set_si(bmap->ineq[i][pos2], -1);
13593 isl_int_set_si(bmap->ineq[i][0], -1);
13594 bmap = isl_basic_map_finalize(bmap);
13596 return bmap;
13597 error:
13598 isl_space_free(space);
13599 isl_basic_map_free(bmap);
13600 return NULL;
13603 /* Add a constraint imposing that the value of the first dimension is
13604 * greater than that of the second.
13606 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13607 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13609 isl_basic_map *gt;
13611 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13613 bmap = isl_basic_map_intersect(bmap, gt);
13615 return bmap;
13618 /* Add a constraint imposing that the value of the first dimension is
13619 * greater than that of the second.
13621 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13622 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13624 isl_basic_map *bmap;
13626 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13628 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13630 return map;
13633 /* Add a constraint imposing that the value of the first dimension is
13634 * smaller than that of the second.
13636 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13637 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13639 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13642 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13643 int pos)
13645 isl_aff *div;
13646 isl_local_space *ls;
13648 if (!bmap)
13649 return NULL;
13651 if (!isl_basic_map_divs_known(bmap))
13652 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13653 "some divs are unknown", return NULL);
13655 ls = isl_basic_map_get_local_space(bmap);
13656 div = isl_local_space_get_div(ls, pos);
13657 isl_local_space_free(ls);
13659 return div;
13662 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13663 int pos)
13665 return isl_basic_map_get_div(bset, pos);
13668 /* Plug in "subs" for set dimension "pos" of "set".
13670 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13671 unsigned pos, __isl_keep isl_aff *subs)
13673 isl_multi_aff *ma;
13675 if (set && isl_set_plain_is_empty(set))
13676 return set;
13678 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13679 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13680 return isl_set_preimage_multi_aff(set, ma);
13683 /* Check if the range of "ma" is compatible with the domain or range
13684 * (depending on "type") of "bmap".
13686 static isl_stat check_basic_map_compatible_range_multi_aff(
13687 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13688 __isl_keep isl_multi_aff *ma)
13690 isl_bool m;
13691 isl_space *ma_space;
13693 ma_space = isl_multi_aff_get_space(ma);
13695 m = isl_space_has_equal_params(bmap->dim, ma_space);
13696 if (m < 0)
13697 goto error;
13698 if (!m)
13699 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13700 "parameters don't match", goto error);
13701 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13702 if (m < 0)
13703 goto error;
13704 if (!m)
13705 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13706 "spaces don't match", goto error);
13708 isl_space_free(ma_space);
13709 return isl_stat_ok;
13710 error:
13711 isl_space_free(ma_space);
13712 return isl_stat_error;
13715 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13716 * coefficients before the transformed range of dimensions,
13717 * the "n_after" coefficients after the transformed range of dimensions
13718 * and the coefficients of the other divs in "bmap".
13720 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13721 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13723 int i;
13724 isl_size n_param;
13725 isl_size n_set;
13726 isl_local_space *ls;
13728 if (n_div == 0)
13729 return bmap;
13731 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13732 n_param = isl_local_space_dim(ls, isl_dim_param);
13733 n_set = isl_local_space_dim(ls, isl_dim_set);
13734 if (n_param < 0 || n_set < 0)
13735 return isl_basic_map_free(bmap);
13737 for (i = 0; i < n_div; ++i) {
13738 int o_bmap = 0, o_ls = 0;
13740 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13741 o_bmap += 1 + 1 + n_param;
13742 o_ls += 1 + 1 + n_param;
13743 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13744 o_bmap += n_before;
13745 isl_seq_cpy(bmap->div[i] + o_bmap,
13746 ls->div->row[i] + o_ls, n_set);
13747 o_bmap += n_set;
13748 o_ls += n_set;
13749 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13750 o_bmap += n_after;
13751 isl_seq_cpy(bmap->div[i] + o_bmap,
13752 ls->div->row[i] + o_ls, n_div);
13753 o_bmap += n_div;
13754 o_ls += n_div;
13755 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13756 bmap = isl_basic_map_add_div_constraints(bmap, i);
13757 if (!bmap)
13758 goto error;
13761 isl_local_space_free(ls);
13762 return bmap;
13763 error:
13764 isl_local_space_free(ls);
13765 return isl_basic_map_free(bmap);
13768 /* How many stride constraints does "ma" enforce?
13769 * That is, how many of the affine expressions have a denominator
13770 * different from one?
13772 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13774 int i;
13775 int strides = 0;
13777 for (i = 0; i < ma->n; ++i)
13778 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13779 strides++;
13781 return strides;
13784 /* For each affine expression in ma of the form
13786 * x_i = (f_i y + h_i)/m_i
13788 * with m_i different from one, add a constraint to "bmap"
13789 * of the form
13791 * f_i y + h_i = m_i alpha_i
13793 * with alpha_i an additional existentially quantified variable.
13795 * The input variables of "ma" correspond to a subset of the variables
13796 * of "bmap". There are "n_before" variables in "bmap" before this
13797 * subset and "n_after" variables after this subset.
13798 * The integer divisions of the affine expressions in "ma" are assumed
13799 * to have been aligned. There are "n_div_ma" of them and
13800 * they appear first in "bmap", straight after the "n_after" variables.
13802 static __isl_give isl_basic_map *add_ma_strides(
13803 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13804 int n_before, int n_after, int n_div_ma)
13806 int i, k;
13807 int div;
13808 isl_size total;
13809 isl_size n_param;
13810 isl_size n_in;
13812 total = isl_basic_map_dim(bmap, isl_dim_all);
13813 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13814 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13815 if (total < 0 || n_param < 0 || n_in < 0)
13816 return isl_basic_map_free(bmap);
13817 for (i = 0; i < ma->n; ++i) {
13818 int o_bmap = 0, o_ma = 1;
13820 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13821 continue;
13822 div = isl_basic_map_alloc_div(bmap);
13823 k = isl_basic_map_alloc_equality(bmap);
13824 if (div < 0 || k < 0)
13825 goto error;
13826 isl_int_set_si(bmap->div[div][0], 0);
13827 isl_seq_cpy(bmap->eq[k] + o_bmap,
13828 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13829 o_bmap += 1 + n_param;
13830 o_ma += 1 + n_param;
13831 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13832 o_bmap += n_before;
13833 isl_seq_cpy(bmap->eq[k] + o_bmap,
13834 ma->u.p[i]->v->el + o_ma, n_in);
13835 o_bmap += n_in;
13836 o_ma += n_in;
13837 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13838 o_bmap += n_after;
13839 isl_seq_cpy(bmap->eq[k] + o_bmap,
13840 ma->u.p[i]->v->el + o_ma, n_div_ma);
13841 o_bmap += n_div_ma;
13842 o_ma += n_div_ma;
13843 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13844 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13845 total++;
13848 return bmap;
13849 error:
13850 isl_basic_map_free(bmap);
13851 return NULL;
13854 /* Replace the domain or range space (depending on "type) of "space" by "set".
13856 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13857 enum isl_dim_type type, __isl_take isl_space *set)
13859 if (type == isl_dim_in) {
13860 space = isl_space_range(space);
13861 space = isl_space_map_from_domain_and_range(set, space);
13862 } else {
13863 space = isl_space_domain(space);
13864 space = isl_space_map_from_domain_and_range(space, set);
13867 return space;
13870 /* Compute the preimage of the domain or range (depending on "type")
13871 * of "bmap" under the function represented by "ma".
13872 * In other words, plug in "ma" in the domain or range of "bmap".
13873 * The result is a basic map that lives in the same space as "bmap"
13874 * except that the domain or range has been replaced by
13875 * the domain space of "ma".
13877 * If bmap is represented by
13879 * A(p) + S u + B x + T v + C(divs) >= 0,
13881 * where u and x are input and output dimensions if type == isl_dim_out
13882 * while x and v are input and output dimensions if type == isl_dim_in,
13883 * and ma is represented by
13885 * x = D(p) + F(y) + G(divs')
13887 * then the result is
13889 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13891 * The divs in the input set are similarly adjusted.
13892 * In particular
13894 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13896 * becomes
13898 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13899 * B_i G(divs') + c_i(divs))/n_i)
13901 * If bmap is not a rational map and if F(y) involves any denominators
13903 * x_i = (f_i y + h_i)/m_i
13905 * then additional constraints are added to ensure that we only
13906 * map back integer points. That is we enforce
13908 * f_i y + h_i = m_i alpha_i
13910 * with alpha_i an additional existentially quantified variable.
13912 * We first copy over the divs from "ma".
13913 * Then we add the modified constraints and divs from "bmap".
13914 * Finally, we add the stride constraints, if needed.
13916 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13917 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13918 __isl_take isl_multi_aff *ma)
13920 int i, k;
13921 isl_space *space;
13922 isl_basic_map *res = NULL;
13923 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13924 isl_int f, c1, c2, g;
13925 isl_bool rational;
13926 int strides;
13928 isl_int_init(f);
13929 isl_int_init(c1);
13930 isl_int_init(c2);
13931 isl_int_init(g);
13933 ma = isl_multi_aff_align_divs(ma);
13934 if (!bmap || !ma)
13935 goto error;
13936 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13937 goto error;
13939 if (type == isl_dim_in) {
13940 n_before = 0;
13941 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13942 } else {
13943 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13944 n_after = 0;
13946 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13947 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13948 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13949 goto error;
13951 space = isl_multi_aff_get_domain_space(ma);
13952 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13953 rational = isl_basic_map_is_rational(bmap);
13954 strides = rational ? 0 : multi_aff_strides(ma);
13955 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13956 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13957 if (rational)
13958 res = isl_basic_map_set_rational(res);
13960 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13961 if (isl_basic_map_alloc_div(res) < 0)
13962 goto error;
13964 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13965 if (!res)
13966 goto error;
13968 for (i = 0; i < bmap->n_eq; ++i) {
13969 k = isl_basic_map_alloc_equality(res);
13970 if (k < 0)
13971 goto error;
13972 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13973 n_after, n_div_ma, n_div_bmap,
13974 f, c1, c2, g, 0) < 0)
13975 goto error;
13978 for (i = 0; i < bmap->n_ineq; ++i) {
13979 k = isl_basic_map_alloc_inequality(res);
13980 if (k < 0)
13981 goto error;
13982 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13983 n_after, n_div_ma, n_div_bmap,
13984 f, c1, c2, g, 0) < 0)
13985 goto error;
13988 for (i = 0; i < bmap->n_div; ++i) {
13989 if (isl_int_is_zero(bmap->div[i][0])) {
13990 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13991 continue;
13993 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13994 n_before, n_after, n_div_ma, n_div_bmap,
13995 f, c1, c2, g, 1) < 0)
13996 goto error;
13999 if (strides)
14000 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
14002 isl_int_clear(f);
14003 isl_int_clear(c1);
14004 isl_int_clear(c2);
14005 isl_int_clear(g);
14006 isl_basic_map_free(bmap);
14007 isl_multi_aff_free(ma);
14008 res = isl_basic_map_simplify(res);
14009 return isl_basic_map_finalize(res);
14010 error:
14011 isl_int_clear(f);
14012 isl_int_clear(c1);
14013 isl_int_clear(c2);
14014 isl_int_clear(g);
14015 isl_basic_map_free(bmap);
14016 isl_multi_aff_free(ma);
14017 isl_basic_map_free(res);
14018 return NULL;
14021 /* Compute the preimage of "bset" under the function represented by "ma".
14022 * In other words, plug in "ma" in "bset". The result is a basic set
14023 * that lives in the domain space of "ma".
14025 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14026 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14028 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14031 /* Compute the preimage of the domain of "bmap" under the function
14032 * represented by "ma".
14033 * In other words, plug in "ma" in the domain of "bmap".
14034 * The result is a basic map that lives in the same space as "bmap"
14035 * except that the domain has been replaced by the domain space of "ma".
14037 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14038 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14040 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14043 /* Compute the preimage of the range of "bmap" under the function
14044 * represented by "ma".
14045 * In other words, plug in "ma" in the range of "bmap".
14046 * The result is a basic map that lives in the same space as "bmap"
14047 * except that the range has been replaced by the domain space of "ma".
14049 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14050 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14052 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14055 /* Check if the range of "ma" is compatible with the domain or range
14056 * (depending on "type") of "map".
14057 * Return isl_stat_error if anything is wrong.
14059 static isl_stat check_map_compatible_range_multi_aff(
14060 __isl_keep isl_map *map, enum isl_dim_type type,
14061 __isl_keep isl_multi_aff *ma)
14063 isl_bool m;
14064 isl_space *ma_space;
14066 ma_space = isl_multi_aff_get_space(ma);
14067 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14068 isl_space_free(ma_space);
14069 if (m < 0)
14070 return isl_stat_error;
14071 if (!m)
14072 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14073 "spaces don't match", return isl_stat_error);
14074 return isl_stat_ok;
14077 /* Compute the preimage of the domain or range (depending on "type")
14078 * of "map" under the function represented by "ma".
14079 * In other words, plug in "ma" in the domain or range of "map".
14080 * The result is a map that lives in the same space as "map"
14081 * except that the domain or range has been replaced by
14082 * the domain space of "ma".
14084 * The parameters are assumed to have been aligned.
14086 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14087 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14089 int i;
14090 isl_space *space;
14092 map = isl_map_cow(map);
14093 ma = isl_multi_aff_align_divs(ma);
14094 if (!map || !ma)
14095 goto error;
14096 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14097 goto error;
14099 for (i = 0; i < map->n; ++i) {
14100 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14101 isl_multi_aff_copy(ma));
14102 if (!map->p[i])
14103 goto error;
14106 space = isl_multi_aff_get_domain_space(ma);
14107 space = isl_space_set(isl_map_get_space(map), type, space);
14109 isl_space_free(isl_map_take_space(map));
14110 map = isl_map_restore_space(map, space);
14111 if (!map)
14112 goto error;
14114 isl_multi_aff_free(ma);
14115 if (map->n > 1)
14116 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14117 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14118 return map;
14119 error:
14120 isl_multi_aff_free(ma);
14121 isl_map_free(map);
14122 return NULL;
14125 /* Compute the preimage of the domain or range (depending on "type")
14126 * of "map" under the function represented by "ma".
14127 * In other words, plug in "ma" in the domain or range of "map".
14128 * The result is a map that lives in the same space as "map"
14129 * except that the domain or range has been replaced by
14130 * the domain space of "ma".
14132 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14133 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14135 isl_bool aligned;
14137 if (!map || !ma)
14138 goto error;
14140 aligned = isl_map_space_has_equal_params(map, ma->space);
14141 if (aligned < 0)
14142 goto error;
14143 if (aligned)
14144 return map_preimage_multi_aff(map, type, ma);
14146 if (isl_map_check_named_params(map) < 0)
14147 goto error;
14148 if (!isl_space_has_named_params(ma->space))
14149 isl_die(map->ctx, isl_error_invalid,
14150 "unaligned unnamed parameters", goto error);
14151 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14152 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14154 return map_preimage_multi_aff(map, type, ma);
14155 error:
14156 isl_multi_aff_free(ma);
14157 return isl_map_free(map);
14160 /* Compute the preimage of "set" under the function represented by "ma".
14161 * In other words, plug in "ma" in "set". The result is a set
14162 * that lives in the domain space of "ma".
14164 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14165 __isl_take isl_multi_aff *ma)
14167 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14170 /* Compute the preimage of the domain of "map" under the function
14171 * represented by "ma".
14172 * In other words, plug in "ma" in the domain of "map".
14173 * The result is a map that lives in the same space as "map"
14174 * except that the domain has been replaced by the domain space of "ma".
14176 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14177 __isl_take isl_multi_aff *ma)
14179 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14182 /* Compute the preimage of the range of "map" under the function
14183 * represented by "ma".
14184 * In other words, plug in "ma" in the range of "map".
14185 * The result is a map that lives in the same space as "map"
14186 * except that the range has been replaced by the domain space of "ma".
14188 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14189 __isl_take isl_multi_aff *ma)
14191 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14194 /* Compute the preimage of "map" under the function represented by "pma".
14195 * In other words, plug in "pma" in the domain or range of "map".
14196 * The result is a map that lives in the same space as "map",
14197 * except that the space of type "type" has been replaced by
14198 * the domain space of "pma".
14200 * The parameters of "map" and "pma" are assumed to have been aligned.
14202 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14203 __isl_take isl_map *map, enum isl_dim_type type,
14204 __isl_take isl_pw_multi_aff *pma)
14206 int i;
14207 isl_map *res;
14209 if (!pma)
14210 goto error;
14212 if (pma->n == 0) {
14213 isl_space *space;
14215 space = isl_pw_multi_aff_get_domain_space(pma);
14216 isl_pw_multi_aff_free(pma);
14217 space = isl_space_set(isl_map_get_space(map), type, space);
14218 isl_map_free(map);
14219 return isl_map_empty(space);
14222 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14223 isl_multi_aff_copy(pma->p[0].maff));
14224 if (type == isl_dim_in)
14225 res = isl_map_intersect_domain(res,
14226 isl_map_copy(pma->p[0].set));
14227 else
14228 res = isl_map_intersect_range(res,
14229 isl_map_copy(pma->p[0].set));
14231 for (i = 1; i < pma->n; ++i) {
14232 isl_map *res_i;
14234 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14235 isl_multi_aff_copy(pma->p[i].maff));
14236 if (type == isl_dim_in)
14237 res_i = isl_map_intersect_domain(res_i,
14238 isl_map_copy(pma->p[i].set));
14239 else
14240 res_i = isl_map_intersect_range(res_i,
14241 isl_map_copy(pma->p[i].set));
14242 res = isl_map_union(res, res_i);
14245 isl_pw_multi_aff_free(pma);
14246 isl_map_free(map);
14247 return res;
14248 error:
14249 isl_pw_multi_aff_free(pma);
14250 isl_map_free(map);
14251 return NULL;
14254 /* Compute the preimage of "map" under the function represented by "pma".
14255 * In other words, plug in "pma" in the domain or range of "map".
14256 * The result is a map that lives in the same space as "map",
14257 * except that the space of type "type" has been replaced by
14258 * the domain space of "pma".
14260 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14261 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14263 isl_bool aligned;
14265 if (!map || !pma)
14266 goto error;
14268 aligned = isl_map_space_has_equal_params(map, pma->dim);
14269 if (aligned < 0)
14270 goto error;
14271 if (aligned)
14272 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14274 if (isl_map_check_named_params(map) < 0)
14275 goto error;
14276 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14277 goto error;
14278 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14279 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14281 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14282 error:
14283 isl_pw_multi_aff_free(pma);
14284 return isl_map_free(map);
14287 /* Compute the preimage of "set" under the function represented by "pma".
14288 * In other words, plug in "pma" in "set". The result is a set
14289 * that lives in the domain space of "pma".
14291 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14292 __isl_take isl_pw_multi_aff *pma)
14294 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14297 /* Compute the preimage of the domain of "map" under the function
14298 * represented by "pma".
14299 * In other words, plug in "pma" in the domain of "map".
14300 * The result is a map that lives in the same space as "map",
14301 * except that domain space has been replaced by the domain space of "pma".
14303 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14304 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14306 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14309 /* Compute the preimage of the range of "map" under the function
14310 * represented by "pma".
14311 * In other words, plug in "pma" in the range of "map".
14312 * The result is a map that lives in the same space as "map",
14313 * except that range space has been replaced by the domain space of "pma".
14315 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14316 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14318 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14321 /* Compute the preimage of "map" under the function represented by "mpa".
14322 * In other words, plug in "mpa" in the domain or range of "map".
14323 * The result is a map that lives in the same space as "map",
14324 * except that the space of type "type" has been replaced by
14325 * the domain space of "mpa".
14327 * If the map does not involve any constraints that refer to the
14328 * dimensions of the substituted space, then the only possible
14329 * effect of "mpa" on the map is to map the space to a different space.
14330 * We create a separate isl_multi_aff to effectuate this change
14331 * in order to avoid spurious splitting of the map along the pieces
14332 * of "mpa".
14333 * If "mpa" has a non-trivial explicit domain, however,
14334 * then the full substitution should be performed.
14336 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14337 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14339 isl_size n;
14340 isl_bool full;
14341 isl_pw_multi_aff *pma;
14343 n = isl_map_dim(map, type);
14344 if (n < 0 || !mpa)
14345 goto error;
14347 full = isl_map_involves_dims(map, type, 0, n);
14348 if (full >= 0 && !full)
14349 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14350 if (full < 0)
14351 goto error;
14352 if (!full) {
14353 isl_space *space;
14354 isl_multi_aff *ma;
14356 space = isl_multi_pw_aff_get_space(mpa);
14357 isl_multi_pw_aff_free(mpa);
14358 ma = isl_multi_aff_zero(space);
14359 return isl_map_preimage_multi_aff(map, type, ma);
14362 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14363 return isl_map_preimage_pw_multi_aff(map, type, pma);
14364 error:
14365 isl_map_free(map);
14366 isl_multi_pw_aff_free(mpa);
14367 return NULL;
14370 /* Compute the preimage of "map" under the function represented by "mpa".
14371 * In other words, plug in "mpa" in the domain "map".
14372 * The result is a map that lives in the same space as "map",
14373 * except that domain space has been replaced by the domain space of "mpa".
14375 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14376 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14378 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14381 /* Compute the preimage of "set" by the function represented by "mpa".
14382 * In other words, plug in "mpa" in "set".
14384 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14385 __isl_take isl_multi_pw_aff *mpa)
14387 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14390 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14391 * on the output dimension "pos" in terms of the parameters,
14392 * the input dimensions and possibly some integer divisions,
14393 * but not any other output dimensions, extract this upper bound
14394 * as a function of all dimensions (with zero coefficients
14395 * for the output dimensions).
14397 * That is, the inequality is of the form
14399 * e(...) + c - m x >= 0
14401 * where e does not depend on any other output dimensions.
14402 * Return (e(...) + c) / m, with the denominator m in the first position.
14404 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14405 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14407 isl_ctx *ctx;
14408 isl_size v_out, total;
14410 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14411 total = isl_basic_map_dim(bmap, isl_dim_all);
14412 if (v_out < 0 || total < 0)
14413 return NULL;
14414 ctx = isl_basic_map_get_ctx(bmap);
14415 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14416 total, v_out + pos);
14419 /* Is constraint "c" of "bmap" of the form
14421 * e(...) + c1 - m x >= 0
14423 * or
14425 * -e(...) + c2 + m x >= 0
14427 * where m > 1 and e does not involve any other output variables?
14429 * "v_out" is the offset to the output variables.
14430 * "d" is the position of x among the output variables.
14431 * "v_div" is the offset to the local variables.
14432 * "total" is the total number of variables.
14434 * Since the purpose of this function is to use the constraint
14435 * to express the output variable as an integer division,
14436 * do not allow the constraint to involve any local variables
14437 * that do not have an explicit representation.
14439 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14440 isl_int *c, int v_out, int d, int v_div, int total)
14442 int i = 0;
14444 if (isl_int_is_zero(c[1 + v_out + d]))
14445 return isl_bool_false;
14446 if (isl_int_is_one(c[1 + v_out + d]))
14447 return isl_bool_false;
14448 if (isl_int_is_negone(c[1 + v_out + d]))
14449 return isl_bool_false;
14450 if (isl_seq_first_non_zero(c + 1 + v_out, d) != -1)
14451 return isl_bool_false;
14452 if (isl_seq_first_non_zero(c + 1 + v_out + d + 1,
14453 v_div - (v_out + d + 1)) != -1)
14454 return isl_bool_false;
14455 for (i = 0; v_div + i < total; ++i) {
14456 isl_bool known, involves;
14458 if (isl_int_is_zero(c[1 + v_div + i]))
14459 continue;
14460 known = isl_basic_map_div_is_known(bmap, i);
14461 if (known < 0 || !known)
14462 return known;
14463 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14464 if (involves < 0 || involves)
14465 return isl_bool_not(involves);
14467 return isl_bool_true;
14470 /* Look for a pair of constraints
14472 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14474 * and
14476 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14478 * that express that the output dimension x at position "pos"
14479 * is some integer division of an expression in terms of the parameters,
14480 * input dimensions and integer divisions.
14481 * If such a pair can be found, then return the index
14482 * of the upper bound constraint, m x <= e(...) + c1.
14483 * Otherwise, return an index beyond the number of constraints.
14485 * In order for the constraints above to express an integer division,
14486 * m needs to be greater than 1 and such that
14488 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14490 * In particular, this ensures that
14492 * x = floor((e(...) + c1) / m)
14494 isl_size isl_basic_map_find_output_upper_div_constraint(
14495 __isl_keep isl_basic_map *bmap, int pos)
14497 int i, j;
14498 isl_size n_ineq;
14499 isl_size v_out, v_div;
14500 isl_size total;
14501 isl_int sum;
14503 total = isl_basic_map_dim(bmap, isl_dim_all);
14504 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14505 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
14506 n_ineq = isl_basic_map_n_inequality(bmap);
14507 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
14508 return isl_size_error;
14510 isl_int_init(sum);
14511 for (i = 0; i < n_ineq; ++i) {
14512 isl_bool potential;
14514 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
14515 v_out, pos, v_div, total);
14516 if (potential < 0)
14517 goto error;
14518 if (!potential)
14519 continue;
14520 for (j = i + 1; j < n_ineq; ++j) {
14521 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
14522 bmap->ineq[j] + 1, total))
14523 continue;
14524 isl_int_add(sum, bmap->ineq[i][0], bmap->ineq[j][0]);
14525 if (isl_int_abs_lt(sum, bmap->ineq[i][1 + v_out + pos]))
14526 break;
14528 if (j < n_ineq)
14529 break;
14531 isl_int_clear(sum);
14533 if (i >= n_ineq)
14534 return n_ineq;
14535 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
14536 return i;
14537 else
14538 return j;
14539 error:
14540 isl_int_clear(sum);
14541 return isl_size_error;
14544 /* Return a copy of the equality constraints of "bset" as a matrix.
14546 __isl_give isl_mat *isl_basic_set_extract_equalities(
14547 __isl_keep isl_basic_set *bset)
14549 isl_ctx *ctx;
14550 isl_size total;
14552 total = isl_basic_set_dim(bset, isl_dim_all);
14553 if (total < 0)
14554 return NULL;
14556 ctx = isl_basic_set_get_ctx(bset);
14557 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14560 /* Are the "n" "coefficients" starting at "first" of the integer division
14561 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14562 * to each other?
14563 * The "coefficient" at position 0 is the denominator.
14564 * The "coefficient" at position 1 is the constant term.
14566 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14567 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14568 unsigned first, unsigned n)
14570 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14571 return isl_bool_error;
14572 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14573 return isl_bool_error;
14574 return isl_seq_eq(bmap1->div[pos1] + first,
14575 bmap2->div[pos2] + first, n);
14578 /* Are the integer division expressions at position "pos1" in "bmap1" and
14579 * "pos2" in "bmap2" equal to each other, except that the constant terms
14580 * are different?
14582 isl_bool isl_basic_map_equal_div_expr_except_constant(
14583 __isl_keep isl_basic_map *bmap1, int pos1,
14584 __isl_keep isl_basic_map *bmap2, int pos2)
14586 isl_bool equal;
14587 isl_size total, total2;
14589 total = isl_basic_map_dim(bmap1, isl_dim_all);
14590 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14591 if (total < 0 || total2 < 0)
14592 return isl_bool_error;
14593 if (total != total2)
14594 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14595 "incomparable div expressions", return isl_bool_error);
14596 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14597 0, 1);
14598 if (equal < 0 || !equal)
14599 return equal;
14600 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14601 1, 1);
14602 if (equal < 0 || equal)
14603 return isl_bool_not(equal);
14604 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14605 2, total);
14608 /* Replace the numerator of the constant term of the integer division
14609 * expression at position "div" in "bmap" by "value".
14610 * The caller guarantees that this does not change the meaning
14611 * of the input.
14613 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14614 __isl_take isl_basic_map *bmap, int div, int value)
14616 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14617 return isl_basic_map_free(bmap);
14619 isl_int_set_si(bmap->div[div][1], value);
14621 return bmap;
14624 /* Is the point "inner" internal to inequality constraint "ineq"
14625 * of "bset"?
14626 * The point is considered to be internal to the inequality constraint,
14627 * if it strictly lies on the positive side of the inequality constraint,
14628 * or if it lies on the constraint and the constraint is lexico-positive.
14630 static isl_bool is_internal(__isl_keep isl_vec *inner,
14631 __isl_keep isl_basic_set *bset, int ineq)
14633 isl_ctx *ctx;
14634 int pos;
14635 isl_size total;
14637 if (!inner || !bset)
14638 return isl_bool_error;
14640 ctx = isl_basic_set_get_ctx(bset);
14641 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14642 &ctx->normalize_gcd);
14643 if (!isl_int_is_zero(ctx->normalize_gcd))
14644 return isl_int_is_nonneg(ctx->normalize_gcd);
14646 total = isl_basic_set_dim(bset, isl_dim_all);
14647 if (total < 0)
14648 return isl_bool_error;
14649 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14650 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14653 /* Tighten the inequality constraints of "bset" that are outward with respect
14654 * to the point "vec".
14655 * That is, tighten the constraints that are not satisfied by "vec".
14657 * "vec" is a point internal to some superset S of "bset" that is used
14658 * to make the subsets of S disjoint, by tightening one half of the constraints
14659 * that separate two subsets. In particular, the constraints of S
14660 * are all satisfied by "vec" and should not be tightened.
14661 * Of the internal constraints, those that have "vec" on the outside
14662 * are tightened. The shared facet is included in the adjacent subset
14663 * with the opposite constraint.
14664 * For constraints that saturate "vec", this criterion cannot be used
14665 * to determine which of the two sides should be tightened.
14666 * Instead, the sign of the first non-zero coefficient is used
14667 * to make this choice. Note that this second criterion is never used
14668 * on the constraints of S since "vec" is interior to "S".
14670 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14671 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14673 int j;
14675 bset = isl_basic_set_cow(bset);
14676 if (!bset)
14677 return NULL;
14678 for (j = 0; j < bset->n_ineq; ++j) {
14679 isl_bool internal;
14681 internal = is_internal(vec, bset, j);
14682 if (internal < 0)
14683 return isl_basic_set_free(bset);
14684 if (internal)
14685 continue;
14686 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14689 return bset;
14692 /* Replace the variables x of type "type" starting at "first" in "bmap"
14693 * by x' with x = M x' with M the matrix trans.
14694 * That is, replace the corresponding coefficients c by c M.
14696 * The transformation matrix should be a square matrix.
14698 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14699 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14700 __isl_take isl_mat *trans)
14702 unsigned pos;
14704 bmap = isl_basic_map_cow(bmap);
14705 if (!bmap || !trans)
14706 goto error;
14708 if (trans->n_row != trans->n_col)
14709 isl_die(trans->ctx, isl_error_invalid,
14710 "expecting square transformation matrix", goto error);
14711 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14712 goto error;
14714 pos = isl_basic_map_offset(bmap, type) + first;
14716 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14717 isl_mat_copy(trans)) < 0)
14718 goto error;
14719 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14720 isl_mat_copy(trans)) < 0)
14721 goto error;
14722 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14723 isl_mat_copy(trans)) < 0)
14724 goto error;
14726 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14727 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14728 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
14730 isl_mat_free(trans);
14731 return bmap;
14732 error:
14733 isl_mat_free(trans);
14734 isl_basic_map_free(bmap);
14735 return NULL;
14738 /* Replace the variables x of type "type" starting at "first" in "bset"
14739 * by x' with x = M x' with M the matrix trans.
14740 * That is, replace the corresponding coefficients c by c M.
14742 * The transformation matrix should be a square matrix.
14744 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14745 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14746 __isl_take isl_mat *trans)
14748 return isl_basic_map_transform_dims(bset, type, first, trans);