add isl_space_get_tuple_domain_hash
[isl.git] / isl_map.c
blobd1b092f56f84422a5531573200d3aa7df5168b0a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
24 #include <string.h>
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
27 #include <isl_blk.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl/map.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
38 #include <isl_sort.h>
39 #include "isl_tab.h"
40 #include <isl/vec.h>
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
60 static __isl_keep const isl_basic_map *const_bset_to_bmap(
61 __isl_keep const isl_basic_set *bset)
63 return (const isl_basic_map *) bset;
66 #undef TYPE
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
70 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
72 switch (type) {
73 case isl_dim_param: return 1;
74 case isl_dim_in: return 1 + space->nparam;
75 case isl_dim_out: return 1 + space->nparam + space->n_in;
76 default: return 0;
80 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
81 enum isl_dim_type type)
83 if (!bmap)
84 return isl_size_error;
85 switch (type) {
86 case isl_dim_cst: return 1;
87 case isl_dim_param:
88 case isl_dim_in:
89 case isl_dim_out: return isl_space_dim(bmap->dim, type);
90 case isl_dim_div: return bmap->n_div;
91 case isl_dim_all: return isl_basic_map_total_dim(bmap);
92 default: return 0;
96 /* Return the space of "map".
98 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
100 return map ? map->dim : NULL;
103 /* Return the space of "set".
105 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
107 return isl_map_peek_space(set_to_map(set));
110 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
112 return isl_space_dim(isl_map_peek_space(map), type);
115 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
117 return isl_map_dim(set_to_map(set), type);
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
123 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
124 enum isl_dim_type type)
126 isl_space *space;
128 space = isl_basic_map_peek_space(bmap);
129 if (!space)
130 return isl_size_error;
132 switch (type) {
133 case isl_dim_param:
134 case isl_dim_in:
135 case isl_dim_out: return isl_space_offset(space, type);
136 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
137 case isl_dim_cst:
138 default:
139 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
140 "invalid dimension type", return isl_size_error);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
147 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
148 enum isl_dim_type type)
150 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
157 enum isl_dim_type type)
159 switch (type) {
160 case isl_dim_cst: return 0;
161 case isl_dim_param:
162 case isl_dim_in:
163 case isl_dim_out:
164 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
165 default: return 0;
169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
172 return isl_basic_map_offset(bset, type);
175 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
177 return pos(map->dim, type);
180 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
181 enum isl_dim_type type)
183 return isl_basic_map_dim(bset, type);
186 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_set);
191 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
193 return isl_basic_set_dim(bset, isl_dim_param);
196 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
201 isl_size isl_set_n_dim(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_set);
206 isl_size isl_set_n_param(__isl_keep isl_set *set)
208 return isl_set_dim(set, isl_dim_param);
211 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
213 isl_size dim;
215 if (!bmap)
216 return isl_size_error;
217 dim = isl_space_dim(bmap->dim, isl_dim_all);
218 if (dim < 0)
219 return isl_size_error;
220 return dim + bmap->n_div;
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
226 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
228 if (!bmap)
229 return isl_size_error;
230 return bmap->n_eq;
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
236 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
238 return isl_basic_map_n_equality(bset_to_bmap(bset));
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
244 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
246 if (!bmap)
247 return isl_size_error;
248 return bmap->n_ineq;
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
254 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
256 return isl_basic_map_n_inequality(bset_to_bmap(bset));
259 /* Do "bmap1" and "bmap2" have the same parameters?
261 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
262 __isl_keep isl_basic_map *bmap2)
264 isl_space *space1, *space2;
266 space1 = isl_basic_map_peek_space(bmap1);
267 space2 = isl_basic_map_peek_space(bmap2);
268 return isl_space_has_equal_params(space1, space2);
271 /* Do "map1" and "map2" have the same parameters?
273 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
274 __isl_keep isl_map *map2)
276 isl_space *space1, *space2;
278 space1 = isl_map_peek_space(map1);
279 space2 = isl_map_peek_space(map2);
280 return isl_space_has_equal_params(space1, space2);
283 /* Do "map" and "set" have the same parameters?
285 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
286 __isl_keep isl_set *set)
288 return isl_map_has_equal_params(map, set_to_map(set));
291 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
293 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
294 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
296 isl_space *bmap_space, *bset_space;
298 bmap_space = isl_basic_map_peek_space(bmap);
299 bset_space = isl_basic_set_peek_space(bset);
300 return isl_space_tuple_is_equal(bmap_space, type,
301 bset_space, isl_dim_set);
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
306 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
307 enum isl_dim_type type, __isl_keep isl_set *set)
309 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
312 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
313 __isl_keep isl_set *set)
315 isl_bool m;
316 if (!map || !set)
317 return isl_bool_error;
318 m = isl_map_has_equal_params(map, set_to_map(set));
319 if (m < 0 || !m)
320 return m;
321 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
324 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
325 __isl_keep isl_basic_set *bset)
327 isl_bool m;
328 if (!bmap || !bset)
329 return isl_bool_error;
330 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
331 if (m < 0 || !m)
332 return m;
333 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
336 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
337 __isl_keep isl_set *set)
339 isl_bool m;
340 if (!map || !set)
341 return isl_bool_error;
342 m = isl_map_has_equal_params(map, set_to_map(set));
343 if (m < 0 || !m)
344 return m;
345 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
348 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
349 __isl_keep isl_basic_set *bset)
351 isl_bool m;
352 if (!bmap || !bset)
353 return isl_bool_error;
354 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
355 if (m < 0 || !m)
356 return m;
357 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
360 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
362 return bmap ? bmap->ctx : NULL;
365 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
367 return bset ? bset->ctx : NULL;
370 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
372 return map ? map->ctx : NULL;
375 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
377 return set ? set->ctx : NULL;
380 /* Return the space of "bmap".
382 __isl_keep isl_space *isl_basic_map_peek_space(
383 __isl_keep const isl_basic_map *bmap)
385 return bmap ? bmap->dim : NULL;
388 /* Return the space of "bset".
390 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
392 return isl_basic_map_peek_space(bset_to_bmap(bset));
395 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
397 return isl_space_copy(isl_basic_map_peek_space(bmap));
400 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
402 return isl_basic_map_get_space(bset_to_bmap(bset));
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
414 static __isl_give isl_space *isl_basic_map_take_space(
415 __isl_keep isl_basic_map *bmap)
417 isl_space *space;
419 if (!bmap)
420 return NULL;
421 if (bmap->ref != 1)
422 return isl_basic_map_get_space(bmap);
423 space = bmap->dim;
424 bmap->dim = NULL;
425 return space;
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
433 static __isl_give isl_basic_map *isl_basic_map_restore_space(
434 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
436 if (!bmap || !space)
437 goto error;
439 if (bmap->dim == space) {
440 isl_space_free(space);
441 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 if (!bmap)
446 goto error;
447 isl_space_free(bmap->dim);
448 bmap->dim = space;
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 isl_space_free(space);
454 return NULL;
457 /* Extract the divs in "bmap" as a matrix.
459 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
461 int i;
462 isl_ctx *ctx;
463 isl_mat *div;
464 isl_size v_div;
465 unsigned cols;
467 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
468 if (v_div < 0)
469 return NULL;
471 ctx = isl_basic_map_get_ctx(bmap);
472 cols = 1 + 1 + v_div + bmap->n_div;
473 div = isl_mat_alloc(ctx, bmap->n_div, cols);
474 if (!div)
475 return NULL;
477 for (i = 0; i < bmap->n_div; ++i)
478 isl_seq_cpy(div->row[i], bmap->div[i], cols);
480 return div;
483 /* Extract the divs in "bset" as a matrix.
485 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
487 return isl_basic_map_get_divs(bset);
490 __isl_give isl_local_space *isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map *bmap)
493 isl_mat *div;
495 if (!bmap)
496 return NULL;
498 div = isl_basic_map_get_divs(bmap);
499 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
502 __isl_give isl_local_space *isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set *bset)
505 return isl_basic_map_get_local_space(bset);
508 /* For each known div d = floor(f/m), add the constraints
510 * f - m d >= 0
511 * -(f-(m-1)) + m d >= 0
513 * Do not finalize the result.
515 static __isl_give isl_basic_map *add_known_div_constraints(
516 __isl_take isl_basic_map *bmap)
518 int i;
519 isl_size n_div;
521 n_div = isl_basic_map_dim(bmap, isl_dim_div);
522 if (n_div < 0)
523 return isl_basic_map_free(bmap);
524 if (n_div == 0)
525 return bmap;
526 bmap = isl_basic_map_cow(bmap);
527 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
528 if (!bmap)
529 return NULL;
530 for (i = 0; i < n_div; ++i) {
531 if (isl_int_is_zero(bmap->div[i][0]))
532 continue;
533 bmap = isl_basic_map_add_div_constraints(bmap, i);
536 return bmap;
539 __isl_give isl_basic_map *isl_basic_map_from_local_space(
540 __isl_take isl_local_space *ls)
542 int i;
543 isl_size n_div;
544 isl_basic_map *bmap;
546 n_div = isl_local_space_dim(ls, isl_dim_div);
547 if (n_div < 0)
548 ls = isl_local_space_free(ls);
549 if (!ls)
550 return NULL;
552 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
553 n_div, 0, 2 * n_div);
555 for (i = 0; i < n_div; ++i)
556 if (isl_basic_map_alloc_div(bmap) < 0)
557 goto error;
559 for (i = 0; i < n_div; ++i)
560 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
561 bmap = add_known_div_constraints(bmap);
563 isl_local_space_free(ls);
564 return bmap;
565 error:
566 isl_local_space_free(ls);
567 isl_basic_map_free(bmap);
568 return NULL;
571 __isl_give isl_basic_set *isl_basic_set_from_local_space(
572 __isl_take isl_local_space *ls)
574 return isl_basic_map_from_local_space(ls);
577 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
579 return isl_space_copy(isl_map_peek_space(map));
582 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
584 if (!set)
585 return NULL;
586 return isl_space_copy(set->dim);
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
598 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
600 isl_space *space;
602 if (!map)
603 return NULL;
604 if (map->ref != 1)
605 return isl_map_get_space(map);
606 space = map->dim;
607 map->dim = NULL;
608 return space;
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
616 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
617 __isl_take isl_space *space)
619 if (!map || !space)
620 goto error;
622 if (map->dim == space) {
623 isl_space_free(space);
624 return map;
627 map = isl_map_cow(map);
628 if (!map)
629 goto error;
630 isl_space_free(map->dim);
631 map->dim = space;
633 return map;
634 error:
635 isl_map_free(map);
636 isl_space_free(space);
637 return NULL;
640 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
643 isl_space *space;
645 space = isl_basic_map_take_space(bmap);
646 space = isl_space_set_tuple_name(space, type, s);
647 bmap = isl_basic_map_restore_space(bmap, space);
648 bmap = isl_basic_map_finalize(bmap);
649 return bmap;
652 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set *bset, const char *s)
655 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
659 enum isl_dim_type type)
661 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
664 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
665 enum isl_dim_type type, const char *s)
667 int i;
668 isl_space *space;
670 map = isl_map_cow(map);
671 if (!map)
672 return NULL;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
676 if (!map->p[i])
677 goto error;
680 space = isl_map_take_space(map);
681 space = isl_space_set_tuple_name(space, type, s);
682 map = isl_map_restore_space(map, space);
684 return map;
685 error:
686 isl_map_free(map);
687 return NULL;
690 /* Replace the identifier of the tuple of type "type" by "id".
692 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map *bmap,
694 enum isl_dim_type type, __isl_take isl_id *id)
696 isl_space *space;
698 space = isl_basic_map_take_space(bmap);
699 space = isl_space_set_tuple_id(space, type, id);
700 bmap = isl_basic_map_restore_space(bmap, space);
701 bmap = isl_basic_map_finalize(bmap);
702 return bmap;
705 /* Replace the identifier of the tuple by "id".
707 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
710 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
713 /* Does the input or output tuple have a name?
715 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
717 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
720 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
721 enum isl_dim_type type)
723 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
726 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
727 const char *s)
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
730 isl_dim_set, s));
733 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
734 enum isl_dim_type type, __isl_take isl_id *id)
736 isl_space *space;
738 space = isl_map_take_space(map);
739 space = isl_space_set_tuple_id(space, type, id);
740 map = isl_map_restore_space(map, space);
742 return isl_map_reset_space(map, isl_map_get_space(map));
745 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
746 __isl_take isl_id *id)
748 return isl_map_set_tuple_id(set, isl_dim_set, id);
751 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
752 enum isl_dim_type type)
754 isl_space *space;
756 space = isl_map_take_space(map);
757 space = isl_space_reset_tuple_id(space, type);
758 map = isl_map_restore_space(map, space);
760 return isl_map_reset_space(map, isl_map_get_space(map));
763 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
765 return isl_map_reset_tuple_id(set, isl_dim_set);
768 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
770 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
773 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
774 enum isl_dim_type type)
776 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
779 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
781 return isl_map_has_tuple_id(set, isl_dim_set);
784 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
786 return isl_map_get_tuple_id(set, isl_dim_set);
789 /* Does the set tuple have a name?
791 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
793 if (!set)
794 return isl_bool_error;
795 return isl_space_has_tuple_name(set->dim, isl_dim_set);
799 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
801 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
804 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
806 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
809 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
810 enum isl_dim_type type, unsigned pos)
812 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
815 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
816 enum isl_dim_type type, unsigned pos)
818 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
821 /* Does the given dimension have a name?
823 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
824 enum isl_dim_type type, unsigned pos)
826 if (!map)
827 return isl_bool_error;
828 return isl_space_has_dim_name(map->dim, type, pos);
831 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
832 enum isl_dim_type type, unsigned pos)
834 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
837 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
838 enum isl_dim_type type, unsigned pos)
840 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
843 /* Does the given dimension have a name?
845 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
846 enum isl_dim_type type, unsigned pos)
848 if (!set)
849 return isl_bool_error;
850 return isl_space_has_dim_name(set->dim, type, pos);
853 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
854 __isl_take isl_basic_map *bmap,
855 enum isl_dim_type type, unsigned pos, const char *s)
857 isl_space *space;
859 space = isl_basic_map_take_space(bmap);
860 space = isl_space_set_dim_name(space, type, pos, s);
861 bmap = isl_basic_map_restore_space(bmap, space);
862 return isl_basic_map_finalize(bmap);
865 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
866 enum isl_dim_type type, unsigned pos, const char *s)
868 int i;
869 isl_space *space;
871 map = isl_map_cow(map);
872 if (!map)
873 return NULL;
875 for (i = 0; i < map->n; ++i) {
876 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
877 if (!map->p[i])
878 goto error;
881 space = isl_map_take_space(map);
882 space = isl_space_set_dim_name(space, type, pos, s);
883 map = isl_map_restore_space(map, space);
885 return map;
886 error:
887 isl_map_free(map);
888 return NULL;
891 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
892 __isl_take isl_basic_set *bset,
893 enum isl_dim_type type, unsigned pos, const char *s)
895 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
896 type, pos, s));
899 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
900 enum isl_dim_type type, unsigned pos, const char *s)
902 return set_from_map(isl_map_set_dim_name(set_to_map(set),
903 type, pos, s));
906 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
907 enum isl_dim_type type, unsigned pos)
909 if (!bmap)
910 return isl_bool_error;
911 return isl_space_has_dim_id(bmap->dim, type, pos);
914 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
915 enum isl_dim_type type, unsigned pos)
917 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
920 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
921 enum isl_dim_type type, unsigned pos)
923 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
926 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
927 enum isl_dim_type type, unsigned pos)
929 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
932 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
933 enum isl_dim_type type, unsigned pos)
935 return isl_map_has_dim_id(set, type, pos);
938 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
939 enum isl_dim_type type, unsigned pos)
941 return isl_map_get_dim_id(set, type, pos);
944 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
945 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
947 isl_space *space;
949 space = isl_map_take_space(map);
950 space = isl_space_set_dim_id(space, type, pos, id);
951 map = isl_map_restore_space(map, space);
953 return isl_map_reset_space(map, isl_map_get_space(map));
956 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
957 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
959 return isl_map_set_dim_id(set, type, pos, id);
962 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
963 __isl_keep isl_id *id)
965 if (!map)
966 return -1;
967 return isl_space_find_dim_by_id(map->dim, type, id);
970 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
971 __isl_keep isl_id *id)
973 return isl_map_find_dim_by_id(set, type, id);
976 /* Return the position of the dimension of the given type and name
977 * in "bmap".
978 * Return -1 if no such dimension can be found.
980 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
981 enum isl_dim_type type, const char *name)
983 if (!bmap)
984 return -1;
985 return isl_space_find_dim_by_name(bmap->dim, type, name);
988 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
989 const char *name)
991 if (!map)
992 return -1;
993 return isl_space_find_dim_by_name(map->dim, type, name);
996 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
997 const char *name)
999 return isl_map_find_dim_by_name(set, type, name);
1002 /* Check whether equality i of bset is a pure stride constraint
1003 * on a single dimension, i.e., of the form
1005 * v = k e
1007 * with k a constant and e an existentially quantified variable.
1009 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1011 isl_size nparam;
1012 isl_size d;
1013 isl_size n_div;
1014 int pos1;
1015 int pos2;
1017 nparam = isl_basic_set_dim(bset, isl_dim_param);
1018 d = isl_basic_set_dim(bset, isl_dim_set);
1019 n_div = isl_basic_set_dim(bset, isl_dim_div);
1020 if (nparam < 0 || d < 0 || n_div < 0)
1021 return isl_bool_error;
1023 if (!isl_int_is_zero(bset->eq[i][0]))
1024 return isl_bool_false;
1026 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1027 return isl_bool_false;
1028 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1029 if (pos1 == -1)
1030 return isl_bool_false;
1031 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1032 d - pos1 - 1) != -1)
1033 return isl_bool_false;
1035 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1036 if (pos2 == -1)
1037 return isl_bool_false;
1038 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1039 n_div - pos2 - 1) != -1)
1040 return isl_bool_false;
1041 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1042 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1043 return isl_bool_false;
1045 return isl_bool_true;
1048 /* Reset the user pointer on all identifiers of parameters and tuples
1049 * of the space of "map".
1051 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1053 isl_space *space;
1055 space = isl_map_get_space(map);
1056 space = isl_space_reset_user(space);
1057 map = isl_map_reset_space(map, space);
1059 return map;
1062 /* Reset the user pointer on all identifiers of parameters and tuples
1063 * of the space of "set".
1065 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1067 return isl_map_reset_user(set);
1070 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1072 if (!bmap)
1073 return isl_bool_error;
1074 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1077 /* Has "map" been marked as a rational map?
1078 * In particular, have all basic maps in "map" been marked this way?
1079 * An empty map is not considered to be rational.
1080 * Maps where only some of the basic maps are marked rational
1081 * are not allowed.
1083 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1085 int i;
1086 isl_bool rational;
1088 if (!map)
1089 return isl_bool_error;
1090 if (map->n == 0)
1091 return isl_bool_false;
1092 rational = isl_basic_map_is_rational(map->p[0]);
1093 if (rational < 0)
1094 return rational;
1095 for (i = 1; i < map->n; ++i) {
1096 isl_bool rational_i;
1098 rational_i = isl_basic_map_is_rational(map->p[i]);
1099 if (rational_i < 0)
1100 return rational_i;
1101 if (rational != rational_i)
1102 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1103 "mixed rational and integer basic maps "
1104 "not supported", return isl_bool_error);
1107 return rational;
1110 /* Has "set" been marked as a rational set?
1111 * In particular, have all basic set in "set" been marked this way?
1112 * An empty set is not considered to be rational.
1113 * Sets where only some of the basic sets are marked rational
1114 * are not allowed.
1116 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1118 return isl_map_is_rational(set);
1121 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1123 return isl_basic_map_is_rational(bset);
1126 /* Does "bmap" contain any rational points?
1128 * If "bmap" has an equality for each dimension, equating the dimension
1129 * to an integer constant, then it has no rational points, even if it
1130 * is marked as rational.
1132 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1134 isl_bool has_rational = isl_bool_true;
1135 isl_size total;
1137 if (!bmap)
1138 return isl_bool_error;
1139 if (isl_basic_map_plain_is_empty(bmap))
1140 return isl_bool_false;
1141 if (!isl_basic_map_is_rational(bmap))
1142 return isl_bool_false;
1143 bmap = isl_basic_map_copy(bmap);
1144 bmap = isl_basic_map_implicit_equalities(bmap);
1145 total = isl_basic_map_dim(bmap, isl_dim_all);
1146 if (total < 0)
1147 return isl_bool_error;
1148 if (bmap->n_eq == total) {
1149 int i, j;
1150 for (i = 0; i < bmap->n_eq; ++i) {
1151 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1152 if (j < 0)
1153 break;
1154 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1155 !isl_int_is_negone(bmap->eq[i][1 + j]))
1156 break;
1157 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1158 total - j - 1);
1159 if (j >= 0)
1160 break;
1162 if (i == bmap->n_eq)
1163 has_rational = isl_bool_false;
1165 isl_basic_map_free(bmap);
1167 return has_rational;
1170 /* Does "map" contain any rational points?
1172 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1174 int i;
1175 isl_bool has_rational;
1177 if (!map)
1178 return isl_bool_error;
1179 for (i = 0; i < map->n; ++i) {
1180 has_rational = isl_basic_map_has_rational(map->p[i]);
1181 if (has_rational < 0 || has_rational)
1182 return has_rational;
1184 return isl_bool_false;
1187 /* Does "set" contain any rational points?
1189 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1191 return isl_map_has_rational(set);
1194 /* Is this basic set a parameter domain?
1196 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1198 if (!bset)
1199 return isl_bool_error;
1200 return isl_space_is_params(bset->dim);
1203 /* Is this set a parameter domain?
1205 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1207 if (!set)
1208 return isl_bool_error;
1209 return isl_space_is_params(set->dim);
1212 /* Is this map actually a parameter domain?
1213 * Users should never call this function. Outside of isl,
1214 * a map can never be a parameter domain.
1216 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1218 if (!map)
1219 return isl_bool_error;
1220 return isl_space_is_params(map->dim);
1223 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1224 __isl_take isl_basic_map *bmap, unsigned extra,
1225 unsigned n_eq, unsigned n_ineq)
1227 int i;
1228 isl_space *space = isl_basic_map_peek_space(bmap);
1229 isl_size n_var = isl_space_dim(space, isl_dim_all);
1230 size_t row_size = 1 + n_var + extra;
1232 bmap->ctx = ctx;
1233 isl_ctx_ref(ctx);
1235 if (n_var < 0)
1236 return isl_basic_map_free(bmap);
1238 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1239 if (isl_blk_is_error(bmap->block))
1240 goto error;
1242 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1243 if ((n_ineq + n_eq) && !bmap->ineq)
1244 goto error;
1246 if (extra == 0) {
1247 bmap->block2 = isl_blk_empty();
1248 bmap->div = NULL;
1249 } else {
1250 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1251 if (isl_blk_is_error(bmap->block2))
1252 goto error;
1254 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1255 if (!bmap->div)
1256 goto error;
1259 for (i = 0; i < n_ineq + n_eq; ++i)
1260 bmap->ineq[i] = bmap->block.data + i * row_size;
1262 for (i = 0; i < extra; ++i)
1263 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1265 bmap->ref = 1;
1266 bmap->flags = 0;
1267 bmap->c_size = n_eq + n_ineq;
1268 bmap->eq = bmap->ineq + n_ineq;
1269 bmap->extra = extra;
1270 bmap->n_eq = 0;
1271 bmap->n_ineq = 0;
1272 bmap->n_div = 0;
1273 bmap->sample = NULL;
1275 return bmap;
1276 error:
1277 isl_basic_map_free(bmap);
1278 return NULL;
1281 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1282 unsigned nparam, unsigned dim, unsigned extra,
1283 unsigned n_eq, unsigned n_ineq)
1285 struct isl_basic_map *bmap;
1286 isl_space *space;
1288 space = isl_space_set_alloc(ctx, nparam, dim);
1289 if (!space)
1290 return NULL;
1292 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1293 return bset_from_bmap(bmap);
1296 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1297 unsigned extra, unsigned n_eq, unsigned n_ineq)
1299 struct isl_basic_map *bmap;
1300 if (!space)
1301 return NULL;
1302 isl_assert(space->ctx, space->n_in == 0, goto error);
1303 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1304 return bset_from_bmap(bmap);
1305 error:
1306 isl_space_free(space);
1307 return NULL;
1310 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1311 unsigned extra, unsigned n_eq, unsigned n_ineq)
1313 struct isl_basic_map *bmap;
1315 if (!space)
1316 return NULL;
1317 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1318 if (!bmap)
1319 goto error;
1320 bmap->dim = space;
1322 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1323 error:
1324 isl_space_free(space);
1325 return NULL;
1328 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1329 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 struct isl_basic_map *bmap;
1333 isl_space *space;
1335 space = isl_space_alloc(ctx, nparam, in, out);
1336 if (!space)
1337 return NULL;
1339 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1340 return bmap;
1343 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1344 __isl_keep isl_basic_map *src)
1346 int i;
1347 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1349 if (!dst || total < 0)
1350 return isl_basic_map_free(dst);
1352 for (i = 0; i < src->n_eq; ++i) {
1353 int j = isl_basic_map_alloc_equality(dst);
1354 if (j < 0)
1355 return isl_basic_map_free(dst);
1356 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1359 for (i = 0; i < src->n_ineq; ++i) {
1360 int j = isl_basic_map_alloc_inequality(dst);
1361 if (j < 0)
1362 return isl_basic_map_free(dst);
1363 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1366 for (i = 0; i < src->n_div; ++i) {
1367 int j = isl_basic_map_alloc_div(dst);
1368 if (j < 0)
1369 return isl_basic_map_free(dst);
1370 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1372 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1373 return dst;
1376 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1378 struct isl_basic_map *dup;
1380 if (!bmap)
1381 return NULL;
1382 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1383 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1384 dup = dup_constraints(dup, bmap);
1385 if (!dup)
1386 return NULL;
1387 dup->flags = bmap->flags;
1388 dup->sample = isl_vec_copy(bmap->sample);
1389 return dup;
1392 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1394 struct isl_basic_map *dup;
1396 dup = isl_basic_map_dup(bset_to_bmap(bset));
1397 return bset_from_bmap(dup);
1400 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1402 if (!bset)
1403 return NULL;
1405 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1406 bset->ref++;
1407 return bset;
1409 return isl_basic_set_dup(bset);
1412 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1414 if (!set)
1415 return NULL;
1417 set->ref++;
1418 return set;
1421 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1423 if (!bmap)
1424 return NULL;
1426 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1427 bmap->ref++;
1428 return bmap;
1430 bmap = isl_basic_map_dup(bmap);
1431 if (bmap)
1432 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1433 return bmap;
1436 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1438 if (!map)
1439 return NULL;
1441 map->ref++;
1442 return map;
1445 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1447 if (!bmap)
1448 return NULL;
1450 if (--bmap->ref > 0)
1451 return NULL;
1453 isl_ctx_deref(bmap->ctx);
1454 free(bmap->div);
1455 isl_blk_free(bmap->ctx, bmap->block2);
1456 free(bmap->ineq);
1457 isl_blk_free(bmap->ctx, bmap->block);
1458 isl_vec_free(bmap->sample);
1459 isl_space_free(bmap->dim);
1460 free(bmap);
1462 return NULL;
1465 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1467 return isl_basic_map_free(bset_to_bmap(bset));
1470 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1472 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1475 /* Check that "bset" does not involve any parameters.
1477 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1479 isl_size nparam;
1481 nparam = isl_basic_set_dim(bset, isl_dim_param);
1482 if (nparam < 0)
1483 return isl_stat_error;
1484 if (nparam != 0)
1485 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1486 "basic set should not have any parameters",
1487 return isl_stat_error);
1488 return isl_stat_ok;
1491 /* Check that "bset" does not involve any local variables.
1493 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1495 isl_size n_div;
1497 n_div = isl_basic_set_dim(bset, isl_dim_div);
1498 if (n_div < 0)
1499 return isl_stat_error;
1500 if (n_div != 0)
1501 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1502 "basic set should not have any local variables",
1503 return isl_stat_error);
1504 return isl_stat_ok;
1507 #undef TYPE
1508 #define TYPE isl_map
1510 #include "isl_check_named_params_templ.c"
1512 #undef TYPE
1513 #define TYPE isl_basic_map
1515 static
1516 #include "isl_check_named_params_templ.c"
1518 /* Check that "bmap1" and "bmap2" have the same parameters,
1519 * reporting an error if they do not.
1521 static isl_stat isl_basic_map_check_equal_params(
1522 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1524 isl_bool match;
1526 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1527 if (match < 0)
1528 return isl_stat_error;
1529 if (!match)
1530 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1531 "parameters don't match", return isl_stat_error);
1532 return isl_stat_ok;
1535 #undef TYPE
1536 #define TYPE isl_map
1538 #include "isl_align_params_bin_templ.c"
1540 #undef SUFFIX
1541 #define SUFFIX set
1542 #undef ARG1
1543 #define ARG1 isl_map
1544 #undef ARG2
1545 #define ARG2 isl_set
1547 #include "isl_align_params_templ.c"
1549 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1550 __isl_keep isl_map *map2,
1551 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1553 isl_bool r;
1555 if (!map1 || !map2)
1556 return isl_bool_error;
1557 if (isl_map_has_equal_params(map1, map2))
1558 return fn(map1, map2);
1559 if (isl_map_check_named_params(map1) < 0)
1560 return isl_bool_error;
1561 if (isl_map_check_named_params(map2) < 0)
1562 return isl_bool_error;
1563 map1 = isl_map_copy(map1);
1564 map2 = isl_map_copy(map2);
1565 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1566 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1567 r = fn(map1, map2);
1568 isl_map_free(map1);
1569 isl_map_free(map2);
1570 return r;
1573 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1575 isl_size total;
1576 struct isl_ctx *ctx;
1578 total = isl_basic_map_dim(bmap, isl_dim_all);
1579 if (total < 0)
1580 return -1;
1581 ctx = bmap->ctx;
1582 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1583 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1584 return -1);
1585 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1586 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1587 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1588 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1589 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1590 isl_int *t;
1591 int j = isl_basic_map_alloc_inequality(bmap);
1592 if (j < 0)
1593 return -1;
1594 t = bmap->ineq[j];
1595 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1596 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1597 bmap->eq[-1] = t;
1598 bmap->n_eq++;
1599 bmap->n_ineq--;
1600 bmap->eq--;
1601 return 0;
1603 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_eq++;
1608 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1613 __isl_give isl_basic_map *isl_basic_map_free_equality(
1614 __isl_take isl_basic_map *bmap, unsigned n)
1616 if (!bmap)
1617 return NULL;
1618 if (n > bmap->n_eq)
1619 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1620 "invalid number of equalities",
1621 isl_basic_map_free(bmap));
1622 bmap->n_eq -= n;
1623 return bmap;
1626 __isl_give isl_basic_set *isl_basic_set_free_equality(
1627 __isl_take isl_basic_set *bset, unsigned n)
1629 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1630 n));
1633 /* Drop the equality constraint at position "pos",
1634 * preserving the order of the other equality constraints.
1636 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1638 isl_int *t;
1639 int r;
1641 if (!bmap)
1642 return -1;
1643 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1645 t = bmap->eq[pos];
1646 bmap->n_eq--;
1647 for (r = pos; r < bmap->n_eq; ++r)
1648 bmap->eq[r] = bmap->eq[r + 1];
1649 bmap->eq[bmap->n_eq] = t;
1651 return 0;
1654 /* Turn inequality "pos" of "bmap" into an equality.
1656 * In particular, we move the inequality in front of the equalities
1657 * and move the last inequality in the position of the moved inequality.
1658 * Note that isl_tab_make_equalities_explicit depends on this particular
1659 * change in the ordering of the constraints.
1661 void isl_basic_map_inequality_to_equality(
1662 __isl_keep isl_basic_map *bmap, unsigned pos)
1664 isl_int *t;
1666 t = bmap->ineq[pos];
1667 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1668 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1669 bmap->eq[-1] = t;
1670 bmap->n_eq++;
1671 bmap->n_ineq--;
1672 bmap->eq--;
1673 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1674 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1675 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1676 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1679 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1681 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1684 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1686 isl_size total;
1687 struct isl_ctx *ctx;
1689 total = isl_basic_map_dim(bmap, isl_dim_all);
1690 if (total < 0)
1691 return -1;
1692 ctx = bmap->ctx;
1693 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1694 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1695 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1696 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1697 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1698 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1699 bmap->extra - bmap->n_div);
1700 return bmap->n_ineq++;
1703 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1705 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1708 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1709 __isl_take isl_basic_map *bmap, unsigned n)
1711 if (!bmap)
1712 return NULL;
1713 if (n > bmap->n_ineq)
1714 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1715 "invalid number of inequalities",
1716 return isl_basic_map_free(bmap));
1717 bmap->n_ineq -= n;
1718 return bmap;
1721 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1722 __isl_take isl_basic_set *bset, unsigned n)
1724 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1725 n));
1728 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1730 isl_int *t;
1731 if (!bmap)
1732 return -1;
1733 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1735 if (pos != bmap->n_ineq - 1) {
1736 t = bmap->ineq[pos];
1737 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1738 bmap->ineq[bmap->n_ineq - 1] = t;
1739 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1741 bmap->n_ineq--;
1742 return 0;
1745 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1747 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1750 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1751 isl_int *eq)
1753 isl_bool empty;
1754 isl_size total;
1755 int k;
1757 empty = isl_basic_map_plain_is_empty(bmap);
1758 if (empty < 0)
1759 return isl_basic_map_free(bmap);
1760 if (empty)
1761 return bmap;
1763 bmap = isl_basic_map_cow(bmap);
1764 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1765 total = isl_basic_map_dim(bmap, isl_dim_all);
1766 if (total < 0)
1767 return isl_basic_map_free(bmap);
1768 k = isl_basic_map_alloc_equality(bmap);
1769 if (k < 0)
1770 goto error;
1771 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1772 return bmap;
1773 error:
1774 isl_basic_map_free(bmap);
1775 return NULL;
1778 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1779 isl_int *eq)
1781 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1784 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1785 isl_int *ineq)
1787 isl_size total;
1788 int k;
1790 bmap = isl_basic_map_cow(bmap);
1791 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1792 total = isl_basic_map_dim(bmap, isl_dim_all);
1793 if (total < 0)
1794 return isl_basic_map_free(bmap);
1795 k = isl_basic_map_alloc_inequality(bmap);
1796 if (k < 0)
1797 goto error;
1798 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1799 return bmap;
1800 error:
1801 isl_basic_map_free(bmap);
1802 return NULL;
1805 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1806 isl_int *ineq)
1808 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1811 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1813 isl_size total;
1815 total = isl_basic_map_dim(bmap, isl_dim_all);
1816 if (total < 0)
1817 return -1;
1818 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1819 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1820 bmap->extra - bmap->n_div);
1821 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1822 return bmap->n_div++;
1825 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1827 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1830 #undef TYPE
1831 #define TYPE isl_basic_map
1832 #include "check_type_range_templ.c"
1834 /* Check that there are "n" dimensions of type "type" starting at "first"
1835 * in "bset".
1837 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1838 enum isl_dim_type type, unsigned first, unsigned n)
1840 return isl_basic_map_check_range(bset_to_bmap(bset),
1841 type, first, n);
1844 /* Insert an extra integer division, prescribed by "div", to "bmap"
1845 * at (integer division) position "pos".
1847 * The integer division is first added at the end and then moved
1848 * into the right position.
1850 __isl_give isl_basic_map *isl_basic_map_insert_div(
1851 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1853 int i, k;
1854 isl_size total;
1856 bmap = isl_basic_map_cow(bmap);
1857 total = isl_basic_map_dim(bmap, isl_dim_all);
1858 if (total < 0 || !div)
1859 return isl_basic_map_free(bmap);
1861 if (div->size != 1 + 1 + total)
1862 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1863 "unexpected size", return isl_basic_map_free(bmap));
1864 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1865 return isl_basic_map_free(bmap);
1867 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1868 k = isl_basic_map_alloc_div(bmap);
1869 if (k < 0)
1870 return isl_basic_map_free(bmap);
1871 isl_seq_cpy(bmap->div[k], div->el, div->size);
1872 isl_int_set_si(bmap->div[k][div->size], 0);
1874 for (i = k; i > pos; --i)
1875 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1877 return bmap;
1880 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1882 if (!bmap)
1883 return isl_stat_error;
1884 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1885 bmap->n_div -= n;
1886 return isl_stat_ok;
1889 static __isl_give isl_basic_map *add_constraints(
1890 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1891 unsigned i_pos, unsigned o_pos)
1893 isl_size total, n_param, n_in, n_out, n_div;
1894 unsigned o_in, o_out;
1895 isl_ctx *ctx;
1896 isl_space *space;
1897 struct isl_dim_map *dim_map;
1899 space = isl_basic_map_peek_space(bmap2);
1900 if (!bmap1 || !space)
1901 goto error;
1903 total = isl_basic_map_dim(bmap1, isl_dim_all);
1904 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1905 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1906 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1907 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1908 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1909 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1910 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1911 goto error;
1912 ctx = isl_basic_map_get_ctx(bmap1);
1913 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1914 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1915 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1916 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1917 isl_dim_map_div(dim_map, bmap2, total);
1919 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1920 error:
1921 isl_basic_map_free(bmap1);
1922 isl_basic_map_free(bmap2);
1923 return NULL;
1926 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1927 unsigned extra, unsigned n_eq, unsigned n_ineq)
1929 isl_space *space;
1930 struct isl_basic_map *ext;
1931 unsigned flags;
1932 int dims_ok;
1934 if (!base)
1935 goto error;
1937 dims_ok = base->extra >= base->n_div + extra;
1939 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1940 room_for_ineq(base, n_ineq))
1941 return base;
1943 extra += base->extra;
1944 n_eq += base->n_eq;
1945 n_ineq += base->n_ineq;
1947 space = isl_basic_map_get_space(base);
1948 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1949 if (!ext)
1950 goto error;
1952 if (dims_ok)
1953 ext->sample = isl_vec_copy(base->sample);
1954 flags = base->flags;
1955 ext = add_constraints(ext, base, 0, 0);
1956 if (ext) {
1957 ext->flags = flags;
1958 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1961 return ext;
1963 error:
1964 isl_basic_map_free(base);
1965 return NULL;
1968 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
1969 unsigned extra, unsigned n_eq, unsigned n_ineq)
1971 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1972 extra, n_eq, n_ineq));
1975 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
1976 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1978 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
1981 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
1982 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1984 isl_basic_map *bmap = bset_to_bmap(base);
1985 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1986 return bset_from_bmap(bmap);
1989 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1991 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1994 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1996 if (!bmap)
1997 return NULL;
1999 if (bmap->ref > 1) {
2000 bmap->ref--;
2001 bmap = isl_basic_map_dup(bmap);
2003 if (bmap) {
2004 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2005 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2007 return bmap;
2010 /* Clear all cached information in "map", either because it is about
2011 * to be modified or because it is being freed.
2012 * Always return the same pointer that is passed in.
2013 * This is needed for the use in isl_map_free.
2015 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2017 isl_basic_map_free(map->cached_simple_hull[0]);
2018 isl_basic_map_free(map->cached_simple_hull[1]);
2019 map->cached_simple_hull[0] = NULL;
2020 map->cached_simple_hull[1] = NULL;
2021 return map;
2024 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2026 return isl_map_cow(set);
2029 /* Return an isl_map that is equal to "map" and that has only
2030 * a single reference.
2032 * If the original input already has only one reference, then
2033 * simply return it, but clear all cached information, since
2034 * it may be rendered invalid by the operations that will be
2035 * performed on the result.
2037 * Otherwise, create a duplicate (without any cached information).
2039 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2041 if (!map)
2042 return NULL;
2044 if (map->ref == 1)
2045 return clear_caches(map);
2046 map->ref--;
2047 return isl_map_dup(map);
2050 static void swap_vars(struct isl_blk blk, isl_int *a,
2051 unsigned a_len, unsigned b_len)
2053 isl_seq_cpy(blk.data, a+a_len, b_len);
2054 isl_seq_cpy(blk.data+b_len, a, a_len);
2055 isl_seq_cpy(a, blk.data, b_len+a_len);
2058 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2059 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2061 int i;
2062 struct isl_blk blk;
2064 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2065 goto error;
2067 if (n1 == 0 || n2 == 0)
2068 return bmap;
2070 bmap = isl_basic_map_cow(bmap);
2071 if (!bmap)
2072 return NULL;
2074 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2075 if (isl_blk_is_error(blk))
2076 goto error;
2078 for (i = 0; i < bmap->n_eq; ++i)
2079 swap_vars(blk,
2080 bmap->eq[i] + pos, n1, n2);
2082 for (i = 0; i < bmap->n_ineq; ++i)
2083 swap_vars(blk,
2084 bmap->ineq[i] + pos, n1, n2);
2086 for (i = 0; i < bmap->n_div; ++i)
2087 swap_vars(blk,
2088 bmap->div[i]+1 + pos, n1, n2);
2090 isl_blk_free(bmap->ctx, blk);
2092 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2093 bmap = isl_basic_map_gauss(bmap, NULL);
2094 return isl_basic_map_finalize(bmap);
2095 error:
2096 isl_basic_map_free(bmap);
2097 return NULL;
2100 /* The given basic map has turned out to be empty.
2101 * Explicitly mark it as such and change the representation
2102 * to a canonical representation of the empty basic map.
2103 * Since the basic map has conflicting constraints,
2104 * it must have at least one constraint, except perhaps
2105 * if it was already explicitly marked as being empty.
2106 * Do nothing in the latter case.
2108 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2109 __isl_take isl_basic_map *bmap)
2111 int i = 0;
2112 isl_bool empty;
2113 isl_size total;
2115 empty = isl_basic_map_plain_is_empty(bmap);
2116 if (empty < 0)
2117 return isl_basic_map_free(bmap);
2118 if (empty)
2119 return bmap;
2120 total = isl_basic_map_dim(bmap, isl_dim_all);
2121 if (total < 0)
2122 return isl_basic_map_free(bmap);
2123 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2124 return isl_basic_map_free(bmap);
2125 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2126 if (!bmap)
2127 return NULL;
2128 if (bmap->n_eq > 0) {
2129 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2130 if (!bmap)
2131 return NULL;
2132 } else {
2133 i = isl_basic_map_alloc_equality(bmap);
2134 if (i < 0)
2135 goto error;
2137 isl_int_set_si(bmap->eq[i][0], 1);
2138 isl_seq_clr(bmap->eq[i]+1, total);
2139 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2140 isl_vec_free(bmap->sample);
2141 bmap->sample = NULL;
2142 return isl_basic_map_finalize(bmap);
2143 error:
2144 isl_basic_map_free(bmap);
2145 return NULL;
2148 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2149 __isl_take isl_basic_set *bset)
2151 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2154 __isl_give isl_basic_map *isl_basic_map_set_rational(
2155 __isl_take isl_basic_map *bmap)
2157 if (!bmap)
2158 return NULL;
2160 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2161 return bmap;
2163 bmap = isl_basic_map_cow(bmap);
2164 if (!bmap)
2165 return NULL;
2167 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2169 return isl_basic_map_finalize(bmap);
2172 __isl_give isl_basic_set *isl_basic_set_set_rational(
2173 __isl_take isl_basic_set *bset)
2175 return isl_basic_map_set_rational(bset);
2178 __isl_give isl_basic_set *isl_basic_set_set_integral(
2179 __isl_take isl_basic_set *bset)
2181 if (!bset)
2182 return NULL;
2184 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2185 return bset;
2187 bset = isl_basic_set_cow(bset);
2188 if (!bset)
2189 return NULL;
2191 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2193 return isl_basic_set_finalize(bset);
2196 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2198 int i;
2200 map = isl_map_cow(map);
2201 if (!map)
2202 return NULL;
2203 for (i = 0; i < map->n; ++i) {
2204 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2205 if (!map->p[i])
2206 goto error;
2208 return map;
2209 error:
2210 isl_map_free(map);
2211 return NULL;
2214 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2216 return isl_map_set_rational(set);
2219 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2220 * of "bmap").
2222 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2224 isl_int *t = bmap->div[a];
2225 bmap->div[a] = bmap->div[b];
2226 bmap->div[b] = t;
2229 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2230 * div definitions accordingly.
2232 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2233 int a, int b)
2235 int i;
2236 isl_size off;
2238 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2239 if (off < 0)
2240 return isl_basic_map_free(bmap);
2242 swap_div(bmap, a, b);
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2250 for (i = 0; i < bmap->n_div; ++i)
2251 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2252 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2254 return bmap;
2257 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2259 isl_seq_cpy(c, c + n, rem);
2260 isl_seq_clr(c + rem, n);
2263 /* Drop n dimensions starting at first.
2265 * In principle, this frees up some extra variables as the number
2266 * of columns remains constant, but we would have to extend
2267 * the div array too as the number of rows in this array is assumed
2268 * to be equal to extra.
2270 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2271 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2273 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2276 /* Move "n" divs starting at "first" to the end of the list of divs.
2278 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2279 unsigned first, unsigned n)
2281 isl_int **div;
2282 int i;
2284 if (first + n == bmap->n_div)
2285 return bmap;
2287 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2288 if (!div)
2289 goto error;
2290 for (i = 0; i < n; ++i)
2291 div[i] = bmap->div[first + i];
2292 for (i = 0; i < bmap->n_div - first - n; ++i)
2293 bmap->div[first + i] = bmap->div[first + n + i];
2294 for (i = 0; i < n; ++i)
2295 bmap->div[bmap->n_div - n + i] = div[i];
2296 free(div);
2297 return bmap;
2298 error:
2299 isl_basic_map_free(bmap);
2300 return NULL;
2303 #undef TYPE
2304 #define TYPE isl_map
2305 static
2306 #include "check_type_range_templ.c"
2308 /* Check that there are "n" dimensions of type "type" starting at "first"
2309 * in "set".
2311 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2312 enum isl_dim_type type, unsigned first, unsigned n)
2314 return isl_map_check_range(set_to_map(set), type, first, n);
2317 /* Drop "n" dimensions of type "type" starting at "first".
2318 * Perform the core computation, without cowing or
2319 * simplifying and finalizing the result.
2321 * In principle, this frees up some extra variables as the number
2322 * of columns remains constant, but we would have to extend
2323 * the div array too as the number of rows in this array is assumed
2324 * to be equal to extra.
2326 __isl_give isl_basic_map *isl_basic_map_drop_core(
2327 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2328 unsigned first, unsigned n)
2330 int i;
2331 unsigned offset;
2332 unsigned left;
2333 isl_size total;
2335 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2336 return isl_basic_map_free(bmap);
2338 total = isl_basic_map_dim(bmap, isl_dim_all);
2339 if (total < 0)
2340 return isl_basic_map_free(bmap);
2342 offset = isl_basic_map_offset(bmap, type) + first;
2343 left = total - (offset - 1) - n;
2344 for (i = 0; i < bmap->n_eq; ++i)
2345 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2347 for (i = 0; i < bmap->n_ineq; ++i)
2348 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2350 for (i = 0; i < bmap->n_div; ++i)
2351 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2353 if (type == isl_dim_div) {
2354 bmap = move_divs_last(bmap, first, n);
2355 if (!bmap)
2356 return NULL;
2357 if (isl_basic_map_free_div(bmap, n) < 0)
2358 return isl_basic_map_free(bmap);
2359 } else
2360 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2361 if (!bmap->dim)
2362 return isl_basic_map_free(bmap);
2364 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2365 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2366 return bmap;
2369 /* Drop "n" dimensions of type "type" starting at "first".
2371 * In principle, this frees up some extra variables as the number
2372 * of columns remains constant, but we would have to extend
2373 * the div array too as the number of rows in this array is assumed
2374 * to be equal to extra.
2376 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 if (!bmap)
2380 return NULL;
2381 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2382 return bmap;
2384 bmap = isl_basic_map_cow(bmap);
2385 if (!bmap)
2386 return NULL;
2388 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2390 bmap = isl_basic_map_simplify(bmap);
2391 return isl_basic_map_finalize(bmap);
2394 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2398 type, first, n));
2401 /* No longer consider "map" to be normalized.
2403 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2405 if (!map)
2406 return NULL;
2407 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2408 return map;
2411 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2412 enum isl_dim_type type, unsigned first, unsigned n)
2414 int i;
2415 isl_space *space;
2417 if (isl_map_check_range(map, type, first, n) < 0)
2418 return isl_map_free(map);
2420 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2421 return map;
2422 map = isl_map_cow(map);
2423 if (!map)
2424 goto error;
2426 for (i = 0; i < map->n; ++i) {
2427 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2428 if (!map->p[i])
2429 goto error;
2431 map = isl_map_unmark_normalized(map);
2433 space = isl_map_take_space(map);
2434 space = isl_space_drop_dims(space, type, first, n);
2435 map = isl_map_restore_space(map, space);
2437 return map;
2438 error:
2439 isl_map_free(map);
2440 return NULL;
2443 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2444 enum isl_dim_type type, unsigned first, unsigned n)
2446 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2449 /* Drop the integer division at position "div", which is assumed
2450 * not to appear in any of the constraints or
2451 * in any of the other integer divisions.
2453 * Since the integer division is redundant, there is no need to cow.
2455 __isl_give isl_basic_map *isl_basic_map_drop_div(
2456 __isl_take isl_basic_map *bmap, unsigned div)
2458 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2461 /* Eliminate the specified n dimensions starting at first from the
2462 * constraints, without removing the dimensions from the space.
2463 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2465 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2466 enum isl_dim_type type, unsigned first, unsigned n)
2468 int i;
2470 if (n == 0)
2471 return map;
2473 if (isl_map_check_range(map, type, first, n) < 0)
2474 return isl_map_free(map);
2476 map = isl_map_cow(map);
2477 if (!map)
2478 return NULL;
2480 for (i = 0; i < map->n; ++i) {
2481 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2482 if (!map->p[i])
2483 goto error;
2485 return map;
2486 error:
2487 isl_map_free(map);
2488 return NULL;
2491 /* Eliminate the specified n dimensions starting at first from the
2492 * constraints, without removing the dimensions from the space.
2493 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2495 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2496 enum isl_dim_type type, unsigned first, unsigned n)
2498 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2501 /* Eliminate the specified n dimensions starting at first from the
2502 * constraints, without removing the dimensions from the space.
2503 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2505 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2506 unsigned first, unsigned n)
2508 return isl_set_eliminate(set, isl_dim_set, first, n);
2511 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2512 __isl_take isl_basic_map *bmap)
2514 isl_size v_div;
2516 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2517 if (v_div < 0)
2518 return isl_basic_map_free(bmap);
2519 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2520 if (!bmap)
2521 return NULL;
2522 bmap->n_div = 0;
2523 return isl_basic_map_finalize(bmap);
2526 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2527 __isl_take isl_basic_set *bset)
2529 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2532 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2534 int i;
2536 if (!map)
2537 return NULL;
2538 if (map->n == 0)
2539 return map;
2541 map = isl_map_cow(map);
2542 if (!map)
2543 return NULL;
2545 for (i = 0; i < map->n; ++i) {
2546 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2547 if (!map->p[i])
2548 goto error;
2550 return map;
2551 error:
2552 isl_map_free(map);
2553 return NULL;
2556 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2558 return isl_map_remove_divs(set);
2561 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2562 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2563 unsigned first, unsigned n)
2565 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2566 return isl_basic_map_free(bmap);
2567 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2568 return bmap;
2569 bmap = isl_basic_map_eliminate_vars(bmap,
2570 isl_basic_map_offset(bmap, type) - 1 + first, n);
2571 if (!bmap)
2572 return bmap;
2573 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2574 return bmap;
2575 bmap = isl_basic_map_drop(bmap, type, first, n);
2576 return bmap;
2579 /* Return true if the definition of the given div (recursively) involves
2580 * any of the given variables.
2582 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2583 unsigned first, unsigned n)
2585 int i;
2586 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2588 if (isl_int_is_zero(bmap->div[div][0]))
2589 return isl_bool_false;
2590 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2591 return isl_bool_true;
2593 for (i = bmap->n_div - 1; i >= 0; --i) {
2594 isl_bool involves;
2596 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2597 continue;
2598 involves = div_involves_vars(bmap, i, first, n);
2599 if (involves < 0 || involves)
2600 return involves;
2603 return isl_bool_false;
2606 /* Try and add a lower and/or upper bound on "div" to "bmap"
2607 * based on inequality "i".
2608 * "total" is the total number of variables (excluding the divs).
2609 * "v" is a temporary object that can be used during the calculations.
2610 * If "lb" is set, then a lower bound should be constructed.
2611 * If "ub" is set, then an upper bound should be constructed.
2613 * The calling function has already checked that the inequality does not
2614 * reference "div", but we still need to check that the inequality is
2615 * of the right form. We'll consider the case where we want to construct
2616 * a lower bound. The construction of upper bounds is similar.
2618 * Let "div" be of the form
2620 * q = floor((a + f(x))/d)
2622 * We essentially check if constraint "i" is of the form
2624 * b + f(x) >= 0
2626 * so that we can use it to derive a lower bound on "div".
2627 * However, we allow a slightly more general form
2629 * b + g(x) >= 0
2631 * with the condition that the coefficients of g(x) - f(x) are all
2632 * divisible by d.
2633 * Rewriting this constraint as
2635 * 0 >= -b - g(x)
2637 * adding a + f(x) to both sides and dividing by d, we obtain
2639 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2641 * Taking the floor on both sides, we obtain
2643 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2645 * or
2647 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2649 * In the case of an upper bound, we construct the constraint
2651 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2654 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2655 __isl_take isl_basic_map *bmap, int div, int i,
2656 unsigned total, isl_int v, int lb, int ub)
2658 int j;
2660 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2661 if (lb) {
2662 isl_int_sub(v, bmap->ineq[i][1 + j],
2663 bmap->div[div][1 + 1 + j]);
2664 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2666 if (ub) {
2667 isl_int_add(v, bmap->ineq[i][1 + j],
2668 bmap->div[div][1 + 1 + j]);
2669 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2672 if (!lb && !ub)
2673 return bmap;
2675 bmap = isl_basic_map_cow(bmap);
2676 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2677 if (lb) {
2678 int k = isl_basic_map_alloc_inequality(bmap);
2679 if (k < 0)
2680 goto error;
2681 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2682 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2683 bmap->div[div][1 + j]);
2684 isl_int_cdiv_q(bmap->ineq[k][j],
2685 bmap->ineq[k][j], bmap->div[div][0]);
2687 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2689 if (ub) {
2690 int k = isl_basic_map_alloc_inequality(bmap);
2691 if (k < 0)
2692 goto error;
2693 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2694 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2695 bmap->div[div][1 + j]);
2696 isl_int_fdiv_q(bmap->ineq[k][j],
2697 bmap->ineq[k][j], bmap->div[div][0]);
2699 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2702 return bmap;
2703 error:
2704 isl_basic_map_free(bmap);
2705 return NULL;
2708 /* This function is called right before "div" is eliminated from "bmap"
2709 * using Fourier-Motzkin.
2710 * Look through the constraints of "bmap" for constraints on the argument
2711 * of the integer division and use them to construct constraints on the
2712 * integer division itself. These constraints can then be combined
2713 * during the Fourier-Motzkin elimination.
2714 * Note that it is only useful to introduce lower bounds on "div"
2715 * if "bmap" already contains upper bounds on "div" as the newly
2716 * introduce lower bounds can then be combined with the pre-existing
2717 * upper bounds. Similarly for upper bounds.
2718 * We therefore first check if "bmap" contains any lower and/or upper bounds
2719 * on "div".
2721 * It is interesting to note that the introduction of these constraints
2722 * can indeed lead to more accurate results, even when compared to
2723 * deriving constraints on the argument of "div" from constraints on "div".
2724 * Consider, for example, the set
2726 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2728 * The second constraint can be rewritten as
2730 * 2 * [(-i-2j+3)/4] + k >= 0
2732 * from which we can derive
2734 * -i - 2j + 3 >= -2k
2736 * or
2738 * i + 2j <= 3 + 2k
2740 * Combined with the first constraint, we obtain
2742 * -3 <= 3 + 2k or k >= -3
2744 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2745 * the first constraint, we obtain
2747 * [(i + 2j)/4] >= [-3/4] = -1
2749 * Combining this constraint with the second constraint, we obtain
2751 * k >= -2
2753 static __isl_give isl_basic_map *insert_bounds_on_div(
2754 __isl_take isl_basic_map *bmap, int div)
2756 int i;
2757 int check_lb, check_ub;
2758 isl_int v;
2759 isl_size v_div;
2761 if (!bmap)
2762 return NULL;
2764 if (isl_int_is_zero(bmap->div[div][0]))
2765 return bmap;
2767 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2768 if (v_div < 0)
2769 return isl_basic_map_free(bmap);
2771 check_lb = 0;
2772 check_ub = 0;
2773 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2774 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2775 if (s > 0)
2776 check_ub = 1;
2777 if (s < 0)
2778 check_lb = 1;
2781 if (!check_lb && !check_ub)
2782 return bmap;
2784 isl_int_init(v);
2786 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2787 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2788 continue;
2790 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2791 check_lb, check_ub);
2794 isl_int_clear(v);
2796 return bmap;
2799 /* Remove all divs (recursively) involving any of the given dimensions
2800 * in their definitions.
2802 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2803 __isl_take isl_basic_map *bmap,
2804 enum isl_dim_type type, unsigned first, unsigned n)
2806 int i;
2808 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2809 return isl_basic_map_free(bmap);
2810 first += isl_basic_map_offset(bmap, type);
2812 for (i = bmap->n_div - 1; i >= 0; --i) {
2813 isl_bool involves;
2815 involves = div_involves_vars(bmap, i, first, n);
2816 if (involves < 0)
2817 return isl_basic_map_free(bmap);
2818 if (!involves)
2819 continue;
2820 bmap = insert_bounds_on_div(bmap, i);
2821 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2822 if (!bmap)
2823 return NULL;
2824 i = bmap->n_div;
2827 return bmap;
2830 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2831 __isl_take isl_basic_set *bset,
2832 enum isl_dim_type type, unsigned first, unsigned n)
2834 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2837 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2838 enum isl_dim_type type, unsigned first, unsigned n)
2840 int i;
2842 if (!map)
2843 return NULL;
2844 if (map->n == 0)
2845 return map;
2847 map = isl_map_cow(map);
2848 if (!map)
2849 return NULL;
2851 for (i = 0; i < map->n; ++i) {
2852 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2853 type, first, n);
2854 if (!map->p[i])
2855 goto error;
2857 return map;
2858 error:
2859 isl_map_free(map);
2860 return NULL;
2863 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2864 enum isl_dim_type type, unsigned first, unsigned n)
2866 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2867 type, first, n));
2870 /* Does the description of "bmap" depend on the specified dimensions?
2871 * We also check whether the dimensions appear in any of the div definitions.
2872 * In principle there is no need for this check. If the dimensions appear
2873 * in a div definition, they also appear in the defining constraints of that
2874 * div.
2876 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2877 enum isl_dim_type type, unsigned first, unsigned n)
2879 int i;
2881 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2882 return isl_bool_error;
2884 first += isl_basic_map_offset(bmap, type);
2885 for (i = 0; i < bmap->n_eq; ++i)
2886 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2887 return isl_bool_true;
2888 for (i = 0; i < bmap->n_ineq; ++i)
2889 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2890 return isl_bool_true;
2891 for (i = 0; i < bmap->n_div; ++i) {
2892 if (isl_int_is_zero(bmap->div[i][0]))
2893 continue;
2894 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2895 return isl_bool_true;
2898 return isl_bool_false;
2901 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2902 enum isl_dim_type type, unsigned first, unsigned n)
2904 int i;
2906 if (isl_map_check_range(map, type, first, n) < 0)
2907 return isl_bool_error;
2909 for (i = 0; i < map->n; ++i) {
2910 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2911 type, first, n);
2912 if (involves < 0 || involves)
2913 return involves;
2916 return isl_bool_false;
2919 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2920 enum isl_dim_type type, unsigned first, unsigned n)
2922 return isl_basic_map_involves_dims(bset, type, first, n);
2925 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2926 enum isl_dim_type type, unsigned first, unsigned n)
2928 return isl_map_involves_dims(set, type, first, n);
2931 /* Does "bset" involve any local variables, i.e., integer divisions?
2933 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2935 isl_size n;
2937 n = isl_basic_set_dim(bset, isl_dim_div);
2938 if (n < 0)
2939 return isl_bool_error;
2940 return isl_bool_ok(n > 0);
2943 /* isl_set_every_basic_set callback that checks whether "bset"
2944 * is free of local variables.
2946 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
2948 return isl_bool_not(isl_basic_set_involves_locals(bset));
2951 /* Does "set" involve any local variables, i.e., integer divisions?
2953 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
2955 isl_bool no_locals;
2957 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
2958 return isl_bool_not(no_locals);
2961 /* Drop all constraints in bmap that involve any of the dimensions
2962 * first to first+n-1.
2963 * This function only performs the actual removal of constraints.
2965 * This function should not call finalize since it is used by
2966 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2968 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2969 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2971 int i;
2973 if (n == 0)
2974 return bmap;
2976 bmap = isl_basic_map_cow(bmap);
2978 if (!bmap)
2979 return NULL;
2981 for (i = bmap->n_eq - 1; i >= 0; --i) {
2982 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2983 continue;
2984 if (isl_basic_map_drop_equality(bmap, i) < 0)
2985 return isl_basic_map_free(bmap);
2988 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2989 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2990 continue;
2991 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2992 return isl_basic_map_free(bmap);
2995 return bmap;
2998 /* Drop all constraints in bset that involve any of the dimensions
2999 * first to first+n-1.
3000 * This function only performs the actual removal of constraints.
3002 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3003 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3005 return isl_basic_map_drop_constraints_involving(bset, first, n);
3008 /* Drop all constraints in bmap that do not involve any of the dimensions
3009 * first to first + n - 1 of the given type.
3011 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3012 __isl_take isl_basic_map *bmap,
3013 enum isl_dim_type type, unsigned first, unsigned n)
3015 int i;
3017 if (n == 0) {
3018 isl_space *space = isl_basic_map_get_space(bmap);
3019 isl_basic_map_free(bmap);
3020 return isl_basic_map_universe(space);
3022 bmap = isl_basic_map_cow(bmap);
3023 if (!bmap)
3024 return NULL;
3026 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3027 return isl_basic_map_free(bmap);
3029 first += isl_basic_map_offset(bmap, type) - 1;
3031 for (i = bmap->n_eq - 1; i >= 0; --i) {
3032 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3033 continue;
3034 if (isl_basic_map_drop_equality(bmap, i) < 0)
3035 return isl_basic_map_free(bmap);
3038 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3039 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3040 continue;
3041 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3042 return isl_basic_map_free(bmap);
3045 bmap = isl_basic_map_add_known_div_constraints(bmap);
3046 return bmap;
3049 /* Drop all constraints in bset that do not involve any of the dimensions
3050 * first to first + n - 1 of the given type.
3052 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3053 __isl_take isl_basic_set *bset,
3054 enum isl_dim_type type, unsigned first, unsigned n)
3056 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3057 type, first, n);
3060 /* Drop all constraints in bmap that involve any of the dimensions
3061 * first to first + n - 1 of the given type.
3063 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3064 __isl_take isl_basic_map *bmap,
3065 enum isl_dim_type type, unsigned first, unsigned n)
3067 if (!bmap)
3068 return NULL;
3069 if (n == 0)
3070 return bmap;
3072 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3073 return isl_basic_map_free(bmap);
3075 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3076 first += isl_basic_map_offset(bmap, type) - 1;
3077 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3078 bmap = isl_basic_map_add_known_div_constraints(bmap);
3079 return bmap;
3082 /* Drop all constraints in bset that involve any of the dimensions
3083 * first to first + n - 1 of the given type.
3085 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3086 __isl_take isl_basic_set *bset,
3087 enum isl_dim_type type, unsigned first, unsigned n)
3089 return isl_basic_map_drop_constraints_involving_dims(bset,
3090 type, first, n);
3093 /* Drop constraints from "map" by applying "drop" to each basic map.
3095 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3096 enum isl_dim_type type, unsigned first, unsigned n,
3097 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3098 enum isl_dim_type type, unsigned first, unsigned n))
3100 int i;
3102 if (isl_map_check_range(map, type, first, n) < 0)
3103 return isl_map_free(map);
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3109 for (i = 0; i < map->n; ++i) {
3110 map->p[i] = drop(map->p[i], type, first, n);
3111 if (!map->p[i])
3112 return isl_map_free(map);
3115 if (map->n > 1)
3116 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3118 return map;
3121 /* Drop all constraints in map that involve any of the dimensions
3122 * first to first + n - 1 of the given type.
3124 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3125 __isl_take isl_map *map,
3126 enum isl_dim_type type, unsigned first, unsigned n)
3128 if (n == 0)
3129 return map;
3130 return drop_constraints(map, type, first, n,
3131 &isl_basic_map_drop_constraints_involving_dims);
3134 /* Drop all constraints in "map" that do not involve any of the dimensions
3135 * first to first + n - 1 of the given type.
3137 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3138 __isl_take isl_map *map,
3139 enum isl_dim_type type, unsigned first, unsigned n)
3141 if (n == 0) {
3142 isl_space *space = isl_map_get_space(map);
3143 isl_map_free(map);
3144 return isl_map_universe(space);
3146 return drop_constraints(map, type, first, n,
3147 &isl_basic_map_drop_constraints_not_involving_dims);
3150 /* Drop all constraints in set that involve any of the dimensions
3151 * first to first + n - 1 of the given type.
3153 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3154 __isl_take isl_set *set,
3155 enum isl_dim_type type, unsigned first, unsigned n)
3157 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3160 /* Drop all constraints in "set" that do not involve any of the dimensions
3161 * first to first + n - 1 of the given type.
3163 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3164 __isl_take isl_set *set,
3165 enum isl_dim_type type, unsigned first, unsigned n)
3167 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3170 /* Does local variable "div" of "bmap" have a complete explicit representation?
3171 * Having a complete explicit representation requires not only
3172 * an explicit representation, but also that all local variables
3173 * that appear in this explicit representation in turn have
3174 * a complete explicit representation.
3176 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3178 int i;
3179 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3180 isl_bool marked;
3182 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3183 if (marked < 0 || marked)
3184 return isl_bool_not(marked);
3186 for (i = bmap->n_div - 1; i >= 0; --i) {
3187 isl_bool known;
3189 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3190 continue;
3191 known = isl_basic_map_div_is_known(bmap, i);
3192 if (known < 0 || !known)
3193 return known;
3196 return isl_bool_true;
3199 /* Remove all divs that are unknown or defined in terms of unknown divs.
3201 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3202 __isl_take isl_basic_map *bmap)
3204 int i;
3206 if (!bmap)
3207 return NULL;
3209 for (i = bmap->n_div - 1; i >= 0; --i) {
3210 if (isl_basic_map_div_is_known(bmap, i))
3211 continue;
3212 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3213 if (!bmap)
3214 return NULL;
3215 i = bmap->n_div;
3218 return bmap;
3221 /* Remove all divs that are unknown or defined in terms of unknown divs.
3223 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3224 __isl_take isl_basic_set *bset)
3226 return isl_basic_map_remove_unknown_divs(bset);
3229 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3231 int i;
3233 if (!map)
3234 return NULL;
3235 if (map->n == 0)
3236 return map;
3238 map = isl_map_cow(map);
3239 if (!map)
3240 return NULL;
3242 for (i = 0; i < map->n; ++i) {
3243 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3244 if (!map->p[i])
3245 goto error;
3247 return map;
3248 error:
3249 isl_map_free(map);
3250 return NULL;
3253 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3255 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3258 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3259 __isl_take isl_basic_set *bset,
3260 enum isl_dim_type type, unsigned first, unsigned n)
3262 isl_basic_map *bmap = bset_to_bmap(bset);
3263 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3264 return bset_from_bmap(bmap);
3267 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3268 enum isl_dim_type type, unsigned first, unsigned n)
3270 int i;
3272 if (n == 0)
3273 return map;
3275 map = isl_map_cow(map);
3276 if (isl_map_check_range(map, type, first, n) < 0)
3277 return isl_map_free(map);
3279 for (i = 0; i < map->n; ++i) {
3280 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3281 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3282 if (!map->p[i])
3283 goto error;
3285 map = isl_map_drop(map, type, first, n);
3286 return map;
3287 error:
3288 isl_map_free(map);
3289 return NULL;
3292 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3293 enum isl_dim_type type, unsigned first, unsigned n)
3295 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3296 type, first, n));
3299 /* Project out n inputs starting at first using Fourier-Motzkin */
3300 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3301 unsigned first, unsigned n)
3303 return isl_map_remove_dims(map, isl_dim_in, first, n);
3306 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3307 FILE *out, int indent)
3309 isl_printer *p;
3311 if (!bset) {
3312 fprintf(out, "null basic set\n");
3313 return;
3316 fprintf(out, "%*s", indent, "");
3317 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3318 bset->ref, bset->dim->nparam, bset->dim->n_out,
3319 bset->extra, bset->flags);
3321 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3322 p = isl_printer_set_dump(p, 1);
3323 p = isl_printer_set_indent(p, indent);
3324 p = isl_printer_start_line(p);
3325 p = isl_printer_print_basic_set(p, bset);
3326 p = isl_printer_end_line(p);
3327 isl_printer_free(p);
3330 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3331 FILE *out, int indent)
3333 isl_printer *p;
3335 if (!bmap) {
3336 fprintf(out, "null basic map\n");
3337 return;
3340 fprintf(out, "%*s", indent, "");
3341 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3342 "flags: %x, n_name: %d\n",
3343 bmap->ref,
3344 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3345 bmap->extra, bmap->flags, bmap->dim->n_id);
3347 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3348 p = isl_printer_set_dump(p, 1);
3349 p = isl_printer_set_indent(p, indent);
3350 p = isl_printer_start_line(p);
3351 p = isl_printer_print_basic_map(p, bmap);
3352 p = isl_printer_end_line(p);
3353 isl_printer_free(p);
3356 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3357 unsigned pos)
3359 isl_size total;
3361 total = isl_basic_map_dim(bmap, isl_dim_all);
3362 if (total < 0)
3363 return isl_basic_map_free(bmap);
3364 if (pos >= bmap->n_ineq)
3365 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3366 "invalid position", return isl_basic_map_free(bmap));
3367 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3368 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3369 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3370 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3371 return bmap;
3374 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3375 unsigned flags)
3377 if (isl_space_check_is_set(space) < 0)
3378 goto error;
3379 return isl_map_alloc_space(space, n, flags);
3380 error:
3381 isl_space_free(space);
3382 return NULL;
3385 /* Make sure "map" has room for at least "n" more basic maps.
3387 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3389 int i;
3390 struct isl_map *grown = NULL;
3392 if (!map)
3393 return NULL;
3394 isl_assert(map->ctx, n >= 0, goto error);
3395 if (map->n + n <= map->size)
3396 return map;
3397 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3398 if (!grown)
3399 goto error;
3400 for (i = 0; i < map->n; ++i) {
3401 grown->p[i] = isl_basic_map_copy(map->p[i]);
3402 if (!grown->p[i])
3403 goto error;
3404 grown->n++;
3406 isl_map_free(map);
3407 return grown;
3408 error:
3409 isl_map_free(grown);
3410 isl_map_free(map);
3411 return NULL;
3414 /* Make sure "set" has room for at least "n" more basic sets.
3416 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3418 return set_from_map(isl_map_grow(set_to_map(set), n));
3421 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3423 return isl_map_from_basic_map(bset);
3426 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3428 struct isl_map *map;
3430 if (!bmap)
3431 return NULL;
3433 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3434 return isl_map_add_basic_map(map, bmap);
3437 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3438 __isl_take isl_basic_set *bset)
3440 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3441 bset_to_bmap(bset)));
3444 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3446 return isl_map_free(set);
3449 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3451 int i;
3453 if (!set) {
3454 fprintf(out, "null set\n");
3455 return;
3458 fprintf(out, "%*s", indent, "");
3459 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3460 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3461 set->flags);
3462 for (i = 0; i < set->n; ++i) {
3463 fprintf(out, "%*s", indent, "");
3464 fprintf(out, "basic set %d:\n", i);
3465 isl_basic_set_print_internal(set->p[i], out, indent+4);
3469 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3471 int i;
3473 if (!map) {
3474 fprintf(out, "null map\n");
3475 return;
3478 fprintf(out, "%*s", indent, "");
3479 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3480 "flags: %x, n_name: %d\n",
3481 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3482 map->dim->n_out, map->flags, map->dim->n_id);
3483 for (i = 0; i < map->n; ++i) {
3484 fprintf(out, "%*s", indent, "");
3485 fprintf(out, "basic map %d:\n", i);
3486 isl_basic_map_print_internal(map->p[i], out, indent+4);
3490 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3492 static isl_stat isl_basic_map_check_compatible_domain(
3493 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3495 isl_bool ok;
3497 ok = isl_basic_map_compatible_domain(bmap, bset);
3498 if (ok < 0)
3499 return isl_stat_error;
3500 if (!ok)
3501 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3502 "incompatible spaces", return isl_stat_error);
3504 return isl_stat_ok;
3507 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3508 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3510 struct isl_basic_map *bmap_domain;
3511 isl_size dim;
3513 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3514 goto error;
3516 dim = isl_basic_set_dim(bset, isl_dim_set);
3517 if (dim < 0)
3518 goto error;
3519 if (dim != 0 &&
3520 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3521 goto error;
3523 bmap = isl_basic_map_cow(bmap);
3524 if (!bmap)
3525 goto error;
3526 bmap = isl_basic_map_extend(bmap,
3527 bset->n_div, bset->n_eq, bset->n_ineq);
3528 bmap_domain = isl_basic_map_from_domain(bset);
3529 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3531 bmap = isl_basic_map_simplify(bmap);
3532 return isl_basic_map_finalize(bmap);
3533 error:
3534 isl_basic_map_free(bmap);
3535 isl_basic_set_free(bset);
3536 return NULL;
3539 /* Check that the space of "bset" is the same as that of the range of "bmap".
3541 static isl_stat isl_basic_map_check_compatible_range(
3542 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3544 isl_bool ok;
3546 ok = isl_basic_map_compatible_range(bmap, bset);
3547 if (ok < 0)
3548 return isl_stat_error;
3549 if (!ok)
3550 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3551 "incompatible spaces", return isl_stat_error);
3553 return isl_stat_ok;
3556 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3557 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3559 struct isl_basic_map *bmap_range;
3560 isl_size dim;
3562 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3563 goto error;
3565 dim = isl_basic_set_dim(bset, isl_dim_set);
3566 if (dim < 0)
3567 goto error;
3568 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3569 goto error;
3571 if (isl_basic_set_plain_is_universe(bset)) {
3572 isl_basic_set_free(bset);
3573 return bmap;
3576 bmap = isl_basic_map_cow(bmap);
3577 if (!bmap)
3578 goto error;
3579 bmap = isl_basic_map_extend(bmap,
3580 bset->n_div, bset->n_eq, bset->n_ineq);
3581 bmap_range = bset_to_bmap(bset);
3582 bmap = add_constraints(bmap, bmap_range, 0, 0);
3584 bmap = isl_basic_map_simplify(bmap);
3585 return isl_basic_map_finalize(bmap);
3586 error:
3587 isl_basic_map_free(bmap);
3588 isl_basic_set_free(bset);
3589 return NULL;
3592 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3593 __isl_keep isl_vec *vec)
3595 int i;
3596 isl_size total;
3597 isl_int s;
3599 total = isl_basic_map_dim(bmap, isl_dim_all);
3600 if (total < 0 || !vec)
3601 return isl_bool_error;
3603 if (1 + total != vec->size)
3604 return isl_bool_false;
3606 isl_int_init(s);
3608 for (i = 0; i < bmap->n_eq; ++i) {
3609 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3610 if (!isl_int_is_zero(s)) {
3611 isl_int_clear(s);
3612 return isl_bool_false;
3616 for (i = 0; i < bmap->n_ineq; ++i) {
3617 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3618 if (isl_int_is_neg(s)) {
3619 isl_int_clear(s);
3620 return isl_bool_false;
3624 isl_int_clear(s);
3626 return isl_bool_true;
3629 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3630 __isl_keep isl_vec *vec)
3632 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3635 __isl_give isl_basic_map *isl_basic_map_intersect(
3636 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3638 struct isl_vec *sample = NULL;
3639 isl_space *space1, *space2;
3640 isl_size dim1, dim2, nparam1, nparam2;
3642 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3643 goto error;
3644 space1 = isl_basic_map_peek_space(bmap1);
3645 space2 = isl_basic_map_peek_space(bmap2);
3646 dim1 = isl_space_dim(space1, isl_dim_all);
3647 dim2 = isl_space_dim(space2, isl_dim_all);
3648 nparam1 = isl_space_dim(space1, isl_dim_param);
3649 nparam2 = isl_space_dim(space2, isl_dim_param);
3650 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3651 goto error;
3652 if (dim1 == nparam1 && dim2 != nparam2)
3653 return isl_basic_map_intersect(bmap2, bmap1);
3655 if (dim2 != nparam2 &&
3656 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3657 goto error;
3659 if (isl_basic_map_plain_is_empty(bmap1)) {
3660 isl_basic_map_free(bmap2);
3661 return bmap1;
3663 if (isl_basic_map_plain_is_empty(bmap2)) {
3664 isl_basic_map_free(bmap1);
3665 return bmap2;
3668 if (bmap1->sample &&
3669 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3670 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3671 sample = isl_vec_copy(bmap1->sample);
3672 else if (bmap2->sample &&
3673 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3674 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3675 sample = isl_vec_copy(bmap2->sample);
3677 bmap1 = isl_basic_map_cow(bmap1);
3678 if (!bmap1)
3679 goto error;
3680 bmap1 = isl_basic_map_extend(bmap1,
3681 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3682 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3684 if (!bmap1)
3685 isl_vec_free(sample);
3686 else if (sample) {
3687 isl_vec_free(bmap1->sample);
3688 bmap1->sample = sample;
3691 bmap1 = isl_basic_map_simplify(bmap1);
3692 return isl_basic_map_finalize(bmap1);
3693 error:
3694 if (sample)
3695 isl_vec_free(sample);
3696 isl_basic_map_free(bmap1);
3697 isl_basic_map_free(bmap2);
3698 return NULL;
3701 __isl_give isl_basic_set *isl_basic_set_intersect(
3702 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3704 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3705 bset_to_bmap(bset2)));
3708 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3709 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3711 return isl_basic_set_intersect(bset1, bset2);
3714 /* Special case of isl_map_intersect, where both map1 and map2
3715 * are convex, without any divs and such that either map1 or map2
3716 * contains a single constraint. This constraint is then simply
3717 * added to the other map.
3719 static __isl_give isl_map *map_intersect_add_constraint(
3720 __isl_take isl_map *map1, __isl_take isl_map *map2)
3722 isl_assert(map1->ctx, map1->n == 1, goto error);
3723 isl_assert(map2->ctx, map1->n == 1, goto error);
3724 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3725 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3727 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3728 return isl_map_intersect(map2, map1);
3730 map1 = isl_map_cow(map1);
3731 if (!map1)
3732 goto error;
3733 if (isl_map_plain_is_empty(map1)) {
3734 isl_map_free(map2);
3735 return map1;
3737 if (map2->p[0]->n_eq == 1)
3738 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3739 else
3740 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3741 map2->p[0]->ineq[0]);
3743 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3744 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3745 if (!map1->p[0])
3746 goto error;
3748 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3749 isl_basic_map_free(map1->p[0]);
3750 map1->n = 0;
3753 isl_map_free(map2);
3755 map1 = isl_map_unmark_normalized(map1);
3756 return map1;
3757 error:
3758 isl_map_free(map1);
3759 isl_map_free(map2);
3760 return NULL;
3763 /* map2 may be either a parameter domain or a map living in the same
3764 * space as map1.
3766 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3767 __isl_take isl_map *map2)
3769 unsigned flags = 0;
3770 isl_bool equal;
3771 isl_map *result;
3772 int i, j;
3773 isl_size dim2, nparam2;
3775 if (!map1 || !map2)
3776 goto error;
3778 if ((isl_map_plain_is_empty(map1) ||
3779 isl_map_plain_is_universe(map2)) &&
3780 isl_space_is_equal(map1->dim, map2->dim)) {
3781 isl_map_free(map2);
3782 return map1;
3784 if ((isl_map_plain_is_empty(map2) ||
3785 isl_map_plain_is_universe(map1)) &&
3786 isl_space_is_equal(map1->dim, map2->dim)) {
3787 isl_map_free(map1);
3788 return map2;
3791 if (map1->n == 1 && map2->n == 1 &&
3792 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3793 isl_space_is_equal(map1->dim, map2->dim) &&
3794 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3795 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3796 return map_intersect_add_constraint(map1, map2);
3798 equal = isl_map_plain_is_equal(map1, map2);
3799 if (equal < 0)
3800 goto error;
3801 if (equal) {
3802 isl_map_free(map2);
3803 return map1;
3806 dim2 = isl_map_dim(map2, isl_dim_all);
3807 nparam2 = isl_map_dim(map2, isl_dim_param);
3808 if (dim2 < 0 || nparam2 < 0)
3809 goto error;
3810 if (dim2 != nparam2)
3811 isl_assert(map1->ctx,
3812 isl_space_is_equal(map1->dim, map2->dim), goto error);
3814 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3815 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3816 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3818 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3819 map1->n * map2->n, flags);
3820 if (!result)
3821 goto error;
3822 for (i = 0; i < map1->n; ++i)
3823 for (j = 0; j < map2->n; ++j) {
3824 struct isl_basic_map *part;
3825 part = isl_basic_map_intersect(
3826 isl_basic_map_copy(map1->p[i]),
3827 isl_basic_map_copy(map2->p[j]));
3828 if (isl_basic_map_is_empty(part) < 0)
3829 part = isl_basic_map_free(part);
3830 result = isl_map_add_basic_map(result, part);
3831 if (!result)
3832 goto error;
3834 isl_map_free(map1);
3835 isl_map_free(map2);
3836 return result;
3837 error:
3838 isl_map_free(map1);
3839 isl_map_free(map2);
3840 return NULL;
3843 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3844 __isl_take isl_map *map2)
3846 if (isl_map_check_equal_space(map1, map2) < 0)
3847 goto error;
3848 return map_intersect_internal(map1, map2);
3849 error:
3850 isl_map_free(map1);
3851 isl_map_free(map2);
3852 return NULL;
3855 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3856 __isl_take isl_map *map2)
3858 isl_map_align_params_bin(&map1, &map2);
3859 return map_intersect(map1, map2);
3862 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3863 __isl_take isl_set *set2)
3865 return set_from_map(isl_map_intersect(set_to_map(set1),
3866 set_to_map(set2)));
3869 /* map_intersect_internal accepts intersections
3870 * with parameter domains, so we can just call that function.
3872 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3873 __isl_take isl_set *params)
3875 isl_map_align_params_set(&map, &params);
3876 return map_intersect_internal(map, params);
3879 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3880 __isl_take isl_set *params)
3882 return isl_map_intersect_params(set, params);
3885 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3887 isl_space *space;
3888 unsigned pos;
3889 isl_size n1, n2;
3891 if (!bmap)
3892 return NULL;
3893 bmap = isl_basic_map_cow(bmap);
3894 if (!bmap)
3895 return NULL;
3896 space = isl_space_reverse(isl_space_copy(bmap->dim));
3897 pos = isl_basic_map_offset(bmap, isl_dim_in);
3898 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3899 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3900 if (n1 < 0 || n2 < 0)
3901 bmap = isl_basic_map_free(bmap);
3902 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3903 return isl_basic_map_reset_space(bmap, space);
3906 /* Given a basic map A -> (B -> C), return the corresponding basic map
3907 * A -> (C -> B).
3909 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3910 __isl_take isl_basic_map *bmap)
3912 isl_space *space;
3913 isl_size offset, n1, n2;
3915 space = isl_basic_map_peek_space(bmap);
3916 if (isl_space_check_range_is_wrapping(space) < 0)
3917 return isl_basic_map_free(bmap);
3918 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3919 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3920 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3921 if (offset < 0 || n1 < 0 || n2 < 0)
3922 return isl_basic_map_free(bmap);
3924 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3926 space = isl_basic_map_take_space(bmap);
3927 space = isl_space_range_reverse(space);
3928 bmap = isl_basic_map_restore_space(bmap, space);
3930 return bmap;
3933 static __isl_give isl_basic_map *basic_map_space_reset(
3934 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3936 isl_space *space;
3938 if (!bmap)
3939 return NULL;
3940 if (!isl_space_is_named_or_nested(bmap->dim, type))
3941 return bmap;
3943 space = isl_basic_map_get_space(bmap);
3944 space = isl_space_reset(space, type);
3945 bmap = isl_basic_map_reset_space(bmap, space);
3946 return bmap;
3949 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3950 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3951 unsigned pos, unsigned n)
3953 isl_bool rational, is_empty;
3954 isl_space *res_space;
3955 struct isl_basic_map *res;
3956 struct isl_dim_map *dim_map;
3957 isl_size total;
3958 unsigned off;
3959 enum isl_dim_type t;
3961 if (n == 0)
3962 return basic_map_space_reset(bmap, type);
3964 is_empty = isl_basic_map_plain_is_empty(bmap);
3965 total = isl_basic_map_dim(bmap, isl_dim_all);
3966 if (is_empty < 0 || total < 0)
3967 return isl_basic_map_free(bmap);
3968 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3969 type, pos, n);
3970 if (!res_space)
3971 return isl_basic_map_free(bmap);
3972 if (is_empty) {
3973 isl_basic_map_free(bmap);
3974 return isl_basic_map_empty(res_space);
3977 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3978 off = 0;
3979 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3980 isl_size dim;
3982 if (t != type) {
3983 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3984 } else {
3985 isl_size size = isl_basic_map_dim(bmap, t);
3986 if (size < 0)
3987 dim_map = isl_dim_map_free(dim_map);
3988 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3989 0, pos, off);
3990 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3991 pos, size - pos, off + pos + n);
3993 dim = isl_space_dim(res_space, t);
3994 if (dim < 0)
3995 dim_map = isl_dim_map_free(dim_map);
3996 off += dim;
3998 isl_dim_map_div(dim_map, bmap, off);
4000 res = isl_basic_map_alloc_space(res_space,
4001 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4002 rational = isl_basic_map_is_rational(bmap);
4003 if (rational < 0)
4004 res = isl_basic_map_free(res);
4005 if (rational)
4006 res = isl_basic_map_set_rational(res);
4007 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4008 return isl_basic_map_finalize(res);
4011 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4012 __isl_take isl_basic_set *bset,
4013 enum isl_dim_type type, unsigned pos, unsigned n)
4015 return isl_basic_map_insert_dims(bset, type, pos, n);
4018 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4019 enum isl_dim_type type, unsigned n)
4021 isl_size dim;
4023 dim = isl_basic_map_dim(bmap, type);
4024 if (dim < 0)
4025 return isl_basic_map_free(bmap);
4026 return isl_basic_map_insert_dims(bmap, type, dim, n);
4029 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4030 enum isl_dim_type type, unsigned n)
4032 if (!bset)
4033 return NULL;
4034 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4035 return isl_basic_map_add_dims(bset, type, n);
4036 error:
4037 isl_basic_set_free(bset);
4038 return NULL;
4041 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4042 enum isl_dim_type type)
4044 isl_space *space;
4046 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4047 return map;
4049 space = isl_map_get_space(map);
4050 space = isl_space_reset(space, type);
4051 map = isl_map_reset_space(map, space);
4052 return map;
4055 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4056 enum isl_dim_type type, unsigned pos, unsigned n)
4058 int i;
4059 isl_space *space;
4061 if (n == 0)
4062 return map_space_reset(map, type);
4064 map = isl_map_cow(map);
4065 if (!map)
4066 return NULL;
4068 for (i = 0; i < map->n; ++i) {
4069 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4070 if (!map->p[i])
4071 goto error;
4074 space = isl_map_take_space(map);
4075 space = isl_space_insert_dims(space, type, pos, n);
4076 map = isl_map_restore_space(map, space);
4078 return map;
4079 error:
4080 isl_map_free(map);
4081 return NULL;
4084 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4085 enum isl_dim_type type, unsigned pos, unsigned n)
4087 return isl_map_insert_dims(set, type, pos, n);
4090 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4091 enum isl_dim_type type, unsigned n)
4093 isl_size dim;
4095 dim = isl_map_dim(map, type);
4096 if (dim < 0)
4097 return isl_map_free(map);
4098 return isl_map_insert_dims(map, type, dim, n);
4101 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4102 enum isl_dim_type type, unsigned n)
4104 if (!set)
4105 return NULL;
4106 isl_assert(set->ctx, type != isl_dim_in, goto error);
4107 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4108 error:
4109 isl_set_free(set);
4110 return NULL;
4113 __isl_give isl_basic_map *isl_basic_map_move_dims(
4114 __isl_take isl_basic_map *bmap,
4115 enum isl_dim_type dst_type, unsigned dst_pos,
4116 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4118 isl_space *space;
4119 struct isl_dim_map *dim_map;
4120 struct isl_basic_map *res;
4121 enum isl_dim_type t;
4122 isl_size total;
4123 unsigned off;
4125 if (!bmap)
4126 return NULL;
4127 if (n == 0) {
4128 bmap = isl_basic_map_reset(bmap, src_type);
4129 bmap = isl_basic_map_reset(bmap, dst_type);
4130 return bmap;
4133 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4134 return isl_basic_map_free(bmap);
4136 if (dst_type == src_type && dst_pos == src_pos)
4137 return bmap;
4139 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4141 if (pos(bmap->dim, dst_type) + dst_pos ==
4142 pos(bmap->dim, src_type) + src_pos +
4143 ((src_type < dst_type) ? n : 0)) {
4144 space = isl_basic_map_take_space(bmap);
4145 space = isl_space_move_dims(space, dst_type, dst_pos,
4146 src_type, src_pos, n);
4147 bmap = isl_basic_map_restore_space(bmap, space);
4148 bmap = isl_basic_map_finalize(bmap);
4150 return bmap;
4153 total = isl_basic_map_dim(bmap, isl_dim_all);
4154 if (total < 0)
4155 return isl_basic_map_free(bmap);
4156 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4158 off = 0;
4159 space = isl_basic_map_peek_space(bmap);
4160 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4161 isl_size size = isl_space_dim(space, t);
4162 if (size < 0)
4163 dim_map = isl_dim_map_free(dim_map);
4164 if (t == dst_type) {
4165 isl_dim_map_dim_range(dim_map, space, t,
4166 0, dst_pos, off);
4167 off += dst_pos;
4168 isl_dim_map_dim_range(dim_map, space, src_type,
4169 src_pos, n, off);
4170 off += n;
4171 isl_dim_map_dim_range(dim_map, space, t,
4172 dst_pos, size - dst_pos, off);
4173 off += size - dst_pos;
4174 } else if (t == src_type) {
4175 isl_dim_map_dim_range(dim_map, space, t,
4176 0, src_pos, off);
4177 off += src_pos;
4178 isl_dim_map_dim_range(dim_map, space, t,
4179 src_pos + n, size - src_pos - n, off);
4180 off += size - src_pos - n;
4181 } else {
4182 isl_dim_map_dim(dim_map, space, t, off);
4183 off += size;
4186 isl_dim_map_div(dim_map, bmap, off);
4188 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4189 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4190 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4191 space = isl_basic_map_take_space(bmap);
4192 space = isl_space_move_dims(space, dst_type, dst_pos,
4193 src_type, src_pos, n);
4194 bmap = isl_basic_map_restore_space(bmap, space);
4195 if (!bmap)
4196 goto error;
4198 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4199 bmap = isl_basic_map_gauss(bmap, NULL);
4200 bmap = isl_basic_map_finalize(bmap);
4202 return bmap;
4203 error:
4204 isl_basic_map_free(bmap);
4205 return NULL;
4208 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4209 enum isl_dim_type dst_type, unsigned dst_pos,
4210 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4212 isl_basic_map *bmap = bset_to_bmap(bset);
4213 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4214 src_type, src_pos, n);
4215 return bset_from_bmap(bmap);
4218 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4219 enum isl_dim_type dst_type, unsigned dst_pos,
4220 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4222 if (!set)
4223 return NULL;
4224 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4225 return set_from_map(isl_map_move_dims(set_to_map(set),
4226 dst_type, dst_pos, src_type, src_pos, n));
4227 error:
4228 isl_set_free(set);
4229 return NULL;
4232 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4233 enum isl_dim_type dst_type, unsigned dst_pos,
4234 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4236 int i;
4237 isl_space *space;
4239 if (n == 0) {
4240 map = isl_map_reset(map, src_type);
4241 map = isl_map_reset(map, dst_type);
4242 return map;
4245 if (isl_map_check_range(map, src_type, src_pos, n))
4246 return isl_map_free(map);
4248 if (dst_type == src_type && dst_pos == src_pos)
4249 return map;
4251 isl_assert(map->ctx, dst_type != src_type, goto error);
4253 map = isl_map_cow(map);
4254 if (!map)
4255 return NULL;
4257 for (i = 0; i < map->n; ++i) {
4258 map->p[i] = isl_basic_map_move_dims(map->p[i],
4259 dst_type, dst_pos,
4260 src_type, src_pos, n);
4261 if (!map->p[i])
4262 goto error;
4265 space = isl_map_take_space(map);
4266 space = isl_space_move_dims(space, dst_type, dst_pos,
4267 src_type, src_pos, n);
4268 map = isl_map_restore_space(map, space);
4270 return map;
4271 error:
4272 isl_map_free(map);
4273 return NULL;
4276 /* Move the specified dimensions to the last columns right before
4277 * the divs. Don't change the dimension specification of bmap.
4278 * That's the responsibility of the caller.
4280 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4281 enum isl_dim_type type, unsigned first, unsigned n)
4283 isl_space *space;
4284 struct isl_dim_map *dim_map;
4285 struct isl_basic_map *res;
4286 enum isl_dim_type t;
4287 isl_size total;
4288 unsigned off;
4290 if (!bmap)
4291 return NULL;
4292 if (isl_basic_map_offset(bmap, type) + first + n ==
4293 isl_basic_map_offset(bmap, isl_dim_div))
4294 return bmap;
4296 total = isl_basic_map_dim(bmap, isl_dim_all);
4297 if (total < 0)
4298 return isl_basic_map_free(bmap);
4299 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4301 off = 0;
4302 space = isl_basic_map_peek_space(bmap);
4303 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4304 isl_size size = isl_space_dim(space, t);
4305 if (size < 0)
4306 dim_map = isl_dim_map_free(dim_map);
4307 if (t == type) {
4308 isl_dim_map_dim_range(dim_map, space, t,
4309 0, first, off);
4310 off += first;
4311 isl_dim_map_dim_range(dim_map, space, t,
4312 first, n, total - bmap->n_div - n);
4313 isl_dim_map_dim_range(dim_map, space, t,
4314 first + n, size - (first + n), off);
4315 off += size - (first + n);
4316 } else {
4317 isl_dim_map_dim(dim_map, space, t, off);
4318 off += size;
4321 isl_dim_map_div(dim_map, bmap, off + n);
4323 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4324 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4325 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4326 return res;
4329 /* Insert "n" rows in the divs of "bmap".
4331 * The number of columns is not changed, which means that the last
4332 * dimensions of "bmap" are being reintepreted as the new divs.
4333 * The space of "bmap" is not adjusted, however, which means
4334 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4335 * from the space of "bmap" is the responsibility of the caller.
4337 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4338 int n)
4340 int i;
4341 size_t row_size;
4342 isl_int **new_div;
4343 isl_int *old;
4345 bmap = isl_basic_map_cow(bmap);
4346 if (!bmap)
4347 return NULL;
4349 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4350 old = bmap->block2.data;
4351 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4352 (bmap->extra + n) * (1 + row_size));
4353 if (!bmap->block2.data)
4354 return isl_basic_map_free(bmap);
4355 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4356 if (!new_div)
4357 return isl_basic_map_free(bmap);
4358 for (i = 0; i < n; ++i) {
4359 new_div[i] = bmap->block2.data +
4360 (bmap->extra + i) * (1 + row_size);
4361 isl_seq_clr(new_div[i], 1 + row_size);
4363 for (i = 0; i < bmap->extra; ++i)
4364 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4365 free(bmap->div);
4366 bmap->div = new_div;
4367 bmap->n_div += n;
4368 bmap->extra += n;
4370 return bmap;
4373 /* Drop constraints from "bmap" that only involve the variables
4374 * of "type" in the range [first, first + n] that are not related
4375 * to any of the variables outside that interval.
4376 * These constraints cannot influence the values for the variables
4377 * outside the interval, except in case they cause "bmap" to be empty.
4378 * Only drop the constraints if "bmap" is known to be non-empty.
4380 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4381 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4382 unsigned first, unsigned n)
4384 int i;
4385 int *groups;
4386 isl_size dim, n_div;
4387 isl_bool non_empty;
4389 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4390 if (non_empty < 0)
4391 return isl_basic_map_free(bmap);
4392 if (!non_empty)
4393 return bmap;
4395 dim = isl_basic_map_dim(bmap, isl_dim_all);
4396 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4397 if (dim < 0 || n_div < 0)
4398 return isl_basic_map_free(bmap);
4399 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4400 if (!groups)
4401 return isl_basic_map_free(bmap);
4402 first += isl_basic_map_offset(bmap, type) - 1;
4403 for (i = 0; i < first; ++i)
4404 groups[i] = -1;
4405 for (i = first + n; i < dim - n_div; ++i)
4406 groups[i] = -1;
4408 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4410 return bmap;
4413 /* Turn the n dimensions of type type, starting at first
4414 * into existentially quantified variables.
4416 * If a subset of the projected out variables are unrelated
4417 * to any of the variables that remain, then the constraints
4418 * involving this subset are simply dropped first.
4420 __isl_give isl_basic_map *isl_basic_map_project_out(
4421 __isl_take isl_basic_map *bmap,
4422 enum isl_dim_type type, unsigned first, unsigned n)
4424 isl_bool empty;
4425 isl_space *space;
4427 if (n == 0)
4428 return basic_map_space_reset(bmap, type);
4429 if (type == isl_dim_div)
4430 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4431 "cannot project out existentially quantified variables",
4432 return isl_basic_map_free(bmap));
4434 empty = isl_basic_map_plain_is_empty(bmap);
4435 if (empty < 0)
4436 return isl_basic_map_free(bmap);
4437 if (empty)
4438 bmap = isl_basic_map_set_to_empty(bmap);
4440 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4441 if (!bmap)
4442 return NULL;
4444 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4445 return isl_basic_map_remove_dims(bmap, type, first, n);
4447 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4448 return isl_basic_map_free(bmap);
4450 bmap = move_last(bmap, type, first, n);
4451 bmap = isl_basic_map_cow(bmap);
4452 bmap = insert_div_rows(bmap, n);
4454 space = isl_basic_map_take_space(bmap);
4455 space = isl_space_drop_dims(space, type, first, n);
4456 bmap = isl_basic_map_restore_space(bmap, space);
4457 bmap = isl_basic_map_simplify(bmap);
4458 bmap = isl_basic_map_drop_redundant_divs(bmap);
4459 return isl_basic_map_finalize(bmap);
4462 /* Turn the n dimensions of type type, starting at first
4463 * into existentially quantified variables.
4465 __isl_give isl_basic_set *isl_basic_set_project_out(
4466 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4467 unsigned first, unsigned n)
4469 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4470 type, first, n));
4473 /* Turn the n dimensions of type type, starting at first
4474 * into existentially quantified variables.
4476 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4477 enum isl_dim_type type, unsigned first, unsigned n)
4479 int i;
4480 isl_space *space;
4482 if (n == 0)
4483 return map_space_reset(map, type);
4485 if (isl_map_check_range(map, type, first, n) < 0)
4486 return isl_map_free(map);
4488 map = isl_map_cow(map);
4489 if (!map)
4490 return NULL;
4492 for (i = 0; i < map->n; ++i) {
4493 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4494 if (!map->p[i])
4495 goto error;
4498 if (map->n > 1)
4499 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4500 map = isl_map_unmark_normalized(map);
4502 space = isl_map_take_space(map);
4503 space = isl_space_drop_dims(space, type, first, n);
4504 map = isl_map_restore_space(map, space);
4506 return map;
4507 error:
4508 isl_map_free(map);
4509 return NULL;
4512 #undef TYPE
4513 #define TYPE isl_map
4514 #include "isl_project_out_all_params_templ.c"
4516 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4517 * into existentially quantified variables.
4519 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4520 enum isl_dim_type type, unsigned first, unsigned n)
4522 isl_size dim;
4524 dim = isl_map_dim(map, type);
4525 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4526 return isl_map_free(map);
4527 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4528 map = isl_map_project_out(map, type, 0, first);
4529 return map;
4532 /* Turn the n dimensions of type type, starting at first
4533 * into existentially quantified variables.
4535 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4536 enum isl_dim_type type, unsigned first, unsigned n)
4538 return set_from_map(isl_map_project_out(set_to_map(set),
4539 type, first, n));
4542 /* If "set" involves a parameter with identifier "id",
4543 * then turn it into an existentially quantified variable.
4545 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4546 __isl_take isl_id *id)
4548 int pos;
4550 if (!set || !id)
4551 goto error;
4552 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4553 isl_id_free(id);
4554 if (pos < 0)
4555 return set;
4556 return isl_set_project_out(set, isl_dim_param, pos, 1);
4557 error:
4558 isl_set_free(set);
4559 isl_id_free(id);
4560 return NULL;
4563 /* If "set" involves any of the parameters with identifiers in "list",
4564 * then turn them into existentially quantified variables.
4566 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4567 __isl_take isl_id_list *list)
4569 int i;
4570 isl_size n;
4572 n = isl_id_list_size(list);
4573 if (n < 0)
4574 goto error;
4575 for (i = 0; i < n; ++i) {
4576 isl_id *id;
4578 id = isl_id_list_get_at(list, i);
4579 set = isl_set_project_out_param_id(set, id);
4582 isl_id_list_free(list);
4583 return set;
4584 error:
4585 isl_id_list_free(list);
4586 isl_set_free(set);
4587 return NULL;
4590 /* Project out all parameters from "set" by existentially quantifying
4591 * over them.
4593 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4595 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4598 /* Return a map that projects the elements in "set" onto their
4599 * "n" set dimensions starting at "first".
4600 * "type" should be equal to isl_dim_set.
4602 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4603 enum isl_dim_type type, unsigned first, unsigned n)
4605 int i;
4606 isl_map *map;
4608 if (type != isl_dim_set)
4609 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4610 "only set dimensions can be projected out", goto error);
4611 if (isl_set_check_range(set, type, first, n) < 0)
4612 return isl_set_free(set);
4614 map = isl_map_from_domain(set);
4615 map = isl_map_add_dims(map, isl_dim_out, n);
4616 for (i = 0; i < n; ++i)
4617 map = isl_map_equate(map, isl_dim_in, first + i,
4618 isl_dim_out, i);
4619 return map;
4620 error:
4621 isl_set_free(set);
4622 return NULL;
4625 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4626 unsigned n)
4628 int i, j;
4629 isl_size total;
4631 total = isl_basic_map_dim(bmap, isl_dim_all);
4632 if (total < 0)
4633 return isl_basic_map_free(bmap);
4634 for (i = 0; i < n; ++i) {
4635 j = isl_basic_map_alloc_div(bmap);
4636 if (j < 0)
4637 goto error;
4638 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4640 return bmap;
4641 error:
4642 isl_basic_map_free(bmap);
4643 return NULL;
4646 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4648 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4649 __isl_keep isl_basic_map *bmap2)
4651 isl_space *space1, *space2;
4653 space1 = isl_basic_map_peek_space(bmap1);
4654 space2 = isl_basic_map_peek_space(bmap2);
4655 return isl_space_tuple_is_equal(space1, isl_dim_out,
4656 space2, isl_dim_in);
4659 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4661 static isl_stat isl_basic_map_check_applies_range(
4662 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4664 isl_bool equal;
4666 equal = isl_basic_map_applies_range(bmap1, bmap2);
4667 if (equal < 0)
4668 return isl_stat_error;
4669 if (!equal)
4670 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4671 "spaces don't match", return isl_stat_error);
4672 return isl_stat_ok;
4675 __isl_give isl_basic_map *isl_basic_map_apply_range(
4676 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4678 isl_space *space_result = NULL;
4679 struct isl_basic_map *bmap;
4680 isl_size n_in, n_out, n, nparam;
4681 unsigned total, pos;
4682 struct isl_dim_map *dim_map1, *dim_map2;
4684 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4685 goto error;
4686 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4687 goto error;
4689 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4690 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4691 n = isl_basic_map_dim(bmap1, isl_dim_out);
4692 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4693 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4694 goto error;
4696 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4697 isl_basic_map_get_space(bmap2));
4699 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4700 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4701 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4702 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4703 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4704 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4705 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4706 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4707 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4708 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4709 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4711 bmap = isl_basic_map_alloc_space(space_result,
4712 bmap1->n_div + bmap2->n_div + n,
4713 bmap1->n_eq + bmap2->n_eq,
4714 bmap1->n_ineq + bmap2->n_ineq);
4715 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4716 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4717 bmap = add_divs(bmap, n);
4718 bmap = isl_basic_map_simplify(bmap);
4719 bmap = isl_basic_map_drop_redundant_divs(bmap);
4720 return isl_basic_map_finalize(bmap);
4721 error:
4722 isl_basic_map_free(bmap1);
4723 isl_basic_map_free(bmap2);
4724 return NULL;
4727 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4728 __isl_take isl_basic_map *bmap)
4730 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4731 goto error;
4733 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4734 bmap));
4735 error:
4736 isl_basic_set_free(bset);
4737 isl_basic_map_free(bmap);
4738 return NULL;
4741 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4742 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4744 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4745 goto error;
4746 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4747 bmap2->dim, isl_dim_in))
4748 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4749 "spaces don't match", goto error);
4751 bmap1 = isl_basic_map_reverse(bmap1);
4752 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4753 return isl_basic_map_reverse(bmap1);
4754 error:
4755 isl_basic_map_free(bmap1);
4756 isl_basic_map_free(bmap2);
4757 return NULL;
4760 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4761 * A \cap B -> f(A) + f(B)
4763 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4764 __isl_take isl_basic_map *bmap2)
4766 isl_size n_in, n_out, nparam;
4767 unsigned total, pos;
4768 struct isl_basic_map *bmap = NULL;
4769 struct isl_dim_map *dim_map1, *dim_map2;
4770 int i;
4772 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4773 goto error;
4775 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4776 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4777 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4778 if (nparam < 0 || n_in < 0 || n_out < 0)
4779 goto error;
4781 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4782 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4783 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4784 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4785 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4786 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4787 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4788 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4789 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4790 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4791 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4793 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4794 bmap1->n_div + bmap2->n_div + 2 * n_out,
4795 bmap1->n_eq + bmap2->n_eq + n_out,
4796 bmap1->n_ineq + bmap2->n_ineq);
4797 for (i = 0; i < n_out; ++i) {
4798 int j = isl_basic_map_alloc_equality(bmap);
4799 if (j < 0)
4800 goto error;
4801 isl_seq_clr(bmap->eq[j], 1+total);
4802 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4803 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4804 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4806 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4807 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4808 bmap = add_divs(bmap, 2 * n_out);
4810 bmap = isl_basic_map_simplify(bmap);
4811 return isl_basic_map_finalize(bmap);
4812 error:
4813 isl_basic_map_free(bmap);
4814 isl_basic_map_free(bmap1);
4815 isl_basic_map_free(bmap2);
4816 return NULL;
4819 /* Given two maps A -> f(A) and B -> g(B), construct a map
4820 * A \cap B -> f(A) + f(B)
4822 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4823 __isl_take isl_map *map2)
4825 struct isl_map *result;
4826 int i, j;
4828 if (isl_map_check_equal_space(map1, map2) < 0)
4829 goto error;
4831 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4832 map1->n * map2->n, 0);
4833 if (!result)
4834 goto error;
4835 for (i = 0; i < map1->n; ++i)
4836 for (j = 0; j < map2->n; ++j) {
4837 struct isl_basic_map *part;
4838 part = isl_basic_map_sum(
4839 isl_basic_map_copy(map1->p[i]),
4840 isl_basic_map_copy(map2->p[j]));
4841 if (isl_basic_map_is_empty(part))
4842 isl_basic_map_free(part);
4843 else
4844 result = isl_map_add_basic_map(result, part);
4845 if (!result)
4846 goto error;
4848 isl_map_free(map1);
4849 isl_map_free(map2);
4850 return result;
4851 error:
4852 isl_map_free(map1);
4853 isl_map_free(map2);
4854 return NULL;
4857 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4858 __isl_take isl_set *set2)
4860 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4863 /* Given a basic map A -> f(A), construct A -> -f(A).
4865 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4867 int i, j;
4868 unsigned off;
4869 isl_size n;
4871 bmap = isl_basic_map_cow(bmap);
4872 n = isl_basic_map_dim(bmap, isl_dim_out);
4873 if (n < 0)
4874 return isl_basic_map_free(bmap);
4876 off = isl_basic_map_offset(bmap, isl_dim_out);
4877 for (i = 0; i < bmap->n_eq; ++i)
4878 for (j = 0; j < n; ++j)
4879 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4880 for (i = 0; i < bmap->n_ineq; ++i)
4881 for (j = 0; j < n; ++j)
4882 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4883 for (i = 0; i < bmap->n_div; ++i)
4884 for (j = 0; j < n; ++j)
4885 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4886 bmap = isl_basic_map_gauss(bmap, NULL);
4887 return isl_basic_map_finalize(bmap);
4890 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4892 return isl_basic_map_neg(bset);
4895 /* Given a map A -> f(A), construct A -> -f(A).
4897 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4899 int i;
4901 map = isl_map_cow(map);
4902 if (!map)
4903 return NULL;
4905 for (i = 0; i < map->n; ++i) {
4906 map->p[i] = isl_basic_map_neg(map->p[i]);
4907 if (!map->p[i])
4908 goto error;
4911 return map;
4912 error:
4913 isl_map_free(map);
4914 return NULL;
4917 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4919 return set_from_map(isl_map_neg(set_to_map(set)));
4922 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4923 * A -> floor(f(A)/d).
4925 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4926 isl_int d)
4928 isl_size n_in, n_out, nparam;
4929 unsigned total, pos;
4930 struct isl_basic_map *result = NULL;
4931 struct isl_dim_map *dim_map;
4932 int i;
4934 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4935 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4936 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4937 if (nparam < 0 || n_in < 0 || n_out < 0)
4938 return isl_basic_map_free(bmap);
4940 total = nparam + n_in + n_out + bmap->n_div + n_out;
4941 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4942 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4943 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4944 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4945 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4947 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4948 bmap->n_div + n_out,
4949 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4950 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4951 result = add_divs(result, n_out);
4952 for (i = 0; i < n_out; ++i) {
4953 int j;
4954 j = isl_basic_map_alloc_inequality(result);
4955 if (j < 0)
4956 goto error;
4957 isl_seq_clr(result->ineq[j], 1+total);
4958 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4959 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4960 j = isl_basic_map_alloc_inequality(result);
4961 if (j < 0)
4962 goto error;
4963 isl_seq_clr(result->ineq[j], 1+total);
4964 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4965 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4966 isl_int_sub_ui(result->ineq[j][0], d, 1);
4969 result = isl_basic_map_simplify(result);
4970 return isl_basic_map_finalize(result);
4971 error:
4972 isl_basic_map_free(result);
4973 return NULL;
4976 /* Given a map A -> f(A) and an integer d, construct a map
4977 * A -> floor(f(A)/d).
4979 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4981 int i;
4983 map = isl_map_cow(map);
4984 if (!map)
4985 return NULL;
4987 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4988 for (i = 0; i < map->n; ++i) {
4989 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4990 if (!map->p[i])
4991 goto error;
4993 map = isl_map_unmark_normalized(map);
4995 return map;
4996 error:
4997 isl_map_free(map);
4998 return NULL;
5001 /* Given a map A -> f(A) and an integer d, construct a map
5002 * A -> floor(f(A)/d).
5004 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5005 __isl_take isl_val *d)
5007 if (!map || !d)
5008 goto error;
5009 if (!isl_val_is_int(d))
5010 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5011 "expecting integer denominator", goto error);
5012 map = isl_map_floordiv(map, d->n);
5013 isl_val_free(d);
5014 return map;
5015 error:
5016 isl_map_free(map);
5017 isl_val_free(d);
5018 return NULL;
5021 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5022 unsigned pos)
5024 int i;
5025 isl_size nparam;
5026 isl_size n_in;
5027 isl_size total;
5029 total = isl_basic_map_dim(bmap, isl_dim_all);
5030 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5031 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5032 if (total < 0 || nparam < 0 || n_in < 0)
5033 return isl_basic_map_free(bmap);
5034 i = isl_basic_map_alloc_equality(bmap);
5035 if (i < 0)
5036 goto error;
5037 isl_seq_clr(bmap->eq[i], 1 + total);
5038 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5039 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5040 return isl_basic_map_finalize(bmap);
5041 error:
5042 isl_basic_map_free(bmap);
5043 return NULL;
5046 /* Add a constraint to "bmap" expressing i_pos < o_pos
5048 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5049 unsigned pos)
5051 int i;
5052 isl_size nparam;
5053 isl_size n_in;
5054 isl_size total;
5056 total = isl_basic_map_dim(bmap, isl_dim_all);
5057 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5058 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5059 if (total < 0 || nparam < 0 || n_in < 0)
5060 return isl_basic_map_free(bmap);
5061 i = isl_basic_map_alloc_inequality(bmap);
5062 if (i < 0)
5063 goto error;
5064 isl_seq_clr(bmap->ineq[i], 1 + total);
5065 isl_int_set_si(bmap->ineq[i][0], -1);
5066 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5067 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5068 return isl_basic_map_finalize(bmap);
5069 error:
5070 isl_basic_map_free(bmap);
5071 return NULL;
5074 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5076 static __isl_give isl_basic_map *var_less_or_equal(
5077 __isl_take isl_basic_map *bmap, unsigned pos)
5079 int i;
5080 isl_size nparam;
5081 isl_size n_in;
5082 isl_size total;
5084 total = isl_basic_map_dim(bmap, isl_dim_all);
5085 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5086 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5087 if (total < 0 || nparam < 0 || n_in < 0)
5088 return isl_basic_map_free(bmap);
5089 i = isl_basic_map_alloc_inequality(bmap);
5090 if (i < 0)
5091 goto error;
5092 isl_seq_clr(bmap->ineq[i], 1 + total);
5093 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5094 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5095 return isl_basic_map_finalize(bmap);
5096 error:
5097 isl_basic_map_free(bmap);
5098 return NULL;
5101 /* Add a constraint to "bmap" expressing i_pos > o_pos
5103 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5104 unsigned pos)
5106 int i;
5107 isl_size nparam;
5108 isl_size n_in;
5109 isl_size total;
5111 total = isl_basic_map_dim(bmap, isl_dim_all);
5112 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5113 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5114 if (total < 0 || nparam < 0 || n_in < 0)
5115 return isl_basic_map_free(bmap);
5116 i = isl_basic_map_alloc_inequality(bmap);
5117 if (i < 0)
5118 goto error;
5119 isl_seq_clr(bmap->ineq[i], 1 + total);
5120 isl_int_set_si(bmap->ineq[i][0], -1);
5121 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5122 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5123 return isl_basic_map_finalize(bmap);
5124 error:
5125 isl_basic_map_free(bmap);
5126 return NULL;
5129 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5131 static __isl_give isl_basic_map *var_more_or_equal(
5132 __isl_take isl_basic_map *bmap, unsigned pos)
5134 int i;
5135 isl_size nparam;
5136 isl_size n_in;
5137 isl_size total;
5139 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5141 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5142 if (total < 0 || nparam < 0 || n_in < 0)
5143 return isl_basic_map_free(bmap);
5144 i = isl_basic_map_alloc_inequality(bmap);
5145 if (i < 0)
5146 goto error;
5147 isl_seq_clr(bmap->ineq[i], 1 + total);
5148 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5149 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5150 return isl_basic_map_finalize(bmap);
5151 error:
5152 isl_basic_map_free(bmap);
5153 return NULL;
5156 __isl_give isl_basic_map *isl_basic_map_equal(
5157 __isl_take isl_space *space, unsigned n_equal)
5159 int i;
5160 struct isl_basic_map *bmap;
5161 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5162 if (!bmap)
5163 return NULL;
5164 for (i = 0; i < n_equal && bmap; ++i)
5165 bmap = var_equal(bmap, i);
5166 return isl_basic_map_finalize(bmap);
5169 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5171 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5172 unsigned pos)
5174 int i;
5175 struct isl_basic_map *bmap;
5176 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5177 if (!bmap)
5178 return NULL;
5179 for (i = 0; i < pos && bmap; ++i)
5180 bmap = var_equal(bmap, i);
5181 if (bmap)
5182 bmap = var_less(bmap, pos);
5183 return isl_basic_map_finalize(bmap);
5186 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5188 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5189 __isl_take isl_space *space, unsigned pos)
5191 int i;
5192 isl_basic_map *bmap;
5194 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5195 for (i = 0; i < pos; ++i)
5196 bmap = var_equal(bmap, i);
5197 bmap = var_less_or_equal(bmap, pos);
5198 return isl_basic_map_finalize(bmap);
5201 /* Return a relation on "space" expressing i_pos > o_pos
5203 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5204 unsigned pos)
5206 int i;
5207 struct isl_basic_map *bmap;
5208 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5209 if (!bmap)
5210 return NULL;
5211 for (i = 0; i < pos && bmap; ++i)
5212 bmap = var_equal(bmap, i);
5213 if (bmap)
5214 bmap = var_more(bmap, pos);
5215 return isl_basic_map_finalize(bmap);
5218 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5220 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5221 __isl_take isl_space *space, unsigned pos)
5223 int i;
5224 isl_basic_map *bmap;
5226 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5227 for (i = 0; i < pos; ++i)
5228 bmap = var_equal(bmap, i);
5229 bmap = var_more_or_equal(bmap, pos);
5230 return isl_basic_map_finalize(bmap);
5233 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5234 unsigned n, int equal)
5236 struct isl_map *map;
5237 int i;
5239 if (n == 0 && equal)
5240 return isl_map_universe(space);
5242 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5244 for (i = 0; i + 1 < n; ++i)
5245 map = isl_map_add_basic_map(map,
5246 isl_basic_map_less_at(isl_space_copy(space), i));
5247 if (n > 0) {
5248 if (equal)
5249 map = isl_map_add_basic_map(map,
5250 isl_basic_map_less_or_equal_at(space, n - 1));
5251 else
5252 map = isl_map_add_basic_map(map,
5253 isl_basic_map_less_at(space, n - 1));
5254 } else
5255 isl_space_free(space);
5257 return map;
5260 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5262 if (!space)
5263 return NULL;
5264 return map_lex_lte_first(space, space->n_out, equal);
5267 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5268 unsigned n)
5270 return map_lex_lte_first(space, n, 0);
5273 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5274 unsigned n)
5276 return map_lex_lte_first(space, n, 1);
5279 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5281 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5284 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5286 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5289 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5290 unsigned n, int equal)
5292 struct isl_map *map;
5293 int i;
5295 if (n == 0 && equal)
5296 return isl_map_universe(space);
5298 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5300 for (i = 0; i + 1 < n; ++i)
5301 map = isl_map_add_basic_map(map,
5302 isl_basic_map_more_at(isl_space_copy(space), i));
5303 if (n > 0) {
5304 if (equal)
5305 map = isl_map_add_basic_map(map,
5306 isl_basic_map_more_or_equal_at(space, n - 1));
5307 else
5308 map = isl_map_add_basic_map(map,
5309 isl_basic_map_more_at(space, n - 1));
5310 } else
5311 isl_space_free(space);
5313 return map;
5316 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5318 if (!space)
5319 return NULL;
5320 return map_lex_gte_first(space, space->n_out, equal);
5323 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5324 unsigned n)
5326 return map_lex_gte_first(space, n, 0);
5329 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5330 unsigned n)
5332 return map_lex_gte_first(space, n, 1);
5335 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5337 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5340 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5342 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5345 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5346 __isl_take isl_set *set2)
5348 isl_map *map;
5349 map = isl_map_lex_le(isl_set_get_space(set1));
5350 map = isl_map_intersect_domain(map, set1);
5351 map = isl_map_intersect_range(map, set2);
5352 return map;
5355 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5356 __isl_take isl_set *set2)
5358 isl_map *map;
5359 map = isl_map_lex_lt(isl_set_get_space(set1));
5360 map = isl_map_intersect_domain(map, set1);
5361 map = isl_map_intersect_range(map, set2);
5362 return map;
5365 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5366 __isl_take isl_set *set2)
5368 isl_map *map;
5369 map = isl_map_lex_ge(isl_set_get_space(set1));
5370 map = isl_map_intersect_domain(map, set1);
5371 map = isl_map_intersect_range(map, set2);
5372 return map;
5375 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5376 __isl_take isl_set *set2)
5378 isl_map *map;
5379 map = isl_map_lex_gt(isl_set_get_space(set1));
5380 map = isl_map_intersect_domain(map, set1);
5381 map = isl_map_intersect_range(map, set2);
5382 return map;
5385 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5386 __isl_take isl_map *map2)
5388 isl_map *map;
5389 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5390 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5391 map = isl_map_apply_range(map, isl_map_reverse(map2));
5392 return map;
5395 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5396 __isl_take isl_map *map2)
5398 isl_map *map;
5399 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5400 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5401 map = isl_map_apply_range(map, isl_map_reverse(map2));
5402 return map;
5405 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5406 __isl_take isl_map *map2)
5408 isl_map *map;
5409 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5410 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5411 map = isl_map_apply_range(map, isl_map_reverse(map2));
5412 return map;
5415 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5416 __isl_take isl_map *map2)
5418 isl_map *map;
5419 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5420 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5421 map = isl_map_apply_range(map, isl_map_reverse(map2));
5422 return map;
5425 /* For the div d = floor(f/m) at position "div", add the constraint
5427 * f - m d >= 0
5429 static __isl_give isl_basic_map *add_upper_div_constraint(
5430 __isl_take isl_basic_map *bmap, unsigned div)
5432 int i;
5433 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5434 isl_size n_div;
5435 unsigned pos;
5437 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5438 if (v_div < 0 || n_div < 0)
5439 return isl_basic_map_free(bmap);
5440 pos = v_div + div;
5441 i = isl_basic_map_alloc_inequality(bmap);
5442 if (i < 0)
5443 return isl_basic_map_free(bmap);
5444 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5445 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5447 return bmap;
5450 /* For the div d = floor(f/m) at position "div", add the constraint
5452 * -(f-(m-1)) + m d >= 0
5454 static __isl_give isl_basic_map *add_lower_div_constraint(
5455 __isl_take isl_basic_map *bmap, unsigned div)
5457 int i;
5458 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5459 isl_size n_div;
5460 unsigned pos;
5462 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5463 if (v_div < 0 || n_div < 0)
5464 return isl_basic_map_free(bmap);
5465 pos = v_div + div;
5466 i = isl_basic_map_alloc_inequality(bmap);
5467 if (i < 0)
5468 return isl_basic_map_free(bmap);
5469 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5470 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5471 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5472 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5474 return bmap;
5477 /* For the div d = floor(f/m) at position "pos", add the constraints
5479 * f - m d >= 0
5480 * -(f-(m-1)) + m d >= 0
5482 * Note that the second constraint is the negation of
5484 * f - m d >= m
5486 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5487 __isl_take isl_basic_map *bmap, unsigned pos)
5489 bmap = add_upper_div_constraint(bmap, pos);
5490 bmap = add_lower_div_constraint(bmap, pos);
5491 return bmap;
5494 /* For each known div d = floor(f/m), add the constraints
5496 * f - m d >= 0
5497 * -(f-(m-1)) + m d >= 0
5499 * Remove duplicate constraints in case of some these div constraints
5500 * already appear in "bmap".
5502 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5503 __isl_take isl_basic_map *bmap)
5505 isl_size n_div;
5507 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5508 if (n_div < 0)
5509 return isl_basic_map_free(bmap);
5510 if (n_div == 0)
5511 return bmap;
5513 bmap = add_known_div_constraints(bmap);
5514 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5515 bmap = isl_basic_map_finalize(bmap);
5516 return bmap;
5519 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5521 * In particular, if this div is of the form d = floor(f/m),
5522 * then add the constraint
5524 * f - m d >= 0
5526 * if sign < 0 or the constraint
5528 * -(f-(m-1)) + m d >= 0
5530 * if sign > 0.
5532 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5533 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5535 if (sign < 0)
5536 return add_upper_div_constraint(bmap, div);
5537 else
5538 return add_lower_div_constraint(bmap, div);
5541 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5542 __isl_take isl_basic_map *bmap)
5544 isl_space *space;
5546 if (!bmap)
5547 goto error;
5548 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5549 bmap->n_div == 0 &&
5550 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5551 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5552 return bset_from_bmap(bmap);
5553 bmap = isl_basic_map_cow(bmap);
5554 if (!bmap)
5555 return NULL;
5556 space = isl_basic_map_take_space(bmap);
5557 space = isl_space_underlying(space, bmap->n_div);
5558 bmap = isl_basic_map_restore_space(bmap, space);
5559 if (!bmap)
5560 return NULL;
5561 bmap->extra -= bmap->n_div;
5562 bmap->n_div = 0;
5563 bmap = isl_basic_map_finalize(bmap);
5564 return bset_from_bmap(bmap);
5565 error:
5566 isl_basic_map_free(bmap);
5567 return NULL;
5570 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5571 __isl_take isl_basic_set *bset)
5573 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5576 /* Replace each element in "list" by the result of applying
5577 * isl_basic_map_underlying_set to the element.
5579 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5580 __isl_take isl_basic_map_list *list)
5582 int i;
5583 isl_size n;
5585 n = isl_basic_map_list_n_basic_map(list);
5586 if (n < 0)
5587 goto error;
5589 for (i = 0; i < n; ++i) {
5590 isl_basic_map *bmap;
5591 isl_basic_set *bset;
5593 bmap = isl_basic_map_list_get_basic_map(list, i);
5594 bset = isl_basic_set_underlying_set(bmap);
5595 list = isl_basic_set_list_set_basic_set(list, i, bset);
5598 return list;
5599 error:
5600 isl_basic_map_list_free(list);
5601 return NULL;
5604 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5605 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5607 struct isl_basic_map *bmap;
5608 struct isl_ctx *ctx;
5609 isl_size dim, bmap_total;
5610 unsigned total;
5611 int i;
5613 if (!bset || !like)
5614 goto error;
5615 ctx = bset->ctx;
5616 if (isl_basic_set_check_no_params(bset) < 0 ||
5617 isl_basic_set_check_no_locals(bset) < 0)
5618 goto error;
5619 dim = isl_basic_set_dim(bset, isl_dim_set);
5620 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5621 if (dim < 0 || bmap_total < 0)
5622 goto error;
5623 isl_assert(ctx, dim == bmap_total, goto error);
5624 if (like->n_div == 0) {
5625 isl_space *space = isl_basic_map_get_space(like);
5626 isl_basic_map_free(like);
5627 return isl_basic_map_reset_space(bset, space);
5629 bset = isl_basic_set_cow(bset);
5630 if (!bset)
5631 goto error;
5632 total = dim + bset->extra;
5633 bmap = bset_to_bmap(bset);
5634 isl_space_free(isl_basic_map_take_space(bmap));
5635 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5636 if (!bmap)
5637 goto error;
5638 bmap->n_div = like->n_div;
5639 bmap->extra += like->n_div;
5640 if (bmap->extra) {
5641 unsigned ltotal;
5642 isl_int **div;
5643 ltotal = total - bmap->extra + like->extra;
5644 if (ltotal > total)
5645 ltotal = total;
5646 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5647 bmap->extra * (1 + 1 + total));
5648 if (isl_blk_is_error(bmap->block2))
5649 goto error;
5650 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5651 if (!div)
5652 goto error;
5653 bmap->div = div;
5654 for (i = 0; i < bmap->extra; ++i)
5655 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5656 for (i = 0; i < like->n_div; ++i) {
5657 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5658 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5660 bmap = isl_basic_map_add_known_div_constraints(bmap);
5662 isl_basic_map_free(like);
5663 bmap = isl_basic_map_simplify(bmap);
5664 bmap = isl_basic_map_finalize(bmap);
5665 return bmap;
5666 error:
5667 isl_basic_map_free(like);
5668 isl_basic_set_free(bset);
5669 return NULL;
5672 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5673 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5675 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5676 bset_to_bmap(like)));
5679 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5681 int i;
5683 map = isl_map_cow(map);
5684 if (!map)
5685 return NULL;
5686 map->dim = isl_space_cow(map->dim);
5687 if (!map->dim)
5688 goto error;
5690 for (i = 1; i < map->n; ++i)
5691 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5692 goto error);
5693 for (i = 0; i < map->n; ++i) {
5694 map->p[i] = bset_to_bmap(
5695 isl_basic_map_underlying_set(map->p[i]));
5696 if (!map->p[i])
5697 goto error;
5699 if (map->n == 0)
5700 map->dim = isl_space_underlying(map->dim, 0);
5701 else {
5702 isl_space_free(map->dim);
5703 map->dim = isl_space_copy(map->p[0]->dim);
5705 if (!map->dim)
5706 goto error;
5707 return set_from_map(map);
5708 error:
5709 isl_map_free(map);
5710 return NULL;
5713 /* Replace the space of "bmap" by "space".
5715 * If the space of "bmap" is identical to "space" (including the identifiers
5716 * of the input and output dimensions), then simply return the original input.
5718 __isl_give isl_basic_map *isl_basic_map_reset_space(
5719 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5721 isl_bool equal;
5722 isl_space *bmap_space;
5724 bmap_space = isl_basic_map_peek_space(bmap);
5725 equal = isl_space_is_equal(bmap_space, space);
5726 if (equal >= 0 && equal)
5727 equal = isl_space_has_equal_ids(bmap_space, space);
5728 if (equal < 0)
5729 goto error;
5730 if (equal) {
5731 isl_space_free(space);
5732 return bmap;
5734 isl_space_free(isl_basic_map_take_space(bmap));
5735 bmap = isl_basic_map_restore_space(bmap, space);
5737 bmap = isl_basic_map_finalize(bmap);
5739 return bmap;
5740 error:
5741 isl_basic_map_free(bmap);
5742 isl_space_free(space);
5743 return NULL;
5746 __isl_give isl_basic_set *isl_basic_set_reset_space(
5747 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5749 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5750 space));
5753 /* Check that the total dimensions of "map" and "space" are the same.
5755 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5756 __isl_keep isl_space *space)
5758 isl_size dim1, dim2;
5760 dim1 = isl_map_dim(map, isl_dim_all);
5761 dim2 = isl_space_dim(space, isl_dim_all);
5762 if (dim1 < 0 || dim2 < 0)
5763 return isl_stat_error;
5764 if (dim1 == dim2)
5765 return isl_stat_ok;
5766 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5767 "total dimensions do not match", return isl_stat_error);
5770 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5771 __isl_take isl_space *space)
5773 int i;
5775 map = isl_map_cow(map);
5776 if (!map || !space)
5777 goto error;
5779 for (i = 0; i < map->n; ++i) {
5780 map->p[i] = isl_basic_map_reset_space(map->p[i],
5781 isl_space_copy(space));
5782 if (!map->p[i])
5783 goto error;
5785 isl_space_free(isl_map_take_space(map));
5786 map = isl_map_restore_space(map, space);
5788 return map;
5789 error:
5790 isl_map_free(map);
5791 isl_space_free(space);
5792 return NULL;
5795 /* Replace the space of "map" by "space", without modifying
5796 * the dimension of "map".
5798 * If the space of "map" is identical to "space" (including the identifiers
5799 * of the input and output dimensions), then simply return the original input.
5801 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5802 __isl_take isl_space *space)
5804 isl_bool equal;
5805 isl_space *map_space;
5807 map_space = isl_map_peek_space(map);
5808 equal = isl_space_is_equal(map_space, space);
5809 if (equal >= 0 && equal)
5810 equal = isl_space_has_equal_ids(map_space, space);
5811 if (equal < 0)
5812 goto error;
5813 if (equal) {
5814 isl_space_free(space);
5815 return map;
5817 if (check_map_space_equal_total_dim(map, space) < 0)
5818 goto error;
5819 return isl_map_reset_space(map, space);
5820 error:
5821 isl_map_free(map);
5822 isl_space_free(space);
5823 return NULL;
5826 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5827 __isl_take isl_space *space)
5829 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5832 /* Compute the parameter domain of the given basic set.
5834 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5836 isl_bool is_params;
5837 isl_space *space;
5838 isl_size n;
5840 is_params = isl_basic_set_is_params(bset);
5841 if (is_params < 0)
5842 return isl_basic_set_free(bset);
5843 if (is_params)
5844 return bset;
5846 n = isl_basic_set_dim(bset, isl_dim_set);
5847 if (n < 0)
5848 return isl_basic_set_free(bset);
5849 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5850 space = isl_basic_set_get_space(bset);
5851 space = isl_space_params(space);
5852 bset = isl_basic_set_reset_space(bset, space);
5853 return bset;
5856 /* Construct a zero-dimensional basic set with the given parameter domain.
5858 __isl_give isl_basic_set *isl_basic_set_from_params(
5859 __isl_take isl_basic_set *bset)
5861 isl_space *space;
5862 space = isl_basic_set_get_space(bset);
5863 space = isl_space_set_from_params(space);
5864 bset = isl_basic_set_reset_space(bset, space);
5865 return bset;
5868 /* Compute the parameter domain of the given set.
5870 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5872 return isl_map_params(set_to_map(set));
5875 /* Construct a zero-dimensional set with the given parameter domain.
5877 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5879 isl_space *space;
5880 space = isl_set_get_space(set);
5881 space = isl_space_set_from_params(space);
5882 set = isl_set_reset_space(set, space);
5883 return set;
5886 /* Compute the parameter domain of the given map.
5888 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5890 isl_space *space;
5891 isl_size n_in, n_out;
5893 n_in = isl_map_dim(map, isl_dim_in);
5894 n_out = isl_map_dim(map, isl_dim_out);
5895 if (n_in < 0 || n_out < 0)
5896 return isl_map_free(map);
5897 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5898 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5899 space = isl_map_get_space(map);
5900 space = isl_space_params(space);
5901 map = isl_map_reset_space(map, space);
5902 return map;
5905 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5907 isl_space *space;
5908 isl_size n_out;
5910 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5911 if (n_out < 0)
5912 return isl_basic_map_free(bmap);
5913 space = isl_space_domain(isl_basic_map_get_space(bmap));
5915 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5917 return isl_basic_map_reset_space(bmap, space);
5920 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5922 if (!bmap)
5923 return isl_bool_error;
5924 return isl_space_may_be_set(bmap->dim);
5927 /* Is this basic map actually a set?
5928 * Users should never call this function. Outside of isl,
5929 * the type should indicate whether something is a set or a map.
5931 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5933 if (!bmap)
5934 return isl_bool_error;
5935 return isl_space_is_set(bmap->dim);
5938 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5940 isl_bool is_set;
5942 is_set = isl_basic_map_is_set(bmap);
5943 if (is_set < 0)
5944 goto error;
5945 if (is_set)
5946 return bmap;
5947 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5948 error:
5949 isl_basic_map_free(bmap);
5950 return NULL;
5953 __isl_give isl_basic_map *isl_basic_map_domain_map(
5954 __isl_take isl_basic_map *bmap)
5956 int i;
5957 isl_space *space;
5958 isl_basic_map *domain;
5959 isl_size nparam, n_in, n_out;
5961 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5962 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5963 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5964 if (nparam < 0 || n_in < 0 || n_out < 0)
5965 return isl_basic_map_free(bmap);
5967 space = isl_basic_map_get_space(bmap);
5968 space = isl_space_from_range(isl_space_domain(space));
5969 domain = isl_basic_map_universe(space);
5971 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5972 bmap = isl_basic_map_apply_range(bmap, domain);
5973 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5975 for (i = 0; i < n_in; ++i)
5976 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5977 isl_dim_out, i);
5979 bmap = isl_basic_map_gauss(bmap, NULL);
5980 return isl_basic_map_finalize(bmap);
5983 __isl_give isl_basic_map *isl_basic_map_range_map(
5984 __isl_take isl_basic_map *bmap)
5986 int i;
5987 isl_space *space;
5988 isl_basic_map *range;
5989 isl_size nparam, n_in, n_out;
5991 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5992 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5993 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5994 if (nparam < 0 || n_in < 0 || n_out < 0)
5995 return isl_basic_map_free(bmap);
5997 space = isl_basic_map_get_space(bmap);
5998 space = isl_space_from_range(isl_space_range(space));
5999 range = isl_basic_map_universe(space);
6001 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6002 bmap = isl_basic_map_apply_range(bmap, range);
6003 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6005 for (i = 0; i < n_out; ++i)
6006 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6007 isl_dim_out, i);
6009 bmap = isl_basic_map_gauss(bmap, NULL);
6010 return isl_basic_map_finalize(bmap);
6013 int isl_map_may_be_set(__isl_keep isl_map *map)
6015 if (!map)
6016 return -1;
6017 return isl_space_may_be_set(map->dim);
6020 /* Is this map actually a set?
6021 * Users should never call this function. Outside of isl,
6022 * the type should indicate whether something is a set or a map.
6024 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6026 if (!map)
6027 return isl_bool_error;
6028 return isl_space_is_set(map->dim);
6031 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6033 isl_space *space;
6034 isl_size n_in;
6036 n_in = isl_map_dim(map, isl_dim_in);
6037 if (n_in < 0)
6038 return set_from_map(isl_map_free(map));
6039 space = isl_space_range(isl_map_get_space(map));
6041 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6043 return set_from_map(isl_map_reset_space(map, space));
6046 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6047 * to each of its basic maps.
6049 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6050 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6051 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6053 int i;
6054 isl_space *space;
6056 map = isl_map_cow(map);
6057 if (!map)
6058 return NULL;
6060 for (i = 0; i < map->n; ++i) {
6061 map->p[i] = fn_bmap(map->p[i]);
6062 if (!map->p[i])
6063 return isl_map_free(map);
6065 map = isl_map_unmark_normalized(map);
6067 space = isl_map_take_space(map);
6068 space = fn_space(space);
6069 map = isl_map_restore_space(map, space);
6071 return map;
6074 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6076 return isl_map_transform(map, &isl_space_domain_map,
6077 &isl_basic_map_domain_map);
6080 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6082 return isl_map_transform(map, &isl_space_range_map,
6083 &isl_basic_map_range_map);
6086 /* Given a wrapped map of the form A[B -> C],
6087 * return the map A[B -> C] -> B.
6089 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6091 isl_id *id;
6092 isl_map *map;
6094 if (!set)
6095 return NULL;
6096 if (!isl_set_has_tuple_id(set))
6097 return isl_map_domain_map(isl_set_unwrap(set));
6099 id = isl_set_get_tuple_id(set);
6100 map = isl_map_domain_map(isl_set_unwrap(set));
6101 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6103 return map;
6106 __isl_give isl_basic_map *isl_basic_map_from_domain(
6107 __isl_take isl_basic_set *bset)
6109 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6112 __isl_give isl_basic_map *isl_basic_map_from_range(
6113 __isl_take isl_basic_set *bset)
6115 isl_space *space;
6116 space = isl_basic_set_get_space(bset);
6117 space = isl_space_from_range(space);
6118 bset = isl_basic_set_reset_space(bset, space);
6119 return bset_to_bmap(bset);
6122 /* Create a relation with the given set as range.
6123 * The domain of the created relation is a zero-dimensional
6124 * flat anonymous space.
6126 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6128 isl_space *space;
6129 space = isl_set_get_space(set);
6130 space = isl_space_from_range(space);
6131 set = isl_set_reset_space(set, space);
6132 return set_to_map(set);
6135 /* Create a relation with the given set as domain.
6136 * The range of the created relation is a zero-dimensional
6137 * flat anonymous space.
6139 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6141 return isl_map_reverse(isl_map_from_range(set));
6144 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6145 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6147 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6150 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6151 __isl_take isl_set *range)
6153 return isl_map_apply_range(isl_map_reverse(domain), range);
6156 /* Return a newly allocated isl_map with given space and flags and
6157 * room for "n" basic maps.
6158 * Make sure that all cached information is cleared.
6160 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6161 unsigned flags)
6163 struct isl_map *map;
6165 if (!space)
6166 return NULL;
6167 if (n < 0)
6168 isl_die(space->ctx, isl_error_internal,
6169 "negative number of basic maps", goto error);
6170 map = isl_calloc(space->ctx, struct isl_map,
6171 sizeof(struct isl_map) +
6172 (n - 1) * sizeof(struct isl_basic_map *));
6173 if (!map)
6174 goto error;
6176 map->ctx = space->ctx;
6177 isl_ctx_ref(map->ctx);
6178 map->ref = 1;
6179 map->size = n;
6180 map->n = 0;
6181 map->dim = space;
6182 map->flags = flags;
6183 return map;
6184 error:
6185 isl_space_free(space);
6186 return NULL;
6189 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6191 struct isl_basic_map *bmap;
6192 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6193 bmap = isl_basic_map_set_to_empty(bmap);
6194 return bmap;
6197 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6199 struct isl_basic_set *bset;
6200 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6201 bset = isl_basic_set_set_to_empty(bset);
6202 return bset;
6205 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6207 struct isl_basic_map *bmap;
6208 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6209 bmap = isl_basic_map_finalize(bmap);
6210 return bmap;
6213 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6215 struct isl_basic_set *bset;
6216 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6217 bset = isl_basic_set_finalize(bset);
6218 return bset;
6221 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6222 __isl_take isl_space *space)
6224 int i;
6225 isl_size total = isl_space_dim(space, isl_dim_all);
6226 isl_basic_map *bmap;
6228 if (total < 0)
6229 space = isl_space_free(space);
6230 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6231 for (i = 0; i < total; ++i) {
6232 int k = isl_basic_map_alloc_inequality(bmap);
6233 if (k < 0)
6234 goto error;
6235 isl_seq_clr(bmap->ineq[k], 1 + total);
6236 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6238 return bmap;
6239 error:
6240 isl_basic_map_free(bmap);
6241 return NULL;
6244 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6245 __isl_take isl_space *space)
6247 return isl_basic_map_nat_universe(space);
6250 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6252 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6255 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6257 return isl_map_nat_universe(space);
6260 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6262 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6265 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6267 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6270 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6272 struct isl_map *map;
6273 if (!space)
6274 return NULL;
6275 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6276 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6277 return map;
6280 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6282 struct isl_set *set;
6283 if (!space)
6284 return NULL;
6285 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6286 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6287 return set;
6290 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6292 int i;
6293 struct isl_map *dup;
6295 if (!map)
6296 return NULL;
6297 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6298 for (i = 0; i < map->n; ++i)
6299 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6300 return dup;
6303 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6304 __isl_take isl_basic_map *bmap)
6306 if (!bmap || !map)
6307 goto error;
6308 if (isl_basic_map_plain_is_empty(bmap)) {
6309 isl_basic_map_free(bmap);
6310 return map;
6312 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6313 goto error;
6314 isl_assert(map->ctx, map->n < map->size, goto error);
6315 map->p[map->n] = bmap;
6316 map->n++;
6317 map = isl_map_unmark_normalized(map);
6318 return map;
6319 error:
6320 if (map)
6321 isl_map_free(map);
6322 if (bmap)
6323 isl_basic_map_free(bmap);
6324 return NULL;
6327 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6329 int i;
6331 if (!map)
6332 return NULL;
6334 if (--map->ref > 0)
6335 return NULL;
6337 clear_caches(map);
6338 isl_ctx_deref(map->ctx);
6339 for (i = 0; i < map->n; ++i)
6340 isl_basic_map_free(map->p[i]);
6341 isl_space_free(map->dim);
6342 free(map);
6344 return NULL;
6347 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6348 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6350 int j;
6351 isl_size total;
6353 total = isl_basic_map_dim(bmap, isl_dim_all);
6354 if (total < 0)
6355 return isl_basic_map_free(bmap);
6357 bmap = isl_basic_map_cow(bmap);
6358 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6359 j = isl_basic_map_alloc_equality(bmap);
6360 if (j < 0)
6361 goto error;
6362 isl_seq_clr(bmap->eq[j] + 1, total);
6363 isl_int_set_si(bmap->eq[j][pos], -1);
6364 isl_int_set_si(bmap->eq[j][0], value);
6365 bmap = isl_basic_map_simplify(bmap);
6366 return isl_basic_map_finalize(bmap);
6367 error:
6368 isl_basic_map_free(bmap);
6369 return NULL;
6372 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6373 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6375 int j;
6376 isl_size total;
6378 total = isl_basic_map_dim(bmap, isl_dim_all);
6379 if (total < 0)
6380 return isl_basic_map_free(bmap);
6382 bmap = isl_basic_map_cow(bmap);
6383 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6384 j = isl_basic_map_alloc_equality(bmap);
6385 if (j < 0)
6386 goto error;
6387 isl_seq_clr(bmap->eq[j] + 1, total);
6388 isl_int_set_si(bmap->eq[j][pos], -1);
6389 isl_int_set(bmap->eq[j][0], value);
6390 bmap = isl_basic_map_simplify(bmap);
6391 return isl_basic_map_finalize(bmap);
6392 error:
6393 isl_basic_map_free(bmap);
6394 return NULL;
6397 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6398 enum isl_dim_type type, unsigned pos, int value)
6400 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6401 return isl_basic_map_free(bmap);
6402 return isl_basic_map_fix_pos_si(bmap,
6403 isl_basic_map_offset(bmap, type) + pos, value);
6406 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6407 enum isl_dim_type type, unsigned pos, isl_int value)
6409 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6410 return isl_basic_map_free(bmap);
6411 return isl_basic_map_fix_pos(bmap,
6412 isl_basic_map_offset(bmap, type) + pos, value);
6415 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6416 * to be equal to "v".
6418 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6419 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6421 if (!bmap || !v)
6422 goto error;
6423 if (!isl_val_is_int(v))
6424 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6425 "expecting integer value", goto error);
6426 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6427 goto error;
6428 pos += isl_basic_map_offset(bmap, type);
6429 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6430 isl_val_free(v);
6431 return bmap;
6432 error:
6433 isl_basic_map_free(bmap);
6434 isl_val_free(v);
6435 return NULL;
6438 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6439 * to be equal to "v".
6441 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6442 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6444 return isl_basic_map_fix_val(bset, type, pos, v);
6447 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6448 enum isl_dim_type type, unsigned pos, int value)
6450 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6451 type, pos, value));
6454 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6455 enum isl_dim_type type, unsigned pos, isl_int value)
6457 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6458 type, pos, value));
6461 /* Remove the basic map at position "i" from "map" if this basic map
6462 * is (obviously) empty.
6464 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6466 isl_bool empty;
6468 if (!map)
6469 return NULL;
6471 empty = isl_basic_map_plain_is_empty(map->p[i]);
6472 if (empty < 0)
6473 return isl_map_free(map);
6474 if (!empty)
6475 return map;
6477 isl_basic_map_free(map->p[i]);
6478 map->n--;
6479 if (i != map->n) {
6480 map->p[i] = map->p[map->n];
6481 map = isl_map_unmark_normalized(map);
6485 return map;
6488 /* Perform "fn" on each basic map of "map", where we may not be holding
6489 * the only reference to "map".
6490 * In particular, "fn" should be a semantics preserving operation
6491 * that we want to apply to all copies of "map". We therefore need
6492 * to be careful not to modify "map" in a way that breaks "map"
6493 * in case anything goes wrong.
6495 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6496 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6498 struct isl_basic_map *bmap;
6499 int i;
6501 if (!map)
6502 return NULL;
6504 for (i = map->n - 1; i >= 0; --i) {
6505 bmap = isl_basic_map_copy(map->p[i]);
6506 bmap = fn(bmap);
6507 if (!bmap)
6508 goto error;
6509 isl_basic_map_free(map->p[i]);
6510 map->p[i] = bmap;
6511 map = remove_if_empty(map, i);
6512 if (!map)
6513 return NULL;
6516 return map;
6517 error:
6518 isl_map_free(map);
6519 return NULL;
6522 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6523 enum isl_dim_type type, unsigned pos, int value)
6525 int i;
6527 map = isl_map_cow(map);
6528 if (isl_map_check_range(map, type, pos, 1) < 0)
6529 return isl_map_free(map);
6530 for (i = map->n - 1; i >= 0; --i) {
6531 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6532 map = remove_if_empty(map, i);
6533 if (!map)
6534 return NULL;
6536 map = isl_map_unmark_normalized(map);
6537 return map;
6540 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6541 enum isl_dim_type type, unsigned pos, int value)
6543 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6546 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6547 enum isl_dim_type type, unsigned pos, isl_int value)
6549 int i;
6551 map = isl_map_cow(map);
6552 if (isl_map_check_range(map, type, pos, 1) < 0)
6553 return isl_map_free(map);
6554 for (i = 0; i < map->n; ++i) {
6555 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6556 if (!map->p[i])
6557 goto error;
6559 map = isl_map_unmark_normalized(map);
6560 return map;
6561 error:
6562 isl_map_free(map);
6563 return NULL;
6566 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6567 enum isl_dim_type type, unsigned pos, isl_int value)
6569 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6572 /* Fix the value of the variable at position "pos" of type "type" of "map"
6573 * to be equal to "v".
6575 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6576 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6578 int i;
6580 map = isl_map_cow(map);
6581 if (!map || !v)
6582 goto error;
6584 if (!isl_val_is_int(v))
6585 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6586 "expecting integer value", goto error);
6587 if (isl_map_check_range(map, type, pos, 1) < 0)
6588 goto error;
6589 for (i = map->n - 1; i >= 0; --i) {
6590 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6591 isl_val_copy(v));
6592 map = remove_if_empty(map, i);
6593 if (!map)
6594 goto error;
6596 map = isl_map_unmark_normalized(map);
6597 isl_val_free(v);
6598 return map;
6599 error:
6600 isl_map_free(map);
6601 isl_val_free(v);
6602 return NULL;
6605 /* Fix the value of the variable at position "pos" of type "type" of "set"
6606 * to be equal to "v".
6608 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6609 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6611 return isl_map_fix_val(set, type, pos, v);
6614 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6615 unsigned input, int value)
6617 return isl_map_fix_si(map, isl_dim_in, input, value);
6620 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6621 int value)
6623 return set_from_map(isl_map_fix_si(set_to_map(set),
6624 isl_dim_set, dim, value));
6627 static __isl_give isl_basic_map *basic_map_bound_si(
6628 __isl_take isl_basic_map *bmap,
6629 enum isl_dim_type type, unsigned pos, int value, int upper)
6631 int j;
6632 isl_size total;
6634 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6635 return isl_basic_map_free(bmap);
6636 total = isl_basic_map_dim(bmap, isl_dim_all);
6637 if (total < 0)
6638 return isl_basic_map_free(bmap);
6639 pos += isl_basic_map_offset(bmap, type);
6640 bmap = isl_basic_map_cow(bmap);
6641 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6642 j = isl_basic_map_alloc_inequality(bmap);
6643 if (j < 0)
6644 goto error;
6645 isl_seq_clr(bmap->ineq[j], 1 + total);
6646 if (upper) {
6647 isl_int_set_si(bmap->ineq[j][pos], -1);
6648 isl_int_set_si(bmap->ineq[j][0], value);
6649 } else {
6650 isl_int_set_si(bmap->ineq[j][pos], 1);
6651 isl_int_set_si(bmap->ineq[j][0], -value);
6653 bmap = isl_basic_map_simplify(bmap);
6654 return isl_basic_map_finalize(bmap);
6655 error:
6656 isl_basic_map_free(bmap);
6657 return NULL;
6660 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6661 __isl_take isl_basic_map *bmap,
6662 enum isl_dim_type type, unsigned pos, int value)
6664 return basic_map_bound_si(bmap, type, pos, value, 0);
6667 /* Constrain the values of the given dimension to be no greater than "value".
6669 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6670 __isl_take isl_basic_map *bmap,
6671 enum isl_dim_type type, unsigned pos, int value)
6673 return basic_map_bound_si(bmap, type, pos, value, 1);
6676 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6677 enum isl_dim_type type, unsigned pos, int value, int upper)
6679 int i;
6681 map = isl_map_cow(map);
6682 if (isl_map_check_range(map, type, pos, 1) < 0)
6683 return isl_map_free(map);
6684 for (i = 0; i < map->n; ++i) {
6685 map->p[i] = basic_map_bound_si(map->p[i],
6686 type, pos, value, upper);
6687 if (!map->p[i])
6688 goto error;
6690 map = isl_map_unmark_normalized(map);
6691 return map;
6692 error:
6693 isl_map_free(map);
6694 return NULL;
6697 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6698 enum isl_dim_type type, unsigned pos, int value)
6700 return map_bound_si(map, type, pos, value, 0);
6703 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6704 enum isl_dim_type type, unsigned pos, int value)
6706 return map_bound_si(map, type, pos, value, 1);
6709 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6710 enum isl_dim_type type, unsigned pos, int value)
6712 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6713 type, pos, value));
6716 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6717 enum isl_dim_type type, unsigned pos, int value)
6719 return isl_map_upper_bound_si(set, type, pos, value);
6722 /* Bound the given variable of "bmap" from below (or above is "upper"
6723 * is set) to "value".
6725 static __isl_give isl_basic_map *basic_map_bound(
6726 __isl_take isl_basic_map *bmap,
6727 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6729 int j;
6730 isl_size total;
6732 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6733 return isl_basic_map_free(bmap);
6734 total = isl_basic_map_dim(bmap, isl_dim_all);
6735 if (total < 0)
6736 return isl_basic_map_free(bmap);
6737 pos += isl_basic_map_offset(bmap, type);
6738 bmap = isl_basic_map_cow(bmap);
6739 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6740 j = isl_basic_map_alloc_inequality(bmap);
6741 if (j < 0)
6742 goto error;
6743 isl_seq_clr(bmap->ineq[j], 1 + total);
6744 if (upper) {
6745 isl_int_set_si(bmap->ineq[j][pos], -1);
6746 isl_int_set(bmap->ineq[j][0], value);
6747 } else {
6748 isl_int_set_si(bmap->ineq[j][pos], 1);
6749 isl_int_neg(bmap->ineq[j][0], value);
6751 bmap = isl_basic_map_simplify(bmap);
6752 return isl_basic_map_finalize(bmap);
6753 error:
6754 isl_basic_map_free(bmap);
6755 return NULL;
6758 /* Bound the given variable of "map" from below (or above is "upper"
6759 * is set) to "value".
6761 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6762 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6764 int i;
6766 map = isl_map_cow(map);
6767 if (isl_map_check_range(map, type, pos, 1) < 0)
6768 return isl_map_free(map);
6769 for (i = map->n - 1; i >= 0; --i) {
6770 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6771 map = remove_if_empty(map, i);
6772 if (!map)
6773 return NULL;
6775 map = isl_map_unmark_normalized(map);
6776 return map;
6779 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6780 enum isl_dim_type type, unsigned pos, isl_int value)
6782 return map_bound(map, type, pos, value, 0);
6785 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6786 enum isl_dim_type type, unsigned pos, isl_int value)
6788 return map_bound(map, type, pos, value, 1);
6791 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6792 enum isl_dim_type type, unsigned pos, isl_int value)
6794 return isl_map_lower_bound(set, type, pos, value);
6797 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6798 enum isl_dim_type type, unsigned pos, isl_int value)
6800 return isl_map_upper_bound(set, type, pos, value);
6803 /* Force the values of the variable at position "pos" of type "type" of "map"
6804 * to be no smaller than "value".
6806 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6807 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6809 if (!value)
6810 goto error;
6811 if (!isl_val_is_int(value))
6812 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6813 "expecting integer value", goto error);
6814 map = isl_map_lower_bound(map, type, pos, value->n);
6815 isl_val_free(value);
6816 return map;
6817 error:
6818 isl_val_free(value);
6819 isl_map_free(map);
6820 return NULL;
6823 /* Force the values of the variable at position "pos" of type "type" of "set"
6824 * to be no smaller than "value".
6826 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6827 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6829 isl_map *map;
6831 map = set_to_map(set);
6832 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6835 /* Force the values of the variable at position "pos" of type "type" of "map"
6836 * to be no greater than "value".
6838 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6839 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6841 if (!value)
6842 goto error;
6843 if (!isl_val_is_int(value))
6844 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6845 "expecting integer value", goto error);
6846 map = isl_map_upper_bound(map, type, pos, value->n);
6847 isl_val_free(value);
6848 return map;
6849 error:
6850 isl_val_free(value);
6851 isl_map_free(map);
6852 return NULL;
6855 /* Force the values of the variable at position "pos" of type "type" of "set"
6856 * to be no greater than "value".
6858 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6859 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6861 isl_map *map;
6863 map = set_to_map(set);
6864 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6867 #undef BASE
6868 #define BASE val
6869 #include "isl_map_bound_templ.c"
6871 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6872 * for each set dimension, by treating the set as a map.
6874 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6875 __isl_take isl_multi_val *bound,
6876 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6877 unsigned pos, __isl_take isl_val *value))
6879 isl_map *map;
6881 map = set_to_map(set);
6882 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6885 #undef BASE
6886 #define BASE pw_aff
6887 #include "isl_map_bound_templ.c"
6889 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6890 * for each set dimension, by converting the set and the bound
6891 * to objects living in a map space.
6893 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6894 __isl_take isl_multi_pw_aff *bound,
6895 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6896 unsigned pos, __isl_take TYPE *value))
6898 isl_map *map;
6900 map = isl_map_from_range(set);
6901 bound = isl_multi_pw_aff_from_range(bound);
6902 map = map_bound_multi_pw_aff(map, bound, set_bound);
6903 return isl_map_range(map);
6906 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6907 * setting a bound on the given output dimension.
6909 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6910 unsigned pos, __isl_take isl_val *v)
6912 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6915 /* Force the values of the set dimensions of "set"
6916 * to be no smaller than the corresponding values in "lower".
6918 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
6919 __isl_take isl_multi_val *lower)
6921 return set_bound_multi_val(set, lower, &map_lower_bound_val);
6924 /* Force the values of the output dimensions of "map"
6925 * to be no smaller than the corresponding values in "lower".
6927 __isl_give isl_map *isl_map_lower_bound_multi_val(__isl_take isl_map *map,
6928 __isl_take isl_multi_val *lower)
6930 return map_bound_multi_val(map, lower, &map_lower_bound_val);
6933 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6934 * setting a bound on the given output dimension.
6936 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
6937 unsigned pos, __isl_take isl_val *v)
6939 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
6942 /* Force the values of the set dimensions of "set"
6943 * to be no greater than the corresponding values in "upper".
6945 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
6946 __isl_take isl_multi_val *upper)
6948 return set_bound_multi_val(set, upper, &map_upper_bound_val);
6951 /* Force the values of the set dimensions of "set"
6952 * to be no greater than the corresponding values in "upper".
6954 __isl_give isl_map *isl_map_upper_bound_multi_val(__isl_take isl_map *map,
6955 __isl_take isl_multi_val *upper)
6957 return map_bound_multi_val(map, upper, &map_upper_bound_val);
6960 /* Force the symbolic constant expression "bound"
6961 * to satisfy the relation "order" with respect to
6962 * the output variable at position "pos" of "map".
6964 * Create an affine expression representing the output variable
6965 * in terms of the range and
6966 * compare it using "order" to "bound" (defined on the domain).
6967 * The result is a relation between elements in domain and range that
6968 * can be intersected with "map".
6970 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
6971 unsigned pos, __isl_take isl_pw_aff *bound,
6972 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
6973 __isl_take isl_pw_aff *pa2))
6975 isl_space *space;
6976 isl_local_space *ls;
6977 isl_pw_aff *var;
6979 space = isl_space_range(isl_map_get_space(map));
6980 ls = isl_local_space_from_space(space);
6981 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
6982 map = isl_map_intersect(map, order(bound, var));
6983 return map;
6986 /* Force the values of the output variable at position "pos" of "map"
6987 * to be no smaller than the symbolic constant expression "lower".
6989 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
6990 unsigned pos, __isl_take isl_pw_aff *lower)
6992 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
6995 /* Force the values of the output variable at position "pos" of "map"
6996 * to be no greater than the symbolic constant expression "upper".
6998 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
6999 unsigned pos, __isl_take isl_pw_aff *upper)
7001 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7004 /* Force the values of the set dimensions of "set"
7005 * to be no smaller than the corresponding constant symbolic expressions
7006 * in "lower".
7008 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7009 __isl_take isl_multi_pw_aff *lower)
7011 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7014 /* Force the values of the set dimensions of "set"
7015 * to be no greater than the corresponding constant symbolic expressions
7016 * in "upper".
7018 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7019 __isl_take isl_multi_pw_aff *upper)
7021 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7024 /* Force the values of the output dimensions of "map"
7025 * to be no smaller than the corresponding constant symbolic expressions
7026 * in "lower".
7028 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7029 __isl_take isl_multi_pw_aff *lower)
7031 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7034 /* Force the values of the output dimensions of "map"
7035 * to be no greater than the corresponding constant symbolic expressions
7036 * in "upper".
7038 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7039 __isl_take isl_multi_pw_aff *upper)
7041 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7044 /* Bound the given variable of "bset" from below (or above is "upper"
7045 * is set) to "value".
7047 static __isl_give isl_basic_set *isl_basic_set_bound(
7048 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7049 isl_int value, int upper)
7051 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7052 type, pos, value, upper));
7055 /* Bound the given variable of "bset" from below (or above is "upper"
7056 * is set) to "value".
7058 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7059 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7060 __isl_take isl_val *value, int upper)
7062 if (!value)
7063 goto error;
7064 if (!isl_val_is_int(value))
7065 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7066 "expecting integer value", goto error);
7067 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7068 isl_val_free(value);
7069 return bset;
7070 error:
7071 isl_val_free(value);
7072 isl_basic_set_free(bset);
7073 return NULL;
7076 /* Bound the given variable of "bset" from below to "value".
7078 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7079 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7080 __isl_take isl_val *value)
7082 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7085 /* Bound the given variable of "bset" from above to "value".
7087 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7088 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7089 __isl_take isl_val *value)
7091 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7094 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7096 return isl_map_transform(map, &isl_space_reverse,
7097 &isl_basic_map_reverse);
7100 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7102 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7104 return isl_map_transform(map, &isl_space_range_reverse,
7105 &isl_basic_map_range_reverse);
7108 #undef TYPE
7109 #define TYPE isl_pw_multi_aff
7110 #undef SUFFIX
7111 #define SUFFIX _pw_multi_aff
7112 #undef EMPTY
7113 #define EMPTY isl_pw_multi_aff_empty
7114 #undef ADD
7115 #define ADD isl_pw_multi_aff_union_add
7116 #include "isl_map_lexopt_templ.c"
7118 /* Given a map "map", compute the lexicographically minimal
7119 * (or maximal) image element for each domain element in dom,
7120 * in the form of an isl_pw_multi_aff.
7121 * If "empty" is not NULL, then set *empty to those elements in dom that
7122 * do not have an image element.
7123 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7124 * should be computed over the domain of "map". "empty" is also NULL
7125 * in this case.
7127 * We first compute the lexicographically minimal or maximal element
7128 * in the first basic map. This results in a partial solution "res"
7129 * and a subset "todo" of dom that still need to be handled.
7130 * We then consider each of the remaining maps in "map" and successively
7131 * update both "res" and "todo".
7132 * If "empty" is NULL, then the todo sets are not needed and therefore
7133 * also not computed.
7135 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7136 __isl_take isl_map *map, __isl_take isl_set *dom,
7137 __isl_give isl_set **empty, unsigned flags)
7139 int i;
7140 int full;
7141 isl_pw_multi_aff *res;
7142 isl_set *todo;
7144 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7145 if (!map || (!full && !dom))
7146 goto error;
7148 if (isl_map_plain_is_empty(map)) {
7149 if (empty)
7150 *empty = dom;
7151 else
7152 isl_set_free(dom);
7153 return isl_pw_multi_aff_from_map(map);
7156 res = basic_map_partial_lexopt_pw_multi_aff(
7157 isl_basic_map_copy(map->p[0]),
7158 isl_set_copy(dom), empty, flags);
7160 if (empty)
7161 todo = *empty;
7162 for (i = 1; i < map->n; ++i) {
7163 isl_pw_multi_aff *res_i;
7165 res_i = basic_map_partial_lexopt_pw_multi_aff(
7166 isl_basic_map_copy(map->p[i]),
7167 isl_set_copy(dom), empty, flags);
7169 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7170 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7171 else
7172 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7174 if (empty)
7175 todo = isl_set_intersect(todo, *empty);
7178 isl_set_free(dom);
7179 isl_map_free(map);
7181 if (empty)
7182 *empty = todo;
7184 return res;
7185 error:
7186 if (empty)
7187 *empty = NULL;
7188 isl_set_free(dom);
7189 isl_map_free(map);
7190 return NULL;
7193 #undef TYPE
7194 #define TYPE isl_map
7195 #undef SUFFIX
7196 #define SUFFIX
7197 #undef EMPTY
7198 #define EMPTY isl_map_empty
7199 #undef ADD
7200 #define ADD isl_map_union_disjoint
7201 #include "isl_map_lexopt_templ.c"
7203 /* Given a map "map", compute the lexicographically minimal
7204 * (or maximal) image element for each domain element in "dom",
7205 * in the form of an isl_map.
7206 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7207 * do not have an image element.
7208 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7209 * should be computed over the domain of "map". "empty" is also NULL
7210 * in this case.
7212 * If the input consists of more than one disjunct, then first
7213 * compute the desired result in the form of an isl_pw_multi_aff and
7214 * then convert that into an isl_map.
7216 * This function used to have an explicit implementation in terms
7217 * of isl_maps, but it would continually intersect the domains of
7218 * partial results with the complement of the domain of the next
7219 * partial solution, potentially leading to an explosion in the number
7220 * of disjuncts if there are several disjuncts in the input.
7221 * An even earlier implementation of this function would look for
7222 * better results in the domain of the partial result and for extra
7223 * results in the complement of this domain, which would lead to
7224 * even more splintering.
7226 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7227 __isl_take isl_map *map, __isl_take isl_set *dom,
7228 __isl_give isl_set **empty, unsigned flags)
7230 int full;
7231 struct isl_map *res;
7232 isl_pw_multi_aff *pma;
7234 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7235 if (!map || (!full && !dom))
7236 goto error;
7238 if (isl_map_plain_is_empty(map)) {
7239 if (empty)
7240 *empty = dom;
7241 else
7242 isl_set_free(dom);
7243 return map;
7246 if (map->n == 1) {
7247 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7248 dom, empty, flags);
7249 isl_map_free(map);
7250 return res;
7253 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7254 flags);
7255 return isl_map_from_pw_multi_aff_internal(pma);
7256 error:
7257 if (empty)
7258 *empty = NULL;
7259 isl_set_free(dom);
7260 isl_map_free(map);
7261 return NULL;
7264 __isl_give isl_map *isl_map_partial_lexmax(
7265 __isl_take isl_map *map, __isl_take isl_set *dom,
7266 __isl_give isl_set **empty)
7268 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7271 __isl_give isl_map *isl_map_partial_lexmin(
7272 __isl_take isl_map *map, __isl_take isl_set *dom,
7273 __isl_give isl_set **empty)
7275 return isl_map_partial_lexopt(map, dom, empty, 0);
7278 __isl_give isl_set *isl_set_partial_lexmin(
7279 __isl_take isl_set *set, __isl_take isl_set *dom,
7280 __isl_give isl_set **empty)
7282 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7283 dom, empty));
7286 __isl_give isl_set *isl_set_partial_lexmax(
7287 __isl_take isl_set *set, __isl_take isl_set *dom,
7288 __isl_give isl_set **empty)
7290 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7291 dom, empty));
7294 /* Compute the lexicographic minimum (or maximum if "flags" includes
7295 * ISL_OPT_MAX) of "bset" over its parametric domain.
7297 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7298 unsigned flags)
7300 return isl_basic_map_lexopt(bset, flags);
7303 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7305 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7308 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7310 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7313 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7315 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7318 /* Compute the lexicographic minimum of "bset" over its parametric domain
7319 * for the purpose of quantifier elimination.
7320 * That is, find an explicit representation for all the existentially
7321 * quantified variables in "bset" by computing their lexicographic
7322 * minimum.
7324 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7325 __isl_take isl_basic_set *bset)
7327 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7330 /* Given a basic map with one output dimension, compute the minimum or
7331 * maximum of that dimension as an isl_pw_aff.
7333 * Compute the optimum as a lexicographic optimum over the single
7334 * output dimension and extract the single isl_pw_aff from the result.
7336 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7337 int max)
7339 isl_pw_multi_aff *pma;
7340 isl_pw_aff *pwaff;
7342 bmap = isl_basic_map_copy(bmap);
7343 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7344 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7345 isl_pw_multi_aff_free(pma);
7347 return pwaff;
7350 /* Compute the minimum or maximum of the given output dimension
7351 * as a function of the parameters and the input dimensions,
7352 * but independently of the other output dimensions.
7354 * We first project out the other output dimension and then compute
7355 * the "lexicographic" maximum in each basic map, combining the results
7356 * using isl_pw_aff_union_max.
7358 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7359 int max)
7361 int i;
7362 isl_pw_aff *pwaff;
7363 isl_size n_out;
7365 n_out = isl_map_dim(map, isl_dim_out);
7366 if (n_out < 0)
7367 map = isl_map_free(map);
7368 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7369 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7370 if (!map)
7371 return NULL;
7373 if (map->n == 0) {
7374 isl_space *space = isl_map_get_space(map);
7375 isl_map_free(map);
7376 return isl_pw_aff_empty(space);
7379 pwaff = basic_map_dim_opt(map->p[0], max);
7380 for (i = 1; i < map->n; ++i) {
7381 isl_pw_aff *pwaff_i;
7383 pwaff_i = basic_map_dim_opt(map->p[i], max);
7384 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7387 isl_map_free(map);
7389 return pwaff;
7392 /* Compute the minimum of the given output dimension as a function of the
7393 * parameters and input dimensions, but independently of
7394 * the other output dimensions.
7396 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7398 return map_dim_opt(map, pos, 0);
7401 /* Compute the maximum of the given output dimension as a function of the
7402 * parameters and input dimensions, but independently of
7403 * the other output dimensions.
7405 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7407 return map_dim_opt(map, pos, 1);
7410 /* Compute the minimum or maximum of the given set dimension
7411 * as a function of the parameters,
7412 * but independently of the other set dimensions.
7414 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7415 int max)
7417 return map_dim_opt(set, pos, max);
7420 /* Compute the maximum of the given set dimension as a function of the
7421 * parameters, but independently of the other set dimensions.
7423 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7425 return set_dim_opt(set, pos, 1);
7428 /* Compute the minimum of the given set dimension as a function of the
7429 * parameters, but independently of the other set dimensions.
7431 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7433 return set_dim_opt(set, pos, 0);
7436 /* Apply a preimage specified by "mat" on the parameters of "bset".
7437 * bset is assumed to have only parameters and divs.
7439 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7440 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7442 isl_size nparam;
7444 nparam = isl_basic_set_dim(bset, isl_dim_param);
7445 if (nparam < 0 || !mat)
7446 goto error;
7448 bset->dim = isl_space_cow(bset->dim);
7449 if (!bset->dim)
7450 goto error;
7452 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7454 bset->dim->nparam = 0;
7455 bset->dim->n_out = nparam;
7456 bset = isl_basic_set_preimage(bset, mat);
7457 if (bset) {
7458 bset->dim->nparam = bset->dim->n_out;
7459 bset->dim->n_out = 0;
7461 return bset;
7462 error:
7463 isl_mat_free(mat);
7464 isl_basic_set_free(bset);
7465 return NULL;
7468 /* Apply a preimage specified by "mat" on the parameters of "set".
7469 * set is assumed to have only parameters and divs.
7471 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7472 __isl_take isl_mat *mat)
7474 isl_space *space;
7475 isl_size nparam;
7477 nparam = isl_set_dim(set, isl_dim_param);
7478 if (nparam < 0 || !mat)
7479 goto error;
7481 if (mat->n_row != 1 + nparam)
7482 isl_die(isl_set_get_ctx(set), isl_error_internal,
7483 "unexpected number of rows", goto error);
7485 space = isl_set_get_space(set);
7486 space = isl_space_move_dims(space, isl_dim_set, 0,
7487 isl_dim_param, 0, nparam);
7488 set = isl_set_reset_space(set, space);
7489 set = isl_set_preimage(set, mat);
7490 nparam = isl_set_dim(set, isl_dim_out);
7491 if (nparam < 0)
7492 set = isl_set_free(set);
7493 space = isl_set_get_space(set);
7494 space = isl_space_move_dims(space, isl_dim_param, 0,
7495 isl_dim_out, 0, nparam);
7496 set = isl_set_reset_space(set, space);
7497 return set;
7498 error:
7499 isl_mat_free(mat);
7500 isl_set_free(set);
7501 return NULL;
7504 /* Intersect the basic set "bset" with the affine space specified by the
7505 * equalities in "eq".
7507 static __isl_give isl_basic_set *basic_set_append_equalities(
7508 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7510 int i, k;
7511 unsigned len;
7513 if (!bset || !eq)
7514 goto error;
7516 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7517 if (!bset)
7518 goto error;
7520 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7521 for (i = 0; i < eq->n_row; ++i) {
7522 k = isl_basic_set_alloc_equality(bset);
7523 if (k < 0)
7524 goto error;
7525 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7526 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7528 isl_mat_free(eq);
7530 bset = isl_basic_set_gauss(bset, NULL);
7531 bset = isl_basic_set_finalize(bset);
7533 return bset;
7534 error:
7535 isl_mat_free(eq);
7536 isl_basic_set_free(bset);
7537 return NULL;
7540 /* Intersect the set "set" with the affine space specified by the
7541 * equalities in "eq".
7543 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7544 __isl_take isl_mat *eq)
7546 int i;
7548 if (!set || !eq)
7549 goto error;
7551 for (i = 0; i < set->n; ++i) {
7552 set->p[i] = basic_set_append_equalities(set->p[i],
7553 isl_mat_copy(eq));
7554 if (!set->p[i])
7555 goto error;
7557 isl_mat_free(eq);
7558 return set;
7559 error:
7560 isl_mat_free(eq);
7561 isl_set_free(set);
7562 return NULL;
7565 /* Given a basic set "bset" that only involves parameters and existentially
7566 * quantified variables, return the index of the first equality
7567 * that only involves parameters. If there is no such equality then
7568 * return bset->n_eq.
7570 * This function assumes that isl_basic_set_gauss has been called on "bset".
7572 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7574 int i, j;
7575 isl_size nparam, n_div;
7577 nparam = isl_basic_set_dim(bset, isl_dim_param);
7578 n_div = isl_basic_set_dim(bset, isl_dim_div);
7579 if (nparam < 0 || n_div < 0)
7580 return -1;
7582 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7583 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7584 ++i;
7587 return i;
7590 /* Compute an explicit representation for the existentially quantified
7591 * variables in "bset" by computing the "minimal value" of the set
7592 * variables. Since there are no set variables, the computation of
7593 * the minimal value essentially computes an explicit representation
7594 * of the non-empty part(s) of "bset".
7596 * The input only involves parameters and existentially quantified variables.
7597 * All equalities among parameters have been removed.
7599 * Since the existentially quantified variables in the result are in general
7600 * going to be different from those in the input, we first replace
7601 * them by the minimal number of variables based on their equalities.
7602 * This should simplify the parametric integer programming.
7604 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7606 isl_morph *morph1, *morph2;
7607 isl_set *set;
7608 isl_size n;
7610 if (!bset)
7611 return NULL;
7612 if (bset->n_eq == 0)
7613 return isl_basic_set_lexmin_compute_divs(bset);
7615 morph1 = isl_basic_set_parameter_compression(bset);
7616 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7617 bset = isl_basic_set_lift(bset);
7618 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7619 bset = isl_morph_basic_set(morph2, bset);
7620 n = isl_basic_set_dim(bset, isl_dim_set);
7621 if (n < 0)
7622 bset = isl_basic_set_free(bset);
7623 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7625 set = isl_basic_set_lexmin_compute_divs(bset);
7627 set = isl_morph_set(isl_morph_inverse(morph1), set);
7629 return set;
7632 /* Project the given basic set onto its parameter domain, possibly introducing
7633 * new, explicit, existential variables in the constraints.
7634 * The input has parameters and (possibly implicit) existential variables.
7635 * The output has the same parameters, but only
7636 * explicit existentially quantified variables.
7638 * The actual projection is performed by pip, but pip doesn't seem
7639 * to like equalities very much, so we first remove the equalities
7640 * among the parameters by performing a variable compression on
7641 * the parameters. Afterward, an inverse transformation is performed
7642 * and the equalities among the parameters are inserted back in.
7644 * The variable compression on the parameters may uncover additional
7645 * equalities that were only implicit before. We therefore check
7646 * if there are any new parameter equalities in the result and
7647 * if so recurse. The removal of parameter equalities is required
7648 * for the parameter compression performed by base_compute_divs.
7650 static __isl_give isl_set *parameter_compute_divs(
7651 __isl_take isl_basic_set *bset)
7653 int i;
7654 struct isl_mat *eq;
7655 struct isl_mat *T, *T2;
7656 struct isl_set *set;
7657 isl_size nparam;
7659 bset = isl_basic_set_cow(bset);
7660 if (!bset)
7661 return NULL;
7663 if (bset->n_eq == 0)
7664 return base_compute_divs(bset);
7666 bset = isl_basic_set_gauss(bset, NULL);
7667 if (!bset)
7668 return NULL;
7669 if (isl_basic_set_plain_is_empty(bset))
7670 return isl_set_from_basic_set(bset);
7672 i = first_parameter_equality(bset);
7673 if (i == bset->n_eq)
7674 return base_compute_divs(bset);
7676 nparam = isl_basic_set_dim(bset, isl_dim_param);
7677 if (nparam < 0)
7678 return isl_set_from_basic_set(isl_basic_set_free(bset));
7679 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7680 0, 1 + nparam);
7681 eq = isl_mat_cow(eq);
7682 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7683 if (T && T->n_col == 0) {
7684 isl_mat_free(T);
7685 isl_mat_free(T2);
7686 isl_mat_free(eq);
7687 bset = isl_basic_set_set_to_empty(bset);
7688 return isl_set_from_basic_set(bset);
7690 bset = basic_set_parameter_preimage(bset, T);
7692 i = first_parameter_equality(bset);
7693 if (!bset)
7694 set = NULL;
7695 else if (i == bset->n_eq)
7696 set = base_compute_divs(bset);
7697 else
7698 set = parameter_compute_divs(bset);
7699 set = set_parameter_preimage(set, T2);
7700 set = set_append_equalities(set, eq);
7701 return set;
7704 /* Insert the divs from "ls" before those of "bmap".
7706 * The number of columns is not changed, which means that the last
7707 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7708 * The caller is responsible for removing the same number of dimensions
7709 * from the space of "bmap".
7711 static __isl_give isl_basic_map *insert_divs_from_local_space(
7712 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7714 int i;
7715 isl_size n_div;
7716 int old_n_div;
7718 n_div = isl_local_space_dim(ls, isl_dim_div);
7719 if (n_div < 0)
7720 return isl_basic_map_free(bmap);
7721 if (n_div == 0)
7722 return bmap;
7724 old_n_div = bmap->n_div;
7725 bmap = insert_div_rows(bmap, n_div);
7726 if (!bmap)
7727 return NULL;
7729 for (i = 0; i < n_div; ++i) {
7730 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7731 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7734 return bmap;
7737 /* Replace the space of "bmap" by the space and divs of "ls".
7739 * If "ls" has any divs, then we simplify the result since we may
7740 * have discovered some additional equalities that could simplify
7741 * the div expressions.
7743 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7744 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7746 isl_size n_div;
7748 bmap = isl_basic_map_cow(bmap);
7749 n_div = isl_local_space_dim(ls, isl_dim_div);
7750 if (!bmap || n_div < 0)
7751 goto error;
7753 bmap = insert_divs_from_local_space(bmap, ls);
7754 if (!bmap)
7755 goto error;
7757 isl_space_free(bmap->dim);
7758 bmap->dim = isl_local_space_get_space(ls);
7759 if (!bmap->dim)
7760 goto error;
7762 isl_local_space_free(ls);
7763 if (n_div > 0)
7764 bmap = isl_basic_map_simplify(bmap);
7765 bmap = isl_basic_map_finalize(bmap);
7766 return bmap;
7767 error:
7768 isl_basic_map_free(bmap);
7769 isl_local_space_free(ls);
7770 return NULL;
7773 /* Replace the space of "map" by the space and divs of "ls".
7775 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7776 __isl_take isl_local_space *ls)
7778 int i;
7780 map = isl_map_cow(map);
7781 if (!map || !ls)
7782 goto error;
7784 for (i = 0; i < map->n; ++i) {
7785 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7786 isl_local_space_copy(ls));
7787 if (!map->p[i])
7788 goto error;
7790 isl_space_free(isl_map_take_space(map));
7791 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7793 isl_local_space_free(ls);
7794 return map;
7795 error:
7796 isl_local_space_free(ls);
7797 isl_map_free(map);
7798 return NULL;
7801 /* Compute an explicit representation for the existentially
7802 * quantified variables for which do not know any explicit representation yet.
7804 * We first sort the existentially quantified variables so that the
7805 * existentially quantified variables for which we already have an explicit
7806 * representation are placed before those for which we do not.
7807 * The input dimensions, the output dimensions and the existentially
7808 * quantified variables for which we already have an explicit
7809 * representation are then turned into parameters.
7810 * compute_divs returns a map with the same parameters and
7811 * no input or output dimensions and the dimension specification
7812 * is reset to that of the input, including the existentially quantified
7813 * variables for which we already had an explicit representation.
7815 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7817 struct isl_basic_set *bset;
7818 struct isl_set *set;
7819 struct isl_map *map;
7820 isl_space *space;
7821 isl_local_space *ls;
7822 isl_size nparam;
7823 isl_size n_in;
7824 isl_size n_out;
7825 int n_known;
7826 int i;
7828 bmap = isl_basic_map_sort_divs(bmap);
7829 bmap = isl_basic_map_cow(bmap);
7830 if (!bmap)
7831 return NULL;
7833 n_known = isl_basic_map_first_unknown_div(bmap);
7834 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7835 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7836 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7837 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7838 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7840 space = isl_space_set_alloc(bmap->ctx,
7841 nparam + n_in + n_out + n_known, 0);
7842 if (!space)
7843 goto error;
7845 ls = isl_basic_map_get_local_space(bmap);
7846 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7847 n_known, bmap->n_div - n_known);
7848 if (n_known > 0) {
7849 for (i = n_known; i < bmap->n_div; ++i)
7850 swap_div(bmap, i - n_known, i);
7851 bmap->n_div -= n_known;
7852 bmap->extra -= n_known;
7854 bmap = isl_basic_map_reset_space(bmap, space);
7855 bset = bset_from_bmap(bmap);
7857 set = parameter_compute_divs(bset);
7858 map = set_to_map(set);
7859 map = replace_space_by_local_space(map, ls);
7861 return map;
7862 error:
7863 isl_basic_map_free(bmap);
7864 return NULL;
7867 /* Remove the explicit representation of local variable "div",
7868 * if there is any.
7870 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7871 __isl_take isl_basic_map *bmap, int div)
7873 isl_bool unknown;
7875 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7876 if (unknown < 0)
7877 return isl_basic_map_free(bmap);
7878 if (unknown)
7879 return bmap;
7881 bmap = isl_basic_map_cow(bmap);
7882 if (!bmap)
7883 return NULL;
7884 isl_int_set_si(bmap->div[div][0], 0);
7885 return bmap;
7888 /* Is local variable "div" of "bmap" marked as not having an explicit
7889 * representation?
7890 * Note that even if "div" is not marked in this way and therefore
7891 * has an explicit representation, this representation may still
7892 * depend (indirectly) on other local variables that do not
7893 * have an explicit representation.
7895 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7896 int div)
7898 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7899 return isl_bool_error;
7900 return isl_int_is_zero(bmap->div[div][0]);
7903 /* Return the position of the first local variable that does not
7904 * have an explicit representation.
7905 * Return the total number of local variables if they all have
7906 * an explicit representation.
7907 * Return -1 on error.
7909 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7911 int i;
7913 if (!bmap)
7914 return -1;
7916 for (i = 0; i < bmap->n_div; ++i) {
7917 if (!isl_basic_map_div_is_known(bmap, i))
7918 return i;
7920 return bmap->n_div;
7923 /* Return the position of the first local variable that does not
7924 * have an explicit representation.
7925 * Return the total number of local variables if they all have
7926 * an explicit representation.
7927 * Return -1 on error.
7929 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7931 return isl_basic_map_first_unknown_div(bset);
7934 /* Does "bmap" have an explicit representation for all local variables?
7936 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7938 int first;
7939 isl_size n;
7941 n = isl_basic_map_dim(bmap, isl_dim_div);
7942 first = isl_basic_map_first_unknown_div(bmap);
7943 if (n < 0 || first < 0)
7944 return isl_bool_error;
7945 return first == n;
7948 /* Do all basic maps in "map" have an explicit representation
7949 * for all local variables?
7951 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7953 int i;
7955 if (!map)
7956 return isl_bool_error;
7958 for (i = 0; i < map->n; ++i) {
7959 int known = isl_basic_map_divs_known(map->p[i]);
7960 if (known <= 0)
7961 return known;
7964 return isl_bool_true;
7967 /* If bmap contains any unknown divs, then compute explicit
7968 * expressions for them. However, this computation may be
7969 * quite expensive, so first try to remove divs that aren't
7970 * strictly needed.
7972 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7974 int known;
7975 struct isl_map *map;
7977 known = isl_basic_map_divs_known(bmap);
7978 if (known < 0)
7979 goto error;
7980 if (known)
7981 return isl_map_from_basic_map(bmap);
7983 bmap = isl_basic_map_drop_redundant_divs(bmap);
7985 known = isl_basic_map_divs_known(bmap);
7986 if (known < 0)
7987 goto error;
7988 if (known)
7989 return isl_map_from_basic_map(bmap);
7991 map = compute_divs(bmap);
7992 return map;
7993 error:
7994 isl_basic_map_free(bmap);
7995 return NULL;
7998 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8000 int i;
8001 int known;
8002 struct isl_map *res;
8004 if (!map)
8005 return NULL;
8006 if (map->n == 0)
8007 return map;
8009 known = isl_map_divs_known(map);
8010 if (known < 0) {
8011 isl_map_free(map);
8012 return NULL;
8014 if (known)
8015 return map;
8017 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8018 for (i = 1 ; i < map->n; ++i) {
8019 struct isl_map *r2;
8020 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8021 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8022 res = isl_map_union_disjoint(res, r2);
8023 else
8024 res = isl_map_union(res, r2);
8026 isl_map_free(map);
8028 return res;
8031 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8033 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8036 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8038 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8041 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8043 isl_space *space;
8044 isl_size n_out;
8046 n_out = isl_map_dim(map, isl_dim_out);
8047 if (n_out < 0)
8048 return set_from_map(isl_map_free(map));
8049 space = isl_space_domain(isl_map_get_space(map));
8051 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8053 return set_from_map(isl_map_reset_space(map, space));
8056 /* Return the union of "map1" and "map2", where we assume for now that
8057 * "map1" and "map2" are disjoint. Note that the basic maps inside
8058 * "map1" or "map2" may not be disjoint from each other.
8059 * Also note that this function is also called from isl_map_union,
8060 * which takes care of handling the situation where "map1" and "map2"
8061 * may not be disjoint.
8063 * If one of the inputs is empty, we can simply return the other input.
8064 * Similarly, if one of the inputs is universal, then it is equal to the union.
8066 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8067 __isl_take isl_map *map2)
8069 int i;
8070 unsigned flags = 0;
8071 struct isl_map *map = NULL;
8072 int is_universe;
8074 if (isl_map_check_equal_space(map1, map2) < 0)
8075 goto error;
8077 if (map1->n == 0) {
8078 isl_map_free(map1);
8079 return map2;
8081 if (map2->n == 0) {
8082 isl_map_free(map2);
8083 return map1;
8086 is_universe = isl_map_plain_is_universe(map1);
8087 if (is_universe < 0)
8088 goto error;
8089 if (is_universe) {
8090 isl_map_free(map2);
8091 return map1;
8094 is_universe = isl_map_plain_is_universe(map2);
8095 if (is_universe < 0)
8096 goto error;
8097 if (is_universe) {
8098 isl_map_free(map1);
8099 return map2;
8102 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8103 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8104 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8106 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8107 map1->n + map2->n, flags);
8108 if (!map)
8109 goto error;
8110 for (i = 0; i < map1->n; ++i) {
8111 map = isl_map_add_basic_map(map,
8112 isl_basic_map_copy(map1->p[i]));
8113 if (!map)
8114 goto error;
8116 for (i = 0; i < map2->n; ++i) {
8117 map = isl_map_add_basic_map(map,
8118 isl_basic_map_copy(map2->p[i]));
8119 if (!map)
8120 goto error;
8122 isl_map_free(map1);
8123 isl_map_free(map2);
8124 return map;
8125 error:
8126 isl_map_free(map);
8127 isl_map_free(map1);
8128 isl_map_free(map2);
8129 return NULL;
8132 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8133 * guaranteed to be disjoint by the caller.
8135 * Note that this functions is called from within isl_map_make_disjoint,
8136 * so we have to be careful not to touch the constraints of the inputs
8137 * in any way.
8139 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8140 __isl_take isl_map *map2)
8142 isl_map_align_params_bin(&map1, &map2);
8143 return map_union_disjoint(map1, map2);
8146 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8147 * not be disjoint.
8149 * We currently simply call map_union_disjoint, the internal operation
8150 * of which does not really depend on the inputs being disjoint.
8151 * If the result contains more than one basic map, then we clear
8152 * the disjoint flag since the result may contain basic maps from
8153 * both inputs and these are not guaranteed to be disjoint.
8155 * As a special case, if "map1" and "map2" are obviously equal,
8156 * then we simply return "map1".
8158 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8159 __isl_take isl_map *map2)
8161 int equal;
8163 if (isl_map_align_params_bin(&map1, &map2) < 0)
8164 goto error;
8166 equal = isl_map_plain_is_equal(map1, map2);
8167 if (equal < 0)
8168 goto error;
8169 if (equal) {
8170 isl_map_free(map2);
8171 return map1;
8174 map1 = map_union_disjoint(map1, map2);
8175 if (!map1)
8176 return NULL;
8177 if (map1->n > 1)
8178 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8179 return map1;
8180 error:
8181 isl_map_free(map1);
8182 isl_map_free(map2);
8183 return NULL;
8186 __isl_give isl_set *isl_set_union_disjoint(
8187 __isl_take isl_set *set1, __isl_take isl_set *set2)
8189 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8190 set_to_map(set2)));
8193 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8194 __isl_take isl_set *set2)
8196 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8199 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8200 * the results in a map living in "space".
8202 * "map" and "set" are assumed to be compatible and non-NULL.
8204 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8205 __isl_take isl_space *space, __isl_take isl_set *set,
8206 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8207 __isl_take isl_basic_set *bset))
8209 unsigned flags = 0;
8210 struct isl_map *result;
8211 int i, j;
8213 if (isl_set_plain_is_universe(set)) {
8214 isl_set_free(set);
8215 return isl_map_reset_equal_dim_space(map, space);
8218 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8219 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8220 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8222 result = isl_map_alloc_space(space, map->n * set->n, flags);
8223 for (i = 0; result && i < map->n; ++i)
8224 for (j = 0; j < set->n; ++j) {
8225 result = isl_map_add_basic_map(result,
8226 fn(isl_basic_map_copy(map->p[i]),
8227 isl_basic_set_copy(set->p[j])));
8228 if (!result)
8229 break;
8232 isl_map_free(map);
8233 isl_set_free(set);
8234 return result;
8237 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8238 __isl_take isl_set *set)
8240 isl_bool ok;
8241 isl_space *space;
8243 isl_map_align_params_set(&map, &set);
8244 ok = isl_map_compatible_range(map, set);
8245 if (ok < 0)
8246 goto error;
8247 if (!ok)
8248 isl_die(set->ctx, isl_error_invalid,
8249 "incompatible spaces", goto error);
8251 space = isl_map_get_space(map);
8252 return map_intersect_set(map, space, set,
8253 &isl_basic_map_intersect_range);
8254 error:
8255 isl_map_free(map);
8256 isl_set_free(set);
8257 return NULL;
8260 /* Intersect the domain of "map" with "set".
8262 * If the domain dimensions of "map" do not have any identifiers,
8263 * then copy them over from "set".
8265 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8266 __isl_take isl_set *set)
8268 isl_bool ok;
8269 isl_space *space;
8271 isl_map_align_params_set(&map, &set);
8272 ok = isl_map_compatible_domain(map, set);
8273 if (ok < 0)
8274 goto error;
8275 if (!ok)
8276 isl_die(set->ctx, isl_error_invalid,
8277 "incompatible spaces", goto error);
8279 space = isl_map_get_space(map);
8280 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8281 isl_set_peek_space(set), isl_dim_set);
8282 return map_intersect_set(map, space, set,
8283 &isl_basic_map_intersect_domain);
8284 error:
8285 isl_map_free(map);
8286 isl_set_free(set);
8287 return NULL;
8290 /* Data structure that specifies how isl_map_intersect_factor
8291 * should operate.
8293 * "preserve_type" is the tuple where the factor differs from
8294 * the input map and of which the identifiers needs
8295 * to be preserved explicitly.
8296 * "other_factor" is used to extract the space of the other factor
8297 * from the space of the product ("map").
8298 * "product" is used to combine the given factor and a universe map
8299 * in the space returned by "other_factor" to produce a map
8300 * that lives in the same space as the input map.
8302 struct isl_intersect_factor_control {
8303 enum isl_dim_type preserve_type;
8304 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8305 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8306 __isl_take isl_map *other);
8309 /* Given a map "map" in some product space and a map "factor"
8310 * living in some factor space, return the intersection.
8312 * After aligning the parameters,
8313 * the map "factor" is first extended to a map living in the same space
8314 * as "map" and then a regular intersection is computed.
8316 * Note that the extension is computed as a product, which is anonymous
8317 * by default. If "map" has an identifier on the corresponding tuple,
8318 * then this identifier needs to be set on the product
8319 * before the intersection is computed.
8321 static __isl_give isl_map *isl_map_intersect_factor(
8322 __isl_take isl_map *map, __isl_take isl_map *factor,
8323 struct isl_intersect_factor_control *control)
8325 isl_bool equal, has_id;
8326 isl_id *id;
8327 isl_space *space;
8328 isl_map *other, *product;
8330 equal = isl_map_has_equal_params(map, factor);
8331 if (equal < 0)
8332 goto error;
8333 if (!equal) {
8334 map = isl_map_align_params(map, isl_map_get_space(factor));
8335 factor = isl_map_align_params(factor, isl_map_get_space(map));
8338 space = isl_map_get_space(map);
8339 has_id = isl_space_has_tuple_id(space, control->preserve_type);
8340 if (has_id < 0)
8341 space = isl_space_free(space);
8342 else if (has_id)
8343 id = isl_space_get_tuple_id(space, control->preserve_type);
8345 other = isl_map_universe(control->other_factor(space));
8346 product = control->product(factor, other);
8348 if (has_id >= 0 && has_id)
8349 product = isl_map_set_tuple_id(product,
8350 control->preserve_type, id);
8352 return map_intersect(map, product);
8353 error:
8354 isl_map_free(map);
8355 isl_map_free(factor);
8356 return NULL;
8359 /* Return the domain product of "map2" and "map1".
8361 static __isl_give isl_map *isl_map_reverse_domain_product(
8362 __isl_take isl_map *map1, __isl_take isl_map *map2)
8364 return isl_map_domain_product(map2, map1);
8367 /* Return the range product of "map2" and "map1".
8369 static __isl_give isl_map *isl_map_reverse_range_product(
8370 __isl_take isl_map *map1, __isl_take isl_map *map2)
8372 return isl_map_range_product(map2, map1);
8375 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8376 * in the space A -> C, return the intersection.
8378 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8379 __isl_take isl_map *map, __isl_take isl_map *factor)
8381 struct isl_intersect_factor_control control = {
8382 .preserve_type = isl_dim_in,
8383 .other_factor = isl_space_domain_factor_range,
8384 .product = isl_map_domain_product,
8387 return isl_map_intersect_factor(map, factor, &control);
8390 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8391 * in the space B -> C, return the intersection.
8393 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8394 __isl_take isl_map *map, __isl_take isl_map *factor)
8396 struct isl_intersect_factor_control control = {
8397 .preserve_type = isl_dim_in,
8398 .other_factor = isl_space_domain_factor_domain,
8399 .product = isl_map_reverse_domain_product,
8402 return isl_map_intersect_factor(map, factor, &control);
8405 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8406 * in the space A -> B, return the intersection.
8408 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8409 __isl_take isl_map *map, __isl_take isl_map *factor)
8411 struct isl_intersect_factor_control control = {
8412 .preserve_type = isl_dim_out,
8413 .other_factor = isl_space_range_factor_range,
8414 .product = isl_map_range_product,
8417 return isl_map_intersect_factor(map, factor, &control);
8420 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8421 * in the space A -> C, return the intersection.
8423 __isl_give isl_map *isl_map_intersect_range_factor_range(
8424 __isl_take isl_map *map, __isl_take isl_map *factor)
8426 struct isl_intersect_factor_control control = {
8427 .preserve_type = isl_dim_out,
8428 .other_factor = isl_space_range_factor_domain,
8429 .product = isl_map_reverse_range_product,
8432 return isl_map_intersect_factor(map, factor, &control);
8435 /* Given a set "set" in a space [A -> B] and a set "domain"
8436 * in the space A, return the intersection.
8438 * The set "domain" is first extended to a set living in the space
8439 * [A -> B] and then a regular intersection is computed.
8441 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8442 __isl_take isl_set *domain)
8444 struct isl_intersect_factor_control control = {
8445 .preserve_type = isl_dim_set,
8446 .other_factor = isl_space_factor_range,
8447 .product = isl_map_range_product,
8450 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8451 set_to_map(domain), &control));
8454 /* Given a set "set" in a space [A -> B] and a set "range"
8455 * in the space B, return the intersection.
8457 * The set "range" is first extended to a set living in the space
8458 * [A -> B] and then a regular intersection is computed.
8460 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8461 __isl_take isl_set *range)
8463 struct isl_intersect_factor_control control = {
8464 .preserve_type = isl_dim_set,
8465 .other_factor = isl_space_factor_domain,
8466 .product = isl_map_reverse_range_product,
8469 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8470 set_to_map(range), &control));
8473 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8474 __isl_take isl_map *map2)
8476 if (isl_map_align_params_bin(&map1, &map2) < 0)
8477 goto error;
8478 map1 = isl_map_reverse(map1);
8479 map1 = isl_map_apply_range(map1, map2);
8480 return isl_map_reverse(map1);
8481 error:
8482 isl_map_free(map1);
8483 isl_map_free(map2);
8484 return NULL;
8487 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8488 __isl_take isl_map *map2)
8490 isl_space *space;
8491 struct isl_map *result;
8492 int i, j;
8494 if (isl_map_align_params_bin(&map1, &map2) < 0)
8495 goto error;
8497 space = isl_space_join(isl_space_copy(map1->dim),
8498 isl_space_copy(map2->dim));
8500 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8501 if (!result)
8502 goto error;
8503 for (i = 0; i < map1->n; ++i)
8504 for (j = 0; j < map2->n; ++j) {
8505 result = isl_map_add_basic_map(result,
8506 isl_basic_map_apply_range(
8507 isl_basic_map_copy(map1->p[i]),
8508 isl_basic_map_copy(map2->p[j])));
8509 if (!result)
8510 goto error;
8512 isl_map_free(map1);
8513 isl_map_free(map2);
8514 if (result && result->n <= 1)
8515 ISL_F_SET(result, ISL_MAP_DISJOINT);
8516 return result;
8517 error:
8518 isl_map_free(map1);
8519 isl_map_free(map2);
8520 return NULL;
8523 /* Is "bmap" a transformation, i.e.,
8524 * does it relate elements from the same space.
8526 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8528 isl_space *space;
8530 space = isl_basic_map_peek_space(bmap);
8531 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8534 /* Check that "bmap" is a transformation, i.e.,
8535 * that it relates elements from the same space.
8537 static isl_stat isl_basic_map_check_transformation(
8538 __isl_keep isl_basic_map *bmap)
8540 isl_bool equal;
8542 equal = isl_basic_map_is_transformation(bmap);
8543 if (equal < 0)
8544 return isl_stat_error;
8545 if (!equal)
8546 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8547 "domain and range don't match", return isl_stat_error);
8548 return isl_stat_ok;
8552 * returns range - domain
8554 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8556 isl_space *target_space;
8557 struct isl_basic_set *bset;
8558 isl_size dim;
8559 isl_size nparam;
8560 isl_size total;
8561 int i;
8563 if (isl_basic_map_check_transformation(bmap) < 0)
8564 return isl_basic_map_free(bmap);
8565 dim = isl_basic_map_dim(bmap, isl_dim_in);
8566 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8567 if (dim < 0 || nparam < 0)
8568 goto error;
8569 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8570 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8571 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8572 total = isl_basic_map_dim(bmap, isl_dim_all);
8573 if (total < 0)
8574 bmap = isl_basic_map_free(bmap);
8575 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8576 for (i = 0; i < dim; ++i) {
8577 int j = isl_basic_map_alloc_equality(bmap);
8578 if (j < 0) {
8579 bmap = isl_basic_map_free(bmap);
8580 break;
8582 isl_seq_clr(bmap->eq[j], 1 + total);
8583 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8584 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8585 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8587 bset = isl_basic_map_domain(bmap);
8588 bset = isl_basic_set_reset_space(bset, target_space);
8589 return bset;
8590 error:
8591 isl_basic_map_free(bmap);
8592 return NULL;
8595 /* Is the tuple of type "type1" of "map" the same as
8596 * the tuple of type "type2" of "space"?
8598 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8599 enum isl_dim_type type1, __isl_keep isl_space *space,
8600 enum isl_dim_type type2)
8602 isl_space *map_space;
8604 map_space = isl_map_peek_space(map);
8605 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8608 /* Is the tuple of type "type1" of "map1" the same as
8609 * the tuple of type "type2" of "map2"?
8611 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8612 enum isl_dim_type type1, __isl_keep isl_map *map2,
8613 enum isl_dim_type type2)
8615 isl_space *space1, *space2;
8617 space1 = isl_map_peek_space(map1);
8618 space2 = isl_map_peek_space(map2);
8619 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8622 /* Is the space of "obj" equal to "space", ignoring parameters?
8624 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8625 __isl_keep isl_space *space)
8627 isl_space *map_space;
8629 map_space = isl_map_peek_space(map);
8630 return isl_space_has_equal_tuples(map_space, space);
8633 /* Check that "map" is a transformation, i.e.,
8634 * that it relates elements from the same space.
8636 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8638 isl_bool equal;
8640 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8641 if (equal < 0)
8642 return isl_stat_error;
8643 if (!equal)
8644 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8645 "domain and range don't match", return isl_stat_error);
8646 return isl_stat_ok;
8650 * returns range - domain
8652 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8654 int i;
8655 isl_space *space;
8656 struct isl_set *result;
8658 if (isl_map_check_transformation(map) < 0)
8659 goto error;
8660 space = isl_map_get_space(map);
8661 space = isl_space_domain(space);
8662 result = isl_set_alloc_space(space, map->n, 0);
8663 if (!result)
8664 goto error;
8665 for (i = 0; i < map->n; ++i)
8666 result = isl_set_add_basic_set(result,
8667 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8668 isl_map_free(map);
8669 return result;
8670 error:
8671 isl_map_free(map);
8672 return NULL;
8676 * returns [domain -> range] -> range - domain
8678 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8679 __isl_take isl_basic_map *bmap)
8681 int i, k;
8682 isl_space *space;
8683 isl_basic_map *domain;
8684 isl_size nparam, n;
8685 isl_size total;
8687 if (isl_basic_map_check_transformation(bmap) < 0)
8688 return isl_basic_map_free(bmap);
8690 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8691 n = isl_basic_map_dim(bmap, isl_dim_in);
8692 if (nparam < 0 || n < 0)
8693 return isl_basic_map_free(bmap);
8695 space = isl_basic_map_get_space(bmap);
8696 space = isl_space_from_range(isl_space_domain(space));
8697 domain = isl_basic_map_universe(space);
8699 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8700 bmap = isl_basic_map_apply_range(bmap, domain);
8701 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8703 total = isl_basic_map_dim(bmap, isl_dim_all);
8704 if (total < 0)
8705 return isl_basic_map_free(bmap);
8707 for (i = 0; i < n; ++i) {
8708 k = isl_basic_map_alloc_equality(bmap);
8709 if (k < 0)
8710 goto error;
8711 isl_seq_clr(bmap->eq[k], 1 + total);
8712 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8713 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8714 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8717 bmap = isl_basic_map_gauss(bmap, NULL);
8718 return isl_basic_map_finalize(bmap);
8719 error:
8720 isl_basic_map_free(bmap);
8721 return NULL;
8725 * returns [domain -> range] -> range - domain
8727 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8729 if (isl_map_check_transformation(map) < 0)
8730 return isl_map_free(map);
8732 return isl_map_transform(map, &isl_space_range_map,
8733 &isl_basic_map_deltas_map);
8736 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8738 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8740 isl_space *space;
8741 isl_map *map;
8743 space = isl_space_map_from_set(isl_set_get_space(deltas));
8744 map = isl_map_deltas_map(isl_map_universe(space));
8745 map = isl_map_intersect_range(map, deltas);
8747 return isl_set_unwrap(isl_map_domain(map));
8750 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8752 isl_size n_in, n_out;
8754 n_in = isl_space_dim(space, isl_dim_in);
8755 n_out = isl_space_dim(space, isl_dim_out);
8756 if (n_in < 0 || n_out < 0)
8757 goto error;
8758 if (n_in != n_out)
8759 isl_die(space->ctx, isl_error_invalid,
8760 "number of input and output dimensions needs to be "
8761 "the same", goto error);
8762 return isl_basic_map_equal(space, n_in);
8763 error:
8764 isl_space_free(space);
8765 return NULL;
8768 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8770 return isl_map_from_basic_map(isl_basic_map_identity(space));
8773 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8775 isl_space *space = isl_set_get_space(set);
8776 isl_map *id;
8777 id = isl_map_identity(isl_space_map_from_set(space));
8778 return isl_map_intersect_range(id, set);
8781 /* Construct a basic set with all set dimensions having only non-negative
8782 * values.
8784 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8785 __isl_take isl_space *space)
8787 int i;
8788 isl_size nparam;
8789 isl_size dim;
8790 isl_size total;
8791 struct isl_basic_set *bset;
8793 nparam = isl_space_dim(space, isl_dim_param);
8794 dim = isl_space_dim(space, isl_dim_set);
8795 total = isl_space_dim(space, isl_dim_all);
8796 if (nparam < 0 || dim < 0 || total < 0)
8797 space = isl_space_free(space);
8798 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8799 if (!bset)
8800 return NULL;
8801 for (i = 0; i < dim; ++i) {
8802 int k = isl_basic_set_alloc_inequality(bset);
8803 if (k < 0)
8804 goto error;
8805 isl_seq_clr(bset->ineq[k], 1 + total);
8806 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8808 return bset;
8809 error:
8810 isl_basic_set_free(bset);
8811 return NULL;
8814 /* Construct the half-space x_pos >= 0.
8816 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8817 int pos)
8819 int k;
8820 isl_size total;
8821 isl_basic_set *nonneg;
8823 total = isl_space_dim(space, isl_dim_all);
8824 if (total < 0)
8825 space = isl_space_free(space);
8826 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8827 k = isl_basic_set_alloc_inequality(nonneg);
8828 if (k < 0)
8829 goto error;
8830 isl_seq_clr(nonneg->ineq[k], 1 + total);
8831 isl_int_set_si(nonneg->ineq[k][pos], 1);
8833 return isl_basic_set_finalize(nonneg);
8834 error:
8835 isl_basic_set_free(nonneg);
8836 return NULL;
8839 /* Construct the half-space x_pos <= -1.
8841 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8842 int pos)
8844 int k;
8845 isl_size total;
8846 isl_basic_set *neg;
8848 total = isl_space_dim(space, isl_dim_all);
8849 if (total < 0)
8850 space = isl_space_free(space);
8851 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8852 k = isl_basic_set_alloc_inequality(neg);
8853 if (k < 0)
8854 goto error;
8855 isl_seq_clr(neg->ineq[k], 1 + total);
8856 isl_int_set_si(neg->ineq[k][0], -1);
8857 isl_int_set_si(neg->ineq[k][pos], -1);
8859 return isl_basic_set_finalize(neg);
8860 error:
8861 isl_basic_set_free(neg);
8862 return NULL;
8865 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8866 enum isl_dim_type type, unsigned first, unsigned n)
8868 int i;
8869 unsigned offset;
8870 isl_basic_set *nonneg;
8871 isl_basic_set *neg;
8873 if (n == 0)
8874 return set;
8876 if (isl_set_check_range(set, type, first, n) < 0)
8877 return isl_set_free(set);
8879 offset = pos(set->dim, type);
8880 for (i = 0; i < n; ++i) {
8881 nonneg = nonneg_halfspace(isl_set_get_space(set),
8882 offset + first + i);
8883 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8885 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8888 return set;
8891 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8892 int len,
8893 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8894 void *user)
8896 isl_set *half;
8898 if (!set)
8899 return isl_stat_error;
8900 if (isl_set_plain_is_empty(set)) {
8901 isl_set_free(set);
8902 return isl_stat_ok;
8904 if (first == len)
8905 return fn(set, signs, user);
8907 signs[first] = 1;
8908 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8909 1 + first));
8910 half = isl_set_intersect(half, isl_set_copy(set));
8911 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8912 goto error;
8914 signs[first] = -1;
8915 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8916 1 + first));
8917 half = isl_set_intersect(half, set);
8918 return foreach_orthant(half, signs, first + 1, len, fn, user);
8919 error:
8920 isl_set_free(set);
8921 return isl_stat_error;
8924 /* Call "fn" on the intersections of "set" with each of the orthants
8925 * (except for obviously empty intersections). The orthant is identified
8926 * by the signs array, with each entry having value 1 or -1 according
8927 * to the sign of the corresponding variable.
8929 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8930 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8931 void *user)
8933 isl_size nparam;
8934 isl_size nvar;
8935 int *signs;
8936 isl_stat r;
8938 if (!set)
8939 return isl_stat_error;
8940 if (isl_set_plain_is_empty(set))
8941 return isl_stat_ok;
8943 nparam = isl_set_dim(set, isl_dim_param);
8944 nvar = isl_set_dim(set, isl_dim_set);
8945 if (nparam < 0 || nvar < 0)
8946 return isl_stat_error;
8948 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8950 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8951 fn, user);
8953 free(signs);
8955 return r;
8958 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8960 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8963 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8964 __isl_keep isl_basic_map *bmap2)
8966 isl_bool is_subset;
8967 struct isl_map *map1;
8968 struct isl_map *map2;
8970 if (!bmap1 || !bmap2)
8971 return isl_bool_error;
8973 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8974 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8976 is_subset = isl_map_is_subset(map1, map2);
8978 isl_map_free(map1);
8979 isl_map_free(map2);
8981 return is_subset;
8984 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8985 __isl_keep isl_basic_set *bset2)
8987 return isl_basic_map_is_subset(bset1, bset2);
8990 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8991 __isl_keep isl_basic_map *bmap2)
8993 isl_bool is_subset;
8995 if (!bmap1 || !bmap2)
8996 return isl_bool_error;
8997 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8998 if (is_subset != isl_bool_true)
8999 return is_subset;
9000 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9001 return is_subset;
9004 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9005 __isl_keep isl_basic_set *bset2)
9007 return isl_basic_map_is_equal(
9008 bset_to_bmap(bset1), bset_to_bmap(bset2));
9011 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9013 int i;
9014 int is_empty;
9016 if (!map)
9017 return isl_bool_error;
9018 for (i = 0; i < map->n; ++i) {
9019 is_empty = isl_basic_map_is_empty(map->p[i]);
9020 if (is_empty < 0)
9021 return isl_bool_error;
9022 if (!is_empty)
9023 return isl_bool_false;
9025 return isl_bool_true;
9028 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9030 return map ? map->n == 0 : isl_bool_error;
9033 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9035 return set ? set->n == 0 : isl_bool_error;
9038 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9040 return isl_map_is_empty(set_to_map(set));
9043 #undef TYPE
9044 #define TYPE isl_basic_map
9046 static
9047 #include "isl_type_has_equal_space_bin_templ.c"
9048 #include "isl_type_check_equal_space_templ.c"
9050 /* Check that "bset1" and "bset2" live in the same space,
9051 * reporting an error if they do not.
9053 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9054 __isl_keep isl_basic_set *bset2)
9056 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9057 bset_to_bmap(bset1));
9060 #undef TYPE
9061 #define TYPE isl_map
9063 #include "isl_type_has_equal_space_bin_templ.c"
9064 #include "isl_type_check_equal_space_templ.c"
9065 #include "isl_type_has_space_templ.c"
9067 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9068 __isl_keep isl_set *set2)
9070 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9073 #undef TYPE1
9074 #define TYPE1 isl_map
9075 #undef TYPE2
9076 #define TYPE2 isl_basic_map
9077 #undef TYPE_PAIR
9078 #define TYPE_PAIR isl_map_basic_map
9080 static
9081 #include "isl_type_has_equal_space_templ.c"
9082 #include "isl_type_check_equal_space_templ.c"
9084 /* Check that "set" and "bset" live in the same space,
9085 * reporting an error if they do not.
9087 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9088 __isl_keep isl_basic_set *bset)
9090 return isl_map_basic_map_check_equal_space(set_to_map(set),
9091 bset_to_bmap(bset));
9094 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9096 isl_bool is_subset;
9098 if (!map1 || !map2)
9099 return isl_bool_error;
9100 is_subset = isl_map_is_subset(map1, map2);
9101 if (is_subset != isl_bool_true)
9102 return is_subset;
9103 is_subset = isl_map_is_subset(map2, map1);
9104 return is_subset;
9107 /* Is "map1" equal to "map2"?
9109 * First check if they are obviously equal.
9110 * If not, then perform a more detailed analysis.
9112 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9114 isl_bool equal;
9116 equal = isl_map_plain_is_equal(map1, map2);
9117 if (equal < 0 || equal)
9118 return equal;
9119 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9122 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9123 __isl_keep isl_basic_map *bmap2)
9125 isl_bool is_subset;
9127 if (!bmap1 || !bmap2)
9128 return isl_bool_error;
9129 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9130 if (is_subset != isl_bool_true)
9131 return is_subset;
9132 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9133 return isl_bool_not(is_subset);
9136 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9137 __isl_keep isl_map *map2)
9139 isl_bool is_subset;
9141 if (!map1 || !map2)
9142 return isl_bool_error;
9143 is_subset = isl_map_is_subset(map1, map2);
9144 if (is_subset != isl_bool_true)
9145 return is_subset;
9146 is_subset = isl_map_is_subset(map2, map1);
9147 return isl_bool_not(is_subset);
9150 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9151 __isl_keep isl_set *set2)
9153 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9156 /* Is "bmap" obviously equal to the universe with the same space?
9158 * That is, does it not have any constraints?
9160 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9162 if (!bmap)
9163 return isl_bool_error;
9164 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9167 /* Is "bset" obviously equal to the universe with the same space?
9169 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9171 return isl_basic_map_plain_is_universe(bset);
9174 /* If "c" does not involve any existentially quantified variables,
9175 * then set *univ to false and abort
9177 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9179 isl_bool *univ = user;
9180 isl_size n;
9182 n = isl_constraint_dim(c, isl_dim_div);
9183 if (n < 0)
9184 c = isl_constraint_free(c);
9185 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9186 isl_constraint_free(c);
9187 if (*univ < 0 || !*univ)
9188 return isl_stat_error;
9189 return isl_stat_ok;
9192 /* Is "bmap" equal to the universe with the same space?
9194 * First check if it is obviously equal to the universe.
9195 * If not and if there are any constraints not involving
9196 * existentially quantified variables, then it is certainly
9197 * not equal to the universe.
9198 * Otherwise, check if the universe is a subset of "bmap".
9200 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9202 isl_size n_div;
9203 isl_bool univ;
9204 isl_basic_map *test;
9206 univ = isl_basic_map_plain_is_universe(bmap);
9207 if (univ < 0 || univ)
9208 return univ;
9209 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9210 if (n_div < 0)
9211 return isl_bool_error;
9212 if (n_div == 0)
9213 return isl_bool_false;
9214 univ = isl_bool_true;
9215 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9216 univ)
9217 return isl_bool_error;
9218 if (univ < 0 || !univ)
9219 return univ;
9220 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9221 univ = isl_basic_map_is_subset(test, bmap);
9222 isl_basic_map_free(test);
9223 return univ;
9226 /* Is "bset" equal to the universe with the same space?
9228 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9230 return isl_basic_map_is_universe(bset);
9233 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9235 int i;
9237 if (!map)
9238 return isl_bool_error;
9240 for (i = 0; i < map->n; ++i) {
9241 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9242 if (r < 0 || r)
9243 return r;
9246 return isl_bool_false;
9249 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9251 return isl_map_plain_is_universe(set_to_map(set));
9254 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9256 struct isl_basic_set *bset = NULL;
9257 struct isl_vec *sample = NULL;
9258 isl_bool empty, non_empty;
9260 if (!bmap)
9261 return isl_bool_error;
9263 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9264 return isl_bool_true;
9266 if (isl_basic_map_plain_is_universe(bmap))
9267 return isl_bool_false;
9269 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9270 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9271 copy = isl_basic_map_remove_redundancies(copy);
9272 empty = isl_basic_map_plain_is_empty(copy);
9273 isl_basic_map_free(copy);
9274 return empty;
9277 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9278 if (non_empty < 0)
9279 return isl_bool_error;
9280 if (non_empty)
9281 return isl_bool_false;
9282 isl_vec_free(bmap->sample);
9283 bmap->sample = NULL;
9284 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9285 if (!bset)
9286 return isl_bool_error;
9287 sample = isl_basic_set_sample_vec(bset);
9288 if (!sample)
9289 return isl_bool_error;
9290 empty = sample->size == 0;
9291 isl_vec_free(bmap->sample);
9292 bmap->sample = sample;
9293 if (empty)
9294 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9296 return empty;
9299 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9301 if (!bmap)
9302 return isl_bool_error;
9303 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9306 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9308 if (!bset)
9309 return isl_bool_error;
9310 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9313 /* Is "bmap" known to be non-empty?
9315 * That is, is the cached sample still valid?
9317 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9319 isl_size total;
9321 if (!bmap)
9322 return isl_bool_error;
9323 if (!bmap->sample)
9324 return isl_bool_false;
9325 total = isl_basic_map_dim(bmap, isl_dim_all);
9326 if (total < 0)
9327 return isl_bool_error;
9328 if (bmap->sample->size != 1 + total)
9329 return isl_bool_false;
9330 return isl_basic_map_contains(bmap, bmap->sample);
9333 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9335 return isl_basic_map_is_empty(bset_to_bmap(bset));
9338 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9339 __isl_take isl_basic_map *bmap2)
9341 struct isl_map *map;
9343 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9344 goto error;
9346 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9347 if (!map)
9348 goto error;
9349 map = isl_map_add_basic_map(map, bmap1);
9350 map = isl_map_add_basic_map(map, bmap2);
9351 return map;
9352 error:
9353 isl_basic_map_free(bmap1);
9354 isl_basic_map_free(bmap2);
9355 return NULL;
9358 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9359 __isl_take isl_basic_set *bset2)
9361 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9362 bset_to_bmap(bset2)));
9365 /* Order divs such that any div only depends on previous divs */
9366 __isl_give isl_basic_map *isl_basic_map_order_divs(
9367 __isl_take isl_basic_map *bmap)
9369 int i;
9370 isl_size off;
9372 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9373 if (off < 0)
9374 return isl_basic_map_free(bmap);
9376 for (i = 0; i < bmap->n_div; ++i) {
9377 int pos;
9378 if (isl_int_is_zero(bmap->div[i][0]))
9379 continue;
9380 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9381 bmap->n_div-i);
9382 if (pos == -1)
9383 continue;
9384 if (pos == 0)
9385 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9386 "integer division depends on itself",
9387 return isl_basic_map_free(bmap));
9388 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9389 if (!bmap)
9390 return NULL;
9391 --i;
9393 return bmap;
9396 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9398 int i;
9400 if (!map)
9401 return 0;
9403 for (i = 0; i < map->n; ++i) {
9404 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9405 if (!map->p[i])
9406 goto error;
9409 return map;
9410 error:
9411 isl_map_free(map);
9412 return NULL;
9415 /* Sort the local variables of "bset".
9417 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9418 __isl_take isl_basic_set *bset)
9420 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9423 /* Apply the expansion computed by isl_merge_divs.
9424 * The expansion itself is given by "exp" while the resulting
9425 * list of divs is given by "div".
9427 * Move the integer divisions of "bmap" into the right position
9428 * according to "exp" and then introduce the additional integer
9429 * divisions, adding div constraints.
9430 * The moving should be done first to avoid moving coefficients
9431 * in the definitions of the extra integer divisions.
9433 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9434 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9436 int i, j;
9437 int n_div;
9439 bmap = isl_basic_map_cow(bmap);
9440 if (!bmap || !div)
9441 goto error;
9443 if (div->n_row < bmap->n_div)
9444 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9445 "not an expansion", goto error);
9447 n_div = bmap->n_div;
9448 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9449 2 * (div->n_row - n_div));
9451 for (i = n_div; i < div->n_row; ++i)
9452 if (isl_basic_map_alloc_div(bmap) < 0)
9453 goto error;
9455 for (j = n_div - 1; j >= 0; --j) {
9456 if (exp[j] == j)
9457 break;
9458 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9459 if (!bmap)
9460 goto error;
9462 j = 0;
9463 for (i = 0; i < div->n_row; ++i) {
9464 if (j < n_div && exp[j] == i) {
9465 j++;
9466 } else {
9467 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9468 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9469 continue;
9470 bmap = isl_basic_map_add_div_constraints(bmap, i);
9471 if (!bmap)
9472 goto error;
9476 isl_mat_free(div);
9477 return bmap;
9478 error:
9479 isl_basic_map_free(bmap);
9480 isl_mat_free(div);
9481 return NULL;
9484 /* Apply the expansion computed by isl_merge_divs.
9485 * The expansion itself is given by "exp" while the resulting
9486 * list of divs is given by "div".
9488 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9489 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9491 return isl_basic_map_expand_divs(bset, div, exp);
9494 /* Look for a div in dst that corresponds to the div "div" in src.
9495 * The divs before "div" in src and dst are assumed to be the same.
9497 * Return the position of the corresponding div in dst
9498 * if there is one. Otherwise, return a position beyond the integer divisions.
9499 * Return -1 on error.
9501 static int find_div(__isl_keep isl_basic_map *dst,
9502 __isl_keep isl_basic_map *src, unsigned div)
9504 int i;
9505 isl_size n_div;
9506 isl_size v_div;
9508 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9509 n_div = isl_basic_map_dim(dst, isl_dim_div);
9510 if (n_div < 0 || v_div < 0)
9511 return -1;
9512 isl_assert(dst->ctx, div <= n_div, return -1);
9513 for (i = div; i < n_div; ++i)
9514 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9515 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9516 n_div - div) == -1)
9517 return i;
9518 return n_div;
9521 /* Align the divs of "dst" to those of "src", adding divs from "src"
9522 * if needed. That is, make sure that the first src->n_div divs
9523 * of the result are equal to those of src.
9525 * The result is not finalized as by design it will have redundant
9526 * divs if any divs from "src" were copied.
9528 __isl_give isl_basic_map *isl_basic_map_align_divs(
9529 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9531 int i;
9532 isl_bool known;
9533 int extended;
9534 isl_size v_div;
9535 isl_size dst_n_div;
9537 if (!dst || !src)
9538 return isl_basic_map_free(dst);
9540 if (src->n_div == 0)
9541 return dst;
9543 known = isl_basic_map_divs_known(src);
9544 if (known < 0)
9545 return isl_basic_map_free(dst);
9546 if (!known)
9547 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9548 "some src divs are unknown",
9549 return isl_basic_map_free(dst));
9551 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9552 if (v_div < 0)
9553 return isl_basic_map_free(dst);
9555 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
9556 if (!src)
9557 return isl_basic_map_free(dst);
9559 extended = 0;
9560 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9561 if (dst_n_div < 0)
9562 dst = isl_basic_map_free(dst);
9563 for (i = 0; i < src->n_div; ++i) {
9564 int j = find_div(dst, src, i);
9565 if (j < 0)
9566 dst = isl_basic_map_free(dst);
9567 if (j == dst_n_div) {
9568 if (!extended) {
9569 int extra = src->n_div - i;
9570 dst = isl_basic_map_cow(dst);
9571 if (!dst)
9572 goto error;
9573 dst = isl_basic_map_extend(dst,
9574 extra, 0, 2 * extra);
9575 extended = 1;
9577 j = isl_basic_map_alloc_div(dst);
9578 if (j < 0)
9579 goto error;
9580 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9581 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9582 dst_n_div++;
9583 dst = isl_basic_map_add_div_constraints(dst, j);
9584 if (!dst)
9585 goto error;
9587 if (j != i)
9588 dst = isl_basic_map_swap_div(dst, i, j);
9589 if (!dst)
9590 goto error;
9592 isl_basic_map_free(src);
9593 return dst;
9594 error:
9595 isl_basic_map_free(src);
9596 isl_basic_map_free(dst);
9597 return NULL;
9600 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9602 int i;
9604 if (!map)
9605 return NULL;
9606 if (map->n == 0)
9607 return map;
9608 map = isl_map_compute_divs(map);
9609 map = isl_map_cow(map);
9610 if (!map)
9611 return NULL;
9613 for (i = 1; i < map->n; ++i)
9614 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9615 for (i = 1; i < map->n; ++i) {
9616 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9617 if (!map->p[i])
9618 return isl_map_free(map);
9621 map = isl_map_unmark_normalized(map);
9622 return map;
9625 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9627 return isl_map_align_divs_internal(map);
9630 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9632 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9635 /* Align the divs of the basic maps in "map" to those
9636 * of the basic maps in "list", as well as to the other basic maps in "map".
9637 * The elements in "list" are assumed to have known divs.
9639 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9640 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9642 int i;
9643 isl_size n;
9645 n = isl_basic_map_list_n_basic_map(list);
9646 map = isl_map_compute_divs(map);
9647 map = isl_map_cow(map);
9648 if (!map || n < 0)
9649 return isl_map_free(map);
9650 if (map->n == 0)
9651 return map;
9653 for (i = 0; i < n; ++i) {
9654 isl_basic_map *bmap;
9656 bmap = isl_basic_map_list_get_basic_map(list, i);
9657 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9658 isl_basic_map_free(bmap);
9660 if (!map->p[0])
9661 return isl_map_free(map);
9663 return isl_map_align_divs_internal(map);
9666 /* Align the divs of each element of "list" to those of "bmap".
9667 * Both "bmap" and the elements of "list" are assumed to have known divs.
9669 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9670 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9672 int i;
9673 isl_size n;
9675 n = isl_basic_map_list_n_basic_map(list);
9676 if (n < 0 || !bmap)
9677 return isl_basic_map_list_free(list);
9679 for (i = 0; i < n; ++i) {
9680 isl_basic_map *bmap_i;
9682 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9683 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9684 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9687 return list;
9690 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9691 __isl_take isl_map *map)
9693 isl_bool ok;
9695 isl_map_align_params_set(&map, &set);
9696 ok = isl_map_compatible_domain(map, set);
9697 if (ok < 0)
9698 goto error;
9699 if (!ok)
9700 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9701 "incompatible spaces", goto error);
9702 map = isl_map_intersect_domain(map, set);
9703 set = isl_map_range(map);
9704 return set;
9705 error:
9706 isl_set_free(set);
9707 isl_map_free(map);
9708 return NULL;
9711 /* There is no need to cow as removing empty parts doesn't change
9712 * the meaning of the set.
9714 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9716 int i;
9718 if (!map)
9719 return NULL;
9721 for (i = map->n - 1; i >= 0; --i)
9722 map = remove_if_empty(map, i);
9724 return map;
9727 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9729 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9732 /* Create a binary relation that maps the shared initial "pos" dimensions
9733 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9735 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9736 __isl_keep isl_basic_set *bset2, int pos)
9738 isl_basic_map *bmap1;
9739 isl_basic_map *bmap2;
9741 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9742 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9743 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9744 isl_dim_out, 0, pos);
9745 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9746 isl_dim_out, 0, pos);
9747 return isl_basic_map_range_product(bmap1, bmap2);
9750 /* Given two basic sets bset1 and bset2, compute the maximal difference
9751 * between the values of dimension pos in bset1 and those in bset2
9752 * for any common value of the parameters and dimensions preceding pos.
9754 static enum isl_lp_result basic_set_maximal_difference_at(
9755 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9756 int pos, isl_int *opt)
9758 isl_basic_map *bmap1;
9759 struct isl_ctx *ctx;
9760 struct isl_vec *obj;
9761 isl_size total;
9762 isl_size nparam;
9763 isl_size dim1;
9764 enum isl_lp_result res;
9766 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9767 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9768 if (nparam < 0 || dim1 < 0 || !bset2)
9769 return isl_lp_error;
9771 bmap1 = join_initial(bset1, bset2, pos);
9772 total = isl_basic_map_dim(bmap1, isl_dim_all);
9773 if (total < 0)
9774 return isl_lp_error;
9776 ctx = bmap1->ctx;
9777 obj = isl_vec_alloc(ctx, 1 + total);
9778 if (!obj)
9779 goto error;
9780 isl_seq_clr(obj->block.data, 1 + total);
9781 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9782 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9783 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9784 opt, NULL, NULL);
9785 isl_basic_map_free(bmap1);
9786 isl_vec_free(obj);
9787 return res;
9788 error:
9789 isl_basic_map_free(bmap1);
9790 return isl_lp_error;
9793 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9794 * for any common value of the parameters and dimensions preceding pos
9795 * in both basic sets, the values of dimension pos in bset1 are
9796 * smaller or larger than those in bset2.
9798 * Returns
9799 * 1 if bset1 follows bset2
9800 * -1 if bset1 precedes bset2
9801 * 0 if bset1 and bset2 are incomparable
9802 * -2 if some error occurred.
9804 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9805 __isl_keep isl_basic_set *bset2, int pos)
9807 isl_int opt;
9808 enum isl_lp_result res;
9809 int cmp;
9811 isl_int_init(opt);
9813 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9815 if (res == isl_lp_empty)
9816 cmp = 0;
9817 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9818 res == isl_lp_unbounded)
9819 cmp = 1;
9820 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9821 cmp = -1;
9822 else
9823 cmp = -2;
9825 isl_int_clear(opt);
9826 return cmp;
9829 /* Given two basic sets bset1 and bset2, check whether
9830 * for any common value of the parameters and dimensions preceding pos
9831 * there is a value of dimension pos in bset1 that is larger
9832 * than a value of the same dimension in bset2.
9834 * Return
9835 * 1 if there exists such a pair
9836 * 0 if there is no such pair, but there is a pair of equal values
9837 * -1 otherwise
9838 * -2 if some error occurred.
9840 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9841 __isl_keep isl_basic_set *bset2, int pos)
9843 isl_bool empty;
9844 isl_basic_map *bmap;
9845 isl_size dim1;
9847 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9848 if (dim1 < 0)
9849 return -2;
9850 bmap = join_initial(bset1, bset2, pos);
9851 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9852 isl_dim_out, dim1 - pos);
9853 empty = isl_basic_map_is_empty(bmap);
9854 if (empty < 0)
9855 goto error;
9856 if (empty) {
9857 isl_basic_map_free(bmap);
9858 return -1;
9860 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9861 isl_dim_out, dim1 - pos);
9862 empty = isl_basic_map_is_empty(bmap);
9863 if (empty < 0)
9864 goto error;
9865 isl_basic_map_free(bmap);
9866 if (empty)
9867 return 0;
9868 return 1;
9869 error:
9870 isl_basic_map_free(bmap);
9871 return -2;
9874 /* Given two sets set1 and set2, check whether
9875 * for any common value of the parameters and dimensions preceding pos
9876 * there is a value of dimension pos in set1 that is larger
9877 * than a value of the same dimension in set2.
9879 * Return
9880 * 1 if there exists such a pair
9881 * 0 if there is no such pair, but there is a pair of equal values
9882 * -1 otherwise
9883 * -2 if some error occurred.
9885 int isl_set_follows_at(__isl_keep isl_set *set1,
9886 __isl_keep isl_set *set2, int pos)
9888 int i, j;
9889 int follows = -1;
9891 if (!set1 || !set2)
9892 return -2;
9894 for (i = 0; i < set1->n; ++i)
9895 for (j = 0; j < set2->n; ++j) {
9896 int f;
9897 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9898 if (f == 1 || f == -2)
9899 return f;
9900 if (f > follows)
9901 follows = f;
9904 return follows;
9907 static isl_bool isl_basic_map_plain_has_fixed_var(
9908 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9910 int i;
9911 int d;
9912 isl_size total;
9914 total = isl_basic_map_dim(bmap, isl_dim_all);
9915 if (total < 0)
9916 return isl_bool_error;
9917 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9918 for (; d+1 > pos; --d)
9919 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9920 break;
9921 if (d != pos)
9922 continue;
9923 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9924 return isl_bool_false;
9925 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9926 return isl_bool_false;
9927 if (!isl_int_is_one(bmap->eq[i][1+d]))
9928 return isl_bool_false;
9929 if (val)
9930 isl_int_neg(*val, bmap->eq[i][0]);
9931 return isl_bool_true;
9933 return isl_bool_false;
9936 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9937 unsigned pos, isl_int *val)
9939 int i;
9940 isl_int v;
9941 isl_int tmp;
9942 isl_bool fixed;
9944 if (!map)
9945 return isl_bool_error;
9946 if (map->n == 0)
9947 return isl_bool_false;
9948 if (map->n == 1)
9949 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9950 isl_int_init(v);
9951 isl_int_init(tmp);
9952 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9953 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9954 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9955 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9956 fixed = isl_bool_false;
9958 if (val)
9959 isl_int_set(*val, v);
9960 isl_int_clear(tmp);
9961 isl_int_clear(v);
9962 return fixed;
9965 static isl_bool isl_basic_set_plain_has_fixed_var(
9966 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9968 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9969 pos, val);
9972 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9973 enum isl_dim_type type, unsigned pos, isl_int *val)
9975 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9976 return isl_bool_error;
9977 return isl_basic_map_plain_has_fixed_var(bmap,
9978 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9981 /* If "bmap" obviously lies on a hyperplane where the given dimension
9982 * has a fixed value, then return that value.
9983 * Otherwise return NaN.
9985 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9986 __isl_keep isl_basic_map *bmap,
9987 enum isl_dim_type type, unsigned pos)
9989 isl_ctx *ctx;
9990 isl_val *v;
9991 isl_bool fixed;
9993 if (!bmap)
9994 return NULL;
9995 ctx = isl_basic_map_get_ctx(bmap);
9996 v = isl_val_alloc(ctx);
9997 if (!v)
9998 return NULL;
9999 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10000 if (fixed < 0)
10001 return isl_val_free(v);
10002 if (fixed) {
10003 isl_int_set_si(v->d, 1);
10004 return v;
10006 isl_val_free(v);
10007 return isl_val_nan(ctx);
10010 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10011 enum isl_dim_type type, unsigned pos, isl_int *val)
10013 if (isl_map_check_range(map, type, pos, 1) < 0)
10014 return isl_bool_error;
10015 return isl_map_plain_has_fixed_var(map,
10016 map_offset(map, type) - 1 + pos, val);
10019 /* If "map" obviously lies on a hyperplane where the given dimension
10020 * has a fixed value, then return that value.
10021 * Otherwise return NaN.
10023 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10024 enum isl_dim_type type, unsigned pos)
10026 isl_ctx *ctx;
10027 isl_val *v;
10028 isl_bool fixed;
10030 if (!map)
10031 return NULL;
10032 ctx = isl_map_get_ctx(map);
10033 v = isl_val_alloc(ctx);
10034 if (!v)
10035 return NULL;
10036 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10037 if (fixed < 0)
10038 return isl_val_free(v);
10039 if (fixed) {
10040 isl_int_set_si(v->d, 1);
10041 return v;
10043 isl_val_free(v);
10044 return isl_val_nan(ctx);
10047 /* If "set" obviously lies on a hyperplane where the given dimension
10048 * has a fixed value, then return that value.
10049 * Otherwise return NaN.
10051 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10052 enum isl_dim_type type, unsigned pos)
10054 return isl_map_plain_get_val_if_fixed(set, type, pos);
10057 /* Return a sequence of values in the same space as "set"
10058 * that are equal to the corresponding set dimensions of "set"
10059 * for those set dimensions that obviously lie on a hyperplane
10060 * where the dimension has a fixed value.
10061 * The other elements are set to NaN.
10063 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10064 __isl_keep isl_set *set)
10066 int i;
10067 isl_size n;
10068 isl_space *space;
10069 isl_multi_val *mv;
10071 space = isl_space_drop_all_params(isl_set_get_space(set));
10072 mv = isl_multi_val_alloc(space);
10073 n = isl_multi_val_size(mv);
10074 if (n < 0)
10075 return isl_multi_val_free(mv);
10077 for (i = 0; i < n; ++i) {
10078 isl_val *v;
10080 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10081 mv = isl_multi_val_set_val(mv, i, v);
10084 return mv;
10087 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10088 * then return this fixed value in *val.
10090 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10091 unsigned dim, isl_int *val)
10093 isl_size nparam;
10095 nparam = isl_basic_set_dim(bset, isl_dim_param);
10096 if (nparam < 0)
10097 return isl_bool_error;
10098 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10101 /* Return -1 if the constraint "c1" should be sorted before "c2"
10102 * and 1 if it should be sorted after "c2".
10103 * Return 0 if the two constraints are the same (up to the constant term).
10105 * In particular, if a constraint involves later variables than another
10106 * then it is sorted after this other constraint.
10107 * uset_gist depends on constraints without existentially quantified
10108 * variables sorting first.
10110 * For constraints that have the same latest variable, those
10111 * with the same coefficient for this latest variable (first in absolute value
10112 * and then in actual value) are grouped together.
10113 * This is useful for detecting pairs of constraints that can
10114 * be chained in their printed representation.
10116 * Finally, within a group, constraints are sorted according to
10117 * their coefficients (excluding the constant term).
10119 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10121 isl_int **c1 = (isl_int **) p1;
10122 isl_int **c2 = (isl_int **) p2;
10123 int l1, l2;
10124 unsigned size = *(unsigned *) arg;
10125 int cmp;
10127 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10128 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10130 if (l1 != l2)
10131 return l1 - l2;
10133 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10134 if (cmp != 0)
10135 return cmp;
10136 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10137 if (cmp != 0)
10138 return -cmp;
10140 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10143 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10144 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10145 * and 0 if the two constraints are the same (up to the constant term).
10147 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10148 isl_int *c1, isl_int *c2)
10150 isl_size total;
10151 unsigned size;
10153 total = isl_basic_map_dim(bmap, isl_dim_all);
10154 if (total < 0)
10155 return -2;
10156 size = total;
10157 return sort_constraint_cmp(&c1, &c2, &size);
10160 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10161 __isl_take isl_basic_map *bmap)
10163 isl_size total;
10164 unsigned size;
10166 if (!bmap)
10167 return NULL;
10168 if (bmap->n_ineq == 0)
10169 return bmap;
10170 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10171 return bmap;
10172 total = isl_basic_map_dim(bmap, isl_dim_all);
10173 if (total < 0)
10174 return isl_basic_map_free(bmap);
10175 size = total;
10176 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10177 &sort_constraint_cmp, &size) < 0)
10178 return isl_basic_map_free(bmap);
10179 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10180 return bmap;
10183 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10184 __isl_take isl_basic_set *bset)
10186 isl_basic_map *bmap = bset_to_bmap(bset);
10187 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10190 __isl_give isl_basic_map *isl_basic_map_normalize(
10191 __isl_take isl_basic_map *bmap)
10193 bmap = isl_basic_map_remove_redundancies(bmap);
10194 bmap = isl_basic_map_sort_constraints(bmap);
10195 return bmap;
10197 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10198 __isl_keep isl_basic_map *bmap2)
10200 int i, cmp;
10201 isl_size total;
10202 isl_space *space1, *space2;
10204 if (!bmap1 || !bmap2)
10205 return -1;
10207 if (bmap1 == bmap2)
10208 return 0;
10209 space1 = isl_basic_map_peek_space(bmap1);
10210 space2 = isl_basic_map_peek_space(bmap2);
10211 cmp = isl_space_cmp(space1, space2);
10212 if (cmp)
10213 return cmp;
10214 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10215 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10216 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10217 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10218 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10219 return 0;
10220 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10221 return 1;
10222 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10223 return -1;
10224 if (bmap1->n_eq != bmap2->n_eq)
10225 return bmap1->n_eq - bmap2->n_eq;
10226 if (bmap1->n_ineq != bmap2->n_ineq)
10227 return bmap1->n_ineq - bmap2->n_ineq;
10228 if (bmap1->n_div != bmap2->n_div)
10229 return bmap1->n_div - bmap2->n_div;
10230 total = isl_basic_map_dim(bmap1, isl_dim_all);
10231 if (total < 0)
10232 return -1;
10233 for (i = 0; i < bmap1->n_eq; ++i) {
10234 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10235 if (cmp)
10236 return cmp;
10238 for (i = 0; i < bmap1->n_ineq; ++i) {
10239 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10240 if (cmp)
10241 return cmp;
10243 for (i = 0; i < bmap1->n_div; ++i) {
10244 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10245 if (cmp)
10246 return cmp;
10248 return 0;
10251 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10252 __isl_keep isl_basic_set *bset2)
10254 return isl_basic_map_plain_cmp(bset1, bset2);
10257 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10259 int i, cmp;
10261 if (set1 == set2)
10262 return 0;
10263 if (set1->n != set2->n)
10264 return set1->n - set2->n;
10266 for (i = 0; i < set1->n; ++i) {
10267 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10268 if (cmp)
10269 return cmp;
10272 return 0;
10275 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10276 __isl_keep isl_basic_map *bmap2)
10278 if (!bmap1 || !bmap2)
10279 return isl_bool_error;
10280 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10283 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10284 __isl_keep isl_basic_set *bset2)
10286 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10287 bset_to_bmap(bset2));
10290 static int qsort_bmap_cmp(const void *p1, const void *p2)
10292 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10293 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10295 return isl_basic_map_plain_cmp(bmap1, bmap2);
10298 /* Sort the basic maps of "map" and remove duplicate basic maps.
10300 * While removing basic maps, we make sure that the basic maps remain
10301 * sorted because isl_map_normalize expects the basic maps of the result
10302 * to be sorted.
10304 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10306 int i, j;
10308 map = isl_map_remove_empty_parts(map);
10309 if (!map)
10310 return NULL;
10311 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10312 for (i = map->n - 1; i >= 1; --i) {
10313 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10314 continue;
10315 isl_basic_map_free(map->p[i-1]);
10316 for (j = i; j < map->n; ++j)
10317 map->p[j - 1] = map->p[j];
10318 map->n--;
10321 return map;
10324 /* Remove obvious duplicates among the basic maps of "map".
10326 * Unlike isl_map_normalize, this function does not remove redundant
10327 * constraints and only removes duplicates that have exactly the same
10328 * constraints in the input. It does sort the constraints and
10329 * the basic maps to ease the detection of duplicates.
10331 * If "map" has already been normalized or if the basic maps are
10332 * disjoint, then there can be no duplicates.
10334 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10336 int i;
10337 isl_basic_map *bmap;
10339 if (!map)
10340 return NULL;
10341 if (map->n <= 1)
10342 return map;
10343 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10344 return map;
10345 for (i = 0; i < map->n; ++i) {
10346 bmap = isl_basic_map_copy(map->p[i]);
10347 bmap = isl_basic_map_sort_constraints(bmap);
10348 if (!bmap)
10349 return isl_map_free(map);
10350 isl_basic_map_free(map->p[i]);
10351 map->p[i] = bmap;
10354 map = sort_and_remove_duplicates(map);
10355 return map;
10358 /* We normalize in place, but if anything goes wrong we need
10359 * to return NULL, so we need to make sure we don't change the
10360 * meaning of any possible other copies of map.
10362 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10364 int i;
10365 struct isl_basic_map *bmap;
10367 if (!map)
10368 return NULL;
10369 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10370 return map;
10371 for (i = 0; i < map->n; ++i) {
10372 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10373 if (!bmap)
10374 goto error;
10375 isl_basic_map_free(map->p[i]);
10376 map->p[i] = bmap;
10379 map = sort_and_remove_duplicates(map);
10380 if (map)
10381 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10382 return map;
10383 error:
10384 isl_map_free(map);
10385 return NULL;
10388 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10390 return set_from_map(isl_map_normalize(set_to_map(set)));
10393 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10394 __isl_keep isl_map *map2)
10396 int i;
10397 isl_bool equal;
10399 if (!map1 || !map2)
10400 return isl_bool_error;
10402 if (map1 == map2)
10403 return isl_bool_true;
10404 equal = isl_map_has_equal_space(map1, map2);
10405 if (equal < 0 || !equal)
10406 return equal;
10408 map1 = isl_map_copy(map1);
10409 map2 = isl_map_copy(map2);
10410 map1 = isl_map_normalize(map1);
10411 map2 = isl_map_normalize(map2);
10412 if (!map1 || !map2)
10413 goto error;
10414 equal = map1->n == map2->n;
10415 for (i = 0; equal && i < map1->n; ++i) {
10416 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10417 if (equal < 0)
10418 goto error;
10420 isl_map_free(map1);
10421 isl_map_free(map2);
10422 return equal;
10423 error:
10424 isl_map_free(map1);
10425 isl_map_free(map2);
10426 return isl_bool_error;
10429 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10430 __isl_keep isl_set *set2)
10432 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10435 /* Return the basic maps in "map" as a list.
10437 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10438 __isl_keep isl_map *map)
10440 int i;
10441 isl_ctx *ctx;
10442 isl_basic_map_list *list;
10444 if (!map)
10445 return NULL;
10446 ctx = isl_map_get_ctx(map);
10447 list = isl_basic_map_list_alloc(ctx, map->n);
10449 for (i = 0; i < map->n; ++i) {
10450 isl_basic_map *bmap;
10452 bmap = isl_basic_map_copy(map->p[i]);
10453 list = isl_basic_map_list_add(list, bmap);
10456 return list;
10459 /* Return the intersection of the elements in the non-empty list "list".
10460 * All elements are assumed to live in the same space.
10462 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10463 __isl_take isl_basic_map_list *list)
10465 int i;
10466 isl_size n;
10467 isl_basic_map *bmap;
10469 n = isl_basic_map_list_n_basic_map(list);
10470 if (n < 0)
10471 goto error;
10472 if (n < 1)
10473 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10474 "expecting non-empty list", goto error);
10476 bmap = isl_basic_map_list_get_basic_map(list, 0);
10477 for (i = 1; i < n; ++i) {
10478 isl_basic_map *bmap_i;
10480 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10481 bmap = isl_basic_map_intersect(bmap, bmap_i);
10484 isl_basic_map_list_free(list);
10485 return bmap;
10486 error:
10487 isl_basic_map_list_free(list);
10488 return NULL;
10491 /* Return the intersection of the elements in the non-empty list "list".
10492 * All elements are assumed to live in the same space.
10494 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10495 __isl_take isl_basic_set_list *list)
10497 return isl_basic_map_list_intersect(list);
10500 /* Return the union of the elements of "list".
10501 * The list is required to have at least one element.
10503 __isl_give isl_set *isl_basic_set_list_union(
10504 __isl_take isl_basic_set_list *list)
10506 int i;
10507 isl_size n;
10508 isl_space *space;
10509 isl_basic_set *bset;
10510 isl_set *set;
10512 n = isl_basic_set_list_n_basic_set(list);
10513 if (n < 0)
10514 goto error;
10515 if (n < 1)
10516 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10517 "expecting non-empty list", goto error);
10519 bset = isl_basic_set_list_get_basic_set(list, 0);
10520 space = isl_basic_set_get_space(bset);
10521 isl_basic_set_free(bset);
10523 set = isl_set_alloc_space(space, n, 0);
10524 for (i = 0; i < n; ++i) {
10525 bset = isl_basic_set_list_get_basic_set(list, i);
10526 set = isl_set_add_basic_set(set, bset);
10529 isl_basic_set_list_free(list);
10530 return set;
10531 error:
10532 isl_basic_set_list_free(list);
10533 return NULL;
10536 /* Return the union of the elements in the non-empty list "list".
10537 * All elements are assumed to live in the same space.
10539 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10541 int i;
10542 isl_size n;
10543 isl_set *set;
10545 n = isl_set_list_n_set(list);
10546 if (n < 0)
10547 goto error;
10548 if (n < 1)
10549 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10550 "expecting non-empty list", goto error);
10552 set = isl_set_list_get_set(list, 0);
10553 for (i = 1; i < n; ++i) {
10554 isl_set *set_i;
10556 set_i = isl_set_list_get_set(list, i);
10557 set = isl_set_union(set, set_i);
10560 isl_set_list_free(list);
10561 return set;
10562 error:
10563 isl_set_list_free(list);
10564 return NULL;
10567 __isl_give isl_basic_map *isl_basic_map_product(
10568 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10570 isl_space *space_result = NULL;
10571 struct isl_basic_map *bmap;
10572 unsigned in1, in2, out1, out2, nparam, total, pos;
10573 struct isl_dim_map *dim_map1, *dim_map2;
10575 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10576 goto error;
10577 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10578 isl_space_copy(bmap2->dim));
10580 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10581 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10582 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10583 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10584 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10586 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10587 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10588 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10589 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10590 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10591 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10592 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10593 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10594 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10595 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10596 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10598 bmap = isl_basic_map_alloc_space(space_result,
10599 bmap1->n_div + bmap2->n_div,
10600 bmap1->n_eq + bmap2->n_eq,
10601 bmap1->n_ineq + bmap2->n_ineq);
10602 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10603 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10604 bmap = isl_basic_map_simplify(bmap);
10605 return isl_basic_map_finalize(bmap);
10606 error:
10607 isl_basic_map_free(bmap1);
10608 isl_basic_map_free(bmap2);
10609 return NULL;
10612 __isl_give isl_basic_map *isl_basic_map_flat_product(
10613 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10615 isl_basic_map *prod;
10617 prod = isl_basic_map_product(bmap1, bmap2);
10618 prod = isl_basic_map_flatten(prod);
10619 return prod;
10622 __isl_give isl_basic_set *isl_basic_set_flat_product(
10623 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10625 return isl_basic_map_flat_range_product(bset1, bset2);
10628 __isl_give isl_basic_map *isl_basic_map_domain_product(
10629 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10631 isl_space *space1, *space2;
10632 isl_space *space_result = NULL;
10633 isl_basic_map *bmap;
10634 isl_size in1, in2, out, nparam;
10635 unsigned total, pos;
10636 struct isl_dim_map *dim_map1, *dim_map2;
10638 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10639 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10640 out = isl_basic_map_dim(bmap1, isl_dim_out);
10641 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10642 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10643 goto error;
10645 space1 = isl_basic_map_get_space(bmap1);
10646 space2 = isl_basic_map_get_space(bmap2);
10647 space_result = isl_space_domain_product(space1, space2);
10649 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10650 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10651 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10652 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10653 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10654 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10655 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10656 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10657 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10658 isl_dim_map_div(dim_map1, bmap1, pos += out);
10659 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10661 bmap = isl_basic_map_alloc_space(space_result,
10662 bmap1->n_div + bmap2->n_div,
10663 bmap1->n_eq + bmap2->n_eq,
10664 bmap1->n_ineq + bmap2->n_ineq);
10665 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10666 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10667 bmap = isl_basic_map_simplify(bmap);
10668 return isl_basic_map_finalize(bmap);
10669 error:
10670 isl_basic_map_free(bmap1);
10671 isl_basic_map_free(bmap2);
10672 return NULL;
10675 __isl_give isl_basic_map *isl_basic_map_range_product(
10676 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10678 isl_bool rational;
10679 isl_space *space_result = NULL;
10680 isl_basic_map *bmap;
10681 isl_size in, out1, out2, nparam;
10682 unsigned total, pos;
10683 struct isl_dim_map *dim_map1, *dim_map2;
10685 rational = isl_basic_map_is_rational(bmap1);
10686 if (rational >= 0 && rational)
10687 rational = isl_basic_map_is_rational(bmap2);
10688 in = isl_basic_map_dim(bmap1, isl_dim_in);
10689 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10690 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10691 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10692 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10693 goto error;
10695 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10696 goto error;
10698 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10699 isl_space_copy(bmap2->dim));
10701 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10702 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10703 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10704 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10705 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10706 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10707 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10708 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10709 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10710 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10711 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10713 bmap = isl_basic_map_alloc_space(space_result,
10714 bmap1->n_div + bmap2->n_div,
10715 bmap1->n_eq + bmap2->n_eq,
10716 bmap1->n_ineq + bmap2->n_ineq);
10717 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10718 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10719 if (rational)
10720 bmap = isl_basic_map_set_rational(bmap);
10721 bmap = isl_basic_map_simplify(bmap);
10722 return isl_basic_map_finalize(bmap);
10723 error:
10724 isl_basic_map_free(bmap1);
10725 isl_basic_map_free(bmap2);
10726 return NULL;
10729 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10730 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10732 isl_basic_map *prod;
10734 prod = isl_basic_map_range_product(bmap1, bmap2);
10735 prod = isl_basic_map_flatten_range(prod);
10736 return prod;
10739 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10740 * and collect the results.
10741 * The result live in the space obtained by calling "space_product"
10742 * on the spaces of "map1" and "map2".
10743 * If "remove_duplicates" is set then the result may contain duplicates
10744 * (even if the inputs do not) and so we try and remove the obvious
10745 * duplicates.
10747 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10748 __isl_take isl_map *map2,
10749 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10750 __isl_take isl_space *right),
10751 __isl_give isl_basic_map *(*basic_map_product)(
10752 __isl_take isl_basic_map *left,
10753 __isl_take isl_basic_map *right),
10754 int remove_duplicates)
10756 unsigned flags = 0;
10757 struct isl_map *result;
10758 int i, j;
10759 isl_bool m;
10761 m = isl_map_has_equal_params(map1, map2);
10762 if (m < 0)
10763 goto error;
10764 if (!m)
10765 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10766 "parameters don't match", goto error);
10768 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10769 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10770 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10772 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10773 isl_space_copy(map2->dim)),
10774 map1->n * map2->n, flags);
10775 if (!result)
10776 goto error;
10777 for (i = 0; i < map1->n; ++i)
10778 for (j = 0; j < map2->n; ++j) {
10779 struct isl_basic_map *part;
10780 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10781 isl_basic_map_copy(map2->p[j]));
10782 if (isl_basic_map_is_empty(part))
10783 isl_basic_map_free(part);
10784 else
10785 result = isl_map_add_basic_map(result, part);
10786 if (!result)
10787 goto error;
10789 if (remove_duplicates)
10790 result = isl_map_remove_obvious_duplicates(result);
10791 isl_map_free(map1);
10792 isl_map_free(map2);
10793 return result;
10794 error:
10795 isl_map_free(map1);
10796 isl_map_free(map2);
10797 return NULL;
10800 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10802 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10803 __isl_take isl_map *map2)
10805 isl_map_align_params_bin(&map1, &map2);
10806 return map_product(map1, map2, &isl_space_product,
10807 &isl_basic_map_product, 0);
10810 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10812 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10813 __isl_take isl_map *map2)
10815 isl_map *prod;
10817 prod = isl_map_product(map1, map2);
10818 prod = isl_map_flatten(prod);
10819 return prod;
10822 /* Given two set A and B, construct its Cartesian product A x B.
10824 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10825 __isl_take isl_set *set2)
10827 return isl_map_range_product(set1, set2);
10830 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10831 __isl_take isl_set *set2)
10833 return isl_map_flat_range_product(set1, set2);
10836 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10838 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10839 __isl_take isl_map *map2)
10841 isl_map_align_params_bin(&map1, &map2);
10842 return map_product(map1, map2, &isl_space_domain_product,
10843 &isl_basic_map_domain_product, 1);
10846 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10848 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10849 __isl_take isl_map *map2)
10851 isl_map_align_params_bin(&map1, &map2);
10852 return map_product(map1, map2, &isl_space_range_product,
10853 &isl_basic_map_range_product, 1);
10856 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10858 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10860 isl_space *space;
10861 isl_size total1, keep1, total2, keep2;
10863 total1 = isl_map_dim(map, isl_dim_in);
10864 total2 = isl_map_dim(map, isl_dim_out);
10865 if (total1 < 0 || total2 < 0)
10866 return isl_map_free(map);
10867 if (!isl_space_domain_is_wrapping(map->dim) ||
10868 !isl_space_range_is_wrapping(map->dim))
10869 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10870 "not a product", return isl_map_free(map));
10872 space = isl_map_get_space(map);
10873 space = isl_space_factor_domain(space);
10874 keep1 = isl_space_dim(space, isl_dim_in);
10875 keep2 = isl_space_dim(space, isl_dim_out);
10876 if (keep1 < 0 || keep2 < 0)
10877 map = isl_map_free(map);
10878 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10879 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10880 map = isl_map_reset_space(map, space);
10882 return map;
10885 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10887 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10889 isl_space *space;
10890 isl_size total1, keep1, total2, keep2;
10892 total1 = isl_map_dim(map, isl_dim_in);
10893 total2 = isl_map_dim(map, isl_dim_out);
10894 if (total1 < 0 || total2 < 0)
10895 return isl_map_free(map);
10896 if (!isl_space_domain_is_wrapping(map->dim) ||
10897 !isl_space_range_is_wrapping(map->dim))
10898 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10899 "not a product", return isl_map_free(map));
10901 space = isl_map_get_space(map);
10902 space = isl_space_factor_range(space);
10903 keep1 = isl_space_dim(space, isl_dim_in);
10904 keep2 = isl_space_dim(space, isl_dim_out);
10905 if (keep1 < 0 || keep2 < 0)
10906 map = isl_map_free(map);
10907 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10908 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10909 map = isl_map_reset_space(map, space);
10911 return map;
10914 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10916 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10918 isl_space *space;
10919 isl_size total, keep;
10921 total = isl_map_dim(map, isl_dim_in);
10922 if (total < 0)
10923 return isl_map_free(map);
10924 if (!isl_space_domain_is_wrapping(map->dim))
10925 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10926 "domain is not a product", return isl_map_free(map));
10928 space = isl_map_get_space(map);
10929 space = isl_space_domain_factor_domain(space);
10930 keep = isl_space_dim(space, isl_dim_in);
10931 if (keep < 0)
10932 map = isl_map_free(map);
10933 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10934 map = isl_map_reset_space(map, space);
10936 return map;
10939 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10941 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10943 isl_space *space;
10944 isl_size total, keep;
10946 total = isl_map_dim(map, isl_dim_in);
10947 if (total < 0)
10948 return isl_map_free(map);
10949 if (!isl_space_domain_is_wrapping(map->dim))
10950 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10951 "domain is not a product", return isl_map_free(map));
10953 space = isl_map_get_space(map);
10954 space = isl_space_domain_factor_range(space);
10955 keep = isl_space_dim(space, isl_dim_in);
10956 if (keep < 0)
10957 map = isl_map_free(map);
10958 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10959 map = isl_map_reset_space(map, space);
10961 return map;
10964 /* Given a map A -> [B -> C], extract the map A -> B.
10966 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10968 isl_space *space;
10969 isl_size total, keep;
10971 total = isl_map_dim(map, isl_dim_out);
10972 if (total < 0)
10973 return isl_map_free(map);
10974 if (!isl_space_range_is_wrapping(map->dim))
10975 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10976 "range is not a product", return isl_map_free(map));
10978 space = isl_map_get_space(map);
10979 space = isl_space_range_factor_domain(space);
10980 keep = isl_space_dim(space, isl_dim_out);
10981 if (keep < 0)
10982 map = isl_map_free(map);
10983 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10984 map = isl_map_reset_space(map, space);
10986 return map;
10989 /* Given a map A -> [B -> C], extract the map A -> C.
10991 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10993 isl_space *space;
10994 isl_size total, keep;
10996 total = isl_map_dim(map, isl_dim_out);
10997 if (total < 0)
10998 return isl_map_free(map);
10999 if (!isl_space_range_is_wrapping(map->dim))
11000 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11001 "range is not a product", return isl_map_free(map));
11003 space = isl_map_get_space(map);
11004 space = isl_space_range_factor_range(space);
11005 keep = isl_space_dim(space, isl_dim_out);
11006 if (keep < 0)
11007 map = isl_map_free(map);
11008 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11009 map = isl_map_reset_space(map, space);
11011 return map;
11014 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11016 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11017 __isl_take isl_map *map2)
11019 isl_map *prod;
11021 prod = isl_map_domain_product(map1, map2);
11022 prod = isl_map_flatten_domain(prod);
11023 return prod;
11026 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11028 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11029 __isl_take isl_map *map2)
11031 isl_map *prod;
11033 prod = isl_map_range_product(map1, map2);
11034 prod = isl_map_flatten_range(prod);
11035 return prod;
11038 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11040 int i;
11041 uint32_t hash = isl_hash_init();
11042 isl_size total;
11044 if (!bmap)
11045 return 0;
11046 bmap = isl_basic_map_copy(bmap);
11047 bmap = isl_basic_map_normalize(bmap);
11048 total = isl_basic_map_dim(bmap, isl_dim_all);
11049 if (total < 0)
11050 return 0;
11051 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11052 for (i = 0; i < bmap->n_eq; ++i) {
11053 uint32_t c_hash;
11054 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11055 isl_hash_hash(hash, c_hash);
11057 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11058 for (i = 0; i < bmap->n_ineq; ++i) {
11059 uint32_t c_hash;
11060 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11061 isl_hash_hash(hash, c_hash);
11063 isl_hash_byte(hash, bmap->n_div & 0xFF);
11064 for (i = 0; i < bmap->n_div; ++i) {
11065 uint32_t c_hash;
11066 if (isl_int_is_zero(bmap->div[i][0]))
11067 continue;
11068 isl_hash_byte(hash, i & 0xFF);
11069 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11070 isl_hash_hash(hash, c_hash);
11072 isl_basic_map_free(bmap);
11073 return hash;
11076 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11078 return isl_basic_map_get_hash(bset_to_bmap(bset));
11081 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11083 int i;
11084 uint32_t hash;
11086 if (!map)
11087 return 0;
11088 map = isl_map_copy(map);
11089 map = isl_map_normalize(map);
11090 if (!map)
11091 return 0;
11093 hash = isl_hash_init();
11094 for (i = 0; i < map->n; ++i) {
11095 uint32_t bmap_hash;
11096 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11097 isl_hash_hash(hash, bmap_hash);
11100 isl_map_free(map);
11102 return hash;
11105 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11107 return isl_map_get_hash(set_to_map(set));
11110 /* Return the number of basic maps in the (current) representation of "map".
11112 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11114 return map ? map->n : isl_size_error;
11117 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11119 return set ? set->n : isl_size_error;
11122 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11123 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11125 int i;
11127 if (!map)
11128 return isl_stat_error;
11130 for (i = 0; i < map->n; ++i)
11131 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11132 return isl_stat_error;
11134 return isl_stat_ok;
11137 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11138 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11140 int i;
11142 if (!set)
11143 return isl_stat_error;
11145 for (i = 0; i < set->n; ++i)
11146 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11147 return isl_stat_error;
11149 return isl_stat_ok;
11152 /* Does "test" succeed on every basic set in "set"?
11154 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11155 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11156 void *user)
11158 int i;
11160 if (!set)
11161 return isl_bool_error;
11163 for (i = 0; i < set->n; ++i) {
11164 isl_bool r;
11166 r = test(set->p[i], user);
11167 if (r < 0 || !r)
11168 return r;
11171 return isl_bool_true;
11174 /* Return a list of basic sets, the union of which is equal to "set".
11176 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11177 __isl_keep isl_set *set)
11179 int i;
11180 isl_basic_set_list *list;
11182 if (!set)
11183 return NULL;
11185 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11186 for (i = 0; i < set->n; ++i) {
11187 isl_basic_set *bset;
11189 bset = isl_basic_set_copy(set->p[i]);
11190 list = isl_basic_set_list_add(list, bset);
11193 return list;
11196 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11198 isl_space *space;
11200 if (!bset)
11201 return NULL;
11203 bset = isl_basic_set_cow(bset);
11204 if (!bset)
11205 return NULL;
11207 space = isl_basic_set_get_space(bset);
11208 space = isl_space_lift(space, bset->n_div);
11209 if (!space)
11210 goto error;
11211 isl_space_free(bset->dim);
11212 bset->dim = space;
11213 bset->extra -= bset->n_div;
11214 bset->n_div = 0;
11216 bset = isl_basic_set_finalize(bset);
11218 return bset;
11219 error:
11220 isl_basic_set_free(bset);
11221 return NULL;
11224 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11226 int i;
11227 isl_space *space;
11228 unsigned n_div;
11230 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11232 if (!set)
11233 return NULL;
11235 set = isl_set_cow(set);
11236 if (!set)
11237 return NULL;
11239 n_div = set->p[0]->n_div;
11240 space = isl_set_get_space(set);
11241 space = isl_space_lift(space, n_div);
11242 if (!space)
11243 goto error;
11244 isl_space_free(set->dim);
11245 set->dim = space;
11247 for (i = 0; i < set->n; ++i) {
11248 set->p[i] = isl_basic_set_lift(set->p[i]);
11249 if (!set->p[i])
11250 goto error;
11253 return set;
11254 error:
11255 isl_set_free(set);
11256 return NULL;
11259 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11261 isl_size dim;
11262 int size = 0;
11264 dim = isl_basic_set_dim(bset, isl_dim_all);
11265 if (dim < 0)
11266 return -1;
11267 size += bset->n_eq * (1 + dim);
11268 size += bset->n_ineq * (1 + dim);
11269 size += bset->n_div * (2 + dim);
11271 return size;
11274 int isl_set_size(__isl_keep isl_set *set)
11276 int i;
11277 int size = 0;
11279 if (!set)
11280 return -1;
11282 for (i = 0; i < set->n; ++i)
11283 size += isl_basic_set_size(set->p[i]);
11285 return size;
11288 /* Check if there is any lower bound (if lower == 0) and/or upper
11289 * bound (if upper == 0) on the specified dim.
11291 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11292 enum isl_dim_type type, unsigned pos, int lower, int upper)
11294 int i;
11296 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11297 return isl_bool_error;
11299 pos += isl_basic_map_offset(bmap, type);
11301 for (i = 0; i < bmap->n_div; ++i) {
11302 if (isl_int_is_zero(bmap->div[i][0]))
11303 continue;
11304 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11305 return isl_bool_true;
11308 for (i = 0; i < bmap->n_eq; ++i)
11309 if (!isl_int_is_zero(bmap->eq[i][pos]))
11310 return isl_bool_true;
11312 for (i = 0; i < bmap->n_ineq; ++i) {
11313 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11314 if (sgn > 0)
11315 lower = 1;
11316 if (sgn < 0)
11317 upper = 1;
11320 return lower && upper;
11323 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11324 enum isl_dim_type type, unsigned pos)
11326 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11329 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11330 enum isl_dim_type type, unsigned pos)
11332 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11335 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11336 enum isl_dim_type type, unsigned pos)
11338 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11341 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11342 enum isl_dim_type type, unsigned pos)
11344 int i;
11346 if (!map)
11347 return isl_bool_error;
11349 for (i = 0; i < map->n; ++i) {
11350 isl_bool bounded;
11351 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11352 if (bounded < 0 || !bounded)
11353 return bounded;
11356 return isl_bool_true;
11359 /* Return true if the specified dim is involved in both an upper bound
11360 * and a lower bound.
11362 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11363 enum isl_dim_type type, unsigned pos)
11365 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11368 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11370 static isl_bool has_any_bound(__isl_keep isl_map *map,
11371 enum isl_dim_type type, unsigned pos,
11372 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11373 enum isl_dim_type type, unsigned pos))
11375 int i;
11377 if (!map)
11378 return isl_bool_error;
11380 for (i = 0; i < map->n; ++i) {
11381 isl_bool bounded;
11382 bounded = fn(map->p[i], type, pos);
11383 if (bounded < 0 || bounded)
11384 return bounded;
11387 return isl_bool_false;
11390 /* Return 1 if the specified dim is involved in any lower bound.
11392 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11393 enum isl_dim_type type, unsigned pos)
11395 return has_any_bound(set, type, pos,
11396 &isl_basic_map_dim_has_lower_bound);
11399 /* Return 1 if the specified dim is involved in any upper bound.
11401 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11402 enum isl_dim_type type, unsigned pos)
11404 return has_any_bound(set, type, pos,
11405 &isl_basic_map_dim_has_upper_bound);
11408 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11410 static isl_bool has_bound(__isl_keep isl_map *map,
11411 enum isl_dim_type type, unsigned pos,
11412 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11413 enum isl_dim_type type, unsigned pos))
11415 int i;
11417 if (!map)
11418 return isl_bool_error;
11420 for (i = 0; i < map->n; ++i) {
11421 isl_bool bounded;
11422 bounded = fn(map->p[i], type, pos);
11423 if (bounded < 0 || !bounded)
11424 return bounded;
11427 return isl_bool_true;
11430 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11432 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11433 enum isl_dim_type type, unsigned pos)
11435 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11438 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11440 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11441 enum isl_dim_type type, unsigned pos)
11443 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11446 /* For each of the "n" variables starting at "first", determine
11447 * the sign of the variable and put the results in the first "n"
11448 * elements of the array "signs".
11449 * Sign
11450 * 1 means that the variable is non-negative
11451 * -1 means that the variable is non-positive
11452 * 0 means the variable attains both positive and negative values.
11454 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11455 unsigned first, unsigned n, int *signs)
11457 isl_vec *bound = NULL;
11458 struct isl_tab *tab = NULL;
11459 struct isl_tab_undo *snap;
11460 int i;
11461 isl_size total;
11463 total = isl_basic_set_dim(bset, isl_dim_all);
11464 if (total < 0 || !signs)
11465 return isl_stat_error;
11467 bound = isl_vec_alloc(bset->ctx, 1 + total);
11468 tab = isl_tab_from_basic_set(bset, 0);
11469 if (!bound || !tab)
11470 goto error;
11472 isl_seq_clr(bound->el, bound->size);
11473 isl_int_set_si(bound->el[0], -1);
11475 snap = isl_tab_snap(tab);
11476 for (i = 0; i < n; ++i) {
11477 int empty;
11479 isl_int_set_si(bound->el[1 + first + i], -1);
11480 if (isl_tab_add_ineq(tab, bound->el) < 0)
11481 goto error;
11482 empty = tab->empty;
11483 isl_int_set_si(bound->el[1 + first + i], 0);
11484 if (isl_tab_rollback(tab, snap) < 0)
11485 goto error;
11487 if (empty) {
11488 signs[i] = 1;
11489 continue;
11492 isl_int_set_si(bound->el[1 + first + i], 1);
11493 if (isl_tab_add_ineq(tab, bound->el) < 0)
11494 goto error;
11495 empty = tab->empty;
11496 isl_int_set_si(bound->el[1 + first + i], 0);
11497 if (isl_tab_rollback(tab, snap) < 0)
11498 goto error;
11500 signs[i] = empty ? -1 : 0;
11503 isl_tab_free(tab);
11504 isl_vec_free(bound);
11505 return isl_stat_ok;
11506 error:
11507 isl_tab_free(tab);
11508 isl_vec_free(bound);
11509 return isl_stat_error;
11512 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11513 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11515 if (!bset || !signs)
11516 return isl_stat_error;
11517 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11518 return isl_stat_error;
11520 first += pos(bset->dim, type) - 1;
11521 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11524 /* Is it possible for the integer division "div" to depend (possibly
11525 * indirectly) on any output dimensions?
11527 * If the div is undefined, then we conservatively assume that it
11528 * may depend on them.
11529 * Otherwise, we check if it actually depends on them or on any integer
11530 * divisions that may depend on them.
11532 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11534 int i;
11535 isl_size n_out, n_div;
11536 unsigned o_out, o_div;
11538 if (isl_int_is_zero(bmap->div[div][0]))
11539 return isl_bool_true;
11541 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11542 if (n_out < 0)
11543 return isl_bool_error;
11544 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11546 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11547 return isl_bool_true;
11549 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11550 if (n_div < 0)
11551 return isl_bool_error;
11552 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11554 for (i = 0; i < n_div; ++i) {
11555 isl_bool may_involve;
11557 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11558 continue;
11559 may_involve = div_may_involve_output(bmap, i);
11560 if (may_involve < 0 || may_involve)
11561 return may_involve;
11564 return isl_bool_false;
11567 /* Return the first integer division of "bmap" in the range
11568 * [first, first + n[ that may depend on any output dimensions and
11569 * that has a non-zero coefficient in "c" (where the first coefficient
11570 * in "c" corresponds to integer division "first").
11572 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11573 isl_int *c, int first, int n)
11575 int k;
11577 if (!bmap)
11578 return -1;
11580 for (k = first; k < first + n; ++k) {
11581 isl_bool may_involve;
11583 if (isl_int_is_zero(c[k]))
11584 continue;
11585 may_involve = div_may_involve_output(bmap, k);
11586 if (may_involve < 0)
11587 return -1;
11588 if (may_involve)
11589 return k;
11592 return first + n;
11595 /* Look for a pair of inequality constraints in "bmap" of the form
11597 * -l + i >= 0 or i >= l
11598 * and
11599 * n + l - i >= 0 or i <= l + n
11601 * with n < "m" and i the output dimension at position "pos".
11602 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11603 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11604 * and earlier output dimensions, as well as integer divisions that do
11605 * not involve any of the output dimensions.
11607 * Return the index of the first inequality constraint or bmap->n_ineq
11608 * if no such pair can be found.
11610 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11611 int pos, isl_int m)
11613 int i, j;
11614 isl_ctx *ctx;
11615 isl_size total;
11616 isl_size n_div, n_out;
11617 unsigned o_div, o_out;
11618 int less;
11620 total = isl_basic_map_dim(bmap, isl_dim_all);
11621 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11622 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11623 if (total < 0 || n_out < 0 || n_div < 0)
11624 return -1;
11626 ctx = isl_basic_map_get_ctx(bmap);
11627 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11628 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11629 for (i = 0; i < bmap->n_ineq; ++i) {
11630 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11631 continue;
11632 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11633 n_out - (pos + 1)) != -1)
11634 continue;
11635 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11636 0, n_div) < n_div)
11637 continue;
11638 for (j = i + 1; j < bmap->n_ineq; ++j) {
11639 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11640 ctx->one))
11641 continue;
11642 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11643 bmap->ineq[j] + 1, total))
11644 continue;
11645 break;
11647 if (j >= bmap->n_ineq)
11648 continue;
11649 isl_int_add(bmap->ineq[i][0],
11650 bmap->ineq[i][0], bmap->ineq[j][0]);
11651 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11652 isl_int_sub(bmap->ineq[i][0],
11653 bmap->ineq[i][0], bmap->ineq[j][0]);
11654 if (!less)
11655 continue;
11656 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11657 return i;
11658 else
11659 return j;
11662 return bmap->n_ineq;
11665 /* Return the index of the equality of "bmap" that defines
11666 * the output dimension "pos" in terms of earlier dimensions.
11667 * The equality may also involve integer divisions, as long
11668 * as those integer divisions are defined in terms of
11669 * parameters or input dimensions.
11670 * In this case, *div is set to the number of integer divisions and
11671 * *ineq is set to the number of inequality constraints (provided
11672 * div and ineq are not NULL).
11674 * The equality may also involve a single integer division involving
11675 * the output dimensions (typically only output dimension "pos") as
11676 * long as the coefficient of output dimension "pos" is 1 or -1 and
11677 * there is a pair of constraints i >= l and i <= l + n, with i referring
11678 * to output dimension "pos", l an expression involving only earlier
11679 * dimensions and n smaller than the coefficient of the integer division
11680 * in the equality. In this case, the output dimension can be defined
11681 * in terms of a modulo expression that does not involve the integer division.
11682 * *div is then set to this single integer division and
11683 * *ineq is set to the index of constraint i >= l.
11685 * Return bmap->n_eq if there is no such equality.
11686 * Return -1 on error.
11688 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11689 int pos, int *div, int *ineq)
11691 int j, k, l;
11692 isl_size n_div, n_out;
11693 unsigned o_div, o_out;
11695 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11696 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11697 if (n_out < 0 || n_div < 0)
11698 return -1;
11700 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11701 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11703 if (ineq)
11704 *ineq = bmap->n_ineq;
11705 if (div)
11706 *div = n_div;
11707 for (j = 0; j < bmap->n_eq; ++j) {
11708 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11709 continue;
11710 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11711 n_out - (pos + 1)) != -1)
11712 continue;
11713 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11714 0, n_div);
11715 if (k >= n_div)
11716 return j;
11717 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11718 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11719 continue;
11720 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11721 k + 1, n_div - (k+1)) < n_div)
11722 continue;
11723 l = find_modulo_constraint_pair(bmap, pos,
11724 bmap->eq[j][o_div + k]);
11725 if (l < 0)
11726 return -1;
11727 if (l >= bmap->n_ineq)
11728 continue;
11729 if (div)
11730 *div = k;
11731 if (ineq)
11732 *ineq = l;
11733 return j;
11736 return bmap->n_eq;
11739 /* Check if the given basic map is obviously single-valued.
11740 * In particular, for each output dimension, check that there is
11741 * an equality that defines the output dimension in terms of
11742 * earlier dimensions.
11744 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11746 int i;
11747 isl_size n_out;
11749 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11750 if (n_out < 0)
11751 return isl_bool_error;
11753 for (i = 0; i < n_out; ++i) {
11754 int eq;
11756 eq = isl_basic_map_output_defining_equality(bmap, i,
11757 NULL, NULL);
11758 if (eq < 0)
11759 return isl_bool_error;
11760 if (eq >= bmap->n_eq)
11761 return isl_bool_false;
11764 return isl_bool_true;
11767 /* Check if the given basic map is single-valued.
11768 * We simply compute
11770 * M \circ M^-1
11772 * and check if the result is a subset of the identity mapping.
11774 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11776 isl_space *space;
11777 isl_basic_map *test;
11778 isl_basic_map *id;
11779 isl_bool sv;
11781 sv = isl_basic_map_plain_is_single_valued(bmap);
11782 if (sv < 0 || sv)
11783 return sv;
11785 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11786 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11788 space = isl_basic_map_get_space(bmap);
11789 space = isl_space_map_from_set(isl_space_range(space));
11790 id = isl_basic_map_identity(space);
11792 sv = isl_basic_map_is_subset(test, id);
11794 isl_basic_map_free(test);
11795 isl_basic_map_free(id);
11797 return sv;
11800 /* Check if the given map is obviously single-valued.
11802 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11804 if (!map)
11805 return isl_bool_error;
11806 if (map->n == 0)
11807 return isl_bool_true;
11808 if (map->n >= 2)
11809 return isl_bool_false;
11811 return isl_basic_map_plain_is_single_valued(map->p[0]);
11814 /* Check if the given map is single-valued.
11815 * We simply compute
11817 * M \circ M^-1
11819 * and check if the result is a subset of the identity mapping.
11821 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11823 isl_space *space;
11824 isl_map *test;
11825 isl_map *id;
11826 isl_bool sv;
11828 sv = isl_map_plain_is_single_valued(map);
11829 if (sv < 0 || sv)
11830 return sv;
11832 test = isl_map_reverse(isl_map_copy(map));
11833 test = isl_map_apply_range(test, isl_map_copy(map));
11835 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11836 id = isl_map_identity(space);
11838 sv = isl_map_is_subset(test, id);
11840 isl_map_free(test);
11841 isl_map_free(id);
11843 return sv;
11846 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11848 isl_bool in;
11850 map = isl_map_copy(map);
11851 map = isl_map_reverse(map);
11852 in = isl_map_is_single_valued(map);
11853 isl_map_free(map);
11855 return in;
11858 /* Check if the given map is obviously injective.
11860 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11862 isl_bool in;
11864 map = isl_map_copy(map);
11865 map = isl_map_reverse(map);
11866 in = isl_map_plain_is_single_valued(map);
11867 isl_map_free(map);
11869 return in;
11872 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11874 isl_bool sv;
11876 sv = isl_map_is_single_valued(map);
11877 if (sv < 0 || !sv)
11878 return sv;
11880 return isl_map_is_injective(map);
11883 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11885 return isl_map_is_single_valued(set_to_map(set));
11888 /* Does "map" only map elements to themselves?
11890 * If the domain and range spaces are different, then "map"
11891 * is considered not to be an identity relation, even if it is empty.
11892 * Otherwise, construct the maximal identity relation and
11893 * check whether "map" is a subset of this relation.
11895 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11897 isl_map *id;
11898 isl_bool equal, is_identity;
11900 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
11901 if (equal < 0 || !equal)
11902 return equal;
11904 id = isl_map_identity(isl_map_get_space(map));
11905 is_identity = isl_map_is_subset(map, id);
11906 isl_map_free(id);
11908 return is_identity;
11911 int isl_map_is_translation(__isl_keep isl_map *map)
11913 int ok;
11914 isl_set *delta;
11916 delta = isl_map_deltas(isl_map_copy(map));
11917 ok = isl_set_is_singleton(delta);
11918 isl_set_free(delta);
11920 return ok;
11923 static int unique(isl_int *p, unsigned pos, unsigned len)
11925 if (isl_seq_first_non_zero(p, pos) != -1)
11926 return 0;
11927 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11928 return 0;
11929 return 1;
11932 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11934 int i, j;
11935 isl_size nvar, n_div;
11936 unsigned ovar;
11938 n_div = isl_basic_set_dim(bset, isl_dim_div);
11939 if (n_div < 0)
11940 return isl_bool_error;
11941 if (n_div != 0)
11942 return isl_bool_false;
11944 nvar = isl_basic_set_dim(bset, isl_dim_set);
11945 if (nvar < 0)
11946 return isl_bool_error;
11947 ovar = isl_space_offset(bset->dim, isl_dim_set);
11948 for (j = 0; j < nvar; ++j) {
11949 int lower = 0, upper = 0;
11950 for (i = 0; i < bset->n_eq; ++i) {
11951 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11952 continue;
11953 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11954 return isl_bool_false;
11955 break;
11957 if (i < bset->n_eq)
11958 continue;
11959 for (i = 0; i < bset->n_ineq; ++i) {
11960 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11961 continue;
11962 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11963 return isl_bool_false;
11964 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11965 lower = 1;
11966 else
11967 upper = 1;
11969 if (!lower || !upper)
11970 return isl_bool_false;
11973 return isl_bool_true;
11976 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11978 if (!set)
11979 return isl_bool_error;
11980 if (set->n != 1)
11981 return isl_bool_false;
11983 return isl_basic_set_is_box(set->p[0]);
11986 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11988 if (!bset)
11989 return isl_bool_error;
11991 return isl_space_is_wrapping(bset->dim);
11994 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11996 if (!set)
11997 return isl_bool_error;
11999 return isl_space_is_wrapping(set->dim);
12002 /* Modify the space of "map" through a call to "change".
12003 * If "can_change" is set (not NULL), then first call it to check
12004 * if the modification is allowed, printing the error message "cannot_change"
12005 * if it is not.
12007 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12008 isl_bool (*can_change)(__isl_keep isl_map *map),
12009 const char *cannot_change,
12010 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12012 isl_bool ok;
12013 isl_space *space;
12015 if (!map)
12016 return NULL;
12018 ok = can_change ? can_change(map) : isl_bool_true;
12019 if (ok < 0)
12020 return isl_map_free(map);
12021 if (!ok)
12022 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12023 return isl_map_free(map));
12025 space = change(isl_map_get_space(map));
12026 map = isl_map_reset_space(map, space);
12028 return map;
12031 /* Is the domain of "map" a wrapped relation?
12033 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12035 if (!map)
12036 return isl_bool_error;
12038 return isl_space_domain_is_wrapping(map->dim);
12041 /* Does "map" have a wrapped relation in both domain and range?
12043 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12045 return isl_space_is_product(isl_map_peek_space(map));
12048 /* Is the range of "map" a wrapped relation?
12050 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12052 if (!map)
12053 return isl_bool_error;
12055 return isl_space_range_is_wrapping(map->dim);
12058 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12060 isl_space *space;
12062 space = isl_basic_map_take_space(bmap);
12063 space = isl_space_wrap(space);
12064 bmap = isl_basic_map_restore_space(bmap, space);
12066 bmap = isl_basic_map_finalize(bmap);
12068 return bset_from_bmap(bmap);
12071 /* Given a map A -> B, return the set (A -> B).
12073 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12075 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12078 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12080 bset = isl_basic_set_cow(bset);
12081 if (!bset)
12082 return NULL;
12084 bset->dim = isl_space_unwrap(bset->dim);
12085 if (!bset->dim)
12086 goto error;
12088 bset = isl_basic_set_finalize(bset);
12090 return bset_to_bmap(bset);
12091 error:
12092 isl_basic_set_free(bset);
12093 return NULL;
12096 /* Given a set (A -> B), return the map A -> B.
12097 * Error out if "set" is not of the form (A -> B).
12099 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12101 return isl_map_change_space(set, &isl_set_is_wrapping,
12102 "not a wrapping set", &isl_space_unwrap);
12105 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12106 enum isl_dim_type type)
12108 isl_space *space;
12110 space = isl_basic_map_take_space(bmap);
12111 space = isl_space_reset(space, type);
12112 bmap = isl_basic_map_restore_space(bmap, space);
12114 bmap = isl_basic_map_mark_final(bmap);
12116 return bmap;
12119 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12120 enum isl_dim_type type)
12122 int i;
12123 isl_space *space;
12125 if (!map)
12126 return NULL;
12128 if (!isl_space_is_named_or_nested(map->dim, type))
12129 return map;
12131 map = isl_map_cow(map);
12132 if (!map)
12133 return NULL;
12135 for (i = 0; i < map->n; ++i) {
12136 map->p[i] = isl_basic_map_reset(map->p[i], type);
12137 if (!map->p[i])
12138 goto error;
12141 space = isl_map_take_space(map);
12142 space = isl_space_reset(space, type);
12143 map = isl_map_restore_space(map, space);
12145 return map;
12146 error:
12147 isl_map_free(map);
12148 return NULL;
12151 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12153 isl_space *space;
12155 space = isl_basic_map_take_space(bmap);
12156 space = isl_space_flatten(space);
12157 bmap = isl_basic_map_restore_space(bmap, space);
12159 bmap = isl_basic_map_mark_final(bmap);
12161 return bmap;
12164 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12166 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12169 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12170 __isl_take isl_basic_map *bmap)
12172 isl_space *space;
12174 space = isl_basic_map_take_space(bmap);
12175 space = isl_space_flatten_domain(space);
12176 bmap = isl_basic_map_restore_space(bmap, space);
12178 bmap = isl_basic_map_mark_final(bmap);
12180 return bmap;
12183 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12184 __isl_take isl_basic_map *bmap)
12186 isl_space *space;
12188 space = isl_basic_map_take_space(bmap);
12189 space = isl_space_flatten_range(space);
12190 bmap = isl_basic_map_restore_space(bmap, space);
12192 bmap = isl_basic_map_mark_final(bmap);
12194 return bmap;
12197 /* Remove any internal structure from the spaces of domain and range of "map".
12199 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12201 if (!map)
12202 return NULL;
12204 if (!map->dim->nested[0] && !map->dim->nested[1])
12205 return map;
12207 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12210 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12212 return set_from_map(isl_map_flatten(set_to_map(set)));
12215 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12217 isl_space *space, *flat_space;
12218 isl_map *map;
12220 space = isl_set_get_space(set);
12221 flat_space = isl_space_flatten(isl_space_copy(space));
12222 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12223 flat_space));
12224 map = isl_map_intersect_domain(map, set);
12226 return map;
12229 /* Remove any internal structure from the space of the domain of "map".
12231 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12233 if (!map)
12234 return NULL;
12236 if (!map->dim->nested[0])
12237 return map;
12239 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12242 /* Remove any internal structure from the space of the range of "map".
12244 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12246 if (!map)
12247 return NULL;
12249 if (!map->dim->nested[1])
12250 return map;
12252 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12255 /* Reorder the dimensions of "bmap" according to the given dim_map
12256 * and set the dimension specification to "space" and
12257 * perform Gaussian elimination on the result.
12259 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12260 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12262 isl_basic_map *res;
12263 unsigned flags;
12264 isl_size n_div;
12266 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12267 if (n_div < 0 || !space || !dim_map)
12268 goto error;
12270 flags = bmap->flags;
12271 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12272 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12273 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12274 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12275 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12276 if (res)
12277 res->flags = flags;
12278 res = isl_basic_map_gauss(res, NULL);
12279 res = isl_basic_map_finalize(res);
12280 return res;
12281 error:
12282 isl_dim_map_free(dim_map);
12283 isl_basic_map_free(bmap);
12284 isl_space_free(space);
12285 return NULL;
12288 /* Reorder the dimensions of "map" according to given reordering.
12290 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12291 __isl_take isl_reordering *r)
12293 int i;
12294 struct isl_dim_map *dim_map;
12296 map = isl_map_cow(map);
12297 dim_map = isl_dim_map_from_reordering(r);
12298 if (!map || !r || !dim_map)
12299 goto error;
12301 for (i = 0; i < map->n; ++i) {
12302 struct isl_dim_map *dim_map_i;
12303 isl_space *space;
12305 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12307 space = isl_reordering_get_space(r);
12308 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12310 if (!map->p[i])
12311 goto error;
12314 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12315 map = isl_map_unmark_normalized(map);
12317 isl_reordering_free(r);
12318 isl_dim_map_free(dim_map);
12319 return map;
12320 error:
12321 isl_dim_map_free(dim_map);
12322 isl_map_free(map);
12323 isl_reordering_free(r);
12324 return NULL;
12327 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12328 __isl_take isl_reordering *r)
12330 return set_from_map(isl_map_realign(set_to_map(set), r));
12333 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12334 __isl_take isl_space *model)
12336 isl_ctx *ctx;
12337 isl_bool aligned;
12339 if (!map || !model)
12340 goto error;
12342 ctx = isl_space_get_ctx(model);
12343 if (!isl_space_has_named_params(model))
12344 isl_die(ctx, isl_error_invalid,
12345 "model has unnamed parameters", goto error);
12346 if (isl_map_check_named_params(map) < 0)
12347 goto error;
12348 aligned = isl_map_space_has_equal_params(map, model);
12349 if (aligned < 0)
12350 goto error;
12351 if (!aligned) {
12352 isl_reordering *exp;
12354 exp = isl_parameter_alignment_reordering(map->dim, model);
12355 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12356 map = isl_map_realign(map, exp);
12359 isl_space_free(model);
12360 return map;
12361 error:
12362 isl_space_free(model);
12363 isl_map_free(map);
12364 return NULL;
12367 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12368 __isl_take isl_space *model)
12370 return isl_map_align_params(set, model);
12373 /* Align the parameters of "bmap" to those of "model", introducing
12374 * additional parameters if needed.
12376 __isl_give isl_basic_map *isl_basic_map_align_params(
12377 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12379 isl_ctx *ctx;
12380 isl_bool equal_params;
12382 if (!bmap || !model)
12383 goto error;
12385 ctx = isl_space_get_ctx(model);
12386 if (!isl_space_has_named_params(model))
12387 isl_die(ctx, isl_error_invalid,
12388 "model has unnamed parameters", goto error);
12389 if (isl_basic_map_check_named_params(bmap) < 0)
12390 goto error;
12391 equal_params = isl_space_has_equal_params(bmap->dim, model);
12392 if (equal_params < 0)
12393 goto error;
12394 if (!equal_params) {
12395 isl_reordering *exp;
12396 struct isl_dim_map *dim_map;
12398 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12399 exp = isl_reordering_extend_space(exp,
12400 isl_basic_map_get_space(bmap));
12401 dim_map = isl_dim_map_from_reordering(exp);
12402 bmap = isl_basic_map_realign(bmap,
12403 isl_reordering_get_space(exp),
12404 isl_dim_map_extend(dim_map, bmap));
12405 isl_reordering_free(exp);
12406 isl_dim_map_free(dim_map);
12409 isl_space_free(model);
12410 return bmap;
12411 error:
12412 isl_space_free(model);
12413 isl_basic_map_free(bmap);
12414 return NULL;
12417 /* Do "bset" and "space" have the same parameters?
12419 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12420 __isl_keep isl_space *space)
12422 isl_space *bset_space;
12424 bset_space = isl_basic_set_peek_space(bset);
12425 return isl_space_has_equal_params(bset_space, space);
12428 /* Do "map" and "space" have the same parameters?
12430 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12431 __isl_keep isl_space *space)
12433 isl_space *map_space;
12435 map_space = isl_map_peek_space(map);
12436 return isl_space_has_equal_params(map_space, space);
12439 /* Do "set" and "space" have the same parameters?
12441 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12442 __isl_keep isl_space *space)
12444 return isl_map_space_has_equal_params(set_to_map(set), space);
12447 /* Align the parameters of "bset" to those of "model", introducing
12448 * additional parameters if needed.
12450 __isl_give isl_basic_set *isl_basic_set_align_params(
12451 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12453 return isl_basic_map_align_params(bset, model);
12456 /* Drop all parameters not referenced by "map".
12458 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12460 int i;
12461 isl_size n;
12463 n = isl_map_dim(map, isl_dim_param);
12464 if (isl_map_check_named_params(map) < 0 || n < 0)
12465 return isl_map_free(map);
12467 for (i = n - 1; i >= 0; i--) {
12468 isl_bool involves;
12470 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12471 if (involves < 0)
12472 return isl_map_free(map);
12473 if (!involves)
12474 map = isl_map_project_out(map, isl_dim_param, i, 1);
12477 return map;
12480 /* Drop all parameters not referenced by "set".
12482 __isl_give isl_set *isl_set_drop_unused_params(
12483 __isl_take isl_set *set)
12485 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12488 /* Drop all parameters not referenced by "bmap".
12490 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12491 __isl_take isl_basic_map *bmap)
12493 isl_size nparam;
12494 int i;
12496 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12497 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12498 return isl_basic_map_free(bmap);
12500 for (i = nparam - 1; i >= 0; i--) {
12501 isl_bool involves;
12503 involves = isl_basic_map_involves_dims(bmap,
12504 isl_dim_param, i, 1);
12505 if (involves < 0)
12506 return isl_basic_map_free(bmap);
12507 if (!involves)
12508 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12511 return bmap;
12514 /* Drop all parameters not referenced by "bset".
12516 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12517 __isl_take isl_basic_set *bset)
12519 return bset_from_bmap(isl_basic_map_drop_unused_params(
12520 bset_to_bmap(bset)));
12523 /* Given a tuple of identifiers "tuple" in a space that corresponds
12524 * to that of "set", if any of those identifiers appear as parameters
12525 * in "set", then equate those parameters with the corresponding
12526 * set dimensions and project out the parameters.
12527 * The result therefore has no such parameters.
12529 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12530 __isl_keep isl_multi_id *tuple)
12532 int i;
12533 isl_size n;
12534 isl_space *set_space, *tuple_space;
12536 set_space = isl_set_peek_space(set);
12537 tuple_space = isl_multi_id_peek_space(tuple);
12538 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12539 return isl_set_free(set);
12540 n = isl_multi_id_size(tuple);
12541 if (n < 0)
12542 return isl_set_free(set);
12543 for (i = 0; i < n; ++i) {
12544 isl_id *id;
12545 int pos;
12547 id = isl_multi_id_get_at(tuple, i);
12548 if (!id)
12549 return isl_set_free(set);
12550 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12551 isl_id_free(id);
12552 if (pos < 0)
12553 continue;
12554 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12555 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12557 return set;
12560 /* Bind the set dimensions of "set" to parameters with identifiers
12561 * specified by "tuple", living in the same space as "set".
12563 * If no parameters with these identifiers appear in "set" already,
12564 * then the set dimensions are simply reinterpreted as parameters.
12565 * Otherwise, the parameters are first equated to the corresponding
12566 * set dimensions.
12568 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12569 __isl_take isl_multi_id *tuple)
12571 isl_space *space;
12573 set = equate_params(set, tuple);
12574 space = isl_set_get_space(set);
12575 space = isl_space_bind_set(space, tuple);
12576 isl_multi_id_free(tuple);
12577 set = isl_set_reset_space(set, space);
12579 return set;
12582 /* Given a tuple of identifiers "tuple" in a space that corresponds
12583 * to the domain of "map", if any of those identifiers appear as parameters
12584 * in "map", then equate those parameters with the corresponding
12585 * input dimensions and project out the parameters.
12586 * The result therefore has no such parameters.
12588 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12589 __isl_keep isl_multi_id *tuple)
12591 int i;
12592 isl_size n;
12593 isl_space *map_space, *tuple_space;
12595 map_space = isl_map_peek_space(map);
12596 tuple_space = isl_multi_id_peek_space(tuple);
12597 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12598 return isl_map_free(map);
12599 n = isl_multi_id_size(tuple);
12600 if (n < 0)
12601 return isl_map_free(map);
12602 for (i = 0; i < n; ++i) {
12603 isl_id *id;
12604 int pos;
12606 id = isl_multi_id_get_at(tuple, i);
12607 if (!id)
12608 return isl_map_free(map);
12609 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12610 isl_id_free(id);
12611 if (pos < 0)
12612 continue;
12613 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12614 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12616 return map;
12619 /* Bind the input dimensions of "map" to parameters with identifiers
12620 * specified by "tuple", living in the domain space of "map".
12622 * If no parameters with these identifiers appear in "map" already,
12623 * then the input dimensions are simply reinterpreted as parameters.
12624 * Otherwise, the parameters are first equated to the corresponding
12625 * input dimensions.
12627 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12628 __isl_take isl_multi_id *tuple)
12630 isl_space *space;
12631 isl_set *set;
12633 map = map_equate_params(map, tuple);
12634 space = isl_map_get_space(map);
12635 space = isl_space_bind_map_domain(space, tuple);
12636 isl_multi_id_free(tuple);
12637 set = set_from_map(isl_map_reset_space(map, space));
12639 return set;
12642 /* Bind the output dimensions of "map" to parameters with identifiers
12643 * specified by "tuple", living in the range space of "map".
12645 * Since binding is more easily implemented on the domain,
12646 * bind the input dimensions of the inverse of "map".
12648 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12649 __isl_take isl_multi_id *tuple)
12651 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12654 /* Insert a domain corresponding to "tuple"
12655 * into the nullary or unary relation "set".
12656 * The result has an extra initial tuple and is therefore
12657 * either a unary or binary relation.
12658 * Any parameters with identifiers in "tuple" are reinterpreted
12659 * as the corresponding domain dimensions.
12661 static __isl_give isl_map *unbind_params_insert_domain(
12662 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12664 isl_space *space;
12665 isl_reordering *r;
12667 space = isl_set_peek_space(set);
12668 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12669 isl_multi_id_free(tuple);
12671 return isl_map_realign(set_to_map(set), r);
12674 /* Construct a set with "tuple" as domain from the parameter domain "set".
12675 * Any parameters with identifiers in "tuple" are reinterpreted
12676 * as the corresponding set dimensions.
12678 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12679 __isl_take isl_multi_id *tuple)
12681 isl_bool is_params;
12683 is_params = isl_set_is_params(set);
12684 if (is_params < 0)
12685 set = isl_set_free(set);
12686 else if (!is_params)
12687 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12688 "expecting parameter domain", set = isl_set_free(set));
12689 return set_from_map(unbind_params_insert_domain(set, tuple));
12692 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12694 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12696 isl_bool is_params;
12698 is_params = isl_set_is_params(set);
12699 if (is_params < 0)
12700 return isl_stat_error;
12701 else if (is_params)
12702 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12703 "expecting proper set", return isl_stat_error);
12705 return isl_stat_ok;
12708 /* Construct a map with "domain" as domain and "set" as range.
12709 * Any parameters with identifiers in "domain" are reinterpreted
12710 * as the corresponding domain dimensions.
12712 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12713 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12715 if (isl_set_check_is_set(set) < 0)
12716 set = isl_set_free(set);
12717 return unbind_params_insert_domain(set, domain);
12720 /* Construct a map with "domain" as domain and "set" as range.
12722 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12723 __isl_take isl_space *domain)
12725 isl_size dim;
12726 isl_space *space;
12727 isl_map *map;
12729 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12730 domain = isl_space_free(domain);
12731 dim = isl_space_dim(domain, isl_dim_set);
12732 if (dim < 0)
12733 domain = isl_space_free(domain);
12734 space = isl_set_get_space(set);
12735 domain = isl_space_replace_params(domain, space);
12736 space = isl_space_map_from_domain_and_range(domain, space);
12738 map = isl_map_from_range(set);
12739 map = isl_map_add_dims(map, isl_dim_in, dim);
12740 map = isl_map_reset_space(map, space);
12742 return map;
12745 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12746 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12747 enum isl_dim_type c2, enum isl_dim_type c3,
12748 enum isl_dim_type c4, enum isl_dim_type c5)
12750 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12751 struct isl_mat *mat;
12752 int i, j, k;
12753 int pos;
12754 isl_size total;
12756 total = isl_basic_map_dim(bmap, isl_dim_all);
12757 if (total < 0)
12758 return NULL;
12759 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12760 if (!mat)
12761 return NULL;
12762 for (i = 0; i < bmap->n_eq; ++i)
12763 for (j = 0, pos = 0; j < 5; ++j) {
12764 int off = isl_basic_map_offset(bmap, c[j]);
12765 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12766 if (dim < 0)
12767 return isl_mat_free(mat);
12768 for (k = 0; k < dim; ++k) {
12769 isl_int_set(mat->row[i][pos],
12770 bmap->eq[i][off + k]);
12771 ++pos;
12775 return mat;
12778 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12779 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12780 enum isl_dim_type c2, enum isl_dim_type c3,
12781 enum isl_dim_type c4, enum isl_dim_type c5)
12783 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12784 struct isl_mat *mat;
12785 int i, j, k;
12786 int pos;
12787 isl_size total;
12789 total = isl_basic_map_dim(bmap, isl_dim_all);
12790 if (total < 0)
12791 return NULL;
12792 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12793 if (!mat)
12794 return NULL;
12795 for (i = 0; i < bmap->n_ineq; ++i)
12796 for (j = 0, pos = 0; j < 5; ++j) {
12797 int off = isl_basic_map_offset(bmap, c[j]);
12798 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12799 if (dim < 0)
12800 return isl_mat_free(mat);
12801 for (k = 0; k < dim; ++k) {
12802 isl_int_set(mat->row[i][pos],
12803 bmap->ineq[i][off + k]);
12804 ++pos;
12808 return mat;
12811 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12812 __isl_take isl_space *space,
12813 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12814 enum isl_dim_type c2, enum isl_dim_type c3,
12815 enum isl_dim_type c4, enum isl_dim_type c5)
12817 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12818 isl_basic_map *bmap = NULL;
12819 isl_size dim;
12820 unsigned total;
12821 unsigned extra;
12822 int i, j, k, l;
12823 int pos;
12825 dim = isl_space_dim(space, isl_dim_all);
12826 if (dim < 0 || !eq || !ineq)
12827 goto error;
12829 if (eq->n_col != ineq->n_col)
12830 isl_die(space->ctx, isl_error_invalid,
12831 "equalities and inequalities matrices should have "
12832 "same number of columns", goto error);
12834 total = 1 + dim;
12836 if (eq->n_col < total)
12837 isl_die(space->ctx, isl_error_invalid,
12838 "number of columns too small", goto error);
12840 extra = eq->n_col - total;
12842 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12843 eq->n_row, ineq->n_row);
12844 if (!bmap)
12845 goto error;
12846 for (i = 0; i < extra; ++i) {
12847 k = isl_basic_map_alloc_div(bmap);
12848 if (k < 0)
12849 goto error;
12850 isl_int_set_si(bmap->div[k][0], 0);
12852 for (i = 0; i < eq->n_row; ++i) {
12853 l = isl_basic_map_alloc_equality(bmap);
12854 if (l < 0)
12855 goto error;
12856 for (j = 0, pos = 0; j < 5; ++j) {
12857 int off = isl_basic_map_offset(bmap, c[j]);
12858 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12859 if (dim < 0)
12860 goto error;
12861 for (k = 0; k < dim; ++k) {
12862 isl_int_set(bmap->eq[l][off + k],
12863 eq->row[i][pos]);
12864 ++pos;
12868 for (i = 0; i < ineq->n_row; ++i) {
12869 l = isl_basic_map_alloc_inequality(bmap);
12870 if (l < 0)
12871 goto error;
12872 for (j = 0, pos = 0; j < 5; ++j) {
12873 int off = isl_basic_map_offset(bmap, c[j]);
12874 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12875 if (dim < 0)
12876 goto error;
12877 for (k = 0; k < dim; ++k) {
12878 isl_int_set(bmap->ineq[l][off + k],
12879 ineq->row[i][pos]);
12880 ++pos;
12885 isl_space_free(space);
12886 isl_mat_free(eq);
12887 isl_mat_free(ineq);
12889 bmap = isl_basic_map_simplify(bmap);
12890 return isl_basic_map_finalize(bmap);
12891 error:
12892 isl_space_free(space);
12893 isl_mat_free(eq);
12894 isl_mat_free(ineq);
12895 isl_basic_map_free(bmap);
12896 return NULL;
12899 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12900 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12901 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12903 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12904 c1, c2, c3, c4, isl_dim_in);
12907 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12908 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12909 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12911 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12912 c1, c2, c3, c4, isl_dim_in);
12915 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12916 __isl_take isl_space *space,
12917 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12918 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12920 isl_basic_map *bmap;
12921 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12922 c1, c2, c3, c4, isl_dim_in);
12923 return bset_from_bmap(bmap);
12926 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12928 if (!bmap)
12929 return isl_bool_error;
12931 return isl_space_can_zip(bmap->dim);
12934 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12936 if (!map)
12937 return isl_bool_error;
12939 return isl_space_can_zip(map->dim);
12942 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12943 * (A -> C) -> (B -> D).
12945 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12947 unsigned pos;
12948 isl_size n_in;
12949 isl_size n1;
12950 isl_size n2;
12952 if (!bmap)
12953 return NULL;
12955 if (!isl_basic_map_can_zip(bmap))
12956 isl_die(bmap->ctx, isl_error_invalid,
12957 "basic map cannot be zipped", goto error);
12958 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12959 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12960 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12961 if (n_in < 0 || n1 < 0 || n2 < 0)
12962 return isl_basic_map_free(bmap);
12963 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12964 bmap = isl_basic_map_cow(bmap);
12965 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12966 if (!bmap)
12967 return NULL;
12968 bmap->dim = isl_space_zip(bmap->dim);
12969 if (!bmap->dim)
12970 goto error;
12971 bmap = isl_basic_map_mark_final(bmap);
12972 return bmap;
12973 error:
12974 isl_basic_map_free(bmap);
12975 return NULL;
12978 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12979 * (A -> C) -> (B -> D).
12981 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12983 if (!map)
12984 return NULL;
12986 if (!isl_map_can_zip(map))
12987 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12988 goto error);
12990 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
12991 error:
12992 isl_map_free(map);
12993 return NULL;
12996 /* Can we apply isl_basic_map_curry to "bmap"?
12997 * That is, does it have a nested relation in its domain?
12999 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13001 if (!bmap)
13002 return isl_bool_error;
13004 return isl_space_can_curry(bmap->dim);
13007 /* Can we apply isl_map_curry to "map"?
13008 * That is, does it have a nested relation in its domain?
13010 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13012 if (!map)
13013 return isl_bool_error;
13015 return isl_space_can_curry(map->dim);
13018 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13019 * A -> (B -> C).
13021 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13024 if (!bmap)
13025 return NULL;
13027 if (!isl_basic_map_can_curry(bmap))
13028 isl_die(bmap->ctx, isl_error_invalid,
13029 "basic map cannot be curried", goto error);
13030 bmap = isl_basic_map_cow(bmap);
13031 if (!bmap)
13032 return NULL;
13033 bmap->dim = isl_space_curry(bmap->dim);
13034 if (!bmap->dim)
13035 goto error;
13036 bmap = isl_basic_map_mark_final(bmap);
13037 return bmap;
13038 error:
13039 isl_basic_map_free(bmap);
13040 return NULL;
13043 /* Given a map (A -> B) -> C, return the corresponding map
13044 * A -> (B -> C).
13046 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13048 return isl_map_change_space(map, &isl_map_can_curry,
13049 "map cannot be curried", &isl_space_curry);
13052 /* Can isl_map_range_curry be applied to "map"?
13053 * That is, does it have a nested relation in its range,
13054 * the domain of which is itself a nested relation?
13056 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13058 if (!map)
13059 return isl_bool_error;
13061 return isl_space_can_range_curry(map->dim);
13064 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13065 * A -> (B -> (C -> D)).
13067 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13069 return isl_map_change_space(map, &isl_map_can_range_curry,
13070 "map range cannot be curried",
13071 &isl_space_range_curry);
13074 /* Can we apply isl_basic_map_uncurry to "bmap"?
13075 * That is, does it have a nested relation in its domain?
13077 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13079 if (!bmap)
13080 return isl_bool_error;
13082 return isl_space_can_uncurry(bmap->dim);
13085 /* Can we apply isl_map_uncurry to "map"?
13086 * That is, does it have a nested relation in its domain?
13088 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13090 if (!map)
13091 return isl_bool_error;
13093 return isl_space_can_uncurry(map->dim);
13096 /* Given a basic map A -> (B -> C), return the corresponding basic map
13097 * (A -> B) -> C.
13099 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13102 if (!bmap)
13103 return NULL;
13105 if (!isl_basic_map_can_uncurry(bmap))
13106 isl_die(bmap->ctx, isl_error_invalid,
13107 "basic map cannot be uncurried",
13108 return isl_basic_map_free(bmap));
13109 bmap = isl_basic_map_cow(bmap);
13110 if (!bmap)
13111 return NULL;
13112 bmap->dim = isl_space_uncurry(bmap->dim);
13113 if (!bmap->dim)
13114 return isl_basic_map_free(bmap);
13115 bmap = isl_basic_map_mark_final(bmap);
13116 return bmap;
13119 /* Given a map A -> (B -> C), return the corresponding map
13120 * (A -> B) -> C.
13122 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13124 return isl_map_change_space(map, &isl_map_can_uncurry,
13125 "map cannot be uncurried", &isl_space_uncurry);
13128 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13129 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13131 return isl_map_equate(set, type1, pos1, type2, pos2);
13134 /* Construct a basic map where the given dimensions are equal to each other.
13136 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13137 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13139 isl_basic_map *bmap = NULL;
13140 int i;
13141 isl_size total;
13143 total = isl_space_dim(space, isl_dim_all);
13144 if (total < 0 ||
13145 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13146 isl_space_check_range(space, type2, pos2, 1) < 0)
13147 goto error;
13149 if (type1 == type2 && pos1 == pos2)
13150 return isl_basic_map_universe(space);
13152 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13153 i = isl_basic_map_alloc_equality(bmap);
13154 if (i < 0)
13155 goto error;
13156 isl_seq_clr(bmap->eq[i], 1 + total);
13157 pos1 += isl_basic_map_offset(bmap, type1);
13158 pos2 += isl_basic_map_offset(bmap, type2);
13159 isl_int_set_si(bmap->eq[i][pos1], -1);
13160 isl_int_set_si(bmap->eq[i][pos2], 1);
13161 bmap = isl_basic_map_finalize(bmap);
13162 isl_space_free(space);
13163 return bmap;
13164 error:
13165 isl_space_free(space);
13166 isl_basic_map_free(bmap);
13167 return NULL;
13170 /* Add a constraint imposing that the given two dimensions are equal.
13172 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13173 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13175 isl_basic_map *eq;
13177 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13179 bmap = isl_basic_map_intersect(bmap, eq);
13181 return bmap;
13184 /* Add a constraint imposing that the given two dimensions are equal.
13186 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13187 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13189 isl_basic_map *bmap;
13191 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13193 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13195 return map;
13198 /* Add a constraint imposing that the given two dimensions have opposite values.
13200 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13201 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13203 isl_basic_map *bmap = NULL;
13204 int i;
13205 isl_size total;
13207 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13208 return isl_map_free(map);
13209 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13210 return isl_map_free(map);
13212 total = isl_map_dim(map, isl_dim_all);
13213 if (total < 0)
13214 return isl_map_free(map);
13215 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13216 i = isl_basic_map_alloc_equality(bmap);
13217 if (i < 0)
13218 goto error;
13219 isl_seq_clr(bmap->eq[i], 1 + total);
13220 pos1 += isl_basic_map_offset(bmap, type1);
13221 pos2 += isl_basic_map_offset(bmap, type2);
13222 isl_int_set_si(bmap->eq[i][pos1], 1);
13223 isl_int_set_si(bmap->eq[i][pos2], 1);
13224 bmap = isl_basic_map_finalize(bmap);
13226 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13228 return map;
13229 error:
13230 isl_basic_map_free(bmap);
13231 isl_map_free(map);
13232 return NULL;
13235 /* Construct a constraint imposing that the value of the first dimension is
13236 * greater than or equal to that of the second.
13238 static __isl_give isl_constraint *constraint_order_ge(
13239 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13240 enum isl_dim_type type2, int pos2)
13242 isl_constraint *c;
13244 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13245 isl_space_check_range(space, type2, pos2, 1) < 0)
13246 space = isl_space_free(space);
13247 if (!space)
13248 return NULL;
13250 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13252 if (type1 == type2 && pos1 == pos2)
13253 return c;
13255 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13256 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13258 return c;
13261 /* Add a constraint imposing that the value of the first dimension is
13262 * greater than or equal to that of the second.
13264 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13265 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13267 isl_constraint *c;
13268 isl_space *space;
13270 if (type1 == type2 && pos1 == pos2)
13271 return bmap;
13272 space = isl_basic_map_get_space(bmap);
13273 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13274 bmap = isl_basic_map_add_constraint(bmap, c);
13276 return bmap;
13279 /* Add a constraint imposing that the value of the first dimension is
13280 * greater than or equal to that of the second.
13282 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13283 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13285 isl_constraint *c;
13286 isl_space *space;
13288 if (type1 == type2 && pos1 == pos2)
13289 return map;
13290 space = isl_map_get_space(map);
13291 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13292 map = isl_map_add_constraint(map, c);
13294 return map;
13297 /* Add a constraint imposing that the value of the first dimension is
13298 * less than or equal to that of the second.
13300 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13301 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13303 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13306 /* Construct a basic map where the value of the first dimension is
13307 * greater than that of the second.
13309 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13310 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13312 isl_basic_map *bmap = NULL;
13313 int i;
13314 isl_size total;
13316 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13317 isl_space_check_range(space, type2, pos2, 1) < 0)
13318 goto error;
13320 if (type1 == type2 && pos1 == pos2)
13321 return isl_basic_map_empty(space);
13323 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13324 total = isl_basic_map_dim(bmap, isl_dim_all);
13325 i = isl_basic_map_alloc_inequality(bmap);
13326 if (total < 0 || i < 0)
13327 return isl_basic_map_free(bmap);
13328 isl_seq_clr(bmap->ineq[i], 1 + total);
13329 pos1 += isl_basic_map_offset(bmap, type1);
13330 pos2 += isl_basic_map_offset(bmap, type2);
13331 isl_int_set_si(bmap->ineq[i][pos1], 1);
13332 isl_int_set_si(bmap->ineq[i][pos2], -1);
13333 isl_int_set_si(bmap->ineq[i][0], -1);
13334 bmap = isl_basic_map_finalize(bmap);
13336 return bmap;
13337 error:
13338 isl_space_free(space);
13339 isl_basic_map_free(bmap);
13340 return NULL;
13343 /* Add a constraint imposing that the value of the first dimension is
13344 * greater than that of the second.
13346 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13349 isl_basic_map *gt;
13351 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13353 bmap = isl_basic_map_intersect(bmap, gt);
13355 return bmap;
13358 /* Add a constraint imposing that the value of the first dimension is
13359 * greater than that of the second.
13361 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13362 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13364 isl_basic_map *bmap;
13366 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13368 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13370 return map;
13373 /* Add a constraint imposing that the value of the first dimension is
13374 * smaller than that of the second.
13376 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13377 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13379 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13382 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13383 int pos)
13385 isl_aff *div;
13386 isl_local_space *ls;
13388 if (!bmap)
13389 return NULL;
13391 if (!isl_basic_map_divs_known(bmap))
13392 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13393 "some divs are unknown", return NULL);
13395 ls = isl_basic_map_get_local_space(bmap);
13396 div = isl_local_space_get_div(ls, pos);
13397 isl_local_space_free(ls);
13399 return div;
13402 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13403 int pos)
13405 return isl_basic_map_get_div(bset, pos);
13408 /* Plug in "subs" for dimension "type", "pos" of "bset".
13410 * Let i be the dimension to replace and let "subs" be of the form
13412 * f/d
13414 * Any integer division with a non-zero coefficient for i,
13416 * floor((a i + g)/m)
13418 * is replaced by
13420 * floor((a f + d g)/(m d))
13422 * Constraints of the form
13424 * a i + g
13426 * are replaced by
13428 * a f + d g
13430 * We currently require that "subs" is an integral expression.
13431 * Handling rational expressions may require us to add stride constraints
13432 * as we do in isl_basic_set_preimage_multi_aff.
13434 __isl_give isl_basic_set *isl_basic_set_substitute(
13435 __isl_take isl_basic_set *bset,
13436 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13438 int i;
13439 isl_int v;
13440 isl_ctx *ctx;
13441 isl_size n_div;
13443 if (bset && isl_basic_set_plain_is_empty(bset))
13444 return bset;
13446 bset = isl_basic_set_cow(bset);
13447 if (!bset || !subs)
13448 goto error;
13450 ctx = isl_basic_set_get_ctx(bset);
13451 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
13452 isl_die(ctx, isl_error_invalid,
13453 "spaces don't match", goto error);
13454 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
13455 if (n_div < 0)
13456 goto error;
13457 if (n_div != 0)
13458 isl_die(ctx, isl_error_unsupported,
13459 "cannot handle divs yet", goto error);
13460 if (!isl_int_is_one(subs->v->el[0]))
13461 isl_die(ctx, isl_error_invalid,
13462 "can only substitute integer expressions", goto error);
13464 pos += isl_basic_set_offset(bset, type);
13466 isl_int_init(v);
13468 for (i = 0; i < bset->n_eq; ++i) {
13469 if (isl_int_is_zero(bset->eq[i][pos]))
13470 continue;
13471 isl_int_set(v, bset->eq[i][pos]);
13472 isl_int_set_si(bset->eq[i][pos], 0);
13473 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
13474 v, subs->v->el + 1, subs->v->size - 1);
13477 for (i = 0; i < bset->n_ineq; ++i) {
13478 if (isl_int_is_zero(bset->ineq[i][pos]))
13479 continue;
13480 isl_int_set(v, bset->ineq[i][pos]);
13481 isl_int_set_si(bset->ineq[i][pos], 0);
13482 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
13483 v, subs->v->el + 1, subs->v->size - 1);
13486 for (i = 0; i < bset->n_div; ++i) {
13487 if (isl_int_is_zero(bset->div[i][1 + pos]))
13488 continue;
13489 isl_int_set(v, bset->div[i][1 + pos]);
13490 isl_int_set_si(bset->div[i][1 + pos], 0);
13491 isl_seq_combine(bset->div[i] + 1,
13492 subs->v->el[0], bset->div[i] + 1,
13493 v, subs->v->el + 1, subs->v->size - 1);
13494 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
13497 isl_int_clear(v);
13499 bset = isl_basic_set_simplify(bset);
13500 return isl_basic_set_finalize(bset);
13501 error:
13502 isl_basic_set_free(bset);
13503 return NULL;
13506 /* Plug in "subs" for dimension "type", "pos" of "set".
13508 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13509 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13511 int i;
13513 if (set && isl_set_plain_is_empty(set))
13514 return set;
13516 set = isl_set_cow(set);
13517 if (!set || !subs)
13518 goto error;
13520 for (i = set->n - 1; i >= 0; --i) {
13521 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
13522 set = set_from_map(remove_if_empty(set_to_map(set), i));
13523 if (!set)
13524 return NULL;
13527 return set;
13528 error:
13529 isl_set_free(set);
13530 return NULL;
13533 /* Check if the range of "ma" is compatible with the domain or range
13534 * (depending on "type") of "bmap".
13536 static isl_stat check_basic_map_compatible_range_multi_aff(
13537 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13538 __isl_keep isl_multi_aff *ma)
13540 isl_bool m;
13541 isl_space *ma_space;
13543 ma_space = isl_multi_aff_get_space(ma);
13545 m = isl_space_has_equal_params(bmap->dim, ma_space);
13546 if (m < 0)
13547 goto error;
13548 if (!m)
13549 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13550 "parameters don't match", goto error);
13551 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13552 if (m < 0)
13553 goto error;
13554 if (!m)
13555 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13556 "spaces don't match", goto error);
13558 isl_space_free(ma_space);
13559 return isl_stat_ok;
13560 error:
13561 isl_space_free(ma_space);
13562 return isl_stat_error;
13565 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13566 * coefficients before the transformed range of dimensions,
13567 * the "n_after" coefficients after the transformed range of dimensions
13568 * and the coefficients of the other divs in "bmap".
13570 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13571 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13573 int i;
13574 isl_size n_param;
13575 isl_size n_set;
13576 isl_local_space *ls;
13578 if (n_div == 0)
13579 return bmap;
13581 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13582 n_param = isl_local_space_dim(ls, isl_dim_param);
13583 n_set = isl_local_space_dim(ls, isl_dim_set);
13584 if (n_param < 0 || n_set < 0)
13585 return isl_basic_map_free(bmap);
13587 for (i = 0; i < n_div; ++i) {
13588 int o_bmap = 0, o_ls = 0;
13590 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13591 o_bmap += 1 + 1 + n_param;
13592 o_ls += 1 + 1 + n_param;
13593 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13594 o_bmap += n_before;
13595 isl_seq_cpy(bmap->div[i] + o_bmap,
13596 ls->div->row[i] + o_ls, n_set);
13597 o_bmap += n_set;
13598 o_ls += n_set;
13599 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13600 o_bmap += n_after;
13601 isl_seq_cpy(bmap->div[i] + o_bmap,
13602 ls->div->row[i] + o_ls, n_div);
13603 o_bmap += n_div;
13604 o_ls += n_div;
13605 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13606 bmap = isl_basic_map_add_div_constraints(bmap, i);
13607 if (!bmap)
13608 goto error;
13611 isl_local_space_free(ls);
13612 return bmap;
13613 error:
13614 isl_local_space_free(ls);
13615 return isl_basic_map_free(bmap);
13618 /* How many stride constraints does "ma" enforce?
13619 * That is, how many of the affine expressions have a denominator
13620 * different from one?
13622 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13624 int i;
13625 int strides = 0;
13627 for (i = 0; i < ma->n; ++i)
13628 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13629 strides++;
13631 return strides;
13634 /* For each affine expression in ma of the form
13636 * x_i = (f_i y + h_i)/m_i
13638 * with m_i different from one, add a constraint to "bmap"
13639 * of the form
13641 * f_i y + h_i = m_i alpha_i
13643 * with alpha_i an additional existentially quantified variable.
13645 * The input variables of "ma" correspond to a subset of the variables
13646 * of "bmap". There are "n_before" variables in "bmap" before this
13647 * subset and "n_after" variables after this subset.
13648 * The integer divisions of the affine expressions in "ma" are assumed
13649 * to have been aligned. There are "n_div_ma" of them and
13650 * they appear first in "bmap", straight after the "n_after" variables.
13652 static __isl_give isl_basic_map *add_ma_strides(
13653 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13654 int n_before, int n_after, int n_div_ma)
13656 int i, k;
13657 int div;
13658 isl_size total;
13659 isl_size n_param;
13660 isl_size n_in;
13662 total = isl_basic_map_dim(bmap, isl_dim_all);
13663 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13664 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13665 if (total < 0 || n_param < 0 || n_in < 0)
13666 return isl_basic_map_free(bmap);
13667 for (i = 0; i < ma->n; ++i) {
13668 int o_bmap = 0, o_ma = 1;
13670 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13671 continue;
13672 div = isl_basic_map_alloc_div(bmap);
13673 k = isl_basic_map_alloc_equality(bmap);
13674 if (div < 0 || k < 0)
13675 goto error;
13676 isl_int_set_si(bmap->div[div][0], 0);
13677 isl_seq_cpy(bmap->eq[k] + o_bmap,
13678 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13679 o_bmap += 1 + n_param;
13680 o_ma += 1 + n_param;
13681 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13682 o_bmap += n_before;
13683 isl_seq_cpy(bmap->eq[k] + o_bmap,
13684 ma->u.p[i]->v->el + o_ma, n_in);
13685 o_bmap += n_in;
13686 o_ma += n_in;
13687 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13688 o_bmap += n_after;
13689 isl_seq_cpy(bmap->eq[k] + o_bmap,
13690 ma->u.p[i]->v->el + o_ma, n_div_ma);
13691 o_bmap += n_div_ma;
13692 o_ma += n_div_ma;
13693 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13694 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13695 total++;
13698 return bmap;
13699 error:
13700 isl_basic_map_free(bmap);
13701 return NULL;
13704 /* Replace the domain or range space (depending on "type) of "space" by "set".
13706 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13707 enum isl_dim_type type, __isl_take isl_space *set)
13709 if (type == isl_dim_in) {
13710 space = isl_space_range(space);
13711 space = isl_space_map_from_domain_and_range(set, space);
13712 } else {
13713 space = isl_space_domain(space);
13714 space = isl_space_map_from_domain_and_range(space, set);
13717 return space;
13720 /* Compute the preimage of the domain or range (depending on "type")
13721 * of "bmap" under the function represented by "ma".
13722 * In other words, plug in "ma" in the domain or range of "bmap".
13723 * The result is a basic map that lives in the same space as "bmap"
13724 * except that the domain or range has been replaced by
13725 * the domain space of "ma".
13727 * If bmap is represented by
13729 * A(p) + S u + B x + T v + C(divs) >= 0,
13731 * where u and x are input and output dimensions if type == isl_dim_out
13732 * while x and v are input and output dimensions if type == isl_dim_in,
13733 * and ma is represented by
13735 * x = D(p) + F(y) + G(divs')
13737 * then the result is
13739 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13741 * The divs in the input set are similarly adjusted.
13742 * In particular
13744 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13746 * becomes
13748 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13749 * B_i G(divs') + c_i(divs))/n_i)
13751 * If bmap is not a rational map and if F(y) involves any denominators
13753 * x_i = (f_i y + h_i)/m_i
13755 * then additional constraints are added to ensure that we only
13756 * map back integer points. That is we enforce
13758 * f_i y + h_i = m_i alpha_i
13760 * with alpha_i an additional existentially quantified variable.
13762 * We first copy over the divs from "ma".
13763 * Then we add the modified constraints and divs from "bmap".
13764 * Finally, we add the stride constraints, if needed.
13766 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13767 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13768 __isl_take isl_multi_aff *ma)
13770 int i, k;
13771 isl_space *space;
13772 isl_basic_map *res = NULL;
13773 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13774 isl_int f, c1, c2, g;
13775 isl_bool rational;
13776 int strides;
13778 isl_int_init(f);
13779 isl_int_init(c1);
13780 isl_int_init(c2);
13781 isl_int_init(g);
13783 ma = isl_multi_aff_align_divs(ma);
13784 if (!bmap || !ma)
13785 goto error;
13786 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13787 goto error;
13789 if (type == isl_dim_in) {
13790 n_before = 0;
13791 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13792 } else {
13793 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13794 n_after = 0;
13796 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13797 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13798 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13799 goto error;
13801 space = isl_multi_aff_get_domain_space(ma);
13802 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13803 rational = isl_basic_map_is_rational(bmap);
13804 strides = rational ? 0 : multi_aff_strides(ma);
13805 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13806 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13807 if (rational)
13808 res = isl_basic_map_set_rational(res);
13810 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13811 if (isl_basic_map_alloc_div(res) < 0)
13812 goto error;
13814 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13815 if (!res)
13816 goto error;
13818 for (i = 0; i < bmap->n_eq; ++i) {
13819 k = isl_basic_map_alloc_equality(res);
13820 if (k < 0)
13821 goto error;
13822 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13823 n_after, n_div_ma, n_div_bmap,
13824 f, c1, c2, g, 0) < 0)
13825 goto error;
13828 for (i = 0; i < bmap->n_ineq; ++i) {
13829 k = isl_basic_map_alloc_inequality(res);
13830 if (k < 0)
13831 goto error;
13832 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13833 n_after, n_div_ma, n_div_bmap,
13834 f, c1, c2, g, 0) < 0)
13835 goto error;
13838 for (i = 0; i < bmap->n_div; ++i) {
13839 if (isl_int_is_zero(bmap->div[i][0])) {
13840 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13841 continue;
13843 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13844 n_before, n_after, n_div_ma, n_div_bmap,
13845 f, c1, c2, g, 1) < 0)
13846 goto error;
13849 if (strides)
13850 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13852 isl_int_clear(f);
13853 isl_int_clear(c1);
13854 isl_int_clear(c2);
13855 isl_int_clear(g);
13856 isl_basic_map_free(bmap);
13857 isl_multi_aff_free(ma);
13858 res = isl_basic_map_simplify(res);
13859 return isl_basic_map_finalize(res);
13860 error:
13861 isl_int_clear(f);
13862 isl_int_clear(c1);
13863 isl_int_clear(c2);
13864 isl_int_clear(g);
13865 isl_basic_map_free(bmap);
13866 isl_multi_aff_free(ma);
13867 isl_basic_map_free(res);
13868 return NULL;
13871 /* Compute the preimage of "bset" under the function represented by "ma".
13872 * In other words, plug in "ma" in "bset". The result is a basic set
13873 * that lives in the domain space of "ma".
13875 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13876 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13878 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13881 /* Compute the preimage of the domain of "bmap" under the function
13882 * represented by "ma".
13883 * In other words, plug in "ma" in the domain of "bmap".
13884 * The result is a basic map that lives in the same space as "bmap"
13885 * except that the domain has been replaced by the domain space of "ma".
13887 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13888 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13890 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13893 /* Compute the preimage of the range of "bmap" under the function
13894 * represented by "ma".
13895 * In other words, plug in "ma" in the range of "bmap".
13896 * The result is a basic map that lives in the same space as "bmap"
13897 * except that the range has been replaced by the domain space of "ma".
13899 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13900 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13902 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13905 /* Check if the range of "ma" is compatible with the domain or range
13906 * (depending on "type") of "map".
13907 * Return isl_stat_error if anything is wrong.
13909 static isl_stat check_map_compatible_range_multi_aff(
13910 __isl_keep isl_map *map, enum isl_dim_type type,
13911 __isl_keep isl_multi_aff *ma)
13913 isl_bool m;
13914 isl_space *ma_space;
13916 ma_space = isl_multi_aff_get_space(ma);
13917 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13918 isl_space_free(ma_space);
13919 if (m < 0)
13920 return isl_stat_error;
13921 if (!m)
13922 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13923 "spaces don't match", return isl_stat_error);
13924 return isl_stat_ok;
13927 /* Compute the preimage of the domain or range (depending on "type")
13928 * of "map" under the function represented by "ma".
13929 * In other words, plug in "ma" in the domain or range of "map".
13930 * The result is a map that lives in the same space as "map"
13931 * except that the domain or range has been replaced by
13932 * the domain space of "ma".
13934 * The parameters are assumed to have been aligned.
13936 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13937 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13939 int i;
13940 isl_space *space;
13942 map = isl_map_cow(map);
13943 ma = isl_multi_aff_align_divs(ma);
13944 if (!map || !ma)
13945 goto error;
13946 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13947 goto error;
13949 for (i = 0; i < map->n; ++i) {
13950 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13951 isl_multi_aff_copy(ma));
13952 if (!map->p[i])
13953 goto error;
13956 space = isl_multi_aff_get_domain_space(ma);
13957 space = isl_space_set(isl_map_get_space(map), type, space);
13959 isl_space_free(isl_map_take_space(map));
13960 map = isl_map_restore_space(map, space);
13961 if (!map)
13962 goto error;
13964 isl_multi_aff_free(ma);
13965 if (map->n > 1)
13966 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13967 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13968 return map;
13969 error:
13970 isl_multi_aff_free(ma);
13971 isl_map_free(map);
13972 return NULL;
13975 /* Compute the preimage of the domain or range (depending on "type")
13976 * of "map" under the function represented by "ma".
13977 * In other words, plug in "ma" in the domain or range of "map".
13978 * The result is a map that lives in the same space as "map"
13979 * except that the domain or range has been replaced by
13980 * the domain space of "ma".
13982 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13983 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13985 isl_bool aligned;
13987 if (!map || !ma)
13988 goto error;
13990 aligned = isl_map_space_has_equal_params(map, ma->space);
13991 if (aligned < 0)
13992 goto error;
13993 if (aligned)
13994 return map_preimage_multi_aff(map, type, ma);
13996 if (isl_map_check_named_params(map) < 0)
13997 goto error;
13998 if (!isl_space_has_named_params(ma->space))
13999 isl_die(map->ctx, isl_error_invalid,
14000 "unaligned unnamed parameters", goto error);
14001 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14002 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14004 return map_preimage_multi_aff(map, type, ma);
14005 error:
14006 isl_multi_aff_free(ma);
14007 return isl_map_free(map);
14010 /* Compute the preimage of "set" under the function represented by "ma".
14011 * In other words, plug in "ma" in "set". The result is a set
14012 * that lives in the domain space of "ma".
14014 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14015 __isl_take isl_multi_aff *ma)
14017 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14020 /* Compute the preimage of the domain of "map" under the function
14021 * represented by "ma".
14022 * In other words, plug in "ma" in the domain of "map".
14023 * The result is a map that lives in the same space as "map"
14024 * except that the domain has been replaced by the domain space of "ma".
14026 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14027 __isl_take isl_multi_aff *ma)
14029 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14032 /* Compute the preimage of the range of "map" under the function
14033 * represented by "ma".
14034 * In other words, plug in "ma" in the range of "map".
14035 * The result is a map that lives in the same space as "map"
14036 * except that the range has been replaced by the domain space of "ma".
14038 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14039 __isl_take isl_multi_aff *ma)
14041 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14044 /* Compute the preimage of "map" under the function represented by "pma".
14045 * In other words, plug in "pma" in the domain or range of "map".
14046 * The result is a map that lives in the same space as "map",
14047 * except that the space of type "type" has been replaced by
14048 * the domain space of "pma".
14050 * The parameters of "map" and "pma" are assumed to have been aligned.
14052 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14053 __isl_take isl_map *map, enum isl_dim_type type,
14054 __isl_take isl_pw_multi_aff *pma)
14056 int i;
14057 isl_map *res;
14059 if (!pma)
14060 goto error;
14062 if (pma->n == 0) {
14063 isl_pw_multi_aff_free(pma);
14064 res = isl_map_empty(isl_map_get_space(map));
14065 isl_map_free(map);
14066 return res;
14069 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14070 isl_multi_aff_copy(pma->p[0].maff));
14071 if (type == isl_dim_in)
14072 res = isl_map_intersect_domain(res,
14073 isl_map_copy(pma->p[0].set));
14074 else
14075 res = isl_map_intersect_range(res,
14076 isl_map_copy(pma->p[0].set));
14078 for (i = 1; i < pma->n; ++i) {
14079 isl_map *res_i;
14081 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14082 isl_multi_aff_copy(pma->p[i].maff));
14083 if (type == isl_dim_in)
14084 res_i = isl_map_intersect_domain(res_i,
14085 isl_map_copy(pma->p[i].set));
14086 else
14087 res_i = isl_map_intersect_range(res_i,
14088 isl_map_copy(pma->p[i].set));
14089 res = isl_map_union(res, res_i);
14092 isl_pw_multi_aff_free(pma);
14093 isl_map_free(map);
14094 return res;
14095 error:
14096 isl_pw_multi_aff_free(pma);
14097 isl_map_free(map);
14098 return NULL;
14101 /* Compute the preimage of "map" under the function represented by "pma".
14102 * In other words, plug in "pma" in the domain or range of "map".
14103 * The result is a map that lives in the same space as "map",
14104 * except that the space of type "type" has been replaced by
14105 * the domain space of "pma".
14107 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14108 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14110 isl_bool aligned;
14112 if (!map || !pma)
14113 goto error;
14115 aligned = isl_map_space_has_equal_params(map, pma->dim);
14116 if (aligned < 0)
14117 goto error;
14118 if (aligned)
14119 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14121 if (isl_map_check_named_params(map) < 0)
14122 goto error;
14123 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14124 goto error;
14125 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14126 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14128 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14129 error:
14130 isl_pw_multi_aff_free(pma);
14131 return isl_map_free(map);
14134 /* Compute the preimage of "set" under the function represented by "pma".
14135 * In other words, plug in "pma" in "set". The result is a set
14136 * that lives in the domain space of "pma".
14138 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14139 __isl_take isl_pw_multi_aff *pma)
14141 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14144 /* Compute the preimage of the domain of "map" under the function
14145 * represented by "pma".
14146 * In other words, plug in "pma" in the domain of "map".
14147 * The result is a map that lives in the same space as "map",
14148 * except that domain space has been replaced by the domain space of "pma".
14150 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14151 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14153 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14156 /* Compute the preimage of the range of "map" under the function
14157 * represented by "pma".
14158 * In other words, plug in "pma" in the range of "map".
14159 * The result is a map that lives in the same space as "map",
14160 * except that range space has been replaced by the domain space of "pma".
14162 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14163 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14165 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14168 /* Compute the preimage of "map" under the function represented by "mpa".
14169 * In other words, plug in "mpa" in the domain or range of "map".
14170 * The result is a map that lives in the same space as "map",
14171 * except that the space of type "type" has been replaced by
14172 * the domain space of "mpa".
14174 * If the map does not involve any constraints that refer to the
14175 * dimensions of the substituted space, then the only possible
14176 * effect of "mpa" on the map is to map the space to a different space.
14177 * We create a separate isl_multi_aff to effectuate this change
14178 * in order to avoid spurious splitting of the map along the pieces
14179 * of "mpa".
14180 * If "mpa" has a non-trivial explicit domain, however,
14181 * then the full substitution should be performed.
14183 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14184 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14186 isl_size n;
14187 isl_bool full;
14188 isl_pw_multi_aff *pma;
14190 n = isl_map_dim(map, type);
14191 if (n < 0 || !mpa)
14192 goto error;
14194 full = isl_map_involves_dims(map, type, 0, n);
14195 if (full >= 0 && !full)
14196 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14197 if (full < 0)
14198 goto error;
14199 if (!full) {
14200 isl_space *space;
14201 isl_multi_aff *ma;
14203 space = isl_multi_pw_aff_get_space(mpa);
14204 isl_multi_pw_aff_free(mpa);
14205 ma = isl_multi_aff_zero(space);
14206 return isl_map_preimage_multi_aff(map, type, ma);
14209 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14210 return isl_map_preimage_pw_multi_aff(map, type, pma);
14211 error:
14212 isl_map_free(map);
14213 isl_multi_pw_aff_free(mpa);
14214 return NULL;
14217 /* Compute the preimage of "map" under the function represented by "mpa".
14218 * In other words, plug in "mpa" in the domain "map".
14219 * The result is a map that lives in the same space as "map",
14220 * except that domain space has been replaced by the domain space of "mpa".
14222 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14223 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14225 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14228 /* Compute the preimage of "set" by the function represented by "mpa".
14229 * In other words, plug in "mpa" in "set".
14231 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14232 __isl_take isl_multi_pw_aff *mpa)
14234 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14237 /* Return a copy of the equality constraints of "bset" as a matrix.
14239 __isl_give isl_mat *isl_basic_set_extract_equalities(
14240 __isl_keep isl_basic_set *bset)
14242 isl_ctx *ctx;
14243 isl_size total;
14245 total = isl_basic_set_dim(bset, isl_dim_all);
14246 if (total < 0)
14247 return NULL;
14249 ctx = isl_basic_set_get_ctx(bset);
14250 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14253 /* Are the "n" "coefficients" starting at "first" of the integer division
14254 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14255 * to each other?
14256 * The "coefficient" at position 0 is the denominator.
14257 * The "coefficient" at position 1 is the constant term.
14259 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14260 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14261 unsigned first, unsigned n)
14263 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14264 return isl_bool_error;
14265 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14266 return isl_bool_error;
14267 return isl_seq_eq(bmap1->div[pos1] + first,
14268 bmap2->div[pos2] + first, n);
14271 /* Are the integer division expressions at position "pos1" in "bmap1" and
14272 * "pos2" in "bmap2" equal to each other, except that the constant terms
14273 * are different?
14275 isl_bool isl_basic_map_equal_div_expr_except_constant(
14276 __isl_keep isl_basic_map *bmap1, int pos1,
14277 __isl_keep isl_basic_map *bmap2, int pos2)
14279 isl_bool equal;
14280 isl_size total, total2;
14282 total = isl_basic_map_dim(bmap1, isl_dim_all);
14283 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14284 if (total < 0 || total2 < 0)
14285 return isl_bool_error;
14286 if (total != total2)
14287 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14288 "incomparable div expressions", return isl_bool_error);
14289 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14290 0, 1);
14291 if (equal < 0 || !equal)
14292 return equal;
14293 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14294 1, 1);
14295 if (equal < 0 || equal)
14296 return isl_bool_not(equal);
14297 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14298 2, total);
14301 /* Replace the numerator of the constant term of the integer division
14302 * expression at position "div" in "bmap" by "value".
14303 * The caller guarantees that this does not change the meaning
14304 * of the input.
14306 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14307 __isl_take isl_basic_map *bmap, int div, int value)
14309 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14310 return isl_basic_map_free(bmap);
14312 isl_int_set_si(bmap->div[div][1], value);
14314 return bmap;
14317 /* Is the point "inner" internal to inequality constraint "ineq"
14318 * of "bset"?
14319 * The point is considered to be internal to the inequality constraint,
14320 * if it strictly lies on the positive side of the inequality constraint,
14321 * or if it lies on the constraint and the constraint is lexico-positive.
14323 static isl_bool is_internal(__isl_keep isl_vec *inner,
14324 __isl_keep isl_basic_set *bset, int ineq)
14326 isl_ctx *ctx;
14327 int pos;
14328 isl_size total;
14330 if (!inner || !bset)
14331 return isl_bool_error;
14333 ctx = isl_basic_set_get_ctx(bset);
14334 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14335 &ctx->normalize_gcd);
14336 if (!isl_int_is_zero(ctx->normalize_gcd))
14337 return isl_int_is_nonneg(ctx->normalize_gcd);
14339 total = isl_basic_set_dim(bset, isl_dim_all);
14340 if (total < 0)
14341 return isl_bool_error;
14342 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14343 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14346 /* Tighten the inequality constraints of "bset" that are outward with respect
14347 * to the point "vec".
14348 * That is, tighten the constraints that are not satisfied by "vec".
14350 * "vec" is a point internal to some superset S of "bset" that is used
14351 * to make the subsets of S disjoint, by tightening one half of the constraints
14352 * that separate two subsets. In particular, the constraints of S
14353 * are all satisfied by "vec" and should not be tightened.
14354 * Of the internal constraints, those that have "vec" on the outside
14355 * are tightened. The shared facet is included in the adjacent subset
14356 * with the opposite constraint.
14357 * For constraints that saturate "vec", this criterion cannot be used
14358 * to determine which of the two sides should be tightened.
14359 * Instead, the sign of the first non-zero coefficient is used
14360 * to make this choice. Note that this second criterion is never used
14361 * on the constraints of S since "vec" is interior to "S".
14363 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14364 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14366 int j;
14368 bset = isl_basic_set_cow(bset);
14369 if (!bset)
14370 return NULL;
14371 for (j = 0; j < bset->n_ineq; ++j) {
14372 isl_bool internal;
14374 internal = is_internal(vec, bset, j);
14375 if (internal < 0)
14376 return isl_basic_set_free(bset);
14377 if (internal)
14378 continue;
14379 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14382 return bset;
14385 /* Replace the variables x of type "type" starting at "first" in "bmap"
14386 * by x' with x = M x' with M the matrix trans.
14387 * That is, replace the corresponding coefficients c by c M.
14389 * The transformation matrix should be a square matrix.
14391 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14392 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14393 __isl_take isl_mat *trans)
14395 unsigned pos;
14397 bmap = isl_basic_map_cow(bmap);
14398 if (!bmap || !trans)
14399 goto error;
14401 if (trans->n_row != trans->n_col)
14402 isl_die(trans->ctx, isl_error_invalid,
14403 "expecting square transformation matrix", goto error);
14404 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14405 goto error;
14407 pos = isl_basic_map_offset(bmap, type) + first;
14409 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14410 isl_mat_copy(trans)) < 0)
14411 goto error;
14412 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14413 isl_mat_copy(trans)) < 0)
14414 goto error;
14415 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14416 isl_mat_copy(trans)) < 0)
14417 goto error;
14419 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14420 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14422 isl_mat_free(trans);
14423 return bmap;
14424 error:
14425 isl_mat_free(trans);
14426 isl_basic_map_free(bmap);
14427 return NULL;
14430 /* Replace the variables x of type "type" starting at "first" in "bset"
14431 * by x' with x = M x' with M the matrix trans.
14432 * That is, replace the corresponding coefficients c by c M.
14434 * The transformation matrix should be a square matrix.
14436 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14437 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14438 __isl_take isl_mat *trans)
14440 return isl_basic_map_transform_dims(bset, type, first, trans);