isl_map.c: room_for_ineq: add memory management annotation
[isl.git] / isl_map.c
blobe6dceded09e001aea7e33dff5280d71d3753beee
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 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
292 __isl_keep isl_set *set)
294 isl_bool m;
295 if (!map || !set)
296 return isl_bool_error;
297 m = isl_map_has_equal_params(map, set_to_map(set));
298 if (m < 0 || !m)
299 return m;
300 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
301 set->dim, isl_dim_set);
304 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
305 __isl_keep isl_basic_set *bset)
307 isl_bool m;
308 if (!bmap || !bset)
309 return isl_bool_error;
310 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
311 if (m < 0 || !m)
312 return m;
313 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
314 bset->dim, isl_dim_set);
317 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
318 __isl_keep isl_set *set)
320 isl_bool m;
321 if (!map || !set)
322 return isl_bool_error;
323 m = isl_map_has_equal_params(map, set_to_map(set));
324 if (m < 0 || !m)
325 return m;
326 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
327 set->dim, isl_dim_set);
330 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
331 __isl_keep isl_basic_set *bset)
333 isl_bool m;
334 if (!bmap || !bset)
335 return isl_bool_error;
336 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
337 if (m < 0 || !m)
338 return m;
339 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
340 bset->dim, isl_dim_set);
343 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
345 return bmap ? bmap->ctx : NULL;
348 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
350 return bset ? bset->ctx : NULL;
353 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
355 return map ? map->ctx : NULL;
358 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
360 return set ? set->ctx : NULL;
363 /* Return the space of "bmap".
365 __isl_keep isl_space *isl_basic_map_peek_space(
366 __isl_keep const isl_basic_map *bmap)
368 return bmap ? bmap->dim : NULL;
371 /* Return the space of "bset".
373 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
375 return isl_basic_map_peek_space(bset_to_bmap(bset));
378 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
380 return isl_space_copy(isl_basic_map_peek_space(bmap));
383 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
385 return isl_basic_map_get_space(bset_to_bmap(bset));
388 /* Return the space of "bmap".
389 * This may be either a copy or the space itself
390 * if there is only one reference to "bmap".
391 * This allows the space to be modified inplace
392 * if both the basic map and its space have only a single reference.
393 * The caller is not allowed to modify "bmap" between this call and
394 * a subsequent call to isl_basic_map_restore_space.
395 * The only exception is that isl_basic_map_free can be called instead.
397 static __isl_give isl_space *isl_basic_map_take_space(
398 __isl_keep isl_basic_map *bmap)
400 isl_space *space;
402 if (!bmap)
403 return NULL;
404 if (bmap->ref != 1)
405 return isl_basic_map_get_space(bmap);
406 space = bmap->dim;
407 bmap->dim = NULL;
408 return space;
411 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
412 * due to a preceding call to isl_basic_map_take_space.
413 * However, in this case, "bmap" only has a single reference and
414 * then the call to isl_basic_map_cow has no effect.
416 static __isl_give isl_basic_map *isl_basic_map_restore_space(
417 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
419 if (!bmap || !space)
420 goto error;
422 if (bmap->dim == space) {
423 isl_space_free(space);
424 return bmap;
427 bmap = isl_basic_map_cow(bmap);
428 if (!bmap)
429 goto error;
430 isl_space_free(bmap->dim);
431 bmap->dim = space;
433 return bmap;
434 error:
435 isl_basic_map_free(bmap);
436 isl_space_free(space);
437 return NULL;
440 /* Extract the divs in "bmap" as a matrix.
442 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
444 int i;
445 isl_ctx *ctx;
446 isl_mat *div;
447 isl_size v_div;
448 unsigned cols;
450 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
451 if (v_div < 0)
452 return NULL;
454 ctx = isl_basic_map_get_ctx(bmap);
455 cols = 1 + 1 + v_div + bmap->n_div;
456 div = isl_mat_alloc(ctx, bmap->n_div, cols);
457 if (!div)
458 return NULL;
460 for (i = 0; i < bmap->n_div; ++i)
461 isl_seq_cpy(div->row[i], bmap->div[i], cols);
463 return div;
466 /* Extract the divs in "bset" as a matrix.
468 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
470 return isl_basic_map_get_divs(bset);
473 __isl_give isl_local_space *isl_basic_map_get_local_space(
474 __isl_keep isl_basic_map *bmap)
476 isl_mat *div;
478 if (!bmap)
479 return NULL;
481 div = isl_basic_map_get_divs(bmap);
482 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
485 __isl_give isl_local_space *isl_basic_set_get_local_space(
486 __isl_keep isl_basic_set *bset)
488 return isl_basic_map_get_local_space(bset);
491 /* For each known div d = floor(f/m), add the constraints
493 * f - m d >= 0
494 * -(f-(m-1)) + m d >= 0
496 * Do not finalize the result.
498 static __isl_give isl_basic_map *add_known_div_constraints(
499 __isl_take isl_basic_map *bmap)
501 int i;
502 isl_size n_div;
504 n_div = isl_basic_map_dim(bmap, isl_dim_div);
505 if (n_div < 0)
506 return isl_basic_map_free(bmap);
507 if (n_div == 0)
508 return bmap;
509 bmap = isl_basic_map_cow(bmap);
510 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
511 if (!bmap)
512 return NULL;
513 for (i = 0; i < n_div; ++i) {
514 if (isl_int_is_zero(bmap->div[i][0]))
515 continue;
516 bmap = isl_basic_map_add_div_constraints(bmap, i);
519 return bmap;
522 __isl_give isl_basic_map *isl_basic_map_from_local_space(
523 __isl_take isl_local_space *ls)
525 int i;
526 isl_size n_div;
527 isl_basic_map *bmap;
529 n_div = isl_local_space_dim(ls, isl_dim_div);
530 if (n_div < 0)
531 ls = isl_local_space_free(ls);
532 if (!ls)
533 return NULL;
535 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
536 n_div, 0, 2 * n_div);
538 for (i = 0; i < n_div; ++i)
539 if (isl_basic_map_alloc_div(bmap) < 0)
540 goto error;
542 for (i = 0; i < n_div; ++i)
543 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
544 bmap = add_known_div_constraints(bmap);
546 isl_local_space_free(ls);
547 return bmap;
548 error:
549 isl_local_space_free(ls);
550 isl_basic_map_free(bmap);
551 return NULL;
554 __isl_give isl_basic_set *isl_basic_set_from_local_space(
555 __isl_take isl_local_space *ls)
557 return isl_basic_map_from_local_space(ls);
560 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
562 return isl_space_copy(isl_map_peek_space(map));
565 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
567 if (!set)
568 return NULL;
569 return isl_space_copy(set->dim);
572 /* Return the space of "map".
573 * This may be either a copy or the space itself
574 * if there is only one reference to "map".
575 * This allows the space to be modified inplace
576 * if both the map and its space have only a single reference.
577 * The caller is not allowed to modify "map" between this call and
578 * a subsequent call to isl_map_restore_space.
579 * The only exception is that isl_map_free can be called instead.
581 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
583 isl_space *space;
585 if (!map)
586 return NULL;
587 if (map->ref != 1)
588 return isl_map_get_space(map);
589 space = map->dim;
590 map->dim = NULL;
591 return space;
594 /* Set the space of "map" to "space", where the space of "map" may be missing
595 * due to a preceding call to isl_map_take_space.
596 * However, in this case, "map" only has a single reference and
597 * then the call to isl_map_cow has no effect.
599 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
600 __isl_take isl_space *space)
602 if (!map || !space)
603 goto error;
605 if (map->dim == space) {
606 isl_space_free(space);
607 return map;
610 map = isl_map_cow(map);
611 if (!map)
612 goto error;
613 isl_space_free(map->dim);
614 map->dim = space;
616 return map;
617 error:
618 isl_map_free(map);
619 isl_space_free(space);
620 return NULL;
623 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
624 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
626 isl_space *space;
628 space = isl_basic_map_take_space(bmap);
629 space = isl_space_set_tuple_name(space, type, s);
630 bmap = isl_basic_map_restore_space(bmap, space);
631 bmap = isl_basic_map_finalize(bmap);
632 return bmap;
635 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
636 __isl_take isl_basic_set *bset, const char *s)
638 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
641 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
642 enum isl_dim_type type)
644 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
647 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
648 enum isl_dim_type type, const char *s)
650 int i;
651 isl_space *space;
653 map = isl_map_cow(map);
654 if (!map)
655 return NULL;
657 for (i = 0; i < map->n; ++i) {
658 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
659 if (!map->p[i])
660 goto error;
663 space = isl_map_take_space(map);
664 space = isl_space_set_tuple_name(space, type, s);
665 map = isl_map_restore_space(map, space);
667 return map;
668 error:
669 isl_map_free(map);
670 return NULL;
673 /* Replace the identifier of the tuple of type "type" by "id".
675 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
676 __isl_take isl_basic_map *bmap,
677 enum isl_dim_type type, __isl_take isl_id *id)
679 isl_space *space;
681 space = isl_basic_map_take_space(bmap);
682 space = isl_space_set_tuple_id(space, type, id);
683 bmap = isl_basic_map_restore_space(bmap, space);
684 bmap = isl_basic_map_finalize(bmap);
685 return bmap;
688 /* Replace the identifier of the tuple by "id".
690 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
691 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
693 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
696 /* Does the input or output tuple have a name?
698 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
700 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
703 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
704 enum isl_dim_type type)
706 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
709 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
710 const char *s)
712 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
713 isl_dim_set, s));
716 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
717 enum isl_dim_type type, __isl_take isl_id *id)
719 isl_space *space;
721 space = isl_map_take_space(map);
722 space = isl_space_set_tuple_id(space, type, id);
723 map = isl_map_restore_space(map, space);
725 return isl_map_reset_space(map, isl_map_get_space(map));
728 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
729 __isl_take isl_id *id)
731 return isl_map_set_tuple_id(set, isl_dim_set, id);
734 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
735 enum isl_dim_type type)
737 isl_space *space;
739 space = isl_map_take_space(map);
740 space = isl_space_reset_tuple_id(space, type);
741 map = isl_map_restore_space(map, space);
743 return isl_map_reset_space(map, isl_map_get_space(map));
746 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
748 return isl_map_reset_tuple_id(set, isl_dim_set);
751 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
753 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
756 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
757 enum isl_dim_type type)
759 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
762 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
764 return isl_map_has_tuple_id(set, isl_dim_set);
767 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
769 return isl_map_get_tuple_id(set, isl_dim_set);
772 /* Does the set tuple have a name?
774 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
776 if (!set)
777 return isl_bool_error;
778 return isl_space_has_tuple_name(set->dim, isl_dim_set);
782 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
784 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
787 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
789 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
792 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
793 enum isl_dim_type type, unsigned pos)
795 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
798 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
799 enum isl_dim_type type, unsigned pos)
801 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
804 /* Does the given dimension have a name?
806 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
807 enum isl_dim_type type, unsigned pos)
809 if (!map)
810 return isl_bool_error;
811 return isl_space_has_dim_name(map->dim, type, pos);
814 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
815 enum isl_dim_type type, unsigned pos)
817 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
820 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
821 enum isl_dim_type type, unsigned pos)
823 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
826 /* Does the given dimension have a name?
828 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
829 enum isl_dim_type type, unsigned pos)
831 if (!set)
832 return isl_bool_error;
833 return isl_space_has_dim_name(set->dim, type, pos);
836 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
837 __isl_take isl_basic_map *bmap,
838 enum isl_dim_type type, unsigned pos, const char *s)
840 isl_space *space;
842 space = isl_basic_map_take_space(bmap);
843 space = isl_space_set_dim_name(space, type, pos, s);
844 bmap = isl_basic_map_restore_space(bmap, space);
845 return isl_basic_map_finalize(bmap);
848 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
849 enum isl_dim_type type, unsigned pos, const char *s)
851 int i;
852 isl_space *space;
854 map = isl_map_cow(map);
855 if (!map)
856 return NULL;
858 for (i = 0; i < map->n; ++i) {
859 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
860 if (!map->p[i])
861 goto error;
864 space = isl_map_take_space(map);
865 space = isl_space_set_dim_name(space, type, pos, s);
866 map = isl_map_restore_space(map, space);
868 return map;
869 error:
870 isl_map_free(map);
871 return NULL;
874 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
875 __isl_take isl_basic_set *bset,
876 enum isl_dim_type type, unsigned pos, const char *s)
878 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
879 type, pos, s));
882 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
883 enum isl_dim_type type, unsigned pos, const char *s)
885 return set_from_map(isl_map_set_dim_name(set_to_map(set),
886 type, pos, s));
889 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
890 enum isl_dim_type type, unsigned pos)
892 if (!bmap)
893 return isl_bool_error;
894 return isl_space_has_dim_id(bmap->dim, type, pos);
897 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
898 enum isl_dim_type type, unsigned pos)
900 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
903 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
904 enum isl_dim_type type, unsigned pos)
906 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
909 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
910 enum isl_dim_type type, unsigned pos)
912 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
915 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
916 enum isl_dim_type type, unsigned pos)
918 return isl_map_has_dim_id(set, type, pos);
921 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
922 enum isl_dim_type type, unsigned pos)
924 return isl_map_get_dim_id(set, type, pos);
927 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
928 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
930 isl_space *space;
932 space = isl_map_take_space(map);
933 space = isl_space_set_dim_id(space, type, pos, id);
934 map = isl_map_restore_space(map, space);
936 return isl_map_reset_space(map, isl_map_get_space(map));
939 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
940 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
942 return isl_map_set_dim_id(set, type, pos, id);
945 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
946 __isl_keep isl_id *id)
948 if (!map)
949 return -1;
950 return isl_space_find_dim_by_id(map->dim, type, id);
953 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
954 __isl_keep isl_id *id)
956 return isl_map_find_dim_by_id(set, type, id);
959 /* Return the position of the dimension of the given type and name
960 * in "bmap".
961 * Return -1 if no such dimension can be found.
963 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
964 enum isl_dim_type type, const char *name)
966 if (!bmap)
967 return -1;
968 return isl_space_find_dim_by_name(bmap->dim, type, name);
971 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
972 const char *name)
974 if (!map)
975 return -1;
976 return isl_space_find_dim_by_name(map->dim, type, name);
979 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
980 const char *name)
982 return isl_map_find_dim_by_name(set, type, name);
985 /* Check whether equality i of bset is a pure stride constraint
986 * on a single dimension, i.e., of the form
988 * v = k e
990 * with k a constant and e an existentially quantified variable.
992 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
994 isl_size nparam;
995 isl_size d;
996 isl_size n_div;
997 int pos1;
998 int pos2;
1000 nparam = isl_basic_set_dim(bset, isl_dim_param);
1001 d = isl_basic_set_dim(bset, isl_dim_set);
1002 n_div = isl_basic_set_dim(bset, isl_dim_div);
1003 if (nparam < 0 || d < 0 || n_div < 0)
1004 return isl_bool_error;
1006 if (!isl_int_is_zero(bset->eq[i][0]))
1007 return isl_bool_false;
1009 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1010 return isl_bool_false;
1011 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1012 if (pos1 == -1)
1013 return isl_bool_false;
1014 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1015 d - pos1 - 1) != -1)
1016 return isl_bool_false;
1018 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1019 if (pos2 == -1)
1020 return isl_bool_false;
1021 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1022 n_div - pos2 - 1) != -1)
1023 return isl_bool_false;
1024 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1025 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1026 return isl_bool_false;
1028 return isl_bool_true;
1031 /* Reset the user pointer on all identifiers of parameters and tuples
1032 * of the space of "map".
1034 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1036 isl_space *space;
1038 space = isl_map_get_space(map);
1039 space = isl_space_reset_user(space);
1040 map = isl_map_reset_space(map, space);
1042 return map;
1045 /* Reset the user pointer on all identifiers of parameters and tuples
1046 * of the space of "set".
1048 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1050 return isl_map_reset_user(set);
1053 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1055 if (!bmap)
1056 return isl_bool_error;
1057 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1060 /* Has "map" been marked as a rational map?
1061 * In particular, have all basic maps in "map" been marked this way?
1062 * An empty map is not considered to be rational.
1063 * Maps where only some of the basic maps are marked rational
1064 * are not allowed.
1066 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1068 int i;
1069 isl_bool rational;
1071 if (!map)
1072 return isl_bool_error;
1073 if (map->n == 0)
1074 return isl_bool_false;
1075 rational = isl_basic_map_is_rational(map->p[0]);
1076 if (rational < 0)
1077 return rational;
1078 for (i = 1; i < map->n; ++i) {
1079 isl_bool rational_i;
1081 rational_i = isl_basic_map_is_rational(map->p[i]);
1082 if (rational_i < 0)
1083 return rational_i;
1084 if (rational != rational_i)
1085 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1086 "mixed rational and integer basic maps "
1087 "not supported", return isl_bool_error);
1090 return rational;
1093 /* Has "set" been marked as a rational set?
1094 * In particular, have all basic set in "set" been marked this way?
1095 * An empty set is not considered to be rational.
1096 * Sets where only some of the basic sets are marked rational
1097 * are not allowed.
1099 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1101 return isl_map_is_rational(set);
1104 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1106 return isl_basic_map_is_rational(bset);
1109 /* Does "bmap" contain any rational points?
1111 * If "bmap" has an equality for each dimension, equating the dimension
1112 * to an integer constant, then it has no rational points, even if it
1113 * is marked as rational.
1115 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1117 isl_bool has_rational = isl_bool_true;
1118 isl_size total;
1120 if (!bmap)
1121 return isl_bool_error;
1122 if (isl_basic_map_plain_is_empty(bmap))
1123 return isl_bool_false;
1124 if (!isl_basic_map_is_rational(bmap))
1125 return isl_bool_false;
1126 bmap = isl_basic_map_copy(bmap);
1127 bmap = isl_basic_map_implicit_equalities(bmap);
1128 total = isl_basic_map_dim(bmap, isl_dim_all);
1129 if (total < 0)
1130 return isl_bool_error;
1131 if (bmap->n_eq == total) {
1132 int i, j;
1133 for (i = 0; i < bmap->n_eq; ++i) {
1134 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1135 if (j < 0)
1136 break;
1137 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1138 !isl_int_is_negone(bmap->eq[i][1 + j]))
1139 break;
1140 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1141 total - j - 1);
1142 if (j >= 0)
1143 break;
1145 if (i == bmap->n_eq)
1146 has_rational = isl_bool_false;
1148 isl_basic_map_free(bmap);
1150 return has_rational;
1153 /* Does "map" contain any rational points?
1155 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1157 int i;
1158 isl_bool has_rational;
1160 if (!map)
1161 return isl_bool_error;
1162 for (i = 0; i < map->n; ++i) {
1163 has_rational = isl_basic_map_has_rational(map->p[i]);
1164 if (has_rational < 0 || has_rational)
1165 return has_rational;
1167 return isl_bool_false;
1170 /* Does "set" contain any rational points?
1172 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1174 return isl_map_has_rational(set);
1177 /* Is this basic set a parameter domain?
1179 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1181 if (!bset)
1182 return isl_bool_error;
1183 return isl_space_is_params(bset->dim);
1186 /* Is this set a parameter domain?
1188 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1190 if (!set)
1191 return isl_bool_error;
1192 return isl_space_is_params(set->dim);
1195 /* Is this map actually a parameter domain?
1196 * Users should never call this function. Outside of isl,
1197 * a map can never be a parameter domain.
1199 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1201 if (!map)
1202 return isl_bool_error;
1203 return isl_space_is_params(map->dim);
1206 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1207 __isl_take isl_basic_map *bmap, unsigned extra,
1208 unsigned n_eq, unsigned n_ineq)
1210 int i;
1211 isl_space *space = isl_basic_map_peek_space(bmap);
1212 isl_size n_var = isl_space_dim(space, isl_dim_all);
1213 size_t row_size = 1 + n_var + extra;
1215 bmap->ctx = ctx;
1216 isl_ctx_ref(ctx);
1218 if (n_var < 0)
1219 return isl_basic_map_free(bmap);
1221 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1222 if (isl_blk_is_error(bmap->block))
1223 goto error;
1225 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1226 if ((n_ineq + n_eq) && !bmap->ineq)
1227 goto error;
1229 if (extra == 0) {
1230 bmap->block2 = isl_blk_empty();
1231 bmap->div = NULL;
1232 } else {
1233 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1234 if (isl_blk_is_error(bmap->block2))
1235 goto error;
1237 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1238 if (!bmap->div)
1239 goto error;
1242 for (i = 0; i < n_ineq + n_eq; ++i)
1243 bmap->ineq[i] = bmap->block.data + i * row_size;
1245 for (i = 0; i < extra; ++i)
1246 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1248 bmap->ref = 1;
1249 bmap->flags = 0;
1250 bmap->c_size = n_eq + n_ineq;
1251 bmap->eq = bmap->ineq + n_ineq;
1252 bmap->extra = extra;
1253 bmap->n_eq = 0;
1254 bmap->n_ineq = 0;
1255 bmap->n_div = 0;
1256 bmap->sample = NULL;
1258 return bmap;
1259 error:
1260 isl_basic_map_free(bmap);
1261 return NULL;
1264 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1265 unsigned nparam, unsigned dim, unsigned extra,
1266 unsigned n_eq, unsigned n_ineq)
1268 struct isl_basic_map *bmap;
1269 isl_space *space;
1271 space = isl_space_set_alloc(ctx, nparam, dim);
1272 if (!space)
1273 return NULL;
1275 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1276 return bset_from_bmap(bmap);
1279 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1280 unsigned extra, unsigned n_eq, unsigned n_ineq)
1282 struct isl_basic_map *bmap;
1283 if (!space)
1284 return NULL;
1285 isl_assert(space->ctx, space->n_in == 0, goto error);
1286 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1287 return bset_from_bmap(bmap);
1288 error:
1289 isl_space_free(space);
1290 return NULL;
1293 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1294 unsigned extra, unsigned n_eq, unsigned n_ineq)
1296 struct isl_basic_map *bmap;
1298 if (!space)
1299 return NULL;
1300 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1301 if (!bmap)
1302 goto error;
1303 bmap->dim = space;
1305 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1306 error:
1307 isl_space_free(space);
1308 return NULL;
1311 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1312 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1313 unsigned n_eq, unsigned n_ineq)
1315 struct isl_basic_map *bmap;
1316 isl_space *space;
1318 space = isl_space_alloc(ctx, nparam, in, out);
1319 if (!space)
1320 return NULL;
1322 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1323 return bmap;
1326 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1327 __isl_keep isl_basic_map *src)
1329 int i;
1330 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1332 if (!dst || total < 0)
1333 return isl_basic_map_free(dst);
1335 for (i = 0; i < src->n_eq; ++i) {
1336 int j = isl_basic_map_alloc_equality(dst);
1337 if (j < 0)
1338 return isl_basic_map_free(dst);
1339 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1342 for (i = 0; i < src->n_ineq; ++i) {
1343 int j = isl_basic_map_alloc_inequality(dst);
1344 if (j < 0)
1345 return isl_basic_map_free(dst);
1346 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1349 for (i = 0; i < src->n_div; ++i) {
1350 int j = isl_basic_map_alloc_div(dst);
1351 if (j < 0)
1352 return isl_basic_map_free(dst);
1353 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1355 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1356 return dst;
1359 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1361 struct isl_basic_map *dup;
1363 if (!bmap)
1364 return NULL;
1365 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1366 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1367 dup = dup_constraints(dup, bmap);
1368 if (!dup)
1369 return NULL;
1370 dup->flags = bmap->flags;
1371 dup->sample = isl_vec_copy(bmap->sample);
1372 return dup;
1375 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1377 struct isl_basic_map *dup;
1379 dup = isl_basic_map_dup(bset_to_bmap(bset));
1380 return bset_from_bmap(dup);
1383 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1385 if (!bset)
1386 return NULL;
1388 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1389 bset->ref++;
1390 return bset;
1392 return isl_basic_set_dup(bset);
1395 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1397 if (!set)
1398 return NULL;
1400 set->ref++;
1401 return set;
1404 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1406 if (!bmap)
1407 return NULL;
1409 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1410 bmap->ref++;
1411 return bmap;
1413 bmap = isl_basic_map_dup(bmap);
1414 if (bmap)
1415 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1416 return bmap;
1419 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1421 if (!map)
1422 return NULL;
1424 map->ref++;
1425 return map;
1428 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1430 if (!bmap)
1431 return NULL;
1433 if (--bmap->ref > 0)
1434 return NULL;
1436 isl_ctx_deref(bmap->ctx);
1437 free(bmap->div);
1438 isl_blk_free(bmap->ctx, bmap->block2);
1439 free(bmap->ineq);
1440 isl_blk_free(bmap->ctx, bmap->block);
1441 isl_vec_free(bmap->sample);
1442 isl_space_free(bmap->dim);
1443 free(bmap);
1445 return NULL;
1448 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1450 return isl_basic_map_free(bset_to_bmap(bset));
1453 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1455 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1458 /* Check that "bset" does not involve any parameters.
1460 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1462 isl_size nparam;
1464 nparam = isl_basic_set_dim(bset, isl_dim_param);
1465 if (nparam < 0)
1466 return isl_stat_error;
1467 if (nparam != 0)
1468 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1469 "basic set should not have any parameters",
1470 return isl_stat_error);
1471 return isl_stat_ok;
1474 /* Check that "bset" does not involve any local variables.
1476 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1478 isl_size n_div;
1480 n_div = isl_basic_set_dim(bset, isl_dim_div);
1481 if (n_div < 0)
1482 return isl_stat_error;
1483 if (n_div != 0)
1484 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1485 "basic set should not have any local variables",
1486 return isl_stat_error);
1487 return isl_stat_ok;
1490 #undef TYPE
1491 #define TYPE isl_map
1493 #include "isl_check_named_params_templ.c"
1495 #undef TYPE
1496 #define TYPE isl_basic_map
1498 static
1499 #include "isl_check_named_params_templ.c"
1501 /* Check that "bmap1" and "bmap2" have the same parameters,
1502 * reporting an error if they do not.
1504 static isl_stat isl_basic_map_check_equal_params(
1505 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1507 isl_bool match;
1509 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1510 if (match < 0)
1511 return isl_stat_error;
1512 if (!match)
1513 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1514 "parameters don't match", return isl_stat_error);
1515 return isl_stat_ok;
1518 #undef TYPE
1519 #define TYPE isl_map
1521 #include "isl_align_params_bin_templ.c"
1523 #undef SUFFIX
1524 #define SUFFIX set
1525 #undef ARG1
1526 #define ARG1 isl_map
1527 #undef ARG2
1528 #define ARG2 isl_set
1530 #include "isl_align_params_templ.c"
1532 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1533 __isl_keep isl_map *map2,
1534 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1536 isl_bool r;
1538 if (!map1 || !map2)
1539 return isl_bool_error;
1540 if (isl_map_has_equal_params(map1, map2))
1541 return fn(map1, map2);
1542 if (isl_map_check_named_params(map1) < 0)
1543 return isl_bool_error;
1544 if (isl_map_check_named_params(map2) < 0)
1545 return isl_bool_error;
1546 map1 = isl_map_copy(map1);
1547 map2 = isl_map_copy(map2);
1548 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1549 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1550 r = fn(map1, map2);
1551 isl_map_free(map1);
1552 isl_map_free(map2);
1553 return r;
1556 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1558 isl_size total;
1559 struct isl_ctx *ctx;
1561 total = isl_basic_map_dim(bmap, isl_dim_all);
1562 if (total < 0)
1563 return -1;
1564 ctx = bmap->ctx;
1565 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1566 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1567 return -1);
1568 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1569 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1570 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1571 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1572 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1573 isl_int *t;
1574 int j = isl_basic_map_alloc_inequality(bmap);
1575 if (j < 0)
1576 return -1;
1577 t = bmap->ineq[j];
1578 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1579 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1580 bmap->eq[-1] = t;
1581 bmap->n_eq++;
1582 bmap->n_ineq--;
1583 bmap->eq--;
1584 return 0;
1586 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1587 bmap->extra - bmap->n_div);
1588 return bmap->n_eq++;
1591 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1593 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1596 __isl_give isl_basic_map *isl_basic_map_free_equality(
1597 __isl_take isl_basic_map *bmap, unsigned n)
1599 if (!bmap)
1600 return NULL;
1601 if (n > bmap->n_eq)
1602 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1603 "invalid number of equalities",
1604 isl_basic_map_free(bmap));
1605 bmap->n_eq -= n;
1606 return bmap;
1609 __isl_give isl_basic_set *isl_basic_set_free_equality(
1610 __isl_take isl_basic_set *bset, unsigned n)
1612 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1613 n));
1616 /* Drop the equality constraint at position "pos",
1617 * preserving the order of the other equality constraints.
1619 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1621 isl_int *t;
1622 int r;
1624 if (!bmap)
1625 return -1;
1626 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1628 t = bmap->eq[pos];
1629 bmap->n_eq--;
1630 for (r = pos; r < bmap->n_eq; ++r)
1631 bmap->eq[r] = bmap->eq[r + 1];
1632 bmap->eq[bmap->n_eq] = t;
1634 return 0;
1637 /* Turn inequality "pos" of "bmap" into an equality.
1639 * In particular, we move the inequality in front of the equalities
1640 * and move the last inequality in the position of the moved inequality.
1641 * Note that isl_tab_make_equalities_explicit depends on this particular
1642 * change in the ordering of the constraints.
1644 void isl_basic_map_inequality_to_equality(
1645 __isl_keep isl_basic_map *bmap, unsigned pos)
1647 isl_int *t;
1649 t = bmap->ineq[pos];
1650 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1651 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1652 bmap->eq[-1] = t;
1653 bmap->n_eq++;
1654 bmap->n_ineq--;
1655 bmap->eq--;
1656 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1657 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1658 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1659 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1662 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1664 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1667 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1669 isl_size total;
1670 struct isl_ctx *ctx;
1672 total = isl_basic_map_dim(bmap, isl_dim_all);
1673 if (total < 0)
1674 return -1;
1675 ctx = bmap->ctx;
1676 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1677 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1678 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1679 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1680 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1681 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1682 bmap->extra - bmap->n_div);
1683 return bmap->n_ineq++;
1686 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1688 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1691 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1692 __isl_take isl_basic_map *bmap, unsigned n)
1694 if (!bmap)
1695 return NULL;
1696 if (n > bmap->n_ineq)
1697 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1698 "invalid number of inequalities",
1699 return isl_basic_map_free(bmap));
1700 bmap->n_ineq -= n;
1701 return bmap;
1704 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1705 __isl_take isl_basic_set *bset, unsigned n)
1707 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1708 n));
1711 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1713 isl_int *t;
1714 if (!bmap)
1715 return -1;
1716 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1718 if (pos != bmap->n_ineq - 1) {
1719 t = bmap->ineq[pos];
1720 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1721 bmap->ineq[bmap->n_ineq - 1] = t;
1722 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1724 bmap->n_ineq--;
1725 return 0;
1728 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1730 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1733 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1734 isl_int *eq)
1736 isl_bool empty;
1737 isl_size total;
1738 int k;
1740 empty = isl_basic_map_plain_is_empty(bmap);
1741 if (empty < 0)
1742 return isl_basic_map_free(bmap);
1743 if (empty)
1744 return bmap;
1746 bmap = isl_basic_map_cow(bmap);
1747 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1748 total = isl_basic_map_dim(bmap, isl_dim_all);
1749 if (total < 0)
1750 return isl_basic_map_free(bmap);
1751 k = isl_basic_map_alloc_equality(bmap);
1752 if (k < 0)
1753 goto error;
1754 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1755 return bmap;
1756 error:
1757 isl_basic_map_free(bmap);
1758 return NULL;
1761 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1762 isl_int *eq)
1764 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1767 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1768 isl_int *ineq)
1770 isl_size total;
1771 int k;
1773 bmap = isl_basic_map_cow(bmap);
1774 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1775 total = isl_basic_map_dim(bmap, isl_dim_all);
1776 if (total < 0)
1777 return isl_basic_map_free(bmap);
1778 k = isl_basic_map_alloc_inequality(bmap);
1779 if (k < 0)
1780 goto error;
1781 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1782 return bmap;
1783 error:
1784 isl_basic_map_free(bmap);
1785 return NULL;
1788 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1789 isl_int *ineq)
1791 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1794 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1796 isl_size total;
1798 total = isl_basic_map_dim(bmap, isl_dim_all);
1799 if (total < 0)
1800 return -1;
1801 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1802 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1803 bmap->extra - bmap->n_div);
1804 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1805 return bmap->n_div++;
1808 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1810 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1813 #undef TYPE
1814 #define TYPE isl_basic_map
1815 #include "check_type_range_templ.c"
1817 /* Check that there are "n" dimensions of type "type" starting at "first"
1818 * in "bset".
1820 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1821 enum isl_dim_type type, unsigned first, unsigned n)
1823 return isl_basic_map_check_range(bset_to_bmap(bset),
1824 type, first, n);
1827 /* Insert an extra integer division, prescribed by "div", to "bmap"
1828 * at (integer division) position "pos".
1830 * The integer division is first added at the end and then moved
1831 * into the right position.
1833 __isl_give isl_basic_map *isl_basic_map_insert_div(
1834 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1836 int i, k;
1837 isl_size total;
1839 bmap = isl_basic_map_cow(bmap);
1840 total = isl_basic_map_dim(bmap, isl_dim_all);
1841 if (total < 0 || !div)
1842 return isl_basic_map_free(bmap);
1844 if (div->size != 1 + 1 + total)
1845 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1846 "unexpected size", return isl_basic_map_free(bmap));
1847 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1848 return isl_basic_map_free(bmap);
1850 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1851 k = isl_basic_map_alloc_div(bmap);
1852 if (k < 0)
1853 return isl_basic_map_free(bmap);
1854 isl_seq_cpy(bmap->div[k], div->el, div->size);
1855 isl_int_set_si(bmap->div[k][div->size], 0);
1857 for (i = k; i > pos; --i)
1858 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1860 return bmap;
1863 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1865 if (!bmap)
1866 return isl_stat_error;
1867 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1868 bmap->n_div -= n;
1869 return isl_stat_ok;
1872 static __isl_give isl_basic_map *add_constraints(
1873 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1874 unsigned i_pos, unsigned o_pos)
1876 isl_size total, n_param, n_in, n_out, n_div;
1877 unsigned o_in, o_out;
1878 isl_ctx *ctx;
1879 isl_space *space;
1880 struct isl_dim_map *dim_map;
1882 space = isl_basic_map_peek_space(bmap2);
1883 if (!bmap1 || !space)
1884 goto error;
1886 total = isl_basic_map_dim(bmap1, isl_dim_all);
1887 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1888 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1889 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1890 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1891 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1892 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1893 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1894 goto error;
1895 ctx = isl_basic_map_get_ctx(bmap1);
1896 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1897 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1898 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1899 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1900 isl_dim_map_div(dim_map, bmap2, total);
1902 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1903 error:
1904 isl_basic_map_free(bmap1);
1905 isl_basic_map_free(bmap2);
1906 return NULL;
1909 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1910 unsigned extra, unsigned n_eq, unsigned n_ineq)
1912 isl_space *space;
1913 struct isl_basic_map *ext;
1914 unsigned flags;
1915 int dims_ok;
1917 if (!base)
1918 goto error;
1920 dims_ok = base->extra >= base->n_div + extra;
1922 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1923 room_for_ineq(base, n_ineq))
1924 return base;
1926 extra += base->extra;
1927 n_eq += base->n_eq;
1928 n_ineq += base->n_ineq;
1930 space = isl_basic_map_get_space(base);
1931 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1932 if (!ext)
1933 goto error;
1935 if (dims_ok)
1936 ext->sample = isl_vec_copy(base->sample);
1937 flags = base->flags;
1938 ext = add_constraints(ext, base, 0, 0);
1939 if (ext) {
1940 ext->flags = flags;
1941 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1944 return ext;
1946 error:
1947 isl_basic_map_free(base);
1948 return NULL;
1951 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
1952 unsigned extra, unsigned n_eq, unsigned n_ineq)
1954 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1955 extra, n_eq, n_ineq));
1958 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
1959 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1961 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
1964 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
1965 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1967 isl_basic_map *bmap = bset_to_bmap(base);
1968 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1969 return bset_from_bmap(bmap);
1972 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1974 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1977 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1979 if (!bmap)
1980 return NULL;
1982 if (bmap->ref > 1) {
1983 bmap->ref--;
1984 bmap = isl_basic_map_dup(bmap);
1986 if (bmap) {
1987 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1988 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1990 return bmap;
1993 /* Clear all cached information in "map", either because it is about
1994 * to be modified or because it is being freed.
1995 * Always return the same pointer that is passed in.
1996 * This is needed for the use in isl_map_free.
1998 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2000 isl_basic_map_free(map->cached_simple_hull[0]);
2001 isl_basic_map_free(map->cached_simple_hull[1]);
2002 map->cached_simple_hull[0] = NULL;
2003 map->cached_simple_hull[1] = NULL;
2004 return map;
2007 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2009 return isl_map_cow(set);
2012 /* Return an isl_map that is equal to "map" and that has only
2013 * a single reference.
2015 * If the original input already has only one reference, then
2016 * simply return it, but clear all cached information, since
2017 * it may be rendered invalid by the operations that will be
2018 * performed on the result.
2020 * Otherwise, create a duplicate (without any cached information).
2022 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2024 if (!map)
2025 return NULL;
2027 if (map->ref == 1)
2028 return clear_caches(map);
2029 map->ref--;
2030 return isl_map_dup(map);
2033 static void swap_vars(struct isl_blk blk, isl_int *a,
2034 unsigned a_len, unsigned b_len)
2036 isl_seq_cpy(blk.data, a+a_len, b_len);
2037 isl_seq_cpy(blk.data+b_len, a, a_len);
2038 isl_seq_cpy(a, blk.data, b_len+a_len);
2041 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2042 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2044 int i;
2045 struct isl_blk blk;
2047 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2048 goto error;
2050 if (n1 == 0 || n2 == 0)
2051 return bmap;
2053 bmap = isl_basic_map_cow(bmap);
2054 if (!bmap)
2055 return NULL;
2057 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2058 if (isl_blk_is_error(blk))
2059 goto error;
2061 for (i = 0; i < bmap->n_eq; ++i)
2062 swap_vars(blk,
2063 bmap->eq[i] + pos, n1, n2);
2065 for (i = 0; i < bmap->n_ineq; ++i)
2066 swap_vars(blk,
2067 bmap->ineq[i] + pos, n1, n2);
2069 for (i = 0; i < bmap->n_div; ++i)
2070 swap_vars(blk,
2071 bmap->div[i]+1 + pos, n1, n2);
2073 isl_blk_free(bmap->ctx, blk);
2075 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2076 bmap = isl_basic_map_gauss(bmap, NULL);
2077 return isl_basic_map_finalize(bmap);
2078 error:
2079 isl_basic_map_free(bmap);
2080 return NULL;
2083 /* The given basic map has turned out to be empty.
2084 * Explicitly mark it as such and change the representation
2085 * to a canonical representation of the empty basic map.
2086 * Since the basic map has conflicting constraints,
2087 * it must have at least one constraint, except perhaps
2088 * if it was already explicitly marked as being empty.
2089 * Do nothing in the latter case.
2091 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2092 __isl_take isl_basic_map *bmap)
2094 int i = 0;
2095 isl_bool empty;
2096 isl_size total;
2098 empty = isl_basic_map_plain_is_empty(bmap);
2099 if (empty < 0)
2100 return isl_basic_map_free(bmap);
2101 if (empty)
2102 return bmap;
2103 total = isl_basic_map_dim(bmap, isl_dim_all);
2104 if (total < 0)
2105 return isl_basic_map_free(bmap);
2106 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2107 return isl_basic_map_free(bmap);
2108 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2109 if (!bmap)
2110 return NULL;
2111 if (bmap->n_eq > 0) {
2112 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2113 if (!bmap)
2114 return NULL;
2115 } else {
2116 i = isl_basic_map_alloc_equality(bmap);
2117 if (i < 0)
2118 goto error;
2120 isl_int_set_si(bmap->eq[i][0], 1);
2121 isl_seq_clr(bmap->eq[i]+1, total);
2122 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2123 isl_vec_free(bmap->sample);
2124 bmap->sample = NULL;
2125 return isl_basic_map_finalize(bmap);
2126 error:
2127 isl_basic_map_free(bmap);
2128 return NULL;
2131 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2132 __isl_take isl_basic_set *bset)
2134 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2137 __isl_give isl_basic_map *isl_basic_map_set_rational(
2138 __isl_take isl_basic_map *bmap)
2140 if (!bmap)
2141 return NULL;
2143 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2144 return bmap;
2146 bmap = isl_basic_map_cow(bmap);
2147 if (!bmap)
2148 return NULL;
2150 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2152 return isl_basic_map_finalize(bmap);
2155 __isl_give isl_basic_set *isl_basic_set_set_rational(
2156 __isl_take isl_basic_set *bset)
2158 return isl_basic_map_set_rational(bset);
2161 __isl_give isl_basic_set *isl_basic_set_set_integral(
2162 __isl_take isl_basic_set *bset)
2164 if (!bset)
2165 return NULL;
2167 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2168 return bset;
2170 bset = isl_basic_set_cow(bset);
2171 if (!bset)
2172 return NULL;
2174 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2176 return isl_basic_set_finalize(bset);
2179 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2181 int i;
2183 map = isl_map_cow(map);
2184 if (!map)
2185 return NULL;
2186 for (i = 0; i < map->n; ++i) {
2187 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2188 if (!map->p[i])
2189 goto error;
2191 return map;
2192 error:
2193 isl_map_free(map);
2194 return NULL;
2197 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2199 return isl_map_set_rational(set);
2202 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2203 * of "bmap").
2205 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2207 isl_int *t = bmap->div[a];
2208 bmap->div[a] = bmap->div[b];
2209 bmap->div[b] = t;
2212 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2213 * div definitions accordingly.
2215 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2216 int a, int b)
2218 int i;
2219 isl_size off;
2221 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2222 if (off < 0)
2223 return isl_basic_map_free(bmap);
2225 swap_div(bmap, a, b);
2227 for (i = 0; i < bmap->n_eq; ++i)
2228 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2230 for (i = 0; i < bmap->n_ineq; ++i)
2231 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2233 for (i = 0; i < bmap->n_div; ++i)
2234 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2235 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2237 return bmap;
2240 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2242 isl_seq_cpy(c, c + n, rem);
2243 isl_seq_clr(c + rem, n);
2246 /* Drop n dimensions starting at first.
2248 * In principle, this frees up some extra variables as the number
2249 * of columns remains constant, but we would have to extend
2250 * the div array too as the number of rows in this array is assumed
2251 * to be equal to extra.
2253 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2254 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2256 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2259 /* Move "n" divs starting at "first" to the end of the list of divs.
2261 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2262 unsigned first, unsigned n)
2264 isl_int **div;
2265 int i;
2267 if (first + n == bmap->n_div)
2268 return bmap;
2270 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2271 if (!div)
2272 goto error;
2273 for (i = 0; i < n; ++i)
2274 div[i] = bmap->div[first + i];
2275 for (i = 0; i < bmap->n_div - first - n; ++i)
2276 bmap->div[first + i] = bmap->div[first + n + i];
2277 for (i = 0; i < n; ++i)
2278 bmap->div[bmap->n_div - n + i] = div[i];
2279 free(div);
2280 return bmap;
2281 error:
2282 isl_basic_map_free(bmap);
2283 return NULL;
2286 #undef TYPE
2287 #define TYPE isl_map
2288 static
2289 #include "check_type_range_templ.c"
2291 /* Check that there are "n" dimensions of type "type" starting at "first"
2292 * in "set".
2294 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2295 enum isl_dim_type type, unsigned first, unsigned n)
2297 return isl_map_check_range(set_to_map(set), type, first, n);
2300 /* Drop "n" dimensions of type "type" starting at "first".
2301 * Perform the core computation, without cowing or
2302 * simplifying and finalizing the result.
2304 * In principle, this frees up some extra variables as the number
2305 * of columns remains constant, but we would have to extend
2306 * the div array too as the number of rows in this array is assumed
2307 * to be equal to extra.
2309 __isl_give isl_basic_map *isl_basic_map_drop_core(
2310 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2311 unsigned first, unsigned n)
2313 int i;
2314 unsigned offset;
2315 unsigned left;
2316 isl_size total;
2318 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2319 return isl_basic_map_free(bmap);
2321 total = isl_basic_map_dim(bmap, isl_dim_all);
2322 if (total < 0)
2323 return isl_basic_map_free(bmap);
2325 offset = isl_basic_map_offset(bmap, type) + first;
2326 left = total - (offset - 1) - n;
2327 for (i = 0; i < bmap->n_eq; ++i)
2328 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2330 for (i = 0; i < bmap->n_ineq; ++i)
2331 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2333 for (i = 0; i < bmap->n_div; ++i)
2334 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2336 if (type == isl_dim_div) {
2337 bmap = move_divs_last(bmap, first, n);
2338 if (!bmap)
2339 return NULL;
2340 if (isl_basic_map_free_div(bmap, n) < 0)
2341 return isl_basic_map_free(bmap);
2342 } else
2343 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2344 if (!bmap->dim)
2345 return isl_basic_map_free(bmap);
2347 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2348 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2349 return bmap;
2352 /* Drop "n" dimensions of type "type" starting at "first".
2354 * In principle, this frees up some extra variables as the number
2355 * of columns remains constant, but we would have to extend
2356 * the div array too as the number of rows in this array is assumed
2357 * to be equal to extra.
2359 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2360 enum isl_dim_type type, unsigned first, unsigned n)
2362 if (!bmap)
2363 return NULL;
2364 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2365 return bmap;
2367 bmap = isl_basic_map_cow(bmap);
2368 if (!bmap)
2369 return NULL;
2371 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2373 bmap = isl_basic_map_simplify(bmap);
2374 return isl_basic_map_finalize(bmap);
2377 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2378 enum isl_dim_type type, unsigned first, unsigned n)
2380 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2381 type, first, n));
2384 /* No longer consider "map" to be normalized.
2386 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2388 if (!map)
2389 return NULL;
2390 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2391 return map;
2394 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 int i;
2398 isl_space *space;
2400 if (isl_map_check_range(map, type, first, n) < 0)
2401 return isl_map_free(map);
2403 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2404 return map;
2405 map = isl_map_cow(map);
2406 if (!map)
2407 goto error;
2409 for (i = 0; i < map->n; ++i) {
2410 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2411 if (!map->p[i])
2412 goto error;
2414 map = isl_map_unmark_normalized(map);
2416 space = isl_map_take_space(map);
2417 space = isl_space_drop_dims(space, type, first, n);
2418 map = isl_map_restore_space(map, space);
2420 return map;
2421 error:
2422 isl_map_free(map);
2423 return NULL;
2426 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2427 enum isl_dim_type type, unsigned first, unsigned n)
2429 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2432 /* Drop the integer division at position "div", which is assumed
2433 * not to appear in any of the constraints or
2434 * in any of the other integer divisions.
2436 * Since the integer division is redundant, there is no need to cow.
2438 __isl_give isl_basic_map *isl_basic_map_drop_div(
2439 __isl_take isl_basic_map *bmap, unsigned div)
2441 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2444 /* Eliminate the specified n dimensions starting at first from the
2445 * constraints, without removing the dimensions from the space.
2446 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2448 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2449 enum isl_dim_type type, unsigned first, unsigned n)
2451 int i;
2453 if (n == 0)
2454 return map;
2456 if (isl_map_check_range(map, type, first, n) < 0)
2457 return isl_map_free(map);
2459 map = isl_map_cow(map);
2460 if (!map)
2461 return NULL;
2463 for (i = 0; i < map->n; ++i) {
2464 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2465 if (!map->p[i])
2466 goto error;
2468 return map;
2469 error:
2470 isl_map_free(map);
2471 return NULL;
2474 /* Eliminate the specified n dimensions starting at first from the
2475 * constraints, without removing the dimensions from the space.
2476 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2478 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2479 enum isl_dim_type type, unsigned first, unsigned n)
2481 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2484 /* Eliminate the specified n dimensions starting at first from the
2485 * constraints, without removing the dimensions from the space.
2486 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2488 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2489 unsigned first, unsigned n)
2491 return isl_set_eliminate(set, isl_dim_set, first, n);
2494 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2495 __isl_take isl_basic_map *bmap)
2497 isl_size v_div;
2499 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2500 if (v_div < 0)
2501 return isl_basic_map_free(bmap);
2502 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2503 if (!bmap)
2504 return NULL;
2505 bmap->n_div = 0;
2506 return isl_basic_map_finalize(bmap);
2509 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2510 __isl_take isl_basic_set *bset)
2512 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2515 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2517 int i;
2519 if (!map)
2520 return NULL;
2521 if (map->n == 0)
2522 return map;
2524 map = isl_map_cow(map);
2525 if (!map)
2526 return NULL;
2528 for (i = 0; i < map->n; ++i) {
2529 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2530 if (!map->p[i])
2531 goto error;
2533 return map;
2534 error:
2535 isl_map_free(map);
2536 return NULL;
2539 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2541 return isl_map_remove_divs(set);
2544 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2545 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2546 unsigned first, unsigned n)
2548 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2549 return isl_basic_map_free(bmap);
2550 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2551 return bmap;
2552 bmap = isl_basic_map_eliminate_vars(bmap,
2553 isl_basic_map_offset(bmap, type) - 1 + first, n);
2554 if (!bmap)
2555 return bmap;
2556 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2557 return bmap;
2558 bmap = isl_basic_map_drop(bmap, type, first, n);
2559 return bmap;
2562 /* Return true if the definition of the given div (recursively) involves
2563 * any of the given variables.
2565 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2566 unsigned first, unsigned n)
2568 int i;
2569 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2571 if (isl_int_is_zero(bmap->div[div][0]))
2572 return isl_bool_false;
2573 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2574 return isl_bool_true;
2576 for (i = bmap->n_div - 1; i >= 0; --i) {
2577 isl_bool involves;
2579 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2580 continue;
2581 involves = div_involves_vars(bmap, i, first, n);
2582 if (involves < 0 || involves)
2583 return involves;
2586 return isl_bool_false;
2589 /* Try and add a lower and/or upper bound on "div" to "bmap"
2590 * based on inequality "i".
2591 * "total" is the total number of variables (excluding the divs).
2592 * "v" is a temporary object that can be used during the calculations.
2593 * If "lb" is set, then a lower bound should be constructed.
2594 * If "ub" is set, then an upper bound should be constructed.
2596 * The calling function has already checked that the inequality does not
2597 * reference "div", but we still need to check that the inequality is
2598 * of the right form. We'll consider the case where we want to construct
2599 * a lower bound. The construction of upper bounds is similar.
2601 * Let "div" be of the form
2603 * q = floor((a + f(x))/d)
2605 * We essentially check if constraint "i" is of the form
2607 * b + f(x) >= 0
2609 * so that we can use it to derive a lower bound on "div".
2610 * However, we allow a slightly more general form
2612 * b + g(x) >= 0
2614 * with the condition that the coefficients of g(x) - f(x) are all
2615 * divisible by d.
2616 * Rewriting this constraint as
2618 * 0 >= -b - g(x)
2620 * adding a + f(x) to both sides and dividing by d, we obtain
2622 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2624 * Taking the floor on both sides, we obtain
2626 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2628 * or
2630 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2632 * In the case of an upper bound, we construct the constraint
2634 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2637 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2638 __isl_take isl_basic_map *bmap, int div, int i,
2639 unsigned total, isl_int v, int lb, int ub)
2641 int j;
2643 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2644 if (lb) {
2645 isl_int_sub(v, bmap->ineq[i][1 + j],
2646 bmap->div[div][1 + 1 + j]);
2647 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2649 if (ub) {
2650 isl_int_add(v, bmap->ineq[i][1 + j],
2651 bmap->div[div][1 + 1 + j]);
2652 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2655 if (!lb && !ub)
2656 return bmap;
2658 bmap = isl_basic_map_cow(bmap);
2659 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2660 if (lb) {
2661 int k = isl_basic_map_alloc_inequality(bmap);
2662 if (k < 0)
2663 goto error;
2664 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2665 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2666 bmap->div[div][1 + j]);
2667 isl_int_cdiv_q(bmap->ineq[k][j],
2668 bmap->ineq[k][j], bmap->div[div][0]);
2670 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2672 if (ub) {
2673 int k = isl_basic_map_alloc_inequality(bmap);
2674 if (k < 0)
2675 goto error;
2676 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2677 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2678 bmap->div[div][1 + j]);
2679 isl_int_fdiv_q(bmap->ineq[k][j],
2680 bmap->ineq[k][j], bmap->div[div][0]);
2682 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2685 return bmap;
2686 error:
2687 isl_basic_map_free(bmap);
2688 return NULL;
2691 /* This function is called right before "div" is eliminated from "bmap"
2692 * using Fourier-Motzkin.
2693 * Look through the constraints of "bmap" for constraints on the argument
2694 * of the integer division and use them to construct constraints on the
2695 * integer division itself. These constraints can then be combined
2696 * during the Fourier-Motzkin elimination.
2697 * Note that it is only useful to introduce lower bounds on "div"
2698 * if "bmap" already contains upper bounds on "div" as the newly
2699 * introduce lower bounds can then be combined with the pre-existing
2700 * upper bounds. Similarly for upper bounds.
2701 * We therefore first check if "bmap" contains any lower and/or upper bounds
2702 * on "div".
2704 * It is interesting to note that the introduction of these constraints
2705 * can indeed lead to more accurate results, even when compared to
2706 * deriving constraints on the argument of "div" from constraints on "div".
2707 * Consider, for example, the set
2709 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2711 * The second constraint can be rewritten as
2713 * 2 * [(-i-2j+3)/4] + k >= 0
2715 * from which we can derive
2717 * -i - 2j + 3 >= -2k
2719 * or
2721 * i + 2j <= 3 + 2k
2723 * Combined with the first constraint, we obtain
2725 * -3 <= 3 + 2k or k >= -3
2727 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2728 * the first constraint, we obtain
2730 * [(i + 2j)/4] >= [-3/4] = -1
2732 * Combining this constraint with the second constraint, we obtain
2734 * k >= -2
2736 static __isl_give isl_basic_map *insert_bounds_on_div(
2737 __isl_take isl_basic_map *bmap, int div)
2739 int i;
2740 int check_lb, check_ub;
2741 isl_int v;
2742 isl_size v_div;
2744 if (!bmap)
2745 return NULL;
2747 if (isl_int_is_zero(bmap->div[div][0]))
2748 return bmap;
2750 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2751 if (v_div < 0)
2752 return isl_basic_map_free(bmap);
2754 check_lb = 0;
2755 check_ub = 0;
2756 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2757 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2758 if (s > 0)
2759 check_ub = 1;
2760 if (s < 0)
2761 check_lb = 1;
2764 if (!check_lb && !check_ub)
2765 return bmap;
2767 isl_int_init(v);
2769 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2770 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2771 continue;
2773 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2774 check_lb, check_ub);
2777 isl_int_clear(v);
2779 return bmap;
2782 /* Remove all divs (recursively) involving any of the given dimensions
2783 * in their definitions.
2785 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2786 __isl_take isl_basic_map *bmap,
2787 enum isl_dim_type type, unsigned first, unsigned n)
2789 int i;
2791 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2792 return isl_basic_map_free(bmap);
2793 first += isl_basic_map_offset(bmap, type);
2795 for (i = bmap->n_div - 1; i >= 0; --i) {
2796 isl_bool involves;
2798 involves = div_involves_vars(bmap, i, first, n);
2799 if (involves < 0)
2800 return isl_basic_map_free(bmap);
2801 if (!involves)
2802 continue;
2803 bmap = insert_bounds_on_div(bmap, i);
2804 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2805 if (!bmap)
2806 return NULL;
2807 i = bmap->n_div;
2810 return bmap;
2813 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2814 __isl_take isl_basic_set *bset,
2815 enum isl_dim_type type, unsigned first, unsigned n)
2817 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2820 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2821 enum isl_dim_type type, unsigned first, unsigned n)
2823 int i;
2825 if (!map)
2826 return NULL;
2827 if (map->n == 0)
2828 return map;
2830 map = isl_map_cow(map);
2831 if (!map)
2832 return NULL;
2834 for (i = 0; i < map->n; ++i) {
2835 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2836 type, first, n);
2837 if (!map->p[i])
2838 goto error;
2840 return map;
2841 error:
2842 isl_map_free(map);
2843 return NULL;
2846 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2847 enum isl_dim_type type, unsigned first, unsigned n)
2849 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2850 type, first, n));
2853 /* Does the description of "bmap" depend on the specified dimensions?
2854 * We also check whether the dimensions appear in any of the div definitions.
2855 * In principle there is no need for this check. If the dimensions appear
2856 * in a div definition, they also appear in the defining constraints of that
2857 * div.
2859 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2860 enum isl_dim_type type, unsigned first, unsigned n)
2862 int i;
2864 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2865 return isl_bool_error;
2867 first += isl_basic_map_offset(bmap, type);
2868 for (i = 0; i < bmap->n_eq; ++i)
2869 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2870 return isl_bool_true;
2871 for (i = 0; i < bmap->n_ineq; ++i)
2872 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2873 return isl_bool_true;
2874 for (i = 0; i < bmap->n_div; ++i) {
2875 if (isl_int_is_zero(bmap->div[i][0]))
2876 continue;
2877 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2878 return isl_bool_true;
2881 return isl_bool_false;
2884 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2885 enum isl_dim_type type, unsigned first, unsigned n)
2887 int i;
2889 if (isl_map_check_range(map, type, first, n) < 0)
2890 return isl_bool_error;
2892 for (i = 0; i < map->n; ++i) {
2893 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2894 type, first, n);
2895 if (involves < 0 || involves)
2896 return involves;
2899 return isl_bool_false;
2902 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2903 enum isl_dim_type type, unsigned first, unsigned n)
2905 return isl_basic_map_involves_dims(bset, type, first, n);
2908 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2909 enum isl_dim_type type, unsigned first, unsigned n)
2911 return isl_map_involves_dims(set, type, first, n);
2914 /* Drop all constraints in bmap that involve any of the dimensions
2915 * first to first+n-1.
2916 * This function only performs the actual removal of constraints.
2918 * This function should not call finalize since it is used by
2919 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2921 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2922 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2924 int i;
2926 if (n == 0)
2927 return bmap;
2929 bmap = isl_basic_map_cow(bmap);
2931 if (!bmap)
2932 return NULL;
2934 for (i = bmap->n_eq - 1; i >= 0; --i) {
2935 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2936 continue;
2937 if (isl_basic_map_drop_equality(bmap, i) < 0)
2938 return isl_basic_map_free(bmap);
2941 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2942 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2943 continue;
2944 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2945 return isl_basic_map_free(bmap);
2948 return bmap;
2951 /* Drop all constraints in bset that involve any of the dimensions
2952 * first to first+n-1.
2953 * This function only performs the actual removal of constraints.
2955 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2956 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2958 return isl_basic_map_drop_constraints_involving(bset, first, n);
2961 /* Drop all constraints in bmap that do not involve any of the dimensions
2962 * first to first + n - 1 of the given type.
2964 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2965 __isl_take isl_basic_map *bmap,
2966 enum isl_dim_type type, unsigned first, unsigned n)
2968 int i;
2970 if (n == 0) {
2971 isl_space *space = isl_basic_map_get_space(bmap);
2972 isl_basic_map_free(bmap);
2973 return isl_basic_map_universe(space);
2975 bmap = isl_basic_map_cow(bmap);
2976 if (!bmap)
2977 return NULL;
2979 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2980 return isl_basic_map_free(bmap);
2982 first += isl_basic_map_offset(bmap, type) - 1;
2984 for (i = bmap->n_eq - 1; i >= 0; --i) {
2985 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2986 continue;
2987 if (isl_basic_map_drop_equality(bmap, i) < 0)
2988 return isl_basic_map_free(bmap);
2991 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2992 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2993 continue;
2994 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2995 return isl_basic_map_free(bmap);
2998 bmap = isl_basic_map_add_known_div_constraints(bmap);
2999 return bmap;
3002 /* Drop all constraints in bset that do not involve any of the dimensions
3003 * first to first + n - 1 of the given type.
3005 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3006 __isl_take isl_basic_set *bset,
3007 enum isl_dim_type type, unsigned first, unsigned n)
3009 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3010 type, first, n);
3013 /* Drop all constraints in bmap that involve any of the dimensions
3014 * first to first + n - 1 of the given type.
3016 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3017 __isl_take isl_basic_map *bmap,
3018 enum isl_dim_type type, unsigned first, unsigned n)
3020 if (!bmap)
3021 return NULL;
3022 if (n == 0)
3023 return bmap;
3025 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3026 return isl_basic_map_free(bmap);
3028 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3029 first += isl_basic_map_offset(bmap, type) - 1;
3030 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3031 bmap = isl_basic_map_add_known_div_constraints(bmap);
3032 return bmap;
3035 /* Drop all constraints in bset that involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3039 __isl_take isl_basic_set *bset,
3040 enum isl_dim_type type, unsigned first, unsigned n)
3042 return isl_basic_map_drop_constraints_involving_dims(bset,
3043 type, first, n);
3046 /* Drop constraints from "map" by applying "drop" to each basic map.
3048 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3049 enum isl_dim_type type, unsigned first, unsigned n,
3050 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3051 enum isl_dim_type type, unsigned first, unsigned n))
3053 int i;
3055 if (isl_map_check_range(map, type, first, n) < 0)
3056 return isl_map_free(map);
3058 map = isl_map_cow(map);
3059 if (!map)
3060 return NULL;
3062 for (i = 0; i < map->n; ++i) {
3063 map->p[i] = drop(map->p[i], type, first, n);
3064 if (!map->p[i])
3065 return isl_map_free(map);
3068 if (map->n > 1)
3069 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3071 return map;
3074 /* Drop all constraints in map that involve any of the dimensions
3075 * first to first + n - 1 of the given type.
3077 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3078 __isl_take isl_map *map,
3079 enum isl_dim_type type, unsigned first, unsigned n)
3081 if (n == 0)
3082 return map;
3083 return drop_constraints(map, type, first, n,
3084 &isl_basic_map_drop_constraints_involving_dims);
3087 /* Drop all constraints in "map" that do not involve any of the dimensions
3088 * first to first + n - 1 of the given type.
3090 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3091 __isl_take isl_map *map,
3092 enum isl_dim_type type, unsigned first, unsigned n)
3094 if (n == 0) {
3095 isl_space *space = isl_map_get_space(map);
3096 isl_map_free(map);
3097 return isl_map_universe(space);
3099 return drop_constraints(map, type, first, n,
3100 &isl_basic_map_drop_constraints_not_involving_dims);
3103 /* Drop all constraints in set that involve any of the dimensions
3104 * first to first + n - 1 of the given type.
3106 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3107 __isl_take isl_set *set,
3108 enum isl_dim_type type, unsigned first, unsigned n)
3110 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3113 /* Drop all constraints in "set" that do not involve any of the dimensions
3114 * first to first + n - 1 of the given type.
3116 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3117 __isl_take isl_set *set,
3118 enum isl_dim_type type, unsigned first, unsigned n)
3120 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3123 /* Does local variable "div" of "bmap" have a complete explicit representation?
3124 * Having a complete explicit representation requires not only
3125 * an explicit representation, but also that all local variables
3126 * that appear in this explicit representation in turn have
3127 * a complete explicit representation.
3129 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3131 int i;
3132 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3133 isl_bool marked;
3135 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3136 if (marked < 0 || marked)
3137 return isl_bool_not(marked);
3139 for (i = bmap->n_div - 1; i >= 0; --i) {
3140 isl_bool known;
3142 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3143 continue;
3144 known = isl_basic_map_div_is_known(bmap, i);
3145 if (known < 0 || !known)
3146 return known;
3149 return isl_bool_true;
3152 /* Remove all divs that are unknown or defined in terms of unknown divs.
3154 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3155 __isl_take isl_basic_map *bmap)
3157 int i;
3159 if (!bmap)
3160 return NULL;
3162 for (i = bmap->n_div - 1; i >= 0; --i) {
3163 if (isl_basic_map_div_is_known(bmap, i))
3164 continue;
3165 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3166 if (!bmap)
3167 return NULL;
3168 i = bmap->n_div;
3171 return bmap;
3174 /* Remove all divs that are unknown or defined in terms of unknown divs.
3176 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3177 __isl_take isl_basic_set *bset)
3179 return isl_basic_map_remove_unknown_divs(bset);
3182 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3184 int i;
3186 if (!map)
3187 return NULL;
3188 if (map->n == 0)
3189 return map;
3191 map = isl_map_cow(map);
3192 if (!map)
3193 return NULL;
3195 for (i = 0; i < map->n; ++i) {
3196 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3197 if (!map->p[i])
3198 goto error;
3200 return map;
3201 error:
3202 isl_map_free(map);
3203 return NULL;
3206 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3208 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3211 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3212 __isl_take isl_basic_set *bset,
3213 enum isl_dim_type type, unsigned first, unsigned n)
3215 isl_basic_map *bmap = bset_to_bmap(bset);
3216 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3217 return bset_from_bmap(bmap);
3220 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3221 enum isl_dim_type type, unsigned first, unsigned n)
3223 int i;
3225 if (n == 0)
3226 return map;
3228 map = isl_map_cow(map);
3229 if (isl_map_check_range(map, type, first, n) < 0)
3230 return isl_map_free(map);
3232 for (i = 0; i < map->n; ++i) {
3233 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3234 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3235 if (!map->p[i])
3236 goto error;
3238 map = isl_map_drop(map, type, first, n);
3239 return map;
3240 error:
3241 isl_map_free(map);
3242 return NULL;
3245 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3246 enum isl_dim_type type, unsigned first, unsigned n)
3248 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3249 type, first, n));
3252 /* Project out n inputs starting at first using Fourier-Motzkin */
3253 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3254 unsigned first, unsigned n)
3256 return isl_map_remove_dims(map, isl_dim_in, first, n);
3259 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3260 FILE *out, int indent)
3262 isl_printer *p;
3264 if (!bset) {
3265 fprintf(out, "null basic set\n");
3266 return;
3269 fprintf(out, "%*s", indent, "");
3270 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3271 bset->ref, bset->dim->nparam, bset->dim->n_out,
3272 bset->extra, bset->flags);
3274 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3275 p = isl_printer_set_dump(p, 1);
3276 p = isl_printer_set_indent(p, indent);
3277 p = isl_printer_start_line(p);
3278 p = isl_printer_print_basic_set(p, bset);
3279 p = isl_printer_end_line(p);
3280 isl_printer_free(p);
3283 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3284 FILE *out, int indent)
3286 isl_printer *p;
3288 if (!bmap) {
3289 fprintf(out, "null basic map\n");
3290 return;
3293 fprintf(out, "%*s", indent, "");
3294 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3295 "flags: %x, n_name: %d\n",
3296 bmap->ref,
3297 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3298 bmap->extra, bmap->flags, bmap->dim->n_id);
3300 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3301 p = isl_printer_set_dump(p, 1);
3302 p = isl_printer_set_indent(p, indent);
3303 p = isl_printer_start_line(p);
3304 p = isl_printer_print_basic_map(p, bmap);
3305 p = isl_printer_end_line(p);
3306 isl_printer_free(p);
3309 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3310 unsigned pos)
3312 isl_size total;
3314 total = isl_basic_map_dim(bmap, isl_dim_all);
3315 if (total < 0)
3316 return isl_basic_map_free(bmap);
3317 if (pos >= bmap->n_ineq)
3318 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3319 "invalid position", return isl_basic_map_free(bmap));
3320 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3321 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3322 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3323 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3324 return bmap;
3327 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3328 unsigned flags)
3330 if (isl_space_check_is_set(space) < 0)
3331 goto error;
3332 return isl_map_alloc_space(space, n, flags);
3333 error:
3334 isl_space_free(space);
3335 return NULL;
3338 /* Make sure "map" has room for at least "n" more basic maps.
3340 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3342 int i;
3343 struct isl_map *grown = NULL;
3345 if (!map)
3346 return NULL;
3347 isl_assert(map->ctx, n >= 0, goto error);
3348 if (map->n + n <= map->size)
3349 return map;
3350 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3351 if (!grown)
3352 goto error;
3353 for (i = 0; i < map->n; ++i) {
3354 grown->p[i] = isl_basic_map_copy(map->p[i]);
3355 if (!grown->p[i])
3356 goto error;
3357 grown->n++;
3359 isl_map_free(map);
3360 return grown;
3361 error:
3362 isl_map_free(grown);
3363 isl_map_free(map);
3364 return NULL;
3367 /* Make sure "set" has room for at least "n" more basic sets.
3369 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3371 return set_from_map(isl_map_grow(set_to_map(set), n));
3374 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3376 return isl_map_from_basic_map(bset);
3379 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3381 struct isl_map *map;
3383 if (!bmap)
3384 return NULL;
3386 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3387 return isl_map_add_basic_map(map, bmap);
3390 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3391 __isl_take isl_basic_set *bset)
3393 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3394 bset_to_bmap(bset)));
3397 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3399 return isl_map_free(set);
3402 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3404 int i;
3406 if (!set) {
3407 fprintf(out, "null set\n");
3408 return;
3411 fprintf(out, "%*s", indent, "");
3412 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3413 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3414 set->flags);
3415 for (i = 0; i < set->n; ++i) {
3416 fprintf(out, "%*s", indent, "");
3417 fprintf(out, "basic set %d:\n", i);
3418 isl_basic_set_print_internal(set->p[i], out, indent+4);
3422 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3424 int i;
3426 if (!map) {
3427 fprintf(out, "null map\n");
3428 return;
3431 fprintf(out, "%*s", indent, "");
3432 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3433 "flags: %x, n_name: %d\n",
3434 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3435 map->dim->n_out, map->flags, map->dim->n_id);
3436 for (i = 0; i < map->n; ++i) {
3437 fprintf(out, "%*s", indent, "");
3438 fprintf(out, "basic map %d:\n", i);
3439 isl_basic_map_print_internal(map->p[i], out, indent+4);
3443 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3445 static isl_stat isl_basic_map_check_compatible_domain(
3446 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3448 isl_bool ok;
3450 ok = isl_basic_map_compatible_domain(bmap, bset);
3451 if (ok < 0)
3452 return isl_stat_error;
3453 if (!ok)
3454 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3455 "incompatible spaces", return isl_stat_error);
3457 return isl_stat_ok;
3460 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3461 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3463 struct isl_basic_map *bmap_domain;
3464 isl_size dim;
3466 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3467 goto error;
3469 dim = isl_basic_set_dim(bset, isl_dim_set);
3470 if (dim < 0)
3471 goto error;
3472 if (dim != 0 &&
3473 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3474 goto error;
3476 bmap = isl_basic_map_cow(bmap);
3477 if (!bmap)
3478 goto error;
3479 bmap = isl_basic_map_extend(bmap,
3480 bset->n_div, bset->n_eq, bset->n_ineq);
3481 bmap_domain = isl_basic_map_from_domain(bset);
3482 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3484 bmap = isl_basic_map_simplify(bmap);
3485 return isl_basic_map_finalize(bmap);
3486 error:
3487 isl_basic_map_free(bmap);
3488 isl_basic_set_free(bset);
3489 return NULL;
3492 /* Check that the space of "bset" is the same as that of the range of "bmap".
3494 static isl_stat isl_basic_map_check_compatible_range(
3495 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3497 isl_bool ok;
3499 ok = isl_basic_map_compatible_range(bmap, bset);
3500 if (ok < 0)
3501 return isl_stat_error;
3502 if (!ok)
3503 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3504 "incompatible spaces", return isl_stat_error);
3506 return isl_stat_ok;
3509 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3510 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3512 struct isl_basic_map *bmap_range;
3513 isl_size dim;
3515 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3516 goto error;
3518 dim = isl_basic_set_dim(bset, isl_dim_set);
3519 if (dim < 0)
3520 goto error;
3521 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3522 goto error;
3524 if (isl_basic_set_plain_is_universe(bset)) {
3525 isl_basic_set_free(bset);
3526 return bmap;
3529 bmap = isl_basic_map_cow(bmap);
3530 if (!bmap)
3531 goto error;
3532 bmap = isl_basic_map_extend(bmap,
3533 bset->n_div, bset->n_eq, bset->n_ineq);
3534 bmap_range = bset_to_bmap(bset);
3535 bmap = add_constraints(bmap, bmap_range, 0, 0);
3537 bmap = isl_basic_map_simplify(bmap);
3538 return isl_basic_map_finalize(bmap);
3539 error:
3540 isl_basic_map_free(bmap);
3541 isl_basic_set_free(bset);
3542 return NULL;
3545 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3546 __isl_keep isl_vec *vec)
3548 int i;
3549 isl_size total;
3550 isl_int s;
3552 total = isl_basic_map_dim(bmap, isl_dim_all);
3553 if (total < 0 || !vec)
3554 return isl_bool_error;
3556 if (1 + total != vec->size)
3557 return isl_bool_false;
3559 isl_int_init(s);
3561 for (i = 0; i < bmap->n_eq; ++i) {
3562 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3563 if (!isl_int_is_zero(s)) {
3564 isl_int_clear(s);
3565 return isl_bool_false;
3569 for (i = 0; i < bmap->n_ineq; ++i) {
3570 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3571 if (isl_int_is_neg(s)) {
3572 isl_int_clear(s);
3573 return isl_bool_false;
3577 isl_int_clear(s);
3579 return isl_bool_true;
3582 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3583 __isl_keep isl_vec *vec)
3585 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3588 __isl_give isl_basic_map *isl_basic_map_intersect(
3589 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3591 struct isl_vec *sample = NULL;
3592 isl_space *space1, *space2;
3593 isl_size dim1, dim2, nparam1, nparam2;
3595 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3596 goto error;
3597 space1 = isl_basic_map_peek_space(bmap1);
3598 space2 = isl_basic_map_peek_space(bmap2);
3599 dim1 = isl_space_dim(space1, isl_dim_all);
3600 dim2 = isl_space_dim(space2, isl_dim_all);
3601 nparam1 = isl_space_dim(space1, isl_dim_param);
3602 nparam2 = isl_space_dim(space2, isl_dim_param);
3603 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3604 goto error;
3605 if (dim1 == nparam1 && dim2 != nparam2)
3606 return isl_basic_map_intersect(bmap2, bmap1);
3608 if (dim2 != nparam2 &&
3609 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3610 goto error;
3612 if (isl_basic_map_plain_is_empty(bmap1)) {
3613 isl_basic_map_free(bmap2);
3614 return bmap1;
3616 if (isl_basic_map_plain_is_empty(bmap2)) {
3617 isl_basic_map_free(bmap1);
3618 return bmap2;
3621 if (bmap1->sample &&
3622 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3623 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3624 sample = isl_vec_copy(bmap1->sample);
3625 else if (bmap2->sample &&
3626 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3627 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3628 sample = isl_vec_copy(bmap2->sample);
3630 bmap1 = isl_basic_map_cow(bmap1);
3631 if (!bmap1)
3632 goto error;
3633 bmap1 = isl_basic_map_extend(bmap1,
3634 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3635 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3637 if (!bmap1)
3638 isl_vec_free(sample);
3639 else if (sample) {
3640 isl_vec_free(bmap1->sample);
3641 bmap1->sample = sample;
3644 bmap1 = isl_basic_map_simplify(bmap1);
3645 return isl_basic_map_finalize(bmap1);
3646 error:
3647 if (sample)
3648 isl_vec_free(sample);
3649 isl_basic_map_free(bmap1);
3650 isl_basic_map_free(bmap2);
3651 return NULL;
3654 __isl_give isl_basic_set *isl_basic_set_intersect(
3655 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3657 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3658 bset_to_bmap(bset2)));
3661 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3662 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3664 return isl_basic_set_intersect(bset1, bset2);
3667 /* Special case of isl_map_intersect, where both map1 and map2
3668 * are convex, without any divs and such that either map1 or map2
3669 * contains a single constraint. This constraint is then simply
3670 * added to the other map.
3672 static __isl_give isl_map *map_intersect_add_constraint(
3673 __isl_take isl_map *map1, __isl_take isl_map *map2)
3675 isl_assert(map1->ctx, map1->n == 1, goto error);
3676 isl_assert(map2->ctx, map1->n == 1, goto error);
3677 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3678 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3680 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3681 return isl_map_intersect(map2, map1);
3683 map1 = isl_map_cow(map1);
3684 if (!map1)
3685 goto error;
3686 if (isl_map_plain_is_empty(map1)) {
3687 isl_map_free(map2);
3688 return map1;
3690 if (map2->p[0]->n_eq == 1)
3691 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3692 else
3693 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3694 map2->p[0]->ineq[0]);
3696 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3697 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3698 if (!map1->p[0])
3699 goto error;
3701 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3702 isl_basic_map_free(map1->p[0]);
3703 map1->n = 0;
3706 isl_map_free(map2);
3708 map1 = isl_map_unmark_normalized(map1);
3709 return map1;
3710 error:
3711 isl_map_free(map1);
3712 isl_map_free(map2);
3713 return NULL;
3716 /* map2 may be either a parameter domain or a map living in the same
3717 * space as map1.
3719 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3720 __isl_take isl_map *map2)
3722 unsigned flags = 0;
3723 isl_bool equal;
3724 isl_map *result;
3725 int i, j;
3726 isl_size dim2, nparam2;
3728 if (!map1 || !map2)
3729 goto error;
3731 if ((isl_map_plain_is_empty(map1) ||
3732 isl_map_plain_is_universe(map2)) &&
3733 isl_space_is_equal(map1->dim, map2->dim)) {
3734 isl_map_free(map2);
3735 return map1;
3737 if ((isl_map_plain_is_empty(map2) ||
3738 isl_map_plain_is_universe(map1)) &&
3739 isl_space_is_equal(map1->dim, map2->dim)) {
3740 isl_map_free(map1);
3741 return map2;
3744 if (map1->n == 1 && map2->n == 1 &&
3745 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3746 isl_space_is_equal(map1->dim, map2->dim) &&
3747 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3748 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3749 return map_intersect_add_constraint(map1, map2);
3751 equal = isl_map_plain_is_equal(map1, map2);
3752 if (equal < 0)
3753 goto error;
3754 if (equal) {
3755 isl_map_free(map2);
3756 return map1;
3759 dim2 = isl_map_dim(map2, isl_dim_all);
3760 nparam2 = isl_map_dim(map2, isl_dim_param);
3761 if (dim2 < 0 || nparam2 < 0)
3762 goto error;
3763 if (dim2 != nparam2)
3764 isl_assert(map1->ctx,
3765 isl_space_is_equal(map1->dim, map2->dim), goto error);
3767 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3768 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3769 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3771 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3772 map1->n * map2->n, flags);
3773 if (!result)
3774 goto error;
3775 for (i = 0; i < map1->n; ++i)
3776 for (j = 0; j < map2->n; ++j) {
3777 struct isl_basic_map *part;
3778 part = isl_basic_map_intersect(
3779 isl_basic_map_copy(map1->p[i]),
3780 isl_basic_map_copy(map2->p[j]));
3781 if (isl_basic_map_is_empty(part) < 0)
3782 part = isl_basic_map_free(part);
3783 result = isl_map_add_basic_map(result, part);
3784 if (!result)
3785 goto error;
3787 isl_map_free(map1);
3788 isl_map_free(map2);
3789 return result;
3790 error:
3791 isl_map_free(map1);
3792 isl_map_free(map2);
3793 return NULL;
3796 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3797 __isl_take isl_map *map2)
3799 if (isl_map_check_equal_space(map1, map2) < 0)
3800 goto error;
3801 return map_intersect_internal(map1, map2);
3802 error:
3803 isl_map_free(map1);
3804 isl_map_free(map2);
3805 return NULL;
3808 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3809 __isl_take isl_map *map2)
3811 isl_map_align_params_bin(&map1, &map2);
3812 return map_intersect(map1, map2);
3815 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3816 __isl_take isl_set *set2)
3818 return set_from_map(isl_map_intersect(set_to_map(set1),
3819 set_to_map(set2)));
3822 /* map_intersect_internal accepts intersections
3823 * with parameter domains, so we can just call that function.
3825 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3826 __isl_take isl_set *params)
3828 isl_map_align_params_set(&map, &params);
3829 return map_intersect_internal(map, params);
3832 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3833 __isl_take isl_set *params)
3835 return isl_map_intersect_params(set, params);
3838 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3840 isl_space *space;
3841 unsigned pos;
3842 isl_size n1, n2;
3844 if (!bmap)
3845 return NULL;
3846 bmap = isl_basic_map_cow(bmap);
3847 if (!bmap)
3848 return NULL;
3849 space = isl_space_reverse(isl_space_copy(bmap->dim));
3850 pos = isl_basic_map_offset(bmap, isl_dim_in);
3851 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3852 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3853 if (n1 < 0 || n2 < 0)
3854 bmap = isl_basic_map_free(bmap);
3855 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3856 return isl_basic_map_reset_space(bmap, space);
3859 /* Given a basic map A -> (B -> C), return the corresponding basic map
3860 * A -> (C -> B).
3862 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3863 __isl_take isl_basic_map *bmap)
3865 isl_space *space;
3866 isl_size offset, n1, n2;
3868 space = isl_basic_map_peek_space(bmap);
3869 if (isl_space_check_range_is_wrapping(space) < 0)
3870 return isl_basic_map_free(bmap);
3871 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3872 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3873 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3874 if (offset < 0 || n1 < 0 || n2 < 0)
3875 return isl_basic_map_free(bmap);
3877 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3879 space = isl_basic_map_take_space(bmap);
3880 space = isl_space_range_reverse(space);
3881 bmap = isl_basic_map_restore_space(bmap, space);
3883 return bmap;
3886 static __isl_give isl_basic_map *basic_map_space_reset(
3887 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3889 isl_space *space;
3891 if (!bmap)
3892 return NULL;
3893 if (!isl_space_is_named_or_nested(bmap->dim, type))
3894 return bmap;
3896 space = isl_basic_map_get_space(bmap);
3897 space = isl_space_reset(space, type);
3898 bmap = isl_basic_map_reset_space(bmap, space);
3899 return bmap;
3902 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3903 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3904 unsigned pos, unsigned n)
3906 isl_bool rational, is_empty;
3907 isl_space *res_space;
3908 struct isl_basic_map *res;
3909 struct isl_dim_map *dim_map;
3910 isl_size total;
3911 unsigned off;
3912 enum isl_dim_type t;
3914 if (n == 0)
3915 return basic_map_space_reset(bmap, type);
3917 is_empty = isl_basic_map_plain_is_empty(bmap);
3918 total = isl_basic_map_dim(bmap, isl_dim_all);
3919 if (is_empty < 0 || total < 0)
3920 return isl_basic_map_free(bmap);
3921 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3922 type, pos, n);
3923 if (!res_space)
3924 return isl_basic_map_free(bmap);
3925 if (is_empty) {
3926 isl_basic_map_free(bmap);
3927 return isl_basic_map_empty(res_space);
3930 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3931 off = 0;
3932 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3933 isl_size dim;
3935 if (t != type) {
3936 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3937 } else {
3938 isl_size size = isl_basic_map_dim(bmap, t);
3939 if (size < 0)
3940 dim_map = isl_dim_map_free(dim_map);
3941 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3942 0, pos, off);
3943 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3944 pos, size - pos, off + pos + n);
3946 dim = isl_space_dim(res_space, t);
3947 if (dim < 0)
3948 dim_map = isl_dim_map_free(dim_map);
3949 off += dim;
3951 isl_dim_map_div(dim_map, bmap, off);
3953 res = isl_basic_map_alloc_space(res_space,
3954 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3955 rational = isl_basic_map_is_rational(bmap);
3956 if (rational < 0)
3957 res = isl_basic_map_free(res);
3958 if (rational)
3959 res = isl_basic_map_set_rational(res);
3960 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3961 return isl_basic_map_finalize(res);
3964 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3965 __isl_take isl_basic_set *bset,
3966 enum isl_dim_type type, unsigned pos, unsigned n)
3968 return isl_basic_map_insert_dims(bset, type, pos, n);
3971 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3972 enum isl_dim_type type, unsigned n)
3974 isl_size dim;
3976 dim = isl_basic_map_dim(bmap, type);
3977 if (dim < 0)
3978 return isl_basic_map_free(bmap);
3979 return isl_basic_map_insert_dims(bmap, type, dim, n);
3982 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3983 enum isl_dim_type type, unsigned n)
3985 if (!bset)
3986 return NULL;
3987 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3988 return isl_basic_map_add_dims(bset, type, n);
3989 error:
3990 isl_basic_set_free(bset);
3991 return NULL;
3994 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3995 enum isl_dim_type type)
3997 isl_space *space;
3999 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4000 return map;
4002 space = isl_map_get_space(map);
4003 space = isl_space_reset(space, type);
4004 map = isl_map_reset_space(map, space);
4005 return map;
4008 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4009 enum isl_dim_type type, unsigned pos, unsigned n)
4011 int i;
4012 isl_space *space;
4014 if (n == 0)
4015 return map_space_reset(map, type);
4017 map = isl_map_cow(map);
4018 if (!map)
4019 return NULL;
4021 for (i = 0; i < map->n; ++i) {
4022 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4023 if (!map->p[i])
4024 goto error;
4027 space = isl_map_take_space(map);
4028 space = isl_space_insert_dims(space, type, pos, n);
4029 map = isl_map_restore_space(map, space);
4031 return map;
4032 error:
4033 isl_map_free(map);
4034 return NULL;
4037 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4038 enum isl_dim_type type, unsigned pos, unsigned n)
4040 return isl_map_insert_dims(set, type, pos, n);
4043 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4044 enum isl_dim_type type, unsigned n)
4046 isl_size dim;
4048 dim = isl_map_dim(map, type);
4049 if (dim < 0)
4050 return isl_map_free(map);
4051 return isl_map_insert_dims(map, type, dim, n);
4054 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4055 enum isl_dim_type type, unsigned n)
4057 if (!set)
4058 return NULL;
4059 isl_assert(set->ctx, type != isl_dim_in, goto error);
4060 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4061 error:
4062 isl_set_free(set);
4063 return NULL;
4066 __isl_give isl_basic_map *isl_basic_map_move_dims(
4067 __isl_take isl_basic_map *bmap,
4068 enum isl_dim_type dst_type, unsigned dst_pos,
4069 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4071 isl_space *space;
4072 struct isl_dim_map *dim_map;
4073 struct isl_basic_map *res;
4074 enum isl_dim_type t;
4075 isl_size total;
4076 unsigned off;
4078 if (!bmap)
4079 return NULL;
4080 if (n == 0) {
4081 bmap = isl_basic_map_reset(bmap, src_type);
4082 bmap = isl_basic_map_reset(bmap, dst_type);
4083 return bmap;
4086 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4087 return isl_basic_map_free(bmap);
4089 if (dst_type == src_type && dst_pos == src_pos)
4090 return bmap;
4092 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4094 if (pos(bmap->dim, dst_type) + dst_pos ==
4095 pos(bmap->dim, src_type) + src_pos +
4096 ((src_type < dst_type) ? n : 0)) {
4097 space = isl_basic_map_take_space(bmap);
4098 space = isl_space_move_dims(space, dst_type, dst_pos,
4099 src_type, src_pos, n);
4100 bmap = isl_basic_map_restore_space(bmap, space);
4101 bmap = isl_basic_map_finalize(bmap);
4103 return bmap;
4106 total = isl_basic_map_dim(bmap, isl_dim_all);
4107 if (total < 0)
4108 return isl_basic_map_free(bmap);
4109 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4111 off = 0;
4112 space = isl_basic_map_peek_space(bmap);
4113 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4114 isl_size size = isl_space_dim(space, t);
4115 if (size < 0)
4116 dim_map = isl_dim_map_free(dim_map);
4117 if (t == dst_type) {
4118 isl_dim_map_dim_range(dim_map, space, t,
4119 0, dst_pos, off);
4120 off += dst_pos;
4121 isl_dim_map_dim_range(dim_map, space, src_type,
4122 src_pos, n, off);
4123 off += n;
4124 isl_dim_map_dim_range(dim_map, space, t,
4125 dst_pos, size - dst_pos, off);
4126 off += size - dst_pos;
4127 } else if (t == src_type) {
4128 isl_dim_map_dim_range(dim_map, space, t,
4129 0, src_pos, off);
4130 off += src_pos;
4131 isl_dim_map_dim_range(dim_map, space, t,
4132 src_pos + n, size - src_pos - n, off);
4133 off += size - src_pos - n;
4134 } else {
4135 isl_dim_map_dim(dim_map, space, t, off);
4136 off += size;
4139 isl_dim_map_div(dim_map, bmap, off);
4141 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4142 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4143 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4144 space = isl_basic_map_take_space(bmap);
4145 space = isl_space_move_dims(space, dst_type, dst_pos,
4146 src_type, src_pos, n);
4147 bmap = isl_basic_map_restore_space(bmap, space);
4148 if (!bmap)
4149 goto error;
4151 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4152 bmap = isl_basic_map_gauss(bmap, NULL);
4153 bmap = isl_basic_map_finalize(bmap);
4155 return bmap;
4156 error:
4157 isl_basic_map_free(bmap);
4158 return NULL;
4161 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4162 enum isl_dim_type dst_type, unsigned dst_pos,
4163 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4165 isl_basic_map *bmap = bset_to_bmap(bset);
4166 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4167 src_type, src_pos, n);
4168 return bset_from_bmap(bmap);
4171 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4172 enum isl_dim_type dst_type, unsigned dst_pos,
4173 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4175 if (!set)
4176 return NULL;
4177 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4178 return set_from_map(isl_map_move_dims(set_to_map(set),
4179 dst_type, dst_pos, src_type, src_pos, n));
4180 error:
4181 isl_set_free(set);
4182 return NULL;
4185 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4186 enum isl_dim_type dst_type, unsigned dst_pos,
4187 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4189 int i;
4190 isl_space *space;
4192 if (n == 0) {
4193 map = isl_map_reset(map, src_type);
4194 map = isl_map_reset(map, dst_type);
4195 return map;
4198 if (isl_map_check_range(map, src_type, src_pos, n))
4199 return isl_map_free(map);
4201 if (dst_type == src_type && dst_pos == src_pos)
4202 return map;
4204 isl_assert(map->ctx, dst_type != src_type, goto error);
4206 map = isl_map_cow(map);
4207 if (!map)
4208 return NULL;
4210 for (i = 0; i < map->n; ++i) {
4211 map->p[i] = isl_basic_map_move_dims(map->p[i],
4212 dst_type, dst_pos,
4213 src_type, src_pos, n);
4214 if (!map->p[i])
4215 goto error;
4218 space = isl_map_take_space(map);
4219 space = isl_space_move_dims(space, dst_type, dst_pos,
4220 src_type, src_pos, n);
4221 map = isl_map_restore_space(map, space);
4223 return map;
4224 error:
4225 isl_map_free(map);
4226 return NULL;
4229 /* Move the specified dimensions to the last columns right before
4230 * the divs. Don't change the dimension specification of bmap.
4231 * That's the responsibility of the caller.
4233 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4234 enum isl_dim_type type, unsigned first, unsigned n)
4236 isl_space *space;
4237 struct isl_dim_map *dim_map;
4238 struct isl_basic_map *res;
4239 enum isl_dim_type t;
4240 isl_size total;
4241 unsigned off;
4243 if (!bmap)
4244 return NULL;
4245 if (isl_basic_map_offset(bmap, type) + first + n ==
4246 isl_basic_map_offset(bmap, isl_dim_div))
4247 return bmap;
4249 total = isl_basic_map_dim(bmap, isl_dim_all);
4250 if (total < 0)
4251 return isl_basic_map_free(bmap);
4252 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4254 off = 0;
4255 space = isl_basic_map_peek_space(bmap);
4256 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4257 isl_size size = isl_space_dim(space, t);
4258 if (size < 0)
4259 dim_map = isl_dim_map_free(dim_map);
4260 if (t == type) {
4261 isl_dim_map_dim_range(dim_map, space, t,
4262 0, first, off);
4263 off += first;
4264 isl_dim_map_dim_range(dim_map, space, t,
4265 first, n, total - bmap->n_div - n);
4266 isl_dim_map_dim_range(dim_map, space, t,
4267 first + n, size - (first + n), off);
4268 off += size - (first + n);
4269 } else {
4270 isl_dim_map_dim(dim_map, space, t, off);
4271 off += size;
4274 isl_dim_map_div(dim_map, bmap, off + n);
4276 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4277 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4278 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4279 return res;
4282 /* Insert "n" rows in the divs of "bmap".
4284 * The number of columns is not changed, which means that the last
4285 * dimensions of "bmap" are being reintepreted as the new divs.
4286 * The space of "bmap" is not adjusted, however, which means
4287 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4288 * from the space of "bmap" is the responsibility of the caller.
4290 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4291 int n)
4293 int i;
4294 size_t row_size;
4295 isl_int **new_div;
4296 isl_int *old;
4298 bmap = isl_basic_map_cow(bmap);
4299 if (!bmap)
4300 return NULL;
4302 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4303 old = bmap->block2.data;
4304 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4305 (bmap->extra + n) * (1 + row_size));
4306 if (!bmap->block2.data)
4307 return isl_basic_map_free(bmap);
4308 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4309 if (!new_div)
4310 return isl_basic_map_free(bmap);
4311 for (i = 0; i < n; ++i) {
4312 new_div[i] = bmap->block2.data +
4313 (bmap->extra + i) * (1 + row_size);
4314 isl_seq_clr(new_div[i], 1 + row_size);
4316 for (i = 0; i < bmap->extra; ++i)
4317 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4318 free(bmap->div);
4319 bmap->div = new_div;
4320 bmap->n_div += n;
4321 bmap->extra += n;
4323 return bmap;
4326 /* Drop constraints from "bmap" that only involve the variables
4327 * of "type" in the range [first, first + n] that are not related
4328 * to any of the variables outside that interval.
4329 * These constraints cannot influence the values for the variables
4330 * outside the interval, except in case they cause "bmap" to be empty.
4331 * Only drop the constraints if "bmap" is known to be non-empty.
4333 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4334 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4335 unsigned first, unsigned n)
4337 int i;
4338 int *groups;
4339 isl_size dim, n_div;
4340 isl_bool non_empty;
4342 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4343 if (non_empty < 0)
4344 return isl_basic_map_free(bmap);
4345 if (!non_empty)
4346 return bmap;
4348 dim = isl_basic_map_dim(bmap, isl_dim_all);
4349 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4350 if (dim < 0 || n_div < 0)
4351 return isl_basic_map_free(bmap);
4352 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4353 if (!groups)
4354 return isl_basic_map_free(bmap);
4355 first += isl_basic_map_offset(bmap, type) - 1;
4356 for (i = 0; i < first; ++i)
4357 groups[i] = -1;
4358 for (i = first + n; i < dim - n_div; ++i)
4359 groups[i] = -1;
4361 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4363 return bmap;
4366 /* Turn the n dimensions of type type, starting at first
4367 * into existentially quantified variables.
4369 * If a subset of the projected out variables are unrelated
4370 * to any of the variables that remain, then the constraints
4371 * involving this subset are simply dropped first.
4373 __isl_give isl_basic_map *isl_basic_map_project_out(
4374 __isl_take isl_basic_map *bmap,
4375 enum isl_dim_type type, unsigned first, unsigned n)
4377 isl_bool empty;
4378 isl_space *space;
4380 if (n == 0)
4381 return basic_map_space_reset(bmap, type);
4382 if (type == isl_dim_div)
4383 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4384 "cannot project out existentially quantified variables",
4385 return isl_basic_map_free(bmap));
4387 empty = isl_basic_map_plain_is_empty(bmap);
4388 if (empty < 0)
4389 return isl_basic_map_free(bmap);
4390 if (empty)
4391 bmap = isl_basic_map_set_to_empty(bmap);
4393 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4394 if (!bmap)
4395 return NULL;
4397 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4398 return isl_basic_map_remove_dims(bmap, type, first, n);
4400 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4401 return isl_basic_map_free(bmap);
4403 bmap = move_last(bmap, type, first, n);
4404 bmap = isl_basic_map_cow(bmap);
4405 bmap = insert_div_rows(bmap, n);
4407 space = isl_basic_map_take_space(bmap);
4408 space = isl_space_drop_dims(space, type, first, n);
4409 bmap = isl_basic_map_restore_space(bmap, space);
4410 bmap = isl_basic_map_simplify(bmap);
4411 bmap = isl_basic_map_drop_redundant_divs(bmap);
4412 return isl_basic_map_finalize(bmap);
4415 /* Turn the n dimensions of type type, starting at first
4416 * into existentially quantified variables.
4418 __isl_give isl_basic_set *isl_basic_set_project_out(
4419 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4420 unsigned first, unsigned n)
4422 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4423 type, first, n));
4426 /* Turn the n dimensions of type type, starting at first
4427 * into existentially quantified variables.
4429 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4430 enum isl_dim_type type, unsigned first, unsigned n)
4432 int i;
4433 isl_space *space;
4435 if (n == 0)
4436 return map_space_reset(map, type);
4438 if (isl_map_check_range(map, type, first, n) < 0)
4439 return isl_map_free(map);
4441 map = isl_map_cow(map);
4442 if (!map)
4443 return NULL;
4445 for (i = 0; i < map->n; ++i) {
4446 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4447 if (!map->p[i])
4448 goto error;
4451 if (map->n > 1)
4452 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4453 map = isl_map_unmark_normalized(map);
4455 space = isl_map_take_space(map);
4456 space = isl_space_drop_dims(space, type, first, n);
4457 map = isl_map_restore_space(map, space);
4459 return map;
4460 error:
4461 isl_map_free(map);
4462 return NULL;
4465 #undef TYPE
4466 #define TYPE isl_map
4467 #include "isl_project_out_all_params_templ.c"
4469 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4470 * into existentially quantified variables.
4472 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4473 enum isl_dim_type type, unsigned first, unsigned n)
4475 isl_size dim;
4477 dim = isl_map_dim(map, type);
4478 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4479 return isl_map_free(map);
4480 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4481 map = isl_map_project_out(map, type, 0, first);
4482 return map;
4485 /* Turn the n dimensions of type type, starting at first
4486 * into existentially quantified variables.
4488 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4489 enum isl_dim_type type, unsigned first, unsigned n)
4491 return set_from_map(isl_map_project_out(set_to_map(set),
4492 type, first, n));
4495 /* If "set" involves a parameter with identifier "id",
4496 * then turn it into an existentially quantified variable.
4498 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4499 __isl_take isl_id *id)
4501 int pos;
4503 if (!set || !id)
4504 goto error;
4505 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4506 isl_id_free(id);
4507 if (pos < 0)
4508 return set;
4509 return isl_set_project_out(set, isl_dim_param, pos, 1);
4510 error:
4511 isl_set_free(set);
4512 isl_id_free(id);
4513 return NULL;
4516 /* If "set" involves any of the parameters with identifiers in "list",
4517 * then turn them into existentially quantified variables.
4519 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4520 __isl_take isl_id_list *list)
4522 int i;
4523 isl_size n;
4525 n = isl_id_list_size(list);
4526 if (n < 0)
4527 goto error;
4528 for (i = 0; i < n; ++i) {
4529 isl_id *id;
4531 id = isl_id_list_get_at(list, i);
4532 set = isl_set_project_out_param_id(set, id);
4535 isl_id_list_free(list);
4536 return set;
4537 error:
4538 isl_id_list_free(list);
4539 isl_set_free(set);
4540 return NULL;
4543 /* Project out all parameters from "set" by existentially quantifying
4544 * over them.
4546 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4548 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4551 /* Return a map that projects the elements in "set" onto their
4552 * "n" set dimensions starting at "first".
4553 * "type" should be equal to isl_dim_set.
4555 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4556 enum isl_dim_type type, unsigned first, unsigned n)
4558 int i;
4559 isl_map *map;
4561 if (type != isl_dim_set)
4562 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4563 "only set dimensions can be projected out", goto error);
4564 if (isl_set_check_range(set, type, first, n) < 0)
4565 return isl_set_free(set);
4567 map = isl_map_from_domain(set);
4568 map = isl_map_add_dims(map, isl_dim_out, n);
4569 for (i = 0; i < n; ++i)
4570 map = isl_map_equate(map, isl_dim_in, first + i,
4571 isl_dim_out, i);
4572 return map;
4573 error:
4574 isl_set_free(set);
4575 return NULL;
4578 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4579 unsigned n)
4581 int i, j;
4582 isl_size total;
4584 total = isl_basic_map_dim(bmap, isl_dim_all);
4585 if (total < 0)
4586 return isl_basic_map_free(bmap);
4587 for (i = 0; i < n; ++i) {
4588 j = isl_basic_map_alloc_div(bmap);
4589 if (j < 0)
4590 goto error;
4591 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4593 return bmap;
4594 error:
4595 isl_basic_map_free(bmap);
4596 return NULL;
4599 __isl_give isl_basic_map *isl_basic_map_apply_range(
4600 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4602 isl_space *space_result = NULL;
4603 struct isl_basic_map *bmap;
4604 isl_size n_in, n_out, n, nparam;
4605 unsigned total, pos;
4606 struct isl_dim_map *dim_map1, *dim_map2;
4608 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4609 goto error;
4610 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4611 bmap2->dim, isl_dim_in))
4612 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4613 "spaces don't match", goto error);
4615 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4616 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4617 n = isl_basic_map_dim(bmap1, isl_dim_out);
4618 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4619 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4620 goto error;
4622 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4623 isl_basic_map_get_space(bmap2));
4625 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4626 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4627 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4628 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4629 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4630 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4631 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4632 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4633 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4634 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4635 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4637 bmap = isl_basic_map_alloc_space(space_result,
4638 bmap1->n_div + bmap2->n_div + n,
4639 bmap1->n_eq + bmap2->n_eq,
4640 bmap1->n_ineq + bmap2->n_ineq);
4641 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4642 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4643 bmap = add_divs(bmap, n);
4644 bmap = isl_basic_map_simplify(bmap);
4645 bmap = isl_basic_map_drop_redundant_divs(bmap);
4646 return isl_basic_map_finalize(bmap);
4647 error:
4648 isl_basic_map_free(bmap1);
4649 isl_basic_map_free(bmap2);
4650 return NULL;
4653 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4654 __isl_take isl_basic_map *bmap)
4656 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4657 goto error;
4659 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4660 bmap));
4661 error:
4662 isl_basic_set_free(bset);
4663 isl_basic_map_free(bmap);
4664 return NULL;
4667 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4668 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4670 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4671 goto error;
4672 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4673 bmap2->dim, isl_dim_in))
4674 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4675 "spaces don't match", goto error);
4677 bmap1 = isl_basic_map_reverse(bmap1);
4678 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4679 return isl_basic_map_reverse(bmap1);
4680 error:
4681 isl_basic_map_free(bmap1);
4682 isl_basic_map_free(bmap2);
4683 return NULL;
4686 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4687 * A \cap B -> f(A) + f(B)
4689 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4690 __isl_take isl_basic_map *bmap2)
4692 isl_size n_in, n_out, nparam;
4693 unsigned total, pos;
4694 struct isl_basic_map *bmap = NULL;
4695 struct isl_dim_map *dim_map1, *dim_map2;
4696 int i;
4698 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4699 goto error;
4701 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4702 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4703 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4704 if (nparam < 0 || n_in < 0 || n_out < 0)
4705 goto error;
4707 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4708 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4709 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4710 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4711 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4712 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4713 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4714 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4715 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4716 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4717 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4719 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4720 bmap1->n_div + bmap2->n_div + 2 * n_out,
4721 bmap1->n_eq + bmap2->n_eq + n_out,
4722 bmap1->n_ineq + bmap2->n_ineq);
4723 for (i = 0; i < n_out; ++i) {
4724 int j = isl_basic_map_alloc_equality(bmap);
4725 if (j < 0)
4726 goto error;
4727 isl_seq_clr(bmap->eq[j], 1+total);
4728 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4729 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4730 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4732 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4733 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4734 bmap = add_divs(bmap, 2 * n_out);
4736 bmap = isl_basic_map_simplify(bmap);
4737 return isl_basic_map_finalize(bmap);
4738 error:
4739 isl_basic_map_free(bmap);
4740 isl_basic_map_free(bmap1);
4741 isl_basic_map_free(bmap2);
4742 return NULL;
4745 /* Given two maps A -> f(A) and B -> g(B), construct a map
4746 * A \cap B -> f(A) + f(B)
4748 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4749 __isl_take isl_map *map2)
4751 struct isl_map *result;
4752 int i, j;
4754 if (isl_map_check_equal_space(map1, map2) < 0)
4755 goto error;
4757 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4758 map1->n * map2->n, 0);
4759 if (!result)
4760 goto error;
4761 for (i = 0; i < map1->n; ++i)
4762 for (j = 0; j < map2->n; ++j) {
4763 struct isl_basic_map *part;
4764 part = isl_basic_map_sum(
4765 isl_basic_map_copy(map1->p[i]),
4766 isl_basic_map_copy(map2->p[j]));
4767 if (isl_basic_map_is_empty(part))
4768 isl_basic_map_free(part);
4769 else
4770 result = isl_map_add_basic_map(result, part);
4771 if (!result)
4772 goto error;
4774 isl_map_free(map1);
4775 isl_map_free(map2);
4776 return result;
4777 error:
4778 isl_map_free(map1);
4779 isl_map_free(map2);
4780 return NULL;
4783 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4784 __isl_take isl_set *set2)
4786 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4789 /* Given a basic map A -> f(A), construct A -> -f(A).
4791 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4793 int i, j;
4794 unsigned off;
4795 isl_size n;
4797 bmap = isl_basic_map_cow(bmap);
4798 n = isl_basic_map_dim(bmap, isl_dim_out);
4799 if (n < 0)
4800 return isl_basic_map_free(bmap);
4802 off = isl_basic_map_offset(bmap, isl_dim_out);
4803 for (i = 0; i < bmap->n_eq; ++i)
4804 for (j = 0; j < n; ++j)
4805 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4806 for (i = 0; i < bmap->n_ineq; ++i)
4807 for (j = 0; j < n; ++j)
4808 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4809 for (i = 0; i < bmap->n_div; ++i)
4810 for (j = 0; j < n; ++j)
4811 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4812 bmap = isl_basic_map_gauss(bmap, NULL);
4813 return isl_basic_map_finalize(bmap);
4816 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4818 return isl_basic_map_neg(bset);
4821 /* Given a map A -> f(A), construct A -> -f(A).
4823 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4825 int i;
4827 map = isl_map_cow(map);
4828 if (!map)
4829 return NULL;
4831 for (i = 0; i < map->n; ++i) {
4832 map->p[i] = isl_basic_map_neg(map->p[i]);
4833 if (!map->p[i])
4834 goto error;
4837 return map;
4838 error:
4839 isl_map_free(map);
4840 return NULL;
4843 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4845 return set_from_map(isl_map_neg(set_to_map(set)));
4848 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4849 * A -> floor(f(A)/d).
4851 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4852 isl_int d)
4854 isl_size n_in, n_out, nparam;
4855 unsigned total, pos;
4856 struct isl_basic_map *result = NULL;
4857 struct isl_dim_map *dim_map;
4858 int i;
4860 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4861 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4862 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4863 if (nparam < 0 || n_in < 0 || n_out < 0)
4864 return isl_basic_map_free(bmap);
4866 total = nparam + n_in + n_out + bmap->n_div + n_out;
4867 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4868 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4869 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4870 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4871 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4873 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4874 bmap->n_div + n_out,
4875 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4876 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4877 result = add_divs(result, n_out);
4878 for (i = 0; i < n_out; ++i) {
4879 int j;
4880 j = isl_basic_map_alloc_inequality(result);
4881 if (j < 0)
4882 goto error;
4883 isl_seq_clr(result->ineq[j], 1+total);
4884 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4885 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4886 j = isl_basic_map_alloc_inequality(result);
4887 if (j < 0)
4888 goto error;
4889 isl_seq_clr(result->ineq[j], 1+total);
4890 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4891 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4892 isl_int_sub_ui(result->ineq[j][0], d, 1);
4895 result = isl_basic_map_simplify(result);
4896 return isl_basic_map_finalize(result);
4897 error:
4898 isl_basic_map_free(result);
4899 return NULL;
4902 /* Given a map A -> f(A) and an integer d, construct a map
4903 * A -> floor(f(A)/d).
4905 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4907 int i;
4909 map = isl_map_cow(map);
4910 if (!map)
4911 return NULL;
4913 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4914 for (i = 0; i < map->n; ++i) {
4915 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4916 if (!map->p[i])
4917 goto error;
4919 map = isl_map_unmark_normalized(map);
4921 return map;
4922 error:
4923 isl_map_free(map);
4924 return NULL;
4927 /* Given a map A -> f(A) and an integer d, construct a map
4928 * A -> floor(f(A)/d).
4930 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4931 __isl_take isl_val *d)
4933 if (!map || !d)
4934 goto error;
4935 if (!isl_val_is_int(d))
4936 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4937 "expecting integer denominator", goto error);
4938 map = isl_map_floordiv(map, d->n);
4939 isl_val_free(d);
4940 return map;
4941 error:
4942 isl_map_free(map);
4943 isl_val_free(d);
4944 return NULL;
4947 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4948 unsigned pos)
4950 int i;
4951 isl_size nparam;
4952 isl_size n_in;
4953 isl_size total;
4955 total = isl_basic_map_dim(bmap, isl_dim_all);
4956 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4957 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4958 if (total < 0 || nparam < 0 || n_in < 0)
4959 return isl_basic_map_free(bmap);
4960 i = isl_basic_map_alloc_equality(bmap);
4961 if (i < 0)
4962 goto error;
4963 isl_seq_clr(bmap->eq[i], 1 + total);
4964 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4965 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4966 return isl_basic_map_finalize(bmap);
4967 error:
4968 isl_basic_map_free(bmap);
4969 return NULL;
4972 /* Add a constraint to "bmap" expressing i_pos < o_pos
4974 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4975 unsigned pos)
4977 int i;
4978 isl_size nparam;
4979 isl_size n_in;
4980 isl_size total;
4982 total = isl_basic_map_dim(bmap, isl_dim_all);
4983 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4984 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4985 if (total < 0 || nparam < 0 || n_in < 0)
4986 return isl_basic_map_free(bmap);
4987 i = isl_basic_map_alloc_inequality(bmap);
4988 if (i < 0)
4989 goto error;
4990 isl_seq_clr(bmap->ineq[i], 1 + total);
4991 isl_int_set_si(bmap->ineq[i][0], -1);
4992 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4993 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4994 return isl_basic_map_finalize(bmap);
4995 error:
4996 isl_basic_map_free(bmap);
4997 return NULL;
5000 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5002 static __isl_give isl_basic_map *var_less_or_equal(
5003 __isl_take isl_basic_map *bmap, unsigned pos)
5005 int i;
5006 isl_size nparam;
5007 isl_size n_in;
5008 isl_size total;
5010 total = isl_basic_map_dim(bmap, isl_dim_all);
5011 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5012 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5013 if (total < 0 || nparam < 0 || n_in < 0)
5014 return isl_basic_map_free(bmap);
5015 i = isl_basic_map_alloc_inequality(bmap);
5016 if (i < 0)
5017 goto error;
5018 isl_seq_clr(bmap->ineq[i], 1 + total);
5019 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5020 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5021 return isl_basic_map_finalize(bmap);
5022 error:
5023 isl_basic_map_free(bmap);
5024 return NULL;
5027 /* Add a constraint to "bmap" expressing i_pos > o_pos
5029 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5030 unsigned pos)
5032 int i;
5033 isl_size nparam;
5034 isl_size n_in;
5035 isl_size total;
5037 total = isl_basic_map_dim(bmap, isl_dim_all);
5038 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5039 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5040 if (total < 0 || nparam < 0 || n_in < 0)
5041 return isl_basic_map_free(bmap);
5042 i = isl_basic_map_alloc_inequality(bmap);
5043 if (i < 0)
5044 goto error;
5045 isl_seq_clr(bmap->ineq[i], 1 + total);
5046 isl_int_set_si(bmap->ineq[i][0], -1);
5047 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5048 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5049 return isl_basic_map_finalize(bmap);
5050 error:
5051 isl_basic_map_free(bmap);
5052 return NULL;
5055 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5057 static __isl_give isl_basic_map *var_more_or_equal(
5058 __isl_take isl_basic_map *bmap, unsigned pos)
5060 int i;
5061 isl_size nparam;
5062 isl_size n_in;
5063 isl_size total;
5065 total = isl_basic_map_dim(bmap, isl_dim_all);
5066 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5067 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5068 if (total < 0 || nparam < 0 || n_in < 0)
5069 return isl_basic_map_free(bmap);
5070 i = isl_basic_map_alloc_inequality(bmap);
5071 if (i < 0)
5072 goto error;
5073 isl_seq_clr(bmap->ineq[i], 1 + total);
5074 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5075 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5076 return isl_basic_map_finalize(bmap);
5077 error:
5078 isl_basic_map_free(bmap);
5079 return NULL;
5082 __isl_give isl_basic_map *isl_basic_map_equal(
5083 __isl_take isl_space *space, unsigned n_equal)
5085 int i;
5086 struct isl_basic_map *bmap;
5087 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5088 if (!bmap)
5089 return NULL;
5090 for (i = 0; i < n_equal && bmap; ++i)
5091 bmap = var_equal(bmap, i);
5092 return isl_basic_map_finalize(bmap);
5095 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5097 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5098 unsigned pos)
5100 int i;
5101 struct isl_basic_map *bmap;
5102 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5103 if (!bmap)
5104 return NULL;
5105 for (i = 0; i < pos && bmap; ++i)
5106 bmap = var_equal(bmap, i);
5107 if (bmap)
5108 bmap = var_less(bmap, pos);
5109 return isl_basic_map_finalize(bmap);
5112 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5114 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5115 __isl_take isl_space *space, unsigned pos)
5117 int i;
5118 isl_basic_map *bmap;
5120 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5121 for (i = 0; i < pos; ++i)
5122 bmap = var_equal(bmap, i);
5123 bmap = var_less_or_equal(bmap, pos);
5124 return isl_basic_map_finalize(bmap);
5127 /* Return a relation on "space" expressing i_pos > o_pos
5129 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5130 unsigned pos)
5132 int i;
5133 struct isl_basic_map *bmap;
5134 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5135 if (!bmap)
5136 return NULL;
5137 for (i = 0; i < pos && bmap; ++i)
5138 bmap = var_equal(bmap, i);
5139 if (bmap)
5140 bmap = var_more(bmap, pos);
5141 return isl_basic_map_finalize(bmap);
5144 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5146 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5147 __isl_take isl_space *space, unsigned pos)
5149 int i;
5150 isl_basic_map *bmap;
5152 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5153 for (i = 0; i < pos; ++i)
5154 bmap = var_equal(bmap, i);
5155 bmap = var_more_or_equal(bmap, pos);
5156 return isl_basic_map_finalize(bmap);
5159 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5160 unsigned n, int equal)
5162 struct isl_map *map;
5163 int i;
5165 if (n == 0 && equal)
5166 return isl_map_universe(space);
5168 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5170 for (i = 0; i + 1 < n; ++i)
5171 map = isl_map_add_basic_map(map,
5172 isl_basic_map_less_at(isl_space_copy(space), i));
5173 if (n > 0) {
5174 if (equal)
5175 map = isl_map_add_basic_map(map,
5176 isl_basic_map_less_or_equal_at(space, n - 1));
5177 else
5178 map = isl_map_add_basic_map(map,
5179 isl_basic_map_less_at(space, n - 1));
5180 } else
5181 isl_space_free(space);
5183 return map;
5186 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5188 if (!space)
5189 return NULL;
5190 return map_lex_lte_first(space, space->n_out, equal);
5193 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5194 unsigned n)
5196 return map_lex_lte_first(space, n, 0);
5199 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5200 unsigned n)
5202 return map_lex_lte_first(space, n, 1);
5205 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5207 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5210 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5212 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5215 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5216 unsigned n, int equal)
5218 struct isl_map *map;
5219 int i;
5221 if (n == 0 && equal)
5222 return isl_map_universe(space);
5224 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5226 for (i = 0; i + 1 < n; ++i)
5227 map = isl_map_add_basic_map(map,
5228 isl_basic_map_more_at(isl_space_copy(space), i));
5229 if (n > 0) {
5230 if (equal)
5231 map = isl_map_add_basic_map(map,
5232 isl_basic_map_more_or_equal_at(space, n - 1));
5233 else
5234 map = isl_map_add_basic_map(map,
5235 isl_basic_map_more_at(space, n - 1));
5236 } else
5237 isl_space_free(space);
5239 return map;
5242 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5244 if (!space)
5245 return NULL;
5246 return map_lex_gte_first(space, space->n_out, equal);
5249 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5250 unsigned n)
5252 return map_lex_gte_first(space, n, 0);
5255 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5256 unsigned n)
5258 return map_lex_gte_first(space, n, 1);
5261 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5263 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5266 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5268 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5271 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5272 __isl_take isl_set *set2)
5274 isl_map *map;
5275 map = isl_map_lex_le(isl_set_get_space(set1));
5276 map = isl_map_intersect_domain(map, set1);
5277 map = isl_map_intersect_range(map, set2);
5278 return map;
5281 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5282 __isl_take isl_set *set2)
5284 isl_map *map;
5285 map = isl_map_lex_lt(isl_set_get_space(set1));
5286 map = isl_map_intersect_domain(map, set1);
5287 map = isl_map_intersect_range(map, set2);
5288 return map;
5291 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5292 __isl_take isl_set *set2)
5294 isl_map *map;
5295 map = isl_map_lex_ge(isl_set_get_space(set1));
5296 map = isl_map_intersect_domain(map, set1);
5297 map = isl_map_intersect_range(map, set2);
5298 return map;
5301 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5302 __isl_take isl_set *set2)
5304 isl_map *map;
5305 map = isl_map_lex_gt(isl_set_get_space(set1));
5306 map = isl_map_intersect_domain(map, set1);
5307 map = isl_map_intersect_range(map, set2);
5308 return map;
5311 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5312 __isl_take isl_map *map2)
5314 isl_map *map;
5315 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5316 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5317 map = isl_map_apply_range(map, isl_map_reverse(map2));
5318 return map;
5321 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5322 __isl_take isl_map *map2)
5324 isl_map *map;
5325 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5326 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5327 map = isl_map_apply_range(map, isl_map_reverse(map2));
5328 return map;
5331 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5332 __isl_take isl_map *map2)
5334 isl_map *map;
5335 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5336 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5337 map = isl_map_apply_range(map, isl_map_reverse(map2));
5338 return map;
5341 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5342 __isl_take isl_map *map2)
5344 isl_map *map;
5345 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5346 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5347 map = isl_map_apply_range(map, isl_map_reverse(map2));
5348 return map;
5351 /* For the div d = floor(f/m) at position "div", add the constraint
5353 * f - m d >= 0
5355 static __isl_give isl_basic_map *add_upper_div_constraint(
5356 __isl_take isl_basic_map *bmap, unsigned div)
5358 int i;
5359 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5360 isl_size n_div;
5361 unsigned pos;
5363 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5364 if (v_div < 0 || n_div < 0)
5365 return isl_basic_map_free(bmap);
5366 pos = v_div + div;
5367 i = isl_basic_map_alloc_inequality(bmap);
5368 if (i < 0)
5369 return isl_basic_map_free(bmap);
5370 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5371 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5373 return bmap;
5376 /* For the div d = floor(f/m) at position "div", add the constraint
5378 * -(f-(m-1)) + m d >= 0
5380 static __isl_give isl_basic_map *add_lower_div_constraint(
5381 __isl_take isl_basic_map *bmap, unsigned div)
5383 int i;
5384 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5385 isl_size n_div;
5386 unsigned pos;
5388 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5389 if (v_div < 0 || n_div < 0)
5390 return isl_basic_map_free(bmap);
5391 pos = v_div + div;
5392 i = isl_basic_map_alloc_inequality(bmap);
5393 if (i < 0)
5394 return isl_basic_map_free(bmap);
5395 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5396 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5397 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5398 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5400 return bmap;
5403 /* For the div d = floor(f/m) at position "pos", add the constraints
5405 * f - m d >= 0
5406 * -(f-(m-1)) + m d >= 0
5408 * Note that the second constraint is the negation of
5410 * f - m d >= m
5412 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5413 __isl_take isl_basic_map *bmap, unsigned pos)
5415 bmap = add_upper_div_constraint(bmap, pos);
5416 bmap = add_lower_div_constraint(bmap, pos);
5417 return bmap;
5420 /* For each known div d = floor(f/m), add the constraints
5422 * f - m d >= 0
5423 * -(f-(m-1)) + m d >= 0
5425 * Remove duplicate constraints in case of some these div constraints
5426 * already appear in "bmap".
5428 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5429 __isl_take isl_basic_map *bmap)
5431 isl_size n_div;
5433 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5434 if (n_div < 0)
5435 return isl_basic_map_free(bmap);
5436 if (n_div == 0)
5437 return bmap;
5439 bmap = add_known_div_constraints(bmap);
5440 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5441 bmap = isl_basic_map_finalize(bmap);
5442 return bmap;
5445 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5447 * In particular, if this div is of the form d = floor(f/m),
5448 * then add the constraint
5450 * f - m d >= 0
5452 * if sign < 0 or the constraint
5454 * -(f-(m-1)) + m d >= 0
5456 * if sign > 0.
5458 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5459 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5461 if (sign < 0)
5462 return add_upper_div_constraint(bmap, div);
5463 else
5464 return add_lower_div_constraint(bmap, div);
5467 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5468 __isl_take isl_basic_map *bmap)
5470 isl_space *space;
5472 if (!bmap)
5473 goto error;
5474 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5475 bmap->n_div == 0 &&
5476 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5477 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5478 return bset_from_bmap(bmap);
5479 bmap = isl_basic_map_cow(bmap);
5480 if (!bmap)
5481 return NULL;
5482 space = isl_basic_map_take_space(bmap);
5483 space = isl_space_underlying(space, bmap->n_div);
5484 bmap = isl_basic_map_restore_space(bmap, space);
5485 if (!bmap)
5486 return NULL;
5487 bmap->extra -= bmap->n_div;
5488 bmap->n_div = 0;
5489 bmap = isl_basic_map_finalize(bmap);
5490 return bset_from_bmap(bmap);
5491 error:
5492 isl_basic_map_free(bmap);
5493 return NULL;
5496 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5497 __isl_take isl_basic_set *bset)
5499 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5502 /* Replace each element in "list" by the result of applying
5503 * isl_basic_map_underlying_set to the element.
5505 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5506 __isl_take isl_basic_map_list *list)
5508 int i;
5509 isl_size n;
5511 n = isl_basic_map_list_n_basic_map(list);
5512 if (n < 0)
5513 goto error;
5515 for (i = 0; i < n; ++i) {
5516 isl_basic_map *bmap;
5517 isl_basic_set *bset;
5519 bmap = isl_basic_map_list_get_basic_map(list, i);
5520 bset = isl_basic_set_underlying_set(bmap);
5521 list = isl_basic_set_list_set_basic_set(list, i, bset);
5524 return list;
5525 error:
5526 isl_basic_map_list_free(list);
5527 return NULL;
5530 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5531 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5533 struct isl_basic_map *bmap;
5534 struct isl_ctx *ctx;
5535 isl_size dim, bmap_total;
5536 unsigned total;
5537 int i;
5539 if (!bset || !like)
5540 goto error;
5541 ctx = bset->ctx;
5542 if (isl_basic_set_check_no_params(bset) < 0 ||
5543 isl_basic_set_check_no_locals(bset) < 0)
5544 goto error;
5545 dim = isl_basic_set_dim(bset, isl_dim_set);
5546 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5547 if (dim < 0 || bmap_total < 0)
5548 goto error;
5549 isl_assert(ctx, dim == bmap_total, goto error);
5550 if (like->n_div == 0) {
5551 isl_space *space = isl_basic_map_get_space(like);
5552 isl_basic_map_free(like);
5553 return isl_basic_map_reset_space(bset, space);
5555 bset = isl_basic_set_cow(bset);
5556 if (!bset)
5557 goto error;
5558 total = dim + bset->extra;
5559 bmap = bset_to_bmap(bset);
5560 isl_space_free(isl_basic_map_take_space(bmap));
5561 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5562 if (!bmap)
5563 goto error;
5564 bmap->n_div = like->n_div;
5565 bmap->extra += like->n_div;
5566 if (bmap->extra) {
5567 unsigned ltotal;
5568 isl_int **div;
5569 ltotal = total - bmap->extra + like->extra;
5570 if (ltotal > total)
5571 ltotal = total;
5572 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5573 bmap->extra * (1 + 1 + total));
5574 if (isl_blk_is_error(bmap->block2))
5575 goto error;
5576 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5577 if (!div)
5578 goto error;
5579 bmap->div = div;
5580 for (i = 0; i < bmap->extra; ++i)
5581 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5582 for (i = 0; i < like->n_div; ++i) {
5583 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5584 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5586 bmap = isl_basic_map_add_known_div_constraints(bmap);
5588 isl_basic_map_free(like);
5589 bmap = isl_basic_map_simplify(bmap);
5590 bmap = isl_basic_map_finalize(bmap);
5591 return bmap;
5592 error:
5593 isl_basic_map_free(like);
5594 isl_basic_set_free(bset);
5595 return NULL;
5598 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5599 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5601 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5602 bset_to_bmap(like)));
5605 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5607 int i;
5609 map = isl_map_cow(map);
5610 if (!map)
5611 return NULL;
5612 map->dim = isl_space_cow(map->dim);
5613 if (!map->dim)
5614 goto error;
5616 for (i = 1; i < map->n; ++i)
5617 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5618 goto error);
5619 for (i = 0; i < map->n; ++i) {
5620 map->p[i] = bset_to_bmap(
5621 isl_basic_map_underlying_set(map->p[i]));
5622 if (!map->p[i])
5623 goto error;
5625 if (map->n == 0)
5626 map->dim = isl_space_underlying(map->dim, 0);
5627 else {
5628 isl_space_free(map->dim);
5629 map->dim = isl_space_copy(map->p[0]->dim);
5631 if (!map->dim)
5632 goto error;
5633 return set_from_map(map);
5634 error:
5635 isl_map_free(map);
5636 return NULL;
5639 /* Replace the space of "bmap" by "space".
5641 * If the space of "bmap" is identical to "space" (including the identifiers
5642 * of the input and output dimensions), then simply return the original input.
5644 __isl_give isl_basic_map *isl_basic_map_reset_space(
5645 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5647 isl_bool equal;
5648 isl_space *bmap_space;
5650 bmap_space = isl_basic_map_peek_space(bmap);
5651 equal = isl_space_is_equal(bmap_space, space);
5652 if (equal >= 0 && equal)
5653 equal = isl_space_has_equal_ids(bmap_space, space);
5654 if (equal < 0)
5655 goto error;
5656 if (equal) {
5657 isl_space_free(space);
5658 return bmap;
5660 isl_space_free(isl_basic_map_take_space(bmap));
5661 bmap = isl_basic_map_restore_space(bmap, space);
5663 bmap = isl_basic_map_finalize(bmap);
5665 return bmap;
5666 error:
5667 isl_basic_map_free(bmap);
5668 isl_space_free(space);
5669 return NULL;
5672 __isl_give isl_basic_set *isl_basic_set_reset_space(
5673 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5675 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5676 space));
5679 /* Check that the total dimensions of "map" and "space" are the same.
5681 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5682 __isl_keep isl_space *space)
5684 isl_size dim1, dim2;
5686 dim1 = isl_map_dim(map, isl_dim_all);
5687 dim2 = isl_space_dim(space, isl_dim_all);
5688 if (dim1 < 0 || dim2 < 0)
5689 return isl_stat_error;
5690 if (dim1 == dim2)
5691 return isl_stat_ok;
5692 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5693 "total dimensions do not match", return isl_stat_error);
5696 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5697 __isl_take isl_space *space)
5699 int i;
5701 map = isl_map_cow(map);
5702 if (!map || !space)
5703 goto error;
5705 for (i = 0; i < map->n; ++i) {
5706 map->p[i] = isl_basic_map_reset_space(map->p[i],
5707 isl_space_copy(space));
5708 if (!map->p[i])
5709 goto error;
5711 isl_space_free(isl_map_take_space(map));
5712 map = isl_map_restore_space(map, space);
5714 return map;
5715 error:
5716 isl_map_free(map);
5717 isl_space_free(space);
5718 return NULL;
5721 /* Replace the space of "map" by "space", without modifying
5722 * the dimension of "map".
5724 * If the space of "map" is identical to "space" (including the identifiers
5725 * of the input and output dimensions), then simply return the original input.
5727 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5728 __isl_take isl_space *space)
5730 isl_bool equal;
5731 isl_space *map_space;
5733 map_space = isl_map_peek_space(map);
5734 equal = isl_space_is_equal(map_space, space);
5735 if (equal >= 0 && equal)
5736 equal = isl_space_has_equal_ids(map_space, space);
5737 if (equal < 0)
5738 goto error;
5739 if (equal) {
5740 isl_space_free(space);
5741 return map;
5743 if (check_map_space_equal_total_dim(map, space) < 0)
5744 goto error;
5745 return isl_map_reset_space(map, space);
5746 error:
5747 isl_map_free(map);
5748 isl_space_free(space);
5749 return NULL;
5752 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5753 __isl_take isl_space *space)
5755 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5758 /* Compute the parameter domain of the given basic set.
5760 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5762 isl_bool is_params;
5763 isl_space *space;
5764 isl_size n;
5766 is_params = isl_basic_set_is_params(bset);
5767 if (is_params < 0)
5768 return isl_basic_set_free(bset);
5769 if (is_params)
5770 return bset;
5772 n = isl_basic_set_dim(bset, isl_dim_set);
5773 if (n < 0)
5774 return isl_basic_set_free(bset);
5775 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5776 space = isl_basic_set_get_space(bset);
5777 space = isl_space_params(space);
5778 bset = isl_basic_set_reset_space(bset, space);
5779 return bset;
5782 /* Construct a zero-dimensional basic set with the given parameter domain.
5784 __isl_give isl_basic_set *isl_basic_set_from_params(
5785 __isl_take isl_basic_set *bset)
5787 isl_space *space;
5788 space = isl_basic_set_get_space(bset);
5789 space = isl_space_set_from_params(space);
5790 bset = isl_basic_set_reset_space(bset, space);
5791 return bset;
5794 /* Compute the parameter domain of the given set.
5796 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5798 return isl_map_params(set_to_map(set));
5801 /* Construct a zero-dimensional set with the given parameter domain.
5803 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5805 isl_space *space;
5806 space = isl_set_get_space(set);
5807 space = isl_space_set_from_params(space);
5808 set = isl_set_reset_space(set, space);
5809 return set;
5812 /* Compute the parameter domain of the given map.
5814 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5816 isl_space *space;
5817 isl_size n_in, n_out;
5819 n_in = isl_map_dim(map, isl_dim_in);
5820 n_out = isl_map_dim(map, isl_dim_out);
5821 if (n_in < 0 || n_out < 0)
5822 return isl_map_free(map);
5823 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5824 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5825 space = isl_map_get_space(map);
5826 space = isl_space_params(space);
5827 map = isl_map_reset_space(map, space);
5828 return map;
5831 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5833 isl_space *space;
5834 isl_size n_out;
5836 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5837 if (n_out < 0)
5838 return isl_basic_map_free(bmap);
5839 space = isl_space_domain(isl_basic_map_get_space(bmap));
5841 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5843 return isl_basic_map_reset_space(bmap, space);
5846 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5848 if (!bmap)
5849 return isl_bool_error;
5850 return isl_space_may_be_set(bmap->dim);
5853 /* Is this basic map actually a set?
5854 * Users should never call this function. Outside of isl,
5855 * the type should indicate whether something is a set or a map.
5857 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5859 if (!bmap)
5860 return isl_bool_error;
5861 return isl_space_is_set(bmap->dim);
5864 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5866 isl_bool is_set;
5868 is_set = isl_basic_map_is_set(bmap);
5869 if (is_set < 0)
5870 goto error;
5871 if (is_set)
5872 return bmap;
5873 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5874 error:
5875 isl_basic_map_free(bmap);
5876 return NULL;
5879 __isl_give isl_basic_map *isl_basic_map_domain_map(
5880 __isl_take isl_basic_map *bmap)
5882 int i;
5883 isl_space *space;
5884 isl_basic_map *domain;
5885 isl_size nparam, n_in, n_out;
5887 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5888 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5889 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5890 if (nparam < 0 || n_in < 0 || n_out < 0)
5891 return isl_basic_map_free(bmap);
5893 space = isl_basic_map_get_space(bmap);
5894 space = isl_space_from_range(isl_space_domain(space));
5895 domain = isl_basic_map_universe(space);
5897 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5898 bmap = isl_basic_map_apply_range(bmap, domain);
5899 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5901 for (i = 0; i < n_in; ++i)
5902 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5903 isl_dim_out, i);
5905 bmap = isl_basic_map_gauss(bmap, NULL);
5906 return isl_basic_map_finalize(bmap);
5909 __isl_give isl_basic_map *isl_basic_map_range_map(
5910 __isl_take isl_basic_map *bmap)
5912 int i;
5913 isl_space *space;
5914 isl_basic_map *range;
5915 isl_size nparam, n_in, n_out;
5917 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5918 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5919 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5920 if (nparam < 0 || n_in < 0 || n_out < 0)
5921 return isl_basic_map_free(bmap);
5923 space = isl_basic_map_get_space(bmap);
5924 space = isl_space_from_range(isl_space_range(space));
5925 range = isl_basic_map_universe(space);
5927 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5928 bmap = isl_basic_map_apply_range(bmap, range);
5929 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5931 for (i = 0; i < n_out; ++i)
5932 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5933 isl_dim_out, i);
5935 bmap = isl_basic_map_gauss(bmap, NULL);
5936 return isl_basic_map_finalize(bmap);
5939 int isl_map_may_be_set(__isl_keep isl_map *map)
5941 if (!map)
5942 return -1;
5943 return isl_space_may_be_set(map->dim);
5946 /* Is this map actually a set?
5947 * Users should never call this function. Outside of isl,
5948 * the type should indicate whether something is a set or a map.
5950 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5952 if (!map)
5953 return isl_bool_error;
5954 return isl_space_is_set(map->dim);
5957 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5959 int i;
5960 isl_bool is_set;
5961 struct isl_set *set;
5963 is_set = isl_map_is_set(map);
5964 if (is_set < 0)
5965 goto error;
5966 if (is_set)
5967 return set_from_map(map);
5969 map = isl_map_cow(map);
5970 if (!map)
5971 goto error;
5973 set = set_from_map(map);
5974 set->dim = isl_space_range(set->dim);
5975 if (!set->dim)
5976 goto error;
5977 for (i = 0; i < map->n; ++i) {
5978 set->p[i] = isl_basic_map_range(map->p[i]);
5979 if (!set->p[i])
5980 goto error;
5982 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5983 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5984 return set;
5985 error:
5986 isl_map_free(map);
5987 return NULL;
5990 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
5991 * to each of its basic maps.
5993 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
5994 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
5995 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
5997 int i;
5998 isl_space *space;
6000 map = isl_map_cow(map);
6001 if (!map)
6002 return NULL;
6004 for (i = 0; i < map->n; ++i) {
6005 map->p[i] = fn_bmap(map->p[i]);
6006 if (!map->p[i])
6007 return isl_map_free(map);
6009 map = isl_map_unmark_normalized(map);
6011 space = isl_map_take_space(map);
6012 space = fn_space(space);
6013 map = isl_map_restore_space(map, space);
6015 return map;
6018 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6020 return isl_map_transform(map, &isl_space_domain_map,
6021 &isl_basic_map_domain_map);
6024 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6026 return isl_map_transform(map, &isl_space_range_map,
6027 &isl_basic_map_range_map);
6030 /* Given a wrapped map of the form A[B -> C],
6031 * return the map A[B -> C] -> B.
6033 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6035 isl_id *id;
6036 isl_map *map;
6038 if (!set)
6039 return NULL;
6040 if (!isl_set_has_tuple_id(set))
6041 return isl_map_domain_map(isl_set_unwrap(set));
6043 id = isl_set_get_tuple_id(set);
6044 map = isl_map_domain_map(isl_set_unwrap(set));
6045 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6047 return map;
6050 __isl_give isl_basic_map *isl_basic_map_from_domain(
6051 __isl_take isl_basic_set *bset)
6053 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6056 __isl_give isl_basic_map *isl_basic_map_from_range(
6057 __isl_take isl_basic_set *bset)
6059 isl_space *space;
6060 space = isl_basic_set_get_space(bset);
6061 space = isl_space_from_range(space);
6062 bset = isl_basic_set_reset_space(bset, space);
6063 return bset_to_bmap(bset);
6066 /* Create a relation with the given set as range.
6067 * The domain of the created relation is a zero-dimensional
6068 * flat anonymous space.
6070 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6072 isl_space *space;
6073 space = isl_set_get_space(set);
6074 space = isl_space_from_range(space);
6075 set = isl_set_reset_space(set, space);
6076 return set_to_map(set);
6079 /* Create a relation with the given set as domain.
6080 * The range of the created relation is a zero-dimensional
6081 * flat anonymous space.
6083 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6085 return isl_map_reverse(isl_map_from_range(set));
6088 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6089 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6091 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6094 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6095 __isl_take isl_set *range)
6097 return isl_map_apply_range(isl_map_reverse(domain), range);
6100 /* Return a newly allocated isl_map with given space and flags and
6101 * room for "n" basic maps.
6102 * Make sure that all cached information is cleared.
6104 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6105 unsigned flags)
6107 struct isl_map *map;
6109 if (!space)
6110 return NULL;
6111 if (n < 0)
6112 isl_die(space->ctx, isl_error_internal,
6113 "negative number of basic maps", goto error);
6114 map = isl_calloc(space->ctx, struct isl_map,
6115 sizeof(struct isl_map) +
6116 (n - 1) * sizeof(struct isl_basic_map *));
6117 if (!map)
6118 goto error;
6120 map->ctx = space->ctx;
6121 isl_ctx_ref(map->ctx);
6122 map->ref = 1;
6123 map->size = n;
6124 map->n = 0;
6125 map->dim = space;
6126 map->flags = flags;
6127 return map;
6128 error:
6129 isl_space_free(space);
6130 return NULL;
6133 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6135 struct isl_basic_map *bmap;
6136 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6137 bmap = isl_basic_map_set_to_empty(bmap);
6138 return bmap;
6141 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6143 struct isl_basic_set *bset;
6144 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6145 bset = isl_basic_set_set_to_empty(bset);
6146 return bset;
6149 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6151 struct isl_basic_map *bmap;
6152 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6153 bmap = isl_basic_map_finalize(bmap);
6154 return bmap;
6157 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6159 struct isl_basic_set *bset;
6160 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6161 bset = isl_basic_set_finalize(bset);
6162 return bset;
6165 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6166 __isl_take isl_space *space)
6168 int i;
6169 isl_size total = isl_space_dim(space, isl_dim_all);
6170 isl_basic_map *bmap;
6172 if (total < 0)
6173 space = isl_space_free(space);
6174 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6175 for (i = 0; i < total; ++i) {
6176 int k = isl_basic_map_alloc_inequality(bmap);
6177 if (k < 0)
6178 goto error;
6179 isl_seq_clr(bmap->ineq[k], 1 + total);
6180 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6182 return bmap;
6183 error:
6184 isl_basic_map_free(bmap);
6185 return NULL;
6188 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6189 __isl_take isl_space *space)
6191 return isl_basic_map_nat_universe(space);
6194 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6196 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6199 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6201 return isl_map_nat_universe(space);
6204 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6206 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6209 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6211 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6214 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6216 struct isl_map *map;
6217 if (!space)
6218 return NULL;
6219 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6220 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6221 return map;
6224 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6226 struct isl_set *set;
6227 if (!space)
6228 return NULL;
6229 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6230 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6231 return set;
6234 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6236 int i;
6237 struct isl_map *dup;
6239 if (!map)
6240 return NULL;
6241 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6242 for (i = 0; i < map->n; ++i)
6243 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6244 return dup;
6247 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6248 __isl_take isl_basic_map *bmap)
6250 if (!bmap || !map)
6251 goto error;
6252 if (isl_basic_map_plain_is_empty(bmap)) {
6253 isl_basic_map_free(bmap);
6254 return map;
6256 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6257 goto error;
6258 isl_assert(map->ctx, map->n < map->size, goto error);
6259 map->p[map->n] = bmap;
6260 map->n++;
6261 map = isl_map_unmark_normalized(map);
6262 return map;
6263 error:
6264 if (map)
6265 isl_map_free(map);
6266 if (bmap)
6267 isl_basic_map_free(bmap);
6268 return NULL;
6271 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6273 int i;
6275 if (!map)
6276 return NULL;
6278 if (--map->ref > 0)
6279 return NULL;
6281 clear_caches(map);
6282 isl_ctx_deref(map->ctx);
6283 for (i = 0; i < map->n; ++i)
6284 isl_basic_map_free(map->p[i]);
6285 isl_space_free(map->dim);
6286 free(map);
6288 return NULL;
6291 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6292 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6294 int j;
6295 isl_size total;
6297 total = isl_basic_map_dim(bmap, isl_dim_all);
6298 if (total < 0)
6299 return isl_basic_map_free(bmap);
6301 bmap = isl_basic_map_cow(bmap);
6302 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6303 j = isl_basic_map_alloc_equality(bmap);
6304 if (j < 0)
6305 goto error;
6306 isl_seq_clr(bmap->eq[j] + 1, total);
6307 isl_int_set_si(bmap->eq[j][pos], -1);
6308 isl_int_set_si(bmap->eq[j][0], value);
6309 bmap = isl_basic_map_simplify(bmap);
6310 return isl_basic_map_finalize(bmap);
6311 error:
6312 isl_basic_map_free(bmap);
6313 return NULL;
6316 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6317 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6319 int j;
6320 isl_size total;
6322 total = isl_basic_map_dim(bmap, isl_dim_all);
6323 if (total < 0)
6324 return isl_basic_map_free(bmap);
6326 bmap = isl_basic_map_cow(bmap);
6327 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6328 j = isl_basic_map_alloc_equality(bmap);
6329 if (j < 0)
6330 goto error;
6331 isl_seq_clr(bmap->eq[j] + 1, total);
6332 isl_int_set_si(bmap->eq[j][pos], -1);
6333 isl_int_set(bmap->eq[j][0], value);
6334 bmap = isl_basic_map_simplify(bmap);
6335 return isl_basic_map_finalize(bmap);
6336 error:
6337 isl_basic_map_free(bmap);
6338 return NULL;
6341 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6342 enum isl_dim_type type, unsigned pos, int value)
6344 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6345 return isl_basic_map_free(bmap);
6346 return isl_basic_map_fix_pos_si(bmap,
6347 isl_basic_map_offset(bmap, type) + pos, value);
6350 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6351 enum isl_dim_type type, unsigned pos, isl_int value)
6353 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6354 return isl_basic_map_free(bmap);
6355 return isl_basic_map_fix_pos(bmap,
6356 isl_basic_map_offset(bmap, type) + pos, value);
6359 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6360 * to be equal to "v".
6362 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6363 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6365 if (!bmap || !v)
6366 goto error;
6367 if (!isl_val_is_int(v))
6368 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6369 "expecting integer value", goto error);
6370 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6371 goto error;
6372 pos += isl_basic_map_offset(bmap, type);
6373 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6374 isl_val_free(v);
6375 return bmap;
6376 error:
6377 isl_basic_map_free(bmap);
6378 isl_val_free(v);
6379 return NULL;
6382 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6383 * to be equal to "v".
6385 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6386 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6388 return isl_basic_map_fix_val(bset, type, pos, v);
6391 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6392 enum isl_dim_type type, unsigned pos, int value)
6394 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6395 type, pos, value));
6398 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6399 enum isl_dim_type type, unsigned pos, isl_int value)
6401 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6402 type, pos, value));
6405 /* Remove the basic map at position "i" from "map" if this basic map
6406 * is (obviously) empty.
6408 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6410 isl_bool empty;
6412 if (!map)
6413 return NULL;
6415 empty = isl_basic_map_plain_is_empty(map->p[i]);
6416 if (empty < 0)
6417 return isl_map_free(map);
6418 if (!empty)
6419 return map;
6421 isl_basic_map_free(map->p[i]);
6422 map->n--;
6423 if (i != map->n) {
6424 map->p[i] = map->p[map->n];
6425 map = isl_map_unmark_normalized(map);
6429 return map;
6432 /* Perform "fn" on each basic map of "map", where we may not be holding
6433 * the only reference to "map".
6434 * In particular, "fn" should be a semantics preserving operation
6435 * that we want to apply to all copies of "map". We therefore need
6436 * to be careful not to modify "map" in a way that breaks "map"
6437 * in case anything goes wrong.
6439 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6440 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6442 struct isl_basic_map *bmap;
6443 int i;
6445 if (!map)
6446 return NULL;
6448 for (i = map->n - 1; i >= 0; --i) {
6449 bmap = isl_basic_map_copy(map->p[i]);
6450 bmap = fn(bmap);
6451 if (!bmap)
6452 goto error;
6453 isl_basic_map_free(map->p[i]);
6454 map->p[i] = bmap;
6455 map = remove_if_empty(map, i);
6456 if (!map)
6457 return NULL;
6460 return map;
6461 error:
6462 isl_map_free(map);
6463 return NULL;
6466 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, int value)
6469 int i;
6471 map = isl_map_cow(map);
6472 if (isl_map_check_range(map, type, pos, 1) < 0)
6473 return isl_map_free(map);
6474 for (i = map->n - 1; i >= 0; --i) {
6475 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6476 map = remove_if_empty(map, i);
6477 if (!map)
6478 return NULL;
6480 map = isl_map_unmark_normalized(map);
6481 return map;
6484 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6485 enum isl_dim_type type, unsigned pos, int value)
6487 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6490 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6491 enum isl_dim_type type, unsigned pos, isl_int value)
6493 int i;
6495 map = isl_map_cow(map);
6496 if (isl_map_check_range(map, type, pos, 1) < 0)
6497 return isl_map_free(map);
6498 for (i = 0; i < map->n; ++i) {
6499 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6500 if (!map->p[i])
6501 goto error;
6503 map = isl_map_unmark_normalized(map);
6504 return map;
6505 error:
6506 isl_map_free(map);
6507 return NULL;
6510 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6511 enum isl_dim_type type, unsigned pos, isl_int value)
6513 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6516 /* Fix the value of the variable at position "pos" of type "type" of "map"
6517 * to be equal to "v".
6519 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6520 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6522 int i;
6524 map = isl_map_cow(map);
6525 if (!map || !v)
6526 goto error;
6528 if (!isl_val_is_int(v))
6529 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6530 "expecting integer value", goto error);
6531 if (isl_map_check_range(map, type, pos, 1) < 0)
6532 goto error;
6533 for (i = map->n - 1; i >= 0; --i) {
6534 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6535 isl_val_copy(v));
6536 map = remove_if_empty(map, i);
6537 if (!map)
6538 goto error;
6540 map = isl_map_unmark_normalized(map);
6541 isl_val_free(v);
6542 return map;
6543 error:
6544 isl_map_free(map);
6545 isl_val_free(v);
6546 return NULL;
6549 /* Fix the value of the variable at position "pos" of type "type" of "set"
6550 * to be equal to "v".
6552 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6553 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6555 return isl_map_fix_val(set, type, pos, v);
6558 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6559 unsigned input, int value)
6561 return isl_map_fix_si(map, isl_dim_in, input, value);
6564 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6565 int value)
6567 return set_from_map(isl_map_fix_si(set_to_map(set),
6568 isl_dim_set, dim, value));
6571 static __isl_give isl_basic_map *basic_map_bound_si(
6572 __isl_take isl_basic_map *bmap,
6573 enum isl_dim_type type, unsigned pos, int value, int upper)
6575 int j;
6576 isl_size total;
6578 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6579 return isl_basic_map_free(bmap);
6580 total = isl_basic_map_dim(bmap, isl_dim_all);
6581 if (total < 0)
6582 return isl_basic_map_free(bmap);
6583 pos += isl_basic_map_offset(bmap, type);
6584 bmap = isl_basic_map_cow(bmap);
6585 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6586 j = isl_basic_map_alloc_inequality(bmap);
6587 if (j < 0)
6588 goto error;
6589 isl_seq_clr(bmap->ineq[j], 1 + total);
6590 if (upper) {
6591 isl_int_set_si(bmap->ineq[j][pos], -1);
6592 isl_int_set_si(bmap->ineq[j][0], value);
6593 } else {
6594 isl_int_set_si(bmap->ineq[j][pos], 1);
6595 isl_int_set_si(bmap->ineq[j][0], -value);
6597 bmap = isl_basic_map_simplify(bmap);
6598 return isl_basic_map_finalize(bmap);
6599 error:
6600 isl_basic_map_free(bmap);
6601 return NULL;
6604 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6605 __isl_take isl_basic_map *bmap,
6606 enum isl_dim_type type, unsigned pos, int value)
6608 return basic_map_bound_si(bmap, type, pos, value, 0);
6611 /* Constrain the values of the given dimension to be no greater than "value".
6613 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6614 __isl_take isl_basic_map *bmap,
6615 enum isl_dim_type type, unsigned pos, int value)
6617 return basic_map_bound_si(bmap, type, pos, value, 1);
6620 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6621 enum isl_dim_type type, unsigned pos, int value, int upper)
6623 int i;
6625 map = isl_map_cow(map);
6626 if (isl_map_check_range(map, type, pos, 1) < 0)
6627 return isl_map_free(map);
6628 for (i = 0; i < map->n; ++i) {
6629 map->p[i] = basic_map_bound_si(map->p[i],
6630 type, pos, value, upper);
6631 if (!map->p[i])
6632 goto error;
6634 map = isl_map_unmark_normalized(map);
6635 return map;
6636 error:
6637 isl_map_free(map);
6638 return NULL;
6641 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6642 enum isl_dim_type type, unsigned pos, int value)
6644 return map_bound_si(map, type, pos, value, 0);
6647 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6648 enum isl_dim_type type, unsigned pos, int value)
6650 return map_bound_si(map, type, pos, value, 1);
6653 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6654 enum isl_dim_type type, unsigned pos, int value)
6656 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6657 type, pos, value));
6660 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6661 enum isl_dim_type type, unsigned pos, int value)
6663 return isl_map_upper_bound_si(set, type, pos, value);
6666 /* Bound the given variable of "bmap" from below (or above is "upper"
6667 * is set) to "value".
6669 static __isl_give isl_basic_map *basic_map_bound(
6670 __isl_take isl_basic_map *bmap,
6671 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6673 int j;
6674 isl_size total;
6676 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6677 return isl_basic_map_free(bmap);
6678 total = isl_basic_map_dim(bmap, isl_dim_all);
6679 if (total < 0)
6680 return isl_basic_map_free(bmap);
6681 pos += isl_basic_map_offset(bmap, type);
6682 bmap = isl_basic_map_cow(bmap);
6683 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6684 j = isl_basic_map_alloc_inequality(bmap);
6685 if (j < 0)
6686 goto error;
6687 isl_seq_clr(bmap->ineq[j], 1 + total);
6688 if (upper) {
6689 isl_int_set_si(bmap->ineq[j][pos], -1);
6690 isl_int_set(bmap->ineq[j][0], value);
6691 } else {
6692 isl_int_set_si(bmap->ineq[j][pos], 1);
6693 isl_int_neg(bmap->ineq[j][0], value);
6695 bmap = isl_basic_map_simplify(bmap);
6696 return isl_basic_map_finalize(bmap);
6697 error:
6698 isl_basic_map_free(bmap);
6699 return NULL;
6702 /* Bound the given variable of "map" from below (or above is "upper"
6703 * is set) to "value".
6705 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6706 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6708 int i;
6710 map = isl_map_cow(map);
6711 if (isl_map_check_range(map, type, pos, 1) < 0)
6712 return isl_map_free(map);
6713 for (i = map->n - 1; i >= 0; --i) {
6714 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6715 map = remove_if_empty(map, i);
6716 if (!map)
6717 return NULL;
6719 map = isl_map_unmark_normalized(map);
6720 return map;
6723 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6724 enum isl_dim_type type, unsigned pos, isl_int value)
6726 return map_bound(map, type, pos, value, 0);
6729 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6730 enum isl_dim_type type, unsigned pos, isl_int value)
6732 return map_bound(map, type, pos, value, 1);
6735 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6736 enum isl_dim_type type, unsigned pos, isl_int value)
6738 return isl_map_lower_bound(set, type, pos, value);
6741 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6742 enum isl_dim_type type, unsigned pos, isl_int value)
6744 return isl_map_upper_bound(set, type, pos, value);
6747 /* Force the values of the variable at position "pos" of type "type" of "map"
6748 * to be no smaller than "value".
6750 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6751 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6753 if (!value)
6754 goto error;
6755 if (!isl_val_is_int(value))
6756 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6757 "expecting integer value", goto error);
6758 map = isl_map_lower_bound(map, type, pos, value->n);
6759 isl_val_free(value);
6760 return map;
6761 error:
6762 isl_val_free(value);
6763 isl_map_free(map);
6764 return NULL;
6767 /* Force the values of the variable at position "pos" of type "type" of "set"
6768 * to be no smaller than "value".
6770 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6771 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6773 isl_map *map;
6775 map = set_to_map(set);
6776 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6779 /* Force the values of the variable at position "pos" of type "type" of "map"
6780 * to be no greater than "value".
6782 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6783 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6785 if (!value)
6786 goto error;
6787 if (!isl_val_is_int(value))
6788 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6789 "expecting integer value", goto error);
6790 map = isl_map_upper_bound(map, type, pos, value->n);
6791 isl_val_free(value);
6792 return map;
6793 error:
6794 isl_val_free(value);
6795 isl_map_free(map);
6796 return NULL;
6799 /* Force the values of the variable at position "pos" of type "type" of "set"
6800 * to be no greater than "value".
6802 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6803 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6805 isl_map *map;
6807 map = set_to_map(set);
6808 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6811 #undef BASE
6812 #define BASE val
6813 #include "isl_map_bound_templ.c"
6815 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6816 * for each set dimension, by treating the set as a map.
6818 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6819 __isl_take isl_multi_val *bound,
6820 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6821 unsigned pos, __isl_take isl_val *value))
6823 isl_map *map;
6825 map = set_to_map(set);
6826 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6829 #undef BASE
6830 #define BASE pw_aff
6831 #include "isl_map_bound_templ.c"
6833 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6834 * for each set dimension, by converting the set and the bound
6835 * to objects living in a map space.
6837 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6838 __isl_take isl_multi_pw_aff *bound,
6839 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6840 unsigned pos, __isl_take TYPE *value))
6842 isl_map *map;
6844 map = isl_map_from_range(set);
6845 bound = isl_multi_pw_aff_from_range(bound);
6846 map = map_bound_multi_pw_aff(map, bound, set_bound);
6847 return isl_map_range(map);
6850 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6851 * setting a bound on the given output dimension.
6853 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6854 unsigned pos, __isl_take isl_val *v)
6856 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6859 /* Force the values of the set dimensions of "set"
6860 * to be no smaller than the corresponding values in "lower".
6862 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
6863 __isl_take isl_multi_val *lower)
6865 return set_bound_multi_val(set, lower, &map_lower_bound_val);
6868 /* Force the values of the output dimensions of "map"
6869 * to be no smaller than the corresponding values in "lower".
6871 __isl_give isl_map *isl_map_lower_bound_multi_val(__isl_take isl_map *map,
6872 __isl_take isl_multi_val *lower)
6874 return map_bound_multi_val(map, lower, &map_lower_bound_val);
6877 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6878 * setting a bound on the given output dimension.
6880 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
6881 unsigned pos, __isl_take isl_val *v)
6883 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
6886 /* Force the values of the set dimensions of "set"
6887 * to be no greater than the corresponding values in "upper".
6889 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
6890 __isl_take isl_multi_val *upper)
6892 return set_bound_multi_val(set, upper, &map_upper_bound_val);
6895 /* Force the values of the set dimensions of "set"
6896 * to be no greater than the corresponding values in "upper".
6898 __isl_give isl_map *isl_map_upper_bound_multi_val(__isl_take isl_map *map,
6899 __isl_take isl_multi_val *upper)
6901 return map_bound_multi_val(map, upper, &map_upper_bound_val);
6904 /* Force the symbolic constant expression "bound"
6905 * to satisfy the relation "order" with respect to
6906 * the output variable at position "pos" of "map".
6908 * Create an affine expression representing the output variable
6909 * in terms of the range and
6910 * compare it using "order" to "bound" (defined on the domain).
6911 * The result is a relation between elements in domain and range that
6912 * can be intersected with "map".
6914 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
6915 unsigned pos, __isl_take isl_pw_aff *bound,
6916 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
6917 __isl_take isl_pw_aff *pa2))
6919 isl_space *space;
6920 isl_local_space *ls;
6921 isl_pw_aff *var;
6923 space = isl_space_range(isl_map_get_space(map));
6924 ls = isl_local_space_from_space(space);
6925 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
6926 map = isl_map_intersect(map, order(bound, var));
6927 return map;
6930 /* Force the values of the output variable at position "pos" of "map"
6931 * to be no smaller than the symbolic constant expression "lower".
6933 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
6934 unsigned pos, __isl_take isl_pw_aff *lower)
6936 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
6939 /* Force the values of the output variable at position "pos" of "map"
6940 * to be no greater than the symbolic constant expression "upper".
6942 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
6943 unsigned pos, __isl_take isl_pw_aff *upper)
6945 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
6948 /* Force the values of the set dimensions of "set"
6949 * to be no smaller than the corresponding constant symbolic expressions
6950 * in "lower".
6952 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
6953 __isl_take isl_multi_pw_aff *lower)
6955 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
6958 /* Force the values of the set dimensions of "set"
6959 * to be no greater than the corresponding constant symbolic expressions
6960 * in "upper".
6962 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
6963 __isl_take isl_multi_pw_aff *upper)
6965 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
6968 /* Force the values of the output dimensions of "map"
6969 * to be no smaller than the corresponding constant symbolic expressions
6970 * in "lower".
6972 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
6973 __isl_take isl_multi_pw_aff *lower)
6975 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
6978 /* Force the values of the output dimensions of "map"
6979 * to be no greater than the corresponding constant symbolic expressions
6980 * in "upper".
6982 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
6983 __isl_take isl_multi_pw_aff *upper)
6985 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
6988 /* Bound the given variable of "bset" from below (or above is "upper"
6989 * is set) to "value".
6991 static __isl_give isl_basic_set *isl_basic_set_bound(
6992 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6993 isl_int value, int upper)
6995 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6996 type, pos, value, upper));
6999 /* Bound the given variable of "bset" from below (or above is "upper"
7000 * is set) to "value".
7002 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7003 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7004 __isl_take isl_val *value, int upper)
7006 if (!value)
7007 goto error;
7008 if (!isl_val_is_int(value))
7009 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7010 "expecting integer value", goto error);
7011 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7012 isl_val_free(value);
7013 return bset;
7014 error:
7015 isl_val_free(value);
7016 isl_basic_set_free(bset);
7017 return NULL;
7020 /* Bound the given variable of "bset" from below to "value".
7022 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7023 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7024 __isl_take isl_val *value)
7026 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7029 /* Bound the given variable of "bset" from above to "value".
7031 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7032 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7033 __isl_take isl_val *value)
7035 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7038 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7040 return isl_map_transform(map, &isl_space_reverse,
7041 &isl_basic_map_reverse);
7044 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7046 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7048 return isl_map_transform(map, &isl_space_range_reverse,
7049 &isl_basic_map_range_reverse);
7052 #undef TYPE
7053 #define TYPE isl_pw_multi_aff
7054 #undef SUFFIX
7055 #define SUFFIX _pw_multi_aff
7056 #undef EMPTY
7057 #define EMPTY isl_pw_multi_aff_empty
7058 #undef ADD
7059 #define ADD isl_pw_multi_aff_union_add
7060 #include "isl_map_lexopt_templ.c"
7062 /* Given a map "map", compute the lexicographically minimal
7063 * (or maximal) image element for each domain element in dom,
7064 * in the form of an isl_pw_multi_aff.
7065 * If "empty" is not NULL, then set *empty to those elements in dom that
7066 * do not have an image element.
7067 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7068 * should be computed over the domain of "map". "empty" is also NULL
7069 * in this case.
7071 * We first compute the lexicographically minimal or maximal element
7072 * in the first basic map. This results in a partial solution "res"
7073 * and a subset "todo" of dom that still need to be handled.
7074 * We then consider each of the remaining maps in "map" and successively
7075 * update both "res" and "todo".
7076 * If "empty" is NULL, then the todo sets are not needed and therefore
7077 * also not computed.
7079 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7080 __isl_take isl_map *map, __isl_take isl_set *dom,
7081 __isl_give isl_set **empty, unsigned flags)
7083 int i;
7084 int full;
7085 isl_pw_multi_aff *res;
7086 isl_set *todo;
7088 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7089 if (!map || (!full && !dom))
7090 goto error;
7092 if (isl_map_plain_is_empty(map)) {
7093 if (empty)
7094 *empty = dom;
7095 else
7096 isl_set_free(dom);
7097 return isl_pw_multi_aff_from_map(map);
7100 res = basic_map_partial_lexopt_pw_multi_aff(
7101 isl_basic_map_copy(map->p[0]),
7102 isl_set_copy(dom), empty, flags);
7104 if (empty)
7105 todo = *empty;
7106 for (i = 1; i < map->n; ++i) {
7107 isl_pw_multi_aff *res_i;
7109 res_i = basic_map_partial_lexopt_pw_multi_aff(
7110 isl_basic_map_copy(map->p[i]),
7111 isl_set_copy(dom), empty, flags);
7113 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7114 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7115 else
7116 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7118 if (empty)
7119 todo = isl_set_intersect(todo, *empty);
7122 isl_set_free(dom);
7123 isl_map_free(map);
7125 if (empty)
7126 *empty = todo;
7128 return res;
7129 error:
7130 if (empty)
7131 *empty = NULL;
7132 isl_set_free(dom);
7133 isl_map_free(map);
7134 return NULL;
7137 #undef TYPE
7138 #define TYPE isl_map
7139 #undef SUFFIX
7140 #define SUFFIX
7141 #undef EMPTY
7142 #define EMPTY isl_map_empty
7143 #undef ADD
7144 #define ADD isl_map_union_disjoint
7145 #include "isl_map_lexopt_templ.c"
7147 /* Given a map "map", compute the lexicographically minimal
7148 * (or maximal) image element for each domain element in "dom",
7149 * in the form of an isl_map.
7150 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7151 * do not have an image element.
7152 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7153 * should be computed over the domain of "map". "empty" is also NULL
7154 * in this case.
7156 * If the input consists of more than one disjunct, then first
7157 * compute the desired result in the form of an isl_pw_multi_aff and
7158 * then convert that into an isl_map.
7160 * This function used to have an explicit implementation in terms
7161 * of isl_maps, but it would continually intersect the domains of
7162 * partial results with the complement of the domain of the next
7163 * partial solution, potentially leading to an explosion in the number
7164 * of disjuncts if there are several disjuncts in the input.
7165 * An even earlier implementation of this function would look for
7166 * better results in the domain of the partial result and for extra
7167 * results in the complement of this domain, which would lead to
7168 * even more splintering.
7170 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7171 __isl_take isl_map *map, __isl_take isl_set *dom,
7172 __isl_give isl_set **empty, unsigned flags)
7174 int full;
7175 struct isl_map *res;
7176 isl_pw_multi_aff *pma;
7178 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7179 if (!map || (!full && !dom))
7180 goto error;
7182 if (isl_map_plain_is_empty(map)) {
7183 if (empty)
7184 *empty = dom;
7185 else
7186 isl_set_free(dom);
7187 return map;
7190 if (map->n == 1) {
7191 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7192 dom, empty, flags);
7193 isl_map_free(map);
7194 return res;
7197 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7198 flags);
7199 return isl_map_from_pw_multi_aff_internal(pma);
7200 error:
7201 if (empty)
7202 *empty = NULL;
7203 isl_set_free(dom);
7204 isl_map_free(map);
7205 return NULL;
7208 __isl_give isl_map *isl_map_partial_lexmax(
7209 __isl_take isl_map *map, __isl_take isl_set *dom,
7210 __isl_give isl_set **empty)
7212 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7215 __isl_give isl_map *isl_map_partial_lexmin(
7216 __isl_take isl_map *map, __isl_take isl_set *dom,
7217 __isl_give isl_set **empty)
7219 return isl_map_partial_lexopt(map, dom, empty, 0);
7222 __isl_give isl_set *isl_set_partial_lexmin(
7223 __isl_take isl_set *set, __isl_take isl_set *dom,
7224 __isl_give isl_set **empty)
7226 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7227 dom, empty));
7230 __isl_give isl_set *isl_set_partial_lexmax(
7231 __isl_take isl_set *set, __isl_take isl_set *dom,
7232 __isl_give isl_set **empty)
7234 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7235 dom, empty));
7238 /* Compute the lexicographic minimum (or maximum if "flags" includes
7239 * ISL_OPT_MAX) of "bset" over its parametric domain.
7241 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7242 unsigned flags)
7244 return isl_basic_map_lexopt(bset, flags);
7247 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7249 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7252 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7254 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7257 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7259 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7262 /* Compute the lexicographic minimum of "bset" over its parametric domain
7263 * for the purpose of quantifier elimination.
7264 * That is, find an explicit representation for all the existentially
7265 * quantified variables in "bset" by computing their lexicographic
7266 * minimum.
7268 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7269 __isl_take isl_basic_set *bset)
7271 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7274 /* Given a basic map with one output dimension, compute the minimum or
7275 * maximum of that dimension as an isl_pw_aff.
7277 * Compute the optimum as a lexicographic optimum over the single
7278 * output dimension and extract the single isl_pw_aff from the result.
7280 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7281 int max)
7283 isl_pw_multi_aff *pma;
7284 isl_pw_aff *pwaff;
7286 bmap = isl_basic_map_copy(bmap);
7287 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7288 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7289 isl_pw_multi_aff_free(pma);
7291 return pwaff;
7294 /* Compute the minimum or maximum of the given output dimension
7295 * as a function of the parameters and the input dimensions,
7296 * but independently of the other output dimensions.
7298 * We first project out the other output dimension and then compute
7299 * the "lexicographic" maximum in each basic map, combining the results
7300 * using isl_pw_aff_union_max.
7302 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7303 int max)
7305 int i;
7306 isl_pw_aff *pwaff;
7307 isl_size n_out;
7309 n_out = isl_map_dim(map, isl_dim_out);
7310 if (n_out < 0)
7311 map = isl_map_free(map);
7312 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7313 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7314 if (!map)
7315 return NULL;
7317 if (map->n == 0) {
7318 isl_space *space = isl_map_get_space(map);
7319 isl_map_free(map);
7320 return isl_pw_aff_empty(space);
7323 pwaff = basic_map_dim_opt(map->p[0], max);
7324 for (i = 1; i < map->n; ++i) {
7325 isl_pw_aff *pwaff_i;
7327 pwaff_i = basic_map_dim_opt(map->p[i], max);
7328 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7331 isl_map_free(map);
7333 return pwaff;
7336 /* Compute the minimum of the given output dimension as a function of the
7337 * parameters and input dimensions, but independently of
7338 * the other output dimensions.
7340 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7342 return map_dim_opt(map, pos, 0);
7345 /* Compute the maximum of the given output dimension as a function of the
7346 * parameters and input dimensions, but independently of
7347 * the other output dimensions.
7349 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7351 return map_dim_opt(map, pos, 1);
7354 /* Compute the minimum or maximum of the given set dimension
7355 * as a function of the parameters,
7356 * but independently of the other set dimensions.
7358 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7359 int max)
7361 return map_dim_opt(set, pos, max);
7364 /* Compute the maximum of the given set dimension as a function of the
7365 * parameters, but independently of the other set dimensions.
7367 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7369 return set_dim_opt(set, pos, 1);
7372 /* Compute the minimum of the given set dimension as a function of the
7373 * parameters, but independently of the other set dimensions.
7375 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7377 return set_dim_opt(set, pos, 0);
7380 /* Apply a preimage specified by "mat" on the parameters of "bset".
7381 * bset is assumed to have only parameters and divs.
7383 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7384 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7386 isl_size nparam;
7388 nparam = isl_basic_set_dim(bset, isl_dim_param);
7389 if (nparam < 0 || !mat)
7390 goto error;
7392 bset->dim = isl_space_cow(bset->dim);
7393 if (!bset->dim)
7394 goto error;
7396 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7398 bset->dim->nparam = 0;
7399 bset->dim->n_out = nparam;
7400 bset = isl_basic_set_preimage(bset, mat);
7401 if (bset) {
7402 bset->dim->nparam = bset->dim->n_out;
7403 bset->dim->n_out = 0;
7405 return bset;
7406 error:
7407 isl_mat_free(mat);
7408 isl_basic_set_free(bset);
7409 return NULL;
7412 /* Apply a preimage specified by "mat" on the parameters of "set".
7413 * set is assumed to have only parameters and divs.
7415 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7416 __isl_take isl_mat *mat)
7418 isl_space *space;
7419 isl_size nparam;
7421 nparam = isl_set_dim(set, isl_dim_param);
7422 if (nparam < 0 || !mat)
7423 goto error;
7425 if (mat->n_row != 1 + nparam)
7426 isl_die(isl_set_get_ctx(set), isl_error_internal,
7427 "unexpected number of rows", goto error);
7429 space = isl_set_get_space(set);
7430 space = isl_space_move_dims(space, isl_dim_set, 0,
7431 isl_dim_param, 0, nparam);
7432 set = isl_set_reset_space(set, space);
7433 set = isl_set_preimage(set, mat);
7434 nparam = isl_set_dim(set, isl_dim_out);
7435 if (nparam < 0)
7436 set = isl_set_free(set);
7437 space = isl_set_get_space(set);
7438 space = isl_space_move_dims(space, isl_dim_param, 0,
7439 isl_dim_out, 0, nparam);
7440 set = isl_set_reset_space(set, space);
7441 return set;
7442 error:
7443 isl_mat_free(mat);
7444 isl_set_free(set);
7445 return NULL;
7448 /* Intersect the basic set "bset" with the affine space specified by the
7449 * equalities in "eq".
7451 static __isl_give isl_basic_set *basic_set_append_equalities(
7452 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7454 int i, k;
7455 unsigned len;
7457 if (!bset || !eq)
7458 goto error;
7460 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7461 if (!bset)
7462 goto error;
7464 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7465 for (i = 0; i < eq->n_row; ++i) {
7466 k = isl_basic_set_alloc_equality(bset);
7467 if (k < 0)
7468 goto error;
7469 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7470 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7472 isl_mat_free(eq);
7474 bset = isl_basic_set_gauss(bset, NULL);
7475 bset = isl_basic_set_finalize(bset);
7477 return bset;
7478 error:
7479 isl_mat_free(eq);
7480 isl_basic_set_free(bset);
7481 return NULL;
7484 /* Intersect the set "set" with the affine space specified by the
7485 * equalities in "eq".
7487 static struct isl_set *set_append_equalities(struct isl_set *set,
7488 struct isl_mat *eq)
7490 int i;
7492 if (!set || !eq)
7493 goto error;
7495 for (i = 0; i < set->n; ++i) {
7496 set->p[i] = basic_set_append_equalities(set->p[i],
7497 isl_mat_copy(eq));
7498 if (!set->p[i])
7499 goto error;
7501 isl_mat_free(eq);
7502 return set;
7503 error:
7504 isl_mat_free(eq);
7505 isl_set_free(set);
7506 return NULL;
7509 /* Given a basic set "bset" that only involves parameters and existentially
7510 * quantified variables, return the index of the first equality
7511 * that only involves parameters. If there is no such equality then
7512 * return bset->n_eq.
7514 * This function assumes that isl_basic_set_gauss has been called on "bset".
7516 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7518 int i, j;
7519 isl_size nparam, n_div;
7521 nparam = isl_basic_set_dim(bset, isl_dim_param);
7522 n_div = isl_basic_set_dim(bset, isl_dim_div);
7523 if (nparam < 0 || n_div < 0)
7524 return -1;
7526 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7527 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7528 ++i;
7531 return i;
7534 /* Compute an explicit representation for the existentially quantified
7535 * variables in "bset" by computing the "minimal value" of the set
7536 * variables. Since there are no set variables, the computation of
7537 * the minimal value essentially computes an explicit representation
7538 * of the non-empty part(s) of "bset".
7540 * The input only involves parameters and existentially quantified variables.
7541 * All equalities among parameters have been removed.
7543 * Since the existentially quantified variables in the result are in general
7544 * going to be different from those in the input, we first replace
7545 * them by the minimal number of variables based on their equalities.
7546 * This should simplify the parametric integer programming.
7548 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7550 isl_morph *morph1, *morph2;
7551 isl_set *set;
7552 isl_size n;
7554 if (!bset)
7555 return NULL;
7556 if (bset->n_eq == 0)
7557 return isl_basic_set_lexmin_compute_divs(bset);
7559 morph1 = isl_basic_set_parameter_compression(bset);
7560 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7561 bset = isl_basic_set_lift(bset);
7562 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7563 bset = isl_morph_basic_set(morph2, bset);
7564 n = isl_basic_set_dim(bset, isl_dim_set);
7565 if (n < 0)
7566 bset = isl_basic_set_free(bset);
7567 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7569 set = isl_basic_set_lexmin_compute_divs(bset);
7571 set = isl_morph_set(isl_morph_inverse(morph1), set);
7573 return set;
7576 /* Project the given basic set onto its parameter domain, possibly introducing
7577 * new, explicit, existential variables in the constraints.
7578 * The input has parameters and (possibly implicit) existential variables.
7579 * The output has the same parameters, but only
7580 * explicit existentially quantified variables.
7582 * The actual projection is performed by pip, but pip doesn't seem
7583 * to like equalities very much, so we first remove the equalities
7584 * among the parameters by performing a variable compression on
7585 * the parameters. Afterward, an inverse transformation is performed
7586 * and the equalities among the parameters are inserted back in.
7588 * The variable compression on the parameters may uncover additional
7589 * equalities that were only implicit before. We therefore check
7590 * if there are any new parameter equalities in the result and
7591 * if so recurse. The removal of parameter equalities is required
7592 * for the parameter compression performed by base_compute_divs.
7594 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7596 int i;
7597 struct isl_mat *eq;
7598 struct isl_mat *T, *T2;
7599 struct isl_set *set;
7600 isl_size nparam;
7602 bset = isl_basic_set_cow(bset);
7603 if (!bset)
7604 return NULL;
7606 if (bset->n_eq == 0)
7607 return base_compute_divs(bset);
7609 bset = isl_basic_set_gauss(bset, NULL);
7610 if (!bset)
7611 return NULL;
7612 if (isl_basic_set_plain_is_empty(bset))
7613 return isl_set_from_basic_set(bset);
7615 i = first_parameter_equality(bset);
7616 if (i == bset->n_eq)
7617 return base_compute_divs(bset);
7619 nparam = isl_basic_set_dim(bset, isl_dim_param);
7620 if (nparam < 0)
7621 return isl_set_from_basic_set(isl_basic_set_free(bset));
7622 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7623 0, 1 + nparam);
7624 eq = isl_mat_cow(eq);
7625 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7626 if (T && T->n_col == 0) {
7627 isl_mat_free(T);
7628 isl_mat_free(T2);
7629 isl_mat_free(eq);
7630 bset = isl_basic_set_set_to_empty(bset);
7631 return isl_set_from_basic_set(bset);
7633 bset = basic_set_parameter_preimage(bset, T);
7635 i = first_parameter_equality(bset);
7636 if (!bset)
7637 set = NULL;
7638 else if (i == bset->n_eq)
7639 set = base_compute_divs(bset);
7640 else
7641 set = parameter_compute_divs(bset);
7642 set = set_parameter_preimage(set, T2);
7643 set = set_append_equalities(set, eq);
7644 return set;
7647 /* Insert the divs from "ls" before those of "bmap".
7649 * The number of columns is not changed, which means that the last
7650 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7651 * The caller is responsible for removing the same number of dimensions
7652 * from the space of "bmap".
7654 static __isl_give isl_basic_map *insert_divs_from_local_space(
7655 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7657 int i;
7658 isl_size n_div;
7659 int old_n_div;
7661 n_div = isl_local_space_dim(ls, isl_dim_div);
7662 if (n_div < 0)
7663 return isl_basic_map_free(bmap);
7664 if (n_div == 0)
7665 return bmap;
7667 old_n_div = bmap->n_div;
7668 bmap = insert_div_rows(bmap, n_div);
7669 if (!bmap)
7670 return NULL;
7672 for (i = 0; i < n_div; ++i) {
7673 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7674 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7677 return bmap;
7680 /* Replace the space of "bmap" by the space and divs of "ls".
7682 * If "ls" has any divs, then we simplify the result since we may
7683 * have discovered some additional equalities that could simplify
7684 * the div expressions.
7686 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7687 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7689 isl_size n_div;
7691 bmap = isl_basic_map_cow(bmap);
7692 n_div = isl_local_space_dim(ls, isl_dim_div);
7693 if (!bmap || n_div < 0)
7694 goto error;
7696 bmap = insert_divs_from_local_space(bmap, ls);
7697 if (!bmap)
7698 goto error;
7700 isl_space_free(bmap->dim);
7701 bmap->dim = isl_local_space_get_space(ls);
7702 if (!bmap->dim)
7703 goto error;
7705 isl_local_space_free(ls);
7706 if (n_div > 0)
7707 bmap = isl_basic_map_simplify(bmap);
7708 bmap = isl_basic_map_finalize(bmap);
7709 return bmap;
7710 error:
7711 isl_basic_map_free(bmap);
7712 isl_local_space_free(ls);
7713 return NULL;
7716 /* Replace the space of "map" by the space and divs of "ls".
7718 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7719 __isl_take isl_local_space *ls)
7721 int i;
7723 map = isl_map_cow(map);
7724 if (!map || !ls)
7725 goto error;
7727 for (i = 0; i < map->n; ++i) {
7728 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7729 isl_local_space_copy(ls));
7730 if (!map->p[i])
7731 goto error;
7733 isl_space_free(isl_map_take_space(map));
7734 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7736 isl_local_space_free(ls);
7737 return map;
7738 error:
7739 isl_local_space_free(ls);
7740 isl_map_free(map);
7741 return NULL;
7744 /* Compute an explicit representation for the existentially
7745 * quantified variables for which do not know any explicit representation yet.
7747 * We first sort the existentially quantified variables so that the
7748 * existentially quantified variables for which we already have an explicit
7749 * representation are placed before those for which we do not.
7750 * The input dimensions, the output dimensions and the existentially
7751 * quantified variables for which we already have an explicit
7752 * representation are then turned into parameters.
7753 * compute_divs returns a map with the same parameters and
7754 * no input or output dimensions and the dimension specification
7755 * is reset to that of the input, including the existentially quantified
7756 * variables for which we already had an explicit representation.
7758 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7760 struct isl_basic_set *bset;
7761 struct isl_set *set;
7762 struct isl_map *map;
7763 isl_space *space;
7764 isl_local_space *ls;
7765 isl_size nparam;
7766 isl_size n_in;
7767 isl_size n_out;
7768 int n_known;
7769 int i;
7771 bmap = isl_basic_map_sort_divs(bmap);
7772 bmap = isl_basic_map_cow(bmap);
7773 if (!bmap)
7774 return NULL;
7776 n_known = isl_basic_map_first_unknown_div(bmap);
7777 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7778 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7779 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7780 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7781 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7783 space = isl_space_set_alloc(bmap->ctx,
7784 nparam + n_in + n_out + n_known, 0);
7785 if (!space)
7786 goto error;
7788 ls = isl_basic_map_get_local_space(bmap);
7789 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7790 n_known, bmap->n_div - n_known);
7791 if (n_known > 0) {
7792 for (i = n_known; i < bmap->n_div; ++i)
7793 swap_div(bmap, i - n_known, i);
7794 bmap->n_div -= n_known;
7795 bmap->extra -= n_known;
7797 bmap = isl_basic_map_reset_space(bmap, space);
7798 bset = bset_from_bmap(bmap);
7800 set = parameter_compute_divs(bset);
7801 map = set_to_map(set);
7802 map = replace_space_by_local_space(map, ls);
7804 return map;
7805 error:
7806 isl_basic_map_free(bmap);
7807 return NULL;
7810 /* Remove the explicit representation of local variable "div",
7811 * if there is any.
7813 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7814 __isl_take isl_basic_map *bmap, int div)
7816 isl_bool unknown;
7818 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7819 if (unknown < 0)
7820 return isl_basic_map_free(bmap);
7821 if (unknown)
7822 return bmap;
7824 bmap = isl_basic_map_cow(bmap);
7825 if (!bmap)
7826 return NULL;
7827 isl_int_set_si(bmap->div[div][0], 0);
7828 return bmap;
7831 /* Is local variable "div" of "bmap" marked as not having an explicit
7832 * representation?
7833 * Note that even if "div" is not marked in this way and therefore
7834 * has an explicit representation, this representation may still
7835 * depend (indirectly) on other local variables that do not
7836 * have an explicit representation.
7838 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7839 int div)
7841 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7842 return isl_bool_error;
7843 return isl_int_is_zero(bmap->div[div][0]);
7846 /* Return the position of the first local variable that does not
7847 * have an explicit representation.
7848 * Return the total number of local variables if they all have
7849 * an explicit representation.
7850 * Return -1 on error.
7852 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7854 int i;
7856 if (!bmap)
7857 return -1;
7859 for (i = 0; i < bmap->n_div; ++i) {
7860 if (!isl_basic_map_div_is_known(bmap, i))
7861 return i;
7863 return bmap->n_div;
7866 /* Return the position of the first local variable that does not
7867 * have an explicit representation.
7868 * Return the total number of local variables if they all have
7869 * an explicit representation.
7870 * Return -1 on error.
7872 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7874 return isl_basic_map_first_unknown_div(bset);
7877 /* Does "bmap" have an explicit representation for all local variables?
7879 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7881 int first;
7882 isl_size n;
7884 n = isl_basic_map_dim(bmap, isl_dim_div);
7885 first = isl_basic_map_first_unknown_div(bmap);
7886 if (n < 0 || first < 0)
7887 return isl_bool_error;
7888 return first == n;
7891 /* Do all basic maps in "map" have an explicit representation
7892 * for all local variables?
7894 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7896 int i;
7898 if (!map)
7899 return isl_bool_error;
7901 for (i = 0; i < map->n; ++i) {
7902 int known = isl_basic_map_divs_known(map->p[i]);
7903 if (known <= 0)
7904 return known;
7907 return isl_bool_true;
7910 /* If bmap contains any unknown divs, then compute explicit
7911 * expressions for them. However, this computation may be
7912 * quite expensive, so first try to remove divs that aren't
7913 * strictly needed.
7915 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7917 int known;
7918 struct isl_map *map;
7920 known = isl_basic_map_divs_known(bmap);
7921 if (known < 0)
7922 goto error;
7923 if (known)
7924 return isl_map_from_basic_map(bmap);
7926 bmap = isl_basic_map_drop_redundant_divs(bmap);
7928 known = isl_basic_map_divs_known(bmap);
7929 if (known < 0)
7930 goto error;
7931 if (known)
7932 return isl_map_from_basic_map(bmap);
7934 map = compute_divs(bmap);
7935 return map;
7936 error:
7937 isl_basic_map_free(bmap);
7938 return NULL;
7941 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7943 int i;
7944 int known;
7945 struct isl_map *res;
7947 if (!map)
7948 return NULL;
7949 if (map->n == 0)
7950 return map;
7952 known = isl_map_divs_known(map);
7953 if (known < 0) {
7954 isl_map_free(map);
7955 return NULL;
7957 if (known)
7958 return map;
7960 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7961 for (i = 1 ; i < map->n; ++i) {
7962 struct isl_map *r2;
7963 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7964 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7965 res = isl_map_union_disjoint(res, r2);
7966 else
7967 res = isl_map_union(res, r2);
7969 isl_map_free(map);
7971 return res;
7974 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7976 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7979 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
7981 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7984 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7986 int i;
7987 struct isl_set *set;
7989 if (!map)
7990 goto error;
7992 map = isl_map_cow(map);
7993 if (!map)
7994 return NULL;
7996 set = set_from_map(map);
7997 set->dim = isl_space_domain(set->dim);
7998 if (!set->dim)
7999 goto error;
8000 for (i = 0; i < map->n; ++i) {
8001 set->p[i] = isl_basic_map_domain(map->p[i]);
8002 if (!set->p[i])
8003 goto error;
8005 ISL_F_CLR(set, ISL_MAP_DISJOINT);
8006 ISL_F_CLR(set, ISL_SET_NORMALIZED);
8007 return set;
8008 error:
8009 isl_map_free(map);
8010 return NULL;
8013 /* Return the union of "map1" and "map2", where we assume for now that
8014 * "map1" and "map2" are disjoint. Note that the basic maps inside
8015 * "map1" or "map2" may not be disjoint from each other.
8016 * Also note that this function is also called from isl_map_union,
8017 * which takes care of handling the situation where "map1" and "map2"
8018 * may not be disjoint.
8020 * If one of the inputs is empty, we can simply return the other input.
8021 * Similarly, if one of the inputs is universal, then it is equal to the union.
8023 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8024 __isl_take isl_map *map2)
8026 int i;
8027 unsigned flags = 0;
8028 struct isl_map *map = NULL;
8029 int is_universe;
8031 if (isl_map_check_equal_space(map1, map2) < 0)
8032 goto error;
8034 if (map1->n == 0) {
8035 isl_map_free(map1);
8036 return map2;
8038 if (map2->n == 0) {
8039 isl_map_free(map2);
8040 return map1;
8043 is_universe = isl_map_plain_is_universe(map1);
8044 if (is_universe < 0)
8045 goto error;
8046 if (is_universe) {
8047 isl_map_free(map2);
8048 return map1;
8051 is_universe = isl_map_plain_is_universe(map2);
8052 if (is_universe < 0)
8053 goto error;
8054 if (is_universe) {
8055 isl_map_free(map1);
8056 return map2;
8059 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8060 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8061 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8063 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8064 map1->n + map2->n, flags);
8065 if (!map)
8066 goto error;
8067 for (i = 0; i < map1->n; ++i) {
8068 map = isl_map_add_basic_map(map,
8069 isl_basic_map_copy(map1->p[i]));
8070 if (!map)
8071 goto error;
8073 for (i = 0; i < map2->n; ++i) {
8074 map = isl_map_add_basic_map(map,
8075 isl_basic_map_copy(map2->p[i]));
8076 if (!map)
8077 goto error;
8079 isl_map_free(map1);
8080 isl_map_free(map2);
8081 return map;
8082 error:
8083 isl_map_free(map);
8084 isl_map_free(map1);
8085 isl_map_free(map2);
8086 return NULL;
8089 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8090 * guaranteed to be disjoint by the caller.
8092 * Note that this functions is called from within isl_map_make_disjoint,
8093 * so we have to be careful not to touch the constraints of the inputs
8094 * in any way.
8096 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8097 __isl_take isl_map *map2)
8099 isl_map_align_params_bin(&map1, &map2);
8100 return map_union_disjoint(map1, map2);
8103 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8104 * not be disjoint.
8106 * We currently simply call map_union_disjoint, the internal operation
8107 * of which does not really depend on the inputs being disjoint.
8108 * If the result contains more than one basic map, then we clear
8109 * the disjoint flag since the result may contain basic maps from
8110 * both inputs and these are not guaranteed to be disjoint.
8112 * As a special case, if "map1" and "map2" are obviously equal,
8113 * then we simply return "map1".
8115 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8116 __isl_take isl_map *map2)
8118 int equal;
8120 if (isl_map_align_params_bin(&map1, &map2) < 0)
8121 goto error;
8123 equal = isl_map_plain_is_equal(map1, map2);
8124 if (equal < 0)
8125 goto error;
8126 if (equal) {
8127 isl_map_free(map2);
8128 return map1;
8131 map1 = map_union_disjoint(map1, map2);
8132 if (!map1)
8133 return NULL;
8134 if (map1->n > 1)
8135 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8136 return map1;
8137 error:
8138 isl_map_free(map1);
8139 isl_map_free(map2);
8140 return NULL;
8143 __isl_give isl_set *isl_set_union_disjoint(
8144 __isl_take isl_set *set1, __isl_take isl_set *set2)
8146 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8147 set_to_map(set2)));
8150 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8151 __isl_take isl_set *set2)
8153 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8156 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8157 * the results in a map living in "space".
8159 * "map" and "set" are assumed to be compatible and non-NULL.
8161 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8162 __isl_take isl_space *space, __isl_take isl_set *set,
8163 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8164 __isl_take isl_basic_set *bset))
8166 unsigned flags = 0;
8167 struct isl_map *result;
8168 int i, j;
8170 if (isl_set_plain_is_universe(set)) {
8171 isl_set_free(set);
8172 return isl_map_reset_equal_dim_space(map, space);
8175 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8176 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8177 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8179 result = isl_map_alloc_space(space, map->n * set->n, flags);
8180 for (i = 0; result && i < map->n; ++i)
8181 for (j = 0; j < set->n; ++j) {
8182 result = isl_map_add_basic_map(result,
8183 fn(isl_basic_map_copy(map->p[i]),
8184 isl_basic_set_copy(set->p[j])));
8185 if (!result)
8186 break;
8189 isl_map_free(map);
8190 isl_set_free(set);
8191 return result;
8194 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8195 __isl_take isl_set *set)
8197 isl_bool ok;
8198 isl_space *space;
8200 isl_map_align_params_set(&map, &set);
8201 ok = isl_map_compatible_range(map, set);
8202 if (ok < 0)
8203 goto error;
8204 if (!ok)
8205 isl_die(set->ctx, isl_error_invalid,
8206 "incompatible spaces", goto error);
8208 space = isl_map_get_space(map);
8209 return map_intersect_set(map, space, set,
8210 &isl_basic_map_intersect_range);
8211 error:
8212 isl_map_free(map);
8213 isl_set_free(set);
8214 return NULL;
8217 /* Intersect the domain of "map" with "set".
8219 * If the domain dimensions of "map" do not have any identifiers,
8220 * then copy them over from "set".
8222 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8223 __isl_take isl_set *set)
8225 isl_bool ok;
8226 isl_space *space;
8228 isl_map_align_params_set(&map, &set);
8229 ok = isl_map_compatible_domain(map, set);
8230 if (ok < 0)
8231 goto error;
8232 if (!ok)
8233 isl_die(set->ctx, isl_error_invalid,
8234 "incompatible spaces", goto error);
8236 space = isl_map_get_space(map);
8237 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8238 isl_set_peek_space(set), isl_dim_set);
8239 return map_intersect_set(map, space, set,
8240 &isl_basic_map_intersect_domain);
8241 error:
8242 isl_map_free(map);
8243 isl_set_free(set);
8244 return NULL;
8247 /* Data structure that specifies how isl_map_intersect_factor
8248 * should operate.
8250 * "preserve_type" is the tuple where the factor differs from
8251 * the input map and of which the identifiers needs
8252 * to be preserved explicitly.
8253 * "other_factor" is used to extract the space of the other factor
8254 * from the space of the product ("map").
8255 * "product" is used to combine the given factor and a universe map
8256 * in the space returned by "other_factor" to produce a map
8257 * that lives in the same space as the input map.
8259 struct isl_intersect_factor_control {
8260 enum isl_dim_type preserve_type;
8261 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8262 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8263 __isl_take isl_map *other);
8266 /* Given a map "map" in some product space and a map "factor"
8267 * living in some factor space, return the intersection.
8269 * After aligning the parameters,
8270 * the map "factor" is first extended to a map living in the same space
8271 * as "map" and then a regular intersection is computed.
8273 * Note that the extension is computed as a product, which is anonymous
8274 * by default. If "map" has an identifier on the corresponding tuple,
8275 * then this identifier needs to be set on the product
8276 * before the intersection is computed.
8278 static __isl_give isl_map *isl_map_intersect_factor(
8279 __isl_take isl_map *map, __isl_take isl_map *factor,
8280 struct isl_intersect_factor_control *control)
8282 isl_bool equal, has_id;
8283 isl_id *id;
8284 isl_space *space;
8285 isl_map *other, *product;
8287 equal = isl_map_has_equal_params(map, factor);
8288 if (equal < 0)
8289 goto error;
8290 if (!equal) {
8291 map = isl_map_align_params(map, isl_map_get_space(factor));
8292 factor = isl_map_align_params(factor, isl_map_get_space(map));
8295 space = isl_map_get_space(map);
8296 has_id = isl_space_has_tuple_id(space, control->preserve_type);
8297 if (has_id < 0)
8298 space = isl_space_free(space);
8299 else if (has_id)
8300 id = isl_space_get_tuple_id(space, control->preserve_type);
8302 other = isl_map_universe(control->other_factor(space));
8303 product = control->product(factor, other);
8305 if (has_id >= 0 && has_id)
8306 product = isl_map_set_tuple_id(product,
8307 control->preserve_type, id);
8309 return map_intersect(map, product);
8310 error:
8311 isl_map_free(map);
8312 isl_map_free(factor);
8313 return NULL;
8316 /* Return the domain product of "map2" and "map1".
8318 static __isl_give isl_map *isl_map_reverse_domain_product(
8319 __isl_take isl_map *map1, __isl_take isl_map *map2)
8321 return isl_map_domain_product(map2, map1);
8324 /* Return the range product of "map2" and "map1".
8326 static __isl_give isl_map *isl_map_reverse_range_product(
8327 __isl_take isl_map *map1, __isl_take isl_map *map2)
8329 return isl_map_range_product(map2, map1);
8332 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8333 * in the space B -> C, return the intersection.
8335 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8336 __isl_take isl_map *map, __isl_take isl_map *factor)
8338 struct isl_intersect_factor_control control = {
8339 .preserve_type = isl_dim_in,
8340 .other_factor = isl_space_domain_factor_domain,
8341 .product = isl_map_reverse_domain_product,
8344 return isl_map_intersect_factor(map, factor, &control);
8347 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8348 * in the space A -> B, return the intersection.
8350 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8351 __isl_take isl_map *map, __isl_take isl_map *factor)
8353 struct isl_intersect_factor_control control = {
8354 .preserve_type = isl_dim_out,
8355 .other_factor = isl_space_range_factor_range,
8356 .product = isl_map_range_product,
8359 return isl_map_intersect_factor(map, factor, &control);
8362 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8363 * in the space A -> C, return the intersection.
8365 __isl_give isl_map *isl_map_intersect_range_factor_range(
8366 __isl_take isl_map *map, __isl_take isl_map *factor)
8368 struct isl_intersect_factor_control control = {
8369 .preserve_type = isl_dim_out,
8370 .other_factor = isl_space_range_factor_domain,
8371 .product = isl_map_reverse_range_product,
8374 return isl_map_intersect_factor(map, factor, &control);
8377 /* Given a set "set" in a space [A -> B] and a set "domain"
8378 * in the space A, return the intersection.
8380 * The set "domain" is first extended to a set living in the space
8381 * [A -> B] and then a regular intersection is computed.
8383 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8384 __isl_take isl_set *domain)
8386 struct isl_intersect_factor_control control = {
8387 .preserve_type = isl_dim_set,
8388 .other_factor = isl_space_factor_range,
8389 .product = isl_map_range_product,
8392 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8393 set_to_map(domain), &control));
8396 /* Given a set "set" in a space [A -> B] and a set "range"
8397 * in the space B, return the intersection.
8399 * The set "range" is first extended to a set living in the space
8400 * [A -> B] and then a regular intersection is computed.
8402 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8403 __isl_take isl_set *range)
8405 struct isl_intersect_factor_control control = {
8406 .preserve_type = isl_dim_set,
8407 .other_factor = isl_space_factor_domain,
8408 .product = isl_map_reverse_range_product,
8411 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8412 set_to_map(range), &control));
8415 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8416 __isl_take isl_map *map2)
8418 if (isl_map_align_params_bin(&map1, &map2) < 0)
8419 goto error;
8420 map1 = isl_map_reverse(map1);
8421 map1 = isl_map_apply_range(map1, map2);
8422 return isl_map_reverse(map1);
8423 error:
8424 isl_map_free(map1);
8425 isl_map_free(map2);
8426 return NULL;
8429 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8430 __isl_take isl_map *map2)
8432 isl_space *space;
8433 struct isl_map *result;
8434 int i, j;
8436 if (isl_map_align_params_bin(&map1, &map2) < 0)
8437 goto error;
8439 space = isl_space_join(isl_space_copy(map1->dim),
8440 isl_space_copy(map2->dim));
8442 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8443 if (!result)
8444 goto error;
8445 for (i = 0; i < map1->n; ++i)
8446 for (j = 0; j < map2->n; ++j) {
8447 result = isl_map_add_basic_map(result,
8448 isl_basic_map_apply_range(
8449 isl_basic_map_copy(map1->p[i]),
8450 isl_basic_map_copy(map2->p[j])));
8451 if (!result)
8452 goto error;
8454 isl_map_free(map1);
8455 isl_map_free(map2);
8456 if (result && result->n <= 1)
8457 ISL_F_SET(result, ISL_MAP_DISJOINT);
8458 return result;
8459 error:
8460 isl_map_free(map1);
8461 isl_map_free(map2);
8462 return NULL;
8466 * returns range - domain
8468 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8470 isl_space *target_space;
8471 struct isl_basic_set *bset;
8472 isl_size dim;
8473 isl_size nparam;
8474 isl_size total;
8475 int i;
8477 if (!bmap)
8478 goto error;
8479 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8480 bmap->dim, isl_dim_out),
8481 goto error);
8482 dim = isl_basic_map_dim(bmap, isl_dim_in);
8483 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8484 if (dim < 0 || nparam < 0)
8485 goto error;
8486 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8487 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8488 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8489 total = isl_basic_map_dim(bmap, isl_dim_all);
8490 if (total < 0)
8491 bmap = isl_basic_map_free(bmap);
8492 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8493 for (i = 0; i < dim; ++i) {
8494 int j = isl_basic_map_alloc_equality(bmap);
8495 if (j < 0) {
8496 bmap = isl_basic_map_free(bmap);
8497 break;
8499 isl_seq_clr(bmap->eq[j], 1 + total);
8500 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8501 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8502 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8504 bset = isl_basic_map_domain(bmap);
8505 bset = isl_basic_set_reset_space(bset, target_space);
8506 return bset;
8507 error:
8508 isl_basic_map_free(bmap);
8509 return NULL;
8512 /* Check that domain and range of "map" are the same.
8514 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8516 isl_space *space;
8517 isl_bool equal;
8519 space = isl_map_peek_space(map);
8520 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8521 if (equal < 0)
8522 return isl_stat_error;
8523 if (!equal)
8524 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8525 "domain and range don't match", return isl_stat_error);
8526 return isl_stat_ok;
8530 * returns range - domain
8532 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8534 int i;
8535 isl_space *space;
8536 struct isl_set *result;
8538 if (!map)
8539 return NULL;
8541 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8542 map->dim, isl_dim_out),
8543 goto error);
8544 space = isl_map_get_space(map);
8545 space = isl_space_domain(space);
8546 result = isl_set_alloc_space(space, map->n, 0);
8547 if (!result)
8548 goto error;
8549 for (i = 0; i < map->n; ++i)
8550 result = isl_set_add_basic_set(result,
8551 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8552 isl_map_free(map);
8553 return result;
8554 error:
8555 isl_map_free(map);
8556 return NULL;
8560 * returns [domain -> range] -> range - domain
8562 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8563 __isl_take isl_basic_map *bmap)
8565 int i, k;
8566 isl_space *space;
8567 isl_basic_map *domain;
8568 isl_size nparam, n;
8569 isl_size total;
8571 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8572 bmap->dim, isl_dim_out))
8573 isl_die(bmap->ctx, isl_error_invalid,
8574 "domain and range don't match", goto error);
8576 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8577 n = isl_basic_map_dim(bmap, isl_dim_in);
8578 if (nparam < 0 || n < 0)
8579 return isl_basic_map_free(bmap);
8581 space = isl_basic_map_get_space(bmap);
8582 space = isl_space_from_range(isl_space_domain(space));
8583 domain = isl_basic_map_universe(space);
8585 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8586 bmap = isl_basic_map_apply_range(bmap, domain);
8587 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8589 total = isl_basic_map_dim(bmap, isl_dim_all);
8590 if (total < 0)
8591 return isl_basic_map_free(bmap);
8593 for (i = 0; i < n; ++i) {
8594 k = isl_basic_map_alloc_equality(bmap);
8595 if (k < 0)
8596 goto error;
8597 isl_seq_clr(bmap->eq[k], 1 + total);
8598 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8599 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8600 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8603 bmap = isl_basic_map_gauss(bmap, NULL);
8604 return isl_basic_map_finalize(bmap);
8605 error:
8606 isl_basic_map_free(bmap);
8607 return NULL;
8611 * returns [domain -> range] -> range - domain
8613 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8615 if (isl_map_check_equal_tuples(map) < 0)
8616 return isl_map_free(map);
8618 return isl_map_transform(map, &isl_space_range_map,
8619 &isl_basic_map_deltas_map);
8622 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8624 isl_size n_in, n_out;
8626 n_in = isl_space_dim(space, isl_dim_in);
8627 n_out = isl_space_dim(space, isl_dim_out);
8628 if (n_in < 0 || n_out < 0)
8629 goto error;
8630 if (n_in != n_out)
8631 isl_die(space->ctx, isl_error_invalid,
8632 "number of input and output dimensions needs to be "
8633 "the same", goto error);
8634 return isl_basic_map_equal(space, n_in);
8635 error:
8636 isl_space_free(space);
8637 return NULL;
8640 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8642 return isl_map_from_basic_map(isl_basic_map_identity(space));
8645 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8647 isl_space *space = isl_set_get_space(set);
8648 isl_map *id;
8649 id = isl_map_identity(isl_space_map_from_set(space));
8650 return isl_map_intersect_range(id, set);
8653 /* Construct a basic set with all set dimensions having only non-negative
8654 * values.
8656 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8657 __isl_take isl_space *space)
8659 int i;
8660 isl_size nparam;
8661 isl_size dim;
8662 isl_size total;
8663 struct isl_basic_set *bset;
8665 nparam = isl_space_dim(space, isl_dim_param);
8666 dim = isl_space_dim(space, isl_dim_set);
8667 total = isl_space_dim(space, isl_dim_all);
8668 if (nparam < 0 || dim < 0 || total < 0)
8669 space = isl_space_free(space);
8670 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8671 if (!bset)
8672 return NULL;
8673 for (i = 0; i < dim; ++i) {
8674 int k = isl_basic_set_alloc_inequality(bset);
8675 if (k < 0)
8676 goto error;
8677 isl_seq_clr(bset->ineq[k], 1 + total);
8678 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8680 return bset;
8681 error:
8682 isl_basic_set_free(bset);
8683 return NULL;
8686 /* Construct the half-space x_pos >= 0.
8688 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8689 int pos)
8691 int k;
8692 isl_size total;
8693 isl_basic_set *nonneg;
8695 total = isl_space_dim(space, isl_dim_all);
8696 if (total < 0)
8697 space = isl_space_free(space);
8698 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8699 k = isl_basic_set_alloc_inequality(nonneg);
8700 if (k < 0)
8701 goto error;
8702 isl_seq_clr(nonneg->ineq[k], 1 + total);
8703 isl_int_set_si(nonneg->ineq[k][pos], 1);
8705 return isl_basic_set_finalize(nonneg);
8706 error:
8707 isl_basic_set_free(nonneg);
8708 return NULL;
8711 /* Construct the half-space x_pos <= -1.
8713 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8714 int pos)
8716 int k;
8717 isl_size total;
8718 isl_basic_set *neg;
8720 total = isl_space_dim(space, isl_dim_all);
8721 if (total < 0)
8722 space = isl_space_free(space);
8723 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8724 k = isl_basic_set_alloc_inequality(neg);
8725 if (k < 0)
8726 goto error;
8727 isl_seq_clr(neg->ineq[k], 1 + total);
8728 isl_int_set_si(neg->ineq[k][0], -1);
8729 isl_int_set_si(neg->ineq[k][pos], -1);
8731 return isl_basic_set_finalize(neg);
8732 error:
8733 isl_basic_set_free(neg);
8734 return NULL;
8737 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8738 enum isl_dim_type type, unsigned first, unsigned n)
8740 int i;
8741 unsigned offset;
8742 isl_basic_set *nonneg;
8743 isl_basic_set *neg;
8745 if (n == 0)
8746 return set;
8748 if (isl_set_check_range(set, type, first, n) < 0)
8749 return isl_set_free(set);
8751 offset = pos(set->dim, type);
8752 for (i = 0; i < n; ++i) {
8753 nonneg = nonneg_halfspace(isl_set_get_space(set),
8754 offset + first + i);
8755 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8757 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8760 return set;
8763 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8764 int len,
8765 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8766 void *user)
8768 isl_set *half;
8770 if (!set)
8771 return isl_stat_error;
8772 if (isl_set_plain_is_empty(set)) {
8773 isl_set_free(set);
8774 return isl_stat_ok;
8776 if (first == len)
8777 return fn(set, signs, user);
8779 signs[first] = 1;
8780 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8781 1 + first));
8782 half = isl_set_intersect(half, isl_set_copy(set));
8783 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8784 goto error;
8786 signs[first] = -1;
8787 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8788 1 + first));
8789 half = isl_set_intersect(half, set);
8790 return foreach_orthant(half, signs, first + 1, len, fn, user);
8791 error:
8792 isl_set_free(set);
8793 return isl_stat_error;
8796 /* Call "fn" on the intersections of "set" with each of the orthants
8797 * (except for obviously empty intersections). The orthant is identified
8798 * by the signs array, with each entry having value 1 or -1 according
8799 * to the sign of the corresponding variable.
8801 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8802 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8803 void *user)
8805 isl_size nparam;
8806 isl_size nvar;
8807 int *signs;
8808 isl_stat r;
8810 if (!set)
8811 return isl_stat_error;
8812 if (isl_set_plain_is_empty(set))
8813 return isl_stat_ok;
8815 nparam = isl_set_dim(set, isl_dim_param);
8816 nvar = isl_set_dim(set, isl_dim_set);
8817 if (nparam < 0 || nvar < 0)
8818 return isl_stat_error;
8820 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8822 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8823 fn, user);
8825 free(signs);
8827 return r;
8830 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8832 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8835 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8836 __isl_keep isl_basic_map *bmap2)
8838 isl_bool is_subset;
8839 struct isl_map *map1;
8840 struct isl_map *map2;
8842 if (!bmap1 || !bmap2)
8843 return isl_bool_error;
8845 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8846 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8848 is_subset = isl_map_is_subset(map1, map2);
8850 isl_map_free(map1);
8851 isl_map_free(map2);
8853 return is_subset;
8856 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8857 __isl_keep isl_basic_set *bset2)
8859 return isl_basic_map_is_subset(bset1, bset2);
8862 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8863 __isl_keep isl_basic_map *bmap2)
8865 isl_bool is_subset;
8867 if (!bmap1 || !bmap2)
8868 return isl_bool_error;
8869 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8870 if (is_subset != isl_bool_true)
8871 return is_subset;
8872 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8873 return is_subset;
8876 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8877 __isl_keep isl_basic_set *bset2)
8879 return isl_basic_map_is_equal(
8880 bset_to_bmap(bset1), bset_to_bmap(bset2));
8883 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8885 int i;
8886 int is_empty;
8888 if (!map)
8889 return isl_bool_error;
8890 for (i = 0; i < map->n; ++i) {
8891 is_empty = isl_basic_map_is_empty(map->p[i]);
8892 if (is_empty < 0)
8893 return isl_bool_error;
8894 if (!is_empty)
8895 return isl_bool_false;
8897 return isl_bool_true;
8900 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8902 return map ? map->n == 0 : isl_bool_error;
8905 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8907 return set ? set->n == 0 : isl_bool_error;
8910 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8912 return isl_map_is_empty(set_to_map(set));
8915 #undef TYPE
8916 #define TYPE isl_basic_map
8918 static
8919 #include "isl_type_has_equal_space_bin_templ.c"
8920 #include "isl_type_check_equal_space_templ.c"
8922 /* Check that "bset1" and "bset2" live in the same space,
8923 * reporting an error if they do not.
8925 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
8926 __isl_keep isl_basic_set *bset2)
8928 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
8929 bset_to_bmap(bset1));
8932 #undef TYPE
8933 #define TYPE isl_map
8935 #include "isl_type_has_equal_space_bin_templ.c"
8936 #include "isl_type_check_equal_space_templ.c"
8938 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8939 __isl_keep isl_set *set2)
8941 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
8944 #undef TYPE1
8945 #define TYPE1 isl_map
8946 #undef TYPE2
8947 #define TYPE2 isl_basic_map
8948 #undef TYPE_PAIR
8949 #define TYPE_PAIR isl_map_basic_map
8951 static
8952 #include "isl_type_has_equal_space_templ.c"
8953 #include "isl_type_check_equal_space_templ.c"
8955 /* Check that "set" and "bset" live in the same space,
8956 * reporting an error if they do not.
8958 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
8959 __isl_keep isl_basic_set *bset)
8961 return isl_map_basic_map_check_equal_space(set_to_map(set),
8962 bset_to_bmap(bset));
8965 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8967 isl_bool is_subset;
8969 if (!map1 || !map2)
8970 return isl_bool_error;
8971 is_subset = isl_map_is_subset(map1, map2);
8972 if (is_subset != isl_bool_true)
8973 return is_subset;
8974 is_subset = isl_map_is_subset(map2, map1);
8975 return is_subset;
8978 /* Is "map1" equal to "map2"?
8980 * First check if they are obviously equal.
8981 * If not, then perform a more detailed analysis.
8983 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8985 isl_bool equal;
8987 equal = isl_map_plain_is_equal(map1, map2);
8988 if (equal < 0 || equal)
8989 return equal;
8990 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8993 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
8994 __isl_keep isl_basic_map *bmap2)
8996 isl_bool is_subset;
8998 if (!bmap1 || !bmap2)
8999 return isl_bool_error;
9000 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9001 if (is_subset != isl_bool_true)
9002 return is_subset;
9003 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9004 return isl_bool_not(is_subset);
9007 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9008 __isl_keep isl_map *map2)
9010 isl_bool is_subset;
9012 if (!map1 || !map2)
9013 return isl_bool_error;
9014 is_subset = isl_map_is_subset(map1, map2);
9015 if (is_subset != isl_bool_true)
9016 return is_subset;
9017 is_subset = isl_map_is_subset(map2, map1);
9018 return isl_bool_not(is_subset);
9021 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9022 __isl_keep isl_set *set2)
9024 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9027 /* Is "bmap" obviously equal to the universe with the same space?
9029 * That is, does it not have any constraints?
9031 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9033 if (!bmap)
9034 return isl_bool_error;
9035 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9038 /* Is "bset" obviously equal to the universe with the same space?
9040 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9042 return isl_basic_map_plain_is_universe(bset);
9045 /* If "c" does not involve any existentially quantified variables,
9046 * then set *univ to false and abort
9048 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9050 isl_bool *univ = user;
9051 isl_size n;
9053 n = isl_constraint_dim(c, isl_dim_div);
9054 if (n < 0)
9055 c = isl_constraint_free(c);
9056 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9057 isl_constraint_free(c);
9058 if (*univ < 0 || !*univ)
9059 return isl_stat_error;
9060 return isl_stat_ok;
9063 /* Is "bmap" equal to the universe with the same space?
9065 * First check if it is obviously equal to the universe.
9066 * If not and if there are any constraints not involving
9067 * existentially quantified variables, then it is certainly
9068 * not equal to the universe.
9069 * Otherwise, check if the universe is a subset of "bmap".
9071 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9073 isl_size n_div;
9074 isl_bool univ;
9075 isl_basic_map *test;
9077 univ = isl_basic_map_plain_is_universe(bmap);
9078 if (univ < 0 || univ)
9079 return univ;
9080 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9081 if (n_div < 0)
9082 return isl_bool_error;
9083 if (n_div == 0)
9084 return isl_bool_false;
9085 univ = isl_bool_true;
9086 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9087 univ)
9088 return isl_bool_error;
9089 if (univ < 0 || !univ)
9090 return univ;
9091 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9092 univ = isl_basic_map_is_subset(test, bmap);
9093 isl_basic_map_free(test);
9094 return univ;
9097 /* Is "bset" equal to the universe with the same space?
9099 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9101 return isl_basic_map_is_universe(bset);
9104 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9106 int i;
9108 if (!map)
9109 return isl_bool_error;
9111 for (i = 0; i < map->n; ++i) {
9112 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9113 if (r < 0 || r)
9114 return r;
9117 return isl_bool_false;
9120 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9122 return isl_map_plain_is_universe(set_to_map(set));
9125 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9127 struct isl_basic_set *bset = NULL;
9128 struct isl_vec *sample = NULL;
9129 isl_bool empty, non_empty;
9131 if (!bmap)
9132 return isl_bool_error;
9134 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9135 return isl_bool_true;
9137 if (isl_basic_map_plain_is_universe(bmap))
9138 return isl_bool_false;
9140 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9141 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9142 copy = isl_basic_map_remove_redundancies(copy);
9143 empty = isl_basic_map_plain_is_empty(copy);
9144 isl_basic_map_free(copy);
9145 return empty;
9148 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9149 if (non_empty < 0)
9150 return isl_bool_error;
9151 if (non_empty)
9152 return isl_bool_false;
9153 isl_vec_free(bmap->sample);
9154 bmap->sample = NULL;
9155 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9156 if (!bset)
9157 return isl_bool_error;
9158 sample = isl_basic_set_sample_vec(bset);
9159 if (!sample)
9160 return isl_bool_error;
9161 empty = sample->size == 0;
9162 isl_vec_free(bmap->sample);
9163 bmap->sample = sample;
9164 if (empty)
9165 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9167 return empty;
9170 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9172 if (!bmap)
9173 return isl_bool_error;
9174 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9177 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9179 if (!bset)
9180 return isl_bool_error;
9181 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9184 /* Is "bmap" known to be non-empty?
9186 * That is, is the cached sample still valid?
9188 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9190 isl_size total;
9192 if (!bmap)
9193 return isl_bool_error;
9194 if (!bmap->sample)
9195 return isl_bool_false;
9196 total = isl_basic_map_dim(bmap, isl_dim_all);
9197 if (total < 0)
9198 return isl_bool_error;
9199 if (bmap->sample->size != 1 + total)
9200 return isl_bool_false;
9201 return isl_basic_map_contains(bmap, bmap->sample);
9204 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9206 return isl_basic_map_is_empty(bset_to_bmap(bset));
9209 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9210 __isl_take isl_basic_map *bmap2)
9212 struct isl_map *map;
9214 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9215 goto error;
9217 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9218 if (!map)
9219 goto error;
9220 map = isl_map_add_basic_map(map, bmap1);
9221 map = isl_map_add_basic_map(map, bmap2);
9222 return map;
9223 error:
9224 isl_basic_map_free(bmap1);
9225 isl_basic_map_free(bmap2);
9226 return NULL;
9229 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9230 __isl_take isl_basic_set *bset2)
9232 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9233 bset_to_bmap(bset2)));
9236 /* Order divs such that any div only depends on previous divs */
9237 __isl_give isl_basic_map *isl_basic_map_order_divs(
9238 __isl_take isl_basic_map *bmap)
9240 int i;
9241 isl_size off;
9243 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9244 if (off < 0)
9245 return isl_basic_map_free(bmap);
9247 for (i = 0; i < bmap->n_div; ++i) {
9248 int pos;
9249 if (isl_int_is_zero(bmap->div[i][0]))
9250 continue;
9251 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9252 bmap->n_div-i);
9253 if (pos == -1)
9254 continue;
9255 if (pos == 0)
9256 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9257 "integer division depends on itself",
9258 return isl_basic_map_free(bmap));
9259 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9260 if (!bmap)
9261 return NULL;
9262 --i;
9264 return bmap;
9267 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9269 int i;
9271 if (!map)
9272 return 0;
9274 for (i = 0; i < map->n; ++i) {
9275 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9276 if (!map->p[i])
9277 goto error;
9280 return map;
9281 error:
9282 isl_map_free(map);
9283 return NULL;
9286 /* Sort the local variables of "bset".
9288 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9289 __isl_take isl_basic_set *bset)
9291 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9294 /* Apply the expansion computed by isl_merge_divs.
9295 * The expansion itself is given by "exp" while the resulting
9296 * list of divs is given by "div".
9298 * Move the integer divisions of "bmap" into the right position
9299 * according to "exp" and then introduce the additional integer
9300 * divisions, adding div constraints.
9301 * The moving should be done first to avoid moving coefficients
9302 * in the definitions of the extra integer divisions.
9304 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9305 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9307 int i, j;
9308 int n_div;
9310 bmap = isl_basic_map_cow(bmap);
9311 if (!bmap || !div)
9312 goto error;
9314 if (div->n_row < bmap->n_div)
9315 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9316 "not an expansion", goto error);
9318 n_div = bmap->n_div;
9319 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9320 2 * (div->n_row - n_div));
9322 for (i = n_div; i < div->n_row; ++i)
9323 if (isl_basic_map_alloc_div(bmap) < 0)
9324 goto error;
9326 for (j = n_div - 1; j >= 0; --j) {
9327 if (exp[j] == j)
9328 break;
9329 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9330 if (!bmap)
9331 goto error;
9333 j = 0;
9334 for (i = 0; i < div->n_row; ++i) {
9335 if (j < n_div && exp[j] == i) {
9336 j++;
9337 } else {
9338 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9339 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9340 continue;
9341 bmap = isl_basic_map_add_div_constraints(bmap, i);
9342 if (!bmap)
9343 goto error;
9347 isl_mat_free(div);
9348 return bmap;
9349 error:
9350 isl_basic_map_free(bmap);
9351 isl_mat_free(div);
9352 return NULL;
9355 /* Apply the expansion computed by isl_merge_divs.
9356 * The expansion itself is given by "exp" while the resulting
9357 * list of divs is given by "div".
9359 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9360 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9362 return isl_basic_map_expand_divs(bset, div, exp);
9365 /* Look for a div in dst that corresponds to the div "div" in src.
9366 * The divs before "div" in src and dst are assumed to be the same.
9368 * Return the position of the corresponding div in dst
9369 * if there is one. Otherwise, return a position beyond the integer divisions.
9370 * Return -1 on error.
9372 static int find_div(__isl_keep isl_basic_map *dst,
9373 __isl_keep isl_basic_map *src, unsigned div)
9375 int i;
9376 isl_size n_div;
9377 isl_size v_div;
9379 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9380 n_div = isl_basic_map_dim(dst, isl_dim_div);
9381 if (n_div < 0 || v_div < 0)
9382 return -1;
9383 isl_assert(dst->ctx, div <= n_div, return -1);
9384 for (i = div; i < n_div; ++i)
9385 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9386 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9387 n_div - div) == -1)
9388 return i;
9389 return n_div;
9392 /* Align the divs of "dst" to those of "src", adding divs from "src"
9393 * if needed. That is, make sure that the first src->n_div divs
9394 * of the result are equal to those of src.
9396 * The result is not finalized as by design it will have redundant
9397 * divs if any divs from "src" were copied.
9399 __isl_give isl_basic_map *isl_basic_map_align_divs(
9400 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9402 int i;
9403 isl_bool known;
9404 int extended;
9405 isl_size v_div;
9406 isl_size dst_n_div;
9408 if (!dst || !src)
9409 return isl_basic_map_free(dst);
9411 if (src->n_div == 0)
9412 return dst;
9414 known = isl_basic_map_divs_known(src);
9415 if (known < 0)
9416 return isl_basic_map_free(dst);
9417 if (!known)
9418 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9419 "some src divs are unknown",
9420 return isl_basic_map_free(dst));
9422 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9423 if (v_div < 0)
9424 return isl_basic_map_free(dst);
9426 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
9427 if (!src)
9428 return isl_basic_map_free(dst);
9430 extended = 0;
9431 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9432 if (dst_n_div < 0)
9433 dst = isl_basic_map_free(dst);
9434 for (i = 0; i < src->n_div; ++i) {
9435 int j = find_div(dst, src, i);
9436 if (j < 0)
9437 dst = isl_basic_map_free(dst);
9438 if (j == dst_n_div) {
9439 if (!extended) {
9440 int extra = src->n_div - i;
9441 dst = isl_basic_map_cow(dst);
9442 if (!dst)
9443 goto error;
9444 dst = isl_basic_map_extend(dst,
9445 extra, 0, 2 * extra);
9446 extended = 1;
9448 j = isl_basic_map_alloc_div(dst);
9449 if (j < 0)
9450 goto error;
9451 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9452 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9453 dst_n_div++;
9454 dst = isl_basic_map_add_div_constraints(dst, j);
9455 if (!dst)
9456 goto error;
9458 if (j != i)
9459 dst = isl_basic_map_swap_div(dst, i, j);
9460 if (!dst)
9461 goto error;
9463 isl_basic_map_free(src);
9464 return dst;
9465 error:
9466 isl_basic_map_free(src);
9467 isl_basic_map_free(dst);
9468 return NULL;
9471 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9473 int i;
9475 if (!map)
9476 return NULL;
9477 if (map->n == 0)
9478 return map;
9479 map = isl_map_compute_divs(map);
9480 map = isl_map_cow(map);
9481 if (!map)
9482 return NULL;
9484 for (i = 1; i < map->n; ++i)
9485 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9486 for (i = 1; i < map->n; ++i) {
9487 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9488 if (!map->p[i])
9489 return isl_map_free(map);
9492 map = isl_map_unmark_normalized(map);
9493 return map;
9496 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9498 return isl_map_align_divs_internal(map);
9501 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9503 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9506 /* Align the divs of the basic maps in "map" to those
9507 * of the basic maps in "list", as well as to the other basic maps in "map".
9508 * The elements in "list" are assumed to have known divs.
9510 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9511 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9513 int i;
9514 isl_size n;
9516 n = isl_basic_map_list_n_basic_map(list);
9517 map = isl_map_compute_divs(map);
9518 map = isl_map_cow(map);
9519 if (!map || n < 0)
9520 return isl_map_free(map);
9521 if (map->n == 0)
9522 return map;
9524 for (i = 0; i < n; ++i) {
9525 isl_basic_map *bmap;
9527 bmap = isl_basic_map_list_get_basic_map(list, i);
9528 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9529 isl_basic_map_free(bmap);
9531 if (!map->p[0])
9532 return isl_map_free(map);
9534 return isl_map_align_divs_internal(map);
9537 /* Align the divs of each element of "list" to those of "bmap".
9538 * Both "bmap" and the elements of "list" are assumed to have known divs.
9540 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9541 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9543 int i;
9544 isl_size n;
9546 n = isl_basic_map_list_n_basic_map(list);
9547 if (n < 0 || !bmap)
9548 return isl_basic_map_list_free(list);
9550 for (i = 0; i < n; ++i) {
9551 isl_basic_map *bmap_i;
9553 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9554 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9555 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9558 return list;
9561 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9562 __isl_take isl_map *map)
9564 isl_bool ok;
9566 isl_map_align_params_set(&map, &set);
9567 ok = isl_map_compatible_domain(map, set);
9568 if (ok < 0)
9569 goto error;
9570 if (!ok)
9571 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9572 "incompatible spaces", goto error);
9573 map = isl_map_intersect_domain(map, set);
9574 set = isl_map_range(map);
9575 return set;
9576 error:
9577 isl_set_free(set);
9578 isl_map_free(map);
9579 return NULL;
9582 /* There is no need to cow as removing empty parts doesn't change
9583 * the meaning of the set.
9585 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9587 int i;
9589 if (!map)
9590 return NULL;
9592 for (i = map->n - 1; i >= 0; --i)
9593 map = remove_if_empty(map, i);
9595 return map;
9598 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9600 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9603 /* Create a binary relation that maps the shared initial "pos" dimensions
9604 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9606 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9607 __isl_keep isl_basic_set *bset2, int pos)
9609 isl_basic_map *bmap1;
9610 isl_basic_map *bmap2;
9612 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9613 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9614 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9615 isl_dim_out, 0, pos);
9616 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9617 isl_dim_out, 0, pos);
9618 return isl_basic_map_range_product(bmap1, bmap2);
9621 /* Given two basic sets bset1 and bset2, compute the maximal difference
9622 * between the values of dimension pos in bset1 and those in bset2
9623 * for any common value of the parameters and dimensions preceding pos.
9625 static enum isl_lp_result basic_set_maximal_difference_at(
9626 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9627 int pos, isl_int *opt)
9629 isl_basic_map *bmap1;
9630 struct isl_ctx *ctx;
9631 struct isl_vec *obj;
9632 isl_size total;
9633 isl_size nparam;
9634 isl_size dim1;
9635 enum isl_lp_result res;
9637 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9638 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9639 if (nparam < 0 || dim1 < 0 || !bset2)
9640 return isl_lp_error;
9642 bmap1 = join_initial(bset1, bset2, pos);
9643 total = isl_basic_map_dim(bmap1, isl_dim_all);
9644 if (total < 0)
9645 return isl_lp_error;
9647 ctx = bmap1->ctx;
9648 obj = isl_vec_alloc(ctx, 1 + total);
9649 if (!obj)
9650 goto error;
9651 isl_seq_clr(obj->block.data, 1 + total);
9652 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9653 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9654 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9655 opt, NULL, NULL);
9656 isl_basic_map_free(bmap1);
9657 isl_vec_free(obj);
9658 return res;
9659 error:
9660 isl_basic_map_free(bmap1);
9661 return isl_lp_error;
9664 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9665 * for any common value of the parameters and dimensions preceding pos
9666 * in both basic sets, the values of dimension pos in bset1 are
9667 * smaller or larger than those in bset2.
9669 * Returns
9670 * 1 if bset1 follows bset2
9671 * -1 if bset1 precedes bset2
9672 * 0 if bset1 and bset2 are incomparable
9673 * -2 if some error occurred.
9675 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9676 __isl_keep isl_basic_set *bset2, int pos)
9678 isl_int opt;
9679 enum isl_lp_result res;
9680 int cmp;
9682 isl_int_init(opt);
9684 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9686 if (res == isl_lp_empty)
9687 cmp = 0;
9688 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9689 res == isl_lp_unbounded)
9690 cmp = 1;
9691 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9692 cmp = -1;
9693 else
9694 cmp = -2;
9696 isl_int_clear(opt);
9697 return cmp;
9700 /* Given two basic sets bset1 and bset2, check whether
9701 * for any common value of the parameters and dimensions preceding pos
9702 * there is a value of dimension pos in bset1 that is larger
9703 * than a value of the same dimension in bset2.
9705 * Return
9706 * 1 if there exists such a pair
9707 * 0 if there is no such pair, but there is a pair of equal values
9708 * -1 otherwise
9709 * -2 if some error occurred.
9711 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9712 __isl_keep isl_basic_set *bset2, int pos)
9714 isl_bool empty;
9715 isl_basic_map *bmap;
9716 isl_size dim1;
9718 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9719 if (dim1 < 0)
9720 return -2;
9721 bmap = join_initial(bset1, bset2, pos);
9722 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9723 isl_dim_out, dim1 - pos);
9724 empty = isl_basic_map_is_empty(bmap);
9725 if (empty < 0)
9726 goto error;
9727 if (empty) {
9728 isl_basic_map_free(bmap);
9729 return -1;
9731 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9732 isl_dim_out, dim1 - pos);
9733 empty = isl_basic_map_is_empty(bmap);
9734 if (empty < 0)
9735 goto error;
9736 isl_basic_map_free(bmap);
9737 if (empty)
9738 return 0;
9739 return 1;
9740 error:
9741 isl_basic_map_free(bmap);
9742 return -2;
9745 /* Given two sets set1 and set2, check whether
9746 * for any common value of the parameters and dimensions preceding pos
9747 * there is a value of dimension pos in set1 that is larger
9748 * than a value of the same dimension in set2.
9750 * Return
9751 * 1 if there exists such a pair
9752 * 0 if there is no such pair, but there is a pair of equal values
9753 * -1 otherwise
9754 * -2 if some error occurred.
9756 int isl_set_follows_at(__isl_keep isl_set *set1,
9757 __isl_keep isl_set *set2, int pos)
9759 int i, j;
9760 int follows = -1;
9762 if (!set1 || !set2)
9763 return -2;
9765 for (i = 0; i < set1->n; ++i)
9766 for (j = 0; j < set2->n; ++j) {
9767 int f;
9768 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9769 if (f == 1 || f == -2)
9770 return f;
9771 if (f > follows)
9772 follows = f;
9775 return follows;
9778 static isl_bool isl_basic_map_plain_has_fixed_var(
9779 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9781 int i;
9782 int d;
9783 isl_size total;
9785 total = isl_basic_map_dim(bmap, isl_dim_all);
9786 if (total < 0)
9787 return isl_bool_error;
9788 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9789 for (; d+1 > pos; --d)
9790 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9791 break;
9792 if (d != pos)
9793 continue;
9794 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9795 return isl_bool_false;
9796 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9797 return isl_bool_false;
9798 if (!isl_int_is_one(bmap->eq[i][1+d]))
9799 return isl_bool_false;
9800 if (val)
9801 isl_int_neg(*val, bmap->eq[i][0]);
9802 return isl_bool_true;
9804 return isl_bool_false;
9807 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9808 unsigned pos, isl_int *val)
9810 int i;
9811 isl_int v;
9812 isl_int tmp;
9813 isl_bool fixed;
9815 if (!map)
9816 return isl_bool_error;
9817 if (map->n == 0)
9818 return isl_bool_false;
9819 if (map->n == 1)
9820 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9821 isl_int_init(v);
9822 isl_int_init(tmp);
9823 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9824 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9825 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9826 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9827 fixed = isl_bool_false;
9829 if (val)
9830 isl_int_set(*val, v);
9831 isl_int_clear(tmp);
9832 isl_int_clear(v);
9833 return fixed;
9836 static isl_bool isl_basic_set_plain_has_fixed_var(
9837 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9839 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9840 pos, val);
9843 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9844 enum isl_dim_type type, unsigned pos, isl_int *val)
9846 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9847 return isl_bool_error;
9848 return isl_basic_map_plain_has_fixed_var(bmap,
9849 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9852 /* If "bmap" obviously lies on a hyperplane where the given dimension
9853 * has a fixed value, then return that value.
9854 * Otherwise return NaN.
9856 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9857 __isl_keep isl_basic_map *bmap,
9858 enum isl_dim_type type, unsigned pos)
9860 isl_ctx *ctx;
9861 isl_val *v;
9862 isl_bool fixed;
9864 if (!bmap)
9865 return NULL;
9866 ctx = isl_basic_map_get_ctx(bmap);
9867 v = isl_val_alloc(ctx);
9868 if (!v)
9869 return NULL;
9870 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9871 if (fixed < 0)
9872 return isl_val_free(v);
9873 if (fixed) {
9874 isl_int_set_si(v->d, 1);
9875 return v;
9877 isl_val_free(v);
9878 return isl_val_nan(ctx);
9881 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9882 enum isl_dim_type type, unsigned pos, isl_int *val)
9884 if (isl_map_check_range(map, type, pos, 1) < 0)
9885 return isl_bool_error;
9886 return isl_map_plain_has_fixed_var(map,
9887 map_offset(map, type) - 1 + pos, val);
9890 /* If "map" obviously lies on a hyperplane where the given dimension
9891 * has a fixed value, then return that value.
9892 * Otherwise return NaN.
9894 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9895 enum isl_dim_type type, unsigned pos)
9897 isl_ctx *ctx;
9898 isl_val *v;
9899 isl_bool fixed;
9901 if (!map)
9902 return NULL;
9903 ctx = isl_map_get_ctx(map);
9904 v = isl_val_alloc(ctx);
9905 if (!v)
9906 return NULL;
9907 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9908 if (fixed < 0)
9909 return isl_val_free(v);
9910 if (fixed) {
9911 isl_int_set_si(v->d, 1);
9912 return v;
9914 isl_val_free(v);
9915 return isl_val_nan(ctx);
9918 /* If "set" obviously lies on a hyperplane where the given dimension
9919 * has a fixed value, then return that value.
9920 * Otherwise return NaN.
9922 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9923 enum isl_dim_type type, unsigned pos)
9925 return isl_map_plain_get_val_if_fixed(set, type, pos);
9928 /* Return a sequence of values in the same space as "set"
9929 * that are equal to the corresponding set dimensions of "set"
9930 * for those set dimensions that obviously lie on a hyperplane
9931 * where the dimension has a fixed value.
9932 * The other elements are set to NaN.
9934 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
9935 __isl_keep isl_set *set)
9937 int i;
9938 isl_size n;
9939 isl_space *space;
9940 isl_multi_val *mv;
9942 space = isl_space_drop_all_params(isl_set_get_space(set));
9943 mv = isl_multi_val_alloc(space);
9944 n = isl_multi_val_size(mv);
9945 if (n < 0)
9946 return isl_multi_val_free(mv);
9948 for (i = 0; i < n; ++i) {
9949 isl_val *v;
9951 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
9952 mv = isl_multi_val_set_val(mv, i, v);
9955 return mv;
9958 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9959 * then return this fixed value in *val.
9961 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9962 unsigned dim, isl_int *val)
9964 isl_size nparam;
9966 nparam = isl_basic_set_dim(bset, isl_dim_param);
9967 if (nparam < 0)
9968 return isl_bool_error;
9969 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
9972 /* Return -1 if the constraint "c1" should be sorted before "c2"
9973 * and 1 if it should be sorted after "c2".
9974 * Return 0 if the two constraints are the same (up to the constant term).
9976 * In particular, if a constraint involves later variables than another
9977 * then it is sorted after this other constraint.
9978 * uset_gist depends on constraints without existentially quantified
9979 * variables sorting first.
9981 * For constraints that have the same latest variable, those
9982 * with the same coefficient for this latest variable (first in absolute value
9983 * and then in actual value) are grouped together.
9984 * This is useful for detecting pairs of constraints that can
9985 * be chained in their printed representation.
9987 * Finally, within a group, constraints are sorted according to
9988 * their coefficients (excluding the constant term).
9990 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9992 isl_int **c1 = (isl_int **) p1;
9993 isl_int **c2 = (isl_int **) p2;
9994 int l1, l2;
9995 unsigned size = *(unsigned *) arg;
9996 int cmp;
9998 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9999 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10001 if (l1 != l2)
10002 return l1 - l2;
10004 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10005 if (cmp != 0)
10006 return cmp;
10007 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10008 if (cmp != 0)
10009 return -cmp;
10011 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10014 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10015 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10016 * and 0 if the two constraints are the same (up to the constant term).
10018 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10019 isl_int *c1, isl_int *c2)
10021 isl_size total;
10022 unsigned size;
10024 total = isl_basic_map_dim(bmap, isl_dim_all);
10025 if (total < 0)
10026 return -2;
10027 size = total;
10028 return sort_constraint_cmp(&c1, &c2, &size);
10031 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10032 __isl_take isl_basic_map *bmap)
10034 isl_size total;
10035 unsigned size;
10037 if (!bmap)
10038 return NULL;
10039 if (bmap->n_ineq == 0)
10040 return bmap;
10041 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10042 return bmap;
10043 total = isl_basic_map_dim(bmap, isl_dim_all);
10044 if (total < 0)
10045 return isl_basic_map_free(bmap);
10046 size = total;
10047 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10048 &sort_constraint_cmp, &size) < 0)
10049 return isl_basic_map_free(bmap);
10050 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10051 return bmap;
10054 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10055 __isl_take isl_basic_set *bset)
10057 isl_basic_map *bmap = bset_to_bmap(bset);
10058 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10061 __isl_give isl_basic_map *isl_basic_map_normalize(
10062 __isl_take isl_basic_map *bmap)
10064 bmap = isl_basic_map_remove_redundancies(bmap);
10065 bmap = isl_basic_map_sort_constraints(bmap);
10066 return bmap;
10068 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10069 __isl_keep isl_basic_map *bmap2)
10071 int i, cmp;
10072 isl_size total;
10073 isl_space *space1, *space2;
10075 if (!bmap1 || !bmap2)
10076 return -1;
10078 if (bmap1 == bmap2)
10079 return 0;
10080 space1 = isl_basic_map_peek_space(bmap1);
10081 space2 = isl_basic_map_peek_space(bmap2);
10082 cmp = isl_space_cmp(space1, space2);
10083 if (cmp)
10084 return cmp;
10085 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10086 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10087 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10088 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10089 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10090 return 0;
10091 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10092 return 1;
10093 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10094 return -1;
10095 if (bmap1->n_eq != bmap2->n_eq)
10096 return bmap1->n_eq - bmap2->n_eq;
10097 if (bmap1->n_ineq != bmap2->n_ineq)
10098 return bmap1->n_ineq - bmap2->n_ineq;
10099 if (bmap1->n_div != bmap2->n_div)
10100 return bmap1->n_div - bmap2->n_div;
10101 total = isl_basic_map_dim(bmap1, isl_dim_all);
10102 if (total < 0)
10103 return -1;
10104 for (i = 0; i < bmap1->n_eq; ++i) {
10105 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10106 if (cmp)
10107 return cmp;
10109 for (i = 0; i < bmap1->n_ineq; ++i) {
10110 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10111 if (cmp)
10112 return cmp;
10114 for (i = 0; i < bmap1->n_div; ++i) {
10115 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10116 if (cmp)
10117 return cmp;
10119 return 0;
10122 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10123 __isl_keep isl_basic_set *bset2)
10125 return isl_basic_map_plain_cmp(bset1, bset2);
10128 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10130 int i, cmp;
10132 if (set1 == set2)
10133 return 0;
10134 if (set1->n != set2->n)
10135 return set1->n - set2->n;
10137 for (i = 0; i < set1->n; ++i) {
10138 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10139 if (cmp)
10140 return cmp;
10143 return 0;
10146 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10147 __isl_keep isl_basic_map *bmap2)
10149 if (!bmap1 || !bmap2)
10150 return isl_bool_error;
10151 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10154 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10155 __isl_keep isl_basic_set *bset2)
10157 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10158 bset_to_bmap(bset2));
10161 static int qsort_bmap_cmp(const void *p1, const void *p2)
10163 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10164 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10166 return isl_basic_map_plain_cmp(bmap1, bmap2);
10169 /* Sort the basic maps of "map" and remove duplicate basic maps.
10171 * While removing basic maps, we make sure that the basic maps remain
10172 * sorted because isl_map_normalize expects the basic maps of the result
10173 * to be sorted.
10175 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10177 int i, j;
10179 map = isl_map_remove_empty_parts(map);
10180 if (!map)
10181 return NULL;
10182 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10183 for (i = map->n - 1; i >= 1; --i) {
10184 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10185 continue;
10186 isl_basic_map_free(map->p[i-1]);
10187 for (j = i; j < map->n; ++j)
10188 map->p[j - 1] = map->p[j];
10189 map->n--;
10192 return map;
10195 /* Remove obvious duplicates among the basic maps of "map".
10197 * Unlike isl_map_normalize, this function does not remove redundant
10198 * constraints and only removes duplicates that have exactly the same
10199 * constraints in the input. It does sort the constraints and
10200 * the basic maps to ease the detection of duplicates.
10202 * If "map" has already been normalized or if the basic maps are
10203 * disjoint, then there can be no duplicates.
10205 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10207 int i;
10208 isl_basic_map *bmap;
10210 if (!map)
10211 return NULL;
10212 if (map->n <= 1)
10213 return map;
10214 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10215 return map;
10216 for (i = 0; i < map->n; ++i) {
10217 bmap = isl_basic_map_copy(map->p[i]);
10218 bmap = isl_basic_map_sort_constraints(bmap);
10219 if (!bmap)
10220 return isl_map_free(map);
10221 isl_basic_map_free(map->p[i]);
10222 map->p[i] = bmap;
10225 map = sort_and_remove_duplicates(map);
10226 return map;
10229 /* We normalize in place, but if anything goes wrong we need
10230 * to return NULL, so we need to make sure we don't change the
10231 * meaning of any possible other copies of map.
10233 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10235 int i;
10236 struct isl_basic_map *bmap;
10238 if (!map)
10239 return NULL;
10240 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10241 return map;
10242 for (i = 0; i < map->n; ++i) {
10243 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10244 if (!bmap)
10245 goto error;
10246 isl_basic_map_free(map->p[i]);
10247 map->p[i] = bmap;
10250 map = sort_and_remove_duplicates(map);
10251 if (map)
10252 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10253 return map;
10254 error:
10255 isl_map_free(map);
10256 return NULL;
10259 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10261 return set_from_map(isl_map_normalize(set_to_map(set)));
10264 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10265 __isl_keep isl_map *map2)
10267 int i;
10268 isl_bool equal;
10270 if (!map1 || !map2)
10271 return isl_bool_error;
10273 if (map1 == map2)
10274 return isl_bool_true;
10275 equal = isl_map_has_equal_space(map1, map2);
10276 if (equal < 0 || !equal)
10277 return equal;
10279 map1 = isl_map_copy(map1);
10280 map2 = isl_map_copy(map2);
10281 map1 = isl_map_normalize(map1);
10282 map2 = isl_map_normalize(map2);
10283 if (!map1 || !map2)
10284 goto error;
10285 equal = map1->n == map2->n;
10286 for (i = 0; equal && i < map1->n; ++i) {
10287 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10288 if (equal < 0)
10289 goto error;
10291 isl_map_free(map1);
10292 isl_map_free(map2);
10293 return equal;
10294 error:
10295 isl_map_free(map1);
10296 isl_map_free(map2);
10297 return isl_bool_error;
10300 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10301 __isl_keep isl_set *set2)
10303 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10306 /* Return the basic maps in "map" as a list.
10308 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10309 __isl_keep isl_map *map)
10311 int i;
10312 isl_ctx *ctx;
10313 isl_basic_map_list *list;
10315 if (!map)
10316 return NULL;
10317 ctx = isl_map_get_ctx(map);
10318 list = isl_basic_map_list_alloc(ctx, map->n);
10320 for (i = 0; i < map->n; ++i) {
10321 isl_basic_map *bmap;
10323 bmap = isl_basic_map_copy(map->p[i]);
10324 list = isl_basic_map_list_add(list, bmap);
10327 return list;
10330 /* Return the intersection of the elements in the non-empty list "list".
10331 * All elements are assumed to live in the same space.
10333 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10334 __isl_take isl_basic_map_list *list)
10336 int i;
10337 isl_size n;
10338 isl_basic_map *bmap;
10340 n = isl_basic_map_list_n_basic_map(list);
10341 if (n < 0)
10342 goto error;
10343 if (n < 1)
10344 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10345 "expecting non-empty list", goto error);
10347 bmap = isl_basic_map_list_get_basic_map(list, 0);
10348 for (i = 1; i < n; ++i) {
10349 isl_basic_map *bmap_i;
10351 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10352 bmap = isl_basic_map_intersect(bmap, bmap_i);
10355 isl_basic_map_list_free(list);
10356 return bmap;
10357 error:
10358 isl_basic_map_list_free(list);
10359 return NULL;
10362 /* Return the intersection of the elements in the non-empty list "list".
10363 * All elements are assumed to live in the same space.
10365 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10366 __isl_take isl_basic_set_list *list)
10368 return isl_basic_map_list_intersect(list);
10371 /* Return the union of the elements of "list".
10372 * The list is required to have at least one element.
10374 __isl_give isl_set *isl_basic_set_list_union(
10375 __isl_take isl_basic_set_list *list)
10377 int i;
10378 isl_size n;
10379 isl_space *space;
10380 isl_basic_set *bset;
10381 isl_set *set;
10383 n = isl_basic_set_list_n_basic_set(list);
10384 if (n < 0)
10385 goto error;
10386 if (n < 1)
10387 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10388 "expecting non-empty list", goto error);
10390 bset = isl_basic_set_list_get_basic_set(list, 0);
10391 space = isl_basic_set_get_space(bset);
10392 isl_basic_set_free(bset);
10394 set = isl_set_alloc_space(space, n, 0);
10395 for (i = 0; i < n; ++i) {
10396 bset = isl_basic_set_list_get_basic_set(list, i);
10397 set = isl_set_add_basic_set(set, bset);
10400 isl_basic_set_list_free(list);
10401 return set;
10402 error:
10403 isl_basic_set_list_free(list);
10404 return NULL;
10407 /* Return the union of the elements in the non-empty list "list".
10408 * All elements are assumed to live in the same space.
10410 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10412 int i;
10413 isl_size n;
10414 isl_set *set;
10416 n = isl_set_list_n_set(list);
10417 if (n < 0)
10418 goto error;
10419 if (n < 1)
10420 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10421 "expecting non-empty list", goto error);
10423 set = isl_set_list_get_set(list, 0);
10424 for (i = 1; i < n; ++i) {
10425 isl_set *set_i;
10427 set_i = isl_set_list_get_set(list, i);
10428 set = isl_set_union(set, set_i);
10431 isl_set_list_free(list);
10432 return set;
10433 error:
10434 isl_set_list_free(list);
10435 return NULL;
10438 __isl_give isl_basic_map *isl_basic_map_product(
10439 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10441 isl_space *space_result = NULL;
10442 struct isl_basic_map *bmap;
10443 unsigned in1, in2, out1, out2, nparam, total, pos;
10444 struct isl_dim_map *dim_map1, *dim_map2;
10446 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10447 goto error;
10448 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10449 isl_space_copy(bmap2->dim));
10451 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10452 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10453 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10454 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10455 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10457 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10458 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10459 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10460 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10461 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10462 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10463 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10464 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10465 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10466 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10467 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10469 bmap = isl_basic_map_alloc_space(space_result,
10470 bmap1->n_div + bmap2->n_div,
10471 bmap1->n_eq + bmap2->n_eq,
10472 bmap1->n_ineq + bmap2->n_ineq);
10473 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10474 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10475 bmap = isl_basic_map_simplify(bmap);
10476 return isl_basic_map_finalize(bmap);
10477 error:
10478 isl_basic_map_free(bmap1);
10479 isl_basic_map_free(bmap2);
10480 return NULL;
10483 __isl_give isl_basic_map *isl_basic_map_flat_product(
10484 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10486 isl_basic_map *prod;
10488 prod = isl_basic_map_product(bmap1, bmap2);
10489 prod = isl_basic_map_flatten(prod);
10490 return prod;
10493 __isl_give isl_basic_set *isl_basic_set_flat_product(
10494 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10496 return isl_basic_map_flat_range_product(bset1, bset2);
10499 __isl_give isl_basic_map *isl_basic_map_domain_product(
10500 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10502 isl_space *space1, *space2;
10503 isl_space *space_result = NULL;
10504 isl_basic_map *bmap;
10505 isl_size in1, in2, out, nparam;
10506 unsigned total, pos;
10507 struct isl_dim_map *dim_map1, *dim_map2;
10509 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10510 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10511 out = isl_basic_map_dim(bmap1, isl_dim_out);
10512 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10513 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10514 goto error;
10516 space1 = isl_basic_map_get_space(bmap1);
10517 space2 = isl_basic_map_get_space(bmap2);
10518 space_result = isl_space_domain_product(space1, space2);
10520 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10521 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10522 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10523 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10524 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10525 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10526 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10527 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10528 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10529 isl_dim_map_div(dim_map1, bmap1, pos += out);
10530 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10532 bmap = isl_basic_map_alloc_space(space_result,
10533 bmap1->n_div + bmap2->n_div,
10534 bmap1->n_eq + bmap2->n_eq,
10535 bmap1->n_ineq + bmap2->n_ineq);
10536 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10537 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10538 bmap = isl_basic_map_simplify(bmap);
10539 return isl_basic_map_finalize(bmap);
10540 error:
10541 isl_basic_map_free(bmap1);
10542 isl_basic_map_free(bmap2);
10543 return NULL;
10546 __isl_give isl_basic_map *isl_basic_map_range_product(
10547 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10549 isl_bool rational;
10550 isl_space *space_result = NULL;
10551 isl_basic_map *bmap;
10552 isl_size in, out1, out2, nparam;
10553 unsigned total, pos;
10554 struct isl_dim_map *dim_map1, *dim_map2;
10556 rational = isl_basic_map_is_rational(bmap1);
10557 if (rational >= 0 && rational)
10558 rational = isl_basic_map_is_rational(bmap2);
10559 in = isl_basic_map_dim(bmap1, isl_dim_in);
10560 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10561 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10562 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10563 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10564 goto error;
10566 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10567 goto error;
10569 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10570 isl_space_copy(bmap2->dim));
10572 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10573 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10574 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10575 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10576 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10577 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10578 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10579 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10580 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10581 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10582 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10584 bmap = isl_basic_map_alloc_space(space_result,
10585 bmap1->n_div + bmap2->n_div,
10586 bmap1->n_eq + bmap2->n_eq,
10587 bmap1->n_ineq + bmap2->n_ineq);
10588 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10589 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10590 if (rational)
10591 bmap = isl_basic_map_set_rational(bmap);
10592 bmap = isl_basic_map_simplify(bmap);
10593 return isl_basic_map_finalize(bmap);
10594 error:
10595 isl_basic_map_free(bmap1);
10596 isl_basic_map_free(bmap2);
10597 return NULL;
10600 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10601 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10603 isl_basic_map *prod;
10605 prod = isl_basic_map_range_product(bmap1, bmap2);
10606 prod = isl_basic_map_flatten_range(prod);
10607 return prod;
10610 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10611 * and collect the results.
10612 * The result live in the space obtained by calling "space_product"
10613 * on the spaces of "map1" and "map2".
10614 * If "remove_duplicates" is set then the result may contain duplicates
10615 * (even if the inputs do not) and so we try and remove the obvious
10616 * duplicates.
10618 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10619 __isl_take isl_map *map2,
10620 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10621 __isl_take isl_space *right),
10622 __isl_give isl_basic_map *(*basic_map_product)(
10623 __isl_take isl_basic_map *left,
10624 __isl_take isl_basic_map *right),
10625 int remove_duplicates)
10627 unsigned flags = 0;
10628 struct isl_map *result;
10629 int i, j;
10630 isl_bool m;
10632 m = isl_map_has_equal_params(map1, map2);
10633 if (m < 0)
10634 goto error;
10635 if (!m)
10636 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10637 "parameters don't match", goto error);
10639 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10640 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10641 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10643 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10644 isl_space_copy(map2->dim)),
10645 map1->n * map2->n, flags);
10646 if (!result)
10647 goto error;
10648 for (i = 0; i < map1->n; ++i)
10649 for (j = 0; j < map2->n; ++j) {
10650 struct isl_basic_map *part;
10651 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10652 isl_basic_map_copy(map2->p[j]));
10653 if (isl_basic_map_is_empty(part))
10654 isl_basic_map_free(part);
10655 else
10656 result = isl_map_add_basic_map(result, part);
10657 if (!result)
10658 goto error;
10660 if (remove_duplicates)
10661 result = isl_map_remove_obvious_duplicates(result);
10662 isl_map_free(map1);
10663 isl_map_free(map2);
10664 return result;
10665 error:
10666 isl_map_free(map1);
10667 isl_map_free(map2);
10668 return NULL;
10671 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10673 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10674 __isl_take isl_map *map2)
10676 isl_map_align_params_bin(&map1, &map2);
10677 return map_product(map1, map2, &isl_space_product,
10678 &isl_basic_map_product, 0);
10681 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10683 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10684 __isl_take isl_map *map2)
10686 isl_map *prod;
10688 prod = isl_map_product(map1, map2);
10689 prod = isl_map_flatten(prod);
10690 return prod;
10693 /* Given two set A and B, construct its Cartesian product A x B.
10695 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10696 __isl_take isl_set *set2)
10698 return isl_map_range_product(set1, set2);
10701 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10702 __isl_take isl_set *set2)
10704 return isl_map_flat_range_product(set1, set2);
10707 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10709 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10710 __isl_take isl_map *map2)
10712 isl_map_align_params_bin(&map1, &map2);
10713 return map_product(map1, map2, &isl_space_domain_product,
10714 &isl_basic_map_domain_product, 1);
10717 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10719 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10720 __isl_take isl_map *map2)
10722 isl_map_align_params_bin(&map1, &map2);
10723 return map_product(map1, map2, &isl_space_range_product,
10724 &isl_basic_map_range_product, 1);
10727 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10729 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10731 isl_space *space;
10732 isl_size total1, keep1, total2, keep2;
10734 total1 = isl_map_dim(map, isl_dim_in);
10735 total2 = isl_map_dim(map, isl_dim_out);
10736 if (total1 < 0 || total2 < 0)
10737 return isl_map_free(map);
10738 if (!isl_space_domain_is_wrapping(map->dim) ||
10739 !isl_space_range_is_wrapping(map->dim))
10740 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10741 "not a product", return isl_map_free(map));
10743 space = isl_map_get_space(map);
10744 space = isl_space_factor_domain(space);
10745 keep1 = isl_space_dim(space, isl_dim_in);
10746 keep2 = isl_space_dim(space, isl_dim_out);
10747 if (keep1 < 0 || keep2 < 0)
10748 map = isl_map_free(map);
10749 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10750 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10751 map = isl_map_reset_space(map, space);
10753 return map;
10756 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10758 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10760 isl_space *space;
10761 isl_size total1, keep1, total2, keep2;
10763 total1 = isl_map_dim(map, isl_dim_in);
10764 total2 = isl_map_dim(map, isl_dim_out);
10765 if (total1 < 0 || total2 < 0)
10766 return isl_map_free(map);
10767 if (!isl_space_domain_is_wrapping(map->dim) ||
10768 !isl_space_range_is_wrapping(map->dim))
10769 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10770 "not a product", return isl_map_free(map));
10772 space = isl_map_get_space(map);
10773 space = isl_space_factor_range(space);
10774 keep1 = isl_space_dim(space, isl_dim_in);
10775 keep2 = isl_space_dim(space, isl_dim_out);
10776 if (keep1 < 0 || keep2 < 0)
10777 map = isl_map_free(map);
10778 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10779 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10780 map = isl_map_reset_space(map, space);
10782 return map;
10785 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10787 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10789 isl_space *space;
10790 isl_size total, keep;
10792 total = isl_map_dim(map, isl_dim_in);
10793 if (total < 0)
10794 return isl_map_free(map);
10795 if (!isl_space_domain_is_wrapping(map->dim))
10796 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10797 "domain is not a product", return isl_map_free(map));
10799 space = isl_map_get_space(map);
10800 space = isl_space_domain_factor_domain(space);
10801 keep = isl_space_dim(space, isl_dim_in);
10802 if (keep < 0)
10803 map = isl_map_free(map);
10804 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10805 map = isl_map_reset_space(map, space);
10807 return map;
10810 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10812 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10814 isl_space *space;
10815 isl_size total, keep;
10817 total = isl_map_dim(map, isl_dim_in);
10818 if (total < 0)
10819 return isl_map_free(map);
10820 if (!isl_space_domain_is_wrapping(map->dim))
10821 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10822 "domain is not a product", return isl_map_free(map));
10824 space = isl_map_get_space(map);
10825 space = isl_space_domain_factor_range(space);
10826 keep = isl_space_dim(space, isl_dim_in);
10827 if (keep < 0)
10828 map = isl_map_free(map);
10829 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10830 map = isl_map_reset_space(map, space);
10832 return map;
10835 /* Given a map A -> [B -> C], extract the map A -> B.
10837 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10839 isl_space *space;
10840 isl_size total, keep;
10842 total = isl_map_dim(map, isl_dim_out);
10843 if (total < 0)
10844 return isl_map_free(map);
10845 if (!isl_space_range_is_wrapping(map->dim))
10846 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10847 "range is not a product", return isl_map_free(map));
10849 space = isl_map_get_space(map);
10850 space = isl_space_range_factor_domain(space);
10851 keep = isl_space_dim(space, isl_dim_out);
10852 if (keep < 0)
10853 map = isl_map_free(map);
10854 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10855 map = isl_map_reset_space(map, space);
10857 return map;
10860 /* Given a map A -> [B -> C], extract the map A -> C.
10862 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10864 isl_space *space;
10865 isl_size total, keep;
10867 total = isl_map_dim(map, isl_dim_out);
10868 if (total < 0)
10869 return isl_map_free(map);
10870 if (!isl_space_range_is_wrapping(map->dim))
10871 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10872 "range is not a product", return isl_map_free(map));
10874 space = isl_map_get_space(map);
10875 space = isl_space_range_factor_range(space);
10876 keep = isl_space_dim(space, isl_dim_out);
10877 if (keep < 0)
10878 map = isl_map_free(map);
10879 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10880 map = isl_map_reset_space(map, space);
10882 return map;
10885 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10887 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10888 __isl_take isl_map *map2)
10890 isl_map *prod;
10892 prod = isl_map_domain_product(map1, map2);
10893 prod = isl_map_flatten_domain(prod);
10894 return prod;
10897 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10899 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10900 __isl_take isl_map *map2)
10902 isl_map *prod;
10904 prod = isl_map_range_product(map1, map2);
10905 prod = isl_map_flatten_range(prod);
10906 return prod;
10909 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10911 int i;
10912 uint32_t hash = isl_hash_init();
10913 isl_size total;
10915 if (!bmap)
10916 return 0;
10917 bmap = isl_basic_map_copy(bmap);
10918 bmap = isl_basic_map_normalize(bmap);
10919 total = isl_basic_map_dim(bmap, isl_dim_all);
10920 if (total < 0)
10921 return 0;
10922 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10923 for (i = 0; i < bmap->n_eq; ++i) {
10924 uint32_t c_hash;
10925 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10926 isl_hash_hash(hash, c_hash);
10928 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10929 for (i = 0; i < bmap->n_ineq; ++i) {
10930 uint32_t c_hash;
10931 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10932 isl_hash_hash(hash, c_hash);
10934 isl_hash_byte(hash, bmap->n_div & 0xFF);
10935 for (i = 0; i < bmap->n_div; ++i) {
10936 uint32_t c_hash;
10937 if (isl_int_is_zero(bmap->div[i][0]))
10938 continue;
10939 isl_hash_byte(hash, i & 0xFF);
10940 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10941 isl_hash_hash(hash, c_hash);
10943 isl_basic_map_free(bmap);
10944 return hash;
10947 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10949 return isl_basic_map_get_hash(bset_to_bmap(bset));
10952 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10954 int i;
10955 uint32_t hash;
10957 if (!map)
10958 return 0;
10959 map = isl_map_copy(map);
10960 map = isl_map_normalize(map);
10961 if (!map)
10962 return 0;
10964 hash = isl_hash_init();
10965 for (i = 0; i < map->n; ++i) {
10966 uint32_t bmap_hash;
10967 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10968 isl_hash_hash(hash, bmap_hash);
10971 isl_map_free(map);
10973 return hash;
10976 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10978 return isl_map_get_hash(set_to_map(set));
10981 /* Return the number of basic maps in the (current) representation of "map".
10983 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
10985 return map ? map->n : isl_size_error;
10988 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
10990 return set ? set->n : isl_size_error;
10993 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10994 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10996 int i;
10998 if (!map)
10999 return isl_stat_error;
11001 for (i = 0; i < map->n; ++i)
11002 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11003 return isl_stat_error;
11005 return isl_stat_ok;
11008 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11009 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11011 int i;
11013 if (!set)
11014 return isl_stat_error;
11016 for (i = 0; i < set->n; ++i)
11017 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11018 return isl_stat_error;
11020 return isl_stat_ok;
11023 /* Return a list of basic sets, the union of which is equal to "set".
11025 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11026 __isl_keep isl_set *set)
11028 int i;
11029 isl_basic_set_list *list;
11031 if (!set)
11032 return NULL;
11034 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11035 for (i = 0; i < set->n; ++i) {
11036 isl_basic_set *bset;
11038 bset = isl_basic_set_copy(set->p[i]);
11039 list = isl_basic_set_list_add(list, bset);
11042 return list;
11045 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11047 isl_space *space;
11049 if (!bset)
11050 return NULL;
11052 bset = isl_basic_set_cow(bset);
11053 if (!bset)
11054 return NULL;
11056 space = isl_basic_set_get_space(bset);
11057 space = isl_space_lift(space, bset->n_div);
11058 if (!space)
11059 goto error;
11060 isl_space_free(bset->dim);
11061 bset->dim = space;
11062 bset->extra -= bset->n_div;
11063 bset->n_div = 0;
11065 bset = isl_basic_set_finalize(bset);
11067 return bset;
11068 error:
11069 isl_basic_set_free(bset);
11070 return NULL;
11073 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11075 int i;
11076 isl_space *space;
11077 unsigned n_div;
11079 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11081 if (!set)
11082 return NULL;
11084 set = isl_set_cow(set);
11085 if (!set)
11086 return NULL;
11088 n_div = set->p[0]->n_div;
11089 space = isl_set_get_space(set);
11090 space = isl_space_lift(space, n_div);
11091 if (!space)
11092 goto error;
11093 isl_space_free(set->dim);
11094 set->dim = space;
11096 for (i = 0; i < set->n; ++i) {
11097 set->p[i] = isl_basic_set_lift(set->p[i]);
11098 if (!set->p[i])
11099 goto error;
11102 return set;
11103 error:
11104 isl_set_free(set);
11105 return NULL;
11108 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11110 isl_size dim;
11111 int size = 0;
11113 dim = isl_basic_set_dim(bset, isl_dim_all);
11114 if (dim < 0)
11115 return -1;
11116 size += bset->n_eq * (1 + dim);
11117 size += bset->n_ineq * (1 + dim);
11118 size += bset->n_div * (2 + dim);
11120 return size;
11123 int isl_set_size(__isl_keep isl_set *set)
11125 int i;
11126 int size = 0;
11128 if (!set)
11129 return -1;
11131 for (i = 0; i < set->n; ++i)
11132 size += isl_basic_set_size(set->p[i]);
11134 return size;
11137 /* Check if there is any lower bound (if lower == 0) and/or upper
11138 * bound (if upper == 0) on the specified dim.
11140 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11141 enum isl_dim_type type, unsigned pos, int lower, int upper)
11143 int i;
11145 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11146 return isl_bool_error;
11148 pos += isl_basic_map_offset(bmap, type);
11150 for (i = 0; i < bmap->n_div; ++i) {
11151 if (isl_int_is_zero(bmap->div[i][0]))
11152 continue;
11153 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11154 return isl_bool_true;
11157 for (i = 0; i < bmap->n_eq; ++i)
11158 if (!isl_int_is_zero(bmap->eq[i][pos]))
11159 return isl_bool_true;
11161 for (i = 0; i < bmap->n_ineq; ++i) {
11162 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11163 if (sgn > 0)
11164 lower = 1;
11165 if (sgn < 0)
11166 upper = 1;
11169 return lower && upper;
11172 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11173 enum isl_dim_type type, unsigned pos)
11175 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11178 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11179 enum isl_dim_type type, unsigned pos)
11181 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11184 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11185 enum isl_dim_type type, unsigned pos)
11187 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11190 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11191 enum isl_dim_type type, unsigned pos)
11193 int i;
11195 if (!map)
11196 return isl_bool_error;
11198 for (i = 0; i < map->n; ++i) {
11199 isl_bool bounded;
11200 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11201 if (bounded < 0 || !bounded)
11202 return bounded;
11205 return isl_bool_true;
11208 /* Return true if the specified dim is involved in both an upper bound
11209 * and a lower bound.
11211 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11212 enum isl_dim_type type, unsigned pos)
11214 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11217 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11219 static isl_bool has_any_bound(__isl_keep isl_map *map,
11220 enum isl_dim_type type, unsigned pos,
11221 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11222 enum isl_dim_type type, unsigned pos))
11224 int i;
11226 if (!map)
11227 return isl_bool_error;
11229 for (i = 0; i < map->n; ++i) {
11230 isl_bool bounded;
11231 bounded = fn(map->p[i], type, pos);
11232 if (bounded < 0 || bounded)
11233 return bounded;
11236 return isl_bool_false;
11239 /* Return 1 if the specified dim is involved in any lower bound.
11241 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11242 enum isl_dim_type type, unsigned pos)
11244 return has_any_bound(set, type, pos,
11245 &isl_basic_map_dim_has_lower_bound);
11248 /* Return 1 if the specified dim is involved in any upper bound.
11250 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11251 enum isl_dim_type type, unsigned pos)
11253 return has_any_bound(set, type, pos,
11254 &isl_basic_map_dim_has_upper_bound);
11257 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11259 static isl_bool has_bound(__isl_keep isl_map *map,
11260 enum isl_dim_type type, unsigned pos,
11261 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11262 enum isl_dim_type type, unsigned pos))
11264 int i;
11266 if (!map)
11267 return isl_bool_error;
11269 for (i = 0; i < map->n; ++i) {
11270 isl_bool bounded;
11271 bounded = fn(map->p[i], type, pos);
11272 if (bounded < 0 || !bounded)
11273 return bounded;
11276 return isl_bool_true;
11279 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11281 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11282 enum isl_dim_type type, unsigned pos)
11284 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11287 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11289 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11290 enum isl_dim_type type, unsigned pos)
11292 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11295 /* For each of the "n" variables starting at "first", determine
11296 * the sign of the variable and put the results in the first "n"
11297 * elements of the array "signs".
11298 * Sign
11299 * 1 means that the variable is non-negative
11300 * -1 means that the variable is non-positive
11301 * 0 means the variable attains both positive and negative values.
11303 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11304 unsigned first, unsigned n, int *signs)
11306 isl_vec *bound = NULL;
11307 struct isl_tab *tab = NULL;
11308 struct isl_tab_undo *snap;
11309 int i;
11310 isl_size total;
11312 total = isl_basic_set_dim(bset, isl_dim_all);
11313 if (total < 0 || !signs)
11314 return isl_stat_error;
11316 bound = isl_vec_alloc(bset->ctx, 1 + total);
11317 tab = isl_tab_from_basic_set(bset, 0);
11318 if (!bound || !tab)
11319 goto error;
11321 isl_seq_clr(bound->el, bound->size);
11322 isl_int_set_si(bound->el[0], -1);
11324 snap = isl_tab_snap(tab);
11325 for (i = 0; i < n; ++i) {
11326 int empty;
11328 isl_int_set_si(bound->el[1 + first + i], -1);
11329 if (isl_tab_add_ineq(tab, bound->el) < 0)
11330 goto error;
11331 empty = tab->empty;
11332 isl_int_set_si(bound->el[1 + first + i], 0);
11333 if (isl_tab_rollback(tab, snap) < 0)
11334 goto error;
11336 if (empty) {
11337 signs[i] = 1;
11338 continue;
11341 isl_int_set_si(bound->el[1 + first + i], 1);
11342 if (isl_tab_add_ineq(tab, bound->el) < 0)
11343 goto error;
11344 empty = tab->empty;
11345 isl_int_set_si(bound->el[1 + first + i], 0);
11346 if (isl_tab_rollback(tab, snap) < 0)
11347 goto error;
11349 signs[i] = empty ? -1 : 0;
11352 isl_tab_free(tab);
11353 isl_vec_free(bound);
11354 return isl_stat_ok;
11355 error:
11356 isl_tab_free(tab);
11357 isl_vec_free(bound);
11358 return isl_stat_error;
11361 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11362 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11364 if (!bset || !signs)
11365 return isl_stat_error;
11366 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11367 return isl_stat_error;
11369 first += pos(bset->dim, type) - 1;
11370 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11373 /* Is it possible for the integer division "div" to depend (possibly
11374 * indirectly) on any output dimensions?
11376 * If the div is undefined, then we conservatively assume that it
11377 * may depend on them.
11378 * Otherwise, we check if it actually depends on them or on any integer
11379 * divisions that may depend on them.
11381 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11383 int i;
11384 isl_size n_out, n_div;
11385 unsigned o_out, o_div;
11387 if (isl_int_is_zero(bmap->div[div][0]))
11388 return isl_bool_true;
11390 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11391 if (n_out < 0)
11392 return isl_bool_error;
11393 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11395 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11396 return isl_bool_true;
11398 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11399 if (n_div < 0)
11400 return isl_bool_error;
11401 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11403 for (i = 0; i < n_div; ++i) {
11404 isl_bool may_involve;
11406 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11407 continue;
11408 may_involve = div_may_involve_output(bmap, i);
11409 if (may_involve < 0 || may_involve)
11410 return may_involve;
11413 return isl_bool_false;
11416 /* Return the first integer division of "bmap" in the range
11417 * [first, first + n[ that may depend on any output dimensions and
11418 * that has a non-zero coefficient in "c" (where the first coefficient
11419 * in "c" corresponds to integer division "first").
11421 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11422 isl_int *c, int first, int n)
11424 int k;
11426 if (!bmap)
11427 return -1;
11429 for (k = first; k < first + n; ++k) {
11430 isl_bool may_involve;
11432 if (isl_int_is_zero(c[k]))
11433 continue;
11434 may_involve = div_may_involve_output(bmap, k);
11435 if (may_involve < 0)
11436 return -1;
11437 if (may_involve)
11438 return k;
11441 return first + n;
11444 /* Look for a pair of inequality constraints in "bmap" of the form
11446 * -l + i >= 0 or i >= l
11447 * and
11448 * n + l - i >= 0 or i <= l + n
11450 * with n < "m" and i the output dimension at position "pos".
11451 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11452 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11453 * and earlier output dimensions, as well as integer divisions that do
11454 * not involve any of the output dimensions.
11456 * Return the index of the first inequality constraint or bmap->n_ineq
11457 * if no such pair can be found.
11459 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11460 int pos, isl_int m)
11462 int i, j;
11463 isl_ctx *ctx;
11464 isl_size total;
11465 isl_size n_div, n_out;
11466 unsigned o_div, o_out;
11467 int less;
11469 total = isl_basic_map_dim(bmap, isl_dim_all);
11470 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11471 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11472 if (total < 0 || n_out < 0 || n_div < 0)
11473 return -1;
11475 ctx = isl_basic_map_get_ctx(bmap);
11476 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11477 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11478 for (i = 0; i < bmap->n_ineq; ++i) {
11479 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11480 continue;
11481 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11482 n_out - (pos + 1)) != -1)
11483 continue;
11484 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11485 0, n_div) < n_div)
11486 continue;
11487 for (j = i + 1; j < bmap->n_ineq; ++j) {
11488 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11489 ctx->one))
11490 continue;
11491 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11492 bmap->ineq[j] + 1, total))
11493 continue;
11494 break;
11496 if (j >= bmap->n_ineq)
11497 continue;
11498 isl_int_add(bmap->ineq[i][0],
11499 bmap->ineq[i][0], bmap->ineq[j][0]);
11500 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11501 isl_int_sub(bmap->ineq[i][0],
11502 bmap->ineq[i][0], bmap->ineq[j][0]);
11503 if (!less)
11504 continue;
11505 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11506 return i;
11507 else
11508 return j;
11511 return bmap->n_ineq;
11514 /* Return the index of the equality of "bmap" that defines
11515 * the output dimension "pos" in terms of earlier dimensions.
11516 * The equality may also involve integer divisions, as long
11517 * as those integer divisions are defined in terms of
11518 * parameters or input dimensions.
11519 * In this case, *div is set to the number of integer divisions and
11520 * *ineq is set to the number of inequality constraints (provided
11521 * div and ineq are not NULL).
11523 * The equality may also involve a single integer division involving
11524 * the output dimensions (typically only output dimension "pos") as
11525 * long as the coefficient of output dimension "pos" is 1 or -1 and
11526 * there is a pair of constraints i >= l and i <= l + n, with i referring
11527 * to output dimension "pos", l an expression involving only earlier
11528 * dimensions and n smaller than the coefficient of the integer division
11529 * in the equality. In this case, the output dimension can be defined
11530 * in terms of a modulo expression that does not involve the integer division.
11531 * *div is then set to this single integer division and
11532 * *ineq is set to the index of constraint i >= l.
11534 * Return bmap->n_eq if there is no such equality.
11535 * Return -1 on error.
11537 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11538 int pos, int *div, int *ineq)
11540 int j, k, l;
11541 isl_size n_div, n_out;
11542 unsigned o_div, o_out;
11544 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11545 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11546 if (n_out < 0 || n_div < 0)
11547 return -1;
11549 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11550 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11552 if (ineq)
11553 *ineq = bmap->n_ineq;
11554 if (div)
11555 *div = n_div;
11556 for (j = 0; j < bmap->n_eq; ++j) {
11557 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11558 continue;
11559 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11560 n_out - (pos + 1)) != -1)
11561 continue;
11562 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11563 0, n_div);
11564 if (k >= n_div)
11565 return j;
11566 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11567 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11568 continue;
11569 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11570 k + 1, n_div - (k+1)) < n_div)
11571 continue;
11572 l = find_modulo_constraint_pair(bmap, pos,
11573 bmap->eq[j][o_div + k]);
11574 if (l < 0)
11575 return -1;
11576 if (l >= bmap->n_ineq)
11577 continue;
11578 if (div)
11579 *div = k;
11580 if (ineq)
11581 *ineq = l;
11582 return j;
11585 return bmap->n_eq;
11588 /* Check if the given basic map is obviously single-valued.
11589 * In particular, for each output dimension, check that there is
11590 * an equality that defines the output dimension in terms of
11591 * earlier dimensions.
11593 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11595 int i;
11596 isl_size n_out;
11598 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11599 if (n_out < 0)
11600 return isl_bool_error;
11602 for (i = 0; i < n_out; ++i) {
11603 int eq;
11605 eq = isl_basic_map_output_defining_equality(bmap, i,
11606 NULL, NULL);
11607 if (eq < 0)
11608 return isl_bool_error;
11609 if (eq >= bmap->n_eq)
11610 return isl_bool_false;
11613 return isl_bool_true;
11616 /* Check if the given basic map is single-valued.
11617 * We simply compute
11619 * M \circ M^-1
11621 * and check if the result is a subset of the identity mapping.
11623 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11625 isl_space *space;
11626 isl_basic_map *test;
11627 isl_basic_map *id;
11628 isl_bool sv;
11630 sv = isl_basic_map_plain_is_single_valued(bmap);
11631 if (sv < 0 || sv)
11632 return sv;
11634 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11635 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11637 space = isl_basic_map_get_space(bmap);
11638 space = isl_space_map_from_set(isl_space_range(space));
11639 id = isl_basic_map_identity(space);
11641 sv = isl_basic_map_is_subset(test, id);
11643 isl_basic_map_free(test);
11644 isl_basic_map_free(id);
11646 return sv;
11649 /* Check if the given map is obviously single-valued.
11651 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11653 if (!map)
11654 return isl_bool_error;
11655 if (map->n == 0)
11656 return isl_bool_true;
11657 if (map->n >= 2)
11658 return isl_bool_false;
11660 return isl_basic_map_plain_is_single_valued(map->p[0]);
11663 /* Check if the given map is single-valued.
11664 * We simply compute
11666 * M \circ M^-1
11668 * and check if the result is a subset of the identity mapping.
11670 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11672 isl_space *space;
11673 isl_map *test;
11674 isl_map *id;
11675 isl_bool sv;
11677 sv = isl_map_plain_is_single_valued(map);
11678 if (sv < 0 || sv)
11679 return sv;
11681 test = isl_map_reverse(isl_map_copy(map));
11682 test = isl_map_apply_range(test, isl_map_copy(map));
11684 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11685 id = isl_map_identity(space);
11687 sv = isl_map_is_subset(test, id);
11689 isl_map_free(test);
11690 isl_map_free(id);
11692 return sv;
11695 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11697 isl_bool in;
11699 map = isl_map_copy(map);
11700 map = isl_map_reverse(map);
11701 in = isl_map_is_single_valued(map);
11702 isl_map_free(map);
11704 return in;
11707 /* Check if the given map is obviously injective.
11709 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11711 isl_bool in;
11713 map = isl_map_copy(map);
11714 map = isl_map_reverse(map);
11715 in = isl_map_plain_is_single_valued(map);
11716 isl_map_free(map);
11718 return in;
11721 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11723 isl_bool sv;
11725 sv = isl_map_is_single_valued(map);
11726 if (sv < 0 || !sv)
11727 return sv;
11729 return isl_map_is_injective(map);
11732 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11734 return isl_map_is_single_valued(set_to_map(set));
11737 /* Does "map" only map elements to themselves?
11739 * If the domain and range spaces are different, then "map"
11740 * is considered not to be an identity relation, even if it is empty.
11741 * Otherwise, construct the maximal identity relation and
11742 * check whether "map" is a subset of this relation.
11744 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11746 isl_space *space;
11747 isl_map *id;
11748 isl_bool equal, is_identity;
11750 space = isl_map_get_space(map);
11751 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11752 isl_space_free(space);
11753 if (equal < 0 || !equal)
11754 return equal;
11756 id = isl_map_identity(isl_map_get_space(map));
11757 is_identity = isl_map_is_subset(map, id);
11758 isl_map_free(id);
11760 return is_identity;
11763 int isl_map_is_translation(__isl_keep isl_map *map)
11765 int ok;
11766 isl_set *delta;
11768 delta = isl_map_deltas(isl_map_copy(map));
11769 ok = isl_set_is_singleton(delta);
11770 isl_set_free(delta);
11772 return ok;
11775 static int unique(isl_int *p, unsigned pos, unsigned len)
11777 if (isl_seq_first_non_zero(p, pos) != -1)
11778 return 0;
11779 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11780 return 0;
11781 return 1;
11784 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11786 int i, j;
11787 isl_size nvar, n_div;
11788 unsigned ovar;
11790 n_div = isl_basic_set_dim(bset, isl_dim_div);
11791 if (n_div < 0)
11792 return isl_bool_error;
11793 if (n_div != 0)
11794 return isl_bool_false;
11796 nvar = isl_basic_set_dim(bset, isl_dim_set);
11797 if (nvar < 0)
11798 return isl_bool_error;
11799 ovar = isl_space_offset(bset->dim, isl_dim_set);
11800 for (j = 0; j < nvar; ++j) {
11801 int lower = 0, upper = 0;
11802 for (i = 0; i < bset->n_eq; ++i) {
11803 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11804 continue;
11805 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11806 return isl_bool_false;
11807 break;
11809 if (i < bset->n_eq)
11810 continue;
11811 for (i = 0; i < bset->n_ineq; ++i) {
11812 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11813 continue;
11814 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11815 return isl_bool_false;
11816 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11817 lower = 1;
11818 else
11819 upper = 1;
11821 if (!lower || !upper)
11822 return isl_bool_false;
11825 return isl_bool_true;
11828 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11830 if (!set)
11831 return isl_bool_error;
11832 if (set->n != 1)
11833 return isl_bool_false;
11835 return isl_basic_set_is_box(set->p[0]);
11838 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11840 if (!bset)
11841 return isl_bool_error;
11843 return isl_space_is_wrapping(bset->dim);
11846 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11848 if (!set)
11849 return isl_bool_error;
11851 return isl_space_is_wrapping(set->dim);
11854 /* Modify the space of "map" through a call to "change".
11855 * If "can_change" is set (not NULL), then first call it to check
11856 * if the modification is allowed, printing the error message "cannot_change"
11857 * if it is not.
11859 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11860 isl_bool (*can_change)(__isl_keep isl_map *map),
11861 const char *cannot_change,
11862 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11864 isl_bool ok;
11865 isl_space *space;
11867 if (!map)
11868 return NULL;
11870 ok = can_change ? can_change(map) : isl_bool_true;
11871 if (ok < 0)
11872 return isl_map_free(map);
11873 if (!ok)
11874 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11875 return isl_map_free(map));
11877 space = change(isl_map_get_space(map));
11878 map = isl_map_reset_space(map, space);
11880 return map;
11883 /* Is the domain of "map" a wrapped relation?
11885 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11887 if (!map)
11888 return isl_bool_error;
11890 return isl_space_domain_is_wrapping(map->dim);
11893 /* Does "map" have a wrapped relation in both domain and range?
11895 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11897 return isl_space_is_product(isl_map_peek_space(map));
11900 /* Is the range of "map" a wrapped relation?
11902 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11904 if (!map)
11905 return isl_bool_error;
11907 return isl_space_range_is_wrapping(map->dim);
11910 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11912 isl_space *space;
11914 space = isl_basic_map_take_space(bmap);
11915 space = isl_space_wrap(space);
11916 bmap = isl_basic_map_restore_space(bmap, space);
11918 bmap = isl_basic_map_finalize(bmap);
11920 return bset_from_bmap(bmap);
11923 /* Given a map A -> B, return the set (A -> B).
11925 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11927 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11930 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11932 bset = isl_basic_set_cow(bset);
11933 if (!bset)
11934 return NULL;
11936 bset->dim = isl_space_unwrap(bset->dim);
11937 if (!bset->dim)
11938 goto error;
11940 bset = isl_basic_set_finalize(bset);
11942 return bset_to_bmap(bset);
11943 error:
11944 isl_basic_set_free(bset);
11945 return NULL;
11948 /* Given a set (A -> B), return the map A -> B.
11949 * Error out if "set" is not of the form (A -> B).
11951 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11953 return isl_map_change_space(set, &isl_set_is_wrapping,
11954 "not a wrapping set", &isl_space_unwrap);
11957 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11958 enum isl_dim_type type)
11960 isl_space *space;
11962 space = isl_basic_map_take_space(bmap);
11963 space = isl_space_reset(space, type);
11964 bmap = isl_basic_map_restore_space(bmap, space);
11966 bmap = isl_basic_map_mark_final(bmap);
11968 return bmap;
11971 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11972 enum isl_dim_type type)
11974 int i;
11975 isl_space *space;
11977 if (!map)
11978 return NULL;
11980 if (!isl_space_is_named_or_nested(map->dim, type))
11981 return map;
11983 map = isl_map_cow(map);
11984 if (!map)
11985 return NULL;
11987 for (i = 0; i < map->n; ++i) {
11988 map->p[i] = isl_basic_map_reset(map->p[i], type);
11989 if (!map->p[i])
11990 goto error;
11993 space = isl_map_take_space(map);
11994 space = isl_space_reset(space, type);
11995 map = isl_map_restore_space(map, space);
11997 return map;
11998 error:
11999 isl_map_free(map);
12000 return NULL;
12003 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12005 isl_space *space;
12007 space = isl_basic_map_take_space(bmap);
12008 space = isl_space_flatten(space);
12009 bmap = isl_basic_map_restore_space(bmap, space);
12011 bmap = isl_basic_map_mark_final(bmap);
12013 return bmap;
12016 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12018 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12021 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12022 __isl_take isl_basic_map *bmap)
12024 isl_space *space;
12026 space = isl_basic_map_take_space(bmap);
12027 space = isl_space_flatten_domain(space);
12028 bmap = isl_basic_map_restore_space(bmap, space);
12030 bmap = isl_basic_map_mark_final(bmap);
12032 return bmap;
12035 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12036 __isl_take isl_basic_map *bmap)
12038 isl_space *space;
12040 space = isl_basic_map_take_space(bmap);
12041 space = isl_space_flatten_range(space);
12042 bmap = isl_basic_map_restore_space(bmap, space);
12044 bmap = isl_basic_map_mark_final(bmap);
12046 return bmap;
12049 /* Remove any internal structure from the spaces of domain and range of "map".
12051 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12053 if (!map)
12054 return NULL;
12056 if (!map->dim->nested[0] && !map->dim->nested[1])
12057 return map;
12059 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12062 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12064 return set_from_map(isl_map_flatten(set_to_map(set)));
12067 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12069 isl_space *space, *flat_space;
12070 isl_map *map;
12072 space = isl_set_get_space(set);
12073 flat_space = isl_space_flatten(isl_space_copy(space));
12074 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12075 flat_space));
12076 map = isl_map_intersect_domain(map, set);
12078 return map;
12081 /* Remove any internal structure from the space of the domain of "map".
12083 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12085 if (!map)
12086 return NULL;
12088 if (!map->dim->nested[0])
12089 return map;
12091 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12094 /* Remove any internal structure from the space of the range of "map".
12096 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12098 if (!map)
12099 return NULL;
12101 if (!map->dim->nested[1])
12102 return map;
12104 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12107 /* Reorder the dimensions of "bmap" according to the given dim_map
12108 * and set the dimension specification to "space" and
12109 * perform Gaussian elimination on the result.
12111 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12112 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12114 isl_basic_map *res;
12115 unsigned flags;
12116 isl_size n_div;
12118 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12119 if (n_div < 0 || !space || !dim_map)
12120 goto error;
12122 flags = bmap->flags;
12123 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12124 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12125 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12126 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12127 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12128 if (res)
12129 res->flags = flags;
12130 res = isl_basic_map_gauss(res, NULL);
12131 res = isl_basic_map_finalize(res);
12132 return res;
12133 error:
12134 isl_dim_map_free(dim_map);
12135 isl_basic_map_free(bmap);
12136 isl_space_free(space);
12137 return NULL;
12140 /* Reorder the dimensions of "map" according to given reordering.
12142 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12143 __isl_take isl_reordering *r)
12145 int i;
12146 struct isl_dim_map *dim_map;
12148 map = isl_map_cow(map);
12149 dim_map = isl_dim_map_from_reordering(r);
12150 if (!map || !r || !dim_map)
12151 goto error;
12153 for (i = 0; i < map->n; ++i) {
12154 struct isl_dim_map *dim_map_i;
12155 isl_space *space;
12157 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12159 space = isl_reordering_get_space(r);
12160 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12162 if (!map->p[i])
12163 goto error;
12166 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12167 map = isl_map_unmark_normalized(map);
12169 isl_reordering_free(r);
12170 isl_dim_map_free(dim_map);
12171 return map;
12172 error:
12173 isl_dim_map_free(dim_map);
12174 isl_map_free(map);
12175 isl_reordering_free(r);
12176 return NULL;
12179 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12180 __isl_take isl_reordering *r)
12182 return set_from_map(isl_map_realign(set_to_map(set), r));
12185 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12186 __isl_take isl_space *model)
12188 isl_ctx *ctx;
12189 isl_bool aligned;
12191 if (!map || !model)
12192 goto error;
12194 ctx = isl_space_get_ctx(model);
12195 if (!isl_space_has_named_params(model))
12196 isl_die(ctx, isl_error_invalid,
12197 "model has unnamed parameters", goto error);
12198 if (isl_map_check_named_params(map) < 0)
12199 goto error;
12200 aligned = isl_map_space_has_equal_params(map, model);
12201 if (aligned < 0)
12202 goto error;
12203 if (!aligned) {
12204 isl_reordering *exp;
12206 exp = isl_parameter_alignment_reordering(map->dim, model);
12207 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12208 map = isl_map_realign(map, exp);
12211 isl_space_free(model);
12212 return map;
12213 error:
12214 isl_space_free(model);
12215 isl_map_free(map);
12216 return NULL;
12219 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12220 __isl_take isl_space *model)
12222 return isl_map_align_params(set, model);
12225 /* Align the parameters of "bmap" to those of "model", introducing
12226 * additional parameters if needed.
12228 __isl_give isl_basic_map *isl_basic_map_align_params(
12229 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12231 isl_ctx *ctx;
12232 isl_bool equal_params;
12234 if (!bmap || !model)
12235 goto error;
12237 ctx = isl_space_get_ctx(model);
12238 if (!isl_space_has_named_params(model))
12239 isl_die(ctx, isl_error_invalid,
12240 "model has unnamed parameters", goto error);
12241 if (isl_basic_map_check_named_params(bmap) < 0)
12242 goto error;
12243 equal_params = isl_space_has_equal_params(bmap->dim, model);
12244 if (equal_params < 0)
12245 goto error;
12246 if (!equal_params) {
12247 isl_reordering *exp;
12248 struct isl_dim_map *dim_map;
12250 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12251 exp = isl_reordering_extend_space(exp,
12252 isl_basic_map_get_space(bmap));
12253 dim_map = isl_dim_map_from_reordering(exp);
12254 bmap = isl_basic_map_realign(bmap,
12255 isl_reordering_get_space(exp),
12256 isl_dim_map_extend(dim_map, bmap));
12257 isl_reordering_free(exp);
12258 isl_dim_map_free(dim_map);
12261 isl_space_free(model);
12262 return bmap;
12263 error:
12264 isl_space_free(model);
12265 isl_basic_map_free(bmap);
12266 return NULL;
12269 /* Do "bset" and "space" have the same parameters?
12271 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12272 __isl_keep isl_space *space)
12274 isl_space *bset_space;
12276 bset_space = isl_basic_set_peek_space(bset);
12277 return isl_space_has_equal_params(bset_space, space);
12280 /* Do "map" and "space" have the same parameters?
12282 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12283 __isl_keep isl_space *space)
12285 isl_space *map_space;
12287 map_space = isl_map_peek_space(map);
12288 return isl_space_has_equal_params(map_space, space);
12291 /* Do "set" and "space" have the same parameters?
12293 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12294 __isl_keep isl_space *space)
12296 return isl_map_space_has_equal_params(set_to_map(set), space);
12299 /* Align the parameters of "bset" to those of "model", introducing
12300 * additional parameters if needed.
12302 __isl_give isl_basic_set *isl_basic_set_align_params(
12303 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12305 return isl_basic_map_align_params(bset, model);
12308 /* Drop all parameters not referenced by "map".
12310 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12312 int i;
12313 isl_size n;
12315 n = isl_map_dim(map, isl_dim_param);
12316 if (isl_map_check_named_params(map) < 0 || n < 0)
12317 return isl_map_free(map);
12319 for (i = n - 1; i >= 0; i--) {
12320 isl_bool involves;
12322 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12323 if (involves < 0)
12324 return isl_map_free(map);
12325 if (!involves)
12326 map = isl_map_project_out(map, isl_dim_param, i, 1);
12329 return map;
12332 /* Drop all parameters not referenced by "set".
12334 __isl_give isl_set *isl_set_drop_unused_params(
12335 __isl_take isl_set *set)
12337 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12340 /* Drop all parameters not referenced by "bmap".
12342 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12343 __isl_take isl_basic_map *bmap)
12345 isl_size nparam;
12346 int i;
12348 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12349 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12350 return isl_basic_map_free(bmap);
12352 for (i = nparam - 1; i >= 0; i--) {
12353 isl_bool involves;
12355 involves = isl_basic_map_involves_dims(bmap,
12356 isl_dim_param, i, 1);
12357 if (involves < 0)
12358 return isl_basic_map_free(bmap);
12359 if (!involves)
12360 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12363 return bmap;
12366 /* Drop all parameters not referenced by "bset".
12368 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12369 __isl_take isl_basic_set *bset)
12371 return bset_from_bmap(isl_basic_map_drop_unused_params(
12372 bset_to_bmap(bset)));
12375 /* Given a tuple of identifiers "tuple" in a space that corresponds
12376 * to that of "set", if any of those identifiers appear as parameters
12377 * in "set", then equate those parameters with the corresponding
12378 * set dimensions and project out the parameters.
12379 * The result therefore has no such parameters.
12381 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12382 __isl_keep isl_multi_id *tuple)
12384 int i;
12385 isl_size n;
12386 isl_space *set_space, *tuple_space;
12388 set_space = isl_set_peek_space(set);
12389 tuple_space = isl_multi_id_peek_space(tuple);
12390 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12391 return isl_set_free(set);
12392 n = isl_multi_id_size(tuple);
12393 if (n < 0)
12394 return isl_set_free(set);
12395 for (i = 0; i < n; ++i) {
12396 isl_id *id;
12397 int pos;
12399 id = isl_multi_id_get_at(tuple, i);
12400 if (!id)
12401 return isl_set_free(set);
12402 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12403 isl_id_free(id);
12404 if (pos < 0)
12405 continue;
12406 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12407 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12409 return set;
12412 /* Bind the set dimensions of "set" to parameters with identifiers
12413 * specified by "tuple", living in the same space as "set".
12415 * If no parameters with these identifiers appear in "set" already,
12416 * then the set dimensions are simply reinterpreted as parameters.
12417 * Otherwise, the parameters are first equated to the corresponding
12418 * set dimensions.
12420 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12421 __isl_take isl_multi_id *tuple)
12423 isl_space *space;
12425 set = equate_params(set, tuple);
12426 space = isl_set_get_space(set);
12427 space = isl_space_bind_set(space, tuple);
12428 isl_multi_id_free(tuple);
12429 set = isl_set_reset_space(set, space);
12431 return set;
12434 /* Given a tuple of identifiers "tuple" in a space that corresponds
12435 * to the domain of "map", if any of those identifiers appear as parameters
12436 * in "map", then equate those parameters with the corresponding
12437 * input dimensions and project out the parameters.
12438 * The result therefore has no such parameters.
12440 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12441 __isl_keep isl_multi_id *tuple)
12443 int i;
12444 isl_size n;
12445 isl_space *map_space, *tuple_space;
12447 map_space = isl_map_peek_space(map);
12448 tuple_space = isl_multi_id_peek_space(tuple);
12449 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12450 return isl_map_free(map);
12451 n = isl_multi_id_size(tuple);
12452 if (n < 0)
12453 return isl_map_free(map);
12454 for (i = 0; i < n; ++i) {
12455 isl_id *id;
12456 int pos;
12458 id = isl_multi_id_get_at(tuple, i);
12459 if (!id)
12460 return isl_map_free(map);
12461 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12462 isl_id_free(id);
12463 if (pos < 0)
12464 continue;
12465 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12466 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12468 return map;
12471 /* Bind the input dimensions of "map" to parameters with identifiers
12472 * specified by "tuple", living in the domain space of "map".
12474 * If no parameters with these identifiers appear in "map" already,
12475 * then the input dimensions are simply reinterpreted as parameters.
12476 * Otherwise, the parameters are first equated to the corresponding
12477 * input dimensions.
12479 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12480 __isl_take isl_multi_id *tuple)
12482 isl_space *space;
12483 isl_set *set;
12485 map = map_equate_params(map, tuple);
12486 space = isl_map_get_space(map);
12487 space = isl_space_bind_map_domain(space, tuple);
12488 isl_multi_id_free(tuple);
12489 set = set_from_map(isl_map_reset_space(map, space));
12491 return set;
12494 /* Bind the output dimensions of "map" to parameters with identifiers
12495 * specified by "tuple", living in the range space of "map".
12497 * Since binding is more easily implemented on the domain,
12498 * bind the input dimensions of the inverse of "map".
12500 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12501 __isl_take isl_multi_id *tuple)
12503 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12506 /* Insert a domain corresponding to "tuple"
12507 * into the nullary or unary relation "set".
12508 * The result has an extra initial tuple and is therefore
12509 * either a unary or binary relation.
12510 * Any parameters with identifiers in "tuple" are reinterpreted
12511 * as the corresponding domain dimensions.
12513 static __isl_give isl_map *unbind_params_insert_domain(
12514 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12516 isl_space *space;
12517 isl_reordering *r;
12519 space = isl_set_peek_space(set);
12520 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12521 isl_multi_id_free(tuple);
12523 return isl_map_realign(set_to_map(set), r);
12526 /* Construct a set with "tuple" as domain from the parameter domain "set".
12527 * Any parameters with identifiers in "tuple" are reinterpreted
12528 * as the corresponding set dimensions.
12530 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12531 __isl_take isl_multi_id *tuple)
12533 isl_bool is_params;
12535 is_params = isl_set_is_params(set);
12536 if (is_params < 0)
12537 set = isl_set_free(set);
12538 else if (!is_params)
12539 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12540 "expecting parameter domain", set = isl_set_free(set));
12541 return set_from_map(unbind_params_insert_domain(set, tuple));
12544 /* Construct a map with "domain" as domain and "set" as range.
12545 * Any parameters with identifiers in "domain" are reinterpreted
12546 * as the corresponding domain dimensions.
12548 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12549 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12551 isl_bool is_params;
12553 is_params = isl_set_is_params(set);
12554 if (is_params < 0)
12555 set = isl_set_free(set);
12556 else if (is_params)
12557 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12558 "expecting proper set", set = isl_set_free(set));
12559 return unbind_params_insert_domain(set, domain);
12562 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12563 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12564 enum isl_dim_type c2, enum isl_dim_type c3,
12565 enum isl_dim_type c4, enum isl_dim_type c5)
12567 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12568 struct isl_mat *mat;
12569 int i, j, k;
12570 int pos;
12571 isl_size total;
12573 total = isl_basic_map_dim(bmap, isl_dim_all);
12574 if (total < 0)
12575 return NULL;
12576 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12577 if (!mat)
12578 return NULL;
12579 for (i = 0; i < bmap->n_eq; ++i)
12580 for (j = 0, pos = 0; j < 5; ++j) {
12581 int off = isl_basic_map_offset(bmap, c[j]);
12582 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12583 if (dim < 0)
12584 return isl_mat_free(mat);
12585 for (k = 0; k < dim; ++k) {
12586 isl_int_set(mat->row[i][pos],
12587 bmap->eq[i][off + k]);
12588 ++pos;
12592 return mat;
12595 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12596 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12597 enum isl_dim_type c2, enum isl_dim_type c3,
12598 enum isl_dim_type c4, enum isl_dim_type c5)
12600 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12601 struct isl_mat *mat;
12602 int i, j, k;
12603 int pos;
12604 isl_size total;
12606 total = isl_basic_map_dim(bmap, isl_dim_all);
12607 if (total < 0)
12608 return NULL;
12609 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12610 if (!mat)
12611 return NULL;
12612 for (i = 0; i < bmap->n_ineq; ++i)
12613 for (j = 0, pos = 0; j < 5; ++j) {
12614 int off = isl_basic_map_offset(bmap, c[j]);
12615 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12616 if (dim < 0)
12617 return isl_mat_free(mat);
12618 for (k = 0; k < dim; ++k) {
12619 isl_int_set(mat->row[i][pos],
12620 bmap->ineq[i][off + k]);
12621 ++pos;
12625 return mat;
12628 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12629 __isl_take isl_space *space,
12630 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12631 enum isl_dim_type c2, enum isl_dim_type c3,
12632 enum isl_dim_type c4, enum isl_dim_type c5)
12634 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12635 isl_basic_map *bmap = NULL;
12636 isl_size dim;
12637 unsigned total;
12638 unsigned extra;
12639 int i, j, k, l;
12640 int pos;
12642 dim = isl_space_dim(space, isl_dim_all);
12643 if (dim < 0 || !eq || !ineq)
12644 goto error;
12646 if (eq->n_col != ineq->n_col)
12647 isl_die(space->ctx, isl_error_invalid,
12648 "equalities and inequalities matrices should have "
12649 "same number of columns", goto error);
12651 total = 1 + dim;
12653 if (eq->n_col < total)
12654 isl_die(space->ctx, isl_error_invalid,
12655 "number of columns too small", goto error);
12657 extra = eq->n_col - total;
12659 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12660 eq->n_row, ineq->n_row);
12661 if (!bmap)
12662 goto error;
12663 for (i = 0; i < extra; ++i) {
12664 k = isl_basic_map_alloc_div(bmap);
12665 if (k < 0)
12666 goto error;
12667 isl_int_set_si(bmap->div[k][0], 0);
12669 for (i = 0; i < eq->n_row; ++i) {
12670 l = isl_basic_map_alloc_equality(bmap);
12671 if (l < 0)
12672 goto error;
12673 for (j = 0, pos = 0; j < 5; ++j) {
12674 int off = isl_basic_map_offset(bmap, c[j]);
12675 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12676 if (dim < 0)
12677 goto error;
12678 for (k = 0; k < dim; ++k) {
12679 isl_int_set(bmap->eq[l][off + k],
12680 eq->row[i][pos]);
12681 ++pos;
12685 for (i = 0; i < ineq->n_row; ++i) {
12686 l = isl_basic_map_alloc_inequality(bmap);
12687 if (l < 0)
12688 goto error;
12689 for (j = 0, pos = 0; j < 5; ++j) {
12690 int off = isl_basic_map_offset(bmap, c[j]);
12691 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12692 if (dim < 0)
12693 goto error;
12694 for (k = 0; k < dim; ++k) {
12695 isl_int_set(bmap->ineq[l][off + k],
12696 ineq->row[i][pos]);
12697 ++pos;
12702 isl_space_free(space);
12703 isl_mat_free(eq);
12704 isl_mat_free(ineq);
12706 bmap = isl_basic_map_simplify(bmap);
12707 return isl_basic_map_finalize(bmap);
12708 error:
12709 isl_space_free(space);
12710 isl_mat_free(eq);
12711 isl_mat_free(ineq);
12712 isl_basic_map_free(bmap);
12713 return NULL;
12716 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12717 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12718 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12720 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12721 c1, c2, c3, c4, isl_dim_in);
12724 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12725 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12726 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12728 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12729 c1, c2, c3, c4, isl_dim_in);
12732 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12733 __isl_take isl_space *space,
12734 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12735 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12737 isl_basic_map *bmap;
12738 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12739 c1, c2, c3, c4, isl_dim_in);
12740 return bset_from_bmap(bmap);
12743 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12745 if (!bmap)
12746 return isl_bool_error;
12748 return isl_space_can_zip(bmap->dim);
12751 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12753 if (!map)
12754 return isl_bool_error;
12756 return isl_space_can_zip(map->dim);
12759 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12760 * (A -> C) -> (B -> D).
12762 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12764 unsigned pos;
12765 isl_size n_in;
12766 isl_size n1;
12767 isl_size n2;
12769 if (!bmap)
12770 return NULL;
12772 if (!isl_basic_map_can_zip(bmap))
12773 isl_die(bmap->ctx, isl_error_invalid,
12774 "basic map cannot be zipped", goto error);
12775 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12776 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12777 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12778 if (n_in < 0 || n1 < 0 || n2 < 0)
12779 return isl_basic_map_free(bmap);
12780 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12781 bmap = isl_basic_map_cow(bmap);
12782 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12783 if (!bmap)
12784 return NULL;
12785 bmap->dim = isl_space_zip(bmap->dim);
12786 if (!bmap->dim)
12787 goto error;
12788 bmap = isl_basic_map_mark_final(bmap);
12789 return bmap;
12790 error:
12791 isl_basic_map_free(bmap);
12792 return NULL;
12795 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12796 * (A -> C) -> (B -> D).
12798 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12800 if (!map)
12801 return NULL;
12803 if (!isl_map_can_zip(map))
12804 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12805 goto error);
12807 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
12808 error:
12809 isl_map_free(map);
12810 return NULL;
12813 /* Can we apply isl_basic_map_curry to "bmap"?
12814 * That is, does it have a nested relation in its domain?
12816 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12818 if (!bmap)
12819 return isl_bool_error;
12821 return isl_space_can_curry(bmap->dim);
12824 /* Can we apply isl_map_curry to "map"?
12825 * That is, does it have a nested relation in its domain?
12827 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12829 if (!map)
12830 return isl_bool_error;
12832 return isl_space_can_curry(map->dim);
12835 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12836 * A -> (B -> C).
12838 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12841 if (!bmap)
12842 return NULL;
12844 if (!isl_basic_map_can_curry(bmap))
12845 isl_die(bmap->ctx, isl_error_invalid,
12846 "basic map cannot be curried", goto error);
12847 bmap = isl_basic_map_cow(bmap);
12848 if (!bmap)
12849 return NULL;
12850 bmap->dim = isl_space_curry(bmap->dim);
12851 if (!bmap->dim)
12852 goto error;
12853 bmap = isl_basic_map_mark_final(bmap);
12854 return bmap;
12855 error:
12856 isl_basic_map_free(bmap);
12857 return NULL;
12860 /* Given a map (A -> B) -> C, return the corresponding map
12861 * A -> (B -> C).
12863 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12865 return isl_map_change_space(map, &isl_map_can_curry,
12866 "map cannot be curried", &isl_space_curry);
12869 /* Can isl_map_range_curry be applied to "map"?
12870 * That is, does it have a nested relation in its range,
12871 * the domain of which is itself a nested relation?
12873 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12875 if (!map)
12876 return isl_bool_error;
12878 return isl_space_can_range_curry(map->dim);
12881 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12882 * A -> (B -> (C -> D)).
12884 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12886 return isl_map_change_space(map, &isl_map_can_range_curry,
12887 "map range cannot be curried",
12888 &isl_space_range_curry);
12891 /* Can we apply isl_basic_map_uncurry to "bmap"?
12892 * That is, does it have a nested relation in its domain?
12894 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12896 if (!bmap)
12897 return isl_bool_error;
12899 return isl_space_can_uncurry(bmap->dim);
12902 /* Can we apply isl_map_uncurry to "map"?
12903 * That is, does it have a nested relation in its domain?
12905 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12907 if (!map)
12908 return isl_bool_error;
12910 return isl_space_can_uncurry(map->dim);
12913 /* Given a basic map A -> (B -> C), return the corresponding basic map
12914 * (A -> B) -> C.
12916 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12919 if (!bmap)
12920 return NULL;
12922 if (!isl_basic_map_can_uncurry(bmap))
12923 isl_die(bmap->ctx, isl_error_invalid,
12924 "basic map cannot be uncurried",
12925 return isl_basic_map_free(bmap));
12926 bmap = isl_basic_map_cow(bmap);
12927 if (!bmap)
12928 return NULL;
12929 bmap->dim = isl_space_uncurry(bmap->dim);
12930 if (!bmap->dim)
12931 return isl_basic_map_free(bmap);
12932 bmap = isl_basic_map_mark_final(bmap);
12933 return bmap;
12936 /* Given a map A -> (B -> C), return the corresponding map
12937 * (A -> B) -> C.
12939 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12941 return isl_map_change_space(map, &isl_map_can_uncurry,
12942 "map cannot be uncurried", &isl_space_uncurry);
12945 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12946 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12948 return isl_map_equate(set, type1, pos1, type2, pos2);
12951 /* Construct a basic map where the given dimensions are equal to each other.
12953 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12954 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12956 isl_basic_map *bmap = NULL;
12957 int i;
12958 isl_size total;
12960 total = isl_space_dim(space, isl_dim_all);
12961 if (total < 0 ||
12962 isl_space_check_range(space, type1, pos1, 1) < 0 ||
12963 isl_space_check_range(space, type2, pos2, 1) < 0)
12964 goto error;
12966 if (type1 == type2 && pos1 == pos2)
12967 return isl_basic_map_universe(space);
12969 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12970 i = isl_basic_map_alloc_equality(bmap);
12971 if (i < 0)
12972 goto error;
12973 isl_seq_clr(bmap->eq[i], 1 + total);
12974 pos1 += isl_basic_map_offset(bmap, type1);
12975 pos2 += isl_basic_map_offset(bmap, type2);
12976 isl_int_set_si(bmap->eq[i][pos1], -1);
12977 isl_int_set_si(bmap->eq[i][pos2], 1);
12978 bmap = isl_basic_map_finalize(bmap);
12979 isl_space_free(space);
12980 return bmap;
12981 error:
12982 isl_space_free(space);
12983 isl_basic_map_free(bmap);
12984 return NULL;
12987 /* Add a constraint imposing that the given two dimensions are equal.
12989 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12990 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12992 isl_basic_map *eq;
12994 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12996 bmap = isl_basic_map_intersect(bmap, eq);
12998 return bmap;
13001 /* Add a constraint imposing that the given two dimensions are equal.
13003 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13004 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13006 isl_basic_map *bmap;
13008 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13010 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13012 return map;
13015 /* Add a constraint imposing that the given two dimensions have opposite values.
13017 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13018 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13020 isl_basic_map *bmap = NULL;
13021 int i;
13022 isl_size total;
13024 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13025 return isl_map_free(map);
13026 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13027 return isl_map_free(map);
13029 total = isl_map_dim(map, isl_dim_all);
13030 if (total < 0)
13031 return isl_map_free(map);
13032 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13033 i = isl_basic_map_alloc_equality(bmap);
13034 if (i < 0)
13035 goto error;
13036 isl_seq_clr(bmap->eq[i], 1 + total);
13037 pos1 += isl_basic_map_offset(bmap, type1);
13038 pos2 += isl_basic_map_offset(bmap, type2);
13039 isl_int_set_si(bmap->eq[i][pos1], 1);
13040 isl_int_set_si(bmap->eq[i][pos2], 1);
13041 bmap = isl_basic_map_finalize(bmap);
13043 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13045 return map;
13046 error:
13047 isl_basic_map_free(bmap);
13048 isl_map_free(map);
13049 return NULL;
13052 /* Construct a constraint imposing that the value of the first dimension is
13053 * greater than or equal to that of the second.
13055 static __isl_give isl_constraint *constraint_order_ge(
13056 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13057 enum isl_dim_type type2, int pos2)
13059 isl_constraint *c;
13061 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13062 isl_space_check_range(space, type2, pos2, 1) < 0)
13063 space = isl_space_free(space);
13064 if (!space)
13065 return NULL;
13067 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13069 if (type1 == type2 && pos1 == pos2)
13070 return c;
13072 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13073 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13075 return c;
13078 /* Add a constraint imposing that the value of the first dimension is
13079 * greater than or equal to that of the second.
13081 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13082 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13084 isl_constraint *c;
13085 isl_space *space;
13087 if (type1 == type2 && pos1 == pos2)
13088 return bmap;
13089 space = isl_basic_map_get_space(bmap);
13090 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13091 bmap = isl_basic_map_add_constraint(bmap, c);
13093 return bmap;
13096 /* Add a constraint imposing that the value of the first dimension is
13097 * greater than or equal to that of the second.
13099 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13100 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13102 isl_constraint *c;
13103 isl_space *space;
13105 if (type1 == type2 && pos1 == pos2)
13106 return map;
13107 space = isl_map_get_space(map);
13108 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13109 map = isl_map_add_constraint(map, c);
13111 return map;
13114 /* Add a constraint imposing that the value of the first dimension is
13115 * less than or equal to that of the second.
13117 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13118 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13120 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13123 /* Construct a basic map where the value of the first dimension is
13124 * greater than that of the second.
13126 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13127 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13129 isl_basic_map *bmap = NULL;
13130 int i;
13131 isl_size total;
13133 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13134 isl_space_check_range(space, type2, pos2, 1) < 0)
13135 goto error;
13137 if (type1 == type2 && pos1 == pos2)
13138 return isl_basic_map_empty(space);
13140 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13141 total = isl_basic_map_dim(bmap, isl_dim_all);
13142 i = isl_basic_map_alloc_inequality(bmap);
13143 if (total < 0 || i < 0)
13144 return isl_basic_map_free(bmap);
13145 isl_seq_clr(bmap->ineq[i], 1 + total);
13146 pos1 += isl_basic_map_offset(bmap, type1);
13147 pos2 += isl_basic_map_offset(bmap, type2);
13148 isl_int_set_si(bmap->ineq[i][pos1], 1);
13149 isl_int_set_si(bmap->ineq[i][pos2], -1);
13150 isl_int_set_si(bmap->ineq[i][0], -1);
13151 bmap = isl_basic_map_finalize(bmap);
13153 return bmap;
13154 error:
13155 isl_space_free(space);
13156 isl_basic_map_free(bmap);
13157 return NULL;
13160 /* Add a constraint imposing that the value of the first dimension is
13161 * greater than that of the second.
13163 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13164 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13166 isl_basic_map *gt;
13168 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13170 bmap = isl_basic_map_intersect(bmap, gt);
13172 return bmap;
13175 /* Add a constraint imposing that the value of the first dimension is
13176 * greater than that of the second.
13178 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13179 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13181 isl_basic_map *bmap;
13183 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13185 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13187 return map;
13190 /* Add a constraint imposing that the value of the first dimension is
13191 * smaller than that of the second.
13193 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13194 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13196 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13199 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13200 int pos)
13202 isl_aff *div;
13203 isl_local_space *ls;
13205 if (!bmap)
13206 return NULL;
13208 if (!isl_basic_map_divs_known(bmap))
13209 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13210 "some divs are unknown", return NULL);
13212 ls = isl_basic_map_get_local_space(bmap);
13213 div = isl_local_space_get_div(ls, pos);
13214 isl_local_space_free(ls);
13216 return div;
13219 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13220 int pos)
13222 return isl_basic_map_get_div(bset, pos);
13225 /* Plug in "subs" for dimension "type", "pos" of "bset".
13227 * Let i be the dimension to replace and let "subs" be of the form
13229 * f/d
13231 * Any integer division with a non-zero coefficient for i,
13233 * floor((a i + g)/m)
13235 * is replaced by
13237 * floor((a f + d g)/(m d))
13239 * Constraints of the form
13241 * a i + g
13243 * are replaced by
13245 * a f + d g
13247 * We currently require that "subs" is an integral expression.
13248 * Handling rational expressions may require us to add stride constraints
13249 * as we do in isl_basic_set_preimage_multi_aff.
13251 __isl_give isl_basic_set *isl_basic_set_substitute(
13252 __isl_take isl_basic_set *bset,
13253 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13255 int i;
13256 isl_int v;
13257 isl_ctx *ctx;
13258 isl_size n_div;
13260 if (bset && isl_basic_set_plain_is_empty(bset))
13261 return bset;
13263 bset = isl_basic_set_cow(bset);
13264 if (!bset || !subs)
13265 goto error;
13267 ctx = isl_basic_set_get_ctx(bset);
13268 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
13269 isl_die(ctx, isl_error_invalid,
13270 "spaces don't match", goto error);
13271 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
13272 if (n_div < 0)
13273 goto error;
13274 if (n_div != 0)
13275 isl_die(ctx, isl_error_unsupported,
13276 "cannot handle divs yet", goto error);
13277 if (!isl_int_is_one(subs->v->el[0]))
13278 isl_die(ctx, isl_error_invalid,
13279 "can only substitute integer expressions", goto error);
13281 pos += isl_basic_set_offset(bset, type);
13283 isl_int_init(v);
13285 for (i = 0; i < bset->n_eq; ++i) {
13286 if (isl_int_is_zero(bset->eq[i][pos]))
13287 continue;
13288 isl_int_set(v, bset->eq[i][pos]);
13289 isl_int_set_si(bset->eq[i][pos], 0);
13290 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
13291 v, subs->v->el + 1, subs->v->size - 1);
13294 for (i = 0; i < bset->n_ineq; ++i) {
13295 if (isl_int_is_zero(bset->ineq[i][pos]))
13296 continue;
13297 isl_int_set(v, bset->ineq[i][pos]);
13298 isl_int_set_si(bset->ineq[i][pos], 0);
13299 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
13300 v, subs->v->el + 1, subs->v->size - 1);
13303 for (i = 0; i < bset->n_div; ++i) {
13304 if (isl_int_is_zero(bset->div[i][1 + pos]))
13305 continue;
13306 isl_int_set(v, bset->div[i][1 + pos]);
13307 isl_int_set_si(bset->div[i][1 + pos], 0);
13308 isl_seq_combine(bset->div[i] + 1,
13309 subs->v->el[0], bset->div[i] + 1,
13310 v, subs->v->el + 1, subs->v->size - 1);
13311 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
13314 isl_int_clear(v);
13316 bset = isl_basic_set_simplify(bset);
13317 return isl_basic_set_finalize(bset);
13318 error:
13319 isl_basic_set_free(bset);
13320 return NULL;
13323 /* Plug in "subs" for dimension "type", "pos" of "set".
13325 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13326 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13328 int i;
13330 if (set && isl_set_plain_is_empty(set))
13331 return set;
13333 set = isl_set_cow(set);
13334 if (!set || !subs)
13335 goto error;
13337 for (i = set->n - 1; i >= 0; --i) {
13338 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
13339 set = set_from_map(remove_if_empty(set_to_map(set), i));
13340 if (!set)
13341 return NULL;
13344 return set;
13345 error:
13346 isl_set_free(set);
13347 return NULL;
13350 /* Check if the range of "ma" is compatible with the domain or range
13351 * (depending on "type") of "bmap".
13353 static isl_stat check_basic_map_compatible_range_multi_aff(
13354 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13355 __isl_keep isl_multi_aff *ma)
13357 isl_bool m;
13358 isl_space *ma_space;
13360 ma_space = isl_multi_aff_get_space(ma);
13362 m = isl_space_has_equal_params(bmap->dim, ma_space);
13363 if (m < 0)
13364 goto error;
13365 if (!m)
13366 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13367 "parameters don't match", goto error);
13368 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13369 if (m < 0)
13370 goto error;
13371 if (!m)
13372 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13373 "spaces don't match", goto error);
13375 isl_space_free(ma_space);
13376 return isl_stat_ok;
13377 error:
13378 isl_space_free(ma_space);
13379 return isl_stat_error;
13382 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13383 * coefficients before the transformed range of dimensions,
13384 * the "n_after" coefficients after the transformed range of dimensions
13385 * and the coefficients of the other divs in "bmap".
13387 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13388 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13390 int i;
13391 isl_size n_param;
13392 isl_size n_set;
13393 isl_local_space *ls;
13395 if (n_div == 0)
13396 return bmap;
13398 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13399 n_param = isl_local_space_dim(ls, isl_dim_param);
13400 n_set = isl_local_space_dim(ls, isl_dim_set);
13401 if (n_param < 0 || n_set < 0)
13402 return isl_basic_map_free(bmap);
13404 for (i = 0; i < n_div; ++i) {
13405 int o_bmap = 0, o_ls = 0;
13407 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13408 o_bmap += 1 + 1 + n_param;
13409 o_ls += 1 + 1 + n_param;
13410 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13411 o_bmap += n_before;
13412 isl_seq_cpy(bmap->div[i] + o_bmap,
13413 ls->div->row[i] + o_ls, n_set);
13414 o_bmap += n_set;
13415 o_ls += n_set;
13416 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13417 o_bmap += n_after;
13418 isl_seq_cpy(bmap->div[i] + o_bmap,
13419 ls->div->row[i] + o_ls, n_div);
13420 o_bmap += n_div;
13421 o_ls += n_div;
13422 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13423 bmap = isl_basic_map_add_div_constraints(bmap, i);
13424 if (!bmap)
13425 goto error;
13428 isl_local_space_free(ls);
13429 return bmap;
13430 error:
13431 isl_local_space_free(ls);
13432 return isl_basic_map_free(bmap);
13435 /* How many stride constraints does "ma" enforce?
13436 * That is, how many of the affine expressions have a denominator
13437 * different from one?
13439 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13441 int i;
13442 int strides = 0;
13444 for (i = 0; i < ma->n; ++i)
13445 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13446 strides++;
13448 return strides;
13451 /* For each affine expression in ma of the form
13453 * x_i = (f_i y + h_i)/m_i
13455 * with m_i different from one, add a constraint to "bmap"
13456 * of the form
13458 * f_i y + h_i = m_i alpha_i
13460 * with alpha_i an additional existentially quantified variable.
13462 * The input variables of "ma" correspond to a subset of the variables
13463 * of "bmap". There are "n_before" variables in "bmap" before this
13464 * subset and "n_after" variables after this subset.
13465 * The integer divisions of the affine expressions in "ma" are assumed
13466 * to have been aligned. There are "n_div_ma" of them and
13467 * they appear first in "bmap", straight after the "n_after" variables.
13469 static __isl_give isl_basic_map *add_ma_strides(
13470 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13471 int n_before, int n_after, int n_div_ma)
13473 int i, k;
13474 int div;
13475 isl_size total;
13476 isl_size n_param;
13477 isl_size n_in;
13479 total = isl_basic_map_dim(bmap, isl_dim_all);
13480 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13481 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13482 if (total < 0 || n_param < 0 || n_in < 0)
13483 return isl_basic_map_free(bmap);
13484 for (i = 0; i < ma->n; ++i) {
13485 int o_bmap = 0, o_ma = 1;
13487 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13488 continue;
13489 div = isl_basic_map_alloc_div(bmap);
13490 k = isl_basic_map_alloc_equality(bmap);
13491 if (div < 0 || k < 0)
13492 goto error;
13493 isl_int_set_si(bmap->div[div][0], 0);
13494 isl_seq_cpy(bmap->eq[k] + o_bmap,
13495 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13496 o_bmap += 1 + n_param;
13497 o_ma += 1 + n_param;
13498 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13499 o_bmap += n_before;
13500 isl_seq_cpy(bmap->eq[k] + o_bmap,
13501 ma->u.p[i]->v->el + o_ma, n_in);
13502 o_bmap += n_in;
13503 o_ma += n_in;
13504 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13505 o_bmap += n_after;
13506 isl_seq_cpy(bmap->eq[k] + o_bmap,
13507 ma->u.p[i]->v->el + o_ma, n_div_ma);
13508 o_bmap += n_div_ma;
13509 o_ma += n_div_ma;
13510 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13511 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13512 total++;
13515 return bmap;
13516 error:
13517 isl_basic_map_free(bmap);
13518 return NULL;
13521 /* Replace the domain or range space (depending on "type) of "space" by "set".
13523 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13524 enum isl_dim_type type, __isl_take isl_space *set)
13526 if (type == isl_dim_in) {
13527 space = isl_space_range(space);
13528 space = isl_space_map_from_domain_and_range(set, space);
13529 } else {
13530 space = isl_space_domain(space);
13531 space = isl_space_map_from_domain_and_range(space, set);
13534 return space;
13537 /* Compute the preimage of the domain or range (depending on "type")
13538 * of "bmap" under the function represented by "ma".
13539 * In other words, plug in "ma" in the domain or range of "bmap".
13540 * The result is a basic map that lives in the same space as "bmap"
13541 * except that the domain or range has been replaced by
13542 * the domain space of "ma".
13544 * If bmap is represented by
13546 * A(p) + S u + B x + T v + C(divs) >= 0,
13548 * where u and x are input and output dimensions if type == isl_dim_out
13549 * while x and v are input and output dimensions if type == isl_dim_in,
13550 * and ma is represented by
13552 * x = D(p) + F(y) + G(divs')
13554 * then the result is
13556 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13558 * The divs in the input set are similarly adjusted.
13559 * In particular
13561 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13563 * becomes
13565 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13566 * B_i G(divs') + c_i(divs))/n_i)
13568 * If bmap is not a rational map and if F(y) involves any denominators
13570 * x_i = (f_i y + h_i)/m_i
13572 * then additional constraints are added to ensure that we only
13573 * map back integer points. That is we enforce
13575 * f_i y + h_i = m_i alpha_i
13577 * with alpha_i an additional existentially quantified variable.
13579 * We first copy over the divs from "ma".
13580 * Then we add the modified constraints and divs from "bmap".
13581 * Finally, we add the stride constraints, if needed.
13583 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13584 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13585 __isl_take isl_multi_aff *ma)
13587 int i, k;
13588 isl_space *space;
13589 isl_basic_map *res = NULL;
13590 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13591 isl_int f, c1, c2, g;
13592 isl_bool rational;
13593 int strides;
13595 isl_int_init(f);
13596 isl_int_init(c1);
13597 isl_int_init(c2);
13598 isl_int_init(g);
13600 ma = isl_multi_aff_align_divs(ma);
13601 if (!bmap || !ma)
13602 goto error;
13603 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13604 goto error;
13606 if (type == isl_dim_in) {
13607 n_before = 0;
13608 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13609 } else {
13610 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13611 n_after = 0;
13613 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13614 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13615 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13616 goto error;
13618 space = isl_multi_aff_get_domain_space(ma);
13619 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13620 rational = isl_basic_map_is_rational(bmap);
13621 strides = rational ? 0 : multi_aff_strides(ma);
13622 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13623 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13624 if (rational)
13625 res = isl_basic_map_set_rational(res);
13627 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13628 if (isl_basic_map_alloc_div(res) < 0)
13629 goto error;
13631 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13632 if (!res)
13633 goto error;
13635 for (i = 0; i < bmap->n_eq; ++i) {
13636 k = isl_basic_map_alloc_equality(res);
13637 if (k < 0)
13638 goto error;
13639 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13640 n_after, n_div_ma, n_div_bmap,
13641 f, c1, c2, g, 0) < 0)
13642 goto error;
13645 for (i = 0; i < bmap->n_ineq; ++i) {
13646 k = isl_basic_map_alloc_inequality(res);
13647 if (k < 0)
13648 goto error;
13649 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13650 n_after, n_div_ma, n_div_bmap,
13651 f, c1, c2, g, 0) < 0)
13652 goto error;
13655 for (i = 0; i < bmap->n_div; ++i) {
13656 if (isl_int_is_zero(bmap->div[i][0])) {
13657 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13658 continue;
13660 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13661 n_before, n_after, n_div_ma, n_div_bmap,
13662 f, c1, c2, g, 1) < 0)
13663 goto error;
13666 if (strides)
13667 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13669 isl_int_clear(f);
13670 isl_int_clear(c1);
13671 isl_int_clear(c2);
13672 isl_int_clear(g);
13673 isl_basic_map_free(bmap);
13674 isl_multi_aff_free(ma);
13675 res = isl_basic_map_simplify(res);
13676 return isl_basic_map_finalize(res);
13677 error:
13678 isl_int_clear(f);
13679 isl_int_clear(c1);
13680 isl_int_clear(c2);
13681 isl_int_clear(g);
13682 isl_basic_map_free(bmap);
13683 isl_multi_aff_free(ma);
13684 isl_basic_map_free(res);
13685 return NULL;
13688 /* Compute the preimage of "bset" under the function represented by "ma".
13689 * In other words, plug in "ma" in "bset". The result is a basic set
13690 * that lives in the domain space of "ma".
13692 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13693 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13695 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13698 /* Compute the preimage of the domain of "bmap" under the function
13699 * represented by "ma".
13700 * In other words, plug in "ma" in the domain of "bmap".
13701 * The result is a basic map that lives in the same space as "bmap"
13702 * except that the domain has been replaced by the domain space of "ma".
13704 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13705 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13707 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13710 /* Compute the preimage of the range of "bmap" under the function
13711 * represented by "ma".
13712 * In other words, plug in "ma" in the range of "bmap".
13713 * The result is a basic map that lives in the same space as "bmap"
13714 * except that the range has been replaced by the domain space of "ma".
13716 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13717 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13719 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13722 /* Check if the range of "ma" is compatible with the domain or range
13723 * (depending on "type") of "map".
13724 * Return isl_stat_error if anything is wrong.
13726 static isl_stat check_map_compatible_range_multi_aff(
13727 __isl_keep isl_map *map, enum isl_dim_type type,
13728 __isl_keep isl_multi_aff *ma)
13730 isl_bool m;
13731 isl_space *ma_space;
13733 ma_space = isl_multi_aff_get_space(ma);
13734 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13735 isl_space_free(ma_space);
13736 if (m < 0)
13737 return isl_stat_error;
13738 if (!m)
13739 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13740 "spaces don't match", return isl_stat_error);
13741 return isl_stat_ok;
13744 /* Compute the preimage of the domain or range (depending on "type")
13745 * of "map" under the function represented by "ma".
13746 * In other words, plug in "ma" in the domain or range of "map".
13747 * The result is a map that lives in the same space as "map"
13748 * except that the domain or range has been replaced by
13749 * the domain space of "ma".
13751 * The parameters are assumed to have been aligned.
13753 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13754 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13756 int i;
13757 isl_space *space;
13759 map = isl_map_cow(map);
13760 ma = isl_multi_aff_align_divs(ma);
13761 if (!map || !ma)
13762 goto error;
13763 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13764 goto error;
13766 for (i = 0; i < map->n; ++i) {
13767 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13768 isl_multi_aff_copy(ma));
13769 if (!map->p[i])
13770 goto error;
13773 space = isl_multi_aff_get_domain_space(ma);
13774 space = isl_space_set(isl_map_get_space(map), type, space);
13776 isl_space_free(isl_map_take_space(map));
13777 map = isl_map_restore_space(map, space);
13778 if (!map)
13779 goto error;
13781 isl_multi_aff_free(ma);
13782 if (map->n > 1)
13783 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13784 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13785 return map;
13786 error:
13787 isl_multi_aff_free(ma);
13788 isl_map_free(map);
13789 return NULL;
13792 /* Compute the preimage of the domain or range (depending on "type")
13793 * of "map" under the function represented by "ma".
13794 * In other words, plug in "ma" in the domain or range of "map".
13795 * The result is a map that lives in the same space as "map"
13796 * except that the domain or range has been replaced by
13797 * the domain space of "ma".
13799 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13800 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13802 isl_bool aligned;
13804 if (!map || !ma)
13805 goto error;
13807 aligned = isl_map_space_has_equal_params(map, ma->space);
13808 if (aligned < 0)
13809 goto error;
13810 if (aligned)
13811 return map_preimage_multi_aff(map, type, ma);
13813 if (isl_map_check_named_params(map) < 0)
13814 goto error;
13815 if (!isl_space_has_named_params(ma->space))
13816 isl_die(map->ctx, isl_error_invalid,
13817 "unaligned unnamed parameters", goto error);
13818 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13819 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13821 return map_preimage_multi_aff(map, type, ma);
13822 error:
13823 isl_multi_aff_free(ma);
13824 return isl_map_free(map);
13827 /* Compute the preimage of "set" under the function represented by "ma".
13828 * In other words, plug in "ma" in "set". The result is a set
13829 * that lives in the domain space of "ma".
13831 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13832 __isl_take isl_multi_aff *ma)
13834 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13837 /* Compute the preimage of the domain of "map" under the function
13838 * represented by "ma".
13839 * In other words, plug in "ma" in the domain of "map".
13840 * The result is a map that lives in the same space as "map"
13841 * except that the domain has been replaced by the domain space of "ma".
13843 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13844 __isl_take isl_multi_aff *ma)
13846 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13849 /* Compute the preimage of the range of "map" under the function
13850 * represented by "ma".
13851 * In other words, plug in "ma" in the range of "map".
13852 * The result is a map that lives in the same space as "map"
13853 * except that the range has been replaced by the domain space of "ma".
13855 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13856 __isl_take isl_multi_aff *ma)
13858 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13861 /* Compute the preimage of "map" under the function represented by "pma".
13862 * In other words, plug in "pma" in the domain or range of "map".
13863 * The result is a map that lives in the same space as "map",
13864 * except that the space of type "type" has been replaced by
13865 * the domain space of "pma".
13867 * The parameters of "map" and "pma" are assumed to have been aligned.
13869 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13870 __isl_take isl_map *map, enum isl_dim_type type,
13871 __isl_take isl_pw_multi_aff *pma)
13873 int i;
13874 isl_map *res;
13876 if (!pma)
13877 goto error;
13879 if (pma->n == 0) {
13880 isl_pw_multi_aff_free(pma);
13881 res = isl_map_empty(isl_map_get_space(map));
13882 isl_map_free(map);
13883 return res;
13886 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13887 isl_multi_aff_copy(pma->p[0].maff));
13888 if (type == isl_dim_in)
13889 res = isl_map_intersect_domain(res,
13890 isl_map_copy(pma->p[0].set));
13891 else
13892 res = isl_map_intersect_range(res,
13893 isl_map_copy(pma->p[0].set));
13895 for (i = 1; i < pma->n; ++i) {
13896 isl_map *res_i;
13898 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13899 isl_multi_aff_copy(pma->p[i].maff));
13900 if (type == isl_dim_in)
13901 res_i = isl_map_intersect_domain(res_i,
13902 isl_map_copy(pma->p[i].set));
13903 else
13904 res_i = isl_map_intersect_range(res_i,
13905 isl_map_copy(pma->p[i].set));
13906 res = isl_map_union(res, res_i);
13909 isl_pw_multi_aff_free(pma);
13910 isl_map_free(map);
13911 return res;
13912 error:
13913 isl_pw_multi_aff_free(pma);
13914 isl_map_free(map);
13915 return NULL;
13918 /* Compute the preimage of "map" under the function represented by "pma".
13919 * In other words, plug in "pma" in the domain or range of "map".
13920 * The result is a map that lives in the same space as "map",
13921 * except that the space of type "type" has been replaced by
13922 * the domain space of "pma".
13924 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13925 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13927 isl_bool aligned;
13929 if (!map || !pma)
13930 goto error;
13932 aligned = isl_map_space_has_equal_params(map, pma->dim);
13933 if (aligned < 0)
13934 goto error;
13935 if (aligned)
13936 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13938 if (isl_map_check_named_params(map) < 0)
13939 goto error;
13940 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13941 goto error;
13942 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13943 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13945 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13946 error:
13947 isl_pw_multi_aff_free(pma);
13948 return isl_map_free(map);
13951 /* Compute the preimage of "set" under the function represented by "pma".
13952 * In other words, plug in "pma" in "set". The result is a set
13953 * that lives in the domain space of "pma".
13955 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13956 __isl_take isl_pw_multi_aff *pma)
13958 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13961 /* Compute the preimage of the domain of "map" under the function
13962 * represented by "pma".
13963 * In other words, plug in "pma" in the domain of "map".
13964 * The result is a map that lives in the same space as "map",
13965 * except that domain space has been replaced by the domain space of "pma".
13967 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13968 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13970 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13973 /* Compute the preimage of the range of "map" under the function
13974 * represented by "pma".
13975 * In other words, plug in "pma" in the range of "map".
13976 * The result is a map that lives in the same space as "map",
13977 * except that range space has been replaced by the domain space of "pma".
13979 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13980 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13982 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13985 /* Compute the preimage of "map" under the function represented by "mpa".
13986 * In other words, plug in "mpa" in the domain or range of "map".
13987 * The result is a map that lives in the same space as "map",
13988 * except that the space of type "type" has been replaced by
13989 * the domain space of "mpa".
13991 * If the map does not involve any constraints that refer to the
13992 * dimensions of the substituted space, then the only possible
13993 * effect of "mpa" on the map is to map the space to a different space.
13994 * We create a separate isl_multi_aff to effectuate this change
13995 * in order to avoid spurious splitting of the map along the pieces
13996 * of "mpa".
13997 * If "mpa" has a non-trivial explicit domain, however,
13998 * then the full substitution should be performed.
14000 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14001 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14003 isl_size n;
14004 isl_bool full;
14005 isl_pw_multi_aff *pma;
14007 n = isl_map_dim(map, type);
14008 if (n < 0 || !mpa)
14009 goto error;
14011 full = isl_map_involves_dims(map, type, 0, n);
14012 if (full >= 0 && !full)
14013 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14014 if (full < 0)
14015 goto error;
14016 if (!full) {
14017 isl_space *space;
14018 isl_multi_aff *ma;
14020 space = isl_multi_pw_aff_get_space(mpa);
14021 isl_multi_pw_aff_free(mpa);
14022 ma = isl_multi_aff_zero(space);
14023 return isl_map_preimage_multi_aff(map, type, ma);
14026 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14027 return isl_map_preimage_pw_multi_aff(map, type, pma);
14028 error:
14029 isl_map_free(map);
14030 isl_multi_pw_aff_free(mpa);
14031 return NULL;
14034 /* Compute the preimage of "map" under the function represented by "mpa".
14035 * In other words, plug in "mpa" in the domain "map".
14036 * The result is a map that lives in the same space as "map",
14037 * except that domain space has been replaced by the domain space of "mpa".
14039 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14040 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14042 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14045 /* Compute the preimage of "set" by the function represented by "mpa".
14046 * In other words, plug in "mpa" in "set".
14048 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14049 __isl_take isl_multi_pw_aff *mpa)
14051 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14054 /* Return a copy of the equality constraints of "bset" as a matrix.
14056 __isl_give isl_mat *isl_basic_set_extract_equalities(
14057 __isl_keep isl_basic_set *bset)
14059 isl_ctx *ctx;
14060 isl_size total;
14062 total = isl_basic_set_dim(bset, isl_dim_all);
14063 if (total < 0)
14064 return NULL;
14066 ctx = isl_basic_set_get_ctx(bset);
14067 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14070 /* Are the "n" "coefficients" starting at "first" of the integer division
14071 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14072 * to each other?
14073 * The "coefficient" at position 0 is the denominator.
14074 * The "coefficient" at position 1 is the constant term.
14076 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14077 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14078 unsigned first, unsigned n)
14080 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14081 return isl_bool_error;
14082 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14083 return isl_bool_error;
14084 return isl_seq_eq(bmap1->div[pos1] + first,
14085 bmap2->div[pos2] + first, n);
14088 /* Are the integer division expressions at position "pos1" in "bmap1" and
14089 * "pos2" in "bmap2" equal to each other, except that the constant terms
14090 * are different?
14092 isl_bool isl_basic_map_equal_div_expr_except_constant(
14093 __isl_keep isl_basic_map *bmap1, int pos1,
14094 __isl_keep isl_basic_map *bmap2, int pos2)
14096 isl_bool equal;
14097 isl_size total, total2;
14099 total = isl_basic_map_dim(bmap1, isl_dim_all);
14100 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14101 if (total < 0 || total2 < 0)
14102 return isl_bool_error;
14103 if (total != total2)
14104 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14105 "incomparable div expressions", return isl_bool_error);
14106 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14107 0, 1);
14108 if (equal < 0 || !equal)
14109 return equal;
14110 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14111 1, 1);
14112 if (equal < 0 || equal)
14113 return isl_bool_not(equal);
14114 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14115 2, total);
14118 /* Replace the numerator of the constant term of the integer division
14119 * expression at position "div" in "bmap" by "value".
14120 * The caller guarantees that this does not change the meaning
14121 * of the input.
14123 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14124 __isl_take isl_basic_map *bmap, int div, int value)
14126 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14127 return isl_basic_map_free(bmap);
14129 isl_int_set_si(bmap->div[div][1], value);
14131 return bmap;
14134 /* Is the point "inner" internal to inequality constraint "ineq"
14135 * of "bset"?
14136 * The point is considered to be internal to the inequality constraint,
14137 * if it strictly lies on the positive side of the inequality constraint,
14138 * or if it lies on the constraint and the constraint is lexico-positive.
14140 static isl_bool is_internal(__isl_keep isl_vec *inner,
14141 __isl_keep isl_basic_set *bset, int ineq)
14143 isl_ctx *ctx;
14144 int pos;
14145 isl_size total;
14147 if (!inner || !bset)
14148 return isl_bool_error;
14150 ctx = isl_basic_set_get_ctx(bset);
14151 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14152 &ctx->normalize_gcd);
14153 if (!isl_int_is_zero(ctx->normalize_gcd))
14154 return isl_int_is_nonneg(ctx->normalize_gcd);
14156 total = isl_basic_set_dim(bset, isl_dim_all);
14157 if (total < 0)
14158 return isl_bool_error;
14159 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14160 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14163 /* Tighten the inequality constraints of "bset" that are outward with respect
14164 * to the point "vec".
14165 * That is, tighten the constraints that are not satisfied by "vec".
14167 * "vec" is a point internal to some superset S of "bset" that is used
14168 * to make the subsets of S disjoint, by tightening one half of the constraints
14169 * that separate two subsets. In particular, the constraints of S
14170 * are all satisfied by "vec" and should not be tightened.
14171 * Of the internal constraints, those that have "vec" on the outside
14172 * are tightened. The shared facet is included in the adjacent subset
14173 * with the opposite constraint.
14174 * For constraints that saturate "vec", this criterion cannot be used
14175 * to determine which of the two sides should be tightened.
14176 * Instead, the sign of the first non-zero coefficient is used
14177 * to make this choice. Note that this second criterion is never used
14178 * on the constraints of S since "vec" is interior to "S".
14180 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14181 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14183 int j;
14185 bset = isl_basic_set_cow(bset);
14186 if (!bset)
14187 return NULL;
14188 for (j = 0; j < bset->n_ineq; ++j) {
14189 isl_bool internal;
14191 internal = is_internal(vec, bset, j);
14192 if (internal < 0)
14193 return isl_basic_set_free(bset);
14194 if (internal)
14195 continue;
14196 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14199 return bset;
14202 /* Replace the variables x of type "type" starting at "first" in "bmap"
14203 * by x' with x = M x' with M the matrix trans.
14204 * That is, replace the corresponding coefficients c by c M.
14206 * The transformation matrix should be a square matrix.
14208 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14209 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14210 __isl_take isl_mat *trans)
14212 unsigned pos;
14214 bmap = isl_basic_map_cow(bmap);
14215 if (!bmap || !trans)
14216 goto error;
14218 if (trans->n_row != trans->n_col)
14219 isl_die(trans->ctx, isl_error_invalid,
14220 "expecting square transformation matrix", goto error);
14221 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14222 goto error;
14224 pos = isl_basic_map_offset(bmap, type) + first;
14226 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14227 isl_mat_copy(trans)) < 0)
14228 goto error;
14229 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14230 isl_mat_copy(trans)) < 0)
14231 goto error;
14232 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14233 isl_mat_copy(trans)) < 0)
14234 goto error;
14236 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14237 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14239 isl_mat_free(trans);
14240 return bmap;
14241 error:
14242 isl_mat_free(trans);
14243 isl_basic_map_free(bmap);
14244 return NULL;
14247 /* Replace the variables x of type "type" starting at "first" in "bset"
14248 * by x' with x = M x' with M the matrix trans.
14249 * That is, replace the corresponding coefficients c by c M.
14251 * The transformation matrix should be a square matrix.
14253 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14254 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14255 __isl_take isl_mat *trans)
14257 return isl_basic_map_transform_dims(bset, type, first, trans);