isl_stream_read_multi_aff: extract out read_universe_params
[isl.git] / isl_map.c
blob349455ee8a57f7f3b08eccb3d3dcdd9848b176b1
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
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
24 #include <string.h>
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
27 #include <isl_blk.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl/map.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
38 #include <isl_sort.h>
39 #include "isl_tab.h"
40 #include <isl/vec.h>
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
60 static __isl_keep const isl_basic_map *const_bset_to_bmap(
61 __isl_keep const isl_basic_set *bset)
63 return (const isl_basic_map *) bset;
66 #undef TYPE
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
70 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
72 switch (type) {
73 case isl_dim_param: return 1;
74 case isl_dim_in: return 1 + space->nparam;
75 case isl_dim_out: return 1 + space->nparam + space->n_in;
76 default: return 0;
80 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
81 enum isl_dim_type type)
83 if (!bmap)
84 return isl_size_error;
85 switch (type) {
86 case isl_dim_cst: return 1;
87 case isl_dim_param:
88 case isl_dim_in:
89 case isl_dim_out: return isl_space_dim(bmap->dim, type);
90 case isl_dim_div: return bmap->n_div;
91 case isl_dim_all: return isl_basic_map_total_dim(bmap);
92 default: return 0;
96 /* Return the space of "map".
98 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
100 return map ? map->dim : NULL;
103 /* Return the space of "set".
105 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
107 return isl_map_peek_space(set_to_map(set));
110 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
112 return isl_space_dim(isl_map_peek_space(map), type);
115 /* Return the dimensionality of the domain (tuple) of the map.
117 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
119 return isl_map_dim(map, isl_dim_in);
122 /* Return the dimensionality of the range (tuple) of the map.
124 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
126 return isl_map_dim(map, isl_dim_out);
129 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
131 return isl_map_dim(set_to_map(set), type);
134 /* Return the dimensionality of the (tuple of the) set.
136 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
138 return isl_set_dim(set, isl_dim_set);
141 /* Return the position of the variables of the given type
142 * within the sequence of variables of "bmap".
144 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
145 enum isl_dim_type type)
147 isl_space *space;
149 space = isl_basic_map_peek_space(bmap);
150 if (!space)
151 return isl_size_error;
153 switch (type) {
154 case isl_dim_param:
155 case isl_dim_in:
156 case isl_dim_out: return isl_space_offset(space, type);
157 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
158 case isl_dim_cst:
159 default:
160 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
161 "invalid dimension type", return isl_size_error);
165 /* Return the position of the variables of the given type
166 * within the sequence of variables of "bset".
168 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
169 enum isl_dim_type type)
171 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
174 /* Return the position of the coefficients of the variables of the given type
175 * within the sequence of coefficients of "bmap".
177 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
178 enum isl_dim_type type)
180 switch (type) {
181 case isl_dim_cst: return 0;
182 case isl_dim_param:
183 case isl_dim_in:
184 case isl_dim_out:
185 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
186 default: return 0;
190 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
191 enum isl_dim_type type)
193 return isl_basic_map_offset(bset, type);
196 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
198 return pos(map->dim, type);
201 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
202 enum isl_dim_type type)
204 return isl_basic_map_dim(bset, type);
207 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
209 return isl_basic_set_dim(bset, isl_dim_set);
212 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
214 return isl_basic_set_dim(bset, isl_dim_param);
217 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
219 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
222 isl_size isl_set_n_dim(__isl_keep isl_set *set)
224 return isl_set_dim(set, isl_dim_set);
227 isl_size isl_set_n_param(__isl_keep isl_set *set)
229 return isl_set_dim(set, isl_dim_param);
232 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
234 isl_size dim;
236 if (!bmap)
237 return isl_size_error;
238 dim = isl_space_dim(bmap->dim, isl_dim_all);
239 if (dim < 0)
240 return isl_size_error;
241 return dim + bmap->n_div;
244 /* Return the number of equality constraints in the description of "bmap".
245 * Return isl_size_error on error.
247 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
249 if (!bmap)
250 return isl_size_error;
251 return bmap->n_eq;
254 /* Return the number of equality constraints in the description of "bset".
255 * Return isl_size_error on error.
257 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
259 return isl_basic_map_n_equality(bset_to_bmap(bset));
262 /* Return the number of inequality constraints in the description of "bmap".
263 * Return isl_size_error on error.
265 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
267 if (!bmap)
268 return isl_size_error;
269 return bmap->n_ineq;
272 /* Return the number of inequality constraints in the description of "bset".
273 * Return isl_size_error on error.
275 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
277 return isl_basic_map_n_inequality(bset_to_bmap(bset));
280 /* Do "bmap1" and "bmap2" have the same parameters?
282 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
283 __isl_keep isl_basic_map *bmap2)
285 isl_space *space1, *space2;
287 space1 = isl_basic_map_peek_space(bmap1);
288 space2 = isl_basic_map_peek_space(bmap2);
289 return isl_space_has_equal_params(space1, space2);
292 /* Do "map1" and "map2" have the same parameters?
294 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
295 __isl_keep isl_map *map2)
297 isl_space *space1, *space2;
299 space1 = isl_map_peek_space(map1);
300 space2 = isl_map_peek_space(map2);
301 return isl_space_has_equal_params(space1, space2);
304 /* Do "map" and "set" have the same parameters?
306 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
307 __isl_keep isl_set *set)
309 return isl_map_has_equal_params(map, set_to_map(set));
312 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
314 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
315 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
317 isl_space *bmap_space, *bset_space;
319 bmap_space = isl_basic_map_peek_space(bmap);
320 bset_space = isl_basic_set_peek_space(bset);
321 return isl_space_tuple_is_equal(bmap_space, type,
322 bset_space, isl_dim_set);
325 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
327 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
328 enum isl_dim_type type, __isl_keep isl_set *set)
330 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
333 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
334 __isl_keep isl_set *set)
336 isl_bool m;
337 if (!map || !set)
338 return isl_bool_error;
339 m = isl_map_has_equal_params(map, set_to_map(set));
340 if (m < 0 || !m)
341 return m;
342 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
345 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
346 __isl_keep isl_basic_set *bset)
348 isl_bool m;
349 if (!bmap || !bset)
350 return isl_bool_error;
351 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
352 if (m < 0 || !m)
353 return m;
354 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
357 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
358 __isl_keep isl_set *set)
360 isl_bool m;
361 if (!map || !set)
362 return isl_bool_error;
363 m = isl_map_has_equal_params(map, set_to_map(set));
364 if (m < 0 || !m)
365 return m;
366 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
369 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
370 __isl_keep isl_basic_set *bset)
372 isl_bool m;
373 if (!bmap || !bset)
374 return isl_bool_error;
375 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
376 if (m < 0 || !m)
377 return m;
378 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
381 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
383 return bmap ? bmap->ctx : NULL;
386 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
388 return bset ? bset->ctx : NULL;
391 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
393 return map ? map->ctx : NULL;
396 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
398 return set ? set->ctx : NULL;
401 /* Return the space of "bmap".
403 __isl_keep isl_space *isl_basic_map_peek_space(
404 __isl_keep const isl_basic_map *bmap)
406 return bmap ? bmap->dim : NULL;
409 /* Return the space of "bset".
411 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
413 return isl_basic_map_peek_space(bset_to_bmap(bset));
416 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
418 return isl_space_copy(isl_basic_map_peek_space(bmap));
421 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
423 return isl_basic_map_get_space(bset_to_bmap(bset));
426 /* Return the space of "bmap".
427 * This may be either a copy or the space itself
428 * if there is only one reference to "bmap".
429 * This allows the space to be modified inplace
430 * if both the basic map and its space have only a single reference.
431 * The caller is not allowed to modify "bmap" between this call and
432 * a subsequent call to isl_basic_map_restore_space.
433 * The only exception is that isl_basic_map_free can be called instead.
435 static __isl_give isl_space *isl_basic_map_take_space(
436 __isl_keep isl_basic_map *bmap)
438 isl_space *space;
440 if (!bmap)
441 return NULL;
442 if (bmap->ref != 1)
443 return isl_basic_map_get_space(bmap);
444 space = bmap->dim;
445 bmap->dim = NULL;
446 return space;
449 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
450 * due to a preceding call to isl_basic_map_take_space.
451 * However, in this case, "bmap" only has a single reference and
452 * then the call to isl_basic_map_cow has no effect.
454 static __isl_give isl_basic_map *isl_basic_map_restore_space(
455 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
457 if (!bmap || !space)
458 goto error;
460 if (bmap->dim == space) {
461 isl_space_free(space);
462 return bmap;
465 bmap = isl_basic_map_cow(bmap);
466 if (!bmap)
467 goto error;
468 isl_space_free(bmap->dim);
469 bmap->dim = space;
471 return bmap;
472 error:
473 isl_basic_map_free(bmap);
474 isl_space_free(space);
475 return NULL;
478 /* Extract the divs in "bmap" as a matrix.
480 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
482 int i;
483 isl_ctx *ctx;
484 isl_mat *div;
485 isl_size v_div;
486 unsigned cols;
488 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
489 if (v_div < 0)
490 return NULL;
492 ctx = isl_basic_map_get_ctx(bmap);
493 cols = 1 + 1 + v_div + bmap->n_div;
494 div = isl_mat_alloc(ctx, bmap->n_div, cols);
495 if (!div)
496 return NULL;
498 for (i = 0; i < bmap->n_div; ++i)
499 isl_seq_cpy(div->row[i], bmap->div[i], cols);
501 return div;
504 /* Extract the divs in "bset" as a matrix.
506 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
508 return isl_basic_map_get_divs(bset);
511 __isl_give isl_local_space *isl_basic_map_get_local_space(
512 __isl_keep isl_basic_map *bmap)
514 isl_mat *div;
516 if (!bmap)
517 return NULL;
519 div = isl_basic_map_get_divs(bmap);
520 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
523 __isl_give isl_local_space *isl_basic_set_get_local_space(
524 __isl_keep isl_basic_set *bset)
526 return isl_basic_map_get_local_space(bset);
529 /* For each known div d = floor(f/m), add the constraints
531 * f - m d >= 0
532 * -(f-(m-1)) + m d >= 0
534 * Do not finalize the result.
536 static __isl_give isl_basic_map *add_known_div_constraints(
537 __isl_take isl_basic_map *bmap)
539 int i;
540 isl_size n_div;
542 n_div = isl_basic_map_dim(bmap, isl_dim_div);
543 if (n_div < 0)
544 return isl_basic_map_free(bmap);
545 if (n_div == 0)
546 return bmap;
547 bmap = isl_basic_map_cow(bmap);
548 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
549 if (!bmap)
550 return NULL;
551 for (i = 0; i < n_div; ++i) {
552 if (isl_int_is_zero(bmap->div[i][0]))
553 continue;
554 bmap = isl_basic_map_add_div_constraints(bmap, i);
557 return bmap;
560 __isl_give isl_basic_map *isl_basic_map_from_local_space(
561 __isl_take isl_local_space *ls)
563 int i;
564 isl_size n_div;
565 isl_basic_map *bmap;
567 n_div = isl_local_space_dim(ls, isl_dim_div);
568 if (n_div < 0)
569 ls = isl_local_space_free(ls);
570 if (!ls)
571 return NULL;
573 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
574 n_div, 0, 2 * n_div);
576 for (i = 0; i < n_div; ++i)
577 if (isl_basic_map_alloc_div(bmap) < 0)
578 goto error;
580 for (i = 0; i < n_div; ++i)
581 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
582 bmap = add_known_div_constraints(bmap);
584 isl_local_space_free(ls);
585 return bmap;
586 error:
587 isl_local_space_free(ls);
588 isl_basic_map_free(bmap);
589 return NULL;
592 __isl_give isl_basic_set *isl_basic_set_from_local_space(
593 __isl_take isl_local_space *ls)
595 return isl_basic_map_from_local_space(ls);
598 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
600 return isl_space_copy(isl_map_peek_space(map));
603 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
605 if (!set)
606 return NULL;
607 return isl_space_copy(set->dim);
610 /* Return the space of "map".
611 * This may be either a copy or the space itself
612 * if there is only one reference to "map".
613 * This allows the space to be modified inplace
614 * if both the map and its space have only a single reference.
615 * The caller is not allowed to modify "map" between this call and
616 * a subsequent call to isl_map_restore_space.
617 * The only exception is that isl_map_free can be called instead.
619 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
621 isl_space *space;
623 if (!map)
624 return NULL;
625 if (map->ref != 1)
626 return isl_map_get_space(map);
627 space = map->dim;
628 map->dim = NULL;
629 return space;
632 /* Set the space of "map" to "space", where the space of "map" may be missing
633 * due to a preceding call to isl_map_take_space.
634 * However, in this case, "map" only has a single reference and
635 * then the call to isl_map_cow has no effect.
637 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
638 __isl_take isl_space *space)
640 if (!map || !space)
641 goto error;
643 if (map->dim == space) {
644 isl_space_free(space);
645 return map;
648 map = isl_map_cow(map);
649 if (!map)
650 goto error;
651 isl_space_free(map->dim);
652 map->dim = space;
654 return map;
655 error:
656 isl_map_free(map);
657 isl_space_free(space);
658 return NULL;
661 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
662 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
664 isl_space *space;
666 space = isl_basic_map_take_space(bmap);
667 space = isl_space_set_tuple_name(space, type, s);
668 bmap = isl_basic_map_restore_space(bmap, space);
669 bmap = isl_basic_map_finalize(bmap);
670 return bmap;
673 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
674 __isl_take isl_basic_set *bset, const char *s)
676 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
679 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
680 enum isl_dim_type type)
682 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
685 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
686 enum isl_dim_type type, const char *s)
688 int i;
689 isl_space *space;
691 map = isl_map_cow(map);
692 if (!map)
693 return NULL;
695 for (i = 0; i < map->n; ++i) {
696 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
697 if (!map->p[i])
698 goto error;
701 space = isl_map_take_space(map);
702 space = isl_space_set_tuple_name(space, type, s);
703 map = isl_map_restore_space(map, space);
705 return map;
706 error:
707 isl_map_free(map);
708 return NULL;
711 /* Replace the identifier of the tuple of type "type" by "id".
713 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
714 __isl_take isl_basic_map *bmap,
715 enum isl_dim_type type, __isl_take isl_id *id)
717 isl_space *space;
719 space = isl_basic_map_take_space(bmap);
720 space = isl_space_set_tuple_id(space, type, id);
721 bmap = isl_basic_map_restore_space(bmap, space);
722 bmap = isl_basic_map_finalize(bmap);
723 return bmap;
726 /* Replace the identifier of the tuple by "id".
728 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
729 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
731 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
734 /* Does the input or output tuple have a name?
736 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
738 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
741 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
742 enum isl_dim_type type)
744 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
747 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
748 const char *s)
750 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
751 isl_dim_set, s));
754 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
755 enum isl_dim_type type, __isl_take isl_id *id)
757 isl_space *space;
759 space = isl_map_take_space(map);
760 space = isl_space_set_tuple_id(space, type, id);
761 map = isl_map_restore_space(map, space);
763 return isl_map_reset_space(map, isl_map_get_space(map));
766 /* Replace the identifier of the domain tuple of "map" by "id".
768 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
769 __isl_take isl_id *id)
771 return isl_map_set_tuple_id(map, isl_dim_in, id);
774 /* Replace the identifier of the range tuple of "map" by "id".
776 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
777 __isl_take isl_id *id)
779 return isl_map_set_tuple_id(map, isl_dim_out, id);
782 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
783 __isl_take isl_id *id)
785 return isl_map_set_tuple_id(set, isl_dim_set, id);
788 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
789 enum isl_dim_type type)
791 isl_space *space;
793 space = isl_map_take_space(map);
794 space = isl_space_reset_tuple_id(space, type);
795 map = isl_map_restore_space(map, space);
797 return isl_map_reset_space(map, isl_map_get_space(map));
800 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
802 return isl_map_reset_tuple_id(set, isl_dim_set);
805 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
807 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
810 /* Does the domain tuple of "map" have an identifier?
812 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
814 return isl_map_has_tuple_id(map, isl_dim_in);
817 /* Does the range tuple of "map" have an identifier?
819 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
821 return isl_map_has_tuple_id(map, isl_dim_out);
824 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
825 enum isl_dim_type type)
827 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
830 /* Return the identifier of the domain tuple of "map", assuming it has one.
832 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
834 return isl_map_get_tuple_id(map, isl_dim_in);
837 /* Return the identifier of the range tuple of "map", assuming it has one.
839 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
841 return isl_map_get_tuple_id(map, isl_dim_out);
844 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
846 return isl_map_has_tuple_id(set, isl_dim_set);
849 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
851 return isl_map_get_tuple_id(set, isl_dim_set);
854 /* Does the set tuple have a name?
856 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
858 if (!set)
859 return isl_bool_error;
860 return isl_space_has_tuple_name(set->dim, isl_dim_set);
864 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
866 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
869 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
871 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
874 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
875 enum isl_dim_type type, unsigned pos)
877 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
880 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
881 enum isl_dim_type type, unsigned pos)
883 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
886 /* Does the given dimension have a name?
888 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
889 enum isl_dim_type type, unsigned pos)
891 if (!map)
892 return isl_bool_error;
893 return isl_space_has_dim_name(map->dim, type, pos);
896 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
897 enum isl_dim_type type, unsigned pos)
899 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
902 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
903 enum isl_dim_type type, unsigned pos)
905 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
908 /* Does the given dimension have a name?
910 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
911 enum isl_dim_type type, unsigned pos)
913 if (!set)
914 return isl_bool_error;
915 return isl_space_has_dim_name(set->dim, type, pos);
918 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
919 __isl_take isl_basic_map *bmap,
920 enum isl_dim_type type, unsigned pos, const char *s)
922 isl_space *space;
924 space = isl_basic_map_take_space(bmap);
925 space = isl_space_set_dim_name(space, type, pos, s);
926 bmap = isl_basic_map_restore_space(bmap, space);
927 return isl_basic_map_finalize(bmap);
930 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
931 enum isl_dim_type type, unsigned pos, const char *s)
933 int i;
934 isl_space *space;
936 map = isl_map_cow(map);
937 if (!map)
938 return NULL;
940 for (i = 0; i < map->n; ++i) {
941 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
942 if (!map->p[i])
943 goto error;
946 space = isl_map_take_space(map);
947 space = isl_space_set_dim_name(space, type, pos, s);
948 map = isl_map_restore_space(map, space);
950 return map;
951 error:
952 isl_map_free(map);
953 return NULL;
956 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
957 __isl_take isl_basic_set *bset,
958 enum isl_dim_type type, unsigned pos, const char *s)
960 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
961 type, pos, s));
964 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
965 enum isl_dim_type type, unsigned pos, const char *s)
967 return set_from_map(isl_map_set_dim_name(set_to_map(set),
968 type, pos, s));
971 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
972 enum isl_dim_type type, unsigned pos)
974 if (!bmap)
975 return isl_bool_error;
976 return isl_space_has_dim_id(bmap->dim, type, pos);
979 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
980 enum isl_dim_type type, unsigned pos)
982 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
985 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
986 enum isl_dim_type type, unsigned pos)
988 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
991 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
992 enum isl_dim_type type, unsigned pos)
994 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
997 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
998 enum isl_dim_type type, unsigned pos)
1000 return isl_map_has_dim_id(set, type, pos);
1003 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1004 enum isl_dim_type type, unsigned pos)
1006 return isl_map_get_dim_id(set, type, pos);
1009 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1010 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1012 isl_space *space;
1014 space = isl_map_take_space(map);
1015 space = isl_space_set_dim_id(space, type, pos, id);
1016 map = isl_map_restore_space(map, space);
1018 return isl_map_reset_space(map, isl_map_get_space(map));
1021 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1022 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1024 return isl_map_set_dim_id(set, type, pos, id);
1027 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1028 __isl_keep isl_id *id)
1030 if (!map)
1031 return -1;
1032 return isl_space_find_dim_by_id(map->dim, type, id);
1035 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1036 __isl_keep isl_id *id)
1038 return isl_map_find_dim_by_id(set, type, id);
1041 /* Return the position of the dimension of the given type and name
1042 * in "bmap".
1043 * Return -1 if no such dimension can be found.
1045 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1046 enum isl_dim_type type, const char *name)
1048 if (!bmap)
1049 return -1;
1050 return isl_space_find_dim_by_name(bmap->dim, type, name);
1053 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1054 const char *name)
1056 if (!map)
1057 return -1;
1058 return isl_space_find_dim_by_name(map->dim, type, name);
1061 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1062 const char *name)
1064 return isl_map_find_dim_by_name(set, type, name);
1067 /* Check whether equality i of bset is a pure stride constraint
1068 * on a single dimension, i.e., of the form
1070 * v = k e
1072 * with k a constant and e an existentially quantified variable.
1074 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1076 isl_size nparam;
1077 isl_size d;
1078 isl_size n_div;
1079 int pos1;
1080 int pos2;
1082 nparam = isl_basic_set_dim(bset, isl_dim_param);
1083 d = isl_basic_set_dim(bset, isl_dim_set);
1084 n_div = isl_basic_set_dim(bset, isl_dim_div);
1085 if (nparam < 0 || d < 0 || n_div < 0)
1086 return isl_bool_error;
1088 if (!isl_int_is_zero(bset->eq[i][0]))
1089 return isl_bool_false;
1091 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1092 return isl_bool_false;
1093 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1094 if (pos1 == -1)
1095 return isl_bool_false;
1096 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1097 d - pos1 - 1) != -1)
1098 return isl_bool_false;
1100 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1101 if (pos2 == -1)
1102 return isl_bool_false;
1103 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1104 n_div - pos2 - 1) != -1)
1105 return isl_bool_false;
1106 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1107 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1108 return isl_bool_false;
1110 return isl_bool_true;
1113 /* Reset the user pointer on all identifiers of parameters and tuples
1114 * of the space of "map".
1116 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1118 isl_space *space;
1120 space = isl_map_get_space(map);
1121 space = isl_space_reset_user(space);
1122 map = isl_map_reset_space(map, space);
1124 return map;
1127 /* Reset the user pointer on all identifiers of parameters and tuples
1128 * of the space of "set".
1130 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1132 return isl_map_reset_user(set);
1135 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1137 if (!bmap)
1138 return isl_bool_error;
1139 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1142 /* Has "map" been marked as a rational map?
1143 * In particular, have all basic maps in "map" been marked this way?
1144 * An empty map is not considered to be rational.
1145 * Maps where only some of the basic maps are marked rational
1146 * are not allowed.
1148 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1150 int i;
1151 isl_bool rational;
1153 if (!map)
1154 return isl_bool_error;
1155 if (map->n == 0)
1156 return isl_bool_false;
1157 rational = isl_basic_map_is_rational(map->p[0]);
1158 if (rational < 0)
1159 return rational;
1160 for (i = 1; i < map->n; ++i) {
1161 isl_bool rational_i;
1163 rational_i = isl_basic_map_is_rational(map->p[i]);
1164 if (rational_i < 0)
1165 return rational_i;
1166 if (rational != rational_i)
1167 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1168 "mixed rational and integer basic maps "
1169 "not supported", return isl_bool_error);
1172 return rational;
1175 /* Has "set" been marked as a rational set?
1176 * In particular, have all basic set in "set" been marked this way?
1177 * An empty set is not considered to be rational.
1178 * Sets where only some of the basic sets are marked rational
1179 * are not allowed.
1181 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1183 return isl_map_is_rational(set);
1186 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1188 return isl_basic_map_is_rational(bset);
1191 /* Does "bmap" contain any rational points?
1193 * If "bmap" has an equality for each dimension, equating the dimension
1194 * to an integer constant, then it has no rational points, even if it
1195 * is marked as rational.
1197 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1199 isl_bool has_rational = isl_bool_true;
1200 isl_size total;
1202 if (!bmap)
1203 return isl_bool_error;
1204 if (isl_basic_map_plain_is_empty(bmap))
1205 return isl_bool_false;
1206 if (!isl_basic_map_is_rational(bmap))
1207 return isl_bool_false;
1208 bmap = isl_basic_map_copy(bmap);
1209 bmap = isl_basic_map_implicit_equalities(bmap);
1210 total = isl_basic_map_dim(bmap, isl_dim_all);
1211 if (total < 0)
1212 return isl_bool_error;
1213 if (bmap->n_eq == total) {
1214 int i, j;
1215 for (i = 0; i < bmap->n_eq; ++i) {
1216 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1217 if (j < 0)
1218 break;
1219 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1220 !isl_int_is_negone(bmap->eq[i][1 + j]))
1221 break;
1222 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1223 total - j - 1);
1224 if (j >= 0)
1225 break;
1227 if (i == bmap->n_eq)
1228 has_rational = isl_bool_false;
1230 isl_basic_map_free(bmap);
1232 return has_rational;
1235 /* Does "map" contain any rational points?
1237 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1239 int i;
1240 isl_bool has_rational;
1242 if (!map)
1243 return isl_bool_error;
1244 for (i = 0; i < map->n; ++i) {
1245 has_rational = isl_basic_map_has_rational(map->p[i]);
1246 if (has_rational < 0 || has_rational)
1247 return has_rational;
1249 return isl_bool_false;
1252 /* Does "set" contain any rational points?
1254 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1256 return isl_map_has_rational(set);
1259 /* Is this basic set a parameter domain?
1261 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1263 if (!bset)
1264 return isl_bool_error;
1265 return isl_space_is_params(bset->dim);
1268 /* Is this set a parameter domain?
1270 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1272 if (!set)
1273 return isl_bool_error;
1274 return isl_space_is_params(set->dim);
1277 /* Is this map actually a parameter domain?
1278 * Users should never call this function. Outside of isl,
1279 * a map can never be a parameter domain.
1281 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1283 if (!map)
1284 return isl_bool_error;
1285 return isl_space_is_params(map->dim);
1288 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1289 __isl_take isl_basic_map *bmap, unsigned extra,
1290 unsigned n_eq, unsigned n_ineq)
1292 int i;
1293 isl_space *space = isl_basic_map_peek_space(bmap);
1294 isl_size n_var = isl_space_dim(space, isl_dim_all);
1295 size_t row_size = 1 + n_var + extra;
1297 bmap->ctx = ctx;
1298 isl_ctx_ref(ctx);
1300 if (n_var < 0)
1301 return isl_basic_map_free(bmap);
1303 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1304 if (isl_blk_is_error(bmap->block))
1305 goto error;
1307 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1308 if ((n_ineq + n_eq) && !bmap->ineq)
1309 goto error;
1311 if (extra == 0) {
1312 bmap->block2 = isl_blk_empty();
1313 bmap->div = NULL;
1314 } else {
1315 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1316 if (isl_blk_is_error(bmap->block2))
1317 goto error;
1319 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1320 if (!bmap->div)
1321 goto error;
1324 for (i = 0; i < n_ineq + n_eq; ++i)
1325 bmap->ineq[i] = bmap->block.data + i * row_size;
1327 for (i = 0; i < extra; ++i)
1328 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1330 bmap->ref = 1;
1331 bmap->flags = 0;
1332 bmap->c_size = n_eq + n_ineq;
1333 bmap->eq = bmap->ineq + n_ineq;
1334 bmap->extra = extra;
1335 bmap->n_eq = 0;
1336 bmap->n_ineq = 0;
1337 bmap->n_div = 0;
1338 bmap->sample = NULL;
1340 return bmap;
1341 error:
1342 isl_basic_map_free(bmap);
1343 return NULL;
1346 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1347 unsigned nparam, unsigned dim, unsigned extra,
1348 unsigned n_eq, unsigned n_ineq)
1350 struct isl_basic_map *bmap;
1351 isl_space *space;
1353 space = isl_space_set_alloc(ctx, nparam, dim);
1354 if (!space)
1355 return NULL;
1357 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1358 return bset_from_bmap(bmap);
1361 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1362 unsigned extra, unsigned n_eq, unsigned n_ineq)
1364 struct isl_basic_map *bmap;
1365 if (!space)
1366 return NULL;
1367 isl_assert(space->ctx, space->n_in == 0, goto error);
1368 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1369 return bset_from_bmap(bmap);
1370 error:
1371 isl_space_free(space);
1372 return NULL;
1375 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1376 unsigned extra, unsigned n_eq, unsigned n_ineq)
1378 struct isl_basic_map *bmap;
1380 if (!space)
1381 return NULL;
1382 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1383 if (!bmap)
1384 goto error;
1385 bmap->dim = space;
1387 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1388 error:
1389 isl_space_free(space);
1390 return NULL;
1393 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1394 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1395 unsigned n_eq, unsigned n_ineq)
1397 struct isl_basic_map *bmap;
1398 isl_space *space;
1400 space = isl_space_alloc(ctx, nparam, in, out);
1401 if (!space)
1402 return NULL;
1404 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1405 return bmap;
1408 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1409 __isl_keep isl_basic_map *src)
1411 int i;
1412 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1414 if (!dst || total < 0)
1415 return isl_basic_map_free(dst);
1417 for (i = 0; i < src->n_eq; ++i) {
1418 int j = isl_basic_map_alloc_equality(dst);
1419 if (j < 0)
1420 return isl_basic_map_free(dst);
1421 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1424 for (i = 0; i < src->n_ineq; ++i) {
1425 int j = isl_basic_map_alloc_inequality(dst);
1426 if (j < 0)
1427 return isl_basic_map_free(dst);
1428 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1431 for (i = 0; i < src->n_div; ++i) {
1432 int j = isl_basic_map_alloc_div(dst);
1433 if (j < 0)
1434 return isl_basic_map_free(dst);
1435 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1437 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1438 return dst;
1441 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1443 struct isl_basic_map *dup;
1445 if (!bmap)
1446 return NULL;
1447 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1448 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1449 dup = dup_constraints(dup, bmap);
1450 if (!dup)
1451 return NULL;
1452 dup->flags = bmap->flags;
1453 dup->sample = isl_vec_copy(bmap->sample);
1454 return dup;
1457 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1459 struct isl_basic_map *dup;
1461 dup = isl_basic_map_dup(bset_to_bmap(bset));
1462 return bset_from_bmap(dup);
1465 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1467 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1470 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1472 if (!set)
1473 return NULL;
1475 set->ref++;
1476 return set;
1479 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1481 if (!bmap)
1482 return NULL;
1484 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1485 bmap->ref++;
1486 return bmap;
1488 bmap = isl_basic_map_dup(bmap);
1489 if (bmap)
1490 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1491 return bmap;
1494 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1496 if (!map)
1497 return NULL;
1499 map->ref++;
1500 return map;
1503 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1505 if (!bmap)
1506 return NULL;
1508 if (--bmap->ref > 0)
1509 return NULL;
1511 isl_ctx_deref(bmap->ctx);
1512 free(bmap->div);
1513 isl_blk_free(bmap->ctx, bmap->block2);
1514 free(bmap->ineq);
1515 isl_blk_free(bmap->ctx, bmap->block);
1516 isl_vec_free(bmap->sample);
1517 isl_space_free(bmap->dim);
1518 free(bmap);
1520 return NULL;
1523 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1525 return isl_basic_map_free(bset_to_bmap(bset));
1528 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1530 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1533 /* Check that "bset" does not involve any parameters.
1535 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1537 isl_size nparam;
1539 nparam = isl_basic_set_dim(bset, isl_dim_param);
1540 if (nparam < 0)
1541 return isl_stat_error;
1542 if (nparam != 0)
1543 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1544 "basic set should not have any parameters",
1545 return isl_stat_error);
1546 return isl_stat_ok;
1549 /* Check that "bset" does not involve any local variables.
1551 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1553 isl_size n_div;
1555 n_div = isl_basic_set_dim(bset, isl_dim_div);
1556 if (n_div < 0)
1557 return isl_stat_error;
1558 if (n_div != 0)
1559 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1560 "basic set should not have any local variables",
1561 return isl_stat_error);
1562 return isl_stat_ok;
1565 #undef TYPE
1566 #define TYPE isl_map
1568 #include "isl_check_named_params_templ.c"
1570 #undef TYPE
1571 #define TYPE isl_basic_map
1573 static
1574 #include "isl_check_named_params_templ.c"
1576 /* Check that "bmap1" and "bmap2" have the same parameters,
1577 * reporting an error if they do not.
1579 static isl_stat isl_basic_map_check_equal_params(
1580 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1582 isl_bool match;
1584 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1585 if (match < 0)
1586 return isl_stat_error;
1587 if (!match)
1588 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1589 "parameters don't match", return isl_stat_error);
1590 return isl_stat_ok;
1593 #undef TYPE
1594 #define TYPE isl_map
1596 #include "isl_align_params_bin_templ.c"
1598 #undef SUFFIX
1599 #define SUFFIX set
1600 #undef ARG1
1601 #define ARG1 isl_map
1602 #undef ARG2
1603 #define ARG2 isl_set
1605 #include "isl_align_params_templ.c"
1607 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1608 __isl_keep isl_map *map2,
1609 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1611 isl_bool r;
1613 if (!map1 || !map2)
1614 return isl_bool_error;
1615 if (isl_map_has_equal_params(map1, map2))
1616 return fn(map1, map2);
1617 if (isl_map_check_named_params(map1) < 0)
1618 return isl_bool_error;
1619 if (isl_map_check_named_params(map2) < 0)
1620 return isl_bool_error;
1621 map1 = isl_map_copy(map1);
1622 map2 = isl_map_copy(map2);
1623 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1624 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1625 r = fn(map1, map2);
1626 isl_map_free(map1);
1627 isl_map_free(map2);
1628 return r;
1631 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1633 isl_size total;
1634 struct isl_ctx *ctx;
1636 total = isl_basic_map_dim(bmap, isl_dim_all);
1637 if (total < 0)
1638 return -1;
1639 ctx = bmap->ctx;
1640 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1641 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1642 return -1);
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1646 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1647 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1648 isl_int *t;
1649 int j = isl_basic_map_alloc_inequality(bmap);
1650 if (j < 0)
1651 return -1;
1652 t = bmap->ineq[j];
1653 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1654 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1655 bmap->eq[-1] = t;
1656 bmap->n_eq++;
1657 bmap->n_ineq--;
1658 bmap->eq--;
1659 return 0;
1661 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1662 bmap->extra - bmap->n_div);
1663 return bmap->n_eq++;
1666 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1668 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1671 __isl_give isl_basic_map *isl_basic_map_free_equality(
1672 __isl_take isl_basic_map *bmap, unsigned n)
1674 if (!bmap)
1675 return NULL;
1676 if (n > bmap->n_eq)
1677 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1678 "invalid number of equalities",
1679 isl_basic_map_free(bmap));
1680 bmap->n_eq -= n;
1681 return bmap;
1684 __isl_give isl_basic_set *isl_basic_set_free_equality(
1685 __isl_take isl_basic_set *bset, unsigned n)
1687 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1688 n));
1691 /* Drop the equality constraint at position "pos",
1692 * preserving the order of the other equality constraints.
1694 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1696 isl_int *t;
1697 int r;
1699 if (!bmap)
1700 return -1;
1701 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1703 t = bmap->eq[pos];
1704 bmap->n_eq--;
1705 for (r = pos; r < bmap->n_eq; ++r)
1706 bmap->eq[r] = bmap->eq[r + 1];
1707 bmap->eq[bmap->n_eq] = t;
1709 return 0;
1712 /* Turn inequality "pos" of "bmap" into an equality.
1714 * In particular, we move the inequality in front of the equalities
1715 * and move the last inequality in the position of the moved inequality.
1716 * Note that isl_tab_make_equalities_explicit depends on this particular
1717 * change in the ordering of the constraints.
1719 void isl_basic_map_inequality_to_equality(
1720 __isl_keep isl_basic_map *bmap, unsigned pos)
1722 isl_int *t;
1724 t = bmap->ineq[pos];
1725 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1726 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1727 bmap->eq[-1] = t;
1728 bmap->n_eq++;
1729 bmap->n_ineq--;
1730 bmap->eq--;
1731 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1734 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1737 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1739 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1742 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1744 isl_size total;
1745 struct isl_ctx *ctx;
1747 total = isl_basic_map_dim(bmap, isl_dim_all);
1748 if (total < 0)
1749 return -1;
1750 ctx = bmap->ctx;
1751 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1755 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1756 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1757 bmap->extra - bmap->n_div);
1758 return bmap->n_ineq++;
1761 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1763 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1766 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1767 __isl_take isl_basic_map *bmap, unsigned n)
1769 if (!bmap)
1770 return NULL;
1771 if (n > bmap->n_ineq)
1772 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1773 "invalid number of inequalities",
1774 return isl_basic_map_free(bmap));
1775 bmap->n_ineq -= n;
1776 return bmap;
1779 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1780 __isl_take isl_basic_set *bset, unsigned n)
1782 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1783 n));
1786 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1788 isl_int *t;
1789 if (!bmap)
1790 return -1;
1791 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1793 if (pos != bmap->n_ineq - 1) {
1794 t = bmap->ineq[pos];
1795 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1796 bmap->ineq[bmap->n_ineq - 1] = t;
1797 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1799 bmap->n_ineq--;
1800 return 0;
1803 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1805 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1808 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1809 isl_int *eq)
1811 isl_bool empty;
1812 isl_size total;
1813 int k;
1815 empty = isl_basic_map_plain_is_empty(bmap);
1816 if (empty < 0)
1817 return isl_basic_map_free(bmap);
1818 if (empty)
1819 return bmap;
1821 bmap = isl_basic_map_cow(bmap);
1822 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1823 total = isl_basic_map_dim(bmap, isl_dim_all);
1824 if (total < 0)
1825 return isl_basic_map_free(bmap);
1826 k = isl_basic_map_alloc_equality(bmap);
1827 if (k < 0)
1828 goto error;
1829 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1830 return bmap;
1831 error:
1832 isl_basic_map_free(bmap);
1833 return NULL;
1836 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1837 isl_int *eq)
1839 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1842 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1843 isl_int *ineq)
1845 isl_size total;
1846 int k;
1848 bmap = isl_basic_map_cow(bmap);
1849 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1850 total = isl_basic_map_dim(bmap, isl_dim_all);
1851 if (total < 0)
1852 return isl_basic_map_free(bmap);
1853 k = isl_basic_map_alloc_inequality(bmap);
1854 if (k < 0)
1855 goto error;
1856 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1857 return bmap;
1858 error:
1859 isl_basic_map_free(bmap);
1860 return NULL;
1863 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1864 isl_int *ineq)
1866 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1869 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1871 isl_size total;
1873 total = isl_basic_map_dim(bmap, isl_dim_all);
1874 if (total < 0)
1875 return -1;
1876 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1877 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1878 bmap->extra - bmap->n_div);
1879 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1880 return bmap->n_div++;
1883 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1885 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1888 #undef TYPE
1889 #define TYPE isl_basic_map
1890 #include "check_type_range_templ.c"
1892 /* Check that there are "n" dimensions of type "type" starting at "first"
1893 * in "bset".
1895 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1896 enum isl_dim_type type, unsigned first, unsigned n)
1898 return isl_basic_map_check_range(bset_to_bmap(bset),
1899 type, first, n);
1902 /* Insert an extra integer division, prescribed by "div", to "bmap"
1903 * at (integer division) position "pos".
1905 * The integer division is first added at the end and then moved
1906 * into the right position.
1908 __isl_give isl_basic_map *isl_basic_map_insert_div(
1909 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1911 int i, k;
1912 isl_size total;
1914 bmap = isl_basic_map_cow(bmap);
1915 total = isl_basic_map_dim(bmap, isl_dim_all);
1916 if (total < 0 || !div)
1917 return isl_basic_map_free(bmap);
1919 if (div->size != 1 + 1 + total)
1920 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1921 "unexpected size", return isl_basic_map_free(bmap));
1922 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1923 return isl_basic_map_free(bmap);
1925 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1926 k = isl_basic_map_alloc_div(bmap);
1927 if (k < 0)
1928 return isl_basic_map_free(bmap);
1929 isl_seq_cpy(bmap->div[k], div->el, div->size);
1930 isl_int_set_si(bmap->div[k][div->size], 0);
1932 for (i = k; i > pos; --i)
1933 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1935 return bmap;
1938 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1940 if (!bmap)
1941 return isl_stat_error;
1942 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1943 bmap->n_div -= n;
1944 return isl_stat_ok;
1947 static __isl_give isl_basic_map *add_constraints(
1948 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1949 unsigned i_pos, unsigned o_pos)
1951 isl_size total, n_param, n_in, n_out, n_div;
1952 unsigned o_in, o_out;
1953 isl_ctx *ctx;
1954 isl_space *space;
1955 struct isl_dim_map *dim_map;
1957 space = isl_basic_map_peek_space(bmap2);
1958 if (!bmap1 || !space)
1959 goto error;
1961 total = isl_basic_map_dim(bmap1, isl_dim_all);
1962 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1963 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1964 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1965 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1966 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1967 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1968 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1969 goto error;
1970 ctx = isl_basic_map_get_ctx(bmap1);
1971 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1972 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1973 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1974 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1975 isl_dim_map_div(dim_map, bmap2, total);
1977 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1978 error:
1979 isl_basic_map_free(bmap1);
1980 isl_basic_map_free(bmap2);
1981 return NULL;
1984 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1985 unsigned extra, unsigned n_eq, unsigned n_ineq)
1987 isl_space *space;
1988 struct isl_basic_map *ext;
1989 unsigned flags;
1990 int dims_ok;
1992 if (!base)
1993 goto error;
1995 dims_ok = base->extra >= base->n_div + extra;
1997 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1998 room_for_ineq(base, n_ineq))
1999 return base;
2001 extra += base->extra;
2002 n_eq += base->n_eq;
2003 n_ineq += base->n_ineq;
2005 space = isl_basic_map_get_space(base);
2006 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2007 if (!ext)
2008 goto error;
2010 if (dims_ok)
2011 ext->sample = isl_vec_copy(base->sample);
2012 flags = base->flags;
2013 ext = add_constraints(ext, base, 0, 0);
2014 if (ext) {
2015 ext->flags = flags;
2016 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2019 return ext;
2021 error:
2022 isl_basic_map_free(base);
2023 return NULL;
2026 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2027 unsigned extra, unsigned n_eq, unsigned n_ineq)
2029 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2030 extra, n_eq, n_ineq));
2033 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2034 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2036 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2039 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2040 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2042 isl_basic_map *bmap = bset_to_bmap(base);
2043 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2044 return bset_from_bmap(bmap);
2047 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2049 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2052 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2054 if (!bmap)
2055 return NULL;
2057 if (bmap->ref > 1) {
2058 bmap->ref--;
2059 bmap = isl_basic_map_dup(bmap);
2061 if (bmap) {
2062 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2063 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2065 return bmap;
2068 /* Clear all cached information in "map", either because it is about
2069 * to be modified or because it is being freed.
2070 * Always return the same pointer that is passed in.
2071 * This is needed for the use in isl_map_free.
2073 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2075 isl_basic_map_free(map->cached_simple_hull[0]);
2076 isl_basic_map_free(map->cached_simple_hull[1]);
2077 map->cached_simple_hull[0] = NULL;
2078 map->cached_simple_hull[1] = NULL;
2079 return map;
2082 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2084 return isl_map_cow(set);
2087 /* Return an isl_map that is equal to "map" and that has only
2088 * a single reference.
2090 * If the original input already has only one reference, then
2091 * simply return it, but clear all cached information, since
2092 * it may be rendered invalid by the operations that will be
2093 * performed on the result.
2095 * Otherwise, create a duplicate (without any cached information).
2097 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2099 if (!map)
2100 return NULL;
2102 if (map->ref == 1)
2103 return clear_caches(map);
2104 map->ref--;
2105 return isl_map_dup(map);
2108 static void swap_vars(struct isl_blk blk, isl_int *a,
2109 unsigned a_len, unsigned b_len)
2111 isl_seq_cpy(blk.data, a+a_len, b_len);
2112 isl_seq_cpy(blk.data+b_len, a, a_len);
2113 isl_seq_cpy(a, blk.data, b_len+a_len);
2116 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2117 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2119 int i;
2120 struct isl_blk blk;
2122 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2123 goto error;
2125 if (n1 == 0 || n2 == 0)
2126 return bmap;
2128 bmap = isl_basic_map_cow(bmap);
2129 if (!bmap)
2130 return NULL;
2132 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2133 if (isl_blk_is_error(blk))
2134 goto error;
2136 for (i = 0; i < bmap->n_eq; ++i)
2137 swap_vars(blk,
2138 bmap->eq[i] + pos, n1, n2);
2140 for (i = 0; i < bmap->n_ineq; ++i)
2141 swap_vars(blk,
2142 bmap->ineq[i] + pos, n1, n2);
2144 for (i = 0; i < bmap->n_div; ++i)
2145 swap_vars(blk,
2146 bmap->div[i]+1 + pos, n1, n2);
2148 isl_blk_free(bmap->ctx, blk);
2150 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2151 bmap = isl_basic_map_gauss(bmap, NULL);
2152 return isl_basic_map_finalize(bmap);
2153 error:
2154 isl_basic_map_free(bmap);
2155 return NULL;
2158 /* The given basic map has turned out to be empty.
2159 * Explicitly mark it as such and change the representation
2160 * to a canonical representation of the empty basic map.
2161 * Since the basic map has conflicting constraints,
2162 * it must have at least one constraint, except perhaps
2163 * if it was already explicitly marked as being empty.
2164 * Do nothing in the latter case, i.e., if it has been marked empty and
2165 * has no constraints.
2167 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2168 __isl_take isl_basic_map *bmap)
2170 int i = 0;
2171 isl_bool empty;
2172 isl_size n;
2173 isl_size total;
2175 n = isl_basic_map_n_constraint(bmap);
2176 empty = isl_basic_map_plain_is_empty(bmap);
2177 if (n < 0 || empty < 0)
2178 return isl_basic_map_free(bmap);
2179 if (n == 0 && empty)
2180 return bmap;
2181 total = isl_basic_map_dim(bmap, isl_dim_all);
2182 if (total < 0)
2183 return isl_basic_map_free(bmap);
2184 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2185 return isl_basic_map_free(bmap);
2186 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2187 if (!bmap)
2188 return NULL;
2189 if (bmap->n_eq > 0) {
2190 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2191 if (!bmap)
2192 return NULL;
2193 } else {
2194 i = isl_basic_map_alloc_equality(bmap);
2195 if (i < 0)
2196 goto error;
2198 isl_int_set_si(bmap->eq[i][0], 1);
2199 isl_seq_clr(bmap->eq[i]+1, total);
2200 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2201 isl_vec_free(bmap->sample);
2202 bmap->sample = NULL;
2203 return isl_basic_map_finalize(bmap);
2204 error:
2205 isl_basic_map_free(bmap);
2206 return NULL;
2209 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2210 __isl_take isl_basic_set *bset)
2212 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2215 __isl_give isl_basic_map *isl_basic_map_set_rational(
2216 __isl_take isl_basic_map *bmap)
2218 if (!bmap)
2219 return NULL;
2221 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2222 return bmap;
2224 bmap = isl_basic_map_cow(bmap);
2225 if (!bmap)
2226 return NULL;
2228 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2230 return isl_basic_map_finalize(bmap);
2233 __isl_give isl_basic_set *isl_basic_set_set_rational(
2234 __isl_take isl_basic_set *bset)
2236 return isl_basic_map_set_rational(bset);
2239 __isl_give isl_basic_set *isl_basic_set_set_integral(
2240 __isl_take isl_basic_set *bset)
2242 if (!bset)
2243 return NULL;
2245 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2246 return bset;
2248 bset = isl_basic_set_cow(bset);
2249 if (!bset)
2250 return NULL;
2252 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2254 return isl_basic_set_finalize(bset);
2257 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2259 int i;
2261 map = isl_map_cow(map);
2262 if (!map)
2263 return NULL;
2264 for (i = 0; i < map->n; ++i) {
2265 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2266 if (!map->p[i])
2267 goto error;
2269 return map;
2270 error:
2271 isl_map_free(map);
2272 return NULL;
2275 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2277 return isl_map_set_rational(set);
2280 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2281 * of "bmap").
2283 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2285 isl_int *t = bmap->div[a];
2286 bmap->div[a] = bmap->div[b];
2287 bmap->div[b] = t;
2290 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2291 * div definitions accordingly.
2293 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2294 int a, int b)
2296 int i;
2297 isl_size off;
2299 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2300 if (off < 0)
2301 return isl_basic_map_free(bmap);
2303 swap_div(bmap, a, b);
2305 for (i = 0; i < bmap->n_eq; ++i)
2306 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2308 for (i = 0; i < bmap->n_ineq; ++i)
2309 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2311 for (i = 0; i < bmap->n_div; ++i)
2312 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2313 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2315 return bmap;
2318 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2320 isl_seq_cpy(c, c + n, rem);
2321 isl_seq_clr(c + rem, n);
2324 /* Drop n dimensions starting at first.
2326 * In principle, this frees up some extra variables as the number
2327 * of columns remains constant, but we would have to extend
2328 * the div array too as the number of rows in this array is assumed
2329 * to be equal to extra.
2331 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2332 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2334 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2337 /* Move "n" divs starting at "first" to the end of the list of divs.
2339 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2340 unsigned first, unsigned n)
2342 isl_int **div;
2343 int i;
2345 if (first + n == bmap->n_div)
2346 return bmap;
2348 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2349 if (!div)
2350 goto error;
2351 for (i = 0; i < n; ++i)
2352 div[i] = bmap->div[first + i];
2353 for (i = 0; i < bmap->n_div - first - n; ++i)
2354 bmap->div[first + i] = bmap->div[first + n + i];
2355 for (i = 0; i < n; ++i)
2356 bmap->div[bmap->n_div - n + i] = div[i];
2357 free(div);
2358 return bmap;
2359 error:
2360 isl_basic_map_free(bmap);
2361 return NULL;
2364 #undef TYPE
2365 #define TYPE isl_map
2366 static
2367 #include "check_type_range_templ.c"
2369 /* Check that there are "n" dimensions of type "type" starting at "first"
2370 * in "set".
2372 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2373 enum isl_dim_type type, unsigned first, unsigned n)
2375 return isl_map_check_range(set_to_map(set), type, first, n);
2378 /* Drop "n" dimensions of type "type" starting at "first".
2379 * Perform the core computation, without cowing or
2380 * simplifying and finalizing the result.
2382 * In principle, this frees up some extra variables as the number
2383 * of columns remains constant, but we would have to extend
2384 * the div array too as the number of rows in this array is assumed
2385 * to be equal to extra.
2387 __isl_give isl_basic_map *isl_basic_map_drop_core(
2388 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2389 unsigned first, unsigned n)
2391 int i;
2392 unsigned offset;
2393 unsigned left;
2394 isl_size total;
2396 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2397 return isl_basic_map_free(bmap);
2399 total = isl_basic_map_dim(bmap, isl_dim_all);
2400 if (total < 0)
2401 return isl_basic_map_free(bmap);
2403 offset = isl_basic_map_offset(bmap, type) + first;
2404 left = total - (offset - 1) - n;
2405 for (i = 0; i < bmap->n_eq; ++i)
2406 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2408 for (i = 0; i < bmap->n_ineq; ++i)
2409 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2411 for (i = 0; i < bmap->n_div; ++i)
2412 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2414 if (type == isl_dim_div) {
2415 bmap = move_divs_last(bmap, first, n);
2416 if (!bmap)
2417 return NULL;
2418 if (isl_basic_map_free_div(bmap, n) < 0)
2419 return isl_basic_map_free(bmap);
2420 } else
2421 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2422 if (!bmap->dim)
2423 return isl_basic_map_free(bmap);
2425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2426 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2427 return bmap;
2430 /* Drop "n" dimensions of type "type" starting at "first".
2432 * In principle, this frees up some extra variables as the number
2433 * of columns remains constant, but we would have to extend
2434 * the div array too as the number of rows in this array is assumed
2435 * to be equal to extra.
2437 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2438 enum isl_dim_type type, unsigned first, unsigned n)
2440 if (!bmap)
2441 return NULL;
2442 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2443 return bmap;
2445 bmap = isl_basic_map_cow(bmap);
2446 if (!bmap)
2447 return NULL;
2449 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2451 bmap = isl_basic_map_simplify(bmap);
2452 return isl_basic_map_finalize(bmap);
2455 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2456 enum isl_dim_type type, unsigned first, unsigned n)
2458 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2459 type, first, n));
2462 /* No longer consider "map" to be normalized.
2464 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2466 if (!map)
2467 return NULL;
2468 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2469 return map;
2472 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2473 enum isl_dim_type type, unsigned first, unsigned n)
2475 int i;
2476 isl_space *space;
2478 if (isl_map_check_range(map, type, first, n) < 0)
2479 return isl_map_free(map);
2481 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2482 return map;
2483 map = isl_map_cow(map);
2484 if (!map)
2485 goto error;
2487 for (i = 0; i < map->n; ++i) {
2488 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2489 if (!map->p[i])
2490 goto error;
2492 map = isl_map_unmark_normalized(map);
2494 space = isl_map_take_space(map);
2495 space = isl_space_drop_dims(space, type, first, n);
2496 map = isl_map_restore_space(map, space);
2498 return map;
2499 error:
2500 isl_map_free(map);
2501 return NULL;
2504 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2505 enum isl_dim_type type, unsigned first, unsigned n)
2507 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2510 /* Drop the integer division at position "div", which is assumed
2511 * not to appear in any of the constraints or
2512 * in any of the other integer divisions.
2514 * Since the integer division is redundant, there is no need to cow.
2516 __isl_give isl_basic_map *isl_basic_map_drop_div(
2517 __isl_take isl_basic_map *bmap, unsigned div)
2519 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2522 /* Eliminate the specified n dimensions starting at first from the
2523 * constraints, without removing the dimensions from the space.
2524 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2526 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2527 enum isl_dim_type type, unsigned first, unsigned n)
2529 int i;
2531 if (n == 0)
2532 return map;
2534 if (isl_map_check_range(map, type, first, n) < 0)
2535 return isl_map_free(map);
2537 map = isl_map_cow(map);
2538 if (!map)
2539 return NULL;
2541 for (i = 0; i < map->n; ++i) {
2542 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2543 if (!map->p[i])
2544 goto error;
2546 return map;
2547 error:
2548 isl_map_free(map);
2549 return NULL;
2552 /* Eliminate the specified n dimensions starting at first from the
2553 * constraints, without removing the dimensions from the space.
2554 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2556 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2557 enum isl_dim_type type, unsigned first, unsigned n)
2559 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2562 /* Eliminate the specified n dimensions starting at first from the
2563 * constraints, without removing the dimensions from the space.
2564 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2566 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2567 unsigned first, unsigned n)
2569 return isl_set_eliminate(set, isl_dim_set, first, n);
2572 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2573 __isl_take isl_basic_map *bmap)
2575 isl_size v_div;
2577 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2578 if (v_div < 0)
2579 return isl_basic_map_free(bmap);
2580 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2581 if (!bmap)
2582 return NULL;
2583 bmap->n_div = 0;
2584 return isl_basic_map_finalize(bmap);
2587 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2588 __isl_take isl_basic_set *bset)
2590 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2593 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2595 int i;
2597 if (!map)
2598 return NULL;
2599 if (map->n == 0)
2600 return map;
2602 map = isl_map_cow(map);
2603 if (!map)
2604 return NULL;
2606 for (i = 0; i < map->n; ++i) {
2607 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2608 if (!map->p[i])
2609 goto error;
2611 return map;
2612 error:
2613 isl_map_free(map);
2614 return NULL;
2617 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2619 return isl_map_remove_divs(set);
2622 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2623 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2624 unsigned first, unsigned n)
2626 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2627 return isl_basic_map_free(bmap);
2628 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2629 return bmap;
2630 bmap = isl_basic_map_eliminate_vars(bmap,
2631 isl_basic_map_offset(bmap, type) - 1 + first, n);
2632 if (!bmap)
2633 return bmap;
2634 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2635 return bmap;
2636 bmap = isl_basic_map_drop(bmap, type, first, n);
2637 return bmap;
2640 /* Return true if the definition of the given div (recursively) involves
2641 * any of the given variables.
2643 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2644 unsigned first, unsigned n)
2646 int i;
2647 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2649 if (isl_int_is_zero(bmap->div[div][0]))
2650 return isl_bool_false;
2651 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2652 return isl_bool_true;
2654 for (i = bmap->n_div - 1; i >= 0; --i) {
2655 isl_bool involves;
2657 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2658 continue;
2659 involves = div_involves_vars(bmap, i, first, n);
2660 if (involves < 0 || involves)
2661 return involves;
2664 return isl_bool_false;
2667 /* Try and add a lower and/or upper bound on "div" to "bmap"
2668 * based on inequality "i".
2669 * "total" is the total number of variables (excluding the divs).
2670 * "v" is a temporary object that can be used during the calculations.
2671 * If "lb" is set, then a lower bound should be constructed.
2672 * If "ub" is set, then an upper bound should be constructed.
2674 * The calling function has already checked that the inequality does not
2675 * reference "div", but we still need to check that the inequality is
2676 * of the right form. We'll consider the case where we want to construct
2677 * a lower bound. The construction of upper bounds is similar.
2679 * Let "div" be of the form
2681 * q = floor((a + f(x))/d)
2683 * We essentially check if constraint "i" is of the form
2685 * b + f(x) >= 0
2687 * so that we can use it to derive a lower bound on "div".
2688 * However, we allow a slightly more general form
2690 * b + g(x) >= 0
2692 * with the condition that the coefficients of g(x) - f(x) are all
2693 * divisible by d.
2694 * Rewriting this constraint as
2696 * 0 >= -b - g(x)
2698 * adding a + f(x) to both sides and dividing by d, we obtain
2700 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2702 * Taking the floor on both sides, we obtain
2704 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2706 * or
2708 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2710 * In the case of an upper bound, we construct the constraint
2712 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2715 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2716 __isl_take isl_basic_map *bmap, int div, int i,
2717 unsigned total, isl_int v, int lb, int ub)
2719 int j;
2721 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2722 if (lb) {
2723 isl_int_sub(v, bmap->ineq[i][1 + j],
2724 bmap->div[div][1 + 1 + j]);
2725 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2727 if (ub) {
2728 isl_int_add(v, bmap->ineq[i][1 + j],
2729 bmap->div[div][1 + 1 + j]);
2730 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2733 if (!lb && !ub)
2734 return bmap;
2736 bmap = isl_basic_map_cow(bmap);
2737 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2738 if (lb) {
2739 int k = isl_basic_map_alloc_inequality(bmap);
2740 if (k < 0)
2741 goto error;
2742 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2743 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2744 bmap->div[div][1 + j]);
2745 isl_int_cdiv_q(bmap->ineq[k][j],
2746 bmap->ineq[k][j], bmap->div[div][0]);
2748 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2750 if (ub) {
2751 int k = isl_basic_map_alloc_inequality(bmap);
2752 if (k < 0)
2753 goto error;
2754 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2755 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2756 bmap->div[div][1 + j]);
2757 isl_int_fdiv_q(bmap->ineq[k][j],
2758 bmap->ineq[k][j], bmap->div[div][0]);
2760 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2763 return bmap;
2764 error:
2765 isl_basic_map_free(bmap);
2766 return NULL;
2769 /* This function is called right before "div" is eliminated from "bmap"
2770 * using Fourier-Motzkin.
2771 * Look through the constraints of "bmap" for constraints on the argument
2772 * of the integer division and use them to construct constraints on the
2773 * integer division itself. These constraints can then be combined
2774 * during the Fourier-Motzkin elimination.
2775 * Note that it is only useful to introduce lower bounds on "div"
2776 * if "bmap" already contains upper bounds on "div" as the newly
2777 * introduce lower bounds can then be combined with the pre-existing
2778 * upper bounds. Similarly for upper bounds.
2779 * We therefore first check if "bmap" contains any lower and/or upper bounds
2780 * on "div".
2782 * It is interesting to note that the introduction of these constraints
2783 * can indeed lead to more accurate results, even when compared to
2784 * deriving constraints on the argument of "div" from constraints on "div".
2785 * Consider, for example, the set
2787 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2789 * The second constraint can be rewritten as
2791 * 2 * [(-i-2j+3)/4] + k >= 0
2793 * from which we can derive
2795 * -i - 2j + 3 >= -2k
2797 * or
2799 * i + 2j <= 3 + 2k
2801 * Combined with the first constraint, we obtain
2803 * -3 <= 3 + 2k or k >= -3
2805 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2806 * the first constraint, we obtain
2808 * [(i + 2j)/4] >= [-3/4] = -1
2810 * Combining this constraint with the second constraint, we obtain
2812 * k >= -2
2814 static __isl_give isl_basic_map *insert_bounds_on_div(
2815 __isl_take isl_basic_map *bmap, int div)
2817 int i;
2818 int check_lb, check_ub;
2819 isl_int v;
2820 isl_size v_div;
2822 if (!bmap)
2823 return NULL;
2825 if (isl_int_is_zero(bmap->div[div][0]))
2826 return bmap;
2828 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2829 if (v_div < 0)
2830 return isl_basic_map_free(bmap);
2832 check_lb = 0;
2833 check_ub = 0;
2834 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2835 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2836 if (s > 0)
2837 check_ub = 1;
2838 if (s < 0)
2839 check_lb = 1;
2842 if (!check_lb && !check_ub)
2843 return bmap;
2845 isl_int_init(v);
2847 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2848 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2849 continue;
2851 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2852 check_lb, check_ub);
2855 isl_int_clear(v);
2857 return bmap;
2860 /* Remove all divs (recursively) involving any of the given dimensions
2861 * in their definitions.
2863 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2864 __isl_take isl_basic_map *bmap,
2865 enum isl_dim_type type, unsigned first, unsigned n)
2867 int i;
2869 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2870 return isl_basic_map_free(bmap);
2871 first += isl_basic_map_offset(bmap, type);
2873 for (i = bmap->n_div - 1; i >= 0; --i) {
2874 isl_bool involves;
2876 involves = div_involves_vars(bmap, i, first, n);
2877 if (involves < 0)
2878 return isl_basic_map_free(bmap);
2879 if (!involves)
2880 continue;
2881 bmap = insert_bounds_on_div(bmap, i);
2882 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2883 if (!bmap)
2884 return NULL;
2885 i = bmap->n_div;
2888 return bmap;
2891 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2892 __isl_take isl_basic_set *bset,
2893 enum isl_dim_type type, unsigned first, unsigned n)
2895 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2898 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2899 enum isl_dim_type type, unsigned first, unsigned n)
2901 int i;
2903 if (!map)
2904 return NULL;
2905 if (map->n == 0)
2906 return map;
2908 map = isl_map_cow(map);
2909 if (!map)
2910 return NULL;
2912 for (i = 0; i < map->n; ++i) {
2913 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2914 type, first, n);
2915 if (!map->p[i])
2916 goto error;
2918 return map;
2919 error:
2920 isl_map_free(map);
2921 return NULL;
2924 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2928 type, first, n));
2931 /* Does the description of "bmap" depend on the specified dimensions?
2932 * We also check whether the dimensions appear in any of the div definitions.
2933 * In principle there is no need for this check. If the dimensions appear
2934 * in a div definition, they also appear in the defining constraints of that
2935 * div.
2937 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2938 enum isl_dim_type type, unsigned first, unsigned n)
2940 int i;
2942 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2943 return isl_bool_error;
2945 first += isl_basic_map_offset(bmap, type);
2946 for (i = 0; i < bmap->n_eq; ++i)
2947 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2948 return isl_bool_true;
2949 for (i = 0; i < bmap->n_ineq; ++i)
2950 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2951 return isl_bool_true;
2952 for (i = 0; i < bmap->n_div; ++i) {
2953 if (isl_int_is_zero(bmap->div[i][0]))
2954 continue;
2955 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2956 return isl_bool_true;
2959 return isl_bool_false;
2962 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2965 int i;
2967 if (isl_map_check_range(map, type, first, n) < 0)
2968 return isl_bool_error;
2970 for (i = 0; i < map->n; ++i) {
2971 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2972 type, first, n);
2973 if (involves < 0 || involves)
2974 return involves;
2977 return isl_bool_false;
2980 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2981 enum isl_dim_type type, unsigned first, unsigned n)
2983 return isl_basic_map_involves_dims(bset, type, first, n);
2986 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2987 enum isl_dim_type type, unsigned first, unsigned n)
2989 return isl_map_involves_dims(set, type, first, n);
2992 /* Does "bset" involve any local variables, i.e., integer divisions?
2994 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2996 isl_size n;
2998 n = isl_basic_set_dim(bset, isl_dim_div);
2999 if (n < 0)
3000 return isl_bool_error;
3001 return isl_bool_ok(n > 0);
3004 /* isl_set_every_basic_set callback that checks whether "bset"
3005 * is free of local variables.
3007 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3009 return isl_bool_not(isl_basic_set_involves_locals(bset));
3012 /* Does "set" involve any local variables, i.e., integer divisions?
3014 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3016 isl_bool no_locals;
3018 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3019 return isl_bool_not(no_locals);
3022 /* Drop all constraints in bmap that involve any of the dimensions
3023 * first to first+n-1.
3024 * This function only performs the actual removal of constraints.
3026 * This function should not call finalize since it is used by
3027 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3029 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3030 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3032 int i;
3034 if (n == 0)
3035 return bmap;
3037 bmap = isl_basic_map_cow(bmap);
3039 if (!bmap)
3040 return NULL;
3042 for (i = bmap->n_eq - 1; i >= 0; --i) {
3043 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3044 continue;
3045 if (isl_basic_map_drop_equality(bmap, i) < 0)
3046 return isl_basic_map_free(bmap);
3049 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3050 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3051 continue;
3052 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3053 return isl_basic_map_free(bmap);
3056 return bmap;
3059 /* Drop all constraints in bset that involve any of the dimensions
3060 * first to first+n-1.
3061 * This function only performs the actual removal of constraints.
3063 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3064 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3066 return isl_basic_map_drop_constraints_involving(bset, first, n);
3069 /* Drop all constraints in bmap that do not involve any of the dimensions
3070 * first to first + n - 1 of the given type.
3072 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3073 __isl_take isl_basic_map *bmap,
3074 enum isl_dim_type type, unsigned first, unsigned n)
3076 int i;
3078 if (n == 0) {
3079 isl_space *space = isl_basic_map_get_space(bmap);
3080 isl_basic_map_free(bmap);
3081 return isl_basic_map_universe(space);
3083 bmap = isl_basic_map_cow(bmap);
3084 if (!bmap)
3085 return NULL;
3087 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3088 return isl_basic_map_free(bmap);
3090 first += isl_basic_map_offset(bmap, type) - 1;
3092 for (i = bmap->n_eq - 1; i >= 0; --i) {
3093 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3094 continue;
3095 if (isl_basic_map_drop_equality(bmap, i) < 0)
3096 return isl_basic_map_free(bmap);
3099 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3100 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3101 continue;
3102 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3103 return isl_basic_map_free(bmap);
3106 bmap = isl_basic_map_add_known_div_constraints(bmap);
3107 return bmap;
3110 /* Drop all constraints in bset that do not involve any of the dimensions
3111 * first to first + n - 1 of the given type.
3113 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3114 __isl_take isl_basic_set *bset,
3115 enum isl_dim_type type, unsigned first, unsigned n)
3117 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3118 type, first, n);
3121 /* Drop all constraints in bmap that involve any of the dimensions
3122 * first to first + n - 1 of the given type.
3124 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3125 __isl_take isl_basic_map *bmap,
3126 enum isl_dim_type type, unsigned first, unsigned n)
3128 if (!bmap)
3129 return NULL;
3130 if (n == 0)
3131 return bmap;
3133 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3134 return isl_basic_map_free(bmap);
3136 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3137 first += isl_basic_map_offset(bmap, type) - 1;
3138 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3139 bmap = isl_basic_map_add_known_div_constraints(bmap);
3140 return bmap;
3143 /* Drop all constraints in bset that involve any of the dimensions
3144 * first to first + n - 1 of the given type.
3146 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3147 __isl_take isl_basic_set *bset,
3148 enum isl_dim_type type, unsigned first, unsigned n)
3150 return isl_basic_map_drop_constraints_involving_dims(bset,
3151 type, first, n);
3154 /* Drop constraints from "map" by applying "drop" to each basic map.
3156 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3157 enum isl_dim_type type, unsigned first, unsigned n,
3158 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3159 enum isl_dim_type type, unsigned first, unsigned n))
3161 int i;
3163 if (isl_map_check_range(map, type, first, n) < 0)
3164 return isl_map_free(map);
3166 map = isl_map_cow(map);
3167 if (!map)
3168 return NULL;
3170 for (i = 0; i < map->n; ++i) {
3171 map->p[i] = drop(map->p[i], type, first, n);
3172 if (!map->p[i])
3173 return isl_map_free(map);
3176 if (map->n > 1)
3177 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3179 return map;
3182 /* Drop all constraints in map that involve any of the dimensions
3183 * first to first + n - 1 of the given type.
3185 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3186 __isl_take isl_map *map,
3187 enum isl_dim_type type, unsigned first, unsigned n)
3189 if (n == 0)
3190 return map;
3191 return drop_constraints(map, type, first, n,
3192 &isl_basic_map_drop_constraints_involving_dims);
3195 /* Drop all constraints in "map" that do not involve any of the dimensions
3196 * first to first + n - 1 of the given type.
3198 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3199 __isl_take isl_map *map,
3200 enum isl_dim_type type, unsigned first, unsigned n)
3202 if (n == 0) {
3203 isl_space *space = isl_map_get_space(map);
3204 isl_map_free(map);
3205 return isl_map_universe(space);
3207 return drop_constraints(map, type, first, n,
3208 &isl_basic_map_drop_constraints_not_involving_dims);
3211 /* Drop all constraints in set that involve any of the dimensions
3212 * first to first + n - 1 of the given type.
3214 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3215 __isl_take isl_set *set,
3216 enum isl_dim_type type, unsigned first, unsigned n)
3218 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3221 /* Drop all constraints in "set" that do not involve any of the dimensions
3222 * first to first + n - 1 of the given type.
3224 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3225 __isl_take isl_set *set,
3226 enum isl_dim_type type, unsigned first, unsigned n)
3228 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3231 /* Does local variable "div" of "bmap" have a complete explicit representation?
3232 * Having a complete explicit representation requires not only
3233 * an explicit representation, but also that all local variables
3234 * that appear in this explicit representation in turn have
3235 * a complete explicit representation.
3237 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3239 int i;
3240 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3241 isl_bool marked;
3243 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3244 if (marked < 0 || marked)
3245 return isl_bool_not(marked);
3247 for (i = bmap->n_div - 1; i >= 0; --i) {
3248 isl_bool known;
3250 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3251 continue;
3252 known = isl_basic_map_div_is_known(bmap, i);
3253 if (known < 0 || !known)
3254 return known;
3257 return isl_bool_true;
3260 /* Remove all divs that are unknown or defined in terms of unknown divs.
3262 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3263 __isl_take isl_basic_map *bmap)
3265 int i;
3267 if (!bmap)
3268 return NULL;
3270 for (i = bmap->n_div - 1; i >= 0; --i) {
3271 if (isl_basic_map_div_is_known(bmap, i))
3272 continue;
3273 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3274 if (!bmap)
3275 return NULL;
3276 i = bmap->n_div;
3279 return bmap;
3282 /* Remove all divs that are unknown or defined in terms of unknown divs.
3284 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3285 __isl_take isl_basic_set *bset)
3287 return isl_basic_map_remove_unknown_divs(bset);
3290 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3292 int i;
3294 if (!map)
3295 return NULL;
3296 if (map->n == 0)
3297 return map;
3299 map = isl_map_cow(map);
3300 if (!map)
3301 return NULL;
3303 for (i = 0; i < map->n; ++i) {
3304 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3305 if (!map->p[i])
3306 goto error;
3308 return map;
3309 error:
3310 isl_map_free(map);
3311 return NULL;
3314 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3316 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3319 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3320 __isl_take isl_basic_set *bset,
3321 enum isl_dim_type type, unsigned first, unsigned n)
3323 isl_basic_map *bmap = bset_to_bmap(bset);
3324 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3325 return bset_from_bmap(bmap);
3328 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3329 enum isl_dim_type type, unsigned first, unsigned n)
3331 int i;
3333 if (n == 0)
3334 return map;
3336 map = isl_map_cow(map);
3337 if (isl_map_check_range(map, type, first, n) < 0)
3338 return isl_map_free(map);
3340 for (i = 0; i < map->n; ++i) {
3341 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3342 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3343 if (!map->p[i])
3344 goto error;
3346 map = isl_map_drop(map, type, first, n);
3347 return map;
3348 error:
3349 isl_map_free(map);
3350 return NULL;
3353 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3354 enum isl_dim_type type, unsigned first, unsigned n)
3356 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3357 type, first, n));
3360 /* Project out n inputs starting at first using Fourier-Motzkin */
3361 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3362 unsigned first, unsigned n)
3364 return isl_map_remove_dims(map, isl_dim_in, first, n);
3367 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3368 FILE *out, int indent)
3370 isl_printer *p;
3372 if (!bset) {
3373 fprintf(out, "null basic set\n");
3374 return;
3377 fprintf(out, "%*s", indent, "");
3378 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3379 bset->ref, bset->dim->nparam, bset->dim->n_out,
3380 bset->extra, bset->flags);
3382 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3383 p = isl_printer_set_dump(p, 1);
3384 p = isl_printer_set_indent(p, indent);
3385 p = isl_printer_start_line(p);
3386 p = isl_printer_print_basic_set(p, bset);
3387 p = isl_printer_end_line(p);
3388 isl_printer_free(p);
3391 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3392 FILE *out, int indent)
3394 isl_printer *p;
3396 if (!bmap) {
3397 fprintf(out, "null basic map\n");
3398 return;
3401 fprintf(out, "%*s", indent, "");
3402 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3403 "flags: %x, n_name: %d\n",
3404 bmap->ref,
3405 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3406 bmap->extra, bmap->flags, bmap->dim->n_id);
3408 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3409 p = isl_printer_set_dump(p, 1);
3410 p = isl_printer_set_indent(p, indent);
3411 p = isl_printer_start_line(p);
3412 p = isl_printer_print_basic_map(p, bmap);
3413 p = isl_printer_end_line(p);
3414 isl_printer_free(p);
3417 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3418 unsigned pos)
3420 isl_size total;
3422 total = isl_basic_map_dim(bmap, isl_dim_all);
3423 if (total < 0)
3424 return isl_basic_map_free(bmap);
3425 if (pos >= bmap->n_ineq)
3426 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3427 "invalid position", return isl_basic_map_free(bmap));
3428 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3429 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3430 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3431 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3432 return bmap;
3435 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3436 unsigned flags)
3438 if (isl_space_check_is_set(space) < 0)
3439 goto error;
3440 return isl_map_alloc_space(space, n, flags);
3441 error:
3442 isl_space_free(space);
3443 return NULL;
3446 /* Make sure "map" has room for at least "n" more basic maps.
3448 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3450 int i;
3451 struct isl_map *grown = NULL;
3453 if (!map)
3454 return NULL;
3455 isl_assert(map->ctx, n >= 0, goto error);
3456 if (map->n + n <= map->size)
3457 return map;
3458 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3459 if (!grown)
3460 goto error;
3461 for (i = 0; i < map->n; ++i) {
3462 grown->p[i] = isl_basic_map_copy(map->p[i]);
3463 if (!grown->p[i])
3464 goto error;
3465 grown->n++;
3467 isl_map_free(map);
3468 return grown;
3469 error:
3470 isl_map_free(grown);
3471 isl_map_free(map);
3472 return NULL;
3475 /* Make sure "set" has room for at least "n" more basic sets.
3477 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3479 return set_from_map(isl_map_grow(set_to_map(set), n));
3482 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3484 return isl_map_from_basic_map(bset);
3487 /* This function performs the same operation as isl_set_from_basic_set,
3488 * but is considered as a function on an isl_basic_set when exported.
3490 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3492 return isl_set_from_basic_set(bset);
3495 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3497 struct isl_map *map;
3499 if (!bmap)
3500 return NULL;
3502 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3503 return isl_map_add_basic_map(map, bmap);
3506 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3507 __isl_take isl_basic_set *bset)
3509 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3510 bset_to_bmap(bset)));
3513 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3515 return isl_map_free(set);
3518 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3520 int i;
3522 if (!set) {
3523 fprintf(out, "null set\n");
3524 return;
3527 fprintf(out, "%*s", indent, "");
3528 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3529 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3530 set->flags);
3531 for (i = 0; i < set->n; ++i) {
3532 fprintf(out, "%*s", indent, "");
3533 fprintf(out, "basic set %d:\n", i);
3534 isl_basic_set_print_internal(set->p[i], out, indent+4);
3538 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3540 int i;
3542 if (!map) {
3543 fprintf(out, "null map\n");
3544 return;
3547 fprintf(out, "%*s", indent, "");
3548 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3549 "flags: %x, n_name: %d\n",
3550 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3551 map->dim->n_out, map->flags, map->dim->n_id);
3552 for (i = 0; i < map->n; ++i) {
3553 fprintf(out, "%*s", indent, "");
3554 fprintf(out, "basic map %d:\n", i);
3555 isl_basic_map_print_internal(map->p[i], out, indent+4);
3559 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3561 static isl_stat isl_basic_map_check_compatible_domain(
3562 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3564 isl_bool ok;
3566 ok = isl_basic_map_compatible_domain(bmap, bset);
3567 if (ok < 0)
3568 return isl_stat_error;
3569 if (!ok)
3570 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3571 "incompatible spaces", return isl_stat_error);
3573 return isl_stat_ok;
3576 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3577 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3579 struct isl_basic_map *bmap_domain;
3580 isl_size dim;
3582 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3583 goto error;
3585 dim = isl_basic_set_dim(bset, isl_dim_set);
3586 if (dim < 0)
3587 goto error;
3588 if (dim != 0 &&
3589 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3590 goto error;
3592 bmap = isl_basic_map_cow(bmap);
3593 if (!bmap)
3594 goto error;
3595 bmap = isl_basic_map_extend(bmap,
3596 bset->n_div, bset->n_eq, bset->n_ineq);
3597 bmap_domain = isl_basic_map_from_domain(bset);
3598 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3600 bmap = isl_basic_map_simplify(bmap);
3601 return isl_basic_map_finalize(bmap);
3602 error:
3603 isl_basic_map_free(bmap);
3604 isl_basic_set_free(bset);
3605 return NULL;
3608 /* Check that the space of "bset" is the same as that of the range of "bmap".
3610 static isl_stat isl_basic_map_check_compatible_range(
3611 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3613 isl_bool ok;
3615 ok = isl_basic_map_compatible_range(bmap, bset);
3616 if (ok < 0)
3617 return isl_stat_error;
3618 if (!ok)
3619 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3620 "incompatible spaces", return isl_stat_error);
3622 return isl_stat_ok;
3625 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3626 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3628 struct isl_basic_map *bmap_range;
3629 isl_size dim;
3631 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3632 goto error;
3634 dim = isl_basic_set_dim(bset, isl_dim_set);
3635 if (dim < 0)
3636 goto error;
3637 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3638 goto error;
3640 if (isl_basic_set_plain_is_universe(bset)) {
3641 isl_basic_set_free(bset);
3642 return bmap;
3645 bmap = isl_basic_map_cow(bmap);
3646 if (!bmap)
3647 goto error;
3648 bmap = isl_basic_map_extend(bmap,
3649 bset->n_div, bset->n_eq, bset->n_ineq);
3650 bmap_range = bset_to_bmap(bset);
3651 bmap = add_constraints(bmap, bmap_range, 0, 0);
3653 bmap = isl_basic_map_simplify(bmap);
3654 return isl_basic_map_finalize(bmap);
3655 error:
3656 isl_basic_map_free(bmap);
3657 isl_basic_set_free(bset);
3658 return NULL;
3661 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3662 __isl_keep isl_vec *vec)
3664 int i;
3665 isl_size total;
3666 isl_int s;
3668 total = isl_basic_map_dim(bmap, isl_dim_all);
3669 if (total < 0 || !vec)
3670 return isl_bool_error;
3672 if (1 + total != vec->size)
3673 return isl_bool_false;
3675 isl_int_init(s);
3677 for (i = 0; i < bmap->n_eq; ++i) {
3678 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3679 if (!isl_int_is_zero(s)) {
3680 isl_int_clear(s);
3681 return isl_bool_false;
3685 for (i = 0; i < bmap->n_ineq; ++i) {
3686 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3687 if (isl_int_is_neg(s)) {
3688 isl_int_clear(s);
3689 return isl_bool_false;
3693 isl_int_clear(s);
3695 return isl_bool_true;
3698 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3699 __isl_keep isl_vec *vec)
3701 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3704 __isl_give isl_basic_map *isl_basic_map_intersect(
3705 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3707 struct isl_vec *sample = NULL;
3708 isl_space *space1, *space2;
3709 isl_size dim1, dim2, nparam1, nparam2;
3711 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3712 goto error;
3713 space1 = isl_basic_map_peek_space(bmap1);
3714 space2 = isl_basic_map_peek_space(bmap2);
3715 dim1 = isl_space_dim(space1, isl_dim_all);
3716 dim2 = isl_space_dim(space2, isl_dim_all);
3717 nparam1 = isl_space_dim(space1, isl_dim_param);
3718 nparam2 = isl_space_dim(space2, isl_dim_param);
3719 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3720 goto error;
3721 if (dim1 == nparam1 && dim2 != nparam2)
3722 return isl_basic_map_intersect(bmap2, bmap1);
3724 if (dim2 != nparam2 &&
3725 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3726 goto error;
3728 if (isl_basic_map_plain_is_empty(bmap1)) {
3729 isl_basic_map_free(bmap2);
3730 return bmap1;
3732 if (isl_basic_map_plain_is_empty(bmap2)) {
3733 isl_basic_map_free(bmap1);
3734 return bmap2;
3737 if (bmap1->sample &&
3738 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3739 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3740 sample = isl_vec_copy(bmap1->sample);
3741 else if (bmap2->sample &&
3742 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3743 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3744 sample = isl_vec_copy(bmap2->sample);
3746 bmap1 = isl_basic_map_cow(bmap1);
3747 if (!bmap1)
3748 goto error;
3749 bmap1 = isl_basic_map_extend(bmap1,
3750 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3751 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3753 if (!bmap1)
3754 isl_vec_free(sample);
3755 else if (sample) {
3756 isl_vec_free(bmap1->sample);
3757 bmap1->sample = sample;
3760 bmap1 = isl_basic_map_simplify(bmap1);
3761 return isl_basic_map_finalize(bmap1);
3762 error:
3763 if (sample)
3764 isl_vec_free(sample);
3765 isl_basic_map_free(bmap1);
3766 isl_basic_map_free(bmap2);
3767 return NULL;
3770 __isl_give isl_basic_set *isl_basic_set_intersect(
3771 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3773 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3774 bset_to_bmap(bset2)));
3777 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3778 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3780 return isl_basic_set_intersect(bset1, bset2);
3783 /* Does "map" consist of a single disjunct, without any local variables?
3785 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
3787 isl_size n_div;
3789 if (!map)
3790 return isl_bool_error;
3791 if (map->n != 1)
3792 return isl_bool_false;
3793 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
3794 if (n_div < 0)
3795 return isl_bool_error;
3796 if (n_div != 0)
3797 return isl_bool_false;
3798 return isl_bool_true;
3801 /* Check that "map" consists of a single disjunct, without any local variables.
3803 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
3805 isl_bool ok;
3807 ok = is_convex_no_locals(map);
3808 if (ok < 0)
3809 return isl_stat_error;
3810 if (ok)
3811 return isl_stat_ok;
3813 isl_die(isl_map_get_ctx(map), isl_error_internal,
3814 "unexpectedly not convex or involving local variables",
3815 return isl_stat_error);
3818 /* Special case of isl_map_intersect, where both map1 and map2
3819 * are convex, without any divs and such that either map1 or map2
3820 * contains a single constraint. This constraint is then simply
3821 * added to the other map.
3823 static __isl_give isl_map *map_intersect_add_constraint(
3824 __isl_take isl_map *map1, __isl_take isl_map *map2)
3826 if (check_convex_no_locals(map1) < 0 ||
3827 check_convex_no_locals(map2) < 0)
3828 goto error;
3830 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3831 return isl_map_intersect(map2, map1);
3833 map1 = isl_map_cow(map1);
3834 if (!map1)
3835 goto error;
3836 if (isl_map_plain_is_empty(map1)) {
3837 isl_map_free(map2);
3838 return map1;
3840 if (map2->p[0]->n_eq == 1)
3841 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3842 else
3843 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3844 map2->p[0]->ineq[0]);
3846 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3847 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3848 if (!map1->p[0])
3849 goto error;
3851 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3852 isl_basic_map_free(map1->p[0]);
3853 map1->n = 0;
3856 isl_map_free(map2);
3858 map1 = isl_map_unmark_normalized(map1);
3859 return map1;
3860 error:
3861 isl_map_free(map1);
3862 isl_map_free(map2);
3863 return NULL;
3866 /* map2 may be either a parameter domain or a map living in the same
3867 * space as map1.
3869 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3870 __isl_take isl_map *map2)
3872 unsigned flags = 0;
3873 isl_bool equal;
3874 isl_map *result;
3875 int i, j;
3876 isl_size dim2, nparam2;
3878 if (!map1 || !map2)
3879 goto error;
3881 if ((isl_map_plain_is_empty(map1) ||
3882 isl_map_plain_is_universe(map2)) &&
3883 isl_space_is_equal(map1->dim, map2->dim)) {
3884 isl_map_free(map2);
3885 return map1;
3887 if ((isl_map_plain_is_empty(map2) ||
3888 isl_map_plain_is_universe(map1)) &&
3889 isl_space_is_equal(map1->dim, map2->dim)) {
3890 isl_map_free(map1);
3891 return map2;
3894 if (is_convex_no_locals(map1) == isl_bool_true &&
3895 is_convex_no_locals(map2) == isl_bool_true &&
3896 isl_space_is_equal(map1->dim, map2->dim) &&
3897 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3898 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3899 return map_intersect_add_constraint(map1, map2);
3901 equal = isl_map_plain_is_equal(map1, map2);
3902 if (equal < 0)
3903 goto error;
3904 if (equal) {
3905 isl_map_free(map2);
3906 return map1;
3909 dim2 = isl_map_dim(map2, isl_dim_all);
3910 nparam2 = isl_map_dim(map2, isl_dim_param);
3911 if (dim2 < 0 || nparam2 < 0)
3912 goto error;
3913 if (dim2 != nparam2)
3914 isl_assert(map1->ctx,
3915 isl_space_is_equal(map1->dim, map2->dim), goto error);
3917 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3918 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3919 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3921 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3922 map1->n * map2->n, flags);
3923 if (!result)
3924 goto error;
3925 for (i = 0; i < map1->n; ++i)
3926 for (j = 0; j < map2->n; ++j) {
3927 struct isl_basic_map *part;
3928 part = isl_basic_map_intersect(
3929 isl_basic_map_copy(map1->p[i]),
3930 isl_basic_map_copy(map2->p[j]));
3931 if (isl_basic_map_is_empty(part) < 0)
3932 part = isl_basic_map_free(part);
3933 result = isl_map_add_basic_map(result, part);
3934 if (!result)
3935 goto error;
3937 isl_map_free(map1);
3938 isl_map_free(map2);
3939 return result;
3940 error:
3941 isl_map_free(map1);
3942 isl_map_free(map2);
3943 return NULL;
3946 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3947 __isl_take isl_map *map2)
3949 if (isl_map_check_equal_space(map1, map2) < 0)
3950 goto error;
3951 return map_intersect_internal(map1, map2);
3952 error:
3953 isl_map_free(map1);
3954 isl_map_free(map2);
3955 return NULL;
3958 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3959 __isl_take isl_map *map2)
3961 isl_map_align_params_bin(&map1, &map2);
3962 return map_intersect(map1, map2);
3965 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3966 __isl_take isl_set *set2)
3968 return set_from_map(isl_map_intersect(set_to_map(set1),
3969 set_to_map(set2)));
3972 /* map_intersect_internal accepts intersections
3973 * with parameter domains, so we can just call that function.
3975 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3976 __isl_take isl_set *params)
3978 isl_map_align_params_set(&map, &params);
3979 return map_intersect_internal(map, params);
3982 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3983 __isl_take isl_set *params)
3985 return isl_map_intersect_params(set, params);
3988 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3990 isl_space *space;
3991 unsigned pos;
3992 isl_size n1, n2;
3994 if (!bmap)
3995 return NULL;
3996 bmap = isl_basic_map_cow(bmap);
3997 if (!bmap)
3998 return NULL;
3999 space = isl_space_reverse(isl_space_copy(bmap->dim));
4000 pos = isl_basic_map_offset(bmap, isl_dim_in);
4001 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4002 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4003 if (n1 < 0 || n2 < 0)
4004 bmap = isl_basic_map_free(bmap);
4005 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4006 return isl_basic_map_reset_space(bmap, space);
4009 /* Given a basic map A -> (B -> C), return the corresponding basic map
4010 * A -> (C -> B).
4012 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4013 __isl_take isl_basic_map *bmap)
4015 isl_space *space;
4016 isl_size offset, n1, n2;
4018 space = isl_basic_map_peek_space(bmap);
4019 if (isl_space_check_range_is_wrapping(space) < 0)
4020 return isl_basic_map_free(bmap);
4021 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
4022 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
4023 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
4024 if (offset < 0 || n1 < 0 || n2 < 0)
4025 return isl_basic_map_free(bmap);
4027 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4029 space = isl_basic_map_take_space(bmap);
4030 space = isl_space_range_reverse(space);
4031 bmap = isl_basic_map_restore_space(bmap, space);
4033 return bmap;
4036 static __isl_give isl_basic_map *basic_map_space_reset(
4037 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4039 isl_space *space;
4041 if (!bmap)
4042 return NULL;
4043 if (!isl_space_is_named_or_nested(bmap->dim, type))
4044 return bmap;
4046 space = isl_basic_map_get_space(bmap);
4047 space = isl_space_reset(space, type);
4048 bmap = isl_basic_map_reset_space(bmap, space);
4049 return bmap;
4052 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4053 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4054 unsigned pos, unsigned n)
4056 isl_bool rational, is_empty;
4057 isl_space *res_space;
4058 struct isl_basic_map *res;
4059 struct isl_dim_map *dim_map;
4060 isl_size total;
4061 unsigned off;
4062 enum isl_dim_type t;
4064 if (n == 0)
4065 return basic_map_space_reset(bmap, type);
4067 is_empty = isl_basic_map_plain_is_empty(bmap);
4068 total = isl_basic_map_dim(bmap, isl_dim_all);
4069 if (is_empty < 0 || total < 0)
4070 return isl_basic_map_free(bmap);
4071 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4072 type, pos, n);
4073 if (!res_space)
4074 return isl_basic_map_free(bmap);
4075 if (is_empty) {
4076 isl_basic_map_free(bmap);
4077 return isl_basic_map_empty(res_space);
4080 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4081 off = 0;
4082 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4083 isl_size dim;
4085 if (t != type) {
4086 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4087 } else {
4088 isl_size size = isl_basic_map_dim(bmap, t);
4089 if (size < 0)
4090 dim_map = isl_dim_map_free(dim_map);
4091 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4092 0, pos, off);
4093 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4094 pos, size - pos, off + pos + n);
4096 dim = isl_space_dim(res_space, t);
4097 if (dim < 0)
4098 dim_map = isl_dim_map_free(dim_map);
4099 off += dim;
4101 isl_dim_map_div(dim_map, bmap, off);
4103 res = isl_basic_map_alloc_space(res_space,
4104 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4105 rational = isl_basic_map_is_rational(bmap);
4106 if (rational < 0)
4107 res = isl_basic_map_free(res);
4108 if (rational)
4109 res = isl_basic_map_set_rational(res);
4110 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4111 return isl_basic_map_finalize(res);
4114 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4115 __isl_take isl_basic_set *bset,
4116 enum isl_dim_type type, unsigned pos, unsigned n)
4118 return isl_basic_map_insert_dims(bset, type, pos, n);
4121 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4122 enum isl_dim_type type, unsigned n)
4124 isl_size dim;
4126 dim = isl_basic_map_dim(bmap, type);
4127 if (dim < 0)
4128 return isl_basic_map_free(bmap);
4129 return isl_basic_map_insert_dims(bmap, type, dim, n);
4132 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4133 enum isl_dim_type type, unsigned n)
4135 if (!bset)
4136 return NULL;
4137 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4138 return isl_basic_map_add_dims(bset, type, n);
4139 error:
4140 isl_basic_set_free(bset);
4141 return NULL;
4144 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4145 enum isl_dim_type type)
4147 isl_space *space;
4149 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4150 return map;
4152 space = isl_map_get_space(map);
4153 space = isl_space_reset(space, type);
4154 map = isl_map_reset_space(map, space);
4155 return map;
4158 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4159 enum isl_dim_type type, unsigned pos, unsigned n)
4161 int i;
4162 isl_space *space;
4164 if (n == 0)
4165 return map_space_reset(map, type);
4167 map = isl_map_cow(map);
4168 if (!map)
4169 return NULL;
4171 for (i = 0; i < map->n; ++i) {
4172 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4173 if (!map->p[i])
4174 goto error;
4177 space = isl_map_take_space(map);
4178 space = isl_space_insert_dims(space, type, pos, n);
4179 map = isl_map_restore_space(map, space);
4181 return map;
4182 error:
4183 isl_map_free(map);
4184 return NULL;
4187 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4188 enum isl_dim_type type, unsigned pos, unsigned n)
4190 return isl_map_insert_dims(set, type, pos, n);
4193 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4194 enum isl_dim_type type, unsigned n)
4196 isl_size dim;
4198 dim = isl_map_dim(map, type);
4199 if (dim < 0)
4200 return isl_map_free(map);
4201 return isl_map_insert_dims(map, type, dim, n);
4204 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4205 enum isl_dim_type type, unsigned n)
4207 if (!set)
4208 return NULL;
4209 isl_assert(set->ctx, type != isl_dim_in, goto error);
4210 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4211 error:
4212 isl_set_free(set);
4213 return NULL;
4216 __isl_give isl_basic_map *isl_basic_map_move_dims(
4217 __isl_take isl_basic_map *bmap,
4218 enum isl_dim_type dst_type, unsigned dst_pos,
4219 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4221 isl_space *space;
4222 struct isl_dim_map *dim_map;
4223 struct isl_basic_map *res;
4224 enum isl_dim_type t;
4225 isl_size total;
4226 unsigned off;
4228 if (!bmap)
4229 return NULL;
4230 if (n == 0) {
4231 bmap = isl_basic_map_reset(bmap, src_type);
4232 bmap = isl_basic_map_reset(bmap, dst_type);
4233 return bmap;
4236 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4237 return isl_basic_map_free(bmap);
4239 if (dst_type == src_type && dst_pos == src_pos)
4240 return bmap;
4242 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4244 if (pos(bmap->dim, dst_type) + dst_pos ==
4245 pos(bmap->dim, src_type) + src_pos +
4246 ((src_type < dst_type) ? n : 0)) {
4247 space = isl_basic_map_take_space(bmap);
4248 space = isl_space_move_dims(space, dst_type, dst_pos,
4249 src_type, src_pos, n);
4250 bmap = isl_basic_map_restore_space(bmap, space);
4251 bmap = isl_basic_map_finalize(bmap);
4253 return bmap;
4256 total = isl_basic_map_dim(bmap, isl_dim_all);
4257 if (total < 0)
4258 return isl_basic_map_free(bmap);
4259 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4261 off = 0;
4262 space = isl_basic_map_peek_space(bmap);
4263 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4264 isl_size size = isl_space_dim(space, t);
4265 if (size < 0)
4266 dim_map = isl_dim_map_free(dim_map);
4267 if (t == dst_type) {
4268 isl_dim_map_dim_range(dim_map, space, t,
4269 0, dst_pos, off);
4270 off += dst_pos;
4271 isl_dim_map_dim_range(dim_map, space, src_type,
4272 src_pos, n, off);
4273 off += n;
4274 isl_dim_map_dim_range(dim_map, space, t,
4275 dst_pos, size - dst_pos, off);
4276 off += size - dst_pos;
4277 } else if (t == src_type) {
4278 isl_dim_map_dim_range(dim_map, space, t,
4279 0, src_pos, off);
4280 off += src_pos;
4281 isl_dim_map_dim_range(dim_map, space, t,
4282 src_pos + n, size - src_pos - n, off);
4283 off += size - src_pos - n;
4284 } else {
4285 isl_dim_map_dim(dim_map, space, t, off);
4286 off += size;
4289 isl_dim_map_div(dim_map, bmap, off);
4291 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4292 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4293 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4294 space = isl_basic_map_take_space(bmap);
4295 space = isl_space_move_dims(space, dst_type, dst_pos,
4296 src_type, src_pos, n);
4297 bmap = isl_basic_map_restore_space(bmap, space);
4298 if (!bmap)
4299 goto error;
4301 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4302 bmap = isl_basic_map_gauss(bmap, NULL);
4303 bmap = isl_basic_map_finalize(bmap);
4305 return bmap;
4306 error:
4307 isl_basic_map_free(bmap);
4308 return NULL;
4311 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4312 enum isl_dim_type dst_type, unsigned dst_pos,
4313 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4315 isl_basic_map *bmap = bset_to_bmap(bset);
4316 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4317 src_type, src_pos, n);
4318 return bset_from_bmap(bmap);
4321 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4322 enum isl_dim_type dst_type, unsigned dst_pos,
4323 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4325 if (!set)
4326 return NULL;
4327 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4328 return set_from_map(isl_map_move_dims(set_to_map(set),
4329 dst_type, dst_pos, src_type, src_pos, n));
4330 error:
4331 isl_set_free(set);
4332 return NULL;
4335 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4336 enum isl_dim_type dst_type, unsigned dst_pos,
4337 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4339 int i;
4340 isl_space *space;
4342 if (n == 0) {
4343 map = isl_map_reset(map, src_type);
4344 map = isl_map_reset(map, dst_type);
4345 return map;
4348 if (isl_map_check_range(map, src_type, src_pos, n))
4349 return isl_map_free(map);
4351 if (dst_type == src_type && dst_pos == src_pos)
4352 return map;
4354 isl_assert(map->ctx, dst_type != src_type, goto error);
4356 map = isl_map_cow(map);
4357 if (!map)
4358 return NULL;
4360 for (i = 0; i < map->n; ++i) {
4361 map->p[i] = isl_basic_map_move_dims(map->p[i],
4362 dst_type, dst_pos,
4363 src_type, src_pos, n);
4364 if (!map->p[i])
4365 goto error;
4368 space = isl_map_take_space(map);
4369 space = isl_space_move_dims(space, dst_type, dst_pos,
4370 src_type, src_pos, n);
4371 map = isl_map_restore_space(map, space);
4373 return map;
4374 error:
4375 isl_map_free(map);
4376 return NULL;
4379 /* Move the specified dimensions to the last columns right before
4380 * the divs. Don't change the dimension specification of bmap.
4381 * That's the responsibility of the caller.
4383 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4384 enum isl_dim_type type, unsigned first, unsigned n)
4386 isl_space *space;
4387 struct isl_dim_map *dim_map;
4388 struct isl_basic_map *res;
4389 enum isl_dim_type t;
4390 isl_size total;
4391 unsigned off;
4393 if (!bmap)
4394 return NULL;
4395 if (isl_basic_map_offset(bmap, type) + first + n ==
4396 isl_basic_map_offset(bmap, isl_dim_div))
4397 return bmap;
4399 total = isl_basic_map_dim(bmap, isl_dim_all);
4400 if (total < 0)
4401 return isl_basic_map_free(bmap);
4402 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4404 off = 0;
4405 space = isl_basic_map_peek_space(bmap);
4406 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4407 isl_size size = isl_space_dim(space, t);
4408 if (size < 0)
4409 dim_map = isl_dim_map_free(dim_map);
4410 if (t == type) {
4411 isl_dim_map_dim_range(dim_map, space, t,
4412 0, first, off);
4413 off += first;
4414 isl_dim_map_dim_range(dim_map, space, t,
4415 first, n, total - bmap->n_div - n);
4416 isl_dim_map_dim_range(dim_map, space, t,
4417 first + n, size - (first + n), off);
4418 off += size - (first + n);
4419 } else {
4420 isl_dim_map_dim(dim_map, space, t, off);
4421 off += size;
4424 isl_dim_map_div(dim_map, bmap, off + n);
4426 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4427 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4428 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4429 return res;
4432 /* Insert "n" rows in the divs of "bmap".
4434 * The number of columns is not changed, which means that the last
4435 * dimensions of "bmap" are being reintepreted as the new divs.
4436 * The space of "bmap" is not adjusted, however, which means
4437 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4438 * from the space of "bmap" is the responsibility of the caller.
4440 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4441 int n)
4443 int i;
4444 size_t row_size;
4445 isl_int **new_div;
4446 isl_int *old;
4448 bmap = isl_basic_map_cow(bmap);
4449 if (!bmap)
4450 return NULL;
4452 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4453 old = bmap->block2.data;
4454 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4455 (bmap->extra + n) * (1 + row_size));
4456 if (!bmap->block2.data)
4457 return isl_basic_map_free(bmap);
4458 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4459 if (!new_div)
4460 return isl_basic_map_free(bmap);
4461 for (i = 0; i < n; ++i) {
4462 new_div[i] = bmap->block2.data +
4463 (bmap->extra + i) * (1 + row_size);
4464 isl_seq_clr(new_div[i], 1 + row_size);
4466 for (i = 0; i < bmap->extra; ++i)
4467 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4468 free(bmap->div);
4469 bmap->div = new_div;
4470 bmap->n_div += n;
4471 bmap->extra += n;
4473 return bmap;
4476 /* Drop constraints from "bmap" that only involve the variables
4477 * of "type" in the range [first, first + n] that are not related
4478 * to any of the variables outside that interval.
4479 * These constraints cannot influence the values for the variables
4480 * outside the interval, except in case they cause "bmap" to be empty.
4481 * Only drop the constraints if "bmap" is known to be non-empty.
4483 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4484 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4485 unsigned first, unsigned n)
4487 int i;
4488 int *groups;
4489 isl_size dim, n_div;
4490 isl_bool non_empty;
4492 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4493 if (non_empty < 0)
4494 return isl_basic_map_free(bmap);
4495 if (!non_empty)
4496 return bmap;
4498 dim = isl_basic_map_dim(bmap, isl_dim_all);
4499 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4500 if (dim < 0 || n_div < 0)
4501 return isl_basic_map_free(bmap);
4502 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4503 if (!groups)
4504 return isl_basic_map_free(bmap);
4505 first += isl_basic_map_offset(bmap, type) - 1;
4506 for (i = 0; i < first; ++i)
4507 groups[i] = -1;
4508 for (i = first + n; i < dim - n_div; ++i)
4509 groups[i] = -1;
4511 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4513 return bmap;
4516 /* Turn the n dimensions of type type, starting at first
4517 * into existentially quantified variables.
4519 * If a subset of the projected out variables are unrelated
4520 * to any of the variables that remain, then the constraints
4521 * involving this subset are simply dropped first.
4523 __isl_give isl_basic_map *isl_basic_map_project_out(
4524 __isl_take isl_basic_map *bmap,
4525 enum isl_dim_type type, unsigned first, unsigned n)
4527 isl_bool empty;
4528 isl_space *space;
4530 if (n == 0)
4531 return basic_map_space_reset(bmap, type);
4532 if (type == isl_dim_div)
4533 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4534 "cannot project out existentially quantified variables",
4535 return isl_basic_map_free(bmap));
4537 empty = isl_basic_map_plain_is_empty(bmap);
4538 if (empty < 0)
4539 return isl_basic_map_free(bmap);
4540 if (empty)
4541 bmap = isl_basic_map_set_to_empty(bmap);
4543 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4544 if (!bmap)
4545 return NULL;
4547 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4548 return isl_basic_map_remove_dims(bmap, type, first, n);
4550 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4551 return isl_basic_map_free(bmap);
4553 bmap = move_last(bmap, type, first, n);
4554 bmap = isl_basic_map_cow(bmap);
4555 bmap = insert_div_rows(bmap, n);
4557 space = isl_basic_map_take_space(bmap);
4558 space = isl_space_drop_dims(space, type, first, n);
4559 bmap = isl_basic_map_restore_space(bmap, space);
4560 bmap = isl_basic_map_simplify(bmap);
4561 bmap = isl_basic_map_drop_redundant_divs(bmap);
4562 return isl_basic_map_finalize(bmap);
4565 /* Turn the n dimensions of type type, starting at first
4566 * into existentially quantified variables.
4568 __isl_give isl_basic_set *isl_basic_set_project_out(
4569 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4570 unsigned first, unsigned n)
4572 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4573 type, first, n));
4576 /* Turn the n dimensions of type type, starting at first
4577 * into existentially quantified variables.
4579 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4580 enum isl_dim_type type, unsigned first, unsigned n)
4582 int i;
4583 isl_space *space;
4585 if (n == 0)
4586 return map_space_reset(map, type);
4588 if (isl_map_check_range(map, type, first, n) < 0)
4589 return isl_map_free(map);
4591 map = isl_map_cow(map);
4592 if (!map)
4593 return NULL;
4595 for (i = 0; i < map->n; ++i) {
4596 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4597 if (!map->p[i])
4598 goto error;
4601 if (map->n > 1)
4602 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4603 map = isl_map_unmark_normalized(map);
4605 space = isl_map_take_space(map);
4606 space = isl_space_drop_dims(space, type, first, n);
4607 map = isl_map_restore_space(map, space);
4609 return map;
4610 error:
4611 isl_map_free(map);
4612 return NULL;
4615 #undef TYPE
4616 #define TYPE isl_map
4617 #include "isl_project_out_all_params_templ.c"
4619 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4620 * into existentially quantified variables.
4622 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4623 enum isl_dim_type type, unsigned first, unsigned n)
4625 isl_size dim;
4627 dim = isl_map_dim(map, type);
4628 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4629 return isl_map_free(map);
4630 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4631 map = isl_map_project_out(map, type, 0, first);
4632 return map;
4635 /* Turn the n dimensions of type type, starting at first
4636 * into existentially quantified variables.
4638 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4639 enum isl_dim_type type, unsigned first, unsigned n)
4641 return set_from_map(isl_map_project_out(set_to_map(set),
4642 type, first, n));
4645 /* If "set" involves a parameter with identifier "id",
4646 * then turn it into an existentially quantified variable.
4648 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4649 __isl_take isl_id *id)
4651 int pos;
4653 if (!set || !id)
4654 goto error;
4655 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4656 isl_id_free(id);
4657 if (pos < 0)
4658 return set;
4659 return isl_set_project_out(set, isl_dim_param, pos, 1);
4660 error:
4661 isl_set_free(set);
4662 isl_id_free(id);
4663 return NULL;
4666 /* If "set" involves any of the parameters with identifiers in "list",
4667 * then turn them into existentially quantified variables.
4669 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4670 __isl_take isl_id_list *list)
4672 int i;
4673 isl_size n;
4675 n = isl_id_list_size(list);
4676 if (n < 0)
4677 goto error;
4678 for (i = 0; i < n; ++i) {
4679 isl_id *id;
4681 id = isl_id_list_get_at(list, i);
4682 set = isl_set_project_out_param_id(set, id);
4685 isl_id_list_free(list);
4686 return set;
4687 error:
4688 isl_id_list_free(list);
4689 isl_set_free(set);
4690 return NULL;
4693 /* Project out all parameters from "set" by existentially quantifying
4694 * over them.
4696 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4698 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4701 /* Return a map that projects the elements in "set" onto their
4702 * "n" set dimensions starting at "first".
4703 * "type" should be equal to isl_dim_set.
4705 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4706 enum isl_dim_type type, unsigned first, unsigned n)
4708 int i;
4709 isl_map *map;
4711 if (type != isl_dim_set)
4712 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4713 "only set dimensions can be projected out", goto error);
4714 if (isl_set_check_range(set, type, first, n) < 0)
4715 return isl_set_free(set);
4717 map = isl_map_from_domain(set);
4718 map = isl_map_add_dims(map, isl_dim_out, n);
4719 for (i = 0; i < n; ++i)
4720 map = isl_map_equate(map, isl_dim_in, first + i,
4721 isl_dim_out, i);
4722 return map;
4723 error:
4724 isl_set_free(set);
4725 return NULL;
4728 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4729 unsigned n)
4731 int i, j;
4732 isl_size total;
4734 total = isl_basic_map_dim(bmap, isl_dim_all);
4735 if (total < 0)
4736 return isl_basic_map_free(bmap);
4737 for (i = 0; i < n; ++i) {
4738 j = isl_basic_map_alloc_div(bmap);
4739 if (j < 0)
4740 goto error;
4741 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4743 return bmap;
4744 error:
4745 isl_basic_map_free(bmap);
4746 return NULL;
4749 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4751 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4752 __isl_keep isl_basic_map *bmap2)
4754 isl_space *space1, *space2;
4756 space1 = isl_basic_map_peek_space(bmap1);
4757 space2 = isl_basic_map_peek_space(bmap2);
4758 return isl_space_tuple_is_equal(space1, isl_dim_out,
4759 space2, isl_dim_in);
4762 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4764 static isl_stat isl_basic_map_check_applies_range(
4765 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4767 isl_bool equal;
4769 equal = isl_basic_map_applies_range(bmap1, bmap2);
4770 if (equal < 0)
4771 return isl_stat_error;
4772 if (!equal)
4773 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4774 "spaces don't match", return isl_stat_error);
4775 return isl_stat_ok;
4778 __isl_give isl_basic_map *isl_basic_map_apply_range(
4779 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4781 isl_space *space_result = NULL;
4782 struct isl_basic_map *bmap;
4783 isl_size n_in, n_out, n, nparam;
4784 unsigned total, pos;
4785 struct isl_dim_map *dim_map1, *dim_map2;
4787 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4788 goto error;
4789 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4790 goto error;
4792 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4793 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4794 n = isl_basic_map_dim(bmap1, isl_dim_out);
4795 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4796 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4797 goto error;
4799 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4800 isl_basic_map_get_space(bmap2));
4802 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4803 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4804 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4805 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4806 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4807 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4808 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4809 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4810 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4811 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4812 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4814 bmap = isl_basic_map_alloc_space(space_result,
4815 bmap1->n_div + bmap2->n_div + n,
4816 bmap1->n_eq + bmap2->n_eq,
4817 bmap1->n_ineq + bmap2->n_ineq);
4818 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4819 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4820 bmap = add_divs(bmap, n);
4821 bmap = isl_basic_map_simplify(bmap);
4822 bmap = isl_basic_map_drop_redundant_divs(bmap);
4823 return isl_basic_map_finalize(bmap);
4824 error:
4825 isl_basic_map_free(bmap1);
4826 isl_basic_map_free(bmap2);
4827 return NULL;
4830 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4831 __isl_take isl_basic_map *bmap)
4833 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4834 goto error;
4836 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4837 bmap));
4838 error:
4839 isl_basic_set_free(bset);
4840 isl_basic_map_free(bmap);
4841 return NULL;
4844 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4845 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4847 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4848 goto error;
4849 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4850 bmap2->dim, isl_dim_in))
4851 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4852 "spaces don't match", goto error);
4854 bmap1 = isl_basic_map_reverse(bmap1);
4855 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4856 return isl_basic_map_reverse(bmap1);
4857 error:
4858 isl_basic_map_free(bmap1);
4859 isl_basic_map_free(bmap2);
4860 return NULL;
4863 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4864 * A \cap B -> f(A) + f(B)
4866 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4867 __isl_take isl_basic_map *bmap2)
4869 isl_size n_in, n_out, nparam;
4870 unsigned total, pos;
4871 struct isl_basic_map *bmap = NULL;
4872 struct isl_dim_map *dim_map1, *dim_map2;
4873 int i;
4875 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4876 goto error;
4878 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4879 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4880 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4881 if (nparam < 0 || n_in < 0 || n_out < 0)
4882 goto error;
4884 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4885 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4886 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4887 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4888 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4889 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4890 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4891 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4892 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4896 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4897 bmap1->n_div + bmap2->n_div + 2 * n_out,
4898 bmap1->n_eq + bmap2->n_eq + n_out,
4899 bmap1->n_ineq + bmap2->n_ineq);
4900 for (i = 0; i < n_out; ++i) {
4901 int j = isl_basic_map_alloc_equality(bmap);
4902 if (j < 0)
4903 goto error;
4904 isl_seq_clr(bmap->eq[j], 1+total);
4905 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4906 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4907 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4909 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4910 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4911 bmap = add_divs(bmap, 2 * n_out);
4913 bmap = isl_basic_map_simplify(bmap);
4914 return isl_basic_map_finalize(bmap);
4915 error:
4916 isl_basic_map_free(bmap);
4917 isl_basic_map_free(bmap1);
4918 isl_basic_map_free(bmap2);
4919 return NULL;
4922 /* Given two maps A -> f(A) and B -> g(B), construct a map
4923 * A \cap B -> f(A) + f(B)
4925 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4926 __isl_take isl_map *map2)
4928 struct isl_map *result;
4929 int i, j;
4931 if (isl_map_check_equal_space(map1, map2) < 0)
4932 goto error;
4934 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4935 map1->n * map2->n, 0);
4936 if (!result)
4937 goto error;
4938 for (i = 0; i < map1->n; ++i)
4939 for (j = 0; j < map2->n; ++j) {
4940 struct isl_basic_map *part;
4941 part = isl_basic_map_sum(
4942 isl_basic_map_copy(map1->p[i]),
4943 isl_basic_map_copy(map2->p[j]));
4944 if (isl_basic_map_is_empty(part))
4945 isl_basic_map_free(part);
4946 else
4947 result = isl_map_add_basic_map(result, part);
4948 if (!result)
4949 goto error;
4951 isl_map_free(map1);
4952 isl_map_free(map2);
4953 return result;
4954 error:
4955 isl_map_free(map1);
4956 isl_map_free(map2);
4957 return NULL;
4960 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4961 __isl_take isl_set *set2)
4963 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4966 /* Given a basic map A -> f(A), construct A -> -f(A).
4968 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4970 int i, j;
4971 unsigned off;
4972 isl_size n;
4974 bmap = isl_basic_map_cow(bmap);
4975 n = isl_basic_map_dim(bmap, isl_dim_out);
4976 if (n < 0)
4977 return isl_basic_map_free(bmap);
4979 off = isl_basic_map_offset(bmap, isl_dim_out);
4980 for (i = 0; i < bmap->n_eq; ++i)
4981 for (j = 0; j < n; ++j)
4982 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4983 for (i = 0; i < bmap->n_ineq; ++i)
4984 for (j = 0; j < n; ++j)
4985 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4986 for (i = 0; i < bmap->n_div; ++i)
4987 for (j = 0; j < n; ++j)
4988 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4989 bmap = isl_basic_map_gauss(bmap, NULL);
4990 return isl_basic_map_finalize(bmap);
4993 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4995 return isl_basic_map_neg(bset);
4998 /* Given a map A -> f(A), construct A -> -f(A).
5000 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5002 int i;
5004 map = isl_map_cow(map);
5005 if (!map)
5006 return NULL;
5008 for (i = 0; i < map->n; ++i) {
5009 map->p[i] = isl_basic_map_neg(map->p[i]);
5010 if (!map->p[i])
5011 goto error;
5014 return map;
5015 error:
5016 isl_map_free(map);
5017 return NULL;
5020 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5022 return set_from_map(isl_map_neg(set_to_map(set)));
5025 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5026 * A -> floor(f(A)/d).
5028 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5029 isl_int d)
5031 isl_size n_in, n_out, nparam;
5032 unsigned total, pos;
5033 struct isl_basic_map *result = NULL;
5034 struct isl_dim_map *dim_map;
5035 int i;
5037 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5038 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5039 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5040 if (nparam < 0 || n_in < 0 || n_out < 0)
5041 return isl_basic_map_free(bmap);
5043 total = nparam + n_in + n_out + bmap->n_div + n_out;
5044 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5045 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5046 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5047 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5048 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5050 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5051 bmap->n_div + n_out,
5052 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5053 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5054 result = add_divs(result, n_out);
5055 for (i = 0; i < n_out; ++i) {
5056 int j;
5057 j = isl_basic_map_alloc_inequality(result);
5058 if (j < 0)
5059 goto error;
5060 isl_seq_clr(result->ineq[j], 1+total);
5061 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5062 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5063 j = isl_basic_map_alloc_inequality(result);
5064 if (j < 0)
5065 goto error;
5066 isl_seq_clr(result->ineq[j], 1+total);
5067 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5068 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5069 isl_int_sub_ui(result->ineq[j][0], d, 1);
5072 result = isl_basic_map_simplify(result);
5073 return isl_basic_map_finalize(result);
5074 error:
5075 isl_basic_map_free(result);
5076 return NULL;
5079 /* Given a map A -> f(A) and an integer d, construct a map
5080 * A -> floor(f(A)/d).
5082 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5084 int i;
5086 map = isl_map_cow(map);
5087 if (!map)
5088 return NULL;
5090 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5091 for (i = 0; i < map->n; ++i) {
5092 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5093 if (!map->p[i])
5094 goto error;
5096 map = isl_map_unmark_normalized(map);
5098 return map;
5099 error:
5100 isl_map_free(map);
5101 return NULL;
5104 /* Given a map A -> f(A) and an integer d, construct a map
5105 * A -> floor(f(A)/d).
5107 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5108 __isl_take isl_val *d)
5110 if (!map || !d)
5111 goto error;
5112 if (!isl_val_is_int(d))
5113 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5114 "expecting integer denominator", goto error);
5115 map = isl_map_floordiv(map, d->n);
5116 isl_val_free(d);
5117 return map;
5118 error:
5119 isl_map_free(map);
5120 isl_val_free(d);
5121 return NULL;
5124 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5125 unsigned pos)
5127 int i;
5128 isl_size nparam;
5129 isl_size n_in;
5130 isl_size total;
5132 total = isl_basic_map_dim(bmap, isl_dim_all);
5133 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5134 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5135 if (total < 0 || nparam < 0 || n_in < 0)
5136 return isl_basic_map_free(bmap);
5137 i = isl_basic_map_alloc_equality(bmap);
5138 if (i < 0)
5139 goto error;
5140 isl_seq_clr(bmap->eq[i], 1 + total);
5141 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5142 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5143 return isl_basic_map_finalize(bmap);
5144 error:
5145 isl_basic_map_free(bmap);
5146 return NULL;
5149 /* Add a constraint to "bmap" expressing i_pos < o_pos
5151 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5152 unsigned pos)
5154 int i;
5155 isl_size nparam;
5156 isl_size n_in;
5157 isl_size total;
5159 total = isl_basic_map_dim(bmap, isl_dim_all);
5160 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5161 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5162 if (total < 0 || nparam < 0 || n_in < 0)
5163 return isl_basic_map_free(bmap);
5164 i = isl_basic_map_alloc_inequality(bmap);
5165 if (i < 0)
5166 goto error;
5167 isl_seq_clr(bmap->ineq[i], 1 + total);
5168 isl_int_set_si(bmap->ineq[i][0], -1);
5169 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5170 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5171 return isl_basic_map_finalize(bmap);
5172 error:
5173 isl_basic_map_free(bmap);
5174 return NULL;
5177 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5179 static __isl_give isl_basic_map *var_less_or_equal(
5180 __isl_take isl_basic_map *bmap, unsigned pos)
5182 int i;
5183 isl_size nparam;
5184 isl_size n_in;
5185 isl_size total;
5187 total = isl_basic_map_dim(bmap, isl_dim_all);
5188 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5189 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5190 if (total < 0 || nparam < 0 || n_in < 0)
5191 return isl_basic_map_free(bmap);
5192 i = isl_basic_map_alloc_inequality(bmap);
5193 if (i < 0)
5194 goto error;
5195 isl_seq_clr(bmap->ineq[i], 1 + total);
5196 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5197 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5198 return isl_basic_map_finalize(bmap);
5199 error:
5200 isl_basic_map_free(bmap);
5201 return NULL;
5204 /* Add a constraint to "bmap" expressing i_pos > o_pos
5206 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5207 unsigned pos)
5209 int i;
5210 isl_size nparam;
5211 isl_size n_in;
5212 isl_size total;
5214 total = isl_basic_map_dim(bmap, isl_dim_all);
5215 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5216 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5217 if (total < 0 || nparam < 0 || n_in < 0)
5218 return isl_basic_map_free(bmap);
5219 i = isl_basic_map_alloc_inequality(bmap);
5220 if (i < 0)
5221 goto error;
5222 isl_seq_clr(bmap->ineq[i], 1 + total);
5223 isl_int_set_si(bmap->ineq[i][0], -1);
5224 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5225 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5226 return isl_basic_map_finalize(bmap);
5227 error:
5228 isl_basic_map_free(bmap);
5229 return NULL;
5232 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5234 static __isl_give isl_basic_map *var_more_or_equal(
5235 __isl_take isl_basic_map *bmap, unsigned pos)
5237 int i;
5238 isl_size nparam;
5239 isl_size n_in;
5240 isl_size total;
5242 total = isl_basic_map_dim(bmap, isl_dim_all);
5243 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5244 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5245 if (total < 0 || nparam < 0 || n_in < 0)
5246 return isl_basic_map_free(bmap);
5247 i = isl_basic_map_alloc_inequality(bmap);
5248 if (i < 0)
5249 goto error;
5250 isl_seq_clr(bmap->ineq[i], 1 + total);
5251 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5252 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5253 return isl_basic_map_finalize(bmap);
5254 error:
5255 isl_basic_map_free(bmap);
5256 return NULL;
5259 __isl_give isl_basic_map *isl_basic_map_equal(
5260 __isl_take isl_space *space, unsigned n_equal)
5262 int i;
5263 struct isl_basic_map *bmap;
5264 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5265 if (!bmap)
5266 return NULL;
5267 for (i = 0; i < n_equal && bmap; ++i)
5268 bmap = var_equal(bmap, i);
5269 return isl_basic_map_finalize(bmap);
5272 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5274 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5275 unsigned pos)
5277 int i;
5278 struct isl_basic_map *bmap;
5279 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5280 if (!bmap)
5281 return NULL;
5282 for (i = 0; i < pos && bmap; ++i)
5283 bmap = var_equal(bmap, i);
5284 if (bmap)
5285 bmap = var_less(bmap, pos);
5286 return isl_basic_map_finalize(bmap);
5289 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5291 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5292 __isl_take isl_space *space, unsigned pos)
5294 int i;
5295 isl_basic_map *bmap;
5297 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5298 for (i = 0; i < pos; ++i)
5299 bmap = var_equal(bmap, i);
5300 bmap = var_less_or_equal(bmap, pos);
5301 return isl_basic_map_finalize(bmap);
5304 /* Return a relation on "space" expressing i_pos > o_pos
5306 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5307 unsigned pos)
5309 int i;
5310 struct isl_basic_map *bmap;
5311 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5312 if (!bmap)
5313 return NULL;
5314 for (i = 0; i < pos && bmap; ++i)
5315 bmap = var_equal(bmap, i);
5316 if (bmap)
5317 bmap = var_more(bmap, pos);
5318 return isl_basic_map_finalize(bmap);
5321 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5323 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5324 __isl_take isl_space *space, unsigned pos)
5326 int i;
5327 isl_basic_map *bmap;
5329 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5330 for (i = 0; i < pos; ++i)
5331 bmap = var_equal(bmap, i);
5332 bmap = var_more_or_equal(bmap, pos);
5333 return isl_basic_map_finalize(bmap);
5336 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5337 unsigned n, int equal)
5339 struct isl_map *map;
5340 int i;
5342 if (n == 0 && equal)
5343 return isl_map_universe(space);
5345 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5347 for (i = 0; i + 1 < n; ++i)
5348 map = isl_map_add_basic_map(map,
5349 isl_basic_map_less_at(isl_space_copy(space), i));
5350 if (n > 0) {
5351 if (equal)
5352 map = isl_map_add_basic_map(map,
5353 isl_basic_map_less_or_equal_at(space, n - 1));
5354 else
5355 map = isl_map_add_basic_map(map,
5356 isl_basic_map_less_at(space, n - 1));
5357 } else
5358 isl_space_free(space);
5360 return map;
5363 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5365 if (!space)
5366 return NULL;
5367 return map_lex_lte_first(space, space->n_out, equal);
5370 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5371 unsigned n)
5373 return map_lex_lte_first(space, n, 0);
5376 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5377 unsigned n)
5379 return map_lex_lte_first(space, n, 1);
5382 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5384 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5387 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5389 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5392 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5393 unsigned n, int equal)
5395 struct isl_map *map;
5396 int i;
5398 if (n == 0 && equal)
5399 return isl_map_universe(space);
5401 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5403 for (i = 0; i + 1 < n; ++i)
5404 map = isl_map_add_basic_map(map,
5405 isl_basic_map_more_at(isl_space_copy(space), i));
5406 if (n > 0) {
5407 if (equal)
5408 map = isl_map_add_basic_map(map,
5409 isl_basic_map_more_or_equal_at(space, n - 1));
5410 else
5411 map = isl_map_add_basic_map(map,
5412 isl_basic_map_more_at(space, n - 1));
5413 } else
5414 isl_space_free(space);
5416 return map;
5419 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5421 if (!space)
5422 return NULL;
5423 return map_lex_gte_first(space, space->n_out, equal);
5426 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5427 unsigned n)
5429 return map_lex_gte_first(space, n, 0);
5432 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5433 unsigned n)
5435 return map_lex_gte_first(space, n, 1);
5438 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5440 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5443 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5445 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5448 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5449 __isl_take isl_set *set2)
5451 isl_map *map;
5452 map = isl_map_lex_le(isl_set_get_space(set1));
5453 map = isl_map_intersect_domain(map, set1);
5454 map = isl_map_intersect_range(map, set2);
5455 return map;
5458 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5459 __isl_take isl_set *set2)
5461 isl_map *map;
5462 map = isl_map_lex_lt(isl_set_get_space(set1));
5463 map = isl_map_intersect_domain(map, set1);
5464 map = isl_map_intersect_range(map, set2);
5465 return map;
5468 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5469 __isl_take isl_set *set2)
5471 isl_map *map;
5472 map = isl_map_lex_ge(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_gt_set(__isl_take isl_set *set1,
5479 __isl_take isl_set *set2)
5481 isl_map *map;
5482 map = isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map *map1,
5489 __isl_take isl_map *map2)
5491 isl_map *map;
5492 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5493 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5494 map = isl_map_apply_range(map, isl_map_reverse(map2));
5495 return map;
5498 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5499 __isl_take isl_map *map2)
5501 isl_map *map;
5502 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5503 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5504 map = isl_map_apply_range(map, isl_map_reverse(map2));
5505 return map;
5508 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5509 __isl_take isl_map *map2)
5511 isl_map *map;
5512 map = isl_map_lex_ge(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_gt_map(__isl_take isl_map *map1,
5519 __isl_take isl_map *map2)
5521 isl_map *map;
5522 map = isl_map_lex_gt(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 /* For the div d = floor(f/m) at position "div", add the constraint
5530 * f - m d >= 0
5532 static __isl_give isl_basic_map *add_upper_div_constraint(
5533 __isl_take isl_basic_map *bmap, unsigned div)
5535 int i;
5536 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5537 isl_size n_div;
5538 unsigned pos;
5540 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5541 if (v_div < 0 || n_div < 0)
5542 return isl_basic_map_free(bmap);
5543 pos = v_div + div;
5544 i = isl_basic_map_alloc_inequality(bmap);
5545 if (i < 0)
5546 return isl_basic_map_free(bmap);
5547 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5548 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5550 return bmap;
5553 /* For the div d = floor(f/m) at position "div", add the constraint
5555 * -(f-(m-1)) + m d >= 0
5557 static __isl_give isl_basic_map *add_lower_div_constraint(
5558 __isl_take isl_basic_map *bmap, unsigned div)
5560 int i;
5561 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5562 isl_size n_div;
5563 unsigned pos;
5565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5566 if (v_div < 0 || n_div < 0)
5567 return isl_basic_map_free(bmap);
5568 pos = v_div + div;
5569 i = isl_basic_map_alloc_inequality(bmap);
5570 if (i < 0)
5571 return isl_basic_map_free(bmap);
5572 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5573 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5574 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5575 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5577 return bmap;
5580 /* For the div d = floor(f/m) at position "pos", add the constraints
5582 * f - m d >= 0
5583 * -(f-(m-1)) + m d >= 0
5585 * Note that the second constraint is the negation of
5587 * f - m d >= m
5589 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5590 __isl_take isl_basic_map *bmap, unsigned pos)
5592 bmap = add_upper_div_constraint(bmap, pos);
5593 bmap = add_lower_div_constraint(bmap, pos);
5594 return bmap;
5597 /* For each known div d = floor(f/m), add the constraints
5599 * f - m d >= 0
5600 * -(f-(m-1)) + m d >= 0
5602 * Remove duplicate constraints in case of some these div constraints
5603 * already appear in "bmap".
5605 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5606 __isl_take isl_basic_map *bmap)
5608 isl_size n_div;
5610 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5611 if (n_div < 0)
5612 return isl_basic_map_free(bmap);
5613 if (n_div == 0)
5614 return bmap;
5616 bmap = add_known_div_constraints(bmap);
5617 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5618 bmap = isl_basic_map_finalize(bmap);
5619 return bmap;
5622 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5624 * In particular, if this div is of the form d = floor(f/m),
5625 * then add the constraint
5627 * f - m d >= 0
5629 * if sign < 0 or the constraint
5631 * -(f-(m-1)) + m d >= 0
5633 * if sign > 0.
5635 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5636 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5638 if (sign < 0)
5639 return add_upper_div_constraint(bmap, div);
5640 else
5641 return add_lower_div_constraint(bmap, div);
5644 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5645 __isl_take isl_basic_map *bmap)
5647 isl_space *space;
5649 if (!bmap)
5650 goto error;
5651 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5652 bmap->n_div == 0 &&
5653 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5654 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5655 return bset_from_bmap(bmap);
5656 bmap = isl_basic_map_cow(bmap);
5657 if (!bmap)
5658 return NULL;
5659 space = isl_basic_map_take_space(bmap);
5660 space = isl_space_underlying(space, bmap->n_div);
5661 bmap = isl_basic_map_restore_space(bmap, space);
5662 if (!bmap)
5663 return NULL;
5664 bmap->extra -= bmap->n_div;
5665 bmap->n_div = 0;
5666 bmap = isl_basic_map_finalize(bmap);
5667 return bset_from_bmap(bmap);
5668 error:
5669 isl_basic_map_free(bmap);
5670 return NULL;
5673 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5674 __isl_take isl_basic_set *bset)
5676 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5679 /* Replace each element in "list" by the result of applying
5680 * isl_basic_map_underlying_set to the element.
5682 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5683 __isl_take isl_basic_map_list *list)
5685 int i;
5686 isl_size n;
5688 n = isl_basic_map_list_n_basic_map(list);
5689 if (n < 0)
5690 goto error;
5692 for (i = 0; i < n; ++i) {
5693 isl_basic_map *bmap;
5694 isl_basic_set *bset;
5696 bmap = isl_basic_map_list_get_basic_map(list, i);
5697 bset = isl_basic_set_underlying_set(bmap);
5698 list = isl_basic_set_list_set_basic_set(list, i, bset);
5701 return list;
5702 error:
5703 isl_basic_map_list_free(list);
5704 return NULL;
5707 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5708 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5710 struct isl_basic_map *bmap;
5711 struct isl_ctx *ctx;
5712 isl_size dim, bmap_total;
5713 unsigned total;
5714 int i;
5716 if (!bset || !like)
5717 goto error;
5718 ctx = bset->ctx;
5719 if (isl_basic_set_check_no_params(bset) < 0 ||
5720 isl_basic_set_check_no_locals(bset) < 0)
5721 goto error;
5722 dim = isl_basic_set_dim(bset, isl_dim_set);
5723 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5724 if (dim < 0 || bmap_total < 0)
5725 goto error;
5726 isl_assert(ctx, dim == bmap_total, goto error);
5727 if (like->n_div == 0) {
5728 isl_space *space = isl_basic_map_get_space(like);
5729 isl_basic_map_free(like);
5730 return isl_basic_map_reset_space(bset, space);
5732 bset = isl_basic_set_cow(bset);
5733 if (!bset)
5734 goto error;
5735 total = dim + bset->extra;
5736 bmap = bset_to_bmap(bset);
5737 isl_space_free(isl_basic_map_take_space(bmap));
5738 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5739 if (!bmap)
5740 goto error;
5741 bmap->n_div = like->n_div;
5742 bmap->extra += like->n_div;
5743 if (bmap->extra) {
5744 unsigned ltotal;
5745 isl_int **div;
5746 ltotal = total - bmap->extra + like->extra;
5747 if (ltotal > total)
5748 ltotal = total;
5749 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5750 bmap->extra * (1 + 1 + total));
5751 if (isl_blk_is_error(bmap->block2))
5752 goto error;
5753 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5754 if (!div)
5755 goto error;
5756 bmap->div = div;
5757 for (i = 0; i < bmap->extra; ++i)
5758 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5759 for (i = 0; i < like->n_div; ++i) {
5760 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5761 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5763 bmap = isl_basic_map_add_known_div_constraints(bmap);
5765 isl_basic_map_free(like);
5766 bmap = isl_basic_map_simplify(bmap);
5767 bmap = isl_basic_map_finalize(bmap);
5768 return bmap;
5769 error:
5770 isl_basic_map_free(like);
5771 isl_basic_set_free(bset);
5772 return NULL;
5775 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5776 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5778 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5779 bset_to_bmap(like)));
5782 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5784 int i;
5786 map = isl_map_cow(map);
5787 if (!map)
5788 return NULL;
5789 map->dim = isl_space_cow(map->dim);
5790 if (!map->dim)
5791 goto error;
5793 for (i = 1; i < map->n; ++i)
5794 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5795 goto error);
5796 for (i = 0; i < map->n; ++i) {
5797 map->p[i] = bset_to_bmap(
5798 isl_basic_map_underlying_set(map->p[i]));
5799 if (!map->p[i])
5800 goto error;
5802 if (map->n == 0)
5803 map->dim = isl_space_underlying(map->dim, 0);
5804 else {
5805 isl_space_free(map->dim);
5806 map->dim = isl_space_copy(map->p[0]->dim);
5808 if (!map->dim)
5809 goto error;
5810 return set_from_map(map);
5811 error:
5812 isl_map_free(map);
5813 return NULL;
5816 /* Replace the space of "bmap" by "space".
5818 * If the space of "bmap" is identical to "space" (including the identifiers
5819 * of the input and output dimensions), then simply return the original input.
5821 __isl_give isl_basic_map *isl_basic_map_reset_space(
5822 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5824 isl_bool equal;
5825 isl_space *bmap_space;
5827 bmap_space = isl_basic_map_peek_space(bmap);
5828 equal = isl_space_is_equal(bmap_space, space);
5829 if (equal >= 0 && equal)
5830 equal = isl_space_has_equal_ids(bmap_space, space);
5831 if (equal < 0)
5832 goto error;
5833 if (equal) {
5834 isl_space_free(space);
5835 return bmap;
5837 isl_space_free(isl_basic_map_take_space(bmap));
5838 bmap = isl_basic_map_restore_space(bmap, space);
5840 bmap = isl_basic_map_finalize(bmap);
5842 return bmap;
5843 error:
5844 isl_basic_map_free(bmap);
5845 isl_space_free(space);
5846 return NULL;
5849 __isl_give isl_basic_set *isl_basic_set_reset_space(
5850 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5852 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5853 space));
5856 /* Check that the total dimensions of "map" and "space" are the same.
5858 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5859 __isl_keep isl_space *space)
5861 isl_size dim1, dim2;
5863 dim1 = isl_map_dim(map, isl_dim_all);
5864 dim2 = isl_space_dim(space, isl_dim_all);
5865 if (dim1 < 0 || dim2 < 0)
5866 return isl_stat_error;
5867 if (dim1 == dim2)
5868 return isl_stat_ok;
5869 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5870 "total dimensions do not match", return isl_stat_error);
5873 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5874 __isl_take isl_space *space)
5876 int i;
5878 map = isl_map_cow(map);
5879 if (!map || !space)
5880 goto error;
5882 for (i = 0; i < map->n; ++i) {
5883 map->p[i] = isl_basic_map_reset_space(map->p[i],
5884 isl_space_copy(space));
5885 if (!map->p[i])
5886 goto error;
5888 isl_space_free(isl_map_take_space(map));
5889 map = isl_map_restore_space(map, space);
5891 return map;
5892 error:
5893 isl_map_free(map);
5894 isl_space_free(space);
5895 return NULL;
5898 /* Replace the space of "map" by "space", without modifying
5899 * the dimension of "map".
5901 * If the space of "map" is identical to "space" (including the identifiers
5902 * of the input and output dimensions), then simply return the original input.
5904 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5905 __isl_take isl_space *space)
5907 isl_bool equal;
5908 isl_space *map_space;
5910 map_space = isl_map_peek_space(map);
5911 equal = isl_space_is_equal(map_space, space);
5912 if (equal >= 0 && equal)
5913 equal = isl_space_has_equal_ids(map_space, space);
5914 if (equal < 0)
5915 goto error;
5916 if (equal) {
5917 isl_space_free(space);
5918 return map;
5920 if (check_map_space_equal_total_dim(map, space) < 0)
5921 goto error;
5922 return isl_map_reset_space(map, space);
5923 error:
5924 isl_map_free(map);
5925 isl_space_free(space);
5926 return NULL;
5929 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5930 __isl_take isl_space *space)
5932 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5935 /* Compute the parameter domain of the given basic set.
5937 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5939 isl_bool is_params;
5940 isl_space *space;
5941 isl_size n;
5943 is_params = isl_basic_set_is_params(bset);
5944 if (is_params < 0)
5945 return isl_basic_set_free(bset);
5946 if (is_params)
5947 return bset;
5949 n = isl_basic_set_dim(bset, isl_dim_set);
5950 if (n < 0)
5951 return isl_basic_set_free(bset);
5952 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5953 space = isl_basic_set_get_space(bset);
5954 space = isl_space_params(space);
5955 bset = isl_basic_set_reset_space(bset, space);
5956 return bset;
5959 /* Construct a zero-dimensional basic set with the given parameter domain.
5961 __isl_give isl_basic_set *isl_basic_set_from_params(
5962 __isl_take isl_basic_set *bset)
5964 isl_space *space;
5965 space = isl_basic_set_get_space(bset);
5966 space = isl_space_set_from_params(space);
5967 bset = isl_basic_set_reset_space(bset, space);
5968 return bset;
5971 /* Compute the parameter domain of the given set.
5973 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5975 return isl_map_params(set_to_map(set));
5978 /* Construct a zero-dimensional set with the given parameter domain.
5980 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5982 isl_space *space;
5983 space = isl_set_get_space(set);
5984 space = isl_space_set_from_params(space);
5985 set = isl_set_reset_space(set, space);
5986 return set;
5989 /* Compute the parameter domain of the given map.
5991 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5993 isl_space *space;
5994 isl_size n_in, n_out;
5996 n_in = isl_map_dim(map, isl_dim_in);
5997 n_out = isl_map_dim(map, isl_dim_out);
5998 if (n_in < 0 || n_out < 0)
5999 return isl_map_free(map);
6000 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6001 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6002 space = isl_map_get_space(map);
6003 space = isl_space_params(space);
6004 map = isl_map_reset_space(map, space);
6005 return map;
6008 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6010 isl_space *space;
6011 isl_size n_out;
6013 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6014 if (n_out < 0)
6015 return isl_basic_map_free(bmap);
6016 space = isl_space_domain(isl_basic_map_get_space(bmap));
6018 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6020 return isl_basic_map_reset_space(bmap, space);
6023 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6025 if (!bmap)
6026 return isl_bool_error;
6027 return isl_space_may_be_set(bmap->dim);
6030 /* Is this basic map actually a set?
6031 * Users should never call this function. Outside of isl,
6032 * the type should indicate whether something is a set or a map.
6034 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6036 if (!bmap)
6037 return isl_bool_error;
6038 return isl_space_is_set(bmap->dim);
6041 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6043 isl_bool is_set;
6045 is_set = isl_basic_map_is_set(bmap);
6046 if (is_set < 0)
6047 goto error;
6048 if (is_set)
6049 return bmap;
6050 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6051 error:
6052 isl_basic_map_free(bmap);
6053 return NULL;
6056 __isl_give isl_basic_map *isl_basic_map_domain_map(
6057 __isl_take isl_basic_map *bmap)
6059 int i;
6060 isl_space *space;
6061 isl_basic_map *domain;
6062 isl_size nparam, n_in, n_out;
6064 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6065 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6066 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6067 if (nparam < 0 || n_in < 0 || n_out < 0)
6068 return isl_basic_map_free(bmap);
6070 space = isl_basic_map_get_space(bmap);
6071 space = isl_space_from_range(isl_space_domain(space));
6072 domain = isl_basic_map_universe(space);
6074 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6075 bmap = isl_basic_map_apply_range(bmap, domain);
6076 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6078 for (i = 0; i < n_in; ++i)
6079 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6080 isl_dim_out, i);
6082 bmap = isl_basic_map_gauss(bmap, NULL);
6083 return isl_basic_map_finalize(bmap);
6086 __isl_give isl_basic_map *isl_basic_map_range_map(
6087 __isl_take isl_basic_map *bmap)
6089 int i;
6090 isl_space *space;
6091 isl_basic_map *range;
6092 isl_size nparam, n_in, n_out;
6094 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6095 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6096 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6097 if (nparam < 0 || n_in < 0 || n_out < 0)
6098 return isl_basic_map_free(bmap);
6100 space = isl_basic_map_get_space(bmap);
6101 space = isl_space_from_range(isl_space_range(space));
6102 range = isl_basic_map_universe(space);
6104 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6105 bmap = isl_basic_map_apply_range(bmap, range);
6106 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6108 for (i = 0; i < n_out; ++i)
6109 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6110 isl_dim_out, i);
6112 bmap = isl_basic_map_gauss(bmap, NULL);
6113 return isl_basic_map_finalize(bmap);
6116 int isl_map_may_be_set(__isl_keep isl_map *map)
6118 if (!map)
6119 return -1;
6120 return isl_space_may_be_set(map->dim);
6123 /* Is this map actually a set?
6124 * Users should never call this function. Outside of isl,
6125 * the type should indicate whether something is a set or a map.
6127 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6129 if (!map)
6130 return isl_bool_error;
6131 return isl_space_is_set(map->dim);
6134 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6136 isl_space *space;
6137 isl_size n_in;
6139 n_in = isl_map_dim(map, isl_dim_in);
6140 if (n_in < 0)
6141 return set_from_map(isl_map_free(map));
6142 space = isl_space_range(isl_map_get_space(map));
6144 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6146 return set_from_map(isl_map_reset_space(map, space));
6149 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6150 * to each of its basic maps.
6152 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6153 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6154 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6156 int i;
6157 isl_space *space;
6159 map = isl_map_cow(map);
6160 if (!map)
6161 return NULL;
6163 for (i = 0; i < map->n; ++i) {
6164 map->p[i] = fn_bmap(map->p[i]);
6165 if (!map->p[i])
6166 return isl_map_free(map);
6168 map = isl_map_unmark_normalized(map);
6170 space = isl_map_take_space(map);
6171 space = fn_space(space);
6172 map = isl_map_restore_space(map, space);
6174 return map;
6177 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6179 return isl_map_transform(map, &isl_space_domain_map,
6180 &isl_basic_map_domain_map);
6183 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6185 return isl_map_transform(map, &isl_space_range_map,
6186 &isl_basic_map_range_map);
6189 /* Given a wrapped map of the form A[B -> C],
6190 * return the map A[B -> C] -> B.
6192 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6194 isl_id *id;
6195 isl_map *map;
6197 if (!set)
6198 return NULL;
6199 if (!isl_set_has_tuple_id(set))
6200 return isl_map_domain_map(isl_set_unwrap(set));
6202 id = isl_set_get_tuple_id(set);
6203 map = isl_map_domain_map(isl_set_unwrap(set));
6204 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6206 return map;
6209 __isl_give isl_basic_map *isl_basic_map_from_domain(
6210 __isl_take isl_basic_set *bset)
6212 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6215 __isl_give isl_basic_map *isl_basic_map_from_range(
6216 __isl_take isl_basic_set *bset)
6218 isl_space *space;
6219 space = isl_basic_set_get_space(bset);
6220 space = isl_space_from_range(space);
6221 bset = isl_basic_set_reset_space(bset, space);
6222 return bset_to_bmap(bset);
6225 /* Create a relation with the given set as range.
6226 * The domain of the created relation is a zero-dimensional
6227 * flat anonymous space.
6229 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6231 isl_space *space;
6232 space = isl_set_get_space(set);
6233 space = isl_space_from_range(space);
6234 set = isl_set_reset_space(set, space);
6235 return set_to_map(set);
6238 /* Create a relation with the given set as domain.
6239 * The range of the created relation is a zero-dimensional
6240 * flat anonymous space.
6242 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6244 return isl_map_reverse(isl_map_from_range(set));
6247 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6248 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6250 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6253 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6254 __isl_take isl_set *range)
6256 return isl_map_apply_range(isl_map_reverse(domain), range);
6259 /* Return a newly allocated isl_map with given space and flags and
6260 * room for "n" basic maps.
6261 * Make sure that all cached information is cleared.
6263 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6264 unsigned flags)
6266 struct isl_map *map;
6268 if (!space)
6269 return NULL;
6270 if (n < 0)
6271 isl_die(space->ctx, isl_error_internal,
6272 "negative number of basic maps", goto error);
6273 map = isl_calloc(space->ctx, struct isl_map,
6274 sizeof(struct isl_map) +
6275 (n - 1) * sizeof(struct isl_basic_map *));
6276 if (!map)
6277 goto error;
6279 map->ctx = space->ctx;
6280 isl_ctx_ref(map->ctx);
6281 map->ref = 1;
6282 map->size = n;
6283 map->n = 0;
6284 map->dim = space;
6285 map->flags = flags;
6286 return map;
6287 error:
6288 isl_space_free(space);
6289 return NULL;
6292 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6294 struct isl_basic_map *bmap;
6295 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6296 bmap = isl_basic_map_set_to_empty(bmap);
6297 return bmap;
6300 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6302 struct isl_basic_set *bset;
6303 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6304 bset = isl_basic_set_set_to_empty(bset);
6305 return bset;
6308 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6310 struct isl_basic_map *bmap;
6311 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6312 bmap = isl_basic_map_finalize(bmap);
6313 return bmap;
6316 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6318 struct isl_basic_set *bset;
6319 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6320 bset = isl_basic_set_finalize(bset);
6321 return bset;
6324 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6325 __isl_take isl_space *space)
6327 int i;
6328 isl_size total = isl_space_dim(space, isl_dim_all);
6329 isl_basic_map *bmap;
6331 if (total < 0)
6332 space = isl_space_free(space);
6333 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6334 for (i = 0; i < total; ++i) {
6335 int k = isl_basic_map_alloc_inequality(bmap);
6336 if (k < 0)
6337 goto error;
6338 isl_seq_clr(bmap->ineq[k], 1 + total);
6339 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6341 return bmap;
6342 error:
6343 isl_basic_map_free(bmap);
6344 return NULL;
6347 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6348 __isl_take isl_space *space)
6350 return isl_basic_map_nat_universe(space);
6353 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6355 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6358 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6360 return isl_map_nat_universe(space);
6363 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6365 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6368 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6370 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6373 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6375 struct isl_map *map;
6376 if (!space)
6377 return NULL;
6378 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6379 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6380 return map;
6383 /* This function performs the same operation as isl_map_universe,
6384 * but is considered as a function on an isl_space when exported.
6386 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6388 return isl_map_universe(space);
6391 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6393 struct isl_set *set;
6394 if (!space)
6395 return NULL;
6396 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6397 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6398 return set;
6401 /* This function performs the same operation as isl_set_universe,
6402 * but is considered as a function on an isl_space when exported.
6404 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6406 return isl_set_universe(space);
6409 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6411 int i;
6412 struct isl_map *dup;
6414 if (!map)
6415 return NULL;
6416 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6417 for (i = 0; i < map->n; ++i)
6418 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6419 return dup;
6422 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6423 __isl_take isl_basic_map *bmap)
6425 if (!bmap || !map)
6426 goto error;
6427 if (isl_basic_map_plain_is_empty(bmap)) {
6428 isl_basic_map_free(bmap);
6429 return map;
6431 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6432 goto error;
6433 isl_assert(map->ctx, map->n < map->size, goto error);
6434 map->p[map->n] = bmap;
6435 map->n++;
6436 map = isl_map_unmark_normalized(map);
6437 return map;
6438 error:
6439 if (map)
6440 isl_map_free(map);
6441 if (bmap)
6442 isl_basic_map_free(bmap);
6443 return NULL;
6446 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6448 int i;
6450 if (!map)
6451 return NULL;
6453 if (--map->ref > 0)
6454 return NULL;
6456 clear_caches(map);
6457 isl_ctx_deref(map->ctx);
6458 for (i = 0; i < map->n; ++i)
6459 isl_basic_map_free(map->p[i]);
6460 isl_space_free(map->dim);
6461 free(map);
6463 return NULL;
6466 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6467 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6469 int j;
6470 isl_size total;
6472 total = isl_basic_map_dim(bmap, isl_dim_all);
6473 if (total < 0)
6474 return isl_basic_map_free(bmap);
6476 bmap = isl_basic_map_cow(bmap);
6477 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6478 j = isl_basic_map_alloc_equality(bmap);
6479 if (j < 0)
6480 goto error;
6481 isl_seq_clr(bmap->eq[j] + 1, total);
6482 isl_int_set_si(bmap->eq[j][pos], -1);
6483 isl_int_set_si(bmap->eq[j][0], value);
6484 bmap = isl_basic_map_simplify(bmap);
6485 return isl_basic_map_finalize(bmap);
6486 error:
6487 isl_basic_map_free(bmap);
6488 return NULL;
6491 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6492 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6494 int j;
6495 isl_size total;
6497 total = isl_basic_map_dim(bmap, isl_dim_all);
6498 if (total < 0)
6499 return isl_basic_map_free(bmap);
6501 bmap = isl_basic_map_cow(bmap);
6502 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6503 j = isl_basic_map_alloc_equality(bmap);
6504 if (j < 0)
6505 goto error;
6506 isl_seq_clr(bmap->eq[j] + 1, total);
6507 isl_int_set_si(bmap->eq[j][pos], -1);
6508 isl_int_set(bmap->eq[j][0], value);
6509 bmap = isl_basic_map_simplify(bmap);
6510 return isl_basic_map_finalize(bmap);
6511 error:
6512 isl_basic_map_free(bmap);
6513 return NULL;
6516 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6517 enum isl_dim_type type, unsigned pos, int value)
6519 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6520 return isl_basic_map_free(bmap);
6521 return isl_basic_map_fix_pos_si(bmap,
6522 isl_basic_map_offset(bmap, type) + pos, value);
6525 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6526 enum isl_dim_type type, unsigned pos, isl_int value)
6528 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6529 return isl_basic_map_free(bmap);
6530 return isl_basic_map_fix_pos(bmap,
6531 isl_basic_map_offset(bmap, type) + pos, value);
6534 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6535 * to be equal to "v".
6537 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6538 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6540 if (!bmap || !v)
6541 goto error;
6542 if (!isl_val_is_int(v))
6543 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6544 "expecting integer value", goto error);
6545 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6546 goto error;
6547 pos += isl_basic_map_offset(bmap, type);
6548 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6549 isl_val_free(v);
6550 return bmap;
6551 error:
6552 isl_basic_map_free(bmap);
6553 isl_val_free(v);
6554 return NULL;
6557 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6558 * to be equal to "v".
6560 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6561 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6563 return isl_basic_map_fix_val(bset, type, pos, v);
6566 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6567 enum isl_dim_type type, unsigned pos, int value)
6569 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6570 type, pos, value));
6573 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6574 enum isl_dim_type type, unsigned pos, isl_int value)
6576 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6577 type, pos, value));
6580 /* Remove the basic map at position "i" from "map" if this basic map
6581 * is (obviously) empty.
6583 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6585 isl_bool empty;
6587 if (!map)
6588 return NULL;
6590 empty = isl_basic_map_plain_is_empty(map->p[i]);
6591 if (empty < 0)
6592 return isl_map_free(map);
6593 if (!empty)
6594 return map;
6596 isl_basic_map_free(map->p[i]);
6597 map->n--;
6598 if (i != map->n) {
6599 map->p[i] = map->p[map->n];
6600 map = isl_map_unmark_normalized(map);
6604 return map;
6607 /* Perform "fn" on each basic map of "map", where we may not be holding
6608 * the only reference to "map".
6609 * In particular, "fn" should be a semantics preserving operation
6610 * that we want to apply to all copies of "map". We therefore need
6611 * to be careful not to modify "map" in a way that breaks "map"
6612 * in case anything goes wrong.
6614 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6615 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6617 struct isl_basic_map *bmap;
6618 int i;
6620 if (!map)
6621 return NULL;
6623 for (i = map->n - 1; i >= 0; --i) {
6624 bmap = isl_basic_map_copy(map->p[i]);
6625 bmap = fn(bmap);
6626 if (!bmap)
6627 goto error;
6628 isl_basic_map_free(map->p[i]);
6629 map->p[i] = bmap;
6630 map = remove_if_empty(map, i);
6631 if (!map)
6632 return NULL;
6635 return map;
6636 error:
6637 isl_map_free(map);
6638 return NULL;
6641 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6642 enum isl_dim_type type, unsigned pos, int value)
6644 int i;
6646 map = isl_map_cow(map);
6647 if (isl_map_check_range(map, type, pos, 1) < 0)
6648 return isl_map_free(map);
6649 for (i = map->n - 1; i >= 0; --i) {
6650 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6651 map = remove_if_empty(map, i);
6652 if (!map)
6653 return NULL;
6655 map = isl_map_unmark_normalized(map);
6656 return map;
6659 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6660 enum isl_dim_type type, unsigned pos, int value)
6662 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6665 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6666 enum isl_dim_type type, unsigned pos, isl_int value)
6668 int i;
6670 map = isl_map_cow(map);
6671 if (isl_map_check_range(map, type, pos, 1) < 0)
6672 return isl_map_free(map);
6673 for (i = 0; i < map->n; ++i) {
6674 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6675 if (!map->p[i])
6676 goto error;
6678 map = isl_map_unmark_normalized(map);
6679 return map;
6680 error:
6681 isl_map_free(map);
6682 return NULL;
6685 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6686 enum isl_dim_type type, unsigned pos, isl_int value)
6688 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6691 /* Fix the value of the variable at position "pos" of type "type" of "map"
6692 * to be equal to "v".
6694 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6695 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6697 int i;
6699 map = isl_map_cow(map);
6700 if (!map || !v)
6701 goto error;
6703 if (!isl_val_is_int(v))
6704 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6705 "expecting integer value", goto error);
6706 if (isl_map_check_range(map, type, pos, 1) < 0)
6707 goto error;
6708 for (i = map->n - 1; i >= 0; --i) {
6709 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6710 isl_val_copy(v));
6711 map = remove_if_empty(map, i);
6712 if (!map)
6713 goto error;
6715 map = isl_map_unmark_normalized(map);
6716 isl_val_free(v);
6717 return map;
6718 error:
6719 isl_map_free(map);
6720 isl_val_free(v);
6721 return NULL;
6724 /* Fix the value of the variable at position "pos" of type "type" of "set"
6725 * to be equal to "v".
6727 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6728 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6730 return isl_map_fix_val(set, type, pos, v);
6733 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6734 unsigned input, int value)
6736 return isl_map_fix_si(map, isl_dim_in, input, value);
6739 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6740 int value)
6742 return set_from_map(isl_map_fix_si(set_to_map(set),
6743 isl_dim_set, dim, value));
6746 static __isl_give isl_basic_map *basic_map_bound_si(
6747 __isl_take isl_basic_map *bmap,
6748 enum isl_dim_type type, unsigned pos, int value, int upper)
6750 int j;
6751 isl_size total;
6753 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6754 return isl_basic_map_free(bmap);
6755 total = isl_basic_map_dim(bmap, isl_dim_all);
6756 if (total < 0)
6757 return isl_basic_map_free(bmap);
6758 pos += isl_basic_map_offset(bmap, type);
6759 bmap = isl_basic_map_cow(bmap);
6760 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6761 j = isl_basic_map_alloc_inequality(bmap);
6762 if (j < 0)
6763 goto error;
6764 isl_seq_clr(bmap->ineq[j], 1 + total);
6765 if (upper) {
6766 isl_int_set_si(bmap->ineq[j][pos], -1);
6767 isl_int_set_si(bmap->ineq[j][0], value);
6768 } else {
6769 isl_int_set_si(bmap->ineq[j][pos], 1);
6770 isl_int_set_si(bmap->ineq[j][0], -value);
6772 bmap = isl_basic_map_simplify(bmap);
6773 return isl_basic_map_finalize(bmap);
6774 error:
6775 isl_basic_map_free(bmap);
6776 return NULL;
6779 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6780 __isl_take isl_basic_map *bmap,
6781 enum isl_dim_type type, unsigned pos, int value)
6783 return basic_map_bound_si(bmap, type, pos, value, 0);
6786 /* Constrain the values of the given dimension to be no greater than "value".
6788 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6789 __isl_take isl_basic_map *bmap,
6790 enum isl_dim_type type, unsigned pos, int value)
6792 return basic_map_bound_si(bmap, type, pos, value, 1);
6795 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6796 enum isl_dim_type type, unsigned pos, int value, int upper)
6798 int i;
6800 map = isl_map_cow(map);
6801 if (isl_map_check_range(map, type, pos, 1) < 0)
6802 return isl_map_free(map);
6803 for (i = 0; i < map->n; ++i) {
6804 map->p[i] = basic_map_bound_si(map->p[i],
6805 type, pos, value, upper);
6806 if (!map->p[i])
6807 goto error;
6809 map = isl_map_unmark_normalized(map);
6810 return map;
6811 error:
6812 isl_map_free(map);
6813 return NULL;
6816 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6817 enum isl_dim_type type, unsigned pos, int value)
6819 return map_bound_si(map, type, pos, value, 0);
6822 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6823 enum isl_dim_type type, unsigned pos, int value)
6825 return map_bound_si(map, type, pos, value, 1);
6828 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6829 enum isl_dim_type type, unsigned pos, int value)
6831 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6832 type, pos, value));
6835 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6836 enum isl_dim_type type, unsigned pos, int value)
6838 return isl_map_upper_bound_si(set, type, pos, value);
6841 /* Bound the given variable of "bmap" from below (or above is "upper"
6842 * is set) to "value".
6844 static __isl_give isl_basic_map *basic_map_bound(
6845 __isl_take isl_basic_map *bmap,
6846 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6848 int j;
6849 isl_size total;
6851 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6852 return isl_basic_map_free(bmap);
6853 total = isl_basic_map_dim(bmap, isl_dim_all);
6854 if (total < 0)
6855 return isl_basic_map_free(bmap);
6856 pos += isl_basic_map_offset(bmap, type);
6857 bmap = isl_basic_map_cow(bmap);
6858 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6859 j = isl_basic_map_alloc_inequality(bmap);
6860 if (j < 0)
6861 goto error;
6862 isl_seq_clr(bmap->ineq[j], 1 + total);
6863 if (upper) {
6864 isl_int_set_si(bmap->ineq[j][pos], -1);
6865 isl_int_set(bmap->ineq[j][0], value);
6866 } else {
6867 isl_int_set_si(bmap->ineq[j][pos], 1);
6868 isl_int_neg(bmap->ineq[j][0], value);
6870 bmap = isl_basic_map_simplify(bmap);
6871 return isl_basic_map_finalize(bmap);
6872 error:
6873 isl_basic_map_free(bmap);
6874 return NULL;
6877 /* Bound the given variable of "map" from below (or above is "upper"
6878 * is set) to "value".
6880 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6881 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6883 int i;
6885 map = isl_map_cow(map);
6886 if (isl_map_check_range(map, type, pos, 1) < 0)
6887 return isl_map_free(map);
6888 for (i = map->n - 1; i >= 0; --i) {
6889 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6890 map = remove_if_empty(map, i);
6891 if (!map)
6892 return NULL;
6894 map = isl_map_unmark_normalized(map);
6895 return map;
6898 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6899 enum isl_dim_type type, unsigned pos, isl_int value)
6901 return map_bound(map, type, pos, value, 0);
6904 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6905 enum isl_dim_type type, unsigned pos, isl_int value)
6907 return map_bound(map, type, pos, value, 1);
6910 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6911 enum isl_dim_type type, unsigned pos, isl_int value)
6913 return isl_map_lower_bound(set, type, pos, value);
6916 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6917 enum isl_dim_type type, unsigned pos, isl_int value)
6919 return isl_map_upper_bound(set, type, pos, value);
6922 /* Force the values of the variable at position "pos" of type "type" of "map"
6923 * to be no smaller than "value".
6925 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6926 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6928 if (!value)
6929 goto error;
6930 if (!isl_val_is_int(value))
6931 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6932 "expecting integer value", goto error);
6933 map = isl_map_lower_bound(map, type, pos, value->n);
6934 isl_val_free(value);
6935 return map;
6936 error:
6937 isl_val_free(value);
6938 isl_map_free(map);
6939 return NULL;
6942 /* Force the values of the variable at position "pos" of type "type" of "set"
6943 * to be no smaller than "value".
6945 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6946 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6948 isl_map *map;
6950 map = set_to_map(set);
6951 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6954 /* Force the values of the variable at position "pos" of type "type" of "map"
6955 * to be no greater than "value".
6957 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6958 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6960 if (!value)
6961 goto error;
6962 if (!isl_val_is_int(value))
6963 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6964 "expecting integer value", goto error);
6965 map = isl_map_upper_bound(map, type, pos, value->n);
6966 isl_val_free(value);
6967 return map;
6968 error:
6969 isl_val_free(value);
6970 isl_map_free(map);
6971 return NULL;
6974 /* Force the values of the variable at position "pos" of type "type" of "set"
6975 * to be no greater than "value".
6977 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6978 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6980 isl_map *map;
6982 map = set_to_map(set);
6983 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6986 /* If "mv" has an explicit domain, then intersect the domain of "map"
6987 * with this explicit domain.
6989 * An isl_multi_val object never has an explicit domain,
6990 * so simply return "map".
6992 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
6993 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
6995 return map;
6998 #undef BASE
6999 #define BASE val
7000 #include "isl_map_bound_templ.c"
7002 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7003 * for each set dimension, by treating the set as a map.
7005 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7006 __isl_take isl_multi_val *bound,
7007 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7008 unsigned pos, __isl_take isl_val *value))
7010 isl_map *map;
7012 map = set_to_map(set);
7013 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7016 #undef BASE
7017 #define BASE pw_aff
7018 #include "isl_map_bound_templ.c"
7020 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7021 * for each set dimension, by converting the set and the bound
7022 * to objects living in a map space.
7024 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7025 __isl_take isl_multi_pw_aff *bound,
7026 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7027 unsigned pos, __isl_take TYPE *value))
7029 isl_map *map;
7031 map = isl_map_from_range(set);
7032 bound = isl_multi_pw_aff_from_range(bound);
7033 map = map_bound_multi_pw_aff(map, bound, set_bound);
7034 return isl_map_range(map);
7037 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7038 * setting a bound on the given output dimension.
7040 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7041 unsigned pos, __isl_take isl_val *v)
7043 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7046 /* Force the values of the set dimensions of "set"
7047 * to be no smaller than the corresponding values in "lower".
7049 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7050 __isl_take isl_multi_val *lower)
7052 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7055 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7056 * setting a bound on the given output dimension.
7058 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7059 unsigned pos, __isl_take isl_val *v)
7061 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7064 /* Force the values of the set dimensions of "set"
7065 * to be no greater than the corresponding values in "upper".
7067 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7068 __isl_take isl_multi_val *upper)
7070 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7073 /* Force the symbolic constant expression "bound"
7074 * to satisfy the relation "order" with respect to
7075 * the output variable at position "pos" of "map".
7077 * Create an affine expression representing the output variable
7078 * in terms of the range and
7079 * compare it using "order" to "bound" (defined on the domain).
7080 * The result is a relation between elements in domain and range that
7081 * can be intersected with "map".
7083 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7084 unsigned pos, __isl_take isl_pw_aff *bound,
7085 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7086 __isl_take isl_pw_aff *pa2))
7088 isl_space *space;
7089 isl_local_space *ls;
7090 isl_pw_aff *var;
7092 space = isl_space_range(isl_map_get_space(map));
7093 ls = isl_local_space_from_space(space);
7094 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7095 map = isl_map_intersect(map, order(bound, var));
7096 return map;
7099 /* Force the values of the output variable at position "pos" of "map"
7100 * to be no smaller than the symbolic constant expression "lower".
7102 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7103 unsigned pos, __isl_take isl_pw_aff *lower)
7105 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7108 /* Force the values of the output variable at position "pos" of "map"
7109 * to be no greater than the symbolic constant expression "upper".
7111 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7112 unsigned pos, __isl_take isl_pw_aff *upper)
7114 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7117 /* Force the values of the set dimensions of "set"
7118 * to be no smaller than the corresponding constant symbolic expressions
7119 * in "lower".
7121 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7122 __isl_take isl_multi_pw_aff *lower)
7124 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7127 /* Force the values of the set dimensions of "set"
7128 * to be no greater than the corresponding constant symbolic expressions
7129 * in "upper".
7131 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7132 __isl_take isl_multi_pw_aff *upper)
7134 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7137 /* Force the values of the output dimensions of "map"
7138 * to be no smaller than the corresponding constant symbolic expressions
7139 * in "lower".
7141 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7142 __isl_take isl_multi_pw_aff *lower)
7144 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7147 /* Force the values of the output dimensions of "map"
7148 * to be no greater than the corresponding constant symbolic expressions
7149 * in "upper".
7151 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7152 __isl_take isl_multi_pw_aff *upper)
7154 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7157 /* Bound the given variable of "bset" from below (or above is "upper"
7158 * is set) to "value".
7160 static __isl_give isl_basic_set *isl_basic_set_bound(
7161 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7162 isl_int value, int upper)
7164 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7165 type, pos, value, upper));
7168 /* Bound the given variable of "bset" from below (or above is "upper"
7169 * is set) to "value".
7171 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7172 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7173 __isl_take isl_val *value, int upper)
7175 if (!value)
7176 goto error;
7177 if (!isl_val_is_int(value))
7178 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7179 "expecting integer value", goto error);
7180 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7181 isl_val_free(value);
7182 return bset;
7183 error:
7184 isl_val_free(value);
7185 isl_basic_set_free(bset);
7186 return NULL;
7189 /* Bound the given variable of "bset" from below to "value".
7191 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7192 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7193 __isl_take isl_val *value)
7195 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7198 /* Bound the given variable of "bset" from above to "value".
7200 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7201 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7202 __isl_take isl_val *value)
7204 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7207 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7209 return isl_map_transform(map, &isl_space_reverse,
7210 &isl_basic_map_reverse);
7213 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7215 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7217 return isl_map_transform(map, &isl_space_range_reverse,
7218 &isl_basic_map_range_reverse);
7221 #undef TYPE
7222 #define TYPE isl_pw_multi_aff
7223 #undef SUFFIX
7224 #define SUFFIX _pw_multi_aff
7225 #undef EMPTY
7226 #define EMPTY isl_pw_multi_aff_empty
7227 #undef ADD
7228 #define ADD isl_pw_multi_aff_union_add
7229 #include "isl_map_lexopt_templ.c"
7231 /* Given a map "map", compute the lexicographically minimal
7232 * (or maximal) image element for each domain element in dom,
7233 * in the form of an isl_pw_multi_aff.
7234 * If "empty" is not NULL, then set *empty to those elements in dom that
7235 * do not have an image element.
7236 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7237 * should be computed over the domain of "map". "empty" is also NULL
7238 * in this case.
7240 * We first compute the lexicographically minimal or maximal element
7241 * in the first basic map. This results in a partial solution "res"
7242 * and a subset "todo" of dom that still need to be handled.
7243 * We then consider each of the remaining maps in "map" and successively
7244 * update both "res" and "todo".
7245 * If "empty" is NULL, then the todo sets are not needed and therefore
7246 * also not computed.
7248 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7249 __isl_take isl_map *map, __isl_take isl_set *dom,
7250 __isl_give isl_set **empty, unsigned flags)
7252 int i;
7253 int full;
7254 isl_pw_multi_aff *res;
7255 isl_set *todo;
7257 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7258 if (!map || (!full && !dom))
7259 goto error;
7261 if (isl_map_plain_is_empty(map)) {
7262 if (empty)
7263 *empty = dom;
7264 else
7265 isl_set_free(dom);
7266 return isl_pw_multi_aff_from_map(map);
7269 res = basic_map_partial_lexopt_pw_multi_aff(
7270 isl_basic_map_copy(map->p[0]),
7271 isl_set_copy(dom), empty, flags);
7273 if (empty)
7274 todo = *empty;
7275 for (i = 1; i < map->n; ++i) {
7276 isl_pw_multi_aff *res_i;
7278 res_i = basic_map_partial_lexopt_pw_multi_aff(
7279 isl_basic_map_copy(map->p[i]),
7280 isl_set_copy(dom), empty, flags);
7282 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7283 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7284 else
7285 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7287 if (empty)
7288 todo = isl_set_intersect(todo, *empty);
7291 isl_set_free(dom);
7292 isl_map_free(map);
7294 if (empty)
7295 *empty = todo;
7297 return res;
7298 error:
7299 if (empty)
7300 *empty = NULL;
7301 isl_set_free(dom);
7302 isl_map_free(map);
7303 return NULL;
7306 #undef TYPE
7307 #define TYPE isl_map
7308 #undef SUFFIX
7309 #define SUFFIX
7310 #undef EMPTY
7311 #define EMPTY isl_map_empty
7312 #undef ADD
7313 #define ADD isl_map_union_disjoint
7314 #include "isl_map_lexopt_templ.c"
7316 /* Given a map "map", compute the lexicographically minimal
7317 * (or maximal) image element for each domain element in "dom",
7318 * in the form of an isl_map.
7319 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7320 * do not have an image element.
7321 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7322 * should be computed over the domain of "map". "empty" is also NULL
7323 * in this case.
7325 * If the input consists of more than one disjunct, then first
7326 * compute the desired result in the form of an isl_pw_multi_aff and
7327 * then convert that into an isl_map.
7329 * This function used to have an explicit implementation in terms
7330 * of isl_maps, but it would continually intersect the domains of
7331 * partial results with the complement of the domain of the next
7332 * partial solution, potentially leading to an explosion in the number
7333 * of disjuncts if there are several disjuncts in the input.
7334 * An even earlier implementation of this function would look for
7335 * better results in the domain of the partial result and for extra
7336 * results in the complement of this domain, which would lead to
7337 * even more splintering.
7339 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7340 __isl_take isl_map *map, __isl_take isl_set *dom,
7341 __isl_give isl_set **empty, unsigned flags)
7343 int full;
7344 struct isl_map *res;
7345 isl_pw_multi_aff *pma;
7347 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7348 if (!map || (!full && !dom))
7349 goto error;
7351 if (isl_map_plain_is_empty(map)) {
7352 if (empty)
7353 *empty = dom;
7354 else
7355 isl_set_free(dom);
7356 return map;
7359 if (map->n == 1) {
7360 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7361 dom, empty, flags);
7362 isl_map_free(map);
7363 return res;
7366 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7367 flags);
7368 return isl_map_from_pw_multi_aff_internal(pma);
7369 error:
7370 if (empty)
7371 *empty = NULL;
7372 isl_set_free(dom);
7373 isl_map_free(map);
7374 return NULL;
7377 __isl_give isl_map *isl_map_partial_lexmax(
7378 __isl_take isl_map *map, __isl_take isl_set *dom,
7379 __isl_give isl_set **empty)
7381 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7384 __isl_give isl_map *isl_map_partial_lexmin(
7385 __isl_take isl_map *map, __isl_take isl_set *dom,
7386 __isl_give isl_set **empty)
7388 return isl_map_partial_lexopt(map, dom, empty, 0);
7391 __isl_give isl_set *isl_set_partial_lexmin(
7392 __isl_take isl_set *set, __isl_take isl_set *dom,
7393 __isl_give isl_set **empty)
7395 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7396 dom, empty));
7399 __isl_give isl_set *isl_set_partial_lexmax(
7400 __isl_take isl_set *set, __isl_take isl_set *dom,
7401 __isl_give isl_set **empty)
7403 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7404 dom, empty));
7407 /* Compute the lexicographic minimum (or maximum if "flags" includes
7408 * ISL_OPT_MAX) of "bset" over its parametric domain.
7410 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7411 unsigned flags)
7413 return isl_basic_map_lexopt(bset, flags);
7416 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7418 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7421 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7423 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7426 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7428 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7431 /* Compute the lexicographic minimum of "bset" over its parametric domain
7432 * for the purpose of quantifier elimination.
7433 * That is, find an explicit representation for all the existentially
7434 * quantified variables in "bset" by computing their lexicographic
7435 * minimum.
7437 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7438 __isl_take isl_basic_set *bset)
7440 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7443 /* Given a basic map with one output dimension, compute the minimum or
7444 * maximum of that dimension as an isl_pw_aff.
7446 * Compute the optimum as a lexicographic optimum over the single
7447 * output dimension and extract the single isl_pw_aff from the result.
7449 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7450 int max)
7452 isl_pw_multi_aff *pma;
7453 isl_pw_aff *pwaff;
7455 bmap = isl_basic_map_copy(bmap);
7456 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7457 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7458 isl_pw_multi_aff_free(pma);
7460 return pwaff;
7463 /* Compute the minimum or maximum of the given output dimension
7464 * as a function of the parameters and the input dimensions,
7465 * but independently of the other output dimensions.
7467 * We first project out the other output dimension and then compute
7468 * the "lexicographic" maximum in each basic map, combining the results
7469 * using isl_pw_aff_union_max.
7471 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7472 int max)
7474 int i;
7475 isl_pw_aff *pwaff;
7476 isl_size n_out;
7478 n_out = isl_map_dim(map, isl_dim_out);
7479 if (n_out < 0)
7480 map = isl_map_free(map);
7481 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7482 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7483 if (!map)
7484 return NULL;
7486 if (map->n == 0) {
7487 isl_space *space = isl_map_get_space(map);
7488 isl_map_free(map);
7489 return isl_pw_aff_empty(space);
7492 pwaff = basic_map_dim_opt(map->p[0], max);
7493 for (i = 1; i < map->n; ++i) {
7494 isl_pw_aff *pwaff_i;
7496 pwaff_i = basic_map_dim_opt(map->p[i], max);
7497 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7500 isl_map_free(map);
7502 return pwaff;
7505 /* Compute the minimum of the given output dimension as a function of the
7506 * parameters and input dimensions, but independently of
7507 * the other output dimensions.
7509 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7511 return map_dim_opt(map, pos, 0);
7514 /* Compute the maximum of the given output dimension as a function of the
7515 * parameters and input dimensions, but independently of
7516 * the other output dimensions.
7518 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7520 return map_dim_opt(map, pos, 1);
7523 /* Compute the minimum or maximum of the given set dimension
7524 * as a function of the parameters,
7525 * but independently of the other set dimensions.
7527 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7528 int max)
7530 return map_dim_opt(set, pos, max);
7533 /* Compute the maximum of the given set dimension as a function of the
7534 * parameters, but independently of the other set dimensions.
7536 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7538 return set_dim_opt(set, pos, 1);
7541 /* Compute the minimum of the given set dimension as a function of the
7542 * parameters, but independently of the other set dimensions.
7544 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7546 return set_dim_opt(set, pos, 0);
7549 /* Apply a preimage specified by "mat" on the parameters of "bset".
7550 * bset is assumed to have only parameters and divs.
7552 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7553 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7555 isl_size nparam;
7557 nparam = isl_basic_set_dim(bset, isl_dim_param);
7558 if (nparam < 0 || !mat)
7559 goto error;
7561 bset->dim = isl_space_cow(bset->dim);
7562 if (!bset->dim)
7563 goto error;
7565 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7567 bset->dim->nparam = 0;
7568 bset->dim->n_out = nparam;
7569 bset = isl_basic_set_preimage(bset, mat);
7570 if (bset) {
7571 bset->dim->nparam = bset->dim->n_out;
7572 bset->dim->n_out = 0;
7574 return bset;
7575 error:
7576 isl_mat_free(mat);
7577 isl_basic_set_free(bset);
7578 return NULL;
7581 /* Apply a preimage specified by "mat" on the parameters of "set".
7582 * set is assumed to have only parameters and divs.
7584 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7585 __isl_take isl_mat *mat)
7587 isl_space *space;
7588 isl_size nparam;
7590 nparam = isl_set_dim(set, isl_dim_param);
7591 if (nparam < 0 || !mat)
7592 goto error;
7594 if (mat->n_row != 1 + nparam)
7595 isl_die(isl_set_get_ctx(set), isl_error_internal,
7596 "unexpected number of rows", goto error);
7598 space = isl_set_get_space(set);
7599 space = isl_space_move_dims(space, isl_dim_set, 0,
7600 isl_dim_param, 0, nparam);
7601 set = isl_set_reset_space(set, space);
7602 set = isl_set_preimage(set, mat);
7603 nparam = isl_set_dim(set, isl_dim_out);
7604 if (nparam < 0)
7605 set = isl_set_free(set);
7606 space = isl_set_get_space(set);
7607 space = isl_space_move_dims(space, isl_dim_param, 0,
7608 isl_dim_out, 0, nparam);
7609 set = isl_set_reset_space(set, space);
7610 return set;
7611 error:
7612 isl_mat_free(mat);
7613 isl_set_free(set);
7614 return NULL;
7617 /* Intersect the basic set "bset" with the affine space specified by the
7618 * equalities in "eq".
7620 static __isl_give isl_basic_set *basic_set_append_equalities(
7621 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7623 int i, k;
7624 unsigned len;
7626 if (!bset || !eq)
7627 goto error;
7629 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7630 if (!bset)
7631 goto error;
7633 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7634 for (i = 0; i < eq->n_row; ++i) {
7635 k = isl_basic_set_alloc_equality(bset);
7636 if (k < 0)
7637 goto error;
7638 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7639 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7641 isl_mat_free(eq);
7643 bset = isl_basic_set_gauss(bset, NULL);
7644 bset = isl_basic_set_finalize(bset);
7646 return bset;
7647 error:
7648 isl_mat_free(eq);
7649 isl_basic_set_free(bset);
7650 return NULL;
7653 /* Intersect the set "set" with the affine space specified by the
7654 * equalities in "eq".
7656 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7657 __isl_take isl_mat *eq)
7659 int i;
7661 if (!set || !eq)
7662 goto error;
7664 for (i = 0; i < set->n; ++i) {
7665 set->p[i] = basic_set_append_equalities(set->p[i],
7666 isl_mat_copy(eq));
7667 if (!set->p[i])
7668 goto error;
7670 isl_mat_free(eq);
7671 return set;
7672 error:
7673 isl_mat_free(eq);
7674 isl_set_free(set);
7675 return NULL;
7678 /* Given a basic set "bset" that only involves parameters and existentially
7679 * quantified variables, return the index of the first equality
7680 * that only involves parameters. If there is no such equality then
7681 * return bset->n_eq.
7683 * This function assumes that isl_basic_set_gauss has been called on "bset".
7685 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7687 int i, j;
7688 isl_size nparam, n_div;
7690 nparam = isl_basic_set_dim(bset, isl_dim_param);
7691 n_div = isl_basic_set_dim(bset, isl_dim_div);
7692 if (nparam < 0 || n_div < 0)
7693 return -1;
7695 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7696 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7697 ++i;
7700 return i;
7703 /* Compute an explicit representation for the existentially quantified
7704 * variables in "bset" by computing the "minimal value" of the set
7705 * variables. Since there are no set variables, the computation of
7706 * the minimal value essentially computes an explicit representation
7707 * of the non-empty part(s) of "bset".
7709 * The input only involves parameters and existentially quantified variables.
7710 * All equalities among parameters have been removed.
7712 * Since the existentially quantified variables in the result are in general
7713 * going to be different from those in the input, we first replace
7714 * them by the minimal number of variables based on their equalities.
7715 * This should simplify the parametric integer programming.
7717 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7719 isl_morph *morph1, *morph2;
7720 isl_set *set;
7721 isl_size n;
7723 if (!bset)
7724 return NULL;
7725 if (bset->n_eq == 0)
7726 return isl_basic_set_lexmin_compute_divs(bset);
7728 morph1 = isl_basic_set_parameter_compression(bset);
7729 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7730 bset = isl_basic_set_lift(bset);
7731 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7732 bset = isl_morph_basic_set(morph2, bset);
7733 n = isl_basic_set_dim(bset, isl_dim_set);
7734 if (n < 0)
7735 bset = isl_basic_set_free(bset);
7736 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7738 set = isl_basic_set_lexmin_compute_divs(bset);
7740 set = isl_morph_set(isl_morph_inverse(morph1), set);
7742 return set;
7745 /* Project the given basic set onto its parameter domain, possibly introducing
7746 * new, explicit, existential variables in the constraints.
7747 * The input has parameters and (possibly implicit) existential variables.
7748 * The output has the same parameters, but only
7749 * explicit existentially quantified variables.
7751 * The actual projection is performed by pip, but pip doesn't seem
7752 * to like equalities very much, so we first remove the equalities
7753 * among the parameters by performing a variable compression on
7754 * the parameters. Afterward, an inverse transformation is performed
7755 * and the equalities among the parameters are inserted back in.
7757 * The variable compression on the parameters may uncover additional
7758 * equalities that were only implicit before. We therefore check
7759 * if there are any new parameter equalities in the result and
7760 * if so recurse. The removal of parameter equalities is required
7761 * for the parameter compression performed by base_compute_divs.
7763 static __isl_give isl_set *parameter_compute_divs(
7764 __isl_take isl_basic_set *bset)
7766 int i;
7767 struct isl_mat *eq;
7768 struct isl_mat *T, *T2;
7769 struct isl_set *set;
7770 isl_size nparam;
7772 bset = isl_basic_set_cow(bset);
7773 if (!bset)
7774 return NULL;
7776 if (bset->n_eq == 0)
7777 return base_compute_divs(bset);
7779 bset = isl_basic_set_gauss(bset, NULL);
7780 if (!bset)
7781 return NULL;
7782 if (isl_basic_set_plain_is_empty(bset))
7783 return isl_set_from_basic_set(bset);
7785 i = first_parameter_equality(bset);
7786 if (i == bset->n_eq)
7787 return base_compute_divs(bset);
7789 nparam = isl_basic_set_dim(bset, isl_dim_param);
7790 if (nparam < 0)
7791 return isl_set_from_basic_set(isl_basic_set_free(bset));
7792 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7793 0, 1 + nparam);
7794 eq = isl_mat_cow(eq);
7795 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7796 if (T && T->n_col == 0) {
7797 isl_mat_free(T);
7798 isl_mat_free(T2);
7799 isl_mat_free(eq);
7800 bset = isl_basic_set_set_to_empty(bset);
7801 return isl_set_from_basic_set(bset);
7803 bset = basic_set_parameter_preimage(bset, T);
7805 i = first_parameter_equality(bset);
7806 if (!bset)
7807 set = NULL;
7808 else if (i == bset->n_eq)
7809 set = base_compute_divs(bset);
7810 else
7811 set = parameter_compute_divs(bset);
7812 set = set_parameter_preimage(set, T2);
7813 set = set_append_equalities(set, eq);
7814 return set;
7817 /* Insert the divs from "ls" before those of "bmap".
7819 * The number of columns is not changed, which means that the last
7820 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7821 * The caller is responsible for removing the same number of dimensions
7822 * from the space of "bmap".
7824 static __isl_give isl_basic_map *insert_divs_from_local_space(
7825 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7827 int i;
7828 isl_size n_div;
7829 int old_n_div;
7831 n_div = isl_local_space_dim(ls, isl_dim_div);
7832 if (n_div < 0)
7833 return isl_basic_map_free(bmap);
7834 if (n_div == 0)
7835 return bmap;
7837 old_n_div = bmap->n_div;
7838 bmap = insert_div_rows(bmap, n_div);
7839 if (!bmap)
7840 return NULL;
7842 for (i = 0; i < n_div; ++i) {
7843 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7844 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7847 return bmap;
7850 /* Replace the space of "bmap" by the space and divs of "ls".
7852 * If "ls" has any divs, then we simplify the result since we may
7853 * have discovered some additional equalities that could simplify
7854 * the div expressions.
7856 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7857 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7859 isl_size n_div;
7861 bmap = isl_basic_map_cow(bmap);
7862 n_div = isl_local_space_dim(ls, isl_dim_div);
7863 if (!bmap || n_div < 0)
7864 goto error;
7866 bmap = insert_divs_from_local_space(bmap, ls);
7867 if (!bmap)
7868 goto error;
7870 isl_space_free(bmap->dim);
7871 bmap->dim = isl_local_space_get_space(ls);
7872 if (!bmap->dim)
7873 goto error;
7875 isl_local_space_free(ls);
7876 if (n_div > 0)
7877 bmap = isl_basic_map_simplify(bmap);
7878 bmap = isl_basic_map_finalize(bmap);
7879 return bmap;
7880 error:
7881 isl_basic_map_free(bmap);
7882 isl_local_space_free(ls);
7883 return NULL;
7886 /* Replace the space of "map" by the space and divs of "ls".
7888 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7889 __isl_take isl_local_space *ls)
7891 int i;
7893 map = isl_map_cow(map);
7894 if (!map || !ls)
7895 goto error;
7897 for (i = 0; i < map->n; ++i) {
7898 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7899 isl_local_space_copy(ls));
7900 if (!map->p[i])
7901 goto error;
7903 isl_space_free(isl_map_take_space(map));
7904 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7906 isl_local_space_free(ls);
7907 return map;
7908 error:
7909 isl_local_space_free(ls);
7910 isl_map_free(map);
7911 return NULL;
7914 /* Compute an explicit representation for the existentially
7915 * quantified variables for which do not know any explicit representation yet.
7917 * We first sort the existentially quantified variables so that the
7918 * existentially quantified variables for which we already have an explicit
7919 * representation are placed before those for which we do not.
7920 * The input dimensions, the output dimensions and the existentially
7921 * quantified variables for which we already have an explicit
7922 * representation are then turned into parameters.
7923 * compute_divs returns a map with the same parameters and
7924 * no input or output dimensions and the dimension specification
7925 * is reset to that of the input, including the existentially quantified
7926 * variables for which we already had an explicit representation.
7928 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7930 struct isl_basic_set *bset;
7931 struct isl_set *set;
7932 struct isl_map *map;
7933 isl_space *space;
7934 isl_local_space *ls;
7935 isl_size nparam;
7936 isl_size n_in;
7937 isl_size n_out;
7938 int n_known;
7939 int i;
7941 bmap = isl_basic_map_sort_divs(bmap);
7942 bmap = isl_basic_map_cow(bmap);
7943 if (!bmap)
7944 return NULL;
7946 n_known = isl_basic_map_first_unknown_div(bmap);
7947 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7948 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7949 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7950 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7951 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7953 space = isl_space_set_alloc(bmap->ctx,
7954 nparam + n_in + n_out + n_known, 0);
7955 if (!space)
7956 goto error;
7958 ls = isl_basic_map_get_local_space(bmap);
7959 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7960 n_known, bmap->n_div - n_known);
7961 if (n_known > 0) {
7962 for (i = n_known; i < bmap->n_div; ++i)
7963 swap_div(bmap, i - n_known, i);
7964 bmap->n_div -= n_known;
7965 bmap->extra -= n_known;
7967 bmap = isl_basic_map_reset_space(bmap, space);
7968 bset = bset_from_bmap(bmap);
7970 set = parameter_compute_divs(bset);
7971 map = set_to_map(set);
7972 map = replace_space_by_local_space(map, ls);
7974 return map;
7975 error:
7976 isl_basic_map_free(bmap);
7977 return NULL;
7980 /* Remove the explicit representation of local variable "div",
7981 * if there is any.
7983 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7984 __isl_take isl_basic_map *bmap, int div)
7986 isl_bool unknown;
7988 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7989 if (unknown < 0)
7990 return isl_basic_map_free(bmap);
7991 if (unknown)
7992 return bmap;
7994 bmap = isl_basic_map_cow(bmap);
7995 if (!bmap)
7996 return NULL;
7997 isl_int_set_si(bmap->div[div][0], 0);
7998 return bmap;
8001 /* Is local variable "div" of "bmap" marked as not having an explicit
8002 * representation?
8003 * Note that even if "div" is not marked in this way and therefore
8004 * has an explicit representation, this representation may still
8005 * depend (indirectly) on other local variables that do not
8006 * have an explicit representation.
8008 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8009 int div)
8011 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8012 return isl_bool_error;
8013 return isl_int_is_zero(bmap->div[div][0]);
8016 /* Return the position of the first local variable that does not
8017 * have an explicit representation.
8018 * Return the total number of local variables if they all have
8019 * an explicit representation.
8020 * Return -1 on error.
8022 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8024 int i;
8026 if (!bmap)
8027 return -1;
8029 for (i = 0; i < bmap->n_div; ++i) {
8030 if (!isl_basic_map_div_is_known(bmap, i))
8031 return i;
8033 return bmap->n_div;
8036 /* Return the position of the first local variable that does not
8037 * have an explicit representation.
8038 * Return the total number of local variables if they all have
8039 * an explicit representation.
8040 * Return -1 on error.
8042 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8044 return isl_basic_map_first_unknown_div(bset);
8047 /* Does "bmap" have an explicit representation for all local variables?
8049 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8051 int first;
8052 isl_size n;
8054 n = isl_basic_map_dim(bmap, isl_dim_div);
8055 first = isl_basic_map_first_unknown_div(bmap);
8056 if (n < 0 || first < 0)
8057 return isl_bool_error;
8058 return first == n;
8061 /* Do all basic maps in "map" have an explicit representation
8062 * for all local variables?
8064 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8066 int i;
8068 if (!map)
8069 return isl_bool_error;
8071 for (i = 0; i < map->n; ++i) {
8072 int known = isl_basic_map_divs_known(map->p[i]);
8073 if (known <= 0)
8074 return known;
8077 return isl_bool_true;
8080 /* If bmap contains any unknown divs, then compute explicit
8081 * expressions for them. However, this computation may be
8082 * quite expensive, so first try to remove divs that aren't
8083 * strictly needed.
8085 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8087 int known;
8088 struct isl_map *map;
8090 known = isl_basic_map_divs_known(bmap);
8091 if (known < 0)
8092 goto error;
8093 if (known)
8094 return isl_map_from_basic_map(bmap);
8096 bmap = isl_basic_map_drop_redundant_divs(bmap);
8098 known = isl_basic_map_divs_known(bmap);
8099 if (known < 0)
8100 goto error;
8101 if (known)
8102 return isl_map_from_basic_map(bmap);
8104 map = compute_divs(bmap);
8105 return map;
8106 error:
8107 isl_basic_map_free(bmap);
8108 return NULL;
8111 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8113 int i;
8114 int known;
8115 struct isl_map *res;
8117 if (!map)
8118 return NULL;
8119 if (map->n == 0)
8120 return map;
8122 known = isl_map_divs_known(map);
8123 if (known < 0) {
8124 isl_map_free(map);
8125 return NULL;
8127 if (known)
8128 return map;
8130 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8131 for (i = 1 ; i < map->n; ++i) {
8132 struct isl_map *r2;
8133 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8134 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8135 res = isl_map_union_disjoint(res, r2);
8136 else
8137 res = isl_map_union(res, r2);
8139 isl_map_free(map);
8141 return res;
8144 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8146 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8149 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8151 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8154 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8156 isl_space *space;
8157 isl_size n_out;
8159 n_out = isl_map_dim(map, isl_dim_out);
8160 if (n_out < 0)
8161 return set_from_map(isl_map_free(map));
8162 space = isl_space_domain(isl_map_get_space(map));
8164 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8166 return set_from_map(isl_map_reset_space(map, space));
8169 /* Return the union of "map1" and "map2", where we assume for now that
8170 * "map1" and "map2" are disjoint. Note that the basic maps inside
8171 * "map1" or "map2" may not be disjoint from each other.
8172 * Also note that this function is also called from isl_map_union,
8173 * which takes care of handling the situation where "map1" and "map2"
8174 * may not be disjoint.
8176 * If one of the inputs is empty, we can simply return the other input.
8177 * Similarly, if one of the inputs is universal, then it is equal to the union.
8179 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8180 __isl_take isl_map *map2)
8182 int i;
8183 unsigned flags = 0;
8184 struct isl_map *map = NULL;
8185 int is_universe;
8187 if (isl_map_check_equal_space(map1, map2) < 0)
8188 goto error;
8190 if (map1->n == 0) {
8191 isl_map_free(map1);
8192 return map2;
8194 if (map2->n == 0) {
8195 isl_map_free(map2);
8196 return map1;
8199 is_universe = isl_map_plain_is_universe(map1);
8200 if (is_universe < 0)
8201 goto error;
8202 if (is_universe) {
8203 isl_map_free(map2);
8204 return map1;
8207 is_universe = isl_map_plain_is_universe(map2);
8208 if (is_universe < 0)
8209 goto error;
8210 if (is_universe) {
8211 isl_map_free(map1);
8212 return map2;
8215 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8216 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8217 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8219 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8220 map1->n + map2->n, flags);
8221 if (!map)
8222 goto error;
8223 for (i = 0; i < map1->n; ++i) {
8224 map = isl_map_add_basic_map(map,
8225 isl_basic_map_copy(map1->p[i]));
8226 if (!map)
8227 goto error;
8229 for (i = 0; i < map2->n; ++i) {
8230 map = isl_map_add_basic_map(map,
8231 isl_basic_map_copy(map2->p[i]));
8232 if (!map)
8233 goto error;
8235 isl_map_free(map1);
8236 isl_map_free(map2);
8237 return map;
8238 error:
8239 isl_map_free(map);
8240 isl_map_free(map1);
8241 isl_map_free(map2);
8242 return NULL;
8245 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8246 * guaranteed to be disjoint by the caller.
8248 * Note that this functions is called from within isl_map_make_disjoint,
8249 * so we have to be careful not to touch the constraints of the inputs
8250 * in any way.
8252 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8253 __isl_take isl_map *map2)
8255 isl_map_align_params_bin(&map1, &map2);
8256 return map_union_disjoint(map1, map2);
8259 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8260 * not be disjoint.
8262 * We currently simply call map_union_disjoint, the internal operation
8263 * of which does not really depend on the inputs being disjoint.
8264 * If the result contains more than one basic map, then we clear
8265 * the disjoint flag since the result may contain basic maps from
8266 * both inputs and these are not guaranteed to be disjoint.
8268 * As a special case, if "map1" and "map2" are obviously equal,
8269 * then we simply return "map1".
8271 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8272 __isl_take isl_map *map2)
8274 int equal;
8276 if (isl_map_align_params_bin(&map1, &map2) < 0)
8277 goto error;
8279 equal = isl_map_plain_is_equal(map1, map2);
8280 if (equal < 0)
8281 goto error;
8282 if (equal) {
8283 isl_map_free(map2);
8284 return map1;
8287 map1 = map_union_disjoint(map1, map2);
8288 if (!map1)
8289 return NULL;
8290 if (map1->n > 1)
8291 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8292 return map1;
8293 error:
8294 isl_map_free(map1);
8295 isl_map_free(map2);
8296 return NULL;
8299 __isl_give isl_set *isl_set_union_disjoint(
8300 __isl_take isl_set *set1, __isl_take isl_set *set2)
8302 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8303 set_to_map(set2)));
8306 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8307 __isl_take isl_set *set2)
8309 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8312 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8313 * the results in a map living in "space".
8315 * "map" and "set" are assumed to be compatible and non-NULL.
8317 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8318 __isl_take isl_space *space, __isl_take isl_set *set,
8319 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8320 __isl_take isl_basic_set *bset))
8322 unsigned flags = 0;
8323 struct isl_map *result;
8324 int i, j;
8326 if (isl_set_plain_is_universe(set)) {
8327 isl_set_free(set);
8328 return isl_map_reset_equal_dim_space(map, space);
8331 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8332 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8333 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8335 result = isl_map_alloc_space(space, map->n * set->n, flags);
8336 for (i = 0; result && i < map->n; ++i)
8337 for (j = 0; j < set->n; ++j) {
8338 result = isl_map_add_basic_map(result,
8339 fn(isl_basic_map_copy(map->p[i]),
8340 isl_basic_set_copy(set->p[j])));
8341 if (!result)
8342 break;
8345 isl_map_free(map);
8346 isl_set_free(set);
8347 return result;
8350 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8351 __isl_take isl_set *set)
8353 isl_bool ok;
8354 isl_space *space;
8356 isl_map_align_params_set(&map, &set);
8357 ok = isl_map_compatible_range(map, set);
8358 if (ok < 0)
8359 goto error;
8360 if (!ok)
8361 isl_die(set->ctx, isl_error_invalid,
8362 "incompatible spaces", goto error);
8364 space = isl_map_get_space(map);
8365 return map_intersect_set(map, space, set,
8366 &isl_basic_map_intersect_range);
8367 error:
8368 isl_map_free(map);
8369 isl_set_free(set);
8370 return NULL;
8373 /* Intersect the domain of "map" with "set".
8375 * If the domain dimensions of "map" do not have any identifiers,
8376 * then copy them over from "set".
8378 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8379 __isl_take isl_set *set)
8381 isl_bool ok;
8382 isl_space *space;
8384 isl_map_align_params_set(&map, &set);
8385 ok = isl_map_compatible_domain(map, set);
8386 if (ok < 0)
8387 goto error;
8388 if (!ok)
8389 isl_die(set->ctx, isl_error_invalid,
8390 "incompatible spaces", goto error);
8392 space = isl_map_get_space(map);
8393 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8394 isl_set_peek_space(set), isl_dim_set);
8395 return map_intersect_set(map, space, set,
8396 &isl_basic_map_intersect_domain);
8397 error:
8398 isl_map_free(map);
8399 isl_set_free(set);
8400 return NULL;
8403 #undef TYPE
8404 #define TYPE isl_map
8405 static
8406 #include "isl_copy_tuple_id_templ.c"
8408 /* Data structure that specifies how isl_map_intersect_factor
8409 * should operate.
8411 * "preserve_type" is the tuple where the factor differs from
8412 * the input map and of which the identifiers needs
8413 * to be preserved explicitly.
8414 * "other_factor" is used to extract the space of the other factor
8415 * from the space of the product ("map").
8416 * "product" is used to combine the given factor and a universe map
8417 * in the space returned by "other_factor" to produce a map
8418 * that lives in the same space as the input map.
8420 struct isl_intersect_factor_control {
8421 enum isl_dim_type preserve_type;
8422 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8423 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8424 __isl_take isl_map *other);
8427 /* Given a map "map" in some product space and a map "factor"
8428 * living in some factor space, return the intersection.
8430 * After aligning the parameters,
8431 * the map "factor" is first extended to a map living in the same space
8432 * as "map" and then a regular intersection is computed.
8434 * Note that the extension is computed as a product, which is anonymous
8435 * by default. If "map" has an identifier on the corresponding tuple,
8436 * then this identifier needs to be set on the product
8437 * before the intersection is computed.
8439 static __isl_give isl_map *isl_map_intersect_factor(
8440 __isl_take isl_map *map, __isl_take isl_map *factor,
8441 struct isl_intersect_factor_control *control)
8443 isl_bool equal;
8444 isl_space *space;
8445 isl_map *other, *product;
8447 equal = isl_map_has_equal_params(map, factor);
8448 if (equal < 0)
8449 goto error;
8450 if (!equal) {
8451 map = isl_map_align_params(map, isl_map_get_space(factor));
8452 factor = isl_map_align_params(factor, isl_map_get_space(map));
8455 space = isl_map_get_space(map);
8456 other = isl_map_universe(control->other_factor(space));
8457 product = control->product(factor, other);
8459 space = isl_map_peek_space(map);
8460 product = isl_map_copy_tuple_id(product, control->preserve_type,
8461 space, control->preserve_type);
8462 return map_intersect(map, product);
8463 error:
8464 isl_map_free(map);
8465 isl_map_free(factor);
8466 return NULL;
8469 /* Return the domain product of "map2" and "map1".
8471 static __isl_give isl_map *isl_map_reverse_domain_product(
8472 __isl_take isl_map *map1, __isl_take isl_map *map2)
8474 return isl_map_domain_product(map2, map1);
8477 /* Return the range product of "map2" and "map1".
8479 static __isl_give isl_map *isl_map_reverse_range_product(
8480 __isl_take isl_map *map1, __isl_take isl_map *map2)
8482 return isl_map_range_product(map2, map1);
8485 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8486 * in the space A -> C, return the intersection.
8488 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8489 __isl_take isl_map *map, __isl_take isl_map *factor)
8491 struct isl_intersect_factor_control control = {
8492 .preserve_type = isl_dim_in,
8493 .other_factor = isl_space_domain_factor_range,
8494 .product = isl_map_domain_product,
8497 return isl_map_intersect_factor(map, factor, &control);
8500 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8501 * in the space B -> C, return the intersection.
8503 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8504 __isl_take isl_map *map, __isl_take isl_map *factor)
8506 struct isl_intersect_factor_control control = {
8507 .preserve_type = isl_dim_in,
8508 .other_factor = isl_space_domain_factor_domain,
8509 .product = isl_map_reverse_domain_product,
8512 return isl_map_intersect_factor(map, factor, &control);
8515 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8516 * in the space A -> B, return the intersection.
8518 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8519 __isl_take isl_map *map, __isl_take isl_map *factor)
8521 struct isl_intersect_factor_control control = {
8522 .preserve_type = isl_dim_out,
8523 .other_factor = isl_space_range_factor_range,
8524 .product = isl_map_range_product,
8527 return isl_map_intersect_factor(map, factor, &control);
8530 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8531 * in the space A -> C, return the intersection.
8533 __isl_give isl_map *isl_map_intersect_range_factor_range(
8534 __isl_take isl_map *map, __isl_take isl_map *factor)
8536 struct isl_intersect_factor_control control = {
8537 .preserve_type = isl_dim_out,
8538 .other_factor = isl_space_range_factor_domain,
8539 .product = isl_map_reverse_range_product,
8542 return isl_map_intersect_factor(map, factor, &control);
8545 /* Given a set "set" in a space [A -> B] and a set "domain"
8546 * in the space A, return the intersection.
8548 * The set "domain" is first extended to a set living in the space
8549 * [A -> B] and then a regular intersection is computed.
8551 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8552 __isl_take isl_set *domain)
8554 struct isl_intersect_factor_control control = {
8555 .preserve_type = isl_dim_set,
8556 .other_factor = isl_space_factor_range,
8557 .product = isl_map_range_product,
8560 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8561 set_to_map(domain), &control));
8564 /* Given a set "set" in a space [A -> B] and a set "range"
8565 * in the space B, return the intersection.
8567 * The set "range" is first extended to a set living in the space
8568 * [A -> B] and then a regular intersection is computed.
8570 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8571 __isl_take isl_set *range)
8573 struct isl_intersect_factor_control control = {
8574 .preserve_type = isl_dim_set,
8575 .other_factor = isl_space_factor_domain,
8576 .product = isl_map_reverse_range_product,
8579 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8580 set_to_map(range), &control));
8583 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8584 * in the space A, return the intersection.
8586 * The set "domain" is extended to a set living in the space [A -> B] and
8587 * the domain of "map" is intersected with this set.
8589 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
8590 __isl_take isl_map *map, __isl_take isl_set *domain)
8592 isl_space *space;
8593 isl_set *factor;
8595 isl_map_align_params_set(&map, &domain);
8596 space = isl_map_get_space(map);
8597 space = isl_space_domain_wrapped_range(space);
8598 factor = isl_set_universe(space);
8599 domain = isl_set_product(domain, factor);
8600 return isl_map_intersect_domain(map, domain);
8603 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8604 * in the space B, return the intersection.
8606 * The set "domain" is extended to a set living in the space [B -> C] and
8607 * the range of "map" is intersected with this set.
8609 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
8610 __isl_take isl_map *map, __isl_take isl_set *domain)
8612 isl_space *space;
8613 isl_set *factor;
8615 isl_map_align_params_set(&map, &domain);
8616 space = isl_map_get_space(map);
8617 space = isl_space_range_wrapped_range(space);
8618 factor = isl_set_universe(space);
8619 domain = isl_set_product(domain, factor);
8620 return isl_map_intersect_range(map, domain);
8623 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8624 __isl_take isl_map *map2)
8626 if (isl_map_align_params_bin(&map1, &map2) < 0)
8627 goto error;
8628 map1 = isl_map_reverse(map1);
8629 map1 = isl_map_apply_range(map1, map2);
8630 return isl_map_reverse(map1);
8631 error:
8632 isl_map_free(map1);
8633 isl_map_free(map2);
8634 return NULL;
8637 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8638 __isl_take isl_map *map2)
8640 isl_space *space;
8641 struct isl_map *result;
8642 int i, j;
8644 if (isl_map_align_params_bin(&map1, &map2) < 0)
8645 goto error;
8647 space = isl_space_join(isl_space_copy(map1->dim),
8648 isl_space_copy(map2->dim));
8650 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8651 if (!result)
8652 goto error;
8653 for (i = 0; i < map1->n; ++i)
8654 for (j = 0; j < map2->n; ++j) {
8655 result = isl_map_add_basic_map(result,
8656 isl_basic_map_apply_range(
8657 isl_basic_map_copy(map1->p[i]),
8658 isl_basic_map_copy(map2->p[j])));
8659 if (!result)
8660 goto error;
8662 isl_map_free(map1);
8663 isl_map_free(map2);
8664 if (result && result->n <= 1)
8665 ISL_F_SET(result, ISL_MAP_DISJOINT);
8666 return result;
8667 error:
8668 isl_map_free(map1);
8669 isl_map_free(map2);
8670 return NULL;
8673 /* Is "bmap" a transformation, i.e.,
8674 * does it relate elements from the same space.
8676 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8678 isl_space *space;
8680 space = isl_basic_map_peek_space(bmap);
8681 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8684 /* Check that "bmap" is a transformation, i.e.,
8685 * that it relates elements from the same space.
8687 static isl_stat isl_basic_map_check_transformation(
8688 __isl_keep isl_basic_map *bmap)
8690 isl_bool equal;
8692 equal = isl_basic_map_is_transformation(bmap);
8693 if (equal < 0)
8694 return isl_stat_error;
8695 if (!equal)
8696 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8697 "domain and range don't match", return isl_stat_error);
8698 return isl_stat_ok;
8702 * returns range - domain
8704 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8706 isl_space *target_space;
8707 struct isl_basic_set *bset;
8708 isl_size dim;
8709 isl_size nparam;
8710 isl_size total;
8711 int i;
8713 if (isl_basic_map_check_transformation(bmap) < 0)
8714 return isl_basic_map_free(bmap);
8715 dim = isl_basic_map_dim(bmap, isl_dim_in);
8716 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8717 if (dim < 0 || nparam < 0)
8718 goto error;
8719 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8720 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8721 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8722 total = isl_basic_map_dim(bmap, isl_dim_all);
8723 if (total < 0)
8724 bmap = isl_basic_map_free(bmap);
8725 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8726 for (i = 0; i < dim; ++i) {
8727 int j = isl_basic_map_alloc_equality(bmap);
8728 if (j < 0) {
8729 bmap = isl_basic_map_free(bmap);
8730 break;
8732 isl_seq_clr(bmap->eq[j], 1 + total);
8733 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8734 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8735 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8737 bset = isl_basic_map_domain(bmap);
8738 bset = isl_basic_set_reset_space(bset, target_space);
8739 return bset;
8740 error:
8741 isl_basic_map_free(bmap);
8742 return NULL;
8745 /* Is the tuple of type "type1" of "map" the same as
8746 * the tuple of type "type2" of "space"?
8748 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8749 enum isl_dim_type type1, __isl_keep isl_space *space,
8750 enum isl_dim_type type2)
8752 isl_space *map_space;
8754 map_space = isl_map_peek_space(map);
8755 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8758 /* Is the tuple of type "type1" of "map1" the same as
8759 * the tuple of type "type2" of "map2"?
8761 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8762 enum isl_dim_type type1, __isl_keep isl_map *map2,
8763 enum isl_dim_type type2)
8765 isl_space *space1, *space2;
8767 space1 = isl_map_peek_space(map1);
8768 space2 = isl_map_peek_space(map2);
8769 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8772 /* Is the space of "obj" equal to "space", ignoring parameters?
8774 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8775 __isl_keep isl_space *space)
8777 isl_space *map_space;
8779 map_space = isl_map_peek_space(map);
8780 return isl_space_has_equal_tuples(map_space, space);
8783 /* Check that "map" is a transformation, i.e.,
8784 * that it relates elements from the same space.
8786 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8788 isl_bool equal;
8790 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8791 if (equal < 0)
8792 return isl_stat_error;
8793 if (!equal)
8794 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8795 "domain and range don't match", return isl_stat_error);
8796 return isl_stat_ok;
8800 * returns range - domain
8802 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8804 int i;
8805 isl_space *space;
8806 struct isl_set *result;
8808 if (isl_map_check_transformation(map) < 0)
8809 goto error;
8810 space = isl_map_get_space(map);
8811 space = isl_space_domain(space);
8812 result = isl_set_alloc_space(space, map->n, 0);
8813 if (!result)
8814 goto error;
8815 for (i = 0; i < map->n; ++i)
8816 result = isl_set_add_basic_set(result,
8817 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8818 isl_map_free(map);
8819 return result;
8820 error:
8821 isl_map_free(map);
8822 return NULL;
8826 * returns [domain -> range] -> range - domain
8828 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8829 __isl_take isl_basic_map *bmap)
8831 int i, k;
8832 isl_space *space;
8833 isl_basic_map *domain;
8834 isl_size nparam, n;
8835 isl_size total;
8837 if (isl_basic_map_check_transformation(bmap) < 0)
8838 return isl_basic_map_free(bmap);
8840 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8841 n = isl_basic_map_dim(bmap, isl_dim_in);
8842 if (nparam < 0 || n < 0)
8843 return isl_basic_map_free(bmap);
8845 space = isl_basic_map_get_space(bmap);
8846 space = isl_space_from_range(isl_space_domain(space));
8847 domain = isl_basic_map_universe(space);
8849 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8850 bmap = isl_basic_map_apply_range(bmap, domain);
8851 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8853 total = isl_basic_map_dim(bmap, isl_dim_all);
8854 if (total < 0)
8855 return isl_basic_map_free(bmap);
8857 for (i = 0; i < n; ++i) {
8858 k = isl_basic_map_alloc_equality(bmap);
8859 if (k < 0)
8860 goto error;
8861 isl_seq_clr(bmap->eq[k], 1 + total);
8862 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8863 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8864 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8867 bmap = isl_basic_map_gauss(bmap, NULL);
8868 return isl_basic_map_finalize(bmap);
8869 error:
8870 isl_basic_map_free(bmap);
8871 return NULL;
8875 * returns [domain -> range] -> range - domain
8877 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8879 if (isl_map_check_transformation(map) < 0)
8880 return isl_map_free(map);
8882 return isl_map_transform(map, &isl_space_range_map,
8883 &isl_basic_map_deltas_map);
8886 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8888 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8890 isl_space *space;
8891 isl_map *map;
8893 space = isl_space_map_from_set(isl_set_get_space(deltas));
8894 map = isl_map_deltas_map(isl_map_universe(space));
8895 map = isl_map_intersect_range(map, deltas);
8897 return isl_set_unwrap(isl_map_domain(map));
8900 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8902 isl_size n_in, n_out;
8904 n_in = isl_space_dim(space, isl_dim_in);
8905 n_out = isl_space_dim(space, isl_dim_out);
8906 if (n_in < 0 || n_out < 0)
8907 goto error;
8908 if (n_in != n_out)
8909 isl_die(space->ctx, isl_error_invalid,
8910 "number of input and output dimensions needs to be "
8911 "the same", goto error);
8912 return isl_basic_map_equal(space, n_in);
8913 error:
8914 isl_space_free(space);
8915 return NULL;
8918 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8920 return isl_map_from_basic_map(isl_basic_map_identity(space));
8923 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8925 isl_space *space = isl_set_get_space(set);
8926 isl_map *id;
8927 id = isl_map_identity(isl_space_map_from_set(space));
8928 return isl_map_intersect_range(id, set);
8931 /* Construct a basic set with all set dimensions having only non-negative
8932 * values.
8934 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8935 __isl_take isl_space *space)
8937 int i;
8938 isl_size nparam;
8939 isl_size dim;
8940 isl_size total;
8941 struct isl_basic_set *bset;
8943 nparam = isl_space_dim(space, isl_dim_param);
8944 dim = isl_space_dim(space, isl_dim_set);
8945 total = isl_space_dim(space, isl_dim_all);
8946 if (nparam < 0 || dim < 0 || total < 0)
8947 space = isl_space_free(space);
8948 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8949 if (!bset)
8950 return NULL;
8951 for (i = 0; i < dim; ++i) {
8952 int k = isl_basic_set_alloc_inequality(bset);
8953 if (k < 0)
8954 goto error;
8955 isl_seq_clr(bset->ineq[k], 1 + total);
8956 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8958 return bset;
8959 error:
8960 isl_basic_set_free(bset);
8961 return NULL;
8964 /* Construct the half-space x_pos >= 0.
8966 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8967 int pos)
8969 int k;
8970 isl_size total;
8971 isl_basic_set *nonneg;
8973 total = isl_space_dim(space, isl_dim_all);
8974 if (total < 0)
8975 space = isl_space_free(space);
8976 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8977 k = isl_basic_set_alloc_inequality(nonneg);
8978 if (k < 0)
8979 goto error;
8980 isl_seq_clr(nonneg->ineq[k], 1 + total);
8981 isl_int_set_si(nonneg->ineq[k][pos], 1);
8983 return isl_basic_set_finalize(nonneg);
8984 error:
8985 isl_basic_set_free(nonneg);
8986 return NULL;
8989 /* Construct the half-space x_pos <= -1.
8991 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8992 int pos)
8994 int k;
8995 isl_size total;
8996 isl_basic_set *neg;
8998 total = isl_space_dim(space, isl_dim_all);
8999 if (total < 0)
9000 space = isl_space_free(space);
9001 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9002 k = isl_basic_set_alloc_inequality(neg);
9003 if (k < 0)
9004 goto error;
9005 isl_seq_clr(neg->ineq[k], 1 + total);
9006 isl_int_set_si(neg->ineq[k][0], -1);
9007 isl_int_set_si(neg->ineq[k][pos], -1);
9009 return isl_basic_set_finalize(neg);
9010 error:
9011 isl_basic_set_free(neg);
9012 return NULL;
9015 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9016 enum isl_dim_type type, unsigned first, unsigned n)
9018 int i;
9019 unsigned offset;
9020 isl_basic_set *nonneg;
9021 isl_basic_set *neg;
9023 if (n == 0)
9024 return set;
9026 if (isl_set_check_range(set, type, first, n) < 0)
9027 return isl_set_free(set);
9029 offset = pos(set->dim, type);
9030 for (i = 0; i < n; ++i) {
9031 nonneg = nonneg_halfspace(isl_set_get_space(set),
9032 offset + first + i);
9033 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9035 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9038 return set;
9041 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9042 int len,
9043 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9044 void *user)
9046 isl_set *half;
9048 if (!set)
9049 return isl_stat_error;
9050 if (isl_set_plain_is_empty(set)) {
9051 isl_set_free(set);
9052 return isl_stat_ok;
9054 if (first == len)
9055 return fn(set, signs, user);
9057 signs[first] = 1;
9058 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9059 1 + first));
9060 half = isl_set_intersect(half, isl_set_copy(set));
9061 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9062 goto error;
9064 signs[first] = -1;
9065 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9066 1 + first));
9067 half = isl_set_intersect(half, set);
9068 return foreach_orthant(half, signs, first + 1, len, fn, user);
9069 error:
9070 isl_set_free(set);
9071 return isl_stat_error;
9074 /* Call "fn" on the intersections of "set" with each of the orthants
9075 * (except for obviously empty intersections). The orthant is identified
9076 * by the signs array, with each entry having value 1 or -1 according
9077 * to the sign of the corresponding variable.
9079 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9080 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9081 void *user)
9083 isl_size nparam;
9084 isl_size nvar;
9085 int *signs;
9086 isl_stat r;
9088 if (!set)
9089 return isl_stat_error;
9090 if (isl_set_plain_is_empty(set))
9091 return isl_stat_ok;
9093 nparam = isl_set_dim(set, isl_dim_param);
9094 nvar = isl_set_dim(set, isl_dim_set);
9095 if (nparam < 0 || nvar < 0)
9096 return isl_stat_error;
9098 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9100 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9101 fn, user);
9103 free(signs);
9105 return r;
9108 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9110 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9113 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9114 __isl_keep isl_basic_map *bmap2)
9116 isl_bool is_subset;
9117 struct isl_map *map1;
9118 struct isl_map *map2;
9120 if (!bmap1 || !bmap2)
9121 return isl_bool_error;
9123 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9124 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9126 is_subset = isl_map_is_subset(map1, map2);
9128 isl_map_free(map1);
9129 isl_map_free(map2);
9131 return is_subset;
9134 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9135 __isl_keep isl_basic_set *bset2)
9137 return isl_basic_map_is_subset(bset1, bset2);
9140 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9141 __isl_keep isl_basic_map *bmap2)
9143 isl_bool is_subset;
9145 if (!bmap1 || !bmap2)
9146 return isl_bool_error;
9147 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9148 if (is_subset != isl_bool_true)
9149 return is_subset;
9150 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9151 return is_subset;
9154 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9155 __isl_keep isl_basic_set *bset2)
9157 return isl_basic_map_is_equal(
9158 bset_to_bmap(bset1), bset_to_bmap(bset2));
9161 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9163 int i;
9164 int is_empty;
9166 if (!map)
9167 return isl_bool_error;
9168 for (i = 0; i < map->n; ++i) {
9169 is_empty = isl_basic_map_is_empty(map->p[i]);
9170 if (is_empty < 0)
9171 return isl_bool_error;
9172 if (!is_empty)
9173 return isl_bool_false;
9175 return isl_bool_true;
9178 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9180 return map ? map->n == 0 : isl_bool_error;
9183 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9185 return set ? set->n == 0 : isl_bool_error;
9188 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9190 return isl_map_is_empty(set_to_map(set));
9193 #undef TYPE
9194 #define TYPE isl_basic_map
9196 static
9197 #include "isl_type_has_equal_space_bin_templ.c"
9198 #include "isl_type_check_equal_space_templ.c"
9200 /* Check that "bset1" and "bset2" live in the same space,
9201 * reporting an error if they do not.
9203 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9204 __isl_keep isl_basic_set *bset2)
9206 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9207 bset_to_bmap(bset1));
9210 #undef TYPE
9211 #define TYPE isl_map
9213 #include "isl_type_has_equal_space_bin_templ.c"
9214 #include "isl_type_check_equal_space_templ.c"
9215 #include "isl_type_has_space_templ.c"
9217 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9218 __isl_keep isl_set *set2)
9220 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9223 #undef TYPE1
9224 #define TYPE1 isl_map
9225 #undef TYPE2
9226 #define TYPE2 isl_basic_map
9227 #undef TYPE_PAIR
9228 #define TYPE_PAIR isl_map_basic_map
9230 static
9231 #include "isl_type_has_equal_space_templ.c"
9232 #include "isl_type_check_equal_space_templ.c"
9234 /* Check that "set" and "bset" live in the same space,
9235 * reporting an error if they do not.
9237 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9238 __isl_keep isl_basic_set *bset)
9240 return isl_map_basic_map_check_equal_space(set_to_map(set),
9241 bset_to_bmap(bset));
9244 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9246 isl_bool is_subset;
9248 if (!map1 || !map2)
9249 return isl_bool_error;
9250 is_subset = isl_map_is_subset(map1, map2);
9251 if (is_subset != isl_bool_true)
9252 return is_subset;
9253 is_subset = isl_map_is_subset(map2, map1);
9254 return is_subset;
9257 /* Is "map1" equal to "map2"?
9259 * First check if they are obviously equal.
9260 * If not, then perform a more detailed analysis.
9262 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9264 isl_bool equal;
9266 equal = isl_map_plain_is_equal(map1, map2);
9267 if (equal < 0 || equal)
9268 return equal;
9269 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9272 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9273 __isl_keep isl_basic_map *bmap2)
9275 isl_bool is_subset;
9277 if (!bmap1 || !bmap2)
9278 return isl_bool_error;
9279 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9280 if (is_subset != isl_bool_true)
9281 return is_subset;
9282 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9283 return isl_bool_not(is_subset);
9286 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9287 __isl_keep isl_map *map2)
9289 isl_bool is_subset;
9291 if (!map1 || !map2)
9292 return isl_bool_error;
9293 is_subset = isl_map_is_subset(map1, map2);
9294 if (is_subset != isl_bool_true)
9295 return is_subset;
9296 is_subset = isl_map_is_subset(map2, map1);
9297 return isl_bool_not(is_subset);
9300 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9301 __isl_keep isl_set *set2)
9303 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9306 /* Is "bmap" obviously equal to the universe with the same space?
9308 * That is, does it not have any constraints?
9310 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9312 if (!bmap)
9313 return isl_bool_error;
9314 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9317 /* Is "bset" obviously equal to the universe with the same space?
9319 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9321 return isl_basic_map_plain_is_universe(bset);
9324 /* If "c" does not involve any existentially quantified variables,
9325 * then set *univ to false and abort
9327 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9329 isl_bool *univ = user;
9330 isl_size n;
9332 n = isl_constraint_dim(c, isl_dim_div);
9333 if (n < 0)
9334 c = isl_constraint_free(c);
9335 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9336 isl_constraint_free(c);
9337 if (*univ < 0 || !*univ)
9338 return isl_stat_error;
9339 return isl_stat_ok;
9342 /* Is "bmap" equal to the universe with the same space?
9344 * First check if it is obviously equal to the universe.
9345 * If not and if there are any constraints not involving
9346 * existentially quantified variables, then it is certainly
9347 * not equal to the universe.
9348 * Otherwise, check if the universe is a subset of "bmap".
9350 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9352 isl_size n_div;
9353 isl_bool univ;
9354 isl_basic_map *test;
9356 univ = isl_basic_map_plain_is_universe(bmap);
9357 if (univ < 0 || univ)
9358 return univ;
9359 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9360 if (n_div < 0)
9361 return isl_bool_error;
9362 if (n_div == 0)
9363 return isl_bool_false;
9364 univ = isl_bool_true;
9365 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9366 univ)
9367 return isl_bool_error;
9368 if (univ < 0 || !univ)
9369 return univ;
9370 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9371 univ = isl_basic_map_is_subset(test, bmap);
9372 isl_basic_map_free(test);
9373 return univ;
9376 /* Is "bset" equal to the universe with the same space?
9378 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9380 return isl_basic_map_is_universe(bset);
9383 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9385 int i;
9387 if (!map)
9388 return isl_bool_error;
9390 for (i = 0; i < map->n; ++i) {
9391 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9392 if (r < 0 || r)
9393 return r;
9396 return isl_bool_false;
9399 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9401 return isl_map_plain_is_universe(set_to_map(set));
9404 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9406 struct isl_basic_set *bset = NULL;
9407 struct isl_vec *sample = NULL;
9408 isl_bool empty, non_empty;
9410 if (!bmap)
9411 return isl_bool_error;
9413 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9414 return isl_bool_true;
9416 if (isl_basic_map_plain_is_universe(bmap))
9417 return isl_bool_false;
9419 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9420 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9421 copy = isl_basic_map_remove_redundancies(copy);
9422 empty = isl_basic_map_plain_is_empty(copy);
9423 isl_basic_map_free(copy);
9424 return empty;
9427 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9428 if (non_empty < 0)
9429 return isl_bool_error;
9430 if (non_empty)
9431 return isl_bool_false;
9432 isl_vec_free(bmap->sample);
9433 bmap->sample = NULL;
9434 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9435 if (!bset)
9436 return isl_bool_error;
9437 sample = isl_basic_set_sample_vec(bset);
9438 if (!sample)
9439 return isl_bool_error;
9440 empty = sample->size == 0;
9441 isl_vec_free(bmap->sample);
9442 bmap->sample = sample;
9443 if (empty)
9444 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9446 return empty;
9449 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9451 if (!bmap)
9452 return isl_bool_error;
9453 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9456 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9458 if (!bset)
9459 return isl_bool_error;
9460 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9463 /* Is "bmap" known to be non-empty?
9465 * That is, is the cached sample still valid?
9467 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9469 isl_size total;
9471 if (!bmap)
9472 return isl_bool_error;
9473 if (!bmap->sample)
9474 return isl_bool_false;
9475 total = isl_basic_map_dim(bmap, isl_dim_all);
9476 if (total < 0)
9477 return isl_bool_error;
9478 if (bmap->sample->size != 1 + total)
9479 return isl_bool_false;
9480 return isl_basic_map_contains(bmap, bmap->sample);
9483 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9485 return isl_basic_map_is_empty(bset_to_bmap(bset));
9488 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9489 __isl_take isl_basic_map *bmap2)
9491 struct isl_map *map;
9493 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9494 goto error;
9496 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9497 if (!map)
9498 goto error;
9499 map = isl_map_add_basic_map(map, bmap1);
9500 map = isl_map_add_basic_map(map, bmap2);
9501 return map;
9502 error:
9503 isl_basic_map_free(bmap1);
9504 isl_basic_map_free(bmap2);
9505 return NULL;
9508 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9509 __isl_take isl_basic_set *bset2)
9511 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9512 bset_to_bmap(bset2)));
9515 /* Order divs such that any div only depends on previous divs */
9516 __isl_give isl_basic_map *isl_basic_map_order_divs(
9517 __isl_take isl_basic_map *bmap)
9519 int i;
9520 isl_size off;
9522 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9523 if (off < 0)
9524 return isl_basic_map_free(bmap);
9526 for (i = 0; i < bmap->n_div; ++i) {
9527 int pos;
9528 if (isl_int_is_zero(bmap->div[i][0]))
9529 continue;
9530 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9531 bmap->n_div-i);
9532 if (pos == -1)
9533 continue;
9534 if (pos == 0)
9535 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9536 "integer division depends on itself",
9537 return isl_basic_map_free(bmap));
9538 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9539 if (!bmap)
9540 return NULL;
9541 --i;
9543 return bmap;
9546 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9548 int i;
9550 if (!map)
9551 return 0;
9553 for (i = 0; i < map->n; ++i) {
9554 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9555 if (!map->p[i])
9556 goto error;
9559 return map;
9560 error:
9561 isl_map_free(map);
9562 return NULL;
9565 /* Sort the local variables of "bset".
9567 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9568 __isl_take isl_basic_set *bset)
9570 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9573 /* Apply the expansion computed by isl_merge_divs.
9574 * The expansion itself is given by "exp" while the resulting
9575 * list of divs is given by "div".
9577 * Move the integer divisions of "bmap" into the right position
9578 * according to "exp" and then introduce the additional integer
9579 * divisions, adding div constraints.
9580 * The moving should be done first to avoid moving coefficients
9581 * in the definitions of the extra integer divisions.
9583 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9584 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9586 int i, j;
9587 int n_div;
9589 bmap = isl_basic_map_cow(bmap);
9590 if (!bmap || !div)
9591 goto error;
9593 if (div->n_row < bmap->n_div)
9594 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9595 "not an expansion", goto error);
9597 n_div = bmap->n_div;
9598 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9599 2 * (div->n_row - n_div));
9601 for (i = n_div; i < div->n_row; ++i)
9602 if (isl_basic_map_alloc_div(bmap) < 0)
9603 goto error;
9605 for (j = n_div - 1; j >= 0; --j) {
9606 if (exp[j] == j)
9607 break;
9608 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9609 if (!bmap)
9610 goto error;
9612 j = 0;
9613 for (i = 0; i < div->n_row; ++i) {
9614 if (j < n_div && exp[j] == i) {
9615 j++;
9616 } else {
9617 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9618 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9619 continue;
9620 bmap = isl_basic_map_add_div_constraints(bmap, i);
9621 if (!bmap)
9622 goto error;
9626 isl_mat_free(div);
9627 return bmap;
9628 error:
9629 isl_basic_map_free(bmap);
9630 isl_mat_free(div);
9631 return NULL;
9634 /* Apply the expansion computed by isl_merge_divs.
9635 * The expansion itself is given by "exp" while the resulting
9636 * list of divs is given by "div".
9638 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9639 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9641 return isl_basic_map_expand_divs(bset, div, exp);
9644 /* Look for a div in dst that corresponds to the div "div" in src.
9645 * The divs before "div" in src and dst are assumed to be the same.
9647 * Return the position of the corresponding div in dst
9648 * if there is one. Otherwise, return a position beyond the integer divisions.
9649 * Return -1 on error.
9651 static int find_div(__isl_keep isl_basic_map *dst,
9652 __isl_keep isl_basic_map *src, unsigned div)
9654 int i;
9655 isl_size n_div;
9656 isl_size v_div;
9658 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9659 n_div = isl_basic_map_dim(dst, isl_dim_div);
9660 if (n_div < 0 || v_div < 0)
9661 return -1;
9662 isl_assert(dst->ctx, div <= n_div, return -1);
9663 for (i = div; i < n_div; ++i)
9664 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9665 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9666 n_div - div) == -1)
9667 return i;
9668 return n_div;
9671 /* Align the divs of "dst" to those of "src", adding divs from "src"
9672 * if needed. That is, make sure that the first src->n_div divs
9673 * of the result are equal to those of src.
9674 * The integer division of "src" are assumed to be ordered.
9676 * The integer divisions are swapped into the right position
9677 * (possibly after adding them first). This may result
9678 * in the remaining integer divisions appearing in the wrong order,
9679 * i.e., with some integer division appearing before
9680 * some other integer division on which it depends.
9681 * The integer divisions therefore need to be ordered.
9682 * This will not affect the integer divisions aligned to those of "src",
9683 * since "src" is assumed to have ordered integer divisions.
9685 * The result is not finalized as by design it will have redundant
9686 * divs if any divs from "src" were copied.
9688 __isl_give isl_basic_map *isl_basic_map_align_divs(
9689 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9691 int i;
9692 isl_bool known;
9693 int extended;
9694 isl_size v_div;
9695 isl_size dst_n_div;
9697 if (!dst || !src)
9698 return isl_basic_map_free(dst);
9700 if (src->n_div == 0)
9701 return dst;
9703 known = isl_basic_map_divs_known(src);
9704 if (known < 0)
9705 return isl_basic_map_free(dst);
9706 if (!known)
9707 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9708 "some src divs are unknown",
9709 return isl_basic_map_free(dst));
9711 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9712 if (v_div < 0)
9713 return isl_basic_map_free(dst);
9715 extended = 0;
9716 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9717 if (dst_n_div < 0)
9718 dst = isl_basic_map_free(dst);
9719 for (i = 0; i < src->n_div; ++i) {
9720 int j = find_div(dst, src, i);
9721 if (j < 0)
9722 dst = isl_basic_map_free(dst);
9723 if (j == dst_n_div) {
9724 if (!extended) {
9725 int extra = src->n_div - i;
9726 dst = isl_basic_map_cow(dst);
9727 if (!dst)
9728 return isl_basic_map_free(dst);
9729 dst = isl_basic_map_extend(dst,
9730 extra, 0, 2 * extra);
9731 extended = 1;
9733 j = isl_basic_map_alloc_div(dst);
9734 if (j < 0)
9735 return isl_basic_map_free(dst);
9736 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9737 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9738 dst_n_div++;
9739 dst = isl_basic_map_add_div_constraints(dst, j);
9740 if (!dst)
9741 return isl_basic_map_free(dst);
9743 if (j != i)
9744 dst = isl_basic_map_swap_div(dst, i, j);
9745 if (!dst)
9746 return isl_basic_map_free(dst);
9748 return isl_basic_map_order_divs(dst);
9751 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9753 int i;
9755 if (!map)
9756 return NULL;
9757 if (map->n == 0)
9758 return map;
9759 map = isl_map_compute_divs(map);
9760 map = isl_map_order_divs(map);
9761 map = isl_map_cow(map);
9762 if (!map)
9763 return NULL;
9765 for (i = 1; i < map->n; ++i)
9766 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9767 for (i = 1; i < map->n; ++i) {
9768 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9769 if (!map->p[i])
9770 return isl_map_free(map);
9773 map = isl_map_unmark_normalized(map);
9774 return map;
9777 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9779 return isl_map_align_divs_internal(map);
9782 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9784 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9787 /* Align the divs of the basic maps in "map" to those
9788 * of the basic maps in "list", as well as to the other basic maps in "map".
9789 * The elements in "list" are assumed to have known divs.
9791 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9792 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9794 int i;
9795 isl_size n;
9797 n = isl_basic_map_list_n_basic_map(list);
9798 map = isl_map_compute_divs(map);
9799 map = isl_map_cow(map);
9800 if (!map || n < 0)
9801 return isl_map_free(map);
9802 if (map->n == 0)
9803 return map;
9805 for (i = 0; i < n; ++i) {
9806 isl_basic_map *bmap;
9808 bmap = isl_basic_map_list_get_basic_map(list, i);
9809 bmap = isl_basic_map_order_divs(bmap);
9810 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9811 isl_basic_map_free(bmap);
9813 if (!map->p[0])
9814 return isl_map_free(map);
9816 return isl_map_align_divs_internal(map);
9819 /* Align the divs of each element of "list" to those of "bmap".
9820 * Both "bmap" and the elements of "list" are assumed to have known divs.
9822 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9823 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9825 int i;
9826 isl_size n;
9828 n = isl_basic_map_list_n_basic_map(list);
9829 if (n < 0 || !bmap)
9830 return isl_basic_map_list_free(list);
9832 for (i = 0; i < n; ++i) {
9833 isl_basic_map *bmap_i;
9835 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9836 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9837 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9840 return list;
9843 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9844 __isl_take isl_map *map)
9846 isl_bool ok;
9848 isl_map_align_params_set(&map, &set);
9849 ok = isl_map_compatible_domain(map, set);
9850 if (ok < 0)
9851 goto error;
9852 if (!ok)
9853 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9854 "incompatible spaces", goto error);
9855 map = isl_map_intersect_domain(map, set);
9856 set = isl_map_range(map);
9857 return set;
9858 error:
9859 isl_set_free(set);
9860 isl_map_free(map);
9861 return NULL;
9864 /* There is no need to cow as removing empty parts doesn't change
9865 * the meaning of the set.
9867 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9869 int i;
9871 if (!map)
9872 return NULL;
9874 for (i = map->n - 1; i >= 0; --i)
9875 map = remove_if_empty(map, i);
9877 return map;
9880 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9882 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9885 /* Create a binary relation that maps the shared initial "pos" dimensions
9886 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9888 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9889 __isl_keep isl_basic_set *bset2, int pos)
9891 isl_basic_map *bmap1;
9892 isl_basic_map *bmap2;
9894 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9895 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9896 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9897 isl_dim_out, 0, pos);
9898 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9899 isl_dim_out, 0, pos);
9900 return isl_basic_map_range_product(bmap1, bmap2);
9903 /* Given two basic sets bset1 and bset2, compute the maximal difference
9904 * between the values of dimension pos in bset1 and those in bset2
9905 * for any common value of the parameters and dimensions preceding pos.
9907 static enum isl_lp_result basic_set_maximal_difference_at(
9908 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9909 int pos, isl_int *opt)
9911 isl_basic_map *bmap1;
9912 struct isl_ctx *ctx;
9913 struct isl_vec *obj;
9914 isl_size total;
9915 isl_size nparam;
9916 isl_size dim1;
9917 enum isl_lp_result res;
9919 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9920 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9921 if (nparam < 0 || dim1 < 0 || !bset2)
9922 return isl_lp_error;
9924 bmap1 = join_initial(bset1, bset2, pos);
9925 total = isl_basic_map_dim(bmap1, isl_dim_all);
9926 if (total < 0)
9927 return isl_lp_error;
9929 ctx = bmap1->ctx;
9930 obj = isl_vec_alloc(ctx, 1 + total);
9931 if (!obj)
9932 goto error;
9933 isl_seq_clr(obj->block.data, 1 + total);
9934 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9935 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9936 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9937 opt, NULL, NULL);
9938 isl_basic_map_free(bmap1);
9939 isl_vec_free(obj);
9940 return res;
9941 error:
9942 isl_basic_map_free(bmap1);
9943 return isl_lp_error;
9946 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9947 * for any common value of the parameters and dimensions preceding pos
9948 * in both basic sets, the values of dimension pos in bset1 are
9949 * smaller or larger than those in bset2.
9951 * Returns
9952 * 1 if bset1 follows bset2
9953 * -1 if bset1 precedes bset2
9954 * 0 if bset1 and bset2 are incomparable
9955 * -2 if some error occurred.
9957 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9958 __isl_keep isl_basic_set *bset2, int pos)
9960 isl_int opt;
9961 enum isl_lp_result res;
9962 int cmp;
9964 isl_int_init(opt);
9966 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9968 if (res == isl_lp_empty)
9969 cmp = 0;
9970 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9971 res == isl_lp_unbounded)
9972 cmp = 1;
9973 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9974 cmp = -1;
9975 else
9976 cmp = -2;
9978 isl_int_clear(opt);
9979 return cmp;
9982 /* Given two basic sets bset1 and bset2, check whether
9983 * for any common value of the parameters and dimensions preceding pos
9984 * there is a value of dimension pos in bset1 that is larger
9985 * than a value of the same dimension in bset2.
9987 * Return
9988 * 1 if there exists such a pair
9989 * 0 if there is no such pair, but there is a pair of equal values
9990 * -1 otherwise
9991 * -2 if some error occurred.
9993 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9994 __isl_keep isl_basic_set *bset2, int pos)
9996 isl_bool empty;
9997 isl_basic_map *bmap;
9998 isl_size dim1;
10000 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10001 if (dim1 < 0)
10002 return -2;
10003 bmap = join_initial(bset1, bset2, pos);
10004 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10005 isl_dim_out, dim1 - pos);
10006 empty = isl_basic_map_is_empty(bmap);
10007 if (empty < 0)
10008 goto error;
10009 if (empty) {
10010 isl_basic_map_free(bmap);
10011 return -1;
10013 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10014 isl_dim_out, dim1 - pos);
10015 empty = isl_basic_map_is_empty(bmap);
10016 if (empty < 0)
10017 goto error;
10018 isl_basic_map_free(bmap);
10019 if (empty)
10020 return 0;
10021 return 1;
10022 error:
10023 isl_basic_map_free(bmap);
10024 return -2;
10027 /* Given two sets set1 and set2, check whether
10028 * for any common value of the parameters and dimensions preceding pos
10029 * there is a value of dimension pos in set1 that is larger
10030 * than a value of the same dimension in set2.
10032 * Return
10033 * 1 if there exists such a pair
10034 * 0 if there is no such pair, but there is a pair of equal values
10035 * -1 otherwise
10036 * -2 if some error occurred.
10038 int isl_set_follows_at(__isl_keep isl_set *set1,
10039 __isl_keep isl_set *set2, int pos)
10041 int i, j;
10042 int follows = -1;
10044 if (!set1 || !set2)
10045 return -2;
10047 for (i = 0; i < set1->n; ++i)
10048 for (j = 0; j < set2->n; ++j) {
10049 int f;
10050 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10051 if (f == 1 || f == -2)
10052 return f;
10053 if (f > follows)
10054 follows = f;
10057 return follows;
10060 static isl_bool isl_basic_map_plain_has_fixed_var(
10061 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10063 int i;
10064 int d;
10065 isl_size total;
10067 total = isl_basic_map_dim(bmap, isl_dim_all);
10068 if (total < 0)
10069 return isl_bool_error;
10070 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10071 for (; d+1 > pos; --d)
10072 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10073 break;
10074 if (d != pos)
10075 continue;
10076 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
10077 return isl_bool_false;
10078 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
10079 return isl_bool_false;
10080 if (!isl_int_is_one(bmap->eq[i][1+d]))
10081 return isl_bool_false;
10082 if (val)
10083 isl_int_neg(*val, bmap->eq[i][0]);
10084 return isl_bool_true;
10086 return isl_bool_false;
10089 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10090 unsigned pos, isl_int *val)
10092 int i;
10093 isl_int v;
10094 isl_int tmp;
10095 isl_bool fixed;
10097 if (!map)
10098 return isl_bool_error;
10099 if (map->n == 0)
10100 return isl_bool_false;
10101 if (map->n == 1)
10102 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10103 isl_int_init(v);
10104 isl_int_init(tmp);
10105 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10106 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10107 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10108 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10109 fixed = isl_bool_false;
10111 if (val)
10112 isl_int_set(*val, v);
10113 isl_int_clear(tmp);
10114 isl_int_clear(v);
10115 return fixed;
10118 static isl_bool isl_basic_set_plain_has_fixed_var(
10119 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10121 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10122 pos, val);
10125 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10126 enum isl_dim_type type, unsigned pos, isl_int *val)
10128 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10129 return isl_bool_error;
10130 return isl_basic_map_plain_has_fixed_var(bmap,
10131 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10134 /* If "bmap" obviously lies on a hyperplane where the given dimension
10135 * has a fixed value, then return that value.
10136 * Otherwise return NaN.
10138 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10139 __isl_keep isl_basic_map *bmap,
10140 enum isl_dim_type type, unsigned pos)
10142 isl_ctx *ctx;
10143 isl_val *v;
10144 isl_bool fixed;
10146 if (!bmap)
10147 return NULL;
10148 ctx = isl_basic_map_get_ctx(bmap);
10149 v = isl_val_alloc(ctx);
10150 if (!v)
10151 return NULL;
10152 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10153 if (fixed < 0)
10154 return isl_val_free(v);
10155 if (fixed) {
10156 isl_int_set_si(v->d, 1);
10157 return v;
10159 isl_val_free(v);
10160 return isl_val_nan(ctx);
10163 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10164 enum isl_dim_type type, unsigned pos, isl_int *val)
10166 if (isl_map_check_range(map, type, pos, 1) < 0)
10167 return isl_bool_error;
10168 return isl_map_plain_has_fixed_var(map,
10169 map_offset(map, type) - 1 + pos, val);
10172 /* If "map" obviously lies on a hyperplane where the given dimension
10173 * has a fixed value, then return that value.
10174 * Otherwise return NaN.
10176 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10177 enum isl_dim_type type, unsigned pos)
10179 isl_ctx *ctx;
10180 isl_val *v;
10181 isl_bool fixed;
10183 if (!map)
10184 return NULL;
10185 ctx = isl_map_get_ctx(map);
10186 v = isl_val_alloc(ctx);
10187 if (!v)
10188 return NULL;
10189 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10190 if (fixed < 0)
10191 return isl_val_free(v);
10192 if (fixed) {
10193 isl_int_set_si(v->d, 1);
10194 return v;
10196 isl_val_free(v);
10197 return isl_val_nan(ctx);
10200 /* If "set" obviously lies on a hyperplane where the given dimension
10201 * has a fixed value, then return that value.
10202 * Otherwise return NaN.
10204 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10205 enum isl_dim_type type, unsigned pos)
10207 return isl_map_plain_get_val_if_fixed(set, type, pos);
10210 /* Return a sequence of values in the same space as "set"
10211 * that are equal to the corresponding set dimensions of "set"
10212 * for those set dimensions that obviously lie on a hyperplane
10213 * where the dimension has a fixed value.
10214 * The other elements are set to NaN.
10216 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10217 __isl_keep isl_set *set)
10219 int i;
10220 isl_size n;
10221 isl_space *space;
10222 isl_multi_val *mv;
10224 space = isl_space_drop_all_params(isl_set_get_space(set));
10225 mv = isl_multi_val_alloc(space);
10226 n = isl_multi_val_size(mv);
10227 if (n < 0)
10228 return isl_multi_val_free(mv);
10230 for (i = 0; i < n; ++i) {
10231 isl_val *v;
10233 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10234 mv = isl_multi_val_set_val(mv, i, v);
10237 return mv;
10240 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10241 * then return this fixed value in *val.
10243 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10244 unsigned dim, isl_int *val)
10246 isl_size nparam;
10248 nparam = isl_basic_set_dim(bset, isl_dim_param);
10249 if (nparam < 0)
10250 return isl_bool_error;
10251 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10254 /* Return -1 if the constraint "c1" should be sorted before "c2"
10255 * and 1 if it should be sorted after "c2".
10256 * Return 0 if the two constraints are the same (up to the constant term).
10258 * In particular, if a constraint involves later variables than another
10259 * then it is sorted after this other constraint.
10260 * uset_gist depends on constraints without existentially quantified
10261 * variables sorting first.
10263 * For constraints that have the same latest variable, those
10264 * with the same coefficient for this latest variable (first in absolute value
10265 * and then in actual value) are grouped together.
10266 * This is useful for detecting pairs of constraints that can
10267 * be chained in their printed representation.
10269 * Finally, within a group, constraints are sorted according to
10270 * their coefficients (excluding the constant term).
10272 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10274 isl_int **c1 = (isl_int **) p1;
10275 isl_int **c2 = (isl_int **) p2;
10276 int l1, l2;
10277 unsigned size = *(unsigned *) arg;
10278 int cmp;
10280 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10281 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10283 if (l1 != l2)
10284 return l1 - l2;
10286 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10287 if (cmp != 0)
10288 return cmp;
10289 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10290 if (cmp != 0)
10291 return -cmp;
10293 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10296 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10297 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10298 * and 0 if the two constraints are the same (up to the constant term).
10300 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10301 isl_int *c1, isl_int *c2)
10303 isl_size total;
10304 unsigned size;
10306 total = isl_basic_map_dim(bmap, isl_dim_all);
10307 if (total < 0)
10308 return -2;
10309 size = total;
10310 return sort_constraint_cmp(&c1, &c2, &size);
10313 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10314 __isl_take isl_basic_map *bmap)
10316 isl_size total;
10317 unsigned size;
10319 if (!bmap)
10320 return NULL;
10321 if (bmap->n_ineq == 0)
10322 return bmap;
10323 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10324 return bmap;
10325 total = isl_basic_map_dim(bmap, isl_dim_all);
10326 if (total < 0)
10327 return isl_basic_map_free(bmap);
10328 size = total;
10329 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10330 &sort_constraint_cmp, &size) < 0)
10331 return isl_basic_map_free(bmap);
10332 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10333 return bmap;
10336 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10337 __isl_take isl_basic_set *bset)
10339 isl_basic_map *bmap = bset_to_bmap(bset);
10340 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10343 __isl_give isl_basic_map *isl_basic_map_normalize(
10344 __isl_take isl_basic_map *bmap)
10346 bmap = isl_basic_map_remove_redundancies(bmap);
10347 bmap = isl_basic_map_sort_constraints(bmap);
10348 return bmap;
10350 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10351 __isl_keep isl_basic_map *bmap2)
10353 int i, cmp;
10354 isl_size total;
10355 isl_space *space1, *space2;
10357 if (!bmap1 || !bmap2)
10358 return -1;
10360 if (bmap1 == bmap2)
10361 return 0;
10362 space1 = isl_basic_map_peek_space(bmap1);
10363 space2 = isl_basic_map_peek_space(bmap2);
10364 cmp = isl_space_cmp(space1, space2);
10365 if (cmp)
10366 return cmp;
10367 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10368 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10369 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10370 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10371 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10372 return 0;
10373 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10374 return 1;
10375 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10376 return -1;
10377 if (bmap1->n_eq != bmap2->n_eq)
10378 return bmap1->n_eq - bmap2->n_eq;
10379 if (bmap1->n_ineq != bmap2->n_ineq)
10380 return bmap1->n_ineq - bmap2->n_ineq;
10381 if (bmap1->n_div != bmap2->n_div)
10382 return bmap1->n_div - bmap2->n_div;
10383 total = isl_basic_map_dim(bmap1, isl_dim_all);
10384 if (total < 0)
10385 return -1;
10386 for (i = 0; i < bmap1->n_eq; ++i) {
10387 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10388 if (cmp)
10389 return cmp;
10391 for (i = 0; i < bmap1->n_ineq; ++i) {
10392 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10393 if (cmp)
10394 return cmp;
10396 for (i = 0; i < bmap1->n_div; ++i) {
10397 isl_bool unknown1, unknown2;
10399 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
10400 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
10401 if (unknown1 < 0 || unknown2 < 0)
10402 return -1;
10403 if (unknown1 && unknown2)
10404 continue;
10405 if (unknown1)
10406 return 1;
10407 if (unknown2)
10408 return -1;
10409 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10410 if (cmp)
10411 return cmp;
10413 return 0;
10416 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10417 __isl_keep isl_basic_set *bset2)
10419 return isl_basic_map_plain_cmp(bset1, bset2);
10422 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10424 int i, cmp;
10426 if (set1 == set2)
10427 return 0;
10428 if (set1->n != set2->n)
10429 return set1->n - set2->n;
10431 for (i = 0; i < set1->n; ++i) {
10432 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10433 if (cmp)
10434 return cmp;
10437 return 0;
10440 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10441 __isl_keep isl_basic_map *bmap2)
10443 if (!bmap1 || !bmap2)
10444 return isl_bool_error;
10445 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10448 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10449 __isl_keep isl_basic_set *bset2)
10451 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10452 bset_to_bmap(bset2));
10455 static int qsort_bmap_cmp(const void *p1, const void *p2)
10457 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10458 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10460 return isl_basic_map_plain_cmp(bmap1, bmap2);
10463 /* Sort the basic maps of "map" and remove duplicate basic maps.
10465 * While removing basic maps, we make sure that the basic maps remain
10466 * sorted because isl_map_normalize expects the basic maps of the result
10467 * to be sorted.
10469 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10471 int i, j;
10473 map = isl_map_remove_empty_parts(map);
10474 if (!map)
10475 return NULL;
10476 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10477 for (i = map->n - 1; i >= 1; --i) {
10478 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10479 continue;
10480 isl_basic_map_free(map->p[i-1]);
10481 for (j = i; j < map->n; ++j)
10482 map->p[j - 1] = map->p[j];
10483 map->n--;
10486 return map;
10489 /* Remove obvious duplicates among the basic maps of "map".
10491 * Unlike isl_map_normalize, this function does not remove redundant
10492 * constraints and only removes duplicates that have exactly the same
10493 * constraints in the input. It does sort the constraints and
10494 * the basic maps to ease the detection of duplicates.
10496 * If "map" has already been normalized or if the basic maps are
10497 * disjoint, then there can be no duplicates.
10499 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10501 int i;
10502 isl_basic_map *bmap;
10504 if (!map)
10505 return NULL;
10506 if (map->n <= 1)
10507 return map;
10508 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10509 return map;
10510 for (i = 0; i < map->n; ++i) {
10511 bmap = isl_basic_map_copy(map->p[i]);
10512 bmap = isl_basic_map_sort_constraints(bmap);
10513 if (!bmap)
10514 return isl_map_free(map);
10515 isl_basic_map_free(map->p[i]);
10516 map->p[i] = bmap;
10519 map = sort_and_remove_duplicates(map);
10520 return map;
10523 /* We normalize in place, but if anything goes wrong we need
10524 * to return NULL, so we need to make sure we don't change the
10525 * meaning of any possible other copies of map.
10527 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10529 int i;
10530 struct isl_basic_map *bmap;
10532 if (!map)
10533 return NULL;
10534 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10535 return map;
10536 for (i = 0; i < map->n; ++i) {
10537 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10538 if (!bmap)
10539 goto error;
10540 isl_basic_map_free(map->p[i]);
10541 map->p[i] = bmap;
10544 map = sort_and_remove_duplicates(map);
10545 if (map)
10546 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10547 return map;
10548 error:
10549 isl_map_free(map);
10550 return NULL;
10553 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10555 return set_from_map(isl_map_normalize(set_to_map(set)));
10558 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10559 __isl_keep isl_map *map2)
10561 int i;
10562 isl_bool equal;
10564 if (!map1 || !map2)
10565 return isl_bool_error;
10567 if (map1 == map2)
10568 return isl_bool_true;
10569 equal = isl_map_has_equal_space(map1, map2);
10570 if (equal < 0 || !equal)
10571 return equal;
10573 map1 = isl_map_copy(map1);
10574 map2 = isl_map_copy(map2);
10575 map1 = isl_map_normalize(map1);
10576 map2 = isl_map_normalize(map2);
10577 if (!map1 || !map2)
10578 goto error;
10579 equal = map1->n == map2->n;
10580 for (i = 0; equal && i < map1->n; ++i) {
10581 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10582 if (equal < 0)
10583 goto error;
10585 isl_map_free(map1);
10586 isl_map_free(map2);
10587 return equal;
10588 error:
10589 isl_map_free(map1);
10590 isl_map_free(map2);
10591 return isl_bool_error;
10594 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10595 __isl_keep isl_set *set2)
10597 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10600 /* Return the basic maps in "map" as a list.
10602 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10603 __isl_keep isl_map *map)
10605 int i;
10606 isl_ctx *ctx;
10607 isl_basic_map_list *list;
10609 if (!map)
10610 return NULL;
10611 ctx = isl_map_get_ctx(map);
10612 list = isl_basic_map_list_alloc(ctx, map->n);
10614 for (i = 0; i < map->n; ++i) {
10615 isl_basic_map *bmap;
10617 bmap = isl_basic_map_copy(map->p[i]);
10618 list = isl_basic_map_list_add(list, bmap);
10621 return list;
10624 /* Return the intersection of the elements in the non-empty list "list".
10625 * All elements are assumed to live in the same space.
10627 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10628 __isl_take isl_basic_map_list *list)
10630 int i;
10631 isl_size n;
10632 isl_basic_map *bmap;
10634 n = isl_basic_map_list_n_basic_map(list);
10635 if (n < 0)
10636 goto error;
10637 if (n < 1)
10638 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10639 "expecting non-empty list", goto error);
10641 bmap = isl_basic_map_list_get_basic_map(list, 0);
10642 for (i = 1; i < n; ++i) {
10643 isl_basic_map *bmap_i;
10645 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10646 bmap = isl_basic_map_intersect(bmap, bmap_i);
10649 isl_basic_map_list_free(list);
10650 return bmap;
10651 error:
10652 isl_basic_map_list_free(list);
10653 return NULL;
10656 /* Return the intersection of the elements in the non-empty list "list".
10657 * All elements are assumed to live in the same space.
10659 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10660 __isl_take isl_basic_set_list *list)
10662 return isl_basic_map_list_intersect(list);
10665 /* Return the union of the elements of "list".
10666 * The list is required to have at least one element.
10668 __isl_give isl_set *isl_basic_set_list_union(
10669 __isl_take isl_basic_set_list *list)
10671 int i;
10672 isl_size n;
10673 isl_space *space;
10674 isl_basic_set *bset;
10675 isl_set *set;
10677 n = isl_basic_set_list_n_basic_set(list);
10678 if (n < 0)
10679 goto error;
10680 if (n < 1)
10681 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10682 "expecting non-empty list", goto error);
10684 bset = isl_basic_set_list_get_basic_set(list, 0);
10685 space = isl_basic_set_get_space(bset);
10686 isl_basic_set_free(bset);
10688 set = isl_set_alloc_space(space, n, 0);
10689 for (i = 0; i < n; ++i) {
10690 bset = isl_basic_set_list_get_basic_set(list, i);
10691 set = isl_set_add_basic_set(set, bset);
10694 isl_basic_set_list_free(list);
10695 return set;
10696 error:
10697 isl_basic_set_list_free(list);
10698 return NULL;
10701 /* Return the union of the elements in the non-empty list "list".
10702 * All elements are assumed to live in the same space.
10704 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10706 int i;
10707 isl_size n;
10708 isl_set *set;
10710 n = isl_set_list_n_set(list);
10711 if (n < 0)
10712 goto error;
10713 if (n < 1)
10714 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10715 "expecting non-empty list", goto error);
10717 set = isl_set_list_get_set(list, 0);
10718 for (i = 1; i < n; ++i) {
10719 isl_set *set_i;
10721 set_i = isl_set_list_get_set(list, i);
10722 set = isl_set_union(set, set_i);
10725 isl_set_list_free(list);
10726 return set;
10727 error:
10728 isl_set_list_free(list);
10729 return NULL;
10732 __isl_give isl_basic_map *isl_basic_map_product(
10733 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10735 isl_space *space_result = NULL;
10736 struct isl_basic_map *bmap;
10737 unsigned in1, in2, out1, out2, nparam, total, pos;
10738 struct isl_dim_map *dim_map1, *dim_map2;
10740 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10741 goto error;
10742 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10743 isl_space_copy(bmap2->dim));
10745 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10746 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10747 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10748 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10749 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10751 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10752 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10753 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10754 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10755 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10756 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10757 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10758 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10759 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10760 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10761 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10763 bmap = isl_basic_map_alloc_space(space_result,
10764 bmap1->n_div + bmap2->n_div,
10765 bmap1->n_eq + bmap2->n_eq,
10766 bmap1->n_ineq + bmap2->n_ineq);
10767 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10768 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10769 bmap = isl_basic_map_simplify(bmap);
10770 return isl_basic_map_finalize(bmap);
10771 error:
10772 isl_basic_map_free(bmap1);
10773 isl_basic_map_free(bmap2);
10774 return NULL;
10777 __isl_give isl_basic_map *isl_basic_map_flat_product(
10778 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10780 isl_basic_map *prod;
10782 prod = isl_basic_map_product(bmap1, bmap2);
10783 prod = isl_basic_map_flatten(prod);
10784 return prod;
10787 __isl_give isl_basic_set *isl_basic_set_flat_product(
10788 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10790 return isl_basic_map_flat_range_product(bset1, bset2);
10793 __isl_give isl_basic_map *isl_basic_map_domain_product(
10794 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10796 isl_space *space1, *space2;
10797 isl_space *space_result = NULL;
10798 isl_basic_map *bmap;
10799 isl_size in1, in2, out, nparam;
10800 unsigned total, pos;
10801 struct isl_dim_map *dim_map1, *dim_map2;
10803 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10804 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10805 out = isl_basic_map_dim(bmap1, isl_dim_out);
10806 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10807 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10808 goto error;
10810 space1 = isl_basic_map_get_space(bmap1);
10811 space2 = isl_basic_map_get_space(bmap2);
10812 space_result = isl_space_domain_product(space1, space2);
10814 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10815 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10816 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10817 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10818 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10819 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10820 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10821 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10822 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10823 isl_dim_map_div(dim_map1, bmap1, pos += out);
10824 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10826 bmap = isl_basic_map_alloc_space(space_result,
10827 bmap1->n_div + bmap2->n_div,
10828 bmap1->n_eq + bmap2->n_eq,
10829 bmap1->n_ineq + bmap2->n_ineq);
10830 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10831 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10832 bmap = isl_basic_map_simplify(bmap);
10833 return isl_basic_map_finalize(bmap);
10834 error:
10835 isl_basic_map_free(bmap1);
10836 isl_basic_map_free(bmap2);
10837 return NULL;
10840 __isl_give isl_basic_map *isl_basic_map_range_product(
10841 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10843 isl_bool rational;
10844 isl_space *space_result = NULL;
10845 isl_basic_map *bmap;
10846 isl_size in, out1, out2, nparam;
10847 unsigned total, pos;
10848 struct isl_dim_map *dim_map1, *dim_map2;
10850 rational = isl_basic_map_is_rational(bmap1);
10851 if (rational >= 0 && rational)
10852 rational = isl_basic_map_is_rational(bmap2);
10853 in = isl_basic_map_dim(bmap1, isl_dim_in);
10854 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10855 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10856 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10857 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10858 goto error;
10860 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10861 goto error;
10863 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10864 isl_space_copy(bmap2->dim));
10866 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10867 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10868 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10869 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10870 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10871 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10872 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10873 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10874 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10875 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10876 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10878 bmap = isl_basic_map_alloc_space(space_result,
10879 bmap1->n_div + bmap2->n_div,
10880 bmap1->n_eq + bmap2->n_eq,
10881 bmap1->n_ineq + bmap2->n_ineq);
10882 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10883 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10884 if (rational)
10885 bmap = isl_basic_map_set_rational(bmap);
10886 bmap = isl_basic_map_simplify(bmap);
10887 return isl_basic_map_finalize(bmap);
10888 error:
10889 isl_basic_map_free(bmap1);
10890 isl_basic_map_free(bmap2);
10891 return NULL;
10894 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10895 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10897 isl_basic_map *prod;
10899 prod = isl_basic_map_range_product(bmap1, bmap2);
10900 prod = isl_basic_map_flatten_range(prod);
10901 return prod;
10904 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10905 * and collect the results.
10906 * The result live in the space obtained by calling "space_product"
10907 * on the spaces of "map1" and "map2".
10908 * If "remove_duplicates" is set then the result may contain duplicates
10909 * (even if the inputs do not) and so we try and remove the obvious
10910 * duplicates.
10912 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10913 __isl_take isl_map *map2,
10914 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10915 __isl_take isl_space *right),
10916 __isl_give isl_basic_map *(*basic_map_product)(
10917 __isl_take isl_basic_map *left,
10918 __isl_take isl_basic_map *right),
10919 int remove_duplicates)
10921 unsigned flags = 0;
10922 struct isl_map *result;
10923 int i, j;
10924 isl_bool m;
10926 m = isl_map_has_equal_params(map1, map2);
10927 if (m < 0)
10928 goto error;
10929 if (!m)
10930 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10931 "parameters don't match", goto error);
10933 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10934 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10935 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10937 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10938 isl_space_copy(map2->dim)),
10939 map1->n * map2->n, flags);
10940 if (!result)
10941 goto error;
10942 for (i = 0; i < map1->n; ++i)
10943 for (j = 0; j < map2->n; ++j) {
10944 struct isl_basic_map *part;
10945 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10946 isl_basic_map_copy(map2->p[j]));
10947 if (isl_basic_map_is_empty(part))
10948 isl_basic_map_free(part);
10949 else
10950 result = isl_map_add_basic_map(result, part);
10951 if (!result)
10952 goto error;
10954 if (remove_duplicates)
10955 result = isl_map_remove_obvious_duplicates(result);
10956 isl_map_free(map1);
10957 isl_map_free(map2);
10958 return result;
10959 error:
10960 isl_map_free(map1);
10961 isl_map_free(map2);
10962 return NULL;
10965 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10967 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10968 __isl_take isl_map *map2)
10970 isl_map_align_params_bin(&map1, &map2);
10971 return map_product(map1, map2, &isl_space_product,
10972 &isl_basic_map_product, 0);
10975 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10977 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10978 __isl_take isl_map *map2)
10980 isl_map *prod;
10982 prod = isl_map_product(map1, map2);
10983 prod = isl_map_flatten(prod);
10984 return prod;
10987 /* Given two set A and B, construct its Cartesian product A x B.
10989 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10990 __isl_take isl_set *set2)
10992 return isl_map_range_product(set1, set2);
10995 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10996 __isl_take isl_set *set2)
10998 return isl_map_flat_range_product(set1, set2);
11001 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11003 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11004 __isl_take isl_map *map2)
11006 isl_map_align_params_bin(&map1, &map2);
11007 return map_product(map1, map2, &isl_space_domain_product,
11008 &isl_basic_map_domain_product, 1);
11011 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11013 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11014 __isl_take isl_map *map2)
11016 isl_map_align_params_bin(&map1, &map2);
11017 return map_product(map1, map2, &isl_space_range_product,
11018 &isl_basic_map_range_product, 1);
11021 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11023 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11025 isl_space *space;
11026 isl_size total1, keep1, total2, keep2;
11028 total1 = isl_map_dim(map, isl_dim_in);
11029 total2 = isl_map_dim(map, isl_dim_out);
11030 if (total1 < 0 || total2 < 0)
11031 return isl_map_free(map);
11032 if (!isl_space_domain_is_wrapping(map->dim) ||
11033 !isl_space_range_is_wrapping(map->dim))
11034 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11035 "not a product", return isl_map_free(map));
11037 space = isl_map_get_space(map);
11038 space = isl_space_factor_domain(space);
11039 keep1 = isl_space_dim(space, isl_dim_in);
11040 keep2 = isl_space_dim(space, isl_dim_out);
11041 if (keep1 < 0 || keep2 < 0)
11042 map = isl_map_free(map);
11043 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11044 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11045 map = isl_map_reset_space(map, space);
11047 return map;
11050 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11052 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11054 isl_space *space;
11055 isl_size total1, keep1, total2, keep2;
11057 total1 = isl_map_dim(map, isl_dim_in);
11058 total2 = isl_map_dim(map, isl_dim_out);
11059 if (total1 < 0 || total2 < 0)
11060 return isl_map_free(map);
11061 if (!isl_space_domain_is_wrapping(map->dim) ||
11062 !isl_space_range_is_wrapping(map->dim))
11063 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11064 "not a product", return isl_map_free(map));
11066 space = isl_map_get_space(map);
11067 space = isl_space_factor_range(space);
11068 keep1 = isl_space_dim(space, isl_dim_in);
11069 keep2 = isl_space_dim(space, isl_dim_out);
11070 if (keep1 < 0 || keep2 < 0)
11071 map = isl_map_free(map);
11072 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11073 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11074 map = isl_map_reset_space(map, space);
11076 return map;
11079 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11081 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11083 isl_space *space;
11084 isl_size total, keep;
11086 total = isl_map_dim(map, isl_dim_in);
11087 if (total < 0)
11088 return isl_map_free(map);
11089 if (!isl_space_domain_is_wrapping(map->dim))
11090 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11091 "domain is not a product", return isl_map_free(map));
11093 space = isl_map_get_space(map);
11094 space = isl_space_domain_factor_domain(space);
11095 keep = isl_space_dim(space, isl_dim_in);
11096 if (keep < 0)
11097 map = isl_map_free(map);
11098 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11099 map = isl_map_reset_space(map, space);
11101 return map;
11104 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11106 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11108 isl_space *space;
11109 isl_size total, keep;
11111 total = isl_map_dim(map, isl_dim_in);
11112 if (total < 0)
11113 return isl_map_free(map);
11114 if (!isl_space_domain_is_wrapping(map->dim))
11115 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11116 "domain is not a product", return isl_map_free(map));
11118 space = isl_map_get_space(map);
11119 space = isl_space_domain_factor_range(space);
11120 keep = isl_space_dim(space, isl_dim_in);
11121 if (keep < 0)
11122 map = isl_map_free(map);
11123 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11124 map = isl_map_reset_space(map, space);
11126 return map;
11129 /* Given a map A -> [B -> C], extract the map A -> B.
11131 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11133 isl_space *space;
11134 isl_size total, keep;
11136 total = isl_map_dim(map, isl_dim_out);
11137 if (total < 0)
11138 return isl_map_free(map);
11139 if (!isl_space_range_is_wrapping(map->dim))
11140 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11141 "range is not a product", return isl_map_free(map));
11143 space = isl_map_get_space(map);
11144 space = isl_space_range_factor_domain(space);
11145 keep = isl_space_dim(space, isl_dim_out);
11146 if (keep < 0)
11147 map = isl_map_free(map);
11148 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11149 map = isl_map_reset_space(map, space);
11151 return map;
11154 /* Given a map A -> [B -> C], extract the map A -> C.
11156 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11158 isl_space *space;
11159 isl_size total, keep;
11161 total = isl_map_dim(map, isl_dim_out);
11162 if (total < 0)
11163 return isl_map_free(map);
11164 if (!isl_space_range_is_wrapping(map->dim))
11165 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11166 "range is not a product", return isl_map_free(map));
11168 space = isl_map_get_space(map);
11169 space = isl_space_range_factor_range(space);
11170 keep = isl_space_dim(space, isl_dim_out);
11171 if (keep < 0)
11172 map = isl_map_free(map);
11173 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11174 map = isl_map_reset_space(map, space);
11176 return map;
11179 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11181 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11182 __isl_take isl_map *map2)
11184 isl_map *prod;
11186 prod = isl_map_domain_product(map1, map2);
11187 prod = isl_map_flatten_domain(prod);
11188 return prod;
11191 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11193 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11194 __isl_take isl_map *map2)
11196 isl_map *prod;
11198 prod = isl_map_range_product(map1, map2);
11199 prod = isl_map_flatten_range(prod);
11200 return prod;
11203 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11205 int i;
11206 uint32_t hash = isl_hash_init();
11207 isl_size total;
11209 if (!bmap)
11210 return 0;
11211 bmap = isl_basic_map_copy(bmap);
11212 bmap = isl_basic_map_normalize(bmap);
11213 total = isl_basic_map_dim(bmap, isl_dim_all);
11214 if (total < 0)
11215 return 0;
11216 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11217 for (i = 0; i < bmap->n_eq; ++i) {
11218 uint32_t c_hash;
11219 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11220 isl_hash_hash(hash, c_hash);
11222 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11223 for (i = 0; i < bmap->n_ineq; ++i) {
11224 uint32_t c_hash;
11225 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11226 isl_hash_hash(hash, c_hash);
11228 isl_hash_byte(hash, bmap->n_div & 0xFF);
11229 for (i = 0; i < bmap->n_div; ++i) {
11230 uint32_t c_hash;
11231 if (isl_int_is_zero(bmap->div[i][0]))
11232 continue;
11233 isl_hash_byte(hash, i & 0xFF);
11234 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11235 isl_hash_hash(hash, c_hash);
11237 isl_basic_map_free(bmap);
11238 return hash;
11241 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11243 return isl_basic_map_get_hash(bset_to_bmap(bset));
11246 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11248 int i;
11249 uint32_t hash;
11251 if (!map)
11252 return 0;
11253 map = isl_map_copy(map);
11254 map = isl_map_normalize(map);
11255 if (!map)
11256 return 0;
11258 hash = isl_hash_init();
11259 for (i = 0; i < map->n; ++i) {
11260 uint32_t bmap_hash;
11261 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11262 isl_hash_hash(hash, bmap_hash);
11265 isl_map_free(map);
11267 return hash;
11270 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11272 return isl_map_get_hash(set_to_map(set));
11275 /* Return the number of basic maps in the (current) representation of "map".
11277 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11279 return map ? map->n : isl_size_error;
11282 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11284 return set ? set->n : isl_size_error;
11287 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11288 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11290 int i;
11292 if (!map)
11293 return isl_stat_error;
11295 for (i = 0; i < map->n; ++i)
11296 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11297 return isl_stat_error;
11299 return isl_stat_ok;
11302 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11303 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11305 int i;
11307 if (!set)
11308 return isl_stat_error;
11310 for (i = 0; i < set->n; ++i)
11311 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11312 return isl_stat_error;
11314 return isl_stat_ok;
11317 /* Does "test" succeed on every basic set in "set"?
11319 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11320 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11321 void *user)
11323 int i;
11325 if (!set)
11326 return isl_bool_error;
11328 for (i = 0; i < set->n; ++i) {
11329 isl_bool r;
11331 r = test(set->p[i], user);
11332 if (r < 0 || !r)
11333 return r;
11336 return isl_bool_true;
11339 /* Return a list of basic sets, the union of which is equal to "set".
11341 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11342 __isl_keep isl_set *set)
11344 int i;
11345 isl_basic_set_list *list;
11347 if (!set)
11348 return NULL;
11350 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11351 for (i = 0; i < set->n; ++i) {
11352 isl_basic_set *bset;
11354 bset = isl_basic_set_copy(set->p[i]);
11355 list = isl_basic_set_list_add(list, bset);
11358 return list;
11361 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11363 isl_space *space;
11365 if (!bset)
11366 return NULL;
11368 bset = isl_basic_set_cow(bset);
11369 if (!bset)
11370 return NULL;
11372 space = isl_basic_set_get_space(bset);
11373 space = isl_space_lift(space, bset->n_div);
11374 if (!space)
11375 goto error;
11376 isl_space_free(bset->dim);
11377 bset->dim = space;
11378 bset->extra -= bset->n_div;
11379 bset->n_div = 0;
11381 bset = isl_basic_set_finalize(bset);
11383 return bset;
11384 error:
11385 isl_basic_set_free(bset);
11386 return NULL;
11389 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11391 int i;
11392 isl_space *space;
11393 unsigned n_div;
11395 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11397 if (!set)
11398 return NULL;
11400 set = isl_set_cow(set);
11401 if (!set)
11402 return NULL;
11404 n_div = set->p[0]->n_div;
11405 space = isl_set_get_space(set);
11406 space = isl_space_lift(space, n_div);
11407 if (!space)
11408 goto error;
11409 isl_space_free(set->dim);
11410 set->dim = space;
11412 for (i = 0; i < set->n; ++i) {
11413 set->p[i] = isl_basic_set_lift(set->p[i]);
11414 if (!set->p[i])
11415 goto error;
11418 return set;
11419 error:
11420 isl_set_free(set);
11421 return NULL;
11424 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11426 isl_size dim;
11427 int size = 0;
11429 dim = isl_basic_set_dim(bset, isl_dim_all);
11430 if (dim < 0)
11431 return -1;
11432 size += bset->n_eq * (1 + dim);
11433 size += bset->n_ineq * (1 + dim);
11434 size += bset->n_div * (2 + dim);
11436 return size;
11439 int isl_set_size(__isl_keep isl_set *set)
11441 int i;
11442 int size = 0;
11444 if (!set)
11445 return -1;
11447 for (i = 0; i < set->n; ++i)
11448 size += isl_basic_set_size(set->p[i]);
11450 return size;
11453 /* Check if there is any lower bound (if lower == 0) and/or upper
11454 * bound (if upper == 0) on the specified dim.
11456 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11457 enum isl_dim_type type, unsigned pos, int lower, int upper)
11459 int i;
11461 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11462 return isl_bool_error;
11464 pos += isl_basic_map_offset(bmap, type);
11466 for (i = 0; i < bmap->n_div; ++i) {
11467 if (isl_int_is_zero(bmap->div[i][0]))
11468 continue;
11469 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11470 return isl_bool_true;
11473 for (i = 0; i < bmap->n_eq; ++i)
11474 if (!isl_int_is_zero(bmap->eq[i][pos]))
11475 return isl_bool_true;
11477 for (i = 0; i < bmap->n_ineq; ++i) {
11478 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11479 if (sgn > 0)
11480 lower = 1;
11481 if (sgn < 0)
11482 upper = 1;
11485 return lower && upper;
11488 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11489 enum isl_dim_type type, unsigned pos)
11491 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11494 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11495 enum isl_dim_type type, unsigned pos)
11497 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11500 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11501 enum isl_dim_type type, unsigned pos)
11503 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11506 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11507 enum isl_dim_type type, unsigned pos)
11509 int i;
11511 if (!map)
11512 return isl_bool_error;
11514 for (i = 0; i < map->n; ++i) {
11515 isl_bool bounded;
11516 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11517 if (bounded < 0 || !bounded)
11518 return bounded;
11521 return isl_bool_true;
11524 /* Return true if the specified dim is involved in both an upper bound
11525 * and a lower bound.
11527 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11528 enum isl_dim_type type, unsigned pos)
11530 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11533 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11535 static isl_bool has_any_bound(__isl_keep isl_map *map,
11536 enum isl_dim_type type, unsigned pos,
11537 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11538 enum isl_dim_type type, unsigned pos))
11540 int i;
11542 if (!map)
11543 return isl_bool_error;
11545 for (i = 0; i < map->n; ++i) {
11546 isl_bool bounded;
11547 bounded = fn(map->p[i], type, pos);
11548 if (bounded < 0 || bounded)
11549 return bounded;
11552 return isl_bool_false;
11555 /* Return 1 if the specified dim is involved in any lower bound.
11557 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11558 enum isl_dim_type type, unsigned pos)
11560 return has_any_bound(set, type, pos,
11561 &isl_basic_map_dim_has_lower_bound);
11564 /* Return 1 if the specified dim is involved in any upper bound.
11566 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11567 enum isl_dim_type type, unsigned pos)
11569 return has_any_bound(set, type, pos,
11570 &isl_basic_map_dim_has_upper_bound);
11573 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11575 static isl_bool has_bound(__isl_keep isl_map *map,
11576 enum isl_dim_type type, unsigned pos,
11577 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11578 enum isl_dim_type type, unsigned pos))
11580 int i;
11582 if (!map)
11583 return isl_bool_error;
11585 for (i = 0; i < map->n; ++i) {
11586 isl_bool bounded;
11587 bounded = fn(map->p[i], type, pos);
11588 if (bounded < 0 || !bounded)
11589 return bounded;
11592 return isl_bool_true;
11595 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11597 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11598 enum isl_dim_type type, unsigned pos)
11600 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11603 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11605 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11606 enum isl_dim_type type, unsigned pos)
11608 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11611 /* For each of the "n" variables starting at "first", determine
11612 * the sign of the variable and put the results in the first "n"
11613 * elements of the array "signs".
11614 * Sign
11615 * 1 means that the variable is non-negative
11616 * -1 means that the variable is non-positive
11617 * 0 means the variable attains both positive and negative values.
11619 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11620 unsigned first, unsigned n, int *signs)
11622 isl_vec *bound = NULL;
11623 struct isl_tab *tab = NULL;
11624 struct isl_tab_undo *snap;
11625 int i;
11626 isl_size total;
11628 total = isl_basic_set_dim(bset, isl_dim_all);
11629 if (total < 0 || !signs)
11630 return isl_stat_error;
11632 bound = isl_vec_alloc(bset->ctx, 1 + total);
11633 tab = isl_tab_from_basic_set(bset, 0);
11634 if (!bound || !tab)
11635 goto error;
11637 isl_seq_clr(bound->el, bound->size);
11638 isl_int_set_si(bound->el[0], -1);
11640 snap = isl_tab_snap(tab);
11641 for (i = 0; i < n; ++i) {
11642 int empty;
11644 isl_int_set_si(bound->el[1 + first + i], -1);
11645 if (isl_tab_add_ineq(tab, bound->el) < 0)
11646 goto error;
11647 empty = tab->empty;
11648 isl_int_set_si(bound->el[1 + first + i], 0);
11649 if (isl_tab_rollback(tab, snap) < 0)
11650 goto error;
11652 if (empty) {
11653 signs[i] = 1;
11654 continue;
11657 isl_int_set_si(bound->el[1 + first + i], 1);
11658 if (isl_tab_add_ineq(tab, bound->el) < 0)
11659 goto error;
11660 empty = tab->empty;
11661 isl_int_set_si(bound->el[1 + first + i], 0);
11662 if (isl_tab_rollback(tab, snap) < 0)
11663 goto error;
11665 signs[i] = empty ? -1 : 0;
11668 isl_tab_free(tab);
11669 isl_vec_free(bound);
11670 return isl_stat_ok;
11671 error:
11672 isl_tab_free(tab);
11673 isl_vec_free(bound);
11674 return isl_stat_error;
11677 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11678 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11680 if (!bset || !signs)
11681 return isl_stat_error;
11682 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11683 return isl_stat_error;
11685 first += pos(bset->dim, type) - 1;
11686 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11689 /* Is it possible for the integer division "div" to depend (possibly
11690 * indirectly) on any output dimensions?
11692 * If the div is undefined, then we conservatively assume that it
11693 * may depend on them.
11694 * Otherwise, we check if it actually depends on them or on any integer
11695 * divisions that may depend on them.
11697 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11699 int i;
11700 isl_size n_out, n_div;
11701 unsigned o_out, o_div;
11703 if (isl_int_is_zero(bmap->div[div][0]))
11704 return isl_bool_true;
11706 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11707 if (n_out < 0)
11708 return isl_bool_error;
11709 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11711 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11712 return isl_bool_true;
11714 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11715 if (n_div < 0)
11716 return isl_bool_error;
11717 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11719 for (i = 0; i < n_div; ++i) {
11720 isl_bool may_involve;
11722 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11723 continue;
11724 may_involve = div_may_involve_output(bmap, i);
11725 if (may_involve < 0 || may_involve)
11726 return may_involve;
11729 return isl_bool_false;
11732 /* Return the first integer division of "bmap" in the range
11733 * [first, first + n[ that may depend on any output dimensions and
11734 * that has a non-zero coefficient in "c" (where the first coefficient
11735 * in "c" corresponds to integer division "first").
11737 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11738 isl_int *c, int first, int n)
11740 int k;
11742 if (!bmap)
11743 return -1;
11745 for (k = first; k < first + n; ++k) {
11746 isl_bool may_involve;
11748 if (isl_int_is_zero(c[k]))
11749 continue;
11750 may_involve = div_may_involve_output(bmap, k);
11751 if (may_involve < 0)
11752 return -1;
11753 if (may_involve)
11754 return k;
11757 return first + n;
11760 /* Look for a pair of inequality constraints in "bmap" of the form
11762 * -l + i >= 0 or i >= l
11763 * and
11764 * n + l - i >= 0 or i <= l + n
11766 * with n < "m" and i the output dimension at position "pos".
11767 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11768 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11769 * and earlier output dimensions, as well as integer divisions that do
11770 * not involve any of the output dimensions.
11772 * Return the index of the first inequality constraint or bmap->n_ineq
11773 * if no such pair can be found.
11775 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11776 int pos, isl_int m)
11778 int i, j;
11779 isl_ctx *ctx;
11780 isl_size total;
11781 isl_size n_div, n_out;
11782 unsigned o_div, o_out;
11783 int less;
11785 total = isl_basic_map_dim(bmap, isl_dim_all);
11786 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11787 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11788 if (total < 0 || n_out < 0 || n_div < 0)
11789 return -1;
11791 ctx = isl_basic_map_get_ctx(bmap);
11792 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11793 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11794 for (i = 0; i < bmap->n_ineq; ++i) {
11795 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11796 continue;
11797 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11798 n_out - (pos + 1)) != -1)
11799 continue;
11800 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11801 0, n_div) < n_div)
11802 continue;
11803 for (j = i + 1; j < bmap->n_ineq; ++j) {
11804 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11805 ctx->one))
11806 continue;
11807 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11808 bmap->ineq[j] + 1, total))
11809 continue;
11810 break;
11812 if (j >= bmap->n_ineq)
11813 continue;
11814 isl_int_add(bmap->ineq[i][0],
11815 bmap->ineq[i][0], bmap->ineq[j][0]);
11816 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11817 isl_int_sub(bmap->ineq[i][0],
11818 bmap->ineq[i][0], bmap->ineq[j][0]);
11819 if (!less)
11820 continue;
11821 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11822 return i;
11823 else
11824 return j;
11827 return bmap->n_ineq;
11830 /* Return the index of the equality of "bmap" that defines
11831 * the output dimension "pos" in terms of earlier dimensions.
11832 * The equality may also involve integer divisions, as long
11833 * as those integer divisions are defined in terms of
11834 * parameters or input dimensions.
11835 * In this case, *div is set to the number of integer divisions and
11836 * *ineq is set to the number of inequality constraints (provided
11837 * div and ineq are not NULL).
11839 * The equality may also involve a single integer division involving
11840 * the output dimensions (typically only output dimension "pos") as
11841 * long as the coefficient of output dimension "pos" is 1 or -1 and
11842 * there is a pair of constraints i >= l and i <= l + n, with i referring
11843 * to output dimension "pos", l an expression involving only earlier
11844 * dimensions and n smaller than the coefficient of the integer division
11845 * in the equality. In this case, the output dimension can be defined
11846 * in terms of a modulo expression that does not involve the integer division.
11847 * *div is then set to this single integer division and
11848 * *ineq is set to the index of constraint i >= l.
11850 * Return bmap->n_eq if there is no such equality.
11851 * Return -1 on error.
11853 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11854 int pos, int *div, int *ineq)
11856 int j, k, l;
11857 isl_size n_div, n_out;
11858 unsigned o_div, o_out;
11860 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11861 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11862 if (n_out < 0 || n_div < 0)
11863 return -1;
11865 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11866 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11868 if (ineq)
11869 *ineq = bmap->n_ineq;
11870 if (div)
11871 *div = n_div;
11872 for (j = 0; j < bmap->n_eq; ++j) {
11873 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11874 continue;
11875 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11876 n_out - (pos + 1)) != -1)
11877 continue;
11878 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11879 0, n_div);
11880 if (k >= n_div)
11881 return j;
11882 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11883 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11884 continue;
11885 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11886 k + 1, n_div - (k+1)) < n_div)
11887 continue;
11888 l = find_modulo_constraint_pair(bmap, pos,
11889 bmap->eq[j][o_div + k]);
11890 if (l < 0)
11891 return -1;
11892 if (l >= bmap->n_ineq)
11893 continue;
11894 if (div)
11895 *div = k;
11896 if (ineq)
11897 *ineq = l;
11898 return j;
11901 return bmap->n_eq;
11904 /* Check if the given basic map is obviously single-valued.
11905 * In particular, for each output dimension, check that there is
11906 * an equality that defines the output dimension in terms of
11907 * earlier dimensions.
11909 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11911 int i;
11912 isl_size n_out;
11914 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11915 if (n_out < 0)
11916 return isl_bool_error;
11918 for (i = 0; i < n_out; ++i) {
11919 int eq;
11921 eq = isl_basic_map_output_defining_equality(bmap, i,
11922 NULL, NULL);
11923 if (eq < 0)
11924 return isl_bool_error;
11925 if (eq >= bmap->n_eq)
11926 return isl_bool_false;
11929 return isl_bool_true;
11932 /* Check if the given basic map is single-valued.
11933 * We simply compute
11935 * M \circ M^-1
11937 * and check if the result is a subset of the identity mapping.
11939 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11941 isl_space *space;
11942 isl_basic_map *test;
11943 isl_basic_map *id;
11944 isl_bool sv;
11946 sv = isl_basic_map_plain_is_single_valued(bmap);
11947 if (sv < 0 || sv)
11948 return sv;
11950 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11951 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11953 space = isl_basic_map_get_space(bmap);
11954 space = isl_space_map_from_set(isl_space_range(space));
11955 id = isl_basic_map_identity(space);
11957 sv = isl_basic_map_is_subset(test, id);
11959 isl_basic_map_free(test);
11960 isl_basic_map_free(id);
11962 return sv;
11965 /* Check if the given map is obviously single-valued.
11967 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11969 if (!map)
11970 return isl_bool_error;
11971 if (map->n == 0)
11972 return isl_bool_true;
11973 if (map->n >= 2)
11974 return isl_bool_false;
11976 return isl_basic_map_plain_is_single_valued(map->p[0]);
11979 /* Check if the given map is single-valued.
11980 * We simply compute
11982 * M \circ M^-1
11984 * and check if the result is a subset of the identity mapping.
11986 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11988 isl_space *space;
11989 isl_map *test;
11990 isl_map *id;
11991 isl_bool sv;
11993 sv = isl_map_plain_is_single_valued(map);
11994 if (sv < 0 || sv)
11995 return sv;
11997 test = isl_map_reverse(isl_map_copy(map));
11998 test = isl_map_apply_range(test, isl_map_copy(map));
12000 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12001 id = isl_map_identity(space);
12003 sv = isl_map_is_subset(test, id);
12005 isl_map_free(test);
12006 isl_map_free(id);
12008 return sv;
12011 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12013 isl_bool in;
12015 map = isl_map_copy(map);
12016 map = isl_map_reverse(map);
12017 in = isl_map_is_single_valued(map);
12018 isl_map_free(map);
12020 return in;
12023 /* Check if the given map is obviously injective.
12025 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12027 isl_bool in;
12029 map = isl_map_copy(map);
12030 map = isl_map_reverse(map);
12031 in = isl_map_plain_is_single_valued(map);
12032 isl_map_free(map);
12034 return in;
12037 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12039 isl_bool sv;
12041 sv = isl_map_is_single_valued(map);
12042 if (sv < 0 || !sv)
12043 return sv;
12045 return isl_map_is_injective(map);
12048 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12050 return isl_map_is_single_valued(set_to_map(set));
12053 /* Does "map" only map elements to themselves?
12055 * If the domain and range spaces are different, then "map"
12056 * is considered not to be an identity relation, even if it is empty.
12057 * Otherwise, construct the maximal identity relation and
12058 * check whether "map" is a subset of this relation.
12060 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12062 isl_map *id;
12063 isl_bool equal, is_identity;
12065 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12066 if (equal < 0 || !equal)
12067 return equal;
12069 id = isl_map_identity(isl_map_get_space(map));
12070 is_identity = isl_map_is_subset(map, id);
12071 isl_map_free(id);
12073 return is_identity;
12076 int isl_map_is_translation(__isl_keep isl_map *map)
12078 int ok;
12079 isl_set *delta;
12081 delta = isl_map_deltas(isl_map_copy(map));
12082 ok = isl_set_is_singleton(delta);
12083 isl_set_free(delta);
12085 return ok;
12088 static int unique(isl_int *p, unsigned pos, unsigned len)
12090 if (isl_seq_first_non_zero(p, pos) != -1)
12091 return 0;
12092 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
12093 return 0;
12094 return 1;
12097 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12099 int i, j;
12100 isl_size nvar, n_div;
12101 unsigned ovar;
12103 n_div = isl_basic_set_dim(bset, isl_dim_div);
12104 if (n_div < 0)
12105 return isl_bool_error;
12106 if (n_div != 0)
12107 return isl_bool_false;
12109 nvar = isl_basic_set_dim(bset, isl_dim_set);
12110 if (nvar < 0)
12111 return isl_bool_error;
12112 ovar = isl_space_offset(bset->dim, isl_dim_set);
12113 for (j = 0; j < nvar; ++j) {
12114 int lower = 0, upper = 0;
12115 for (i = 0; i < bset->n_eq; ++i) {
12116 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12117 continue;
12118 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12119 return isl_bool_false;
12120 break;
12122 if (i < bset->n_eq)
12123 continue;
12124 for (i = 0; i < bset->n_ineq; ++i) {
12125 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12126 continue;
12127 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12128 return isl_bool_false;
12129 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12130 lower = 1;
12131 else
12132 upper = 1;
12134 if (!lower || !upper)
12135 return isl_bool_false;
12138 return isl_bool_true;
12141 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12143 if (!set)
12144 return isl_bool_error;
12145 if (set->n != 1)
12146 return isl_bool_false;
12148 return isl_basic_set_is_box(set->p[0]);
12151 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12153 if (!bset)
12154 return isl_bool_error;
12156 return isl_space_is_wrapping(bset->dim);
12159 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12161 if (!set)
12162 return isl_bool_error;
12164 return isl_space_is_wrapping(set->dim);
12167 /* Modify the space of "map" through a call to "change".
12168 * If "can_change" is set (not NULL), then first call it to check
12169 * if the modification is allowed, printing the error message "cannot_change"
12170 * if it is not.
12172 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12173 isl_bool (*can_change)(__isl_keep isl_map *map),
12174 const char *cannot_change,
12175 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12177 isl_bool ok;
12178 isl_space *space;
12180 if (!map)
12181 return NULL;
12183 ok = can_change ? can_change(map) : isl_bool_true;
12184 if (ok < 0)
12185 return isl_map_free(map);
12186 if (!ok)
12187 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12188 return isl_map_free(map));
12190 space = change(isl_map_get_space(map));
12191 map = isl_map_reset_space(map, space);
12193 return map;
12196 /* Is the domain of "map" a wrapped relation?
12198 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12200 if (!map)
12201 return isl_bool_error;
12203 return isl_space_domain_is_wrapping(map->dim);
12206 /* Does "map" have a wrapped relation in both domain and range?
12208 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12210 return isl_space_is_product(isl_map_peek_space(map));
12213 /* Is the range of "map" a wrapped relation?
12215 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12217 if (!map)
12218 return isl_bool_error;
12220 return isl_space_range_is_wrapping(map->dim);
12223 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12225 isl_space *space;
12227 space = isl_basic_map_take_space(bmap);
12228 space = isl_space_wrap(space);
12229 bmap = isl_basic_map_restore_space(bmap, space);
12231 bmap = isl_basic_map_finalize(bmap);
12233 return bset_from_bmap(bmap);
12236 /* Given a map A -> B, return the set (A -> B).
12238 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12240 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12243 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12245 bset = isl_basic_set_cow(bset);
12246 if (!bset)
12247 return NULL;
12249 bset->dim = isl_space_unwrap(bset->dim);
12250 if (!bset->dim)
12251 goto error;
12253 bset = isl_basic_set_finalize(bset);
12255 return bset_to_bmap(bset);
12256 error:
12257 isl_basic_set_free(bset);
12258 return NULL;
12261 /* Given a set (A -> B), return the map A -> B.
12262 * Error out if "set" is not of the form (A -> B).
12264 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12266 return isl_map_change_space(set, &isl_set_is_wrapping,
12267 "not a wrapping set", &isl_space_unwrap);
12270 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12271 enum isl_dim_type type)
12273 isl_space *space;
12275 space = isl_basic_map_take_space(bmap);
12276 space = isl_space_reset(space, type);
12277 bmap = isl_basic_map_restore_space(bmap, space);
12279 bmap = isl_basic_map_mark_final(bmap);
12281 return bmap;
12284 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12285 enum isl_dim_type type)
12287 int i;
12288 isl_space *space;
12290 if (!map)
12291 return NULL;
12293 if (!isl_space_is_named_or_nested(map->dim, type))
12294 return map;
12296 map = isl_map_cow(map);
12297 if (!map)
12298 return NULL;
12300 for (i = 0; i < map->n; ++i) {
12301 map->p[i] = isl_basic_map_reset(map->p[i], type);
12302 if (!map->p[i])
12303 goto error;
12306 space = isl_map_take_space(map);
12307 space = isl_space_reset(space, type);
12308 map = isl_map_restore_space(map, space);
12310 return map;
12311 error:
12312 isl_map_free(map);
12313 return NULL;
12316 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12318 isl_space *space;
12320 space = isl_basic_map_take_space(bmap);
12321 space = isl_space_flatten(space);
12322 bmap = isl_basic_map_restore_space(bmap, space);
12324 bmap = isl_basic_map_mark_final(bmap);
12326 return bmap;
12329 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12331 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12334 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12335 __isl_take isl_basic_map *bmap)
12337 isl_space *space;
12339 space = isl_basic_map_take_space(bmap);
12340 space = isl_space_flatten_domain(space);
12341 bmap = isl_basic_map_restore_space(bmap, space);
12343 bmap = isl_basic_map_mark_final(bmap);
12345 return bmap;
12348 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12349 __isl_take isl_basic_map *bmap)
12351 isl_space *space;
12353 space = isl_basic_map_take_space(bmap);
12354 space = isl_space_flatten_range(space);
12355 bmap = isl_basic_map_restore_space(bmap, space);
12357 bmap = isl_basic_map_mark_final(bmap);
12359 return bmap;
12362 /* Remove any internal structure from the spaces of domain and range of "map".
12364 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12366 if (!map)
12367 return NULL;
12369 if (!map->dim->nested[0] && !map->dim->nested[1])
12370 return map;
12372 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12375 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12377 return set_from_map(isl_map_flatten(set_to_map(set)));
12380 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12382 isl_space *space, *flat_space;
12383 isl_map *map;
12385 space = isl_set_get_space(set);
12386 flat_space = isl_space_flatten(isl_space_copy(space));
12387 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12388 flat_space));
12389 map = isl_map_intersect_domain(map, set);
12391 return map;
12394 /* Remove any internal structure from the space of the domain of "map".
12396 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12398 if (!map)
12399 return NULL;
12401 if (!map->dim->nested[0])
12402 return map;
12404 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12407 /* Remove any internal structure from the space of the range of "map".
12409 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12411 if (!map)
12412 return NULL;
12414 if (!map->dim->nested[1])
12415 return map;
12417 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12420 /* Reorder the dimensions of "bmap" according to the given dim_map
12421 * and set the dimension specification to "space" and
12422 * perform Gaussian elimination on the result.
12424 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12425 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12427 isl_basic_map *res;
12428 unsigned flags;
12429 isl_size n_div;
12431 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12432 if (n_div < 0 || !space || !dim_map)
12433 goto error;
12435 flags = bmap->flags;
12436 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12437 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12438 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12439 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12440 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12441 if (res)
12442 res->flags = flags;
12443 res = isl_basic_map_gauss(res, NULL);
12444 res = isl_basic_map_finalize(res);
12445 return res;
12446 error:
12447 isl_dim_map_free(dim_map);
12448 isl_basic_map_free(bmap);
12449 isl_space_free(space);
12450 return NULL;
12453 /* Reorder the dimensions of "map" according to given reordering.
12455 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12456 __isl_take isl_reordering *r)
12458 int i;
12459 struct isl_dim_map *dim_map;
12461 map = isl_map_cow(map);
12462 dim_map = isl_dim_map_from_reordering(r);
12463 if (!map || !r || !dim_map)
12464 goto error;
12466 for (i = 0; i < map->n; ++i) {
12467 struct isl_dim_map *dim_map_i;
12468 isl_space *space;
12470 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12472 space = isl_reordering_get_space(r);
12473 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12475 if (!map->p[i])
12476 goto error;
12479 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12480 map = isl_map_unmark_normalized(map);
12482 isl_reordering_free(r);
12483 isl_dim_map_free(dim_map);
12484 return map;
12485 error:
12486 isl_dim_map_free(dim_map);
12487 isl_map_free(map);
12488 isl_reordering_free(r);
12489 return NULL;
12492 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12493 __isl_take isl_reordering *r)
12495 return set_from_map(isl_map_realign(set_to_map(set), r));
12498 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12499 __isl_take isl_space *model)
12501 isl_ctx *ctx;
12502 isl_bool aligned;
12504 if (!map || !model)
12505 goto error;
12507 ctx = isl_space_get_ctx(model);
12508 if (!isl_space_has_named_params(model))
12509 isl_die(ctx, isl_error_invalid,
12510 "model has unnamed parameters", goto error);
12511 if (isl_map_check_named_params(map) < 0)
12512 goto error;
12513 aligned = isl_map_space_has_equal_params(map, model);
12514 if (aligned < 0)
12515 goto error;
12516 if (!aligned) {
12517 isl_reordering *exp;
12519 exp = isl_parameter_alignment_reordering(map->dim, model);
12520 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12521 map = isl_map_realign(map, exp);
12524 isl_space_free(model);
12525 return map;
12526 error:
12527 isl_space_free(model);
12528 isl_map_free(map);
12529 return NULL;
12532 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12533 __isl_take isl_space *model)
12535 return isl_map_align_params(set, model);
12538 /* Align the parameters of "bmap" to those of "model", introducing
12539 * additional parameters if needed.
12541 __isl_give isl_basic_map *isl_basic_map_align_params(
12542 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12544 isl_ctx *ctx;
12545 isl_bool equal_params;
12547 if (!bmap || !model)
12548 goto error;
12550 ctx = isl_space_get_ctx(model);
12551 if (!isl_space_has_named_params(model))
12552 isl_die(ctx, isl_error_invalid,
12553 "model has unnamed parameters", goto error);
12554 if (isl_basic_map_check_named_params(bmap) < 0)
12555 goto error;
12556 equal_params = isl_space_has_equal_params(bmap->dim, model);
12557 if (equal_params < 0)
12558 goto error;
12559 if (!equal_params) {
12560 isl_reordering *exp;
12561 struct isl_dim_map *dim_map;
12563 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12564 exp = isl_reordering_extend_space(exp,
12565 isl_basic_map_get_space(bmap));
12566 dim_map = isl_dim_map_from_reordering(exp);
12567 bmap = isl_basic_map_realign(bmap,
12568 isl_reordering_get_space(exp),
12569 isl_dim_map_extend(dim_map, bmap));
12570 isl_reordering_free(exp);
12571 isl_dim_map_free(dim_map);
12574 isl_space_free(model);
12575 return bmap;
12576 error:
12577 isl_space_free(model);
12578 isl_basic_map_free(bmap);
12579 return NULL;
12582 /* Do "bset" and "space" have the same parameters?
12584 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12585 __isl_keep isl_space *space)
12587 isl_space *bset_space;
12589 bset_space = isl_basic_set_peek_space(bset);
12590 return isl_space_has_equal_params(bset_space, space);
12593 /* Do "map" and "space" have the same parameters?
12595 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12596 __isl_keep isl_space *space)
12598 isl_space *map_space;
12600 map_space = isl_map_peek_space(map);
12601 return isl_space_has_equal_params(map_space, space);
12604 /* Do "set" and "space" have the same parameters?
12606 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12607 __isl_keep isl_space *space)
12609 return isl_map_space_has_equal_params(set_to_map(set), space);
12612 /* Align the parameters of "bset" to those of "model", introducing
12613 * additional parameters if needed.
12615 __isl_give isl_basic_set *isl_basic_set_align_params(
12616 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12618 return isl_basic_map_align_params(bset, model);
12621 /* Drop all parameters not referenced by "map".
12623 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12625 int i;
12626 isl_size n;
12628 n = isl_map_dim(map, isl_dim_param);
12629 if (isl_map_check_named_params(map) < 0 || n < 0)
12630 return isl_map_free(map);
12632 for (i = n - 1; i >= 0; i--) {
12633 isl_bool involves;
12635 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12636 if (involves < 0)
12637 return isl_map_free(map);
12638 if (!involves)
12639 map = isl_map_project_out(map, isl_dim_param, i, 1);
12642 return map;
12645 /* Drop all parameters not referenced by "set".
12647 __isl_give isl_set *isl_set_drop_unused_params(
12648 __isl_take isl_set *set)
12650 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12653 /* Drop all parameters not referenced by "bmap".
12655 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12656 __isl_take isl_basic_map *bmap)
12658 isl_size nparam;
12659 int i;
12661 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12662 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12663 return isl_basic_map_free(bmap);
12665 for (i = nparam - 1; i >= 0; i--) {
12666 isl_bool involves;
12668 involves = isl_basic_map_involves_dims(bmap,
12669 isl_dim_param, i, 1);
12670 if (involves < 0)
12671 return isl_basic_map_free(bmap);
12672 if (!involves)
12673 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12676 return bmap;
12679 /* Drop all parameters not referenced by "bset".
12681 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12682 __isl_take isl_basic_set *bset)
12684 return bset_from_bmap(isl_basic_map_drop_unused_params(
12685 bset_to_bmap(bset)));
12688 /* Given a tuple of identifiers "tuple" in a space that corresponds
12689 * to that of "set", if any of those identifiers appear as parameters
12690 * in "set", then equate those parameters with the corresponding
12691 * set dimensions and project out the parameters.
12692 * The result therefore has no such parameters.
12694 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12695 __isl_keep isl_multi_id *tuple)
12697 int i;
12698 isl_size n;
12699 isl_space *set_space, *tuple_space;
12701 set_space = isl_set_peek_space(set);
12702 tuple_space = isl_multi_id_peek_space(tuple);
12703 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12704 return isl_set_free(set);
12705 n = isl_multi_id_size(tuple);
12706 if (n < 0)
12707 return isl_set_free(set);
12708 for (i = 0; i < n; ++i) {
12709 isl_id *id;
12710 int pos;
12712 id = isl_multi_id_get_at(tuple, i);
12713 if (!id)
12714 return isl_set_free(set);
12715 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12716 isl_id_free(id);
12717 if (pos < 0)
12718 continue;
12719 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12720 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12722 return set;
12725 /* Bind the set dimensions of "set" to parameters with identifiers
12726 * specified by "tuple", living in the same space as "set".
12728 * If no parameters with these identifiers appear in "set" already,
12729 * then the set dimensions are simply reinterpreted as parameters.
12730 * Otherwise, the parameters are first equated to the corresponding
12731 * set dimensions.
12733 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12734 __isl_take isl_multi_id *tuple)
12736 isl_space *space;
12738 set = equate_params(set, tuple);
12739 space = isl_set_get_space(set);
12740 space = isl_space_bind_set(space, tuple);
12741 isl_multi_id_free(tuple);
12742 set = isl_set_reset_space(set, space);
12744 return set;
12747 /* Given a tuple of identifiers "tuple" in a space that corresponds
12748 * to the domain of "map", if any of those identifiers appear as parameters
12749 * in "map", then equate those parameters with the corresponding
12750 * input dimensions and project out the parameters.
12751 * The result therefore has no such parameters.
12753 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12754 __isl_keep isl_multi_id *tuple)
12756 int i;
12757 isl_size n;
12758 isl_space *map_space, *tuple_space;
12760 map_space = isl_map_peek_space(map);
12761 tuple_space = isl_multi_id_peek_space(tuple);
12762 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12763 return isl_map_free(map);
12764 n = isl_multi_id_size(tuple);
12765 if (n < 0)
12766 return isl_map_free(map);
12767 for (i = 0; i < n; ++i) {
12768 isl_id *id;
12769 int pos;
12771 id = isl_multi_id_get_at(tuple, i);
12772 if (!id)
12773 return isl_map_free(map);
12774 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12775 isl_id_free(id);
12776 if (pos < 0)
12777 continue;
12778 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12779 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12781 return map;
12784 /* Bind the input dimensions of "map" to parameters with identifiers
12785 * specified by "tuple", living in the domain space of "map".
12787 * If no parameters with these identifiers appear in "map" already,
12788 * then the input dimensions are simply reinterpreted as parameters.
12789 * Otherwise, the parameters are first equated to the corresponding
12790 * input dimensions.
12792 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12793 __isl_take isl_multi_id *tuple)
12795 isl_space *space;
12796 isl_set *set;
12798 map = map_equate_params(map, tuple);
12799 space = isl_map_get_space(map);
12800 space = isl_space_bind_map_domain(space, tuple);
12801 isl_multi_id_free(tuple);
12802 set = set_from_map(isl_map_reset_space(map, space));
12804 return set;
12807 /* Bind the output dimensions of "map" to parameters with identifiers
12808 * specified by "tuple", living in the range space of "map".
12810 * Since binding is more easily implemented on the domain,
12811 * bind the input dimensions of the inverse of "map".
12813 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12814 __isl_take isl_multi_id *tuple)
12816 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12819 /* Insert a domain corresponding to "tuple"
12820 * into the nullary or unary relation "set".
12821 * The result has an extra initial tuple and is therefore
12822 * either a unary or binary relation.
12823 * Any parameters with identifiers in "tuple" are reinterpreted
12824 * as the corresponding domain dimensions.
12826 static __isl_give isl_map *unbind_params_insert_domain(
12827 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12829 isl_space *space;
12830 isl_reordering *r;
12832 space = isl_set_peek_space(set);
12833 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12834 isl_multi_id_free(tuple);
12836 return isl_map_realign(set_to_map(set), r);
12839 /* Construct a set with "tuple" as domain from the parameter domain "set".
12840 * Any parameters with identifiers in "tuple" are reinterpreted
12841 * as the corresponding set dimensions.
12843 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12844 __isl_take isl_multi_id *tuple)
12846 isl_bool is_params;
12848 is_params = isl_set_is_params(set);
12849 if (is_params < 0)
12850 set = isl_set_free(set);
12851 else if (!is_params)
12852 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12853 "expecting parameter domain", set = isl_set_free(set));
12854 return set_from_map(unbind_params_insert_domain(set, tuple));
12857 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12859 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12861 isl_bool is_params;
12863 is_params = isl_set_is_params(set);
12864 if (is_params < 0)
12865 return isl_stat_error;
12866 else if (is_params)
12867 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12868 "expecting proper set", return isl_stat_error);
12870 return isl_stat_ok;
12873 /* Construct a map with "domain" as domain and "set" as range.
12874 * Any parameters with identifiers in "domain" are reinterpreted
12875 * as the corresponding domain dimensions.
12877 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12878 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12880 if (isl_set_check_is_set(set) < 0)
12881 set = isl_set_free(set);
12882 return unbind_params_insert_domain(set, domain);
12885 /* Construct a map with "domain" as domain and "set" as range.
12887 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12888 __isl_take isl_space *domain)
12890 isl_size dim;
12891 isl_space *space;
12892 isl_map *map;
12894 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12895 domain = isl_space_free(domain);
12896 dim = isl_space_dim(domain, isl_dim_set);
12897 if (dim < 0)
12898 domain = isl_space_free(domain);
12899 space = isl_set_get_space(set);
12900 domain = isl_space_replace_params(domain, space);
12901 space = isl_space_map_from_domain_and_range(domain, space);
12903 map = isl_map_from_range(set);
12904 map = isl_map_add_dims(map, isl_dim_in, dim);
12905 map = isl_map_reset_space(map, space);
12907 return map;
12910 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12911 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12912 enum isl_dim_type c2, enum isl_dim_type c3,
12913 enum isl_dim_type c4, enum isl_dim_type c5)
12915 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12916 struct isl_mat *mat;
12917 int i, j, k;
12918 int pos;
12919 isl_size total;
12921 total = isl_basic_map_dim(bmap, isl_dim_all);
12922 if (total < 0)
12923 return NULL;
12924 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12925 if (!mat)
12926 return NULL;
12927 for (i = 0; i < bmap->n_eq; ++i)
12928 for (j = 0, pos = 0; j < 5; ++j) {
12929 int off = isl_basic_map_offset(bmap, c[j]);
12930 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12931 if (dim < 0)
12932 return isl_mat_free(mat);
12933 for (k = 0; k < dim; ++k) {
12934 isl_int_set(mat->row[i][pos],
12935 bmap->eq[i][off + k]);
12936 ++pos;
12940 return mat;
12943 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12944 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12945 enum isl_dim_type c2, enum isl_dim_type c3,
12946 enum isl_dim_type c4, enum isl_dim_type c5)
12948 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12949 struct isl_mat *mat;
12950 int i, j, k;
12951 int pos;
12952 isl_size total;
12954 total = isl_basic_map_dim(bmap, isl_dim_all);
12955 if (total < 0)
12956 return NULL;
12957 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12958 if (!mat)
12959 return NULL;
12960 for (i = 0; i < bmap->n_ineq; ++i)
12961 for (j = 0, pos = 0; j < 5; ++j) {
12962 int off = isl_basic_map_offset(bmap, c[j]);
12963 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12964 if (dim < 0)
12965 return isl_mat_free(mat);
12966 for (k = 0; k < dim; ++k) {
12967 isl_int_set(mat->row[i][pos],
12968 bmap->ineq[i][off + k]);
12969 ++pos;
12973 return mat;
12976 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12977 __isl_take isl_space *space,
12978 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12979 enum isl_dim_type c2, enum isl_dim_type c3,
12980 enum isl_dim_type c4, enum isl_dim_type c5)
12982 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12983 isl_basic_map *bmap = NULL;
12984 isl_size dim;
12985 unsigned total;
12986 unsigned extra;
12987 int i, j, k, l;
12988 int pos;
12990 dim = isl_space_dim(space, isl_dim_all);
12991 if (dim < 0 || !eq || !ineq)
12992 goto error;
12994 if (eq->n_col != ineq->n_col)
12995 isl_die(space->ctx, isl_error_invalid,
12996 "equalities and inequalities matrices should have "
12997 "same number of columns", goto error);
12999 total = 1 + dim;
13001 if (eq->n_col < total)
13002 isl_die(space->ctx, isl_error_invalid,
13003 "number of columns too small", goto error);
13005 extra = eq->n_col - total;
13007 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13008 eq->n_row, ineq->n_row);
13009 if (!bmap)
13010 goto error;
13011 for (i = 0; i < extra; ++i) {
13012 k = isl_basic_map_alloc_div(bmap);
13013 if (k < 0)
13014 goto error;
13015 isl_int_set_si(bmap->div[k][0], 0);
13017 for (i = 0; i < eq->n_row; ++i) {
13018 l = isl_basic_map_alloc_equality(bmap);
13019 if (l < 0)
13020 goto error;
13021 for (j = 0, pos = 0; j < 5; ++j) {
13022 int off = isl_basic_map_offset(bmap, c[j]);
13023 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13024 if (dim < 0)
13025 goto error;
13026 for (k = 0; k < dim; ++k) {
13027 isl_int_set(bmap->eq[l][off + k],
13028 eq->row[i][pos]);
13029 ++pos;
13033 for (i = 0; i < ineq->n_row; ++i) {
13034 l = isl_basic_map_alloc_inequality(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->ineq[l][off + k],
13044 ineq->row[i][pos]);
13045 ++pos;
13050 isl_space_free(space);
13051 isl_mat_free(eq);
13052 isl_mat_free(ineq);
13054 bmap = isl_basic_map_simplify(bmap);
13055 return isl_basic_map_finalize(bmap);
13056 error:
13057 isl_space_free(space);
13058 isl_mat_free(eq);
13059 isl_mat_free(ineq);
13060 isl_basic_map_free(bmap);
13061 return NULL;
13064 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13065 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13066 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13068 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13069 c1, c2, c3, c4, isl_dim_in);
13072 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13073 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13074 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13076 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13077 c1, c2, c3, c4, isl_dim_in);
13080 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13081 __isl_take isl_space *space,
13082 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13083 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13085 isl_basic_map *bmap;
13086 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13087 c1, c2, c3, c4, isl_dim_in);
13088 return bset_from_bmap(bmap);
13091 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13093 if (!bmap)
13094 return isl_bool_error;
13096 return isl_space_can_zip(bmap->dim);
13099 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13101 if (!map)
13102 return isl_bool_error;
13104 return isl_space_can_zip(map->dim);
13107 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13108 * (A -> C) -> (B -> D).
13110 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13112 unsigned pos;
13113 isl_size n_in;
13114 isl_size n1;
13115 isl_size n2;
13117 if (!bmap)
13118 return NULL;
13120 if (!isl_basic_map_can_zip(bmap))
13121 isl_die(bmap->ctx, isl_error_invalid,
13122 "basic map cannot be zipped", goto error);
13123 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13124 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13125 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13126 if (n_in < 0 || n1 < 0 || n2 < 0)
13127 return isl_basic_map_free(bmap);
13128 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13129 bmap = isl_basic_map_cow(bmap);
13130 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13131 if (!bmap)
13132 return NULL;
13133 bmap->dim = isl_space_zip(bmap->dim);
13134 if (!bmap->dim)
13135 goto error;
13136 bmap = isl_basic_map_mark_final(bmap);
13137 return bmap;
13138 error:
13139 isl_basic_map_free(bmap);
13140 return NULL;
13143 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13144 * (A -> C) -> (B -> D).
13146 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13148 if (!map)
13149 return NULL;
13151 if (!isl_map_can_zip(map))
13152 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13153 goto error);
13155 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13156 error:
13157 isl_map_free(map);
13158 return NULL;
13161 /* Can we apply isl_basic_map_curry to "bmap"?
13162 * That is, does it have a nested relation in its domain?
13164 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13166 if (!bmap)
13167 return isl_bool_error;
13169 return isl_space_can_curry(bmap->dim);
13172 /* Can we apply isl_map_curry to "map"?
13173 * That is, does it have a nested relation in its domain?
13175 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13177 if (!map)
13178 return isl_bool_error;
13180 return isl_space_can_curry(map->dim);
13183 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13184 * A -> (B -> C).
13186 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13189 if (!bmap)
13190 return NULL;
13192 if (!isl_basic_map_can_curry(bmap))
13193 isl_die(bmap->ctx, isl_error_invalid,
13194 "basic map cannot be curried", goto error);
13195 bmap = isl_basic_map_cow(bmap);
13196 if (!bmap)
13197 return NULL;
13198 bmap->dim = isl_space_curry(bmap->dim);
13199 if (!bmap->dim)
13200 goto error;
13201 bmap = isl_basic_map_mark_final(bmap);
13202 return bmap;
13203 error:
13204 isl_basic_map_free(bmap);
13205 return NULL;
13208 /* Given a map (A -> B) -> C, return the corresponding map
13209 * A -> (B -> C).
13211 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13213 return isl_map_change_space(map, &isl_map_can_curry,
13214 "map cannot be curried", &isl_space_curry);
13217 /* Can isl_map_range_curry be applied to "map"?
13218 * That is, does it have a nested relation in its range,
13219 * the domain of which is itself a nested relation?
13221 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13223 if (!map)
13224 return isl_bool_error;
13226 return isl_space_can_range_curry(map->dim);
13229 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13230 * A -> (B -> (C -> D)).
13232 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13234 return isl_map_change_space(map, &isl_map_can_range_curry,
13235 "map range cannot be curried",
13236 &isl_space_range_curry);
13239 /* Can we apply isl_basic_map_uncurry to "bmap"?
13240 * That is, does it have a nested relation in its domain?
13242 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13244 if (!bmap)
13245 return isl_bool_error;
13247 return isl_space_can_uncurry(bmap->dim);
13250 /* Can we apply isl_map_uncurry to "map"?
13251 * That is, does it have a nested relation in its domain?
13253 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13255 if (!map)
13256 return isl_bool_error;
13258 return isl_space_can_uncurry(map->dim);
13261 /* Given a basic map A -> (B -> C), return the corresponding basic map
13262 * (A -> B) -> C.
13264 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13267 if (!bmap)
13268 return NULL;
13270 if (!isl_basic_map_can_uncurry(bmap))
13271 isl_die(bmap->ctx, isl_error_invalid,
13272 "basic map cannot be uncurried",
13273 return isl_basic_map_free(bmap));
13274 bmap = isl_basic_map_cow(bmap);
13275 if (!bmap)
13276 return NULL;
13277 bmap->dim = isl_space_uncurry(bmap->dim);
13278 if (!bmap->dim)
13279 return isl_basic_map_free(bmap);
13280 bmap = isl_basic_map_mark_final(bmap);
13281 return bmap;
13284 /* Given a map A -> (B -> C), return the corresponding map
13285 * (A -> B) -> C.
13287 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13289 return isl_map_change_space(map, &isl_map_can_uncurry,
13290 "map cannot be uncurried", &isl_space_uncurry);
13293 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13296 return isl_map_equate(set, type1, pos1, type2, pos2);
13299 /* Construct a basic map where the given dimensions are equal to each other.
13301 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13302 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13304 isl_basic_map *bmap = NULL;
13305 int i;
13306 isl_size total;
13308 total = isl_space_dim(space, isl_dim_all);
13309 if (total < 0 ||
13310 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13311 isl_space_check_range(space, type2, pos2, 1) < 0)
13312 goto error;
13314 if (type1 == type2 && pos1 == pos2)
13315 return isl_basic_map_universe(space);
13317 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13318 i = isl_basic_map_alloc_equality(bmap);
13319 if (i < 0)
13320 goto error;
13321 isl_seq_clr(bmap->eq[i], 1 + total);
13322 pos1 += isl_basic_map_offset(bmap, type1);
13323 pos2 += isl_basic_map_offset(bmap, type2);
13324 isl_int_set_si(bmap->eq[i][pos1], -1);
13325 isl_int_set_si(bmap->eq[i][pos2], 1);
13326 bmap = isl_basic_map_finalize(bmap);
13327 isl_space_free(space);
13328 return bmap;
13329 error:
13330 isl_space_free(space);
13331 isl_basic_map_free(bmap);
13332 return NULL;
13335 /* Add a constraint imposing that the given two dimensions are equal.
13337 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13338 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13340 isl_basic_map *eq;
13342 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13344 bmap = isl_basic_map_intersect(bmap, eq);
13346 return bmap;
13349 /* Add a constraint imposing that the given two dimensions are equal.
13351 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13352 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13354 isl_basic_map *bmap;
13356 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13358 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13360 return map;
13363 /* Add a constraint imposing that the given two dimensions have opposite values.
13365 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13366 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13368 isl_basic_map *bmap = NULL;
13369 int i;
13370 isl_size total;
13372 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13373 return isl_map_free(map);
13374 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13375 return isl_map_free(map);
13377 total = isl_map_dim(map, isl_dim_all);
13378 if (total < 0)
13379 return isl_map_free(map);
13380 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13381 i = isl_basic_map_alloc_equality(bmap);
13382 if (i < 0)
13383 goto error;
13384 isl_seq_clr(bmap->eq[i], 1 + total);
13385 pos1 += isl_basic_map_offset(bmap, type1);
13386 pos2 += isl_basic_map_offset(bmap, type2);
13387 isl_int_set_si(bmap->eq[i][pos1], 1);
13388 isl_int_set_si(bmap->eq[i][pos2], 1);
13389 bmap = isl_basic_map_finalize(bmap);
13391 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13393 return map;
13394 error:
13395 isl_basic_map_free(bmap);
13396 isl_map_free(map);
13397 return NULL;
13400 /* Construct a constraint imposing that the value of the first dimension is
13401 * greater than or equal to that of the second.
13403 static __isl_give isl_constraint *constraint_order_ge(
13404 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13405 enum isl_dim_type type2, int pos2)
13407 isl_constraint *c;
13409 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13410 isl_space_check_range(space, type2, pos2, 1) < 0)
13411 space = isl_space_free(space);
13412 if (!space)
13413 return NULL;
13415 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13417 if (type1 == type2 && pos1 == pos2)
13418 return c;
13420 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13421 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13423 return c;
13426 /* Add a constraint imposing that the value of the first dimension is
13427 * greater than or equal to that of the second.
13429 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13430 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13432 isl_constraint *c;
13433 isl_space *space;
13435 if (type1 == type2 && pos1 == pos2)
13436 return bmap;
13437 space = isl_basic_map_get_space(bmap);
13438 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13439 bmap = isl_basic_map_add_constraint(bmap, c);
13441 return bmap;
13444 /* Add a constraint imposing that the value of the first dimension is
13445 * greater than or equal to that of the second.
13447 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13448 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13450 isl_constraint *c;
13451 isl_space *space;
13453 if (type1 == type2 && pos1 == pos2)
13454 return map;
13455 space = isl_map_get_space(map);
13456 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13457 map = isl_map_add_constraint(map, c);
13459 return map;
13462 /* Add a constraint imposing that the value of the first dimension is
13463 * less than or equal to that of the second.
13465 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13466 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13468 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13471 /* Construct a basic map where the value of the first dimension is
13472 * greater than that of the second.
13474 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13475 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13477 isl_basic_map *bmap = NULL;
13478 int i;
13479 isl_size total;
13481 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13482 isl_space_check_range(space, type2, pos2, 1) < 0)
13483 goto error;
13485 if (type1 == type2 && pos1 == pos2)
13486 return isl_basic_map_empty(space);
13488 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13489 total = isl_basic_map_dim(bmap, isl_dim_all);
13490 i = isl_basic_map_alloc_inequality(bmap);
13491 if (total < 0 || i < 0)
13492 return isl_basic_map_free(bmap);
13493 isl_seq_clr(bmap->ineq[i], 1 + total);
13494 pos1 += isl_basic_map_offset(bmap, type1);
13495 pos2 += isl_basic_map_offset(bmap, type2);
13496 isl_int_set_si(bmap->ineq[i][pos1], 1);
13497 isl_int_set_si(bmap->ineq[i][pos2], -1);
13498 isl_int_set_si(bmap->ineq[i][0], -1);
13499 bmap = isl_basic_map_finalize(bmap);
13501 return bmap;
13502 error:
13503 isl_space_free(space);
13504 isl_basic_map_free(bmap);
13505 return NULL;
13508 /* Add a constraint imposing that the value of the first dimension is
13509 * greater than that of the second.
13511 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13512 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13514 isl_basic_map *gt;
13516 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13518 bmap = isl_basic_map_intersect(bmap, gt);
13520 return bmap;
13523 /* Add a constraint imposing that the value of the first dimension is
13524 * greater than that of the second.
13526 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13527 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13529 isl_basic_map *bmap;
13531 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13533 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13535 return map;
13538 /* Add a constraint imposing that the value of the first dimension is
13539 * smaller than that of the second.
13541 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13542 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13544 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13547 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13548 int pos)
13550 isl_aff *div;
13551 isl_local_space *ls;
13553 if (!bmap)
13554 return NULL;
13556 if (!isl_basic_map_divs_known(bmap))
13557 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13558 "some divs are unknown", return NULL);
13560 ls = isl_basic_map_get_local_space(bmap);
13561 div = isl_local_space_get_div(ls, pos);
13562 isl_local_space_free(ls);
13564 return div;
13567 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13568 int pos)
13570 return isl_basic_map_get_div(bset, pos);
13573 /* Plug in "subs" for set dimension "pos" of "set".
13575 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13576 unsigned pos, __isl_keep isl_aff *subs)
13578 isl_multi_aff *ma;
13580 if (set && isl_set_plain_is_empty(set))
13581 return set;
13583 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13584 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13585 return isl_set_preimage_multi_aff(set, ma);
13588 /* Check if the range of "ma" is compatible with the domain or range
13589 * (depending on "type") of "bmap".
13591 static isl_stat check_basic_map_compatible_range_multi_aff(
13592 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13593 __isl_keep isl_multi_aff *ma)
13595 isl_bool m;
13596 isl_space *ma_space;
13598 ma_space = isl_multi_aff_get_space(ma);
13600 m = isl_space_has_equal_params(bmap->dim, ma_space);
13601 if (m < 0)
13602 goto error;
13603 if (!m)
13604 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13605 "parameters don't match", goto error);
13606 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13607 if (m < 0)
13608 goto error;
13609 if (!m)
13610 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13611 "spaces don't match", goto error);
13613 isl_space_free(ma_space);
13614 return isl_stat_ok;
13615 error:
13616 isl_space_free(ma_space);
13617 return isl_stat_error;
13620 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13621 * coefficients before the transformed range of dimensions,
13622 * the "n_after" coefficients after the transformed range of dimensions
13623 * and the coefficients of the other divs in "bmap".
13625 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13626 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13628 int i;
13629 isl_size n_param;
13630 isl_size n_set;
13631 isl_local_space *ls;
13633 if (n_div == 0)
13634 return bmap;
13636 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13637 n_param = isl_local_space_dim(ls, isl_dim_param);
13638 n_set = isl_local_space_dim(ls, isl_dim_set);
13639 if (n_param < 0 || n_set < 0)
13640 return isl_basic_map_free(bmap);
13642 for (i = 0; i < n_div; ++i) {
13643 int o_bmap = 0, o_ls = 0;
13645 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13646 o_bmap += 1 + 1 + n_param;
13647 o_ls += 1 + 1 + n_param;
13648 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13649 o_bmap += n_before;
13650 isl_seq_cpy(bmap->div[i] + o_bmap,
13651 ls->div->row[i] + o_ls, n_set);
13652 o_bmap += n_set;
13653 o_ls += n_set;
13654 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13655 o_bmap += n_after;
13656 isl_seq_cpy(bmap->div[i] + o_bmap,
13657 ls->div->row[i] + o_ls, n_div);
13658 o_bmap += n_div;
13659 o_ls += n_div;
13660 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13661 bmap = isl_basic_map_add_div_constraints(bmap, i);
13662 if (!bmap)
13663 goto error;
13666 isl_local_space_free(ls);
13667 return bmap;
13668 error:
13669 isl_local_space_free(ls);
13670 return isl_basic_map_free(bmap);
13673 /* How many stride constraints does "ma" enforce?
13674 * That is, how many of the affine expressions have a denominator
13675 * different from one?
13677 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13679 int i;
13680 int strides = 0;
13682 for (i = 0; i < ma->n; ++i)
13683 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13684 strides++;
13686 return strides;
13689 /* For each affine expression in ma of the form
13691 * x_i = (f_i y + h_i)/m_i
13693 * with m_i different from one, add a constraint to "bmap"
13694 * of the form
13696 * f_i y + h_i = m_i alpha_i
13698 * with alpha_i an additional existentially quantified variable.
13700 * The input variables of "ma" correspond to a subset of the variables
13701 * of "bmap". There are "n_before" variables in "bmap" before this
13702 * subset and "n_after" variables after this subset.
13703 * The integer divisions of the affine expressions in "ma" are assumed
13704 * to have been aligned. There are "n_div_ma" of them and
13705 * they appear first in "bmap", straight after the "n_after" variables.
13707 static __isl_give isl_basic_map *add_ma_strides(
13708 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13709 int n_before, int n_after, int n_div_ma)
13711 int i, k;
13712 int div;
13713 isl_size total;
13714 isl_size n_param;
13715 isl_size n_in;
13717 total = isl_basic_map_dim(bmap, isl_dim_all);
13718 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13719 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13720 if (total < 0 || n_param < 0 || n_in < 0)
13721 return isl_basic_map_free(bmap);
13722 for (i = 0; i < ma->n; ++i) {
13723 int o_bmap = 0, o_ma = 1;
13725 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13726 continue;
13727 div = isl_basic_map_alloc_div(bmap);
13728 k = isl_basic_map_alloc_equality(bmap);
13729 if (div < 0 || k < 0)
13730 goto error;
13731 isl_int_set_si(bmap->div[div][0], 0);
13732 isl_seq_cpy(bmap->eq[k] + o_bmap,
13733 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13734 o_bmap += 1 + n_param;
13735 o_ma += 1 + n_param;
13736 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13737 o_bmap += n_before;
13738 isl_seq_cpy(bmap->eq[k] + o_bmap,
13739 ma->u.p[i]->v->el + o_ma, n_in);
13740 o_bmap += n_in;
13741 o_ma += n_in;
13742 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13743 o_bmap += n_after;
13744 isl_seq_cpy(bmap->eq[k] + o_bmap,
13745 ma->u.p[i]->v->el + o_ma, n_div_ma);
13746 o_bmap += n_div_ma;
13747 o_ma += n_div_ma;
13748 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13749 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13750 total++;
13753 return bmap;
13754 error:
13755 isl_basic_map_free(bmap);
13756 return NULL;
13759 /* Replace the domain or range space (depending on "type) of "space" by "set".
13761 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13762 enum isl_dim_type type, __isl_take isl_space *set)
13764 if (type == isl_dim_in) {
13765 space = isl_space_range(space);
13766 space = isl_space_map_from_domain_and_range(set, space);
13767 } else {
13768 space = isl_space_domain(space);
13769 space = isl_space_map_from_domain_and_range(space, set);
13772 return space;
13775 /* Compute the preimage of the domain or range (depending on "type")
13776 * of "bmap" under the function represented by "ma".
13777 * In other words, plug in "ma" in the domain or range of "bmap".
13778 * The result is a basic map that lives in the same space as "bmap"
13779 * except that the domain or range has been replaced by
13780 * the domain space of "ma".
13782 * If bmap is represented by
13784 * A(p) + S u + B x + T v + C(divs) >= 0,
13786 * where u and x are input and output dimensions if type == isl_dim_out
13787 * while x and v are input and output dimensions if type == isl_dim_in,
13788 * and ma is represented by
13790 * x = D(p) + F(y) + G(divs')
13792 * then the result is
13794 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13796 * The divs in the input set are similarly adjusted.
13797 * In particular
13799 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13801 * becomes
13803 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13804 * B_i G(divs') + c_i(divs))/n_i)
13806 * If bmap is not a rational map and if F(y) involves any denominators
13808 * x_i = (f_i y + h_i)/m_i
13810 * then additional constraints are added to ensure that we only
13811 * map back integer points. That is we enforce
13813 * f_i y + h_i = m_i alpha_i
13815 * with alpha_i an additional existentially quantified variable.
13817 * We first copy over the divs from "ma".
13818 * Then we add the modified constraints and divs from "bmap".
13819 * Finally, we add the stride constraints, if needed.
13821 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13822 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13823 __isl_take isl_multi_aff *ma)
13825 int i, k;
13826 isl_space *space;
13827 isl_basic_map *res = NULL;
13828 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13829 isl_int f, c1, c2, g;
13830 isl_bool rational;
13831 int strides;
13833 isl_int_init(f);
13834 isl_int_init(c1);
13835 isl_int_init(c2);
13836 isl_int_init(g);
13838 ma = isl_multi_aff_align_divs(ma);
13839 if (!bmap || !ma)
13840 goto error;
13841 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13842 goto error;
13844 if (type == isl_dim_in) {
13845 n_before = 0;
13846 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13847 } else {
13848 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13849 n_after = 0;
13851 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13852 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13853 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13854 goto error;
13856 space = isl_multi_aff_get_domain_space(ma);
13857 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13858 rational = isl_basic_map_is_rational(bmap);
13859 strides = rational ? 0 : multi_aff_strides(ma);
13860 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13861 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13862 if (rational)
13863 res = isl_basic_map_set_rational(res);
13865 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13866 if (isl_basic_map_alloc_div(res) < 0)
13867 goto error;
13869 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13870 if (!res)
13871 goto error;
13873 for (i = 0; i < bmap->n_eq; ++i) {
13874 k = isl_basic_map_alloc_equality(res);
13875 if (k < 0)
13876 goto error;
13877 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13878 n_after, n_div_ma, n_div_bmap,
13879 f, c1, c2, g, 0) < 0)
13880 goto error;
13883 for (i = 0; i < bmap->n_ineq; ++i) {
13884 k = isl_basic_map_alloc_inequality(res);
13885 if (k < 0)
13886 goto error;
13887 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13888 n_after, n_div_ma, n_div_bmap,
13889 f, c1, c2, g, 0) < 0)
13890 goto error;
13893 for (i = 0; i < bmap->n_div; ++i) {
13894 if (isl_int_is_zero(bmap->div[i][0])) {
13895 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13896 continue;
13898 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13899 n_before, n_after, n_div_ma, n_div_bmap,
13900 f, c1, c2, g, 1) < 0)
13901 goto error;
13904 if (strides)
13905 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13907 isl_int_clear(f);
13908 isl_int_clear(c1);
13909 isl_int_clear(c2);
13910 isl_int_clear(g);
13911 isl_basic_map_free(bmap);
13912 isl_multi_aff_free(ma);
13913 res = isl_basic_map_simplify(res);
13914 return isl_basic_map_finalize(res);
13915 error:
13916 isl_int_clear(f);
13917 isl_int_clear(c1);
13918 isl_int_clear(c2);
13919 isl_int_clear(g);
13920 isl_basic_map_free(bmap);
13921 isl_multi_aff_free(ma);
13922 isl_basic_map_free(res);
13923 return NULL;
13926 /* Compute the preimage of "bset" under the function represented by "ma".
13927 * In other words, plug in "ma" in "bset". The result is a basic set
13928 * that lives in the domain space of "ma".
13930 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13931 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13933 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13936 /* Compute the preimage of the domain of "bmap" under the function
13937 * represented by "ma".
13938 * In other words, plug in "ma" in the domain of "bmap".
13939 * The result is a basic map that lives in the same space as "bmap"
13940 * except that the domain has been replaced by the domain space of "ma".
13942 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13943 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13945 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13948 /* Compute the preimage of the range of "bmap" under the function
13949 * represented by "ma".
13950 * In other words, plug in "ma" in the range of "bmap".
13951 * The result is a basic map that lives in the same space as "bmap"
13952 * except that the range has been replaced by the domain space of "ma".
13954 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13955 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13957 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13960 /* Check if the range of "ma" is compatible with the domain or range
13961 * (depending on "type") of "map".
13962 * Return isl_stat_error if anything is wrong.
13964 static isl_stat check_map_compatible_range_multi_aff(
13965 __isl_keep isl_map *map, enum isl_dim_type type,
13966 __isl_keep isl_multi_aff *ma)
13968 isl_bool m;
13969 isl_space *ma_space;
13971 ma_space = isl_multi_aff_get_space(ma);
13972 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13973 isl_space_free(ma_space);
13974 if (m < 0)
13975 return isl_stat_error;
13976 if (!m)
13977 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13978 "spaces don't match", return isl_stat_error);
13979 return isl_stat_ok;
13982 /* Compute the preimage of the domain or range (depending on "type")
13983 * of "map" under the function represented by "ma".
13984 * In other words, plug in "ma" in the domain or range of "map".
13985 * The result is a map that lives in the same space as "map"
13986 * except that the domain or range has been replaced by
13987 * the domain space of "ma".
13989 * The parameters are assumed to have been aligned.
13991 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13992 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13994 int i;
13995 isl_space *space;
13997 map = isl_map_cow(map);
13998 ma = isl_multi_aff_align_divs(ma);
13999 if (!map || !ma)
14000 goto error;
14001 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14002 goto error;
14004 for (i = 0; i < map->n; ++i) {
14005 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14006 isl_multi_aff_copy(ma));
14007 if (!map->p[i])
14008 goto error;
14011 space = isl_multi_aff_get_domain_space(ma);
14012 space = isl_space_set(isl_map_get_space(map), type, space);
14014 isl_space_free(isl_map_take_space(map));
14015 map = isl_map_restore_space(map, space);
14016 if (!map)
14017 goto error;
14019 isl_multi_aff_free(ma);
14020 if (map->n > 1)
14021 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14022 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14023 return map;
14024 error:
14025 isl_multi_aff_free(ma);
14026 isl_map_free(map);
14027 return NULL;
14030 /* Compute the preimage of the domain or range (depending on "type")
14031 * of "map" under the function represented by "ma".
14032 * In other words, plug in "ma" in the domain or range of "map".
14033 * The result is a map that lives in the same space as "map"
14034 * except that the domain or range has been replaced by
14035 * the domain space of "ma".
14037 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14038 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14040 isl_bool aligned;
14042 if (!map || !ma)
14043 goto error;
14045 aligned = isl_map_space_has_equal_params(map, ma->space);
14046 if (aligned < 0)
14047 goto error;
14048 if (aligned)
14049 return map_preimage_multi_aff(map, type, ma);
14051 if (isl_map_check_named_params(map) < 0)
14052 goto error;
14053 if (!isl_space_has_named_params(ma->space))
14054 isl_die(map->ctx, isl_error_invalid,
14055 "unaligned unnamed parameters", goto error);
14056 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14057 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14059 return map_preimage_multi_aff(map, type, ma);
14060 error:
14061 isl_multi_aff_free(ma);
14062 return isl_map_free(map);
14065 /* Compute the preimage of "set" under the function represented by "ma".
14066 * In other words, plug in "ma" in "set". The result is a set
14067 * that lives in the domain space of "ma".
14069 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14070 __isl_take isl_multi_aff *ma)
14072 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14075 /* Compute the preimage of the domain of "map" under the function
14076 * represented by "ma".
14077 * In other words, plug in "ma" in the domain of "map".
14078 * The result is a map that lives in the same space as "map"
14079 * except that the domain has been replaced by the domain space of "ma".
14081 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14082 __isl_take isl_multi_aff *ma)
14084 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14087 /* Compute the preimage of the range of "map" under the function
14088 * represented by "ma".
14089 * In other words, plug in "ma" in the range of "map".
14090 * The result is a map that lives in the same space as "map"
14091 * except that the range has been replaced by the domain space of "ma".
14093 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14094 __isl_take isl_multi_aff *ma)
14096 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14099 /* Compute the preimage of "map" under the function represented by "pma".
14100 * In other words, plug in "pma" in the domain or range of "map".
14101 * The result is a map that lives in the same space as "map",
14102 * except that the space of type "type" has been replaced by
14103 * the domain space of "pma".
14105 * The parameters of "map" and "pma" are assumed to have been aligned.
14107 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14108 __isl_take isl_map *map, enum isl_dim_type type,
14109 __isl_take isl_pw_multi_aff *pma)
14111 int i;
14112 isl_map *res;
14114 if (!pma)
14115 goto error;
14117 if (pma->n == 0) {
14118 isl_pw_multi_aff_free(pma);
14119 res = isl_map_empty(isl_map_get_space(map));
14120 isl_map_free(map);
14121 return res;
14124 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14125 isl_multi_aff_copy(pma->p[0].maff));
14126 if (type == isl_dim_in)
14127 res = isl_map_intersect_domain(res,
14128 isl_map_copy(pma->p[0].set));
14129 else
14130 res = isl_map_intersect_range(res,
14131 isl_map_copy(pma->p[0].set));
14133 for (i = 1; i < pma->n; ++i) {
14134 isl_map *res_i;
14136 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14137 isl_multi_aff_copy(pma->p[i].maff));
14138 if (type == isl_dim_in)
14139 res_i = isl_map_intersect_domain(res_i,
14140 isl_map_copy(pma->p[i].set));
14141 else
14142 res_i = isl_map_intersect_range(res_i,
14143 isl_map_copy(pma->p[i].set));
14144 res = isl_map_union(res, res_i);
14147 isl_pw_multi_aff_free(pma);
14148 isl_map_free(map);
14149 return res;
14150 error:
14151 isl_pw_multi_aff_free(pma);
14152 isl_map_free(map);
14153 return NULL;
14156 /* Compute the preimage of "map" under the function represented by "pma".
14157 * In other words, plug in "pma" in the domain or range of "map".
14158 * The result is a map that lives in the same space as "map",
14159 * except that the space of type "type" has been replaced by
14160 * the domain space of "pma".
14162 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14163 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14165 isl_bool aligned;
14167 if (!map || !pma)
14168 goto error;
14170 aligned = isl_map_space_has_equal_params(map, pma->dim);
14171 if (aligned < 0)
14172 goto error;
14173 if (aligned)
14174 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14176 if (isl_map_check_named_params(map) < 0)
14177 goto error;
14178 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14179 goto error;
14180 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14181 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14183 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14184 error:
14185 isl_pw_multi_aff_free(pma);
14186 return isl_map_free(map);
14189 /* Compute the preimage of "set" under the function represented by "pma".
14190 * In other words, plug in "pma" in "set". The result is a set
14191 * that lives in the domain space of "pma".
14193 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14194 __isl_take isl_pw_multi_aff *pma)
14196 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14199 /* Compute the preimage of the domain of "map" under the function
14200 * represented by "pma".
14201 * In other words, plug in "pma" in the domain of "map".
14202 * The result is a map that lives in the same space as "map",
14203 * except that domain space has been replaced by the domain space of "pma".
14205 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14206 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14208 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14211 /* Compute the preimage of the range of "map" under the function
14212 * represented by "pma".
14213 * In other words, plug in "pma" in the range of "map".
14214 * The result is a map that lives in the same space as "map",
14215 * except that range space has been replaced by the domain space of "pma".
14217 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14218 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14220 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14223 /* Compute the preimage of "map" under the function represented by "mpa".
14224 * In other words, plug in "mpa" in the domain or range of "map".
14225 * The result is a map that lives in the same space as "map",
14226 * except that the space of type "type" has been replaced by
14227 * the domain space of "mpa".
14229 * If the map does not involve any constraints that refer to the
14230 * dimensions of the substituted space, then the only possible
14231 * effect of "mpa" on the map is to map the space to a different space.
14232 * We create a separate isl_multi_aff to effectuate this change
14233 * in order to avoid spurious splitting of the map along the pieces
14234 * of "mpa".
14235 * If "mpa" has a non-trivial explicit domain, however,
14236 * then the full substitution should be performed.
14238 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14239 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14241 isl_size n;
14242 isl_bool full;
14243 isl_pw_multi_aff *pma;
14245 n = isl_map_dim(map, type);
14246 if (n < 0 || !mpa)
14247 goto error;
14249 full = isl_map_involves_dims(map, type, 0, n);
14250 if (full >= 0 && !full)
14251 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14252 if (full < 0)
14253 goto error;
14254 if (!full) {
14255 isl_space *space;
14256 isl_multi_aff *ma;
14258 space = isl_multi_pw_aff_get_space(mpa);
14259 isl_multi_pw_aff_free(mpa);
14260 ma = isl_multi_aff_zero(space);
14261 return isl_map_preimage_multi_aff(map, type, ma);
14264 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14265 return isl_map_preimage_pw_multi_aff(map, type, pma);
14266 error:
14267 isl_map_free(map);
14268 isl_multi_pw_aff_free(mpa);
14269 return NULL;
14272 /* Compute the preimage of "map" under the function represented by "mpa".
14273 * In other words, plug in "mpa" in the domain "map".
14274 * The result is a map that lives in the same space as "map",
14275 * except that domain space has been replaced by the domain space of "mpa".
14277 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14278 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14280 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14283 /* Compute the preimage of "set" by the function represented by "mpa".
14284 * In other words, plug in "mpa" in "set".
14286 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14287 __isl_take isl_multi_pw_aff *mpa)
14289 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14292 /* Return a copy of the equality constraints of "bset" as a matrix.
14294 __isl_give isl_mat *isl_basic_set_extract_equalities(
14295 __isl_keep isl_basic_set *bset)
14297 isl_ctx *ctx;
14298 isl_size total;
14300 total = isl_basic_set_dim(bset, isl_dim_all);
14301 if (total < 0)
14302 return NULL;
14304 ctx = isl_basic_set_get_ctx(bset);
14305 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14308 /* Are the "n" "coefficients" starting at "first" of the integer division
14309 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14310 * to each other?
14311 * The "coefficient" at position 0 is the denominator.
14312 * The "coefficient" at position 1 is the constant term.
14314 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14315 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14316 unsigned first, unsigned n)
14318 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14319 return isl_bool_error;
14320 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14321 return isl_bool_error;
14322 return isl_seq_eq(bmap1->div[pos1] + first,
14323 bmap2->div[pos2] + first, n);
14326 /* Are the integer division expressions at position "pos1" in "bmap1" and
14327 * "pos2" in "bmap2" equal to each other, except that the constant terms
14328 * are different?
14330 isl_bool isl_basic_map_equal_div_expr_except_constant(
14331 __isl_keep isl_basic_map *bmap1, int pos1,
14332 __isl_keep isl_basic_map *bmap2, int pos2)
14334 isl_bool equal;
14335 isl_size total, total2;
14337 total = isl_basic_map_dim(bmap1, isl_dim_all);
14338 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14339 if (total < 0 || total2 < 0)
14340 return isl_bool_error;
14341 if (total != total2)
14342 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14343 "incomparable div expressions", return isl_bool_error);
14344 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14345 0, 1);
14346 if (equal < 0 || !equal)
14347 return equal;
14348 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14349 1, 1);
14350 if (equal < 0 || equal)
14351 return isl_bool_not(equal);
14352 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14353 2, total);
14356 /* Replace the numerator of the constant term of the integer division
14357 * expression at position "div" in "bmap" by "value".
14358 * The caller guarantees that this does not change the meaning
14359 * of the input.
14361 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14362 __isl_take isl_basic_map *bmap, int div, int value)
14364 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14365 return isl_basic_map_free(bmap);
14367 isl_int_set_si(bmap->div[div][1], value);
14369 return bmap;
14372 /* Is the point "inner" internal to inequality constraint "ineq"
14373 * of "bset"?
14374 * The point is considered to be internal to the inequality constraint,
14375 * if it strictly lies on the positive side of the inequality constraint,
14376 * or if it lies on the constraint and the constraint is lexico-positive.
14378 static isl_bool is_internal(__isl_keep isl_vec *inner,
14379 __isl_keep isl_basic_set *bset, int ineq)
14381 isl_ctx *ctx;
14382 int pos;
14383 isl_size total;
14385 if (!inner || !bset)
14386 return isl_bool_error;
14388 ctx = isl_basic_set_get_ctx(bset);
14389 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14390 &ctx->normalize_gcd);
14391 if (!isl_int_is_zero(ctx->normalize_gcd))
14392 return isl_int_is_nonneg(ctx->normalize_gcd);
14394 total = isl_basic_set_dim(bset, isl_dim_all);
14395 if (total < 0)
14396 return isl_bool_error;
14397 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14398 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14401 /* Tighten the inequality constraints of "bset" that are outward with respect
14402 * to the point "vec".
14403 * That is, tighten the constraints that are not satisfied by "vec".
14405 * "vec" is a point internal to some superset S of "bset" that is used
14406 * to make the subsets of S disjoint, by tightening one half of the constraints
14407 * that separate two subsets. In particular, the constraints of S
14408 * are all satisfied by "vec" and should not be tightened.
14409 * Of the internal constraints, those that have "vec" on the outside
14410 * are tightened. The shared facet is included in the adjacent subset
14411 * with the opposite constraint.
14412 * For constraints that saturate "vec", this criterion cannot be used
14413 * to determine which of the two sides should be tightened.
14414 * Instead, the sign of the first non-zero coefficient is used
14415 * to make this choice. Note that this second criterion is never used
14416 * on the constraints of S since "vec" is interior to "S".
14418 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14419 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14421 int j;
14423 bset = isl_basic_set_cow(bset);
14424 if (!bset)
14425 return NULL;
14426 for (j = 0; j < bset->n_ineq; ++j) {
14427 isl_bool internal;
14429 internal = is_internal(vec, bset, j);
14430 if (internal < 0)
14431 return isl_basic_set_free(bset);
14432 if (internal)
14433 continue;
14434 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14437 return bset;
14440 /* Replace the variables x of type "type" starting at "first" in "bmap"
14441 * by x' with x = M x' with M the matrix trans.
14442 * That is, replace the corresponding coefficients c by c M.
14444 * The transformation matrix should be a square matrix.
14446 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14447 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14448 __isl_take isl_mat *trans)
14450 unsigned pos;
14452 bmap = isl_basic_map_cow(bmap);
14453 if (!bmap || !trans)
14454 goto error;
14456 if (trans->n_row != trans->n_col)
14457 isl_die(trans->ctx, isl_error_invalid,
14458 "expecting square transformation matrix", goto error);
14459 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14460 goto error;
14462 pos = isl_basic_map_offset(bmap, type) + first;
14464 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14465 isl_mat_copy(trans)) < 0)
14466 goto error;
14467 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14468 isl_mat_copy(trans)) < 0)
14469 goto error;
14470 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14471 isl_mat_copy(trans)) < 0)
14472 goto error;
14474 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14475 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14477 isl_mat_free(trans);
14478 return bmap;
14479 error:
14480 isl_mat_free(trans);
14481 isl_basic_map_free(bmap);
14482 return NULL;
14485 /* Replace the variables x of type "type" starting at "first" in "bset"
14486 * by x' with x = M x' with M the matrix trans.
14487 * That is, replace the corresponding coefficients c by c M.
14489 * The transformation matrix should be a square matrix.
14491 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14492 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14493 __isl_take isl_mat *trans)
14495 return isl_basic_map_transform_dims(bset, type, first, trans);