isl_*_dim: return isl_size
[isl.git] / isl_map.c
blob9a4995a78196fd0117608d2ebe0f8184f948ae79
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 int 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 -1;
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 -1);
138 /* Return the position of the variables of the given type
139 * within the sequence of variables of "bset".
141 int 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 int 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 int 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 int 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 int 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 int 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 int 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, n;
5335 if (!list)
5336 return NULL;
5338 n = isl_basic_map_list_n_basic_map(list);
5339 for (i = 0; i < n; ++i) {
5340 isl_basic_map *bmap;
5341 isl_basic_set *bset;
5343 bmap = isl_basic_map_list_get_basic_map(list, i);
5344 bset = isl_basic_set_underlying_set(bmap);
5345 list = isl_basic_set_list_set_basic_set(list, i, bset);
5348 return list;
5351 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5352 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5354 struct isl_basic_map *bmap;
5355 struct isl_ctx *ctx;
5356 isl_size dim, bmap_total;
5357 unsigned total;
5358 int i;
5360 if (!bset || !like)
5361 goto error;
5362 ctx = bset->ctx;
5363 if (isl_basic_set_check_no_params(bset) < 0 ||
5364 isl_basic_set_check_no_locals(bset) < 0)
5365 goto error;
5366 dim = isl_basic_set_dim(bset, isl_dim_set);
5367 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5368 if (dim < 0 || bmap_total < 0)
5369 goto error;
5370 isl_assert(ctx, dim == bmap_total, goto error);
5371 if (like->n_div == 0) {
5372 isl_space *space = isl_basic_map_get_space(like);
5373 isl_basic_map_free(like);
5374 return isl_basic_map_reset_space(bset, space);
5376 bset = isl_basic_set_cow(bset);
5377 if (!bset)
5378 goto error;
5379 total = dim + bset->extra;
5380 bmap = bset_to_bmap(bset);
5381 isl_space_free(bmap->dim);
5382 bmap->dim = isl_space_copy(like->dim);
5383 if (!bmap->dim)
5384 goto error;
5385 bmap->n_div = like->n_div;
5386 bmap->extra += like->n_div;
5387 if (bmap->extra) {
5388 unsigned ltotal;
5389 isl_int **div;
5390 ltotal = total - bmap->extra + like->extra;
5391 if (ltotal > total)
5392 ltotal = total;
5393 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5394 bmap->extra * (1 + 1 + total));
5395 if (isl_blk_is_error(bmap->block2))
5396 goto error;
5397 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5398 if (!div)
5399 goto error;
5400 bmap->div = div;
5401 for (i = 0; i < bmap->extra; ++i)
5402 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5403 for (i = 0; i < like->n_div; ++i) {
5404 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5405 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5407 bmap = isl_basic_map_add_known_div_constraints(bmap);
5409 isl_basic_map_free(like);
5410 bmap = isl_basic_map_simplify(bmap);
5411 bmap = isl_basic_map_finalize(bmap);
5412 return bmap;
5413 error:
5414 isl_basic_map_free(like);
5415 isl_basic_set_free(bset);
5416 return NULL;
5419 struct isl_basic_set *isl_basic_set_from_underlying_set(
5420 struct isl_basic_set *bset, struct isl_basic_set *like)
5422 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5423 bset_to_bmap(like)));
5426 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5428 int i;
5430 map = isl_map_cow(map);
5431 if (!map)
5432 return NULL;
5433 map->dim = isl_space_cow(map->dim);
5434 if (!map->dim)
5435 goto error;
5437 for (i = 1; i < map->n; ++i)
5438 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5439 goto error);
5440 for (i = 0; i < map->n; ++i) {
5441 map->p[i] = bset_to_bmap(
5442 isl_basic_map_underlying_set(map->p[i]));
5443 if (!map->p[i])
5444 goto error;
5446 if (map->n == 0)
5447 map->dim = isl_space_underlying(map->dim, 0);
5448 else {
5449 isl_space_free(map->dim);
5450 map->dim = isl_space_copy(map->p[0]->dim);
5452 if (!map->dim)
5453 goto error;
5454 return set_from_map(map);
5455 error:
5456 isl_map_free(map);
5457 return NULL;
5460 /* Replace the space of "bmap" by "space".
5462 * If the space of "bmap" is identical to "space" (including the identifiers
5463 * of the input and output dimensions), then simply return the original input.
5465 __isl_give isl_basic_map *isl_basic_map_reset_space(
5466 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5468 isl_bool equal;
5469 isl_space *bmap_space;
5471 bmap_space = isl_basic_map_peek_space(bmap);
5472 equal = isl_space_is_equal(bmap_space, space);
5473 if (equal >= 0 && equal)
5474 equal = isl_space_has_equal_ids(bmap_space, space);
5475 if (equal < 0)
5476 goto error;
5477 if (equal) {
5478 isl_space_free(space);
5479 return bmap;
5481 bmap = isl_basic_map_cow(bmap);
5482 if (!bmap || !space)
5483 goto error;
5485 isl_space_free(bmap->dim);
5486 bmap->dim = space;
5488 bmap = isl_basic_map_finalize(bmap);
5490 return bmap;
5491 error:
5492 isl_basic_map_free(bmap);
5493 isl_space_free(space);
5494 return NULL;
5497 __isl_give isl_basic_set *isl_basic_set_reset_space(
5498 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5500 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5501 dim));
5504 /* Check that the total dimensions of "map" and "space" are the same.
5506 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5507 __isl_keep isl_space *space)
5509 isl_size dim1, dim2;
5511 dim1 = isl_map_dim(map, isl_dim_all);
5512 dim2 = isl_space_dim(space, isl_dim_all);
5513 if (dim1 < 0 || dim2 < 0)
5514 return isl_stat_error;
5515 if (dim1 == dim2)
5516 return isl_stat_ok;
5517 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5518 "total dimensions do not match", return isl_stat_error);
5521 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5522 __isl_take isl_space *space)
5524 int i;
5526 map = isl_map_cow(map);
5527 if (!map || !space)
5528 goto error;
5530 for (i = 0; i < map->n; ++i) {
5531 map->p[i] = isl_basic_map_reset_space(map->p[i],
5532 isl_space_copy(space));
5533 if (!map->p[i])
5534 goto error;
5536 isl_space_free(map->dim);
5537 map->dim = space;
5539 return map;
5540 error:
5541 isl_map_free(map);
5542 isl_space_free(space);
5543 return NULL;
5546 /* Replace the space of "map" by "space", without modifying
5547 * the dimension of "map".
5549 * If the space of "map" is identical to "space" (including the identifiers
5550 * of the input and output dimensions), then simply return the original input.
5552 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5553 __isl_take isl_space *space)
5555 isl_bool equal;
5556 isl_space *map_space;
5558 map_space = isl_map_peek_space(map);
5559 equal = isl_space_is_equal(map_space, space);
5560 if (equal >= 0 && equal)
5561 equal = isl_space_has_equal_ids(map_space, space);
5562 if (equal < 0)
5563 goto error;
5564 if (equal) {
5565 isl_space_free(space);
5566 return map;
5568 if (check_map_space_equal_total_dim(map, space) < 0)
5569 goto error;
5570 return isl_map_reset_space(map, space);
5571 error:
5572 isl_map_free(map);
5573 isl_space_free(space);
5574 return NULL;
5577 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5578 __isl_take isl_space *dim)
5580 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5583 /* Compute the parameter domain of the given basic set.
5585 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5587 isl_bool is_params;
5588 isl_space *space;
5589 isl_size n;
5591 is_params = isl_basic_set_is_params(bset);
5592 if (is_params < 0)
5593 return isl_basic_set_free(bset);
5594 if (is_params)
5595 return bset;
5597 n = isl_basic_set_dim(bset, isl_dim_set);
5598 if (n < 0)
5599 return isl_basic_set_free(bset);
5600 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5601 space = isl_basic_set_get_space(bset);
5602 space = isl_space_params(space);
5603 bset = isl_basic_set_reset_space(bset, space);
5604 return bset;
5607 /* Construct a zero-dimensional basic set with the given parameter domain.
5609 __isl_give isl_basic_set *isl_basic_set_from_params(
5610 __isl_take isl_basic_set *bset)
5612 isl_space *space;
5613 space = isl_basic_set_get_space(bset);
5614 space = isl_space_set_from_params(space);
5615 bset = isl_basic_set_reset_space(bset, space);
5616 return bset;
5619 /* Compute the parameter domain of the given set.
5621 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5623 return isl_map_params(set_to_map(set));
5626 /* Construct a zero-dimensional set with the given parameter domain.
5628 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5630 isl_space *space;
5631 space = isl_set_get_space(set);
5632 space = isl_space_set_from_params(space);
5633 set = isl_set_reset_space(set, space);
5634 return set;
5637 /* Compute the parameter domain of the given map.
5639 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5641 isl_space *space;
5642 isl_size n_in, n_out;
5644 n_in = isl_map_dim(map, isl_dim_in);
5645 n_out = isl_map_dim(map, isl_dim_out);
5646 if (n_in < 0 || n_out < 0)
5647 return isl_map_free(map);
5648 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5649 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5650 space = isl_map_get_space(map);
5651 space = isl_space_params(space);
5652 map = isl_map_reset_space(map, space);
5653 return map;
5656 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5658 isl_space *space;
5659 isl_size n_out;
5661 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5662 if (n_out < 0)
5663 return isl_basic_map_free(bmap);
5664 space = isl_space_domain(isl_basic_map_get_space(bmap));
5666 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5668 return isl_basic_map_reset_space(bmap, space);
5671 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5673 if (!bmap)
5674 return isl_bool_error;
5675 return isl_space_may_be_set(bmap->dim);
5678 /* Is this basic map actually a set?
5679 * Users should never call this function. Outside of isl,
5680 * the type should indicate whether something is a set or a map.
5682 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5684 if (!bmap)
5685 return isl_bool_error;
5686 return isl_space_is_set(bmap->dim);
5689 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5691 isl_bool is_set;
5693 is_set = isl_basic_map_is_set(bmap);
5694 if (is_set < 0)
5695 goto error;
5696 if (is_set)
5697 return bmap;
5698 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5699 error:
5700 isl_basic_map_free(bmap);
5701 return NULL;
5704 __isl_give isl_basic_map *isl_basic_map_domain_map(
5705 __isl_take isl_basic_map *bmap)
5707 int i;
5708 isl_space *space;
5709 isl_basic_map *domain;
5710 isl_size nparam, n_in, n_out;
5712 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5713 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5714 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5715 if (nparam < 0 || n_in < 0 || n_out < 0)
5716 return isl_basic_map_free(bmap);
5718 space = isl_basic_map_get_space(bmap);
5719 space = isl_space_from_range(isl_space_domain(space));
5720 domain = isl_basic_map_universe(space);
5722 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5723 bmap = isl_basic_map_apply_range(bmap, domain);
5724 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5726 for (i = 0; i < n_in; ++i)
5727 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5728 isl_dim_out, i);
5730 bmap = isl_basic_map_gauss(bmap, NULL);
5731 return isl_basic_map_finalize(bmap);
5734 __isl_give isl_basic_map *isl_basic_map_range_map(
5735 __isl_take isl_basic_map *bmap)
5737 int i;
5738 isl_space *space;
5739 isl_basic_map *range;
5740 isl_size nparam, n_in, n_out;
5742 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5743 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5744 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5745 if (nparam < 0 || n_in < 0 || n_out < 0)
5746 return isl_basic_map_free(bmap);
5748 space = isl_basic_map_get_space(bmap);
5749 space = isl_space_from_range(isl_space_range(space));
5750 range = isl_basic_map_universe(space);
5752 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5753 bmap = isl_basic_map_apply_range(bmap, range);
5754 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5756 for (i = 0; i < n_out; ++i)
5757 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5758 isl_dim_out, i);
5760 bmap = isl_basic_map_gauss(bmap, NULL);
5761 return isl_basic_map_finalize(bmap);
5764 int isl_map_may_be_set(__isl_keep isl_map *map)
5766 if (!map)
5767 return -1;
5768 return isl_space_may_be_set(map->dim);
5771 /* Is this map actually a set?
5772 * Users should never call this function. Outside of isl,
5773 * the type should indicate whether something is a set or a map.
5775 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5777 if (!map)
5778 return isl_bool_error;
5779 return isl_space_is_set(map->dim);
5782 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5784 int i;
5785 isl_bool is_set;
5786 struct isl_set *set;
5788 is_set = isl_map_is_set(map);
5789 if (is_set < 0)
5790 goto error;
5791 if (is_set)
5792 return set_from_map(map);
5794 map = isl_map_cow(map);
5795 if (!map)
5796 goto error;
5798 set = set_from_map(map);
5799 set->dim = isl_space_range(set->dim);
5800 if (!set->dim)
5801 goto error;
5802 for (i = 0; i < map->n; ++i) {
5803 set->p[i] = isl_basic_map_range(map->p[i]);
5804 if (!set->p[i])
5805 goto error;
5807 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5808 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5809 return set;
5810 error:
5811 isl_map_free(map);
5812 return NULL;
5815 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5817 int i;
5819 map = isl_map_cow(map);
5820 if (!map)
5821 return NULL;
5823 map->dim = isl_space_domain_map(map->dim);
5824 if (!map->dim)
5825 goto error;
5826 for (i = 0; i < map->n; ++i) {
5827 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5828 if (!map->p[i])
5829 goto error;
5831 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5832 map = isl_map_unmark_normalized(map);
5833 return map;
5834 error:
5835 isl_map_free(map);
5836 return NULL;
5839 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5841 int i;
5842 isl_space *range_dim;
5844 map = isl_map_cow(map);
5845 if (!map)
5846 return NULL;
5848 range_dim = isl_space_range(isl_map_get_space(map));
5849 range_dim = isl_space_from_range(range_dim);
5850 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5851 map->dim = isl_space_join(map->dim, range_dim);
5852 if (!map->dim)
5853 goto error;
5854 for (i = 0; i < map->n; ++i) {
5855 map->p[i] = isl_basic_map_range_map(map->p[i]);
5856 if (!map->p[i])
5857 goto error;
5859 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5860 map = isl_map_unmark_normalized(map);
5861 return map;
5862 error:
5863 isl_map_free(map);
5864 return NULL;
5867 /* Given a wrapped map of the form A[B -> C],
5868 * return the map A[B -> C] -> B.
5870 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5872 isl_id *id;
5873 isl_map *map;
5875 if (!set)
5876 return NULL;
5877 if (!isl_set_has_tuple_id(set))
5878 return isl_map_domain_map(isl_set_unwrap(set));
5880 id = isl_set_get_tuple_id(set);
5881 map = isl_map_domain_map(isl_set_unwrap(set));
5882 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5884 return map;
5887 __isl_give isl_basic_map *isl_basic_map_from_domain(
5888 __isl_take isl_basic_set *bset)
5890 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5893 __isl_give isl_basic_map *isl_basic_map_from_range(
5894 __isl_take isl_basic_set *bset)
5896 isl_space *space;
5897 space = isl_basic_set_get_space(bset);
5898 space = isl_space_from_range(space);
5899 bset = isl_basic_set_reset_space(bset, space);
5900 return bset_to_bmap(bset);
5903 /* Create a relation with the given set as range.
5904 * The domain of the created relation is a zero-dimensional
5905 * flat anonymous space.
5907 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5909 isl_space *space;
5910 space = isl_set_get_space(set);
5911 space = isl_space_from_range(space);
5912 set = isl_set_reset_space(set, space);
5913 return set_to_map(set);
5916 /* Create a relation with the given set as domain.
5917 * The range of the created relation is a zero-dimensional
5918 * flat anonymous space.
5920 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5922 return isl_map_reverse(isl_map_from_range(set));
5925 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5926 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5928 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5931 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5932 __isl_take isl_set *range)
5934 return isl_map_apply_range(isl_map_reverse(domain), range);
5937 /* Return a newly allocated isl_map with given space and flags and
5938 * room for "n" basic maps.
5939 * Make sure that all cached information is cleared.
5941 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5942 unsigned flags)
5944 struct isl_map *map;
5946 if (!space)
5947 return NULL;
5948 if (n < 0)
5949 isl_die(space->ctx, isl_error_internal,
5950 "negative number of basic maps", goto error);
5951 map = isl_calloc(space->ctx, struct isl_map,
5952 sizeof(struct isl_map) +
5953 (n - 1) * sizeof(struct isl_basic_map *));
5954 if (!map)
5955 goto error;
5957 map->ctx = space->ctx;
5958 isl_ctx_ref(map->ctx);
5959 map->ref = 1;
5960 map->size = n;
5961 map->n = 0;
5962 map->dim = space;
5963 map->flags = flags;
5964 return map;
5965 error:
5966 isl_space_free(space);
5967 return NULL;
5970 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5972 struct isl_basic_map *bmap;
5973 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5974 bmap = isl_basic_map_set_to_empty(bmap);
5975 return bmap;
5978 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5980 struct isl_basic_set *bset;
5981 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5982 bset = isl_basic_set_set_to_empty(bset);
5983 return bset;
5986 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5988 struct isl_basic_map *bmap;
5989 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5990 bmap = isl_basic_map_finalize(bmap);
5991 return bmap;
5994 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5996 struct isl_basic_set *bset;
5997 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5998 bset = isl_basic_set_finalize(bset);
5999 return bset;
6002 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6003 __isl_take isl_space *space)
6005 int i;
6006 isl_size total = isl_space_dim(space, isl_dim_all);
6007 isl_basic_map *bmap;
6009 if (total < 0)
6010 space = isl_space_free(space);
6011 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6012 for (i = 0; i < total; ++i) {
6013 int k = isl_basic_map_alloc_inequality(bmap);
6014 if (k < 0)
6015 goto error;
6016 isl_seq_clr(bmap->ineq[k], 1 + total);
6017 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6019 return bmap;
6020 error:
6021 isl_basic_map_free(bmap);
6022 return NULL;
6025 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6026 __isl_take isl_space *space)
6028 return isl_basic_map_nat_universe(space);
6031 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6033 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6036 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6038 return isl_map_nat_universe(dim);
6041 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6043 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6046 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6048 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6051 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6053 struct isl_map *map;
6054 if (!space)
6055 return NULL;
6056 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6057 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6058 return map;
6061 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6063 struct isl_set *set;
6064 if (!space)
6065 return NULL;
6066 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6067 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6068 return set;
6071 struct isl_map *isl_map_dup(struct isl_map *map)
6073 int i;
6074 struct isl_map *dup;
6076 if (!map)
6077 return NULL;
6078 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6079 for (i = 0; i < map->n; ++i)
6080 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6081 return dup;
6084 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6085 __isl_take isl_basic_map *bmap)
6087 if (!bmap || !map)
6088 goto error;
6089 if (isl_basic_map_plain_is_empty(bmap)) {
6090 isl_basic_map_free(bmap);
6091 return map;
6093 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6094 isl_assert(map->ctx, map->n < map->size, goto error);
6095 map->p[map->n] = bmap;
6096 map->n++;
6097 map = isl_map_unmark_normalized(map);
6098 return map;
6099 error:
6100 if (map)
6101 isl_map_free(map);
6102 if (bmap)
6103 isl_basic_map_free(bmap);
6104 return NULL;
6107 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6109 int i;
6111 if (!map)
6112 return NULL;
6114 if (--map->ref > 0)
6115 return NULL;
6117 clear_caches(map);
6118 isl_ctx_deref(map->ctx);
6119 for (i = 0; i < map->n; ++i)
6120 isl_basic_map_free(map->p[i]);
6121 isl_space_free(map->dim);
6122 free(map);
6124 return NULL;
6127 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6128 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6130 int j;
6131 isl_size total;
6133 total = isl_basic_map_dim(bmap, isl_dim_all);
6134 if (total < 0)
6135 return isl_basic_map_free(bmap);
6137 bmap = isl_basic_map_cow(bmap);
6138 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6139 j = isl_basic_map_alloc_equality(bmap);
6140 if (j < 0)
6141 goto error;
6142 isl_seq_clr(bmap->eq[j] + 1, total);
6143 isl_int_set_si(bmap->eq[j][pos], -1);
6144 isl_int_set_si(bmap->eq[j][0], value);
6145 bmap = isl_basic_map_simplify(bmap);
6146 return isl_basic_map_finalize(bmap);
6147 error:
6148 isl_basic_map_free(bmap);
6149 return NULL;
6152 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6153 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6155 int j;
6156 isl_size total;
6158 total = isl_basic_map_dim(bmap, isl_dim_all);
6159 if (total < 0)
6160 return isl_basic_map_free(bmap);
6162 bmap = isl_basic_map_cow(bmap);
6163 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6164 j = isl_basic_map_alloc_equality(bmap);
6165 if (j < 0)
6166 goto error;
6167 isl_seq_clr(bmap->eq[j] + 1, total);
6168 isl_int_set_si(bmap->eq[j][pos], -1);
6169 isl_int_set(bmap->eq[j][0], value);
6170 bmap = isl_basic_map_simplify(bmap);
6171 return isl_basic_map_finalize(bmap);
6172 error:
6173 isl_basic_map_free(bmap);
6174 return NULL;
6177 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6178 enum isl_dim_type type, unsigned pos, int value)
6180 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6181 return isl_basic_map_free(bmap);
6182 return isl_basic_map_fix_pos_si(bmap,
6183 isl_basic_map_offset(bmap, type) + pos, value);
6186 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6187 enum isl_dim_type type, unsigned pos, isl_int value)
6189 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6190 return isl_basic_map_free(bmap);
6191 return isl_basic_map_fix_pos(bmap,
6192 isl_basic_map_offset(bmap, type) + pos, value);
6195 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6196 * to be equal to "v".
6198 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6199 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6201 if (!bmap || !v)
6202 goto error;
6203 if (!isl_val_is_int(v))
6204 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6205 "expecting integer value", goto error);
6206 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6207 goto error;
6208 pos += isl_basic_map_offset(bmap, type);
6209 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6210 isl_val_free(v);
6211 return bmap;
6212 error:
6213 isl_basic_map_free(bmap);
6214 isl_val_free(v);
6215 return NULL;
6218 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6219 * to be equal to "v".
6221 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6222 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6224 return isl_basic_map_fix_val(bset, type, pos, v);
6227 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6228 enum isl_dim_type type, unsigned pos, int value)
6230 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6231 type, pos, value));
6234 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6235 enum isl_dim_type type, unsigned pos, isl_int value)
6237 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6238 type, pos, value));
6241 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6242 unsigned input, int value)
6244 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6247 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6248 unsigned dim, int value)
6250 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6251 isl_dim_set, dim, value));
6254 /* Remove the basic map at position "i" from "map" if this basic map
6255 * is (obviously) empty.
6257 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6259 isl_bool empty;
6261 if (!map)
6262 return NULL;
6264 empty = isl_basic_map_plain_is_empty(map->p[i]);
6265 if (empty < 0)
6266 return isl_map_free(map);
6267 if (!empty)
6268 return map;
6270 isl_basic_map_free(map->p[i]);
6271 map->n--;
6272 if (i != map->n) {
6273 map->p[i] = map->p[map->n];
6274 map = isl_map_unmark_normalized(map);
6278 return map;
6281 /* Perform "fn" on each basic map of "map", where we may not be holding
6282 * the only reference to "map".
6283 * In particular, "fn" should be a semantics preserving operation
6284 * that we want to apply to all copies of "map". We therefore need
6285 * to be careful not to modify "map" in a way that breaks "map"
6286 * in case anything goes wrong.
6288 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6289 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6291 struct isl_basic_map *bmap;
6292 int i;
6294 if (!map)
6295 return NULL;
6297 for (i = map->n - 1; i >= 0; --i) {
6298 bmap = isl_basic_map_copy(map->p[i]);
6299 bmap = fn(bmap);
6300 if (!bmap)
6301 goto error;
6302 isl_basic_map_free(map->p[i]);
6303 map->p[i] = bmap;
6304 map = remove_if_empty(map, i);
6305 if (!map)
6306 return NULL;
6309 return map;
6310 error:
6311 isl_map_free(map);
6312 return NULL;
6315 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6316 enum isl_dim_type type, unsigned pos, int value)
6318 int i;
6320 map = isl_map_cow(map);
6321 if (isl_map_check_range(map, type, pos, 1) < 0)
6322 return isl_map_free(map);
6323 for (i = map->n - 1; i >= 0; --i) {
6324 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6325 map = remove_if_empty(map, i);
6326 if (!map)
6327 return NULL;
6329 map = isl_map_unmark_normalized(map);
6330 return map;
6333 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6334 enum isl_dim_type type, unsigned pos, int value)
6336 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6339 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6340 enum isl_dim_type type, unsigned pos, isl_int value)
6342 int i;
6344 map = isl_map_cow(map);
6345 if (isl_map_check_range(map, type, pos, 1) < 0)
6346 return isl_map_free(map);
6347 for (i = 0; i < map->n; ++i) {
6348 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6349 if (!map->p[i])
6350 goto error;
6352 map = isl_map_unmark_normalized(map);
6353 return map;
6354 error:
6355 isl_map_free(map);
6356 return NULL;
6359 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6360 enum isl_dim_type type, unsigned pos, isl_int value)
6362 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6365 /* Fix the value of the variable at position "pos" of type "type" of "map"
6366 * to be equal to "v".
6368 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6369 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6371 int i;
6373 map = isl_map_cow(map);
6374 if (!map || !v)
6375 goto error;
6377 if (!isl_val_is_int(v))
6378 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6379 "expecting integer value", goto error);
6380 if (isl_map_check_range(map, type, pos, 1) < 0)
6381 goto error;
6382 for (i = map->n - 1; i >= 0; --i) {
6383 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6384 isl_val_copy(v));
6385 map = remove_if_empty(map, i);
6386 if (!map)
6387 goto error;
6389 map = isl_map_unmark_normalized(map);
6390 isl_val_free(v);
6391 return map;
6392 error:
6393 isl_map_free(map);
6394 isl_val_free(v);
6395 return NULL;
6398 /* Fix the value of the variable at position "pos" of type "type" of "set"
6399 * to be equal to "v".
6401 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6402 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6404 return isl_map_fix_val(set, type, pos, v);
6407 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6408 unsigned input, int value)
6410 return isl_map_fix_si(map, isl_dim_in, input, value);
6413 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6415 return set_from_map(isl_map_fix_si(set_to_map(set),
6416 isl_dim_set, dim, value));
6419 static __isl_give isl_basic_map *basic_map_bound_si(
6420 __isl_take isl_basic_map *bmap,
6421 enum isl_dim_type type, unsigned pos, int value, int upper)
6423 int j;
6424 isl_size total;
6426 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6427 return isl_basic_map_free(bmap);
6428 total = isl_basic_map_dim(bmap, isl_dim_all);
6429 if (total < 0)
6430 return isl_basic_map_free(bmap);
6431 pos += isl_basic_map_offset(bmap, type);
6432 bmap = isl_basic_map_cow(bmap);
6433 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6434 j = isl_basic_map_alloc_inequality(bmap);
6435 if (j < 0)
6436 goto error;
6437 isl_seq_clr(bmap->ineq[j], 1 + total);
6438 if (upper) {
6439 isl_int_set_si(bmap->ineq[j][pos], -1);
6440 isl_int_set_si(bmap->ineq[j][0], value);
6441 } else {
6442 isl_int_set_si(bmap->ineq[j][pos], 1);
6443 isl_int_set_si(bmap->ineq[j][0], -value);
6445 bmap = isl_basic_map_simplify(bmap);
6446 return isl_basic_map_finalize(bmap);
6447 error:
6448 isl_basic_map_free(bmap);
6449 return NULL;
6452 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6453 __isl_take isl_basic_map *bmap,
6454 enum isl_dim_type type, unsigned pos, int value)
6456 return basic_map_bound_si(bmap, type, pos, value, 0);
6459 /* Constrain the values of the given dimension to be no greater than "value".
6461 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6462 __isl_take isl_basic_map *bmap,
6463 enum isl_dim_type type, unsigned pos, int value)
6465 return basic_map_bound_si(bmap, type, pos, value, 1);
6468 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6469 enum isl_dim_type type, unsigned pos, int value, int upper)
6471 int i;
6473 map = isl_map_cow(map);
6474 if (isl_map_check_range(map, type, pos, 1) < 0)
6475 return isl_map_free(map);
6476 for (i = 0; i < map->n; ++i) {
6477 map->p[i] = basic_map_bound_si(map->p[i],
6478 type, pos, value, upper);
6479 if (!map->p[i])
6480 goto error;
6482 map = isl_map_unmark_normalized(map);
6483 return map;
6484 error:
6485 isl_map_free(map);
6486 return NULL;
6489 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6490 enum isl_dim_type type, unsigned pos, int value)
6492 return map_bound_si(map, type, pos, value, 0);
6495 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6496 enum isl_dim_type type, unsigned pos, int value)
6498 return map_bound_si(map, type, pos, value, 1);
6501 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6502 enum isl_dim_type type, unsigned pos, int value)
6504 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6505 type, pos, value));
6508 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6509 enum isl_dim_type type, unsigned pos, int value)
6511 return isl_map_upper_bound_si(set, type, pos, value);
6514 /* Bound the given variable of "bmap" from below (or above is "upper"
6515 * is set) to "value".
6517 static __isl_give isl_basic_map *basic_map_bound(
6518 __isl_take isl_basic_map *bmap,
6519 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6521 int j;
6522 isl_size total;
6524 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6525 return isl_basic_map_free(bmap);
6526 total = isl_basic_map_dim(bmap, isl_dim_all);
6527 if (total < 0)
6528 return isl_basic_map_free(bmap);
6529 pos += isl_basic_map_offset(bmap, type);
6530 bmap = isl_basic_map_cow(bmap);
6531 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6532 j = isl_basic_map_alloc_inequality(bmap);
6533 if (j < 0)
6534 goto error;
6535 isl_seq_clr(bmap->ineq[j], 1 + total);
6536 if (upper) {
6537 isl_int_set_si(bmap->ineq[j][pos], -1);
6538 isl_int_set(bmap->ineq[j][0], value);
6539 } else {
6540 isl_int_set_si(bmap->ineq[j][pos], 1);
6541 isl_int_neg(bmap->ineq[j][0], value);
6543 bmap = isl_basic_map_simplify(bmap);
6544 return isl_basic_map_finalize(bmap);
6545 error:
6546 isl_basic_map_free(bmap);
6547 return NULL;
6550 /* Bound the given variable of "map" from below (or above is "upper"
6551 * is set) to "value".
6553 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6554 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6556 int i;
6558 map = isl_map_cow(map);
6559 if (isl_map_check_range(map, type, pos, 1) < 0)
6560 return isl_map_free(map);
6561 for (i = map->n - 1; i >= 0; --i) {
6562 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6563 map = remove_if_empty(map, i);
6564 if (!map)
6565 return NULL;
6567 map = isl_map_unmark_normalized(map);
6568 return map;
6571 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6572 enum isl_dim_type type, unsigned pos, isl_int value)
6574 return map_bound(map, type, pos, value, 0);
6577 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6578 enum isl_dim_type type, unsigned pos, isl_int value)
6580 return map_bound(map, type, pos, value, 1);
6583 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6584 enum isl_dim_type type, unsigned pos, isl_int value)
6586 return isl_map_lower_bound(set, type, pos, value);
6589 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6590 enum isl_dim_type type, unsigned pos, isl_int value)
6592 return isl_map_upper_bound(set, type, pos, value);
6595 /* Force the values of the variable at position "pos" of type "type" of "set"
6596 * to be no smaller than "value".
6598 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6599 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6601 if (!value)
6602 goto error;
6603 if (!isl_val_is_int(value))
6604 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6605 "expecting integer value", goto error);
6606 set = isl_set_lower_bound(set, type, pos, value->n);
6607 isl_val_free(value);
6608 return set;
6609 error:
6610 isl_val_free(value);
6611 isl_set_free(set);
6612 return NULL;
6615 /* Force the values of the variable at position "pos" of type "type" of "set"
6616 * to be no greater than "value".
6618 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6619 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6621 if (!value)
6622 goto error;
6623 if (!isl_val_is_int(value))
6624 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6625 "expecting integer value", goto error);
6626 set = isl_set_upper_bound(set, type, pos, value->n);
6627 isl_val_free(value);
6628 return set;
6629 error:
6630 isl_val_free(value);
6631 isl_set_free(set);
6632 return NULL;
6635 /* Bound the given variable of "bset" from below (or above is "upper"
6636 * is set) to "value".
6638 static __isl_give isl_basic_set *isl_basic_set_bound(
6639 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6640 isl_int value, int upper)
6642 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6643 type, pos, value, upper));
6646 /* Bound the given variable of "bset" from below (or above is "upper"
6647 * is set) to "value".
6649 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6650 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6651 __isl_take isl_val *value, int upper)
6653 if (!value)
6654 goto error;
6655 if (!isl_val_is_int(value))
6656 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6657 "expecting integer value", goto error);
6658 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6659 isl_val_free(value);
6660 return bset;
6661 error:
6662 isl_val_free(value);
6663 isl_basic_set_free(bset);
6664 return NULL;
6667 /* Bound the given variable of "bset" from below to "value".
6669 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6670 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6671 __isl_take isl_val *value)
6673 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6676 /* Bound the given variable of "bset" from above to "value".
6678 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6679 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6680 __isl_take isl_val *value)
6682 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6685 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6687 int i;
6689 map = isl_map_cow(map);
6690 if (!map)
6691 return NULL;
6693 map->dim = isl_space_reverse(map->dim);
6694 if (!map->dim)
6695 goto error;
6696 for (i = 0; i < map->n; ++i) {
6697 map->p[i] = isl_basic_map_reverse(map->p[i]);
6698 if (!map->p[i])
6699 goto error;
6701 map = isl_map_unmark_normalized(map);
6702 return map;
6703 error:
6704 isl_map_free(map);
6705 return NULL;
6708 #undef TYPE
6709 #define TYPE isl_pw_multi_aff
6710 #undef SUFFIX
6711 #define SUFFIX _pw_multi_aff
6712 #undef EMPTY
6713 #define EMPTY isl_pw_multi_aff_empty
6714 #undef ADD
6715 #define ADD isl_pw_multi_aff_union_add
6716 #include "isl_map_lexopt_templ.c"
6718 /* Given a map "map", compute the lexicographically minimal
6719 * (or maximal) image element for each domain element in dom,
6720 * in the form of an isl_pw_multi_aff.
6721 * If "empty" is not NULL, then set *empty to those elements in dom that
6722 * do not have an image element.
6723 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6724 * should be computed over the domain of "map". "empty" is also NULL
6725 * in this case.
6727 * We first compute the lexicographically minimal or maximal element
6728 * in the first basic map. This results in a partial solution "res"
6729 * and a subset "todo" of dom that still need to be handled.
6730 * We then consider each of the remaining maps in "map" and successively
6731 * update both "res" and "todo".
6732 * If "empty" is NULL, then the todo sets are not needed and therefore
6733 * also not computed.
6735 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6736 __isl_take isl_map *map, __isl_take isl_set *dom,
6737 __isl_give isl_set **empty, unsigned flags)
6739 int i;
6740 int full;
6741 isl_pw_multi_aff *res;
6742 isl_set *todo;
6744 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6745 if (!map || (!full && !dom))
6746 goto error;
6748 if (isl_map_plain_is_empty(map)) {
6749 if (empty)
6750 *empty = dom;
6751 else
6752 isl_set_free(dom);
6753 return isl_pw_multi_aff_from_map(map);
6756 res = basic_map_partial_lexopt_pw_multi_aff(
6757 isl_basic_map_copy(map->p[0]),
6758 isl_set_copy(dom), empty, flags);
6760 if (empty)
6761 todo = *empty;
6762 for (i = 1; i < map->n; ++i) {
6763 isl_pw_multi_aff *res_i;
6765 res_i = basic_map_partial_lexopt_pw_multi_aff(
6766 isl_basic_map_copy(map->p[i]),
6767 isl_set_copy(dom), empty, flags);
6769 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6770 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6771 else
6772 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6774 if (empty)
6775 todo = isl_set_intersect(todo, *empty);
6778 isl_set_free(dom);
6779 isl_map_free(map);
6781 if (empty)
6782 *empty = todo;
6784 return res;
6785 error:
6786 if (empty)
6787 *empty = NULL;
6788 isl_set_free(dom);
6789 isl_map_free(map);
6790 return NULL;
6793 #undef TYPE
6794 #define TYPE isl_map
6795 #undef SUFFIX
6796 #define SUFFIX
6797 #undef EMPTY
6798 #define EMPTY isl_map_empty
6799 #undef ADD
6800 #define ADD isl_map_union_disjoint
6801 #include "isl_map_lexopt_templ.c"
6803 /* Given a map "map", compute the lexicographically minimal
6804 * (or maximal) image element for each domain element in "dom",
6805 * in the form of an isl_map.
6806 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6807 * do not have an image element.
6808 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6809 * should be computed over the domain of "map". "empty" is also NULL
6810 * in this case.
6812 * If the input consists of more than one disjunct, then first
6813 * compute the desired result in the form of an isl_pw_multi_aff and
6814 * then convert that into an isl_map.
6816 * This function used to have an explicit implementation in terms
6817 * of isl_maps, but it would continually intersect the domains of
6818 * partial results with the complement of the domain of the next
6819 * partial solution, potentially leading to an explosion in the number
6820 * of disjuncts if there are several disjuncts in the input.
6821 * An even earlier implementation of this function would look for
6822 * better results in the domain of the partial result and for extra
6823 * results in the complement of this domain, which would lead to
6824 * even more splintering.
6826 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6827 __isl_take isl_map *map, __isl_take isl_set *dom,
6828 __isl_give isl_set **empty, unsigned flags)
6830 int full;
6831 struct isl_map *res;
6832 isl_pw_multi_aff *pma;
6834 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6835 if (!map || (!full && !dom))
6836 goto error;
6838 if (isl_map_plain_is_empty(map)) {
6839 if (empty)
6840 *empty = dom;
6841 else
6842 isl_set_free(dom);
6843 return map;
6846 if (map->n == 1) {
6847 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6848 dom, empty, flags);
6849 isl_map_free(map);
6850 return res;
6853 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6854 flags);
6855 return isl_map_from_pw_multi_aff(pma);
6856 error:
6857 if (empty)
6858 *empty = NULL;
6859 isl_set_free(dom);
6860 isl_map_free(map);
6861 return NULL;
6864 __isl_give isl_map *isl_map_partial_lexmax(
6865 __isl_take isl_map *map, __isl_take isl_set *dom,
6866 __isl_give isl_set **empty)
6868 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6871 __isl_give isl_map *isl_map_partial_lexmin(
6872 __isl_take isl_map *map, __isl_take isl_set *dom,
6873 __isl_give isl_set **empty)
6875 return isl_map_partial_lexopt(map, dom, empty, 0);
6878 __isl_give isl_set *isl_set_partial_lexmin(
6879 __isl_take isl_set *set, __isl_take isl_set *dom,
6880 __isl_give isl_set **empty)
6882 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6883 dom, empty));
6886 __isl_give isl_set *isl_set_partial_lexmax(
6887 __isl_take isl_set *set, __isl_take isl_set *dom,
6888 __isl_give isl_set **empty)
6890 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6891 dom, empty));
6894 /* Compute the lexicographic minimum (or maximum if "flags" includes
6895 * ISL_OPT_MAX) of "bset" over its parametric domain.
6897 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6898 unsigned flags)
6900 return isl_basic_map_lexopt(bset, flags);
6903 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6905 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6908 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6910 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6913 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6915 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6918 /* Compute the lexicographic minimum of "bset" over its parametric domain
6919 * for the purpose of quantifier elimination.
6920 * That is, find an explicit representation for all the existentially
6921 * quantified variables in "bset" by computing their lexicographic
6922 * minimum.
6924 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6925 __isl_take isl_basic_set *bset)
6927 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6930 /* Given a basic map with one output dimension, compute the minimum or
6931 * maximum of that dimension as an isl_pw_aff.
6933 * Compute the optimum as a lexicographic optimum over the single
6934 * output dimension and extract the single isl_pw_aff from the result.
6936 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6937 int max)
6939 isl_pw_multi_aff *pma;
6940 isl_pw_aff *pwaff;
6942 bmap = isl_basic_map_copy(bmap);
6943 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6944 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6945 isl_pw_multi_aff_free(pma);
6947 return pwaff;
6950 /* Compute the minimum or maximum of the given output dimension
6951 * as a function of the parameters and the input dimensions,
6952 * but independently of the other output dimensions.
6954 * We first project out the other output dimension and then compute
6955 * the "lexicographic" maximum in each basic map, combining the results
6956 * using isl_pw_aff_union_max.
6958 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6959 int max)
6961 int i;
6962 isl_pw_aff *pwaff;
6963 isl_size n_out;
6965 n_out = isl_map_dim(map, isl_dim_out);
6966 if (n_out < 0)
6967 map = isl_map_free(map);
6968 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6969 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6970 if (!map)
6971 return NULL;
6973 if (map->n == 0) {
6974 isl_space *space = isl_map_get_space(map);
6975 isl_map_free(map);
6976 return isl_pw_aff_empty(space);
6979 pwaff = basic_map_dim_opt(map->p[0], max);
6980 for (i = 1; i < map->n; ++i) {
6981 isl_pw_aff *pwaff_i;
6983 pwaff_i = basic_map_dim_opt(map->p[i], max);
6984 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6987 isl_map_free(map);
6989 return pwaff;
6992 /* Compute the minimum of the given output dimension as a function of the
6993 * parameters and input dimensions, but independently of
6994 * the other output dimensions.
6996 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6998 return map_dim_opt(map, pos, 0);
7001 /* Compute the maximum of the given output dimension as a function of the
7002 * parameters and input dimensions, but independently of
7003 * the other output dimensions.
7005 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7007 return map_dim_opt(map, pos, 1);
7010 /* Compute the minimum or maximum of the given set dimension
7011 * as a function of the parameters,
7012 * but independently of the other set dimensions.
7014 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7015 int max)
7017 return map_dim_opt(set, pos, max);
7020 /* Compute the maximum of the given set dimension as a function of the
7021 * parameters, but independently of the other set dimensions.
7023 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7025 return set_dim_opt(set, pos, 1);
7028 /* Compute the minimum of the given set dimension as a function of the
7029 * parameters, but independently of the other set dimensions.
7031 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7033 return set_dim_opt(set, pos, 0);
7036 /* Apply a preimage specified by "mat" on the parameters of "bset".
7037 * bset is assumed to have only parameters and divs.
7039 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7040 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7042 isl_size nparam;
7044 nparam = isl_basic_set_dim(bset, isl_dim_param);
7045 if (nparam < 0 || !mat)
7046 goto error;
7048 bset->dim = isl_space_cow(bset->dim);
7049 if (!bset->dim)
7050 goto error;
7052 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7054 bset->dim->nparam = 0;
7055 bset->dim->n_out = nparam;
7056 bset = isl_basic_set_preimage(bset, mat);
7057 if (bset) {
7058 bset->dim->nparam = bset->dim->n_out;
7059 bset->dim->n_out = 0;
7061 return bset;
7062 error:
7063 isl_mat_free(mat);
7064 isl_basic_set_free(bset);
7065 return NULL;
7068 /* Apply a preimage specified by "mat" on the parameters of "set".
7069 * set is assumed to have only parameters and divs.
7071 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7072 __isl_take isl_mat *mat)
7074 isl_space *space;
7075 isl_size nparam;
7077 nparam = isl_set_dim(set, isl_dim_param);
7078 if (nparam < 0 || !mat)
7079 goto error;
7081 if (mat->n_row != 1 + nparam)
7082 isl_die(isl_set_get_ctx(set), isl_error_internal,
7083 "unexpected number of rows", goto error);
7085 space = isl_set_get_space(set);
7086 space = isl_space_move_dims(space, isl_dim_set, 0,
7087 isl_dim_param, 0, nparam);
7088 set = isl_set_reset_space(set, space);
7089 set = isl_set_preimage(set, mat);
7090 nparam = isl_set_dim(set, isl_dim_out);
7091 if (nparam < 0)
7092 set = isl_set_free(set);
7093 space = isl_set_get_space(set);
7094 space = isl_space_move_dims(space, isl_dim_param, 0,
7095 isl_dim_out, 0, nparam);
7096 set = isl_set_reset_space(set, space);
7097 return set;
7098 error:
7099 isl_mat_free(mat);
7100 isl_set_free(set);
7101 return NULL;
7104 /* Intersect the basic set "bset" with the affine space specified by the
7105 * equalities in "eq".
7107 static __isl_give isl_basic_set *basic_set_append_equalities(
7108 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7110 int i, k;
7111 unsigned len;
7113 if (!bset || !eq)
7114 goto error;
7116 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7117 eq->n_row, 0);
7118 if (!bset)
7119 goto error;
7121 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7122 for (i = 0; i < eq->n_row; ++i) {
7123 k = isl_basic_set_alloc_equality(bset);
7124 if (k < 0)
7125 goto error;
7126 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7127 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7129 isl_mat_free(eq);
7131 bset = isl_basic_set_gauss(bset, NULL);
7132 bset = isl_basic_set_finalize(bset);
7134 return bset;
7135 error:
7136 isl_mat_free(eq);
7137 isl_basic_set_free(bset);
7138 return NULL;
7141 /* Intersect the set "set" with the affine space specified by the
7142 * equalities in "eq".
7144 static struct isl_set *set_append_equalities(struct isl_set *set,
7145 struct isl_mat *eq)
7147 int i;
7149 if (!set || !eq)
7150 goto error;
7152 for (i = 0; i < set->n; ++i) {
7153 set->p[i] = basic_set_append_equalities(set->p[i],
7154 isl_mat_copy(eq));
7155 if (!set->p[i])
7156 goto error;
7158 isl_mat_free(eq);
7159 return set;
7160 error:
7161 isl_mat_free(eq);
7162 isl_set_free(set);
7163 return NULL;
7166 /* Given a basic set "bset" that only involves parameters and existentially
7167 * quantified variables, return the index of the first equality
7168 * that only involves parameters. If there is no such equality then
7169 * return bset->n_eq.
7171 * This function assumes that isl_basic_set_gauss has been called on "bset".
7173 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7175 int i, j;
7176 isl_size nparam, n_div;
7178 nparam = isl_basic_set_dim(bset, isl_dim_param);
7179 n_div = isl_basic_set_dim(bset, isl_dim_div);
7180 if (nparam < 0 || n_div < 0)
7181 return -1;
7183 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7184 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7185 ++i;
7188 return i;
7191 /* Compute an explicit representation for the existentially quantified
7192 * variables in "bset" by computing the "minimal value" of the set
7193 * variables. Since there are no set variables, the computation of
7194 * the minimal value essentially computes an explicit representation
7195 * of the non-empty part(s) of "bset".
7197 * The input only involves parameters and existentially quantified variables.
7198 * All equalities among parameters have been removed.
7200 * Since the existentially quantified variables in the result are in general
7201 * going to be different from those in the input, we first replace
7202 * them by the minimal number of variables based on their equalities.
7203 * This should simplify the parametric integer programming.
7205 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7207 isl_morph *morph1, *morph2;
7208 isl_set *set;
7209 isl_size n;
7211 if (!bset)
7212 return NULL;
7213 if (bset->n_eq == 0)
7214 return isl_basic_set_lexmin_compute_divs(bset);
7216 morph1 = isl_basic_set_parameter_compression(bset);
7217 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7218 bset = isl_basic_set_lift(bset);
7219 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7220 bset = isl_morph_basic_set(morph2, bset);
7221 n = isl_basic_set_dim(bset, isl_dim_set);
7222 if (n < 0)
7223 bset = isl_basic_set_free(bset);
7224 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7226 set = isl_basic_set_lexmin_compute_divs(bset);
7228 set = isl_morph_set(isl_morph_inverse(morph1), set);
7230 return set;
7233 /* Project the given basic set onto its parameter domain, possibly introducing
7234 * new, explicit, existential variables in the constraints.
7235 * The input has parameters and (possibly implicit) existential variables.
7236 * The output has the same parameters, but only
7237 * explicit existentially quantified variables.
7239 * The actual projection is performed by pip, but pip doesn't seem
7240 * to like equalities very much, so we first remove the equalities
7241 * among the parameters by performing a variable compression on
7242 * the parameters. Afterward, an inverse transformation is performed
7243 * and the equalities among the parameters are inserted back in.
7245 * The variable compression on the parameters may uncover additional
7246 * equalities that were only implicit before. We therefore check
7247 * if there are any new parameter equalities in the result and
7248 * if so recurse. The removal of parameter equalities is required
7249 * for the parameter compression performed by base_compute_divs.
7251 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7253 int i;
7254 struct isl_mat *eq;
7255 struct isl_mat *T, *T2;
7256 struct isl_set *set;
7257 isl_size nparam;
7259 bset = isl_basic_set_cow(bset);
7260 if (!bset)
7261 return NULL;
7263 if (bset->n_eq == 0)
7264 return base_compute_divs(bset);
7266 bset = isl_basic_set_gauss(bset, NULL);
7267 if (!bset)
7268 return NULL;
7269 if (isl_basic_set_plain_is_empty(bset))
7270 return isl_set_from_basic_set(bset);
7272 i = first_parameter_equality(bset);
7273 if (i == bset->n_eq)
7274 return base_compute_divs(bset);
7276 nparam = isl_basic_set_dim(bset, isl_dim_param);
7277 if (nparam < 0)
7278 return isl_set_from_basic_set(isl_basic_set_free(bset));
7279 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7280 0, 1 + nparam);
7281 eq = isl_mat_cow(eq);
7282 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7283 if (T && T->n_col == 0) {
7284 isl_mat_free(T);
7285 isl_mat_free(T2);
7286 isl_mat_free(eq);
7287 bset = isl_basic_set_set_to_empty(bset);
7288 return isl_set_from_basic_set(bset);
7290 bset = basic_set_parameter_preimage(bset, T);
7292 i = first_parameter_equality(bset);
7293 if (!bset)
7294 set = NULL;
7295 else if (i == bset->n_eq)
7296 set = base_compute_divs(bset);
7297 else
7298 set = parameter_compute_divs(bset);
7299 set = set_parameter_preimage(set, T2);
7300 set = set_append_equalities(set, eq);
7301 return set;
7304 /* Insert the divs from "ls" before those of "bmap".
7306 * The number of columns is not changed, which means that the last
7307 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7308 * The caller is responsible for removing the same number of dimensions
7309 * from the space of "bmap".
7311 static __isl_give isl_basic_map *insert_divs_from_local_space(
7312 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7314 int i;
7315 isl_size n_div;
7316 int old_n_div;
7318 n_div = isl_local_space_dim(ls, isl_dim_div);
7319 if (n_div < 0)
7320 return isl_basic_map_free(bmap);
7321 if (n_div == 0)
7322 return bmap;
7324 old_n_div = bmap->n_div;
7325 bmap = insert_div_rows(bmap, n_div);
7326 if (!bmap)
7327 return NULL;
7329 for (i = 0; i < n_div; ++i) {
7330 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7331 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7334 return bmap;
7337 /* Replace the space of "bmap" by the space and divs of "ls".
7339 * If "ls" has any divs, then we simplify the result since we may
7340 * have discovered some additional equalities that could simplify
7341 * the div expressions.
7343 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7344 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7346 isl_size n_div;
7348 bmap = isl_basic_map_cow(bmap);
7349 n_div = isl_local_space_dim(ls, isl_dim_div);
7350 if (!bmap || n_div < 0)
7351 goto error;
7353 bmap = insert_divs_from_local_space(bmap, ls);
7354 if (!bmap)
7355 goto error;
7357 isl_space_free(bmap->dim);
7358 bmap->dim = isl_local_space_get_space(ls);
7359 if (!bmap->dim)
7360 goto error;
7362 isl_local_space_free(ls);
7363 if (n_div > 0)
7364 bmap = isl_basic_map_simplify(bmap);
7365 bmap = isl_basic_map_finalize(bmap);
7366 return bmap;
7367 error:
7368 isl_basic_map_free(bmap);
7369 isl_local_space_free(ls);
7370 return NULL;
7373 /* Replace the space of "map" by the space and divs of "ls".
7375 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7376 __isl_take isl_local_space *ls)
7378 int i;
7380 map = isl_map_cow(map);
7381 if (!map || !ls)
7382 goto error;
7384 for (i = 0; i < map->n; ++i) {
7385 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7386 isl_local_space_copy(ls));
7387 if (!map->p[i])
7388 goto error;
7390 isl_space_free(map->dim);
7391 map->dim = isl_local_space_get_space(ls);
7392 if (!map->dim)
7393 goto error;
7395 isl_local_space_free(ls);
7396 return map;
7397 error:
7398 isl_local_space_free(ls);
7399 isl_map_free(map);
7400 return NULL;
7403 /* Compute an explicit representation for the existentially
7404 * quantified variables for which do not know any explicit representation yet.
7406 * We first sort the existentially quantified variables so that the
7407 * existentially quantified variables for which we already have an explicit
7408 * representation are placed before those for which we do not.
7409 * The input dimensions, the output dimensions and the existentially
7410 * quantified variables for which we already have an explicit
7411 * representation are then turned into parameters.
7412 * compute_divs returns a map with the same parameters and
7413 * no input or output dimensions and the dimension specification
7414 * is reset to that of the input, including the existentially quantified
7415 * variables for which we already had an explicit representation.
7417 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7419 struct isl_basic_set *bset;
7420 struct isl_set *set;
7421 struct isl_map *map;
7422 isl_space *space;
7423 isl_local_space *ls;
7424 isl_size nparam;
7425 isl_size n_in;
7426 isl_size n_out;
7427 int n_known;
7428 int i;
7430 bmap = isl_basic_map_sort_divs(bmap);
7431 bmap = isl_basic_map_cow(bmap);
7432 if (!bmap)
7433 return NULL;
7435 n_known = isl_basic_map_first_unknown_div(bmap);
7436 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7437 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7438 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7439 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7440 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7442 space = isl_space_set_alloc(bmap->ctx,
7443 nparam + n_in + n_out + n_known, 0);
7444 if (!space)
7445 goto error;
7447 ls = isl_basic_map_get_local_space(bmap);
7448 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7449 n_known, bmap->n_div - n_known);
7450 if (n_known > 0) {
7451 for (i = n_known; i < bmap->n_div; ++i)
7452 swap_div(bmap, i - n_known, i);
7453 bmap->n_div -= n_known;
7454 bmap->extra -= n_known;
7456 bmap = isl_basic_map_reset_space(bmap, space);
7457 bset = bset_from_bmap(bmap);
7459 set = parameter_compute_divs(bset);
7460 map = set_to_map(set);
7461 map = replace_space_by_local_space(map, ls);
7463 return map;
7464 error:
7465 isl_basic_map_free(bmap);
7466 return NULL;
7469 /* Remove the explicit representation of local variable "div",
7470 * if there is any.
7472 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7473 __isl_take isl_basic_map *bmap, int div)
7475 isl_bool unknown;
7477 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7478 if (unknown < 0)
7479 return isl_basic_map_free(bmap);
7480 if (unknown)
7481 return bmap;
7483 bmap = isl_basic_map_cow(bmap);
7484 if (!bmap)
7485 return NULL;
7486 isl_int_set_si(bmap->div[div][0], 0);
7487 return bmap;
7490 /* Is local variable "div" of "bmap" marked as not having an explicit
7491 * representation?
7492 * Note that even if "div" is not marked in this way and therefore
7493 * has an explicit representation, this representation may still
7494 * depend (indirectly) on other local variables that do not
7495 * have an explicit representation.
7497 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7498 int div)
7500 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7501 return isl_bool_error;
7502 return isl_int_is_zero(bmap->div[div][0]);
7505 /* Return the position of the first local variable that does not
7506 * have an explicit representation.
7507 * Return the total number of local variables if they all have
7508 * an explicit representation.
7509 * Return -1 on error.
7511 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7513 int i;
7515 if (!bmap)
7516 return -1;
7518 for (i = 0; i < bmap->n_div; ++i) {
7519 if (!isl_basic_map_div_is_known(bmap, i))
7520 return i;
7522 return bmap->n_div;
7525 /* Return the position of the first local variable that does not
7526 * have an explicit representation.
7527 * Return the total number of local variables if they all have
7528 * an explicit representation.
7529 * Return -1 on error.
7531 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7533 return isl_basic_map_first_unknown_div(bset);
7536 /* Does "bmap" have an explicit representation for all local variables?
7538 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7540 int first;
7541 isl_size n;
7543 n = isl_basic_map_dim(bmap, isl_dim_div);
7544 first = isl_basic_map_first_unknown_div(bmap);
7545 if (n < 0 || first < 0)
7546 return isl_bool_error;
7547 return first == n;
7550 /* Do all basic maps in "map" have an explicit representation
7551 * for all local variables?
7553 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7555 int i;
7557 if (!map)
7558 return isl_bool_error;
7560 for (i = 0; i < map->n; ++i) {
7561 int known = isl_basic_map_divs_known(map->p[i]);
7562 if (known <= 0)
7563 return known;
7566 return isl_bool_true;
7569 /* If bmap contains any unknown divs, then compute explicit
7570 * expressions for them. However, this computation may be
7571 * quite expensive, so first try to remove divs that aren't
7572 * strictly needed.
7574 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7576 int known;
7577 struct isl_map *map;
7579 known = isl_basic_map_divs_known(bmap);
7580 if (known < 0)
7581 goto error;
7582 if (known)
7583 return isl_map_from_basic_map(bmap);
7585 bmap = isl_basic_map_drop_redundant_divs(bmap);
7587 known = isl_basic_map_divs_known(bmap);
7588 if (known < 0)
7589 goto error;
7590 if (known)
7591 return isl_map_from_basic_map(bmap);
7593 map = compute_divs(bmap);
7594 return map;
7595 error:
7596 isl_basic_map_free(bmap);
7597 return NULL;
7600 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7602 int i;
7603 int known;
7604 struct isl_map *res;
7606 if (!map)
7607 return NULL;
7608 if (map->n == 0)
7609 return map;
7611 known = isl_map_divs_known(map);
7612 if (known < 0) {
7613 isl_map_free(map);
7614 return NULL;
7616 if (known)
7617 return map;
7619 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7620 for (i = 1 ; i < map->n; ++i) {
7621 struct isl_map *r2;
7622 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7623 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7624 res = isl_map_union_disjoint(res, r2);
7625 else
7626 res = isl_map_union(res, r2);
7628 isl_map_free(map);
7630 return res;
7633 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7635 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7638 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7640 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7643 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7645 int i;
7646 struct isl_set *set;
7648 if (!map)
7649 goto error;
7651 map = isl_map_cow(map);
7652 if (!map)
7653 return NULL;
7655 set = set_from_map(map);
7656 set->dim = isl_space_domain(set->dim);
7657 if (!set->dim)
7658 goto error;
7659 for (i = 0; i < map->n; ++i) {
7660 set->p[i] = isl_basic_map_domain(map->p[i]);
7661 if (!set->p[i])
7662 goto error;
7664 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7665 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7666 return set;
7667 error:
7668 isl_map_free(map);
7669 return NULL;
7672 /* Return the union of "map1" and "map2", where we assume for now that
7673 * "map1" and "map2" are disjoint. Note that the basic maps inside
7674 * "map1" or "map2" may not be disjoint from each other.
7675 * Also note that this function is also called from isl_map_union,
7676 * which takes care of handling the situation where "map1" and "map2"
7677 * may not be disjoint.
7679 * If one of the inputs is empty, we can simply return the other input.
7680 * Similarly, if one of the inputs is universal, then it is equal to the union.
7682 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7683 __isl_take isl_map *map2)
7685 int i;
7686 unsigned flags = 0;
7687 struct isl_map *map = NULL;
7688 int is_universe;
7690 if (!map1 || !map2)
7691 goto error;
7693 if (!isl_space_is_equal(map1->dim, map2->dim))
7694 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7695 "spaces don't match", goto error);
7697 if (map1->n == 0) {
7698 isl_map_free(map1);
7699 return map2;
7701 if (map2->n == 0) {
7702 isl_map_free(map2);
7703 return map1;
7706 is_universe = isl_map_plain_is_universe(map1);
7707 if (is_universe < 0)
7708 goto error;
7709 if (is_universe) {
7710 isl_map_free(map2);
7711 return map1;
7714 is_universe = isl_map_plain_is_universe(map2);
7715 if (is_universe < 0)
7716 goto error;
7717 if (is_universe) {
7718 isl_map_free(map1);
7719 return map2;
7722 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7723 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7724 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7726 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7727 map1->n + map2->n, flags);
7728 if (!map)
7729 goto error;
7730 for (i = 0; i < map1->n; ++i) {
7731 map = isl_map_add_basic_map(map,
7732 isl_basic_map_copy(map1->p[i]));
7733 if (!map)
7734 goto error;
7736 for (i = 0; i < map2->n; ++i) {
7737 map = isl_map_add_basic_map(map,
7738 isl_basic_map_copy(map2->p[i]));
7739 if (!map)
7740 goto error;
7742 isl_map_free(map1);
7743 isl_map_free(map2);
7744 return map;
7745 error:
7746 isl_map_free(map);
7747 isl_map_free(map1);
7748 isl_map_free(map2);
7749 return NULL;
7752 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7753 * guaranteed to be disjoint by the caller.
7755 * Note that this functions is called from within isl_map_make_disjoint,
7756 * so we have to be careful not to touch the constraints of the inputs
7757 * in any way.
7759 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7760 __isl_take isl_map *map2)
7762 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7765 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7766 * not be disjoint. The parameters are assumed to have been aligned.
7768 * We currently simply call map_union_disjoint, the internal operation
7769 * of which does not really depend on the inputs being disjoint.
7770 * If the result contains more than one basic map, then we clear
7771 * the disjoint flag since the result may contain basic maps from
7772 * both inputs and these are not guaranteed to be disjoint.
7774 * As a special case, if "map1" and "map2" are obviously equal,
7775 * then we simply return "map1".
7777 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7778 __isl_take isl_map *map2)
7780 int equal;
7782 if (!map1 || !map2)
7783 goto error;
7785 equal = isl_map_plain_is_equal(map1, map2);
7786 if (equal < 0)
7787 goto error;
7788 if (equal) {
7789 isl_map_free(map2);
7790 return map1;
7793 map1 = map_union_disjoint(map1, map2);
7794 if (!map1)
7795 return NULL;
7796 if (map1->n > 1)
7797 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7798 return map1;
7799 error:
7800 isl_map_free(map1);
7801 isl_map_free(map2);
7802 return NULL;
7805 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7806 * not be disjoint.
7808 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7809 __isl_take isl_map *map2)
7811 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7814 __isl_give isl_set *isl_set_union_disjoint(
7815 __isl_take isl_set *set1, __isl_take isl_set *set2)
7817 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7818 set_to_map(set2)));
7821 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7823 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7826 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7827 * the results.
7829 * "map" and "set" are assumed to be compatible and non-NULL.
7831 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7832 __isl_take isl_set *set,
7833 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7834 __isl_take isl_basic_set *bset))
7836 unsigned flags = 0;
7837 struct isl_map *result;
7838 int i, j;
7840 if (isl_set_plain_is_universe(set)) {
7841 isl_set_free(set);
7842 return map;
7845 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7846 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7847 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7849 result = isl_map_alloc_space(isl_space_copy(map->dim),
7850 map->n * set->n, flags);
7851 for (i = 0; result && i < map->n; ++i)
7852 for (j = 0; j < set->n; ++j) {
7853 result = isl_map_add_basic_map(result,
7854 fn(isl_basic_map_copy(map->p[i]),
7855 isl_basic_set_copy(set->p[j])));
7856 if (!result)
7857 break;
7860 isl_map_free(map);
7861 isl_set_free(set);
7862 return result;
7865 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7866 __isl_take isl_set *set)
7868 isl_bool ok;
7870 ok = isl_map_compatible_range(map, set);
7871 if (ok < 0)
7872 goto error;
7873 if (!ok)
7874 isl_die(set->ctx, isl_error_invalid,
7875 "incompatible spaces", goto error);
7877 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7878 error:
7879 isl_map_free(map);
7880 isl_set_free(set);
7881 return NULL;
7884 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7885 __isl_take isl_set *set)
7887 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7890 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7891 __isl_take isl_set *set)
7893 isl_bool ok;
7895 ok = isl_map_compatible_domain(map, set);
7896 if (ok < 0)
7897 goto error;
7898 if (!ok)
7899 isl_die(set->ctx, isl_error_invalid,
7900 "incompatible spaces", goto error);
7902 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7903 error:
7904 isl_map_free(map);
7905 isl_set_free(set);
7906 return NULL;
7909 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7910 __isl_take isl_set *set)
7912 return isl_map_align_params_map_map_and(map, set,
7913 &map_intersect_domain);
7916 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7917 * in the space B -> C, return the intersection.
7918 * The parameters are assumed to have been aligned.
7920 * The map "factor" is first extended to a map living in the space
7921 * [A -> B] -> C and then a regular intersection is computed.
7923 static __isl_give isl_map *map_intersect_domain_factor_range(
7924 __isl_take isl_map *map, __isl_take isl_map *factor)
7926 isl_space *space;
7927 isl_map *ext_factor;
7929 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7930 ext_factor = isl_map_universe(space);
7931 ext_factor = isl_map_domain_product(ext_factor, factor);
7932 return map_intersect(map, ext_factor);
7935 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7936 * in the space B -> C, return the intersection.
7938 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7939 __isl_take isl_map *map, __isl_take isl_map *factor)
7941 return isl_map_align_params_map_map_and(map, factor,
7942 &map_intersect_domain_factor_range);
7945 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7946 * in the space A -> C, return the intersection.
7948 * The map "factor" is first extended to a map living in the space
7949 * A -> [B -> C] and then a regular intersection is computed.
7951 static __isl_give isl_map *map_intersect_range_factor_range(
7952 __isl_take isl_map *map, __isl_take isl_map *factor)
7954 isl_space *space;
7955 isl_map *ext_factor;
7957 space = isl_space_range_factor_domain(isl_map_get_space(map));
7958 ext_factor = isl_map_universe(space);
7959 ext_factor = isl_map_range_product(ext_factor, factor);
7960 return isl_map_intersect(map, ext_factor);
7963 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7964 * in the space A -> C, return the intersection.
7966 __isl_give isl_map *isl_map_intersect_range_factor_range(
7967 __isl_take isl_map *map, __isl_take isl_map *factor)
7969 return isl_map_align_params_map_map_and(map, factor,
7970 &map_intersect_range_factor_range);
7973 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7974 __isl_take isl_map *map2)
7976 if (!map1 || !map2)
7977 goto error;
7978 map1 = isl_map_reverse(map1);
7979 map1 = isl_map_apply_range(map1, map2);
7980 return isl_map_reverse(map1);
7981 error:
7982 isl_map_free(map1);
7983 isl_map_free(map2);
7984 return NULL;
7987 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7988 __isl_take isl_map *map2)
7990 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7993 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7994 __isl_take isl_map *map2)
7996 isl_space *space;
7997 struct isl_map *result;
7998 int i, j;
8000 if (!map1 || !map2)
8001 goto error;
8003 space = isl_space_join(isl_space_copy(map1->dim),
8004 isl_space_copy(map2->dim));
8006 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8007 if (!result)
8008 goto error;
8009 for (i = 0; i < map1->n; ++i)
8010 for (j = 0; j < map2->n; ++j) {
8011 result = isl_map_add_basic_map(result,
8012 isl_basic_map_apply_range(
8013 isl_basic_map_copy(map1->p[i]),
8014 isl_basic_map_copy(map2->p[j])));
8015 if (!result)
8016 goto error;
8018 isl_map_free(map1);
8019 isl_map_free(map2);
8020 if (result && result->n <= 1)
8021 ISL_F_SET(result, ISL_MAP_DISJOINT);
8022 return result;
8023 error:
8024 isl_map_free(map1);
8025 isl_map_free(map2);
8026 return NULL;
8029 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8030 __isl_take isl_map *map2)
8032 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8036 * returns range - domain
8038 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8040 isl_space *target_space;
8041 struct isl_basic_set *bset;
8042 isl_size dim;
8043 isl_size nparam;
8044 isl_size total;
8045 int i;
8047 if (!bmap)
8048 goto error;
8049 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8050 bmap->dim, isl_dim_out),
8051 goto error);
8052 dim = isl_basic_map_dim(bmap, isl_dim_in);
8053 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8054 if (dim < 0 || nparam < 0)
8055 goto error;
8056 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8057 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8058 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8059 total = isl_basic_map_dim(bmap, isl_dim_all);
8060 if (total < 0)
8061 bmap = isl_basic_map_free(bmap);
8062 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8063 for (i = 0; i < dim; ++i) {
8064 int j = isl_basic_map_alloc_equality(bmap);
8065 if (j < 0) {
8066 bmap = isl_basic_map_free(bmap);
8067 break;
8069 isl_seq_clr(bmap->eq[j], 1 + total);
8070 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8071 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8072 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8074 bset = isl_basic_map_domain(bmap);
8075 bset = isl_basic_set_reset_space(bset, target_space);
8076 return bset;
8077 error:
8078 isl_basic_map_free(bmap);
8079 return NULL;
8082 /* Check that domain and range of "map" are the same.
8084 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8086 isl_space *space;
8087 isl_bool equal;
8089 space = isl_map_peek_space(map);
8090 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8091 if (equal < 0)
8092 return isl_stat_error;
8093 if (!equal)
8094 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8095 "domain and range don't match", return isl_stat_error);
8096 return isl_stat_ok;
8100 * returns range - domain
8102 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8104 int i;
8105 isl_space *dim;
8106 struct isl_set *result;
8108 if (!map)
8109 return NULL;
8111 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8112 map->dim, isl_dim_out),
8113 goto error);
8114 dim = isl_map_get_space(map);
8115 dim = isl_space_domain(dim);
8116 result = isl_set_alloc_space(dim, map->n, 0);
8117 if (!result)
8118 goto error;
8119 for (i = 0; i < map->n; ++i)
8120 result = isl_set_add_basic_set(result,
8121 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8122 isl_map_free(map);
8123 return result;
8124 error:
8125 isl_map_free(map);
8126 return NULL;
8130 * returns [domain -> range] -> range - domain
8132 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8133 __isl_take isl_basic_map *bmap)
8135 int i, k;
8136 isl_space *space;
8137 isl_basic_map *domain;
8138 isl_size nparam, n;
8139 isl_size total;
8141 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8142 bmap->dim, isl_dim_out))
8143 isl_die(bmap->ctx, isl_error_invalid,
8144 "domain and range don't match", goto error);
8146 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8147 n = isl_basic_map_dim(bmap, isl_dim_in);
8148 if (nparam < 0 || n)
8149 return isl_basic_map_free(bmap);
8151 space = isl_basic_map_get_space(bmap);
8152 space = isl_space_from_range(isl_space_domain(space));
8153 domain = isl_basic_map_universe(space);
8155 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8156 bmap = isl_basic_map_apply_range(bmap, domain);
8157 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8159 total = isl_basic_map_dim(bmap, isl_dim_all);
8160 if (total < 0)
8161 return isl_basic_map_free(bmap);
8163 for (i = 0; i < n; ++i) {
8164 k = isl_basic_map_alloc_equality(bmap);
8165 if (k < 0)
8166 goto error;
8167 isl_seq_clr(bmap->eq[k], 1 + total);
8168 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8169 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8170 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8173 bmap = isl_basic_map_gauss(bmap, NULL);
8174 return isl_basic_map_finalize(bmap);
8175 error:
8176 isl_basic_map_free(bmap);
8177 return NULL;
8181 * returns [domain -> range] -> range - domain
8183 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8185 int i;
8186 isl_space *domain_space;
8188 if (isl_map_check_equal_tuples(map) < 0)
8189 return isl_map_free(map);
8191 map = isl_map_cow(map);
8192 if (!map)
8193 return NULL;
8195 domain_space = isl_space_domain(isl_map_get_space(map));
8196 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8197 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8198 if (!map->dim)
8199 goto error;
8200 for (i = 0; i < map->n; ++i) {
8201 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8202 if (!map->p[i])
8203 goto error;
8205 map = isl_map_unmark_normalized(map);
8206 return map;
8207 error:
8208 isl_map_free(map);
8209 return NULL;
8212 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8214 isl_size n_in, n_out;
8216 n_in = isl_space_dim(space, isl_dim_in);
8217 n_out = isl_space_dim(space, isl_dim_out);
8218 if (n_in < 0 || n_out < 0)
8219 goto error;
8220 if (n_in != n_out)
8221 isl_die(space->ctx, isl_error_invalid,
8222 "number of input and output dimensions needs to be "
8223 "the same", goto error);
8224 return isl_basic_map_equal(space, n_in);
8225 error:
8226 isl_space_free(space);
8227 return NULL;
8230 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8232 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8235 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8237 isl_space *dim = isl_set_get_space(set);
8238 isl_map *id;
8239 id = isl_map_identity(isl_space_map_from_set(dim));
8240 return isl_map_intersect_range(id, set);
8243 /* Construct a basic set with all set dimensions having only non-negative
8244 * values.
8246 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8247 __isl_take isl_space *space)
8249 int i;
8250 isl_size nparam;
8251 isl_size dim;
8252 isl_size total;
8253 struct isl_basic_set *bset;
8255 nparam = isl_space_dim(space, isl_dim_param);
8256 dim = isl_space_dim(space, isl_dim_set);
8257 total = isl_space_dim(space, isl_dim_all);
8258 if (nparam < 0 || dim < 0 || total < 0)
8259 space = isl_space_free(space);
8260 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8261 if (!bset)
8262 return NULL;
8263 for (i = 0; i < dim; ++i) {
8264 int k = isl_basic_set_alloc_inequality(bset);
8265 if (k < 0)
8266 goto error;
8267 isl_seq_clr(bset->ineq[k], 1 + total);
8268 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8270 return bset;
8271 error:
8272 isl_basic_set_free(bset);
8273 return NULL;
8276 /* Construct the half-space x_pos >= 0.
8278 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8279 int pos)
8281 int k;
8282 isl_size total;
8283 isl_basic_set *nonneg;
8285 total = isl_space_dim(space, isl_dim_all);
8286 if (total < 0)
8287 space = isl_space_free(space);
8288 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8289 k = isl_basic_set_alloc_inequality(nonneg);
8290 if (k < 0)
8291 goto error;
8292 isl_seq_clr(nonneg->ineq[k], 1 + total);
8293 isl_int_set_si(nonneg->ineq[k][pos], 1);
8295 return isl_basic_set_finalize(nonneg);
8296 error:
8297 isl_basic_set_free(nonneg);
8298 return NULL;
8301 /* Construct the half-space x_pos <= -1.
8303 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8304 int pos)
8306 int k;
8307 isl_size total;
8308 isl_basic_set *neg;
8310 total = isl_space_dim(space, isl_dim_all);
8311 if (total < 0)
8312 space = isl_space_free(space);
8313 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8314 k = isl_basic_set_alloc_inequality(neg);
8315 if (k < 0)
8316 goto error;
8317 isl_seq_clr(neg->ineq[k], 1 + total);
8318 isl_int_set_si(neg->ineq[k][0], -1);
8319 isl_int_set_si(neg->ineq[k][pos], -1);
8321 return isl_basic_set_finalize(neg);
8322 error:
8323 isl_basic_set_free(neg);
8324 return NULL;
8327 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8328 enum isl_dim_type type, unsigned first, unsigned n)
8330 int i;
8331 unsigned offset;
8332 isl_basic_set *nonneg;
8333 isl_basic_set *neg;
8335 if (n == 0)
8336 return set;
8338 if (isl_set_check_range(set, type, first, n) < 0)
8339 return isl_set_free(set);
8341 offset = pos(set->dim, type);
8342 for (i = 0; i < n; ++i) {
8343 nonneg = nonneg_halfspace(isl_set_get_space(set),
8344 offset + first + i);
8345 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8347 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8350 return set;
8353 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8354 int len,
8355 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8356 void *user)
8358 isl_set *half;
8360 if (!set)
8361 return isl_stat_error;
8362 if (isl_set_plain_is_empty(set)) {
8363 isl_set_free(set);
8364 return isl_stat_ok;
8366 if (first == len)
8367 return fn(set, signs, user);
8369 signs[first] = 1;
8370 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8371 1 + first));
8372 half = isl_set_intersect(half, isl_set_copy(set));
8373 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8374 goto error;
8376 signs[first] = -1;
8377 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8378 1 + first));
8379 half = isl_set_intersect(half, set);
8380 return foreach_orthant(half, signs, first + 1, len, fn, user);
8381 error:
8382 isl_set_free(set);
8383 return isl_stat_error;
8386 /* Call "fn" on the intersections of "set" with each of the orthants
8387 * (except for obviously empty intersections). The orthant is identified
8388 * by the signs array, with each entry having value 1 or -1 according
8389 * to the sign of the corresponding variable.
8391 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8392 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8393 void *user)
8395 isl_size nparam;
8396 isl_size nvar;
8397 int *signs;
8398 isl_stat r;
8400 if (!set)
8401 return isl_stat_error;
8402 if (isl_set_plain_is_empty(set))
8403 return isl_stat_ok;
8405 nparam = isl_set_dim(set, isl_dim_param);
8406 nvar = isl_set_dim(set, isl_dim_set);
8407 if (nparam < 0 || nvar < 0)
8408 return isl_stat_error;
8410 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8412 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8413 fn, user);
8415 free(signs);
8417 return r;
8420 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8422 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8425 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8426 __isl_keep isl_basic_map *bmap2)
8428 isl_bool is_subset;
8429 struct isl_map *map1;
8430 struct isl_map *map2;
8432 if (!bmap1 || !bmap2)
8433 return isl_bool_error;
8435 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8436 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8438 is_subset = isl_map_is_subset(map1, map2);
8440 isl_map_free(map1);
8441 isl_map_free(map2);
8443 return is_subset;
8446 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8447 __isl_keep isl_basic_set *bset2)
8449 return isl_basic_map_is_subset(bset1, bset2);
8452 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8453 __isl_keep isl_basic_map *bmap2)
8455 isl_bool is_subset;
8457 if (!bmap1 || !bmap2)
8458 return isl_bool_error;
8459 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8460 if (is_subset != isl_bool_true)
8461 return is_subset;
8462 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8463 return is_subset;
8466 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8467 __isl_keep isl_basic_set *bset2)
8469 return isl_basic_map_is_equal(
8470 bset_to_bmap(bset1), bset_to_bmap(bset2));
8473 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8475 int i;
8476 int is_empty;
8478 if (!map)
8479 return isl_bool_error;
8480 for (i = 0; i < map->n; ++i) {
8481 is_empty = isl_basic_map_is_empty(map->p[i]);
8482 if (is_empty < 0)
8483 return isl_bool_error;
8484 if (!is_empty)
8485 return isl_bool_false;
8487 return isl_bool_true;
8490 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8492 return map ? map->n == 0 : isl_bool_error;
8495 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8497 return set ? set->n == 0 : isl_bool_error;
8500 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8502 return isl_map_is_empty(set_to_map(set));
8505 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8506 __isl_keep isl_map *map2)
8508 if (!map1 || !map2)
8509 return isl_bool_error;
8511 return isl_space_is_equal(map1->dim, map2->dim);
8514 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8515 __isl_keep isl_set *set2)
8517 if (!set1 || !set2)
8518 return isl_bool_error;
8520 return isl_space_is_equal(set1->dim, set2->dim);
8523 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8525 isl_bool is_subset;
8527 if (!map1 || !map2)
8528 return isl_bool_error;
8529 is_subset = isl_map_is_subset(map1, map2);
8530 if (is_subset != isl_bool_true)
8531 return is_subset;
8532 is_subset = isl_map_is_subset(map2, map1);
8533 return is_subset;
8536 /* Is "map1" equal to "map2"?
8538 * First check if they are obviously equal.
8539 * If not, then perform a more detailed analysis.
8541 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8543 isl_bool equal;
8545 equal = isl_map_plain_is_equal(map1, map2);
8546 if (equal < 0 || equal)
8547 return equal;
8548 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8551 isl_bool isl_basic_map_is_strict_subset(
8552 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8554 isl_bool is_subset;
8556 if (!bmap1 || !bmap2)
8557 return isl_bool_error;
8558 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8559 if (is_subset != isl_bool_true)
8560 return is_subset;
8561 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8562 return isl_bool_not(is_subset);
8565 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8566 __isl_keep isl_map *map2)
8568 isl_bool is_subset;
8570 if (!map1 || !map2)
8571 return isl_bool_error;
8572 is_subset = isl_map_is_subset(map1, map2);
8573 if (is_subset != isl_bool_true)
8574 return is_subset;
8575 is_subset = isl_map_is_subset(map2, map1);
8576 return isl_bool_not(is_subset);
8579 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8580 __isl_keep isl_set *set2)
8582 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8585 /* Is "bmap" obviously equal to the universe with the same space?
8587 * That is, does it not have any constraints?
8589 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8591 if (!bmap)
8592 return isl_bool_error;
8593 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8596 /* Is "bset" obviously equal to the universe with the same space?
8598 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8600 return isl_basic_map_plain_is_universe(bset);
8603 /* If "c" does not involve any existentially quantified variables,
8604 * then set *univ to false and abort
8606 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8608 isl_bool *univ = user;
8609 isl_size n;
8611 n = isl_constraint_dim(c, isl_dim_div);
8612 if (n < 0)
8613 c = isl_constraint_free(c);
8614 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8615 isl_constraint_free(c);
8616 if (*univ < 0 || !*univ)
8617 return isl_stat_error;
8618 return isl_stat_ok;
8621 /* Is "bmap" equal to the universe with the same space?
8623 * First check if it is obviously equal to the universe.
8624 * If not and if there are any constraints not involving
8625 * existentially quantified variables, then it is certainly
8626 * not equal to the universe.
8627 * Otherwise, check if the universe is a subset of "bmap".
8629 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8631 isl_size n_div;
8632 isl_bool univ;
8633 isl_basic_map *test;
8635 univ = isl_basic_map_plain_is_universe(bmap);
8636 if (univ < 0 || univ)
8637 return univ;
8638 n_div = isl_basic_map_dim(bmap, isl_dim_div);
8639 if (n_div < 0)
8640 return isl_bool_error;
8641 if (n_div == 0)
8642 return isl_bool_false;
8643 univ = isl_bool_true;
8644 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8645 univ)
8646 return isl_bool_error;
8647 if (univ < 0 || !univ)
8648 return univ;
8649 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8650 univ = isl_basic_map_is_subset(test, bmap);
8651 isl_basic_map_free(test);
8652 return univ;
8655 /* Is "bset" equal to the universe with the same space?
8657 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8659 return isl_basic_map_is_universe(bset);
8662 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8664 int i;
8666 if (!map)
8667 return isl_bool_error;
8669 for (i = 0; i < map->n; ++i) {
8670 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8671 if (r < 0 || r)
8672 return r;
8675 return isl_bool_false;
8678 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8680 return isl_map_plain_is_universe(set_to_map(set));
8683 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8685 struct isl_basic_set *bset = NULL;
8686 struct isl_vec *sample = NULL;
8687 isl_bool empty, non_empty;
8689 if (!bmap)
8690 return isl_bool_error;
8692 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8693 return isl_bool_true;
8695 if (isl_basic_map_plain_is_universe(bmap))
8696 return isl_bool_false;
8698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8699 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8700 copy = isl_basic_map_remove_redundancies(copy);
8701 empty = isl_basic_map_plain_is_empty(copy);
8702 isl_basic_map_free(copy);
8703 return empty;
8706 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8707 if (non_empty < 0)
8708 return isl_bool_error;
8709 if (non_empty)
8710 return isl_bool_false;
8711 isl_vec_free(bmap->sample);
8712 bmap->sample = NULL;
8713 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8714 if (!bset)
8715 return isl_bool_error;
8716 sample = isl_basic_set_sample_vec(bset);
8717 if (!sample)
8718 return isl_bool_error;
8719 empty = sample->size == 0;
8720 isl_vec_free(bmap->sample);
8721 bmap->sample = sample;
8722 if (empty)
8723 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8725 return empty;
8728 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8730 if (!bmap)
8731 return isl_bool_error;
8732 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8735 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8737 if (!bset)
8738 return isl_bool_error;
8739 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8742 /* Is "bmap" known to be non-empty?
8744 * That is, is the cached sample still valid?
8746 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8748 isl_size total;
8750 if (!bmap)
8751 return isl_bool_error;
8752 if (!bmap->sample)
8753 return isl_bool_false;
8754 total = isl_basic_map_dim(bmap, isl_dim_all);
8755 if (total < 0)
8756 return isl_bool_error;
8757 if (bmap->sample->size != 1 + total)
8758 return isl_bool_false;
8759 return isl_basic_map_contains(bmap, bmap->sample);
8762 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8764 return isl_basic_map_is_empty(bset_to_bmap(bset));
8767 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8768 __isl_take isl_basic_map *bmap2)
8770 struct isl_map *map;
8771 if (!bmap1 || !bmap2)
8772 goto error;
8774 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8776 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8777 if (!map)
8778 goto error;
8779 map = isl_map_add_basic_map(map, bmap1);
8780 map = isl_map_add_basic_map(map, bmap2);
8781 return map;
8782 error:
8783 isl_basic_map_free(bmap1);
8784 isl_basic_map_free(bmap2);
8785 return NULL;
8788 struct isl_set *isl_basic_set_union(
8789 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8791 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8792 bset_to_bmap(bset2)));
8795 /* Order divs such that any div only depends on previous divs */
8796 __isl_give isl_basic_map *isl_basic_map_order_divs(
8797 __isl_take isl_basic_map *bmap)
8799 int i;
8800 int off;
8802 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8803 if (off < 0)
8804 return isl_basic_map_free(bmap);
8806 for (i = 0; i < bmap->n_div; ++i) {
8807 int pos;
8808 if (isl_int_is_zero(bmap->div[i][0]))
8809 continue;
8810 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8811 bmap->n_div-i);
8812 if (pos == -1)
8813 continue;
8814 if (pos == 0)
8815 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8816 "integer division depends on itself",
8817 return isl_basic_map_free(bmap));
8818 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8819 if (!bmap)
8820 return NULL;
8821 --i;
8823 return bmap;
8826 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8828 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8831 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8833 int i;
8835 if (!map)
8836 return 0;
8838 for (i = 0; i < map->n; ++i) {
8839 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8840 if (!map->p[i])
8841 goto error;
8844 return map;
8845 error:
8846 isl_map_free(map);
8847 return NULL;
8850 /* Sort the local variables of "bset".
8852 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8853 __isl_take isl_basic_set *bset)
8855 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8858 /* Apply the expansion computed by isl_merge_divs.
8859 * The expansion itself is given by "exp" while the resulting
8860 * list of divs is given by "div".
8862 * Move the integer divisions of "bmap" into the right position
8863 * according to "exp" and then introduce the additional integer
8864 * divisions, adding div constraints.
8865 * The moving should be done first to avoid moving coefficients
8866 * in the definitions of the extra integer divisions.
8868 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8869 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8871 int i, j;
8872 int n_div;
8874 bmap = isl_basic_map_cow(bmap);
8875 if (!bmap || !div)
8876 goto error;
8878 if (div->n_row < bmap->n_div)
8879 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8880 "not an expansion", goto error);
8882 n_div = bmap->n_div;
8883 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8884 div->n_row - n_div, 0,
8885 2 * (div->n_row - n_div));
8887 for (i = n_div; i < div->n_row; ++i)
8888 if (isl_basic_map_alloc_div(bmap) < 0)
8889 goto error;
8891 for (j = n_div - 1; j >= 0; --j) {
8892 if (exp[j] == j)
8893 break;
8894 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8895 if (!bmap)
8896 goto error;
8898 j = 0;
8899 for (i = 0; i < div->n_row; ++i) {
8900 if (j < n_div && exp[j] == i) {
8901 j++;
8902 } else {
8903 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8904 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8905 continue;
8906 bmap = isl_basic_map_add_div_constraints(bmap, i);
8907 if (!bmap)
8908 goto error;
8912 isl_mat_free(div);
8913 return bmap;
8914 error:
8915 isl_basic_map_free(bmap);
8916 isl_mat_free(div);
8917 return NULL;
8920 /* Apply the expansion computed by isl_merge_divs.
8921 * The expansion itself is given by "exp" while the resulting
8922 * list of divs is given by "div".
8924 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8925 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8927 return isl_basic_map_expand_divs(bset, div, exp);
8930 /* Look for a div in dst that corresponds to the div "div" in src.
8931 * The divs before "div" in src and dst are assumed to be the same.
8933 * Return the position of the corresponding div in dst
8934 * if there is one. Otherwise, return a position beyond the integer divisions.
8935 * Return -1 on error.
8937 static int find_div(__isl_keep isl_basic_map *dst,
8938 __isl_keep isl_basic_map *src, unsigned div)
8940 int i;
8941 isl_size n_div;
8942 int v_div;
8944 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8945 n_div = isl_basic_map_dim(dst, isl_dim_div);
8946 if (n_div < 0 || v_div < 0)
8947 return -1;
8948 isl_assert(dst->ctx, div <= n_div, return -1);
8949 for (i = div; i < n_div; ++i)
8950 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8951 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8952 n_div - div) == -1)
8953 return i;
8954 return n_div;
8957 /* Align the divs of "dst" to those of "src", adding divs from "src"
8958 * if needed. That is, make sure that the first src->n_div divs
8959 * of the result are equal to those of src.
8961 * The result is not finalized as by design it will have redundant
8962 * divs if any divs from "src" were copied.
8964 __isl_give isl_basic_map *isl_basic_map_align_divs(
8965 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8967 int i;
8968 isl_bool known;
8969 int extended;
8970 int v_div;
8971 isl_size dst_n_div;
8973 if (!dst || !src)
8974 return isl_basic_map_free(dst);
8976 if (src->n_div == 0)
8977 return dst;
8979 known = isl_basic_map_divs_known(src);
8980 if (known < 0)
8981 return isl_basic_map_free(dst);
8982 if (!known)
8983 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8984 "some src divs are unknown",
8985 return isl_basic_map_free(dst));
8987 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8988 if (v_div < 0)
8989 return isl_basic_map_free(dst);
8991 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8992 if (!src)
8993 return isl_basic_map_free(dst);
8995 extended = 0;
8996 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8997 if (dst_n_div < 0)
8998 dst = isl_basic_map_free(dst);
8999 for (i = 0; i < src->n_div; ++i) {
9000 int j = find_div(dst, src, i);
9001 if (j < 0)
9002 dst = isl_basic_map_free(dst);
9003 if (j == dst_n_div) {
9004 if (!extended) {
9005 int extra = src->n_div - i;
9006 dst = isl_basic_map_cow(dst);
9007 if (!dst)
9008 goto error;
9009 dst = isl_basic_map_extend_space(dst,
9010 isl_space_copy(dst->dim),
9011 extra, 0, 2 * extra);
9012 extended = 1;
9014 j = isl_basic_map_alloc_div(dst);
9015 if (j < 0)
9016 goto error;
9017 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9018 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9019 dst_n_div++;
9020 dst = isl_basic_map_add_div_constraints(dst, j);
9021 if (!dst)
9022 goto error;
9024 if (j != i)
9025 dst = isl_basic_map_swap_div(dst, i, j);
9026 if (!dst)
9027 goto error;
9029 isl_basic_map_free(src);
9030 return dst;
9031 error:
9032 isl_basic_map_free(src);
9033 isl_basic_map_free(dst);
9034 return NULL;
9037 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9039 int i;
9041 if (!map)
9042 return NULL;
9043 if (map->n == 0)
9044 return map;
9045 map = isl_map_compute_divs(map);
9046 map = isl_map_cow(map);
9047 if (!map)
9048 return NULL;
9050 for (i = 1; i < map->n; ++i)
9051 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9052 for (i = 1; i < map->n; ++i) {
9053 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9054 if (!map->p[i])
9055 return isl_map_free(map);
9058 map = isl_map_unmark_normalized(map);
9059 return map;
9062 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9064 return isl_map_align_divs_internal(map);
9067 struct isl_set *isl_set_align_divs(struct isl_set *set)
9069 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9072 /* Align the divs of the basic maps in "map" to those
9073 * of the basic maps in "list", as well as to the other basic maps in "map".
9074 * The elements in "list" are assumed to have known divs.
9076 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9077 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9079 int i, n;
9081 map = isl_map_compute_divs(map);
9082 map = isl_map_cow(map);
9083 if (!map || !list)
9084 return isl_map_free(map);
9085 if (map->n == 0)
9086 return map;
9088 n = isl_basic_map_list_n_basic_map(list);
9089 for (i = 0; i < n; ++i) {
9090 isl_basic_map *bmap;
9092 bmap = isl_basic_map_list_get_basic_map(list, i);
9093 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9094 isl_basic_map_free(bmap);
9096 if (!map->p[0])
9097 return isl_map_free(map);
9099 return isl_map_align_divs_internal(map);
9102 /* Align the divs of each element of "list" to those of "bmap".
9103 * Both "bmap" and the elements of "list" are assumed to have known divs.
9105 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9106 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9108 int i, n;
9110 if (!list || !bmap)
9111 return isl_basic_map_list_free(list);
9113 n = isl_basic_map_list_n_basic_map(list);
9114 for (i = 0; i < n; ++i) {
9115 isl_basic_map *bmap_i;
9117 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9118 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9119 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9122 return list;
9125 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9126 __isl_take isl_map *map)
9128 isl_bool ok;
9130 ok = isl_map_compatible_domain(map, set);
9131 if (ok < 0)
9132 goto error;
9133 if (!ok)
9134 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9135 "incompatible spaces", goto error);
9136 map = isl_map_intersect_domain(map, set);
9137 set = isl_map_range(map);
9138 return set;
9139 error:
9140 isl_set_free(set);
9141 isl_map_free(map);
9142 return NULL;
9145 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9146 __isl_take isl_map *map)
9148 return isl_map_align_params_map_map_and(set, map, &set_apply);
9151 /* There is no need to cow as removing empty parts doesn't change
9152 * the meaning of the set.
9154 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9156 int i;
9158 if (!map)
9159 return NULL;
9161 for (i = map->n - 1; i >= 0; --i)
9162 map = remove_if_empty(map, i);
9164 return map;
9167 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9169 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9172 /* Create a binary relation that maps the shared initial "pos" dimensions
9173 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9175 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9176 __isl_keep isl_basic_set *bset2, int pos)
9178 isl_basic_map *bmap1;
9179 isl_basic_map *bmap2;
9181 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9182 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9183 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9184 isl_dim_out, 0, pos);
9185 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9186 isl_dim_out, 0, pos);
9187 return isl_basic_map_range_product(bmap1, bmap2);
9190 /* Given two basic sets bset1 and bset2, compute the maximal difference
9191 * between the values of dimension pos in bset1 and those in bset2
9192 * for any common value of the parameters and dimensions preceding pos.
9194 static enum isl_lp_result basic_set_maximal_difference_at(
9195 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9196 int pos, isl_int *opt)
9198 isl_basic_map *bmap1;
9199 struct isl_ctx *ctx;
9200 struct isl_vec *obj;
9201 isl_size total;
9202 isl_size nparam;
9203 isl_size dim1;
9204 enum isl_lp_result res;
9206 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9207 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9208 if (nparam < 0 || dim1 < 0 || !bset2)
9209 return isl_lp_error;
9211 bmap1 = join_initial(bset1, bset2, pos);
9212 total = isl_basic_map_dim(bmap1, isl_dim_all);
9213 if (total < 0)
9214 return isl_lp_error;
9216 ctx = bmap1->ctx;
9217 obj = isl_vec_alloc(ctx, 1 + total);
9218 if (!obj)
9219 goto error;
9220 isl_seq_clr(obj->block.data, 1 + total);
9221 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9222 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9223 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9224 opt, NULL, NULL);
9225 isl_basic_map_free(bmap1);
9226 isl_vec_free(obj);
9227 return res;
9228 error:
9229 isl_basic_map_free(bmap1);
9230 return isl_lp_error;
9233 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9234 * for any common value of the parameters and dimensions preceding pos
9235 * in both basic sets, the values of dimension pos in bset1 are
9236 * smaller or larger than those in bset2.
9238 * Returns
9239 * 1 if bset1 follows bset2
9240 * -1 if bset1 precedes bset2
9241 * 0 if bset1 and bset2 are incomparable
9242 * -2 if some error occurred.
9244 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9245 __isl_keep isl_basic_set *bset2, int pos)
9247 isl_int opt;
9248 enum isl_lp_result res;
9249 int cmp;
9251 isl_int_init(opt);
9253 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9255 if (res == isl_lp_empty)
9256 cmp = 0;
9257 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9258 res == isl_lp_unbounded)
9259 cmp = 1;
9260 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9261 cmp = -1;
9262 else
9263 cmp = -2;
9265 isl_int_clear(opt);
9266 return cmp;
9269 /* Given two basic sets bset1 and bset2, check whether
9270 * for any common value of the parameters and dimensions preceding pos
9271 * there is a value of dimension pos in bset1 that is larger
9272 * than a value of the same dimension in bset2.
9274 * Return
9275 * 1 if there exists such a pair
9276 * 0 if there is no such pair, but there is a pair of equal values
9277 * -1 otherwise
9278 * -2 if some error occurred.
9280 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9281 __isl_keep isl_basic_set *bset2, int pos)
9283 isl_bool empty;
9284 isl_basic_map *bmap;
9285 isl_size dim1;
9287 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9288 if (dim1 < 0)
9289 return -2;
9290 bmap = join_initial(bset1, bset2, pos);
9291 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9292 isl_dim_out, dim1 - pos);
9293 empty = isl_basic_map_is_empty(bmap);
9294 if (empty < 0)
9295 goto error;
9296 if (empty) {
9297 isl_basic_map_free(bmap);
9298 return -1;
9300 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9301 isl_dim_out, dim1 - pos);
9302 empty = isl_basic_map_is_empty(bmap);
9303 if (empty < 0)
9304 goto error;
9305 isl_basic_map_free(bmap);
9306 if (empty)
9307 return 0;
9308 return 1;
9309 error:
9310 isl_basic_map_free(bmap);
9311 return -2;
9314 /* Given two sets set1 and set2, check whether
9315 * for any common value of the parameters and dimensions preceding pos
9316 * there is a value of dimension pos in set1 that is larger
9317 * than a value of the same dimension in set2.
9319 * Return
9320 * 1 if there exists such a pair
9321 * 0 if there is no such pair, but there is a pair of equal values
9322 * -1 otherwise
9323 * -2 if some error occurred.
9325 int isl_set_follows_at(__isl_keep isl_set *set1,
9326 __isl_keep isl_set *set2, int pos)
9328 int i, j;
9329 int follows = -1;
9331 if (!set1 || !set2)
9332 return -2;
9334 for (i = 0; i < set1->n; ++i)
9335 for (j = 0; j < set2->n; ++j) {
9336 int f;
9337 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9338 if (f == 1 || f == -2)
9339 return f;
9340 if (f > follows)
9341 follows = f;
9344 return follows;
9347 static isl_bool isl_basic_map_plain_has_fixed_var(
9348 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9350 int i;
9351 int d;
9352 isl_size total;
9354 total = isl_basic_map_dim(bmap, isl_dim_all);
9355 if (total < 0)
9356 return isl_bool_error;
9357 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9358 for (; d+1 > pos; --d)
9359 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9360 break;
9361 if (d != pos)
9362 continue;
9363 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9364 return isl_bool_false;
9365 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9366 return isl_bool_false;
9367 if (!isl_int_is_one(bmap->eq[i][1+d]))
9368 return isl_bool_false;
9369 if (val)
9370 isl_int_neg(*val, bmap->eq[i][0]);
9371 return isl_bool_true;
9373 return isl_bool_false;
9376 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9377 unsigned pos, isl_int *val)
9379 int i;
9380 isl_int v;
9381 isl_int tmp;
9382 isl_bool fixed;
9384 if (!map)
9385 return isl_bool_error;
9386 if (map->n == 0)
9387 return isl_bool_false;
9388 if (map->n == 1)
9389 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9390 isl_int_init(v);
9391 isl_int_init(tmp);
9392 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9393 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9394 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9395 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9396 fixed = isl_bool_false;
9398 if (val)
9399 isl_int_set(*val, v);
9400 isl_int_clear(tmp);
9401 isl_int_clear(v);
9402 return fixed;
9405 static isl_bool isl_basic_set_plain_has_fixed_var(
9406 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9408 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9409 pos, val);
9412 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9413 enum isl_dim_type type, unsigned pos, isl_int *val)
9415 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9416 return isl_bool_error;
9417 return isl_basic_map_plain_has_fixed_var(bmap,
9418 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9421 /* If "bmap" obviously lies on a hyperplane where the given dimension
9422 * has a fixed value, then return that value.
9423 * Otherwise return NaN.
9425 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9426 __isl_keep isl_basic_map *bmap,
9427 enum isl_dim_type type, unsigned pos)
9429 isl_ctx *ctx;
9430 isl_val *v;
9431 isl_bool fixed;
9433 if (!bmap)
9434 return NULL;
9435 ctx = isl_basic_map_get_ctx(bmap);
9436 v = isl_val_alloc(ctx);
9437 if (!v)
9438 return NULL;
9439 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9440 if (fixed < 0)
9441 return isl_val_free(v);
9442 if (fixed) {
9443 isl_int_set_si(v->d, 1);
9444 return v;
9446 isl_val_free(v);
9447 return isl_val_nan(ctx);
9450 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9451 enum isl_dim_type type, unsigned pos, isl_int *val)
9453 if (isl_map_check_range(map, type, pos, 1) < 0)
9454 return isl_bool_error;
9455 return isl_map_plain_has_fixed_var(map,
9456 map_offset(map, type) - 1 + pos, val);
9459 /* If "map" obviously lies on a hyperplane where the given dimension
9460 * has a fixed value, then return that value.
9461 * Otherwise return NaN.
9463 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9464 enum isl_dim_type type, unsigned pos)
9466 isl_ctx *ctx;
9467 isl_val *v;
9468 isl_bool fixed;
9470 if (!map)
9471 return NULL;
9472 ctx = isl_map_get_ctx(map);
9473 v = isl_val_alloc(ctx);
9474 if (!v)
9475 return NULL;
9476 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9477 if (fixed < 0)
9478 return isl_val_free(v);
9479 if (fixed) {
9480 isl_int_set_si(v->d, 1);
9481 return v;
9483 isl_val_free(v);
9484 return isl_val_nan(ctx);
9487 /* If "set" obviously lies on a hyperplane where the given dimension
9488 * has a fixed value, then return that value.
9489 * Otherwise return NaN.
9491 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9492 enum isl_dim_type type, unsigned pos)
9494 return isl_map_plain_get_val_if_fixed(set, type, pos);
9497 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9498 * then return this fixed value in *val.
9500 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9501 unsigned dim, isl_int *val)
9503 isl_size nparam;
9505 nparam = isl_basic_set_dim(bset, isl_dim_param);
9506 if (nparam < 0)
9507 return isl_bool_error;
9508 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
9511 /* Return -1 if the constraint "c1" should be sorted before "c2"
9512 * and 1 if it should be sorted after "c2".
9513 * Return 0 if the two constraints are the same (up to the constant term).
9515 * In particular, if a constraint involves later variables than another
9516 * then it is sorted after this other constraint.
9517 * uset_gist depends on constraints without existentially quantified
9518 * variables sorting first.
9520 * For constraints that have the same latest variable, those
9521 * with the same coefficient for this latest variable (first in absolute value
9522 * and then in actual value) are grouped together.
9523 * This is useful for detecting pairs of constraints that can
9524 * be chained in their printed representation.
9526 * Finally, within a group, constraints are sorted according to
9527 * their coefficients (excluding the constant term).
9529 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9531 isl_int **c1 = (isl_int **) p1;
9532 isl_int **c2 = (isl_int **) p2;
9533 int l1, l2;
9534 unsigned size = *(unsigned *) arg;
9535 int cmp;
9537 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9538 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9540 if (l1 != l2)
9541 return l1 - l2;
9543 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9544 if (cmp != 0)
9545 return cmp;
9546 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9547 if (cmp != 0)
9548 return -cmp;
9550 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9553 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9554 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9555 * and 0 if the two constraints are the same (up to the constant term).
9557 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9558 isl_int *c1, isl_int *c2)
9560 isl_size total;
9561 unsigned size;
9563 total = isl_basic_map_dim(bmap, isl_dim_all);
9564 if (total < 0)
9565 return -2;
9566 size = total;
9567 return sort_constraint_cmp(&c1, &c2, &size);
9570 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9571 __isl_take isl_basic_map *bmap)
9573 isl_size total;
9574 unsigned size;
9576 if (!bmap)
9577 return NULL;
9578 if (bmap->n_ineq == 0)
9579 return bmap;
9580 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9581 return bmap;
9582 total = isl_basic_map_dim(bmap, isl_dim_all);
9583 if (total < 0)
9584 return isl_basic_map_free(bmap);
9585 size = total;
9586 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9587 &sort_constraint_cmp, &size) < 0)
9588 return isl_basic_map_free(bmap);
9589 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9590 return bmap;
9593 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9594 __isl_take isl_basic_set *bset)
9596 isl_basic_map *bmap = bset_to_bmap(bset);
9597 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9600 __isl_give isl_basic_map *isl_basic_map_normalize(
9601 __isl_take isl_basic_map *bmap)
9603 bmap = isl_basic_map_remove_redundancies(bmap);
9604 bmap = isl_basic_map_sort_constraints(bmap);
9605 return bmap;
9607 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9608 __isl_keep isl_basic_map *bmap2)
9610 int i, cmp;
9611 isl_size total;
9612 isl_space *space1, *space2;
9614 if (!bmap1 || !bmap2)
9615 return -1;
9617 if (bmap1 == bmap2)
9618 return 0;
9619 space1 = isl_basic_map_peek_space(bmap1);
9620 space2 = isl_basic_map_peek_space(bmap2);
9621 cmp = isl_space_cmp(space1, space2);
9622 if (cmp)
9623 return cmp;
9624 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9625 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9626 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9627 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9628 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9629 return 0;
9630 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9631 return 1;
9632 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9633 return -1;
9634 if (bmap1->n_eq != bmap2->n_eq)
9635 return bmap1->n_eq - bmap2->n_eq;
9636 if (bmap1->n_ineq != bmap2->n_ineq)
9637 return bmap1->n_ineq - bmap2->n_ineq;
9638 if (bmap1->n_div != bmap2->n_div)
9639 return bmap1->n_div - bmap2->n_div;
9640 total = isl_basic_map_dim(bmap1, isl_dim_all);
9641 if (total < 0)
9642 return -1;
9643 for (i = 0; i < bmap1->n_eq; ++i) {
9644 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9645 if (cmp)
9646 return cmp;
9648 for (i = 0; i < bmap1->n_ineq; ++i) {
9649 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9650 if (cmp)
9651 return cmp;
9653 for (i = 0; i < bmap1->n_div; ++i) {
9654 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9655 if (cmp)
9656 return cmp;
9658 return 0;
9661 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9662 __isl_keep isl_basic_set *bset2)
9664 return isl_basic_map_plain_cmp(bset1, bset2);
9667 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9669 int i, cmp;
9671 if (set1 == set2)
9672 return 0;
9673 if (set1->n != set2->n)
9674 return set1->n - set2->n;
9676 for (i = 0; i < set1->n; ++i) {
9677 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9678 if (cmp)
9679 return cmp;
9682 return 0;
9685 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9686 __isl_keep isl_basic_map *bmap2)
9688 if (!bmap1 || !bmap2)
9689 return isl_bool_error;
9690 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9693 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9694 __isl_keep isl_basic_set *bset2)
9696 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9697 bset_to_bmap(bset2));
9700 static int qsort_bmap_cmp(const void *p1, const void *p2)
9702 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9703 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9705 return isl_basic_map_plain_cmp(bmap1, bmap2);
9708 /* Sort the basic maps of "map" and remove duplicate basic maps.
9710 * While removing basic maps, we make sure that the basic maps remain
9711 * sorted because isl_map_normalize expects the basic maps of the result
9712 * to be sorted.
9714 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9716 int i, j;
9718 map = isl_map_remove_empty_parts(map);
9719 if (!map)
9720 return NULL;
9721 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9722 for (i = map->n - 1; i >= 1; --i) {
9723 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9724 continue;
9725 isl_basic_map_free(map->p[i-1]);
9726 for (j = i; j < map->n; ++j)
9727 map->p[j - 1] = map->p[j];
9728 map->n--;
9731 return map;
9734 /* Remove obvious duplicates among the basic maps of "map".
9736 * Unlike isl_map_normalize, this function does not remove redundant
9737 * constraints and only removes duplicates that have exactly the same
9738 * constraints in the input. It does sort the constraints and
9739 * the basic maps to ease the detection of duplicates.
9741 * If "map" has already been normalized or if the basic maps are
9742 * disjoint, then there can be no duplicates.
9744 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9746 int i;
9747 isl_basic_map *bmap;
9749 if (!map)
9750 return NULL;
9751 if (map->n <= 1)
9752 return map;
9753 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9754 return map;
9755 for (i = 0; i < map->n; ++i) {
9756 bmap = isl_basic_map_copy(map->p[i]);
9757 bmap = isl_basic_map_sort_constraints(bmap);
9758 if (!bmap)
9759 return isl_map_free(map);
9760 isl_basic_map_free(map->p[i]);
9761 map->p[i] = bmap;
9764 map = sort_and_remove_duplicates(map);
9765 return map;
9768 /* We normalize in place, but if anything goes wrong we need
9769 * to return NULL, so we need to make sure we don't change the
9770 * meaning of any possible other copies of map.
9772 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9774 int i;
9775 struct isl_basic_map *bmap;
9777 if (!map)
9778 return NULL;
9779 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9780 return map;
9781 for (i = 0; i < map->n; ++i) {
9782 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9783 if (!bmap)
9784 goto error;
9785 isl_basic_map_free(map->p[i]);
9786 map->p[i] = bmap;
9789 map = sort_and_remove_duplicates(map);
9790 if (map)
9791 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9792 return map;
9793 error:
9794 isl_map_free(map);
9795 return NULL;
9798 struct isl_set *isl_set_normalize(struct isl_set *set)
9800 return set_from_map(isl_map_normalize(set_to_map(set)));
9803 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9804 __isl_keep isl_map *map2)
9806 int i;
9807 isl_bool equal;
9809 if (!map1 || !map2)
9810 return isl_bool_error;
9812 if (map1 == map2)
9813 return isl_bool_true;
9814 if (!isl_space_is_equal(map1->dim, map2->dim))
9815 return isl_bool_false;
9817 map1 = isl_map_copy(map1);
9818 map2 = isl_map_copy(map2);
9819 map1 = isl_map_normalize(map1);
9820 map2 = isl_map_normalize(map2);
9821 if (!map1 || !map2)
9822 goto error;
9823 equal = map1->n == map2->n;
9824 for (i = 0; equal && i < map1->n; ++i) {
9825 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9826 if (equal < 0)
9827 goto error;
9829 isl_map_free(map1);
9830 isl_map_free(map2);
9831 return equal;
9832 error:
9833 isl_map_free(map1);
9834 isl_map_free(map2);
9835 return isl_bool_error;
9838 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9839 __isl_keep isl_set *set2)
9841 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9844 /* Return the basic maps in "map" as a list.
9846 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9847 __isl_keep isl_map *map)
9849 int i;
9850 isl_ctx *ctx;
9851 isl_basic_map_list *list;
9853 if (!map)
9854 return NULL;
9855 ctx = isl_map_get_ctx(map);
9856 list = isl_basic_map_list_alloc(ctx, map->n);
9858 for (i = 0; i < map->n; ++i) {
9859 isl_basic_map *bmap;
9861 bmap = isl_basic_map_copy(map->p[i]);
9862 list = isl_basic_map_list_add(list, bmap);
9865 return list;
9868 /* Return the intersection of the elements in the non-empty list "list".
9869 * All elements are assumed to live in the same space.
9871 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9872 __isl_take isl_basic_map_list *list)
9874 int i, n;
9875 isl_basic_map *bmap;
9877 if (!list)
9878 return NULL;
9879 n = isl_basic_map_list_n_basic_map(list);
9880 if (n < 1)
9881 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9882 "expecting non-empty list", goto error);
9884 bmap = isl_basic_map_list_get_basic_map(list, 0);
9885 for (i = 1; i < n; ++i) {
9886 isl_basic_map *bmap_i;
9888 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9889 bmap = isl_basic_map_intersect(bmap, bmap_i);
9892 isl_basic_map_list_free(list);
9893 return bmap;
9894 error:
9895 isl_basic_map_list_free(list);
9896 return NULL;
9899 /* Return the intersection of the elements in the non-empty list "list".
9900 * All elements are assumed to live in the same space.
9902 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9903 __isl_take isl_basic_set_list *list)
9905 return isl_basic_map_list_intersect(list);
9908 /* Return the union of the elements of "list".
9909 * The list is required to have at least one element.
9911 __isl_give isl_set *isl_basic_set_list_union(
9912 __isl_take isl_basic_set_list *list)
9914 int i, n;
9915 isl_space *space;
9916 isl_basic_set *bset;
9917 isl_set *set;
9919 if (!list)
9920 return NULL;
9921 n = isl_basic_set_list_n_basic_set(list);
9922 if (n < 1)
9923 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9924 "expecting non-empty list", goto error);
9926 bset = isl_basic_set_list_get_basic_set(list, 0);
9927 space = isl_basic_set_get_space(bset);
9928 isl_basic_set_free(bset);
9930 set = isl_set_alloc_space(space, n, 0);
9931 for (i = 0; i < n; ++i) {
9932 bset = isl_basic_set_list_get_basic_set(list, i);
9933 set = isl_set_add_basic_set(set, bset);
9936 isl_basic_set_list_free(list);
9937 return set;
9938 error:
9939 isl_basic_set_list_free(list);
9940 return NULL;
9943 /* Return the union of the elements in the non-empty list "list".
9944 * All elements are assumed to live in the same space.
9946 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9948 int i, n;
9949 isl_set *set;
9951 if (!list)
9952 return NULL;
9953 n = isl_set_list_n_set(list);
9954 if (n < 1)
9955 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9956 "expecting non-empty list", goto error);
9958 set = isl_set_list_get_set(list, 0);
9959 for (i = 1; i < n; ++i) {
9960 isl_set *set_i;
9962 set_i = isl_set_list_get_set(list, i);
9963 set = isl_set_union(set, set_i);
9966 isl_set_list_free(list);
9967 return set;
9968 error:
9969 isl_set_list_free(list);
9970 return NULL;
9973 __isl_give isl_basic_map *isl_basic_map_product(
9974 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9976 isl_space *space_result = NULL;
9977 struct isl_basic_map *bmap;
9978 unsigned in1, in2, out1, out2, nparam, total, pos;
9979 struct isl_dim_map *dim_map1, *dim_map2;
9981 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9982 goto error;
9983 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9984 isl_space_copy(bmap2->dim));
9986 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9987 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9988 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9989 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9990 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9992 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9993 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9994 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9995 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9996 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9997 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9998 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9999 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10000 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10001 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10002 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10004 bmap = isl_basic_map_alloc_space(space_result,
10005 bmap1->n_div + bmap2->n_div,
10006 bmap1->n_eq + bmap2->n_eq,
10007 bmap1->n_ineq + bmap2->n_ineq);
10008 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10009 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10010 bmap = isl_basic_map_simplify(bmap);
10011 return isl_basic_map_finalize(bmap);
10012 error:
10013 isl_basic_map_free(bmap1);
10014 isl_basic_map_free(bmap2);
10015 return NULL;
10018 __isl_give isl_basic_map *isl_basic_map_flat_product(
10019 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10021 isl_basic_map *prod;
10023 prod = isl_basic_map_product(bmap1, bmap2);
10024 prod = isl_basic_map_flatten(prod);
10025 return prod;
10028 __isl_give isl_basic_set *isl_basic_set_flat_product(
10029 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10031 return isl_basic_map_flat_range_product(bset1, bset2);
10034 __isl_give isl_basic_map *isl_basic_map_domain_product(
10035 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10037 isl_space *space1, *space2;
10038 isl_space *space_result = NULL;
10039 isl_basic_map *bmap;
10040 isl_size in1, in2, out, nparam;
10041 unsigned total, pos;
10042 struct isl_dim_map *dim_map1, *dim_map2;
10044 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10045 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10046 out = isl_basic_map_dim(bmap1, isl_dim_out);
10047 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10048 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10049 goto error;
10051 space1 = isl_basic_map_get_space(bmap1);
10052 space2 = isl_basic_map_get_space(bmap2);
10053 space_result = isl_space_domain_product(space1, space2);
10055 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10056 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10057 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10058 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10059 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10060 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10061 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10062 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10063 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10064 isl_dim_map_div(dim_map1, bmap1, pos += out);
10065 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10067 bmap = isl_basic_map_alloc_space(space_result,
10068 bmap1->n_div + bmap2->n_div,
10069 bmap1->n_eq + bmap2->n_eq,
10070 bmap1->n_ineq + bmap2->n_ineq);
10071 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10072 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10073 bmap = isl_basic_map_simplify(bmap);
10074 return isl_basic_map_finalize(bmap);
10075 error:
10076 isl_basic_map_free(bmap1);
10077 isl_basic_map_free(bmap2);
10078 return NULL;
10081 __isl_give isl_basic_map *isl_basic_map_range_product(
10082 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10084 isl_bool rational;
10085 isl_space *space_result = NULL;
10086 isl_basic_map *bmap;
10087 isl_size in, out1, out2, nparam;
10088 unsigned total, pos;
10089 struct isl_dim_map *dim_map1, *dim_map2;
10091 rational = isl_basic_map_is_rational(bmap1);
10092 if (rational >= 0 && rational)
10093 rational = isl_basic_map_is_rational(bmap2);
10094 in = isl_basic_map_dim(bmap1, isl_dim_in);
10095 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10096 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10097 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10098 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10099 goto error;
10101 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10102 goto error;
10104 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10105 isl_space_copy(bmap2->dim));
10107 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10108 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10109 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10110 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10111 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10112 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10113 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10114 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10115 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10116 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10117 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10119 bmap = isl_basic_map_alloc_space(space_result,
10120 bmap1->n_div + bmap2->n_div,
10121 bmap1->n_eq + bmap2->n_eq,
10122 bmap1->n_ineq + bmap2->n_ineq);
10123 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10124 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10125 if (rational)
10126 bmap = isl_basic_map_set_rational(bmap);
10127 bmap = isl_basic_map_simplify(bmap);
10128 return isl_basic_map_finalize(bmap);
10129 error:
10130 isl_basic_map_free(bmap1);
10131 isl_basic_map_free(bmap2);
10132 return NULL;
10135 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10136 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10138 isl_basic_map *prod;
10140 prod = isl_basic_map_range_product(bmap1, bmap2);
10141 prod = isl_basic_map_flatten_range(prod);
10142 return prod;
10145 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10146 * and collect the results.
10147 * The result live in the space obtained by calling "space_product"
10148 * on the spaces of "map1" and "map2".
10149 * If "remove_duplicates" is set then the result may contain duplicates
10150 * (even if the inputs do not) and so we try and remove the obvious
10151 * duplicates.
10153 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10154 __isl_take isl_map *map2,
10155 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10156 __isl_take isl_space *right),
10157 __isl_give isl_basic_map *(*basic_map_product)(
10158 __isl_take isl_basic_map *left,
10159 __isl_take isl_basic_map *right),
10160 int remove_duplicates)
10162 unsigned flags = 0;
10163 struct isl_map *result;
10164 int i, j;
10165 isl_bool m;
10167 m = isl_map_has_equal_params(map1, map2);
10168 if (m < 0)
10169 goto error;
10170 if (!m)
10171 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10172 "parameters don't match", goto error);
10174 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10175 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10176 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10178 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10179 isl_space_copy(map2->dim)),
10180 map1->n * map2->n, flags);
10181 if (!result)
10182 goto error;
10183 for (i = 0; i < map1->n; ++i)
10184 for (j = 0; j < map2->n; ++j) {
10185 struct isl_basic_map *part;
10186 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10187 isl_basic_map_copy(map2->p[j]));
10188 if (isl_basic_map_is_empty(part))
10189 isl_basic_map_free(part);
10190 else
10191 result = isl_map_add_basic_map(result, part);
10192 if (!result)
10193 goto error;
10195 if (remove_duplicates)
10196 result = isl_map_remove_obvious_duplicates(result);
10197 isl_map_free(map1);
10198 isl_map_free(map2);
10199 return result;
10200 error:
10201 isl_map_free(map1);
10202 isl_map_free(map2);
10203 return NULL;
10206 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10208 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10209 __isl_take isl_map *map2)
10211 return map_product(map1, map2, &isl_space_product,
10212 &isl_basic_map_product, 0);
10215 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10216 __isl_take isl_map *map2)
10218 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10221 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10223 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10224 __isl_take isl_map *map2)
10226 isl_map *prod;
10228 prod = isl_map_product(map1, map2);
10229 prod = isl_map_flatten(prod);
10230 return prod;
10233 /* Given two set A and B, construct its Cartesian product A x B.
10235 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10237 return isl_map_range_product(set1, set2);
10240 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10241 __isl_take isl_set *set2)
10243 return isl_map_flat_range_product(set1, set2);
10246 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10248 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10249 __isl_take isl_map *map2)
10251 return map_product(map1, map2, &isl_space_domain_product,
10252 &isl_basic_map_domain_product, 1);
10255 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10257 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10258 __isl_take isl_map *map2)
10260 return map_product(map1, map2, &isl_space_range_product,
10261 &isl_basic_map_range_product, 1);
10264 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10265 __isl_take isl_map *map2)
10267 return isl_map_align_params_map_map_and(map1, map2,
10268 &map_domain_product_aligned);
10271 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10272 __isl_take isl_map *map2)
10274 return isl_map_align_params_map_map_and(map1, map2,
10275 &map_range_product_aligned);
10278 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10280 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10282 isl_space *space;
10283 isl_size total1, keep1, total2, keep2;
10285 total1 = isl_map_dim(map, isl_dim_in);
10286 total2 = isl_map_dim(map, isl_dim_out);
10287 if (total1 < 0 || total2 < 0)
10288 return isl_map_free(map);
10289 if (!isl_space_domain_is_wrapping(map->dim) ||
10290 !isl_space_range_is_wrapping(map->dim))
10291 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10292 "not a product", return isl_map_free(map));
10294 space = isl_map_get_space(map);
10295 space = isl_space_factor_domain(space);
10296 keep1 = isl_space_dim(space, isl_dim_in);
10297 keep2 = isl_space_dim(space, isl_dim_out);
10298 if (keep1 < 0 || keep2 < 0)
10299 map = isl_map_free(map);
10300 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10301 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10302 map = isl_map_reset_space(map, space);
10304 return map;
10307 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10309 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10311 isl_space *space;
10312 isl_size total1, keep1, total2, keep2;
10314 total1 = isl_map_dim(map, isl_dim_in);
10315 total2 = isl_map_dim(map, isl_dim_out);
10316 if (total1 < 0 || total2 < 0)
10317 return isl_map_free(map);
10318 if (!isl_space_domain_is_wrapping(map->dim) ||
10319 !isl_space_range_is_wrapping(map->dim))
10320 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10321 "not a product", return isl_map_free(map));
10323 space = isl_map_get_space(map);
10324 space = isl_space_factor_range(space);
10325 keep1 = isl_space_dim(space, isl_dim_in);
10326 keep2 = isl_space_dim(space, isl_dim_out);
10327 if (keep1 < 0 || keep2 < 0)
10328 map = isl_map_free(map);
10329 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10330 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10331 map = isl_map_reset_space(map, space);
10333 return map;
10336 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10338 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10340 isl_space *space;
10341 isl_size total, keep;
10343 total = isl_map_dim(map, isl_dim_in);
10344 if (total < 0)
10345 return isl_map_free(map);
10346 if (!isl_space_domain_is_wrapping(map->dim))
10347 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10348 "domain is not a product", return isl_map_free(map));
10350 space = isl_map_get_space(map);
10351 space = isl_space_domain_factor_domain(space);
10352 keep = isl_space_dim(space, isl_dim_in);
10353 if (keep < 0)
10354 map = isl_map_free(map);
10355 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10356 map = isl_map_reset_space(map, space);
10358 return map;
10361 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10363 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10365 isl_space *space;
10366 isl_size total, keep;
10368 total = isl_map_dim(map, isl_dim_in);
10369 if (total < 0)
10370 return isl_map_free(map);
10371 if (!isl_space_domain_is_wrapping(map->dim))
10372 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10373 "domain is not a product", return isl_map_free(map));
10375 space = isl_map_get_space(map);
10376 space = isl_space_domain_factor_range(space);
10377 keep = isl_space_dim(space, isl_dim_in);
10378 if (keep < 0)
10379 map = isl_map_free(map);
10380 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10381 map = isl_map_reset_space(map, space);
10383 return map;
10386 /* Given a map A -> [B -> C], extract the map A -> B.
10388 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10390 isl_space *space;
10391 isl_size total, keep;
10393 total = isl_map_dim(map, isl_dim_out);
10394 if (total < 0)
10395 return isl_map_free(map);
10396 if (!isl_space_range_is_wrapping(map->dim))
10397 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10398 "range is not a product", return isl_map_free(map));
10400 space = isl_map_get_space(map);
10401 space = isl_space_range_factor_domain(space);
10402 keep = isl_space_dim(space, isl_dim_out);
10403 if (keep < 0)
10404 map = isl_map_free(map);
10405 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10406 map = isl_map_reset_space(map, space);
10408 return map;
10411 /* Given a map A -> [B -> C], extract the map A -> C.
10413 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10415 isl_space *space;
10416 isl_size total, keep;
10418 total = isl_map_dim(map, isl_dim_out);
10419 if (total < 0)
10420 return isl_map_free(map);
10421 if (!isl_space_range_is_wrapping(map->dim))
10422 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10423 "range is not a product", return isl_map_free(map));
10425 space = isl_map_get_space(map);
10426 space = isl_space_range_factor_range(space);
10427 keep = isl_space_dim(space, isl_dim_out);
10428 if (keep < 0)
10429 map = isl_map_free(map);
10430 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10431 map = isl_map_reset_space(map, space);
10433 return map;
10436 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10438 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10439 __isl_take isl_map *map2)
10441 isl_map *prod;
10443 prod = isl_map_domain_product(map1, map2);
10444 prod = isl_map_flatten_domain(prod);
10445 return prod;
10448 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10450 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10451 __isl_take isl_map *map2)
10453 isl_map *prod;
10455 prod = isl_map_range_product(map1, map2);
10456 prod = isl_map_flatten_range(prod);
10457 return prod;
10460 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10462 int i;
10463 uint32_t hash = isl_hash_init();
10464 isl_size total;
10466 if (!bmap)
10467 return 0;
10468 bmap = isl_basic_map_copy(bmap);
10469 bmap = isl_basic_map_normalize(bmap);
10470 total = isl_basic_map_dim(bmap, isl_dim_all);
10471 if (total < 0)
10472 return 0;
10473 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10474 for (i = 0; i < bmap->n_eq; ++i) {
10475 uint32_t c_hash;
10476 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10477 isl_hash_hash(hash, c_hash);
10479 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10480 for (i = 0; i < bmap->n_ineq; ++i) {
10481 uint32_t c_hash;
10482 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10483 isl_hash_hash(hash, c_hash);
10485 isl_hash_byte(hash, bmap->n_div & 0xFF);
10486 for (i = 0; i < bmap->n_div; ++i) {
10487 uint32_t c_hash;
10488 if (isl_int_is_zero(bmap->div[i][0]))
10489 continue;
10490 isl_hash_byte(hash, i & 0xFF);
10491 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10492 isl_hash_hash(hash, c_hash);
10494 isl_basic_map_free(bmap);
10495 return hash;
10498 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10500 return isl_basic_map_get_hash(bset_to_bmap(bset));
10503 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10505 int i;
10506 uint32_t hash;
10508 if (!map)
10509 return 0;
10510 map = isl_map_copy(map);
10511 map = isl_map_normalize(map);
10512 if (!map)
10513 return 0;
10515 hash = isl_hash_init();
10516 for (i = 0; i < map->n; ++i) {
10517 uint32_t bmap_hash;
10518 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10519 isl_hash_hash(hash, bmap_hash);
10522 isl_map_free(map);
10524 return hash;
10527 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10529 return isl_map_get_hash(set_to_map(set));
10532 /* Return the number of basic maps in the (current) representation of "map".
10534 int isl_map_n_basic_map(__isl_keep isl_map *map)
10536 return map ? map->n : 0;
10539 int isl_set_n_basic_set(__isl_keep isl_set *set)
10541 return set ? set->n : 0;
10544 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10545 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10547 int i;
10549 if (!map)
10550 return isl_stat_error;
10552 for (i = 0; i < map->n; ++i)
10553 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10554 return isl_stat_error;
10556 return isl_stat_ok;
10559 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10560 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10562 int i;
10564 if (!set)
10565 return isl_stat_error;
10567 for (i = 0; i < set->n; ++i)
10568 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10569 return isl_stat_error;
10571 return isl_stat_ok;
10574 /* Return a list of basic sets, the union of which is equal to "set".
10576 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10577 __isl_keep isl_set *set)
10579 int i;
10580 isl_basic_set_list *list;
10582 if (!set)
10583 return NULL;
10585 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10586 for (i = 0; i < set->n; ++i) {
10587 isl_basic_set *bset;
10589 bset = isl_basic_set_copy(set->p[i]);
10590 list = isl_basic_set_list_add(list, bset);
10593 return list;
10596 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10598 isl_space *space;
10600 if (!bset)
10601 return NULL;
10603 bset = isl_basic_set_cow(bset);
10604 if (!bset)
10605 return NULL;
10607 space = isl_basic_set_get_space(bset);
10608 space = isl_space_lift(space, bset->n_div);
10609 if (!space)
10610 goto error;
10611 isl_space_free(bset->dim);
10612 bset->dim = space;
10613 bset->extra -= bset->n_div;
10614 bset->n_div = 0;
10616 bset = isl_basic_set_finalize(bset);
10618 return bset;
10619 error:
10620 isl_basic_set_free(bset);
10621 return NULL;
10624 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10626 int i;
10627 isl_space *space;
10628 unsigned n_div;
10630 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10632 if (!set)
10633 return NULL;
10635 set = isl_set_cow(set);
10636 if (!set)
10637 return NULL;
10639 n_div = set->p[0]->n_div;
10640 space = isl_set_get_space(set);
10641 space = isl_space_lift(space, n_div);
10642 if (!space)
10643 goto error;
10644 isl_space_free(set->dim);
10645 set->dim = space;
10647 for (i = 0; i < set->n; ++i) {
10648 set->p[i] = isl_basic_set_lift(set->p[i]);
10649 if (!set->p[i])
10650 goto error;
10653 return set;
10654 error:
10655 isl_set_free(set);
10656 return NULL;
10659 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10661 isl_size dim;
10662 int size = 0;
10664 dim = isl_basic_set_dim(bset, isl_dim_all);
10665 if (dim < 0)
10666 return -1;
10667 size += bset->n_eq * (1 + dim);
10668 size += bset->n_ineq * (1 + dim);
10669 size += bset->n_div * (2 + dim);
10671 return size;
10674 int isl_set_size(__isl_keep isl_set *set)
10676 int i;
10677 int size = 0;
10679 if (!set)
10680 return -1;
10682 for (i = 0; i < set->n; ++i)
10683 size += isl_basic_set_size(set->p[i]);
10685 return size;
10688 /* Check if there is any lower bound (if lower == 0) and/or upper
10689 * bound (if upper == 0) on the specified dim.
10691 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10692 enum isl_dim_type type, unsigned pos, int lower, int upper)
10694 int i;
10696 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10697 return isl_bool_error;
10699 pos += isl_basic_map_offset(bmap, type);
10701 for (i = 0; i < bmap->n_div; ++i) {
10702 if (isl_int_is_zero(bmap->div[i][0]))
10703 continue;
10704 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10705 return isl_bool_true;
10708 for (i = 0; i < bmap->n_eq; ++i)
10709 if (!isl_int_is_zero(bmap->eq[i][pos]))
10710 return isl_bool_true;
10712 for (i = 0; i < bmap->n_ineq; ++i) {
10713 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10714 if (sgn > 0)
10715 lower = 1;
10716 if (sgn < 0)
10717 upper = 1;
10720 return lower && upper;
10723 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10724 enum isl_dim_type type, unsigned pos)
10726 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10729 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10730 enum isl_dim_type type, unsigned pos)
10732 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10735 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10736 enum isl_dim_type type, unsigned pos)
10738 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10741 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10742 enum isl_dim_type type, unsigned pos)
10744 int i;
10746 if (!map)
10747 return isl_bool_error;
10749 for (i = 0; i < map->n; ++i) {
10750 isl_bool bounded;
10751 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10752 if (bounded < 0 || !bounded)
10753 return bounded;
10756 return isl_bool_true;
10759 /* Return true if the specified dim is involved in both an upper bound
10760 * and a lower bound.
10762 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10763 enum isl_dim_type type, unsigned pos)
10765 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10768 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10770 static isl_bool has_any_bound(__isl_keep isl_map *map,
10771 enum isl_dim_type type, unsigned pos,
10772 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10773 enum isl_dim_type type, unsigned pos))
10775 int i;
10777 if (!map)
10778 return isl_bool_error;
10780 for (i = 0; i < map->n; ++i) {
10781 isl_bool bounded;
10782 bounded = fn(map->p[i], type, pos);
10783 if (bounded < 0 || bounded)
10784 return bounded;
10787 return isl_bool_false;
10790 /* Return 1 if the specified dim is involved in any lower bound.
10792 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10793 enum isl_dim_type type, unsigned pos)
10795 return has_any_bound(set, type, pos,
10796 &isl_basic_map_dim_has_lower_bound);
10799 /* Return 1 if the specified dim is involved in any upper bound.
10801 isl_bool isl_set_dim_has_any_upper_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_upper_bound);
10808 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10810 static isl_bool has_bound(__isl_keep isl_map *map,
10811 enum isl_dim_type type, unsigned pos,
10812 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10813 enum isl_dim_type type, unsigned pos))
10815 int i;
10817 if (!map)
10818 return isl_bool_error;
10820 for (i = 0; i < map->n; ++i) {
10821 isl_bool bounded;
10822 bounded = fn(map->p[i], type, pos);
10823 if (bounded < 0 || !bounded)
10824 return bounded;
10827 return isl_bool_true;
10830 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10832 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10833 enum isl_dim_type type, unsigned pos)
10835 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10838 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10840 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10841 enum isl_dim_type type, unsigned pos)
10843 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10846 /* For each of the "n" variables starting at "first", determine
10847 * the sign of the variable and put the results in the first "n"
10848 * elements of the array "signs".
10849 * Sign
10850 * 1 means that the variable is non-negative
10851 * -1 means that the variable is non-positive
10852 * 0 means the variable attains both positive and negative values.
10854 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10855 unsigned first, unsigned n, int *signs)
10857 isl_vec *bound = NULL;
10858 struct isl_tab *tab = NULL;
10859 struct isl_tab_undo *snap;
10860 int i;
10861 isl_size total;
10863 total = isl_basic_set_dim(bset, isl_dim_all);
10864 if (total < 0 || !signs)
10865 return isl_stat_error;
10867 bound = isl_vec_alloc(bset->ctx, 1 + total);
10868 tab = isl_tab_from_basic_set(bset, 0);
10869 if (!bound || !tab)
10870 goto error;
10872 isl_seq_clr(bound->el, bound->size);
10873 isl_int_set_si(bound->el[0], -1);
10875 snap = isl_tab_snap(tab);
10876 for (i = 0; i < n; ++i) {
10877 int empty;
10879 isl_int_set_si(bound->el[1 + first + i], -1);
10880 if (isl_tab_add_ineq(tab, bound->el) < 0)
10881 goto error;
10882 empty = tab->empty;
10883 isl_int_set_si(bound->el[1 + first + i], 0);
10884 if (isl_tab_rollback(tab, snap) < 0)
10885 goto error;
10887 if (empty) {
10888 signs[i] = 1;
10889 continue;
10892 isl_int_set_si(bound->el[1 + first + i], 1);
10893 if (isl_tab_add_ineq(tab, bound->el) < 0)
10894 goto error;
10895 empty = tab->empty;
10896 isl_int_set_si(bound->el[1 + first + i], 0);
10897 if (isl_tab_rollback(tab, snap) < 0)
10898 goto error;
10900 signs[i] = empty ? -1 : 0;
10903 isl_tab_free(tab);
10904 isl_vec_free(bound);
10905 return isl_stat_ok;
10906 error:
10907 isl_tab_free(tab);
10908 isl_vec_free(bound);
10909 return isl_stat_error;
10912 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10913 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10915 if (!bset || !signs)
10916 return isl_stat_error;
10917 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10918 return isl_stat_error;
10920 first += pos(bset->dim, type) - 1;
10921 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10924 /* Is it possible for the integer division "div" to depend (possibly
10925 * indirectly) on any output dimensions?
10927 * If the div is undefined, then we conservatively assume that it
10928 * may depend on them.
10929 * Otherwise, we check if it actually depends on them or on any integer
10930 * divisions that may depend on them.
10932 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10934 int i;
10935 isl_size n_out, n_div;
10936 unsigned o_out, o_div;
10938 if (isl_int_is_zero(bmap->div[div][0]))
10939 return isl_bool_true;
10941 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10942 if (n_out < 0)
10943 return isl_bool_error;
10944 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10946 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10947 return isl_bool_true;
10949 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10950 if (n_div < 0)
10951 return isl_bool_error;
10952 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10954 for (i = 0; i < n_div; ++i) {
10955 isl_bool may_involve;
10957 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10958 continue;
10959 may_involve = div_may_involve_output(bmap, i);
10960 if (may_involve < 0 || may_involve)
10961 return may_involve;
10964 return isl_bool_false;
10967 /* Return the first integer division of "bmap" in the range
10968 * [first, first + n[ that may depend on any output dimensions and
10969 * that has a non-zero coefficient in "c" (where the first coefficient
10970 * in "c" corresponds to integer division "first").
10972 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10973 isl_int *c, int first, int n)
10975 int k;
10977 if (!bmap)
10978 return -1;
10980 for (k = first; k < first + n; ++k) {
10981 isl_bool may_involve;
10983 if (isl_int_is_zero(c[k]))
10984 continue;
10985 may_involve = div_may_involve_output(bmap, k);
10986 if (may_involve < 0)
10987 return -1;
10988 if (may_involve)
10989 return k;
10992 return first + n;
10995 /* Look for a pair of inequality constraints in "bmap" of the form
10997 * -l + i >= 0 or i >= l
10998 * and
10999 * n + l - i >= 0 or i <= l + n
11001 * with n < "m" and i the output dimension at position "pos".
11002 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11003 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11004 * and earlier output dimensions, as well as integer divisions that do
11005 * not involve any of the output dimensions.
11007 * Return the index of the first inequality constraint or bmap->n_ineq
11008 * if no such pair can be found.
11010 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11011 int pos, isl_int m)
11013 int i, j;
11014 isl_ctx *ctx;
11015 isl_size total;
11016 isl_size n_div, n_out;
11017 unsigned o_div, o_out;
11018 int less;
11020 total = isl_basic_map_dim(bmap, isl_dim_all);
11021 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11022 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11023 if (total < 0 || n_out < 0 || n_div < 0)
11024 return -1;
11026 ctx = isl_basic_map_get_ctx(bmap);
11027 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11028 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11029 for (i = 0; i < bmap->n_ineq; ++i) {
11030 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11031 continue;
11032 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11033 n_out - (pos + 1)) != -1)
11034 continue;
11035 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11036 0, n_div) < n_div)
11037 continue;
11038 for (j = i + 1; j < bmap->n_ineq; ++j) {
11039 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11040 ctx->one))
11041 continue;
11042 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11043 bmap->ineq[j] + 1, total))
11044 continue;
11045 break;
11047 if (j >= bmap->n_ineq)
11048 continue;
11049 isl_int_add(bmap->ineq[i][0],
11050 bmap->ineq[i][0], bmap->ineq[j][0]);
11051 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11052 isl_int_sub(bmap->ineq[i][0],
11053 bmap->ineq[i][0], bmap->ineq[j][0]);
11054 if (!less)
11055 continue;
11056 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11057 return i;
11058 else
11059 return j;
11062 return bmap->n_ineq;
11065 /* Return the index of the equality of "bmap" that defines
11066 * the output dimension "pos" in terms of earlier dimensions.
11067 * The equality may also involve integer divisions, as long
11068 * as those integer divisions are defined in terms of
11069 * parameters or input dimensions.
11070 * In this case, *div is set to the number of integer divisions and
11071 * *ineq is set to the number of inequality constraints (provided
11072 * div and ineq are not NULL).
11074 * The equality may also involve a single integer division involving
11075 * the output dimensions (typically only output dimension "pos") as
11076 * long as the coefficient of output dimension "pos" is 1 or -1 and
11077 * there is a pair of constraints i >= l and i <= l + n, with i referring
11078 * to output dimension "pos", l an expression involving only earlier
11079 * dimensions and n smaller than the coefficient of the integer division
11080 * in the equality. In this case, the output dimension can be defined
11081 * in terms of a modulo expression that does not involve the integer division.
11082 * *div is then set to this single integer division and
11083 * *ineq is set to the index of constraint i >= l.
11085 * Return bmap->n_eq if there is no such equality.
11086 * Return -1 on error.
11088 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11089 int pos, int *div, int *ineq)
11091 int j, k, l;
11092 isl_size n_div, n_out;
11093 unsigned o_div, o_out;
11095 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11096 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11097 if (n_out < 0 || n_div < 0)
11098 return -1;
11100 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11101 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11103 if (ineq)
11104 *ineq = bmap->n_ineq;
11105 if (div)
11106 *div = n_div;
11107 for (j = 0; j < bmap->n_eq; ++j) {
11108 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11109 continue;
11110 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11111 n_out - (pos + 1)) != -1)
11112 continue;
11113 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11114 0, n_div);
11115 if (k >= n_div)
11116 return j;
11117 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11118 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11119 continue;
11120 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11121 k + 1, n_div - (k+1)) < n_div)
11122 continue;
11123 l = find_modulo_constraint_pair(bmap, pos,
11124 bmap->eq[j][o_div + k]);
11125 if (l < 0)
11126 return -1;
11127 if (l >= bmap->n_ineq)
11128 continue;
11129 if (div)
11130 *div = k;
11131 if (ineq)
11132 *ineq = l;
11133 return j;
11136 return bmap->n_eq;
11139 /* Check if the given basic map is obviously single-valued.
11140 * In particular, for each output dimension, check that there is
11141 * an equality that defines the output dimension in terms of
11142 * earlier dimensions.
11144 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11146 int i;
11147 isl_size n_out;
11149 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11150 if (n_out < 0)
11151 return isl_bool_error;
11153 for (i = 0; i < n_out; ++i) {
11154 int eq;
11156 eq = isl_basic_map_output_defining_equality(bmap, i,
11157 NULL, NULL);
11158 if (eq < 0)
11159 return isl_bool_error;
11160 if (eq >= bmap->n_eq)
11161 return isl_bool_false;
11164 return isl_bool_true;
11167 /* Check if the given basic map is single-valued.
11168 * We simply compute
11170 * M \circ M^-1
11172 * and check if the result is a subset of the identity mapping.
11174 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11176 isl_space *space;
11177 isl_basic_map *test;
11178 isl_basic_map *id;
11179 isl_bool sv;
11181 sv = isl_basic_map_plain_is_single_valued(bmap);
11182 if (sv < 0 || sv)
11183 return sv;
11185 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11186 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11188 space = isl_basic_map_get_space(bmap);
11189 space = isl_space_map_from_set(isl_space_range(space));
11190 id = isl_basic_map_identity(space);
11192 sv = isl_basic_map_is_subset(test, id);
11194 isl_basic_map_free(test);
11195 isl_basic_map_free(id);
11197 return sv;
11200 /* Check if the given map is obviously single-valued.
11202 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11204 if (!map)
11205 return isl_bool_error;
11206 if (map->n == 0)
11207 return isl_bool_true;
11208 if (map->n >= 2)
11209 return isl_bool_false;
11211 return isl_basic_map_plain_is_single_valued(map->p[0]);
11214 /* Check if the given map is single-valued.
11215 * We simply compute
11217 * M \circ M^-1
11219 * and check if the result is a subset of the identity mapping.
11221 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11223 isl_space *dim;
11224 isl_map *test;
11225 isl_map *id;
11226 isl_bool sv;
11228 sv = isl_map_plain_is_single_valued(map);
11229 if (sv < 0 || sv)
11230 return sv;
11232 test = isl_map_reverse(isl_map_copy(map));
11233 test = isl_map_apply_range(test, isl_map_copy(map));
11235 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11236 id = isl_map_identity(dim);
11238 sv = isl_map_is_subset(test, id);
11240 isl_map_free(test);
11241 isl_map_free(id);
11243 return sv;
11246 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11248 isl_bool in;
11250 map = isl_map_copy(map);
11251 map = isl_map_reverse(map);
11252 in = isl_map_is_single_valued(map);
11253 isl_map_free(map);
11255 return in;
11258 /* Check if the given map is obviously injective.
11260 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11262 isl_bool in;
11264 map = isl_map_copy(map);
11265 map = isl_map_reverse(map);
11266 in = isl_map_plain_is_single_valued(map);
11267 isl_map_free(map);
11269 return in;
11272 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11274 isl_bool sv;
11276 sv = isl_map_is_single_valued(map);
11277 if (sv < 0 || !sv)
11278 return sv;
11280 return isl_map_is_injective(map);
11283 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11285 return isl_map_is_single_valued(set_to_map(set));
11288 /* Does "map" only map elements to themselves?
11290 * If the domain and range spaces are different, then "map"
11291 * is considered not to be an identity relation, even if it is empty.
11292 * Otherwise, construct the maximal identity relation and
11293 * check whether "map" is a subset of this relation.
11295 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11297 isl_space *space;
11298 isl_map *id;
11299 isl_bool equal, is_identity;
11301 space = isl_map_get_space(map);
11302 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11303 isl_space_free(space);
11304 if (equal < 0 || !equal)
11305 return equal;
11307 id = isl_map_identity(isl_map_get_space(map));
11308 is_identity = isl_map_is_subset(map, id);
11309 isl_map_free(id);
11311 return is_identity;
11314 int isl_map_is_translation(__isl_keep isl_map *map)
11316 int ok;
11317 isl_set *delta;
11319 delta = isl_map_deltas(isl_map_copy(map));
11320 ok = isl_set_is_singleton(delta);
11321 isl_set_free(delta);
11323 return ok;
11326 static int unique(isl_int *p, unsigned pos, unsigned len)
11328 if (isl_seq_first_non_zero(p, pos) != -1)
11329 return 0;
11330 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11331 return 0;
11332 return 1;
11335 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11337 int i, j;
11338 isl_size nvar, n_div;
11339 unsigned ovar;
11341 n_div = isl_basic_set_dim(bset, isl_dim_div);
11342 if (n_div < 0)
11343 return isl_bool_error;
11344 if (n_div != 0)
11345 return isl_bool_false;
11347 nvar = isl_basic_set_dim(bset, isl_dim_set);
11348 if (nvar < 0)
11349 return isl_bool_error;
11350 ovar = isl_space_offset(bset->dim, isl_dim_set);
11351 for (j = 0; j < nvar; ++j) {
11352 int lower = 0, upper = 0;
11353 for (i = 0; i < bset->n_eq; ++i) {
11354 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11355 continue;
11356 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11357 return isl_bool_false;
11358 break;
11360 if (i < bset->n_eq)
11361 continue;
11362 for (i = 0; i < bset->n_ineq; ++i) {
11363 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11364 continue;
11365 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11366 return isl_bool_false;
11367 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11368 lower = 1;
11369 else
11370 upper = 1;
11372 if (!lower || !upper)
11373 return isl_bool_false;
11376 return isl_bool_true;
11379 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11381 if (!set)
11382 return isl_bool_error;
11383 if (set->n != 1)
11384 return isl_bool_false;
11386 return isl_basic_set_is_box(set->p[0]);
11389 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11391 if (!bset)
11392 return isl_bool_error;
11394 return isl_space_is_wrapping(bset->dim);
11397 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11399 if (!set)
11400 return isl_bool_error;
11402 return isl_space_is_wrapping(set->dim);
11405 /* Modify the space of "map" through a call to "change".
11406 * If "can_change" is set (not NULL), then first call it to check
11407 * if the modification is allowed, printing the error message "cannot_change"
11408 * if it is not.
11410 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11411 isl_bool (*can_change)(__isl_keep isl_map *map),
11412 const char *cannot_change,
11413 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11415 isl_bool ok;
11416 isl_space *space;
11418 if (!map)
11419 return NULL;
11421 ok = can_change ? can_change(map) : isl_bool_true;
11422 if (ok < 0)
11423 return isl_map_free(map);
11424 if (!ok)
11425 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11426 return isl_map_free(map));
11428 space = change(isl_map_get_space(map));
11429 map = isl_map_reset_space(map, space);
11431 return map;
11434 /* Is the domain of "map" a wrapped relation?
11436 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11438 if (!map)
11439 return isl_bool_error;
11441 return isl_space_domain_is_wrapping(map->dim);
11444 /* Does "map" have a wrapped relation in both domain and range?
11446 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11448 return isl_space_is_product(isl_map_peek_space(map));
11451 /* Is the range of "map" a wrapped relation?
11453 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11455 if (!map)
11456 return isl_bool_error;
11458 return isl_space_range_is_wrapping(map->dim);
11461 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11463 bmap = isl_basic_map_cow(bmap);
11464 if (!bmap)
11465 return NULL;
11467 bmap->dim = isl_space_wrap(bmap->dim);
11468 if (!bmap->dim)
11469 goto error;
11471 bmap = isl_basic_map_finalize(bmap);
11473 return bset_from_bmap(bmap);
11474 error:
11475 isl_basic_map_free(bmap);
11476 return NULL;
11479 /* Given a map A -> B, return the set (A -> B).
11481 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11483 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11486 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11488 bset = isl_basic_set_cow(bset);
11489 if (!bset)
11490 return NULL;
11492 bset->dim = isl_space_unwrap(bset->dim);
11493 if (!bset->dim)
11494 goto error;
11496 bset = isl_basic_set_finalize(bset);
11498 return bset_to_bmap(bset);
11499 error:
11500 isl_basic_set_free(bset);
11501 return NULL;
11504 /* Given a set (A -> B), return the map A -> B.
11505 * Error out if "set" is not of the form (A -> B).
11507 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11509 return isl_map_change_space(set, &isl_set_is_wrapping,
11510 "not a wrapping set", &isl_space_unwrap);
11513 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11514 enum isl_dim_type type)
11516 if (!bmap)
11517 return NULL;
11519 if (!isl_space_is_named_or_nested(bmap->dim, type))
11520 return bmap;
11522 bmap = isl_basic_map_cow(bmap);
11523 if (!bmap)
11524 return NULL;
11526 bmap->dim = isl_space_reset(bmap->dim, type);
11527 if (!bmap->dim)
11528 goto error;
11530 bmap = isl_basic_map_finalize(bmap);
11532 return bmap;
11533 error:
11534 isl_basic_map_free(bmap);
11535 return NULL;
11538 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11539 enum isl_dim_type type)
11541 int i;
11543 if (!map)
11544 return NULL;
11546 if (!isl_space_is_named_or_nested(map->dim, type))
11547 return map;
11549 map = isl_map_cow(map);
11550 if (!map)
11551 return NULL;
11553 for (i = 0; i < map->n; ++i) {
11554 map->p[i] = isl_basic_map_reset(map->p[i], type);
11555 if (!map->p[i])
11556 goto error;
11558 map->dim = isl_space_reset(map->dim, type);
11559 if (!map->dim)
11560 goto error;
11562 return map;
11563 error:
11564 isl_map_free(map);
11565 return NULL;
11568 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11570 if (!bmap)
11571 return NULL;
11573 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11574 return bmap;
11576 bmap = isl_basic_map_cow(bmap);
11577 if (!bmap)
11578 return NULL;
11580 bmap->dim = isl_space_flatten(bmap->dim);
11581 if (!bmap->dim)
11582 goto error;
11584 bmap = isl_basic_map_finalize(bmap);
11586 return bmap;
11587 error:
11588 isl_basic_map_free(bmap);
11589 return NULL;
11592 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11594 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11597 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11598 __isl_take isl_basic_map *bmap)
11600 if (!bmap)
11601 return NULL;
11603 if (!bmap->dim->nested[0])
11604 return bmap;
11606 bmap = isl_basic_map_cow(bmap);
11607 if (!bmap)
11608 return NULL;
11610 bmap->dim = isl_space_flatten_domain(bmap->dim);
11611 if (!bmap->dim)
11612 goto error;
11614 bmap = isl_basic_map_finalize(bmap);
11616 return bmap;
11617 error:
11618 isl_basic_map_free(bmap);
11619 return NULL;
11622 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11623 __isl_take isl_basic_map *bmap)
11625 if (!bmap)
11626 return NULL;
11628 if (!bmap->dim->nested[1])
11629 return bmap;
11631 bmap = isl_basic_map_cow(bmap);
11632 if (!bmap)
11633 return NULL;
11635 bmap->dim = isl_space_flatten_range(bmap->dim);
11636 if (!bmap->dim)
11637 goto error;
11639 bmap = isl_basic_map_finalize(bmap);
11641 return bmap;
11642 error:
11643 isl_basic_map_free(bmap);
11644 return NULL;
11647 /* Remove any internal structure from the spaces of domain and range of "map".
11649 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11651 if (!map)
11652 return NULL;
11654 if (!map->dim->nested[0] && !map->dim->nested[1])
11655 return map;
11657 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11660 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11662 return set_from_map(isl_map_flatten(set_to_map(set)));
11665 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11667 isl_space *space, *flat_space;
11668 isl_map *map;
11670 space = isl_set_get_space(set);
11671 flat_space = isl_space_flatten(isl_space_copy(space));
11672 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11673 flat_space));
11674 map = isl_map_intersect_domain(map, set);
11676 return map;
11679 /* Remove any internal structure from the space of the domain of "map".
11681 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11683 if (!map)
11684 return NULL;
11686 if (!map->dim->nested[0])
11687 return map;
11689 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11692 /* Remove any internal structure from the space of the range of "map".
11694 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11696 if (!map)
11697 return NULL;
11699 if (!map->dim->nested[1])
11700 return map;
11702 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11705 /* Reorder the dimensions of "bmap" according to the given dim_map
11706 * and set the dimension specification to "space" and
11707 * perform Gaussian elimination on the result.
11709 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11710 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11712 isl_basic_map *res;
11713 unsigned flags;
11714 isl_size n_div;
11716 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11717 if (n_div < 0 || !space || !dim_map)
11718 goto error;
11720 flags = bmap->flags;
11721 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11722 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11723 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11724 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11725 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11726 if (res)
11727 res->flags = flags;
11728 res = isl_basic_map_gauss(res, NULL);
11729 res = isl_basic_map_finalize(res);
11730 return res;
11731 error:
11732 isl_dim_map_free(dim_map);
11733 isl_basic_map_free(bmap);
11734 isl_space_free(space);
11735 return NULL;
11738 /* Reorder the dimensions of "map" according to given reordering.
11740 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11741 __isl_take isl_reordering *r)
11743 int i;
11744 struct isl_dim_map *dim_map;
11746 map = isl_map_cow(map);
11747 dim_map = isl_dim_map_from_reordering(r);
11748 if (!map || !r || !dim_map)
11749 goto error;
11751 for (i = 0; i < map->n; ++i) {
11752 struct isl_dim_map *dim_map_i;
11753 isl_space *space;
11755 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11757 space = isl_reordering_get_space(r);
11758 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11760 if (!map->p[i])
11761 goto error;
11764 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11765 map = isl_map_unmark_normalized(map);
11767 isl_reordering_free(r);
11768 isl_dim_map_free(dim_map);
11769 return map;
11770 error:
11771 isl_dim_map_free(dim_map);
11772 isl_map_free(map);
11773 isl_reordering_free(r);
11774 return NULL;
11777 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11778 __isl_take isl_reordering *r)
11780 return set_from_map(isl_map_realign(set_to_map(set), r));
11783 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11784 __isl_take isl_space *model)
11786 isl_ctx *ctx;
11787 isl_bool aligned;
11789 if (!map || !model)
11790 goto error;
11792 ctx = isl_space_get_ctx(model);
11793 if (!isl_space_has_named_params(model))
11794 isl_die(ctx, isl_error_invalid,
11795 "model has unnamed parameters", goto error);
11796 if (isl_map_check_named_params(map) < 0)
11797 goto error;
11798 aligned = isl_map_space_has_equal_params(map, model);
11799 if (aligned < 0)
11800 goto error;
11801 if (!aligned) {
11802 isl_reordering *exp;
11804 exp = isl_parameter_alignment_reordering(map->dim, model);
11805 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11806 map = isl_map_realign(map, exp);
11809 isl_space_free(model);
11810 return map;
11811 error:
11812 isl_space_free(model);
11813 isl_map_free(map);
11814 return NULL;
11817 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11818 __isl_take isl_space *model)
11820 return isl_map_align_params(set, model);
11823 /* Align the parameters of "bmap" to those of "model", introducing
11824 * additional parameters if needed.
11826 __isl_give isl_basic_map *isl_basic_map_align_params(
11827 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11829 isl_ctx *ctx;
11830 isl_bool equal_params;
11832 if (!bmap || !model)
11833 goto error;
11835 ctx = isl_space_get_ctx(model);
11836 if (!isl_space_has_named_params(model))
11837 isl_die(ctx, isl_error_invalid,
11838 "model has unnamed parameters", goto error);
11839 if (isl_basic_map_check_named_params(bmap) < 0)
11840 goto error;
11841 equal_params = isl_space_has_equal_params(bmap->dim, model);
11842 if (equal_params < 0)
11843 goto error;
11844 if (!equal_params) {
11845 isl_reordering *exp;
11846 struct isl_dim_map *dim_map;
11848 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11849 exp = isl_reordering_extend_space(exp,
11850 isl_basic_map_get_space(bmap));
11851 dim_map = isl_dim_map_from_reordering(exp);
11852 bmap = isl_basic_map_realign(bmap,
11853 isl_reordering_get_space(exp),
11854 isl_dim_map_extend(dim_map, bmap));
11855 isl_reordering_free(exp);
11856 isl_dim_map_free(dim_map);
11859 isl_space_free(model);
11860 return bmap;
11861 error:
11862 isl_space_free(model);
11863 isl_basic_map_free(bmap);
11864 return NULL;
11867 /* Do "bset" and "space" have the same parameters?
11869 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11870 __isl_keep isl_space *space)
11872 isl_space *bset_space;
11874 bset_space = isl_basic_set_peek_space(bset);
11875 return isl_space_has_equal_params(bset_space, space);
11878 /* Do "map" and "space" have the same parameters?
11880 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11881 __isl_keep isl_space *space)
11883 isl_space *map_space;
11885 map_space = isl_map_peek_space(map);
11886 return isl_space_has_equal_params(map_space, space);
11889 /* Do "set" and "space" have the same parameters?
11891 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11892 __isl_keep isl_space *space)
11894 return isl_map_space_has_equal_params(set_to_map(set), space);
11897 /* Align the parameters of "bset" to those of "model", introducing
11898 * additional parameters if needed.
11900 __isl_give isl_basic_set *isl_basic_set_align_params(
11901 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11903 return isl_basic_map_align_params(bset, model);
11906 /* Drop all parameters not referenced by "map".
11908 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11910 int i;
11911 isl_size n;
11913 n = isl_map_dim(map, isl_dim_param);
11914 if (isl_map_check_named_params(map) < 0 || n < 0)
11915 return isl_map_free(map);
11917 for (i = n - 1; i >= 0; i--) {
11918 isl_bool involves;
11920 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11921 if (involves < 0)
11922 return isl_map_free(map);
11923 if (!involves)
11924 map = isl_map_project_out(map, isl_dim_param, i, 1);
11927 return map;
11930 /* Drop all parameters not referenced by "set".
11932 __isl_give isl_set *isl_set_drop_unused_params(
11933 __isl_take isl_set *set)
11935 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11938 /* Drop all parameters not referenced by "bmap".
11940 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11941 __isl_take isl_basic_map *bmap)
11943 isl_size nparam;
11944 int i;
11946 nparam = isl_basic_map_dim(bmap, isl_dim_param);
11947 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
11948 return isl_basic_map_free(bmap);
11950 for (i = nparam - 1; i >= 0; i--) {
11951 isl_bool involves;
11953 involves = isl_basic_map_involves_dims(bmap,
11954 isl_dim_param, i, 1);
11955 if (involves < 0)
11956 return isl_basic_map_free(bmap);
11957 if (!involves)
11958 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11961 return bmap;
11964 /* Drop all parameters not referenced by "bset".
11966 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11967 __isl_take isl_basic_set *bset)
11969 return bset_from_bmap(isl_basic_map_drop_unused_params(
11970 bset_to_bmap(bset)));
11973 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11974 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11975 enum isl_dim_type c2, enum isl_dim_type c3,
11976 enum isl_dim_type c4, enum isl_dim_type c5)
11978 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11979 struct isl_mat *mat;
11980 int i, j, k;
11981 int pos;
11982 isl_size total;
11984 total = isl_basic_map_dim(bmap, isl_dim_all);
11985 if (total < 0)
11986 return NULL;
11987 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
11988 if (!mat)
11989 return NULL;
11990 for (i = 0; i < bmap->n_eq; ++i)
11991 for (j = 0, pos = 0; j < 5; ++j) {
11992 int off = isl_basic_map_offset(bmap, c[j]);
11993 isl_size dim = isl_basic_map_dim(bmap, c[j]);
11994 if (dim < 0)
11995 return isl_mat_free(mat);
11996 for (k = 0; k < dim; ++k) {
11997 isl_int_set(mat->row[i][pos],
11998 bmap->eq[i][off + k]);
11999 ++pos;
12003 return mat;
12006 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12007 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12008 enum isl_dim_type c2, enum isl_dim_type c3,
12009 enum isl_dim_type c4, enum isl_dim_type c5)
12011 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12012 struct isl_mat *mat;
12013 int i, j, k;
12014 int pos;
12015 isl_size total;
12017 total = isl_basic_map_dim(bmap, isl_dim_all);
12018 if (total < 0)
12019 return NULL;
12020 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12021 if (!mat)
12022 return NULL;
12023 for (i = 0; i < bmap->n_ineq; ++i)
12024 for (j = 0, pos = 0; j < 5; ++j) {
12025 int off = isl_basic_map_offset(bmap, c[j]);
12026 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12027 if (dim < 0)
12028 return isl_mat_free(mat);
12029 for (k = 0; k < dim; ++k) {
12030 isl_int_set(mat->row[i][pos],
12031 bmap->ineq[i][off + k]);
12032 ++pos;
12036 return mat;
12039 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12040 __isl_take isl_space *space,
12041 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12042 enum isl_dim_type c2, enum isl_dim_type c3,
12043 enum isl_dim_type c4, enum isl_dim_type c5)
12045 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12046 isl_basic_map *bmap = NULL;
12047 isl_size dim;
12048 unsigned total;
12049 unsigned extra;
12050 int i, j, k, l;
12051 int pos;
12053 dim = isl_space_dim(space, isl_dim_all);
12054 if (dim < 0 || !eq || !ineq)
12055 goto error;
12057 if (eq->n_col != ineq->n_col)
12058 isl_die(space->ctx, isl_error_invalid,
12059 "equalities and inequalities matrices should have "
12060 "same number of columns", goto error);
12062 total = 1 + dim;
12064 if (eq->n_col < total)
12065 isl_die(space->ctx, isl_error_invalid,
12066 "number of columns too small", goto error);
12068 extra = eq->n_col - total;
12070 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12071 eq->n_row, ineq->n_row);
12072 if (!bmap)
12073 goto error;
12074 for (i = 0; i < extra; ++i) {
12075 k = isl_basic_map_alloc_div(bmap);
12076 if (k < 0)
12077 goto error;
12078 isl_int_set_si(bmap->div[k][0], 0);
12080 for (i = 0; i < eq->n_row; ++i) {
12081 l = isl_basic_map_alloc_equality(bmap);
12082 if (l < 0)
12083 goto error;
12084 for (j = 0, pos = 0; j < 5; ++j) {
12085 int off = isl_basic_map_offset(bmap, c[j]);
12086 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12087 if (dim < 0)
12088 goto error;
12089 for (k = 0; k < dim; ++k) {
12090 isl_int_set(bmap->eq[l][off + k],
12091 eq->row[i][pos]);
12092 ++pos;
12096 for (i = 0; i < ineq->n_row; ++i) {
12097 l = isl_basic_map_alloc_inequality(bmap);
12098 if (l < 0)
12099 goto error;
12100 for (j = 0, pos = 0; j < 5; ++j) {
12101 int off = isl_basic_map_offset(bmap, c[j]);
12102 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12103 if (dim < 0)
12104 goto error;
12105 for (k = 0; k < dim; ++k) {
12106 isl_int_set(bmap->ineq[l][off + k],
12107 ineq->row[i][pos]);
12108 ++pos;
12113 isl_space_free(space);
12114 isl_mat_free(eq);
12115 isl_mat_free(ineq);
12117 bmap = isl_basic_map_simplify(bmap);
12118 return isl_basic_map_finalize(bmap);
12119 error:
12120 isl_space_free(space);
12121 isl_mat_free(eq);
12122 isl_mat_free(ineq);
12123 isl_basic_map_free(bmap);
12124 return NULL;
12127 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12128 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12129 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12131 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12132 c1, c2, c3, c4, isl_dim_in);
12135 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12136 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12137 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12139 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12140 c1, c2, c3, c4, isl_dim_in);
12143 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12144 __isl_take isl_space *dim,
12145 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12146 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12148 isl_basic_map *bmap;
12149 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12150 c1, c2, c3, c4, isl_dim_in);
12151 return bset_from_bmap(bmap);
12154 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12156 if (!bmap)
12157 return isl_bool_error;
12159 return isl_space_can_zip(bmap->dim);
12162 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12164 if (!map)
12165 return isl_bool_error;
12167 return isl_space_can_zip(map->dim);
12170 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12171 * (A -> C) -> (B -> D).
12173 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12175 unsigned pos;
12176 isl_size n_in;
12177 isl_size n1;
12178 isl_size n2;
12180 if (!bmap)
12181 return NULL;
12183 if (!isl_basic_map_can_zip(bmap))
12184 isl_die(bmap->ctx, isl_error_invalid,
12185 "basic map cannot be zipped", goto error);
12186 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12187 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12188 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12189 if (n_in < 0 || n1 < 0 || n2 < 0)
12190 return isl_basic_map_free(bmap);
12191 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12192 bmap = isl_basic_map_cow(bmap);
12193 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12194 if (!bmap)
12195 return NULL;
12196 bmap->dim = isl_space_zip(bmap->dim);
12197 if (!bmap->dim)
12198 goto error;
12199 bmap = isl_basic_map_mark_final(bmap);
12200 return bmap;
12201 error:
12202 isl_basic_map_free(bmap);
12203 return NULL;
12206 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12207 * (A -> C) -> (B -> D).
12209 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12211 int i;
12213 if (!map)
12214 return NULL;
12216 if (!isl_map_can_zip(map))
12217 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12218 goto error);
12220 map = isl_map_cow(map);
12221 if (!map)
12222 return NULL;
12224 for (i = 0; i < map->n; ++i) {
12225 map->p[i] = isl_basic_map_zip(map->p[i]);
12226 if (!map->p[i])
12227 goto error;
12230 map->dim = isl_space_zip(map->dim);
12231 if (!map->dim)
12232 goto error;
12234 return map;
12235 error:
12236 isl_map_free(map);
12237 return NULL;
12240 /* Can we apply isl_basic_map_curry to "bmap"?
12241 * That is, does it have a nested relation in its domain?
12243 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12245 if (!bmap)
12246 return isl_bool_error;
12248 return isl_space_can_curry(bmap->dim);
12251 /* Can we apply isl_map_curry to "map"?
12252 * That is, does it have a nested relation in its domain?
12254 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12256 if (!map)
12257 return isl_bool_error;
12259 return isl_space_can_curry(map->dim);
12262 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12263 * A -> (B -> C).
12265 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12268 if (!bmap)
12269 return NULL;
12271 if (!isl_basic_map_can_curry(bmap))
12272 isl_die(bmap->ctx, isl_error_invalid,
12273 "basic map cannot be curried", goto error);
12274 bmap = isl_basic_map_cow(bmap);
12275 if (!bmap)
12276 return NULL;
12277 bmap->dim = isl_space_curry(bmap->dim);
12278 if (!bmap->dim)
12279 goto error;
12280 bmap = isl_basic_map_mark_final(bmap);
12281 return bmap;
12282 error:
12283 isl_basic_map_free(bmap);
12284 return NULL;
12287 /* Given a map (A -> B) -> C, return the corresponding map
12288 * A -> (B -> C).
12290 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12292 return isl_map_change_space(map, &isl_map_can_curry,
12293 "map cannot be curried", &isl_space_curry);
12296 /* Can isl_map_range_curry be applied to "map"?
12297 * That is, does it have a nested relation in its range,
12298 * the domain of which is itself a nested relation?
12300 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12302 if (!map)
12303 return isl_bool_error;
12305 return isl_space_can_range_curry(map->dim);
12308 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12309 * A -> (B -> (C -> D)).
12311 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12313 return isl_map_change_space(map, &isl_map_can_range_curry,
12314 "map range cannot be curried",
12315 &isl_space_range_curry);
12318 /* Can we apply isl_basic_map_uncurry to "bmap"?
12319 * That is, does it have a nested relation in its domain?
12321 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12323 if (!bmap)
12324 return isl_bool_error;
12326 return isl_space_can_uncurry(bmap->dim);
12329 /* Can we apply isl_map_uncurry to "map"?
12330 * That is, does it have a nested relation in its domain?
12332 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12334 if (!map)
12335 return isl_bool_error;
12337 return isl_space_can_uncurry(map->dim);
12340 /* Given a basic map A -> (B -> C), return the corresponding basic map
12341 * (A -> B) -> C.
12343 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12346 if (!bmap)
12347 return NULL;
12349 if (!isl_basic_map_can_uncurry(bmap))
12350 isl_die(bmap->ctx, isl_error_invalid,
12351 "basic map cannot be uncurried",
12352 return isl_basic_map_free(bmap));
12353 bmap = isl_basic_map_cow(bmap);
12354 if (!bmap)
12355 return NULL;
12356 bmap->dim = isl_space_uncurry(bmap->dim);
12357 if (!bmap->dim)
12358 return isl_basic_map_free(bmap);
12359 bmap = isl_basic_map_mark_final(bmap);
12360 return bmap;
12363 /* Given a map A -> (B -> C), return the corresponding map
12364 * (A -> B) -> C.
12366 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12368 return isl_map_change_space(map, &isl_map_can_uncurry,
12369 "map cannot be uncurried", &isl_space_uncurry);
12372 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12373 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12375 return isl_map_equate(set, type1, pos1, type2, pos2);
12378 /* Construct a basic map where the given dimensions are equal to each other.
12380 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12381 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12383 isl_basic_map *bmap = NULL;
12384 int i;
12385 isl_size total;
12387 total = isl_space_dim(space, isl_dim_all);
12388 if (total < 0 ||
12389 isl_space_check_range(space, type1, pos1, 1) < 0 ||
12390 isl_space_check_range(space, type2, pos2, 1) < 0)
12391 goto error;
12393 if (type1 == type2 && pos1 == pos2)
12394 return isl_basic_map_universe(space);
12396 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12397 i = isl_basic_map_alloc_equality(bmap);
12398 if (i < 0)
12399 goto error;
12400 isl_seq_clr(bmap->eq[i], 1 + total);
12401 pos1 += isl_basic_map_offset(bmap, type1);
12402 pos2 += isl_basic_map_offset(bmap, type2);
12403 isl_int_set_si(bmap->eq[i][pos1], -1);
12404 isl_int_set_si(bmap->eq[i][pos2], 1);
12405 bmap = isl_basic_map_finalize(bmap);
12406 isl_space_free(space);
12407 return bmap;
12408 error:
12409 isl_space_free(space);
12410 isl_basic_map_free(bmap);
12411 return NULL;
12414 /* Add a constraint imposing that the given two dimensions are equal.
12416 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12417 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12419 isl_basic_map *eq;
12421 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12423 bmap = isl_basic_map_intersect(bmap, eq);
12425 return bmap;
12428 /* Add a constraint imposing that the given two dimensions are equal.
12430 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12431 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12433 isl_basic_map *bmap;
12435 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12437 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12439 return map;
12442 /* Add a constraint imposing that the given two dimensions have opposite values.
12444 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12445 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12447 isl_basic_map *bmap = NULL;
12448 int i;
12449 isl_size total;
12451 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12452 return isl_map_free(map);
12453 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12454 return isl_map_free(map);
12456 total = isl_map_dim(map, isl_dim_all);
12457 if (total < 0)
12458 return isl_map_free(map);
12459 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12460 i = isl_basic_map_alloc_equality(bmap);
12461 if (i < 0)
12462 goto error;
12463 isl_seq_clr(bmap->eq[i], 1 + total);
12464 pos1 += isl_basic_map_offset(bmap, type1);
12465 pos2 += isl_basic_map_offset(bmap, type2);
12466 isl_int_set_si(bmap->eq[i][pos1], 1);
12467 isl_int_set_si(bmap->eq[i][pos2], 1);
12468 bmap = isl_basic_map_finalize(bmap);
12470 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12472 return map;
12473 error:
12474 isl_basic_map_free(bmap);
12475 isl_map_free(map);
12476 return NULL;
12479 /* Construct a constraint imposing that the value of the first dimension is
12480 * greater than or equal to that of the second.
12482 static __isl_give isl_constraint *constraint_order_ge(
12483 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12484 enum isl_dim_type type2, int pos2)
12486 isl_constraint *c;
12488 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12489 isl_space_check_range(space, type2, pos2, 1) < 0)
12490 space = isl_space_free(space);
12491 if (!space)
12492 return NULL;
12494 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12496 if (type1 == type2 && pos1 == pos2)
12497 return c;
12499 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12500 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12502 return c;
12505 /* Add a constraint imposing that the value of the first dimension is
12506 * greater than or equal to that of the second.
12508 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12509 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12511 isl_constraint *c;
12512 isl_space *space;
12514 if (type1 == type2 && pos1 == pos2)
12515 return bmap;
12516 space = isl_basic_map_get_space(bmap);
12517 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12518 bmap = isl_basic_map_add_constraint(bmap, c);
12520 return bmap;
12523 /* Add a constraint imposing that the value of the first dimension is
12524 * greater than or equal to that of the second.
12526 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12527 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12529 isl_constraint *c;
12530 isl_space *space;
12532 if (type1 == type2 && pos1 == pos2)
12533 return map;
12534 space = isl_map_get_space(map);
12535 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12536 map = isl_map_add_constraint(map, c);
12538 return map;
12541 /* Add a constraint imposing that the value of the first dimension is
12542 * less than or equal to that of the second.
12544 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12545 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12547 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12550 /* Construct a basic map where the value of the first dimension is
12551 * greater than that of the second.
12553 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12554 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12556 isl_basic_map *bmap = NULL;
12557 int i;
12558 isl_size total;
12560 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12561 isl_space_check_range(space, type2, pos2, 1) < 0)
12562 goto error;
12564 if (type1 == type2 && pos1 == pos2)
12565 return isl_basic_map_empty(space);
12567 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12568 total = isl_basic_map_dim(bmap, isl_dim_all);
12569 i = isl_basic_map_alloc_inequality(bmap);
12570 if (total < 0 || i < 0)
12571 return isl_basic_map_free(bmap);
12572 isl_seq_clr(bmap->ineq[i], 1 + total);
12573 pos1 += isl_basic_map_offset(bmap, type1);
12574 pos2 += isl_basic_map_offset(bmap, type2);
12575 isl_int_set_si(bmap->ineq[i][pos1], 1);
12576 isl_int_set_si(bmap->ineq[i][pos2], -1);
12577 isl_int_set_si(bmap->ineq[i][0], -1);
12578 bmap = isl_basic_map_finalize(bmap);
12580 return bmap;
12581 error:
12582 isl_space_free(space);
12583 isl_basic_map_free(bmap);
12584 return NULL;
12587 /* Add a constraint imposing that the value of the first dimension is
12588 * greater than that of the second.
12590 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12591 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12593 isl_basic_map *gt;
12595 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12597 bmap = isl_basic_map_intersect(bmap, gt);
12599 return bmap;
12602 /* Add a constraint imposing that the value of the first dimension is
12603 * greater than that of the second.
12605 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12606 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12608 isl_basic_map *bmap;
12610 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12612 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12614 return map;
12617 /* Add a constraint imposing that the value of the first dimension is
12618 * smaller than that of the second.
12620 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12621 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12623 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12626 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12627 int pos)
12629 isl_aff *div;
12630 isl_local_space *ls;
12632 if (!bmap)
12633 return NULL;
12635 if (!isl_basic_map_divs_known(bmap))
12636 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12637 "some divs are unknown", return NULL);
12639 ls = isl_basic_map_get_local_space(bmap);
12640 div = isl_local_space_get_div(ls, pos);
12641 isl_local_space_free(ls);
12643 return div;
12646 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12647 int pos)
12649 return isl_basic_map_get_div(bset, pos);
12652 /* Plug in "subs" for dimension "type", "pos" of "bset".
12654 * Let i be the dimension to replace and let "subs" be of the form
12656 * f/d
12658 * Any integer division with a non-zero coefficient for i,
12660 * floor((a i + g)/m)
12662 * is replaced by
12664 * floor((a f + d g)/(m d))
12666 * Constraints of the form
12668 * a i + g
12670 * are replaced by
12672 * a f + d g
12674 * We currently require that "subs" is an integral expression.
12675 * Handling rational expressions may require us to add stride constraints
12676 * as we do in isl_basic_set_preimage_multi_aff.
12678 __isl_give isl_basic_set *isl_basic_set_substitute(
12679 __isl_take isl_basic_set *bset,
12680 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12682 int i;
12683 isl_int v;
12684 isl_ctx *ctx;
12685 isl_size n_div;
12687 if (bset && isl_basic_set_plain_is_empty(bset))
12688 return bset;
12690 bset = isl_basic_set_cow(bset);
12691 if (!bset || !subs)
12692 goto error;
12694 ctx = isl_basic_set_get_ctx(bset);
12695 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12696 isl_die(ctx, isl_error_invalid,
12697 "spaces don't match", goto error);
12698 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
12699 if (n_div < 0)
12700 goto error;
12701 if (n_div != 0)
12702 isl_die(ctx, isl_error_unsupported,
12703 "cannot handle divs yet", goto error);
12704 if (!isl_int_is_one(subs->v->el[0]))
12705 isl_die(ctx, isl_error_invalid,
12706 "can only substitute integer expressions", goto error);
12708 pos += isl_basic_set_offset(bset, type);
12710 isl_int_init(v);
12712 for (i = 0; i < bset->n_eq; ++i) {
12713 if (isl_int_is_zero(bset->eq[i][pos]))
12714 continue;
12715 isl_int_set(v, bset->eq[i][pos]);
12716 isl_int_set_si(bset->eq[i][pos], 0);
12717 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12718 v, subs->v->el + 1, subs->v->size - 1);
12721 for (i = 0; i < bset->n_ineq; ++i) {
12722 if (isl_int_is_zero(bset->ineq[i][pos]))
12723 continue;
12724 isl_int_set(v, bset->ineq[i][pos]);
12725 isl_int_set_si(bset->ineq[i][pos], 0);
12726 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12727 v, subs->v->el + 1, subs->v->size - 1);
12730 for (i = 0; i < bset->n_div; ++i) {
12731 if (isl_int_is_zero(bset->div[i][1 + pos]))
12732 continue;
12733 isl_int_set(v, bset->div[i][1 + pos]);
12734 isl_int_set_si(bset->div[i][1 + pos], 0);
12735 isl_seq_combine(bset->div[i] + 1,
12736 subs->v->el[0], bset->div[i] + 1,
12737 v, subs->v->el + 1, subs->v->size - 1);
12738 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12741 isl_int_clear(v);
12743 bset = isl_basic_set_simplify(bset);
12744 return isl_basic_set_finalize(bset);
12745 error:
12746 isl_basic_set_free(bset);
12747 return NULL;
12750 /* Plug in "subs" for dimension "type", "pos" of "set".
12752 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12753 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12755 int i;
12757 if (set && isl_set_plain_is_empty(set))
12758 return set;
12760 set = isl_set_cow(set);
12761 if (!set || !subs)
12762 goto error;
12764 for (i = set->n - 1; i >= 0; --i) {
12765 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12766 set = set_from_map(remove_if_empty(set_to_map(set), i));
12767 if (!set)
12768 return NULL;
12771 return set;
12772 error:
12773 isl_set_free(set);
12774 return NULL;
12777 /* Check if the range of "ma" is compatible with the domain or range
12778 * (depending on "type") of "bmap".
12780 static isl_stat check_basic_map_compatible_range_multi_aff(
12781 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12782 __isl_keep isl_multi_aff *ma)
12784 isl_bool m;
12785 isl_space *ma_space;
12787 ma_space = isl_multi_aff_get_space(ma);
12789 m = isl_space_has_equal_params(bmap->dim, ma_space);
12790 if (m < 0)
12791 goto error;
12792 if (!m)
12793 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12794 "parameters don't match", goto error);
12795 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12796 if (m < 0)
12797 goto error;
12798 if (!m)
12799 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12800 "spaces don't match", goto error);
12802 isl_space_free(ma_space);
12803 return isl_stat_ok;
12804 error:
12805 isl_space_free(ma_space);
12806 return isl_stat_error;
12809 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12810 * coefficients before the transformed range of dimensions,
12811 * the "n_after" coefficients after the transformed range of dimensions
12812 * and the coefficients of the other divs in "bmap".
12814 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12815 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12817 int i;
12818 isl_size n_param;
12819 isl_size n_set;
12820 isl_local_space *ls;
12822 if (n_div == 0)
12823 return bmap;
12825 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12826 n_param = isl_local_space_dim(ls, isl_dim_param);
12827 n_set = isl_local_space_dim(ls, isl_dim_set);
12828 if (n_param < 0 || n_set < 0)
12829 return isl_basic_map_free(bmap);
12831 for (i = 0; i < n_div; ++i) {
12832 int o_bmap = 0, o_ls = 0;
12834 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12835 o_bmap += 1 + 1 + n_param;
12836 o_ls += 1 + 1 + n_param;
12837 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12838 o_bmap += n_before;
12839 isl_seq_cpy(bmap->div[i] + o_bmap,
12840 ls->div->row[i] + o_ls, n_set);
12841 o_bmap += n_set;
12842 o_ls += n_set;
12843 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12844 o_bmap += n_after;
12845 isl_seq_cpy(bmap->div[i] + o_bmap,
12846 ls->div->row[i] + o_ls, n_div);
12847 o_bmap += n_div;
12848 o_ls += n_div;
12849 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12850 bmap = isl_basic_map_add_div_constraints(bmap, i);
12851 if (!bmap)
12852 goto error;
12855 isl_local_space_free(ls);
12856 return bmap;
12857 error:
12858 isl_local_space_free(ls);
12859 return isl_basic_map_free(bmap);
12862 /* How many stride constraints does "ma" enforce?
12863 * That is, how many of the affine expressions have a denominator
12864 * different from one?
12866 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12868 int i;
12869 int strides = 0;
12871 for (i = 0; i < ma->n; ++i)
12872 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12873 strides++;
12875 return strides;
12878 /* For each affine expression in ma of the form
12880 * x_i = (f_i y + h_i)/m_i
12882 * with m_i different from one, add a constraint to "bmap"
12883 * of the form
12885 * f_i y + h_i = m_i alpha_i
12887 * with alpha_i an additional existentially quantified variable.
12889 * The input variables of "ma" correspond to a subset of the variables
12890 * of "bmap". There are "n_before" variables in "bmap" before this
12891 * subset and "n_after" variables after this subset.
12892 * The integer divisions of the affine expressions in "ma" are assumed
12893 * to have been aligned. There are "n_div_ma" of them and
12894 * they appear first in "bmap", straight after the "n_after" variables.
12896 static __isl_give isl_basic_map *add_ma_strides(
12897 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12898 int n_before, int n_after, int n_div_ma)
12900 int i, k;
12901 int div;
12902 isl_size total;
12903 isl_size n_param;
12904 isl_size n_in;
12906 total = isl_basic_map_dim(bmap, isl_dim_all);
12907 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12908 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12909 if (total < 0 || n_param < 0 || n_in < 0)
12910 return isl_basic_map_free(bmap);
12911 for (i = 0; i < ma->n; ++i) {
12912 int o_bmap = 0, o_ma = 1;
12914 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12915 continue;
12916 div = isl_basic_map_alloc_div(bmap);
12917 k = isl_basic_map_alloc_equality(bmap);
12918 if (div < 0 || k < 0)
12919 goto error;
12920 isl_int_set_si(bmap->div[div][0], 0);
12921 isl_seq_cpy(bmap->eq[k] + o_bmap,
12922 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12923 o_bmap += 1 + n_param;
12924 o_ma += 1 + n_param;
12925 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12926 o_bmap += n_before;
12927 isl_seq_cpy(bmap->eq[k] + o_bmap,
12928 ma->u.p[i]->v->el + o_ma, n_in);
12929 o_bmap += n_in;
12930 o_ma += n_in;
12931 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12932 o_bmap += n_after;
12933 isl_seq_cpy(bmap->eq[k] + o_bmap,
12934 ma->u.p[i]->v->el + o_ma, n_div_ma);
12935 o_bmap += n_div_ma;
12936 o_ma += n_div_ma;
12937 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12938 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12939 total++;
12942 return bmap;
12943 error:
12944 isl_basic_map_free(bmap);
12945 return NULL;
12948 /* Replace the domain or range space (depending on "type) of "space" by "set".
12950 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12951 enum isl_dim_type type, __isl_take isl_space *set)
12953 if (type == isl_dim_in) {
12954 space = isl_space_range(space);
12955 space = isl_space_map_from_domain_and_range(set, space);
12956 } else {
12957 space = isl_space_domain(space);
12958 space = isl_space_map_from_domain_and_range(space, set);
12961 return space;
12964 /* Compute the preimage of the domain or range (depending on "type")
12965 * of "bmap" under the function represented by "ma".
12966 * In other words, plug in "ma" in the domain or range of "bmap".
12967 * The result is a basic map that lives in the same space as "bmap"
12968 * except that the domain or range has been replaced by
12969 * the domain space of "ma".
12971 * If bmap is represented by
12973 * A(p) + S u + B x + T v + C(divs) >= 0,
12975 * where u and x are input and output dimensions if type == isl_dim_out
12976 * while x and v are input and output dimensions if type == isl_dim_in,
12977 * and ma is represented by
12979 * x = D(p) + F(y) + G(divs')
12981 * then the result is
12983 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12985 * The divs in the input set are similarly adjusted.
12986 * In particular
12988 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12990 * becomes
12992 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12993 * B_i G(divs') + c_i(divs))/n_i)
12995 * If bmap is not a rational map and if F(y) involves any denominators
12997 * x_i = (f_i y + h_i)/m_i
12999 * then additional constraints are added to ensure that we only
13000 * map back integer points. That is we enforce
13002 * f_i y + h_i = m_i alpha_i
13004 * with alpha_i an additional existentially quantified variable.
13006 * We first copy over the divs from "ma".
13007 * Then we add the modified constraints and divs from "bmap".
13008 * Finally, we add the stride constraints, if needed.
13010 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13011 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13012 __isl_take isl_multi_aff *ma)
13014 int i, k;
13015 isl_space *space;
13016 isl_basic_map *res = NULL;
13017 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13018 isl_int f, c1, c2, g;
13019 isl_bool rational;
13020 int strides;
13022 isl_int_init(f);
13023 isl_int_init(c1);
13024 isl_int_init(c2);
13025 isl_int_init(g);
13027 ma = isl_multi_aff_align_divs(ma);
13028 if (!bmap || !ma)
13029 goto error;
13030 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13031 goto error;
13033 if (type == isl_dim_in) {
13034 n_before = 0;
13035 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13036 } else {
13037 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13038 n_after = 0;
13040 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13041 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13042 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13043 goto error;
13045 space = isl_multi_aff_get_domain_space(ma);
13046 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13047 rational = isl_basic_map_is_rational(bmap);
13048 strides = rational ? 0 : multi_aff_strides(ma);
13049 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13050 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13051 if (rational)
13052 res = isl_basic_map_set_rational(res);
13054 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13055 if (isl_basic_map_alloc_div(res) < 0)
13056 goto error;
13058 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13059 if (!res)
13060 goto error;
13062 for (i = 0; i < bmap->n_eq; ++i) {
13063 k = isl_basic_map_alloc_equality(res);
13064 if (k < 0)
13065 goto error;
13066 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13067 n_after, n_div_ma, n_div_bmap,
13068 f, c1, c2, g, 0) < 0)
13069 goto error;
13072 for (i = 0; i < bmap->n_ineq; ++i) {
13073 k = isl_basic_map_alloc_inequality(res);
13074 if (k < 0)
13075 goto error;
13076 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13077 n_after, n_div_ma, n_div_bmap,
13078 f, c1, c2, g, 0) < 0)
13079 goto error;
13082 for (i = 0; i < bmap->n_div; ++i) {
13083 if (isl_int_is_zero(bmap->div[i][0])) {
13084 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13085 continue;
13087 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13088 n_before, n_after, n_div_ma, n_div_bmap,
13089 f, c1, c2, g, 1) < 0)
13090 goto error;
13093 if (strides)
13094 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13096 isl_int_clear(f);
13097 isl_int_clear(c1);
13098 isl_int_clear(c2);
13099 isl_int_clear(g);
13100 isl_basic_map_free(bmap);
13101 isl_multi_aff_free(ma);
13102 res = isl_basic_map_simplify(res);
13103 return isl_basic_map_finalize(res);
13104 error:
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 isl_basic_map_free(res);
13112 return NULL;
13115 /* Compute the preimage of "bset" under the function represented by "ma".
13116 * In other words, plug in "ma" in "bset". The result is a basic set
13117 * that lives in the domain space of "ma".
13119 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13120 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13122 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13125 /* Compute the preimage of the domain of "bmap" under the function
13126 * represented by "ma".
13127 * In other words, plug in "ma" in the domain of "bmap".
13128 * The result is a basic map that lives in the same space as "bmap"
13129 * except that the domain has been replaced by the domain space of "ma".
13131 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13132 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13134 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13137 /* Compute the preimage of the range of "bmap" under the function
13138 * represented by "ma".
13139 * In other words, plug in "ma" in the range of "bmap".
13140 * The result is a basic map that lives in the same space as "bmap"
13141 * except that the range has been replaced by the domain space of "ma".
13143 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13144 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13146 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13149 /* Check if the range of "ma" is compatible with the domain or range
13150 * (depending on "type") of "map".
13151 * Return isl_stat_error if anything is wrong.
13153 static isl_stat check_map_compatible_range_multi_aff(
13154 __isl_keep isl_map *map, enum isl_dim_type type,
13155 __isl_keep isl_multi_aff *ma)
13157 isl_bool m;
13158 isl_space *ma_space;
13160 ma_space = isl_multi_aff_get_space(ma);
13161 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13162 isl_space_free(ma_space);
13163 if (m < 0)
13164 return isl_stat_error;
13165 if (!m)
13166 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13167 "spaces don't match", return isl_stat_error);
13168 return isl_stat_ok;
13171 /* Compute the preimage of the domain or range (depending on "type")
13172 * of "map" under the function represented by "ma".
13173 * In other words, plug in "ma" in the domain or range of "map".
13174 * The result is a map that lives in the same space as "map"
13175 * except that the domain or range has been replaced by
13176 * the domain space of "ma".
13178 * The parameters are assumed to have been aligned.
13180 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13181 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13183 int i;
13184 isl_space *space;
13186 map = isl_map_cow(map);
13187 ma = isl_multi_aff_align_divs(ma);
13188 if (!map || !ma)
13189 goto error;
13190 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13191 goto error;
13193 for (i = 0; i < map->n; ++i) {
13194 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13195 isl_multi_aff_copy(ma));
13196 if (!map->p[i])
13197 goto error;
13200 space = isl_multi_aff_get_domain_space(ma);
13201 space = isl_space_set(isl_map_get_space(map), type, space);
13203 isl_space_free(map->dim);
13204 map->dim = space;
13205 if (!map->dim)
13206 goto error;
13208 isl_multi_aff_free(ma);
13209 if (map->n > 1)
13210 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13211 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13212 return map;
13213 error:
13214 isl_multi_aff_free(ma);
13215 isl_map_free(map);
13216 return NULL;
13219 /* Compute the preimage of the domain or range (depending on "type")
13220 * of "map" under the function represented by "ma".
13221 * In other words, plug in "ma" in the domain or range of "map".
13222 * The result is a map that lives in the same space as "map"
13223 * except that the domain or range has been replaced by
13224 * the domain space of "ma".
13226 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13227 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13229 isl_bool aligned;
13231 if (!map || !ma)
13232 goto error;
13234 aligned = isl_map_space_has_equal_params(map, ma->space);
13235 if (aligned < 0)
13236 goto error;
13237 if (aligned)
13238 return map_preimage_multi_aff(map, type, ma);
13240 if (isl_map_check_named_params(map) < 0)
13241 goto error;
13242 if (!isl_space_has_named_params(ma->space))
13243 isl_die(map->ctx, isl_error_invalid,
13244 "unaligned unnamed parameters", goto error);
13245 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13246 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13248 return map_preimage_multi_aff(map, type, ma);
13249 error:
13250 isl_multi_aff_free(ma);
13251 return isl_map_free(map);
13254 /* Compute the preimage of "set" under the function represented by "ma".
13255 * In other words, plug in "ma" in "set". The result is a set
13256 * that lives in the domain space of "ma".
13258 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13259 __isl_take isl_multi_aff *ma)
13261 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13264 /* Compute the preimage of the domain of "map" under the function
13265 * represented by "ma".
13266 * In other words, plug in "ma" in the domain of "map".
13267 * The result is a map that lives in the same space as "map"
13268 * except that the domain has been replaced by the domain space of "ma".
13270 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13271 __isl_take isl_multi_aff *ma)
13273 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13276 /* Compute the preimage of the range of "map" under the function
13277 * represented by "ma".
13278 * In other words, plug in "ma" in the range of "map".
13279 * The result is a map that lives in the same space as "map"
13280 * except that the range has been replaced by the domain space of "ma".
13282 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13283 __isl_take isl_multi_aff *ma)
13285 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13288 /* Compute the preimage of "map" under the function represented by "pma".
13289 * In other words, plug in "pma" in the domain or range of "map".
13290 * The result is a map that lives in the same space as "map",
13291 * except that the space of type "type" has been replaced by
13292 * the domain space of "pma".
13294 * The parameters of "map" and "pma" are assumed to have been aligned.
13296 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13297 __isl_take isl_map *map, enum isl_dim_type type,
13298 __isl_take isl_pw_multi_aff *pma)
13300 int i;
13301 isl_map *res;
13303 if (!pma)
13304 goto error;
13306 if (pma->n == 0) {
13307 isl_pw_multi_aff_free(pma);
13308 res = isl_map_empty(isl_map_get_space(map));
13309 isl_map_free(map);
13310 return res;
13313 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13314 isl_multi_aff_copy(pma->p[0].maff));
13315 if (type == isl_dim_in)
13316 res = isl_map_intersect_domain(res,
13317 isl_map_copy(pma->p[0].set));
13318 else
13319 res = isl_map_intersect_range(res,
13320 isl_map_copy(pma->p[0].set));
13322 for (i = 1; i < pma->n; ++i) {
13323 isl_map *res_i;
13325 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13326 isl_multi_aff_copy(pma->p[i].maff));
13327 if (type == isl_dim_in)
13328 res_i = isl_map_intersect_domain(res_i,
13329 isl_map_copy(pma->p[i].set));
13330 else
13331 res_i = isl_map_intersect_range(res_i,
13332 isl_map_copy(pma->p[i].set));
13333 res = isl_map_union(res, res_i);
13336 isl_pw_multi_aff_free(pma);
13337 isl_map_free(map);
13338 return res;
13339 error:
13340 isl_pw_multi_aff_free(pma);
13341 isl_map_free(map);
13342 return NULL;
13345 /* Compute the preimage of "map" under the function represented by "pma".
13346 * In other words, plug in "pma" in the domain or range of "map".
13347 * The result is a map that lives in the same space as "map",
13348 * except that the space of type "type" has been replaced by
13349 * the domain space of "pma".
13351 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13352 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13354 isl_bool aligned;
13356 if (!map || !pma)
13357 goto error;
13359 aligned = isl_map_space_has_equal_params(map, pma->dim);
13360 if (aligned < 0)
13361 goto error;
13362 if (aligned)
13363 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13365 if (isl_map_check_named_params(map) < 0)
13366 goto error;
13367 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13368 goto error;
13369 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13370 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13372 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13373 error:
13374 isl_pw_multi_aff_free(pma);
13375 return isl_map_free(map);
13378 /* Compute the preimage of "set" under the function represented by "pma".
13379 * In other words, plug in "pma" in "set". The result is a set
13380 * that lives in the domain space of "pma".
13382 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13383 __isl_take isl_pw_multi_aff *pma)
13385 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13388 /* Compute the preimage of the domain of "map" under the function
13389 * represented by "pma".
13390 * In other words, plug in "pma" in the domain of "map".
13391 * The result is a map that lives in the same space as "map",
13392 * except that domain space has been replaced by the domain space of "pma".
13394 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13395 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13397 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13400 /* Compute the preimage of the range of "map" under the function
13401 * represented by "pma".
13402 * In other words, plug in "pma" in the range of "map".
13403 * The result is a map that lives in the same space as "map",
13404 * except that range space has been replaced by the domain space of "pma".
13406 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13407 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13409 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13412 /* Compute the preimage of "map" under the function represented by "mpa".
13413 * In other words, plug in "mpa" in the domain or range of "map".
13414 * The result is a map that lives in the same space as "map",
13415 * except that the space of type "type" has been replaced by
13416 * the domain space of "mpa".
13418 * If the map does not involve any constraints that refer to the
13419 * dimensions of the substituted space, then the only possible
13420 * effect of "mpa" on the map is to map the space to a different space.
13421 * We create a separate isl_multi_aff to effectuate this change
13422 * in order to avoid spurious splitting of the map along the pieces
13423 * of "mpa".
13424 * If "mpa" has a non-trivial explicit domain, however,
13425 * then the full substitution should be performed.
13427 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13428 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13430 isl_size n;
13431 isl_bool full;
13432 isl_pw_multi_aff *pma;
13434 n = isl_map_dim(map, type);
13435 if (n < 0 || !mpa)
13436 goto error;
13438 full = isl_map_involves_dims(map, type, 0, n);
13439 if (full >= 0 && !full)
13440 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13441 if (full < 0)
13442 goto error;
13443 if (!full) {
13444 isl_space *space;
13445 isl_multi_aff *ma;
13447 space = isl_multi_pw_aff_get_space(mpa);
13448 isl_multi_pw_aff_free(mpa);
13449 ma = isl_multi_aff_zero(space);
13450 return isl_map_preimage_multi_aff(map, type, ma);
13453 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13454 return isl_map_preimage_pw_multi_aff(map, type, pma);
13455 error:
13456 isl_map_free(map);
13457 isl_multi_pw_aff_free(mpa);
13458 return NULL;
13461 /* Compute the preimage of "map" under the function represented by "mpa".
13462 * In other words, plug in "mpa" in the domain "map".
13463 * The result is a map that lives in the same space as "map",
13464 * except that domain space has been replaced by the domain space of "mpa".
13466 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13467 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13469 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13472 /* Compute the preimage of "set" by the function represented by "mpa".
13473 * In other words, plug in "mpa" in "set".
13475 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13476 __isl_take isl_multi_pw_aff *mpa)
13478 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13481 /* Return a copy of the equality constraints of "bset" as a matrix.
13483 __isl_give isl_mat *isl_basic_set_extract_equalities(
13484 __isl_keep isl_basic_set *bset)
13486 isl_ctx *ctx;
13487 isl_size total;
13489 total = isl_basic_set_dim(bset, isl_dim_all);
13490 if (total < 0)
13491 return NULL;
13493 ctx = isl_basic_set_get_ctx(bset);
13494 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
13497 /* Are the "n" "coefficients" starting at "first" of the integer division
13498 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13499 * to each other?
13500 * The "coefficient" at position 0 is the denominator.
13501 * The "coefficient" at position 1 is the constant term.
13503 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13504 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13505 unsigned first, unsigned n)
13507 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13508 return isl_bool_error;
13509 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13510 return isl_bool_error;
13511 return isl_seq_eq(bmap1->div[pos1] + first,
13512 bmap2->div[pos2] + first, n);
13515 /* Are the integer division expressions at position "pos1" in "bmap1" and
13516 * "pos2" in "bmap2" equal to each other, except that the constant terms
13517 * are different?
13519 isl_bool isl_basic_map_equal_div_expr_except_constant(
13520 __isl_keep isl_basic_map *bmap1, int pos1,
13521 __isl_keep isl_basic_map *bmap2, int pos2)
13523 isl_bool equal;
13524 isl_size total, total2;
13526 total = isl_basic_map_dim(bmap1, isl_dim_all);
13527 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
13528 if (total < 0 || total2 < 0)
13529 return isl_bool_error;
13530 if (total != total2)
13531 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13532 "incomparable div expressions", return isl_bool_error);
13533 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13534 0, 1);
13535 if (equal < 0 || !equal)
13536 return equal;
13537 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13538 1, 1);
13539 if (equal < 0 || equal)
13540 return isl_bool_not(equal);
13541 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13542 2, total);
13545 /* Replace the numerator of the constant term of the integer division
13546 * expression at position "div" in "bmap" by "value".
13547 * The caller guarantees that this does not change the meaning
13548 * of the input.
13550 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13551 __isl_take isl_basic_map *bmap, int div, int value)
13553 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13554 return isl_basic_map_free(bmap);
13556 isl_int_set_si(bmap->div[div][1], value);
13558 return bmap;
13561 /* Is the point "inner" internal to inequality constraint "ineq"
13562 * of "bset"?
13563 * The point is considered to be internal to the inequality constraint,
13564 * if it strictly lies on the positive side of the inequality constraint,
13565 * or if it lies on the constraint and the constraint is lexico-positive.
13567 static isl_bool is_internal(__isl_keep isl_vec *inner,
13568 __isl_keep isl_basic_set *bset, int ineq)
13570 isl_ctx *ctx;
13571 int pos;
13572 isl_size total;
13574 if (!inner || !bset)
13575 return isl_bool_error;
13577 ctx = isl_basic_set_get_ctx(bset);
13578 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13579 &ctx->normalize_gcd);
13580 if (!isl_int_is_zero(ctx->normalize_gcd))
13581 return isl_int_is_nonneg(ctx->normalize_gcd);
13583 total = isl_basic_set_dim(bset, isl_dim_all);
13584 if (total < 0)
13585 return isl_bool_error;
13586 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13587 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13590 /* Tighten the inequality constraints of "bset" that are outward with respect
13591 * to the point "vec".
13592 * That is, tighten the constraints that are not satisfied by "vec".
13594 * "vec" is a point internal to some superset S of "bset" that is used
13595 * to make the subsets of S disjoint, by tightening one half of the constraints
13596 * that separate two subsets. In particular, the constraints of S
13597 * are all satisfied by "vec" and should not be tightened.
13598 * Of the internal constraints, those that have "vec" on the outside
13599 * are tightened. The shared facet is included in the adjacent subset
13600 * with the opposite constraint.
13601 * For constraints that saturate "vec", this criterion cannot be used
13602 * to determine which of the two sides should be tightened.
13603 * Instead, the sign of the first non-zero coefficient is used
13604 * to make this choice. Note that this second criterion is never used
13605 * on the constraints of S since "vec" is interior to "S".
13607 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13608 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13610 int j;
13612 bset = isl_basic_set_cow(bset);
13613 if (!bset)
13614 return NULL;
13615 for (j = 0; j < bset->n_ineq; ++j) {
13616 isl_bool internal;
13618 internal = is_internal(vec, bset, j);
13619 if (internal < 0)
13620 return isl_basic_set_free(bset);
13621 if (internal)
13622 continue;
13623 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13626 return bset;
13629 /* Replace the variables x of type "type" starting at "first" in "bmap"
13630 * by x' with x = M x' with M the matrix trans.
13631 * That is, replace the corresponding coefficients c by c M.
13633 * The transformation matrix should be a square matrix.
13635 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13636 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13637 __isl_take isl_mat *trans)
13639 unsigned pos;
13641 bmap = isl_basic_map_cow(bmap);
13642 if (!bmap || !trans)
13643 goto error;
13645 if (trans->n_row != trans->n_col)
13646 isl_die(trans->ctx, isl_error_invalid,
13647 "expecting square transformation matrix", goto error);
13648 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13649 goto error;
13651 pos = isl_basic_map_offset(bmap, type) + first;
13653 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13654 isl_mat_copy(trans)) < 0)
13655 goto error;
13656 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13657 isl_mat_copy(trans)) < 0)
13658 goto error;
13659 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13660 isl_mat_copy(trans)) < 0)
13661 goto error;
13663 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13664 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13666 isl_mat_free(trans);
13667 return bmap;
13668 error:
13669 isl_mat_free(trans);
13670 isl_basic_map_free(bmap);
13671 return NULL;
13674 /* Replace the variables x of type "type" starting at "first" in "bset"
13675 * by x' with x = M x' with M the matrix trans.
13676 * That is, replace the corresponding coefficients c by c M.
13678 * The transformation matrix should be a square matrix.
13680 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13681 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13682 __isl_take isl_mat *trans)
13684 return isl_basic_map_transform_dims(bset, type, first, trans);