isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_map.c
blob5cce4841e64897d28bf0fc0a0e8303f7dffeb642
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
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
47 #include <isl_printer_private.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep const isl_basic_map *const_bset_to_bmap(
59 __isl_keep const isl_basic_set *bset)
61 return (const isl_basic_map *) bset;
64 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
75 enum isl_dim_type type)
77 if (!bmap)
78 return isl_size_error;
79 switch (type) {
80 case isl_dim_cst: return 1;
81 case isl_dim_param:
82 case isl_dim_in:
83 case isl_dim_out: return isl_space_dim(bmap->dim, type);
84 case isl_dim_div: return bmap->n_div;
85 case isl_dim_all: return isl_basic_map_total_dim(bmap);
86 default: return 0;
90 /* Return the space of "map".
92 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94 return map ? map->dim : NULL;
97 /* Return the space of "set".
99 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
101 return isl_map_peek_space(set_to_map(set));
104 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
106 return isl_space_dim(isl_map_peek_space(map), type);
109 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
111 return isl_map_dim(set_to_map(set), type);
114 /* Return the position of the variables of the given type
115 * within the sequence of variables of "bmap".
117 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
118 enum isl_dim_type type)
120 isl_space *space;
122 space = isl_basic_map_peek_space(bmap);
123 if (!space)
124 return isl_size_error;
126 switch (type) {
127 case isl_dim_param:
128 case isl_dim_in:
129 case isl_dim_out: return isl_space_offset(space, type);
130 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
131 case isl_dim_cst:
132 default:
133 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
134 "invalid dimension type", return isl_size_error);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
142 enum isl_dim_type type)
144 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
147 /* Return the position of the coefficients of the variables of the given type
148 * within the sequence of coefficients of "bmap".
150 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
151 enum isl_dim_type type)
153 switch (type) {
154 case isl_dim_cst: return 0;
155 case isl_dim_param:
156 case isl_dim_in:
157 case isl_dim_out:
158 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
159 default: return 0;
163 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_offset(bset, type);
169 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
171 return pos(map->dim, type);
174 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
175 enum isl_dim_type type)
177 return isl_basic_map_dim(bset, type);
180 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
182 return isl_basic_set_dim(bset, isl_dim_set);
185 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
187 return isl_basic_set_dim(bset, isl_dim_param);
190 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
192 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
195 isl_size isl_set_n_dim(__isl_keep isl_set *set)
197 return isl_set_dim(set, isl_dim_set);
200 isl_size isl_set_n_param(__isl_keep isl_set *set)
202 return isl_set_dim(set, isl_dim_param);
205 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
207 isl_size dim;
209 if (!bmap)
210 return isl_size_error;
211 dim = isl_space_dim(bmap->dim, isl_dim_all);
212 if (dim < 0)
213 return isl_size_error;
214 return dim + bmap->n_div;
217 /* Return the number of equality constraints in the description of "bmap".
218 * Return -1 on error.
220 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
222 if (!bmap)
223 return -1;
224 return bmap->n_eq;
227 /* Return the number of equality constraints in the description of "bset".
228 * Return -1 on error.
230 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
232 return isl_basic_map_n_equality(bset_to_bmap(bset));
235 /* Return the number of inequality constraints in the description of "bmap".
236 * Return -1 on error.
238 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
240 if (!bmap)
241 return -1;
242 return bmap->n_ineq;
245 /* Return the number of inequality constraints in the description of "bset".
246 * Return -1 on error.
248 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
250 return isl_basic_map_n_inequality(bset_to_bmap(bset));
253 /* Do "bmap1" and "bmap2" have the same parameters?
255 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
256 __isl_keep isl_basic_map *bmap2)
258 isl_space *space1, *space2;
260 space1 = isl_basic_map_peek_space(bmap1);
261 space2 = isl_basic_map_peek_space(bmap2);
262 return isl_space_has_equal_params(space1, space2);
265 /* Do "map1" and "map2" have the same parameters?
267 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
268 __isl_keep isl_map *map2)
270 isl_space *space1, *space2;
272 space1 = isl_map_peek_space(map1);
273 space2 = isl_map_peek_space(map2);
274 return isl_space_has_equal_params(space1, space2);
277 /* Do "map" and "set" have the same parameters?
279 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
280 __isl_keep isl_set *set)
282 return isl_map_has_equal_params(map, set_to_map(set));
285 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
286 __isl_keep isl_set *set)
288 isl_bool m;
289 if (!map || !set)
290 return isl_bool_error;
291 m = isl_map_has_equal_params(map, set_to_map(set));
292 if (m < 0 || !m)
293 return m;
294 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
295 set->dim, isl_dim_set);
298 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
299 __isl_keep isl_basic_set *bset)
301 isl_bool m;
302 if (!bmap || !bset)
303 return isl_bool_error;
304 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
305 if (m < 0 || !m)
306 return m;
307 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
308 bset->dim, isl_dim_set);
311 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
312 __isl_keep isl_set *set)
314 isl_bool m;
315 if (!map || !set)
316 return isl_bool_error;
317 m = isl_map_has_equal_params(map, set_to_map(set));
318 if (m < 0 || !m)
319 return m;
320 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
321 set->dim, isl_dim_set);
324 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
325 __isl_keep isl_basic_set *bset)
327 isl_bool m;
328 if (!bmap || !bset)
329 return isl_bool_error;
330 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
331 if (m < 0 || !m)
332 return m;
333 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
334 bset->dim, isl_dim_set);
337 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
339 return bmap ? bmap->ctx : NULL;
342 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
344 return bset ? bset->ctx : NULL;
347 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
349 return map ? map->ctx : NULL;
352 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
354 return set ? set->ctx : NULL;
357 /* Return the space of "bmap".
359 __isl_keep isl_space *isl_basic_map_peek_space(
360 __isl_keep const isl_basic_map *bmap)
362 return bmap ? bmap->dim : NULL;
365 /* Return the space of "bset".
367 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
369 return isl_basic_map_peek_space(bset_to_bmap(bset));
372 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
374 return isl_space_copy(isl_basic_map_peek_space(bmap));
377 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
379 return isl_basic_map_get_space(bset_to_bmap(bset));
382 /* Extract the divs in "bmap" as a matrix.
384 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
386 int i;
387 isl_ctx *ctx;
388 isl_mat *div;
389 isl_size v_div;
390 unsigned cols;
392 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
393 if (v_div < 0)
394 return NULL;
396 ctx = isl_basic_map_get_ctx(bmap);
397 cols = 1 + 1 + v_div + bmap->n_div;
398 div = isl_mat_alloc(ctx, bmap->n_div, cols);
399 if (!div)
400 return NULL;
402 for (i = 0; i < bmap->n_div; ++i)
403 isl_seq_cpy(div->row[i], bmap->div[i], cols);
405 return div;
408 /* Extract the divs in "bset" as a matrix.
410 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
412 return isl_basic_map_get_divs(bset);
415 __isl_give isl_local_space *isl_basic_map_get_local_space(
416 __isl_keep isl_basic_map *bmap)
418 isl_mat *div;
420 if (!bmap)
421 return NULL;
423 div = isl_basic_map_get_divs(bmap);
424 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
427 __isl_give isl_local_space *isl_basic_set_get_local_space(
428 __isl_keep isl_basic_set *bset)
430 return isl_basic_map_get_local_space(bset);
433 /* For each known div d = floor(f/m), add the constraints
435 * f - m d >= 0
436 * -(f-(m-1)) + m d >= 0
438 * Do not finalize the result.
440 static __isl_give isl_basic_map *add_known_div_constraints(
441 __isl_take isl_basic_map *bmap)
443 int i;
444 isl_size n_div;
446 n_div = isl_basic_map_dim(bmap, isl_dim_div);
447 if (n_div < 0)
448 return isl_basic_map_free(bmap);
449 if (n_div == 0)
450 return bmap;
451 bmap = isl_basic_map_cow(bmap);
452 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
453 if (!bmap)
454 return NULL;
455 for (i = 0; i < n_div; ++i) {
456 if (isl_int_is_zero(bmap->div[i][0]))
457 continue;
458 bmap = isl_basic_map_add_div_constraints(bmap, i);
461 return bmap;
464 __isl_give isl_basic_map *isl_basic_map_from_local_space(
465 __isl_take isl_local_space *ls)
467 int i;
468 isl_size n_div;
469 isl_basic_map *bmap;
471 n_div = isl_local_space_dim(ls, isl_dim_div);
472 if (n_div < 0)
473 ls = isl_local_space_free(ls);
474 if (!ls)
475 return NULL;
477 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
478 n_div, 0, 2 * n_div);
480 for (i = 0; i < n_div; ++i)
481 if (isl_basic_map_alloc_div(bmap) < 0)
482 goto error;
484 for (i = 0; i < n_div; ++i)
485 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
486 bmap = add_known_div_constraints(bmap);
488 isl_local_space_free(ls);
489 return bmap;
490 error:
491 isl_local_space_free(ls);
492 isl_basic_map_free(bmap);
493 return NULL;
496 __isl_give isl_basic_set *isl_basic_set_from_local_space(
497 __isl_take isl_local_space *ls)
499 return isl_basic_map_from_local_space(ls);
502 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
504 return isl_space_copy(isl_map_peek_space(map));
507 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
509 if (!set)
510 return NULL;
511 return isl_space_copy(set->dim);
514 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
515 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
517 bmap = isl_basic_map_cow(bmap);
518 if (!bmap)
519 return NULL;
520 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
521 if (!bmap->dim)
522 goto error;
523 bmap = isl_basic_map_finalize(bmap);
524 return bmap;
525 error:
526 isl_basic_map_free(bmap);
527 return NULL;
530 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
531 __isl_take isl_basic_set *bset, const char *s)
533 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
536 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
537 enum isl_dim_type type)
539 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
542 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
543 enum isl_dim_type type, const char *s)
545 int i;
547 map = isl_map_cow(map);
548 if (!map)
549 return NULL;
551 map->dim = isl_space_set_tuple_name(map->dim, type, s);
552 if (!map->dim)
553 goto error;
555 for (i = 0; i < map->n; ++i) {
556 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
557 if (!map->p[i])
558 goto error;
561 return map;
562 error:
563 isl_map_free(map);
564 return NULL;
567 /* Replace the identifier of the tuple of type "type" by "id".
569 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
570 __isl_take isl_basic_map *bmap,
571 enum isl_dim_type type, __isl_take isl_id *id)
573 bmap = isl_basic_map_cow(bmap);
574 if (!bmap)
575 goto error;
576 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
577 if (!bmap->dim)
578 return isl_basic_map_free(bmap);
579 bmap = isl_basic_map_finalize(bmap);
580 return bmap;
581 error:
582 isl_id_free(id);
583 return NULL;
586 /* Replace the identifier of the tuple by "id".
588 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
589 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
591 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
594 /* Does the input or output tuple have a name?
596 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
598 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
601 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
602 enum isl_dim_type type)
604 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
607 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
608 const char *s)
610 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
611 isl_dim_set, s));
614 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
615 enum isl_dim_type type, __isl_take isl_id *id)
617 map = isl_map_cow(map);
618 if (!map)
619 goto error;
621 map->dim = isl_space_set_tuple_id(map->dim, type, id);
623 return isl_map_reset_space(map, isl_space_copy(map->dim));
624 error:
625 isl_id_free(id);
626 return NULL;
629 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
630 __isl_take isl_id *id)
632 return isl_map_set_tuple_id(set, isl_dim_set, id);
635 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
636 enum isl_dim_type type)
638 map = isl_map_cow(map);
639 if (!map)
640 return NULL;
642 map->dim = isl_space_reset_tuple_id(map->dim, type);
644 return isl_map_reset_space(map, isl_space_copy(map->dim));
647 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
649 return isl_map_reset_tuple_id(set, isl_dim_set);
652 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
654 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
657 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
658 enum isl_dim_type type)
660 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
663 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
665 return isl_map_has_tuple_id(set, isl_dim_set);
668 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
670 return isl_map_get_tuple_id(set, isl_dim_set);
673 /* Does the set tuple have a name?
675 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
677 if (!set)
678 return isl_bool_error;
679 return isl_space_has_tuple_name(set->dim, isl_dim_set);
683 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
685 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
688 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
690 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
693 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
694 enum isl_dim_type type, unsigned pos)
696 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
699 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
700 enum isl_dim_type type, unsigned pos)
702 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
705 /* Does the given dimension have a name?
707 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
708 enum isl_dim_type type, unsigned pos)
710 if (!map)
711 return isl_bool_error;
712 return isl_space_has_dim_name(map->dim, type, pos);
715 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
716 enum isl_dim_type type, unsigned pos)
718 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
721 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
722 enum isl_dim_type type, unsigned pos)
724 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
727 /* Does the given dimension have a name?
729 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
730 enum isl_dim_type type, unsigned pos)
732 if (!set)
733 return isl_bool_error;
734 return isl_space_has_dim_name(set->dim, type, pos);
737 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
738 __isl_take isl_basic_map *bmap,
739 enum isl_dim_type type, unsigned pos, const char *s)
741 bmap = isl_basic_map_cow(bmap);
742 if (!bmap)
743 return NULL;
744 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
745 if (!bmap->dim)
746 goto error;
747 return isl_basic_map_finalize(bmap);
748 error:
749 isl_basic_map_free(bmap);
750 return NULL;
753 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
754 enum isl_dim_type type, unsigned pos, const char *s)
756 int i;
758 map = isl_map_cow(map);
759 if (!map)
760 return NULL;
762 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
763 if (!map->dim)
764 goto error;
766 for (i = 0; i < map->n; ++i) {
767 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
768 if (!map->p[i])
769 goto error;
772 return map;
773 error:
774 isl_map_free(map);
775 return NULL;
778 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
779 __isl_take isl_basic_set *bset,
780 enum isl_dim_type type, unsigned pos, const char *s)
782 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
783 type, pos, s));
786 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
787 enum isl_dim_type type, unsigned pos, const char *s)
789 return set_from_map(isl_map_set_dim_name(set_to_map(set),
790 type, pos, s));
793 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
794 enum isl_dim_type type, unsigned pos)
796 if (!bmap)
797 return isl_bool_error;
798 return isl_space_has_dim_id(bmap->dim, type, pos);
801 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
802 enum isl_dim_type type, unsigned pos)
804 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
807 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
808 enum isl_dim_type type, unsigned pos)
810 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
813 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
814 enum isl_dim_type type, unsigned pos)
816 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
819 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
820 enum isl_dim_type type, unsigned pos)
822 return isl_map_has_dim_id(set, type, pos);
825 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
826 enum isl_dim_type type, unsigned pos)
828 return isl_map_get_dim_id(set, type, pos);
831 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
832 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
834 map = isl_map_cow(map);
835 if (!map)
836 goto error;
838 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
840 return isl_map_reset_space(map, isl_space_copy(map->dim));
841 error:
842 isl_id_free(id);
843 return NULL;
846 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
847 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
849 return isl_map_set_dim_id(set, type, pos, id);
852 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
853 __isl_keep isl_id *id)
855 if (!map)
856 return -1;
857 return isl_space_find_dim_by_id(map->dim, type, id);
860 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
861 __isl_keep isl_id *id)
863 return isl_map_find_dim_by_id(set, type, id);
866 /* Return the position of the dimension of the given type and name
867 * in "bmap".
868 * Return -1 if no such dimension can be found.
870 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
871 enum isl_dim_type type, const char *name)
873 if (!bmap)
874 return -1;
875 return isl_space_find_dim_by_name(bmap->dim, type, name);
878 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
879 const char *name)
881 if (!map)
882 return -1;
883 return isl_space_find_dim_by_name(map->dim, type, name);
886 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
887 const char *name)
889 return isl_map_find_dim_by_name(set, type, name);
892 /* Check whether equality i of bset is a pure stride constraint
893 * on a single dimension, i.e., of the form
895 * v = k e
897 * with k a constant and e an existentially quantified variable.
899 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
901 isl_size nparam;
902 isl_size d;
903 isl_size n_div;
904 int pos1;
905 int pos2;
907 nparam = isl_basic_set_dim(bset, isl_dim_param);
908 d = isl_basic_set_dim(bset, isl_dim_set);
909 n_div = isl_basic_set_dim(bset, isl_dim_div);
910 if (nparam < 0 || d < 0 || n_div < 0)
911 return isl_bool_error;
913 if (!isl_int_is_zero(bset->eq[i][0]))
914 return isl_bool_false;
916 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
917 return isl_bool_false;
918 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
919 if (pos1 == -1)
920 return isl_bool_false;
921 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
922 d - pos1 - 1) != -1)
923 return isl_bool_false;
925 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
926 if (pos2 == -1)
927 return isl_bool_false;
928 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
929 n_div - pos2 - 1) != -1)
930 return isl_bool_false;
931 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
932 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
933 return isl_bool_false;
935 return isl_bool_true;
938 /* Reset the user pointer on all identifiers of parameters and tuples
939 * of the space of "map".
941 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
943 isl_space *space;
945 space = isl_map_get_space(map);
946 space = isl_space_reset_user(space);
947 map = isl_map_reset_space(map, space);
949 return map;
952 /* Reset the user pointer on all identifiers of parameters and tuples
953 * of the space of "set".
955 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
957 return isl_map_reset_user(set);
960 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
962 if (!bmap)
963 return isl_bool_error;
964 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
967 /* Has "map" been marked as a rational map?
968 * In particular, have all basic maps in "map" been marked this way?
969 * An empty map is not considered to be rational.
970 * Maps where only some of the basic maps are marked rational
971 * are not allowed.
973 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
975 int i;
976 isl_bool rational;
978 if (!map)
979 return isl_bool_error;
980 if (map->n == 0)
981 return isl_bool_false;
982 rational = isl_basic_map_is_rational(map->p[0]);
983 if (rational < 0)
984 return rational;
985 for (i = 1; i < map->n; ++i) {
986 isl_bool rational_i;
988 rational_i = isl_basic_map_is_rational(map->p[i]);
989 if (rational_i < 0)
990 return rational_i;
991 if (rational != rational_i)
992 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
993 "mixed rational and integer basic maps "
994 "not supported", return isl_bool_error);
997 return rational;
1000 /* Has "set" been marked as a rational set?
1001 * In particular, have all basic set in "set" been marked this way?
1002 * An empty set is not considered to be rational.
1003 * Sets where only some of the basic sets are marked rational
1004 * are not allowed.
1006 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1008 return isl_map_is_rational(set);
1011 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1013 return isl_basic_map_is_rational(bset);
1016 /* Does "bmap" contain any rational points?
1018 * If "bmap" has an equality for each dimension, equating the dimension
1019 * to an integer constant, then it has no rational points, even if it
1020 * is marked as rational.
1022 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1024 isl_bool has_rational = isl_bool_true;
1025 isl_size total;
1027 if (!bmap)
1028 return isl_bool_error;
1029 if (isl_basic_map_plain_is_empty(bmap))
1030 return isl_bool_false;
1031 if (!isl_basic_map_is_rational(bmap))
1032 return isl_bool_false;
1033 bmap = isl_basic_map_copy(bmap);
1034 bmap = isl_basic_map_implicit_equalities(bmap);
1035 total = isl_basic_map_dim(bmap, isl_dim_all);
1036 if (total < 0)
1037 return isl_bool_error;
1038 if (bmap->n_eq == total) {
1039 int i, j;
1040 for (i = 0; i < bmap->n_eq; ++i) {
1041 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1042 if (j < 0)
1043 break;
1044 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1045 !isl_int_is_negone(bmap->eq[i][1 + j]))
1046 break;
1047 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1048 total - j - 1);
1049 if (j >= 0)
1050 break;
1052 if (i == bmap->n_eq)
1053 has_rational = isl_bool_false;
1055 isl_basic_map_free(bmap);
1057 return has_rational;
1060 /* Does "map" contain any rational points?
1062 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1064 int i;
1065 isl_bool has_rational;
1067 if (!map)
1068 return isl_bool_error;
1069 for (i = 0; i < map->n; ++i) {
1070 has_rational = isl_basic_map_has_rational(map->p[i]);
1071 if (has_rational < 0 || has_rational)
1072 return has_rational;
1074 return isl_bool_false;
1077 /* Does "set" contain any rational points?
1079 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1081 return isl_map_has_rational(set);
1084 /* Is this basic set a parameter domain?
1086 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1088 if (!bset)
1089 return isl_bool_error;
1090 return isl_space_is_params(bset->dim);
1093 /* Is this set a parameter domain?
1095 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1097 if (!set)
1098 return isl_bool_error;
1099 return isl_space_is_params(set->dim);
1102 /* Is this map actually a parameter domain?
1103 * Users should never call this function. Outside of isl,
1104 * a map can never be a parameter domain.
1106 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1108 if (!map)
1109 return isl_bool_error;
1110 return isl_space_is_params(map->dim);
1113 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1114 __isl_take isl_basic_map *bmap, unsigned extra,
1115 unsigned n_eq, unsigned n_ineq)
1117 int i;
1118 isl_space *space = isl_basic_map_peek_space(bmap);
1119 isl_size n_var = isl_space_dim(space, isl_dim_all);
1120 size_t row_size = 1 + n_var + extra;
1122 bmap->ctx = ctx;
1123 isl_ctx_ref(ctx);
1125 if (n_var < 0)
1126 return isl_basic_map_free(bmap);
1128 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1129 if (isl_blk_is_error(bmap->block))
1130 goto error;
1132 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1133 if ((n_ineq + n_eq) && !bmap->ineq)
1134 goto error;
1136 if (extra == 0) {
1137 bmap->block2 = isl_blk_empty();
1138 bmap->div = NULL;
1139 } else {
1140 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1141 if (isl_blk_is_error(bmap->block2))
1142 goto error;
1144 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1145 if (!bmap->div)
1146 goto error;
1149 for (i = 0; i < n_ineq + n_eq; ++i)
1150 bmap->ineq[i] = bmap->block.data + i * row_size;
1152 for (i = 0; i < extra; ++i)
1153 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1155 bmap->ref = 1;
1156 bmap->flags = 0;
1157 bmap->c_size = n_eq + n_ineq;
1158 bmap->eq = bmap->ineq + n_ineq;
1159 bmap->extra = extra;
1160 bmap->n_eq = 0;
1161 bmap->n_ineq = 0;
1162 bmap->n_div = 0;
1163 bmap->sample = NULL;
1165 return bmap;
1166 error:
1167 isl_basic_map_free(bmap);
1168 return NULL;
1171 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1172 unsigned nparam, unsigned dim, unsigned extra,
1173 unsigned n_eq, unsigned n_ineq)
1175 struct isl_basic_map *bmap;
1176 isl_space *space;
1178 space = isl_space_set_alloc(ctx, nparam, dim);
1179 if (!space)
1180 return NULL;
1182 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1183 return bset_from_bmap(bmap);
1186 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1187 unsigned extra, unsigned n_eq, unsigned n_ineq)
1189 struct isl_basic_map *bmap;
1190 if (!dim)
1191 return NULL;
1192 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1193 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1194 return bset_from_bmap(bmap);
1195 error:
1196 isl_space_free(dim);
1197 return NULL;
1200 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1201 unsigned extra, unsigned n_eq, unsigned n_ineq)
1203 struct isl_basic_map *bmap;
1205 if (!space)
1206 return NULL;
1207 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1208 if (!bmap)
1209 goto error;
1210 bmap->dim = space;
1212 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1213 error:
1214 isl_space_free(space);
1215 return NULL;
1218 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1219 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1220 unsigned n_eq, unsigned n_ineq)
1222 struct isl_basic_map *bmap;
1223 isl_space *dim;
1225 dim = isl_space_alloc(ctx, nparam, in, out);
1226 if (!dim)
1227 return NULL;
1229 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1230 return bmap;
1233 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1234 __isl_keep isl_basic_map *src)
1236 int i;
1237 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1239 if (!dst || total < 0)
1240 return isl_basic_map_free(dst);
1242 for (i = 0; i < src->n_eq; ++i) {
1243 int j = isl_basic_map_alloc_equality(dst);
1244 if (j < 0)
1245 return isl_basic_map_free(dst);
1246 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1249 for (i = 0; i < src->n_ineq; ++i) {
1250 int j = isl_basic_map_alloc_inequality(dst);
1251 if (j < 0)
1252 return isl_basic_map_free(dst);
1253 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1256 for (i = 0; i < src->n_div; ++i) {
1257 int j = isl_basic_map_alloc_div(dst);
1258 if (j < 0)
1259 return isl_basic_map_free(dst);
1260 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1262 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1263 return dst;
1266 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1268 struct isl_basic_map *dup;
1270 if (!bmap)
1271 return NULL;
1272 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1273 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1274 dup = dup_constraints(dup, bmap);
1275 if (!dup)
1276 return NULL;
1277 dup->flags = bmap->flags;
1278 dup->sample = isl_vec_copy(bmap->sample);
1279 return dup;
1282 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1284 struct isl_basic_map *dup;
1286 dup = isl_basic_map_dup(bset_to_bmap(bset));
1287 return bset_from_bmap(dup);
1290 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1292 if (!bset)
1293 return NULL;
1295 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1296 bset->ref++;
1297 return bset;
1299 return isl_basic_set_dup(bset);
1302 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1304 if (!set)
1305 return NULL;
1307 set->ref++;
1308 return set;
1311 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1313 if (!bmap)
1314 return NULL;
1316 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1317 bmap->ref++;
1318 return bmap;
1320 bmap = isl_basic_map_dup(bmap);
1321 if (bmap)
1322 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1323 return bmap;
1326 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1328 if (!map)
1329 return NULL;
1331 map->ref++;
1332 return map;
1335 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (--bmap->ref > 0)
1341 return NULL;
1343 isl_ctx_deref(bmap->ctx);
1344 free(bmap->div);
1345 isl_blk_free(bmap->ctx, bmap->block2);
1346 free(bmap->ineq);
1347 isl_blk_free(bmap->ctx, bmap->block);
1348 isl_vec_free(bmap->sample);
1349 isl_space_free(bmap->dim);
1350 free(bmap);
1352 return NULL;
1355 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1357 return isl_basic_map_free(bset_to_bmap(bset));
1360 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1362 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1365 /* Check that "bset" does not involve any parameters.
1367 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1369 isl_size nparam;
1371 nparam = isl_basic_set_dim(bset, isl_dim_param);
1372 if (nparam < 0)
1373 return isl_stat_error;
1374 if (nparam != 0)
1375 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1376 "basic set should not have any parameters",
1377 return isl_stat_error);
1378 return isl_stat_ok;
1381 /* Check that "bset" does not involve any local variables.
1383 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1385 isl_size n_div;
1387 n_div = isl_basic_set_dim(bset, isl_dim_div);
1388 if (n_div < 0)
1389 return isl_stat_error;
1390 if (n_div != 0)
1391 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1392 "basic set should not have any local variables",
1393 return isl_stat_error);
1394 return isl_stat_ok;
1397 /* Check that "map" has only named parameters, reporting an error
1398 * if it does not.
1400 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1402 return isl_space_check_named_params(isl_map_peek_space(map));
1405 /* Check that "bmap" has only named parameters, reporting an error
1406 * if it does not.
1408 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1410 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1413 /* Check that "bmap1" and "bmap2" have the same parameters,
1414 * reporting an error if they do not.
1416 static isl_stat isl_basic_map_check_equal_params(
1417 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1419 isl_bool match;
1421 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1422 if (match < 0)
1423 return isl_stat_error;
1424 if (!match)
1425 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1426 "parameters don't match", return isl_stat_error);
1427 return isl_stat_ok;
1430 __isl_give isl_map *isl_map_align_params_map_map_and(
1431 __isl_take isl_map *map1, __isl_take isl_map *map2,
1432 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1433 __isl_take isl_map *map2))
1435 if (!map1 || !map2)
1436 goto error;
1437 if (isl_map_has_equal_params(map1, map2))
1438 return fn(map1, map2);
1439 if (isl_map_check_named_params(map1) < 0)
1440 goto error;
1441 if (isl_map_check_named_params(map2) < 0)
1442 goto error;
1443 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1444 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1445 return fn(map1, map2);
1446 error:
1447 isl_map_free(map1);
1448 isl_map_free(map2);
1449 return NULL;
1452 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1453 __isl_keep isl_map *map2,
1454 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1456 isl_bool r;
1458 if (!map1 || !map2)
1459 return isl_bool_error;
1460 if (isl_map_has_equal_params(map1, map2))
1461 return fn(map1, map2);
1462 if (isl_map_check_named_params(map1) < 0)
1463 return isl_bool_error;
1464 if (isl_map_check_named_params(map2) < 0)
1465 return isl_bool_error;
1466 map1 = isl_map_copy(map1);
1467 map2 = isl_map_copy(map2);
1468 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1469 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1470 r = fn(map1, map2);
1471 isl_map_free(map1);
1472 isl_map_free(map2);
1473 return r;
1476 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1478 isl_size total;
1479 struct isl_ctx *ctx;
1481 total = isl_basic_map_dim(bmap, isl_dim_all);
1482 if (total < 0)
1483 return -1;
1484 ctx = bmap->ctx;
1485 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1486 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1487 return -1);
1488 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1489 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1490 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1491 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1492 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1493 isl_int *t;
1494 int j = isl_basic_map_alloc_inequality(bmap);
1495 if (j < 0)
1496 return -1;
1497 t = bmap->ineq[j];
1498 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1499 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1500 bmap->eq[-1] = t;
1501 bmap->n_eq++;
1502 bmap->n_ineq--;
1503 bmap->eq--;
1504 return 0;
1506 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1507 bmap->extra - bmap->n_div);
1508 return bmap->n_eq++;
1511 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1513 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1516 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1518 if (!bmap)
1519 return -1;
1520 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1521 bmap->n_eq -= n;
1522 return 0;
1525 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1527 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1530 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1532 isl_int *t;
1533 if (!bmap)
1534 return -1;
1535 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1537 if (pos != bmap->n_eq - 1) {
1538 t = bmap->eq[pos];
1539 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1540 bmap->eq[bmap->n_eq - 1] = t;
1542 bmap->n_eq--;
1543 return 0;
1546 /* Turn inequality "pos" of "bmap" into an equality.
1548 * In particular, we move the inequality in front of the equalities
1549 * and move the last inequality in the position of the moved inequality.
1550 * Note that isl_tab_make_equalities_explicit depends on this particular
1551 * change in the ordering of the constraints.
1553 void isl_basic_map_inequality_to_equality(
1554 struct isl_basic_map *bmap, unsigned pos)
1556 isl_int *t;
1558 t = bmap->ineq[pos];
1559 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1560 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1561 bmap->eq[-1] = t;
1562 bmap->n_eq++;
1563 bmap->n_ineq--;
1564 bmap->eq--;
1565 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1566 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1567 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1568 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1571 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1573 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1576 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1578 isl_size total;
1579 struct isl_ctx *ctx;
1581 total = isl_basic_map_dim(bmap, isl_dim_all);
1582 if (total < 0)
1583 return -1;
1584 ctx = bmap->ctx;
1585 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1586 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1587 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1588 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1589 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1590 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1591 bmap->extra - bmap->n_div);
1592 return bmap->n_ineq++;
1595 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1597 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1600 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1602 if (!bmap)
1603 return -1;
1604 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1605 bmap->n_ineq -= n;
1606 return 0;
1609 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1611 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1614 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1616 isl_int *t;
1617 if (!bmap)
1618 return -1;
1619 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1621 if (pos != bmap->n_ineq - 1) {
1622 t = bmap->ineq[pos];
1623 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1624 bmap->ineq[bmap->n_ineq - 1] = t;
1625 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1627 bmap->n_ineq--;
1628 return 0;
1631 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1633 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1636 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1637 isl_int *eq)
1639 isl_bool empty;
1640 isl_size total;
1641 int k;
1643 empty = isl_basic_map_plain_is_empty(bmap);
1644 if (empty < 0)
1645 return isl_basic_map_free(bmap);
1646 if (empty)
1647 return bmap;
1649 bmap = isl_basic_map_cow(bmap);
1650 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1651 total = isl_basic_map_dim(bmap, isl_dim_all);
1652 if (total < 0)
1653 return isl_basic_map_free(bmap);
1654 k = isl_basic_map_alloc_equality(bmap);
1655 if (k < 0)
1656 goto error;
1657 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1658 return bmap;
1659 error:
1660 isl_basic_map_free(bmap);
1661 return NULL;
1664 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1665 isl_int *eq)
1667 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1670 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1671 isl_int *ineq)
1673 isl_size total;
1674 int k;
1676 bmap = isl_basic_map_cow(bmap);
1677 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1678 total = isl_basic_map_dim(bmap, isl_dim_all);
1679 if (total < 0)
1680 return isl_basic_map_free(bmap);
1681 k = isl_basic_map_alloc_inequality(bmap);
1682 if (k < 0)
1683 goto error;
1684 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1685 return bmap;
1686 error:
1687 isl_basic_map_free(bmap);
1688 return NULL;
1691 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1692 isl_int *ineq)
1694 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1697 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1699 isl_size total;
1701 total = isl_basic_map_dim(bmap, isl_dim_all);
1702 if (total < 0)
1703 return -1;
1704 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1705 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1706 bmap->extra - bmap->n_div);
1707 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1708 return bmap->n_div++;
1711 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1713 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1716 #undef TYPE
1717 #define TYPE isl_basic_map
1718 #include "check_type_range_templ.c"
1720 /* Check that there are "n" dimensions of type "type" starting at "first"
1721 * in "bset".
1723 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1724 enum isl_dim_type type, unsigned first, unsigned n)
1726 return isl_basic_map_check_range(bset_to_bmap(bset),
1727 type, first, n);
1730 /* Insert an extra integer division, prescribed by "div", to "bmap"
1731 * at (integer division) position "pos".
1733 * The integer division is first added at the end and then moved
1734 * into the right position.
1736 __isl_give isl_basic_map *isl_basic_map_insert_div(
1737 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1739 int i, k;
1740 isl_size total;
1742 bmap = isl_basic_map_cow(bmap);
1743 total = isl_basic_map_dim(bmap, isl_dim_all);
1744 if (total < 0 || !div)
1745 return isl_basic_map_free(bmap);
1747 if (div->size != 1 + 1 + total)
1748 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1749 "unexpected size", return isl_basic_map_free(bmap));
1750 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1751 return isl_basic_map_free(bmap);
1753 bmap = isl_basic_map_extend_space(bmap,
1754 isl_basic_map_get_space(bmap), 1, 0, 2);
1755 k = isl_basic_map_alloc_div(bmap);
1756 if (k < 0)
1757 return isl_basic_map_free(bmap);
1758 isl_seq_cpy(bmap->div[k], div->el, div->size);
1759 isl_int_set_si(bmap->div[k][div->size], 0);
1761 for (i = k; i > pos; --i)
1762 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1764 return bmap;
1767 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1769 if (!bmap)
1770 return isl_stat_error;
1771 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1772 bmap->n_div -= n;
1773 return isl_stat_ok;
1776 static __isl_give isl_basic_map *add_constraints(
1777 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1778 unsigned i_pos, unsigned o_pos)
1780 isl_size total, n_param, n_in, n_out, n_div;
1781 unsigned o_in, o_out;
1782 isl_ctx *ctx;
1783 isl_space *space;
1784 struct isl_dim_map *dim_map;
1786 space = isl_basic_map_peek_space(bmap2);
1787 if (!bmap1 || !space)
1788 goto error;
1790 total = isl_basic_map_dim(bmap1, isl_dim_all);
1791 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1792 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1793 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1794 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1795 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1796 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1797 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1798 goto error;
1799 ctx = isl_basic_map_get_ctx(bmap1);
1800 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1801 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1802 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1804 isl_dim_map_div(dim_map, bmap2, total);
1806 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1807 error:
1808 isl_basic_map_free(bmap1);
1809 isl_basic_map_free(bmap2);
1810 return NULL;
1813 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1814 struct isl_basic_set *bset2, unsigned pos)
1816 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1817 bset_to_bmap(bset2), 0, pos));
1820 __isl_give isl_basic_map *isl_basic_map_extend_space(
1821 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1822 unsigned extra, unsigned n_eq, unsigned n_ineq)
1824 struct isl_basic_map *ext;
1825 unsigned flags;
1826 int dims_ok;
1828 if (!space)
1829 goto error;
1831 if (!base)
1832 goto error;
1834 dims_ok = isl_space_is_equal(base->dim, space) &&
1835 base->extra >= base->n_div + extra;
1837 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1838 room_for_ineq(base, n_ineq)) {
1839 isl_space_free(space);
1840 return base;
1843 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1844 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1845 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1846 extra += base->extra;
1847 n_eq += base->n_eq;
1848 n_ineq += base->n_ineq;
1850 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1851 space = NULL;
1852 if (!ext)
1853 goto error;
1855 if (dims_ok)
1856 ext->sample = isl_vec_copy(base->sample);
1857 flags = base->flags;
1858 ext = add_constraints(ext, base, 0, 0);
1859 if (ext) {
1860 ext->flags = flags;
1861 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1864 return ext;
1866 error:
1867 isl_space_free(space);
1868 isl_basic_map_free(base);
1869 return NULL;
1872 __isl_give isl_basic_set *isl_basic_set_extend_space(
1873 __isl_take isl_basic_set *base,
1874 __isl_take isl_space *dim, unsigned extra,
1875 unsigned n_eq, unsigned n_ineq)
1877 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1878 dim, extra, n_eq, n_ineq));
1881 struct isl_basic_map *isl_basic_map_extend_constraints(
1882 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1884 if (!base)
1885 return NULL;
1886 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1887 0, n_eq, n_ineq);
1890 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1891 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1892 unsigned n_eq, unsigned n_ineq)
1894 struct isl_basic_map *bmap;
1895 isl_space *dim;
1897 if (!base)
1898 return NULL;
1899 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1900 if (!dim)
1901 goto error;
1903 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1904 return bmap;
1905 error:
1906 isl_basic_map_free(base);
1907 return NULL;
1910 struct isl_basic_set *isl_basic_set_extend_constraints(
1911 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1913 isl_basic_map *bmap = bset_to_bmap(base);
1914 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1915 return bset_from_bmap(bmap);
1918 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1920 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1923 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1925 if (!bmap)
1926 return NULL;
1928 if (bmap->ref > 1) {
1929 bmap->ref--;
1930 bmap = isl_basic_map_dup(bmap);
1932 if (bmap) {
1933 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1934 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1936 return bmap;
1939 /* Clear all cached information in "map", either because it is about
1940 * to be modified or because it is being freed.
1941 * Always return the same pointer that is passed in.
1942 * This is needed for the use in isl_map_free.
1944 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1946 isl_basic_map_free(map->cached_simple_hull[0]);
1947 isl_basic_map_free(map->cached_simple_hull[1]);
1948 map->cached_simple_hull[0] = NULL;
1949 map->cached_simple_hull[1] = NULL;
1950 return map;
1953 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1955 return isl_map_cow(set);
1958 /* Return an isl_map that is equal to "map" and that has only
1959 * a single reference.
1961 * If the original input already has only one reference, then
1962 * simply return it, but clear all cached information, since
1963 * it may be rendered invalid by the operations that will be
1964 * performed on the result.
1966 * Otherwise, create a duplicate (without any cached information).
1968 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1970 if (!map)
1971 return NULL;
1973 if (map->ref == 1)
1974 return clear_caches(map);
1975 map->ref--;
1976 return isl_map_dup(map);
1979 static void swap_vars(struct isl_blk blk, isl_int *a,
1980 unsigned a_len, unsigned b_len)
1982 isl_seq_cpy(blk.data, a+a_len, b_len);
1983 isl_seq_cpy(blk.data+b_len, a, a_len);
1984 isl_seq_cpy(a, blk.data, b_len+a_len);
1987 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1988 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1990 int i;
1991 struct isl_blk blk;
1993 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1994 goto error;
1996 if (n1 == 0 || n2 == 0)
1997 return bmap;
1999 bmap = isl_basic_map_cow(bmap);
2000 if (!bmap)
2001 return NULL;
2003 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2004 if (isl_blk_is_error(blk))
2005 goto error;
2007 for (i = 0; i < bmap->n_eq; ++i)
2008 swap_vars(blk,
2009 bmap->eq[i] + pos, n1, n2);
2011 for (i = 0; i < bmap->n_ineq; ++i)
2012 swap_vars(blk,
2013 bmap->ineq[i] + pos, n1, n2);
2015 for (i = 0; i < bmap->n_div; ++i)
2016 swap_vars(blk,
2017 bmap->div[i]+1 + pos, n1, n2);
2019 isl_blk_free(bmap->ctx, blk);
2021 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2022 bmap = isl_basic_map_gauss(bmap, NULL);
2023 return isl_basic_map_finalize(bmap);
2024 error:
2025 isl_basic_map_free(bmap);
2026 return NULL;
2029 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2030 __isl_take isl_basic_map *bmap)
2032 int i = 0;
2033 isl_size total;
2035 total = isl_basic_map_dim(bmap, isl_dim_all);
2036 if (total < 0)
2037 return isl_basic_map_free(bmap);
2038 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2039 return isl_basic_map_free(bmap);
2040 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2041 if (bmap->n_eq > 0)
2042 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2043 else {
2044 i = isl_basic_map_alloc_equality(bmap);
2045 if (i < 0)
2046 goto error;
2048 isl_int_set_si(bmap->eq[i][0], 1);
2049 isl_seq_clr(bmap->eq[i]+1, total);
2050 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2051 isl_vec_free(bmap->sample);
2052 bmap->sample = NULL;
2053 return isl_basic_map_finalize(bmap);
2054 error:
2055 isl_basic_map_free(bmap);
2056 return NULL;
2059 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2060 __isl_take isl_basic_set *bset)
2062 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2065 __isl_give isl_basic_map *isl_basic_map_set_rational(
2066 __isl_take isl_basic_map *bmap)
2068 if (!bmap)
2069 return NULL;
2071 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2072 return bmap;
2074 bmap = isl_basic_map_cow(bmap);
2075 if (!bmap)
2076 return NULL;
2078 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2080 return isl_basic_map_finalize(bmap);
2083 __isl_give isl_basic_set *isl_basic_set_set_rational(
2084 __isl_take isl_basic_set *bset)
2086 return isl_basic_map_set_rational(bset);
2089 __isl_give isl_basic_set *isl_basic_set_set_integral(
2090 __isl_take isl_basic_set *bset)
2092 if (!bset)
2093 return NULL;
2095 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2096 return bset;
2098 bset = isl_basic_set_cow(bset);
2099 if (!bset)
2100 return NULL;
2102 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2104 return isl_basic_set_finalize(bset);
2107 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2109 int i;
2111 map = isl_map_cow(map);
2112 if (!map)
2113 return NULL;
2114 for (i = 0; i < map->n; ++i) {
2115 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2116 if (!map->p[i])
2117 goto error;
2119 return map;
2120 error:
2121 isl_map_free(map);
2122 return NULL;
2125 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2127 return isl_map_set_rational(set);
2130 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2131 * of "bmap").
2133 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2135 isl_int *t = bmap->div[a];
2136 bmap->div[a] = bmap->div[b];
2137 bmap->div[b] = t;
2140 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2141 * div definitions accordingly.
2143 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2144 int a, int b)
2146 int i;
2147 isl_size off;
2149 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2150 if (off < 0)
2151 return isl_basic_map_free(bmap);
2153 swap_div(bmap, a, b);
2155 for (i = 0; i < bmap->n_eq; ++i)
2156 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2158 for (i = 0; i < bmap->n_ineq; ++i)
2159 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2161 for (i = 0; i < bmap->n_div; ++i)
2162 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2163 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2165 return bmap;
2168 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2170 isl_seq_cpy(c, c + n, rem);
2171 isl_seq_clr(c + rem, n);
2174 /* Drop n dimensions starting at first.
2176 * In principle, this frees up some extra variables as the number
2177 * of columns remains constant, but we would have to extend
2178 * the div array too as the number of rows in this array is assumed
2179 * to be equal to extra.
2181 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2182 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2184 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2187 /* Move "n" divs starting at "first" to the end of the list of divs.
2189 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2190 unsigned first, unsigned n)
2192 isl_int **div;
2193 int i;
2195 if (first + n == bmap->n_div)
2196 return bmap;
2198 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2199 if (!div)
2200 goto error;
2201 for (i = 0; i < n; ++i)
2202 div[i] = bmap->div[first + i];
2203 for (i = 0; i < bmap->n_div - first - n; ++i)
2204 bmap->div[first + i] = bmap->div[first + n + i];
2205 for (i = 0; i < n; ++i)
2206 bmap->div[bmap->n_div - n + i] = div[i];
2207 free(div);
2208 return bmap;
2209 error:
2210 isl_basic_map_free(bmap);
2211 return NULL;
2214 #undef TYPE
2215 #define TYPE isl_map
2216 static
2217 #include "check_type_range_templ.c"
2219 /* Check that there are "n" dimensions of type "type" starting at "first"
2220 * in "set".
2222 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 return isl_map_check_range(set_to_map(set), type, first, n);
2228 /* Drop "n" dimensions of type "type" starting at "first".
2229 * Perform the core computation, without cowing or
2230 * simplifying and finalizing the result.
2232 * In principle, this frees up some extra variables as the number
2233 * of columns remains constant, but we would have to extend
2234 * the div array too as the number of rows in this array is assumed
2235 * to be equal to extra.
2237 __isl_give isl_basic_map *isl_basic_map_drop_core(
2238 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2239 unsigned first, unsigned n)
2241 int i;
2242 unsigned offset;
2243 unsigned left;
2244 isl_size total;
2246 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2247 return isl_basic_map_free(bmap);
2249 total = isl_basic_map_dim(bmap, isl_dim_all);
2250 if (total < 0)
2251 return isl_basic_map_free(bmap);
2253 offset = isl_basic_map_offset(bmap, type) + first;
2254 left = total - (offset - 1) - n;
2255 for (i = 0; i < bmap->n_eq; ++i)
2256 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2258 for (i = 0; i < bmap->n_ineq; ++i)
2259 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2261 for (i = 0; i < bmap->n_div; ++i)
2262 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2264 if (type == isl_dim_div) {
2265 bmap = move_divs_last(bmap, first, n);
2266 if (!bmap)
2267 return NULL;
2268 if (isl_basic_map_free_div(bmap, n) < 0)
2269 return isl_basic_map_free(bmap);
2270 } else
2271 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2272 if (!bmap->dim)
2273 return isl_basic_map_free(bmap);
2275 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2276 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2277 return bmap;
2280 /* Drop "n" dimensions of type "type" starting at "first".
2282 * In principle, this frees up some extra variables as the number
2283 * of columns remains constant, but we would have to extend
2284 * the div array too as the number of rows in this array is assumed
2285 * to be equal to extra.
2287 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2288 enum isl_dim_type type, unsigned first, unsigned n)
2290 if (!bmap)
2291 return NULL;
2292 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2293 return bmap;
2295 bmap = isl_basic_map_cow(bmap);
2296 if (!bmap)
2297 return NULL;
2299 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2301 bmap = isl_basic_map_simplify(bmap);
2302 return isl_basic_map_finalize(bmap);
2305 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2306 enum isl_dim_type type, unsigned first, unsigned n)
2308 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2309 type, first, n));
2312 /* No longer consider "map" to be normalized.
2314 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2316 if (!map)
2317 return NULL;
2318 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2319 return map;
2322 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2323 enum isl_dim_type type, unsigned first, unsigned n)
2325 int i;
2327 if (isl_map_check_range(map, type, first, n) < 0)
2328 return isl_map_free(map);
2330 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2331 return map;
2332 map = isl_map_cow(map);
2333 if (!map)
2334 goto error;
2335 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2336 if (!map->dim)
2337 goto error;
2339 for (i = 0; i < map->n; ++i) {
2340 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2341 if (!map->p[i])
2342 goto error;
2344 map = isl_map_unmark_normalized(map);
2346 return map;
2347 error:
2348 isl_map_free(map);
2349 return NULL;
2352 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2358 /* Drop the integer division at position "div", which is assumed
2359 * not to appear in any of the constraints or
2360 * in any of the other integer divisions.
2362 * Since the integer division is redundant, there is no need to cow.
2364 __isl_give isl_basic_map *isl_basic_map_drop_div(
2365 __isl_take isl_basic_map *bmap, unsigned div)
2367 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2370 /* Eliminate the specified n dimensions starting at first from the
2371 * constraints, without removing the dimensions from the space.
2372 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2374 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2375 enum isl_dim_type type, unsigned first, unsigned n)
2377 int i;
2379 if (n == 0)
2380 return map;
2382 if (isl_map_check_range(map, type, first, n) < 0)
2383 return isl_map_free(map);
2385 map = isl_map_cow(map);
2386 if (!map)
2387 return NULL;
2389 for (i = 0; i < map->n; ++i) {
2390 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2391 if (!map->p[i])
2392 goto error;
2394 return map;
2395 error:
2396 isl_map_free(map);
2397 return NULL;
2400 /* Eliminate the specified n dimensions starting at first from the
2401 * constraints, without removing the dimensions from the space.
2402 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2404 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2405 enum isl_dim_type type, unsigned first, unsigned n)
2407 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2410 /* Eliminate the specified n dimensions starting at first from the
2411 * constraints, without removing the dimensions from the space.
2412 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2414 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2415 unsigned first, unsigned n)
2417 return isl_set_eliminate(set, isl_dim_set, first, n);
2420 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2421 __isl_take isl_basic_map *bmap)
2423 isl_size v_div;
2425 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2426 if (v_div < 0)
2427 return isl_basic_map_free(bmap);
2428 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2429 if (!bmap)
2430 return NULL;
2431 bmap->n_div = 0;
2432 return isl_basic_map_finalize(bmap);
2435 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2436 __isl_take isl_basic_set *bset)
2438 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2441 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2443 int i;
2445 if (!map)
2446 return NULL;
2447 if (map->n == 0)
2448 return map;
2450 map = isl_map_cow(map);
2451 if (!map)
2452 return NULL;
2454 for (i = 0; i < map->n; ++i) {
2455 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2456 if (!map->p[i])
2457 goto error;
2459 return map;
2460 error:
2461 isl_map_free(map);
2462 return NULL;
2465 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2467 return isl_map_remove_divs(set);
2470 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2471 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2472 unsigned first, unsigned n)
2474 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2475 return isl_basic_map_free(bmap);
2476 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2477 return bmap;
2478 bmap = isl_basic_map_eliminate_vars(bmap,
2479 isl_basic_map_offset(bmap, type) - 1 + first, n);
2480 if (!bmap)
2481 return bmap;
2482 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2483 return bmap;
2484 bmap = isl_basic_map_drop(bmap, type, first, n);
2485 return bmap;
2488 /* Return true if the definition of the given div (recursively) involves
2489 * any of the given variables.
2491 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2492 unsigned first, unsigned n)
2494 int i;
2495 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2497 if (isl_int_is_zero(bmap->div[div][0]))
2498 return isl_bool_false;
2499 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2500 return isl_bool_true;
2502 for (i = bmap->n_div - 1; i >= 0; --i) {
2503 isl_bool involves;
2505 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2506 continue;
2507 involves = div_involves_vars(bmap, i, first, n);
2508 if (involves < 0 || involves)
2509 return involves;
2512 return isl_bool_false;
2515 /* Try and add a lower and/or upper bound on "div" to "bmap"
2516 * based on inequality "i".
2517 * "total" is the total number of variables (excluding the divs).
2518 * "v" is a temporary object that can be used during the calculations.
2519 * If "lb" is set, then a lower bound should be constructed.
2520 * If "ub" is set, then an upper bound should be constructed.
2522 * The calling function has already checked that the inequality does not
2523 * reference "div", but we still need to check that the inequality is
2524 * of the right form. We'll consider the case where we want to construct
2525 * a lower bound. The construction of upper bounds is similar.
2527 * Let "div" be of the form
2529 * q = floor((a + f(x))/d)
2531 * We essentially check if constraint "i" is of the form
2533 * b + f(x) >= 0
2535 * so that we can use it to derive a lower bound on "div".
2536 * However, we allow a slightly more general form
2538 * b + g(x) >= 0
2540 * with the condition that the coefficients of g(x) - f(x) are all
2541 * divisible by d.
2542 * Rewriting this constraint as
2544 * 0 >= -b - g(x)
2546 * adding a + f(x) to both sides and dividing by d, we obtain
2548 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2550 * Taking the floor on both sides, we obtain
2552 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2554 * or
2556 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2558 * In the case of an upper bound, we construct the constraint
2560 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2563 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2564 __isl_take isl_basic_map *bmap, int div, int i,
2565 unsigned total, isl_int v, int lb, int ub)
2567 int j;
2569 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2570 if (lb) {
2571 isl_int_sub(v, bmap->ineq[i][1 + j],
2572 bmap->div[div][1 + 1 + j]);
2573 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2575 if (ub) {
2576 isl_int_add(v, bmap->ineq[i][1 + j],
2577 bmap->div[div][1 + 1 + j]);
2578 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2581 if (!lb && !ub)
2582 return bmap;
2584 bmap = isl_basic_map_cow(bmap);
2585 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2586 if (lb) {
2587 int k = isl_basic_map_alloc_inequality(bmap);
2588 if (k < 0)
2589 goto error;
2590 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2591 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2592 bmap->div[div][1 + j]);
2593 isl_int_cdiv_q(bmap->ineq[k][j],
2594 bmap->ineq[k][j], bmap->div[div][0]);
2596 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2598 if (ub) {
2599 int k = isl_basic_map_alloc_inequality(bmap);
2600 if (k < 0)
2601 goto error;
2602 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2603 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2604 bmap->div[div][1 + j]);
2605 isl_int_fdiv_q(bmap->ineq[k][j],
2606 bmap->ineq[k][j], bmap->div[div][0]);
2608 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2611 return bmap;
2612 error:
2613 isl_basic_map_free(bmap);
2614 return NULL;
2617 /* This function is called right before "div" is eliminated from "bmap"
2618 * using Fourier-Motzkin.
2619 * Look through the constraints of "bmap" for constraints on the argument
2620 * of the integer division and use them to construct constraints on the
2621 * integer division itself. These constraints can then be combined
2622 * during the Fourier-Motzkin elimination.
2623 * Note that it is only useful to introduce lower bounds on "div"
2624 * if "bmap" already contains upper bounds on "div" as the newly
2625 * introduce lower bounds can then be combined with the pre-existing
2626 * upper bounds. Similarly for upper bounds.
2627 * We therefore first check if "bmap" contains any lower and/or upper bounds
2628 * on "div".
2630 * It is interesting to note that the introduction of these constraints
2631 * can indeed lead to more accurate results, even when compared to
2632 * deriving constraints on the argument of "div" from constraints on "div".
2633 * Consider, for example, the set
2635 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2637 * The second constraint can be rewritten as
2639 * 2 * [(-i-2j+3)/4] + k >= 0
2641 * from which we can derive
2643 * -i - 2j + 3 >= -2k
2645 * or
2647 * i + 2j <= 3 + 2k
2649 * Combined with the first constraint, we obtain
2651 * -3 <= 3 + 2k or k >= -3
2653 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2654 * the first constraint, we obtain
2656 * [(i + 2j)/4] >= [-3/4] = -1
2658 * Combining this constraint with the second constraint, we obtain
2660 * k >= -2
2662 static __isl_give isl_basic_map *insert_bounds_on_div(
2663 __isl_take isl_basic_map *bmap, int div)
2665 int i;
2666 int check_lb, check_ub;
2667 isl_int v;
2668 isl_size v_div;
2670 if (!bmap)
2671 return NULL;
2673 if (isl_int_is_zero(bmap->div[div][0]))
2674 return bmap;
2676 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2677 if (v_div < 0)
2678 return isl_basic_map_free(bmap);
2680 check_lb = 0;
2681 check_ub = 0;
2682 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2683 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2684 if (s > 0)
2685 check_ub = 1;
2686 if (s < 0)
2687 check_lb = 1;
2690 if (!check_lb && !check_ub)
2691 return bmap;
2693 isl_int_init(v);
2695 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2696 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2697 continue;
2699 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2700 check_lb, check_ub);
2703 isl_int_clear(v);
2705 return bmap;
2708 /* Remove all divs (recursively) involving any of the given dimensions
2709 * in their definitions.
2711 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2712 __isl_take isl_basic_map *bmap,
2713 enum isl_dim_type type, unsigned first, unsigned n)
2715 int i;
2717 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2718 return isl_basic_map_free(bmap);
2719 first += isl_basic_map_offset(bmap, type);
2721 for (i = bmap->n_div - 1; i >= 0; --i) {
2722 isl_bool involves;
2724 involves = div_involves_vars(bmap, i, first, n);
2725 if (involves < 0)
2726 return isl_basic_map_free(bmap);
2727 if (!involves)
2728 continue;
2729 bmap = insert_bounds_on_div(bmap, i);
2730 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2731 if (!bmap)
2732 return NULL;
2733 i = bmap->n_div;
2736 return bmap;
2739 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2740 __isl_take isl_basic_set *bset,
2741 enum isl_dim_type type, unsigned first, unsigned n)
2743 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2746 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2747 enum isl_dim_type type, unsigned first, unsigned n)
2749 int i;
2751 if (!map)
2752 return NULL;
2753 if (map->n == 0)
2754 return map;
2756 map = isl_map_cow(map);
2757 if (!map)
2758 return NULL;
2760 for (i = 0; i < map->n; ++i) {
2761 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2762 type, first, n);
2763 if (!map->p[i])
2764 goto error;
2766 return map;
2767 error:
2768 isl_map_free(map);
2769 return NULL;
2772 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2773 enum isl_dim_type type, unsigned first, unsigned n)
2775 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2776 type, first, n));
2779 /* Does the description of "bmap" depend on the specified dimensions?
2780 * We also check whether the dimensions appear in any of the div definitions.
2781 * In principle there is no need for this check. If the dimensions appear
2782 * in a div definition, they also appear in the defining constraints of that
2783 * div.
2785 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2786 enum isl_dim_type type, unsigned first, unsigned n)
2788 int i;
2790 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2791 return isl_bool_error;
2793 first += isl_basic_map_offset(bmap, type);
2794 for (i = 0; i < bmap->n_eq; ++i)
2795 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2796 return isl_bool_true;
2797 for (i = 0; i < bmap->n_ineq; ++i)
2798 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2799 return isl_bool_true;
2800 for (i = 0; i < bmap->n_div; ++i) {
2801 if (isl_int_is_zero(bmap->div[i][0]))
2802 continue;
2803 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2804 return isl_bool_true;
2807 return isl_bool_false;
2810 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2811 enum isl_dim_type type, unsigned first, unsigned n)
2813 int i;
2815 if (isl_map_check_range(map, type, first, n) < 0)
2816 return isl_bool_error;
2818 for (i = 0; i < map->n; ++i) {
2819 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2820 type, first, n);
2821 if (involves < 0 || involves)
2822 return involves;
2825 return isl_bool_false;
2828 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2829 enum isl_dim_type type, unsigned first, unsigned n)
2831 return isl_basic_map_involves_dims(bset, type, first, n);
2834 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2835 enum isl_dim_type type, unsigned first, unsigned n)
2837 return isl_map_involves_dims(set, type, first, n);
2840 /* Drop all constraints in bmap that involve any of the dimensions
2841 * first to first+n-1.
2842 * This function only performs the actual removal of constraints.
2844 * This function should not call finalize since it is used by
2845 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2847 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2848 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2850 int i;
2852 if (n == 0)
2853 return bmap;
2855 bmap = isl_basic_map_cow(bmap);
2857 if (!bmap)
2858 return NULL;
2860 for (i = bmap->n_eq - 1; i >= 0; --i) {
2861 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2862 continue;
2863 isl_basic_map_drop_equality(bmap, i);
2866 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2867 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2868 continue;
2869 isl_basic_map_drop_inequality(bmap, i);
2872 return bmap;
2875 /* Drop all constraints in bset that involve any of the dimensions
2876 * first to first+n-1.
2877 * This function only performs the actual removal of constraints.
2879 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2880 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2882 return isl_basic_map_drop_constraints_involving(bset, first, n);
2885 /* Drop all constraints in bmap that do not involve any of the dimensions
2886 * first to first + n - 1 of the given type.
2888 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2889 __isl_take isl_basic_map *bmap,
2890 enum isl_dim_type type, unsigned first, unsigned n)
2892 int i;
2894 if (n == 0) {
2895 isl_space *space = isl_basic_map_get_space(bmap);
2896 isl_basic_map_free(bmap);
2897 return isl_basic_map_universe(space);
2899 bmap = isl_basic_map_cow(bmap);
2900 if (!bmap)
2901 return NULL;
2903 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2904 return isl_basic_map_free(bmap);
2906 first += isl_basic_map_offset(bmap, type) - 1;
2908 for (i = bmap->n_eq - 1; i >= 0; --i) {
2909 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2910 continue;
2911 isl_basic_map_drop_equality(bmap, i);
2914 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2915 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2916 continue;
2917 isl_basic_map_drop_inequality(bmap, i);
2920 bmap = isl_basic_map_add_known_div_constraints(bmap);
2921 return bmap;
2924 /* Drop all constraints in bset that do not involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2928 __isl_take isl_basic_set *bset,
2929 enum isl_dim_type type, unsigned first, unsigned n)
2931 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2932 type, first, n);
2935 /* Drop all constraints in bmap that involve any of the dimensions
2936 * first to first + n - 1 of the given type.
2938 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2939 __isl_take isl_basic_map *bmap,
2940 enum isl_dim_type type, unsigned first, unsigned n)
2942 if (!bmap)
2943 return NULL;
2944 if (n == 0)
2945 return bmap;
2947 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2948 return isl_basic_map_free(bmap);
2950 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2951 first += isl_basic_map_offset(bmap, type) - 1;
2952 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2953 bmap = isl_basic_map_add_known_div_constraints(bmap);
2954 return bmap;
2957 /* Drop all constraints in bset that involve any of the dimensions
2958 * first to first + n - 1 of the given type.
2960 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2961 __isl_take isl_basic_set *bset,
2962 enum isl_dim_type type, unsigned first, unsigned n)
2964 return isl_basic_map_drop_constraints_involving_dims(bset,
2965 type, first, n);
2968 /* Drop constraints from "map" by applying "drop" to each basic map.
2970 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2971 enum isl_dim_type type, unsigned first, unsigned n,
2972 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2973 enum isl_dim_type type, unsigned first, unsigned n))
2975 int i;
2977 if (isl_map_check_range(map, type, first, n) < 0)
2978 return isl_map_free(map);
2980 map = isl_map_cow(map);
2981 if (!map)
2982 return NULL;
2984 for (i = 0; i < map->n; ++i) {
2985 map->p[i] = drop(map->p[i], type, first, n);
2986 if (!map->p[i])
2987 return isl_map_free(map);
2990 if (map->n > 1)
2991 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2993 return map;
2996 /* Drop all constraints in map that involve any of the dimensions
2997 * first to first + n - 1 of the given type.
2999 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3000 __isl_take isl_map *map,
3001 enum isl_dim_type type, unsigned first, unsigned n)
3003 if (n == 0)
3004 return map;
3005 return drop_constraints(map, type, first, n,
3006 &isl_basic_map_drop_constraints_involving_dims);
3009 /* Drop all constraints in "map" that do not involve any of the dimensions
3010 * first to first + n - 1 of the given type.
3012 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3013 __isl_take isl_map *map,
3014 enum isl_dim_type type, unsigned first, unsigned n)
3016 if (n == 0) {
3017 isl_space *space = isl_map_get_space(map);
3018 isl_map_free(map);
3019 return isl_map_universe(space);
3021 return drop_constraints(map, type, first, n,
3022 &isl_basic_map_drop_constraints_not_involving_dims);
3025 /* Drop all constraints in set that involve any of the dimensions
3026 * first to first + n - 1 of the given type.
3028 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3029 __isl_take isl_set *set,
3030 enum isl_dim_type type, unsigned first, unsigned n)
3032 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3035 /* Drop all constraints in "set" that do not involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3039 __isl_take isl_set *set,
3040 enum isl_dim_type type, unsigned first, unsigned n)
3042 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3045 /* Does local variable "div" of "bmap" have a complete explicit representation?
3046 * Having a complete explicit representation requires not only
3047 * an explicit representation, but also that all local variables
3048 * that appear in this explicit representation in turn have
3049 * a complete explicit representation.
3051 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3053 int i;
3054 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3055 isl_bool marked;
3057 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3058 if (marked < 0 || marked)
3059 return isl_bool_not(marked);
3061 for (i = bmap->n_div - 1; i >= 0; --i) {
3062 isl_bool known;
3064 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3065 continue;
3066 known = isl_basic_map_div_is_known(bmap, i);
3067 if (known < 0 || !known)
3068 return known;
3071 return isl_bool_true;
3074 /* Remove all divs that are unknown or defined in terms of unknown divs.
3076 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3077 __isl_take isl_basic_map *bmap)
3079 int i;
3081 if (!bmap)
3082 return NULL;
3084 for (i = bmap->n_div - 1; i >= 0; --i) {
3085 if (isl_basic_map_div_is_known(bmap, i))
3086 continue;
3087 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3088 if (!bmap)
3089 return NULL;
3090 i = bmap->n_div;
3093 return bmap;
3096 /* Remove all divs that are unknown or defined in terms of unknown divs.
3098 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3099 __isl_take isl_basic_set *bset)
3101 return isl_basic_map_remove_unknown_divs(bset);
3104 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3106 int i;
3108 if (!map)
3109 return NULL;
3110 if (map->n == 0)
3111 return map;
3113 map = isl_map_cow(map);
3114 if (!map)
3115 return NULL;
3117 for (i = 0; i < map->n; ++i) {
3118 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3119 if (!map->p[i])
3120 goto error;
3122 return map;
3123 error:
3124 isl_map_free(map);
3125 return NULL;
3128 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3130 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3133 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3134 __isl_take isl_basic_set *bset,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 isl_basic_map *bmap = bset_to_bmap(bset);
3138 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3139 return bset_from_bmap(bmap);
3142 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3143 enum isl_dim_type type, unsigned first, unsigned n)
3145 int i;
3147 if (n == 0)
3148 return map;
3150 map = isl_map_cow(map);
3151 if (isl_map_check_range(map, type, first, n) < 0)
3152 return isl_map_free(map);
3154 for (i = 0; i < map->n; ++i) {
3155 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3156 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3157 if (!map->p[i])
3158 goto error;
3160 map = isl_map_drop(map, type, first, n);
3161 return map;
3162 error:
3163 isl_map_free(map);
3164 return NULL;
3167 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3168 enum isl_dim_type type, unsigned first, unsigned n)
3170 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3171 type, first, n));
3174 /* Project out n inputs starting at first using Fourier-Motzkin */
3175 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3176 unsigned first, unsigned n)
3178 return isl_map_remove_dims(map, isl_dim_in, first, n);
3181 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3182 FILE *out, int indent)
3184 isl_printer *p;
3186 if (!bset) {
3187 fprintf(out, "null basic set\n");
3188 return;
3191 fprintf(out, "%*s", indent, "");
3192 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3193 bset->ref, bset->dim->nparam, bset->dim->n_out,
3194 bset->extra, bset->flags);
3196 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3197 p = isl_printer_set_dump(p, 1);
3198 p = isl_printer_set_indent(p, indent);
3199 p = isl_printer_start_line(p);
3200 p = isl_printer_print_basic_set(p, bset);
3201 p = isl_printer_end_line(p);
3202 isl_printer_free(p);
3205 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3206 FILE *out, int indent)
3208 isl_printer *p;
3210 if (!bmap) {
3211 fprintf(out, "null basic map\n");
3212 return;
3215 fprintf(out, "%*s", indent, "");
3216 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3217 "flags: %x, n_name: %d\n",
3218 bmap->ref,
3219 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3220 bmap->extra, bmap->flags, bmap->dim->n_id);
3222 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3223 p = isl_printer_set_dump(p, 1);
3224 p = isl_printer_set_indent(p, indent);
3225 p = isl_printer_start_line(p);
3226 p = isl_printer_print_basic_map(p, bmap);
3227 p = isl_printer_end_line(p);
3228 isl_printer_free(p);
3231 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3232 unsigned pos)
3234 isl_size total;
3236 total = isl_basic_map_dim(bmap, isl_dim_all);
3237 if (total < 0)
3238 return isl_basic_map_free(bmap);
3239 if (pos >= bmap->n_ineq)
3240 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3241 "invalid position", return isl_basic_map_free(bmap));
3242 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3243 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3244 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3245 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3246 return bmap;
3249 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3250 unsigned flags)
3252 if (isl_space_check_is_set(space) < 0)
3253 goto error;
3254 return isl_map_alloc_space(space, n, flags);
3255 error:
3256 isl_space_free(space);
3257 return NULL;
3260 /* Make sure "map" has room for at least "n" more basic maps.
3262 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3264 int i;
3265 struct isl_map *grown = NULL;
3267 if (!map)
3268 return NULL;
3269 isl_assert(map->ctx, n >= 0, goto error);
3270 if (map->n + n <= map->size)
3271 return map;
3272 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3273 if (!grown)
3274 goto error;
3275 for (i = 0; i < map->n; ++i) {
3276 grown->p[i] = isl_basic_map_copy(map->p[i]);
3277 if (!grown->p[i])
3278 goto error;
3279 grown->n++;
3281 isl_map_free(map);
3282 return grown;
3283 error:
3284 isl_map_free(grown);
3285 isl_map_free(map);
3286 return NULL;
3289 /* Make sure "set" has room for at least "n" more basic sets.
3291 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3293 return set_from_map(isl_map_grow(set_to_map(set), n));
3296 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3298 return isl_map_from_basic_map(bset);
3301 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3303 struct isl_map *map;
3305 if (!bmap)
3306 return NULL;
3308 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3309 return isl_map_add_basic_map(map, bmap);
3312 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3313 __isl_take isl_basic_set *bset)
3315 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3316 bset_to_bmap(bset)));
3319 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3321 return isl_map_free(set);
3324 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3326 int i;
3328 if (!set) {
3329 fprintf(out, "null set\n");
3330 return;
3333 fprintf(out, "%*s", indent, "");
3334 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3335 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3336 set->flags);
3337 for (i = 0; i < set->n; ++i) {
3338 fprintf(out, "%*s", indent, "");
3339 fprintf(out, "basic set %d:\n", i);
3340 isl_basic_set_print_internal(set->p[i], out, indent+4);
3344 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3346 int i;
3348 if (!map) {
3349 fprintf(out, "null map\n");
3350 return;
3353 fprintf(out, "%*s", indent, "");
3354 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3355 "flags: %x, n_name: %d\n",
3356 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3357 map->dim->n_out, map->flags, map->dim->n_id);
3358 for (i = 0; i < map->n; ++i) {
3359 fprintf(out, "%*s", indent, "");
3360 fprintf(out, "basic map %d:\n", i);
3361 isl_basic_map_print_internal(map->p[i], out, indent+4);
3365 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3366 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3368 struct isl_basic_map *bmap_domain;
3369 isl_size dim;
3371 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3372 goto error;
3374 dim = isl_basic_set_dim(bset, isl_dim_set);
3375 if (dim < 0)
3376 goto error;
3377 if (dim != 0)
3378 isl_assert(bset->ctx,
3379 isl_basic_map_compatible_domain(bmap, bset), goto error);
3381 bmap = isl_basic_map_cow(bmap);
3382 if (!bmap)
3383 goto error;
3384 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3385 bset->n_div, bset->n_eq, bset->n_ineq);
3386 bmap_domain = isl_basic_map_from_domain(bset);
3387 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3389 bmap = isl_basic_map_simplify(bmap);
3390 return isl_basic_map_finalize(bmap);
3391 error:
3392 isl_basic_map_free(bmap);
3393 isl_basic_set_free(bset);
3394 return NULL;
3397 /* Check that the space of "bset" is the same as that of the range of "bmap".
3399 static isl_stat isl_basic_map_check_compatible_range(
3400 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3402 isl_bool ok;
3404 ok = isl_basic_map_compatible_range(bmap, bset);
3405 if (ok < 0)
3406 return isl_stat_error;
3407 if (!ok)
3408 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3409 "incompatible spaces", return isl_stat_error);
3411 return isl_stat_ok;
3414 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3415 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3417 struct isl_basic_map *bmap_range;
3418 isl_size dim;
3420 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3421 goto error;
3423 dim = isl_basic_set_dim(bset, isl_dim_set);
3424 if (dim < 0)
3425 goto error;
3426 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3427 goto error;
3429 if (isl_basic_set_plain_is_universe(bset)) {
3430 isl_basic_set_free(bset);
3431 return bmap;
3434 bmap = isl_basic_map_cow(bmap);
3435 if (!bmap)
3436 goto error;
3437 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3438 bset->n_div, bset->n_eq, bset->n_ineq);
3439 bmap_range = bset_to_bmap(bset);
3440 bmap = add_constraints(bmap, bmap_range, 0, 0);
3442 bmap = isl_basic_map_simplify(bmap);
3443 return isl_basic_map_finalize(bmap);
3444 error:
3445 isl_basic_map_free(bmap);
3446 isl_basic_set_free(bset);
3447 return NULL;
3450 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3451 __isl_keep isl_vec *vec)
3453 int i;
3454 isl_size total;
3455 isl_int s;
3457 total = isl_basic_map_dim(bmap, isl_dim_all);
3458 if (total < 0 || !vec)
3459 return isl_bool_error;
3461 if (1 + total != vec->size)
3462 return isl_bool_false;
3464 isl_int_init(s);
3466 for (i = 0; i < bmap->n_eq; ++i) {
3467 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3468 if (!isl_int_is_zero(s)) {
3469 isl_int_clear(s);
3470 return isl_bool_false;
3474 for (i = 0; i < bmap->n_ineq; ++i) {
3475 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3476 if (isl_int_is_neg(s)) {
3477 isl_int_clear(s);
3478 return isl_bool_false;
3482 isl_int_clear(s);
3484 return isl_bool_true;
3487 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3488 __isl_keep isl_vec *vec)
3490 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3493 __isl_give isl_basic_map *isl_basic_map_intersect(
3494 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3496 struct isl_vec *sample = NULL;
3497 isl_space *space1, *space2;
3498 isl_size dim1, dim2, nparam1, nparam2;
3500 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3501 goto error;
3502 space1 = isl_basic_map_peek_space(bmap1);
3503 space2 = isl_basic_map_peek_space(bmap2);
3504 dim1 = isl_space_dim(space1, isl_dim_all);
3505 dim2 = isl_space_dim(space2, isl_dim_all);
3506 nparam1 = isl_space_dim(space1, isl_dim_param);
3507 nparam2 = isl_space_dim(space2, isl_dim_param);
3508 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3509 goto error;
3510 if (dim1 == nparam1 && dim2 != nparam2)
3511 return isl_basic_map_intersect(bmap2, bmap1);
3513 if (dim2 != nparam2)
3514 isl_assert(bmap1->ctx,
3515 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3517 if (isl_basic_map_plain_is_empty(bmap1)) {
3518 isl_basic_map_free(bmap2);
3519 return bmap1;
3521 if (isl_basic_map_plain_is_empty(bmap2)) {
3522 isl_basic_map_free(bmap1);
3523 return bmap2;
3526 if (bmap1->sample &&
3527 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3528 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3529 sample = isl_vec_copy(bmap1->sample);
3530 else if (bmap2->sample &&
3531 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3532 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3533 sample = isl_vec_copy(bmap2->sample);
3535 bmap1 = isl_basic_map_cow(bmap1);
3536 if (!bmap1)
3537 goto error;
3538 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3539 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3540 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3542 if (!bmap1)
3543 isl_vec_free(sample);
3544 else if (sample) {
3545 isl_vec_free(bmap1->sample);
3546 bmap1->sample = sample;
3549 bmap1 = isl_basic_map_simplify(bmap1);
3550 return isl_basic_map_finalize(bmap1);
3551 error:
3552 if (sample)
3553 isl_vec_free(sample);
3554 isl_basic_map_free(bmap1);
3555 isl_basic_map_free(bmap2);
3556 return NULL;
3559 struct isl_basic_set *isl_basic_set_intersect(
3560 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3562 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3563 bset_to_bmap(bset2)));
3566 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3567 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3569 return isl_basic_set_intersect(bset1, bset2);
3572 /* Special case of isl_map_intersect, where both map1 and map2
3573 * are convex, without any divs and such that either map1 or map2
3574 * contains a single constraint. This constraint is then simply
3575 * added to the other map.
3577 static __isl_give isl_map *map_intersect_add_constraint(
3578 __isl_take isl_map *map1, __isl_take isl_map *map2)
3580 isl_assert(map1->ctx, map1->n == 1, goto error);
3581 isl_assert(map2->ctx, map1->n == 1, goto error);
3582 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3583 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3585 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3586 return isl_map_intersect(map2, map1);
3588 map1 = isl_map_cow(map1);
3589 if (!map1)
3590 goto error;
3591 if (isl_map_plain_is_empty(map1)) {
3592 isl_map_free(map2);
3593 return map1;
3595 if (map2->p[0]->n_eq == 1)
3596 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3597 else
3598 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3599 map2->p[0]->ineq[0]);
3601 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3602 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3603 if (!map1->p[0])
3604 goto error;
3606 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3607 isl_basic_map_free(map1->p[0]);
3608 map1->n = 0;
3611 isl_map_free(map2);
3613 map1 = isl_map_unmark_normalized(map1);
3614 return map1;
3615 error:
3616 isl_map_free(map1);
3617 isl_map_free(map2);
3618 return NULL;
3621 /* map2 may be either a parameter domain or a map living in the same
3622 * space as map1.
3624 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3625 __isl_take isl_map *map2)
3627 unsigned flags = 0;
3628 isl_bool equal;
3629 isl_map *result;
3630 int i, j;
3631 isl_size dim2, nparam2;
3633 if (!map1 || !map2)
3634 goto error;
3636 if ((isl_map_plain_is_empty(map1) ||
3637 isl_map_plain_is_universe(map2)) &&
3638 isl_space_is_equal(map1->dim, map2->dim)) {
3639 isl_map_free(map2);
3640 return map1;
3642 if ((isl_map_plain_is_empty(map2) ||
3643 isl_map_plain_is_universe(map1)) &&
3644 isl_space_is_equal(map1->dim, map2->dim)) {
3645 isl_map_free(map1);
3646 return map2;
3649 if (map1->n == 1 && map2->n == 1 &&
3650 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3651 isl_space_is_equal(map1->dim, map2->dim) &&
3652 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3653 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3654 return map_intersect_add_constraint(map1, map2);
3656 equal = isl_map_plain_is_equal(map1, map2);
3657 if (equal < 0)
3658 goto error;
3659 if (equal) {
3660 isl_map_free(map2);
3661 return map1;
3664 dim2 = isl_map_dim(map2, isl_dim_all);
3665 nparam2 = isl_map_dim(map2, isl_dim_param);
3666 if (dim2 < 0 || nparam2 < 0)
3667 goto error;
3668 if (dim2 != nparam2)
3669 isl_assert(map1->ctx,
3670 isl_space_is_equal(map1->dim, map2->dim), goto error);
3672 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3673 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3674 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3676 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3677 map1->n * map2->n, flags);
3678 if (!result)
3679 goto error;
3680 for (i = 0; i < map1->n; ++i)
3681 for (j = 0; j < map2->n; ++j) {
3682 struct isl_basic_map *part;
3683 part = isl_basic_map_intersect(
3684 isl_basic_map_copy(map1->p[i]),
3685 isl_basic_map_copy(map2->p[j]));
3686 if (isl_basic_map_is_empty(part) < 0)
3687 part = isl_basic_map_free(part);
3688 result = isl_map_add_basic_map(result, part);
3689 if (!result)
3690 goto error;
3692 isl_map_free(map1);
3693 isl_map_free(map2);
3694 return result;
3695 error:
3696 isl_map_free(map1);
3697 isl_map_free(map2);
3698 return NULL;
3701 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3702 __isl_take isl_map *map2)
3704 if (!map1 || !map2)
3705 goto error;
3706 if (!isl_space_is_equal(map1->dim, map2->dim))
3707 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3708 "spaces don't match", goto error);
3709 return map_intersect_internal(map1, map2);
3710 error:
3711 isl_map_free(map1);
3712 isl_map_free(map2);
3713 return NULL;
3716 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3717 __isl_take isl_map *map2)
3719 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3722 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3724 return set_from_map(isl_map_intersect(set_to_map(set1),
3725 set_to_map(set2)));
3728 /* map_intersect_internal accepts intersections
3729 * with parameter domains, so we can just call that function.
3731 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3732 __isl_take isl_set *params)
3734 return map_intersect_internal(map, params);
3737 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3738 __isl_take isl_map *map2)
3740 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3743 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3744 __isl_take isl_set *params)
3746 return isl_map_intersect_params(set, params);
3749 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3751 isl_space *space;
3752 unsigned pos;
3753 isl_size n1, n2;
3755 if (!bmap)
3756 return NULL;
3757 bmap = isl_basic_map_cow(bmap);
3758 if (!bmap)
3759 return NULL;
3760 space = isl_space_reverse(isl_space_copy(bmap->dim));
3761 pos = isl_basic_map_offset(bmap, isl_dim_in);
3762 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3763 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3764 if (n1 < 0 || n2 < 0)
3765 bmap = isl_basic_map_free(bmap);
3766 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3767 return isl_basic_map_reset_space(bmap, space);
3770 static __isl_give isl_basic_map *basic_map_space_reset(
3771 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3773 isl_space *space;
3775 if (!bmap)
3776 return NULL;
3777 if (!isl_space_is_named_or_nested(bmap->dim, type))
3778 return bmap;
3780 space = isl_basic_map_get_space(bmap);
3781 space = isl_space_reset(space, type);
3782 bmap = isl_basic_map_reset_space(bmap, space);
3783 return bmap;
3786 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3787 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3788 unsigned pos, unsigned n)
3790 isl_bool rational, is_empty;
3791 isl_space *res_space;
3792 struct isl_basic_map *res;
3793 struct isl_dim_map *dim_map;
3794 isl_size total;
3795 unsigned off;
3796 enum isl_dim_type t;
3798 if (n == 0)
3799 return basic_map_space_reset(bmap, type);
3801 is_empty = isl_basic_map_plain_is_empty(bmap);
3802 total = isl_basic_map_dim(bmap, isl_dim_all);
3803 if (is_empty < 0 || total < 0)
3804 return isl_basic_map_free(bmap);
3805 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3806 type, pos, n);
3807 if (!res_space)
3808 return isl_basic_map_free(bmap);
3809 if (is_empty) {
3810 isl_basic_map_free(bmap);
3811 return isl_basic_map_empty(res_space);
3814 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3815 off = 0;
3816 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3817 isl_size dim;
3819 if (t != type) {
3820 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3821 } else {
3822 isl_size size = isl_basic_map_dim(bmap, t);
3823 if (size < 0)
3824 dim_map = isl_dim_map_free(dim_map);
3825 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3826 0, pos, off);
3827 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3828 pos, size - pos, off + pos + n);
3830 dim = isl_space_dim(res_space, t);
3831 if (dim < 0)
3832 dim_map = isl_dim_map_free(dim_map);
3833 off += dim;
3835 isl_dim_map_div(dim_map, bmap, off);
3837 res = isl_basic_map_alloc_space(res_space,
3838 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3839 rational = isl_basic_map_is_rational(bmap);
3840 if (rational < 0)
3841 res = isl_basic_map_free(res);
3842 if (rational)
3843 res = isl_basic_map_set_rational(res);
3844 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3845 return isl_basic_map_finalize(res);
3848 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3849 __isl_take isl_basic_set *bset,
3850 enum isl_dim_type type, unsigned pos, unsigned n)
3852 return isl_basic_map_insert_dims(bset, type, pos, n);
3855 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3856 enum isl_dim_type type, unsigned n)
3858 isl_size dim;
3860 dim = isl_basic_map_dim(bmap, type);
3861 if (dim < 0)
3862 return isl_basic_map_free(bmap);
3863 return isl_basic_map_insert_dims(bmap, type, dim, n);
3866 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3867 enum isl_dim_type type, unsigned n)
3869 if (!bset)
3870 return NULL;
3871 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3872 return isl_basic_map_add_dims(bset, type, n);
3873 error:
3874 isl_basic_set_free(bset);
3875 return NULL;
3878 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3879 enum isl_dim_type type)
3881 isl_space *space;
3883 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3884 return map;
3886 space = isl_map_get_space(map);
3887 space = isl_space_reset(space, type);
3888 map = isl_map_reset_space(map, space);
3889 return map;
3892 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3893 enum isl_dim_type type, unsigned pos, unsigned n)
3895 int i;
3897 if (n == 0)
3898 return map_space_reset(map, type);
3900 map = isl_map_cow(map);
3901 if (!map)
3902 return NULL;
3904 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3905 if (!map->dim)
3906 goto error;
3908 for (i = 0; i < map->n; ++i) {
3909 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3910 if (!map->p[i])
3911 goto error;
3914 return map;
3915 error:
3916 isl_map_free(map);
3917 return NULL;
3920 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3921 enum isl_dim_type type, unsigned pos, unsigned n)
3923 return isl_map_insert_dims(set, type, pos, n);
3926 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3927 enum isl_dim_type type, unsigned n)
3929 isl_size dim;
3931 dim = isl_map_dim(map, type);
3932 if (dim < 0)
3933 return isl_map_free(map);
3934 return isl_map_insert_dims(map, type, dim, n);
3937 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3938 enum isl_dim_type type, unsigned n)
3940 if (!set)
3941 return NULL;
3942 isl_assert(set->ctx, type != isl_dim_in, goto error);
3943 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3944 error:
3945 isl_set_free(set);
3946 return NULL;
3949 __isl_give isl_basic_map *isl_basic_map_move_dims(
3950 __isl_take isl_basic_map *bmap,
3951 enum isl_dim_type dst_type, unsigned dst_pos,
3952 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3954 isl_space *space;
3955 struct isl_dim_map *dim_map;
3956 struct isl_basic_map *res;
3957 enum isl_dim_type t;
3958 isl_size total;
3959 unsigned off;
3961 if (!bmap)
3962 return NULL;
3963 if (n == 0) {
3964 bmap = isl_basic_map_reset(bmap, src_type);
3965 bmap = isl_basic_map_reset(bmap, dst_type);
3966 return bmap;
3969 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3970 return isl_basic_map_free(bmap);
3972 if (dst_type == src_type && dst_pos == src_pos)
3973 return bmap;
3975 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3977 if (pos(bmap->dim, dst_type) + dst_pos ==
3978 pos(bmap->dim, src_type) + src_pos +
3979 ((src_type < dst_type) ? n : 0)) {
3980 bmap = isl_basic_map_cow(bmap);
3981 if (!bmap)
3982 return NULL;
3984 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3985 src_type, src_pos, n);
3986 if (!bmap->dim)
3987 goto error;
3989 bmap = isl_basic_map_finalize(bmap);
3991 return bmap;
3994 total = isl_basic_map_dim(bmap, isl_dim_all);
3995 if (total < 0)
3996 return isl_basic_map_free(bmap);
3997 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3999 off = 0;
4000 space = isl_basic_map_peek_space(bmap);
4001 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4002 isl_size size = isl_space_dim(space, t);
4003 if (size < 0)
4004 dim_map = isl_dim_map_free(dim_map);
4005 if (t == dst_type) {
4006 isl_dim_map_dim_range(dim_map, space, t,
4007 0, dst_pos, off);
4008 off += dst_pos;
4009 isl_dim_map_dim_range(dim_map, space, src_type,
4010 src_pos, n, off);
4011 off += n;
4012 isl_dim_map_dim_range(dim_map, space, t,
4013 dst_pos, size - dst_pos, off);
4014 off += size - dst_pos;
4015 } else if (t == src_type) {
4016 isl_dim_map_dim_range(dim_map, space, t,
4017 0, src_pos, off);
4018 off += src_pos;
4019 isl_dim_map_dim_range(dim_map, space, t,
4020 src_pos + n, size - src_pos - n, off);
4021 off += size - src_pos - n;
4022 } else {
4023 isl_dim_map_dim(dim_map, space, t, off);
4024 off += size;
4027 isl_dim_map_div(dim_map, bmap, off);
4029 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4030 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4031 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4032 if (!bmap)
4033 goto error;
4035 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4036 src_type, src_pos, n);
4037 if (!bmap->dim)
4038 goto error;
4040 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4041 bmap = isl_basic_map_gauss(bmap, NULL);
4042 bmap = isl_basic_map_finalize(bmap);
4044 return bmap;
4045 error:
4046 isl_basic_map_free(bmap);
4047 return NULL;
4050 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4051 enum isl_dim_type dst_type, unsigned dst_pos,
4052 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4054 isl_basic_map *bmap = bset_to_bmap(bset);
4055 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4056 src_type, src_pos, n);
4057 return bset_from_bmap(bmap);
4060 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4061 enum isl_dim_type dst_type, unsigned dst_pos,
4062 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4064 if (!set)
4065 return NULL;
4066 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4067 return set_from_map(isl_map_move_dims(set_to_map(set),
4068 dst_type, dst_pos, src_type, src_pos, n));
4069 error:
4070 isl_set_free(set);
4071 return NULL;
4074 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4075 enum isl_dim_type dst_type, unsigned dst_pos,
4076 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4078 int i;
4080 if (n == 0) {
4081 map = isl_map_reset(map, src_type);
4082 map = isl_map_reset(map, dst_type);
4083 return map;
4086 if (isl_map_check_range(map, src_type, src_pos, n))
4087 return isl_map_free(map);
4089 if (dst_type == src_type && dst_pos == src_pos)
4090 return map;
4092 isl_assert(map->ctx, dst_type != src_type, goto error);
4094 map = isl_map_cow(map);
4095 if (!map)
4096 return NULL;
4098 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4099 if (!map->dim)
4100 goto error;
4102 for (i = 0; i < map->n; ++i) {
4103 map->p[i] = isl_basic_map_move_dims(map->p[i],
4104 dst_type, dst_pos,
4105 src_type, src_pos, n);
4106 if (!map->p[i])
4107 goto error;
4110 return map;
4111 error:
4112 isl_map_free(map);
4113 return NULL;
4116 /* Move the specified dimensions to the last columns right before
4117 * the divs. Don't change the dimension specification of bmap.
4118 * That's the responsibility of the caller.
4120 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4121 enum isl_dim_type type, unsigned first, unsigned n)
4123 isl_space *space;
4124 struct isl_dim_map *dim_map;
4125 struct isl_basic_map *res;
4126 enum isl_dim_type t;
4127 isl_size total;
4128 unsigned off;
4130 if (!bmap)
4131 return NULL;
4132 if (isl_basic_map_offset(bmap, type) + first + n ==
4133 isl_basic_map_offset(bmap, isl_dim_div))
4134 return bmap;
4136 total = isl_basic_map_dim(bmap, isl_dim_all);
4137 if (total < 0)
4138 return isl_basic_map_free(bmap);
4139 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4141 off = 0;
4142 space = isl_basic_map_peek_space(bmap);
4143 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4144 isl_size size = isl_space_dim(space, t);
4145 if (size < 0)
4146 dim_map = isl_dim_map_free(dim_map);
4147 if (t == type) {
4148 isl_dim_map_dim_range(dim_map, space, t,
4149 0, first, off);
4150 off += first;
4151 isl_dim_map_dim_range(dim_map, space, t,
4152 first, n, total - bmap->n_div - n);
4153 isl_dim_map_dim_range(dim_map, space, t,
4154 first + n, size - (first + n), off);
4155 off += size - (first + n);
4156 } else {
4157 isl_dim_map_dim(dim_map, space, t, off);
4158 off += size;
4161 isl_dim_map_div(dim_map, bmap, off + n);
4163 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4164 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4165 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4166 return res;
4169 /* Insert "n" rows in the divs of "bmap".
4171 * The number of columns is not changed, which means that the last
4172 * dimensions of "bmap" are being reintepreted as the new divs.
4173 * The space of "bmap" is not adjusted, however, which means
4174 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4175 * from the space of "bmap" is the responsibility of the caller.
4177 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4178 int n)
4180 int i;
4181 size_t row_size;
4182 isl_int **new_div;
4183 isl_int *old;
4185 bmap = isl_basic_map_cow(bmap);
4186 if (!bmap)
4187 return NULL;
4189 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4190 old = bmap->block2.data;
4191 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4192 (bmap->extra + n) * (1 + row_size));
4193 if (!bmap->block2.data)
4194 return isl_basic_map_free(bmap);
4195 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4196 if (!new_div)
4197 return isl_basic_map_free(bmap);
4198 for (i = 0; i < n; ++i) {
4199 new_div[i] = bmap->block2.data +
4200 (bmap->extra + i) * (1 + row_size);
4201 isl_seq_clr(new_div[i], 1 + row_size);
4203 for (i = 0; i < bmap->extra; ++i)
4204 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4205 free(bmap->div);
4206 bmap->div = new_div;
4207 bmap->n_div += n;
4208 bmap->extra += n;
4210 return bmap;
4213 /* Drop constraints from "bmap" that only involve the variables
4214 * of "type" in the range [first, first + n] that are not related
4215 * to any of the variables outside that interval.
4216 * These constraints cannot influence the values for the variables
4217 * outside the interval, except in case they cause "bmap" to be empty.
4218 * Only drop the constraints if "bmap" is known to be non-empty.
4220 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4221 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4222 unsigned first, unsigned n)
4224 int i;
4225 int *groups;
4226 isl_size dim, n_div;
4227 isl_bool non_empty;
4229 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4230 if (non_empty < 0)
4231 return isl_basic_map_free(bmap);
4232 if (!non_empty)
4233 return bmap;
4235 dim = isl_basic_map_dim(bmap, isl_dim_all);
4236 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4237 if (dim < 0 || n_div < 0)
4238 return isl_basic_map_free(bmap);
4239 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4240 if (!groups)
4241 return isl_basic_map_free(bmap);
4242 first += isl_basic_map_offset(bmap, type) - 1;
4243 for (i = 0; i < first; ++i)
4244 groups[i] = -1;
4245 for (i = first + n; i < dim - n_div; ++i)
4246 groups[i] = -1;
4248 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4250 return bmap;
4253 /* Turn the n dimensions of type type, starting at first
4254 * into existentially quantified variables.
4256 * If a subset of the projected out variables are unrelated
4257 * to any of the variables that remain, then the constraints
4258 * involving this subset are simply dropped first.
4260 __isl_give isl_basic_map *isl_basic_map_project_out(
4261 __isl_take isl_basic_map *bmap,
4262 enum isl_dim_type type, unsigned first, unsigned n)
4264 isl_bool empty;
4266 if (n == 0)
4267 return basic_map_space_reset(bmap, type);
4268 if (type == isl_dim_div)
4269 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4270 "cannot project out existentially quantified variables",
4271 return isl_basic_map_free(bmap));
4273 empty = isl_basic_map_plain_is_empty(bmap);
4274 if (empty < 0)
4275 return isl_basic_map_free(bmap);
4276 if (empty)
4277 bmap = isl_basic_map_set_to_empty(bmap);
4279 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4280 if (!bmap)
4281 return NULL;
4283 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4284 return isl_basic_map_remove_dims(bmap, type, first, n);
4286 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4287 return isl_basic_map_free(bmap);
4289 bmap = move_last(bmap, type, first, n);
4290 bmap = isl_basic_map_cow(bmap);
4291 bmap = insert_div_rows(bmap, n);
4292 if (!bmap)
4293 return NULL;
4295 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4296 if (!bmap->dim)
4297 goto error;
4298 bmap = isl_basic_map_simplify(bmap);
4299 bmap = isl_basic_map_drop_redundant_divs(bmap);
4300 return isl_basic_map_finalize(bmap);
4301 error:
4302 isl_basic_map_free(bmap);
4303 return NULL;
4306 /* Turn the n dimensions of type type, starting at first
4307 * into existentially quantified variables.
4309 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4310 enum isl_dim_type type, unsigned first, unsigned n)
4312 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4313 type, first, n));
4316 /* Turn the n dimensions of type type, starting at first
4317 * into existentially quantified variables.
4319 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4320 enum isl_dim_type type, unsigned first, unsigned n)
4322 int i;
4324 if (n == 0)
4325 return map_space_reset(map, type);
4327 if (isl_map_check_range(map, type, first, n) < 0)
4328 return isl_map_free(map);
4330 map = isl_map_cow(map);
4331 if (!map)
4332 return NULL;
4334 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4335 if (!map->dim)
4336 goto error;
4338 for (i = 0; i < map->n; ++i) {
4339 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4340 if (!map->p[i])
4341 goto error;
4344 return map;
4345 error:
4346 isl_map_free(map);
4347 return NULL;
4350 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4351 * into existentially quantified variables.
4353 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4354 enum isl_dim_type type, unsigned first, unsigned n)
4356 isl_size dim;
4358 dim = isl_map_dim(map, type);
4359 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4360 return isl_map_free(map);
4361 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4362 map = isl_map_project_out(map, type, 0, first);
4363 return map;
4366 /* Turn the n dimensions of type type, starting at first
4367 * into existentially quantified variables.
4369 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4370 enum isl_dim_type type, unsigned first, unsigned n)
4372 return set_from_map(isl_map_project_out(set_to_map(set),
4373 type, first, n));
4376 /* Return a map that projects the elements in "set" onto their
4377 * "n" set dimensions starting at "first".
4378 * "type" should be equal to isl_dim_set.
4380 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4381 enum isl_dim_type type, unsigned first, unsigned n)
4383 int i;
4384 isl_map *map;
4386 if (type != isl_dim_set)
4387 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4388 "only set dimensions can be projected out", goto error);
4389 if (isl_set_check_range(set, type, first, n) < 0)
4390 return isl_set_free(set);
4392 map = isl_map_from_domain(set);
4393 map = isl_map_add_dims(map, isl_dim_out, n);
4394 for (i = 0; i < n; ++i)
4395 map = isl_map_equate(map, isl_dim_in, first + i,
4396 isl_dim_out, i);
4397 return map;
4398 error:
4399 isl_set_free(set);
4400 return NULL;
4403 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4404 unsigned n)
4406 int i, j;
4407 isl_size total;
4409 total = isl_basic_map_dim(bmap, isl_dim_all);
4410 if (total < 0)
4411 return isl_basic_map_free(bmap);
4412 for (i = 0; i < n; ++i) {
4413 j = isl_basic_map_alloc_div(bmap);
4414 if (j < 0)
4415 goto error;
4416 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4418 return bmap;
4419 error:
4420 isl_basic_map_free(bmap);
4421 return NULL;
4424 struct isl_basic_map *isl_basic_map_apply_range(
4425 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4427 isl_space *space_result = NULL;
4428 struct isl_basic_map *bmap;
4429 isl_size n_in, n_out, n, nparam;
4430 unsigned total, pos;
4431 struct isl_dim_map *dim_map1, *dim_map2;
4433 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4434 goto error;
4435 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4436 bmap2->dim, isl_dim_in))
4437 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4438 "spaces don't match", goto error);
4440 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4441 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4442 n = isl_basic_map_dim(bmap1, isl_dim_out);
4443 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4444 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4445 goto error;
4447 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4448 isl_basic_map_get_space(bmap2));
4450 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4451 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4452 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4453 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4454 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4455 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4456 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4457 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4458 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4459 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4460 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4462 bmap = isl_basic_map_alloc_space(space_result,
4463 bmap1->n_div + bmap2->n_div + n,
4464 bmap1->n_eq + bmap2->n_eq,
4465 bmap1->n_ineq + bmap2->n_ineq);
4466 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4467 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4468 bmap = add_divs(bmap, n);
4469 bmap = isl_basic_map_simplify(bmap);
4470 bmap = isl_basic_map_drop_redundant_divs(bmap);
4471 return isl_basic_map_finalize(bmap);
4472 error:
4473 isl_basic_map_free(bmap1);
4474 isl_basic_map_free(bmap2);
4475 return NULL;
4478 struct isl_basic_set *isl_basic_set_apply(
4479 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4481 if (!bset || !bmap)
4482 goto error;
4484 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4485 goto error);
4487 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4488 bmap));
4489 error:
4490 isl_basic_set_free(bset);
4491 isl_basic_map_free(bmap);
4492 return NULL;
4495 struct isl_basic_map *isl_basic_map_apply_domain(
4496 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4498 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4499 goto error;
4500 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4501 bmap2->dim, isl_dim_in))
4502 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4503 "spaces don't match", goto error);
4505 bmap1 = isl_basic_map_reverse(bmap1);
4506 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4507 return isl_basic_map_reverse(bmap1);
4508 error:
4509 isl_basic_map_free(bmap1);
4510 isl_basic_map_free(bmap2);
4511 return NULL;
4514 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4515 * A \cap B -> f(A) + f(B)
4517 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4518 __isl_take isl_basic_map *bmap2)
4520 isl_size n_in, n_out, nparam;
4521 unsigned total, pos;
4522 struct isl_basic_map *bmap = NULL;
4523 struct isl_dim_map *dim_map1, *dim_map2;
4524 int i;
4526 if (!bmap1 || !bmap2)
4527 goto error;
4529 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4530 goto error);
4532 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4533 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4534 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4535 if (nparam < 0 || n_in < 0 || n_out < 0)
4536 goto error;
4538 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4539 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4540 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4541 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4542 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4543 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4544 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4545 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4546 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4547 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4548 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4550 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4551 bmap1->n_div + bmap2->n_div + 2 * n_out,
4552 bmap1->n_eq + bmap2->n_eq + n_out,
4553 bmap1->n_ineq + bmap2->n_ineq);
4554 for (i = 0; i < n_out; ++i) {
4555 int j = isl_basic_map_alloc_equality(bmap);
4556 if (j < 0)
4557 goto error;
4558 isl_seq_clr(bmap->eq[j], 1+total);
4559 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4560 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4561 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4563 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4564 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4565 bmap = add_divs(bmap, 2 * n_out);
4567 bmap = isl_basic_map_simplify(bmap);
4568 return isl_basic_map_finalize(bmap);
4569 error:
4570 isl_basic_map_free(bmap);
4571 isl_basic_map_free(bmap1);
4572 isl_basic_map_free(bmap2);
4573 return NULL;
4576 /* Given two maps A -> f(A) and B -> g(B), construct a map
4577 * A \cap B -> f(A) + f(B)
4579 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4580 __isl_take isl_map *map2)
4582 struct isl_map *result;
4583 int i, j;
4585 if (!map1 || !map2)
4586 goto error;
4588 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4590 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4591 map1->n * map2->n, 0);
4592 if (!result)
4593 goto error;
4594 for (i = 0; i < map1->n; ++i)
4595 for (j = 0; j < map2->n; ++j) {
4596 struct isl_basic_map *part;
4597 part = isl_basic_map_sum(
4598 isl_basic_map_copy(map1->p[i]),
4599 isl_basic_map_copy(map2->p[j]));
4600 if (isl_basic_map_is_empty(part))
4601 isl_basic_map_free(part);
4602 else
4603 result = isl_map_add_basic_map(result, part);
4604 if (!result)
4605 goto error;
4607 isl_map_free(map1);
4608 isl_map_free(map2);
4609 return result;
4610 error:
4611 isl_map_free(map1);
4612 isl_map_free(map2);
4613 return NULL;
4616 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4617 __isl_take isl_set *set2)
4619 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4622 /* Given a basic map A -> f(A), construct A -> -f(A).
4624 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4626 int i, j;
4627 unsigned off;
4628 isl_size n;
4630 bmap = isl_basic_map_cow(bmap);
4631 n = isl_basic_map_dim(bmap, isl_dim_out);
4632 if (n < 0)
4633 return isl_basic_map_free(bmap);
4635 off = isl_basic_map_offset(bmap, isl_dim_out);
4636 for (i = 0; i < bmap->n_eq; ++i)
4637 for (j = 0; j < n; ++j)
4638 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4639 for (i = 0; i < bmap->n_ineq; ++i)
4640 for (j = 0; j < n; ++j)
4641 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4642 for (i = 0; i < bmap->n_div; ++i)
4643 for (j = 0; j < n; ++j)
4644 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4645 bmap = isl_basic_map_gauss(bmap, NULL);
4646 return isl_basic_map_finalize(bmap);
4649 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4651 return isl_basic_map_neg(bset);
4654 /* Given a map A -> f(A), construct A -> -f(A).
4656 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4658 int i;
4660 map = isl_map_cow(map);
4661 if (!map)
4662 return NULL;
4664 for (i = 0; i < map->n; ++i) {
4665 map->p[i] = isl_basic_map_neg(map->p[i]);
4666 if (!map->p[i])
4667 goto error;
4670 return map;
4671 error:
4672 isl_map_free(map);
4673 return NULL;
4676 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4678 return set_from_map(isl_map_neg(set_to_map(set)));
4681 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4682 * A -> floor(f(A)/d).
4684 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4685 isl_int d)
4687 isl_size n_in, n_out, nparam;
4688 unsigned total, pos;
4689 struct isl_basic_map *result = NULL;
4690 struct isl_dim_map *dim_map;
4691 int i;
4693 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4694 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4695 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4696 if (nparam < 0 || n_in < 0 || n_out < 0)
4697 return isl_basic_map_free(bmap);
4699 total = nparam + n_in + n_out + bmap->n_div + n_out;
4700 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4701 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4702 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4703 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4704 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4706 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4707 bmap->n_div + n_out,
4708 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4709 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4710 result = add_divs(result, n_out);
4711 for (i = 0; i < n_out; ++i) {
4712 int j;
4713 j = isl_basic_map_alloc_inequality(result);
4714 if (j < 0)
4715 goto error;
4716 isl_seq_clr(result->ineq[j], 1+total);
4717 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4718 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4719 j = isl_basic_map_alloc_inequality(result);
4720 if (j < 0)
4721 goto error;
4722 isl_seq_clr(result->ineq[j], 1+total);
4723 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4724 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4725 isl_int_sub_ui(result->ineq[j][0], d, 1);
4728 result = isl_basic_map_simplify(result);
4729 return isl_basic_map_finalize(result);
4730 error:
4731 isl_basic_map_free(result);
4732 return NULL;
4735 /* Given a map A -> f(A) and an integer d, construct a map
4736 * A -> floor(f(A)/d).
4738 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4740 int i;
4742 map = isl_map_cow(map);
4743 if (!map)
4744 return NULL;
4746 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4747 for (i = 0; i < map->n; ++i) {
4748 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4749 if (!map->p[i])
4750 goto error;
4752 map = isl_map_unmark_normalized(map);
4754 return map;
4755 error:
4756 isl_map_free(map);
4757 return NULL;
4760 /* Given a map A -> f(A) and an integer d, construct a map
4761 * A -> floor(f(A)/d).
4763 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4764 __isl_take isl_val *d)
4766 if (!map || !d)
4767 goto error;
4768 if (!isl_val_is_int(d))
4769 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4770 "expecting integer denominator", goto error);
4771 map = isl_map_floordiv(map, d->n);
4772 isl_val_free(d);
4773 return map;
4774 error:
4775 isl_map_free(map);
4776 isl_val_free(d);
4777 return NULL;
4780 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4781 unsigned pos)
4783 int i;
4784 isl_size nparam;
4785 isl_size n_in;
4786 isl_size total;
4788 total = isl_basic_map_dim(bmap, isl_dim_all);
4789 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4790 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4791 if (total < 0 || nparam < 0 || n_in < 0)
4792 return isl_basic_map_free(bmap);
4793 i = isl_basic_map_alloc_equality(bmap);
4794 if (i < 0)
4795 goto error;
4796 isl_seq_clr(bmap->eq[i], 1 + total);
4797 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4798 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4799 return isl_basic_map_finalize(bmap);
4800 error:
4801 isl_basic_map_free(bmap);
4802 return NULL;
4805 /* Add a constraint to "bmap" expressing i_pos < o_pos
4807 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4808 unsigned pos)
4810 int i;
4811 isl_size nparam;
4812 isl_size n_in;
4813 isl_size total;
4815 total = isl_basic_map_dim(bmap, isl_dim_all);
4816 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4817 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4818 if (total < 0 || nparam < 0 || n_in < 0)
4819 return isl_basic_map_free(bmap);
4820 i = isl_basic_map_alloc_inequality(bmap);
4821 if (i < 0)
4822 goto error;
4823 isl_seq_clr(bmap->ineq[i], 1 + total);
4824 isl_int_set_si(bmap->ineq[i][0], -1);
4825 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4826 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4827 return isl_basic_map_finalize(bmap);
4828 error:
4829 isl_basic_map_free(bmap);
4830 return NULL;
4833 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4835 static __isl_give isl_basic_map *var_less_or_equal(
4836 __isl_take isl_basic_map *bmap, unsigned pos)
4838 int i;
4839 isl_size nparam;
4840 isl_size n_in;
4841 isl_size total;
4843 total = isl_basic_map_dim(bmap, isl_dim_all);
4844 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4845 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4846 if (total < 0 || nparam < 0 || n_in < 0)
4847 return isl_basic_map_free(bmap);
4848 i = isl_basic_map_alloc_inequality(bmap);
4849 if (i < 0)
4850 goto error;
4851 isl_seq_clr(bmap->ineq[i], 1 + total);
4852 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4853 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4854 return isl_basic_map_finalize(bmap);
4855 error:
4856 isl_basic_map_free(bmap);
4857 return NULL;
4860 /* Add a constraint to "bmap" expressing i_pos > o_pos
4862 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4863 unsigned pos)
4865 int i;
4866 isl_size nparam;
4867 isl_size n_in;
4868 isl_size total;
4870 total = isl_basic_map_dim(bmap, isl_dim_all);
4871 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4872 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4873 if (total < 0 || nparam < 0 || n_in < 0)
4874 return isl_basic_map_free(bmap);
4875 i = isl_basic_map_alloc_inequality(bmap);
4876 if (i < 0)
4877 goto error;
4878 isl_seq_clr(bmap->ineq[i], 1 + total);
4879 isl_int_set_si(bmap->ineq[i][0], -1);
4880 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4881 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4882 return isl_basic_map_finalize(bmap);
4883 error:
4884 isl_basic_map_free(bmap);
4885 return NULL;
4888 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4890 static __isl_give isl_basic_map *var_more_or_equal(
4891 __isl_take isl_basic_map *bmap, unsigned pos)
4893 int i;
4894 isl_size nparam;
4895 isl_size n_in;
4896 isl_size total;
4898 total = isl_basic_map_dim(bmap, isl_dim_all);
4899 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4900 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4901 if (total < 0 || nparam < 0 || n_in < 0)
4902 return isl_basic_map_free(bmap);
4903 i = isl_basic_map_alloc_inequality(bmap);
4904 if (i < 0)
4905 goto error;
4906 isl_seq_clr(bmap->ineq[i], 1 + total);
4907 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4908 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4909 return isl_basic_map_finalize(bmap);
4910 error:
4911 isl_basic_map_free(bmap);
4912 return NULL;
4915 __isl_give isl_basic_map *isl_basic_map_equal(
4916 __isl_take isl_space *space, unsigned n_equal)
4918 int i;
4919 struct isl_basic_map *bmap;
4920 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4921 if (!bmap)
4922 return NULL;
4923 for (i = 0; i < n_equal && bmap; ++i)
4924 bmap = var_equal(bmap, i);
4925 return isl_basic_map_finalize(bmap);
4928 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4930 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4931 unsigned pos)
4933 int i;
4934 struct isl_basic_map *bmap;
4935 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4936 if (!bmap)
4937 return NULL;
4938 for (i = 0; i < pos && bmap; ++i)
4939 bmap = var_equal(bmap, i);
4940 if (bmap)
4941 bmap = var_less(bmap, pos);
4942 return isl_basic_map_finalize(bmap);
4945 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4947 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4948 __isl_take isl_space *space, unsigned pos)
4950 int i;
4951 isl_basic_map *bmap;
4953 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4954 for (i = 0; i < pos; ++i)
4955 bmap = var_equal(bmap, i);
4956 bmap = var_less_or_equal(bmap, pos);
4957 return isl_basic_map_finalize(bmap);
4960 /* Return a relation on "space" expressing i_pos > o_pos
4962 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4963 unsigned pos)
4965 int i;
4966 struct isl_basic_map *bmap;
4967 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4968 if (!bmap)
4969 return NULL;
4970 for (i = 0; i < pos && bmap; ++i)
4971 bmap = var_equal(bmap, i);
4972 if (bmap)
4973 bmap = var_more(bmap, pos);
4974 return isl_basic_map_finalize(bmap);
4977 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4979 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4980 __isl_take isl_space *space, unsigned pos)
4982 int i;
4983 isl_basic_map *bmap;
4985 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4986 for (i = 0; i < pos; ++i)
4987 bmap = var_equal(bmap, i);
4988 bmap = var_more_or_equal(bmap, pos);
4989 return isl_basic_map_finalize(bmap);
4992 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4993 unsigned n, int equal)
4995 struct isl_map *map;
4996 int i;
4998 if (n == 0 && equal)
4999 return isl_map_universe(space);
5001 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5003 for (i = 0; i + 1 < n; ++i)
5004 map = isl_map_add_basic_map(map,
5005 isl_basic_map_less_at(isl_space_copy(space), i));
5006 if (n > 0) {
5007 if (equal)
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_less_or_equal_at(space, n - 1));
5010 else
5011 map = isl_map_add_basic_map(map,
5012 isl_basic_map_less_at(space, n - 1));
5013 } else
5014 isl_space_free(space);
5016 return map;
5019 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5021 if (!space)
5022 return NULL;
5023 return map_lex_lte_first(space, space->n_out, equal);
5026 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5028 return map_lex_lte_first(dim, n, 0);
5031 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5033 return map_lex_lte_first(dim, n, 1);
5036 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5038 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5041 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5043 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5046 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5047 unsigned n, int equal)
5049 struct isl_map *map;
5050 int i;
5052 if (n == 0 && equal)
5053 return isl_map_universe(space);
5055 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5057 for (i = 0; i + 1 < n; ++i)
5058 map = isl_map_add_basic_map(map,
5059 isl_basic_map_more_at(isl_space_copy(space), i));
5060 if (n > 0) {
5061 if (equal)
5062 map = isl_map_add_basic_map(map,
5063 isl_basic_map_more_or_equal_at(space, n - 1));
5064 else
5065 map = isl_map_add_basic_map(map,
5066 isl_basic_map_more_at(space, n - 1));
5067 } else
5068 isl_space_free(space);
5070 return map;
5073 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5075 if (!space)
5076 return NULL;
5077 return map_lex_gte_first(space, space->n_out, equal);
5080 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5082 return map_lex_gte_first(dim, n, 0);
5085 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5087 return map_lex_gte_first(dim, n, 1);
5090 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5092 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5095 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5097 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5100 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5101 __isl_take isl_set *set2)
5103 isl_map *map;
5104 map = isl_map_lex_le(isl_set_get_space(set1));
5105 map = isl_map_intersect_domain(map, set1);
5106 map = isl_map_intersect_range(map, set2);
5107 return map;
5110 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5111 __isl_take isl_set *set2)
5113 isl_map *map;
5114 map = isl_map_lex_lt(isl_set_get_space(set1));
5115 map = isl_map_intersect_domain(map, set1);
5116 map = isl_map_intersect_range(map, set2);
5117 return map;
5120 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5121 __isl_take isl_set *set2)
5123 isl_map *map;
5124 map = isl_map_lex_ge(isl_set_get_space(set1));
5125 map = isl_map_intersect_domain(map, set1);
5126 map = isl_map_intersect_range(map, set2);
5127 return map;
5130 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5131 __isl_take isl_set *set2)
5133 isl_map *map;
5134 map = isl_map_lex_gt(isl_set_get_space(set1));
5135 map = isl_map_intersect_domain(map, set1);
5136 map = isl_map_intersect_range(map, set2);
5137 return map;
5140 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5141 __isl_take isl_map *map2)
5143 isl_map *map;
5144 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5145 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5146 map = isl_map_apply_range(map, isl_map_reverse(map2));
5147 return map;
5150 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5151 __isl_take isl_map *map2)
5153 isl_map *map;
5154 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5155 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5156 map = isl_map_apply_range(map, isl_map_reverse(map2));
5157 return map;
5160 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5161 __isl_take isl_map *map2)
5163 isl_map *map;
5164 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5165 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5166 map = isl_map_apply_range(map, isl_map_reverse(map2));
5167 return map;
5170 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5171 __isl_take isl_map *map2)
5173 isl_map *map;
5174 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5175 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5176 map = isl_map_apply_range(map, isl_map_reverse(map2));
5177 return map;
5180 /* For the div d = floor(f/m) at position "div", add the constraint
5182 * f - m d >= 0
5184 static __isl_give isl_basic_map *add_upper_div_constraint(
5185 __isl_take isl_basic_map *bmap, unsigned div)
5187 int i;
5188 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5189 isl_size n_div;
5190 unsigned pos;
5192 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5193 if (v_div < 0 || n_div < 0)
5194 return isl_basic_map_free(bmap);
5195 pos = v_div + div;
5196 i = isl_basic_map_alloc_inequality(bmap);
5197 if (i < 0)
5198 return isl_basic_map_free(bmap);
5199 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5200 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5202 return bmap;
5205 /* For the div d = floor(f/m) at position "div", add the constraint
5207 * -(f-(m-1)) + m d >= 0
5209 static __isl_give isl_basic_map *add_lower_div_constraint(
5210 __isl_take isl_basic_map *bmap, unsigned div)
5212 int i;
5213 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5214 isl_size n_div;
5215 unsigned pos;
5217 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5218 if (v_div < 0 || n_div < 0)
5219 return isl_basic_map_free(bmap);
5220 pos = v_div + div;
5221 i = isl_basic_map_alloc_inequality(bmap);
5222 if (i < 0)
5223 return isl_basic_map_free(bmap);
5224 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5225 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5226 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5227 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5229 return bmap;
5232 /* For the div d = floor(f/m) at position "pos", add the constraints
5234 * f - m d >= 0
5235 * -(f-(m-1)) + m d >= 0
5237 * Note that the second constraint is the negation of
5239 * f - m d >= m
5241 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5242 __isl_take isl_basic_map *bmap, unsigned pos)
5244 bmap = add_upper_div_constraint(bmap, pos);
5245 bmap = add_lower_div_constraint(bmap, pos);
5246 return bmap;
5249 /* For each known div d = floor(f/m), add the constraints
5251 * f - m d >= 0
5252 * -(f-(m-1)) + m d >= 0
5254 * Remove duplicate constraints in case of some these div constraints
5255 * already appear in "bmap".
5257 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5258 __isl_take isl_basic_map *bmap)
5260 isl_size n_div;
5262 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5263 if (n_div < 0)
5264 return isl_basic_map_free(bmap);
5265 if (n_div == 0)
5266 return bmap;
5268 bmap = add_known_div_constraints(bmap);
5269 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5270 bmap = isl_basic_map_finalize(bmap);
5271 return bmap;
5274 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5276 * In particular, if this div is of the form d = floor(f/m),
5277 * then add the constraint
5279 * f - m d >= 0
5281 * if sign < 0 or the constraint
5283 * -(f-(m-1)) + m d >= 0
5285 * if sign > 0.
5287 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5288 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5290 if (sign < 0)
5291 return add_upper_div_constraint(bmap, div);
5292 else
5293 return add_lower_div_constraint(bmap, div);
5296 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5297 __isl_take isl_basic_map *bmap)
5299 if (!bmap)
5300 goto error;
5301 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5302 bmap->n_div == 0 &&
5303 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5304 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5305 return bset_from_bmap(bmap);
5306 bmap = isl_basic_map_cow(bmap);
5307 if (!bmap)
5308 goto error;
5309 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5310 if (!bmap->dim)
5311 goto error;
5312 bmap->extra -= bmap->n_div;
5313 bmap->n_div = 0;
5314 bmap = isl_basic_map_finalize(bmap);
5315 return bset_from_bmap(bmap);
5316 error:
5317 isl_basic_map_free(bmap);
5318 return NULL;
5321 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5322 __isl_take isl_basic_set *bset)
5324 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5327 /* Replace each element in "list" by the result of applying
5328 * isl_basic_map_underlying_set to the element.
5330 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5331 __isl_take isl_basic_map_list *list)
5333 int i;
5334 isl_size n;
5336 n = isl_basic_map_list_n_basic_map(list);
5337 if (n < 0)
5338 goto error;
5340 for (i = 0; i < n; ++i) {
5341 isl_basic_map *bmap;
5342 isl_basic_set *bset;
5344 bmap = isl_basic_map_list_get_basic_map(list, i);
5345 bset = isl_basic_set_underlying_set(bmap);
5346 list = isl_basic_set_list_set_basic_set(list, i, bset);
5349 return list;
5350 error:
5351 isl_basic_map_list_free(list);
5352 return NULL;
5355 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5356 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5358 struct isl_basic_map *bmap;
5359 struct isl_ctx *ctx;
5360 isl_size dim, bmap_total;
5361 unsigned total;
5362 int i;
5364 if (!bset || !like)
5365 goto error;
5366 ctx = bset->ctx;
5367 if (isl_basic_set_check_no_params(bset) < 0 ||
5368 isl_basic_set_check_no_locals(bset) < 0)
5369 goto error;
5370 dim = isl_basic_set_dim(bset, isl_dim_set);
5371 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5372 if (dim < 0 || bmap_total < 0)
5373 goto error;
5374 isl_assert(ctx, dim == bmap_total, goto error);
5375 if (like->n_div == 0) {
5376 isl_space *space = isl_basic_map_get_space(like);
5377 isl_basic_map_free(like);
5378 return isl_basic_map_reset_space(bset, space);
5380 bset = isl_basic_set_cow(bset);
5381 if (!bset)
5382 goto error;
5383 total = dim + bset->extra;
5384 bmap = bset_to_bmap(bset);
5385 isl_space_free(bmap->dim);
5386 bmap->dim = isl_space_copy(like->dim);
5387 if (!bmap->dim)
5388 goto error;
5389 bmap->n_div = like->n_div;
5390 bmap->extra += like->n_div;
5391 if (bmap->extra) {
5392 unsigned ltotal;
5393 isl_int **div;
5394 ltotal = total - bmap->extra + like->extra;
5395 if (ltotal > total)
5396 ltotal = total;
5397 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5398 bmap->extra * (1 + 1 + total));
5399 if (isl_blk_is_error(bmap->block2))
5400 goto error;
5401 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5402 if (!div)
5403 goto error;
5404 bmap->div = div;
5405 for (i = 0; i < bmap->extra; ++i)
5406 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5407 for (i = 0; i < like->n_div; ++i) {
5408 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5409 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5411 bmap = isl_basic_map_add_known_div_constraints(bmap);
5413 isl_basic_map_free(like);
5414 bmap = isl_basic_map_simplify(bmap);
5415 bmap = isl_basic_map_finalize(bmap);
5416 return bmap;
5417 error:
5418 isl_basic_map_free(like);
5419 isl_basic_set_free(bset);
5420 return NULL;
5423 struct isl_basic_set *isl_basic_set_from_underlying_set(
5424 struct isl_basic_set *bset, struct isl_basic_set *like)
5426 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5427 bset_to_bmap(like)));
5430 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5432 int i;
5434 map = isl_map_cow(map);
5435 if (!map)
5436 return NULL;
5437 map->dim = isl_space_cow(map->dim);
5438 if (!map->dim)
5439 goto error;
5441 for (i = 1; i < map->n; ++i)
5442 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5443 goto error);
5444 for (i = 0; i < map->n; ++i) {
5445 map->p[i] = bset_to_bmap(
5446 isl_basic_map_underlying_set(map->p[i]));
5447 if (!map->p[i])
5448 goto error;
5450 if (map->n == 0)
5451 map->dim = isl_space_underlying(map->dim, 0);
5452 else {
5453 isl_space_free(map->dim);
5454 map->dim = isl_space_copy(map->p[0]->dim);
5456 if (!map->dim)
5457 goto error;
5458 return set_from_map(map);
5459 error:
5460 isl_map_free(map);
5461 return NULL;
5464 /* Replace the space of "bmap" by "space".
5466 * If the space of "bmap" is identical to "space" (including the identifiers
5467 * of the input and output dimensions), then simply return the original input.
5469 __isl_give isl_basic_map *isl_basic_map_reset_space(
5470 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5472 isl_bool equal;
5473 isl_space *bmap_space;
5475 bmap_space = isl_basic_map_peek_space(bmap);
5476 equal = isl_space_is_equal(bmap_space, space);
5477 if (equal >= 0 && equal)
5478 equal = isl_space_has_equal_ids(bmap_space, space);
5479 if (equal < 0)
5480 goto error;
5481 if (equal) {
5482 isl_space_free(space);
5483 return bmap;
5485 bmap = isl_basic_map_cow(bmap);
5486 if (!bmap || !space)
5487 goto error;
5489 isl_space_free(bmap->dim);
5490 bmap->dim = space;
5492 bmap = isl_basic_map_finalize(bmap);
5494 return bmap;
5495 error:
5496 isl_basic_map_free(bmap);
5497 isl_space_free(space);
5498 return NULL;
5501 __isl_give isl_basic_set *isl_basic_set_reset_space(
5502 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5504 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5505 dim));
5508 /* Check that the total dimensions of "map" and "space" are the same.
5510 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5511 __isl_keep isl_space *space)
5513 isl_size dim1, dim2;
5515 dim1 = isl_map_dim(map, isl_dim_all);
5516 dim2 = isl_space_dim(space, isl_dim_all);
5517 if (dim1 < 0 || dim2 < 0)
5518 return isl_stat_error;
5519 if (dim1 == dim2)
5520 return isl_stat_ok;
5521 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5522 "total dimensions do not match", return isl_stat_error);
5525 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5526 __isl_take isl_space *space)
5528 int i;
5530 map = isl_map_cow(map);
5531 if (!map || !space)
5532 goto error;
5534 for (i = 0; i < map->n; ++i) {
5535 map->p[i] = isl_basic_map_reset_space(map->p[i],
5536 isl_space_copy(space));
5537 if (!map->p[i])
5538 goto error;
5540 isl_space_free(map->dim);
5541 map->dim = space;
5543 return map;
5544 error:
5545 isl_map_free(map);
5546 isl_space_free(space);
5547 return NULL;
5550 /* Replace the space of "map" by "space", without modifying
5551 * the dimension of "map".
5553 * If the space of "map" is identical to "space" (including the identifiers
5554 * of the input and output dimensions), then simply return the original input.
5556 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5557 __isl_take isl_space *space)
5559 isl_bool equal;
5560 isl_space *map_space;
5562 map_space = isl_map_peek_space(map);
5563 equal = isl_space_is_equal(map_space, space);
5564 if (equal >= 0 && equal)
5565 equal = isl_space_has_equal_ids(map_space, space);
5566 if (equal < 0)
5567 goto error;
5568 if (equal) {
5569 isl_space_free(space);
5570 return map;
5572 if (check_map_space_equal_total_dim(map, space) < 0)
5573 goto error;
5574 return isl_map_reset_space(map, space);
5575 error:
5576 isl_map_free(map);
5577 isl_space_free(space);
5578 return NULL;
5581 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5582 __isl_take isl_space *dim)
5584 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5587 /* Compute the parameter domain of the given basic set.
5589 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5591 isl_bool is_params;
5592 isl_space *space;
5593 isl_size n;
5595 is_params = isl_basic_set_is_params(bset);
5596 if (is_params < 0)
5597 return isl_basic_set_free(bset);
5598 if (is_params)
5599 return bset;
5601 n = isl_basic_set_dim(bset, isl_dim_set);
5602 if (n < 0)
5603 return isl_basic_set_free(bset);
5604 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5605 space = isl_basic_set_get_space(bset);
5606 space = isl_space_params(space);
5607 bset = isl_basic_set_reset_space(bset, space);
5608 return bset;
5611 /* Construct a zero-dimensional basic set with the given parameter domain.
5613 __isl_give isl_basic_set *isl_basic_set_from_params(
5614 __isl_take isl_basic_set *bset)
5616 isl_space *space;
5617 space = isl_basic_set_get_space(bset);
5618 space = isl_space_set_from_params(space);
5619 bset = isl_basic_set_reset_space(bset, space);
5620 return bset;
5623 /* Compute the parameter domain of the given set.
5625 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5627 return isl_map_params(set_to_map(set));
5630 /* Construct a zero-dimensional set with the given parameter domain.
5632 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5634 isl_space *space;
5635 space = isl_set_get_space(set);
5636 space = isl_space_set_from_params(space);
5637 set = isl_set_reset_space(set, space);
5638 return set;
5641 /* Compute the parameter domain of the given map.
5643 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5645 isl_space *space;
5646 isl_size n_in, n_out;
5648 n_in = isl_map_dim(map, isl_dim_in);
5649 n_out = isl_map_dim(map, isl_dim_out);
5650 if (n_in < 0 || n_out < 0)
5651 return isl_map_free(map);
5652 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5653 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5654 space = isl_map_get_space(map);
5655 space = isl_space_params(space);
5656 map = isl_map_reset_space(map, space);
5657 return map;
5660 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5662 isl_space *space;
5663 isl_size n_out;
5665 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5666 if (n_out < 0)
5667 return isl_basic_map_free(bmap);
5668 space = isl_space_domain(isl_basic_map_get_space(bmap));
5670 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5672 return isl_basic_map_reset_space(bmap, space);
5675 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5677 if (!bmap)
5678 return isl_bool_error;
5679 return isl_space_may_be_set(bmap->dim);
5682 /* Is this basic map actually a set?
5683 * Users should never call this function. Outside of isl,
5684 * the type should indicate whether something is a set or a map.
5686 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5688 if (!bmap)
5689 return isl_bool_error;
5690 return isl_space_is_set(bmap->dim);
5693 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5695 isl_bool is_set;
5697 is_set = isl_basic_map_is_set(bmap);
5698 if (is_set < 0)
5699 goto error;
5700 if (is_set)
5701 return bmap;
5702 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5703 error:
5704 isl_basic_map_free(bmap);
5705 return NULL;
5708 __isl_give isl_basic_map *isl_basic_map_domain_map(
5709 __isl_take isl_basic_map *bmap)
5711 int i;
5712 isl_space *space;
5713 isl_basic_map *domain;
5714 isl_size nparam, n_in, n_out;
5716 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5717 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5718 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5719 if (nparam < 0 || n_in < 0 || n_out < 0)
5720 return isl_basic_map_free(bmap);
5722 space = isl_basic_map_get_space(bmap);
5723 space = isl_space_from_range(isl_space_domain(space));
5724 domain = isl_basic_map_universe(space);
5726 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5727 bmap = isl_basic_map_apply_range(bmap, domain);
5728 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5730 for (i = 0; i < n_in; ++i)
5731 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5732 isl_dim_out, i);
5734 bmap = isl_basic_map_gauss(bmap, NULL);
5735 return isl_basic_map_finalize(bmap);
5738 __isl_give isl_basic_map *isl_basic_map_range_map(
5739 __isl_take isl_basic_map *bmap)
5741 int i;
5742 isl_space *space;
5743 isl_basic_map *range;
5744 isl_size nparam, n_in, n_out;
5746 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5747 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5748 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5749 if (nparam < 0 || n_in < 0 || n_out < 0)
5750 return isl_basic_map_free(bmap);
5752 space = isl_basic_map_get_space(bmap);
5753 space = isl_space_from_range(isl_space_range(space));
5754 range = isl_basic_map_universe(space);
5756 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5757 bmap = isl_basic_map_apply_range(bmap, range);
5758 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5760 for (i = 0; i < n_out; ++i)
5761 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5762 isl_dim_out, i);
5764 bmap = isl_basic_map_gauss(bmap, NULL);
5765 return isl_basic_map_finalize(bmap);
5768 int isl_map_may_be_set(__isl_keep isl_map *map)
5770 if (!map)
5771 return -1;
5772 return isl_space_may_be_set(map->dim);
5775 /* Is this map actually a set?
5776 * Users should never call this function. Outside of isl,
5777 * the type should indicate whether something is a set or a map.
5779 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5781 if (!map)
5782 return isl_bool_error;
5783 return isl_space_is_set(map->dim);
5786 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5788 int i;
5789 isl_bool is_set;
5790 struct isl_set *set;
5792 is_set = isl_map_is_set(map);
5793 if (is_set < 0)
5794 goto error;
5795 if (is_set)
5796 return set_from_map(map);
5798 map = isl_map_cow(map);
5799 if (!map)
5800 goto error;
5802 set = set_from_map(map);
5803 set->dim = isl_space_range(set->dim);
5804 if (!set->dim)
5805 goto error;
5806 for (i = 0; i < map->n; ++i) {
5807 set->p[i] = isl_basic_map_range(map->p[i]);
5808 if (!set->p[i])
5809 goto error;
5811 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5812 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5813 return set;
5814 error:
5815 isl_map_free(map);
5816 return NULL;
5819 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5821 int i;
5823 map = isl_map_cow(map);
5824 if (!map)
5825 return NULL;
5827 map->dim = isl_space_domain_map(map->dim);
5828 if (!map->dim)
5829 goto error;
5830 for (i = 0; i < map->n; ++i) {
5831 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5832 if (!map->p[i])
5833 goto error;
5835 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5836 map = isl_map_unmark_normalized(map);
5837 return map;
5838 error:
5839 isl_map_free(map);
5840 return NULL;
5843 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5845 int i;
5846 isl_space *range_dim;
5848 map = isl_map_cow(map);
5849 if (!map)
5850 return NULL;
5852 range_dim = isl_space_range(isl_map_get_space(map));
5853 range_dim = isl_space_from_range(range_dim);
5854 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5855 map->dim = isl_space_join(map->dim, range_dim);
5856 if (!map->dim)
5857 goto error;
5858 for (i = 0; i < map->n; ++i) {
5859 map->p[i] = isl_basic_map_range_map(map->p[i]);
5860 if (!map->p[i])
5861 goto error;
5863 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5864 map = isl_map_unmark_normalized(map);
5865 return map;
5866 error:
5867 isl_map_free(map);
5868 return NULL;
5871 /* Given a wrapped map of the form A[B -> C],
5872 * return the map A[B -> C] -> B.
5874 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5876 isl_id *id;
5877 isl_map *map;
5879 if (!set)
5880 return NULL;
5881 if (!isl_set_has_tuple_id(set))
5882 return isl_map_domain_map(isl_set_unwrap(set));
5884 id = isl_set_get_tuple_id(set);
5885 map = isl_map_domain_map(isl_set_unwrap(set));
5886 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5888 return map;
5891 __isl_give isl_basic_map *isl_basic_map_from_domain(
5892 __isl_take isl_basic_set *bset)
5894 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5897 __isl_give isl_basic_map *isl_basic_map_from_range(
5898 __isl_take isl_basic_set *bset)
5900 isl_space *space;
5901 space = isl_basic_set_get_space(bset);
5902 space = isl_space_from_range(space);
5903 bset = isl_basic_set_reset_space(bset, space);
5904 return bset_to_bmap(bset);
5907 /* Create a relation with the given set as range.
5908 * The domain of the created relation is a zero-dimensional
5909 * flat anonymous space.
5911 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5913 isl_space *space;
5914 space = isl_set_get_space(set);
5915 space = isl_space_from_range(space);
5916 set = isl_set_reset_space(set, space);
5917 return set_to_map(set);
5920 /* Create a relation with the given set as domain.
5921 * The range of the created relation is a zero-dimensional
5922 * flat anonymous space.
5924 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5926 return isl_map_reverse(isl_map_from_range(set));
5929 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5930 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5932 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5935 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5936 __isl_take isl_set *range)
5938 return isl_map_apply_range(isl_map_reverse(domain), range);
5941 /* Return a newly allocated isl_map with given space and flags and
5942 * room for "n" basic maps.
5943 * Make sure that all cached information is cleared.
5945 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5946 unsigned flags)
5948 struct isl_map *map;
5950 if (!space)
5951 return NULL;
5952 if (n < 0)
5953 isl_die(space->ctx, isl_error_internal,
5954 "negative number of basic maps", goto error);
5955 map = isl_calloc(space->ctx, struct isl_map,
5956 sizeof(struct isl_map) +
5957 (n - 1) * sizeof(struct isl_basic_map *));
5958 if (!map)
5959 goto error;
5961 map->ctx = space->ctx;
5962 isl_ctx_ref(map->ctx);
5963 map->ref = 1;
5964 map->size = n;
5965 map->n = 0;
5966 map->dim = space;
5967 map->flags = flags;
5968 return map;
5969 error:
5970 isl_space_free(space);
5971 return NULL;
5974 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5976 struct isl_basic_map *bmap;
5977 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5978 bmap = isl_basic_map_set_to_empty(bmap);
5979 return bmap;
5982 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5984 struct isl_basic_set *bset;
5985 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5986 bset = isl_basic_set_set_to_empty(bset);
5987 return bset;
5990 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5992 struct isl_basic_map *bmap;
5993 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5994 bmap = isl_basic_map_finalize(bmap);
5995 return bmap;
5998 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6000 struct isl_basic_set *bset;
6001 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6002 bset = isl_basic_set_finalize(bset);
6003 return bset;
6006 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6007 __isl_take isl_space *space)
6009 int i;
6010 isl_size total = isl_space_dim(space, isl_dim_all);
6011 isl_basic_map *bmap;
6013 if (total < 0)
6014 space = isl_space_free(space);
6015 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6016 for (i = 0; i < total; ++i) {
6017 int k = isl_basic_map_alloc_inequality(bmap);
6018 if (k < 0)
6019 goto error;
6020 isl_seq_clr(bmap->ineq[k], 1 + total);
6021 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6023 return bmap;
6024 error:
6025 isl_basic_map_free(bmap);
6026 return NULL;
6029 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6030 __isl_take isl_space *space)
6032 return isl_basic_map_nat_universe(space);
6035 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6037 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6040 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6042 return isl_map_nat_universe(dim);
6045 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6047 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6050 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6052 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6055 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6057 struct isl_map *map;
6058 if (!space)
6059 return NULL;
6060 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6061 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6062 return map;
6065 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6067 struct isl_set *set;
6068 if (!space)
6069 return NULL;
6070 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6071 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6072 return set;
6075 struct isl_map *isl_map_dup(struct isl_map *map)
6077 int i;
6078 struct isl_map *dup;
6080 if (!map)
6081 return NULL;
6082 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6083 for (i = 0; i < map->n; ++i)
6084 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6085 return dup;
6088 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6089 __isl_take isl_basic_map *bmap)
6091 if (!bmap || !map)
6092 goto error;
6093 if (isl_basic_map_plain_is_empty(bmap)) {
6094 isl_basic_map_free(bmap);
6095 return map;
6097 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6098 isl_assert(map->ctx, map->n < map->size, goto error);
6099 map->p[map->n] = bmap;
6100 map->n++;
6101 map = isl_map_unmark_normalized(map);
6102 return map;
6103 error:
6104 if (map)
6105 isl_map_free(map);
6106 if (bmap)
6107 isl_basic_map_free(bmap);
6108 return NULL;
6111 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6113 int i;
6115 if (!map)
6116 return NULL;
6118 if (--map->ref > 0)
6119 return NULL;
6121 clear_caches(map);
6122 isl_ctx_deref(map->ctx);
6123 for (i = 0; i < map->n; ++i)
6124 isl_basic_map_free(map->p[i]);
6125 isl_space_free(map->dim);
6126 free(map);
6128 return NULL;
6131 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6132 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6134 int j;
6135 isl_size total;
6137 total = isl_basic_map_dim(bmap, isl_dim_all);
6138 if (total < 0)
6139 return isl_basic_map_free(bmap);
6141 bmap = isl_basic_map_cow(bmap);
6142 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6143 j = isl_basic_map_alloc_equality(bmap);
6144 if (j < 0)
6145 goto error;
6146 isl_seq_clr(bmap->eq[j] + 1, total);
6147 isl_int_set_si(bmap->eq[j][pos], -1);
6148 isl_int_set_si(bmap->eq[j][0], value);
6149 bmap = isl_basic_map_simplify(bmap);
6150 return isl_basic_map_finalize(bmap);
6151 error:
6152 isl_basic_map_free(bmap);
6153 return NULL;
6156 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6157 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6159 int j;
6160 isl_size total;
6162 total = isl_basic_map_dim(bmap, isl_dim_all);
6163 if (total < 0)
6164 return isl_basic_map_free(bmap);
6166 bmap = isl_basic_map_cow(bmap);
6167 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6168 j = isl_basic_map_alloc_equality(bmap);
6169 if (j < 0)
6170 goto error;
6171 isl_seq_clr(bmap->eq[j] + 1, total);
6172 isl_int_set_si(bmap->eq[j][pos], -1);
6173 isl_int_set(bmap->eq[j][0], value);
6174 bmap = isl_basic_map_simplify(bmap);
6175 return isl_basic_map_finalize(bmap);
6176 error:
6177 isl_basic_map_free(bmap);
6178 return NULL;
6181 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6182 enum isl_dim_type type, unsigned pos, int value)
6184 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6185 return isl_basic_map_free(bmap);
6186 return isl_basic_map_fix_pos_si(bmap,
6187 isl_basic_map_offset(bmap, type) + pos, value);
6190 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6191 enum isl_dim_type type, unsigned pos, isl_int value)
6193 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6194 return isl_basic_map_free(bmap);
6195 return isl_basic_map_fix_pos(bmap,
6196 isl_basic_map_offset(bmap, type) + pos, value);
6199 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6200 * to be equal to "v".
6202 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6203 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6205 if (!bmap || !v)
6206 goto error;
6207 if (!isl_val_is_int(v))
6208 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6209 "expecting integer value", goto error);
6210 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6211 goto error;
6212 pos += isl_basic_map_offset(bmap, type);
6213 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6214 isl_val_free(v);
6215 return bmap;
6216 error:
6217 isl_basic_map_free(bmap);
6218 isl_val_free(v);
6219 return NULL;
6222 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6223 * to be equal to "v".
6225 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6226 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6228 return isl_basic_map_fix_val(bset, type, pos, v);
6231 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6232 enum isl_dim_type type, unsigned pos, int value)
6234 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6235 type, pos, value));
6238 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6239 enum isl_dim_type type, unsigned pos, isl_int value)
6241 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6242 type, pos, value));
6245 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6246 unsigned input, int value)
6248 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6251 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6252 unsigned dim, int value)
6254 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6255 isl_dim_set, dim, value));
6258 /* Remove the basic map at position "i" from "map" if this basic map
6259 * is (obviously) empty.
6261 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6263 isl_bool empty;
6265 if (!map)
6266 return NULL;
6268 empty = isl_basic_map_plain_is_empty(map->p[i]);
6269 if (empty < 0)
6270 return isl_map_free(map);
6271 if (!empty)
6272 return map;
6274 isl_basic_map_free(map->p[i]);
6275 map->n--;
6276 if (i != map->n) {
6277 map->p[i] = map->p[map->n];
6278 map = isl_map_unmark_normalized(map);
6282 return map;
6285 /* Perform "fn" on each basic map of "map", where we may not be holding
6286 * the only reference to "map".
6287 * In particular, "fn" should be a semantics preserving operation
6288 * that we want to apply to all copies of "map". We therefore need
6289 * to be careful not to modify "map" in a way that breaks "map"
6290 * in case anything goes wrong.
6292 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6293 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6295 struct isl_basic_map *bmap;
6296 int i;
6298 if (!map)
6299 return NULL;
6301 for (i = map->n - 1; i >= 0; --i) {
6302 bmap = isl_basic_map_copy(map->p[i]);
6303 bmap = fn(bmap);
6304 if (!bmap)
6305 goto error;
6306 isl_basic_map_free(map->p[i]);
6307 map->p[i] = bmap;
6308 map = remove_if_empty(map, i);
6309 if (!map)
6310 return NULL;
6313 return map;
6314 error:
6315 isl_map_free(map);
6316 return NULL;
6319 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6320 enum isl_dim_type type, unsigned pos, int value)
6322 int i;
6324 map = isl_map_cow(map);
6325 if (isl_map_check_range(map, type, pos, 1) < 0)
6326 return isl_map_free(map);
6327 for (i = map->n - 1; i >= 0; --i) {
6328 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6329 map = remove_if_empty(map, i);
6330 if (!map)
6331 return NULL;
6333 map = isl_map_unmark_normalized(map);
6334 return map;
6337 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6338 enum isl_dim_type type, unsigned pos, int value)
6340 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6343 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6344 enum isl_dim_type type, unsigned pos, isl_int value)
6346 int i;
6348 map = isl_map_cow(map);
6349 if (isl_map_check_range(map, type, pos, 1) < 0)
6350 return isl_map_free(map);
6351 for (i = 0; i < map->n; ++i) {
6352 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6353 if (!map->p[i])
6354 goto error;
6356 map = isl_map_unmark_normalized(map);
6357 return map;
6358 error:
6359 isl_map_free(map);
6360 return NULL;
6363 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6364 enum isl_dim_type type, unsigned pos, isl_int value)
6366 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6369 /* Fix the value of the variable at position "pos" of type "type" of "map"
6370 * to be equal to "v".
6372 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6373 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6375 int i;
6377 map = isl_map_cow(map);
6378 if (!map || !v)
6379 goto error;
6381 if (!isl_val_is_int(v))
6382 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6383 "expecting integer value", goto error);
6384 if (isl_map_check_range(map, type, pos, 1) < 0)
6385 goto error;
6386 for (i = map->n - 1; i >= 0; --i) {
6387 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6388 isl_val_copy(v));
6389 map = remove_if_empty(map, i);
6390 if (!map)
6391 goto error;
6393 map = isl_map_unmark_normalized(map);
6394 isl_val_free(v);
6395 return map;
6396 error:
6397 isl_map_free(map);
6398 isl_val_free(v);
6399 return NULL;
6402 /* Fix the value of the variable at position "pos" of type "type" of "set"
6403 * to be equal to "v".
6405 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6406 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6408 return isl_map_fix_val(set, type, pos, v);
6411 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6412 unsigned input, int value)
6414 return isl_map_fix_si(map, isl_dim_in, input, value);
6417 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6419 return set_from_map(isl_map_fix_si(set_to_map(set),
6420 isl_dim_set, dim, value));
6423 static __isl_give isl_basic_map *basic_map_bound_si(
6424 __isl_take isl_basic_map *bmap,
6425 enum isl_dim_type type, unsigned pos, int value, int upper)
6427 int j;
6428 isl_size total;
6430 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6431 return isl_basic_map_free(bmap);
6432 total = isl_basic_map_dim(bmap, isl_dim_all);
6433 if (total < 0)
6434 return isl_basic_map_free(bmap);
6435 pos += isl_basic_map_offset(bmap, type);
6436 bmap = isl_basic_map_cow(bmap);
6437 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6438 j = isl_basic_map_alloc_inequality(bmap);
6439 if (j < 0)
6440 goto error;
6441 isl_seq_clr(bmap->ineq[j], 1 + total);
6442 if (upper) {
6443 isl_int_set_si(bmap->ineq[j][pos], -1);
6444 isl_int_set_si(bmap->ineq[j][0], value);
6445 } else {
6446 isl_int_set_si(bmap->ineq[j][pos], 1);
6447 isl_int_set_si(bmap->ineq[j][0], -value);
6449 bmap = isl_basic_map_simplify(bmap);
6450 return isl_basic_map_finalize(bmap);
6451 error:
6452 isl_basic_map_free(bmap);
6453 return NULL;
6456 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6457 __isl_take isl_basic_map *bmap,
6458 enum isl_dim_type type, unsigned pos, int value)
6460 return basic_map_bound_si(bmap, type, pos, value, 0);
6463 /* Constrain the values of the given dimension to be no greater than "value".
6465 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6466 __isl_take isl_basic_map *bmap,
6467 enum isl_dim_type type, unsigned pos, int value)
6469 return basic_map_bound_si(bmap, type, pos, value, 1);
6472 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6473 enum isl_dim_type type, unsigned pos, int value, int upper)
6475 int i;
6477 map = isl_map_cow(map);
6478 if (isl_map_check_range(map, type, pos, 1) < 0)
6479 return isl_map_free(map);
6480 for (i = 0; i < map->n; ++i) {
6481 map->p[i] = basic_map_bound_si(map->p[i],
6482 type, pos, value, upper);
6483 if (!map->p[i])
6484 goto error;
6486 map = isl_map_unmark_normalized(map);
6487 return map;
6488 error:
6489 isl_map_free(map);
6490 return NULL;
6493 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6494 enum isl_dim_type type, unsigned pos, int value)
6496 return map_bound_si(map, type, pos, value, 0);
6499 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6500 enum isl_dim_type type, unsigned pos, int value)
6502 return map_bound_si(map, type, pos, value, 1);
6505 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6506 enum isl_dim_type type, unsigned pos, int value)
6508 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6509 type, pos, value));
6512 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6513 enum isl_dim_type type, unsigned pos, int value)
6515 return isl_map_upper_bound_si(set, type, pos, value);
6518 /* Bound the given variable of "bmap" from below (or above is "upper"
6519 * is set) to "value".
6521 static __isl_give isl_basic_map *basic_map_bound(
6522 __isl_take isl_basic_map *bmap,
6523 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6525 int j;
6526 isl_size total;
6528 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6529 return isl_basic_map_free(bmap);
6530 total = isl_basic_map_dim(bmap, isl_dim_all);
6531 if (total < 0)
6532 return isl_basic_map_free(bmap);
6533 pos += isl_basic_map_offset(bmap, type);
6534 bmap = isl_basic_map_cow(bmap);
6535 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6536 j = isl_basic_map_alloc_inequality(bmap);
6537 if (j < 0)
6538 goto error;
6539 isl_seq_clr(bmap->ineq[j], 1 + total);
6540 if (upper) {
6541 isl_int_set_si(bmap->ineq[j][pos], -1);
6542 isl_int_set(bmap->ineq[j][0], value);
6543 } else {
6544 isl_int_set_si(bmap->ineq[j][pos], 1);
6545 isl_int_neg(bmap->ineq[j][0], value);
6547 bmap = isl_basic_map_simplify(bmap);
6548 return isl_basic_map_finalize(bmap);
6549 error:
6550 isl_basic_map_free(bmap);
6551 return NULL;
6554 /* Bound the given variable of "map" from below (or above is "upper"
6555 * is set) to "value".
6557 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6558 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6560 int i;
6562 map = isl_map_cow(map);
6563 if (isl_map_check_range(map, type, pos, 1) < 0)
6564 return isl_map_free(map);
6565 for (i = map->n - 1; i >= 0; --i) {
6566 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6567 map = remove_if_empty(map, i);
6568 if (!map)
6569 return NULL;
6571 map = isl_map_unmark_normalized(map);
6572 return map;
6575 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6576 enum isl_dim_type type, unsigned pos, isl_int value)
6578 return map_bound(map, type, pos, value, 0);
6581 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6582 enum isl_dim_type type, unsigned pos, isl_int value)
6584 return map_bound(map, type, pos, value, 1);
6587 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6588 enum isl_dim_type type, unsigned pos, isl_int value)
6590 return isl_map_lower_bound(set, type, pos, value);
6593 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6594 enum isl_dim_type type, unsigned pos, isl_int value)
6596 return isl_map_upper_bound(set, type, pos, value);
6599 /* Force the values of the variable at position "pos" of type "type" of "set"
6600 * to be no smaller than "value".
6602 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6603 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6605 if (!value)
6606 goto error;
6607 if (!isl_val_is_int(value))
6608 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6609 "expecting integer value", goto error);
6610 set = isl_set_lower_bound(set, type, pos, value->n);
6611 isl_val_free(value);
6612 return set;
6613 error:
6614 isl_val_free(value);
6615 isl_set_free(set);
6616 return NULL;
6619 /* Force the values of the variable at position "pos" of type "type" of "set"
6620 * to be no greater than "value".
6622 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6623 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6625 if (!value)
6626 goto error;
6627 if (!isl_val_is_int(value))
6628 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6629 "expecting integer value", goto error);
6630 set = isl_set_upper_bound(set, type, pos, value->n);
6631 isl_val_free(value);
6632 return set;
6633 error:
6634 isl_val_free(value);
6635 isl_set_free(set);
6636 return NULL;
6639 /* Bound the given variable of "bset" from below (or above is "upper"
6640 * is set) to "value".
6642 static __isl_give isl_basic_set *isl_basic_set_bound(
6643 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6644 isl_int value, int upper)
6646 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6647 type, pos, value, upper));
6650 /* Bound the given variable of "bset" from below (or above is "upper"
6651 * is set) to "value".
6653 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6654 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6655 __isl_take isl_val *value, int upper)
6657 if (!value)
6658 goto error;
6659 if (!isl_val_is_int(value))
6660 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6661 "expecting integer value", goto error);
6662 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6663 isl_val_free(value);
6664 return bset;
6665 error:
6666 isl_val_free(value);
6667 isl_basic_set_free(bset);
6668 return NULL;
6671 /* Bound the given variable of "bset" from below to "value".
6673 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6674 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6675 __isl_take isl_val *value)
6677 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6680 /* Bound the given variable of "bset" from above to "value".
6682 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6683 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6684 __isl_take isl_val *value)
6686 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6689 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6691 int i;
6693 map = isl_map_cow(map);
6694 if (!map)
6695 return NULL;
6697 map->dim = isl_space_reverse(map->dim);
6698 if (!map->dim)
6699 goto error;
6700 for (i = 0; i < map->n; ++i) {
6701 map->p[i] = isl_basic_map_reverse(map->p[i]);
6702 if (!map->p[i])
6703 goto error;
6705 map = isl_map_unmark_normalized(map);
6706 return map;
6707 error:
6708 isl_map_free(map);
6709 return NULL;
6712 #undef TYPE
6713 #define TYPE isl_pw_multi_aff
6714 #undef SUFFIX
6715 #define SUFFIX _pw_multi_aff
6716 #undef EMPTY
6717 #define EMPTY isl_pw_multi_aff_empty
6718 #undef ADD
6719 #define ADD isl_pw_multi_aff_union_add
6720 #include "isl_map_lexopt_templ.c"
6722 /* Given a map "map", compute the lexicographically minimal
6723 * (or maximal) image element for each domain element in dom,
6724 * in the form of an isl_pw_multi_aff.
6725 * If "empty" is not NULL, then set *empty to those elements in dom that
6726 * do not have an image element.
6727 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6728 * should be computed over the domain of "map". "empty" is also NULL
6729 * in this case.
6731 * We first compute the lexicographically minimal or maximal element
6732 * in the first basic map. This results in a partial solution "res"
6733 * and a subset "todo" of dom that still need to be handled.
6734 * We then consider each of the remaining maps in "map" and successively
6735 * update both "res" and "todo".
6736 * If "empty" is NULL, then the todo sets are not needed and therefore
6737 * also not computed.
6739 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6740 __isl_take isl_map *map, __isl_take isl_set *dom,
6741 __isl_give isl_set **empty, unsigned flags)
6743 int i;
6744 int full;
6745 isl_pw_multi_aff *res;
6746 isl_set *todo;
6748 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6749 if (!map || (!full && !dom))
6750 goto error;
6752 if (isl_map_plain_is_empty(map)) {
6753 if (empty)
6754 *empty = dom;
6755 else
6756 isl_set_free(dom);
6757 return isl_pw_multi_aff_from_map(map);
6760 res = basic_map_partial_lexopt_pw_multi_aff(
6761 isl_basic_map_copy(map->p[0]),
6762 isl_set_copy(dom), empty, flags);
6764 if (empty)
6765 todo = *empty;
6766 for (i = 1; i < map->n; ++i) {
6767 isl_pw_multi_aff *res_i;
6769 res_i = basic_map_partial_lexopt_pw_multi_aff(
6770 isl_basic_map_copy(map->p[i]),
6771 isl_set_copy(dom), empty, flags);
6773 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6774 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6775 else
6776 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6778 if (empty)
6779 todo = isl_set_intersect(todo, *empty);
6782 isl_set_free(dom);
6783 isl_map_free(map);
6785 if (empty)
6786 *empty = todo;
6788 return res;
6789 error:
6790 if (empty)
6791 *empty = NULL;
6792 isl_set_free(dom);
6793 isl_map_free(map);
6794 return NULL;
6797 #undef TYPE
6798 #define TYPE isl_map
6799 #undef SUFFIX
6800 #define SUFFIX
6801 #undef EMPTY
6802 #define EMPTY isl_map_empty
6803 #undef ADD
6804 #define ADD isl_map_union_disjoint
6805 #include "isl_map_lexopt_templ.c"
6807 /* Given a map "map", compute the lexicographically minimal
6808 * (or maximal) image element for each domain element in "dom",
6809 * in the form of an isl_map.
6810 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6811 * do not have an image element.
6812 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6813 * should be computed over the domain of "map". "empty" is also NULL
6814 * in this case.
6816 * If the input consists of more than one disjunct, then first
6817 * compute the desired result in the form of an isl_pw_multi_aff and
6818 * then convert that into an isl_map.
6820 * This function used to have an explicit implementation in terms
6821 * of isl_maps, but it would continually intersect the domains of
6822 * partial results with the complement of the domain of the next
6823 * partial solution, potentially leading to an explosion in the number
6824 * of disjuncts if there are several disjuncts in the input.
6825 * An even earlier implementation of this function would look for
6826 * better results in the domain of the partial result and for extra
6827 * results in the complement of this domain, which would lead to
6828 * even more splintering.
6830 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6831 __isl_take isl_map *map, __isl_take isl_set *dom,
6832 __isl_give isl_set **empty, unsigned flags)
6834 int full;
6835 struct isl_map *res;
6836 isl_pw_multi_aff *pma;
6838 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6839 if (!map || (!full && !dom))
6840 goto error;
6842 if (isl_map_plain_is_empty(map)) {
6843 if (empty)
6844 *empty = dom;
6845 else
6846 isl_set_free(dom);
6847 return map;
6850 if (map->n == 1) {
6851 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6852 dom, empty, flags);
6853 isl_map_free(map);
6854 return res;
6857 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6858 flags);
6859 return isl_map_from_pw_multi_aff(pma);
6860 error:
6861 if (empty)
6862 *empty = NULL;
6863 isl_set_free(dom);
6864 isl_map_free(map);
6865 return NULL;
6868 __isl_give isl_map *isl_map_partial_lexmax(
6869 __isl_take isl_map *map, __isl_take isl_set *dom,
6870 __isl_give isl_set **empty)
6872 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6875 __isl_give isl_map *isl_map_partial_lexmin(
6876 __isl_take isl_map *map, __isl_take isl_set *dom,
6877 __isl_give isl_set **empty)
6879 return isl_map_partial_lexopt(map, dom, empty, 0);
6882 __isl_give isl_set *isl_set_partial_lexmin(
6883 __isl_take isl_set *set, __isl_take isl_set *dom,
6884 __isl_give isl_set **empty)
6886 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6887 dom, empty));
6890 __isl_give isl_set *isl_set_partial_lexmax(
6891 __isl_take isl_set *set, __isl_take isl_set *dom,
6892 __isl_give isl_set **empty)
6894 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6895 dom, empty));
6898 /* Compute the lexicographic minimum (or maximum if "flags" includes
6899 * ISL_OPT_MAX) of "bset" over its parametric domain.
6901 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6902 unsigned flags)
6904 return isl_basic_map_lexopt(bset, flags);
6907 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6909 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6912 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6914 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6917 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6919 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6922 /* Compute the lexicographic minimum of "bset" over its parametric domain
6923 * for the purpose of quantifier elimination.
6924 * That is, find an explicit representation for all the existentially
6925 * quantified variables in "bset" by computing their lexicographic
6926 * minimum.
6928 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6929 __isl_take isl_basic_set *bset)
6931 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6934 /* Given a basic map with one output dimension, compute the minimum or
6935 * maximum of that dimension as an isl_pw_aff.
6937 * Compute the optimum as a lexicographic optimum over the single
6938 * output dimension and extract the single isl_pw_aff from the result.
6940 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6941 int max)
6943 isl_pw_multi_aff *pma;
6944 isl_pw_aff *pwaff;
6946 bmap = isl_basic_map_copy(bmap);
6947 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6948 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6949 isl_pw_multi_aff_free(pma);
6951 return pwaff;
6954 /* Compute the minimum or maximum of the given output dimension
6955 * as a function of the parameters and the input dimensions,
6956 * but independently of the other output dimensions.
6958 * We first project out the other output dimension and then compute
6959 * the "lexicographic" maximum in each basic map, combining the results
6960 * using isl_pw_aff_union_max.
6962 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6963 int max)
6965 int i;
6966 isl_pw_aff *pwaff;
6967 isl_size n_out;
6969 n_out = isl_map_dim(map, isl_dim_out);
6970 if (n_out < 0)
6971 map = isl_map_free(map);
6972 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6973 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6974 if (!map)
6975 return NULL;
6977 if (map->n == 0) {
6978 isl_space *space = isl_map_get_space(map);
6979 isl_map_free(map);
6980 return isl_pw_aff_empty(space);
6983 pwaff = basic_map_dim_opt(map->p[0], max);
6984 for (i = 1; i < map->n; ++i) {
6985 isl_pw_aff *pwaff_i;
6987 pwaff_i = basic_map_dim_opt(map->p[i], max);
6988 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6991 isl_map_free(map);
6993 return pwaff;
6996 /* Compute the minimum of the given output dimension as a function of the
6997 * parameters and input dimensions, but independently of
6998 * the other output dimensions.
7000 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7002 return map_dim_opt(map, pos, 0);
7005 /* Compute the maximum of the given output dimension as a function of the
7006 * parameters and input dimensions, but independently of
7007 * the other output dimensions.
7009 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7011 return map_dim_opt(map, pos, 1);
7014 /* Compute the minimum or maximum of the given set dimension
7015 * as a function of the parameters,
7016 * but independently of the other set dimensions.
7018 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7019 int max)
7021 return map_dim_opt(set, pos, max);
7024 /* Compute the maximum of the given set dimension as a function of the
7025 * parameters, but independently of the other set dimensions.
7027 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7029 return set_dim_opt(set, pos, 1);
7032 /* Compute the minimum of the given set dimension as a function of the
7033 * parameters, but independently of the other set dimensions.
7035 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7037 return set_dim_opt(set, pos, 0);
7040 /* Apply a preimage specified by "mat" on the parameters of "bset".
7041 * bset is assumed to have only parameters and divs.
7043 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7044 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7046 isl_size nparam;
7048 nparam = isl_basic_set_dim(bset, isl_dim_param);
7049 if (nparam < 0 || !mat)
7050 goto error;
7052 bset->dim = isl_space_cow(bset->dim);
7053 if (!bset->dim)
7054 goto error;
7056 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7058 bset->dim->nparam = 0;
7059 bset->dim->n_out = nparam;
7060 bset = isl_basic_set_preimage(bset, mat);
7061 if (bset) {
7062 bset->dim->nparam = bset->dim->n_out;
7063 bset->dim->n_out = 0;
7065 return bset;
7066 error:
7067 isl_mat_free(mat);
7068 isl_basic_set_free(bset);
7069 return NULL;
7072 /* Apply a preimage specified by "mat" on the parameters of "set".
7073 * set is assumed to have only parameters and divs.
7075 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7076 __isl_take isl_mat *mat)
7078 isl_space *space;
7079 isl_size nparam;
7081 nparam = isl_set_dim(set, isl_dim_param);
7082 if (nparam < 0 || !mat)
7083 goto error;
7085 if (mat->n_row != 1 + nparam)
7086 isl_die(isl_set_get_ctx(set), isl_error_internal,
7087 "unexpected number of rows", goto error);
7089 space = isl_set_get_space(set);
7090 space = isl_space_move_dims(space, isl_dim_set, 0,
7091 isl_dim_param, 0, nparam);
7092 set = isl_set_reset_space(set, space);
7093 set = isl_set_preimage(set, mat);
7094 nparam = isl_set_dim(set, isl_dim_out);
7095 if (nparam < 0)
7096 set = isl_set_free(set);
7097 space = isl_set_get_space(set);
7098 space = isl_space_move_dims(space, isl_dim_param, 0,
7099 isl_dim_out, 0, nparam);
7100 set = isl_set_reset_space(set, space);
7101 return set;
7102 error:
7103 isl_mat_free(mat);
7104 isl_set_free(set);
7105 return NULL;
7108 /* Intersect the basic set "bset" with the affine space specified by the
7109 * equalities in "eq".
7111 static __isl_give isl_basic_set *basic_set_append_equalities(
7112 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7114 int i, k;
7115 unsigned len;
7117 if (!bset || !eq)
7118 goto error;
7120 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7121 eq->n_row, 0);
7122 if (!bset)
7123 goto error;
7125 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7126 for (i = 0; i < eq->n_row; ++i) {
7127 k = isl_basic_set_alloc_equality(bset);
7128 if (k < 0)
7129 goto error;
7130 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7131 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7133 isl_mat_free(eq);
7135 bset = isl_basic_set_gauss(bset, NULL);
7136 bset = isl_basic_set_finalize(bset);
7138 return bset;
7139 error:
7140 isl_mat_free(eq);
7141 isl_basic_set_free(bset);
7142 return NULL;
7145 /* Intersect the set "set" with the affine space specified by the
7146 * equalities in "eq".
7148 static struct isl_set *set_append_equalities(struct isl_set *set,
7149 struct isl_mat *eq)
7151 int i;
7153 if (!set || !eq)
7154 goto error;
7156 for (i = 0; i < set->n; ++i) {
7157 set->p[i] = basic_set_append_equalities(set->p[i],
7158 isl_mat_copy(eq));
7159 if (!set->p[i])
7160 goto error;
7162 isl_mat_free(eq);
7163 return set;
7164 error:
7165 isl_mat_free(eq);
7166 isl_set_free(set);
7167 return NULL;
7170 /* Given a basic set "bset" that only involves parameters and existentially
7171 * quantified variables, return the index of the first equality
7172 * that only involves parameters. If there is no such equality then
7173 * return bset->n_eq.
7175 * This function assumes that isl_basic_set_gauss has been called on "bset".
7177 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7179 int i, j;
7180 isl_size nparam, n_div;
7182 nparam = isl_basic_set_dim(bset, isl_dim_param);
7183 n_div = isl_basic_set_dim(bset, isl_dim_div);
7184 if (nparam < 0 || n_div < 0)
7185 return -1;
7187 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7188 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7189 ++i;
7192 return i;
7195 /* Compute an explicit representation for the existentially quantified
7196 * variables in "bset" by computing the "minimal value" of the set
7197 * variables. Since there are no set variables, the computation of
7198 * the minimal value essentially computes an explicit representation
7199 * of the non-empty part(s) of "bset".
7201 * The input only involves parameters and existentially quantified variables.
7202 * All equalities among parameters have been removed.
7204 * Since the existentially quantified variables in the result are in general
7205 * going to be different from those in the input, we first replace
7206 * them by the minimal number of variables based on their equalities.
7207 * This should simplify the parametric integer programming.
7209 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7211 isl_morph *morph1, *morph2;
7212 isl_set *set;
7213 isl_size n;
7215 if (!bset)
7216 return NULL;
7217 if (bset->n_eq == 0)
7218 return isl_basic_set_lexmin_compute_divs(bset);
7220 morph1 = isl_basic_set_parameter_compression(bset);
7221 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7222 bset = isl_basic_set_lift(bset);
7223 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7224 bset = isl_morph_basic_set(morph2, bset);
7225 n = isl_basic_set_dim(bset, isl_dim_set);
7226 if (n < 0)
7227 bset = isl_basic_set_free(bset);
7228 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7230 set = isl_basic_set_lexmin_compute_divs(bset);
7232 set = isl_morph_set(isl_morph_inverse(morph1), set);
7234 return set;
7237 /* Project the given basic set onto its parameter domain, possibly introducing
7238 * new, explicit, existential variables in the constraints.
7239 * The input has parameters and (possibly implicit) existential variables.
7240 * The output has the same parameters, but only
7241 * explicit existentially quantified variables.
7243 * The actual projection is performed by pip, but pip doesn't seem
7244 * to like equalities very much, so we first remove the equalities
7245 * among the parameters by performing a variable compression on
7246 * the parameters. Afterward, an inverse transformation is performed
7247 * and the equalities among the parameters are inserted back in.
7249 * The variable compression on the parameters may uncover additional
7250 * equalities that were only implicit before. We therefore check
7251 * if there are any new parameter equalities in the result and
7252 * if so recurse. The removal of parameter equalities is required
7253 * for the parameter compression performed by base_compute_divs.
7255 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7257 int i;
7258 struct isl_mat *eq;
7259 struct isl_mat *T, *T2;
7260 struct isl_set *set;
7261 isl_size nparam;
7263 bset = isl_basic_set_cow(bset);
7264 if (!bset)
7265 return NULL;
7267 if (bset->n_eq == 0)
7268 return base_compute_divs(bset);
7270 bset = isl_basic_set_gauss(bset, NULL);
7271 if (!bset)
7272 return NULL;
7273 if (isl_basic_set_plain_is_empty(bset))
7274 return isl_set_from_basic_set(bset);
7276 i = first_parameter_equality(bset);
7277 if (i == bset->n_eq)
7278 return base_compute_divs(bset);
7280 nparam = isl_basic_set_dim(bset, isl_dim_param);
7281 if (nparam < 0)
7282 return isl_set_from_basic_set(isl_basic_set_free(bset));
7283 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7284 0, 1 + nparam);
7285 eq = isl_mat_cow(eq);
7286 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7287 if (T && T->n_col == 0) {
7288 isl_mat_free(T);
7289 isl_mat_free(T2);
7290 isl_mat_free(eq);
7291 bset = isl_basic_set_set_to_empty(bset);
7292 return isl_set_from_basic_set(bset);
7294 bset = basic_set_parameter_preimage(bset, T);
7296 i = first_parameter_equality(bset);
7297 if (!bset)
7298 set = NULL;
7299 else if (i == bset->n_eq)
7300 set = base_compute_divs(bset);
7301 else
7302 set = parameter_compute_divs(bset);
7303 set = set_parameter_preimage(set, T2);
7304 set = set_append_equalities(set, eq);
7305 return set;
7308 /* Insert the divs from "ls" before those of "bmap".
7310 * The number of columns is not changed, which means that the last
7311 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7312 * The caller is responsible for removing the same number of dimensions
7313 * from the space of "bmap".
7315 static __isl_give isl_basic_map *insert_divs_from_local_space(
7316 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7318 int i;
7319 isl_size n_div;
7320 int old_n_div;
7322 n_div = isl_local_space_dim(ls, isl_dim_div);
7323 if (n_div < 0)
7324 return isl_basic_map_free(bmap);
7325 if (n_div == 0)
7326 return bmap;
7328 old_n_div = bmap->n_div;
7329 bmap = insert_div_rows(bmap, n_div);
7330 if (!bmap)
7331 return NULL;
7333 for (i = 0; i < n_div; ++i) {
7334 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7335 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7338 return bmap;
7341 /* Replace the space of "bmap" by the space and divs of "ls".
7343 * If "ls" has any divs, then we simplify the result since we may
7344 * have discovered some additional equalities that could simplify
7345 * the div expressions.
7347 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7348 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7350 isl_size n_div;
7352 bmap = isl_basic_map_cow(bmap);
7353 n_div = isl_local_space_dim(ls, isl_dim_div);
7354 if (!bmap || n_div < 0)
7355 goto error;
7357 bmap = insert_divs_from_local_space(bmap, ls);
7358 if (!bmap)
7359 goto error;
7361 isl_space_free(bmap->dim);
7362 bmap->dim = isl_local_space_get_space(ls);
7363 if (!bmap->dim)
7364 goto error;
7366 isl_local_space_free(ls);
7367 if (n_div > 0)
7368 bmap = isl_basic_map_simplify(bmap);
7369 bmap = isl_basic_map_finalize(bmap);
7370 return bmap;
7371 error:
7372 isl_basic_map_free(bmap);
7373 isl_local_space_free(ls);
7374 return NULL;
7377 /* Replace the space of "map" by the space and divs of "ls".
7379 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7380 __isl_take isl_local_space *ls)
7382 int i;
7384 map = isl_map_cow(map);
7385 if (!map || !ls)
7386 goto error;
7388 for (i = 0; i < map->n; ++i) {
7389 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7390 isl_local_space_copy(ls));
7391 if (!map->p[i])
7392 goto error;
7394 isl_space_free(map->dim);
7395 map->dim = isl_local_space_get_space(ls);
7396 if (!map->dim)
7397 goto error;
7399 isl_local_space_free(ls);
7400 return map;
7401 error:
7402 isl_local_space_free(ls);
7403 isl_map_free(map);
7404 return NULL;
7407 /* Compute an explicit representation for the existentially
7408 * quantified variables for which do not know any explicit representation yet.
7410 * We first sort the existentially quantified variables so that the
7411 * existentially quantified variables for which we already have an explicit
7412 * representation are placed before those for which we do not.
7413 * The input dimensions, the output dimensions and the existentially
7414 * quantified variables for which we already have an explicit
7415 * representation are then turned into parameters.
7416 * compute_divs returns a map with the same parameters and
7417 * no input or output dimensions and the dimension specification
7418 * is reset to that of the input, including the existentially quantified
7419 * variables for which we already had an explicit representation.
7421 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7423 struct isl_basic_set *bset;
7424 struct isl_set *set;
7425 struct isl_map *map;
7426 isl_space *space;
7427 isl_local_space *ls;
7428 isl_size nparam;
7429 isl_size n_in;
7430 isl_size n_out;
7431 int n_known;
7432 int i;
7434 bmap = isl_basic_map_sort_divs(bmap);
7435 bmap = isl_basic_map_cow(bmap);
7436 if (!bmap)
7437 return NULL;
7439 n_known = isl_basic_map_first_unknown_div(bmap);
7440 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7441 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7442 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7443 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7444 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7446 space = isl_space_set_alloc(bmap->ctx,
7447 nparam + n_in + n_out + n_known, 0);
7448 if (!space)
7449 goto error;
7451 ls = isl_basic_map_get_local_space(bmap);
7452 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7453 n_known, bmap->n_div - n_known);
7454 if (n_known > 0) {
7455 for (i = n_known; i < bmap->n_div; ++i)
7456 swap_div(bmap, i - n_known, i);
7457 bmap->n_div -= n_known;
7458 bmap->extra -= n_known;
7460 bmap = isl_basic_map_reset_space(bmap, space);
7461 bset = bset_from_bmap(bmap);
7463 set = parameter_compute_divs(bset);
7464 map = set_to_map(set);
7465 map = replace_space_by_local_space(map, ls);
7467 return map;
7468 error:
7469 isl_basic_map_free(bmap);
7470 return NULL;
7473 /* Remove the explicit representation of local variable "div",
7474 * if there is any.
7476 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7477 __isl_take isl_basic_map *bmap, int div)
7479 isl_bool unknown;
7481 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7482 if (unknown < 0)
7483 return isl_basic_map_free(bmap);
7484 if (unknown)
7485 return bmap;
7487 bmap = isl_basic_map_cow(bmap);
7488 if (!bmap)
7489 return NULL;
7490 isl_int_set_si(bmap->div[div][0], 0);
7491 return bmap;
7494 /* Is local variable "div" of "bmap" marked as not having an explicit
7495 * representation?
7496 * Note that even if "div" is not marked in this way and therefore
7497 * has an explicit representation, this representation may still
7498 * depend (indirectly) on other local variables that do not
7499 * have an explicit representation.
7501 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7502 int div)
7504 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7505 return isl_bool_error;
7506 return isl_int_is_zero(bmap->div[div][0]);
7509 /* Return the position of the first local variable that does not
7510 * have an explicit representation.
7511 * Return the total number of local variables if they all have
7512 * an explicit representation.
7513 * Return -1 on error.
7515 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7517 int i;
7519 if (!bmap)
7520 return -1;
7522 for (i = 0; i < bmap->n_div; ++i) {
7523 if (!isl_basic_map_div_is_known(bmap, i))
7524 return i;
7526 return bmap->n_div;
7529 /* Return the position of the first local variable that does not
7530 * have an explicit representation.
7531 * Return the total number of local variables if they all have
7532 * an explicit representation.
7533 * Return -1 on error.
7535 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7537 return isl_basic_map_first_unknown_div(bset);
7540 /* Does "bmap" have an explicit representation for all local variables?
7542 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7544 int first;
7545 isl_size n;
7547 n = isl_basic_map_dim(bmap, isl_dim_div);
7548 first = isl_basic_map_first_unknown_div(bmap);
7549 if (n < 0 || first < 0)
7550 return isl_bool_error;
7551 return first == n;
7554 /* Do all basic maps in "map" have an explicit representation
7555 * for all local variables?
7557 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7559 int i;
7561 if (!map)
7562 return isl_bool_error;
7564 for (i = 0; i < map->n; ++i) {
7565 int known = isl_basic_map_divs_known(map->p[i]);
7566 if (known <= 0)
7567 return known;
7570 return isl_bool_true;
7573 /* If bmap contains any unknown divs, then compute explicit
7574 * expressions for them. However, this computation may be
7575 * quite expensive, so first try to remove divs that aren't
7576 * strictly needed.
7578 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7580 int known;
7581 struct isl_map *map;
7583 known = isl_basic_map_divs_known(bmap);
7584 if (known < 0)
7585 goto error;
7586 if (known)
7587 return isl_map_from_basic_map(bmap);
7589 bmap = isl_basic_map_drop_redundant_divs(bmap);
7591 known = isl_basic_map_divs_known(bmap);
7592 if (known < 0)
7593 goto error;
7594 if (known)
7595 return isl_map_from_basic_map(bmap);
7597 map = compute_divs(bmap);
7598 return map;
7599 error:
7600 isl_basic_map_free(bmap);
7601 return NULL;
7604 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7606 int i;
7607 int known;
7608 struct isl_map *res;
7610 if (!map)
7611 return NULL;
7612 if (map->n == 0)
7613 return map;
7615 known = isl_map_divs_known(map);
7616 if (known < 0) {
7617 isl_map_free(map);
7618 return NULL;
7620 if (known)
7621 return map;
7623 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7624 for (i = 1 ; i < map->n; ++i) {
7625 struct isl_map *r2;
7626 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7627 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7628 res = isl_map_union_disjoint(res, r2);
7629 else
7630 res = isl_map_union(res, r2);
7632 isl_map_free(map);
7634 return res;
7637 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7639 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7642 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7644 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7647 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7649 int i;
7650 struct isl_set *set;
7652 if (!map)
7653 goto error;
7655 map = isl_map_cow(map);
7656 if (!map)
7657 return NULL;
7659 set = set_from_map(map);
7660 set->dim = isl_space_domain(set->dim);
7661 if (!set->dim)
7662 goto error;
7663 for (i = 0; i < map->n; ++i) {
7664 set->p[i] = isl_basic_map_domain(map->p[i]);
7665 if (!set->p[i])
7666 goto error;
7668 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7669 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7670 return set;
7671 error:
7672 isl_map_free(map);
7673 return NULL;
7676 /* Return the union of "map1" and "map2", where we assume for now that
7677 * "map1" and "map2" are disjoint. Note that the basic maps inside
7678 * "map1" or "map2" may not be disjoint from each other.
7679 * Also note that this function is also called from isl_map_union,
7680 * which takes care of handling the situation where "map1" and "map2"
7681 * may not be disjoint.
7683 * If one of the inputs is empty, we can simply return the other input.
7684 * Similarly, if one of the inputs is universal, then it is equal to the union.
7686 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7687 __isl_take isl_map *map2)
7689 int i;
7690 unsigned flags = 0;
7691 struct isl_map *map = NULL;
7692 int is_universe;
7694 if (!map1 || !map2)
7695 goto error;
7697 if (!isl_space_is_equal(map1->dim, map2->dim))
7698 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7699 "spaces don't match", goto error);
7701 if (map1->n == 0) {
7702 isl_map_free(map1);
7703 return map2;
7705 if (map2->n == 0) {
7706 isl_map_free(map2);
7707 return map1;
7710 is_universe = isl_map_plain_is_universe(map1);
7711 if (is_universe < 0)
7712 goto error;
7713 if (is_universe) {
7714 isl_map_free(map2);
7715 return map1;
7718 is_universe = isl_map_plain_is_universe(map2);
7719 if (is_universe < 0)
7720 goto error;
7721 if (is_universe) {
7722 isl_map_free(map1);
7723 return map2;
7726 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7727 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7728 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7730 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7731 map1->n + map2->n, flags);
7732 if (!map)
7733 goto error;
7734 for (i = 0; i < map1->n; ++i) {
7735 map = isl_map_add_basic_map(map,
7736 isl_basic_map_copy(map1->p[i]));
7737 if (!map)
7738 goto error;
7740 for (i = 0; i < map2->n; ++i) {
7741 map = isl_map_add_basic_map(map,
7742 isl_basic_map_copy(map2->p[i]));
7743 if (!map)
7744 goto error;
7746 isl_map_free(map1);
7747 isl_map_free(map2);
7748 return map;
7749 error:
7750 isl_map_free(map);
7751 isl_map_free(map1);
7752 isl_map_free(map2);
7753 return NULL;
7756 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7757 * guaranteed to be disjoint by the caller.
7759 * Note that this functions is called from within isl_map_make_disjoint,
7760 * so we have to be careful not to touch the constraints of the inputs
7761 * in any way.
7763 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7764 __isl_take isl_map *map2)
7766 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7769 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7770 * not be disjoint. The parameters are assumed to have been aligned.
7772 * We currently simply call map_union_disjoint, the internal operation
7773 * of which does not really depend on the inputs being disjoint.
7774 * If the result contains more than one basic map, then we clear
7775 * the disjoint flag since the result may contain basic maps from
7776 * both inputs and these are not guaranteed to be disjoint.
7778 * As a special case, if "map1" and "map2" are obviously equal,
7779 * then we simply return "map1".
7781 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7782 __isl_take isl_map *map2)
7784 int equal;
7786 if (!map1 || !map2)
7787 goto error;
7789 equal = isl_map_plain_is_equal(map1, map2);
7790 if (equal < 0)
7791 goto error;
7792 if (equal) {
7793 isl_map_free(map2);
7794 return map1;
7797 map1 = map_union_disjoint(map1, map2);
7798 if (!map1)
7799 return NULL;
7800 if (map1->n > 1)
7801 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7802 return map1;
7803 error:
7804 isl_map_free(map1);
7805 isl_map_free(map2);
7806 return NULL;
7809 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7810 * not be disjoint.
7812 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7813 __isl_take isl_map *map2)
7815 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7818 __isl_give isl_set *isl_set_union_disjoint(
7819 __isl_take isl_set *set1, __isl_take isl_set *set2)
7821 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7822 set_to_map(set2)));
7825 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7827 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7830 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7831 * the results.
7833 * "map" and "set" are assumed to be compatible and non-NULL.
7835 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7836 __isl_take isl_set *set,
7837 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7838 __isl_take isl_basic_set *bset))
7840 unsigned flags = 0;
7841 struct isl_map *result;
7842 int i, j;
7844 if (isl_set_plain_is_universe(set)) {
7845 isl_set_free(set);
7846 return map;
7849 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7850 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7851 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7853 result = isl_map_alloc_space(isl_space_copy(map->dim),
7854 map->n * set->n, flags);
7855 for (i = 0; result && i < map->n; ++i)
7856 for (j = 0; j < set->n; ++j) {
7857 result = isl_map_add_basic_map(result,
7858 fn(isl_basic_map_copy(map->p[i]),
7859 isl_basic_set_copy(set->p[j])));
7860 if (!result)
7861 break;
7864 isl_map_free(map);
7865 isl_set_free(set);
7866 return result;
7869 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7870 __isl_take isl_set *set)
7872 isl_bool ok;
7874 ok = isl_map_compatible_range(map, set);
7875 if (ok < 0)
7876 goto error;
7877 if (!ok)
7878 isl_die(set->ctx, isl_error_invalid,
7879 "incompatible spaces", goto error);
7881 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7882 error:
7883 isl_map_free(map);
7884 isl_set_free(set);
7885 return NULL;
7888 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7889 __isl_take isl_set *set)
7891 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7894 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7895 __isl_take isl_set *set)
7897 isl_bool ok;
7899 ok = isl_map_compatible_domain(map, set);
7900 if (ok < 0)
7901 goto error;
7902 if (!ok)
7903 isl_die(set->ctx, isl_error_invalid,
7904 "incompatible spaces", goto error);
7906 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7907 error:
7908 isl_map_free(map);
7909 isl_set_free(set);
7910 return NULL;
7913 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7914 __isl_take isl_set *set)
7916 return isl_map_align_params_map_map_and(map, set,
7917 &map_intersect_domain);
7920 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7921 * in the space B -> C, return the intersection.
7922 * The parameters are assumed to have been aligned.
7924 * The map "factor" is first extended to a map living in the space
7925 * [A -> B] -> C and then a regular intersection is computed.
7927 static __isl_give isl_map *map_intersect_domain_factor_range(
7928 __isl_take isl_map *map, __isl_take isl_map *factor)
7930 isl_space *space;
7931 isl_map *ext_factor;
7933 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7934 ext_factor = isl_map_universe(space);
7935 ext_factor = isl_map_domain_product(ext_factor, factor);
7936 return map_intersect(map, ext_factor);
7939 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7940 * in the space B -> C, return the intersection.
7942 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7943 __isl_take isl_map *map, __isl_take isl_map *factor)
7945 return isl_map_align_params_map_map_and(map, factor,
7946 &map_intersect_domain_factor_range);
7949 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7950 * in the space A -> C, return the intersection.
7952 * The map "factor" is first extended to a map living in the space
7953 * A -> [B -> C] and then a regular intersection is computed.
7955 static __isl_give isl_map *map_intersect_range_factor_range(
7956 __isl_take isl_map *map, __isl_take isl_map *factor)
7958 isl_space *space;
7959 isl_map *ext_factor;
7961 space = isl_space_range_factor_domain(isl_map_get_space(map));
7962 ext_factor = isl_map_universe(space);
7963 ext_factor = isl_map_range_product(ext_factor, factor);
7964 return isl_map_intersect(map, ext_factor);
7967 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7968 * in the space A -> C, return the intersection.
7970 __isl_give isl_map *isl_map_intersect_range_factor_range(
7971 __isl_take isl_map *map, __isl_take isl_map *factor)
7973 return isl_map_align_params_map_map_and(map, factor,
7974 &map_intersect_range_factor_range);
7977 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7978 __isl_take isl_map *map2)
7980 if (!map1 || !map2)
7981 goto error;
7982 map1 = isl_map_reverse(map1);
7983 map1 = isl_map_apply_range(map1, map2);
7984 return isl_map_reverse(map1);
7985 error:
7986 isl_map_free(map1);
7987 isl_map_free(map2);
7988 return NULL;
7991 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7992 __isl_take isl_map *map2)
7994 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7997 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7998 __isl_take isl_map *map2)
8000 isl_space *space;
8001 struct isl_map *result;
8002 int i, j;
8004 if (!map1 || !map2)
8005 goto error;
8007 space = isl_space_join(isl_space_copy(map1->dim),
8008 isl_space_copy(map2->dim));
8010 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8011 if (!result)
8012 goto error;
8013 for (i = 0; i < map1->n; ++i)
8014 for (j = 0; j < map2->n; ++j) {
8015 result = isl_map_add_basic_map(result,
8016 isl_basic_map_apply_range(
8017 isl_basic_map_copy(map1->p[i]),
8018 isl_basic_map_copy(map2->p[j])));
8019 if (!result)
8020 goto error;
8022 isl_map_free(map1);
8023 isl_map_free(map2);
8024 if (result && result->n <= 1)
8025 ISL_F_SET(result, ISL_MAP_DISJOINT);
8026 return result;
8027 error:
8028 isl_map_free(map1);
8029 isl_map_free(map2);
8030 return NULL;
8033 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8034 __isl_take isl_map *map2)
8036 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8040 * returns range - domain
8042 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8044 isl_space *target_space;
8045 struct isl_basic_set *bset;
8046 isl_size dim;
8047 isl_size nparam;
8048 isl_size total;
8049 int i;
8051 if (!bmap)
8052 goto error;
8053 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8054 bmap->dim, isl_dim_out),
8055 goto error);
8056 dim = isl_basic_map_dim(bmap, isl_dim_in);
8057 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8058 if (dim < 0 || nparam < 0)
8059 goto error;
8060 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8061 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8062 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8063 total = isl_basic_map_dim(bmap, isl_dim_all);
8064 if (total < 0)
8065 bmap = isl_basic_map_free(bmap);
8066 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8067 for (i = 0; i < dim; ++i) {
8068 int j = isl_basic_map_alloc_equality(bmap);
8069 if (j < 0) {
8070 bmap = isl_basic_map_free(bmap);
8071 break;
8073 isl_seq_clr(bmap->eq[j], 1 + total);
8074 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8075 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8076 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8078 bset = isl_basic_map_domain(bmap);
8079 bset = isl_basic_set_reset_space(bset, target_space);
8080 return bset;
8081 error:
8082 isl_basic_map_free(bmap);
8083 return NULL;
8086 /* Check that domain and range of "map" are the same.
8088 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8090 isl_space *space;
8091 isl_bool equal;
8093 space = isl_map_peek_space(map);
8094 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8095 if (equal < 0)
8096 return isl_stat_error;
8097 if (!equal)
8098 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8099 "domain and range don't match", return isl_stat_error);
8100 return isl_stat_ok;
8104 * returns range - domain
8106 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8108 int i;
8109 isl_space *dim;
8110 struct isl_set *result;
8112 if (!map)
8113 return NULL;
8115 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8116 map->dim, isl_dim_out),
8117 goto error);
8118 dim = isl_map_get_space(map);
8119 dim = isl_space_domain(dim);
8120 result = isl_set_alloc_space(dim, map->n, 0);
8121 if (!result)
8122 goto error;
8123 for (i = 0; i < map->n; ++i)
8124 result = isl_set_add_basic_set(result,
8125 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8126 isl_map_free(map);
8127 return result;
8128 error:
8129 isl_map_free(map);
8130 return NULL;
8134 * returns [domain -> range] -> range - domain
8136 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8137 __isl_take isl_basic_map *bmap)
8139 int i, k;
8140 isl_space *space;
8141 isl_basic_map *domain;
8142 isl_size nparam, n;
8143 isl_size total;
8145 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8146 bmap->dim, isl_dim_out))
8147 isl_die(bmap->ctx, isl_error_invalid,
8148 "domain and range don't match", goto error);
8150 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8151 n = isl_basic_map_dim(bmap, isl_dim_in);
8152 if (nparam < 0 || n)
8153 return isl_basic_map_free(bmap);
8155 space = isl_basic_map_get_space(bmap);
8156 space = isl_space_from_range(isl_space_domain(space));
8157 domain = isl_basic_map_universe(space);
8159 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8160 bmap = isl_basic_map_apply_range(bmap, domain);
8161 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8163 total = isl_basic_map_dim(bmap, isl_dim_all);
8164 if (total < 0)
8165 return isl_basic_map_free(bmap);
8167 for (i = 0; i < n; ++i) {
8168 k = isl_basic_map_alloc_equality(bmap);
8169 if (k < 0)
8170 goto error;
8171 isl_seq_clr(bmap->eq[k], 1 + total);
8172 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8173 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8174 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8177 bmap = isl_basic_map_gauss(bmap, NULL);
8178 return isl_basic_map_finalize(bmap);
8179 error:
8180 isl_basic_map_free(bmap);
8181 return NULL;
8185 * returns [domain -> range] -> range - domain
8187 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8189 int i;
8190 isl_space *domain_space;
8192 if (isl_map_check_equal_tuples(map) < 0)
8193 return isl_map_free(map);
8195 map = isl_map_cow(map);
8196 if (!map)
8197 return NULL;
8199 domain_space = isl_space_domain(isl_map_get_space(map));
8200 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8201 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8202 if (!map->dim)
8203 goto error;
8204 for (i = 0; i < map->n; ++i) {
8205 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8206 if (!map->p[i])
8207 goto error;
8209 map = isl_map_unmark_normalized(map);
8210 return map;
8211 error:
8212 isl_map_free(map);
8213 return NULL;
8216 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8218 isl_size n_in, n_out;
8220 n_in = isl_space_dim(space, isl_dim_in);
8221 n_out = isl_space_dim(space, isl_dim_out);
8222 if (n_in < 0 || n_out < 0)
8223 goto error;
8224 if (n_in != n_out)
8225 isl_die(space->ctx, isl_error_invalid,
8226 "number of input and output dimensions needs to be "
8227 "the same", goto error);
8228 return isl_basic_map_equal(space, n_in);
8229 error:
8230 isl_space_free(space);
8231 return NULL;
8234 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8236 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8239 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8241 isl_space *dim = isl_set_get_space(set);
8242 isl_map *id;
8243 id = isl_map_identity(isl_space_map_from_set(dim));
8244 return isl_map_intersect_range(id, set);
8247 /* Construct a basic set with all set dimensions having only non-negative
8248 * values.
8250 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8251 __isl_take isl_space *space)
8253 int i;
8254 isl_size nparam;
8255 isl_size dim;
8256 isl_size total;
8257 struct isl_basic_set *bset;
8259 nparam = isl_space_dim(space, isl_dim_param);
8260 dim = isl_space_dim(space, isl_dim_set);
8261 total = isl_space_dim(space, isl_dim_all);
8262 if (nparam < 0 || dim < 0 || total < 0)
8263 space = isl_space_free(space);
8264 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8265 if (!bset)
8266 return NULL;
8267 for (i = 0; i < dim; ++i) {
8268 int k = isl_basic_set_alloc_inequality(bset);
8269 if (k < 0)
8270 goto error;
8271 isl_seq_clr(bset->ineq[k], 1 + total);
8272 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8274 return bset;
8275 error:
8276 isl_basic_set_free(bset);
8277 return NULL;
8280 /* Construct the half-space x_pos >= 0.
8282 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8283 int pos)
8285 int k;
8286 isl_size total;
8287 isl_basic_set *nonneg;
8289 total = isl_space_dim(space, isl_dim_all);
8290 if (total < 0)
8291 space = isl_space_free(space);
8292 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8293 k = isl_basic_set_alloc_inequality(nonneg);
8294 if (k < 0)
8295 goto error;
8296 isl_seq_clr(nonneg->ineq[k], 1 + total);
8297 isl_int_set_si(nonneg->ineq[k][pos], 1);
8299 return isl_basic_set_finalize(nonneg);
8300 error:
8301 isl_basic_set_free(nonneg);
8302 return NULL;
8305 /* Construct the half-space x_pos <= -1.
8307 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8308 int pos)
8310 int k;
8311 isl_size total;
8312 isl_basic_set *neg;
8314 total = isl_space_dim(space, isl_dim_all);
8315 if (total < 0)
8316 space = isl_space_free(space);
8317 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8318 k = isl_basic_set_alloc_inequality(neg);
8319 if (k < 0)
8320 goto error;
8321 isl_seq_clr(neg->ineq[k], 1 + total);
8322 isl_int_set_si(neg->ineq[k][0], -1);
8323 isl_int_set_si(neg->ineq[k][pos], -1);
8325 return isl_basic_set_finalize(neg);
8326 error:
8327 isl_basic_set_free(neg);
8328 return NULL;
8331 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8332 enum isl_dim_type type, unsigned first, unsigned n)
8334 int i;
8335 unsigned offset;
8336 isl_basic_set *nonneg;
8337 isl_basic_set *neg;
8339 if (n == 0)
8340 return set;
8342 if (isl_set_check_range(set, type, first, n) < 0)
8343 return isl_set_free(set);
8345 offset = pos(set->dim, type);
8346 for (i = 0; i < n; ++i) {
8347 nonneg = nonneg_halfspace(isl_set_get_space(set),
8348 offset + first + i);
8349 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8351 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8354 return set;
8357 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8358 int len,
8359 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8360 void *user)
8362 isl_set *half;
8364 if (!set)
8365 return isl_stat_error;
8366 if (isl_set_plain_is_empty(set)) {
8367 isl_set_free(set);
8368 return isl_stat_ok;
8370 if (first == len)
8371 return fn(set, signs, user);
8373 signs[first] = 1;
8374 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8375 1 + first));
8376 half = isl_set_intersect(half, isl_set_copy(set));
8377 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8378 goto error;
8380 signs[first] = -1;
8381 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8382 1 + first));
8383 half = isl_set_intersect(half, set);
8384 return foreach_orthant(half, signs, first + 1, len, fn, user);
8385 error:
8386 isl_set_free(set);
8387 return isl_stat_error;
8390 /* Call "fn" on the intersections of "set" with each of the orthants
8391 * (except for obviously empty intersections). The orthant is identified
8392 * by the signs array, with each entry having value 1 or -1 according
8393 * to the sign of the corresponding variable.
8395 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8396 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8397 void *user)
8399 isl_size nparam;
8400 isl_size nvar;
8401 int *signs;
8402 isl_stat r;
8404 if (!set)
8405 return isl_stat_error;
8406 if (isl_set_plain_is_empty(set))
8407 return isl_stat_ok;
8409 nparam = isl_set_dim(set, isl_dim_param);
8410 nvar = isl_set_dim(set, isl_dim_set);
8411 if (nparam < 0 || nvar < 0)
8412 return isl_stat_error;
8414 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8416 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8417 fn, user);
8419 free(signs);
8421 return r;
8424 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8426 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8429 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8430 __isl_keep isl_basic_map *bmap2)
8432 isl_bool is_subset;
8433 struct isl_map *map1;
8434 struct isl_map *map2;
8436 if (!bmap1 || !bmap2)
8437 return isl_bool_error;
8439 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8440 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8442 is_subset = isl_map_is_subset(map1, map2);
8444 isl_map_free(map1);
8445 isl_map_free(map2);
8447 return is_subset;
8450 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8451 __isl_keep isl_basic_set *bset2)
8453 return isl_basic_map_is_subset(bset1, bset2);
8456 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8457 __isl_keep isl_basic_map *bmap2)
8459 isl_bool is_subset;
8461 if (!bmap1 || !bmap2)
8462 return isl_bool_error;
8463 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8464 if (is_subset != isl_bool_true)
8465 return is_subset;
8466 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8467 return is_subset;
8470 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8471 __isl_keep isl_basic_set *bset2)
8473 return isl_basic_map_is_equal(
8474 bset_to_bmap(bset1), bset_to_bmap(bset2));
8477 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8479 int i;
8480 int is_empty;
8482 if (!map)
8483 return isl_bool_error;
8484 for (i = 0; i < map->n; ++i) {
8485 is_empty = isl_basic_map_is_empty(map->p[i]);
8486 if (is_empty < 0)
8487 return isl_bool_error;
8488 if (!is_empty)
8489 return isl_bool_false;
8491 return isl_bool_true;
8494 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8496 return map ? map->n == 0 : isl_bool_error;
8499 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8501 return set ? set->n == 0 : isl_bool_error;
8504 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8506 return isl_map_is_empty(set_to_map(set));
8509 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8510 __isl_keep isl_map *map2)
8512 if (!map1 || !map2)
8513 return isl_bool_error;
8515 return isl_space_is_equal(map1->dim, map2->dim);
8518 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8519 __isl_keep isl_set *set2)
8521 if (!set1 || !set2)
8522 return isl_bool_error;
8524 return isl_space_is_equal(set1->dim, set2->dim);
8527 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8529 isl_bool is_subset;
8531 if (!map1 || !map2)
8532 return isl_bool_error;
8533 is_subset = isl_map_is_subset(map1, map2);
8534 if (is_subset != isl_bool_true)
8535 return is_subset;
8536 is_subset = isl_map_is_subset(map2, map1);
8537 return is_subset;
8540 /* Is "map1" equal to "map2"?
8542 * First check if they are obviously equal.
8543 * If not, then perform a more detailed analysis.
8545 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8547 isl_bool equal;
8549 equal = isl_map_plain_is_equal(map1, map2);
8550 if (equal < 0 || equal)
8551 return equal;
8552 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8555 isl_bool isl_basic_map_is_strict_subset(
8556 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8558 isl_bool is_subset;
8560 if (!bmap1 || !bmap2)
8561 return isl_bool_error;
8562 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8563 if (is_subset != isl_bool_true)
8564 return is_subset;
8565 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8566 return isl_bool_not(is_subset);
8569 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8570 __isl_keep isl_map *map2)
8572 isl_bool is_subset;
8574 if (!map1 || !map2)
8575 return isl_bool_error;
8576 is_subset = isl_map_is_subset(map1, map2);
8577 if (is_subset != isl_bool_true)
8578 return is_subset;
8579 is_subset = isl_map_is_subset(map2, map1);
8580 return isl_bool_not(is_subset);
8583 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8584 __isl_keep isl_set *set2)
8586 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8589 /* Is "bmap" obviously equal to the universe with the same space?
8591 * That is, does it not have any constraints?
8593 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8595 if (!bmap)
8596 return isl_bool_error;
8597 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8600 /* Is "bset" obviously equal to the universe with the same space?
8602 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8604 return isl_basic_map_plain_is_universe(bset);
8607 /* If "c" does not involve any existentially quantified variables,
8608 * then set *univ to false and abort
8610 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8612 isl_bool *univ = user;
8613 isl_size n;
8615 n = isl_constraint_dim(c, isl_dim_div);
8616 if (n < 0)
8617 c = isl_constraint_free(c);
8618 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8619 isl_constraint_free(c);
8620 if (*univ < 0 || !*univ)
8621 return isl_stat_error;
8622 return isl_stat_ok;
8625 /* Is "bmap" equal to the universe with the same space?
8627 * First check if it is obviously equal to the universe.
8628 * If not and if there are any constraints not involving
8629 * existentially quantified variables, then it is certainly
8630 * not equal to the universe.
8631 * Otherwise, check if the universe is a subset of "bmap".
8633 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8635 isl_size n_div;
8636 isl_bool univ;
8637 isl_basic_map *test;
8639 univ = isl_basic_map_plain_is_universe(bmap);
8640 if (univ < 0 || univ)
8641 return univ;
8642 n_div = isl_basic_map_dim(bmap, isl_dim_div);
8643 if (n_div < 0)
8644 return isl_bool_error;
8645 if (n_div == 0)
8646 return isl_bool_false;
8647 univ = isl_bool_true;
8648 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8649 univ)
8650 return isl_bool_error;
8651 if (univ < 0 || !univ)
8652 return univ;
8653 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8654 univ = isl_basic_map_is_subset(test, bmap);
8655 isl_basic_map_free(test);
8656 return univ;
8659 /* Is "bset" equal to the universe with the same space?
8661 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8663 return isl_basic_map_is_universe(bset);
8666 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8668 int i;
8670 if (!map)
8671 return isl_bool_error;
8673 for (i = 0; i < map->n; ++i) {
8674 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8675 if (r < 0 || r)
8676 return r;
8679 return isl_bool_false;
8682 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8684 return isl_map_plain_is_universe(set_to_map(set));
8687 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8689 struct isl_basic_set *bset = NULL;
8690 struct isl_vec *sample = NULL;
8691 isl_bool empty, non_empty;
8693 if (!bmap)
8694 return isl_bool_error;
8696 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8697 return isl_bool_true;
8699 if (isl_basic_map_plain_is_universe(bmap))
8700 return isl_bool_false;
8702 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8703 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8704 copy = isl_basic_map_remove_redundancies(copy);
8705 empty = isl_basic_map_plain_is_empty(copy);
8706 isl_basic_map_free(copy);
8707 return empty;
8710 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8711 if (non_empty < 0)
8712 return isl_bool_error;
8713 if (non_empty)
8714 return isl_bool_false;
8715 isl_vec_free(bmap->sample);
8716 bmap->sample = NULL;
8717 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8718 if (!bset)
8719 return isl_bool_error;
8720 sample = isl_basic_set_sample_vec(bset);
8721 if (!sample)
8722 return isl_bool_error;
8723 empty = sample->size == 0;
8724 isl_vec_free(bmap->sample);
8725 bmap->sample = sample;
8726 if (empty)
8727 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8729 return empty;
8732 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8734 if (!bmap)
8735 return isl_bool_error;
8736 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8739 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8741 if (!bset)
8742 return isl_bool_error;
8743 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8746 /* Is "bmap" known to be non-empty?
8748 * That is, is the cached sample still valid?
8750 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8752 isl_size total;
8754 if (!bmap)
8755 return isl_bool_error;
8756 if (!bmap->sample)
8757 return isl_bool_false;
8758 total = isl_basic_map_dim(bmap, isl_dim_all);
8759 if (total < 0)
8760 return isl_bool_error;
8761 if (bmap->sample->size != 1 + total)
8762 return isl_bool_false;
8763 return isl_basic_map_contains(bmap, bmap->sample);
8766 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8768 return isl_basic_map_is_empty(bset_to_bmap(bset));
8771 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8772 __isl_take isl_basic_map *bmap2)
8774 struct isl_map *map;
8775 if (!bmap1 || !bmap2)
8776 goto error;
8778 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8780 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8781 if (!map)
8782 goto error;
8783 map = isl_map_add_basic_map(map, bmap1);
8784 map = isl_map_add_basic_map(map, bmap2);
8785 return map;
8786 error:
8787 isl_basic_map_free(bmap1);
8788 isl_basic_map_free(bmap2);
8789 return NULL;
8792 struct isl_set *isl_basic_set_union(
8793 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8795 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8796 bset_to_bmap(bset2)));
8799 /* Order divs such that any div only depends on previous divs */
8800 __isl_give isl_basic_map *isl_basic_map_order_divs(
8801 __isl_take isl_basic_map *bmap)
8803 int i;
8804 isl_size off;
8806 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8807 if (off < 0)
8808 return isl_basic_map_free(bmap);
8810 for (i = 0; i < bmap->n_div; ++i) {
8811 int pos;
8812 if (isl_int_is_zero(bmap->div[i][0]))
8813 continue;
8814 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8815 bmap->n_div-i);
8816 if (pos == -1)
8817 continue;
8818 if (pos == 0)
8819 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8820 "integer division depends on itself",
8821 return isl_basic_map_free(bmap));
8822 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8823 if (!bmap)
8824 return NULL;
8825 --i;
8827 return bmap;
8830 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8832 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8835 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8837 int i;
8839 if (!map)
8840 return 0;
8842 for (i = 0; i < map->n; ++i) {
8843 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8844 if (!map->p[i])
8845 goto error;
8848 return map;
8849 error:
8850 isl_map_free(map);
8851 return NULL;
8854 /* Sort the local variables of "bset".
8856 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8857 __isl_take isl_basic_set *bset)
8859 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8862 /* Apply the expansion computed by isl_merge_divs.
8863 * The expansion itself is given by "exp" while the resulting
8864 * list of divs is given by "div".
8866 * Move the integer divisions of "bmap" into the right position
8867 * according to "exp" and then introduce the additional integer
8868 * divisions, adding div constraints.
8869 * The moving should be done first to avoid moving coefficients
8870 * in the definitions of the extra integer divisions.
8872 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8873 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8875 int i, j;
8876 int n_div;
8878 bmap = isl_basic_map_cow(bmap);
8879 if (!bmap || !div)
8880 goto error;
8882 if (div->n_row < bmap->n_div)
8883 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8884 "not an expansion", goto error);
8886 n_div = bmap->n_div;
8887 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8888 div->n_row - n_div, 0,
8889 2 * (div->n_row - n_div));
8891 for (i = n_div; i < div->n_row; ++i)
8892 if (isl_basic_map_alloc_div(bmap) < 0)
8893 goto error;
8895 for (j = n_div - 1; j >= 0; --j) {
8896 if (exp[j] == j)
8897 break;
8898 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8899 if (!bmap)
8900 goto error;
8902 j = 0;
8903 for (i = 0; i < div->n_row; ++i) {
8904 if (j < n_div && exp[j] == i) {
8905 j++;
8906 } else {
8907 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8908 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8909 continue;
8910 bmap = isl_basic_map_add_div_constraints(bmap, i);
8911 if (!bmap)
8912 goto error;
8916 isl_mat_free(div);
8917 return bmap;
8918 error:
8919 isl_basic_map_free(bmap);
8920 isl_mat_free(div);
8921 return NULL;
8924 /* Apply the expansion computed by isl_merge_divs.
8925 * The expansion itself is given by "exp" while the resulting
8926 * list of divs is given by "div".
8928 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8929 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8931 return isl_basic_map_expand_divs(bset, div, exp);
8934 /* Look for a div in dst that corresponds to the div "div" in src.
8935 * The divs before "div" in src and dst are assumed to be the same.
8937 * Return the position of the corresponding div in dst
8938 * if there is one. Otherwise, return a position beyond the integer divisions.
8939 * Return -1 on error.
8941 static int find_div(__isl_keep isl_basic_map *dst,
8942 __isl_keep isl_basic_map *src, unsigned div)
8944 int i;
8945 isl_size n_div;
8946 isl_size v_div;
8948 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8949 n_div = isl_basic_map_dim(dst, isl_dim_div);
8950 if (n_div < 0 || v_div < 0)
8951 return -1;
8952 isl_assert(dst->ctx, div <= n_div, return -1);
8953 for (i = div; i < n_div; ++i)
8954 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8955 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8956 n_div - div) == -1)
8957 return i;
8958 return n_div;
8961 /* Align the divs of "dst" to those of "src", adding divs from "src"
8962 * if needed. That is, make sure that the first src->n_div divs
8963 * of the result are equal to those of src.
8965 * The result is not finalized as by design it will have redundant
8966 * divs if any divs from "src" were copied.
8968 __isl_give isl_basic_map *isl_basic_map_align_divs(
8969 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8971 int i;
8972 isl_bool known;
8973 int extended;
8974 isl_size v_div;
8975 isl_size dst_n_div;
8977 if (!dst || !src)
8978 return isl_basic_map_free(dst);
8980 if (src->n_div == 0)
8981 return dst;
8983 known = isl_basic_map_divs_known(src);
8984 if (known < 0)
8985 return isl_basic_map_free(dst);
8986 if (!known)
8987 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8988 "some src divs are unknown",
8989 return isl_basic_map_free(dst));
8991 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8992 if (v_div < 0)
8993 return isl_basic_map_free(dst);
8995 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8996 if (!src)
8997 return isl_basic_map_free(dst);
8999 extended = 0;
9000 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9001 if (dst_n_div < 0)
9002 dst = isl_basic_map_free(dst);
9003 for (i = 0; i < src->n_div; ++i) {
9004 int j = find_div(dst, src, i);
9005 if (j < 0)
9006 dst = isl_basic_map_free(dst);
9007 if (j == dst_n_div) {
9008 if (!extended) {
9009 int extra = src->n_div - i;
9010 dst = isl_basic_map_cow(dst);
9011 if (!dst)
9012 goto error;
9013 dst = isl_basic_map_extend_space(dst,
9014 isl_space_copy(dst->dim),
9015 extra, 0, 2 * extra);
9016 extended = 1;
9018 j = isl_basic_map_alloc_div(dst);
9019 if (j < 0)
9020 goto error;
9021 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9022 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9023 dst_n_div++;
9024 dst = isl_basic_map_add_div_constraints(dst, j);
9025 if (!dst)
9026 goto error;
9028 if (j != i)
9029 dst = isl_basic_map_swap_div(dst, i, j);
9030 if (!dst)
9031 goto error;
9033 isl_basic_map_free(src);
9034 return dst;
9035 error:
9036 isl_basic_map_free(src);
9037 isl_basic_map_free(dst);
9038 return NULL;
9041 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9043 int i;
9045 if (!map)
9046 return NULL;
9047 if (map->n == 0)
9048 return map;
9049 map = isl_map_compute_divs(map);
9050 map = isl_map_cow(map);
9051 if (!map)
9052 return NULL;
9054 for (i = 1; i < map->n; ++i)
9055 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9056 for (i = 1; i < map->n; ++i) {
9057 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9058 if (!map->p[i])
9059 return isl_map_free(map);
9062 map = isl_map_unmark_normalized(map);
9063 return map;
9066 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9068 return isl_map_align_divs_internal(map);
9071 struct isl_set *isl_set_align_divs(struct isl_set *set)
9073 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9076 /* Align the divs of the basic maps in "map" to those
9077 * of the basic maps in "list", as well as to the other basic maps in "map".
9078 * The elements in "list" are assumed to have known divs.
9080 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9081 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9083 int i;
9084 isl_size n;
9086 n = isl_basic_map_list_n_basic_map(list);
9087 map = isl_map_compute_divs(map);
9088 map = isl_map_cow(map);
9089 if (!map || n < 0)
9090 return isl_map_free(map);
9091 if (map->n == 0)
9092 return map;
9094 for (i = 0; i < n; ++i) {
9095 isl_basic_map *bmap;
9097 bmap = isl_basic_map_list_get_basic_map(list, i);
9098 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9099 isl_basic_map_free(bmap);
9101 if (!map->p[0])
9102 return isl_map_free(map);
9104 return isl_map_align_divs_internal(map);
9107 /* Align the divs of each element of "list" to those of "bmap".
9108 * Both "bmap" and the elements of "list" are assumed to have known divs.
9110 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9111 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9113 int i;
9114 isl_size n;
9116 n = isl_basic_map_list_n_basic_map(list);
9117 if (n < 0 || !bmap)
9118 return isl_basic_map_list_free(list);
9120 for (i = 0; i < n; ++i) {
9121 isl_basic_map *bmap_i;
9123 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9124 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9125 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9128 return list;
9131 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9132 __isl_take isl_map *map)
9134 isl_bool ok;
9136 ok = isl_map_compatible_domain(map, set);
9137 if (ok < 0)
9138 goto error;
9139 if (!ok)
9140 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9141 "incompatible spaces", goto error);
9142 map = isl_map_intersect_domain(map, set);
9143 set = isl_map_range(map);
9144 return set;
9145 error:
9146 isl_set_free(set);
9147 isl_map_free(map);
9148 return NULL;
9151 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9152 __isl_take isl_map *map)
9154 return isl_map_align_params_map_map_and(set, map, &set_apply);
9157 /* There is no need to cow as removing empty parts doesn't change
9158 * the meaning of the set.
9160 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9162 int i;
9164 if (!map)
9165 return NULL;
9167 for (i = map->n - 1; i >= 0; --i)
9168 map = remove_if_empty(map, i);
9170 return map;
9173 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9175 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9178 /* Create a binary relation that maps the shared initial "pos" dimensions
9179 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9181 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9182 __isl_keep isl_basic_set *bset2, int pos)
9184 isl_basic_map *bmap1;
9185 isl_basic_map *bmap2;
9187 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9188 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9189 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9190 isl_dim_out, 0, pos);
9191 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9192 isl_dim_out, 0, pos);
9193 return isl_basic_map_range_product(bmap1, bmap2);
9196 /* Given two basic sets bset1 and bset2, compute the maximal difference
9197 * between the values of dimension pos in bset1 and those in bset2
9198 * for any common value of the parameters and dimensions preceding pos.
9200 static enum isl_lp_result basic_set_maximal_difference_at(
9201 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9202 int pos, isl_int *opt)
9204 isl_basic_map *bmap1;
9205 struct isl_ctx *ctx;
9206 struct isl_vec *obj;
9207 isl_size total;
9208 isl_size nparam;
9209 isl_size dim1;
9210 enum isl_lp_result res;
9212 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9213 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9214 if (nparam < 0 || dim1 < 0 || !bset2)
9215 return isl_lp_error;
9217 bmap1 = join_initial(bset1, bset2, pos);
9218 total = isl_basic_map_dim(bmap1, isl_dim_all);
9219 if (total < 0)
9220 return isl_lp_error;
9222 ctx = bmap1->ctx;
9223 obj = isl_vec_alloc(ctx, 1 + total);
9224 if (!obj)
9225 goto error;
9226 isl_seq_clr(obj->block.data, 1 + total);
9227 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9228 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9229 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9230 opt, NULL, NULL);
9231 isl_basic_map_free(bmap1);
9232 isl_vec_free(obj);
9233 return res;
9234 error:
9235 isl_basic_map_free(bmap1);
9236 return isl_lp_error;
9239 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9240 * for any common value of the parameters and dimensions preceding pos
9241 * in both basic sets, the values of dimension pos in bset1 are
9242 * smaller or larger than those in bset2.
9244 * Returns
9245 * 1 if bset1 follows bset2
9246 * -1 if bset1 precedes bset2
9247 * 0 if bset1 and bset2 are incomparable
9248 * -2 if some error occurred.
9250 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9251 __isl_keep isl_basic_set *bset2, int pos)
9253 isl_int opt;
9254 enum isl_lp_result res;
9255 int cmp;
9257 isl_int_init(opt);
9259 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9261 if (res == isl_lp_empty)
9262 cmp = 0;
9263 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9264 res == isl_lp_unbounded)
9265 cmp = 1;
9266 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9267 cmp = -1;
9268 else
9269 cmp = -2;
9271 isl_int_clear(opt);
9272 return cmp;
9275 /* Given two basic sets bset1 and bset2, check whether
9276 * for any common value of the parameters and dimensions preceding pos
9277 * there is a value of dimension pos in bset1 that is larger
9278 * than a value of the same dimension in bset2.
9280 * Return
9281 * 1 if there exists such a pair
9282 * 0 if there is no such pair, but there is a pair of equal values
9283 * -1 otherwise
9284 * -2 if some error occurred.
9286 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9287 __isl_keep isl_basic_set *bset2, int pos)
9289 isl_bool empty;
9290 isl_basic_map *bmap;
9291 isl_size dim1;
9293 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9294 if (dim1 < 0)
9295 return -2;
9296 bmap = join_initial(bset1, bset2, pos);
9297 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9298 isl_dim_out, dim1 - pos);
9299 empty = isl_basic_map_is_empty(bmap);
9300 if (empty < 0)
9301 goto error;
9302 if (empty) {
9303 isl_basic_map_free(bmap);
9304 return -1;
9306 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9307 isl_dim_out, dim1 - pos);
9308 empty = isl_basic_map_is_empty(bmap);
9309 if (empty < 0)
9310 goto error;
9311 isl_basic_map_free(bmap);
9312 if (empty)
9313 return 0;
9314 return 1;
9315 error:
9316 isl_basic_map_free(bmap);
9317 return -2;
9320 /* Given two sets set1 and set2, check whether
9321 * for any common value of the parameters and dimensions preceding pos
9322 * there is a value of dimension pos in set1 that is larger
9323 * than a value of the same dimension in set2.
9325 * Return
9326 * 1 if there exists such a pair
9327 * 0 if there is no such pair, but there is a pair of equal values
9328 * -1 otherwise
9329 * -2 if some error occurred.
9331 int isl_set_follows_at(__isl_keep isl_set *set1,
9332 __isl_keep isl_set *set2, int pos)
9334 int i, j;
9335 int follows = -1;
9337 if (!set1 || !set2)
9338 return -2;
9340 for (i = 0; i < set1->n; ++i)
9341 for (j = 0; j < set2->n; ++j) {
9342 int f;
9343 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9344 if (f == 1 || f == -2)
9345 return f;
9346 if (f > follows)
9347 follows = f;
9350 return follows;
9353 static isl_bool isl_basic_map_plain_has_fixed_var(
9354 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9356 int i;
9357 int d;
9358 isl_size total;
9360 total = isl_basic_map_dim(bmap, isl_dim_all);
9361 if (total < 0)
9362 return isl_bool_error;
9363 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9364 for (; d+1 > pos; --d)
9365 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9366 break;
9367 if (d != pos)
9368 continue;
9369 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9370 return isl_bool_false;
9371 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9372 return isl_bool_false;
9373 if (!isl_int_is_one(bmap->eq[i][1+d]))
9374 return isl_bool_false;
9375 if (val)
9376 isl_int_neg(*val, bmap->eq[i][0]);
9377 return isl_bool_true;
9379 return isl_bool_false;
9382 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9383 unsigned pos, isl_int *val)
9385 int i;
9386 isl_int v;
9387 isl_int tmp;
9388 isl_bool fixed;
9390 if (!map)
9391 return isl_bool_error;
9392 if (map->n == 0)
9393 return isl_bool_false;
9394 if (map->n == 1)
9395 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9396 isl_int_init(v);
9397 isl_int_init(tmp);
9398 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9399 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9400 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9401 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9402 fixed = isl_bool_false;
9404 if (val)
9405 isl_int_set(*val, v);
9406 isl_int_clear(tmp);
9407 isl_int_clear(v);
9408 return fixed;
9411 static isl_bool isl_basic_set_plain_has_fixed_var(
9412 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9414 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9415 pos, val);
9418 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9419 enum isl_dim_type type, unsigned pos, isl_int *val)
9421 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9422 return isl_bool_error;
9423 return isl_basic_map_plain_has_fixed_var(bmap,
9424 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9427 /* If "bmap" obviously lies on a hyperplane where the given dimension
9428 * has a fixed value, then return that value.
9429 * Otherwise return NaN.
9431 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9432 __isl_keep isl_basic_map *bmap,
9433 enum isl_dim_type type, unsigned pos)
9435 isl_ctx *ctx;
9436 isl_val *v;
9437 isl_bool fixed;
9439 if (!bmap)
9440 return NULL;
9441 ctx = isl_basic_map_get_ctx(bmap);
9442 v = isl_val_alloc(ctx);
9443 if (!v)
9444 return NULL;
9445 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9446 if (fixed < 0)
9447 return isl_val_free(v);
9448 if (fixed) {
9449 isl_int_set_si(v->d, 1);
9450 return v;
9452 isl_val_free(v);
9453 return isl_val_nan(ctx);
9456 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9457 enum isl_dim_type type, unsigned pos, isl_int *val)
9459 if (isl_map_check_range(map, type, pos, 1) < 0)
9460 return isl_bool_error;
9461 return isl_map_plain_has_fixed_var(map,
9462 map_offset(map, type) - 1 + pos, val);
9465 /* If "map" obviously lies on a hyperplane where the given dimension
9466 * has a fixed value, then return that value.
9467 * Otherwise return NaN.
9469 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9470 enum isl_dim_type type, unsigned pos)
9472 isl_ctx *ctx;
9473 isl_val *v;
9474 isl_bool fixed;
9476 if (!map)
9477 return NULL;
9478 ctx = isl_map_get_ctx(map);
9479 v = isl_val_alloc(ctx);
9480 if (!v)
9481 return NULL;
9482 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9483 if (fixed < 0)
9484 return isl_val_free(v);
9485 if (fixed) {
9486 isl_int_set_si(v->d, 1);
9487 return v;
9489 isl_val_free(v);
9490 return isl_val_nan(ctx);
9493 /* If "set" obviously lies on a hyperplane where the given dimension
9494 * has a fixed value, then return that value.
9495 * Otherwise return NaN.
9497 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9498 enum isl_dim_type type, unsigned pos)
9500 return isl_map_plain_get_val_if_fixed(set, type, pos);
9503 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9504 * then return this fixed value in *val.
9506 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9507 unsigned dim, isl_int *val)
9509 isl_size nparam;
9511 nparam = isl_basic_set_dim(bset, isl_dim_param);
9512 if (nparam < 0)
9513 return isl_bool_error;
9514 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
9517 /* Return -1 if the constraint "c1" should be sorted before "c2"
9518 * and 1 if it should be sorted after "c2".
9519 * Return 0 if the two constraints are the same (up to the constant term).
9521 * In particular, if a constraint involves later variables than another
9522 * then it is sorted after this other constraint.
9523 * uset_gist depends on constraints without existentially quantified
9524 * variables sorting first.
9526 * For constraints that have the same latest variable, those
9527 * with the same coefficient for this latest variable (first in absolute value
9528 * and then in actual value) are grouped together.
9529 * This is useful for detecting pairs of constraints that can
9530 * be chained in their printed representation.
9532 * Finally, within a group, constraints are sorted according to
9533 * their coefficients (excluding the constant term).
9535 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9537 isl_int **c1 = (isl_int **) p1;
9538 isl_int **c2 = (isl_int **) p2;
9539 int l1, l2;
9540 unsigned size = *(unsigned *) arg;
9541 int cmp;
9543 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9544 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9546 if (l1 != l2)
9547 return l1 - l2;
9549 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9550 if (cmp != 0)
9551 return cmp;
9552 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9553 if (cmp != 0)
9554 return -cmp;
9556 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9559 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9560 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9561 * and 0 if the two constraints are the same (up to the constant term).
9563 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9564 isl_int *c1, isl_int *c2)
9566 isl_size total;
9567 unsigned size;
9569 total = isl_basic_map_dim(bmap, isl_dim_all);
9570 if (total < 0)
9571 return -2;
9572 size = total;
9573 return sort_constraint_cmp(&c1, &c2, &size);
9576 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9577 __isl_take isl_basic_map *bmap)
9579 isl_size total;
9580 unsigned size;
9582 if (!bmap)
9583 return NULL;
9584 if (bmap->n_ineq == 0)
9585 return bmap;
9586 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9587 return bmap;
9588 total = isl_basic_map_dim(bmap, isl_dim_all);
9589 if (total < 0)
9590 return isl_basic_map_free(bmap);
9591 size = total;
9592 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9593 &sort_constraint_cmp, &size) < 0)
9594 return isl_basic_map_free(bmap);
9595 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9596 return bmap;
9599 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9600 __isl_take isl_basic_set *bset)
9602 isl_basic_map *bmap = bset_to_bmap(bset);
9603 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9606 __isl_give isl_basic_map *isl_basic_map_normalize(
9607 __isl_take isl_basic_map *bmap)
9609 bmap = isl_basic_map_remove_redundancies(bmap);
9610 bmap = isl_basic_map_sort_constraints(bmap);
9611 return bmap;
9613 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9614 __isl_keep isl_basic_map *bmap2)
9616 int i, cmp;
9617 isl_size total;
9618 isl_space *space1, *space2;
9620 if (!bmap1 || !bmap2)
9621 return -1;
9623 if (bmap1 == bmap2)
9624 return 0;
9625 space1 = isl_basic_map_peek_space(bmap1);
9626 space2 = isl_basic_map_peek_space(bmap2);
9627 cmp = isl_space_cmp(space1, space2);
9628 if (cmp)
9629 return cmp;
9630 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9631 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9632 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9633 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9634 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9635 return 0;
9636 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9637 return 1;
9638 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9639 return -1;
9640 if (bmap1->n_eq != bmap2->n_eq)
9641 return bmap1->n_eq - bmap2->n_eq;
9642 if (bmap1->n_ineq != bmap2->n_ineq)
9643 return bmap1->n_ineq - bmap2->n_ineq;
9644 if (bmap1->n_div != bmap2->n_div)
9645 return bmap1->n_div - bmap2->n_div;
9646 total = isl_basic_map_dim(bmap1, isl_dim_all);
9647 if (total < 0)
9648 return -1;
9649 for (i = 0; i < bmap1->n_eq; ++i) {
9650 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9651 if (cmp)
9652 return cmp;
9654 for (i = 0; i < bmap1->n_ineq; ++i) {
9655 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9656 if (cmp)
9657 return cmp;
9659 for (i = 0; i < bmap1->n_div; ++i) {
9660 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9661 if (cmp)
9662 return cmp;
9664 return 0;
9667 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9668 __isl_keep isl_basic_set *bset2)
9670 return isl_basic_map_plain_cmp(bset1, bset2);
9673 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9675 int i, cmp;
9677 if (set1 == set2)
9678 return 0;
9679 if (set1->n != set2->n)
9680 return set1->n - set2->n;
9682 for (i = 0; i < set1->n; ++i) {
9683 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9684 if (cmp)
9685 return cmp;
9688 return 0;
9691 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9692 __isl_keep isl_basic_map *bmap2)
9694 if (!bmap1 || !bmap2)
9695 return isl_bool_error;
9696 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9699 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9700 __isl_keep isl_basic_set *bset2)
9702 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9703 bset_to_bmap(bset2));
9706 static int qsort_bmap_cmp(const void *p1, const void *p2)
9708 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9709 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9711 return isl_basic_map_plain_cmp(bmap1, bmap2);
9714 /* Sort the basic maps of "map" and remove duplicate basic maps.
9716 * While removing basic maps, we make sure that the basic maps remain
9717 * sorted because isl_map_normalize expects the basic maps of the result
9718 * to be sorted.
9720 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9722 int i, j;
9724 map = isl_map_remove_empty_parts(map);
9725 if (!map)
9726 return NULL;
9727 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9728 for (i = map->n - 1; i >= 1; --i) {
9729 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9730 continue;
9731 isl_basic_map_free(map->p[i-1]);
9732 for (j = i; j < map->n; ++j)
9733 map->p[j - 1] = map->p[j];
9734 map->n--;
9737 return map;
9740 /* Remove obvious duplicates among the basic maps of "map".
9742 * Unlike isl_map_normalize, this function does not remove redundant
9743 * constraints and only removes duplicates that have exactly the same
9744 * constraints in the input. It does sort the constraints and
9745 * the basic maps to ease the detection of duplicates.
9747 * If "map" has already been normalized or if the basic maps are
9748 * disjoint, then there can be no duplicates.
9750 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9752 int i;
9753 isl_basic_map *bmap;
9755 if (!map)
9756 return NULL;
9757 if (map->n <= 1)
9758 return map;
9759 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9760 return map;
9761 for (i = 0; i < map->n; ++i) {
9762 bmap = isl_basic_map_copy(map->p[i]);
9763 bmap = isl_basic_map_sort_constraints(bmap);
9764 if (!bmap)
9765 return isl_map_free(map);
9766 isl_basic_map_free(map->p[i]);
9767 map->p[i] = bmap;
9770 map = sort_and_remove_duplicates(map);
9771 return map;
9774 /* We normalize in place, but if anything goes wrong we need
9775 * to return NULL, so we need to make sure we don't change the
9776 * meaning of any possible other copies of map.
9778 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9780 int i;
9781 struct isl_basic_map *bmap;
9783 if (!map)
9784 return NULL;
9785 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9786 return map;
9787 for (i = 0; i < map->n; ++i) {
9788 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9789 if (!bmap)
9790 goto error;
9791 isl_basic_map_free(map->p[i]);
9792 map->p[i] = bmap;
9795 map = sort_and_remove_duplicates(map);
9796 if (map)
9797 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9798 return map;
9799 error:
9800 isl_map_free(map);
9801 return NULL;
9804 struct isl_set *isl_set_normalize(struct isl_set *set)
9806 return set_from_map(isl_map_normalize(set_to_map(set)));
9809 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9810 __isl_keep isl_map *map2)
9812 int i;
9813 isl_bool equal;
9815 if (!map1 || !map2)
9816 return isl_bool_error;
9818 if (map1 == map2)
9819 return isl_bool_true;
9820 if (!isl_space_is_equal(map1->dim, map2->dim))
9821 return isl_bool_false;
9823 map1 = isl_map_copy(map1);
9824 map2 = isl_map_copy(map2);
9825 map1 = isl_map_normalize(map1);
9826 map2 = isl_map_normalize(map2);
9827 if (!map1 || !map2)
9828 goto error;
9829 equal = map1->n == map2->n;
9830 for (i = 0; equal && i < map1->n; ++i) {
9831 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9832 if (equal < 0)
9833 goto error;
9835 isl_map_free(map1);
9836 isl_map_free(map2);
9837 return equal;
9838 error:
9839 isl_map_free(map1);
9840 isl_map_free(map2);
9841 return isl_bool_error;
9844 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9845 __isl_keep isl_set *set2)
9847 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9850 /* Return the basic maps in "map" as a list.
9852 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9853 __isl_keep isl_map *map)
9855 int i;
9856 isl_ctx *ctx;
9857 isl_basic_map_list *list;
9859 if (!map)
9860 return NULL;
9861 ctx = isl_map_get_ctx(map);
9862 list = isl_basic_map_list_alloc(ctx, map->n);
9864 for (i = 0; i < map->n; ++i) {
9865 isl_basic_map *bmap;
9867 bmap = isl_basic_map_copy(map->p[i]);
9868 list = isl_basic_map_list_add(list, bmap);
9871 return list;
9874 /* Return the intersection of the elements in the non-empty list "list".
9875 * All elements are assumed to live in the same space.
9877 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9878 __isl_take isl_basic_map_list *list)
9880 int i;
9881 isl_size n;
9882 isl_basic_map *bmap;
9884 n = isl_basic_map_list_n_basic_map(list);
9885 if (n < 0)
9886 goto error;
9887 if (n < 1)
9888 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9889 "expecting non-empty list", goto error);
9891 bmap = isl_basic_map_list_get_basic_map(list, 0);
9892 for (i = 1; i < n; ++i) {
9893 isl_basic_map *bmap_i;
9895 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9896 bmap = isl_basic_map_intersect(bmap, bmap_i);
9899 isl_basic_map_list_free(list);
9900 return bmap;
9901 error:
9902 isl_basic_map_list_free(list);
9903 return NULL;
9906 /* Return the intersection of the elements in the non-empty list "list".
9907 * All elements are assumed to live in the same space.
9909 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9910 __isl_take isl_basic_set_list *list)
9912 return isl_basic_map_list_intersect(list);
9915 /* Return the union of the elements of "list".
9916 * The list is required to have at least one element.
9918 __isl_give isl_set *isl_basic_set_list_union(
9919 __isl_take isl_basic_set_list *list)
9921 int i;
9922 isl_size n;
9923 isl_space *space;
9924 isl_basic_set *bset;
9925 isl_set *set;
9927 n = isl_basic_set_list_n_basic_set(list);
9928 if (n < 0)
9929 goto error;
9930 if (n < 1)
9931 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9932 "expecting non-empty list", goto error);
9934 bset = isl_basic_set_list_get_basic_set(list, 0);
9935 space = isl_basic_set_get_space(bset);
9936 isl_basic_set_free(bset);
9938 set = isl_set_alloc_space(space, n, 0);
9939 for (i = 0; i < n; ++i) {
9940 bset = isl_basic_set_list_get_basic_set(list, i);
9941 set = isl_set_add_basic_set(set, bset);
9944 isl_basic_set_list_free(list);
9945 return set;
9946 error:
9947 isl_basic_set_list_free(list);
9948 return NULL;
9951 /* Return the union of the elements in the non-empty list "list".
9952 * All elements are assumed to live in the same space.
9954 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9956 int i;
9957 isl_size n;
9958 isl_set *set;
9960 n = isl_set_list_n_set(list);
9961 if (n < 0)
9962 goto error;
9963 if (n < 1)
9964 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9965 "expecting non-empty list", goto error);
9967 set = isl_set_list_get_set(list, 0);
9968 for (i = 1; i < n; ++i) {
9969 isl_set *set_i;
9971 set_i = isl_set_list_get_set(list, i);
9972 set = isl_set_union(set, set_i);
9975 isl_set_list_free(list);
9976 return set;
9977 error:
9978 isl_set_list_free(list);
9979 return NULL;
9982 __isl_give isl_basic_map *isl_basic_map_product(
9983 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9985 isl_space *space_result = NULL;
9986 struct isl_basic_map *bmap;
9987 unsigned in1, in2, out1, out2, nparam, total, pos;
9988 struct isl_dim_map *dim_map1, *dim_map2;
9990 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9991 goto error;
9992 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9993 isl_space_copy(bmap2->dim));
9995 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9996 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9997 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9998 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9999 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10001 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10002 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10003 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10004 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10005 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10006 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10007 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10008 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10009 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10010 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10011 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10013 bmap = isl_basic_map_alloc_space(space_result,
10014 bmap1->n_div + bmap2->n_div,
10015 bmap1->n_eq + bmap2->n_eq,
10016 bmap1->n_ineq + bmap2->n_ineq);
10017 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10018 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10019 bmap = isl_basic_map_simplify(bmap);
10020 return isl_basic_map_finalize(bmap);
10021 error:
10022 isl_basic_map_free(bmap1);
10023 isl_basic_map_free(bmap2);
10024 return NULL;
10027 __isl_give isl_basic_map *isl_basic_map_flat_product(
10028 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10030 isl_basic_map *prod;
10032 prod = isl_basic_map_product(bmap1, bmap2);
10033 prod = isl_basic_map_flatten(prod);
10034 return prod;
10037 __isl_give isl_basic_set *isl_basic_set_flat_product(
10038 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10040 return isl_basic_map_flat_range_product(bset1, bset2);
10043 __isl_give isl_basic_map *isl_basic_map_domain_product(
10044 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10046 isl_space *space1, *space2;
10047 isl_space *space_result = NULL;
10048 isl_basic_map *bmap;
10049 isl_size in1, in2, out, nparam;
10050 unsigned total, pos;
10051 struct isl_dim_map *dim_map1, *dim_map2;
10053 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10054 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10055 out = isl_basic_map_dim(bmap1, isl_dim_out);
10056 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10057 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10058 goto error;
10060 space1 = isl_basic_map_get_space(bmap1);
10061 space2 = isl_basic_map_get_space(bmap2);
10062 space_result = isl_space_domain_product(space1, space2);
10064 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10065 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10066 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10069 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10070 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10073 isl_dim_map_div(dim_map1, bmap1, pos += out);
10074 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10076 bmap = isl_basic_map_alloc_space(space_result,
10077 bmap1->n_div + bmap2->n_div,
10078 bmap1->n_eq + bmap2->n_eq,
10079 bmap1->n_ineq + bmap2->n_ineq);
10080 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10081 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10082 bmap = isl_basic_map_simplify(bmap);
10083 return isl_basic_map_finalize(bmap);
10084 error:
10085 isl_basic_map_free(bmap1);
10086 isl_basic_map_free(bmap2);
10087 return NULL;
10090 __isl_give isl_basic_map *isl_basic_map_range_product(
10091 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10093 isl_bool rational;
10094 isl_space *space_result = NULL;
10095 isl_basic_map *bmap;
10096 isl_size in, out1, out2, nparam;
10097 unsigned total, pos;
10098 struct isl_dim_map *dim_map1, *dim_map2;
10100 rational = isl_basic_map_is_rational(bmap1);
10101 if (rational >= 0 && rational)
10102 rational = isl_basic_map_is_rational(bmap2);
10103 in = isl_basic_map_dim(bmap1, isl_dim_in);
10104 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10105 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10106 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10107 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10108 goto error;
10110 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10111 goto error;
10113 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10114 isl_space_copy(bmap2->dim));
10116 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10117 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10118 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10119 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10120 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10121 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10122 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10123 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10124 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10125 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10126 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10128 bmap = isl_basic_map_alloc_space(space_result,
10129 bmap1->n_div + bmap2->n_div,
10130 bmap1->n_eq + bmap2->n_eq,
10131 bmap1->n_ineq + bmap2->n_ineq);
10132 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10133 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10134 if (rational)
10135 bmap = isl_basic_map_set_rational(bmap);
10136 bmap = isl_basic_map_simplify(bmap);
10137 return isl_basic_map_finalize(bmap);
10138 error:
10139 isl_basic_map_free(bmap1);
10140 isl_basic_map_free(bmap2);
10141 return NULL;
10144 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10145 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10147 isl_basic_map *prod;
10149 prod = isl_basic_map_range_product(bmap1, bmap2);
10150 prod = isl_basic_map_flatten_range(prod);
10151 return prod;
10154 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10155 * and collect the results.
10156 * The result live in the space obtained by calling "space_product"
10157 * on the spaces of "map1" and "map2".
10158 * If "remove_duplicates" is set then the result may contain duplicates
10159 * (even if the inputs do not) and so we try and remove the obvious
10160 * duplicates.
10162 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10163 __isl_take isl_map *map2,
10164 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10165 __isl_take isl_space *right),
10166 __isl_give isl_basic_map *(*basic_map_product)(
10167 __isl_take isl_basic_map *left,
10168 __isl_take isl_basic_map *right),
10169 int remove_duplicates)
10171 unsigned flags = 0;
10172 struct isl_map *result;
10173 int i, j;
10174 isl_bool m;
10176 m = isl_map_has_equal_params(map1, map2);
10177 if (m < 0)
10178 goto error;
10179 if (!m)
10180 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10181 "parameters don't match", goto error);
10183 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10184 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10185 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10187 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10188 isl_space_copy(map2->dim)),
10189 map1->n * map2->n, flags);
10190 if (!result)
10191 goto error;
10192 for (i = 0; i < map1->n; ++i)
10193 for (j = 0; j < map2->n; ++j) {
10194 struct isl_basic_map *part;
10195 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10196 isl_basic_map_copy(map2->p[j]));
10197 if (isl_basic_map_is_empty(part))
10198 isl_basic_map_free(part);
10199 else
10200 result = isl_map_add_basic_map(result, part);
10201 if (!result)
10202 goto error;
10204 if (remove_duplicates)
10205 result = isl_map_remove_obvious_duplicates(result);
10206 isl_map_free(map1);
10207 isl_map_free(map2);
10208 return result;
10209 error:
10210 isl_map_free(map1);
10211 isl_map_free(map2);
10212 return NULL;
10215 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10217 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10218 __isl_take isl_map *map2)
10220 return map_product(map1, map2, &isl_space_product,
10221 &isl_basic_map_product, 0);
10224 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10225 __isl_take isl_map *map2)
10227 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10230 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10232 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10233 __isl_take isl_map *map2)
10235 isl_map *prod;
10237 prod = isl_map_product(map1, map2);
10238 prod = isl_map_flatten(prod);
10239 return prod;
10242 /* Given two set A and B, construct its Cartesian product A x B.
10244 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10246 return isl_map_range_product(set1, set2);
10249 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10250 __isl_take isl_set *set2)
10252 return isl_map_flat_range_product(set1, set2);
10255 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10257 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10258 __isl_take isl_map *map2)
10260 return map_product(map1, map2, &isl_space_domain_product,
10261 &isl_basic_map_domain_product, 1);
10264 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10266 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10267 __isl_take isl_map *map2)
10269 return map_product(map1, map2, &isl_space_range_product,
10270 &isl_basic_map_range_product, 1);
10273 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10274 __isl_take isl_map *map2)
10276 return isl_map_align_params_map_map_and(map1, map2,
10277 &map_domain_product_aligned);
10280 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10281 __isl_take isl_map *map2)
10283 return isl_map_align_params_map_map_and(map1, map2,
10284 &map_range_product_aligned);
10287 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10289 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10291 isl_space *space;
10292 isl_size total1, keep1, total2, keep2;
10294 total1 = isl_map_dim(map, isl_dim_in);
10295 total2 = isl_map_dim(map, isl_dim_out);
10296 if (total1 < 0 || total2 < 0)
10297 return isl_map_free(map);
10298 if (!isl_space_domain_is_wrapping(map->dim) ||
10299 !isl_space_range_is_wrapping(map->dim))
10300 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10301 "not a product", return isl_map_free(map));
10303 space = isl_map_get_space(map);
10304 space = isl_space_factor_domain(space);
10305 keep1 = isl_space_dim(space, isl_dim_in);
10306 keep2 = isl_space_dim(space, isl_dim_out);
10307 if (keep1 < 0 || keep2 < 0)
10308 map = isl_map_free(map);
10309 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10310 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10311 map = isl_map_reset_space(map, space);
10313 return map;
10316 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10318 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10320 isl_space *space;
10321 isl_size total1, keep1, total2, keep2;
10323 total1 = isl_map_dim(map, isl_dim_in);
10324 total2 = isl_map_dim(map, isl_dim_out);
10325 if (total1 < 0 || total2 < 0)
10326 return isl_map_free(map);
10327 if (!isl_space_domain_is_wrapping(map->dim) ||
10328 !isl_space_range_is_wrapping(map->dim))
10329 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10330 "not a product", return isl_map_free(map));
10332 space = isl_map_get_space(map);
10333 space = isl_space_factor_range(space);
10334 keep1 = isl_space_dim(space, isl_dim_in);
10335 keep2 = isl_space_dim(space, isl_dim_out);
10336 if (keep1 < 0 || keep2 < 0)
10337 map = isl_map_free(map);
10338 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10339 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10340 map = isl_map_reset_space(map, space);
10342 return map;
10345 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10347 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10349 isl_space *space;
10350 isl_size total, keep;
10352 total = isl_map_dim(map, isl_dim_in);
10353 if (total < 0)
10354 return isl_map_free(map);
10355 if (!isl_space_domain_is_wrapping(map->dim))
10356 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10357 "domain is not a product", return isl_map_free(map));
10359 space = isl_map_get_space(map);
10360 space = isl_space_domain_factor_domain(space);
10361 keep = isl_space_dim(space, isl_dim_in);
10362 if (keep < 0)
10363 map = isl_map_free(map);
10364 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10365 map = isl_map_reset_space(map, space);
10367 return map;
10370 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10372 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10374 isl_space *space;
10375 isl_size total, keep;
10377 total = isl_map_dim(map, isl_dim_in);
10378 if (total < 0)
10379 return isl_map_free(map);
10380 if (!isl_space_domain_is_wrapping(map->dim))
10381 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10382 "domain is not a product", return isl_map_free(map));
10384 space = isl_map_get_space(map);
10385 space = isl_space_domain_factor_range(space);
10386 keep = isl_space_dim(space, isl_dim_in);
10387 if (keep < 0)
10388 map = isl_map_free(map);
10389 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10390 map = isl_map_reset_space(map, space);
10392 return map;
10395 /* Given a map A -> [B -> C], extract the map A -> B.
10397 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10399 isl_space *space;
10400 isl_size total, keep;
10402 total = isl_map_dim(map, isl_dim_out);
10403 if (total < 0)
10404 return isl_map_free(map);
10405 if (!isl_space_range_is_wrapping(map->dim))
10406 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10407 "range is not a product", return isl_map_free(map));
10409 space = isl_map_get_space(map);
10410 space = isl_space_range_factor_domain(space);
10411 keep = isl_space_dim(space, isl_dim_out);
10412 if (keep < 0)
10413 map = isl_map_free(map);
10414 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10415 map = isl_map_reset_space(map, space);
10417 return map;
10420 /* Given a map A -> [B -> C], extract the map A -> C.
10422 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10424 isl_space *space;
10425 isl_size total, keep;
10427 total = isl_map_dim(map, isl_dim_out);
10428 if (total < 0)
10429 return isl_map_free(map);
10430 if (!isl_space_range_is_wrapping(map->dim))
10431 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10432 "range is not a product", return isl_map_free(map));
10434 space = isl_map_get_space(map);
10435 space = isl_space_range_factor_range(space);
10436 keep = isl_space_dim(space, isl_dim_out);
10437 if (keep < 0)
10438 map = isl_map_free(map);
10439 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10440 map = isl_map_reset_space(map, space);
10442 return map;
10445 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10447 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10448 __isl_take isl_map *map2)
10450 isl_map *prod;
10452 prod = isl_map_domain_product(map1, map2);
10453 prod = isl_map_flatten_domain(prod);
10454 return prod;
10457 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10459 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10460 __isl_take isl_map *map2)
10462 isl_map *prod;
10464 prod = isl_map_range_product(map1, map2);
10465 prod = isl_map_flatten_range(prod);
10466 return prod;
10469 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10471 int i;
10472 uint32_t hash = isl_hash_init();
10473 isl_size total;
10475 if (!bmap)
10476 return 0;
10477 bmap = isl_basic_map_copy(bmap);
10478 bmap = isl_basic_map_normalize(bmap);
10479 total = isl_basic_map_dim(bmap, isl_dim_all);
10480 if (total < 0)
10481 return 0;
10482 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10483 for (i = 0; i < bmap->n_eq; ++i) {
10484 uint32_t c_hash;
10485 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10486 isl_hash_hash(hash, c_hash);
10488 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10489 for (i = 0; i < bmap->n_ineq; ++i) {
10490 uint32_t c_hash;
10491 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10492 isl_hash_hash(hash, c_hash);
10494 isl_hash_byte(hash, bmap->n_div & 0xFF);
10495 for (i = 0; i < bmap->n_div; ++i) {
10496 uint32_t c_hash;
10497 if (isl_int_is_zero(bmap->div[i][0]))
10498 continue;
10499 isl_hash_byte(hash, i & 0xFF);
10500 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10501 isl_hash_hash(hash, c_hash);
10503 isl_basic_map_free(bmap);
10504 return hash;
10507 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10509 return isl_basic_map_get_hash(bset_to_bmap(bset));
10512 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10514 int i;
10515 uint32_t hash;
10517 if (!map)
10518 return 0;
10519 map = isl_map_copy(map);
10520 map = isl_map_normalize(map);
10521 if (!map)
10522 return 0;
10524 hash = isl_hash_init();
10525 for (i = 0; i < map->n; ++i) {
10526 uint32_t bmap_hash;
10527 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10528 isl_hash_hash(hash, bmap_hash);
10531 isl_map_free(map);
10533 return hash;
10536 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10538 return isl_map_get_hash(set_to_map(set));
10541 /* Return the number of basic maps in the (current) representation of "map".
10543 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
10545 return map ? map->n : isl_size_error;
10548 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
10550 return set ? set->n : isl_size_error;
10553 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10554 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10556 int i;
10558 if (!map)
10559 return isl_stat_error;
10561 for (i = 0; i < map->n; ++i)
10562 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10563 return isl_stat_error;
10565 return isl_stat_ok;
10568 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10569 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10571 int i;
10573 if (!set)
10574 return isl_stat_error;
10576 for (i = 0; i < set->n; ++i)
10577 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10578 return isl_stat_error;
10580 return isl_stat_ok;
10583 /* Return a list of basic sets, the union of which is equal to "set".
10585 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10586 __isl_keep isl_set *set)
10588 int i;
10589 isl_basic_set_list *list;
10591 if (!set)
10592 return NULL;
10594 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10595 for (i = 0; i < set->n; ++i) {
10596 isl_basic_set *bset;
10598 bset = isl_basic_set_copy(set->p[i]);
10599 list = isl_basic_set_list_add(list, bset);
10602 return list;
10605 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10607 isl_space *space;
10609 if (!bset)
10610 return NULL;
10612 bset = isl_basic_set_cow(bset);
10613 if (!bset)
10614 return NULL;
10616 space = isl_basic_set_get_space(bset);
10617 space = isl_space_lift(space, bset->n_div);
10618 if (!space)
10619 goto error;
10620 isl_space_free(bset->dim);
10621 bset->dim = space;
10622 bset->extra -= bset->n_div;
10623 bset->n_div = 0;
10625 bset = isl_basic_set_finalize(bset);
10627 return bset;
10628 error:
10629 isl_basic_set_free(bset);
10630 return NULL;
10633 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10635 int i;
10636 isl_space *space;
10637 unsigned n_div;
10639 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10641 if (!set)
10642 return NULL;
10644 set = isl_set_cow(set);
10645 if (!set)
10646 return NULL;
10648 n_div = set->p[0]->n_div;
10649 space = isl_set_get_space(set);
10650 space = isl_space_lift(space, n_div);
10651 if (!space)
10652 goto error;
10653 isl_space_free(set->dim);
10654 set->dim = space;
10656 for (i = 0; i < set->n; ++i) {
10657 set->p[i] = isl_basic_set_lift(set->p[i]);
10658 if (!set->p[i])
10659 goto error;
10662 return set;
10663 error:
10664 isl_set_free(set);
10665 return NULL;
10668 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10670 isl_size dim;
10671 int size = 0;
10673 dim = isl_basic_set_dim(bset, isl_dim_all);
10674 if (dim < 0)
10675 return -1;
10676 size += bset->n_eq * (1 + dim);
10677 size += bset->n_ineq * (1 + dim);
10678 size += bset->n_div * (2 + dim);
10680 return size;
10683 int isl_set_size(__isl_keep isl_set *set)
10685 int i;
10686 int size = 0;
10688 if (!set)
10689 return -1;
10691 for (i = 0; i < set->n; ++i)
10692 size += isl_basic_set_size(set->p[i]);
10694 return size;
10697 /* Check if there is any lower bound (if lower == 0) and/or upper
10698 * bound (if upper == 0) on the specified dim.
10700 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10701 enum isl_dim_type type, unsigned pos, int lower, int upper)
10703 int i;
10705 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10706 return isl_bool_error;
10708 pos += isl_basic_map_offset(bmap, type);
10710 for (i = 0; i < bmap->n_div; ++i) {
10711 if (isl_int_is_zero(bmap->div[i][0]))
10712 continue;
10713 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10714 return isl_bool_true;
10717 for (i = 0; i < bmap->n_eq; ++i)
10718 if (!isl_int_is_zero(bmap->eq[i][pos]))
10719 return isl_bool_true;
10721 for (i = 0; i < bmap->n_ineq; ++i) {
10722 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10723 if (sgn > 0)
10724 lower = 1;
10725 if (sgn < 0)
10726 upper = 1;
10729 return lower && upper;
10732 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10733 enum isl_dim_type type, unsigned pos)
10735 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10738 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10739 enum isl_dim_type type, unsigned pos)
10741 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10744 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10745 enum isl_dim_type type, unsigned pos)
10747 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10750 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10751 enum isl_dim_type type, unsigned pos)
10753 int i;
10755 if (!map)
10756 return isl_bool_error;
10758 for (i = 0; i < map->n; ++i) {
10759 isl_bool bounded;
10760 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10761 if (bounded < 0 || !bounded)
10762 return bounded;
10765 return isl_bool_true;
10768 /* Return true if the specified dim is involved in both an upper bound
10769 * and a lower bound.
10771 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10772 enum isl_dim_type type, unsigned pos)
10774 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10777 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10779 static isl_bool has_any_bound(__isl_keep isl_map *map,
10780 enum isl_dim_type type, unsigned pos,
10781 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10782 enum isl_dim_type type, unsigned pos))
10784 int i;
10786 if (!map)
10787 return isl_bool_error;
10789 for (i = 0; i < map->n; ++i) {
10790 isl_bool bounded;
10791 bounded = fn(map->p[i], type, pos);
10792 if (bounded < 0 || bounded)
10793 return bounded;
10796 return isl_bool_false;
10799 /* Return 1 if the specified dim is involved in any lower bound.
10801 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10802 enum isl_dim_type type, unsigned pos)
10804 return has_any_bound(set, type, pos,
10805 &isl_basic_map_dim_has_lower_bound);
10808 /* Return 1 if the specified dim is involved in any upper bound.
10810 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10811 enum isl_dim_type type, unsigned pos)
10813 return has_any_bound(set, type, pos,
10814 &isl_basic_map_dim_has_upper_bound);
10817 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10819 static isl_bool has_bound(__isl_keep isl_map *map,
10820 enum isl_dim_type type, unsigned pos,
10821 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10822 enum isl_dim_type type, unsigned pos))
10824 int i;
10826 if (!map)
10827 return isl_bool_error;
10829 for (i = 0; i < map->n; ++i) {
10830 isl_bool bounded;
10831 bounded = fn(map->p[i], type, pos);
10832 if (bounded < 0 || !bounded)
10833 return bounded;
10836 return isl_bool_true;
10839 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10841 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10842 enum isl_dim_type type, unsigned pos)
10844 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10847 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10849 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10850 enum isl_dim_type type, unsigned pos)
10852 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10855 /* For each of the "n" variables starting at "first", determine
10856 * the sign of the variable and put the results in the first "n"
10857 * elements of the array "signs".
10858 * Sign
10859 * 1 means that the variable is non-negative
10860 * -1 means that the variable is non-positive
10861 * 0 means the variable attains both positive and negative values.
10863 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10864 unsigned first, unsigned n, int *signs)
10866 isl_vec *bound = NULL;
10867 struct isl_tab *tab = NULL;
10868 struct isl_tab_undo *snap;
10869 int i;
10870 isl_size total;
10872 total = isl_basic_set_dim(bset, isl_dim_all);
10873 if (total < 0 || !signs)
10874 return isl_stat_error;
10876 bound = isl_vec_alloc(bset->ctx, 1 + total);
10877 tab = isl_tab_from_basic_set(bset, 0);
10878 if (!bound || !tab)
10879 goto error;
10881 isl_seq_clr(bound->el, bound->size);
10882 isl_int_set_si(bound->el[0], -1);
10884 snap = isl_tab_snap(tab);
10885 for (i = 0; i < n; ++i) {
10886 int empty;
10888 isl_int_set_si(bound->el[1 + first + i], -1);
10889 if (isl_tab_add_ineq(tab, bound->el) < 0)
10890 goto error;
10891 empty = tab->empty;
10892 isl_int_set_si(bound->el[1 + first + i], 0);
10893 if (isl_tab_rollback(tab, snap) < 0)
10894 goto error;
10896 if (empty) {
10897 signs[i] = 1;
10898 continue;
10901 isl_int_set_si(bound->el[1 + first + i], 1);
10902 if (isl_tab_add_ineq(tab, bound->el) < 0)
10903 goto error;
10904 empty = tab->empty;
10905 isl_int_set_si(bound->el[1 + first + i], 0);
10906 if (isl_tab_rollback(tab, snap) < 0)
10907 goto error;
10909 signs[i] = empty ? -1 : 0;
10912 isl_tab_free(tab);
10913 isl_vec_free(bound);
10914 return isl_stat_ok;
10915 error:
10916 isl_tab_free(tab);
10917 isl_vec_free(bound);
10918 return isl_stat_error;
10921 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10922 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10924 if (!bset || !signs)
10925 return isl_stat_error;
10926 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10927 return isl_stat_error;
10929 first += pos(bset->dim, type) - 1;
10930 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10933 /* Is it possible for the integer division "div" to depend (possibly
10934 * indirectly) on any output dimensions?
10936 * If the div is undefined, then we conservatively assume that it
10937 * may depend on them.
10938 * Otherwise, we check if it actually depends on them or on any integer
10939 * divisions that may depend on them.
10941 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10943 int i;
10944 isl_size n_out, n_div;
10945 unsigned o_out, o_div;
10947 if (isl_int_is_zero(bmap->div[div][0]))
10948 return isl_bool_true;
10950 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10951 if (n_out < 0)
10952 return isl_bool_error;
10953 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10955 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10956 return isl_bool_true;
10958 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10959 if (n_div < 0)
10960 return isl_bool_error;
10961 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10963 for (i = 0; i < n_div; ++i) {
10964 isl_bool may_involve;
10966 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10967 continue;
10968 may_involve = div_may_involve_output(bmap, i);
10969 if (may_involve < 0 || may_involve)
10970 return may_involve;
10973 return isl_bool_false;
10976 /* Return the first integer division of "bmap" in the range
10977 * [first, first + n[ that may depend on any output dimensions and
10978 * that has a non-zero coefficient in "c" (where the first coefficient
10979 * in "c" corresponds to integer division "first").
10981 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10982 isl_int *c, int first, int n)
10984 int k;
10986 if (!bmap)
10987 return -1;
10989 for (k = first; k < first + n; ++k) {
10990 isl_bool may_involve;
10992 if (isl_int_is_zero(c[k]))
10993 continue;
10994 may_involve = div_may_involve_output(bmap, k);
10995 if (may_involve < 0)
10996 return -1;
10997 if (may_involve)
10998 return k;
11001 return first + n;
11004 /* Look for a pair of inequality constraints in "bmap" of the form
11006 * -l + i >= 0 or i >= l
11007 * and
11008 * n + l - i >= 0 or i <= l + n
11010 * with n < "m" and i the output dimension at position "pos".
11011 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11012 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11013 * and earlier output dimensions, as well as integer divisions that do
11014 * not involve any of the output dimensions.
11016 * Return the index of the first inequality constraint or bmap->n_ineq
11017 * if no such pair can be found.
11019 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11020 int pos, isl_int m)
11022 int i, j;
11023 isl_ctx *ctx;
11024 isl_size total;
11025 isl_size n_div, n_out;
11026 unsigned o_div, o_out;
11027 int less;
11029 total = isl_basic_map_dim(bmap, isl_dim_all);
11030 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11031 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11032 if (total < 0 || n_out < 0 || n_div < 0)
11033 return -1;
11035 ctx = isl_basic_map_get_ctx(bmap);
11036 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11037 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11038 for (i = 0; i < bmap->n_ineq; ++i) {
11039 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11040 continue;
11041 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11042 n_out - (pos + 1)) != -1)
11043 continue;
11044 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11045 0, n_div) < n_div)
11046 continue;
11047 for (j = i + 1; j < bmap->n_ineq; ++j) {
11048 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11049 ctx->one))
11050 continue;
11051 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11052 bmap->ineq[j] + 1, total))
11053 continue;
11054 break;
11056 if (j >= bmap->n_ineq)
11057 continue;
11058 isl_int_add(bmap->ineq[i][0],
11059 bmap->ineq[i][0], bmap->ineq[j][0]);
11060 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11061 isl_int_sub(bmap->ineq[i][0],
11062 bmap->ineq[i][0], bmap->ineq[j][0]);
11063 if (!less)
11064 continue;
11065 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11066 return i;
11067 else
11068 return j;
11071 return bmap->n_ineq;
11074 /* Return the index of the equality of "bmap" that defines
11075 * the output dimension "pos" in terms of earlier dimensions.
11076 * The equality may also involve integer divisions, as long
11077 * as those integer divisions are defined in terms of
11078 * parameters or input dimensions.
11079 * In this case, *div is set to the number of integer divisions and
11080 * *ineq is set to the number of inequality constraints (provided
11081 * div and ineq are not NULL).
11083 * The equality may also involve a single integer division involving
11084 * the output dimensions (typically only output dimension "pos") as
11085 * long as the coefficient of output dimension "pos" is 1 or -1 and
11086 * there is a pair of constraints i >= l and i <= l + n, with i referring
11087 * to output dimension "pos", l an expression involving only earlier
11088 * dimensions and n smaller than the coefficient of the integer division
11089 * in the equality. In this case, the output dimension can be defined
11090 * in terms of a modulo expression that does not involve the integer division.
11091 * *div is then set to this single integer division and
11092 * *ineq is set to the index of constraint i >= l.
11094 * Return bmap->n_eq if there is no such equality.
11095 * Return -1 on error.
11097 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11098 int pos, int *div, int *ineq)
11100 int j, k, l;
11101 isl_size n_div, n_out;
11102 unsigned o_div, o_out;
11104 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11105 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11106 if (n_out < 0 || n_div < 0)
11107 return -1;
11109 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11110 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11112 if (ineq)
11113 *ineq = bmap->n_ineq;
11114 if (div)
11115 *div = n_div;
11116 for (j = 0; j < bmap->n_eq; ++j) {
11117 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11118 continue;
11119 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11120 n_out - (pos + 1)) != -1)
11121 continue;
11122 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11123 0, n_div);
11124 if (k >= n_div)
11125 return j;
11126 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11127 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11128 continue;
11129 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11130 k + 1, n_div - (k+1)) < n_div)
11131 continue;
11132 l = find_modulo_constraint_pair(bmap, pos,
11133 bmap->eq[j][o_div + k]);
11134 if (l < 0)
11135 return -1;
11136 if (l >= bmap->n_ineq)
11137 continue;
11138 if (div)
11139 *div = k;
11140 if (ineq)
11141 *ineq = l;
11142 return j;
11145 return bmap->n_eq;
11148 /* Check if the given basic map is obviously single-valued.
11149 * In particular, for each output dimension, check that there is
11150 * an equality that defines the output dimension in terms of
11151 * earlier dimensions.
11153 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11155 int i;
11156 isl_size n_out;
11158 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11159 if (n_out < 0)
11160 return isl_bool_error;
11162 for (i = 0; i < n_out; ++i) {
11163 int eq;
11165 eq = isl_basic_map_output_defining_equality(bmap, i,
11166 NULL, NULL);
11167 if (eq < 0)
11168 return isl_bool_error;
11169 if (eq >= bmap->n_eq)
11170 return isl_bool_false;
11173 return isl_bool_true;
11176 /* Check if the given basic map is single-valued.
11177 * We simply compute
11179 * M \circ M^-1
11181 * and check if the result is a subset of the identity mapping.
11183 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11185 isl_space *space;
11186 isl_basic_map *test;
11187 isl_basic_map *id;
11188 isl_bool sv;
11190 sv = isl_basic_map_plain_is_single_valued(bmap);
11191 if (sv < 0 || sv)
11192 return sv;
11194 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11195 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11197 space = isl_basic_map_get_space(bmap);
11198 space = isl_space_map_from_set(isl_space_range(space));
11199 id = isl_basic_map_identity(space);
11201 sv = isl_basic_map_is_subset(test, id);
11203 isl_basic_map_free(test);
11204 isl_basic_map_free(id);
11206 return sv;
11209 /* Check if the given map is obviously single-valued.
11211 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11213 if (!map)
11214 return isl_bool_error;
11215 if (map->n == 0)
11216 return isl_bool_true;
11217 if (map->n >= 2)
11218 return isl_bool_false;
11220 return isl_basic_map_plain_is_single_valued(map->p[0]);
11223 /* Check if the given map is single-valued.
11224 * We simply compute
11226 * M \circ M^-1
11228 * and check if the result is a subset of the identity mapping.
11230 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11232 isl_space *dim;
11233 isl_map *test;
11234 isl_map *id;
11235 isl_bool sv;
11237 sv = isl_map_plain_is_single_valued(map);
11238 if (sv < 0 || sv)
11239 return sv;
11241 test = isl_map_reverse(isl_map_copy(map));
11242 test = isl_map_apply_range(test, isl_map_copy(map));
11244 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11245 id = isl_map_identity(dim);
11247 sv = isl_map_is_subset(test, id);
11249 isl_map_free(test);
11250 isl_map_free(id);
11252 return sv;
11255 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11257 isl_bool in;
11259 map = isl_map_copy(map);
11260 map = isl_map_reverse(map);
11261 in = isl_map_is_single_valued(map);
11262 isl_map_free(map);
11264 return in;
11267 /* Check if the given map is obviously injective.
11269 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11271 isl_bool in;
11273 map = isl_map_copy(map);
11274 map = isl_map_reverse(map);
11275 in = isl_map_plain_is_single_valued(map);
11276 isl_map_free(map);
11278 return in;
11281 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11283 isl_bool sv;
11285 sv = isl_map_is_single_valued(map);
11286 if (sv < 0 || !sv)
11287 return sv;
11289 return isl_map_is_injective(map);
11292 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11294 return isl_map_is_single_valued(set_to_map(set));
11297 /* Does "map" only map elements to themselves?
11299 * If the domain and range spaces are different, then "map"
11300 * is considered not to be an identity relation, even if it is empty.
11301 * Otherwise, construct the maximal identity relation and
11302 * check whether "map" is a subset of this relation.
11304 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11306 isl_space *space;
11307 isl_map *id;
11308 isl_bool equal, is_identity;
11310 space = isl_map_get_space(map);
11311 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11312 isl_space_free(space);
11313 if (equal < 0 || !equal)
11314 return equal;
11316 id = isl_map_identity(isl_map_get_space(map));
11317 is_identity = isl_map_is_subset(map, id);
11318 isl_map_free(id);
11320 return is_identity;
11323 int isl_map_is_translation(__isl_keep isl_map *map)
11325 int ok;
11326 isl_set *delta;
11328 delta = isl_map_deltas(isl_map_copy(map));
11329 ok = isl_set_is_singleton(delta);
11330 isl_set_free(delta);
11332 return ok;
11335 static int unique(isl_int *p, unsigned pos, unsigned len)
11337 if (isl_seq_first_non_zero(p, pos) != -1)
11338 return 0;
11339 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11340 return 0;
11341 return 1;
11344 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11346 int i, j;
11347 isl_size nvar, n_div;
11348 unsigned ovar;
11350 n_div = isl_basic_set_dim(bset, isl_dim_div);
11351 if (n_div < 0)
11352 return isl_bool_error;
11353 if (n_div != 0)
11354 return isl_bool_false;
11356 nvar = isl_basic_set_dim(bset, isl_dim_set);
11357 if (nvar < 0)
11358 return isl_bool_error;
11359 ovar = isl_space_offset(bset->dim, isl_dim_set);
11360 for (j = 0; j < nvar; ++j) {
11361 int lower = 0, upper = 0;
11362 for (i = 0; i < bset->n_eq; ++i) {
11363 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11364 continue;
11365 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11366 return isl_bool_false;
11367 break;
11369 if (i < bset->n_eq)
11370 continue;
11371 for (i = 0; i < bset->n_ineq; ++i) {
11372 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11373 continue;
11374 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11375 return isl_bool_false;
11376 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11377 lower = 1;
11378 else
11379 upper = 1;
11381 if (!lower || !upper)
11382 return isl_bool_false;
11385 return isl_bool_true;
11388 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11390 if (!set)
11391 return isl_bool_error;
11392 if (set->n != 1)
11393 return isl_bool_false;
11395 return isl_basic_set_is_box(set->p[0]);
11398 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11400 if (!bset)
11401 return isl_bool_error;
11403 return isl_space_is_wrapping(bset->dim);
11406 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11408 if (!set)
11409 return isl_bool_error;
11411 return isl_space_is_wrapping(set->dim);
11414 /* Modify the space of "map" through a call to "change".
11415 * If "can_change" is set (not NULL), then first call it to check
11416 * if the modification is allowed, printing the error message "cannot_change"
11417 * if it is not.
11419 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11420 isl_bool (*can_change)(__isl_keep isl_map *map),
11421 const char *cannot_change,
11422 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11424 isl_bool ok;
11425 isl_space *space;
11427 if (!map)
11428 return NULL;
11430 ok = can_change ? can_change(map) : isl_bool_true;
11431 if (ok < 0)
11432 return isl_map_free(map);
11433 if (!ok)
11434 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11435 return isl_map_free(map));
11437 space = change(isl_map_get_space(map));
11438 map = isl_map_reset_space(map, space);
11440 return map;
11443 /* Is the domain of "map" a wrapped relation?
11445 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11447 if (!map)
11448 return isl_bool_error;
11450 return isl_space_domain_is_wrapping(map->dim);
11453 /* Does "map" have a wrapped relation in both domain and range?
11455 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11457 return isl_space_is_product(isl_map_peek_space(map));
11460 /* Is the range of "map" a wrapped relation?
11462 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11464 if (!map)
11465 return isl_bool_error;
11467 return isl_space_range_is_wrapping(map->dim);
11470 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11472 bmap = isl_basic_map_cow(bmap);
11473 if (!bmap)
11474 return NULL;
11476 bmap->dim = isl_space_wrap(bmap->dim);
11477 if (!bmap->dim)
11478 goto error;
11480 bmap = isl_basic_map_finalize(bmap);
11482 return bset_from_bmap(bmap);
11483 error:
11484 isl_basic_map_free(bmap);
11485 return NULL;
11488 /* Given a map A -> B, return the set (A -> B).
11490 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11492 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11495 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11497 bset = isl_basic_set_cow(bset);
11498 if (!bset)
11499 return NULL;
11501 bset->dim = isl_space_unwrap(bset->dim);
11502 if (!bset->dim)
11503 goto error;
11505 bset = isl_basic_set_finalize(bset);
11507 return bset_to_bmap(bset);
11508 error:
11509 isl_basic_set_free(bset);
11510 return NULL;
11513 /* Given a set (A -> B), return the map A -> B.
11514 * Error out if "set" is not of the form (A -> B).
11516 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11518 return isl_map_change_space(set, &isl_set_is_wrapping,
11519 "not a wrapping set", &isl_space_unwrap);
11522 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11523 enum isl_dim_type type)
11525 if (!bmap)
11526 return NULL;
11528 if (!isl_space_is_named_or_nested(bmap->dim, type))
11529 return bmap;
11531 bmap = isl_basic_map_cow(bmap);
11532 if (!bmap)
11533 return NULL;
11535 bmap->dim = isl_space_reset(bmap->dim, type);
11536 if (!bmap->dim)
11537 goto error;
11539 bmap = isl_basic_map_finalize(bmap);
11541 return bmap;
11542 error:
11543 isl_basic_map_free(bmap);
11544 return NULL;
11547 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11548 enum isl_dim_type type)
11550 int i;
11552 if (!map)
11553 return NULL;
11555 if (!isl_space_is_named_or_nested(map->dim, type))
11556 return map;
11558 map = isl_map_cow(map);
11559 if (!map)
11560 return NULL;
11562 for (i = 0; i < map->n; ++i) {
11563 map->p[i] = isl_basic_map_reset(map->p[i], type);
11564 if (!map->p[i])
11565 goto error;
11567 map->dim = isl_space_reset(map->dim, type);
11568 if (!map->dim)
11569 goto error;
11571 return map;
11572 error:
11573 isl_map_free(map);
11574 return NULL;
11577 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11579 if (!bmap)
11580 return NULL;
11582 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11583 return bmap;
11585 bmap = isl_basic_map_cow(bmap);
11586 if (!bmap)
11587 return NULL;
11589 bmap->dim = isl_space_flatten(bmap->dim);
11590 if (!bmap->dim)
11591 goto error;
11593 bmap = isl_basic_map_finalize(bmap);
11595 return bmap;
11596 error:
11597 isl_basic_map_free(bmap);
11598 return NULL;
11601 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11603 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11606 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11607 __isl_take isl_basic_map *bmap)
11609 if (!bmap)
11610 return NULL;
11612 if (!bmap->dim->nested[0])
11613 return bmap;
11615 bmap = isl_basic_map_cow(bmap);
11616 if (!bmap)
11617 return NULL;
11619 bmap->dim = isl_space_flatten_domain(bmap->dim);
11620 if (!bmap->dim)
11621 goto error;
11623 bmap = isl_basic_map_finalize(bmap);
11625 return bmap;
11626 error:
11627 isl_basic_map_free(bmap);
11628 return NULL;
11631 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11632 __isl_take isl_basic_map *bmap)
11634 if (!bmap)
11635 return NULL;
11637 if (!bmap->dim->nested[1])
11638 return bmap;
11640 bmap = isl_basic_map_cow(bmap);
11641 if (!bmap)
11642 return NULL;
11644 bmap->dim = isl_space_flatten_range(bmap->dim);
11645 if (!bmap->dim)
11646 goto error;
11648 bmap = isl_basic_map_finalize(bmap);
11650 return bmap;
11651 error:
11652 isl_basic_map_free(bmap);
11653 return NULL;
11656 /* Remove any internal structure from the spaces of domain and range of "map".
11658 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11660 if (!map)
11661 return NULL;
11663 if (!map->dim->nested[0] && !map->dim->nested[1])
11664 return map;
11666 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11669 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11671 return set_from_map(isl_map_flatten(set_to_map(set)));
11674 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11676 isl_space *space, *flat_space;
11677 isl_map *map;
11679 space = isl_set_get_space(set);
11680 flat_space = isl_space_flatten(isl_space_copy(space));
11681 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11682 flat_space));
11683 map = isl_map_intersect_domain(map, set);
11685 return map;
11688 /* Remove any internal structure from the space of the domain of "map".
11690 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11692 if (!map)
11693 return NULL;
11695 if (!map->dim->nested[0])
11696 return map;
11698 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11701 /* Remove any internal structure from the space of the range of "map".
11703 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11705 if (!map)
11706 return NULL;
11708 if (!map->dim->nested[1])
11709 return map;
11711 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11714 /* Reorder the dimensions of "bmap" according to the given dim_map
11715 * and set the dimension specification to "space" and
11716 * perform Gaussian elimination on the result.
11718 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11719 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11721 isl_basic_map *res;
11722 unsigned flags;
11723 isl_size n_div;
11725 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11726 if (n_div < 0 || !space || !dim_map)
11727 goto error;
11729 flags = bmap->flags;
11730 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11731 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11732 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11733 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11734 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11735 if (res)
11736 res->flags = flags;
11737 res = isl_basic_map_gauss(res, NULL);
11738 res = isl_basic_map_finalize(res);
11739 return res;
11740 error:
11741 isl_dim_map_free(dim_map);
11742 isl_basic_map_free(bmap);
11743 isl_space_free(space);
11744 return NULL;
11747 /* Reorder the dimensions of "map" according to given reordering.
11749 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11750 __isl_take isl_reordering *r)
11752 int i;
11753 struct isl_dim_map *dim_map;
11755 map = isl_map_cow(map);
11756 dim_map = isl_dim_map_from_reordering(r);
11757 if (!map || !r || !dim_map)
11758 goto error;
11760 for (i = 0; i < map->n; ++i) {
11761 struct isl_dim_map *dim_map_i;
11762 isl_space *space;
11764 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11766 space = isl_reordering_get_space(r);
11767 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11769 if (!map->p[i])
11770 goto error;
11773 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11774 map = isl_map_unmark_normalized(map);
11776 isl_reordering_free(r);
11777 isl_dim_map_free(dim_map);
11778 return map;
11779 error:
11780 isl_dim_map_free(dim_map);
11781 isl_map_free(map);
11782 isl_reordering_free(r);
11783 return NULL;
11786 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11787 __isl_take isl_reordering *r)
11789 return set_from_map(isl_map_realign(set_to_map(set), r));
11792 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11793 __isl_take isl_space *model)
11795 isl_ctx *ctx;
11796 isl_bool aligned;
11798 if (!map || !model)
11799 goto error;
11801 ctx = isl_space_get_ctx(model);
11802 if (!isl_space_has_named_params(model))
11803 isl_die(ctx, isl_error_invalid,
11804 "model has unnamed parameters", goto error);
11805 if (isl_map_check_named_params(map) < 0)
11806 goto error;
11807 aligned = isl_map_space_has_equal_params(map, model);
11808 if (aligned < 0)
11809 goto error;
11810 if (!aligned) {
11811 isl_reordering *exp;
11813 exp = isl_parameter_alignment_reordering(map->dim, model);
11814 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11815 map = isl_map_realign(map, exp);
11818 isl_space_free(model);
11819 return map;
11820 error:
11821 isl_space_free(model);
11822 isl_map_free(map);
11823 return NULL;
11826 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11827 __isl_take isl_space *model)
11829 return isl_map_align_params(set, model);
11832 /* Align the parameters of "bmap" to those of "model", introducing
11833 * additional parameters if needed.
11835 __isl_give isl_basic_map *isl_basic_map_align_params(
11836 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11838 isl_ctx *ctx;
11839 isl_bool equal_params;
11841 if (!bmap || !model)
11842 goto error;
11844 ctx = isl_space_get_ctx(model);
11845 if (!isl_space_has_named_params(model))
11846 isl_die(ctx, isl_error_invalid,
11847 "model has unnamed parameters", goto error);
11848 if (isl_basic_map_check_named_params(bmap) < 0)
11849 goto error;
11850 equal_params = isl_space_has_equal_params(bmap->dim, model);
11851 if (equal_params < 0)
11852 goto error;
11853 if (!equal_params) {
11854 isl_reordering *exp;
11855 struct isl_dim_map *dim_map;
11857 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11858 exp = isl_reordering_extend_space(exp,
11859 isl_basic_map_get_space(bmap));
11860 dim_map = isl_dim_map_from_reordering(exp);
11861 bmap = isl_basic_map_realign(bmap,
11862 isl_reordering_get_space(exp),
11863 isl_dim_map_extend(dim_map, bmap));
11864 isl_reordering_free(exp);
11865 isl_dim_map_free(dim_map);
11868 isl_space_free(model);
11869 return bmap;
11870 error:
11871 isl_space_free(model);
11872 isl_basic_map_free(bmap);
11873 return NULL;
11876 /* Do "bset" and "space" have the same parameters?
11878 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11879 __isl_keep isl_space *space)
11881 isl_space *bset_space;
11883 bset_space = isl_basic_set_peek_space(bset);
11884 return isl_space_has_equal_params(bset_space, space);
11887 /* Do "map" and "space" have the same parameters?
11889 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11890 __isl_keep isl_space *space)
11892 isl_space *map_space;
11894 map_space = isl_map_peek_space(map);
11895 return isl_space_has_equal_params(map_space, space);
11898 /* Do "set" and "space" have the same parameters?
11900 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11901 __isl_keep isl_space *space)
11903 return isl_map_space_has_equal_params(set_to_map(set), space);
11906 /* Align the parameters of "bset" to those of "model", introducing
11907 * additional parameters if needed.
11909 __isl_give isl_basic_set *isl_basic_set_align_params(
11910 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11912 return isl_basic_map_align_params(bset, model);
11915 /* Drop all parameters not referenced by "map".
11917 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11919 int i;
11920 isl_size n;
11922 n = isl_map_dim(map, isl_dim_param);
11923 if (isl_map_check_named_params(map) < 0 || n < 0)
11924 return isl_map_free(map);
11926 for (i = n - 1; i >= 0; i--) {
11927 isl_bool involves;
11929 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11930 if (involves < 0)
11931 return isl_map_free(map);
11932 if (!involves)
11933 map = isl_map_project_out(map, isl_dim_param, i, 1);
11936 return map;
11939 /* Drop all parameters not referenced by "set".
11941 __isl_give isl_set *isl_set_drop_unused_params(
11942 __isl_take isl_set *set)
11944 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11947 /* Drop all parameters not referenced by "bmap".
11949 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11950 __isl_take isl_basic_map *bmap)
11952 isl_size nparam;
11953 int i;
11955 nparam = isl_basic_map_dim(bmap, isl_dim_param);
11956 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
11957 return isl_basic_map_free(bmap);
11959 for (i = nparam - 1; i >= 0; i--) {
11960 isl_bool involves;
11962 involves = isl_basic_map_involves_dims(bmap,
11963 isl_dim_param, i, 1);
11964 if (involves < 0)
11965 return isl_basic_map_free(bmap);
11966 if (!involves)
11967 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11970 return bmap;
11973 /* Drop all parameters not referenced by "bset".
11975 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11976 __isl_take isl_basic_set *bset)
11978 return bset_from_bmap(isl_basic_map_drop_unused_params(
11979 bset_to_bmap(bset)));
11982 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11983 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11984 enum isl_dim_type c2, enum isl_dim_type c3,
11985 enum isl_dim_type c4, enum isl_dim_type c5)
11987 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11988 struct isl_mat *mat;
11989 int i, j, k;
11990 int pos;
11991 isl_size total;
11993 total = isl_basic_map_dim(bmap, isl_dim_all);
11994 if (total < 0)
11995 return NULL;
11996 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
11997 if (!mat)
11998 return NULL;
11999 for (i = 0; i < bmap->n_eq; ++i)
12000 for (j = 0, pos = 0; j < 5; ++j) {
12001 int off = isl_basic_map_offset(bmap, c[j]);
12002 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12003 if (dim < 0)
12004 return isl_mat_free(mat);
12005 for (k = 0; k < dim; ++k) {
12006 isl_int_set(mat->row[i][pos],
12007 bmap->eq[i][off + k]);
12008 ++pos;
12012 return mat;
12015 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12016 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12017 enum isl_dim_type c2, enum isl_dim_type c3,
12018 enum isl_dim_type c4, enum isl_dim_type c5)
12020 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12021 struct isl_mat *mat;
12022 int i, j, k;
12023 int pos;
12024 isl_size total;
12026 total = isl_basic_map_dim(bmap, isl_dim_all);
12027 if (total < 0)
12028 return NULL;
12029 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12030 if (!mat)
12031 return NULL;
12032 for (i = 0; i < bmap->n_ineq; ++i)
12033 for (j = 0, pos = 0; j < 5; ++j) {
12034 int off = isl_basic_map_offset(bmap, c[j]);
12035 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12036 if (dim < 0)
12037 return isl_mat_free(mat);
12038 for (k = 0; k < dim; ++k) {
12039 isl_int_set(mat->row[i][pos],
12040 bmap->ineq[i][off + k]);
12041 ++pos;
12045 return mat;
12048 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12049 __isl_take isl_space *space,
12050 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12051 enum isl_dim_type c2, enum isl_dim_type c3,
12052 enum isl_dim_type c4, enum isl_dim_type c5)
12054 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12055 isl_basic_map *bmap = NULL;
12056 isl_size dim;
12057 unsigned total;
12058 unsigned extra;
12059 int i, j, k, l;
12060 int pos;
12062 dim = isl_space_dim(space, isl_dim_all);
12063 if (dim < 0 || !eq || !ineq)
12064 goto error;
12066 if (eq->n_col != ineq->n_col)
12067 isl_die(space->ctx, isl_error_invalid,
12068 "equalities and inequalities matrices should have "
12069 "same number of columns", goto error);
12071 total = 1 + dim;
12073 if (eq->n_col < total)
12074 isl_die(space->ctx, isl_error_invalid,
12075 "number of columns too small", goto error);
12077 extra = eq->n_col - total;
12079 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12080 eq->n_row, ineq->n_row);
12081 if (!bmap)
12082 goto error;
12083 for (i = 0; i < extra; ++i) {
12084 k = isl_basic_map_alloc_div(bmap);
12085 if (k < 0)
12086 goto error;
12087 isl_int_set_si(bmap->div[k][0], 0);
12089 for (i = 0; i < eq->n_row; ++i) {
12090 l = isl_basic_map_alloc_equality(bmap);
12091 if (l < 0)
12092 goto error;
12093 for (j = 0, pos = 0; j < 5; ++j) {
12094 int off = isl_basic_map_offset(bmap, c[j]);
12095 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12096 if (dim < 0)
12097 goto error;
12098 for (k = 0; k < dim; ++k) {
12099 isl_int_set(bmap->eq[l][off + k],
12100 eq->row[i][pos]);
12101 ++pos;
12105 for (i = 0; i < ineq->n_row; ++i) {
12106 l = isl_basic_map_alloc_inequality(bmap);
12107 if (l < 0)
12108 goto error;
12109 for (j = 0, pos = 0; j < 5; ++j) {
12110 int off = isl_basic_map_offset(bmap, c[j]);
12111 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12112 if (dim < 0)
12113 goto error;
12114 for (k = 0; k < dim; ++k) {
12115 isl_int_set(bmap->ineq[l][off + k],
12116 ineq->row[i][pos]);
12117 ++pos;
12122 isl_space_free(space);
12123 isl_mat_free(eq);
12124 isl_mat_free(ineq);
12126 bmap = isl_basic_map_simplify(bmap);
12127 return isl_basic_map_finalize(bmap);
12128 error:
12129 isl_space_free(space);
12130 isl_mat_free(eq);
12131 isl_mat_free(ineq);
12132 isl_basic_map_free(bmap);
12133 return NULL;
12136 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12137 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12138 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12140 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12141 c1, c2, c3, c4, isl_dim_in);
12144 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12145 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12146 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12148 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12149 c1, c2, c3, c4, isl_dim_in);
12152 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12153 __isl_take isl_space *dim,
12154 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12155 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12157 isl_basic_map *bmap;
12158 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12159 c1, c2, c3, c4, isl_dim_in);
12160 return bset_from_bmap(bmap);
12163 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12165 if (!bmap)
12166 return isl_bool_error;
12168 return isl_space_can_zip(bmap->dim);
12171 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12173 if (!map)
12174 return isl_bool_error;
12176 return isl_space_can_zip(map->dim);
12179 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12180 * (A -> C) -> (B -> D).
12182 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12184 unsigned pos;
12185 isl_size n_in;
12186 isl_size n1;
12187 isl_size n2;
12189 if (!bmap)
12190 return NULL;
12192 if (!isl_basic_map_can_zip(bmap))
12193 isl_die(bmap->ctx, isl_error_invalid,
12194 "basic map cannot be zipped", goto error);
12195 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12196 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12197 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12198 if (n_in < 0 || n1 < 0 || n2 < 0)
12199 return isl_basic_map_free(bmap);
12200 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12201 bmap = isl_basic_map_cow(bmap);
12202 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12203 if (!bmap)
12204 return NULL;
12205 bmap->dim = isl_space_zip(bmap->dim);
12206 if (!bmap->dim)
12207 goto error;
12208 bmap = isl_basic_map_mark_final(bmap);
12209 return bmap;
12210 error:
12211 isl_basic_map_free(bmap);
12212 return NULL;
12215 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12216 * (A -> C) -> (B -> D).
12218 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12220 int i;
12222 if (!map)
12223 return NULL;
12225 if (!isl_map_can_zip(map))
12226 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12227 goto error);
12229 map = isl_map_cow(map);
12230 if (!map)
12231 return NULL;
12233 for (i = 0; i < map->n; ++i) {
12234 map->p[i] = isl_basic_map_zip(map->p[i]);
12235 if (!map->p[i])
12236 goto error;
12239 map->dim = isl_space_zip(map->dim);
12240 if (!map->dim)
12241 goto error;
12243 return map;
12244 error:
12245 isl_map_free(map);
12246 return NULL;
12249 /* Can we apply isl_basic_map_curry to "bmap"?
12250 * That is, does it have a nested relation in its domain?
12252 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12254 if (!bmap)
12255 return isl_bool_error;
12257 return isl_space_can_curry(bmap->dim);
12260 /* Can we apply isl_map_curry to "map"?
12261 * That is, does it have a nested relation in its domain?
12263 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12265 if (!map)
12266 return isl_bool_error;
12268 return isl_space_can_curry(map->dim);
12271 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12272 * A -> (B -> C).
12274 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12277 if (!bmap)
12278 return NULL;
12280 if (!isl_basic_map_can_curry(bmap))
12281 isl_die(bmap->ctx, isl_error_invalid,
12282 "basic map cannot be curried", goto error);
12283 bmap = isl_basic_map_cow(bmap);
12284 if (!bmap)
12285 return NULL;
12286 bmap->dim = isl_space_curry(bmap->dim);
12287 if (!bmap->dim)
12288 goto error;
12289 bmap = isl_basic_map_mark_final(bmap);
12290 return bmap;
12291 error:
12292 isl_basic_map_free(bmap);
12293 return NULL;
12296 /* Given a map (A -> B) -> C, return the corresponding map
12297 * A -> (B -> C).
12299 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12301 return isl_map_change_space(map, &isl_map_can_curry,
12302 "map cannot be curried", &isl_space_curry);
12305 /* Can isl_map_range_curry be applied to "map"?
12306 * That is, does it have a nested relation in its range,
12307 * the domain of which is itself a nested relation?
12309 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12311 if (!map)
12312 return isl_bool_error;
12314 return isl_space_can_range_curry(map->dim);
12317 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12318 * A -> (B -> (C -> D)).
12320 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12322 return isl_map_change_space(map, &isl_map_can_range_curry,
12323 "map range cannot be curried",
12324 &isl_space_range_curry);
12327 /* Can we apply isl_basic_map_uncurry to "bmap"?
12328 * That is, does it have a nested relation in its domain?
12330 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12332 if (!bmap)
12333 return isl_bool_error;
12335 return isl_space_can_uncurry(bmap->dim);
12338 /* Can we apply isl_map_uncurry to "map"?
12339 * That is, does it have a nested relation in its domain?
12341 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12343 if (!map)
12344 return isl_bool_error;
12346 return isl_space_can_uncurry(map->dim);
12349 /* Given a basic map A -> (B -> C), return the corresponding basic map
12350 * (A -> B) -> C.
12352 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12355 if (!bmap)
12356 return NULL;
12358 if (!isl_basic_map_can_uncurry(bmap))
12359 isl_die(bmap->ctx, isl_error_invalid,
12360 "basic map cannot be uncurried",
12361 return isl_basic_map_free(bmap));
12362 bmap = isl_basic_map_cow(bmap);
12363 if (!bmap)
12364 return NULL;
12365 bmap->dim = isl_space_uncurry(bmap->dim);
12366 if (!bmap->dim)
12367 return isl_basic_map_free(bmap);
12368 bmap = isl_basic_map_mark_final(bmap);
12369 return bmap;
12372 /* Given a map A -> (B -> C), return the corresponding map
12373 * (A -> B) -> C.
12375 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12377 return isl_map_change_space(map, &isl_map_can_uncurry,
12378 "map cannot be uncurried", &isl_space_uncurry);
12381 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12382 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12384 return isl_map_equate(set, type1, pos1, type2, pos2);
12387 /* Construct a basic map where the given dimensions are equal to each other.
12389 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12390 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12392 isl_basic_map *bmap = NULL;
12393 int i;
12394 isl_size total;
12396 total = isl_space_dim(space, isl_dim_all);
12397 if (total < 0 ||
12398 isl_space_check_range(space, type1, pos1, 1) < 0 ||
12399 isl_space_check_range(space, type2, pos2, 1) < 0)
12400 goto error;
12402 if (type1 == type2 && pos1 == pos2)
12403 return isl_basic_map_universe(space);
12405 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12406 i = isl_basic_map_alloc_equality(bmap);
12407 if (i < 0)
12408 goto error;
12409 isl_seq_clr(bmap->eq[i], 1 + total);
12410 pos1 += isl_basic_map_offset(bmap, type1);
12411 pos2 += isl_basic_map_offset(bmap, type2);
12412 isl_int_set_si(bmap->eq[i][pos1], -1);
12413 isl_int_set_si(bmap->eq[i][pos2], 1);
12414 bmap = isl_basic_map_finalize(bmap);
12415 isl_space_free(space);
12416 return bmap;
12417 error:
12418 isl_space_free(space);
12419 isl_basic_map_free(bmap);
12420 return NULL;
12423 /* Add a constraint imposing that the given two dimensions are equal.
12425 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12426 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12428 isl_basic_map *eq;
12430 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12432 bmap = isl_basic_map_intersect(bmap, eq);
12434 return bmap;
12437 /* Add a constraint imposing that the given two dimensions are equal.
12439 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12440 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12442 isl_basic_map *bmap;
12444 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12446 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12448 return map;
12451 /* Add a constraint imposing that the given two dimensions have opposite values.
12453 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12454 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12456 isl_basic_map *bmap = NULL;
12457 int i;
12458 isl_size total;
12460 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12461 return isl_map_free(map);
12462 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12463 return isl_map_free(map);
12465 total = isl_map_dim(map, isl_dim_all);
12466 if (total < 0)
12467 return isl_map_free(map);
12468 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12469 i = isl_basic_map_alloc_equality(bmap);
12470 if (i < 0)
12471 goto error;
12472 isl_seq_clr(bmap->eq[i], 1 + total);
12473 pos1 += isl_basic_map_offset(bmap, type1);
12474 pos2 += isl_basic_map_offset(bmap, type2);
12475 isl_int_set_si(bmap->eq[i][pos1], 1);
12476 isl_int_set_si(bmap->eq[i][pos2], 1);
12477 bmap = isl_basic_map_finalize(bmap);
12479 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12481 return map;
12482 error:
12483 isl_basic_map_free(bmap);
12484 isl_map_free(map);
12485 return NULL;
12488 /* Construct a constraint imposing that the value of the first dimension is
12489 * greater than or equal to that of the second.
12491 static __isl_give isl_constraint *constraint_order_ge(
12492 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12493 enum isl_dim_type type2, int pos2)
12495 isl_constraint *c;
12497 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12498 isl_space_check_range(space, type2, pos2, 1) < 0)
12499 space = isl_space_free(space);
12500 if (!space)
12501 return NULL;
12503 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12505 if (type1 == type2 && pos1 == pos2)
12506 return c;
12508 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12509 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12511 return c;
12514 /* Add a constraint imposing that the value of the first dimension is
12515 * greater than or equal to that of the second.
12517 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12518 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12520 isl_constraint *c;
12521 isl_space *space;
12523 if (type1 == type2 && pos1 == pos2)
12524 return bmap;
12525 space = isl_basic_map_get_space(bmap);
12526 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12527 bmap = isl_basic_map_add_constraint(bmap, c);
12529 return bmap;
12532 /* Add a constraint imposing that the value of the first dimension is
12533 * greater than or equal to that of the second.
12535 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12536 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12538 isl_constraint *c;
12539 isl_space *space;
12541 if (type1 == type2 && pos1 == pos2)
12542 return map;
12543 space = isl_map_get_space(map);
12544 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12545 map = isl_map_add_constraint(map, c);
12547 return map;
12550 /* Add a constraint imposing that the value of the first dimension is
12551 * less than or equal to that of the second.
12553 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12554 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12556 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12559 /* Construct a basic map where the value of the first dimension is
12560 * greater than that of the second.
12562 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12563 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12565 isl_basic_map *bmap = NULL;
12566 int i;
12567 isl_size total;
12569 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12570 isl_space_check_range(space, type2, pos2, 1) < 0)
12571 goto error;
12573 if (type1 == type2 && pos1 == pos2)
12574 return isl_basic_map_empty(space);
12576 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12577 total = isl_basic_map_dim(bmap, isl_dim_all);
12578 i = isl_basic_map_alloc_inequality(bmap);
12579 if (total < 0 || i < 0)
12580 return isl_basic_map_free(bmap);
12581 isl_seq_clr(bmap->ineq[i], 1 + total);
12582 pos1 += isl_basic_map_offset(bmap, type1);
12583 pos2 += isl_basic_map_offset(bmap, type2);
12584 isl_int_set_si(bmap->ineq[i][pos1], 1);
12585 isl_int_set_si(bmap->ineq[i][pos2], -1);
12586 isl_int_set_si(bmap->ineq[i][0], -1);
12587 bmap = isl_basic_map_finalize(bmap);
12589 return bmap;
12590 error:
12591 isl_space_free(space);
12592 isl_basic_map_free(bmap);
12593 return NULL;
12596 /* Add a constraint imposing that the value of the first dimension is
12597 * greater than that of the second.
12599 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12600 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12602 isl_basic_map *gt;
12604 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12606 bmap = isl_basic_map_intersect(bmap, gt);
12608 return bmap;
12611 /* Add a constraint imposing that the value of the first dimension is
12612 * greater than that of the second.
12614 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12615 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12617 isl_basic_map *bmap;
12619 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12621 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12623 return map;
12626 /* Add a constraint imposing that the value of the first dimension is
12627 * smaller than that of the second.
12629 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12630 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12632 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12635 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12636 int pos)
12638 isl_aff *div;
12639 isl_local_space *ls;
12641 if (!bmap)
12642 return NULL;
12644 if (!isl_basic_map_divs_known(bmap))
12645 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12646 "some divs are unknown", return NULL);
12648 ls = isl_basic_map_get_local_space(bmap);
12649 div = isl_local_space_get_div(ls, pos);
12650 isl_local_space_free(ls);
12652 return div;
12655 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12656 int pos)
12658 return isl_basic_map_get_div(bset, pos);
12661 /* Plug in "subs" for dimension "type", "pos" of "bset".
12663 * Let i be the dimension to replace and let "subs" be of the form
12665 * f/d
12667 * Any integer division with a non-zero coefficient for i,
12669 * floor((a i + g)/m)
12671 * is replaced by
12673 * floor((a f + d g)/(m d))
12675 * Constraints of the form
12677 * a i + g
12679 * are replaced by
12681 * a f + d g
12683 * We currently require that "subs" is an integral expression.
12684 * Handling rational expressions may require us to add stride constraints
12685 * as we do in isl_basic_set_preimage_multi_aff.
12687 __isl_give isl_basic_set *isl_basic_set_substitute(
12688 __isl_take isl_basic_set *bset,
12689 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12691 int i;
12692 isl_int v;
12693 isl_ctx *ctx;
12694 isl_size n_div;
12696 if (bset && isl_basic_set_plain_is_empty(bset))
12697 return bset;
12699 bset = isl_basic_set_cow(bset);
12700 if (!bset || !subs)
12701 goto error;
12703 ctx = isl_basic_set_get_ctx(bset);
12704 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12705 isl_die(ctx, isl_error_invalid,
12706 "spaces don't match", goto error);
12707 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
12708 if (n_div < 0)
12709 goto error;
12710 if (n_div != 0)
12711 isl_die(ctx, isl_error_unsupported,
12712 "cannot handle divs yet", goto error);
12713 if (!isl_int_is_one(subs->v->el[0]))
12714 isl_die(ctx, isl_error_invalid,
12715 "can only substitute integer expressions", goto error);
12717 pos += isl_basic_set_offset(bset, type);
12719 isl_int_init(v);
12721 for (i = 0; i < bset->n_eq; ++i) {
12722 if (isl_int_is_zero(bset->eq[i][pos]))
12723 continue;
12724 isl_int_set(v, bset->eq[i][pos]);
12725 isl_int_set_si(bset->eq[i][pos], 0);
12726 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12727 v, subs->v->el + 1, subs->v->size - 1);
12730 for (i = 0; i < bset->n_ineq; ++i) {
12731 if (isl_int_is_zero(bset->ineq[i][pos]))
12732 continue;
12733 isl_int_set(v, bset->ineq[i][pos]);
12734 isl_int_set_si(bset->ineq[i][pos], 0);
12735 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12736 v, subs->v->el + 1, subs->v->size - 1);
12739 for (i = 0; i < bset->n_div; ++i) {
12740 if (isl_int_is_zero(bset->div[i][1 + pos]))
12741 continue;
12742 isl_int_set(v, bset->div[i][1 + pos]);
12743 isl_int_set_si(bset->div[i][1 + pos], 0);
12744 isl_seq_combine(bset->div[i] + 1,
12745 subs->v->el[0], bset->div[i] + 1,
12746 v, subs->v->el + 1, subs->v->size - 1);
12747 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12750 isl_int_clear(v);
12752 bset = isl_basic_set_simplify(bset);
12753 return isl_basic_set_finalize(bset);
12754 error:
12755 isl_basic_set_free(bset);
12756 return NULL;
12759 /* Plug in "subs" for dimension "type", "pos" of "set".
12761 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12762 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12764 int i;
12766 if (set && isl_set_plain_is_empty(set))
12767 return set;
12769 set = isl_set_cow(set);
12770 if (!set || !subs)
12771 goto error;
12773 for (i = set->n - 1; i >= 0; --i) {
12774 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12775 set = set_from_map(remove_if_empty(set_to_map(set), i));
12776 if (!set)
12777 return NULL;
12780 return set;
12781 error:
12782 isl_set_free(set);
12783 return NULL;
12786 /* Check if the range of "ma" is compatible with the domain or range
12787 * (depending on "type") of "bmap".
12789 static isl_stat check_basic_map_compatible_range_multi_aff(
12790 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12791 __isl_keep isl_multi_aff *ma)
12793 isl_bool m;
12794 isl_space *ma_space;
12796 ma_space = isl_multi_aff_get_space(ma);
12798 m = isl_space_has_equal_params(bmap->dim, ma_space);
12799 if (m < 0)
12800 goto error;
12801 if (!m)
12802 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12803 "parameters don't match", goto error);
12804 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12805 if (m < 0)
12806 goto error;
12807 if (!m)
12808 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12809 "spaces don't match", goto error);
12811 isl_space_free(ma_space);
12812 return isl_stat_ok;
12813 error:
12814 isl_space_free(ma_space);
12815 return isl_stat_error;
12818 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12819 * coefficients before the transformed range of dimensions,
12820 * the "n_after" coefficients after the transformed range of dimensions
12821 * and the coefficients of the other divs in "bmap".
12823 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12824 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12826 int i;
12827 isl_size n_param;
12828 isl_size n_set;
12829 isl_local_space *ls;
12831 if (n_div == 0)
12832 return bmap;
12834 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12835 n_param = isl_local_space_dim(ls, isl_dim_param);
12836 n_set = isl_local_space_dim(ls, isl_dim_set);
12837 if (n_param < 0 || n_set < 0)
12838 return isl_basic_map_free(bmap);
12840 for (i = 0; i < n_div; ++i) {
12841 int o_bmap = 0, o_ls = 0;
12843 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12844 o_bmap += 1 + 1 + n_param;
12845 o_ls += 1 + 1 + n_param;
12846 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12847 o_bmap += n_before;
12848 isl_seq_cpy(bmap->div[i] + o_bmap,
12849 ls->div->row[i] + o_ls, n_set);
12850 o_bmap += n_set;
12851 o_ls += n_set;
12852 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12853 o_bmap += n_after;
12854 isl_seq_cpy(bmap->div[i] + o_bmap,
12855 ls->div->row[i] + o_ls, n_div);
12856 o_bmap += n_div;
12857 o_ls += n_div;
12858 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12859 bmap = isl_basic_map_add_div_constraints(bmap, i);
12860 if (!bmap)
12861 goto error;
12864 isl_local_space_free(ls);
12865 return bmap;
12866 error:
12867 isl_local_space_free(ls);
12868 return isl_basic_map_free(bmap);
12871 /* How many stride constraints does "ma" enforce?
12872 * That is, how many of the affine expressions have a denominator
12873 * different from one?
12875 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12877 int i;
12878 int strides = 0;
12880 for (i = 0; i < ma->n; ++i)
12881 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12882 strides++;
12884 return strides;
12887 /* For each affine expression in ma of the form
12889 * x_i = (f_i y + h_i)/m_i
12891 * with m_i different from one, add a constraint to "bmap"
12892 * of the form
12894 * f_i y + h_i = m_i alpha_i
12896 * with alpha_i an additional existentially quantified variable.
12898 * The input variables of "ma" correspond to a subset of the variables
12899 * of "bmap". There are "n_before" variables in "bmap" before this
12900 * subset and "n_after" variables after this subset.
12901 * The integer divisions of the affine expressions in "ma" are assumed
12902 * to have been aligned. There are "n_div_ma" of them and
12903 * they appear first in "bmap", straight after the "n_after" variables.
12905 static __isl_give isl_basic_map *add_ma_strides(
12906 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12907 int n_before, int n_after, int n_div_ma)
12909 int i, k;
12910 int div;
12911 isl_size total;
12912 isl_size n_param;
12913 isl_size n_in;
12915 total = isl_basic_map_dim(bmap, isl_dim_all);
12916 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12917 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12918 if (total < 0 || n_param < 0 || n_in < 0)
12919 return isl_basic_map_free(bmap);
12920 for (i = 0; i < ma->n; ++i) {
12921 int o_bmap = 0, o_ma = 1;
12923 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12924 continue;
12925 div = isl_basic_map_alloc_div(bmap);
12926 k = isl_basic_map_alloc_equality(bmap);
12927 if (div < 0 || k < 0)
12928 goto error;
12929 isl_int_set_si(bmap->div[div][0], 0);
12930 isl_seq_cpy(bmap->eq[k] + o_bmap,
12931 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12932 o_bmap += 1 + n_param;
12933 o_ma += 1 + n_param;
12934 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12935 o_bmap += n_before;
12936 isl_seq_cpy(bmap->eq[k] + o_bmap,
12937 ma->u.p[i]->v->el + o_ma, n_in);
12938 o_bmap += n_in;
12939 o_ma += n_in;
12940 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12941 o_bmap += n_after;
12942 isl_seq_cpy(bmap->eq[k] + o_bmap,
12943 ma->u.p[i]->v->el + o_ma, n_div_ma);
12944 o_bmap += n_div_ma;
12945 o_ma += n_div_ma;
12946 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12947 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12948 total++;
12951 return bmap;
12952 error:
12953 isl_basic_map_free(bmap);
12954 return NULL;
12957 /* Replace the domain or range space (depending on "type) of "space" by "set".
12959 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12960 enum isl_dim_type type, __isl_take isl_space *set)
12962 if (type == isl_dim_in) {
12963 space = isl_space_range(space);
12964 space = isl_space_map_from_domain_and_range(set, space);
12965 } else {
12966 space = isl_space_domain(space);
12967 space = isl_space_map_from_domain_and_range(space, set);
12970 return space;
12973 /* Compute the preimage of the domain or range (depending on "type")
12974 * of "bmap" under the function represented by "ma".
12975 * In other words, plug in "ma" in the domain or range of "bmap".
12976 * The result is a basic map that lives in the same space as "bmap"
12977 * except that the domain or range has been replaced by
12978 * the domain space of "ma".
12980 * If bmap is represented by
12982 * A(p) + S u + B x + T v + C(divs) >= 0,
12984 * where u and x are input and output dimensions if type == isl_dim_out
12985 * while x and v are input and output dimensions if type == isl_dim_in,
12986 * and ma is represented by
12988 * x = D(p) + F(y) + G(divs')
12990 * then the result is
12992 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12994 * The divs in the input set are similarly adjusted.
12995 * In particular
12997 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12999 * becomes
13001 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13002 * B_i G(divs') + c_i(divs))/n_i)
13004 * If bmap is not a rational map and if F(y) involves any denominators
13006 * x_i = (f_i y + h_i)/m_i
13008 * then additional constraints are added to ensure that we only
13009 * map back integer points. That is we enforce
13011 * f_i y + h_i = m_i alpha_i
13013 * with alpha_i an additional existentially quantified variable.
13015 * We first copy over the divs from "ma".
13016 * Then we add the modified constraints and divs from "bmap".
13017 * Finally, we add the stride constraints, if needed.
13019 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13020 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13021 __isl_take isl_multi_aff *ma)
13023 int i, k;
13024 isl_space *space;
13025 isl_basic_map *res = NULL;
13026 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13027 isl_int f, c1, c2, g;
13028 isl_bool rational;
13029 int strides;
13031 isl_int_init(f);
13032 isl_int_init(c1);
13033 isl_int_init(c2);
13034 isl_int_init(g);
13036 ma = isl_multi_aff_align_divs(ma);
13037 if (!bmap || !ma)
13038 goto error;
13039 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13040 goto error;
13042 if (type == isl_dim_in) {
13043 n_before = 0;
13044 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13045 } else {
13046 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13047 n_after = 0;
13049 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13050 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13051 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13052 goto error;
13054 space = isl_multi_aff_get_domain_space(ma);
13055 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13056 rational = isl_basic_map_is_rational(bmap);
13057 strides = rational ? 0 : multi_aff_strides(ma);
13058 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13059 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13060 if (rational)
13061 res = isl_basic_map_set_rational(res);
13063 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13064 if (isl_basic_map_alloc_div(res) < 0)
13065 goto error;
13067 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13068 if (!res)
13069 goto error;
13071 for (i = 0; i < bmap->n_eq; ++i) {
13072 k = isl_basic_map_alloc_equality(res);
13073 if (k < 0)
13074 goto error;
13075 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13076 n_after, n_div_ma, n_div_bmap,
13077 f, c1, c2, g, 0) < 0)
13078 goto error;
13081 for (i = 0; i < bmap->n_ineq; ++i) {
13082 k = isl_basic_map_alloc_inequality(res);
13083 if (k < 0)
13084 goto error;
13085 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13086 n_after, n_div_ma, n_div_bmap,
13087 f, c1, c2, g, 0) < 0)
13088 goto error;
13091 for (i = 0; i < bmap->n_div; ++i) {
13092 if (isl_int_is_zero(bmap->div[i][0])) {
13093 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13094 continue;
13096 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13097 n_before, n_after, n_div_ma, n_div_bmap,
13098 f, c1, c2, g, 1) < 0)
13099 goto error;
13102 if (strides)
13103 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13105 isl_int_clear(f);
13106 isl_int_clear(c1);
13107 isl_int_clear(c2);
13108 isl_int_clear(g);
13109 isl_basic_map_free(bmap);
13110 isl_multi_aff_free(ma);
13111 res = isl_basic_map_simplify(res);
13112 return isl_basic_map_finalize(res);
13113 error:
13114 isl_int_clear(f);
13115 isl_int_clear(c1);
13116 isl_int_clear(c2);
13117 isl_int_clear(g);
13118 isl_basic_map_free(bmap);
13119 isl_multi_aff_free(ma);
13120 isl_basic_map_free(res);
13121 return NULL;
13124 /* Compute the preimage of "bset" under the function represented by "ma".
13125 * In other words, plug in "ma" in "bset". The result is a basic set
13126 * that lives in the domain space of "ma".
13128 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13129 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13131 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13134 /* Compute the preimage of the domain of "bmap" under the function
13135 * represented by "ma".
13136 * In other words, plug in "ma" in the domain of "bmap".
13137 * The result is a basic map that lives in the same space as "bmap"
13138 * except that the domain has been replaced by the domain space of "ma".
13140 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13141 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13143 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13146 /* Compute the preimage of the range of "bmap" under the function
13147 * represented by "ma".
13148 * In other words, plug in "ma" in the range of "bmap".
13149 * The result is a basic map that lives in the same space as "bmap"
13150 * except that the range has been replaced by the domain space of "ma".
13152 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13153 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13155 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13158 /* Check if the range of "ma" is compatible with the domain or range
13159 * (depending on "type") of "map".
13160 * Return isl_stat_error if anything is wrong.
13162 static isl_stat check_map_compatible_range_multi_aff(
13163 __isl_keep isl_map *map, enum isl_dim_type type,
13164 __isl_keep isl_multi_aff *ma)
13166 isl_bool m;
13167 isl_space *ma_space;
13169 ma_space = isl_multi_aff_get_space(ma);
13170 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13171 isl_space_free(ma_space);
13172 if (m < 0)
13173 return isl_stat_error;
13174 if (!m)
13175 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13176 "spaces don't match", return isl_stat_error);
13177 return isl_stat_ok;
13180 /* Compute the preimage of the domain or range (depending on "type")
13181 * of "map" under the function represented by "ma".
13182 * In other words, plug in "ma" in the domain or range of "map".
13183 * The result is a map that lives in the same space as "map"
13184 * except that the domain or range has been replaced by
13185 * the domain space of "ma".
13187 * The parameters are assumed to have been aligned.
13189 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13190 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13192 int i;
13193 isl_space *space;
13195 map = isl_map_cow(map);
13196 ma = isl_multi_aff_align_divs(ma);
13197 if (!map || !ma)
13198 goto error;
13199 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13200 goto error;
13202 for (i = 0; i < map->n; ++i) {
13203 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13204 isl_multi_aff_copy(ma));
13205 if (!map->p[i])
13206 goto error;
13209 space = isl_multi_aff_get_domain_space(ma);
13210 space = isl_space_set(isl_map_get_space(map), type, space);
13212 isl_space_free(map->dim);
13213 map->dim = space;
13214 if (!map->dim)
13215 goto error;
13217 isl_multi_aff_free(ma);
13218 if (map->n > 1)
13219 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13220 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13221 return map;
13222 error:
13223 isl_multi_aff_free(ma);
13224 isl_map_free(map);
13225 return NULL;
13228 /* Compute the preimage of the domain or range (depending on "type")
13229 * of "map" under the function represented by "ma".
13230 * In other words, plug in "ma" in the domain or range of "map".
13231 * The result is a map that lives in the same space as "map"
13232 * except that the domain or range has been replaced by
13233 * the domain space of "ma".
13235 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13236 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13238 isl_bool aligned;
13240 if (!map || !ma)
13241 goto error;
13243 aligned = isl_map_space_has_equal_params(map, ma->space);
13244 if (aligned < 0)
13245 goto error;
13246 if (aligned)
13247 return map_preimage_multi_aff(map, type, ma);
13249 if (isl_map_check_named_params(map) < 0)
13250 goto error;
13251 if (!isl_space_has_named_params(ma->space))
13252 isl_die(map->ctx, isl_error_invalid,
13253 "unaligned unnamed parameters", goto error);
13254 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13255 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13257 return map_preimage_multi_aff(map, type, ma);
13258 error:
13259 isl_multi_aff_free(ma);
13260 return isl_map_free(map);
13263 /* Compute the preimage of "set" under the function represented by "ma".
13264 * In other words, plug in "ma" in "set". The result is a set
13265 * that lives in the domain space of "ma".
13267 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13268 __isl_take isl_multi_aff *ma)
13270 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13273 /* Compute the preimage of the domain of "map" under the function
13274 * represented by "ma".
13275 * In other words, plug in "ma" in the domain of "map".
13276 * The result is a map that lives in the same space as "map"
13277 * except that the domain has been replaced by the domain space of "ma".
13279 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13280 __isl_take isl_multi_aff *ma)
13282 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13285 /* Compute the preimage of the range of "map" under the function
13286 * represented by "ma".
13287 * In other words, plug in "ma" in the range of "map".
13288 * The result is a map that lives in the same space as "map"
13289 * except that the range has been replaced by the domain space of "ma".
13291 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13292 __isl_take isl_multi_aff *ma)
13294 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13297 /* Compute the preimage of "map" under the function represented by "pma".
13298 * In other words, plug in "pma" in the domain or range of "map".
13299 * The result is a map that lives in the same space as "map",
13300 * except that the space of type "type" has been replaced by
13301 * the domain space of "pma".
13303 * The parameters of "map" and "pma" are assumed to have been aligned.
13305 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13306 __isl_take isl_map *map, enum isl_dim_type type,
13307 __isl_take isl_pw_multi_aff *pma)
13309 int i;
13310 isl_map *res;
13312 if (!pma)
13313 goto error;
13315 if (pma->n == 0) {
13316 isl_pw_multi_aff_free(pma);
13317 res = isl_map_empty(isl_map_get_space(map));
13318 isl_map_free(map);
13319 return res;
13322 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13323 isl_multi_aff_copy(pma->p[0].maff));
13324 if (type == isl_dim_in)
13325 res = isl_map_intersect_domain(res,
13326 isl_map_copy(pma->p[0].set));
13327 else
13328 res = isl_map_intersect_range(res,
13329 isl_map_copy(pma->p[0].set));
13331 for (i = 1; i < pma->n; ++i) {
13332 isl_map *res_i;
13334 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13335 isl_multi_aff_copy(pma->p[i].maff));
13336 if (type == isl_dim_in)
13337 res_i = isl_map_intersect_domain(res_i,
13338 isl_map_copy(pma->p[i].set));
13339 else
13340 res_i = isl_map_intersect_range(res_i,
13341 isl_map_copy(pma->p[i].set));
13342 res = isl_map_union(res, res_i);
13345 isl_pw_multi_aff_free(pma);
13346 isl_map_free(map);
13347 return res;
13348 error:
13349 isl_pw_multi_aff_free(pma);
13350 isl_map_free(map);
13351 return NULL;
13354 /* Compute the preimage of "map" under the function represented by "pma".
13355 * In other words, plug in "pma" in the domain or range of "map".
13356 * The result is a map that lives in the same space as "map",
13357 * except that the space of type "type" has been replaced by
13358 * the domain space of "pma".
13360 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13361 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13363 isl_bool aligned;
13365 if (!map || !pma)
13366 goto error;
13368 aligned = isl_map_space_has_equal_params(map, pma->dim);
13369 if (aligned < 0)
13370 goto error;
13371 if (aligned)
13372 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13374 if (isl_map_check_named_params(map) < 0)
13375 goto error;
13376 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13377 goto error;
13378 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13379 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13381 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13382 error:
13383 isl_pw_multi_aff_free(pma);
13384 return isl_map_free(map);
13387 /* Compute the preimage of "set" under the function represented by "pma".
13388 * In other words, plug in "pma" in "set". The result is a set
13389 * that lives in the domain space of "pma".
13391 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13392 __isl_take isl_pw_multi_aff *pma)
13394 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13397 /* Compute the preimage of the domain of "map" under the function
13398 * represented by "pma".
13399 * In other words, plug in "pma" in the domain of "map".
13400 * The result is a map that lives in the same space as "map",
13401 * except that domain space has been replaced by the domain space of "pma".
13403 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13404 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13406 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13409 /* Compute the preimage of the range of "map" under the function
13410 * represented by "pma".
13411 * In other words, plug in "pma" in the range of "map".
13412 * The result is a map that lives in the same space as "map",
13413 * except that range space has been replaced by the domain space of "pma".
13415 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13416 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13418 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13421 /* Compute the preimage of "map" under the function represented by "mpa".
13422 * In other words, plug in "mpa" in the domain or range of "map".
13423 * The result is a map that lives in the same space as "map",
13424 * except that the space of type "type" has been replaced by
13425 * the domain space of "mpa".
13427 * If the map does not involve any constraints that refer to the
13428 * dimensions of the substituted space, then the only possible
13429 * effect of "mpa" on the map is to map the space to a different space.
13430 * We create a separate isl_multi_aff to effectuate this change
13431 * in order to avoid spurious splitting of the map along the pieces
13432 * of "mpa".
13433 * If "mpa" has a non-trivial explicit domain, however,
13434 * then the full substitution should be performed.
13436 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13437 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13439 isl_size n;
13440 isl_bool full;
13441 isl_pw_multi_aff *pma;
13443 n = isl_map_dim(map, type);
13444 if (n < 0 || !mpa)
13445 goto error;
13447 full = isl_map_involves_dims(map, type, 0, n);
13448 if (full >= 0 && !full)
13449 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13450 if (full < 0)
13451 goto error;
13452 if (!full) {
13453 isl_space *space;
13454 isl_multi_aff *ma;
13456 space = isl_multi_pw_aff_get_space(mpa);
13457 isl_multi_pw_aff_free(mpa);
13458 ma = isl_multi_aff_zero(space);
13459 return isl_map_preimage_multi_aff(map, type, ma);
13462 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13463 return isl_map_preimage_pw_multi_aff(map, type, pma);
13464 error:
13465 isl_map_free(map);
13466 isl_multi_pw_aff_free(mpa);
13467 return NULL;
13470 /* Compute the preimage of "map" under the function represented by "mpa".
13471 * In other words, plug in "mpa" in the domain "map".
13472 * The result is a map that lives in the same space as "map",
13473 * except that domain space has been replaced by the domain space of "mpa".
13475 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13476 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13478 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13481 /* Compute the preimage of "set" by the function represented by "mpa".
13482 * In other words, plug in "mpa" in "set".
13484 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13485 __isl_take isl_multi_pw_aff *mpa)
13487 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13490 /* Return a copy of the equality constraints of "bset" as a matrix.
13492 __isl_give isl_mat *isl_basic_set_extract_equalities(
13493 __isl_keep isl_basic_set *bset)
13495 isl_ctx *ctx;
13496 isl_size total;
13498 total = isl_basic_set_dim(bset, isl_dim_all);
13499 if (total < 0)
13500 return NULL;
13502 ctx = isl_basic_set_get_ctx(bset);
13503 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
13506 /* Are the "n" "coefficients" starting at "first" of the integer division
13507 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13508 * to each other?
13509 * The "coefficient" at position 0 is the denominator.
13510 * The "coefficient" at position 1 is the constant term.
13512 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13513 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13514 unsigned first, unsigned n)
13516 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13517 return isl_bool_error;
13518 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13519 return isl_bool_error;
13520 return isl_seq_eq(bmap1->div[pos1] + first,
13521 bmap2->div[pos2] + first, n);
13524 /* Are the integer division expressions at position "pos1" in "bmap1" and
13525 * "pos2" in "bmap2" equal to each other, except that the constant terms
13526 * are different?
13528 isl_bool isl_basic_map_equal_div_expr_except_constant(
13529 __isl_keep isl_basic_map *bmap1, int pos1,
13530 __isl_keep isl_basic_map *bmap2, int pos2)
13532 isl_bool equal;
13533 isl_size total, total2;
13535 total = isl_basic_map_dim(bmap1, isl_dim_all);
13536 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
13537 if (total < 0 || total2 < 0)
13538 return isl_bool_error;
13539 if (total != total2)
13540 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13541 "incomparable div expressions", return isl_bool_error);
13542 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13543 0, 1);
13544 if (equal < 0 || !equal)
13545 return equal;
13546 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13547 1, 1);
13548 if (equal < 0 || equal)
13549 return isl_bool_not(equal);
13550 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13551 2, total);
13554 /* Replace the numerator of the constant term of the integer division
13555 * expression at position "div" in "bmap" by "value".
13556 * The caller guarantees that this does not change the meaning
13557 * of the input.
13559 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13560 __isl_take isl_basic_map *bmap, int div, int value)
13562 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13563 return isl_basic_map_free(bmap);
13565 isl_int_set_si(bmap->div[div][1], value);
13567 return bmap;
13570 /* Is the point "inner" internal to inequality constraint "ineq"
13571 * of "bset"?
13572 * The point is considered to be internal to the inequality constraint,
13573 * if it strictly lies on the positive side of the inequality constraint,
13574 * or if it lies on the constraint and the constraint is lexico-positive.
13576 static isl_bool is_internal(__isl_keep isl_vec *inner,
13577 __isl_keep isl_basic_set *bset, int ineq)
13579 isl_ctx *ctx;
13580 int pos;
13581 isl_size total;
13583 if (!inner || !bset)
13584 return isl_bool_error;
13586 ctx = isl_basic_set_get_ctx(bset);
13587 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13588 &ctx->normalize_gcd);
13589 if (!isl_int_is_zero(ctx->normalize_gcd))
13590 return isl_int_is_nonneg(ctx->normalize_gcd);
13592 total = isl_basic_set_dim(bset, isl_dim_all);
13593 if (total < 0)
13594 return isl_bool_error;
13595 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13596 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13599 /* Tighten the inequality constraints of "bset" that are outward with respect
13600 * to the point "vec".
13601 * That is, tighten the constraints that are not satisfied by "vec".
13603 * "vec" is a point internal to some superset S of "bset" that is used
13604 * to make the subsets of S disjoint, by tightening one half of the constraints
13605 * that separate two subsets. In particular, the constraints of S
13606 * are all satisfied by "vec" and should not be tightened.
13607 * Of the internal constraints, those that have "vec" on the outside
13608 * are tightened. The shared facet is included in the adjacent subset
13609 * with the opposite constraint.
13610 * For constraints that saturate "vec", this criterion cannot be used
13611 * to determine which of the two sides should be tightened.
13612 * Instead, the sign of the first non-zero coefficient is used
13613 * to make this choice. Note that this second criterion is never used
13614 * on the constraints of S since "vec" is interior to "S".
13616 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13617 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13619 int j;
13621 bset = isl_basic_set_cow(bset);
13622 if (!bset)
13623 return NULL;
13624 for (j = 0; j < bset->n_ineq; ++j) {
13625 isl_bool internal;
13627 internal = is_internal(vec, bset, j);
13628 if (internal < 0)
13629 return isl_basic_set_free(bset);
13630 if (internal)
13631 continue;
13632 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13635 return bset;
13638 /* Replace the variables x of type "type" starting at "first" in "bmap"
13639 * by x' with x = M x' with M the matrix trans.
13640 * That is, replace the corresponding coefficients c by c M.
13642 * The transformation matrix should be a square matrix.
13644 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13645 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13646 __isl_take isl_mat *trans)
13648 unsigned pos;
13650 bmap = isl_basic_map_cow(bmap);
13651 if (!bmap || !trans)
13652 goto error;
13654 if (trans->n_row != trans->n_col)
13655 isl_die(trans->ctx, isl_error_invalid,
13656 "expecting square transformation matrix", goto error);
13657 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13658 goto error;
13660 pos = isl_basic_map_offset(bmap, type) + first;
13662 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13663 isl_mat_copy(trans)) < 0)
13664 goto error;
13665 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13666 isl_mat_copy(trans)) < 0)
13667 goto error;
13668 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13669 isl_mat_copy(trans)) < 0)
13670 goto error;
13672 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13673 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13675 isl_mat_free(trans);
13676 return bmap;
13677 error:
13678 isl_mat_free(trans);
13679 isl_basic_map_free(bmap);
13680 return NULL;
13683 /* Replace the variables x of type "type" starting at "first" in "bset"
13684 * by x' with x = M x' with M the matrix trans.
13685 * That is, replace the corresponding coefficients c by c M.
13687 * The transformation matrix should be a square matrix.
13689 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13690 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13691 __isl_take isl_mat *trans)
13693 return isl_basic_map_transform_dims(bset, type, first, trans);