isl_basic_map_make_strides_explicit: move down isl_basic_map_get_ctx call
[isl.git] / isl_map.c
blob9b3fedf34d9f4da5e9a856528ed3ed46e25c9ab6
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 static 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 /* Drop all constraints in bmap that involve any of the dimensions
2932 * first to first+n-1.
2933 * This function only performs the actual removal of constraints.
2935 * This function should not call finalize since it is used by
2936 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2938 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2939 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2941 int i;
2943 if (n == 0)
2944 return bmap;
2946 bmap = isl_basic_map_cow(bmap);
2948 if (!bmap)
2949 return NULL;
2951 for (i = bmap->n_eq - 1; i >= 0; --i) {
2952 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2953 continue;
2954 if (isl_basic_map_drop_equality(bmap, i) < 0)
2955 return isl_basic_map_free(bmap);
2958 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2959 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2960 continue;
2961 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2962 return isl_basic_map_free(bmap);
2965 return bmap;
2968 /* Drop all constraints in bset that involve any of the dimensions
2969 * first to first+n-1.
2970 * This function only performs the actual removal of constraints.
2972 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2973 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2975 return isl_basic_map_drop_constraints_involving(bset, first, n);
2978 /* Drop all constraints in bmap that do not involve any of the dimensions
2979 * first to first + n - 1 of the given type.
2981 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2982 __isl_take isl_basic_map *bmap,
2983 enum isl_dim_type type, unsigned first, unsigned n)
2985 int i;
2987 if (n == 0) {
2988 isl_space *space = isl_basic_map_get_space(bmap);
2989 isl_basic_map_free(bmap);
2990 return isl_basic_map_universe(space);
2992 bmap = isl_basic_map_cow(bmap);
2993 if (!bmap)
2994 return NULL;
2996 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2997 return isl_basic_map_free(bmap);
2999 first += isl_basic_map_offset(bmap, type) - 1;
3001 for (i = bmap->n_eq - 1; i >= 0; --i) {
3002 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3003 continue;
3004 if (isl_basic_map_drop_equality(bmap, i) < 0)
3005 return isl_basic_map_free(bmap);
3008 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3009 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3010 continue;
3011 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3012 return isl_basic_map_free(bmap);
3015 bmap = isl_basic_map_add_known_div_constraints(bmap);
3016 return bmap;
3019 /* Drop all constraints in bset that do not involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3023 __isl_take isl_basic_set *bset,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3026 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3027 type, first, n);
3030 /* Drop all constraints in bmap that involve any of the dimensions
3031 * first to first + n - 1 of the given type.
3033 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3034 __isl_take isl_basic_map *bmap,
3035 enum isl_dim_type type, unsigned first, unsigned n)
3037 if (!bmap)
3038 return NULL;
3039 if (n == 0)
3040 return bmap;
3042 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3043 return isl_basic_map_free(bmap);
3045 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3046 first += isl_basic_map_offset(bmap, type) - 1;
3047 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3048 bmap = isl_basic_map_add_known_div_constraints(bmap);
3049 return bmap;
3052 /* Drop all constraints in bset that involve any of the dimensions
3053 * first to first + n - 1 of the given type.
3055 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3056 __isl_take isl_basic_set *bset,
3057 enum isl_dim_type type, unsigned first, unsigned n)
3059 return isl_basic_map_drop_constraints_involving_dims(bset,
3060 type, first, n);
3063 /* Drop constraints from "map" by applying "drop" to each basic map.
3065 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3066 enum isl_dim_type type, unsigned first, unsigned n,
3067 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3068 enum isl_dim_type type, unsigned first, unsigned n))
3070 int i;
3072 if (isl_map_check_range(map, type, first, n) < 0)
3073 return isl_map_free(map);
3075 map = isl_map_cow(map);
3076 if (!map)
3077 return NULL;
3079 for (i = 0; i < map->n; ++i) {
3080 map->p[i] = drop(map->p[i], type, first, n);
3081 if (!map->p[i])
3082 return isl_map_free(map);
3085 if (map->n > 1)
3086 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3088 return map;
3091 /* Drop all constraints in map that involve any of the dimensions
3092 * first to first + n - 1 of the given type.
3094 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3095 __isl_take isl_map *map,
3096 enum isl_dim_type type, unsigned first, unsigned n)
3098 if (n == 0)
3099 return map;
3100 return drop_constraints(map, type, first, n,
3101 &isl_basic_map_drop_constraints_involving_dims);
3104 /* Drop all constraints in "map" that do not involve any of the dimensions
3105 * first to first + n - 1 of the given type.
3107 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3108 __isl_take isl_map *map,
3109 enum isl_dim_type type, unsigned first, unsigned n)
3111 if (n == 0) {
3112 isl_space *space = isl_map_get_space(map);
3113 isl_map_free(map);
3114 return isl_map_universe(space);
3116 return drop_constraints(map, type, first, n,
3117 &isl_basic_map_drop_constraints_not_involving_dims);
3120 /* Drop all constraints in set that involve any of the dimensions
3121 * first to first + n - 1 of the given type.
3123 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3124 __isl_take isl_set *set,
3125 enum isl_dim_type type, unsigned first, unsigned n)
3127 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3130 /* Drop all constraints in "set" that do not involve any of the dimensions
3131 * first to first + n - 1 of the given type.
3133 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3134 __isl_take isl_set *set,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3140 /* Does local variable "div" of "bmap" have a complete explicit representation?
3141 * Having a complete explicit representation requires not only
3142 * an explicit representation, but also that all local variables
3143 * that appear in this explicit representation in turn have
3144 * a complete explicit representation.
3146 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3148 int i;
3149 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3150 isl_bool marked;
3152 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3153 if (marked < 0 || marked)
3154 return isl_bool_not(marked);
3156 for (i = bmap->n_div - 1; i >= 0; --i) {
3157 isl_bool known;
3159 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3160 continue;
3161 known = isl_basic_map_div_is_known(bmap, i);
3162 if (known < 0 || !known)
3163 return known;
3166 return isl_bool_true;
3169 /* Remove all divs that are unknown or defined in terms of unknown divs.
3171 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3172 __isl_take isl_basic_map *bmap)
3174 int i;
3176 if (!bmap)
3177 return NULL;
3179 for (i = bmap->n_div - 1; i >= 0; --i) {
3180 if (isl_basic_map_div_is_known(bmap, i))
3181 continue;
3182 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3183 if (!bmap)
3184 return NULL;
3185 i = bmap->n_div;
3188 return bmap;
3191 /* Remove all divs that are unknown or defined in terms of unknown divs.
3193 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3194 __isl_take isl_basic_set *bset)
3196 return isl_basic_map_remove_unknown_divs(bset);
3199 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3201 int i;
3203 if (!map)
3204 return NULL;
3205 if (map->n == 0)
3206 return map;
3208 map = isl_map_cow(map);
3209 if (!map)
3210 return NULL;
3212 for (i = 0; i < map->n; ++i) {
3213 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3214 if (!map->p[i])
3215 goto error;
3217 return map;
3218 error:
3219 isl_map_free(map);
3220 return NULL;
3223 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3225 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3228 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3229 __isl_take isl_basic_set *bset,
3230 enum isl_dim_type type, unsigned first, unsigned n)
3232 isl_basic_map *bmap = bset_to_bmap(bset);
3233 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3234 return bset_from_bmap(bmap);
3237 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3238 enum isl_dim_type type, unsigned first, unsigned n)
3240 int i;
3242 if (n == 0)
3243 return map;
3245 map = isl_map_cow(map);
3246 if (isl_map_check_range(map, type, first, n) < 0)
3247 return isl_map_free(map);
3249 for (i = 0; i < map->n; ++i) {
3250 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3251 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3252 if (!map->p[i])
3253 goto error;
3255 map = isl_map_drop(map, type, first, n);
3256 return map;
3257 error:
3258 isl_map_free(map);
3259 return NULL;
3262 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3263 enum isl_dim_type type, unsigned first, unsigned n)
3265 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3266 type, first, n));
3269 /* Project out n inputs starting at first using Fourier-Motzkin */
3270 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3271 unsigned first, unsigned n)
3273 return isl_map_remove_dims(map, isl_dim_in, first, n);
3276 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3277 FILE *out, int indent)
3279 isl_printer *p;
3281 if (!bset) {
3282 fprintf(out, "null basic set\n");
3283 return;
3286 fprintf(out, "%*s", indent, "");
3287 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3288 bset->ref, bset->dim->nparam, bset->dim->n_out,
3289 bset->extra, bset->flags);
3291 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3292 p = isl_printer_set_dump(p, 1);
3293 p = isl_printer_set_indent(p, indent);
3294 p = isl_printer_start_line(p);
3295 p = isl_printer_print_basic_set(p, bset);
3296 p = isl_printer_end_line(p);
3297 isl_printer_free(p);
3300 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3301 FILE *out, int indent)
3303 isl_printer *p;
3305 if (!bmap) {
3306 fprintf(out, "null basic map\n");
3307 return;
3310 fprintf(out, "%*s", indent, "");
3311 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3312 "flags: %x, n_name: %d\n",
3313 bmap->ref,
3314 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3315 bmap->extra, bmap->flags, bmap->dim->n_id);
3317 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3318 p = isl_printer_set_dump(p, 1);
3319 p = isl_printer_set_indent(p, indent);
3320 p = isl_printer_start_line(p);
3321 p = isl_printer_print_basic_map(p, bmap);
3322 p = isl_printer_end_line(p);
3323 isl_printer_free(p);
3326 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3327 unsigned pos)
3329 isl_size total;
3331 total = isl_basic_map_dim(bmap, isl_dim_all);
3332 if (total < 0)
3333 return isl_basic_map_free(bmap);
3334 if (pos >= bmap->n_ineq)
3335 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3336 "invalid position", return isl_basic_map_free(bmap));
3337 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3338 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3339 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3340 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3341 return bmap;
3344 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3345 unsigned flags)
3347 if (isl_space_check_is_set(space) < 0)
3348 goto error;
3349 return isl_map_alloc_space(space, n, flags);
3350 error:
3351 isl_space_free(space);
3352 return NULL;
3355 /* Make sure "map" has room for at least "n" more basic maps.
3357 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3359 int i;
3360 struct isl_map *grown = NULL;
3362 if (!map)
3363 return NULL;
3364 isl_assert(map->ctx, n >= 0, goto error);
3365 if (map->n + n <= map->size)
3366 return map;
3367 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3368 if (!grown)
3369 goto error;
3370 for (i = 0; i < map->n; ++i) {
3371 grown->p[i] = isl_basic_map_copy(map->p[i]);
3372 if (!grown->p[i])
3373 goto error;
3374 grown->n++;
3376 isl_map_free(map);
3377 return grown;
3378 error:
3379 isl_map_free(grown);
3380 isl_map_free(map);
3381 return NULL;
3384 /* Make sure "set" has room for at least "n" more basic sets.
3386 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3388 return set_from_map(isl_map_grow(set_to_map(set), n));
3391 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3393 return isl_map_from_basic_map(bset);
3396 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3398 struct isl_map *map;
3400 if (!bmap)
3401 return NULL;
3403 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3404 return isl_map_add_basic_map(map, bmap);
3407 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3408 __isl_take isl_basic_set *bset)
3410 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3411 bset_to_bmap(bset)));
3414 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3416 return isl_map_free(set);
3419 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3421 int i;
3423 if (!set) {
3424 fprintf(out, "null set\n");
3425 return;
3428 fprintf(out, "%*s", indent, "");
3429 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3430 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3431 set->flags);
3432 for (i = 0; i < set->n; ++i) {
3433 fprintf(out, "%*s", indent, "");
3434 fprintf(out, "basic set %d:\n", i);
3435 isl_basic_set_print_internal(set->p[i], out, indent+4);
3439 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3441 int i;
3443 if (!map) {
3444 fprintf(out, "null map\n");
3445 return;
3448 fprintf(out, "%*s", indent, "");
3449 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3450 "flags: %x, n_name: %d\n",
3451 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3452 map->dim->n_out, map->flags, map->dim->n_id);
3453 for (i = 0; i < map->n; ++i) {
3454 fprintf(out, "%*s", indent, "");
3455 fprintf(out, "basic map %d:\n", i);
3456 isl_basic_map_print_internal(map->p[i], out, indent+4);
3460 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3462 static isl_stat isl_basic_map_check_compatible_domain(
3463 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3465 isl_bool ok;
3467 ok = isl_basic_map_compatible_domain(bmap, bset);
3468 if (ok < 0)
3469 return isl_stat_error;
3470 if (!ok)
3471 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3472 "incompatible spaces", return isl_stat_error);
3474 return isl_stat_ok;
3477 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3478 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3480 struct isl_basic_map *bmap_domain;
3481 isl_size dim;
3483 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3484 goto error;
3486 dim = isl_basic_set_dim(bset, isl_dim_set);
3487 if (dim < 0)
3488 goto error;
3489 if (dim != 0 &&
3490 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3491 goto error;
3493 bmap = isl_basic_map_cow(bmap);
3494 if (!bmap)
3495 goto error;
3496 bmap = isl_basic_map_extend(bmap,
3497 bset->n_div, bset->n_eq, bset->n_ineq);
3498 bmap_domain = isl_basic_map_from_domain(bset);
3499 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3501 bmap = isl_basic_map_simplify(bmap);
3502 return isl_basic_map_finalize(bmap);
3503 error:
3504 isl_basic_map_free(bmap);
3505 isl_basic_set_free(bset);
3506 return NULL;
3509 /* Check that the space of "bset" is the same as that of the range of "bmap".
3511 static isl_stat isl_basic_map_check_compatible_range(
3512 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3514 isl_bool ok;
3516 ok = isl_basic_map_compatible_range(bmap, bset);
3517 if (ok < 0)
3518 return isl_stat_error;
3519 if (!ok)
3520 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3521 "incompatible spaces", return isl_stat_error);
3523 return isl_stat_ok;
3526 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3527 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3529 struct isl_basic_map *bmap_range;
3530 isl_size dim;
3532 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3533 goto error;
3535 dim = isl_basic_set_dim(bset, isl_dim_set);
3536 if (dim < 0)
3537 goto error;
3538 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3539 goto error;
3541 if (isl_basic_set_plain_is_universe(bset)) {
3542 isl_basic_set_free(bset);
3543 return bmap;
3546 bmap = isl_basic_map_cow(bmap);
3547 if (!bmap)
3548 goto error;
3549 bmap = isl_basic_map_extend(bmap,
3550 bset->n_div, bset->n_eq, bset->n_ineq);
3551 bmap_range = bset_to_bmap(bset);
3552 bmap = add_constraints(bmap, bmap_range, 0, 0);
3554 bmap = isl_basic_map_simplify(bmap);
3555 return isl_basic_map_finalize(bmap);
3556 error:
3557 isl_basic_map_free(bmap);
3558 isl_basic_set_free(bset);
3559 return NULL;
3562 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3563 __isl_keep isl_vec *vec)
3565 int i;
3566 isl_size total;
3567 isl_int s;
3569 total = isl_basic_map_dim(bmap, isl_dim_all);
3570 if (total < 0 || !vec)
3571 return isl_bool_error;
3573 if (1 + total != vec->size)
3574 return isl_bool_false;
3576 isl_int_init(s);
3578 for (i = 0; i < bmap->n_eq; ++i) {
3579 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3580 if (!isl_int_is_zero(s)) {
3581 isl_int_clear(s);
3582 return isl_bool_false;
3586 for (i = 0; i < bmap->n_ineq; ++i) {
3587 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3588 if (isl_int_is_neg(s)) {
3589 isl_int_clear(s);
3590 return isl_bool_false;
3594 isl_int_clear(s);
3596 return isl_bool_true;
3599 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3600 __isl_keep isl_vec *vec)
3602 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3605 __isl_give isl_basic_map *isl_basic_map_intersect(
3606 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3608 struct isl_vec *sample = NULL;
3609 isl_space *space1, *space2;
3610 isl_size dim1, dim2, nparam1, nparam2;
3612 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3613 goto error;
3614 space1 = isl_basic_map_peek_space(bmap1);
3615 space2 = isl_basic_map_peek_space(bmap2);
3616 dim1 = isl_space_dim(space1, isl_dim_all);
3617 dim2 = isl_space_dim(space2, isl_dim_all);
3618 nparam1 = isl_space_dim(space1, isl_dim_param);
3619 nparam2 = isl_space_dim(space2, isl_dim_param);
3620 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3621 goto error;
3622 if (dim1 == nparam1 && dim2 != nparam2)
3623 return isl_basic_map_intersect(bmap2, bmap1);
3625 if (dim2 != nparam2 &&
3626 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3627 goto error;
3629 if (isl_basic_map_plain_is_empty(bmap1)) {
3630 isl_basic_map_free(bmap2);
3631 return bmap1;
3633 if (isl_basic_map_plain_is_empty(bmap2)) {
3634 isl_basic_map_free(bmap1);
3635 return bmap2;
3638 if (bmap1->sample &&
3639 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3640 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3641 sample = isl_vec_copy(bmap1->sample);
3642 else if (bmap2->sample &&
3643 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3644 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3645 sample = isl_vec_copy(bmap2->sample);
3647 bmap1 = isl_basic_map_cow(bmap1);
3648 if (!bmap1)
3649 goto error;
3650 bmap1 = isl_basic_map_extend(bmap1,
3651 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3652 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3654 if (!bmap1)
3655 isl_vec_free(sample);
3656 else if (sample) {
3657 isl_vec_free(bmap1->sample);
3658 bmap1->sample = sample;
3661 bmap1 = isl_basic_map_simplify(bmap1);
3662 return isl_basic_map_finalize(bmap1);
3663 error:
3664 if (sample)
3665 isl_vec_free(sample);
3666 isl_basic_map_free(bmap1);
3667 isl_basic_map_free(bmap2);
3668 return NULL;
3671 __isl_give isl_basic_set *isl_basic_set_intersect(
3672 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3674 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3675 bset_to_bmap(bset2)));
3678 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3679 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3681 return isl_basic_set_intersect(bset1, bset2);
3684 /* Special case of isl_map_intersect, where both map1 and map2
3685 * are convex, without any divs and such that either map1 or map2
3686 * contains a single constraint. This constraint is then simply
3687 * added to the other map.
3689 static __isl_give isl_map *map_intersect_add_constraint(
3690 __isl_take isl_map *map1, __isl_take isl_map *map2)
3692 isl_assert(map1->ctx, map1->n == 1, goto error);
3693 isl_assert(map2->ctx, map1->n == 1, goto error);
3694 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3695 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3697 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3698 return isl_map_intersect(map2, map1);
3700 map1 = isl_map_cow(map1);
3701 if (!map1)
3702 goto error;
3703 if (isl_map_plain_is_empty(map1)) {
3704 isl_map_free(map2);
3705 return map1;
3707 if (map2->p[0]->n_eq == 1)
3708 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3709 else
3710 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3711 map2->p[0]->ineq[0]);
3713 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3714 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3715 if (!map1->p[0])
3716 goto error;
3718 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3719 isl_basic_map_free(map1->p[0]);
3720 map1->n = 0;
3723 isl_map_free(map2);
3725 map1 = isl_map_unmark_normalized(map1);
3726 return map1;
3727 error:
3728 isl_map_free(map1);
3729 isl_map_free(map2);
3730 return NULL;
3733 /* map2 may be either a parameter domain or a map living in the same
3734 * space as map1.
3736 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3737 __isl_take isl_map *map2)
3739 unsigned flags = 0;
3740 isl_bool equal;
3741 isl_map *result;
3742 int i, j;
3743 isl_size dim2, nparam2;
3745 if (!map1 || !map2)
3746 goto error;
3748 if ((isl_map_plain_is_empty(map1) ||
3749 isl_map_plain_is_universe(map2)) &&
3750 isl_space_is_equal(map1->dim, map2->dim)) {
3751 isl_map_free(map2);
3752 return map1;
3754 if ((isl_map_plain_is_empty(map2) ||
3755 isl_map_plain_is_universe(map1)) &&
3756 isl_space_is_equal(map1->dim, map2->dim)) {
3757 isl_map_free(map1);
3758 return map2;
3761 if (map1->n == 1 && map2->n == 1 &&
3762 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3763 isl_space_is_equal(map1->dim, map2->dim) &&
3764 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3765 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3766 return map_intersect_add_constraint(map1, map2);
3768 equal = isl_map_plain_is_equal(map1, map2);
3769 if (equal < 0)
3770 goto error;
3771 if (equal) {
3772 isl_map_free(map2);
3773 return map1;
3776 dim2 = isl_map_dim(map2, isl_dim_all);
3777 nparam2 = isl_map_dim(map2, isl_dim_param);
3778 if (dim2 < 0 || nparam2 < 0)
3779 goto error;
3780 if (dim2 != nparam2)
3781 isl_assert(map1->ctx,
3782 isl_space_is_equal(map1->dim, map2->dim), goto error);
3784 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3785 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3786 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3788 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3789 map1->n * map2->n, flags);
3790 if (!result)
3791 goto error;
3792 for (i = 0; i < map1->n; ++i)
3793 for (j = 0; j < map2->n; ++j) {
3794 struct isl_basic_map *part;
3795 part = isl_basic_map_intersect(
3796 isl_basic_map_copy(map1->p[i]),
3797 isl_basic_map_copy(map2->p[j]));
3798 if (isl_basic_map_is_empty(part) < 0)
3799 part = isl_basic_map_free(part);
3800 result = isl_map_add_basic_map(result, part);
3801 if (!result)
3802 goto error;
3804 isl_map_free(map1);
3805 isl_map_free(map2);
3806 return result;
3807 error:
3808 isl_map_free(map1);
3809 isl_map_free(map2);
3810 return NULL;
3813 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3814 __isl_take isl_map *map2)
3816 if (isl_map_check_equal_space(map1, map2) < 0)
3817 goto error;
3818 return map_intersect_internal(map1, map2);
3819 error:
3820 isl_map_free(map1);
3821 isl_map_free(map2);
3822 return NULL;
3825 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3826 __isl_take isl_map *map2)
3828 isl_map_align_params_bin(&map1, &map2);
3829 return map_intersect(map1, map2);
3832 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3833 __isl_take isl_set *set2)
3835 return set_from_map(isl_map_intersect(set_to_map(set1),
3836 set_to_map(set2)));
3839 /* map_intersect_internal accepts intersections
3840 * with parameter domains, so we can just call that function.
3842 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3843 __isl_take isl_set *params)
3845 isl_map_align_params_set(&map, &params);
3846 return map_intersect_internal(map, params);
3849 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3850 __isl_take isl_set *params)
3852 return isl_map_intersect_params(set, params);
3855 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3857 isl_space *space;
3858 unsigned pos;
3859 isl_size n1, n2;
3861 if (!bmap)
3862 return NULL;
3863 bmap = isl_basic_map_cow(bmap);
3864 if (!bmap)
3865 return NULL;
3866 space = isl_space_reverse(isl_space_copy(bmap->dim));
3867 pos = isl_basic_map_offset(bmap, isl_dim_in);
3868 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3869 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3870 if (n1 < 0 || n2 < 0)
3871 bmap = isl_basic_map_free(bmap);
3872 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3873 return isl_basic_map_reset_space(bmap, space);
3876 /* Given a basic map A -> (B -> C), return the corresponding basic map
3877 * A -> (C -> B).
3879 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3880 __isl_take isl_basic_map *bmap)
3882 isl_space *space;
3883 isl_size offset, n1, n2;
3885 space = isl_basic_map_peek_space(bmap);
3886 if (isl_space_check_range_is_wrapping(space) < 0)
3887 return isl_basic_map_free(bmap);
3888 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3889 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3890 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3891 if (offset < 0 || n1 < 0 || n2 < 0)
3892 return isl_basic_map_free(bmap);
3894 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3896 space = isl_basic_map_take_space(bmap);
3897 space = isl_space_range_reverse(space);
3898 bmap = isl_basic_map_restore_space(bmap, space);
3900 return bmap;
3903 static __isl_give isl_basic_map *basic_map_space_reset(
3904 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3906 isl_space *space;
3908 if (!bmap)
3909 return NULL;
3910 if (!isl_space_is_named_or_nested(bmap->dim, type))
3911 return bmap;
3913 space = isl_basic_map_get_space(bmap);
3914 space = isl_space_reset(space, type);
3915 bmap = isl_basic_map_reset_space(bmap, space);
3916 return bmap;
3919 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3920 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3921 unsigned pos, unsigned n)
3923 isl_bool rational, is_empty;
3924 isl_space *res_space;
3925 struct isl_basic_map *res;
3926 struct isl_dim_map *dim_map;
3927 isl_size total;
3928 unsigned off;
3929 enum isl_dim_type t;
3931 if (n == 0)
3932 return basic_map_space_reset(bmap, type);
3934 is_empty = isl_basic_map_plain_is_empty(bmap);
3935 total = isl_basic_map_dim(bmap, isl_dim_all);
3936 if (is_empty < 0 || total < 0)
3937 return isl_basic_map_free(bmap);
3938 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3939 type, pos, n);
3940 if (!res_space)
3941 return isl_basic_map_free(bmap);
3942 if (is_empty) {
3943 isl_basic_map_free(bmap);
3944 return isl_basic_map_empty(res_space);
3947 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3948 off = 0;
3949 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3950 isl_size dim;
3952 if (t != type) {
3953 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3954 } else {
3955 isl_size size = isl_basic_map_dim(bmap, t);
3956 if (size < 0)
3957 dim_map = isl_dim_map_free(dim_map);
3958 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3959 0, pos, off);
3960 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3961 pos, size - pos, off + pos + n);
3963 dim = isl_space_dim(res_space, t);
3964 if (dim < 0)
3965 dim_map = isl_dim_map_free(dim_map);
3966 off += dim;
3968 isl_dim_map_div(dim_map, bmap, off);
3970 res = isl_basic_map_alloc_space(res_space,
3971 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3972 rational = isl_basic_map_is_rational(bmap);
3973 if (rational < 0)
3974 res = isl_basic_map_free(res);
3975 if (rational)
3976 res = isl_basic_map_set_rational(res);
3977 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3978 return isl_basic_map_finalize(res);
3981 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3982 __isl_take isl_basic_set *bset,
3983 enum isl_dim_type type, unsigned pos, unsigned n)
3985 return isl_basic_map_insert_dims(bset, type, pos, n);
3988 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3989 enum isl_dim_type type, unsigned n)
3991 isl_size dim;
3993 dim = isl_basic_map_dim(bmap, type);
3994 if (dim < 0)
3995 return isl_basic_map_free(bmap);
3996 return isl_basic_map_insert_dims(bmap, type, dim, n);
3999 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4000 enum isl_dim_type type, unsigned n)
4002 if (!bset)
4003 return NULL;
4004 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4005 return isl_basic_map_add_dims(bset, type, n);
4006 error:
4007 isl_basic_set_free(bset);
4008 return NULL;
4011 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4012 enum isl_dim_type type)
4014 isl_space *space;
4016 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4017 return map;
4019 space = isl_map_get_space(map);
4020 space = isl_space_reset(space, type);
4021 map = isl_map_reset_space(map, space);
4022 return map;
4025 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4026 enum isl_dim_type type, unsigned pos, unsigned n)
4028 int i;
4029 isl_space *space;
4031 if (n == 0)
4032 return map_space_reset(map, type);
4034 map = isl_map_cow(map);
4035 if (!map)
4036 return NULL;
4038 for (i = 0; i < map->n; ++i) {
4039 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4040 if (!map->p[i])
4041 goto error;
4044 space = isl_map_take_space(map);
4045 space = isl_space_insert_dims(space, type, pos, n);
4046 map = isl_map_restore_space(map, space);
4048 return map;
4049 error:
4050 isl_map_free(map);
4051 return NULL;
4054 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4055 enum isl_dim_type type, unsigned pos, unsigned n)
4057 return isl_map_insert_dims(set, type, pos, n);
4060 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4061 enum isl_dim_type type, unsigned n)
4063 isl_size dim;
4065 dim = isl_map_dim(map, type);
4066 if (dim < 0)
4067 return isl_map_free(map);
4068 return isl_map_insert_dims(map, type, dim, n);
4071 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4072 enum isl_dim_type type, unsigned n)
4074 if (!set)
4075 return NULL;
4076 isl_assert(set->ctx, type != isl_dim_in, goto error);
4077 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4078 error:
4079 isl_set_free(set);
4080 return NULL;
4083 __isl_give isl_basic_map *isl_basic_map_move_dims(
4084 __isl_take isl_basic_map *bmap,
4085 enum isl_dim_type dst_type, unsigned dst_pos,
4086 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4088 isl_space *space;
4089 struct isl_dim_map *dim_map;
4090 struct isl_basic_map *res;
4091 enum isl_dim_type t;
4092 isl_size total;
4093 unsigned off;
4095 if (!bmap)
4096 return NULL;
4097 if (n == 0) {
4098 bmap = isl_basic_map_reset(bmap, src_type);
4099 bmap = isl_basic_map_reset(bmap, dst_type);
4100 return bmap;
4103 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4104 return isl_basic_map_free(bmap);
4106 if (dst_type == src_type && dst_pos == src_pos)
4107 return bmap;
4109 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4111 if (pos(bmap->dim, dst_type) + dst_pos ==
4112 pos(bmap->dim, src_type) + src_pos +
4113 ((src_type < dst_type) ? n : 0)) {
4114 space = isl_basic_map_take_space(bmap);
4115 space = isl_space_move_dims(space, dst_type, dst_pos,
4116 src_type, src_pos, n);
4117 bmap = isl_basic_map_restore_space(bmap, space);
4118 bmap = isl_basic_map_finalize(bmap);
4120 return bmap;
4123 total = isl_basic_map_dim(bmap, isl_dim_all);
4124 if (total < 0)
4125 return isl_basic_map_free(bmap);
4126 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4128 off = 0;
4129 space = isl_basic_map_peek_space(bmap);
4130 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4131 isl_size size = isl_space_dim(space, t);
4132 if (size < 0)
4133 dim_map = isl_dim_map_free(dim_map);
4134 if (t == dst_type) {
4135 isl_dim_map_dim_range(dim_map, space, t,
4136 0, dst_pos, off);
4137 off += dst_pos;
4138 isl_dim_map_dim_range(dim_map, space, src_type,
4139 src_pos, n, off);
4140 off += n;
4141 isl_dim_map_dim_range(dim_map, space, t,
4142 dst_pos, size - dst_pos, off);
4143 off += size - dst_pos;
4144 } else if (t == src_type) {
4145 isl_dim_map_dim_range(dim_map, space, t,
4146 0, src_pos, off);
4147 off += src_pos;
4148 isl_dim_map_dim_range(dim_map, space, t,
4149 src_pos + n, size - src_pos - n, off);
4150 off += size - src_pos - n;
4151 } else {
4152 isl_dim_map_dim(dim_map, space, t, off);
4153 off += size;
4156 isl_dim_map_div(dim_map, bmap, off);
4158 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4159 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4160 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4161 space = isl_basic_map_take_space(bmap);
4162 space = isl_space_move_dims(space, dst_type, dst_pos,
4163 src_type, src_pos, n);
4164 bmap = isl_basic_map_restore_space(bmap, space);
4165 if (!bmap)
4166 goto error;
4168 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4169 bmap = isl_basic_map_gauss(bmap, NULL);
4170 bmap = isl_basic_map_finalize(bmap);
4172 return bmap;
4173 error:
4174 isl_basic_map_free(bmap);
4175 return NULL;
4178 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4179 enum isl_dim_type dst_type, unsigned dst_pos,
4180 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4182 isl_basic_map *bmap = bset_to_bmap(bset);
4183 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4184 src_type, src_pos, n);
4185 return bset_from_bmap(bmap);
4188 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4189 enum isl_dim_type dst_type, unsigned dst_pos,
4190 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4192 if (!set)
4193 return NULL;
4194 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4195 return set_from_map(isl_map_move_dims(set_to_map(set),
4196 dst_type, dst_pos, src_type, src_pos, n));
4197 error:
4198 isl_set_free(set);
4199 return NULL;
4202 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4203 enum isl_dim_type dst_type, unsigned dst_pos,
4204 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4206 int i;
4207 isl_space *space;
4209 if (n == 0) {
4210 map = isl_map_reset(map, src_type);
4211 map = isl_map_reset(map, dst_type);
4212 return map;
4215 if (isl_map_check_range(map, src_type, src_pos, n))
4216 return isl_map_free(map);
4218 if (dst_type == src_type && dst_pos == src_pos)
4219 return map;
4221 isl_assert(map->ctx, dst_type != src_type, goto error);
4223 map = isl_map_cow(map);
4224 if (!map)
4225 return NULL;
4227 for (i = 0; i < map->n; ++i) {
4228 map->p[i] = isl_basic_map_move_dims(map->p[i],
4229 dst_type, dst_pos,
4230 src_type, src_pos, n);
4231 if (!map->p[i])
4232 goto error;
4235 space = isl_map_take_space(map);
4236 space = isl_space_move_dims(space, dst_type, dst_pos,
4237 src_type, src_pos, n);
4238 map = isl_map_restore_space(map, space);
4240 return map;
4241 error:
4242 isl_map_free(map);
4243 return NULL;
4246 /* Move the specified dimensions to the last columns right before
4247 * the divs. Don't change the dimension specification of bmap.
4248 * That's the responsibility of the caller.
4250 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4251 enum isl_dim_type type, unsigned first, unsigned n)
4253 isl_space *space;
4254 struct isl_dim_map *dim_map;
4255 struct isl_basic_map *res;
4256 enum isl_dim_type t;
4257 isl_size total;
4258 unsigned off;
4260 if (!bmap)
4261 return NULL;
4262 if (isl_basic_map_offset(bmap, type) + first + n ==
4263 isl_basic_map_offset(bmap, isl_dim_div))
4264 return bmap;
4266 total = isl_basic_map_dim(bmap, isl_dim_all);
4267 if (total < 0)
4268 return isl_basic_map_free(bmap);
4269 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4271 off = 0;
4272 space = isl_basic_map_peek_space(bmap);
4273 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4274 isl_size size = isl_space_dim(space, t);
4275 if (size < 0)
4276 dim_map = isl_dim_map_free(dim_map);
4277 if (t == type) {
4278 isl_dim_map_dim_range(dim_map, space, t,
4279 0, first, off);
4280 off += first;
4281 isl_dim_map_dim_range(dim_map, space, t,
4282 first, n, total - bmap->n_div - n);
4283 isl_dim_map_dim_range(dim_map, space, t,
4284 first + n, size - (first + n), off);
4285 off += size - (first + n);
4286 } else {
4287 isl_dim_map_dim(dim_map, space, t, off);
4288 off += size;
4291 isl_dim_map_div(dim_map, bmap, off + n);
4293 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4294 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4295 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4296 return res;
4299 /* Insert "n" rows in the divs of "bmap".
4301 * The number of columns is not changed, which means that the last
4302 * dimensions of "bmap" are being reintepreted as the new divs.
4303 * The space of "bmap" is not adjusted, however, which means
4304 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4305 * from the space of "bmap" is the responsibility of the caller.
4307 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4308 int n)
4310 int i;
4311 size_t row_size;
4312 isl_int **new_div;
4313 isl_int *old;
4315 bmap = isl_basic_map_cow(bmap);
4316 if (!bmap)
4317 return NULL;
4319 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4320 old = bmap->block2.data;
4321 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4322 (bmap->extra + n) * (1 + row_size));
4323 if (!bmap->block2.data)
4324 return isl_basic_map_free(bmap);
4325 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4326 if (!new_div)
4327 return isl_basic_map_free(bmap);
4328 for (i = 0; i < n; ++i) {
4329 new_div[i] = bmap->block2.data +
4330 (bmap->extra + i) * (1 + row_size);
4331 isl_seq_clr(new_div[i], 1 + row_size);
4333 for (i = 0; i < bmap->extra; ++i)
4334 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4335 free(bmap->div);
4336 bmap->div = new_div;
4337 bmap->n_div += n;
4338 bmap->extra += n;
4340 return bmap;
4343 /* Drop constraints from "bmap" that only involve the variables
4344 * of "type" in the range [first, first + n] that are not related
4345 * to any of the variables outside that interval.
4346 * These constraints cannot influence the values for the variables
4347 * outside the interval, except in case they cause "bmap" to be empty.
4348 * Only drop the constraints if "bmap" is known to be non-empty.
4350 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4351 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4352 unsigned first, unsigned n)
4354 int i;
4355 int *groups;
4356 isl_size dim, n_div;
4357 isl_bool non_empty;
4359 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4360 if (non_empty < 0)
4361 return isl_basic_map_free(bmap);
4362 if (!non_empty)
4363 return bmap;
4365 dim = isl_basic_map_dim(bmap, isl_dim_all);
4366 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4367 if (dim < 0 || n_div < 0)
4368 return isl_basic_map_free(bmap);
4369 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4370 if (!groups)
4371 return isl_basic_map_free(bmap);
4372 first += isl_basic_map_offset(bmap, type) - 1;
4373 for (i = 0; i < first; ++i)
4374 groups[i] = -1;
4375 for (i = first + n; i < dim - n_div; ++i)
4376 groups[i] = -1;
4378 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4380 return bmap;
4383 /* Turn the n dimensions of type type, starting at first
4384 * into existentially quantified variables.
4386 * If a subset of the projected out variables are unrelated
4387 * to any of the variables that remain, then the constraints
4388 * involving this subset are simply dropped first.
4390 __isl_give isl_basic_map *isl_basic_map_project_out(
4391 __isl_take isl_basic_map *bmap,
4392 enum isl_dim_type type, unsigned first, unsigned n)
4394 isl_bool empty;
4395 isl_space *space;
4397 if (n == 0)
4398 return basic_map_space_reset(bmap, type);
4399 if (type == isl_dim_div)
4400 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4401 "cannot project out existentially quantified variables",
4402 return isl_basic_map_free(bmap));
4404 empty = isl_basic_map_plain_is_empty(bmap);
4405 if (empty < 0)
4406 return isl_basic_map_free(bmap);
4407 if (empty)
4408 bmap = isl_basic_map_set_to_empty(bmap);
4410 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4411 if (!bmap)
4412 return NULL;
4414 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4415 return isl_basic_map_remove_dims(bmap, type, first, n);
4417 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4418 return isl_basic_map_free(bmap);
4420 bmap = move_last(bmap, type, first, n);
4421 bmap = isl_basic_map_cow(bmap);
4422 bmap = insert_div_rows(bmap, n);
4424 space = isl_basic_map_take_space(bmap);
4425 space = isl_space_drop_dims(space, type, first, n);
4426 bmap = isl_basic_map_restore_space(bmap, space);
4427 bmap = isl_basic_map_simplify(bmap);
4428 bmap = isl_basic_map_drop_redundant_divs(bmap);
4429 return isl_basic_map_finalize(bmap);
4432 /* Turn the n dimensions of type type, starting at first
4433 * into existentially quantified variables.
4435 __isl_give isl_basic_set *isl_basic_set_project_out(
4436 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4437 unsigned first, unsigned n)
4439 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4440 type, first, n));
4443 /* Turn the n dimensions of type type, starting at first
4444 * into existentially quantified variables.
4446 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4447 enum isl_dim_type type, unsigned first, unsigned n)
4449 int i;
4450 isl_space *space;
4452 if (n == 0)
4453 return map_space_reset(map, type);
4455 if (isl_map_check_range(map, type, first, n) < 0)
4456 return isl_map_free(map);
4458 map = isl_map_cow(map);
4459 if (!map)
4460 return NULL;
4462 for (i = 0; i < map->n; ++i) {
4463 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4464 if (!map->p[i])
4465 goto error;
4468 if (map->n > 1)
4469 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4470 map = isl_map_unmark_normalized(map);
4472 space = isl_map_take_space(map);
4473 space = isl_space_drop_dims(space, type, first, n);
4474 map = isl_map_restore_space(map, space);
4476 return map;
4477 error:
4478 isl_map_free(map);
4479 return NULL;
4482 #undef TYPE
4483 #define TYPE isl_map
4484 #include "isl_project_out_all_params_templ.c"
4486 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4487 * into existentially quantified variables.
4489 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4490 enum isl_dim_type type, unsigned first, unsigned n)
4492 isl_size dim;
4494 dim = isl_map_dim(map, type);
4495 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4496 return isl_map_free(map);
4497 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4498 map = isl_map_project_out(map, type, 0, first);
4499 return map;
4502 /* Turn the n dimensions of type type, starting at first
4503 * into existentially quantified variables.
4505 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4506 enum isl_dim_type type, unsigned first, unsigned n)
4508 return set_from_map(isl_map_project_out(set_to_map(set),
4509 type, first, n));
4512 /* If "set" involves a parameter with identifier "id",
4513 * then turn it into an existentially quantified variable.
4515 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4516 __isl_take isl_id *id)
4518 int pos;
4520 if (!set || !id)
4521 goto error;
4522 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4523 isl_id_free(id);
4524 if (pos < 0)
4525 return set;
4526 return isl_set_project_out(set, isl_dim_param, pos, 1);
4527 error:
4528 isl_set_free(set);
4529 isl_id_free(id);
4530 return NULL;
4533 /* If "set" involves any of the parameters with identifiers in "list",
4534 * then turn them into existentially quantified variables.
4536 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4537 __isl_take isl_id_list *list)
4539 int i;
4540 isl_size n;
4542 n = isl_id_list_size(list);
4543 if (n < 0)
4544 goto error;
4545 for (i = 0; i < n; ++i) {
4546 isl_id *id;
4548 id = isl_id_list_get_at(list, i);
4549 set = isl_set_project_out_param_id(set, id);
4552 isl_id_list_free(list);
4553 return set;
4554 error:
4555 isl_id_list_free(list);
4556 isl_set_free(set);
4557 return NULL;
4560 /* Project out all parameters from "set" by existentially quantifying
4561 * over them.
4563 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4565 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4568 /* Return a map that projects the elements in "set" onto their
4569 * "n" set dimensions starting at "first".
4570 * "type" should be equal to isl_dim_set.
4572 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4573 enum isl_dim_type type, unsigned first, unsigned n)
4575 int i;
4576 isl_map *map;
4578 if (type != isl_dim_set)
4579 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4580 "only set dimensions can be projected out", goto error);
4581 if (isl_set_check_range(set, type, first, n) < 0)
4582 return isl_set_free(set);
4584 map = isl_map_from_domain(set);
4585 map = isl_map_add_dims(map, isl_dim_out, n);
4586 for (i = 0; i < n; ++i)
4587 map = isl_map_equate(map, isl_dim_in, first + i,
4588 isl_dim_out, i);
4589 return map;
4590 error:
4591 isl_set_free(set);
4592 return NULL;
4595 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4596 unsigned n)
4598 int i, j;
4599 isl_size total;
4601 total = isl_basic_map_dim(bmap, isl_dim_all);
4602 if (total < 0)
4603 return isl_basic_map_free(bmap);
4604 for (i = 0; i < n; ++i) {
4605 j = isl_basic_map_alloc_div(bmap);
4606 if (j < 0)
4607 goto error;
4608 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4610 return bmap;
4611 error:
4612 isl_basic_map_free(bmap);
4613 return NULL;
4616 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4618 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4619 __isl_keep isl_basic_map *bmap2)
4621 isl_space *space1, *space2;
4623 space1 = isl_basic_map_peek_space(bmap1);
4624 space2 = isl_basic_map_peek_space(bmap2);
4625 return isl_space_tuple_is_equal(space1, isl_dim_out,
4626 space2, isl_dim_in);
4629 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4631 static isl_stat isl_basic_map_check_applies_range(
4632 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4634 isl_bool equal;
4636 equal = isl_basic_map_applies_range(bmap1, bmap2);
4637 if (equal < 0)
4638 return isl_stat_error;
4639 if (!equal)
4640 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4641 "spaces don't match", return isl_stat_error);
4642 return isl_stat_ok;
4645 __isl_give isl_basic_map *isl_basic_map_apply_range(
4646 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4648 isl_space *space_result = NULL;
4649 struct isl_basic_map *bmap;
4650 isl_size n_in, n_out, n, nparam;
4651 unsigned total, pos;
4652 struct isl_dim_map *dim_map1, *dim_map2;
4654 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4655 goto error;
4656 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4657 goto error;
4659 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4660 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4661 n = isl_basic_map_dim(bmap1, isl_dim_out);
4662 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4663 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4664 goto error;
4666 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4667 isl_basic_map_get_space(bmap2));
4669 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4670 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4671 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4672 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4673 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4674 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4675 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4676 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4677 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4678 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4679 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4681 bmap = isl_basic_map_alloc_space(space_result,
4682 bmap1->n_div + bmap2->n_div + n,
4683 bmap1->n_eq + bmap2->n_eq,
4684 bmap1->n_ineq + bmap2->n_ineq);
4685 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4686 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4687 bmap = add_divs(bmap, n);
4688 bmap = isl_basic_map_simplify(bmap);
4689 bmap = isl_basic_map_drop_redundant_divs(bmap);
4690 return isl_basic_map_finalize(bmap);
4691 error:
4692 isl_basic_map_free(bmap1);
4693 isl_basic_map_free(bmap2);
4694 return NULL;
4697 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4698 __isl_take isl_basic_map *bmap)
4700 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4701 goto error;
4703 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4704 bmap));
4705 error:
4706 isl_basic_set_free(bset);
4707 isl_basic_map_free(bmap);
4708 return NULL;
4711 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4712 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4714 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4715 goto error;
4716 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4717 bmap2->dim, isl_dim_in))
4718 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4719 "spaces don't match", goto error);
4721 bmap1 = isl_basic_map_reverse(bmap1);
4722 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4723 return isl_basic_map_reverse(bmap1);
4724 error:
4725 isl_basic_map_free(bmap1);
4726 isl_basic_map_free(bmap2);
4727 return NULL;
4730 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4731 * A \cap B -> f(A) + f(B)
4733 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4734 __isl_take isl_basic_map *bmap2)
4736 isl_size n_in, n_out, nparam;
4737 unsigned total, pos;
4738 struct isl_basic_map *bmap = NULL;
4739 struct isl_dim_map *dim_map1, *dim_map2;
4740 int i;
4742 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4743 goto error;
4745 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4746 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4747 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4748 if (nparam < 0 || n_in < 0 || n_out < 0)
4749 goto error;
4751 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4752 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4753 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4754 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4755 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4756 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4757 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4758 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4759 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4760 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4761 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4763 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4764 bmap1->n_div + bmap2->n_div + 2 * n_out,
4765 bmap1->n_eq + bmap2->n_eq + n_out,
4766 bmap1->n_ineq + bmap2->n_ineq);
4767 for (i = 0; i < n_out; ++i) {
4768 int j = isl_basic_map_alloc_equality(bmap);
4769 if (j < 0)
4770 goto error;
4771 isl_seq_clr(bmap->eq[j], 1+total);
4772 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4773 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4774 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4776 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4777 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4778 bmap = add_divs(bmap, 2 * n_out);
4780 bmap = isl_basic_map_simplify(bmap);
4781 return isl_basic_map_finalize(bmap);
4782 error:
4783 isl_basic_map_free(bmap);
4784 isl_basic_map_free(bmap1);
4785 isl_basic_map_free(bmap2);
4786 return NULL;
4789 /* Given two maps A -> f(A) and B -> g(B), construct a map
4790 * A \cap B -> f(A) + f(B)
4792 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4793 __isl_take isl_map *map2)
4795 struct isl_map *result;
4796 int i, j;
4798 if (isl_map_check_equal_space(map1, map2) < 0)
4799 goto error;
4801 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4802 map1->n * map2->n, 0);
4803 if (!result)
4804 goto error;
4805 for (i = 0; i < map1->n; ++i)
4806 for (j = 0; j < map2->n; ++j) {
4807 struct isl_basic_map *part;
4808 part = isl_basic_map_sum(
4809 isl_basic_map_copy(map1->p[i]),
4810 isl_basic_map_copy(map2->p[j]));
4811 if (isl_basic_map_is_empty(part))
4812 isl_basic_map_free(part);
4813 else
4814 result = isl_map_add_basic_map(result, part);
4815 if (!result)
4816 goto error;
4818 isl_map_free(map1);
4819 isl_map_free(map2);
4820 return result;
4821 error:
4822 isl_map_free(map1);
4823 isl_map_free(map2);
4824 return NULL;
4827 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4828 __isl_take isl_set *set2)
4830 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4833 /* Given a basic map A -> f(A), construct A -> -f(A).
4835 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4837 int i, j;
4838 unsigned off;
4839 isl_size n;
4841 bmap = isl_basic_map_cow(bmap);
4842 n = isl_basic_map_dim(bmap, isl_dim_out);
4843 if (n < 0)
4844 return isl_basic_map_free(bmap);
4846 off = isl_basic_map_offset(bmap, isl_dim_out);
4847 for (i = 0; i < bmap->n_eq; ++i)
4848 for (j = 0; j < n; ++j)
4849 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4850 for (i = 0; i < bmap->n_ineq; ++i)
4851 for (j = 0; j < n; ++j)
4852 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4853 for (i = 0; i < bmap->n_div; ++i)
4854 for (j = 0; j < n; ++j)
4855 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4856 bmap = isl_basic_map_gauss(bmap, NULL);
4857 return isl_basic_map_finalize(bmap);
4860 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4862 return isl_basic_map_neg(bset);
4865 /* Given a map A -> f(A), construct A -> -f(A).
4867 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4869 int i;
4871 map = isl_map_cow(map);
4872 if (!map)
4873 return NULL;
4875 for (i = 0; i < map->n; ++i) {
4876 map->p[i] = isl_basic_map_neg(map->p[i]);
4877 if (!map->p[i])
4878 goto error;
4881 return map;
4882 error:
4883 isl_map_free(map);
4884 return NULL;
4887 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4889 return set_from_map(isl_map_neg(set_to_map(set)));
4892 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4893 * A -> floor(f(A)/d).
4895 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4896 isl_int d)
4898 isl_size n_in, n_out, nparam;
4899 unsigned total, pos;
4900 struct isl_basic_map *result = NULL;
4901 struct isl_dim_map *dim_map;
4902 int i;
4904 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4905 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4906 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4907 if (nparam < 0 || n_in < 0 || n_out < 0)
4908 return isl_basic_map_free(bmap);
4910 total = nparam + n_in + n_out + bmap->n_div + n_out;
4911 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4912 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4913 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4914 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4915 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4917 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4918 bmap->n_div + n_out,
4919 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4920 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4921 result = add_divs(result, n_out);
4922 for (i = 0; i < n_out; ++i) {
4923 int j;
4924 j = isl_basic_map_alloc_inequality(result);
4925 if (j < 0)
4926 goto error;
4927 isl_seq_clr(result->ineq[j], 1+total);
4928 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4929 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4930 j = isl_basic_map_alloc_inequality(result);
4931 if (j < 0)
4932 goto error;
4933 isl_seq_clr(result->ineq[j], 1+total);
4934 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4935 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4936 isl_int_sub_ui(result->ineq[j][0], d, 1);
4939 result = isl_basic_map_simplify(result);
4940 return isl_basic_map_finalize(result);
4941 error:
4942 isl_basic_map_free(result);
4943 return NULL;
4946 /* Given a map A -> f(A) and an integer d, construct a map
4947 * A -> floor(f(A)/d).
4949 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4951 int i;
4953 map = isl_map_cow(map);
4954 if (!map)
4955 return NULL;
4957 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4958 for (i = 0; i < map->n; ++i) {
4959 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4960 if (!map->p[i])
4961 goto error;
4963 map = isl_map_unmark_normalized(map);
4965 return map;
4966 error:
4967 isl_map_free(map);
4968 return NULL;
4971 /* Given a map A -> f(A) and an integer d, construct a map
4972 * A -> floor(f(A)/d).
4974 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4975 __isl_take isl_val *d)
4977 if (!map || !d)
4978 goto error;
4979 if (!isl_val_is_int(d))
4980 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4981 "expecting integer denominator", goto error);
4982 map = isl_map_floordiv(map, d->n);
4983 isl_val_free(d);
4984 return map;
4985 error:
4986 isl_map_free(map);
4987 isl_val_free(d);
4988 return NULL;
4991 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4992 unsigned pos)
4994 int i;
4995 isl_size nparam;
4996 isl_size n_in;
4997 isl_size total;
4999 total = isl_basic_map_dim(bmap, isl_dim_all);
5000 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5001 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5002 if (total < 0 || nparam < 0 || n_in < 0)
5003 return isl_basic_map_free(bmap);
5004 i = isl_basic_map_alloc_equality(bmap);
5005 if (i < 0)
5006 goto error;
5007 isl_seq_clr(bmap->eq[i], 1 + total);
5008 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5009 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5010 return isl_basic_map_finalize(bmap);
5011 error:
5012 isl_basic_map_free(bmap);
5013 return NULL;
5016 /* Add a constraint to "bmap" expressing i_pos < o_pos
5018 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5019 unsigned pos)
5021 int i;
5022 isl_size nparam;
5023 isl_size n_in;
5024 isl_size total;
5026 total = isl_basic_map_dim(bmap, isl_dim_all);
5027 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5028 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5029 if (total < 0 || nparam < 0 || n_in < 0)
5030 return isl_basic_map_free(bmap);
5031 i = isl_basic_map_alloc_inequality(bmap);
5032 if (i < 0)
5033 goto error;
5034 isl_seq_clr(bmap->ineq[i], 1 + total);
5035 isl_int_set_si(bmap->ineq[i][0], -1);
5036 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5037 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5038 return isl_basic_map_finalize(bmap);
5039 error:
5040 isl_basic_map_free(bmap);
5041 return NULL;
5044 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5046 static __isl_give isl_basic_map *var_less_or_equal(
5047 __isl_take isl_basic_map *bmap, unsigned pos)
5049 int i;
5050 isl_size nparam;
5051 isl_size n_in;
5052 isl_size total;
5054 total = isl_basic_map_dim(bmap, isl_dim_all);
5055 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5056 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5057 if (total < 0 || nparam < 0 || n_in < 0)
5058 return isl_basic_map_free(bmap);
5059 i = isl_basic_map_alloc_inequality(bmap);
5060 if (i < 0)
5061 goto error;
5062 isl_seq_clr(bmap->ineq[i], 1 + total);
5063 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5064 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5065 return isl_basic_map_finalize(bmap);
5066 error:
5067 isl_basic_map_free(bmap);
5068 return NULL;
5071 /* Add a constraint to "bmap" expressing i_pos > o_pos
5073 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5074 unsigned pos)
5076 int i;
5077 isl_size nparam;
5078 isl_size n_in;
5079 isl_size total;
5081 total = isl_basic_map_dim(bmap, isl_dim_all);
5082 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5083 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5084 if (total < 0 || nparam < 0 || n_in < 0)
5085 return isl_basic_map_free(bmap);
5086 i = isl_basic_map_alloc_inequality(bmap);
5087 if (i < 0)
5088 goto error;
5089 isl_seq_clr(bmap->ineq[i], 1 + total);
5090 isl_int_set_si(bmap->ineq[i][0], -1);
5091 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5092 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5093 return isl_basic_map_finalize(bmap);
5094 error:
5095 isl_basic_map_free(bmap);
5096 return NULL;
5099 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5101 static __isl_give isl_basic_map *var_more_or_equal(
5102 __isl_take isl_basic_map *bmap, unsigned pos)
5104 int i;
5105 isl_size nparam;
5106 isl_size n_in;
5107 isl_size total;
5109 total = isl_basic_map_dim(bmap, isl_dim_all);
5110 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5111 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5112 if (total < 0 || nparam < 0 || n_in < 0)
5113 return isl_basic_map_free(bmap);
5114 i = isl_basic_map_alloc_inequality(bmap);
5115 if (i < 0)
5116 goto error;
5117 isl_seq_clr(bmap->ineq[i], 1 + total);
5118 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5119 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5120 return isl_basic_map_finalize(bmap);
5121 error:
5122 isl_basic_map_free(bmap);
5123 return NULL;
5126 __isl_give isl_basic_map *isl_basic_map_equal(
5127 __isl_take isl_space *space, unsigned n_equal)
5129 int i;
5130 struct isl_basic_map *bmap;
5131 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5132 if (!bmap)
5133 return NULL;
5134 for (i = 0; i < n_equal && bmap; ++i)
5135 bmap = var_equal(bmap, i);
5136 return isl_basic_map_finalize(bmap);
5139 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5141 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5142 unsigned pos)
5144 int i;
5145 struct isl_basic_map *bmap;
5146 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5147 if (!bmap)
5148 return NULL;
5149 for (i = 0; i < pos && bmap; ++i)
5150 bmap = var_equal(bmap, i);
5151 if (bmap)
5152 bmap = var_less(bmap, pos);
5153 return isl_basic_map_finalize(bmap);
5156 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5158 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5159 __isl_take isl_space *space, unsigned pos)
5161 int i;
5162 isl_basic_map *bmap;
5164 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5165 for (i = 0; i < pos; ++i)
5166 bmap = var_equal(bmap, i);
5167 bmap = var_less_or_equal(bmap, pos);
5168 return isl_basic_map_finalize(bmap);
5171 /* Return a relation on "space" expressing i_pos > o_pos
5173 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5174 unsigned pos)
5176 int i;
5177 struct isl_basic_map *bmap;
5178 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5179 if (!bmap)
5180 return NULL;
5181 for (i = 0; i < pos && bmap; ++i)
5182 bmap = var_equal(bmap, i);
5183 if (bmap)
5184 bmap = var_more(bmap, pos);
5185 return isl_basic_map_finalize(bmap);
5188 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5190 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5191 __isl_take isl_space *space, unsigned pos)
5193 int i;
5194 isl_basic_map *bmap;
5196 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5197 for (i = 0; i < pos; ++i)
5198 bmap = var_equal(bmap, i);
5199 bmap = var_more_or_equal(bmap, pos);
5200 return isl_basic_map_finalize(bmap);
5203 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5204 unsigned n, int equal)
5206 struct isl_map *map;
5207 int i;
5209 if (n == 0 && equal)
5210 return isl_map_universe(space);
5212 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5214 for (i = 0; i + 1 < n; ++i)
5215 map = isl_map_add_basic_map(map,
5216 isl_basic_map_less_at(isl_space_copy(space), i));
5217 if (n > 0) {
5218 if (equal)
5219 map = isl_map_add_basic_map(map,
5220 isl_basic_map_less_or_equal_at(space, n - 1));
5221 else
5222 map = isl_map_add_basic_map(map,
5223 isl_basic_map_less_at(space, n - 1));
5224 } else
5225 isl_space_free(space);
5227 return map;
5230 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5232 if (!space)
5233 return NULL;
5234 return map_lex_lte_first(space, space->n_out, equal);
5237 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5238 unsigned n)
5240 return map_lex_lte_first(space, n, 0);
5243 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5244 unsigned n)
5246 return map_lex_lte_first(space, n, 1);
5249 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5251 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5254 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5256 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5259 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5260 unsigned n, int equal)
5262 struct isl_map *map;
5263 int i;
5265 if (n == 0 && equal)
5266 return isl_map_universe(space);
5268 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5270 for (i = 0; i + 1 < n; ++i)
5271 map = isl_map_add_basic_map(map,
5272 isl_basic_map_more_at(isl_space_copy(space), i));
5273 if (n > 0) {
5274 if (equal)
5275 map = isl_map_add_basic_map(map,
5276 isl_basic_map_more_or_equal_at(space, n - 1));
5277 else
5278 map = isl_map_add_basic_map(map,
5279 isl_basic_map_more_at(space, n - 1));
5280 } else
5281 isl_space_free(space);
5283 return map;
5286 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5288 if (!space)
5289 return NULL;
5290 return map_lex_gte_first(space, space->n_out, equal);
5293 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5294 unsigned n)
5296 return map_lex_gte_first(space, n, 0);
5299 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5300 unsigned n)
5302 return map_lex_gte_first(space, n, 1);
5305 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5307 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5310 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5312 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5315 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5316 __isl_take isl_set *set2)
5318 isl_map *map;
5319 map = isl_map_lex_le(isl_set_get_space(set1));
5320 map = isl_map_intersect_domain(map, set1);
5321 map = isl_map_intersect_range(map, set2);
5322 return map;
5325 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5326 __isl_take isl_set *set2)
5328 isl_map *map;
5329 map = isl_map_lex_lt(isl_set_get_space(set1));
5330 map = isl_map_intersect_domain(map, set1);
5331 map = isl_map_intersect_range(map, set2);
5332 return map;
5335 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5336 __isl_take isl_set *set2)
5338 isl_map *map;
5339 map = isl_map_lex_ge(isl_set_get_space(set1));
5340 map = isl_map_intersect_domain(map, set1);
5341 map = isl_map_intersect_range(map, set2);
5342 return map;
5345 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5346 __isl_take isl_set *set2)
5348 isl_map *map;
5349 map = isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map *map1,
5356 __isl_take isl_map *map2)
5358 isl_map *map;
5359 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5360 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5361 map = isl_map_apply_range(map, isl_map_reverse(map2));
5362 return map;
5365 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5366 __isl_take isl_map *map2)
5368 isl_map *map;
5369 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5370 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5371 map = isl_map_apply_range(map, isl_map_reverse(map2));
5372 return map;
5375 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5376 __isl_take isl_map *map2)
5378 isl_map *map;
5379 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5380 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5381 map = isl_map_apply_range(map, isl_map_reverse(map2));
5382 return map;
5385 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5386 __isl_take isl_map *map2)
5388 isl_map *map;
5389 map = isl_map_lex_gt(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 /* For the div d = floor(f/m) at position "div", add the constraint
5397 * f - m d >= 0
5399 static __isl_give isl_basic_map *add_upper_div_constraint(
5400 __isl_take isl_basic_map *bmap, unsigned div)
5402 int i;
5403 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5404 isl_size n_div;
5405 unsigned pos;
5407 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5408 if (v_div < 0 || n_div < 0)
5409 return isl_basic_map_free(bmap);
5410 pos = v_div + div;
5411 i = isl_basic_map_alloc_inequality(bmap);
5412 if (i < 0)
5413 return isl_basic_map_free(bmap);
5414 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5415 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5417 return bmap;
5420 /* For the div d = floor(f/m) at position "div", add the constraint
5422 * -(f-(m-1)) + m d >= 0
5424 static __isl_give isl_basic_map *add_lower_div_constraint(
5425 __isl_take isl_basic_map *bmap, unsigned div)
5427 int i;
5428 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5429 isl_size n_div;
5430 unsigned pos;
5432 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5433 if (v_div < 0 || n_div < 0)
5434 return isl_basic_map_free(bmap);
5435 pos = v_div + div;
5436 i = isl_basic_map_alloc_inequality(bmap);
5437 if (i < 0)
5438 return isl_basic_map_free(bmap);
5439 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5440 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5441 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5442 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5444 return bmap;
5447 /* For the div d = floor(f/m) at position "pos", add the constraints
5449 * f - m d >= 0
5450 * -(f-(m-1)) + m d >= 0
5452 * Note that the second constraint is the negation of
5454 * f - m d >= m
5456 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5457 __isl_take isl_basic_map *bmap, unsigned pos)
5459 bmap = add_upper_div_constraint(bmap, pos);
5460 bmap = add_lower_div_constraint(bmap, pos);
5461 return bmap;
5464 /* For each known div d = floor(f/m), add the constraints
5466 * f - m d >= 0
5467 * -(f-(m-1)) + m d >= 0
5469 * Remove duplicate constraints in case of some these div constraints
5470 * already appear in "bmap".
5472 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5473 __isl_take isl_basic_map *bmap)
5475 isl_size n_div;
5477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5478 if (n_div < 0)
5479 return isl_basic_map_free(bmap);
5480 if (n_div == 0)
5481 return bmap;
5483 bmap = add_known_div_constraints(bmap);
5484 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5485 bmap = isl_basic_map_finalize(bmap);
5486 return bmap;
5489 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5491 * In particular, if this div is of the form d = floor(f/m),
5492 * then add the constraint
5494 * f - m d >= 0
5496 * if sign < 0 or the constraint
5498 * -(f-(m-1)) + m d >= 0
5500 * if sign > 0.
5502 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5503 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5505 if (sign < 0)
5506 return add_upper_div_constraint(bmap, div);
5507 else
5508 return add_lower_div_constraint(bmap, div);
5511 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5512 __isl_take isl_basic_map *bmap)
5514 isl_space *space;
5516 if (!bmap)
5517 goto error;
5518 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5519 bmap->n_div == 0 &&
5520 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5521 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5522 return bset_from_bmap(bmap);
5523 bmap = isl_basic_map_cow(bmap);
5524 if (!bmap)
5525 return NULL;
5526 space = isl_basic_map_take_space(bmap);
5527 space = isl_space_underlying(space, bmap->n_div);
5528 bmap = isl_basic_map_restore_space(bmap, space);
5529 if (!bmap)
5530 return NULL;
5531 bmap->extra -= bmap->n_div;
5532 bmap->n_div = 0;
5533 bmap = isl_basic_map_finalize(bmap);
5534 return bset_from_bmap(bmap);
5535 error:
5536 isl_basic_map_free(bmap);
5537 return NULL;
5540 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5541 __isl_take isl_basic_set *bset)
5543 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5546 /* Replace each element in "list" by the result of applying
5547 * isl_basic_map_underlying_set to the element.
5549 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5550 __isl_take isl_basic_map_list *list)
5552 int i;
5553 isl_size n;
5555 n = isl_basic_map_list_n_basic_map(list);
5556 if (n < 0)
5557 goto error;
5559 for (i = 0; i < n; ++i) {
5560 isl_basic_map *bmap;
5561 isl_basic_set *bset;
5563 bmap = isl_basic_map_list_get_basic_map(list, i);
5564 bset = isl_basic_set_underlying_set(bmap);
5565 list = isl_basic_set_list_set_basic_set(list, i, bset);
5568 return list;
5569 error:
5570 isl_basic_map_list_free(list);
5571 return NULL;
5574 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5575 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5577 struct isl_basic_map *bmap;
5578 struct isl_ctx *ctx;
5579 isl_size dim, bmap_total;
5580 unsigned total;
5581 int i;
5583 if (!bset || !like)
5584 goto error;
5585 ctx = bset->ctx;
5586 if (isl_basic_set_check_no_params(bset) < 0 ||
5587 isl_basic_set_check_no_locals(bset) < 0)
5588 goto error;
5589 dim = isl_basic_set_dim(bset, isl_dim_set);
5590 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5591 if (dim < 0 || bmap_total < 0)
5592 goto error;
5593 isl_assert(ctx, dim == bmap_total, goto error);
5594 if (like->n_div == 0) {
5595 isl_space *space = isl_basic_map_get_space(like);
5596 isl_basic_map_free(like);
5597 return isl_basic_map_reset_space(bset, space);
5599 bset = isl_basic_set_cow(bset);
5600 if (!bset)
5601 goto error;
5602 total = dim + bset->extra;
5603 bmap = bset_to_bmap(bset);
5604 isl_space_free(isl_basic_map_take_space(bmap));
5605 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5606 if (!bmap)
5607 goto error;
5608 bmap->n_div = like->n_div;
5609 bmap->extra += like->n_div;
5610 if (bmap->extra) {
5611 unsigned ltotal;
5612 isl_int **div;
5613 ltotal = total - bmap->extra + like->extra;
5614 if (ltotal > total)
5615 ltotal = total;
5616 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5617 bmap->extra * (1 + 1 + total));
5618 if (isl_blk_is_error(bmap->block2))
5619 goto error;
5620 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5621 if (!div)
5622 goto error;
5623 bmap->div = div;
5624 for (i = 0; i < bmap->extra; ++i)
5625 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5626 for (i = 0; i < like->n_div; ++i) {
5627 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5628 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5630 bmap = isl_basic_map_add_known_div_constraints(bmap);
5632 isl_basic_map_free(like);
5633 bmap = isl_basic_map_simplify(bmap);
5634 bmap = isl_basic_map_finalize(bmap);
5635 return bmap;
5636 error:
5637 isl_basic_map_free(like);
5638 isl_basic_set_free(bset);
5639 return NULL;
5642 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5643 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5645 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5646 bset_to_bmap(like)));
5649 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5651 int i;
5653 map = isl_map_cow(map);
5654 if (!map)
5655 return NULL;
5656 map->dim = isl_space_cow(map->dim);
5657 if (!map->dim)
5658 goto error;
5660 for (i = 1; i < map->n; ++i)
5661 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5662 goto error);
5663 for (i = 0; i < map->n; ++i) {
5664 map->p[i] = bset_to_bmap(
5665 isl_basic_map_underlying_set(map->p[i]));
5666 if (!map->p[i])
5667 goto error;
5669 if (map->n == 0)
5670 map->dim = isl_space_underlying(map->dim, 0);
5671 else {
5672 isl_space_free(map->dim);
5673 map->dim = isl_space_copy(map->p[0]->dim);
5675 if (!map->dim)
5676 goto error;
5677 return set_from_map(map);
5678 error:
5679 isl_map_free(map);
5680 return NULL;
5683 /* Replace the space of "bmap" by "space".
5685 * If the space of "bmap" is identical to "space" (including the identifiers
5686 * of the input and output dimensions), then simply return the original input.
5688 __isl_give isl_basic_map *isl_basic_map_reset_space(
5689 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5691 isl_bool equal;
5692 isl_space *bmap_space;
5694 bmap_space = isl_basic_map_peek_space(bmap);
5695 equal = isl_space_is_equal(bmap_space, space);
5696 if (equal >= 0 && equal)
5697 equal = isl_space_has_equal_ids(bmap_space, space);
5698 if (equal < 0)
5699 goto error;
5700 if (equal) {
5701 isl_space_free(space);
5702 return bmap;
5704 isl_space_free(isl_basic_map_take_space(bmap));
5705 bmap = isl_basic_map_restore_space(bmap, space);
5707 bmap = isl_basic_map_finalize(bmap);
5709 return bmap;
5710 error:
5711 isl_basic_map_free(bmap);
5712 isl_space_free(space);
5713 return NULL;
5716 __isl_give isl_basic_set *isl_basic_set_reset_space(
5717 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5719 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5720 space));
5723 /* Check that the total dimensions of "map" and "space" are the same.
5725 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5726 __isl_keep isl_space *space)
5728 isl_size dim1, dim2;
5730 dim1 = isl_map_dim(map, isl_dim_all);
5731 dim2 = isl_space_dim(space, isl_dim_all);
5732 if (dim1 < 0 || dim2 < 0)
5733 return isl_stat_error;
5734 if (dim1 == dim2)
5735 return isl_stat_ok;
5736 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5737 "total dimensions do not match", return isl_stat_error);
5740 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5741 __isl_take isl_space *space)
5743 int i;
5745 map = isl_map_cow(map);
5746 if (!map || !space)
5747 goto error;
5749 for (i = 0; i < map->n; ++i) {
5750 map->p[i] = isl_basic_map_reset_space(map->p[i],
5751 isl_space_copy(space));
5752 if (!map->p[i])
5753 goto error;
5755 isl_space_free(isl_map_take_space(map));
5756 map = isl_map_restore_space(map, space);
5758 return map;
5759 error:
5760 isl_map_free(map);
5761 isl_space_free(space);
5762 return NULL;
5765 /* Replace the space of "map" by "space", without modifying
5766 * the dimension of "map".
5768 * If the space of "map" is identical to "space" (including the identifiers
5769 * of the input and output dimensions), then simply return the original input.
5771 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5772 __isl_take isl_space *space)
5774 isl_bool equal;
5775 isl_space *map_space;
5777 map_space = isl_map_peek_space(map);
5778 equal = isl_space_is_equal(map_space, space);
5779 if (equal >= 0 && equal)
5780 equal = isl_space_has_equal_ids(map_space, space);
5781 if (equal < 0)
5782 goto error;
5783 if (equal) {
5784 isl_space_free(space);
5785 return map;
5787 if (check_map_space_equal_total_dim(map, space) < 0)
5788 goto error;
5789 return isl_map_reset_space(map, space);
5790 error:
5791 isl_map_free(map);
5792 isl_space_free(space);
5793 return NULL;
5796 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5797 __isl_take isl_space *space)
5799 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5802 /* Compute the parameter domain of the given basic set.
5804 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5806 isl_bool is_params;
5807 isl_space *space;
5808 isl_size n;
5810 is_params = isl_basic_set_is_params(bset);
5811 if (is_params < 0)
5812 return isl_basic_set_free(bset);
5813 if (is_params)
5814 return bset;
5816 n = isl_basic_set_dim(bset, isl_dim_set);
5817 if (n < 0)
5818 return isl_basic_set_free(bset);
5819 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5820 space = isl_basic_set_get_space(bset);
5821 space = isl_space_params(space);
5822 bset = isl_basic_set_reset_space(bset, space);
5823 return bset;
5826 /* Construct a zero-dimensional basic set with the given parameter domain.
5828 __isl_give isl_basic_set *isl_basic_set_from_params(
5829 __isl_take isl_basic_set *bset)
5831 isl_space *space;
5832 space = isl_basic_set_get_space(bset);
5833 space = isl_space_set_from_params(space);
5834 bset = isl_basic_set_reset_space(bset, space);
5835 return bset;
5838 /* Compute the parameter domain of the given set.
5840 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5842 return isl_map_params(set_to_map(set));
5845 /* Construct a zero-dimensional set with the given parameter domain.
5847 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5849 isl_space *space;
5850 space = isl_set_get_space(set);
5851 space = isl_space_set_from_params(space);
5852 set = isl_set_reset_space(set, space);
5853 return set;
5856 /* Compute the parameter domain of the given map.
5858 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5860 isl_space *space;
5861 isl_size n_in, n_out;
5863 n_in = isl_map_dim(map, isl_dim_in);
5864 n_out = isl_map_dim(map, isl_dim_out);
5865 if (n_in < 0 || n_out < 0)
5866 return isl_map_free(map);
5867 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5868 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5869 space = isl_map_get_space(map);
5870 space = isl_space_params(space);
5871 map = isl_map_reset_space(map, space);
5872 return map;
5875 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5877 isl_space *space;
5878 isl_size n_out;
5880 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5881 if (n_out < 0)
5882 return isl_basic_map_free(bmap);
5883 space = isl_space_domain(isl_basic_map_get_space(bmap));
5885 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5887 return isl_basic_map_reset_space(bmap, space);
5890 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5892 if (!bmap)
5893 return isl_bool_error;
5894 return isl_space_may_be_set(bmap->dim);
5897 /* Is this basic map actually a set?
5898 * Users should never call this function. Outside of isl,
5899 * the type should indicate whether something is a set or a map.
5901 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5903 if (!bmap)
5904 return isl_bool_error;
5905 return isl_space_is_set(bmap->dim);
5908 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5910 isl_bool is_set;
5912 is_set = isl_basic_map_is_set(bmap);
5913 if (is_set < 0)
5914 goto error;
5915 if (is_set)
5916 return bmap;
5917 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5918 error:
5919 isl_basic_map_free(bmap);
5920 return NULL;
5923 __isl_give isl_basic_map *isl_basic_map_domain_map(
5924 __isl_take isl_basic_map *bmap)
5926 int i;
5927 isl_space *space;
5928 isl_basic_map *domain;
5929 isl_size nparam, n_in, n_out;
5931 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5932 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5933 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5934 if (nparam < 0 || n_in < 0 || n_out < 0)
5935 return isl_basic_map_free(bmap);
5937 space = isl_basic_map_get_space(bmap);
5938 space = isl_space_from_range(isl_space_domain(space));
5939 domain = isl_basic_map_universe(space);
5941 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5942 bmap = isl_basic_map_apply_range(bmap, domain);
5943 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5945 for (i = 0; i < n_in; ++i)
5946 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5947 isl_dim_out, i);
5949 bmap = isl_basic_map_gauss(bmap, NULL);
5950 return isl_basic_map_finalize(bmap);
5953 __isl_give isl_basic_map *isl_basic_map_range_map(
5954 __isl_take isl_basic_map *bmap)
5956 int i;
5957 isl_space *space;
5958 isl_basic_map *range;
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_range(space));
5969 range = 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, range);
5973 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5975 for (i = 0; i < n_out; ++i)
5976 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5977 isl_dim_out, i);
5979 bmap = isl_basic_map_gauss(bmap, NULL);
5980 return isl_basic_map_finalize(bmap);
5983 int isl_map_may_be_set(__isl_keep isl_map *map)
5985 if (!map)
5986 return -1;
5987 return isl_space_may_be_set(map->dim);
5990 /* Is this map actually a set?
5991 * Users should never call this function. Outside of isl,
5992 * the type should indicate whether something is a set or a map.
5994 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5996 if (!map)
5997 return isl_bool_error;
5998 return isl_space_is_set(map->dim);
6001 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6003 int i;
6004 isl_bool is_set;
6005 struct isl_set *set;
6007 is_set = isl_map_is_set(map);
6008 if (is_set < 0)
6009 goto error;
6010 if (is_set)
6011 return set_from_map(map);
6013 map = isl_map_cow(map);
6014 if (!map)
6015 goto error;
6017 set = set_from_map(map);
6018 set->dim = isl_space_range(set->dim);
6019 if (!set->dim)
6020 goto error;
6021 for (i = 0; i < map->n; ++i) {
6022 set->p[i] = isl_basic_map_range(map->p[i]);
6023 if (!set->p[i])
6024 goto error;
6026 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6027 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6028 return set;
6029 error:
6030 isl_map_free(map);
6031 return NULL;
6034 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6035 * to each of its basic maps.
6037 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6038 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6039 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6041 int i;
6042 isl_space *space;
6044 map = isl_map_cow(map);
6045 if (!map)
6046 return NULL;
6048 for (i = 0; i < map->n; ++i) {
6049 map->p[i] = fn_bmap(map->p[i]);
6050 if (!map->p[i])
6051 return isl_map_free(map);
6053 map = isl_map_unmark_normalized(map);
6055 space = isl_map_take_space(map);
6056 space = fn_space(space);
6057 map = isl_map_restore_space(map, space);
6059 return map;
6062 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6064 return isl_map_transform(map, &isl_space_domain_map,
6065 &isl_basic_map_domain_map);
6068 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6070 return isl_map_transform(map, &isl_space_range_map,
6071 &isl_basic_map_range_map);
6074 /* Given a wrapped map of the form A[B -> C],
6075 * return the map A[B -> C] -> B.
6077 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6079 isl_id *id;
6080 isl_map *map;
6082 if (!set)
6083 return NULL;
6084 if (!isl_set_has_tuple_id(set))
6085 return isl_map_domain_map(isl_set_unwrap(set));
6087 id = isl_set_get_tuple_id(set);
6088 map = isl_map_domain_map(isl_set_unwrap(set));
6089 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6091 return map;
6094 __isl_give isl_basic_map *isl_basic_map_from_domain(
6095 __isl_take isl_basic_set *bset)
6097 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6100 __isl_give isl_basic_map *isl_basic_map_from_range(
6101 __isl_take isl_basic_set *bset)
6103 isl_space *space;
6104 space = isl_basic_set_get_space(bset);
6105 space = isl_space_from_range(space);
6106 bset = isl_basic_set_reset_space(bset, space);
6107 return bset_to_bmap(bset);
6110 /* Create a relation with the given set as range.
6111 * The domain of the created relation is a zero-dimensional
6112 * flat anonymous space.
6114 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6116 isl_space *space;
6117 space = isl_set_get_space(set);
6118 space = isl_space_from_range(space);
6119 set = isl_set_reset_space(set, space);
6120 return set_to_map(set);
6123 /* Create a relation with the given set as domain.
6124 * The range of the created relation is a zero-dimensional
6125 * flat anonymous space.
6127 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6129 return isl_map_reverse(isl_map_from_range(set));
6132 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6133 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6135 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6138 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6139 __isl_take isl_set *range)
6141 return isl_map_apply_range(isl_map_reverse(domain), range);
6144 /* Return a newly allocated isl_map with given space and flags and
6145 * room for "n" basic maps.
6146 * Make sure that all cached information is cleared.
6148 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6149 unsigned flags)
6151 struct isl_map *map;
6153 if (!space)
6154 return NULL;
6155 if (n < 0)
6156 isl_die(space->ctx, isl_error_internal,
6157 "negative number of basic maps", goto error);
6158 map = isl_calloc(space->ctx, struct isl_map,
6159 sizeof(struct isl_map) +
6160 (n - 1) * sizeof(struct isl_basic_map *));
6161 if (!map)
6162 goto error;
6164 map->ctx = space->ctx;
6165 isl_ctx_ref(map->ctx);
6166 map->ref = 1;
6167 map->size = n;
6168 map->n = 0;
6169 map->dim = space;
6170 map->flags = flags;
6171 return map;
6172 error:
6173 isl_space_free(space);
6174 return NULL;
6177 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6179 struct isl_basic_map *bmap;
6180 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6181 bmap = isl_basic_map_set_to_empty(bmap);
6182 return bmap;
6185 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6187 struct isl_basic_set *bset;
6188 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6189 bset = isl_basic_set_set_to_empty(bset);
6190 return bset;
6193 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6195 struct isl_basic_map *bmap;
6196 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6197 bmap = isl_basic_map_finalize(bmap);
6198 return bmap;
6201 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6203 struct isl_basic_set *bset;
6204 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6205 bset = isl_basic_set_finalize(bset);
6206 return bset;
6209 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6210 __isl_take isl_space *space)
6212 int i;
6213 isl_size total = isl_space_dim(space, isl_dim_all);
6214 isl_basic_map *bmap;
6216 if (total < 0)
6217 space = isl_space_free(space);
6218 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6219 for (i = 0; i < total; ++i) {
6220 int k = isl_basic_map_alloc_inequality(bmap);
6221 if (k < 0)
6222 goto error;
6223 isl_seq_clr(bmap->ineq[k], 1 + total);
6224 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6226 return bmap;
6227 error:
6228 isl_basic_map_free(bmap);
6229 return NULL;
6232 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6233 __isl_take isl_space *space)
6235 return isl_basic_map_nat_universe(space);
6238 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6240 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6243 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6245 return isl_map_nat_universe(space);
6248 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6250 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6253 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6255 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6258 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6260 struct isl_map *map;
6261 if (!space)
6262 return NULL;
6263 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6264 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6265 return map;
6268 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6270 struct isl_set *set;
6271 if (!space)
6272 return NULL;
6273 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6274 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6275 return set;
6278 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6280 int i;
6281 struct isl_map *dup;
6283 if (!map)
6284 return NULL;
6285 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6286 for (i = 0; i < map->n; ++i)
6287 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6288 return dup;
6291 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6292 __isl_take isl_basic_map *bmap)
6294 if (!bmap || !map)
6295 goto error;
6296 if (isl_basic_map_plain_is_empty(bmap)) {
6297 isl_basic_map_free(bmap);
6298 return map;
6300 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6301 goto error;
6302 isl_assert(map->ctx, map->n < map->size, goto error);
6303 map->p[map->n] = bmap;
6304 map->n++;
6305 map = isl_map_unmark_normalized(map);
6306 return map;
6307 error:
6308 if (map)
6309 isl_map_free(map);
6310 if (bmap)
6311 isl_basic_map_free(bmap);
6312 return NULL;
6315 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6317 int i;
6319 if (!map)
6320 return NULL;
6322 if (--map->ref > 0)
6323 return NULL;
6325 clear_caches(map);
6326 isl_ctx_deref(map->ctx);
6327 for (i = 0; i < map->n; ++i)
6328 isl_basic_map_free(map->p[i]);
6329 isl_space_free(map->dim);
6330 free(map);
6332 return NULL;
6335 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6336 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6338 int j;
6339 isl_size total;
6341 total = isl_basic_map_dim(bmap, isl_dim_all);
6342 if (total < 0)
6343 return isl_basic_map_free(bmap);
6345 bmap = isl_basic_map_cow(bmap);
6346 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6347 j = isl_basic_map_alloc_equality(bmap);
6348 if (j < 0)
6349 goto error;
6350 isl_seq_clr(bmap->eq[j] + 1, total);
6351 isl_int_set_si(bmap->eq[j][pos], -1);
6352 isl_int_set_si(bmap->eq[j][0], value);
6353 bmap = isl_basic_map_simplify(bmap);
6354 return isl_basic_map_finalize(bmap);
6355 error:
6356 isl_basic_map_free(bmap);
6357 return NULL;
6360 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6361 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6363 int j;
6364 isl_size total;
6366 total = isl_basic_map_dim(bmap, isl_dim_all);
6367 if (total < 0)
6368 return isl_basic_map_free(bmap);
6370 bmap = isl_basic_map_cow(bmap);
6371 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6372 j = isl_basic_map_alloc_equality(bmap);
6373 if (j < 0)
6374 goto error;
6375 isl_seq_clr(bmap->eq[j] + 1, total);
6376 isl_int_set_si(bmap->eq[j][pos], -1);
6377 isl_int_set(bmap->eq[j][0], value);
6378 bmap = isl_basic_map_simplify(bmap);
6379 return isl_basic_map_finalize(bmap);
6380 error:
6381 isl_basic_map_free(bmap);
6382 return NULL;
6385 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6386 enum isl_dim_type type, unsigned pos, int value)
6388 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6389 return isl_basic_map_free(bmap);
6390 return isl_basic_map_fix_pos_si(bmap,
6391 isl_basic_map_offset(bmap, type) + pos, value);
6394 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6395 enum isl_dim_type type, unsigned pos, isl_int value)
6397 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6398 return isl_basic_map_free(bmap);
6399 return isl_basic_map_fix_pos(bmap,
6400 isl_basic_map_offset(bmap, type) + pos, value);
6403 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6404 * to be equal to "v".
6406 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6407 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6409 if (!bmap || !v)
6410 goto error;
6411 if (!isl_val_is_int(v))
6412 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6413 "expecting integer value", goto error);
6414 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6415 goto error;
6416 pos += isl_basic_map_offset(bmap, type);
6417 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6418 isl_val_free(v);
6419 return bmap;
6420 error:
6421 isl_basic_map_free(bmap);
6422 isl_val_free(v);
6423 return NULL;
6426 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6427 * to be equal to "v".
6429 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6430 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6432 return isl_basic_map_fix_val(bset, type, pos, v);
6435 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6436 enum isl_dim_type type, unsigned pos, int value)
6438 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6439 type, pos, value));
6442 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6443 enum isl_dim_type type, unsigned pos, isl_int value)
6445 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6446 type, pos, value));
6449 /* Remove the basic map at position "i" from "map" if this basic map
6450 * is (obviously) empty.
6452 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6454 isl_bool empty;
6456 if (!map)
6457 return NULL;
6459 empty = isl_basic_map_plain_is_empty(map->p[i]);
6460 if (empty < 0)
6461 return isl_map_free(map);
6462 if (!empty)
6463 return map;
6465 isl_basic_map_free(map->p[i]);
6466 map->n--;
6467 if (i != map->n) {
6468 map->p[i] = map->p[map->n];
6469 map = isl_map_unmark_normalized(map);
6473 return map;
6476 /* Perform "fn" on each basic map of "map", where we may not be holding
6477 * the only reference to "map".
6478 * In particular, "fn" should be a semantics preserving operation
6479 * that we want to apply to all copies of "map". We therefore need
6480 * to be careful not to modify "map" in a way that breaks "map"
6481 * in case anything goes wrong.
6483 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6484 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6486 struct isl_basic_map *bmap;
6487 int i;
6489 if (!map)
6490 return NULL;
6492 for (i = map->n - 1; i >= 0; --i) {
6493 bmap = isl_basic_map_copy(map->p[i]);
6494 bmap = fn(bmap);
6495 if (!bmap)
6496 goto error;
6497 isl_basic_map_free(map->p[i]);
6498 map->p[i] = bmap;
6499 map = remove_if_empty(map, i);
6500 if (!map)
6501 return NULL;
6504 return map;
6505 error:
6506 isl_map_free(map);
6507 return NULL;
6510 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6511 enum isl_dim_type type, unsigned pos, int value)
6513 int i;
6515 map = isl_map_cow(map);
6516 if (isl_map_check_range(map, type, pos, 1) < 0)
6517 return isl_map_free(map);
6518 for (i = map->n - 1; i >= 0; --i) {
6519 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6520 map = remove_if_empty(map, i);
6521 if (!map)
6522 return NULL;
6524 map = isl_map_unmark_normalized(map);
6525 return map;
6528 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6529 enum isl_dim_type type, unsigned pos, int value)
6531 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6534 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6535 enum isl_dim_type type, unsigned pos, isl_int value)
6537 int i;
6539 map = isl_map_cow(map);
6540 if (isl_map_check_range(map, type, pos, 1) < 0)
6541 return isl_map_free(map);
6542 for (i = 0; i < map->n; ++i) {
6543 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6544 if (!map->p[i])
6545 goto error;
6547 map = isl_map_unmark_normalized(map);
6548 return map;
6549 error:
6550 isl_map_free(map);
6551 return NULL;
6554 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6555 enum isl_dim_type type, unsigned pos, isl_int value)
6557 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6560 /* Fix the value of the variable at position "pos" of type "type" of "map"
6561 * to be equal to "v".
6563 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6564 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6566 int i;
6568 map = isl_map_cow(map);
6569 if (!map || !v)
6570 goto error;
6572 if (!isl_val_is_int(v))
6573 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6574 "expecting integer value", goto error);
6575 if (isl_map_check_range(map, type, pos, 1) < 0)
6576 goto error;
6577 for (i = map->n - 1; i >= 0; --i) {
6578 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6579 isl_val_copy(v));
6580 map = remove_if_empty(map, i);
6581 if (!map)
6582 goto error;
6584 map = isl_map_unmark_normalized(map);
6585 isl_val_free(v);
6586 return map;
6587 error:
6588 isl_map_free(map);
6589 isl_val_free(v);
6590 return NULL;
6593 /* Fix the value of the variable at position "pos" of type "type" of "set"
6594 * to be equal to "v".
6596 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6597 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6599 return isl_map_fix_val(set, type, pos, v);
6602 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6603 unsigned input, int value)
6605 return isl_map_fix_si(map, isl_dim_in, input, value);
6608 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6609 int value)
6611 return set_from_map(isl_map_fix_si(set_to_map(set),
6612 isl_dim_set, dim, value));
6615 static __isl_give isl_basic_map *basic_map_bound_si(
6616 __isl_take isl_basic_map *bmap,
6617 enum isl_dim_type type, unsigned pos, int value, int upper)
6619 int j;
6620 isl_size total;
6622 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6623 return isl_basic_map_free(bmap);
6624 total = isl_basic_map_dim(bmap, isl_dim_all);
6625 if (total < 0)
6626 return isl_basic_map_free(bmap);
6627 pos += isl_basic_map_offset(bmap, type);
6628 bmap = isl_basic_map_cow(bmap);
6629 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6630 j = isl_basic_map_alloc_inequality(bmap);
6631 if (j < 0)
6632 goto error;
6633 isl_seq_clr(bmap->ineq[j], 1 + total);
6634 if (upper) {
6635 isl_int_set_si(bmap->ineq[j][pos], -1);
6636 isl_int_set_si(bmap->ineq[j][0], value);
6637 } else {
6638 isl_int_set_si(bmap->ineq[j][pos], 1);
6639 isl_int_set_si(bmap->ineq[j][0], -value);
6641 bmap = isl_basic_map_simplify(bmap);
6642 return isl_basic_map_finalize(bmap);
6643 error:
6644 isl_basic_map_free(bmap);
6645 return NULL;
6648 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6649 __isl_take isl_basic_map *bmap,
6650 enum isl_dim_type type, unsigned pos, int value)
6652 return basic_map_bound_si(bmap, type, pos, value, 0);
6655 /* Constrain the values of the given dimension to be no greater than "value".
6657 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6658 __isl_take isl_basic_map *bmap,
6659 enum isl_dim_type type, unsigned pos, int value)
6661 return basic_map_bound_si(bmap, type, pos, value, 1);
6664 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6665 enum isl_dim_type type, unsigned pos, int value, int upper)
6667 int i;
6669 map = isl_map_cow(map);
6670 if (isl_map_check_range(map, type, pos, 1) < 0)
6671 return isl_map_free(map);
6672 for (i = 0; i < map->n; ++i) {
6673 map->p[i] = basic_map_bound_si(map->p[i],
6674 type, pos, value, upper);
6675 if (!map->p[i])
6676 goto error;
6678 map = isl_map_unmark_normalized(map);
6679 return map;
6680 error:
6681 isl_map_free(map);
6682 return NULL;
6685 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6686 enum isl_dim_type type, unsigned pos, int value)
6688 return map_bound_si(map, type, pos, value, 0);
6691 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6692 enum isl_dim_type type, unsigned pos, int value)
6694 return map_bound_si(map, type, pos, value, 1);
6697 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6698 enum isl_dim_type type, unsigned pos, int value)
6700 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6701 type, pos, value));
6704 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6705 enum isl_dim_type type, unsigned pos, int value)
6707 return isl_map_upper_bound_si(set, type, pos, value);
6710 /* Bound the given variable of "bmap" from below (or above is "upper"
6711 * is set) to "value".
6713 static __isl_give isl_basic_map *basic_map_bound(
6714 __isl_take isl_basic_map *bmap,
6715 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6717 int j;
6718 isl_size total;
6720 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6721 return isl_basic_map_free(bmap);
6722 total = isl_basic_map_dim(bmap, isl_dim_all);
6723 if (total < 0)
6724 return isl_basic_map_free(bmap);
6725 pos += isl_basic_map_offset(bmap, type);
6726 bmap = isl_basic_map_cow(bmap);
6727 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6728 j = isl_basic_map_alloc_inequality(bmap);
6729 if (j < 0)
6730 goto error;
6731 isl_seq_clr(bmap->ineq[j], 1 + total);
6732 if (upper) {
6733 isl_int_set_si(bmap->ineq[j][pos], -1);
6734 isl_int_set(bmap->ineq[j][0], value);
6735 } else {
6736 isl_int_set_si(bmap->ineq[j][pos], 1);
6737 isl_int_neg(bmap->ineq[j][0], value);
6739 bmap = isl_basic_map_simplify(bmap);
6740 return isl_basic_map_finalize(bmap);
6741 error:
6742 isl_basic_map_free(bmap);
6743 return NULL;
6746 /* Bound the given variable of "map" from below (or above is "upper"
6747 * is set) to "value".
6749 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6750 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6752 int i;
6754 map = isl_map_cow(map);
6755 if (isl_map_check_range(map, type, pos, 1) < 0)
6756 return isl_map_free(map);
6757 for (i = map->n - 1; i >= 0; --i) {
6758 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6759 map = remove_if_empty(map, i);
6760 if (!map)
6761 return NULL;
6763 map = isl_map_unmark_normalized(map);
6764 return map;
6767 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6768 enum isl_dim_type type, unsigned pos, isl_int value)
6770 return map_bound(map, type, pos, value, 0);
6773 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6774 enum isl_dim_type type, unsigned pos, isl_int value)
6776 return map_bound(map, type, pos, value, 1);
6779 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6780 enum isl_dim_type type, unsigned pos, isl_int value)
6782 return isl_map_lower_bound(set, type, pos, value);
6785 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6786 enum isl_dim_type type, unsigned pos, isl_int value)
6788 return isl_map_upper_bound(set, type, pos, value);
6791 /* Force the values of the variable at position "pos" of type "type" of "map"
6792 * to be no smaller than "value".
6794 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6795 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6797 if (!value)
6798 goto error;
6799 if (!isl_val_is_int(value))
6800 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6801 "expecting integer value", goto error);
6802 map = isl_map_lower_bound(map, type, pos, value->n);
6803 isl_val_free(value);
6804 return map;
6805 error:
6806 isl_val_free(value);
6807 isl_map_free(map);
6808 return NULL;
6811 /* Force the values of the variable at position "pos" of type "type" of "set"
6812 * to be no smaller than "value".
6814 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6815 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6817 isl_map *map;
6819 map = set_to_map(set);
6820 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6823 /* Force the values of the variable at position "pos" of type "type" of "map"
6824 * to be no greater than "value".
6826 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6827 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6829 if (!value)
6830 goto error;
6831 if (!isl_val_is_int(value))
6832 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6833 "expecting integer value", goto error);
6834 map = isl_map_upper_bound(map, type, pos, value->n);
6835 isl_val_free(value);
6836 return map;
6837 error:
6838 isl_val_free(value);
6839 isl_map_free(map);
6840 return NULL;
6843 /* Force the values of the variable at position "pos" of type "type" of "set"
6844 * to be no greater than "value".
6846 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6847 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6849 isl_map *map;
6851 map = set_to_map(set);
6852 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6855 #undef BASE
6856 #define BASE val
6857 #include "isl_map_bound_templ.c"
6859 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6860 * for each set dimension, by treating the set as a map.
6862 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6863 __isl_take isl_multi_val *bound,
6864 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6865 unsigned pos, __isl_take isl_val *value))
6867 isl_map *map;
6869 map = set_to_map(set);
6870 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6873 #undef BASE
6874 #define BASE pw_aff
6875 #include "isl_map_bound_templ.c"
6877 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6878 * for each set dimension, by converting the set and the bound
6879 * to objects living in a map space.
6881 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6882 __isl_take isl_multi_pw_aff *bound,
6883 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6884 unsigned pos, __isl_take TYPE *value))
6886 isl_map *map;
6888 map = isl_map_from_range(set);
6889 bound = isl_multi_pw_aff_from_range(bound);
6890 map = map_bound_multi_pw_aff(map, bound, set_bound);
6891 return isl_map_range(map);
6894 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6895 * setting a bound on the given output dimension.
6897 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6898 unsigned pos, __isl_take isl_val *v)
6900 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6903 /* Force the values of the set dimensions of "set"
6904 * to be no smaller than the corresponding values in "lower".
6906 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
6907 __isl_take isl_multi_val *lower)
6909 return set_bound_multi_val(set, lower, &map_lower_bound_val);
6912 /* Force the values of the output dimensions of "map"
6913 * to be no smaller than the corresponding values in "lower".
6915 __isl_give isl_map *isl_map_lower_bound_multi_val(__isl_take isl_map *map,
6916 __isl_take isl_multi_val *lower)
6918 return map_bound_multi_val(map, lower, &map_lower_bound_val);
6921 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6922 * setting a bound on the given output dimension.
6924 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
6925 unsigned pos, __isl_take isl_val *v)
6927 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
6930 /* Force the values of the set dimensions of "set"
6931 * to be no greater than the corresponding values in "upper".
6933 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
6934 __isl_take isl_multi_val *upper)
6936 return set_bound_multi_val(set, upper, &map_upper_bound_val);
6939 /* Force the values of the set dimensions of "set"
6940 * to be no greater than the corresponding values in "upper".
6942 __isl_give isl_map *isl_map_upper_bound_multi_val(__isl_take isl_map *map,
6943 __isl_take isl_multi_val *upper)
6945 return map_bound_multi_val(map, upper, &map_upper_bound_val);
6948 /* Force the symbolic constant expression "bound"
6949 * to satisfy the relation "order" with respect to
6950 * the output variable at position "pos" of "map".
6952 * Create an affine expression representing the output variable
6953 * in terms of the range and
6954 * compare it using "order" to "bound" (defined on the domain).
6955 * The result is a relation between elements in domain and range that
6956 * can be intersected with "map".
6958 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
6959 unsigned pos, __isl_take isl_pw_aff *bound,
6960 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
6961 __isl_take isl_pw_aff *pa2))
6963 isl_space *space;
6964 isl_local_space *ls;
6965 isl_pw_aff *var;
6967 space = isl_space_range(isl_map_get_space(map));
6968 ls = isl_local_space_from_space(space);
6969 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
6970 map = isl_map_intersect(map, order(bound, var));
6971 return map;
6974 /* Force the values of the output variable at position "pos" of "map"
6975 * to be no smaller than the symbolic constant expression "lower".
6977 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
6978 unsigned pos, __isl_take isl_pw_aff *lower)
6980 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
6983 /* Force the values of the output variable at position "pos" of "map"
6984 * to be no greater than the symbolic constant expression "upper".
6986 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
6987 unsigned pos, __isl_take isl_pw_aff *upper)
6989 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
6992 /* Force the values of the set dimensions of "set"
6993 * to be no smaller than the corresponding constant symbolic expressions
6994 * in "lower".
6996 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
6997 __isl_take isl_multi_pw_aff *lower)
6999 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7002 /* Force the values of the set dimensions of "set"
7003 * to be no greater than the corresponding constant symbolic expressions
7004 * in "upper".
7006 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7007 __isl_take isl_multi_pw_aff *upper)
7009 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7012 /* Force the values of the output dimensions of "map"
7013 * to be no smaller than the corresponding constant symbolic expressions
7014 * in "lower".
7016 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7017 __isl_take isl_multi_pw_aff *lower)
7019 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7022 /* Force the values of the output dimensions of "map"
7023 * to be no greater than the corresponding constant symbolic expressions
7024 * in "upper".
7026 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7027 __isl_take isl_multi_pw_aff *upper)
7029 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7032 /* Bound the given variable of "bset" from below (or above is "upper"
7033 * is set) to "value".
7035 static __isl_give isl_basic_set *isl_basic_set_bound(
7036 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7037 isl_int value, int upper)
7039 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7040 type, pos, value, upper));
7043 /* Bound the given variable of "bset" from below (or above is "upper"
7044 * is set) to "value".
7046 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7047 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7048 __isl_take isl_val *value, int upper)
7050 if (!value)
7051 goto error;
7052 if (!isl_val_is_int(value))
7053 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7054 "expecting integer value", goto error);
7055 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7056 isl_val_free(value);
7057 return bset;
7058 error:
7059 isl_val_free(value);
7060 isl_basic_set_free(bset);
7061 return NULL;
7064 /* Bound the given variable of "bset" from below to "value".
7066 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7067 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7068 __isl_take isl_val *value)
7070 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7073 /* Bound the given variable of "bset" from above to "value".
7075 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7076 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7077 __isl_take isl_val *value)
7079 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7082 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7084 return isl_map_transform(map, &isl_space_reverse,
7085 &isl_basic_map_reverse);
7088 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7090 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7092 return isl_map_transform(map, &isl_space_range_reverse,
7093 &isl_basic_map_range_reverse);
7096 #undef TYPE
7097 #define TYPE isl_pw_multi_aff
7098 #undef SUFFIX
7099 #define SUFFIX _pw_multi_aff
7100 #undef EMPTY
7101 #define EMPTY isl_pw_multi_aff_empty
7102 #undef ADD
7103 #define ADD isl_pw_multi_aff_union_add
7104 #include "isl_map_lexopt_templ.c"
7106 /* Given a map "map", compute the lexicographically minimal
7107 * (or maximal) image element for each domain element in dom,
7108 * in the form of an isl_pw_multi_aff.
7109 * If "empty" is not NULL, then set *empty to those elements in dom that
7110 * do not have an image element.
7111 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7112 * should be computed over the domain of "map". "empty" is also NULL
7113 * in this case.
7115 * We first compute the lexicographically minimal or maximal element
7116 * in the first basic map. This results in a partial solution "res"
7117 * and a subset "todo" of dom that still need to be handled.
7118 * We then consider each of the remaining maps in "map" and successively
7119 * update both "res" and "todo".
7120 * If "empty" is NULL, then the todo sets are not needed and therefore
7121 * also not computed.
7123 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7124 __isl_take isl_map *map, __isl_take isl_set *dom,
7125 __isl_give isl_set **empty, unsigned flags)
7127 int i;
7128 int full;
7129 isl_pw_multi_aff *res;
7130 isl_set *todo;
7132 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7133 if (!map || (!full && !dom))
7134 goto error;
7136 if (isl_map_plain_is_empty(map)) {
7137 if (empty)
7138 *empty = dom;
7139 else
7140 isl_set_free(dom);
7141 return isl_pw_multi_aff_from_map(map);
7144 res = basic_map_partial_lexopt_pw_multi_aff(
7145 isl_basic_map_copy(map->p[0]),
7146 isl_set_copy(dom), empty, flags);
7148 if (empty)
7149 todo = *empty;
7150 for (i = 1; i < map->n; ++i) {
7151 isl_pw_multi_aff *res_i;
7153 res_i = basic_map_partial_lexopt_pw_multi_aff(
7154 isl_basic_map_copy(map->p[i]),
7155 isl_set_copy(dom), empty, flags);
7157 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7158 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7159 else
7160 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7162 if (empty)
7163 todo = isl_set_intersect(todo, *empty);
7166 isl_set_free(dom);
7167 isl_map_free(map);
7169 if (empty)
7170 *empty = todo;
7172 return res;
7173 error:
7174 if (empty)
7175 *empty = NULL;
7176 isl_set_free(dom);
7177 isl_map_free(map);
7178 return NULL;
7181 #undef TYPE
7182 #define TYPE isl_map
7183 #undef SUFFIX
7184 #define SUFFIX
7185 #undef EMPTY
7186 #define EMPTY isl_map_empty
7187 #undef ADD
7188 #define ADD isl_map_union_disjoint
7189 #include "isl_map_lexopt_templ.c"
7191 /* Given a map "map", compute the lexicographically minimal
7192 * (or maximal) image element for each domain element in "dom",
7193 * in the form of an isl_map.
7194 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7195 * do not have an image element.
7196 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7197 * should be computed over the domain of "map". "empty" is also NULL
7198 * in this case.
7200 * If the input consists of more than one disjunct, then first
7201 * compute the desired result in the form of an isl_pw_multi_aff and
7202 * then convert that into an isl_map.
7204 * This function used to have an explicit implementation in terms
7205 * of isl_maps, but it would continually intersect the domains of
7206 * partial results with the complement of the domain of the next
7207 * partial solution, potentially leading to an explosion in the number
7208 * of disjuncts if there are several disjuncts in the input.
7209 * An even earlier implementation of this function would look for
7210 * better results in the domain of the partial result and for extra
7211 * results in the complement of this domain, which would lead to
7212 * even more splintering.
7214 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7215 __isl_take isl_map *map, __isl_take isl_set *dom,
7216 __isl_give isl_set **empty, unsigned flags)
7218 int full;
7219 struct isl_map *res;
7220 isl_pw_multi_aff *pma;
7222 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7223 if (!map || (!full && !dom))
7224 goto error;
7226 if (isl_map_plain_is_empty(map)) {
7227 if (empty)
7228 *empty = dom;
7229 else
7230 isl_set_free(dom);
7231 return map;
7234 if (map->n == 1) {
7235 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7236 dom, empty, flags);
7237 isl_map_free(map);
7238 return res;
7241 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7242 flags);
7243 return isl_map_from_pw_multi_aff_internal(pma);
7244 error:
7245 if (empty)
7246 *empty = NULL;
7247 isl_set_free(dom);
7248 isl_map_free(map);
7249 return NULL;
7252 __isl_give isl_map *isl_map_partial_lexmax(
7253 __isl_take isl_map *map, __isl_take isl_set *dom,
7254 __isl_give isl_set **empty)
7256 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7259 __isl_give isl_map *isl_map_partial_lexmin(
7260 __isl_take isl_map *map, __isl_take isl_set *dom,
7261 __isl_give isl_set **empty)
7263 return isl_map_partial_lexopt(map, dom, empty, 0);
7266 __isl_give isl_set *isl_set_partial_lexmin(
7267 __isl_take isl_set *set, __isl_take isl_set *dom,
7268 __isl_give isl_set **empty)
7270 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7271 dom, empty));
7274 __isl_give isl_set *isl_set_partial_lexmax(
7275 __isl_take isl_set *set, __isl_take isl_set *dom,
7276 __isl_give isl_set **empty)
7278 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7279 dom, empty));
7282 /* Compute the lexicographic minimum (or maximum if "flags" includes
7283 * ISL_OPT_MAX) of "bset" over its parametric domain.
7285 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7286 unsigned flags)
7288 return isl_basic_map_lexopt(bset, flags);
7291 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7293 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7296 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7298 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7301 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7303 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7306 /* Compute the lexicographic minimum of "bset" over its parametric domain
7307 * for the purpose of quantifier elimination.
7308 * That is, find an explicit representation for all the existentially
7309 * quantified variables in "bset" by computing their lexicographic
7310 * minimum.
7312 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7313 __isl_take isl_basic_set *bset)
7315 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7318 /* Given a basic map with one output dimension, compute the minimum or
7319 * maximum of that dimension as an isl_pw_aff.
7321 * Compute the optimum as a lexicographic optimum over the single
7322 * output dimension and extract the single isl_pw_aff from the result.
7324 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7325 int max)
7327 isl_pw_multi_aff *pma;
7328 isl_pw_aff *pwaff;
7330 bmap = isl_basic_map_copy(bmap);
7331 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7332 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7333 isl_pw_multi_aff_free(pma);
7335 return pwaff;
7338 /* Compute the minimum or maximum of the given output dimension
7339 * as a function of the parameters and the input dimensions,
7340 * but independently of the other output dimensions.
7342 * We first project out the other output dimension and then compute
7343 * the "lexicographic" maximum in each basic map, combining the results
7344 * using isl_pw_aff_union_max.
7346 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7347 int max)
7349 int i;
7350 isl_pw_aff *pwaff;
7351 isl_size n_out;
7353 n_out = isl_map_dim(map, isl_dim_out);
7354 if (n_out < 0)
7355 map = isl_map_free(map);
7356 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7357 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7358 if (!map)
7359 return NULL;
7361 if (map->n == 0) {
7362 isl_space *space = isl_map_get_space(map);
7363 isl_map_free(map);
7364 return isl_pw_aff_empty(space);
7367 pwaff = basic_map_dim_opt(map->p[0], max);
7368 for (i = 1; i < map->n; ++i) {
7369 isl_pw_aff *pwaff_i;
7371 pwaff_i = basic_map_dim_opt(map->p[i], max);
7372 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7375 isl_map_free(map);
7377 return pwaff;
7380 /* Compute the minimum of the given output dimension as a function of the
7381 * parameters and input dimensions, but independently of
7382 * the other output dimensions.
7384 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7386 return map_dim_opt(map, pos, 0);
7389 /* Compute the maximum of the given output dimension as a function of the
7390 * parameters and input dimensions, but independently of
7391 * the other output dimensions.
7393 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7395 return map_dim_opt(map, pos, 1);
7398 /* Compute the minimum or maximum of the given set dimension
7399 * as a function of the parameters,
7400 * but independently of the other set dimensions.
7402 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7403 int max)
7405 return map_dim_opt(set, pos, max);
7408 /* Compute the maximum of the given set dimension as a function of the
7409 * parameters, but independently of the other set dimensions.
7411 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7413 return set_dim_opt(set, pos, 1);
7416 /* Compute the minimum of the given set dimension as a function of the
7417 * parameters, but independently of the other set dimensions.
7419 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7421 return set_dim_opt(set, pos, 0);
7424 /* Apply a preimage specified by "mat" on the parameters of "bset".
7425 * bset is assumed to have only parameters and divs.
7427 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7428 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7430 isl_size nparam;
7432 nparam = isl_basic_set_dim(bset, isl_dim_param);
7433 if (nparam < 0 || !mat)
7434 goto error;
7436 bset->dim = isl_space_cow(bset->dim);
7437 if (!bset->dim)
7438 goto error;
7440 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7442 bset->dim->nparam = 0;
7443 bset->dim->n_out = nparam;
7444 bset = isl_basic_set_preimage(bset, mat);
7445 if (bset) {
7446 bset->dim->nparam = bset->dim->n_out;
7447 bset->dim->n_out = 0;
7449 return bset;
7450 error:
7451 isl_mat_free(mat);
7452 isl_basic_set_free(bset);
7453 return NULL;
7456 /* Apply a preimage specified by "mat" on the parameters of "set".
7457 * set is assumed to have only parameters and divs.
7459 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7460 __isl_take isl_mat *mat)
7462 isl_space *space;
7463 isl_size nparam;
7465 nparam = isl_set_dim(set, isl_dim_param);
7466 if (nparam < 0 || !mat)
7467 goto error;
7469 if (mat->n_row != 1 + nparam)
7470 isl_die(isl_set_get_ctx(set), isl_error_internal,
7471 "unexpected number of rows", goto error);
7473 space = isl_set_get_space(set);
7474 space = isl_space_move_dims(space, isl_dim_set, 0,
7475 isl_dim_param, 0, nparam);
7476 set = isl_set_reset_space(set, space);
7477 set = isl_set_preimage(set, mat);
7478 nparam = isl_set_dim(set, isl_dim_out);
7479 if (nparam < 0)
7480 set = isl_set_free(set);
7481 space = isl_set_get_space(set);
7482 space = isl_space_move_dims(space, isl_dim_param, 0,
7483 isl_dim_out, 0, nparam);
7484 set = isl_set_reset_space(set, space);
7485 return set;
7486 error:
7487 isl_mat_free(mat);
7488 isl_set_free(set);
7489 return NULL;
7492 /* Intersect the basic set "bset" with the affine space specified by the
7493 * equalities in "eq".
7495 static __isl_give isl_basic_set *basic_set_append_equalities(
7496 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7498 int i, k;
7499 unsigned len;
7501 if (!bset || !eq)
7502 goto error;
7504 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7505 if (!bset)
7506 goto error;
7508 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7509 for (i = 0; i < eq->n_row; ++i) {
7510 k = isl_basic_set_alloc_equality(bset);
7511 if (k < 0)
7512 goto error;
7513 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7514 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7516 isl_mat_free(eq);
7518 bset = isl_basic_set_gauss(bset, NULL);
7519 bset = isl_basic_set_finalize(bset);
7521 return bset;
7522 error:
7523 isl_mat_free(eq);
7524 isl_basic_set_free(bset);
7525 return NULL;
7528 /* Intersect the set "set" with the affine space specified by the
7529 * equalities in "eq".
7531 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7532 __isl_take isl_mat *eq)
7534 int i;
7536 if (!set || !eq)
7537 goto error;
7539 for (i = 0; i < set->n; ++i) {
7540 set->p[i] = basic_set_append_equalities(set->p[i],
7541 isl_mat_copy(eq));
7542 if (!set->p[i])
7543 goto error;
7545 isl_mat_free(eq);
7546 return set;
7547 error:
7548 isl_mat_free(eq);
7549 isl_set_free(set);
7550 return NULL;
7553 /* Given a basic set "bset" that only involves parameters and existentially
7554 * quantified variables, return the index of the first equality
7555 * that only involves parameters. If there is no such equality then
7556 * return bset->n_eq.
7558 * This function assumes that isl_basic_set_gauss has been called on "bset".
7560 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7562 int i, j;
7563 isl_size nparam, n_div;
7565 nparam = isl_basic_set_dim(bset, isl_dim_param);
7566 n_div = isl_basic_set_dim(bset, isl_dim_div);
7567 if (nparam < 0 || n_div < 0)
7568 return -1;
7570 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7571 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7572 ++i;
7575 return i;
7578 /* Compute an explicit representation for the existentially quantified
7579 * variables in "bset" by computing the "minimal value" of the set
7580 * variables. Since there are no set variables, the computation of
7581 * the minimal value essentially computes an explicit representation
7582 * of the non-empty part(s) of "bset".
7584 * The input only involves parameters and existentially quantified variables.
7585 * All equalities among parameters have been removed.
7587 * Since the existentially quantified variables in the result are in general
7588 * going to be different from those in the input, we first replace
7589 * them by the minimal number of variables based on their equalities.
7590 * This should simplify the parametric integer programming.
7592 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7594 isl_morph *morph1, *morph2;
7595 isl_set *set;
7596 isl_size n;
7598 if (!bset)
7599 return NULL;
7600 if (bset->n_eq == 0)
7601 return isl_basic_set_lexmin_compute_divs(bset);
7603 morph1 = isl_basic_set_parameter_compression(bset);
7604 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7605 bset = isl_basic_set_lift(bset);
7606 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7607 bset = isl_morph_basic_set(morph2, bset);
7608 n = isl_basic_set_dim(bset, isl_dim_set);
7609 if (n < 0)
7610 bset = isl_basic_set_free(bset);
7611 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7613 set = isl_basic_set_lexmin_compute_divs(bset);
7615 set = isl_morph_set(isl_morph_inverse(morph1), set);
7617 return set;
7620 /* Project the given basic set onto its parameter domain, possibly introducing
7621 * new, explicit, existential variables in the constraints.
7622 * The input has parameters and (possibly implicit) existential variables.
7623 * The output has the same parameters, but only
7624 * explicit existentially quantified variables.
7626 * The actual projection is performed by pip, but pip doesn't seem
7627 * to like equalities very much, so we first remove the equalities
7628 * among the parameters by performing a variable compression on
7629 * the parameters. Afterward, an inverse transformation is performed
7630 * and the equalities among the parameters are inserted back in.
7632 * The variable compression on the parameters may uncover additional
7633 * equalities that were only implicit before. We therefore check
7634 * if there are any new parameter equalities in the result and
7635 * if so recurse. The removal of parameter equalities is required
7636 * for the parameter compression performed by base_compute_divs.
7638 static __isl_give isl_set *parameter_compute_divs(
7639 __isl_take isl_basic_set *bset)
7641 int i;
7642 struct isl_mat *eq;
7643 struct isl_mat *T, *T2;
7644 struct isl_set *set;
7645 isl_size nparam;
7647 bset = isl_basic_set_cow(bset);
7648 if (!bset)
7649 return NULL;
7651 if (bset->n_eq == 0)
7652 return base_compute_divs(bset);
7654 bset = isl_basic_set_gauss(bset, NULL);
7655 if (!bset)
7656 return NULL;
7657 if (isl_basic_set_plain_is_empty(bset))
7658 return isl_set_from_basic_set(bset);
7660 i = first_parameter_equality(bset);
7661 if (i == bset->n_eq)
7662 return base_compute_divs(bset);
7664 nparam = isl_basic_set_dim(bset, isl_dim_param);
7665 if (nparam < 0)
7666 return isl_set_from_basic_set(isl_basic_set_free(bset));
7667 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7668 0, 1 + nparam);
7669 eq = isl_mat_cow(eq);
7670 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7671 if (T && T->n_col == 0) {
7672 isl_mat_free(T);
7673 isl_mat_free(T2);
7674 isl_mat_free(eq);
7675 bset = isl_basic_set_set_to_empty(bset);
7676 return isl_set_from_basic_set(bset);
7678 bset = basic_set_parameter_preimage(bset, T);
7680 i = first_parameter_equality(bset);
7681 if (!bset)
7682 set = NULL;
7683 else if (i == bset->n_eq)
7684 set = base_compute_divs(bset);
7685 else
7686 set = parameter_compute_divs(bset);
7687 set = set_parameter_preimage(set, T2);
7688 set = set_append_equalities(set, eq);
7689 return set;
7692 /* Insert the divs from "ls" before those of "bmap".
7694 * The number of columns is not changed, which means that the last
7695 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7696 * The caller is responsible for removing the same number of dimensions
7697 * from the space of "bmap".
7699 static __isl_give isl_basic_map *insert_divs_from_local_space(
7700 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7702 int i;
7703 isl_size n_div;
7704 int old_n_div;
7706 n_div = isl_local_space_dim(ls, isl_dim_div);
7707 if (n_div < 0)
7708 return isl_basic_map_free(bmap);
7709 if (n_div == 0)
7710 return bmap;
7712 old_n_div = bmap->n_div;
7713 bmap = insert_div_rows(bmap, n_div);
7714 if (!bmap)
7715 return NULL;
7717 for (i = 0; i < n_div; ++i) {
7718 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7719 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7722 return bmap;
7725 /* Replace the space of "bmap" by the space and divs of "ls".
7727 * If "ls" has any divs, then we simplify the result since we may
7728 * have discovered some additional equalities that could simplify
7729 * the div expressions.
7731 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7732 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7734 isl_size n_div;
7736 bmap = isl_basic_map_cow(bmap);
7737 n_div = isl_local_space_dim(ls, isl_dim_div);
7738 if (!bmap || n_div < 0)
7739 goto error;
7741 bmap = insert_divs_from_local_space(bmap, ls);
7742 if (!bmap)
7743 goto error;
7745 isl_space_free(bmap->dim);
7746 bmap->dim = isl_local_space_get_space(ls);
7747 if (!bmap->dim)
7748 goto error;
7750 isl_local_space_free(ls);
7751 if (n_div > 0)
7752 bmap = isl_basic_map_simplify(bmap);
7753 bmap = isl_basic_map_finalize(bmap);
7754 return bmap;
7755 error:
7756 isl_basic_map_free(bmap);
7757 isl_local_space_free(ls);
7758 return NULL;
7761 /* Replace the space of "map" by the space and divs of "ls".
7763 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7764 __isl_take isl_local_space *ls)
7766 int i;
7768 map = isl_map_cow(map);
7769 if (!map || !ls)
7770 goto error;
7772 for (i = 0; i < map->n; ++i) {
7773 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7774 isl_local_space_copy(ls));
7775 if (!map->p[i])
7776 goto error;
7778 isl_space_free(isl_map_take_space(map));
7779 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7781 isl_local_space_free(ls);
7782 return map;
7783 error:
7784 isl_local_space_free(ls);
7785 isl_map_free(map);
7786 return NULL;
7789 /* Compute an explicit representation for the existentially
7790 * quantified variables for which do not know any explicit representation yet.
7792 * We first sort the existentially quantified variables so that the
7793 * existentially quantified variables for which we already have an explicit
7794 * representation are placed before those for which we do not.
7795 * The input dimensions, the output dimensions and the existentially
7796 * quantified variables for which we already have an explicit
7797 * representation are then turned into parameters.
7798 * compute_divs returns a map with the same parameters and
7799 * no input or output dimensions and the dimension specification
7800 * is reset to that of the input, including the existentially quantified
7801 * variables for which we already had an explicit representation.
7803 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7805 struct isl_basic_set *bset;
7806 struct isl_set *set;
7807 struct isl_map *map;
7808 isl_space *space;
7809 isl_local_space *ls;
7810 isl_size nparam;
7811 isl_size n_in;
7812 isl_size n_out;
7813 int n_known;
7814 int i;
7816 bmap = isl_basic_map_sort_divs(bmap);
7817 bmap = isl_basic_map_cow(bmap);
7818 if (!bmap)
7819 return NULL;
7821 n_known = isl_basic_map_first_unknown_div(bmap);
7822 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7823 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7824 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7825 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7826 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7828 space = isl_space_set_alloc(bmap->ctx,
7829 nparam + n_in + n_out + n_known, 0);
7830 if (!space)
7831 goto error;
7833 ls = isl_basic_map_get_local_space(bmap);
7834 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7835 n_known, bmap->n_div - n_known);
7836 if (n_known > 0) {
7837 for (i = n_known; i < bmap->n_div; ++i)
7838 swap_div(bmap, i - n_known, i);
7839 bmap->n_div -= n_known;
7840 bmap->extra -= n_known;
7842 bmap = isl_basic_map_reset_space(bmap, space);
7843 bset = bset_from_bmap(bmap);
7845 set = parameter_compute_divs(bset);
7846 map = set_to_map(set);
7847 map = replace_space_by_local_space(map, ls);
7849 return map;
7850 error:
7851 isl_basic_map_free(bmap);
7852 return NULL;
7855 /* Remove the explicit representation of local variable "div",
7856 * if there is any.
7858 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7859 __isl_take isl_basic_map *bmap, int div)
7861 isl_bool unknown;
7863 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7864 if (unknown < 0)
7865 return isl_basic_map_free(bmap);
7866 if (unknown)
7867 return bmap;
7869 bmap = isl_basic_map_cow(bmap);
7870 if (!bmap)
7871 return NULL;
7872 isl_int_set_si(bmap->div[div][0], 0);
7873 return bmap;
7876 /* Is local variable "div" of "bmap" marked as not having an explicit
7877 * representation?
7878 * Note that even if "div" is not marked in this way and therefore
7879 * has an explicit representation, this representation may still
7880 * depend (indirectly) on other local variables that do not
7881 * have an explicit representation.
7883 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7884 int div)
7886 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7887 return isl_bool_error;
7888 return isl_int_is_zero(bmap->div[div][0]);
7891 /* Return the position of the first local variable that does not
7892 * have an explicit representation.
7893 * Return the total number of local variables if they all have
7894 * an explicit representation.
7895 * Return -1 on error.
7897 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7899 int i;
7901 if (!bmap)
7902 return -1;
7904 for (i = 0; i < bmap->n_div; ++i) {
7905 if (!isl_basic_map_div_is_known(bmap, i))
7906 return i;
7908 return bmap->n_div;
7911 /* Return the position of the first local variable that does not
7912 * have an explicit representation.
7913 * Return the total number of local variables if they all have
7914 * an explicit representation.
7915 * Return -1 on error.
7917 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7919 return isl_basic_map_first_unknown_div(bset);
7922 /* Does "bmap" have an explicit representation for all local variables?
7924 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7926 int first;
7927 isl_size n;
7929 n = isl_basic_map_dim(bmap, isl_dim_div);
7930 first = isl_basic_map_first_unknown_div(bmap);
7931 if (n < 0 || first < 0)
7932 return isl_bool_error;
7933 return first == n;
7936 /* Do all basic maps in "map" have an explicit representation
7937 * for all local variables?
7939 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7941 int i;
7943 if (!map)
7944 return isl_bool_error;
7946 for (i = 0; i < map->n; ++i) {
7947 int known = isl_basic_map_divs_known(map->p[i]);
7948 if (known <= 0)
7949 return known;
7952 return isl_bool_true;
7955 /* If bmap contains any unknown divs, then compute explicit
7956 * expressions for them. However, this computation may be
7957 * quite expensive, so first try to remove divs that aren't
7958 * strictly needed.
7960 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7962 int known;
7963 struct isl_map *map;
7965 known = isl_basic_map_divs_known(bmap);
7966 if (known < 0)
7967 goto error;
7968 if (known)
7969 return isl_map_from_basic_map(bmap);
7971 bmap = isl_basic_map_drop_redundant_divs(bmap);
7973 known = isl_basic_map_divs_known(bmap);
7974 if (known < 0)
7975 goto error;
7976 if (known)
7977 return isl_map_from_basic_map(bmap);
7979 map = compute_divs(bmap);
7980 return map;
7981 error:
7982 isl_basic_map_free(bmap);
7983 return NULL;
7986 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7988 int i;
7989 int known;
7990 struct isl_map *res;
7992 if (!map)
7993 return NULL;
7994 if (map->n == 0)
7995 return map;
7997 known = isl_map_divs_known(map);
7998 if (known < 0) {
7999 isl_map_free(map);
8000 return NULL;
8002 if (known)
8003 return map;
8005 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8006 for (i = 1 ; i < map->n; ++i) {
8007 struct isl_map *r2;
8008 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8009 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8010 res = isl_map_union_disjoint(res, r2);
8011 else
8012 res = isl_map_union(res, r2);
8014 isl_map_free(map);
8016 return res;
8019 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8021 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8024 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8026 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8029 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8031 int i;
8032 struct isl_set *set;
8034 if (!map)
8035 goto error;
8037 map = isl_map_cow(map);
8038 if (!map)
8039 return NULL;
8041 set = set_from_map(map);
8042 set->dim = isl_space_domain(set->dim);
8043 if (!set->dim)
8044 goto error;
8045 for (i = 0; i < map->n; ++i) {
8046 set->p[i] = isl_basic_map_domain(map->p[i]);
8047 if (!set->p[i])
8048 goto error;
8050 ISL_F_CLR(set, ISL_MAP_DISJOINT);
8051 ISL_F_CLR(set, ISL_SET_NORMALIZED);
8052 return set;
8053 error:
8054 isl_map_free(map);
8055 return NULL;
8058 /* Return the union of "map1" and "map2", where we assume for now that
8059 * "map1" and "map2" are disjoint. Note that the basic maps inside
8060 * "map1" or "map2" may not be disjoint from each other.
8061 * Also note that this function is also called from isl_map_union,
8062 * which takes care of handling the situation where "map1" and "map2"
8063 * may not be disjoint.
8065 * If one of the inputs is empty, we can simply return the other input.
8066 * Similarly, if one of the inputs is universal, then it is equal to the union.
8068 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8069 __isl_take isl_map *map2)
8071 int i;
8072 unsigned flags = 0;
8073 struct isl_map *map = NULL;
8074 int is_universe;
8076 if (isl_map_check_equal_space(map1, map2) < 0)
8077 goto error;
8079 if (map1->n == 0) {
8080 isl_map_free(map1);
8081 return map2;
8083 if (map2->n == 0) {
8084 isl_map_free(map2);
8085 return map1;
8088 is_universe = isl_map_plain_is_universe(map1);
8089 if (is_universe < 0)
8090 goto error;
8091 if (is_universe) {
8092 isl_map_free(map2);
8093 return map1;
8096 is_universe = isl_map_plain_is_universe(map2);
8097 if (is_universe < 0)
8098 goto error;
8099 if (is_universe) {
8100 isl_map_free(map1);
8101 return map2;
8104 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8105 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8106 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8108 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8109 map1->n + map2->n, flags);
8110 if (!map)
8111 goto error;
8112 for (i = 0; i < map1->n; ++i) {
8113 map = isl_map_add_basic_map(map,
8114 isl_basic_map_copy(map1->p[i]));
8115 if (!map)
8116 goto error;
8118 for (i = 0; i < map2->n; ++i) {
8119 map = isl_map_add_basic_map(map,
8120 isl_basic_map_copy(map2->p[i]));
8121 if (!map)
8122 goto error;
8124 isl_map_free(map1);
8125 isl_map_free(map2);
8126 return map;
8127 error:
8128 isl_map_free(map);
8129 isl_map_free(map1);
8130 isl_map_free(map2);
8131 return NULL;
8134 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8135 * guaranteed to be disjoint by the caller.
8137 * Note that this functions is called from within isl_map_make_disjoint,
8138 * so we have to be careful not to touch the constraints of the inputs
8139 * in any way.
8141 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8142 __isl_take isl_map *map2)
8144 isl_map_align_params_bin(&map1, &map2);
8145 return map_union_disjoint(map1, map2);
8148 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8149 * not be disjoint.
8151 * We currently simply call map_union_disjoint, the internal operation
8152 * of which does not really depend on the inputs being disjoint.
8153 * If the result contains more than one basic map, then we clear
8154 * the disjoint flag since the result may contain basic maps from
8155 * both inputs and these are not guaranteed to be disjoint.
8157 * As a special case, if "map1" and "map2" are obviously equal,
8158 * then we simply return "map1".
8160 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8161 __isl_take isl_map *map2)
8163 int equal;
8165 if (isl_map_align_params_bin(&map1, &map2) < 0)
8166 goto error;
8168 equal = isl_map_plain_is_equal(map1, map2);
8169 if (equal < 0)
8170 goto error;
8171 if (equal) {
8172 isl_map_free(map2);
8173 return map1;
8176 map1 = map_union_disjoint(map1, map2);
8177 if (!map1)
8178 return NULL;
8179 if (map1->n > 1)
8180 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8181 return map1;
8182 error:
8183 isl_map_free(map1);
8184 isl_map_free(map2);
8185 return NULL;
8188 __isl_give isl_set *isl_set_union_disjoint(
8189 __isl_take isl_set *set1, __isl_take isl_set *set2)
8191 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8192 set_to_map(set2)));
8195 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8196 __isl_take isl_set *set2)
8198 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8201 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8202 * the results in a map living in "space".
8204 * "map" and "set" are assumed to be compatible and non-NULL.
8206 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8207 __isl_take isl_space *space, __isl_take isl_set *set,
8208 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8209 __isl_take isl_basic_set *bset))
8211 unsigned flags = 0;
8212 struct isl_map *result;
8213 int i, j;
8215 if (isl_set_plain_is_universe(set)) {
8216 isl_set_free(set);
8217 return isl_map_reset_equal_dim_space(map, space);
8220 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8221 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8222 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8224 result = isl_map_alloc_space(space, map->n * set->n, flags);
8225 for (i = 0; result && i < map->n; ++i)
8226 for (j = 0; j < set->n; ++j) {
8227 result = isl_map_add_basic_map(result,
8228 fn(isl_basic_map_copy(map->p[i]),
8229 isl_basic_set_copy(set->p[j])));
8230 if (!result)
8231 break;
8234 isl_map_free(map);
8235 isl_set_free(set);
8236 return result;
8239 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8240 __isl_take isl_set *set)
8242 isl_bool ok;
8243 isl_space *space;
8245 isl_map_align_params_set(&map, &set);
8246 ok = isl_map_compatible_range(map, set);
8247 if (ok < 0)
8248 goto error;
8249 if (!ok)
8250 isl_die(set->ctx, isl_error_invalid,
8251 "incompatible spaces", goto error);
8253 space = isl_map_get_space(map);
8254 return map_intersect_set(map, space, set,
8255 &isl_basic_map_intersect_range);
8256 error:
8257 isl_map_free(map);
8258 isl_set_free(set);
8259 return NULL;
8262 /* Intersect the domain of "map" with "set".
8264 * If the domain dimensions of "map" do not have any identifiers,
8265 * then copy them over from "set".
8267 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8268 __isl_take isl_set *set)
8270 isl_bool ok;
8271 isl_space *space;
8273 isl_map_align_params_set(&map, &set);
8274 ok = isl_map_compatible_domain(map, set);
8275 if (ok < 0)
8276 goto error;
8277 if (!ok)
8278 isl_die(set->ctx, isl_error_invalid,
8279 "incompatible spaces", goto error);
8281 space = isl_map_get_space(map);
8282 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8283 isl_set_peek_space(set), isl_dim_set);
8284 return map_intersect_set(map, space, set,
8285 &isl_basic_map_intersect_domain);
8286 error:
8287 isl_map_free(map);
8288 isl_set_free(set);
8289 return NULL;
8292 /* Data structure that specifies how isl_map_intersect_factor
8293 * should operate.
8295 * "preserve_type" is the tuple where the factor differs from
8296 * the input map and of which the identifiers needs
8297 * to be preserved explicitly.
8298 * "other_factor" is used to extract the space of the other factor
8299 * from the space of the product ("map").
8300 * "product" is used to combine the given factor and a universe map
8301 * in the space returned by "other_factor" to produce a map
8302 * that lives in the same space as the input map.
8304 struct isl_intersect_factor_control {
8305 enum isl_dim_type preserve_type;
8306 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8307 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8308 __isl_take isl_map *other);
8311 /* Given a map "map" in some product space and a map "factor"
8312 * living in some factor space, return the intersection.
8314 * After aligning the parameters,
8315 * the map "factor" is first extended to a map living in the same space
8316 * as "map" and then a regular intersection is computed.
8318 * Note that the extension is computed as a product, which is anonymous
8319 * by default. If "map" has an identifier on the corresponding tuple,
8320 * then this identifier needs to be set on the product
8321 * before the intersection is computed.
8323 static __isl_give isl_map *isl_map_intersect_factor(
8324 __isl_take isl_map *map, __isl_take isl_map *factor,
8325 struct isl_intersect_factor_control *control)
8327 isl_bool equal, has_id;
8328 isl_id *id;
8329 isl_space *space;
8330 isl_map *other, *product;
8332 equal = isl_map_has_equal_params(map, factor);
8333 if (equal < 0)
8334 goto error;
8335 if (!equal) {
8336 map = isl_map_align_params(map, isl_map_get_space(factor));
8337 factor = isl_map_align_params(factor, isl_map_get_space(map));
8340 space = isl_map_get_space(map);
8341 has_id = isl_space_has_tuple_id(space, control->preserve_type);
8342 if (has_id < 0)
8343 space = isl_space_free(space);
8344 else if (has_id)
8345 id = isl_space_get_tuple_id(space, control->preserve_type);
8347 other = isl_map_universe(control->other_factor(space));
8348 product = control->product(factor, other);
8350 if (has_id >= 0 && has_id)
8351 product = isl_map_set_tuple_id(product,
8352 control->preserve_type, id);
8354 return map_intersect(map, product);
8355 error:
8356 isl_map_free(map);
8357 isl_map_free(factor);
8358 return NULL;
8361 /* Return the domain product of "map2" and "map1".
8363 static __isl_give isl_map *isl_map_reverse_domain_product(
8364 __isl_take isl_map *map1, __isl_take isl_map *map2)
8366 return isl_map_domain_product(map2, map1);
8369 /* Return the range product of "map2" and "map1".
8371 static __isl_give isl_map *isl_map_reverse_range_product(
8372 __isl_take isl_map *map1, __isl_take isl_map *map2)
8374 return isl_map_range_product(map2, map1);
8377 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8378 * in the space B -> C, return the intersection.
8380 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8381 __isl_take isl_map *map, __isl_take isl_map *factor)
8383 struct isl_intersect_factor_control control = {
8384 .preserve_type = isl_dim_in,
8385 .other_factor = isl_space_domain_factor_domain,
8386 .product = isl_map_reverse_domain_product,
8389 return isl_map_intersect_factor(map, factor, &control);
8392 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8393 * in the space A -> B, return the intersection.
8395 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8396 __isl_take isl_map *map, __isl_take isl_map *factor)
8398 struct isl_intersect_factor_control control = {
8399 .preserve_type = isl_dim_out,
8400 .other_factor = isl_space_range_factor_range,
8401 .product = isl_map_range_product,
8404 return isl_map_intersect_factor(map, factor, &control);
8407 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8408 * in the space A -> C, return the intersection.
8410 __isl_give isl_map *isl_map_intersect_range_factor_range(
8411 __isl_take isl_map *map, __isl_take isl_map *factor)
8413 struct isl_intersect_factor_control control = {
8414 .preserve_type = isl_dim_out,
8415 .other_factor = isl_space_range_factor_domain,
8416 .product = isl_map_reverse_range_product,
8419 return isl_map_intersect_factor(map, factor, &control);
8422 /* Given a set "set" in a space [A -> B] and a set "domain"
8423 * in the space A, return the intersection.
8425 * The set "domain" is first extended to a set living in the space
8426 * [A -> B] and then a regular intersection is computed.
8428 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8429 __isl_take isl_set *domain)
8431 struct isl_intersect_factor_control control = {
8432 .preserve_type = isl_dim_set,
8433 .other_factor = isl_space_factor_range,
8434 .product = isl_map_range_product,
8437 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8438 set_to_map(domain), &control));
8441 /* Given a set "set" in a space [A -> B] and a set "range"
8442 * in the space B, return the intersection.
8444 * The set "range" is first extended to a set living in the space
8445 * [A -> B] and then a regular intersection is computed.
8447 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8448 __isl_take isl_set *range)
8450 struct isl_intersect_factor_control control = {
8451 .preserve_type = isl_dim_set,
8452 .other_factor = isl_space_factor_domain,
8453 .product = isl_map_reverse_range_product,
8456 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8457 set_to_map(range), &control));
8460 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8461 __isl_take isl_map *map2)
8463 if (isl_map_align_params_bin(&map1, &map2) < 0)
8464 goto error;
8465 map1 = isl_map_reverse(map1);
8466 map1 = isl_map_apply_range(map1, map2);
8467 return isl_map_reverse(map1);
8468 error:
8469 isl_map_free(map1);
8470 isl_map_free(map2);
8471 return NULL;
8474 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8475 __isl_take isl_map *map2)
8477 isl_space *space;
8478 struct isl_map *result;
8479 int i, j;
8481 if (isl_map_align_params_bin(&map1, &map2) < 0)
8482 goto error;
8484 space = isl_space_join(isl_space_copy(map1->dim),
8485 isl_space_copy(map2->dim));
8487 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8488 if (!result)
8489 goto error;
8490 for (i = 0; i < map1->n; ++i)
8491 for (j = 0; j < map2->n; ++j) {
8492 result = isl_map_add_basic_map(result,
8493 isl_basic_map_apply_range(
8494 isl_basic_map_copy(map1->p[i]),
8495 isl_basic_map_copy(map2->p[j])));
8496 if (!result)
8497 goto error;
8499 isl_map_free(map1);
8500 isl_map_free(map2);
8501 if (result && result->n <= 1)
8502 ISL_F_SET(result, ISL_MAP_DISJOINT);
8503 return result;
8504 error:
8505 isl_map_free(map1);
8506 isl_map_free(map2);
8507 return NULL;
8510 /* Is "bmap" a transformation, i.e.,
8511 * does it relate elements from the same space.
8513 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8515 isl_space *space;
8517 space = isl_basic_map_peek_space(bmap);
8518 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8521 /* Check that "bmap" is a transformation, i.e.,
8522 * that it relates elements from the same space.
8524 static isl_stat isl_basic_map_check_transformation(
8525 __isl_keep isl_basic_map *bmap)
8527 isl_bool equal;
8529 equal = isl_basic_map_is_transformation(bmap);
8530 if (equal < 0)
8531 return isl_stat_error;
8532 if (!equal)
8533 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8534 "domain and range don't match", return isl_stat_error);
8535 return isl_stat_ok;
8539 * returns range - domain
8541 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8543 isl_space *target_space;
8544 struct isl_basic_set *bset;
8545 isl_size dim;
8546 isl_size nparam;
8547 isl_size total;
8548 int i;
8550 if (isl_basic_map_check_transformation(bmap) < 0)
8551 return isl_basic_map_free(bmap);
8552 dim = isl_basic_map_dim(bmap, isl_dim_in);
8553 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8554 if (dim < 0 || nparam < 0)
8555 goto error;
8556 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8557 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8558 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8559 total = isl_basic_map_dim(bmap, isl_dim_all);
8560 if (total < 0)
8561 bmap = isl_basic_map_free(bmap);
8562 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8563 for (i = 0; i < dim; ++i) {
8564 int j = isl_basic_map_alloc_equality(bmap);
8565 if (j < 0) {
8566 bmap = isl_basic_map_free(bmap);
8567 break;
8569 isl_seq_clr(bmap->eq[j], 1 + total);
8570 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8571 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8572 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8574 bset = isl_basic_map_domain(bmap);
8575 bset = isl_basic_set_reset_space(bset, target_space);
8576 return bset;
8577 error:
8578 isl_basic_map_free(bmap);
8579 return NULL;
8582 /* Is the tuple of type "type1" of "map" the same as
8583 * the tuple of type "type2" of "space"?
8585 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8586 enum isl_dim_type type1, __isl_keep isl_space *space,
8587 enum isl_dim_type type2)
8589 isl_space *map_space;
8591 map_space = isl_map_peek_space(map);
8592 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8595 /* Is the tuple of type "type1" of "map1" the same as
8596 * the tuple of type "type2" of "map2"?
8598 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8599 enum isl_dim_type type1, __isl_keep isl_map *map2,
8600 enum isl_dim_type type2)
8602 isl_space *space1, *space2;
8604 space1 = isl_map_peek_space(map1);
8605 space2 = isl_map_peek_space(map2);
8606 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8609 /* Check that "map" is a transformation, i.e.,
8610 * that it relates elements from the same space.
8612 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8614 isl_bool equal;
8616 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8617 if (equal < 0)
8618 return isl_stat_error;
8619 if (!equal)
8620 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8621 "domain and range don't match", return isl_stat_error);
8622 return isl_stat_ok;
8626 * returns range - domain
8628 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8630 int i;
8631 isl_space *space;
8632 struct isl_set *result;
8634 if (isl_map_check_transformation(map) < 0)
8635 goto error;
8636 space = isl_map_get_space(map);
8637 space = isl_space_domain(space);
8638 result = isl_set_alloc_space(space, map->n, 0);
8639 if (!result)
8640 goto error;
8641 for (i = 0; i < map->n; ++i)
8642 result = isl_set_add_basic_set(result,
8643 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8644 isl_map_free(map);
8645 return result;
8646 error:
8647 isl_map_free(map);
8648 return NULL;
8652 * returns [domain -> range] -> range - domain
8654 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8655 __isl_take isl_basic_map *bmap)
8657 int i, k;
8658 isl_space *space;
8659 isl_basic_map *domain;
8660 isl_size nparam, n;
8661 isl_size total;
8663 if (isl_basic_map_check_transformation(bmap) < 0)
8664 return isl_basic_map_free(bmap);
8666 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8667 n = isl_basic_map_dim(bmap, isl_dim_in);
8668 if (nparam < 0 || n < 0)
8669 return isl_basic_map_free(bmap);
8671 space = isl_basic_map_get_space(bmap);
8672 space = isl_space_from_range(isl_space_domain(space));
8673 domain = isl_basic_map_universe(space);
8675 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8676 bmap = isl_basic_map_apply_range(bmap, domain);
8677 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8679 total = isl_basic_map_dim(bmap, isl_dim_all);
8680 if (total < 0)
8681 return isl_basic_map_free(bmap);
8683 for (i = 0; i < n; ++i) {
8684 k = isl_basic_map_alloc_equality(bmap);
8685 if (k < 0)
8686 goto error;
8687 isl_seq_clr(bmap->eq[k], 1 + total);
8688 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8689 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8690 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8693 bmap = isl_basic_map_gauss(bmap, NULL);
8694 return isl_basic_map_finalize(bmap);
8695 error:
8696 isl_basic_map_free(bmap);
8697 return NULL;
8701 * returns [domain -> range] -> range - domain
8703 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8705 if (isl_map_check_transformation(map) < 0)
8706 return isl_map_free(map);
8708 return isl_map_transform(map, &isl_space_range_map,
8709 &isl_basic_map_deltas_map);
8712 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8714 isl_size n_in, n_out;
8716 n_in = isl_space_dim(space, isl_dim_in);
8717 n_out = isl_space_dim(space, isl_dim_out);
8718 if (n_in < 0 || n_out < 0)
8719 goto error;
8720 if (n_in != n_out)
8721 isl_die(space->ctx, isl_error_invalid,
8722 "number of input and output dimensions needs to be "
8723 "the same", goto error);
8724 return isl_basic_map_equal(space, n_in);
8725 error:
8726 isl_space_free(space);
8727 return NULL;
8730 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8732 return isl_map_from_basic_map(isl_basic_map_identity(space));
8735 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8737 isl_space *space = isl_set_get_space(set);
8738 isl_map *id;
8739 id = isl_map_identity(isl_space_map_from_set(space));
8740 return isl_map_intersect_range(id, set);
8743 /* Construct a basic set with all set dimensions having only non-negative
8744 * values.
8746 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8747 __isl_take isl_space *space)
8749 int i;
8750 isl_size nparam;
8751 isl_size dim;
8752 isl_size total;
8753 struct isl_basic_set *bset;
8755 nparam = isl_space_dim(space, isl_dim_param);
8756 dim = isl_space_dim(space, isl_dim_set);
8757 total = isl_space_dim(space, isl_dim_all);
8758 if (nparam < 0 || dim < 0 || total < 0)
8759 space = isl_space_free(space);
8760 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8761 if (!bset)
8762 return NULL;
8763 for (i = 0; i < dim; ++i) {
8764 int k = isl_basic_set_alloc_inequality(bset);
8765 if (k < 0)
8766 goto error;
8767 isl_seq_clr(bset->ineq[k], 1 + total);
8768 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8770 return bset;
8771 error:
8772 isl_basic_set_free(bset);
8773 return NULL;
8776 /* Construct the half-space x_pos >= 0.
8778 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8779 int pos)
8781 int k;
8782 isl_size total;
8783 isl_basic_set *nonneg;
8785 total = isl_space_dim(space, isl_dim_all);
8786 if (total < 0)
8787 space = isl_space_free(space);
8788 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8789 k = isl_basic_set_alloc_inequality(nonneg);
8790 if (k < 0)
8791 goto error;
8792 isl_seq_clr(nonneg->ineq[k], 1 + total);
8793 isl_int_set_si(nonneg->ineq[k][pos], 1);
8795 return isl_basic_set_finalize(nonneg);
8796 error:
8797 isl_basic_set_free(nonneg);
8798 return NULL;
8801 /* Construct the half-space x_pos <= -1.
8803 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8804 int pos)
8806 int k;
8807 isl_size total;
8808 isl_basic_set *neg;
8810 total = isl_space_dim(space, isl_dim_all);
8811 if (total < 0)
8812 space = isl_space_free(space);
8813 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8814 k = isl_basic_set_alloc_inequality(neg);
8815 if (k < 0)
8816 goto error;
8817 isl_seq_clr(neg->ineq[k], 1 + total);
8818 isl_int_set_si(neg->ineq[k][0], -1);
8819 isl_int_set_si(neg->ineq[k][pos], -1);
8821 return isl_basic_set_finalize(neg);
8822 error:
8823 isl_basic_set_free(neg);
8824 return NULL;
8827 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8828 enum isl_dim_type type, unsigned first, unsigned n)
8830 int i;
8831 unsigned offset;
8832 isl_basic_set *nonneg;
8833 isl_basic_set *neg;
8835 if (n == 0)
8836 return set;
8838 if (isl_set_check_range(set, type, first, n) < 0)
8839 return isl_set_free(set);
8841 offset = pos(set->dim, type);
8842 for (i = 0; i < n; ++i) {
8843 nonneg = nonneg_halfspace(isl_set_get_space(set),
8844 offset + first + i);
8845 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8847 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8850 return set;
8853 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8854 int len,
8855 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8856 void *user)
8858 isl_set *half;
8860 if (!set)
8861 return isl_stat_error;
8862 if (isl_set_plain_is_empty(set)) {
8863 isl_set_free(set);
8864 return isl_stat_ok;
8866 if (first == len)
8867 return fn(set, signs, user);
8869 signs[first] = 1;
8870 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8871 1 + first));
8872 half = isl_set_intersect(half, isl_set_copy(set));
8873 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8874 goto error;
8876 signs[first] = -1;
8877 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8878 1 + first));
8879 half = isl_set_intersect(half, set);
8880 return foreach_orthant(half, signs, first + 1, len, fn, user);
8881 error:
8882 isl_set_free(set);
8883 return isl_stat_error;
8886 /* Call "fn" on the intersections of "set" with each of the orthants
8887 * (except for obviously empty intersections). The orthant is identified
8888 * by the signs array, with each entry having value 1 or -1 according
8889 * to the sign of the corresponding variable.
8891 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8892 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8893 void *user)
8895 isl_size nparam;
8896 isl_size nvar;
8897 int *signs;
8898 isl_stat r;
8900 if (!set)
8901 return isl_stat_error;
8902 if (isl_set_plain_is_empty(set))
8903 return isl_stat_ok;
8905 nparam = isl_set_dim(set, isl_dim_param);
8906 nvar = isl_set_dim(set, isl_dim_set);
8907 if (nparam < 0 || nvar < 0)
8908 return isl_stat_error;
8910 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8912 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8913 fn, user);
8915 free(signs);
8917 return r;
8920 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8922 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8925 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8926 __isl_keep isl_basic_map *bmap2)
8928 isl_bool is_subset;
8929 struct isl_map *map1;
8930 struct isl_map *map2;
8932 if (!bmap1 || !bmap2)
8933 return isl_bool_error;
8935 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8936 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8938 is_subset = isl_map_is_subset(map1, map2);
8940 isl_map_free(map1);
8941 isl_map_free(map2);
8943 return is_subset;
8946 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8947 __isl_keep isl_basic_set *bset2)
8949 return isl_basic_map_is_subset(bset1, bset2);
8952 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8953 __isl_keep isl_basic_map *bmap2)
8955 isl_bool is_subset;
8957 if (!bmap1 || !bmap2)
8958 return isl_bool_error;
8959 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8960 if (is_subset != isl_bool_true)
8961 return is_subset;
8962 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8963 return is_subset;
8966 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8967 __isl_keep isl_basic_set *bset2)
8969 return isl_basic_map_is_equal(
8970 bset_to_bmap(bset1), bset_to_bmap(bset2));
8973 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8975 int i;
8976 int is_empty;
8978 if (!map)
8979 return isl_bool_error;
8980 for (i = 0; i < map->n; ++i) {
8981 is_empty = isl_basic_map_is_empty(map->p[i]);
8982 if (is_empty < 0)
8983 return isl_bool_error;
8984 if (!is_empty)
8985 return isl_bool_false;
8987 return isl_bool_true;
8990 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8992 return map ? map->n == 0 : isl_bool_error;
8995 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8997 return set ? set->n == 0 : isl_bool_error;
9000 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9002 return isl_map_is_empty(set_to_map(set));
9005 #undef TYPE
9006 #define TYPE isl_basic_map
9008 static
9009 #include "isl_type_has_equal_space_bin_templ.c"
9010 #include "isl_type_check_equal_space_templ.c"
9012 /* Check that "bset1" and "bset2" live in the same space,
9013 * reporting an error if they do not.
9015 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9016 __isl_keep isl_basic_set *bset2)
9018 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9019 bset_to_bmap(bset1));
9022 #undef TYPE
9023 #define TYPE isl_map
9025 #include "isl_type_has_equal_space_bin_templ.c"
9026 #include "isl_type_check_equal_space_templ.c"
9028 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9029 __isl_keep isl_set *set2)
9031 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9034 #undef TYPE1
9035 #define TYPE1 isl_map
9036 #undef TYPE2
9037 #define TYPE2 isl_basic_map
9038 #undef TYPE_PAIR
9039 #define TYPE_PAIR isl_map_basic_map
9041 static
9042 #include "isl_type_has_equal_space_templ.c"
9043 #include "isl_type_check_equal_space_templ.c"
9045 /* Check that "set" and "bset" live in the same space,
9046 * reporting an error if they do not.
9048 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9049 __isl_keep isl_basic_set *bset)
9051 return isl_map_basic_map_check_equal_space(set_to_map(set),
9052 bset_to_bmap(bset));
9055 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9057 isl_bool is_subset;
9059 if (!map1 || !map2)
9060 return isl_bool_error;
9061 is_subset = isl_map_is_subset(map1, map2);
9062 if (is_subset != isl_bool_true)
9063 return is_subset;
9064 is_subset = isl_map_is_subset(map2, map1);
9065 return is_subset;
9068 /* Is "map1" equal to "map2"?
9070 * First check if they are obviously equal.
9071 * If not, then perform a more detailed analysis.
9073 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9075 isl_bool equal;
9077 equal = isl_map_plain_is_equal(map1, map2);
9078 if (equal < 0 || equal)
9079 return equal;
9080 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9083 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9084 __isl_keep isl_basic_map *bmap2)
9086 isl_bool is_subset;
9088 if (!bmap1 || !bmap2)
9089 return isl_bool_error;
9090 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9091 if (is_subset != isl_bool_true)
9092 return is_subset;
9093 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9094 return isl_bool_not(is_subset);
9097 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9098 __isl_keep isl_map *map2)
9100 isl_bool is_subset;
9102 if (!map1 || !map2)
9103 return isl_bool_error;
9104 is_subset = isl_map_is_subset(map1, map2);
9105 if (is_subset != isl_bool_true)
9106 return is_subset;
9107 is_subset = isl_map_is_subset(map2, map1);
9108 return isl_bool_not(is_subset);
9111 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9112 __isl_keep isl_set *set2)
9114 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9117 /* Is "bmap" obviously equal to the universe with the same space?
9119 * That is, does it not have any constraints?
9121 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9123 if (!bmap)
9124 return isl_bool_error;
9125 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9128 /* Is "bset" obviously equal to the universe with the same space?
9130 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9132 return isl_basic_map_plain_is_universe(bset);
9135 /* If "c" does not involve any existentially quantified variables,
9136 * then set *univ to false and abort
9138 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9140 isl_bool *univ = user;
9141 isl_size n;
9143 n = isl_constraint_dim(c, isl_dim_div);
9144 if (n < 0)
9145 c = isl_constraint_free(c);
9146 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9147 isl_constraint_free(c);
9148 if (*univ < 0 || !*univ)
9149 return isl_stat_error;
9150 return isl_stat_ok;
9153 /* Is "bmap" equal to the universe with the same space?
9155 * First check if it is obviously equal to the universe.
9156 * If not and if there are any constraints not involving
9157 * existentially quantified variables, then it is certainly
9158 * not equal to the universe.
9159 * Otherwise, check if the universe is a subset of "bmap".
9161 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9163 isl_size n_div;
9164 isl_bool univ;
9165 isl_basic_map *test;
9167 univ = isl_basic_map_plain_is_universe(bmap);
9168 if (univ < 0 || univ)
9169 return univ;
9170 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9171 if (n_div < 0)
9172 return isl_bool_error;
9173 if (n_div == 0)
9174 return isl_bool_false;
9175 univ = isl_bool_true;
9176 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9177 univ)
9178 return isl_bool_error;
9179 if (univ < 0 || !univ)
9180 return univ;
9181 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9182 univ = isl_basic_map_is_subset(test, bmap);
9183 isl_basic_map_free(test);
9184 return univ;
9187 /* Is "bset" equal to the universe with the same space?
9189 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9191 return isl_basic_map_is_universe(bset);
9194 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9196 int i;
9198 if (!map)
9199 return isl_bool_error;
9201 for (i = 0; i < map->n; ++i) {
9202 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9203 if (r < 0 || r)
9204 return r;
9207 return isl_bool_false;
9210 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9212 return isl_map_plain_is_universe(set_to_map(set));
9215 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9217 struct isl_basic_set *bset = NULL;
9218 struct isl_vec *sample = NULL;
9219 isl_bool empty, non_empty;
9221 if (!bmap)
9222 return isl_bool_error;
9224 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9225 return isl_bool_true;
9227 if (isl_basic_map_plain_is_universe(bmap))
9228 return isl_bool_false;
9230 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9231 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9232 copy = isl_basic_map_remove_redundancies(copy);
9233 empty = isl_basic_map_plain_is_empty(copy);
9234 isl_basic_map_free(copy);
9235 return empty;
9238 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9239 if (non_empty < 0)
9240 return isl_bool_error;
9241 if (non_empty)
9242 return isl_bool_false;
9243 isl_vec_free(bmap->sample);
9244 bmap->sample = NULL;
9245 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9246 if (!bset)
9247 return isl_bool_error;
9248 sample = isl_basic_set_sample_vec(bset);
9249 if (!sample)
9250 return isl_bool_error;
9251 empty = sample->size == 0;
9252 isl_vec_free(bmap->sample);
9253 bmap->sample = sample;
9254 if (empty)
9255 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9257 return empty;
9260 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9262 if (!bmap)
9263 return isl_bool_error;
9264 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9267 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9269 if (!bset)
9270 return isl_bool_error;
9271 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9274 /* Is "bmap" known to be non-empty?
9276 * That is, is the cached sample still valid?
9278 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9280 isl_size total;
9282 if (!bmap)
9283 return isl_bool_error;
9284 if (!bmap->sample)
9285 return isl_bool_false;
9286 total = isl_basic_map_dim(bmap, isl_dim_all);
9287 if (total < 0)
9288 return isl_bool_error;
9289 if (bmap->sample->size != 1 + total)
9290 return isl_bool_false;
9291 return isl_basic_map_contains(bmap, bmap->sample);
9294 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9296 return isl_basic_map_is_empty(bset_to_bmap(bset));
9299 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9300 __isl_take isl_basic_map *bmap2)
9302 struct isl_map *map;
9304 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9305 goto error;
9307 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9308 if (!map)
9309 goto error;
9310 map = isl_map_add_basic_map(map, bmap1);
9311 map = isl_map_add_basic_map(map, bmap2);
9312 return map;
9313 error:
9314 isl_basic_map_free(bmap1);
9315 isl_basic_map_free(bmap2);
9316 return NULL;
9319 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9320 __isl_take isl_basic_set *bset2)
9322 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9323 bset_to_bmap(bset2)));
9326 /* Order divs such that any div only depends on previous divs */
9327 __isl_give isl_basic_map *isl_basic_map_order_divs(
9328 __isl_take isl_basic_map *bmap)
9330 int i;
9331 isl_size off;
9333 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9334 if (off < 0)
9335 return isl_basic_map_free(bmap);
9337 for (i = 0; i < bmap->n_div; ++i) {
9338 int pos;
9339 if (isl_int_is_zero(bmap->div[i][0]))
9340 continue;
9341 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9342 bmap->n_div-i);
9343 if (pos == -1)
9344 continue;
9345 if (pos == 0)
9346 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9347 "integer division depends on itself",
9348 return isl_basic_map_free(bmap));
9349 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9350 if (!bmap)
9351 return NULL;
9352 --i;
9354 return bmap;
9357 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9359 int i;
9361 if (!map)
9362 return 0;
9364 for (i = 0; i < map->n; ++i) {
9365 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9366 if (!map->p[i])
9367 goto error;
9370 return map;
9371 error:
9372 isl_map_free(map);
9373 return NULL;
9376 /* Sort the local variables of "bset".
9378 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9379 __isl_take isl_basic_set *bset)
9381 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9384 /* Apply the expansion computed by isl_merge_divs.
9385 * The expansion itself is given by "exp" while the resulting
9386 * list of divs is given by "div".
9388 * Move the integer divisions of "bmap" into the right position
9389 * according to "exp" and then introduce the additional integer
9390 * divisions, adding div constraints.
9391 * The moving should be done first to avoid moving coefficients
9392 * in the definitions of the extra integer divisions.
9394 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9395 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9397 int i, j;
9398 int n_div;
9400 bmap = isl_basic_map_cow(bmap);
9401 if (!bmap || !div)
9402 goto error;
9404 if (div->n_row < bmap->n_div)
9405 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9406 "not an expansion", goto error);
9408 n_div = bmap->n_div;
9409 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9410 2 * (div->n_row - n_div));
9412 for (i = n_div; i < div->n_row; ++i)
9413 if (isl_basic_map_alloc_div(bmap) < 0)
9414 goto error;
9416 for (j = n_div - 1; j >= 0; --j) {
9417 if (exp[j] == j)
9418 break;
9419 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9420 if (!bmap)
9421 goto error;
9423 j = 0;
9424 for (i = 0; i < div->n_row; ++i) {
9425 if (j < n_div && exp[j] == i) {
9426 j++;
9427 } else {
9428 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9429 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9430 continue;
9431 bmap = isl_basic_map_add_div_constraints(bmap, i);
9432 if (!bmap)
9433 goto error;
9437 isl_mat_free(div);
9438 return bmap;
9439 error:
9440 isl_basic_map_free(bmap);
9441 isl_mat_free(div);
9442 return NULL;
9445 /* Apply the expansion computed by isl_merge_divs.
9446 * The expansion itself is given by "exp" while the resulting
9447 * list of divs is given by "div".
9449 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9450 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9452 return isl_basic_map_expand_divs(bset, div, exp);
9455 /* Look for a div in dst that corresponds to the div "div" in src.
9456 * The divs before "div" in src and dst are assumed to be the same.
9458 * Return the position of the corresponding div in dst
9459 * if there is one. Otherwise, return a position beyond the integer divisions.
9460 * Return -1 on error.
9462 static int find_div(__isl_keep isl_basic_map *dst,
9463 __isl_keep isl_basic_map *src, unsigned div)
9465 int i;
9466 isl_size n_div;
9467 isl_size v_div;
9469 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9470 n_div = isl_basic_map_dim(dst, isl_dim_div);
9471 if (n_div < 0 || v_div < 0)
9472 return -1;
9473 isl_assert(dst->ctx, div <= n_div, return -1);
9474 for (i = div; i < n_div; ++i)
9475 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9476 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9477 n_div - div) == -1)
9478 return i;
9479 return n_div;
9482 /* Align the divs of "dst" to those of "src", adding divs from "src"
9483 * if needed. That is, make sure that the first src->n_div divs
9484 * of the result are equal to those of src.
9486 * The result is not finalized as by design it will have redundant
9487 * divs if any divs from "src" were copied.
9489 __isl_give isl_basic_map *isl_basic_map_align_divs(
9490 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9492 int i;
9493 isl_bool known;
9494 int extended;
9495 isl_size v_div;
9496 isl_size dst_n_div;
9498 if (!dst || !src)
9499 return isl_basic_map_free(dst);
9501 if (src->n_div == 0)
9502 return dst;
9504 known = isl_basic_map_divs_known(src);
9505 if (known < 0)
9506 return isl_basic_map_free(dst);
9507 if (!known)
9508 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9509 "some src divs are unknown",
9510 return isl_basic_map_free(dst));
9512 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9513 if (v_div < 0)
9514 return isl_basic_map_free(dst);
9516 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
9517 if (!src)
9518 return isl_basic_map_free(dst);
9520 extended = 0;
9521 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9522 if (dst_n_div < 0)
9523 dst = isl_basic_map_free(dst);
9524 for (i = 0; i < src->n_div; ++i) {
9525 int j = find_div(dst, src, i);
9526 if (j < 0)
9527 dst = isl_basic_map_free(dst);
9528 if (j == dst_n_div) {
9529 if (!extended) {
9530 int extra = src->n_div - i;
9531 dst = isl_basic_map_cow(dst);
9532 if (!dst)
9533 goto error;
9534 dst = isl_basic_map_extend(dst,
9535 extra, 0, 2 * extra);
9536 extended = 1;
9538 j = isl_basic_map_alloc_div(dst);
9539 if (j < 0)
9540 goto error;
9541 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9542 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9543 dst_n_div++;
9544 dst = isl_basic_map_add_div_constraints(dst, j);
9545 if (!dst)
9546 goto error;
9548 if (j != i)
9549 dst = isl_basic_map_swap_div(dst, i, j);
9550 if (!dst)
9551 goto error;
9553 isl_basic_map_free(src);
9554 return dst;
9555 error:
9556 isl_basic_map_free(src);
9557 isl_basic_map_free(dst);
9558 return NULL;
9561 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9563 int i;
9565 if (!map)
9566 return NULL;
9567 if (map->n == 0)
9568 return map;
9569 map = isl_map_compute_divs(map);
9570 map = isl_map_cow(map);
9571 if (!map)
9572 return NULL;
9574 for (i = 1; i < map->n; ++i)
9575 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9576 for (i = 1; i < map->n; ++i) {
9577 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9578 if (!map->p[i])
9579 return isl_map_free(map);
9582 map = isl_map_unmark_normalized(map);
9583 return map;
9586 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9588 return isl_map_align_divs_internal(map);
9591 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9593 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9596 /* Align the divs of the basic maps in "map" to those
9597 * of the basic maps in "list", as well as to the other basic maps in "map".
9598 * The elements in "list" are assumed to have known divs.
9600 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9601 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9603 int i;
9604 isl_size n;
9606 n = isl_basic_map_list_n_basic_map(list);
9607 map = isl_map_compute_divs(map);
9608 map = isl_map_cow(map);
9609 if (!map || n < 0)
9610 return isl_map_free(map);
9611 if (map->n == 0)
9612 return map;
9614 for (i = 0; i < n; ++i) {
9615 isl_basic_map *bmap;
9617 bmap = isl_basic_map_list_get_basic_map(list, i);
9618 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9619 isl_basic_map_free(bmap);
9621 if (!map->p[0])
9622 return isl_map_free(map);
9624 return isl_map_align_divs_internal(map);
9627 /* Align the divs of each element of "list" to those of "bmap".
9628 * Both "bmap" and the elements of "list" are assumed to have known divs.
9630 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9631 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9633 int i;
9634 isl_size n;
9636 n = isl_basic_map_list_n_basic_map(list);
9637 if (n < 0 || !bmap)
9638 return isl_basic_map_list_free(list);
9640 for (i = 0; i < n; ++i) {
9641 isl_basic_map *bmap_i;
9643 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9644 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9645 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9648 return list;
9651 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9652 __isl_take isl_map *map)
9654 isl_bool ok;
9656 isl_map_align_params_set(&map, &set);
9657 ok = isl_map_compatible_domain(map, set);
9658 if (ok < 0)
9659 goto error;
9660 if (!ok)
9661 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9662 "incompatible spaces", goto error);
9663 map = isl_map_intersect_domain(map, set);
9664 set = isl_map_range(map);
9665 return set;
9666 error:
9667 isl_set_free(set);
9668 isl_map_free(map);
9669 return NULL;
9672 /* There is no need to cow as removing empty parts doesn't change
9673 * the meaning of the set.
9675 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9677 int i;
9679 if (!map)
9680 return NULL;
9682 for (i = map->n - 1; i >= 0; --i)
9683 map = remove_if_empty(map, i);
9685 return map;
9688 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9690 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9693 /* Create a binary relation that maps the shared initial "pos" dimensions
9694 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9696 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9697 __isl_keep isl_basic_set *bset2, int pos)
9699 isl_basic_map *bmap1;
9700 isl_basic_map *bmap2;
9702 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9703 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9704 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9705 isl_dim_out, 0, pos);
9706 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9707 isl_dim_out, 0, pos);
9708 return isl_basic_map_range_product(bmap1, bmap2);
9711 /* Given two basic sets bset1 and bset2, compute the maximal difference
9712 * between the values of dimension pos in bset1 and those in bset2
9713 * for any common value of the parameters and dimensions preceding pos.
9715 static enum isl_lp_result basic_set_maximal_difference_at(
9716 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9717 int pos, isl_int *opt)
9719 isl_basic_map *bmap1;
9720 struct isl_ctx *ctx;
9721 struct isl_vec *obj;
9722 isl_size total;
9723 isl_size nparam;
9724 isl_size dim1;
9725 enum isl_lp_result res;
9727 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9728 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9729 if (nparam < 0 || dim1 < 0 || !bset2)
9730 return isl_lp_error;
9732 bmap1 = join_initial(bset1, bset2, pos);
9733 total = isl_basic_map_dim(bmap1, isl_dim_all);
9734 if (total < 0)
9735 return isl_lp_error;
9737 ctx = bmap1->ctx;
9738 obj = isl_vec_alloc(ctx, 1 + total);
9739 if (!obj)
9740 goto error;
9741 isl_seq_clr(obj->block.data, 1 + total);
9742 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9743 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9744 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9745 opt, NULL, NULL);
9746 isl_basic_map_free(bmap1);
9747 isl_vec_free(obj);
9748 return res;
9749 error:
9750 isl_basic_map_free(bmap1);
9751 return isl_lp_error;
9754 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9755 * for any common value of the parameters and dimensions preceding pos
9756 * in both basic sets, the values of dimension pos in bset1 are
9757 * smaller or larger than those in bset2.
9759 * Returns
9760 * 1 if bset1 follows bset2
9761 * -1 if bset1 precedes bset2
9762 * 0 if bset1 and bset2 are incomparable
9763 * -2 if some error occurred.
9765 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9766 __isl_keep isl_basic_set *bset2, int pos)
9768 isl_int opt;
9769 enum isl_lp_result res;
9770 int cmp;
9772 isl_int_init(opt);
9774 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9776 if (res == isl_lp_empty)
9777 cmp = 0;
9778 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9779 res == isl_lp_unbounded)
9780 cmp = 1;
9781 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9782 cmp = -1;
9783 else
9784 cmp = -2;
9786 isl_int_clear(opt);
9787 return cmp;
9790 /* Given two basic sets bset1 and bset2, check whether
9791 * for any common value of the parameters and dimensions preceding pos
9792 * there is a value of dimension pos in bset1 that is larger
9793 * than a value of the same dimension in bset2.
9795 * Return
9796 * 1 if there exists such a pair
9797 * 0 if there is no such pair, but there is a pair of equal values
9798 * -1 otherwise
9799 * -2 if some error occurred.
9801 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9802 __isl_keep isl_basic_set *bset2, int pos)
9804 isl_bool empty;
9805 isl_basic_map *bmap;
9806 isl_size dim1;
9808 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9809 if (dim1 < 0)
9810 return -2;
9811 bmap = join_initial(bset1, bset2, pos);
9812 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9813 isl_dim_out, dim1 - pos);
9814 empty = isl_basic_map_is_empty(bmap);
9815 if (empty < 0)
9816 goto error;
9817 if (empty) {
9818 isl_basic_map_free(bmap);
9819 return -1;
9821 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9822 isl_dim_out, dim1 - pos);
9823 empty = isl_basic_map_is_empty(bmap);
9824 if (empty < 0)
9825 goto error;
9826 isl_basic_map_free(bmap);
9827 if (empty)
9828 return 0;
9829 return 1;
9830 error:
9831 isl_basic_map_free(bmap);
9832 return -2;
9835 /* Given two sets set1 and set2, check whether
9836 * for any common value of the parameters and dimensions preceding pos
9837 * there is a value of dimension pos in set1 that is larger
9838 * than a value of the same dimension in set2.
9840 * Return
9841 * 1 if there exists such a pair
9842 * 0 if there is no such pair, but there is a pair of equal values
9843 * -1 otherwise
9844 * -2 if some error occurred.
9846 int isl_set_follows_at(__isl_keep isl_set *set1,
9847 __isl_keep isl_set *set2, int pos)
9849 int i, j;
9850 int follows = -1;
9852 if (!set1 || !set2)
9853 return -2;
9855 for (i = 0; i < set1->n; ++i)
9856 for (j = 0; j < set2->n; ++j) {
9857 int f;
9858 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9859 if (f == 1 || f == -2)
9860 return f;
9861 if (f > follows)
9862 follows = f;
9865 return follows;
9868 static isl_bool isl_basic_map_plain_has_fixed_var(
9869 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9871 int i;
9872 int d;
9873 isl_size total;
9875 total = isl_basic_map_dim(bmap, isl_dim_all);
9876 if (total < 0)
9877 return isl_bool_error;
9878 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9879 for (; d+1 > pos; --d)
9880 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9881 break;
9882 if (d != pos)
9883 continue;
9884 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9885 return isl_bool_false;
9886 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9887 return isl_bool_false;
9888 if (!isl_int_is_one(bmap->eq[i][1+d]))
9889 return isl_bool_false;
9890 if (val)
9891 isl_int_neg(*val, bmap->eq[i][0]);
9892 return isl_bool_true;
9894 return isl_bool_false;
9897 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9898 unsigned pos, isl_int *val)
9900 int i;
9901 isl_int v;
9902 isl_int tmp;
9903 isl_bool fixed;
9905 if (!map)
9906 return isl_bool_error;
9907 if (map->n == 0)
9908 return isl_bool_false;
9909 if (map->n == 1)
9910 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9911 isl_int_init(v);
9912 isl_int_init(tmp);
9913 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9914 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9915 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9916 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9917 fixed = isl_bool_false;
9919 if (val)
9920 isl_int_set(*val, v);
9921 isl_int_clear(tmp);
9922 isl_int_clear(v);
9923 return fixed;
9926 static isl_bool isl_basic_set_plain_has_fixed_var(
9927 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9929 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9930 pos, val);
9933 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9934 enum isl_dim_type type, unsigned pos, isl_int *val)
9936 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9937 return isl_bool_error;
9938 return isl_basic_map_plain_has_fixed_var(bmap,
9939 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9942 /* If "bmap" obviously lies on a hyperplane where the given dimension
9943 * has a fixed value, then return that value.
9944 * Otherwise return NaN.
9946 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9947 __isl_keep isl_basic_map *bmap,
9948 enum isl_dim_type type, unsigned pos)
9950 isl_ctx *ctx;
9951 isl_val *v;
9952 isl_bool fixed;
9954 if (!bmap)
9955 return NULL;
9956 ctx = isl_basic_map_get_ctx(bmap);
9957 v = isl_val_alloc(ctx);
9958 if (!v)
9959 return NULL;
9960 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9961 if (fixed < 0)
9962 return isl_val_free(v);
9963 if (fixed) {
9964 isl_int_set_si(v->d, 1);
9965 return v;
9967 isl_val_free(v);
9968 return isl_val_nan(ctx);
9971 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9972 enum isl_dim_type type, unsigned pos, isl_int *val)
9974 if (isl_map_check_range(map, type, pos, 1) < 0)
9975 return isl_bool_error;
9976 return isl_map_plain_has_fixed_var(map,
9977 map_offset(map, type) - 1 + pos, val);
9980 /* If "map" obviously lies on a hyperplane where the given dimension
9981 * has a fixed value, then return that value.
9982 * Otherwise return NaN.
9984 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9985 enum isl_dim_type type, unsigned pos)
9987 isl_ctx *ctx;
9988 isl_val *v;
9989 isl_bool fixed;
9991 if (!map)
9992 return NULL;
9993 ctx = isl_map_get_ctx(map);
9994 v = isl_val_alloc(ctx);
9995 if (!v)
9996 return NULL;
9997 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9998 if (fixed < 0)
9999 return isl_val_free(v);
10000 if (fixed) {
10001 isl_int_set_si(v->d, 1);
10002 return v;
10004 isl_val_free(v);
10005 return isl_val_nan(ctx);
10008 /* If "set" obviously lies on a hyperplane where the given dimension
10009 * has a fixed value, then return that value.
10010 * Otherwise return NaN.
10012 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10013 enum isl_dim_type type, unsigned pos)
10015 return isl_map_plain_get_val_if_fixed(set, type, pos);
10018 /* Return a sequence of values in the same space as "set"
10019 * that are equal to the corresponding set dimensions of "set"
10020 * for those set dimensions that obviously lie on a hyperplane
10021 * where the dimension has a fixed value.
10022 * The other elements are set to NaN.
10024 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10025 __isl_keep isl_set *set)
10027 int i;
10028 isl_size n;
10029 isl_space *space;
10030 isl_multi_val *mv;
10032 space = isl_space_drop_all_params(isl_set_get_space(set));
10033 mv = isl_multi_val_alloc(space);
10034 n = isl_multi_val_size(mv);
10035 if (n < 0)
10036 return isl_multi_val_free(mv);
10038 for (i = 0; i < n; ++i) {
10039 isl_val *v;
10041 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10042 mv = isl_multi_val_set_val(mv, i, v);
10045 return mv;
10048 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10049 * then return this fixed value in *val.
10051 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10052 unsigned dim, isl_int *val)
10054 isl_size nparam;
10056 nparam = isl_basic_set_dim(bset, isl_dim_param);
10057 if (nparam < 0)
10058 return isl_bool_error;
10059 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10062 /* Return -1 if the constraint "c1" should be sorted before "c2"
10063 * and 1 if it should be sorted after "c2".
10064 * Return 0 if the two constraints are the same (up to the constant term).
10066 * In particular, if a constraint involves later variables than another
10067 * then it is sorted after this other constraint.
10068 * uset_gist depends on constraints without existentially quantified
10069 * variables sorting first.
10071 * For constraints that have the same latest variable, those
10072 * with the same coefficient for this latest variable (first in absolute value
10073 * and then in actual value) are grouped together.
10074 * This is useful for detecting pairs of constraints that can
10075 * be chained in their printed representation.
10077 * Finally, within a group, constraints are sorted according to
10078 * their coefficients (excluding the constant term).
10080 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10082 isl_int **c1 = (isl_int **) p1;
10083 isl_int **c2 = (isl_int **) p2;
10084 int l1, l2;
10085 unsigned size = *(unsigned *) arg;
10086 int cmp;
10088 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10089 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10091 if (l1 != l2)
10092 return l1 - l2;
10094 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10095 if (cmp != 0)
10096 return cmp;
10097 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10098 if (cmp != 0)
10099 return -cmp;
10101 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10104 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10105 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10106 * and 0 if the two constraints are the same (up to the constant term).
10108 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10109 isl_int *c1, isl_int *c2)
10111 isl_size total;
10112 unsigned size;
10114 total = isl_basic_map_dim(bmap, isl_dim_all);
10115 if (total < 0)
10116 return -2;
10117 size = total;
10118 return sort_constraint_cmp(&c1, &c2, &size);
10121 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10122 __isl_take isl_basic_map *bmap)
10124 isl_size total;
10125 unsigned size;
10127 if (!bmap)
10128 return NULL;
10129 if (bmap->n_ineq == 0)
10130 return bmap;
10131 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10132 return bmap;
10133 total = isl_basic_map_dim(bmap, isl_dim_all);
10134 if (total < 0)
10135 return isl_basic_map_free(bmap);
10136 size = total;
10137 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10138 &sort_constraint_cmp, &size) < 0)
10139 return isl_basic_map_free(bmap);
10140 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10141 return bmap;
10144 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10145 __isl_take isl_basic_set *bset)
10147 isl_basic_map *bmap = bset_to_bmap(bset);
10148 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10151 __isl_give isl_basic_map *isl_basic_map_normalize(
10152 __isl_take isl_basic_map *bmap)
10154 bmap = isl_basic_map_remove_redundancies(bmap);
10155 bmap = isl_basic_map_sort_constraints(bmap);
10156 return bmap;
10158 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10159 __isl_keep isl_basic_map *bmap2)
10161 int i, cmp;
10162 isl_size total;
10163 isl_space *space1, *space2;
10165 if (!bmap1 || !bmap2)
10166 return -1;
10168 if (bmap1 == bmap2)
10169 return 0;
10170 space1 = isl_basic_map_peek_space(bmap1);
10171 space2 = isl_basic_map_peek_space(bmap2);
10172 cmp = isl_space_cmp(space1, space2);
10173 if (cmp)
10174 return cmp;
10175 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10176 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10177 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10178 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10179 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10180 return 0;
10181 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10182 return 1;
10183 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10184 return -1;
10185 if (bmap1->n_eq != bmap2->n_eq)
10186 return bmap1->n_eq - bmap2->n_eq;
10187 if (bmap1->n_ineq != bmap2->n_ineq)
10188 return bmap1->n_ineq - bmap2->n_ineq;
10189 if (bmap1->n_div != bmap2->n_div)
10190 return bmap1->n_div - bmap2->n_div;
10191 total = isl_basic_map_dim(bmap1, isl_dim_all);
10192 if (total < 0)
10193 return -1;
10194 for (i = 0; i < bmap1->n_eq; ++i) {
10195 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10196 if (cmp)
10197 return cmp;
10199 for (i = 0; i < bmap1->n_ineq; ++i) {
10200 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10201 if (cmp)
10202 return cmp;
10204 for (i = 0; i < bmap1->n_div; ++i) {
10205 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10206 if (cmp)
10207 return cmp;
10209 return 0;
10212 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10213 __isl_keep isl_basic_set *bset2)
10215 return isl_basic_map_plain_cmp(bset1, bset2);
10218 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10220 int i, cmp;
10222 if (set1 == set2)
10223 return 0;
10224 if (set1->n != set2->n)
10225 return set1->n - set2->n;
10227 for (i = 0; i < set1->n; ++i) {
10228 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10229 if (cmp)
10230 return cmp;
10233 return 0;
10236 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10237 __isl_keep isl_basic_map *bmap2)
10239 if (!bmap1 || !bmap2)
10240 return isl_bool_error;
10241 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10244 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10245 __isl_keep isl_basic_set *bset2)
10247 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10248 bset_to_bmap(bset2));
10251 static int qsort_bmap_cmp(const void *p1, const void *p2)
10253 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10254 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10256 return isl_basic_map_plain_cmp(bmap1, bmap2);
10259 /* Sort the basic maps of "map" and remove duplicate basic maps.
10261 * While removing basic maps, we make sure that the basic maps remain
10262 * sorted because isl_map_normalize expects the basic maps of the result
10263 * to be sorted.
10265 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10267 int i, j;
10269 map = isl_map_remove_empty_parts(map);
10270 if (!map)
10271 return NULL;
10272 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10273 for (i = map->n - 1; i >= 1; --i) {
10274 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10275 continue;
10276 isl_basic_map_free(map->p[i-1]);
10277 for (j = i; j < map->n; ++j)
10278 map->p[j - 1] = map->p[j];
10279 map->n--;
10282 return map;
10285 /* Remove obvious duplicates among the basic maps of "map".
10287 * Unlike isl_map_normalize, this function does not remove redundant
10288 * constraints and only removes duplicates that have exactly the same
10289 * constraints in the input. It does sort the constraints and
10290 * the basic maps to ease the detection of duplicates.
10292 * If "map" has already been normalized or if the basic maps are
10293 * disjoint, then there can be no duplicates.
10295 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10297 int i;
10298 isl_basic_map *bmap;
10300 if (!map)
10301 return NULL;
10302 if (map->n <= 1)
10303 return map;
10304 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10305 return map;
10306 for (i = 0; i < map->n; ++i) {
10307 bmap = isl_basic_map_copy(map->p[i]);
10308 bmap = isl_basic_map_sort_constraints(bmap);
10309 if (!bmap)
10310 return isl_map_free(map);
10311 isl_basic_map_free(map->p[i]);
10312 map->p[i] = bmap;
10315 map = sort_and_remove_duplicates(map);
10316 return map;
10319 /* We normalize in place, but if anything goes wrong we need
10320 * to return NULL, so we need to make sure we don't change the
10321 * meaning of any possible other copies of map.
10323 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10325 int i;
10326 struct isl_basic_map *bmap;
10328 if (!map)
10329 return NULL;
10330 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10331 return map;
10332 for (i = 0; i < map->n; ++i) {
10333 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10334 if (!bmap)
10335 goto error;
10336 isl_basic_map_free(map->p[i]);
10337 map->p[i] = bmap;
10340 map = sort_and_remove_duplicates(map);
10341 if (map)
10342 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10343 return map;
10344 error:
10345 isl_map_free(map);
10346 return NULL;
10349 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10351 return set_from_map(isl_map_normalize(set_to_map(set)));
10354 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10355 __isl_keep isl_map *map2)
10357 int i;
10358 isl_bool equal;
10360 if (!map1 || !map2)
10361 return isl_bool_error;
10363 if (map1 == map2)
10364 return isl_bool_true;
10365 equal = isl_map_has_equal_space(map1, map2);
10366 if (equal < 0 || !equal)
10367 return equal;
10369 map1 = isl_map_copy(map1);
10370 map2 = isl_map_copy(map2);
10371 map1 = isl_map_normalize(map1);
10372 map2 = isl_map_normalize(map2);
10373 if (!map1 || !map2)
10374 goto error;
10375 equal = map1->n == map2->n;
10376 for (i = 0; equal && i < map1->n; ++i) {
10377 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10378 if (equal < 0)
10379 goto error;
10381 isl_map_free(map1);
10382 isl_map_free(map2);
10383 return equal;
10384 error:
10385 isl_map_free(map1);
10386 isl_map_free(map2);
10387 return isl_bool_error;
10390 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10391 __isl_keep isl_set *set2)
10393 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10396 /* Return the basic maps in "map" as a list.
10398 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10399 __isl_keep isl_map *map)
10401 int i;
10402 isl_ctx *ctx;
10403 isl_basic_map_list *list;
10405 if (!map)
10406 return NULL;
10407 ctx = isl_map_get_ctx(map);
10408 list = isl_basic_map_list_alloc(ctx, map->n);
10410 for (i = 0; i < map->n; ++i) {
10411 isl_basic_map *bmap;
10413 bmap = isl_basic_map_copy(map->p[i]);
10414 list = isl_basic_map_list_add(list, bmap);
10417 return list;
10420 /* Return the intersection of the elements in the non-empty list "list".
10421 * All elements are assumed to live in the same space.
10423 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10424 __isl_take isl_basic_map_list *list)
10426 int i;
10427 isl_size n;
10428 isl_basic_map *bmap;
10430 n = isl_basic_map_list_n_basic_map(list);
10431 if (n < 0)
10432 goto error;
10433 if (n < 1)
10434 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10435 "expecting non-empty list", goto error);
10437 bmap = isl_basic_map_list_get_basic_map(list, 0);
10438 for (i = 1; i < n; ++i) {
10439 isl_basic_map *bmap_i;
10441 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10442 bmap = isl_basic_map_intersect(bmap, bmap_i);
10445 isl_basic_map_list_free(list);
10446 return bmap;
10447 error:
10448 isl_basic_map_list_free(list);
10449 return NULL;
10452 /* Return the intersection of the elements in the non-empty list "list".
10453 * All elements are assumed to live in the same space.
10455 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10456 __isl_take isl_basic_set_list *list)
10458 return isl_basic_map_list_intersect(list);
10461 /* Return the union of the elements of "list".
10462 * The list is required to have at least one element.
10464 __isl_give isl_set *isl_basic_set_list_union(
10465 __isl_take isl_basic_set_list *list)
10467 int i;
10468 isl_size n;
10469 isl_space *space;
10470 isl_basic_set *bset;
10471 isl_set *set;
10473 n = isl_basic_set_list_n_basic_set(list);
10474 if (n < 0)
10475 goto error;
10476 if (n < 1)
10477 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10478 "expecting non-empty list", goto error);
10480 bset = isl_basic_set_list_get_basic_set(list, 0);
10481 space = isl_basic_set_get_space(bset);
10482 isl_basic_set_free(bset);
10484 set = isl_set_alloc_space(space, n, 0);
10485 for (i = 0; i < n; ++i) {
10486 bset = isl_basic_set_list_get_basic_set(list, i);
10487 set = isl_set_add_basic_set(set, bset);
10490 isl_basic_set_list_free(list);
10491 return set;
10492 error:
10493 isl_basic_set_list_free(list);
10494 return NULL;
10497 /* Return the union of the elements in the non-empty list "list".
10498 * All elements are assumed to live in the same space.
10500 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10502 int i;
10503 isl_size n;
10504 isl_set *set;
10506 n = isl_set_list_n_set(list);
10507 if (n < 0)
10508 goto error;
10509 if (n < 1)
10510 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10511 "expecting non-empty list", goto error);
10513 set = isl_set_list_get_set(list, 0);
10514 for (i = 1; i < n; ++i) {
10515 isl_set *set_i;
10517 set_i = isl_set_list_get_set(list, i);
10518 set = isl_set_union(set, set_i);
10521 isl_set_list_free(list);
10522 return set;
10523 error:
10524 isl_set_list_free(list);
10525 return NULL;
10528 __isl_give isl_basic_map *isl_basic_map_product(
10529 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10531 isl_space *space_result = NULL;
10532 struct isl_basic_map *bmap;
10533 unsigned in1, in2, out1, out2, nparam, total, pos;
10534 struct isl_dim_map *dim_map1, *dim_map2;
10536 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10537 goto error;
10538 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10539 isl_space_copy(bmap2->dim));
10541 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10542 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10543 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10544 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10545 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10547 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10548 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10549 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10550 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10551 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10552 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10553 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10554 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10555 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10556 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10557 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10559 bmap = isl_basic_map_alloc_space(space_result,
10560 bmap1->n_div + bmap2->n_div,
10561 bmap1->n_eq + bmap2->n_eq,
10562 bmap1->n_ineq + bmap2->n_ineq);
10563 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10564 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10565 bmap = isl_basic_map_simplify(bmap);
10566 return isl_basic_map_finalize(bmap);
10567 error:
10568 isl_basic_map_free(bmap1);
10569 isl_basic_map_free(bmap2);
10570 return NULL;
10573 __isl_give isl_basic_map *isl_basic_map_flat_product(
10574 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10576 isl_basic_map *prod;
10578 prod = isl_basic_map_product(bmap1, bmap2);
10579 prod = isl_basic_map_flatten(prod);
10580 return prod;
10583 __isl_give isl_basic_set *isl_basic_set_flat_product(
10584 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10586 return isl_basic_map_flat_range_product(bset1, bset2);
10589 __isl_give isl_basic_map *isl_basic_map_domain_product(
10590 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10592 isl_space *space1, *space2;
10593 isl_space *space_result = NULL;
10594 isl_basic_map *bmap;
10595 isl_size in1, in2, out, nparam;
10596 unsigned total, pos;
10597 struct isl_dim_map *dim_map1, *dim_map2;
10599 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10600 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10601 out = isl_basic_map_dim(bmap1, isl_dim_out);
10602 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10603 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10604 goto error;
10606 space1 = isl_basic_map_get_space(bmap1);
10607 space2 = isl_basic_map_get_space(bmap2);
10608 space_result = isl_space_domain_product(space1, space2);
10610 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10611 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10612 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10617 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10618 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10619 isl_dim_map_div(dim_map1, bmap1, pos += out);
10620 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10622 bmap = isl_basic_map_alloc_space(space_result,
10623 bmap1->n_div + bmap2->n_div,
10624 bmap1->n_eq + bmap2->n_eq,
10625 bmap1->n_ineq + bmap2->n_ineq);
10626 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10627 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10628 bmap = isl_basic_map_simplify(bmap);
10629 return isl_basic_map_finalize(bmap);
10630 error:
10631 isl_basic_map_free(bmap1);
10632 isl_basic_map_free(bmap2);
10633 return NULL;
10636 __isl_give isl_basic_map *isl_basic_map_range_product(
10637 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10639 isl_bool rational;
10640 isl_space *space_result = NULL;
10641 isl_basic_map *bmap;
10642 isl_size in, out1, out2, nparam;
10643 unsigned total, pos;
10644 struct isl_dim_map *dim_map1, *dim_map2;
10646 rational = isl_basic_map_is_rational(bmap1);
10647 if (rational >= 0 && rational)
10648 rational = isl_basic_map_is_rational(bmap2);
10649 in = isl_basic_map_dim(bmap1, isl_dim_in);
10650 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10651 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10652 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10653 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10654 goto error;
10656 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10657 goto error;
10659 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10660 isl_space_copy(bmap2->dim));
10662 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10663 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10664 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10665 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10666 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10667 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10668 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10669 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10670 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10671 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10672 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10674 bmap = isl_basic_map_alloc_space(space_result,
10675 bmap1->n_div + bmap2->n_div,
10676 bmap1->n_eq + bmap2->n_eq,
10677 bmap1->n_ineq + bmap2->n_ineq);
10678 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10679 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10680 if (rational)
10681 bmap = isl_basic_map_set_rational(bmap);
10682 bmap = isl_basic_map_simplify(bmap);
10683 return isl_basic_map_finalize(bmap);
10684 error:
10685 isl_basic_map_free(bmap1);
10686 isl_basic_map_free(bmap2);
10687 return NULL;
10690 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10691 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10693 isl_basic_map *prod;
10695 prod = isl_basic_map_range_product(bmap1, bmap2);
10696 prod = isl_basic_map_flatten_range(prod);
10697 return prod;
10700 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10701 * and collect the results.
10702 * The result live in the space obtained by calling "space_product"
10703 * on the spaces of "map1" and "map2".
10704 * If "remove_duplicates" is set then the result may contain duplicates
10705 * (even if the inputs do not) and so we try and remove the obvious
10706 * duplicates.
10708 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10709 __isl_take isl_map *map2,
10710 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10711 __isl_take isl_space *right),
10712 __isl_give isl_basic_map *(*basic_map_product)(
10713 __isl_take isl_basic_map *left,
10714 __isl_take isl_basic_map *right),
10715 int remove_duplicates)
10717 unsigned flags = 0;
10718 struct isl_map *result;
10719 int i, j;
10720 isl_bool m;
10722 m = isl_map_has_equal_params(map1, map2);
10723 if (m < 0)
10724 goto error;
10725 if (!m)
10726 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10727 "parameters don't match", goto error);
10729 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10730 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10731 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10733 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10734 isl_space_copy(map2->dim)),
10735 map1->n * map2->n, flags);
10736 if (!result)
10737 goto error;
10738 for (i = 0; i < map1->n; ++i)
10739 for (j = 0; j < map2->n; ++j) {
10740 struct isl_basic_map *part;
10741 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10742 isl_basic_map_copy(map2->p[j]));
10743 if (isl_basic_map_is_empty(part))
10744 isl_basic_map_free(part);
10745 else
10746 result = isl_map_add_basic_map(result, part);
10747 if (!result)
10748 goto error;
10750 if (remove_duplicates)
10751 result = isl_map_remove_obvious_duplicates(result);
10752 isl_map_free(map1);
10753 isl_map_free(map2);
10754 return result;
10755 error:
10756 isl_map_free(map1);
10757 isl_map_free(map2);
10758 return NULL;
10761 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10763 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10764 __isl_take isl_map *map2)
10766 isl_map_align_params_bin(&map1, &map2);
10767 return map_product(map1, map2, &isl_space_product,
10768 &isl_basic_map_product, 0);
10771 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10773 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10774 __isl_take isl_map *map2)
10776 isl_map *prod;
10778 prod = isl_map_product(map1, map2);
10779 prod = isl_map_flatten(prod);
10780 return prod;
10783 /* Given two set A and B, construct its Cartesian product A x B.
10785 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10786 __isl_take isl_set *set2)
10788 return isl_map_range_product(set1, set2);
10791 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10792 __isl_take isl_set *set2)
10794 return isl_map_flat_range_product(set1, set2);
10797 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10799 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10800 __isl_take isl_map *map2)
10802 isl_map_align_params_bin(&map1, &map2);
10803 return map_product(map1, map2, &isl_space_domain_product,
10804 &isl_basic_map_domain_product, 1);
10807 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10809 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10810 __isl_take isl_map *map2)
10812 isl_map_align_params_bin(&map1, &map2);
10813 return map_product(map1, map2, &isl_space_range_product,
10814 &isl_basic_map_range_product, 1);
10817 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10819 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10821 isl_space *space;
10822 isl_size total1, keep1, total2, keep2;
10824 total1 = isl_map_dim(map, isl_dim_in);
10825 total2 = isl_map_dim(map, isl_dim_out);
10826 if (total1 < 0 || total2 < 0)
10827 return isl_map_free(map);
10828 if (!isl_space_domain_is_wrapping(map->dim) ||
10829 !isl_space_range_is_wrapping(map->dim))
10830 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10831 "not a product", return isl_map_free(map));
10833 space = isl_map_get_space(map);
10834 space = isl_space_factor_domain(space);
10835 keep1 = isl_space_dim(space, isl_dim_in);
10836 keep2 = isl_space_dim(space, isl_dim_out);
10837 if (keep1 < 0 || keep2 < 0)
10838 map = isl_map_free(map);
10839 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10840 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10841 map = isl_map_reset_space(map, space);
10843 return map;
10846 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10848 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10850 isl_space *space;
10851 isl_size total1, keep1, total2, keep2;
10853 total1 = isl_map_dim(map, isl_dim_in);
10854 total2 = isl_map_dim(map, isl_dim_out);
10855 if (total1 < 0 || total2 < 0)
10856 return isl_map_free(map);
10857 if (!isl_space_domain_is_wrapping(map->dim) ||
10858 !isl_space_range_is_wrapping(map->dim))
10859 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10860 "not a product", return isl_map_free(map));
10862 space = isl_map_get_space(map);
10863 space = isl_space_factor_range(space);
10864 keep1 = isl_space_dim(space, isl_dim_in);
10865 keep2 = isl_space_dim(space, isl_dim_out);
10866 if (keep1 < 0 || keep2 < 0)
10867 map = isl_map_free(map);
10868 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10869 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10870 map = isl_map_reset_space(map, space);
10872 return map;
10875 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10877 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10879 isl_space *space;
10880 isl_size total, keep;
10882 total = isl_map_dim(map, isl_dim_in);
10883 if (total < 0)
10884 return isl_map_free(map);
10885 if (!isl_space_domain_is_wrapping(map->dim))
10886 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10887 "domain is not a product", return isl_map_free(map));
10889 space = isl_map_get_space(map);
10890 space = isl_space_domain_factor_domain(space);
10891 keep = isl_space_dim(space, isl_dim_in);
10892 if (keep < 0)
10893 map = isl_map_free(map);
10894 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10895 map = isl_map_reset_space(map, space);
10897 return map;
10900 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10902 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10904 isl_space *space;
10905 isl_size total, keep;
10907 total = isl_map_dim(map, isl_dim_in);
10908 if (total < 0)
10909 return isl_map_free(map);
10910 if (!isl_space_domain_is_wrapping(map->dim))
10911 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10912 "domain is not a product", return isl_map_free(map));
10914 space = isl_map_get_space(map);
10915 space = isl_space_domain_factor_range(space);
10916 keep = isl_space_dim(space, isl_dim_in);
10917 if (keep < 0)
10918 map = isl_map_free(map);
10919 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10920 map = isl_map_reset_space(map, space);
10922 return map;
10925 /* Given a map A -> [B -> C], extract the map A -> B.
10927 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10929 isl_space *space;
10930 isl_size total, keep;
10932 total = isl_map_dim(map, isl_dim_out);
10933 if (total < 0)
10934 return isl_map_free(map);
10935 if (!isl_space_range_is_wrapping(map->dim))
10936 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10937 "range is not a product", return isl_map_free(map));
10939 space = isl_map_get_space(map);
10940 space = isl_space_range_factor_domain(space);
10941 keep = isl_space_dim(space, isl_dim_out);
10942 if (keep < 0)
10943 map = isl_map_free(map);
10944 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10945 map = isl_map_reset_space(map, space);
10947 return map;
10950 /* Given a map A -> [B -> C], extract the map A -> C.
10952 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10954 isl_space *space;
10955 isl_size total, keep;
10957 total = isl_map_dim(map, isl_dim_out);
10958 if (total < 0)
10959 return isl_map_free(map);
10960 if (!isl_space_range_is_wrapping(map->dim))
10961 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10962 "range is not a product", return isl_map_free(map));
10964 space = isl_map_get_space(map);
10965 space = isl_space_range_factor_range(space);
10966 keep = isl_space_dim(space, isl_dim_out);
10967 if (keep < 0)
10968 map = isl_map_free(map);
10969 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10970 map = isl_map_reset_space(map, space);
10972 return map;
10975 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10977 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10978 __isl_take isl_map *map2)
10980 isl_map *prod;
10982 prod = isl_map_domain_product(map1, map2);
10983 prod = isl_map_flatten_domain(prod);
10984 return prod;
10987 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10989 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10990 __isl_take isl_map *map2)
10992 isl_map *prod;
10994 prod = isl_map_range_product(map1, map2);
10995 prod = isl_map_flatten_range(prod);
10996 return prod;
10999 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11001 int i;
11002 uint32_t hash = isl_hash_init();
11003 isl_size total;
11005 if (!bmap)
11006 return 0;
11007 bmap = isl_basic_map_copy(bmap);
11008 bmap = isl_basic_map_normalize(bmap);
11009 total = isl_basic_map_dim(bmap, isl_dim_all);
11010 if (total < 0)
11011 return 0;
11012 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11013 for (i = 0; i < bmap->n_eq; ++i) {
11014 uint32_t c_hash;
11015 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11016 isl_hash_hash(hash, c_hash);
11018 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11019 for (i = 0; i < bmap->n_ineq; ++i) {
11020 uint32_t c_hash;
11021 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11022 isl_hash_hash(hash, c_hash);
11024 isl_hash_byte(hash, bmap->n_div & 0xFF);
11025 for (i = 0; i < bmap->n_div; ++i) {
11026 uint32_t c_hash;
11027 if (isl_int_is_zero(bmap->div[i][0]))
11028 continue;
11029 isl_hash_byte(hash, i & 0xFF);
11030 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11031 isl_hash_hash(hash, c_hash);
11033 isl_basic_map_free(bmap);
11034 return hash;
11037 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11039 return isl_basic_map_get_hash(bset_to_bmap(bset));
11042 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11044 int i;
11045 uint32_t hash;
11047 if (!map)
11048 return 0;
11049 map = isl_map_copy(map);
11050 map = isl_map_normalize(map);
11051 if (!map)
11052 return 0;
11054 hash = isl_hash_init();
11055 for (i = 0; i < map->n; ++i) {
11056 uint32_t bmap_hash;
11057 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11058 isl_hash_hash(hash, bmap_hash);
11061 isl_map_free(map);
11063 return hash;
11066 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11068 return isl_map_get_hash(set_to_map(set));
11071 /* Return the number of basic maps in the (current) representation of "map".
11073 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11075 return map ? map->n : isl_size_error;
11078 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11080 return set ? set->n : isl_size_error;
11083 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11084 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11086 int i;
11088 if (!map)
11089 return isl_stat_error;
11091 for (i = 0; i < map->n; ++i)
11092 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11093 return isl_stat_error;
11095 return isl_stat_ok;
11098 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11099 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11101 int i;
11103 if (!set)
11104 return isl_stat_error;
11106 for (i = 0; i < set->n; ++i)
11107 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11108 return isl_stat_error;
11110 return isl_stat_ok;
11113 /* Return a list of basic sets, the union of which is equal to "set".
11115 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11116 __isl_keep isl_set *set)
11118 int i;
11119 isl_basic_set_list *list;
11121 if (!set)
11122 return NULL;
11124 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11125 for (i = 0; i < set->n; ++i) {
11126 isl_basic_set *bset;
11128 bset = isl_basic_set_copy(set->p[i]);
11129 list = isl_basic_set_list_add(list, bset);
11132 return list;
11135 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11137 isl_space *space;
11139 if (!bset)
11140 return NULL;
11142 bset = isl_basic_set_cow(bset);
11143 if (!bset)
11144 return NULL;
11146 space = isl_basic_set_get_space(bset);
11147 space = isl_space_lift(space, bset->n_div);
11148 if (!space)
11149 goto error;
11150 isl_space_free(bset->dim);
11151 bset->dim = space;
11152 bset->extra -= bset->n_div;
11153 bset->n_div = 0;
11155 bset = isl_basic_set_finalize(bset);
11157 return bset;
11158 error:
11159 isl_basic_set_free(bset);
11160 return NULL;
11163 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11165 int i;
11166 isl_space *space;
11167 unsigned n_div;
11169 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11171 if (!set)
11172 return NULL;
11174 set = isl_set_cow(set);
11175 if (!set)
11176 return NULL;
11178 n_div = set->p[0]->n_div;
11179 space = isl_set_get_space(set);
11180 space = isl_space_lift(space, n_div);
11181 if (!space)
11182 goto error;
11183 isl_space_free(set->dim);
11184 set->dim = space;
11186 for (i = 0; i < set->n; ++i) {
11187 set->p[i] = isl_basic_set_lift(set->p[i]);
11188 if (!set->p[i])
11189 goto error;
11192 return set;
11193 error:
11194 isl_set_free(set);
11195 return NULL;
11198 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11200 isl_size dim;
11201 int size = 0;
11203 dim = isl_basic_set_dim(bset, isl_dim_all);
11204 if (dim < 0)
11205 return -1;
11206 size += bset->n_eq * (1 + dim);
11207 size += bset->n_ineq * (1 + dim);
11208 size += bset->n_div * (2 + dim);
11210 return size;
11213 int isl_set_size(__isl_keep isl_set *set)
11215 int i;
11216 int size = 0;
11218 if (!set)
11219 return -1;
11221 for (i = 0; i < set->n; ++i)
11222 size += isl_basic_set_size(set->p[i]);
11224 return size;
11227 /* Check if there is any lower bound (if lower == 0) and/or upper
11228 * bound (if upper == 0) on the specified dim.
11230 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11231 enum isl_dim_type type, unsigned pos, int lower, int upper)
11233 int i;
11235 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11236 return isl_bool_error;
11238 pos += isl_basic_map_offset(bmap, type);
11240 for (i = 0; i < bmap->n_div; ++i) {
11241 if (isl_int_is_zero(bmap->div[i][0]))
11242 continue;
11243 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11244 return isl_bool_true;
11247 for (i = 0; i < bmap->n_eq; ++i)
11248 if (!isl_int_is_zero(bmap->eq[i][pos]))
11249 return isl_bool_true;
11251 for (i = 0; i < bmap->n_ineq; ++i) {
11252 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11253 if (sgn > 0)
11254 lower = 1;
11255 if (sgn < 0)
11256 upper = 1;
11259 return lower && upper;
11262 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11263 enum isl_dim_type type, unsigned pos)
11265 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11268 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11269 enum isl_dim_type type, unsigned pos)
11271 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11274 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11275 enum isl_dim_type type, unsigned pos)
11277 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11280 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11281 enum isl_dim_type type, unsigned pos)
11283 int i;
11285 if (!map)
11286 return isl_bool_error;
11288 for (i = 0; i < map->n; ++i) {
11289 isl_bool bounded;
11290 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11291 if (bounded < 0 || !bounded)
11292 return bounded;
11295 return isl_bool_true;
11298 /* Return true if the specified dim is involved in both an upper bound
11299 * and a lower bound.
11301 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11302 enum isl_dim_type type, unsigned pos)
11304 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11307 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11309 static isl_bool has_any_bound(__isl_keep isl_map *map,
11310 enum isl_dim_type type, unsigned pos,
11311 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11312 enum isl_dim_type type, unsigned pos))
11314 int i;
11316 if (!map)
11317 return isl_bool_error;
11319 for (i = 0; i < map->n; ++i) {
11320 isl_bool bounded;
11321 bounded = fn(map->p[i], type, pos);
11322 if (bounded < 0 || bounded)
11323 return bounded;
11326 return isl_bool_false;
11329 /* Return 1 if the specified dim is involved in any lower bound.
11331 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11332 enum isl_dim_type type, unsigned pos)
11334 return has_any_bound(set, type, pos,
11335 &isl_basic_map_dim_has_lower_bound);
11338 /* Return 1 if the specified dim is involved in any upper bound.
11340 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11341 enum isl_dim_type type, unsigned pos)
11343 return has_any_bound(set, type, pos,
11344 &isl_basic_map_dim_has_upper_bound);
11347 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11349 static isl_bool has_bound(__isl_keep isl_map *map,
11350 enum isl_dim_type type, unsigned pos,
11351 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11352 enum isl_dim_type type, unsigned pos))
11354 int i;
11356 if (!map)
11357 return isl_bool_error;
11359 for (i = 0; i < map->n; ++i) {
11360 isl_bool bounded;
11361 bounded = fn(map->p[i], type, pos);
11362 if (bounded < 0 || !bounded)
11363 return bounded;
11366 return isl_bool_true;
11369 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11371 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11372 enum isl_dim_type type, unsigned pos)
11374 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11377 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11379 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11380 enum isl_dim_type type, unsigned pos)
11382 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11385 /* For each of the "n" variables starting at "first", determine
11386 * the sign of the variable and put the results in the first "n"
11387 * elements of the array "signs".
11388 * Sign
11389 * 1 means that the variable is non-negative
11390 * -1 means that the variable is non-positive
11391 * 0 means the variable attains both positive and negative values.
11393 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11394 unsigned first, unsigned n, int *signs)
11396 isl_vec *bound = NULL;
11397 struct isl_tab *tab = NULL;
11398 struct isl_tab_undo *snap;
11399 int i;
11400 isl_size total;
11402 total = isl_basic_set_dim(bset, isl_dim_all);
11403 if (total < 0 || !signs)
11404 return isl_stat_error;
11406 bound = isl_vec_alloc(bset->ctx, 1 + total);
11407 tab = isl_tab_from_basic_set(bset, 0);
11408 if (!bound || !tab)
11409 goto error;
11411 isl_seq_clr(bound->el, bound->size);
11412 isl_int_set_si(bound->el[0], -1);
11414 snap = isl_tab_snap(tab);
11415 for (i = 0; i < n; ++i) {
11416 int empty;
11418 isl_int_set_si(bound->el[1 + first + i], -1);
11419 if (isl_tab_add_ineq(tab, bound->el) < 0)
11420 goto error;
11421 empty = tab->empty;
11422 isl_int_set_si(bound->el[1 + first + i], 0);
11423 if (isl_tab_rollback(tab, snap) < 0)
11424 goto error;
11426 if (empty) {
11427 signs[i] = 1;
11428 continue;
11431 isl_int_set_si(bound->el[1 + first + i], 1);
11432 if (isl_tab_add_ineq(tab, bound->el) < 0)
11433 goto error;
11434 empty = tab->empty;
11435 isl_int_set_si(bound->el[1 + first + i], 0);
11436 if (isl_tab_rollback(tab, snap) < 0)
11437 goto error;
11439 signs[i] = empty ? -1 : 0;
11442 isl_tab_free(tab);
11443 isl_vec_free(bound);
11444 return isl_stat_ok;
11445 error:
11446 isl_tab_free(tab);
11447 isl_vec_free(bound);
11448 return isl_stat_error;
11451 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11452 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11454 if (!bset || !signs)
11455 return isl_stat_error;
11456 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11457 return isl_stat_error;
11459 first += pos(bset->dim, type) - 1;
11460 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11463 /* Is it possible for the integer division "div" to depend (possibly
11464 * indirectly) on any output dimensions?
11466 * If the div is undefined, then we conservatively assume that it
11467 * may depend on them.
11468 * Otherwise, we check if it actually depends on them or on any integer
11469 * divisions that may depend on them.
11471 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11473 int i;
11474 isl_size n_out, n_div;
11475 unsigned o_out, o_div;
11477 if (isl_int_is_zero(bmap->div[div][0]))
11478 return isl_bool_true;
11480 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11481 if (n_out < 0)
11482 return isl_bool_error;
11483 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11485 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11486 return isl_bool_true;
11488 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11489 if (n_div < 0)
11490 return isl_bool_error;
11491 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11493 for (i = 0; i < n_div; ++i) {
11494 isl_bool may_involve;
11496 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11497 continue;
11498 may_involve = div_may_involve_output(bmap, i);
11499 if (may_involve < 0 || may_involve)
11500 return may_involve;
11503 return isl_bool_false;
11506 /* Return the first integer division of "bmap" in the range
11507 * [first, first + n[ that may depend on any output dimensions and
11508 * that has a non-zero coefficient in "c" (where the first coefficient
11509 * in "c" corresponds to integer division "first").
11511 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11512 isl_int *c, int first, int n)
11514 int k;
11516 if (!bmap)
11517 return -1;
11519 for (k = first; k < first + n; ++k) {
11520 isl_bool may_involve;
11522 if (isl_int_is_zero(c[k]))
11523 continue;
11524 may_involve = div_may_involve_output(bmap, k);
11525 if (may_involve < 0)
11526 return -1;
11527 if (may_involve)
11528 return k;
11531 return first + n;
11534 /* Look for a pair of inequality constraints in "bmap" of the form
11536 * -l + i >= 0 or i >= l
11537 * and
11538 * n + l - i >= 0 or i <= l + n
11540 * with n < "m" and i the output dimension at position "pos".
11541 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11542 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11543 * and earlier output dimensions, as well as integer divisions that do
11544 * not involve any of the output dimensions.
11546 * Return the index of the first inequality constraint or bmap->n_ineq
11547 * if no such pair can be found.
11549 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11550 int pos, isl_int m)
11552 int i, j;
11553 isl_ctx *ctx;
11554 isl_size total;
11555 isl_size n_div, n_out;
11556 unsigned o_div, o_out;
11557 int less;
11559 total = isl_basic_map_dim(bmap, isl_dim_all);
11560 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11561 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11562 if (total < 0 || n_out < 0 || n_div < 0)
11563 return -1;
11565 ctx = isl_basic_map_get_ctx(bmap);
11566 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11567 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11568 for (i = 0; i < bmap->n_ineq; ++i) {
11569 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11570 continue;
11571 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11572 n_out - (pos + 1)) != -1)
11573 continue;
11574 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11575 0, n_div) < n_div)
11576 continue;
11577 for (j = i + 1; j < bmap->n_ineq; ++j) {
11578 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11579 ctx->one))
11580 continue;
11581 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11582 bmap->ineq[j] + 1, total))
11583 continue;
11584 break;
11586 if (j >= bmap->n_ineq)
11587 continue;
11588 isl_int_add(bmap->ineq[i][0],
11589 bmap->ineq[i][0], bmap->ineq[j][0]);
11590 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11591 isl_int_sub(bmap->ineq[i][0],
11592 bmap->ineq[i][0], bmap->ineq[j][0]);
11593 if (!less)
11594 continue;
11595 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11596 return i;
11597 else
11598 return j;
11601 return bmap->n_ineq;
11604 /* Return the index of the equality of "bmap" that defines
11605 * the output dimension "pos" in terms of earlier dimensions.
11606 * The equality may also involve integer divisions, as long
11607 * as those integer divisions are defined in terms of
11608 * parameters or input dimensions.
11609 * In this case, *div is set to the number of integer divisions and
11610 * *ineq is set to the number of inequality constraints (provided
11611 * div and ineq are not NULL).
11613 * The equality may also involve a single integer division involving
11614 * the output dimensions (typically only output dimension "pos") as
11615 * long as the coefficient of output dimension "pos" is 1 or -1 and
11616 * there is a pair of constraints i >= l and i <= l + n, with i referring
11617 * to output dimension "pos", l an expression involving only earlier
11618 * dimensions and n smaller than the coefficient of the integer division
11619 * in the equality. In this case, the output dimension can be defined
11620 * in terms of a modulo expression that does not involve the integer division.
11621 * *div is then set to this single integer division and
11622 * *ineq is set to the index of constraint i >= l.
11624 * Return bmap->n_eq if there is no such equality.
11625 * Return -1 on error.
11627 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11628 int pos, int *div, int *ineq)
11630 int j, k, l;
11631 isl_size n_div, n_out;
11632 unsigned o_div, o_out;
11634 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11635 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11636 if (n_out < 0 || n_div < 0)
11637 return -1;
11639 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11640 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11642 if (ineq)
11643 *ineq = bmap->n_ineq;
11644 if (div)
11645 *div = n_div;
11646 for (j = 0; j < bmap->n_eq; ++j) {
11647 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11648 continue;
11649 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11650 n_out - (pos + 1)) != -1)
11651 continue;
11652 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11653 0, n_div);
11654 if (k >= n_div)
11655 return j;
11656 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11657 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11658 continue;
11659 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11660 k + 1, n_div - (k+1)) < n_div)
11661 continue;
11662 l = find_modulo_constraint_pair(bmap, pos,
11663 bmap->eq[j][o_div + k]);
11664 if (l < 0)
11665 return -1;
11666 if (l >= bmap->n_ineq)
11667 continue;
11668 if (div)
11669 *div = k;
11670 if (ineq)
11671 *ineq = l;
11672 return j;
11675 return bmap->n_eq;
11678 /* Check if the given basic map is obviously single-valued.
11679 * In particular, for each output dimension, check that there is
11680 * an equality that defines the output dimension in terms of
11681 * earlier dimensions.
11683 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11685 int i;
11686 isl_size n_out;
11688 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11689 if (n_out < 0)
11690 return isl_bool_error;
11692 for (i = 0; i < n_out; ++i) {
11693 int eq;
11695 eq = isl_basic_map_output_defining_equality(bmap, i,
11696 NULL, NULL);
11697 if (eq < 0)
11698 return isl_bool_error;
11699 if (eq >= bmap->n_eq)
11700 return isl_bool_false;
11703 return isl_bool_true;
11706 /* Check if the given basic map is single-valued.
11707 * We simply compute
11709 * M \circ M^-1
11711 * and check if the result is a subset of the identity mapping.
11713 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11715 isl_space *space;
11716 isl_basic_map *test;
11717 isl_basic_map *id;
11718 isl_bool sv;
11720 sv = isl_basic_map_plain_is_single_valued(bmap);
11721 if (sv < 0 || sv)
11722 return sv;
11724 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11725 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11727 space = isl_basic_map_get_space(bmap);
11728 space = isl_space_map_from_set(isl_space_range(space));
11729 id = isl_basic_map_identity(space);
11731 sv = isl_basic_map_is_subset(test, id);
11733 isl_basic_map_free(test);
11734 isl_basic_map_free(id);
11736 return sv;
11739 /* Check if the given map is obviously single-valued.
11741 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11743 if (!map)
11744 return isl_bool_error;
11745 if (map->n == 0)
11746 return isl_bool_true;
11747 if (map->n >= 2)
11748 return isl_bool_false;
11750 return isl_basic_map_plain_is_single_valued(map->p[0]);
11753 /* Check if the given map is single-valued.
11754 * We simply compute
11756 * M \circ M^-1
11758 * and check if the result is a subset of the identity mapping.
11760 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11762 isl_space *space;
11763 isl_map *test;
11764 isl_map *id;
11765 isl_bool sv;
11767 sv = isl_map_plain_is_single_valued(map);
11768 if (sv < 0 || sv)
11769 return sv;
11771 test = isl_map_reverse(isl_map_copy(map));
11772 test = isl_map_apply_range(test, isl_map_copy(map));
11774 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11775 id = isl_map_identity(space);
11777 sv = isl_map_is_subset(test, id);
11779 isl_map_free(test);
11780 isl_map_free(id);
11782 return sv;
11785 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11787 isl_bool in;
11789 map = isl_map_copy(map);
11790 map = isl_map_reverse(map);
11791 in = isl_map_is_single_valued(map);
11792 isl_map_free(map);
11794 return in;
11797 /* Check if the given map is obviously injective.
11799 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11801 isl_bool in;
11803 map = isl_map_copy(map);
11804 map = isl_map_reverse(map);
11805 in = isl_map_plain_is_single_valued(map);
11806 isl_map_free(map);
11808 return in;
11811 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11813 isl_bool sv;
11815 sv = isl_map_is_single_valued(map);
11816 if (sv < 0 || !sv)
11817 return sv;
11819 return isl_map_is_injective(map);
11822 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11824 return isl_map_is_single_valued(set_to_map(set));
11827 /* Does "map" only map elements to themselves?
11829 * If the domain and range spaces are different, then "map"
11830 * is considered not to be an identity relation, even if it is empty.
11831 * Otherwise, construct the maximal identity relation and
11832 * check whether "map" is a subset of this relation.
11834 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11836 isl_map *id;
11837 isl_bool equal, is_identity;
11839 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
11840 if (equal < 0 || !equal)
11841 return equal;
11843 id = isl_map_identity(isl_map_get_space(map));
11844 is_identity = isl_map_is_subset(map, id);
11845 isl_map_free(id);
11847 return is_identity;
11850 int isl_map_is_translation(__isl_keep isl_map *map)
11852 int ok;
11853 isl_set *delta;
11855 delta = isl_map_deltas(isl_map_copy(map));
11856 ok = isl_set_is_singleton(delta);
11857 isl_set_free(delta);
11859 return ok;
11862 static int unique(isl_int *p, unsigned pos, unsigned len)
11864 if (isl_seq_first_non_zero(p, pos) != -1)
11865 return 0;
11866 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11867 return 0;
11868 return 1;
11871 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11873 int i, j;
11874 isl_size nvar, n_div;
11875 unsigned ovar;
11877 n_div = isl_basic_set_dim(bset, isl_dim_div);
11878 if (n_div < 0)
11879 return isl_bool_error;
11880 if (n_div != 0)
11881 return isl_bool_false;
11883 nvar = isl_basic_set_dim(bset, isl_dim_set);
11884 if (nvar < 0)
11885 return isl_bool_error;
11886 ovar = isl_space_offset(bset->dim, isl_dim_set);
11887 for (j = 0; j < nvar; ++j) {
11888 int lower = 0, upper = 0;
11889 for (i = 0; i < bset->n_eq; ++i) {
11890 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11891 continue;
11892 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11893 return isl_bool_false;
11894 break;
11896 if (i < bset->n_eq)
11897 continue;
11898 for (i = 0; i < bset->n_ineq; ++i) {
11899 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11900 continue;
11901 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11902 return isl_bool_false;
11903 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11904 lower = 1;
11905 else
11906 upper = 1;
11908 if (!lower || !upper)
11909 return isl_bool_false;
11912 return isl_bool_true;
11915 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11917 if (!set)
11918 return isl_bool_error;
11919 if (set->n != 1)
11920 return isl_bool_false;
11922 return isl_basic_set_is_box(set->p[0]);
11925 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11927 if (!bset)
11928 return isl_bool_error;
11930 return isl_space_is_wrapping(bset->dim);
11933 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11935 if (!set)
11936 return isl_bool_error;
11938 return isl_space_is_wrapping(set->dim);
11941 /* Modify the space of "map" through a call to "change".
11942 * If "can_change" is set (not NULL), then first call it to check
11943 * if the modification is allowed, printing the error message "cannot_change"
11944 * if it is not.
11946 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11947 isl_bool (*can_change)(__isl_keep isl_map *map),
11948 const char *cannot_change,
11949 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11951 isl_bool ok;
11952 isl_space *space;
11954 if (!map)
11955 return NULL;
11957 ok = can_change ? can_change(map) : isl_bool_true;
11958 if (ok < 0)
11959 return isl_map_free(map);
11960 if (!ok)
11961 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11962 return isl_map_free(map));
11964 space = change(isl_map_get_space(map));
11965 map = isl_map_reset_space(map, space);
11967 return map;
11970 /* Is the domain of "map" a wrapped relation?
11972 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11974 if (!map)
11975 return isl_bool_error;
11977 return isl_space_domain_is_wrapping(map->dim);
11980 /* Does "map" have a wrapped relation in both domain and range?
11982 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11984 return isl_space_is_product(isl_map_peek_space(map));
11987 /* Is the range of "map" a wrapped relation?
11989 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11991 if (!map)
11992 return isl_bool_error;
11994 return isl_space_range_is_wrapping(map->dim);
11997 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11999 isl_space *space;
12001 space = isl_basic_map_take_space(bmap);
12002 space = isl_space_wrap(space);
12003 bmap = isl_basic_map_restore_space(bmap, space);
12005 bmap = isl_basic_map_finalize(bmap);
12007 return bset_from_bmap(bmap);
12010 /* Given a map A -> B, return the set (A -> B).
12012 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12014 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12017 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12019 bset = isl_basic_set_cow(bset);
12020 if (!bset)
12021 return NULL;
12023 bset->dim = isl_space_unwrap(bset->dim);
12024 if (!bset->dim)
12025 goto error;
12027 bset = isl_basic_set_finalize(bset);
12029 return bset_to_bmap(bset);
12030 error:
12031 isl_basic_set_free(bset);
12032 return NULL;
12035 /* Given a set (A -> B), return the map A -> B.
12036 * Error out if "set" is not of the form (A -> B).
12038 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12040 return isl_map_change_space(set, &isl_set_is_wrapping,
12041 "not a wrapping set", &isl_space_unwrap);
12044 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12045 enum isl_dim_type type)
12047 isl_space *space;
12049 space = isl_basic_map_take_space(bmap);
12050 space = isl_space_reset(space, type);
12051 bmap = isl_basic_map_restore_space(bmap, space);
12053 bmap = isl_basic_map_mark_final(bmap);
12055 return bmap;
12058 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12059 enum isl_dim_type type)
12061 int i;
12062 isl_space *space;
12064 if (!map)
12065 return NULL;
12067 if (!isl_space_is_named_or_nested(map->dim, type))
12068 return map;
12070 map = isl_map_cow(map);
12071 if (!map)
12072 return NULL;
12074 for (i = 0; i < map->n; ++i) {
12075 map->p[i] = isl_basic_map_reset(map->p[i], type);
12076 if (!map->p[i])
12077 goto error;
12080 space = isl_map_take_space(map);
12081 space = isl_space_reset(space, type);
12082 map = isl_map_restore_space(map, space);
12084 return map;
12085 error:
12086 isl_map_free(map);
12087 return NULL;
12090 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12092 isl_space *space;
12094 space = isl_basic_map_take_space(bmap);
12095 space = isl_space_flatten(space);
12096 bmap = isl_basic_map_restore_space(bmap, space);
12098 bmap = isl_basic_map_mark_final(bmap);
12100 return bmap;
12103 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12105 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12108 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12109 __isl_take isl_basic_map *bmap)
12111 isl_space *space;
12113 space = isl_basic_map_take_space(bmap);
12114 space = isl_space_flatten_domain(space);
12115 bmap = isl_basic_map_restore_space(bmap, space);
12117 bmap = isl_basic_map_mark_final(bmap);
12119 return bmap;
12122 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12123 __isl_take isl_basic_map *bmap)
12125 isl_space *space;
12127 space = isl_basic_map_take_space(bmap);
12128 space = isl_space_flatten_range(space);
12129 bmap = isl_basic_map_restore_space(bmap, space);
12131 bmap = isl_basic_map_mark_final(bmap);
12133 return bmap;
12136 /* Remove any internal structure from the spaces of domain and range of "map".
12138 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12140 if (!map)
12141 return NULL;
12143 if (!map->dim->nested[0] && !map->dim->nested[1])
12144 return map;
12146 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12149 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12151 return set_from_map(isl_map_flatten(set_to_map(set)));
12154 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12156 isl_space *space, *flat_space;
12157 isl_map *map;
12159 space = isl_set_get_space(set);
12160 flat_space = isl_space_flatten(isl_space_copy(space));
12161 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12162 flat_space));
12163 map = isl_map_intersect_domain(map, set);
12165 return map;
12168 /* Remove any internal structure from the space of the domain of "map".
12170 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12172 if (!map)
12173 return NULL;
12175 if (!map->dim->nested[0])
12176 return map;
12178 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12181 /* Remove any internal structure from the space of the range of "map".
12183 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12185 if (!map)
12186 return NULL;
12188 if (!map->dim->nested[1])
12189 return map;
12191 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12194 /* Reorder the dimensions of "bmap" according to the given dim_map
12195 * and set the dimension specification to "space" and
12196 * perform Gaussian elimination on the result.
12198 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12199 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12201 isl_basic_map *res;
12202 unsigned flags;
12203 isl_size n_div;
12205 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12206 if (n_div < 0 || !space || !dim_map)
12207 goto error;
12209 flags = bmap->flags;
12210 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12211 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12212 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12213 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12214 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12215 if (res)
12216 res->flags = flags;
12217 res = isl_basic_map_gauss(res, NULL);
12218 res = isl_basic_map_finalize(res);
12219 return res;
12220 error:
12221 isl_dim_map_free(dim_map);
12222 isl_basic_map_free(bmap);
12223 isl_space_free(space);
12224 return NULL;
12227 /* Reorder the dimensions of "map" according to given reordering.
12229 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12230 __isl_take isl_reordering *r)
12232 int i;
12233 struct isl_dim_map *dim_map;
12235 map = isl_map_cow(map);
12236 dim_map = isl_dim_map_from_reordering(r);
12237 if (!map || !r || !dim_map)
12238 goto error;
12240 for (i = 0; i < map->n; ++i) {
12241 struct isl_dim_map *dim_map_i;
12242 isl_space *space;
12244 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12246 space = isl_reordering_get_space(r);
12247 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12249 if (!map->p[i])
12250 goto error;
12253 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12254 map = isl_map_unmark_normalized(map);
12256 isl_reordering_free(r);
12257 isl_dim_map_free(dim_map);
12258 return map;
12259 error:
12260 isl_dim_map_free(dim_map);
12261 isl_map_free(map);
12262 isl_reordering_free(r);
12263 return NULL;
12266 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12267 __isl_take isl_reordering *r)
12269 return set_from_map(isl_map_realign(set_to_map(set), r));
12272 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12273 __isl_take isl_space *model)
12275 isl_ctx *ctx;
12276 isl_bool aligned;
12278 if (!map || !model)
12279 goto error;
12281 ctx = isl_space_get_ctx(model);
12282 if (!isl_space_has_named_params(model))
12283 isl_die(ctx, isl_error_invalid,
12284 "model has unnamed parameters", goto error);
12285 if (isl_map_check_named_params(map) < 0)
12286 goto error;
12287 aligned = isl_map_space_has_equal_params(map, model);
12288 if (aligned < 0)
12289 goto error;
12290 if (!aligned) {
12291 isl_reordering *exp;
12293 exp = isl_parameter_alignment_reordering(map->dim, model);
12294 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12295 map = isl_map_realign(map, exp);
12298 isl_space_free(model);
12299 return map;
12300 error:
12301 isl_space_free(model);
12302 isl_map_free(map);
12303 return NULL;
12306 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12307 __isl_take isl_space *model)
12309 return isl_map_align_params(set, model);
12312 /* Align the parameters of "bmap" to those of "model", introducing
12313 * additional parameters if needed.
12315 __isl_give isl_basic_map *isl_basic_map_align_params(
12316 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12318 isl_ctx *ctx;
12319 isl_bool equal_params;
12321 if (!bmap || !model)
12322 goto error;
12324 ctx = isl_space_get_ctx(model);
12325 if (!isl_space_has_named_params(model))
12326 isl_die(ctx, isl_error_invalid,
12327 "model has unnamed parameters", goto error);
12328 if (isl_basic_map_check_named_params(bmap) < 0)
12329 goto error;
12330 equal_params = isl_space_has_equal_params(bmap->dim, model);
12331 if (equal_params < 0)
12332 goto error;
12333 if (!equal_params) {
12334 isl_reordering *exp;
12335 struct isl_dim_map *dim_map;
12337 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12338 exp = isl_reordering_extend_space(exp,
12339 isl_basic_map_get_space(bmap));
12340 dim_map = isl_dim_map_from_reordering(exp);
12341 bmap = isl_basic_map_realign(bmap,
12342 isl_reordering_get_space(exp),
12343 isl_dim_map_extend(dim_map, bmap));
12344 isl_reordering_free(exp);
12345 isl_dim_map_free(dim_map);
12348 isl_space_free(model);
12349 return bmap;
12350 error:
12351 isl_space_free(model);
12352 isl_basic_map_free(bmap);
12353 return NULL;
12356 /* Do "bset" and "space" have the same parameters?
12358 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12359 __isl_keep isl_space *space)
12361 isl_space *bset_space;
12363 bset_space = isl_basic_set_peek_space(bset);
12364 return isl_space_has_equal_params(bset_space, space);
12367 /* Do "map" and "space" have the same parameters?
12369 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12370 __isl_keep isl_space *space)
12372 isl_space *map_space;
12374 map_space = isl_map_peek_space(map);
12375 return isl_space_has_equal_params(map_space, space);
12378 /* Do "set" and "space" have the same parameters?
12380 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12381 __isl_keep isl_space *space)
12383 return isl_map_space_has_equal_params(set_to_map(set), space);
12386 /* Align the parameters of "bset" to those of "model", introducing
12387 * additional parameters if needed.
12389 __isl_give isl_basic_set *isl_basic_set_align_params(
12390 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12392 return isl_basic_map_align_params(bset, model);
12395 /* Drop all parameters not referenced by "map".
12397 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12399 int i;
12400 isl_size n;
12402 n = isl_map_dim(map, isl_dim_param);
12403 if (isl_map_check_named_params(map) < 0 || n < 0)
12404 return isl_map_free(map);
12406 for (i = n - 1; i >= 0; i--) {
12407 isl_bool involves;
12409 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12410 if (involves < 0)
12411 return isl_map_free(map);
12412 if (!involves)
12413 map = isl_map_project_out(map, isl_dim_param, i, 1);
12416 return map;
12419 /* Drop all parameters not referenced by "set".
12421 __isl_give isl_set *isl_set_drop_unused_params(
12422 __isl_take isl_set *set)
12424 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12427 /* Drop all parameters not referenced by "bmap".
12429 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12430 __isl_take isl_basic_map *bmap)
12432 isl_size nparam;
12433 int i;
12435 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12436 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12437 return isl_basic_map_free(bmap);
12439 for (i = nparam - 1; i >= 0; i--) {
12440 isl_bool involves;
12442 involves = isl_basic_map_involves_dims(bmap,
12443 isl_dim_param, i, 1);
12444 if (involves < 0)
12445 return isl_basic_map_free(bmap);
12446 if (!involves)
12447 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12450 return bmap;
12453 /* Drop all parameters not referenced by "bset".
12455 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12456 __isl_take isl_basic_set *bset)
12458 return bset_from_bmap(isl_basic_map_drop_unused_params(
12459 bset_to_bmap(bset)));
12462 /* Given a tuple of identifiers "tuple" in a space that corresponds
12463 * to that of "set", if any of those identifiers appear as parameters
12464 * in "set", then equate those parameters with the corresponding
12465 * set dimensions and project out the parameters.
12466 * The result therefore has no such parameters.
12468 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12469 __isl_keep isl_multi_id *tuple)
12471 int i;
12472 isl_size n;
12473 isl_space *set_space, *tuple_space;
12475 set_space = isl_set_peek_space(set);
12476 tuple_space = isl_multi_id_peek_space(tuple);
12477 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12478 return isl_set_free(set);
12479 n = isl_multi_id_size(tuple);
12480 if (n < 0)
12481 return isl_set_free(set);
12482 for (i = 0; i < n; ++i) {
12483 isl_id *id;
12484 int pos;
12486 id = isl_multi_id_get_at(tuple, i);
12487 if (!id)
12488 return isl_set_free(set);
12489 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12490 isl_id_free(id);
12491 if (pos < 0)
12492 continue;
12493 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12494 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12496 return set;
12499 /* Bind the set dimensions of "set" to parameters with identifiers
12500 * specified by "tuple", living in the same space as "set".
12502 * If no parameters with these identifiers appear in "set" already,
12503 * then the set dimensions are simply reinterpreted as parameters.
12504 * Otherwise, the parameters are first equated to the corresponding
12505 * set dimensions.
12507 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12508 __isl_take isl_multi_id *tuple)
12510 isl_space *space;
12512 set = equate_params(set, tuple);
12513 space = isl_set_get_space(set);
12514 space = isl_space_bind_set(space, tuple);
12515 isl_multi_id_free(tuple);
12516 set = isl_set_reset_space(set, space);
12518 return set;
12521 /* Given a tuple of identifiers "tuple" in a space that corresponds
12522 * to the domain of "map", if any of those identifiers appear as parameters
12523 * in "map", then equate those parameters with the corresponding
12524 * input dimensions and project out the parameters.
12525 * The result therefore has no such parameters.
12527 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12528 __isl_keep isl_multi_id *tuple)
12530 int i;
12531 isl_size n;
12532 isl_space *map_space, *tuple_space;
12534 map_space = isl_map_peek_space(map);
12535 tuple_space = isl_multi_id_peek_space(tuple);
12536 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12537 return isl_map_free(map);
12538 n = isl_multi_id_size(tuple);
12539 if (n < 0)
12540 return isl_map_free(map);
12541 for (i = 0; i < n; ++i) {
12542 isl_id *id;
12543 int pos;
12545 id = isl_multi_id_get_at(tuple, i);
12546 if (!id)
12547 return isl_map_free(map);
12548 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12549 isl_id_free(id);
12550 if (pos < 0)
12551 continue;
12552 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12553 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12555 return map;
12558 /* Bind the input dimensions of "map" to parameters with identifiers
12559 * specified by "tuple", living in the domain space of "map".
12561 * If no parameters with these identifiers appear in "map" already,
12562 * then the input dimensions are simply reinterpreted as parameters.
12563 * Otherwise, the parameters are first equated to the corresponding
12564 * input dimensions.
12566 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12567 __isl_take isl_multi_id *tuple)
12569 isl_space *space;
12570 isl_set *set;
12572 map = map_equate_params(map, tuple);
12573 space = isl_map_get_space(map);
12574 space = isl_space_bind_map_domain(space, tuple);
12575 isl_multi_id_free(tuple);
12576 set = set_from_map(isl_map_reset_space(map, space));
12578 return set;
12581 /* Bind the output dimensions of "map" to parameters with identifiers
12582 * specified by "tuple", living in the range space of "map".
12584 * Since binding is more easily implemented on the domain,
12585 * bind the input dimensions of the inverse of "map".
12587 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12588 __isl_take isl_multi_id *tuple)
12590 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12593 /* Insert a domain corresponding to "tuple"
12594 * into the nullary or unary relation "set".
12595 * The result has an extra initial tuple and is therefore
12596 * either a unary or binary relation.
12597 * Any parameters with identifiers in "tuple" are reinterpreted
12598 * as the corresponding domain dimensions.
12600 static __isl_give isl_map *unbind_params_insert_domain(
12601 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12603 isl_space *space;
12604 isl_reordering *r;
12606 space = isl_set_peek_space(set);
12607 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12608 isl_multi_id_free(tuple);
12610 return isl_map_realign(set_to_map(set), r);
12613 /* Construct a set with "tuple" as domain from the parameter domain "set".
12614 * Any parameters with identifiers in "tuple" are reinterpreted
12615 * as the corresponding set dimensions.
12617 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12618 __isl_take isl_multi_id *tuple)
12620 isl_bool is_params;
12622 is_params = isl_set_is_params(set);
12623 if (is_params < 0)
12624 set = isl_set_free(set);
12625 else if (!is_params)
12626 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12627 "expecting parameter domain", set = isl_set_free(set));
12628 return set_from_map(unbind_params_insert_domain(set, tuple));
12631 /* Construct a map with "domain" as domain and "set" as range.
12632 * Any parameters with identifiers in "domain" are reinterpreted
12633 * as the corresponding domain dimensions.
12635 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12636 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12638 isl_bool is_params;
12640 is_params = isl_set_is_params(set);
12641 if (is_params < 0)
12642 set = isl_set_free(set);
12643 else if (is_params)
12644 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12645 "expecting proper set", set = isl_set_free(set));
12646 return unbind_params_insert_domain(set, domain);
12649 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12650 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12651 enum isl_dim_type c2, enum isl_dim_type c3,
12652 enum isl_dim_type c4, enum isl_dim_type c5)
12654 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12655 struct isl_mat *mat;
12656 int i, j, k;
12657 int pos;
12658 isl_size total;
12660 total = isl_basic_map_dim(bmap, isl_dim_all);
12661 if (total < 0)
12662 return NULL;
12663 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12664 if (!mat)
12665 return NULL;
12666 for (i = 0; i < bmap->n_eq; ++i)
12667 for (j = 0, pos = 0; j < 5; ++j) {
12668 int off = isl_basic_map_offset(bmap, c[j]);
12669 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12670 if (dim < 0)
12671 return isl_mat_free(mat);
12672 for (k = 0; k < dim; ++k) {
12673 isl_int_set(mat->row[i][pos],
12674 bmap->eq[i][off + k]);
12675 ++pos;
12679 return mat;
12682 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12683 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12684 enum isl_dim_type c2, enum isl_dim_type c3,
12685 enum isl_dim_type c4, enum isl_dim_type c5)
12687 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12688 struct isl_mat *mat;
12689 int i, j, k;
12690 int pos;
12691 isl_size total;
12693 total = isl_basic_map_dim(bmap, isl_dim_all);
12694 if (total < 0)
12695 return NULL;
12696 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12697 if (!mat)
12698 return NULL;
12699 for (i = 0; i < bmap->n_ineq; ++i)
12700 for (j = 0, pos = 0; j < 5; ++j) {
12701 int off = isl_basic_map_offset(bmap, c[j]);
12702 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12703 if (dim < 0)
12704 return isl_mat_free(mat);
12705 for (k = 0; k < dim; ++k) {
12706 isl_int_set(mat->row[i][pos],
12707 bmap->ineq[i][off + k]);
12708 ++pos;
12712 return mat;
12715 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12716 __isl_take isl_space *space,
12717 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12718 enum isl_dim_type c2, enum isl_dim_type c3,
12719 enum isl_dim_type c4, enum isl_dim_type c5)
12721 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12722 isl_basic_map *bmap = NULL;
12723 isl_size dim;
12724 unsigned total;
12725 unsigned extra;
12726 int i, j, k, l;
12727 int pos;
12729 dim = isl_space_dim(space, isl_dim_all);
12730 if (dim < 0 || !eq || !ineq)
12731 goto error;
12733 if (eq->n_col != ineq->n_col)
12734 isl_die(space->ctx, isl_error_invalid,
12735 "equalities and inequalities matrices should have "
12736 "same number of columns", goto error);
12738 total = 1 + dim;
12740 if (eq->n_col < total)
12741 isl_die(space->ctx, isl_error_invalid,
12742 "number of columns too small", goto error);
12744 extra = eq->n_col - total;
12746 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12747 eq->n_row, ineq->n_row);
12748 if (!bmap)
12749 goto error;
12750 for (i = 0; i < extra; ++i) {
12751 k = isl_basic_map_alloc_div(bmap);
12752 if (k < 0)
12753 goto error;
12754 isl_int_set_si(bmap->div[k][0], 0);
12756 for (i = 0; i < eq->n_row; ++i) {
12757 l = isl_basic_map_alloc_equality(bmap);
12758 if (l < 0)
12759 goto error;
12760 for (j = 0, pos = 0; j < 5; ++j) {
12761 int off = isl_basic_map_offset(bmap, c[j]);
12762 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12763 if (dim < 0)
12764 goto error;
12765 for (k = 0; k < dim; ++k) {
12766 isl_int_set(bmap->eq[l][off + k],
12767 eq->row[i][pos]);
12768 ++pos;
12772 for (i = 0; i < ineq->n_row; ++i) {
12773 l = isl_basic_map_alloc_inequality(bmap);
12774 if (l < 0)
12775 goto error;
12776 for (j = 0, pos = 0; j < 5; ++j) {
12777 int off = isl_basic_map_offset(bmap, c[j]);
12778 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12779 if (dim < 0)
12780 goto error;
12781 for (k = 0; k < dim; ++k) {
12782 isl_int_set(bmap->ineq[l][off + k],
12783 ineq->row[i][pos]);
12784 ++pos;
12789 isl_space_free(space);
12790 isl_mat_free(eq);
12791 isl_mat_free(ineq);
12793 bmap = isl_basic_map_simplify(bmap);
12794 return isl_basic_map_finalize(bmap);
12795 error:
12796 isl_space_free(space);
12797 isl_mat_free(eq);
12798 isl_mat_free(ineq);
12799 isl_basic_map_free(bmap);
12800 return NULL;
12803 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12804 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12805 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12807 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12808 c1, c2, c3, c4, isl_dim_in);
12811 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12812 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12813 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12815 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12816 c1, c2, c3, c4, isl_dim_in);
12819 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12820 __isl_take isl_space *space,
12821 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12822 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12824 isl_basic_map *bmap;
12825 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12826 c1, c2, c3, c4, isl_dim_in);
12827 return bset_from_bmap(bmap);
12830 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12832 if (!bmap)
12833 return isl_bool_error;
12835 return isl_space_can_zip(bmap->dim);
12838 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12840 if (!map)
12841 return isl_bool_error;
12843 return isl_space_can_zip(map->dim);
12846 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12847 * (A -> C) -> (B -> D).
12849 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12851 unsigned pos;
12852 isl_size n_in;
12853 isl_size n1;
12854 isl_size n2;
12856 if (!bmap)
12857 return NULL;
12859 if (!isl_basic_map_can_zip(bmap))
12860 isl_die(bmap->ctx, isl_error_invalid,
12861 "basic map cannot be zipped", goto error);
12862 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12863 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12864 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12865 if (n_in < 0 || n1 < 0 || n2 < 0)
12866 return isl_basic_map_free(bmap);
12867 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12868 bmap = isl_basic_map_cow(bmap);
12869 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12870 if (!bmap)
12871 return NULL;
12872 bmap->dim = isl_space_zip(bmap->dim);
12873 if (!bmap->dim)
12874 goto error;
12875 bmap = isl_basic_map_mark_final(bmap);
12876 return bmap;
12877 error:
12878 isl_basic_map_free(bmap);
12879 return NULL;
12882 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12883 * (A -> C) -> (B -> D).
12885 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12887 if (!map)
12888 return NULL;
12890 if (!isl_map_can_zip(map))
12891 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12892 goto error);
12894 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
12895 error:
12896 isl_map_free(map);
12897 return NULL;
12900 /* Can we apply isl_basic_map_curry to "bmap"?
12901 * That is, does it have a nested relation in its domain?
12903 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12905 if (!bmap)
12906 return isl_bool_error;
12908 return isl_space_can_curry(bmap->dim);
12911 /* Can we apply isl_map_curry to "map"?
12912 * That is, does it have a nested relation in its domain?
12914 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12916 if (!map)
12917 return isl_bool_error;
12919 return isl_space_can_curry(map->dim);
12922 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12923 * A -> (B -> C).
12925 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12928 if (!bmap)
12929 return NULL;
12931 if (!isl_basic_map_can_curry(bmap))
12932 isl_die(bmap->ctx, isl_error_invalid,
12933 "basic map cannot be curried", goto error);
12934 bmap = isl_basic_map_cow(bmap);
12935 if (!bmap)
12936 return NULL;
12937 bmap->dim = isl_space_curry(bmap->dim);
12938 if (!bmap->dim)
12939 goto error;
12940 bmap = isl_basic_map_mark_final(bmap);
12941 return bmap;
12942 error:
12943 isl_basic_map_free(bmap);
12944 return NULL;
12947 /* Given a map (A -> B) -> C, return the corresponding map
12948 * A -> (B -> C).
12950 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12952 return isl_map_change_space(map, &isl_map_can_curry,
12953 "map cannot be curried", &isl_space_curry);
12956 /* Can isl_map_range_curry be applied to "map"?
12957 * That is, does it have a nested relation in its range,
12958 * the domain of which is itself a nested relation?
12960 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12962 if (!map)
12963 return isl_bool_error;
12965 return isl_space_can_range_curry(map->dim);
12968 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12969 * A -> (B -> (C -> D)).
12971 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12973 return isl_map_change_space(map, &isl_map_can_range_curry,
12974 "map range cannot be curried",
12975 &isl_space_range_curry);
12978 /* Can we apply isl_basic_map_uncurry to "bmap"?
12979 * That is, does it have a nested relation in its domain?
12981 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12983 if (!bmap)
12984 return isl_bool_error;
12986 return isl_space_can_uncurry(bmap->dim);
12989 /* Can we apply isl_map_uncurry to "map"?
12990 * That is, does it have a nested relation in its domain?
12992 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12994 if (!map)
12995 return isl_bool_error;
12997 return isl_space_can_uncurry(map->dim);
13000 /* Given a basic map A -> (B -> C), return the corresponding basic map
13001 * (A -> B) -> C.
13003 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13006 if (!bmap)
13007 return NULL;
13009 if (!isl_basic_map_can_uncurry(bmap))
13010 isl_die(bmap->ctx, isl_error_invalid,
13011 "basic map cannot be uncurried",
13012 return isl_basic_map_free(bmap));
13013 bmap = isl_basic_map_cow(bmap);
13014 if (!bmap)
13015 return NULL;
13016 bmap->dim = isl_space_uncurry(bmap->dim);
13017 if (!bmap->dim)
13018 return isl_basic_map_free(bmap);
13019 bmap = isl_basic_map_mark_final(bmap);
13020 return bmap;
13023 /* Given a map A -> (B -> C), return the corresponding map
13024 * (A -> B) -> C.
13026 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13028 return isl_map_change_space(map, &isl_map_can_uncurry,
13029 "map cannot be uncurried", &isl_space_uncurry);
13032 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13033 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13035 return isl_map_equate(set, type1, pos1, type2, pos2);
13038 /* Construct a basic map where the given dimensions are equal to each other.
13040 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13041 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13043 isl_basic_map *bmap = NULL;
13044 int i;
13045 isl_size total;
13047 total = isl_space_dim(space, isl_dim_all);
13048 if (total < 0 ||
13049 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13050 isl_space_check_range(space, type2, pos2, 1) < 0)
13051 goto error;
13053 if (type1 == type2 && pos1 == pos2)
13054 return isl_basic_map_universe(space);
13056 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13057 i = isl_basic_map_alloc_equality(bmap);
13058 if (i < 0)
13059 goto error;
13060 isl_seq_clr(bmap->eq[i], 1 + total);
13061 pos1 += isl_basic_map_offset(bmap, type1);
13062 pos2 += isl_basic_map_offset(bmap, type2);
13063 isl_int_set_si(bmap->eq[i][pos1], -1);
13064 isl_int_set_si(bmap->eq[i][pos2], 1);
13065 bmap = isl_basic_map_finalize(bmap);
13066 isl_space_free(space);
13067 return bmap;
13068 error:
13069 isl_space_free(space);
13070 isl_basic_map_free(bmap);
13071 return NULL;
13074 /* Add a constraint imposing that the given two dimensions are equal.
13076 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13077 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13079 isl_basic_map *eq;
13081 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13083 bmap = isl_basic_map_intersect(bmap, eq);
13085 return bmap;
13088 /* Add a constraint imposing that the given two dimensions are equal.
13090 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13091 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13093 isl_basic_map *bmap;
13095 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13097 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13099 return map;
13102 /* Add a constraint imposing that the given two dimensions have opposite values.
13104 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13105 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13107 isl_basic_map *bmap = NULL;
13108 int i;
13109 isl_size total;
13111 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13112 return isl_map_free(map);
13113 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13114 return isl_map_free(map);
13116 total = isl_map_dim(map, isl_dim_all);
13117 if (total < 0)
13118 return isl_map_free(map);
13119 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13120 i = isl_basic_map_alloc_equality(bmap);
13121 if (i < 0)
13122 goto error;
13123 isl_seq_clr(bmap->eq[i], 1 + total);
13124 pos1 += isl_basic_map_offset(bmap, type1);
13125 pos2 += isl_basic_map_offset(bmap, type2);
13126 isl_int_set_si(bmap->eq[i][pos1], 1);
13127 isl_int_set_si(bmap->eq[i][pos2], 1);
13128 bmap = isl_basic_map_finalize(bmap);
13130 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13132 return map;
13133 error:
13134 isl_basic_map_free(bmap);
13135 isl_map_free(map);
13136 return NULL;
13139 /* Construct a constraint imposing that the value of the first dimension is
13140 * greater than or equal to that of the second.
13142 static __isl_give isl_constraint *constraint_order_ge(
13143 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13144 enum isl_dim_type type2, int pos2)
13146 isl_constraint *c;
13148 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13149 isl_space_check_range(space, type2, pos2, 1) < 0)
13150 space = isl_space_free(space);
13151 if (!space)
13152 return NULL;
13154 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13156 if (type1 == type2 && pos1 == pos2)
13157 return c;
13159 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13160 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13162 return c;
13165 /* Add a constraint imposing that the value of the first dimension is
13166 * greater than or equal to that of the second.
13168 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13169 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13171 isl_constraint *c;
13172 isl_space *space;
13174 if (type1 == type2 && pos1 == pos2)
13175 return bmap;
13176 space = isl_basic_map_get_space(bmap);
13177 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13178 bmap = isl_basic_map_add_constraint(bmap, c);
13180 return bmap;
13183 /* Add a constraint imposing that the value of the first dimension is
13184 * greater than or equal to that of the second.
13186 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13187 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13189 isl_constraint *c;
13190 isl_space *space;
13192 if (type1 == type2 && pos1 == pos2)
13193 return map;
13194 space = isl_map_get_space(map);
13195 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13196 map = isl_map_add_constraint(map, c);
13198 return map;
13201 /* Add a constraint imposing that the value of the first dimension is
13202 * less than or equal to that of the second.
13204 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13205 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13207 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13210 /* Construct a basic map where the value of the first dimension is
13211 * greater than that of the second.
13213 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13214 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13216 isl_basic_map *bmap = NULL;
13217 int i;
13218 isl_size total;
13220 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13221 isl_space_check_range(space, type2, pos2, 1) < 0)
13222 goto error;
13224 if (type1 == type2 && pos1 == pos2)
13225 return isl_basic_map_empty(space);
13227 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13228 total = isl_basic_map_dim(bmap, isl_dim_all);
13229 i = isl_basic_map_alloc_inequality(bmap);
13230 if (total < 0 || i < 0)
13231 return isl_basic_map_free(bmap);
13232 isl_seq_clr(bmap->ineq[i], 1 + total);
13233 pos1 += isl_basic_map_offset(bmap, type1);
13234 pos2 += isl_basic_map_offset(bmap, type2);
13235 isl_int_set_si(bmap->ineq[i][pos1], 1);
13236 isl_int_set_si(bmap->ineq[i][pos2], -1);
13237 isl_int_set_si(bmap->ineq[i][0], -1);
13238 bmap = isl_basic_map_finalize(bmap);
13240 return bmap;
13241 error:
13242 isl_space_free(space);
13243 isl_basic_map_free(bmap);
13244 return NULL;
13247 /* Add a constraint imposing that the value of the first dimension is
13248 * greater than that of the second.
13250 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13251 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13253 isl_basic_map *gt;
13255 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13257 bmap = isl_basic_map_intersect(bmap, gt);
13259 return bmap;
13262 /* Add a constraint imposing that the value of the first dimension is
13263 * greater than that of the second.
13265 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13266 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13268 isl_basic_map *bmap;
13270 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13272 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13274 return map;
13277 /* Add a constraint imposing that the value of the first dimension is
13278 * smaller than that of the second.
13280 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13281 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13283 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13286 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13287 int pos)
13289 isl_aff *div;
13290 isl_local_space *ls;
13292 if (!bmap)
13293 return NULL;
13295 if (!isl_basic_map_divs_known(bmap))
13296 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13297 "some divs are unknown", return NULL);
13299 ls = isl_basic_map_get_local_space(bmap);
13300 div = isl_local_space_get_div(ls, pos);
13301 isl_local_space_free(ls);
13303 return div;
13306 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13307 int pos)
13309 return isl_basic_map_get_div(bset, pos);
13312 /* Plug in "subs" for dimension "type", "pos" of "bset".
13314 * Let i be the dimension to replace and let "subs" be of the form
13316 * f/d
13318 * Any integer division with a non-zero coefficient for i,
13320 * floor((a i + g)/m)
13322 * is replaced by
13324 * floor((a f + d g)/(m d))
13326 * Constraints of the form
13328 * a i + g
13330 * are replaced by
13332 * a f + d g
13334 * We currently require that "subs" is an integral expression.
13335 * Handling rational expressions may require us to add stride constraints
13336 * as we do in isl_basic_set_preimage_multi_aff.
13338 __isl_give isl_basic_set *isl_basic_set_substitute(
13339 __isl_take isl_basic_set *bset,
13340 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13342 int i;
13343 isl_int v;
13344 isl_ctx *ctx;
13345 isl_size n_div;
13347 if (bset && isl_basic_set_plain_is_empty(bset))
13348 return bset;
13350 bset = isl_basic_set_cow(bset);
13351 if (!bset || !subs)
13352 goto error;
13354 ctx = isl_basic_set_get_ctx(bset);
13355 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
13356 isl_die(ctx, isl_error_invalid,
13357 "spaces don't match", goto error);
13358 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
13359 if (n_div < 0)
13360 goto error;
13361 if (n_div != 0)
13362 isl_die(ctx, isl_error_unsupported,
13363 "cannot handle divs yet", goto error);
13364 if (!isl_int_is_one(subs->v->el[0]))
13365 isl_die(ctx, isl_error_invalid,
13366 "can only substitute integer expressions", goto error);
13368 pos += isl_basic_set_offset(bset, type);
13370 isl_int_init(v);
13372 for (i = 0; i < bset->n_eq; ++i) {
13373 if (isl_int_is_zero(bset->eq[i][pos]))
13374 continue;
13375 isl_int_set(v, bset->eq[i][pos]);
13376 isl_int_set_si(bset->eq[i][pos], 0);
13377 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
13378 v, subs->v->el + 1, subs->v->size - 1);
13381 for (i = 0; i < bset->n_ineq; ++i) {
13382 if (isl_int_is_zero(bset->ineq[i][pos]))
13383 continue;
13384 isl_int_set(v, bset->ineq[i][pos]);
13385 isl_int_set_si(bset->ineq[i][pos], 0);
13386 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
13387 v, subs->v->el + 1, subs->v->size - 1);
13390 for (i = 0; i < bset->n_div; ++i) {
13391 if (isl_int_is_zero(bset->div[i][1 + pos]))
13392 continue;
13393 isl_int_set(v, bset->div[i][1 + pos]);
13394 isl_int_set_si(bset->div[i][1 + pos], 0);
13395 isl_seq_combine(bset->div[i] + 1,
13396 subs->v->el[0], bset->div[i] + 1,
13397 v, subs->v->el + 1, subs->v->size - 1);
13398 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
13401 isl_int_clear(v);
13403 bset = isl_basic_set_simplify(bset);
13404 return isl_basic_set_finalize(bset);
13405 error:
13406 isl_basic_set_free(bset);
13407 return NULL;
13410 /* Plug in "subs" for dimension "type", "pos" of "set".
13412 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13413 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13415 int i;
13417 if (set && isl_set_plain_is_empty(set))
13418 return set;
13420 set = isl_set_cow(set);
13421 if (!set || !subs)
13422 goto error;
13424 for (i = set->n - 1; i >= 0; --i) {
13425 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
13426 set = set_from_map(remove_if_empty(set_to_map(set), i));
13427 if (!set)
13428 return NULL;
13431 return set;
13432 error:
13433 isl_set_free(set);
13434 return NULL;
13437 /* Check if the range of "ma" is compatible with the domain or range
13438 * (depending on "type") of "bmap".
13440 static isl_stat check_basic_map_compatible_range_multi_aff(
13441 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13442 __isl_keep isl_multi_aff *ma)
13444 isl_bool m;
13445 isl_space *ma_space;
13447 ma_space = isl_multi_aff_get_space(ma);
13449 m = isl_space_has_equal_params(bmap->dim, ma_space);
13450 if (m < 0)
13451 goto error;
13452 if (!m)
13453 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13454 "parameters don't match", goto error);
13455 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13456 if (m < 0)
13457 goto error;
13458 if (!m)
13459 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13460 "spaces don't match", goto error);
13462 isl_space_free(ma_space);
13463 return isl_stat_ok;
13464 error:
13465 isl_space_free(ma_space);
13466 return isl_stat_error;
13469 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13470 * coefficients before the transformed range of dimensions,
13471 * the "n_after" coefficients after the transformed range of dimensions
13472 * and the coefficients of the other divs in "bmap".
13474 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13475 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13477 int i;
13478 isl_size n_param;
13479 isl_size n_set;
13480 isl_local_space *ls;
13482 if (n_div == 0)
13483 return bmap;
13485 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13486 n_param = isl_local_space_dim(ls, isl_dim_param);
13487 n_set = isl_local_space_dim(ls, isl_dim_set);
13488 if (n_param < 0 || n_set < 0)
13489 return isl_basic_map_free(bmap);
13491 for (i = 0; i < n_div; ++i) {
13492 int o_bmap = 0, o_ls = 0;
13494 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13495 o_bmap += 1 + 1 + n_param;
13496 o_ls += 1 + 1 + n_param;
13497 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13498 o_bmap += n_before;
13499 isl_seq_cpy(bmap->div[i] + o_bmap,
13500 ls->div->row[i] + o_ls, n_set);
13501 o_bmap += n_set;
13502 o_ls += n_set;
13503 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13504 o_bmap += n_after;
13505 isl_seq_cpy(bmap->div[i] + o_bmap,
13506 ls->div->row[i] + o_ls, n_div);
13507 o_bmap += n_div;
13508 o_ls += n_div;
13509 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13510 bmap = isl_basic_map_add_div_constraints(bmap, i);
13511 if (!bmap)
13512 goto error;
13515 isl_local_space_free(ls);
13516 return bmap;
13517 error:
13518 isl_local_space_free(ls);
13519 return isl_basic_map_free(bmap);
13522 /* How many stride constraints does "ma" enforce?
13523 * That is, how many of the affine expressions have a denominator
13524 * different from one?
13526 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13528 int i;
13529 int strides = 0;
13531 for (i = 0; i < ma->n; ++i)
13532 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13533 strides++;
13535 return strides;
13538 /* For each affine expression in ma of the form
13540 * x_i = (f_i y + h_i)/m_i
13542 * with m_i different from one, add a constraint to "bmap"
13543 * of the form
13545 * f_i y + h_i = m_i alpha_i
13547 * with alpha_i an additional existentially quantified variable.
13549 * The input variables of "ma" correspond to a subset of the variables
13550 * of "bmap". There are "n_before" variables in "bmap" before this
13551 * subset and "n_after" variables after this subset.
13552 * The integer divisions of the affine expressions in "ma" are assumed
13553 * to have been aligned. There are "n_div_ma" of them and
13554 * they appear first in "bmap", straight after the "n_after" variables.
13556 static __isl_give isl_basic_map *add_ma_strides(
13557 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13558 int n_before, int n_after, int n_div_ma)
13560 int i, k;
13561 int div;
13562 isl_size total;
13563 isl_size n_param;
13564 isl_size n_in;
13566 total = isl_basic_map_dim(bmap, isl_dim_all);
13567 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13568 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13569 if (total < 0 || n_param < 0 || n_in < 0)
13570 return isl_basic_map_free(bmap);
13571 for (i = 0; i < ma->n; ++i) {
13572 int o_bmap = 0, o_ma = 1;
13574 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13575 continue;
13576 div = isl_basic_map_alloc_div(bmap);
13577 k = isl_basic_map_alloc_equality(bmap);
13578 if (div < 0 || k < 0)
13579 goto error;
13580 isl_int_set_si(bmap->div[div][0], 0);
13581 isl_seq_cpy(bmap->eq[k] + o_bmap,
13582 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13583 o_bmap += 1 + n_param;
13584 o_ma += 1 + n_param;
13585 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13586 o_bmap += n_before;
13587 isl_seq_cpy(bmap->eq[k] + o_bmap,
13588 ma->u.p[i]->v->el + o_ma, n_in);
13589 o_bmap += n_in;
13590 o_ma += n_in;
13591 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13592 o_bmap += n_after;
13593 isl_seq_cpy(bmap->eq[k] + o_bmap,
13594 ma->u.p[i]->v->el + o_ma, n_div_ma);
13595 o_bmap += n_div_ma;
13596 o_ma += n_div_ma;
13597 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13598 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13599 total++;
13602 return bmap;
13603 error:
13604 isl_basic_map_free(bmap);
13605 return NULL;
13608 /* Replace the domain or range space (depending on "type) of "space" by "set".
13610 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13611 enum isl_dim_type type, __isl_take isl_space *set)
13613 if (type == isl_dim_in) {
13614 space = isl_space_range(space);
13615 space = isl_space_map_from_domain_and_range(set, space);
13616 } else {
13617 space = isl_space_domain(space);
13618 space = isl_space_map_from_domain_and_range(space, set);
13621 return space;
13624 /* Compute the preimage of the domain or range (depending on "type")
13625 * of "bmap" under the function represented by "ma".
13626 * In other words, plug in "ma" in the domain or range of "bmap".
13627 * The result is a basic map that lives in the same space as "bmap"
13628 * except that the domain or range has been replaced by
13629 * the domain space of "ma".
13631 * If bmap is represented by
13633 * A(p) + S u + B x + T v + C(divs) >= 0,
13635 * where u and x are input and output dimensions if type == isl_dim_out
13636 * while x and v are input and output dimensions if type == isl_dim_in,
13637 * and ma is represented by
13639 * x = D(p) + F(y) + G(divs')
13641 * then the result is
13643 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13645 * The divs in the input set are similarly adjusted.
13646 * In particular
13648 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13650 * becomes
13652 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13653 * B_i G(divs') + c_i(divs))/n_i)
13655 * If bmap is not a rational map and if F(y) involves any denominators
13657 * x_i = (f_i y + h_i)/m_i
13659 * then additional constraints are added to ensure that we only
13660 * map back integer points. That is we enforce
13662 * f_i y + h_i = m_i alpha_i
13664 * with alpha_i an additional existentially quantified variable.
13666 * We first copy over the divs from "ma".
13667 * Then we add the modified constraints and divs from "bmap".
13668 * Finally, we add the stride constraints, if needed.
13670 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13671 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13672 __isl_take isl_multi_aff *ma)
13674 int i, k;
13675 isl_space *space;
13676 isl_basic_map *res = NULL;
13677 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13678 isl_int f, c1, c2, g;
13679 isl_bool rational;
13680 int strides;
13682 isl_int_init(f);
13683 isl_int_init(c1);
13684 isl_int_init(c2);
13685 isl_int_init(g);
13687 ma = isl_multi_aff_align_divs(ma);
13688 if (!bmap || !ma)
13689 goto error;
13690 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13691 goto error;
13693 if (type == isl_dim_in) {
13694 n_before = 0;
13695 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13696 } else {
13697 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13698 n_after = 0;
13700 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13701 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13702 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13703 goto error;
13705 space = isl_multi_aff_get_domain_space(ma);
13706 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13707 rational = isl_basic_map_is_rational(bmap);
13708 strides = rational ? 0 : multi_aff_strides(ma);
13709 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13710 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13711 if (rational)
13712 res = isl_basic_map_set_rational(res);
13714 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13715 if (isl_basic_map_alloc_div(res) < 0)
13716 goto error;
13718 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13719 if (!res)
13720 goto error;
13722 for (i = 0; i < bmap->n_eq; ++i) {
13723 k = isl_basic_map_alloc_equality(res);
13724 if (k < 0)
13725 goto error;
13726 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13727 n_after, n_div_ma, n_div_bmap,
13728 f, c1, c2, g, 0) < 0)
13729 goto error;
13732 for (i = 0; i < bmap->n_ineq; ++i) {
13733 k = isl_basic_map_alloc_inequality(res);
13734 if (k < 0)
13735 goto error;
13736 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13737 n_after, n_div_ma, n_div_bmap,
13738 f, c1, c2, g, 0) < 0)
13739 goto error;
13742 for (i = 0; i < bmap->n_div; ++i) {
13743 if (isl_int_is_zero(bmap->div[i][0])) {
13744 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13745 continue;
13747 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13748 n_before, n_after, n_div_ma, n_div_bmap,
13749 f, c1, c2, g, 1) < 0)
13750 goto error;
13753 if (strides)
13754 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13756 isl_int_clear(f);
13757 isl_int_clear(c1);
13758 isl_int_clear(c2);
13759 isl_int_clear(g);
13760 isl_basic_map_free(bmap);
13761 isl_multi_aff_free(ma);
13762 res = isl_basic_map_simplify(res);
13763 return isl_basic_map_finalize(res);
13764 error:
13765 isl_int_clear(f);
13766 isl_int_clear(c1);
13767 isl_int_clear(c2);
13768 isl_int_clear(g);
13769 isl_basic_map_free(bmap);
13770 isl_multi_aff_free(ma);
13771 isl_basic_map_free(res);
13772 return NULL;
13775 /* Compute the preimage of "bset" under the function represented by "ma".
13776 * In other words, plug in "ma" in "bset". The result is a basic set
13777 * that lives in the domain space of "ma".
13779 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13780 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13782 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13785 /* Compute the preimage of the domain of "bmap" under the function
13786 * represented by "ma".
13787 * In other words, plug in "ma" in the domain of "bmap".
13788 * The result is a basic map that lives in the same space as "bmap"
13789 * except that the domain has been replaced by the domain space of "ma".
13791 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13792 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13794 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13797 /* Compute the preimage of the range of "bmap" under the function
13798 * represented by "ma".
13799 * In other words, plug in "ma" in the range of "bmap".
13800 * The result is a basic map that lives in the same space as "bmap"
13801 * except that the range has been replaced by the domain space of "ma".
13803 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13804 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13806 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13809 /* Check if the range of "ma" is compatible with the domain or range
13810 * (depending on "type") of "map".
13811 * Return isl_stat_error if anything is wrong.
13813 static isl_stat check_map_compatible_range_multi_aff(
13814 __isl_keep isl_map *map, enum isl_dim_type type,
13815 __isl_keep isl_multi_aff *ma)
13817 isl_bool m;
13818 isl_space *ma_space;
13820 ma_space = isl_multi_aff_get_space(ma);
13821 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13822 isl_space_free(ma_space);
13823 if (m < 0)
13824 return isl_stat_error;
13825 if (!m)
13826 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13827 "spaces don't match", return isl_stat_error);
13828 return isl_stat_ok;
13831 /* Compute the preimage of the domain or range (depending on "type")
13832 * of "map" under the function represented by "ma".
13833 * In other words, plug in "ma" in the domain or range of "map".
13834 * The result is a map that lives in the same space as "map"
13835 * except that the domain or range has been replaced by
13836 * the domain space of "ma".
13838 * The parameters are assumed to have been aligned.
13840 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13841 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13843 int i;
13844 isl_space *space;
13846 map = isl_map_cow(map);
13847 ma = isl_multi_aff_align_divs(ma);
13848 if (!map || !ma)
13849 goto error;
13850 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13851 goto error;
13853 for (i = 0; i < map->n; ++i) {
13854 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13855 isl_multi_aff_copy(ma));
13856 if (!map->p[i])
13857 goto error;
13860 space = isl_multi_aff_get_domain_space(ma);
13861 space = isl_space_set(isl_map_get_space(map), type, space);
13863 isl_space_free(isl_map_take_space(map));
13864 map = isl_map_restore_space(map, space);
13865 if (!map)
13866 goto error;
13868 isl_multi_aff_free(ma);
13869 if (map->n > 1)
13870 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13871 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13872 return map;
13873 error:
13874 isl_multi_aff_free(ma);
13875 isl_map_free(map);
13876 return NULL;
13879 /* Compute the preimage of the domain or range (depending on "type")
13880 * of "map" under the function represented by "ma".
13881 * In other words, plug in "ma" in the domain or range of "map".
13882 * The result is a map that lives in the same space as "map"
13883 * except that the domain or range has been replaced by
13884 * the domain space of "ma".
13886 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13887 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13889 isl_bool aligned;
13891 if (!map || !ma)
13892 goto error;
13894 aligned = isl_map_space_has_equal_params(map, ma->space);
13895 if (aligned < 0)
13896 goto error;
13897 if (aligned)
13898 return map_preimage_multi_aff(map, type, ma);
13900 if (isl_map_check_named_params(map) < 0)
13901 goto error;
13902 if (!isl_space_has_named_params(ma->space))
13903 isl_die(map->ctx, isl_error_invalid,
13904 "unaligned unnamed parameters", goto error);
13905 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13906 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13908 return map_preimage_multi_aff(map, type, ma);
13909 error:
13910 isl_multi_aff_free(ma);
13911 return isl_map_free(map);
13914 /* Compute the preimage of "set" under the function represented by "ma".
13915 * In other words, plug in "ma" in "set". The result is a set
13916 * that lives in the domain space of "ma".
13918 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13919 __isl_take isl_multi_aff *ma)
13921 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13924 /* Compute the preimage of the domain of "map" under the function
13925 * represented by "ma".
13926 * In other words, plug in "ma" in the domain of "map".
13927 * The result is a map that lives in the same space as "map"
13928 * except that the domain has been replaced by the domain space of "ma".
13930 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13931 __isl_take isl_multi_aff *ma)
13933 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13936 /* Compute the preimage of the range of "map" under the function
13937 * represented by "ma".
13938 * In other words, plug in "ma" in the range of "map".
13939 * The result is a map that lives in the same space as "map"
13940 * except that the range has been replaced by the domain space of "ma".
13942 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13943 __isl_take isl_multi_aff *ma)
13945 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13948 /* Compute the preimage of "map" under the function represented by "pma".
13949 * In other words, plug in "pma" in the domain or range of "map".
13950 * The result is a map that lives in the same space as "map",
13951 * except that the space of type "type" has been replaced by
13952 * the domain space of "pma".
13954 * The parameters of "map" and "pma" are assumed to have been aligned.
13956 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13957 __isl_take isl_map *map, enum isl_dim_type type,
13958 __isl_take isl_pw_multi_aff *pma)
13960 int i;
13961 isl_map *res;
13963 if (!pma)
13964 goto error;
13966 if (pma->n == 0) {
13967 isl_pw_multi_aff_free(pma);
13968 res = isl_map_empty(isl_map_get_space(map));
13969 isl_map_free(map);
13970 return res;
13973 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13974 isl_multi_aff_copy(pma->p[0].maff));
13975 if (type == isl_dim_in)
13976 res = isl_map_intersect_domain(res,
13977 isl_map_copy(pma->p[0].set));
13978 else
13979 res = isl_map_intersect_range(res,
13980 isl_map_copy(pma->p[0].set));
13982 for (i = 1; i < pma->n; ++i) {
13983 isl_map *res_i;
13985 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13986 isl_multi_aff_copy(pma->p[i].maff));
13987 if (type == isl_dim_in)
13988 res_i = isl_map_intersect_domain(res_i,
13989 isl_map_copy(pma->p[i].set));
13990 else
13991 res_i = isl_map_intersect_range(res_i,
13992 isl_map_copy(pma->p[i].set));
13993 res = isl_map_union(res, res_i);
13996 isl_pw_multi_aff_free(pma);
13997 isl_map_free(map);
13998 return res;
13999 error:
14000 isl_pw_multi_aff_free(pma);
14001 isl_map_free(map);
14002 return NULL;
14005 /* Compute the preimage of "map" under the function represented by "pma".
14006 * In other words, plug in "pma" in the domain or range of "map".
14007 * The result is a map that lives in the same space as "map",
14008 * except that the space of type "type" has been replaced by
14009 * the domain space of "pma".
14011 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14012 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14014 isl_bool aligned;
14016 if (!map || !pma)
14017 goto error;
14019 aligned = isl_map_space_has_equal_params(map, pma->dim);
14020 if (aligned < 0)
14021 goto error;
14022 if (aligned)
14023 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14025 if (isl_map_check_named_params(map) < 0)
14026 goto error;
14027 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14028 goto error;
14029 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14030 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14032 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14033 error:
14034 isl_pw_multi_aff_free(pma);
14035 return isl_map_free(map);
14038 /* Compute the preimage of "set" under the function represented by "pma".
14039 * In other words, plug in "pma" in "set". The result is a set
14040 * that lives in the domain space of "pma".
14042 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14043 __isl_take isl_pw_multi_aff *pma)
14045 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14048 /* Compute the preimage of the domain of "map" under the function
14049 * represented by "pma".
14050 * In other words, plug in "pma" in the domain of "map".
14051 * The result is a map that lives in the same space as "map",
14052 * except that domain space has been replaced by the domain space of "pma".
14054 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14055 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14057 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14060 /* Compute the preimage of the range of "map" under the function
14061 * represented by "pma".
14062 * In other words, plug in "pma" in the range of "map".
14063 * The result is a map that lives in the same space as "map",
14064 * except that range space has been replaced by the domain space of "pma".
14066 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14067 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14069 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14072 /* Compute the preimage of "map" under the function represented by "mpa".
14073 * In other words, plug in "mpa" in the domain or range of "map".
14074 * The result is a map that lives in the same space as "map",
14075 * except that the space of type "type" has been replaced by
14076 * the domain space of "mpa".
14078 * If the map does not involve any constraints that refer to the
14079 * dimensions of the substituted space, then the only possible
14080 * effect of "mpa" on the map is to map the space to a different space.
14081 * We create a separate isl_multi_aff to effectuate this change
14082 * in order to avoid spurious splitting of the map along the pieces
14083 * of "mpa".
14084 * If "mpa" has a non-trivial explicit domain, however,
14085 * then the full substitution should be performed.
14087 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14088 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14090 isl_size n;
14091 isl_bool full;
14092 isl_pw_multi_aff *pma;
14094 n = isl_map_dim(map, type);
14095 if (n < 0 || !mpa)
14096 goto error;
14098 full = isl_map_involves_dims(map, type, 0, n);
14099 if (full >= 0 && !full)
14100 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14101 if (full < 0)
14102 goto error;
14103 if (!full) {
14104 isl_space *space;
14105 isl_multi_aff *ma;
14107 space = isl_multi_pw_aff_get_space(mpa);
14108 isl_multi_pw_aff_free(mpa);
14109 ma = isl_multi_aff_zero(space);
14110 return isl_map_preimage_multi_aff(map, type, ma);
14113 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14114 return isl_map_preimage_pw_multi_aff(map, type, pma);
14115 error:
14116 isl_map_free(map);
14117 isl_multi_pw_aff_free(mpa);
14118 return NULL;
14121 /* Compute the preimage of "map" under the function represented by "mpa".
14122 * In other words, plug in "mpa" in the domain "map".
14123 * The result is a map that lives in the same space as "map",
14124 * except that domain space has been replaced by the domain space of "mpa".
14126 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14127 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14129 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14132 /* Compute the preimage of "set" by the function represented by "mpa".
14133 * In other words, plug in "mpa" in "set".
14135 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14136 __isl_take isl_multi_pw_aff *mpa)
14138 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14141 /* Return a copy of the equality constraints of "bset" as a matrix.
14143 __isl_give isl_mat *isl_basic_set_extract_equalities(
14144 __isl_keep isl_basic_set *bset)
14146 isl_ctx *ctx;
14147 isl_size total;
14149 total = isl_basic_set_dim(bset, isl_dim_all);
14150 if (total < 0)
14151 return NULL;
14153 ctx = isl_basic_set_get_ctx(bset);
14154 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14157 /* Are the "n" "coefficients" starting at "first" of the integer division
14158 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14159 * to each other?
14160 * The "coefficient" at position 0 is the denominator.
14161 * The "coefficient" at position 1 is the constant term.
14163 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14164 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14165 unsigned first, unsigned n)
14167 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14168 return isl_bool_error;
14169 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14170 return isl_bool_error;
14171 return isl_seq_eq(bmap1->div[pos1] + first,
14172 bmap2->div[pos2] + first, n);
14175 /* Are the integer division expressions at position "pos1" in "bmap1" and
14176 * "pos2" in "bmap2" equal to each other, except that the constant terms
14177 * are different?
14179 isl_bool isl_basic_map_equal_div_expr_except_constant(
14180 __isl_keep isl_basic_map *bmap1, int pos1,
14181 __isl_keep isl_basic_map *bmap2, int pos2)
14183 isl_bool equal;
14184 isl_size total, total2;
14186 total = isl_basic_map_dim(bmap1, isl_dim_all);
14187 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14188 if (total < 0 || total2 < 0)
14189 return isl_bool_error;
14190 if (total != total2)
14191 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14192 "incomparable div expressions", return isl_bool_error);
14193 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14194 0, 1);
14195 if (equal < 0 || !equal)
14196 return equal;
14197 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14198 1, 1);
14199 if (equal < 0 || equal)
14200 return isl_bool_not(equal);
14201 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14202 2, total);
14205 /* Replace the numerator of the constant term of the integer division
14206 * expression at position "div" in "bmap" by "value".
14207 * The caller guarantees that this does not change the meaning
14208 * of the input.
14210 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14211 __isl_take isl_basic_map *bmap, int div, int value)
14213 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14214 return isl_basic_map_free(bmap);
14216 isl_int_set_si(bmap->div[div][1], value);
14218 return bmap;
14221 /* Is the point "inner" internal to inequality constraint "ineq"
14222 * of "bset"?
14223 * The point is considered to be internal to the inequality constraint,
14224 * if it strictly lies on the positive side of the inequality constraint,
14225 * or if it lies on the constraint and the constraint is lexico-positive.
14227 static isl_bool is_internal(__isl_keep isl_vec *inner,
14228 __isl_keep isl_basic_set *bset, int ineq)
14230 isl_ctx *ctx;
14231 int pos;
14232 isl_size total;
14234 if (!inner || !bset)
14235 return isl_bool_error;
14237 ctx = isl_basic_set_get_ctx(bset);
14238 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14239 &ctx->normalize_gcd);
14240 if (!isl_int_is_zero(ctx->normalize_gcd))
14241 return isl_int_is_nonneg(ctx->normalize_gcd);
14243 total = isl_basic_set_dim(bset, isl_dim_all);
14244 if (total < 0)
14245 return isl_bool_error;
14246 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14247 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14250 /* Tighten the inequality constraints of "bset" that are outward with respect
14251 * to the point "vec".
14252 * That is, tighten the constraints that are not satisfied by "vec".
14254 * "vec" is a point internal to some superset S of "bset" that is used
14255 * to make the subsets of S disjoint, by tightening one half of the constraints
14256 * that separate two subsets. In particular, the constraints of S
14257 * are all satisfied by "vec" and should not be tightened.
14258 * Of the internal constraints, those that have "vec" on the outside
14259 * are tightened. The shared facet is included in the adjacent subset
14260 * with the opposite constraint.
14261 * For constraints that saturate "vec", this criterion cannot be used
14262 * to determine which of the two sides should be tightened.
14263 * Instead, the sign of the first non-zero coefficient is used
14264 * to make this choice. Note that this second criterion is never used
14265 * on the constraints of S since "vec" is interior to "S".
14267 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14268 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14270 int j;
14272 bset = isl_basic_set_cow(bset);
14273 if (!bset)
14274 return NULL;
14275 for (j = 0; j < bset->n_ineq; ++j) {
14276 isl_bool internal;
14278 internal = is_internal(vec, bset, j);
14279 if (internal < 0)
14280 return isl_basic_set_free(bset);
14281 if (internal)
14282 continue;
14283 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14286 return bset;
14289 /* Replace the variables x of type "type" starting at "first" in "bmap"
14290 * by x' with x = M x' with M the matrix trans.
14291 * That is, replace the corresponding coefficients c by c M.
14293 * The transformation matrix should be a square matrix.
14295 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14296 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14297 __isl_take isl_mat *trans)
14299 unsigned pos;
14301 bmap = isl_basic_map_cow(bmap);
14302 if (!bmap || !trans)
14303 goto error;
14305 if (trans->n_row != trans->n_col)
14306 isl_die(trans->ctx, isl_error_invalid,
14307 "expecting square transformation matrix", goto error);
14308 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14309 goto error;
14311 pos = isl_basic_map_offset(bmap, type) + first;
14313 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14314 isl_mat_copy(trans)) < 0)
14315 goto error;
14316 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14317 isl_mat_copy(trans)) < 0)
14318 goto error;
14319 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14320 isl_mat_copy(trans)) < 0)
14321 goto error;
14323 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14324 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14326 isl_mat_free(trans);
14327 return bmap;
14328 error:
14329 isl_mat_free(trans);
14330 isl_basic_map_free(bmap);
14331 return NULL;
14334 /* Replace the variables x of type "type" starting at "first" in "bset"
14335 * by x' with x = M x' with M the matrix trans.
14336 * That is, replace the corresponding coefficients c by c M.
14338 * The transformation matrix should be a square matrix.
14340 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14341 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14342 __isl_take isl_mat *trans)
14344 return isl_basic_map_transform_dims(bset, type, first, trans);