detect more forms of modulo expressions when extracting a function
[isl.git] / isl_map.c
blobc4f80f48b90e514e662d9e7963980476998f8f43
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2021-2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
23 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
26 #include <string.h>
27 #include <isl_ctx_private.h>
28 #include <isl_map_private.h>
29 #include <isl_blk.h>
30 #include <isl_id_private.h>
31 #include <isl/constraint.h>
32 #include "isl_space_private.h"
33 #include "isl_equalities.h"
34 #include <isl_lp_private.h>
35 #include <isl_seq.h>
36 #include <isl/set.h>
37 #include <isl/map.h>
38 #include <isl_reordering.h>
39 #include "isl_sample.h"
40 #include <isl_sort.h>
41 #include "isl_tab.h"
42 #include <isl/vec.h>
43 #include <isl_mat_private.h>
44 #include <isl_vec_private.h>
45 #include <isl_dim_map.h>
46 #include <isl_local_space_private.h>
47 #include <isl_aff_private.h>
48 #include <isl_options_private.h>
49 #include <isl_morph.h>
50 #include <isl_val_private.h>
51 #include <isl_printer_private.h>
52 #include <isl_maybe_aff.h>
54 #include <bset_to_bmap.c>
55 #include <bset_from_bmap.c>
56 #include <set_to_map.c>
57 #include <set_from_map.c>
59 /* Treat "bset" as a basic map.
60 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
61 * this function performs a redundant cast.
63 static __isl_keep const isl_basic_map *const_bset_to_bmap(
64 __isl_keep const isl_basic_set *bset)
66 return (const isl_basic_map *) bset;
69 #undef TYPE
70 #define TYPE isl_basic_map
71 #include "has_single_reference_templ.c"
73 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
75 switch (type) {
76 case isl_dim_param: return 1;
77 case isl_dim_in: return 1 + space->nparam;
78 case isl_dim_out: return 1 + space->nparam + space->n_in;
79 default: return 0;
83 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
84 enum isl_dim_type type)
86 if (!bmap)
87 return isl_size_error;
88 switch (type) {
89 case isl_dim_cst: return 1;
90 case isl_dim_param:
91 case isl_dim_in:
92 case isl_dim_out: return isl_space_dim(bmap->dim, type);
93 case isl_dim_div: return bmap->n_div;
94 case isl_dim_all: return isl_basic_map_total_dim(bmap);
95 default: return 0;
99 /* Return the space of "map".
101 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
103 return map ? map->dim : NULL;
106 /* Return the space of "set".
108 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
110 return isl_map_peek_space(set_to_map(set));
113 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
115 return isl_space_dim(isl_map_peek_space(map), type);
118 /* Return the dimensionality of the domain (tuple) of the map.
120 isl_size isl_map_domain_tuple_dim(__isl_keep isl_map *map)
122 return isl_map_dim(map, isl_dim_in);
125 /* Return the dimensionality of the range (tuple) of the map.
127 isl_size isl_map_range_tuple_dim(__isl_keep isl_map *map)
129 return isl_map_dim(map, isl_dim_out);
132 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
134 return isl_map_dim(set_to_map(set), type);
137 /* Return the dimensionality of the (tuple of the) set.
139 isl_size isl_set_tuple_dim(__isl_keep isl_set *set)
141 return isl_set_dim(set, isl_dim_set);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bmap".
147 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
148 enum isl_dim_type type)
150 isl_space *space;
152 space = isl_basic_map_peek_space(bmap);
154 switch (type) {
155 case isl_dim_param:
156 case isl_dim_in:
157 case isl_dim_out: return isl_space_offset(space, type);
158 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
159 case isl_dim_cst:
160 default:
161 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
162 "invalid dimension type", return isl_size_error);
166 /* Return the position of the variables of the given type
167 * within the sequence of variables of "bset".
169 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
172 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
175 /* Return the position of the coefficients of the variables of the given type
176 * within the sequence of coefficients of "bmap".
178 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
179 enum isl_dim_type type)
181 switch (type) {
182 case isl_dim_cst: return 0;
183 case isl_dim_param:
184 case isl_dim_in:
185 case isl_dim_out:
186 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
187 default: return 0;
191 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
192 enum isl_dim_type type)
194 return isl_basic_map_offset(bset, type);
197 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
199 return pos(map->dim, type);
202 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
203 enum isl_dim_type type)
205 return isl_basic_map_dim(bset, type);
208 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
210 return isl_basic_set_dim(bset, isl_dim_set);
213 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
215 return isl_basic_set_dim(bset, isl_dim_param);
218 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
220 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
223 isl_size isl_set_n_dim(__isl_keep isl_set *set)
225 return isl_set_dim(set, isl_dim_set);
228 isl_size isl_set_n_param(__isl_keep isl_set *set)
230 return isl_set_dim(set, isl_dim_param);
233 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
235 isl_size dim;
237 if (!bmap)
238 return isl_size_error;
239 dim = isl_space_dim(bmap->dim, isl_dim_all);
240 if (dim < 0)
241 return isl_size_error;
242 return dim + bmap->n_div;
245 /* Return the number of equality constraints in the description of "bmap".
246 * Return isl_size_error on error.
248 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
250 if (!bmap)
251 return isl_size_error;
252 return bmap->n_eq;
255 /* Return the number of equality constraints in the description of "bset".
256 * Return isl_size_error on error.
258 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
260 return isl_basic_map_n_equality(bset_to_bmap(bset));
263 /* Return the number of inequality constraints in the description of "bmap".
264 * Return isl_size_error on error.
266 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
268 if (!bmap)
269 return isl_size_error;
270 return bmap->n_ineq;
273 /* Return the number of inequality constraints in the description of "bset".
274 * Return isl_size_error on error.
276 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
278 return isl_basic_map_n_inequality(bset_to_bmap(bset));
281 /* Do "bmap1" and "bmap2" have the same parameters?
283 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
284 __isl_keep isl_basic_map *bmap2)
286 isl_space *space1, *space2;
288 space1 = isl_basic_map_peek_space(bmap1);
289 space2 = isl_basic_map_peek_space(bmap2);
290 return isl_space_has_equal_params(space1, space2);
293 /* Do "map1" and "map2" have the same parameters?
295 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
296 __isl_keep isl_map *map2)
298 isl_space *space1, *space2;
300 space1 = isl_map_peek_space(map1);
301 space2 = isl_map_peek_space(map2);
302 return isl_space_has_equal_params(space1, space2);
305 /* Do "map" and "set" have the same parameters?
307 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
308 __isl_keep isl_set *set)
310 return isl_map_has_equal_params(map, set_to_map(set));
313 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
315 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
316 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
318 isl_space *bmap_space, *bset_space;
320 bmap_space = isl_basic_map_peek_space(bmap);
321 bset_space = isl_basic_set_peek_space(bset);
322 return isl_space_tuple_is_equal(bmap_space, type,
323 bset_space, isl_dim_set);
326 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
328 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
329 enum isl_dim_type type, __isl_keep isl_set *set)
331 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
334 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
335 __isl_keep isl_set *set)
337 isl_bool m;
338 if (!map || !set)
339 return isl_bool_error;
340 m = isl_map_has_equal_params(map, set_to_map(set));
341 if (m < 0 || !m)
342 return m;
343 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
346 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
347 __isl_keep isl_basic_set *bset)
349 isl_bool m;
350 if (!bmap || !bset)
351 return isl_bool_error;
352 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
353 if (m < 0 || !m)
354 return m;
355 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
358 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
359 __isl_keep isl_set *set)
361 isl_bool m;
362 if (!map || !set)
363 return isl_bool_error;
364 m = isl_map_has_equal_params(map, set_to_map(set));
365 if (m < 0 || !m)
366 return m;
367 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
370 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
371 __isl_keep isl_basic_set *bset)
373 isl_bool m;
374 if (!bmap || !bset)
375 return isl_bool_error;
376 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
377 if (m < 0 || !m)
378 return m;
379 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
382 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
384 return bmap ? bmap->ctx : NULL;
387 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
389 return bset ? bset->ctx : NULL;
392 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
394 return map ? map->ctx : NULL;
397 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
399 return set ? set->ctx : NULL;
402 /* Return the space of "bmap".
404 __isl_keep isl_space *isl_basic_map_peek_space(
405 __isl_keep const isl_basic_map *bmap)
407 return bmap ? bmap->dim : NULL;
410 /* Return the space of "bset".
412 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
414 return isl_basic_map_peek_space(bset_to_bmap(bset));
417 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
419 return isl_space_copy(isl_basic_map_peek_space(bmap));
422 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
424 return isl_basic_map_get_space(bset_to_bmap(bset));
427 /* Return the space of "bmap".
428 * This may be either a copy or the space itself
429 * if there is only one reference to "bmap".
430 * This allows the space to be modified inplace
431 * if both the basic map and its space have only a single reference.
432 * The caller is not allowed to modify "bmap" between this call and
433 * a subsequent call to isl_basic_map_restore_space.
434 * The only exception is that isl_basic_map_free can be called instead.
436 static __isl_give isl_space *isl_basic_map_take_space(
437 __isl_keep isl_basic_map *bmap)
439 isl_space *space;
441 if (!bmap)
442 return NULL;
443 if (bmap->ref != 1)
444 return isl_basic_map_get_space(bmap);
445 space = bmap->dim;
446 bmap->dim = NULL;
447 return space;
450 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
451 * due to a preceding call to isl_basic_map_take_space.
452 * However, in this case, "bmap" only has a single reference and
453 * then the call to isl_basic_map_cow has no effect.
455 static __isl_give isl_basic_map *isl_basic_map_restore_space(
456 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
458 if (!bmap || !space)
459 goto error;
461 if (bmap->dim == space) {
462 isl_space_free(space);
463 return bmap;
466 bmap = isl_basic_map_cow(bmap);
467 if (!bmap)
468 goto error;
469 isl_space_free(bmap->dim);
470 bmap->dim = space;
472 return bmap;
473 error:
474 isl_basic_map_free(bmap);
475 isl_space_free(space);
476 return NULL;
479 /* Extract the divs in "bmap" as a matrix.
481 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
483 int i;
484 isl_ctx *ctx;
485 isl_mat *div;
486 isl_size v_div;
487 unsigned cols;
489 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
490 if (v_div < 0)
491 return NULL;
493 ctx = isl_basic_map_get_ctx(bmap);
494 cols = 1 + 1 + v_div + bmap->n_div;
495 div = isl_mat_alloc(ctx, bmap->n_div, cols);
496 if (!div)
497 return NULL;
499 for (i = 0; i < bmap->n_div; ++i)
500 isl_seq_cpy(div->row[i], bmap->div[i], cols);
502 return div;
505 /* Extract the divs in "bset" as a matrix.
507 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
509 return isl_basic_map_get_divs(bset);
512 __isl_give isl_local_space *isl_basic_map_get_local_space(
513 __isl_keep isl_basic_map *bmap)
515 isl_mat *div;
517 if (!bmap)
518 return NULL;
520 div = isl_basic_map_get_divs(bmap);
521 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
524 __isl_give isl_local_space *isl_basic_set_get_local_space(
525 __isl_keep isl_basic_set *bset)
527 return isl_basic_map_get_local_space(bset);
530 /* For each known div d = floor(f/m), add the constraints
532 * f - m d >= 0
533 * -(f-(m-1)) + m d >= 0
535 * Do not finalize the result.
537 static __isl_give isl_basic_map *add_known_div_constraints(
538 __isl_take isl_basic_map *bmap)
540 int i;
541 isl_size n_div;
543 n_div = isl_basic_map_dim(bmap, isl_dim_div);
544 if (n_div < 0)
545 return isl_basic_map_free(bmap);
546 if (n_div == 0)
547 return bmap;
548 bmap = isl_basic_map_cow(bmap);
549 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
550 if (!bmap)
551 return NULL;
552 for (i = 0; i < n_div; ++i) {
553 if (isl_int_is_zero(bmap->div[i][0]))
554 continue;
555 bmap = isl_basic_map_add_div_constraints(bmap, i);
558 return bmap;
561 __isl_give isl_basic_map *isl_basic_map_from_local_space(
562 __isl_take isl_local_space *ls)
564 int i;
565 isl_size n_div;
566 isl_basic_map *bmap;
568 n_div = isl_local_space_dim(ls, isl_dim_div);
569 if (n_div < 0)
570 ls = isl_local_space_free(ls);
571 if (!ls)
572 return NULL;
574 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
575 n_div, 0, 2 * n_div);
577 for (i = 0; i < n_div; ++i)
578 if (isl_basic_map_alloc_div(bmap) < 0)
579 goto error;
581 for (i = 0; i < n_div; ++i)
582 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
583 bmap = add_known_div_constraints(bmap);
585 isl_local_space_free(ls);
586 return bmap;
587 error:
588 isl_local_space_free(ls);
589 isl_basic_map_free(bmap);
590 return NULL;
593 __isl_give isl_basic_set *isl_basic_set_from_local_space(
594 __isl_take isl_local_space *ls)
596 return isl_basic_map_from_local_space(ls);
599 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
601 return isl_space_copy(isl_map_peek_space(map));
604 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
606 if (!set)
607 return NULL;
608 return isl_space_copy(set->dim);
611 /* Return the space of "map".
612 * This may be either a copy or the space itself
613 * if there is only one reference to "map".
614 * This allows the space to be modified inplace
615 * if both the map and its space have only a single reference.
616 * The caller is not allowed to modify "map" between this call and
617 * a subsequent call to isl_map_restore_space.
618 * The only exception is that isl_map_free can be called instead.
620 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
622 isl_space *space;
624 if (!map)
625 return NULL;
626 if (map->ref != 1)
627 return isl_map_get_space(map);
628 space = map->dim;
629 map->dim = NULL;
630 return space;
633 /* Set the space of "map" to "space", where the space of "map" may be missing
634 * due to a preceding call to isl_map_take_space.
635 * However, in this case, "map" only has a single reference and
636 * then the call to isl_map_cow has no effect.
638 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
639 __isl_take isl_space *space)
641 if (!map || !space)
642 goto error;
644 if (map->dim == space) {
645 isl_space_free(space);
646 return map;
649 map = isl_map_cow(map);
650 if (!map)
651 goto error;
652 isl_space_free(map->dim);
653 map->dim = space;
655 return map;
656 error:
657 isl_map_free(map);
658 isl_space_free(space);
659 return NULL;
662 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
663 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
665 isl_space *space;
667 space = isl_basic_map_take_space(bmap);
668 space = isl_space_set_tuple_name(space, type, s);
669 bmap = isl_basic_map_restore_space(bmap, space);
670 bmap = isl_basic_map_finalize(bmap);
671 return bmap;
674 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
675 __isl_take isl_basic_set *bset, const char *s)
677 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
680 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
681 enum isl_dim_type type)
683 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
686 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
687 enum isl_dim_type type, const char *s)
689 int i;
690 isl_space *space;
692 map = isl_map_cow(map);
693 if (!map)
694 return NULL;
696 for (i = 0; i < map->n; ++i) {
697 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
698 if (!map->p[i])
699 goto error;
702 space = isl_map_take_space(map);
703 space = isl_space_set_tuple_name(space, type, s);
704 map = isl_map_restore_space(map, space);
706 return map;
707 error:
708 isl_map_free(map);
709 return NULL;
712 /* Replace the identifier of the tuple of type "type" by "id".
714 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
715 __isl_take isl_basic_map *bmap,
716 enum isl_dim_type type, __isl_take isl_id *id)
718 isl_space *space;
720 space = isl_basic_map_take_space(bmap);
721 space = isl_space_set_tuple_id(space, type, id);
722 bmap = isl_basic_map_restore_space(bmap, space);
723 bmap = isl_basic_map_finalize(bmap);
724 return bmap;
727 /* Replace the identifier of the tuple by "id".
729 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
730 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
732 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
735 /* Does the input or output tuple have a name?
737 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
739 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
742 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
743 enum isl_dim_type type)
745 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
748 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
749 const char *s)
751 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
752 isl_dim_set, s));
755 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
756 enum isl_dim_type type, __isl_take isl_id *id)
758 isl_space *space;
760 space = isl_map_take_space(map);
761 space = isl_space_set_tuple_id(space, type, id);
762 map = isl_map_restore_space(map, space);
764 return isl_map_reset_space(map, isl_map_get_space(map));
767 /* Replace the identifier of the domain tuple of "map" by "id".
769 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
770 __isl_take isl_id *id)
772 return isl_map_set_tuple_id(map, isl_dim_in, id);
775 /* Replace the identifier of the range tuple of "map" by "id".
777 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
778 __isl_take isl_id *id)
780 return isl_map_set_tuple_id(map, isl_dim_out, id);
783 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
784 __isl_take isl_id *id)
786 return isl_map_set_tuple_id(set, isl_dim_set, id);
789 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
790 enum isl_dim_type type)
792 isl_space *space;
794 space = isl_map_take_space(map);
795 space = isl_space_reset_tuple_id(space, type);
796 map = isl_map_restore_space(map, space);
798 return isl_map_reset_space(map, isl_map_get_space(map));
801 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
803 return isl_map_reset_tuple_id(set, isl_dim_set);
806 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
808 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
811 /* Does the domain tuple of "map" have an identifier?
813 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
815 return isl_map_has_tuple_id(map, isl_dim_in);
818 /* Does the range tuple of "map" have an identifier?
820 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
822 return isl_map_has_tuple_id(map, isl_dim_out);
825 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
826 enum isl_dim_type type)
828 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
831 /* Return the identifier of the domain tuple of "map", assuming it has one.
833 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
835 return isl_map_get_tuple_id(map, isl_dim_in);
838 /* Return the identifier of the range tuple of "map", assuming it has one.
840 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
842 return isl_map_get_tuple_id(map, isl_dim_out);
845 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
847 return isl_map_has_tuple_id(set, isl_dim_set);
850 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
852 return isl_map_get_tuple_id(set, isl_dim_set);
855 /* Does the set tuple have a name?
857 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
859 if (!set)
860 return isl_bool_error;
861 return isl_space_has_tuple_name(set->dim, isl_dim_set);
865 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
867 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
870 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
872 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
875 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
876 enum isl_dim_type type, unsigned pos)
878 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
881 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
882 enum isl_dim_type type, unsigned pos)
884 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
887 /* Does the given dimension have a name?
889 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
890 enum isl_dim_type type, unsigned pos)
892 if (!map)
893 return isl_bool_error;
894 return isl_space_has_dim_name(map->dim, type, pos);
897 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
898 enum isl_dim_type type, unsigned pos)
900 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
903 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
904 enum isl_dim_type type, unsigned pos)
906 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
909 /* Does the given dimension have a name?
911 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
912 enum isl_dim_type type, unsigned pos)
914 if (!set)
915 return isl_bool_error;
916 return isl_space_has_dim_name(set->dim, type, pos);
919 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
920 __isl_take isl_basic_map *bmap,
921 enum isl_dim_type type, unsigned pos, const char *s)
923 isl_space *space;
925 space = isl_basic_map_take_space(bmap);
926 space = isl_space_set_dim_name(space, type, pos, s);
927 bmap = isl_basic_map_restore_space(bmap, space);
928 return isl_basic_map_finalize(bmap);
931 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
932 enum isl_dim_type type, unsigned pos, const char *s)
934 int i;
935 isl_space *space;
937 map = isl_map_cow(map);
938 if (!map)
939 return NULL;
941 for (i = 0; i < map->n; ++i) {
942 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
943 if (!map->p[i])
944 goto error;
947 space = isl_map_take_space(map);
948 space = isl_space_set_dim_name(space, type, pos, s);
949 map = isl_map_restore_space(map, space);
951 return map;
952 error:
953 isl_map_free(map);
954 return NULL;
957 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
958 __isl_take isl_basic_set *bset,
959 enum isl_dim_type type, unsigned pos, const char *s)
961 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
962 type, pos, s));
965 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
966 enum isl_dim_type type, unsigned pos, const char *s)
968 return set_from_map(isl_map_set_dim_name(set_to_map(set),
969 type, pos, s));
972 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
973 enum isl_dim_type type, unsigned pos)
975 if (!bmap)
976 return isl_bool_error;
977 return isl_space_has_dim_id(bmap->dim, type, pos);
980 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
981 enum isl_dim_type type, unsigned pos)
983 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
986 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
987 enum isl_dim_type type, unsigned pos)
989 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
992 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
993 enum isl_dim_type type, unsigned pos)
995 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
998 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
999 enum isl_dim_type type, unsigned pos)
1001 return isl_map_has_dim_id(set, type, pos);
1004 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
1005 enum isl_dim_type type, unsigned pos)
1007 return isl_map_get_dim_id(set, type, pos);
1010 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
1011 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1013 isl_space *space;
1015 space = isl_map_take_space(map);
1016 space = isl_space_set_dim_id(space, type, pos, id);
1017 map = isl_map_restore_space(map, space);
1019 return isl_map_reset_space(map, isl_map_get_space(map));
1022 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1023 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1025 return isl_map_set_dim_id(set, type, pos, id);
1028 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1029 __isl_keep isl_id *id)
1031 if (!map)
1032 return -1;
1033 return isl_space_find_dim_by_id(map->dim, type, id);
1036 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1037 __isl_keep isl_id *id)
1039 return isl_map_find_dim_by_id(set, type, id);
1042 /* Return the position of the dimension of the given type and name
1043 * in "bmap".
1044 * Return -1 if no such dimension can be found.
1046 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1047 enum isl_dim_type type, const char *name)
1049 if (!bmap)
1050 return -1;
1051 return isl_space_find_dim_by_name(bmap->dim, type, name);
1054 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1055 const char *name)
1057 if (!map)
1058 return -1;
1059 return isl_space_find_dim_by_name(map->dim, type, name);
1062 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1063 const char *name)
1065 return isl_map_find_dim_by_name(set, type, name);
1068 /* Check whether equality i of bset is a pure stride constraint
1069 * on a single dimension, i.e., of the form
1071 * v = k e
1073 * with k a constant and e an existentially quantified variable.
1075 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1077 isl_size nparam;
1078 isl_size d;
1079 isl_size n_div;
1080 int pos1;
1081 int pos2;
1083 nparam = isl_basic_set_dim(bset, isl_dim_param);
1084 d = isl_basic_set_dim(bset, isl_dim_set);
1085 n_div = isl_basic_set_dim(bset, isl_dim_div);
1086 if (nparam < 0 || d < 0 || n_div < 0)
1087 return isl_bool_error;
1089 if (!isl_int_is_zero(bset->eq[i][0]))
1090 return isl_bool_false;
1092 if (isl_seq_any_non_zero(bset->eq[i] + 1, nparam))
1093 return isl_bool_false;
1094 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1095 if (pos1 == -1)
1096 return isl_bool_false;
1097 if (isl_seq_any_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1098 d - pos1 - 1))
1099 return isl_bool_false;
1101 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1102 if (pos2 == -1)
1103 return isl_bool_false;
1104 if (isl_seq_any_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1105 n_div - pos2 - 1))
1106 return isl_bool_false;
1107 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1108 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1109 return isl_bool_false;
1111 return isl_bool_true;
1114 /* Reset the user pointer on all identifiers of parameters and tuples
1115 * of the space of "map".
1117 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1119 isl_space *space;
1121 space = isl_map_get_space(map);
1122 space = isl_space_reset_user(space);
1123 map = isl_map_reset_space(map, space);
1125 return map;
1128 /* Reset the user pointer on all identifiers of parameters and tuples
1129 * of the space of "set".
1131 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1133 return isl_map_reset_user(set);
1136 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1138 if (!bmap)
1139 return isl_bool_error;
1140 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1143 /* Has "map" been marked as a rational map?
1144 * In particular, have all basic maps in "map" been marked this way?
1145 * An empty map is not considered to be rational.
1146 * Maps where only some of the basic maps are marked rational
1147 * are not allowed.
1149 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1151 int i;
1152 isl_bool rational;
1154 if (!map)
1155 return isl_bool_error;
1156 if (map->n == 0)
1157 return isl_bool_false;
1158 rational = isl_basic_map_is_rational(map->p[0]);
1159 if (rational < 0)
1160 return rational;
1161 for (i = 1; i < map->n; ++i) {
1162 isl_bool rational_i;
1164 rational_i = isl_basic_map_is_rational(map->p[i]);
1165 if (rational_i < 0)
1166 return rational_i;
1167 if (rational != rational_i)
1168 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1169 "mixed rational and integer basic maps "
1170 "not supported", return isl_bool_error);
1173 return rational;
1176 /* Has "set" been marked as a rational set?
1177 * In particular, have all basic set in "set" been marked this way?
1178 * An empty set is not considered to be rational.
1179 * Sets where only some of the basic sets are marked rational
1180 * are not allowed.
1182 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1184 return isl_map_is_rational(set);
1187 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1189 return isl_basic_map_is_rational(bset);
1192 /* Does "bmap" contain any rational points?
1194 * If "bmap" has an equality for each dimension, equating the dimension
1195 * to an integer constant, then it has no rational points, even if it
1196 * is marked as rational.
1198 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1200 isl_bool has_rational = isl_bool_true;
1201 isl_size total;
1203 if (!bmap)
1204 return isl_bool_error;
1205 if (isl_basic_map_plain_is_empty(bmap))
1206 return isl_bool_false;
1207 if (!isl_basic_map_is_rational(bmap))
1208 return isl_bool_false;
1209 bmap = isl_basic_map_copy(bmap);
1210 bmap = isl_basic_map_implicit_equalities(bmap);
1211 total = isl_basic_map_dim(bmap, isl_dim_all);
1212 if (total < 0)
1213 return isl_bool_error;
1214 if (bmap->n_eq == total) {
1215 int i, j;
1216 for (i = 0; i < bmap->n_eq; ++i) {
1217 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1218 if (j < 0)
1219 break;
1220 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1221 !isl_int_is_negone(bmap->eq[i][1 + j]))
1222 break;
1223 if (isl_seq_any_non_zero(bmap->eq[i] + 1 + j + 1,
1224 total - j - 1))
1225 break;
1227 if (i == bmap->n_eq)
1228 has_rational = isl_bool_false;
1230 isl_basic_map_free(bmap);
1232 return has_rational;
1235 /* Does "map" contain any rational points?
1237 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1239 int i;
1240 isl_bool has_rational;
1242 if (!map)
1243 return isl_bool_error;
1244 for (i = 0; i < map->n; ++i) {
1245 has_rational = isl_basic_map_has_rational(map->p[i]);
1246 if (has_rational < 0 || has_rational)
1247 return has_rational;
1249 return isl_bool_false;
1252 /* Does "set" contain any rational points?
1254 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1256 return isl_map_has_rational(set);
1259 /* Is this basic set a parameter domain?
1261 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1263 if (!bset)
1264 return isl_bool_error;
1265 return isl_space_is_params(bset->dim);
1268 /* Is this set a parameter domain?
1270 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1272 if (!set)
1273 return isl_bool_error;
1274 return isl_space_is_params(set->dim);
1277 /* Is this map actually a parameter domain?
1278 * Users should never call this function. Outside of isl,
1279 * a map can never be a parameter domain.
1281 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1283 if (!map)
1284 return isl_bool_error;
1285 return isl_space_is_params(map->dim);
1288 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1289 __isl_take isl_basic_map *bmap, unsigned extra,
1290 unsigned n_eq, unsigned n_ineq)
1292 int i;
1293 isl_space *space = isl_basic_map_peek_space(bmap);
1294 isl_size n_var = isl_space_dim(space, isl_dim_all);
1295 size_t row_size = 1 + n_var + extra;
1297 bmap->ctx = ctx;
1298 isl_ctx_ref(ctx);
1300 if (n_var < 0)
1301 return isl_basic_map_free(bmap);
1303 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1304 if (isl_blk_is_error(bmap->block))
1305 goto error;
1307 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1308 if ((n_ineq + n_eq) && !bmap->ineq)
1309 goto error;
1311 if (extra == 0) {
1312 bmap->block2 = isl_blk_empty();
1313 bmap->div = NULL;
1314 } else {
1315 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1316 if (isl_blk_is_error(bmap->block2))
1317 goto error;
1319 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1320 if (!bmap->div)
1321 goto error;
1324 for (i = 0; i < n_ineq + n_eq; ++i)
1325 bmap->ineq[i] = bmap->block.data + i * row_size;
1327 for (i = 0; i < extra; ++i)
1328 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1330 bmap->ref = 1;
1331 bmap->flags = 0;
1332 bmap->c_size = n_eq + n_ineq;
1333 bmap->eq = bmap->ineq + n_ineq;
1334 bmap->extra = extra;
1335 bmap->n_eq = 0;
1336 bmap->n_ineq = 0;
1337 bmap->n_div = 0;
1338 bmap->sample = NULL;
1340 return bmap;
1341 error:
1342 isl_basic_map_free(bmap);
1343 return NULL;
1346 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1347 unsigned nparam, unsigned dim, unsigned extra,
1348 unsigned n_eq, unsigned n_ineq)
1350 struct isl_basic_map *bmap;
1351 isl_space *space;
1353 space = isl_space_set_alloc(ctx, nparam, dim);
1354 if (!space)
1355 return NULL;
1357 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1358 return bset_from_bmap(bmap);
1361 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1362 unsigned extra, unsigned n_eq, unsigned n_ineq)
1364 struct isl_basic_map *bmap;
1365 if (!space)
1366 return NULL;
1367 isl_assert(space->ctx, space->n_in == 0, goto error);
1368 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1369 return bset_from_bmap(bmap);
1370 error:
1371 isl_space_free(space);
1372 return NULL;
1375 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1376 unsigned extra, unsigned n_eq, unsigned n_ineq)
1378 struct isl_basic_map *bmap;
1380 if (!space)
1381 return NULL;
1382 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1383 if (!bmap)
1384 goto error;
1385 bmap->dim = space;
1387 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1388 error:
1389 isl_space_free(space);
1390 return NULL;
1393 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1394 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1395 unsigned n_eq, unsigned n_ineq)
1397 struct isl_basic_map *bmap;
1398 isl_space *space;
1400 space = isl_space_alloc(ctx, nparam, in, out);
1401 if (!space)
1402 return NULL;
1404 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1405 return bmap;
1408 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1409 __isl_keep isl_basic_map *src)
1411 int i;
1412 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1414 if (!dst || total < 0)
1415 return isl_basic_map_free(dst);
1417 for (i = 0; i < src->n_eq; ++i) {
1418 int j = isl_basic_map_alloc_equality(dst);
1419 if (j < 0)
1420 return isl_basic_map_free(dst);
1421 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1424 for (i = 0; i < src->n_ineq; ++i) {
1425 int j = isl_basic_map_alloc_inequality(dst);
1426 if (j < 0)
1427 return isl_basic_map_free(dst);
1428 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1431 for (i = 0; i < src->n_div; ++i) {
1432 int j = isl_basic_map_alloc_div(dst);
1433 if (j < 0)
1434 return isl_basic_map_free(dst);
1435 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1437 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1438 return dst;
1441 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1443 struct isl_basic_map *dup;
1445 if (!bmap)
1446 return NULL;
1447 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1448 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1449 dup = dup_constraints(dup, bmap);
1450 if (!dup)
1451 return NULL;
1452 dup->flags = bmap->flags;
1453 dup->sample = isl_vec_copy(bmap->sample);
1454 return dup;
1457 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1459 struct isl_basic_map *dup;
1461 dup = isl_basic_map_dup(bset_to_bmap(bset));
1462 return bset_from_bmap(dup);
1465 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1467 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset)));
1470 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1472 if (!set)
1473 return NULL;
1475 set->ref++;
1476 return set;
1479 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1481 if (!bmap)
1482 return NULL;
1484 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1485 bmap->ref++;
1486 return bmap;
1488 bmap = isl_basic_map_dup(bmap);
1489 if (bmap)
1490 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1491 return bmap;
1494 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1496 if (!map)
1497 return NULL;
1499 map->ref++;
1500 return map;
1503 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1505 if (!bmap)
1506 return NULL;
1508 if (--bmap->ref > 0)
1509 return NULL;
1511 isl_ctx_deref(bmap->ctx);
1512 free(bmap->div);
1513 isl_blk_free(bmap->ctx, bmap->block2);
1514 free(bmap->ineq);
1515 isl_blk_free(bmap->ctx, bmap->block);
1516 isl_vec_free(bmap->sample);
1517 isl_space_free(bmap->dim);
1518 free(bmap);
1520 return NULL;
1523 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1525 return isl_basic_map_free(bset_to_bmap(bset));
1528 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1530 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1533 /* Check that "bset" does not involve any parameters.
1535 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1537 isl_size nparam;
1539 nparam = isl_basic_set_dim(bset, isl_dim_param);
1540 if (nparam < 0)
1541 return isl_stat_error;
1542 if (nparam != 0)
1543 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1544 "basic set should not have any parameters",
1545 return isl_stat_error);
1546 return isl_stat_ok;
1549 /* Check that "bset" does not involve any local variables.
1551 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1553 isl_size n_div;
1555 n_div = isl_basic_set_dim(bset, isl_dim_div);
1556 if (n_div < 0)
1557 return isl_stat_error;
1558 if (n_div != 0)
1559 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1560 "basic set should not have any local variables",
1561 return isl_stat_error);
1562 return isl_stat_ok;
1565 #undef TYPE
1566 #define TYPE isl_map
1568 #include "isl_check_named_params_templ.c"
1570 #undef TYPE
1571 #define TYPE isl_basic_map
1573 static
1574 #include "isl_check_named_params_templ.c"
1576 /* Check that "bmap1" and "bmap2" have the same parameters,
1577 * reporting an error if they do not.
1579 static isl_stat isl_basic_map_check_equal_params(
1580 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1582 isl_bool match;
1584 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1585 if (match < 0)
1586 return isl_stat_error;
1587 if (!match)
1588 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1589 "parameters don't match", return isl_stat_error);
1590 return isl_stat_ok;
1593 #undef TYPE
1594 #define TYPE isl_map
1596 #include "isl_align_params_bin_templ.c"
1598 #undef SUFFIX
1599 #define SUFFIX set
1600 #undef ARG1
1601 #define ARG1 isl_map
1602 #undef ARG2
1603 #define ARG2 isl_set
1605 #include "isl_align_params_templ.c"
1607 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1608 __isl_keep isl_map *map2,
1609 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1611 isl_bool r;
1613 if (!map1 || !map2)
1614 return isl_bool_error;
1615 if (isl_map_has_equal_params(map1, map2))
1616 return fn(map1, map2);
1617 if (isl_map_check_named_params(map1) < 0)
1618 return isl_bool_error;
1619 if (isl_map_check_named_params(map2) < 0)
1620 return isl_bool_error;
1621 map1 = isl_map_copy(map1);
1622 map2 = isl_map_copy(map2);
1623 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1624 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1625 r = fn(map1, map2);
1626 isl_map_free(map1);
1627 isl_map_free(map2);
1628 return r;
1631 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1633 isl_size total;
1634 struct isl_ctx *ctx;
1636 total = isl_basic_map_dim(bmap, isl_dim_all);
1637 if (total < 0)
1638 return -1;
1639 ctx = bmap->ctx;
1640 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1641 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1642 return -1);
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1644 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1645 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1646 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1647 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1648 isl_int *t;
1649 int j = isl_basic_map_alloc_inequality(bmap);
1650 if (j < 0)
1651 return -1;
1652 t = bmap->ineq[j];
1653 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1654 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1655 bmap->eq[-1] = t;
1656 bmap->n_eq++;
1657 bmap->n_ineq--;
1658 bmap->eq--;
1659 return 0;
1661 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1662 bmap->extra - bmap->n_div);
1663 return bmap->n_eq++;
1666 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1668 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1671 __isl_give isl_basic_map *isl_basic_map_free_equality(
1672 __isl_take isl_basic_map *bmap, unsigned n)
1674 if (!bmap)
1675 return NULL;
1676 if (n > bmap->n_eq)
1677 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1678 "invalid number of equalities",
1679 isl_basic_map_free(bmap));
1680 bmap->n_eq -= n;
1681 return bmap;
1684 __isl_give isl_basic_set *isl_basic_set_free_equality(
1685 __isl_take isl_basic_set *bset, unsigned n)
1687 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1688 n));
1691 /* Drop the equality constraint at position "pos",
1692 * preserving the order of the other equality constraints.
1694 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1696 isl_int *t;
1697 int r;
1699 if (!bmap)
1700 return -1;
1701 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1703 t = bmap->eq[pos];
1704 bmap->n_eq--;
1705 for (r = pos; r < bmap->n_eq; ++r)
1706 bmap->eq[r] = bmap->eq[r + 1];
1707 bmap->eq[bmap->n_eq] = t;
1709 return 0;
1712 /* Turn inequality "pos" of "bmap" into an equality.
1714 * In particular, we move the inequality in front of the equalities
1715 * and move the last inequality in the position of the moved inequality.
1716 * Note that isl_tab_make_equalities_explicit depends on this particular
1717 * change in the ordering of the constraints.
1719 void isl_basic_map_inequality_to_equality(
1720 __isl_keep isl_basic_map *bmap, unsigned pos)
1722 isl_int *t;
1724 t = bmap->ineq[pos];
1725 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1726 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1727 bmap->eq[-1] = t;
1728 bmap->n_eq++;
1729 bmap->n_ineq--;
1730 bmap->eq--;
1731 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1732 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1734 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1737 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1739 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1742 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1744 isl_size total;
1745 struct isl_ctx *ctx;
1747 total = isl_basic_map_dim(bmap, isl_dim_all);
1748 if (total < 0)
1749 return -1;
1750 ctx = bmap->ctx;
1751 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1753 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1754 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1755 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1756 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1757 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1758 bmap->extra - bmap->n_div);
1759 return bmap->n_ineq++;
1762 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1764 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1767 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1768 __isl_take isl_basic_map *bmap, unsigned n)
1770 if (!bmap)
1771 return NULL;
1772 if (n > bmap->n_ineq)
1773 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1774 "invalid number of inequalities",
1775 return isl_basic_map_free(bmap));
1776 bmap->n_ineq -= n;
1777 return bmap;
1780 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1781 __isl_take isl_basic_set *bset, unsigned n)
1783 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1784 n));
1787 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1789 isl_int *t;
1790 if (!bmap)
1791 return -1;
1792 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1794 if (pos != bmap->n_ineq - 1) {
1795 t = bmap->ineq[pos];
1796 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1797 bmap->ineq[bmap->n_ineq - 1] = t;
1798 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1800 bmap->n_ineq--;
1801 return 0;
1804 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1806 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1809 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1810 isl_int *eq)
1812 isl_bool empty;
1813 isl_size total;
1814 int k;
1816 empty = isl_basic_map_plain_is_empty(bmap);
1817 if (empty < 0)
1818 return isl_basic_map_free(bmap);
1819 if (empty)
1820 return bmap;
1822 bmap = isl_basic_map_cow(bmap);
1823 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1824 total = isl_basic_map_dim(bmap, isl_dim_all);
1825 if (total < 0)
1826 return isl_basic_map_free(bmap);
1827 k = isl_basic_map_alloc_equality(bmap);
1828 if (k < 0)
1829 goto error;
1830 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1831 return bmap;
1832 error:
1833 isl_basic_map_free(bmap);
1834 return NULL;
1837 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1838 isl_int *eq)
1840 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1843 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1844 isl_int *ineq)
1846 isl_size total;
1847 int k;
1849 bmap = isl_basic_map_cow(bmap);
1850 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1851 total = isl_basic_map_dim(bmap, isl_dim_all);
1852 if (total < 0)
1853 return isl_basic_map_free(bmap);
1854 k = isl_basic_map_alloc_inequality(bmap);
1855 if (k < 0)
1856 goto error;
1857 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1858 return bmap;
1859 error:
1860 isl_basic_map_free(bmap);
1861 return NULL;
1864 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1865 isl_int *ineq)
1867 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1870 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1872 isl_size total;
1874 total = isl_basic_map_dim(bmap, isl_dim_all);
1875 if (total < 0)
1876 return -1;
1877 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1878 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1879 bmap->extra - bmap->n_div);
1880 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1881 return bmap->n_div++;
1884 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1886 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1889 #undef TYPE
1890 #define TYPE isl_basic_map
1891 #include "check_type_range_templ.c"
1893 /* Check that there are "n" dimensions of type "type" starting at "first"
1894 * in "bset".
1896 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1897 enum isl_dim_type type, unsigned first, unsigned n)
1899 return isl_basic_map_check_range(bset_to_bmap(bset),
1900 type, first, n);
1903 /* Insert an extra integer division, prescribed by "div", to "bmap"
1904 * at (integer division) position "pos".
1906 * The integer division is first added at the end and then moved
1907 * into the right position.
1909 __isl_give isl_basic_map *isl_basic_map_insert_div(
1910 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1912 int i, k;
1913 isl_size total;
1915 bmap = isl_basic_map_cow(bmap);
1916 total = isl_basic_map_dim(bmap, isl_dim_all);
1917 if (total < 0 || !div)
1918 return isl_basic_map_free(bmap);
1920 if (div->size != 1 + 1 + total)
1921 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1922 "unexpected size", return isl_basic_map_free(bmap));
1923 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1924 return isl_basic_map_free(bmap);
1926 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1927 k = isl_basic_map_alloc_div(bmap);
1928 if (k < 0)
1929 return isl_basic_map_free(bmap);
1930 isl_seq_cpy(bmap->div[k], div->el, div->size);
1931 isl_int_set_si(bmap->div[k][div->size], 0);
1933 for (i = k; i > pos; --i)
1934 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1936 return bmap;
1939 /* Insert an extra integer division, prescribed by "div", to "bset"
1940 * at (integer division) position "pos".
1942 __isl_give isl_basic_set *isl_basic_set_insert_div(
1943 __isl_take isl_basic_set *bset, int pos, __isl_keep isl_vec *div)
1945 isl_basic_map *bmap = bset_to_bmap(bset);
1946 bmap = isl_basic_map_insert_div(bmap, pos, div);
1947 return bset_from_bmap(bmap);
1950 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1952 if (!bmap)
1953 return isl_stat_error;
1954 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1955 bmap->n_div -= n;
1956 return isl_stat_ok;
1959 static __isl_give isl_basic_map *add_constraints(
1960 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1961 unsigned i_pos, unsigned o_pos)
1963 isl_size total, n_param, n_in, n_out, n_div;
1964 unsigned o_in, o_out;
1965 isl_ctx *ctx;
1966 isl_space *space;
1967 struct isl_dim_map *dim_map;
1969 space = isl_basic_map_peek_space(bmap2);
1970 if (!bmap1 || !space)
1971 goto error;
1973 total = isl_basic_map_dim(bmap1, isl_dim_all);
1974 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1975 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1976 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1977 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1978 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1979 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1980 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1981 goto error;
1982 ctx = isl_basic_map_get_ctx(bmap1);
1983 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1984 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1985 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1986 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1987 isl_dim_map_div(dim_map, bmap2, total);
1989 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1990 error:
1991 isl_basic_map_free(bmap1);
1992 isl_basic_map_free(bmap2);
1993 return NULL;
1996 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1997 unsigned extra, unsigned n_eq, unsigned n_ineq)
1999 isl_space *space;
2000 struct isl_basic_map *ext;
2001 unsigned flags;
2002 int dims_ok;
2004 if (!base)
2005 goto error;
2007 dims_ok = base->extra >= base->n_div + extra;
2009 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
2010 room_for_ineq(base, n_ineq))
2011 return base;
2013 extra += base->extra;
2014 n_eq += base->n_eq;
2015 n_ineq += base->n_ineq;
2017 space = isl_basic_map_get_space(base);
2018 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
2019 if (!ext)
2020 goto error;
2022 if (dims_ok)
2023 ext->sample = isl_vec_copy(base->sample);
2024 flags = base->flags;
2025 ext = add_constraints(ext, base, 0, 0);
2026 if (ext) {
2027 ext->flags = flags;
2028 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2031 return ext;
2033 error:
2034 isl_basic_map_free(base);
2035 return NULL;
2038 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2039 unsigned extra, unsigned n_eq, unsigned n_ineq)
2041 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2042 extra, n_eq, n_ineq));
2045 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2046 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2048 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2051 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2052 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2054 isl_basic_map *bmap = bset_to_bmap(base);
2055 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2056 return bset_from_bmap(bmap);
2059 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2061 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2064 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2066 if (!bmap)
2067 return NULL;
2069 if (bmap->ref > 1) {
2070 bmap->ref--;
2071 bmap = isl_basic_map_dup(bmap);
2073 if (bmap)
2074 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2075 return bmap;
2078 /* Clear all cached information in "map", either because it is about
2079 * to be modified or because it is being freed.
2080 * Always return the same pointer that is passed in.
2081 * This is needed for the use in isl_map_free.
2083 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2085 isl_basic_map_free(map->cached_simple_hull[0]);
2086 isl_basic_map_free(map->cached_simple_hull[1]);
2087 map->cached_simple_hull[0] = NULL;
2088 map->cached_simple_hull[1] = NULL;
2089 return map;
2092 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2094 return isl_map_cow(set);
2097 /* Return an isl_map that is equal to "map" and that has only
2098 * a single reference.
2100 * If the original input already has only one reference, then
2101 * simply return it, but clear all cached information, since
2102 * it may be rendered invalid by the operations that will be
2103 * performed on the result.
2105 * Otherwise, create a duplicate (without any cached information).
2107 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2109 if (!map)
2110 return NULL;
2112 if (map->ref == 1)
2113 return clear_caches(map);
2114 map->ref--;
2115 return isl_map_dup(map);
2118 static void swap_vars(struct isl_blk blk, isl_int *a,
2119 unsigned a_len, unsigned b_len)
2121 isl_seq_cpy(blk.data, a+a_len, b_len);
2122 isl_seq_cpy(blk.data+b_len, a, a_len);
2123 isl_seq_cpy(a, blk.data, b_len+a_len);
2126 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2127 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2129 int i;
2130 struct isl_blk blk;
2132 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2133 goto error;
2135 if (n1 == 0 || n2 == 0)
2136 return bmap;
2138 bmap = isl_basic_map_cow(bmap);
2139 if (!bmap)
2140 return NULL;
2142 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2143 if (isl_blk_is_error(blk))
2144 goto error;
2146 for (i = 0; i < bmap->n_eq; ++i)
2147 swap_vars(blk,
2148 bmap->eq[i] + pos, n1, n2);
2150 for (i = 0; i < bmap->n_ineq; ++i)
2151 swap_vars(blk,
2152 bmap->ineq[i] + pos, n1, n2);
2154 for (i = 0; i < bmap->n_div; ++i)
2155 swap_vars(blk,
2156 bmap->div[i]+1 + pos, n1, n2);
2158 isl_blk_free(bmap->ctx, blk);
2160 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2161 bmap = isl_basic_map_gauss(bmap, NULL);
2162 return isl_basic_map_finalize(bmap);
2163 error:
2164 isl_basic_map_free(bmap);
2165 return NULL;
2168 /* The given basic map has turned out to be empty.
2169 * Explicitly mark it as such and change the representation
2170 * to a canonical representation of the empty basic map.
2171 * Since the basic map has conflicting constraints,
2172 * it must have at least one constraint, except perhaps
2173 * if it was already explicitly marked as being empty.
2174 * Do nothing in the latter case, i.e., if it has been marked empty and
2175 * has no constraints.
2177 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2178 __isl_take isl_basic_map *bmap)
2180 int i = 0;
2181 isl_bool empty;
2182 isl_size n;
2183 isl_size total;
2185 n = isl_basic_map_n_constraint(bmap);
2186 empty = isl_basic_map_plain_is_empty(bmap);
2187 if (n < 0 || empty < 0)
2188 return isl_basic_map_free(bmap);
2189 if (n == 0 && empty)
2190 return bmap;
2191 total = isl_basic_map_dim(bmap, isl_dim_all);
2192 if (total < 0)
2193 return isl_basic_map_free(bmap);
2194 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2195 return isl_basic_map_free(bmap);
2196 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2197 if (!bmap)
2198 return NULL;
2199 if (bmap->n_eq > 0) {
2200 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2201 if (!bmap)
2202 return NULL;
2203 } else {
2204 i = isl_basic_map_alloc_equality(bmap);
2205 if (i < 0)
2206 goto error;
2208 isl_int_set_si(bmap->eq[i][0], 1);
2209 isl_seq_clr(bmap->eq[i]+1, total);
2210 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2211 isl_vec_free(bmap->sample);
2212 bmap->sample = NULL;
2213 return isl_basic_map_finalize(bmap);
2214 error:
2215 isl_basic_map_free(bmap);
2216 return NULL;
2219 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2220 __isl_take isl_basic_set *bset)
2222 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2225 __isl_give isl_basic_map *isl_basic_map_set_rational(
2226 __isl_take isl_basic_map *bmap)
2228 if (!bmap)
2229 return NULL;
2231 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2232 return bmap;
2234 bmap = isl_basic_map_cow(bmap);
2235 if (!bmap)
2236 return NULL;
2238 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2240 return isl_basic_map_finalize(bmap);
2243 __isl_give isl_basic_set *isl_basic_set_set_rational(
2244 __isl_take isl_basic_set *bset)
2246 return isl_basic_map_set_rational(bset);
2249 __isl_give isl_basic_set *isl_basic_set_set_integral(
2250 __isl_take isl_basic_set *bset)
2252 if (!bset)
2253 return NULL;
2255 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2256 return bset;
2258 bset = isl_basic_set_cow(bset);
2259 if (!bset)
2260 return NULL;
2262 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2264 return isl_basic_set_finalize(bset);
2267 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2269 int i;
2271 map = isl_map_cow(map);
2272 if (!map)
2273 return NULL;
2274 for (i = 0; i < map->n; ++i) {
2275 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2276 if (!map->p[i])
2277 goto error;
2279 return map;
2280 error:
2281 isl_map_free(map);
2282 return NULL;
2285 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2287 return isl_map_set_rational(set);
2290 /* Given a constraint "c" that expresses a bound
2291 * on the variable at position "pos" in terms of the first "len" variables
2292 * (other than the variable itself if pos < len), extract this bound
2293 * as a function of those first "len" variables.
2295 * That is, the constraint is of one of the following forms
2297 * -e(...) + m x >= 0
2298 * e(...) - m x >= 0
2299 * -e(...) + m x = 0
2300 * e(...) - m x = 0
2302 * Return (e(...)) / m, with the denominator m in the first position.
2304 static __isl_give isl_vec *extract_bound_from_constraint(isl_ctx *ctx,
2305 isl_int *c, int len, int pos)
2307 isl_vec *v;
2309 v = isl_vec_alloc(ctx, 1 + 1 + len);
2310 if (!v)
2311 return NULL;
2312 if (isl_int_is_pos(c[1 + pos])) {
2313 isl_int_set(v->el[0], c[1 + pos]);
2314 isl_seq_neg(v->el + 1, c, 1 + len);
2315 } else {
2316 isl_int_neg(v->el[0], c[1 + pos]);
2317 isl_seq_cpy(v->el + 1, c, 1 + len);
2319 if (pos < len)
2320 isl_int_set_si(v->el[1 + 1 + pos], 0);
2322 return v;
2325 /* Return the position of the last non-zero coefficient of
2326 * inequality constraint "ineq" of "bmap" or length "len",
2327 * given that the coefficient at position "first" is non-zero,
2328 * or that it is known that there is at least one coefficient
2329 * after "first" that is non-zero.
2331 static int extend_last_non_zero(__isl_keep isl_basic_map *bmap, int ineq,
2332 int first, unsigned len)
2334 int last;
2336 last = isl_seq_last_non_zero(bmap->ineq[ineq] + 1 + first + 1,
2337 len - (first + 1));
2338 if (last < 0)
2339 return first;
2340 else
2341 return first + 1 + last;
2344 /* Do the inequality constraints "i" and "j" of "bmap"
2345 * form a pair of opposite constraints, in the (first) "len" coefficients?
2347 static int is_constraint_pair(__isl_keep isl_basic_map *bmap, int i, int j,
2348 unsigned len)
2350 return isl_seq_is_neg(bmap->ineq[i] + 1, bmap->ineq[j] + 1, len);
2353 /* Given that inequality constraints "i" and "j" of "bmap"
2354 * form a pair of opposite constraints
2356 * f(x) + c1 >= 0
2357 * -f(x) + c2 >= 0
2359 * or
2361 * -c1 <= f(x) <= c2
2363 * do they allow for at most "bound" values in that direction?
2364 * That is, is the sum of their constant terms smaller than "bound"?
2366 * "tmp" is a temporary location that can be used to store the sum.
2368 static int constraint_pair_has_bound(__isl_keep isl_basic_map *bmap,
2369 int i, int j, isl_int bound, isl_int *tmp)
2371 isl_int_add(*tmp, bmap->ineq[i][0], bmap->ineq[j][0]);
2372 return isl_int_abs_lt(*tmp, bound);
2375 /* Return the position of an inequality constraint in "bmap"
2376 * that together with inequality constraint "ineq" forms
2377 * a pair of opposite constraints that allow at most "bound" values
2378 * in their shared direction and that appears before "ineq".
2379 * Return a position beyond the number of inequality constraints
2380 * if no such constraint can be found.
2382 * The constraints of "bmap" are assumed to have been sorted.
2383 * This means that as soon as a constraint is found where the value
2384 * of the last coefficient (in absolute value) is different from that of "ineq",
2385 * no opposite constraint can be found.
2386 * It also means that only the coefficients up to this last coefficient
2387 * need to be compared.
2389 * "pos" is the position of a coefficient that is known to be non-zero.
2390 * If no such position is known a priori, then the value 0 can be passed in.
2391 * "len" is the number of (relevant) coefficients in the constraints.
2392 * "tmp" is a temporary location that can be used to store the sum.
2394 static isl_size find_earlier_constraint_in_pair(__isl_keep isl_basic_map *bmap,
2395 int ineq, int pos, int len, isl_int bound, isl_int *tmp)
2397 int j;
2398 int last;
2399 isl_size n_ineq;
2401 n_ineq = isl_basic_map_n_inequality(bmap);
2402 if (n_ineq < 0)
2403 return isl_size_error;
2405 last = extend_last_non_zero(bmap, ineq, pos, len);
2407 for (j = ineq - 1; j >= 0; --j) {
2408 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + last],
2409 bmap->ineq[j][1 + last]))
2410 return n_ineq;
2411 if (!is_constraint_pair(bmap, ineq, j, last + 1))
2412 continue;
2413 if (constraint_pair_has_bound(bmap, ineq, j, bound, tmp))
2414 return j;
2415 return n_ineq;
2418 return n_ineq;
2421 /* Return the position of an inequality constraint in "bmap"
2422 * that together with inequality constraint "ineq" forms
2423 * a pair of opposite constraints that allow at most "bound" values
2424 * in their shared direction and that appears after "ineq".
2425 * Return a position beyond the number of inequality constraints
2426 * if no such constraint can be found.
2428 * The constraints of "bmap" are assumed to have been sorted.
2429 * This means that as soon as a constraint is found where the value
2430 * of the last coefficient (in absolute value) is different from that of "ineq",
2431 * no opposite constraint can be found.
2432 * It also means that only the coefficients up to this last coefficient
2433 * need to be compared.
2435 * "pos" is the position of a coefficient that is known to be non-zero.
2436 * If no such position is known a priori, then the value 0 can be passed in.
2437 * "len" is the number of (relevant) coefficients in the constraints.
2438 * "tmp" is a temporary location that can be used to store the sum.
2440 static isl_size find_later_constraint_in_pair(__isl_keep isl_basic_map *bmap,
2441 int ineq, int pos, int len, isl_int bound, isl_int *tmp)
2443 int j;
2444 int last;
2445 isl_size n_ineq;
2447 n_ineq = isl_basic_map_n_inequality(bmap);
2448 if (n_ineq < 0)
2449 return isl_size_error;
2451 last = extend_last_non_zero(bmap, ineq, pos, len);
2453 for (j = ineq + 1; j < n_ineq; ++j) {
2454 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + last],
2455 bmap->ineq[j][1 + last]))
2456 return n_ineq;
2457 if (isl_seq_any_non_zero(bmap->ineq[j] + 1 + last + 1,
2458 len - (last + 1)))
2459 return n_ineq;
2460 if (!is_constraint_pair(bmap, ineq, j, last + 1))
2461 continue;
2462 if (constraint_pair_has_bound(bmap, ineq, j, bound, tmp))
2463 return j;
2464 return n_ineq;
2467 return n_ineq;
2470 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2471 * of "bmap").
2473 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2475 isl_int *t = bmap->div[a];
2476 bmap->div[a] = bmap->div[b];
2477 bmap->div[b] = t;
2480 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2481 * div definitions accordingly.
2483 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2484 int a, int b)
2486 int i;
2487 isl_size off;
2489 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2490 if (off < 0)
2491 return isl_basic_map_free(bmap);
2493 swap_div(bmap, a, b);
2495 for (i = 0; i < bmap->n_eq; ++i)
2496 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2498 for (i = 0; i < bmap->n_ineq; ++i)
2499 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2501 for (i = 0; i < bmap->n_div; ++i)
2502 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2503 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2505 return bmap;
2508 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2510 isl_seq_cpy(c, c + n, rem);
2511 isl_seq_clr(c + rem, n);
2514 /* Drop n dimensions starting at first.
2516 * In principle, this frees up some extra variables as the number
2517 * of columns remains constant, but we would have to extend
2518 * the div array too as the number of rows in this array is assumed
2519 * to be equal to extra.
2521 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2522 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2524 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2527 /* Move "n" divs starting at "first" to the end of the list of divs.
2529 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2530 unsigned first, unsigned n)
2532 isl_int **div;
2533 int i;
2535 if (first + n == bmap->n_div)
2536 return bmap;
2538 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2539 if (!div)
2540 goto error;
2541 for (i = 0; i < n; ++i)
2542 div[i] = bmap->div[first + i];
2543 for (i = 0; i < bmap->n_div - first - n; ++i)
2544 bmap->div[first + i] = bmap->div[first + n + i];
2545 for (i = 0; i < n; ++i)
2546 bmap->div[bmap->n_div - n + i] = div[i];
2547 free(div);
2548 return bmap;
2549 error:
2550 isl_basic_map_free(bmap);
2551 return NULL;
2554 #undef TYPE
2555 #define TYPE isl_map
2556 static
2557 #include "check_type_range_templ.c"
2559 /* Check that there are "n" dimensions of type "type" starting at "first"
2560 * in "set".
2562 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2563 enum isl_dim_type type, unsigned first, unsigned n)
2565 return isl_map_check_range(set_to_map(set), type, first, n);
2568 /* Drop "n" dimensions of type "type" starting at "first".
2569 * Perform the core computation, without cowing or
2570 * simplifying and finalizing the result.
2572 * In principle, this frees up some extra variables as the number
2573 * of columns remains constant, but we would have to extend
2574 * the div array too as the number of rows in this array is assumed
2575 * to be equal to extra.
2577 __isl_give isl_basic_map *isl_basic_map_drop_core(
2578 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2579 unsigned first, unsigned n)
2581 int i;
2582 unsigned offset;
2583 unsigned left;
2584 isl_size total;
2586 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2587 return isl_basic_map_free(bmap);
2589 total = isl_basic_map_dim(bmap, isl_dim_all);
2590 if (total < 0)
2591 return isl_basic_map_free(bmap);
2593 offset = isl_basic_map_offset(bmap, type) + first;
2594 left = total - (offset - 1) - n;
2595 for (i = 0; i < bmap->n_eq; ++i)
2596 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2598 for (i = 0; i < bmap->n_ineq; ++i)
2599 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2601 for (i = 0; i < bmap->n_div; ++i)
2602 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2604 if (type == isl_dim_div) {
2605 bmap = move_divs_last(bmap, first, n);
2606 if (!bmap)
2607 return NULL;
2608 if (isl_basic_map_free_div(bmap, n) < 0)
2609 return isl_basic_map_free(bmap);
2610 } else
2611 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2612 if (!bmap->dim)
2613 return isl_basic_map_free(bmap);
2615 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2616 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2617 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2618 return bmap;
2621 /* Drop "n" dimensions of type "type" starting at "first".
2623 * In principle, this frees up some extra variables as the number
2624 * of columns remains constant, but we would have to extend
2625 * the div array too as the number of rows in this array is assumed
2626 * to be equal to extra.
2628 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2629 enum isl_dim_type type, unsigned first, unsigned n)
2631 if (!bmap)
2632 return NULL;
2633 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2634 return bmap;
2636 bmap = isl_basic_map_cow(bmap);
2637 if (!bmap)
2638 return NULL;
2640 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2642 bmap = isl_basic_map_simplify(bmap);
2643 return isl_basic_map_finalize(bmap);
2646 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2647 enum isl_dim_type type, unsigned first, unsigned n)
2649 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2650 type, first, n));
2653 /* No longer consider "map" to be normalized.
2655 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2657 if (!map)
2658 return NULL;
2659 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2660 return map;
2663 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2664 enum isl_dim_type type, unsigned first, unsigned n)
2666 int i;
2667 isl_space *space;
2669 if (isl_map_check_range(map, type, first, n) < 0)
2670 return isl_map_free(map);
2672 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2673 return map;
2674 map = isl_map_cow(map);
2675 if (!map)
2676 goto error;
2678 for (i = 0; i < map->n; ++i) {
2679 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2680 if (!map->p[i])
2681 goto error;
2683 map = isl_map_unmark_normalized(map);
2685 space = isl_map_take_space(map);
2686 space = isl_space_drop_dims(space, type, first, n);
2687 map = isl_map_restore_space(map, space);
2689 return map;
2690 error:
2691 isl_map_free(map);
2692 return NULL;
2695 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2696 enum isl_dim_type type, unsigned first, unsigned n)
2698 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2701 /* Drop the integer division at position "div", which is assumed
2702 * not to appear in any of the constraints or
2703 * in any of the other integer divisions.
2705 * Since the integer division is redundant, there is no need to cow.
2707 __isl_give isl_basic_map *isl_basic_map_drop_div(
2708 __isl_take isl_basic_map *bmap, unsigned div)
2710 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2713 /* Eliminate the specified n dimensions starting at first from the
2714 * constraints, without removing the dimensions from the space.
2715 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2717 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2718 enum isl_dim_type type, unsigned first, unsigned n)
2720 int i;
2722 if (n == 0)
2723 return map;
2725 if (isl_map_check_range(map, type, first, n) < 0)
2726 return isl_map_free(map);
2728 map = isl_map_cow(map);
2729 if (!map)
2730 return NULL;
2732 for (i = 0; i < map->n; ++i) {
2733 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2734 if (!map->p[i])
2735 goto error;
2737 return map;
2738 error:
2739 isl_map_free(map);
2740 return NULL;
2743 /* Eliminate the specified n dimensions starting at first from the
2744 * constraints, without removing the dimensions from the space.
2745 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2747 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2748 enum isl_dim_type type, unsigned first, unsigned n)
2750 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2753 /* Eliminate the specified n dimensions starting at first from the
2754 * constraints, without removing the dimensions from the space.
2755 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2757 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2758 unsigned first, unsigned n)
2760 return isl_set_eliminate(set, isl_dim_set, first, n);
2763 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2764 __isl_take isl_basic_map *bmap)
2766 isl_size v_div;
2768 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2769 if (v_div < 0)
2770 return isl_basic_map_free(bmap);
2771 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2772 if (!bmap)
2773 return NULL;
2774 bmap->n_div = 0;
2775 return isl_basic_map_finalize(bmap);
2778 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2779 __isl_take isl_basic_set *bset)
2781 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2784 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2786 int i;
2788 if (!map)
2789 return NULL;
2790 if (map->n == 0)
2791 return map;
2793 map = isl_map_cow(map);
2794 if (!map)
2795 return NULL;
2797 for (i = 0; i < map->n; ++i) {
2798 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2799 if (!map->p[i])
2800 goto error;
2802 return map;
2803 error:
2804 isl_map_free(map);
2805 return NULL;
2808 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2810 return isl_map_remove_divs(set);
2813 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2814 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2815 unsigned first, unsigned n)
2817 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2818 return isl_basic_map_free(bmap);
2819 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2820 return bmap;
2821 bmap = isl_basic_map_eliminate_vars(bmap,
2822 isl_basic_map_offset(bmap, type) - 1 + first, n);
2823 if (!bmap)
2824 return bmap;
2825 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2826 return bmap;
2827 bmap = isl_basic_map_drop(bmap, type, first, n);
2828 return bmap;
2831 /* Does the local variable "div" of "bmap" have a known expression
2832 * that involves the "n" variables starting at "first"?
2834 isl_bool isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map *bmap,
2835 int div, unsigned first, unsigned n)
2837 isl_bool unknown;
2839 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
2840 if (unknown < 0 || unknown)
2841 return isl_bool_not(unknown);
2842 if (isl_seq_any_non_zero(bmap->div[div] + 1 + 1 + first, n))
2843 return isl_bool_true;
2845 return isl_bool_false;
2848 /* Return true if the definition of the given div (recursively) involves
2849 * any of the given variables.
2851 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2852 unsigned first, unsigned n)
2854 int i;
2855 isl_bool involves;
2856 isl_size n_div, v_div;
2858 involves = isl_basic_map_div_expr_involves_vars(bmap, div, first, n);
2859 if (involves < 0 || involves)
2860 return involves;
2862 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2863 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2864 if (n_div < 0 || v_div < 0)
2865 return isl_bool_error;
2866 for (i = n_div - 1; i >= 0; --i) {
2867 isl_bool involves;
2869 if (isl_int_is_zero(bmap->div[div][1 + 1 + v_div + i]))
2870 continue;
2871 involves = div_involves_vars(bmap, i, first, n);
2872 if (involves < 0 || involves)
2873 return involves;
2876 return isl_bool_false;
2879 /* Does the definition of any integer division involve
2880 * any of the given variables?
2882 isl_bool isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map *bmap,
2883 unsigned first, unsigned n)
2885 int i;
2886 isl_size n_div;
2888 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2889 if (n_div < 0)
2890 return isl_bool_error;
2892 for (i = 0; i < n_div; ++i) {
2893 isl_bool has;
2895 has = isl_basic_map_div_expr_involves_vars(bmap, i, first, n);
2896 if (has < 0 || has)
2897 return has;
2900 return isl_bool_false;
2903 /* Try and add a lower and/or upper bound on "div" to "bmap"
2904 * based on inequality "i".
2905 * "total" is the total number of variables (excluding the divs).
2906 * "v" is a temporary object that can be used during the calculations.
2907 * If "lb" is set, then a lower bound should be constructed.
2908 * If "ub" is set, then an upper bound should be constructed.
2910 * The calling function has already checked that the inequality does not
2911 * reference "div", but we still need to check that the inequality is
2912 * of the right form. We'll consider the case where we want to construct
2913 * a lower bound. The construction of upper bounds is similar.
2915 * Let "div" be of the form
2917 * q = floor((a + f(x))/d)
2919 * We essentially check if constraint "i" is of the form
2921 * b + f(x) >= 0
2923 * so that we can use it to derive a lower bound on "div".
2924 * However, we allow a slightly more general form
2926 * b + g(x) >= 0
2928 * with the condition that the coefficients of g(x) - f(x) are all
2929 * divisible by d.
2930 * Rewriting this constraint as
2932 * 0 >= -b - g(x)
2934 * adding a + f(x) to both sides and dividing by d, we obtain
2936 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2938 * Taking the floor on both sides, we obtain
2940 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2942 * or
2944 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2946 * In the case of an upper bound, we construct the constraint
2948 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2951 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2952 __isl_take isl_basic_map *bmap, int div, int i,
2953 unsigned total, isl_int v, int lb, int ub)
2955 int j;
2957 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2958 if (lb) {
2959 isl_int_sub(v, bmap->ineq[i][1 + j],
2960 bmap->div[div][1 + 1 + j]);
2961 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2963 if (ub) {
2964 isl_int_add(v, bmap->ineq[i][1 + j],
2965 bmap->div[div][1 + 1 + j]);
2966 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2969 if (!lb && !ub)
2970 return bmap;
2972 bmap = isl_basic_map_cow(bmap);
2973 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2974 if (lb) {
2975 int k = isl_basic_map_alloc_inequality(bmap);
2976 if (k < 0)
2977 goto error;
2978 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2979 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2980 bmap->div[div][1 + j]);
2981 isl_int_cdiv_q(bmap->ineq[k][j],
2982 bmap->ineq[k][j], bmap->div[div][0]);
2984 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2986 if (ub) {
2987 int k = isl_basic_map_alloc_inequality(bmap);
2988 if (k < 0)
2989 goto error;
2990 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2991 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2992 bmap->div[div][1 + j]);
2993 isl_int_fdiv_q(bmap->ineq[k][j],
2994 bmap->ineq[k][j], bmap->div[div][0]);
2996 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2999 return bmap;
3000 error:
3001 isl_basic_map_free(bmap);
3002 return NULL;
3005 /* This function is called right before "div" is eliminated from "bmap"
3006 * using Fourier-Motzkin.
3007 * Look through the constraints of "bmap" for constraints on the argument
3008 * of the integer division and use them to construct constraints on the
3009 * integer division itself. These constraints can then be combined
3010 * during the Fourier-Motzkin elimination.
3011 * Note that it is only useful to introduce lower bounds on "div"
3012 * if "bmap" already contains upper bounds on "div" as the newly
3013 * introduce lower bounds can then be combined with the pre-existing
3014 * upper bounds. Similarly for upper bounds.
3015 * We therefore first check if "bmap" contains any lower and/or upper bounds
3016 * on "div".
3018 * It is interesting to note that the introduction of these constraints
3019 * can indeed lead to more accurate results, even when compared to
3020 * deriving constraints on the argument of "div" from constraints on "div".
3021 * Consider, for example, the set
3023 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
3025 * The second constraint can be rewritten as
3027 * 2 * [(-i-2j+3)/4] + k >= 0
3029 * from which we can derive
3031 * -i - 2j + 3 >= -2k
3033 * or
3035 * i + 2j <= 3 + 2k
3037 * Combined with the first constraint, we obtain
3039 * -3 <= 3 + 2k or k >= -3
3041 * If, on the other hand we derive a constraint on [(i+2j)/4] from
3042 * the first constraint, we obtain
3044 * [(i + 2j)/4] >= [-3/4] = -1
3046 * Combining this constraint with the second constraint, we obtain
3048 * k >= -2
3050 static __isl_give isl_basic_map *insert_bounds_on_div(
3051 __isl_take isl_basic_map *bmap, int div)
3053 int i;
3054 int check_lb, check_ub;
3055 isl_int v;
3056 isl_size v_div;
3058 if (!bmap)
3059 return NULL;
3061 if (isl_int_is_zero(bmap->div[div][0]))
3062 return bmap;
3064 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3065 if (v_div < 0)
3066 return isl_basic_map_free(bmap);
3068 check_lb = 0;
3069 check_ub = 0;
3070 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
3071 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
3072 if (s > 0)
3073 check_ub = 1;
3074 if (s < 0)
3075 check_lb = 1;
3078 if (!check_lb && !check_ub)
3079 return bmap;
3081 isl_int_init(v);
3083 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
3084 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
3085 continue;
3087 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
3088 check_lb, check_ub);
3091 isl_int_clear(v);
3093 return bmap;
3096 /* Remove all divs (recursively) involving any of the given variables
3097 * in their definitions.
3099 static __isl_give isl_basic_map *remove_divs_involving_vars(
3100 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3102 int i;
3104 for (i = bmap->n_div - 1; i >= 0; --i) {
3105 isl_bool involves;
3107 involves = div_involves_vars(bmap, i, first, n);
3108 if (involves < 0)
3109 return isl_basic_map_free(bmap);
3110 if (!involves)
3111 continue;
3112 bmap = insert_bounds_on_div(bmap, i);
3113 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3114 if (!bmap)
3115 return NULL;
3116 i = bmap->n_div;
3119 return bmap;
3122 /* Data structure for communicating data between detect_mod and
3123 * substitute_div_mod.
3125 * "pos" is the position of the variable that is being examined.
3127 * "lower_f" is the index of the constraint
3128 * -f(x) + m i + t m n h(alpha) >= 0
3129 * "lower_g" is the index of the constraint
3130 * -g(x) + i >= 0
3131 * "m" and "n" correspond to the values in the first constraint.
3132 * "sum" is a temporary variable that is used internally inside detect_mod.
3134 struct isl_detect_mod_data {
3135 unsigned pos;
3137 int lower_f;
3138 int lower_g;
3140 isl_int m;
3141 isl_int n;
3143 isl_int sum;
3146 /* Initialize "data".
3148 static void isl_detect_mod_data_init(struct isl_detect_mod_data *data)
3150 isl_int_init(data->m);
3151 isl_int_init(data->n);
3152 isl_int_init(data->sum);
3155 /* Free any memory allocated by "data".
3157 static void isl_detect_mod_data_clear(struct isl_detect_mod_data *data)
3159 isl_int_clear(data->sum);
3160 isl_int_clear(data->n);
3161 isl_int_clear(data->m);
3164 /* Is the variable at position data->pos
3165 * equal to a specific case of a nested modulo?
3167 * In particular, look for two pairs of constraints
3169 * -f(x) + m i + t m n h(alpha) >= 0
3170 * f(x) - m i - t m n h(alpha) + c >= 0
3172 * and
3174 * -g(x) + i >= 0
3175 * g(x) - i + d >= 0
3177 * where f(x) and g(x) are expressions in the other variables,
3178 * excluding local variables,
3179 * h(alpha) is a non-zero expression in the local variable,
3180 * t is +1 or -1,
3181 * c < m, and
3182 * d < n.
3184 * If these pairs of constraints are found,
3185 * then store the constraint index of the first f-constraint in data->lower_f,
3186 * the index of the first g-constraint in data->lower_g and
3187 * the values m and n in data->m and data->n.
3189 * The constraints are assumed to have been sorted,
3190 * which means that the constraints in a pair are close to each other.
3191 * The sorting also means that the f-constraints appear
3192 * after the g-constraints.
3194 static isl_bool detect_mod(__isl_keep isl_basic_map *bmap,
3195 struct isl_detect_mod_data *data)
3197 int i;
3198 isl_size j;
3199 isl_size n_ineq;
3200 isl_size v_div, n_div, total;
3202 n_ineq = isl_basic_map_n_inequality(bmap);
3203 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3204 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3205 total = isl_basic_map_dim(bmap, isl_dim_all);
3206 if (n_ineq < 0 || v_div < 0 || n_div < 0 || total < 0)
3207 return isl_bool_error;
3208 if (n_ineq < 4)
3209 return isl_bool_false;
3211 for (i = n_ineq - 1; i >= 1; --i) {
3212 isl_seq_gcd(bmap->ineq[i] + 1 + v_div, n_div, &data->n);
3213 if (isl_int_is_zero(data->n))
3214 return isl_bool_false;
3215 isl_int_abs(data->m, bmap->ineq[i][1 + data->pos]);
3216 j = find_earlier_constraint_in_pair(bmap, i, data->pos, total,
3217 data->m, &data->sum);
3218 if (j < 0)
3219 return isl_bool_error;
3220 if (j >= n_ineq)
3221 continue;
3222 if (!isl_int_is_divisible_by(data->n, data->m))
3223 continue;
3224 if (isl_int_is_pos(bmap->ineq[i][1 + data->pos]))
3225 data->lower_f = i;
3226 else
3227 data->lower_f = j;
3228 isl_int_divexact(data->n, data->n, data->m);
3229 isl_int_abs(data->n, data->n);
3230 break;
3232 if (i < 1)
3233 return isl_bool_false;
3234 for (i = j - 1; i >= 1; --i) {
3235 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + v_div, n_div))
3236 continue;
3237 if (!isl_int_is_one(bmap->ineq[i][1 + data->pos]) &&
3238 !isl_int_is_negone(bmap->ineq[i][1 + data->pos]))
3239 continue;
3240 j = find_earlier_constraint_in_pair(bmap, i, data->pos, v_div,
3241 data->m, &data->sum);
3242 if (j < 0)
3243 return isl_bool_error;
3244 if (j >= n_ineq)
3245 continue;
3246 if (isl_int_is_pos(bmap->ineq[i][1 + data->pos]))
3247 data->lower_g = i;
3248 else
3249 data->lower_g = j;
3250 break;
3252 if (i < 1)
3253 return isl_bool_false;
3254 return isl_bool_true;
3257 /* Given an affine expression "aff" in the variables of "bset" that expresses
3258 * a bound on the variable at position "pos" in terms of the other variables,
3259 * extract this expression as a function of those other variables,
3260 * excluding any local variables.
3262 static __isl_give isl_aff *extract_aff(
3263 __isl_keep isl_basic_set *bset, isl_int *aff, int pos)
3265 isl_size dim;
3266 isl_ctx *ctx;
3267 isl_vec *v;
3268 isl_space *space;
3269 isl_local_space *ls;
3271 space = isl_basic_set_peek_space(bset);
3272 dim = isl_space_dim(space, isl_dim_all);
3273 if (dim < 0)
3274 return NULL;
3275 ls = isl_local_space_from_space(isl_space_copy(space));
3276 ctx = isl_basic_set_get_ctx(bset);
3277 v = extract_bound_from_constraint(ctx, aff, dim, pos);
3278 return isl_aff_alloc_vec(ls, v);
3281 /* Given that inequality "ineq" of "bset" expresses a lower bound
3282 * on the variable at position "pos" in terms of the other variables,
3283 * extract this lower bound as a function of those other variables,
3284 * excluding any local variables.
3286 static __isl_give isl_aff *extract_lower_bound_aff(
3287 __isl_keep isl_basic_set *bset, int ineq, int pos)
3289 if (!bset)
3290 return NULL;
3291 return extract_aff(bset, bset->ineq[ineq], pos);
3294 /* Given that there are two pairs of constraints
3296 * -f(x) + m i + t m n h(alpha) >= 0
3297 * f(x) - m i - t m n h(alpha) + c >= 0
3299 * and
3301 * -g(x) + i >= 0
3302 * g(x) - i + d >= 0
3304 * where f(x) and g(x) are expressions in the other variables,
3305 * excluding local variables,
3306 * h(alpha) is a non-zero expression in the local variable,
3307 * t is +1 or -1,
3308 * c < m, and
3309 * d < n,
3310 * derive an expression for i and plug that into "bmap".
3312 * In particular, the first pair of constraints implies
3314 * 0 <= (-f(x) + m i) % (m n) <= c
3315 * 0 <= (m floor(-f(x)/m) + (-f(x)) mod m + m i) % (m n) <= c
3316 * 0 <= (m floor(-f(x)/m) + m i) % (m n) + (-f(x)) mod m <= c
3317 * 0 <= m ((floor(-f(x)/m) + i) % n) + (-f(x)) mod m <= c
3319 * or
3321 * (floor(-f(x)/m) + i) % n = 0
3323 * because c < m, which can be rewritten to
3325 * (floor(-f(x)/m) + g(x) - g(x) + i) % n = 0
3327 * or
3329 * (-g(x) + i) % n = (-floor(-f(x)/m) - g(x)) % n
3331 * Since 0 <= -g(x) + i <= d < n,
3333 * -g(x) + i = (-floor(-f(x)/m) - g(x)) % n
3335 * or
3337 * i = (-floor(-f(x)/m) - g(x)) % n + g(x)
3339 * Note that the lower bounds on i derived from the original constraints are
3341 * i >= f(x)/m
3342 * i >= g(x)
3344 * (ignoring local variables).
3346 * The expression for i is plugged into "bmap", which results
3347 * in a basic map equivalent to "bmap" given i is equal to the expression,
3348 * so an equality constraint is added to ensure the meaning is preserved.
3350 static __isl_give isl_basic_map *substitute_div_mod(
3351 __isl_take isl_basic_map *bmap, unsigned pos,
3352 struct isl_detect_mod_data *data)
3354 isl_ctx *ctx;
3355 isl_basic_set *bset, *eq;
3356 isl_space *space;
3357 isl_aff *g, *f, *aff, *it;
3358 isl_val *v;
3359 isl_size v_in;
3360 isl_multi_aff *ma;
3362 v_in = isl_basic_map_var_offset(bmap, isl_dim_in);
3363 if (v_in < 0)
3364 return isl_basic_map_free(bmap);
3366 ctx = isl_basic_map_get_ctx(bmap);
3367 bset = isl_basic_map_wrap(bmap);
3368 space = isl_basic_set_get_space(bset);
3369 g = extract_lower_bound_aff(bset, data->lower_g, pos);
3370 f = extract_lower_bound_aff(bset, data->lower_f, pos);
3371 ma = isl_space_identity_multi_aff_on_domain(space);
3372 aff = isl_aff_neg(isl_aff_floor(isl_aff_neg(f)));
3373 aff = isl_aff_sub(aff, isl_aff_copy(g));
3374 v = isl_val_int_from_isl_int(ctx, data->n);
3375 aff = isl_aff_mod_val(aff, v);
3376 aff = isl_aff_add(aff, g);
3377 it = isl_multi_aff_get_at(ma, pos - v_in);
3378 eq = isl_aff_eq_basic_set(it, isl_aff_copy(aff));
3379 ma = isl_multi_aff_set_at(ma, pos - v_in, aff);
3380 bset = isl_basic_set_preimage_multi_aff(bset, ma);
3381 bset = isl_basic_set_intersect(bset, eq);
3382 return isl_basic_set_unwrap(bset);
3385 /* Remove all divs (recursively) involving any of the given dimensions
3386 * in their definitions.
3388 * If it is a single input or output dimension that
3389 * should not appear in any integer division expression and
3390 * if there is indeed an integer division expression
3391 * involving that dimension, then check if there are any constraints
3392 * that imply that the given dimension is equal to some expression
3393 * in the other dimensions.
3394 * If so, plug in that expression for the given dimension
3395 * so that the integer division expressions no longer
3396 * involve that dimension.
3398 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
3399 __isl_take isl_basic_map *bmap,
3400 enum isl_dim_type type, unsigned first, unsigned n)
3402 isl_size off;
3403 isl_bool used;
3404 isl_bool found;
3405 struct isl_detect_mod_data data;
3407 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3408 return isl_basic_map_free(bmap);
3409 off = isl_basic_map_var_offset(bmap, type);
3410 if (off < 0)
3411 return isl_basic_map_free(bmap);
3412 first += off;
3414 if (type == isl_dim_param || type == isl_dim_div || n != 1)
3415 return remove_divs_involving_vars(bmap, first, n);
3416 used = isl_basic_map_any_div_involves_vars(bmap, first, n);
3417 if (used < 0)
3418 return isl_basic_map_free(bmap);
3419 if (!used)
3420 return bmap;
3421 bmap = isl_basic_map_sort_constraints(bmap);
3422 isl_detect_mod_data_init(&data);
3423 data.pos = first;
3424 found = detect_mod(bmap, &data);
3425 if (!found)
3426 bmap = remove_divs_involving_vars(bmap, first, n);
3427 else
3428 bmap = substitute_div_mod(bmap, first, &data);
3429 isl_detect_mod_data_clear(&data);
3430 return bmap;
3433 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
3434 __isl_take isl_basic_set *bset,
3435 enum isl_dim_type type, unsigned first, unsigned n)
3437 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
3440 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
3441 enum isl_dim_type type, unsigned first, unsigned n)
3443 int i;
3445 if (!map)
3446 return NULL;
3447 if (map->n == 0)
3448 return map;
3450 map = isl_map_cow(map);
3451 if (!map)
3452 return NULL;
3454 for (i = 0; i < map->n; ++i) {
3455 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
3456 type, first, n);
3457 if (!map->p[i])
3458 goto error;
3460 return map;
3461 error:
3462 isl_map_free(map);
3463 return NULL;
3466 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
3467 enum isl_dim_type type, unsigned first, unsigned n)
3469 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
3470 type, first, n));
3473 /* Does the description of "bmap" depend on the specified dimensions?
3474 * We also check whether the dimensions appear in any of the div definitions.
3475 * In principle there is no need for this check. If the dimensions appear
3476 * in a div definition, they also appear in the defining constraints of that
3477 * div.
3479 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
3480 enum isl_dim_type type, unsigned first, unsigned n)
3482 int i;
3484 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3485 return isl_bool_error;
3487 first += isl_basic_map_offset(bmap, type);
3488 for (i = 0; i < bmap->n_eq; ++i)
3489 if (isl_seq_any_non_zero(bmap->eq[i] + first, n))
3490 return isl_bool_true;
3491 for (i = 0; i < bmap->n_ineq; ++i)
3492 if (isl_seq_any_non_zero(bmap->ineq[i] + first, n))
3493 return isl_bool_true;
3494 for (i = 0; i < bmap->n_div; ++i) {
3495 if (isl_int_is_zero(bmap->div[i][0]))
3496 continue;
3497 if (isl_seq_any_non_zero(bmap->div[i] + 1 + first, n))
3498 return isl_bool_true;
3501 return isl_bool_false;
3504 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
3505 enum isl_dim_type type, unsigned first, unsigned n)
3507 int i;
3509 if (isl_map_check_range(map, type, first, n) < 0)
3510 return isl_bool_error;
3512 for (i = 0; i < map->n; ++i) {
3513 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
3514 type, first, n);
3515 if (involves < 0 || involves)
3516 return involves;
3519 return isl_bool_false;
3522 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
3523 enum isl_dim_type type, unsigned first, unsigned n)
3525 return isl_basic_map_involves_dims(bset, type, first, n);
3528 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
3529 enum isl_dim_type type, unsigned first, unsigned n)
3531 return isl_map_involves_dims(set, type, first, n);
3534 /* Does "bset" involve any local variables, i.e., integer divisions?
3536 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
3538 isl_size n;
3540 n = isl_basic_set_dim(bset, isl_dim_div);
3541 if (n < 0)
3542 return isl_bool_error;
3543 return isl_bool_ok(n > 0);
3546 /* isl_set_every_basic_set callback that checks whether "bset"
3547 * is free of local variables.
3549 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
3551 return isl_bool_not(isl_basic_set_involves_locals(bset));
3554 /* Does "set" involve any local variables, i.e., integer divisions?
3556 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3558 isl_bool no_locals;
3560 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3561 return isl_bool_not(no_locals);
3564 /* Drop all constraints in bmap that involve any of the dimensions
3565 * first to first+n-1.
3566 * This function only performs the actual removal of constraints.
3568 * This function should not call finalize since it is used by
3569 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3571 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3572 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3574 int i;
3576 if (n == 0)
3577 return bmap;
3579 bmap = isl_basic_map_cow(bmap);
3581 if (!bmap)
3582 return NULL;
3584 for (i = bmap->n_eq - 1; i >= 0; --i) {
3585 if (!isl_seq_any_non_zero(bmap->eq[i] + 1 + first, n))
3586 continue;
3587 if (isl_basic_map_drop_equality(bmap, i) < 0)
3588 return isl_basic_map_free(bmap);
3591 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3592 if (!isl_seq_any_non_zero(bmap->ineq[i] + 1 + first, n))
3593 continue;
3594 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3595 return isl_basic_map_free(bmap);
3598 return bmap;
3601 /* Drop all constraints in bset that involve any of the dimensions
3602 * first to first+n-1.
3603 * This function only performs the actual removal of constraints.
3605 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3606 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3608 return isl_basic_map_drop_constraints_involving(bset, first, n);
3611 /* Drop all constraints in bmap that do not involve any of the dimensions
3612 * first to first + n - 1 of the given type.
3614 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3615 __isl_take isl_basic_map *bmap,
3616 enum isl_dim_type type, unsigned first, unsigned n)
3618 int i;
3620 if (n == 0) {
3621 isl_space *space = isl_basic_map_get_space(bmap);
3622 isl_basic_map_free(bmap);
3623 return isl_basic_map_universe(space);
3625 bmap = isl_basic_map_cow(bmap);
3626 if (!bmap)
3627 return NULL;
3629 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3630 return isl_basic_map_free(bmap);
3632 first += isl_basic_map_offset(bmap, type) - 1;
3634 for (i = bmap->n_eq - 1; i >= 0; --i) {
3635 if (isl_seq_any_non_zero(bmap->eq[i] + 1 + first, n))
3636 continue;
3637 if (isl_basic_map_drop_equality(bmap, i) < 0)
3638 return isl_basic_map_free(bmap);
3641 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3642 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + first, n))
3643 continue;
3644 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3645 return isl_basic_map_free(bmap);
3648 bmap = isl_basic_map_add_known_div_constraints(bmap);
3649 return bmap;
3652 /* Drop all constraints in bset that do not involve any of the dimensions
3653 * first to first + n - 1 of the given type.
3655 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3656 __isl_take isl_basic_set *bset,
3657 enum isl_dim_type type, unsigned first, unsigned n)
3659 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3660 type, first, n);
3663 /* Drop all constraints in bmap that involve any of the dimensions
3664 * first to first + n - 1 of the given type.
3666 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3667 __isl_take isl_basic_map *bmap,
3668 enum isl_dim_type type, unsigned first, unsigned n)
3670 if (!bmap)
3671 return NULL;
3672 if (n == 0)
3673 return bmap;
3675 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3676 return isl_basic_map_free(bmap);
3678 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3679 first += isl_basic_map_offset(bmap, type) - 1;
3680 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3681 bmap = isl_basic_map_add_known_div_constraints(bmap);
3682 return bmap;
3685 /* Drop all constraints in bset that involve any of the dimensions
3686 * first to first + n - 1 of the given type.
3688 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3689 __isl_take isl_basic_set *bset,
3690 enum isl_dim_type type, unsigned first, unsigned n)
3692 return isl_basic_map_drop_constraints_involving_dims(bset,
3693 type, first, n);
3696 /* Drop constraints from "map" by applying "drop" to each basic map.
3698 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3699 enum isl_dim_type type, unsigned first, unsigned n,
3700 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3701 enum isl_dim_type type, unsigned first, unsigned n))
3703 int i;
3705 if (isl_map_check_range(map, type, first, n) < 0)
3706 return isl_map_free(map);
3708 map = isl_map_cow(map);
3709 if (!map)
3710 return NULL;
3712 for (i = 0; i < map->n; ++i) {
3713 map->p[i] = drop(map->p[i], type, first, n);
3714 if (!map->p[i])
3715 return isl_map_free(map);
3718 if (map->n > 1)
3719 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3721 return map;
3724 /* Drop all constraints in map that involve any of the dimensions
3725 * first to first + n - 1 of the given type.
3727 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3728 __isl_take isl_map *map,
3729 enum isl_dim_type type, unsigned first, unsigned n)
3731 if (n == 0)
3732 return map;
3733 return drop_constraints(map, type, first, n,
3734 &isl_basic_map_drop_constraints_involving_dims);
3737 /* Drop all constraints in "map" that do not involve any of the dimensions
3738 * first to first + n - 1 of the given type.
3740 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3741 __isl_take isl_map *map,
3742 enum isl_dim_type type, unsigned first, unsigned n)
3744 if (n == 0) {
3745 isl_space *space = isl_map_get_space(map);
3746 isl_map_free(map);
3747 return isl_map_universe(space);
3749 return drop_constraints(map, type, first, n,
3750 &isl_basic_map_drop_constraints_not_involving_dims);
3753 /* Drop all constraints in set that involve any of the dimensions
3754 * first to first + n - 1 of the given type.
3756 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3757 __isl_take isl_set *set,
3758 enum isl_dim_type type, unsigned first, unsigned n)
3760 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3763 /* Drop all constraints in "set" that do not involve any of the dimensions
3764 * first to first + n - 1 of the given type.
3766 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3767 __isl_take isl_set *set,
3768 enum isl_dim_type type, unsigned first, unsigned n)
3770 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3773 /* Does local variable "div" of "bmap" have a complete explicit representation?
3774 * Having a complete explicit representation requires not only
3775 * an explicit representation, but also that all local variables
3776 * that appear in this explicit representation in turn have
3777 * a complete explicit representation.
3779 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3781 int i;
3782 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3783 isl_bool marked;
3785 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3786 if (marked < 0 || marked)
3787 return isl_bool_not(marked);
3789 for (i = bmap->n_div - 1; i >= 0; --i) {
3790 isl_bool known;
3792 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3793 continue;
3794 known = isl_basic_map_div_is_known(bmap, i);
3795 if (known < 0 || !known)
3796 return known;
3799 return isl_bool_true;
3802 /* Remove all divs that are unknown or defined in terms of unknown divs.
3804 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3805 __isl_take isl_basic_map *bmap)
3807 int i;
3809 if (!bmap)
3810 return NULL;
3812 for (i = bmap->n_div - 1; i >= 0; --i) {
3813 if (isl_basic_map_div_is_known(bmap, i))
3814 continue;
3815 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3816 if (!bmap)
3817 return NULL;
3818 i = bmap->n_div;
3821 return bmap;
3824 /* Remove all divs that are unknown or defined in terms of unknown divs.
3826 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3827 __isl_take isl_basic_set *bset)
3829 return isl_basic_map_remove_unknown_divs(bset);
3832 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3834 int i;
3836 if (!map)
3837 return NULL;
3838 if (map->n == 0)
3839 return map;
3841 map = isl_map_cow(map);
3842 if (!map)
3843 return NULL;
3845 for (i = 0; i < map->n; ++i) {
3846 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3847 if (!map->p[i])
3848 goto error;
3850 return map;
3851 error:
3852 isl_map_free(map);
3853 return NULL;
3856 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3858 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3861 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3862 __isl_take isl_basic_set *bset,
3863 enum isl_dim_type type, unsigned first, unsigned n)
3865 isl_basic_map *bmap = bset_to_bmap(bset);
3866 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3867 return bset_from_bmap(bmap);
3870 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3871 enum isl_dim_type type, unsigned first, unsigned n)
3873 int i;
3875 if (n == 0)
3876 return map;
3878 map = isl_map_cow(map);
3879 if (isl_map_check_range(map, type, first, n) < 0)
3880 return isl_map_free(map);
3882 for (i = 0; i < map->n; ++i) {
3883 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3884 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3885 if (!map->p[i])
3886 goto error;
3888 map = isl_map_drop(map, type, first, n);
3889 return map;
3890 error:
3891 isl_map_free(map);
3892 return NULL;
3895 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3896 enum isl_dim_type type, unsigned first, unsigned n)
3898 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3899 type, first, n));
3902 /* Project out n inputs starting at first using Fourier-Motzkin */
3903 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3904 unsigned first, unsigned n)
3906 return isl_map_remove_dims(map, isl_dim_in, first, n);
3909 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3910 FILE *out, int indent)
3912 isl_printer *p;
3914 if (!bset) {
3915 fprintf(out, "null basic set\n");
3916 return;
3919 fprintf(out, "%*s", indent, "");
3920 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3921 bset->ref, bset->dim->nparam, bset->dim->n_out,
3922 bset->extra, bset->flags);
3924 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3925 p = isl_printer_set_dump(p, 1);
3926 p = isl_printer_set_indent(p, indent);
3927 p = isl_printer_start_line(p);
3928 p = isl_printer_print_basic_set(p, bset);
3929 p = isl_printer_end_line(p);
3930 isl_printer_free(p);
3933 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3934 FILE *out, int indent)
3936 isl_printer *p;
3938 if (!bmap) {
3939 fprintf(out, "null basic map\n");
3940 return;
3943 fprintf(out, "%*s", indent, "");
3944 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3945 "flags: %x, n_name: %d\n",
3946 bmap->ref,
3947 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3948 bmap->extra, bmap->flags, bmap->dim->n_id);
3950 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3951 p = isl_printer_set_dump(p, 1);
3952 p = isl_printer_set_indent(p, indent);
3953 p = isl_printer_start_line(p);
3954 p = isl_printer_print_basic_map(p, bmap);
3955 p = isl_printer_end_line(p);
3956 isl_printer_free(p);
3959 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3960 unsigned pos)
3962 isl_size total;
3964 total = isl_basic_map_dim(bmap, isl_dim_all);
3965 if (total < 0)
3966 return isl_basic_map_free(bmap);
3967 if (pos >= bmap->n_ineq)
3968 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3969 "invalid position", return isl_basic_map_free(bmap));
3970 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3971 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3972 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3973 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3974 return bmap;
3977 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3978 unsigned flags)
3980 if (isl_space_check_is_set(space) < 0)
3981 goto error;
3982 return isl_map_alloc_space(space, n, flags);
3983 error:
3984 isl_space_free(space);
3985 return NULL;
3988 /* Make sure "map" has room for at least "n" more basic maps.
3990 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3992 int i;
3993 struct isl_map *grown = NULL;
3995 if (!map)
3996 return NULL;
3997 isl_assert(map->ctx, n >= 0, goto error);
3998 if (map->n + n <= map->size)
3999 return map;
4000 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
4001 if (!grown)
4002 goto error;
4003 for (i = 0; i < map->n; ++i) {
4004 grown->p[i] = isl_basic_map_copy(map->p[i]);
4005 if (!grown->p[i])
4006 goto error;
4007 grown->n++;
4009 isl_map_free(map);
4010 return grown;
4011 error:
4012 isl_map_free(grown);
4013 isl_map_free(map);
4014 return NULL;
4017 /* Make sure "set" has room for at least "n" more basic sets.
4019 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
4021 return set_from_map(isl_map_grow(set_to_map(set), n));
4024 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
4026 return isl_map_from_basic_map(bset);
4029 /* This function performs the same operation as isl_set_from_basic_set,
4030 * but is considered as a function on an isl_basic_set when exported.
4032 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
4034 return isl_set_from_basic_set(bset);
4037 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
4039 struct isl_map *map;
4041 if (!bmap)
4042 return NULL;
4044 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
4045 return isl_map_add_basic_map(map, bmap);
4048 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
4049 __isl_take isl_basic_set *bset)
4051 return set_from_map(isl_map_add_basic_map(set_to_map(set),
4052 bset_to_bmap(bset)));
4055 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
4057 return isl_map_free(set);
4060 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
4062 int i;
4064 if (!set) {
4065 fprintf(out, "null set\n");
4066 return;
4069 fprintf(out, "%*s", indent, "");
4070 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
4071 set->ref, set->n, set->dim->nparam, set->dim->n_out,
4072 set->flags);
4073 for (i = 0; i < set->n; ++i) {
4074 fprintf(out, "%*s", indent, "");
4075 fprintf(out, "basic set %d:\n", i);
4076 isl_basic_set_print_internal(set->p[i], out, indent+4);
4080 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
4082 int i;
4084 if (!map) {
4085 fprintf(out, "null map\n");
4086 return;
4089 fprintf(out, "%*s", indent, "");
4090 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
4091 "flags: %x, n_name: %d\n",
4092 map->ref, map->n, map->dim->nparam, map->dim->n_in,
4093 map->dim->n_out, map->flags, map->dim->n_id);
4094 for (i = 0; i < map->n; ++i) {
4095 fprintf(out, "%*s", indent, "");
4096 fprintf(out, "basic map %d:\n", i);
4097 isl_basic_map_print_internal(map->p[i], out, indent+4);
4101 /* Check that the space of "bset" is the same as that of the domain of "bmap".
4103 static isl_stat isl_basic_map_check_compatible_domain(
4104 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
4106 isl_bool ok;
4108 ok = isl_basic_map_compatible_domain(bmap, bset);
4109 if (ok < 0)
4110 return isl_stat_error;
4111 if (!ok)
4112 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
4113 "incompatible spaces", return isl_stat_error);
4115 return isl_stat_ok;
4118 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
4119 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4121 struct isl_basic_map *bmap_domain;
4122 isl_size dim;
4124 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
4125 goto error;
4127 dim = isl_basic_set_dim(bset, isl_dim_set);
4128 if (dim < 0)
4129 goto error;
4130 if (dim != 0 &&
4131 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4132 goto error;
4134 bmap = isl_basic_map_cow(bmap);
4135 if (!bmap)
4136 goto error;
4137 bmap = isl_basic_map_extend(bmap,
4138 bset->n_div, bset->n_eq, bset->n_ineq);
4139 bmap_domain = isl_basic_map_from_domain(bset);
4140 bmap = add_constraints(bmap, bmap_domain, 0, 0);
4142 bmap = isl_basic_map_simplify(bmap);
4143 return isl_basic_map_finalize(bmap);
4144 error:
4145 isl_basic_map_free(bmap);
4146 isl_basic_set_free(bset);
4147 return NULL;
4150 /* Check that the space of "bset" is the same as that of the range of "bmap".
4152 static isl_stat isl_basic_map_check_compatible_range(
4153 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
4155 isl_bool ok;
4157 ok = isl_basic_map_compatible_range(bmap, bset);
4158 if (ok < 0)
4159 return isl_stat_error;
4160 if (!ok)
4161 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
4162 "incompatible spaces", return isl_stat_error);
4164 return isl_stat_ok;
4167 __isl_give isl_basic_map *isl_basic_map_intersect_range(
4168 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4170 struct isl_basic_map *bmap_range;
4171 isl_size dim;
4173 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
4174 goto error;
4176 dim = isl_basic_set_dim(bset, isl_dim_set);
4177 if (dim < 0)
4178 goto error;
4179 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
4180 goto error;
4182 if (isl_basic_set_plain_is_universe(bset)) {
4183 isl_basic_set_free(bset);
4184 return bmap;
4187 bmap = isl_basic_map_cow(bmap);
4188 if (!bmap)
4189 goto error;
4190 bmap = isl_basic_map_extend(bmap,
4191 bset->n_div, bset->n_eq, bset->n_ineq);
4192 bmap_range = bset_to_bmap(bset);
4193 bmap = add_constraints(bmap, bmap_range, 0, 0);
4195 bmap = isl_basic_map_simplify(bmap);
4196 return isl_basic_map_finalize(bmap);
4197 error:
4198 isl_basic_map_free(bmap);
4199 isl_basic_set_free(bset);
4200 return NULL;
4203 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
4204 __isl_keep isl_vec *vec)
4206 int i;
4207 isl_size total;
4208 isl_int s;
4210 total = isl_basic_map_dim(bmap, isl_dim_all);
4211 if (total < 0 || !vec)
4212 return isl_bool_error;
4214 if (1 + total != vec->size)
4215 return isl_bool_false;
4217 isl_int_init(s);
4219 for (i = 0; i < bmap->n_eq; ++i) {
4220 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
4221 if (!isl_int_is_zero(s)) {
4222 isl_int_clear(s);
4223 return isl_bool_false;
4227 for (i = 0; i < bmap->n_ineq; ++i) {
4228 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
4229 if (isl_int_is_neg(s)) {
4230 isl_int_clear(s);
4231 return isl_bool_false;
4235 isl_int_clear(s);
4237 return isl_bool_true;
4240 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
4241 __isl_keep isl_vec *vec)
4243 return isl_basic_map_contains(bset_to_bmap(bset), vec);
4246 __isl_give isl_basic_map *isl_basic_map_intersect(
4247 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4249 struct isl_vec *sample = NULL;
4250 isl_space *space1, *space2;
4251 isl_size dim1, dim2, nparam1, nparam2;
4253 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4254 goto error;
4255 space1 = isl_basic_map_peek_space(bmap1);
4256 space2 = isl_basic_map_peek_space(bmap2);
4257 dim1 = isl_space_dim(space1, isl_dim_all);
4258 dim2 = isl_space_dim(space2, isl_dim_all);
4259 nparam1 = isl_space_dim(space1, isl_dim_param);
4260 nparam2 = isl_space_dim(space2, isl_dim_param);
4261 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
4262 goto error;
4263 if (dim1 == nparam1 && dim2 != nparam2)
4264 return isl_basic_map_intersect(bmap2, bmap1);
4266 if (dim2 != nparam2 &&
4267 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4268 goto error;
4270 if (isl_basic_map_plain_is_empty(bmap1)) {
4271 isl_basic_map_free(bmap2);
4272 return bmap1;
4274 if (isl_basic_map_plain_is_empty(bmap2)) {
4275 isl_basic_map_free(bmap1);
4276 return bmap2;
4279 if (bmap1->sample &&
4280 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
4281 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
4282 sample = isl_vec_copy(bmap1->sample);
4283 else if (bmap2->sample &&
4284 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
4285 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
4286 sample = isl_vec_copy(bmap2->sample);
4288 bmap1 = isl_basic_map_cow(bmap1);
4289 if (!bmap1)
4290 goto error;
4291 bmap1 = isl_basic_map_extend(bmap1,
4292 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4293 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
4295 if (!bmap1)
4296 isl_vec_free(sample);
4297 else if (sample) {
4298 isl_vec_free(bmap1->sample);
4299 bmap1->sample = sample;
4302 bmap1 = isl_basic_map_simplify(bmap1);
4303 return isl_basic_map_finalize(bmap1);
4304 error:
4305 if (sample)
4306 isl_vec_free(sample);
4307 isl_basic_map_free(bmap1);
4308 isl_basic_map_free(bmap2);
4309 return NULL;
4312 __isl_give isl_basic_set *isl_basic_set_intersect(
4313 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
4315 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
4316 bset_to_bmap(bset2)));
4319 /* Intersect the parameter domain of "bmap" with "bset".
4321 * isl_basic_map_intersect handles this as a special case.
4323 __isl_give isl_basic_map *isl_basic_map_intersect_params(
4324 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
4326 return isl_basic_map_intersect(bmap, bset);
4329 __isl_give isl_basic_set *isl_basic_set_intersect_params(
4330 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
4332 isl_basic_map *bmap;
4334 bmap = bset_to_bmap(bset1);
4335 bmap = isl_basic_map_intersect_params(bmap, bset2);
4336 return bset_from_bmap(bmap);
4339 /* Does "map" consist of a single disjunct, without any local variables?
4341 static isl_bool is_convex_no_locals(__isl_keep isl_map *map)
4343 isl_size n_div;
4345 if (!map)
4346 return isl_bool_error;
4347 if (map->n != 1)
4348 return isl_bool_false;
4349 n_div = isl_basic_map_dim(map->p[0], isl_dim_div);
4350 if (n_div < 0)
4351 return isl_bool_error;
4352 if (n_div != 0)
4353 return isl_bool_false;
4354 return isl_bool_true;
4357 /* Check that "map" consists of a single disjunct, without any local variables.
4359 static isl_stat check_convex_no_locals(__isl_keep isl_map *map)
4361 isl_bool ok;
4363 ok = is_convex_no_locals(map);
4364 if (ok < 0)
4365 return isl_stat_error;
4366 if (ok)
4367 return isl_stat_ok;
4369 isl_die(isl_map_get_ctx(map), isl_error_internal,
4370 "unexpectedly not convex or involving local variables",
4371 return isl_stat_error);
4374 /* Special case of isl_map_intersect, where both map1 and map2
4375 * are convex, without any divs and such that either map1 or map2
4376 * contains a single constraint. This constraint is then simply
4377 * added to the other map.
4379 static __isl_give isl_map *map_intersect_add_constraint(
4380 __isl_take isl_map *map1, __isl_take isl_map *map2)
4382 if (check_convex_no_locals(map1) < 0 ||
4383 check_convex_no_locals(map2) < 0)
4384 goto error;
4386 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
4387 return isl_map_intersect(map2, map1);
4389 map1 = isl_map_cow(map1);
4390 if (!map1)
4391 goto error;
4392 if (isl_map_plain_is_empty(map1)) {
4393 isl_map_free(map2);
4394 return map1;
4396 if (map2->p[0]->n_eq == 1)
4397 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
4398 else
4399 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
4400 map2->p[0]->ineq[0]);
4402 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
4403 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
4404 if (!map1->p[0])
4405 goto error;
4407 if (isl_basic_map_plain_is_empty(map1->p[0])) {
4408 isl_basic_map_free(map1->p[0]);
4409 map1->n = 0;
4412 isl_map_free(map2);
4414 map1 = isl_map_unmark_normalized(map1);
4415 return map1;
4416 error:
4417 isl_map_free(map1);
4418 isl_map_free(map2);
4419 return NULL;
4422 /* map2 may be either a parameter domain or a map living in the same
4423 * space as map1.
4425 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
4426 __isl_take isl_map *map2)
4428 unsigned flags = 0;
4429 isl_bool equal;
4430 isl_map *result;
4431 int i, j;
4432 isl_size dim2, nparam2;
4434 if (!map1 || !map2)
4435 goto error;
4437 if ((isl_map_plain_is_empty(map1) ||
4438 isl_map_plain_is_universe(map2)) &&
4439 isl_space_is_equal(map1->dim, map2->dim)) {
4440 isl_map_free(map2);
4441 return map1;
4443 if ((isl_map_plain_is_empty(map2) ||
4444 isl_map_plain_is_universe(map1)) &&
4445 isl_space_is_equal(map1->dim, map2->dim)) {
4446 isl_map_free(map1);
4447 return map2;
4450 if (is_convex_no_locals(map1) == isl_bool_true &&
4451 is_convex_no_locals(map2) == isl_bool_true &&
4452 isl_space_is_equal(map1->dim, map2->dim) &&
4453 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
4454 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
4455 return map_intersect_add_constraint(map1, map2);
4457 equal = isl_map_plain_is_equal(map1, map2);
4458 if (equal < 0)
4459 goto error;
4460 if (equal) {
4461 isl_map_free(map2);
4462 return map1;
4465 dim2 = isl_map_dim(map2, isl_dim_all);
4466 nparam2 = isl_map_dim(map2, isl_dim_param);
4467 if (dim2 < 0 || nparam2 < 0)
4468 goto error;
4469 if (dim2 != nparam2)
4470 isl_assert(map1->ctx,
4471 isl_space_is_equal(map1->dim, map2->dim), goto error);
4473 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4474 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4475 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4477 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4478 map1->n * map2->n, flags);
4479 if (!result)
4480 goto error;
4481 for (i = 0; i < map1->n; ++i)
4482 for (j = 0; j < map2->n; ++j) {
4483 struct isl_basic_map *part;
4484 part = isl_basic_map_intersect(
4485 isl_basic_map_copy(map1->p[i]),
4486 isl_basic_map_copy(map2->p[j]));
4487 if (isl_basic_map_is_empty(part) < 0)
4488 part = isl_basic_map_free(part);
4489 result = isl_map_add_basic_map(result, part);
4490 if (!result)
4491 goto error;
4493 isl_map_free(map1);
4494 isl_map_free(map2);
4495 return result;
4496 error:
4497 isl_map_free(map1);
4498 isl_map_free(map2);
4499 return NULL;
4502 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
4503 __isl_take isl_map *map2)
4505 if (isl_map_check_equal_space(map1, map2) < 0)
4506 goto error;
4507 return map_intersect_internal(map1, map2);
4508 error:
4509 isl_map_free(map1);
4510 isl_map_free(map2);
4511 return NULL;
4514 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
4515 __isl_take isl_map *map2)
4517 isl_map_align_params_bin(&map1, &map2);
4518 return map_intersect(map1, map2);
4521 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
4522 __isl_take isl_set *set2)
4524 return set_from_map(isl_map_intersect(set_to_map(set1),
4525 set_to_map(set2)));
4528 /* map_intersect_internal accepts intersections
4529 * with parameter domains, so we can just call that function.
4531 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
4532 __isl_take isl_set *params)
4534 isl_map_align_params_set(&map, &params);
4535 return map_intersect_internal(map, params);
4538 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
4539 __isl_take isl_set *params)
4541 return isl_map_intersect_params(set, params);
4544 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
4546 isl_space *space;
4547 unsigned pos;
4548 isl_size n1, n2;
4550 if (!bmap)
4551 return NULL;
4552 bmap = isl_basic_map_cow(bmap);
4553 if (!bmap)
4554 return NULL;
4555 space = isl_space_reverse(isl_space_copy(bmap->dim));
4556 pos = isl_basic_map_offset(bmap, isl_dim_in);
4557 n1 = isl_basic_map_dim(bmap, isl_dim_in);
4558 n2 = isl_basic_map_dim(bmap, isl_dim_out);
4559 if (n1 < 0 || n2 < 0)
4560 bmap = isl_basic_map_free(bmap);
4561 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
4562 return isl_basic_map_reset_space(bmap, space);
4565 /* Given a basic map where the tuple of type "type" is a wrapped map,
4566 * swap domain and range of that wrapped map.
4568 static __isl_give isl_basic_map *isl_basic_map_reverse_wrapped(
4569 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4571 isl_space *space;
4572 isl_size offset, n1, n2;
4574 space = isl_basic_map_peek_space(bmap);
4575 offset = isl_basic_map_var_offset(bmap, type);
4576 n1 = isl_space_wrapped_dim(space, type, isl_dim_in);
4577 n2 = isl_space_wrapped_dim(space, type, isl_dim_out);
4578 if (offset < 0 || n1 < 0 || n2 < 0)
4579 return isl_basic_map_free(bmap);
4581 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
4583 space = isl_basic_map_take_space(bmap);
4584 space = isl_space_reverse_wrapped(space, type);
4585 bmap = isl_basic_map_restore_space(bmap, space);
4587 return bmap;
4590 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4591 * (B -> A) -> C.
4593 static __isl_give isl_basic_map *isl_basic_map_domain_reverse(
4594 __isl_take isl_basic_map *bmap)
4596 isl_space *space;
4598 space = isl_basic_map_peek_space(bmap);
4599 if (isl_space_check_domain_is_wrapping(space) < 0)
4600 return isl_basic_map_free(bmap);
4601 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_in);
4603 return bmap;
4606 /* Given a basic map A -> (B -> C), return the corresponding basic map
4607 * A -> (C -> B).
4609 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
4610 __isl_take isl_basic_map *bmap)
4612 isl_space *space;
4614 space = isl_basic_map_peek_space(bmap);
4615 if (isl_space_check_range_is_wrapping(space) < 0)
4616 return isl_basic_map_free(bmap);
4617 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_out);
4619 return bmap;
4622 /* Given a basic map that is actually a basic set (A -> B),
4623 * return the corresponding basic set (B -> A) as a basic map.
4625 static __isl_give isl_basic_map *isl_basic_map_set_reverse(
4626 __isl_take isl_basic_map *bmap)
4628 isl_space *space;
4630 space = isl_basic_map_peek_space(bmap);
4631 if (isl_space_check_is_wrapping(space) < 0)
4632 return isl_basic_map_free(bmap);
4633 bmap = isl_basic_map_reverse_wrapped(bmap, isl_dim_set);
4635 return bmap;
4638 static __isl_give isl_basic_map *basic_map_space_reset(
4639 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
4641 isl_space *space;
4643 if (!bmap)
4644 return NULL;
4645 if (!isl_space_is_named_or_nested(bmap->dim, type))
4646 return bmap;
4648 space = isl_basic_map_get_space(bmap);
4649 space = isl_space_reset(space, type);
4650 bmap = isl_basic_map_reset_space(bmap, space);
4651 return bmap;
4654 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4655 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4656 unsigned pos, unsigned n)
4658 isl_bool rational, is_empty;
4659 isl_space *res_space;
4660 struct isl_basic_map *res;
4661 struct isl_dim_map *dim_map;
4662 isl_size total;
4663 unsigned off;
4664 enum isl_dim_type t;
4666 if (n == 0)
4667 return basic_map_space_reset(bmap, type);
4669 is_empty = isl_basic_map_plain_is_empty(bmap);
4670 total = isl_basic_map_dim(bmap, isl_dim_all);
4671 if (is_empty < 0 || total < 0)
4672 return isl_basic_map_free(bmap);
4673 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4674 type, pos, n);
4675 if (!res_space)
4676 return isl_basic_map_free(bmap);
4677 if (is_empty) {
4678 isl_basic_map_free(bmap);
4679 return isl_basic_map_empty(res_space);
4682 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4683 off = 0;
4684 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4685 isl_size dim;
4687 if (t != type) {
4688 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4689 } else {
4690 isl_size size = isl_basic_map_dim(bmap, t);
4691 if (size < 0)
4692 dim_map = isl_dim_map_free(dim_map);
4693 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4694 0, pos, off);
4695 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4696 pos, size - pos, off + pos + n);
4698 dim = isl_space_dim(res_space, t);
4699 if (dim < 0)
4700 dim_map = isl_dim_map_free(dim_map);
4701 off += dim;
4703 isl_dim_map_div(dim_map, bmap, off);
4705 res = isl_basic_map_alloc_space(res_space,
4706 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4707 rational = isl_basic_map_is_rational(bmap);
4708 if (rational < 0)
4709 res = isl_basic_map_free(res);
4710 if (rational)
4711 res = isl_basic_map_set_rational(res);
4712 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4713 return isl_basic_map_finalize(res);
4716 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4717 __isl_take isl_basic_set *bset,
4718 enum isl_dim_type type, unsigned pos, unsigned n)
4720 return isl_basic_map_insert_dims(bset, type, pos, n);
4723 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4724 enum isl_dim_type type, unsigned n)
4726 isl_size dim;
4728 dim = isl_basic_map_dim(bmap, type);
4729 if (dim < 0)
4730 return isl_basic_map_free(bmap);
4731 return isl_basic_map_insert_dims(bmap, type, dim, n);
4734 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4735 enum isl_dim_type type, unsigned n)
4737 if (!bset)
4738 return NULL;
4739 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4740 return isl_basic_map_add_dims(bset, type, n);
4741 error:
4742 isl_basic_set_free(bset);
4743 return NULL;
4746 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4747 enum isl_dim_type type)
4749 isl_space *space;
4751 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4752 return map;
4754 space = isl_map_get_space(map);
4755 space = isl_space_reset(space, type);
4756 map = isl_map_reset_space(map, space);
4757 return map;
4760 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4761 enum isl_dim_type type, unsigned pos, unsigned n)
4763 int i;
4764 isl_space *space;
4766 if (n == 0)
4767 return map_space_reset(map, type);
4769 map = isl_map_cow(map);
4770 if (!map)
4771 return NULL;
4773 for (i = 0; i < map->n; ++i) {
4774 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4775 if (!map->p[i])
4776 goto error;
4779 space = isl_map_take_space(map);
4780 space = isl_space_insert_dims(space, type, pos, n);
4781 map = isl_map_restore_space(map, space);
4783 return map;
4784 error:
4785 isl_map_free(map);
4786 return NULL;
4789 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4790 enum isl_dim_type type, unsigned pos, unsigned n)
4792 return isl_map_insert_dims(set, type, pos, n);
4795 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4796 enum isl_dim_type type, unsigned n)
4798 isl_size dim;
4800 dim = isl_map_dim(map, type);
4801 if (dim < 0)
4802 return isl_map_free(map);
4803 return isl_map_insert_dims(map, type, dim, n);
4806 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4807 enum isl_dim_type type, unsigned n)
4809 if (!set)
4810 return NULL;
4811 isl_assert(set->ctx, type != isl_dim_in, goto error);
4812 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4813 error:
4814 isl_set_free(set);
4815 return NULL;
4818 __isl_give isl_basic_map *isl_basic_map_move_dims(
4819 __isl_take isl_basic_map *bmap,
4820 enum isl_dim_type dst_type, unsigned dst_pos,
4821 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4823 isl_space *space;
4824 struct isl_dim_map *dim_map;
4825 struct isl_basic_map *res;
4826 enum isl_dim_type t;
4827 isl_size total;
4828 unsigned off;
4830 if (!bmap)
4831 return NULL;
4832 if (n == 0) {
4833 bmap = isl_basic_map_reset(bmap, src_type);
4834 bmap = isl_basic_map_reset(bmap, dst_type);
4835 return bmap;
4838 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4839 return isl_basic_map_free(bmap);
4841 if (dst_type == src_type && dst_pos == src_pos)
4842 return bmap;
4844 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4846 if (pos(bmap->dim, dst_type) + dst_pos ==
4847 pos(bmap->dim, src_type) + src_pos +
4848 ((src_type < dst_type) ? n : 0)) {
4849 space = isl_basic_map_take_space(bmap);
4850 space = isl_space_move_dims(space, dst_type, dst_pos,
4851 src_type, src_pos, n);
4852 bmap = isl_basic_map_restore_space(bmap, space);
4853 bmap = isl_basic_map_finalize(bmap);
4855 return bmap;
4858 total = isl_basic_map_dim(bmap, isl_dim_all);
4859 if (total < 0)
4860 return isl_basic_map_free(bmap);
4861 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4863 off = 0;
4864 space = isl_basic_map_peek_space(bmap);
4865 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4866 isl_size size = isl_space_dim(space, t);
4867 if (size < 0)
4868 dim_map = isl_dim_map_free(dim_map);
4869 if (t == dst_type) {
4870 isl_dim_map_dim_range(dim_map, space, t,
4871 0, dst_pos, off);
4872 off += dst_pos;
4873 isl_dim_map_dim_range(dim_map, space, src_type,
4874 src_pos, n, off);
4875 off += n;
4876 isl_dim_map_dim_range(dim_map, space, t,
4877 dst_pos, size - dst_pos, off);
4878 off += size - dst_pos;
4879 } else if (t == src_type) {
4880 isl_dim_map_dim_range(dim_map, space, t,
4881 0, src_pos, off);
4882 off += src_pos;
4883 isl_dim_map_dim_range(dim_map, space, t,
4884 src_pos + n, size - src_pos - n, off);
4885 off += size - src_pos - n;
4886 } else {
4887 isl_dim_map_dim(dim_map, space, t, off);
4888 off += size;
4891 isl_dim_map_div(dim_map, bmap, off);
4893 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4894 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4895 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4896 space = isl_basic_map_take_space(bmap);
4897 space = isl_space_move_dims(space, dst_type, dst_pos,
4898 src_type, src_pos, n);
4899 bmap = isl_basic_map_restore_space(bmap, space);
4900 if (!bmap)
4901 goto error;
4903 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4904 bmap = isl_basic_map_gauss(bmap, NULL);
4905 bmap = isl_basic_map_finalize(bmap);
4907 return bmap;
4908 error:
4909 isl_basic_map_free(bmap);
4910 return NULL;
4913 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4914 enum isl_dim_type dst_type, unsigned dst_pos,
4915 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4917 isl_basic_map *bmap = bset_to_bmap(bset);
4918 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4919 src_type, src_pos, n);
4920 return bset_from_bmap(bmap);
4923 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4924 enum isl_dim_type dst_type, unsigned dst_pos,
4925 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4927 if (!set)
4928 return NULL;
4929 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4930 return set_from_map(isl_map_move_dims(set_to_map(set),
4931 dst_type, dst_pos, src_type, src_pos, n));
4932 error:
4933 isl_set_free(set);
4934 return NULL;
4937 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4938 enum isl_dim_type dst_type, unsigned dst_pos,
4939 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4941 int i;
4942 isl_space *space;
4944 if (n == 0) {
4945 map = isl_map_reset(map, src_type);
4946 map = isl_map_reset(map, dst_type);
4947 return map;
4950 if (isl_map_check_range(map, src_type, src_pos, n))
4951 return isl_map_free(map);
4953 if (dst_type == src_type && dst_pos == src_pos)
4954 return map;
4956 isl_assert(map->ctx, dst_type != src_type, goto error);
4958 map = isl_map_cow(map);
4959 if (!map)
4960 return NULL;
4962 for (i = 0; i < map->n; ++i) {
4963 map->p[i] = isl_basic_map_move_dims(map->p[i],
4964 dst_type, dst_pos,
4965 src_type, src_pos, n);
4966 if (!map->p[i])
4967 goto error;
4970 space = isl_map_take_space(map);
4971 space = isl_space_move_dims(space, dst_type, dst_pos,
4972 src_type, src_pos, n);
4973 map = isl_map_restore_space(map, space);
4975 return map;
4976 error:
4977 isl_map_free(map);
4978 return NULL;
4981 /* Move the specified dimensions to the last columns right before
4982 * the divs. Don't change the dimension specification of bmap.
4983 * That's the responsibility of the caller.
4985 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4986 enum isl_dim_type type, unsigned first, unsigned n)
4988 isl_space *space;
4989 struct isl_dim_map *dim_map;
4990 struct isl_basic_map *res;
4991 enum isl_dim_type t;
4992 isl_size total;
4993 unsigned off;
4995 if (!bmap)
4996 return NULL;
4997 if (isl_basic_map_offset(bmap, type) + first + n ==
4998 isl_basic_map_offset(bmap, isl_dim_div))
4999 return bmap;
5001 total = isl_basic_map_dim(bmap, isl_dim_all);
5002 if (total < 0)
5003 return isl_basic_map_free(bmap);
5004 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5006 off = 0;
5007 space = isl_basic_map_peek_space(bmap);
5008 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
5009 isl_size size = isl_space_dim(space, t);
5010 if (size < 0)
5011 dim_map = isl_dim_map_free(dim_map);
5012 if (t == type) {
5013 isl_dim_map_dim_range(dim_map, space, t,
5014 0, first, off);
5015 off += first;
5016 isl_dim_map_dim_range(dim_map, space, t,
5017 first, n, total - bmap->n_div - n);
5018 isl_dim_map_dim_range(dim_map, space, t,
5019 first + n, size - (first + n), off);
5020 off += size - (first + n);
5021 } else {
5022 isl_dim_map_dim(dim_map, space, t, off);
5023 off += size;
5026 isl_dim_map_div(dim_map, bmap, off + n);
5028 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
5029 bmap->n_div, bmap->n_eq, bmap->n_ineq);
5030 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
5031 return res;
5034 /* Insert "n" rows in the divs of "bmap".
5036 * The number of columns is not changed, which means that the last
5037 * dimensions of "bmap" are being reintepreted as the new divs.
5038 * The space of "bmap" is not adjusted, however, which means
5039 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
5040 * from the space of "bmap" is the responsibility of the caller.
5042 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
5043 int n)
5045 int i;
5046 size_t row_size;
5047 isl_int **new_div;
5048 isl_int *old;
5050 bmap = isl_basic_map_cow(bmap);
5051 if (!bmap)
5052 return NULL;
5054 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
5055 old = bmap->block2.data;
5056 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
5057 (bmap->extra + n) * (1 + row_size));
5058 if (!bmap->block2.data)
5059 return isl_basic_map_free(bmap);
5060 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
5061 if (!new_div)
5062 return isl_basic_map_free(bmap);
5063 for (i = 0; i < n; ++i) {
5064 new_div[i] = bmap->block2.data +
5065 (bmap->extra + i) * (1 + row_size);
5066 isl_seq_clr(new_div[i], 1 + row_size);
5068 for (i = 0; i < bmap->extra; ++i)
5069 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
5070 free(bmap->div);
5071 bmap->div = new_div;
5072 bmap->n_div += n;
5073 bmap->extra += n;
5075 return bmap;
5078 /* Drop constraints from "bmap" that only involve the variables
5079 * of "type" in the range [first, first + n] that are not related
5080 * to any of the variables outside that interval.
5081 * These constraints cannot influence the values for the variables
5082 * outside the interval, except in case they cause "bmap" to be empty.
5083 * Only drop the constraints if "bmap" is known to be non-empty.
5085 static __isl_give isl_basic_map *drop_irrelevant_constraints(
5086 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
5087 unsigned first, unsigned n)
5089 int i;
5090 int *groups;
5091 isl_size dim, n_div;
5092 isl_bool non_empty;
5094 non_empty = isl_basic_map_plain_is_non_empty(bmap);
5095 if (non_empty < 0)
5096 return isl_basic_map_free(bmap);
5097 if (!non_empty)
5098 return bmap;
5100 dim = isl_basic_map_dim(bmap, isl_dim_all);
5101 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5102 if (dim < 0 || n_div < 0)
5103 return isl_basic_map_free(bmap);
5104 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
5105 if (!groups)
5106 return isl_basic_map_free(bmap);
5107 first += isl_basic_map_offset(bmap, type) - 1;
5108 for (i = 0; i < first; ++i)
5109 groups[i] = -1;
5110 for (i = first + n; i < dim - n_div; ++i)
5111 groups[i] = -1;
5113 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
5115 return bmap;
5118 /* Turn the n dimensions of type type, starting at first
5119 * into existentially quantified variables.
5121 * If a subset of the projected out variables are unrelated
5122 * to any of the variables that remain, then the constraints
5123 * involving this subset are simply dropped first.
5125 __isl_give isl_basic_map *isl_basic_map_project_out(
5126 __isl_take isl_basic_map *bmap,
5127 enum isl_dim_type type, unsigned first, unsigned n)
5129 isl_bool empty;
5130 isl_space *space;
5132 if (n == 0)
5133 return basic_map_space_reset(bmap, type);
5134 if (type == isl_dim_div)
5135 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5136 "cannot project out existentially quantified variables",
5137 return isl_basic_map_free(bmap));
5139 empty = isl_basic_map_plain_is_empty(bmap);
5140 if (empty < 0)
5141 return isl_basic_map_free(bmap);
5142 if (empty)
5143 bmap = isl_basic_map_set_to_empty(bmap);
5145 bmap = drop_irrelevant_constraints(bmap, type, first, n);
5146 if (!bmap)
5147 return NULL;
5149 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
5150 return isl_basic_map_remove_dims(bmap, type, first, n);
5152 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
5153 return isl_basic_map_free(bmap);
5155 bmap = move_last(bmap, type, first, n);
5156 bmap = isl_basic_map_cow(bmap);
5157 bmap = insert_div_rows(bmap, n);
5159 space = isl_basic_map_take_space(bmap);
5160 space = isl_space_drop_dims(space, type, first, n);
5161 bmap = isl_basic_map_restore_space(bmap, space);
5162 bmap = isl_basic_map_simplify(bmap);
5163 bmap = isl_basic_map_drop_redundant_divs(bmap);
5164 return isl_basic_map_finalize(bmap);
5167 /* Turn the n dimensions of type type, starting at first
5168 * into existentially quantified variables.
5170 __isl_give isl_basic_set *isl_basic_set_project_out(
5171 __isl_take isl_basic_set *bset, enum isl_dim_type type,
5172 unsigned first, unsigned n)
5174 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
5175 type, first, n));
5178 /* Turn the n dimensions of type type, starting at first
5179 * into existentially quantified variables.
5181 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
5182 enum isl_dim_type type, unsigned first, unsigned n)
5184 int i;
5185 isl_space *space;
5187 if (n == 0)
5188 return map_space_reset(map, type);
5190 if (isl_map_check_range(map, type, first, n) < 0)
5191 return isl_map_free(map);
5193 map = isl_map_cow(map);
5194 if (!map)
5195 return NULL;
5197 for (i = 0; i < map->n; ++i) {
5198 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
5199 if (!map->p[i])
5200 goto error;
5203 if (map->n > 1)
5204 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5205 map = isl_map_unmark_normalized(map);
5207 space = isl_map_take_space(map);
5208 space = isl_space_drop_dims(space, type, first, n);
5209 map = isl_map_restore_space(map, space);
5211 return map;
5212 error:
5213 isl_map_free(map);
5214 return NULL;
5217 #undef TYPE
5218 #define TYPE isl_map
5219 #include "isl_project_out_all_params_templ.c"
5220 #include "isl_project_out_param_templ.c"
5222 /* Turn all the dimensions of type "type", except the "n" starting at "first"
5223 * into existentially quantified variables.
5225 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
5226 enum isl_dim_type type, unsigned first, unsigned n)
5228 isl_size dim;
5230 dim = isl_map_dim(map, type);
5231 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
5232 return isl_map_free(map);
5233 map = isl_map_project_out(map, type, first + n, dim - (first + n));
5234 map = isl_map_project_out(map, type, 0, first);
5235 return map;
5238 /* Turn the n dimensions of type type, starting at first
5239 * into existentially quantified variables.
5241 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
5242 enum isl_dim_type type, unsigned first, unsigned n)
5244 return set_from_map(isl_map_project_out(set_to_map(set),
5245 type, first, n));
5248 /* If "set" involves a parameter with identifier "id",
5249 * then turn it into an existentially quantified variable.
5251 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
5252 __isl_take isl_id *id)
5254 return set_from_map(isl_map_project_out_param_id(set_to_map(set), id));
5257 /* If "set" involves any of the parameters with identifiers in "list",
5258 * then turn them into existentially quantified variables.
5260 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
5261 __isl_take isl_id_list *list)
5263 isl_map *map;
5265 map = set_to_map(set);
5266 map = isl_map_project_out_param_id_list(map, list);
5267 return set_from_map(map);
5270 /* Project out all parameters from "set" by existentially quantifying
5271 * over them.
5273 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
5275 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
5278 /* Return a map that projects the elements in "set" onto their
5279 * "n" set dimensions starting at "first".
5280 * "type" should be equal to isl_dim_set.
5282 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
5283 enum isl_dim_type type, unsigned first, unsigned n)
5285 int i;
5286 isl_map *map;
5288 if (type != isl_dim_set)
5289 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5290 "only set dimensions can be projected out", goto error);
5291 if (isl_set_check_range(set, type, first, n) < 0)
5292 return isl_set_free(set);
5294 map = isl_map_from_domain(set);
5295 map = isl_map_add_dims(map, isl_dim_out, n);
5296 for (i = 0; i < n; ++i)
5297 map = isl_map_equate(map, isl_dim_in, first + i,
5298 isl_dim_out, i);
5299 return map;
5300 error:
5301 isl_set_free(set);
5302 return NULL;
5305 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
5306 unsigned n)
5308 int i, j;
5309 isl_size total;
5311 total = isl_basic_map_dim(bmap, isl_dim_all);
5312 if (total < 0)
5313 return isl_basic_map_free(bmap);
5314 for (i = 0; i < n; ++i) {
5315 j = isl_basic_map_alloc_div(bmap);
5316 if (j < 0)
5317 goto error;
5318 isl_seq_clr(bmap->div[j], 1 + 1 + total);
5320 return bmap;
5321 error:
5322 isl_basic_map_free(bmap);
5323 return NULL;
5326 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
5328 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
5329 __isl_keep isl_basic_map *bmap2)
5331 isl_space *space1, *space2;
5333 space1 = isl_basic_map_peek_space(bmap1);
5334 space2 = isl_basic_map_peek_space(bmap2);
5335 return isl_space_tuple_is_equal(space1, isl_dim_out,
5336 space2, isl_dim_in);
5339 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
5341 static isl_stat isl_basic_map_check_applies_range(
5342 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
5344 isl_bool equal;
5346 equal = isl_basic_map_applies_range(bmap1, bmap2);
5347 if (equal < 0)
5348 return isl_stat_error;
5349 if (!equal)
5350 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
5351 "spaces don't match", return isl_stat_error);
5352 return isl_stat_ok;
5355 __isl_give isl_basic_map *isl_basic_map_apply_range(
5356 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
5358 isl_space *space_result = NULL;
5359 struct isl_basic_map *bmap;
5360 isl_size n_in, n_out, n, nparam;
5361 unsigned total, pos;
5362 struct isl_dim_map *dim_map1, *dim_map2;
5364 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
5365 goto error;
5366 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
5367 goto error;
5369 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
5370 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
5371 n = isl_basic_map_dim(bmap1, isl_dim_out);
5372 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
5373 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
5374 goto error;
5376 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
5377 isl_basic_map_get_space(bmap2));
5379 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
5380 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5381 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5382 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5383 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5384 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5385 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
5386 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
5387 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5388 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
5389 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
5391 bmap = isl_basic_map_alloc_space(space_result,
5392 bmap1->n_div + bmap2->n_div + n,
5393 bmap1->n_eq + bmap2->n_eq,
5394 bmap1->n_ineq + bmap2->n_ineq);
5395 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
5396 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5397 bmap = add_divs(bmap, n);
5398 bmap = isl_basic_map_simplify(bmap);
5399 bmap = isl_basic_map_drop_redundant_divs(bmap);
5400 return isl_basic_map_finalize(bmap);
5401 error:
5402 isl_basic_map_free(bmap1);
5403 isl_basic_map_free(bmap2);
5404 return NULL;
5407 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
5408 __isl_take isl_basic_map *bmap)
5410 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
5411 goto error;
5413 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
5414 bmap));
5415 error:
5416 isl_basic_set_free(bset);
5417 isl_basic_map_free(bmap);
5418 return NULL;
5421 __isl_give isl_basic_map *isl_basic_map_apply_domain(
5422 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
5424 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
5425 goto error;
5426 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
5427 bmap2->dim, isl_dim_in))
5428 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
5429 "spaces don't match", goto error);
5431 bmap1 = isl_basic_map_reverse(bmap1);
5432 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
5433 return isl_basic_map_reverse(bmap1);
5434 error:
5435 isl_basic_map_free(bmap1);
5436 isl_basic_map_free(bmap2);
5437 return NULL;
5440 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5441 * A \cap B -> f(A) + f(B)
5443 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
5444 __isl_take isl_basic_map *bmap2)
5446 isl_size n_in, n_out, nparam;
5447 unsigned total, pos;
5448 struct isl_basic_map *bmap = NULL;
5449 struct isl_dim_map *dim_map1, *dim_map2;
5450 int i;
5452 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
5453 goto error;
5455 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
5456 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
5457 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
5458 if (nparam < 0 || n_in < 0 || n_out < 0)
5459 goto error;
5461 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
5462 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5463 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
5464 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5465 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
5466 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5467 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
5468 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
5469 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5470 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
5471 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
5473 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
5474 bmap1->n_div + bmap2->n_div + 2 * n_out,
5475 bmap1->n_eq + bmap2->n_eq + n_out,
5476 bmap1->n_ineq + bmap2->n_ineq);
5477 for (i = 0; i < n_out; ++i) {
5478 int j = isl_basic_map_alloc_equality(bmap);
5479 if (j < 0)
5480 goto error;
5481 isl_seq_clr(bmap->eq[j], 1+total);
5482 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
5483 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
5484 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
5486 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
5487 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
5488 bmap = add_divs(bmap, 2 * n_out);
5490 bmap = isl_basic_map_simplify(bmap);
5491 return isl_basic_map_finalize(bmap);
5492 error:
5493 isl_basic_map_free(bmap);
5494 isl_basic_map_free(bmap1);
5495 isl_basic_map_free(bmap2);
5496 return NULL;
5499 /* Given two maps A -> f(A) and B -> g(B), construct a map
5500 * A \cap B -> f(A) + f(B)
5502 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
5503 __isl_take isl_map *map2)
5505 struct isl_map *result;
5506 int i, j;
5508 if (isl_map_check_equal_space(map1, map2) < 0)
5509 goto error;
5511 result = isl_map_alloc_space(isl_space_copy(map1->dim),
5512 map1->n * map2->n, 0);
5513 if (!result)
5514 goto error;
5515 for (i = 0; i < map1->n; ++i)
5516 for (j = 0; j < map2->n; ++j) {
5517 struct isl_basic_map *part;
5518 part = isl_basic_map_sum(
5519 isl_basic_map_copy(map1->p[i]),
5520 isl_basic_map_copy(map2->p[j]));
5521 if (isl_basic_map_is_empty(part))
5522 isl_basic_map_free(part);
5523 else
5524 result = isl_map_add_basic_map(result, part);
5525 if (!result)
5526 goto error;
5528 isl_map_free(map1);
5529 isl_map_free(map2);
5530 return result;
5531 error:
5532 isl_map_free(map1);
5533 isl_map_free(map2);
5534 return NULL;
5537 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
5538 __isl_take isl_set *set2)
5540 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
5543 /* Given a basic map A -> f(A), construct A -> -f(A).
5545 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
5547 int i, j;
5548 unsigned off;
5549 isl_size n;
5551 bmap = isl_basic_map_cow(bmap);
5552 n = isl_basic_map_dim(bmap, isl_dim_out);
5553 if (n < 0)
5554 return isl_basic_map_free(bmap);
5556 off = isl_basic_map_offset(bmap, isl_dim_out);
5557 for (i = 0; i < bmap->n_eq; ++i)
5558 for (j = 0; j < n; ++j)
5559 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
5560 for (i = 0; i < bmap->n_ineq; ++i)
5561 for (j = 0; j < n; ++j)
5562 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
5563 for (i = 0; i < bmap->n_div; ++i)
5564 for (j = 0; j < n; ++j)
5565 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
5566 bmap = isl_basic_map_gauss(bmap, NULL);
5567 return isl_basic_map_finalize(bmap);
5570 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
5572 return isl_basic_map_neg(bset);
5575 /* Given a map A -> f(A), construct A -> -f(A).
5577 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
5579 int i;
5581 map = isl_map_cow(map);
5582 if (!map)
5583 return NULL;
5585 for (i = 0; i < map->n; ++i) {
5586 map->p[i] = isl_basic_map_neg(map->p[i]);
5587 if (!map->p[i])
5588 goto error;
5591 return map;
5592 error:
5593 isl_map_free(map);
5594 return NULL;
5597 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
5599 return set_from_map(isl_map_neg(set_to_map(set)));
5602 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5603 * A -> floor(f(A)/d).
5605 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
5606 isl_int d)
5608 isl_size n_in, n_out, nparam;
5609 unsigned total, pos;
5610 struct isl_basic_map *result = NULL;
5611 struct isl_dim_map *dim_map;
5612 int i;
5614 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5615 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5616 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5617 if (nparam < 0 || n_in < 0 || n_out < 0)
5618 return isl_basic_map_free(bmap);
5620 total = nparam + n_in + n_out + bmap->n_div + n_out;
5621 dim_map = isl_dim_map_alloc(bmap->ctx, total);
5622 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
5623 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
5624 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5625 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5627 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5628 bmap->n_div + n_out,
5629 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5630 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5631 result = add_divs(result, n_out);
5632 for (i = 0; i < n_out; ++i) {
5633 int j;
5634 j = isl_basic_map_alloc_inequality(result);
5635 if (j < 0)
5636 goto error;
5637 isl_seq_clr(result->ineq[j], 1+total);
5638 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5639 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5640 j = isl_basic_map_alloc_inequality(result);
5641 if (j < 0)
5642 goto error;
5643 isl_seq_clr(result->ineq[j], 1+total);
5644 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5645 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5646 isl_int_sub_ui(result->ineq[j][0], d, 1);
5649 result = isl_basic_map_simplify(result);
5650 return isl_basic_map_finalize(result);
5651 error:
5652 isl_basic_map_free(result);
5653 return NULL;
5656 /* Given a map A -> f(A) and an integer d, construct a map
5657 * A -> floor(f(A)/d).
5659 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5661 int i;
5663 map = isl_map_cow(map);
5664 if (!map)
5665 return NULL;
5667 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5668 for (i = 0; i < map->n; ++i) {
5669 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5670 if (!map->p[i])
5671 goto error;
5673 map = isl_map_unmark_normalized(map);
5675 return map;
5676 error:
5677 isl_map_free(map);
5678 return NULL;
5681 /* Given a map A -> f(A) and an integer d, construct a map
5682 * A -> floor(f(A)/d).
5684 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5685 __isl_take isl_val *d)
5687 if (!map || !d)
5688 goto error;
5689 if (!isl_val_is_int(d))
5690 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5691 "expecting integer denominator", goto error);
5692 map = isl_map_floordiv(map, d->n);
5693 isl_val_free(d);
5694 return map;
5695 error:
5696 isl_map_free(map);
5697 isl_val_free(d);
5698 return NULL;
5701 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5702 unsigned pos)
5704 int i;
5705 isl_size nparam;
5706 isl_size n_in;
5707 isl_size total;
5709 total = isl_basic_map_dim(bmap, isl_dim_all);
5710 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5711 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5712 if (total < 0 || nparam < 0 || n_in < 0)
5713 return isl_basic_map_free(bmap);
5714 i = isl_basic_map_alloc_equality(bmap);
5715 if (i < 0)
5716 goto error;
5717 isl_seq_clr(bmap->eq[i], 1 + total);
5718 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5719 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5720 return isl_basic_map_finalize(bmap);
5721 error:
5722 isl_basic_map_free(bmap);
5723 return NULL;
5726 /* Add a constraint to "bmap" expressing i_pos < o_pos
5728 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5729 unsigned pos)
5731 int i;
5732 isl_size nparam;
5733 isl_size n_in;
5734 isl_size total;
5736 total = isl_basic_map_dim(bmap, isl_dim_all);
5737 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5738 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5739 if (total < 0 || nparam < 0 || n_in < 0)
5740 return isl_basic_map_free(bmap);
5741 i = isl_basic_map_alloc_inequality(bmap);
5742 if (i < 0)
5743 goto error;
5744 isl_seq_clr(bmap->ineq[i], 1 + total);
5745 isl_int_set_si(bmap->ineq[i][0], -1);
5746 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5747 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5748 return isl_basic_map_finalize(bmap);
5749 error:
5750 isl_basic_map_free(bmap);
5751 return NULL;
5754 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5756 static __isl_give isl_basic_map *var_less_or_equal(
5757 __isl_take isl_basic_map *bmap, unsigned pos)
5759 int i;
5760 isl_size nparam;
5761 isl_size n_in;
5762 isl_size total;
5764 total = isl_basic_map_dim(bmap, isl_dim_all);
5765 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5766 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5767 if (total < 0 || nparam < 0 || n_in < 0)
5768 return isl_basic_map_free(bmap);
5769 i = isl_basic_map_alloc_inequality(bmap);
5770 if (i < 0)
5771 goto error;
5772 isl_seq_clr(bmap->ineq[i], 1 + total);
5773 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5774 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5775 return isl_basic_map_finalize(bmap);
5776 error:
5777 isl_basic_map_free(bmap);
5778 return NULL;
5781 /* Add a constraint to "bmap" expressing i_pos > o_pos
5783 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5784 unsigned pos)
5786 int i;
5787 isl_size nparam;
5788 isl_size n_in;
5789 isl_size total;
5791 total = isl_basic_map_dim(bmap, isl_dim_all);
5792 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5793 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5794 if (total < 0 || nparam < 0 || n_in < 0)
5795 return isl_basic_map_free(bmap);
5796 i = isl_basic_map_alloc_inequality(bmap);
5797 if (i < 0)
5798 goto error;
5799 isl_seq_clr(bmap->ineq[i], 1 + total);
5800 isl_int_set_si(bmap->ineq[i][0], -1);
5801 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5802 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5803 return isl_basic_map_finalize(bmap);
5804 error:
5805 isl_basic_map_free(bmap);
5806 return NULL;
5809 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5811 static __isl_give isl_basic_map *var_more_or_equal(
5812 __isl_take isl_basic_map *bmap, unsigned pos)
5814 int i;
5815 isl_size nparam;
5816 isl_size n_in;
5817 isl_size total;
5819 total = isl_basic_map_dim(bmap, isl_dim_all);
5820 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5821 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5822 if (total < 0 || nparam < 0 || n_in < 0)
5823 return isl_basic_map_free(bmap);
5824 i = isl_basic_map_alloc_inequality(bmap);
5825 if (i < 0)
5826 goto error;
5827 isl_seq_clr(bmap->ineq[i], 1 + total);
5828 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5829 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5830 return isl_basic_map_finalize(bmap);
5831 error:
5832 isl_basic_map_free(bmap);
5833 return NULL;
5836 __isl_give isl_basic_map *isl_basic_map_equal(
5837 __isl_take isl_space *space, unsigned n_equal)
5839 int i;
5840 struct isl_basic_map *bmap;
5841 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5842 if (!bmap)
5843 return NULL;
5844 for (i = 0; i < n_equal && bmap; ++i)
5845 bmap = var_equal(bmap, i);
5846 return isl_basic_map_finalize(bmap);
5849 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5851 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5852 unsigned pos)
5854 int i;
5855 struct isl_basic_map *bmap;
5856 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5857 if (!bmap)
5858 return NULL;
5859 for (i = 0; i < pos && bmap; ++i)
5860 bmap = var_equal(bmap, i);
5861 if (bmap)
5862 bmap = var_less(bmap, pos);
5863 return isl_basic_map_finalize(bmap);
5866 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5868 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5869 __isl_take isl_space *space, unsigned pos)
5871 int i;
5872 isl_basic_map *bmap;
5874 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5875 for (i = 0; i < pos; ++i)
5876 bmap = var_equal(bmap, i);
5877 bmap = var_less_or_equal(bmap, pos);
5878 return isl_basic_map_finalize(bmap);
5881 /* Return a relation on "space" expressing i_pos > o_pos
5883 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5884 unsigned pos)
5886 int i;
5887 struct isl_basic_map *bmap;
5888 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5889 if (!bmap)
5890 return NULL;
5891 for (i = 0; i < pos && bmap; ++i)
5892 bmap = var_equal(bmap, i);
5893 if (bmap)
5894 bmap = var_more(bmap, pos);
5895 return isl_basic_map_finalize(bmap);
5898 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5900 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5901 __isl_take isl_space *space, unsigned pos)
5903 int i;
5904 isl_basic_map *bmap;
5906 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5907 for (i = 0; i < pos; ++i)
5908 bmap = var_equal(bmap, i);
5909 bmap = var_more_or_equal(bmap, pos);
5910 return isl_basic_map_finalize(bmap);
5913 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5914 unsigned n, int equal)
5916 struct isl_map *map;
5917 int i;
5919 if (n == 0 && equal)
5920 return isl_map_universe(space);
5922 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5924 for (i = 0; i + 1 < n; ++i)
5925 map = isl_map_add_basic_map(map,
5926 isl_basic_map_less_at(isl_space_copy(space), i));
5927 if (n > 0) {
5928 if (equal)
5929 map = isl_map_add_basic_map(map,
5930 isl_basic_map_less_or_equal_at(space, n - 1));
5931 else
5932 map = isl_map_add_basic_map(map,
5933 isl_basic_map_less_at(space, n - 1));
5934 } else
5935 isl_space_free(space);
5937 return map;
5940 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5942 if (!space)
5943 return NULL;
5944 return map_lex_lte_first(space, space->n_out, equal);
5947 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5948 unsigned n)
5950 return map_lex_lte_first(space, n, 0);
5953 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5954 unsigned n)
5956 return map_lex_lte_first(space, n, 1);
5959 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5961 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5964 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5966 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5969 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5970 unsigned n, int equal)
5972 struct isl_map *map;
5973 int i;
5975 if (n == 0 && equal)
5976 return isl_map_universe(space);
5978 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5980 for (i = 0; i + 1 < n; ++i)
5981 map = isl_map_add_basic_map(map,
5982 isl_basic_map_more_at(isl_space_copy(space), i));
5983 if (n > 0) {
5984 if (equal)
5985 map = isl_map_add_basic_map(map,
5986 isl_basic_map_more_or_equal_at(space, n - 1));
5987 else
5988 map = isl_map_add_basic_map(map,
5989 isl_basic_map_more_at(space, n - 1));
5990 } else
5991 isl_space_free(space);
5993 return map;
5996 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5998 if (!space)
5999 return NULL;
6000 return map_lex_gte_first(space, space->n_out, equal);
6003 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
6004 unsigned n)
6006 return map_lex_gte_first(space, n, 0);
6009 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
6010 unsigned n)
6012 return map_lex_gte_first(space, n, 1);
6015 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
6017 return map_lex_gte(isl_space_map_from_set(set_space), 0);
6020 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
6022 return map_lex_gte(isl_space_map_from_set(set_space), 1);
6025 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
6026 __isl_take isl_set *set2)
6028 isl_map *map;
6029 map = isl_map_lex_le(isl_set_get_space(set1));
6030 map = isl_map_intersect_domain(map, set1);
6031 map = isl_map_intersect_range(map, set2);
6032 return map;
6035 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
6036 __isl_take isl_set *set2)
6038 isl_map *map;
6039 map = isl_map_lex_lt(isl_set_get_space(set1));
6040 map = isl_map_intersect_domain(map, set1);
6041 map = isl_map_intersect_range(map, set2);
6042 return map;
6045 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
6046 __isl_take isl_set *set2)
6048 isl_map *map;
6049 map = isl_map_lex_ge(isl_set_get_space(set1));
6050 map = isl_map_intersect_domain(map, set1);
6051 map = isl_map_intersect_range(map, set2);
6052 return map;
6055 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
6056 __isl_take isl_set *set2)
6058 isl_map *map;
6059 map = isl_map_lex_gt(isl_set_get_space(set1));
6060 map = isl_map_intersect_domain(map, set1);
6061 map = isl_map_intersect_range(map, set2);
6062 return map;
6065 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
6066 __isl_take isl_map *map2)
6068 isl_map *map;
6069 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
6070 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6071 map = isl_map_apply_range(map, isl_map_reverse(map2));
6072 return map;
6075 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
6076 __isl_take isl_map *map2)
6078 isl_map *map;
6079 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
6080 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6081 map = isl_map_apply_range(map, isl_map_reverse(map2));
6082 return map;
6085 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
6086 __isl_take isl_map *map2)
6088 isl_map *map;
6089 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
6090 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6091 map = isl_map_apply_range(map, isl_map_reverse(map2));
6092 return map;
6095 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
6096 __isl_take isl_map *map2)
6098 isl_map *map;
6099 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
6100 map = isl_map_apply_domain(map, isl_map_reverse(map1));
6101 map = isl_map_apply_range(map, isl_map_reverse(map2));
6102 return map;
6105 /* For the div d = floor(f/m) at position "div", add the constraint
6107 * f - m d >= 0
6109 static __isl_give isl_basic_map *add_upper_div_constraint(
6110 __isl_take isl_basic_map *bmap, unsigned div)
6112 int i;
6113 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
6114 isl_size n_div;
6115 unsigned pos;
6117 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6118 if (v_div < 0 || n_div < 0)
6119 return isl_basic_map_free(bmap);
6120 pos = v_div + div;
6121 i = isl_basic_map_alloc_inequality(bmap);
6122 if (i < 0)
6123 return isl_basic_map_free(bmap);
6124 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
6125 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
6127 return bmap;
6130 /* For the div d = floor(f/m) at position "div", add the constraint
6132 * -(f-(m-1)) + m d >= 0
6134 static __isl_give isl_basic_map *add_lower_div_constraint(
6135 __isl_take isl_basic_map *bmap, unsigned div)
6137 int i;
6138 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
6139 isl_size n_div;
6140 unsigned pos;
6142 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6143 if (v_div < 0 || n_div < 0)
6144 return isl_basic_map_free(bmap);
6145 pos = v_div + div;
6146 i = isl_basic_map_alloc_inequality(bmap);
6147 if (i < 0)
6148 return isl_basic_map_free(bmap);
6149 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
6150 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
6151 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
6152 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
6154 return bmap;
6157 /* For the div d = floor(f/m) at position "pos", add the constraints
6159 * f - m d >= 0
6160 * -(f-(m-1)) + m d >= 0
6162 * Note that the second constraint is the negation of
6164 * f - m d >= m
6166 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
6167 __isl_take isl_basic_map *bmap, unsigned pos)
6169 bmap = add_upper_div_constraint(bmap, pos);
6170 bmap = add_lower_div_constraint(bmap, pos);
6171 return bmap;
6174 /* For the div d = floor(f/m) at position "pos", add the constraints
6176 * f - m d >= 0
6177 * -(f-(m-1)) + m d >= 0
6179 * Note that the second constraint is the negation of
6181 * f - m d >= m
6183 __isl_give isl_basic_set *isl_basic_set_add_div_constraints(
6184 __isl_take isl_basic_set *bset, unsigned pos)
6186 isl_basic_map *bmap = bset_to_bmap(bset);
6187 bmap = isl_basic_map_add_div_constraints(bmap, pos);
6188 return bset_from_bmap(bmap);
6191 /* For each known div d = floor(f/m), add the constraints
6193 * f - m d >= 0
6194 * -(f-(m-1)) + m d >= 0
6196 * Remove duplicate constraints in case of some these div constraints
6197 * already appear in "bmap".
6199 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
6200 __isl_take isl_basic_map *bmap)
6202 isl_size n_div;
6204 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6205 if (n_div < 0)
6206 return isl_basic_map_free(bmap);
6207 if (n_div == 0)
6208 return bmap;
6210 bmap = add_known_div_constraints(bmap);
6211 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
6212 bmap = isl_basic_map_finalize(bmap);
6213 return bmap;
6216 /* Add the div constraint of sign "sign" for div "div" of "bmap".
6218 * In particular, if this div is of the form d = floor(f/m),
6219 * then add the constraint
6221 * f - m d >= 0
6223 * if sign < 0 or the constraint
6225 * -(f-(m-1)) + m d >= 0
6227 * if sign > 0.
6229 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
6230 __isl_take isl_basic_map *bmap, unsigned div, int sign)
6232 if (sign < 0)
6233 return add_upper_div_constraint(bmap, div);
6234 else
6235 return add_lower_div_constraint(bmap, div);
6238 __isl_give isl_basic_set *isl_basic_map_underlying_set(
6239 __isl_take isl_basic_map *bmap)
6241 isl_space *space;
6243 if (!bmap)
6244 goto error;
6245 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
6246 bmap->n_div == 0 &&
6247 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
6248 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
6249 return bset_from_bmap(bmap);
6250 bmap = isl_basic_map_cow(bmap);
6251 if (!bmap)
6252 return NULL;
6253 space = isl_basic_map_take_space(bmap);
6254 space = isl_space_underlying(space, bmap->n_div);
6255 bmap = isl_basic_map_restore_space(bmap, space);
6256 if (!bmap)
6257 return NULL;
6258 bmap->extra -= bmap->n_div;
6259 bmap->n_div = 0;
6260 bmap = isl_basic_map_finalize(bmap);
6261 return bset_from_bmap(bmap);
6262 error:
6263 isl_basic_map_free(bmap);
6264 return NULL;
6267 __isl_give isl_basic_set *isl_basic_set_underlying_set(
6268 __isl_take isl_basic_set *bset)
6270 return isl_basic_map_underlying_set(bset_to_bmap(bset));
6273 /* Replace each element in "list" by the result of applying
6274 * isl_basic_map_underlying_set to the element.
6276 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
6277 __isl_take isl_basic_map_list *list)
6279 int i;
6280 isl_size n;
6282 n = isl_basic_map_list_n_basic_map(list);
6283 if (n < 0)
6284 goto error;
6286 for (i = 0; i < n; ++i) {
6287 isl_basic_map *bmap;
6288 isl_basic_set *bset;
6290 bmap = isl_basic_map_list_get_basic_map(list, i);
6291 bset = isl_basic_set_underlying_set(bmap);
6292 list = isl_basic_set_list_set_basic_set(list, i, bset);
6295 return list;
6296 error:
6297 isl_basic_map_list_free(list);
6298 return NULL;
6301 __isl_give isl_basic_map *isl_basic_map_overlying_set(
6302 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
6304 struct isl_basic_map *bmap;
6305 struct isl_ctx *ctx;
6306 isl_size dim, bmap_total;
6307 unsigned total;
6308 int i;
6310 if (!bset || !like)
6311 goto error;
6312 ctx = bset->ctx;
6313 if (isl_basic_set_check_no_params(bset) < 0 ||
6314 isl_basic_set_check_no_locals(bset) < 0)
6315 goto error;
6316 dim = isl_basic_set_dim(bset, isl_dim_set);
6317 bmap_total = isl_basic_map_dim(like, isl_dim_all);
6318 if (dim < 0 || bmap_total < 0)
6319 goto error;
6320 isl_assert(ctx, dim == bmap_total, goto error);
6321 if (like->n_div == 0) {
6322 isl_space *space = isl_basic_map_get_space(like);
6323 isl_basic_map_free(like);
6324 return isl_basic_map_reset_space(bset, space);
6326 bset = isl_basic_set_cow(bset);
6327 if (!bset)
6328 goto error;
6329 total = dim + bset->extra;
6330 bmap = bset_to_bmap(bset);
6331 isl_space_free(isl_basic_map_take_space(bmap));
6332 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
6333 if (!bmap)
6334 goto error;
6335 bmap->n_div = like->n_div;
6336 bmap->extra += like->n_div;
6337 if (bmap->extra) {
6338 unsigned ltotal;
6339 isl_int **div;
6340 ltotal = total - bmap->extra + like->extra;
6341 if (ltotal > total)
6342 ltotal = total;
6343 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
6344 bmap->extra * (1 + 1 + total));
6345 if (isl_blk_is_error(bmap->block2))
6346 goto error;
6347 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
6348 if (!div)
6349 goto error;
6350 bmap->div = div;
6351 for (i = 0; i < bmap->extra; ++i)
6352 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
6353 for (i = 0; i < like->n_div; ++i) {
6354 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
6355 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
6357 bmap = isl_basic_map_add_known_div_constraints(bmap);
6359 isl_basic_map_free(like);
6360 bmap = isl_basic_map_simplify(bmap);
6361 bmap = isl_basic_map_finalize(bmap);
6362 return bmap;
6363 error:
6364 isl_basic_map_free(like);
6365 isl_basic_set_free(bset);
6366 return NULL;
6369 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
6370 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
6372 return bset_from_bmap(isl_basic_map_overlying_set(bset,
6373 bset_to_bmap(like)));
6376 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
6378 int i;
6380 map = isl_map_cow(map);
6381 if (!map)
6382 return NULL;
6383 map->dim = isl_space_cow(map->dim);
6384 if (!map->dim)
6385 goto error;
6387 for (i = 1; i < map->n; ++i)
6388 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
6389 goto error);
6390 for (i = 0; i < map->n; ++i) {
6391 map->p[i] = bset_to_bmap(
6392 isl_basic_map_underlying_set(map->p[i]));
6393 if (!map->p[i])
6394 goto error;
6396 if (map->n == 0)
6397 map->dim = isl_space_underlying(map->dim, 0);
6398 else {
6399 isl_space_free(map->dim);
6400 map->dim = isl_space_copy(map->p[0]->dim);
6402 if (!map->dim)
6403 goto error;
6404 return set_from_map(map);
6405 error:
6406 isl_map_free(map);
6407 return NULL;
6410 /* Replace the space of "bmap" by "space".
6412 * If the space of "bmap" is identical to "space" (including the identifiers
6413 * of the input and output dimensions), then simply return the original input.
6415 __isl_give isl_basic_map *isl_basic_map_reset_space(
6416 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
6418 isl_bool equal;
6419 isl_space *bmap_space;
6421 bmap_space = isl_basic_map_peek_space(bmap);
6422 equal = isl_space_is_equal(bmap_space, space);
6423 if (equal >= 0 && equal)
6424 equal = isl_space_has_equal_ids(bmap_space, space);
6425 if (equal < 0)
6426 goto error;
6427 if (equal) {
6428 isl_space_free(space);
6429 return bmap;
6431 isl_space_free(isl_basic_map_take_space(bmap));
6432 bmap = isl_basic_map_restore_space(bmap, space);
6434 bmap = isl_basic_map_finalize(bmap);
6436 return bmap;
6437 error:
6438 isl_basic_map_free(bmap);
6439 isl_space_free(space);
6440 return NULL;
6443 __isl_give isl_basic_set *isl_basic_set_reset_space(
6444 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
6446 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
6447 space));
6450 /* Check that the total dimensions of "map" and "space" are the same.
6452 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
6453 __isl_keep isl_space *space)
6455 isl_size dim1, dim2;
6457 dim1 = isl_map_dim(map, isl_dim_all);
6458 dim2 = isl_space_dim(space, isl_dim_all);
6459 if (dim1 < 0 || dim2 < 0)
6460 return isl_stat_error;
6461 if (dim1 == dim2)
6462 return isl_stat_ok;
6463 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6464 "total dimensions do not match", return isl_stat_error);
6467 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
6468 __isl_take isl_space *space)
6470 int i;
6472 map = isl_map_cow(map);
6473 if (!map || !space)
6474 goto error;
6476 for (i = 0; i < map->n; ++i) {
6477 map->p[i] = isl_basic_map_reset_space(map->p[i],
6478 isl_space_copy(space));
6479 if (!map->p[i])
6480 goto error;
6482 isl_space_free(isl_map_take_space(map));
6483 map = isl_map_restore_space(map, space);
6485 return map;
6486 error:
6487 isl_map_free(map);
6488 isl_space_free(space);
6489 return NULL;
6492 /* Replace the space of "map" by "space", without modifying
6493 * the dimension of "map".
6495 * If the space of "map" is identical to "space" (including the identifiers
6496 * of the input and output dimensions), then simply return the original input.
6498 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
6499 __isl_take isl_space *space)
6501 isl_bool equal;
6502 isl_space *map_space;
6504 map_space = isl_map_peek_space(map);
6505 equal = isl_space_is_equal(map_space, space);
6506 if (equal >= 0 && equal)
6507 equal = isl_space_has_equal_ids(map_space, space);
6508 if (equal < 0)
6509 goto error;
6510 if (equal) {
6511 isl_space_free(space);
6512 return map;
6514 if (check_map_space_equal_total_dim(map, space) < 0)
6515 goto error;
6516 return isl_map_reset_space(map, space);
6517 error:
6518 isl_map_free(map);
6519 isl_space_free(space);
6520 return NULL;
6523 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
6524 __isl_take isl_space *space)
6526 return set_from_map(isl_map_reset_space(set_to_map(set), space));
6529 /* Compute the parameter domain of the given basic set.
6531 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
6533 isl_bool is_params;
6534 isl_space *space;
6535 isl_size n;
6537 is_params = isl_basic_set_is_params(bset);
6538 if (is_params < 0)
6539 return isl_basic_set_free(bset);
6540 if (is_params)
6541 return bset;
6543 n = isl_basic_set_dim(bset, isl_dim_set);
6544 if (n < 0)
6545 return isl_basic_set_free(bset);
6546 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6547 space = isl_basic_set_get_space(bset);
6548 space = isl_space_params(space);
6549 bset = isl_basic_set_reset_space(bset, space);
6550 return bset;
6553 /* Construct a zero-dimensional basic set with the given parameter domain.
6555 __isl_give isl_basic_set *isl_basic_set_from_params(
6556 __isl_take isl_basic_set *bset)
6558 isl_space *space;
6559 space = isl_basic_set_get_space(bset);
6560 space = isl_space_set_from_params(space);
6561 bset = isl_basic_set_reset_space(bset, space);
6562 return bset;
6565 /* Compute the parameter domain of the given set.
6567 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
6569 return isl_map_params(set_to_map(set));
6572 /* Construct a zero-dimensional set with the given parameter domain.
6574 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
6576 isl_space *space;
6577 space = isl_set_get_space(set);
6578 space = isl_space_set_from_params(space);
6579 set = isl_set_reset_space(set, space);
6580 return set;
6583 /* Compute the parameter domain of the given map.
6585 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
6587 isl_space *space;
6588 isl_size n_in, n_out;
6590 n_in = isl_map_dim(map, isl_dim_in);
6591 n_out = isl_map_dim(map, isl_dim_out);
6592 if (n_in < 0 || n_out < 0)
6593 return isl_map_free(map);
6594 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6595 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
6596 space = isl_map_get_space(map);
6597 space = isl_space_params(space);
6598 map = isl_map_reset_space(map, space);
6599 return map;
6602 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
6604 isl_space *space;
6605 isl_size n_out;
6607 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6608 if (n_out < 0)
6609 return isl_basic_map_free(bmap);
6610 space = isl_space_domain(isl_basic_map_get_space(bmap));
6612 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
6614 return isl_basic_map_reset_space(bmap, space);
6617 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
6619 if (!bmap)
6620 return isl_bool_error;
6621 return isl_space_may_be_set(bmap->dim);
6624 /* Is this basic map actually a set?
6625 * Users should never call this function. Outside of isl,
6626 * the type should indicate whether something is a set or a map.
6628 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
6630 if (!bmap)
6631 return isl_bool_error;
6632 return isl_space_is_set(bmap->dim);
6635 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
6637 isl_bool is_set;
6639 is_set = isl_basic_map_is_set(bmap);
6640 if (is_set < 0)
6641 goto error;
6642 if (is_set)
6643 return bmap;
6644 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6645 error:
6646 isl_basic_map_free(bmap);
6647 return NULL;
6650 __isl_give isl_basic_map *isl_basic_map_domain_map(
6651 __isl_take isl_basic_map *bmap)
6653 int i;
6654 isl_space *space;
6655 isl_basic_map *domain;
6656 isl_size nparam, n_in, n_out;
6658 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6659 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6660 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6661 if (nparam < 0 || n_in < 0 || n_out < 0)
6662 return isl_basic_map_free(bmap);
6664 space = isl_basic_map_get_space(bmap);
6665 space = isl_space_from_range(isl_space_domain(space));
6666 domain = isl_basic_map_universe(space);
6668 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6669 bmap = isl_basic_map_apply_range(bmap, domain);
6670 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6672 for (i = 0; i < n_in; ++i)
6673 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6674 isl_dim_out, i);
6676 bmap = isl_basic_map_gauss(bmap, NULL);
6677 return isl_basic_map_finalize(bmap);
6680 __isl_give isl_basic_map *isl_basic_map_range_map(
6681 __isl_take isl_basic_map *bmap)
6683 int i;
6684 isl_space *space;
6685 isl_basic_map *range;
6686 isl_size nparam, n_in, n_out;
6688 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6689 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6690 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6691 if (nparam < 0 || n_in < 0 || n_out < 0)
6692 return isl_basic_map_free(bmap);
6694 space = isl_basic_map_get_space(bmap);
6695 space = isl_space_from_range(isl_space_range(space));
6696 range = isl_basic_map_universe(space);
6698 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6699 bmap = isl_basic_map_apply_range(bmap, range);
6700 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6702 for (i = 0; i < n_out; ++i)
6703 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6704 isl_dim_out, i);
6706 bmap = isl_basic_map_gauss(bmap, NULL);
6707 return isl_basic_map_finalize(bmap);
6710 int isl_map_may_be_set(__isl_keep isl_map *map)
6712 if (!map)
6713 return -1;
6714 return isl_space_may_be_set(map->dim);
6717 /* Is this map actually a set?
6718 * Users should never call this function. Outside of isl,
6719 * the type should indicate whether something is a set or a map.
6721 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6723 if (!map)
6724 return isl_bool_error;
6725 return isl_space_is_set(map->dim);
6728 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6730 isl_space *space;
6731 isl_size n_in;
6733 n_in = isl_map_dim(map, isl_dim_in);
6734 if (n_in < 0)
6735 return set_from_map(isl_map_free(map));
6736 space = isl_space_range(isl_map_get_space(map));
6738 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6740 return set_from_map(isl_map_reset_space(map, space));
6743 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6744 * to each of its basic maps.
6746 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6747 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6748 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6750 int i;
6751 isl_space *space;
6753 map = isl_map_cow(map);
6754 if (!map)
6755 return NULL;
6757 for (i = 0; i < map->n; ++i) {
6758 map->p[i] = fn_bmap(map->p[i]);
6759 if (!map->p[i])
6760 return isl_map_free(map);
6762 map = isl_map_unmark_normalized(map);
6764 space = isl_map_take_space(map);
6765 space = fn_space(space);
6766 map = isl_map_restore_space(map, space);
6768 return map;
6771 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6773 return isl_map_transform(map, &isl_space_domain_map,
6774 &isl_basic_map_domain_map);
6777 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6779 return isl_map_transform(map, &isl_space_range_map,
6780 &isl_basic_map_range_map);
6783 /* Given a wrapped map of the form A[B -> C],
6784 * return the map A[B -> C] -> B.
6786 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6788 isl_id *id;
6789 isl_map *map;
6791 if (!set)
6792 return NULL;
6793 if (!isl_set_has_tuple_id(set))
6794 return isl_map_domain_map(isl_set_unwrap(set));
6796 id = isl_set_get_tuple_id(set);
6797 map = isl_map_domain_map(isl_set_unwrap(set));
6798 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6800 return map;
6803 __isl_give isl_basic_map *isl_basic_map_from_domain(
6804 __isl_take isl_basic_set *bset)
6806 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6809 __isl_give isl_basic_map *isl_basic_map_from_range(
6810 __isl_take isl_basic_set *bset)
6812 isl_space *space;
6813 space = isl_basic_set_get_space(bset);
6814 space = isl_space_from_range(space);
6815 bset = isl_basic_set_reset_space(bset, space);
6816 return bset_to_bmap(bset);
6819 /* Create a relation with the given set as range.
6820 * The domain of the created relation is a zero-dimensional
6821 * flat anonymous space.
6823 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6825 isl_space *space;
6826 space = isl_set_get_space(set);
6827 space = isl_space_from_range(space);
6828 set = isl_set_reset_space(set, space);
6829 return set_to_map(set);
6832 /* Create a relation with the given set as domain.
6833 * The range of the created relation is a zero-dimensional
6834 * flat anonymous space.
6836 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6838 return isl_map_reverse(isl_map_from_range(set));
6841 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6842 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6844 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6847 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6848 __isl_take isl_set *range)
6850 return isl_map_apply_range(isl_map_reverse(domain), range);
6853 /* Return a newly allocated isl_map with given space and flags and
6854 * room for "n" basic maps.
6855 * Make sure that all cached information is cleared.
6857 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6858 unsigned flags)
6860 struct isl_map *map;
6862 if (!space)
6863 return NULL;
6864 if (n < 0)
6865 isl_die(space->ctx, isl_error_internal,
6866 "negative number of basic maps", goto error);
6867 map = isl_calloc(space->ctx, struct isl_map,
6868 sizeof(struct isl_map) +
6869 (n - 1) * sizeof(struct isl_basic_map *));
6870 if (!map)
6871 goto error;
6873 map->ctx = space->ctx;
6874 isl_ctx_ref(map->ctx);
6875 map->ref = 1;
6876 map->size = n;
6877 map->n = 0;
6878 map->dim = space;
6879 map->flags = flags;
6880 return map;
6881 error:
6882 isl_space_free(space);
6883 return NULL;
6886 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6888 struct isl_basic_map *bmap;
6889 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6890 bmap = isl_basic_map_set_to_empty(bmap);
6891 return bmap;
6894 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6896 struct isl_basic_set *bset;
6897 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6898 bset = isl_basic_set_set_to_empty(bset);
6899 return bset;
6902 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6904 struct isl_basic_map *bmap;
6905 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6906 bmap = isl_basic_map_finalize(bmap);
6907 return bmap;
6910 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6912 struct isl_basic_set *bset;
6913 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6914 bset = isl_basic_set_finalize(bset);
6915 return bset;
6918 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6919 __isl_take isl_space *space)
6921 int i;
6922 isl_size total = isl_space_dim(space, isl_dim_all);
6923 isl_basic_map *bmap;
6925 if (total < 0)
6926 space = isl_space_free(space);
6927 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6928 for (i = 0; i < total; ++i) {
6929 int k = isl_basic_map_alloc_inequality(bmap);
6930 if (k < 0)
6931 goto error;
6932 isl_seq_clr(bmap->ineq[k], 1 + total);
6933 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6935 return bmap;
6936 error:
6937 isl_basic_map_free(bmap);
6938 return NULL;
6941 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6942 __isl_take isl_space *space)
6944 return isl_basic_map_nat_universe(space);
6947 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6949 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6952 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6954 return isl_map_nat_universe(space);
6957 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6959 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6962 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6964 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6967 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6969 struct isl_map *map;
6970 if (!space)
6971 return NULL;
6972 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6973 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6974 return map;
6977 /* This function performs the same operation as isl_map_universe,
6978 * but is considered as a function on an isl_space when exported.
6980 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6982 return isl_map_universe(space);
6985 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6987 struct isl_set *set;
6988 if (!space)
6989 return NULL;
6990 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6991 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6992 return set;
6995 /* This function performs the same operation as isl_set_universe,
6996 * but is considered as a function on an isl_space when exported.
6998 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
7000 return isl_set_universe(space);
7003 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
7005 int i;
7006 struct isl_map *dup;
7008 if (!map)
7009 return NULL;
7010 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
7011 for (i = 0; i < map->n; ++i)
7012 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
7013 return dup;
7016 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
7017 __isl_take isl_basic_map *bmap)
7019 if (!bmap || !map)
7020 goto error;
7021 if (isl_basic_map_plain_is_empty(bmap)) {
7022 isl_basic_map_free(bmap);
7023 return map;
7025 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
7026 goto error;
7027 isl_assert(map->ctx, map->n < map->size, goto error);
7028 map->p[map->n] = bmap;
7029 map->n++;
7030 map = isl_map_unmark_normalized(map);
7031 return map;
7032 error:
7033 if (map)
7034 isl_map_free(map);
7035 if (bmap)
7036 isl_basic_map_free(bmap);
7037 return NULL;
7040 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
7042 int i;
7044 if (!map)
7045 return NULL;
7047 if (--map->ref > 0)
7048 return NULL;
7050 clear_caches(map);
7051 isl_ctx_deref(map->ctx);
7052 for (i = 0; i < map->n; ++i)
7053 isl_basic_map_free(map->p[i]);
7054 isl_space_free(map->dim);
7055 free(map);
7057 return NULL;
7060 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
7061 __isl_take isl_basic_map *bmap, unsigned pos, int value)
7063 int j;
7064 isl_size total;
7066 total = isl_basic_map_dim(bmap, isl_dim_all);
7067 if (total < 0)
7068 return isl_basic_map_free(bmap);
7070 bmap = isl_basic_map_cow(bmap);
7071 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
7072 j = isl_basic_map_alloc_equality(bmap);
7073 if (j < 0)
7074 goto error;
7075 isl_seq_clr(bmap->eq[j] + 1, total);
7076 isl_int_set_si(bmap->eq[j][pos], -1);
7077 isl_int_set_si(bmap->eq[j][0], value);
7078 bmap = isl_basic_map_simplify(bmap);
7079 return isl_basic_map_finalize(bmap);
7080 error:
7081 isl_basic_map_free(bmap);
7082 return NULL;
7085 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
7086 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
7088 int j;
7089 isl_size total;
7091 total = isl_basic_map_dim(bmap, isl_dim_all);
7092 if (total < 0)
7093 return isl_basic_map_free(bmap);
7095 bmap = isl_basic_map_cow(bmap);
7096 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
7097 j = isl_basic_map_alloc_equality(bmap);
7098 if (j < 0)
7099 goto error;
7100 isl_seq_clr(bmap->eq[j] + 1, total);
7101 isl_int_set_si(bmap->eq[j][pos], -1);
7102 isl_int_set(bmap->eq[j][0], value);
7103 bmap = isl_basic_map_simplify(bmap);
7104 return isl_basic_map_finalize(bmap);
7105 error:
7106 isl_basic_map_free(bmap);
7107 return NULL;
7110 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
7111 enum isl_dim_type type, unsigned pos, int value)
7113 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7114 return isl_basic_map_free(bmap);
7115 return isl_basic_map_fix_pos_si(bmap,
7116 isl_basic_map_offset(bmap, type) + pos, value);
7119 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
7120 enum isl_dim_type type, unsigned pos, isl_int value)
7122 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7123 return isl_basic_map_free(bmap);
7124 return isl_basic_map_fix_pos(bmap,
7125 isl_basic_map_offset(bmap, type) + pos, value);
7128 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
7129 * to be equal to "v".
7131 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
7132 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7134 if (!bmap || !v)
7135 goto error;
7136 if (!isl_val_is_int(v))
7137 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
7138 "expecting integer value", goto error);
7139 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7140 goto error;
7141 pos += isl_basic_map_offset(bmap, type);
7142 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
7143 isl_val_free(v);
7144 return bmap;
7145 error:
7146 isl_basic_map_free(bmap);
7147 isl_val_free(v);
7148 return NULL;
7151 /* Fix the value of the variable at position "pos" of type "type" of "bset"
7152 * to be equal to "v".
7154 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
7155 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7157 return isl_basic_map_fix_val(bset, type, pos, v);
7160 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
7161 enum isl_dim_type type, unsigned pos, int value)
7163 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
7164 type, pos, value));
7167 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
7168 enum isl_dim_type type, unsigned pos, isl_int value)
7170 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
7171 type, pos, value));
7174 /* Remove the basic map at position "i" from "map" if this basic map
7175 * is (obviously) empty.
7177 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
7179 isl_bool empty;
7181 if (!map)
7182 return NULL;
7184 empty = isl_basic_map_plain_is_empty(map->p[i]);
7185 if (empty < 0)
7186 return isl_map_free(map);
7187 if (!empty)
7188 return map;
7190 isl_basic_map_free(map->p[i]);
7191 map->n--;
7192 if (i != map->n) {
7193 map->p[i] = map->p[map->n];
7194 map = isl_map_unmark_normalized(map);
7198 return map;
7201 /* Perform "fn" on each basic map of "map", where we may not be holding
7202 * the only reference to "map".
7203 * In particular, "fn" should be a semantics preserving operation
7204 * that we want to apply to all copies of "map". We therefore need
7205 * to be careful not to modify "map" in a way that breaks "map"
7206 * in case anything goes wrong.
7208 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
7209 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
7211 struct isl_basic_map *bmap;
7212 int i;
7214 if (!map)
7215 return NULL;
7217 for (i = map->n - 1; i >= 0; --i) {
7218 bmap = isl_basic_map_copy(map->p[i]);
7219 bmap = fn(bmap);
7220 if (!bmap)
7221 goto error;
7222 isl_basic_map_free(map->p[i]);
7223 map->p[i] = bmap;
7224 map = remove_if_empty(map, i);
7225 if (!map)
7226 return NULL;
7229 return map;
7230 error:
7231 isl_map_free(map);
7232 return NULL;
7235 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
7236 enum isl_dim_type type, unsigned pos, int value)
7238 int i;
7240 map = isl_map_cow(map);
7241 if (isl_map_check_range(map, type, pos, 1) < 0)
7242 return isl_map_free(map);
7243 for (i = map->n - 1; i >= 0; --i) {
7244 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
7245 map = remove_if_empty(map, i);
7246 if (!map)
7247 return NULL;
7249 map = isl_map_unmark_normalized(map);
7250 return map;
7253 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
7254 enum isl_dim_type type, unsigned pos, int value)
7256 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
7259 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
7260 enum isl_dim_type type, unsigned pos, isl_int value)
7262 int i;
7264 map = isl_map_cow(map);
7265 if (isl_map_check_range(map, type, pos, 1) < 0)
7266 return isl_map_free(map);
7267 for (i = 0; i < map->n; ++i) {
7268 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
7269 if (!map->p[i])
7270 goto error;
7272 map = isl_map_unmark_normalized(map);
7273 return map;
7274 error:
7275 isl_map_free(map);
7276 return NULL;
7279 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
7280 enum isl_dim_type type, unsigned pos, isl_int value)
7282 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
7285 /* Fix the value of the variable at position "pos" of type "type" of "map"
7286 * to be equal to "v".
7288 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
7289 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7291 int i;
7293 map = isl_map_cow(map);
7294 if (!map || !v)
7295 goto error;
7297 if (!isl_val_is_int(v))
7298 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7299 "expecting integer value", goto error);
7300 if (isl_map_check_range(map, type, pos, 1) < 0)
7301 goto error;
7302 for (i = map->n - 1; i >= 0; --i) {
7303 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
7304 isl_val_copy(v));
7305 map = remove_if_empty(map, i);
7306 if (!map)
7307 goto error;
7309 map = isl_map_unmark_normalized(map);
7310 isl_val_free(v);
7311 return map;
7312 error:
7313 isl_map_free(map);
7314 isl_val_free(v);
7315 return NULL;
7318 /* Fix the value of the variable at position "pos" of type "type" of "set"
7319 * to be equal to "v".
7321 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
7322 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
7324 return isl_map_fix_val(set, type, pos, v);
7327 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
7328 unsigned input, int value)
7330 return isl_map_fix_si(map, isl_dim_in, input, value);
7333 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
7334 int value)
7336 return set_from_map(isl_map_fix_si(set_to_map(set),
7337 isl_dim_set, dim, value));
7340 static __isl_give isl_basic_map *basic_map_bound_si(
7341 __isl_take isl_basic_map *bmap,
7342 enum isl_dim_type type, unsigned pos, int value, int upper)
7344 int j;
7345 isl_size total;
7347 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7348 return isl_basic_map_free(bmap);
7349 total = isl_basic_map_dim(bmap, isl_dim_all);
7350 if (total < 0)
7351 return isl_basic_map_free(bmap);
7352 pos += isl_basic_map_offset(bmap, type);
7353 bmap = isl_basic_map_cow(bmap);
7354 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
7355 j = isl_basic_map_alloc_inequality(bmap);
7356 if (j < 0)
7357 goto error;
7358 isl_seq_clr(bmap->ineq[j], 1 + total);
7359 if (upper) {
7360 isl_int_set_si(bmap->ineq[j][pos], -1);
7361 isl_int_set_si(bmap->ineq[j][0], value);
7362 } else {
7363 isl_int_set_si(bmap->ineq[j][pos], 1);
7364 isl_int_set_si(bmap->ineq[j][0], -value);
7366 bmap = isl_basic_map_simplify(bmap);
7367 return isl_basic_map_finalize(bmap);
7368 error:
7369 isl_basic_map_free(bmap);
7370 return NULL;
7373 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
7374 __isl_take isl_basic_map *bmap,
7375 enum isl_dim_type type, unsigned pos, int value)
7377 return basic_map_bound_si(bmap, type, pos, value, 0);
7380 /* Constrain the values of the given dimension to be no greater than "value".
7382 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
7383 __isl_take isl_basic_map *bmap,
7384 enum isl_dim_type type, unsigned pos, int value)
7386 return basic_map_bound_si(bmap, type, pos, value, 1);
7389 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
7390 enum isl_dim_type type, unsigned pos, int value, int upper)
7392 int i;
7394 map = isl_map_cow(map);
7395 if (isl_map_check_range(map, type, pos, 1) < 0)
7396 return isl_map_free(map);
7397 for (i = 0; i < map->n; ++i) {
7398 map->p[i] = basic_map_bound_si(map->p[i],
7399 type, pos, value, upper);
7400 if (!map->p[i])
7401 goto error;
7403 map = isl_map_unmark_normalized(map);
7404 return map;
7405 error:
7406 isl_map_free(map);
7407 return NULL;
7410 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
7411 enum isl_dim_type type, unsigned pos, int value)
7413 return map_bound_si(map, type, pos, value, 0);
7416 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
7417 enum isl_dim_type type, unsigned pos, int value)
7419 return map_bound_si(map, type, pos, value, 1);
7422 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
7423 enum isl_dim_type type, unsigned pos, int value)
7425 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
7426 type, pos, value));
7429 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
7430 enum isl_dim_type type, unsigned pos, int value)
7432 return isl_map_upper_bound_si(set, type, pos, value);
7435 /* Bound the given variable of "bmap" from below (or above is "upper"
7436 * is set) to "value".
7438 static __isl_give isl_basic_map *basic_map_bound(
7439 __isl_take isl_basic_map *bmap,
7440 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7442 int j;
7443 isl_size total;
7445 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
7446 return isl_basic_map_free(bmap);
7447 total = isl_basic_map_dim(bmap, isl_dim_all);
7448 if (total < 0)
7449 return isl_basic_map_free(bmap);
7450 pos += isl_basic_map_offset(bmap, type);
7451 bmap = isl_basic_map_cow(bmap);
7452 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
7453 j = isl_basic_map_alloc_inequality(bmap);
7454 if (j < 0)
7455 goto error;
7456 isl_seq_clr(bmap->ineq[j], 1 + total);
7457 if (upper) {
7458 isl_int_set_si(bmap->ineq[j][pos], -1);
7459 isl_int_set(bmap->ineq[j][0], value);
7460 } else {
7461 isl_int_set_si(bmap->ineq[j][pos], 1);
7462 isl_int_neg(bmap->ineq[j][0], value);
7464 bmap = isl_basic_map_simplify(bmap);
7465 return isl_basic_map_finalize(bmap);
7466 error:
7467 isl_basic_map_free(bmap);
7468 return NULL;
7471 /* Bound the given variable of "map" from below (or above is "upper"
7472 * is set) to "value".
7474 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
7475 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
7477 int i;
7479 map = isl_map_cow(map);
7480 if (isl_map_check_range(map, type, pos, 1) < 0)
7481 return isl_map_free(map);
7482 for (i = map->n - 1; i >= 0; --i) {
7483 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
7484 map = remove_if_empty(map, i);
7485 if (!map)
7486 return NULL;
7488 map = isl_map_unmark_normalized(map);
7489 return map;
7492 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
7493 enum isl_dim_type type, unsigned pos, isl_int value)
7495 return map_bound(map, type, pos, value, 0);
7498 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
7499 enum isl_dim_type type, unsigned pos, isl_int value)
7501 return map_bound(map, type, pos, value, 1);
7504 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
7505 enum isl_dim_type type, unsigned pos, isl_int value)
7507 return isl_map_lower_bound(set, type, pos, value);
7510 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
7511 enum isl_dim_type type, unsigned pos, isl_int value)
7513 return isl_map_upper_bound(set, type, pos, value);
7516 /* Force the values of the variable at position "pos" of type "type" of "map"
7517 * to be no smaller than "value".
7519 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
7520 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7522 if (!value)
7523 goto error;
7524 if (!isl_val_is_int(value))
7525 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7526 "expecting integer value", goto error);
7527 map = isl_map_lower_bound(map, type, pos, value->n);
7528 isl_val_free(value);
7529 return map;
7530 error:
7531 isl_val_free(value);
7532 isl_map_free(map);
7533 return NULL;
7536 /* Force the values of the variable at position "pos" of type "type" of "set"
7537 * to be no smaller than "value".
7539 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
7540 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7542 isl_map *map;
7544 map = set_to_map(set);
7545 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
7548 /* Force the values of the variable at position "pos" of type "type" of "map"
7549 * to be no greater than "value".
7551 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
7552 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7554 if (!value)
7555 goto error;
7556 if (!isl_val_is_int(value))
7557 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7558 "expecting integer value", goto error);
7559 map = isl_map_upper_bound(map, type, pos, value->n);
7560 isl_val_free(value);
7561 return map;
7562 error:
7563 isl_val_free(value);
7564 isl_map_free(map);
7565 return NULL;
7568 /* Force the values of the variable at position "pos" of type "type" of "set"
7569 * to be no greater than "value".
7571 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
7572 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
7574 isl_map *map;
7576 map = set_to_map(set);
7577 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
7580 /* If "mv" has an explicit domain, then intersect the domain of "map"
7581 * with this explicit domain.
7583 * An isl_multi_val object never has an explicit domain,
7584 * so simply return "map".
7586 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
7587 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
7589 return map;
7592 #undef BASE
7593 #define BASE val
7594 #include "isl_map_bound_templ.c"
7596 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7597 * for each set dimension, by treating the set as a map.
7599 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
7600 __isl_take isl_multi_val *bound,
7601 __isl_give isl_map *map_bound(__isl_take isl_map *map,
7602 unsigned pos, __isl_take isl_val *value))
7604 isl_map *map;
7606 map = set_to_map(set);
7607 return set_from_map(map_bound_multi_val(map, bound, map_bound));
7610 #undef BASE
7611 #define BASE pw_aff
7612 #include "isl_map_bound_templ.c"
7614 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7615 * for each set dimension, by converting the set and the bound
7616 * to objects living in a map space.
7618 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
7619 __isl_take isl_multi_pw_aff *bound,
7620 __isl_give isl_map *set_bound(__isl_take isl_map *map,
7621 unsigned pos, __isl_take TYPE *value))
7623 isl_map *map;
7625 map = isl_map_from_range(set);
7626 bound = isl_multi_pw_aff_from_range(bound);
7627 map = map_bound_multi_pw_aff(map, bound, set_bound);
7628 return isl_map_range(map);
7631 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7632 * setting a bound on the given output dimension.
7634 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
7635 unsigned pos, __isl_take isl_val *v)
7637 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
7640 /* Force the values of the set dimensions of "set"
7641 * to be no smaller than the corresponding values in "lower".
7643 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7644 __isl_take isl_multi_val *lower)
7646 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7649 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7650 * setting a bound on the given output dimension.
7652 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7653 unsigned pos, __isl_take isl_val *v)
7655 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7658 /* Force the values of the set dimensions of "set"
7659 * to be no greater than the corresponding values in "upper".
7661 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7662 __isl_take isl_multi_val *upper)
7664 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7667 /* Force the symbolic constant expression "bound"
7668 * to satisfy the relation "order" with respect to
7669 * the output variable at position "pos" of "map".
7671 * Create an affine expression representing the output variable
7672 * in terms of the range and
7673 * compare it using "order" to "bound" (defined on the domain).
7674 * The result is a relation between elements in domain and range that
7675 * can be intersected with "map".
7677 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7678 unsigned pos, __isl_take isl_pw_aff *bound,
7679 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7680 __isl_take isl_pw_aff *pa2))
7682 isl_space *space;
7683 isl_local_space *ls;
7684 isl_pw_aff *var;
7686 space = isl_space_range(isl_map_get_space(map));
7687 ls = isl_local_space_from_space(space);
7688 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7689 map = isl_map_intersect(map, order(bound, var));
7690 return map;
7693 /* Force the values of the output variable at position "pos" of "map"
7694 * to be no smaller than the symbolic constant expression "lower".
7696 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7697 unsigned pos, __isl_take isl_pw_aff *lower)
7699 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7702 /* Force the values of the output variable at position "pos" of "map"
7703 * to be no greater than the symbolic constant expression "upper".
7705 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7706 unsigned pos, __isl_take isl_pw_aff *upper)
7708 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7711 /* Force the values of the set dimensions of "set"
7712 * to be no smaller than the corresponding constant symbolic expressions
7713 * in "lower".
7715 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7716 __isl_take isl_multi_pw_aff *lower)
7718 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7721 /* Force the values of the set dimensions of "set"
7722 * to be no greater than the corresponding constant symbolic expressions
7723 * in "upper".
7725 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7726 __isl_take isl_multi_pw_aff *upper)
7728 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7731 /* Force the values of the output dimensions of "map"
7732 * to be no smaller than the corresponding constant symbolic expressions
7733 * in "lower".
7735 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7736 __isl_take isl_multi_pw_aff *lower)
7738 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7741 /* Force the values of the output dimensions of "map"
7742 * to be no greater than the corresponding constant symbolic expressions
7743 * in "upper".
7745 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7746 __isl_take isl_multi_pw_aff *upper)
7748 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7751 /* Bound the given variable of "bset" from below (or above is "upper"
7752 * is set) to "value".
7754 static __isl_give isl_basic_set *isl_basic_set_bound(
7755 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7756 isl_int value, int upper)
7758 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7759 type, pos, value, upper));
7762 /* Bound the given variable of "bset" from below (or above is "upper"
7763 * is set) to "value".
7765 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7766 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7767 __isl_take isl_val *value, int upper)
7769 if (!value)
7770 goto error;
7771 if (!isl_val_is_int(value))
7772 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7773 "expecting integer value", goto error);
7774 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7775 isl_val_free(value);
7776 return bset;
7777 error:
7778 isl_val_free(value);
7779 isl_basic_set_free(bset);
7780 return NULL;
7783 /* Bound the given variable of "bset" from below to "value".
7785 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7786 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7787 __isl_take isl_val *value)
7789 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7792 /* Bound the given variable of "bset" from above to "value".
7794 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7795 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7796 __isl_take isl_val *value)
7798 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7801 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7803 return isl_map_transform(map, &isl_space_reverse,
7804 &isl_basic_map_reverse);
7807 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7809 __isl_give isl_map *isl_map_domain_reverse(__isl_take isl_map *map)
7811 return isl_map_transform(map, &isl_space_domain_reverse,
7812 &isl_basic_map_domain_reverse);
7815 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7817 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7819 return isl_map_transform(map, &isl_space_range_reverse,
7820 &isl_basic_map_range_reverse);
7823 /* Given a set (A -> B), return the corresponding set (B -> A).
7825 __isl_give isl_set *isl_set_wrapped_reverse(__isl_take isl_set *set)
7827 isl_map *map = set_to_map(set);
7829 map = isl_map_transform(map, &isl_space_wrapped_reverse,
7830 &isl_basic_map_set_reverse);
7831 return set_from_map(map);
7834 #undef TYPE
7835 #define TYPE isl_pw_multi_aff
7836 #undef SUFFIX
7837 #define SUFFIX _pw_multi_aff
7838 #undef EMPTY
7839 #define EMPTY isl_pw_multi_aff_empty
7840 #undef ADD
7841 #define ADD isl_pw_multi_aff_union_add
7842 #include "isl_map_lexopt_templ.c"
7844 /* Given a map "map", compute the lexicographically minimal
7845 * (or maximal) image element for each domain element in dom,
7846 * in the form of an isl_pw_multi_aff.
7847 * If "empty" is not NULL, then set *empty to those elements in dom that
7848 * do not have an image element.
7849 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7850 * should be computed over the domain of "map". "empty" is also NULL
7851 * in this case.
7853 * We first compute the lexicographically minimal or maximal element
7854 * in the first basic map. This results in a partial solution "res"
7855 * and a subset "todo" of dom that still need to be handled.
7856 * We then consider each of the remaining maps in "map" and successively
7857 * update both "res" and "todo".
7858 * If "empty" is NULL, then the todo sets are not needed and therefore
7859 * also not computed.
7861 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7862 __isl_take isl_map *map, __isl_take isl_set *dom,
7863 __isl_give isl_set **empty, unsigned flags)
7865 int i;
7866 int full;
7867 isl_pw_multi_aff *res;
7868 isl_set *todo;
7870 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7871 if (!map || (!full && !dom))
7872 goto error;
7874 if (isl_map_plain_is_empty(map)) {
7875 if (empty)
7876 *empty = dom;
7877 else
7878 isl_set_free(dom);
7879 return isl_pw_multi_aff_from_map(map);
7882 res = basic_map_partial_lexopt_pw_multi_aff(
7883 isl_basic_map_copy(map->p[0]),
7884 isl_set_copy(dom), empty, flags);
7886 if (empty)
7887 todo = *empty;
7888 for (i = 1; i < map->n; ++i) {
7889 isl_pw_multi_aff *res_i;
7891 res_i = basic_map_partial_lexopt_pw_multi_aff(
7892 isl_basic_map_copy(map->p[i]),
7893 isl_set_copy(dom), empty, flags);
7895 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7896 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7897 else
7898 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7900 if (empty)
7901 todo = isl_set_intersect(todo, *empty);
7904 isl_set_free(dom);
7905 isl_map_free(map);
7907 if (empty)
7908 *empty = todo;
7910 return res;
7911 error:
7912 if (empty)
7913 *empty = NULL;
7914 isl_set_free(dom);
7915 isl_map_free(map);
7916 return NULL;
7919 #undef TYPE
7920 #define TYPE isl_map
7921 #undef SUFFIX
7922 #define SUFFIX
7923 #undef EMPTY
7924 #define EMPTY isl_map_empty
7925 #undef ADD
7926 #define ADD isl_map_union_disjoint
7927 #include "isl_map_lexopt_templ.c"
7929 /* Given a map "map", compute the lexicographically minimal
7930 * (or maximal) image element for each domain element in "dom",
7931 * in the form of an isl_map.
7932 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7933 * do not have an image element.
7934 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7935 * should be computed over the domain of "map". "empty" is also NULL
7936 * in this case.
7938 * If the input consists of more than one disjunct, then first
7939 * compute the desired result in the form of an isl_pw_multi_aff and
7940 * then convert that into an isl_map.
7942 * This function used to have an explicit implementation in terms
7943 * of isl_maps, but it would continually intersect the domains of
7944 * partial results with the complement of the domain of the next
7945 * partial solution, potentially leading to an explosion in the number
7946 * of disjuncts if there are several disjuncts in the input.
7947 * An even earlier implementation of this function would look for
7948 * better results in the domain of the partial result and for extra
7949 * results in the complement of this domain, which would lead to
7950 * even more splintering.
7952 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7953 __isl_take isl_map *map, __isl_take isl_set *dom,
7954 __isl_give isl_set **empty, unsigned flags)
7956 int full;
7957 struct isl_map *res;
7958 isl_pw_multi_aff *pma;
7960 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7961 if (!map || (!full && !dom))
7962 goto error;
7964 if (isl_map_plain_is_empty(map)) {
7965 if (empty)
7966 *empty = dom;
7967 else
7968 isl_set_free(dom);
7969 return map;
7972 if (map->n == 1) {
7973 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7974 dom, empty, flags);
7975 isl_map_free(map);
7976 return res;
7979 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7980 flags);
7981 return isl_map_from_pw_multi_aff_internal(pma);
7982 error:
7983 if (empty)
7984 *empty = NULL;
7985 isl_set_free(dom);
7986 isl_map_free(map);
7987 return NULL;
7990 __isl_give isl_map *isl_map_partial_lexmax(
7991 __isl_take isl_map *map, __isl_take isl_set *dom,
7992 __isl_give isl_set **empty)
7994 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7997 __isl_give isl_map *isl_map_partial_lexmin(
7998 __isl_take isl_map *map, __isl_take isl_set *dom,
7999 __isl_give isl_set **empty)
8001 return isl_map_partial_lexopt(map, dom, empty, 0);
8004 __isl_give isl_set *isl_set_partial_lexmin(
8005 __isl_take isl_set *set, __isl_take isl_set *dom,
8006 __isl_give isl_set **empty)
8008 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
8009 dom, empty));
8012 __isl_give isl_set *isl_set_partial_lexmax(
8013 __isl_take isl_set *set, __isl_take isl_set *dom,
8014 __isl_give isl_set **empty)
8016 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
8017 dom, empty));
8020 /* Compute the lexicographic minimum (or maximum if "flags" includes
8021 * ISL_OPT_MAX) of "bset" over its parametric domain.
8023 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
8024 unsigned flags)
8026 return isl_basic_map_lexopt(bset, flags);
8029 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
8031 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
8034 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
8036 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
8039 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
8041 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
8044 /* Compute the lexicographic minimum of "bset" over its parametric domain
8045 * for the purpose of quantifier elimination.
8046 * That is, find an explicit representation for all the existentially
8047 * quantified variables in "bset" by computing their lexicographic
8048 * minimum.
8050 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
8051 __isl_take isl_basic_set *bset)
8053 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
8056 /* Given a basic map with one output dimension, compute the minimum or
8057 * maximum of that dimension as an isl_pw_aff.
8059 * Compute the optimum as a lexicographic optimum over the single
8060 * output dimension and extract the single isl_pw_aff from the result.
8062 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
8063 int max)
8065 isl_pw_multi_aff *pma;
8066 isl_pw_aff *pwaff;
8068 bmap = isl_basic_map_copy(bmap);
8069 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
8070 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
8071 isl_pw_multi_aff_free(pma);
8073 return pwaff;
8076 /* Compute the minimum or maximum of the given output dimension
8077 * as a function of the parameters and the input dimensions,
8078 * but independently of the other output dimensions.
8080 * We first project out the other output dimension and then compute
8081 * the "lexicographic" maximum in each basic map, combining the results
8082 * using isl_pw_aff_union_max.
8084 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
8085 int max)
8087 int i;
8088 isl_pw_aff *pwaff;
8089 isl_size n_out;
8091 n_out = isl_map_dim(map, isl_dim_out);
8092 if (n_out < 0)
8093 map = isl_map_free(map);
8094 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
8095 map = isl_map_project_out(map, isl_dim_out, 0, pos);
8096 if (!map)
8097 return NULL;
8099 if (map->n == 0) {
8100 isl_space *space = isl_map_get_space(map);
8101 isl_map_free(map);
8102 return isl_pw_aff_empty(space);
8105 pwaff = basic_map_dim_opt(map->p[0], max);
8106 for (i = 1; i < map->n; ++i) {
8107 isl_pw_aff *pwaff_i;
8109 pwaff_i = basic_map_dim_opt(map->p[i], max);
8110 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
8113 isl_map_free(map);
8115 return pwaff;
8118 /* Compute the minimum of the given output dimension as a function of the
8119 * parameters and input dimensions, but independently of
8120 * the other output dimensions.
8122 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
8124 return map_dim_opt(map, pos, 0);
8127 /* Compute the maximum of the given output dimension as a function of the
8128 * parameters and input dimensions, but independently of
8129 * the other output dimensions.
8131 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
8133 return map_dim_opt(map, pos, 1);
8136 /* Compute the minimum or maximum of the given set dimension
8137 * as a function of the parameters,
8138 * but independently of the other set dimensions.
8140 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
8141 int max)
8143 return map_dim_opt(set, pos, max);
8146 /* Compute the maximum of the given set dimension as a function of the
8147 * parameters, but independently of the other set dimensions.
8149 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
8151 return set_dim_opt(set, pos, 1);
8154 /* Compute the minimum of the given set dimension as a function of the
8155 * parameters, but independently of the other set dimensions.
8157 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
8159 return set_dim_opt(set, pos, 0);
8162 /* Apply a preimage specified by "mat" on the parameters of "bset".
8163 * bset is assumed to have only parameters and divs.
8165 static __isl_give isl_basic_set *basic_set_parameter_preimage(
8166 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
8168 isl_size nparam;
8170 nparam = isl_basic_set_dim(bset, isl_dim_param);
8171 if (nparam < 0 || !mat)
8172 goto error;
8174 bset->dim = isl_space_cow(bset->dim);
8175 if (!bset->dim)
8176 goto error;
8178 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
8180 bset->dim->nparam = 0;
8181 bset->dim->n_out = nparam;
8182 bset = isl_basic_set_preimage(bset, mat);
8183 if (bset) {
8184 bset->dim->nparam = bset->dim->n_out;
8185 bset->dim->n_out = 0;
8187 return bset;
8188 error:
8189 isl_mat_free(mat);
8190 isl_basic_set_free(bset);
8191 return NULL;
8194 /* Apply a preimage specified by "mat" on the parameters of "set".
8195 * set is assumed to have only parameters and divs.
8197 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
8198 __isl_take isl_mat *mat)
8200 isl_space *space;
8201 isl_size nparam;
8203 nparam = isl_set_dim(set, isl_dim_param);
8204 if (nparam < 0 || !mat)
8205 goto error;
8207 if (mat->n_row != 1 + nparam)
8208 isl_die(isl_set_get_ctx(set), isl_error_internal,
8209 "unexpected number of rows", goto error);
8211 space = isl_set_get_space(set);
8212 space = isl_space_move_dims(space, isl_dim_set, 0,
8213 isl_dim_param, 0, nparam);
8214 set = isl_set_reset_space(set, space);
8215 set = isl_set_preimage(set, mat);
8216 nparam = isl_set_dim(set, isl_dim_out);
8217 if (nparam < 0)
8218 set = isl_set_free(set);
8219 space = isl_set_get_space(set);
8220 space = isl_space_move_dims(space, isl_dim_param, 0,
8221 isl_dim_out, 0, nparam);
8222 set = isl_set_reset_space(set, space);
8223 return set;
8224 error:
8225 isl_mat_free(mat);
8226 isl_set_free(set);
8227 return NULL;
8230 /* Intersect the basic set "bset" with the affine space specified by the
8231 * equalities in "eq".
8233 static __isl_give isl_basic_set *basic_set_append_equalities(
8234 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
8236 int i, k;
8237 unsigned len;
8239 if (!bset || !eq)
8240 goto error;
8242 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
8243 if (!bset)
8244 goto error;
8246 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
8247 for (i = 0; i < eq->n_row; ++i) {
8248 k = isl_basic_set_alloc_equality(bset);
8249 if (k < 0)
8250 goto error;
8251 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
8252 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
8254 isl_mat_free(eq);
8256 bset = isl_basic_set_gauss(bset, NULL);
8257 bset = isl_basic_set_finalize(bset);
8259 return bset;
8260 error:
8261 isl_mat_free(eq);
8262 isl_basic_set_free(bset);
8263 return NULL;
8266 /* Intersect the set "set" with the affine space specified by the
8267 * equalities in "eq".
8269 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
8270 __isl_take isl_mat *eq)
8272 int i;
8274 if (!set || !eq)
8275 goto error;
8277 for (i = 0; i < set->n; ++i) {
8278 set->p[i] = basic_set_append_equalities(set->p[i],
8279 isl_mat_copy(eq));
8280 if (!set->p[i])
8281 goto error;
8283 isl_mat_free(eq);
8284 return set;
8285 error:
8286 isl_mat_free(eq);
8287 isl_set_free(set);
8288 return NULL;
8291 /* Given a basic set "bset" that only involves parameters and existentially
8292 * quantified variables, return the index of the first equality
8293 * that only involves parameters. If there is no such equality then
8294 * return bset->n_eq.
8296 * This function assumes that isl_basic_set_gauss has been called on "bset".
8298 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
8300 int i, j;
8301 isl_size nparam, n_div;
8303 nparam = isl_basic_set_dim(bset, isl_dim_param);
8304 n_div = isl_basic_set_dim(bset, isl_dim_div);
8305 if (nparam < 0 || n_div < 0)
8306 return -1;
8308 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
8309 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
8310 ++i;
8313 return i;
8316 /* Compute an explicit representation for the existentially quantified
8317 * variables in "bset" by computing the "minimal value" of the set
8318 * variables. Since there are no set variables, the computation of
8319 * the minimal value essentially computes an explicit representation
8320 * of the non-empty part(s) of "bset".
8322 * The input only involves parameters and existentially quantified variables.
8323 * All equalities among parameters have been removed.
8325 * Since the existentially quantified variables in the result are in general
8326 * going to be different from those in the input, we first replace
8327 * them by the minimal number of variables based on their equalities.
8328 * This should simplify the parametric integer programming.
8330 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
8332 isl_morph *morph1, *morph2;
8333 isl_set *set;
8334 isl_size n;
8336 if (!bset)
8337 return NULL;
8338 if (bset->n_eq == 0)
8339 return isl_basic_set_lexmin_compute_divs(bset);
8341 morph1 = isl_basic_set_parameter_compression(bset);
8342 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
8343 bset = isl_basic_set_lift(bset);
8344 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
8345 bset = isl_morph_basic_set(morph2, bset);
8346 n = isl_basic_set_dim(bset, isl_dim_set);
8347 if (n < 0)
8348 bset = isl_basic_set_free(bset);
8349 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
8351 set = isl_basic_set_lexmin_compute_divs(bset);
8353 set = isl_morph_set(isl_morph_inverse(morph1), set);
8355 return set;
8358 /* Project the given basic set onto its parameter domain, possibly introducing
8359 * new, explicit, existential variables in the constraints.
8360 * The input has parameters and (possibly implicit) existential variables.
8361 * The output has the same parameters, but only
8362 * explicit existentially quantified variables.
8364 * The actual projection is performed by pip, but pip doesn't seem
8365 * to like equalities very much, so we first remove the equalities
8366 * among the parameters by performing a variable compression on
8367 * the parameters. Afterward, an inverse transformation is performed
8368 * and the equalities among the parameters are inserted back in.
8370 * The variable compression on the parameters may uncover additional
8371 * equalities that were only implicit before. We therefore check
8372 * if there are any new parameter equalities in the result and
8373 * if so recurse. The removal of parameter equalities is required
8374 * for the parameter compression performed by base_compute_divs.
8376 static __isl_give isl_set *parameter_compute_divs(
8377 __isl_take isl_basic_set *bset)
8379 int i;
8380 struct isl_mat *eq;
8381 struct isl_mat *T, *T2;
8382 struct isl_set *set;
8383 isl_size nparam;
8385 bset = isl_basic_set_cow(bset);
8386 if (!bset)
8387 return NULL;
8389 if (bset->n_eq == 0)
8390 return base_compute_divs(bset);
8392 bset = isl_basic_set_gauss(bset, NULL);
8393 if (!bset)
8394 return NULL;
8395 if (isl_basic_set_plain_is_empty(bset))
8396 return isl_set_from_basic_set(bset);
8398 i = first_parameter_equality(bset);
8399 if (i == bset->n_eq)
8400 return base_compute_divs(bset);
8402 nparam = isl_basic_set_dim(bset, isl_dim_param);
8403 if (nparam < 0)
8404 return isl_set_from_basic_set(isl_basic_set_free(bset));
8405 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
8406 0, 1 + nparam);
8407 eq = isl_mat_cow(eq);
8408 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
8409 if (T && T->n_col == 0) {
8410 isl_mat_free(T);
8411 isl_mat_free(T2);
8412 isl_mat_free(eq);
8413 bset = isl_basic_set_set_to_empty(bset);
8414 return isl_set_from_basic_set(bset);
8416 bset = basic_set_parameter_preimage(bset, T);
8418 i = first_parameter_equality(bset);
8419 if (!bset)
8420 set = NULL;
8421 else if (i == bset->n_eq)
8422 set = base_compute_divs(bset);
8423 else
8424 set = parameter_compute_divs(bset);
8425 set = set_parameter_preimage(set, T2);
8426 set = set_append_equalities(set, eq);
8427 return set;
8430 /* Insert the divs from "ls" before those of "bmap".
8432 * The number of columns is not changed, which means that the last
8433 * dimensions of "bmap" are being reintepreted as the divs from "ls".
8434 * The caller is responsible for removing the same number of dimensions
8435 * from the space of "bmap".
8437 static __isl_give isl_basic_map *insert_divs_from_local_space(
8438 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
8440 int i;
8441 isl_size n_div;
8442 int old_n_div;
8444 n_div = isl_local_space_dim(ls, isl_dim_div);
8445 if (n_div < 0)
8446 return isl_basic_map_free(bmap);
8447 if (n_div == 0)
8448 return bmap;
8450 old_n_div = bmap->n_div;
8451 bmap = insert_div_rows(bmap, n_div);
8452 if (!bmap)
8453 return NULL;
8455 for (i = 0; i < n_div; ++i) {
8456 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
8457 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
8460 return bmap;
8463 /* Replace the space of "bmap" by the space and divs of "ls".
8465 * If "ls" has any divs, then we simplify the result since we may
8466 * have discovered some additional equalities that could simplify
8467 * the div expressions.
8469 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
8470 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
8472 isl_size n_div;
8474 bmap = isl_basic_map_cow(bmap);
8475 n_div = isl_local_space_dim(ls, isl_dim_div);
8476 if (!bmap || n_div < 0)
8477 goto error;
8479 bmap = insert_divs_from_local_space(bmap, ls);
8480 if (!bmap)
8481 goto error;
8483 isl_space_free(bmap->dim);
8484 bmap->dim = isl_local_space_get_space(ls);
8485 if (!bmap->dim)
8486 goto error;
8488 isl_local_space_free(ls);
8489 if (n_div > 0)
8490 bmap = isl_basic_map_simplify(bmap);
8491 bmap = isl_basic_map_finalize(bmap);
8492 return bmap;
8493 error:
8494 isl_basic_map_free(bmap);
8495 isl_local_space_free(ls);
8496 return NULL;
8499 /* Replace the space of "map" by the space and divs of "ls".
8501 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
8502 __isl_take isl_local_space *ls)
8504 int i;
8506 map = isl_map_cow(map);
8507 if (!map || !ls)
8508 goto error;
8510 for (i = 0; i < map->n; ++i) {
8511 map->p[i] = basic_replace_space_by_local_space(map->p[i],
8512 isl_local_space_copy(ls));
8513 if (!map->p[i])
8514 goto error;
8516 isl_space_free(isl_map_take_space(map));
8517 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
8519 isl_local_space_free(ls);
8520 return map;
8521 error:
8522 isl_local_space_free(ls);
8523 isl_map_free(map);
8524 return NULL;
8527 /* Compute an explicit representation for the existentially
8528 * quantified variables for which do not know any explicit representation yet.
8530 * We first sort the existentially quantified variables so that the
8531 * existentially quantified variables for which we already have an explicit
8532 * representation are placed before those for which we do not.
8533 * The input dimensions, the output dimensions and the existentially
8534 * quantified variables for which we already have an explicit
8535 * representation are then turned into parameters.
8536 * compute_divs returns a map with the same parameters and
8537 * no input or output dimensions and the dimension specification
8538 * is reset to that of the input, including the existentially quantified
8539 * variables for which we already had an explicit representation.
8541 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
8543 struct isl_basic_set *bset;
8544 struct isl_set *set;
8545 struct isl_map *map;
8546 isl_space *space;
8547 isl_local_space *ls;
8548 isl_size nparam;
8549 isl_size n_in;
8550 isl_size n_out;
8551 int n_known;
8552 int i;
8554 bmap = isl_basic_map_sort_divs(bmap);
8555 bmap = isl_basic_map_cow(bmap);
8556 if (!bmap)
8557 return NULL;
8559 n_known = isl_basic_map_first_unknown_div(bmap);
8560 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8561 n_in = isl_basic_map_dim(bmap, isl_dim_in);
8562 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8563 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
8564 return isl_map_from_basic_map(isl_basic_map_free(bmap));
8566 space = isl_space_set_alloc(bmap->ctx,
8567 nparam + n_in + n_out + n_known, 0);
8568 if (!space)
8569 goto error;
8571 ls = isl_basic_map_get_local_space(bmap);
8572 ls = isl_local_space_drop_dims(ls, isl_dim_div,
8573 n_known, bmap->n_div - n_known);
8574 if (n_known > 0) {
8575 for (i = n_known; i < bmap->n_div; ++i)
8576 swap_div(bmap, i - n_known, i);
8577 bmap->n_div -= n_known;
8578 bmap->extra -= n_known;
8580 bmap = isl_basic_map_reset_space(bmap, space);
8581 bset = bset_from_bmap(bmap);
8583 set = parameter_compute_divs(bset);
8584 map = set_to_map(set);
8585 map = replace_space_by_local_space(map, ls);
8587 return map;
8588 error:
8589 isl_basic_map_free(bmap);
8590 return NULL;
8593 /* Is the integer division at position "div" of "bmap" integral?
8594 * That is, does it have denominator 1?
8596 isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
8598 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8599 return isl_bool_error;
8600 return isl_int_is_one(bmap->div[div][0]);
8603 /* Remove the explicit representation of local variable "div",
8604 * if there is any.
8606 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
8607 __isl_take isl_basic_map *bmap, int div)
8609 isl_bool unknown;
8611 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
8612 if (unknown < 0)
8613 return isl_basic_map_free(bmap);
8614 if (unknown)
8615 return bmap;
8617 bmap = isl_basic_map_cow(bmap);
8618 if (!bmap)
8619 return NULL;
8620 isl_int_set_si(bmap->div[div][0], 0);
8621 return bmap;
8624 /* Is local variable "div" of "bmap" marked as not having an explicit
8625 * representation?
8626 * Note that even if "div" is not marked in this way and therefore
8627 * has an explicit representation, this representation may still
8628 * depend (indirectly) on other local variables that do not
8629 * have an explicit representation.
8631 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
8632 int div)
8634 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
8635 return isl_bool_error;
8636 return isl_int_is_zero(bmap->div[div][0]);
8639 /* Return the position of the first local variable that does not
8640 * have an explicit representation.
8641 * Return the total number of local variables if they all have
8642 * an explicit representation.
8643 * Return -1 on error.
8645 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
8647 int i;
8649 if (!bmap)
8650 return -1;
8652 for (i = 0; i < bmap->n_div; ++i) {
8653 if (!isl_basic_map_div_is_known(bmap, i))
8654 return i;
8656 return bmap->n_div;
8659 /* Return the position of the first local variable that does not
8660 * have an explicit representation.
8661 * Return the total number of local variables if they all have
8662 * an explicit representation.
8663 * Return -1 on error.
8665 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
8667 return isl_basic_map_first_unknown_div(bset);
8670 /* Does "bmap" have an explicit representation for all local variables?
8672 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8674 int first;
8675 isl_size n;
8677 n = isl_basic_map_dim(bmap, isl_dim_div);
8678 first = isl_basic_map_first_unknown_div(bmap);
8679 if (n < 0 || first < 0)
8680 return isl_bool_error;
8681 return first == n;
8684 /* Do all basic maps in "map" have an explicit representation
8685 * for all local variables?
8687 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8689 int i;
8691 if (!map)
8692 return isl_bool_error;
8694 for (i = 0; i < map->n; ++i) {
8695 int known = isl_basic_map_divs_known(map->p[i]);
8696 if (known <= 0)
8697 return known;
8700 return isl_bool_true;
8703 /* If bmap contains any unknown divs, then compute explicit
8704 * expressions for them. However, this computation may be
8705 * quite expensive, so first try to remove divs that aren't
8706 * strictly needed.
8708 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8710 int known;
8711 struct isl_map *map;
8713 known = isl_basic_map_divs_known(bmap);
8714 if (known < 0)
8715 goto error;
8716 if (known)
8717 return isl_map_from_basic_map(bmap);
8719 bmap = isl_basic_map_drop_redundant_divs(bmap);
8721 known = isl_basic_map_divs_known(bmap);
8722 if (known < 0)
8723 goto error;
8724 if (known)
8725 return isl_map_from_basic_map(bmap);
8727 map = compute_divs(bmap);
8728 return map;
8729 error:
8730 isl_basic_map_free(bmap);
8731 return NULL;
8734 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8736 int i;
8737 int known;
8738 struct isl_map *res;
8740 if (!map)
8741 return NULL;
8742 if (map->n == 0)
8743 return map;
8745 known = isl_map_divs_known(map);
8746 if (known < 0) {
8747 isl_map_free(map);
8748 return NULL;
8750 if (known)
8751 return map;
8753 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8754 for (i = 1 ; i < map->n; ++i) {
8755 struct isl_map *r2;
8756 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8757 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8758 res = isl_map_union_disjoint(res, r2);
8759 else
8760 res = isl_map_union(res, r2);
8762 isl_map_free(map);
8764 return res;
8767 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8769 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8772 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8774 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8777 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8779 isl_space *space;
8780 isl_size n_out;
8782 n_out = isl_map_dim(map, isl_dim_out);
8783 if (n_out < 0)
8784 return set_from_map(isl_map_free(map));
8785 space = isl_space_domain(isl_map_get_space(map));
8787 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8789 return set_from_map(isl_map_reset_space(map, space));
8792 /* Return the union of "map1" and "map2", where we assume for now that
8793 * "map1" and "map2" are disjoint. Note that the basic maps inside
8794 * "map1" or "map2" may not be disjoint from each other.
8795 * Also note that this function is also called from isl_map_union,
8796 * which takes care of handling the situation where "map1" and "map2"
8797 * may not be disjoint.
8799 * If one of the inputs is empty, we can simply return the other input.
8800 * Similarly, if one of the inputs is universal, then it is equal to the union.
8802 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8803 __isl_take isl_map *map2)
8805 int i;
8806 unsigned flags = 0;
8807 struct isl_map *map = NULL;
8808 int is_universe;
8810 if (isl_map_check_equal_space(map1, map2) < 0)
8811 goto error;
8813 if (map1->n == 0) {
8814 isl_map_free(map1);
8815 return map2;
8817 if (map2->n == 0) {
8818 isl_map_free(map2);
8819 return map1;
8822 is_universe = isl_map_plain_is_universe(map1);
8823 if (is_universe < 0)
8824 goto error;
8825 if (is_universe) {
8826 isl_map_free(map2);
8827 return map1;
8830 is_universe = isl_map_plain_is_universe(map2);
8831 if (is_universe < 0)
8832 goto error;
8833 if (is_universe) {
8834 isl_map_free(map1);
8835 return map2;
8838 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8839 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8840 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8842 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8843 map1->n + map2->n, flags);
8844 if (!map)
8845 goto error;
8846 for (i = 0; i < map1->n; ++i) {
8847 map = isl_map_add_basic_map(map,
8848 isl_basic_map_copy(map1->p[i]));
8849 if (!map)
8850 goto error;
8852 for (i = 0; i < map2->n; ++i) {
8853 map = isl_map_add_basic_map(map,
8854 isl_basic_map_copy(map2->p[i]));
8855 if (!map)
8856 goto error;
8858 isl_map_free(map1);
8859 isl_map_free(map2);
8860 return map;
8861 error:
8862 isl_map_free(map);
8863 isl_map_free(map1);
8864 isl_map_free(map2);
8865 return NULL;
8868 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8869 * guaranteed to be disjoint by the caller.
8871 * Note that this functions is called from within isl_map_make_disjoint,
8872 * so we have to be careful not to touch the constraints of the inputs
8873 * in any way.
8875 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8876 __isl_take isl_map *map2)
8878 isl_map_align_params_bin(&map1, &map2);
8879 return map_union_disjoint(map1, map2);
8882 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8883 * not be disjoint.
8885 * We currently simply call map_union_disjoint, the internal operation
8886 * of which does not really depend on the inputs being disjoint.
8887 * If the result contains more than one basic map, then we clear
8888 * the disjoint flag since the result may contain basic maps from
8889 * both inputs and these are not guaranteed to be disjoint.
8891 * As a special case, if "map1" and "map2" are obviously equal,
8892 * then we simply return "map1".
8894 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8895 __isl_take isl_map *map2)
8897 int equal;
8899 if (isl_map_align_params_bin(&map1, &map2) < 0)
8900 goto error;
8902 equal = isl_map_plain_is_equal(map1, map2);
8903 if (equal < 0)
8904 goto error;
8905 if (equal) {
8906 isl_map_free(map2);
8907 return map1;
8910 map1 = map_union_disjoint(map1, map2);
8911 if (!map1)
8912 return NULL;
8913 if (map1->n > 1)
8914 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8915 return map1;
8916 error:
8917 isl_map_free(map1);
8918 isl_map_free(map2);
8919 return NULL;
8922 __isl_give isl_set *isl_set_union_disjoint(
8923 __isl_take isl_set *set1, __isl_take isl_set *set2)
8925 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8926 set_to_map(set2)));
8929 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8930 __isl_take isl_set *set2)
8932 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8935 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8936 * the results in a map living in "space".
8938 * "map" and "set" are assumed to be compatible and non-NULL.
8940 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8941 __isl_take isl_space *space, __isl_take isl_set *set,
8942 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8943 __isl_take isl_basic_set *bset))
8945 unsigned flags = 0;
8946 struct isl_map *result;
8947 int i, j;
8949 if (isl_set_plain_is_universe(set)) {
8950 isl_set_free(set);
8951 return isl_map_reset_equal_dim_space(map, space);
8954 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8955 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8956 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8958 result = isl_map_alloc_space(space, map->n * set->n, flags);
8959 for (i = 0; result && i < map->n; ++i)
8960 for (j = 0; j < set->n; ++j) {
8961 result = isl_map_add_basic_map(result,
8962 fn(isl_basic_map_copy(map->p[i]),
8963 isl_basic_set_copy(set->p[j])));
8964 if (!result)
8965 break;
8968 isl_map_free(map);
8969 isl_set_free(set);
8970 return result;
8973 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8974 __isl_take isl_set *set)
8976 isl_bool ok;
8977 isl_space *space;
8979 isl_map_align_params_set(&map, &set);
8980 ok = isl_map_compatible_range(map, set);
8981 if (ok < 0)
8982 goto error;
8983 if (!ok)
8984 isl_die(set->ctx, isl_error_invalid,
8985 "incompatible spaces", goto error);
8987 space = isl_map_get_space(map);
8988 return map_intersect_set(map, space, set,
8989 &isl_basic_map_intersect_range);
8990 error:
8991 isl_map_free(map);
8992 isl_set_free(set);
8993 return NULL;
8996 /* Intersect the domain of "map" with "set".
8998 * If the domain dimensions of "map" do not have any identifiers,
8999 * then copy them over from "set".
9001 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
9002 __isl_take isl_set *set)
9004 isl_bool ok;
9005 isl_space *space;
9007 isl_map_align_params_set(&map, &set);
9008 ok = isl_map_compatible_domain(map, set);
9009 if (ok < 0)
9010 goto error;
9011 if (!ok)
9012 isl_die(set->ctx, isl_error_invalid,
9013 "incompatible spaces", goto error);
9015 space = isl_map_get_space(map);
9016 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
9017 isl_set_peek_space(set), isl_dim_set);
9018 return map_intersect_set(map, space, set,
9019 &isl_basic_map_intersect_domain);
9020 error:
9021 isl_map_free(map);
9022 isl_set_free(set);
9023 return NULL;
9026 #undef BASE
9027 #define BASE map
9028 static
9029 #include "isl_copy_tuple_id_templ.c"
9031 /* Data structure that specifies how isl_map_intersect_factor
9032 * should operate.
9034 * "preserve_type" is the tuple where the factor differs from
9035 * the input map and of which the identifiers needs
9036 * to be preserved explicitly.
9037 * "other_factor" is used to extract the space of the other factor
9038 * from the space of the product ("map").
9039 * "product" is used to combine the given factor and a universe map
9040 * in the space returned by "other_factor" to produce a map
9041 * that lives in the same space as the input map.
9043 struct isl_intersect_factor_control {
9044 enum isl_dim_type preserve_type;
9045 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
9046 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
9047 __isl_take isl_map *other);
9050 /* Given a map "map" in some product space and a map "factor"
9051 * living in some factor space, return the intersection.
9053 * After aligning the parameters,
9054 * the map "factor" is first extended to a map living in the same space
9055 * as "map" and then a regular intersection is computed.
9057 * Note that the extension is computed as a product, which is anonymous
9058 * by default. If "map" has an identifier on the corresponding tuple,
9059 * then this identifier needs to be set on the product
9060 * before the intersection is computed.
9062 static __isl_give isl_map *isl_map_intersect_factor(
9063 __isl_take isl_map *map, __isl_take isl_map *factor,
9064 struct isl_intersect_factor_control *control)
9066 isl_bool equal;
9067 isl_space *space;
9068 isl_map *other, *product;
9070 equal = isl_map_has_equal_params(map, factor);
9071 if (equal < 0)
9072 goto error;
9073 if (!equal) {
9074 map = isl_map_align_params(map, isl_map_get_space(factor));
9075 factor = isl_map_align_params(factor, isl_map_get_space(map));
9078 space = isl_map_get_space(map);
9079 other = isl_map_universe(control->other_factor(space));
9080 product = control->product(factor, other);
9082 space = isl_map_peek_space(map);
9083 product = isl_map_copy_tuple_id(product, control->preserve_type,
9084 space, control->preserve_type);
9085 return map_intersect(map, product);
9086 error:
9087 isl_map_free(map);
9088 isl_map_free(factor);
9089 return NULL;
9092 /* Return the domain product of "map2" and "map1".
9094 static __isl_give isl_map *isl_map_reverse_domain_product(
9095 __isl_take isl_map *map1, __isl_take isl_map *map2)
9097 return isl_map_domain_product(map2, map1);
9100 /* Return the range product of "map2" and "map1".
9102 static __isl_give isl_map *isl_map_reverse_range_product(
9103 __isl_take isl_map *map1, __isl_take isl_map *map2)
9105 return isl_map_range_product(map2, map1);
9108 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9109 * in the space A -> C, return the intersection.
9111 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
9112 __isl_take isl_map *map, __isl_take isl_map *factor)
9114 struct isl_intersect_factor_control control = {
9115 .preserve_type = isl_dim_in,
9116 .other_factor = isl_space_domain_factor_range,
9117 .product = isl_map_domain_product,
9120 return isl_map_intersect_factor(map, factor, &control);
9123 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
9124 * in the space B -> C, return the intersection.
9126 __isl_give isl_map *isl_map_intersect_domain_factor_range(
9127 __isl_take isl_map *map, __isl_take isl_map *factor)
9129 struct isl_intersect_factor_control control = {
9130 .preserve_type = isl_dim_in,
9131 .other_factor = isl_space_domain_factor_domain,
9132 .product = isl_map_reverse_domain_product,
9135 return isl_map_intersect_factor(map, factor, &control);
9138 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9139 * in the space A -> B, return the intersection.
9141 __isl_give isl_map *isl_map_intersect_range_factor_domain(
9142 __isl_take isl_map *map, __isl_take isl_map *factor)
9144 struct isl_intersect_factor_control control = {
9145 .preserve_type = isl_dim_out,
9146 .other_factor = isl_space_range_factor_range,
9147 .product = isl_map_range_product,
9150 return isl_map_intersect_factor(map, factor, &control);
9153 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
9154 * in the space A -> C, return the intersection.
9156 __isl_give isl_map *isl_map_intersect_range_factor_range(
9157 __isl_take isl_map *map, __isl_take isl_map *factor)
9159 struct isl_intersect_factor_control control = {
9160 .preserve_type = isl_dim_out,
9161 .other_factor = isl_space_range_factor_domain,
9162 .product = isl_map_reverse_range_product,
9165 return isl_map_intersect_factor(map, factor, &control);
9168 /* Given a set "set" in a space [A -> B] and a set "domain"
9169 * in the space A, return the intersection.
9171 * The set "domain" is first extended to a set living in the space
9172 * [A -> B] and then a regular intersection is computed.
9174 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
9175 __isl_take isl_set *domain)
9177 struct isl_intersect_factor_control control = {
9178 .preserve_type = isl_dim_set,
9179 .other_factor = isl_space_factor_range,
9180 .product = isl_map_range_product,
9183 return set_from_map(isl_map_intersect_factor(set_to_map(set),
9184 set_to_map(domain), &control));
9187 /* Given a set "set" in a space [A -> B] and a set "range"
9188 * in the space B, return the intersection.
9190 * The set "range" is first extended to a set living in the space
9191 * [A -> B] and then a regular intersection is computed.
9193 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
9194 __isl_take isl_set *range)
9196 struct isl_intersect_factor_control control = {
9197 .preserve_type = isl_dim_set,
9198 .other_factor = isl_space_factor_domain,
9199 .product = isl_map_reverse_range_product,
9202 return set_from_map(isl_map_intersect_factor(set_to_map(set),
9203 set_to_map(range), &control));
9206 #undef BASE
9207 #define BASE set
9208 static
9209 #include "isl_copy_tuple_id_templ.c"
9211 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
9212 * in the space A, return the intersection.
9214 * The set "domain" is extended to a set living in the space [A -> B] and
9215 * the domain of "map" is intersected with this set.
9217 * If "map" has an identifier on the domain tuple,
9218 * then this identifier needs to be set on this product
9219 * before the intersection is computed.
9221 __isl_give isl_map *isl_map_intersect_domain_wrapped_domain(
9222 __isl_take isl_map *map, __isl_take isl_set *domain)
9224 isl_space *space;
9225 isl_set *factor;
9227 isl_map_align_params_set(&map, &domain);
9228 space = isl_map_get_space(map);
9229 space = isl_space_domain_wrapped_range(space);
9230 factor = isl_set_universe(space);
9231 domain = isl_set_product(domain, factor);
9232 space = isl_map_peek_space(map);
9233 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_in);
9234 return isl_map_intersect_domain(map, domain);
9237 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
9238 * in the space B, return the intersection.
9240 * The set "domain" is extended to a set living in the space [B -> C] and
9241 * the range of "map" is intersected with this set.
9243 * If "map" has an identifier on the range tuple,
9244 * then this identifier needs to be set on this product
9245 * before the intersection is computed.
9247 __isl_give isl_map *isl_map_intersect_range_wrapped_domain(
9248 __isl_take isl_map *map, __isl_take isl_set *domain)
9250 isl_space *space;
9251 isl_set *factor;
9253 isl_map_align_params_set(&map, &domain);
9254 space = isl_map_get_space(map);
9255 space = isl_space_range_wrapped_range(space);
9256 factor = isl_set_universe(space);
9257 domain = isl_set_product(domain, factor);
9258 space = isl_map_peek_space(map);
9259 domain = isl_set_copy_tuple_id(domain, isl_dim_set, space, isl_dim_out);
9260 return isl_map_intersect_range(map, domain);
9263 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
9264 __isl_take isl_map *map2)
9266 if (isl_map_align_params_bin(&map1, &map2) < 0)
9267 goto error;
9268 map1 = isl_map_reverse(map1);
9269 map1 = isl_map_apply_range(map1, map2);
9270 return isl_map_reverse(map1);
9271 error:
9272 isl_map_free(map1);
9273 isl_map_free(map2);
9274 return NULL;
9277 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
9278 __isl_take isl_map *map2)
9280 isl_space *space;
9281 struct isl_map *result;
9282 int i, j;
9284 if (isl_map_align_params_bin(&map1, &map2) < 0)
9285 goto error;
9287 space = isl_space_join(isl_space_copy(map1->dim),
9288 isl_space_copy(map2->dim));
9290 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
9291 if (!result)
9292 goto error;
9293 for (i = 0; i < map1->n; ++i)
9294 for (j = 0; j < map2->n; ++j) {
9295 result = isl_map_add_basic_map(result,
9296 isl_basic_map_apply_range(
9297 isl_basic_map_copy(map1->p[i]),
9298 isl_basic_map_copy(map2->p[j])));
9299 if (!result)
9300 goto error;
9302 isl_map_free(map1);
9303 isl_map_free(map2);
9304 if (result && result->n <= 1)
9305 ISL_F_SET(result, ISL_MAP_DISJOINT);
9306 return result;
9307 error:
9308 isl_map_free(map1);
9309 isl_map_free(map2);
9310 return NULL;
9313 /* Is "bmap" a transformation, i.e.,
9314 * does it relate elements from the same space.
9316 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
9318 isl_space *space;
9320 space = isl_basic_map_peek_space(bmap);
9321 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
9324 /* Check that "bmap" is a transformation, i.e.,
9325 * that it relates elements from the same space.
9327 static isl_stat isl_basic_map_check_transformation(
9328 __isl_keep isl_basic_map *bmap)
9330 isl_bool equal;
9332 equal = isl_basic_map_is_transformation(bmap);
9333 if (equal < 0)
9334 return isl_stat_error;
9335 if (!equal)
9336 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9337 "domain and range don't match", return isl_stat_error);
9338 return isl_stat_ok;
9342 * returns range - domain
9344 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
9346 isl_space *target_space;
9347 struct isl_basic_set *bset;
9348 isl_size dim;
9349 isl_size nparam;
9350 isl_size total;
9351 int i;
9353 if (isl_basic_map_check_transformation(bmap) < 0)
9354 return isl_basic_map_free(bmap);
9355 dim = isl_basic_map_dim(bmap, isl_dim_in);
9356 nparam = isl_basic_map_dim(bmap, isl_dim_param);
9357 if (dim < 0 || nparam < 0)
9358 goto error;
9359 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
9360 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
9361 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
9362 total = isl_basic_map_dim(bmap, isl_dim_all);
9363 if (total < 0)
9364 bmap = isl_basic_map_free(bmap);
9365 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
9366 for (i = 0; i < dim; ++i) {
9367 int j = isl_basic_map_alloc_equality(bmap);
9368 if (j < 0) {
9369 bmap = isl_basic_map_free(bmap);
9370 break;
9372 isl_seq_clr(bmap->eq[j], 1 + total);
9373 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
9374 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
9375 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
9377 bset = isl_basic_map_domain(bmap);
9378 bset = isl_basic_set_reset_space(bset, target_space);
9379 return bset;
9380 error:
9381 isl_basic_map_free(bmap);
9382 return NULL;
9385 /* Is the tuple of type "type1" of "map" the same as
9386 * the tuple of type "type2" of "space"?
9388 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
9389 enum isl_dim_type type1, __isl_keep isl_space *space,
9390 enum isl_dim_type type2)
9392 isl_space *map_space;
9394 map_space = isl_map_peek_space(map);
9395 return isl_space_tuple_is_equal(map_space, type1, space, type2);
9398 /* Is the tuple of type "type1" of "map1" the same as
9399 * the tuple of type "type2" of "map2"?
9401 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
9402 enum isl_dim_type type1, __isl_keep isl_map *map2,
9403 enum isl_dim_type type2)
9405 isl_space *space1, *space2;
9407 space1 = isl_map_peek_space(map1);
9408 space2 = isl_map_peek_space(map2);
9409 return isl_space_tuple_is_equal(space1, type1, space2, type2);
9412 /* Is the space of "obj" equal to "space", ignoring parameters?
9414 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
9415 __isl_keep isl_space *space)
9417 isl_space *map_space;
9419 map_space = isl_map_peek_space(map);
9420 return isl_space_has_equal_tuples(map_space, space);
9423 /* Check that "map" is a transformation, i.e.,
9424 * that it relates elements from the same space.
9426 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
9428 isl_bool equal;
9430 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
9431 if (equal < 0)
9432 return isl_stat_error;
9433 if (!equal)
9434 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9435 "domain and range don't match", return isl_stat_error);
9436 return isl_stat_ok;
9440 * returns range - domain
9442 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
9444 int i;
9445 isl_space *space;
9446 struct isl_set *result;
9448 if (isl_map_check_transformation(map) < 0)
9449 goto error;
9450 space = isl_map_get_space(map);
9451 space = isl_space_domain(space);
9452 result = isl_set_alloc_space(space, map->n, 0);
9453 if (!result)
9454 goto error;
9455 for (i = 0; i < map->n; ++i)
9456 result = isl_set_add_basic_set(result,
9457 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
9458 isl_map_free(map);
9459 return result;
9460 error:
9461 isl_map_free(map);
9462 return NULL;
9466 * returns [domain -> range] -> range - domain
9468 __isl_give isl_basic_map *isl_basic_map_deltas_map(
9469 __isl_take isl_basic_map *bmap)
9471 int i, k;
9472 isl_space *space;
9473 isl_basic_map *domain;
9474 isl_size nparam, n;
9475 isl_size total;
9477 if (isl_basic_map_check_transformation(bmap) < 0)
9478 return isl_basic_map_free(bmap);
9480 nparam = isl_basic_map_dim(bmap, isl_dim_param);
9481 n = isl_basic_map_dim(bmap, isl_dim_in);
9482 if (nparam < 0 || n < 0)
9483 return isl_basic_map_free(bmap);
9485 space = isl_basic_map_get_space(bmap);
9486 space = isl_space_from_range(isl_space_domain(space));
9487 domain = isl_basic_map_universe(space);
9489 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
9490 bmap = isl_basic_map_apply_range(bmap, domain);
9491 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
9493 total = isl_basic_map_dim(bmap, isl_dim_all);
9494 if (total < 0)
9495 return isl_basic_map_free(bmap);
9497 for (i = 0; i < n; ++i) {
9498 k = isl_basic_map_alloc_equality(bmap);
9499 if (k < 0)
9500 goto error;
9501 isl_seq_clr(bmap->eq[k], 1 + total);
9502 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
9503 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
9504 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
9507 bmap = isl_basic_map_gauss(bmap, NULL);
9508 return isl_basic_map_finalize(bmap);
9509 error:
9510 isl_basic_map_free(bmap);
9511 return NULL;
9515 * returns [domain -> range] -> range - domain
9517 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
9519 if (isl_map_check_transformation(map) < 0)
9520 return isl_map_free(map);
9522 return isl_map_transform(map, &isl_space_range_map,
9523 &isl_basic_map_deltas_map);
9526 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9528 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
9530 isl_space *space;
9531 isl_map *map;
9533 space = isl_space_map_from_set(isl_set_get_space(deltas));
9534 map = isl_map_deltas_map(isl_map_universe(space));
9535 map = isl_map_intersect_range(map, deltas);
9537 return isl_set_unwrap(isl_map_domain(map));
9540 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
9542 isl_size n_in, n_out;
9544 n_in = isl_space_dim(space, isl_dim_in);
9545 n_out = isl_space_dim(space, isl_dim_out);
9546 if (n_in < 0 || n_out < 0)
9547 goto error;
9548 if (n_in != n_out)
9549 isl_die(space->ctx, isl_error_invalid,
9550 "number of input and output dimensions needs to be "
9551 "the same", goto error);
9552 return isl_basic_map_equal(space, n_in);
9553 error:
9554 isl_space_free(space);
9555 return NULL;
9558 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
9560 return isl_map_from_basic_map(isl_basic_map_identity(space));
9563 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
9565 isl_space *space = isl_set_get_space(set);
9566 isl_map *id;
9567 id = isl_map_identity(isl_space_map_from_set(space));
9568 return isl_map_intersect_range(id, set);
9571 /* Construct a basic set with all set dimensions having only non-negative
9572 * values.
9574 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
9575 __isl_take isl_space *space)
9577 int i;
9578 isl_size nparam;
9579 isl_size dim;
9580 isl_size total;
9581 struct isl_basic_set *bset;
9583 nparam = isl_space_dim(space, isl_dim_param);
9584 dim = isl_space_dim(space, isl_dim_set);
9585 total = isl_space_dim(space, isl_dim_all);
9586 if (nparam < 0 || dim < 0 || total < 0)
9587 space = isl_space_free(space);
9588 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
9589 if (!bset)
9590 return NULL;
9591 for (i = 0; i < dim; ++i) {
9592 int k = isl_basic_set_alloc_inequality(bset);
9593 if (k < 0)
9594 goto error;
9595 isl_seq_clr(bset->ineq[k], 1 + total);
9596 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
9598 return bset;
9599 error:
9600 isl_basic_set_free(bset);
9601 return NULL;
9604 /* Construct the half-space x_pos >= 0.
9606 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
9607 int pos)
9609 int k;
9610 isl_size total;
9611 isl_basic_set *nonneg;
9613 total = isl_space_dim(space, isl_dim_all);
9614 if (total < 0)
9615 space = isl_space_free(space);
9616 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
9617 k = isl_basic_set_alloc_inequality(nonneg);
9618 if (k < 0)
9619 goto error;
9620 isl_seq_clr(nonneg->ineq[k], 1 + total);
9621 isl_int_set_si(nonneg->ineq[k][pos], 1);
9623 return isl_basic_set_finalize(nonneg);
9624 error:
9625 isl_basic_set_free(nonneg);
9626 return NULL;
9629 /* Construct the half-space x_pos <= -1.
9631 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
9632 int pos)
9634 int k;
9635 isl_size total;
9636 isl_basic_set *neg;
9638 total = isl_space_dim(space, isl_dim_all);
9639 if (total < 0)
9640 space = isl_space_free(space);
9641 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
9642 k = isl_basic_set_alloc_inequality(neg);
9643 if (k < 0)
9644 goto error;
9645 isl_seq_clr(neg->ineq[k], 1 + total);
9646 isl_int_set_si(neg->ineq[k][0], -1);
9647 isl_int_set_si(neg->ineq[k][pos], -1);
9649 return isl_basic_set_finalize(neg);
9650 error:
9651 isl_basic_set_free(neg);
9652 return NULL;
9655 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
9656 enum isl_dim_type type, unsigned first, unsigned n)
9658 int i;
9659 unsigned offset;
9660 isl_basic_set *nonneg;
9661 isl_basic_set *neg;
9663 if (n == 0)
9664 return set;
9666 if (isl_set_check_range(set, type, first, n) < 0)
9667 return isl_set_free(set);
9669 offset = pos(set->dim, type);
9670 for (i = 0; i < n; ++i) {
9671 nonneg = nonneg_halfspace(isl_set_get_space(set),
9672 offset + first + i);
9673 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
9675 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
9678 return set;
9681 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
9682 int len,
9683 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9684 void *user)
9686 isl_set *half;
9688 if (!set)
9689 return isl_stat_error;
9690 if (isl_set_plain_is_empty(set)) {
9691 isl_set_free(set);
9692 return isl_stat_ok;
9694 if (first == len)
9695 return fn(set, signs, user);
9697 signs[first] = 1;
9698 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
9699 1 + first));
9700 half = isl_set_intersect(half, isl_set_copy(set));
9701 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
9702 goto error;
9704 signs[first] = -1;
9705 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
9706 1 + first));
9707 half = isl_set_intersect(half, set);
9708 return foreach_orthant(half, signs, first + 1, len, fn, user);
9709 error:
9710 isl_set_free(set);
9711 return isl_stat_error;
9714 /* Call "fn" on the intersections of "set" with each of the orthants
9715 * (except for obviously empty intersections). The orthant is identified
9716 * by the signs array, with each entry having value 1 or -1 according
9717 * to the sign of the corresponding variable.
9719 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
9720 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
9721 void *user)
9723 isl_size nparam;
9724 isl_size nvar;
9725 int *signs;
9726 isl_stat r;
9728 if (!set)
9729 return isl_stat_error;
9730 if (isl_set_plain_is_empty(set))
9731 return isl_stat_ok;
9733 nparam = isl_set_dim(set, isl_dim_param);
9734 nvar = isl_set_dim(set, isl_dim_set);
9735 if (nparam < 0 || nvar < 0)
9736 return isl_stat_error;
9738 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9740 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9741 fn, user);
9743 free(signs);
9745 return r;
9748 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9750 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9753 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9754 __isl_keep isl_basic_map *bmap2)
9756 isl_bool is_subset;
9757 struct isl_map *map1;
9758 struct isl_map *map2;
9760 if (!bmap1 || !bmap2)
9761 return isl_bool_error;
9763 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9764 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9766 is_subset = isl_map_is_subset(map1, map2);
9768 isl_map_free(map1);
9769 isl_map_free(map2);
9771 return is_subset;
9774 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9775 __isl_keep isl_basic_set *bset2)
9777 return isl_basic_map_is_subset(bset1, bset2);
9780 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9781 __isl_keep isl_basic_map *bmap2)
9783 isl_bool is_subset;
9785 if (!bmap1 || !bmap2)
9786 return isl_bool_error;
9787 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9788 if (is_subset != isl_bool_true)
9789 return is_subset;
9790 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9791 return is_subset;
9794 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9795 __isl_keep isl_basic_set *bset2)
9797 return isl_basic_map_is_equal(
9798 bset_to_bmap(bset1), bset_to_bmap(bset2));
9801 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9803 int i;
9804 int is_empty;
9806 if (!map)
9807 return isl_bool_error;
9808 for (i = 0; i < map->n; ++i) {
9809 is_empty = isl_basic_map_is_empty(map->p[i]);
9810 if (is_empty < 0)
9811 return isl_bool_error;
9812 if (!is_empty)
9813 return isl_bool_false;
9815 return isl_bool_true;
9818 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9820 return map ? map->n == 0 : isl_bool_error;
9823 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9825 return set ? set->n == 0 : isl_bool_error;
9828 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9830 return isl_map_is_empty(set_to_map(set));
9833 #undef TYPE
9834 #define TYPE isl_basic_map
9836 static
9837 #include "isl_type_has_equal_space_bin_templ.c"
9838 #include "isl_type_check_equal_space_templ.c"
9840 /* Check that "bset1" and "bset2" live in the same space,
9841 * reporting an error if they do not.
9843 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9844 __isl_keep isl_basic_set *bset2)
9846 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9847 bset_to_bmap(bset1));
9850 #undef TYPE
9851 #define TYPE isl_map
9853 #include "isl_type_has_equal_space_bin_templ.c"
9854 #include "isl_type_check_equal_space_templ.c"
9855 #include "isl_type_has_space_templ.c"
9857 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9858 __isl_keep isl_set *set2)
9860 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9863 #undef TYPE1
9864 #define TYPE1 isl_map
9865 #undef TYPE2
9866 #define TYPE2 isl_basic_map
9867 #undef TYPE_PAIR
9868 #define TYPE_PAIR isl_map_basic_map
9870 static
9871 #include "isl_type_has_equal_space_templ.c"
9872 #include "isl_type_check_equal_space_templ.c"
9874 /* Check that "set" and "bset" live in the same space,
9875 * reporting an error if they do not.
9877 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9878 __isl_keep isl_basic_set *bset)
9880 return isl_map_basic_map_check_equal_space(set_to_map(set),
9881 bset_to_bmap(bset));
9884 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9886 isl_bool is_subset;
9888 if (!map1 || !map2)
9889 return isl_bool_error;
9890 is_subset = isl_map_is_subset(map1, map2);
9891 if (is_subset != isl_bool_true)
9892 return is_subset;
9893 is_subset = isl_map_is_subset(map2, map1);
9894 return is_subset;
9897 /* Is "map1" equal to "map2"?
9899 * First check if they are obviously equal.
9900 * If not, then perform a more detailed analysis.
9902 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9904 isl_bool equal;
9906 equal = isl_map_plain_is_equal(map1, map2);
9907 if (equal < 0 || equal)
9908 return equal;
9909 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9912 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9913 __isl_keep isl_basic_map *bmap2)
9915 isl_bool is_subset;
9917 if (!bmap1 || !bmap2)
9918 return isl_bool_error;
9919 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9920 if (is_subset != isl_bool_true)
9921 return is_subset;
9922 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9923 return isl_bool_not(is_subset);
9926 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9927 __isl_keep isl_map *map2)
9929 isl_bool is_subset;
9931 if (!map1 || !map2)
9932 return isl_bool_error;
9933 is_subset = isl_map_is_subset(map1, map2);
9934 if (is_subset != isl_bool_true)
9935 return is_subset;
9936 is_subset = isl_map_is_subset(map2, map1);
9937 return isl_bool_not(is_subset);
9940 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9941 __isl_keep isl_set *set2)
9943 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9946 /* Is "bmap" obviously equal to the universe with the same space?
9948 * That is, does it not have any constraints?
9950 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9952 if (!bmap)
9953 return isl_bool_error;
9954 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9957 /* Is "bset" obviously equal to the universe with the same space?
9959 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9961 return isl_basic_map_plain_is_universe(bset);
9964 /* If "c" does not involve any existentially quantified variables,
9965 * then set *univ to false and abort
9967 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9969 isl_bool *univ = user;
9970 isl_size n;
9972 n = isl_constraint_dim(c, isl_dim_div);
9973 if (n < 0)
9974 c = isl_constraint_free(c);
9975 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9976 isl_constraint_free(c);
9977 if (*univ < 0 || !*univ)
9978 return isl_stat_error;
9979 return isl_stat_ok;
9982 /* Is "bmap" equal to the universe with the same space?
9984 * First check if it is obviously equal to the universe.
9985 * If not and if there are any constraints not involving
9986 * existentially quantified variables, then it is certainly
9987 * not equal to the universe.
9988 * Otherwise, check if the universe is a subset of "bmap".
9990 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9992 isl_size n_div;
9993 isl_bool univ;
9994 isl_basic_map *test;
9996 univ = isl_basic_map_plain_is_universe(bmap);
9997 if (univ < 0 || univ)
9998 return univ;
9999 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10000 if (n_div < 0)
10001 return isl_bool_error;
10002 if (n_div == 0)
10003 return isl_bool_false;
10004 univ = isl_bool_true;
10005 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
10006 univ)
10007 return isl_bool_error;
10008 if (univ < 0 || !univ)
10009 return univ;
10010 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
10011 univ = isl_basic_map_is_subset(test, bmap);
10012 isl_basic_map_free(test);
10013 return univ;
10016 /* Is "bset" equal to the universe with the same space?
10018 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
10020 return isl_basic_map_is_universe(bset);
10023 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
10025 int i;
10027 if (!map)
10028 return isl_bool_error;
10030 for (i = 0; i < map->n; ++i) {
10031 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
10032 if (r < 0 || r)
10033 return r;
10036 return isl_bool_false;
10039 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
10041 return isl_map_plain_is_universe(set_to_map(set));
10044 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
10046 struct isl_basic_set *bset = NULL;
10047 struct isl_vec *sample = NULL;
10048 isl_bool empty, non_empty;
10050 if (!bmap)
10051 return isl_bool_error;
10053 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
10054 return isl_bool_true;
10056 if (isl_basic_map_plain_is_universe(bmap))
10057 return isl_bool_false;
10059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
10060 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
10061 copy = isl_basic_map_remove_redundancies(copy);
10062 empty = isl_basic_map_plain_is_empty(copy);
10063 isl_basic_map_free(copy);
10064 return empty;
10067 non_empty = isl_basic_map_plain_is_non_empty(bmap);
10068 if (non_empty < 0)
10069 return isl_bool_error;
10070 if (non_empty)
10071 return isl_bool_false;
10072 isl_vec_free(bmap->sample);
10073 bmap->sample = NULL;
10074 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
10075 if (!bset)
10076 return isl_bool_error;
10077 sample = isl_basic_set_sample_vec(bset);
10078 if (!sample)
10079 return isl_bool_error;
10080 empty = sample->size == 0;
10081 isl_vec_free(bmap->sample);
10082 bmap->sample = sample;
10083 if (empty)
10084 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
10086 return empty;
10089 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
10091 if (!bmap)
10092 return isl_bool_error;
10093 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
10096 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
10098 if (!bset)
10099 return isl_bool_error;
10100 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
10103 /* Is "bmap" known to be non-empty?
10105 * That is, is the cached sample still valid?
10107 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
10109 isl_size total;
10111 if (!bmap)
10112 return isl_bool_error;
10113 if (!bmap->sample)
10114 return isl_bool_false;
10115 total = isl_basic_map_dim(bmap, isl_dim_all);
10116 if (total < 0)
10117 return isl_bool_error;
10118 if (bmap->sample->size != 1 + total)
10119 return isl_bool_false;
10120 return isl_basic_map_contains(bmap, bmap->sample);
10123 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
10125 return isl_basic_map_is_empty(bset_to_bmap(bset));
10128 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
10129 __isl_take isl_basic_map *bmap2)
10131 struct isl_map *map;
10133 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
10134 goto error;
10136 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
10137 if (!map)
10138 goto error;
10139 map = isl_map_add_basic_map(map, bmap1);
10140 map = isl_map_add_basic_map(map, bmap2);
10141 return map;
10142 error:
10143 isl_basic_map_free(bmap1);
10144 isl_basic_map_free(bmap2);
10145 return NULL;
10148 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
10149 __isl_take isl_basic_set *bset2)
10151 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
10152 bset_to_bmap(bset2)));
10155 /* Order divs such that any div only depends on previous divs */
10156 __isl_give isl_basic_map *isl_basic_map_order_divs(
10157 __isl_take isl_basic_map *bmap)
10159 int i;
10160 isl_size off;
10162 off = isl_basic_map_var_offset(bmap, isl_dim_div);
10163 if (off < 0)
10164 return isl_basic_map_free(bmap);
10166 for (i = 0; i < bmap->n_div; ++i) {
10167 int pos;
10168 if (isl_int_is_zero(bmap->div[i][0]))
10169 continue;
10170 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
10171 bmap->n_div-i);
10172 if (pos == -1)
10173 continue;
10174 if (pos == 0)
10175 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
10176 "integer division depends on itself",
10177 return isl_basic_map_free(bmap));
10178 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
10179 if (!bmap)
10180 return NULL;
10181 --i;
10183 return bmap;
10186 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
10188 int i;
10190 if (!map)
10191 return 0;
10193 for (i = 0; i < map->n; ++i) {
10194 map->p[i] = isl_basic_map_order_divs(map->p[i]);
10195 if (!map->p[i])
10196 goto error;
10199 return map;
10200 error:
10201 isl_map_free(map);
10202 return NULL;
10205 /* Sort the local variables of "bset".
10207 __isl_give isl_basic_set *isl_basic_set_sort_divs(
10208 __isl_take isl_basic_set *bset)
10210 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
10213 /* Apply the expansion computed by isl_merge_divs.
10214 * The expansion itself is given by "exp" while the resulting
10215 * list of divs is given by "div".
10217 * Move the integer divisions of "bmap" into the right position
10218 * according to "exp" and then introduce the additional integer
10219 * divisions, adding div constraints.
10220 * The moving should be done first to avoid moving coefficients
10221 * in the definitions of the extra integer divisions.
10223 __isl_give isl_basic_map *isl_basic_map_expand_divs(
10224 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
10226 int i, j;
10227 int n_div;
10229 bmap = isl_basic_map_cow(bmap);
10230 if (!bmap || !div)
10231 goto error;
10233 if (div->n_row < bmap->n_div)
10234 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
10235 "not an expansion", goto error);
10237 n_div = bmap->n_div;
10238 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
10239 2 * (div->n_row - n_div));
10241 for (i = n_div; i < div->n_row; ++i)
10242 if (isl_basic_map_alloc_div(bmap) < 0)
10243 goto error;
10245 for (j = n_div - 1; j >= 0; --j) {
10246 if (exp[j] == j)
10247 break;
10248 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
10249 if (!bmap)
10250 goto error;
10252 j = 0;
10253 for (i = 0; i < div->n_row; ++i) {
10254 if (j < n_div && exp[j] == i) {
10255 j++;
10256 } else {
10257 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
10258 if (isl_basic_map_div_is_marked_unknown(bmap, i))
10259 continue;
10260 bmap = isl_basic_map_add_div_constraints(bmap, i);
10261 if (!bmap)
10262 goto error;
10266 isl_mat_free(div);
10267 return bmap;
10268 error:
10269 isl_basic_map_free(bmap);
10270 isl_mat_free(div);
10271 return NULL;
10274 /* Apply the expansion computed by isl_merge_divs.
10275 * The expansion itself is given by "exp" while the resulting
10276 * list of divs is given by "div".
10278 __isl_give isl_basic_set *isl_basic_set_expand_divs(
10279 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
10281 return isl_basic_map_expand_divs(bset, div, exp);
10284 /* Look for a div in dst that corresponds to the div "div" in src.
10285 * The divs before "div" in src and dst are assumed to be the same.
10287 * Return the position of the corresponding div in dst
10288 * if there is one. Otherwise, return a position beyond the integer divisions.
10289 * Return isl_size_error on error.
10291 static isl_size find_div(__isl_keep isl_basic_map *dst,
10292 __isl_keep isl_basic_map *src, unsigned div)
10294 int i;
10295 isl_size n_div;
10296 isl_size v_div;
10298 v_div = isl_basic_map_var_offset(src, isl_dim_div);
10299 n_div = isl_basic_map_dim(dst, isl_dim_div);
10300 if (n_div < 0 || v_div < 0)
10301 return isl_size_error;
10302 isl_assert(dst->ctx, div <= n_div, return isl_size_error);
10303 for (i = div; i < n_div; ++i)
10304 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
10305 !isl_seq_any_non_zero(dst->div[i] + 1 + 1 + v_div + div,
10306 n_div - div))
10307 return i;
10308 return n_div;
10311 /* Align the local variables of "dst" to those of "src",
10312 * adding local variables from "src" if needed.
10313 * That is, make sure that the first src->n_div local variables
10314 * of the result correspond to those of src.
10315 * For any integer division that is copied to "dst",
10316 * the defining constraints are also introduced to "dst".
10317 * For an existentially quantified variable (without an explicit definition)
10318 * only an unconstrained existentially quantified variable
10319 * in the same positions is introduced.
10320 * The integer division of "src" are assumed to be ordered.
10322 * The integer divisions are swapped into the right position
10323 * (possibly after adding them first). This may result
10324 * in the remaining integer divisions appearing in the wrong order,
10325 * i.e., with some integer division appearing before
10326 * some other integer division on which it depends.
10327 * The integer divisions therefore need to be ordered.
10328 * This will not affect the integer divisions aligned to those of "src",
10329 * since "src" is assumed to have ordered integer divisions.
10331 * The result is not finalized as by design it will have redundant
10332 * divs if any divs from "src" were copied.
10334 __isl_give isl_basic_map *isl_basic_map_align_divs(
10335 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
10337 int i;
10338 int extended;
10339 isl_size v_div;
10340 isl_size dst_n_div, src_n_div;
10342 src_n_div = isl_basic_map_dim(src, isl_dim_div);
10343 if (!dst || src_n_div < 0)
10344 return isl_basic_map_free(dst);
10346 if (src_n_div == 0)
10347 return dst;
10349 v_div = isl_basic_map_var_offset(src, isl_dim_div);
10350 if (v_div < 0)
10351 return isl_basic_map_free(dst);
10353 extended = 0;
10354 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
10355 if (dst_n_div < 0)
10356 dst = isl_basic_map_free(dst);
10357 for (i = 0; i < src_n_div; ++i) {
10358 isl_bool known;
10359 isl_size j;
10361 known = isl_basic_map_div_is_known(src, i);
10362 if (known < 0)
10363 return isl_basic_map_free(dst);
10364 j = known ? find_div(dst, src, i) : dst_n_div;
10365 if (j < 0)
10366 dst = isl_basic_map_free(dst);
10367 if (j == dst_n_div) {
10368 if (!extended) {
10369 int extra = src_n_div - i;
10370 dst = isl_basic_map_cow(dst);
10371 if (!dst)
10372 return isl_basic_map_free(dst);
10373 dst = isl_basic_map_extend(dst,
10374 extra, 0, 2 * extra);
10375 extended = 1;
10377 j = isl_basic_map_alloc_div(dst);
10378 if (j < 0)
10379 return isl_basic_map_free(dst);
10380 dst_n_div++;
10381 if (!known)
10382 continue;
10383 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
10384 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst_n_div - i);
10385 dst = isl_basic_map_add_div_constraints(dst, j);
10386 if (!dst)
10387 return isl_basic_map_free(dst);
10389 if (j != i)
10390 dst = isl_basic_map_swap_div(dst, i, j);
10391 if (!dst)
10392 return isl_basic_map_free(dst);
10394 return isl_basic_map_order_divs(dst);
10397 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
10399 int i;
10401 if (!map)
10402 return NULL;
10403 if (map->n == 0)
10404 return map;
10405 map = isl_map_compute_divs(map);
10406 map = isl_map_order_divs(map);
10407 map = isl_map_cow(map);
10408 if (!map)
10409 return NULL;
10411 for (i = 1; i < map->n; ++i)
10412 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
10413 for (i = 1; i < map->n; ++i) {
10414 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
10415 if (!map->p[i])
10416 return isl_map_free(map);
10419 map = isl_map_unmark_normalized(map);
10420 return map;
10423 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
10425 return isl_map_align_divs_internal(map);
10428 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
10430 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10433 /* Align the divs of the basic maps in "map" to those
10434 * of the basic maps in "list", as well as to the other basic maps in "map".
10435 * The elements in "list" are assumed to have known divs.
10437 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
10438 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
10440 int i;
10441 isl_size n;
10443 n = isl_basic_map_list_n_basic_map(list);
10444 map = isl_map_compute_divs(map);
10445 map = isl_map_cow(map);
10446 if (!map || n < 0)
10447 return isl_map_free(map);
10448 if (map->n == 0)
10449 return map;
10451 for (i = 0; i < n; ++i) {
10452 isl_basic_map *bmap;
10454 bmap = isl_basic_map_list_get_basic_map(list, i);
10455 bmap = isl_basic_map_order_divs(bmap);
10456 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
10457 isl_basic_map_free(bmap);
10459 if (!map->p[0])
10460 return isl_map_free(map);
10462 return isl_map_align_divs_internal(map);
10465 /* Align the divs of each element of "list" to those of "bmap".
10466 * Both "bmap" and the elements of "list" are assumed to have known divs.
10468 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
10469 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
10471 int i;
10472 isl_size n;
10474 n = isl_basic_map_list_n_basic_map(list);
10475 if (n < 0 || !bmap)
10476 return isl_basic_map_list_free(list);
10478 for (i = 0; i < n; ++i) {
10479 isl_basic_map *bmap_i;
10481 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10482 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
10483 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
10486 return list;
10489 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
10490 __isl_take isl_map *map)
10492 isl_bool ok;
10494 isl_map_align_params_set(&map, &set);
10495 ok = isl_map_compatible_domain(map, set);
10496 if (ok < 0)
10497 goto error;
10498 if (!ok)
10499 isl_die(isl_set_get_ctx(set), isl_error_invalid,
10500 "incompatible spaces", goto error);
10501 map = isl_map_intersect_domain(map, set);
10502 set = isl_map_range(map);
10503 return set;
10504 error:
10505 isl_set_free(set);
10506 isl_map_free(map);
10507 return NULL;
10510 /* There is no need to cow as removing empty parts doesn't change
10511 * the meaning of the set.
10513 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
10515 int i;
10517 if (!map)
10518 return NULL;
10520 for (i = map->n - 1; i >= 0; --i)
10521 map = remove_if_empty(map, i);
10523 return map;
10526 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
10528 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
10531 /* Create a binary relation that maps the shared initial "pos" dimensions
10532 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10534 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
10535 __isl_keep isl_basic_set *bset2, int pos)
10537 isl_basic_map *bmap1;
10538 isl_basic_map *bmap2;
10540 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
10541 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
10542 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
10543 isl_dim_out, 0, pos);
10544 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
10545 isl_dim_out, 0, pos);
10546 return isl_basic_map_range_product(bmap1, bmap2);
10549 /* Given two basic sets bset1 and bset2, compute the maximal difference
10550 * between the values of dimension pos in bset1 and those in bset2
10551 * for any common value of the parameters and dimensions preceding pos.
10553 static enum isl_lp_result basic_set_maximal_difference_at(
10554 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
10555 int pos, isl_int *opt)
10557 isl_basic_map *bmap1;
10558 struct isl_ctx *ctx;
10559 struct isl_vec *obj;
10560 isl_size total;
10561 isl_size nparam;
10562 isl_size dim1;
10563 enum isl_lp_result res;
10565 nparam = isl_basic_set_dim(bset1, isl_dim_param);
10566 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10567 if (nparam < 0 || dim1 < 0 || !bset2)
10568 return isl_lp_error;
10570 bmap1 = join_initial(bset1, bset2, pos);
10571 total = isl_basic_map_dim(bmap1, isl_dim_all);
10572 if (total < 0)
10573 return isl_lp_error;
10575 ctx = bmap1->ctx;
10576 obj = isl_vec_alloc(ctx, 1 + total);
10577 if (!obj)
10578 goto error;
10579 isl_seq_clr(obj->block.data, 1 + total);
10580 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
10581 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
10582 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
10583 opt, NULL, NULL);
10584 isl_basic_map_free(bmap1);
10585 isl_vec_free(obj);
10586 return res;
10587 error:
10588 isl_basic_map_free(bmap1);
10589 return isl_lp_error;
10592 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10593 * for any common value of the parameters and dimensions preceding pos
10594 * in both basic sets, the values of dimension pos in bset1 are
10595 * smaller or larger than those in bset2.
10597 * Returns
10598 * 1 if bset1 follows bset2
10599 * -1 if bset1 precedes bset2
10600 * 0 if bset1 and bset2 are incomparable
10601 * -2 if some error occurred.
10603 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
10604 __isl_keep isl_basic_set *bset2, int pos)
10606 isl_int opt;
10607 enum isl_lp_result res;
10608 int cmp;
10610 isl_int_init(opt);
10612 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
10614 if (res == isl_lp_empty)
10615 cmp = 0;
10616 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
10617 res == isl_lp_unbounded)
10618 cmp = 1;
10619 else if (res == isl_lp_ok && isl_int_is_neg(opt))
10620 cmp = -1;
10621 else
10622 cmp = -2;
10624 isl_int_clear(opt);
10625 return cmp;
10628 /* Given two basic sets bset1 and bset2, check whether
10629 * for any common value of the parameters and dimensions preceding pos
10630 * there is a value of dimension pos in bset1 that is larger
10631 * than a value of the same dimension in bset2.
10633 * Return
10634 * 1 if there exists such a pair
10635 * 0 if there is no such pair, but there is a pair of equal values
10636 * -1 otherwise
10637 * -2 if some error occurred.
10639 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
10640 __isl_keep isl_basic_set *bset2, int pos)
10642 isl_bool empty;
10643 isl_basic_map *bmap;
10644 isl_size dim1;
10646 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
10647 if (dim1 < 0)
10648 return -2;
10649 bmap = join_initial(bset1, bset2, pos);
10650 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
10651 isl_dim_out, dim1 - pos);
10652 empty = isl_basic_map_is_empty(bmap);
10653 if (empty < 0)
10654 goto error;
10655 if (empty) {
10656 isl_basic_map_free(bmap);
10657 return -1;
10659 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
10660 isl_dim_out, dim1 - pos);
10661 empty = isl_basic_map_is_empty(bmap);
10662 if (empty < 0)
10663 goto error;
10664 isl_basic_map_free(bmap);
10665 if (empty)
10666 return 0;
10667 return 1;
10668 error:
10669 isl_basic_map_free(bmap);
10670 return -2;
10673 /* Given two sets set1 and set2, check whether
10674 * for any common value of the parameters and dimensions preceding pos
10675 * there is a value of dimension pos in set1 that is larger
10676 * than a value of the same dimension in set2.
10678 * Return
10679 * 1 if there exists such a pair
10680 * 0 if there is no such pair, but there is a pair of equal values
10681 * -1 otherwise
10682 * -2 if some error occurred.
10684 int isl_set_follows_at(__isl_keep isl_set *set1,
10685 __isl_keep isl_set *set2, int pos)
10687 int i, j;
10688 int follows = -1;
10690 if (!set1 || !set2)
10691 return -2;
10693 for (i = 0; i < set1->n; ++i)
10694 for (j = 0; j < set2->n; ++j) {
10695 int f;
10696 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
10697 if (f == 1 || f == -2)
10698 return f;
10699 if (f > follows)
10700 follows = f;
10703 return follows;
10706 static isl_bool isl_basic_map_plain_has_fixed_var(
10707 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
10709 int i;
10710 int d;
10711 isl_size total;
10713 total = isl_basic_map_dim(bmap, isl_dim_all);
10714 if (total < 0)
10715 return isl_bool_error;
10716 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
10717 for (; d+1 > pos; --d)
10718 if (!isl_int_is_zero(bmap->eq[i][1+d]))
10719 break;
10720 if (d != pos)
10721 continue;
10722 if (isl_seq_any_non_zero(bmap->eq[i]+1, d))
10723 return isl_bool_false;
10724 if (isl_seq_any_non_zero(bmap->eq[i]+1+d+1, total-d-1))
10725 return isl_bool_false;
10726 if (!isl_int_is_one(bmap->eq[i][1+d]))
10727 return isl_bool_false;
10728 if (val)
10729 isl_int_neg(*val, bmap->eq[i][0]);
10730 return isl_bool_true;
10732 return isl_bool_false;
10735 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10736 unsigned pos, isl_int *val)
10738 int i;
10739 isl_int v;
10740 isl_int tmp;
10741 isl_bool fixed;
10743 if (!map)
10744 return isl_bool_error;
10745 if (map->n == 0)
10746 return isl_bool_false;
10747 if (map->n == 1)
10748 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10749 isl_int_init(v);
10750 isl_int_init(tmp);
10751 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10752 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10753 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10754 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10755 fixed = isl_bool_false;
10757 if (val)
10758 isl_int_set(*val, v);
10759 isl_int_clear(tmp);
10760 isl_int_clear(v);
10761 return fixed;
10764 static isl_bool isl_basic_set_plain_has_fixed_var(
10765 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10767 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10768 pos, val);
10771 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10772 enum isl_dim_type type, unsigned pos, isl_int *val)
10774 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10775 return isl_bool_error;
10776 return isl_basic_map_plain_has_fixed_var(bmap,
10777 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10780 /* If "bmap" obviously lies on a hyperplane where the given dimension
10781 * has a fixed value, then return that value.
10782 * Otherwise return NaN.
10784 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10785 __isl_keep isl_basic_map *bmap,
10786 enum isl_dim_type type, unsigned pos)
10788 isl_ctx *ctx;
10789 isl_val *v;
10790 isl_bool fixed;
10792 if (!bmap)
10793 return NULL;
10794 ctx = isl_basic_map_get_ctx(bmap);
10795 v = isl_val_alloc(ctx);
10796 if (!v)
10797 return NULL;
10798 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10799 if (fixed < 0)
10800 return isl_val_free(v);
10801 if (fixed) {
10802 isl_int_set_si(v->d, 1);
10803 return v;
10805 isl_val_free(v);
10806 return isl_val_nan(ctx);
10809 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10810 enum isl_dim_type type, unsigned pos, isl_int *val)
10812 if (isl_map_check_range(map, type, pos, 1) < 0)
10813 return isl_bool_error;
10814 return isl_map_plain_has_fixed_var(map,
10815 map_offset(map, type) - 1 + pos, val);
10818 /* If "map" obviously lies on a hyperplane where the given dimension
10819 * has a fixed value, then return that value.
10820 * Otherwise return NaN.
10822 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10823 enum isl_dim_type type, unsigned pos)
10825 isl_ctx *ctx;
10826 isl_val *v;
10827 isl_bool fixed;
10829 if (!map)
10830 return NULL;
10831 ctx = isl_map_get_ctx(map);
10832 v = isl_val_alloc(ctx);
10833 if (!v)
10834 return NULL;
10835 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10836 if (fixed < 0)
10837 return isl_val_free(v);
10838 if (fixed) {
10839 isl_int_set_si(v->d, 1);
10840 return v;
10842 isl_val_free(v);
10843 return isl_val_nan(ctx);
10846 /* If "set" obviously lies on a hyperplane where the given dimension
10847 * has a fixed value, then return that value.
10848 * Otherwise return NaN.
10850 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10851 enum isl_dim_type type, unsigned pos)
10853 return isl_map_plain_get_val_if_fixed(set, type, pos);
10856 /* Return a sequence of values in the same space as "set"
10857 * that are equal to the corresponding set dimensions of "set"
10858 * for those set dimensions that obviously lie on a hyperplane
10859 * where the dimension has a fixed value.
10860 * The other elements are set to NaN.
10862 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10863 __isl_keep isl_set *set)
10865 int i;
10866 isl_size n;
10867 isl_space *space;
10868 isl_multi_val *mv;
10870 space = isl_space_drop_all_params(isl_set_get_space(set));
10871 mv = isl_multi_val_alloc(space);
10872 n = isl_multi_val_size(mv);
10873 if (n < 0)
10874 return isl_multi_val_free(mv);
10876 for (i = 0; i < n; ++i) {
10877 isl_val *v;
10879 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10880 mv = isl_multi_val_set_val(mv, i, v);
10883 return mv;
10886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10887 * then return this fixed value in *val.
10889 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10890 unsigned dim, isl_int *val)
10892 isl_size nparam;
10894 nparam = isl_basic_set_dim(bset, isl_dim_param);
10895 if (nparam < 0)
10896 return isl_bool_error;
10897 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10900 /* Return -1 if the constraint "c1" should be sorted before "c2"
10901 * and 1 if it should be sorted after "c2".
10902 * Return 0 if the two constraints are the same (up to the constant term).
10904 * In particular, if a constraint involves later variables than another
10905 * then it is sorted after this other constraint.
10906 * uset_gist depends on constraints without existentially quantified
10907 * variables sorting first.
10909 * For constraints that have the same latest variable, those
10910 * with the same coefficient for this latest variable (first in absolute value
10911 * and then in actual value) are grouped together.
10912 * This is useful for detecting pairs of constraints that can
10913 * be chained in their printed representation.
10915 * Finally, within a group, constraints are sorted according to
10916 * their coefficients (excluding the constant term).
10918 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10920 isl_int **c1 = (isl_int **) p1;
10921 isl_int **c2 = (isl_int **) p2;
10922 int l1, l2;
10923 unsigned size = *(unsigned *) arg;
10924 int cmp;
10926 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10927 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10929 if (l1 != l2)
10930 return l1 - l2;
10932 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10933 if (cmp != 0)
10934 return cmp;
10935 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10936 if (cmp != 0)
10937 return -cmp;
10939 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10942 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10943 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10944 * and 0 if the two constraints are the same (up to the constant term).
10946 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10947 isl_int *c1, isl_int *c2)
10949 isl_size total;
10950 unsigned size;
10952 total = isl_basic_map_dim(bmap, isl_dim_all);
10953 if (total < 0)
10954 return -2;
10955 size = total;
10956 return sort_constraint_cmp(&c1, &c2, &size);
10959 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10960 __isl_take isl_basic_map *bmap)
10962 isl_size total;
10963 unsigned size;
10965 if (!bmap)
10966 return NULL;
10967 if (bmap->n_ineq == 0)
10968 return bmap;
10969 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10970 return bmap;
10971 total = isl_basic_map_dim(bmap, isl_dim_all);
10972 if (total < 0)
10973 return isl_basic_map_free(bmap);
10974 size = total;
10975 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10976 &sort_constraint_cmp, &size) < 0)
10977 return isl_basic_map_free(bmap);
10978 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10979 return bmap;
10982 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10983 __isl_take isl_basic_set *bset)
10985 isl_basic_map *bmap = bset_to_bmap(bset);
10986 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10989 __isl_give isl_basic_map *isl_basic_map_normalize(
10990 __isl_take isl_basic_map *bmap)
10992 bmap = isl_basic_map_remove_redundancies(bmap);
10993 bmap = isl_basic_map_sort_constraints(bmap);
10994 return bmap;
10996 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10997 __isl_keep isl_basic_map *bmap2)
10999 int i, cmp;
11000 isl_size total;
11001 isl_space *space1, *space2;
11003 if (!bmap1 || !bmap2)
11004 return -1;
11006 if (bmap1 == bmap2)
11007 return 0;
11008 space1 = isl_basic_map_peek_space(bmap1);
11009 space2 = isl_basic_map_peek_space(bmap2);
11010 cmp = isl_space_cmp(space1, space2);
11011 if (cmp)
11012 return cmp;
11013 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
11014 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
11015 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
11016 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
11017 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11018 return 0;
11019 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
11020 return 1;
11021 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
11022 return -1;
11023 if (bmap1->n_eq != bmap2->n_eq)
11024 return bmap1->n_eq - bmap2->n_eq;
11025 if (bmap1->n_ineq != bmap2->n_ineq)
11026 return bmap1->n_ineq - bmap2->n_ineq;
11027 if (bmap1->n_div != bmap2->n_div)
11028 return bmap1->n_div - bmap2->n_div;
11029 total = isl_basic_map_dim(bmap1, isl_dim_all);
11030 if (total < 0)
11031 return -1;
11032 for (i = 0; i < bmap1->n_eq; ++i) {
11033 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
11034 if (cmp)
11035 return cmp;
11037 for (i = 0; i < bmap1->n_ineq; ++i) {
11038 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
11039 if (cmp)
11040 return cmp;
11042 for (i = 0; i < bmap1->n_div; ++i) {
11043 isl_bool unknown1, unknown2;
11045 unknown1 = isl_basic_map_div_is_marked_unknown(bmap1, i);
11046 unknown2 = isl_basic_map_div_is_marked_unknown(bmap2, i);
11047 if (unknown1 < 0 || unknown2 < 0)
11048 return -1;
11049 if (unknown1 && unknown2)
11050 continue;
11051 if (unknown1)
11052 return 1;
11053 if (unknown2)
11054 return -1;
11055 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
11056 if (cmp)
11057 return cmp;
11059 return 0;
11062 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
11063 __isl_keep isl_basic_set *bset2)
11065 return isl_basic_map_plain_cmp(bset1, bset2);
11068 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
11070 int i, cmp;
11072 if (set1 == set2)
11073 return 0;
11074 if (set1->n != set2->n)
11075 return set1->n - set2->n;
11077 for (i = 0; i < set1->n; ++i) {
11078 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
11079 if (cmp)
11080 return cmp;
11083 return 0;
11086 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
11087 __isl_keep isl_basic_map *bmap2)
11089 if (!bmap1 || !bmap2)
11090 return isl_bool_error;
11091 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
11094 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
11095 __isl_keep isl_basic_set *bset2)
11097 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
11098 bset_to_bmap(bset2));
11101 static int qsort_bmap_cmp(const void *p1, const void *p2)
11103 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
11104 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
11106 return isl_basic_map_plain_cmp(bmap1, bmap2);
11109 /* Sort the basic maps of "map" and remove duplicate basic maps.
11111 * While removing basic maps, we make sure that the basic maps remain
11112 * sorted because isl_map_normalize expects the basic maps of the result
11113 * to be sorted.
11115 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
11117 int i, j;
11119 map = isl_map_remove_empty_parts(map);
11120 if (!map)
11121 return NULL;
11122 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
11123 for (i = map->n - 1; i >= 1; --i) {
11124 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
11125 continue;
11126 isl_basic_map_free(map->p[i-1]);
11127 for (j = i; j < map->n; ++j)
11128 map->p[j - 1] = map->p[j];
11129 map->n--;
11132 return map;
11135 /* Remove obvious duplicates among the basic maps of "map".
11137 * Unlike isl_map_normalize, this function does not remove redundant
11138 * constraints and only removes duplicates that have exactly the same
11139 * constraints in the input. It does sort the constraints and
11140 * the basic maps to ease the detection of duplicates.
11142 * If "map" has already been normalized or if the basic maps are
11143 * disjoint, then there can be no duplicates.
11145 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
11147 int i;
11148 isl_basic_map *bmap;
11150 if (!map)
11151 return NULL;
11152 if (map->n <= 1)
11153 return map;
11154 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
11155 return map;
11156 for (i = 0; i < map->n; ++i) {
11157 bmap = isl_basic_map_copy(map->p[i]);
11158 bmap = isl_basic_map_sort_constraints(bmap);
11159 if (!bmap)
11160 return isl_map_free(map);
11161 isl_basic_map_free(map->p[i]);
11162 map->p[i] = bmap;
11165 map = sort_and_remove_duplicates(map);
11166 return map;
11169 /* We normalize in place, but if anything goes wrong we need
11170 * to return NULL, so we need to make sure we don't change the
11171 * meaning of any possible other copies of map.
11173 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
11175 int i;
11176 struct isl_basic_map *bmap;
11178 if (!map)
11179 return NULL;
11180 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
11181 return map;
11182 for (i = 0; i < map->n; ++i) {
11183 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
11184 if (!bmap)
11185 goto error;
11186 isl_basic_map_free(map->p[i]);
11187 map->p[i] = bmap;
11190 map = sort_and_remove_duplicates(map);
11191 if (map)
11192 ISL_F_SET(map, ISL_MAP_NORMALIZED);
11193 return map;
11194 error:
11195 isl_map_free(map);
11196 return NULL;
11199 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
11201 return set_from_map(isl_map_normalize(set_to_map(set)));
11204 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
11205 __isl_keep isl_map *map2)
11207 int i;
11208 isl_bool equal;
11210 if (!map1 || !map2)
11211 return isl_bool_error;
11213 if (map1 == map2)
11214 return isl_bool_true;
11215 equal = isl_map_has_equal_space(map1, map2);
11216 if (equal < 0 || !equal)
11217 return equal;
11219 map1 = isl_map_copy(map1);
11220 map2 = isl_map_copy(map2);
11221 map1 = isl_map_normalize(map1);
11222 map2 = isl_map_normalize(map2);
11223 if (!map1 || !map2)
11224 goto error;
11225 equal = map1->n == map2->n;
11226 for (i = 0; equal && i < map1->n; ++i) {
11227 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
11228 if (equal < 0)
11229 goto error;
11231 isl_map_free(map1);
11232 isl_map_free(map2);
11233 return equal;
11234 error:
11235 isl_map_free(map1);
11236 isl_map_free(map2);
11237 return isl_bool_error;
11240 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
11241 __isl_keep isl_set *set2)
11243 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
11246 /* Return the basic maps in "map" as a list.
11248 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
11249 __isl_keep isl_map *map)
11251 int i;
11252 isl_ctx *ctx;
11253 isl_basic_map_list *list;
11255 if (!map)
11256 return NULL;
11257 ctx = isl_map_get_ctx(map);
11258 list = isl_basic_map_list_alloc(ctx, map->n);
11260 for (i = 0; i < map->n; ++i) {
11261 isl_basic_map *bmap;
11263 bmap = isl_basic_map_copy(map->p[i]);
11264 list = isl_basic_map_list_add(list, bmap);
11267 return list;
11270 /* Return the intersection of the elements in the non-empty list "list".
11271 * All elements are assumed to live in the same space.
11273 __isl_give isl_basic_map *isl_basic_map_list_intersect(
11274 __isl_take isl_basic_map_list *list)
11276 int i;
11277 isl_size n;
11278 isl_basic_map *bmap;
11280 n = isl_basic_map_list_n_basic_map(list);
11281 if (n < 0)
11282 goto error;
11283 if (n < 1)
11284 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
11285 "expecting non-empty list", goto error);
11287 bmap = isl_basic_map_list_get_basic_map(list, 0);
11288 for (i = 1; i < n; ++i) {
11289 isl_basic_map *bmap_i;
11291 bmap_i = isl_basic_map_list_get_basic_map(list, i);
11292 bmap = isl_basic_map_intersect(bmap, bmap_i);
11295 isl_basic_map_list_free(list);
11296 return bmap;
11297 error:
11298 isl_basic_map_list_free(list);
11299 return NULL;
11302 /* Return the intersection of the elements in the non-empty list "list".
11303 * All elements are assumed to live in the same space.
11305 __isl_give isl_basic_set *isl_basic_set_list_intersect(
11306 __isl_take isl_basic_set_list *list)
11308 return isl_basic_map_list_intersect(list);
11311 /* Return the union of the elements of "list".
11312 * The list is required to have at least one element.
11314 __isl_give isl_set *isl_basic_set_list_union(
11315 __isl_take isl_basic_set_list *list)
11317 int i;
11318 isl_size n;
11319 isl_space *space;
11320 isl_basic_set *bset;
11321 isl_set *set;
11323 n = isl_basic_set_list_n_basic_set(list);
11324 if (n < 0)
11325 goto error;
11326 if (n < 1)
11327 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
11328 "expecting non-empty list", goto error);
11330 bset = isl_basic_set_list_get_basic_set(list, 0);
11331 space = isl_basic_set_get_space(bset);
11332 isl_basic_set_free(bset);
11334 set = isl_set_alloc_space(space, n, 0);
11335 for (i = 0; i < n; ++i) {
11336 bset = isl_basic_set_list_get_basic_set(list, i);
11337 set = isl_set_add_basic_set(set, bset);
11340 isl_basic_set_list_free(list);
11341 return set;
11342 error:
11343 isl_basic_set_list_free(list);
11344 return NULL;
11347 /* Return the union of the elements in the non-empty list "list".
11348 * All elements are assumed to live in the same space.
11350 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
11352 int i;
11353 isl_size n;
11354 isl_set *set;
11356 n = isl_set_list_n_set(list);
11357 if (n < 0)
11358 goto error;
11359 if (n < 1)
11360 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
11361 "expecting non-empty list", goto error);
11363 set = isl_set_list_get_set(list, 0);
11364 for (i = 1; i < n; ++i) {
11365 isl_set *set_i;
11367 set_i = isl_set_list_get_set(list, i);
11368 set = isl_set_union(set, set_i);
11371 isl_set_list_free(list);
11372 return set;
11373 error:
11374 isl_set_list_free(list);
11375 return NULL;
11378 __isl_give isl_basic_map *isl_basic_map_product(
11379 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11381 isl_space *space_result = NULL;
11382 struct isl_basic_map *bmap;
11383 unsigned in1, in2, out1, out2, nparam, total, pos;
11384 struct isl_dim_map *dim_map1, *dim_map2;
11386 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11387 goto error;
11388 space_result = isl_space_product(isl_space_copy(bmap1->dim),
11389 isl_space_copy(bmap2->dim));
11391 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11392 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11393 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11394 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11395 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11397 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
11398 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11399 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11400 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11401 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11402 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11403 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11404 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11405 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11406 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11407 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11409 bmap = isl_basic_map_alloc_space(space_result,
11410 bmap1->n_div + bmap2->n_div,
11411 bmap1->n_eq + bmap2->n_eq,
11412 bmap1->n_ineq + bmap2->n_ineq);
11413 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11414 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11415 bmap = isl_basic_map_simplify(bmap);
11416 return isl_basic_map_finalize(bmap);
11417 error:
11418 isl_basic_map_free(bmap1);
11419 isl_basic_map_free(bmap2);
11420 return NULL;
11423 __isl_give isl_basic_map *isl_basic_map_flat_product(
11424 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11426 isl_basic_map *prod;
11428 prod = isl_basic_map_product(bmap1, bmap2);
11429 prod = isl_basic_map_flatten(prod);
11430 return prod;
11433 __isl_give isl_basic_set *isl_basic_set_flat_product(
11434 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
11436 return isl_basic_map_flat_range_product(bset1, bset2);
11439 __isl_give isl_basic_map *isl_basic_map_domain_product(
11440 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11442 isl_space *space1, *space2;
11443 isl_space *space_result = NULL;
11444 isl_basic_map *bmap;
11445 isl_size in1, in2, out, nparam;
11446 unsigned total, pos;
11447 struct isl_dim_map *dim_map1, *dim_map2;
11449 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
11450 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
11451 out = isl_basic_map_dim(bmap1, isl_dim_out);
11452 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11453 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
11454 goto error;
11456 space1 = isl_basic_map_get_space(bmap1);
11457 space2 = isl_basic_map_get_space(bmap2);
11458 space_result = isl_space_domain_product(space1, space2);
11460 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
11461 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11462 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11463 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11464 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11465 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11466 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
11467 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
11468 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
11469 isl_dim_map_div(dim_map1, bmap1, pos += out);
11470 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11472 bmap = isl_basic_map_alloc_space(space_result,
11473 bmap1->n_div + bmap2->n_div,
11474 bmap1->n_eq + bmap2->n_eq,
11475 bmap1->n_ineq + bmap2->n_ineq);
11476 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11477 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11478 bmap = isl_basic_map_simplify(bmap);
11479 return isl_basic_map_finalize(bmap);
11480 error:
11481 isl_basic_map_free(bmap1);
11482 isl_basic_map_free(bmap2);
11483 return NULL;
11486 __isl_give isl_basic_map *isl_basic_map_range_product(
11487 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11489 isl_bool rational;
11490 isl_space *space_result = NULL;
11491 isl_basic_map *bmap;
11492 isl_size in, out1, out2, nparam;
11493 unsigned total, pos;
11494 struct isl_dim_map *dim_map1, *dim_map2;
11496 rational = isl_basic_map_is_rational(bmap1);
11497 if (rational >= 0 && rational)
11498 rational = isl_basic_map_is_rational(bmap2);
11499 in = isl_basic_map_dim(bmap1, isl_dim_in);
11500 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
11501 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
11502 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
11503 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
11504 goto error;
11506 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
11507 goto error;
11509 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
11510 isl_space_copy(bmap2->dim));
11512 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
11513 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
11514 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
11515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
11516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
11517 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
11518 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
11519 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
11520 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
11521 isl_dim_map_div(dim_map1, bmap1, pos += out2);
11522 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
11524 bmap = isl_basic_map_alloc_space(space_result,
11525 bmap1->n_div + bmap2->n_div,
11526 bmap1->n_eq + bmap2->n_eq,
11527 bmap1->n_ineq + bmap2->n_ineq);
11528 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
11529 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
11530 if (rational)
11531 bmap = isl_basic_map_set_rational(bmap);
11532 bmap = isl_basic_map_simplify(bmap);
11533 return isl_basic_map_finalize(bmap);
11534 error:
11535 isl_basic_map_free(bmap1);
11536 isl_basic_map_free(bmap2);
11537 return NULL;
11540 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
11541 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
11543 isl_basic_map *prod;
11545 prod = isl_basic_map_range_product(bmap1, bmap2);
11546 prod = isl_basic_map_flatten_range(prod);
11547 return prod;
11550 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11551 * and collect the results.
11552 * The result live in the space obtained by calling "space_product"
11553 * on the spaces of "map1" and "map2".
11554 * If "remove_duplicates" is set then the result may contain duplicates
11555 * (even if the inputs do not) and so we try and remove the obvious
11556 * duplicates.
11558 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
11559 __isl_take isl_map *map2,
11560 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
11561 __isl_take isl_space *right),
11562 __isl_give isl_basic_map *(*basic_map_product)(
11563 __isl_take isl_basic_map *left,
11564 __isl_take isl_basic_map *right),
11565 int remove_duplicates)
11567 unsigned flags = 0;
11568 struct isl_map *result;
11569 int i, j;
11570 isl_bool m;
11572 m = isl_map_has_equal_params(map1, map2);
11573 if (m < 0)
11574 goto error;
11575 if (!m)
11576 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
11577 "parameters don't match", goto error);
11579 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
11580 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
11581 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
11583 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
11584 isl_space_copy(map2->dim)),
11585 map1->n * map2->n, flags);
11586 if (!result)
11587 goto error;
11588 for (i = 0; i < map1->n; ++i)
11589 for (j = 0; j < map2->n; ++j) {
11590 struct isl_basic_map *part;
11591 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
11592 isl_basic_map_copy(map2->p[j]));
11593 if (isl_basic_map_is_empty(part))
11594 isl_basic_map_free(part);
11595 else
11596 result = isl_map_add_basic_map(result, part);
11597 if (!result)
11598 goto error;
11600 if (remove_duplicates)
11601 result = isl_map_remove_obvious_duplicates(result);
11602 isl_map_free(map1);
11603 isl_map_free(map2);
11604 return result;
11605 error:
11606 isl_map_free(map1);
11607 isl_map_free(map2);
11608 return NULL;
11611 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11613 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
11614 __isl_take isl_map *map2)
11616 isl_map_align_params_bin(&map1, &map2);
11617 return map_product(map1, map2, &isl_space_product,
11618 &isl_basic_map_product, 0);
11621 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11623 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
11624 __isl_take isl_map *map2)
11626 isl_map *prod;
11628 prod = isl_map_product(map1, map2);
11629 prod = isl_map_flatten(prod);
11630 return prod;
11633 /* Given two set A and B, construct its Cartesian product A x B.
11635 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
11636 __isl_take isl_set *set2)
11638 return isl_map_range_product(set1, set2);
11641 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
11642 __isl_take isl_set *set2)
11644 return isl_map_flat_range_product(set1, set2);
11647 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11649 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
11650 __isl_take isl_map *map2)
11652 isl_map_align_params_bin(&map1, &map2);
11653 return map_product(map1, map2, &isl_space_domain_product,
11654 &isl_basic_map_domain_product, 1);
11657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11659 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
11660 __isl_take isl_map *map2)
11662 isl_map_align_params_bin(&map1, &map2);
11663 return map_product(map1, map2, &isl_space_range_product,
11664 &isl_basic_map_range_product, 1);
11667 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11669 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
11671 isl_space *space;
11672 isl_size total1, keep1, total2, keep2;
11674 total1 = isl_map_dim(map, isl_dim_in);
11675 total2 = isl_map_dim(map, isl_dim_out);
11676 if (total1 < 0 || total2 < 0)
11677 return isl_map_free(map);
11678 if (!isl_space_domain_is_wrapping(map->dim) ||
11679 !isl_space_range_is_wrapping(map->dim))
11680 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11681 "not a product", return isl_map_free(map));
11683 space = isl_map_get_space(map);
11684 space = isl_space_factor_domain(space);
11685 keep1 = isl_space_dim(space, isl_dim_in);
11686 keep2 = isl_space_dim(space, isl_dim_out);
11687 if (keep1 < 0 || keep2 < 0)
11688 map = isl_map_free(map);
11689 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
11690 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
11691 map = isl_map_reset_space(map, space);
11693 return map;
11696 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11698 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
11700 isl_space *space;
11701 isl_size total1, keep1, total2, keep2;
11703 total1 = isl_map_dim(map, isl_dim_in);
11704 total2 = isl_map_dim(map, isl_dim_out);
11705 if (total1 < 0 || total2 < 0)
11706 return isl_map_free(map);
11707 if (!isl_space_domain_is_wrapping(map->dim) ||
11708 !isl_space_range_is_wrapping(map->dim))
11709 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11710 "not a product", return isl_map_free(map));
11712 space = isl_map_get_space(map);
11713 space = isl_space_factor_range(space);
11714 keep1 = isl_space_dim(space, isl_dim_in);
11715 keep2 = isl_space_dim(space, isl_dim_out);
11716 if (keep1 < 0 || keep2 < 0)
11717 map = isl_map_free(map);
11718 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
11719 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
11720 map = isl_map_reset_space(map, space);
11722 return map;
11725 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11727 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
11729 isl_space *space;
11730 isl_size total, keep;
11732 total = isl_map_dim(map, isl_dim_in);
11733 if (total < 0)
11734 return isl_map_free(map);
11735 if (!isl_space_domain_is_wrapping(map->dim))
11736 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11737 "domain is not a product", return isl_map_free(map));
11739 space = isl_map_get_space(map);
11740 space = isl_space_domain_factor_domain(space);
11741 keep = isl_space_dim(space, isl_dim_in);
11742 if (keep < 0)
11743 map = isl_map_free(map);
11744 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
11745 map = isl_map_reset_space(map, space);
11747 return map;
11750 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11752 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11754 isl_space *space;
11755 isl_size total, keep;
11757 total = isl_map_dim(map, isl_dim_in);
11758 if (total < 0)
11759 return isl_map_free(map);
11760 if (!isl_space_domain_is_wrapping(map->dim))
11761 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11762 "domain is not a product", return isl_map_free(map));
11764 space = isl_map_get_space(map);
11765 space = isl_space_domain_factor_range(space);
11766 keep = isl_space_dim(space, isl_dim_in);
11767 if (keep < 0)
11768 map = isl_map_free(map);
11769 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11770 map = isl_map_reset_space(map, space);
11772 return map;
11775 /* Given a map A -> [B -> C], extract the map A -> B.
11777 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11779 isl_space *space;
11780 isl_size total, keep;
11782 total = isl_map_dim(map, isl_dim_out);
11783 if (total < 0)
11784 return isl_map_free(map);
11785 if (!isl_space_range_is_wrapping(map->dim))
11786 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11787 "range is not a product", return isl_map_free(map));
11789 space = isl_map_get_space(map);
11790 space = isl_space_range_factor_domain(space);
11791 keep = isl_space_dim(space, isl_dim_out);
11792 if (keep < 0)
11793 map = isl_map_free(map);
11794 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11795 map = isl_map_reset_space(map, space);
11797 return map;
11800 /* Given a map A -> [B -> C], extract the map A -> C.
11802 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11804 isl_space *space;
11805 isl_size total, keep;
11807 total = isl_map_dim(map, isl_dim_out);
11808 if (total < 0)
11809 return isl_map_free(map);
11810 if (!isl_space_range_is_wrapping(map->dim))
11811 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11812 "range is not a product", return isl_map_free(map));
11814 space = isl_map_get_space(map);
11815 space = isl_space_range_factor_range(space);
11816 keep = isl_space_dim(space, isl_dim_out);
11817 if (keep < 0)
11818 map = isl_map_free(map);
11819 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11820 map = isl_map_reset_space(map, space);
11822 return map;
11825 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11827 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11828 __isl_take isl_map *map2)
11830 isl_map *prod;
11832 prod = isl_map_domain_product(map1, map2);
11833 prod = isl_map_flatten_domain(prod);
11834 return prod;
11837 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11839 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11840 __isl_take isl_map *map2)
11842 isl_map *prod;
11844 prod = isl_map_range_product(map1, map2);
11845 prod = isl_map_flatten_range(prod);
11846 return prod;
11849 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11851 int i;
11852 uint32_t hash = isl_hash_init();
11853 isl_size total;
11855 if (!bmap)
11856 return 0;
11857 bmap = isl_basic_map_copy(bmap);
11858 bmap = isl_basic_map_normalize(bmap);
11859 total = isl_basic_map_dim(bmap, isl_dim_all);
11860 if (total < 0)
11861 return 0;
11862 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11863 for (i = 0; i < bmap->n_eq; ++i) {
11864 uint32_t c_hash;
11865 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11866 isl_hash_hash(hash, c_hash);
11868 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11869 for (i = 0; i < bmap->n_ineq; ++i) {
11870 uint32_t c_hash;
11871 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11872 isl_hash_hash(hash, c_hash);
11874 isl_hash_byte(hash, bmap->n_div & 0xFF);
11875 for (i = 0; i < bmap->n_div; ++i) {
11876 uint32_t c_hash;
11877 if (isl_int_is_zero(bmap->div[i][0]))
11878 continue;
11879 isl_hash_byte(hash, i & 0xFF);
11880 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11881 isl_hash_hash(hash, c_hash);
11883 isl_basic_map_free(bmap);
11884 return hash;
11887 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11889 return isl_basic_map_get_hash(bset_to_bmap(bset));
11892 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11894 int i;
11895 uint32_t hash;
11897 if (!map)
11898 return 0;
11899 map = isl_map_copy(map);
11900 map = isl_map_normalize(map);
11901 if (!map)
11902 return 0;
11904 hash = isl_hash_init();
11905 for (i = 0; i < map->n; ++i) {
11906 uint32_t bmap_hash;
11907 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11908 isl_hash_hash(hash, bmap_hash);
11911 isl_map_free(map);
11913 return hash;
11916 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11918 return isl_map_get_hash(set_to_map(set));
11921 /* Return the number of basic maps in the (current) representation of "map".
11923 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11925 return map ? map->n : isl_size_error;
11928 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11930 return set ? set->n : isl_size_error;
11933 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11934 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11936 int i;
11938 if (!map)
11939 return isl_stat_error;
11941 for (i = 0; i < map->n; ++i)
11942 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11943 return isl_stat_error;
11945 return isl_stat_ok;
11948 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11949 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11951 int i;
11953 if (!set)
11954 return isl_stat_error;
11956 for (i = 0; i < set->n; ++i)
11957 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11958 return isl_stat_error;
11960 return isl_stat_ok;
11963 /* Does "test" succeed on every basic set in "set"?
11965 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11966 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11967 void *user)
11969 int i;
11971 if (!set)
11972 return isl_bool_error;
11974 for (i = 0; i < set->n; ++i) {
11975 isl_bool r;
11977 r = test(set->p[i], user);
11978 if (r < 0 || !r)
11979 return r;
11982 return isl_bool_true;
11985 /* Return a list of basic sets, the union of which is equal to "set".
11987 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11988 __isl_keep isl_set *set)
11990 int i;
11991 isl_basic_set_list *list;
11993 if (!set)
11994 return NULL;
11996 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11997 for (i = 0; i < set->n; ++i) {
11998 isl_basic_set *bset;
12000 bset = isl_basic_set_copy(set->p[i]);
12001 list = isl_basic_set_list_add(list, bset);
12004 return list;
12007 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
12009 isl_space *space;
12011 if (!bset)
12012 return NULL;
12014 bset = isl_basic_set_cow(bset);
12015 if (!bset)
12016 return NULL;
12018 space = isl_basic_set_get_space(bset);
12019 space = isl_space_lift(space, bset->n_div);
12020 if (!space)
12021 goto error;
12022 isl_space_free(bset->dim);
12023 bset->dim = space;
12024 bset->extra -= bset->n_div;
12025 bset->n_div = 0;
12027 bset = isl_basic_set_finalize(bset);
12029 return bset;
12030 error:
12031 isl_basic_set_free(bset);
12032 return NULL;
12035 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
12037 int i;
12038 isl_space *space;
12039 unsigned n_div;
12041 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
12043 if (!set)
12044 return NULL;
12046 set = isl_set_cow(set);
12047 if (!set)
12048 return NULL;
12050 n_div = set->p[0]->n_div;
12051 space = isl_set_get_space(set);
12052 space = isl_space_lift(space, n_div);
12053 if (!space)
12054 goto error;
12055 isl_space_free(set->dim);
12056 set->dim = space;
12058 for (i = 0; i < set->n; ++i) {
12059 set->p[i] = isl_basic_set_lift(set->p[i]);
12060 if (!set->p[i])
12061 goto error;
12064 return set;
12065 error:
12066 isl_set_free(set);
12067 return NULL;
12070 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
12072 isl_size dim;
12073 int size = 0;
12075 dim = isl_basic_set_dim(bset, isl_dim_all);
12076 if (dim < 0)
12077 return -1;
12078 size += bset->n_eq * (1 + dim);
12079 size += bset->n_ineq * (1 + dim);
12080 size += bset->n_div * (2 + dim);
12082 return size;
12085 int isl_set_size(__isl_keep isl_set *set)
12087 int i;
12088 int size = 0;
12090 if (!set)
12091 return -1;
12093 for (i = 0; i < set->n; ++i)
12094 size += isl_basic_set_size(set->p[i]);
12096 return size;
12099 /* Check if there is any lower bound (if lower == 0) and/or upper
12100 * bound (if upper == 0) on the specified dim.
12102 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
12103 enum isl_dim_type type, unsigned pos, int lower, int upper)
12105 int i;
12106 isl_bool involves;
12107 isl_size off;
12109 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
12110 return isl_bool_error;
12112 off = isl_basic_map_var_offset(bmap, type);
12113 if (off < 0)
12114 return isl_bool_error;
12115 involves = isl_basic_map_any_div_involves_vars(bmap, off + pos, 1);
12116 if (involves < 0 || involves)
12117 return involves;
12119 for (i = 0; i < bmap->n_eq; ++i)
12120 if (!isl_int_is_zero(bmap->eq[i][1 + off + pos]))
12121 return isl_bool_true;
12123 for (i = 0; i < bmap->n_ineq; ++i) {
12124 int sgn = isl_int_sgn(bmap->ineq[i][1 + off + pos]);
12125 if (sgn > 0)
12126 lower = 1;
12127 if (sgn < 0)
12128 upper = 1;
12131 return lower && upper;
12134 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
12135 enum isl_dim_type type, unsigned pos)
12137 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
12140 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
12141 enum isl_dim_type type, unsigned pos)
12143 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
12146 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
12147 enum isl_dim_type type, unsigned pos)
12149 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
12152 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
12153 enum isl_dim_type type, unsigned pos)
12155 int i;
12157 if (!map)
12158 return isl_bool_error;
12160 for (i = 0; i < map->n; ++i) {
12161 isl_bool bounded;
12162 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
12163 if (bounded < 0 || !bounded)
12164 return bounded;
12167 return isl_bool_true;
12170 /* Return true if the specified dim is involved in both an upper bound
12171 * and a lower bound.
12173 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
12174 enum isl_dim_type type, unsigned pos)
12176 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
12179 /* Does "map" have a bound (according to "fn") for any of its basic maps?
12181 static isl_bool has_any_bound(__isl_keep isl_map *map,
12182 enum isl_dim_type type, unsigned pos,
12183 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12184 enum isl_dim_type type, unsigned pos))
12186 int i;
12188 if (!map)
12189 return isl_bool_error;
12191 for (i = 0; i < map->n; ++i) {
12192 isl_bool bounded;
12193 bounded = fn(map->p[i], type, pos);
12194 if (bounded < 0 || bounded)
12195 return bounded;
12198 return isl_bool_false;
12201 /* Return 1 if the specified dim is involved in any lower bound.
12203 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
12204 enum isl_dim_type type, unsigned pos)
12206 return has_any_bound(set, type, pos,
12207 &isl_basic_map_dim_has_lower_bound);
12210 /* Return 1 if the specified dim is involved in any upper bound.
12212 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
12213 enum isl_dim_type type, unsigned pos)
12215 return has_any_bound(set, type, pos,
12216 &isl_basic_map_dim_has_upper_bound);
12219 /* Does "map" have a bound (according to "fn") for all of its basic maps?
12221 static isl_bool has_bound(__isl_keep isl_map *map,
12222 enum isl_dim_type type, unsigned pos,
12223 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
12224 enum isl_dim_type type, unsigned pos))
12226 int i;
12228 if (!map)
12229 return isl_bool_error;
12231 for (i = 0; i < map->n; ++i) {
12232 isl_bool bounded;
12233 bounded = fn(map->p[i], type, pos);
12234 if (bounded < 0 || !bounded)
12235 return bounded;
12238 return isl_bool_true;
12241 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
12243 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
12244 enum isl_dim_type type, unsigned pos)
12246 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
12249 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
12251 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
12252 enum isl_dim_type type, unsigned pos)
12254 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
12257 /* For each of the "n" variables starting at "first", determine
12258 * the sign of the variable and put the results in the first "n"
12259 * elements of the array "signs".
12260 * Sign
12261 * 1 means that the variable is non-negative
12262 * -1 means that the variable is non-positive
12263 * 0 means the variable attains both positive and negative values.
12265 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
12266 unsigned first, unsigned n, int *signs)
12268 isl_vec *bound = NULL;
12269 struct isl_tab *tab = NULL;
12270 struct isl_tab_undo *snap;
12271 int i;
12272 isl_size total;
12274 total = isl_basic_set_dim(bset, isl_dim_all);
12275 if (total < 0 || !signs)
12276 return isl_stat_error;
12278 bound = isl_vec_alloc(bset->ctx, 1 + total);
12279 tab = isl_tab_from_basic_set(bset, 0);
12280 if (!bound || !tab)
12281 goto error;
12283 isl_seq_clr(bound->el, bound->size);
12284 isl_int_set_si(bound->el[0], -1);
12286 snap = isl_tab_snap(tab);
12287 for (i = 0; i < n; ++i) {
12288 int empty;
12290 isl_int_set_si(bound->el[1 + first + i], -1);
12291 if (isl_tab_add_ineq(tab, bound->el) < 0)
12292 goto error;
12293 empty = tab->empty;
12294 isl_int_set_si(bound->el[1 + first + i], 0);
12295 if (isl_tab_rollback(tab, snap) < 0)
12296 goto error;
12298 if (empty) {
12299 signs[i] = 1;
12300 continue;
12303 isl_int_set_si(bound->el[1 + first + i], 1);
12304 if (isl_tab_add_ineq(tab, bound->el) < 0)
12305 goto error;
12306 empty = tab->empty;
12307 isl_int_set_si(bound->el[1 + first + i], 0);
12308 if (isl_tab_rollback(tab, snap) < 0)
12309 goto error;
12311 signs[i] = empty ? -1 : 0;
12314 isl_tab_free(tab);
12315 isl_vec_free(bound);
12316 return isl_stat_ok;
12317 error:
12318 isl_tab_free(tab);
12319 isl_vec_free(bound);
12320 return isl_stat_error;
12323 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
12324 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
12326 if (!bset || !signs)
12327 return isl_stat_error;
12328 if (isl_basic_set_check_range(bset, type, first, n) < 0)
12329 return isl_stat_error;
12331 first += pos(bset->dim, type) - 1;
12332 return isl_basic_set_vars_get_sign(bset, first, n, signs);
12335 /* Is it possible for the integer division "div" to depend (possibly
12336 * indirectly) on any output dimensions?
12338 * If the div is undefined, then we conservatively assume that it
12339 * may depend on them.
12340 * Otherwise, we check if it actually depends on them or on any integer
12341 * divisions that may depend on them.
12343 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
12345 int i;
12346 isl_size n_out, n_div;
12347 unsigned o_out, o_div;
12349 if (isl_int_is_zero(bmap->div[div][0]))
12350 return isl_bool_true;
12352 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12353 if (n_out < 0)
12354 return isl_bool_error;
12355 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12357 if (isl_seq_any_non_zero(bmap->div[div] + 1 + o_out, n_out))
12358 return isl_bool_true;
12360 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12361 if (n_div < 0)
12362 return isl_bool_error;
12363 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12365 for (i = 0; i < n_div; ++i) {
12366 isl_bool may_involve;
12368 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
12369 continue;
12370 may_involve = div_may_involve_output(bmap, i);
12371 if (may_involve < 0 || may_involve)
12372 return may_involve;
12375 return isl_bool_false;
12378 /* Return the first integer division of "bmap" in the range
12379 * [first, first + n[ that may depend on any output dimensions and
12380 * that has a non-zero coefficient in "c" (where the first coefficient
12381 * in "c" corresponds to integer division "first").
12383 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
12384 isl_int *c, int first, int n)
12386 int k;
12388 if (!bmap)
12389 return -1;
12391 for (k = first; k < first + n; ++k) {
12392 isl_bool may_involve;
12394 if (isl_int_is_zero(c[k]))
12395 continue;
12396 may_involve = div_may_involve_output(bmap, k);
12397 if (may_involve < 0)
12398 return -1;
12399 if (may_involve)
12400 return k;
12403 return first + n;
12406 /* Look for a pair of inequality constraints in "bmap" of the form
12408 * -l + i >= 0 or i >= l
12409 * and
12410 * n + l - i >= 0 or i <= l + n
12412 * with n < "m" and i the output dimension at position "pos".
12413 * (Note that n >= 0 as otherwise the two constraints would conflict.)
12414 * Furthermore, "l" is only allowed to involve parameters, input dimensions
12415 * and earlier output dimensions, as well as integer divisions that do
12416 * not involve any of the output dimensions.
12418 * Return the index of the first inequality constraint or bmap->n_ineq
12419 * if no such pair can be found.
12421 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
12422 int pos, isl_int m)
12424 int i, j;
12425 isl_ctx *ctx;
12426 isl_size total;
12427 isl_size n_div, n_out;
12428 unsigned o_div, o_out;
12429 int less;
12431 total = isl_basic_map_dim(bmap, isl_dim_all);
12432 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12433 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12434 if (total < 0 || n_out < 0 || n_div < 0)
12435 return -1;
12437 ctx = isl_basic_map_get_ctx(bmap);
12438 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12439 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12440 for (i = 0; i < bmap->n_ineq; ++i) {
12441 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
12442 continue;
12443 if (isl_seq_any_non_zero(bmap->ineq[i] + o_out + pos + 1,
12444 n_out - (pos + 1)))
12445 continue;
12446 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
12447 0, n_div) < n_div)
12448 continue;
12449 for (j = i + 1; j < bmap->n_ineq; ++j) {
12450 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
12451 ctx->one))
12452 continue;
12453 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
12454 bmap->ineq[j] + 1, total))
12455 continue;
12456 break;
12458 if (j >= bmap->n_ineq)
12459 continue;
12460 isl_int_add(bmap->ineq[i][0],
12461 bmap->ineq[i][0], bmap->ineq[j][0]);
12462 less = isl_int_abs_lt(bmap->ineq[i][0], m);
12463 isl_int_sub(bmap->ineq[i][0],
12464 bmap->ineq[i][0], bmap->ineq[j][0]);
12465 if (!less)
12466 continue;
12467 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
12468 return i;
12469 else
12470 return j;
12473 return bmap->n_ineq;
12476 /* Return the index of the equality of "bmap" that defines
12477 * the output dimension "pos" in terms of earlier dimensions.
12478 * The equality may also involve integer divisions, as long
12479 * as those integer divisions are defined in terms of
12480 * parameters or input dimensions.
12481 * In this case, *div is set to the number of integer divisions and
12482 * *ineq is set to the number of inequality constraints (provided
12483 * div and ineq are not NULL).
12485 * The equality may also involve a single integer division involving
12486 * the output dimensions (typically only output dimension "pos") as
12487 * long as the coefficient of output dimension "pos" is 1 or -1 and
12488 * there is a pair of constraints i >= l and i <= l + n, with i referring
12489 * to output dimension "pos", l an expression involving only earlier
12490 * dimensions and n smaller than the coefficient of the integer division
12491 * in the equality. In this case, the output dimension can be defined
12492 * in terms of a modulo expression that does not involve the integer division.
12493 * *div is then set to this single integer division and
12494 * *ineq is set to the index of constraint i >= l.
12496 * Return bmap->n_eq if there is no such equality.
12497 * Return -1 on error.
12499 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
12500 int pos, int *div, int *ineq)
12502 int j, k, l;
12503 isl_size n_div, n_out;
12504 unsigned o_div, o_out;
12506 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12507 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12508 if (n_out < 0 || n_div < 0)
12509 return -1;
12511 o_out = isl_basic_map_offset(bmap, isl_dim_out);
12512 o_div = isl_basic_map_offset(bmap, isl_dim_div);
12514 if (ineq)
12515 *ineq = bmap->n_ineq;
12516 if (div)
12517 *div = n_div;
12518 for (j = 0; j < bmap->n_eq; ++j) {
12519 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
12520 continue;
12521 if (isl_seq_any_non_zero(bmap->eq[j] + o_out + pos + 1,
12522 n_out - (pos + 1)))
12523 continue;
12524 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12525 0, n_div);
12526 if (k >= n_div)
12527 return j;
12528 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
12529 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
12530 continue;
12531 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
12532 k + 1, n_div - (k+1)) < n_div)
12533 continue;
12534 l = find_modulo_constraint_pair(bmap, pos,
12535 bmap->eq[j][o_div + k]);
12536 if (l < 0)
12537 return -1;
12538 if (l >= bmap->n_ineq)
12539 continue;
12540 if (div)
12541 *div = k;
12542 if (ineq)
12543 *ineq = l;
12544 return j;
12547 return bmap->n_eq;
12550 /* Check if the given basic map is obviously single-valued.
12551 * In particular, for each output dimension, check that there is
12552 * an equality that defines the output dimension in terms of
12553 * earlier dimensions.
12555 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
12557 int i;
12558 isl_size n_out;
12560 n_out = isl_basic_map_dim(bmap, isl_dim_out);
12561 if (n_out < 0)
12562 return isl_bool_error;
12564 for (i = 0; i < n_out; ++i) {
12565 int eq;
12567 eq = isl_basic_map_output_defining_equality(bmap, i,
12568 NULL, NULL);
12569 if (eq < 0)
12570 return isl_bool_error;
12571 if (eq >= bmap->n_eq)
12572 return isl_bool_false;
12575 return isl_bool_true;
12578 /* Check if the given basic map is single-valued.
12579 * We simply compute
12581 * M \circ M^-1
12583 * and check if the result is a subset of the identity mapping.
12585 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
12587 isl_space *space;
12588 isl_basic_map *test;
12589 isl_basic_map *id;
12590 isl_bool sv;
12592 sv = isl_basic_map_plain_is_single_valued(bmap);
12593 if (sv < 0 || sv)
12594 return sv;
12596 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
12597 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
12599 space = isl_basic_map_get_space(bmap);
12600 space = isl_space_map_from_set(isl_space_range(space));
12601 id = isl_basic_map_identity(space);
12603 sv = isl_basic_map_is_subset(test, id);
12605 isl_basic_map_free(test);
12606 isl_basic_map_free(id);
12608 return sv;
12611 /* Check if the given map is obviously single-valued.
12613 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
12615 if (!map)
12616 return isl_bool_error;
12617 if (map->n == 0)
12618 return isl_bool_true;
12619 if (map->n >= 2)
12620 return isl_bool_false;
12622 return isl_basic_map_plain_is_single_valued(map->p[0]);
12625 /* Check if the given map is single-valued.
12626 * We simply compute
12628 * M \circ M^-1
12630 * and check if the result is a subset of the identity mapping.
12632 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
12634 isl_space *space;
12635 isl_map *test;
12636 isl_map *id;
12637 isl_bool sv;
12639 sv = isl_map_plain_is_single_valued(map);
12640 if (sv < 0 || sv)
12641 return sv;
12643 test = isl_map_reverse(isl_map_copy(map));
12644 test = isl_map_apply_range(test, isl_map_copy(map));
12646 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
12647 id = isl_map_identity(space);
12649 sv = isl_map_is_subset(test, id);
12651 isl_map_free(test);
12652 isl_map_free(id);
12654 return sv;
12657 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
12659 isl_bool in;
12661 map = isl_map_copy(map);
12662 map = isl_map_reverse(map);
12663 in = isl_map_is_single_valued(map);
12664 isl_map_free(map);
12666 return in;
12669 /* Check if the given map is obviously injective.
12671 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
12673 isl_bool in;
12675 map = isl_map_copy(map);
12676 map = isl_map_reverse(map);
12677 in = isl_map_plain_is_single_valued(map);
12678 isl_map_free(map);
12680 return in;
12683 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
12685 isl_bool sv;
12687 sv = isl_map_is_single_valued(map);
12688 if (sv < 0 || !sv)
12689 return sv;
12691 return isl_map_is_injective(map);
12694 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
12696 return isl_map_is_single_valued(set_to_map(set));
12699 /* Does "map" only map elements to themselves?
12701 * If the domain and range spaces are different, then "map"
12702 * is considered not to be an identity relation, even if it is empty.
12703 * Otherwise, construct the maximal identity relation and
12704 * check whether "map" is a subset of this relation.
12706 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
12708 isl_map *id;
12709 isl_bool equal, is_identity;
12711 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
12712 if (equal < 0 || !equal)
12713 return equal;
12715 id = isl_map_identity(isl_map_get_space(map));
12716 is_identity = isl_map_is_subset(map, id);
12717 isl_map_free(id);
12719 return is_identity;
12722 int isl_map_is_translation(__isl_keep isl_map *map)
12724 int ok;
12725 isl_set *delta;
12727 delta = isl_map_deltas(isl_map_copy(map));
12728 ok = isl_set_is_singleton(delta);
12729 isl_set_free(delta);
12731 return ok;
12734 static int unique(isl_int *p, unsigned pos, unsigned len)
12736 if (isl_seq_any_non_zero(p, pos))
12737 return 0;
12738 if (isl_seq_any_non_zero(p + pos + 1, len - pos - 1))
12739 return 0;
12740 return 1;
12743 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
12745 int i, j;
12746 isl_size nvar, ovar, n_div;
12748 n_div = isl_basic_set_dim(bset, isl_dim_div);
12749 if (n_div < 0)
12750 return isl_bool_error;
12751 if (n_div != 0)
12752 return isl_bool_false;
12754 nvar = isl_basic_set_dim(bset, isl_dim_set);
12755 ovar = isl_space_offset(bset->dim, isl_dim_set);
12756 if (nvar < 0 || ovar < 0)
12757 return isl_bool_error;
12758 for (j = 0; j < nvar; ++j) {
12759 int lower = 0, upper = 0;
12760 for (i = 0; i < bset->n_eq; ++i) {
12761 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12762 continue;
12763 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12764 return isl_bool_false;
12765 break;
12767 if (i < bset->n_eq)
12768 continue;
12769 for (i = 0; i < bset->n_ineq; ++i) {
12770 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12771 continue;
12772 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12773 return isl_bool_false;
12774 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12775 lower = 1;
12776 else
12777 upper = 1;
12779 if (!lower || !upper)
12780 return isl_bool_false;
12783 return isl_bool_true;
12786 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12788 if (!set)
12789 return isl_bool_error;
12790 if (set->n != 1)
12791 return isl_bool_false;
12793 return isl_basic_set_is_box(set->p[0]);
12796 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12798 if (!bset)
12799 return isl_bool_error;
12801 return isl_space_is_wrapping(bset->dim);
12804 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12806 if (!set)
12807 return isl_bool_error;
12809 return isl_space_is_wrapping(set->dim);
12812 /* Modify the space of "map" through a call to "change".
12813 * If "can_change" is set (not NULL), then first call it to check
12814 * if the modification is allowed, printing the error message "cannot_change"
12815 * if it is not.
12817 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12818 isl_bool (*can_change)(__isl_keep isl_map *map),
12819 const char *cannot_change,
12820 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12822 isl_bool ok;
12823 isl_space *space;
12825 if (!map)
12826 return NULL;
12828 ok = can_change ? can_change(map) : isl_bool_true;
12829 if (ok < 0)
12830 return isl_map_free(map);
12831 if (!ok)
12832 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12833 return isl_map_free(map));
12835 space = change(isl_map_get_space(map));
12836 map = isl_map_reset_space(map, space);
12838 return map;
12841 /* Is the domain of "map" a wrapped relation?
12843 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12845 if (!map)
12846 return isl_bool_error;
12848 return isl_space_domain_is_wrapping(map->dim);
12851 /* Does "map" have a wrapped relation in both domain and range?
12853 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12855 return isl_space_is_product(isl_map_peek_space(map));
12858 /* Is the range of "map" a wrapped relation?
12860 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12862 if (!map)
12863 return isl_bool_error;
12865 return isl_space_range_is_wrapping(map->dim);
12868 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12870 isl_space *space;
12872 space = isl_basic_map_take_space(bmap);
12873 space = isl_space_wrap(space);
12874 bmap = isl_basic_map_restore_space(bmap, space);
12876 bmap = isl_basic_map_finalize(bmap);
12878 return bset_from_bmap(bmap);
12881 /* Given a map A -> B, return the set (A -> B).
12883 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12885 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12888 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12890 bset = isl_basic_set_cow(bset);
12891 if (!bset)
12892 return NULL;
12894 bset->dim = isl_space_unwrap(bset->dim);
12895 if (!bset->dim)
12896 goto error;
12898 bset = isl_basic_set_finalize(bset);
12900 return bset_to_bmap(bset);
12901 error:
12902 isl_basic_set_free(bset);
12903 return NULL;
12906 /* Given a set (A -> B), return the map A -> B.
12907 * Error out if "set" is not of the form (A -> B).
12909 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12911 return isl_map_change_space(set, &isl_set_is_wrapping,
12912 "not a wrapping set", &isl_space_unwrap);
12915 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12916 enum isl_dim_type type)
12918 isl_space *space;
12920 space = isl_basic_map_take_space(bmap);
12921 space = isl_space_reset(space, type);
12922 bmap = isl_basic_map_restore_space(bmap, space);
12924 bmap = isl_basic_map_mark_final(bmap);
12926 return bmap;
12929 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12930 enum isl_dim_type type)
12932 int i;
12933 isl_space *space;
12935 if (!map)
12936 return NULL;
12938 if (!isl_space_is_named_or_nested(map->dim, type))
12939 return map;
12941 map = isl_map_cow(map);
12942 if (!map)
12943 return NULL;
12945 for (i = 0; i < map->n; ++i) {
12946 map->p[i] = isl_basic_map_reset(map->p[i], type);
12947 if (!map->p[i])
12948 goto error;
12951 space = isl_map_take_space(map);
12952 space = isl_space_reset(space, type);
12953 map = isl_map_restore_space(map, space);
12955 return map;
12956 error:
12957 isl_map_free(map);
12958 return NULL;
12961 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12963 isl_space *space;
12965 space = isl_basic_map_take_space(bmap);
12966 space = isl_space_flatten(space);
12967 bmap = isl_basic_map_restore_space(bmap, space);
12969 bmap = isl_basic_map_mark_final(bmap);
12971 return bmap;
12974 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12976 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12979 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12980 __isl_take isl_basic_map *bmap)
12982 isl_space *space;
12984 space = isl_basic_map_take_space(bmap);
12985 space = isl_space_flatten_domain(space);
12986 bmap = isl_basic_map_restore_space(bmap, space);
12988 bmap = isl_basic_map_mark_final(bmap);
12990 return bmap;
12993 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12994 __isl_take isl_basic_map *bmap)
12996 isl_space *space;
12998 space = isl_basic_map_take_space(bmap);
12999 space = isl_space_flatten_range(space);
13000 bmap = isl_basic_map_restore_space(bmap, space);
13002 bmap = isl_basic_map_mark_final(bmap);
13004 return bmap;
13007 /* Remove any internal structure from the spaces of domain and range of "map".
13009 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
13011 if (!map)
13012 return NULL;
13014 if (!map->dim->nested[0] && !map->dim->nested[1])
13015 return map;
13017 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
13020 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
13022 return set_from_map(isl_map_flatten(set_to_map(set)));
13025 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
13027 isl_space *space, *flat_space;
13028 isl_map *map;
13030 space = isl_set_get_space(set);
13031 flat_space = isl_space_flatten(isl_space_copy(space));
13032 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
13033 flat_space));
13034 map = isl_map_intersect_domain(map, set);
13036 return map;
13039 /* Remove any internal structure from the space of the domain of "map".
13041 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
13043 if (!map)
13044 return NULL;
13046 if (!map->dim->nested[0])
13047 return map;
13049 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
13052 /* Remove any internal structure from the space of the range of "map".
13054 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
13056 if (!map)
13057 return NULL;
13059 if (!map->dim->nested[1])
13060 return map;
13062 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
13065 /* Reorder the dimensions of "bmap" according to the given dim_map
13066 * and set the dimension specification to "space" and
13067 * perform Gaussian elimination on the result.
13069 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
13070 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
13072 isl_basic_map *res;
13073 unsigned flags;
13074 isl_size n_div;
13076 n_div = isl_basic_map_dim(bmap, isl_dim_div);
13077 if (n_div < 0 || !space || !dim_map)
13078 goto error;
13080 flags = bmap->flags;
13081 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
13082 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
13083 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
13084 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
13085 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
13086 if (res)
13087 res->flags = flags;
13088 res = isl_basic_map_gauss(res, NULL);
13089 res = isl_basic_map_finalize(res);
13090 return res;
13091 error:
13092 isl_dim_map_free(dim_map);
13093 isl_basic_map_free(bmap);
13094 isl_space_free(space);
13095 return NULL;
13098 /* Reorder the dimensions of "map" according to given reordering.
13100 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
13101 __isl_take isl_reordering *r)
13103 int i;
13104 struct isl_dim_map *dim_map;
13106 map = isl_map_cow(map);
13107 dim_map = isl_dim_map_from_reordering(r);
13108 if (!map || !r || !dim_map)
13109 goto error;
13111 for (i = 0; i < map->n; ++i) {
13112 struct isl_dim_map *dim_map_i;
13113 isl_space *space;
13115 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
13117 space = isl_reordering_get_space(r);
13118 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
13120 if (!map->p[i])
13121 goto error;
13124 map = isl_map_reset_space(map, isl_reordering_get_space(r));
13125 map = isl_map_unmark_normalized(map);
13127 isl_reordering_free(r);
13128 isl_dim_map_free(dim_map);
13129 return map;
13130 error:
13131 isl_dim_map_free(dim_map);
13132 isl_map_free(map);
13133 isl_reordering_free(r);
13134 return NULL;
13137 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
13138 __isl_take isl_reordering *r)
13140 return set_from_map(isl_map_realign(set_to_map(set), r));
13143 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
13144 __isl_take isl_space *model)
13146 isl_ctx *ctx;
13147 isl_bool aligned;
13149 if (!map || !model)
13150 goto error;
13152 ctx = isl_space_get_ctx(model);
13153 if (!isl_space_has_named_params(model))
13154 isl_die(ctx, isl_error_invalid,
13155 "model has unnamed parameters", goto error);
13156 if (isl_map_check_named_params(map) < 0)
13157 goto error;
13158 aligned = isl_map_space_has_equal_params(map, model);
13159 if (aligned < 0)
13160 goto error;
13161 if (!aligned) {
13162 isl_space *space;
13163 isl_reordering *exp;
13165 space = isl_map_peek_space(map);
13166 exp = isl_parameter_alignment_reordering(space, model);
13167 map = isl_map_realign(map, exp);
13170 isl_space_free(model);
13171 return map;
13172 error:
13173 isl_space_free(model);
13174 isl_map_free(map);
13175 return NULL;
13178 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
13179 __isl_take isl_space *model)
13181 return isl_map_align_params(set, model);
13184 /* Align the parameters of "bmap" to those of "model", introducing
13185 * additional parameters if needed.
13187 __isl_give isl_basic_map *isl_basic_map_align_params(
13188 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
13190 isl_ctx *ctx;
13191 isl_bool equal_params;
13192 isl_space *bmap_space;
13194 if (!bmap || !model)
13195 goto error;
13197 ctx = isl_space_get_ctx(model);
13198 if (!isl_space_has_named_params(model))
13199 isl_die(ctx, isl_error_invalid,
13200 "model has unnamed parameters", goto error);
13201 if (isl_basic_map_check_named_params(bmap) < 0)
13202 goto error;
13203 bmap_space = isl_basic_map_peek_space(bmap);
13204 equal_params = isl_space_has_equal_params(bmap_space, model);
13205 if (equal_params < 0)
13206 goto error;
13207 if (!equal_params) {
13208 isl_reordering *exp;
13209 struct isl_dim_map *dim_map;
13211 exp = isl_parameter_alignment_reordering(bmap_space, model);
13212 dim_map = isl_dim_map_from_reordering(exp);
13213 bmap = isl_basic_map_realign(bmap,
13214 isl_reordering_get_space(exp),
13215 isl_dim_map_extend(dim_map, bmap));
13216 isl_reordering_free(exp);
13217 isl_dim_map_free(dim_map);
13220 isl_space_free(model);
13221 return bmap;
13222 error:
13223 isl_space_free(model);
13224 isl_basic_map_free(bmap);
13225 return NULL;
13228 /* Do "bset" and "space" have the same parameters?
13230 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
13231 __isl_keep isl_space *space)
13233 isl_space *bset_space;
13235 bset_space = isl_basic_set_peek_space(bset);
13236 return isl_space_has_equal_params(bset_space, space);
13239 /* Do "map" and "space" have the same parameters?
13241 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
13242 __isl_keep isl_space *space)
13244 isl_space *map_space;
13246 map_space = isl_map_peek_space(map);
13247 return isl_space_has_equal_params(map_space, space);
13250 /* Do "set" and "space" have the same parameters?
13252 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
13253 __isl_keep isl_space *space)
13255 return isl_map_space_has_equal_params(set_to_map(set), space);
13258 /* Align the parameters of "bset" to those of "model", introducing
13259 * additional parameters if needed.
13261 __isl_give isl_basic_set *isl_basic_set_align_params(
13262 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
13264 return isl_basic_map_align_params(bset, model);
13267 #undef TYPE
13268 #define TYPE isl_map
13269 #define isl_map_drop_dims isl_map_drop
13270 #include "isl_drop_unused_params_templ.c"
13272 /* Drop all parameters not referenced by "set".
13274 __isl_give isl_set *isl_set_drop_unused_params(
13275 __isl_take isl_set *set)
13277 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
13280 #undef TYPE
13281 #define TYPE isl_basic_map
13282 #define isl_basic_map_drop_dims isl_basic_map_drop
13283 #include "isl_drop_unused_params_templ.c"
13285 /* Drop all parameters not referenced by "bset".
13287 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
13288 __isl_take isl_basic_set *bset)
13290 return bset_from_bmap(isl_basic_map_drop_unused_params(
13291 bset_to_bmap(bset)));
13294 /* Given a tuple of identifiers "tuple" in a space that corresponds
13295 * to that of "set", if any of those identifiers appear as parameters
13296 * in "set", then equate those parameters with the corresponding
13297 * set dimensions and project out the parameters.
13298 * The result therefore has no such parameters.
13300 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
13301 __isl_keep isl_multi_id *tuple)
13303 int i;
13304 isl_size n;
13305 isl_space *set_space, *tuple_space;
13307 set_space = isl_set_peek_space(set);
13308 tuple_space = isl_multi_id_peek_space(tuple);
13309 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
13310 return isl_set_free(set);
13311 n = isl_multi_id_size(tuple);
13312 if (n < 0)
13313 return isl_set_free(set);
13314 for (i = 0; i < n; ++i) {
13315 isl_id *id;
13316 int pos;
13318 id = isl_multi_id_get_at(tuple, i);
13319 if (!id)
13320 return isl_set_free(set);
13321 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
13322 isl_id_free(id);
13323 if (pos < 0)
13324 continue;
13325 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
13326 set = isl_set_project_out(set, isl_dim_param, pos, 1);
13328 return set;
13331 /* Bind the set dimensions of "set" to parameters with identifiers
13332 * specified by "tuple", living in the same space as "set".
13334 * If no parameters with these identifiers appear in "set" already,
13335 * then the set dimensions are simply reinterpreted as parameters.
13336 * Otherwise, the parameters are first equated to the corresponding
13337 * set dimensions.
13339 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
13340 __isl_take isl_multi_id *tuple)
13342 isl_space *space;
13344 set = equate_params(set, tuple);
13345 space = isl_set_get_space(set);
13346 space = isl_space_bind_set(space, tuple);
13347 isl_multi_id_free(tuple);
13348 set = isl_set_reset_space(set, space);
13350 return set;
13353 /* Given a tuple of identifiers "tuple" in a space that corresponds
13354 * to the domain of "map", if any of those identifiers appear as parameters
13355 * in "map", then equate those parameters with the corresponding
13356 * input dimensions and project out the parameters.
13357 * The result therefore has no such parameters.
13359 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
13360 __isl_keep isl_multi_id *tuple)
13362 int i;
13363 isl_size n;
13364 isl_space *map_space, *tuple_space;
13366 map_space = isl_map_peek_space(map);
13367 tuple_space = isl_multi_id_peek_space(tuple);
13368 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
13369 return isl_map_free(map);
13370 n = isl_multi_id_size(tuple);
13371 if (n < 0)
13372 return isl_map_free(map);
13373 for (i = 0; i < n; ++i) {
13374 isl_id *id;
13375 int pos;
13377 id = isl_multi_id_get_at(tuple, i);
13378 if (!id)
13379 return isl_map_free(map);
13380 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
13381 isl_id_free(id);
13382 if (pos < 0)
13383 continue;
13384 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
13385 map = isl_map_project_out(map, isl_dim_param, pos, 1);
13387 return map;
13390 /* Bind the input dimensions of "map" to parameters with identifiers
13391 * specified by "tuple", living in the domain space of "map".
13393 * If no parameters with these identifiers appear in "map" already,
13394 * then the input dimensions are simply reinterpreted as parameters.
13395 * Otherwise, the parameters are first equated to the corresponding
13396 * input dimensions.
13398 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
13399 __isl_take isl_multi_id *tuple)
13401 isl_space *space;
13402 isl_set *set;
13404 map = map_equate_params(map, tuple);
13405 space = isl_map_get_space(map);
13406 space = isl_space_bind_map_domain(space, tuple);
13407 isl_multi_id_free(tuple);
13408 set = set_from_map(isl_map_reset_space(map, space));
13410 return set;
13413 /* Bind the output dimensions of "map" to parameters with identifiers
13414 * specified by "tuple", living in the range space of "map".
13416 * Since binding is more easily implemented on the domain,
13417 * bind the input dimensions of the inverse of "map".
13419 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
13420 __isl_take isl_multi_id *tuple)
13422 return isl_map_bind_domain(isl_map_reverse(map), tuple);
13425 /* Insert a domain corresponding to "tuple"
13426 * into the nullary or unary relation "set".
13427 * The result has an extra initial tuple and is therefore
13428 * either a unary or binary relation.
13429 * Any parameters with identifiers in "tuple" are reinterpreted
13430 * as the corresponding domain dimensions.
13432 static __isl_give isl_map *unbind_params_insert_domain(
13433 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
13435 isl_space *space;
13436 isl_reordering *r;
13438 space = isl_set_peek_space(set);
13439 r = isl_reordering_unbind_params_insert_domain(space, tuple);
13440 isl_multi_id_free(tuple);
13442 return isl_map_realign(set_to_map(set), r);
13445 /* Construct a set with "tuple" as domain from the parameter domain "set".
13446 * Any parameters with identifiers in "tuple" are reinterpreted
13447 * as the corresponding set dimensions.
13449 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
13450 __isl_take isl_multi_id *tuple)
13452 isl_bool is_params;
13454 is_params = isl_set_is_params(set);
13455 if (is_params < 0)
13456 set = isl_set_free(set);
13457 else if (!is_params)
13458 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13459 "expecting parameter domain", set = isl_set_free(set));
13460 return set_from_map(unbind_params_insert_domain(set, tuple));
13463 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13465 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
13467 isl_bool is_params;
13469 is_params = isl_set_is_params(set);
13470 if (is_params < 0)
13471 return isl_stat_error;
13472 else if (is_params)
13473 isl_die(isl_set_get_ctx(set), isl_error_invalid,
13474 "expecting proper set", return isl_stat_error);
13476 return isl_stat_ok;
13479 /* Construct a map with "domain" as domain and "set" as range.
13480 * Any parameters with identifiers in "domain" are reinterpreted
13481 * as the corresponding domain dimensions.
13483 __isl_give isl_map *isl_set_unbind_params_insert_domain(
13484 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
13486 if (isl_set_check_is_set(set) < 0)
13487 set = isl_set_free(set);
13488 return unbind_params_insert_domain(set, domain);
13491 /* Construct a map with "domain" as domain and "set" as range.
13493 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
13494 __isl_take isl_space *domain)
13496 isl_size dim;
13497 isl_space *space;
13498 isl_map *map;
13500 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
13501 domain = isl_space_free(domain);
13502 dim = isl_space_dim(domain, isl_dim_set);
13503 if (dim < 0)
13504 domain = isl_space_free(domain);
13505 space = isl_set_get_space(set);
13506 domain = isl_space_replace_params(domain, space);
13507 space = isl_space_map_from_domain_and_range(domain, space);
13509 map = isl_map_from_range(set);
13510 map = isl_map_add_dims(map, isl_dim_in, dim);
13511 map = isl_map_reset_space(map, space);
13513 return map;
13516 __isl_give isl_mat *isl_basic_map_equalities_matrix(
13517 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13518 enum isl_dim_type c2, enum isl_dim_type c3,
13519 enum isl_dim_type c4, enum isl_dim_type c5)
13521 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13522 struct isl_mat *mat;
13523 int i, j, k;
13524 int pos;
13525 isl_size total;
13527 total = isl_basic_map_dim(bmap, isl_dim_all);
13528 if (total < 0)
13529 return NULL;
13530 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
13531 if (!mat)
13532 return NULL;
13533 for (i = 0; i < bmap->n_eq; ++i)
13534 for (j = 0, pos = 0; j < 5; ++j) {
13535 int off = isl_basic_map_offset(bmap, c[j]);
13536 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13537 if (dim < 0)
13538 return isl_mat_free(mat);
13539 for (k = 0; k < dim; ++k) {
13540 isl_int_set(mat->row[i][pos],
13541 bmap->eq[i][off + k]);
13542 ++pos;
13546 return mat;
13549 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
13550 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
13551 enum isl_dim_type c2, enum isl_dim_type c3,
13552 enum isl_dim_type c4, enum isl_dim_type c5)
13554 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13555 struct isl_mat *mat;
13556 int i, j, k;
13557 int pos;
13558 isl_size total;
13560 total = isl_basic_map_dim(bmap, isl_dim_all);
13561 if (total < 0)
13562 return NULL;
13563 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
13564 if (!mat)
13565 return NULL;
13566 for (i = 0; i < bmap->n_ineq; ++i)
13567 for (j = 0, pos = 0; j < 5; ++j) {
13568 int off = isl_basic_map_offset(bmap, c[j]);
13569 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13570 if (dim < 0)
13571 return isl_mat_free(mat);
13572 for (k = 0; k < dim; ++k) {
13573 isl_int_set(mat->row[i][pos],
13574 bmap->ineq[i][off + k]);
13575 ++pos;
13579 return mat;
13582 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
13583 __isl_take isl_space *space,
13584 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13585 enum isl_dim_type c2, enum isl_dim_type c3,
13586 enum isl_dim_type c4, enum isl_dim_type c5)
13588 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
13589 isl_basic_map *bmap = NULL;
13590 isl_size dim;
13591 unsigned total;
13592 unsigned extra;
13593 int i, j, k, l;
13594 int pos;
13596 dim = isl_space_dim(space, isl_dim_all);
13597 if (dim < 0 || !eq || !ineq)
13598 goto error;
13600 if (eq->n_col != ineq->n_col)
13601 isl_die(space->ctx, isl_error_invalid,
13602 "equalities and inequalities matrices should have "
13603 "same number of columns", goto error);
13605 total = 1 + dim;
13607 if (eq->n_col < total)
13608 isl_die(space->ctx, isl_error_invalid,
13609 "number of columns too small", goto error);
13611 extra = eq->n_col - total;
13613 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
13614 eq->n_row, ineq->n_row);
13615 if (!bmap)
13616 goto error;
13617 for (i = 0; i < extra; ++i) {
13618 k = isl_basic_map_alloc_div(bmap);
13619 if (k < 0)
13620 goto error;
13621 isl_int_set_si(bmap->div[k][0], 0);
13623 for (i = 0; i < eq->n_row; ++i) {
13624 l = isl_basic_map_alloc_equality(bmap);
13625 if (l < 0)
13626 goto error;
13627 for (j = 0, pos = 0; j < 5; ++j) {
13628 int off = isl_basic_map_offset(bmap, c[j]);
13629 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13630 if (dim < 0)
13631 goto error;
13632 for (k = 0; k < dim; ++k) {
13633 isl_int_set(bmap->eq[l][off + k],
13634 eq->row[i][pos]);
13635 ++pos;
13639 for (i = 0; i < ineq->n_row; ++i) {
13640 l = isl_basic_map_alloc_inequality(bmap);
13641 if (l < 0)
13642 goto error;
13643 for (j = 0, pos = 0; j < 5; ++j) {
13644 int off = isl_basic_map_offset(bmap, c[j]);
13645 isl_size dim = isl_basic_map_dim(bmap, c[j]);
13646 if (dim < 0)
13647 goto error;
13648 for (k = 0; k < dim; ++k) {
13649 isl_int_set(bmap->ineq[l][off + k],
13650 ineq->row[i][pos]);
13651 ++pos;
13656 isl_space_free(space);
13657 isl_mat_free(eq);
13658 isl_mat_free(ineq);
13660 bmap = isl_basic_map_simplify(bmap);
13661 return isl_basic_map_finalize(bmap);
13662 error:
13663 isl_space_free(space);
13664 isl_mat_free(eq);
13665 isl_mat_free(ineq);
13666 isl_basic_map_free(bmap);
13667 return NULL;
13670 __isl_give isl_mat *isl_basic_set_equalities_matrix(
13671 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13672 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13674 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
13675 c1, c2, c3, c4, isl_dim_in);
13678 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
13679 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
13680 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13682 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
13683 c1, c2, c3, c4, isl_dim_in);
13686 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
13687 __isl_take isl_space *space,
13688 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
13689 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
13691 isl_basic_map *bmap;
13692 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
13693 c1, c2, c3, c4, isl_dim_in);
13694 return bset_from_bmap(bmap);
13697 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
13699 if (!bmap)
13700 return isl_bool_error;
13702 return isl_space_can_zip(bmap->dim);
13705 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13707 if (!map)
13708 return isl_bool_error;
13710 return isl_space_can_zip(map->dim);
13713 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13714 * (A -> C) -> (B -> D).
13716 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13718 unsigned pos;
13719 isl_size n_in;
13720 isl_size n1;
13721 isl_size n2;
13723 if (!bmap)
13724 return NULL;
13726 if (!isl_basic_map_can_zip(bmap))
13727 isl_die(bmap->ctx, isl_error_invalid,
13728 "basic map cannot be zipped", goto error);
13729 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13730 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13731 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13732 if (n_in < 0 || n1 < 0 || n2 < 0)
13733 return isl_basic_map_free(bmap);
13734 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13735 bmap = isl_basic_map_cow(bmap);
13736 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13737 if (!bmap)
13738 return NULL;
13739 bmap->dim = isl_space_zip(bmap->dim);
13740 if (!bmap->dim)
13741 goto error;
13742 bmap = isl_basic_map_mark_final(bmap);
13743 return bmap;
13744 error:
13745 isl_basic_map_free(bmap);
13746 return NULL;
13749 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13750 * (A -> C) -> (B -> D).
13752 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13754 if (!map)
13755 return NULL;
13757 if (!isl_map_can_zip(map))
13758 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13759 goto error);
13761 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13762 error:
13763 isl_map_free(map);
13764 return NULL;
13767 /* Can we apply isl_basic_map_curry to "bmap"?
13768 * That is, does it have a nested relation in its domain?
13770 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13772 if (!bmap)
13773 return isl_bool_error;
13775 return isl_space_can_curry(bmap->dim);
13778 /* Can we apply isl_map_curry to "map"?
13779 * That is, does it have a nested relation in its domain?
13781 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13783 if (!map)
13784 return isl_bool_error;
13786 return isl_space_can_curry(map->dim);
13789 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13790 * A -> (B -> C).
13792 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13795 if (!bmap)
13796 return NULL;
13798 if (!isl_basic_map_can_curry(bmap))
13799 isl_die(bmap->ctx, isl_error_invalid,
13800 "basic map cannot be curried", goto error);
13801 bmap = isl_basic_map_cow(bmap);
13802 if (!bmap)
13803 return NULL;
13804 bmap->dim = isl_space_curry(bmap->dim);
13805 if (!bmap->dim)
13806 goto error;
13807 bmap = isl_basic_map_mark_final(bmap);
13808 return bmap;
13809 error:
13810 isl_basic_map_free(bmap);
13811 return NULL;
13814 /* Given a map (A -> B) -> C, return the corresponding map
13815 * A -> (B -> C).
13817 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13819 return isl_map_change_space(map, &isl_map_can_curry,
13820 "map cannot be curried", &isl_space_curry);
13823 /* Can isl_map_range_curry be applied to "map"?
13824 * That is, does it have a nested relation in its range,
13825 * the domain of which is itself a nested relation?
13827 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13829 if (!map)
13830 return isl_bool_error;
13832 return isl_space_can_range_curry(map->dim);
13835 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13836 * A -> (B -> (C -> D)).
13838 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13840 return isl_map_change_space(map, &isl_map_can_range_curry,
13841 "map range cannot be curried",
13842 &isl_space_range_curry);
13845 /* Can we apply isl_basic_map_uncurry to "bmap"?
13846 * That is, does it have a nested relation in its domain?
13848 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13850 if (!bmap)
13851 return isl_bool_error;
13853 return isl_space_can_uncurry(bmap->dim);
13856 /* Can we apply isl_map_uncurry to "map"?
13857 * That is, does it have a nested relation in its domain?
13859 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13861 if (!map)
13862 return isl_bool_error;
13864 return isl_space_can_uncurry(map->dim);
13867 /* Given a basic map A -> (B -> C), return the corresponding basic map
13868 * (A -> B) -> C.
13870 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13873 if (!bmap)
13874 return NULL;
13876 if (!isl_basic_map_can_uncurry(bmap))
13877 isl_die(bmap->ctx, isl_error_invalid,
13878 "basic map cannot be uncurried",
13879 return isl_basic_map_free(bmap));
13880 bmap = isl_basic_map_cow(bmap);
13881 if (!bmap)
13882 return NULL;
13883 bmap->dim = isl_space_uncurry(bmap->dim);
13884 if (!bmap->dim)
13885 return isl_basic_map_free(bmap);
13886 bmap = isl_basic_map_mark_final(bmap);
13887 return bmap;
13890 /* Given a map A -> (B -> C), return the corresponding map
13891 * (A -> B) -> C.
13893 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13895 return isl_map_change_space(map, &isl_map_can_uncurry,
13896 "map cannot be uncurried", &isl_space_uncurry);
13899 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13900 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13902 return isl_map_equate(set, type1, pos1, type2, pos2);
13905 /* Construct a basic map where the given dimensions are equal to each other.
13907 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13908 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13910 isl_basic_map *bmap = NULL;
13911 int i;
13912 isl_size total;
13914 total = isl_space_dim(space, isl_dim_all);
13915 if (total < 0 ||
13916 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13917 isl_space_check_range(space, type2, pos2, 1) < 0)
13918 goto error;
13920 if (type1 == type2 && pos1 == pos2)
13921 return isl_basic_map_universe(space);
13923 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13924 i = isl_basic_map_alloc_equality(bmap);
13925 if (i < 0)
13926 goto error;
13927 isl_seq_clr(bmap->eq[i], 1 + total);
13928 pos1 += isl_basic_map_offset(bmap, type1);
13929 pos2 += isl_basic_map_offset(bmap, type2);
13930 isl_int_set_si(bmap->eq[i][pos1], -1);
13931 isl_int_set_si(bmap->eq[i][pos2], 1);
13932 bmap = isl_basic_map_finalize(bmap);
13933 isl_space_free(space);
13934 return bmap;
13935 error:
13936 isl_space_free(space);
13937 isl_basic_map_free(bmap);
13938 return NULL;
13941 /* Add a constraint imposing that the given two dimensions are equal.
13943 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13944 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13946 isl_basic_map *eq;
13948 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13950 bmap = isl_basic_map_intersect(bmap, eq);
13952 return bmap;
13955 /* Add a constraint imposing that the given two dimensions are equal.
13957 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13958 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13960 isl_basic_map *bmap;
13962 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13964 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13966 return map;
13969 /* Add a constraint imposing that the given two dimensions have opposite values.
13971 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13972 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13974 isl_basic_map *bmap = NULL;
13975 int i;
13976 isl_size total;
13978 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13979 return isl_map_free(map);
13980 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13981 return isl_map_free(map);
13983 total = isl_map_dim(map, isl_dim_all);
13984 if (total < 0)
13985 return isl_map_free(map);
13986 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13987 i = isl_basic_map_alloc_equality(bmap);
13988 if (i < 0)
13989 goto error;
13990 isl_seq_clr(bmap->eq[i], 1 + total);
13991 pos1 += isl_basic_map_offset(bmap, type1);
13992 pos2 += isl_basic_map_offset(bmap, type2);
13993 isl_int_set_si(bmap->eq[i][pos1], 1);
13994 isl_int_set_si(bmap->eq[i][pos2], 1);
13995 bmap = isl_basic_map_finalize(bmap);
13997 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13999 return map;
14000 error:
14001 isl_basic_map_free(bmap);
14002 isl_map_free(map);
14003 return NULL;
14006 /* Construct a constraint imposing that the value of the first dimension is
14007 * greater than or equal to that of the second.
14009 static __isl_give isl_constraint *constraint_order_ge(
14010 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
14011 enum isl_dim_type type2, int pos2)
14013 isl_constraint *c;
14015 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14016 isl_space_check_range(space, type2, pos2, 1) < 0)
14017 space = isl_space_free(space);
14018 if (!space)
14019 return NULL;
14021 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
14023 if (type1 == type2 && pos1 == pos2)
14024 return c;
14026 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
14027 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
14029 return c;
14032 /* Add a constraint imposing that the value of the first dimension is
14033 * greater than or equal to that of the second.
14035 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
14036 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14038 isl_constraint *c;
14039 isl_space *space;
14041 if (type1 == type2 && pos1 == pos2)
14042 return bmap;
14043 space = isl_basic_map_get_space(bmap);
14044 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14045 bmap = isl_basic_map_add_constraint(bmap, c);
14047 return bmap;
14050 /* Add a constraint imposing that the value of the first dimension is
14051 * greater than or equal to that of the second.
14053 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
14054 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14056 isl_constraint *c;
14057 isl_space *space;
14059 if (type1 == type2 && pos1 == pos2)
14060 return map;
14061 space = isl_map_get_space(map);
14062 c = constraint_order_ge(space, type1, pos1, type2, pos2);
14063 map = isl_map_add_constraint(map, c);
14065 return map;
14068 /* Add a constraint imposing that the value of the first dimension is
14069 * less than or equal to that of the second.
14071 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
14072 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14074 return isl_map_order_ge(map, type2, pos2, type1, pos1);
14077 /* Construct a basic map where the value of the first dimension is
14078 * greater than that of the second.
14080 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
14081 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14083 isl_basic_map *bmap = NULL;
14084 int i;
14085 isl_size total;
14087 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
14088 isl_space_check_range(space, type2, pos2, 1) < 0)
14089 goto error;
14091 if (type1 == type2 && pos1 == pos2)
14092 return isl_basic_map_empty(space);
14094 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
14095 total = isl_basic_map_dim(bmap, isl_dim_all);
14096 i = isl_basic_map_alloc_inequality(bmap);
14097 if (total < 0 || i < 0)
14098 return isl_basic_map_free(bmap);
14099 isl_seq_clr(bmap->ineq[i], 1 + total);
14100 pos1 += isl_basic_map_offset(bmap, type1);
14101 pos2 += isl_basic_map_offset(bmap, type2);
14102 isl_int_set_si(bmap->ineq[i][pos1], 1);
14103 isl_int_set_si(bmap->ineq[i][pos2], -1);
14104 isl_int_set_si(bmap->ineq[i][0], -1);
14105 bmap = isl_basic_map_finalize(bmap);
14107 return bmap;
14108 error:
14109 isl_space_free(space);
14110 isl_basic_map_free(bmap);
14111 return NULL;
14114 /* Add a constraint imposing that the value of the first dimension is
14115 * greater than that of the second.
14117 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
14118 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14120 isl_basic_map *gt;
14122 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
14124 bmap = isl_basic_map_intersect(bmap, gt);
14126 return bmap;
14129 /* Add a constraint imposing that the value of the first dimension is
14130 * greater than that of the second.
14132 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
14133 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14135 isl_basic_map *bmap;
14137 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
14139 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
14141 return map;
14144 /* Add a constraint imposing that the value of the first dimension is
14145 * smaller than that of the second.
14147 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
14148 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
14150 return isl_map_order_gt(map, type2, pos2, type1, pos1);
14153 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
14154 int pos)
14156 isl_aff *div;
14157 isl_local_space *ls;
14159 if (!bmap)
14160 return NULL;
14162 if (!isl_basic_map_divs_known(bmap))
14163 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14164 "some divs are unknown", return NULL);
14166 ls = isl_basic_map_get_local_space(bmap);
14167 div = isl_local_space_get_div(ls, pos);
14168 isl_local_space_free(ls);
14170 return div;
14173 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
14174 int pos)
14176 return isl_basic_map_get_div(bset, pos);
14179 /* Plug in "subs" for set dimension "pos" of "set".
14181 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
14182 unsigned pos, __isl_keep isl_aff *subs)
14184 isl_multi_aff *ma;
14186 if (set && isl_set_plain_is_empty(set))
14187 return set;
14189 ma = isl_multi_aff_identity_on_domain_space(isl_set_get_space(set));
14190 ma = isl_multi_aff_set_aff(ma, pos, isl_aff_copy(subs));
14191 return isl_set_preimage_multi_aff(set, ma);
14194 /* Check if the range of "ma" is compatible with the domain or range
14195 * (depending on "type") of "bmap".
14197 static isl_stat check_basic_map_compatible_range_multi_aff(
14198 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
14199 __isl_keep isl_multi_aff *ma)
14201 isl_bool m;
14202 isl_space *ma_space;
14204 ma_space = isl_multi_aff_get_space(ma);
14206 m = isl_space_has_equal_params(bmap->dim, ma_space);
14207 if (m < 0)
14208 goto error;
14209 if (!m)
14210 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14211 "parameters don't match", goto error);
14212 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
14213 if (m < 0)
14214 goto error;
14215 if (!m)
14216 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
14217 "spaces don't match", goto error);
14219 isl_space_free(ma_space);
14220 return isl_stat_ok;
14221 error:
14222 isl_space_free(ma_space);
14223 return isl_stat_error;
14226 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
14227 * coefficients before the transformed range of dimensions,
14228 * the "n_after" coefficients after the transformed range of dimensions
14229 * and the coefficients of the other divs in "bmap".
14231 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
14232 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
14234 int i;
14235 isl_size n_param;
14236 isl_size n_set;
14237 isl_local_space *ls;
14239 if (n_div == 0)
14240 return bmap;
14242 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
14243 n_param = isl_local_space_dim(ls, isl_dim_param);
14244 n_set = isl_local_space_dim(ls, isl_dim_set);
14245 if (n_param < 0 || n_set < 0)
14246 return isl_basic_map_free(bmap);
14248 for (i = 0; i < n_div; ++i) {
14249 int o_bmap = 0, o_ls = 0;
14251 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
14252 o_bmap += 1 + 1 + n_param;
14253 o_ls += 1 + 1 + n_param;
14254 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
14255 o_bmap += n_before;
14256 isl_seq_cpy(bmap->div[i] + o_bmap,
14257 ls->div->row[i] + o_ls, n_set);
14258 o_bmap += n_set;
14259 o_ls += n_set;
14260 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
14261 o_bmap += n_after;
14262 isl_seq_cpy(bmap->div[i] + o_bmap,
14263 ls->div->row[i] + o_ls, n_div);
14264 o_bmap += n_div;
14265 o_ls += n_div;
14266 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
14267 bmap = isl_basic_map_add_div_constraints(bmap, i);
14268 if (!bmap)
14269 goto error;
14272 isl_local_space_free(ls);
14273 return bmap;
14274 error:
14275 isl_local_space_free(ls);
14276 return isl_basic_map_free(bmap);
14279 /* How many stride constraints does "ma" enforce?
14280 * That is, how many of the affine expressions have a denominator
14281 * different from one?
14283 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
14285 int i;
14286 int strides = 0;
14288 for (i = 0; i < ma->n; ++i)
14289 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
14290 strides++;
14292 return strides;
14295 /* For each affine expression in ma of the form
14297 * x_i = (f_i y + h_i)/m_i
14299 * with m_i different from one, add a constraint to "bmap"
14300 * of the form
14302 * f_i y + h_i = m_i alpha_i
14304 * with alpha_i an additional existentially quantified variable.
14306 * The input variables of "ma" correspond to a subset of the variables
14307 * of "bmap". There are "n_before" variables in "bmap" before this
14308 * subset and "n_after" variables after this subset.
14309 * The integer divisions of the affine expressions in "ma" are assumed
14310 * to have been aligned. There are "n_div_ma" of them and
14311 * they appear first in "bmap", straight after the "n_after" variables.
14313 static __isl_give isl_basic_map *add_ma_strides(
14314 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
14315 int n_before, int n_after, int n_div_ma)
14317 int i, k;
14318 int div;
14319 isl_size total;
14320 isl_size n_param;
14321 isl_size n_in;
14323 total = isl_basic_map_dim(bmap, isl_dim_all);
14324 n_param = isl_multi_aff_dim(ma, isl_dim_param);
14325 n_in = isl_multi_aff_dim(ma, isl_dim_in);
14326 if (total < 0 || n_param < 0 || n_in < 0)
14327 return isl_basic_map_free(bmap);
14328 for (i = 0; i < ma->n; ++i) {
14329 int o_bmap = 0, o_ma = 1;
14331 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
14332 continue;
14333 div = isl_basic_map_alloc_div(bmap);
14334 k = isl_basic_map_alloc_equality(bmap);
14335 if (div < 0 || k < 0)
14336 goto error;
14337 isl_int_set_si(bmap->div[div][0], 0);
14338 isl_seq_cpy(bmap->eq[k] + o_bmap,
14339 ma->u.p[i]->v->el + o_ma, 1 + n_param);
14340 o_bmap += 1 + n_param;
14341 o_ma += 1 + n_param;
14342 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
14343 o_bmap += n_before;
14344 isl_seq_cpy(bmap->eq[k] + o_bmap,
14345 ma->u.p[i]->v->el + o_ma, n_in);
14346 o_bmap += n_in;
14347 o_ma += n_in;
14348 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
14349 o_bmap += n_after;
14350 isl_seq_cpy(bmap->eq[k] + o_bmap,
14351 ma->u.p[i]->v->el + o_ma, n_div_ma);
14352 o_bmap += n_div_ma;
14353 o_ma += n_div_ma;
14354 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
14355 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
14356 total++;
14359 return bmap;
14360 error:
14361 isl_basic_map_free(bmap);
14362 return NULL;
14365 /* Replace the domain or range space (depending on "type) of "space" by "set".
14367 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
14368 enum isl_dim_type type, __isl_take isl_space *set)
14370 if (type == isl_dim_in) {
14371 space = isl_space_range(space);
14372 space = isl_space_map_from_domain_and_range(set, space);
14373 } else {
14374 space = isl_space_domain(space);
14375 space = isl_space_map_from_domain_and_range(space, set);
14378 return space;
14381 /* Compute the preimage of the domain or range (depending on "type")
14382 * of "bmap" under the function represented by "ma".
14383 * In other words, plug in "ma" in the domain or range of "bmap".
14384 * The result is a basic map that lives in the same space as "bmap"
14385 * except that the domain or range has been replaced by
14386 * the domain space of "ma".
14388 * If bmap is represented by
14390 * A(p) + S u + B x + T v + C(divs) >= 0,
14392 * where u and x are input and output dimensions if type == isl_dim_out
14393 * while x and v are input and output dimensions if type == isl_dim_in,
14394 * and ma is represented by
14396 * x = D(p) + F(y) + G(divs')
14398 * then the result is
14400 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
14402 * The divs in the input set are similarly adjusted.
14403 * In particular
14405 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
14407 * becomes
14409 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
14410 * B_i G(divs') + c_i(divs))/n_i)
14412 * If bmap is not a rational map and if F(y) involves any denominators
14414 * x_i = (f_i y + h_i)/m_i
14416 * then additional constraints are added to ensure that we only
14417 * map back integer points. That is we enforce
14419 * f_i y + h_i = m_i alpha_i
14421 * with alpha_i an additional existentially quantified variable.
14423 * We first copy over the divs from "ma".
14424 * Then we add the modified constraints and divs from "bmap".
14425 * Finally, we add the stride constraints, if needed.
14427 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
14428 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
14429 __isl_take isl_multi_aff *ma)
14431 int i, k;
14432 isl_space *space;
14433 isl_basic_map *res = NULL;
14434 isl_size n_before, n_after, n_div_bmap, n_div_ma;
14435 isl_int f, c1, c2, g;
14436 isl_bool rational;
14437 int strides;
14439 isl_int_init(f);
14440 isl_int_init(c1);
14441 isl_int_init(c2);
14442 isl_int_init(g);
14444 ma = isl_multi_aff_align_divs(ma);
14445 if (!bmap || !ma)
14446 goto error;
14447 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
14448 goto error;
14450 if (type == isl_dim_in) {
14451 n_before = 0;
14452 n_after = isl_basic_map_dim(bmap, isl_dim_out);
14453 } else {
14454 n_before = isl_basic_map_dim(bmap, isl_dim_in);
14455 n_after = 0;
14457 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
14458 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
14459 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
14460 goto error;
14462 space = isl_multi_aff_get_domain_space(ma);
14463 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
14464 rational = isl_basic_map_is_rational(bmap);
14465 strides = rational ? 0 : multi_aff_strides(ma);
14466 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
14467 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
14468 if (rational)
14469 res = isl_basic_map_set_rational(res);
14471 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
14472 if (isl_basic_map_alloc_div(res) < 0)
14473 goto error;
14475 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
14476 if (!res)
14477 goto error;
14479 for (i = 0; i < bmap->n_eq; ++i) {
14480 k = isl_basic_map_alloc_equality(res);
14481 if (k < 0)
14482 goto error;
14483 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
14484 n_after, n_div_ma, n_div_bmap,
14485 f, c1, c2, g, 0) < 0)
14486 goto error;
14489 for (i = 0; i < bmap->n_ineq; ++i) {
14490 k = isl_basic_map_alloc_inequality(res);
14491 if (k < 0)
14492 goto error;
14493 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
14494 n_after, n_div_ma, n_div_bmap,
14495 f, c1, c2, g, 0) < 0)
14496 goto error;
14499 for (i = 0; i < bmap->n_div; ++i) {
14500 if (isl_int_is_zero(bmap->div[i][0])) {
14501 isl_int_set_si(res->div[n_div_ma + i][0], 0);
14502 continue;
14504 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
14505 n_before, n_after, n_div_ma, n_div_bmap,
14506 f, c1, c2, g, 1) < 0)
14507 goto error;
14510 if (strides)
14511 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
14513 isl_int_clear(f);
14514 isl_int_clear(c1);
14515 isl_int_clear(c2);
14516 isl_int_clear(g);
14517 isl_basic_map_free(bmap);
14518 isl_multi_aff_free(ma);
14519 res = isl_basic_map_simplify(res);
14520 return isl_basic_map_finalize(res);
14521 error:
14522 isl_int_clear(f);
14523 isl_int_clear(c1);
14524 isl_int_clear(c2);
14525 isl_int_clear(g);
14526 isl_basic_map_free(bmap);
14527 isl_multi_aff_free(ma);
14528 isl_basic_map_free(res);
14529 return NULL;
14532 /* Compute the preimage of "bset" under the function represented by "ma".
14533 * In other words, plug in "ma" in "bset". The result is a basic set
14534 * that lives in the domain space of "ma".
14536 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
14537 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
14539 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
14542 /* Compute the preimage of the domain of "bmap" under the function
14543 * represented by "ma".
14544 * In other words, plug in "ma" in the domain of "bmap".
14545 * The result is a basic map that lives in the same space as "bmap"
14546 * except that the domain has been replaced by the domain space of "ma".
14548 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
14549 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14551 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
14554 /* Compute the preimage of the range of "bmap" under the function
14555 * represented by "ma".
14556 * In other words, plug in "ma" in the range of "bmap".
14557 * The result is a basic map that lives in the same space as "bmap"
14558 * except that the range has been replaced by the domain space of "ma".
14560 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
14561 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
14563 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
14566 /* Check if the range of "ma" is compatible with the domain or range
14567 * (depending on "type") of "map".
14568 * Return isl_stat_error if anything is wrong.
14570 static isl_stat check_map_compatible_range_multi_aff(
14571 __isl_keep isl_map *map, enum isl_dim_type type,
14572 __isl_keep isl_multi_aff *ma)
14574 isl_bool m;
14575 isl_space *ma_space;
14577 ma_space = isl_multi_aff_get_space(ma);
14578 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
14579 isl_space_free(ma_space);
14580 if (m < 0)
14581 return isl_stat_error;
14582 if (!m)
14583 isl_die(isl_map_get_ctx(map), isl_error_invalid,
14584 "spaces don't match", return isl_stat_error);
14585 return isl_stat_ok;
14588 /* Compute the preimage of the domain or range (depending on "type")
14589 * of "map" under the function represented by "ma".
14590 * In other words, plug in "ma" in the domain or range of "map".
14591 * The result is a map that lives in the same space as "map"
14592 * except that the domain or range has been replaced by
14593 * the domain space of "ma".
14595 * The parameters are assumed to have been aligned.
14597 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14598 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14600 int i;
14601 isl_space *space;
14603 map = isl_map_cow(map);
14604 ma = isl_multi_aff_align_divs(ma);
14605 if (!map || !ma)
14606 goto error;
14607 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14608 goto error;
14610 for (i = 0; i < map->n; ++i) {
14611 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14612 isl_multi_aff_copy(ma));
14613 if (!map->p[i])
14614 goto error;
14617 space = isl_multi_aff_get_domain_space(ma);
14618 space = isl_space_set(isl_map_get_space(map), type, space);
14620 isl_space_free(isl_map_take_space(map));
14621 map = isl_map_restore_space(map, space);
14622 if (!map)
14623 goto error;
14625 isl_multi_aff_free(ma);
14626 if (map->n > 1)
14627 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14628 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14629 return map;
14630 error:
14631 isl_multi_aff_free(ma);
14632 isl_map_free(map);
14633 return NULL;
14636 /* Compute the preimage of the domain or range (depending on "type")
14637 * of "map" under the function represented by "ma".
14638 * In other words, plug in "ma" in the domain or range of "map".
14639 * The result is a map that lives in the same space as "map"
14640 * except that the domain or range has been replaced by
14641 * the domain space of "ma".
14643 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14644 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14646 isl_bool aligned;
14648 if (!map || !ma)
14649 goto error;
14651 aligned = isl_map_space_has_equal_params(map, ma->space);
14652 if (aligned < 0)
14653 goto error;
14654 if (aligned)
14655 return map_preimage_multi_aff(map, type, ma);
14657 if (isl_map_check_named_params(map) < 0)
14658 goto error;
14659 if (!isl_space_has_named_params(ma->space))
14660 isl_die(map->ctx, isl_error_invalid,
14661 "unaligned unnamed parameters", goto error);
14662 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14663 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14665 return map_preimage_multi_aff(map, type, ma);
14666 error:
14667 isl_multi_aff_free(ma);
14668 return isl_map_free(map);
14671 /* Compute the preimage of "set" under the function represented by "ma".
14672 * In other words, plug in "ma" in "set". The result is a set
14673 * that lives in the domain space of "ma".
14675 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14676 __isl_take isl_multi_aff *ma)
14678 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14681 /* Compute the preimage of the domain of "map" under the function
14682 * represented by "ma".
14683 * In other words, plug in "ma" in the domain of "map".
14684 * The result is a map that lives in the same space as "map"
14685 * except that the domain has been replaced by the domain space of "ma".
14687 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14688 __isl_take isl_multi_aff *ma)
14690 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14693 /* Compute the preimage of the range of "map" under the function
14694 * represented by "ma".
14695 * In other words, plug in "ma" in the range of "map".
14696 * The result is a map that lives in the same space as "map"
14697 * except that the range has been replaced by the domain space of "ma".
14699 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14700 __isl_take isl_multi_aff *ma)
14702 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14705 /* Compute the preimage of "map" under the function represented by "pma".
14706 * In other words, plug in "pma" in the domain or range of "map".
14707 * The result is a map that lives in the same space as "map",
14708 * except that the space of type "type" has been replaced by
14709 * the domain space of "pma".
14711 * The parameters of "map" and "pma" are assumed to have been aligned.
14713 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14714 __isl_take isl_map *map, enum isl_dim_type type,
14715 __isl_take isl_pw_multi_aff *pma)
14717 int i;
14718 isl_map *res;
14720 if (!pma)
14721 goto error;
14723 if (pma->n == 0) {
14724 isl_space *space;
14726 space = isl_pw_multi_aff_get_domain_space(pma);
14727 isl_pw_multi_aff_free(pma);
14728 space = isl_space_set(isl_map_get_space(map), type, space);
14729 isl_map_free(map);
14730 return isl_map_empty(space);
14733 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14734 isl_multi_aff_copy(pma->p[0].maff));
14735 if (type == isl_dim_in)
14736 res = isl_map_intersect_domain(res,
14737 isl_map_copy(pma->p[0].set));
14738 else
14739 res = isl_map_intersect_range(res,
14740 isl_map_copy(pma->p[0].set));
14742 for (i = 1; i < pma->n; ++i) {
14743 isl_map *res_i;
14745 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14746 isl_multi_aff_copy(pma->p[i].maff));
14747 if (type == isl_dim_in)
14748 res_i = isl_map_intersect_domain(res_i,
14749 isl_map_copy(pma->p[i].set));
14750 else
14751 res_i = isl_map_intersect_range(res_i,
14752 isl_map_copy(pma->p[i].set));
14753 res = isl_map_union(res, res_i);
14756 isl_pw_multi_aff_free(pma);
14757 isl_map_free(map);
14758 return res;
14759 error:
14760 isl_pw_multi_aff_free(pma);
14761 isl_map_free(map);
14762 return NULL;
14765 /* Compute the preimage of "map" under the function represented by "pma".
14766 * In other words, plug in "pma" in the domain or range of "map".
14767 * The result is a map that lives in the same space as "map",
14768 * except that the space of type "type" has been replaced by
14769 * the domain space of "pma".
14771 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14772 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14774 isl_bool aligned;
14776 if (!map || !pma)
14777 goto error;
14779 aligned = isl_map_space_has_equal_params(map, pma->dim);
14780 if (aligned < 0)
14781 goto error;
14782 if (aligned)
14783 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14785 if (isl_map_check_named_params(map) < 0)
14786 goto error;
14787 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14788 goto error;
14789 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14790 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14792 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14793 error:
14794 isl_pw_multi_aff_free(pma);
14795 return isl_map_free(map);
14798 /* Compute the preimage of "set" under the function represented by "pma".
14799 * In other words, plug in "pma" in "set". The result is a set
14800 * that lives in the domain space of "pma".
14802 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14803 __isl_take isl_pw_multi_aff *pma)
14805 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14808 /* Compute the preimage of the domain of "map" under the function
14809 * represented by "pma".
14810 * In other words, plug in "pma" in the domain of "map".
14811 * The result is a map that lives in the same space as "map",
14812 * except that domain space has been replaced by the domain space of "pma".
14814 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14815 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14817 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14820 /* Compute the preimage of the range of "map" under the function
14821 * represented by "pma".
14822 * In other words, plug in "pma" in the range of "map".
14823 * The result is a map that lives in the same space as "map",
14824 * except that range space has been replaced by the domain space of "pma".
14826 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14827 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14829 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14832 /* Compute the preimage of "map" under the function represented by "mpa".
14833 * In other words, plug in "mpa" in the domain or range of "map".
14834 * The result is a map that lives in the same space as "map",
14835 * except that the space of type "type" has been replaced by
14836 * the domain space of "mpa".
14838 * If the map does not involve any constraints that refer to the
14839 * dimensions of the substituted space, then the only possible
14840 * effect of "mpa" on the map is to map the space to a different space.
14841 * We create a separate isl_multi_aff to effectuate this change
14842 * in order to avoid spurious splitting of the map along the pieces
14843 * of "mpa".
14844 * If "mpa" has a non-trivial explicit domain, however,
14845 * then the full substitution should be performed.
14847 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14848 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14850 isl_size n;
14851 isl_bool full;
14852 isl_pw_multi_aff *pma;
14854 n = isl_map_dim(map, type);
14855 if (n < 0 || !mpa)
14856 goto error;
14858 full = isl_map_involves_dims(map, type, 0, n);
14859 if (full >= 0 && !full)
14860 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14861 if (full < 0)
14862 goto error;
14863 if (!full) {
14864 isl_space *space;
14865 isl_multi_aff *ma;
14867 space = isl_multi_pw_aff_get_space(mpa);
14868 isl_multi_pw_aff_free(mpa);
14869 ma = isl_multi_aff_zero(space);
14870 return isl_map_preimage_multi_aff(map, type, ma);
14873 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14874 return isl_map_preimage_pw_multi_aff(map, type, pma);
14875 error:
14876 isl_map_free(map);
14877 isl_multi_pw_aff_free(mpa);
14878 return NULL;
14881 /* Compute the preimage of "map" under the function represented by "mpa".
14882 * In other words, plug in "mpa" in the domain "map".
14883 * The result is a map that lives in the same space as "map",
14884 * except that domain space has been replaced by the domain space of "mpa".
14886 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14887 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14889 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14892 /* Compute the preimage of "set" by the function represented by "mpa".
14893 * In other words, plug in "mpa" in "set".
14895 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14896 __isl_take isl_multi_pw_aff *mpa)
14898 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14901 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14902 * on the output dimension "pos" in terms of the parameters,
14903 * the input dimensions and possibly some integer divisions,
14904 * but not any other output dimensions, extract this upper bound
14905 * as a function of all dimensions (with zero coefficients
14906 * for the output dimensions).
14908 * That is, the inequality is of the form
14910 * e(...) + c - m x >= 0
14912 * where e does not depend on any other output dimensions.
14913 * Return (e(...) + c) / m, with the denominator m in the first position.
14915 __isl_give isl_vec *isl_basic_map_inequality_extract_output_upper_bound(
14916 __isl_keep isl_basic_map *bmap, int ineq, int pos)
14918 isl_ctx *ctx;
14919 isl_size v_out, total;
14921 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
14922 total = isl_basic_map_dim(bmap, isl_dim_all);
14923 if (v_out < 0 || total < 0)
14924 return NULL;
14925 ctx = isl_basic_map_get_ctx(bmap);
14926 return extract_bound_from_constraint(ctx, bmap->ineq[ineq],
14927 total, v_out + pos);
14930 /* Is constraint "c" of "bmap" of the form
14932 * e(...) + c1 - m x >= 0
14934 * or
14936 * -e(...) + c2 + m x >= 0
14938 * where m > 1 and e does not involve any other output variables?
14940 * "v_out" is the offset to the output variables.
14941 * "d" is the position of x among the output variables.
14942 * "v_div" is the offset to the local variables.
14943 * "total" is the total number of variables.
14945 * Since the purpose of this function is to use the constraint
14946 * to express the output variable as an integer division,
14947 * do not allow the constraint to involve any local variables
14948 * that do not have an explicit representation.
14950 static isl_bool is_potential_div_constraint(__isl_keep isl_basic_map *bmap,
14951 isl_int *c, int v_out, int d, int v_div, int total)
14953 int i = 0;
14955 if (isl_int_is_zero(c[1 + v_out + d]))
14956 return isl_bool_false;
14957 if (isl_int_is_one(c[1 + v_out + d]))
14958 return isl_bool_false;
14959 if (isl_int_is_negone(c[1 + v_out + d]))
14960 return isl_bool_false;
14961 if (isl_seq_any_non_zero(c + 1 + v_out, d))
14962 return isl_bool_false;
14963 if (isl_seq_any_non_zero(c + 1 + v_out + d + 1, v_div - (v_out + d + 1)))
14964 return isl_bool_false;
14965 for (i = 0; v_div + i < total; ++i) {
14966 isl_bool known, involves;
14968 if (isl_int_is_zero(c[1 + v_div + i]))
14969 continue;
14970 known = isl_basic_map_div_is_known(bmap, i);
14971 if (known < 0 || !known)
14972 return known;
14973 involves = div_involves_vars(bmap, i, v_out, v_div - v_out);
14974 if (involves < 0 || involves)
14975 return isl_bool_not(involves);
14977 return isl_bool_true;
14980 /* Look for a pair of constraints
14982 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14984 * and
14986 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14988 * that express that the output dimension x at position "pos"
14989 * is some integer division of an expression in terms of the parameters,
14990 * input dimensions and integer divisions.
14991 * If such a pair can be found, then return the index
14992 * of the upper bound constraint, m x <= e(...) + c1.
14993 * Otherwise, return an index beyond the number of constraints.
14995 * The constraints of "bmap" are assumed to have been sorted.
14997 * In order for the constraints above to express an integer division,
14998 * m needs to be greater than 1 and such that
15000 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15002 * In particular, this ensures that
15004 * x = floor((e(...) + c1) / m)
15006 isl_size isl_basic_map_find_output_upper_div_constraint(
15007 __isl_keep isl_basic_map *bmap, int pos)
15009 int i;
15010 isl_size j;
15011 isl_size n_ineq;
15012 isl_size v_out, v_div;
15013 isl_size total;
15014 isl_int sum;
15016 total = isl_basic_map_dim(bmap, isl_dim_all);
15017 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15018 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15019 n_ineq = isl_basic_map_n_inequality(bmap);
15020 if (total < 0 || v_out < 0 || v_div < 0 || n_ineq < 0)
15021 return isl_size_error;
15023 isl_int_init(sum);
15024 for (i = 0; i < n_ineq; ++i) {
15025 isl_bool potential;
15027 potential = is_potential_div_constraint(bmap, bmap->ineq[i],
15028 v_out, pos, v_div, total);
15029 if (potential < 0)
15030 goto error;
15031 if (!potential)
15032 continue;
15034 j = find_later_constraint_in_pair(bmap, i, v_out + pos, total,
15035 bmap->ineq[i][1 + v_out + pos], &sum);
15036 if (j < 0)
15037 goto error;
15038 if (j < n_ineq)
15039 break;
15041 isl_int_clear(sum);
15043 if (i >= n_ineq)
15044 return n_ineq;
15045 if (isl_int_is_pos(bmap->ineq[j][1 + v_out + pos]))
15046 return i;
15047 else
15048 return j;
15049 error:
15050 isl_int_clear(sum);
15051 return isl_size_error;
15054 /* Look for a pair of constraints
15056 * -g(x) + i >= 0
15057 * g(x) - i + d >= 0
15059 * in "bmap" on the output dimension at position "pos" such that
15060 * g(x) is an expression in the parameters and input dimensions, and
15061 * d < "n".
15062 * Return the index of the first constraint if such a pair can be found.
15063 * Otherwise, return an index beyond the number of inequality constraints.
15065 * The constraints are assumed to have been sorted.
15067 static isl_size find_output_lower_mod_constraint(__isl_keep isl_basic_map *bmap,
15068 int pos, isl_int n)
15070 int i;
15071 isl_size n_ineq;
15072 isl_size v_out, n_out;
15073 isl_size v_div, n_div;
15074 isl_size j;
15075 isl_int tmp;
15077 n_ineq = isl_basic_map_n_inequality(bmap);
15078 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15079 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15080 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15081 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15082 if (n_ineq < 0 || v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15083 return isl_size_error;
15085 isl_int_init(tmp);
15086 for (i = n_ineq - 1; i >= 0; --i) {
15087 if (!isl_int_is_one(bmap->ineq[i][1 + v_out + pos]) &&
15088 !isl_int_is_negone(bmap->ineq[i][1 + v_out + pos]))
15089 continue;
15090 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + v_div, n_div))
15091 continue;
15092 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + v_out, pos))
15093 continue;
15094 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + v_out + pos + 1,
15095 n_out - (pos + 1)))
15096 continue;
15097 j = find_earlier_constraint_in_pair(bmap, i, v_out + pos, v_div,
15098 n, &tmp);
15099 if (j >= n_ineq)
15100 continue;
15101 isl_int_clear(tmp);
15102 if (j < 0)
15103 return j;
15104 if (isl_int_is_one(bmap->ineq[i][1 + v_out + pos]))
15105 return i;
15106 return j;
15109 isl_int_clear(tmp);
15110 return n_ineq;
15113 /* Turn "bmap" into a basic set for constructing an affine expression
15114 * that can later be plugged into an output dimension of "bmap".
15115 * "is_set" is set if "bmap" is actually a basic set already.
15117 * If "bmap" is a basic set already, then simply cast it.
15118 * Otherwise, wrap "bmap" into a basic set.
15120 static __isl_give isl_basic_set *wrap_for_plug_in(
15121 __isl_take isl_basic_map *bmap, int is_set)
15123 if (is_set)
15124 return bset_from_bmap(bmap);
15125 else
15126 return isl_basic_map_wrap(bmap);
15129 /* Given an affine expression "aff" that was constructed on top of
15130 * the result of wrap_for_plug_in and that does not depend on the output space,
15131 * project out this output space.
15132 * "is_set" is the same as in the call to wrap_for_plug_in.
15134 * If "is_set" is set then the output space is actually the set space
15135 * of the original basic set and this appears as the domain of "aff".
15136 * Otherwise, the output space appears as the range in the nested domain.
15138 static __isl_give isl_aff *unwrap_plug_in(__isl_take isl_aff *aff, int is_set)
15140 if (is_set)
15141 return isl_aff_project_domain_on_params(aff);
15142 else
15143 return isl_aff_domain_factor_domain(aff);
15146 /* Given that equality "eq" of "bset" expresses
15147 * the variable at position "pos" in terms of the other variables,
15148 * extract this expression as a function of those other variables,
15149 * excluding any local variables.
15151 static __isl_give isl_aff *extract_expr_aff(
15152 __isl_keep isl_basic_set *bset, int eq, int pos)
15154 if (!bset)
15155 return NULL;
15156 return extract_aff(bset, bset->eq[eq], pos);
15159 /* Given an integer division "div" of the form
15161 * (f(x) + a i)//m
15163 * with i the variable at position "pos" and f not involving
15164 * any local variables, return the expression
15166 * -f(x)/a
15168 static __isl_give isl_aff *extract_div_expr_aff(
15169 __isl_keep isl_basic_set *bset, int div, int pos)
15171 if (!bset)
15172 return NULL;
15173 return extract_aff(bset, bset->div[div] + 1, pos);
15176 /* Given the presence in "bmap" of an equality constraint "eq"
15178 * f(x) + t m i + m n h(y) = 0
15180 * and a pair of inequality constraints
15182 * -g(x) + i >= 0 ("lower")
15183 * g(x) - i + d >= 0
15185 * where i is the output dimension at position "pos",
15186 * f(x) and g(x) are expressions in the parameters and
15187 * input dimensions (if any),
15188 * h(y) is an expression in the other output dimensions,
15189 * t is +1 or -1, and
15190 * d < "n",
15191 * return the expression
15193 * (-t f(x)/m - g(x)) mod n + g(x)
15195 * The expression is first constructed in terms of the wrapped space of "bmap".
15196 * Since this expression does not depend on the original output dimensions,
15197 * they can be removed, resulting in an expression in terms
15198 * of the input dimensions.
15199 * If "bmap" is actually a set, then the resulting expression has no domain.
15201 * Also note that the "f" variable contains "-t (f(x)/m + n h(y))"
15202 * but the "n h(y)" part is removed by taking the modulo with respect to n.
15204 static __isl_give isl_aff *construct_mod(__isl_keep isl_basic_map *bmap,
15205 int pos, int eq, int lower, isl_int n)
15207 isl_ctx *ctx;
15208 isl_basic_set *bset;
15209 isl_aff *f, *g, *mod;
15210 isl_val *v;
15211 isl_bool is_set;
15213 is_set = isl_basic_map_is_set(bmap);
15214 if (is_set < 0)
15215 return NULL;
15216 ctx = isl_basic_map_get_ctx(bmap);
15218 bset = wrap_for_plug_in(isl_basic_map_copy(bmap), is_set);
15219 g = extract_lower_bound_aff(bset, lower, pos);
15220 f = extract_expr_aff(bset, eq, pos);
15221 isl_basic_set_free(bset);
15223 mod = isl_aff_sub(f, isl_aff_copy(g));
15224 v = isl_val_int_from_isl_int(ctx, n);
15225 mod = isl_aff_mod_val(mod, v);
15226 mod = isl_aff_add(mod, g);
15227 mod = unwrap_plug_in(mod, is_set);
15229 return mod;
15232 /* Look for a combination of constraints in "bmap" (including
15233 * an equality constraint) that ensure
15234 * that output dimension "pos" is equal to some modulo expression
15235 * in the parameters and input dimensions and
15236 * return this expression if found.
15238 * The constraints are assumed to have been sorted.
15240 * In particular look for an equality constraint
15242 * f(x) + t m i + m n h(y) = 0
15244 * and a pair of inequality constraints
15246 * -g(x) + i >= 0
15247 * g(x) - i + d >= 0
15249 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15250 * h(y) is an expression in local variables and other output dimensions,
15251 * t is +1 or -1, and
15252 * d < n.
15254 * If such a combination of constraints can be found
15255 * then
15257 * i = -t f(x)/m - t n h(y)
15258 * i - g(x) = -t f(x)/m - t n h(y) - g(x)
15260 * and so, because 0 <= i - g(x) <= d < n,
15262 * i - g(x) = (-t f(x)/m - t n h(y) - g(x)) mod n
15263 * = (-t f(x)/m - g(x)) mod n
15265 * That is,
15267 * i = (-t f(x)/m - g(x)) mod n + g(x)
15269 static __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_mod_eq(
15270 __isl_keep isl_basic_map *bmap, int pos)
15272 int i, j;
15273 isl_size n_eq, n_ineq;
15274 isl_size v_out, n_out;
15275 isl_size v_div, n_div;
15276 isl_size lower;
15277 isl_int gcd;
15278 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15280 n_eq = isl_basic_map_n_equality(bmap);
15281 n_ineq = isl_basic_map_n_inequality(bmap);
15282 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15283 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15284 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15285 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15286 if (n_eq < 0 || n_ineq < 0 ||
15287 v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15288 goto error;
15289 if (n_eq == 0 || n_ineq < 2 || n_out == 1)
15290 return res;
15292 isl_int_init(gcd);
15293 for (i = 0; i < n_eq; ++i) {
15294 if (isl_int_is_zero(bmap->eq[i][1 + v_out + pos]))
15295 continue;
15296 isl_seq_gcd(bmap->eq[i] + 1 + v_div, n_div, &gcd);
15297 for (j = 0; j < n_out; ++j) {
15298 if (j == pos)
15299 continue;
15300 isl_int_gcd(gcd, gcd, bmap->eq[i][1 + v_out + j]);
15302 if (!isl_int_abs_gt(gcd, bmap->eq[i][1 + v_out + pos]))
15303 continue;
15304 if (!isl_int_is_divisible_by(gcd, bmap->eq[i][1 + v_out + pos]))
15305 continue;
15306 isl_int_divexact(gcd, gcd, bmap->eq[i][1 + v_out + pos]);
15307 isl_int_abs(gcd, gcd);
15308 lower = find_output_lower_mod_constraint(bmap, pos, gcd);
15309 if (lower >= n_ineq)
15310 continue;
15311 res.valid = isl_bool_true;
15312 if (lower >= 0)
15313 res.value = construct_mod(bmap, v_out + pos, i,
15314 lower, gcd);
15315 if (!res.value)
15316 res.valid = isl_bool_error;
15317 isl_int_clear(gcd);
15318 return res;
15321 isl_int_clear(gcd);
15322 return res;
15323 error:
15324 res.valid = isl_bool_error;
15325 return res;
15328 /* Given that the integer division "div" of "bmap" is of the form
15330 * (f(x) + t m i)//(m n)
15332 * with t equal to 1 or -1 and n positive,
15333 * while the inequality constraint "lower" is of the form
15335 * -g(x) + i >= 0
15337 * with neither involving any local variables or output dimensions other than i,
15338 * construct the expression
15340 * (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15342 * "pos" is the position of the variable i.
15343 * "t" and "n" are the values t and n.
15345 * The expression is first constructed in terms of the wrapped space of "bmap".
15346 * Since this expression does not depend on the original output dimensions,
15347 * these can be removed, resulting in an expression in terms
15348 * of the input dimensions.
15349 * If "bmap" is actually a set, then the resulting expression has no domain.
15351 * First obtain the expressions
15353 * -t f(x)/m
15354 * g(x)
15356 * multiply the first with -t, take the floor and multiply with -t again,
15357 * to obtain
15359 * -t f(x)//m
15361 * Add t (n - 1) and subtract g(x) to obtain
15363 * t (n - 1) - t f(x)//m - g(x)
15365 * Finally, compute the modulo with respect to m and add g(x) back.
15367 static __isl_give isl_aff *construct_mod_ineq(__isl_keep isl_basic_map *bmap,
15368 int pos, int div, int lower, int t, isl_int n)
15370 isl_ctx *ctx;
15371 isl_basic_set *bset;
15372 isl_aff *f, *g;
15373 isl_val *v;
15374 isl_bool is_set;
15376 is_set = isl_basic_map_is_set(bmap);
15377 if (is_set < 0)
15378 return NULL;
15379 ctx = isl_basic_map_get_ctx(bmap);
15381 bset = wrap_for_plug_in(isl_basic_map_copy(bmap), is_set);
15382 f = extract_div_expr_aff(bset, div, pos);
15383 g = extract_lower_bound_aff(bset, lower, pos);
15384 isl_basic_set_free(bset);
15386 if (t > 0)
15387 isl_aff_neg(f);
15388 f = isl_aff_floor(f);
15389 if (t > 0)
15390 isl_aff_neg(f);
15392 v = isl_val_int_from_isl_int(ctx, n);
15393 v = isl_val_sub_ui(v, 1);
15394 if (t < 0)
15395 v = isl_val_neg(v);
15396 f = isl_aff_add_constant_val(f, v);
15398 f = isl_aff_sub(f, isl_aff_copy(g));
15399 v = isl_val_int_from_isl_int(ctx, n);
15400 f = isl_aff_mod_val(f, v);
15401 f = isl_aff_add(f, g);
15403 f = unwrap_plug_in(f, is_set);
15405 return f;
15408 /* Given that inequality constraint "ineq" of "bmap" is of the form
15410 * f(x) + t m i - m n e + c >= 0
15412 * with integer division "e" of the form
15414 * e = (f(x) + t m i)//(m n)
15416 * f(x) an expression in the parameters and input dimensions and
15417 * t is +1 or -1,
15418 * check whether c <= -(n - 1) m and then look for
15419 * a pair of inequality constraints
15421 * -g(x) + i >= 0
15422 * g(x) - i + d >= 0
15424 * where g(x) is an expression in the parameters and input dimensions and
15425 * d < n.
15426 * If successful, return the expression
15428 * (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15430 * "v_out" is the position of the output dimensions among the variables.
15431 * "pos" is the position of the output dimension i.
15432 * "n_ineq" is the number of inequality constraints.
15433 * "v_div" is the position of the local dimensions among the variables.
15436 * First extract the constant value "n".
15437 * If c' is the constant term of f(x), then the constant term
15438 * of the inequality constraint is c' + c.
15439 * Check that c' + c <= c' - (n - 1) m.
15440 * If so, and if the pair of inequality constraints can be found,
15441 * then extract the expression above.
15443 static __isl_give isl_maybe_isl_aff try_find_output_mod_ineq_at(
15444 __isl_keep isl_basic_map *bmap, int v_out, int pos,
15445 int ineq, int n_ineq, int v_div, int e)
15447 isl_int n, t;
15448 isl_size lower;
15449 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15451 isl_int_init(n);
15452 isl_int_init(t);
15454 isl_int_divexact(n, bmap->ineq[ineq][1 + v_div + e],
15455 bmap->ineq[ineq][1 + v_out + pos]);
15456 isl_int_abs(n, n);
15457 isl_int_sub_ui(t, n, 1);
15458 isl_int_mul(t, t, bmap->ineq[ineq][1 + v_out + pos]);
15459 isl_int_abs(t, t);
15460 isl_int_neg(t, t);
15461 isl_int_add(t, t, bmap->div[e][1]);
15462 if (isl_int_le(bmap->ineq[ineq][0], t)) {
15463 lower = find_output_lower_mod_constraint(bmap, pos, n);
15464 if (lower < n_ineq) {
15465 int t;
15467 t = isl_int_sgn(bmap->ineq[ineq][1 + v_out + pos]);
15468 res.valid = isl_bool_true;
15469 res.value = construct_mod_ineq(bmap, v_out + pos,
15470 e, lower, t, n);
15474 isl_int_clear(t);
15475 isl_int_clear(n);
15477 return res;
15480 /* In the sequence of length "len" starting at "p",
15481 * is the element at "pos" the only non-zero element?
15483 static int only_non_zero(isl_int *p, unsigned pos, unsigned len)
15485 if (isl_int_is_zero(p[pos]))
15486 return 0;
15487 return unique(p, pos, len);
15490 /* Look for a combination of inequality constraints in "bmap" that ensure
15491 * that output dimension "pos" is equal to some modulo expression
15492 * in the parameters and input dimensions and
15493 * return this expression if found.
15495 * The constraints are assumed to have been sorted.
15497 * In particular, look for an integer division of the form
15499 * e = (f(x) + t m i)//(m n)
15501 * with a corresponding inequality constraint
15503 * f(x) + t m i - m n e + c >= 0 (*)
15505 * and a pair of inequality constraints
15507 * -g(x) + i >= 0
15508 * g(x) - i + d >= 0
15510 * where f(x) and g(x) are expressions in the parameters and input dimensions,
15511 * c <= -(n - 1) m
15512 * t is +1 or -1, and
15513 * d < n.
15515 * Note that
15517 * e = (f(x) + t m i)//(m n) = (f(x)//m + t i)//n
15519 * and so
15521 * f(x)//m + t i - (n - 1) <= n e <= f(x)//m + t i
15523 * and in particular
15525 * f(x)//m <= - t i + n e + (n - 1)
15527 * Constraint (*) implies
15529 * f(x) >= -t m i + m n e - c >= -t m i + m n e + (n - 1) m
15531 * and so
15533 * f(x)//m >= - t i + n e + (n - 1)
15535 * Together, this implies
15537 * f(x)//m = -t i + n e + (n - 1)
15538 * t i = n e + (n - 1) - f(x)//m
15539 * i = t n e + t (n - 1) - t f(x)//m
15540 * i - g(x) = t n e + t (n - 1) - t f(x)//m - g(x)
15542 * and so, because 0 <= i - g(x) <= d < n,
15544 * i - g(x) = (t (n - 1) - t f(x)//m - g(x)) mod n
15546 * That is,
15548 * i = (t (n - 1) - t f(x)//m - g(x)) mod n + g(x)
15551 * First look for a suitable inequality constraint for (*),
15552 * with the corresponding integer division.
15553 * If any such combination is found, look for an appropriate g(x) and
15554 * construct the corresponding expression in try_find_output_mod_ineq_at.
15556 static __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_mod_ineq(
15557 __isl_keep isl_basic_map *bmap, int pos)
15559 int i;
15560 isl_size n_ineq;
15561 isl_size v_out, n_out;
15562 isl_size v_div, n_div;
15563 isl_maybe_isl_aff no = { isl_bool_false, NULL };
15564 isl_maybe_isl_aff error = { isl_bool_error, NULL };
15566 n_ineq = isl_basic_map_n_inequality(bmap);
15567 v_out = isl_basic_map_var_offset(bmap, isl_dim_out);
15568 n_out = isl_basic_map_dim(bmap, isl_dim_out);
15569 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
15570 n_div = isl_basic_map_dim(bmap, isl_dim_div);
15571 if (n_ineq < 0 || v_out < 0 || n_out < 0 || v_div < 0 || n_div < 0)
15572 return error;
15573 if (n_div < 1 || n_ineq < 4)
15574 return no;
15576 for (i = 0; i < n_ineq; ++i) {
15577 isl_bool unknown;
15578 int e;
15580 if (!only_non_zero(bmap->ineq[i] + 1 + v_out, pos, n_out))
15581 continue;
15582 e = isl_seq_last_non_zero(bmap->ineq[i] + 1 + v_div, n_div);
15583 if (e == -1)
15584 continue;
15585 unknown = isl_basic_map_div_is_marked_unknown(bmap, e);
15586 if (unknown < 0)
15587 return error;
15588 if (unknown)
15589 continue;
15590 if (isl_seq_any_non_zero(bmap->ineq[i] + 1 + v_div, e))
15591 continue;
15592 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + e]))
15593 continue;
15594 if (!isl_int_is_divisible_by(bmap->ineq[i][1 + v_div + e],
15595 bmap->ineq[i][1 + v_out + pos]))
15596 continue;
15597 if (!isl_int_abs_eq(bmap->ineq[i][1 + v_div + e],
15598 bmap->div[e][0]))
15599 continue;
15600 if (!isl_seq_eq(bmap->ineq[i] + 1, bmap->div[e] + 2, v_div + e))
15601 continue;
15602 return try_find_output_mod_ineq_at(bmap, v_out, pos, i, n_ineq,
15603 v_div, e);
15606 return no;
15609 /* Look for a combination of constraints in "bmap" that ensure
15610 * that output dimension "pos" is equal to some modulo expression
15611 * in the parameters and input dimensions and
15612 * return this expression if found.
15614 * The constraints are assumed to have been sorted.
15616 * First look for a pattern involving an equality constraint and
15617 * then look for a pattern involving only inequality constraints.
15619 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_mod(
15620 __isl_keep isl_basic_map *bmap, int pos)
15622 isl_maybe_isl_aff mod;
15624 mod = isl_basic_map_try_find_output_mod_eq(bmap, pos);
15625 if (mod.valid < 0 || mod.valid)
15626 return mod;
15627 return isl_basic_map_try_find_output_mod_ineq(bmap, pos);
15630 /* Construct an isl_aff from the given domain local space "ls" and
15631 * coefficients "v", where the local space may involve
15632 * local variables without a known expression, as long as these
15633 * do not have a non-zero coefficient in "v".
15634 * These need to be pruned away first since an isl_aff cannot
15635 * reference any local variables without a known expression.
15636 * For simplicity, remove all local variables that have a zero coefficient and
15637 * that are not used in other local variables with a non-zero coefficient.
15639 static __isl_give isl_aff *isl_aff_alloc_vec_prune(
15640 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
15642 int i;
15643 isl_size n_div, v_div;
15645 n_div = isl_local_space_dim(ls, isl_dim_div);
15646 v_div = isl_local_space_var_offset(ls, isl_dim_div);
15647 if (n_div < 0 || v_div < 0 || !v)
15648 goto error;
15649 for (i = n_div - 1; i >= 0; --i) {
15650 isl_bool involves;
15652 if (!isl_int_is_zero(v->el[1 + 1 + v_div + i]))
15653 continue;
15654 involves = isl_local_space_involves_dims(ls, isl_dim_div, i, 1);
15655 if (involves < 0)
15656 goto error;
15657 if (involves)
15658 continue;
15659 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
15660 v = isl_vec_drop_els(v, 1 + 1 + v_div + i, 1);
15661 if (!v)
15662 goto error;
15665 return isl_aff_alloc_vec(ls, v);
15666 error:
15667 isl_local_space_free(ls);
15668 isl_vec_free(v);
15669 return NULL;
15672 /* Look for a pair of constraints in "bmap" that ensure
15673 * that output dimension "pos" is equal to some integer division expression
15674 * in the parameters and input dimensions and
15675 * return this expression if found.
15677 * In particular, looks for a pair of constraints
15679 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
15681 * and
15683 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
15685 * where m > 1 and e only depends on parameters and input dimensions,
15686 * and such that
15688 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
15690 * If such a pair of constraints can be found
15691 * then
15693 * x = floor((e(...) + c1) / m)
15695 * with e(...) an expression that does not involve any other output dimensions.
15697 * Note that we know that
15699 * c1 + c2 >= 1
15701 * If c1 + c2 were 0, then we would have detected an equality during
15702 * simplification. If c1 + c2 were negative, then we would have detected
15703 * a contradiction.
15705 * The constraint defining the integer division is guaranteed not to involve
15706 * any local variables without a known expression, but such local variables
15707 * may appear in other constraints. They therefore need to be removed
15708 * during the construction of the affine expression.
15710 static __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_div(
15711 __isl_keep isl_basic_map *bmap, int pos)
15713 isl_size i;
15714 isl_size n_ineq;
15715 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15716 isl_local_space *ls;
15717 isl_aff *aff;
15718 isl_vec *v;
15719 isl_bool is_set;
15721 n_ineq = isl_basic_map_n_inequality(bmap);
15722 if (n_ineq < 0)
15723 goto error;
15725 i = isl_basic_map_find_output_upper_div_constraint(bmap, pos);
15726 if (i < 0)
15727 goto error;
15728 if (i >= n_ineq)
15729 return res;
15731 is_set = isl_basic_map_is_set(bmap);
15732 if (is_set < 0)
15733 bmap = isl_basic_map_free(bmap);
15735 ls = isl_basic_map_get_local_space(bmap);
15736 if (!is_set)
15737 ls = isl_local_space_wrap(ls);
15738 v = isl_basic_map_inequality_extract_output_upper_bound(bmap, i, pos);
15740 aff = isl_aff_alloc_vec_prune(ls, v);
15741 aff = isl_aff_floor(aff);
15743 if (is_set)
15744 aff = isl_aff_project_domain_on_params(aff);
15745 else
15746 aff = isl_aff_domain_factor_domain(aff);
15748 res.valid = isl_bool_true;
15749 res.value = aff;
15750 return res;
15751 error:
15752 res.valid = isl_bool_error;
15753 return res;
15756 /* Look for a combination of constraints in "bmap" that ensure
15757 * that output dimension "pos" is equal to some integer division or
15758 * modulo expression in the parameters and input dimensions and
15759 * return this expression if found.
15761 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_output_div_mod(
15762 __isl_keep isl_basic_map *bmap, int pos)
15764 isl_maybe_isl_aff div;
15766 div = isl_basic_map_try_find_output_div(bmap, pos);
15767 if (div.valid < 0 || div.valid)
15768 return div;
15769 return isl_basic_map_try_find_output_mod(bmap, pos);
15772 /* Look for a combination of constraints in "bmap" that ensure
15773 * that any output dimension is equal to some integer division or
15774 * modulo expression in the parameters and input dimensions and
15775 * return this expression if found.
15776 * The position of the output dimension (if any) is returned in "pos".
15778 __isl_give isl_maybe_isl_aff isl_basic_map_try_find_any_output_div_mod(
15779 __isl_keep isl_basic_map *bmap, int *pos)
15781 isl_size dim;
15782 isl_maybe_isl_aff res = { isl_bool_false, NULL };
15784 dim = isl_basic_map_dim(bmap, isl_dim_out);
15785 if (dim < 0)
15786 goto error;
15788 for (*pos = 0; *pos < dim; ++*pos) {
15789 res = isl_basic_map_try_find_output_div_mod(bmap, *pos);
15790 if (res.valid < 0 || res.valid)
15791 return res;
15794 return res;
15795 error:
15796 res.valid = isl_bool_error;
15797 return res;
15800 /* Return a copy of the equality constraints of "bset" as a matrix.
15802 __isl_give isl_mat *isl_basic_set_extract_equalities(
15803 __isl_keep isl_basic_set *bset)
15805 isl_ctx *ctx;
15806 isl_size total;
15808 total = isl_basic_set_dim(bset, isl_dim_all);
15809 if (total < 0)
15810 return NULL;
15812 ctx = isl_basic_set_get_ctx(bset);
15813 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
15816 /* Are the "n" "coefficients" starting at "first" of the integer division
15817 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
15818 * to each other?
15819 * The "coefficient" at position 0 is the denominator.
15820 * The "coefficient" at position 1 is the constant term.
15822 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
15823 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
15824 unsigned first, unsigned n)
15826 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
15827 return isl_bool_error;
15828 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
15829 return isl_bool_error;
15830 return isl_seq_eq(bmap1->div[pos1] + first,
15831 bmap2->div[pos2] + first, n);
15834 /* Are the integer division expressions at position "pos1" in "bmap1" and
15835 * "pos2" in "bmap2" equal to each other, except that the constant terms
15836 * are different?
15838 isl_bool isl_basic_map_equal_div_expr_except_constant(
15839 __isl_keep isl_basic_map *bmap1, int pos1,
15840 __isl_keep isl_basic_map *bmap2, int pos2)
15842 isl_bool equal;
15843 isl_size total, total2;
15845 total = isl_basic_map_dim(bmap1, isl_dim_all);
15846 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
15847 if (total < 0 || total2 < 0)
15848 return isl_bool_error;
15849 if (total != total2)
15850 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
15851 "incomparable div expressions", return isl_bool_error);
15852 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15853 0, 1);
15854 if (equal < 0 || !equal)
15855 return equal;
15856 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15857 1, 1);
15858 if (equal < 0 || equal)
15859 return isl_bool_not(equal);
15860 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
15861 2, total);
15864 /* Replace the numerator of the constant term of the integer division
15865 * expression at position "div" in "bmap" by "value".
15866 * The caller guarantees that this does not change the meaning
15867 * of the input.
15869 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
15870 __isl_take isl_basic_map *bmap, int div, int value)
15872 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
15873 return isl_basic_map_free(bmap);
15875 isl_int_set_si(bmap->div[div][1], value);
15877 return bmap;
15880 /* Is the point "inner" internal to inequality constraint "ineq"
15881 * of "bset"?
15882 * The point is considered to be internal to the inequality constraint,
15883 * if it strictly lies on the positive side of the inequality constraint,
15884 * or if it lies on the constraint and the constraint is lexico-positive.
15886 static isl_bool is_internal(__isl_keep isl_vec *inner,
15887 __isl_keep isl_basic_set *bset, int ineq)
15889 isl_ctx *ctx;
15890 int pos;
15891 isl_size total;
15893 if (!inner || !bset)
15894 return isl_bool_error;
15896 ctx = isl_basic_set_get_ctx(bset);
15897 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
15898 &ctx->normalize_gcd);
15899 if (!isl_int_is_zero(ctx->normalize_gcd))
15900 return isl_int_is_nonneg(ctx->normalize_gcd);
15902 total = isl_basic_set_dim(bset, isl_dim_all);
15903 if (total < 0)
15904 return isl_bool_error;
15905 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
15906 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
15909 /* Tighten the inequality constraints of "bset" that are outward with respect
15910 * to the point "vec".
15911 * That is, tighten the constraints that are not satisfied by "vec".
15913 * "vec" is a point internal to some superset S of "bset" that is used
15914 * to make the subsets of S disjoint, by tightening one half of the constraints
15915 * that separate two subsets. In particular, the constraints of S
15916 * are all satisfied by "vec" and should not be tightened.
15917 * Of the internal constraints, those that have "vec" on the outside
15918 * are tightened. The shared facet is included in the adjacent subset
15919 * with the opposite constraint.
15920 * For constraints that saturate "vec", this criterion cannot be used
15921 * to determine which of the two sides should be tightened.
15922 * Instead, the sign of the first non-zero coefficient is used
15923 * to make this choice. Note that this second criterion is never used
15924 * on the constraints of S since "vec" is interior to "S".
15926 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
15927 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
15929 int j;
15931 bset = isl_basic_set_cow(bset);
15932 if (!bset)
15933 return NULL;
15934 for (j = 0; j < bset->n_ineq; ++j) {
15935 isl_bool internal;
15937 internal = is_internal(vec, bset, j);
15938 if (internal < 0)
15939 return isl_basic_set_free(bset);
15940 if (internal)
15941 continue;
15942 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
15945 return bset;
15948 /* Replace the variables x of type "type" starting at "first" in "bmap"
15949 * by x' with x = M x' with M the matrix trans.
15950 * That is, replace the corresponding coefficients c by c M.
15952 * The transformation matrix should be a square matrix.
15954 __isl_give isl_basic_map *isl_basic_map_transform_dims(
15955 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
15956 __isl_take isl_mat *trans)
15958 unsigned pos;
15960 bmap = isl_basic_map_cow(bmap);
15961 if (!bmap || !trans)
15962 goto error;
15964 if (trans->n_row != trans->n_col)
15965 isl_die(trans->ctx, isl_error_invalid,
15966 "expecting square transformation matrix", goto error);
15967 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
15968 goto error;
15970 pos = isl_basic_map_offset(bmap, type) + first;
15972 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
15973 isl_mat_copy(trans)) < 0)
15974 goto error;
15975 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
15976 isl_mat_copy(trans)) < 0)
15977 goto error;
15978 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
15979 isl_mat_copy(trans)) < 0)
15980 goto error;
15982 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
15983 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
15984 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
15986 isl_mat_free(trans);
15987 return bmap;
15988 error:
15989 isl_mat_free(trans);
15990 isl_basic_map_free(bmap);
15991 return NULL;
15994 /* Replace the variables x of type "type" starting at "first" in "bset"
15995 * by x' with x = M x' with M the matrix trans.
15996 * That is, replace the corresponding coefficients c by c M.
15998 * The transformation matrix should be a square matrix.
16000 __isl_give isl_basic_set *isl_basic_set_transform_dims(
16001 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
16002 __isl_take isl_mat *trans)
16004 return isl_basic_map_transform_dims(bset, type, first, trans);