isl_set_substitute: implement in terms of isl_set_preimage_multi_aff
[isl.git] / isl_map.c
blobe4578460c59dccb07dbaa003805e79dc738683bd
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 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
117 return isl_map_dim(set_to_map(set), type);
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
123 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
124 enum isl_dim_type type)
126 isl_space *space;
128 space = isl_basic_map_peek_space(bmap);
129 if (!space)
130 return isl_size_error;
132 switch (type) {
133 case isl_dim_param:
134 case isl_dim_in:
135 case isl_dim_out: return isl_space_offset(space, type);
136 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
137 case isl_dim_cst:
138 default:
139 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
140 "invalid dimension type", return isl_size_error);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
147 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
148 enum isl_dim_type type)
150 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
157 enum isl_dim_type type)
159 switch (type) {
160 case isl_dim_cst: return 0;
161 case isl_dim_param:
162 case isl_dim_in:
163 case isl_dim_out:
164 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
165 default: return 0;
169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
172 return isl_basic_map_offset(bset, type);
175 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
177 return pos(map->dim, type);
180 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
181 enum isl_dim_type type)
183 return isl_basic_map_dim(bset, type);
186 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_set);
191 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
193 return isl_basic_set_dim(bset, isl_dim_param);
196 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
201 isl_size isl_set_n_dim(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_set);
206 isl_size isl_set_n_param(__isl_keep isl_set *set)
208 return isl_set_dim(set, isl_dim_param);
211 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
213 isl_size dim;
215 if (!bmap)
216 return isl_size_error;
217 dim = isl_space_dim(bmap->dim, isl_dim_all);
218 if (dim < 0)
219 return isl_size_error;
220 return dim + bmap->n_div;
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
226 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
228 if (!bmap)
229 return isl_size_error;
230 return bmap->n_eq;
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
236 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
238 return isl_basic_map_n_equality(bset_to_bmap(bset));
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
244 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
246 if (!bmap)
247 return isl_size_error;
248 return bmap->n_ineq;
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
254 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
256 return isl_basic_map_n_inequality(bset_to_bmap(bset));
259 /* Do "bmap1" and "bmap2" have the same parameters?
261 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
262 __isl_keep isl_basic_map *bmap2)
264 isl_space *space1, *space2;
266 space1 = isl_basic_map_peek_space(bmap1);
267 space2 = isl_basic_map_peek_space(bmap2);
268 return isl_space_has_equal_params(space1, space2);
271 /* Do "map1" and "map2" have the same parameters?
273 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
274 __isl_keep isl_map *map2)
276 isl_space *space1, *space2;
278 space1 = isl_map_peek_space(map1);
279 space2 = isl_map_peek_space(map2);
280 return isl_space_has_equal_params(space1, space2);
283 /* Do "map" and "set" have the same parameters?
285 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
286 __isl_keep isl_set *set)
288 return isl_map_has_equal_params(map, set_to_map(set));
291 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
293 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
294 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
296 isl_space *bmap_space, *bset_space;
298 bmap_space = isl_basic_map_peek_space(bmap);
299 bset_space = isl_basic_set_peek_space(bset);
300 return isl_space_tuple_is_equal(bmap_space, type,
301 bset_space, isl_dim_set);
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
306 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
307 enum isl_dim_type type, __isl_keep isl_set *set)
309 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
312 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
313 __isl_keep isl_set *set)
315 isl_bool m;
316 if (!map || !set)
317 return isl_bool_error;
318 m = isl_map_has_equal_params(map, set_to_map(set));
319 if (m < 0 || !m)
320 return m;
321 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
324 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
325 __isl_keep isl_basic_set *bset)
327 isl_bool m;
328 if (!bmap || !bset)
329 return isl_bool_error;
330 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
331 if (m < 0 || !m)
332 return m;
333 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
336 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
337 __isl_keep isl_set *set)
339 isl_bool m;
340 if (!map || !set)
341 return isl_bool_error;
342 m = isl_map_has_equal_params(map, set_to_map(set));
343 if (m < 0 || !m)
344 return m;
345 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
348 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
349 __isl_keep isl_basic_set *bset)
351 isl_bool m;
352 if (!bmap || !bset)
353 return isl_bool_error;
354 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
355 if (m < 0 || !m)
356 return m;
357 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
360 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
362 return bmap ? bmap->ctx : NULL;
365 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
367 return bset ? bset->ctx : NULL;
370 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
372 return map ? map->ctx : NULL;
375 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
377 return set ? set->ctx : NULL;
380 /* Return the space of "bmap".
382 __isl_keep isl_space *isl_basic_map_peek_space(
383 __isl_keep const isl_basic_map *bmap)
385 return bmap ? bmap->dim : NULL;
388 /* Return the space of "bset".
390 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
392 return isl_basic_map_peek_space(bset_to_bmap(bset));
395 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
397 return isl_space_copy(isl_basic_map_peek_space(bmap));
400 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
402 return isl_basic_map_get_space(bset_to_bmap(bset));
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
414 static __isl_give isl_space *isl_basic_map_take_space(
415 __isl_keep isl_basic_map *bmap)
417 isl_space *space;
419 if (!bmap)
420 return NULL;
421 if (bmap->ref != 1)
422 return isl_basic_map_get_space(bmap);
423 space = bmap->dim;
424 bmap->dim = NULL;
425 return space;
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
433 static __isl_give isl_basic_map *isl_basic_map_restore_space(
434 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
436 if (!bmap || !space)
437 goto error;
439 if (bmap->dim == space) {
440 isl_space_free(space);
441 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 if (!bmap)
446 goto error;
447 isl_space_free(bmap->dim);
448 bmap->dim = space;
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 isl_space_free(space);
454 return NULL;
457 /* Extract the divs in "bmap" as a matrix.
459 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
461 int i;
462 isl_ctx *ctx;
463 isl_mat *div;
464 isl_size v_div;
465 unsigned cols;
467 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
468 if (v_div < 0)
469 return NULL;
471 ctx = isl_basic_map_get_ctx(bmap);
472 cols = 1 + 1 + v_div + bmap->n_div;
473 div = isl_mat_alloc(ctx, bmap->n_div, cols);
474 if (!div)
475 return NULL;
477 for (i = 0; i < bmap->n_div; ++i)
478 isl_seq_cpy(div->row[i], bmap->div[i], cols);
480 return div;
483 /* Extract the divs in "bset" as a matrix.
485 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
487 return isl_basic_map_get_divs(bset);
490 __isl_give isl_local_space *isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map *bmap)
493 isl_mat *div;
495 if (!bmap)
496 return NULL;
498 div = isl_basic_map_get_divs(bmap);
499 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
502 __isl_give isl_local_space *isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set *bset)
505 return isl_basic_map_get_local_space(bset);
508 /* For each known div d = floor(f/m), add the constraints
510 * f - m d >= 0
511 * -(f-(m-1)) + m d >= 0
513 * Do not finalize the result.
515 static __isl_give isl_basic_map *add_known_div_constraints(
516 __isl_take isl_basic_map *bmap)
518 int i;
519 isl_size n_div;
521 n_div = isl_basic_map_dim(bmap, isl_dim_div);
522 if (n_div < 0)
523 return isl_basic_map_free(bmap);
524 if (n_div == 0)
525 return bmap;
526 bmap = isl_basic_map_cow(bmap);
527 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
528 if (!bmap)
529 return NULL;
530 for (i = 0; i < n_div; ++i) {
531 if (isl_int_is_zero(bmap->div[i][0]))
532 continue;
533 bmap = isl_basic_map_add_div_constraints(bmap, i);
536 return bmap;
539 __isl_give isl_basic_map *isl_basic_map_from_local_space(
540 __isl_take isl_local_space *ls)
542 int i;
543 isl_size n_div;
544 isl_basic_map *bmap;
546 n_div = isl_local_space_dim(ls, isl_dim_div);
547 if (n_div < 0)
548 ls = isl_local_space_free(ls);
549 if (!ls)
550 return NULL;
552 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
553 n_div, 0, 2 * n_div);
555 for (i = 0; i < n_div; ++i)
556 if (isl_basic_map_alloc_div(bmap) < 0)
557 goto error;
559 for (i = 0; i < n_div; ++i)
560 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
561 bmap = add_known_div_constraints(bmap);
563 isl_local_space_free(ls);
564 return bmap;
565 error:
566 isl_local_space_free(ls);
567 isl_basic_map_free(bmap);
568 return NULL;
571 __isl_give isl_basic_set *isl_basic_set_from_local_space(
572 __isl_take isl_local_space *ls)
574 return isl_basic_map_from_local_space(ls);
577 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
579 return isl_space_copy(isl_map_peek_space(map));
582 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
584 if (!set)
585 return NULL;
586 return isl_space_copy(set->dim);
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
598 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
600 isl_space *space;
602 if (!map)
603 return NULL;
604 if (map->ref != 1)
605 return isl_map_get_space(map);
606 space = map->dim;
607 map->dim = NULL;
608 return space;
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
616 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
617 __isl_take isl_space *space)
619 if (!map || !space)
620 goto error;
622 if (map->dim == space) {
623 isl_space_free(space);
624 return map;
627 map = isl_map_cow(map);
628 if (!map)
629 goto error;
630 isl_space_free(map->dim);
631 map->dim = space;
633 return map;
634 error:
635 isl_map_free(map);
636 isl_space_free(space);
637 return NULL;
640 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
643 isl_space *space;
645 space = isl_basic_map_take_space(bmap);
646 space = isl_space_set_tuple_name(space, type, s);
647 bmap = isl_basic_map_restore_space(bmap, space);
648 bmap = isl_basic_map_finalize(bmap);
649 return bmap;
652 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set *bset, const char *s)
655 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
659 enum isl_dim_type type)
661 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
664 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
665 enum isl_dim_type type, const char *s)
667 int i;
668 isl_space *space;
670 map = isl_map_cow(map);
671 if (!map)
672 return NULL;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
676 if (!map->p[i])
677 goto error;
680 space = isl_map_take_space(map);
681 space = isl_space_set_tuple_name(space, type, s);
682 map = isl_map_restore_space(map, space);
684 return map;
685 error:
686 isl_map_free(map);
687 return NULL;
690 /* Replace the identifier of the tuple of type "type" by "id".
692 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map *bmap,
694 enum isl_dim_type type, __isl_take isl_id *id)
696 isl_space *space;
698 space = isl_basic_map_take_space(bmap);
699 space = isl_space_set_tuple_id(space, type, id);
700 bmap = isl_basic_map_restore_space(bmap, space);
701 bmap = isl_basic_map_finalize(bmap);
702 return bmap;
705 /* Replace the identifier of the tuple by "id".
707 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
710 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
713 /* Does the input or output tuple have a name?
715 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
717 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
720 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
721 enum isl_dim_type type)
723 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
726 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
727 const char *s)
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
730 isl_dim_set, s));
733 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
734 enum isl_dim_type type, __isl_take isl_id *id)
736 isl_space *space;
738 space = isl_map_take_space(map);
739 space = isl_space_set_tuple_id(space, type, id);
740 map = isl_map_restore_space(map, space);
742 return isl_map_reset_space(map, isl_map_get_space(map));
745 /* Replace the identifier of the domain tuple of "map" by "id".
747 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
748 __isl_take isl_id *id)
750 return isl_map_set_tuple_id(map, isl_dim_in, id);
753 /* Replace the identifier of the range tuple of "map" by "id".
755 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
756 __isl_take isl_id *id)
758 return isl_map_set_tuple_id(map, isl_dim_out, id);
761 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
762 __isl_take isl_id *id)
764 return isl_map_set_tuple_id(set, isl_dim_set, id);
767 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
768 enum isl_dim_type type)
770 isl_space *space;
772 space = isl_map_take_space(map);
773 space = isl_space_reset_tuple_id(space, type);
774 map = isl_map_restore_space(map, space);
776 return isl_map_reset_space(map, isl_map_get_space(map));
779 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
781 return isl_map_reset_tuple_id(set, isl_dim_set);
784 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
786 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
789 /* Does the domain tuple of "map" have an identifier?
791 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
793 return isl_map_has_tuple_id(map, isl_dim_in);
796 /* Does the range tuple of "map" have an identifier?
798 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
800 return isl_map_has_tuple_id(map, isl_dim_out);
803 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
804 enum isl_dim_type type)
806 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
809 /* Return the identifier of the domain tuple of "map", assuming it has one.
811 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
813 return isl_map_get_tuple_id(map, isl_dim_in);
816 /* Return the identifier of the range tuple of "map", assuming it has one.
818 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
820 return isl_map_get_tuple_id(map, isl_dim_out);
823 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
825 return isl_map_has_tuple_id(set, isl_dim_set);
828 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
830 return isl_map_get_tuple_id(set, isl_dim_set);
833 /* Does the set tuple have a name?
835 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
837 if (!set)
838 return isl_bool_error;
839 return isl_space_has_tuple_name(set->dim, isl_dim_set);
843 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
845 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
848 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
850 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
853 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
854 enum isl_dim_type type, unsigned pos)
856 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
859 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
860 enum isl_dim_type type, unsigned pos)
862 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
865 /* Does the given dimension have a name?
867 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
868 enum isl_dim_type type, unsigned pos)
870 if (!map)
871 return isl_bool_error;
872 return isl_space_has_dim_name(map->dim, type, pos);
875 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
876 enum isl_dim_type type, unsigned pos)
878 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
881 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
882 enum isl_dim_type type, unsigned pos)
884 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
887 /* Does the given dimension have a name?
889 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
890 enum isl_dim_type type, unsigned pos)
892 if (!set)
893 return isl_bool_error;
894 return isl_space_has_dim_name(set->dim, type, pos);
897 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
898 __isl_take isl_basic_map *bmap,
899 enum isl_dim_type type, unsigned pos, const char *s)
901 isl_space *space;
903 space = isl_basic_map_take_space(bmap);
904 space = isl_space_set_dim_name(space, type, pos, s);
905 bmap = isl_basic_map_restore_space(bmap, space);
906 return isl_basic_map_finalize(bmap);
909 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
910 enum isl_dim_type type, unsigned pos, const char *s)
912 int i;
913 isl_space *space;
915 map = isl_map_cow(map);
916 if (!map)
917 return NULL;
919 for (i = 0; i < map->n; ++i) {
920 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
921 if (!map->p[i])
922 goto error;
925 space = isl_map_take_space(map);
926 space = isl_space_set_dim_name(space, type, pos, s);
927 map = isl_map_restore_space(map, space);
929 return map;
930 error:
931 isl_map_free(map);
932 return NULL;
935 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
936 __isl_take isl_basic_set *bset,
937 enum isl_dim_type type, unsigned pos, const char *s)
939 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
940 type, pos, s));
943 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
944 enum isl_dim_type type, unsigned pos, const char *s)
946 return set_from_map(isl_map_set_dim_name(set_to_map(set),
947 type, pos, s));
950 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
951 enum isl_dim_type type, unsigned pos)
953 if (!bmap)
954 return isl_bool_error;
955 return isl_space_has_dim_id(bmap->dim, type, pos);
958 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
959 enum isl_dim_type type, unsigned pos)
961 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
964 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
965 enum isl_dim_type type, unsigned pos)
967 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
970 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
971 enum isl_dim_type type, unsigned pos)
973 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
976 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
977 enum isl_dim_type type, unsigned pos)
979 return isl_map_has_dim_id(set, type, pos);
982 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
983 enum isl_dim_type type, unsigned pos)
985 return isl_map_get_dim_id(set, type, pos);
988 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
989 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
991 isl_space *space;
993 space = isl_map_take_space(map);
994 space = isl_space_set_dim_id(space, type, pos, id);
995 map = isl_map_restore_space(map, space);
997 return isl_map_reset_space(map, isl_map_get_space(map));
1000 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1001 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1003 return isl_map_set_dim_id(set, type, pos, id);
1006 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1007 __isl_keep isl_id *id)
1009 if (!map)
1010 return -1;
1011 return isl_space_find_dim_by_id(map->dim, type, id);
1014 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1015 __isl_keep isl_id *id)
1017 return isl_map_find_dim_by_id(set, type, id);
1020 /* Return the position of the dimension of the given type and name
1021 * in "bmap".
1022 * Return -1 if no such dimension can be found.
1024 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1025 enum isl_dim_type type, const char *name)
1027 if (!bmap)
1028 return -1;
1029 return isl_space_find_dim_by_name(bmap->dim, type, name);
1032 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1033 const char *name)
1035 if (!map)
1036 return -1;
1037 return isl_space_find_dim_by_name(map->dim, type, name);
1040 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1041 const char *name)
1043 return isl_map_find_dim_by_name(set, type, name);
1046 /* Check whether equality i of bset is a pure stride constraint
1047 * on a single dimension, i.e., of the form
1049 * v = k e
1051 * with k a constant and e an existentially quantified variable.
1053 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1055 isl_size nparam;
1056 isl_size d;
1057 isl_size n_div;
1058 int pos1;
1059 int pos2;
1061 nparam = isl_basic_set_dim(bset, isl_dim_param);
1062 d = isl_basic_set_dim(bset, isl_dim_set);
1063 n_div = isl_basic_set_dim(bset, isl_dim_div);
1064 if (nparam < 0 || d < 0 || n_div < 0)
1065 return isl_bool_error;
1067 if (!isl_int_is_zero(bset->eq[i][0]))
1068 return isl_bool_false;
1070 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1071 return isl_bool_false;
1072 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1073 if (pos1 == -1)
1074 return isl_bool_false;
1075 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1076 d - pos1 - 1) != -1)
1077 return isl_bool_false;
1079 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1080 if (pos2 == -1)
1081 return isl_bool_false;
1082 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1083 n_div - pos2 - 1) != -1)
1084 return isl_bool_false;
1085 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1086 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1087 return isl_bool_false;
1089 return isl_bool_true;
1092 /* Reset the user pointer on all identifiers of parameters and tuples
1093 * of the space of "map".
1095 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1097 isl_space *space;
1099 space = isl_map_get_space(map);
1100 space = isl_space_reset_user(space);
1101 map = isl_map_reset_space(map, space);
1103 return map;
1106 /* Reset the user pointer on all identifiers of parameters and tuples
1107 * of the space of "set".
1109 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1111 return isl_map_reset_user(set);
1114 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1116 if (!bmap)
1117 return isl_bool_error;
1118 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1121 /* Has "map" been marked as a rational map?
1122 * In particular, have all basic maps in "map" been marked this way?
1123 * An empty map is not considered to be rational.
1124 * Maps where only some of the basic maps are marked rational
1125 * are not allowed.
1127 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1129 int i;
1130 isl_bool rational;
1132 if (!map)
1133 return isl_bool_error;
1134 if (map->n == 0)
1135 return isl_bool_false;
1136 rational = isl_basic_map_is_rational(map->p[0]);
1137 if (rational < 0)
1138 return rational;
1139 for (i = 1; i < map->n; ++i) {
1140 isl_bool rational_i;
1142 rational_i = isl_basic_map_is_rational(map->p[i]);
1143 if (rational_i < 0)
1144 return rational_i;
1145 if (rational != rational_i)
1146 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1147 "mixed rational and integer basic maps "
1148 "not supported", return isl_bool_error);
1151 return rational;
1154 /* Has "set" been marked as a rational set?
1155 * In particular, have all basic set in "set" been marked this way?
1156 * An empty set is not considered to be rational.
1157 * Sets where only some of the basic sets are marked rational
1158 * are not allowed.
1160 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1162 return isl_map_is_rational(set);
1165 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1167 return isl_basic_map_is_rational(bset);
1170 /* Does "bmap" contain any rational points?
1172 * If "bmap" has an equality for each dimension, equating the dimension
1173 * to an integer constant, then it has no rational points, even if it
1174 * is marked as rational.
1176 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1178 isl_bool has_rational = isl_bool_true;
1179 isl_size total;
1181 if (!bmap)
1182 return isl_bool_error;
1183 if (isl_basic_map_plain_is_empty(bmap))
1184 return isl_bool_false;
1185 if (!isl_basic_map_is_rational(bmap))
1186 return isl_bool_false;
1187 bmap = isl_basic_map_copy(bmap);
1188 bmap = isl_basic_map_implicit_equalities(bmap);
1189 total = isl_basic_map_dim(bmap, isl_dim_all);
1190 if (total < 0)
1191 return isl_bool_error;
1192 if (bmap->n_eq == total) {
1193 int i, j;
1194 for (i = 0; i < bmap->n_eq; ++i) {
1195 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1196 if (j < 0)
1197 break;
1198 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1199 !isl_int_is_negone(bmap->eq[i][1 + j]))
1200 break;
1201 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1202 total - j - 1);
1203 if (j >= 0)
1204 break;
1206 if (i == bmap->n_eq)
1207 has_rational = isl_bool_false;
1209 isl_basic_map_free(bmap);
1211 return has_rational;
1214 /* Does "map" contain any rational points?
1216 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1218 int i;
1219 isl_bool has_rational;
1221 if (!map)
1222 return isl_bool_error;
1223 for (i = 0; i < map->n; ++i) {
1224 has_rational = isl_basic_map_has_rational(map->p[i]);
1225 if (has_rational < 0 || has_rational)
1226 return has_rational;
1228 return isl_bool_false;
1231 /* Does "set" contain any rational points?
1233 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1235 return isl_map_has_rational(set);
1238 /* Is this basic set a parameter domain?
1240 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1242 if (!bset)
1243 return isl_bool_error;
1244 return isl_space_is_params(bset->dim);
1247 /* Is this set a parameter domain?
1249 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1251 if (!set)
1252 return isl_bool_error;
1253 return isl_space_is_params(set->dim);
1256 /* Is this map actually a parameter domain?
1257 * Users should never call this function. Outside of isl,
1258 * a map can never be a parameter domain.
1260 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1262 if (!map)
1263 return isl_bool_error;
1264 return isl_space_is_params(map->dim);
1267 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1268 __isl_take isl_basic_map *bmap, unsigned extra,
1269 unsigned n_eq, unsigned n_ineq)
1271 int i;
1272 isl_space *space = isl_basic_map_peek_space(bmap);
1273 isl_size n_var = isl_space_dim(space, isl_dim_all);
1274 size_t row_size = 1 + n_var + extra;
1276 bmap->ctx = ctx;
1277 isl_ctx_ref(ctx);
1279 if (n_var < 0)
1280 return isl_basic_map_free(bmap);
1282 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1283 if (isl_blk_is_error(bmap->block))
1284 goto error;
1286 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1287 if ((n_ineq + n_eq) && !bmap->ineq)
1288 goto error;
1290 if (extra == 0) {
1291 bmap->block2 = isl_blk_empty();
1292 bmap->div = NULL;
1293 } else {
1294 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1295 if (isl_blk_is_error(bmap->block2))
1296 goto error;
1298 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1299 if (!bmap->div)
1300 goto error;
1303 for (i = 0; i < n_ineq + n_eq; ++i)
1304 bmap->ineq[i] = bmap->block.data + i * row_size;
1306 for (i = 0; i < extra; ++i)
1307 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1309 bmap->ref = 1;
1310 bmap->flags = 0;
1311 bmap->c_size = n_eq + n_ineq;
1312 bmap->eq = bmap->ineq + n_ineq;
1313 bmap->extra = extra;
1314 bmap->n_eq = 0;
1315 bmap->n_ineq = 0;
1316 bmap->n_div = 0;
1317 bmap->sample = NULL;
1319 return bmap;
1320 error:
1321 isl_basic_map_free(bmap);
1322 return NULL;
1325 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1326 unsigned nparam, unsigned dim, unsigned extra,
1327 unsigned n_eq, unsigned n_ineq)
1329 struct isl_basic_map *bmap;
1330 isl_space *space;
1332 space = isl_space_set_alloc(ctx, nparam, dim);
1333 if (!space)
1334 return NULL;
1336 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1337 return bset_from_bmap(bmap);
1340 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1341 unsigned extra, unsigned n_eq, unsigned n_ineq)
1343 struct isl_basic_map *bmap;
1344 if (!space)
1345 return NULL;
1346 isl_assert(space->ctx, space->n_in == 0, goto error);
1347 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1348 return bset_from_bmap(bmap);
1349 error:
1350 isl_space_free(space);
1351 return NULL;
1354 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1355 unsigned extra, unsigned n_eq, unsigned n_ineq)
1357 struct isl_basic_map *bmap;
1359 if (!space)
1360 return NULL;
1361 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1362 if (!bmap)
1363 goto error;
1364 bmap->dim = space;
1366 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1367 error:
1368 isl_space_free(space);
1369 return NULL;
1372 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1373 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1374 unsigned n_eq, unsigned n_ineq)
1376 struct isl_basic_map *bmap;
1377 isl_space *space;
1379 space = isl_space_alloc(ctx, nparam, in, out);
1380 if (!space)
1381 return NULL;
1383 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1384 return bmap;
1387 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1388 __isl_keep isl_basic_map *src)
1390 int i;
1391 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1393 if (!dst || total < 0)
1394 return isl_basic_map_free(dst);
1396 for (i = 0; i < src->n_eq; ++i) {
1397 int j = isl_basic_map_alloc_equality(dst);
1398 if (j < 0)
1399 return isl_basic_map_free(dst);
1400 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1403 for (i = 0; i < src->n_ineq; ++i) {
1404 int j = isl_basic_map_alloc_inequality(dst);
1405 if (j < 0)
1406 return isl_basic_map_free(dst);
1407 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1410 for (i = 0; i < src->n_div; ++i) {
1411 int j = isl_basic_map_alloc_div(dst);
1412 if (j < 0)
1413 return isl_basic_map_free(dst);
1414 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1416 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1417 return dst;
1420 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1422 struct isl_basic_map *dup;
1424 if (!bmap)
1425 return NULL;
1426 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1427 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1428 dup = dup_constraints(dup, bmap);
1429 if (!dup)
1430 return NULL;
1431 dup->flags = bmap->flags;
1432 dup->sample = isl_vec_copy(bmap->sample);
1433 return dup;
1436 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1438 struct isl_basic_map *dup;
1440 dup = isl_basic_map_dup(bset_to_bmap(bset));
1441 return bset_from_bmap(dup);
1444 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1446 if (!bset)
1447 return NULL;
1449 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1450 bset->ref++;
1451 return bset;
1453 return isl_basic_set_dup(bset);
1456 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1458 if (!set)
1459 return NULL;
1461 set->ref++;
1462 return set;
1465 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1467 if (!bmap)
1468 return NULL;
1470 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1471 bmap->ref++;
1472 return bmap;
1474 bmap = isl_basic_map_dup(bmap);
1475 if (bmap)
1476 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1477 return bmap;
1480 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1482 if (!map)
1483 return NULL;
1485 map->ref++;
1486 return map;
1489 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1491 if (!bmap)
1492 return NULL;
1494 if (--bmap->ref > 0)
1495 return NULL;
1497 isl_ctx_deref(bmap->ctx);
1498 free(bmap->div);
1499 isl_blk_free(bmap->ctx, bmap->block2);
1500 free(bmap->ineq);
1501 isl_blk_free(bmap->ctx, bmap->block);
1502 isl_vec_free(bmap->sample);
1503 isl_space_free(bmap->dim);
1504 free(bmap);
1506 return NULL;
1509 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1511 return isl_basic_map_free(bset_to_bmap(bset));
1514 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1516 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1519 /* Check that "bset" does not involve any parameters.
1521 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1523 isl_size nparam;
1525 nparam = isl_basic_set_dim(bset, isl_dim_param);
1526 if (nparam < 0)
1527 return isl_stat_error;
1528 if (nparam != 0)
1529 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1530 "basic set should not have any parameters",
1531 return isl_stat_error);
1532 return isl_stat_ok;
1535 /* Check that "bset" does not involve any local variables.
1537 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1539 isl_size n_div;
1541 n_div = isl_basic_set_dim(bset, isl_dim_div);
1542 if (n_div < 0)
1543 return isl_stat_error;
1544 if (n_div != 0)
1545 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1546 "basic set should not have any local variables",
1547 return isl_stat_error);
1548 return isl_stat_ok;
1551 #undef TYPE
1552 #define TYPE isl_map
1554 #include "isl_check_named_params_templ.c"
1556 #undef TYPE
1557 #define TYPE isl_basic_map
1559 static
1560 #include "isl_check_named_params_templ.c"
1562 /* Check that "bmap1" and "bmap2" have the same parameters,
1563 * reporting an error if they do not.
1565 static isl_stat isl_basic_map_check_equal_params(
1566 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1568 isl_bool match;
1570 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1571 if (match < 0)
1572 return isl_stat_error;
1573 if (!match)
1574 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1575 "parameters don't match", return isl_stat_error);
1576 return isl_stat_ok;
1579 #undef TYPE
1580 #define TYPE isl_map
1582 #include "isl_align_params_bin_templ.c"
1584 #undef SUFFIX
1585 #define SUFFIX set
1586 #undef ARG1
1587 #define ARG1 isl_map
1588 #undef ARG2
1589 #define ARG2 isl_set
1591 #include "isl_align_params_templ.c"
1593 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1594 __isl_keep isl_map *map2,
1595 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1597 isl_bool r;
1599 if (!map1 || !map2)
1600 return isl_bool_error;
1601 if (isl_map_has_equal_params(map1, map2))
1602 return fn(map1, map2);
1603 if (isl_map_check_named_params(map1) < 0)
1604 return isl_bool_error;
1605 if (isl_map_check_named_params(map2) < 0)
1606 return isl_bool_error;
1607 map1 = isl_map_copy(map1);
1608 map2 = isl_map_copy(map2);
1609 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1610 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1611 r = fn(map1, map2);
1612 isl_map_free(map1);
1613 isl_map_free(map2);
1614 return r;
1617 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1619 isl_size total;
1620 struct isl_ctx *ctx;
1622 total = isl_basic_map_dim(bmap, isl_dim_all);
1623 if (total < 0)
1624 return -1;
1625 ctx = bmap->ctx;
1626 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1627 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1628 return -1);
1629 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1630 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1631 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1632 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1633 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1634 isl_int *t;
1635 int j = isl_basic_map_alloc_inequality(bmap);
1636 if (j < 0)
1637 return -1;
1638 t = bmap->ineq[j];
1639 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1640 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1641 bmap->eq[-1] = t;
1642 bmap->n_eq++;
1643 bmap->n_ineq--;
1644 bmap->eq--;
1645 return 0;
1647 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1648 bmap->extra - bmap->n_div);
1649 return bmap->n_eq++;
1652 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1654 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1657 __isl_give isl_basic_map *isl_basic_map_free_equality(
1658 __isl_take isl_basic_map *bmap, unsigned n)
1660 if (!bmap)
1661 return NULL;
1662 if (n > bmap->n_eq)
1663 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1664 "invalid number of equalities",
1665 isl_basic_map_free(bmap));
1666 bmap->n_eq -= n;
1667 return bmap;
1670 __isl_give isl_basic_set *isl_basic_set_free_equality(
1671 __isl_take isl_basic_set *bset, unsigned n)
1673 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1674 n));
1677 /* Drop the equality constraint at position "pos",
1678 * preserving the order of the other equality constraints.
1680 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1682 isl_int *t;
1683 int r;
1685 if (!bmap)
1686 return -1;
1687 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1689 t = bmap->eq[pos];
1690 bmap->n_eq--;
1691 for (r = pos; r < bmap->n_eq; ++r)
1692 bmap->eq[r] = bmap->eq[r + 1];
1693 bmap->eq[bmap->n_eq] = t;
1695 return 0;
1698 /* Turn inequality "pos" of "bmap" into an equality.
1700 * In particular, we move the inequality in front of the equalities
1701 * and move the last inequality in the position of the moved inequality.
1702 * Note that isl_tab_make_equalities_explicit depends on this particular
1703 * change in the ordering of the constraints.
1705 void isl_basic_map_inequality_to_equality(
1706 __isl_keep isl_basic_map *bmap, unsigned pos)
1708 isl_int *t;
1710 t = bmap->ineq[pos];
1711 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1712 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1713 bmap->eq[-1] = t;
1714 bmap->n_eq++;
1715 bmap->n_ineq--;
1716 bmap->eq--;
1717 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1718 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1719 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1720 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1723 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1725 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1728 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1730 isl_size total;
1731 struct isl_ctx *ctx;
1733 total = isl_basic_map_dim(bmap, isl_dim_all);
1734 if (total < 0)
1735 return -1;
1736 ctx = bmap->ctx;
1737 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1738 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1739 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1740 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1741 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1742 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1743 bmap->extra - bmap->n_div);
1744 return bmap->n_ineq++;
1747 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1749 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1752 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1753 __isl_take isl_basic_map *bmap, unsigned n)
1755 if (!bmap)
1756 return NULL;
1757 if (n > bmap->n_ineq)
1758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1759 "invalid number of inequalities",
1760 return isl_basic_map_free(bmap));
1761 bmap->n_ineq -= n;
1762 return bmap;
1765 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1766 __isl_take isl_basic_set *bset, unsigned n)
1768 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1769 n));
1772 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1774 isl_int *t;
1775 if (!bmap)
1776 return -1;
1777 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1779 if (pos != bmap->n_ineq - 1) {
1780 t = bmap->ineq[pos];
1781 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1782 bmap->ineq[bmap->n_ineq - 1] = t;
1783 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1785 bmap->n_ineq--;
1786 return 0;
1789 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1791 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1794 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1795 isl_int *eq)
1797 isl_bool empty;
1798 isl_size total;
1799 int k;
1801 empty = isl_basic_map_plain_is_empty(bmap);
1802 if (empty < 0)
1803 return isl_basic_map_free(bmap);
1804 if (empty)
1805 return bmap;
1807 bmap = isl_basic_map_cow(bmap);
1808 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1809 total = isl_basic_map_dim(bmap, isl_dim_all);
1810 if (total < 0)
1811 return isl_basic_map_free(bmap);
1812 k = isl_basic_map_alloc_equality(bmap);
1813 if (k < 0)
1814 goto error;
1815 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1816 return bmap;
1817 error:
1818 isl_basic_map_free(bmap);
1819 return NULL;
1822 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1823 isl_int *eq)
1825 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1828 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1829 isl_int *ineq)
1831 isl_size total;
1832 int k;
1834 bmap = isl_basic_map_cow(bmap);
1835 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1836 total = isl_basic_map_dim(bmap, isl_dim_all);
1837 if (total < 0)
1838 return isl_basic_map_free(bmap);
1839 k = isl_basic_map_alloc_inequality(bmap);
1840 if (k < 0)
1841 goto error;
1842 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1843 return bmap;
1844 error:
1845 isl_basic_map_free(bmap);
1846 return NULL;
1849 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1850 isl_int *ineq)
1852 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1855 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1857 isl_size total;
1859 total = isl_basic_map_dim(bmap, isl_dim_all);
1860 if (total < 0)
1861 return -1;
1862 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1863 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1864 bmap->extra - bmap->n_div);
1865 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1866 return bmap->n_div++;
1869 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1871 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1874 #undef TYPE
1875 #define TYPE isl_basic_map
1876 #include "check_type_range_templ.c"
1878 /* Check that there are "n" dimensions of type "type" starting at "first"
1879 * in "bset".
1881 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1882 enum isl_dim_type type, unsigned first, unsigned n)
1884 return isl_basic_map_check_range(bset_to_bmap(bset),
1885 type, first, n);
1888 /* Insert an extra integer division, prescribed by "div", to "bmap"
1889 * at (integer division) position "pos".
1891 * The integer division is first added at the end and then moved
1892 * into the right position.
1894 __isl_give isl_basic_map *isl_basic_map_insert_div(
1895 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1897 int i, k;
1898 isl_size total;
1900 bmap = isl_basic_map_cow(bmap);
1901 total = isl_basic_map_dim(bmap, isl_dim_all);
1902 if (total < 0 || !div)
1903 return isl_basic_map_free(bmap);
1905 if (div->size != 1 + 1 + total)
1906 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1907 "unexpected size", return isl_basic_map_free(bmap));
1908 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1909 return isl_basic_map_free(bmap);
1911 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1912 k = isl_basic_map_alloc_div(bmap);
1913 if (k < 0)
1914 return isl_basic_map_free(bmap);
1915 isl_seq_cpy(bmap->div[k], div->el, div->size);
1916 isl_int_set_si(bmap->div[k][div->size], 0);
1918 for (i = k; i > pos; --i)
1919 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1921 return bmap;
1924 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1926 if (!bmap)
1927 return isl_stat_error;
1928 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1929 bmap->n_div -= n;
1930 return isl_stat_ok;
1933 static __isl_give isl_basic_map *add_constraints(
1934 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1935 unsigned i_pos, unsigned o_pos)
1937 isl_size total, n_param, n_in, n_out, n_div;
1938 unsigned o_in, o_out;
1939 isl_ctx *ctx;
1940 isl_space *space;
1941 struct isl_dim_map *dim_map;
1943 space = isl_basic_map_peek_space(bmap2);
1944 if (!bmap1 || !space)
1945 goto error;
1947 total = isl_basic_map_dim(bmap1, isl_dim_all);
1948 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1949 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1950 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1951 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1952 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1953 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1954 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1955 goto error;
1956 ctx = isl_basic_map_get_ctx(bmap1);
1957 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1958 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1959 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1960 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1961 isl_dim_map_div(dim_map, bmap2, total);
1963 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1964 error:
1965 isl_basic_map_free(bmap1);
1966 isl_basic_map_free(bmap2);
1967 return NULL;
1970 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1971 unsigned extra, unsigned n_eq, unsigned n_ineq)
1973 isl_space *space;
1974 struct isl_basic_map *ext;
1975 unsigned flags;
1976 int dims_ok;
1978 if (!base)
1979 goto error;
1981 dims_ok = base->extra >= base->n_div + extra;
1983 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1984 room_for_ineq(base, n_ineq))
1985 return base;
1987 extra += base->extra;
1988 n_eq += base->n_eq;
1989 n_ineq += base->n_ineq;
1991 space = isl_basic_map_get_space(base);
1992 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1993 if (!ext)
1994 goto error;
1996 if (dims_ok)
1997 ext->sample = isl_vec_copy(base->sample);
1998 flags = base->flags;
1999 ext = add_constraints(ext, base, 0, 0);
2000 if (ext) {
2001 ext->flags = flags;
2002 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2005 return ext;
2007 error:
2008 isl_basic_map_free(base);
2009 return NULL;
2012 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2013 unsigned extra, unsigned n_eq, unsigned n_ineq)
2015 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2016 extra, n_eq, n_ineq));
2019 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2020 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2022 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2025 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2026 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2028 isl_basic_map *bmap = bset_to_bmap(base);
2029 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2030 return bset_from_bmap(bmap);
2033 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2035 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2038 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2040 if (!bmap)
2041 return NULL;
2043 if (bmap->ref > 1) {
2044 bmap->ref--;
2045 bmap = isl_basic_map_dup(bmap);
2047 if (bmap) {
2048 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2049 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2051 return bmap;
2054 /* Clear all cached information in "map", either because it is about
2055 * to be modified or because it is being freed.
2056 * Always return the same pointer that is passed in.
2057 * This is needed for the use in isl_map_free.
2059 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2061 isl_basic_map_free(map->cached_simple_hull[0]);
2062 isl_basic_map_free(map->cached_simple_hull[1]);
2063 map->cached_simple_hull[0] = NULL;
2064 map->cached_simple_hull[1] = NULL;
2065 return map;
2068 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2070 return isl_map_cow(set);
2073 /* Return an isl_map that is equal to "map" and that has only
2074 * a single reference.
2076 * If the original input already has only one reference, then
2077 * simply return it, but clear all cached information, since
2078 * it may be rendered invalid by the operations that will be
2079 * performed on the result.
2081 * Otherwise, create a duplicate (without any cached information).
2083 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2085 if (!map)
2086 return NULL;
2088 if (map->ref == 1)
2089 return clear_caches(map);
2090 map->ref--;
2091 return isl_map_dup(map);
2094 static void swap_vars(struct isl_blk blk, isl_int *a,
2095 unsigned a_len, unsigned b_len)
2097 isl_seq_cpy(blk.data, a+a_len, b_len);
2098 isl_seq_cpy(blk.data+b_len, a, a_len);
2099 isl_seq_cpy(a, blk.data, b_len+a_len);
2102 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2103 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2105 int i;
2106 struct isl_blk blk;
2108 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2109 goto error;
2111 if (n1 == 0 || n2 == 0)
2112 return bmap;
2114 bmap = isl_basic_map_cow(bmap);
2115 if (!bmap)
2116 return NULL;
2118 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2119 if (isl_blk_is_error(blk))
2120 goto error;
2122 for (i = 0; i < bmap->n_eq; ++i)
2123 swap_vars(blk,
2124 bmap->eq[i] + pos, n1, n2);
2126 for (i = 0; i < bmap->n_ineq; ++i)
2127 swap_vars(blk,
2128 bmap->ineq[i] + pos, n1, n2);
2130 for (i = 0; i < bmap->n_div; ++i)
2131 swap_vars(blk,
2132 bmap->div[i]+1 + pos, n1, n2);
2134 isl_blk_free(bmap->ctx, blk);
2136 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2137 bmap = isl_basic_map_gauss(bmap, NULL);
2138 return isl_basic_map_finalize(bmap);
2139 error:
2140 isl_basic_map_free(bmap);
2141 return NULL;
2144 /* The given basic map has turned out to be empty.
2145 * Explicitly mark it as such and change the representation
2146 * to a canonical representation of the empty basic map.
2147 * Since the basic map has conflicting constraints,
2148 * it must have at least one constraint, except perhaps
2149 * if it was already explicitly marked as being empty.
2150 * Do nothing in the latter case, i.e., if it has been marked empty and
2151 * has no constraints.
2153 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2154 __isl_take isl_basic_map *bmap)
2156 int i = 0;
2157 isl_bool empty;
2158 isl_size n;
2159 isl_size total;
2161 n = isl_basic_map_n_constraint(bmap);
2162 empty = isl_basic_map_plain_is_empty(bmap);
2163 if (n < 0 || empty < 0)
2164 return isl_basic_map_free(bmap);
2165 if (n == 0 && empty)
2166 return bmap;
2167 total = isl_basic_map_dim(bmap, isl_dim_all);
2168 if (total < 0)
2169 return isl_basic_map_free(bmap);
2170 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2171 return isl_basic_map_free(bmap);
2172 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2173 if (!bmap)
2174 return NULL;
2175 if (bmap->n_eq > 0) {
2176 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2177 if (!bmap)
2178 return NULL;
2179 } else {
2180 i = isl_basic_map_alloc_equality(bmap);
2181 if (i < 0)
2182 goto error;
2184 isl_int_set_si(bmap->eq[i][0], 1);
2185 isl_seq_clr(bmap->eq[i]+1, total);
2186 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2187 isl_vec_free(bmap->sample);
2188 bmap->sample = NULL;
2189 return isl_basic_map_finalize(bmap);
2190 error:
2191 isl_basic_map_free(bmap);
2192 return NULL;
2195 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2196 __isl_take isl_basic_set *bset)
2198 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2201 __isl_give isl_basic_map *isl_basic_map_set_rational(
2202 __isl_take isl_basic_map *bmap)
2204 if (!bmap)
2205 return NULL;
2207 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2208 return bmap;
2210 bmap = isl_basic_map_cow(bmap);
2211 if (!bmap)
2212 return NULL;
2214 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2216 return isl_basic_map_finalize(bmap);
2219 __isl_give isl_basic_set *isl_basic_set_set_rational(
2220 __isl_take isl_basic_set *bset)
2222 return isl_basic_map_set_rational(bset);
2225 __isl_give isl_basic_set *isl_basic_set_set_integral(
2226 __isl_take isl_basic_set *bset)
2228 if (!bset)
2229 return NULL;
2231 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2232 return bset;
2234 bset = isl_basic_set_cow(bset);
2235 if (!bset)
2236 return NULL;
2238 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2240 return isl_basic_set_finalize(bset);
2243 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2245 int i;
2247 map = isl_map_cow(map);
2248 if (!map)
2249 return NULL;
2250 for (i = 0; i < map->n; ++i) {
2251 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2252 if (!map->p[i])
2253 goto error;
2255 return map;
2256 error:
2257 isl_map_free(map);
2258 return NULL;
2261 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2263 return isl_map_set_rational(set);
2266 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2267 * of "bmap").
2269 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2271 isl_int *t = bmap->div[a];
2272 bmap->div[a] = bmap->div[b];
2273 bmap->div[b] = t;
2276 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2277 * div definitions accordingly.
2279 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2280 int a, int b)
2282 int i;
2283 isl_size off;
2285 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2286 if (off < 0)
2287 return isl_basic_map_free(bmap);
2289 swap_div(bmap, a, b);
2291 for (i = 0; i < bmap->n_eq; ++i)
2292 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2294 for (i = 0; i < bmap->n_ineq; ++i)
2295 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2297 for (i = 0; i < bmap->n_div; ++i)
2298 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2299 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2301 return bmap;
2304 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2306 isl_seq_cpy(c, c + n, rem);
2307 isl_seq_clr(c + rem, n);
2310 /* Drop n dimensions starting at first.
2312 * In principle, this frees up some extra variables as the number
2313 * of columns remains constant, but we would have to extend
2314 * the div array too as the number of rows in this array is assumed
2315 * to be equal to extra.
2317 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2318 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2320 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2323 /* Move "n" divs starting at "first" to the end of the list of divs.
2325 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2326 unsigned first, unsigned n)
2328 isl_int **div;
2329 int i;
2331 if (first + n == bmap->n_div)
2332 return bmap;
2334 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2335 if (!div)
2336 goto error;
2337 for (i = 0; i < n; ++i)
2338 div[i] = bmap->div[first + i];
2339 for (i = 0; i < bmap->n_div - first - n; ++i)
2340 bmap->div[first + i] = bmap->div[first + n + i];
2341 for (i = 0; i < n; ++i)
2342 bmap->div[bmap->n_div - n + i] = div[i];
2343 free(div);
2344 return bmap;
2345 error:
2346 isl_basic_map_free(bmap);
2347 return NULL;
2350 #undef TYPE
2351 #define TYPE isl_map
2352 static
2353 #include "check_type_range_templ.c"
2355 /* Check that there are "n" dimensions of type "type" starting at "first"
2356 * in "set".
2358 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2359 enum isl_dim_type type, unsigned first, unsigned n)
2361 return isl_map_check_range(set_to_map(set), type, first, n);
2364 /* Drop "n" dimensions of type "type" starting at "first".
2365 * Perform the core computation, without cowing or
2366 * simplifying and finalizing the result.
2368 * In principle, this frees up some extra variables as the number
2369 * of columns remains constant, but we would have to extend
2370 * the div array too as the number of rows in this array is assumed
2371 * to be equal to extra.
2373 __isl_give isl_basic_map *isl_basic_map_drop_core(
2374 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2375 unsigned first, unsigned n)
2377 int i;
2378 unsigned offset;
2379 unsigned left;
2380 isl_size total;
2382 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2383 return isl_basic_map_free(bmap);
2385 total = isl_basic_map_dim(bmap, isl_dim_all);
2386 if (total < 0)
2387 return isl_basic_map_free(bmap);
2389 offset = isl_basic_map_offset(bmap, type) + first;
2390 left = total - (offset - 1) - n;
2391 for (i = 0; i < bmap->n_eq; ++i)
2392 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2394 for (i = 0; i < bmap->n_ineq; ++i)
2395 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2397 for (i = 0; i < bmap->n_div; ++i)
2398 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2400 if (type == isl_dim_div) {
2401 bmap = move_divs_last(bmap, first, n);
2402 if (!bmap)
2403 return NULL;
2404 if (isl_basic_map_free_div(bmap, n) < 0)
2405 return isl_basic_map_free(bmap);
2406 } else
2407 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2408 if (!bmap->dim)
2409 return isl_basic_map_free(bmap);
2411 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2412 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2413 return bmap;
2416 /* Drop "n" dimensions of type "type" starting at "first".
2418 * In principle, this frees up some extra variables as the number
2419 * of columns remains constant, but we would have to extend
2420 * the div array too as the number of rows in this array is assumed
2421 * to be equal to extra.
2423 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2426 if (!bmap)
2427 return NULL;
2428 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2429 return bmap;
2431 bmap = isl_basic_map_cow(bmap);
2432 if (!bmap)
2433 return NULL;
2435 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2437 bmap = isl_basic_map_simplify(bmap);
2438 return isl_basic_map_finalize(bmap);
2441 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2442 enum isl_dim_type type, unsigned first, unsigned n)
2444 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2445 type, first, n));
2448 /* No longer consider "map" to be normalized.
2450 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2452 if (!map)
2453 return NULL;
2454 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2455 return map;
2458 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2459 enum isl_dim_type type, unsigned first, unsigned n)
2461 int i;
2462 isl_space *space;
2464 if (isl_map_check_range(map, type, first, n) < 0)
2465 return isl_map_free(map);
2467 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2468 return map;
2469 map = isl_map_cow(map);
2470 if (!map)
2471 goto error;
2473 for (i = 0; i < map->n; ++i) {
2474 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2475 if (!map->p[i])
2476 goto error;
2478 map = isl_map_unmark_normalized(map);
2480 space = isl_map_take_space(map);
2481 space = isl_space_drop_dims(space, type, first, n);
2482 map = isl_map_restore_space(map, space);
2484 return map;
2485 error:
2486 isl_map_free(map);
2487 return NULL;
2490 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2491 enum isl_dim_type type, unsigned first, unsigned n)
2493 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2496 /* Drop the integer division at position "div", which is assumed
2497 * not to appear in any of the constraints or
2498 * in any of the other integer divisions.
2500 * Since the integer division is redundant, there is no need to cow.
2502 __isl_give isl_basic_map *isl_basic_map_drop_div(
2503 __isl_take isl_basic_map *bmap, unsigned div)
2505 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2508 /* Eliminate the specified n dimensions starting at first from the
2509 * constraints, without removing the dimensions from the space.
2510 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2512 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2513 enum isl_dim_type type, unsigned first, unsigned n)
2515 int i;
2517 if (n == 0)
2518 return map;
2520 if (isl_map_check_range(map, type, first, n) < 0)
2521 return isl_map_free(map);
2523 map = isl_map_cow(map);
2524 if (!map)
2525 return NULL;
2527 for (i = 0; i < map->n; ++i) {
2528 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2529 if (!map->p[i])
2530 goto error;
2532 return map;
2533 error:
2534 isl_map_free(map);
2535 return NULL;
2538 /* Eliminate the specified n dimensions starting at first from the
2539 * constraints, without removing the dimensions from the space.
2540 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2542 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2543 enum isl_dim_type type, unsigned first, unsigned n)
2545 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2548 /* Eliminate the specified n dimensions starting at first from the
2549 * constraints, without removing the dimensions from the space.
2550 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2552 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2553 unsigned first, unsigned n)
2555 return isl_set_eliminate(set, isl_dim_set, first, n);
2558 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2559 __isl_take isl_basic_map *bmap)
2561 isl_size v_div;
2563 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2564 if (v_div < 0)
2565 return isl_basic_map_free(bmap);
2566 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2567 if (!bmap)
2568 return NULL;
2569 bmap->n_div = 0;
2570 return isl_basic_map_finalize(bmap);
2573 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2574 __isl_take isl_basic_set *bset)
2576 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2579 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2581 int i;
2583 if (!map)
2584 return NULL;
2585 if (map->n == 0)
2586 return map;
2588 map = isl_map_cow(map);
2589 if (!map)
2590 return NULL;
2592 for (i = 0; i < map->n; ++i) {
2593 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2594 if (!map->p[i])
2595 goto error;
2597 return map;
2598 error:
2599 isl_map_free(map);
2600 return NULL;
2603 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2605 return isl_map_remove_divs(set);
2608 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2609 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2610 unsigned first, unsigned n)
2612 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2613 return isl_basic_map_free(bmap);
2614 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2615 return bmap;
2616 bmap = isl_basic_map_eliminate_vars(bmap,
2617 isl_basic_map_offset(bmap, type) - 1 + first, n);
2618 if (!bmap)
2619 return bmap;
2620 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2621 return bmap;
2622 bmap = isl_basic_map_drop(bmap, type, first, n);
2623 return bmap;
2626 /* Return true if the definition of the given div (recursively) involves
2627 * any of the given variables.
2629 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2630 unsigned first, unsigned n)
2632 int i;
2633 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2635 if (isl_int_is_zero(bmap->div[div][0]))
2636 return isl_bool_false;
2637 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2638 return isl_bool_true;
2640 for (i = bmap->n_div - 1; i >= 0; --i) {
2641 isl_bool involves;
2643 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2644 continue;
2645 involves = div_involves_vars(bmap, i, first, n);
2646 if (involves < 0 || involves)
2647 return involves;
2650 return isl_bool_false;
2653 /* Try and add a lower and/or upper bound on "div" to "bmap"
2654 * based on inequality "i".
2655 * "total" is the total number of variables (excluding the divs).
2656 * "v" is a temporary object that can be used during the calculations.
2657 * If "lb" is set, then a lower bound should be constructed.
2658 * If "ub" is set, then an upper bound should be constructed.
2660 * The calling function has already checked that the inequality does not
2661 * reference "div", but we still need to check that the inequality is
2662 * of the right form. We'll consider the case where we want to construct
2663 * a lower bound. The construction of upper bounds is similar.
2665 * Let "div" be of the form
2667 * q = floor((a + f(x))/d)
2669 * We essentially check if constraint "i" is of the form
2671 * b + f(x) >= 0
2673 * so that we can use it to derive a lower bound on "div".
2674 * However, we allow a slightly more general form
2676 * b + g(x) >= 0
2678 * with the condition that the coefficients of g(x) - f(x) are all
2679 * divisible by d.
2680 * Rewriting this constraint as
2682 * 0 >= -b - g(x)
2684 * adding a + f(x) to both sides and dividing by d, we obtain
2686 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2688 * Taking the floor on both sides, we obtain
2690 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2692 * or
2694 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2696 * In the case of an upper bound, we construct the constraint
2698 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2701 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2702 __isl_take isl_basic_map *bmap, int div, int i,
2703 unsigned total, isl_int v, int lb, int ub)
2705 int j;
2707 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2708 if (lb) {
2709 isl_int_sub(v, bmap->ineq[i][1 + j],
2710 bmap->div[div][1 + 1 + j]);
2711 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2713 if (ub) {
2714 isl_int_add(v, bmap->ineq[i][1 + j],
2715 bmap->div[div][1 + 1 + j]);
2716 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2719 if (!lb && !ub)
2720 return bmap;
2722 bmap = isl_basic_map_cow(bmap);
2723 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2724 if (lb) {
2725 int k = isl_basic_map_alloc_inequality(bmap);
2726 if (k < 0)
2727 goto error;
2728 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2729 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2730 bmap->div[div][1 + j]);
2731 isl_int_cdiv_q(bmap->ineq[k][j],
2732 bmap->ineq[k][j], bmap->div[div][0]);
2734 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2736 if (ub) {
2737 int k = isl_basic_map_alloc_inequality(bmap);
2738 if (k < 0)
2739 goto error;
2740 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2741 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2742 bmap->div[div][1 + j]);
2743 isl_int_fdiv_q(bmap->ineq[k][j],
2744 bmap->ineq[k][j], bmap->div[div][0]);
2746 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2749 return bmap;
2750 error:
2751 isl_basic_map_free(bmap);
2752 return NULL;
2755 /* This function is called right before "div" is eliminated from "bmap"
2756 * using Fourier-Motzkin.
2757 * Look through the constraints of "bmap" for constraints on the argument
2758 * of the integer division and use them to construct constraints on the
2759 * integer division itself. These constraints can then be combined
2760 * during the Fourier-Motzkin elimination.
2761 * Note that it is only useful to introduce lower bounds on "div"
2762 * if "bmap" already contains upper bounds on "div" as the newly
2763 * introduce lower bounds can then be combined with the pre-existing
2764 * upper bounds. Similarly for upper bounds.
2765 * We therefore first check if "bmap" contains any lower and/or upper bounds
2766 * on "div".
2768 * It is interesting to note that the introduction of these constraints
2769 * can indeed lead to more accurate results, even when compared to
2770 * deriving constraints on the argument of "div" from constraints on "div".
2771 * Consider, for example, the set
2773 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2775 * The second constraint can be rewritten as
2777 * 2 * [(-i-2j+3)/4] + k >= 0
2779 * from which we can derive
2781 * -i - 2j + 3 >= -2k
2783 * or
2785 * i + 2j <= 3 + 2k
2787 * Combined with the first constraint, we obtain
2789 * -3 <= 3 + 2k or k >= -3
2791 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2792 * the first constraint, we obtain
2794 * [(i + 2j)/4] >= [-3/4] = -1
2796 * Combining this constraint with the second constraint, we obtain
2798 * k >= -2
2800 static __isl_give isl_basic_map *insert_bounds_on_div(
2801 __isl_take isl_basic_map *bmap, int div)
2803 int i;
2804 int check_lb, check_ub;
2805 isl_int v;
2806 isl_size v_div;
2808 if (!bmap)
2809 return NULL;
2811 if (isl_int_is_zero(bmap->div[div][0]))
2812 return bmap;
2814 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2815 if (v_div < 0)
2816 return isl_basic_map_free(bmap);
2818 check_lb = 0;
2819 check_ub = 0;
2820 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2821 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2822 if (s > 0)
2823 check_ub = 1;
2824 if (s < 0)
2825 check_lb = 1;
2828 if (!check_lb && !check_ub)
2829 return bmap;
2831 isl_int_init(v);
2833 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2834 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2835 continue;
2837 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2838 check_lb, check_ub);
2841 isl_int_clear(v);
2843 return bmap;
2846 /* Remove all divs (recursively) involving any of the given dimensions
2847 * in their definitions.
2849 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2850 __isl_take isl_basic_map *bmap,
2851 enum isl_dim_type type, unsigned first, unsigned n)
2853 int i;
2855 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2856 return isl_basic_map_free(bmap);
2857 first += isl_basic_map_offset(bmap, type);
2859 for (i = bmap->n_div - 1; i >= 0; --i) {
2860 isl_bool involves;
2862 involves = div_involves_vars(bmap, i, first, n);
2863 if (involves < 0)
2864 return isl_basic_map_free(bmap);
2865 if (!involves)
2866 continue;
2867 bmap = insert_bounds_on_div(bmap, i);
2868 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2869 if (!bmap)
2870 return NULL;
2871 i = bmap->n_div;
2874 return bmap;
2877 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2878 __isl_take isl_basic_set *bset,
2879 enum isl_dim_type type, unsigned first, unsigned n)
2881 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2884 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2885 enum isl_dim_type type, unsigned first, unsigned n)
2887 int i;
2889 if (!map)
2890 return NULL;
2891 if (map->n == 0)
2892 return map;
2894 map = isl_map_cow(map);
2895 if (!map)
2896 return NULL;
2898 for (i = 0; i < map->n; ++i) {
2899 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2900 type, first, n);
2901 if (!map->p[i])
2902 goto error;
2904 return map;
2905 error:
2906 isl_map_free(map);
2907 return NULL;
2910 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2911 enum isl_dim_type type, unsigned first, unsigned n)
2913 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2914 type, first, n));
2917 /* Does the description of "bmap" depend on the specified dimensions?
2918 * We also check whether the dimensions appear in any of the div definitions.
2919 * In principle there is no need for this check. If the dimensions appear
2920 * in a div definition, they also appear in the defining constraints of that
2921 * div.
2923 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2924 enum isl_dim_type type, unsigned first, unsigned n)
2926 int i;
2928 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2929 return isl_bool_error;
2931 first += isl_basic_map_offset(bmap, type);
2932 for (i = 0; i < bmap->n_eq; ++i)
2933 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2934 return isl_bool_true;
2935 for (i = 0; i < bmap->n_ineq; ++i)
2936 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2937 return isl_bool_true;
2938 for (i = 0; i < bmap->n_div; ++i) {
2939 if (isl_int_is_zero(bmap->div[i][0]))
2940 continue;
2941 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2942 return isl_bool_true;
2945 return isl_bool_false;
2948 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2949 enum isl_dim_type type, unsigned first, unsigned n)
2951 int i;
2953 if (isl_map_check_range(map, type, first, n) < 0)
2954 return isl_bool_error;
2956 for (i = 0; i < map->n; ++i) {
2957 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2958 type, first, n);
2959 if (involves < 0 || involves)
2960 return involves;
2963 return isl_bool_false;
2966 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2967 enum isl_dim_type type, unsigned first, unsigned n)
2969 return isl_basic_map_involves_dims(bset, type, first, n);
2972 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2975 return isl_map_involves_dims(set, type, first, n);
2978 /* Does "bset" involve any local variables, i.e., integer divisions?
2980 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2982 isl_size n;
2984 n = isl_basic_set_dim(bset, isl_dim_div);
2985 if (n < 0)
2986 return isl_bool_error;
2987 return isl_bool_ok(n > 0);
2990 /* isl_set_every_basic_set callback that checks whether "bset"
2991 * is free of local variables.
2993 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
2995 return isl_bool_not(isl_basic_set_involves_locals(bset));
2998 /* Does "set" involve any local variables, i.e., integer divisions?
3000 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3002 isl_bool no_locals;
3004 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3005 return isl_bool_not(no_locals);
3008 /* Drop all constraints in bmap that involve any of the dimensions
3009 * first to first+n-1.
3010 * This function only performs the actual removal of constraints.
3012 * This function should not call finalize since it is used by
3013 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3015 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3016 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3018 int i;
3020 if (n == 0)
3021 return bmap;
3023 bmap = isl_basic_map_cow(bmap);
3025 if (!bmap)
3026 return NULL;
3028 for (i = bmap->n_eq - 1; i >= 0; --i) {
3029 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3030 continue;
3031 if (isl_basic_map_drop_equality(bmap, i) < 0)
3032 return isl_basic_map_free(bmap);
3035 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3036 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3037 continue;
3038 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3039 return isl_basic_map_free(bmap);
3042 return bmap;
3045 /* Drop all constraints in bset that involve any of the dimensions
3046 * first to first+n-1.
3047 * This function only performs the actual removal of constraints.
3049 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3050 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3052 return isl_basic_map_drop_constraints_involving(bset, first, n);
3055 /* Drop all constraints in bmap that do not involve any of the dimensions
3056 * first to first + n - 1 of the given type.
3058 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3059 __isl_take isl_basic_map *bmap,
3060 enum isl_dim_type type, unsigned first, unsigned n)
3062 int i;
3064 if (n == 0) {
3065 isl_space *space = isl_basic_map_get_space(bmap);
3066 isl_basic_map_free(bmap);
3067 return isl_basic_map_universe(space);
3069 bmap = isl_basic_map_cow(bmap);
3070 if (!bmap)
3071 return NULL;
3073 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3074 return isl_basic_map_free(bmap);
3076 first += isl_basic_map_offset(bmap, type) - 1;
3078 for (i = bmap->n_eq - 1; i >= 0; --i) {
3079 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3080 continue;
3081 if (isl_basic_map_drop_equality(bmap, i) < 0)
3082 return isl_basic_map_free(bmap);
3085 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3086 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3087 continue;
3088 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3089 return isl_basic_map_free(bmap);
3092 bmap = isl_basic_map_add_known_div_constraints(bmap);
3093 return bmap;
3096 /* Drop all constraints in bset that do not involve any of the dimensions
3097 * first to first + n - 1 of the given type.
3099 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3100 __isl_take isl_basic_set *bset,
3101 enum isl_dim_type type, unsigned first, unsigned n)
3103 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3104 type, first, n);
3107 /* Drop all constraints in bmap that involve any of the dimensions
3108 * first to first + n - 1 of the given type.
3110 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3111 __isl_take isl_basic_map *bmap,
3112 enum isl_dim_type type, unsigned first, unsigned n)
3114 if (!bmap)
3115 return NULL;
3116 if (n == 0)
3117 return bmap;
3119 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3120 return isl_basic_map_free(bmap);
3122 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3123 first += isl_basic_map_offset(bmap, type) - 1;
3124 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3125 bmap = isl_basic_map_add_known_div_constraints(bmap);
3126 return bmap;
3129 /* Drop all constraints in bset that involve any of the dimensions
3130 * first to first + n - 1 of the given type.
3132 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3133 __isl_take isl_basic_set *bset,
3134 enum isl_dim_type type, unsigned first, unsigned n)
3136 return isl_basic_map_drop_constraints_involving_dims(bset,
3137 type, first, n);
3140 /* Drop constraints from "map" by applying "drop" to each basic map.
3142 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3143 enum isl_dim_type type, unsigned first, unsigned n,
3144 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3145 enum isl_dim_type type, unsigned first, unsigned n))
3147 int i;
3149 if (isl_map_check_range(map, type, first, n) < 0)
3150 return isl_map_free(map);
3152 map = isl_map_cow(map);
3153 if (!map)
3154 return NULL;
3156 for (i = 0; i < map->n; ++i) {
3157 map->p[i] = drop(map->p[i], type, first, n);
3158 if (!map->p[i])
3159 return isl_map_free(map);
3162 if (map->n > 1)
3163 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3165 return map;
3168 /* Drop all constraints in map that involve any of the dimensions
3169 * first to first + n - 1 of the given type.
3171 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3172 __isl_take isl_map *map,
3173 enum isl_dim_type type, unsigned first, unsigned n)
3175 if (n == 0)
3176 return map;
3177 return drop_constraints(map, type, first, n,
3178 &isl_basic_map_drop_constraints_involving_dims);
3181 /* Drop all constraints in "map" that do not involve any of the dimensions
3182 * first to first + n - 1 of the given type.
3184 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3185 __isl_take isl_map *map,
3186 enum isl_dim_type type, unsigned first, unsigned n)
3188 if (n == 0) {
3189 isl_space *space = isl_map_get_space(map);
3190 isl_map_free(map);
3191 return isl_map_universe(space);
3193 return drop_constraints(map, type, first, n,
3194 &isl_basic_map_drop_constraints_not_involving_dims);
3197 /* Drop all constraints in set that involve any of the dimensions
3198 * first to first + n - 1 of the given type.
3200 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3201 __isl_take isl_set *set,
3202 enum isl_dim_type type, unsigned first, unsigned n)
3204 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3207 /* Drop all constraints in "set" that do not involve any of the dimensions
3208 * first to first + n - 1 of the given type.
3210 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3211 __isl_take isl_set *set,
3212 enum isl_dim_type type, unsigned first, unsigned n)
3214 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3217 /* Does local variable "div" of "bmap" have a complete explicit representation?
3218 * Having a complete explicit representation requires not only
3219 * an explicit representation, but also that all local variables
3220 * that appear in this explicit representation in turn have
3221 * a complete explicit representation.
3223 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3225 int i;
3226 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3227 isl_bool marked;
3229 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3230 if (marked < 0 || marked)
3231 return isl_bool_not(marked);
3233 for (i = bmap->n_div - 1; i >= 0; --i) {
3234 isl_bool known;
3236 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3237 continue;
3238 known = isl_basic_map_div_is_known(bmap, i);
3239 if (known < 0 || !known)
3240 return known;
3243 return isl_bool_true;
3246 /* Remove all divs that are unknown or defined in terms of unknown divs.
3248 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3249 __isl_take isl_basic_map *bmap)
3251 int i;
3253 if (!bmap)
3254 return NULL;
3256 for (i = bmap->n_div - 1; i >= 0; --i) {
3257 if (isl_basic_map_div_is_known(bmap, i))
3258 continue;
3259 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3260 if (!bmap)
3261 return NULL;
3262 i = bmap->n_div;
3265 return bmap;
3268 /* Remove all divs that are unknown or defined in terms of unknown divs.
3270 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3271 __isl_take isl_basic_set *bset)
3273 return isl_basic_map_remove_unknown_divs(bset);
3276 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3278 int i;
3280 if (!map)
3281 return NULL;
3282 if (map->n == 0)
3283 return map;
3285 map = isl_map_cow(map);
3286 if (!map)
3287 return NULL;
3289 for (i = 0; i < map->n; ++i) {
3290 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3291 if (!map->p[i])
3292 goto error;
3294 return map;
3295 error:
3296 isl_map_free(map);
3297 return NULL;
3300 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3302 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3305 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3306 __isl_take isl_basic_set *bset,
3307 enum isl_dim_type type, unsigned first, unsigned n)
3309 isl_basic_map *bmap = bset_to_bmap(bset);
3310 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3311 return bset_from_bmap(bmap);
3314 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3315 enum isl_dim_type type, unsigned first, unsigned n)
3317 int i;
3319 if (n == 0)
3320 return map;
3322 map = isl_map_cow(map);
3323 if (isl_map_check_range(map, type, first, n) < 0)
3324 return isl_map_free(map);
3326 for (i = 0; i < map->n; ++i) {
3327 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3328 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3329 if (!map->p[i])
3330 goto error;
3332 map = isl_map_drop(map, type, first, n);
3333 return map;
3334 error:
3335 isl_map_free(map);
3336 return NULL;
3339 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3340 enum isl_dim_type type, unsigned first, unsigned n)
3342 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3343 type, first, n));
3346 /* Project out n inputs starting at first using Fourier-Motzkin */
3347 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3348 unsigned first, unsigned n)
3350 return isl_map_remove_dims(map, isl_dim_in, first, n);
3353 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3354 FILE *out, int indent)
3356 isl_printer *p;
3358 if (!bset) {
3359 fprintf(out, "null basic set\n");
3360 return;
3363 fprintf(out, "%*s", indent, "");
3364 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3365 bset->ref, bset->dim->nparam, bset->dim->n_out,
3366 bset->extra, bset->flags);
3368 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3369 p = isl_printer_set_dump(p, 1);
3370 p = isl_printer_set_indent(p, indent);
3371 p = isl_printer_start_line(p);
3372 p = isl_printer_print_basic_set(p, bset);
3373 p = isl_printer_end_line(p);
3374 isl_printer_free(p);
3377 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3378 FILE *out, int indent)
3380 isl_printer *p;
3382 if (!bmap) {
3383 fprintf(out, "null basic map\n");
3384 return;
3387 fprintf(out, "%*s", indent, "");
3388 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3389 "flags: %x, n_name: %d\n",
3390 bmap->ref,
3391 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3392 bmap->extra, bmap->flags, bmap->dim->n_id);
3394 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3395 p = isl_printer_set_dump(p, 1);
3396 p = isl_printer_set_indent(p, indent);
3397 p = isl_printer_start_line(p);
3398 p = isl_printer_print_basic_map(p, bmap);
3399 p = isl_printer_end_line(p);
3400 isl_printer_free(p);
3403 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3404 unsigned pos)
3406 isl_size total;
3408 total = isl_basic_map_dim(bmap, isl_dim_all);
3409 if (total < 0)
3410 return isl_basic_map_free(bmap);
3411 if (pos >= bmap->n_ineq)
3412 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3413 "invalid position", return isl_basic_map_free(bmap));
3414 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3415 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3416 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3417 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3418 return bmap;
3421 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3422 unsigned flags)
3424 if (isl_space_check_is_set(space) < 0)
3425 goto error;
3426 return isl_map_alloc_space(space, n, flags);
3427 error:
3428 isl_space_free(space);
3429 return NULL;
3432 /* Make sure "map" has room for at least "n" more basic maps.
3434 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3436 int i;
3437 struct isl_map *grown = NULL;
3439 if (!map)
3440 return NULL;
3441 isl_assert(map->ctx, n >= 0, goto error);
3442 if (map->n + n <= map->size)
3443 return map;
3444 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3445 if (!grown)
3446 goto error;
3447 for (i = 0; i < map->n; ++i) {
3448 grown->p[i] = isl_basic_map_copy(map->p[i]);
3449 if (!grown->p[i])
3450 goto error;
3451 grown->n++;
3453 isl_map_free(map);
3454 return grown;
3455 error:
3456 isl_map_free(grown);
3457 isl_map_free(map);
3458 return NULL;
3461 /* Make sure "set" has room for at least "n" more basic sets.
3463 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3465 return set_from_map(isl_map_grow(set_to_map(set), n));
3468 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3470 return isl_map_from_basic_map(bset);
3473 /* This function performs the same operation as isl_set_from_basic_set,
3474 * but is considered as a function on an isl_basic_set when exported.
3476 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3478 return isl_set_from_basic_set(bset);
3481 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3483 struct isl_map *map;
3485 if (!bmap)
3486 return NULL;
3488 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3489 return isl_map_add_basic_map(map, bmap);
3492 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3493 __isl_take isl_basic_set *bset)
3495 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3496 bset_to_bmap(bset)));
3499 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3501 return isl_map_free(set);
3504 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3506 int i;
3508 if (!set) {
3509 fprintf(out, "null set\n");
3510 return;
3513 fprintf(out, "%*s", indent, "");
3514 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3515 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3516 set->flags);
3517 for (i = 0; i < set->n; ++i) {
3518 fprintf(out, "%*s", indent, "");
3519 fprintf(out, "basic set %d:\n", i);
3520 isl_basic_set_print_internal(set->p[i], out, indent+4);
3524 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3526 int i;
3528 if (!map) {
3529 fprintf(out, "null map\n");
3530 return;
3533 fprintf(out, "%*s", indent, "");
3534 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3535 "flags: %x, n_name: %d\n",
3536 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3537 map->dim->n_out, map->flags, map->dim->n_id);
3538 for (i = 0; i < map->n; ++i) {
3539 fprintf(out, "%*s", indent, "");
3540 fprintf(out, "basic map %d:\n", i);
3541 isl_basic_map_print_internal(map->p[i], out, indent+4);
3545 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3547 static isl_stat isl_basic_map_check_compatible_domain(
3548 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3550 isl_bool ok;
3552 ok = isl_basic_map_compatible_domain(bmap, bset);
3553 if (ok < 0)
3554 return isl_stat_error;
3555 if (!ok)
3556 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3557 "incompatible spaces", return isl_stat_error);
3559 return isl_stat_ok;
3562 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3563 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3565 struct isl_basic_map *bmap_domain;
3566 isl_size dim;
3568 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3569 goto error;
3571 dim = isl_basic_set_dim(bset, isl_dim_set);
3572 if (dim < 0)
3573 goto error;
3574 if (dim != 0 &&
3575 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3576 goto error;
3578 bmap = isl_basic_map_cow(bmap);
3579 if (!bmap)
3580 goto error;
3581 bmap = isl_basic_map_extend(bmap,
3582 bset->n_div, bset->n_eq, bset->n_ineq);
3583 bmap_domain = isl_basic_map_from_domain(bset);
3584 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3586 bmap = isl_basic_map_simplify(bmap);
3587 return isl_basic_map_finalize(bmap);
3588 error:
3589 isl_basic_map_free(bmap);
3590 isl_basic_set_free(bset);
3591 return NULL;
3594 /* Check that the space of "bset" is the same as that of the range of "bmap".
3596 static isl_stat isl_basic_map_check_compatible_range(
3597 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3599 isl_bool ok;
3601 ok = isl_basic_map_compatible_range(bmap, bset);
3602 if (ok < 0)
3603 return isl_stat_error;
3604 if (!ok)
3605 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3606 "incompatible spaces", return isl_stat_error);
3608 return isl_stat_ok;
3611 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3612 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3614 struct isl_basic_map *bmap_range;
3615 isl_size dim;
3617 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3618 goto error;
3620 dim = isl_basic_set_dim(bset, isl_dim_set);
3621 if (dim < 0)
3622 goto error;
3623 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3624 goto error;
3626 if (isl_basic_set_plain_is_universe(bset)) {
3627 isl_basic_set_free(bset);
3628 return bmap;
3631 bmap = isl_basic_map_cow(bmap);
3632 if (!bmap)
3633 goto error;
3634 bmap = isl_basic_map_extend(bmap,
3635 bset->n_div, bset->n_eq, bset->n_ineq);
3636 bmap_range = bset_to_bmap(bset);
3637 bmap = add_constraints(bmap, bmap_range, 0, 0);
3639 bmap = isl_basic_map_simplify(bmap);
3640 return isl_basic_map_finalize(bmap);
3641 error:
3642 isl_basic_map_free(bmap);
3643 isl_basic_set_free(bset);
3644 return NULL;
3647 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3648 __isl_keep isl_vec *vec)
3650 int i;
3651 isl_size total;
3652 isl_int s;
3654 total = isl_basic_map_dim(bmap, isl_dim_all);
3655 if (total < 0 || !vec)
3656 return isl_bool_error;
3658 if (1 + total != vec->size)
3659 return isl_bool_false;
3661 isl_int_init(s);
3663 for (i = 0; i < bmap->n_eq; ++i) {
3664 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3665 if (!isl_int_is_zero(s)) {
3666 isl_int_clear(s);
3667 return isl_bool_false;
3671 for (i = 0; i < bmap->n_ineq; ++i) {
3672 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3673 if (isl_int_is_neg(s)) {
3674 isl_int_clear(s);
3675 return isl_bool_false;
3679 isl_int_clear(s);
3681 return isl_bool_true;
3684 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3685 __isl_keep isl_vec *vec)
3687 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3690 __isl_give isl_basic_map *isl_basic_map_intersect(
3691 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3693 struct isl_vec *sample = NULL;
3694 isl_space *space1, *space2;
3695 isl_size dim1, dim2, nparam1, nparam2;
3697 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3698 goto error;
3699 space1 = isl_basic_map_peek_space(bmap1);
3700 space2 = isl_basic_map_peek_space(bmap2);
3701 dim1 = isl_space_dim(space1, isl_dim_all);
3702 dim2 = isl_space_dim(space2, isl_dim_all);
3703 nparam1 = isl_space_dim(space1, isl_dim_param);
3704 nparam2 = isl_space_dim(space2, isl_dim_param);
3705 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3706 goto error;
3707 if (dim1 == nparam1 && dim2 != nparam2)
3708 return isl_basic_map_intersect(bmap2, bmap1);
3710 if (dim2 != nparam2 &&
3711 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3712 goto error;
3714 if (isl_basic_map_plain_is_empty(bmap1)) {
3715 isl_basic_map_free(bmap2);
3716 return bmap1;
3718 if (isl_basic_map_plain_is_empty(bmap2)) {
3719 isl_basic_map_free(bmap1);
3720 return bmap2;
3723 if (bmap1->sample &&
3724 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3725 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3726 sample = isl_vec_copy(bmap1->sample);
3727 else if (bmap2->sample &&
3728 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3729 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3730 sample = isl_vec_copy(bmap2->sample);
3732 bmap1 = isl_basic_map_cow(bmap1);
3733 if (!bmap1)
3734 goto error;
3735 bmap1 = isl_basic_map_extend(bmap1,
3736 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3737 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3739 if (!bmap1)
3740 isl_vec_free(sample);
3741 else if (sample) {
3742 isl_vec_free(bmap1->sample);
3743 bmap1->sample = sample;
3746 bmap1 = isl_basic_map_simplify(bmap1);
3747 return isl_basic_map_finalize(bmap1);
3748 error:
3749 if (sample)
3750 isl_vec_free(sample);
3751 isl_basic_map_free(bmap1);
3752 isl_basic_map_free(bmap2);
3753 return NULL;
3756 __isl_give isl_basic_set *isl_basic_set_intersect(
3757 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3759 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3760 bset_to_bmap(bset2)));
3763 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3764 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3766 return isl_basic_set_intersect(bset1, bset2);
3769 /* Special case of isl_map_intersect, where both map1 and map2
3770 * are convex, without any divs and such that either map1 or map2
3771 * contains a single constraint. This constraint is then simply
3772 * added to the other map.
3774 static __isl_give isl_map *map_intersect_add_constraint(
3775 __isl_take isl_map *map1, __isl_take isl_map *map2)
3777 isl_assert(map1->ctx, map1->n == 1, goto error);
3778 isl_assert(map2->ctx, map1->n == 1, goto error);
3779 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3780 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3782 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3783 return isl_map_intersect(map2, map1);
3785 map1 = isl_map_cow(map1);
3786 if (!map1)
3787 goto error;
3788 if (isl_map_plain_is_empty(map1)) {
3789 isl_map_free(map2);
3790 return map1;
3792 if (map2->p[0]->n_eq == 1)
3793 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3794 else
3795 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3796 map2->p[0]->ineq[0]);
3798 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3799 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3800 if (!map1->p[0])
3801 goto error;
3803 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3804 isl_basic_map_free(map1->p[0]);
3805 map1->n = 0;
3808 isl_map_free(map2);
3810 map1 = isl_map_unmark_normalized(map1);
3811 return map1;
3812 error:
3813 isl_map_free(map1);
3814 isl_map_free(map2);
3815 return NULL;
3818 /* map2 may be either a parameter domain or a map living in the same
3819 * space as map1.
3821 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3822 __isl_take isl_map *map2)
3824 unsigned flags = 0;
3825 isl_bool equal;
3826 isl_map *result;
3827 int i, j;
3828 isl_size dim2, nparam2;
3830 if (!map1 || !map2)
3831 goto error;
3833 if ((isl_map_plain_is_empty(map1) ||
3834 isl_map_plain_is_universe(map2)) &&
3835 isl_space_is_equal(map1->dim, map2->dim)) {
3836 isl_map_free(map2);
3837 return map1;
3839 if ((isl_map_plain_is_empty(map2) ||
3840 isl_map_plain_is_universe(map1)) &&
3841 isl_space_is_equal(map1->dim, map2->dim)) {
3842 isl_map_free(map1);
3843 return map2;
3846 if (map1->n == 1 && map2->n == 1 &&
3847 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3848 isl_space_is_equal(map1->dim, map2->dim) &&
3849 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3850 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3851 return map_intersect_add_constraint(map1, map2);
3853 equal = isl_map_plain_is_equal(map1, map2);
3854 if (equal < 0)
3855 goto error;
3856 if (equal) {
3857 isl_map_free(map2);
3858 return map1;
3861 dim2 = isl_map_dim(map2, isl_dim_all);
3862 nparam2 = isl_map_dim(map2, isl_dim_param);
3863 if (dim2 < 0 || nparam2 < 0)
3864 goto error;
3865 if (dim2 != nparam2)
3866 isl_assert(map1->ctx,
3867 isl_space_is_equal(map1->dim, map2->dim), goto error);
3869 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3870 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3871 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3873 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3874 map1->n * map2->n, flags);
3875 if (!result)
3876 goto error;
3877 for (i = 0; i < map1->n; ++i)
3878 for (j = 0; j < map2->n; ++j) {
3879 struct isl_basic_map *part;
3880 part = isl_basic_map_intersect(
3881 isl_basic_map_copy(map1->p[i]),
3882 isl_basic_map_copy(map2->p[j]));
3883 if (isl_basic_map_is_empty(part) < 0)
3884 part = isl_basic_map_free(part);
3885 result = isl_map_add_basic_map(result, part);
3886 if (!result)
3887 goto error;
3889 isl_map_free(map1);
3890 isl_map_free(map2);
3891 return result;
3892 error:
3893 isl_map_free(map1);
3894 isl_map_free(map2);
3895 return NULL;
3898 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3899 __isl_take isl_map *map2)
3901 if (isl_map_check_equal_space(map1, map2) < 0)
3902 goto error;
3903 return map_intersect_internal(map1, map2);
3904 error:
3905 isl_map_free(map1);
3906 isl_map_free(map2);
3907 return NULL;
3910 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3911 __isl_take isl_map *map2)
3913 isl_map_align_params_bin(&map1, &map2);
3914 return map_intersect(map1, map2);
3917 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3918 __isl_take isl_set *set2)
3920 return set_from_map(isl_map_intersect(set_to_map(set1),
3921 set_to_map(set2)));
3924 /* map_intersect_internal accepts intersections
3925 * with parameter domains, so we can just call that function.
3927 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3928 __isl_take isl_set *params)
3930 isl_map_align_params_set(&map, &params);
3931 return map_intersect_internal(map, params);
3934 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3935 __isl_take isl_set *params)
3937 return isl_map_intersect_params(set, params);
3940 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3942 isl_space *space;
3943 unsigned pos;
3944 isl_size n1, n2;
3946 if (!bmap)
3947 return NULL;
3948 bmap = isl_basic_map_cow(bmap);
3949 if (!bmap)
3950 return NULL;
3951 space = isl_space_reverse(isl_space_copy(bmap->dim));
3952 pos = isl_basic_map_offset(bmap, isl_dim_in);
3953 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3954 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3955 if (n1 < 0 || n2 < 0)
3956 bmap = isl_basic_map_free(bmap);
3957 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3958 return isl_basic_map_reset_space(bmap, space);
3961 /* Given a basic map A -> (B -> C), return the corresponding basic map
3962 * A -> (C -> B).
3964 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3965 __isl_take isl_basic_map *bmap)
3967 isl_space *space;
3968 isl_size offset, n1, n2;
3970 space = isl_basic_map_peek_space(bmap);
3971 if (isl_space_check_range_is_wrapping(space) < 0)
3972 return isl_basic_map_free(bmap);
3973 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3974 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3975 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3976 if (offset < 0 || n1 < 0 || n2 < 0)
3977 return isl_basic_map_free(bmap);
3979 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3981 space = isl_basic_map_take_space(bmap);
3982 space = isl_space_range_reverse(space);
3983 bmap = isl_basic_map_restore_space(bmap, space);
3985 return bmap;
3988 static __isl_give isl_basic_map *basic_map_space_reset(
3989 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3991 isl_space *space;
3993 if (!bmap)
3994 return NULL;
3995 if (!isl_space_is_named_or_nested(bmap->dim, type))
3996 return bmap;
3998 space = isl_basic_map_get_space(bmap);
3999 space = isl_space_reset(space, type);
4000 bmap = isl_basic_map_reset_space(bmap, space);
4001 return bmap;
4004 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4005 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4006 unsigned pos, unsigned n)
4008 isl_bool rational, is_empty;
4009 isl_space *res_space;
4010 struct isl_basic_map *res;
4011 struct isl_dim_map *dim_map;
4012 isl_size total;
4013 unsigned off;
4014 enum isl_dim_type t;
4016 if (n == 0)
4017 return basic_map_space_reset(bmap, type);
4019 is_empty = isl_basic_map_plain_is_empty(bmap);
4020 total = isl_basic_map_dim(bmap, isl_dim_all);
4021 if (is_empty < 0 || total < 0)
4022 return isl_basic_map_free(bmap);
4023 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4024 type, pos, n);
4025 if (!res_space)
4026 return isl_basic_map_free(bmap);
4027 if (is_empty) {
4028 isl_basic_map_free(bmap);
4029 return isl_basic_map_empty(res_space);
4032 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4033 off = 0;
4034 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4035 isl_size dim;
4037 if (t != type) {
4038 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4039 } else {
4040 isl_size size = isl_basic_map_dim(bmap, t);
4041 if (size < 0)
4042 dim_map = isl_dim_map_free(dim_map);
4043 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4044 0, pos, off);
4045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4046 pos, size - pos, off + pos + n);
4048 dim = isl_space_dim(res_space, t);
4049 if (dim < 0)
4050 dim_map = isl_dim_map_free(dim_map);
4051 off += dim;
4053 isl_dim_map_div(dim_map, bmap, off);
4055 res = isl_basic_map_alloc_space(res_space,
4056 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4057 rational = isl_basic_map_is_rational(bmap);
4058 if (rational < 0)
4059 res = isl_basic_map_free(res);
4060 if (rational)
4061 res = isl_basic_map_set_rational(res);
4062 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4063 return isl_basic_map_finalize(res);
4066 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4067 __isl_take isl_basic_set *bset,
4068 enum isl_dim_type type, unsigned pos, unsigned n)
4070 return isl_basic_map_insert_dims(bset, type, pos, n);
4073 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4074 enum isl_dim_type type, unsigned n)
4076 isl_size dim;
4078 dim = isl_basic_map_dim(bmap, type);
4079 if (dim < 0)
4080 return isl_basic_map_free(bmap);
4081 return isl_basic_map_insert_dims(bmap, type, dim, n);
4084 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4085 enum isl_dim_type type, unsigned n)
4087 if (!bset)
4088 return NULL;
4089 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4090 return isl_basic_map_add_dims(bset, type, n);
4091 error:
4092 isl_basic_set_free(bset);
4093 return NULL;
4096 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4097 enum isl_dim_type type)
4099 isl_space *space;
4101 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4102 return map;
4104 space = isl_map_get_space(map);
4105 space = isl_space_reset(space, type);
4106 map = isl_map_reset_space(map, space);
4107 return map;
4110 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4111 enum isl_dim_type type, unsigned pos, unsigned n)
4113 int i;
4114 isl_space *space;
4116 if (n == 0)
4117 return map_space_reset(map, type);
4119 map = isl_map_cow(map);
4120 if (!map)
4121 return NULL;
4123 for (i = 0; i < map->n; ++i) {
4124 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4125 if (!map->p[i])
4126 goto error;
4129 space = isl_map_take_space(map);
4130 space = isl_space_insert_dims(space, type, pos, n);
4131 map = isl_map_restore_space(map, space);
4133 return map;
4134 error:
4135 isl_map_free(map);
4136 return NULL;
4139 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4140 enum isl_dim_type type, unsigned pos, unsigned n)
4142 return isl_map_insert_dims(set, type, pos, n);
4145 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4146 enum isl_dim_type type, unsigned n)
4148 isl_size dim;
4150 dim = isl_map_dim(map, type);
4151 if (dim < 0)
4152 return isl_map_free(map);
4153 return isl_map_insert_dims(map, type, dim, n);
4156 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4157 enum isl_dim_type type, unsigned n)
4159 if (!set)
4160 return NULL;
4161 isl_assert(set->ctx, type != isl_dim_in, goto error);
4162 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4163 error:
4164 isl_set_free(set);
4165 return NULL;
4168 __isl_give isl_basic_map *isl_basic_map_move_dims(
4169 __isl_take isl_basic_map *bmap,
4170 enum isl_dim_type dst_type, unsigned dst_pos,
4171 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4173 isl_space *space;
4174 struct isl_dim_map *dim_map;
4175 struct isl_basic_map *res;
4176 enum isl_dim_type t;
4177 isl_size total;
4178 unsigned off;
4180 if (!bmap)
4181 return NULL;
4182 if (n == 0) {
4183 bmap = isl_basic_map_reset(bmap, src_type);
4184 bmap = isl_basic_map_reset(bmap, dst_type);
4185 return bmap;
4188 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4189 return isl_basic_map_free(bmap);
4191 if (dst_type == src_type && dst_pos == src_pos)
4192 return bmap;
4194 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4196 if (pos(bmap->dim, dst_type) + dst_pos ==
4197 pos(bmap->dim, src_type) + src_pos +
4198 ((src_type < dst_type) ? n : 0)) {
4199 space = isl_basic_map_take_space(bmap);
4200 space = isl_space_move_dims(space, dst_type, dst_pos,
4201 src_type, src_pos, n);
4202 bmap = isl_basic_map_restore_space(bmap, space);
4203 bmap = isl_basic_map_finalize(bmap);
4205 return bmap;
4208 total = isl_basic_map_dim(bmap, isl_dim_all);
4209 if (total < 0)
4210 return isl_basic_map_free(bmap);
4211 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4213 off = 0;
4214 space = isl_basic_map_peek_space(bmap);
4215 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4216 isl_size size = isl_space_dim(space, t);
4217 if (size < 0)
4218 dim_map = isl_dim_map_free(dim_map);
4219 if (t == dst_type) {
4220 isl_dim_map_dim_range(dim_map, space, t,
4221 0, dst_pos, off);
4222 off += dst_pos;
4223 isl_dim_map_dim_range(dim_map, space, src_type,
4224 src_pos, n, off);
4225 off += n;
4226 isl_dim_map_dim_range(dim_map, space, t,
4227 dst_pos, size - dst_pos, off);
4228 off += size - dst_pos;
4229 } else if (t == src_type) {
4230 isl_dim_map_dim_range(dim_map, space, t,
4231 0, src_pos, off);
4232 off += src_pos;
4233 isl_dim_map_dim_range(dim_map, space, t,
4234 src_pos + n, size - src_pos - n, off);
4235 off += size - src_pos - n;
4236 } else {
4237 isl_dim_map_dim(dim_map, space, t, off);
4238 off += size;
4241 isl_dim_map_div(dim_map, bmap, off);
4243 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4244 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4245 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4246 space = isl_basic_map_take_space(bmap);
4247 space = isl_space_move_dims(space, dst_type, dst_pos,
4248 src_type, src_pos, n);
4249 bmap = isl_basic_map_restore_space(bmap, space);
4250 if (!bmap)
4251 goto error;
4253 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4254 bmap = isl_basic_map_gauss(bmap, NULL);
4255 bmap = isl_basic_map_finalize(bmap);
4257 return bmap;
4258 error:
4259 isl_basic_map_free(bmap);
4260 return NULL;
4263 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4264 enum isl_dim_type dst_type, unsigned dst_pos,
4265 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4267 isl_basic_map *bmap = bset_to_bmap(bset);
4268 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4269 src_type, src_pos, n);
4270 return bset_from_bmap(bmap);
4273 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4274 enum isl_dim_type dst_type, unsigned dst_pos,
4275 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4277 if (!set)
4278 return NULL;
4279 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4280 return set_from_map(isl_map_move_dims(set_to_map(set),
4281 dst_type, dst_pos, src_type, src_pos, n));
4282 error:
4283 isl_set_free(set);
4284 return NULL;
4287 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4288 enum isl_dim_type dst_type, unsigned dst_pos,
4289 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4291 int i;
4292 isl_space *space;
4294 if (n == 0) {
4295 map = isl_map_reset(map, src_type);
4296 map = isl_map_reset(map, dst_type);
4297 return map;
4300 if (isl_map_check_range(map, src_type, src_pos, n))
4301 return isl_map_free(map);
4303 if (dst_type == src_type && dst_pos == src_pos)
4304 return map;
4306 isl_assert(map->ctx, dst_type != src_type, goto error);
4308 map = isl_map_cow(map);
4309 if (!map)
4310 return NULL;
4312 for (i = 0; i < map->n; ++i) {
4313 map->p[i] = isl_basic_map_move_dims(map->p[i],
4314 dst_type, dst_pos,
4315 src_type, src_pos, n);
4316 if (!map->p[i])
4317 goto error;
4320 space = isl_map_take_space(map);
4321 space = isl_space_move_dims(space, dst_type, dst_pos,
4322 src_type, src_pos, n);
4323 map = isl_map_restore_space(map, space);
4325 return map;
4326 error:
4327 isl_map_free(map);
4328 return NULL;
4331 /* Move the specified dimensions to the last columns right before
4332 * the divs. Don't change the dimension specification of bmap.
4333 * That's the responsibility of the caller.
4335 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4336 enum isl_dim_type type, unsigned first, unsigned n)
4338 isl_space *space;
4339 struct isl_dim_map *dim_map;
4340 struct isl_basic_map *res;
4341 enum isl_dim_type t;
4342 isl_size total;
4343 unsigned off;
4345 if (!bmap)
4346 return NULL;
4347 if (isl_basic_map_offset(bmap, type) + first + n ==
4348 isl_basic_map_offset(bmap, isl_dim_div))
4349 return bmap;
4351 total = isl_basic_map_dim(bmap, isl_dim_all);
4352 if (total < 0)
4353 return isl_basic_map_free(bmap);
4354 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4356 off = 0;
4357 space = isl_basic_map_peek_space(bmap);
4358 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4359 isl_size size = isl_space_dim(space, t);
4360 if (size < 0)
4361 dim_map = isl_dim_map_free(dim_map);
4362 if (t == type) {
4363 isl_dim_map_dim_range(dim_map, space, t,
4364 0, first, off);
4365 off += first;
4366 isl_dim_map_dim_range(dim_map, space, t,
4367 first, n, total - bmap->n_div - n);
4368 isl_dim_map_dim_range(dim_map, space, t,
4369 first + n, size - (first + n), off);
4370 off += size - (first + n);
4371 } else {
4372 isl_dim_map_dim(dim_map, space, t, off);
4373 off += size;
4376 isl_dim_map_div(dim_map, bmap, off + n);
4378 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4379 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4380 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4381 return res;
4384 /* Insert "n" rows in the divs of "bmap".
4386 * The number of columns is not changed, which means that the last
4387 * dimensions of "bmap" are being reintepreted as the new divs.
4388 * The space of "bmap" is not adjusted, however, which means
4389 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4390 * from the space of "bmap" is the responsibility of the caller.
4392 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4393 int n)
4395 int i;
4396 size_t row_size;
4397 isl_int **new_div;
4398 isl_int *old;
4400 bmap = isl_basic_map_cow(bmap);
4401 if (!bmap)
4402 return NULL;
4404 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4405 old = bmap->block2.data;
4406 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4407 (bmap->extra + n) * (1 + row_size));
4408 if (!bmap->block2.data)
4409 return isl_basic_map_free(bmap);
4410 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4411 if (!new_div)
4412 return isl_basic_map_free(bmap);
4413 for (i = 0; i < n; ++i) {
4414 new_div[i] = bmap->block2.data +
4415 (bmap->extra + i) * (1 + row_size);
4416 isl_seq_clr(new_div[i], 1 + row_size);
4418 for (i = 0; i < bmap->extra; ++i)
4419 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4420 free(bmap->div);
4421 bmap->div = new_div;
4422 bmap->n_div += n;
4423 bmap->extra += n;
4425 return bmap;
4428 /* Drop constraints from "bmap" that only involve the variables
4429 * of "type" in the range [first, first + n] that are not related
4430 * to any of the variables outside that interval.
4431 * These constraints cannot influence the values for the variables
4432 * outside the interval, except in case they cause "bmap" to be empty.
4433 * Only drop the constraints if "bmap" is known to be non-empty.
4435 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4436 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4437 unsigned first, unsigned n)
4439 int i;
4440 int *groups;
4441 isl_size dim, n_div;
4442 isl_bool non_empty;
4444 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4445 if (non_empty < 0)
4446 return isl_basic_map_free(bmap);
4447 if (!non_empty)
4448 return bmap;
4450 dim = isl_basic_map_dim(bmap, isl_dim_all);
4451 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4452 if (dim < 0 || n_div < 0)
4453 return isl_basic_map_free(bmap);
4454 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4455 if (!groups)
4456 return isl_basic_map_free(bmap);
4457 first += isl_basic_map_offset(bmap, type) - 1;
4458 for (i = 0; i < first; ++i)
4459 groups[i] = -1;
4460 for (i = first + n; i < dim - n_div; ++i)
4461 groups[i] = -1;
4463 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4465 return bmap;
4468 /* Turn the n dimensions of type type, starting at first
4469 * into existentially quantified variables.
4471 * If a subset of the projected out variables are unrelated
4472 * to any of the variables that remain, then the constraints
4473 * involving this subset are simply dropped first.
4475 __isl_give isl_basic_map *isl_basic_map_project_out(
4476 __isl_take isl_basic_map *bmap,
4477 enum isl_dim_type type, unsigned first, unsigned n)
4479 isl_bool empty;
4480 isl_space *space;
4482 if (n == 0)
4483 return basic_map_space_reset(bmap, type);
4484 if (type == isl_dim_div)
4485 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4486 "cannot project out existentially quantified variables",
4487 return isl_basic_map_free(bmap));
4489 empty = isl_basic_map_plain_is_empty(bmap);
4490 if (empty < 0)
4491 return isl_basic_map_free(bmap);
4492 if (empty)
4493 bmap = isl_basic_map_set_to_empty(bmap);
4495 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4496 if (!bmap)
4497 return NULL;
4499 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4500 return isl_basic_map_remove_dims(bmap, type, first, n);
4502 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4503 return isl_basic_map_free(bmap);
4505 bmap = move_last(bmap, type, first, n);
4506 bmap = isl_basic_map_cow(bmap);
4507 bmap = insert_div_rows(bmap, n);
4509 space = isl_basic_map_take_space(bmap);
4510 space = isl_space_drop_dims(space, type, first, n);
4511 bmap = isl_basic_map_restore_space(bmap, space);
4512 bmap = isl_basic_map_simplify(bmap);
4513 bmap = isl_basic_map_drop_redundant_divs(bmap);
4514 return isl_basic_map_finalize(bmap);
4517 /* Turn the n dimensions of type type, starting at first
4518 * into existentially quantified variables.
4520 __isl_give isl_basic_set *isl_basic_set_project_out(
4521 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4522 unsigned first, unsigned n)
4524 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4525 type, first, n));
4528 /* Turn the n dimensions of type type, starting at first
4529 * into existentially quantified variables.
4531 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4532 enum isl_dim_type type, unsigned first, unsigned n)
4534 int i;
4535 isl_space *space;
4537 if (n == 0)
4538 return map_space_reset(map, type);
4540 if (isl_map_check_range(map, type, first, n) < 0)
4541 return isl_map_free(map);
4543 map = isl_map_cow(map);
4544 if (!map)
4545 return NULL;
4547 for (i = 0; i < map->n; ++i) {
4548 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4549 if (!map->p[i])
4550 goto error;
4553 if (map->n > 1)
4554 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4555 map = isl_map_unmark_normalized(map);
4557 space = isl_map_take_space(map);
4558 space = isl_space_drop_dims(space, type, first, n);
4559 map = isl_map_restore_space(map, space);
4561 return map;
4562 error:
4563 isl_map_free(map);
4564 return NULL;
4567 #undef TYPE
4568 #define TYPE isl_map
4569 #include "isl_project_out_all_params_templ.c"
4571 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4572 * into existentially quantified variables.
4574 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4575 enum isl_dim_type type, unsigned first, unsigned n)
4577 isl_size dim;
4579 dim = isl_map_dim(map, type);
4580 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4581 return isl_map_free(map);
4582 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4583 map = isl_map_project_out(map, type, 0, first);
4584 return map;
4587 /* Turn the n dimensions of type type, starting at first
4588 * into existentially quantified variables.
4590 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4591 enum isl_dim_type type, unsigned first, unsigned n)
4593 return set_from_map(isl_map_project_out(set_to_map(set),
4594 type, first, n));
4597 /* If "set" involves a parameter with identifier "id",
4598 * then turn it into an existentially quantified variable.
4600 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4601 __isl_take isl_id *id)
4603 int pos;
4605 if (!set || !id)
4606 goto error;
4607 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4608 isl_id_free(id);
4609 if (pos < 0)
4610 return set;
4611 return isl_set_project_out(set, isl_dim_param, pos, 1);
4612 error:
4613 isl_set_free(set);
4614 isl_id_free(id);
4615 return NULL;
4618 /* If "set" involves any of the parameters with identifiers in "list",
4619 * then turn them into existentially quantified variables.
4621 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4622 __isl_take isl_id_list *list)
4624 int i;
4625 isl_size n;
4627 n = isl_id_list_size(list);
4628 if (n < 0)
4629 goto error;
4630 for (i = 0; i < n; ++i) {
4631 isl_id *id;
4633 id = isl_id_list_get_at(list, i);
4634 set = isl_set_project_out_param_id(set, id);
4637 isl_id_list_free(list);
4638 return set;
4639 error:
4640 isl_id_list_free(list);
4641 isl_set_free(set);
4642 return NULL;
4645 /* Project out all parameters from "set" by existentially quantifying
4646 * over them.
4648 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4650 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4653 /* Return a map that projects the elements in "set" onto their
4654 * "n" set dimensions starting at "first".
4655 * "type" should be equal to isl_dim_set.
4657 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4658 enum isl_dim_type type, unsigned first, unsigned n)
4660 int i;
4661 isl_map *map;
4663 if (type != isl_dim_set)
4664 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4665 "only set dimensions can be projected out", goto error);
4666 if (isl_set_check_range(set, type, first, n) < 0)
4667 return isl_set_free(set);
4669 map = isl_map_from_domain(set);
4670 map = isl_map_add_dims(map, isl_dim_out, n);
4671 for (i = 0; i < n; ++i)
4672 map = isl_map_equate(map, isl_dim_in, first + i,
4673 isl_dim_out, i);
4674 return map;
4675 error:
4676 isl_set_free(set);
4677 return NULL;
4680 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4681 unsigned n)
4683 int i, j;
4684 isl_size total;
4686 total = isl_basic_map_dim(bmap, isl_dim_all);
4687 if (total < 0)
4688 return isl_basic_map_free(bmap);
4689 for (i = 0; i < n; ++i) {
4690 j = isl_basic_map_alloc_div(bmap);
4691 if (j < 0)
4692 goto error;
4693 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4695 return bmap;
4696 error:
4697 isl_basic_map_free(bmap);
4698 return NULL;
4701 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4703 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4704 __isl_keep isl_basic_map *bmap2)
4706 isl_space *space1, *space2;
4708 space1 = isl_basic_map_peek_space(bmap1);
4709 space2 = isl_basic_map_peek_space(bmap2);
4710 return isl_space_tuple_is_equal(space1, isl_dim_out,
4711 space2, isl_dim_in);
4714 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4716 static isl_stat isl_basic_map_check_applies_range(
4717 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4719 isl_bool equal;
4721 equal = isl_basic_map_applies_range(bmap1, bmap2);
4722 if (equal < 0)
4723 return isl_stat_error;
4724 if (!equal)
4725 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4726 "spaces don't match", return isl_stat_error);
4727 return isl_stat_ok;
4730 __isl_give isl_basic_map *isl_basic_map_apply_range(
4731 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4733 isl_space *space_result = NULL;
4734 struct isl_basic_map *bmap;
4735 isl_size n_in, n_out, n, nparam;
4736 unsigned total, pos;
4737 struct isl_dim_map *dim_map1, *dim_map2;
4739 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4740 goto error;
4741 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4742 goto error;
4744 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4745 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4746 n = isl_basic_map_dim(bmap1, isl_dim_out);
4747 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4748 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4749 goto error;
4751 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4752 isl_basic_map_get_space(bmap2));
4754 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4755 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4756 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4757 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4758 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4759 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4760 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4761 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4762 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4763 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4764 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4766 bmap = isl_basic_map_alloc_space(space_result,
4767 bmap1->n_div + bmap2->n_div + n,
4768 bmap1->n_eq + bmap2->n_eq,
4769 bmap1->n_ineq + bmap2->n_ineq);
4770 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4771 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4772 bmap = add_divs(bmap, n);
4773 bmap = isl_basic_map_simplify(bmap);
4774 bmap = isl_basic_map_drop_redundant_divs(bmap);
4775 return isl_basic_map_finalize(bmap);
4776 error:
4777 isl_basic_map_free(bmap1);
4778 isl_basic_map_free(bmap2);
4779 return NULL;
4782 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4783 __isl_take isl_basic_map *bmap)
4785 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4786 goto error;
4788 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4789 bmap));
4790 error:
4791 isl_basic_set_free(bset);
4792 isl_basic_map_free(bmap);
4793 return NULL;
4796 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4797 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4799 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4800 goto error;
4801 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4802 bmap2->dim, isl_dim_in))
4803 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4804 "spaces don't match", goto error);
4806 bmap1 = isl_basic_map_reverse(bmap1);
4807 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4808 return isl_basic_map_reverse(bmap1);
4809 error:
4810 isl_basic_map_free(bmap1);
4811 isl_basic_map_free(bmap2);
4812 return NULL;
4815 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4816 * A \cap B -> f(A) + f(B)
4818 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4819 __isl_take isl_basic_map *bmap2)
4821 isl_size n_in, n_out, nparam;
4822 unsigned total, pos;
4823 struct isl_basic_map *bmap = NULL;
4824 struct isl_dim_map *dim_map1, *dim_map2;
4825 int i;
4827 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4828 goto error;
4830 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4831 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4832 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4833 if (nparam < 0 || n_in < 0 || n_out < 0)
4834 goto error;
4836 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4837 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4838 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4839 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4840 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4843 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4844 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4848 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4849 bmap1->n_div + bmap2->n_div + 2 * n_out,
4850 bmap1->n_eq + bmap2->n_eq + n_out,
4851 bmap1->n_ineq + bmap2->n_ineq);
4852 for (i = 0; i < n_out; ++i) {
4853 int j = isl_basic_map_alloc_equality(bmap);
4854 if (j < 0)
4855 goto error;
4856 isl_seq_clr(bmap->eq[j], 1+total);
4857 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4858 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4859 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4861 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4862 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4863 bmap = add_divs(bmap, 2 * n_out);
4865 bmap = isl_basic_map_simplify(bmap);
4866 return isl_basic_map_finalize(bmap);
4867 error:
4868 isl_basic_map_free(bmap);
4869 isl_basic_map_free(bmap1);
4870 isl_basic_map_free(bmap2);
4871 return NULL;
4874 /* Given two maps A -> f(A) and B -> g(B), construct a map
4875 * A \cap B -> f(A) + f(B)
4877 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4878 __isl_take isl_map *map2)
4880 struct isl_map *result;
4881 int i, j;
4883 if (isl_map_check_equal_space(map1, map2) < 0)
4884 goto error;
4886 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4887 map1->n * map2->n, 0);
4888 if (!result)
4889 goto error;
4890 for (i = 0; i < map1->n; ++i)
4891 for (j = 0; j < map2->n; ++j) {
4892 struct isl_basic_map *part;
4893 part = isl_basic_map_sum(
4894 isl_basic_map_copy(map1->p[i]),
4895 isl_basic_map_copy(map2->p[j]));
4896 if (isl_basic_map_is_empty(part))
4897 isl_basic_map_free(part);
4898 else
4899 result = isl_map_add_basic_map(result, part);
4900 if (!result)
4901 goto error;
4903 isl_map_free(map1);
4904 isl_map_free(map2);
4905 return result;
4906 error:
4907 isl_map_free(map1);
4908 isl_map_free(map2);
4909 return NULL;
4912 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4913 __isl_take isl_set *set2)
4915 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4918 /* Given a basic map A -> f(A), construct A -> -f(A).
4920 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4922 int i, j;
4923 unsigned off;
4924 isl_size n;
4926 bmap = isl_basic_map_cow(bmap);
4927 n = isl_basic_map_dim(bmap, isl_dim_out);
4928 if (n < 0)
4929 return isl_basic_map_free(bmap);
4931 off = isl_basic_map_offset(bmap, isl_dim_out);
4932 for (i = 0; i < bmap->n_eq; ++i)
4933 for (j = 0; j < n; ++j)
4934 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4935 for (i = 0; i < bmap->n_ineq; ++i)
4936 for (j = 0; j < n; ++j)
4937 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4938 for (i = 0; i < bmap->n_div; ++i)
4939 for (j = 0; j < n; ++j)
4940 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4941 bmap = isl_basic_map_gauss(bmap, NULL);
4942 return isl_basic_map_finalize(bmap);
4945 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4947 return isl_basic_map_neg(bset);
4950 /* Given a map A -> f(A), construct A -> -f(A).
4952 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4954 int i;
4956 map = isl_map_cow(map);
4957 if (!map)
4958 return NULL;
4960 for (i = 0; i < map->n; ++i) {
4961 map->p[i] = isl_basic_map_neg(map->p[i]);
4962 if (!map->p[i])
4963 goto error;
4966 return map;
4967 error:
4968 isl_map_free(map);
4969 return NULL;
4972 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4974 return set_from_map(isl_map_neg(set_to_map(set)));
4977 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4978 * A -> floor(f(A)/d).
4980 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4981 isl_int d)
4983 isl_size n_in, n_out, nparam;
4984 unsigned total, pos;
4985 struct isl_basic_map *result = NULL;
4986 struct isl_dim_map *dim_map;
4987 int i;
4989 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4990 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4991 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4992 if (nparam < 0 || n_in < 0 || n_out < 0)
4993 return isl_basic_map_free(bmap);
4995 total = nparam + n_in + n_out + bmap->n_div + n_out;
4996 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4997 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4998 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4999 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5000 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5002 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5003 bmap->n_div + n_out,
5004 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5005 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5006 result = add_divs(result, n_out);
5007 for (i = 0; i < n_out; ++i) {
5008 int j;
5009 j = isl_basic_map_alloc_inequality(result);
5010 if (j < 0)
5011 goto error;
5012 isl_seq_clr(result->ineq[j], 1+total);
5013 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5014 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5015 j = isl_basic_map_alloc_inequality(result);
5016 if (j < 0)
5017 goto error;
5018 isl_seq_clr(result->ineq[j], 1+total);
5019 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5020 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5021 isl_int_sub_ui(result->ineq[j][0], d, 1);
5024 result = isl_basic_map_simplify(result);
5025 return isl_basic_map_finalize(result);
5026 error:
5027 isl_basic_map_free(result);
5028 return NULL;
5031 /* Given a map A -> f(A) and an integer d, construct a map
5032 * A -> floor(f(A)/d).
5034 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5036 int i;
5038 map = isl_map_cow(map);
5039 if (!map)
5040 return NULL;
5042 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5043 for (i = 0; i < map->n; ++i) {
5044 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5045 if (!map->p[i])
5046 goto error;
5048 map = isl_map_unmark_normalized(map);
5050 return map;
5051 error:
5052 isl_map_free(map);
5053 return NULL;
5056 /* Given a map A -> f(A) and an integer d, construct a map
5057 * A -> floor(f(A)/d).
5059 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5060 __isl_take isl_val *d)
5062 if (!map || !d)
5063 goto error;
5064 if (!isl_val_is_int(d))
5065 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5066 "expecting integer denominator", goto error);
5067 map = isl_map_floordiv(map, d->n);
5068 isl_val_free(d);
5069 return map;
5070 error:
5071 isl_map_free(map);
5072 isl_val_free(d);
5073 return NULL;
5076 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5077 unsigned pos)
5079 int i;
5080 isl_size nparam;
5081 isl_size n_in;
5082 isl_size total;
5084 total = isl_basic_map_dim(bmap, isl_dim_all);
5085 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5086 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5087 if (total < 0 || nparam < 0 || n_in < 0)
5088 return isl_basic_map_free(bmap);
5089 i = isl_basic_map_alloc_equality(bmap);
5090 if (i < 0)
5091 goto error;
5092 isl_seq_clr(bmap->eq[i], 1 + total);
5093 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5094 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5095 return isl_basic_map_finalize(bmap);
5096 error:
5097 isl_basic_map_free(bmap);
5098 return NULL;
5101 /* Add a constraint to "bmap" expressing i_pos < o_pos
5103 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5104 unsigned pos)
5106 int i;
5107 isl_size nparam;
5108 isl_size n_in;
5109 isl_size total;
5111 total = isl_basic_map_dim(bmap, isl_dim_all);
5112 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5113 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5114 if (total < 0 || nparam < 0 || n_in < 0)
5115 return isl_basic_map_free(bmap);
5116 i = isl_basic_map_alloc_inequality(bmap);
5117 if (i < 0)
5118 goto error;
5119 isl_seq_clr(bmap->ineq[i], 1 + total);
5120 isl_int_set_si(bmap->ineq[i][0], -1);
5121 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5122 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5123 return isl_basic_map_finalize(bmap);
5124 error:
5125 isl_basic_map_free(bmap);
5126 return NULL;
5129 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5131 static __isl_give isl_basic_map *var_less_or_equal(
5132 __isl_take isl_basic_map *bmap, unsigned pos)
5134 int i;
5135 isl_size nparam;
5136 isl_size n_in;
5137 isl_size total;
5139 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5141 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5142 if (total < 0 || nparam < 0 || n_in < 0)
5143 return isl_basic_map_free(bmap);
5144 i = isl_basic_map_alloc_inequality(bmap);
5145 if (i < 0)
5146 goto error;
5147 isl_seq_clr(bmap->ineq[i], 1 + total);
5148 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5149 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5150 return isl_basic_map_finalize(bmap);
5151 error:
5152 isl_basic_map_free(bmap);
5153 return NULL;
5156 /* Add a constraint to "bmap" expressing i_pos > o_pos
5158 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5159 unsigned pos)
5161 int i;
5162 isl_size nparam;
5163 isl_size n_in;
5164 isl_size total;
5166 total = isl_basic_map_dim(bmap, isl_dim_all);
5167 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5168 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5169 if (total < 0 || nparam < 0 || n_in < 0)
5170 return isl_basic_map_free(bmap);
5171 i = isl_basic_map_alloc_inequality(bmap);
5172 if (i < 0)
5173 goto error;
5174 isl_seq_clr(bmap->ineq[i], 1 + total);
5175 isl_int_set_si(bmap->ineq[i][0], -1);
5176 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5177 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5178 return isl_basic_map_finalize(bmap);
5179 error:
5180 isl_basic_map_free(bmap);
5181 return NULL;
5184 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5186 static __isl_give isl_basic_map *var_more_or_equal(
5187 __isl_take isl_basic_map *bmap, unsigned pos)
5189 int i;
5190 isl_size nparam;
5191 isl_size n_in;
5192 isl_size total;
5194 total = isl_basic_map_dim(bmap, isl_dim_all);
5195 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5196 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5197 if (total < 0 || nparam < 0 || n_in < 0)
5198 return isl_basic_map_free(bmap);
5199 i = isl_basic_map_alloc_inequality(bmap);
5200 if (i < 0)
5201 goto error;
5202 isl_seq_clr(bmap->ineq[i], 1 + total);
5203 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5204 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5205 return isl_basic_map_finalize(bmap);
5206 error:
5207 isl_basic_map_free(bmap);
5208 return NULL;
5211 __isl_give isl_basic_map *isl_basic_map_equal(
5212 __isl_take isl_space *space, unsigned n_equal)
5214 int i;
5215 struct isl_basic_map *bmap;
5216 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5217 if (!bmap)
5218 return NULL;
5219 for (i = 0; i < n_equal && bmap; ++i)
5220 bmap = var_equal(bmap, i);
5221 return isl_basic_map_finalize(bmap);
5224 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5226 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5227 unsigned pos)
5229 int i;
5230 struct isl_basic_map *bmap;
5231 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5232 if (!bmap)
5233 return NULL;
5234 for (i = 0; i < pos && bmap; ++i)
5235 bmap = var_equal(bmap, i);
5236 if (bmap)
5237 bmap = var_less(bmap, pos);
5238 return isl_basic_map_finalize(bmap);
5241 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5243 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5244 __isl_take isl_space *space, unsigned pos)
5246 int i;
5247 isl_basic_map *bmap;
5249 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5250 for (i = 0; i < pos; ++i)
5251 bmap = var_equal(bmap, i);
5252 bmap = var_less_or_equal(bmap, pos);
5253 return isl_basic_map_finalize(bmap);
5256 /* Return a relation on "space" expressing i_pos > o_pos
5258 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5259 unsigned pos)
5261 int i;
5262 struct isl_basic_map *bmap;
5263 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5264 if (!bmap)
5265 return NULL;
5266 for (i = 0; i < pos && bmap; ++i)
5267 bmap = var_equal(bmap, i);
5268 if (bmap)
5269 bmap = var_more(bmap, pos);
5270 return isl_basic_map_finalize(bmap);
5273 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5275 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5276 __isl_take isl_space *space, unsigned pos)
5278 int i;
5279 isl_basic_map *bmap;
5281 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5282 for (i = 0; i < pos; ++i)
5283 bmap = var_equal(bmap, i);
5284 bmap = var_more_or_equal(bmap, pos);
5285 return isl_basic_map_finalize(bmap);
5288 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5289 unsigned n, int equal)
5291 struct isl_map *map;
5292 int i;
5294 if (n == 0 && equal)
5295 return isl_map_universe(space);
5297 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5299 for (i = 0; i + 1 < n; ++i)
5300 map = isl_map_add_basic_map(map,
5301 isl_basic_map_less_at(isl_space_copy(space), i));
5302 if (n > 0) {
5303 if (equal)
5304 map = isl_map_add_basic_map(map,
5305 isl_basic_map_less_or_equal_at(space, n - 1));
5306 else
5307 map = isl_map_add_basic_map(map,
5308 isl_basic_map_less_at(space, n - 1));
5309 } else
5310 isl_space_free(space);
5312 return map;
5315 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5317 if (!space)
5318 return NULL;
5319 return map_lex_lte_first(space, space->n_out, equal);
5322 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5323 unsigned n)
5325 return map_lex_lte_first(space, n, 0);
5328 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5329 unsigned n)
5331 return map_lex_lte_first(space, n, 1);
5334 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5336 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5339 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5341 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5344 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5345 unsigned n, int equal)
5347 struct isl_map *map;
5348 int i;
5350 if (n == 0 && equal)
5351 return isl_map_universe(space);
5353 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5355 for (i = 0; i + 1 < n; ++i)
5356 map = isl_map_add_basic_map(map,
5357 isl_basic_map_more_at(isl_space_copy(space), i));
5358 if (n > 0) {
5359 if (equal)
5360 map = isl_map_add_basic_map(map,
5361 isl_basic_map_more_or_equal_at(space, n - 1));
5362 else
5363 map = isl_map_add_basic_map(map,
5364 isl_basic_map_more_at(space, n - 1));
5365 } else
5366 isl_space_free(space);
5368 return map;
5371 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5373 if (!space)
5374 return NULL;
5375 return map_lex_gte_first(space, space->n_out, equal);
5378 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5379 unsigned n)
5381 return map_lex_gte_first(space, n, 0);
5384 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5385 unsigned n)
5387 return map_lex_gte_first(space, n, 1);
5390 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5392 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5395 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5397 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5400 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5401 __isl_take isl_set *set2)
5403 isl_map *map;
5404 map = isl_map_lex_le(isl_set_get_space(set1));
5405 map = isl_map_intersect_domain(map, set1);
5406 map = isl_map_intersect_range(map, set2);
5407 return map;
5410 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5411 __isl_take isl_set *set2)
5413 isl_map *map;
5414 map = isl_map_lex_lt(isl_set_get_space(set1));
5415 map = isl_map_intersect_domain(map, set1);
5416 map = isl_map_intersect_range(map, set2);
5417 return map;
5420 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5421 __isl_take isl_set *set2)
5423 isl_map *map;
5424 map = isl_map_lex_ge(isl_set_get_space(set1));
5425 map = isl_map_intersect_domain(map, set1);
5426 map = isl_map_intersect_range(map, set2);
5427 return map;
5430 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5431 __isl_take isl_set *set2)
5433 isl_map *map;
5434 map = isl_map_lex_gt(isl_set_get_space(set1));
5435 map = isl_map_intersect_domain(map, set1);
5436 map = isl_map_intersect_range(map, set2);
5437 return map;
5440 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5441 __isl_take isl_map *map2)
5443 isl_map *map;
5444 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5445 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5446 map = isl_map_apply_range(map, isl_map_reverse(map2));
5447 return map;
5450 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5451 __isl_take isl_map *map2)
5453 isl_map *map;
5454 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5455 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5456 map = isl_map_apply_range(map, isl_map_reverse(map2));
5457 return map;
5460 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5461 __isl_take isl_map *map2)
5463 isl_map *map;
5464 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5465 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5466 map = isl_map_apply_range(map, isl_map_reverse(map2));
5467 return map;
5470 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5471 __isl_take isl_map *map2)
5473 isl_map *map;
5474 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5475 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5476 map = isl_map_apply_range(map, isl_map_reverse(map2));
5477 return map;
5480 /* For the div d = floor(f/m) at position "div", add the constraint
5482 * f - m d >= 0
5484 static __isl_give isl_basic_map *add_upper_div_constraint(
5485 __isl_take isl_basic_map *bmap, unsigned div)
5487 int i;
5488 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5489 isl_size n_div;
5490 unsigned pos;
5492 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5493 if (v_div < 0 || n_div < 0)
5494 return isl_basic_map_free(bmap);
5495 pos = v_div + div;
5496 i = isl_basic_map_alloc_inequality(bmap);
5497 if (i < 0)
5498 return isl_basic_map_free(bmap);
5499 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5500 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5502 return bmap;
5505 /* For the div d = floor(f/m) at position "div", add the constraint
5507 * -(f-(m-1)) + m d >= 0
5509 static __isl_give isl_basic_map *add_lower_div_constraint(
5510 __isl_take isl_basic_map *bmap, unsigned div)
5512 int i;
5513 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5514 isl_size n_div;
5515 unsigned pos;
5517 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5518 if (v_div < 0 || n_div < 0)
5519 return isl_basic_map_free(bmap);
5520 pos = v_div + div;
5521 i = isl_basic_map_alloc_inequality(bmap);
5522 if (i < 0)
5523 return isl_basic_map_free(bmap);
5524 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5525 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5526 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5527 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5529 return bmap;
5532 /* For the div d = floor(f/m) at position "pos", add the constraints
5534 * f - m d >= 0
5535 * -(f-(m-1)) + m d >= 0
5537 * Note that the second constraint is the negation of
5539 * f - m d >= m
5541 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5542 __isl_take isl_basic_map *bmap, unsigned pos)
5544 bmap = add_upper_div_constraint(bmap, pos);
5545 bmap = add_lower_div_constraint(bmap, pos);
5546 return bmap;
5549 /* For each known div d = floor(f/m), add the constraints
5551 * f - m d >= 0
5552 * -(f-(m-1)) + m d >= 0
5554 * Remove duplicate constraints in case of some these div constraints
5555 * already appear in "bmap".
5557 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5558 __isl_take isl_basic_map *bmap)
5560 isl_size n_div;
5562 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5563 if (n_div < 0)
5564 return isl_basic_map_free(bmap);
5565 if (n_div == 0)
5566 return bmap;
5568 bmap = add_known_div_constraints(bmap);
5569 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5570 bmap = isl_basic_map_finalize(bmap);
5571 return bmap;
5574 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5576 * In particular, if this div is of the form d = floor(f/m),
5577 * then add the constraint
5579 * f - m d >= 0
5581 * if sign < 0 or the constraint
5583 * -(f-(m-1)) + m d >= 0
5585 * if sign > 0.
5587 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5588 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5590 if (sign < 0)
5591 return add_upper_div_constraint(bmap, div);
5592 else
5593 return add_lower_div_constraint(bmap, div);
5596 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5597 __isl_take isl_basic_map *bmap)
5599 isl_space *space;
5601 if (!bmap)
5602 goto error;
5603 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5604 bmap->n_div == 0 &&
5605 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5606 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5607 return bset_from_bmap(bmap);
5608 bmap = isl_basic_map_cow(bmap);
5609 if (!bmap)
5610 return NULL;
5611 space = isl_basic_map_take_space(bmap);
5612 space = isl_space_underlying(space, bmap->n_div);
5613 bmap = isl_basic_map_restore_space(bmap, space);
5614 if (!bmap)
5615 return NULL;
5616 bmap->extra -= bmap->n_div;
5617 bmap->n_div = 0;
5618 bmap = isl_basic_map_finalize(bmap);
5619 return bset_from_bmap(bmap);
5620 error:
5621 isl_basic_map_free(bmap);
5622 return NULL;
5625 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5626 __isl_take isl_basic_set *bset)
5628 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5631 /* Replace each element in "list" by the result of applying
5632 * isl_basic_map_underlying_set to the element.
5634 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5635 __isl_take isl_basic_map_list *list)
5637 int i;
5638 isl_size n;
5640 n = isl_basic_map_list_n_basic_map(list);
5641 if (n < 0)
5642 goto error;
5644 for (i = 0; i < n; ++i) {
5645 isl_basic_map *bmap;
5646 isl_basic_set *bset;
5648 bmap = isl_basic_map_list_get_basic_map(list, i);
5649 bset = isl_basic_set_underlying_set(bmap);
5650 list = isl_basic_set_list_set_basic_set(list, i, bset);
5653 return list;
5654 error:
5655 isl_basic_map_list_free(list);
5656 return NULL;
5659 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5660 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5662 struct isl_basic_map *bmap;
5663 struct isl_ctx *ctx;
5664 isl_size dim, bmap_total;
5665 unsigned total;
5666 int i;
5668 if (!bset || !like)
5669 goto error;
5670 ctx = bset->ctx;
5671 if (isl_basic_set_check_no_params(bset) < 0 ||
5672 isl_basic_set_check_no_locals(bset) < 0)
5673 goto error;
5674 dim = isl_basic_set_dim(bset, isl_dim_set);
5675 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5676 if (dim < 0 || bmap_total < 0)
5677 goto error;
5678 isl_assert(ctx, dim == bmap_total, goto error);
5679 if (like->n_div == 0) {
5680 isl_space *space = isl_basic_map_get_space(like);
5681 isl_basic_map_free(like);
5682 return isl_basic_map_reset_space(bset, space);
5684 bset = isl_basic_set_cow(bset);
5685 if (!bset)
5686 goto error;
5687 total = dim + bset->extra;
5688 bmap = bset_to_bmap(bset);
5689 isl_space_free(isl_basic_map_take_space(bmap));
5690 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5691 if (!bmap)
5692 goto error;
5693 bmap->n_div = like->n_div;
5694 bmap->extra += like->n_div;
5695 if (bmap->extra) {
5696 unsigned ltotal;
5697 isl_int **div;
5698 ltotal = total - bmap->extra + like->extra;
5699 if (ltotal > total)
5700 ltotal = total;
5701 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5702 bmap->extra * (1 + 1 + total));
5703 if (isl_blk_is_error(bmap->block2))
5704 goto error;
5705 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5706 if (!div)
5707 goto error;
5708 bmap->div = div;
5709 for (i = 0; i < bmap->extra; ++i)
5710 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5711 for (i = 0; i < like->n_div; ++i) {
5712 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5713 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5715 bmap = isl_basic_map_add_known_div_constraints(bmap);
5717 isl_basic_map_free(like);
5718 bmap = isl_basic_map_simplify(bmap);
5719 bmap = isl_basic_map_finalize(bmap);
5720 return bmap;
5721 error:
5722 isl_basic_map_free(like);
5723 isl_basic_set_free(bset);
5724 return NULL;
5727 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5728 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5730 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5731 bset_to_bmap(like)));
5734 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5736 int i;
5738 map = isl_map_cow(map);
5739 if (!map)
5740 return NULL;
5741 map->dim = isl_space_cow(map->dim);
5742 if (!map->dim)
5743 goto error;
5745 for (i = 1; i < map->n; ++i)
5746 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5747 goto error);
5748 for (i = 0; i < map->n; ++i) {
5749 map->p[i] = bset_to_bmap(
5750 isl_basic_map_underlying_set(map->p[i]));
5751 if (!map->p[i])
5752 goto error;
5754 if (map->n == 0)
5755 map->dim = isl_space_underlying(map->dim, 0);
5756 else {
5757 isl_space_free(map->dim);
5758 map->dim = isl_space_copy(map->p[0]->dim);
5760 if (!map->dim)
5761 goto error;
5762 return set_from_map(map);
5763 error:
5764 isl_map_free(map);
5765 return NULL;
5768 /* Replace the space of "bmap" by "space".
5770 * If the space of "bmap" is identical to "space" (including the identifiers
5771 * of the input and output dimensions), then simply return the original input.
5773 __isl_give isl_basic_map *isl_basic_map_reset_space(
5774 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5776 isl_bool equal;
5777 isl_space *bmap_space;
5779 bmap_space = isl_basic_map_peek_space(bmap);
5780 equal = isl_space_is_equal(bmap_space, space);
5781 if (equal >= 0 && equal)
5782 equal = isl_space_has_equal_ids(bmap_space, space);
5783 if (equal < 0)
5784 goto error;
5785 if (equal) {
5786 isl_space_free(space);
5787 return bmap;
5789 isl_space_free(isl_basic_map_take_space(bmap));
5790 bmap = isl_basic_map_restore_space(bmap, space);
5792 bmap = isl_basic_map_finalize(bmap);
5794 return bmap;
5795 error:
5796 isl_basic_map_free(bmap);
5797 isl_space_free(space);
5798 return NULL;
5801 __isl_give isl_basic_set *isl_basic_set_reset_space(
5802 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5804 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5805 space));
5808 /* Check that the total dimensions of "map" and "space" are the same.
5810 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5811 __isl_keep isl_space *space)
5813 isl_size dim1, dim2;
5815 dim1 = isl_map_dim(map, isl_dim_all);
5816 dim2 = isl_space_dim(space, isl_dim_all);
5817 if (dim1 < 0 || dim2 < 0)
5818 return isl_stat_error;
5819 if (dim1 == dim2)
5820 return isl_stat_ok;
5821 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5822 "total dimensions do not match", return isl_stat_error);
5825 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5826 __isl_take isl_space *space)
5828 int i;
5830 map = isl_map_cow(map);
5831 if (!map || !space)
5832 goto error;
5834 for (i = 0; i < map->n; ++i) {
5835 map->p[i] = isl_basic_map_reset_space(map->p[i],
5836 isl_space_copy(space));
5837 if (!map->p[i])
5838 goto error;
5840 isl_space_free(isl_map_take_space(map));
5841 map = isl_map_restore_space(map, space);
5843 return map;
5844 error:
5845 isl_map_free(map);
5846 isl_space_free(space);
5847 return NULL;
5850 /* Replace the space of "map" by "space", without modifying
5851 * the dimension of "map".
5853 * If the space of "map" is identical to "space" (including the identifiers
5854 * of the input and output dimensions), then simply return the original input.
5856 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5857 __isl_take isl_space *space)
5859 isl_bool equal;
5860 isl_space *map_space;
5862 map_space = isl_map_peek_space(map);
5863 equal = isl_space_is_equal(map_space, space);
5864 if (equal >= 0 && equal)
5865 equal = isl_space_has_equal_ids(map_space, space);
5866 if (equal < 0)
5867 goto error;
5868 if (equal) {
5869 isl_space_free(space);
5870 return map;
5872 if (check_map_space_equal_total_dim(map, space) < 0)
5873 goto error;
5874 return isl_map_reset_space(map, space);
5875 error:
5876 isl_map_free(map);
5877 isl_space_free(space);
5878 return NULL;
5881 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5882 __isl_take isl_space *space)
5884 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5887 /* Compute the parameter domain of the given basic set.
5889 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5891 isl_bool is_params;
5892 isl_space *space;
5893 isl_size n;
5895 is_params = isl_basic_set_is_params(bset);
5896 if (is_params < 0)
5897 return isl_basic_set_free(bset);
5898 if (is_params)
5899 return bset;
5901 n = isl_basic_set_dim(bset, isl_dim_set);
5902 if (n < 0)
5903 return isl_basic_set_free(bset);
5904 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5905 space = isl_basic_set_get_space(bset);
5906 space = isl_space_params(space);
5907 bset = isl_basic_set_reset_space(bset, space);
5908 return bset;
5911 /* Construct a zero-dimensional basic set with the given parameter domain.
5913 __isl_give isl_basic_set *isl_basic_set_from_params(
5914 __isl_take isl_basic_set *bset)
5916 isl_space *space;
5917 space = isl_basic_set_get_space(bset);
5918 space = isl_space_set_from_params(space);
5919 bset = isl_basic_set_reset_space(bset, space);
5920 return bset;
5923 /* Compute the parameter domain of the given set.
5925 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5927 return isl_map_params(set_to_map(set));
5930 /* Construct a zero-dimensional set with the given parameter domain.
5932 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5934 isl_space *space;
5935 space = isl_set_get_space(set);
5936 space = isl_space_set_from_params(space);
5937 set = isl_set_reset_space(set, space);
5938 return set;
5941 /* Compute the parameter domain of the given map.
5943 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5945 isl_space *space;
5946 isl_size n_in, n_out;
5948 n_in = isl_map_dim(map, isl_dim_in);
5949 n_out = isl_map_dim(map, isl_dim_out);
5950 if (n_in < 0 || n_out < 0)
5951 return isl_map_free(map);
5952 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5953 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5954 space = isl_map_get_space(map);
5955 space = isl_space_params(space);
5956 map = isl_map_reset_space(map, space);
5957 return map;
5960 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5962 isl_space *space;
5963 isl_size n_out;
5965 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5966 if (n_out < 0)
5967 return isl_basic_map_free(bmap);
5968 space = isl_space_domain(isl_basic_map_get_space(bmap));
5970 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5972 return isl_basic_map_reset_space(bmap, space);
5975 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5977 if (!bmap)
5978 return isl_bool_error;
5979 return isl_space_may_be_set(bmap->dim);
5982 /* Is this basic map actually a set?
5983 * Users should never call this function. Outside of isl,
5984 * the type should indicate whether something is a set or a map.
5986 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5988 if (!bmap)
5989 return isl_bool_error;
5990 return isl_space_is_set(bmap->dim);
5993 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5995 isl_bool is_set;
5997 is_set = isl_basic_map_is_set(bmap);
5998 if (is_set < 0)
5999 goto error;
6000 if (is_set)
6001 return bmap;
6002 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6003 error:
6004 isl_basic_map_free(bmap);
6005 return NULL;
6008 __isl_give isl_basic_map *isl_basic_map_domain_map(
6009 __isl_take isl_basic_map *bmap)
6011 int i;
6012 isl_space *space;
6013 isl_basic_map *domain;
6014 isl_size nparam, n_in, n_out;
6016 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6017 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6018 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6019 if (nparam < 0 || n_in < 0 || n_out < 0)
6020 return isl_basic_map_free(bmap);
6022 space = isl_basic_map_get_space(bmap);
6023 space = isl_space_from_range(isl_space_domain(space));
6024 domain = isl_basic_map_universe(space);
6026 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6027 bmap = isl_basic_map_apply_range(bmap, domain);
6028 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6030 for (i = 0; i < n_in; ++i)
6031 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6032 isl_dim_out, i);
6034 bmap = isl_basic_map_gauss(bmap, NULL);
6035 return isl_basic_map_finalize(bmap);
6038 __isl_give isl_basic_map *isl_basic_map_range_map(
6039 __isl_take isl_basic_map *bmap)
6041 int i;
6042 isl_space *space;
6043 isl_basic_map *range;
6044 isl_size nparam, n_in, n_out;
6046 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6047 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6048 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6049 if (nparam < 0 || n_in < 0 || n_out < 0)
6050 return isl_basic_map_free(bmap);
6052 space = isl_basic_map_get_space(bmap);
6053 space = isl_space_from_range(isl_space_range(space));
6054 range = isl_basic_map_universe(space);
6056 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6057 bmap = isl_basic_map_apply_range(bmap, range);
6058 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6060 for (i = 0; i < n_out; ++i)
6061 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6062 isl_dim_out, i);
6064 bmap = isl_basic_map_gauss(bmap, NULL);
6065 return isl_basic_map_finalize(bmap);
6068 int isl_map_may_be_set(__isl_keep isl_map *map)
6070 if (!map)
6071 return -1;
6072 return isl_space_may_be_set(map->dim);
6075 /* Is this map actually a set?
6076 * Users should never call this function. Outside of isl,
6077 * the type should indicate whether something is a set or a map.
6079 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6081 if (!map)
6082 return isl_bool_error;
6083 return isl_space_is_set(map->dim);
6086 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6088 isl_space *space;
6089 isl_size n_in;
6091 n_in = isl_map_dim(map, isl_dim_in);
6092 if (n_in < 0)
6093 return set_from_map(isl_map_free(map));
6094 space = isl_space_range(isl_map_get_space(map));
6096 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6098 return set_from_map(isl_map_reset_space(map, space));
6101 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6102 * to each of its basic maps.
6104 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6105 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6106 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6108 int i;
6109 isl_space *space;
6111 map = isl_map_cow(map);
6112 if (!map)
6113 return NULL;
6115 for (i = 0; i < map->n; ++i) {
6116 map->p[i] = fn_bmap(map->p[i]);
6117 if (!map->p[i])
6118 return isl_map_free(map);
6120 map = isl_map_unmark_normalized(map);
6122 space = isl_map_take_space(map);
6123 space = fn_space(space);
6124 map = isl_map_restore_space(map, space);
6126 return map;
6129 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6131 return isl_map_transform(map, &isl_space_domain_map,
6132 &isl_basic_map_domain_map);
6135 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6137 return isl_map_transform(map, &isl_space_range_map,
6138 &isl_basic_map_range_map);
6141 /* Given a wrapped map of the form A[B -> C],
6142 * return the map A[B -> C] -> B.
6144 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6146 isl_id *id;
6147 isl_map *map;
6149 if (!set)
6150 return NULL;
6151 if (!isl_set_has_tuple_id(set))
6152 return isl_map_domain_map(isl_set_unwrap(set));
6154 id = isl_set_get_tuple_id(set);
6155 map = isl_map_domain_map(isl_set_unwrap(set));
6156 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6158 return map;
6161 __isl_give isl_basic_map *isl_basic_map_from_domain(
6162 __isl_take isl_basic_set *bset)
6164 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6167 __isl_give isl_basic_map *isl_basic_map_from_range(
6168 __isl_take isl_basic_set *bset)
6170 isl_space *space;
6171 space = isl_basic_set_get_space(bset);
6172 space = isl_space_from_range(space);
6173 bset = isl_basic_set_reset_space(bset, space);
6174 return bset_to_bmap(bset);
6177 /* Create a relation with the given set as range.
6178 * The domain of the created relation is a zero-dimensional
6179 * flat anonymous space.
6181 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6183 isl_space *space;
6184 space = isl_set_get_space(set);
6185 space = isl_space_from_range(space);
6186 set = isl_set_reset_space(set, space);
6187 return set_to_map(set);
6190 /* Create a relation with the given set as domain.
6191 * The range of the created relation is a zero-dimensional
6192 * flat anonymous space.
6194 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6196 return isl_map_reverse(isl_map_from_range(set));
6199 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6200 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6202 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6205 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6206 __isl_take isl_set *range)
6208 return isl_map_apply_range(isl_map_reverse(domain), range);
6211 /* Return a newly allocated isl_map with given space and flags and
6212 * room for "n" basic maps.
6213 * Make sure that all cached information is cleared.
6215 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6216 unsigned flags)
6218 struct isl_map *map;
6220 if (!space)
6221 return NULL;
6222 if (n < 0)
6223 isl_die(space->ctx, isl_error_internal,
6224 "negative number of basic maps", goto error);
6225 map = isl_calloc(space->ctx, struct isl_map,
6226 sizeof(struct isl_map) +
6227 (n - 1) * sizeof(struct isl_basic_map *));
6228 if (!map)
6229 goto error;
6231 map->ctx = space->ctx;
6232 isl_ctx_ref(map->ctx);
6233 map->ref = 1;
6234 map->size = n;
6235 map->n = 0;
6236 map->dim = space;
6237 map->flags = flags;
6238 return map;
6239 error:
6240 isl_space_free(space);
6241 return NULL;
6244 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6246 struct isl_basic_map *bmap;
6247 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6248 bmap = isl_basic_map_set_to_empty(bmap);
6249 return bmap;
6252 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6254 struct isl_basic_set *bset;
6255 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6256 bset = isl_basic_set_set_to_empty(bset);
6257 return bset;
6260 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6262 struct isl_basic_map *bmap;
6263 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6264 bmap = isl_basic_map_finalize(bmap);
6265 return bmap;
6268 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6270 struct isl_basic_set *bset;
6271 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6272 bset = isl_basic_set_finalize(bset);
6273 return bset;
6276 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6277 __isl_take isl_space *space)
6279 int i;
6280 isl_size total = isl_space_dim(space, isl_dim_all);
6281 isl_basic_map *bmap;
6283 if (total < 0)
6284 space = isl_space_free(space);
6285 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6286 for (i = 0; i < total; ++i) {
6287 int k = isl_basic_map_alloc_inequality(bmap);
6288 if (k < 0)
6289 goto error;
6290 isl_seq_clr(bmap->ineq[k], 1 + total);
6291 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6293 return bmap;
6294 error:
6295 isl_basic_map_free(bmap);
6296 return NULL;
6299 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6300 __isl_take isl_space *space)
6302 return isl_basic_map_nat_universe(space);
6305 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6307 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6310 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6312 return isl_map_nat_universe(space);
6315 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6317 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6320 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6322 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6325 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6327 struct isl_map *map;
6328 if (!space)
6329 return NULL;
6330 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6331 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6332 return map;
6335 /* This function performs the same operation as isl_map_universe,
6336 * but is considered as a function on an isl_space when exported.
6338 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6340 return isl_map_universe(space);
6343 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6345 struct isl_set *set;
6346 if (!space)
6347 return NULL;
6348 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6349 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6350 return set;
6353 /* This function performs the same operation as isl_set_universe,
6354 * but is considered as a function on an isl_space when exported.
6356 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6358 return isl_set_universe(space);
6361 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6363 int i;
6364 struct isl_map *dup;
6366 if (!map)
6367 return NULL;
6368 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6369 for (i = 0; i < map->n; ++i)
6370 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6371 return dup;
6374 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6375 __isl_take isl_basic_map *bmap)
6377 if (!bmap || !map)
6378 goto error;
6379 if (isl_basic_map_plain_is_empty(bmap)) {
6380 isl_basic_map_free(bmap);
6381 return map;
6383 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6384 goto error;
6385 isl_assert(map->ctx, map->n < map->size, goto error);
6386 map->p[map->n] = bmap;
6387 map->n++;
6388 map = isl_map_unmark_normalized(map);
6389 return map;
6390 error:
6391 if (map)
6392 isl_map_free(map);
6393 if (bmap)
6394 isl_basic_map_free(bmap);
6395 return NULL;
6398 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6400 int i;
6402 if (!map)
6403 return NULL;
6405 if (--map->ref > 0)
6406 return NULL;
6408 clear_caches(map);
6409 isl_ctx_deref(map->ctx);
6410 for (i = 0; i < map->n; ++i)
6411 isl_basic_map_free(map->p[i]);
6412 isl_space_free(map->dim);
6413 free(map);
6415 return NULL;
6418 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6419 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6421 int j;
6422 isl_size total;
6424 total = isl_basic_map_dim(bmap, isl_dim_all);
6425 if (total < 0)
6426 return isl_basic_map_free(bmap);
6428 bmap = isl_basic_map_cow(bmap);
6429 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6430 j = isl_basic_map_alloc_equality(bmap);
6431 if (j < 0)
6432 goto error;
6433 isl_seq_clr(bmap->eq[j] + 1, total);
6434 isl_int_set_si(bmap->eq[j][pos], -1);
6435 isl_int_set_si(bmap->eq[j][0], value);
6436 bmap = isl_basic_map_simplify(bmap);
6437 return isl_basic_map_finalize(bmap);
6438 error:
6439 isl_basic_map_free(bmap);
6440 return NULL;
6443 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6444 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6446 int j;
6447 isl_size total;
6449 total = isl_basic_map_dim(bmap, isl_dim_all);
6450 if (total < 0)
6451 return isl_basic_map_free(bmap);
6453 bmap = isl_basic_map_cow(bmap);
6454 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6455 j = isl_basic_map_alloc_equality(bmap);
6456 if (j < 0)
6457 goto error;
6458 isl_seq_clr(bmap->eq[j] + 1, total);
6459 isl_int_set_si(bmap->eq[j][pos], -1);
6460 isl_int_set(bmap->eq[j][0], value);
6461 bmap = isl_basic_map_simplify(bmap);
6462 return isl_basic_map_finalize(bmap);
6463 error:
6464 isl_basic_map_free(bmap);
6465 return NULL;
6468 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6469 enum isl_dim_type type, unsigned pos, int value)
6471 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6472 return isl_basic_map_free(bmap);
6473 return isl_basic_map_fix_pos_si(bmap,
6474 isl_basic_map_offset(bmap, type) + pos, value);
6477 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6478 enum isl_dim_type type, unsigned pos, isl_int value)
6480 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6481 return isl_basic_map_free(bmap);
6482 return isl_basic_map_fix_pos(bmap,
6483 isl_basic_map_offset(bmap, type) + pos, value);
6486 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6487 * to be equal to "v".
6489 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6490 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6492 if (!bmap || !v)
6493 goto error;
6494 if (!isl_val_is_int(v))
6495 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6496 "expecting integer value", goto error);
6497 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6498 goto error;
6499 pos += isl_basic_map_offset(bmap, type);
6500 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6501 isl_val_free(v);
6502 return bmap;
6503 error:
6504 isl_basic_map_free(bmap);
6505 isl_val_free(v);
6506 return NULL;
6509 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6510 * to be equal to "v".
6512 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6513 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6515 return isl_basic_map_fix_val(bset, type, pos, v);
6518 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6519 enum isl_dim_type type, unsigned pos, int value)
6521 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6522 type, pos, value));
6525 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6526 enum isl_dim_type type, unsigned pos, isl_int value)
6528 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6529 type, pos, value));
6532 /* Remove the basic map at position "i" from "map" if this basic map
6533 * is (obviously) empty.
6535 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6537 isl_bool empty;
6539 if (!map)
6540 return NULL;
6542 empty = isl_basic_map_plain_is_empty(map->p[i]);
6543 if (empty < 0)
6544 return isl_map_free(map);
6545 if (!empty)
6546 return map;
6548 isl_basic_map_free(map->p[i]);
6549 map->n--;
6550 if (i != map->n) {
6551 map->p[i] = map->p[map->n];
6552 map = isl_map_unmark_normalized(map);
6556 return map;
6559 /* Perform "fn" on each basic map of "map", where we may not be holding
6560 * the only reference to "map".
6561 * In particular, "fn" should be a semantics preserving operation
6562 * that we want to apply to all copies of "map". We therefore need
6563 * to be careful not to modify "map" in a way that breaks "map"
6564 * in case anything goes wrong.
6566 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6567 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6569 struct isl_basic_map *bmap;
6570 int i;
6572 if (!map)
6573 return NULL;
6575 for (i = map->n - 1; i >= 0; --i) {
6576 bmap = isl_basic_map_copy(map->p[i]);
6577 bmap = fn(bmap);
6578 if (!bmap)
6579 goto error;
6580 isl_basic_map_free(map->p[i]);
6581 map->p[i] = bmap;
6582 map = remove_if_empty(map, i);
6583 if (!map)
6584 return NULL;
6587 return map;
6588 error:
6589 isl_map_free(map);
6590 return NULL;
6593 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6594 enum isl_dim_type type, unsigned pos, int value)
6596 int i;
6598 map = isl_map_cow(map);
6599 if (isl_map_check_range(map, type, pos, 1) < 0)
6600 return isl_map_free(map);
6601 for (i = map->n - 1; i >= 0; --i) {
6602 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6603 map = remove_if_empty(map, i);
6604 if (!map)
6605 return NULL;
6607 map = isl_map_unmark_normalized(map);
6608 return map;
6611 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6612 enum isl_dim_type type, unsigned pos, int value)
6614 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6617 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6618 enum isl_dim_type type, unsigned pos, isl_int value)
6620 int i;
6622 map = isl_map_cow(map);
6623 if (isl_map_check_range(map, type, pos, 1) < 0)
6624 return isl_map_free(map);
6625 for (i = 0; i < map->n; ++i) {
6626 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6627 if (!map->p[i])
6628 goto error;
6630 map = isl_map_unmark_normalized(map);
6631 return map;
6632 error:
6633 isl_map_free(map);
6634 return NULL;
6637 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6638 enum isl_dim_type type, unsigned pos, isl_int value)
6640 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6643 /* Fix the value of the variable at position "pos" of type "type" of "map"
6644 * to be equal to "v".
6646 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6647 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6649 int i;
6651 map = isl_map_cow(map);
6652 if (!map || !v)
6653 goto error;
6655 if (!isl_val_is_int(v))
6656 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6657 "expecting integer value", goto error);
6658 if (isl_map_check_range(map, type, pos, 1) < 0)
6659 goto error;
6660 for (i = map->n - 1; i >= 0; --i) {
6661 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6662 isl_val_copy(v));
6663 map = remove_if_empty(map, i);
6664 if (!map)
6665 goto error;
6667 map = isl_map_unmark_normalized(map);
6668 isl_val_free(v);
6669 return map;
6670 error:
6671 isl_map_free(map);
6672 isl_val_free(v);
6673 return NULL;
6676 /* Fix the value of the variable at position "pos" of type "type" of "set"
6677 * to be equal to "v".
6679 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6680 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6682 return isl_map_fix_val(set, type, pos, v);
6685 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6686 unsigned input, int value)
6688 return isl_map_fix_si(map, isl_dim_in, input, value);
6691 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6692 int value)
6694 return set_from_map(isl_map_fix_si(set_to_map(set),
6695 isl_dim_set, dim, value));
6698 static __isl_give isl_basic_map *basic_map_bound_si(
6699 __isl_take isl_basic_map *bmap,
6700 enum isl_dim_type type, unsigned pos, int value, int upper)
6702 int j;
6703 isl_size total;
6705 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6706 return isl_basic_map_free(bmap);
6707 total = isl_basic_map_dim(bmap, isl_dim_all);
6708 if (total < 0)
6709 return isl_basic_map_free(bmap);
6710 pos += isl_basic_map_offset(bmap, type);
6711 bmap = isl_basic_map_cow(bmap);
6712 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6713 j = isl_basic_map_alloc_inequality(bmap);
6714 if (j < 0)
6715 goto error;
6716 isl_seq_clr(bmap->ineq[j], 1 + total);
6717 if (upper) {
6718 isl_int_set_si(bmap->ineq[j][pos], -1);
6719 isl_int_set_si(bmap->ineq[j][0], value);
6720 } else {
6721 isl_int_set_si(bmap->ineq[j][pos], 1);
6722 isl_int_set_si(bmap->ineq[j][0], -value);
6724 bmap = isl_basic_map_simplify(bmap);
6725 return isl_basic_map_finalize(bmap);
6726 error:
6727 isl_basic_map_free(bmap);
6728 return NULL;
6731 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6732 __isl_take isl_basic_map *bmap,
6733 enum isl_dim_type type, unsigned pos, int value)
6735 return basic_map_bound_si(bmap, type, pos, value, 0);
6738 /* Constrain the values of the given dimension to be no greater than "value".
6740 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6741 __isl_take isl_basic_map *bmap,
6742 enum isl_dim_type type, unsigned pos, int value)
6744 return basic_map_bound_si(bmap, type, pos, value, 1);
6747 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6748 enum isl_dim_type type, unsigned pos, int value, int upper)
6750 int i;
6752 map = isl_map_cow(map);
6753 if (isl_map_check_range(map, type, pos, 1) < 0)
6754 return isl_map_free(map);
6755 for (i = 0; i < map->n; ++i) {
6756 map->p[i] = basic_map_bound_si(map->p[i],
6757 type, pos, value, upper);
6758 if (!map->p[i])
6759 goto error;
6761 map = isl_map_unmark_normalized(map);
6762 return map;
6763 error:
6764 isl_map_free(map);
6765 return NULL;
6768 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6769 enum isl_dim_type type, unsigned pos, int value)
6771 return map_bound_si(map, type, pos, value, 0);
6774 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6775 enum isl_dim_type type, unsigned pos, int value)
6777 return map_bound_si(map, type, pos, value, 1);
6780 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6781 enum isl_dim_type type, unsigned pos, int value)
6783 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6784 type, pos, value));
6787 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6788 enum isl_dim_type type, unsigned pos, int value)
6790 return isl_map_upper_bound_si(set, type, pos, value);
6793 /* Bound the given variable of "bmap" from below (or above is "upper"
6794 * is set) to "value".
6796 static __isl_give isl_basic_map *basic_map_bound(
6797 __isl_take isl_basic_map *bmap,
6798 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6800 int j;
6801 isl_size total;
6803 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6804 return isl_basic_map_free(bmap);
6805 total = isl_basic_map_dim(bmap, isl_dim_all);
6806 if (total < 0)
6807 return isl_basic_map_free(bmap);
6808 pos += isl_basic_map_offset(bmap, type);
6809 bmap = isl_basic_map_cow(bmap);
6810 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6811 j = isl_basic_map_alloc_inequality(bmap);
6812 if (j < 0)
6813 goto error;
6814 isl_seq_clr(bmap->ineq[j], 1 + total);
6815 if (upper) {
6816 isl_int_set_si(bmap->ineq[j][pos], -1);
6817 isl_int_set(bmap->ineq[j][0], value);
6818 } else {
6819 isl_int_set_si(bmap->ineq[j][pos], 1);
6820 isl_int_neg(bmap->ineq[j][0], value);
6822 bmap = isl_basic_map_simplify(bmap);
6823 return isl_basic_map_finalize(bmap);
6824 error:
6825 isl_basic_map_free(bmap);
6826 return NULL;
6829 /* Bound the given variable of "map" from below (or above is "upper"
6830 * is set) to "value".
6832 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6833 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6835 int i;
6837 map = isl_map_cow(map);
6838 if (isl_map_check_range(map, type, pos, 1) < 0)
6839 return isl_map_free(map);
6840 for (i = map->n - 1; i >= 0; --i) {
6841 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6842 map = remove_if_empty(map, i);
6843 if (!map)
6844 return NULL;
6846 map = isl_map_unmark_normalized(map);
6847 return map;
6850 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6851 enum isl_dim_type type, unsigned pos, isl_int value)
6853 return map_bound(map, type, pos, value, 0);
6856 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6857 enum isl_dim_type type, unsigned pos, isl_int value)
6859 return map_bound(map, type, pos, value, 1);
6862 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6863 enum isl_dim_type type, unsigned pos, isl_int value)
6865 return isl_map_lower_bound(set, type, pos, value);
6868 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6869 enum isl_dim_type type, unsigned pos, isl_int value)
6871 return isl_map_upper_bound(set, type, pos, value);
6874 /* Force the values of the variable at position "pos" of type "type" of "map"
6875 * to be no smaller than "value".
6877 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6878 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6880 if (!value)
6881 goto error;
6882 if (!isl_val_is_int(value))
6883 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6884 "expecting integer value", goto error);
6885 map = isl_map_lower_bound(map, type, pos, value->n);
6886 isl_val_free(value);
6887 return map;
6888 error:
6889 isl_val_free(value);
6890 isl_map_free(map);
6891 return NULL;
6894 /* Force the values of the variable at position "pos" of type "type" of "set"
6895 * to be no smaller than "value".
6897 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6898 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6900 isl_map *map;
6902 map = set_to_map(set);
6903 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6906 /* Force the values of the variable at position "pos" of type "type" of "map"
6907 * to be no greater than "value".
6909 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6910 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6912 if (!value)
6913 goto error;
6914 if (!isl_val_is_int(value))
6915 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6916 "expecting integer value", goto error);
6917 map = isl_map_upper_bound(map, type, pos, value->n);
6918 isl_val_free(value);
6919 return map;
6920 error:
6921 isl_val_free(value);
6922 isl_map_free(map);
6923 return NULL;
6926 /* Force the values of the variable at position "pos" of type "type" of "set"
6927 * to be no greater than "value".
6929 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6930 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6932 isl_map *map;
6934 map = set_to_map(set);
6935 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6938 /* If "mv" has an explicit domain, then intersect the domain of "map"
6939 * with this explicit domain.
6941 * An isl_multi_val object never has an explicit domain,
6942 * so simply return "map".
6944 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
6945 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
6947 return map;
6950 #undef BASE
6951 #define BASE val
6952 #include "isl_map_bound_templ.c"
6954 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6955 * for each set dimension, by treating the set as a map.
6957 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6958 __isl_take isl_multi_val *bound,
6959 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6960 unsigned pos, __isl_take isl_val *value))
6962 isl_map *map;
6964 map = set_to_map(set);
6965 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6968 #undef BASE
6969 #define BASE pw_aff
6970 #include "isl_map_bound_templ.c"
6972 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6973 * for each set dimension, by converting the set and the bound
6974 * to objects living in a map space.
6976 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6977 __isl_take isl_multi_pw_aff *bound,
6978 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6979 unsigned pos, __isl_take TYPE *value))
6981 isl_map *map;
6983 map = isl_map_from_range(set);
6984 bound = isl_multi_pw_aff_from_range(bound);
6985 map = map_bound_multi_pw_aff(map, bound, set_bound);
6986 return isl_map_range(map);
6989 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6990 * setting a bound on the given output dimension.
6992 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6993 unsigned pos, __isl_take isl_val *v)
6995 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6998 /* Force the values of the set dimensions of "set"
6999 * to be no smaller than the corresponding values in "lower".
7001 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7002 __isl_take isl_multi_val *lower)
7004 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7007 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7008 * setting a bound on the given output dimension.
7010 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7011 unsigned pos, __isl_take isl_val *v)
7013 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7016 /* Force the values of the set dimensions of "set"
7017 * to be no greater than the corresponding values in "upper".
7019 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7020 __isl_take isl_multi_val *upper)
7022 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7025 /* Force the symbolic constant expression "bound"
7026 * to satisfy the relation "order" with respect to
7027 * the output variable at position "pos" of "map".
7029 * Create an affine expression representing the output variable
7030 * in terms of the range and
7031 * compare it using "order" to "bound" (defined on the domain).
7032 * The result is a relation between elements in domain and range that
7033 * can be intersected with "map".
7035 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7036 unsigned pos, __isl_take isl_pw_aff *bound,
7037 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7038 __isl_take isl_pw_aff *pa2))
7040 isl_space *space;
7041 isl_local_space *ls;
7042 isl_pw_aff *var;
7044 space = isl_space_range(isl_map_get_space(map));
7045 ls = isl_local_space_from_space(space);
7046 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7047 map = isl_map_intersect(map, order(bound, var));
7048 return map;
7051 /* Force the values of the output variable at position "pos" of "map"
7052 * to be no smaller than the symbolic constant expression "lower".
7054 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7055 unsigned pos, __isl_take isl_pw_aff *lower)
7057 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7060 /* Force the values of the output variable at position "pos" of "map"
7061 * to be no greater than the symbolic constant expression "upper".
7063 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7064 unsigned pos, __isl_take isl_pw_aff *upper)
7066 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7069 /* Force the values of the set dimensions of "set"
7070 * to be no smaller than the corresponding constant symbolic expressions
7071 * in "lower".
7073 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7074 __isl_take isl_multi_pw_aff *lower)
7076 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7079 /* Force the values of the set dimensions of "set"
7080 * to be no greater than the corresponding constant symbolic expressions
7081 * in "upper".
7083 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7084 __isl_take isl_multi_pw_aff *upper)
7086 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7089 /* Force the values of the output dimensions of "map"
7090 * to be no smaller than the corresponding constant symbolic expressions
7091 * in "lower".
7093 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7094 __isl_take isl_multi_pw_aff *lower)
7096 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7099 /* Force the values of the output dimensions of "map"
7100 * to be no greater than the corresponding constant symbolic expressions
7101 * in "upper".
7103 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7104 __isl_take isl_multi_pw_aff *upper)
7106 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7109 /* Bound the given variable of "bset" from below (or above is "upper"
7110 * is set) to "value".
7112 static __isl_give isl_basic_set *isl_basic_set_bound(
7113 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7114 isl_int value, int upper)
7116 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7117 type, pos, value, upper));
7120 /* Bound the given variable of "bset" from below (or above is "upper"
7121 * is set) to "value".
7123 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7124 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7125 __isl_take isl_val *value, int upper)
7127 if (!value)
7128 goto error;
7129 if (!isl_val_is_int(value))
7130 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7131 "expecting integer value", goto error);
7132 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7133 isl_val_free(value);
7134 return bset;
7135 error:
7136 isl_val_free(value);
7137 isl_basic_set_free(bset);
7138 return NULL;
7141 /* Bound the given variable of "bset" from below to "value".
7143 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7144 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7145 __isl_take isl_val *value)
7147 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7150 /* Bound the given variable of "bset" from above to "value".
7152 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7153 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7154 __isl_take isl_val *value)
7156 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7159 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7161 return isl_map_transform(map, &isl_space_reverse,
7162 &isl_basic_map_reverse);
7165 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7167 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7169 return isl_map_transform(map, &isl_space_range_reverse,
7170 &isl_basic_map_range_reverse);
7173 #undef TYPE
7174 #define TYPE isl_pw_multi_aff
7175 #undef SUFFIX
7176 #define SUFFIX _pw_multi_aff
7177 #undef EMPTY
7178 #define EMPTY isl_pw_multi_aff_empty
7179 #undef ADD
7180 #define ADD isl_pw_multi_aff_union_add
7181 #include "isl_map_lexopt_templ.c"
7183 /* Given a map "map", compute the lexicographically minimal
7184 * (or maximal) image element for each domain element in dom,
7185 * in the form of an isl_pw_multi_aff.
7186 * If "empty" is not NULL, then set *empty to those elements in dom that
7187 * do not have an image element.
7188 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7189 * should be computed over the domain of "map". "empty" is also NULL
7190 * in this case.
7192 * We first compute the lexicographically minimal or maximal element
7193 * in the first basic map. This results in a partial solution "res"
7194 * and a subset "todo" of dom that still need to be handled.
7195 * We then consider each of the remaining maps in "map" and successively
7196 * update both "res" and "todo".
7197 * If "empty" is NULL, then the todo sets are not needed and therefore
7198 * also not computed.
7200 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7201 __isl_take isl_map *map, __isl_take isl_set *dom,
7202 __isl_give isl_set **empty, unsigned flags)
7204 int i;
7205 int full;
7206 isl_pw_multi_aff *res;
7207 isl_set *todo;
7209 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7210 if (!map || (!full && !dom))
7211 goto error;
7213 if (isl_map_plain_is_empty(map)) {
7214 if (empty)
7215 *empty = dom;
7216 else
7217 isl_set_free(dom);
7218 return isl_pw_multi_aff_from_map(map);
7221 res = basic_map_partial_lexopt_pw_multi_aff(
7222 isl_basic_map_copy(map->p[0]),
7223 isl_set_copy(dom), empty, flags);
7225 if (empty)
7226 todo = *empty;
7227 for (i = 1; i < map->n; ++i) {
7228 isl_pw_multi_aff *res_i;
7230 res_i = basic_map_partial_lexopt_pw_multi_aff(
7231 isl_basic_map_copy(map->p[i]),
7232 isl_set_copy(dom), empty, flags);
7234 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7235 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7236 else
7237 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7239 if (empty)
7240 todo = isl_set_intersect(todo, *empty);
7243 isl_set_free(dom);
7244 isl_map_free(map);
7246 if (empty)
7247 *empty = todo;
7249 return res;
7250 error:
7251 if (empty)
7252 *empty = NULL;
7253 isl_set_free(dom);
7254 isl_map_free(map);
7255 return NULL;
7258 #undef TYPE
7259 #define TYPE isl_map
7260 #undef SUFFIX
7261 #define SUFFIX
7262 #undef EMPTY
7263 #define EMPTY isl_map_empty
7264 #undef ADD
7265 #define ADD isl_map_union_disjoint
7266 #include "isl_map_lexopt_templ.c"
7268 /* Given a map "map", compute the lexicographically minimal
7269 * (or maximal) image element for each domain element in "dom",
7270 * in the form of an isl_map.
7271 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7272 * do not have an image element.
7273 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7274 * should be computed over the domain of "map". "empty" is also NULL
7275 * in this case.
7277 * If the input consists of more than one disjunct, then first
7278 * compute the desired result in the form of an isl_pw_multi_aff and
7279 * then convert that into an isl_map.
7281 * This function used to have an explicit implementation in terms
7282 * of isl_maps, but it would continually intersect the domains of
7283 * partial results with the complement of the domain of the next
7284 * partial solution, potentially leading to an explosion in the number
7285 * of disjuncts if there are several disjuncts in the input.
7286 * An even earlier implementation of this function would look for
7287 * better results in the domain of the partial result and for extra
7288 * results in the complement of this domain, which would lead to
7289 * even more splintering.
7291 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7292 __isl_take isl_map *map, __isl_take isl_set *dom,
7293 __isl_give isl_set **empty, unsigned flags)
7295 int full;
7296 struct isl_map *res;
7297 isl_pw_multi_aff *pma;
7299 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7300 if (!map || (!full && !dom))
7301 goto error;
7303 if (isl_map_plain_is_empty(map)) {
7304 if (empty)
7305 *empty = dom;
7306 else
7307 isl_set_free(dom);
7308 return map;
7311 if (map->n == 1) {
7312 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7313 dom, empty, flags);
7314 isl_map_free(map);
7315 return res;
7318 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7319 flags);
7320 return isl_map_from_pw_multi_aff_internal(pma);
7321 error:
7322 if (empty)
7323 *empty = NULL;
7324 isl_set_free(dom);
7325 isl_map_free(map);
7326 return NULL;
7329 __isl_give isl_map *isl_map_partial_lexmax(
7330 __isl_take isl_map *map, __isl_take isl_set *dom,
7331 __isl_give isl_set **empty)
7333 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7336 __isl_give isl_map *isl_map_partial_lexmin(
7337 __isl_take isl_map *map, __isl_take isl_set *dom,
7338 __isl_give isl_set **empty)
7340 return isl_map_partial_lexopt(map, dom, empty, 0);
7343 __isl_give isl_set *isl_set_partial_lexmin(
7344 __isl_take isl_set *set, __isl_take isl_set *dom,
7345 __isl_give isl_set **empty)
7347 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7348 dom, empty));
7351 __isl_give isl_set *isl_set_partial_lexmax(
7352 __isl_take isl_set *set, __isl_take isl_set *dom,
7353 __isl_give isl_set **empty)
7355 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7356 dom, empty));
7359 /* Compute the lexicographic minimum (or maximum if "flags" includes
7360 * ISL_OPT_MAX) of "bset" over its parametric domain.
7362 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7363 unsigned flags)
7365 return isl_basic_map_lexopt(bset, flags);
7368 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7370 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7373 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7375 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7378 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7380 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7383 /* Compute the lexicographic minimum of "bset" over its parametric domain
7384 * for the purpose of quantifier elimination.
7385 * That is, find an explicit representation for all the existentially
7386 * quantified variables in "bset" by computing their lexicographic
7387 * minimum.
7389 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7390 __isl_take isl_basic_set *bset)
7392 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7395 /* Given a basic map with one output dimension, compute the minimum or
7396 * maximum of that dimension as an isl_pw_aff.
7398 * Compute the optimum as a lexicographic optimum over the single
7399 * output dimension and extract the single isl_pw_aff from the result.
7401 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7402 int max)
7404 isl_pw_multi_aff *pma;
7405 isl_pw_aff *pwaff;
7407 bmap = isl_basic_map_copy(bmap);
7408 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7409 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7410 isl_pw_multi_aff_free(pma);
7412 return pwaff;
7415 /* Compute the minimum or maximum of the given output dimension
7416 * as a function of the parameters and the input dimensions,
7417 * but independently of the other output dimensions.
7419 * We first project out the other output dimension and then compute
7420 * the "lexicographic" maximum in each basic map, combining the results
7421 * using isl_pw_aff_union_max.
7423 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7424 int max)
7426 int i;
7427 isl_pw_aff *pwaff;
7428 isl_size n_out;
7430 n_out = isl_map_dim(map, isl_dim_out);
7431 if (n_out < 0)
7432 map = isl_map_free(map);
7433 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7434 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7435 if (!map)
7436 return NULL;
7438 if (map->n == 0) {
7439 isl_space *space = isl_map_get_space(map);
7440 isl_map_free(map);
7441 return isl_pw_aff_empty(space);
7444 pwaff = basic_map_dim_opt(map->p[0], max);
7445 for (i = 1; i < map->n; ++i) {
7446 isl_pw_aff *pwaff_i;
7448 pwaff_i = basic_map_dim_opt(map->p[i], max);
7449 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7452 isl_map_free(map);
7454 return pwaff;
7457 /* Compute the minimum of the given output dimension as a function of the
7458 * parameters and input dimensions, but independently of
7459 * the other output dimensions.
7461 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7463 return map_dim_opt(map, pos, 0);
7466 /* Compute the maximum of the given output dimension as a function of the
7467 * parameters and input dimensions, but independently of
7468 * the other output dimensions.
7470 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7472 return map_dim_opt(map, pos, 1);
7475 /* Compute the minimum or maximum of the given set dimension
7476 * as a function of the parameters,
7477 * but independently of the other set dimensions.
7479 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7480 int max)
7482 return map_dim_opt(set, pos, max);
7485 /* Compute the maximum of the given set dimension as a function of the
7486 * parameters, but independently of the other set dimensions.
7488 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7490 return set_dim_opt(set, pos, 1);
7493 /* Compute the minimum of the given set dimension as a function of the
7494 * parameters, but independently of the other set dimensions.
7496 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7498 return set_dim_opt(set, pos, 0);
7501 /* Apply a preimage specified by "mat" on the parameters of "bset".
7502 * bset is assumed to have only parameters and divs.
7504 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7505 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7507 isl_size nparam;
7509 nparam = isl_basic_set_dim(bset, isl_dim_param);
7510 if (nparam < 0 || !mat)
7511 goto error;
7513 bset->dim = isl_space_cow(bset->dim);
7514 if (!bset->dim)
7515 goto error;
7517 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7519 bset->dim->nparam = 0;
7520 bset->dim->n_out = nparam;
7521 bset = isl_basic_set_preimage(bset, mat);
7522 if (bset) {
7523 bset->dim->nparam = bset->dim->n_out;
7524 bset->dim->n_out = 0;
7526 return bset;
7527 error:
7528 isl_mat_free(mat);
7529 isl_basic_set_free(bset);
7530 return NULL;
7533 /* Apply a preimage specified by "mat" on the parameters of "set".
7534 * set is assumed to have only parameters and divs.
7536 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7537 __isl_take isl_mat *mat)
7539 isl_space *space;
7540 isl_size nparam;
7542 nparam = isl_set_dim(set, isl_dim_param);
7543 if (nparam < 0 || !mat)
7544 goto error;
7546 if (mat->n_row != 1 + nparam)
7547 isl_die(isl_set_get_ctx(set), isl_error_internal,
7548 "unexpected number of rows", goto error);
7550 space = isl_set_get_space(set);
7551 space = isl_space_move_dims(space, isl_dim_set, 0,
7552 isl_dim_param, 0, nparam);
7553 set = isl_set_reset_space(set, space);
7554 set = isl_set_preimage(set, mat);
7555 nparam = isl_set_dim(set, isl_dim_out);
7556 if (nparam < 0)
7557 set = isl_set_free(set);
7558 space = isl_set_get_space(set);
7559 space = isl_space_move_dims(space, isl_dim_param, 0,
7560 isl_dim_out, 0, nparam);
7561 set = isl_set_reset_space(set, space);
7562 return set;
7563 error:
7564 isl_mat_free(mat);
7565 isl_set_free(set);
7566 return NULL;
7569 /* Intersect the basic set "bset" with the affine space specified by the
7570 * equalities in "eq".
7572 static __isl_give isl_basic_set *basic_set_append_equalities(
7573 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7575 int i, k;
7576 unsigned len;
7578 if (!bset || !eq)
7579 goto error;
7581 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7582 if (!bset)
7583 goto error;
7585 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7586 for (i = 0; i < eq->n_row; ++i) {
7587 k = isl_basic_set_alloc_equality(bset);
7588 if (k < 0)
7589 goto error;
7590 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7591 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7593 isl_mat_free(eq);
7595 bset = isl_basic_set_gauss(bset, NULL);
7596 bset = isl_basic_set_finalize(bset);
7598 return bset;
7599 error:
7600 isl_mat_free(eq);
7601 isl_basic_set_free(bset);
7602 return NULL;
7605 /* Intersect the set "set" with the affine space specified by the
7606 * equalities in "eq".
7608 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7609 __isl_take isl_mat *eq)
7611 int i;
7613 if (!set || !eq)
7614 goto error;
7616 for (i = 0; i < set->n; ++i) {
7617 set->p[i] = basic_set_append_equalities(set->p[i],
7618 isl_mat_copy(eq));
7619 if (!set->p[i])
7620 goto error;
7622 isl_mat_free(eq);
7623 return set;
7624 error:
7625 isl_mat_free(eq);
7626 isl_set_free(set);
7627 return NULL;
7630 /* Given a basic set "bset" that only involves parameters and existentially
7631 * quantified variables, return the index of the first equality
7632 * that only involves parameters. If there is no such equality then
7633 * return bset->n_eq.
7635 * This function assumes that isl_basic_set_gauss has been called on "bset".
7637 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7639 int i, j;
7640 isl_size nparam, n_div;
7642 nparam = isl_basic_set_dim(bset, isl_dim_param);
7643 n_div = isl_basic_set_dim(bset, isl_dim_div);
7644 if (nparam < 0 || n_div < 0)
7645 return -1;
7647 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7648 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7649 ++i;
7652 return i;
7655 /* Compute an explicit representation for the existentially quantified
7656 * variables in "bset" by computing the "minimal value" of the set
7657 * variables. Since there are no set variables, the computation of
7658 * the minimal value essentially computes an explicit representation
7659 * of the non-empty part(s) of "bset".
7661 * The input only involves parameters and existentially quantified variables.
7662 * All equalities among parameters have been removed.
7664 * Since the existentially quantified variables in the result are in general
7665 * going to be different from those in the input, we first replace
7666 * them by the minimal number of variables based on their equalities.
7667 * This should simplify the parametric integer programming.
7669 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7671 isl_morph *morph1, *morph2;
7672 isl_set *set;
7673 isl_size n;
7675 if (!bset)
7676 return NULL;
7677 if (bset->n_eq == 0)
7678 return isl_basic_set_lexmin_compute_divs(bset);
7680 morph1 = isl_basic_set_parameter_compression(bset);
7681 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7682 bset = isl_basic_set_lift(bset);
7683 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7684 bset = isl_morph_basic_set(morph2, bset);
7685 n = isl_basic_set_dim(bset, isl_dim_set);
7686 if (n < 0)
7687 bset = isl_basic_set_free(bset);
7688 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7690 set = isl_basic_set_lexmin_compute_divs(bset);
7692 set = isl_morph_set(isl_morph_inverse(morph1), set);
7694 return set;
7697 /* Project the given basic set onto its parameter domain, possibly introducing
7698 * new, explicit, existential variables in the constraints.
7699 * The input has parameters and (possibly implicit) existential variables.
7700 * The output has the same parameters, but only
7701 * explicit existentially quantified variables.
7703 * The actual projection is performed by pip, but pip doesn't seem
7704 * to like equalities very much, so we first remove the equalities
7705 * among the parameters by performing a variable compression on
7706 * the parameters. Afterward, an inverse transformation is performed
7707 * and the equalities among the parameters are inserted back in.
7709 * The variable compression on the parameters may uncover additional
7710 * equalities that were only implicit before. We therefore check
7711 * if there are any new parameter equalities in the result and
7712 * if so recurse. The removal of parameter equalities is required
7713 * for the parameter compression performed by base_compute_divs.
7715 static __isl_give isl_set *parameter_compute_divs(
7716 __isl_take isl_basic_set *bset)
7718 int i;
7719 struct isl_mat *eq;
7720 struct isl_mat *T, *T2;
7721 struct isl_set *set;
7722 isl_size nparam;
7724 bset = isl_basic_set_cow(bset);
7725 if (!bset)
7726 return NULL;
7728 if (bset->n_eq == 0)
7729 return base_compute_divs(bset);
7731 bset = isl_basic_set_gauss(bset, NULL);
7732 if (!bset)
7733 return NULL;
7734 if (isl_basic_set_plain_is_empty(bset))
7735 return isl_set_from_basic_set(bset);
7737 i = first_parameter_equality(bset);
7738 if (i == bset->n_eq)
7739 return base_compute_divs(bset);
7741 nparam = isl_basic_set_dim(bset, isl_dim_param);
7742 if (nparam < 0)
7743 return isl_set_from_basic_set(isl_basic_set_free(bset));
7744 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7745 0, 1 + nparam);
7746 eq = isl_mat_cow(eq);
7747 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7748 if (T && T->n_col == 0) {
7749 isl_mat_free(T);
7750 isl_mat_free(T2);
7751 isl_mat_free(eq);
7752 bset = isl_basic_set_set_to_empty(bset);
7753 return isl_set_from_basic_set(bset);
7755 bset = basic_set_parameter_preimage(bset, T);
7757 i = first_parameter_equality(bset);
7758 if (!bset)
7759 set = NULL;
7760 else if (i == bset->n_eq)
7761 set = base_compute_divs(bset);
7762 else
7763 set = parameter_compute_divs(bset);
7764 set = set_parameter_preimage(set, T2);
7765 set = set_append_equalities(set, eq);
7766 return set;
7769 /* Insert the divs from "ls" before those of "bmap".
7771 * The number of columns is not changed, which means that the last
7772 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7773 * The caller is responsible for removing the same number of dimensions
7774 * from the space of "bmap".
7776 static __isl_give isl_basic_map *insert_divs_from_local_space(
7777 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7779 int i;
7780 isl_size n_div;
7781 int old_n_div;
7783 n_div = isl_local_space_dim(ls, isl_dim_div);
7784 if (n_div < 0)
7785 return isl_basic_map_free(bmap);
7786 if (n_div == 0)
7787 return bmap;
7789 old_n_div = bmap->n_div;
7790 bmap = insert_div_rows(bmap, n_div);
7791 if (!bmap)
7792 return NULL;
7794 for (i = 0; i < n_div; ++i) {
7795 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7796 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7799 return bmap;
7802 /* Replace the space of "bmap" by the space and divs of "ls".
7804 * If "ls" has any divs, then we simplify the result since we may
7805 * have discovered some additional equalities that could simplify
7806 * the div expressions.
7808 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7809 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7811 isl_size n_div;
7813 bmap = isl_basic_map_cow(bmap);
7814 n_div = isl_local_space_dim(ls, isl_dim_div);
7815 if (!bmap || n_div < 0)
7816 goto error;
7818 bmap = insert_divs_from_local_space(bmap, ls);
7819 if (!bmap)
7820 goto error;
7822 isl_space_free(bmap->dim);
7823 bmap->dim = isl_local_space_get_space(ls);
7824 if (!bmap->dim)
7825 goto error;
7827 isl_local_space_free(ls);
7828 if (n_div > 0)
7829 bmap = isl_basic_map_simplify(bmap);
7830 bmap = isl_basic_map_finalize(bmap);
7831 return bmap;
7832 error:
7833 isl_basic_map_free(bmap);
7834 isl_local_space_free(ls);
7835 return NULL;
7838 /* Replace the space of "map" by the space and divs of "ls".
7840 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7841 __isl_take isl_local_space *ls)
7843 int i;
7845 map = isl_map_cow(map);
7846 if (!map || !ls)
7847 goto error;
7849 for (i = 0; i < map->n; ++i) {
7850 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7851 isl_local_space_copy(ls));
7852 if (!map->p[i])
7853 goto error;
7855 isl_space_free(isl_map_take_space(map));
7856 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7858 isl_local_space_free(ls);
7859 return map;
7860 error:
7861 isl_local_space_free(ls);
7862 isl_map_free(map);
7863 return NULL;
7866 /* Compute an explicit representation for the existentially
7867 * quantified variables for which do not know any explicit representation yet.
7869 * We first sort the existentially quantified variables so that the
7870 * existentially quantified variables for which we already have an explicit
7871 * representation are placed before those for which we do not.
7872 * The input dimensions, the output dimensions and the existentially
7873 * quantified variables for which we already have an explicit
7874 * representation are then turned into parameters.
7875 * compute_divs returns a map with the same parameters and
7876 * no input or output dimensions and the dimension specification
7877 * is reset to that of the input, including the existentially quantified
7878 * variables for which we already had an explicit representation.
7880 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7882 struct isl_basic_set *bset;
7883 struct isl_set *set;
7884 struct isl_map *map;
7885 isl_space *space;
7886 isl_local_space *ls;
7887 isl_size nparam;
7888 isl_size n_in;
7889 isl_size n_out;
7890 int n_known;
7891 int i;
7893 bmap = isl_basic_map_sort_divs(bmap);
7894 bmap = isl_basic_map_cow(bmap);
7895 if (!bmap)
7896 return NULL;
7898 n_known = isl_basic_map_first_unknown_div(bmap);
7899 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7900 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7901 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7902 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7903 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7905 space = isl_space_set_alloc(bmap->ctx,
7906 nparam + n_in + n_out + n_known, 0);
7907 if (!space)
7908 goto error;
7910 ls = isl_basic_map_get_local_space(bmap);
7911 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7912 n_known, bmap->n_div - n_known);
7913 if (n_known > 0) {
7914 for (i = n_known; i < bmap->n_div; ++i)
7915 swap_div(bmap, i - n_known, i);
7916 bmap->n_div -= n_known;
7917 bmap->extra -= n_known;
7919 bmap = isl_basic_map_reset_space(bmap, space);
7920 bset = bset_from_bmap(bmap);
7922 set = parameter_compute_divs(bset);
7923 map = set_to_map(set);
7924 map = replace_space_by_local_space(map, ls);
7926 return map;
7927 error:
7928 isl_basic_map_free(bmap);
7929 return NULL;
7932 /* Remove the explicit representation of local variable "div",
7933 * if there is any.
7935 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7936 __isl_take isl_basic_map *bmap, int div)
7938 isl_bool unknown;
7940 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7941 if (unknown < 0)
7942 return isl_basic_map_free(bmap);
7943 if (unknown)
7944 return bmap;
7946 bmap = isl_basic_map_cow(bmap);
7947 if (!bmap)
7948 return NULL;
7949 isl_int_set_si(bmap->div[div][0], 0);
7950 return bmap;
7953 /* Is local variable "div" of "bmap" marked as not having an explicit
7954 * representation?
7955 * Note that even if "div" is not marked in this way and therefore
7956 * has an explicit representation, this representation may still
7957 * depend (indirectly) on other local variables that do not
7958 * have an explicit representation.
7960 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7961 int div)
7963 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7964 return isl_bool_error;
7965 return isl_int_is_zero(bmap->div[div][0]);
7968 /* Return the position of the first local variable that does not
7969 * have an explicit representation.
7970 * Return the total number of local variables if they all have
7971 * an explicit representation.
7972 * Return -1 on error.
7974 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7976 int i;
7978 if (!bmap)
7979 return -1;
7981 for (i = 0; i < bmap->n_div; ++i) {
7982 if (!isl_basic_map_div_is_known(bmap, i))
7983 return i;
7985 return bmap->n_div;
7988 /* Return the position of the first local variable that does not
7989 * have an explicit representation.
7990 * Return the total number of local variables if they all have
7991 * an explicit representation.
7992 * Return -1 on error.
7994 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7996 return isl_basic_map_first_unknown_div(bset);
7999 /* Does "bmap" have an explicit representation for all local variables?
8001 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8003 int first;
8004 isl_size n;
8006 n = isl_basic_map_dim(bmap, isl_dim_div);
8007 first = isl_basic_map_first_unknown_div(bmap);
8008 if (n < 0 || first < 0)
8009 return isl_bool_error;
8010 return first == n;
8013 /* Do all basic maps in "map" have an explicit representation
8014 * for all local variables?
8016 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8018 int i;
8020 if (!map)
8021 return isl_bool_error;
8023 for (i = 0; i < map->n; ++i) {
8024 int known = isl_basic_map_divs_known(map->p[i]);
8025 if (known <= 0)
8026 return known;
8029 return isl_bool_true;
8032 /* If bmap contains any unknown divs, then compute explicit
8033 * expressions for them. However, this computation may be
8034 * quite expensive, so first try to remove divs that aren't
8035 * strictly needed.
8037 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8039 int known;
8040 struct isl_map *map;
8042 known = isl_basic_map_divs_known(bmap);
8043 if (known < 0)
8044 goto error;
8045 if (known)
8046 return isl_map_from_basic_map(bmap);
8048 bmap = isl_basic_map_drop_redundant_divs(bmap);
8050 known = isl_basic_map_divs_known(bmap);
8051 if (known < 0)
8052 goto error;
8053 if (known)
8054 return isl_map_from_basic_map(bmap);
8056 map = compute_divs(bmap);
8057 return map;
8058 error:
8059 isl_basic_map_free(bmap);
8060 return NULL;
8063 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8065 int i;
8066 int known;
8067 struct isl_map *res;
8069 if (!map)
8070 return NULL;
8071 if (map->n == 0)
8072 return map;
8074 known = isl_map_divs_known(map);
8075 if (known < 0) {
8076 isl_map_free(map);
8077 return NULL;
8079 if (known)
8080 return map;
8082 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8083 for (i = 1 ; i < map->n; ++i) {
8084 struct isl_map *r2;
8085 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8086 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8087 res = isl_map_union_disjoint(res, r2);
8088 else
8089 res = isl_map_union(res, r2);
8091 isl_map_free(map);
8093 return res;
8096 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8098 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8101 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8103 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8106 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8108 isl_space *space;
8109 isl_size n_out;
8111 n_out = isl_map_dim(map, isl_dim_out);
8112 if (n_out < 0)
8113 return set_from_map(isl_map_free(map));
8114 space = isl_space_domain(isl_map_get_space(map));
8116 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8118 return set_from_map(isl_map_reset_space(map, space));
8121 /* Return the union of "map1" and "map2", where we assume for now that
8122 * "map1" and "map2" are disjoint. Note that the basic maps inside
8123 * "map1" or "map2" may not be disjoint from each other.
8124 * Also note that this function is also called from isl_map_union,
8125 * which takes care of handling the situation where "map1" and "map2"
8126 * may not be disjoint.
8128 * If one of the inputs is empty, we can simply return the other input.
8129 * Similarly, if one of the inputs is universal, then it is equal to the union.
8131 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8132 __isl_take isl_map *map2)
8134 int i;
8135 unsigned flags = 0;
8136 struct isl_map *map = NULL;
8137 int is_universe;
8139 if (isl_map_check_equal_space(map1, map2) < 0)
8140 goto error;
8142 if (map1->n == 0) {
8143 isl_map_free(map1);
8144 return map2;
8146 if (map2->n == 0) {
8147 isl_map_free(map2);
8148 return map1;
8151 is_universe = isl_map_plain_is_universe(map1);
8152 if (is_universe < 0)
8153 goto error;
8154 if (is_universe) {
8155 isl_map_free(map2);
8156 return map1;
8159 is_universe = isl_map_plain_is_universe(map2);
8160 if (is_universe < 0)
8161 goto error;
8162 if (is_universe) {
8163 isl_map_free(map1);
8164 return map2;
8167 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8168 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8169 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8171 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8172 map1->n + map2->n, flags);
8173 if (!map)
8174 goto error;
8175 for (i = 0; i < map1->n; ++i) {
8176 map = isl_map_add_basic_map(map,
8177 isl_basic_map_copy(map1->p[i]));
8178 if (!map)
8179 goto error;
8181 for (i = 0; i < map2->n; ++i) {
8182 map = isl_map_add_basic_map(map,
8183 isl_basic_map_copy(map2->p[i]));
8184 if (!map)
8185 goto error;
8187 isl_map_free(map1);
8188 isl_map_free(map2);
8189 return map;
8190 error:
8191 isl_map_free(map);
8192 isl_map_free(map1);
8193 isl_map_free(map2);
8194 return NULL;
8197 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8198 * guaranteed to be disjoint by the caller.
8200 * Note that this functions is called from within isl_map_make_disjoint,
8201 * so we have to be careful not to touch the constraints of the inputs
8202 * in any way.
8204 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8205 __isl_take isl_map *map2)
8207 isl_map_align_params_bin(&map1, &map2);
8208 return map_union_disjoint(map1, map2);
8211 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8212 * not be disjoint.
8214 * We currently simply call map_union_disjoint, the internal operation
8215 * of which does not really depend on the inputs being disjoint.
8216 * If the result contains more than one basic map, then we clear
8217 * the disjoint flag since the result may contain basic maps from
8218 * both inputs and these are not guaranteed to be disjoint.
8220 * As a special case, if "map1" and "map2" are obviously equal,
8221 * then we simply return "map1".
8223 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8224 __isl_take isl_map *map2)
8226 int equal;
8228 if (isl_map_align_params_bin(&map1, &map2) < 0)
8229 goto error;
8231 equal = isl_map_plain_is_equal(map1, map2);
8232 if (equal < 0)
8233 goto error;
8234 if (equal) {
8235 isl_map_free(map2);
8236 return map1;
8239 map1 = map_union_disjoint(map1, map2);
8240 if (!map1)
8241 return NULL;
8242 if (map1->n > 1)
8243 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8244 return map1;
8245 error:
8246 isl_map_free(map1);
8247 isl_map_free(map2);
8248 return NULL;
8251 __isl_give isl_set *isl_set_union_disjoint(
8252 __isl_take isl_set *set1, __isl_take isl_set *set2)
8254 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8255 set_to_map(set2)));
8258 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8259 __isl_take isl_set *set2)
8261 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8264 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8265 * the results in a map living in "space".
8267 * "map" and "set" are assumed to be compatible and non-NULL.
8269 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8270 __isl_take isl_space *space, __isl_take isl_set *set,
8271 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8272 __isl_take isl_basic_set *bset))
8274 unsigned flags = 0;
8275 struct isl_map *result;
8276 int i, j;
8278 if (isl_set_plain_is_universe(set)) {
8279 isl_set_free(set);
8280 return isl_map_reset_equal_dim_space(map, space);
8283 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8284 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8285 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8287 result = isl_map_alloc_space(space, map->n * set->n, flags);
8288 for (i = 0; result && i < map->n; ++i)
8289 for (j = 0; j < set->n; ++j) {
8290 result = isl_map_add_basic_map(result,
8291 fn(isl_basic_map_copy(map->p[i]),
8292 isl_basic_set_copy(set->p[j])));
8293 if (!result)
8294 break;
8297 isl_map_free(map);
8298 isl_set_free(set);
8299 return result;
8302 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8303 __isl_take isl_set *set)
8305 isl_bool ok;
8306 isl_space *space;
8308 isl_map_align_params_set(&map, &set);
8309 ok = isl_map_compatible_range(map, set);
8310 if (ok < 0)
8311 goto error;
8312 if (!ok)
8313 isl_die(set->ctx, isl_error_invalid,
8314 "incompatible spaces", goto error);
8316 space = isl_map_get_space(map);
8317 return map_intersect_set(map, space, set,
8318 &isl_basic_map_intersect_range);
8319 error:
8320 isl_map_free(map);
8321 isl_set_free(set);
8322 return NULL;
8325 /* Intersect the domain of "map" with "set".
8327 * If the domain dimensions of "map" do not have any identifiers,
8328 * then copy them over from "set".
8330 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8331 __isl_take isl_set *set)
8333 isl_bool ok;
8334 isl_space *space;
8336 isl_map_align_params_set(&map, &set);
8337 ok = isl_map_compatible_domain(map, set);
8338 if (ok < 0)
8339 goto error;
8340 if (!ok)
8341 isl_die(set->ctx, isl_error_invalid,
8342 "incompatible spaces", goto error);
8344 space = isl_map_get_space(map);
8345 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8346 isl_set_peek_space(set), isl_dim_set);
8347 return map_intersect_set(map, space, set,
8348 &isl_basic_map_intersect_domain);
8349 error:
8350 isl_map_free(map);
8351 isl_set_free(set);
8352 return NULL;
8355 #undef TYPE
8356 #define TYPE isl_map
8357 static
8358 #include "isl_copy_tuple_id_templ.c"
8360 /* Data structure that specifies how isl_map_intersect_factor
8361 * should operate.
8363 * "preserve_type" is the tuple where the factor differs from
8364 * the input map and of which the identifiers needs
8365 * to be preserved explicitly.
8366 * "other_factor" is used to extract the space of the other factor
8367 * from the space of the product ("map").
8368 * "product" is used to combine the given factor and a universe map
8369 * in the space returned by "other_factor" to produce a map
8370 * that lives in the same space as the input map.
8372 struct isl_intersect_factor_control {
8373 enum isl_dim_type preserve_type;
8374 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8375 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8376 __isl_take isl_map *other);
8379 /* Given a map "map" in some product space and a map "factor"
8380 * living in some factor space, return the intersection.
8382 * After aligning the parameters,
8383 * the map "factor" is first extended to a map living in the same space
8384 * as "map" and then a regular intersection is computed.
8386 * Note that the extension is computed as a product, which is anonymous
8387 * by default. If "map" has an identifier on the corresponding tuple,
8388 * then this identifier needs to be set on the product
8389 * before the intersection is computed.
8391 static __isl_give isl_map *isl_map_intersect_factor(
8392 __isl_take isl_map *map, __isl_take isl_map *factor,
8393 struct isl_intersect_factor_control *control)
8395 isl_bool equal;
8396 isl_space *space;
8397 isl_map *other, *product;
8399 equal = isl_map_has_equal_params(map, factor);
8400 if (equal < 0)
8401 goto error;
8402 if (!equal) {
8403 map = isl_map_align_params(map, isl_map_get_space(factor));
8404 factor = isl_map_align_params(factor, isl_map_get_space(map));
8407 space = isl_map_get_space(map);
8408 other = isl_map_universe(control->other_factor(space));
8409 product = control->product(factor, other);
8411 space = isl_map_peek_space(map);
8412 product = isl_map_copy_tuple_id(product, control->preserve_type,
8413 space, control->preserve_type);
8414 return map_intersect(map, product);
8415 error:
8416 isl_map_free(map);
8417 isl_map_free(factor);
8418 return NULL;
8421 /* Return the domain product of "map2" and "map1".
8423 static __isl_give isl_map *isl_map_reverse_domain_product(
8424 __isl_take isl_map *map1, __isl_take isl_map *map2)
8426 return isl_map_domain_product(map2, map1);
8429 /* Return the range product of "map2" and "map1".
8431 static __isl_give isl_map *isl_map_reverse_range_product(
8432 __isl_take isl_map *map1, __isl_take isl_map *map2)
8434 return isl_map_range_product(map2, map1);
8437 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8438 * in the space A -> C, return the intersection.
8440 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8441 __isl_take isl_map *map, __isl_take isl_map *factor)
8443 struct isl_intersect_factor_control control = {
8444 .preserve_type = isl_dim_in,
8445 .other_factor = isl_space_domain_factor_range,
8446 .product = isl_map_domain_product,
8449 return isl_map_intersect_factor(map, factor, &control);
8452 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8453 * in the space B -> C, return the intersection.
8455 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8456 __isl_take isl_map *map, __isl_take isl_map *factor)
8458 struct isl_intersect_factor_control control = {
8459 .preserve_type = isl_dim_in,
8460 .other_factor = isl_space_domain_factor_domain,
8461 .product = isl_map_reverse_domain_product,
8464 return isl_map_intersect_factor(map, factor, &control);
8467 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8468 * in the space A -> B, return the intersection.
8470 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8471 __isl_take isl_map *map, __isl_take isl_map *factor)
8473 struct isl_intersect_factor_control control = {
8474 .preserve_type = isl_dim_out,
8475 .other_factor = isl_space_range_factor_range,
8476 .product = isl_map_range_product,
8479 return isl_map_intersect_factor(map, factor, &control);
8482 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8483 * in the space A -> C, return the intersection.
8485 __isl_give isl_map *isl_map_intersect_range_factor_range(
8486 __isl_take isl_map *map, __isl_take isl_map *factor)
8488 struct isl_intersect_factor_control control = {
8489 .preserve_type = isl_dim_out,
8490 .other_factor = isl_space_range_factor_domain,
8491 .product = isl_map_reverse_range_product,
8494 return isl_map_intersect_factor(map, factor, &control);
8497 /* Given a set "set" in a space [A -> B] and a set "domain"
8498 * in the space A, return the intersection.
8500 * The set "domain" is first extended to a set living in the space
8501 * [A -> B] and then a regular intersection is computed.
8503 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8504 __isl_take isl_set *domain)
8506 struct isl_intersect_factor_control control = {
8507 .preserve_type = isl_dim_set,
8508 .other_factor = isl_space_factor_range,
8509 .product = isl_map_range_product,
8512 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8513 set_to_map(domain), &control));
8516 /* Given a set "set" in a space [A -> B] and a set "range"
8517 * in the space B, return the intersection.
8519 * The set "range" is first extended to a set living in the space
8520 * [A -> B] and then a regular intersection is computed.
8522 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8523 __isl_take isl_set *range)
8525 struct isl_intersect_factor_control control = {
8526 .preserve_type = isl_dim_set,
8527 .other_factor = isl_space_factor_domain,
8528 .product = isl_map_reverse_range_product,
8531 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8532 set_to_map(range), &control));
8535 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8536 __isl_take isl_map *map2)
8538 if (isl_map_align_params_bin(&map1, &map2) < 0)
8539 goto error;
8540 map1 = isl_map_reverse(map1);
8541 map1 = isl_map_apply_range(map1, map2);
8542 return isl_map_reverse(map1);
8543 error:
8544 isl_map_free(map1);
8545 isl_map_free(map2);
8546 return NULL;
8549 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8550 __isl_take isl_map *map2)
8552 isl_space *space;
8553 struct isl_map *result;
8554 int i, j;
8556 if (isl_map_align_params_bin(&map1, &map2) < 0)
8557 goto error;
8559 space = isl_space_join(isl_space_copy(map1->dim),
8560 isl_space_copy(map2->dim));
8562 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8563 if (!result)
8564 goto error;
8565 for (i = 0; i < map1->n; ++i)
8566 for (j = 0; j < map2->n; ++j) {
8567 result = isl_map_add_basic_map(result,
8568 isl_basic_map_apply_range(
8569 isl_basic_map_copy(map1->p[i]),
8570 isl_basic_map_copy(map2->p[j])));
8571 if (!result)
8572 goto error;
8574 isl_map_free(map1);
8575 isl_map_free(map2);
8576 if (result && result->n <= 1)
8577 ISL_F_SET(result, ISL_MAP_DISJOINT);
8578 return result;
8579 error:
8580 isl_map_free(map1);
8581 isl_map_free(map2);
8582 return NULL;
8585 /* Is "bmap" a transformation, i.e.,
8586 * does it relate elements from the same space.
8588 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8590 isl_space *space;
8592 space = isl_basic_map_peek_space(bmap);
8593 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8596 /* Check that "bmap" is a transformation, i.e.,
8597 * that it relates elements from the same space.
8599 static isl_stat isl_basic_map_check_transformation(
8600 __isl_keep isl_basic_map *bmap)
8602 isl_bool equal;
8604 equal = isl_basic_map_is_transformation(bmap);
8605 if (equal < 0)
8606 return isl_stat_error;
8607 if (!equal)
8608 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8609 "domain and range don't match", return isl_stat_error);
8610 return isl_stat_ok;
8614 * returns range - domain
8616 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8618 isl_space *target_space;
8619 struct isl_basic_set *bset;
8620 isl_size dim;
8621 isl_size nparam;
8622 isl_size total;
8623 int i;
8625 if (isl_basic_map_check_transformation(bmap) < 0)
8626 return isl_basic_map_free(bmap);
8627 dim = isl_basic_map_dim(bmap, isl_dim_in);
8628 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8629 if (dim < 0 || nparam < 0)
8630 goto error;
8631 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8632 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8633 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8634 total = isl_basic_map_dim(bmap, isl_dim_all);
8635 if (total < 0)
8636 bmap = isl_basic_map_free(bmap);
8637 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8638 for (i = 0; i < dim; ++i) {
8639 int j = isl_basic_map_alloc_equality(bmap);
8640 if (j < 0) {
8641 bmap = isl_basic_map_free(bmap);
8642 break;
8644 isl_seq_clr(bmap->eq[j], 1 + total);
8645 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8646 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8647 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8649 bset = isl_basic_map_domain(bmap);
8650 bset = isl_basic_set_reset_space(bset, target_space);
8651 return bset;
8652 error:
8653 isl_basic_map_free(bmap);
8654 return NULL;
8657 /* Is the tuple of type "type1" of "map" the same as
8658 * the tuple of type "type2" of "space"?
8660 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8661 enum isl_dim_type type1, __isl_keep isl_space *space,
8662 enum isl_dim_type type2)
8664 isl_space *map_space;
8666 map_space = isl_map_peek_space(map);
8667 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8670 /* Is the tuple of type "type1" of "map1" the same as
8671 * the tuple of type "type2" of "map2"?
8673 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8674 enum isl_dim_type type1, __isl_keep isl_map *map2,
8675 enum isl_dim_type type2)
8677 isl_space *space1, *space2;
8679 space1 = isl_map_peek_space(map1);
8680 space2 = isl_map_peek_space(map2);
8681 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8684 /* Is the space of "obj" equal to "space", ignoring parameters?
8686 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8687 __isl_keep isl_space *space)
8689 isl_space *map_space;
8691 map_space = isl_map_peek_space(map);
8692 return isl_space_has_equal_tuples(map_space, space);
8695 /* Check that "map" is a transformation, i.e.,
8696 * that it relates elements from the same space.
8698 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8700 isl_bool equal;
8702 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8703 if (equal < 0)
8704 return isl_stat_error;
8705 if (!equal)
8706 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8707 "domain and range don't match", return isl_stat_error);
8708 return isl_stat_ok;
8712 * returns range - domain
8714 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8716 int i;
8717 isl_space *space;
8718 struct isl_set *result;
8720 if (isl_map_check_transformation(map) < 0)
8721 goto error;
8722 space = isl_map_get_space(map);
8723 space = isl_space_domain(space);
8724 result = isl_set_alloc_space(space, map->n, 0);
8725 if (!result)
8726 goto error;
8727 for (i = 0; i < map->n; ++i)
8728 result = isl_set_add_basic_set(result,
8729 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8730 isl_map_free(map);
8731 return result;
8732 error:
8733 isl_map_free(map);
8734 return NULL;
8738 * returns [domain -> range] -> range - domain
8740 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8741 __isl_take isl_basic_map *bmap)
8743 int i, k;
8744 isl_space *space;
8745 isl_basic_map *domain;
8746 isl_size nparam, n;
8747 isl_size total;
8749 if (isl_basic_map_check_transformation(bmap) < 0)
8750 return isl_basic_map_free(bmap);
8752 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8753 n = isl_basic_map_dim(bmap, isl_dim_in);
8754 if (nparam < 0 || n < 0)
8755 return isl_basic_map_free(bmap);
8757 space = isl_basic_map_get_space(bmap);
8758 space = isl_space_from_range(isl_space_domain(space));
8759 domain = isl_basic_map_universe(space);
8761 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8762 bmap = isl_basic_map_apply_range(bmap, domain);
8763 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8765 total = isl_basic_map_dim(bmap, isl_dim_all);
8766 if (total < 0)
8767 return isl_basic_map_free(bmap);
8769 for (i = 0; i < n; ++i) {
8770 k = isl_basic_map_alloc_equality(bmap);
8771 if (k < 0)
8772 goto error;
8773 isl_seq_clr(bmap->eq[k], 1 + total);
8774 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8775 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8776 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8779 bmap = isl_basic_map_gauss(bmap, NULL);
8780 return isl_basic_map_finalize(bmap);
8781 error:
8782 isl_basic_map_free(bmap);
8783 return NULL;
8787 * returns [domain -> range] -> range - domain
8789 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8791 if (isl_map_check_transformation(map) < 0)
8792 return isl_map_free(map);
8794 return isl_map_transform(map, &isl_space_range_map,
8795 &isl_basic_map_deltas_map);
8798 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8800 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8802 isl_space *space;
8803 isl_map *map;
8805 space = isl_space_map_from_set(isl_set_get_space(deltas));
8806 map = isl_map_deltas_map(isl_map_universe(space));
8807 map = isl_map_intersect_range(map, deltas);
8809 return isl_set_unwrap(isl_map_domain(map));
8812 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8814 isl_size n_in, n_out;
8816 n_in = isl_space_dim(space, isl_dim_in);
8817 n_out = isl_space_dim(space, isl_dim_out);
8818 if (n_in < 0 || n_out < 0)
8819 goto error;
8820 if (n_in != n_out)
8821 isl_die(space->ctx, isl_error_invalid,
8822 "number of input and output dimensions needs to be "
8823 "the same", goto error);
8824 return isl_basic_map_equal(space, n_in);
8825 error:
8826 isl_space_free(space);
8827 return NULL;
8830 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8832 return isl_map_from_basic_map(isl_basic_map_identity(space));
8835 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8837 isl_space *space = isl_set_get_space(set);
8838 isl_map *id;
8839 id = isl_map_identity(isl_space_map_from_set(space));
8840 return isl_map_intersect_range(id, set);
8843 /* Construct a basic set with all set dimensions having only non-negative
8844 * values.
8846 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8847 __isl_take isl_space *space)
8849 int i;
8850 isl_size nparam;
8851 isl_size dim;
8852 isl_size total;
8853 struct isl_basic_set *bset;
8855 nparam = isl_space_dim(space, isl_dim_param);
8856 dim = isl_space_dim(space, isl_dim_set);
8857 total = isl_space_dim(space, isl_dim_all);
8858 if (nparam < 0 || dim < 0 || total < 0)
8859 space = isl_space_free(space);
8860 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8861 if (!bset)
8862 return NULL;
8863 for (i = 0; i < dim; ++i) {
8864 int k = isl_basic_set_alloc_inequality(bset);
8865 if (k < 0)
8866 goto error;
8867 isl_seq_clr(bset->ineq[k], 1 + total);
8868 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8870 return bset;
8871 error:
8872 isl_basic_set_free(bset);
8873 return NULL;
8876 /* Construct the half-space x_pos >= 0.
8878 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8879 int pos)
8881 int k;
8882 isl_size total;
8883 isl_basic_set *nonneg;
8885 total = isl_space_dim(space, isl_dim_all);
8886 if (total < 0)
8887 space = isl_space_free(space);
8888 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8889 k = isl_basic_set_alloc_inequality(nonneg);
8890 if (k < 0)
8891 goto error;
8892 isl_seq_clr(nonneg->ineq[k], 1 + total);
8893 isl_int_set_si(nonneg->ineq[k][pos], 1);
8895 return isl_basic_set_finalize(nonneg);
8896 error:
8897 isl_basic_set_free(nonneg);
8898 return NULL;
8901 /* Construct the half-space x_pos <= -1.
8903 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8904 int pos)
8906 int k;
8907 isl_size total;
8908 isl_basic_set *neg;
8910 total = isl_space_dim(space, isl_dim_all);
8911 if (total < 0)
8912 space = isl_space_free(space);
8913 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8914 k = isl_basic_set_alloc_inequality(neg);
8915 if (k < 0)
8916 goto error;
8917 isl_seq_clr(neg->ineq[k], 1 + total);
8918 isl_int_set_si(neg->ineq[k][0], -1);
8919 isl_int_set_si(neg->ineq[k][pos], -1);
8921 return isl_basic_set_finalize(neg);
8922 error:
8923 isl_basic_set_free(neg);
8924 return NULL;
8927 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8928 enum isl_dim_type type, unsigned first, unsigned n)
8930 int i;
8931 unsigned offset;
8932 isl_basic_set *nonneg;
8933 isl_basic_set *neg;
8935 if (n == 0)
8936 return set;
8938 if (isl_set_check_range(set, type, first, n) < 0)
8939 return isl_set_free(set);
8941 offset = pos(set->dim, type);
8942 for (i = 0; i < n; ++i) {
8943 nonneg = nonneg_halfspace(isl_set_get_space(set),
8944 offset + first + i);
8945 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8947 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8950 return set;
8953 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8954 int len,
8955 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8956 void *user)
8958 isl_set *half;
8960 if (!set)
8961 return isl_stat_error;
8962 if (isl_set_plain_is_empty(set)) {
8963 isl_set_free(set);
8964 return isl_stat_ok;
8966 if (first == len)
8967 return fn(set, signs, user);
8969 signs[first] = 1;
8970 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8971 1 + first));
8972 half = isl_set_intersect(half, isl_set_copy(set));
8973 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8974 goto error;
8976 signs[first] = -1;
8977 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8978 1 + first));
8979 half = isl_set_intersect(half, set);
8980 return foreach_orthant(half, signs, first + 1, len, fn, user);
8981 error:
8982 isl_set_free(set);
8983 return isl_stat_error;
8986 /* Call "fn" on the intersections of "set" with each of the orthants
8987 * (except for obviously empty intersections). The orthant is identified
8988 * by the signs array, with each entry having value 1 or -1 according
8989 * to the sign of the corresponding variable.
8991 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8992 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8993 void *user)
8995 isl_size nparam;
8996 isl_size nvar;
8997 int *signs;
8998 isl_stat r;
9000 if (!set)
9001 return isl_stat_error;
9002 if (isl_set_plain_is_empty(set))
9003 return isl_stat_ok;
9005 nparam = isl_set_dim(set, isl_dim_param);
9006 nvar = isl_set_dim(set, isl_dim_set);
9007 if (nparam < 0 || nvar < 0)
9008 return isl_stat_error;
9010 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9012 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9013 fn, user);
9015 free(signs);
9017 return r;
9020 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9022 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9025 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9026 __isl_keep isl_basic_map *bmap2)
9028 isl_bool is_subset;
9029 struct isl_map *map1;
9030 struct isl_map *map2;
9032 if (!bmap1 || !bmap2)
9033 return isl_bool_error;
9035 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9036 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9038 is_subset = isl_map_is_subset(map1, map2);
9040 isl_map_free(map1);
9041 isl_map_free(map2);
9043 return is_subset;
9046 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9047 __isl_keep isl_basic_set *bset2)
9049 return isl_basic_map_is_subset(bset1, bset2);
9052 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9053 __isl_keep isl_basic_map *bmap2)
9055 isl_bool is_subset;
9057 if (!bmap1 || !bmap2)
9058 return isl_bool_error;
9059 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9060 if (is_subset != isl_bool_true)
9061 return is_subset;
9062 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9063 return is_subset;
9066 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9067 __isl_keep isl_basic_set *bset2)
9069 return isl_basic_map_is_equal(
9070 bset_to_bmap(bset1), bset_to_bmap(bset2));
9073 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9075 int i;
9076 int is_empty;
9078 if (!map)
9079 return isl_bool_error;
9080 for (i = 0; i < map->n; ++i) {
9081 is_empty = isl_basic_map_is_empty(map->p[i]);
9082 if (is_empty < 0)
9083 return isl_bool_error;
9084 if (!is_empty)
9085 return isl_bool_false;
9087 return isl_bool_true;
9090 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9092 return map ? map->n == 0 : isl_bool_error;
9095 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9097 return set ? set->n == 0 : isl_bool_error;
9100 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9102 return isl_map_is_empty(set_to_map(set));
9105 #undef TYPE
9106 #define TYPE isl_basic_map
9108 static
9109 #include "isl_type_has_equal_space_bin_templ.c"
9110 #include "isl_type_check_equal_space_templ.c"
9112 /* Check that "bset1" and "bset2" live in the same space,
9113 * reporting an error if they do not.
9115 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9116 __isl_keep isl_basic_set *bset2)
9118 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9119 bset_to_bmap(bset1));
9122 #undef TYPE
9123 #define TYPE isl_map
9125 #include "isl_type_has_equal_space_bin_templ.c"
9126 #include "isl_type_check_equal_space_templ.c"
9127 #include "isl_type_has_space_templ.c"
9129 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9130 __isl_keep isl_set *set2)
9132 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9135 #undef TYPE1
9136 #define TYPE1 isl_map
9137 #undef TYPE2
9138 #define TYPE2 isl_basic_map
9139 #undef TYPE_PAIR
9140 #define TYPE_PAIR isl_map_basic_map
9142 static
9143 #include "isl_type_has_equal_space_templ.c"
9144 #include "isl_type_check_equal_space_templ.c"
9146 /* Check that "set" and "bset" live in the same space,
9147 * reporting an error if they do not.
9149 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9150 __isl_keep isl_basic_set *bset)
9152 return isl_map_basic_map_check_equal_space(set_to_map(set),
9153 bset_to_bmap(bset));
9156 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9158 isl_bool is_subset;
9160 if (!map1 || !map2)
9161 return isl_bool_error;
9162 is_subset = isl_map_is_subset(map1, map2);
9163 if (is_subset != isl_bool_true)
9164 return is_subset;
9165 is_subset = isl_map_is_subset(map2, map1);
9166 return is_subset;
9169 /* Is "map1" equal to "map2"?
9171 * First check if they are obviously equal.
9172 * If not, then perform a more detailed analysis.
9174 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9176 isl_bool equal;
9178 equal = isl_map_plain_is_equal(map1, map2);
9179 if (equal < 0 || equal)
9180 return equal;
9181 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9184 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9185 __isl_keep isl_basic_map *bmap2)
9187 isl_bool is_subset;
9189 if (!bmap1 || !bmap2)
9190 return isl_bool_error;
9191 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9192 if (is_subset != isl_bool_true)
9193 return is_subset;
9194 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9195 return isl_bool_not(is_subset);
9198 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9199 __isl_keep isl_map *map2)
9201 isl_bool is_subset;
9203 if (!map1 || !map2)
9204 return isl_bool_error;
9205 is_subset = isl_map_is_subset(map1, map2);
9206 if (is_subset != isl_bool_true)
9207 return is_subset;
9208 is_subset = isl_map_is_subset(map2, map1);
9209 return isl_bool_not(is_subset);
9212 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9213 __isl_keep isl_set *set2)
9215 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9218 /* Is "bmap" obviously equal to the universe with the same space?
9220 * That is, does it not have any constraints?
9222 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9224 if (!bmap)
9225 return isl_bool_error;
9226 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9229 /* Is "bset" obviously equal to the universe with the same space?
9231 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9233 return isl_basic_map_plain_is_universe(bset);
9236 /* If "c" does not involve any existentially quantified variables,
9237 * then set *univ to false and abort
9239 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9241 isl_bool *univ = user;
9242 isl_size n;
9244 n = isl_constraint_dim(c, isl_dim_div);
9245 if (n < 0)
9246 c = isl_constraint_free(c);
9247 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9248 isl_constraint_free(c);
9249 if (*univ < 0 || !*univ)
9250 return isl_stat_error;
9251 return isl_stat_ok;
9254 /* Is "bmap" equal to the universe with the same space?
9256 * First check if it is obviously equal to the universe.
9257 * If not and if there are any constraints not involving
9258 * existentially quantified variables, then it is certainly
9259 * not equal to the universe.
9260 * Otherwise, check if the universe is a subset of "bmap".
9262 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9264 isl_size n_div;
9265 isl_bool univ;
9266 isl_basic_map *test;
9268 univ = isl_basic_map_plain_is_universe(bmap);
9269 if (univ < 0 || univ)
9270 return univ;
9271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9272 if (n_div < 0)
9273 return isl_bool_error;
9274 if (n_div == 0)
9275 return isl_bool_false;
9276 univ = isl_bool_true;
9277 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9278 univ)
9279 return isl_bool_error;
9280 if (univ < 0 || !univ)
9281 return univ;
9282 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9283 univ = isl_basic_map_is_subset(test, bmap);
9284 isl_basic_map_free(test);
9285 return univ;
9288 /* Is "bset" equal to the universe with the same space?
9290 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9292 return isl_basic_map_is_universe(bset);
9295 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9297 int i;
9299 if (!map)
9300 return isl_bool_error;
9302 for (i = 0; i < map->n; ++i) {
9303 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9304 if (r < 0 || r)
9305 return r;
9308 return isl_bool_false;
9311 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9313 return isl_map_plain_is_universe(set_to_map(set));
9316 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9318 struct isl_basic_set *bset = NULL;
9319 struct isl_vec *sample = NULL;
9320 isl_bool empty, non_empty;
9322 if (!bmap)
9323 return isl_bool_error;
9325 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9326 return isl_bool_true;
9328 if (isl_basic_map_plain_is_universe(bmap))
9329 return isl_bool_false;
9331 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9332 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9333 copy = isl_basic_map_remove_redundancies(copy);
9334 empty = isl_basic_map_plain_is_empty(copy);
9335 isl_basic_map_free(copy);
9336 return empty;
9339 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9340 if (non_empty < 0)
9341 return isl_bool_error;
9342 if (non_empty)
9343 return isl_bool_false;
9344 isl_vec_free(bmap->sample);
9345 bmap->sample = NULL;
9346 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9347 if (!bset)
9348 return isl_bool_error;
9349 sample = isl_basic_set_sample_vec(bset);
9350 if (!sample)
9351 return isl_bool_error;
9352 empty = sample->size == 0;
9353 isl_vec_free(bmap->sample);
9354 bmap->sample = sample;
9355 if (empty)
9356 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9358 return empty;
9361 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9363 if (!bmap)
9364 return isl_bool_error;
9365 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9368 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9370 if (!bset)
9371 return isl_bool_error;
9372 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9375 /* Is "bmap" known to be non-empty?
9377 * That is, is the cached sample still valid?
9379 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9381 isl_size total;
9383 if (!bmap)
9384 return isl_bool_error;
9385 if (!bmap->sample)
9386 return isl_bool_false;
9387 total = isl_basic_map_dim(bmap, isl_dim_all);
9388 if (total < 0)
9389 return isl_bool_error;
9390 if (bmap->sample->size != 1 + total)
9391 return isl_bool_false;
9392 return isl_basic_map_contains(bmap, bmap->sample);
9395 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9397 return isl_basic_map_is_empty(bset_to_bmap(bset));
9400 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9401 __isl_take isl_basic_map *bmap2)
9403 struct isl_map *map;
9405 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9406 goto error;
9408 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9409 if (!map)
9410 goto error;
9411 map = isl_map_add_basic_map(map, bmap1);
9412 map = isl_map_add_basic_map(map, bmap2);
9413 return map;
9414 error:
9415 isl_basic_map_free(bmap1);
9416 isl_basic_map_free(bmap2);
9417 return NULL;
9420 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9421 __isl_take isl_basic_set *bset2)
9423 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9424 bset_to_bmap(bset2)));
9427 /* Order divs such that any div only depends on previous divs */
9428 __isl_give isl_basic_map *isl_basic_map_order_divs(
9429 __isl_take isl_basic_map *bmap)
9431 int i;
9432 isl_size off;
9434 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9435 if (off < 0)
9436 return isl_basic_map_free(bmap);
9438 for (i = 0; i < bmap->n_div; ++i) {
9439 int pos;
9440 if (isl_int_is_zero(bmap->div[i][0]))
9441 continue;
9442 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9443 bmap->n_div-i);
9444 if (pos == -1)
9445 continue;
9446 if (pos == 0)
9447 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9448 "integer division depends on itself",
9449 return isl_basic_map_free(bmap));
9450 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9451 if (!bmap)
9452 return NULL;
9453 --i;
9455 return bmap;
9458 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9460 int i;
9462 if (!map)
9463 return 0;
9465 for (i = 0; i < map->n; ++i) {
9466 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9467 if (!map->p[i])
9468 goto error;
9471 return map;
9472 error:
9473 isl_map_free(map);
9474 return NULL;
9477 /* Sort the local variables of "bset".
9479 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9480 __isl_take isl_basic_set *bset)
9482 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9485 /* Apply the expansion computed by isl_merge_divs.
9486 * The expansion itself is given by "exp" while the resulting
9487 * list of divs is given by "div".
9489 * Move the integer divisions of "bmap" into the right position
9490 * according to "exp" and then introduce the additional integer
9491 * divisions, adding div constraints.
9492 * The moving should be done first to avoid moving coefficients
9493 * in the definitions of the extra integer divisions.
9495 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9496 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9498 int i, j;
9499 int n_div;
9501 bmap = isl_basic_map_cow(bmap);
9502 if (!bmap || !div)
9503 goto error;
9505 if (div->n_row < bmap->n_div)
9506 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9507 "not an expansion", goto error);
9509 n_div = bmap->n_div;
9510 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9511 2 * (div->n_row - n_div));
9513 for (i = n_div; i < div->n_row; ++i)
9514 if (isl_basic_map_alloc_div(bmap) < 0)
9515 goto error;
9517 for (j = n_div - 1; j >= 0; --j) {
9518 if (exp[j] == j)
9519 break;
9520 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9521 if (!bmap)
9522 goto error;
9524 j = 0;
9525 for (i = 0; i < div->n_row; ++i) {
9526 if (j < n_div && exp[j] == i) {
9527 j++;
9528 } else {
9529 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9530 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9531 continue;
9532 bmap = isl_basic_map_add_div_constraints(bmap, i);
9533 if (!bmap)
9534 goto error;
9538 isl_mat_free(div);
9539 return bmap;
9540 error:
9541 isl_basic_map_free(bmap);
9542 isl_mat_free(div);
9543 return NULL;
9546 /* Apply the expansion computed by isl_merge_divs.
9547 * The expansion itself is given by "exp" while the resulting
9548 * list of divs is given by "div".
9550 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9551 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9553 return isl_basic_map_expand_divs(bset, div, exp);
9556 /* Look for a div in dst that corresponds to the div "div" in src.
9557 * The divs before "div" in src and dst are assumed to be the same.
9559 * Return the position of the corresponding div in dst
9560 * if there is one. Otherwise, return a position beyond the integer divisions.
9561 * Return -1 on error.
9563 static int find_div(__isl_keep isl_basic_map *dst,
9564 __isl_keep isl_basic_map *src, unsigned div)
9566 int i;
9567 isl_size n_div;
9568 isl_size v_div;
9570 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9571 n_div = isl_basic_map_dim(dst, isl_dim_div);
9572 if (n_div < 0 || v_div < 0)
9573 return -1;
9574 isl_assert(dst->ctx, div <= n_div, return -1);
9575 for (i = div; i < n_div; ++i)
9576 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9577 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9578 n_div - div) == -1)
9579 return i;
9580 return n_div;
9583 /* Align the divs of "dst" to those of "src", adding divs from "src"
9584 * if needed. That is, make sure that the first src->n_div divs
9585 * of the result are equal to those of src.
9586 * The integer division of "src" are assumed to be ordered.
9588 * The integer divisions are swapped into the right position
9589 * (possibly after adding them first). This may result
9590 * in the remaining integer divisions appearing in the wrong order,
9591 * i.e., with some integer division appearing before
9592 * some other integer division on which it depends.
9593 * The integer divisions therefore need to be ordered.
9594 * This will not affect the integer divisions aligned to those of "src",
9595 * since "src" is assumed to have ordered integer divisions.
9597 * The result is not finalized as by design it will have redundant
9598 * divs if any divs from "src" were copied.
9600 __isl_give isl_basic_map *isl_basic_map_align_divs(
9601 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9603 int i;
9604 isl_bool known;
9605 int extended;
9606 isl_size v_div;
9607 isl_size dst_n_div;
9609 if (!dst || !src)
9610 return isl_basic_map_free(dst);
9612 if (src->n_div == 0)
9613 return dst;
9615 known = isl_basic_map_divs_known(src);
9616 if (known < 0)
9617 return isl_basic_map_free(dst);
9618 if (!known)
9619 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9620 "some src divs are unknown",
9621 return isl_basic_map_free(dst));
9623 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9624 if (v_div < 0)
9625 return isl_basic_map_free(dst);
9627 extended = 0;
9628 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9629 if (dst_n_div < 0)
9630 dst = isl_basic_map_free(dst);
9631 for (i = 0; i < src->n_div; ++i) {
9632 int j = find_div(dst, src, i);
9633 if (j < 0)
9634 dst = isl_basic_map_free(dst);
9635 if (j == dst_n_div) {
9636 if (!extended) {
9637 int extra = src->n_div - i;
9638 dst = isl_basic_map_cow(dst);
9639 if (!dst)
9640 return isl_basic_map_free(dst);
9641 dst = isl_basic_map_extend(dst,
9642 extra, 0, 2 * extra);
9643 extended = 1;
9645 j = isl_basic_map_alloc_div(dst);
9646 if (j < 0)
9647 return isl_basic_map_free(dst);
9648 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9649 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9650 dst_n_div++;
9651 dst = isl_basic_map_add_div_constraints(dst, j);
9652 if (!dst)
9653 return isl_basic_map_free(dst);
9655 if (j != i)
9656 dst = isl_basic_map_swap_div(dst, i, j);
9657 if (!dst)
9658 return isl_basic_map_free(dst);
9660 return isl_basic_map_order_divs(dst);
9663 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9665 int i;
9667 if (!map)
9668 return NULL;
9669 if (map->n == 0)
9670 return map;
9671 map = isl_map_compute_divs(map);
9672 map = isl_map_order_divs(map);
9673 map = isl_map_cow(map);
9674 if (!map)
9675 return NULL;
9677 for (i = 1; i < map->n; ++i)
9678 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9679 for (i = 1; i < map->n; ++i) {
9680 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9681 if (!map->p[i])
9682 return isl_map_free(map);
9685 map = isl_map_unmark_normalized(map);
9686 return map;
9689 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9691 return isl_map_align_divs_internal(map);
9694 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9696 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9699 /* Align the divs of the basic maps in "map" to those
9700 * of the basic maps in "list", as well as to the other basic maps in "map".
9701 * The elements in "list" are assumed to have known divs.
9703 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9704 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9706 int i;
9707 isl_size n;
9709 n = isl_basic_map_list_n_basic_map(list);
9710 map = isl_map_compute_divs(map);
9711 map = isl_map_cow(map);
9712 if (!map || n < 0)
9713 return isl_map_free(map);
9714 if (map->n == 0)
9715 return map;
9717 for (i = 0; i < n; ++i) {
9718 isl_basic_map *bmap;
9720 bmap = isl_basic_map_list_get_basic_map(list, i);
9721 bmap = isl_basic_map_order_divs(bmap);
9722 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9723 isl_basic_map_free(bmap);
9725 if (!map->p[0])
9726 return isl_map_free(map);
9728 return isl_map_align_divs_internal(map);
9731 /* Align the divs of each element of "list" to those of "bmap".
9732 * Both "bmap" and the elements of "list" are assumed to have known divs.
9734 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9735 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9737 int i;
9738 isl_size n;
9740 n = isl_basic_map_list_n_basic_map(list);
9741 if (n < 0 || !bmap)
9742 return isl_basic_map_list_free(list);
9744 for (i = 0; i < n; ++i) {
9745 isl_basic_map *bmap_i;
9747 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9748 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9749 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9752 return list;
9755 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9756 __isl_take isl_map *map)
9758 isl_bool ok;
9760 isl_map_align_params_set(&map, &set);
9761 ok = isl_map_compatible_domain(map, set);
9762 if (ok < 0)
9763 goto error;
9764 if (!ok)
9765 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9766 "incompatible spaces", goto error);
9767 map = isl_map_intersect_domain(map, set);
9768 set = isl_map_range(map);
9769 return set;
9770 error:
9771 isl_set_free(set);
9772 isl_map_free(map);
9773 return NULL;
9776 /* There is no need to cow as removing empty parts doesn't change
9777 * the meaning of the set.
9779 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9781 int i;
9783 if (!map)
9784 return NULL;
9786 for (i = map->n - 1; i >= 0; --i)
9787 map = remove_if_empty(map, i);
9789 return map;
9792 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9794 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9797 /* Create a binary relation that maps the shared initial "pos" dimensions
9798 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9800 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9801 __isl_keep isl_basic_set *bset2, int pos)
9803 isl_basic_map *bmap1;
9804 isl_basic_map *bmap2;
9806 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9807 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9808 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9809 isl_dim_out, 0, pos);
9810 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9811 isl_dim_out, 0, pos);
9812 return isl_basic_map_range_product(bmap1, bmap2);
9815 /* Given two basic sets bset1 and bset2, compute the maximal difference
9816 * between the values of dimension pos in bset1 and those in bset2
9817 * for any common value of the parameters and dimensions preceding pos.
9819 static enum isl_lp_result basic_set_maximal_difference_at(
9820 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9821 int pos, isl_int *opt)
9823 isl_basic_map *bmap1;
9824 struct isl_ctx *ctx;
9825 struct isl_vec *obj;
9826 isl_size total;
9827 isl_size nparam;
9828 isl_size dim1;
9829 enum isl_lp_result res;
9831 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9832 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9833 if (nparam < 0 || dim1 < 0 || !bset2)
9834 return isl_lp_error;
9836 bmap1 = join_initial(bset1, bset2, pos);
9837 total = isl_basic_map_dim(bmap1, isl_dim_all);
9838 if (total < 0)
9839 return isl_lp_error;
9841 ctx = bmap1->ctx;
9842 obj = isl_vec_alloc(ctx, 1 + total);
9843 if (!obj)
9844 goto error;
9845 isl_seq_clr(obj->block.data, 1 + total);
9846 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9847 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9848 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9849 opt, NULL, NULL);
9850 isl_basic_map_free(bmap1);
9851 isl_vec_free(obj);
9852 return res;
9853 error:
9854 isl_basic_map_free(bmap1);
9855 return isl_lp_error;
9858 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9859 * for any common value of the parameters and dimensions preceding pos
9860 * in both basic sets, the values of dimension pos in bset1 are
9861 * smaller or larger than those in bset2.
9863 * Returns
9864 * 1 if bset1 follows bset2
9865 * -1 if bset1 precedes bset2
9866 * 0 if bset1 and bset2 are incomparable
9867 * -2 if some error occurred.
9869 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9870 __isl_keep isl_basic_set *bset2, int pos)
9872 isl_int opt;
9873 enum isl_lp_result res;
9874 int cmp;
9876 isl_int_init(opt);
9878 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9880 if (res == isl_lp_empty)
9881 cmp = 0;
9882 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9883 res == isl_lp_unbounded)
9884 cmp = 1;
9885 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9886 cmp = -1;
9887 else
9888 cmp = -2;
9890 isl_int_clear(opt);
9891 return cmp;
9894 /* Given two basic sets bset1 and bset2, check whether
9895 * for any common value of the parameters and dimensions preceding pos
9896 * there is a value of dimension pos in bset1 that is larger
9897 * than a value of the same dimension in bset2.
9899 * Return
9900 * 1 if there exists such a pair
9901 * 0 if there is no such pair, but there is a pair of equal values
9902 * -1 otherwise
9903 * -2 if some error occurred.
9905 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9906 __isl_keep isl_basic_set *bset2, int pos)
9908 isl_bool empty;
9909 isl_basic_map *bmap;
9910 isl_size dim1;
9912 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9913 if (dim1 < 0)
9914 return -2;
9915 bmap = join_initial(bset1, bset2, pos);
9916 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9917 isl_dim_out, dim1 - pos);
9918 empty = isl_basic_map_is_empty(bmap);
9919 if (empty < 0)
9920 goto error;
9921 if (empty) {
9922 isl_basic_map_free(bmap);
9923 return -1;
9925 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9926 isl_dim_out, dim1 - pos);
9927 empty = isl_basic_map_is_empty(bmap);
9928 if (empty < 0)
9929 goto error;
9930 isl_basic_map_free(bmap);
9931 if (empty)
9932 return 0;
9933 return 1;
9934 error:
9935 isl_basic_map_free(bmap);
9936 return -2;
9939 /* Given two sets set1 and set2, check whether
9940 * for any common value of the parameters and dimensions preceding pos
9941 * there is a value of dimension pos in set1 that is larger
9942 * than a value of the same dimension in set2.
9944 * Return
9945 * 1 if there exists such a pair
9946 * 0 if there is no such pair, but there is a pair of equal values
9947 * -1 otherwise
9948 * -2 if some error occurred.
9950 int isl_set_follows_at(__isl_keep isl_set *set1,
9951 __isl_keep isl_set *set2, int pos)
9953 int i, j;
9954 int follows = -1;
9956 if (!set1 || !set2)
9957 return -2;
9959 for (i = 0; i < set1->n; ++i)
9960 for (j = 0; j < set2->n; ++j) {
9961 int f;
9962 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9963 if (f == 1 || f == -2)
9964 return f;
9965 if (f > follows)
9966 follows = f;
9969 return follows;
9972 static isl_bool isl_basic_map_plain_has_fixed_var(
9973 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9975 int i;
9976 int d;
9977 isl_size total;
9979 total = isl_basic_map_dim(bmap, isl_dim_all);
9980 if (total < 0)
9981 return isl_bool_error;
9982 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9983 for (; d+1 > pos; --d)
9984 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9985 break;
9986 if (d != pos)
9987 continue;
9988 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9989 return isl_bool_false;
9990 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9991 return isl_bool_false;
9992 if (!isl_int_is_one(bmap->eq[i][1+d]))
9993 return isl_bool_false;
9994 if (val)
9995 isl_int_neg(*val, bmap->eq[i][0]);
9996 return isl_bool_true;
9998 return isl_bool_false;
10001 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10002 unsigned pos, isl_int *val)
10004 int i;
10005 isl_int v;
10006 isl_int tmp;
10007 isl_bool fixed;
10009 if (!map)
10010 return isl_bool_error;
10011 if (map->n == 0)
10012 return isl_bool_false;
10013 if (map->n == 1)
10014 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10015 isl_int_init(v);
10016 isl_int_init(tmp);
10017 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10018 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10019 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10020 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10021 fixed = isl_bool_false;
10023 if (val)
10024 isl_int_set(*val, v);
10025 isl_int_clear(tmp);
10026 isl_int_clear(v);
10027 return fixed;
10030 static isl_bool isl_basic_set_plain_has_fixed_var(
10031 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10033 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10034 pos, val);
10037 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10038 enum isl_dim_type type, unsigned pos, isl_int *val)
10040 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10041 return isl_bool_error;
10042 return isl_basic_map_plain_has_fixed_var(bmap,
10043 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10046 /* If "bmap" obviously lies on a hyperplane where the given dimension
10047 * has a fixed value, then return that value.
10048 * Otherwise return NaN.
10050 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10051 __isl_keep isl_basic_map *bmap,
10052 enum isl_dim_type type, unsigned pos)
10054 isl_ctx *ctx;
10055 isl_val *v;
10056 isl_bool fixed;
10058 if (!bmap)
10059 return NULL;
10060 ctx = isl_basic_map_get_ctx(bmap);
10061 v = isl_val_alloc(ctx);
10062 if (!v)
10063 return NULL;
10064 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10065 if (fixed < 0)
10066 return isl_val_free(v);
10067 if (fixed) {
10068 isl_int_set_si(v->d, 1);
10069 return v;
10071 isl_val_free(v);
10072 return isl_val_nan(ctx);
10075 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10076 enum isl_dim_type type, unsigned pos, isl_int *val)
10078 if (isl_map_check_range(map, type, pos, 1) < 0)
10079 return isl_bool_error;
10080 return isl_map_plain_has_fixed_var(map,
10081 map_offset(map, type) - 1 + pos, val);
10084 /* If "map" obviously lies on a hyperplane where the given dimension
10085 * has a fixed value, then return that value.
10086 * Otherwise return NaN.
10088 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10089 enum isl_dim_type type, unsigned pos)
10091 isl_ctx *ctx;
10092 isl_val *v;
10093 isl_bool fixed;
10095 if (!map)
10096 return NULL;
10097 ctx = isl_map_get_ctx(map);
10098 v = isl_val_alloc(ctx);
10099 if (!v)
10100 return NULL;
10101 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10102 if (fixed < 0)
10103 return isl_val_free(v);
10104 if (fixed) {
10105 isl_int_set_si(v->d, 1);
10106 return v;
10108 isl_val_free(v);
10109 return isl_val_nan(ctx);
10112 /* If "set" obviously lies on a hyperplane where the given dimension
10113 * has a fixed value, then return that value.
10114 * Otherwise return NaN.
10116 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10117 enum isl_dim_type type, unsigned pos)
10119 return isl_map_plain_get_val_if_fixed(set, type, pos);
10122 /* Return a sequence of values in the same space as "set"
10123 * that are equal to the corresponding set dimensions of "set"
10124 * for those set dimensions that obviously lie on a hyperplane
10125 * where the dimension has a fixed value.
10126 * The other elements are set to NaN.
10128 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10129 __isl_keep isl_set *set)
10131 int i;
10132 isl_size n;
10133 isl_space *space;
10134 isl_multi_val *mv;
10136 space = isl_space_drop_all_params(isl_set_get_space(set));
10137 mv = isl_multi_val_alloc(space);
10138 n = isl_multi_val_size(mv);
10139 if (n < 0)
10140 return isl_multi_val_free(mv);
10142 for (i = 0; i < n; ++i) {
10143 isl_val *v;
10145 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10146 mv = isl_multi_val_set_val(mv, i, v);
10149 return mv;
10152 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10153 * then return this fixed value in *val.
10155 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10156 unsigned dim, isl_int *val)
10158 isl_size nparam;
10160 nparam = isl_basic_set_dim(bset, isl_dim_param);
10161 if (nparam < 0)
10162 return isl_bool_error;
10163 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10166 /* Return -1 if the constraint "c1" should be sorted before "c2"
10167 * and 1 if it should be sorted after "c2".
10168 * Return 0 if the two constraints are the same (up to the constant term).
10170 * In particular, if a constraint involves later variables than another
10171 * then it is sorted after this other constraint.
10172 * uset_gist depends on constraints without existentially quantified
10173 * variables sorting first.
10175 * For constraints that have the same latest variable, those
10176 * with the same coefficient for this latest variable (first in absolute value
10177 * and then in actual value) are grouped together.
10178 * This is useful for detecting pairs of constraints that can
10179 * be chained in their printed representation.
10181 * Finally, within a group, constraints are sorted according to
10182 * their coefficients (excluding the constant term).
10184 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10186 isl_int **c1 = (isl_int **) p1;
10187 isl_int **c2 = (isl_int **) p2;
10188 int l1, l2;
10189 unsigned size = *(unsigned *) arg;
10190 int cmp;
10192 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10193 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10195 if (l1 != l2)
10196 return l1 - l2;
10198 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10199 if (cmp != 0)
10200 return cmp;
10201 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10202 if (cmp != 0)
10203 return -cmp;
10205 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10208 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10209 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10210 * and 0 if the two constraints are the same (up to the constant term).
10212 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10213 isl_int *c1, isl_int *c2)
10215 isl_size total;
10216 unsigned size;
10218 total = isl_basic_map_dim(bmap, isl_dim_all);
10219 if (total < 0)
10220 return -2;
10221 size = total;
10222 return sort_constraint_cmp(&c1, &c2, &size);
10225 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10226 __isl_take isl_basic_map *bmap)
10228 isl_size total;
10229 unsigned size;
10231 if (!bmap)
10232 return NULL;
10233 if (bmap->n_ineq == 0)
10234 return bmap;
10235 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10236 return bmap;
10237 total = isl_basic_map_dim(bmap, isl_dim_all);
10238 if (total < 0)
10239 return isl_basic_map_free(bmap);
10240 size = total;
10241 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10242 &sort_constraint_cmp, &size) < 0)
10243 return isl_basic_map_free(bmap);
10244 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10245 return bmap;
10248 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10249 __isl_take isl_basic_set *bset)
10251 isl_basic_map *bmap = bset_to_bmap(bset);
10252 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10255 __isl_give isl_basic_map *isl_basic_map_normalize(
10256 __isl_take isl_basic_map *bmap)
10258 bmap = isl_basic_map_remove_redundancies(bmap);
10259 bmap = isl_basic_map_sort_constraints(bmap);
10260 return bmap;
10262 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10263 __isl_keep isl_basic_map *bmap2)
10265 int i, cmp;
10266 isl_size total;
10267 isl_space *space1, *space2;
10269 if (!bmap1 || !bmap2)
10270 return -1;
10272 if (bmap1 == bmap2)
10273 return 0;
10274 space1 = isl_basic_map_peek_space(bmap1);
10275 space2 = isl_basic_map_peek_space(bmap2);
10276 cmp = isl_space_cmp(space1, space2);
10277 if (cmp)
10278 return cmp;
10279 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10280 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10281 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10282 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10283 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10284 return 0;
10285 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10286 return 1;
10287 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10288 return -1;
10289 if (bmap1->n_eq != bmap2->n_eq)
10290 return bmap1->n_eq - bmap2->n_eq;
10291 if (bmap1->n_ineq != bmap2->n_ineq)
10292 return bmap1->n_ineq - bmap2->n_ineq;
10293 if (bmap1->n_div != bmap2->n_div)
10294 return bmap1->n_div - bmap2->n_div;
10295 total = isl_basic_map_dim(bmap1, isl_dim_all);
10296 if (total < 0)
10297 return -1;
10298 for (i = 0; i < bmap1->n_eq; ++i) {
10299 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10300 if (cmp)
10301 return cmp;
10303 for (i = 0; i < bmap1->n_ineq; ++i) {
10304 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10305 if (cmp)
10306 return cmp;
10308 for (i = 0; i < bmap1->n_div; ++i) {
10309 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10310 if (cmp)
10311 return cmp;
10313 return 0;
10316 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10317 __isl_keep isl_basic_set *bset2)
10319 return isl_basic_map_plain_cmp(bset1, bset2);
10322 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10324 int i, cmp;
10326 if (set1 == set2)
10327 return 0;
10328 if (set1->n != set2->n)
10329 return set1->n - set2->n;
10331 for (i = 0; i < set1->n; ++i) {
10332 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10333 if (cmp)
10334 return cmp;
10337 return 0;
10340 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10341 __isl_keep isl_basic_map *bmap2)
10343 if (!bmap1 || !bmap2)
10344 return isl_bool_error;
10345 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10348 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10349 __isl_keep isl_basic_set *bset2)
10351 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10352 bset_to_bmap(bset2));
10355 static int qsort_bmap_cmp(const void *p1, const void *p2)
10357 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10358 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10360 return isl_basic_map_plain_cmp(bmap1, bmap2);
10363 /* Sort the basic maps of "map" and remove duplicate basic maps.
10365 * While removing basic maps, we make sure that the basic maps remain
10366 * sorted because isl_map_normalize expects the basic maps of the result
10367 * to be sorted.
10369 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10371 int i, j;
10373 map = isl_map_remove_empty_parts(map);
10374 if (!map)
10375 return NULL;
10376 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10377 for (i = map->n - 1; i >= 1; --i) {
10378 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10379 continue;
10380 isl_basic_map_free(map->p[i-1]);
10381 for (j = i; j < map->n; ++j)
10382 map->p[j - 1] = map->p[j];
10383 map->n--;
10386 return map;
10389 /* Remove obvious duplicates among the basic maps of "map".
10391 * Unlike isl_map_normalize, this function does not remove redundant
10392 * constraints and only removes duplicates that have exactly the same
10393 * constraints in the input. It does sort the constraints and
10394 * the basic maps to ease the detection of duplicates.
10396 * If "map" has already been normalized or if the basic maps are
10397 * disjoint, then there can be no duplicates.
10399 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10401 int i;
10402 isl_basic_map *bmap;
10404 if (!map)
10405 return NULL;
10406 if (map->n <= 1)
10407 return map;
10408 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10409 return map;
10410 for (i = 0; i < map->n; ++i) {
10411 bmap = isl_basic_map_copy(map->p[i]);
10412 bmap = isl_basic_map_sort_constraints(bmap);
10413 if (!bmap)
10414 return isl_map_free(map);
10415 isl_basic_map_free(map->p[i]);
10416 map->p[i] = bmap;
10419 map = sort_and_remove_duplicates(map);
10420 return map;
10423 /* We normalize in place, but if anything goes wrong we need
10424 * to return NULL, so we need to make sure we don't change the
10425 * meaning of any possible other copies of map.
10427 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10429 int i;
10430 struct isl_basic_map *bmap;
10432 if (!map)
10433 return NULL;
10434 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10435 return map;
10436 for (i = 0; i < map->n; ++i) {
10437 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10438 if (!bmap)
10439 goto error;
10440 isl_basic_map_free(map->p[i]);
10441 map->p[i] = bmap;
10444 map = sort_and_remove_duplicates(map);
10445 if (map)
10446 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10447 return map;
10448 error:
10449 isl_map_free(map);
10450 return NULL;
10453 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10455 return set_from_map(isl_map_normalize(set_to_map(set)));
10458 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10459 __isl_keep isl_map *map2)
10461 int i;
10462 isl_bool equal;
10464 if (!map1 || !map2)
10465 return isl_bool_error;
10467 if (map1 == map2)
10468 return isl_bool_true;
10469 equal = isl_map_has_equal_space(map1, map2);
10470 if (equal < 0 || !equal)
10471 return equal;
10473 map1 = isl_map_copy(map1);
10474 map2 = isl_map_copy(map2);
10475 map1 = isl_map_normalize(map1);
10476 map2 = isl_map_normalize(map2);
10477 if (!map1 || !map2)
10478 goto error;
10479 equal = map1->n == map2->n;
10480 for (i = 0; equal && i < map1->n; ++i) {
10481 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10482 if (equal < 0)
10483 goto error;
10485 isl_map_free(map1);
10486 isl_map_free(map2);
10487 return equal;
10488 error:
10489 isl_map_free(map1);
10490 isl_map_free(map2);
10491 return isl_bool_error;
10494 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10495 __isl_keep isl_set *set2)
10497 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10500 /* Return the basic maps in "map" as a list.
10502 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10503 __isl_keep isl_map *map)
10505 int i;
10506 isl_ctx *ctx;
10507 isl_basic_map_list *list;
10509 if (!map)
10510 return NULL;
10511 ctx = isl_map_get_ctx(map);
10512 list = isl_basic_map_list_alloc(ctx, map->n);
10514 for (i = 0; i < map->n; ++i) {
10515 isl_basic_map *bmap;
10517 bmap = isl_basic_map_copy(map->p[i]);
10518 list = isl_basic_map_list_add(list, bmap);
10521 return list;
10524 /* Return the intersection of the elements in the non-empty list "list".
10525 * All elements are assumed to live in the same space.
10527 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10528 __isl_take isl_basic_map_list *list)
10530 int i;
10531 isl_size n;
10532 isl_basic_map *bmap;
10534 n = isl_basic_map_list_n_basic_map(list);
10535 if (n < 0)
10536 goto error;
10537 if (n < 1)
10538 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10539 "expecting non-empty list", goto error);
10541 bmap = isl_basic_map_list_get_basic_map(list, 0);
10542 for (i = 1; i < n; ++i) {
10543 isl_basic_map *bmap_i;
10545 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10546 bmap = isl_basic_map_intersect(bmap, bmap_i);
10549 isl_basic_map_list_free(list);
10550 return bmap;
10551 error:
10552 isl_basic_map_list_free(list);
10553 return NULL;
10556 /* Return the intersection of the elements in the non-empty list "list".
10557 * All elements are assumed to live in the same space.
10559 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10560 __isl_take isl_basic_set_list *list)
10562 return isl_basic_map_list_intersect(list);
10565 /* Return the union of the elements of "list".
10566 * The list is required to have at least one element.
10568 __isl_give isl_set *isl_basic_set_list_union(
10569 __isl_take isl_basic_set_list *list)
10571 int i;
10572 isl_size n;
10573 isl_space *space;
10574 isl_basic_set *bset;
10575 isl_set *set;
10577 n = isl_basic_set_list_n_basic_set(list);
10578 if (n < 0)
10579 goto error;
10580 if (n < 1)
10581 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10582 "expecting non-empty list", goto error);
10584 bset = isl_basic_set_list_get_basic_set(list, 0);
10585 space = isl_basic_set_get_space(bset);
10586 isl_basic_set_free(bset);
10588 set = isl_set_alloc_space(space, n, 0);
10589 for (i = 0; i < n; ++i) {
10590 bset = isl_basic_set_list_get_basic_set(list, i);
10591 set = isl_set_add_basic_set(set, bset);
10594 isl_basic_set_list_free(list);
10595 return set;
10596 error:
10597 isl_basic_set_list_free(list);
10598 return NULL;
10601 /* Return the union of the elements in the non-empty list "list".
10602 * All elements are assumed to live in the same space.
10604 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10606 int i;
10607 isl_size n;
10608 isl_set *set;
10610 n = isl_set_list_n_set(list);
10611 if (n < 0)
10612 goto error;
10613 if (n < 1)
10614 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10615 "expecting non-empty list", goto error);
10617 set = isl_set_list_get_set(list, 0);
10618 for (i = 1; i < n; ++i) {
10619 isl_set *set_i;
10621 set_i = isl_set_list_get_set(list, i);
10622 set = isl_set_union(set, set_i);
10625 isl_set_list_free(list);
10626 return set;
10627 error:
10628 isl_set_list_free(list);
10629 return NULL;
10632 __isl_give isl_basic_map *isl_basic_map_product(
10633 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10635 isl_space *space_result = NULL;
10636 struct isl_basic_map *bmap;
10637 unsigned in1, in2, out1, out2, nparam, total, pos;
10638 struct isl_dim_map *dim_map1, *dim_map2;
10640 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10641 goto error;
10642 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10643 isl_space_copy(bmap2->dim));
10645 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10646 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10647 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10648 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10649 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10651 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10652 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10653 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10654 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10655 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10656 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10657 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10658 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10659 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10660 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10661 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10663 bmap = isl_basic_map_alloc_space(space_result,
10664 bmap1->n_div + bmap2->n_div,
10665 bmap1->n_eq + bmap2->n_eq,
10666 bmap1->n_ineq + bmap2->n_ineq);
10667 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10668 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10669 bmap = isl_basic_map_simplify(bmap);
10670 return isl_basic_map_finalize(bmap);
10671 error:
10672 isl_basic_map_free(bmap1);
10673 isl_basic_map_free(bmap2);
10674 return NULL;
10677 __isl_give isl_basic_map *isl_basic_map_flat_product(
10678 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10680 isl_basic_map *prod;
10682 prod = isl_basic_map_product(bmap1, bmap2);
10683 prod = isl_basic_map_flatten(prod);
10684 return prod;
10687 __isl_give isl_basic_set *isl_basic_set_flat_product(
10688 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10690 return isl_basic_map_flat_range_product(bset1, bset2);
10693 __isl_give isl_basic_map *isl_basic_map_domain_product(
10694 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10696 isl_space *space1, *space2;
10697 isl_space *space_result = NULL;
10698 isl_basic_map *bmap;
10699 isl_size in1, in2, out, nparam;
10700 unsigned total, pos;
10701 struct isl_dim_map *dim_map1, *dim_map2;
10703 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10704 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10705 out = isl_basic_map_dim(bmap1, isl_dim_out);
10706 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10707 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10708 goto error;
10710 space1 = isl_basic_map_get_space(bmap1);
10711 space2 = isl_basic_map_get_space(bmap2);
10712 space_result = isl_space_domain_product(space1, space2);
10714 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10715 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10716 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10717 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10718 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10719 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10720 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10721 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10722 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10723 isl_dim_map_div(dim_map1, bmap1, pos += out);
10724 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10726 bmap = isl_basic_map_alloc_space(space_result,
10727 bmap1->n_div + bmap2->n_div,
10728 bmap1->n_eq + bmap2->n_eq,
10729 bmap1->n_ineq + bmap2->n_ineq);
10730 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10731 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10732 bmap = isl_basic_map_simplify(bmap);
10733 return isl_basic_map_finalize(bmap);
10734 error:
10735 isl_basic_map_free(bmap1);
10736 isl_basic_map_free(bmap2);
10737 return NULL;
10740 __isl_give isl_basic_map *isl_basic_map_range_product(
10741 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10743 isl_bool rational;
10744 isl_space *space_result = NULL;
10745 isl_basic_map *bmap;
10746 isl_size in, out1, out2, nparam;
10747 unsigned total, pos;
10748 struct isl_dim_map *dim_map1, *dim_map2;
10750 rational = isl_basic_map_is_rational(bmap1);
10751 if (rational >= 0 && rational)
10752 rational = isl_basic_map_is_rational(bmap2);
10753 in = isl_basic_map_dim(bmap1, isl_dim_in);
10754 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10755 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10756 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10757 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10758 goto error;
10760 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10761 goto error;
10763 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10764 isl_space_copy(bmap2->dim));
10766 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10767 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10768 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10769 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10770 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10771 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10772 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10773 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10774 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10775 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10776 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10778 bmap = isl_basic_map_alloc_space(space_result,
10779 bmap1->n_div + bmap2->n_div,
10780 bmap1->n_eq + bmap2->n_eq,
10781 bmap1->n_ineq + bmap2->n_ineq);
10782 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10783 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10784 if (rational)
10785 bmap = isl_basic_map_set_rational(bmap);
10786 bmap = isl_basic_map_simplify(bmap);
10787 return isl_basic_map_finalize(bmap);
10788 error:
10789 isl_basic_map_free(bmap1);
10790 isl_basic_map_free(bmap2);
10791 return NULL;
10794 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10795 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10797 isl_basic_map *prod;
10799 prod = isl_basic_map_range_product(bmap1, bmap2);
10800 prod = isl_basic_map_flatten_range(prod);
10801 return prod;
10804 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10805 * and collect the results.
10806 * The result live in the space obtained by calling "space_product"
10807 * on the spaces of "map1" and "map2".
10808 * If "remove_duplicates" is set then the result may contain duplicates
10809 * (even if the inputs do not) and so we try and remove the obvious
10810 * duplicates.
10812 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10813 __isl_take isl_map *map2,
10814 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10815 __isl_take isl_space *right),
10816 __isl_give isl_basic_map *(*basic_map_product)(
10817 __isl_take isl_basic_map *left,
10818 __isl_take isl_basic_map *right),
10819 int remove_duplicates)
10821 unsigned flags = 0;
10822 struct isl_map *result;
10823 int i, j;
10824 isl_bool m;
10826 m = isl_map_has_equal_params(map1, map2);
10827 if (m < 0)
10828 goto error;
10829 if (!m)
10830 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10831 "parameters don't match", goto error);
10833 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10834 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10835 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10837 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10838 isl_space_copy(map2->dim)),
10839 map1->n * map2->n, flags);
10840 if (!result)
10841 goto error;
10842 for (i = 0; i < map1->n; ++i)
10843 for (j = 0; j < map2->n; ++j) {
10844 struct isl_basic_map *part;
10845 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10846 isl_basic_map_copy(map2->p[j]));
10847 if (isl_basic_map_is_empty(part))
10848 isl_basic_map_free(part);
10849 else
10850 result = isl_map_add_basic_map(result, part);
10851 if (!result)
10852 goto error;
10854 if (remove_duplicates)
10855 result = isl_map_remove_obvious_duplicates(result);
10856 isl_map_free(map1);
10857 isl_map_free(map2);
10858 return result;
10859 error:
10860 isl_map_free(map1);
10861 isl_map_free(map2);
10862 return NULL;
10865 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10867 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10868 __isl_take isl_map *map2)
10870 isl_map_align_params_bin(&map1, &map2);
10871 return map_product(map1, map2, &isl_space_product,
10872 &isl_basic_map_product, 0);
10875 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10877 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10878 __isl_take isl_map *map2)
10880 isl_map *prod;
10882 prod = isl_map_product(map1, map2);
10883 prod = isl_map_flatten(prod);
10884 return prod;
10887 /* Given two set A and B, construct its Cartesian product A x B.
10889 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10890 __isl_take isl_set *set2)
10892 return isl_map_range_product(set1, set2);
10895 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10896 __isl_take isl_set *set2)
10898 return isl_map_flat_range_product(set1, set2);
10901 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10903 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10904 __isl_take isl_map *map2)
10906 isl_map_align_params_bin(&map1, &map2);
10907 return map_product(map1, map2, &isl_space_domain_product,
10908 &isl_basic_map_domain_product, 1);
10911 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10913 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10914 __isl_take isl_map *map2)
10916 isl_map_align_params_bin(&map1, &map2);
10917 return map_product(map1, map2, &isl_space_range_product,
10918 &isl_basic_map_range_product, 1);
10921 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10923 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10925 isl_space *space;
10926 isl_size total1, keep1, total2, keep2;
10928 total1 = isl_map_dim(map, isl_dim_in);
10929 total2 = isl_map_dim(map, isl_dim_out);
10930 if (total1 < 0 || total2 < 0)
10931 return isl_map_free(map);
10932 if (!isl_space_domain_is_wrapping(map->dim) ||
10933 !isl_space_range_is_wrapping(map->dim))
10934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10935 "not a product", return isl_map_free(map));
10937 space = isl_map_get_space(map);
10938 space = isl_space_factor_domain(space);
10939 keep1 = isl_space_dim(space, isl_dim_in);
10940 keep2 = isl_space_dim(space, isl_dim_out);
10941 if (keep1 < 0 || keep2 < 0)
10942 map = isl_map_free(map);
10943 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10944 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10945 map = isl_map_reset_space(map, space);
10947 return map;
10950 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10952 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10954 isl_space *space;
10955 isl_size total1, keep1, total2, keep2;
10957 total1 = isl_map_dim(map, isl_dim_in);
10958 total2 = isl_map_dim(map, isl_dim_out);
10959 if (total1 < 0 || total2 < 0)
10960 return isl_map_free(map);
10961 if (!isl_space_domain_is_wrapping(map->dim) ||
10962 !isl_space_range_is_wrapping(map->dim))
10963 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10964 "not a product", return isl_map_free(map));
10966 space = isl_map_get_space(map);
10967 space = isl_space_factor_range(space);
10968 keep1 = isl_space_dim(space, isl_dim_in);
10969 keep2 = isl_space_dim(space, isl_dim_out);
10970 if (keep1 < 0 || keep2 < 0)
10971 map = isl_map_free(map);
10972 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10973 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10974 map = isl_map_reset_space(map, space);
10976 return map;
10979 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10981 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10983 isl_space *space;
10984 isl_size total, keep;
10986 total = isl_map_dim(map, isl_dim_in);
10987 if (total < 0)
10988 return isl_map_free(map);
10989 if (!isl_space_domain_is_wrapping(map->dim))
10990 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10991 "domain is not a product", return isl_map_free(map));
10993 space = isl_map_get_space(map);
10994 space = isl_space_domain_factor_domain(space);
10995 keep = isl_space_dim(space, isl_dim_in);
10996 if (keep < 0)
10997 map = isl_map_free(map);
10998 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10999 map = isl_map_reset_space(map, space);
11001 return map;
11004 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11006 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11008 isl_space *space;
11009 isl_size total, keep;
11011 total = isl_map_dim(map, isl_dim_in);
11012 if (total < 0)
11013 return isl_map_free(map);
11014 if (!isl_space_domain_is_wrapping(map->dim))
11015 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11016 "domain is not a product", return isl_map_free(map));
11018 space = isl_map_get_space(map);
11019 space = isl_space_domain_factor_range(space);
11020 keep = isl_space_dim(space, isl_dim_in);
11021 if (keep < 0)
11022 map = isl_map_free(map);
11023 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11024 map = isl_map_reset_space(map, space);
11026 return map;
11029 /* Given a map A -> [B -> C], extract the map A -> B.
11031 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11033 isl_space *space;
11034 isl_size total, keep;
11036 total = isl_map_dim(map, isl_dim_out);
11037 if (total < 0)
11038 return isl_map_free(map);
11039 if (!isl_space_range_is_wrapping(map->dim))
11040 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11041 "range is not a product", return isl_map_free(map));
11043 space = isl_map_get_space(map);
11044 space = isl_space_range_factor_domain(space);
11045 keep = isl_space_dim(space, isl_dim_out);
11046 if (keep < 0)
11047 map = isl_map_free(map);
11048 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11049 map = isl_map_reset_space(map, space);
11051 return map;
11054 /* Given a map A -> [B -> C], extract the map A -> C.
11056 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11058 isl_space *space;
11059 isl_size total, keep;
11061 total = isl_map_dim(map, isl_dim_out);
11062 if (total < 0)
11063 return isl_map_free(map);
11064 if (!isl_space_range_is_wrapping(map->dim))
11065 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11066 "range is not a product", return isl_map_free(map));
11068 space = isl_map_get_space(map);
11069 space = isl_space_range_factor_range(space);
11070 keep = isl_space_dim(space, isl_dim_out);
11071 if (keep < 0)
11072 map = isl_map_free(map);
11073 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11074 map = isl_map_reset_space(map, space);
11076 return map;
11079 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11081 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11082 __isl_take isl_map *map2)
11084 isl_map *prod;
11086 prod = isl_map_domain_product(map1, map2);
11087 prod = isl_map_flatten_domain(prod);
11088 return prod;
11091 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11093 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11094 __isl_take isl_map *map2)
11096 isl_map *prod;
11098 prod = isl_map_range_product(map1, map2);
11099 prod = isl_map_flatten_range(prod);
11100 return prod;
11103 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11105 int i;
11106 uint32_t hash = isl_hash_init();
11107 isl_size total;
11109 if (!bmap)
11110 return 0;
11111 bmap = isl_basic_map_copy(bmap);
11112 bmap = isl_basic_map_normalize(bmap);
11113 total = isl_basic_map_dim(bmap, isl_dim_all);
11114 if (total < 0)
11115 return 0;
11116 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11117 for (i = 0; i < bmap->n_eq; ++i) {
11118 uint32_t c_hash;
11119 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11120 isl_hash_hash(hash, c_hash);
11122 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11123 for (i = 0; i < bmap->n_ineq; ++i) {
11124 uint32_t c_hash;
11125 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11126 isl_hash_hash(hash, c_hash);
11128 isl_hash_byte(hash, bmap->n_div & 0xFF);
11129 for (i = 0; i < bmap->n_div; ++i) {
11130 uint32_t c_hash;
11131 if (isl_int_is_zero(bmap->div[i][0]))
11132 continue;
11133 isl_hash_byte(hash, i & 0xFF);
11134 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11135 isl_hash_hash(hash, c_hash);
11137 isl_basic_map_free(bmap);
11138 return hash;
11141 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11143 return isl_basic_map_get_hash(bset_to_bmap(bset));
11146 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11148 int i;
11149 uint32_t hash;
11151 if (!map)
11152 return 0;
11153 map = isl_map_copy(map);
11154 map = isl_map_normalize(map);
11155 if (!map)
11156 return 0;
11158 hash = isl_hash_init();
11159 for (i = 0; i < map->n; ++i) {
11160 uint32_t bmap_hash;
11161 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11162 isl_hash_hash(hash, bmap_hash);
11165 isl_map_free(map);
11167 return hash;
11170 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11172 return isl_map_get_hash(set_to_map(set));
11175 /* Return the number of basic maps in the (current) representation of "map".
11177 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11179 return map ? map->n : isl_size_error;
11182 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11184 return set ? set->n : isl_size_error;
11187 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11188 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11190 int i;
11192 if (!map)
11193 return isl_stat_error;
11195 for (i = 0; i < map->n; ++i)
11196 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11197 return isl_stat_error;
11199 return isl_stat_ok;
11202 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11203 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11205 int i;
11207 if (!set)
11208 return isl_stat_error;
11210 for (i = 0; i < set->n; ++i)
11211 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11212 return isl_stat_error;
11214 return isl_stat_ok;
11217 /* Does "test" succeed on every basic set in "set"?
11219 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11220 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11221 void *user)
11223 int i;
11225 if (!set)
11226 return isl_bool_error;
11228 for (i = 0; i < set->n; ++i) {
11229 isl_bool r;
11231 r = test(set->p[i], user);
11232 if (r < 0 || !r)
11233 return r;
11236 return isl_bool_true;
11239 /* Return a list of basic sets, the union of which is equal to "set".
11241 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11242 __isl_keep isl_set *set)
11244 int i;
11245 isl_basic_set_list *list;
11247 if (!set)
11248 return NULL;
11250 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11251 for (i = 0; i < set->n; ++i) {
11252 isl_basic_set *bset;
11254 bset = isl_basic_set_copy(set->p[i]);
11255 list = isl_basic_set_list_add(list, bset);
11258 return list;
11261 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11263 isl_space *space;
11265 if (!bset)
11266 return NULL;
11268 bset = isl_basic_set_cow(bset);
11269 if (!bset)
11270 return NULL;
11272 space = isl_basic_set_get_space(bset);
11273 space = isl_space_lift(space, bset->n_div);
11274 if (!space)
11275 goto error;
11276 isl_space_free(bset->dim);
11277 bset->dim = space;
11278 bset->extra -= bset->n_div;
11279 bset->n_div = 0;
11281 bset = isl_basic_set_finalize(bset);
11283 return bset;
11284 error:
11285 isl_basic_set_free(bset);
11286 return NULL;
11289 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11291 int i;
11292 isl_space *space;
11293 unsigned n_div;
11295 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11297 if (!set)
11298 return NULL;
11300 set = isl_set_cow(set);
11301 if (!set)
11302 return NULL;
11304 n_div = set->p[0]->n_div;
11305 space = isl_set_get_space(set);
11306 space = isl_space_lift(space, n_div);
11307 if (!space)
11308 goto error;
11309 isl_space_free(set->dim);
11310 set->dim = space;
11312 for (i = 0; i < set->n; ++i) {
11313 set->p[i] = isl_basic_set_lift(set->p[i]);
11314 if (!set->p[i])
11315 goto error;
11318 return set;
11319 error:
11320 isl_set_free(set);
11321 return NULL;
11324 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11326 isl_size dim;
11327 int size = 0;
11329 dim = isl_basic_set_dim(bset, isl_dim_all);
11330 if (dim < 0)
11331 return -1;
11332 size += bset->n_eq * (1 + dim);
11333 size += bset->n_ineq * (1 + dim);
11334 size += bset->n_div * (2 + dim);
11336 return size;
11339 int isl_set_size(__isl_keep isl_set *set)
11341 int i;
11342 int size = 0;
11344 if (!set)
11345 return -1;
11347 for (i = 0; i < set->n; ++i)
11348 size += isl_basic_set_size(set->p[i]);
11350 return size;
11353 /* Check if there is any lower bound (if lower == 0) and/or upper
11354 * bound (if upper == 0) on the specified dim.
11356 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11357 enum isl_dim_type type, unsigned pos, int lower, int upper)
11359 int i;
11361 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11362 return isl_bool_error;
11364 pos += isl_basic_map_offset(bmap, type);
11366 for (i = 0; i < bmap->n_div; ++i) {
11367 if (isl_int_is_zero(bmap->div[i][0]))
11368 continue;
11369 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11370 return isl_bool_true;
11373 for (i = 0; i < bmap->n_eq; ++i)
11374 if (!isl_int_is_zero(bmap->eq[i][pos]))
11375 return isl_bool_true;
11377 for (i = 0; i < bmap->n_ineq; ++i) {
11378 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11379 if (sgn > 0)
11380 lower = 1;
11381 if (sgn < 0)
11382 upper = 1;
11385 return lower && upper;
11388 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11389 enum isl_dim_type type, unsigned pos)
11391 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11394 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11395 enum isl_dim_type type, unsigned pos)
11397 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11400 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11401 enum isl_dim_type type, unsigned pos)
11403 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11406 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11407 enum isl_dim_type type, unsigned pos)
11409 int i;
11411 if (!map)
11412 return isl_bool_error;
11414 for (i = 0; i < map->n; ++i) {
11415 isl_bool bounded;
11416 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11417 if (bounded < 0 || !bounded)
11418 return bounded;
11421 return isl_bool_true;
11424 /* Return true if the specified dim is involved in both an upper bound
11425 * and a lower bound.
11427 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11428 enum isl_dim_type type, unsigned pos)
11430 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11433 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11435 static isl_bool has_any_bound(__isl_keep isl_map *map,
11436 enum isl_dim_type type, unsigned pos,
11437 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11438 enum isl_dim_type type, unsigned pos))
11440 int i;
11442 if (!map)
11443 return isl_bool_error;
11445 for (i = 0; i < map->n; ++i) {
11446 isl_bool bounded;
11447 bounded = fn(map->p[i], type, pos);
11448 if (bounded < 0 || bounded)
11449 return bounded;
11452 return isl_bool_false;
11455 /* Return 1 if the specified dim is involved in any lower bound.
11457 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11458 enum isl_dim_type type, unsigned pos)
11460 return has_any_bound(set, type, pos,
11461 &isl_basic_map_dim_has_lower_bound);
11464 /* Return 1 if the specified dim is involved in any upper bound.
11466 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11467 enum isl_dim_type type, unsigned pos)
11469 return has_any_bound(set, type, pos,
11470 &isl_basic_map_dim_has_upper_bound);
11473 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11475 static isl_bool has_bound(__isl_keep isl_map *map,
11476 enum isl_dim_type type, unsigned pos,
11477 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11478 enum isl_dim_type type, unsigned pos))
11480 int i;
11482 if (!map)
11483 return isl_bool_error;
11485 for (i = 0; i < map->n; ++i) {
11486 isl_bool bounded;
11487 bounded = fn(map->p[i], type, pos);
11488 if (bounded < 0 || !bounded)
11489 return bounded;
11492 return isl_bool_true;
11495 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11497 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11498 enum isl_dim_type type, unsigned pos)
11500 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11503 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11505 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11506 enum isl_dim_type type, unsigned pos)
11508 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11511 /* For each of the "n" variables starting at "first", determine
11512 * the sign of the variable and put the results in the first "n"
11513 * elements of the array "signs".
11514 * Sign
11515 * 1 means that the variable is non-negative
11516 * -1 means that the variable is non-positive
11517 * 0 means the variable attains both positive and negative values.
11519 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11520 unsigned first, unsigned n, int *signs)
11522 isl_vec *bound = NULL;
11523 struct isl_tab *tab = NULL;
11524 struct isl_tab_undo *snap;
11525 int i;
11526 isl_size total;
11528 total = isl_basic_set_dim(bset, isl_dim_all);
11529 if (total < 0 || !signs)
11530 return isl_stat_error;
11532 bound = isl_vec_alloc(bset->ctx, 1 + total);
11533 tab = isl_tab_from_basic_set(bset, 0);
11534 if (!bound || !tab)
11535 goto error;
11537 isl_seq_clr(bound->el, bound->size);
11538 isl_int_set_si(bound->el[0], -1);
11540 snap = isl_tab_snap(tab);
11541 for (i = 0; i < n; ++i) {
11542 int empty;
11544 isl_int_set_si(bound->el[1 + first + i], -1);
11545 if (isl_tab_add_ineq(tab, bound->el) < 0)
11546 goto error;
11547 empty = tab->empty;
11548 isl_int_set_si(bound->el[1 + first + i], 0);
11549 if (isl_tab_rollback(tab, snap) < 0)
11550 goto error;
11552 if (empty) {
11553 signs[i] = 1;
11554 continue;
11557 isl_int_set_si(bound->el[1 + first + i], 1);
11558 if (isl_tab_add_ineq(tab, bound->el) < 0)
11559 goto error;
11560 empty = tab->empty;
11561 isl_int_set_si(bound->el[1 + first + i], 0);
11562 if (isl_tab_rollback(tab, snap) < 0)
11563 goto error;
11565 signs[i] = empty ? -1 : 0;
11568 isl_tab_free(tab);
11569 isl_vec_free(bound);
11570 return isl_stat_ok;
11571 error:
11572 isl_tab_free(tab);
11573 isl_vec_free(bound);
11574 return isl_stat_error;
11577 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11578 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11580 if (!bset || !signs)
11581 return isl_stat_error;
11582 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11583 return isl_stat_error;
11585 first += pos(bset->dim, type) - 1;
11586 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11589 /* Is it possible for the integer division "div" to depend (possibly
11590 * indirectly) on any output dimensions?
11592 * If the div is undefined, then we conservatively assume that it
11593 * may depend on them.
11594 * Otherwise, we check if it actually depends on them or on any integer
11595 * divisions that may depend on them.
11597 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11599 int i;
11600 isl_size n_out, n_div;
11601 unsigned o_out, o_div;
11603 if (isl_int_is_zero(bmap->div[div][0]))
11604 return isl_bool_true;
11606 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11607 if (n_out < 0)
11608 return isl_bool_error;
11609 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11611 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11612 return isl_bool_true;
11614 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11615 if (n_div < 0)
11616 return isl_bool_error;
11617 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11619 for (i = 0; i < n_div; ++i) {
11620 isl_bool may_involve;
11622 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11623 continue;
11624 may_involve = div_may_involve_output(bmap, i);
11625 if (may_involve < 0 || may_involve)
11626 return may_involve;
11629 return isl_bool_false;
11632 /* Return the first integer division of "bmap" in the range
11633 * [first, first + n[ that may depend on any output dimensions and
11634 * that has a non-zero coefficient in "c" (where the first coefficient
11635 * in "c" corresponds to integer division "first").
11637 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11638 isl_int *c, int first, int n)
11640 int k;
11642 if (!bmap)
11643 return -1;
11645 for (k = first; k < first + n; ++k) {
11646 isl_bool may_involve;
11648 if (isl_int_is_zero(c[k]))
11649 continue;
11650 may_involve = div_may_involve_output(bmap, k);
11651 if (may_involve < 0)
11652 return -1;
11653 if (may_involve)
11654 return k;
11657 return first + n;
11660 /* Look for a pair of inequality constraints in "bmap" of the form
11662 * -l + i >= 0 or i >= l
11663 * and
11664 * n + l - i >= 0 or i <= l + n
11666 * with n < "m" and i the output dimension at position "pos".
11667 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11668 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11669 * and earlier output dimensions, as well as integer divisions that do
11670 * not involve any of the output dimensions.
11672 * Return the index of the first inequality constraint or bmap->n_ineq
11673 * if no such pair can be found.
11675 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11676 int pos, isl_int m)
11678 int i, j;
11679 isl_ctx *ctx;
11680 isl_size total;
11681 isl_size n_div, n_out;
11682 unsigned o_div, o_out;
11683 int less;
11685 total = isl_basic_map_dim(bmap, isl_dim_all);
11686 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11687 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11688 if (total < 0 || n_out < 0 || n_div < 0)
11689 return -1;
11691 ctx = isl_basic_map_get_ctx(bmap);
11692 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11693 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11694 for (i = 0; i < bmap->n_ineq; ++i) {
11695 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11696 continue;
11697 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11698 n_out - (pos + 1)) != -1)
11699 continue;
11700 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11701 0, n_div) < n_div)
11702 continue;
11703 for (j = i + 1; j < bmap->n_ineq; ++j) {
11704 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11705 ctx->one))
11706 continue;
11707 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11708 bmap->ineq[j] + 1, total))
11709 continue;
11710 break;
11712 if (j >= bmap->n_ineq)
11713 continue;
11714 isl_int_add(bmap->ineq[i][0],
11715 bmap->ineq[i][0], bmap->ineq[j][0]);
11716 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11717 isl_int_sub(bmap->ineq[i][0],
11718 bmap->ineq[i][0], bmap->ineq[j][0]);
11719 if (!less)
11720 continue;
11721 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11722 return i;
11723 else
11724 return j;
11727 return bmap->n_ineq;
11730 /* Return the index of the equality of "bmap" that defines
11731 * the output dimension "pos" in terms of earlier dimensions.
11732 * The equality may also involve integer divisions, as long
11733 * as those integer divisions are defined in terms of
11734 * parameters or input dimensions.
11735 * In this case, *div is set to the number of integer divisions and
11736 * *ineq is set to the number of inequality constraints (provided
11737 * div and ineq are not NULL).
11739 * The equality may also involve a single integer division involving
11740 * the output dimensions (typically only output dimension "pos") as
11741 * long as the coefficient of output dimension "pos" is 1 or -1 and
11742 * there is a pair of constraints i >= l and i <= l + n, with i referring
11743 * to output dimension "pos", l an expression involving only earlier
11744 * dimensions and n smaller than the coefficient of the integer division
11745 * in the equality. In this case, the output dimension can be defined
11746 * in terms of a modulo expression that does not involve the integer division.
11747 * *div is then set to this single integer division and
11748 * *ineq is set to the index of constraint i >= l.
11750 * Return bmap->n_eq if there is no such equality.
11751 * Return -1 on error.
11753 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11754 int pos, int *div, int *ineq)
11756 int j, k, l;
11757 isl_size n_div, n_out;
11758 unsigned o_div, o_out;
11760 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11761 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11762 if (n_out < 0 || n_div < 0)
11763 return -1;
11765 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11766 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11768 if (ineq)
11769 *ineq = bmap->n_ineq;
11770 if (div)
11771 *div = n_div;
11772 for (j = 0; j < bmap->n_eq; ++j) {
11773 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11774 continue;
11775 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11776 n_out - (pos + 1)) != -1)
11777 continue;
11778 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11779 0, n_div);
11780 if (k >= n_div)
11781 return j;
11782 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11783 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11784 continue;
11785 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11786 k + 1, n_div - (k+1)) < n_div)
11787 continue;
11788 l = find_modulo_constraint_pair(bmap, pos,
11789 bmap->eq[j][o_div + k]);
11790 if (l < 0)
11791 return -1;
11792 if (l >= bmap->n_ineq)
11793 continue;
11794 if (div)
11795 *div = k;
11796 if (ineq)
11797 *ineq = l;
11798 return j;
11801 return bmap->n_eq;
11804 /* Check if the given basic map is obviously single-valued.
11805 * In particular, for each output dimension, check that there is
11806 * an equality that defines the output dimension in terms of
11807 * earlier dimensions.
11809 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11811 int i;
11812 isl_size n_out;
11814 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11815 if (n_out < 0)
11816 return isl_bool_error;
11818 for (i = 0; i < n_out; ++i) {
11819 int eq;
11821 eq = isl_basic_map_output_defining_equality(bmap, i,
11822 NULL, NULL);
11823 if (eq < 0)
11824 return isl_bool_error;
11825 if (eq >= bmap->n_eq)
11826 return isl_bool_false;
11829 return isl_bool_true;
11832 /* Check if the given basic map is single-valued.
11833 * We simply compute
11835 * M \circ M^-1
11837 * and check if the result is a subset of the identity mapping.
11839 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11841 isl_space *space;
11842 isl_basic_map *test;
11843 isl_basic_map *id;
11844 isl_bool sv;
11846 sv = isl_basic_map_plain_is_single_valued(bmap);
11847 if (sv < 0 || sv)
11848 return sv;
11850 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11851 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11853 space = isl_basic_map_get_space(bmap);
11854 space = isl_space_map_from_set(isl_space_range(space));
11855 id = isl_basic_map_identity(space);
11857 sv = isl_basic_map_is_subset(test, id);
11859 isl_basic_map_free(test);
11860 isl_basic_map_free(id);
11862 return sv;
11865 /* Check if the given map is obviously single-valued.
11867 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11869 if (!map)
11870 return isl_bool_error;
11871 if (map->n == 0)
11872 return isl_bool_true;
11873 if (map->n >= 2)
11874 return isl_bool_false;
11876 return isl_basic_map_plain_is_single_valued(map->p[0]);
11879 /* Check if the given map is single-valued.
11880 * We simply compute
11882 * M \circ M^-1
11884 * and check if the result is a subset of the identity mapping.
11886 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11888 isl_space *space;
11889 isl_map *test;
11890 isl_map *id;
11891 isl_bool sv;
11893 sv = isl_map_plain_is_single_valued(map);
11894 if (sv < 0 || sv)
11895 return sv;
11897 test = isl_map_reverse(isl_map_copy(map));
11898 test = isl_map_apply_range(test, isl_map_copy(map));
11900 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11901 id = isl_map_identity(space);
11903 sv = isl_map_is_subset(test, id);
11905 isl_map_free(test);
11906 isl_map_free(id);
11908 return sv;
11911 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11913 isl_bool in;
11915 map = isl_map_copy(map);
11916 map = isl_map_reverse(map);
11917 in = isl_map_is_single_valued(map);
11918 isl_map_free(map);
11920 return in;
11923 /* Check if the given map is obviously injective.
11925 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11927 isl_bool in;
11929 map = isl_map_copy(map);
11930 map = isl_map_reverse(map);
11931 in = isl_map_plain_is_single_valued(map);
11932 isl_map_free(map);
11934 return in;
11937 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11939 isl_bool sv;
11941 sv = isl_map_is_single_valued(map);
11942 if (sv < 0 || !sv)
11943 return sv;
11945 return isl_map_is_injective(map);
11948 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11950 return isl_map_is_single_valued(set_to_map(set));
11953 /* Does "map" only map elements to themselves?
11955 * If the domain and range spaces are different, then "map"
11956 * is considered not to be an identity relation, even if it is empty.
11957 * Otherwise, construct the maximal identity relation and
11958 * check whether "map" is a subset of this relation.
11960 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11962 isl_map *id;
11963 isl_bool equal, is_identity;
11965 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
11966 if (equal < 0 || !equal)
11967 return equal;
11969 id = isl_map_identity(isl_map_get_space(map));
11970 is_identity = isl_map_is_subset(map, id);
11971 isl_map_free(id);
11973 return is_identity;
11976 int isl_map_is_translation(__isl_keep isl_map *map)
11978 int ok;
11979 isl_set *delta;
11981 delta = isl_map_deltas(isl_map_copy(map));
11982 ok = isl_set_is_singleton(delta);
11983 isl_set_free(delta);
11985 return ok;
11988 static int unique(isl_int *p, unsigned pos, unsigned len)
11990 if (isl_seq_first_non_zero(p, pos) != -1)
11991 return 0;
11992 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11993 return 0;
11994 return 1;
11997 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11999 int i, j;
12000 isl_size nvar, n_div;
12001 unsigned ovar;
12003 n_div = isl_basic_set_dim(bset, isl_dim_div);
12004 if (n_div < 0)
12005 return isl_bool_error;
12006 if (n_div != 0)
12007 return isl_bool_false;
12009 nvar = isl_basic_set_dim(bset, isl_dim_set);
12010 if (nvar < 0)
12011 return isl_bool_error;
12012 ovar = isl_space_offset(bset->dim, isl_dim_set);
12013 for (j = 0; j < nvar; ++j) {
12014 int lower = 0, upper = 0;
12015 for (i = 0; i < bset->n_eq; ++i) {
12016 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12017 continue;
12018 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12019 return isl_bool_false;
12020 break;
12022 if (i < bset->n_eq)
12023 continue;
12024 for (i = 0; i < bset->n_ineq; ++i) {
12025 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12026 continue;
12027 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12028 return isl_bool_false;
12029 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12030 lower = 1;
12031 else
12032 upper = 1;
12034 if (!lower || !upper)
12035 return isl_bool_false;
12038 return isl_bool_true;
12041 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12043 if (!set)
12044 return isl_bool_error;
12045 if (set->n != 1)
12046 return isl_bool_false;
12048 return isl_basic_set_is_box(set->p[0]);
12051 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12053 if (!bset)
12054 return isl_bool_error;
12056 return isl_space_is_wrapping(bset->dim);
12059 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12061 if (!set)
12062 return isl_bool_error;
12064 return isl_space_is_wrapping(set->dim);
12067 /* Modify the space of "map" through a call to "change".
12068 * If "can_change" is set (not NULL), then first call it to check
12069 * if the modification is allowed, printing the error message "cannot_change"
12070 * if it is not.
12072 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12073 isl_bool (*can_change)(__isl_keep isl_map *map),
12074 const char *cannot_change,
12075 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12077 isl_bool ok;
12078 isl_space *space;
12080 if (!map)
12081 return NULL;
12083 ok = can_change ? can_change(map) : isl_bool_true;
12084 if (ok < 0)
12085 return isl_map_free(map);
12086 if (!ok)
12087 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12088 return isl_map_free(map));
12090 space = change(isl_map_get_space(map));
12091 map = isl_map_reset_space(map, space);
12093 return map;
12096 /* Is the domain of "map" a wrapped relation?
12098 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12100 if (!map)
12101 return isl_bool_error;
12103 return isl_space_domain_is_wrapping(map->dim);
12106 /* Does "map" have a wrapped relation in both domain and range?
12108 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12110 return isl_space_is_product(isl_map_peek_space(map));
12113 /* Is the range of "map" a wrapped relation?
12115 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12117 if (!map)
12118 return isl_bool_error;
12120 return isl_space_range_is_wrapping(map->dim);
12123 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12125 isl_space *space;
12127 space = isl_basic_map_take_space(bmap);
12128 space = isl_space_wrap(space);
12129 bmap = isl_basic_map_restore_space(bmap, space);
12131 bmap = isl_basic_map_finalize(bmap);
12133 return bset_from_bmap(bmap);
12136 /* Given a map A -> B, return the set (A -> B).
12138 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12140 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12143 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12145 bset = isl_basic_set_cow(bset);
12146 if (!bset)
12147 return NULL;
12149 bset->dim = isl_space_unwrap(bset->dim);
12150 if (!bset->dim)
12151 goto error;
12153 bset = isl_basic_set_finalize(bset);
12155 return bset_to_bmap(bset);
12156 error:
12157 isl_basic_set_free(bset);
12158 return NULL;
12161 /* Given a set (A -> B), return the map A -> B.
12162 * Error out if "set" is not of the form (A -> B).
12164 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12166 return isl_map_change_space(set, &isl_set_is_wrapping,
12167 "not a wrapping set", &isl_space_unwrap);
12170 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12171 enum isl_dim_type type)
12173 isl_space *space;
12175 space = isl_basic_map_take_space(bmap);
12176 space = isl_space_reset(space, type);
12177 bmap = isl_basic_map_restore_space(bmap, space);
12179 bmap = isl_basic_map_mark_final(bmap);
12181 return bmap;
12184 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12185 enum isl_dim_type type)
12187 int i;
12188 isl_space *space;
12190 if (!map)
12191 return NULL;
12193 if (!isl_space_is_named_or_nested(map->dim, type))
12194 return map;
12196 map = isl_map_cow(map);
12197 if (!map)
12198 return NULL;
12200 for (i = 0; i < map->n; ++i) {
12201 map->p[i] = isl_basic_map_reset(map->p[i], type);
12202 if (!map->p[i])
12203 goto error;
12206 space = isl_map_take_space(map);
12207 space = isl_space_reset(space, type);
12208 map = isl_map_restore_space(map, space);
12210 return map;
12211 error:
12212 isl_map_free(map);
12213 return NULL;
12216 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12218 isl_space *space;
12220 space = isl_basic_map_take_space(bmap);
12221 space = isl_space_flatten(space);
12222 bmap = isl_basic_map_restore_space(bmap, space);
12224 bmap = isl_basic_map_mark_final(bmap);
12226 return bmap;
12229 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12231 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12234 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12235 __isl_take isl_basic_map *bmap)
12237 isl_space *space;
12239 space = isl_basic_map_take_space(bmap);
12240 space = isl_space_flatten_domain(space);
12241 bmap = isl_basic_map_restore_space(bmap, space);
12243 bmap = isl_basic_map_mark_final(bmap);
12245 return bmap;
12248 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12249 __isl_take isl_basic_map *bmap)
12251 isl_space *space;
12253 space = isl_basic_map_take_space(bmap);
12254 space = isl_space_flatten_range(space);
12255 bmap = isl_basic_map_restore_space(bmap, space);
12257 bmap = isl_basic_map_mark_final(bmap);
12259 return bmap;
12262 /* Remove any internal structure from the spaces of domain and range of "map".
12264 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12266 if (!map)
12267 return NULL;
12269 if (!map->dim->nested[0] && !map->dim->nested[1])
12270 return map;
12272 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12275 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12277 return set_from_map(isl_map_flatten(set_to_map(set)));
12280 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12282 isl_space *space, *flat_space;
12283 isl_map *map;
12285 space = isl_set_get_space(set);
12286 flat_space = isl_space_flatten(isl_space_copy(space));
12287 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12288 flat_space));
12289 map = isl_map_intersect_domain(map, set);
12291 return map;
12294 /* Remove any internal structure from the space of the domain of "map".
12296 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12298 if (!map)
12299 return NULL;
12301 if (!map->dim->nested[0])
12302 return map;
12304 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12307 /* Remove any internal structure from the space of the range of "map".
12309 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12311 if (!map)
12312 return NULL;
12314 if (!map->dim->nested[1])
12315 return map;
12317 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12320 /* Reorder the dimensions of "bmap" according to the given dim_map
12321 * and set the dimension specification to "space" and
12322 * perform Gaussian elimination on the result.
12324 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12325 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12327 isl_basic_map *res;
12328 unsigned flags;
12329 isl_size n_div;
12331 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12332 if (n_div < 0 || !space || !dim_map)
12333 goto error;
12335 flags = bmap->flags;
12336 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12337 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12338 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12339 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12340 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12341 if (res)
12342 res->flags = flags;
12343 res = isl_basic_map_gauss(res, NULL);
12344 res = isl_basic_map_finalize(res);
12345 return res;
12346 error:
12347 isl_dim_map_free(dim_map);
12348 isl_basic_map_free(bmap);
12349 isl_space_free(space);
12350 return NULL;
12353 /* Reorder the dimensions of "map" according to given reordering.
12355 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12356 __isl_take isl_reordering *r)
12358 int i;
12359 struct isl_dim_map *dim_map;
12361 map = isl_map_cow(map);
12362 dim_map = isl_dim_map_from_reordering(r);
12363 if (!map || !r || !dim_map)
12364 goto error;
12366 for (i = 0; i < map->n; ++i) {
12367 struct isl_dim_map *dim_map_i;
12368 isl_space *space;
12370 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12372 space = isl_reordering_get_space(r);
12373 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12375 if (!map->p[i])
12376 goto error;
12379 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12380 map = isl_map_unmark_normalized(map);
12382 isl_reordering_free(r);
12383 isl_dim_map_free(dim_map);
12384 return map;
12385 error:
12386 isl_dim_map_free(dim_map);
12387 isl_map_free(map);
12388 isl_reordering_free(r);
12389 return NULL;
12392 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12393 __isl_take isl_reordering *r)
12395 return set_from_map(isl_map_realign(set_to_map(set), r));
12398 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12399 __isl_take isl_space *model)
12401 isl_ctx *ctx;
12402 isl_bool aligned;
12404 if (!map || !model)
12405 goto error;
12407 ctx = isl_space_get_ctx(model);
12408 if (!isl_space_has_named_params(model))
12409 isl_die(ctx, isl_error_invalid,
12410 "model has unnamed parameters", goto error);
12411 if (isl_map_check_named_params(map) < 0)
12412 goto error;
12413 aligned = isl_map_space_has_equal_params(map, model);
12414 if (aligned < 0)
12415 goto error;
12416 if (!aligned) {
12417 isl_reordering *exp;
12419 exp = isl_parameter_alignment_reordering(map->dim, model);
12420 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12421 map = isl_map_realign(map, exp);
12424 isl_space_free(model);
12425 return map;
12426 error:
12427 isl_space_free(model);
12428 isl_map_free(map);
12429 return NULL;
12432 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12433 __isl_take isl_space *model)
12435 return isl_map_align_params(set, model);
12438 /* Align the parameters of "bmap" to those of "model", introducing
12439 * additional parameters if needed.
12441 __isl_give isl_basic_map *isl_basic_map_align_params(
12442 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12444 isl_ctx *ctx;
12445 isl_bool equal_params;
12447 if (!bmap || !model)
12448 goto error;
12450 ctx = isl_space_get_ctx(model);
12451 if (!isl_space_has_named_params(model))
12452 isl_die(ctx, isl_error_invalid,
12453 "model has unnamed parameters", goto error);
12454 if (isl_basic_map_check_named_params(bmap) < 0)
12455 goto error;
12456 equal_params = isl_space_has_equal_params(bmap->dim, model);
12457 if (equal_params < 0)
12458 goto error;
12459 if (!equal_params) {
12460 isl_reordering *exp;
12461 struct isl_dim_map *dim_map;
12463 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12464 exp = isl_reordering_extend_space(exp,
12465 isl_basic_map_get_space(bmap));
12466 dim_map = isl_dim_map_from_reordering(exp);
12467 bmap = isl_basic_map_realign(bmap,
12468 isl_reordering_get_space(exp),
12469 isl_dim_map_extend(dim_map, bmap));
12470 isl_reordering_free(exp);
12471 isl_dim_map_free(dim_map);
12474 isl_space_free(model);
12475 return bmap;
12476 error:
12477 isl_space_free(model);
12478 isl_basic_map_free(bmap);
12479 return NULL;
12482 /* Do "bset" and "space" have the same parameters?
12484 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12485 __isl_keep isl_space *space)
12487 isl_space *bset_space;
12489 bset_space = isl_basic_set_peek_space(bset);
12490 return isl_space_has_equal_params(bset_space, space);
12493 /* Do "map" and "space" have the same parameters?
12495 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12496 __isl_keep isl_space *space)
12498 isl_space *map_space;
12500 map_space = isl_map_peek_space(map);
12501 return isl_space_has_equal_params(map_space, space);
12504 /* Do "set" and "space" have the same parameters?
12506 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12507 __isl_keep isl_space *space)
12509 return isl_map_space_has_equal_params(set_to_map(set), space);
12512 /* Align the parameters of "bset" to those of "model", introducing
12513 * additional parameters if needed.
12515 __isl_give isl_basic_set *isl_basic_set_align_params(
12516 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12518 return isl_basic_map_align_params(bset, model);
12521 /* Drop all parameters not referenced by "map".
12523 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12525 int i;
12526 isl_size n;
12528 n = isl_map_dim(map, isl_dim_param);
12529 if (isl_map_check_named_params(map) < 0 || n < 0)
12530 return isl_map_free(map);
12532 for (i = n - 1; i >= 0; i--) {
12533 isl_bool involves;
12535 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12536 if (involves < 0)
12537 return isl_map_free(map);
12538 if (!involves)
12539 map = isl_map_project_out(map, isl_dim_param, i, 1);
12542 return map;
12545 /* Drop all parameters not referenced by "set".
12547 __isl_give isl_set *isl_set_drop_unused_params(
12548 __isl_take isl_set *set)
12550 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12553 /* Drop all parameters not referenced by "bmap".
12555 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12556 __isl_take isl_basic_map *bmap)
12558 isl_size nparam;
12559 int i;
12561 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12562 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12563 return isl_basic_map_free(bmap);
12565 for (i = nparam - 1; i >= 0; i--) {
12566 isl_bool involves;
12568 involves = isl_basic_map_involves_dims(bmap,
12569 isl_dim_param, i, 1);
12570 if (involves < 0)
12571 return isl_basic_map_free(bmap);
12572 if (!involves)
12573 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12576 return bmap;
12579 /* Drop all parameters not referenced by "bset".
12581 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12582 __isl_take isl_basic_set *bset)
12584 return bset_from_bmap(isl_basic_map_drop_unused_params(
12585 bset_to_bmap(bset)));
12588 /* Given a tuple of identifiers "tuple" in a space that corresponds
12589 * to that of "set", if any of those identifiers appear as parameters
12590 * in "set", then equate those parameters with the corresponding
12591 * set dimensions and project out the parameters.
12592 * The result therefore has no such parameters.
12594 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12595 __isl_keep isl_multi_id *tuple)
12597 int i;
12598 isl_size n;
12599 isl_space *set_space, *tuple_space;
12601 set_space = isl_set_peek_space(set);
12602 tuple_space = isl_multi_id_peek_space(tuple);
12603 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12604 return isl_set_free(set);
12605 n = isl_multi_id_size(tuple);
12606 if (n < 0)
12607 return isl_set_free(set);
12608 for (i = 0; i < n; ++i) {
12609 isl_id *id;
12610 int pos;
12612 id = isl_multi_id_get_at(tuple, i);
12613 if (!id)
12614 return isl_set_free(set);
12615 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12616 isl_id_free(id);
12617 if (pos < 0)
12618 continue;
12619 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12620 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12622 return set;
12625 /* Bind the set dimensions of "set" to parameters with identifiers
12626 * specified by "tuple", living in the same space as "set".
12628 * If no parameters with these identifiers appear in "set" already,
12629 * then the set dimensions are simply reinterpreted as parameters.
12630 * Otherwise, the parameters are first equated to the corresponding
12631 * set dimensions.
12633 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12634 __isl_take isl_multi_id *tuple)
12636 isl_space *space;
12638 set = equate_params(set, tuple);
12639 space = isl_set_get_space(set);
12640 space = isl_space_bind_set(space, tuple);
12641 isl_multi_id_free(tuple);
12642 set = isl_set_reset_space(set, space);
12644 return set;
12647 /* Given a tuple of identifiers "tuple" in a space that corresponds
12648 * to the domain of "map", if any of those identifiers appear as parameters
12649 * in "map", then equate those parameters with the corresponding
12650 * input dimensions and project out the parameters.
12651 * The result therefore has no such parameters.
12653 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12654 __isl_keep isl_multi_id *tuple)
12656 int i;
12657 isl_size n;
12658 isl_space *map_space, *tuple_space;
12660 map_space = isl_map_peek_space(map);
12661 tuple_space = isl_multi_id_peek_space(tuple);
12662 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12663 return isl_map_free(map);
12664 n = isl_multi_id_size(tuple);
12665 if (n < 0)
12666 return isl_map_free(map);
12667 for (i = 0; i < n; ++i) {
12668 isl_id *id;
12669 int pos;
12671 id = isl_multi_id_get_at(tuple, i);
12672 if (!id)
12673 return isl_map_free(map);
12674 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12675 isl_id_free(id);
12676 if (pos < 0)
12677 continue;
12678 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12679 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12681 return map;
12684 /* Bind the input dimensions of "map" to parameters with identifiers
12685 * specified by "tuple", living in the domain space of "map".
12687 * If no parameters with these identifiers appear in "map" already,
12688 * then the input dimensions are simply reinterpreted as parameters.
12689 * Otherwise, the parameters are first equated to the corresponding
12690 * input dimensions.
12692 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12693 __isl_take isl_multi_id *tuple)
12695 isl_space *space;
12696 isl_set *set;
12698 map = map_equate_params(map, tuple);
12699 space = isl_map_get_space(map);
12700 space = isl_space_bind_map_domain(space, tuple);
12701 isl_multi_id_free(tuple);
12702 set = set_from_map(isl_map_reset_space(map, space));
12704 return set;
12707 /* Bind the output dimensions of "map" to parameters with identifiers
12708 * specified by "tuple", living in the range space of "map".
12710 * Since binding is more easily implemented on the domain,
12711 * bind the input dimensions of the inverse of "map".
12713 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12714 __isl_take isl_multi_id *tuple)
12716 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12719 /* Insert a domain corresponding to "tuple"
12720 * into the nullary or unary relation "set".
12721 * The result has an extra initial tuple and is therefore
12722 * either a unary or binary relation.
12723 * Any parameters with identifiers in "tuple" are reinterpreted
12724 * as the corresponding domain dimensions.
12726 static __isl_give isl_map *unbind_params_insert_domain(
12727 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12729 isl_space *space;
12730 isl_reordering *r;
12732 space = isl_set_peek_space(set);
12733 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12734 isl_multi_id_free(tuple);
12736 return isl_map_realign(set_to_map(set), r);
12739 /* Construct a set with "tuple" as domain from the parameter domain "set".
12740 * Any parameters with identifiers in "tuple" are reinterpreted
12741 * as the corresponding set dimensions.
12743 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12744 __isl_take isl_multi_id *tuple)
12746 isl_bool is_params;
12748 is_params = isl_set_is_params(set);
12749 if (is_params < 0)
12750 set = isl_set_free(set);
12751 else if (!is_params)
12752 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12753 "expecting parameter domain", set = isl_set_free(set));
12754 return set_from_map(unbind_params_insert_domain(set, tuple));
12757 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12759 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12761 isl_bool is_params;
12763 is_params = isl_set_is_params(set);
12764 if (is_params < 0)
12765 return isl_stat_error;
12766 else if (is_params)
12767 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12768 "expecting proper set", return isl_stat_error);
12770 return isl_stat_ok;
12773 /* Construct a map with "domain" as domain and "set" as range.
12774 * Any parameters with identifiers in "domain" are reinterpreted
12775 * as the corresponding domain dimensions.
12777 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12778 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12780 if (isl_set_check_is_set(set) < 0)
12781 set = isl_set_free(set);
12782 return unbind_params_insert_domain(set, domain);
12785 /* Construct a map with "domain" as domain and "set" as range.
12787 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12788 __isl_take isl_space *domain)
12790 isl_size dim;
12791 isl_space *space;
12792 isl_map *map;
12794 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12795 domain = isl_space_free(domain);
12796 dim = isl_space_dim(domain, isl_dim_set);
12797 if (dim < 0)
12798 domain = isl_space_free(domain);
12799 space = isl_set_get_space(set);
12800 domain = isl_space_replace_params(domain, space);
12801 space = isl_space_map_from_domain_and_range(domain, space);
12803 map = isl_map_from_range(set);
12804 map = isl_map_add_dims(map, isl_dim_in, dim);
12805 map = isl_map_reset_space(map, space);
12807 return map;
12810 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12811 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12812 enum isl_dim_type c2, enum isl_dim_type c3,
12813 enum isl_dim_type c4, enum isl_dim_type c5)
12815 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12816 struct isl_mat *mat;
12817 int i, j, k;
12818 int pos;
12819 isl_size total;
12821 total = isl_basic_map_dim(bmap, isl_dim_all);
12822 if (total < 0)
12823 return NULL;
12824 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12825 if (!mat)
12826 return NULL;
12827 for (i = 0; i < bmap->n_eq; ++i)
12828 for (j = 0, pos = 0; j < 5; ++j) {
12829 int off = isl_basic_map_offset(bmap, c[j]);
12830 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12831 if (dim < 0)
12832 return isl_mat_free(mat);
12833 for (k = 0; k < dim; ++k) {
12834 isl_int_set(mat->row[i][pos],
12835 bmap->eq[i][off + k]);
12836 ++pos;
12840 return mat;
12843 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12844 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12845 enum isl_dim_type c2, enum isl_dim_type c3,
12846 enum isl_dim_type c4, enum isl_dim_type c5)
12848 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12849 struct isl_mat *mat;
12850 int i, j, k;
12851 int pos;
12852 isl_size total;
12854 total = isl_basic_map_dim(bmap, isl_dim_all);
12855 if (total < 0)
12856 return NULL;
12857 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12858 if (!mat)
12859 return NULL;
12860 for (i = 0; i < bmap->n_ineq; ++i)
12861 for (j = 0, pos = 0; j < 5; ++j) {
12862 int off = isl_basic_map_offset(bmap, c[j]);
12863 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12864 if (dim < 0)
12865 return isl_mat_free(mat);
12866 for (k = 0; k < dim; ++k) {
12867 isl_int_set(mat->row[i][pos],
12868 bmap->ineq[i][off + k]);
12869 ++pos;
12873 return mat;
12876 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12877 __isl_take isl_space *space,
12878 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12879 enum isl_dim_type c2, enum isl_dim_type c3,
12880 enum isl_dim_type c4, enum isl_dim_type c5)
12882 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12883 isl_basic_map *bmap = NULL;
12884 isl_size dim;
12885 unsigned total;
12886 unsigned extra;
12887 int i, j, k, l;
12888 int pos;
12890 dim = isl_space_dim(space, isl_dim_all);
12891 if (dim < 0 || !eq || !ineq)
12892 goto error;
12894 if (eq->n_col != ineq->n_col)
12895 isl_die(space->ctx, isl_error_invalid,
12896 "equalities and inequalities matrices should have "
12897 "same number of columns", goto error);
12899 total = 1 + dim;
12901 if (eq->n_col < total)
12902 isl_die(space->ctx, isl_error_invalid,
12903 "number of columns too small", goto error);
12905 extra = eq->n_col - total;
12907 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12908 eq->n_row, ineq->n_row);
12909 if (!bmap)
12910 goto error;
12911 for (i = 0; i < extra; ++i) {
12912 k = isl_basic_map_alloc_div(bmap);
12913 if (k < 0)
12914 goto error;
12915 isl_int_set_si(bmap->div[k][0], 0);
12917 for (i = 0; i < eq->n_row; ++i) {
12918 l = isl_basic_map_alloc_equality(bmap);
12919 if (l < 0)
12920 goto error;
12921 for (j = 0, pos = 0; j < 5; ++j) {
12922 int off = isl_basic_map_offset(bmap, c[j]);
12923 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12924 if (dim < 0)
12925 goto error;
12926 for (k = 0; k < dim; ++k) {
12927 isl_int_set(bmap->eq[l][off + k],
12928 eq->row[i][pos]);
12929 ++pos;
12933 for (i = 0; i < ineq->n_row; ++i) {
12934 l = isl_basic_map_alloc_inequality(bmap);
12935 if (l < 0)
12936 goto error;
12937 for (j = 0, pos = 0; j < 5; ++j) {
12938 int off = isl_basic_map_offset(bmap, c[j]);
12939 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12940 if (dim < 0)
12941 goto error;
12942 for (k = 0; k < dim; ++k) {
12943 isl_int_set(bmap->ineq[l][off + k],
12944 ineq->row[i][pos]);
12945 ++pos;
12950 isl_space_free(space);
12951 isl_mat_free(eq);
12952 isl_mat_free(ineq);
12954 bmap = isl_basic_map_simplify(bmap);
12955 return isl_basic_map_finalize(bmap);
12956 error:
12957 isl_space_free(space);
12958 isl_mat_free(eq);
12959 isl_mat_free(ineq);
12960 isl_basic_map_free(bmap);
12961 return NULL;
12964 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12965 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12966 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12968 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12969 c1, c2, c3, c4, isl_dim_in);
12972 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12973 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12974 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12976 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12977 c1, c2, c3, c4, isl_dim_in);
12980 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12981 __isl_take isl_space *space,
12982 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12983 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12985 isl_basic_map *bmap;
12986 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12987 c1, c2, c3, c4, isl_dim_in);
12988 return bset_from_bmap(bmap);
12991 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12993 if (!bmap)
12994 return isl_bool_error;
12996 return isl_space_can_zip(bmap->dim);
12999 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13001 if (!map)
13002 return isl_bool_error;
13004 return isl_space_can_zip(map->dim);
13007 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13008 * (A -> C) -> (B -> D).
13010 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13012 unsigned pos;
13013 isl_size n_in;
13014 isl_size n1;
13015 isl_size n2;
13017 if (!bmap)
13018 return NULL;
13020 if (!isl_basic_map_can_zip(bmap))
13021 isl_die(bmap->ctx, isl_error_invalid,
13022 "basic map cannot be zipped", goto error);
13023 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13024 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13025 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13026 if (n_in < 0 || n1 < 0 || n2 < 0)
13027 return isl_basic_map_free(bmap);
13028 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13029 bmap = isl_basic_map_cow(bmap);
13030 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13031 if (!bmap)
13032 return NULL;
13033 bmap->dim = isl_space_zip(bmap->dim);
13034 if (!bmap->dim)
13035 goto error;
13036 bmap = isl_basic_map_mark_final(bmap);
13037 return bmap;
13038 error:
13039 isl_basic_map_free(bmap);
13040 return NULL;
13043 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13044 * (A -> C) -> (B -> D).
13046 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13048 if (!map)
13049 return NULL;
13051 if (!isl_map_can_zip(map))
13052 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13053 goto error);
13055 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13056 error:
13057 isl_map_free(map);
13058 return NULL;
13061 /* Can we apply isl_basic_map_curry to "bmap"?
13062 * That is, does it have a nested relation in its domain?
13064 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13066 if (!bmap)
13067 return isl_bool_error;
13069 return isl_space_can_curry(bmap->dim);
13072 /* Can we apply isl_map_curry to "map"?
13073 * That is, does it have a nested relation in its domain?
13075 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13077 if (!map)
13078 return isl_bool_error;
13080 return isl_space_can_curry(map->dim);
13083 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13084 * A -> (B -> C).
13086 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13089 if (!bmap)
13090 return NULL;
13092 if (!isl_basic_map_can_curry(bmap))
13093 isl_die(bmap->ctx, isl_error_invalid,
13094 "basic map cannot be curried", goto error);
13095 bmap = isl_basic_map_cow(bmap);
13096 if (!bmap)
13097 return NULL;
13098 bmap->dim = isl_space_curry(bmap->dim);
13099 if (!bmap->dim)
13100 goto error;
13101 bmap = isl_basic_map_mark_final(bmap);
13102 return bmap;
13103 error:
13104 isl_basic_map_free(bmap);
13105 return NULL;
13108 /* Given a map (A -> B) -> C, return the corresponding map
13109 * A -> (B -> C).
13111 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13113 return isl_map_change_space(map, &isl_map_can_curry,
13114 "map cannot be curried", &isl_space_curry);
13117 /* Can isl_map_range_curry be applied to "map"?
13118 * That is, does it have a nested relation in its range,
13119 * the domain of which is itself a nested relation?
13121 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13123 if (!map)
13124 return isl_bool_error;
13126 return isl_space_can_range_curry(map->dim);
13129 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13130 * A -> (B -> (C -> D)).
13132 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13134 return isl_map_change_space(map, &isl_map_can_range_curry,
13135 "map range cannot be curried",
13136 &isl_space_range_curry);
13139 /* Can we apply isl_basic_map_uncurry to "bmap"?
13140 * That is, does it have a nested relation in its domain?
13142 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13144 if (!bmap)
13145 return isl_bool_error;
13147 return isl_space_can_uncurry(bmap->dim);
13150 /* Can we apply isl_map_uncurry to "map"?
13151 * That is, does it have a nested relation in its domain?
13153 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13155 if (!map)
13156 return isl_bool_error;
13158 return isl_space_can_uncurry(map->dim);
13161 /* Given a basic map A -> (B -> C), return the corresponding basic map
13162 * (A -> B) -> C.
13164 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13167 if (!bmap)
13168 return NULL;
13170 if (!isl_basic_map_can_uncurry(bmap))
13171 isl_die(bmap->ctx, isl_error_invalid,
13172 "basic map cannot be uncurried",
13173 return isl_basic_map_free(bmap));
13174 bmap = isl_basic_map_cow(bmap);
13175 if (!bmap)
13176 return NULL;
13177 bmap->dim = isl_space_uncurry(bmap->dim);
13178 if (!bmap->dim)
13179 return isl_basic_map_free(bmap);
13180 bmap = isl_basic_map_mark_final(bmap);
13181 return bmap;
13184 /* Given a map A -> (B -> C), return the corresponding map
13185 * (A -> B) -> C.
13187 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13189 return isl_map_change_space(map, &isl_map_can_uncurry,
13190 "map cannot be uncurried", &isl_space_uncurry);
13193 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13194 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13196 return isl_map_equate(set, type1, pos1, type2, pos2);
13199 /* Construct a basic map where the given dimensions are equal to each other.
13201 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13202 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13204 isl_basic_map *bmap = NULL;
13205 int i;
13206 isl_size total;
13208 total = isl_space_dim(space, isl_dim_all);
13209 if (total < 0 ||
13210 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13211 isl_space_check_range(space, type2, pos2, 1) < 0)
13212 goto error;
13214 if (type1 == type2 && pos1 == pos2)
13215 return isl_basic_map_universe(space);
13217 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13218 i = isl_basic_map_alloc_equality(bmap);
13219 if (i < 0)
13220 goto error;
13221 isl_seq_clr(bmap->eq[i], 1 + total);
13222 pos1 += isl_basic_map_offset(bmap, type1);
13223 pos2 += isl_basic_map_offset(bmap, type2);
13224 isl_int_set_si(bmap->eq[i][pos1], -1);
13225 isl_int_set_si(bmap->eq[i][pos2], 1);
13226 bmap = isl_basic_map_finalize(bmap);
13227 isl_space_free(space);
13228 return bmap;
13229 error:
13230 isl_space_free(space);
13231 isl_basic_map_free(bmap);
13232 return NULL;
13235 /* Add a constraint imposing that the given two dimensions are equal.
13237 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13238 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13240 isl_basic_map *eq;
13242 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13244 bmap = isl_basic_map_intersect(bmap, eq);
13246 return bmap;
13249 /* Add a constraint imposing that the given two dimensions are equal.
13251 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13252 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13254 isl_basic_map *bmap;
13256 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13258 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13260 return map;
13263 /* Add a constraint imposing that the given two dimensions have opposite values.
13265 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13266 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13268 isl_basic_map *bmap = NULL;
13269 int i;
13270 isl_size total;
13272 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13273 return isl_map_free(map);
13274 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13275 return isl_map_free(map);
13277 total = isl_map_dim(map, isl_dim_all);
13278 if (total < 0)
13279 return isl_map_free(map);
13280 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13281 i = isl_basic_map_alloc_equality(bmap);
13282 if (i < 0)
13283 goto error;
13284 isl_seq_clr(bmap->eq[i], 1 + total);
13285 pos1 += isl_basic_map_offset(bmap, type1);
13286 pos2 += isl_basic_map_offset(bmap, type2);
13287 isl_int_set_si(bmap->eq[i][pos1], 1);
13288 isl_int_set_si(bmap->eq[i][pos2], 1);
13289 bmap = isl_basic_map_finalize(bmap);
13291 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13293 return map;
13294 error:
13295 isl_basic_map_free(bmap);
13296 isl_map_free(map);
13297 return NULL;
13300 /* Construct a constraint imposing that the value of the first dimension is
13301 * greater than or equal to that of the second.
13303 static __isl_give isl_constraint *constraint_order_ge(
13304 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13305 enum isl_dim_type type2, int pos2)
13307 isl_constraint *c;
13309 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13310 isl_space_check_range(space, type2, pos2, 1) < 0)
13311 space = isl_space_free(space);
13312 if (!space)
13313 return NULL;
13315 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13317 if (type1 == type2 && pos1 == pos2)
13318 return c;
13320 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13321 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13323 return c;
13326 /* Add a constraint imposing that the value of the first dimension is
13327 * greater than or equal to that of the second.
13329 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13330 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13332 isl_constraint *c;
13333 isl_space *space;
13335 if (type1 == type2 && pos1 == pos2)
13336 return bmap;
13337 space = isl_basic_map_get_space(bmap);
13338 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13339 bmap = isl_basic_map_add_constraint(bmap, c);
13341 return bmap;
13344 /* Add a constraint imposing that the value of the first dimension is
13345 * greater than or equal to that of the second.
13347 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13348 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13350 isl_constraint *c;
13351 isl_space *space;
13353 if (type1 == type2 && pos1 == pos2)
13354 return map;
13355 space = isl_map_get_space(map);
13356 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13357 map = isl_map_add_constraint(map, c);
13359 return map;
13362 /* Add a constraint imposing that the value of the first dimension is
13363 * less than or equal to that of the second.
13365 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13366 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13368 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13371 /* Construct a basic map where the value of the first dimension is
13372 * greater than that of the second.
13374 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13377 isl_basic_map *bmap = NULL;
13378 int i;
13379 isl_size total;
13381 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13382 isl_space_check_range(space, type2, pos2, 1) < 0)
13383 goto error;
13385 if (type1 == type2 && pos1 == pos2)
13386 return isl_basic_map_empty(space);
13388 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13389 total = isl_basic_map_dim(bmap, isl_dim_all);
13390 i = isl_basic_map_alloc_inequality(bmap);
13391 if (total < 0 || i < 0)
13392 return isl_basic_map_free(bmap);
13393 isl_seq_clr(bmap->ineq[i], 1 + total);
13394 pos1 += isl_basic_map_offset(bmap, type1);
13395 pos2 += isl_basic_map_offset(bmap, type2);
13396 isl_int_set_si(bmap->ineq[i][pos1], 1);
13397 isl_int_set_si(bmap->ineq[i][pos2], -1);
13398 isl_int_set_si(bmap->ineq[i][0], -1);
13399 bmap = isl_basic_map_finalize(bmap);
13401 return bmap;
13402 error:
13403 isl_space_free(space);
13404 isl_basic_map_free(bmap);
13405 return NULL;
13408 /* Add a constraint imposing that the value of the first dimension is
13409 * greater than that of the second.
13411 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13412 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13414 isl_basic_map *gt;
13416 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13418 bmap = isl_basic_map_intersect(bmap, gt);
13420 return bmap;
13423 /* Add a constraint imposing that the value of the first dimension is
13424 * greater than that of the second.
13426 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13427 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13429 isl_basic_map *bmap;
13431 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13433 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13435 return map;
13438 /* Add a constraint imposing that the value of the first dimension is
13439 * smaller than that of the second.
13441 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13442 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13444 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13447 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13448 int pos)
13450 isl_aff *div;
13451 isl_local_space *ls;
13453 if (!bmap)
13454 return NULL;
13456 if (!isl_basic_map_divs_known(bmap))
13457 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13458 "some divs are unknown", return NULL);
13460 ls = isl_basic_map_get_local_space(bmap);
13461 div = isl_local_space_get_div(ls, pos);
13462 isl_local_space_free(ls);
13464 return div;
13467 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13468 int pos)
13470 return isl_basic_map_get_div(bset, pos);
13473 /* Plug in "subs" for set dimension "pos" of "set".
13475 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13476 unsigned pos, __isl_keep isl_aff *subs)
13478 isl_multi_aff *ma;
13480 if (set && isl_set_plain_is_empty(set))
13481 return set;
13483 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
13484 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
13485 return isl_set_preimage_multi_aff(set, ma);
13488 /* Check if the range of "ma" is compatible with the domain or range
13489 * (depending on "type") of "bmap".
13491 static isl_stat check_basic_map_compatible_range_multi_aff(
13492 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13493 __isl_keep isl_multi_aff *ma)
13495 isl_bool m;
13496 isl_space *ma_space;
13498 ma_space = isl_multi_aff_get_space(ma);
13500 m = isl_space_has_equal_params(bmap->dim, ma_space);
13501 if (m < 0)
13502 goto error;
13503 if (!m)
13504 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13505 "parameters don't match", goto error);
13506 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13507 if (m < 0)
13508 goto error;
13509 if (!m)
13510 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13511 "spaces don't match", goto error);
13513 isl_space_free(ma_space);
13514 return isl_stat_ok;
13515 error:
13516 isl_space_free(ma_space);
13517 return isl_stat_error;
13520 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13521 * coefficients before the transformed range of dimensions,
13522 * the "n_after" coefficients after the transformed range of dimensions
13523 * and the coefficients of the other divs in "bmap".
13525 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13526 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13528 int i;
13529 isl_size n_param;
13530 isl_size n_set;
13531 isl_local_space *ls;
13533 if (n_div == 0)
13534 return bmap;
13536 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13537 n_param = isl_local_space_dim(ls, isl_dim_param);
13538 n_set = isl_local_space_dim(ls, isl_dim_set);
13539 if (n_param < 0 || n_set < 0)
13540 return isl_basic_map_free(bmap);
13542 for (i = 0; i < n_div; ++i) {
13543 int o_bmap = 0, o_ls = 0;
13545 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13546 o_bmap += 1 + 1 + n_param;
13547 o_ls += 1 + 1 + n_param;
13548 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13549 o_bmap += n_before;
13550 isl_seq_cpy(bmap->div[i] + o_bmap,
13551 ls->div->row[i] + o_ls, n_set);
13552 o_bmap += n_set;
13553 o_ls += n_set;
13554 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13555 o_bmap += n_after;
13556 isl_seq_cpy(bmap->div[i] + o_bmap,
13557 ls->div->row[i] + o_ls, n_div);
13558 o_bmap += n_div;
13559 o_ls += n_div;
13560 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13561 bmap = isl_basic_map_add_div_constraints(bmap, i);
13562 if (!bmap)
13563 goto error;
13566 isl_local_space_free(ls);
13567 return bmap;
13568 error:
13569 isl_local_space_free(ls);
13570 return isl_basic_map_free(bmap);
13573 /* How many stride constraints does "ma" enforce?
13574 * That is, how many of the affine expressions have a denominator
13575 * different from one?
13577 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13579 int i;
13580 int strides = 0;
13582 for (i = 0; i < ma->n; ++i)
13583 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13584 strides++;
13586 return strides;
13589 /* For each affine expression in ma of the form
13591 * x_i = (f_i y + h_i)/m_i
13593 * with m_i different from one, add a constraint to "bmap"
13594 * of the form
13596 * f_i y + h_i = m_i alpha_i
13598 * with alpha_i an additional existentially quantified variable.
13600 * The input variables of "ma" correspond to a subset of the variables
13601 * of "bmap". There are "n_before" variables in "bmap" before this
13602 * subset and "n_after" variables after this subset.
13603 * The integer divisions of the affine expressions in "ma" are assumed
13604 * to have been aligned. There are "n_div_ma" of them and
13605 * they appear first in "bmap", straight after the "n_after" variables.
13607 static __isl_give isl_basic_map *add_ma_strides(
13608 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13609 int n_before, int n_after, int n_div_ma)
13611 int i, k;
13612 int div;
13613 isl_size total;
13614 isl_size n_param;
13615 isl_size n_in;
13617 total = isl_basic_map_dim(bmap, isl_dim_all);
13618 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13619 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13620 if (total < 0 || n_param < 0 || n_in < 0)
13621 return isl_basic_map_free(bmap);
13622 for (i = 0; i < ma->n; ++i) {
13623 int o_bmap = 0, o_ma = 1;
13625 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13626 continue;
13627 div = isl_basic_map_alloc_div(bmap);
13628 k = isl_basic_map_alloc_equality(bmap);
13629 if (div < 0 || k < 0)
13630 goto error;
13631 isl_int_set_si(bmap->div[div][0], 0);
13632 isl_seq_cpy(bmap->eq[k] + o_bmap,
13633 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13634 o_bmap += 1 + n_param;
13635 o_ma += 1 + n_param;
13636 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13637 o_bmap += n_before;
13638 isl_seq_cpy(bmap->eq[k] + o_bmap,
13639 ma->u.p[i]->v->el + o_ma, n_in);
13640 o_bmap += n_in;
13641 o_ma += n_in;
13642 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13643 o_bmap += n_after;
13644 isl_seq_cpy(bmap->eq[k] + o_bmap,
13645 ma->u.p[i]->v->el + o_ma, n_div_ma);
13646 o_bmap += n_div_ma;
13647 o_ma += n_div_ma;
13648 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13649 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13650 total++;
13653 return bmap;
13654 error:
13655 isl_basic_map_free(bmap);
13656 return NULL;
13659 /* Replace the domain or range space (depending on "type) of "space" by "set".
13661 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13662 enum isl_dim_type type, __isl_take isl_space *set)
13664 if (type == isl_dim_in) {
13665 space = isl_space_range(space);
13666 space = isl_space_map_from_domain_and_range(set, space);
13667 } else {
13668 space = isl_space_domain(space);
13669 space = isl_space_map_from_domain_and_range(space, set);
13672 return space;
13675 /* Compute the preimage of the domain or range (depending on "type")
13676 * of "bmap" under the function represented by "ma".
13677 * In other words, plug in "ma" in the domain or range of "bmap".
13678 * The result is a basic map that lives in the same space as "bmap"
13679 * except that the domain or range has been replaced by
13680 * the domain space of "ma".
13682 * If bmap is represented by
13684 * A(p) + S u + B x + T v + C(divs) >= 0,
13686 * where u and x are input and output dimensions if type == isl_dim_out
13687 * while x and v are input and output dimensions if type == isl_dim_in,
13688 * and ma is represented by
13690 * x = D(p) + F(y) + G(divs')
13692 * then the result is
13694 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13696 * The divs in the input set are similarly adjusted.
13697 * In particular
13699 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13701 * becomes
13703 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13704 * B_i G(divs') + c_i(divs))/n_i)
13706 * If bmap is not a rational map and if F(y) involves any denominators
13708 * x_i = (f_i y + h_i)/m_i
13710 * then additional constraints are added to ensure that we only
13711 * map back integer points. That is we enforce
13713 * f_i y + h_i = m_i alpha_i
13715 * with alpha_i an additional existentially quantified variable.
13717 * We first copy over the divs from "ma".
13718 * Then we add the modified constraints and divs from "bmap".
13719 * Finally, we add the stride constraints, if needed.
13721 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13722 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13723 __isl_take isl_multi_aff *ma)
13725 int i, k;
13726 isl_space *space;
13727 isl_basic_map *res = NULL;
13728 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13729 isl_int f, c1, c2, g;
13730 isl_bool rational;
13731 int strides;
13733 isl_int_init(f);
13734 isl_int_init(c1);
13735 isl_int_init(c2);
13736 isl_int_init(g);
13738 ma = isl_multi_aff_align_divs(ma);
13739 if (!bmap || !ma)
13740 goto error;
13741 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13742 goto error;
13744 if (type == isl_dim_in) {
13745 n_before = 0;
13746 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13747 } else {
13748 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13749 n_after = 0;
13751 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13752 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13753 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13754 goto error;
13756 space = isl_multi_aff_get_domain_space(ma);
13757 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13758 rational = isl_basic_map_is_rational(bmap);
13759 strides = rational ? 0 : multi_aff_strides(ma);
13760 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13761 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13762 if (rational)
13763 res = isl_basic_map_set_rational(res);
13765 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13766 if (isl_basic_map_alloc_div(res) < 0)
13767 goto error;
13769 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13770 if (!res)
13771 goto error;
13773 for (i = 0; i < bmap->n_eq; ++i) {
13774 k = isl_basic_map_alloc_equality(res);
13775 if (k < 0)
13776 goto error;
13777 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13778 n_after, n_div_ma, n_div_bmap,
13779 f, c1, c2, g, 0) < 0)
13780 goto error;
13783 for (i = 0; i < bmap->n_ineq; ++i) {
13784 k = isl_basic_map_alloc_inequality(res);
13785 if (k < 0)
13786 goto error;
13787 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13788 n_after, n_div_ma, n_div_bmap,
13789 f, c1, c2, g, 0) < 0)
13790 goto error;
13793 for (i = 0; i < bmap->n_div; ++i) {
13794 if (isl_int_is_zero(bmap->div[i][0])) {
13795 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13796 continue;
13798 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13799 n_before, n_after, n_div_ma, n_div_bmap,
13800 f, c1, c2, g, 1) < 0)
13801 goto error;
13804 if (strides)
13805 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13807 isl_int_clear(f);
13808 isl_int_clear(c1);
13809 isl_int_clear(c2);
13810 isl_int_clear(g);
13811 isl_basic_map_free(bmap);
13812 isl_multi_aff_free(ma);
13813 res = isl_basic_map_simplify(res);
13814 return isl_basic_map_finalize(res);
13815 error:
13816 isl_int_clear(f);
13817 isl_int_clear(c1);
13818 isl_int_clear(c2);
13819 isl_int_clear(g);
13820 isl_basic_map_free(bmap);
13821 isl_multi_aff_free(ma);
13822 isl_basic_map_free(res);
13823 return NULL;
13826 /* Compute the preimage of "bset" under the function represented by "ma".
13827 * In other words, plug in "ma" in "bset". The result is a basic set
13828 * that lives in the domain space of "ma".
13830 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13831 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13833 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13836 /* Compute the preimage of the domain of "bmap" under the function
13837 * represented by "ma".
13838 * In other words, plug in "ma" in the domain of "bmap".
13839 * The result is a basic map that lives in the same space as "bmap"
13840 * except that the domain has been replaced by the domain space of "ma".
13842 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13843 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13845 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13848 /* Compute the preimage of the range of "bmap" under the function
13849 * represented by "ma".
13850 * In other words, plug in "ma" in the range of "bmap".
13851 * The result is a basic map that lives in the same space as "bmap"
13852 * except that the range has been replaced by the domain space of "ma".
13854 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13855 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13857 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13860 /* Check if the range of "ma" is compatible with the domain or range
13861 * (depending on "type") of "map".
13862 * Return isl_stat_error if anything is wrong.
13864 static isl_stat check_map_compatible_range_multi_aff(
13865 __isl_keep isl_map *map, enum isl_dim_type type,
13866 __isl_keep isl_multi_aff *ma)
13868 isl_bool m;
13869 isl_space *ma_space;
13871 ma_space = isl_multi_aff_get_space(ma);
13872 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13873 isl_space_free(ma_space);
13874 if (m < 0)
13875 return isl_stat_error;
13876 if (!m)
13877 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13878 "spaces don't match", return isl_stat_error);
13879 return isl_stat_ok;
13882 /* Compute the preimage of the domain or range (depending on "type")
13883 * of "map" under the function represented by "ma".
13884 * In other words, plug in "ma" in the domain or range of "map".
13885 * The result is a map that lives in the same space as "map"
13886 * except that the domain or range has been replaced by
13887 * the domain space of "ma".
13889 * The parameters are assumed to have been aligned.
13891 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13892 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13894 int i;
13895 isl_space *space;
13897 map = isl_map_cow(map);
13898 ma = isl_multi_aff_align_divs(ma);
13899 if (!map || !ma)
13900 goto error;
13901 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13902 goto error;
13904 for (i = 0; i < map->n; ++i) {
13905 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13906 isl_multi_aff_copy(ma));
13907 if (!map->p[i])
13908 goto error;
13911 space = isl_multi_aff_get_domain_space(ma);
13912 space = isl_space_set(isl_map_get_space(map), type, space);
13914 isl_space_free(isl_map_take_space(map));
13915 map = isl_map_restore_space(map, space);
13916 if (!map)
13917 goto error;
13919 isl_multi_aff_free(ma);
13920 if (map->n > 1)
13921 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13922 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13923 return map;
13924 error:
13925 isl_multi_aff_free(ma);
13926 isl_map_free(map);
13927 return NULL;
13930 /* Compute the preimage of the domain or range (depending on "type")
13931 * of "map" under the function represented by "ma".
13932 * In other words, plug in "ma" in the domain or range of "map".
13933 * The result is a map that lives in the same space as "map"
13934 * except that the domain or range has been replaced by
13935 * the domain space of "ma".
13937 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13938 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13940 isl_bool aligned;
13942 if (!map || !ma)
13943 goto error;
13945 aligned = isl_map_space_has_equal_params(map, ma->space);
13946 if (aligned < 0)
13947 goto error;
13948 if (aligned)
13949 return map_preimage_multi_aff(map, type, ma);
13951 if (isl_map_check_named_params(map) < 0)
13952 goto error;
13953 if (!isl_space_has_named_params(ma->space))
13954 isl_die(map->ctx, isl_error_invalid,
13955 "unaligned unnamed parameters", goto error);
13956 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13957 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13959 return map_preimage_multi_aff(map, type, ma);
13960 error:
13961 isl_multi_aff_free(ma);
13962 return isl_map_free(map);
13965 /* Compute the preimage of "set" under the function represented by "ma".
13966 * In other words, plug in "ma" in "set". The result is a set
13967 * that lives in the domain space of "ma".
13969 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13970 __isl_take isl_multi_aff *ma)
13972 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13975 /* Compute the preimage of the domain of "map" under the function
13976 * represented by "ma".
13977 * In other words, plug in "ma" in the domain of "map".
13978 * The result is a map that lives in the same space as "map"
13979 * except that the domain has been replaced by the domain space of "ma".
13981 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13982 __isl_take isl_multi_aff *ma)
13984 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13987 /* Compute the preimage of the range of "map" under the function
13988 * represented by "ma".
13989 * In other words, plug in "ma" in the range of "map".
13990 * The result is a map that lives in the same space as "map"
13991 * except that the range has been replaced by the domain space of "ma".
13993 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13994 __isl_take isl_multi_aff *ma)
13996 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13999 /* Compute the preimage of "map" under the function represented by "pma".
14000 * In other words, plug in "pma" in the domain or range of "map".
14001 * The result is a map that lives in the same space as "map",
14002 * except that the space of type "type" has been replaced by
14003 * the domain space of "pma".
14005 * The parameters of "map" and "pma" are assumed to have been aligned.
14007 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14008 __isl_take isl_map *map, enum isl_dim_type type,
14009 __isl_take isl_pw_multi_aff *pma)
14011 int i;
14012 isl_map *res;
14014 if (!pma)
14015 goto error;
14017 if (pma->n == 0) {
14018 isl_pw_multi_aff_free(pma);
14019 res = isl_map_empty(isl_map_get_space(map));
14020 isl_map_free(map);
14021 return res;
14024 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14025 isl_multi_aff_copy(pma->p[0].maff));
14026 if (type == isl_dim_in)
14027 res = isl_map_intersect_domain(res,
14028 isl_map_copy(pma->p[0].set));
14029 else
14030 res = isl_map_intersect_range(res,
14031 isl_map_copy(pma->p[0].set));
14033 for (i = 1; i < pma->n; ++i) {
14034 isl_map *res_i;
14036 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14037 isl_multi_aff_copy(pma->p[i].maff));
14038 if (type == isl_dim_in)
14039 res_i = isl_map_intersect_domain(res_i,
14040 isl_map_copy(pma->p[i].set));
14041 else
14042 res_i = isl_map_intersect_range(res_i,
14043 isl_map_copy(pma->p[i].set));
14044 res = isl_map_union(res, res_i);
14047 isl_pw_multi_aff_free(pma);
14048 isl_map_free(map);
14049 return res;
14050 error:
14051 isl_pw_multi_aff_free(pma);
14052 isl_map_free(map);
14053 return NULL;
14056 /* Compute the preimage of "map" under the function represented by "pma".
14057 * In other words, plug in "pma" in the domain or range of "map".
14058 * The result is a map that lives in the same space as "map",
14059 * except that the space of type "type" has been replaced by
14060 * the domain space of "pma".
14062 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14063 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14065 isl_bool aligned;
14067 if (!map || !pma)
14068 goto error;
14070 aligned = isl_map_space_has_equal_params(map, pma->dim);
14071 if (aligned < 0)
14072 goto error;
14073 if (aligned)
14074 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14076 if (isl_map_check_named_params(map) < 0)
14077 goto error;
14078 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14079 goto error;
14080 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14081 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14083 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14084 error:
14085 isl_pw_multi_aff_free(pma);
14086 return isl_map_free(map);
14089 /* Compute the preimage of "set" under the function represented by "pma".
14090 * In other words, plug in "pma" in "set". The result is a set
14091 * that lives in the domain space of "pma".
14093 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14094 __isl_take isl_pw_multi_aff *pma)
14096 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14099 /* Compute the preimage of the domain of "map" under the function
14100 * represented by "pma".
14101 * In other words, plug in "pma" in the domain of "map".
14102 * The result is a map that lives in the same space as "map",
14103 * except that domain space has been replaced by the domain space of "pma".
14105 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14106 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14108 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14111 /* Compute the preimage of the range of "map" under the function
14112 * represented by "pma".
14113 * In other words, plug in "pma" in the range of "map".
14114 * The result is a map that lives in the same space as "map",
14115 * except that range space has been replaced by the domain space of "pma".
14117 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14118 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14120 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14123 /* Compute the preimage of "map" under the function represented by "mpa".
14124 * In other words, plug in "mpa" in the domain or range of "map".
14125 * The result is a map that lives in the same space as "map",
14126 * except that the space of type "type" has been replaced by
14127 * the domain space of "mpa".
14129 * If the map does not involve any constraints that refer to the
14130 * dimensions of the substituted space, then the only possible
14131 * effect of "mpa" on the map is to map the space to a different space.
14132 * We create a separate isl_multi_aff to effectuate this change
14133 * in order to avoid spurious splitting of the map along the pieces
14134 * of "mpa".
14135 * If "mpa" has a non-trivial explicit domain, however,
14136 * then the full substitution should be performed.
14138 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14139 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14141 isl_size n;
14142 isl_bool full;
14143 isl_pw_multi_aff *pma;
14145 n = isl_map_dim(map, type);
14146 if (n < 0 || !mpa)
14147 goto error;
14149 full = isl_map_involves_dims(map, type, 0, n);
14150 if (full >= 0 && !full)
14151 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14152 if (full < 0)
14153 goto error;
14154 if (!full) {
14155 isl_space *space;
14156 isl_multi_aff *ma;
14158 space = isl_multi_pw_aff_get_space(mpa);
14159 isl_multi_pw_aff_free(mpa);
14160 ma = isl_multi_aff_zero(space);
14161 return isl_map_preimage_multi_aff(map, type, ma);
14164 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14165 return isl_map_preimage_pw_multi_aff(map, type, pma);
14166 error:
14167 isl_map_free(map);
14168 isl_multi_pw_aff_free(mpa);
14169 return NULL;
14172 /* Compute the preimage of "map" under the function represented by "mpa".
14173 * In other words, plug in "mpa" in the domain "map".
14174 * The result is a map that lives in the same space as "map",
14175 * except that domain space has been replaced by the domain space of "mpa".
14177 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14178 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14180 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14183 /* Compute the preimage of "set" by the function represented by "mpa".
14184 * In other words, plug in "mpa" in "set".
14186 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14187 __isl_take isl_multi_pw_aff *mpa)
14189 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14192 /* Return a copy of the equality constraints of "bset" as a matrix.
14194 __isl_give isl_mat *isl_basic_set_extract_equalities(
14195 __isl_keep isl_basic_set *bset)
14197 isl_ctx *ctx;
14198 isl_size total;
14200 total = isl_basic_set_dim(bset, isl_dim_all);
14201 if (total < 0)
14202 return NULL;
14204 ctx = isl_basic_set_get_ctx(bset);
14205 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14208 /* Are the "n" "coefficients" starting at "first" of the integer division
14209 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14210 * to each other?
14211 * The "coefficient" at position 0 is the denominator.
14212 * The "coefficient" at position 1 is the constant term.
14214 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14215 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14216 unsigned first, unsigned n)
14218 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14219 return isl_bool_error;
14220 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14221 return isl_bool_error;
14222 return isl_seq_eq(bmap1->div[pos1] + first,
14223 bmap2->div[pos2] + first, n);
14226 /* Are the integer division expressions at position "pos1" in "bmap1" and
14227 * "pos2" in "bmap2" equal to each other, except that the constant terms
14228 * are different?
14230 isl_bool isl_basic_map_equal_div_expr_except_constant(
14231 __isl_keep isl_basic_map *bmap1, int pos1,
14232 __isl_keep isl_basic_map *bmap2, int pos2)
14234 isl_bool equal;
14235 isl_size total, total2;
14237 total = isl_basic_map_dim(bmap1, isl_dim_all);
14238 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14239 if (total < 0 || total2 < 0)
14240 return isl_bool_error;
14241 if (total != total2)
14242 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14243 "incomparable div expressions", return isl_bool_error);
14244 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14245 0, 1);
14246 if (equal < 0 || !equal)
14247 return equal;
14248 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14249 1, 1);
14250 if (equal < 0 || equal)
14251 return isl_bool_not(equal);
14252 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14253 2, total);
14256 /* Replace the numerator of the constant term of the integer division
14257 * expression at position "div" in "bmap" by "value".
14258 * The caller guarantees that this does not change the meaning
14259 * of the input.
14261 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14262 __isl_take isl_basic_map *bmap, int div, int value)
14264 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14265 return isl_basic_map_free(bmap);
14267 isl_int_set_si(bmap->div[div][1], value);
14269 return bmap;
14272 /* Is the point "inner" internal to inequality constraint "ineq"
14273 * of "bset"?
14274 * The point is considered to be internal to the inequality constraint,
14275 * if it strictly lies on the positive side of the inequality constraint,
14276 * or if it lies on the constraint and the constraint is lexico-positive.
14278 static isl_bool is_internal(__isl_keep isl_vec *inner,
14279 __isl_keep isl_basic_set *bset, int ineq)
14281 isl_ctx *ctx;
14282 int pos;
14283 isl_size total;
14285 if (!inner || !bset)
14286 return isl_bool_error;
14288 ctx = isl_basic_set_get_ctx(bset);
14289 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14290 &ctx->normalize_gcd);
14291 if (!isl_int_is_zero(ctx->normalize_gcd))
14292 return isl_int_is_nonneg(ctx->normalize_gcd);
14294 total = isl_basic_set_dim(bset, isl_dim_all);
14295 if (total < 0)
14296 return isl_bool_error;
14297 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14298 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14301 /* Tighten the inequality constraints of "bset" that are outward with respect
14302 * to the point "vec".
14303 * That is, tighten the constraints that are not satisfied by "vec".
14305 * "vec" is a point internal to some superset S of "bset" that is used
14306 * to make the subsets of S disjoint, by tightening one half of the constraints
14307 * that separate two subsets. In particular, the constraints of S
14308 * are all satisfied by "vec" and should not be tightened.
14309 * Of the internal constraints, those that have "vec" on the outside
14310 * are tightened. The shared facet is included in the adjacent subset
14311 * with the opposite constraint.
14312 * For constraints that saturate "vec", this criterion cannot be used
14313 * to determine which of the two sides should be tightened.
14314 * Instead, the sign of the first non-zero coefficient is used
14315 * to make this choice. Note that this second criterion is never used
14316 * on the constraints of S since "vec" is interior to "S".
14318 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14319 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14321 int j;
14323 bset = isl_basic_set_cow(bset);
14324 if (!bset)
14325 return NULL;
14326 for (j = 0; j < bset->n_ineq; ++j) {
14327 isl_bool internal;
14329 internal = is_internal(vec, bset, j);
14330 if (internal < 0)
14331 return isl_basic_set_free(bset);
14332 if (internal)
14333 continue;
14334 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14337 return bset;
14340 /* Replace the variables x of type "type" starting at "first" in "bmap"
14341 * by x' with x = M x' with M the matrix trans.
14342 * That is, replace the corresponding coefficients c by c M.
14344 * The transformation matrix should be a square matrix.
14346 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14347 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14348 __isl_take isl_mat *trans)
14350 unsigned pos;
14352 bmap = isl_basic_map_cow(bmap);
14353 if (!bmap || !trans)
14354 goto error;
14356 if (trans->n_row != trans->n_col)
14357 isl_die(trans->ctx, isl_error_invalid,
14358 "expecting square transformation matrix", goto error);
14359 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14360 goto error;
14362 pos = isl_basic_map_offset(bmap, type) + first;
14364 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14365 isl_mat_copy(trans)) < 0)
14366 goto error;
14367 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14368 isl_mat_copy(trans)) < 0)
14369 goto error;
14370 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14371 isl_mat_copy(trans)) < 0)
14372 goto error;
14374 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14375 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14377 isl_mat_free(trans);
14378 return bmap;
14379 error:
14380 isl_mat_free(trans);
14381 isl_basic_map_free(bmap);
14382 return NULL;
14385 /* Replace the variables x of type "type" starting at "first" in "bset"
14386 * by x' with x = M x' with M the matrix trans.
14387 * That is, replace the corresponding coefficients c by c M.
14389 * The transformation matrix should be a square matrix.
14391 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14392 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14393 __isl_take isl_mat *trans)
14395 return isl_basic_map_transform_dims(bset, type, first, trans);