isl_aff.c: pw_multi_aff_from_map_div: allow nested integer divisions
[isl.git] / isl_map.c
blobeaebe45b870a3b2a6adc21be917352e632ce5916
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <string.h>
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
28 #include <isl_blk.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
34 #include <isl_seq.h>
35 #include <isl/set.h>
36 #include <isl/map.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
39 #include <isl_sort.h>
40 #include "isl_tab.h"
41 #include <isl/vec.h>
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep const isl_basic_map *const_bset_to_bmap(
62 __isl_keep const isl_basic_set *bset)
64 return (const isl_basic_map *) bset;
67 #undef TYPE
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
73 switch (type) {
74 case isl_dim_param: return 1;
75 case isl_dim_in: return 1 + space->nparam;
76 case isl_dim_out: return 1 + space->nparam + space->n_in;
77 default: return 0;
81 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
82 enum isl_dim_type type)
84 if (!bmap)
85 return isl_size_error;
86 switch (type) {
87 case isl_dim_cst: return 1;
88 case isl_dim_param:
89 case isl_dim_in:
90 case isl_dim_out: return isl_space_dim(bmap->dim, type);
91 case isl_dim_div: return bmap->n_div;
92 case isl_dim_all: return isl_basic_map_total_dim(bmap);
93 default: return 0;
97 /* Return the space of "map".
99 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
101 return map ? map->dim : NULL;
104 /* Return the space of "set".
106 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
108 return isl_map_peek_space(set_to_map(set));
111 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
113 return isl_space_dim(isl_map_peek_space(map), type);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
120 return isl_map_dim(map, isl_dim_in);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
127 return isl_map_dim(map, isl_dim_out);
130 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
132 return isl_map_dim(set_to_map(set), type);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_set);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
146 enum isl_dim_type type)
148 isl_space *space;
150 space = isl_basic_map_peek_space(bmap);
152 switch (type) {
153 case isl_dim_param:
154 case isl_dim_in:
155 case isl_dim_out: return isl_space_offset(space, type);
156 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
157 case isl_dim_cst:
158 default:
159 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
160 "invalid dimension type", return isl_size_error);
164 /* Return the position of the variables of the given type
165 * within the sequence of variables of "bset".
167 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
168 enum isl_dim_type type)
170 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
173 /* Return the position of the coefficients of the variables of the given type
174 * within the sequence of coefficients of "bmap".
176 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
177 enum isl_dim_type type)
179 switch (type) {
180 case isl_dim_cst: return 0;
181 case isl_dim_param:
182 case isl_dim_in:
183 case isl_dim_out:
184 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
185 default: return 0;
189 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
190 enum isl_dim_type type)
192 return isl_basic_map_offset(bset, type);
195 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
197 return pos(map->dim, type);
200 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
201 enum isl_dim_type type)
203 return isl_basic_map_dim(bset, type);
206 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
208 return isl_basic_set_dim(bset, isl_dim_set);
211 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
213 return isl_basic_set_dim(bset, isl_dim_param);
216 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
218 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
221 isl_size isl_set_n_dim(__isl_keep isl_set *set)
223 return isl_set_dim(set, isl_dim_set);
226 isl_size isl_set_n_param(__isl_keep isl_set *set)
228 return isl_set_dim(set, isl_dim_param);
231 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
233 isl_size dim;
235 if (!bmap)
236 return isl_size_error;
237 dim = isl_space_dim(bmap->dim, isl_dim_all);
238 if (dim < 0)
239 return isl_size_error;
240 return dim + bmap->n_div;
243 /* Return the number of equality constraints in the description of "bmap".
244 * Return isl_size_error on error.
246 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
248 if (!bmap)
249 return isl_size_error;
250 return bmap->n_eq;
253 /* Return the number of equality constraints in the description of "bset".
254 * Return isl_size_error on error.
256 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
258 return isl_basic_map_n_equality(bset_to_bmap(bset));
261 /* Return the number of inequality constraints in the description of "bmap".
262 * Return isl_size_error on error.
264 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
266 if (!bmap)
267 return isl_size_error;
268 return bmap->n_ineq;
271 /* Return the number of inequality constraints in the description of "bset".
272 * Return isl_size_error on error.
274 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
276 return isl_basic_map_n_inequality(bset_to_bmap(bset));
279 /* Do "bmap1" and "bmap2" have the same parameters?
281 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
282 __isl_keep isl_basic_map *bmap2)
284 isl_space *space1, *space2;
286 space1 = isl_basic_map_peek_space(bmap1);
287 space2 = isl_basic_map_peek_space(bmap2);
288 return isl_space_has_equal_params(space1, space2);
291 /* Do "map1" and "map2" have the same parameters?
293 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
294 __isl_keep isl_map *map2)
296 isl_space *space1, *space2;
298 space1 = isl_map_peek_space(map1);
299 space2 = isl_map_peek_space(map2);
300 return isl_space_has_equal_params(space1, space2);
303 /* Do "map" and "set" have the same parameters?
305 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
306 __isl_keep isl_set *set)
308 return isl_map_has_equal_params(map, set_to_map(set));
311 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
313 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
314 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
316 isl_space *bmap_space, *bset_space;
318 bmap_space = isl_basic_map_peek_space(bmap);
319 bset_space = isl_basic_set_peek_space(bset);
320 return isl_space_tuple_is_equal(bmap_space, type,
321 bset_space, isl_dim_set);
324 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
326 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
327 enum isl_dim_type type, __isl_keep isl_set *set)
329 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
332 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
333 __isl_keep isl_set *set)
335 isl_bool m;
336 if (!map || !set)
337 return isl_bool_error;
338 m = isl_map_has_equal_params(map, set_to_map(set));
339 if (m < 0 || !m)
340 return m;
341 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
344 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
345 __isl_keep isl_basic_set *bset)
347 isl_bool m;
348 if (!bmap || !bset)
349 return isl_bool_error;
350 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
351 if (m < 0 || !m)
352 return m;
353 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
356 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
357 __isl_keep isl_set *set)
359 isl_bool m;
360 if (!map || !set)
361 return isl_bool_error;
362 m = isl_map_has_equal_params(map, set_to_map(set));
363 if (m < 0 || !m)
364 return m;
365 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
368 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
369 __isl_keep isl_basic_set *bset)
371 isl_bool m;
372 if (!bmap || !bset)
373 return isl_bool_error;
374 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
375 if (m < 0 || !m)
376 return m;
377 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
380 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
382 return bmap ? bmap->ctx : NULL;
385 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
387 return bset ? bset->ctx : NULL;
390 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
392 return map ? map->ctx : NULL;
395 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
397 return set ? set->ctx : NULL;
400 /* Return the space of "bmap".
402 __isl_keep isl_space *isl_basic_map_peek_space(
403 __isl_keep const isl_basic_map *bmap)
405 return bmap ? bmap->dim : NULL;
408 /* Return the space of "bset".
410 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
412 return isl_basic_map_peek_space(bset_to_bmap(bset));
415 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
417 return isl_space_copy(isl_basic_map_peek_space(bmap));
420 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
422 return isl_basic_map_get_space(bset_to_bmap(bset));
425 /* Return the space of "bmap".
426 * This may be either a copy or the space itself
427 * if there is only one reference to "bmap".
428 * This allows the space to be modified inplace
429 * if both the basic map and its space have only a single reference.
430 * The caller is not allowed to modify "bmap" between this call and
431 * a subsequent call to isl_basic_map_restore_space.
432 * The only exception is that isl_basic_map_free can be called instead.
434 static __isl_give isl_space *isl_basic_map_take_space(
435 __isl_keep isl_basic_map *bmap)
437 isl_space *space;
439 if (!bmap)
440 return NULL;
441 if (bmap->ref != 1)
442 return isl_basic_map_get_space(bmap);
443 space = bmap->dim;
444 bmap->dim = NULL;
445 return space;
448 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
449 * due to a preceding call to isl_basic_map_take_space.
450 * However, in this case, "bmap" only has a single reference and
451 * then the call to isl_basic_map_cow has no effect.
453 static __isl_give isl_basic_map *isl_basic_map_restore_space(
454 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
456 if (!bmap || !space)
457 goto error;
459 if (bmap->dim == space) {
460 isl_space_free(space);
461 return bmap;
464 bmap = isl_basic_map_cow(bmap);
465 if (!bmap)
466 goto error;
467 isl_space_free(bmap->dim);
468 bmap->dim = space;
470 return bmap;
471 error:
472 isl_basic_map_free(bmap);
473 isl_space_free(space);
474 return NULL;
477 /* Extract the divs in "bmap" as a matrix.
479 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
481 int i;
482 isl_ctx *ctx;
483 isl_mat *div;
484 isl_size v_div;
485 unsigned cols;
487 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
488 if (v_div < 0)
489 return NULL;
491 ctx = isl_basic_map_get_ctx(bmap);
492 cols = 1 + 1 + v_div + bmap->n_div;
493 div = isl_mat_alloc(ctx, bmap->n_div, cols);
494 if (!div)
495 return NULL;
497 for (i = 0; i < bmap->n_div; ++i)
498 isl_seq_cpy(div->row[i], bmap->div[i], cols);
500 return div;
503 /* Extract the divs in "bset" as a matrix.
505 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
507 return isl_basic_map_get_divs(bset);
510 __isl_give isl_local_space *isl_basic_map_get_local_space(
511 __isl_keep isl_basic_map *bmap)
513 isl_mat *div;
515 if (!bmap)
516 return NULL;
518 div = isl_basic_map_get_divs(bmap);
519 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
522 __isl_give isl_local_space *isl_basic_set_get_local_space(
523 __isl_keep isl_basic_set *bset)
525 return isl_basic_map_get_local_space(bset);
528 /* For each known div d = floor(f/m), add the constraints
530 * f - m d >= 0
531 * -(f-(m-1)) + m d >= 0
533 * Do not finalize the result.
535 static __isl_give isl_basic_map *add_known_div_constraints(
536 __isl_take isl_basic_map *bmap)
538 int i;
539 isl_size n_div;
541 n_div = isl_basic_map_dim(bmap, isl_dim_div);
542 if (n_div < 0)
543 return isl_basic_map_free(bmap);
544 if (n_div == 0)
545 return bmap;
546 bmap = isl_basic_map_cow(bmap);
547 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
548 if (!bmap)
549 return NULL;
550 for (i = 0; i < n_div; ++i) {
551 if (isl_int_is_zero(bmap->div[i][0]))
552 continue;
553 bmap = isl_basic_map_add_div_constraints(bmap, i);
556 return bmap;
559 __isl_give isl_basic_map *isl_basic_map_from_local_space(
560 __isl_take isl_local_space *ls)
562 int i;
563 isl_size n_div;
564 isl_basic_map *bmap;
566 n_div = isl_local_space_dim(ls, isl_dim_div);
567 if (n_div < 0)
568 ls = isl_local_space_free(ls);
569 if (!ls)
570 return NULL;
572 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
573 n_div, 0, 2 * n_div);
575 for (i = 0; i < n_div; ++i)
576 if (isl_basic_map_alloc_div(bmap) < 0)
577 goto error;
579 for (i = 0; i < n_div; ++i)
580 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
581 bmap = add_known_div_constraints(bmap);
583 isl_local_space_free(ls);
584 return bmap;
585 error:
586 isl_local_space_free(ls);
587 isl_basic_map_free(bmap);
588 return NULL;
591 __isl_give isl_basic_set *isl_basic_set_from_local_space(
592 __isl_take isl_local_space *ls)
594 return isl_basic_map_from_local_space(ls);
597 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
599 return isl_space_copy(isl_map_peek_space(map));
602 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
604 if (!set)
605 return NULL;
606 return isl_space_copy(set->dim);
609 /* Return the space of "map".
610 * This may be either a copy or the space itself
611 * if there is only one reference to "map".
612 * This allows the space to be modified inplace
613 * if both the map and its space have only a single reference.
614 * The caller is not allowed to modify "map" between this call and
615 * a subsequent call to isl_map_restore_space.
616 * The only exception is that isl_map_free can be called instead.
618 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
620 isl_space *space;
622 if (!map)
623 return NULL;
624 if (map->ref != 1)
625 return isl_map_get_space(map);
626 space = map->dim;
627 map->dim = NULL;
628 return space;
631 /* Set the space of "map" to "space", where the space of "map" may be missing
632 * due to a preceding call to isl_map_take_space.
633 * However, in this case, "map" only has a single reference and
634 * then the call to isl_map_cow has no effect.
636 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
637 __isl_take isl_space *space)
639 if (!map || !space)
640 goto error;
642 if (map->dim == space) {
643 isl_space_free(space);
644 return map;
647 map = isl_map_cow(map);
648 if (!map)
649 goto error;
650 isl_space_free(map->dim);
651 map->dim = space;
653 return map;
654 error:
655 isl_map_free(map);
656 isl_space_free(space);
657 return NULL;
660 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
661 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
663 isl_space *space;
665 space = isl_basic_map_take_space(bmap);
666 space = isl_space_set_tuple_name(space, type, s);
667 bmap = isl_basic_map_restore_space(bmap, space);
668 bmap = isl_basic_map_finalize(bmap);
669 return bmap;
672 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
673 __isl_take isl_basic_set *bset, const char *s)
675 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
678 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
679 enum isl_dim_type type)
681 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
684 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
685 enum isl_dim_type type, const char *s)
687 int i;
688 isl_space *space;
690 map = isl_map_cow(map);
691 if (!map)
692 return NULL;
694 for (i = 0; i < map->n; ++i) {
695 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
696 if (!map->p[i])
697 goto error;
700 space = isl_map_take_space(map);
701 space = isl_space_set_tuple_name(space, type, s);
702 map = isl_map_restore_space(map, space);
704 return map;
705 error:
706 isl_map_free(map);
707 return NULL;
710 /* Replace the identifier of the tuple of type "type" by "id".
712 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
713 __isl_take isl_basic_map *bmap,
714 enum isl_dim_type type, __isl_take isl_id *id)
716 isl_space *space;
718 space = isl_basic_map_take_space(bmap);
719 space = isl_space_set_tuple_id(space, type, id);
720 bmap = isl_basic_map_restore_space(bmap, space);
721 bmap = isl_basic_map_finalize(bmap);
722 return bmap;
725 /* Replace the identifier of the tuple by "id".
727 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
728 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
730 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
733 /* Does the input or output tuple have a name?
735 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
737 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
740 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
741 enum isl_dim_type type)
743 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
746 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
747 const char *s)
749 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
750 isl_dim_set, s));
753 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
754 enum isl_dim_type type, __isl_take isl_id *id)
756 isl_space *space;
758 space = isl_map_take_space(map);
759 space = isl_space_set_tuple_id(space, type, id);
760 map = isl_map_restore_space(map, space);
762 return isl_map_reset_space(map, isl_map_get_space(map));
765 /* Replace the identifier of the domain tuple of "map" by "id".
767 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
768 __isl_take isl_id *id)
770 return isl_map_set_tuple_id(map, isl_dim_in, id);
773 /* Replace the identifier of the range tuple of "map" by "id".
775 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
776 __isl_take isl_id *id)
778 return isl_map_set_tuple_id(map, isl_dim_out, id);
781 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
782 __isl_take isl_id *id)
784 return isl_map_set_tuple_id(set, isl_dim_set, id);
787 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
788 enum isl_dim_type type)
790 isl_space *space;
792 space = isl_map_take_space(map);
793 space = isl_space_reset_tuple_id(space, type);
794 map = isl_map_restore_space(map, space);
796 return isl_map_reset_space(map, isl_map_get_space(map));
799 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
801 return isl_map_reset_tuple_id(set, isl_dim_set);
804 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
806 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
809 /* Does the domain tuple of "map" have an identifier?
811 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
813 return isl_map_has_tuple_id(map, isl_dim_in);
816 /* Does the range tuple of "map" have an identifier?
818 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
820 return isl_map_has_tuple_id(map, isl_dim_out);
823 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
824 enum isl_dim_type type)
826 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
829 /* Return the identifier of the domain tuple of "map", assuming it has one.
831 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
833 return isl_map_get_tuple_id(map, isl_dim_in);
836 /* Return the identifier of the range tuple of "map", assuming it has one.
838 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
840 return isl_map_get_tuple_id(map, isl_dim_out);
843 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
845 return isl_map_has_tuple_id(set, isl_dim_set);
848 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
850 return isl_map_get_tuple_id(set, isl_dim_set);
853 /* Does the set tuple have a name?
855 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
857 if (!set)
858 return isl_bool_error;
859 return isl_space_has_tuple_name(set->dim, isl_dim_set);
863 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
865 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
868 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
870 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
873 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
874 enum isl_dim_type type, unsigned pos)
876 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
879 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
880 enum isl_dim_type type, unsigned pos)
882 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
885 /* Does the given dimension have a name?
887 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
888 enum isl_dim_type type, unsigned pos)
890 if (!map)
891 return isl_bool_error;
892 return isl_space_has_dim_name(map->dim, type, pos);
895 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
896 enum isl_dim_type type, unsigned pos)
898 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
901 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
902 enum isl_dim_type type, unsigned pos)
904 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
907 /* Does the given dimension have a name?
909 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
910 enum isl_dim_type type, unsigned pos)
912 if (!set)
913 return isl_bool_error;
914 return isl_space_has_dim_name(set->dim, type, pos);
917 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
918 __isl_take isl_basic_map *bmap,
919 enum isl_dim_type type, unsigned pos, const char *s)
921 isl_space *space;
923 space = isl_basic_map_take_space(bmap);
924 space = isl_space_set_dim_name(space, type, pos, s);
925 bmap = isl_basic_map_restore_space(bmap, space);
926 return isl_basic_map_finalize(bmap);
929 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
930 enum isl_dim_type type, unsigned pos, const char *s)
932 int i;
933 isl_space *space;
935 map = isl_map_cow(map);
936 if (!map)
937 return NULL;
939 for (i = 0; i < map->n; ++i) {
940 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
941 if (!map->p[i])
942 goto error;
945 space = isl_map_take_space(map);
946 space = isl_space_set_dim_name(space, type, pos, s);
947 map = isl_map_restore_space(map, space);
949 return map;
950 error:
951 isl_map_free(map);
952 return NULL;
955 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
956 __isl_take isl_basic_set *bset,
957 enum isl_dim_type type, unsigned pos, const char *s)
959 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
960 type, pos, s));
963 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
964 enum isl_dim_type type, unsigned pos, const char *s)
966 return set_from_map(isl_map_set_dim_name(set_to_map(set),
967 type, pos, s));
970 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
971 enum isl_dim_type type, unsigned pos)
973 if (!bmap)
974 return isl_bool_error;
975 return isl_space_has_dim_id(bmap->dim, type, pos);
978 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
979 enum isl_dim_type type, unsigned pos)
981 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
984 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
985 enum isl_dim_type type, unsigned pos)
987 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
990 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
991 enum isl_dim_type type, unsigned pos)
993 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
996 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
997 enum isl_dim_type type, unsigned pos)
999 return isl_map_has_dim_id(set, type, pos);
1002 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1003 enum isl_dim_type type, unsigned pos)
1005 return isl_map_get_dim_id(set, type, pos);
1008 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1009 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1011 isl_space *space;
1013 space = isl_map_take_space(map);
1014 space = isl_space_set_dim_id(space, type, pos, id);
1015 map = isl_map_restore_space(map, space);
1017 return isl_map_reset_space(map, isl_map_get_space(map));
1020 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1021 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1023 return isl_map_set_dim_id(set, type, pos, id);
1026 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1027 __isl_keep isl_id *id)
1029 if (!map)
1030 return -1;
1031 return isl_space_find_dim_by_id(map->dim, type, id);
1034 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1035 __isl_keep isl_id *id)
1037 return isl_map_find_dim_by_id(set, type, id);
1040 /* Return the position of the dimension of the given type and name
1041 * in "bmap".
1042 * Return -1 if no such dimension can be found.
1044 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1045 enum isl_dim_type type, const char *name)
1047 if (!bmap)
1048 return -1;
1049 return isl_space_find_dim_by_name(bmap->dim, type, name);
1052 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1053 const char *name)
1055 if (!map)
1056 return -1;
1057 return isl_space_find_dim_by_name(map->dim, type, name);
1060 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1061 const char *name)
1063 return isl_map_find_dim_by_name(set, type, name);
1066 /* Check whether equality i of bset is a pure stride constraint
1067 * on a single dimension, i.e., of the form
1069 * v = k e
1071 * with k a constant and e an existentially quantified variable.
1073 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1075 isl_size nparam;
1076 isl_size d;
1077 isl_size n_div;
1078 int pos1;
1079 int pos2;
1081 nparam = isl_basic_set_dim(bset, isl_dim_param);
1082 d = isl_basic_set_dim(bset, isl_dim_set);
1083 n_div = isl_basic_set_dim(bset, isl_dim_div);
1084 if (nparam < 0 || d < 0 || n_div < 0)
1085 return isl_bool_error;
1087 if (!isl_int_is_zero(bset->eq[i][0]))
1088 return isl_bool_false;
1090 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1091 return isl_bool_false;
1092 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1093 if (pos1 == -1)
1094 return isl_bool_false;
1095 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1096 d - pos1 - 1) != -1)
1097 return isl_bool_false;
1099 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1100 if (pos2 == -1)
1101 return isl_bool_false;
1102 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1103 n_div - pos2 - 1) != -1)
1104 return isl_bool_false;
1105 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1106 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1107 return isl_bool_false;
1109 return isl_bool_true;
1112 /* Reset the user pointer on all identifiers of parameters and tuples
1113 * of the space of "map".
1115 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1117 isl_space *space;
1119 space = isl_map_get_space(map);
1120 space = isl_space_reset_user(space);
1121 map = isl_map_reset_space(map, space);
1123 return map;
1126 /* Reset the user pointer on all identifiers of parameters and tuples
1127 * of the space of "set".
1129 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1131 return isl_map_reset_user(set);
1134 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1136 if (!bmap)
1137 return isl_bool_error;
1138 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1141 /* Has "map" been marked as a rational map?
1142 * In particular, have all basic maps in "map" been marked this way?
1143 * An empty map is not considered to be rational.
1144 * Maps where only some of the basic maps are marked rational
1145 * are not allowed.
1147 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1149 int i;
1150 isl_bool rational;
1152 if (!map)
1153 return isl_bool_error;
1154 if (map->n == 0)
1155 return isl_bool_false;
1156 rational = isl_basic_map_is_rational(map->p[0]);
1157 if (rational < 0)
1158 return rational;
1159 for (i = 1; i < map->n; ++i) {
1160 isl_bool rational_i;
1162 rational_i = isl_basic_map_is_rational(map->p[i]);
1163 if (rational_i < 0)
1164 return rational_i;
1165 if (rational != rational_i)
1166 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1167 "mixed rational and integer basic maps "
1168 "not supported", return isl_bool_error);
1171 return rational;
1174 /* Has "set" been marked as a rational set?
1175 * In particular, have all basic set in "set" been marked this way?
1176 * An empty set is not considered to be rational.
1177 * Sets where only some of the basic sets are marked rational
1178 * are not allowed.
1180 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1182 return isl_map_is_rational(set);
1185 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1187 return isl_basic_map_is_rational(bset);
1190 /* Does "bmap" contain any rational points?
1192 * If "bmap" has an equality for each dimension, equating the dimension
1193 * to an integer constant, then it has no rational points, even if it
1194 * is marked as rational.
1196 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1198 isl_bool has_rational = isl_bool_true;
1199 isl_size total;
1201 if (!bmap)
1202 return isl_bool_error;
1203 if (isl_basic_map_plain_is_empty(bmap))
1204 return isl_bool_false;
1205 if (!isl_basic_map_is_rational(bmap))
1206 return isl_bool_false;
1207 bmap = isl_basic_map_copy(bmap);
1208 bmap = isl_basic_map_implicit_equalities(bmap);
1209 total = isl_basic_map_dim(bmap, isl_dim_all);
1210 if (total < 0)
1211 return isl_bool_error;
1212 if (bmap->n_eq == total) {
1213 int i, j;
1214 for (i = 0; i < bmap->n_eq; ++i) {
1215 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1216 if (j < 0)
1217 break;
1218 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1219 !isl_int_is_negone(bmap->eq[i][1 + j]))
1220 break;
1221 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1222 total - j - 1);
1223 if (j >= 0)
1224 break;
1226 if (i == bmap->n_eq)
1227 has_rational = isl_bool_false;
1229 isl_basic_map_free(bmap);
1231 return has_rational;
1234 /* Does "map" contain any rational points?
1236 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1238 int i;
1239 isl_bool has_rational;
1241 if (!map)
1242 return isl_bool_error;
1243 for (i = 0; i < map->n; ++i) {
1244 has_rational = isl_basic_map_has_rational(map->p[i]);
1245 if (has_rational < 0 || has_rational)
1246 return has_rational;
1248 return isl_bool_false;
1251 /* Does "set" contain any rational points?
1253 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1255 return isl_map_has_rational(set);
1258 /* Is this basic set a parameter domain?
1260 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1262 if (!bset)
1263 return isl_bool_error;
1264 return isl_space_is_params(bset->dim);
1267 /* Is this set a parameter domain?
1269 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1271 if (!set)
1272 return isl_bool_error;
1273 return isl_space_is_params(set->dim);
1276 /* Is this map actually a parameter domain?
1277 * Users should never call this function. Outside of isl,
1278 * a map can never be a parameter domain.
1280 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1282 if (!map)
1283 return isl_bool_error;
1284 return isl_space_is_params(map->dim);
1287 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1288 __isl_take isl_basic_map *bmap, unsigned extra,
1289 unsigned n_eq, unsigned n_ineq)
1291 int i;
1292 isl_space *space = isl_basic_map_peek_space(bmap);
1293 isl_size n_var = isl_space_dim(space, isl_dim_all);
1294 size_t row_size = 1 + n_var + extra;
1296 bmap->ctx = ctx;
1297 isl_ctx_ref(ctx);
1299 if (n_var < 0)
1300 return isl_basic_map_free(bmap);
1302 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1303 if (isl_blk_is_error(bmap->block))
1304 goto error;
1306 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1307 if ((n_ineq + n_eq) && !bmap->ineq)
1308 goto error;
1310 if (extra == 0) {
1311 bmap->block2 = isl_blk_empty();
1312 bmap->div = NULL;
1313 } else {
1314 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1315 if (isl_blk_is_error(bmap->block2))
1316 goto error;
1318 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1319 if (!bmap->div)
1320 goto error;
1323 for (i = 0; i < n_ineq + n_eq; ++i)
1324 bmap->ineq[i] = bmap->block.data + i * row_size;
1326 for (i = 0; i < extra; ++i)
1327 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1329 bmap->ref = 1;
1330 bmap->flags = 0;
1331 bmap->c_size = n_eq + n_ineq;
1332 bmap->eq = bmap->ineq + n_ineq;
1333 bmap->extra = extra;
1334 bmap->n_eq = 0;
1335 bmap->n_ineq = 0;
1336 bmap->n_div = 0;
1337 bmap->sample = NULL;
1339 return bmap;
1340 error:
1341 isl_basic_map_free(bmap);
1342 return NULL;
1345 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1346 unsigned nparam, unsigned dim, unsigned extra,
1347 unsigned n_eq, unsigned n_ineq)
1349 struct isl_basic_map *bmap;
1350 isl_space *space;
1352 space = isl_space_set_alloc(ctx, nparam, dim);
1353 if (!space)
1354 return NULL;
1356 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1357 return bset_from_bmap(bmap);
1360 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1361 unsigned extra, unsigned n_eq, unsigned n_ineq)
1363 struct isl_basic_map *bmap;
1364 if (!space)
1365 return NULL;
1366 isl_assert(space->ctx, space->n_in == 0, goto error);
1367 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1368 return bset_from_bmap(bmap);
1369 error:
1370 isl_space_free(space);
1371 return NULL;
1374 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1375 unsigned extra, unsigned n_eq, unsigned n_ineq)
1377 struct isl_basic_map *bmap;
1379 if (!space)
1380 return NULL;
1381 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1382 if (!bmap)
1383 goto error;
1384 bmap->dim = space;
1386 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1387 error:
1388 isl_space_free(space);
1389 return NULL;
1392 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1393 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1394 unsigned n_eq, unsigned n_ineq)
1396 struct isl_basic_map *bmap;
1397 isl_space *space;
1399 space = isl_space_alloc(ctx, nparam, in, out);
1400 if (!space)
1401 return NULL;
1403 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1404 return bmap;
1407 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1408 __isl_keep isl_basic_map *src)
1410 int i;
1411 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1413 if (!dst || total < 0)
1414 return isl_basic_map_free(dst);
1416 for (i = 0; i < src->n_eq; ++i) {
1417 int j = isl_basic_map_alloc_equality(dst);
1418 if (j < 0)
1419 return isl_basic_map_free(dst);
1420 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1423 for (i = 0; i < src->n_ineq; ++i) {
1424 int j = isl_basic_map_alloc_inequality(dst);
1425 if (j < 0)
1426 return isl_basic_map_free(dst);
1427 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1430 for (i = 0; i < src->n_div; ++i) {
1431 int j = isl_basic_map_alloc_div(dst);
1432 if (j < 0)
1433 return isl_basic_map_free(dst);
1434 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1436 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1437 return dst;
1440 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1442 struct isl_basic_map *dup;
1444 if (!bmap)
1445 return NULL;
1446 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1447 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1448 dup = dup_constraints(dup, bmap);
1449 if (!dup)
1450 return NULL;
1451 dup->flags = bmap->flags;
1452 dup->sample = isl_vec_copy(bmap->sample);
1453 return dup;
1456 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1458 struct isl_basic_map *dup;
1460 dup = isl_basic_map_dup(bset_to_bmap(bset));
1461 return bset_from_bmap(dup);
1464 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1466 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1469 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1471 if (!set)
1472 return NULL;
1474 set->ref++;
1475 return set;
1478 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1480 if (!bmap)
1481 return NULL;
1483 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1484 bmap->ref++;
1485 return bmap;
1487 bmap = isl_basic_map_dup(bmap);
1488 if (bmap)
1489 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1490 return bmap;
1493 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1495 if (!map)
1496 return NULL;
1498 map->ref++;
1499 return map;
1502 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1504 if (!bmap)
1505 return NULL;
1507 if (--bmap->ref > 0)
1508 return NULL;
1510 isl_ctx_deref(bmap->ctx);
1511 free(bmap->div);
1512 isl_blk_free(bmap->ctx, bmap->block2);
1513 free(bmap->ineq);
1514 isl_blk_free(bmap->ctx, bmap->block);
1515 isl_vec_free(bmap->sample);
1516 isl_space_free(bmap->dim);
1517 free(bmap);
1519 return NULL;
1522 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1524 return isl_basic_map_free(bset_to_bmap(bset));
1527 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1529 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1532 /* Check that "bset" does not involve any parameters.
1534 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1536 isl_size nparam;
1538 nparam = isl_basic_set_dim(bset, isl_dim_param);
1539 if (nparam < 0)
1540 return isl_stat_error;
1541 if (nparam != 0)
1542 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1543 "basic set should not have any parameters",
1544 return isl_stat_error);
1545 return isl_stat_ok;
1548 /* Check that "bset" does not involve any local variables.
1550 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1552 isl_size n_div;
1554 n_div = isl_basic_set_dim(bset, isl_dim_div);
1555 if (n_div < 0)
1556 return isl_stat_error;
1557 if (n_div != 0)
1558 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1559 "basic set should not have any local variables",
1560 return isl_stat_error);
1561 return isl_stat_ok;
1564 #undef TYPE
1565 #define TYPE isl_map
1567 #include "isl_check_named_params_templ.c"
1569 #undef TYPE
1570 #define TYPE isl_basic_map
1572 static
1573 #include "isl_check_named_params_templ.c"
1575 /* Check that "bmap1" and "bmap2" have the same parameters,
1576 * reporting an error if they do not.
1578 static isl_stat isl_basic_map_check_equal_params(
1579 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1581 isl_bool match;
1583 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1584 if (match < 0)
1585 return isl_stat_error;
1586 if (!match)
1587 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1588 "parameters don't match", return isl_stat_error);
1589 return isl_stat_ok;
1592 #undef TYPE
1593 #define TYPE isl_map
1595 #include "isl_align_params_bin_templ.c"
1597 #undef SUFFIX
1598 #define SUFFIX set
1599 #undef ARG1
1600 #define ARG1 isl_map
1601 #undef ARG2
1602 #define ARG2 isl_set
1604 #include "isl_align_params_templ.c"
1606 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1607 __isl_keep isl_map *map2,
1608 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1610 isl_bool r;
1612 if (!map1 || !map2)
1613 return isl_bool_error;
1614 if (isl_map_has_equal_params(map1, map2))
1615 return fn(map1, map2);
1616 if (isl_map_check_named_params(map1) < 0)
1617 return isl_bool_error;
1618 if (isl_map_check_named_params(map2) < 0)
1619 return isl_bool_error;
1620 map1 = isl_map_copy(map1);
1621 map2 = isl_map_copy(map2);
1622 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1623 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1624 r = fn(map1, map2);
1625 isl_map_free(map1);
1626 isl_map_free(map2);
1627 return r;
1630 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1632 isl_size total;
1633 struct isl_ctx *ctx;
1635 total = isl_basic_map_dim(bmap, isl_dim_all);
1636 if (total < 0)
1637 return -1;
1638 ctx = bmap->ctx;
1639 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1640 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1641 return -1);
1642 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1646 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1647 isl_int *t;
1648 int j = isl_basic_map_alloc_inequality(bmap);
1649 if (j < 0)
1650 return -1;
1651 t = bmap->ineq[j];
1652 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1653 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1654 bmap->eq[-1] = t;
1655 bmap->n_eq++;
1656 bmap->n_ineq--;
1657 bmap->eq--;
1658 return 0;
1660 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1661 bmap->extra - bmap->n_div);
1662 return bmap->n_eq++;
1665 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1667 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1670 __isl_give isl_basic_map *isl_basic_map_free_equality(
1671 __isl_take isl_basic_map *bmap, unsigned n)
1673 if (!bmap)
1674 return NULL;
1675 if (n > bmap->n_eq)
1676 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1677 "invalid number of equalities",
1678 isl_basic_map_free(bmap));
1679 bmap->n_eq -= n;
1680 return bmap;
1683 __isl_give isl_basic_set *isl_basic_set_free_equality(
1684 __isl_take isl_basic_set *bset, unsigned n)
1686 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1687 n));
1690 /* Drop the equality constraint at position "pos",
1691 * preserving the order of the other equality constraints.
1693 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1695 isl_int *t;
1696 int r;
1698 if (!bmap)
1699 return -1;
1700 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1702 t = bmap->eq[pos];
1703 bmap->n_eq--;
1704 for (r = pos; r < bmap->n_eq; ++r)
1705 bmap->eq[r] = bmap->eq[r + 1];
1706 bmap->eq[bmap->n_eq] = t;
1708 return 0;
1711 /* Turn inequality "pos" of "bmap" into an equality.
1713 * In particular, we move the inequality in front of the equalities
1714 * and move the last inequality in the position of the moved inequality.
1715 * Note that isl_tab_make_equalities_explicit depends on this particular
1716 * change in the ordering of the constraints.
1718 void isl_basic_map_inequality_to_equality(
1719 __isl_keep isl_basic_map *bmap, unsigned pos)
1721 isl_int *t;
1723 t = bmap->ineq[pos];
1724 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1725 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1726 bmap->eq[-1] = t;
1727 bmap->n_eq++;
1728 bmap->n_ineq--;
1729 bmap->eq--;
1730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1731 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1736 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1738 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1741 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1743 isl_size total;
1744 struct isl_ctx *ctx;
1746 total = isl_basic_map_dim(bmap, isl_dim_all);
1747 if (total < 0)
1748 return -1;
1749 ctx = bmap->ctx;
1750 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1751 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1755 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1756 bmap->extra - bmap->n_div);
1757 return bmap->n_ineq++;
1760 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1762 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1765 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1766 __isl_take isl_basic_map *bmap, unsigned n)
1768 if (!bmap)
1769 return NULL;
1770 if (n > bmap->n_ineq)
1771 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1772 "invalid number of inequalities",
1773 return isl_basic_map_free(bmap));
1774 bmap->n_ineq -= n;
1775 return bmap;
1778 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1779 __isl_take isl_basic_set *bset, unsigned n)
1781 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1782 n));
1785 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1787 isl_int *t;
1788 if (!bmap)
1789 return -1;
1790 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1792 if (pos != bmap->n_ineq - 1) {
1793 t = bmap->ineq[pos];
1794 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1795 bmap->ineq[bmap->n_ineq - 1] = t;
1796 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1798 bmap->n_ineq--;
1799 return 0;
1802 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1804 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1807 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1808 isl_int *eq)
1810 isl_bool empty;
1811 isl_size total;
1812 int k;
1814 empty = isl_basic_map_plain_is_empty(bmap);
1815 if (empty < 0)
1816 return isl_basic_map_free(bmap);
1817 if (empty)
1818 return bmap;
1820 bmap = isl_basic_map_cow(bmap);
1821 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1822 total = isl_basic_map_dim(bmap, isl_dim_all);
1823 if (total < 0)
1824 return isl_basic_map_free(bmap);
1825 k = isl_basic_map_alloc_equality(bmap);
1826 if (k < 0)
1827 goto error;
1828 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1829 return bmap;
1830 error:
1831 isl_basic_map_free(bmap);
1832 return NULL;
1835 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1836 isl_int *eq)
1838 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1841 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1842 isl_int *ineq)
1844 isl_size total;
1845 int k;
1847 bmap = isl_basic_map_cow(bmap);
1848 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1849 total = isl_basic_map_dim(bmap, isl_dim_all);
1850 if (total < 0)
1851 return isl_basic_map_free(bmap);
1852 k = isl_basic_map_alloc_inequality(bmap);
1853 if (k < 0)
1854 goto error;
1855 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1856 return bmap;
1857 error:
1858 isl_basic_map_free(bmap);
1859 return NULL;
1862 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1863 isl_int *ineq)
1865 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1868 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1870 isl_size total;
1872 total = isl_basic_map_dim(bmap, isl_dim_all);
1873 if (total < 0)
1874 return -1;
1875 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1876 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1877 bmap->extra - bmap->n_div);
1878 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1879 return bmap->n_div++;
1882 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1884 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1887 #undef TYPE
1888 #define TYPE isl_basic_map
1889 #include "check_type_range_templ.c"
1891 /* Check that there are "n" dimensions of type "type" starting at "first"
1892 * in "bset".
1894 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1895 enum isl_dim_type type, unsigned first, unsigned n)
1897 return isl_basic_map_check_range(bset_to_bmap(bset),
1898 type, first, n);
1901 /* Insert an extra integer division, prescribed by "div", to "bmap"
1902 * at (integer division) position "pos".
1904 * The integer division is first added at the end and then moved
1905 * into the right position.
1907 __isl_give isl_basic_map *isl_basic_map_insert_div(
1908 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1910 int i, k;
1911 isl_size total;
1913 bmap = isl_basic_map_cow(bmap);
1914 total = isl_basic_map_dim(bmap, isl_dim_all);
1915 if (total < 0 || !div)
1916 return isl_basic_map_free(bmap);
1918 if (div->size != 1 + 1 + total)
1919 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1920 "unexpected size", return isl_basic_map_free(bmap));
1921 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1922 return isl_basic_map_free(bmap);
1924 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1925 k = isl_basic_map_alloc_div(bmap);
1926 if (k < 0)
1927 return isl_basic_map_free(bmap);
1928 isl_seq_cpy(bmap->div[k], div->el, div->size);
1929 isl_int_set_si(bmap->div[k][div->size], 0);
1931 for (i = k; i > pos; --i)
1932 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1934 return bmap;
1937 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1939 if (!bmap)
1940 return isl_stat_error;
1941 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1942 bmap->n_div -= n;
1943 return isl_stat_ok;
1946 static __isl_give isl_basic_map *add_constraints(
1947 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1948 unsigned i_pos, unsigned o_pos)
1950 isl_size total, n_param, n_in, n_out, n_div;
1951 unsigned o_in, o_out;
1952 isl_ctx *ctx;
1953 isl_space *space;
1954 struct isl_dim_map *dim_map;
1956 space = isl_basic_map_peek_space(bmap2);
1957 if (!bmap1 || !space)
1958 goto error;
1960 total = isl_basic_map_dim(bmap1, isl_dim_all);
1961 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1962 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1963 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1964 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1965 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1966 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1967 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1968 goto error;
1969 ctx = isl_basic_map_get_ctx(bmap1);
1970 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1971 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1972 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1974 isl_dim_map_div(dim_map, bmap2, total);
1976 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1977 error:
1978 isl_basic_map_free(bmap1);
1979 isl_basic_map_free(bmap2);
1980 return NULL;
1983 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1984 unsigned extra, unsigned n_eq, unsigned n_ineq)
1986 isl_space *space;
1987 struct isl_basic_map *ext;
1988 unsigned flags;
1989 int dims_ok;
1991 if (!base)
1992 goto error;
1994 dims_ok = base->extra >= base->n_div + extra;
1996 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1997 room_for_ineq(base, n_ineq))
1998 return base;
2000 extra += base->extra;
2001 n_eq += base->n_eq;
2002 n_ineq += base->n_ineq;
2004 space = isl_basic_map_get_space(base);
2005 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2006 if (!ext)
2007 goto error;
2009 if (dims_ok)
2010 ext->sample = isl_vec_copy(base->sample);
2011 flags = base->flags;
2012 ext = add_constraints(ext, base, 0, 0);
2013 if (ext) {
2014 ext->flags = flags;
2015 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2018 return ext;
2020 error:
2021 isl_basic_map_free(base);
2022 return NULL;
2025 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2026 unsigned extra, unsigned n_eq, unsigned n_ineq)
2028 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2029 extra, n_eq, n_ineq));
2032 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2033 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2035 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2038 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2039 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2041 isl_basic_map *bmap = bset_to_bmap(base);
2042 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2043 return bset_from_bmap(bmap);
2046 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2048 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2051 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2053 if (!bmap)
2054 return NULL;
2056 if (bmap->ref > 1) {
2057 bmap->ref--;
2058 bmap = isl_basic_map_dup(bmap);
2060 if (bmap) {
2061 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2062 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2064 return bmap;
2067 /* Clear all cached information in "map", either because it is about
2068 * to be modified or because it is being freed.
2069 * Always return the same pointer that is passed in.
2070 * This is needed for the use in isl_map_free.
2072 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2074 isl_basic_map_free(map->cached_simple_hull[0]);
2075 isl_basic_map_free(map->cached_simple_hull[1]);
2076 map->cached_simple_hull[0] = NULL;
2077 map->cached_simple_hull[1] = NULL;
2078 return map;
2081 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2083 return isl_map_cow(set);
2086 /* Return an isl_map that is equal to "map" and that has only
2087 * a single reference.
2089 * If the original input already has only one reference, then
2090 * simply return it, but clear all cached information, since
2091 * it may be rendered invalid by the operations that will be
2092 * performed on the result.
2094 * Otherwise, create a duplicate (without any cached information).
2096 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2098 if (!map)
2099 return NULL;
2101 if (map->ref == 1)
2102 return clear_caches(map);
2103 map->ref--;
2104 return isl_map_dup(map);
2107 static void swap_vars(struct isl_blk blk, isl_int *a,
2108 unsigned a_len, unsigned b_len)
2110 isl_seq_cpy(blk.data, a+a_len, b_len);
2111 isl_seq_cpy(blk.data+b_len, a, a_len);
2112 isl_seq_cpy(a, blk.data, b_len+a_len);
2115 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2116 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2118 int i;
2119 struct isl_blk blk;
2121 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2122 goto error;
2124 if (n1 == 0 || n2 == 0)
2125 return bmap;
2127 bmap = isl_basic_map_cow(bmap);
2128 if (!bmap)
2129 return NULL;
2131 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2132 if (isl_blk_is_error(blk))
2133 goto error;
2135 for (i = 0; i < bmap->n_eq; ++i)
2136 swap_vars(blk,
2137 bmap->eq[i] + pos, n1, n2);
2139 for (i = 0; i < bmap->n_ineq; ++i)
2140 swap_vars(blk,
2141 bmap->ineq[i] + pos, n1, n2);
2143 for (i = 0; i < bmap->n_div; ++i)
2144 swap_vars(blk,
2145 bmap->div[i]+1 + pos, n1, n2);
2147 isl_blk_free(bmap->ctx, blk);
2149 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2150 bmap = isl_basic_map_gauss(bmap, NULL);
2151 return isl_basic_map_finalize(bmap);
2152 error:
2153 isl_basic_map_free(bmap);
2154 return NULL;
2157 /* The given basic map has turned out to be empty.
2158 * Explicitly mark it as such and change the representation
2159 * to a canonical representation of the empty basic map.
2160 * Since the basic map has conflicting constraints,
2161 * it must have at least one constraint, except perhaps
2162 * if it was already explicitly marked as being empty.
2163 * Do nothing in the latter case, i.e., if it has been marked empty and
2164 * has no constraints.
2166 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2167 __isl_take isl_basic_map *bmap)
2169 int i = 0;
2170 isl_bool empty;
2171 isl_size n;
2172 isl_size total;
2174 n = isl_basic_map_n_constraint(bmap);
2175 empty = isl_basic_map_plain_is_empty(bmap);
2176 if (n < 0 || empty < 0)
2177 return isl_basic_map_free(bmap);
2178 if (n == 0 && empty)
2179 return bmap;
2180 total = isl_basic_map_dim(bmap, isl_dim_all);
2181 if (total < 0)
2182 return isl_basic_map_free(bmap);
2183 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2184 return isl_basic_map_free(bmap);
2185 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2186 if (!bmap)
2187 return NULL;
2188 if (bmap->n_eq > 0) {
2189 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2190 if (!bmap)
2191 return NULL;
2192 } else {
2193 i = isl_basic_map_alloc_equality(bmap);
2194 if (i < 0)
2195 goto error;
2197 isl_int_set_si(bmap->eq[i][0], 1);
2198 isl_seq_clr(bmap->eq[i]+1, total);
2199 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2200 isl_vec_free(bmap->sample);
2201 bmap->sample = NULL;
2202 return isl_basic_map_finalize(bmap);
2203 error:
2204 isl_basic_map_free(bmap);
2205 return NULL;
2208 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2209 __isl_take isl_basic_set *bset)
2211 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2214 __isl_give isl_basic_map *isl_basic_map_set_rational(
2215 __isl_take isl_basic_map *bmap)
2217 if (!bmap)
2218 return NULL;
2220 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2221 return bmap;
2223 bmap = isl_basic_map_cow(bmap);
2224 if (!bmap)
2225 return NULL;
2227 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2229 return isl_basic_map_finalize(bmap);
2232 __isl_give isl_basic_set *isl_basic_set_set_rational(
2233 __isl_take isl_basic_set *bset)
2235 return isl_basic_map_set_rational(bset);
2238 __isl_give isl_basic_set *isl_basic_set_set_integral(
2239 __isl_take isl_basic_set *bset)
2241 if (!bset)
2242 return NULL;
2244 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2245 return bset;
2247 bset = isl_basic_set_cow(bset);
2248 if (!bset)
2249 return NULL;
2251 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2253 return isl_basic_set_finalize(bset);
2256 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2258 int i;
2260 map = isl_map_cow(map);
2261 if (!map)
2262 return NULL;
2263 for (i = 0; i < map->n; ++i) {
2264 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2265 if (!map->p[i])
2266 goto error;
2268 return map;
2269 error:
2270 isl_map_free(map);
2271 return NULL;
2274 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2276 return isl_map_set_rational(set);
2279 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2280 * of "bmap").
2282 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2284 isl_int *t = bmap->div[a];
2285 bmap->div[a] = bmap->div[b];
2286 bmap->div[b] = t;
2289 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2290 * div definitions accordingly.
2292 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2293 int a, int b)
2295 int i;
2296 isl_size off;
2298 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2299 if (off < 0)
2300 return isl_basic_map_free(bmap);
2302 swap_div(bmap, a, b);
2304 for (i = 0; i < bmap->n_eq; ++i)
2305 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2307 for (i = 0; i < bmap->n_ineq; ++i)
2308 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2310 for (i = 0; i < bmap->n_div; ++i)
2311 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2312 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2314 return bmap;
2317 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2319 isl_seq_cpy(c, c + n, rem);
2320 isl_seq_clr(c + rem, n);
2323 /* Drop n dimensions starting at first.
2325 * In principle, this frees up some extra variables as the number
2326 * of columns remains constant, but we would have to extend
2327 * the div array too as the number of rows in this array is assumed
2328 * to be equal to extra.
2330 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2331 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2333 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2336 /* Move "n" divs starting at "first" to the end of the list of divs.
2338 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2339 unsigned first, unsigned n)
2341 isl_int **div;
2342 int i;
2344 if (first + n == bmap->n_div)
2345 return bmap;
2347 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2348 if (!div)
2349 goto error;
2350 for (i = 0; i < n; ++i)
2351 div[i] = bmap->div[first + i];
2352 for (i = 0; i < bmap->n_div - first - n; ++i)
2353 bmap->div[first + i] = bmap->div[first + n + i];
2354 for (i = 0; i < n; ++i)
2355 bmap->div[bmap->n_div - n + i] = div[i];
2356 free(div);
2357 return bmap;
2358 error:
2359 isl_basic_map_free(bmap);
2360 return NULL;
2363 #undef TYPE
2364 #define TYPE isl_map
2365 static
2366 #include "check_type_range_templ.c"
2368 /* Check that there are "n" dimensions of type "type" starting at "first"
2369 * in "set".
2371 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2372 enum isl_dim_type type, unsigned first, unsigned n)
2374 return isl_map_check_range(set_to_map(set), type, first, n);
2377 /* Drop "n" dimensions of type "type" starting at "first".
2378 * Perform the core computation, without cowing or
2379 * simplifying and finalizing the result.
2381 * In principle, this frees up some extra variables as the number
2382 * of columns remains constant, but we would have to extend
2383 * the div array too as the number of rows in this array is assumed
2384 * to be equal to extra.
2386 __isl_give isl_basic_map *isl_basic_map_drop_core(
2387 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2388 unsigned first, unsigned n)
2390 int i;
2391 unsigned offset;
2392 unsigned left;
2393 isl_size total;
2395 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2396 return isl_basic_map_free(bmap);
2398 total = isl_basic_map_dim(bmap, isl_dim_all);
2399 if (total < 0)
2400 return isl_basic_map_free(bmap);
2402 offset = isl_basic_map_offset(bmap, type) + first;
2403 left = total - (offset - 1) - n;
2404 for (i = 0; i < bmap->n_eq; ++i)
2405 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2407 for (i = 0; i < bmap->n_ineq; ++i)
2408 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2410 for (i = 0; i < bmap->n_div; ++i)
2411 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2413 if (type == isl_dim_div) {
2414 bmap = move_divs_last(bmap, first, n);
2415 if (!bmap)
2416 return NULL;
2417 if (isl_basic_map_free_div(bmap, n) < 0)
2418 return isl_basic_map_free(bmap);
2419 } else
2420 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2421 if (!bmap->dim)
2422 return isl_basic_map_free(bmap);
2424 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2425 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2426 return bmap;
2429 /* Drop "n" dimensions of type "type" starting at "first".
2431 * In principle, this frees up some extra variables as the number
2432 * of columns remains constant, but we would have to extend
2433 * the div array too as the number of rows in this array is assumed
2434 * to be equal to extra.
2436 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2437 enum isl_dim_type type, unsigned first, unsigned n)
2439 if (!bmap)
2440 return NULL;
2441 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2442 return bmap;
2444 bmap = isl_basic_map_cow(bmap);
2445 if (!bmap)
2446 return NULL;
2448 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2450 bmap = isl_basic_map_simplify(bmap);
2451 return isl_basic_map_finalize(bmap);
2454 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2455 enum isl_dim_type type, unsigned first, unsigned n)
2457 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2458 type, first, n));
2461 /* No longer consider "map" to be normalized.
2463 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2465 if (!map)
2466 return NULL;
2467 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2468 return map;
2471 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 int i;
2475 isl_space *space;
2477 if (isl_map_check_range(map, type, first, n) < 0)
2478 return isl_map_free(map);
2480 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2481 return map;
2482 map = isl_map_cow(map);
2483 if (!map)
2484 goto error;
2486 for (i = 0; i < map->n; ++i) {
2487 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2488 if (!map->p[i])
2489 goto error;
2491 map = isl_map_unmark_normalized(map);
2493 space = isl_map_take_space(map);
2494 space = isl_space_drop_dims(space, type, first, n);
2495 map = isl_map_restore_space(map, space);
2497 return map;
2498 error:
2499 isl_map_free(map);
2500 return NULL;
2503 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2504 enum isl_dim_type type, unsigned first, unsigned n)
2506 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2509 /* Drop the integer division at position "div", which is assumed
2510 * not to appear in any of the constraints or
2511 * in any of the other integer divisions.
2513 * Since the integer division is redundant, there is no need to cow.
2515 __isl_give isl_basic_map *isl_basic_map_drop_div(
2516 __isl_take isl_basic_map *bmap, unsigned div)
2518 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2521 /* Eliminate the specified n dimensions starting at first from the
2522 * constraints, without removing the dimensions from the space.
2523 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2525 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2526 enum isl_dim_type type, unsigned first, unsigned n)
2528 int i;
2530 if (n == 0)
2531 return map;
2533 if (isl_map_check_range(map, type, first, n) < 0)
2534 return isl_map_free(map);
2536 map = isl_map_cow(map);
2537 if (!map)
2538 return NULL;
2540 for (i = 0; i < map->n; ++i) {
2541 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2542 if (!map->p[i])
2543 goto error;
2545 return map;
2546 error:
2547 isl_map_free(map);
2548 return NULL;
2551 /* Eliminate the specified n dimensions starting at first from the
2552 * constraints, without removing the dimensions from the space.
2553 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2555 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2556 enum isl_dim_type type, unsigned first, unsigned n)
2558 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2561 /* Eliminate the specified n dimensions starting at first from the
2562 * constraints, without removing the dimensions from the space.
2563 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2565 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2566 unsigned first, unsigned n)
2568 return isl_set_eliminate(set, isl_dim_set, first, n);
2571 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2572 __isl_take isl_basic_map *bmap)
2574 isl_size v_div;
2576 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2577 if (v_div < 0)
2578 return isl_basic_map_free(bmap);
2579 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2580 if (!bmap)
2581 return NULL;
2582 bmap->n_div = 0;
2583 return isl_basic_map_finalize(bmap);
2586 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2587 __isl_take isl_basic_set *bset)
2589 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2592 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2594 int i;
2596 if (!map)
2597 return NULL;
2598 if (map->n == 0)
2599 return map;
2601 map = isl_map_cow(map);
2602 if (!map)
2603 return NULL;
2605 for (i = 0; i < map->n; ++i) {
2606 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2607 if (!map->p[i])
2608 goto error;
2610 return map;
2611 error:
2612 isl_map_free(map);
2613 return NULL;
2616 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2618 return isl_map_remove_divs(set);
2621 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2622 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2623 unsigned first, unsigned n)
2625 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2626 return isl_basic_map_free(bmap);
2627 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2628 return bmap;
2629 bmap = isl_basic_map_eliminate_vars(bmap,
2630 isl_basic_map_offset(bmap, type) - 1 + first, n);
2631 if (!bmap)
2632 return bmap;
2633 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2634 return bmap;
2635 bmap = isl_basic_map_drop(bmap, type, first, n);
2636 return bmap;
2639 /* Return true if the definition of the given div (recursively) involves
2640 * any of the given variables.
2642 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2643 unsigned first, unsigned n)
2645 int i;
2646 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2648 if (isl_int_is_zero(bmap->div[div][0]))
2649 return isl_bool_false;
2650 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2651 return isl_bool_true;
2653 for (i = bmap->n_div - 1; i >= 0; --i) {
2654 isl_bool involves;
2656 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2657 continue;
2658 involves = div_involves_vars(bmap, i, first, n);
2659 if (involves < 0 || involves)
2660 return involves;
2663 return isl_bool_false;
2666 /* Try and add a lower and/or upper bound on "div" to "bmap"
2667 * based on inequality "i".
2668 * "total" is the total number of variables (excluding the divs).
2669 * "v" is a temporary object that can be used during the calculations.
2670 * If "lb" is set, then a lower bound should be constructed.
2671 * If "ub" is set, then an upper bound should be constructed.
2673 * The calling function has already checked that the inequality does not
2674 * reference "div", but we still need to check that the inequality is
2675 * of the right form. We'll consider the case where we want to construct
2676 * a lower bound. The construction of upper bounds is similar.
2678 * Let "div" be of the form
2680 * q = floor((a + f(x))/d)
2682 * We essentially check if constraint "i" is of the form
2684 * b + f(x) >= 0
2686 * so that we can use it to derive a lower bound on "div".
2687 * However, we allow a slightly more general form
2689 * b + g(x) >= 0
2691 * with the condition that the coefficients of g(x) - f(x) are all
2692 * divisible by d.
2693 * Rewriting this constraint as
2695 * 0 >= -b - g(x)
2697 * adding a + f(x) to both sides and dividing by d, we obtain
2699 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2701 * Taking the floor on both sides, we obtain
2703 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2705 * or
2707 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2709 * In the case of an upper bound, we construct the constraint
2711 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2714 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2715 __isl_take isl_basic_map *bmap, int div, int i,
2716 unsigned total, isl_int v, int lb, int ub)
2718 int j;
2720 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2721 if (lb) {
2722 isl_int_sub(v, bmap->ineq[i][1 + j],
2723 bmap->div[div][1 + 1 + j]);
2724 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2726 if (ub) {
2727 isl_int_add(v, bmap->ineq[i][1 + j],
2728 bmap->div[div][1 + 1 + j]);
2729 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2732 if (!lb && !ub)
2733 return bmap;
2735 bmap = isl_basic_map_cow(bmap);
2736 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2737 if (lb) {
2738 int k = isl_basic_map_alloc_inequality(bmap);
2739 if (k < 0)
2740 goto error;
2741 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2742 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2743 bmap->div[div][1 + j]);
2744 isl_int_cdiv_q(bmap->ineq[k][j],
2745 bmap->ineq[k][j], bmap->div[div][0]);
2747 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2749 if (ub) {
2750 int k = isl_basic_map_alloc_inequality(bmap);
2751 if (k < 0)
2752 goto error;
2753 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2754 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2755 bmap->div[div][1 + j]);
2756 isl_int_fdiv_q(bmap->ineq[k][j],
2757 bmap->ineq[k][j], bmap->div[div][0]);
2759 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2762 return bmap;
2763 error:
2764 isl_basic_map_free(bmap);
2765 return NULL;
2768 /* This function is called right before "div" is eliminated from "bmap"
2769 * using Fourier-Motzkin.
2770 * Look through the constraints of "bmap" for constraints on the argument
2771 * of the integer division and use them to construct constraints on the
2772 * integer division itself. These constraints can then be combined
2773 * during the Fourier-Motzkin elimination.
2774 * Note that it is only useful to introduce lower bounds on "div"
2775 * if "bmap" already contains upper bounds on "div" as the newly
2776 * introduce lower bounds can then be combined with the pre-existing
2777 * upper bounds. Similarly for upper bounds.
2778 * We therefore first check if "bmap" contains any lower and/or upper bounds
2779 * on "div".
2781 * It is interesting to note that the introduction of these constraints
2782 * can indeed lead to more accurate results, even when compared to
2783 * deriving constraints on the argument of "div" from constraints on "div".
2784 * Consider, for example, the set
2786 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2788 * The second constraint can be rewritten as
2790 * 2 * [(-i-2j+3)/4] + k >= 0
2792 * from which we can derive
2794 * -i - 2j + 3 >= -2k
2796 * or
2798 * i + 2j <= 3 + 2k
2800 * Combined with the first constraint, we obtain
2802 * -3 <= 3 + 2k or k >= -3
2804 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2805 * the first constraint, we obtain
2807 * [(i + 2j)/4] >= [-3/4] = -1
2809 * Combining this constraint with the second constraint, we obtain
2811 * k >= -2
2813 static __isl_give isl_basic_map *insert_bounds_on_div(
2814 __isl_take isl_basic_map *bmap, int div)
2816 int i;
2817 int check_lb, check_ub;
2818 isl_int v;
2819 isl_size v_div;
2821 if (!bmap)
2822 return NULL;
2824 if (isl_int_is_zero(bmap->div[div][0]))
2825 return bmap;
2827 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2828 if (v_div < 0)
2829 return isl_basic_map_free(bmap);
2831 check_lb = 0;
2832 check_ub = 0;
2833 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2834 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2835 if (s > 0)
2836 check_ub = 1;
2837 if (s < 0)
2838 check_lb = 1;
2841 if (!check_lb && !check_ub)
2842 return bmap;
2844 isl_int_init(v);
2846 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2847 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2848 continue;
2850 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2851 check_lb, check_ub);
2854 isl_int_clear(v);
2856 return bmap;
2859 /* Remove all divs (recursively) involving any of the given dimensions
2860 * in their definitions.
2862 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2863 __isl_take isl_basic_map *bmap,
2864 enum isl_dim_type type, unsigned first, unsigned n)
2866 int i;
2868 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2869 return isl_basic_map_free(bmap);
2870 first += isl_basic_map_offset(bmap, type);
2872 for (i = bmap->n_div - 1; i >= 0; --i) {
2873 isl_bool involves;
2875 involves = div_involves_vars(bmap, i, first, n);
2876 if (involves < 0)
2877 return isl_basic_map_free(bmap);
2878 if (!involves)
2879 continue;
2880 bmap = insert_bounds_on_div(bmap, i);
2881 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2882 if (!bmap)
2883 return NULL;
2884 i = bmap->n_div;
2887 return bmap;
2890 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2891 __isl_take isl_basic_set *bset,
2892 enum isl_dim_type type, unsigned first, unsigned n)
2894 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2897 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2898 enum isl_dim_type type, unsigned first, unsigned n)
2900 int i;
2902 if (!map)
2903 return NULL;
2904 if (map->n == 0)
2905 return map;
2907 map = isl_map_cow(map);
2908 if (!map)
2909 return NULL;
2911 for (i = 0; i < map->n; ++i) {
2912 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2913 type, first, n);
2914 if (!map->p[i])
2915 goto error;
2917 return map;
2918 error:
2919 isl_map_free(map);
2920 return NULL;
2923 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2924 enum isl_dim_type type, unsigned first, unsigned n)
2926 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2927 type, first, n));
2930 /* Does the description of "bmap" depend on the specified dimensions?
2931 * We also check whether the dimensions appear in any of the div definitions.
2932 * In principle there is no need for this check. If the dimensions appear
2933 * in a div definition, they also appear in the defining constraints of that
2934 * div.
2936 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2937 enum isl_dim_type type, unsigned first, unsigned n)
2939 int i;
2941 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2942 return isl_bool_error;
2944 first += isl_basic_map_offset(bmap, type);
2945 for (i = 0; i < bmap->n_eq; ++i)
2946 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2947 return isl_bool_true;
2948 for (i = 0; i < bmap->n_ineq; ++i)
2949 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2950 return isl_bool_true;
2951 for (i = 0; i < bmap->n_div; ++i) {
2952 if (isl_int_is_zero(bmap->div[i][0]))
2953 continue;
2954 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2955 return isl_bool_true;
2958 return isl_bool_false;
2961 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2962 enum isl_dim_type type, unsigned first, unsigned n)
2964 int i;
2966 if (isl_map_check_range(map, type, first, n) < 0)
2967 return isl_bool_error;
2969 for (i = 0; i < map->n; ++i) {
2970 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2971 type, first, n);
2972 if (involves < 0 || involves)
2973 return involves;
2976 return isl_bool_false;
2979 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2980 enum isl_dim_type type, unsigned first, unsigned n)
2982 return isl_basic_map_involves_dims(bset, type, first, n);
2985 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2986 enum isl_dim_type type, unsigned first, unsigned n)
2988 return isl_map_involves_dims(set, type, first, n);
2991 /* Does "bset" involve any local variables, i.e., integer divisions?
2993 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2995 isl_size n;
2997 n = isl_basic_set_dim(bset, isl_dim_div);
2998 if (n < 0)
2999 return isl_bool_error;
3000 return isl_bool_ok(n > 0);
3003 /* isl_set_every_basic_set callback that checks whether "bset"
3004 * is free of local variables.
3006 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3008 return isl_bool_not(isl_basic_set_involves_locals(bset));
3011 /* Does "set" involve any local variables, i.e., integer divisions?
3013 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3015 isl_bool no_locals;
3017 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3018 return isl_bool_not(no_locals);
3021 /* Drop all constraints in bmap that involve any of the dimensions
3022 * first to first+n-1.
3023 * This function only performs the actual removal of constraints.
3025 * This function should not call finalize since it is used by
3026 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3028 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3029 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3031 int i;
3033 if (n == 0)
3034 return bmap;
3036 bmap = isl_basic_map_cow(bmap);
3038 if (!bmap)
3039 return NULL;
3041 for (i = bmap->n_eq - 1; i >= 0; --i) {
3042 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3043 continue;
3044 if (isl_basic_map_drop_equality(bmap, i) < 0)
3045 return isl_basic_map_free(bmap);
3048 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3049 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3050 continue;
3051 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3052 return isl_basic_map_free(bmap);
3055 return bmap;
3058 /* Drop all constraints in bset that involve any of the dimensions
3059 * first to first+n-1.
3060 * This function only performs the actual removal of constraints.
3062 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3063 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3065 return isl_basic_map_drop_constraints_involving(bset, first, n);
3068 /* Drop all constraints in bmap that do not involve any of the dimensions
3069 * first to first + n - 1 of the given type.
3071 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3072 __isl_take isl_basic_map *bmap,
3073 enum isl_dim_type type, unsigned first, unsigned n)
3075 int i;
3077 if (n == 0) {
3078 isl_space *space = isl_basic_map_get_space(bmap);
3079 isl_basic_map_free(bmap);
3080 return isl_basic_map_universe(space);
3082 bmap = isl_basic_map_cow(bmap);
3083 if (!bmap)
3084 return NULL;
3086 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3087 return isl_basic_map_free(bmap);
3089 first += isl_basic_map_offset(bmap, type) - 1;
3091 for (i = bmap->n_eq - 1; i >= 0; --i) {
3092 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3093 continue;
3094 if (isl_basic_map_drop_equality(bmap, i) < 0)
3095 return isl_basic_map_free(bmap);
3098 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3099 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3100 continue;
3101 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3102 return isl_basic_map_free(bmap);
3105 bmap = isl_basic_map_add_known_div_constraints(bmap);
3106 return bmap;
3109 /* Drop all constraints in bset that do not involve any of the dimensions
3110 * first to first + n - 1 of the given type.
3112 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3113 __isl_take isl_basic_set *bset,
3114 enum isl_dim_type type, unsigned first, unsigned n)
3116 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3117 type, first, n);
3120 /* Drop all constraints in bmap that involve any of the dimensions
3121 * first to first + n - 1 of the given type.
3123 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3124 __isl_take isl_basic_map *bmap,
3125 enum isl_dim_type type, unsigned first, unsigned n)
3127 if (!bmap)
3128 return NULL;
3129 if (n == 0)
3130 return bmap;
3132 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3133 return isl_basic_map_free(bmap);
3135 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3136 first += isl_basic_map_offset(bmap, type) - 1;
3137 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3138 bmap = isl_basic_map_add_known_div_constraints(bmap);
3139 return bmap;
3142 /* Drop all constraints in bset that involve any of the dimensions
3143 * first to first + n - 1 of the given type.
3145 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3146 __isl_take isl_basic_set *bset,
3147 enum isl_dim_type type, unsigned first, unsigned n)
3149 return isl_basic_map_drop_constraints_involving_dims(bset,
3150 type, first, n);
3153 /* Drop constraints from "map" by applying "drop" to each basic map.
3155 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3156 enum isl_dim_type type, unsigned first, unsigned n,
3157 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3158 enum isl_dim_type type, unsigned first, unsigned n))
3160 int i;
3162 if (isl_map_check_range(map, type, first, n) < 0)
3163 return isl_map_free(map);
3165 map = isl_map_cow(map);
3166 if (!map)
3167 return NULL;
3169 for (i = 0; i < map->n; ++i) {
3170 map->p[i] = drop(map->p[i], type, first, n);
3171 if (!map->p[i])
3172 return isl_map_free(map);
3175 if (map->n > 1)
3176 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3178 return map;
3181 /* Drop all constraints in map that involve any of the dimensions
3182 * first to first + n - 1 of the given type.
3184 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3185 __isl_take isl_map *map,
3186 enum isl_dim_type type, unsigned first, unsigned n)
3188 if (n == 0)
3189 return map;
3190 return drop_constraints(map, type, first, n,
3191 &isl_basic_map_drop_constraints_involving_dims);
3194 /* Drop all constraints in "map" that do not involve any of the dimensions
3195 * first to first + n - 1 of the given type.
3197 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3198 __isl_take isl_map *map,
3199 enum isl_dim_type type, unsigned first, unsigned n)
3201 if (n == 0) {
3202 isl_space *space = isl_map_get_space(map);
3203 isl_map_free(map);
3204 return isl_map_universe(space);
3206 return drop_constraints(map, type, first, n,
3207 &isl_basic_map_drop_constraints_not_involving_dims);
3210 /* Drop all constraints in set that involve any of the dimensions
3211 * first to first + n - 1 of the given type.
3213 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3214 __isl_take isl_set *set,
3215 enum isl_dim_type type, unsigned first, unsigned n)
3217 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3220 /* Drop all constraints in "set" that do not involve any of the dimensions
3221 * first to first + n - 1 of the given type.
3223 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3224 __isl_take isl_set *set,
3225 enum isl_dim_type type, unsigned first, unsigned n)
3227 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3230 /* Does local variable "div" of "bmap" have a complete explicit representation?
3231 * Having a complete explicit representation requires not only
3232 * an explicit representation, but also that all local variables
3233 * that appear in this explicit representation in turn have
3234 * a complete explicit representation.
3236 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3238 int i;
3239 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3240 isl_bool marked;
3242 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3243 if (marked < 0 || marked)
3244 return isl_bool_not(marked);
3246 for (i = bmap->n_div - 1; i >= 0; --i) {
3247 isl_bool known;
3249 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3250 continue;
3251 known = isl_basic_map_div_is_known(bmap, i);
3252 if (known < 0 || !known)
3253 return known;
3256 return isl_bool_true;
3259 /* Remove all divs that are unknown or defined in terms of unknown divs.
3261 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3262 __isl_take isl_basic_map *bmap)
3264 int i;
3266 if (!bmap)
3267 return NULL;
3269 for (i = bmap->n_div - 1; i >= 0; --i) {
3270 if (isl_basic_map_div_is_known(bmap, i))
3271 continue;
3272 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3273 if (!bmap)
3274 return NULL;
3275 i = bmap->n_div;
3278 return bmap;
3281 /* Remove all divs that are unknown or defined in terms of unknown divs.
3283 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3284 __isl_take isl_basic_set *bset)
3286 return isl_basic_map_remove_unknown_divs(bset);
3289 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3291 int i;
3293 if (!map)
3294 return NULL;
3295 if (map->n == 0)
3296 return map;
3298 map = isl_map_cow(map);
3299 if (!map)
3300 return NULL;
3302 for (i = 0; i < map->n; ++i) {
3303 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3304 if (!map->p[i])
3305 goto error;
3307 return map;
3308 error:
3309 isl_map_free(map);
3310 return NULL;
3313 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3315 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3318 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3319 __isl_take isl_basic_set *bset,
3320 enum isl_dim_type type, unsigned first, unsigned n)
3322 isl_basic_map *bmap = bset_to_bmap(bset);
3323 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3324 return bset_from_bmap(bmap);
3327 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3328 enum isl_dim_type type, unsigned first, unsigned n)
3330 int i;
3332 if (n == 0)
3333 return map;
3335 map = isl_map_cow(map);
3336 if (isl_map_check_range(map, type, first, n) < 0)
3337 return isl_map_free(map);
3339 for (i = 0; i < map->n; ++i) {
3340 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3341 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3342 if (!map->p[i])
3343 goto error;
3345 map = isl_map_drop(map, type, first, n);
3346 return map;
3347 error:
3348 isl_map_free(map);
3349 return NULL;
3352 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3353 enum isl_dim_type type, unsigned first, unsigned n)
3355 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3356 type, first, n));
3359 /* Project out n inputs starting at first using Fourier-Motzkin */
3360 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3361 unsigned first, unsigned n)
3363 return isl_map_remove_dims(map, isl_dim_in, first, n);
3366 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3367 FILE *out, int indent)
3369 isl_printer *p;
3371 if (!bset) {
3372 fprintf(out, "null basic set\n");
3373 return;
3376 fprintf(out, "%*s", indent, "");
3377 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3378 bset->ref, bset->dim->nparam, bset->dim->n_out,
3379 bset->extra, bset->flags);
3381 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3382 p = isl_printer_set_dump(p, 1);
3383 p = isl_printer_set_indent(p, indent);
3384 p = isl_printer_start_line(p);
3385 p = isl_printer_print_basic_set(p, bset);
3386 p = isl_printer_end_line(p);
3387 isl_printer_free(p);
3390 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3391 FILE *out, int indent)
3393 isl_printer *p;
3395 if (!bmap) {
3396 fprintf(out, "null basic map\n");
3397 return;
3400 fprintf(out, "%*s", indent, "");
3401 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3402 "flags: %x, n_name: %d\n",
3403 bmap->ref,
3404 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3405 bmap->extra, bmap->flags, bmap->dim->n_id);
3407 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3408 p = isl_printer_set_dump(p, 1);
3409 p = isl_printer_set_indent(p, indent);
3410 p = isl_printer_start_line(p);
3411 p = isl_printer_print_basic_map(p, bmap);
3412 p = isl_printer_end_line(p);
3413 isl_printer_free(p);
3416 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3417 unsigned pos)
3419 isl_size total;
3421 total = isl_basic_map_dim(bmap, isl_dim_all);
3422 if (total < 0)
3423 return isl_basic_map_free(bmap);
3424 if (pos >= bmap->n_ineq)
3425 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3426 "invalid position", return isl_basic_map_free(bmap));
3427 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3428 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3429 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3430 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3431 return bmap;
3434 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3435 unsigned flags)
3437 if (isl_space_check_is_set(space) < 0)
3438 goto error;
3439 return isl_map_alloc_space(space, n, flags);
3440 error:
3441 isl_space_free(space);
3442 return NULL;
3445 /* Make sure "map" has room for at least "n" more basic maps.
3447 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3449 int i;
3450 struct isl_map *grown = NULL;
3452 if (!map)
3453 return NULL;
3454 isl_assert(map->ctx, n >= 0, goto error);
3455 if (map->n + n <= map->size)
3456 return map;
3457 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3458 if (!grown)
3459 goto error;
3460 for (i = 0; i < map->n; ++i) {
3461 grown->p[i] = isl_basic_map_copy(map->p[i]);
3462 if (!grown->p[i])
3463 goto error;
3464 grown->n++;
3466 isl_map_free(map);
3467 return grown;
3468 error:
3469 isl_map_free(grown);
3470 isl_map_free(map);
3471 return NULL;
3474 /* Make sure "set" has room for at least "n" more basic sets.
3476 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3478 return set_from_map(isl_map_grow(set_to_map(set), n));
3481 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3483 return isl_map_from_basic_map(bset);
3486 /* This function performs the same operation as isl_set_from_basic_set,
3487 * but is considered as a function on an isl_basic_set when exported.
3489 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3491 return isl_set_from_basic_set(bset);
3494 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3496 struct isl_map *map;
3498 if (!bmap)
3499 return NULL;
3501 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3502 return isl_map_add_basic_map(map, bmap);
3505 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3506 __isl_take isl_basic_set *bset)
3508 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3509 bset_to_bmap(bset)));
3512 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3514 return isl_map_free(set);
3517 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3519 int i;
3521 if (!set) {
3522 fprintf(out, "null set\n");
3523 return;
3526 fprintf(out, "%*s", indent, "");
3527 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3528 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3529 set->flags);
3530 for (i = 0; i < set->n; ++i) {
3531 fprintf(out, "%*s", indent, "");
3532 fprintf(out, "basic set %d:\n", i);
3533 isl_basic_set_print_internal(set->p[i], out, indent+4);
3537 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3539 int i;
3541 if (!map) {
3542 fprintf(out, "null map\n");
3543 return;
3546 fprintf(out, "%*s", indent, "");
3547 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3548 "flags: %x, n_name: %d\n",
3549 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3550 map->dim->n_out, map->flags, map->dim->n_id);
3551 for (i = 0; i < map->n; ++i) {
3552 fprintf(out, "%*s", indent, "");
3553 fprintf(out, "basic map %d:\n", i);
3554 isl_basic_map_print_internal(map->p[i], out, indent+4);
3558 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3560 static isl_stat isl_basic_map_check_compatible_domain(
3561 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3563 isl_bool ok;
3565 ok = isl_basic_map_compatible_domain(bmap, bset);
3566 if (ok < 0)
3567 return isl_stat_error;
3568 if (!ok)
3569 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3570 "incompatible spaces", return isl_stat_error);
3572 return isl_stat_ok;
3575 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3576 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3578 struct isl_basic_map *bmap_domain;
3579 isl_size dim;
3581 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3582 goto error;
3584 dim = isl_basic_set_dim(bset, isl_dim_set);
3585 if (dim < 0)
3586 goto error;
3587 if (dim != 0 &&
3588 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3589 goto error;
3591 bmap = isl_basic_map_cow(bmap);
3592 if (!bmap)
3593 goto error;
3594 bmap = isl_basic_map_extend(bmap,
3595 bset->n_div, bset->n_eq, bset->n_ineq);
3596 bmap_domain = isl_basic_map_from_domain(bset);
3597 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3599 bmap = isl_basic_map_simplify(bmap);
3600 return isl_basic_map_finalize(bmap);
3601 error:
3602 isl_basic_map_free(bmap);
3603 isl_basic_set_free(bset);
3604 return NULL;
3607 /* Check that the space of "bset" is the same as that of the range of "bmap".
3609 static isl_stat isl_basic_map_check_compatible_range(
3610 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3612 isl_bool ok;
3614 ok = isl_basic_map_compatible_range(bmap, bset);
3615 if (ok < 0)
3616 return isl_stat_error;
3617 if (!ok)
3618 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3619 "incompatible spaces", return isl_stat_error);
3621 return isl_stat_ok;
3624 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3625 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3627 struct isl_basic_map *bmap_range;
3628 isl_size dim;
3630 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3631 goto error;
3633 dim = isl_basic_set_dim(bset, isl_dim_set);
3634 if (dim < 0)
3635 goto error;
3636 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3637 goto error;
3639 if (isl_basic_set_plain_is_universe(bset)) {
3640 isl_basic_set_free(bset);
3641 return bmap;
3644 bmap = isl_basic_map_cow(bmap);
3645 if (!bmap)
3646 goto error;
3647 bmap = isl_basic_map_extend(bmap,
3648 bset->n_div, bset->n_eq, bset->n_ineq);
3649 bmap_range = bset_to_bmap(bset);
3650 bmap = add_constraints(bmap, bmap_range, 0, 0);
3652 bmap = isl_basic_map_simplify(bmap);
3653 return isl_basic_map_finalize(bmap);
3654 error:
3655 isl_basic_map_free(bmap);
3656 isl_basic_set_free(bset);
3657 return NULL;
3660 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3661 __isl_keep isl_vec *vec)
3663 int i;
3664 isl_size total;
3665 isl_int s;
3667 total = isl_basic_map_dim(bmap, isl_dim_all);
3668 if (total < 0 || !vec)
3669 return isl_bool_error;
3671 if (1 + total != vec->size)
3672 return isl_bool_false;
3674 isl_int_init(s);
3676 for (i = 0; i < bmap->n_eq; ++i) {
3677 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3678 if (!isl_int_is_zero(s)) {
3679 isl_int_clear(s);
3680 return isl_bool_false;
3684 for (i = 0; i < bmap->n_ineq; ++i) {
3685 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3686 if (isl_int_is_neg(s)) {
3687 isl_int_clear(s);
3688 return isl_bool_false;
3692 isl_int_clear(s);
3694 return isl_bool_true;
3697 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3698 __isl_keep isl_vec *vec)
3700 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3703 __isl_give isl_basic_map *isl_basic_map_intersect(
3704 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3706 struct isl_vec *sample = NULL;
3707 isl_space *space1, *space2;
3708 isl_size dim1, dim2, nparam1, nparam2;
3710 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3711 goto error;
3712 space1 = isl_basic_map_peek_space(bmap1);
3713 space2 = isl_basic_map_peek_space(bmap2);
3714 dim1 = isl_space_dim(space1, isl_dim_all);
3715 dim2 = isl_space_dim(space2, isl_dim_all);
3716 nparam1 = isl_space_dim(space1, isl_dim_param);
3717 nparam2 = isl_space_dim(space2, isl_dim_param);
3718 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3719 goto error;
3720 if (dim1 == nparam1 && dim2 != nparam2)
3721 return isl_basic_map_intersect(bmap2, bmap1);
3723 if (dim2 != nparam2 &&
3724 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3725 goto error;
3727 if (isl_basic_map_plain_is_empty(bmap1)) {
3728 isl_basic_map_free(bmap2);
3729 return bmap1;
3731 if (isl_basic_map_plain_is_empty(bmap2)) {
3732 isl_basic_map_free(bmap1);
3733 return bmap2;
3736 if (bmap1->sample &&
3737 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3738 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3739 sample = isl_vec_copy(bmap1->sample);
3740 else if (bmap2->sample &&
3741 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3742 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3743 sample = isl_vec_copy(bmap2->sample);
3745 bmap1 = isl_basic_map_cow(bmap1);
3746 if (!bmap1)
3747 goto error;
3748 bmap1 = isl_basic_map_extend(bmap1,
3749 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3750 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3752 if (!bmap1)
3753 isl_vec_free(sample);
3754 else if (sample) {
3755 isl_vec_free(bmap1->sample);
3756 bmap1->sample = sample;
3759 bmap1 = isl_basic_map_simplify(bmap1);
3760 return isl_basic_map_finalize(bmap1);
3761 error:
3762 if (sample)
3763 isl_vec_free(sample);
3764 isl_basic_map_free(bmap1);
3765 isl_basic_map_free(bmap2);
3766 return NULL;
3769 __isl_give isl_basic_set *isl_basic_set_intersect(
3770 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3772 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3773 bset_to_bmap(bset2)));
3776 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3777 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3779 return isl_basic_set_intersect(bset1, bset2);
3782 /* Does "map" consist of a single disjunct, without any local variables?
3784 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3786 isl_size n_div;
3788 if (!map)
3789 return isl_bool_error;
3790 if (map->n != 1)
3791 return isl_bool_false;
3792 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3793 if (n_div < 0)
3794 return isl_bool_error;
3795 if (n_div != 0)
3796 return isl_bool_false;
3797 return isl_bool_true;
3800 /* Check that "map" consists of a single disjunct, without any local variables.
3802 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3804 isl_bool ok;
3806 ok = is_convex_no_locals(map);
3807 if (ok < 0)
3808 return isl_stat_error;
3809 if (ok)
3810 return isl_stat_ok;
3812 isl_die(isl_map_get_ctx(map), isl_error_internal,
3813 "unexpectedly not convex or involving local variables",
3814 return isl_stat_error);
3817 /* Special case of isl_map_intersect, where both map1 and map2
3818 * are convex, without any divs and such that either map1 or map2
3819 * contains a single constraint. This constraint is then simply
3820 * added to the other map.
3822 static __isl_give isl_map *map_intersect_add_constraint(
3823 __isl_take isl_map *map1, __isl_take isl_map *map2)
3825 if (check_convex_no_locals(map1) < 0 ||
3826 check_convex_no_locals(map2) < 0)
3827 goto error;
3829 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3830 return isl_map_intersect(map2, map1);
3832 map1 = isl_map_cow(map1);
3833 if (!map1)
3834 goto error;
3835 if (isl_map_plain_is_empty(map1)) {
3836 isl_map_free(map2);
3837 return map1;
3839 if (map2->p[0]->n_eq == 1)
3840 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3841 else
3842 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3843 map2->p[0]->ineq[0]);
3845 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3846 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3847 if (!map1->p[0])
3848 goto error;
3850 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3851 isl_basic_map_free(map1->p[0]);
3852 map1->n = 0;
3855 isl_map_free(map2);
3857 map1 = isl_map_unmark_normalized(map1);
3858 return map1;
3859 error:
3860 isl_map_free(map1);
3861 isl_map_free(map2);
3862 return NULL;
3865 /* map2 may be either a parameter domain or a map living in the same
3866 * space as map1.
3868 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3869 __isl_take isl_map *map2)
3871 unsigned flags = 0;
3872 isl_bool equal;
3873 isl_map *result;
3874 int i, j;
3875 isl_size dim2, nparam2;
3877 if (!map1 || !map2)
3878 goto error;
3880 if ((isl_map_plain_is_empty(map1) ||
3881 isl_map_plain_is_universe(map2)) &&
3882 isl_space_is_equal(map1->dim, map2->dim)) {
3883 isl_map_free(map2);
3884 return map1;
3886 if ((isl_map_plain_is_empty(map2) ||
3887 isl_map_plain_is_universe(map1)) &&
3888 isl_space_is_equal(map1->dim, map2->dim)) {
3889 isl_map_free(map1);
3890 return map2;
3893 if (is_convex_no_locals(map1) == isl_bool_true &&
3894 is_convex_no_locals(map2) == isl_bool_true &&
3895 isl_space_is_equal(map1->dim, map2->dim) &&
3896 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3897 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3898 return map_intersect_add_constraint(map1, map2);
3900 equal = isl_map_plain_is_equal(map1, map2);
3901 if (equal < 0)
3902 goto error;
3903 if (equal) {
3904 isl_map_free(map2);
3905 return map1;
3908 dim2 = isl_map_dim(map2, isl_dim_all);
3909 nparam2 = isl_map_dim(map2, isl_dim_param);
3910 if (dim2 < 0 || nparam2 < 0)
3911 goto error;
3912 if (dim2 != nparam2)
3913 isl_assert(map1->ctx,
3914 isl_space_is_equal(map1->dim, map2->dim), goto error);
3916 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3917 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3918 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3920 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3921 map1->n * map2->n, flags);
3922 if (!result)
3923 goto error;
3924 for (i = 0; i < map1->n; ++i)
3925 for (j = 0; j < map2->n; ++j) {
3926 struct isl_basic_map *part;
3927 part = isl_basic_map_intersect(
3928 isl_basic_map_copy(map1->p[i]),
3929 isl_basic_map_copy(map2->p[j]));
3930 if (isl_basic_map_is_empty(part) < 0)
3931 part = isl_basic_map_free(part);
3932 result = isl_map_add_basic_map(result, part);
3933 if (!result)
3934 goto error;
3936 isl_map_free(map1);
3937 isl_map_free(map2);
3938 return result;
3939 error:
3940 isl_map_free(map1);
3941 isl_map_free(map2);
3942 return NULL;
3945 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3946 __isl_take isl_map *map2)
3948 if (isl_map_check_equal_space(map1, map2) < 0)
3949 goto error;
3950 return map_intersect_internal(map1, map2);
3951 error:
3952 isl_map_free(map1);
3953 isl_map_free(map2);
3954 return NULL;
3957 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3958 __isl_take isl_map *map2)
3960 isl_map_align_params_bin(&map1, &map2);
3961 return map_intersect(map1, map2);
3964 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3965 __isl_take isl_set *set2)
3967 return set_from_map(isl_map_intersect(set_to_map(set1),
3968 set_to_map(set2)));
3971 /* map_intersect_internal accepts intersections
3972 * with parameter domains, so we can just call that function.
3974 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3975 __isl_take isl_set *params)
3977 isl_map_align_params_set(&map, &params);
3978 return map_intersect_internal(map, params);
3981 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3982 __isl_take isl_set *params)
3984 return isl_map_intersect_params(set, params);
3987 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3989 isl_space *space;
3990 unsigned pos;
3991 isl_size n1, n2;
3993 if (!bmap)
3994 return NULL;
3995 bmap = isl_basic_map_cow(bmap);
3996 if (!bmap)
3997 return NULL;
3998 space = isl_space_reverse(isl_space_copy(bmap->dim));
3999 pos = isl_basic_map_offset(bmap, isl_dim_in);
4000 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4001 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4002 if (n1 < 0 || n2 < 0)
4003 bmap = isl_basic_map_free(bmap);
4004 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4005 return isl_basic_map_reset_space(bmap, space);
4008 /* Given a basic map where the tuple of type "type" is a wrapped map,
4009 * swap domain and range of that wrapped map.
4011 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4012 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4014 isl_space *space;
4015 isl_size offset, n1, n2;
4017 space = isl_basic_map_peek_space(bmap);
4018 offset = isl_basic_map_var_offset(bmap, type);
4019 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4020 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4021 if (offset < 0 || n1 < 0 || n2 < 0)
4022 return isl_basic_map_free(bmap);
4024 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4026 space = isl_basic_map_take_space(bmap);
4027 space = isl_space_reverse_wrapped(space, type);
4028 bmap = isl_basic_map_restore_space(bmap, space);
4030 return bmap;
4033 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4034 * (B -> A) -> C.
4036 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4037 __isl_take isl_basic_map *bmap)
4039 isl_space *space;
4041 space = isl_basic_map_peek_space(bmap);
4042 if (isl_space_check_domain_is_wrapping(space) < 0)
4043 return isl_basic_map_free(bmap);
4044 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4046 return bmap;
4049 /* Given a basic map A -> (B -> C), return the corresponding basic map
4050 * A -> (C -> B).
4052 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4053 __isl_take isl_basic_map *bmap)
4055 isl_space *space;
4057 space = isl_basic_map_peek_space(bmap);
4058 if (isl_space_check_range_is_wrapping(space) < 0)
4059 return isl_basic_map_free(bmap);
4060 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4062 return bmap;
4065 /* Given a basic map that is actually a basic set (A -> B),
4066 * return the corresponding basic set (B -> A) as a basic map.
4068 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4069 __isl_take isl_basic_map *bmap)
4071 isl_space *space;
4073 space = isl_basic_map_peek_space(bmap);
4074 if (isl_space_check_is_wrapping(space) < 0)
4075 return isl_basic_map_free(bmap);
4076 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4078 return bmap;
4081 static __isl_give isl_basic_map *basic_map_space_reset(
4082 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4084 isl_space *space;
4086 if (!bmap)
4087 return NULL;
4088 if (!isl_space_is_named_or_nested(bmap->dim, type))
4089 return bmap;
4091 space = isl_basic_map_get_space(bmap);
4092 space = isl_space_reset(space, type);
4093 bmap = isl_basic_map_reset_space(bmap, space);
4094 return bmap;
4097 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4098 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4099 unsigned pos, unsigned n)
4101 isl_bool rational, is_empty;
4102 isl_space *res_space;
4103 struct isl_basic_map *res;
4104 struct isl_dim_map *dim_map;
4105 isl_size total;
4106 unsigned off;
4107 enum isl_dim_type t;
4109 if (n == 0)
4110 return basic_map_space_reset(bmap, type);
4112 is_empty = isl_basic_map_plain_is_empty(bmap);
4113 total = isl_basic_map_dim(bmap, isl_dim_all);
4114 if (is_empty < 0 || total < 0)
4115 return isl_basic_map_free(bmap);
4116 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4117 type, pos, n);
4118 if (!res_space)
4119 return isl_basic_map_free(bmap);
4120 if (is_empty) {
4121 isl_basic_map_free(bmap);
4122 return isl_basic_map_empty(res_space);
4125 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4126 off = 0;
4127 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4128 isl_size dim;
4130 if (t != type) {
4131 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4132 } else {
4133 isl_size size = isl_basic_map_dim(bmap, t);
4134 if (size < 0)
4135 dim_map = isl_dim_map_free(dim_map);
4136 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4137 0, pos, off);
4138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4139 pos, size - pos, off + pos + n);
4141 dim = isl_space_dim(res_space, t);
4142 if (dim < 0)
4143 dim_map = isl_dim_map_free(dim_map);
4144 off += dim;
4146 isl_dim_map_div(dim_map, bmap, off);
4148 res = isl_basic_map_alloc_space(res_space,
4149 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4150 rational = isl_basic_map_is_rational(bmap);
4151 if (rational < 0)
4152 res = isl_basic_map_free(res);
4153 if (rational)
4154 res = isl_basic_map_set_rational(res);
4155 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4156 return isl_basic_map_finalize(res);
4159 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4160 __isl_take isl_basic_set *bset,
4161 enum isl_dim_type type, unsigned pos, unsigned n)
4163 return isl_basic_map_insert_dims(bset, type, pos, n);
4166 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4167 enum isl_dim_type type, unsigned n)
4169 isl_size dim;
4171 dim = isl_basic_map_dim(bmap, type);
4172 if (dim < 0)
4173 return isl_basic_map_free(bmap);
4174 return isl_basic_map_insert_dims(bmap, type, dim, n);
4177 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4178 enum isl_dim_type type, unsigned n)
4180 if (!bset)
4181 return NULL;
4182 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4183 return isl_basic_map_add_dims(bset, type, n);
4184 error:
4185 isl_basic_set_free(bset);
4186 return NULL;
4189 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4190 enum isl_dim_type type)
4192 isl_space *space;
4194 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4195 return map;
4197 space = isl_map_get_space(map);
4198 space = isl_space_reset(space, type);
4199 map = isl_map_reset_space(map, space);
4200 return map;
4203 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4204 enum isl_dim_type type, unsigned pos, unsigned n)
4206 int i;
4207 isl_space *space;
4209 if (n == 0)
4210 return map_space_reset(map, type);
4212 map = isl_map_cow(map);
4213 if (!map)
4214 return NULL;
4216 for (i = 0; i < map->n; ++i) {
4217 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4218 if (!map->p[i])
4219 goto error;
4222 space = isl_map_take_space(map);
4223 space = isl_space_insert_dims(space, type, pos, n);
4224 map = isl_map_restore_space(map, space);
4226 return map;
4227 error:
4228 isl_map_free(map);
4229 return NULL;
4232 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4233 enum isl_dim_type type, unsigned pos, unsigned n)
4235 return isl_map_insert_dims(set, type, pos, n);
4238 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4239 enum isl_dim_type type, unsigned n)
4241 isl_size dim;
4243 dim = isl_map_dim(map, type);
4244 if (dim < 0)
4245 return isl_map_free(map);
4246 return isl_map_insert_dims(map, type, dim, n);
4249 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4250 enum isl_dim_type type, unsigned n)
4252 if (!set)
4253 return NULL;
4254 isl_assert(set->ctx, type != isl_dim_in, goto error);
4255 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4256 error:
4257 isl_set_free(set);
4258 return NULL;
4261 __isl_give isl_basic_map *isl_basic_map_move_dims(
4262 __isl_take isl_basic_map *bmap,
4263 enum isl_dim_type dst_type, unsigned dst_pos,
4264 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4266 isl_space *space;
4267 struct isl_dim_map *dim_map;
4268 struct isl_basic_map *res;
4269 enum isl_dim_type t;
4270 isl_size total;
4271 unsigned off;
4273 if (!bmap)
4274 return NULL;
4275 if (n == 0) {
4276 bmap = isl_basic_map_reset(bmap, src_type);
4277 bmap = isl_basic_map_reset(bmap, dst_type);
4278 return bmap;
4281 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4282 return isl_basic_map_free(bmap);
4284 if (dst_type == src_type && dst_pos == src_pos)
4285 return bmap;
4287 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4289 if (pos(bmap->dim, dst_type) + dst_pos ==
4290 pos(bmap->dim, src_type) + src_pos +
4291 ((src_type < dst_type) ? n : 0)) {
4292 space = isl_basic_map_take_space(bmap);
4293 space = isl_space_move_dims(space, dst_type, dst_pos,
4294 src_type, src_pos, n);
4295 bmap = isl_basic_map_restore_space(bmap, space);
4296 bmap = isl_basic_map_finalize(bmap);
4298 return bmap;
4301 total = isl_basic_map_dim(bmap, isl_dim_all);
4302 if (total < 0)
4303 return isl_basic_map_free(bmap);
4304 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4306 off = 0;
4307 space = isl_basic_map_peek_space(bmap);
4308 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4309 isl_size size = isl_space_dim(space, t);
4310 if (size < 0)
4311 dim_map = isl_dim_map_free(dim_map);
4312 if (t == dst_type) {
4313 isl_dim_map_dim_range(dim_map, space, t,
4314 0, dst_pos, off);
4315 off += dst_pos;
4316 isl_dim_map_dim_range(dim_map, space, src_type,
4317 src_pos, n, off);
4318 off += n;
4319 isl_dim_map_dim_range(dim_map, space, t,
4320 dst_pos, size - dst_pos, off);
4321 off += size - dst_pos;
4322 } else if (t == src_type) {
4323 isl_dim_map_dim_range(dim_map, space, t,
4324 0, src_pos, off);
4325 off += src_pos;
4326 isl_dim_map_dim_range(dim_map, space, t,
4327 src_pos + n, size - src_pos - n, off);
4328 off += size - src_pos - n;
4329 } else {
4330 isl_dim_map_dim(dim_map, space, t, off);
4331 off += size;
4334 isl_dim_map_div(dim_map, bmap, off);
4336 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4337 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4338 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4339 space = isl_basic_map_take_space(bmap);
4340 space = isl_space_move_dims(space, dst_type, dst_pos,
4341 src_type, src_pos, n);
4342 bmap = isl_basic_map_restore_space(bmap, space);
4343 if (!bmap)
4344 goto error;
4346 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4347 bmap = isl_basic_map_gauss(bmap, NULL);
4348 bmap = isl_basic_map_finalize(bmap);
4350 return bmap;
4351 error:
4352 isl_basic_map_free(bmap);
4353 return NULL;
4356 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4357 enum isl_dim_type dst_type, unsigned dst_pos,
4358 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4360 isl_basic_map *bmap = bset_to_bmap(bset);
4361 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4362 src_type, src_pos, n);
4363 return bset_from_bmap(bmap);
4366 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4367 enum isl_dim_type dst_type, unsigned dst_pos,
4368 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4370 if (!set)
4371 return NULL;
4372 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4373 return set_from_map(isl_map_move_dims(set_to_map(set),
4374 dst_type, dst_pos, src_type, src_pos, n));
4375 error:
4376 isl_set_free(set);
4377 return NULL;
4380 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4381 enum isl_dim_type dst_type, unsigned dst_pos,
4382 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4384 int i;
4385 isl_space *space;
4387 if (n == 0) {
4388 map = isl_map_reset(map, src_type);
4389 map = isl_map_reset(map, dst_type);
4390 return map;
4393 if (isl_map_check_range(map, src_type, src_pos, n))
4394 return isl_map_free(map);
4396 if (dst_type == src_type && dst_pos == src_pos)
4397 return map;
4399 isl_assert(map->ctx, dst_type != src_type, goto error);
4401 map = isl_map_cow(map);
4402 if (!map)
4403 return NULL;
4405 for (i = 0; i < map->n; ++i) {
4406 map->p[i] = isl_basic_map_move_dims(map->p[i],
4407 dst_type, dst_pos,
4408 src_type, src_pos, n);
4409 if (!map->p[i])
4410 goto error;
4413 space = isl_map_take_space(map);
4414 space = isl_space_move_dims(space, dst_type, dst_pos,
4415 src_type, src_pos, n);
4416 map = isl_map_restore_space(map, space);
4418 return map;
4419 error:
4420 isl_map_free(map);
4421 return NULL;
4424 /* Move the specified dimensions to the last columns right before
4425 * the divs. Don't change the dimension specification of bmap.
4426 * That's the responsibility of the caller.
4428 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4429 enum isl_dim_type type, unsigned first, unsigned n)
4431 isl_space *space;
4432 struct isl_dim_map *dim_map;
4433 struct isl_basic_map *res;
4434 enum isl_dim_type t;
4435 isl_size total;
4436 unsigned off;
4438 if (!bmap)
4439 return NULL;
4440 if (isl_basic_map_offset(bmap, type) + first + n ==
4441 isl_basic_map_offset(bmap, isl_dim_div))
4442 return bmap;
4444 total = isl_basic_map_dim(bmap, isl_dim_all);
4445 if (total < 0)
4446 return isl_basic_map_free(bmap);
4447 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4449 off = 0;
4450 space = isl_basic_map_peek_space(bmap);
4451 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4452 isl_size size = isl_space_dim(space, t);
4453 if (size < 0)
4454 dim_map = isl_dim_map_free(dim_map);
4455 if (t == type) {
4456 isl_dim_map_dim_range(dim_map, space, t,
4457 0, first, off);
4458 off += first;
4459 isl_dim_map_dim_range(dim_map, space, t,
4460 first, n, total - bmap->n_div - n);
4461 isl_dim_map_dim_range(dim_map, space, t,
4462 first + n, size - (first + n), off);
4463 off += size - (first + n);
4464 } else {
4465 isl_dim_map_dim(dim_map, space, t, off);
4466 off += size;
4469 isl_dim_map_div(dim_map, bmap, off + n);
4471 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4472 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4473 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4474 return res;
4477 /* Insert "n" rows in the divs of "bmap".
4479 * The number of columns is not changed, which means that the last
4480 * dimensions of "bmap" are being reintepreted as the new divs.
4481 * The space of "bmap" is not adjusted, however, which means
4482 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4483 * from the space of "bmap" is the responsibility of the caller.
4485 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4486 int n)
4488 int i;
4489 size_t row_size;
4490 isl_int **new_div;
4491 isl_int *old;
4493 bmap = isl_basic_map_cow(bmap);
4494 if (!bmap)
4495 return NULL;
4497 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4498 old = bmap->block2.data;
4499 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4500 (bmap->extra + n) * (1 + row_size));
4501 if (!bmap->block2.data)
4502 return isl_basic_map_free(bmap);
4503 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4504 if (!new_div)
4505 return isl_basic_map_free(bmap);
4506 for (i = 0; i < n; ++i) {
4507 new_div[i] = bmap->block2.data +
4508 (bmap->extra + i) * (1 + row_size);
4509 isl_seq_clr(new_div[i], 1 + row_size);
4511 for (i = 0; i < bmap->extra; ++i)
4512 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4513 free(bmap->div);
4514 bmap->div = new_div;
4515 bmap->n_div += n;
4516 bmap->extra += n;
4518 return bmap;
4521 /* Drop constraints from "bmap" that only involve the variables
4522 * of "type" in the range [first, first + n] that are not related
4523 * to any of the variables outside that interval.
4524 * These constraints cannot influence the values for the variables
4525 * outside the interval, except in case they cause "bmap" to be empty.
4526 * Only drop the constraints if "bmap" is known to be non-empty.
4528 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4529 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4530 unsigned first, unsigned n)
4532 int i;
4533 int *groups;
4534 isl_size dim, n_div;
4535 isl_bool non_empty;
4537 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4538 if (non_empty < 0)
4539 return isl_basic_map_free(bmap);
4540 if (!non_empty)
4541 return bmap;
4543 dim = isl_basic_map_dim(bmap, isl_dim_all);
4544 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4545 if (dim < 0 || n_div < 0)
4546 return isl_basic_map_free(bmap);
4547 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4548 if (!groups)
4549 return isl_basic_map_free(bmap);
4550 first += isl_basic_map_offset(bmap, type) - 1;
4551 for (i = 0; i < first; ++i)
4552 groups[i] = -1;
4553 for (i = first + n; i < dim - n_div; ++i)
4554 groups[i] = -1;
4556 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4558 return bmap;
4561 /* Turn the n dimensions of type type, starting at first
4562 * into existentially quantified variables.
4564 * If a subset of the projected out variables are unrelated
4565 * to any of the variables that remain, then the constraints
4566 * involving this subset are simply dropped first.
4568 __isl_give isl_basic_map *isl_basic_map_project_out(
4569 __isl_take isl_basic_map *bmap,
4570 enum isl_dim_type type, unsigned first, unsigned n)
4572 isl_bool empty;
4573 isl_space *space;
4575 if (n == 0)
4576 return basic_map_space_reset(bmap, type);
4577 if (type == isl_dim_div)
4578 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4579 "cannot project out existentially quantified variables",
4580 return isl_basic_map_free(bmap));
4582 empty = isl_basic_map_plain_is_empty(bmap);
4583 if (empty < 0)
4584 return isl_basic_map_free(bmap);
4585 if (empty)
4586 bmap = isl_basic_map_set_to_empty(bmap);
4588 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4589 if (!bmap)
4590 return NULL;
4592 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4593 return isl_basic_map_remove_dims(bmap, type, first, n);
4595 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4596 return isl_basic_map_free(bmap);
4598 bmap = move_last(bmap, type, first, n);
4599 bmap = isl_basic_map_cow(bmap);
4600 bmap = insert_div_rows(bmap, n);
4602 space = isl_basic_map_take_space(bmap);
4603 space = isl_space_drop_dims(space, type, first, n);
4604 bmap = isl_basic_map_restore_space(bmap, space);
4605 bmap = isl_basic_map_simplify(bmap);
4606 bmap = isl_basic_map_drop_redundant_divs(bmap);
4607 return isl_basic_map_finalize(bmap);
4610 /* Turn the n dimensions of type type, starting at first
4611 * into existentially quantified variables.
4613 __isl_give isl_basic_set *isl_basic_set_project_out(
4614 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4615 unsigned first, unsigned n)
4617 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4618 type, first, n));
4621 /* Turn the n dimensions of type type, starting at first
4622 * into existentially quantified variables.
4624 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4625 enum isl_dim_type type, unsigned first, unsigned n)
4627 int i;
4628 isl_space *space;
4630 if (n == 0)
4631 return map_space_reset(map, type);
4633 if (isl_map_check_range(map, type, first, n) < 0)
4634 return isl_map_free(map);
4636 map = isl_map_cow(map);
4637 if (!map)
4638 return NULL;
4640 for (i = 0; i < map->n; ++i) {
4641 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4642 if (!map->p[i])
4643 goto error;
4646 if (map->n > 1)
4647 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4648 map = isl_map_unmark_normalized(map);
4650 space = isl_map_take_space(map);
4651 space = isl_space_drop_dims(space, type, first, n);
4652 map = isl_map_restore_space(map, space);
4654 return map;
4655 error:
4656 isl_map_free(map);
4657 return NULL;
4660 #undef TYPE
4661 #define TYPE isl_map
4662 #include "isl_project_out_all_params_templ.c"
4663 #include "isl_project_out_param_templ.c"
4665 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4666 * into existentially quantified variables.
4668 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4669 enum isl_dim_type type, unsigned first, unsigned n)
4671 isl_size dim;
4673 dim = isl_map_dim(map, type);
4674 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4675 return isl_map_free(map);
4676 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4677 map = isl_map_project_out(map, type, 0, first);
4678 return map;
4681 /* Turn the n dimensions of type type, starting at first
4682 * into existentially quantified variables.
4684 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4685 enum isl_dim_type type, unsigned first, unsigned n)
4687 return set_from_map(isl_map_project_out(set_to_map(set),
4688 type, first, n));
4691 /* If "set" involves a parameter with identifier "id",
4692 * then turn it into an existentially quantified variable.
4694 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4695 __isl_take isl_id *id)
4697 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
4700 /* If "set" involves any of the parameters with identifiers in "list",
4701 * then turn them into existentially quantified variables.
4703 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4704 __isl_take isl_id_list *list)
4706 isl_map *map;
4708 map = set_to_map(set);
4709 map = isl_map_project_out_param_id_list(map, list);
4710 return set_from_map(map);
4713 /* Project out all parameters from "set" by existentially quantifying
4714 * over them.
4716 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4718 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4721 /* Return a map that projects the elements in "set" onto their
4722 * "n" set dimensions starting at "first".
4723 * "type" should be equal to isl_dim_set.
4725 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4726 enum isl_dim_type type, unsigned first, unsigned n)
4728 int i;
4729 isl_map *map;
4731 if (type != isl_dim_set)
4732 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4733 "only set dimensions can be projected out", goto error);
4734 if (isl_set_check_range(set, type, first, n) < 0)
4735 return isl_set_free(set);
4737 map = isl_map_from_domain(set);
4738 map = isl_map_add_dims(map, isl_dim_out, n);
4739 for (i = 0; i < n; ++i)
4740 map = isl_map_equate(map, isl_dim_in, first + i,
4741 isl_dim_out, i);
4742 return map;
4743 error:
4744 isl_set_free(set);
4745 return NULL;
4748 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4749 unsigned n)
4751 int i, j;
4752 isl_size total;
4754 total = isl_basic_map_dim(bmap, isl_dim_all);
4755 if (total < 0)
4756 return isl_basic_map_free(bmap);
4757 for (i = 0; i < n; ++i) {
4758 j = isl_basic_map_alloc_div(bmap);
4759 if (j < 0)
4760 goto error;
4761 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4763 return bmap;
4764 error:
4765 isl_basic_map_free(bmap);
4766 return NULL;
4769 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4771 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4772 __isl_keep isl_basic_map *bmap2)
4774 isl_space *space1, *space2;
4776 space1 = isl_basic_map_peek_space(bmap1);
4777 space2 = isl_basic_map_peek_space(bmap2);
4778 return isl_space_tuple_is_equal(space1, isl_dim_out,
4779 space2, isl_dim_in);
4782 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4784 static isl_stat isl_basic_map_check_applies_range(
4785 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4787 isl_bool equal;
4789 equal = isl_basic_map_applies_range(bmap1, bmap2);
4790 if (equal < 0)
4791 return isl_stat_error;
4792 if (!equal)
4793 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4794 "spaces don't match", return isl_stat_error);
4795 return isl_stat_ok;
4798 __isl_give isl_basic_map *isl_basic_map_apply_range(
4799 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4801 isl_space *space_result = NULL;
4802 struct isl_basic_map *bmap;
4803 isl_size n_in, n_out, n, nparam;
4804 unsigned total, pos;
4805 struct isl_dim_map *dim_map1, *dim_map2;
4807 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4808 goto error;
4809 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4810 goto error;
4812 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4813 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4814 n = isl_basic_map_dim(bmap1, isl_dim_out);
4815 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4816 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4817 goto error;
4819 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4820 isl_basic_map_get_space(bmap2));
4822 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4823 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4824 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4825 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4826 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4827 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4828 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4829 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4830 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4831 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4832 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4834 bmap = isl_basic_map_alloc_space(space_result,
4835 bmap1->n_div + bmap2->n_div + n,
4836 bmap1->n_eq + bmap2->n_eq,
4837 bmap1->n_ineq + bmap2->n_ineq);
4838 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4839 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4840 bmap = add_divs(bmap, n);
4841 bmap = isl_basic_map_simplify(bmap);
4842 bmap = isl_basic_map_drop_redundant_divs(bmap);
4843 return isl_basic_map_finalize(bmap);
4844 error:
4845 isl_basic_map_free(bmap1);
4846 isl_basic_map_free(bmap2);
4847 return NULL;
4850 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4851 __isl_take isl_basic_map *bmap)
4853 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4854 goto error;
4856 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4857 bmap));
4858 error:
4859 isl_basic_set_free(bset);
4860 isl_basic_map_free(bmap);
4861 return NULL;
4864 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4865 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4867 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4868 goto error;
4869 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4870 bmap2->dim, isl_dim_in))
4871 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4872 "spaces don't match", goto error);
4874 bmap1 = isl_basic_map_reverse(bmap1);
4875 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4876 return isl_basic_map_reverse(bmap1);
4877 error:
4878 isl_basic_map_free(bmap1);
4879 isl_basic_map_free(bmap2);
4880 return NULL;
4883 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4884 * A \cap B -> f(A) + f(B)
4886 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4887 __isl_take isl_basic_map *bmap2)
4889 isl_size n_in, n_out, nparam;
4890 unsigned total, pos;
4891 struct isl_basic_map *bmap = NULL;
4892 struct isl_dim_map *dim_map1, *dim_map2;
4893 int i;
4895 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4896 goto error;
4898 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4899 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4900 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4901 if (nparam < 0 || n_in < 0 || n_out < 0)
4902 goto error;
4904 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4905 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4906 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4907 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4908 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4911 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4912 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4916 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4917 bmap1->n_div + bmap2->n_div + 2 * n_out,
4918 bmap1->n_eq + bmap2->n_eq + n_out,
4919 bmap1->n_ineq + bmap2->n_ineq);
4920 for (i = 0; i < n_out; ++i) {
4921 int j = isl_basic_map_alloc_equality(bmap);
4922 if (j < 0)
4923 goto error;
4924 isl_seq_clr(bmap->eq[j], 1+total);
4925 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4926 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4927 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4929 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4930 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4931 bmap = add_divs(bmap, 2 * n_out);
4933 bmap = isl_basic_map_simplify(bmap);
4934 return isl_basic_map_finalize(bmap);
4935 error:
4936 isl_basic_map_free(bmap);
4937 isl_basic_map_free(bmap1);
4938 isl_basic_map_free(bmap2);
4939 return NULL;
4942 /* Given two maps A -> f(A) and B -> g(B), construct a map
4943 * A \cap B -> f(A) + f(B)
4945 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4946 __isl_take isl_map *map2)
4948 struct isl_map *result;
4949 int i, j;
4951 if (isl_map_check_equal_space(map1, map2) < 0)
4952 goto error;
4954 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4955 map1->n * map2->n, 0);
4956 if (!result)
4957 goto error;
4958 for (i = 0; i < map1->n; ++i)
4959 for (j = 0; j < map2->n; ++j) {
4960 struct isl_basic_map *part;
4961 part = isl_basic_map_sum(
4962 isl_basic_map_copy(map1->p[i]),
4963 isl_basic_map_copy(map2->p[j]));
4964 if (isl_basic_map_is_empty(part))
4965 isl_basic_map_free(part);
4966 else
4967 result = isl_map_add_basic_map(result, part);
4968 if (!result)
4969 goto error;
4971 isl_map_free(map1);
4972 isl_map_free(map2);
4973 return result;
4974 error:
4975 isl_map_free(map1);
4976 isl_map_free(map2);
4977 return NULL;
4980 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4981 __isl_take isl_set *set2)
4983 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4986 /* Given a basic map A -> f(A), construct A -> -f(A).
4988 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4990 int i, j;
4991 unsigned off;
4992 isl_size n;
4994 bmap = isl_basic_map_cow(bmap);
4995 n = isl_basic_map_dim(bmap, isl_dim_out);
4996 if (n < 0)
4997 return isl_basic_map_free(bmap);
4999 off = isl_basic_map_offset(bmap, isl_dim_out);
5000 for (i = 0; i < bmap->n_eq; ++i)
5001 for (j = 0; j < n; ++j)
5002 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5003 for (i = 0; i < bmap->n_ineq; ++i)
5004 for (j = 0; j < n; ++j)
5005 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5006 for (i = 0; i < bmap->n_div; ++i)
5007 for (j = 0; j < n; ++j)
5008 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5009 bmap = isl_basic_map_gauss(bmap, NULL);
5010 return isl_basic_map_finalize(bmap);
5013 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5015 return isl_basic_map_neg(bset);
5018 /* Given a map A -> f(A), construct A -> -f(A).
5020 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5022 int i;
5024 map = isl_map_cow(map);
5025 if (!map)
5026 return NULL;
5028 for (i = 0; i < map->n; ++i) {
5029 map->p[i] = isl_basic_map_neg(map->p[i]);
5030 if (!map->p[i])
5031 goto error;
5034 return map;
5035 error:
5036 isl_map_free(map);
5037 return NULL;
5040 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5042 return set_from_map(isl_map_neg(set_to_map(set)));
5045 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5046 * A -> floor(f(A)/d).
5048 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5049 isl_int d)
5051 isl_size n_in, n_out, nparam;
5052 unsigned total, pos;
5053 struct isl_basic_map *result = NULL;
5054 struct isl_dim_map *dim_map;
5055 int i;
5057 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5058 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5059 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5060 if (nparam < 0 || n_in < 0 || n_out < 0)
5061 return isl_basic_map_free(bmap);
5063 total = nparam + n_in + n_out + bmap->n_div + n_out;
5064 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5065 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5066 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5067 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5068 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5070 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5071 bmap->n_div + n_out,
5072 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5073 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5074 result = add_divs(result, n_out);
5075 for (i = 0; i < n_out; ++i) {
5076 int j;
5077 j = isl_basic_map_alloc_inequality(result);
5078 if (j < 0)
5079 goto error;
5080 isl_seq_clr(result->ineq[j], 1+total);
5081 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5082 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5083 j = isl_basic_map_alloc_inequality(result);
5084 if (j < 0)
5085 goto error;
5086 isl_seq_clr(result->ineq[j], 1+total);
5087 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5088 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5089 isl_int_sub_ui(result->ineq[j][0], d, 1);
5092 result = isl_basic_map_simplify(result);
5093 return isl_basic_map_finalize(result);
5094 error:
5095 isl_basic_map_free(result);
5096 return NULL;
5099 /* Given a map A -> f(A) and an integer d, construct a map
5100 * A -> floor(f(A)/d).
5102 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5104 int i;
5106 map = isl_map_cow(map);
5107 if (!map)
5108 return NULL;
5110 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5111 for (i = 0; i < map->n; ++i) {
5112 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5113 if (!map->p[i])
5114 goto error;
5116 map = isl_map_unmark_normalized(map);
5118 return map;
5119 error:
5120 isl_map_free(map);
5121 return NULL;
5124 /* Given a map A -> f(A) and an integer d, construct a map
5125 * A -> floor(f(A)/d).
5127 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5128 __isl_take isl_val *d)
5130 if (!map || !d)
5131 goto error;
5132 if (!isl_val_is_int(d))
5133 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5134 "expecting integer denominator", goto error);
5135 map = isl_map_floordiv(map, d->n);
5136 isl_val_free(d);
5137 return map;
5138 error:
5139 isl_map_free(map);
5140 isl_val_free(d);
5141 return NULL;
5144 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5145 unsigned pos)
5147 int i;
5148 isl_size nparam;
5149 isl_size n_in;
5150 isl_size total;
5152 total = isl_basic_map_dim(bmap, isl_dim_all);
5153 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5154 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5155 if (total < 0 || nparam < 0 || n_in < 0)
5156 return isl_basic_map_free(bmap);
5157 i = isl_basic_map_alloc_equality(bmap);
5158 if (i < 0)
5159 goto error;
5160 isl_seq_clr(bmap->eq[i], 1 + total);
5161 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5162 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5163 return isl_basic_map_finalize(bmap);
5164 error:
5165 isl_basic_map_free(bmap);
5166 return NULL;
5169 /* Add a constraint to "bmap" expressing i_pos < o_pos
5171 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5172 unsigned pos)
5174 int i;
5175 isl_size nparam;
5176 isl_size n_in;
5177 isl_size total;
5179 total = isl_basic_map_dim(bmap, isl_dim_all);
5180 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5181 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5182 if (total < 0 || nparam < 0 || n_in < 0)
5183 return isl_basic_map_free(bmap);
5184 i = isl_basic_map_alloc_inequality(bmap);
5185 if (i < 0)
5186 goto error;
5187 isl_seq_clr(bmap->ineq[i], 1 + total);
5188 isl_int_set_si(bmap->ineq[i][0], -1);
5189 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5190 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5191 return isl_basic_map_finalize(bmap);
5192 error:
5193 isl_basic_map_free(bmap);
5194 return NULL;
5197 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5199 static __isl_give isl_basic_map *var_less_or_equal(
5200 __isl_take isl_basic_map *bmap, unsigned pos)
5202 int i;
5203 isl_size nparam;
5204 isl_size n_in;
5205 isl_size total;
5207 total = isl_basic_map_dim(bmap, isl_dim_all);
5208 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5209 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5210 if (total < 0 || nparam < 0 || n_in < 0)
5211 return isl_basic_map_free(bmap);
5212 i = isl_basic_map_alloc_inequality(bmap);
5213 if (i < 0)
5214 goto error;
5215 isl_seq_clr(bmap->ineq[i], 1 + total);
5216 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5217 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5218 return isl_basic_map_finalize(bmap);
5219 error:
5220 isl_basic_map_free(bmap);
5221 return NULL;
5224 /* Add a constraint to "bmap" expressing i_pos > o_pos
5226 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5227 unsigned pos)
5229 int i;
5230 isl_size nparam;
5231 isl_size n_in;
5232 isl_size total;
5234 total = isl_basic_map_dim(bmap, isl_dim_all);
5235 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5236 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5237 if (total < 0 || nparam < 0 || n_in < 0)
5238 return isl_basic_map_free(bmap);
5239 i = isl_basic_map_alloc_inequality(bmap);
5240 if (i < 0)
5241 goto error;
5242 isl_seq_clr(bmap->ineq[i], 1 + total);
5243 isl_int_set_si(bmap->ineq[i][0], -1);
5244 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5245 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5246 return isl_basic_map_finalize(bmap);
5247 error:
5248 isl_basic_map_free(bmap);
5249 return NULL;
5252 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5254 static __isl_give isl_basic_map *var_more_or_equal(
5255 __isl_take isl_basic_map *bmap, unsigned pos)
5257 int i;
5258 isl_size nparam;
5259 isl_size n_in;
5260 isl_size total;
5262 total = isl_basic_map_dim(bmap, isl_dim_all);
5263 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5264 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5265 if (total < 0 || nparam < 0 || n_in < 0)
5266 return isl_basic_map_free(bmap);
5267 i = isl_basic_map_alloc_inequality(bmap);
5268 if (i < 0)
5269 goto error;
5270 isl_seq_clr(bmap->ineq[i], 1 + total);
5271 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5272 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5273 return isl_basic_map_finalize(bmap);
5274 error:
5275 isl_basic_map_free(bmap);
5276 return NULL;
5279 __isl_give isl_basic_map *isl_basic_map_equal(
5280 __isl_take isl_space *space, unsigned n_equal)
5282 int i;
5283 struct isl_basic_map *bmap;
5284 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5285 if (!bmap)
5286 return NULL;
5287 for (i = 0; i < n_equal && bmap; ++i)
5288 bmap = var_equal(bmap, i);
5289 return isl_basic_map_finalize(bmap);
5292 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5294 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5295 unsigned pos)
5297 int i;
5298 struct isl_basic_map *bmap;
5299 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5300 if (!bmap)
5301 return NULL;
5302 for (i = 0; i < pos && bmap; ++i)
5303 bmap = var_equal(bmap, i);
5304 if (bmap)
5305 bmap = var_less(bmap, pos);
5306 return isl_basic_map_finalize(bmap);
5309 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5311 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5312 __isl_take isl_space *space, unsigned pos)
5314 int i;
5315 isl_basic_map *bmap;
5317 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5318 for (i = 0; i < pos; ++i)
5319 bmap = var_equal(bmap, i);
5320 bmap = var_less_or_equal(bmap, pos);
5321 return isl_basic_map_finalize(bmap);
5324 /* Return a relation on "space" expressing i_pos > o_pos
5326 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5327 unsigned pos)
5329 int i;
5330 struct isl_basic_map *bmap;
5331 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5332 if (!bmap)
5333 return NULL;
5334 for (i = 0; i < pos && bmap; ++i)
5335 bmap = var_equal(bmap, i);
5336 if (bmap)
5337 bmap = var_more(bmap, pos);
5338 return isl_basic_map_finalize(bmap);
5341 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5343 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5344 __isl_take isl_space *space, unsigned pos)
5346 int i;
5347 isl_basic_map *bmap;
5349 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5350 for (i = 0; i < pos; ++i)
5351 bmap = var_equal(bmap, i);
5352 bmap = var_more_or_equal(bmap, pos);
5353 return isl_basic_map_finalize(bmap);
5356 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5357 unsigned n, int equal)
5359 struct isl_map *map;
5360 int i;
5362 if (n == 0 && equal)
5363 return isl_map_universe(space);
5365 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5367 for (i = 0; i + 1 < n; ++i)
5368 map = isl_map_add_basic_map(map,
5369 isl_basic_map_less_at(isl_space_copy(space), i));
5370 if (n > 0) {
5371 if (equal)
5372 map = isl_map_add_basic_map(map,
5373 isl_basic_map_less_or_equal_at(space, n - 1));
5374 else
5375 map = isl_map_add_basic_map(map,
5376 isl_basic_map_less_at(space, n - 1));
5377 } else
5378 isl_space_free(space);
5380 return map;
5383 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5385 if (!space)
5386 return NULL;
5387 return map_lex_lte_first(space, space->n_out, equal);
5390 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5391 unsigned n)
5393 return map_lex_lte_first(space, n, 0);
5396 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5397 unsigned n)
5399 return map_lex_lte_first(space, n, 1);
5402 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5404 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5407 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5409 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5412 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5413 unsigned n, int equal)
5415 struct isl_map *map;
5416 int i;
5418 if (n == 0 && equal)
5419 return isl_map_universe(space);
5421 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5423 for (i = 0; i + 1 < n; ++i)
5424 map = isl_map_add_basic_map(map,
5425 isl_basic_map_more_at(isl_space_copy(space), i));
5426 if (n > 0) {
5427 if (equal)
5428 map = isl_map_add_basic_map(map,
5429 isl_basic_map_more_or_equal_at(space, n - 1));
5430 else
5431 map = isl_map_add_basic_map(map,
5432 isl_basic_map_more_at(space, n - 1));
5433 } else
5434 isl_space_free(space);
5436 return map;
5439 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5441 if (!space)
5442 return NULL;
5443 return map_lex_gte_first(space, space->n_out, equal);
5446 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5447 unsigned n)
5449 return map_lex_gte_first(space, n, 0);
5452 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5453 unsigned n)
5455 return map_lex_gte_first(space, n, 1);
5458 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5460 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5463 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5465 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5468 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5469 __isl_take isl_set *set2)
5471 isl_map *map;
5472 map = isl_map_lex_le(isl_set_get_space(set1));
5473 map = isl_map_intersect_domain(map, set1);
5474 map = isl_map_intersect_range(map, set2);
5475 return map;
5478 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5479 __isl_take isl_set *set2)
5481 isl_map *map;
5482 map = isl_map_lex_lt(isl_set_get_space(set1));
5483 map = isl_map_intersect_domain(map, set1);
5484 map = isl_map_intersect_range(map, set2);
5485 return map;
5488 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5489 __isl_take isl_set *set2)
5491 isl_map *map;
5492 map = isl_map_lex_ge(isl_set_get_space(set1));
5493 map = isl_map_intersect_domain(map, set1);
5494 map = isl_map_intersect_range(map, set2);
5495 return map;
5498 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5499 __isl_take isl_set *set2)
5501 isl_map *map;
5502 map = isl_map_lex_gt(isl_set_get_space(set1));
5503 map = isl_map_intersect_domain(map, set1);
5504 map = isl_map_intersect_range(map, set2);
5505 return map;
5508 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5509 __isl_take isl_map *map2)
5511 isl_map *map;
5512 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5513 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5514 map = isl_map_apply_range(map, isl_map_reverse(map2));
5515 return map;
5518 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5519 __isl_take isl_map *map2)
5521 isl_map *map;
5522 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5523 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5524 map = isl_map_apply_range(map, isl_map_reverse(map2));
5525 return map;
5528 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5529 __isl_take isl_map *map2)
5531 isl_map *map;
5532 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5533 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5534 map = isl_map_apply_range(map, isl_map_reverse(map2));
5535 return map;
5538 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5539 __isl_take isl_map *map2)
5541 isl_map *map;
5542 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5543 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5544 map = isl_map_apply_range(map, isl_map_reverse(map2));
5545 return map;
5548 /* For the div d = floor(f/m) at position "div", add the constraint
5550 * f - m d >= 0
5552 static __isl_give isl_basic_map *add_upper_div_constraint(
5553 __isl_take isl_basic_map *bmap, unsigned div)
5555 int i;
5556 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5557 isl_size n_div;
5558 unsigned pos;
5560 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5561 if (v_div < 0 || n_div < 0)
5562 return isl_basic_map_free(bmap);
5563 pos = v_div + div;
5564 i = isl_basic_map_alloc_inequality(bmap);
5565 if (i < 0)
5566 return isl_basic_map_free(bmap);
5567 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5568 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5570 return bmap;
5573 /* For the div d = floor(f/m) at position "div", add the constraint
5575 * -(f-(m-1)) + m d >= 0
5577 static __isl_give isl_basic_map *add_lower_div_constraint(
5578 __isl_take isl_basic_map *bmap, unsigned div)
5580 int i;
5581 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5582 isl_size n_div;
5583 unsigned pos;
5585 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5586 if (v_div < 0 || n_div < 0)
5587 return isl_basic_map_free(bmap);
5588 pos = v_div + div;
5589 i = isl_basic_map_alloc_inequality(bmap);
5590 if (i < 0)
5591 return isl_basic_map_free(bmap);
5592 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5593 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5594 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5595 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5597 return bmap;
5600 /* For the div d = floor(f/m) at position "pos", add the constraints
5602 * f - m d >= 0
5603 * -(f-(m-1)) + m d >= 0
5605 * Note that the second constraint is the negation of
5607 * f - m d >= m
5609 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5610 __isl_take isl_basic_map *bmap, unsigned pos)
5612 bmap = add_upper_div_constraint(bmap, pos);
5613 bmap = add_lower_div_constraint(bmap, pos);
5614 return bmap;
5617 /* For each known div d = floor(f/m), add the constraints
5619 * f - m d >= 0
5620 * -(f-(m-1)) + m d >= 0
5622 * Remove duplicate constraints in case of some these div constraints
5623 * already appear in "bmap".
5625 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5626 __isl_take isl_basic_map *bmap)
5628 isl_size n_div;
5630 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5631 if (n_div < 0)
5632 return isl_basic_map_free(bmap);
5633 if (n_div == 0)
5634 return bmap;
5636 bmap = add_known_div_constraints(bmap);
5637 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5638 bmap = isl_basic_map_finalize(bmap);
5639 return bmap;
5642 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5644 * In particular, if this div is of the form d = floor(f/m),
5645 * then add the constraint
5647 * f - m d >= 0
5649 * if sign < 0 or the constraint
5651 * -(f-(m-1)) + m d >= 0
5653 * if sign > 0.
5655 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5656 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5658 if (sign < 0)
5659 return add_upper_div_constraint(bmap, div);
5660 else
5661 return add_lower_div_constraint(bmap, div);
5664 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5665 __isl_take isl_basic_map *bmap)
5667 isl_space *space;
5669 if (!bmap)
5670 goto error;
5671 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5672 bmap->n_div == 0 &&
5673 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5674 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5675 return bset_from_bmap(bmap);
5676 bmap = isl_basic_map_cow(bmap);
5677 if (!bmap)
5678 return NULL;
5679 space = isl_basic_map_take_space(bmap);
5680 space = isl_space_underlying(space, bmap->n_div);
5681 bmap = isl_basic_map_restore_space(bmap, space);
5682 if (!bmap)
5683 return NULL;
5684 bmap->extra -= bmap->n_div;
5685 bmap->n_div = 0;
5686 bmap = isl_basic_map_finalize(bmap);
5687 return bset_from_bmap(bmap);
5688 error:
5689 isl_basic_map_free(bmap);
5690 return NULL;
5693 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5694 __isl_take isl_basic_set *bset)
5696 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5699 /* Replace each element in "list" by the result of applying
5700 * isl_basic_map_underlying_set to the element.
5702 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5703 __isl_take isl_basic_map_list *list)
5705 int i;
5706 isl_size n;
5708 n = isl_basic_map_list_n_basic_map(list);
5709 if (n < 0)
5710 goto error;
5712 for (i = 0; i < n; ++i) {
5713 isl_basic_map *bmap;
5714 isl_basic_set *bset;
5716 bmap = isl_basic_map_list_get_basic_map(list, i);
5717 bset = isl_basic_set_underlying_set(bmap);
5718 list = isl_basic_set_list_set_basic_set(list, i, bset);
5721 return list;
5722 error:
5723 isl_basic_map_list_free(list);
5724 return NULL;
5727 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5728 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5730 struct isl_basic_map *bmap;
5731 struct isl_ctx *ctx;
5732 isl_size dim, bmap_total;
5733 unsigned total;
5734 int i;
5736 if (!bset || !like)
5737 goto error;
5738 ctx = bset->ctx;
5739 if (isl_basic_set_check_no_params(bset) < 0 ||
5740 isl_basic_set_check_no_locals(bset) < 0)
5741 goto error;
5742 dim = isl_basic_set_dim(bset, isl_dim_set);
5743 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5744 if (dim < 0 || bmap_total < 0)
5745 goto error;
5746 isl_assert(ctx, dim == bmap_total, goto error);
5747 if (like->n_div == 0) {
5748 isl_space *space = isl_basic_map_get_space(like);
5749 isl_basic_map_free(like);
5750 return isl_basic_map_reset_space(bset, space);
5752 bset = isl_basic_set_cow(bset);
5753 if (!bset)
5754 goto error;
5755 total = dim + bset->extra;
5756 bmap = bset_to_bmap(bset);
5757 isl_space_free(isl_basic_map_take_space(bmap));
5758 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5759 if (!bmap)
5760 goto error;
5761 bmap->n_div = like->n_div;
5762 bmap->extra += like->n_div;
5763 if (bmap->extra) {
5764 unsigned ltotal;
5765 isl_int **div;
5766 ltotal = total - bmap->extra + like->extra;
5767 if (ltotal > total)
5768 ltotal = total;
5769 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5770 bmap->extra * (1 + 1 + total));
5771 if (isl_blk_is_error(bmap->block2))
5772 goto error;
5773 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5774 if (!div)
5775 goto error;
5776 bmap->div = div;
5777 for (i = 0; i < bmap->extra; ++i)
5778 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5779 for (i = 0; i < like->n_div; ++i) {
5780 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5781 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5783 bmap = isl_basic_map_add_known_div_constraints(bmap);
5785 isl_basic_map_free(like);
5786 bmap = isl_basic_map_simplify(bmap);
5787 bmap = isl_basic_map_finalize(bmap);
5788 return bmap;
5789 error:
5790 isl_basic_map_free(like);
5791 isl_basic_set_free(bset);
5792 return NULL;
5795 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5796 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5798 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5799 bset_to_bmap(like)));
5802 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5804 int i;
5806 map = isl_map_cow(map);
5807 if (!map)
5808 return NULL;
5809 map->dim = isl_space_cow(map->dim);
5810 if (!map->dim)
5811 goto error;
5813 for (i = 1; i < map->n; ++i)
5814 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5815 goto error);
5816 for (i = 0; i < map->n; ++i) {
5817 map->p[i] = bset_to_bmap(
5818 isl_basic_map_underlying_set(map->p[i]));
5819 if (!map->p[i])
5820 goto error;
5822 if (map->n == 0)
5823 map->dim = isl_space_underlying(map->dim, 0);
5824 else {
5825 isl_space_free(map->dim);
5826 map->dim = isl_space_copy(map->p[0]->dim);
5828 if (!map->dim)
5829 goto error;
5830 return set_from_map(map);
5831 error:
5832 isl_map_free(map);
5833 return NULL;
5836 /* Replace the space of "bmap" by "space".
5838 * If the space of "bmap" is identical to "space" (including the identifiers
5839 * of the input and output dimensions), then simply return the original input.
5841 __isl_give isl_basic_map *isl_basic_map_reset_space(
5842 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5844 isl_bool equal;
5845 isl_space *bmap_space;
5847 bmap_space = isl_basic_map_peek_space(bmap);
5848 equal = isl_space_is_equal(bmap_space, space);
5849 if (equal >= 0 && equal)
5850 equal = isl_space_has_equal_ids(bmap_space, space);
5851 if (equal < 0)
5852 goto error;
5853 if (equal) {
5854 isl_space_free(space);
5855 return bmap;
5857 isl_space_free(isl_basic_map_take_space(bmap));
5858 bmap = isl_basic_map_restore_space(bmap, space);
5860 bmap = isl_basic_map_finalize(bmap);
5862 return bmap;
5863 error:
5864 isl_basic_map_free(bmap);
5865 isl_space_free(space);
5866 return NULL;
5869 __isl_give isl_basic_set *isl_basic_set_reset_space(
5870 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5872 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5873 space));
5876 /* Check that the total dimensions of "map" and "space" are the same.
5878 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5879 __isl_keep isl_space *space)
5881 isl_size dim1, dim2;
5883 dim1 = isl_map_dim(map, isl_dim_all);
5884 dim2 = isl_space_dim(space, isl_dim_all);
5885 if (dim1 < 0 || dim2 < 0)
5886 return isl_stat_error;
5887 if (dim1 == dim2)
5888 return isl_stat_ok;
5889 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5890 "total dimensions do not match", return isl_stat_error);
5893 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5894 __isl_take isl_space *space)
5896 int i;
5898 map = isl_map_cow(map);
5899 if (!map || !space)
5900 goto error;
5902 for (i = 0; i < map->n; ++i) {
5903 map->p[i] = isl_basic_map_reset_space(map->p[i],
5904 isl_space_copy(space));
5905 if (!map->p[i])
5906 goto error;
5908 isl_space_free(isl_map_take_space(map));
5909 map = isl_map_restore_space(map, space);
5911 return map;
5912 error:
5913 isl_map_free(map);
5914 isl_space_free(space);
5915 return NULL;
5918 /* Replace the space of "map" by "space", without modifying
5919 * the dimension of "map".
5921 * If the space of "map" is identical to "space" (including the identifiers
5922 * of the input and output dimensions), then simply return the original input.
5924 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5925 __isl_take isl_space *space)
5927 isl_bool equal;
5928 isl_space *map_space;
5930 map_space = isl_map_peek_space(map);
5931 equal = isl_space_is_equal(map_space, space);
5932 if (equal >= 0 && equal)
5933 equal = isl_space_has_equal_ids(map_space, space);
5934 if (equal < 0)
5935 goto error;
5936 if (equal) {
5937 isl_space_free(space);
5938 return map;
5940 if (check_map_space_equal_total_dim(map, space) < 0)
5941 goto error;
5942 return isl_map_reset_space(map, space);
5943 error:
5944 isl_map_free(map);
5945 isl_space_free(space);
5946 return NULL;
5949 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5950 __isl_take isl_space *space)
5952 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5955 /* Compute the parameter domain of the given basic set.
5957 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5959 isl_bool is_params;
5960 isl_space *space;
5961 isl_size n;
5963 is_params = isl_basic_set_is_params(bset);
5964 if (is_params < 0)
5965 return isl_basic_set_free(bset);
5966 if (is_params)
5967 return bset;
5969 n = isl_basic_set_dim(bset, isl_dim_set);
5970 if (n < 0)
5971 return isl_basic_set_free(bset);
5972 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5973 space = isl_basic_set_get_space(bset);
5974 space = isl_space_params(space);
5975 bset = isl_basic_set_reset_space(bset, space);
5976 return bset;
5979 /* Construct a zero-dimensional basic set with the given parameter domain.
5981 __isl_give isl_basic_set *isl_basic_set_from_params(
5982 __isl_take isl_basic_set *bset)
5984 isl_space *space;
5985 space = isl_basic_set_get_space(bset);
5986 space = isl_space_set_from_params(space);
5987 bset = isl_basic_set_reset_space(bset, space);
5988 return bset;
5991 /* Compute the parameter domain of the given set.
5993 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5995 return isl_map_params(set_to_map(set));
5998 /* Construct a zero-dimensional set with the given parameter domain.
6000 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6002 isl_space *space;
6003 space = isl_set_get_space(set);
6004 space = isl_space_set_from_params(space);
6005 set = isl_set_reset_space(set, space);
6006 return set;
6009 /* Compute the parameter domain of the given map.
6011 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6013 isl_space *space;
6014 isl_size n_in, n_out;
6016 n_in = isl_map_dim(map, isl_dim_in);
6017 n_out = isl_map_dim(map, isl_dim_out);
6018 if (n_in < 0 || n_out < 0)
6019 return isl_map_free(map);
6020 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6021 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6022 space = isl_map_get_space(map);
6023 space = isl_space_params(space);
6024 map = isl_map_reset_space(map, space);
6025 return map;
6028 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6030 isl_space *space;
6031 isl_size n_out;
6033 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6034 if (n_out < 0)
6035 return isl_basic_map_free(bmap);
6036 space = isl_space_domain(isl_basic_map_get_space(bmap));
6038 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6040 return isl_basic_map_reset_space(bmap, space);
6043 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6045 if (!bmap)
6046 return isl_bool_error;
6047 return isl_space_may_be_set(bmap->dim);
6050 /* Is this basic map actually a set?
6051 * Users should never call this function. Outside of isl,
6052 * the type should indicate whether something is a set or a map.
6054 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6056 if (!bmap)
6057 return isl_bool_error;
6058 return isl_space_is_set(bmap->dim);
6061 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6063 isl_bool is_set;
6065 is_set = isl_basic_map_is_set(bmap);
6066 if (is_set < 0)
6067 goto error;
6068 if (is_set)
6069 return bmap;
6070 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6071 error:
6072 isl_basic_map_free(bmap);
6073 return NULL;
6076 __isl_give isl_basic_map *isl_basic_map_domain_map(
6077 __isl_take isl_basic_map *bmap)
6079 int i;
6080 isl_space *space;
6081 isl_basic_map *domain;
6082 isl_size nparam, n_in, n_out;
6084 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6085 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6086 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6087 if (nparam < 0 || n_in < 0 || n_out < 0)
6088 return isl_basic_map_free(bmap);
6090 space = isl_basic_map_get_space(bmap);
6091 space = isl_space_from_range(isl_space_domain(space));
6092 domain = isl_basic_map_universe(space);
6094 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6095 bmap = isl_basic_map_apply_range(bmap, domain);
6096 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6098 for (i = 0; i < n_in; ++i)
6099 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6100 isl_dim_out, i);
6102 bmap = isl_basic_map_gauss(bmap, NULL);
6103 return isl_basic_map_finalize(bmap);
6106 __isl_give isl_basic_map *isl_basic_map_range_map(
6107 __isl_take isl_basic_map *bmap)
6109 int i;
6110 isl_space *space;
6111 isl_basic_map *range;
6112 isl_size nparam, n_in, n_out;
6114 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6115 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6116 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6117 if (nparam < 0 || n_in < 0 || n_out < 0)
6118 return isl_basic_map_free(bmap);
6120 space = isl_basic_map_get_space(bmap);
6121 space = isl_space_from_range(isl_space_range(space));
6122 range = isl_basic_map_universe(space);
6124 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6125 bmap = isl_basic_map_apply_range(bmap, range);
6126 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6128 for (i = 0; i < n_out; ++i)
6129 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6130 isl_dim_out, i);
6132 bmap = isl_basic_map_gauss(bmap, NULL);
6133 return isl_basic_map_finalize(bmap);
6136 int isl_map_may_be_set(__isl_keep isl_map *map)
6138 if (!map)
6139 return -1;
6140 return isl_space_may_be_set(map->dim);
6143 /* Is this map actually a set?
6144 * Users should never call this function. Outside of isl,
6145 * the type should indicate whether something is a set or a map.
6147 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6149 if (!map)
6150 return isl_bool_error;
6151 return isl_space_is_set(map->dim);
6154 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6156 isl_space *space;
6157 isl_size n_in;
6159 n_in = isl_map_dim(map, isl_dim_in);
6160 if (n_in < 0)
6161 return set_from_map(isl_map_free(map));
6162 space = isl_space_range(isl_map_get_space(map));
6164 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6166 return set_from_map(isl_map_reset_space(map, space));
6169 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6170 * to each of its basic maps.
6172 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6173 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6174 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6176 int i;
6177 isl_space *space;
6179 map = isl_map_cow(map);
6180 if (!map)
6181 return NULL;
6183 for (i = 0; i < map->n; ++i) {
6184 map->p[i] = fn_bmap(map->p[i]);
6185 if (!map->p[i])
6186 return isl_map_free(map);
6188 map = isl_map_unmark_normalized(map);
6190 space = isl_map_take_space(map);
6191 space = fn_space(space);
6192 map = isl_map_restore_space(map, space);
6194 return map;
6197 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6199 return isl_map_transform(map, &isl_space_domain_map,
6200 &isl_basic_map_domain_map);
6203 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6205 return isl_map_transform(map, &isl_space_range_map,
6206 &isl_basic_map_range_map);
6209 /* Given a wrapped map of the form A[B -> C],
6210 * return the map A[B -> C] -> B.
6212 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6214 isl_id *id;
6215 isl_map *map;
6217 if (!set)
6218 return NULL;
6219 if (!isl_set_has_tuple_id(set))
6220 return isl_map_domain_map(isl_set_unwrap(set));
6222 id = isl_set_get_tuple_id(set);
6223 map = isl_map_domain_map(isl_set_unwrap(set));
6224 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6226 return map;
6229 __isl_give isl_basic_map *isl_basic_map_from_domain(
6230 __isl_take isl_basic_set *bset)
6232 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6235 __isl_give isl_basic_map *isl_basic_map_from_range(
6236 __isl_take isl_basic_set *bset)
6238 isl_space *space;
6239 space = isl_basic_set_get_space(bset);
6240 space = isl_space_from_range(space);
6241 bset = isl_basic_set_reset_space(bset, space);
6242 return bset_to_bmap(bset);
6245 /* Create a relation with the given set as range.
6246 * The domain of the created relation is a zero-dimensional
6247 * flat anonymous space.
6249 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6251 isl_space *space;
6252 space = isl_set_get_space(set);
6253 space = isl_space_from_range(space);
6254 set = isl_set_reset_space(set, space);
6255 return set_to_map(set);
6258 /* Create a relation with the given set as domain.
6259 * The range of the created relation is a zero-dimensional
6260 * flat anonymous space.
6262 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6264 return isl_map_reverse(isl_map_from_range(set));
6267 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6268 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6270 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6273 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6274 __isl_take isl_set *range)
6276 return isl_map_apply_range(isl_map_reverse(domain), range);
6279 /* Return a newly allocated isl_map with given space and flags and
6280 * room for "n" basic maps.
6281 * Make sure that all cached information is cleared.
6283 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6284 unsigned flags)
6286 struct isl_map *map;
6288 if (!space)
6289 return NULL;
6290 if (n < 0)
6291 isl_die(space->ctx, isl_error_internal,
6292 "negative number of basic maps", goto error);
6293 map = isl_calloc(space->ctx, struct isl_map,
6294 sizeof(struct isl_map) +
6295 (n - 1) * sizeof(struct isl_basic_map *));
6296 if (!map)
6297 goto error;
6299 map->ctx = space->ctx;
6300 isl_ctx_ref(map->ctx);
6301 map->ref = 1;
6302 map->size = n;
6303 map->n = 0;
6304 map->dim = space;
6305 map->flags = flags;
6306 return map;
6307 error:
6308 isl_space_free(space);
6309 return NULL;
6312 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6314 struct isl_basic_map *bmap;
6315 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6316 bmap = isl_basic_map_set_to_empty(bmap);
6317 return bmap;
6320 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6322 struct isl_basic_set *bset;
6323 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6324 bset = isl_basic_set_set_to_empty(bset);
6325 return bset;
6328 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6330 struct isl_basic_map *bmap;
6331 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6332 bmap = isl_basic_map_finalize(bmap);
6333 return bmap;
6336 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6338 struct isl_basic_set *bset;
6339 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6340 bset = isl_basic_set_finalize(bset);
6341 return bset;
6344 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6345 __isl_take isl_space *space)
6347 int i;
6348 isl_size total = isl_space_dim(space, isl_dim_all);
6349 isl_basic_map *bmap;
6351 if (total < 0)
6352 space = isl_space_free(space);
6353 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6354 for (i = 0; i < total; ++i) {
6355 int k = isl_basic_map_alloc_inequality(bmap);
6356 if (k < 0)
6357 goto error;
6358 isl_seq_clr(bmap->ineq[k], 1 + total);
6359 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6361 return bmap;
6362 error:
6363 isl_basic_map_free(bmap);
6364 return NULL;
6367 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6368 __isl_take isl_space *space)
6370 return isl_basic_map_nat_universe(space);
6373 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6375 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6378 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6380 return isl_map_nat_universe(space);
6383 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6385 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6388 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6390 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6393 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6395 struct isl_map *map;
6396 if (!space)
6397 return NULL;
6398 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6399 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6400 return map;
6403 /* This function performs the same operation as isl_map_universe,
6404 * but is considered as a function on an isl_space when exported.
6406 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6408 return isl_map_universe(space);
6411 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6413 struct isl_set *set;
6414 if (!space)
6415 return NULL;
6416 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6417 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6418 return set;
6421 /* This function performs the same operation as isl_set_universe,
6422 * but is considered as a function on an isl_space when exported.
6424 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6426 return isl_set_universe(space);
6429 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6431 int i;
6432 struct isl_map *dup;
6434 if (!map)
6435 return NULL;
6436 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6437 for (i = 0; i < map->n; ++i)
6438 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6439 return dup;
6442 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6443 __isl_take isl_basic_map *bmap)
6445 if (!bmap || !map)
6446 goto error;
6447 if (isl_basic_map_plain_is_empty(bmap)) {
6448 isl_basic_map_free(bmap);
6449 return map;
6451 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6452 goto error;
6453 isl_assert(map->ctx, map->n < map->size, goto error);
6454 map->p[map->n] = bmap;
6455 map->n++;
6456 map = isl_map_unmark_normalized(map);
6457 return map;
6458 error:
6459 if (map)
6460 isl_map_free(map);
6461 if (bmap)
6462 isl_basic_map_free(bmap);
6463 return NULL;
6466 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6468 int i;
6470 if (!map)
6471 return NULL;
6473 if (--map->ref > 0)
6474 return NULL;
6476 clear_caches(map);
6477 isl_ctx_deref(map->ctx);
6478 for (i = 0; i < map->n; ++i)
6479 isl_basic_map_free(map->p[i]);
6480 isl_space_free(map->dim);
6481 free(map);
6483 return NULL;
6486 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6487 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6489 int j;
6490 isl_size total;
6492 total = isl_basic_map_dim(bmap, isl_dim_all);
6493 if (total < 0)
6494 return isl_basic_map_free(bmap);
6496 bmap = isl_basic_map_cow(bmap);
6497 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6498 j = isl_basic_map_alloc_equality(bmap);
6499 if (j < 0)
6500 goto error;
6501 isl_seq_clr(bmap->eq[j] + 1, total);
6502 isl_int_set_si(bmap->eq[j][pos], -1);
6503 isl_int_set_si(bmap->eq[j][0], value);
6504 bmap = isl_basic_map_simplify(bmap);
6505 return isl_basic_map_finalize(bmap);
6506 error:
6507 isl_basic_map_free(bmap);
6508 return NULL;
6511 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6512 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6514 int j;
6515 isl_size total;
6517 total = isl_basic_map_dim(bmap, isl_dim_all);
6518 if (total < 0)
6519 return isl_basic_map_free(bmap);
6521 bmap = isl_basic_map_cow(bmap);
6522 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6523 j = isl_basic_map_alloc_equality(bmap);
6524 if (j < 0)
6525 goto error;
6526 isl_seq_clr(bmap->eq[j] + 1, total);
6527 isl_int_set_si(bmap->eq[j][pos], -1);
6528 isl_int_set(bmap->eq[j][0], value);
6529 bmap = isl_basic_map_simplify(bmap);
6530 return isl_basic_map_finalize(bmap);
6531 error:
6532 isl_basic_map_free(bmap);
6533 return NULL;
6536 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6537 enum isl_dim_type type, unsigned pos, int value)
6539 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6540 return isl_basic_map_free(bmap);
6541 return isl_basic_map_fix_pos_si(bmap,
6542 isl_basic_map_offset(bmap, type) + pos, value);
6545 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6546 enum isl_dim_type type, unsigned pos, isl_int value)
6548 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6549 return isl_basic_map_free(bmap);
6550 return isl_basic_map_fix_pos(bmap,
6551 isl_basic_map_offset(bmap, type) + pos, value);
6554 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6555 * to be equal to "v".
6557 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6558 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6560 if (!bmap || !v)
6561 goto error;
6562 if (!isl_val_is_int(v))
6563 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6564 "expecting integer value", goto error);
6565 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6566 goto error;
6567 pos += isl_basic_map_offset(bmap, type);
6568 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6569 isl_val_free(v);
6570 return bmap;
6571 error:
6572 isl_basic_map_free(bmap);
6573 isl_val_free(v);
6574 return NULL;
6577 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6578 * to be equal to "v".
6580 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6581 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6583 return isl_basic_map_fix_val(bset, type, pos, v);
6586 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6587 enum isl_dim_type type, unsigned pos, int value)
6589 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6590 type, pos, value));
6593 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6594 enum isl_dim_type type, unsigned pos, isl_int value)
6596 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6597 type, pos, value));
6600 /* Remove the basic map at position "i" from "map" if this basic map
6601 * is (obviously) empty.
6603 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6605 isl_bool empty;
6607 if (!map)
6608 return NULL;
6610 empty = isl_basic_map_plain_is_empty(map->p[i]);
6611 if (empty < 0)
6612 return isl_map_free(map);
6613 if (!empty)
6614 return map;
6616 isl_basic_map_free(map->p[i]);
6617 map->n--;
6618 if (i != map->n) {
6619 map->p[i] = map->p[map->n];
6620 map = isl_map_unmark_normalized(map);
6624 return map;
6627 /* Perform "fn" on each basic map of "map", where we may not be holding
6628 * the only reference to "map".
6629 * In particular, "fn" should be a semantics preserving operation
6630 * that we want to apply to all copies of "map". We therefore need
6631 * to be careful not to modify "map" in a way that breaks "map"
6632 * in case anything goes wrong.
6634 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6635 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6637 struct isl_basic_map *bmap;
6638 int i;
6640 if (!map)
6641 return NULL;
6643 for (i = map->n - 1; i >= 0; --i) {
6644 bmap = isl_basic_map_copy(map->p[i]);
6645 bmap = fn(bmap);
6646 if (!bmap)
6647 goto error;
6648 isl_basic_map_free(map->p[i]);
6649 map->p[i] = bmap;
6650 map = remove_if_empty(map, i);
6651 if (!map)
6652 return NULL;
6655 return map;
6656 error:
6657 isl_map_free(map);
6658 return NULL;
6661 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6662 enum isl_dim_type type, unsigned pos, int value)
6664 int i;
6666 map = isl_map_cow(map);
6667 if (isl_map_check_range(map, type, pos, 1) < 0)
6668 return isl_map_free(map);
6669 for (i = map->n - 1; i >= 0; --i) {
6670 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6671 map = remove_if_empty(map, i);
6672 if (!map)
6673 return NULL;
6675 map = isl_map_unmark_normalized(map);
6676 return map;
6679 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6680 enum isl_dim_type type, unsigned pos, int value)
6682 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6685 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6686 enum isl_dim_type type, unsigned pos, isl_int value)
6688 int i;
6690 map = isl_map_cow(map);
6691 if (isl_map_check_range(map, type, pos, 1) < 0)
6692 return isl_map_free(map);
6693 for (i = 0; i < map->n; ++i) {
6694 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6695 if (!map->p[i])
6696 goto error;
6698 map = isl_map_unmark_normalized(map);
6699 return map;
6700 error:
6701 isl_map_free(map);
6702 return NULL;
6705 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6706 enum isl_dim_type type, unsigned pos, isl_int value)
6708 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6711 /* Fix the value of the variable at position "pos" of type "type" of "map"
6712 * to be equal to "v".
6714 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6715 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6717 int i;
6719 map = isl_map_cow(map);
6720 if (!map || !v)
6721 goto error;
6723 if (!isl_val_is_int(v))
6724 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6725 "expecting integer value", goto error);
6726 if (isl_map_check_range(map, type, pos, 1) < 0)
6727 goto error;
6728 for (i = map->n - 1; i >= 0; --i) {
6729 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6730 isl_val_copy(v));
6731 map = remove_if_empty(map, i);
6732 if (!map)
6733 goto error;
6735 map = isl_map_unmark_normalized(map);
6736 isl_val_free(v);
6737 return map;
6738 error:
6739 isl_map_free(map);
6740 isl_val_free(v);
6741 return NULL;
6744 /* Fix the value of the variable at position "pos" of type "type" of "set"
6745 * to be equal to "v".
6747 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6748 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6750 return isl_map_fix_val(set, type, pos, v);
6753 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6754 unsigned input, int value)
6756 return isl_map_fix_si(map, isl_dim_in, input, value);
6759 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6760 int value)
6762 return set_from_map(isl_map_fix_si(set_to_map(set),
6763 isl_dim_set, dim, value));
6766 static __isl_give isl_basic_map *basic_map_bound_si(
6767 __isl_take isl_basic_map *bmap,
6768 enum isl_dim_type type, unsigned pos, int value, int upper)
6770 int j;
6771 isl_size total;
6773 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6774 return isl_basic_map_free(bmap);
6775 total = isl_basic_map_dim(bmap, isl_dim_all);
6776 if (total < 0)
6777 return isl_basic_map_free(bmap);
6778 pos += isl_basic_map_offset(bmap, type);
6779 bmap = isl_basic_map_cow(bmap);
6780 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6781 j = isl_basic_map_alloc_inequality(bmap);
6782 if (j < 0)
6783 goto error;
6784 isl_seq_clr(bmap->ineq[j], 1 + total);
6785 if (upper) {
6786 isl_int_set_si(bmap->ineq[j][pos], -1);
6787 isl_int_set_si(bmap->ineq[j][0], value);
6788 } else {
6789 isl_int_set_si(bmap->ineq[j][pos], 1);
6790 isl_int_set_si(bmap->ineq[j][0], -value);
6792 bmap = isl_basic_map_simplify(bmap);
6793 return isl_basic_map_finalize(bmap);
6794 error:
6795 isl_basic_map_free(bmap);
6796 return NULL;
6799 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6800 __isl_take isl_basic_map *bmap,
6801 enum isl_dim_type type, unsigned pos, int value)
6803 return basic_map_bound_si(bmap, type, pos, value, 0);
6806 /* Constrain the values of the given dimension to be no greater than "value".
6808 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6809 __isl_take isl_basic_map *bmap,
6810 enum isl_dim_type type, unsigned pos, int value)
6812 return basic_map_bound_si(bmap, type, pos, value, 1);
6815 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6816 enum isl_dim_type type, unsigned pos, int value, int upper)
6818 int i;
6820 map = isl_map_cow(map);
6821 if (isl_map_check_range(map, type, pos, 1) < 0)
6822 return isl_map_free(map);
6823 for (i = 0; i < map->n; ++i) {
6824 map->p[i] = basic_map_bound_si(map->p[i],
6825 type, pos, value, upper);
6826 if (!map->p[i])
6827 goto error;
6829 map = isl_map_unmark_normalized(map);
6830 return map;
6831 error:
6832 isl_map_free(map);
6833 return NULL;
6836 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6837 enum isl_dim_type type, unsigned pos, int value)
6839 return map_bound_si(map, type, pos, value, 0);
6842 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6843 enum isl_dim_type type, unsigned pos, int value)
6845 return map_bound_si(map, type, pos, value, 1);
6848 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6849 enum isl_dim_type type, unsigned pos, int value)
6851 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6852 type, pos, value));
6855 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6856 enum isl_dim_type type, unsigned pos, int value)
6858 return isl_map_upper_bound_si(set, type, pos, value);
6861 /* Bound the given variable of "bmap" from below (or above is "upper"
6862 * is set) to "value".
6864 static __isl_give isl_basic_map *basic_map_bound(
6865 __isl_take isl_basic_map *bmap,
6866 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6868 int j;
6869 isl_size total;
6871 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6872 return isl_basic_map_free(bmap);
6873 total = isl_basic_map_dim(bmap, isl_dim_all);
6874 if (total < 0)
6875 return isl_basic_map_free(bmap);
6876 pos += isl_basic_map_offset(bmap, type);
6877 bmap = isl_basic_map_cow(bmap);
6878 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6879 j = isl_basic_map_alloc_inequality(bmap);
6880 if (j < 0)
6881 goto error;
6882 isl_seq_clr(bmap->ineq[j], 1 + total);
6883 if (upper) {
6884 isl_int_set_si(bmap->ineq[j][pos], -1);
6885 isl_int_set(bmap->ineq[j][0], value);
6886 } else {
6887 isl_int_set_si(bmap->ineq[j][pos], 1);
6888 isl_int_neg(bmap->ineq[j][0], value);
6890 bmap = isl_basic_map_simplify(bmap);
6891 return isl_basic_map_finalize(bmap);
6892 error:
6893 isl_basic_map_free(bmap);
6894 return NULL;
6897 /* Bound the given variable of "map" from below (or above is "upper"
6898 * is set) to "value".
6900 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6901 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6903 int i;
6905 map = isl_map_cow(map);
6906 if (isl_map_check_range(map, type, pos, 1) < 0)
6907 return isl_map_free(map);
6908 for (i = map->n - 1; i >= 0; --i) {
6909 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6910 map = remove_if_empty(map, i);
6911 if (!map)
6912 return NULL;
6914 map = isl_map_unmark_normalized(map);
6915 return map;
6918 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6919 enum isl_dim_type type, unsigned pos, isl_int value)
6921 return map_bound(map, type, pos, value, 0);
6924 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6925 enum isl_dim_type type, unsigned pos, isl_int value)
6927 return map_bound(map, type, pos, value, 1);
6930 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6931 enum isl_dim_type type, unsigned pos, isl_int value)
6933 return isl_map_lower_bound(set, type, pos, value);
6936 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6937 enum isl_dim_type type, unsigned pos, isl_int value)
6939 return isl_map_upper_bound(set, type, pos, value);
6942 /* Force the values of the variable at position "pos" of type "type" of "map"
6943 * to be no smaller than "value".
6945 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6946 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6948 if (!value)
6949 goto error;
6950 if (!isl_val_is_int(value))
6951 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6952 "expecting integer value", goto error);
6953 map = isl_map_lower_bound(map, type, pos, value->n);
6954 isl_val_free(value);
6955 return map;
6956 error:
6957 isl_val_free(value);
6958 isl_map_free(map);
6959 return NULL;
6962 /* Force the values of the variable at position "pos" of type "type" of "set"
6963 * to be no smaller than "value".
6965 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6966 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6968 isl_map *map;
6970 map = set_to_map(set);
6971 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6974 /* Force the values of the variable at position "pos" of type "type" of "map"
6975 * to be no greater than "value".
6977 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6978 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6980 if (!value)
6981 goto error;
6982 if (!isl_val_is_int(value))
6983 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6984 "expecting integer value", goto error);
6985 map = isl_map_upper_bound(map, type, pos, value->n);
6986 isl_val_free(value);
6987 return map;
6988 error:
6989 isl_val_free(value);
6990 isl_map_free(map);
6991 return NULL;
6994 /* Force the values of the variable at position "pos" of type "type" of "set"
6995 * to be no greater than "value".
6997 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6998 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7000 isl_map *map;
7002 map = set_to_map(set);
7003 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7006 /* If "mv" has an explicit domain, then intersect the domain of "map"
7007 * with this explicit domain.
7009 * An isl_multi_val object never has an explicit domain,
7010 * so simply return "map".
7012 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7013 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7015 return map;
7018 #undef BASE
7019 #define BASE val
7020 #include "isl_map_bound_templ.c"
7022 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7023 * for each set dimension, by treating the set as a map.
7025 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7026 __isl_take isl_multi_val *bound,
7027 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7028 unsigned pos, __isl_take isl_val *value))
7030 isl_map *map;
7032 map = set_to_map(set);
7033 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7036 #undef BASE
7037 #define BASE pw_aff
7038 #include "isl_map_bound_templ.c"
7040 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7041 * for each set dimension, by converting the set and the bound
7042 * to objects living in a map space.
7044 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7045 __isl_take isl_multi_pw_aff *bound,
7046 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7047 unsigned pos, __isl_take TYPE *value))
7049 isl_map *map;
7051 map = isl_map_from_range(set);
7052 bound = isl_multi_pw_aff_from_range(bound);
7053 map = map_bound_multi_pw_aff(map, bound, set_bound);
7054 return isl_map_range(map);
7057 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7058 * setting a bound on the given output dimension.
7060 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7061 unsigned pos, __isl_take isl_val *v)
7063 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7066 /* Force the values of the set dimensions of "set"
7067 * to be no smaller than the corresponding values in "lower".
7069 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7070 __isl_take isl_multi_val *lower)
7072 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7075 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7076 * setting a bound on the given output dimension.
7078 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7079 unsigned pos, __isl_take isl_val *v)
7081 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7084 /* Force the values of the set dimensions of "set"
7085 * to be no greater than the corresponding values in "upper".
7087 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7088 __isl_take isl_multi_val *upper)
7090 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7093 /* Force the symbolic constant expression "bound"
7094 * to satisfy the relation "order" with respect to
7095 * the output variable at position "pos" of "map".
7097 * Create an affine expression representing the output variable
7098 * in terms of the range and
7099 * compare it using "order" to "bound" (defined on the domain).
7100 * The result is a relation between elements in domain and range that
7101 * can be intersected with "map".
7103 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7104 unsigned pos, __isl_take isl_pw_aff *bound,
7105 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7106 __isl_take isl_pw_aff *pa2))
7108 isl_space *space;
7109 isl_local_space *ls;
7110 isl_pw_aff *var;
7112 space = isl_space_range(isl_map_get_space(map));
7113 ls = isl_local_space_from_space(space);
7114 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7115 map = isl_map_intersect(map, order(bound, var));
7116 return map;
7119 /* Force the values of the output variable at position "pos" of "map"
7120 * to be no smaller than the symbolic constant expression "lower".
7122 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7123 unsigned pos, __isl_take isl_pw_aff *lower)
7125 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7128 /* Force the values of the output variable at position "pos" of "map"
7129 * to be no greater than the symbolic constant expression "upper".
7131 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7132 unsigned pos, __isl_take isl_pw_aff *upper)
7134 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7137 /* Force the values of the set dimensions of "set"
7138 * to be no smaller than the corresponding constant symbolic expressions
7139 * in "lower".
7141 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7142 __isl_take isl_multi_pw_aff *lower)
7144 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7147 /* Force the values of the set dimensions of "set"
7148 * to be no greater than the corresponding constant symbolic expressions
7149 * in "upper".
7151 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7152 __isl_take isl_multi_pw_aff *upper)
7154 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7157 /* Force the values of the output dimensions of "map"
7158 * to be no smaller than the corresponding constant symbolic expressions
7159 * in "lower".
7161 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7162 __isl_take isl_multi_pw_aff *lower)
7164 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7167 /* Force the values of the output dimensions of "map"
7168 * to be no greater than the corresponding constant symbolic expressions
7169 * in "upper".
7171 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7172 __isl_take isl_multi_pw_aff *upper)
7174 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7177 /* Bound the given variable of "bset" from below (or above is "upper"
7178 * is set) to "value".
7180 static __isl_give isl_basic_set *isl_basic_set_bound(
7181 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7182 isl_int value, int upper)
7184 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7185 type, pos, value, upper));
7188 /* Bound the given variable of "bset" from below (or above is "upper"
7189 * is set) to "value".
7191 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7192 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7193 __isl_take isl_val *value, int upper)
7195 if (!value)
7196 goto error;
7197 if (!isl_val_is_int(value))
7198 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7199 "expecting integer value", goto error);
7200 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7201 isl_val_free(value);
7202 return bset;
7203 error:
7204 isl_val_free(value);
7205 isl_basic_set_free(bset);
7206 return NULL;
7209 /* Bound the given variable of "bset" from below to "value".
7211 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7212 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7213 __isl_take isl_val *value)
7215 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7218 /* Bound the given variable of "bset" from above to "value".
7220 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7221 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7222 __isl_take isl_val *value)
7224 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7227 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7229 return isl_map_transform(map, &isl_space_reverse,
7230 &isl_basic_map_reverse);
7233 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7235 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7237 return isl_map_transform(map, &isl_space_domain_reverse,
7238 &isl_basic_map_domain_reverse);
7241 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7243 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7245 return isl_map_transform(map, &isl_space_range_reverse,
7246 &isl_basic_map_range_reverse);
7249 /* Given a set (A -> B), return the corresponding set (B -> A).
7251 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7253 isl_map *map = set_to_map(set);
7255 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7256 &isl_basic_map_set_reverse);
7257 return set_from_map(map);
7260 #undef TYPE
7261 #define TYPE isl_pw_multi_aff
7262 #undef SUFFIX
7263 #define SUFFIX _pw_multi_aff
7264 #undef EMPTY
7265 #define EMPTY isl_pw_multi_aff_empty
7266 #undef ADD
7267 #define ADD isl_pw_multi_aff_union_add
7268 #include "isl_map_lexopt_templ.c"
7270 /* Given a map "map", compute the lexicographically minimal
7271 * (or maximal) image element for each domain element in dom,
7272 * in the form of an isl_pw_multi_aff.
7273 * If "empty" is not NULL, then set *empty to those elements in dom that
7274 * do not have an image element.
7275 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7276 * should be computed over the domain of "map". "empty" is also NULL
7277 * in this case.
7279 * We first compute the lexicographically minimal or maximal element
7280 * in the first basic map. This results in a partial solution "res"
7281 * and a subset "todo" of dom that still need to be handled.
7282 * We then consider each of the remaining maps in "map" and successively
7283 * update both "res" and "todo".
7284 * If "empty" is NULL, then the todo sets are not needed and therefore
7285 * also not computed.
7287 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7288 __isl_take isl_map *map, __isl_take isl_set *dom,
7289 __isl_give isl_set **empty, unsigned flags)
7291 int i;
7292 int full;
7293 isl_pw_multi_aff *res;
7294 isl_set *todo;
7296 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7297 if (!map || (!full && !dom))
7298 goto error;
7300 if (isl_map_plain_is_empty(map)) {
7301 if (empty)
7302 *empty = dom;
7303 else
7304 isl_set_free(dom);
7305 return isl_pw_multi_aff_from_map(map);
7308 res = basic_map_partial_lexopt_pw_multi_aff(
7309 isl_basic_map_copy(map->p[0]),
7310 isl_set_copy(dom), empty, flags);
7312 if (empty)
7313 todo = *empty;
7314 for (i = 1; i < map->n; ++i) {
7315 isl_pw_multi_aff *res_i;
7317 res_i = basic_map_partial_lexopt_pw_multi_aff(
7318 isl_basic_map_copy(map->p[i]),
7319 isl_set_copy(dom), empty, flags);
7321 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7322 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7323 else
7324 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7326 if (empty)
7327 todo = isl_set_intersect(todo, *empty);
7330 isl_set_free(dom);
7331 isl_map_free(map);
7333 if (empty)
7334 *empty = todo;
7336 return res;
7337 error:
7338 if (empty)
7339 *empty = NULL;
7340 isl_set_free(dom);
7341 isl_map_free(map);
7342 return NULL;
7345 #undef TYPE
7346 #define TYPE isl_map
7347 #undef SUFFIX
7348 #define SUFFIX
7349 #undef EMPTY
7350 #define EMPTY isl_map_empty
7351 #undef ADD
7352 #define ADD isl_map_union_disjoint
7353 #include "isl_map_lexopt_templ.c"
7355 /* Given a map "map", compute the lexicographically minimal
7356 * (or maximal) image element for each domain element in "dom",
7357 * in the form of an isl_map.
7358 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7359 * do not have an image element.
7360 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7361 * should be computed over the domain of "map". "empty" is also NULL
7362 * in this case.
7364 * If the input consists of more than one disjunct, then first
7365 * compute the desired result in the form of an isl_pw_multi_aff and
7366 * then convert that into an isl_map.
7368 * This function used to have an explicit implementation in terms
7369 * of isl_maps, but it would continually intersect the domains of
7370 * partial results with the complement of the domain of the next
7371 * partial solution, potentially leading to an explosion in the number
7372 * of disjuncts if there are several disjuncts in the input.
7373 * An even earlier implementation of this function would look for
7374 * better results in the domain of the partial result and for extra
7375 * results in the complement of this domain, which would lead to
7376 * even more splintering.
7378 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7379 __isl_take isl_map *map, __isl_take isl_set *dom,
7380 __isl_give isl_set **empty, unsigned flags)
7382 int full;
7383 struct isl_map *res;
7384 isl_pw_multi_aff *pma;
7386 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7387 if (!map || (!full && !dom))
7388 goto error;
7390 if (isl_map_plain_is_empty(map)) {
7391 if (empty)
7392 *empty = dom;
7393 else
7394 isl_set_free(dom);
7395 return map;
7398 if (map->n == 1) {
7399 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7400 dom, empty, flags);
7401 isl_map_free(map);
7402 return res;
7405 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7406 flags);
7407 return isl_map_from_pw_multi_aff_internal(pma);
7408 error:
7409 if (empty)
7410 *empty = NULL;
7411 isl_set_free(dom);
7412 isl_map_free(map);
7413 return NULL;
7416 __isl_give isl_map *isl_map_partial_lexmax(
7417 __isl_take isl_map *map, __isl_take isl_set *dom,
7418 __isl_give isl_set **empty)
7420 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7423 __isl_give isl_map *isl_map_partial_lexmin(
7424 __isl_take isl_map *map, __isl_take isl_set *dom,
7425 __isl_give isl_set **empty)
7427 return isl_map_partial_lexopt(map, dom, empty, 0);
7430 __isl_give isl_set *isl_set_partial_lexmin(
7431 __isl_take isl_set *set, __isl_take isl_set *dom,
7432 __isl_give isl_set **empty)
7434 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7435 dom, empty));
7438 __isl_give isl_set *isl_set_partial_lexmax(
7439 __isl_take isl_set *set, __isl_take isl_set *dom,
7440 __isl_give isl_set **empty)
7442 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7443 dom, empty));
7446 /* Compute the lexicographic minimum (or maximum if "flags" includes
7447 * ISL_OPT_MAX) of "bset" over its parametric domain.
7449 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7450 unsigned flags)
7452 return isl_basic_map_lexopt(bset, flags);
7455 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7457 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7460 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7462 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7465 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7467 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7470 /* Compute the lexicographic minimum of "bset" over its parametric domain
7471 * for the purpose of quantifier elimination.
7472 * That is, find an explicit representation for all the existentially
7473 * quantified variables in "bset" by computing their lexicographic
7474 * minimum.
7476 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7477 __isl_take isl_basic_set *bset)
7479 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7482 /* Given a basic map with one output dimension, compute the minimum or
7483 * maximum of that dimension as an isl_pw_aff.
7485 * Compute the optimum as a lexicographic optimum over the single
7486 * output dimension and extract the single isl_pw_aff from the result.
7488 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7489 int max)
7491 isl_pw_multi_aff *pma;
7492 isl_pw_aff *pwaff;
7494 bmap = isl_basic_map_copy(bmap);
7495 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7496 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7497 isl_pw_multi_aff_free(pma);
7499 return pwaff;
7502 /* Compute the minimum or maximum of the given output dimension
7503 * as a function of the parameters and the input dimensions,
7504 * but independently of the other output dimensions.
7506 * We first project out the other output dimension and then compute
7507 * the "lexicographic" maximum in each basic map, combining the results
7508 * using isl_pw_aff_union_max.
7510 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7511 int max)
7513 int i;
7514 isl_pw_aff *pwaff;
7515 isl_size n_out;
7517 n_out = isl_map_dim(map, isl_dim_out);
7518 if (n_out < 0)
7519 map = isl_map_free(map);
7520 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7521 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7522 if (!map)
7523 return NULL;
7525 if (map->n == 0) {
7526 isl_space *space = isl_map_get_space(map);
7527 isl_map_free(map);
7528 return isl_pw_aff_empty(space);
7531 pwaff = basic_map_dim_opt(map->p[0], max);
7532 for (i = 1; i < map->n; ++i) {
7533 isl_pw_aff *pwaff_i;
7535 pwaff_i = basic_map_dim_opt(map->p[i], max);
7536 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7539 isl_map_free(map);
7541 return pwaff;
7544 /* Compute the minimum of the given output dimension as a function of the
7545 * parameters and input dimensions, but independently of
7546 * the other output dimensions.
7548 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7550 return map_dim_opt(map, pos, 0);
7553 /* Compute the maximum of the given output dimension as a function of the
7554 * parameters and input dimensions, but independently of
7555 * the other output dimensions.
7557 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7559 return map_dim_opt(map, pos, 1);
7562 /* Compute the minimum or maximum of the given set dimension
7563 * as a function of the parameters,
7564 * but independently of the other set dimensions.
7566 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7567 int max)
7569 return map_dim_opt(set, pos, max);
7572 /* Compute the maximum of the given set dimension as a function of the
7573 * parameters, but independently of the other set dimensions.
7575 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7577 return set_dim_opt(set, pos, 1);
7580 /* Compute the minimum of the given set dimension as a function of the
7581 * parameters, but independently of the other set dimensions.
7583 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7585 return set_dim_opt(set, pos, 0);
7588 /* Apply a preimage specified by "mat" on the parameters of "bset".
7589 * bset is assumed to have only parameters and divs.
7591 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7592 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7594 isl_size nparam;
7596 nparam = isl_basic_set_dim(bset, isl_dim_param);
7597 if (nparam < 0 || !mat)
7598 goto error;
7600 bset->dim = isl_space_cow(bset->dim);
7601 if (!bset->dim)
7602 goto error;
7604 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7606 bset->dim->nparam = 0;
7607 bset->dim->n_out = nparam;
7608 bset = isl_basic_set_preimage(bset, mat);
7609 if (bset) {
7610 bset->dim->nparam = bset->dim->n_out;
7611 bset->dim->n_out = 0;
7613 return bset;
7614 error:
7615 isl_mat_free(mat);
7616 isl_basic_set_free(bset);
7617 return NULL;
7620 /* Apply a preimage specified by "mat" on the parameters of "set".
7621 * set is assumed to have only parameters and divs.
7623 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7624 __isl_take isl_mat *mat)
7626 isl_space *space;
7627 isl_size nparam;
7629 nparam = isl_set_dim(set, isl_dim_param);
7630 if (nparam < 0 || !mat)
7631 goto error;
7633 if (mat->n_row != 1 + nparam)
7634 isl_die(isl_set_get_ctx(set), isl_error_internal,
7635 "unexpected number of rows", goto error);
7637 space = isl_set_get_space(set);
7638 space = isl_space_move_dims(space, isl_dim_set, 0,
7639 isl_dim_param, 0, nparam);
7640 set = isl_set_reset_space(set, space);
7641 set = isl_set_preimage(set, mat);
7642 nparam = isl_set_dim(set, isl_dim_out);
7643 if (nparam < 0)
7644 set = isl_set_free(set);
7645 space = isl_set_get_space(set);
7646 space = isl_space_move_dims(space, isl_dim_param, 0,
7647 isl_dim_out, 0, nparam);
7648 set = isl_set_reset_space(set, space);
7649 return set;
7650 error:
7651 isl_mat_free(mat);
7652 isl_set_free(set);
7653 return NULL;
7656 /* Intersect the basic set "bset" with the affine space specified by the
7657 * equalities in "eq".
7659 static __isl_give isl_basic_set *basic_set_append_equalities(
7660 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7662 int i, k;
7663 unsigned len;
7665 if (!bset || !eq)
7666 goto error;
7668 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7669 if (!bset)
7670 goto error;
7672 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7673 for (i = 0; i < eq->n_row; ++i) {
7674 k = isl_basic_set_alloc_equality(bset);
7675 if (k < 0)
7676 goto error;
7677 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7678 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7680 isl_mat_free(eq);
7682 bset = isl_basic_set_gauss(bset, NULL);
7683 bset = isl_basic_set_finalize(bset);
7685 return bset;
7686 error:
7687 isl_mat_free(eq);
7688 isl_basic_set_free(bset);
7689 return NULL;
7692 /* Intersect the set "set" with the affine space specified by the
7693 * equalities in "eq".
7695 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7696 __isl_take isl_mat *eq)
7698 int i;
7700 if (!set || !eq)
7701 goto error;
7703 for (i = 0; i < set->n; ++i) {
7704 set->p[i] = basic_set_append_equalities(set->p[i],
7705 isl_mat_copy(eq));
7706 if (!set->p[i])
7707 goto error;
7709 isl_mat_free(eq);
7710 return set;
7711 error:
7712 isl_mat_free(eq);
7713 isl_set_free(set);
7714 return NULL;
7717 /* Given a basic set "bset" that only involves parameters and existentially
7718 * quantified variables, return the index of the first equality
7719 * that only involves parameters. If there is no such equality then
7720 * return bset->n_eq.
7722 * This function assumes that isl_basic_set_gauss has been called on "bset".
7724 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7726 int i, j;
7727 isl_size nparam, n_div;
7729 nparam = isl_basic_set_dim(bset, isl_dim_param);
7730 n_div = isl_basic_set_dim(bset, isl_dim_div);
7731 if (nparam < 0 || n_div < 0)
7732 return -1;
7734 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7735 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7736 ++i;
7739 return i;
7742 /* Compute an explicit representation for the existentially quantified
7743 * variables in "bset" by computing the "minimal value" of the set
7744 * variables. Since there are no set variables, the computation of
7745 * the minimal value essentially computes an explicit representation
7746 * of the non-empty part(s) of "bset".
7748 * The input only involves parameters and existentially quantified variables.
7749 * All equalities among parameters have been removed.
7751 * Since the existentially quantified variables in the result are in general
7752 * going to be different from those in the input, we first replace
7753 * them by the minimal number of variables based on their equalities.
7754 * This should simplify the parametric integer programming.
7756 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7758 isl_morph *morph1, *morph2;
7759 isl_set *set;
7760 isl_size n;
7762 if (!bset)
7763 return NULL;
7764 if (bset->n_eq == 0)
7765 return isl_basic_set_lexmin_compute_divs(bset);
7767 morph1 = isl_basic_set_parameter_compression(bset);
7768 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7769 bset = isl_basic_set_lift(bset);
7770 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7771 bset = isl_morph_basic_set(morph2, bset);
7772 n = isl_basic_set_dim(bset, isl_dim_set);
7773 if (n < 0)
7774 bset = isl_basic_set_free(bset);
7775 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7777 set = isl_basic_set_lexmin_compute_divs(bset);
7779 set = isl_morph_set(isl_morph_inverse(morph1), set);
7781 return set;
7784 /* Project the given basic set onto its parameter domain, possibly introducing
7785 * new, explicit, existential variables in the constraints.
7786 * The input has parameters and (possibly implicit) existential variables.
7787 * The output has the same parameters, but only
7788 * explicit existentially quantified variables.
7790 * The actual projection is performed by pip, but pip doesn't seem
7791 * to like equalities very much, so we first remove the equalities
7792 * among the parameters by performing a variable compression on
7793 * the parameters. Afterward, an inverse transformation is performed
7794 * and the equalities among the parameters are inserted back in.
7796 * The variable compression on the parameters may uncover additional
7797 * equalities that were only implicit before. We therefore check
7798 * if there are any new parameter equalities in the result and
7799 * if so recurse. The removal of parameter equalities is required
7800 * for the parameter compression performed by base_compute_divs.
7802 static __isl_give isl_set *parameter_compute_divs(
7803 __isl_take isl_basic_set *bset)
7805 int i;
7806 struct isl_mat *eq;
7807 struct isl_mat *T, *T2;
7808 struct isl_set *set;
7809 isl_size nparam;
7811 bset = isl_basic_set_cow(bset);
7812 if (!bset)
7813 return NULL;
7815 if (bset->n_eq == 0)
7816 return base_compute_divs(bset);
7818 bset = isl_basic_set_gauss(bset, NULL);
7819 if (!bset)
7820 return NULL;
7821 if (isl_basic_set_plain_is_empty(bset))
7822 return isl_set_from_basic_set(bset);
7824 i = first_parameter_equality(bset);
7825 if (i == bset->n_eq)
7826 return base_compute_divs(bset);
7828 nparam = isl_basic_set_dim(bset, isl_dim_param);
7829 if (nparam < 0)
7830 return isl_set_from_basic_set(isl_basic_set_free(bset));
7831 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7832 0, 1 + nparam);
7833 eq = isl_mat_cow(eq);
7834 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7835 if (T && T->n_col == 0) {
7836 isl_mat_free(T);
7837 isl_mat_free(T2);
7838 isl_mat_free(eq);
7839 bset = isl_basic_set_set_to_empty(bset);
7840 return isl_set_from_basic_set(bset);
7842 bset = basic_set_parameter_preimage(bset, T);
7844 i = first_parameter_equality(bset);
7845 if (!bset)
7846 set = NULL;
7847 else if (i == bset->n_eq)
7848 set = base_compute_divs(bset);
7849 else
7850 set = parameter_compute_divs(bset);
7851 set = set_parameter_preimage(set, T2);
7852 set = set_append_equalities(set, eq);
7853 return set;
7856 /* Insert the divs from "ls" before those of "bmap".
7858 * The number of columns is not changed, which means that the last
7859 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7860 * The caller is responsible for removing the same number of dimensions
7861 * from the space of "bmap".
7863 static __isl_give isl_basic_map *insert_divs_from_local_space(
7864 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7866 int i;
7867 isl_size n_div;
7868 int old_n_div;
7870 n_div = isl_local_space_dim(ls, isl_dim_div);
7871 if (n_div < 0)
7872 return isl_basic_map_free(bmap);
7873 if (n_div == 0)
7874 return bmap;
7876 old_n_div = bmap->n_div;
7877 bmap = insert_div_rows(bmap, n_div);
7878 if (!bmap)
7879 return NULL;
7881 for (i = 0; i < n_div; ++i) {
7882 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7883 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7886 return bmap;
7889 /* Replace the space of "bmap" by the space and divs of "ls".
7891 * If "ls" has any divs, then we simplify the result since we may
7892 * have discovered some additional equalities that could simplify
7893 * the div expressions.
7895 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7896 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7898 isl_size n_div;
7900 bmap = isl_basic_map_cow(bmap);
7901 n_div = isl_local_space_dim(ls, isl_dim_div);
7902 if (!bmap || n_div < 0)
7903 goto error;
7905 bmap = insert_divs_from_local_space(bmap, ls);
7906 if (!bmap)
7907 goto error;
7909 isl_space_free(bmap->dim);
7910 bmap->dim = isl_local_space_get_space(ls);
7911 if (!bmap->dim)
7912 goto error;
7914 isl_local_space_free(ls);
7915 if (n_div > 0)
7916 bmap = isl_basic_map_simplify(bmap);
7917 bmap = isl_basic_map_finalize(bmap);
7918 return bmap;
7919 error:
7920 isl_basic_map_free(bmap);
7921 isl_local_space_free(ls);
7922 return NULL;
7925 /* Replace the space of "map" by the space and divs of "ls".
7927 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7928 __isl_take isl_local_space *ls)
7930 int i;
7932 map = isl_map_cow(map);
7933 if (!map || !ls)
7934 goto error;
7936 for (i = 0; i < map->n; ++i) {
7937 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7938 isl_local_space_copy(ls));
7939 if (!map->p[i])
7940 goto error;
7942 isl_space_free(isl_map_take_space(map));
7943 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7945 isl_local_space_free(ls);
7946 return map;
7947 error:
7948 isl_local_space_free(ls);
7949 isl_map_free(map);
7950 return NULL;
7953 /* Compute an explicit representation for the existentially
7954 * quantified variables for which do not know any explicit representation yet.
7956 * We first sort the existentially quantified variables so that the
7957 * existentially quantified variables for which we already have an explicit
7958 * representation are placed before those for which we do not.
7959 * The input dimensions, the output dimensions and the existentially
7960 * quantified variables for which we already have an explicit
7961 * representation are then turned into parameters.
7962 * compute_divs returns a map with the same parameters and
7963 * no input or output dimensions and the dimension specification
7964 * is reset to that of the input, including the existentially quantified
7965 * variables for which we already had an explicit representation.
7967 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7969 struct isl_basic_set *bset;
7970 struct isl_set *set;
7971 struct isl_map *map;
7972 isl_space *space;
7973 isl_local_space *ls;
7974 isl_size nparam;
7975 isl_size n_in;
7976 isl_size n_out;
7977 int n_known;
7978 int i;
7980 bmap = isl_basic_map_sort_divs(bmap);
7981 bmap = isl_basic_map_cow(bmap);
7982 if (!bmap)
7983 return NULL;
7985 n_known = isl_basic_map_first_unknown_div(bmap);
7986 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7987 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7988 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7989 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7990 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7992 space = isl_space_set_alloc(bmap->ctx,
7993 nparam + n_in + n_out + n_known, 0);
7994 if (!space)
7995 goto error;
7997 ls = isl_basic_map_get_local_space(bmap);
7998 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7999 n_known, bmap->n_div - n_known);
8000 if (n_known > 0) {
8001 for (i = n_known; i < bmap->n_div; ++i)
8002 swap_div(bmap, i - n_known, i);
8003 bmap->n_div -= n_known;
8004 bmap->extra -= n_known;
8006 bmap = isl_basic_map_reset_space(bmap, space);
8007 bset = bset_from_bmap(bmap);
8009 set = parameter_compute_divs(bset);
8010 map = set_to_map(set);
8011 map = replace_space_by_local_space(map, ls);
8013 return map;
8014 error:
8015 isl_basic_map_free(bmap);
8016 return NULL;
8019 /* Remove the explicit representation of local variable "div",
8020 * if there is any.
8022 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8023 __isl_take isl_basic_map *bmap, int div)
8025 isl_bool unknown;
8027 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8028 if (unknown < 0)
8029 return isl_basic_map_free(bmap);
8030 if (unknown)
8031 return bmap;
8033 bmap = isl_basic_map_cow(bmap);
8034 if (!bmap)
8035 return NULL;
8036 isl_int_set_si(bmap->div[div][0], 0);
8037 return bmap;
8040 /* Is local variable "div" of "bmap" marked as not having an explicit
8041 * representation?
8042 * Note that even if "div" is not marked in this way and therefore
8043 * has an explicit representation, this representation may still
8044 * depend (indirectly) on other local variables that do not
8045 * have an explicit representation.
8047 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8048 int div)
8050 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8051 return isl_bool_error;
8052 return isl_int_is_zero(bmap->div[div][0]);
8055 /* Return the position of the first local variable that does not
8056 * have an explicit representation.
8057 * Return the total number of local variables if they all have
8058 * an explicit representation.
8059 * Return -1 on error.
8061 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8063 int i;
8065 if (!bmap)
8066 return -1;
8068 for (i = 0; i < bmap->n_div; ++i) {
8069 if (!isl_basic_map_div_is_known(bmap, i))
8070 return i;
8072 return bmap->n_div;
8075 /* Return the position of the first local variable that does not
8076 * have an explicit representation.
8077 * Return the total number of local variables if they all have
8078 * an explicit representation.
8079 * Return -1 on error.
8081 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8083 return isl_basic_map_first_unknown_div(bset);
8086 /* Does "bmap" have an explicit representation for all local variables?
8088 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8090 int first;
8091 isl_size n;
8093 n = isl_basic_map_dim(bmap, isl_dim_div);
8094 first = isl_basic_map_first_unknown_div(bmap);
8095 if (n < 0 || first < 0)
8096 return isl_bool_error;
8097 return first == n;
8100 /* Do all basic maps in "map" have an explicit representation
8101 * for all local variables?
8103 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8105 int i;
8107 if (!map)
8108 return isl_bool_error;
8110 for (i = 0; i < map->n; ++i) {
8111 int known = isl_basic_map_divs_known(map->p[i]);
8112 if (known <= 0)
8113 return known;
8116 return isl_bool_true;
8119 /* If bmap contains any unknown divs, then compute explicit
8120 * expressions for them. However, this computation may be
8121 * quite expensive, so first try to remove divs that aren't
8122 * strictly needed.
8124 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8126 int known;
8127 struct isl_map *map;
8129 known = isl_basic_map_divs_known(bmap);
8130 if (known < 0)
8131 goto error;
8132 if (known)
8133 return isl_map_from_basic_map(bmap);
8135 bmap = isl_basic_map_drop_redundant_divs(bmap);
8137 known = isl_basic_map_divs_known(bmap);
8138 if (known < 0)
8139 goto error;
8140 if (known)
8141 return isl_map_from_basic_map(bmap);
8143 map = compute_divs(bmap);
8144 return map;
8145 error:
8146 isl_basic_map_free(bmap);
8147 return NULL;
8150 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8152 int i;
8153 int known;
8154 struct isl_map *res;
8156 if (!map)
8157 return NULL;
8158 if (map->n == 0)
8159 return map;
8161 known = isl_map_divs_known(map);
8162 if (known < 0) {
8163 isl_map_free(map);
8164 return NULL;
8166 if (known)
8167 return map;
8169 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8170 for (i = 1 ; i < map->n; ++i) {
8171 struct isl_map *r2;
8172 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8173 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8174 res = isl_map_union_disjoint(res, r2);
8175 else
8176 res = isl_map_union(res, r2);
8178 isl_map_free(map);
8180 return res;
8183 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8185 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8188 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8190 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8193 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8195 isl_space *space;
8196 isl_size n_out;
8198 n_out = isl_map_dim(map, isl_dim_out);
8199 if (n_out < 0)
8200 return set_from_map(isl_map_free(map));
8201 space = isl_space_domain(isl_map_get_space(map));
8203 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8205 return set_from_map(isl_map_reset_space(map, space));
8208 /* Return the union of "map1" and "map2", where we assume for now that
8209 * "map1" and "map2" are disjoint. Note that the basic maps inside
8210 * "map1" or "map2" may not be disjoint from each other.
8211 * Also note that this function is also called from isl_map_union,
8212 * which takes care of handling the situation where "map1" and "map2"
8213 * may not be disjoint.
8215 * If one of the inputs is empty, we can simply return the other input.
8216 * Similarly, if one of the inputs is universal, then it is equal to the union.
8218 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8219 __isl_take isl_map *map2)
8221 int i;
8222 unsigned flags = 0;
8223 struct isl_map *map = NULL;
8224 int is_universe;
8226 if (isl_map_check_equal_space(map1, map2) < 0)
8227 goto error;
8229 if (map1->n == 0) {
8230 isl_map_free(map1);
8231 return map2;
8233 if (map2->n == 0) {
8234 isl_map_free(map2);
8235 return map1;
8238 is_universe = isl_map_plain_is_universe(map1);
8239 if (is_universe < 0)
8240 goto error;
8241 if (is_universe) {
8242 isl_map_free(map2);
8243 return map1;
8246 is_universe = isl_map_plain_is_universe(map2);
8247 if (is_universe < 0)
8248 goto error;
8249 if (is_universe) {
8250 isl_map_free(map1);
8251 return map2;
8254 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8255 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8256 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8258 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8259 map1->n + map2->n, flags);
8260 if (!map)
8261 goto error;
8262 for (i = 0; i < map1->n; ++i) {
8263 map = isl_map_add_basic_map(map,
8264 isl_basic_map_copy(map1->p[i]));
8265 if (!map)
8266 goto error;
8268 for (i = 0; i < map2->n; ++i) {
8269 map = isl_map_add_basic_map(map,
8270 isl_basic_map_copy(map2->p[i]));
8271 if (!map)
8272 goto error;
8274 isl_map_free(map1);
8275 isl_map_free(map2);
8276 return map;
8277 error:
8278 isl_map_free(map);
8279 isl_map_free(map1);
8280 isl_map_free(map2);
8281 return NULL;
8284 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8285 * guaranteed to be disjoint by the caller.
8287 * Note that this functions is called from within isl_map_make_disjoint,
8288 * so we have to be careful not to touch the constraints of the inputs
8289 * in any way.
8291 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8292 __isl_take isl_map *map2)
8294 isl_map_align_params_bin(&map1, &map2);
8295 return map_union_disjoint(map1, map2);
8298 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8299 * not be disjoint.
8301 * We currently simply call map_union_disjoint, the internal operation
8302 * of which does not really depend on the inputs being disjoint.
8303 * If the result contains more than one basic map, then we clear
8304 * the disjoint flag since the result may contain basic maps from
8305 * both inputs and these are not guaranteed to be disjoint.
8307 * As a special case, if "map1" and "map2" are obviously equal,
8308 * then we simply return "map1".
8310 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8311 __isl_take isl_map *map2)
8313 int equal;
8315 if (isl_map_align_params_bin(&map1, &map2) < 0)
8316 goto error;
8318 equal = isl_map_plain_is_equal(map1, map2);
8319 if (equal < 0)
8320 goto error;
8321 if (equal) {
8322 isl_map_free(map2);
8323 return map1;
8326 map1 = map_union_disjoint(map1, map2);
8327 if (!map1)
8328 return NULL;
8329 if (map1->n > 1)
8330 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8331 return map1;
8332 error:
8333 isl_map_free(map1);
8334 isl_map_free(map2);
8335 return NULL;
8338 __isl_give isl_set *isl_set_union_disjoint(
8339 __isl_take isl_set *set1, __isl_take isl_set *set2)
8341 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8342 set_to_map(set2)));
8345 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8346 __isl_take isl_set *set2)
8348 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8351 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8352 * the results in a map living in "space".
8354 * "map" and "set" are assumed to be compatible and non-NULL.
8356 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8357 __isl_take isl_space *space, __isl_take isl_set *set,
8358 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8359 __isl_take isl_basic_set *bset))
8361 unsigned flags = 0;
8362 struct isl_map *result;
8363 int i, j;
8365 if (isl_set_plain_is_universe(set)) {
8366 isl_set_free(set);
8367 return isl_map_reset_equal_dim_space(map, space);
8370 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8371 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8372 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8374 result = isl_map_alloc_space(space, map->n * set->n, flags);
8375 for (i = 0; result && i < map->n; ++i)
8376 for (j = 0; j < set->n; ++j) {
8377 result = isl_map_add_basic_map(result,
8378 fn(isl_basic_map_copy(map->p[i]),
8379 isl_basic_set_copy(set->p[j])));
8380 if (!result)
8381 break;
8384 isl_map_free(map);
8385 isl_set_free(set);
8386 return result;
8389 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8390 __isl_take isl_set *set)
8392 isl_bool ok;
8393 isl_space *space;
8395 isl_map_align_params_set(&map, &set);
8396 ok = isl_map_compatible_range(map, set);
8397 if (ok < 0)
8398 goto error;
8399 if (!ok)
8400 isl_die(set->ctx, isl_error_invalid,
8401 "incompatible spaces", goto error);
8403 space = isl_map_get_space(map);
8404 return map_intersect_set(map, space, set,
8405 &isl_basic_map_intersect_range);
8406 error:
8407 isl_map_free(map);
8408 isl_set_free(set);
8409 return NULL;
8412 /* Intersect the domain of "map" with "set".
8414 * If the domain dimensions of "map" do not have any identifiers,
8415 * then copy them over from "set".
8417 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8418 __isl_take isl_set *set)
8420 isl_bool ok;
8421 isl_space *space;
8423 isl_map_align_params_set(&map, &set);
8424 ok = isl_map_compatible_domain(map, set);
8425 if (ok < 0)
8426 goto error;
8427 if (!ok)
8428 isl_die(set->ctx, isl_error_invalid,
8429 "incompatible spaces", goto error);
8431 space = isl_map_get_space(map);
8432 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8433 isl_set_peek_space(set), isl_dim_set);
8434 return map_intersect_set(map, space, set,
8435 &isl_basic_map_intersect_domain);
8436 error:
8437 isl_map_free(map);
8438 isl_set_free(set);
8439 return NULL;
8442 #undef BASE
8443 #define BASE map
8444 static
8445 #include "isl_copy_tuple_id_templ.c"
8447 /* Data structure that specifies how isl_map_intersect_factor
8448 * should operate.
8450 * "preserve_type" is the tuple where the factor differs from
8451 * the input map and of which the identifiers needs
8452 * to be preserved explicitly.
8453 * "other_factor" is used to extract the space of the other factor
8454 * from the space of the product ("map").
8455 * "product" is used to combine the given factor and a universe map
8456 * in the space returned by "other_factor" to produce a map
8457 * that lives in the same space as the input map.
8459 struct isl_intersect_factor_control {
8460 enum isl_dim_type preserve_type;
8461 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8462 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8463 __isl_take isl_map *other);
8466 /* Given a map "map" in some product space and a map "factor"
8467 * living in some factor space, return the intersection.
8469 * After aligning the parameters,
8470 * the map "factor" is first extended to a map living in the same space
8471 * as "map" and then a regular intersection is computed.
8473 * Note that the extension is computed as a product, which is anonymous
8474 * by default. If "map" has an identifier on the corresponding tuple,
8475 * then this identifier needs to be set on the product
8476 * before the intersection is computed.
8478 static __isl_give isl_map *isl_map_intersect_factor(
8479 __isl_take isl_map *map, __isl_take isl_map *factor,
8480 struct isl_intersect_factor_control *control)
8482 isl_bool equal;
8483 isl_space *space;
8484 isl_map *other, *product;
8486 equal = isl_map_has_equal_params(map, factor);
8487 if (equal < 0)
8488 goto error;
8489 if (!equal) {
8490 map = isl_map_align_params(map, isl_map_get_space(factor));
8491 factor = isl_map_align_params(factor, isl_map_get_space(map));
8494 space = isl_map_get_space(map);
8495 other = isl_map_universe(control->other_factor(space));
8496 product = control->product(factor, other);
8498 space = isl_map_peek_space(map);
8499 product = isl_map_copy_tuple_id(product, control->preserve_type,
8500 space, control->preserve_type);
8501 return map_intersect(map, product);
8502 error:
8503 isl_map_free(map);
8504 isl_map_free(factor);
8505 return NULL;
8508 /* Return the domain product of "map2" and "map1".
8510 static __isl_give isl_map *isl_map_reverse_domain_product(
8511 __isl_take isl_map *map1, __isl_take isl_map *map2)
8513 return isl_map_domain_product(map2, map1);
8516 /* Return the range product of "map2" and "map1".
8518 static __isl_give isl_map *isl_map_reverse_range_product(
8519 __isl_take isl_map *map1, __isl_take isl_map *map2)
8521 return isl_map_range_product(map2, map1);
8524 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8525 * in the space A -> C, return the intersection.
8527 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8528 __isl_take isl_map *map, __isl_take isl_map *factor)
8530 struct isl_intersect_factor_control control = {
8531 .preserve_type = isl_dim_in,
8532 .other_factor = isl_space_domain_factor_range,
8533 .product = isl_map_domain_product,
8536 return isl_map_intersect_factor(map, factor, &control);
8539 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8540 * in the space B -> C, return the intersection.
8542 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8543 __isl_take isl_map *map, __isl_take isl_map *factor)
8545 struct isl_intersect_factor_control control = {
8546 .preserve_type = isl_dim_in,
8547 .other_factor = isl_space_domain_factor_domain,
8548 .product = isl_map_reverse_domain_product,
8551 return isl_map_intersect_factor(map, factor, &control);
8554 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8555 * in the space A -> B, return the intersection.
8557 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8558 __isl_take isl_map *map, __isl_take isl_map *factor)
8560 struct isl_intersect_factor_control control = {
8561 .preserve_type = isl_dim_out,
8562 .other_factor = isl_space_range_factor_range,
8563 .product = isl_map_range_product,
8566 return isl_map_intersect_factor(map, factor, &control);
8569 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8570 * in the space A -> C, return the intersection.
8572 __isl_give isl_map *isl_map_intersect_range_factor_range(
8573 __isl_take isl_map *map, __isl_take isl_map *factor)
8575 struct isl_intersect_factor_control control = {
8576 .preserve_type = isl_dim_out,
8577 .other_factor = isl_space_range_factor_domain,
8578 .product = isl_map_reverse_range_product,
8581 return isl_map_intersect_factor(map, factor, &control);
8584 /* Given a set "set" in a space [A -> B] and a set "domain"
8585 * in the space A, return the intersection.
8587 * The set "domain" is first extended to a set living in the space
8588 * [A -> B] and then a regular intersection is computed.
8590 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8591 __isl_take isl_set *domain)
8593 struct isl_intersect_factor_control control = {
8594 .preserve_type = isl_dim_set,
8595 .other_factor = isl_space_factor_range,
8596 .product = isl_map_range_product,
8599 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8600 set_to_map(domain), &control));
8603 /* Given a set "set" in a space [A -> B] and a set "range"
8604 * in the space B, return the intersection.
8606 * The set "range" is first extended to a set living in the space
8607 * [A -> B] and then a regular intersection is computed.
8609 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8610 __isl_take isl_set *range)
8612 struct isl_intersect_factor_control control = {
8613 .preserve_type = isl_dim_set,
8614 .other_factor = isl_space_factor_domain,
8615 .product = isl_map_reverse_range_product,
8618 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8619 set_to_map(range), &control));
8622 #undef BASE
8623 #define BASE set
8624 static
8625 #include "isl_copy_tuple_id_templ.c"
8627 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8628 * in the space A, return the intersection.
8630 * The set "domain" is extended to a set living in the space [A -> B] and
8631 * the domain of "map" is intersected with this set.
8633 * If "map" has an identifier on the domain tuple,
8634 * then this identifier needs to be set on this product
8635 * before the intersection is computed.
8637 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8638 __isl_take isl_map *map, __isl_take isl_set *domain)
8640 isl_space *space;
8641 isl_set *factor;
8643 isl_map_align_params_set(&map, &domain);
8644 space = isl_map_get_space(map);
8645 space = isl_space_domain_wrapped_range(space);
8646 factor = isl_set_universe(space);
8647 domain = isl_set_product(domain, factor);
8648 space = isl_map_peek_space(map);
8649 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
8650 return isl_map_intersect_domain(map, domain);
8653 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8654 * in the space B, return the intersection.
8656 * The set "domain" is extended to a set living in the space [B -> C] and
8657 * the range of "map" is intersected with this set.
8659 * If "map" has an identifier on the range tuple,
8660 * then this identifier needs to be set on this product
8661 * before the intersection is computed.
8663 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8664 __isl_take isl_map *map, __isl_take isl_set *domain)
8666 isl_space *space;
8667 isl_set *factor;
8669 isl_map_align_params_set(&map, &domain);
8670 space = isl_map_get_space(map);
8671 space = isl_space_range_wrapped_range(space);
8672 factor = isl_set_universe(space);
8673 domain = isl_set_product(domain, factor);
8674 space = isl_map_peek_space(map);
8675 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
8676 return isl_map_intersect_range(map, domain);
8679 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8680 __isl_take isl_map *map2)
8682 if (isl_map_align_params_bin(&map1, &map2) < 0)
8683 goto error;
8684 map1 = isl_map_reverse(map1);
8685 map1 = isl_map_apply_range(map1, map2);
8686 return isl_map_reverse(map1);
8687 error:
8688 isl_map_free(map1);
8689 isl_map_free(map2);
8690 return NULL;
8693 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8694 __isl_take isl_map *map2)
8696 isl_space *space;
8697 struct isl_map *result;
8698 int i, j;
8700 if (isl_map_align_params_bin(&map1, &map2) < 0)
8701 goto error;
8703 space = isl_space_join(isl_space_copy(map1->dim),
8704 isl_space_copy(map2->dim));
8706 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8707 if (!result)
8708 goto error;
8709 for (i = 0; i < map1->n; ++i)
8710 for (j = 0; j < map2->n; ++j) {
8711 result = isl_map_add_basic_map(result,
8712 isl_basic_map_apply_range(
8713 isl_basic_map_copy(map1->p[i]),
8714 isl_basic_map_copy(map2->p[j])));
8715 if (!result)
8716 goto error;
8718 isl_map_free(map1);
8719 isl_map_free(map2);
8720 if (result && result->n <= 1)
8721 ISL_F_SET(result, ISL_MAP_DISJOINT);
8722 return result;
8723 error:
8724 isl_map_free(map1);
8725 isl_map_free(map2);
8726 return NULL;
8729 /* Is "bmap" a transformation, i.e.,
8730 * does it relate elements from the same space.
8732 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8734 isl_space *space;
8736 space = isl_basic_map_peek_space(bmap);
8737 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8740 /* Check that "bmap" is a transformation, i.e.,
8741 * that it relates elements from the same space.
8743 static isl_stat isl_basic_map_check_transformation(
8744 __isl_keep isl_basic_map *bmap)
8746 isl_bool equal;
8748 equal = isl_basic_map_is_transformation(bmap);
8749 if (equal < 0)
8750 return isl_stat_error;
8751 if (!equal)
8752 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8753 "domain and range don't match", return isl_stat_error);
8754 return isl_stat_ok;
8758 * returns range - domain
8760 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8762 isl_space *target_space;
8763 struct isl_basic_set *bset;
8764 isl_size dim;
8765 isl_size nparam;
8766 isl_size total;
8767 int i;
8769 if (isl_basic_map_check_transformation(bmap) < 0)
8770 return isl_basic_map_free(bmap);
8771 dim = isl_basic_map_dim(bmap, isl_dim_in);
8772 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8773 if (dim < 0 || nparam < 0)
8774 goto error;
8775 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8776 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8777 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8778 total = isl_basic_map_dim(bmap, isl_dim_all);
8779 if (total < 0)
8780 bmap = isl_basic_map_free(bmap);
8781 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8782 for (i = 0; i < dim; ++i) {
8783 int j = isl_basic_map_alloc_equality(bmap);
8784 if (j < 0) {
8785 bmap = isl_basic_map_free(bmap);
8786 break;
8788 isl_seq_clr(bmap->eq[j], 1 + total);
8789 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8790 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8791 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8793 bset = isl_basic_map_domain(bmap);
8794 bset = isl_basic_set_reset_space(bset, target_space);
8795 return bset;
8796 error:
8797 isl_basic_map_free(bmap);
8798 return NULL;
8801 /* Is the tuple of type "type1" of "map" the same as
8802 * the tuple of type "type2" of "space"?
8804 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8805 enum isl_dim_type type1, __isl_keep isl_space *space,
8806 enum isl_dim_type type2)
8808 isl_space *map_space;
8810 map_space = isl_map_peek_space(map);
8811 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8814 /* Is the tuple of type "type1" of "map1" the same as
8815 * the tuple of type "type2" of "map2"?
8817 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8818 enum isl_dim_type type1, __isl_keep isl_map *map2,
8819 enum isl_dim_type type2)
8821 isl_space *space1, *space2;
8823 space1 = isl_map_peek_space(map1);
8824 space2 = isl_map_peek_space(map2);
8825 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8828 /* Is the space of "obj" equal to "space", ignoring parameters?
8830 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8831 __isl_keep isl_space *space)
8833 isl_space *map_space;
8835 map_space = isl_map_peek_space(map);
8836 return isl_space_has_equal_tuples(map_space, space);
8839 /* Check that "map" is a transformation, i.e.,
8840 * that it relates elements from the same space.
8842 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8844 isl_bool equal;
8846 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8847 if (equal < 0)
8848 return isl_stat_error;
8849 if (!equal)
8850 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8851 "domain and range don't match", return isl_stat_error);
8852 return isl_stat_ok;
8856 * returns range - domain
8858 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8860 int i;
8861 isl_space *space;
8862 struct isl_set *result;
8864 if (isl_map_check_transformation(map) < 0)
8865 goto error;
8866 space = isl_map_get_space(map);
8867 space = isl_space_domain(space);
8868 result = isl_set_alloc_space(space, map->n, 0);
8869 if (!result)
8870 goto error;
8871 for (i = 0; i < map->n; ++i)
8872 result = isl_set_add_basic_set(result,
8873 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8874 isl_map_free(map);
8875 return result;
8876 error:
8877 isl_map_free(map);
8878 return NULL;
8882 * returns [domain -> range] -> range - domain
8884 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8885 __isl_take isl_basic_map *bmap)
8887 int i, k;
8888 isl_space *space;
8889 isl_basic_map *domain;
8890 isl_size nparam, n;
8891 isl_size total;
8893 if (isl_basic_map_check_transformation(bmap) < 0)
8894 return isl_basic_map_free(bmap);
8896 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8897 n = isl_basic_map_dim(bmap, isl_dim_in);
8898 if (nparam < 0 || n < 0)
8899 return isl_basic_map_free(bmap);
8901 space = isl_basic_map_get_space(bmap);
8902 space = isl_space_from_range(isl_space_domain(space));
8903 domain = isl_basic_map_universe(space);
8905 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8906 bmap = isl_basic_map_apply_range(bmap, domain);
8907 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8909 total = isl_basic_map_dim(bmap, isl_dim_all);
8910 if (total < 0)
8911 return isl_basic_map_free(bmap);
8913 for (i = 0; i < n; ++i) {
8914 k = isl_basic_map_alloc_equality(bmap);
8915 if (k < 0)
8916 goto error;
8917 isl_seq_clr(bmap->eq[k], 1 + total);
8918 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8919 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8920 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8923 bmap = isl_basic_map_gauss(bmap, NULL);
8924 return isl_basic_map_finalize(bmap);
8925 error:
8926 isl_basic_map_free(bmap);
8927 return NULL;
8931 * returns [domain -> range] -> range - domain
8933 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8935 if (isl_map_check_transformation(map) < 0)
8936 return isl_map_free(map);
8938 return isl_map_transform(map, &isl_space_range_map,
8939 &isl_basic_map_deltas_map);
8942 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8944 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8946 isl_space *space;
8947 isl_map *map;
8949 space = isl_space_map_from_set(isl_set_get_space(deltas));
8950 map = isl_map_deltas_map(isl_map_universe(space));
8951 map = isl_map_intersect_range(map, deltas);
8953 return isl_set_unwrap(isl_map_domain(map));
8956 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8958 isl_size n_in, n_out;
8960 n_in = isl_space_dim(space, isl_dim_in);
8961 n_out = isl_space_dim(space, isl_dim_out);
8962 if (n_in < 0 || n_out < 0)
8963 goto error;
8964 if (n_in != n_out)
8965 isl_die(space->ctx, isl_error_invalid,
8966 "number of input and output dimensions needs to be "
8967 "the same", goto error);
8968 return isl_basic_map_equal(space, n_in);
8969 error:
8970 isl_space_free(space);
8971 return NULL;
8974 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8976 return isl_map_from_basic_map(isl_basic_map_identity(space));
8979 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8981 isl_space *space = isl_set_get_space(set);
8982 isl_map *id;
8983 id = isl_map_identity(isl_space_map_from_set(space));
8984 return isl_map_intersect_range(id, set);
8987 /* Construct a basic set with all set dimensions having only non-negative
8988 * values.
8990 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8991 __isl_take isl_space *space)
8993 int i;
8994 isl_size nparam;
8995 isl_size dim;
8996 isl_size total;
8997 struct isl_basic_set *bset;
8999 nparam = isl_space_dim(space, isl_dim_param);
9000 dim = isl_space_dim(space, isl_dim_set);
9001 total = isl_space_dim(space, isl_dim_all);
9002 if (nparam < 0 || dim < 0 || total < 0)
9003 space = isl_space_free(space);
9004 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9005 if (!bset)
9006 return NULL;
9007 for (i = 0; i < dim; ++i) {
9008 int k = isl_basic_set_alloc_inequality(bset);
9009 if (k < 0)
9010 goto error;
9011 isl_seq_clr(bset->ineq[k], 1 + total);
9012 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9014 return bset;
9015 error:
9016 isl_basic_set_free(bset);
9017 return NULL;
9020 /* Construct the half-space x_pos >= 0.
9022 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9023 int pos)
9025 int k;
9026 isl_size total;
9027 isl_basic_set *nonneg;
9029 total = isl_space_dim(space, isl_dim_all);
9030 if (total < 0)
9031 space = isl_space_free(space);
9032 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9033 k = isl_basic_set_alloc_inequality(nonneg);
9034 if (k < 0)
9035 goto error;
9036 isl_seq_clr(nonneg->ineq[k], 1 + total);
9037 isl_int_set_si(nonneg->ineq[k][pos], 1);
9039 return isl_basic_set_finalize(nonneg);
9040 error:
9041 isl_basic_set_free(nonneg);
9042 return NULL;
9045 /* Construct the half-space x_pos <= -1.
9047 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9048 int pos)
9050 int k;
9051 isl_size total;
9052 isl_basic_set *neg;
9054 total = isl_space_dim(space, isl_dim_all);
9055 if (total < 0)
9056 space = isl_space_free(space);
9057 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9058 k = isl_basic_set_alloc_inequality(neg);
9059 if (k < 0)
9060 goto error;
9061 isl_seq_clr(neg->ineq[k], 1 + total);
9062 isl_int_set_si(neg->ineq[k][0], -1);
9063 isl_int_set_si(neg->ineq[k][pos], -1);
9065 return isl_basic_set_finalize(neg);
9066 error:
9067 isl_basic_set_free(neg);
9068 return NULL;
9071 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9072 enum isl_dim_type type, unsigned first, unsigned n)
9074 int i;
9075 unsigned offset;
9076 isl_basic_set *nonneg;
9077 isl_basic_set *neg;
9079 if (n == 0)
9080 return set;
9082 if (isl_set_check_range(set, type, first, n) < 0)
9083 return isl_set_free(set);
9085 offset = pos(set->dim, type);
9086 for (i = 0; i < n; ++i) {
9087 nonneg = nonneg_halfspace(isl_set_get_space(set),
9088 offset + first + i);
9089 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9091 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9094 return set;
9097 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9098 int len,
9099 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9100 void *user)
9102 isl_set *half;
9104 if (!set)
9105 return isl_stat_error;
9106 if (isl_set_plain_is_empty(set)) {
9107 isl_set_free(set);
9108 return isl_stat_ok;
9110 if (first == len)
9111 return fn(set, signs, user);
9113 signs[first] = 1;
9114 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9115 1 + first));
9116 half = isl_set_intersect(half, isl_set_copy(set));
9117 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9118 goto error;
9120 signs[first] = -1;
9121 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9122 1 + first));
9123 half = isl_set_intersect(half, set);
9124 return foreach_orthant(half, signs, first + 1, len, fn, user);
9125 error:
9126 isl_set_free(set);
9127 return isl_stat_error;
9130 /* Call "fn" on the intersections of "set" with each of the orthants
9131 * (except for obviously empty intersections). The orthant is identified
9132 * by the signs array, with each entry having value 1 or -1 according
9133 * to the sign of the corresponding variable.
9135 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9136 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9137 void *user)
9139 isl_size nparam;
9140 isl_size nvar;
9141 int *signs;
9142 isl_stat r;
9144 if (!set)
9145 return isl_stat_error;
9146 if (isl_set_plain_is_empty(set))
9147 return isl_stat_ok;
9149 nparam = isl_set_dim(set, isl_dim_param);
9150 nvar = isl_set_dim(set, isl_dim_set);
9151 if (nparam < 0 || nvar < 0)
9152 return isl_stat_error;
9154 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9156 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9157 fn, user);
9159 free(signs);
9161 return r;
9164 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9166 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9169 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9170 __isl_keep isl_basic_map *bmap2)
9172 isl_bool is_subset;
9173 struct isl_map *map1;
9174 struct isl_map *map2;
9176 if (!bmap1 || !bmap2)
9177 return isl_bool_error;
9179 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9180 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9182 is_subset = isl_map_is_subset(map1, map2);
9184 isl_map_free(map1);
9185 isl_map_free(map2);
9187 return is_subset;
9190 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9191 __isl_keep isl_basic_set *bset2)
9193 return isl_basic_map_is_subset(bset1, bset2);
9196 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9197 __isl_keep isl_basic_map *bmap2)
9199 isl_bool is_subset;
9201 if (!bmap1 || !bmap2)
9202 return isl_bool_error;
9203 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9204 if (is_subset != isl_bool_true)
9205 return is_subset;
9206 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9207 return is_subset;
9210 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9211 __isl_keep isl_basic_set *bset2)
9213 return isl_basic_map_is_equal(
9214 bset_to_bmap(bset1), bset_to_bmap(bset2));
9217 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9219 int i;
9220 int is_empty;
9222 if (!map)
9223 return isl_bool_error;
9224 for (i = 0; i < map->n; ++i) {
9225 is_empty = isl_basic_map_is_empty(map->p[i]);
9226 if (is_empty < 0)
9227 return isl_bool_error;
9228 if (!is_empty)
9229 return isl_bool_false;
9231 return isl_bool_true;
9234 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9236 return map ? map->n == 0 : isl_bool_error;
9239 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9241 return set ? set->n == 0 : isl_bool_error;
9244 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9246 return isl_map_is_empty(set_to_map(set));
9249 #undef TYPE
9250 #define TYPE isl_basic_map
9252 static
9253 #include "isl_type_has_equal_space_bin_templ.c"
9254 #include "isl_type_check_equal_space_templ.c"
9256 /* Check that "bset1" and "bset2" live in the same space,
9257 * reporting an error if they do not.
9259 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9260 __isl_keep isl_basic_set *bset2)
9262 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9263 bset_to_bmap(bset1));
9266 #undef TYPE
9267 #define TYPE isl_map
9269 #include "isl_type_has_equal_space_bin_templ.c"
9270 #include "isl_type_check_equal_space_templ.c"
9271 #include "isl_type_has_space_templ.c"
9273 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9274 __isl_keep isl_set *set2)
9276 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9279 #undef TYPE1
9280 #define TYPE1 isl_map
9281 #undef TYPE2
9282 #define TYPE2 isl_basic_map
9283 #undef TYPE_PAIR
9284 #define TYPE_PAIR isl_map_basic_map
9286 static
9287 #include "isl_type_has_equal_space_templ.c"
9288 #include "isl_type_check_equal_space_templ.c"
9290 /* Check that "set" and "bset" live in the same space,
9291 * reporting an error if they do not.
9293 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9294 __isl_keep isl_basic_set *bset)
9296 return isl_map_basic_map_check_equal_space(set_to_map(set),
9297 bset_to_bmap(bset));
9300 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9302 isl_bool is_subset;
9304 if (!map1 || !map2)
9305 return isl_bool_error;
9306 is_subset = isl_map_is_subset(map1, map2);
9307 if (is_subset != isl_bool_true)
9308 return is_subset;
9309 is_subset = isl_map_is_subset(map2, map1);
9310 return is_subset;
9313 /* Is "map1" equal to "map2"?
9315 * First check if they are obviously equal.
9316 * If not, then perform a more detailed analysis.
9318 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9320 isl_bool equal;
9322 equal = isl_map_plain_is_equal(map1, map2);
9323 if (equal < 0 || equal)
9324 return equal;
9325 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9328 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9329 __isl_keep isl_basic_map *bmap2)
9331 isl_bool is_subset;
9333 if (!bmap1 || !bmap2)
9334 return isl_bool_error;
9335 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9336 if (is_subset != isl_bool_true)
9337 return is_subset;
9338 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9339 return isl_bool_not(is_subset);
9342 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9343 __isl_keep isl_map *map2)
9345 isl_bool is_subset;
9347 if (!map1 || !map2)
9348 return isl_bool_error;
9349 is_subset = isl_map_is_subset(map1, map2);
9350 if (is_subset != isl_bool_true)
9351 return is_subset;
9352 is_subset = isl_map_is_subset(map2, map1);
9353 return isl_bool_not(is_subset);
9356 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9357 __isl_keep isl_set *set2)
9359 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9362 /* Is "bmap" obviously equal to the universe with the same space?
9364 * That is, does it not have any constraints?
9366 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9368 if (!bmap)
9369 return isl_bool_error;
9370 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9373 /* Is "bset" obviously equal to the universe with the same space?
9375 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9377 return isl_basic_map_plain_is_universe(bset);
9380 /* If "c" does not involve any existentially quantified variables,
9381 * then set *univ to false and abort
9383 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9385 isl_bool *univ = user;
9386 isl_size n;
9388 n = isl_constraint_dim(c, isl_dim_div);
9389 if (n < 0)
9390 c = isl_constraint_free(c);
9391 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9392 isl_constraint_free(c);
9393 if (*univ < 0 || !*univ)
9394 return isl_stat_error;
9395 return isl_stat_ok;
9398 /* Is "bmap" equal to the universe with the same space?
9400 * First check if it is obviously equal to the universe.
9401 * If not and if there are any constraints not involving
9402 * existentially quantified variables, then it is certainly
9403 * not equal to the universe.
9404 * Otherwise, check if the universe is a subset of "bmap".
9406 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9408 isl_size n_div;
9409 isl_bool univ;
9410 isl_basic_map *test;
9412 univ = isl_basic_map_plain_is_universe(bmap);
9413 if (univ < 0 || univ)
9414 return univ;
9415 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9416 if (n_div < 0)
9417 return isl_bool_error;
9418 if (n_div == 0)
9419 return isl_bool_false;
9420 univ = isl_bool_true;
9421 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9422 univ)
9423 return isl_bool_error;
9424 if (univ < 0 || !univ)
9425 return univ;
9426 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9427 univ = isl_basic_map_is_subset(test, bmap);
9428 isl_basic_map_free(test);
9429 return univ;
9432 /* Is "bset" equal to the universe with the same space?
9434 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9436 return isl_basic_map_is_universe(bset);
9439 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9441 int i;
9443 if (!map)
9444 return isl_bool_error;
9446 for (i = 0; i < map->n; ++i) {
9447 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9448 if (r < 0 || r)
9449 return r;
9452 return isl_bool_false;
9455 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9457 return isl_map_plain_is_universe(set_to_map(set));
9460 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9462 struct isl_basic_set *bset = NULL;
9463 struct isl_vec *sample = NULL;
9464 isl_bool empty, non_empty;
9466 if (!bmap)
9467 return isl_bool_error;
9469 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9470 return isl_bool_true;
9472 if (isl_basic_map_plain_is_universe(bmap))
9473 return isl_bool_false;
9475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9476 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9477 copy = isl_basic_map_remove_redundancies(copy);
9478 empty = isl_basic_map_plain_is_empty(copy);
9479 isl_basic_map_free(copy);
9480 return empty;
9483 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9484 if (non_empty < 0)
9485 return isl_bool_error;
9486 if (non_empty)
9487 return isl_bool_false;
9488 isl_vec_free(bmap->sample);
9489 bmap->sample = NULL;
9490 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9491 if (!bset)
9492 return isl_bool_error;
9493 sample = isl_basic_set_sample_vec(bset);
9494 if (!sample)
9495 return isl_bool_error;
9496 empty = sample->size == 0;
9497 isl_vec_free(bmap->sample);
9498 bmap->sample = sample;
9499 if (empty)
9500 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9502 return empty;
9505 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9507 if (!bmap)
9508 return isl_bool_error;
9509 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9512 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9514 if (!bset)
9515 return isl_bool_error;
9516 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9519 /* Is "bmap" known to be non-empty?
9521 * That is, is the cached sample still valid?
9523 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9525 isl_size total;
9527 if (!bmap)
9528 return isl_bool_error;
9529 if (!bmap->sample)
9530 return isl_bool_false;
9531 total = isl_basic_map_dim(bmap, isl_dim_all);
9532 if (total < 0)
9533 return isl_bool_error;
9534 if (bmap->sample->size != 1 + total)
9535 return isl_bool_false;
9536 return isl_basic_map_contains(bmap, bmap->sample);
9539 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9541 return isl_basic_map_is_empty(bset_to_bmap(bset));
9544 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9545 __isl_take isl_basic_map *bmap2)
9547 struct isl_map *map;
9549 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9550 goto error;
9552 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9553 if (!map)
9554 goto error;
9555 map = isl_map_add_basic_map(map, bmap1);
9556 map = isl_map_add_basic_map(map, bmap2);
9557 return map;
9558 error:
9559 isl_basic_map_free(bmap1);
9560 isl_basic_map_free(bmap2);
9561 return NULL;
9564 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9565 __isl_take isl_basic_set *bset2)
9567 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9568 bset_to_bmap(bset2)));
9571 /* Order divs such that any div only depends on previous divs */
9572 __isl_give isl_basic_map *isl_basic_map_order_divs(
9573 __isl_take isl_basic_map *bmap)
9575 int i;
9576 isl_size off;
9578 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9579 if (off < 0)
9580 return isl_basic_map_free(bmap);
9582 for (i = 0; i < bmap->n_div; ++i) {
9583 int pos;
9584 if (isl_int_is_zero(bmap->div[i][0]))
9585 continue;
9586 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9587 bmap->n_div-i);
9588 if (pos == -1)
9589 continue;
9590 if (pos == 0)
9591 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9592 "integer division depends on itself",
9593 return isl_basic_map_free(bmap));
9594 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9595 if (!bmap)
9596 return NULL;
9597 --i;
9599 return bmap;
9602 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9604 int i;
9606 if (!map)
9607 return 0;
9609 for (i = 0; i < map->n; ++i) {
9610 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9611 if (!map->p[i])
9612 goto error;
9615 return map;
9616 error:
9617 isl_map_free(map);
9618 return NULL;
9621 /* Sort the local variables of "bset".
9623 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9624 __isl_take isl_basic_set *bset)
9626 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9629 /* Apply the expansion computed by isl_merge_divs.
9630 * The expansion itself is given by "exp" while the resulting
9631 * list of divs is given by "div".
9633 * Move the integer divisions of "bmap" into the right position
9634 * according to "exp" and then introduce the additional integer
9635 * divisions, adding div constraints.
9636 * The moving should be done first to avoid moving coefficients
9637 * in the definitions of the extra integer divisions.
9639 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9640 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9642 int i, j;
9643 int n_div;
9645 bmap = isl_basic_map_cow(bmap);
9646 if (!bmap || !div)
9647 goto error;
9649 if (div->n_row < bmap->n_div)
9650 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9651 "not an expansion", goto error);
9653 n_div = bmap->n_div;
9654 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9655 2 * (div->n_row - n_div));
9657 for (i = n_div; i < div->n_row; ++i)
9658 if (isl_basic_map_alloc_div(bmap) < 0)
9659 goto error;
9661 for (j = n_div - 1; j >= 0; --j) {
9662 if (exp[j] == j)
9663 break;
9664 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9665 if (!bmap)
9666 goto error;
9668 j = 0;
9669 for (i = 0; i < div->n_row; ++i) {
9670 if (j < n_div && exp[j] == i) {
9671 j++;
9672 } else {
9673 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9674 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9675 continue;
9676 bmap = isl_basic_map_add_div_constraints(bmap, i);
9677 if (!bmap)
9678 goto error;
9682 isl_mat_free(div);
9683 return bmap;
9684 error:
9685 isl_basic_map_free(bmap);
9686 isl_mat_free(div);
9687 return NULL;
9690 /* Apply the expansion computed by isl_merge_divs.
9691 * The expansion itself is given by "exp" while the resulting
9692 * list of divs is given by "div".
9694 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9695 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9697 return isl_basic_map_expand_divs(bset, div, exp);
9700 /* Look for a div in dst that corresponds to the div "div" in src.
9701 * The divs before "div" in src and dst are assumed to be the same.
9703 * Return the position of the corresponding div in dst
9704 * if there is one. Otherwise, return a position beyond the integer divisions.
9705 * Return -1 on error.
9707 static int find_div(__isl_keep isl_basic_map *dst,
9708 __isl_keep isl_basic_map *src, unsigned div)
9710 int i;
9711 isl_size n_div;
9712 isl_size v_div;
9714 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9715 n_div = isl_basic_map_dim(dst, isl_dim_div);
9716 if (n_div < 0 || v_div < 0)
9717 return -1;
9718 isl_assert(dst->ctx, div <= n_div, return -1);
9719 for (i = div; i < n_div; ++i)
9720 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9721 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9722 n_div - div) == -1)
9723 return i;
9724 return n_div;
9727 /* Align the divs of "dst" to those of "src", adding divs from "src"
9728 * if needed. That is, make sure that the first src->n_div divs
9729 * of the result are equal to those of src.
9730 * The integer division of "src" are assumed to be ordered.
9732 * The integer divisions are swapped into the right position
9733 * (possibly after adding them first). This may result
9734 * in the remaining integer divisions appearing in the wrong order,
9735 * i.e., with some integer division appearing before
9736 * some other integer division on which it depends.
9737 * The integer divisions therefore need to be ordered.
9738 * This will not affect the integer divisions aligned to those of "src",
9739 * since "src" is assumed to have ordered integer divisions.
9741 * The result is not finalized as by design it will have redundant
9742 * divs if any divs from "src" were copied.
9744 __isl_give isl_basic_map *isl_basic_map_align_divs(
9745 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9747 int i;
9748 isl_bool known;
9749 int extended;
9750 isl_size v_div;
9751 isl_size dst_n_div;
9753 if (!dst || !src)
9754 return isl_basic_map_free(dst);
9756 if (src->n_div == 0)
9757 return dst;
9759 known = isl_basic_map_divs_known(src);
9760 if (known < 0)
9761 return isl_basic_map_free(dst);
9762 if (!known)
9763 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9764 "some src divs are unknown",
9765 return isl_basic_map_free(dst));
9767 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9768 if (v_div < 0)
9769 return isl_basic_map_free(dst);
9771 extended = 0;
9772 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9773 if (dst_n_div < 0)
9774 dst = isl_basic_map_free(dst);
9775 for (i = 0; i < src->n_div; ++i) {
9776 int j = find_div(dst, src, i);
9777 if (j < 0)
9778 dst = isl_basic_map_free(dst);
9779 if (j == dst_n_div) {
9780 if (!extended) {
9781 int extra = src->n_div - i;
9782 dst = isl_basic_map_cow(dst);
9783 if (!dst)
9784 return isl_basic_map_free(dst);
9785 dst = isl_basic_map_extend(dst,
9786 extra, 0, 2 * extra);
9787 extended = 1;
9789 j = isl_basic_map_alloc_div(dst);
9790 if (j < 0)
9791 return isl_basic_map_free(dst);
9792 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9793 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9794 dst_n_div++;
9795 dst = isl_basic_map_add_div_constraints(dst, j);
9796 if (!dst)
9797 return isl_basic_map_free(dst);
9799 if (j != i)
9800 dst = isl_basic_map_swap_div(dst, i, j);
9801 if (!dst)
9802 return isl_basic_map_free(dst);
9804 return isl_basic_map_order_divs(dst);
9807 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9809 int i;
9811 if (!map)
9812 return NULL;
9813 if (map->n == 0)
9814 return map;
9815 map = isl_map_compute_divs(map);
9816 map = isl_map_order_divs(map);
9817 map = isl_map_cow(map);
9818 if (!map)
9819 return NULL;
9821 for (i = 1; i < map->n; ++i)
9822 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9823 for (i = 1; i < map->n; ++i) {
9824 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9825 if (!map->p[i])
9826 return isl_map_free(map);
9829 map = isl_map_unmark_normalized(map);
9830 return map;
9833 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9835 return isl_map_align_divs_internal(map);
9838 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9840 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9843 /* Align the divs of the basic maps in "map" to those
9844 * of the basic maps in "list", as well as to the other basic maps in "map".
9845 * The elements in "list" are assumed to have known divs.
9847 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9848 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9850 int i;
9851 isl_size n;
9853 n = isl_basic_map_list_n_basic_map(list);
9854 map = isl_map_compute_divs(map);
9855 map = isl_map_cow(map);
9856 if (!map || n < 0)
9857 return isl_map_free(map);
9858 if (map->n == 0)
9859 return map;
9861 for (i = 0; i < n; ++i) {
9862 isl_basic_map *bmap;
9864 bmap = isl_basic_map_list_get_basic_map(list, i);
9865 bmap = isl_basic_map_order_divs(bmap);
9866 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9867 isl_basic_map_free(bmap);
9869 if (!map->p[0])
9870 return isl_map_free(map);
9872 return isl_map_align_divs_internal(map);
9875 /* Align the divs of each element of "list" to those of "bmap".
9876 * Both "bmap" and the elements of "list" are assumed to have known divs.
9878 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9879 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9881 int i;
9882 isl_size n;
9884 n = isl_basic_map_list_n_basic_map(list);
9885 if (n < 0 || !bmap)
9886 return isl_basic_map_list_free(list);
9888 for (i = 0; i < n; ++i) {
9889 isl_basic_map *bmap_i;
9891 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9892 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9893 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9896 return list;
9899 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9900 __isl_take isl_map *map)
9902 isl_bool ok;
9904 isl_map_align_params_set(&map, &set);
9905 ok = isl_map_compatible_domain(map, set);
9906 if (ok < 0)
9907 goto error;
9908 if (!ok)
9909 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9910 "incompatible spaces", goto error);
9911 map = isl_map_intersect_domain(map, set);
9912 set = isl_map_range(map);
9913 return set;
9914 error:
9915 isl_set_free(set);
9916 isl_map_free(map);
9917 return NULL;
9920 /* There is no need to cow as removing empty parts doesn't change
9921 * the meaning of the set.
9923 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9925 int i;
9927 if (!map)
9928 return NULL;
9930 for (i = map->n - 1; i >= 0; --i)
9931 map = remove_if_empty(map, i);
9933 return map;
9936 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9938 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9941 /* Create a binary relation that maps the shared initial "pos" dimensions
9942 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9944 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9945 __isl_keep isl_basic_set *bset2, int pos)
9947 isl_basic_map *bmap1;
9948 isl_basic_map *bmap2;
9950 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9951 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9952 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9953 isl_dim_out, 0, pos);
9954 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9955 isl_dim_out, 0, pos);
9956 return isl_basic_map_range_product(bmap1, bmap2);
9959 /* Given two basic sets bset1 and bset2, compute the maximal difference
9960 * between the values of dimension pos in bset1 and those in bset2
9961 * for any common value of the parameters and dimensions preceding pos.
9963 static enum isl_lp_result basic_set_maximal_difference_at(
9964 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9965 int pos, isl_int *opt)
9967 isl_basic_map *bmap1;
9968 struct isl_ctx *ctx;
9969 struct isl_vec *obj;
9970 isl_size total;
9971 isl_size nparam;
9972 isl_size dim1;
9973 enum isl_lp_result res;
9975 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9976 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9977 if (nparam < 0 || dim1 < 0 || !bset2)
9978 return isl_lp_error;
9980 bmap1 = join_initial(bset1, bset2, pos);
9981 total = isl_basic_map_dim(bmap1, isl_dim_all);
9982 if (total < 0)
9983 return isl_lp_error;
9985 ctx = bmap1->ctx;
9986 obj = isl_vec_alloc(ctx, 1 + total);
9987 if (!obj)
9988 goto error;
9989 isl_seq_clr(obj->block.data, 1 + total);
9990 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9991 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9992 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9993 opt, NULL, NULL);
9994 isl_basic_map_free(bmap1);
9995 isl_vec_free(obj);
9996 return res;
9997 error:
9998 isl_basic_map_free(bmap1);
9999 return isl_lp_error;
10002 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10003 * for any common value of the parameters and dimensions preceding pos
10004 * in both basic sets, the values of dimension pos in bset1 are
10005 * smaller or larger than those in bset2.
10007 * Returns
10008 * 1 if bset1 follows bset2
10009 * -1 if bset1 precedes bset2
10010 * 0 if bset1 and bset2 are incomparable
10011 * -2 if some error occurred.
10013 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10014 __isl_keep isl_basic_set *bset2, int pos)
10016 isl_int opt;
10017 enum isl_lp_result res;
10018 int cmp;
10020 isl_int_init(opt);
10022 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10024 if (res == isl_lp_empty)
10025 cmp = 0;
10026 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10027 res == isl_lp_unbounded)
10028 cmp = 1;
10029 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10030 cmp = -1;
10031 else
10032 cmp = -2;
10034 isl_int_clear(opt);
10035 return cmp;
10038 /* Given two basic sets bset1 and bset2, check whether
10039 * for any common value of the parameters and dimensions preceding pos
10040 * there is a value of dimension pos in bset1 that is larger
10041 * than a value of the same dimension in bset2.
10043 * Return
10044 * 1 if there exists such a pair
10045 * 0 if there is no such pair, but there is a pair of equal values
10046 * -1 otherwise
10047 * -2 if some error occurred.
10049 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10050 __isl_keep isl_basic_set *bset2, int pos)
10052 isl_bool empty;
10053 isl_basic_map *bmap;
10054 isl_size dim1;
10056 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10057 if (dim1 < 0)
10058 return -2;
10059 bmap = join_initial(bset1, bset2, pos);
10060 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10061 isl_dim_out, dim1 - pos);
10062 empty = isl_basic_map_is_empty(bmap);
10063 if (empty < 0)
10064 goto error;
10065 if (empty) {
10066 isl_basic_map_free(bmap);
10067 return -1;
10069 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10070 isl_dim_out, dim1 - pos);
10071 empty = isl_basic_map_is_empty(bmap);
10072 if (empty < 0)
10073 goto error;
10074 isl_basic_map_free(bmap);
10075 if (empty)
10076 return 0;
10077 return 1;
10078 error:
10079 isl_basic_map_free(bmap);
10080 return -2;
10083 /* Given two sets set1 and set2, check whether
10084 * for any common value of the parameters and dimensions preceding pos
10085 * there is a value of dimension pos in set1 that is larger
10086 * than a value of the same dimension in set2.
10088 * Return
10089 * 1 if there exists such a pair
10090 * 0 if there is no such pair, but there is a pair of equal values
10091 * -1 otherwise
10092 * -2 if some error occurred.
10094 int isl_set_follows_at(__isl_keep isl_set *set1,
10095 __isl_keep isl_set *set2, int pos)
10097 int i, j;
10098 int follows = -1;
10100 if (!set1 || !set2)
10101 return -2;
10103 for (i = 0; i < set1->n; ++i)
10104 for (j = 0; j < set2->n; ++j) {
10105 int f;
10106 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10107 if (f == 1 || f == -2)
10108 return f;
10109 if (f > follows)
10110 follows = f;
10113 return follows;
10116 static isl_bool isl_basic_map_plain_has_fixed_var(
10117 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10119 int i;
10120 int d;
10121 isl_size total;
10123 total = isl_basic_map_dim(bmap, isl_dim_all);
10124 if (total < 0)
10125 return isl_bool_error;
10126 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10127 for (; d+1 > pos; --d)
10128 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10129 break;
10130 if (d != pos)
10131 continue;
10132 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10133 return isl_bool_false;
10134 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10135 return isl_bool_false;
10136 if (!isl_int_is_one(bmap->eq[i][1+d]))
10137 return isl_bool_false;
10138 if (val)
10139 isl_int_neg(*val, bmap->eq[i][0]);
10140 return isl_bool_true;
10142 return isl_bool_false;
10145 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10146 unsigned pos, isl_int *val)
10148 int i;
10149 isl_int v;
10150 isl_int tmp;
10151 isl_bool fixed;
10153 if (!map)
10154 return isl_bool_error;
10155 if (map->n == 0)
10156 return isl_bool_false;
10157 if (map->n == 1)
10158 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10159 isl_int_init(v);
10160 isl_int_init(tmp);
10161 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10162 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10163 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10164 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10165 fixed = isl_bool_false;
10167 if (val)
10168 isl_int_set(*val, v);
10169 isl_int_clear(tmp);
10170 isl_int_clear(v);
10171 return fixed;
10174 static isl_bool isl_basic_set_plain_has_fixed_var(
10175 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10177 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10178 pos, val);
10181 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10182 enum isl_dim_type type, unsigned pos, isl_int *val)
10184 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10185 return isl_bool_error;
10186 return isl_basic_map_plain_has_fixed_var(bmap,
10187 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10190 /* If "bmap" obviously lies on a hyperplane where the given dimension
10191 * has a fixed value, then return that value.
10192 * Otherwise return NaN.
10194 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10195 __isl_keep isl_basic_map *bmap,
10196 enum isl_dim_type type, unsigned pos)
10198 isl_ctx *ctx;
10199 isl_val *v;
10200 isl_bool fixed;
10202 if (!bmap)
10203 return NULL;
10204 ctx = isl_basic_map_get_ctx(bmap);
10205 v = isl_val_alloc(ctx);
10206 if (!v)
10207 return NULL;
10208 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10209 if (fixed < 0)
10210 return isl_val_free(v);
10211 if (fixed) {
10212 isl_int_set_si(v->d, 1);
10213 return v;
10215 isl_val_free(v);
10216 return isl_val_nan(ctx);
10219 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10220 enum isl_dim_type type, unsigned pos, isl_int *val)
10222 if (isl_map_check_range(map, type, pos, 1) < 0)
10223 return isl_bool_error;
10224 return isl_map_plain_has_fixed_var(map,
10225 map_offset(map, type) - 1 + pos, val);
10228 /* If "map" obviously lies on a hyperplane where the given dimension
10229 * has a fixed value, then return that value.
10230 * Otherwise return NaN.
10232 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10233 enum isl_dim_type type, unsigned pos)
10235 isl_ctx *ctx;
10236 isl_val *v;
10237 isl_bool fixed;
10239 if (!map)
10240 return NULL;
10241 ctx = isl_map_get_ctx(map);
10242 v = isl_val_alloc(ctx);
10243 if (!v)
10244 return NULL;
10245 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10246 if (fixed < 0)
10247 return isl_val_free(v);
10248 if (fixed) {
10249 isl_int_set_si(v->d, 1);
10250 return v;
10252 isl_val_free(v);
10253 return isl_val_nan(ctx);
10256 /* If "set" obviously lies on a hyperplane where the given dimension
10257 * has a fixed value, then return that value.
10258 * Otherwise return NaN.
10260 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10261 enum isl_dim_type type, unsigned pos)
10263 return isl_map_plain_get_val_if_fixed(set, type, pos);
10266 /* Return a sequence of values in the same space as "set"
10267 * that are equal to the corresponding set dimensions of "set"
10268 * for those set dimensions that obviously lie on a hyperplane
10269 * where the dimension has a fixed value.
10270 * The other elements are set to NaN.
10272 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10273 __isl_keep isl_set *set)
10275 int i;
10276 isl_size n;
10277 isl_space *space;
10278 isl_multi_val *mv;
10280 space = isl_space_drop_all_params(isl_set_get_space(set));
10281 mv = isl_multi_val_alloc(space);
10282 n = isl_multi_val_size(mv);
10283 if (n < 0)
10284 return isl_multi_val_free(mv);
10286 for (i = 0; i < n; ++i) {
10287 isl_val *v;
10289 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10290 mv = isl_multi_val_set_val(mv, i, v);
10293 return mv;
10296 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10297 * then return this fixed value in *val.
10299 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10300 unsigned dim, isl_int *val)
10302 isl_size nparam;
10304 nparam = isl_basic_set_dim(bset, isl_dim_param);
10305 if (nparam < 0)
10306 return isl_bool_error;
10307 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10310 /* Return -1 if the constraint "c1" should be sorted before "c2"
10311 * and 1 if it should be sorted after "c2".
10312 * Return 0 if the two constraints are the same (up to the constant term).
10314 * In particular, if a constraint involves later variables than another
10315 * then it is sorted after this other constraint.
10316 * uset_gist depends on constraints without existentially quantified
10317 * variables sorting first.
10319 * For constraints that have the same latest variable, those
10320 * with the same coefficient for this latest variable (first in absolute value
10321 * and then in actual value) are grouped together.
10322 * This is useful for detecting pairs of constraints that can
10323 * be chained in their printed representation.
10325 * Finally, within a group, constraints are sorted according to
10326 * their coefficients (excluding the constant term).
10328 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10330 isl_int **c1 = (isl_int **) p1;
10331 isl_int **c2 = (isl_int **) p2;
10332 int l1, l2;
10333 unsigned size = *(unsigned *) arg;
10334 int cmp;
10336 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10337 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10339 if (l1 != l2)
10340 return l1 - l2;
10342 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10343 if (cmp != 0)
10344 return cmp;
10345 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10346 if (cmp != 0)
10347 return -cmp;
10349 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10352 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10353 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10354 * and 0 if the two constraints are the same (up to the constant term).
10356 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10357 isl_int *c1, isl_int *c2)
10359 isl_size total;
10360 unsigned size;
10362 total = isl_basic_map_dim(bmap, isl_dim_all);
10363 if (total < 0)
10364 return -2;
10365 size = total;
10366 return sort_constraint_cmp(&c1, &c2, &size);
10369 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10370 __isl_take isl_basic_map *bmap)
10372 isl_size total;
10373 unsigned size;
10375 if (!bmap)
10376 return NULL;
10377 if (bmap->n_ineq == 0)
10378 return bmap;
10379 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10380 return bmap;
10381 total = isl_basic_map_dim(bmap, isl_dim_all);
10382 if (total < 0)
10383 return isl_basic_map_free(bmap);
10384 size = total;
10385 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10386 &sort_constraint_cmp, &size) < 0)
10387 return isl_basic_map_free(bmap);
10388 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10389 return bmap;
10392 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10393 __isl_take isl_basic_set *bset)
10395 isl_basic_map *bmap = bset_to_bmap(bset);
10396 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10399 __isl_give isl_basic_map *isl_basic_map_normalize(
10400 __isl_take isl_basic_map *bmap)
10402 bmap = isl_basic_map_remove_redundancies(bmap);
10403 bmap = isl_basic_map_sort_constraints(bmap);
10404 return bmap;
10406 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10407 __isl_keep isl_basic_map *bmap2)
10409 int i, cmp;
10410 isl_size total;
10411 isl_space *space1, *space2;
10413 if (!bmap1 || !bmap2)
10414 return -1;
10416 if (bmap1 == bmap2)
10417 return 0;
10418 space1 = isl_basic_map_peek_space(bmap1);
10419 space2 = isl_basic_map_peek_space(bmap2);
10420 cmp = isl_space_cmp(space1, space2);
10421 if (cmp)
10422 return cmp;
10423 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10424 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10425 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10426 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10427 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10428 return 0;
10429 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10430 return 1;
10431 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10432 return -1;
10433 if (bmap1->n_eq != bmap2->n_eq)
10434 return bmap1->n_eq - bmap2->n_eq;
10435 if (bmap1->n_ineq != bmap2->n_ineq)
10436 return bmap1->n_ineq - bmap2->n_ineq;
10437 if (bmap1->n_div != bmap2->n_div)
10438 return bmap1->n_div - bmap2->n_div;
10439 total = isl_basic_map_dim(bmap1, isl_dim_all);
10440 if (total < 0)
10441 return -1;
10442 for (i = 0; i < bmap1->n_eq; ++i) {
10443 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10444 if (cmp)
10445 return cmp;
10447 for (i = 0; i < bmap1->n_ineq; ++i) {
10448 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10449 if (cmp)
10450 return cmp;
10452 for (i = 0; i < bmap1->n_div; ++i) {
10453 isl_bool unknown1, unknown2;
10455 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10456 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10457 if (unknown1 < 0 || unknown2 < 0)
10458 return -1;
10459 if (unknown1 && unknown2)
10460 continue;
10461 if (unknown1)
10462 return 1;
10463 if (unknown2)
10464 return -1;
10465 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10466 if (cmp)
10467 return cmp;
10469 return 0;
10472 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10473 __isl_keep isl_basic_set *bset2)
10475 return isl_basic_map_plain_cmp(bset1, bset2);
10478 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10480 int i, cmp;
10482 if (set1 == set2)
10483 return 0;
10484 if (set1->n != set2->n)
10485 return set1->n - set2->n;
10487 for (i = 0; i < set1->n; ++i) {
10488 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10489 if (cmp)
10490 return cmp;
10493 return 0;
10496 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10497 __isl_keep isl_basic_map *bmap2)
10499 if (!bmap1 || !bmap2)
10500 return isl_bool_error;
10501 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10504 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10505 __isl_keep isl_basic_set *bset2)
10507 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10508 bset_to_bmap(bset2));
10511 static int qsort_bmap_cmp(const void *p1, const void *p2)
10513 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10514 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10516 return isl_basic_map_plain_cmp(bmap1, bmap2);
10519 /* Sort the basic maps of "map" and remove duplicate basic maps.
10521 * While removing basic maps, we make sure that the basic maps remain
10522 * sorted because isl_map_normalize expects the basic maps of the result
10523 * to be sorted.
10525 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10527 int i, j;
10529 map = isl_map_remove_empty_parts(map);
10530 if (!map)
10531 return NULL;
10532 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10533 for (i = map->n - 1; i >= 1; --i) {
10534 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10535 continue;
10536 isl_basic_map_free(map->p[i-1]);
10537 for (j = i; j < map->n; ++j)
10538 map->p[j - 1] = map->p[j];
10539 map->n--;
10542 return map;
10545 /* Remove obvious duplicates among the basic maps of "map".
10547 * Unlike isl_map_normalize, this function does not remove redundant
10548 * constraints and only removes duplicates that have exactly the same
10549 * constraints in the input. It does sort the constraints and
10550 * the basic maps to ease the detection of duplicates.
10552 * If "map" has already been normalized or if the basic maps are
10553 * disjoint, then there can be no duplicates.
10555 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10557 int i;
10558 isl_basic_map *bmap;
10560 if (!map)
10561 return NULL;
10562 if (map->n <= 1)
10563 return map;
10564 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10565 return map;
10566 for (i = 0; i < map->n; ++i) {
10567 bmap = isl_basic_map_copy(map->p[i]);
10568 bmap = isl_basic_map_sort_constraints(bmap);
10569 if (!bmap)
10570 return isl_map_free(map);
10571 isl_basic_map_free(map->p[i]);
10572 map->p[i] = bmap;
10575 map = sort_and_remove_duplicates(map);
10576 return map;
10579 /* We normalize in place, but if anything goes wrong we need
10580 * to return NULL, so we need to make sure we don't change the
10581 * meaning of any possible other copies of map.
10583 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10585 int i;
10586 struct isl_basic_map *bmap;
10588 if (!map)
10589 return NULL;
10590 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10591 return map;
10592 for (i = 0; i < map->n; ++i) {
10593 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10594 if (!bmap)
10595 goto error;
10596 isl_basic_map_free(map->p[i]);
10597 map->p[i] = bmap;
10600 map = sort_and_remove_duplicates(map);
10601 if (map)
10602 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10603 return map;
10604 error:
10605 isl_map_free(map);
10606 return NULL;
10609 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10611 return set_from_map(isl_map_normalize(set_to_map(set)));
10614 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10615 __isl_keep isl_map *map2)
10617 int i;
10618 isl_bool equal;
10620 if (!map1 || !map2)
10621 return isl_bool_error;
10623 if (map1 == map2)
10624 return isl_bool_true;
10625 equal = isl_map_has_equal_space(map1, map2);
10626 if (equal < 0 || !equal)
10627 return equal;
10629 map1 = isl_map_copy(map1);
10630 map2 = isl_map_copy(map2);
10631 map1 = isl_map_normalize(map1);
10632 map2 = isl_map_normalize(map2);
10633 if (!map1 || !map2)
10634 goto error;
10635 equal = map1->n == map2->n;
10636 for (i = 0; equal && i < map1->n; ++i) {
10637 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10638 if (equal < 0)
10639 goto error;
10641 isl_map_free(map1);
10642 isl_map_free(map2);
10643 return equal;
10644 error:
10645 isl_map_free(map1);
10646 isl_map_free(map2);
10647 return isl_bool_error;
10650 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10651 __isl_keep isl_set *set2)
10653 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10656 /* Return the basic maps in "map" as a list.
10658 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10659 __isl_keep isl_map *map)
10661 int i;
10662 isl_ctx *ctx;
10663 isl_basic_map_list *list;
10665 if (!map)
10666 return NULL;
10667 ctx = isl_map_get_ctx(map);
10668 list = isl_basic_map_list_alloc(ctx, map->n);
10670 for (i = 0; i < map->n; ++i) {
10671 isl_basic_map *bmap;
10673 bmap = isl_basic_map_copy(map->p[i]);
10674 list = isl_basic_map_list_add(list, bmap);
10677 return list;
10680 /* Return the intersection of the elements in the non-empty list "list".
10681 * All elements are assumed to live in the same space.
10683 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10684 __isl_take isl_basic_map_list *list)
10686 int i;
10687 isl_size n;
10688 isl_basic_map *bmap;
10690 n = isl_basic_map_list_n_basic_map(list);
10691 if (n < 0)
10692 goto error;
10693 if (n < 1)
10694 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10695 "expecting non-empty list", goto error);
10697 bmap = isl_basic_map_list_get_basic_map(list, 0);
10698 for (i = 1; i < n; ++i) {
10699 isl_basic_map *bmap_i;
10701 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10702 bmap = isl_basic_map_intersect(bmap, bmap_i);
10705 isl_basic_map_list_free(list);
10706 return bmap;
10707 error:
10708 isl_basic_map_list_free(list);
10709 return NULL;
10712 /* Return the intersection of the elements in the non-empty list "list".
10713 * All elements are assumed to live in the same space.
10715 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10716 __isl_take isl_basic_set_list *list)
10718 return isl_basic_map_list_intersect(list);
10721 /* Return the union of the elements of "list".
10722 * The list is required to have at least one element.
10724 __isl_give isl_set *isl_basic_set_list_union(
10725 __isl_take isl_basic_set_list *list)
10727 int i;
10728 isl_size n;
10729 isl_space *space;
10730 isl_basic_set *bset;
10731 isl_set *set;
10733 n = isl_basic_set_list_n_basic_set(list);
10734 if (n < 0)
10735 goto error;
10736 if (n < 1)
10737 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10738 "expecting non-empty list", goto error);
10740 bset = isl_basic_set_list_get_basic_set(list, 0);
10741 space = isl_basic_set_get_space(bset);
10742 isl_basic_set_free(bset);
10744 set = isl_set_alloc_space(space, n, 0);
10745 for (i = 0; i < n; ++i) {
10746 bset = isl_basic_set_list_get_basic_set(list, i);
10747 set = isl_set_add_basic_set(set, bset);
10750 isl_basic_set_list_free(list);
10751 return set;
10752 error:
10753 isl_basic_set_list_free(list);
10754 return NULL;
10757 /* Return the union of the elements in the non-empty list "list".
10758 * All elements are assumed to live in the same space.
10760 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10762 int i;
10763 isl_size n;
10764 isl_set *set;
10766 n = isl_set_list_n_set(list);
10767 if (n < 0)
10768 goto error;
10769 if (n < 1)
10770 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10771 "expecting non-empty list", goto error);
10773 set = isl_set_list_get_set(list, 0);
10774 for (i = 1; i < n; ++i) {
10775 isl_set *set_i;
10777 set_i = isl_set_list_get_set(list, i);
10778 set = isl_set_union(set, set_i);
10781 isl_set_list_free(list);
10782 return set;
10783 error:
10784 isl_set_list_free(list);
10785 return NULL;
10788 __isl_give isl_basic_map *isl_basic_map_product(
10789 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10791 isl_space *space_result = NULL;
10792 struct isl_basic_map *bmap;
10793 unsigned in1, in2, out1, out2, nparam, total, pos;
10794 struct isl_dim_map *dim_map1, *dim_map2;
10796 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10797 goto error;
10798 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10799 isl_space_copy(bmap2->dim));
10801 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10802 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10803 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10804 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10805 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10807 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10808 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10809 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10814 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10815 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10816 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10817 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10819 bmap = isl_basic_map_alloc_space(space_result,
10820 bmap1->n_div + bmap2->n_div,
10821 bmap1->n_eq + bmap2->n_eq,
10822 bmap1->n_ineq + bmap2->n_ineq);
10823 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10825 bmap = isl_basic_map_simplify(bmap);
10826 return isl_basic_map_finalize(bmap);
10827 error:
10828 isl_basic_map_free(bmap1);
10829 isl_basic_map_free(bmap2);
10830 return NULL;
10833 __isl_give isl_basic_map *isl_basic_map_flat_product(
10834 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10836 isl_basic_map *prod;
10838 prod = isl_basic_map_product(bmap1, bmap2);
10839 prod = isl_basic_map_flatten(prod);
10840 return prod;
10843 __isl_give isl_basic_set *isl_basic_set_flat_product(
10844 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10846 return isl_basic_map_flat_range_product(bset1, bset2);
10849 __isl_give isl_basic_map *isl_basic_map_domain_product(
10850 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10852 isl_space *space1, *space2;
10853 isl_space *space_result = NULL;
10854 isl_basic_map *bmap;
10855 isl_size in1, in2, out, nparam;
10856 unsigned total, pos;
10857 struct isl_dim_map *dim_map1, *dim_map2;
10859 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10860 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10861 out = isl_basic_map_dim(bmap1, isl_dim_out);
10862 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10863 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10864 goto error;
10866 space1 = isl_basic_map_get_space(bmap1);
10867 space2 = isl_basic_map_get_space(bmap2);
10868 space_result = isl_space_domain_product(space1, space2);
10870 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10871 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10872 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10873 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10874 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10875 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10876 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10877 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10878 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10879 isl_dim_map_div(dim_map1, bmap1, pos += out);
10880 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10882 bmap = isl_basic_map_alloc_space(space_result,
10883 bmap1->n_div + bmap2->n_div,
10884 bmap1->n_eq + bmap2->n_eq,
10885 bmap1->n_ineq + bmap2->n_ineq);
10886 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10887 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10888 bmap = isl_basic_map_simplify(bmap);
10889 return isl_basic_map_finalize(bmap);
10890 error:
10891 isl_basic_map_free(bmap1);
10892 isl_basic_map_free(bmap2);
10893 return NULL;
10896 __isl_give isl_basic_map *isl_basic_map_range_product(
10897 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10899 isl_bool rational;
10900 isl_space *space_result = NULL;
10901 isl_basic_map *bmap;
10902 isl_size in, out1, out2, nparam;
10903 unsigned total, pos;
10904 struct isl_dim_map *dim_map1, *dim_map2;
10906 rational = isl_basic_map_is_rational(bmap1);
10907 if (rational >= 0 && rational)
10908 rational = isl_basic_map_is_rational(bmap2);
10909 in = isl_basic_map_dim(bmap1, isl_dim_in);
10910 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10911 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10912 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10913 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10914 goto error;
10916 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10917 goto error;
10919 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10920 isl_space_copy(bmap2->dim));
10922 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10923 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10924 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10925 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10926 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10929 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10930 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10931 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10932 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10934 bmap = isl_basic_map_alloc_space(space_result,
10935 bmap1->n_div + bmap2->n_div,
10936 bmap1->n_eq + bmap2->n_eq,
10937 bmap1->n_ineq + bmap2->n_ineq);
10938 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10939 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10940 if (rational)
10941 bmap = isl_basic_map_set_rational(bmap);
10942 bmap = isl_basic_map_simplify(bmap);
10943 return isl_basic_map_finalize(bmap);
10944 error:
10945 isl_basic_map_free(bmap1);
10946 isl_basic_map_free(bmap2);
10947 return NULL;
10950 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10951 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10953 isl_basic_map *prod;
10955 prod = isl_basic_map_range_product(bmap1, bmap2);
10956 prod = isl_basic_map_flatten_range(prod);
10957 return prod;
10960 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10961 * and collect the results.
10962 * The result live in the space obtained by calling "space_product"
10963 * on the spaces of "map1" and "map2".
10964 * If "remove_duplicates" is set then the result may contain duplicates
10965 * (even if the inputs do not) and so we try and remove the obvious
10966 * duplicates.
10968 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10969 __isl_take isl_map *map2,
10970 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10971 __isl_take isl_space *right),
10972 __isl_give isl_basic_map *(*basic_map_product)(
10973 __isl_take isl_basic_map *left,
10974 __isl_take isl_basic_map *right),
10975 int remove_duplicates)
10977 unsigned flags = 0;
10978 struct isl_map *result;
10979 int i, j;
10980 isl_bool m;
10982 m = isl_map_has_equal_params(map1, map2);
10983 if (m < 0)
10984 goto error;
10985 if (!m)
10986 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10987 "parameters don't match", goto error);
10989 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10990 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10991 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10993 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10994 isl_space_copy(map2->dim)),
10995 map1->n * map2->n, flags);
10996 if (!result)
10997 goto error;
10998 for (i = 0; i < map1->n; ++i)
10999 for (j = 0; j < map2->n; ++j) {
11000 struct isl_basic_map *part;
11001 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11002 isl_basic_map_copy(map2->p[j]));
11003 if (isl_basic_map_is_empty(part))
11004 isl_basic_map_free(part);
11005 else
11006 result = isl_map_add_basic_map(result, part);
11007 if (!result)
11008 goto error;
11010 if (remove_duplicates)
11011 result = isl_map_remove_obvious_duplicates(result);
11012 isl_map_free(map1);
11013 isl_map_free(map2);
11014 return result;
11015 error:
11016 isl_map_free(map1);
11017 isl_map_free(map2);
11018 return NULL;
11021 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11023 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11024 __isl_take isl_map *map2)
11026 isl_map_align_params_bin(&map1, &map2);
11027 return map_product(map1, map2, &isl_space_product,
11028 &isl_basic_map_product, 0);
11031 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11033 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11034 __isl_take isl_map *map2)
11036 isl_map *prod;
11038 prod = isl_map_product(map1, map2);
11039 prod = isl_map_flatten(prod);
11040 return prod;
11043 /* Given two set A and B, construct its Cartesian product A x B.
11045 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11046 __isl_take isl_set *set2)
11048 return isl_map_range_product(set1, set2);
11051 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11052 __isl_take isl_set *set2)
11054 return isl_map_flat_range_product(set1, set2);
11057 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11059 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11060 __isl_take isl_map *map2)
11062 isl_map_align_params_bin(&map1, &map2);
11063 return map_product(map1, map2, &isl_space_domain_product,
11064 &isl_basic_map_domain_product, 1);
11067 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11069 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11070 __isl_take isl_map *map2)
11072 isl_map_align_params_bin(&map1, &map2);
11073 return map_product(map1, map2, &isl_space_range_product,
11074 &isl_basic_map_range_product, 1);
11077 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11079 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11081 isl_space *space;
11082 isl_size total1, keep1, total2, keep2;
11084 total1 = isl_map_dim(map, isl_dim_in);
11085 total2 = isl_map_dim(map, isl_dim_out);
11086 if (total1 < 0 || total2 < 0)
11087 return isl_map_free(map);
11088 if (!isl_space_domain_is_wrapping(map->dim) ||
11089 !isl_space_range_is_wrapping(map->dim))
11090 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11091 "not a product", return isl_map_free(map));
11093 space = isl_map_get_space(map);
11094 space = isl_space_factor_domain(space);
11095 keep1 = isl_space_dim(space, isl_dim_in);
11096 keep2 = isl_space_dim(space, isl_dim_out);
11097 if (keep1 < 0 || keep2 < 0)
11098 map = isl_map_free(map);
11099 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11100 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11101 map = isl_map_reset_space(map, space);
11103 return map;
11106 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11108 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11110 isl_space *space;
11111 isl_size total1, keep1, total2, keep2;
11113 total1 = isl_map_dim(map, isl_dim_in);
11114 total2 = isl_map_dim(map, isl_dim_out);
11115 if (total1 < 0 || total2 < 0)
11116 return isl_map_free(map);
11117 if (!isl_space_domain_is_wrapping(map->dim) ||
11118 !isl_space_range_is_wrapping(map->dim))
11119 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11120 "not a product", return isl_map_free(map));
11122 space = isl_map_get_space(map);
11123 space = isl_space_factor_range(space);
11124 keep1 = isl_space_dim(space, isl_dim_in);
11125 keep2 = isl_space_dim(space, isl_dim_out);
11126 if (keep1 < 0 || keep2 < 0)
11127 map = isl_map_free(map);
11128 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11129 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11130 map = isl_map_reset_space(map, space);
11132 return map;
11135 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11137 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11139 isl_space *space;
11140 isl_size total, keep;
11142 total = isl_map_dim(map, isl_dim_in);
11143 if (total < 0)
11144 return isl_map_free(map);
11145 if (!isl_space_domain_is_wrapping(map->dim))
11146 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11147 "domain is not a product", return isl_map_free(map));
11149 space = isl_map_get_space(map);
11150 space = isl_space_domain_factor_domain(space);
11151 keep = isl_space_dim(space, isl_dim_in);
11152 if (keep < 0)
11153 map = isl_map_free(map);
11154 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11155 map = isl_map_reset_space(map, space);
11157 return map;
11160 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11162 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11164 isl_space *space;
11165 isl_size total, keep;
11167 total = isl_map_dim(map, isl_dim_in);
11168 if (total < 0)
11169 return isl_map_free(map);
11170 if (!isl_space_domain_is_wrapping(map->dim))
11171 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11172 "domain is not a product", return isl_map_free(map));
11174 space = isl_map_get_space(map);
11175 space = isl_space_domain_factor_range(space);
11176 keep = isl_space_dim(space, isl_dim_in);
11177 if (keep < 0)
11178 map = isl_map_free(map);
11179 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11180 map = isl_map_reset_space(map, space);
11182 return map;
11185 /* Given a map A -> [B -> C], extract the map A -> B.
11187 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11189 isl_space *space;
11190 isl_size total, keep;
11192 total = isl_map_dim(map, isl_dim_out);
11193 if (total < 0)
11194 return isl_map_free(map);
11195 if (!isl_space_range_is_wrapping(map->dim))
11196 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11197 "range is not a product", return isl_map_free(map));
11199 space = isl_map_get_space(map);
11200 space = isl_space_range_factor_domain(space);
11201 keep = isl_space_dim(space, isl_dim_out);
11202 if (keep < 0)
11203 map = isl_map_free(map);
11204 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11205 map = isl_map_reset_space(map, space);
11207 return map;
11210 /* Given a map A -> [B -> C], extract the map A -> C.
11212 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11214 isl_space *space;
11215 isl_size total, keep;
11217 total = isl_map_dim(map, isl_dim_out);
11218 if (total < 0)
11219 return isl_map_free(map);
11220 if (!isl_space_range_is_wrapping(map->dim))
11221 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11222 "range is not a product", return isl_map_free(map));
11224 space = isl_map_get_space(map);
11225 space = isl_space_range_factor_range(space);
11226 keep = isl_space_dim(space, isl_dim_out);
11227 if (keep < 0)
11228 map = isl_map_free(map);
11229 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11230 map = isl_map_reset_space(map, space);
11232 return map;
11235 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11237 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11238 __isl_take isl_map *map2)
11240 isl_map *prod;
11242 prod = isl_map_domain_product(map1, map2);
11243 prod = isl_map_flatten_domain(prod);
11244 return prod;
11247 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11249 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11250 __isl_take isl_map *map2)
11252 isl_map *prod;
11254 prod = isl_map_range_product(map1, map2);
11255 prod = isl_map_flatten_range(prod);
11256 return prod;
11259 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11261 int i;
11262 uint32_t hash = isl_hash_init();
11263 isl_size total;
11265 if (!bmap)
11266 return 0;
11267 bmap = isl_basic_map_copy(bmap);
11268 bmap = isl_basic_map_normalize(bmap);
11269 total = isl_basic_map_dim(bmap, isl_dim_all);
11270 if (total < 0)
11271 return 0;
11272 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11273 for (i = 0; i < bmap->n_eq; ++i) {
11274 uint32_t c_hash;
11275 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11276 isl_hash_hash(hash, c_hash);
11278 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11279 for (i = 0; i < bmap->n_ineq; ++i) {
11280 uint32_t c_hash;
11281 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11282 isl_hash_hash(hash, c_hash);
11284 isl_hash_byte(hash, bmap->n_div & 0xFF);
11285 for (i = 0; i < bmap->n_div; ++i) {
11286 uint32_t c_hash;
11287 if (isl_int_is_zero(bmap->div[i][0]))
11288 continue;
11289 isl_hash_byte(hash, i & 0xFF);
11290 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11291 isl_hash_hash(hash, c_hash);
11293 isl_basic_map_free(bmap);
11294 return hash;
11297 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11299 return isl_basic_map_get_hash(bset_to_bmap(bset));
11302 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11304 int i;
11305 uint32_t hash;
11307 if (!map)
11308 return 0;
11309 map = isl_map_copy(map);
11310 map = isl_map_normalize(map);
11311 if (!map)
11312 return 0;
11314 hash = isl_hash_init();
11315 for (i = 0; i < map->n; ++i) {
11316 uint32_t bmap_hash;
11317 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11318 isl_hash_hash(hash, bmap_hash);
11321 isl_map_free(map);
11323 return hash;
11326 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11328 return isl_map_get_hash(set_to_map(set));
11331 /* Return the number of basic maps in the (current) representation of "map".
11333 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11335 return map ? map->n : isl_size_error;
11338 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11340 return set ? set->n : isl_size_error;
11343 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11344 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11346 int i;
11348 if (!map)
11349 return isl_stat_error;
11351 for (i = 0; i < map->n; ++i)
11352 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11353 return isl_stat_error;
11355 return isl_stat_ok;
11358 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11359 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11361 int i;
11363 if (!set)
11364 return isl_stat_error;
11366 for (i = 0; i < set->n; ++i)
11367 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11368 return isl_stat_error;
11370 return isl_stat_ok;
11373 /* Does "test" succeed on every basic set in "set"?
11375 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11376 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11377 void *user)
11379 int i;
11381 if (!set)
11382 return isl_bool_error;
11384 for (i = 0; i < set->n; ++i) {
11385 isl_bool r;
11387 r = test(set->p[i], user);
11388 if (r < 0 || !r)
11389 return r;
11392 return isl_bool_true;
11395 /* Return a list of basic sets, the union of which is equal to "set".
11397 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11398 __isl_keep isl_set *set)
11400 int i;
11401 isl_basic_set_list *list;
11403 if (!set)
11404 return NULL;
11406 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11407 for (i = 0; i < set->n; ++i) {
11408 isl_basic_set *bset;
11410 bset = isl_basic_set_copy(set->p[i]);
11411 list = isl_basic_set_list_add(list, bset);
11414 return list;
11417 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11419 isl_space *space;
11421 if (!bset)
11422 return NULL;
11424 bset = isl_basic_set_cow(bset);
11425 if (!bset)
11426 return NULL;
11428 space = isl_basic_set_get_space(bset);
11429 space = isl_space_lift(space, bset->n_div);
11430 if (!space)
11431 goto error;
11432 isl_space_free(bset->dim);
11433 bset->dim = space;
11434 bset->extra -= bset->n_div;
11435 bset->n_div = 0;
11437 bset = isl_basic_set_finalize(bset);
11439 return bset;
11440 error:
11441 isl_basic_set_free(bset);
11442 return NULL;
11445 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11447 int i;
11448 isl_space *space;
11449 unsigned n_div;
11451 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11453 if (!set)
11454 return NULL;
11456 set = isl_set_cow(set);
11457 if (!set)
11458 return NULL;
11460 n_div = set->p[0]->n_div;
11461 space = isl_set_get_space(set);
11462 space = isl_space_lift(space, n_div);
11463 if (!space)
11464 goto error;
11465 isl_space_free(set->dim);
11466 set->dim = space;
11468 for (i = 0; i < set->n; ++i) {
11469 set->p[i] = isl_basic_set_lift(set->p[i]);
11470 if (!set->p[i])
11471 goto error;
11474 return set;
11475 error:
11476 isl_set_free(set);
11477 return NULL;
11480 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11482 isl_size dim;
11483 int size = 0;
11485 dim = isl_basic_set_dim(bset, isl_dim_all);
11486 if (dim < 0)
11487 return -1;
11488 size += bset->n_eq * (1 + dim);
11489 size += bset->n_ineq * (1 + dim);
11490 size += bset->n_div * (2 + dim);
11492 return size;
11495 int isl_set_size(__isl_keep isl_set *set)
11497 int i;
11498 int size = 0;
11500 if (!set)
11501 return -1;
11503 for (i = 0; i < set->n; ++i)
11504 size += isl_basic_set_size(set->p[i]);
11506 return size;
11509 /* Check if there is any lower bound (if lower == 0) and/or upper
11510 * bound (if upper == 0) on the specified dim.
11512 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11513 enum isl_dim_type type, unsigned pos, int lower, int upper)
11515 int i;
11517 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11518 return isl_bool_error;
11520 pos += isl_basic_map_offset(bmap, type);
11522 for (i = 0; i < bmap->n_div; ++i) {
11523 if (isl_int_is_zero(bmap->div[i][0]))
11524 continue;
11525 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11526 return isl_bool_true;
11529 for (i = 0; i < bmap->n_eq; ++i)
11530 if (!isl_int_is_zero(bmap->eq[i][pos]))
11531 return isl_bool_true;
11533 for (i = 0; i < bmap->n_ineq; ++i) {
11534 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11535 if (sgn > 0)
11536 lower = 1;
11537 if (sgn < 0)
11538 upper = 1;
11541 return lower && upper;
11544 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11545 enum isl_dim_type type, unsigned pos)
11547 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11550 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11551 enum isl_dim_type type, unsigned pos)
11553 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11556 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11557 enum isl_dim_type type, unsigned pos)
11559 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11562 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11563 enum isl_dim_type type, unsigned pos)
11565 int i;
11567 if (!map)
11568 return isl_bool_error;
11570 for (i = 0; i < map->n; ++i) {
11571 isl_bool bounded;
11572 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11573 if (bounded < 0 || !bounded)
11574 return bounded;
11577 return isl_bool_true;
11580 /* Return true if the specified dim is involved in both an upper bound
11581 * and a lower bound.
11583 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11584 enum isl_dim_type type, unsigned pos)
11586 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11589 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11591 static isl_bool has_any_bound(__isl_keep isl_map *map,
11592 enum isl_dim_type type, unsigned pos,
11593 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11594 enum isl_dim_type type, unsigned pos))
11596 int i;
11598 if (!map)
11599 return isl_bool_error;
11601 for (i = 0; i < map->n; ++i) {
11602 isl_bool bounded;
11603 bounded = fn(map->p[i], type, pos);
11604 if (bounded < 0 || bounded)
11605 return bounded;
11608 return isl_bool_false;
11611 /* Return 1 if the specified dim is involved in any lower bound.
11613 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11614 enum isl_dim_type type, unsigned pos)
11616 return has_any_bound(set, type, pos,
11617 &isl_basic_map_dim_has_lower_bound);
11620 /* Return 1 if the specified dim is involved in any upper bound.
11622 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11623 enum isl_dim_type type, unsigned pos)
11625 return has_any_bound(set, type, pos,
11626 &isl_basic_map_dim_has_upper_bound);
11629 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11631 static isl_bool has_bound(__isl_keep isl_map *map,
11632 enum isl_dim_type type, unsigned pos,
11633 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11634 enum isl_dim_type type, unsigned pos))
11636 int i;
11638 if (!map)
11639 return isl_bool_error;
11641 for (i = 0; i < map->n; ++i) {
11642 isl_bool bounded;
11643 bounded = fn(map->p[i], type, pos);
11644 if (bounded < 0 || !bounded)
11645 return bounded;
11648 return isl_bool_true;
11651 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11653 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11654 enum isl_dim_type type, unsigned pos)
11656 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11659 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11661 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11662 enum isl_dim_type type, unsigned pos)
11664 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11667 /* For each of the "n" variables starting at "first", determine
11668 * the sign of the variable and put the results in the first "n"
11669 * elements of the array "signs".
11670 * Sign
11671 * 1 means that the variable is non-negative
11672 * -1 means that the variable is non-positive
11673 * 0 means the variable attains both positive and negative values.
11675 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11676 unsigned first, unsigned n, int *signs)
11678 isl_vec *bound = NULL;
11679 struct isl_tab *tab = NULL;
11680 struct isl_tab_undo *snap;
11681 int i;
11682 isl_size total;
11684 total = isl_basic_set_dim(bset, isl_dim_all);
11685 if (total < 0 || !signs)
11686 return isl_stat_error;
11688 bound = isl_vec_alloc(bset->ctx, 1 + total);
11689 tab = isl_tab_from_basic_set(bset, 0);
11690 if (!bound || !tab)
11691 goto error;
11693 isl_seq_clr(bound->el, bound->size);
11694 isl_int_set_si(bound->el[0], -1);
11696 snap = isl_tab_snap(tab);
11697 for (i = 0; i < n; ++i) {
11698 int empty;
11700 isl_int_set_si(bound->el[1 + first + i], -1);
11701 if (isl_tab_add_ineq(tab, bound->el) < 0)
11702 goto error;
11703 empty = tab->empty;
11704 isl_int_set_si(bound->el[1 + first + i], 0);
11705 if (isl_tab_rollback(tab, snap) < 0)
11706 goto error;
11708 if (empty) {
11709 signs[i] = 1;
11710 continue;
11713 isl_int_set_si(bound->el[1 + first + i], 1);
11714 if (isl_tab_add_ineq(tab, bound->el) < 0)
11715 goto error;
11716 empty = tab->empty;
11717 isl_int_set_si(bound->el[1 + first + i], 0);
11718 if (isl_tab_rollback(tab, snap) < 0)
11719 goto error;
11721 signs[i] = empty ? -1 : 0;
11724 isl_tab_free(tab);
11725 isl_vec_free(bound);
11726 return isl_stat_ok;
11727 error:
11728 isl_tab_free(tab);
11729 isl_vec_free(bound);
11730 return isl_stat_error;
11733 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11734 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11736 if (!bset || !signs)
11737 return isl_stat_error;
11738 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11739 return isl_stat_error;
11741 first += pos(bset->dim, type) - 1;
11742 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11745 /* Is it possible for the integer division "div" to depend (possibly
11746 * indirectly) on any output dimensions?
11748 * If the div is undefined, then we conservatively assume that it
11749 * may depend on them.
11750 * Otherwise, we check if it actually depends on them or on any integer
11751 * divisions that may depend on them.
11753 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11755 int i;
11756 isl_size n_out, n_div;
11757 unsigned o_out, o_div;
11759 if (isl_int_is_zero(bmap->div[div][0]))
11760 return isl_bool_true;
11762 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11763 if (n_out < 0)
11764 return isl_bool_error;
11765 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11767 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11768 return isl_bool_true;
11770 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11771 if (n_div < 0)
11772 return isl_bool_error;
11773 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11775 for (i = 0; i < n_div; ++i) {
11776 isl_bool may_involve;
11778 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11779 continue;
11780 may_involve = div_may_involve_output(bmap, i);
11781 if (may_involve < 0 || may_involve)
11782 return may_involve;
11785 return isl_bool_false;
11788 /* Return the first integer division of "bmap" in the range
11789 * [first, first + n[ that may depend on any output dimensions and
11790 * that has a non-zero coefficient in "c" (where the first coefficient
11791 * in "c" corresponds to integer division "first").
11793 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11794 isl_int *c, int first, int n)
11796 int k;
11798 if (!bmap)
11799 return -1;
11801 for (k = first; k < first + n; ++k) {
11802 isl_bool may_involve;
11804 if (isl_int_is_zero(c[k]))
11805 continue;
11806 may_involve = div_may_involve_output(bmap, k);
11807 if (may_involve < 0)
11808 return -1;
11809 if (may_involve)
11810 return k;
11813 return first + n;
11816 /* Look for a pair of inequality constraints in "bmap" of the form
11818 * -l + i >= 0 or i >= l
11819 * and
11820 * n + l - i >= 0 or i <= l + n
11822 * with n < "m" and i the output dimension at position "pos".
11823 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11824 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11825 * and earlier output dimensions, as well as integer divisions that do
11826 * not involve any of the output dimensions.
11828 * Return the index of the first inequality constraint or bmap->n_ineq
11829 * if no such pair can be found.
11831 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11832 int pos, isl_int m)
11834 int i, j;
11835 isl_ctx *ctx;
11836 isl_size total;
11837 isl_size n_div, n_out;
11838 unsigned o_div, o_out;
11839 int less;
11841 total = isl_basic_map_dim(bmap, isl_dim_all);
11842 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11843 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11844 if (total < 0 || n_out < 0 || n_div < 0)
11845 return -1;
11847 ctx = isl_basic_map_get_ctx(bmap);
11848 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11849 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11850 for (i = 0; i < bmap->n_ineq; ++i) {
11851 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11852 continue;
11853 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11854 n_out - (pos + 1)) != -1)
11855 continue;
11856 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11857 0, n_div) < n_div)
11858 continue;
11859 for (j = i + 1; j < bmap->n_ineq; ++j) {
11860 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11861 ctx->one))
11862 continue;
11863 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11864 bmap->ineq[j] + 1, total))
11865 continue;
11866 break;
11868 if (j >= bmap->n_ineq)
11869 continue;
11870 isl_int_add(bmap->ineq[i][0],
11871 bmap->ineq[i][0], bmap->ineq[j][0]);
11872 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11873 isl_int_sub(bmap->ineq[i][0],
11874 bmap->ineq[i][0], bmap->ineq[j][0]);
11875 if (!less)
11876 continue;
11877 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11878 return i;
11879 else
11880 return j;
11883 return bmap->n_ineq;
11886 /* Return the index of the equality of "bmap" that defines
11887 * the output dimension "pos" in terms of earlier dimensions.
11888 * The equality may also involve integer divisions, as long
11889 * as those integer divisions are defined in terms of
11890 * parameters or input dimensions.
11891 * In this case, *div is set to the number of integer divisions and
11892 * *ineq is set to the number of inequality constraints (provided
11893 * div and ineq are not NULL).
11895 * The equality may also involve a single integer division involving
11896 * the output dimensions (typically only output dimension "pos") as
11897 * long as the coefficient of output dimension "pos" is 1 or -1 and
11898 * there is a pair of constraints i >= l and i <= l + n, with i referring
11899 * to output dimension "pos", l an expression involving only earlier
11900 * dimensions and n smaller than the coefficient of the integer division
11901 * in the equality. In this case, the output dimension can be defined
11902 * in terms of a modulo expression that does not involve the integer division.
11903 * *div is then set to this single integer division and
11904 * *ineq is set to the index of constraint i >= l.
11906 * Return bmap->n_eq if there is no such equality.
11907 * Return -1 on error.
11909 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11910 int pos, int *div, int *ineq)
11912 int j, k, l;
11913 isl_size n_div, n_out;
11914 unsigned o_div, o_out;
11916 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11917 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11918 if (n_out < 0 || n_div < 0)
11919 return -1;
11921 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11922 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11924 if (ineq)
11925 *ineq = bmap->n_ineq;
11926 if (div)
11927 *div = n_div;
11928 for (j = 0; j < bmap->n_eq; ++j) {
11929 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11930 continue;
11931 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11932 n_out - (pos + 1)) != -1)
11933 continue;
11934 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11935 0, n_div);
11936 if (k >= n_div)
11937 return j;
11938 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11939 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11940 continue;
11941 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11942 k + 1, n_div - (k+1)) < n_div)
11943 continue;
11944 l = find_modulo_constraint_pair(bmap, pos,
11945 bmap->eq[j][o_div + k]);
11946 if (l < 0)
11947 return -1;
11948 if (l >= bmap->n_ineq)
11949 continue;
11950 if (div)
11951 *div = k;
11952 if (ineq)
11953 *ineq = l;
11954 return j;
11957 return bmap->n_eq;
11960 /* Check if the given basic map is obviously single-valued.
11961 * In particular, for each output dimension, check that there is
11962 * an equality that defines the output dimension in terms of
11963 * earlier dimensions.
11965 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11967 int i;
11968 isl_size n_out;
11970 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11971 if (n_out < 0)
11972 return isl_bool_error;
11974 for (i = 0; i < n_out; ++i) {
11975 int eq;
11977 eq = isl_basic_map_output_defining_equality(bmap, i,
11978 NULL, NULL);
11979 if (eq < 0)
11980 return isl_bool_error;
11981 if (eq >= bmap->n_eq)
11982 return isl_bool_false;
11985 return isl_bool_true;
11988 /* Check if the given basic map is single-valued.
11989 * We simply compute
11991 * M \circ M^-1
11993 * and check if the result is a subset of the identity mapping.
11995 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11997 isl_space *space;
11998 isl_basic_map *test;
11999 isl_basic_map *id;
12000 isl_bool sv;
12002 sv = isl_basic_map_plain_is_single_valued(bmap);
12003 if (sv < 0 || sv)
12004 return sv;
12006 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12007 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12009 space = isl_basic_map_get_space(bmap);
12010 space = isl_space_map_from_set(isl_space_range(space));
12011 id = isl_basic_map_identity(space);
12013 sv = isl_basic_map_is_subset(test, id);
12015 isl_basic_map_free(test);
12016 isl_basic_map_free(id);
12018 return sv;
12021 /* Check if the given map is obviously single-valued.
12023 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12025 if (!map)
12026 return isl_bool_error;
12027 if (map->n == 0)
12028 return isl_bool_true;
12029 if (map->n >= 2)
12030 return isl_bool_false;
12032 return isl_basic_map_plain_is_single_valued(map->p[0]);
12035 /* Check if the given map is single-valued.
12036 * We simply compute
12038 * M \circ M^-1
12040 * and check if the result is a subset of the identity mapping.
12042 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12044 isl_space *space;
12045 isl_map *test;
12046 isl_map *id;
12047 isl_bool sv;
12049 sv = isl_map_plain_is_single_valued(map);
12050 if (sv < 0 || sv)
12051 return sv;
12053 test = isl_map_reverse(isl_map_copy(map));
12054 test = isl_map_apply_range(test, isl_map_copy(map));
12056 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12057 id = isl_map_identity(space);
12059 sv = isl_map_is_subset(test, id);
12061 isl_map_free(test);
12062 isl_map_free(id);
12064 return sv;
12067 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12069 isl_bool in;
12071 map = isl_map_copy(map);
12072 map = isl_map_reverse(map);
12073 in = isl_map_is_single_valued(map);
12074 isl_map_free(map);
12076 return in;
12079 /* Check if the given map is obviously injective.
12081 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12083 isl_bool in;
12085 map = isl_map_copy(map);
12086 map = isl_map_reverse(map);
12087 in = isl_map_plain_is_single_valued(map);
12088 isl_map_free(map);
12090 return in;
12093 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12095 isl_bool sv;
12097 sv = isl_map_is_single_valued(map);
12098 if (sv < 0 || !sv)
12099 return sv;
12101 return isl_map_is_injective(map);
12104 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12106 return isl_map_is_single_valued(set_to_map(set));
12109 /* Does "map" only map elements to themselves?
12111 * If the domain and range spaces are different, then "map"
12112 * is considered not to be an identity relation, even if it is empty.
12113 * Otherwise, construct the maximal identity relation and
12114 * check whether "map" is a subset of this relation.
12116 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12118 isl_map *id;
12119 isl_bool equal, is_identity;
12121 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12122 if (equal < 0 || !equal)
12123 return equal;
12125 id = isl_map_identity(isl_map_get_space(map));
12126 is_identity = isl_map_is_subset(map, id);
12127 isl_map_free(id);
12129 return is_identity;
12132 int isl_map_is_translation(__isl_keep isl_map *map)
12134 int ok;
12135 isl_set *delta;
12137 delta = isl_map_deltas(isl_map_copy(map));
12138 ok = isl_set_is_singleton(delta);
12139 isl_set_free(delta);
12141 return ok;
12144 static int unique(isl_int *p, unsigned pos, unsigned len)
12146 if (isl_seq_first_non_zero(p, pos) != -1)
12147 return 0;
12148 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12149 return 0;
12150 return 1;
12153 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12155 int i, j;
12156 isl_size nvar, ovar, n_div;
12158 n_div = isl_basic_set_dim(bset, isl_dim_div);
12159 if (n_div < 0)
12160 return isl_bool_error;
12161 if (n_div != 0)
12162 return isl_bool_false;
12164 nvar = isl_basic_set_dim(bset, isl_dim_set);
12165 ovar = isl_space_offset(bset->dim, isl_dim_set);
12166 if (nvar < 0 || ovar < 0)
12167 return isl_bool_error;
12168 for (j = 0; j < nvar; ++j) {
12169 int lower = 0, upper = 0;
12170 for (i = 0; i < bset->n_eq; ++i) {
12171 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12172 continue;
12173 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12174 return isl_bool_false;
12175 break;
12177 if (i < bset->n_eq)
12178 continue;
12179 for (i = 0; i < bset->n_ineq; ++i) {
12180 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12181 continue;
12182 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12183 return isl_bool_false;
12184 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12185 lower = 1;
12186 else
12187 upper = 1;
12189 if (!lower || !upper)
12190 return isl_bool_false;
12193 return isl_bool_true;
12196 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12198 if (!set)
12199 return isl_bool_error;
12200 if (set->n != 1)
12201 return isl_bool_false;
12203 return isl_basic_set_is_box(set->p[0]);
12206 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12208 if (!bset)
12209 return isl_bool_error;
12211 return isl_space_is_wrapping(bset->dim);
12214 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12216 if (!set)
12217 return isl_bool_error;
12219 return isl_space_is_wrapping(set->dim);
12222 /* Modify the space of "map" through a call to "change".
12223 * If "can_change" is set (not NULL), then first call it to check
12224 * if the modification is allowed, printing the error message "cannot_change"
12225 * if it is not.
12227 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12228 isl_bool (*can_change)(__isl_keep isl_map *map),
12229 const char *cannot_change,
12230 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12232 isl_bool ok;
12233 isl_space *space;
12235 if (!map)
12236 return NULL;
12238 ok = can_change ? can_change(map) : isl_bool_true;
12239 if (ok < 0)
12240 return isl_map_free(map);
12241 if (!ok)
12242 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12243 return isl_map_free(map));
12245 space = change(isl_map_get_space(map));
12246 map = isl_map_reset_space(map, space);
12248 return map;
12251 /* Is the domain of "map" a wrapped relation?
12253 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12255 if (!map)
12256 return isl_bool_error;
12258 return isl_space_domain_is_wrapping(map->dim);
12261 /* Does "map" have a wrapped relation in both domain and range?
12263 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12265 return isl_space_is_product(isl_map_peek_space(map));
12268 /* Is the range of "map" a wrapped relation?
12270 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12272 if (!map)
12273 return isl_bool_error;
12275 return isl_space_range_is_wrapping(map->dim);
12278 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12280 isl_space *space;
12282 space = isl_basic_map_take_space(bmap);
12283 space = isl_space_wrap(space);
12284 bmap = isl_basic_map_restore_space(bmap, space);
12286 bmap = isl_basic_map_finalize(bmap);
12288 return bset_from_bmap(bmap);
12291 /* Given a map A -> B, return the set (A -> B).
12293 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12295 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12298 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12300 bset = isl_basic_set_cow(bset);
12301 if (!bset)
12302 return NULL;
12304 bset->dim = isl_space_unwrap(bset->dim);
12305 if (!bset->dim)
12306 goto error;
12308 bset = isl_basic_set_finalize(bset);
12310 return bset_to_bmap(bset);
12311 error:
12312 isl_basic_set_free(bset);
12313 return NULL;
12316 /* Given a set (A -> B), return the map A -> B.
12317 * Error out if "set" is not of the form (A -> B).
12319 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12321 return isl_map_change_space(set, &isl_set_is_wrapping,
12322 "not a wrapping set", &isl_space_unwrap);
12325 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12326 enum isl_dim_type type)
12328 isl_space *space;
12330 space = isl_basic_map_take_space(bmap);
12331 space = isl_space_reset(space, type);
12332 bmap = isl_basic_map_restore_space(bmap, space);
12334 bmap = isl_basic_map_mark_final(bmap);
12336 return bmap;
12339 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12340 enum isl_dim_type type)
12342 int i;
12343 isl_space *space;
12345 if (!map)
12346 return NULL;
12348 if (!isl_space_is_named_or_nested(map->dim, type))
12349 return map;
12351 map = isl_map_cow(map);
12352 if (!map)
12353 return NULL;
12355 for (i = 0; i < map->n; ++i) {
12356 map->p[i] = isl_basic_map_reset(map->p[i], type);
12357 if (!map->p[i])
12358 goto error;
12361 space = isl_map_take_space(map);
12362 space = isl_space_reset(space, type);
12363 map = isl_map_restore_space(map, space);
12365 return map;
12366 error:
12367 isl_map_free(map);
12368 return NULL;
12371 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12373 isl_space *space;
12375 space = isl_basic_map_take_space(bmap);
12376 space = isl_space_flatten(space);
12377 bmap = isl_basic_map_restore_space(bmap, space);
12379 bmap = isl_basic_map_mark_final(bmap);
12381 return bmap;
12384 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12386 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12389 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12390 __isl_take isl_basic_map *bmap)
12392 isl_space *space;
12394 space = isl_basic_map_take_space(bmap);
12395 space = isl_space_flatten_domain(space);
12396 bmap = isl_basic_map_restore_space(bmap, space);
12398 bmap = isl_basic_map_mark_final(bmap);
12400 return bmap;
12403 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12404 __isl_take isl_basic_map *bmap)
12406 isl_space *space;
12408 space = isl_basic_map_take_space(bmap);
12409 space = isl_space_flatten_range(space);
12410 bmap = isl_basic_map_restore_space(bmap, space);
12412 bmap = isl_basic_map_mark_final(bmap);
12414 return bmap;
12417 /* Remove any internal structure from the spaces of domain and range of "map".
12419 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12421 if (!map)
12422 return NULL;
12424 if (!map->dim->nested[0] && !map->dim->nested[1])
12425 return map;
12427 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12430 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12432 return set_from_map(isl_map_flatten(set_to_map(set)));
12435 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12437 isl_space *space, *flat_space;
12438 isl_map *map;
12440 space = isl_set_get_space(set);
12441 flat_space = isl_space_flatten(isl_space_copy(space));
12442 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12443 flat_space));
12444 map = isl_map_intersect_domain(map, set);
12446 return map;
12449 /* Remove any internal structure from the space of the domain of "map".
12451 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12453 if (!map)
12454 return NULL;
12456 if (!map->dim->nested[0])
12457 return map;
12459 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12462 /* Remove any internal structure from the space of the range of "map".
12464 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12466 if (!map)
12467 return NULL;
12469 if (!map->dim->nested[1])
12470 return map;
12472 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12475 /* Reorder the dimensions of "bmap" according to the given dim_map
12476 * and set the dimension specification to "space" and
12477 * perform Gaussian elimination on the result.
12479 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12480 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12482 isl_basic_map *res;
12483 unsigned flags;
12484 isl_size n_div;
12486 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12487 if (n_div < 0 || !space || !dim_map)
12488 goto error;
12490 flags = bmap->flags;
12491 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12492 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12493 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12494 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12495 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12496 if (res)
12497 res->flags = flags;
12498 res = isl_basic_map_gauss(res, NULL);
12499 res = isl_basic_map_finalize(res);
12500 return res;
12501 error:
12502 isl_dim_map_free(dim_map);
12503 isl_basic_map_free(bmap);
12504 isl_space_free(space);
12505 return NULL;
12508 /* Reorder the dimensions of "map" according to given reordering.
12510 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12511 __isl_take isl_reordering *r)
12513 int i;
12514 struct isl_dim_map *dim_map;
12516 map = isl_map_cow(map);
12517 dim_map = isl_dim_map_from_reordering(r);
12518 if (!map || !r || !dim_map)
12519 goto error;
12521 for (i = 0; i < map->n; ++i) {
12522 struct isl_dim_map *dim_map_i;
12523 isl_space *space;
12525 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12527 space = isl_reordering_get_space(r);
12528 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12530 if (!map->p[i])
12531 goto error;
12534 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12535 map = isl_map_unmark_normalized(map);
12537 isl_reordering_free(r);
12538 isl_dim_map_free(dim_map);
12539 return map;
12540 error:
12541 isl_dim_map_free(dim_map);
12542 isl_map_free(map);
12543 isl_reordering_free(r);
12544 return NULL;
12547 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12548 __isl_take isl_reordering *r)
12550 return set_from_map(isl_map_realign(set_to_map(set), r));
12553 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12554 __isl_take isl_space *model)
12556 isl_ctx *ctx;
12557 isl_bool aligned;
12559 if (!map || !model)
12560 goto error;
12562 ctx = isl_space_get_ctx(model);
12563 if (!isl_space_has_named_params(model))
12564 isl_die(ctx, isl_error_invalid,
12565 "model has unnamed parameters", goto error);
12566 if (isl_map_check_named_params(map) < 0)
12567 goto error;
12568 aligned = isl_map_space_has_equal_params(map, model);
12569 if (aligned < 0)
12570 goto error;
12571 if (!aligned) {
12572 isl_space *space;
12573 isl_reordering *exp;
12575 space = isl_map_peek_space(map);
12576 exp = isl_parameter_alignment_reordering(space, model);
12577 map = isl_map_realign(map, exp);
12580 isl_space_free(model);
12581 return map;
12582 error:
12583 isl_space_free(model);
12584 isl_map_free(map);
12585 return NULL;
12588 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12589 __isl_take isl_space *model)
12591 return isl_map_align_params(set, model);
12594 /* Align the parameters of "bmap" to those of "model", introducing
12595 * additional parameters if needed.
12597 __isl_give isl_basic_map *isl_basic_map_align_params(
12598 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12600 isl_ctx *ctx;
12601 isl_bool equal_params;
12602 isl_space *bmap_space;
12604 if (!bmap || !model)
12605 goto error;
12607 ctx = isl_space_get_ctx(model);
12608 if (!isl_space_has_named_params(model))
12609 isl_die(ctx, isl_error_invalid,
12610 "model has unnamed parameters", goto error);
12611 if (isl_basic_map_check_named_params(bmap) < 0)
12612 goto error;
12613 bmap_space = isl_basic_map_peek_space(bmap);
12614 equal_params = isl_space_has_equal_params(bmap_space, model);
12615 if (equal_params < 0)
12616 goto error;
12617 if (!equal_params) {
12618 isl_reordering *exp;
12619 struct isl_dim_map *dim_map;
12621 exp = isl_parameter_alignment_reordering(bmap_space, model);
12622 dim_map = isl_dim_map_from_reordering(exp);
12623 bmap = isl_basic_map_realign(bmap,
12624 isl_reordering_get_space(exp),
12625 isl_dim_map_extend(dim_map, bmap));
12626 isl_reordering_free(exp);
12627 isl_dim_map_free(dim_map);
12630 isl_space_free(model);
12631 return bmap;
12632 error:
12633 isl_space_free(model);
12634 isl_basic_map_free(bmap);
12635 return NULL;
12638 /* Do "bset" and "space" have the same parameters?
12640 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12641 __isl_keep isl_space *space)
12643 isl_space *bset_space;
12645 bset_space = isl_basic_set_peek_space(bset);
12646 return isl_space_has_equal_params(bset_space, space);
12649 /* Do "map" and "space" have the same parameters?
12651 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12652 __isl_keep isl_space *space)
12654 isl_space *map_space;
12656 map_space = isl_map_peek_space(map);
12657 return isl_space_has_equal_params(map_space, space);
12660 /* Do "set" and "space" have the same parameters?
12662 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12663 __isl_keep isl_space *space)
12665 return isl_map_space_has_equal_params(set_to_map(set), space);
12668 /* Align the parameters of "bset" to those of "model", introducing
12669 * additional parameters if needed.
12671 __isl_give isl_basic_set *isl_basic_set_align_params(
12672 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12674 return isl_basic_map_align_params(bset, model);
12677 #undef TYPE
12678 #define TYPE isl_map
12679 #define isl_map_drop_dims isl_map_drop
12680 #include "isl_drop_unused_params_templ.c"
12682 /* Drop all parameters not referenced by "set".
12684 __isl_give isl_set *isl_set_drop_unused_params(
12685 __isl_take isl_set *set)
12687 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12690 #undef TYPE
12691 #define TYPE isl_basic_map
12692 #define isl_basic_map_drop_dims isl_basic_map_drop
12693 #include "isl_drop_unused_params_templ.c"
12695 /* Drop all parameters not referenced by "bset".
12697 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12698 __isl_take isl_basic_set *bset)
12700 return bset_from_bmap(isl_basic_map_drop_unused_params(
12701 bset_to_bmap(bset)));
12704 /* Given a tuple of identifiers "tuple" in a space that corresponds
12705 * to that of "set", if any of those identifiers appear as parameters
12706 * in "set", then equate those parameters with the corresponding
12707 * set dimensions and project out the parameters.
12708 * The result therefore has no such parameters.
12710 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12711 __isl_keep isl_multi_id *tuple)
12713 int i;
12714 isl_size n;
12715 isl_space *set_space, *tuple_space;
12717 set_space = isl_set_peek_space(set);
12718 tuple_space = isl_multi_id_peek_space(tuple);
12719 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12720 return isl_set_free(set);
12721 n = isl_multi_id_size(tuple);
12722 if (n < 0)
12723 return isl_set_free(set);
12724 for (i = 0; i < n; ++i) {
12725 isl_id *id;
12726 int pos;
12728 id = isl_multi_id_get_at(tuple, i);
12729 if (!id)
12730 return isl_set_free(set);
12731 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12732 isl_id_free(id);
12733 if (pos < 0)
12734 continue;
12735 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12736 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12738 return set;
12741 /* Bind the set dimensions of "set" to parameters with identifiers
12742 * specified by "tuple", living in the same space as "set".
12744 * If no parameters with these identifiers appear in "set" already,
12745 * then the set dimensions are simply reinterpreted as parameters.
12746 * Otherwise, the parameters are first equated to the corresponding
12747 * set dimensions.
12749 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12750 __isl_take isl_multi_id *tuple)
12752 isl_space *space;
12754 set = equate_params(set, tuple);
12755 space = isl_set_get_space(set);
12756 space = isl_space_bind_set(space, tuple);
12757 isl_multi_id_free(tuple);
12758 set = isl_set_reset_space(set, space);
12760 return set;
12763 /* Given a tuple of identifiers "tuple" in a space that corresponds
12764 * to the domain of "map", if any of those identifiers appear as parameters
12765 * in "map", then equate those parameters with the corresponding
12766 * input dimensions and project out the parameters.
12767 * The result therefore has no such parameters.
12769 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12770 __isl_keep isl_multi_id *tuple)
12772 int i;
12773 isl_size n;
12774 isl_space *map_space, *tuple_space;
12776 map_space = isl_map_peek_space(map);
12777 tuple_space = isl_multi_id_peek_space(tuple);
12778 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12779 return isl_map_free(map);
12780 n = isl_multi_id_size(tuple);
12781 if (n < 0)
12782 return isl_map_free(map);
12783 for (i = 0; i < n; ++i) {
12784 isl_id *id;
12785 int pos;
12787 id = isl_multi_id_get_at(tuple, i);
12788 if (!id)
12789 return isl_map_free(map);
12790 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12791 isl_id_free(id);
12792 if (pos < 0)
12793 continue;
12794 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12795 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12797 return map;
12800 /* Bind the input dimensions of "map" to parameters with identifiers
12801 * specified by "tuple", living in the domain space of "map".
12803 * If no parameters with these identifiers appear in "map" already,
12804 * then the input dimensions are simply reinterpreted as parameters.
12805 * Otherwise, the parameters are first equated to the corresponding
12806 * input dimensions.
12808 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12809 __isl_take isl_multi_id *tuple)
12811 isl_space *space;
12812 isl_set *set;
12814 map = map_equate_params(map, tuple);
12815 space = isl_map_get_space(map);
12816 space = isl_space_bind_map_domain(space, tuple);
12817 isl_multi_id_free(tuple);
12818 set = set_from_map(isl_map_reset_space(map, space));
12820 return set;
12823 /* Bind the output dimensions of "map" to parameters with identifiers
12824 * specified by "tuple", living in the range space of "map".
12826 * Since binding is more easily implemented on the domain,
12827 * bind the input dimensions of the inverse of "map".
12829 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12830 __isl_take isl_multi_id *tuple)
12832 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12835 /* Insert a domain corresponding to "tuple"
12836 * into the nullary or unary relation "set".
12837 * The result has an extra initial tuple and is therefore
12838 * either a unary or binary relation.
12839 * Any parameters with identifiers in "tuple" are reinterpreted
12840 * as the corresponding domain dimensions.
12842 static __isl_give isl_map *unbind_params_insert_domain(
12843 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12845 isl_space *space;
12846 isl_reordering *r;
12848 space = isl_set_peek_space(set);
12849 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12850 isl_multi_id_free(tuple);
12852 return isl_map_realign(set_to_map(set), r);
12855 /* Construct a set with "tuple" as domain from the parameter domain "set".
12856 * Any parameters with identifiers in "tuple" are reinterpreted
12857 * as the corresponding set dimensions.
12859 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12860 __isl_take isl_multi_id *tuple)
12862 isl_bool is_params;
12864 is_params = isl_set_is_params(set);
12865 if (is_params < 0)
12866 set = isl_set_free(set);
12867 else if (!is_params)
12868 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12869 "expecting parameter domain", set = isl_set_free(set));
12870 return set_from_map(unbind_params_insert_domain(set, tuple));
12873 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12875 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12877 isl_bool is_params;
12879 is_params = isl_set_is_params(set);
12880 if (is_params < 0)
12881 return isl_stat_error;
12882 else if (is_params)
12883 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12884 "expecting proper set", return isl_stat_error);
12886 return isl_stat_ok;
12889 /* Construct a map with "domain" as domain and "set" as range.
12890 * Any parameters with identifiers in "domain" are reinterpreted
12891 * as the corresponding domain dimensions.
12893 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12894 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12896 if (isl_set_check_is_set(set) < 0)
12897 set = isl_set_free(set);
12898 return unbind_params_insert_domain(set, domain);
12901 /* Construct a map with "domain" as domain and "set" as range.
12903 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12904 __isl_take isl_space *domain)
12906 isl_size dim;
12907 isl_space *space;
12908 isl_map *map;
12910 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12911 domain = isl_space_free(domain);
12912 dim = isl_space_dim(domain, isl_dim_set);
12913 if (dim < 0)
12914 domain = isl_space_free(domain);
12915 space = isl_set_get_space(set);
12916 domain = isl_space_replace_params(domain, space);
12917 space = isl_space_map_from_domain_and_range(domain, space);
12919 map = isl_map_from_range(set);
12920 map = isl_map_add_dims(map, isl_dim_in, dim);
12921 map = isl_map_reset_space(map, space);
12923 return map;
12926 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12927 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12928 enum isl_dim_type c2, enum isl_dim_type c3,
12929 enum isl_dim_type c4, enum isl_dim_type c5)
12931 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12932 struct isl_mat *mat;
12933 int i, j, k;
12934 int pos;
12935 isl_size total;
12937 total = isl_basic_map_dim(bmap, isl_dim_all);
12938 if (total < 0)
12939 return NULL;
12940 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12941 if (!mat)
12942 return NULL;
12943 for (i = 0; i < bmap->n_eq; ++i)
12944 for (j = 0, pos = 0; j < 5; ++j) {
12945 int off = isl_basic_map_offset(bmap, c[j]);
12946 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12947 if (dim < 0)
12948 return isl_mat_free(mat);
12949 for (k = 0; k < dim; ++k) {
12950 isl_int_set(mat->row[i][pos],
12951 bmap->eq[i][off + k]);
12952 ++pos;
12956 return mat;
12959 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12960 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12961 enum isl_dim_type c2, enum isl_dim_type c3,
12962 enum isl_dim_type c4, enum isl_dim_type c5)
12964 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12965 struct isl_mat *mat;
12966 int i, j, k;
12967 int pos;
12968 isl_size total;
12970 total = isl_basic_map_dim(bmap, isl_dim_all);
12971 if (total < 0)
12972 return NULL;
12973 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12974 if (!mat)
12975 return NULL;
12976 for (i = 0; i < bmap->n_ineq; ++i)
12977 for (j = 0, pos = 0; j < 5; ++j) {
12978 int off = isl_basic_map_offset(bmap, c[j]);
12979 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12980 if (dim < 0)
12981 return isl_mat_free(mat);
12982 for (k = 0; k < dim; ++k) {
12983 isl_int_set(mat->row[i][pos],
12984 bmap->ineq[i][off + k]);
12985 ++pos;
12989 return mat;
12992 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12993 __isl_take isl_space *space,
12994 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12995 enum isl_dim_type c2, enum isl_dim_type c3,
12996 enum isl_dim_type c4, enum isl_dim_type c5)
12998 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12999 isl_basic_map *bmap = NULL;
13000 isl_size dim;
13001 unsigned total;
13002 unsigned extra;
13003 int i, j, k, l;
13004 int pos;
13006 dim = isl_space_dim(space, isl_dim_all);
13007 if (dim < 0 || !eq || !ineq)
13008 goto error;
13010 if (eq->n_col != ineq->n_col)
13011 isl_die(space->ctx, isl_error_invalid,
13012 "equalities and inequalities matrices should have "
13013 "same number of columns", goto error);
13015 total = 1 + dim;
13017 if (eq->n_col < total)
13018 isl_die(space->ctx, isl_error_invalid,
13019 "number of columns too small", goto error);
13021 extra = eq->n_col - total;
13023 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13024 eq->n_row, ineq->n_row);
13025 if (!bmap)
13026 goto error;
13027 for (i = 0; i < extra; ++i) {
13028 k = isl_basic_map_alloc_div(bmap);
13029 if (k < 0)
13030 goto error;
13031 isl_int_set_si(bmap->div[k][0], 0);
13033 for (i = 0; i < eq->n_row; ++i) {
13034 l = isl_basic_map_alloc_equality(bmap);
13035 if (l < 0)
13036 goto error;
13037 for (j = 0, pos = 0; j < 5; ++j) {
13038 int off = isl_basic_map_offset(bmap, c[j]);
13039 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13040 if (dim < 0)
13041 goto error;
13042 for (k = 0; k < dim; ++k) {
13043 isl_int_set(bmap->eq[l][off + k],
13044 eq->row[i][pos]);
13045 ++pos;
13049 for (i = 0; i < ineq->n_row; ++i) {
13050 l = isl_basic_map_alloc_inequality(bmap);
13051 if (l < 0)
13052 goto error;
13053 for (j = 0, pos = 0; j < 5; ++j) {
13054 int off = isl_basic_map_offset(bmap, c[j]);
13055 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13056 if (dim < 0)
13057 goto error;
13058 for (k = 0; k < dim; ++k) {
13059 isl_int_set(bmap->ineq[l][off + k],
13060 ineq->row[i][pos]);
13061 ++pos;
13066 isl_space_free(space);
13067 isl_mat_free(eq);
13068 isl_mat_free(ineq);
13070 bmap = isl_basic_map_simplify(bmap);
13071 return isl_basic_map_finalize(bmap);
13072 error:
13073 isl_space_free(space);
13074 isl_mat_free(eq);
13075 isl_mat_free(ineq);
13076 isl_basic_map_free(bmap);
13077 return NULL;
13080 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13081 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13082 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13084 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13085 c1, c2, c3, c4, isl_dim_in);
13088 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13089 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13090 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13092 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13093 c1, c2, c3, c4, isl_dim_in);
13096 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13097 __isl_take isl_space *space,
13098 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13099 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13101 isl_basic_map *bmap;
13102 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13103 c1, c2, c3, c4, isl_dim_in);
13104 return bset_from_bmap(bmap);
13107 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13109 if (!bmap)
13110 return isl_bool_error;
13112 return isl_space_can_zip(bmap->dim);
13115 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13117 if (!map)
13118 return isl_bool_error;
13120 return isl_space_can_zip(map->dim);
13123 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13124 * (A -> C) -> (B -> D).
13126 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13128 unsigned pos;
13129 isl_size n_in;
13130 isl_size n1;
13131 isl_size n2;
13133 if (!bmap)
13134 return NULL;
13136 if (!isl_basic_map_can_zip(bmap))
13137 isl_die(bmap->ctx, isl_error_invalid,
13138 "basic map cannot be zipped", goto error);
13139 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13140 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13141 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13142 if (n_in < 0 || n1 < 0 || n2 < 0)
13143 return isl_basic_map_free(bmap);
13144 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13145 bmap = isl_basic_map_cow(bmap);
13146 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13147 if (!bmap)
13148 return NULL;
13149 bmap->dim = isl_space_zip(bmap->dim);
13150 if (!bmap->dim)
13151 goto error;
13152 bmap = isl_basic_map_mark_final(bmap);
13153 return bmap;
13154 error:
13155 isl_basic_map_free(bmap);
13156 return NULL;
13159 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13160 * (A -> C) -> (B -> D).
13162 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13164 if (!map)
13165 return NULL;
13167 if (!isl_map_can_zip(map))
13168 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13169 goto error);
13171 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13172 error:
13173 isl_map_free(map);
13174 return NULL;
13177 /* Can we apply isl_basic_map_curry to "bmap"?
13178 * That is, does it have a nested relation in its domain?
13180 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13182 if (!bmap)
13183 return isl_bool_error;
13185 return isl_space_can_curry(bmap->dim);
13188 /* Can we apply isl_map_curry to "map"?
13189 * That is, does it have a nested relation in its domain?
13191 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13193 if (!map)
13194 return isl_bool_error;
13196 return isl_space_can_curry(map->dim);
13199 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13200 * A -> (B -> C).
13202 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13205 if (!bmap)
13206 return NULL;
13208 if (!isl_basic_map_can_curry(bmap))
13209 isl_die(bmap->ctx, isl_error_invalid,
13210 "basic map cannot be curried", goto error);
13211 bmap = isl_basic_map_cow(bmap);
13212 if (!bmap)
13213 return NULL;
13214 bmap->dim = isl_space_curry(bmap->dim);
13215 if (!bmap->dim)
13216 goto error;
13217 bmap = isl_basic_map_mark_final(bmap);
13218 return bmap;
13219 error:
13220 isl_basic_map_free(bmap);
13221 return NULL;
13224 /* Given a map (A -> B) -> C, return the corresponding map
13225 * A -> (B -> C).
13227 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13229 return isl_map_change_space(map, &isl_map_can_curry,
13230 "map cannot be curried", &isl_space_curry);
13233 /* Can isl_map_range_curry be applied to "map"?
13234 * That is, does it have a nested relation in its range,
13235 * the domain of which is itself a nested relation?
13237 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13239 if (!map)
13240 return isl_bool_error;
13242 return isl_space_can_range_curry(map->dim);
13245 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13246 * A -> (B -> (C -> D)).
13248 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13250 return isl_map_change_space(map, &isl_map_can_range_curry,
13251 "map range cannot be curried",
13252 &isl_space_range_curry);
13255 /* Can we apply isl_basic_map_uncurry to "bmap"?
13256 * That is, does it have a nested relation in its domain?
13258 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13260 if (!bmap)
13261 return isl_bool_error;
13263 return isl_space_can_uncurry(bmap->dim);
13266 /* Can we apply isl_map_uncurry to "map"?
13267 * That is, does it have a nested relation in its domain?
13269 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13271 if (!map)
13272 return isl_bool_error;
13274 return isl_space_can_uncurry(map->dim);
13277 /* Given a basic map A -> (B -> C), return the corresponding basic map
13278 * (A -> B) -> C.
13280 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13283 if (!bmap)
13284 return NULL;
13286 if (!isl_basic_map_can_uncurry(bmap))
13287 isl_die(bmap->ctx, isl_error_invalid,
13288 "basic map cannot be uncurried",
13289 return isl_basic_map_free(bmap));
13290 bmap = isl_basic_map_cow(bmap);
13291 if (!bmap)
13292 return NULL;
13293 bmap->dim = isl_space_uncurry(bmap->dim);
13294 if (!bmap->dim)
13295 return isl_basic_map_free(bmap);
13296 bmap = isl_basic_map_mark_final(bmap);
13297 return bmap;
13300 /* Given a map A -> (B -> C), return the corresponding map
13301 * (A -> B) -> C.
13303 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13305 return isl_map_change_space(map, &isl_map_can_uncurry,
13306 "map cannot be uncurried", &isl_space_uncurry);
13309 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13310 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13312 return isl_map_equate(set, type1, pos1, type2, pos2);
13315 /* Construct a basic map where the given dimensions are equal to each other.
13317 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13318 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13320 isl_basic_map *bmap = NULL;
13321 int i;
13322 isl_size total;
13324 total = isl_space_dim(space, isl_dim_all);
13325 if (total < 0 ||
13326 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13327 isl_space_check_range(space, type2, pos2, 1) < 0)
13328 goto error;
13330 if (type1 == type2 && pos1 == pos2)
13331 return isl_basic_map_universe(space);
13333 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13334 i = isl_basic_map_alloc_equality(bmap);
13335 if (i < 0)
13336 goto error;
13337 isl_seq_clr(bmap->eq[i], 1 + total);
13338 pos1 += isl_basic_map_offset(bmap, type1);
13339 pos2 += isl_basic_map_offset(bmap, type2);
13340 isl_int_set_si(bmap->eq[i][pos1], -1);
13341 isl_int_set_si(bmap->eq[i][pos2], 1);
13342 bmap = isl_basic_map_finalize(bmap);
13343 isl_space_free(space);
13344 return bmap;
13345 error:
13346 isl_space_free(space);
13347 isl_basic_map_free(bmap);
13348 return NULL;
13351 /* Add a constraint imposing that the given two dimensions are equal.
13353 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13354 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13356 isl_basic_map *eq;
13358 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13360 bmap = isl_basic_map_intersect(bmap, eq);
13362 return bmap;
13365 /* Add a constraint imposing that the given two dimensions are equal.
13367 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13368 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13370 isl_basic_map *bmap;
13372 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13374 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13376 return map;
13379 /* Add a constraint imposing that the given two dimensions have opposite values.
13381 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13382 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13384 isl_basic_map *bmap = NULL;
13385 int i;
13386 isl_size total;
13388 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13389 return isl_map_free(map);
13390 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13391 return isl_map_free(map);
13393 total = isl_map_dim(map, isl_dim_all);
13394 if (total < 0)
13395 return isl_map_free(map);
13396 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13397 i = isl_basic_map_alloc_equality(bmap);
13398 if (i < 0)
13399 goto error;
13400 isl_seq_clr(bmap->eq[i], 1 + total);
13401 pos1 += isl_basic_map_offset(bmap, type1);
13402 pos2 += isl_basic_map_offset(bmap, type2);
13403 isl_int_set_si(bmap->eq[i][pos1], 1);
13404 isl_int_set_si(bmap->eq[i][pos2], 1);
13405 bmap = isl_basic_map_finalize(bmap);
13407 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13409 return map;
13410 error:
13411 isl_basic_map_free(bmap);
13412 isl_map_free(map);
13413 return NULL;
13416 /* Construct a constraint imposing that the value of the first dimension is
13417 * greater than or equal to that of the second.
13419 static __isl_give isl_constraint *constraint_order_ge(
13420 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13421 enum isl_dim_type type2, int pos2)
13423 isl_constraint *c;
13425 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13426 isl_space_check_range(space, type2, pos2, 1) < 0)
13427 space = isl_space_free(space);
13428 if (!space)
13429 return NULL;
13431 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13433 if (type1 == type2 && pos1 == pos2)
13434 return c;
13436 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13437 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13439 return c;
13442 /* Add a constraint imposing that the value of the first dimension is
13443 * greater than or equal to that of the second.
13445 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13446 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13448 isl_constraint *c;
13449 isl_space *space;
13451 if (type1 == type2 && pos1 == pos2)
13452 return bmap;
13453 space = isl_basic_map_get_space(bmap);
13454 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13455 bmap = isl_basic_map_add_constraint(bmap, c);
13457 return bmap;
13460 /* Add a constraint imposing that the value of the first dimension is
13461 * greater than or equal to that of the second.
13463 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13464 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13466 isl_constraint *c;
13467 isl_space *space;
13469 if (type1 == type2 && pos1 == pos2)
13470 return map;
13471 space = isl_map_get_space(map);
13472 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13473 map = isl_map_add_constraint(map, c);
13475 return map;
13478 /* Add a constraint imposing that the value of the first dimension is
13479 * less than or equal to that of the second.
13481 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13482 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13484 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13487 /* Construct a basic map where the value of the first dimension is
13488 * greater than that of the second.
13490 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13491 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13493 isl_basic_map *bmap = NULL;
13494 int i;
13495 isl_size total;
13497 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13498 isl_space_check_range(space, type2, pos2, 1) < 0)
13499 goto error;
13501 if (type1 == type2 && pos1 == pos2)
13502 return isl_basic_map_empty(space);
13504 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13505 total = isl_basic_map_dim(bmap, isl_dim_all);
13506 i = isl_basic_map_alloc_inequality(bmap);
13507 if (total < 0 || i < 0)
13508 return isl_basic_map_free(bmap);
13509 isl_seq_clr(bmap->ineq[i], 1 + total);
13510 pos1 += isl_basic_map_offset(bmap, type1);
13511 pos2 += isl_basic_map_offset(bmap, type2);
13512 isl_int_set_si(bmap->ineq[i][pos1], 1);
13513 isl_int_set_si(bmap->ineq[i][pos2], -1);
13514 isl_int_set_si(bmap->ineq[i][0], -1);
13515 bmap = isl_basic_map_finalize(bmap);
13517 return bmap;
13518 error:
13519 isl_space_free(space);
13520 isl_basic_map_free(bmap);
13521 return NULL;
13524 /* Add a constraint imposing that the value of the first dimension is
13525 * greater than that of the second.
13527 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13528 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13530 isl_basic_map *gt;
13532 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13534 bmap = isl_basic_map_intersect(bmap, gt);
13536 return bmap;
13539 /* Add a constraint imposing that the value of the first dimension is
13540 * greater than that of the second.
13542 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13543 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13545 isl_basic_map *bmap;
13547 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13549 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13551 return map;
13554 /* Add a constraint imposing that the value of the first dimension is
13555 * smaller than that of the second.
13557 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13558 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13560 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13563 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13564 int pos)
13566 isl_aff *div;
13567 isl_local_space *ls;
13569 if (!bmap)
13570 return NULL;
13572 if (!isl_basic_map_divs_known(bmap))
13573 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13574 "some divs are unknown", return NULL);
13576 ls = isl_basic_map_get_local_space(bmap);
13577 div = isl_local_space_get_div(ls, pos);
13578 isl_local_space_free(ls);
13580 return div;
13583 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13584 int pos)
13586 return isl_basic_map_get_div(bset, pos);
13589 /* Plug in "subs" for set dimension "pos" of "set".
13591 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13592 unsigned pos, __isl_keep isl_aff *subs)
13594 isl_multi_aff *ma;
13596 if (set && isl_set_plain_is_empty(set))
13597 return set;
13599 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13600 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13601 return isl_set_preimage_multi_aff(set, ma);
13604 /* Check if the range of "ma" is compatible with the domain or range
13605 * (depending on "type") of "bmap".
13607 static isl_stat check_basic_map_compatible_range_multi_aff(
13608 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13609 __isl_keep isl_multi_aff *ma)
13611 isl_bool m;
13612 isl_space *ma_space;
13614 ma_space = isl_multi_aff_get_space(ma);
13616 m = isl_space_has_equal_params(bmap->dim, ma_space);
13617 if (m < 0)
13618 goto error;
13619 if (!m)
13620 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13621 "parameters don't match", goto error);
13622 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13623 if (m < 0)
13624 goto error;
13625 if (!m)
13626 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13627 "spaces don't match", goto error);
13629 isl_space_free(ma_space);
13630 return isl_stat_ok;
13631 error:
13632 isl_space_free(ma_space);
13633 return isl_stat_error;
13636 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13637 * coefficients before the transformed range of dimensions,
13638 * the "n_after" coefficients after the transformed range of dimensions
13639 * and the coefficients of the other divs in "bmap".
13641 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13642 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13644 int i;
13645 isl_size n_param;
13646 isl_size n_set;
13647 isl_local_space *ls;
13649 if (n_div == 0)
13650 return bmap;
13652 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13653 n_param = isl_local_space_dim(ls, isl_dim_param);
13654 n_set = isl_local_space_dim(ls, isl_dim_set);
13655 if (n_param < 0 || n_set < 0)
13656 return isl_basic_map_free(bmap);
13658 for (i = 0; i < n_div; ++i) {
13659 int o_bmap = 0, o_ls = 0;
13661 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13662 o_bmap += 1 + 1 + n_param;
13663 o_ls += 1 + 1 + n_param;
13664 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13665 o_bmap += n_before;
13666 isl_seq_cpy(bmap->div[i] + o_bmap,
13667 ls->div->row[i] + o_ls, n_set);
13668 o_bmap += n_set;
13669 o_ls += n_set;
13670 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13671 o_bmap += n_after;
13672 isl_seq_cpy(bmap->div[i] + o_bmap,
13673 ls->div->row[i] + o_ls, n_div);
13674 o_bmap += n_div;
13675 o_ls += n_div;
13676 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13677 bmap = isl_basic_map_add_div_constraints(bmap, i);
13678 if (!bmap)
13679 goto error;
13682 isl_local_space_free(ls);
13683 return bmap;
13684 error:
13685 isl_local_space_free(ls);
13686 return isl_basic_map_free(bmap);
13689 /* How many stride constraints does "ma" enforce?
13690 * That is, how many of the affine expressions have a denominator
13691 * different from one?
13693 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13695 int i;
13696 int strides = 0;
13698 for (i = 0; i < ma->n; ++i)
13699 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13700 strides++;
13702 return strides;
13705 /* For each affine expression in ma of the form
13707 * x_i = (f_i y + h_i)/m_i
13709 * with m_i different from one, add a constraint to "bmap"
13710 * of the form
13712 * f_i y + h_i = m_i alpha_i
13714 * with alpha_i an additional existentially quantified variable.
13716 * The input variables of "ma" correspond to a subset of the variables
13717 * of "bmap". There are "n_before" variables in "bmap" before this
13718 * subset and "n_after" variables after this subset.
13719 * The integer divisions of the affine expressions in "ma" are assumed
13720 * to have been aligned. There are "n_div_ma" of them and
13721 * they appear first in "bmap", straight after the "n_after" variables.
13723 static __isl_give isl_basic_map *add_ma_strides(
13724 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13725 int n_before, int n_after, int n_div_ma)
13727 int i, k;
13728 int div;
13729 isl_size total;
13730 isl_size n_param;
13731 isl_size n_in;
13733 total = isl_basic_map_dim(bmap, isl_dim_all);
13734 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13735 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13736 if (total < 0 || n_param < 0 || n_in < 0)
13737 return isl_basic_map_free(bmap);
13738 for (i = 0; i < ma->n; ++i) {
13739 int o_bmap = 0, o_ma = 1;
13741 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13742 continue;
13743 div = isl_basic_map_alloc_div(bmap);
13744 k = isl_basic_map_alloc_equality(bmap);
13745 if (div < 0 || k < 0)
13746 goto error;
13747 isl_int_set_si(bmap->div[div][0], 0);
13748 isl_seq_cpy(bmap->eq[k] + o_bmap,
13749 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13750 o_bmap += 1 + n_param;
13751 o_ma += 1 + n_param;
13752 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13753 o_bmap += n_before;
13754 isl_seq_cpy(bmap->eq[k] + o_bmap,
13755 ma->u.p[i]->v->el + o_ma, n_in);
13756 o_bmap += n_in;
13757 o_ma += n_in;
13758 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13759 o_bmap += n_after;
13760 isl_seq_cpy(bmap->eq[k] + o_bmap,
13761 ma->u.p[i]->v->el + o_ma, n_div_ma);
13762 o_bmap += n_div_ma;
13763 o_ma += n_div_ma;
13764 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13765 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13766 total++;
13769 return bmap;
13770 error:
13771 isl_basic_map_free(bmap);
13772 return NULL;
13775 /* Replace the domain or range space (depending on "type) of "space" by "set".
13777 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13778 enum isl_dim_type type, __isl_take isl_space *set)
13780 if (type == isl_dim_in) {
13781 space = isl_space_range(space);
13782 space = isl_space_map_from_domain_and_range(set, space);
13783 } else {
13784 space = isl_space_domain(space);
13785 space = isl_space_map_from_domain_and_range(space, set);
13788 return space;
13791 /* Compute the preimage of the domain or range (depending on "type")
13792 * of "bmap" under the function represented by "ma".
13793 * In other words, plug in "ma" in the domain or range of "bmap".
13794 * The result is a basic map that lives in the same space as "bmap"
13795 * except that the domain or range has been replaced by
13796 * the domain space of "ma".
13798 * If bmap is represented by
13800 * A(p) + S u + B x + T v + C(divs) >= 0,
13802 * where u and x are input and output dimensions if type == isl_dim_out
13803 * while x and v are input and output dimensions if type == isl_dim_in,
13804 * and ma is represented by
13806 * x = D(p) + F(y) + G(divs')
13808 * then the result is
13810 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13812 * The divs in the input set are similarly adjusted.
13813 * In particular
13815 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13817 * becomes
13819 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13820 * B_i G(divs') + c_i(divs))/n_i)
13822 * If bmap is not a rational map and if F(y) involves any denominators
13824 * x_i = (f_i y + h_i)/m_i
13826 * then additional constraints are added to ensure that we only
13827 * map back integer points. That is we enforce
13829 * f_i y + h_i = m_i alpha_i
13831 * with alpha_i an additional existentially quantified variable.
13833 * We first copy over the divs from "ma".
13834 * Then we add the modified constraints and divs from "bmap".
13835 * Finally, we add the stride constraints, if needed.
13837 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13838 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13839 __isl_take isl_multi_aff *ma)
13841 int i, k;
13842 isl_space *space;
13843 isl_basic_map *res = NULL;
13844 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13845 isl_int f, c1, c2, g;
13846 isl_bool rational;
13847 int strides;
13849 isl_int_init(f);
13850 isl_int_init(c1);
13851 isl_int_init(c2);
13852 isl_int_init(g);
13854 ma = isl_multi_aff_align_divs(ma);
13855 if (!bmap || !ma)
13856 goto error;
13857 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13858 goto error;
13860 if (type == isl_dim_in) {
13861 n_before = 0;
13862 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13863 } else {
13864 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13865 n_after = 0;
13867 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13868 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13869 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13870 goto error;
13872 space = isl_multi_aff_get_domain_space(ma);
13873 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13874 rational = isl_basic_map_is_rational(bmap);
13875 strides = rational ? 0 : multi_aff_strides(ma);
13876 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13877 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13878 if (rational)
13879 res = isl_basic_map_set_rational(res);
13881 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13882 if (isl_basic_map_alloc_div(res) < 0)
13883 goto error;
13885 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13886 if (!res)
13887 goto error;
13889 for (i = 0; i < bmap->n_eq; ++i) {
13890 k = isl_basic_map_alloc_equality(res);
13891 if (k < 0)
13892 goto error;
13893 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13894 n_after, n_div_ma, n_div_bmap,
13895 f, c1, c2, g, 0) < 0)
13896 goto error;
13899 for (i = 0; i < bmap->n_ineq; ++i) {
13900 k = isl_basic_map_alloc_inequality(res);
13901 if (k < 0)
13902 goto error;
13903 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13904 n_after, n_div_ma, n_div_bmap,
13905 f, c1, c2, g, 0) < 0)
13906 goto error;
13909 for (i = 0; i < bmap->n_div; ++i) {
13910 if (isl_int_is_zero(bmap->div[i][0])) {
13911 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13912 continue;
13914 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13915 n_before, n_after, n_div_ma, n_div_bmap,
13916 f, c1, c2, g, 1) < 0)
13917 goto error;
13920 if (strides)
13921 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13923 isl_int_clear(f);
13924 isl_int_clear(c1);
13925 isl_int_clear(c2);
13926 isl_int_clear(g);
13927 isl_basic_map_free(bmap);
13928 isl_multi_aff_free(ma);
13929 res = isl_basic_map_simplify(res);
13930 return isl_basic_map_finalize(res);
13931 error:
13932 isl_int_clear(f);
13933 isl_int_clear(c1);
13934 isl_int_clear(c2);
13935 isl_int_clear(g);
13936 isl_basic_map_free(bmap);
13937 isl_multi_aff_free(ma);
13938 isl_basic_map_free(res);
13939 return NULL;
13942 /* Compute the preimage of "bset" under the function represented by "ma".
13943 * In other words, plug in "ma" in "bset". The result is a basic set
13944 * that lives in the domain space of "ma".
13946 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13947 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13949 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13952 /* Compute the preimage of the domain of "bmap" under the function
13953 * represented by "ma".
13954 * In other words, plug in "ma" in the domain of "bmap".
13955 * The result is a basic map that lives in the same space as "bmap"
13956 * except that the domain has been replaced by the domain space of "ma".
13958 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13959 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13961 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13964 /* Compute the preimage of the range of "bmap" under the function
13965 * represented by "ma".
13966 * In other words, plug in "ma" in the range of "bmap".
13967 * The result is a basic map that lives in the same space as "bmap"
13968 * except that the range has been replaced by the domain space of "ma".
13970 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13971 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13973 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13976 /* Check if the range of "ma" is compatible with the domain or range
13977 * (depending on "type") of "map".
13978 * Return isl_stat_error if anything is wrong.
13980 static isl_stat check_map_compatible_range_multi_aff(
13981 __isl_keep isl_map *map, enum isl_dim_type type,
13982 __isl_keep isl_multi_aff *ma)
13984 isl_bool m;
13985 isl_space *ma_space;
13987 ma_space = isl_multi_aff_get_space(ma);
13988 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13989 isl_space_free(ma_space);
13990 if (m < 0)
13991 return isl_stat_error;
13992 if (!m)
13993 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13994 "spaces don't match", return isl_stat_error);
13995 return isl_stat_ok;
13998 /* Compute the preimage of the domain or range (depending on "type")
13999 * of "map" under the function represented by "ma".
14000 * In other words, plug in "ma" in the domain or range of "map".
14001 * The result is a map that lives in the same space as "map"
14002 * except that the domain or range has been replaced by
14003 * the domain space of "ma".
14005 * The parameters are assumed to have been aligned.
14007 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14008 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14010 int i;
14011 isl_space *space;
14013 map = isl_map_cow(map);
14014 ma = isl_multi_aff_align_divs(ma);
14015 if (!map || !ma)
14016 goto error;
14017 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14018 goto error;
14020 for (i = 0; i < map->n; ++i) {
14021 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14022 isl_multi_aff_copy(ma));
14023 if (!map->p[i])
14024 goto error;
14027 space = isl_multi_aff_get_domain_space(ma);
14028 space = isl_space_set(isl_map_get_space(map), type, space);
14030 isl_space_free(isl_map_take_space(map));
14031 map = isl_map_restore_space(map, space);
14032 if (!map)
14033 goto error;
14035 isl_multi_aff_free(ma);
14036 if (map->n > 1)
14037 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14038 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14039 return map;
14040 error:
14041 isl_multi_aff_free(ma);
14042 isl_map_free(map);
14043 return NULL;
14046 /* Compute the preimage of the domain or range (depending on "type")
14047 * of "map" under the function represented by "ma".
14048 * In other words, plug in "ma" in the domain or range of "map".
14049 * The result is a map that lives in the same space as "map"
14050 * except that the domain or range has been replaced by
14051 * the domain space of "ma".
14053 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14054 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14056 isl_bool aligned;
14058 if (!map || !ma)
14059 goto error;
14061 aligned = isl_map_space_has_equal_params(map, ma->space);
14062 if (aligned < 0)
14063 goto error;
14064 if (aligned)
14065 return map_preimage_multi_aff(map, type, ma);
14067 if (isl_map_check_named_params(map) < 0)
14068 goto error;
14069 if (!isl_space_has_named_params(ma->space))
14070 isl_die(map->ctx, isl_error_invalid,
14071 "unaligned unnamed parameters", goto error);
14072 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14073 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14075 return map_preimage_multi_aff(map, type, ma);
14076 error:
14077 isl_multi_aff_free(ma);
14078 return isl_map_free(map);
14081 /* Compute the preimage of "set" under the function represented by "ma".
14082 * In other words, plug in "ma" in "set". The result is a set
14083 * that lives in the domain space of "ma".
14085 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14086 __isl_take isl_multi_aff *ma)
14088 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14091 /* Compute the preimage of the domain of "map" under the function
14092 * represented by "ma".
14093 * In other words, plug in "ma" in the domain of "map".
14094 * The result is a map that lives in the same space as "map"
14095 * except that the domain has been replaced by the domain space of "ma".
14097 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14098 __isl_take isl_multi_aff *ma)
14100 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14103 /* Compute the preimage of the range of "map" under the function
14104 * represented by "ma".
14105 * In other words, plug in "ma" in the range of "map".
14106 * The result is a map that lives in the same space as "map"
14107 * except that the range has been replaced by the domain space of "ma".
14109 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14110 __isl_take isl_multi_aff *ma)
14112 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14115 /* Compute the preimage of "map" under the function represented by "pma".
14116 * In other words, plug in "pma" in the domain or range of "map".
14117 * The result is a map that lives in the same space as "map",
14118 * except that the space of type "type" has been replaced by
14119 * the domain space of "pma".
14121 * The parameters of "map" and "pma" are assumed to have been aligned.
14123 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14124 __isl_take isl_map *map, enum isl_dim_type type,
14125 __isl_take isl_pw_multi_aff *pma)
14127 int i;
14128 isl_map *res;
14130 if (!pma)
14131 goto error;
14133 if (pma->n == 0) {
14134 isl_space *space;
14136 space = isl_pw_multi_aff_get_domain_space(pma);
14137 isl_pw_multi_aff_free(pma);
14138 space = isl_space_set(isl_map_get_space(map), type, space);
14139 isl_map_free(map);
14140 return isl_map_empty(space);
14143 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14144 isl_multi_aff_copy(pma->p[0].maff));
14145 if (type == isl_dim_in)
14146 res = isl_map_intersect_domain(res,
14147 isl_map_copy(pma->p[0].set));
14148 else
14149 res = isl_map_intersect_range(res,
14150 isl_map_copy(pma->p[0].set));
14152 for (i = 1; i < pma->n; ++i) {
14153 isl_map *res_i;
14155 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14156 isl_multi_aff_copy(pma->p[i].maff));
14157 if (type == isl_dim_in)
14158 res_i = isl_map_intersect_domain(res_i,
14159 isl_map_copy(pma->p[i].set));
14160 else
14161 res_i = isl_map_intersect_range(res_i,
14162 isl_map_copy(pma->p[i].set));
14163 res = isl_map_union(res, res_i);
14166 isl_pw_multi_aff_free(pma);
14167 isl_map_free(map);
14168 return res;
14169 error:
14170 isl_pw_multi_aff_free(pma);
14171 isl_map_free(map);
14172 return NULL;
14175 /* Compute the preimage of "map" under the function represented by "pma".
14176 * In other words, plug in "pma" in the domain or range of "map".
14177 * The result is a map that lives in the same space as "map",
14178 * except that the space of type "type" has been replaced by
14179 * the domain space of "pma".
14181 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14182 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14184 isl_bool aligned;
14186 if (!map || !pma)
14187 goto error;
14189 aligned = isl_map_space_has_equal_params(map, pma->dim);
14190 if (aligned < 0)
14191 goto error;
14192 if (aligned)
14193 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14195 if (isl_map_check_named_params(map) < 0)
14196 goto error;
14197 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14198 goto error;
14199 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14200 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14202 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14203 error:
14204 isl_pw_multi_aff_free(pma);
14205 return isl_map_free(map);
14208 /* Compute the preimage of "set" under the function represented by "pma".
14209 * In other words, plug in "pma" in "set". The result is a set
14210 * that lives in the domain space of "pma".
14212 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14213 __isl_take isl_pw_multi_aff *pma)
14215 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14218 /* Compute the preimage of the domain of "map" under the function
14219 * represented by "pma".
14220 * In other words, plug in "pma" in the domain of "map".
14221 * The result is a map that lives in the same space as "map",
14222 * except that domain space has been replaced by the domain space of "pma".
14224 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14225 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14227 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14230 /* Compute the preimage of the range of "map" under the function
14231 * represented by "pma".
14232 * In other words, plug in "pma" in the range of "map".
14233 * The result is a map that lives in the same space as "map",
14234 * except that range space has been replaced by the domain space of "pma".
14236 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14237 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14239 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14242 /* Compute the preimage of "map" under the function represented by "mpa".
14243 * In other words, plug in "mpa" in the domain or range of "map".
14244 * The result is a map that lives in the same space as "map",
14245 * except that the space of type "type" has been replaced by
14246 * the domain space of "mpa".
14248 * If the map does not involve any constraints that refer to the
14249 * dimensions of the substituted space, then the only possible
14250 * effect of "mpa" on the map is to map the space to a different space.
14251 * We create a separate isl_multi_aff to effectuate this change
14252 * in order to avoid spurious splitting of the map along the pieces
14253 * of "mpa".
14254 * If "mpa" has a non-trivial explicit domain, however,
14255 * then the full substitution should be performed.
14257 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14258 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14260 isl_size n;
14261 isl_bool full;
14262 isl_pw_multi_aff *pma;
14264 n = isl_map_dim(map, type);
14265 if (n < 0 || !mpa)
14266 goto error;
14268 full = isl_map_involves_dims(map, type, 0, n);
14269 if (full >= 0 && !full)
14270 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14271 if (full < 0)
14272 goto error;
14273 if (!full) {
14274 isl_space *space;
14275 isl_multi_aff *ma;
14277 space = isl_multi_pw_aff_get_space(mpa);
14278 isl_multi_pw_aff_free(mpa);
14279 ma = isl_multi_aff_zero(space);
14280 return isl_map_preimage_multi_aff(map, type, ma);
14283 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14284 return isl_map_preimage_pw_multi_aff(map, type, pma);
14285 error:
14286 isl_map_free(map);
14287 isl_multi_pw_aff_free(mpa);
14288 return NULL;
14291 /* Compute the preimage of "map" under the function represented by "mpa".
14292 * In other words, plug in "mpa" in the domain "map".
14293 * The result is a map that lives in the same space as "map",
14294 * except that domain space has been replaced by the domain space of "mpa".
14296 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14297 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14299 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14302 /* Compute the preimage of "set" by the function represented by "mpa".
14303 * In other words, plug in "mpa" in "set".
14305 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14306 __isl_take isl_multi_pw_aff *mpa)
14308 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14311 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14312 * on the output dimension "pos" in terms of the parameters,
14313 * the input dimensions and possibly some integer divisions,
14314 * but not any other output dimensions, extract this upper bound
14315 * as a function of all dimensions (with zero coefficients
14316 * for the output dimensions).
14318 * That is, the inequality is of the form
14320 * e(...) + c - m x >= 0
14322 * where e does not depend on any other output dimensions.
14323 * Return (e(...) + c) / m, with the denominator m in the first position.
14325 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14326 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14328 isl_ctx *ctx;
14329 isl_vec *v;
14330 isl_size v_out, total;
14332 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14333 total = isl_basic_map_dim(bmap, isl_dim_all);
14334 if (v_out < 0 || total < 0)
14335 return NULL;
14336 ctx = isl_basic_map_get_ctx(bmap);
14337 v = isl_vec_alloc(ctx, 1 + 1 + total);
14338 if (!v)
14339 return NULL;
14340 isl_int_neg(v->el[0], bmap->ineq[ineq][1 + v_out + pos]);
14341 isl_seq_cpy(v->el + 1, bmap->ineq[ineq], 1 + total);
14342 isl_int_set_si(v->el[1 + 1 + v_out + pos], 0);
14344 return v;
14347 /* Is constraint "c" of the form
14349 * e(...) + c1 - m x >= 0
14351 * or
14353 * -e(...) + c2 + m x >= 0
14355 * where m > 1 and e only depends on parameters and input dimensions?
14357 * "offset" is the offset of the output dimensions
14358 * "pos" is the position of output dimension x.
14360 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
14362 if (isl_int_is_zero(c[offset + d]))
14363 return 0;
14364 if (isl_int_is_one(c[offset + d]))
14365 return 0;
14366 if (isl_int_is_negone(c[offset + d]))
14367 return 0;
14368 if (isl_seq_first_non_zero(c + offset, d) != -1)
14369 return 0;
14370 if (isl_seq_first_non_zero(c + offset + d + 1,
14371 total - (offset + d + 1)) != -1)
14372 return 0;
14373 return 1;
14376 /* Look for a pair of constraints
14378 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14380 * and
14382 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14384 * that express that the output dimension x at position "pos"
14385 * is some integer division of an expression in terms of the parameters and
14386 * input dimension.
14387 * If such a pair can be found, then return the index
14388 * of the upper bound constraint, m x <= e(...) + c1.
14389 * Otherwise, return an index beyond the number of constraints.
14391 * In order for the constraints above to express an integer division,
14392 * m needs to be greater than 1 and such that
14394 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14396 * In particular, this ensures that
14398 * x = floor((e(...) + c1) / m)
14400 isl_size isl_basic_map_find_output_upper_div_constraint(
14401 __isl_keep isl_basic_map *bmap, int pos)
14403 int i, j;
14404 isl_size n_ineq;
14405 isl_size v_out;
14406 isl_size total;
14407 isl_int sum;
14409 total = isl_basic_map_dim(bmap, isl_dim_all);
14410 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14411 n_ineq = isl_basic_map_n_inequality(bmap);
14412 if (total < 0 || v_out < 0 || n_ineq < 0)
14413 return isl_size_error;
14415 isl_int_init(sum);
14416 for (i = 0; i < n_ineq; ++i) {
14417 if (!is_potential_div_constraint(bmap->ineq[i],
14418 1 + v_out, pos, 1 + total))
14419 continue;
14420 for (j = i + 1; j < n_ineq; ++j) {
14421 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
14422 bmap->ineq[j] + 1, total))
14423 continue;
14424 isl_int_add(sum, bmap->ineq[i][0], bmap->ineq[j][0]);
14425 if (isl_int_abs_lt(sum, bmap->ineq[i][1 + v_out + pos]))
14426 break;
14428 if (j < n_ineq)
14429 break;
14431 isl_int_clear(sum);
14433 if (i >= n_ineq)
14434 return n_ineq;
14435 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
14436 return i;
14437 else
14438 return j;
14441 /* Return a copy of the equality constraints of "bset" as a matrix.
14443 __isl_give isl_mat *isl_basic_set_extract_equalities(
14444 __isl_keep isl_basic_set *bset)
14446 isl_ctx *ctx;
14447 isl_size total;
14449 total = isl_basic_set_dim(bset, isl_dim_all);
14450 if (total < 0)
14451 return NULL;
14453 ctx = isl_basic_set_get_ctx(bset);
14454 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14457 /* Are the "n" "coefficients" starting at "first" of the integer division
14458 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14459 * to each other?
14460 * The "coefficient" at position 0 is the denominator.
14461 * The "coefficient" at position 1 is the constant term.
14463 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14464 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14465 unsigned first, unsigned n)
14467 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14468 return isl_bool_error;
14469 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14470 return isl_bool_error;
14471 return isl_seq_eq(bmap1->div[pos1] + first,
14472 bmap2->div[pos2] + first, n);
14475 /* Are the integer division expressions at position "pos1" in "bmap1" and
14476 * "pos2" in "bmap2" equal to each other, except that the constant terms
14477 * are different?
14479 isl_bool isl_basic_map_equal_div_expr_except_constant(
14480 __isl_keep isl_basic_map *bmap1, int pos1,
14481 __isl_keep isl_basic_map *bmap2, int pos2)
14483 isl_bool equal;
14484 isl_size total, total2;
14486 total = isl_basic_map_dim(bmap1, isl_dim_all);
14487 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14488 if (total < 0 || total2 < 0)
14489 return isl_bool_error;
14490 if (total != total2)
14491 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14492 "incomparable div expressions", return isl_bool_error);
14493 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14494 0, 1);
14495 if (equal < 0 || !equal)
14496 return equal;
14497 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14498 1, 1);
14499 if (equal < 0 || equal)
14500 return isl_bool_not(equal);
14501 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14502 2, total);
14505 /* Replace the numerator of the constant term of the integer division
14506 * expression at position "div" in "bmap" by "value".
14507 * The caller guarantees that this does not change the meaning
14508 * of the input.
14510 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14511 __isl_take isl_basic_map *bmap, int div, int value)
14513 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14514 return isl_basic_map_free(bmap);
14516 isl_int_set_si(bmap->div[div][1], value);
14518 return bmap;
14521 /* Is the point "inner" internal to inequality constraint "ineq"
14522 * of "bset"?
14523 * The point is considered to be internal to the inequality constraint,
14524 * if it strictly lies on the positive side of the inequality constraint,
14525 * or if it lies on the constraint and the constraint is lexico-positive.
14527 static isl_bool is_internal(__isl_keep isl_vec *inner,
14528 __isl_keep isl_basic_set *bset, int ineq)
14530 isl_ctx *ctx;
14531 int pos;
14532 isl_size total;
14534 if (!inner || !bset)
14535 return isl_bool_error;
14537 ctx = isl_basic_set_get_ctx(bset);
14538 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14539 &ctx->normalize_gcd);
14540 if (!isl_int_is_zero(ctx->normalize_gcd))
14541 return isl_int_is_nonneg(ctx->normalize_gcd);
14543 total = isl_basic_set_dim(bset, isl_dim_all);
14544 if (total < 0)
14545 return isl_bool_error;
14546 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14547 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14550 /* Tighten the inequality constraints of "bset" that are outward with respect
14551 * to the point "vec".
14552 * That is, tighten the constraints that are not satisfied by "vec".
14554 * "vec" is a point internal to some superset S of "bset" that is used
14555 * to make the subsets of S disjoint, by tightening one half of the constraints
14556 * that separate two subsets. In particular, the constraints of S
14557 * are all satisfied by "vec" and should not be tightened.
14558 * Of the internal constraints, those that have "vec" on the outside
14559 * are tightened. The shared facet is included in the adjacent subset
14560 * with the opposite constraint.
14561 * For constraints that saturate "vec", this criterion cannot be used
14562 * to determine which of the two sides should be tightened.
14563 * Instead, the sign of the first non-zero coefficient is used
14564 * to make this choice. Note that this second criterion is never used
14565 * on the constraints of S since "vec" is interior to "S".
14567 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14568 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14570 int j;
14572 bset = isl_basic_set_cow(bset);
14573 if (!bset)
14574 return NULL;
14575 for (j = 0; j < bset->n_ineq; ++j) {
14576 isl_bool internal;
14578 internal = is_internal(vec, bset, j);
14579 if (internal < 0)
14580 return isl_basic_set_free(bset);
14581 if (internal)
14582 continue;
14583 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14586 return bset;
14589 /* Replace the variables x of type "type" starting at "first" in "bmap"
14590 * by x' with x = M x' with M the matrix trans.
14591 * That is, replace the corresponding coefficients c by c M.
14593 * The transformation matrix should be a square matrix.
14595 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14596 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14597 __isl_take isl_mat *trans)
14599 unsigned pos;
14601 bmap = isl_basic_map_cow(bmap);
14602 if (!bmap || !trans)
14603 goto error;
14605 if (trans->n_row != trans->n_col)
14606 isl_die(trans->ctx, isl_error_invalid,
14607 "expecting square transformation matrix", goto error);
14608 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14609 goto error;
14611 pos = isl_basic_map_offset(bmap, type) + first;
14613 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14614 isl_mat_copy(trans)) < 0)
14615 goto error;
14616 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14617 isl_mat_copy(trans)) < 0)
14618 goto error;
14619 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14620 isl_mat_copy(trans)) < 0)
14621 goto error;
14623 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14624 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14626 isl_mat_free(trans);
14627 return bmap;
14628 error:
14629 isl_mat_free(trans);
14630 isl_basic_map_free(bmap);
14631 return NULL;
14634 /* Replace the variables x of type "type" starting at "first" in "bset"
14635 * by x' with x = M x' with M the matrix trans.
14636 * That is, replace the corresponding coefficients c by c M.
14638 * The transformation matrix should be a square matrix.
14640 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14641 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14642 __isl_take isl_mat *trans)
14644 return isl_basic_map_transform_dims(bset, type, first, trans);