Merge branch 'maint'
[isl.git] / isl_map.c
blob6f220cd1a12ce0279e0bb71cb5d36d30ff4ba498
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 #undef TYPE
65 #define TYPE isl_basic_map
66 #include "has_single_reference_templ.c"
68 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
70 switch (type) {
71 case isl_dim_param: return 1;
72 case isl_dim_in: return 1 + dim->nparam;
73 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
74 default: return 0;
78 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
79 enum isl_dim_type type)
81 if (!bmap)
82 return isl_size_error;
83 switch (type) {
84 case isl_dim_cst: return 1;
85 case isl_dim_param:
86 case isl_dim_in:
87 case isl_dim_out: return isl_space_dim(bmap->dim, type);
88 case isl_dim_div: return bmap->n_div;
89 case isl_dim_all: return isl_basic_map_total_dim(bmap);
90 default: return 0;
94 /* Return the space of "map".
96 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
98 return map ? map->dim : NULL;
101 /* Return the space of "set".
103 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
105 return isl_map_peek_space(set_to_map(set));
108 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
110 return isl_space_dim(isl_map_peek_space(map), type);
113 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
115 return isl_map_dim(set_to_map(set), type);
118 /* Return the position of the variables of the given type
119 * within the sequence of variables of "bmap".
121 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
122 enum isl_dim_type type)
124 isl_space *space;
126 space = isl_basic_map_peek_space(bmap);
127 if (!space)
128 return isl_size_error;
130 switch (type) {
131 case isl_dim_param:
132 case isl_dim_in:
133 case isl_dim_out: return isl_space_offset(space, type);
134 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
135 case isl_dim_cst:
136 default:
137 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
138 "invalid dimension type", return isl_size_error);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bset".
145 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
146 enum isl_dim_type type)
148 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
151 /* Return the position of the coefficients of the variables of the given type
152 * within the sequence of coefficients of "bmap".
154 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
155 enum isl_dim_type type)
157 switch (type) {
158 case isl_dim_cst: return 0;
159 case isl_dim_param:
160 case isl_dim_in:
161 case isl_dim_out:
162 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
163 default: return 0;
167 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
168 enum isl_dim_type type)
170 return isl_basic_map_offset(bset, type);
173 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
175 return pos(map->dim, type);
178 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
179 enum isl_dim_type type)
181 return isl_basic_map_dim(bset, type);
184 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
186 return isl_basic_set_dim(bset, isl_dim_set);
189 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
191 return isl_basic_set_dim(bset, isl_dim_param);
194 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
196 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
199 isl_size isl_set_n_dim(__isl_keep isl_set *set)
201 return isl_set_dim(set, isl_dim_set);
204 isl_size isl_set_n_param(__isl_keep isl_set *set)
206 return isl_set_dim(set, isl_dim_param);
209 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
211 isl_size dim;
213 if (!bmap)
214 return isl_size_error;
215 dim = isl_space_dim(bmap->dim, isl_dim_all);
216 if (dim < 0)
217 return isl_size_error;
218 return dim + bmap->n_div;
221 /* Return the number of equality constraints in the description of "bmap".
222 * Return -1 on error.
224 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
226 if (!bmap)
227 return -1;
228 return bmap->n_eq;
231 /* Return the number of equality constraints in the description of "bset".
232 * Return -1 on error.
234 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
236 return isl_basic_map_n_equality(bset_to_bmap(bset));
239 /* Return the number of inequality constraints in the description of "bmap".
240 * Return -1 on error.
242 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
244 if (!bmap)
245 return -1;
246 return bmap->n_ineq;
249 /* Return the number of inequality constraints in the description of "bset".
250 * Return -1 on error.
252 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
254 return isl_basic_map_n_inequality(bset_to_bmap(bset));
257 /* Do "bmap1" and "bmap2" have the same parameters?
259 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
260 __isl_keep isl_basic_map *bmap2)
262 isl_space *space1, *space2;
264 space1 = isl_basic_map_peek_space(bmap1);
265 space2 = isl_basic_map_peek_space(bmap2);
266 return isl_space_has_equal_params(space1, space2);
269 /* Do "map1" and "map2" have the same parameters?
271 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
272 __isl_keep isl_map *map2)
274 isl_space *space1, *space2;
276 space1 = isl_map_peek_space(map1);
277 space2 = isl_map_peek_space(map2);
278 return isl_space_has_equal_params(space1, space2);
281 /* Do "map" and "set" have the same parameters?
283 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
284 __isl_keep isl_set *set)
286 return isl_map_has_equal_params(map, set_to_map(set));
289 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
290 __isl_keep isl_set *set)
292 isl_bool m;
293 if (!map || !set)
294 return isl_bool_error;
295 m = isl_map_has_equal_params(map, set_to_map(set));
296 if (m < 0 || !m)
297 return m;
298 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
299 set->dim, isl_dim_set);
302 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
303 __isl_keep isl_basic_set *bset)
305 isl_bool m;
306 if (!bmap || !bset)
307 return isl_bool_error;
308 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
309 if (m < 0 || !m)
310 return m;
311 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
312 bset->dim, isl_dim_set);
315 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
316 __isl_keep isl_set *set)
318 isl_bool m;
319 if (!map || !set)
320 return isl_bool_error;
321 m = isl_map_has_equal_params(map, set_to_map(set));
322 if (m < 0 || !m)
323 return m;
324 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
325 set->dim, isl_dim_set);
328 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
329 __isl_keep isl_basic_set *bset)
331 isl_bool m;
332 if (!bmap || !bset)
333 return isl_bool_error;
334 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
335 if (m < 0 || !m)
336 return m;
337 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
338 bset->dim, isl_dim_set);
341 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
343 return bmap ? bmap->ctx : NULL;
346 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
348 return bset ? bset->ctx : NULL;
351 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
353 return map ? map->ctx : NULL;
356 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
358 return set ? set->ctx : NULL;
361 /* Return the space of "bmap".
363 __isl_keep isl_space *isl_basic_map_peek_space(
364 __isl_keep const isl_basic_map *bmap)
366 return bmap ? bmap->dim : NULL;
369 /* Return the space of "bset".
371 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
373 return isl_basic_map_peek_space(bset_to_bmap(bset));
376 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
378 return isl_space_copy(isl_basic_map_peek_space(bmap));
381 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
383 return isl_basic_map_get_space(bset_to_bmap(bset));
386 /* Extract the divs in "bmap" as a matrix.
388 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
390 int i;
391 isl_ctx *ctx;
392 isl_mat *div;
393 isl_size v_div;
394 unsigned cols;
396 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
397 if (v_div < 0)
398 return NULL;
400 ctx = isl_basic_map_get_ctx(bmap);
401 cols = 1 + 1 + v_div + bmap->n_div;
402 div = isl_mat_alloc(ctx, bmap->n_div, cols);
403 if (!div)
404 return NULL;
406 for (i = 0; i < bmap->n_div; ++i)
407 isl_seq_cpy(div->row[i], bmap->div[i], cols);
409 return div;
412 /* Extract the divs in "bset" as a matrix.
414 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
416 return isl_basic_map_get_divs(bset);
419 __isl_give isl_local_space *isl_basic_map_get_local_space(
420 __isl_keep isl_basic_map *bmap)
422 isl_mat *div;
424 if (!bmap)
425 return NULL;
427 div = isl_basic_map_get_divs(bmap);
428 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
431 __isl_give isl_local_space *isl_basic_set_get_local_space(
432 __isl_keep isl_basic_set *bset)
434 return isl_basic_map_get_local_space(bset);
437 /* For each known div d = floor(f/m), add the constraints
439 * f - m d >= 0
440 * -(f-(m-1)) + m d >= 0
442 * Do not finalize the result.
444 static __isl_give isl_basic_map *add_known_div_constraints(
445 __isl_take isl_basic_map *bmap)
447 int i;
448 isl_size n_div;
450 n_div = isl_basic_map_dim(bmap, isl_dim_div);
451 if (n_div < 0)
452 return isl_basic_map_free(bmap);
453 if (n_div == 0)
454 return bmap;
455 bmap = isl_basic_map_cow(bmap);
456 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
457 if (!bmap)
458 return NULL;
459 for (i = 0; i < n_div; ++i) {
460 if (isl_int_is_zero(bmap->div[i][0]))
461 continue;
462 bmap = isl_basic_map_add_div_constraints(bmap, i);
465 return bmap;
468 __isl_give isl_basic_map *isl_basic_map_from_local_space(
469 __isl_take isl_local_space *ls)
471 int i;
472 isl_size n_div;
473 isl_basic_map *bmap;
475 n_div = isl_local_space_dim(ls, isl_dim_div);
476 if (n_div < 0)
477 ls = isl_local_space_free(ls);
478 if (!ls)
479 return NULL;
481 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
482 n_div, 0, 2 * n_div);
484 for (i = 0; i < n_div; ++i)
485 if (isl_basic_map_alloc_div(bmap) < 0)
486 goto error;
488 for (i = 0; i < n_div; ++i)
489 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
490 bmap = add_known_div_constraints(bmap);
492 isl_local_space_free(ls);
493 return bmap;
494 error:
495 isl_local_space_free(ls);
496 isl_basic_map_free(bmap);
497 return NULL;
500 __isl_give isl_basic_set *isl_basic_set_from_local_space(
501 __isl_take isl_local_space *ls)
503 return isl_basic_map_from_local_space(ls);
506 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
508 return isl_space_copy(isl_map_peek_space(map));
511 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
513 if (!set)
514 return NULL;
515 return isl_space_copy(set->dim);
518 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
519 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
521 bmap = isl_basic_map_cow(bmap);
522 if (!bmap)
523 return NULL;
524 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
525 if (!bmap->dim)
526 goto error;
527 bmap = isl_basic_map_finalize(bmap);
528 return bmap;
529 error:
530 isl_basic_map_free(bmap);
531 return NULL;
534 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
535 __isl_take isl_basic_set *bset, const char *s)
537 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
540 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
541 enum isl_dim_type type)
543 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
546 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
547 enum isl_dim_type type, const char *s)
549 int i;
551 map = isl_map_cow(map);
552 if (!map)
553 return NULL;
555 map->dim = isl_space_set_tuple_name(map->dim, type, s);
556 if (!map->dim)
557 goto error;
559 for (i = 0; i < map->n; ++i) {
560 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
561 if (!map->p[i])
562 goto error;
565 return map;
566 error:
567 isl_map_free(map);
568 return NULL;
571 /* Replace the identifier of the tuple of type "type" by "id".
573 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
574 __isl_take isl_basic_map *bmap,
575 enum isl_dim_type type, __isl_take isl_id *id)
577 bmap = isl_basic_map_cow(bmap);
578 if (!bmap)
579 goto error;
580 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
581 if (!bmap->dim)
582 return isl_basic_map_free(bmap);
583 bmap = isl_basic_map_finalize(bmap);
584 return bmap;
585 error:
586 isl_id_free(id);
587 return NULL;
590 /* Replace the identifier of the tuple by "id".
592 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
593 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
595 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
598 /* Does the input or output tuple have a name?
600 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
602 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
605 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
606 enum isl_dim_type type)
608 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
611 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
612 const char *s)
614 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
615 isl_dim_set, s));
618 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
619 enum isl_dim_type type, __isl_take isl_id *id)
621 map = isl_map_cow(map);
622 if (!map)
623 goto error;
625 map->dim = isl_space_set_tuple_id(map->dim, type, id);
627 return isl_map_reset_space(map, isl_space_copy(map->dim));
628 error:
629 isl_id_free(id);
630 return NULL;
633 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
634 __isl_take isl_id *id)
636 return isl_map_set_tuple_id(set, isl_dim_set, id);
639 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
640 enum isl_dim_type type)
642 map = isl_map_cow(map);
643 if (!map)
644 return NULL;
646 map->dim = isl_space_reset_tuple_id(map->dim, type);
648 return isl_map_reset_space(map, isl_space_copy(map->dim));
651 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
653 return isl_map_reset_tuple_id(set, isl_dim_set);
656 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
658 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
661 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
662 enum isl_dim_type type)
664 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
667 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
669 return isl_map_has_tuple_id(set, isl_dim_set);
672 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
674 return isl_map_get_tuple_id(set, isl_dim_set);
677 /* Does the set tuple have a name?
679 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
681 if (!set)
682 return isl_bool_error;
683 return isl_space_has_tuple_name(set->dim, isl_dim_set);
687 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
689 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
692 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
694 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
697 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
698 enum isl_dim_type type, unsigned pos)
700 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
703 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
704 enum isl_dim_type type, unsigned pos)
706 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
709 /* Does the given dimension have a name?
711 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
712 enum isl_dim_type type, unsigned pos)
714 if (!map)
715 return isl_bool_error;
716 return isl_space_has_dim_name(map->dim, type, pos);
719 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
720 enum isl_dim_type type, unsigned pos)
722 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
725 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
726 enum isl_dim_type type, unsigned pos)
728 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
731 /* Does the given dimension have a name?
733 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
736 if (!set)
737 return isl_bool_error;
738 return isl_space_has_dim_name(set->dim, type, pos);
741 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
742 __isl_take isl_basic_map *bmap,
743 enum isl_dim_type type, unsigned pos, const char *s)
745 bmap = isl_basic_map_cow(bmap);
746 if (!bmap)
747 return NULL;
748 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
749 if (!bmap->dim)
750 goto error;
751 return isl_basic_map_finalize(bmap);
752 error:
753 isl_basic_map_free(bmap);
754 return NULL;
757 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
758 enum isl_dim_type type, unsigned pos, const char *s)
760 int i;
762 map = isl_map_cow(map);
763 if (!map)
764 return NULL;
766 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
767 if (!map->dim)
768 goto error;
770 for (i = 0; i < map->n; ++i) {
771 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
772 if (!map->p[i])
773 goto error;
776 return map;
777 error:
778 isl_map_free(map);
779 return NULL;
782 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
783 __isl_take isl_basic_set *bset,
784 enum isl_dim_type type, unsigned pos, const char *s)
786 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
787 type, pos, s));
790 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
791 enum isl_dim_type type, unsigned pos, const char *s)
793 return set_from_map(isl_map_set_dim_name(set_to_map(set),
794 type, pos, s));
797 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
798 enum isl_dim_type type, unsigned pos)
800 if (!bmap)
801 return isl_bool_error;
802 return isl_space_has_dim_id(bmap->dim, type, pos);
805 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
806 enum isl_dim_type type, unsigned pos)
808 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
811 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
812 enum isl_dim_type type, unsigned pos)
814 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
817 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
818 enum isl_dim_type type, unsigned pos)
820 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
823 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
824 enum isl_dim_type type, unsigned pos)
826 return isl_map_has_dim_id(set, type, pos);
829 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
830 enum isl_dim_type type, unsigned pos)
832 return isl_map_get_dim_id(set, type, pos);
835 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
836 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
838 map = isl_map_cow(map);
839 if (!map)
840 goto error;
842 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
844 return isl_map_reset_space(map, isl_space_copy(map->dim));
845 error:
846 isl_id_free(id);
847 return NULL;
850 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
851 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
853 return isl_map_set_dim_id(set, type, pos, id);
856 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
857 __isl_keep isl_id *id)
859 if (!map)
860 return -1;
861 return isl_space_find_dim_by_id(map->dim, type, id);
864 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
865 __isl_keep isl_id *id)
867 return isl_map_find_dim_by_id(set, type, id);
870 /* Return the position of the dimension of the given type and name
871 * in "bmap".
872 * Return -1 if no such dimension can be found.
874 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
875 enum isl_dim_type type, const char *name)
877 if (!bmap)
878 return -1;
879 return isl_space_find_dim_by_name(bmap->dim, type, name);
882 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
883 const char *name)
885 if (!map)
886 return -1;
887 return isl_space_find_dim_by_name(map->dim, type, name);
890 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
891 const char *name)
893 return isl_map_find_dim_by_name(set, type, name);
896 /* Check whether equality i of bset is a pure stride constraint
897 * on a single dimension, i.e., of the form
899 * v = k e
901 * with k a constant and e an existentially quantified variable.
903 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
905 isl_size nparam;
906 isl_size d;
907 isl_size n_div;
908 int pos1;
909 int pos2;
911 nparam = isl_basic_set_dim(bset, isl_dim_param);
912 d = isl_basic_set_dim(bset, isl_dim_set);
913 n_div = isl_basic_set_dim(bset, isl_dim_div);
914 if (nparam < 0 || d < 0 || n_div < 0)
915 return isl_bool_error;
917 if (!isl_int_is_zero(bset->eq[i][0]))
918 return isl_bool_false;
920 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
921 return isl_bool_false;
922 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
923 if (pos1 == -1)
924 return isl_bool_false;
925 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
926 d - pos1 - 1) != -1)
927 return isl_bool_false;
929 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
930 if (pos2 == -1)
931 return isl_bool_false;
932 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
933 n_div - pos2 - 1) != -1)
934 return isl_bool_false;
935 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
936 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
937 return isl_bool_false;
939 return isl_bool_true;
942 /* Reset the user pointer on all identifiers of parameters and tuples
943 * of the space of "map".
945 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
947 isl_space *space;
949 space = isl_map_get_space(map);
950 space = isl_space_reset_user(space);
951 map = isl_map_reset_space(map, space);
953 return map;
956 /* Reset the user pointer on all identifiers of parameters and tuples
957 * of the space of "set".
959 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
961 return isl_map_reset_user(set);
964 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
966 if (!bmap)
967 return isl_bool_error;
968 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
971 /* Has "map" been marked as a rational map?
972 * In particular, have all basic maps in "map" been marked this way?
973 * An empty map is not considered to be rational.
974 * Maps where only some of the basic maps are marked rational
975 * are not allowed.
977 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
979 int i;
980 isl_bool rational;
982 if (!map)
983 return isl_bool_error;
984 if (map->n == 0)
985 return isl_bool_false;
986 rational = isl_basic_map_is_rational(map->p[0]);
987 if (rational < 0)
988 return rational;
989 for (i = 1; i < map->n; ++i) {
990 isl_bool rational_i;
992 rational_i = isl_basic_map_is_rational(map->p[i]);
993 if (rational_i < 0)
994 return rational_i;
995 if (rational != rational_i)
996 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
997 "mixed rational and integer basic maps "
998 "not supported", return isl_bool_error);
1001 return rational;
1004 /* Has "set" been marked as a rational set?
1005 * In particular, have all basic set in "set" been marked this way?
1006 * An empty set is not considered to be rational.
1007 * Sets where only some of the basic sets are marked rational
1008 * are not allowed.
1010 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1012 return isl_map_is_rational(set);
1015 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1017 return isl_basic_map_is_rational(bset);
1020 /* Does "bmap" contain any rational points?
1022 * If "bmap" has an equality for each dimension, equating the dimension
1023 * to an integer constant, then it has no rational points, even if it
1024 * is marked as rational.
1026 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1028 isl_bool has_rational = isl_bool_true;
1029 isl_size total;
1031 if (!bmap)
1032 return isl_bool_error;
1033 if (isl_basic_map_plain_is_empty(bmap))
1034 return isl_bool_false;
1035 if (!isl_basic_map_is_rational(bmap))
1036 return isl_bool_false;
1037 bmap = isl_basic_map_copy(bmap);
1038 bmap = isl_basic_map_implicit_equalities(bmap);
1039 total = isl_basic_map_dim(bmap, isl_dim_all);
1040 if (total < 0)
1041 return isl_bool_error;
1042 if (bmap->n_eq == total) {
1043 int i, j;
1044 for (i = 0; i < bmap->n_eq; ++i) {
1045 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1046 if (j < 0)
1047 break;
1048 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1049 !isl_int_is_negone(bmap->eq[i][1 + j]))
1050 break;
1051 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1052 total - j - 1);
1053 if (j >= 0)
1054 break;
1056 if (i == bmap->n_eq)
1057 has_rational = isl_bool_false;
1059 isl_basic_map_free(bmap);
1061 return has_rational;
1064 /* Does "map" contain any rational points?
1066 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1068 int i;
1069 isl_bool has_rational;
1071 if (!map)
1072 return isl_bool_error;
1073 for (i = 0; i < map->n; ++i) {
1074 has_rational = isl_basic_map_has_rational(map->p[i]);
1075 if (has_rational < 0 || has_rational)
1076 return has_rational;
1078 return isl_bool_false;
1081 /* Does "set" contain any rational points?
1083 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1085 return isl_map_has_rational(set);
1088 /* Is this basic set a parameter domain?
1090 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1092 if (!bset)
1093 return isl_bool_error;
1094 return isl_space_is_params(bset->dim);
1097 /* Is this set a parameter domain?
1099 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1101 if (!set)
1102 return isl_bool_error;
1103 return isl_space_is_params(set->dim);
1106 /* Is this map actually a parameter domain?
1107 * Users should never call this function. Outside of isl,
1108 * a map can never be a parameter domain.
1110 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1112 if (!map)
1113 return isl_bool_error;
1114 return isl_space_is_params(map->dim);
1117 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1118 __isl_take isl_basic_map *bmap, unsigned extra,
1119 unsigned n_eq, unsigned n_ineq)
1121 int i;
1122 isl_space *space = isl_basic_map_peek_space(bmap);
1123 isl_size n_var = isl_space_dim(space, isl_dim_all);
1124 size_t row_size = 1 + n_var + extra;
1126 bmap->ctx = ctx;
1127 isl_ctx_ref(ctx);
1129 if (n_var < 0)
1130 return isl_basic_map_free(bmap);
1132 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1133 if (isl_blk_is_error(bmap->block))
1134 goto error;
1136 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1137 if ((n_ineq + n_eq) && !bmap->ineq)
1138 goto error;
1140 if (extra == 0) {
1141 bmap->block2 = isl_blk_empty();
1142 bmap->div = NULL;
1143 } else {
1144 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1145 if (isl_blk_is_error(bmap->block2))
1146 goto error;
1148 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1149 if (!bmap->div)
1150 goto error;
1153 for (i = 0; i < n_ineq + n_eq; ++i)
1154 bmap->ineq[i] = bmap->block.data + i * row_size;
1156 for (i = 0; i < extra; ++i)
1157 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1159 bmap->ref = 1;
1160 bmap->flags = 0;
1161 bmap->c_size = n_eq + n_ineq;
1162 bmap->eq = bmap->ineq + n_ineq;
1163 bmap->extra = extra;
1164 bmap->n_eq = 0;
1165 bmap->n_ineq = 0;
1166 bmap->n_div = 0;
1167 bmap->sample = NULL;
1169 return bmap;
1170 error:
1171 isl_basic_map_free(bmap);
1172 return NULL;
1175 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1176 unsigned nparam, unsigned dim, unsigned extra,
1177 unsigned n_eq, unsigned n_ineq)
1179 struct isl_basic_map *bmap;
1180 isl_space *space;
1182 space = isl_space_set_alloc(ctx, nparam, dim);
1183 if (!space)
1184 return NULL;
1186 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1187 return bset_from_bmap(bmap);
1190 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1191 unsigned extra, unsigned n_eq, unsigned n_ineq)
1193 struct isl_basic_map *bmap;
1194 if (!dim)
1195 return NULL;
1196 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1197 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1198 return bset_from_bmap(bmap);
1199 error:
1200 isl_space_free(dim);
1201 return NULL;
1204 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1205 unsigned extra, unsigned n_eq, unsigned n_ineq)
1207 struct isl_basic_map *bmap;
1209 if (!space)
1210 return NULL;
1211 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1212 if (!bmap)
1213 goto error;
1214 bmap->dim = space;
1216 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1217 error:
1218 isl_space_free(space);
1219 return NULL;
1222 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1223 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1224 unsigned n_eq, unsigned n_ineq)
1226 struct isl_basic_map *bmap;
1227 isl_space *dim;
1229 dim = isl_space_alloc(ctx, nparam, in, out);
1230 if (!dim)
1231 return NULL;
1233 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1234 return bmap;
1237 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1238 __isl_keep isl_basic_map *src)
1240 int i;
1241 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1243 if (!dst || total < 0)
1244 return isl_basic_map_free(dst);
1246 for (i = 0; i < src->n_eq; ++i) {
1247 int j = isl_basic_map_alloc_equality(dst);
1248 if (j < 0)
1249 return isl_basic_map_free(dst);
1250 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1253 for (i = 0; i < src->n_ineq; ++i) {
1254 int j = isl_basic_map_alloc_inequality(dst);
1255 if (j < 0)
1256 return isl_basic_map_free(dst);
1257 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1260 for (i = 0; i < src->n_div; ++i) {
1261 int j = isl_basic_map_alloc_div(dst);
1262 if (j < 0)
1263 return isl_basic_map_free(dst);
1264 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1266 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1267 return dst;
1270 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1272 struct isl_basic_map *dup;
1274 if (!bmap)
1275 return NULL;
1276 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1277 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1278 dup = dup_constraints(dup, bmap);
1279 if (!dup)
1280 return NULL;
1281 dup->flags = bmap->flags;
1282 dup->sample = isl_vec_copy(bmap->sample);
1283 return dup;
1286 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1288 struct isl_basic_map *dup;
1290 dup = isl_basic_map_dup(bset_to_bmap(bset));
1291 return bset_from_bmap(dup);
1294 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1296 if (!bset)
1297 return NULL;
1299 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1300 bset->ref++;
1301 return bset;
1303 return isl_basic_set_dup(bset);
1306 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1308 if (!set)
1309 return NULL;
1311 set->ref++;
1312 return set;
1315 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1317 if (!bmap)
1318 return NULL;
1320 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1321 bmap->ref++;
1322 return bmap;
1324 bmap = isl_basic_map_dup(bmap);
1325 if (bmap)
1326 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1327 return bmap;
1330 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1332 if (!map)
1333 return NULL;
1335 map->ref++;
1336 return map;
1339 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1341 if (!bmap)
1342 return NULL;
1344 if (--bmap->ref > 0)
1345 return NULL;
1347 isl_ctx_deref(bmap->ctx);
1348 free(bmap->div);
1349 isl_blk_free(bmap->ctx, bmap->block2);
1350 free(bmap->ineq);
1351 isl_blk_free(bmap->ctx, bmap->block);
1352 isl_vec_free(bmap->sample);
1353 isl_space_free(bmap->dim);
1354 free(bmap);
1356 return NULL;
1359 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1361 return isl_basic_map_free(bset_to_bmap(bset));
1364 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1366 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1369 /* Check that "bset" does not involve any parameters.
1371 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1373 isl_size nparam;
1375 nparam = isl_basic_set_dim(bset, isl_dim_param);
1376 if (nparam < 0)
1377 return isl_stat_error;
1378 if (nparam != 0)
1379 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1380 "basic set should not have any parameters",
1381 return isl_stat_error);
1382 return isl_stat_ok;
1385 /* Check that "bset" does not involve any local variables.
1387 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1389 isl_size n_div;
1391 n_div = isl_basic_set_dim(bset, isl_dim_div);
1392 if (n_div < 0)
1393 return isl_stat_error;
1394 if (n_div != 0)
1395 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1396 "basic set should not have any local variables",
1397 return isl_stat_error);
1398 return isl_stat_ok;
1401 /* Check that "map" has only named parameters, reporting an error
1402 * if it does not.
1404 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1406 return isl_space_check_named_params(isl_map_peek_space(map));
1409 /* Check that "bmap" has only named parameters, reporting an error
1410 * if it does not.
1412 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1414 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1417 /* Check that "bmap1" and "bmap2" have the same parameters,
1418 * reporting an error if they do not.
1420 static isl_stat isl_basic_map_check_equal_params(
1421 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1423 isl_bool match;
1425 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1426 if (match < 0)
1427 return isl_stat_error;
1428 if (!match)
1429 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1430 "parameters don't match", return isl_stat_error);
1431 return isl_stat_ok;
1434 __isl_give isl_map *isl_map_align_params_map_map_and(
1435 __isl_take isl_map *map1, __isl_take isl_map *map2,
1436 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1437 __isl_take isl_map *map2))
1439 if (!map1 || !map2)
1440 goto error;
1441 if (isl_map_has_equal_params(map1, map2))
1442 return fn(map1, map2);
1443 if (isl_map_check_named_params(map1) < 0)
1444 goto error;
1445 if (isl_map_check_named_params(map2) < 0)
1446 goto error;
1447 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1448 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1449 return fn(map1, map2);
1450 error:
1451 isl_map_free(map1);
1452 isl_map_free(map2);
1453 return NULL;
1456 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1457 __isl_keep isl_map *map2,
1458 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1460 isl_bool r;
1462 if (!map1 || !map2)
1463 return isl_bool_error;
1464 if (isl_map_has_equal_params(map1, map2))
1465 return fn(map1, map2);
1466 if (isl_map_check_named_params(map1) < 0)
1467 return isl_bool_error;
1468 if (isl_map_check_named_params(map2) < 0)
1469 return isl_bool_error;
1470 map1 = isl_map_copy(map1);
1471 map2 = isl_map_copy(map2);
1472 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1473 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1474 r = fn(map1, map2);
1475 isl_map_free(map1);
1476 isl_map_free(map2);
1477 return r;
1480 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1482 isl_size total;
1483 struct isl_ctx *ctx;
1485 total = isl_basic_map_dim(bmap, isl_dim_all);
1486 if (total < 0)
1487 return -1;
1488 ctx = bmap->ctx;
1489 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1490 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1491 return -1);
1492 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1493 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1494 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1495 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1496 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1497 isl_int *t;
1498 int j = isl_basic_map_alloc_inequality(bmap);
1499 if (j < 0)
1500 return -1;
1501 t = bmap->ineq[j];
1502 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1503 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1504 bmap->eq[-1] = t;
1505 bmap->n_eq++;
1506 bmap->n_ineq--;
1507 bmap->eq--;
1508 return 0;
1510 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1511 bmap->extra - bmap->n_div);
1512 return bmap->n_eq++;
1515 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1517 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1520 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1522 if (!bmap)
1523 return -1;
1524 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1525 bmap->n_eq -= n;
1526 return 0;
1529 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1531 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1534 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1536 isl_int *t;
1537 if (!bmap)
1538 return -1;
1539 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1541 if (pos != bmap->n_eq - 1) {
1542 t = bmap->eq[pos];
1543 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1544 bmap->eq[bmap->n_eq - 1] = t;
1546 bmap->n_eq--;
1547 return 0;
1550 /* Turn inequality "pos" of "bmap" into an equality.
1552 * In particular, we move the inequality in front of the equalities
1553 * and move the last inequality in the position of the moved inequality.
1554 * Note that isl_tab_make_equalities_explicit depends on this particular
1555 * change in the ordering of the constraints.
1557 void isl_basic_map_inequality_to_equality(
1558 struct isl_basic_map *bmap, unsigned pos)
1560 isl_int *t;
1562 t = bmap->ineq[pos];
1563 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1564 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1565 bmap->eq[-1] = t;
1566 bmap->n_eq++;
1567 bmap->n_ineq--;
1568 bmap->eq--;
1569 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1570 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1571 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1572 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1575 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1577 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1580 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1582 isl_size total;
1583 struct isl_ctx *ctx;
1585 total = isl_basic_map_dim(bmap, isl_dim_all);
1586 if (total < 0)
1587 return -1;
1588 ctx = bmap->ctx;
1589 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1590 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1591 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1592 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1593 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1594 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1595 bmap->extra - bmap->n_div);
1596 return bmap->n_ineq++;
1599 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1601 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1604 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1606 if (!bmap)
1607 return -1;
1608 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1609 bmap->n_ineq -= n;
1610 return 0;
1613 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1615 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1618 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1620 isl_int *t;
1621 if (!bmap)
1622 return -1;
1623 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1625 if (pos != bmap->n_ineq - 1) {
1626 t = bmap->ineq[pos];
1627 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1628 bmap->ineq[bmap->n_ineq - 1] = t;
1629 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1631 bmap->n_ineq--;
1632 return 0;
1635 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1637 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1640 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1641 isl_int *eq)
1643 isl_bool empty;
1644 isl_size total;
1645 int k;
1647 empty = isl_basic_map_plain_is_empty(bmap);
1648 if (empty < 0)
1649 return isl_basic_map_free(bmap);
1650 if (empty)
1651 return bmap;
1653 bmap = isl_basic_map_cow(bmap);
1654 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1655 total = isl_basic_map_dim(bmap, isl_dim_all);
1656 if (total < 0)
1657 return isl_basic_map_free(bmap);
1658 k = isl_basic_map_alloc_equality(bmap);
1659 if (k < 0)
1660 goto error;
1661 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1662 return bmap;
1663 error:
1664 isl_basic_map_free(bmap);
1665 return NULL;
1668 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1669 isl_int *eq)
1671 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1674 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1675 isl_int *ineq)
1677 isl_size total;
1678 int k;
1680 bmap = isl_basic_map_cow(bmap);
1681 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1682 total = isl_basic_map_dim(bmap, isl_dim_all);
1683 if (total < 0)
1684 return isl_basic_map_free(bmap);
1685 k = isl_basic_map_alloc_inequality(bmap);
1686 if (k < 0)
1687 goto error;
1688 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1689 return bmap;
1690 error:
1691 isl_basic_map_free(bmap);
1692 return NULL;
1695 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1696 isl_int *ineq)
1698 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1701 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1703 isl_size total;
1705 total = isl_basic_map_dim(bmap, isl_dim_all);
1706 if (total < 0)
1707 return -1;
1708 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1709 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1710 bmap->extra - bmap->n_div);
1711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1712 return bmap->n_div++;
1715 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1717 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1720 #undef TYPE
1721 #define TYPE isl_basic_map
1722 #include "check_type_range_templ.c"
1724 /* Check that there are "n" dimensions of type "type" starting at "first"
1725 * in "bset".
1727 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1728 enum isl_dim_type type, unsigned first, unsigned n)
1730 return isl_basic_map_check_range(bset_to_bmap(bset),
1731 type, first, n);
1734 /* Insert an extra integer division, prescribed by "div", to "bmap"
1735 * at (integer division) position "pos".
1737 * The integer division is first added at the end and then moved
1738 * into the right position.
1740 __isl_give isl_basic_map *isl_basic_map_insert_div(
1741 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1743 int i, k;
1744 isl_size total;
1746 bmap = isl_basic_map_cow(bmap);
1747 total = isl_basic_map_dim(bmap, isl_dim_all);
1748 if (total < 0 || !div)
1749 return isl_basic_map_free(bmap);
1751 if (div->size != 1 + 1 + total)
1752 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1753 "unexpected size", return isl_basic_map_free(bmap));
1754 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1755 return isl_basic_map_free(bmap);
1757 bmap = isl_basic_map_extend_space(bmap,
1758 isl_basic_map_get_space(bmap), 1, 0, 2);
1759 k = isl_basic_map_alloc_div(bmap);
1760 if (k < 0)
1761 return isl_basic_map_free(bmap);
1762 isl_seq_cpy(bmap->div[k], div->el, div->size);
1763 isl_int_set_si(bmap->div[k][div->size], 0);
1765 for (i = k; i > pos; --i)
1766 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1768 return bmap;
1771 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1773 if (!bmap)
1774 return isl_stat_error;
1775 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1776 bmap->n_div -= n;
1777 return isl_stat_ok;
1780 static __isl_give isl_basic_map *add_constraints(
1781 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1782 unsigned i_pos, unsigned o_pos)
1784 isl_size total, n_param, n_in, n_out, n_div;
1785 unsigned o_in, o_out;
1786 isl_ctx *ctx;
1787 isl_space *space;
1788 struct isl_dim_map *dim_map;
1790 space = isl_basic_map_peek_space(bmap2);
1791 if (!bmap1 || !space)
1792 goto error;
1794 total = isl_basic_map_dim(bmap1, isl_dim_all);
1795 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1796 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1797 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1798 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1799 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1800 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1801 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1802 goto error;
1803 ctx = isl_basic_map_get_ctx(bmap1);
1804 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1805 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1806 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1807 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1808 isl_dim_map_div(dim_map, bmap2, total);
1810 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1811 error:
1812 isl_basic_map_free(bmap1);
1813 isl_basic_map_free(bmap2);
1814 return NULL;
1817 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1818 struct isl_basic_set *bset2, unsigned pos)
1820 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1821 bset_to_bmap(bset2), 0, pos));
1824 __isl_give isl_basic_map *isl_basic_map_extend_space(
1825 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1826 unsigned extra, unsigned n_eq, unsigned n_ineq)
1828 struct isl_basic_map *ext;
1829 unsigned flags;
1830 int dims_ok;
1832 if (!space)
1833 goto error;
1835 if (!base)
1836 goto error;
1838 dims_ok = isl_space_is_equal(base->dim, space) &&
1839 base->extra >= base->n_div + extra;
1841 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1842 room_for_ineq(base, n_ineq)) {
1843 isl_space_free(space);
1844 return base;
1847 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1848 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1849 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1850 extra += base->extra;
1851 n_eq += base->n_eq;
1852 n_ineq += base->n_ineq;
1854 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1855 space = NULL;
1856 if (!ext)
1857 goto error;
1859 if (dims_ok)
1860 ext->sample = isl_vec_copy(base->sample);
1861 flags = base->flags;
1862 ext = add_constraints(ext, base, 0, 0);
1863 if (ext) {
1864 ext->flags = flags;
1865 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1868 return ext;
1870 error:
1871 isl_space_free(space);
1872 isl_basic_map_free(base);
1873 return NULL;
1876 __isl_give isl_basic_set *isl_basic_set_extend_space(
1877 __isl_take isl_basic_set *base,
1878 __isl_take isl_space *dim, unsigned extra,
1879 unsigned n_eq, unsigned n_ineq)
1881 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1882 dim, extra, n_eq, n_ineq));
1885 struct isl_basic_map *isl_basic_map_extend_constraints(
1886 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1888 if (!base)
1889 return NULL;
1890 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1891 0, n_eq, n_ineq);
1894 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1895 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1896 unsigned n_eq, unsigned n_ineq)
1898 struct isl_basic_map *bmap;
1899 isl_space *dim;
1901 if (!base)
1902 return NULL;
1903 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1904 if (!dim)
1905 goto error;
1907 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1908 return bmap;
1909 error:
1910 isl_basic_map_free(base);
1911 return NULL;
1914 struct isl_basic_set *isl_basic_set_extend_constraints(
1915 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1917 isl_basic_map *bmap = bset_to_bmap(base);
1918 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1919 return bset_from_bmap(bmap);
1922 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1924 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1927 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1929 if (!bmap)
1930 return NULL;
1932 if (bmap->ref > 1) {
1933 bmap->ref--;
1934 bmap = isl_basic_map_dup(bmap);
1936 if (bmap) {
1937 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1938 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1940 return bmap;
1943 /* Clear all cached information in "map", either because it is about
1944 * to be modified or because it is being freed.
1945 * Always return the same pointer that is passed in.
1946 * This is needed for the use in isl_map_free.
1948 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1950 isl_basic_map_free(map->cached_simple_hull[0]);
1951 isl_basic_map_free(map->cached_simple_hull[1]);
1952 map->cached_simple_hull[0] = NULL;
1953 map->cached_simple_hull[1] = NULL;
1954 return map;
1957 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1959 return isl_map_cow(set);
1962 /* Return an isl_map that is equal to "map" and that has only
1963 * a single reference.
1965 * If the original input already has only one reference, then
1966 * simply return it, but clear all cached information, since
1967 * it may be rendered invalid by the operations that will be
1968 * performed on the result.
1970 * Otherwise, create a duplicate (without any cached information).
1972 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1974 if (!map)
1975 return NULL;
1977 if (map->ref == 1)
1978 return clear_caches(map);
1979 map->ref--;
1980 return isl_map_dup(map);
1983 static void swap_vars(struct isl_blk blk, isl_int *a,
1984 unsigned a_len, unsigned b_len)
1986 isl_seq_cpy(blk.data, a+a_len, b_len);
1987 isl_seq_cpy(blk.data+b_len, a, a_len);
1988 isl_seq_cpy(a, blk.data, b_len+a_len);
1991 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1992 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1994 int i;
1995 struct isl_blk blk;
1997 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1998 goto error;
2000 if (n1 == 0 || n2 == 0)
2001 return bmap;
2003 bmap = isl_basic_map_cow(bmap);
2004 if (!bmap)
2005 return NULL;
2007 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2008 if (isl_blk_is_error(blk))
2009 goto error;
2011 for (i = 0; i < bmap->n_eq; ++i)
2012 swap_vars(blk,
2013 bmap->eq[i] + pos, n1, n2);
2015 for (i = 0; i < bmap->n_ineq; ++i)
2016 swap_vars(blk,
2017 bmap->ineq[i] + pos, n1, n2);
2019 for (i = 0; i < bmap->n_div; ++i)
2020 swap_vars(blk,
2021 bmap->div[i]+1 + pos, n1, n2);
2023 isl_blk_free(bmap->ctx, blk);
2025 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2026 bmap = isl_basic_map_gauss(bmap, NULL);
2027 return isl_basic_map_finalize(bmap);
2028 error:
2029 isl_basic_map_free(bmap);
2030 return NULL;
2033 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2034 __isl_take isl_basic_map *bmap)
2036 int i = 0;
2037 isl_size total;
2039 total = isl_basic_map_dim(bmap, isl_dim_all);
2040 if (total < 0)
2041 return isl_basic_map_free(bmap);
2042 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2043 return isl_basic_map_free(bmap);
2044 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2045 if (bmap->n_eq > 0)
2046 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2047 else {
2048 i = isl_basic_map_alloc_equality(bmap);
2049 if (i < 0)
2050 goto error;
2052 isl_int_set_si(bmap->eq[i][0], 1);
2053 isl_seq_clr(bmap->eq[i]+1, total);
2054 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2055 isl_vec_free(bmap->sample);
2056 bmap->sample = NULL;
2057 return isl_basic_map_finalize(bmap);
2058 error:
2059 isl_basic_map_free(bmap);
2060 return NULL;
2063 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2064 __isl_take isl_basic_set *bset)
2066 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2069 __isl_give isl_basic_map *isl_basic_map_set_rational(
2070 __isl_take isl_basic_map *bmap)
2072 if (!bmap)
2073 return NULL;
2075 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2076 return bmap;
2078 bmap = isl_basic_map_cow(bmap);
2079 if (!bmap)
2080 return NULL;
2082 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2084 return isl_basic_map_finalize(bmap);
2087 __isl_give isl_basic_set *isl_basic_set_set_rational(
2088 __isl_take isl_basic_set *bset)
2090 return isl_basic_map_set_rational(bset);
2093 __isl_give isl_basic_set *isl_basic_set_set_integral(
2094 __isl_take isl_basic_set *bset)
2096 if (!bset)
2097 return NULL;
2099 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2100 return bset;
2102 bset = isl_basic_set_cow(bset);
2103 if (!bset)
2104 return NULL;
2106 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2108 return isl_basic_set_finalize(bset);
2111 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2113 int i;
2115 map = isl_map_cow(map);
2116 if (!map)
2117 return NULL;
2118 for (i = 0; i < map->n; ++i) {
2119 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2120 if (!map->p[i])
2121 goto error;
2123 return map;
2124 error:
2125 isl_map_free(map);
2126 return NULL;
2129 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2131 return isl_map_set_rational(set);
2134 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2135 * of "bmap").
2137 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2139 isl_int *t = bmap->div[a];
2140 bmap->div[a] = bmap->div[b];
2141 bmap->div[b] = t;
2144 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2145 * div definitions accordingly.
2147 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2148 int a, int b)
2150 int i;
2151 isl_size off;
2153 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2154 if (off < 0)
2155 return isl_basic_map_free(bmap);
2157 swap_div(bmap, a, b);
2159 for (i = 0; i < bmap->n_eq; ++i)
2160 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2162 for (i = 0; i < bmap->n_ineq; ++i)
2163 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2165 for (i = 0; i < bmap->n_div; ++i)
2166 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2167 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2169 return bmap;
2172 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2174 isl_seq_cpy(c, c + n, rem);
2175 isl_seq_clr(c + rem, n);
2178 /* Drop n dimensions starting at first.
2180 * In principle, this frees up some extra variables as the number
2181 * of columns remains constant, but we would have to extend
2182 * the div array too as the number of rows in this array is assumed
2183 * to be equal to extra.
2185 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2186 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2188 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2191 /* Move "n" divs starting at "first" to the end of the list of divs.
2193 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2194 unsigned first, unsigned n)
2196 isl_int **div;
2197 int i;
2199 if (first + n == bmap->n_div)
2200 return bmap;
2202 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2203 if (!div)
2204 goto error;
2205 for (i = 0; i < n; ++i)
2206 div[i] = bmap->div[first + i];
2207 for (i = 0; i < bmap->n_div - first - n; ++i)
2208 bmap->div[first + i] = bmap->div[first + n + i];
2209 for (i = 0; i < n; ++i)
2210 bmap->div[bmap->n_div - n + i] = div[i];
2211 free(div);
2212 return bmap;
2213 error:
2214 isl_basic_map_free(bmap);
2215 return NULL;
2218 #undef TYPE
2219 #define TYPE isl_map
2220 static
2221 #include "check_type_range_templ.c"
2223 /* Check that there are "n" dimensions of type "type" starting at "first"
2224 * in "set".
2226 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2227 enum isl_dim_type type, unsigned first, unsigned n)
2229 return isl_map_check_range(set_to_map(set), type, first, n);
2232 /* Drop "n" dimensions of type "type" starting at "first".
2233 * Perform the core computation, without cowing or
2234 * simplifying and finalizing the result.
2236 * In principle, this frees up some extra variables as the number
2237 * of columns remains constant, but we would have to extend
2238 * the div array too as the number of rows in this array is assumed
2239 * to be equal to extra.
2241 __isl_give isl_basic_map *isl_basic_map_drop_core(
2242 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2243 unsigned first, unsigned n)
2245 int i;
2246 unsigned offset;
2247 unsigned left;
2248 isl_size total;
2250 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2251 return isl_basic_map_free(bmap);
2253 total = isl_basic_map_dim(bmap, isl_dim_all);
2254 if (total < 0)
2255 return isl_basic_map_free(bmap);
2257 offset = isl_basic_map_offset(bmap, type) + first;
2258 left = total - (offset - 1) - n;
2259 for (i = 0; i < bmap->n_eq; ++i)
2260 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2262 for (i = 0; i < bmap->n_ineq; ++i)
2263 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2265 for (i = 0; i < bmap->n_div; ++i)
2266 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2268 if (type == isl_dim_div) {
2269 bmap = move_divs_last(bmap, first, n);
2270 if (!bmap)
2271 return NULL;
2272 if (isl_basic_map_free_div(bmap, n) < 0)
2273 return isl_basic_map_free(bmap);
2274 } else
2275 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2276 if (!bmap->dim)
2277 return isl_basic_map_free(bmap);
2279 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2280 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2281 return bmap;
2284 /* Drop "n" dimensions of type "type" starting at "first".
2286 * In principle, this frees up some extra variables as the number
2287 * of columns remains constant, but we would have to extend
2288 * the div array too as the number of rows in this array is assumed
2289 * to be equal to extra.
2291 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2292 enum isl_dim_type type, unsigned first, unsigned n)
2294 if (!bmap)
2295 return NULL;
2296 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2297 return bmap;
2299 bmap = isl_basic_map_cow(bmap);
2300 if (!bmap)
2301 return NULL;
2303 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2305 bmap = isl_basic_map_simplify(bmap);
2306 return isl_basic_map_finalize(bmap);
2309 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2310 enum isl_dim_type type, unsigned first, unsigned n)
2312 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2313 type, first, n));
2316 /* No longer consider "map" to be normalized.
2318 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2320 if (!map)
2321 return NULL;
2322 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2323 return map;
2326 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2327 enum isl_dim_type type, unsigned first, unsigned n)
2329 int i;
2331 if (isl_map_check_range(map, type, first, n) < 0)
2332 return isl_map_free(map);
2334 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2335 return map;
2336 map = isl_map_cow(map);
2337 if (!map)
2338 goto error;
2339 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2340 if (!map->dim)
2341 goto error;
2343 for (i = 0; i < map->n; ++i) {
2344 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2345 if (!map->p[i])
2346 goto error;
2348 map = isl_map_unmark_normalized(map);
2350 return map;
2351 error:
2352 isl_map_free(map);
2353 return NULL;
2356 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2357 enum isl_dim_type type, unsigned first, unsigned n)
2359 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2362 /* Drop the integer division at position "div", which is assumed
2363 * not to appear in any of the constraints or
2364 * in any of the other integer divisions.
2366 * Since the integer division is redundant, there is no need to cow.
2368 __isl_give isl_basic_map *isl_basic_map_drop_div(
2369 __isl_take isl_basic_map *bmap, unsigned div)
2371 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2374 /* Eliminate the specified n dimensions starting at first from the
2375 * constraints, without removing the dimensions from the space.
2376 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2378 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2381 int i;
2383 if (n == 0)
2384 return map;
2386 if (isl_map_check_range(map, type, first, n) < 0)
2387 return isl_map_free(map);
2389 map = isl_map_cow(map);
2390 if (!map)
2391 return NULL;
2393 for (i = 0; i < map->n; ++i) {
2394 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2395 if (!map->p[i])
2396 goto error;
2398 return map;
2399 error:
2400 isl_map_free(map);
2401 return NULL;
2404 /* Eliminate the specified n dimensions starting at first from the
2405 * constraints, without removing the dimensions from the space.
2406 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2414 /* Eliminate the specified n dimensions starting at first from the
2415 * constraints, without removing the dimensions from the space.
2416 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2418 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2419 unsigned first, unsigned n)
2421 return isl_set_eliminate(set, isl_dim_set, first, n);
2424 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2425 __isl_take isl_basic_map *bmap)
2427 isl_size v_div;
2429 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2430 if (v_div < 0)
2431 return isl_basic_map_free(bmap);
2432 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2433 if (!bmap)
2434 return NULL;
2435 bmap->n_div = 0;
2436 return isl_basic_map_finalize(bmap);
2439 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2440 __isl_take isl_basic_set *bset)
2442 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2445 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2447 int i;
2449 if (!map)
2450 return NULL;
2451 if (map->n == 0)
2452 return map;
2454 map = isl_map_cow(map);
2455 if (!map)
2456 return NULL;
2458 for (i = 0; i < map->n; ++i) {
2459 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2460 if (!map->p[i])
2461 goto error;
2463 return map;
2464 error:
2465 isl_map_free(map);
2466 return NULL;
2469 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2471 return isl_map_remove_divs(set);
2474 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2475 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2476 unsigned first, unsigned n)
2478 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2479 return isl_basic_map_free(bmap);
2480 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2481 return bmap;
2482 bmap = isl_basic_map_eliminate_vars(bmap,
2483 isl_basic_map_offset(bmap, type) - 1 + first, n);
2484 if (!bmap)
2485 return bmap;
2486 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2487 return bmap;
2488 bmap = isl_basic_map_drop(bmap, type, first, n);
2489 return bmap;
2492 /* Return true if the definition of the given div (recursively) involves
2493 * any of the given variables.
2495 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2496 unsigned first, unsigned n)
2498 int i;
2499 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2501 if (isl_int_is_zero(bmap->div[div][0]))
2502 return isl_bool_false;
2503 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2504 return isl_bool_true;
2506 for (i = bmap->n_div - 1; i >= 0; --i) {
2507 isl_bool involves;
2509 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2510 continue;
2511 involves = div_involves_vars(bmap, i, first, n);
2512 if (involves < 0 || involves)
2513 return involves;
2516 return isl_bool_false;
2519 /* Try and add a lower and/or upper bound on "div" to "bmap"
2520 * based on inequality "i".
2521 * "total" is the total number of variables (excluding the divs).
2522 * "v" is a temporary object that can be used during the calculations.
2523 * If "lb" is set, then a lower bound should be constructed.
2524 * If "ub" is set, then an upper bound should be constructed.
2526 * The calling function has already checked that the inequality does not
2527 * reference "div", but we still need to check that the inequality is
2528 * of the right form. We'll consider the case where we want to construct
2529 * a lower bound. The construction of upper bounds is similar.
2531 * Let "div" be of the form
2533 * q = floor((a + f(x))/d)
2535 * We essentially check if constraint "i" is of the form
2537 * b + f(x) >= 0
2539 * so that we can use it to derive a lower bound on "div".
2540 * However, we allow a slightly more general form
2542 * b + g(x) >= 0
2544 * with the condition that the coefficients of g(x) - f(x) are all
2545 * divisible by d.
2546 * Rewriting this constraint as
2548 * 0 >= -b - g(x)
2550 * adding a + f(x) to both sides and dividing by d, we obtain
2552 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2554 * Taking the floor on both sides, we obtain
2556 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2558 * or
2560 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2562 * In the case of an upper bound, we construct the constraint
2564 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2567 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2568 __isl_take isl_basic_map *bmap, int div, int i,
2569 unsigned total, isl_int v, int lb, int ub)
2571 int j;
2573 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2574 if (lb) {
2575 isl_int_sub(v, bmap->ineq[i][1 + j],
2576 bmap->div[div][1 + 1 + j]);
2577 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2579 if (ub) {
2580 isl_int_add(v, bmap->ineq[i][1 + j],
2581 bmap->div[div][1 + 1 + j]);
2582 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2585 if (!lb && !ub)
2586 return bmap;
2588 bmap = isl_basic_map_cow(bmap);
2589 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2590 if (lb) {
2591 int k = isl_basic_map_alloc_inequality(bmap);
2592 if (k < 0)
2593 goto error;
2594 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2595 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2596 bmap->div[div][1 + j]);
2597 isl_int_cdiv_q(bmap->ineq[k][j],
2598 bmap->ineq[k][j], bmap->div[div][0]);
2600 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2602 if (ub) {
2603 int k = isl_basic_map_alloc_inequality(bmap);
2604 if (k < 0)
2605 goto error;
2606 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2607 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2608 bmap->div[div][1 + j]);
2609 isl_int_fdiv_q(bmap->ineq[k][j],
2610 bmap->ineq[k][j], bmap->div[div][0]);
2612 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2615 return bmap;
2616 error:
2617 isl_basic_map_free(bmap);
2618 return NULL;
2621 /* This function is called right before "div" is eliminated from "bmap"
2622 * using Fourier-Motzkin.
2623 * Look through the constraints of "bmap" for constraints on the argument
2624 * of the integer division and use them to construct constraints on the
2625 * integer division itself. These constraints can then be combined
2626 * during the Fourier-Motzkin elimination.
2627 * Note that it is only useful to introduce lower bounds on "div"
2628 * if "bmap" already contains upper bounds on "div" as the newly
2629 * introduce lower bounds can then be combined with the pre-existing
2630 * upper bounds. Similarly for upper bounds.
2631 * We therefore first check if "bmap" contains any lower and/or upper bounds
2632 * on "div".
2634 * It is interesting to note that the introduction of these constraints
2635 * can indeed lead to more accurate results, even when compared to
2636 * deriving constraints on the argument of "div" from constraints on "div".
2637 * Consider, for example, the set
2639 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2641 * The second constraint can be rewritten as
2643 * 2 * [(-i-2j+3)/4] + k >= 0
2645 * from which we can derive
2647 * -i - 2j + 3 >= -2k
2649 * or
2651 * i + 2j <= 3 + 2k
2653 * Combined with the first constraint, we obtain
2655 * -3 <= 3 + 2k or k >= -3
2657 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2658 * the first constraint, we obtain
2660 * [(i + 2j)/4] >= [-3/4] = -1
2662 * Combining this constraint with the second constraint, we obtain
2664 * k >= -2
2666 static __isl_give isl_basic_map *insert_bounds_on_div(
2667 __isl_take isl_basic_map *bmap, int div)
2669 int i;
2670 int check_lb, check_ub;
2671 isl_int v;
2672 isl_size v_div;
2674 if (!bmap)
2675 return NULL;
2677 if (isl_int_is_zero(bmap->div[div][0]))
2678 return bmap;
2680 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2681 if (v_div < 0)
2682 return isl_basic_map_free(bmap);
2684 check_lb = 0;
2685 check_ub = 0;
2686 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2687 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2688 if (s > 0)
2689 check_ub = 1;
2690 if (s < 0)
2691 check_lb = 1;
2694 if (!check_lb && !check_ub)
2695 return bmap;
2697 isl_int_init(v);
2699 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2700 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2701 continue;
2703 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2704 check_lb, check_ub);
2707 isl_int_clear(v);
2709 return bmap;
2712 /* Remove all divs (recursively) involving any of the given dimensions
2713 * in their definitions.
2715 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2716 __isl_take isl_basic_map *bmap,
2717 enum isl_dim_type type, unsigned first, unsigned n)
2719 int i;
2721 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2722 return isl_basic_map_free(bmap);
2723 first += isl_basic_map_offset(bmap, type);
2725 for (i = bmap->n_div - 1; i >= 0; --i) {
2726 isl_bool involves;
2728 involves = div_involves_vars(bmap, i, first, n);
2729 if (involves < 0)
2730 return isl_basic_map_free(bmap);
2731 if (!involves)
2732 continue;
2733 bmap = insert_bounds_on_div(bmap, i);
2734 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2735 if (!bmap)
2736 return NULL;
2737 i = bmap->n_div;
2740 return bmap;
2743 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2744 __isl_take isl_basic_set *bset,
2745 enum isl_dim_type type, unsigned first, unsigned n)
2747 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2750 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2751 enum isl_dim_type type, unsigned first, unsigned n)
2753 int i;
2755 if (!map)
2756 return NULL;
2757 if (map->n == 0)
2758 return map;
2760 map = isl_map_cow(map);
2761 if (!map)
2762 return NULL;
2764 for (i = 0; i < map->n; ++i) {
2765 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2766 type, first, n);
2767 if (!map->p[i])
2768 goto error;
2770 return map;
2771 error:
2772 isl_map_free(map);
2773 return NULL;
2776 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2777 enum isl_dim_type type, unsigned first, unsigned n)
2779 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2780 type, first, n));
2783 /* Does the description of "bmap" depend on the specified dimensions?
2784 * We also check whether the dimensions appear in any of the div definitions.
2785 * In principle there is no need for this check. If the dimensions appear
2786 * in a div definition, they also appear in the defining constraints of that
2787 * div.
2789 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2790 enum isl_dim_type type, unsigned first, unsigned n)
2792 int i;
2794 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2795 return isl_bool_error;
2797 first += isl_basic_map_offset(bmap, type);
2798 for (i = 0; i < bmap->n_eq; ++i)
2799 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2800 return isl_bool_true;
2801 for (i = 0; i < bmap->n_ineq; ++i)
2802 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2803 return isl_bool_true;
2804 for (i = 0; i < bmap->n_div; ++i) {
2805 if (isl_int_is_zero(bmap->div[i][0]))
2806 continue;
2807 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2808 return isl_bool_true;
2811 return isl_bool_false;
2814 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2815 enum isl_dim_type type, unsigned first, unsigned n)
2817 int i;
2819 if (isl_map_check_range(map, type, first, n) < 0)
2820 return isl_bool_error;
2822 for (i = 0; i < map->n; ++i) {
2823 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2824 type, first, n);
2825 if (involves < 0 || involves)
2826 return involves;
2829 return isl_bool_false;
2832 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2833 enum isl_dim_type type, unsigned first, unsigned n)
2835 return isl_basic_map_involves_dims(bset, type, first, n);
2838 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2839 enum isl_dim_type type, unsigned first, unsigned n)
2841 return isl_map_involves_dims(set, type, first, n);
2844 /* Drop all constraints in bmap that involve any of the dimensions
2845 * first to first+n-1.
2846 * This function only performs the actual removal of constraints.
2848 * This function should not call finalize since it is used by
2849 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2851 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2852 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2854 int i;
2856 if (n == 0)
2857 return bmap;
2859 bmap = isl_basic_map_cow(bmap);
2861 if (!bmap)
2862 return NULL;
2864 for (i = bmap->n_eq - 1; i >= 0; --i) {
2865 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2866 continue;
2867 isl_basic_map_drop_equality(bmap, i);
2870 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2871 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2872 continue;
2873 isl_basic_map_drop_inequality(bmap, i);
2876 return bmap;
2879 /* Drop all constraints in bset that involve any of the dimensions
2880 * first to first+n-1.
2881 * This function only performs the actual removal of constraints.
2883 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2884 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2886 return isl_basic_map_drop_constraints_involving(bset, first, n);
2889 /* Drop all constraints in bmap that do not involve any of the dimensions
2890 * first to first + n - 1 of the given type.
2892 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2893 __isl_take isl_basic_map *bmap,
2894 enum isl_dim_type type, unsigned first, unsigned n)
2896 int i;
2898 if (n == 0) {
2899 isl_space *space = isl_basic_map_get_space(bmap);
2900 isl_basic_map_free(bmap);
2901 return isl_basic_map_universe(space);
2903 bmap = isl_basic_map_cow(bmap);
2904 if (!bmap)
2905 return NULL;
2907 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2908 return isl_basic_map_free(bmap);
2910 first += isl_basic_map_offset(bmap, type) - 1;
2912 for (i = bmap->n_eq - 1; i >= 0; --i) {
2913 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2914 continue;
2915 isl_basic_map_drop_equality(bmap, i);
2918 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2919 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2920 continue;
2921 isl_basic_map_drop_inequality(bmap, i);
2924 bmap = isl_basic_map_add_known_div_constraints(bmap);
2925 return bmap;
2928 /* Drop all constraints in bset that do not involve any of the dimensions
2929 * first to first + n - 1 of the given type.
2931 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2932 __isl_take isl_basic_set *bset,
2933 enum isl_dim_type type, unsigned first, unsigned n)
2935 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2936 type, first, n);
2939 /* Drop all constraints in bmap that involve any of the dimensions
2940 * first to first + n - 1 of the given type.
2942 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2943 __isl_take isl_basic_map *bmap,
2944 enum isl_dim_type type, unsigned first, unsigned n)
2946 if (!bmap)
2947 return NULL;
2948 if (n == 0)
2949 return bmap;
2951 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2952 return isl_basic_map_free(bmap);
2954 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2955 first += isl_basic_map_offset(bmap, type) - 1;
2956 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2957 bmap = isl_basic_map_add_known_div_constraints(bmap);
2958 return bmap;
2961 /* Drop all constraints in bset that involve any of the dimensions
2962 * first to first + n - 1 of the given type.
2964 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2965 __isl_take isl_basic_set *bset,
2966 enum isl_dim_type type, unsigned first, unsigned n)
2968 return isl_basic_map_drop_constraints_involving_dims(bset,
2969 type, first, n);
2972 /* Drop constraints from "map" by applying "drop" to each basic map.
2974 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2975 enum isl_dim_type type, unsigned first, unsigned n,
2976 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2977 enum isl_dim_type type, unsigned first, unsigned n))
2979 int i;
2981 if (isl_map_check_range(map, type, first, n) < 0)
2982 return isl_map_free(map);
2984 map = isl_map_cow(map);
2985 if (!map)
2986 return NULL;
2988 for (i = 0; i < map->n; ++i) {
2989 map->p[i] = drop(map->p[i], type, first, n);
2990 if (!map->p[i])
2991 return isl_map_free(map);
2994 if (map->n > 1)
2995 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2997 return map;
3000 /* Drop all constraints in map that involve any of the dimensions
3001 * first to first + n - 1 of the given type.
3003 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3004 __isl_take isl_map *map,
3005 enum isl_dim_type type, unsigned first, unsigned n)
3007 if (n == 0)
3008 return map;
3009 return drop_constraints(map, type, first, n,
3010 &isl_basic_map_drop_constraints_involving_dims);
3013 /* Drop all constraints in "map" that do not involve any of the dimensions
3014 * first to first + n - 1 of the given type.
3016 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3017 __isl_take isl_map *map,
3018 enum isl_dim_type type, unsigned first, unsigned n)
3020 if (n == 0) {
3021 isl_space *space = isl_map_get_space(map);
3022 isl_map_free(map);
3023 return isl_map_universe(space);
3025 return drop_constraints(map, type, first, n,
3026 &isl_basic_map_drop_constraints_not_involving_dims);
3029 /* Drop all constraints in set that involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3032 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3033 __isl_take isl_set *set,
3034 enum isl_dim_type type, unsigned first, unsigned n)
3036 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3039 /* Drop all constraints in "set" that do not involve any of the dimensions
3040 * first to first + n - 1 of the given type.
3042 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3043 __isl_take isl_set *set,
3044 enum isl_dim_type type, unsigned first, unsigned n)
3046 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3049 /* Does local variable "div" of "bmap" have a complete explicit representation?
3050 * Having a complete explicit representation requires not only
3051 * an explicit representation, but also that all local variables
3052 * that appear in this explicit representation in turn have
3053 * a complete explicit representation.
3055 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3057 int i;
3058 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3059 isl_bool marked;
3061 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3062 if (marked < 0 || marked)
3063 return isl_bool_not(marked);
3065 for (i = bmap->n_div - 1; i >= 0; --i) {
3066 isl_bool known;
3068 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3069 continue;
3070 known = isl_basic_map_div_is_known(bmap, i);
3071 if (known < 0 || !known)
3072 return known;
3075 return isl_bool_true;
3078 /* Remove all divs that are unknown or defined in terms of unknown divs.
3080 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3081 __isl_take isl_basic_map *bmap)
3083 int i;
3085 if (!bmap)
3086 return NULL;
3088 for (i = bmap->n_div - 1; i >= 0; --i) {
3089 if (isl_basic_map_div_is_known(bmap, i))
3090 continue;
3091 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3092 if (!bmap)
3093 return NULL;
3094 i = bmap->n_div;
3097 return bmap;
3100 /* Remove all divs that are unknown or defined in terms of unknown divs.
3102 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3103 __isl_take isl_basic_set *bset)
3105 return isl_basic_map_remove_unknown_divs(bset);
3108 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3110 int i;
3112 if (!map)
3113 return NULL;
3114 if (map->n == 0)
3115 return map;
3117 map = isl_map_cow(map);
3118 if (!map)
3119 return NULL;
3121 for (i = 0; i < map->n; ++i) {
3122 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3123 if (!map->p[i])
3124 goto error;
3126 return map;
3127 error:
3128 isl_map_free(map);
3129 return NULL;
3132 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3134 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3137 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3138 __isl_take isl_basic_set *bset,
3139 enum isl_dim_type type, unsigned first, unsigned n)
3141 isl_basic_map *bmap = bset_to_bmap(bset);
3142 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3143 return bset_from_bmap(bmap);
3146 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3147 enum isl_dim_type type, unsigned first, unsigned n)
3149 int i;
3151 if (n == 0)
3152 return map;
3154 map = isl_map_cow(map);
3155 if (isl_map_check_range(map, type, first, n) < 0)
3156 return isl_map_free(map);
3158 for (i = 0; i < map->n; ++i) {
3159 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3160 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3161 if (!map->p[i])
3162 goto error;
3164 map = isl_map_drop(map, type, first, n);
3165 return map;
3166 error:
3167 isl_map_free(map);
3168 return NULL;
3171 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3172 enum isl_dim_type type, unsigned first, unsigned n)
3174 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3175 type, first, n));
3178 /* Project out n inputs starting at first using Fourier-Motzkin */
3179 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3180 unsigned first, unsigned n)
3182 return isl_map_remove_dims(map, isl_dim_in, first, n);
3185 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3186 FILE *out, int indent)
3188 isl_printer *p;
3190 if (!bset) {
3191 fprintf(out, "null basic set\n");
3192 return;
3195 fprintf(out, "%*s", indent, "");
3196 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3197 bset->ref, bset->dim->nparam, bset->dim->n_out,
3198 bset->extra, bset->flags);
3200 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3201 p = isl_printer_set_dump(p, 1);
3202 p = isl_printer_set_indent(p, indent);
3203 p = isl_printer_start_line(p);
3204 p = isl_printer_print_basic_set(p, bset);
3205 p = isl_printer_end_line(p);
3206 isl_printer_free(p);
3209 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3210 FILE *out, int indent)
3212 isl_printer *p;
3214 if (!bmap) {
3215 fprintf(out, "null basic map\n");
3216 return;
3219 fprintf(out, "%*s", indent, "");
3220 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3221 "flags: %x, n_name: %d\n",
3222 bmap->ref,
3223 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3224 bmap->extra, bmap->flags, bmap->dim->n_id);
3226 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3227 p = isl_printer_set_dump(p, 1);
3228 p = isl_printer_set_indent(p, indent);
3229 p = isl_printer_start_line(p);
3230 p = isl_printer_print_basic_map(p, bmap);
3231 p = isl_printer_end_line(p);
3232 isl_printer_free(p);
3235 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3236 unsigned pos)
3238 isl_size total;
3240 total = isl_basic_map_dim(bmap, isl_dim_all);
3241 if (total < 0)
3242 return isl_basic_map_free(bmap);
3243 if (pos >= bmap->n_ineq)
3244 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3245 "invalid position", return isl_basic_map_free(bmap));
3246 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3247 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3248 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3249 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3250 return bmap;
3253 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3254 unsigned flags)
3256 if (isl_space_check_is_set(space) < 0)
3257 goto error;
3258 return isl_map_alloc_space(space, n, flags);
3259 error:
3260 isl_space_free(space);
3261 return NULL;
3264 /* Make sure "map" has room for at least "n" more basic maps.
3266 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3268 int i;
3269 struct isl_map *grown = NULL;
3271 if (!map)
3272 return NULL;
3273 isl_assert(map->ctx, n >= 0, goto error);
3274 if (map->n + n <= map->size)
3275 return map;
3276 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3277 if (!grown)
3278 goto error;
3279 for (i = 0; i < map->n; ++i) {
3280 grown->p[i] = isl_basic_map_copy(map->p[i]);
3281 if (!grown->p[i])
3282 goto error;
3283 grown->n++;
3285 isl_map_free(map);
3286 return grown;
3287 error:
3288 isl_map_free(grown);
3289 isl_map_free(map);
3290 return NULL;
3293 /* Make sure "set" has room for at least "n" more basic sets.
3295 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3297 return set_from_map(isl_map_grow(set_to_map(set), n));
3300 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3302 return isl_map_from_basic_map(bset);
3305 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3307 struct isl_map *map;
3309 if (!bmap)
3310 return NULL;
3312 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3313 return isl_map_add_basic_map(map, bmap);
3316 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3317 __isl_take isl_basic_set *bset)
3319 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3320 bset_to_bmap(bset)));
3323 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3325 return isl_map_free(set);
3328 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3330 int i;
3332 if (!set) {
3333 fprintf(out, "null set\n");
3334 return;
3337 fprintf(out, "%*s", indent, "");
3338 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3339 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3340 set->flags);
3341 for (i = 0; i < set->n; ++i) {
3342 fprintf(out, "%*s", indent, "");
3343 fprintf(out, "basic set %d:\n", i);
3344 isl_basic_set_print_internal(set->p[i], out, indent+4);
3348 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3350 int i;
3352 if (!map) {
3353 fprintf(out, "null map\n");
3354 return;
3357 fprintf(out, "%*s", indent, "");
3358 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3359 "flags: %x, n_name: %d\n",
3360 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3361 map->dim->n_out, map->flags, map->dim->n_id);
3362 for (i = 0; i < map->n; ++i) {
3363 fprintf(out, "%*s", indent, "");
3364 fprintf(out, "basic map %d:\n", i);
3365 isl_basic_map_print_internal(map->p[i], out, indent+4);
3369 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3370 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3372 struct isl_basic_map *bmap_domain;
3373 isl_size dim;
3375 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3376 goto error;
3378 dim = isl_basic_set_dim(bset, isl_dim_set);
3379 if (dim < 0)
3380 goto error;
3381 if (dim != 0)
3382 isl_assert(bset->ctx,
3383 isl_basic_map_compatible_domain(bmap, bset), goto error);
3385 bmap = isl_basic_map_cow(bmap);
3386 if (!bmap)
3387 goto error;
3388 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3389 bset->n_div, bset->n_eq, bset->n_ineq);
3390 bmap_domain = isl_basic_map_from_domain(bset);
3391 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3393 bmap = isl_basic_map_simplify(bmap);
3394 return isl_basic_map_finalize(bmap);
3395 error:
3396 isl_basic_map_free(bmap);
3397 isl_basic_set_free(bset);
3398 return NULL;
3401 /* Check that the space of "bset" is the same as that of the range of "bmap".
3403 static isl_stat isl_basic_map_check_compatible_range(
3404 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3406 isl_bool ok;
3408 ok = isl_basic_map_compatible_range(bmap, bset);
3409 if (ok < 0)
3410 return isl_stat_error;
3411 if (!ok)
3412 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3413 "incompatible spaces", return isl_stat_error);
3415 return isl_stat_ok;
3418 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3419 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3421 struct isl_basic_map *bmap_range;
3422 isl_size dim;
3424 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3425 goto error;
3427 dim = isl_basic_set_dim(bset, isl_dim_set);
3428 if (dim < 0)
3429 goto error;
3430 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3431 goto error;
3433 if (isl_basic_set_plain_is_universe(bset)) {
3434 isl_basic_set_free(bset);
3435 return bmap;
3438 bmap = isl_basic_map_cow(bmap);
3439 if (!bmap)
3440 goto error;
3441 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3442 bset->n_div, bset->n_eq, bset->n_ineq);
3443 bmap_range = bset_to_bmap(bset);
3444 bmap = add_constraints(bmap, bmap_range, 0, 0);
3446 bmap = isl_basic_map_simplify(bmap);
3447 return isl_basic_map_finalize(bmap);
3448 error:
3449 isl_basic_map_free(bmap);
3450 isl_basic_set_free(bset);
3451 return NULL;
3454 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3455 __isl_keep isl_vec *vec)
3457 int i;
3458 isl_size total;
3459 isl_int s;
3461 total = isl_basic_map_dim(bmap, isl_dim_all);
3462 if (total < 0 || !vec)
3463 return isl_bool_error;
3465 if (1 + total != vec->size)
3466 return isl_bool_false;
3468 isl_int_init(s);
3470 for (i = 0; i < bmap->n_eq; ++i) {
3471 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3472 if (!isl_int_is_zero(s)) {
3473 isl_int_clear(s);
3474 return isl_bool_false;
3478 for (i = 0; i < bmap->n_ineq; ++i) {
3479 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3480 if (isl_int_is_neg(s)) {
3481 isl_int_clear(s);
3482 return isl_bool_false;
3486 isl_int_clear(s);
3488 return isl_bool_true;
3491 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3492 __isl_keep isl_vec *vec)
3494 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3497 __isl_give isl_basic_map *isl_basic_map_intersect(
3498 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3500 struct isl_vec *sample = NULL;
3501 isl_space *space1, *space2;
3502 isl_size dim1, dim2, nparam1, nparam2;
3504 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3505 goto error;
3506 space1 = isl_basic_map_peek_space(bmap1);
3507 space2 = isl_basic_map_peek_space(bmap2);
3508 dim1 = isl_space_dim(space1, isl_dim_all);
3509 dim2 = isl_space_dim(space2, isl_dim_all);
3510 nparam1 = isl_space_dim(space1, isl_dim_param);
3511 nparam2 = isl_space_dim(space2, isl_dim_param);
3512 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3513 goto error;
3514 if (dim1 == nparam1 && dim2 != nparam2)
3515 return isl_basic_map_intersect(bmap2, bmap1);
3517 if (dim2 != nparam2)
3518 isl_assert(bmap1->ctx,
3519 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3521 if (isl_basic_map_plain_is_empty(bmap1)) {
3522 isl_basic_map_free(bmap2);
3523 return bmap1;
3525 if (isl_basic_map_plain_is_empty(bmap2)) {
3526 isl_basic_map_free(bmap1);
3527 return bmap2;
3530 if (bmap1->sample &&
3531 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3532 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3533 sample = isl_vec_copy(bmap1->sample);
3534 else if (bmap2->sample &&
3535 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3536 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3537 sample = isl_vec_copy(bmap2->sample);
3539 bmap1 = isl_basic_map_cow(bmap1);
3540 if (!bmap1)
3541 goto error;
3542 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3543 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3544 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3546 if (!bmap1)
3547 isl_vec_free(sample);
3548 else if (sample) {
3549 isl_vec_free(bmap1->sample);
3550 bmap1->sample = sample;
3553 bmap1 = isl_basic_map_simplify(bmap1);
3554 return isl_basic_map_finalize(bmap1);
3555 error:
3556 if (sample)
3557 isl_vec_free(sample);
3558 isl_basic_map_free(bmap1);
3559 isl_basic_map_free(bmap2);
3560 return NULL;
3563 struct isl_basic_set *isl_basic_set_intersect(
3564 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3566 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3567 bset_to_bmap(bset2)));
3570 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3571 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3573 return isl_basic_set_intersect(bset1, bset2);
3576 /* Special case of isl_map_intersect, where both map1 and map2
3577 * are convex, without any divs and such that either map1 or map2
3578 * contains a single constraint. This constraint is then simply
3579 * added to the other map.
3581 static __isl_give isl_map *map_intersect_add_constraint(
3582 __isl_take isl_map *map1, __isl_take isl_map *map2)
3584 isl_assert(map1->ctx, map1->n == 1, goto error);
3585 isl_assert(map2->ctx, map1->n == 1, goto error);
3586 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3587 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3589 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3590 return isl_map_intersect(map2, map1);
3592 map1 = isl_map_cow(map1);
3593 if (!map1)
3594 goto error;
3595 if (isl_map_plain_is_empty(map1)) {
3596 isl_map_free(map2);
3597 return map1;
3599 if (map2->p[0]->n_eq == 1)
3600 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3601 else
3602 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3603 map2->p[0]->ineq[0]);
3605 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3606 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3607 if (!map1->p[0])
3608 goto error;
3610 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3611 isl_basic_map_free(map1->p[0]);
3612 map1->n = 0;
3615 isl_map_free(map2);
3617 map1 = isl_map_unmark_normalized(map1);
3618 return map1;
3619 error:
3620 isl_map_free(map1);
3621 isl_map_free(map2);
3622 return NULL;
3625 /* map2 may be either a parameter domain or a map living in the same
3626 * space as map1.
3628 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3629 __isl_take isl_map *map2)
3631 unsigned flags = 0;
3632 isl_bool equal;
3633 isl_map *result;
3634 int i, j;
3635 isl_size dim2, nparam2;
3637 if (!map1 || !map2)
3638 goto error;
3640 if ((isl_map_plain_is_empty(map1) ||
3641 isl_map_plain_is_universe(map2)) &&
3642 isl_space_is_equal(map1->dim, map2->dim)) {
3643 isl_map_free(map2);
3644 return map1;
3646 if ((isl_map_plain_is_empty(map2) ||
3647 isl_map_plain_is_universe(map1)) &&
3648 isl_space_is_equal(map1->dim, map2->dim)) {
3649 isl_map_free(map1);
3650 return map2;
3653 if (map1->n == 1 && map2->n == 1 &&
3654 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3655 isl_space_is_equal(map1->dim, map2->dim) &&
3656 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3657 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3658 return map_intersect_add_constraint(map1, map2);
3660 equal = isl_map_plain_is_equal(map1, map2);
3661 if (equal < 0)
3662 goto error;
3663 if (equal) {
3664 isl_map_free(map2);
3665 return map1;
3668 dim2 = isl_map_dim(map2, isl_dim_all);
3669 nparam2 = isl_map_dim(map2, isl_dim_param);
3670 if (dim2 < 0 || nparam2 < 0)
3671 goto error;
3672 if (dim2 != nparam2)
3673 isl_assert(map1->ctx,
3674 isl_space_is_equal(map1->dim, map2->dim), goto error);
3676 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3677 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3678 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3680 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3681 map1->n * map2->n, flags);
3682 if (!result)
3683 goto error;
3684 for (i = 0; i < map1->n; ++i)
3685 for (j = 0; j < map2->n; ++j) {
3686 struct isl_basic_map *part;
3687 part = isl_basic_map_intersect(
3688 isl_basic_map_copy(map1->p[i]),
3689 isl_basic_map_copy(map2->p[j]));
3690 if (isl_basic_map_is_empty(part) < 0)
3691 part = isl_basic_map_free(part);
3692 result = isl_map_add_basic_map(result, part);
3693 if (!result)
3694 goto error;
3696 isl_map_free(map1);
3697 isl_map_free(map2);
3698 return result;
3699 error:
3700 isl_map_free(map1);
3701 isl_map_free(map2);
3702 return NULL;
3705 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3706 __isl_take isl_map *map2)
3708 if (!map1 || !map2)
3709 goto error;
3710 if (!isl_space_is_equal(map1->dim, map2->dim))
3711 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3712 "spaces don't match", goto error);
3713 return map_intersect_internal(map1, map2);
3714 error:
3715 isl_map_free(map1);
3716 isl_map_free(map2);
3717 return NULL;
3720 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3721 __isl_take isl_map *map2)
3723 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3726 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3728 return set_from_map(isl_map_intersect(set_to_map(set1),
3729 set_to_map(set2)));
3732 /* map_intersect_internal accepts intersections
3733 * with parameter domains, so we can just call that function.
3735 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3736 __isl_take isl_set *params)
3738 return map_intersect_internal(map, params);
3741 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3742 __isl_take isl_map *map2)
3744 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3747 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3748 __isl_take isl_set *params)
3750 return isl_map_intersect_params(set, params);
3753 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3755 isl_space *space;
3756 unsigned pos;
3757 isl_size n1, n2;
3759 if (!bmap)
3760 return NULL;
3761 bmap = isl_basic_map_cow(bmap);
3762 if (!bmap)
3763 return NULL;
3764 space = isl_space_reverse(isl_space_copy(bmap->dim));
3765 pos = isl_basic_map_offset(bmap, isl_dim_in);
3766 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3767 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3768 if (n1 < 0 || n2 < 0)
3769 bmap = isl_basic_map_free(bmap);
3770 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3771 return isl_basic_map_reset_space(bmap, space);
3774 static __isl_give isl_basic_map *basic_map_space_reset(
3775 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3777 isl_space *space;
3779 if (!bmap)
3780 return NULL;
3781 if (!isl_space_is_named_or_nested(bmap->dim, type))
3782 return bmap;
3784 space = isl_basic_map_get_space(bmap);
3785 space = isl_space_reset(space, type);
3786 bmap = isl_basic_map_reset_space(bmap, space);
3787 return bmap;
3790 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3791 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3792 unsigned pos, unsigned n)
3794 isl_bool rational, is_empty;
3795 isl_space *res_space;
3796 struct isl_basic_map *res;
3797 struct isl_dim_map *dim_map;
3798 isl_size total;
3799 unsigned off;
3800 enum isl_dim_type t;
3802 if (n == 0)
3803 return basic_map_space_reset(bmap, type);
3805 is_empty = isl_basic_map_plain_is_empty(bmap);
3806 total = isl_basic_map_dim(bmap, isl_dim_all);
3807 if (is_empty < 0 || total < 0)
3808 return isl_basic_map_free(bmap);
3809 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3810 type, pos, n);
3811 if (!res_space)
3812 return isl_basic_map_free(bmap);
3813 if (is_empty) {
3814 isl_basic_map_free(bmap);
3815 return isl_basic_map_empty(res_space);
3818 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
3819 off = 0;
3820 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3821 isl_size dim;
3823 if (t != type) {
3824 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3825 } else {
3826 isl_size size = isl_basic_map_dim(bmap, t);
3827 if (size < 0)
3828 dim_map = isl_dim_map_free(dim_map);
3829 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3830 0, pos, off);
3831 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3832 pos, size - pos, off + pos + n);
3834 dim = isl_space_dim(res_space, t);
3835 if (dim < 0)
3836 dim_map = isl_dim_map_free(dim_map);
3837 off += dim;
3839 isl_dim_map_div(dim_map, bmap, off);
3841 res = isl_basic_map_alloc_space(res_space,
3842 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3843 rational = isl_basic_map_is_rational(bmap);
3844 if (rational < 0)
3845 res = isl_basic_map_free(res);
3846 if (rational)
3847 res = isl_basic_map_set_rational(res);
3848 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3849 return isl_basic_map_finalize(res);
3852 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3853 __isl_take isl_basic_set *bset,
3854 enum isl_dim_type type, unsigned pos, unsigned n)
3856 return isl_basic_map_insert_dims(bset, type, pos, n);
3859 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3860 enum isl_dim_type type, unsigned n)
3862 isl_size dim;
3864 dim = isl_basic_map_dim(bmap, type);
3865 if (dim < 0)
3866 return isl_basic_map_free(bmap);
3867 return isl_basic_map_insert_dims(bmap, type, dim, n);
3870 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3871 enum isl_dim_type type, unsigned n)
3873 if (!bset)
3874 return NULL;
3875 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3876 return isl_basic_map_add_dims(bset, type, n);
3877 error:
3878 isl_basic_set_free(bset);
3879 return NULL;
3882 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3883 enum isl_dim_type type)
3885 isl_space *space;
3887 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3888 return map;
3890 space = isl_map_get_space(map);
3891 space = isl_space_reset(space, type);
3892 map = isl_map_reset_space(map, space);
3893 return map;
3896 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3897 enum isl_dim_type type, unsigned pos, unsigned n)
3899 int i;
3901 if (n == 0)
3902 return map_space_reset(map, type);
3904 map = isl_map_cow(map);
3905 if (!map)
3906 return NULL;
3908 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3909 if (!map->dim)
3910 goto error;
3912 for (i = 0; i < map->n; ++i) {
3913 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3914 if (!map->p[i])
3915 goto error;
3918 return map;
3919 error:
3920 isl_map_free(map);
3921 return NULL;
3924 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3925 enum isl_dim_type type, unsigned pos, unsigned n)
3927 return isl_map_insert_dims(set, type, pos, n);
3930 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3931 enum isl_dim_type type, unsigned n)
3933 isl_size dim;
3935 dim = isl_map_dim(map, type);
3936 if (dim < 0)
3937 return isl_map_free(map);
3938 return isl_map_insert_dims(map, type, dim, n);
3941 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3942 enum isl_dim_type type, unsigned n)
3944 if (!set)
3945 return NULL;
3946 isl_assert(set->ctx, type != isl_dim_in, goto error);
3947 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3948 error:
3949 isl_set_free(set);
3950 return NULL;
3953 __isl_give isl_basic_map *isl_basic_map_move_dims(
3954 __isl_take isl_basic_map *bmap,
3955 enum isl_dim_type dst_type, unsigned dst_pos,
3956 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3958 isl_space *space;
3959 struct isl_dim_map *dim_map;
3960 struct isl_basic_map *res;
3961 enum isl_dim_type t;
3962 isl_size total;
3963 unsigned off;
3965 if (!bmap)
3966 return NULL;
3967 if (n == 0) {
3968 bmap = isl_basic_map_reset(bmap, src_type);
3969 bmap = isl_basic_map_reset(bmap, dst_type);
3970 return bmap;
3973 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3974 return isl_basic_map_free(bmap);
3976 if (dst_type == src_type && dst_pos == src_pos)
3977 return bmap;
3979 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3981 if (pos(bmap->dim, dst_type) + dst_pos ==
3982 pos(bmap->dim, src_type) + src_pos +
3983 ((src_type < dst_type) ? n : 0)) {
3984 bmap = isl_basic_map_cow(bmap);
3985 if (!bmap)
3986 return NULL;
3988 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3989 src_type, src_pos, n);
3990 if (!bmap->dim)
3991 goto error;
3993 bmap = isl_basic_map_finalize(bmap);
3995 return bmap;
3998 total = isl_basic_map_dim(bmap, isl_dim_all);
3999 if (total < 0)
4000 return isl_basic_map_free(bmap);
4001 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4003 off = 0;
4004 space = isl_basic_map_peek_space(bmap);
4005 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4006 isl_size size = isl_space_dim(space, t);
4007 if (size < 0)
4008 dim_map = isl_dim_map_free(dim_map);
4009 if (t == dst_type) {
4010 isl_dim_map_dim_range(dim_map, space, t,
4011 0, dst_pos, off);
4012 off += dst_pos;
4013 isl_dim_map_dim_range(dim_map, space, src_type,
4014 src_pos, n, off);
4015 off += n;
4016 isl_dim_map_dim_range(dim_map, space, t,
4017 dst_pos, size - dst_pos, off);
4018 off += size - dst_pos;
4019 } else if (t == src_type) {
4020 isl_dim_map_dim_range(dim_map, space, t,
4021 0, src_pos, off);
4022 off += src_pos;
4023 isl_dim_map_dim_range(dim_map, space, t,
4024 src_pos + n, size - src_pos - n, off);
4025 off += size - src_pos - n;
4026 } else {
4027 isl_dim_map_dim(dim_map, space, t, off);
4028 off += size;
4031 isl_dim_map_div(dim_map, bmap, off);
4033 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4034 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4035 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4036 if (!bmap)
4037 goto error;
4039 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4040 src_type, src_pos, n);
4041 if (!bmap->dim)
4042 goto error;
4044 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4045 bmap = isl_basic_map_gauss(bmap, NULL);
4046 bmap = isl_basic_map_finalize(bmap);
4048 return bmap;
4049 error:
4050 isl_basic_map_free(bmap);
4051 return NULL;
4054 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4055 enum isl_dim_type dst_type, unsigned dst_pos,
4056 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4058 isl_basic_map *bmap = bset_to_bmap(bset);
4059 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4060 src_type, src_pos, n);
4061 return bset_from_bmap(bmap);
4064 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4065 enum isl_dim_type dst_type, unsigned dst_pos,
4066 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4068 if (!set)
4069 return NULL;
4070 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4071 return set_from_map(isl_map_move_dims(set_to_map(set),
4072 dst_type, dst_pos, src_type, src_pos, n));
4073 error:
4074 isl_set_free(set);
4075 return NULL;
4078 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4079 enum isl_dim_type dst_type, unsigned dst_pos,
4080 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4082 int i;
4084 if (n == 0) {
4085 map = isl_map_reset(map, src_type);
4086 map = isl_map_reset(map, dst_type);
4087 return map;
4090 if (isl_map_check_range(map, src_type, src_pos, n))
4091 return isl_map_free(map);
4093 if (dst_type == src_type && dst_pos == src_pos)
4094 return map;
4096 isl_assert(map->ctx, dst_type != src_type, goto error);
4098 map = isl_map_cow(map);
4099 if (!map)
4100 return NULL;
4102 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4103 if (!map->dim)
4104 goto error;
4106 for (i = 0; i < map->n; ++i) {
4107 map->p[i] = isl_basic_map_move_dims(map->p[i],
4108 dst_type, dst_pos,
4109 src_type, src_pos, n);
4110 if (!map->p[i])
4111 goto error;
4114 return map;
4115 error:
4116 isl_map_free(map);
4117 return NULL;
4120 /* Move the specified dimensions to the last columns right before
4121 * the divs. Don't change the dimension specification of bmap.
4122 * That's the responsibility of the caller.
4124 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4125 enum isl_dim_type type, unsigned first, unsigned n)
4127 isl_space *space;
4128 struct isl_dim_map *dim_map;
4129 struct isl_basic_map *res;
4130 enum isl_dim_type t;
4131 isl_size total;
4132 unsigned off;
4134 if (!bmap)
4135 return NULL;
4136 if (isl_basic_map_offset(bmap, type) + first + n ==
4137 isl_basic_map_offset(bmap, isl_dim_div))
4138 return bmap;
4140 total = isl_basic_map_dim(bmap, isl_dim_all);
4141 if (total < 0)
4142 return isl_basic_map_free(bmap);
4143 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4145 off = 0;
4146 space = isl_basic_map_peek_space(bmap);
4147 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4148 isl_size size = isl_space_dim(space, t);
4149 if (size < 0)
4150 dim_map = isl_dim_map_free(dim_map);
4151 if (t == type) {
4152 isl_dim_map_dim_range(dim_map, space, t,
4153 0, first, off);
4154 off += first;
4155 isl_dim_map_dim_range(dim_map, space, t,
4156 first, n, total - bmap->n_div - n);
4157 isl_dim_map_dim_range(dim_map, space, t,
4158 first + n, size - (first + n), off);
4159 off += size - (first + n);
4160 } else {
4161 isl_dim_map_dim(dim_map, space, t, off);
4162 off += size;
4165 isl_dim_map_div(dim_map, bmap, off + n);
4167 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4168 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4169 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4170 return res;
4173 /* Insert "n" rows in the divs of "bmap".
4175 * The number of columns is not changed, which means that the last
4176 * dimensions of "bmap" are being reintepreted as the new divs.
4177 * The space of "bmap" is not adjusted, however, which means
4178 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4179 * from the space of "bmap" is the responsibility of the caller.
4181 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4182 int n)
4184 int i;
4185 size_t row_size;
4186 isl_int **new_div;
4187 isl_int *old;
4189 bmap = isl_basic_map_cow(bmap);
4190 if (!bmap)
4191 return NULL;
4193 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4194 old = bmap->block2.data;
4195 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4196 (bmap->extra + n) * (1 + row_size));
4197 if (!bmap->block2.data)
4198 return isl_basic_map_free(bmap);
4199 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4200 if (!new_div)
4201 return isl_basic_map_free(bmap);
4202 for (i = 0; i < n; ++i) {
4203 new_div[i] = bmap->block2.data +
4204 (bmap->extra + i) * (1 + row_size);
4205 isl_seq_clr(new_div[i], 1 + row_size);
4207 for (i = 0; i < bmap->extra; ++i)
4208 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4209 free(bmap->div);
4210 bmap->div = new_div;
4211 bmap->n_div += n;
4212 bmap->extra += n;
4214 return bmap;
4217 /* Drop constraints from "bmap" that only involve the variables
4218 * of "type" in the range [first, first + n] that are not related
4219 * to any of the variables outside that interval.
4220 * These constraints cannot influence the values for the variables
4221 * outside the interval, except in case they cause "bmap" to be empty.
4222 * Only drop the constraints if "bmap" is known to be non-empty.
4224 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4225 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4226 unsigned first, unsigned n)
4228 int i;
4229 int *groups;
4230 isl_size dim, n_div;
4231 isl_bool non_empty;
4233 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4234 if (non_empty < 0)
4235 return isl_basic_map_free(bmap);
4236 if (!non_empty)
4237 return bmap;
4239 dim = isl_basic_map_dim(bmap, isl_dim_all);
4240 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4241 if (dim < 0 || n_div < 0)
4242 return isl_basic_map_free(bmap);
4243 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4244 if (!groups)
4245 return isl_basic_map_free(bmap);
4246 first += isl_basic_map_offset(bmap, type) - 1;
4247 for (i = 0; i < first; ++i)
4248 groups[i] = -1;
4249 for (i = first + n; i < dim - n_div; ++i)
4250 groups[i] = -1;
4252 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4254 return bmap;
4257 /* Turn the n dimensions of type type, starting at first
4258 * into existentially quantified variables.
4260 * If a subset of the projected out variables are unrelated
4261 * to any of the variables that remain, then the constraints
4262 * involving this subset are simply dropped first.
4264 __isl_give isl_basic_map *isl_basic_map_project_out(
4265 __isl_take isl_basic_map *bmap,
4266 enum isl_dim_type type, unsigned first, unsigned n)
4268 isl_bool empty;
4270 if (n == 0)
4271 return basic_map_space_reset(bmap, type);
4272 if (type == isl_dim_div)
4273 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4274 "cannot project out existentially quantified variables",
4275 return isl_basic_map_free(bmap));
4277 empty = isl_basic_map_plain_is_empty(bmap);
4278 if (empty < 0)
4279 return isl_basic_map_free(bmap);
4280 if (empty)
4281 bmap = isl_basic_map_set_to_empty(bmap);
4283 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4284 if (!bmap)
4285 return NULL;
4287 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4288 return isl_basic_map_remove_dims(bmap, type, first, n);
4290 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4291 return isl_basic_map_free(bmap);
4293 bmap = move_last(bmap, type, first, n);
4294 bmap = isl_basic_map_cow(bmap);
4295 bmap = insert_div_rows(bmap, n);
4296 if (!bmap)
4297 return NULL;
4299 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4300 if (!bmap->dim)
4301 goto error;
4302 bmap = isl_basic_map_simplify(bmap);
4303 bmap = isl_basic_map_drop_redundant_divs(bmap);
4304 return isl_basic_map_finalize(bmap);
4305 error:
4306 isl_basic_map_free(bmap);
4307 return NULL;
4310 /* Turn the n dimensions of type type, starting at first
4311 * into existentially quantified variables.
4313 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4314 enum isl_dim_type type, unsigned first, unsigned n)
4316 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4317 type, first, n));
4320 /* Turn the n dimensions of type type, starting at first
4321 * into existentially quantified variables.
4323 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4324 enum isl_dim_type type, unsigned first, unsigned n)
4326 int i;
4328 if (n == 0)
4329 return map_space_reset(map, type);
4331 if (isl_map_check_range(map, type, first, n) < 0)
4332 return isl_map_free(map);
4334 map = isl_map_cow(map);
4335 if (!map)
4336 return NULL;
4338 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4339 if (!map->dim)
4340 goto error;
4342 for (i = 0; i < map->n; ++i) {
4343 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4344 if (!map->p[i])
4345 goto error;
4348 return map;
4349 error:
4350 isl_map_free(map);
4351 return NULL;
4354 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4355 * into existentially quantified variables.
4357 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4358 enum isl_dim_type type, unsigned first, unsigned n)
4360 isl_size dim;
4362 dim = isl_map_dim(map, type);
4363 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4364 return isl_map_free(map);
4365 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4366 map = isl_map_project_out(map, type, 0, first);
4367 return map;
4370 /* Turn the n dimensions of type type, starting at first
4371 * into existentially quantified variables.
4373 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4374 enum isl_dim_type type, unsigned first, unsigned n)
4376 return set_from_map(isl_map_project_out(set_to_map(set),
4377 type, first, n));
4380 /* Return a map that projects the elements in "set" onto their
4381 * "n" set dimensions starting at "first".
4382 * "type" should be equal to isl_dim_set.
4384 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4385 enum isl_dim_type type, unsigned first, unsigned n)
4387 int i;
4388 isl_map *map;
4390 if (type != isl_dim_set)
4391 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4392 "only set dimensions can be projected out", goto error);
4393 if (isl_set_check_range(set, type, first, n) < 0)
4394 return isl_set_free(set);
4396 map = isl_map_from_domain(set);
4397 map = isl_map_add_dims(map, isl_dim_out, n);
4398 for (i = 0; i < n; ++i)
4399 map = isl_map_equate(map, isl_dim_in, first + i,
4400 isl_dim_out, i);
4401 return map;
4402 error:
4403 isl_set_free(set);
4404 return NULL;
4407 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4408 unsigned n)
4410 int i, j;
4411 isl_size total;
4413 total = isl_basic_map_dim(bmap, isl_dim_all);
4414 if (total < 0)
4415 return isl_basic_map_free(bmap);
4416 for (i = 0; i < n; ++i) {
4417 j = isl_basic_map_alloc_div(bmap);
4418 if (j < 0)
4419 goto error;
4420 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4422 return bmap;
4423 error:
4424 isl_basic_map_free(bmap);
4425 return NULL;
4428 struct isl_basic_map *isl_basic_map_apply_range(
4429 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4431 isl_space *space_result = NULL;
4432 struct isl_basic_map *bmap;
4433 isl_size n_in, n_out, n, nparam;
4434 unsigned total, pos;
4435 struct isl_dim_map *dim_map1, *dim_map2;
4437 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4438 goto error;
4439 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4440 bmap2->dim, isl_dim_in))
4441 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4442 "spaces don't match", goto error);
4444 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4445 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4446 n = isl_basic_map_dim(bmap1, isl_dim_out);
4447 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4448 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4449 goto error;
4451 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4452 isl_basic_map_get_space(bmap2));
4454 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4455 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4456 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4457 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4458 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4459 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4460 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4461 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4462 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4463 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4464 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4466 bmap = isl_basic_map_alloc_space(space_result,
4467 bmap1->n_div + bmap2->n_div + n,
4468 bmap1->n_eq + bmap2->n_eq,
4469 bmap1->n_ineq + bmap2->n_ineq);
4470 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4471 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4472 bmap = add_divs(bmap, n);
4473 bmap = isl_basic_map_simplify(bmap);
4474 bmap = isl_basic_map_drop_redundant_divs(bmap);
4475 return isl_basic_map_finalize(bmap);
4476 error:
4477 isl_basic_map_free(bmap1);
4478 isl_basic_map_free(bmap2);
4479 return NULL;
4482 struct isl_basic_set *isl_basic_set_apply(
4483 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4485 if (!bset || !bmap)
4486 goto error;
4488 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4489 goto error);
4491 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4492 bmap));
4493 error:
4494 isl_basic_set_free(bset);
4495 isl_basic_map_free(bmap);
4496 return NULL;
4499 struct isl_basic_map *isl_basic_map_apply_domain(
4500 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4502 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4503 goto error;
4504 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4505 bmap2->dim, isl_dim_in))
4506 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4507 "spaces don't match", goto error);
4509 bmap1 = isl_basic_map_reverse(bmap1);
4510 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4511 return isl_basic_map_reverse(bmap1);
4512 error:
4513 isl_basic_map_free(bmap1);
4514 isl_basic_map_free(bmap2);
4515 return NULL;
4518 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4519 * A \cap B -> f(A) + f(B)
4521 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4522 __isl_take isl_basic_map *bmap2)
4524 isl_size n_in, n_out, nparam;
4525 unsigned total, pos;
4526 struct isl_basic_map *bmap = NULL;
4527 struct isl_dim_map *dim_map1, *dim_map2;
4528 int i;
4530 if (!bmap1 || !bmap2)
4531 goto error;
4533 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4534 goto error);
4536 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4537 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4538 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4539 if (nparam < 0 || n_in < 0 || n_out < 0)
4540 goto error;
4542 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4543 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4544 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4545 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4546 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4547 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4548 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4549 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4550 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4551 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4552 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4554 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4555 bmap1->n_div + bmap2->n_div + 2 * n_out,
4556 bmap1->n_eq + bmap2->n_eq + n_out,
4557 bmap1->n_ineq + bmap2->n_ineq);
4558 for (i = 0; i < n_out; ++i) {
4559 int j = isl_basic_map_alloc_equality(bmap);
4560 if (j < 0)
4561 goto error;
4562 isl_seq_clr(bmap->eq[j], 1+total);
4563 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4564 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4565 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4567 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4568 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4569 bmap = add_divs(bmap, 2 * n_out);
4571 bmap = isl_basic_map_simplify(bmap);
4572 return isl_basic_map_finalize(bmap);
4573 error:
4574 isl_basic_map_free(bmap);
4575 isl_basic_map_free(bmap1);
4576 isl_basic_map_free(bmap2);
4577 return NULL;
4580 /* Given two maps A -> f(A) and B -> g(B), construct a map
4581 * A \cap B -> f(A) + f(B)
4583 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4584 __isl_take isl_map *map2)
4586 struct isl_map *result;
4587 int i, j;
4589 if (!map1 || !map2)
4590 goto error;
4592 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4594 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4595 map1->n * map2->n, 0);
4596 if (!result)
4597 goto error;
4598 for (i = 0; i < map1->n; ++i)
4599 for (j = 0; j < map2->n; ++j) {
4600 struct isl_basic_map *part;
4601 part = isl_basic_map_sum(
4602 isl_basic_map_copy(map1->p[i]),
4603 isl_basic_map_copy(map2->p[j]));
4604 if (isl_basic_map_is_empty(part))
4605 isl_basic_map_free(part);
4606 else
4607 result = isl_map_add_basic_map(result, part);
4608 if (!result)
4609 goto error;
4611 isl_map_free(map1);
4612 isl_map_free(map2);
4613 return result;
4614 error:
4615 isl_map_free(map1);
4616 isl_map_free(map2);
4617 return NULL;
4620 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4621 __isl_take isl_set *set2)
4623 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4626 /* Given a basic map A -> f(A), construct A -> -f(A).
4628 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4630 int i, j;
4631 unsigned off;
4632 isl_size n;
4634 bmap = isl_basic_map_cow(bmap);
4635 n = isl_basic_map_dim(bmap, isl_dim_out);
4636 if (n < 0)
4637 return isl_basic_map_free(bmap);
4639 off = isl_basic_map_offset(bmap, isl_dim_out);
4640 for (i = 0; i < bmap->n_eq; ++i)
4641 for (j = 0; j < n; ++j)
4642 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4643 for (i = 0; i < bmap->n_ineq; ++i)
4644 for (j = 0; j < n; ++j)
4645 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4646 for (i = 0; i < bmap->n_div; ++i)
4647 for (j = 0; j < n; ++j)
4648 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4649 bmap = isl_basic_map_gauss(bmap, NULL);
4650 return isl_basic_map_finalize(bmap);
4653 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4655 return isl_basic_map_neg(bset);
4658 /* Given a map A -> f(A), construct A -> -f(A).
4660 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4662 int i;
4664 map = isl_map_cow(map);
4665 if (!map)
4666 return NULL;
4668 for (i = 0; i < map->n; ++i) {
4669 map->p[i] = isl_basic_map_neg(map->p[i]);
4670 if (!map->p[i])
4671 goto error;
4674 return map;
4675 error:
4676 isl_map_free(map);
4677 return NULL;
4680 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4682 return set_from_map(isl_map_neg(set_to_map(set)));
4685 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4686 * A -> floor(f(A)/d).
4688 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4689 isl_int d)
4691 isl_size n_in, n_out, nparam;
4692 unsigned total, pos;
4693 struct isl_basic_map *result = NULL;
4694 struct isl_dim_map *dim_map;
4695 int i;
4697 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4698 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4699 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4700 if (nparam < 0 || n_in < 0 || n_out < 0)
4701 return isl_basic_map_free(bmap);
4703 total = nparam + n_in + n_out + bmap->n_div + n_out;
4704 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4705 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4706 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4707 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4708 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4710 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4711 bmap->n_div + n_out,
4712 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4713 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4714 result = add_divs(result, n_out);
4715 for (i = 0; i < n_out; ++i) {
4716 int j;
4717 j = isl_basic_map_alloc_inequality(result);
4718 if (j < 0)
4719 goto error;
4720 isl_seq_clr(result->ineq[j], 1+total);
4721 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4722 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4723 j = isl_basic_map_alloc_inequality(result);
4724 if (j < 0)
4725 goto error;
4726 isl_seq_clr(result->ineq[j], 1+total);
4727 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4728 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4729 isl_int_sub_ui(result->ineq[j][0], d, 1);
4732 result = isl_basic_map_simplify(result);
4733 return isl_basic_map_finalize(result);
4734 error:
4735 isl_basic_map_free(result);
4736 return NULL;
4739 /* Given a map A -> f(A) and an integer d, construct a map
4740 * A -> floor(f(A)/d).
4742 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4744 int i;
4746 map = isl_map_cow(map);
4747 if (!map)
4748 return NULL;
4750 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4751 for (i = 0; i < map->n; ++i) {
4752 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4753 if (!map->p[i])
4754 goto error;
4756 map = isl_map_unmark_normalized(map);
4758 return map;
4759 error:
4760 isl_map_free(map);
4761 return NULL;
4764 /* Given a map A -> f(A) and an integer d, construct a map
4765 * A -> floor(f(A)/d).
4767 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4768 __isl_take isl_val *d)
4770 if (!map || !d)
4771 goto error;
4772 if (!isl_val_is_int(d))
4773 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4774 "expecting integer denominator", goto error);
4775 map = isl_map_floordiv(map, d->n);
4776 isl_val_free(d);
4777 return map;
4778 error:
4779 isl_map_free(map);
4780 isl_val_free(d);
4781 return NULL;
4784 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4785 unsigned pos)
4787 int i;
4788 isl_size nparam;
4789 isl_size n_in;
4790 isl_size total;
4792 total = isl_basic_map_dim(bmap, isl_dim_all);
4793 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4794 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4795 if (total < 0 || nparam < 0 || n_in < 0)
4796 return isl_basic_map_free(bmap);
4797 i = isl_basic_map_alloc_equality(bmap);
4798 if (i < 0)
4799 goto error;
4800 isl_seq_clr(bmap->eq[i], 1 + total);
4801 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4802 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4803 return isl_basic_map_finalize(bmap);
4804 error:
4805 isl_basic_map_free(bmap);
4806 return NULL;
4809 /* Add a constraint to "bmap" expressing i_pos < o_pos
4811 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4812 unsigned pos)
4814 int i;
4815 isl_size nparam;
4816 isl_size n_in;
4817 isl_size total;
4819 total = isl_basic_map_dim(bmap, isl_dim_all);
4820 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4821 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4822 if (total < 0 || nparam < 0 || n_in < 0)
4823 return isl_basic_map_free(bmap);
4824 i = isl_basic_map_alloc_inequality(bmap);
4825 if (i < 0)
4826 goto error;
4827 isl_seq_clr(bmap->ineq[i], 1 + total);
4828 isl_int_set_si(bmap->ineq[i][0], -1);
4829 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4830 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4831 return isl_basic_map_finalize(bmap);
4832 error:
4833 isl_basic_map_free(bmap);
4834 return NULL;
4837 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4839 static __isl_give isl_basic_map *var_less_or_equal(
4840 __isl_take isl_basic_map *bmap, unsigned pos)
4842 int i;
4843 isl_size nparam;
4844 isl_size n_in;
4845 isl_size total;
4847 total = isl_basic_map_dim(bmap, isl_dim_all);
4848 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4849 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4850 if (total < 0 || nparam < 0 || n_in < 0)
4851 return isl_basic_map_free(bmap);
4852 i = isl_basic_map_alloc_inequality(bmap);
4853 if (i < 0)
4854 goto error;
4855 isl_seq_clr(bmap->ineq[i], 1 + total);
4856 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4857 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4858 return isl_basic_map_finalize(bmap);
4859 error:
4860 isl_basic_map_free(bmap);
4861 return NULL;
4864 /* Add a constraint to "bmap" expressing i_pos > o_pos
4866 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4867 unsigned pos)
4869 int i;
4870 isl_size nparam;
4871 isl_size n_in;
4872 isl_size total;
4874 total = isl_basic_map_dim(bmap, isl_dim_all);
4875 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4876 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4877 if (total < 0 || nparam < 0 || n_in < 0)
4878 return isl_basic_map_free(bmap);
4879 i = isl_basic_map_alloc_inequality(bmap);
4880 if (i < 0)
4881 goto error;
4882 isl_seq_clr(bmap->ineq[i], 1 + total);
4883 isl_int_set_si(bmap->ineq[i][0], -1);
4884 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4885 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4886 return isl_basic_map_finalize(bmap);
4887 error:
4888 isl_basic_map_free(bmap);
4889 return NULL;
4892 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4894 static __isl_give isl_basic_map *var_more_or_equal(
4895 __isl_take isl_basic_map *bmap, unsigned pos)
4897 int i;
4898 isl_size nparam;
4899 isl_size n_in;
4900 isl_size total;
4902 total = isl_basic_map_dim(bmap, isl_dim_all);
4903 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4904 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4905 if (total < 0 || nparam < 0 || n_in < 0)
4906 return isl_basic_map_free(bmap);
4907 i = isl_basic_map_alloc_inequality(bmap);
4908 if (i < 0)
4909 goto error;
4910 isl_seq_clr(bmap->ineq[i], 1 + total);
4911 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4912 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4913 return isl_basic_map_finalize(bmap);
4914 error:
4915 isl_basic_map_free(bmap);
4916 return NULL;
4919 __isl_give isl_basic_map *isl_basic_map_equal(
4920 __isl_take isl_space *space, unsigned n_equal)
4922 int i;
4923 struct isl_basic_map *bmap;
4924 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4925 if (!bmap)
4926 return NULL;
4927 for (i = 0; i < n_equal && bmap; ++i)
4928 bmap = var_equal(bmap, i);
4929 return isl_basic_map_finalize(bmap);
4932 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4934 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4935 unsigned pos)
4937 int i;
4938 struct isl_basic_map *bmap;
4939 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4940 if (!bmap)
4941 return NULL;
4942 for (i = 0; i < pos && bmap; ++i)
4943 bmap = var_equal(bmap, i);
4944 if (bmap)
4945 bmap = var_less(bmap, pos);
4946 return isl_basic_map_finalize(bmap);
4949 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4951 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4952 __isl_take isl_space *space, unsigned pos)
4954 int i;
4955 isl_basic_map *bmap;
4957 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4958 for (i = 0; i < pos; ++i)
4959 bmap = var_equal(bmap, i);
4960 bmap = var_less_or_equal(bmap, pos);
4961 return isl_basic_map_finalize(bmap);
4964 /* Return a relation on "space" expressing i_pos > o_pos
4966 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4967 unsigned pos)
4969 int i;
4970 struct isl_basic_map *bmap;
4971 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4972 if (!bmap)
4973 return NULL;
4974 for (i = 0; i < pos && bmap; ++i)
4975 bmap = var_equal(bmap, i);
4976 if (bmap)
4977 bmap = var_more(bmap, pos);
4978 return isl_basic_map_finalize(bmap);
4981 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4983 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4984 __isl_take isl_space *space, unsigned pos)
4986 int i;
4987 isl_basic_map *bmap;
4989 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4990 for (i = 0; i < pos; ++i)
4991 bmap = var_equal(bmap, i);
4992 bmap = var_more_or_equal(bmap, pos);
4993 return isl_basic_map_finalize(bmap);
4996 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4997 unsigned n, int equal)
4999 struct isl_map *map;
5000 int i;
5002 if (n == 0 && equal)
5003 return isl_map_universe(space);
5005 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5007 for (i = 0; i + 1 < n; ++i)
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_less_at(isl_space_copy(space), i));
5010 if (n > 0) {
5011 if (equal)
5012 map = isl_map_add_basic_map(map,
5013 isl_basic_map_less_or_equal_at(space, n - 1));
5014 else
5015 map = isl_map_add_basic_map(map,
5016 isl_basic_map_less_at(space, n - 1));
5017 } else
5018 isl_space_free(space);
5020 return map;
5023 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5025 if (!space)
5026 return NULL;
5027 return map_lex_lte_first(space, space->n_out, equal);
5030 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5032 return map_lex_lte_first(dim, n, 0);
5035 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5037 return map_lex_lte_first(dim, n, 1);
5040 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5042 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5045 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5047 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5050 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5051 unsigned n, int equal)
5053 struct isl_map *map;
5054 int i;
5056 if (n == 0 && equal)
5057 return isl_map_universe(space);
5059 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5061 for (i = 0; i + 1 < n; ++i)
5062 map = isl_map_add_basic_map(map,
5063 isl_basic_map_more_at(isl_space_copy(space), i));
5064 if (n > 0) {
5065 if (equal)
5066 map = isl_map_add_basic_map(map,
5067 isl_basic_map_more_or_equal_at(space, n - 1));
5068 else
5069 map = isl_map_add_basic_map(map,
5070 isl_basic_map_more_at(space, n - 1));
5071 } else
5072 isl_space_free(space);
5074 return map;
5077 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5079 if (!space)
5080 return NULL;
5081 return map_lex_gte_first(space, space->n_out, equal);
5084 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5086 return map_lex_gte_first(dim, n, 0);
5089 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5091 return map_lex_gte_first(dim, n, 1);
5094 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5096 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5099 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5101 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5104 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5105 __isl_take isl_set *set2)
5107 isl_map *map;
5108 map = isl_map_lex_le(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5114 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5115 __isl_take isl_set *set2)
5117 isl_map *map;
5118 map = isl_map_lex_lt(isl_set_get_space(set1));
5119 map = isl_map_intersect_domain(map, set1);
5120 map = isl_map_intersect_range(map, set2);
5121 return map;
5124 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5125 __isl_take isl_set *set2)
5127 isl_map *map;
5128 map = isl_map_lex_ge(isl_set_get_space(set1));
5129 map = isl_map_intersect_domain(map, set1);
5130 map = isl_map_intersect_range(map, set2);
5131 return map;
5134 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5135 __isl_take isl_set *set2)
5137 isl_map *map;
5138 map = isl_map_lex_gt(isl_set_get_space(set1));
5139 map = isl_map_intersect_domain(map, set1);
5140 map = isl_map_intersect_range(map, set2);
5141 return map;
5144 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5147 isl_map *map;
5148 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5154 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5155 __isl_take isl_map *map2)
5157 isl_map *map;
5158 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5159 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5160 map = isl_map_apply_range(map, isl_map_reverse(map2));
5161 return map;
5164 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5165 __isl_take isl_map *map2)
5167 isl_map *map;
5168 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5169 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5170 map = isl_map_apply_range(map, isl_map_reverse(map2));
5171 return map;
5174 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5175 __isl_take isl_map *map2)
5177 isl_map *map;
5178 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5179 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5180 map = isl_map_apply_range(map, isl_map_reverse(map2));
5181 return map;
5184 /* For the div d = floor(f/m) at position "div", add the constraint
5186 * f - m d >= 0
5188 static __isl_give isl_basic_map *add_upper_div_constraint(
5189 __isl_take isl_basic_map *bmap, unsigned div)
5191 int i;
5192 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5193 isl_size n_div;
5194 unsigned pos;
5196 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5197 if (v_div < 0 || n_div < 0)
5198 return isl_basic_map_free(bmap);
5199 pos = v_div + div;
5200 i = isl_basic_map_alloc_inequality(bmap);
5201 if (i < 0)
5202 return isl_basic_map_free(bmap);
5203 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5204 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5206 return bmap;
5209 /* For the div d = floor(f/m) at position "div", add the constraint
5211 * -(f-(m-1)) + m d >= 0
5213 static __isl_give isl_basic_map *add_lower_div_constraint(
5214 __isl_take isl_basic_map *bmap, unsigned div)
5216 int i;
5217 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5218 isl_size n_div;
5219 unsigned pos;
5221 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5222 if (v_div < 0 || n_div < 0)
5223 return isl_basic_map_free(bmap);
5224 pos = v_div + div;
5225 i = isl_basic_map_alloc_inequality(bmap);
5226 if (i < 0)
5227 return isl_basic_map_free(bmap);
5228 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5229 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5230 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5231 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5233 return bmap;
5236 /* For the div d = floor(f/m) at position "pos", add the constraints
5238 * f - m d >= 0
5239 * -(f-(m-1)) + m d >= 0
5241 * Note that the second constraint is the negation of
5243 * f - m d >= m
5245 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5246 __isl_take isl_basic_map *bmap, unsigned pos)
5248 bmap = add_upper_div_constraint(bmap, pos);
5249 bmap = add_lower_div_constraint(bmap, pos);
5250 return bmap;
5253 /* For each known div d = floor(f/m), add the constraints
5255 * f - m d >= 0
5256 * -(f-(m-1)) + m d >= 0
5258 * Remove duplicate constraints in case of some these div constraints
5259 * already appear in "bmap".
5261 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5262 __isl_take isl_basic_map *bmap)
5264 isl_size n_div;
5266 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5267 if (n_div < 0)
5268 return isl_basic_map_free(bmap);
5269 if (n_div == 0)
5270 return bmap;
5272 bmap = add_known_div_constraints(bmap);
5273 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5274 bmap = isl_basic_map_finalize(bmap);
5275 return bmap;
5278 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5280 * In particular, if this div is of the form d = floor(f/m),
5281 * then add the constraint
5283 * f - m d >= 0
5285 * if sign < 0 or the constraint
5287 * -(f-(m-1)) + m d >= 0
5289 * if sign > 0.
5291 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5292 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5294 if (sign < 0)
5295 return add_upper_div_constraint(bmap, div);
5296 else
5297 return add_lower_div_constraint(bmap, div);
5300 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5301 __isl_take isl_basic_map *bmap)
5303 if (!bmap)
5304 goto error;
5305 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5306 bmap->n_div == 0 &&
5307 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5308 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5309 return bset_from_bmap(bmap);
5310 bmap = isl_basic_map_cow(bmap);
5311 if (!bmap)
5312 goto error;
5313 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5314 if (!bmap->dim)
5315 goto error;
5316 bmap->extra -= bmap->n_div;
5317 bmap->n_div = 0;
5318 bmap = isl_basic_map_finalize(bmap);
5319 return bset_from_bmap(bmap);
5320 error:
5321 isl_basic_map_free(bmap);
5322 return NULL;
5325 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5326 __isl_take isl_basic_set *bset)
5328 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5331 /* Replace each element in "list" by the result of applying
5332 * isl_basic_map_underlying_set to the element.
5334 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5335 __isl_take isl_basic_map_list *list)
5337 int i;
5338 isl_size n;
5340 n = isl_basic_map_list_n_basic_map(list);
5341 if (n < 0)
5342 goto error;
5344 for (i = 0; i < n; ++i) {
5345 isl_basic_map *bmap;
5346 isl_basic_set *bset;
5348 bmap = isl_basic_map_list_get_basic_map(list, i);
5349 bset = isl_basic_set_underlying_set(bmap);
5350 list = isl_basic_set_list_set_basic_set(list, i, bset);
5353 return list;
5354 error:
5355 isl_basic_map_list_free(list);
5356 return NULL;
5359 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5360 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5362 struct isl_basic_map *bmap;
5363 struct isl_ctx *ctx;
5364 isl_size dim, bmap_total;
5365 unsigned total;
5366 int i;
5368 if (!bset || !like)
5369 goto error;
5370 ctx = bset->ctx;
5371 if (isl_basic_set_check_no_params(bset) < 0 ||
5372 isl_basic_set_check_no_locals(bset) < 0)
5373 goto error;
5374 dim = isl_basic_set_dim(bset, isl_dim_set);
5375 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5376 if (dim < 0 || bmap_total < 0)
5377 goto error;
5378 isl_assert(ctx, dim == bmap_total, goto error);
5379 if (like->n_div == 0) {
5380 isl_space *space = isl_basic_map_get_space(like);
5381 isl_basic_map_free(like);
5382 return isl_basic_map_reset_space(bset, space);
5384 bset = isl_basic_set_cow(bset);
5385 if (!bset)
5386 goto error;
5387 total = dim + bset->extra;
5388 bmap = bset_to_bmap(bset);
5389 isl_space_free(bmap->dim);
5390 bmap->dim = isl_space_copy(like->dim);
5391 if (!bmap->dim)
5392 goto error;
5393 bmap->n_div = like->n_div;
5394 bmap->extra += like->n_div;
5395 if (bmap->extra) {
5396 unsigned ltotal;
5397 isl_int **div;
5398 ltotal = total - bmap->extra + like->extra;
5399 if (ltotal > total)
5400 ltotal = total;
5401 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5402 bmap->extra * (1 + 1 + total));
5403 if (isl_blk_is_error(bmap->block2))
5404 goto error;
5405 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5406 if (!div)
5407 goto error;
5408 bmap->div = div;
5409 for (i = 0; i < bmap->extra; ++i)
5410 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5411 for (i = 0; i < like->n_div; ++i) {
5412 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5413 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5415 bmap = isl_basic_map_add_known_div_constraints(bmap);
5417 isl_basic_map_free(like);
5418 bmap = isl_basic_map_simplify(bmap);
5419 bmap = isl_basic_map_finalize(bmap);
5420 return bmap;
5421 error:
5422 isl_basic_map_free(like);
5423 isl_basic_set_free(bset);
5424 return NULL;
5427 struct isl_basic_set *isl_basic_set_from_underlying_set(
5428 struct isl_basic_set *bset, struct isl_basic_set *like)
5430 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5431 bset_to_bmap(like)));
5434 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5436 int i;
5438 map = isl_map_cow(map);
5439 if (!map)
5440 return NULL;
5441 map->dim = isl_space_cow(map->dim);
5442 if (!map->dim)
5443 goto error;
5445 for (i = 1; i < map->n; ++i)
5446 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5447 goto error);
5448 for (i = 0; i < map->n; ++i) {
5449 map->p[i] = bset_to_bmap(
5450 isl_basic_map_underlying_set(map->p[i]));
5451 if (!map->p[i])
5452 goto error;
5454 if (map->n == 0)
5455 map->dim = isl_space_underlying(map->dim, 0);
5456 else {
5457 isl_space_free(map->dim);
5458 map->dim = isl_space_copy(map->p[0]->dim);
5460 if (!map->dim)
5461 goto error;
5462 return set_from_map(map);
5463 error:
5464 isl_map_free(map);
5465 return NULL;
5468 /* Replace the space of "bmap" by "space".
5470 * If the space of "bmap" is identical to "space" (including the identifiers
5471 * of the input and output dimensions), then simply return the original input.
5473 __isl_give isl_basic_map *isl_basic_map_reset_space(
5474 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5476 isl_bool equal;
5477 isl_space *bmap_space;
5479 bmap_space = isl_basic_map_peek_space(bmap);
5480 equal = isl_space_is_equal(bmap_space, space);
5481 if (equal >= 0 && equal)
5482 equal = isl_space_has_equal_ids(bmap_space, space);
5483 if (equal < 0)
5484 goto error;
5485 if (equal) {
5486 isl_space_free(space);
5487 return bmap;
5489 bmap = isl_basic_map_cow(bmap);
5490 if (!bmap || !space)
5491 goto error;
5493 isl_space_free(bmap->dim);
5494 bmap->dim = space;
5496 bmap = isl_basic_map_finalize(bmap);
5498 return bmap;
5499 error:
5500 isl_basic_map_free(bmap);
5501 isl_space_free(space);
5502 return NULL;
5505 __isl_give isl_basic_set *isl_basic_set_reset_space(
5506 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5508 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5509 dim));
5512 /* Check that the total dimensions of "map" and "space" are the same.
5514 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5515 __isl_keep isl_space *space)
5517 isl_size dim1, dim2;
5519 dim1 = isl_map_dim(map, isl_dim_all);
5520 dim2 = isl_space_dim(space, isl_dim_all);
5521 if (dim1 < 0 || dim2 < 0)
5522 return isl_stat_error;
5523 if (dim1 == dim2)
5524 return isl_stat_ok;
5525 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5526 "total dimensions do not match", return isl_stat_error);
5529 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5530 __isl_take isl_space *space)
5532 int i;
5534 map = isl_map_cow(map);
5535 if (!map || !space)
5536 goto error;
5538 for (i = 0; i < map->n; ++i) {
5539 map->p[i] = isl_basic_map_reset_space(map->p[i],
5540 isl_space_copy(space));
5541 if (!map->p[i])
5542 goto error;
5544 isl_space_free(map->dim);
5545 map->dim = space;
5547 return map;
5548 error:
5549 isl_map_free(map);
5550 isl_space_free(space);
5551 return NULL;
5554 /* Replace the space of "map" by "space", without modifying
5555 * the dimension of "map".
5557 * If the space of "map" is identical to "space" (including the identifiers
5558 * of the input and output dimensions), then simply return the original input.
5560 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5561 __isl_take isl_space *space)
5563 isl_bool equal;
5564 isl_space *map_space;
5566 map_space = isl_map_peek_space(map);
5567 equal = isl_space_is_equal(map_space, space);
5568 if (equal >= 0 && equal)
5569 equal = isl_space_has_equal_ids(map_space, space);
5570 if (equal < 0)
5571 goto error;
5572 if (equal) {
5573 isl_space_free(space);
5574 return map;
5576 if (check_map_space_equal_total_dim(map, space) < 0)
5577 goto error;
5578 return isl_map_reset_space(map, space);
5579 error:
5580 isl_map_free(map);
5581 isl_space_free(space);
5582 return NULL;
5585 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5586 __isl_take isl_space *dim)
5588 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5591 /* Compute the parameter domain of the given basic set.
5593 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5595 isl_bool is_params;
5596 isl_space *space;
5597 isl_size n;
5599 is_params = isl_basic_set_is_params(bset);
5600 if (is_params < 0)
5601 return isl_basic_set_free(bset);
5602 if (is_params)
5603 return bset;
5605 n = isl_basic_set_dim(bset, isl_dim_set);
5606 if (n < 0)
5607 return isl_basic_set_free(bset);
5608 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5609 space = isl_basic_set_get_space(bset);
5610 space = isl_space_params(space);
5611 bset = isl_basic_set_reset_space(bset, space);
5612 return bset;
5615 /* Construct a zero-dimensional basic set with the given parameter domain.
5617 __isl_give isl_basic_set *isl_basic_set_from_params(
5618 __isl_take isl_basic_set *bset)
5620 isl_space *space;
5621 space = isl_basic_set_get_space(bset);
5622 space = isl_space_set_from_params(space);
5623 bset = isl_basic_set_reset_space(bset, space);
5624 return bset;
5627 /* Compute the parameter domain of the given set.
5629 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5631 return isl_map_params(set_to_map(set));
5634 /* Construct a zero-dimensional set with the given parameter domain.
5636 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5638 isl_space *space;
5639 space = isl_set_get_space(set);
5640 space = isl_space_set_from_params(space);
5641 set = isl_set_reset_space(set, space);
5642 return set;
5645 /* Compute the parameter domain of the given map.
5647 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5649 isl_space *space;
5650 isl_size n_in, n_out;
5652 n_in = isl_map_dim(map, isl_dim_in);
5653 n_out = isl_map_dim(map, isl_dim_out);
5654 if (n_in < 0 || n_out < 0)
5655 return isl_map_free(map);
5656 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5657 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5658 space = isl_map_get_space(map);
5659 space = isl_space_params(space);
5660 map = isl_map_reset_space(map, space);
5661 return map;
5664 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5666 isl_space *space;
5667 isl_size n_out;
5669 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5670 if (n_out < 0)
5671 return isl_basic_map_free(bmap);
5672 space = isl_space_domain(isl_basic_map_get_space(bmap));
5674 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5676 return isl_basic_map_reset_space(bmap, space);
5679 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5681 if (!bmap)
5682 return isl_bool_error;
5683 return isl_space_may_be_set(bmap->dim);
5686 /* Is this basic map actually a set?
5687 * Users should never call this function. Outside of isl,
5688 * the type should indicate whether something is a set or a map.
5690 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5692 if (!bmap)
5693 return isl_bool_error;
5694 return isl_space_is_set(bmap->dim);
5697 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5699 isl_bool is_set;
5701 is_set = isl_basic_map_is_set(bmap);
5702 if (is_set < 0)
5703 goto error;
5704 if (is_set)
5705 return bmap;
5706 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5707 error:
5708 isl_basic_map_free(bmap);
5709 return NULL;
5712 __isl_give isl_basic_map *isl_basic_map_domain_map(
5713 __isl_take isl_basic_map *bmap)
5715 int i;
5716 isl_space *space;
5717 isl_basic_map *domain;
5718 isl_size nparam, n_in, n_out;
5720 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5721 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5723 if (nparam < 0 || n_in < 0 || n_out < 0)
5724 return isl_basic_map_free(bmap);
5726 space = isl_basic_map_get_space(bmap);
5727 space = isl_space_from_range(isl_space_domain(space));
5728 domain = isl_basic_map_universe(space);
5730 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5731 bmap = isl_basic_map_apply_range(bmap, domain);
5732 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5734 for (i = 0; i < n_in; ++i)
5735 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5736 isl_dim_out, i);
5738 bmap = isl_basic_map_gauss(bmap, NULL);
5739 return isl_basic_map_finalize(bmap);
5742 __isl_give isl_basic_map *isl_basic_map_range_map(
5743 __isl_take isl_basic_map *bmap)
5745 int i;
5746 isl_space *space;
5747 isl_basic_map *range;
5748 isl_size nparam, n_in, n_out;
5750 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5751 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5752 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5753 if (nparam < 0 || n_in < 0 || n_out < 0)
5754 return isl_basic_map_free(bmap);
5756 space = isl_basic_map_get_space(bmap);
5757 space = isl_space_from_range(isl_space_range(space));
5758 range = isl_basic_map_universe(space);
5760 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5761 bmap = isl_basic_map_apply_range(bmap, range);
5762 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5764 for (i = 0; i < n_out; ++i)
5765 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5766 isl_dim_out, i);
5768 bmap = isl_basic_map_gauss(bmap, NULL);
5769 return isl_basic_map_finalize(bmap);
5772 int isl_map_may_be_set(__isl_keep isl_map *map)
5774 if (!map)
5775 return -1;
5776 return isl_space_may_be_set(map->dim);
5779 /* Is this map actually a set?
5780 * Users should never call this function. Outside of isl,
5781 * the type should indicate whether something is a set or a map.
5783 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5785 if (!map)
5786 return isl_bool_error;
5787 return isl_space_is_set(map->dim);
5790 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5792 int i;
5793 isl_bool is_set;
5794 struct isl_set *set;
5796 is_set = isl_map_is_set(map);
5797 if (is_set < 0)
5798 goto error;
5799 if (is_set)
5800 return set_from_map(map);
5802 map = isl_map_cow(map);
5803 if (!map)
5804 goto error;
5806 set = set_from_map(map);
5807 set->dim = isl_space_range(set->dim);
5808 if (!set->dim)
5809 goto error;
5810 for (i = 0; i < map->n; ++i) {
5811 set->p[i] = isl_basic_map_range(map->p[i]);
5812 if (!set->p[i])
5813 goto error;
5815 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5816 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5817 return set;
5818 error:
5819 isl_map_free(map);
5820 return NULL;
5823 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5825 int i;
5827 map = isl_map_cow(map);
5828 if (!map)
5829 return NULL;
5831 map->dim = isl_space_domain_map(map->dim);
5832 if (!map->dim)
5833 goto error;
5834 for (i = 0; i < map->n; ++i) {
5835 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5836 if (!map->p[i])
5837 goto error;
5839 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5840 map = isl_map_unmark_normalized(map);
5841 return map;
5842 error:
5843 isl_map_free(map);
5844 return NULL;
5847 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5849 int i;
5850 isl_space *range_dim;
5852 map = isl_map_cow(map);
5853 if (!map)
5854 return NULL;
5856 range_dim = isl_space_range(isl_map_get_space(map));
5857 range_dim = isl_space_from_range(range_dim);
5858 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5859 map->dim = isl_space_join(map->dim, range_dim);
5860 if (!map->dim)
5861 goto error;
5862 for (i = 0; i < map->n; ++i) {
5863 map->p[i] = isl_basic_map_range_map(map->p[i]);
5864 if (!map->p[i])
5865 goto error;
5867 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5868 map = isl_map_unmark_normalized(map);
5869 return map;
5870 error:
5871 isl_map_free(map);
5872 return NULL;
5875 /* Given a wrapped map of the form A[B -> C],
5876 * return the map A[B -> C] -> B.
5878 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5880 isl_id *id;
5881 isl_map *map;
5883 if (!set)
5884 return NULL;
5885 if (!isl_set_has_tuple_id(set))
5886 return isl_map_domain_map(isl_set_unwrap(set));
5888 id = isl_set_get_tuple_id(set);
5889 map = isl_map_domain_map(isl_set_unwrap(set));
5890 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5892 return map;
5895 __isl_give isl_basic_map *isl_basic_map_from_domain(
5896 __isl_take isl_basic_set *bset)
5898 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5901 __isl_give isl_basic_map *isl_basic_map_from_range(
5902 __isl_take isl_basic_set *bset)
5904 isl_space *space;
5905 space = isl_basic_set_get_space(bset);
5906 space = isl_space_from_range(space);
5907 bset = isl_basic_set_reset_space(bset, space);
5908 return bset_to_bmap(bset);
5911 /* Create a relation with the given set as range.
5912 * The domain of the created relation is a zero-dimensional
5913 * flat anonymous space.
5915 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5917 isl_space *space;
5918 space = isl_set_get_space(set);
5919 space = isl_space_from_range(space);
5920 set = isl_set_reset_space(set, space);
5921 return set_to_map(set);
5924 /* Create a relation with the given set as domain.
5925 * The range of the created relation is a zero-dimensional
5926 * flat anonymous space.
5928 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5930 return isl_map_reverse(isl_map_from_range(set));
5933 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5934 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5936 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5939 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5940 __isl_take isl_set *range)
5942 return isl_map_apply_range(isl_map_reverse(domain), range);
5945 /* Return a newly allocated isl_map with given space and flags and
5946 * room for "n" basic maps.
5947 * Make sure that all cached information is cleared.
5949 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5950 unsigned flags)
5952 struct isl_map *map;
5954 if (!space)
5955 return NULL;
5956 if (n < 0)
5957 isl_die(space->ctx, isl_error_internal,
5958 "negative number of basic maps", goto error);
5959 map = isl_calloc(space->ctx, struct isl_map,
5960 sizeof(struct isl_map) +
5961 (n - 1) * sizeof(struct isl_basic_map *));
5962 if (!map)
5963 goto error;
5965 map->ctx = space->ctx;
5966 isl_ctx_ref(map->ctx);
5967 map->ref = 1;
5968 map->size = n;
5969 map->n = 0;
5970 map->dim = space;
5971 map->flags = flags;
5972 return map;
5973 error:
5974 isl_space_free(space);
5975 return NULL;
5978 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5980 struct isl_basic_map *bmap;
5981 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5982 bmap = isl_basic_map_set_to_empty(bmap);
5983 return bmap;
5986 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5988 struct isl_basic_set *bset;
5989 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5990 bset = isl_basic_set_set_to_empty(bset);
5991 return bset;
5994 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5996 struct isl_basic_map *bmap;
5997 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5998 bmap = isl_basic_map_finalize(bmap);
5999 return bmap;
6002 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6004 struct isl_basic_set *bset;
6005 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6006 bset = isl_basic_set_finalize(bset);
6007 return bset;
6010 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6011 __isl_take isl_space *space)
6013 int i;
6014 isl_size total = isl_space_dim(space, isl_dim_all);
6015 isl_basic_map *bmap;
6017 if (total < 0)
6018 space = isl_space_free(space);
6019 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6020 for (i = 0; i < total; ++i) {
6021 int k = isl_basic_map_alloc_inequality(bmap);
6022 if (k < 0)
6023 goto error;
6024 isl_seq_clr(bmap->ineq[k], 1 + total);
6025 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6027 return bmap;
6028 error:
6029 isl_basic_map_free(bmap);
6030 return NULL;
6033 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6034 __isl_take isl_space *space)
6036 return isl_basic_map_nat_universe(space);
6039 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6041 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6044 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6046 return isl_map_nat_universe(dim);
6049 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6051 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6054 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6056 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6059 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6061 struct isl_map *map;
6062 if (!space)
6063 return NULL;
6064 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6065 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6066 return map;
6069 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6071 struct isl_set *set;
6072 if (!space)
6073 return NULL;
6074 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6075 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6076 return set;
6079 struct isl_map *isl_map_dup(struct isl_map *map)
6081 int i;
6082 struct isl_map *dup;
6084 if (!map)
6085 return NULL;
6086 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6087 for (i = 0; i < map->n; ++i)
6088 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6089 return dup;
6092 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6093 __isl_take isl_basic_map *bmap)
6095 if (!bmap || !map)
6096 goto error;
6097 if (isl_basic_map_plain_is_empty(bmap)) {
6098 isl_basic_map_free(bmap);
6099 return map;
6101 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6102 isl_assert(map->ctx, map->n < map->size, goto error);
6103 map->p[map->n] = bmap;
6104 map->n++;
6105 map = isl_map_unmark_normalized(map);
6106 return map;
6107 error:
6108 if (map)
6109 isl_map_free(map);
6110 if (bmap)
6111 isl_basic_map_free(bmap);
6112 return NULL;
6115 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6117 int i;
6119 if (!map)
6120 return NULL;
6122 if (--map->ref > 0)
6123 return NULL;
6125 clear_caches(map);
6126 isl_ctx_deref(map->ctx);
6127 for (i = 0; i < map->n; ++i)
6128 isl_basic_map_free(map->p[i]);
6129 isl_space_free(map->dim);
6130 free(map);
6132 return NULL;
6135 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6136 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6138 int j;
6139 isl_size total;
6141 total = isl_basic_map_dim(bmap, isl_dim_all);
6142 if (total < 0)
6143 return isl_basic_map_free(bmap);
6145 bmap = isl_basic_map_cow(bmap);
6146 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6147 j = isl_basic_map_alloc_equality(bmap);
6148 if (j < 0)
6149 goto error;
6150 isl_seq_clr(bmap->eq[j] + 1, total);
6151 isl_int_set_si(bmap->eq[j][pos], -1);
6152 isl_int_set_si(bmap->eq[j][0], value);
6153 bmap = isl_basic_map_simplify(bmap);
6154 return isl_basic_map_finalize(bmap);
6155 error:
6156 isl_basic_map_free(bmap);
6157 return NULL;
6160 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6161 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6163 int j;
6164 isl_size total;
6166 total = isl_basic_map_dim(bmap, isl_dim_all);
6167 if (total < 0)
6168 return isl_basic_map_free(bmap);
6170 bmap = isl_basic_map_cow(bmap);
6171 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6172 j = isl_basic_map_alloc_equality(bmap);
6173 if (j < 0)
6174 goto error;
6175 isl_seq_clr(bmap->eq[j] + 1, total);
6176 isl_int_set_si(bmap->eq[j][pos], -1);
6177 isl_int_set(bmap->eq[j][0], value);
6178 bmap = isl_basic_map_simplify(bmap);
6179 return isl_basic_map_finalize(bmap);
6180 error:
6181 isl_basic_map_free(bmap);
6182 return NULL;
6185 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6186 enum isl_dim_type type, unsigned pos, int value)
6188 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6189 return isl_basic_map_free(bmap);
6190 return isl_basic_map_fix_pos_si(bmap,
6191 isl_basic_map_offset(bmap, type) + pos, value);
6194 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6195 enum isl_dim_type type, unsigned pos, isl_int value)
6197 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6198 return isl_basic_map_free(bmap);
6199 return isl_basic_map_fix_pos(bmap,
6200 isl_basic_map_offset(bmap, type) + pos, value);
6203 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6204 * to be equal to "v".
6206 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6207 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6209 if (!bmap || !v)
6210 goto error;
6211 if (!isl_val_is_int(v))
6212 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6213 "expecting integer value", goto error);
6214 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6215 goto error;
6216 pos += isl_basic_map_offset(bmap, type);
6217 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6218 isl_val_free(v);
6219 return bmap;
6220 error:
6221 isl_basic_map_free(bmap);
6222 isl_val_free(v);
6223 return NULL;
6226 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6227 * to be equal to "v".
6229 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6230 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6232 return isl_basic_map_fix_val(bset, type, pos, v);
6235 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6236 enum isl_dim_type type, unsigned pos, int value)
6238 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6239 type, pos, value));
6242 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6243 enum isl_dim_type type, unsigned pos, isl_int value)
6245 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6246 type, pos, value));
6249 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6250 unsigned input, int value)
6252 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6255 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6256 unsigned dim, int value)
6258 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6259 isl_dim_set, dim, value));
6262 /* Remove the basic map at position "i" from "map" if this basic map
6263 * is (obviously) empty.
6265 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6267 isl_bool empty;
6269 if (!map)
6270 return NULL;
6272 empty = isl_basic_map_plain_is_empty(map->p[i]);
6273 if (empty < 0)
6274 return isl_map_free(map);
6275 if (!empty)
6276 return map;
6278 isl_basic_map_free(map->p[i]);
6279 map->n--;
6280 if (i != map->n) {
6281 map->p[i] = map->p[map->n];
6282 map = isl_map_unmark_normalized(map);
6286 return map;
6289 /* Perform "fn" on each basic map of "map", where we may not be holding
6290 * the only reference to "map".
6291 * In particular, "fn" should be a semantics preserving operation
6292 * that we want to apply to all copies of "map". We therefore need
6293 * to be careful not to modify "map" in a way that breaks "map"
6294 * in case anything goes wrong.
6296 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6297 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6299 struct isl_basic_map *bmap;
6300 int i;
6302 if (!map)
6303 return NULL;
6305 for (i = map->n - 1; i >= 0; --i) {
6306 bmap = isl_basic_map_copy(map->p[i]);
6307 bmap = fn(bmap);
6308 if (!bmap)
6309 goto error;
6310 isl_basic_map_free(map->p[i]);
6311 map->p[i] = bmap;
6312 map = remove_if_empty(map, i);
6313 if (!map)
6314 return NULL;
6317 return map;
6318 error:
6319 isl_map_free(map);
6320 return NULL;
6323 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6324 enum isl_dim_type type, unsigned pos, int value)
6326 int i;
6328 map = isl_map_cow(map);
6329 if (isl_map_check_range(map, type, pos, 1) < 0)
6330 return isl_map_free(map);
6331 for (i = map->n - 1; i >= 0; --i) {
6332 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6333 map = remove_if_empty(map, i);
6334 if (!map)
6335 return NULL;
6337 map = isl_map_unmark_normalized(map);
6338 return map;
6341 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6342 enum isl_dim_type type, unsigned pos, int value)
6344 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6347 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6348 enum isl_dim_type type, unsigned pos, isl_int value)
6350 int i;
6352 map = isl_map_cow(map);
6353 if (isl_map_check_range(map, type, pos, 1) < 0)
6354 return isl_map_free(map);
6355 for (i = 0; i < map->n; ++i) {
6356 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6357 if (!map->p[i])
6358 goto error;
6360 map = isl_map_unmark_normalized(map);
6361 return map;
6362 error:
6363 isl_map_free(map);
6364 return NULL;
6367 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6368 enum isl_dim_type type, unsigned pos, isl_int value)
6370 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6373 /* Fix the value of the variable at position "pos" of type "type" of "map"
6374 * to be equal to "v".
6376 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6377 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6379 int i;
6381 map = isl_map_cow(map);
6382 if (!map || !v)
6383 goto error;
6385 if (!isl_val_is_int(v))
6386 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6387 "expecting integer value", goto error);
6388 if (isl_map_check_range(map, type, pos, 1) < 0)
6389 goto error;
6390 for (i = map->n - 1; i >= 0; --i) {
6391 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6392 isl_val_copy(v));
6393 map = remove_if_empty(map, i);
6394 if (!map)
6395 goto error;
6397 map = isl_map_unmark_normalized(map);
6398 isl_val_free(v);
6399 return map;
6400 error:
6401 isl_map_free(map);
6402 isl_val_free(v);
6403 return NULL;
6406 /* Fix the value of the variable at position "pos" of type "type" of "set"
6407 * to be equal to "v".
6409 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6410 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6412 return isl_map_fix_val(set, type, pos, v);
6415 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6416 unsigned input, int value)
6418 return isl_map_fix_si(map, isl_dim_in, input, value);
6421 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6423 return set_from_map(isl_map_fix_si(set_to_map(set),
6424 isl_dim_set, dim, value));
6427 static __isl_give isl_basic_map *basic_map_bound_si(
6428 __isl_take isl_basic_map *bmap,
6429 enum isl_dim_type type, unsigned pos, int value, int upper)
6431 int j;
6432 isl_size total;
6434 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6435 return isl_basic_map_free(bmap);
6436 total = isl_basic_map_dim(bmap, isl_dim_all);
6437 if (total < 0)
6438 return isl_basic_map_free(bmap);
6439 pos += isl_basic_map_offset(bmap, type);
6440 bmap = isl_basic_map_cow(bmap);
6441 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6442 j = isl_basic_map_alloc_inequality(bmap);
6443 if (j < 0)
6444 goto error;
6445 isl_seq_clr(bmap->ineq[j], 1 + total);
6446 if (upper) {
6447 isl_int_set_si(bmap->ineq[j][pos], -1);
6448 isl_int_set_si(bmap->ineq[j][0], value);
6449 } else {
6450 isl_int_set_si(bmap->ineq[j][pos], 1);
6451 isl_int_set_si(bmap->ineq[j][0], -value);
6453 bmap = isl_basic_map_simplify(bmap);
6454 return isl_basic_map_finalize(bmap);
6455 error:
6456 isl_basic_map_free(bmap);
6457 return NULL;
6460 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6461 __isl_take isl_basic_map *bmap,
6462 enum isl_dim_type type, unsigned pos, int value)
6464 return basic_map_bound_si(bmap, type, pos, value, 0);
6467 /* Constrain the values of the given dimension to be no greater than "value".
6469 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6470 __isl_take isl_basic_map *bmap,
6471 enum isl_dim_type type, unsigned pos, int value)
6473 return basic_map_bound_si(bmap, type, pos, value, 1);
6476 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6477 enum isl_dim_type type, unsigned pos, int value, int upper)
6479 int i;
6481 map = isl_map_cow(map);
6482 if (isl_map_check_range(map, type, pos, 1) < 0)
6483 return isl_map_free(map);
6484 for (i = 0; i < map->n; ++i) {
6485 map->p[i] = basic_map_bound_si(map->p[i],
6486 type, pos, value, upper);
6487 if (!map->p[i])
6488 goto error;
6490 map = isl_map_unmark_normalized(map);
6491 return map;
6492 error:
6493 isl_map_free(map);
6494 return NULL;
6497 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6498 enum isl_dim_type type, unsigned pos, int value)
6500 return map_bound_si(map, type, pos, value, 0);
6503 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6504 enum isl_dim_type type, unsigned pos, int value)
6506 return map_bound_si(map, type, pos, value, 1);
6509 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6510 enum isl_dim_type type, unsigned pos, int value)
6512 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6513 type, pos, value));
6516 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6517 enum isl_dim_type type, unsigned pos, int value)
6519 return isl_map_upper_bound_si(set, type, pos, value);
6522 /* Bound the given variable of "bmap" from below (or above is "upper"
6523 * is set) to "value".
6525 static __isl_give isl_basic_map *basic_map_bound(
6526 __isl_take isl_basic_map *bmap,
6527 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6529 int j;
6530 isl_size total;
6532 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6533 return isl_basic_map_free(bmap);
6534 total = isl_basic_map_dim(bmap, isl_dim_all);
6535 if (total < 0)
6536 return isl_basic_map_free(bmap);
6537 pos += isl_basic_map_offset(bmap, type);
6538 bmap = isl_basic_map_cow(bmap);
6539 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6540 j = isl_basic_map_alloc_inequality(bmap);
6541 if (j < 0)
6542 goto error;
6543 isl_seq_clr(bmap->ineq[j], 1 + total);
6544 if (upper) {
6545 isl_int_set_si(bmap->ineq[j][pos], -1);
6546 isl_int_set(bmap->ineq[j][0], value);
6547 } else {
6548 isl_int_set_si(bmap->ineq[j][pos], 1);
6549 isl_int_neg(bmap->ineq[j][0], value);
6551 bmap = isl_basic_map_simplify(bmap);
6552 return isl_basic_map_finalize(bmap);
6553 error:
6554 isl_basic_map_free(bmap);
6555 return NULL;
6558 /* Bound the given variable of "map" from below (or above is "upper"
6559 * is set) to "value".
6561 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6562 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6564 int i;
6566 map = isl_map_cow(map);
6567 if (isl_map_check_range(map, type, pos, 1) < 0)
6568 return isl_map_free(map);
6569 for (i = map->n - 1; i >= 0; --i) {
6570 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6571 map = remove_if_empty(map, i);
6572 if (!map)
6573 return NULL;
6575 map = isl_map_unmark_normalized(map);
6576 return map;
6579 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6580 enum isl_dim_type type, unsigned pos, isl_int value)
6582 return map_bound(map, type, pos, value, 0);
6585 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6586 enum isl_dim_type type, unsigned pos, isl_int value)
6588 return map_bound(map, type, pos, value, 1);
6591 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6592 enum isl_dim_type type, unsigned pos, isl_int value)
6594 return isl_map_lower_bound(set, type, pos, value);
6597 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6598 enum isl_dim_type type, unsigned pos, isl_int value)
6600 return isl_map_upper_bound(set, type, pos, value);
6603 /* Force the values of the variable at position "pos" of type "type" of "set"
6604 * to be no smaller than "value".
6606 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6607 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6609 if (!value)
6610 goto error;
6611 if (!isl_val_is_int(value))
6612 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6613 "expecting integer value", goto error);
6614 set = isl_set_lower_bound(set, type, pos, value->n);
6615 isl_val_free(value);
6616 return set;
6617 error:
6618 isl_val_free(value);
6619 isl_set_free(set);
6620 return NULL;
6623 /* Force the values of the variable at position "pos" of type "type" of "set"
6624 * to be no greater than "value".
6626 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6627 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6629 if (!value)
6630 goto error;
6631 if (!isl_val_is_int(value))
6632 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6633 "expecting integer value", goto error);
6634 set = isl_set_upper_bound(set, type, pos, value->n);
6635 isl_val_free(value);
6636 return set;
6637 error:
6638 isl_val_free(value);
6639 isl_set_free(set);
6640 return NULL;
6643 /* Bound the given variable of "bset" from below (or above is "upper"
6644 * is set) to "value".
6646 static __isl_give isl_basic_set *isl_basic_set_bound(
6647 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6648 isl_int value, int upper)
6650 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6651 type, pos, value, upper));
6654 /* Bound the given variable of "bset" from below (or above is "upper"
6655 * is set) to "value".
6657 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6658 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6659 __isl_take isl_val *value, int upper)
6661 if (!value)
6662 goto error;
6663 if (!isl_val_is_int(value))
6664 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6665 "expecting integer value", goto error);
6666 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6667 isl_val_free(value);
6668 return bset;
6669 error:
6670 isl_val_free(value);
6671 isl_basic_set_free(bset);
6672 return NULL;
6675 /* Bound the given variable of "bset" from below to "value".
6677 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6678 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6679 __isl_take isl_val *value)
6681 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6684 /* Bound the given variable of "bset" from above to "value".
6686 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6687 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6688 __isl_take isl_val *value)
6690 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6693 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6695 int i;
6697 map = isl_map_cow(map);
6698 if (!map)
6699 return NULL;
6701 map->dim = isl_space_reverse(map->dim);
6702 if (!map->dim)
6703 goto error;
6704 for (i = 0; i < map->n; ++i) {
6705 map->p[i] = isl_basic_map_reverse(map->p[i]);
6706 if (!map->p[i])
6707 goto error;
6709 map = isl_map_unmark_normalized(map);
6710 return map;
6711 error:
6712 isl_map_free(map);
6713 return NULL;
6716 #undef TYPE
6717 #define TYPE isl_pw_multi_aff
6718 #undef SUFFIX
6719 #define SUFFIX _pw_multi_aff
6720 #undef EMPTY
6721 #define EMPTY isl_pw_multi_aff_empty
6722 #undef ADD
6723 #define ADD isl_pw_multi_aff_union_add
6724 #include "isl_map_lexopt_templ.c"
6726 /* Given a map "map", compute the lexicographically minimal
6727 * (or maximal) image element for each domain element in dom,
6728 * in the form of an isl_pw_multi_aff.
6729 * If "empty" is not NULL, then set *empty to those elements in dom that
6730 * do not have an image element.
6731 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6732 * should be computed over the domain of "map". "empty" is also NULL
6733 * in this case.
6735 * We first compute the lexicographically minimal or maximal element
6736 * in the first basic map. This results in a partial solution "res"
6737 * and a subset "todo" of dom that still need to be handled.
6738 * We then consider each of the remaining maps in "map" and successively
6739 * update both "res" and "todo".
6740 * If "empty" is NULL, then the todo sets are not needed and therefore
6741 * also not computed.
6743 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6744 __isl_take isl_map *map, __isl_take isl_set *dom,
6745 __isl_give isl_set **empty, unsigned flags)
6747 int i;
6748 int full;
6749 isl_pw_multi_aff *res;
6750 isl_set *todo;
6752 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6753 if (!map || (!full && !dom))
6754 goto error;
6756 if (isl_map_plain_is_empty(map)) {
6757 if (empty)
6758 *empty = dom;
6759 else
6760 isl_set_free(dom);
6761 return isl_pw_multi_aff_from_map(map);
6764 res = basic_map_partial_lexopt_pw_multi_aff(
6765 isl_basic_map_copy(map->p[0]),
6766 isl_set_copy(dom), empty, flags);
6768 if (empty)
6769 todo = *empty;
6770 for (i = 1; i < map->n; ++i) {
6771 isl_pw_multi_aff *res_i;
6773 res_i = basic_map_partial_lexopt_pw_multi_aff(
6774 isl_basic_map_copy(map->p[i]),
6775 isl_set_copy(dom), empty, flags);
6777 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6778 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6779 else
6780 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6782 if (empty)
6783 todo = isl_set_intersect(todo, *empty);
6786 isl_set_free(dom);
6787 isl_map_free(map);
6789 if (empty)
6790 *empty = todo;
6792 return res;
6793 error:
6794 if (empty)
6795 *empty = NULL;
6796 isl_set_free(dom);
6797 isl_map_free(map);
6798 return NULL;
6801 #undef TYPE
6802 #define TYPE isl_map
6803 #undef SUFFIX
6804 #define SUFFIX
6805 #undef EMPTY
6806 #define EMPTY isl_map_empty
6807 #undef ADD
6808 #define ADD isl_map_union_disjoint
6809 #include "isl_map_lexopt_templ.c"
6811 /* Given a map "map", compute the lexicographically minimal
6812 * (or maximal) image element for each domain element in "dom",
6813 * in the form of an isl_map.
6814 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6815 * do not have an image element.
6816 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6817 * should be computed over the domain of "map". "empty" is also NULL
6818 * in this case.
6820 * If the input consists of more than one disjunct, then first
6821 * compute the desired result in the form of an isl_pw_multi_aff and
6822 * then convert that into an isl_map.
6824 * This function used to have an explicit implementation in terms
6825 * of isl_maps, but it would continually intersect the domains of
6826 * partial results with the complement of the domain of the next
6827 * partial solution, potentially leading to an explosion in the number
6828 * of disjuncts if there are several disjuncts in the input.
6829 * An even earlier implementation of this function would look for
6830 * better results in the domain of the partial result and for extra
6831 * results in the complement of this domain, which would lead to
6832 * even more splintering.
6834 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6835 __isl_take isl_map *map, __isl_take isl_set *dom,
6836 __isl_give isl_set **empty, unsigned flags)
6838 int full;
6839 struct isl_map *res;
6840 isl_pw_multi_aff *pma;
6842 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6843 if (!map || (!full && !dom))
6844 goto error;
6846 if (isl_map_plain_is_empty(map)) {
6847 if (empty)
6848 *empty = dom;
6849 else
6850 isl_set_free(dom);
6851 return map;
6854 if (map->n == 1) {
6855 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6856 dom, empty, flags);
6857 isl_map_free(map);
6858 return res;
6861 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6862 flags);
6863 return isl_map_from_pw_multi_aff(pma);
6864 error:
6865 if (empty)
6866 *empty = NULL;
6867 isl_set_free(dom);
6868 isl_map_free(map);
6869 return NULL;
6872 __isl_give isl_map *isl_map_partial_lexmax(
6873 __isl_take isl_map *map, __isl_take isl_set *dom,
6874 __isl_give isl_set **empty)
6876 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6879 __isl_give isl_map *isl_map_partial_lexmin(
6880 __isl_take isl_map *map, __isl_take isl_set *dom,
6881 __isl_give isl_set **empty)
6883 return isl_map_partial_lexopt(map, dom, empty, 0);
6886 __isl_give isl_set *isl_set_partial_lexmin(
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_lexmin(set_to_map(set),
6891 dom, empty));
6894 __isl_give isl_set *isl_set_partial_lexmax(
6895 __isl_take isl_set *set, __isl_take isl_set *dom,
6896 __isl_give isl_set **empty)
6898 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6899 dom, empty));
6902 /* Compute the lexicographic minimum (or maximum if "flags" includes
6903 * ISL_OPT_MAX) of "bset" over its parametric domain.
6905 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6906 unsigned flags)
6908 return isl_basic_map_lexopt(bset, flags);
6911 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6913 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6916 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6918 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6921 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6923 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6926 /* Compute the lexicographic minimum of "bset" over its parametric domain
6927 * for the purpose of quantifier elimination.
6928 * That is, find an explicit representation for all the existentially
6929 * quantified variables in "bset" by computing their lexicographic
6930 * minimum.
6932 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6933 __isl_take isl_basic_set *bset)
6935 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6938 /* Given a basic map with one output dimension, compute the minimum or
6939 * maximum of that dimension as an isl_pw_aff.
6941 * Compute the optimum as a lexicographic optimum over the single
6942 * output dimension and extract the single isl_pw_aff from the result.
6944 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6945 int max)
6947 isl_pw_multi_aff *pma;
6948 isl_pw_aff *pwaff;
6950 bmap = isl_basic_map_copy(bmap);
6951 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6952 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6953 isl_pw_multi_aff_free(pma);
6955 return pwaff;
6958 /* Compute the minimum or maximum of the given output dimension
6959 * as a function of the parameters and the input dimensions,
6960 * but independently of the other output dimensions.
6962 * We first project out the other output dimension and then compute
6963 * the "lexicographic" maximum in each basic map, combining the results
6964 * using isl_pw_aff_union_max.
6966 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6967 int max)
6969 int i;
6970 isl_pw_aff *pwaff;
6971 isl_size n_out;
6973 n_out = isl_map_dim(map, isl_dim_out);
6974 if (n_out < 0)
6975 map = isl_map_free(map);
6976 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6977 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6978 if (!map)
6979 return NULL;
6981 if (map->n == 0) {
6982 isl_space *space = isl_map_get_space(map);
6983 isl_map_free(map);
6984 return isl_pw_aff_empty(space);
6987 pwaff = basic_map_dim_opt(map->p[0], max);
6988 for (i = 1; i < map->n; ++i) {
6989 isl_pw_aff *pwaff_i;
6991 pwaff_i = basic_map_dim_opt(map->p[i], max);
6992 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6995 isl_map_free(map);
6997 return pwaff;
7000 /* Compute the minimum of the given output dimension as a function of the
7001 * parameters and input dimensions, but independently of
7002 * the other output dimensions.
7004 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7006 return map_dim_opt(map, pos, 0);
7009 /* Compute the maximum of the given output dimension as a function of the
7010 * parameters and input dimensions, but independently of
7011 * the other output dimensions.
7013 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7015 return map_dim_opt(map, pos, 1);
7018 /* Compute the minimum or maximum of the given set dimension
7019 * as a function of the parameters,
7020 * but independently of the other set dimensions.
7022 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7023 int max)
7025 return map_dim_opt(set, pos, max);
7028 /* Compute the maximum 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_max(__isl_take isl_set *set, int pos)
7033 return set_dim_opt(set, pos, 1);
7036 /* Compute the minimum of the given set dimension as a function of the
7037 * parameters, but independently of the other set dimensions.
7039 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7041 return set_dim_opt(set, pos, 0);
7044 /* Apply a preimage specified by "mat" on the parameters of "bset".
7045 * bset is assumed to have only parameters and divs.
7047 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7048 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7050 isl_size nparam;
7052 nparam = isl_basic_set_dim(bset, isl_dim_param);
7053 if (nparam < 0 || !mat)
7054 goto error;
7056 bset->dim = isl_space_cow(bset->dim);
7057 if (!bset->dim)
7058 goto error;
7060 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7062 bset->dim->nparam = 0;
7063 bset->dim->n_out = nparam;
7064 bset = isl_basic_set_preimage(bset, mat);
7065 if (bset) {
7066 bset->dim->nparam = bset->dim->n_out;
7067 bset->dim->n_out = 0;
7069 return bset;
7070 error:
7071 isl_mat_free(mat);
7072 isl_basic_set_free(bset);
7073 return NULL;
7076 /* Apply a preimage specified by "mat" on the parameters of "set".
7077 * set is assumed to have only parameters and divs.
7079 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7080 __isl_take isl_mat *mat)
7082 isl_space *space;
7083 isl_size nparam;
7085 nparam = isl_set_dim(set, isl_dim_param);
7086 if (nparam < 0 || !mat)
7087 goto error;
7089 if (mat->n_row != 1 + nparam)
7090 isl_die(isl_set_get_ctx(set), isl_error_internal,
7091 "unexpected number of rows", goto error);
7093 space = isl_set_get_space(set);
7094 space = isl_space_move_dims(space, isl_dim_set, 0,
7095 isl_dim_param, 0, nparam);
7096 set = isl_set_reset_space(set, space);
7097 set = isl_set_preimage(set, mat);
7098 nparam = isl_set_dim(set, isl_dim_out);
7099 if (nparam < 0)
7100 set = isl_set_free(set);
7101 space = isl_set_get_space(set);
7102 space = isl_space_move_dims(space, isl_dim_param, 0,
7103 isl_dim_out, 0, nparam);
7104 set = isl_set_reset_space(set, space);
7105 return set;
7106 error:
7107 isl_mat_free(mat);
7108 isl_set_free(set);
7109 return NULL;
7112 /* Intersect the basic set "bset" with the affine space specified by the
7113 * equalities in "eq".
7115 static __isl_give isl_basic_set *basic_set_append_equalities(
7116 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7118 int i, k;
7119 unsigned len;
7121 if (!bset || !eq)
7122 goto error;
7124 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7125 eq->n_row, 0);
7126 if (!bset)
7127 goto error;
7129 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7130 for (i = 0; i < eq->n_row; ++i) {
7131 k = isl_basic_set_alloc_equality(bset);
7132 if (k < 0)
7133 goto error;
7134 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7135 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7137 isl_mat_free(eq);
7139 bset = isl_basic_set_gauss(bset, NULL);
7140 bset = isl_basic_set_finalize(bset);
7142 return bset;
7143 error:
7144 isl_mat_free(eq);
7145 isl_basic_set_free(bset);
7146 return NULL;
7149 /* Intersect the set "set" with the affine space specified by the
7150 * equalities in "eq".
7152 static struct isl_set *set_append_equalities(struct isl_set *set,
7153 struct isl_mat *eq)
7155 int i;
7157 if (!set || !eq)
7158 goto error;
7160 for (i = 0; i < set->n; ++i) {
7161 set->p[i] = basic_set_append_equalities(set->p[i],
7162 isl_mat_copy(eq));
7163 if (!set->p[i])
7164 goto error;
7166 isl_mat_free(eq);
7167 return set;
7168 error:
7169 isl_mat_free(eq);
7170 isl_set_free(set);
7171 return NULL;
7174 /* Given a basic set "bset" that only involves parameters and existentially
7175 * quantified variables, return the index of the first equality
7176 * that only involves parameters. If there is no such equality then
7177 * return bset->n_eq.
7179 * This function assumes that isl_basic_set_gauss has been called on "bset".
7181 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7183 int i, j;
7184 isl_size nparam, n_div;
7186 nparam = isl_basic_set_dim(bset, isl_dim_param);
7187 n_div = isl_basic_set_dim(bset, isl_dim_div);
7188 if (nparam < 0 || n_div < 0)
7189 return -1;
7191 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7192 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7193 ++i;
7196 return i;
7199 /* Compute an explicit representation for the existentially quantified
7200 * variables in "bset" by computing the "minimal value" of the set
7201 * variables. Since there are no set variables, the computation of
7202 * the minimal value essentially computes an explicit representation
7203 * of the non-empty part(s) of "bset".
7205 * The input only involves parameters and existentially quantified variables.
7206 * All equalities among parameters have been removed.
7208 * Since the existentially quantified variables in the result are in general
7209 * going to be different from those in the input, we first replace
7210 * them by the minimal number of variables based on their equalities.
7211 * This should simplify the parametric integer programming.
7213 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7215 isl_morph *morph1, *morph2;
7216 isl_set *set;
7217 isl_size n;
7219 if (!bset)
7220 return NULL;
7221 if (bset->n_eq == 0)
7222 return isl_basic_set_lexmin_compute_divs(bset);
7224 morph1 = isl_basic_set_parameter_compression(bset);
7225 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7226 bset = isl_basic_set_lift(bset);
7227 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7228 bset = isl_morph_basic_set(morph2, bset);
7229 n = isl_basic_set_dim(bset, isl_dim_set);
7230 if (n < 0)
7231 bset = isl_basic_set_free(bset);
7232 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7234 set = isl_basic_set_lexmin_compute_divs(bset);
7236 set = isl_morph_set(isl_morph_inverse(morph1), set);
7238 return set;
7241 /* Project the given basic set onto its parameter domain, possibly introducing
7242 * new, explicit, existential variables in the constraints.
7243 * The input has parameters and (possibly implicit) existential variables.
7244 * The output has the same parameters, but only
7245 * explicit existentially quantified variables.
7247 * The actual projection is performed by pip, but pip doesn't seem
7248 * to like equalities very much, so we first remove the equalities
7249 * among the parameters by performing a variable compression on
7250 * the parameters. Afterward, an inverse transformation is performed
7251 * and the equalities among the parameters are inserted back in.
7253 * The variable compression on the parameters may uncover additional
7254 * equalities that were only implicit before. We therefore check
7255 * if there are any new parameter equalities in the result and
7256 * if so recurse. The removal of parameter equalities is required
7257 * for the parameter compression performed by base_compute_divs.
7259 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7261 int i;
7262 struct isl_mat *eq;
7263 struct isl_mat *T, *T2;
7264 struct isl_set *set;
7265 isl_size nparam;
7267 bset = isl_basic_set_cow(bset);
7268 if (!bset)
7269 return NULL;
7271 if (bset->n_eq == 0)
7272 return base_compute_divs(bset);
7274 bset = isl_basic_set_gauss(bset, NULL);
7275 if (!bset)
7276 return NULL;
7277 if (isl_basic_set_plain_is_empty(bset))
7278 return isl_set_from_basic_set(bset);
7280 i = first_parameter_equality(bset);
7281 if (i == bset->n_eq)
7282 return base_compute_divs(bset);
7284 nparam = isl_basic_set_dim(bset, isl_dim_param);
7285 if (nparam < 0)
7286 return isl_set_from_basic_set(isl_basic_set_free(bset));
7287 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7288 0, 1 + nparam);
7289 eq = isl_mat_cow(eq);
7290 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7291 if (T && T->n_col == 0) {
7292 isl_mat_free(T);
7293 isl_mat_free(T2);
7294 isl_mat_free(eq);
7295 bset = isl_basic_set_set_to_empty(bset);
7296 return isl_set_from_basic_set(bset);
7298 bset = basic_set_parameter_preimage(bset, T);
7300 i = first_parameter_equality(bset);
7301 if (!bset)
7302 set = NULL;
7303 else if (i == bset->n_eq)
7304 set = base_compute_divs(bset);
7305 else
7306 set = parameter_compute_divs(bset);
7307 set = set_parameter_preimage(set, T2);
7308 set = set_append_equalities(set, eq);
7309 return set;
7312 /* Insert the divs from "ls" before those of "bmap".
7314 * The number of columns is not changed, which means that the last
7315 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7316 * The caller is responsible for removing the same number of dimensions
7317 * from the space of "bmap".
7319 static __isl_give isl_basic_map *insert_divs_from_local_space(
7320 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7322 int i;
7323 isl_size n_div;
7324 int old_n_div;
7326 n_div = isl_local_space_dim(ls, isl_dim_div);
7327 if (n_div < 0)
7328 return isl_basic_map_free(bmap);
7329 if (n_div == 0)
7330 return bmap;
7332 old_n_div = bmap->n_div;
7333 bmap = insert_div_rows(bmap, n_div);
7334 if (!bmap)
7335 return NULL;
7337 for (i = 0; i < n_div; ++i) {
7338 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7339 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7342 return bmap;
7345 /* Replace the space of "bmap" by the space and divs of "ls".
7347 * If "ls" has any divs, then we simplify the result since we may
7348 * have discovered some additional equalities that could simplify
7349 * the div expressions.
7351 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7352 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7354 isl_size n_div;
7356 bmap = isl_basic_map_cow(bmap);
7357 n_div = isl_local_space_dim(ls, isl_dim_div);
7358 if (!bmap || n_div < 0)
7359 goto error;
7361 bmap = insert_divs_from_local_space(bmap, ls);
7362 if (!bmap)
7363 goto error;
7365 isl_space_free(bmap->dim);
7366 bmap->dim = isl_local_space_get_space(ls);
7367 if (!bmap->dim)
7368 goto error;
7370 isl_local_space_free(ls);
7371 if (n_div > 0)
7372 bmap = isl_basic_map_simplify(bmap);
7373 bmap = isl_basic_map_finalize(bmap);
7374 return bmap;
7375 error:
7376 isl_basic_map_free(bmap);
7377 isl_local_space_free(ls);
7378 return NULL;
7381 /* Replace the space of "map" by the space and divs of "ls".
7383 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7384 __isl_take isl_local_space *ls)
7386 int i;
7388 map = isl_map_cow(map);
7389 if (!map || !ls)
7390 goto error;
7392 for (i = 0; i < map->n; ++i) {
7393 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7394 isl_local_space_copy(ls));
7395 if (!map->p[i])
7396 goto error;
7398 isl_space_free(map->dim);
7399 map->dim = isl_local_space_get_space(ls);
7400 if (!map->dim)
7401 goto error;
7403 isl_local_space_free(ls);
7404 return map;
7405 error:
7406 isl_local_space_free(ls);
7407 isl_map_free(map);
7408 return NULL;
7411 /* Compute an explicit representation for the existentially
7412 * quantified variables for which do not know any explicit representation yet.
7414 * We first sort the existentially quantified variables so that the
7415 * existentially quantified variables for which we already have an explicit
7416 * representation are placed before those for which we do not.
7417 * The input dimensions, the output dimensions and the existentially
7418 * quantified variables for which we already have an explicit
7419 * representation are then turned into parameters.
7420 * compute_divs returns a map with the same parameters and
7421 * no input or output dimensions and the dimension specification
7422 * is reset to that of the input, including the existentially quantified
7423 * variables for which we already had an explicit representation.
7425 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7427 struct isl_basic_set *bset;
7428 struct isl_set *set;
7429 struct isl_map *map;
7430 isl_space *space;
7431 isl_local_space *ls;
7432 isl_size nparam;
7433 isl_size n_in;
7434 isl_size n_out;
7435 int n_known;
7436 int i;
7438 bmap = isl_basic_map_sort_divs(bmap);
7439 bmap = isl_basic_map_cow(bmap);
7440 if (!bmap)
7441 return NULL;
7443 n_known = isl_basic_map_first_unknown_div(bmap);
7444 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7445 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7446 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7447 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7448 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7450 space = isl_space_set_alloc(bmap->ctx,
7451 nparam + n_in + n_out + n_known, 0);
7452 if (!space)
7453 goto error;
7455 ls = isl_basic_map_get_local_space(bmap);
7456 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7457 n_known, bmap->n_div - n_known);
7458 if (n_known > 0) {
7459 for (i = n_known; i < bmap->n_div; ++i)
7460 swap_div(bmap, i - n_known, i);
7461 bmap->n_div -= n_known;
7462 bmap->extra -= n_known;
7464 bmap = isl_basic_map_reset_space(bmap, space);
7465 bset = bset_from_bmap(bmap);
7467 set = parameter_compute_divs(bset);
7468 map = set_to_map(set);
7469 map = replace_space_by_local_space(map, ls);
7471 return map;
7472 error:
7473 isl_basic_map_free(bmap);
7474 return NULL;
7477 /* Remove the explicit representation of local variable "div",
7478 * if there is any.
7480 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7481 __isl_take isl_basic_map *bmap, int div)
7483 isl_bool unknown;
7485 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7486 if (unknown < 0)
7487 return isl_basic_map_free(bmap);
7488 if (unknown)
7489 return bmap;
7491 bmap = isl_basic_map_cow(bmap);
7492 if (!bmap)
7493 return NULL;
7494 isl_int_set_si(bmap->div[div][0], 0);
7495 return bmap;
7498 /* Is local variable "div" of "bmap" marked as not having an explicit
7499 * representation?
7500 * Note that even if "div" is not marked in this way and therefore
7501 * has an explicit representation, this representation may still
7502 * depend (indirectly) on other local variables that do not
7503 * have an explicit representation.
7505 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7506 int div)
7508 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7509 return isl_bool_error;
7510 return isl_int_is_zero(bmap->div[div][0]);
7513 /* Return the position of the first local variable that does not
7514 * have an explicit representation.
7515 * Return the total number of local variables if they all have
7516 * an explicit representation.
7517 * Return -1 on error.
7519 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7521 int i;
7523 if (!bmap)
7524 return -1;
7526 for (i = 0; i < bmap->n_div; ++i) {
7527 if (!isl_basic_map_div_is_known(bmap, i))
7528 return i;
7530 return bmap->n_div;
7533 /* Return the position of the first local variable that does not
7534 * have an explicit representation.
7535 * Return the total number of local variables if they all have
7536 * an explicit representation.
7537 * Return -1 on error.
7539 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7541 return isl_basic_map_first_unknown_div(bset);
7544 /* Does "bmap" have an explicit representation for all local variables?
7546 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7548 int first;
7549 isl_size n;
7551 n = isl_basic_map_dim(bmap, isl_dim_div);
7552 first = isl_basic_map_first_unknown_div(bmap);
7553 if (n < 0 || first < 0)
7554 return isl_bool_error;
7555 return first == n;
7558 /* Do all basic maps in "map" have an explicit representation
7559 * for all local variables?
7561 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7563 int i;
7565 if (!map)
7566 return isl_bool_error;
7568 for (i = 0; i < map->n; ++i) {
7569 int known = isl_basic_map_divs_known(map->p[i]);
7570 if (known <= 0)
7571 return known;
7574 return isl_bool_true;
7577 /* If bmap contains any unknown divs, then compute explicit
7578 * expressions for them. However, this computation may be
7579 * quite expensive, so first try to remove divs that aren't
7580 * strictly needed.
7582 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7584 int known;
7585 struct isl_map *map;
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 bmap = isl_basic_map_drop_redundant_divs(bmap);
7595 known = isl_basic_map_divs_known(bmap);
7596 if (known < 0)
7597 goto error;
7598 if (known)
7599 return isl_map_from_basic_map(bmap);
7601 map = compute_divs(bmap);
7602 return map;
7603 error:
7604 isl_basic_map_free(bmap);
7605 return NULL;
7608 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7610 int i;
7611 int known;
7612 struct isl_map *res;
7614 if (!map)
7615 return NULL;
7616 if (map->n == 0)
7617 return map;
7619 known = isl_map_divs_known(map);
7620 if (known < 0) {
7621 isl_map_free(map);
7622 return NULL;
7624 if (known)
7625 return map;
7627 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7628 for (i = 1 ; i < map->n; ++i) {
7629 struct isl_map *r2;
7630 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7631 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7632 res = isl_map_union_disjoint(res, r2);
7633 else
7634 res = isl_map_union(res, r2);
7636 isl_map_free(map);
7638 return res;
7641 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7643 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7646 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7648 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7651 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7653 int i;
7654 struct isl_set *set;
7656 if (!map)
7657 goto error;
7659 map = isl_map_cow(map);
7660 if (!map)
7661 return NULL;
7663 set = set_from_map(map);
7664 set->dim = isl_space_domain(set->dim);
7665 if (!set->dim)
7666 goto error;
7667 for (i = 0; i < map->n; ++i) {
7668 set->p[i] = isl_basic_map_domain(map->p[i]);
7669 if (!set->p[i])
7670 goto error;
7672 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7673 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7674 return set;
7675 error:
7676 isl_map_free(map);
7677 return NULL;
7680 /* Return the union of "map1" and "map2", where we assume for now that
7681 * "map1" and "map2" are disjoint. Note that the basic maps inside
7682 * "map1" or "map2" may not be disjoint from each other.
7683 * Also note that this function is also called from isl_map_union,
7684 * which takes care of handling the situation where "map1" and "map2"
7685 * may not be disjoint.
7687 * If one of the inputs is empty, we can simply return the other input.
7688 * Similarly, if one of the inputs is universal, then it is equal to the union.
7690 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7691 __isl_take isl_map *map2)
7693 int i;
7694 unsigned flags = 0;
7695 struct isl_map *map = NULL;
7696 int is_universe;
7698 if (!map1 || !map2)
7699 goto error;
7701 if (!isl_space_is_equal(map1->dim, map2->dim))
7702 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7703 "spaces don't match", goto error);
7705 if (map1->n == 0) {
7706 isl_map_free(map1);
7707 return map2;
7709 if (map2->n == 0) {
7710 isl_map_free(map2);
7711 return map1;
7714 is_universe = isl_map_plain_is_universe(map1);
7715 if (is_universe < 0)
7716 goto error;
7717 if (is_universe) {
7718 isl_map_free(map2);
7719 return map1;
7722 is_universe = isl_map_plain_is_universe(map2);
7723 if (is_universe < 0)
7724 goto error;
7725 if (is_universe) {
7726 isl_map_free(map1);
7727 return map2;
7730 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7731 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7732 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7734 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7735 map1->n + map2->n, flags);
7736 if (!map)
7737 goto error;
7738 for (i = 0; i < map1->n; ++i) {
7739 map = isl_map_add_basic_map(map,
7740 isl_basic_map_copy(map1->p[i]));
7741 if (!map)
7742 goto error;
7744 for (i = 0; i < map2->n; ++i) {
7745 map = isl_map_add_basic_map(map,
7746 isl_basic_map_copy(map2->p[i]));
7747 if (!map)
7748 goto error;
7750 isl_map_free(map1);
7751 isl_map_free(map2);
7752 return map;
7753 error:
7754 isl_map_free(map);
7755 isl_map_free(map1);
7756 isl_map_free(map2);
7757 return NULL;
7760 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7761 * guaranteed to be disjoint by the caller.
7763 * Note that this functions is called from within isl_map_make_disjoint,
7764 * so we have to be careful not to touch the constraints of the inputs
7765 * in any way.
7767 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7768 __isl_take isl_map *map2)
7770 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7773 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7774 * not be disjoint. The parameters are assumed to have been aligned.
7776 * We currently simply call map_union_disjoint, the internal operation
7777 * of which does not really depend on the inputs being disjoint.
7778 * If the result contains more than one basic map, then we clear
7779 * the disjoint flag since the result may contain basic maps from
7780 * both inputs and these are not guaranteed to be disjoint.
7782 * As a special case, if "map1" and "map2" are obviously equal,
7783 * then we simply return "map1".
7785 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7786 __isl_take isl_map *map2)
7788 int equal;
7790 if (!map1 || !map2)
7791 goto error;
7793 equal = isl_map_plain_is_equal(map1, map2);
7794 if (equal < 0)
7795 goto error;
7796 if (equal) {
7797 isl_map_free(map2);
7798 return map1;
7801 map1 = map_union_disjoint(map1, map2);
7802 if (!map1)
7803 return NULL;
7804 if (map1->n > 1)
7805 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7806 return map1;
7807 error:
7808 isl_map_free(map1);
7809 isl_map_free(map2);
7810 return NULL;
7813 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7814 * not be disjoint.
7816 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7817 __isl_take isl_map *map2)
7819 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7822 __isl_give isl_set *isl_set_union_disjoint(
7823 __isl_take isl_set *set1, __isl_take isl_set *set2)
7825 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7826 set_to_map(set2)));
7829 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7831 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7834 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7835 * the results.
7837 * "map" and "set" are assumed to be compatible and non-NULL.
7839 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7840 __isl_take isl_set *set,
7841 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7842 __isl_take isl_basic_set *bset))
7844 unsigned flags = 0;
7845 struct isl_map *result;
7846 int i, j;
7848 if (isl_set_plain_is_universe(set)) {
7849 isl_set_free(set);
7850 return map;
7853 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7854 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7855 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7857 result = isl_map_alloc_space(isl_space_copy(map->dim),
7858 map->n * set->n, flags);
7859 for (i = 0; result && i < map->n; ++i)
7860 for (j = 0; j < set->n; ++j) {
7861 result = isl_map_add_basic_map(result,
7862 fn(isl_basic_map_copy(map->p[i]),
7863 isl_basic_set_copy(set->p[j])));
7864 if (!result)
7865 break;
7868 isl_map_free(map);
7869 isl_set_free(set);
7870 return result;
7873 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7874 __isl_take isl_set *set)
7876 isl_bool ok;
7878 ok = isl_map_compatible_range(map, set);
7879 if (ok < 0)
7880 goto error;
7881 if (!ok)
7882 isl_die(set->ctx, isl_error_invalid,
7883 "incompatible spaces", goto error);
7885 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7886 error:
7887 isl_map_free(map);
7888 isl_set_free(set);
7889 return NULL;
7892 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7893 __isl_take isl_set *set)
7895 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7898 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7899 __isl_take isl_set *set)
7901 isl_bool ok;
7903 ok = isl_map_compatible_domain(map, set);
7904 if (ok < 0)
7905 goto error;
7906 if (!ok)
7907 isl_die(set->ctx, isl_error_invalid,
7908 "incompatible spaces", goto error);
7910 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7911 error:
7912 isl_map_free(map);
7913 isl_set_free(set);
7914 return NULL;
7917 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7918 __isl_take isl_set *set)
7920 return isl_map_align_params_map_map_and(map, set,
7921 &map_intersect_domain);
7924 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7925 * in the space B -> C, return the intersection.
7926 * The parameters are assumed to have been aligned.
7928 * The map "factor" is first extended to a map living in the space
7929 * [A -> B] -> C and then a regular intersection is computed.
7931 static __isl_give isl_map *map_intersect_domain_factor_range(
7932 __isl_take isl_map *map, __isl_take isl_map *factor)
7934 isl_space *space;
7935 isl_map *ext_factor;
7937 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7938 ext_factor = isl_map_universe(space);
7939 ext_factor = isl_map_domain_product(ext_factor, factor);
7940 return map_intersect(map, ext_factor);
7943 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7944 * in the space B -> C, return the intersection.
7946 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7947 __isl_take isl_map *map, __isl_take isl_map *factor)
7949 return isl_map_align_params_map_map_and(map, factor,
7950 &map_intersect_domain_factor_range);
7953 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7954 * in the space A -> C, return the intersection.
7956 * The map "factor" is first extended to a map living in the space
7957 * A -> [B -> C] and then a regular intersection is computed.
7959 static __isl_give isl_map *map_intersect_range_factor_range(
7960 __isl_take isl_map *map, __isl_take isl_map *factor)
7962 isl_space *space;
7963 isl_map *ext_factor;
7965 space = isl_space_range_factor_domain(isl_map_get_space(map));
7966 ext_factor = isl_map_universe(space);
7967 ext_factor = isl_map_range_product(ext_factor, factor);
7968 return isl_map_intersect(map, ext_factor);
7971 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7972 * in the space A -> C, return the intersection.
7974 __isl_give isl_map *isl_map_intersect_range_factor_range(
7975 __isl_take isl_map *map, __isl_take isl_map *factor)
7977 return isl_map_align_params_map_map_and(map, factor,
7978 &map_intersect_range_factor_range);
7981 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7982 __isl_take isl_map *map2)
7984 if (!map1 || !map2)
7985 goto error;
7986 map1 = isl_map_reverse(map1);
7987 map1 = isl_map_apply_range(map1, map2);
7988 return isl_map_reverse(map1);
7989 error:
7990 isl_map_free(map1);
7991 isl_map_free(map2);
7992 return NULL;
7995 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7996 __isl_take isl_map *map2)
7998 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
8001 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
8002 __isl_take isl_map *map2)
8004 isl_space *space;
8005 struct isl_map *result;
8006 int i, j;
8008 if (!map1 || !map2)
8009 goto error;
8011 space = isl_space_join(isl_space_copy(map1->dim),
8012 isl_space_copy(map2->dim));
8014 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8015 if (!result)
8016 goto error;
8017 for (i = 0; i < map1->n; ++i)
8018 for (j = 0; j < map2->n; ++j) {
8019 result = isl_map_add_basic_map(result,
8020 isl_basic_map_apply_range(
8021 isl_basic_map_copy(map1->p[i]),
8022 isl_basic_map_copy(map2->p[j])));
8023 if (!result)
8024 goto error;
8026 isl_map_free(map1);
8027 isl_map_free(map2);
8028 if (result && result->n <= 1)
8029 ISL_F_SET(result, ISL_MAP_DISJOINT);
8030 return result;
8031 error:
8032 isl_map_free(map1);
8033 isl_map_free(map2);
8034 return NULL;
8037 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8038 __isl_take isl_map *map2)
8040 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8044 * returns range - domain
8046 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8048 isl_space *target_space;
8049 struct isl_basic_set *bset;
8050 isl_size dim;
8051 isl_size nparam;
8052 isl_size total;
8053 int i;
8055 if (!bmap)
8056 goto error;
8057 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8058 bmap->dim, isl_dim_out),
8059 goto error);
8060 dim = isl_basic_map_dim(bmap, isl_dim_in);
8061 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8062 if (dim < 0 || nparam < 0)
8063 goto error;
8064 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8065 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8066 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8067 total = isl_basic_map_dim(bmap, isl_dim_all);
8068 if (total < 0)
8069 bmap = isl_basic_map_free(bmap);
8070 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8071 for (i = 0; i < dim; ++i) {
8072 int j = isl_basic_map_alloc_equality(bmap);
8073 if (j < 0) {
8074 bmap = isl_basic_map_free(bmap);
8075 break;
8077 isl_seq_clr(bmap->eq[j], 1 + total);
8078 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8079 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8080 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8082 bset = isl_basic_map_domain(bmap);
8083 bset = isl_basic_set_reset_space(bset, target_space);
8084 return bset;
8085 error:
8086 isl_basic_map_free(bmap);
8087 return NULL;
8090 /* Check that domain and range of "map" are the same.
8092 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8094 isl_space *space;
8095 isl_bool equal;
8097 space = isl_map_peek_space(map);
8098 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8099 if (equal < 0)
8100 return isl_stat_error;
8101 if (!equal)
8102 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8103 "domain and range don't match", return isl_stat_error);
8104 return isl_stat_ok;
8108 * returns range - domain
8110 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8112 int i;
8113 isl_space *dim;
8114 struct isl_set *result;
8116 if (!map)
8117 return NULL;
8119 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8120 map->dim, isl_dim_out),
8121 goto error);
8122 dim = isl_map_get_space(map);
8123 dim = isl_space_domain(dim);
8124 result = isl_set_alloc_space(dim, map->n, 0);
8125 if (!result)
8126 goto error;
8127 for (i = 0; i < map->n; ++i)
8128 result = isl_set_add_basic_set(result,
8129 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8130 isl_map_free(map);
8131 return result;
8132 error:
8133 isl_map_free(map);
8134 return NULL;
8138 * returns [domain -> range] -> range - domain
8140 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8141 __isl_take isl_basic_map *bmap)
8143 int i, k;
8144 isl_space *space;
8145 isl_basic_map *domain;
8146 isl_size nparam, n;
8147 isl_size total;
8149 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8150 bmap->dim, isl_dim_out))
8151 isl_die(bmap->ctx, isl_error_invalid,
8152 "domain and range don't match", goto error);
8154 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8155 n = isl_basic_map_dim(bmap, isl_dim_in);
8156 if (nparam < 0 || n)
8157 return isl_basic_map_free(bmap);
8159 space = isl_basic_map_get_space(bmap);
8160 space = isl_space_from_range(isl_space_domain(space));
8161 domain = isl_basic_map_universe(space);
8163 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8164 bmap = isl_basic_map_apply_range(bmap, domain);
8165 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8167 total = isl_basic_map_dim(bmap, isl_dim_all);
8168 if (total < 0)
8169 return isl_basic_map_free(bmap);
8171 for (i = 0; i < n; ++i) {
8172 k = isl_basic_map_alloc_equality(bmap);
8173 if (k < 0)
8174 goto error;
8175 isl_seq_clr(bmap->eq[k], 1 + total);
8176 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8177 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8178 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8181 bmap = isl_basic_map_gauss(bmap, NULL);
8182 return isl_basic_map_finalize(bmap);
8183 error:
8184 isl_basic_map_free(bmap);
8185 return NULL;
8189 * returns [domain -> range] -> range - domain
8191 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8193 int i;
8194 isl_space *domain_space;
8196 if (isl_map_check_equal_tuples(map) < 0)
8197 return isl_map_free(map);
8199 map = isl_map_cow(map);
8200 if (!map)
8201 return NULL;
8203 domain_space = isl_space_domain(isl_map_get_space(map));
8204 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8205 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8206 if (!map->dim)
8207 goto error;
8208 for (i = 0; i < map->n; ++i) {
8209 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8210 if (!map->p[i])
8211 goto error;
8213 map = isl_map_unmark_normalized(map);
8214 return map;
8215 error:
8216 isl_map_free(map);
8217 return NULL;
8220 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8222 isl_size n_in, n_out;
8224 n_in = isl_space_dim(space, isl_dim_in);
8225 n_out = isl_space_dim(space, isl_dim_out);
8226 if (n_in < 0 || n_out < 0)
8227 goto error;
8228 if (n_in != n_out)
8229 isl_die(space->ctx, isl_error_invalid,
8230 "number of input and output dimensions needs to be "
8231 "the same", goto error);
8232 return isl_basic_map_equal(space, n_in);
8233 error:
8234 isl_space_free(space);
8235 return NULL;
8238 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8240 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8243 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8245 isl_space *dim = isl_set_get_space(set);
8246 isl_map *id;
8247 id = isl_map_identity(isl_space_map_from_set(dim));
8248 return isl_map_intersect_range(id, set);
8251 /* Construct a basic set with all set dimensions having only non-negative
8252 * values.
8254 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8255 __isl_take isl_space *space)
8257 int i;
8258 isl_size nparam;
8259 isl_size dim;
8260 isl_size total;
8261 struct isl_basic_set *bset;
8263 nparam = isl_space_dim(space, isl_dim_param);
8264 dim = isl_space_dim(space, isl_dim_set);
8265 total = isl_space_dim(space, isl_dim_all);
8266 if (nparam < 0 || dim < 0 || total < 0)
8267 space = isl_space_free(space);
8268 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8269 if (!bset)
8270 return NULL;
8271 for (i = 0; i < dim; ++i) {
8272 int k = isl_basic_set_alloc_inequality(bset);
8273 if (k < 0)
8274 goto error;
8275 isl_seq_clr(bset->ineq[k], 1 + total);
8276 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8278 return bset;
8279 error:
8280 isl_basic_set_free(bset);
8281 return NULL;
8284 /* Construct the half-space x_pos >= 0.
8286 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8287 int pos)
8289 int k;
8290 isl_size total;
8291 isl_basic_set *nonneg;
8293 total = isl_space_dim(space, isl_dim_all);
8294 if (total < 0)
8295 space = isl_space_free(space);
8296 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8297 k = isl_basic_set_alloc_inequality(nonneg);
8298 if (k < 0)
8299 goto error;
8300 isl_seq_clr(nonneg->ineq[k], 1 + total);
8301 isl_int_set_si(nonneg->ineq[k][pos], 1);
8303 return isl_basic_set_finalize(nonneg);
8304 error:
8305 isl_basic_set_free(nonneg);
8306 return NULL;
8309 /* Construct the half-space x_pos <= -1.
8311 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8312 int pos)
8314 int k;
8315 isl_size total;
8316 isl_basic_set *neg;
8318 total = isl_space_dim(space, isl_dim_all);
8319 if (total < 0)
8320 space = isl_space_free(space);
8321 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8322 k = isl_basic_set_alloc_inequality(neg);
8323 if (k < 0)
8324 goto error;
8325 isl_seq_clr(neg->ineq[k], 1 + total);
8326 isl_int_set_si(neg->ineq[k][0], -1);
8327 isl_int_set_si(neg->ineq[k][pos], -1);
8329 return isl_basic_set_finalize(neg);
8330 error:
8331 isl_basic_set_free(neg);
8332 return NULL;
8335 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8336 enum isl_dim_type type, unsigned first, unsigned n)
8338 int i;
8339 unsigned offset;
8340 isl_basic_set *nonneg;
8341 isl_basic_set *neg;
8343 if (n == 0)
8344 return set;
8346 if (isl_set_check_range(set, type, first, n) < 0)
8347 return isl_set_free(set);
8349 offset = pos(set->dim, type);
8350 for (i = 0; i < n; ++i) {
8351 nonneg = nonneg_halfspace(isl_set_get_space(set),
8352 offset + first + i);
8353 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8355 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8358 return set;
8361 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8362 int len,
8363 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8364 void *user)
8366 isl_set *half;
8368 if (!set)
8369 return isl_stat_error;
8370 if (isl_set_plain_is_empty(set)) {
8371 isl_set_free(set);
8372 return isl_stat_ok;
8374 if (first == len)
8375 return fn(set, signs, user);
8377 signs[first] = 1;
8378 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8379 1 + first));
8380 half = isl_set_intersect(half, isl_set_copy(set));
8381 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8382 goto error;
8384 signs[first] = -1;
8385 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8386 1 + first));
8387 half = isl_set_intersect(half, set);
8388 return foreach_orthant(half, signs, first + 1, len, fn, user);
8389 error:
8390 isl_set_free(set);
8391 return isl_stat_error;
8394 /* Call "fn" on the intersections of "set" with each of the orthants
8395 * (except for obviously empty intersections). The orthant is identified
8396 * by the signs array, with each entry having value 1 or -1 according
8397 * to the sign of the corresponding variable.
8399 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8400 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8401 void *user)
8403 isl_size nparam;
8404 isl_size nvar;
8405 int *signs;
8406 isl_stat r;
8408 if (!set)
8409 return isl_stat_error;
8410 if (isl_set_plain_is_empty(set))
8411 return isl_stat_ok;
8413 nparam = isl_set_dim(set, isl_dim_param);
8414 nvar = isl_set_dim(set, isl_dim_set);
8415 if (nparam < 0 || nvar < 0)
8416 return isl_stat_error;
8418 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8420 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8421 fn, user);
8423 free(signs);
8425 return r;
8428 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8430 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8433 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8434 __isl_keep isl_basic_map *bmap2)
8436 isl_bool is_subset;
8437 struct isl_map *map1;
8438 struct isl_map *map2;
8440 if (!bmap1 || !bmap2)
8441 return isl_bool_error;
8443 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8444 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8446 is_subset = isl_map_is_subset(map1, map2);
8448 isl_map_free(map1);
8449 isl_map_free(map2);
8451 return is_subset;
8454 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8455 __isl_keep isl_basic_set *bset2)
8457 return isl_basic_map_is_subset(bset1, bset2);
8460 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8461 __isl_keep isl_basic_map *bmap2)
8463 isl_bool is_subset;
8465 if (!bmap1 || !bmap2)
8466 return isl_bool_error;
8467 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8468 if (is_subset != isl_bool_true)
8469 return is_subset;
8470 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8471 return is_subset;
8474 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8475 __isl_keep isl_basic_set *bset2)
8477 return isl_basic_map_is_equal(
8478 bset_to_bmap(bset1), bset_to_bmap(bset2));
8481 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8483 int i;
8484 int is_empty;
8486 if (!map)
8487 return isl_bool_error;
8488 for (i = 0; i < map->n; ++i) {
8489 is_empty = isl_basic_map_is_empty(map->p[i]);
8490 if (is_empty < 0)
8491 return isl_bool_error;
8492 if (!is_empty)
8493 return isl_bool_false;
8495 return isl_bool_true;
8498 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8500 return map ? map->n == 0 : isl_bool_error;
8503 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8505 return set ? set->n == 0 : isl_bool_error;
8508 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8510 return isl_map_is_empty(set_to_map(set));
8513 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8514 __isl_keep isl_map *map2)
8516 if (!map1 || !map2)
8517 return isl_bool_error;
8519 return isl_space_is_equal(map1->dim, map2->dim);
8522 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8523 __isl_keep isl_set *set2)
8525 if (!set1 || !set2)
8526 return isl_bool_error;
8528 return isl_space_is_equal(set1->dim, set2->dim);
8531 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8533 isl_bool is_subset;
8535 if (!map1 || !map2)
8536 return isl_bool_error;
8537 is_subset = isl_map_is_subset(map1, map2);
8538 if (is_subset != isl_bool_true)
8539 return is_subset;
8540 is_subset = isl_map_is_subset(map2, map1);
8541 return is_subset;
8544 /* Is "map1" equal to "map2"?
8546 * First check if they are obviously equal.
8547 * If not, then perform a more detailed analysis.
8549 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8551 isl_bool equal;
8553 equal = isl_map_plain_is_equal(map1, map2);
8554 if (equal < 0 || equal)
8555 return equal;
8556 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8559 isl_bool isl_basic_map_is_strict_subset(
8560 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8562 isl_bool is_subset;
8564 if (!bmap1 || !bmap2)
8565 return isl_bool_error;
8566 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8567 if (is_subset != isl_bool_true)
8568 return is_subset;
8569 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8570 return isl_bool_not(is_subset);
8573 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8574 __isl_keep isl_map *map2)
8576 isl_bool is_subset;
8578 if (!map1 || !map2)
8579 return isl_bool_error;
8580 is_subset = isl_map_is_subset(map1, map2);
8581 if (is_subset != isl_bool_true)
8582 return is_subset;
8583 is_subset = isl_map_is_subset(map2, map1);
8584 return isl_bool_not(is_subset);
8587 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8588 __isl_keep isl_set *set2)
8590 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8593 /* Is "bmap" obviously equal to the universe with the same space?
8595 * That is, does it not have any constraints?
8597 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8599 if (!bmap)
8600 return isl_bool_error;
8601 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8604 /* Is "bset" obviously equal to the universe with the same space?
8606 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8608 return isl_basic_map_plain_is_universe(bset);
8611 /* If "c" does not involve any existentially quantified variables,
8612 * then set *univ to false and abort
8614 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8616 isl_bool *univ = user;
8617 isl_size n;
8619 n = isl_constraint_dim(c, isl_dim_div);
8620 if (n < 0)
8621 c = isl_constraint_free(c);
8622 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8623 isl_constraint_free(c);
8624 if (*univ < 0 || !*univ)
8625 return isl_stat_error;
8626 return isl_stat_ok;
8629 /* Is "bmap" equal to the universe with the same space?
8631 * First check if it is obviously equal to the universe.
8632 * If not and if there are any constraints not involving
8633 * existentially quantified variables, then it is certainly
8634 * not equal to the universe.
8635 * Otherwise, check if the universe is a subset of "bmap".
8637 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8639 isl_size n_div;
8640 isl_bool univ;
8641 isl_basic_map *test;
8643 univ = isl_basic_map_plain_is_universe(bmap);
8644 if (univ < 0 || univ)
8645 return univ;
8646 n_div = isl_basic_map_dim(bmap, isl_dim_div);
8647 if (n_div < 0)
8648 return isl_bool_error;
8649 if (n_div == 0)
8650 return isl_bool_false;
8651 univ = isl_bool_true;
8652 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8653 univ)
8654 return isl_bool_error;
8655 if (univ < 0 || !univ)
8656 return univ;
8657 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8658 univ = isl_basic_map_is_subset(test, bmap);
8659 isl_basic_map_free(test);
8660 return univ;
8663 /* Is "bset" equal to the universe with the same space?
8665 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8667 return isl_basic_map_is_universe(bset);
8670 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8672 int i;
8674 if (!map)
8675 return isl_bool_error;
8677 for (i = 0; i < map->n; ++i) {
8678 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8679 if (r < 0 || r)
8680 return r;
8683 return isl_bool_false;
8686 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8688 return isl_map_plain_is_universe(set_to_map(set));
8691 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8693 struct isl_basic_set *bset = NULL;
8694 struct isl_vec *sample = NULL;
8695 isl_bool empty, non_empty;
8697 if (!bmap)
8698 return isl_bool_error;
8700 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8701 return isl_bool_true;
8703 if (isl_basic_map_plain_is_universe(bmap))
8704 return isl_bool_false;
8706 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8707 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8708 copy = isl_basic_map_remove_redundancies(copy);
8709 empty = isl_basic_map_plain_is_empty(copy);
8710 isl_basic_map_free(copy);
8711 return empty;
8714 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8715 if (non_empty < 0)
8716 return isl_bool_error;
8717 if (non_empty)
8718 return isl_bool_false;
8719 isl_vec_free(bmap->sample);
8720 bmap->sample = NULL;
8721 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8722 if (!bset)
8723 return isl_bool_error;
8724 sample = isl_basic_set_sample_vec(bset);
8725 if (!sample)
8726 return isl_bool_error;
8727 empty = sample->size == 0;
8728 isl_vec_free(bmap->sample);
8729 bmap->sample = sample;
8730 if (empty)
8731 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8733 return empty;
8736 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8738 if (!bmap)
8739 return isl_bool_error;
8740 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8743 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8745 if (!bset)
8746 return isl_bool_error;
8747 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8750 /* Is "bmap" known to be non-empty?
8752 * That is, is the cached sample still valid?
8754 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8756 isl_size total;
8758 if (!bmap)
8759 return isl_bool_error;
8760 if (!bmap->sample)
8761 return isl_bool_false;
8762 total = isl_basic_map_dim(bmap, isl_dim_all);
8763 if (total < 0)
8764 return isl_bool_error;
8765 if (bmap->sample->size != 1 + total)
8766 return isl_bool_false;
8767 return isl_basic_map_contains(bmap, bmap->sample);
8770 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8772 return isl_basic_map_is_empty(bset_to_bmap(bset));
8775 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8776 __isl_take isl_basic_map *bmap2)
8778 struct isl_map *map;
8779 if (!bmap1 || !bmap2)
8780 goto error;
8782 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8784 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8785 if (!map)
8786 goto error;
8787 map = isl_map_add_basic_map(map, bmap1);
8788 map = isl_map_add_basic_map(map, bmap2);
8789 return map;
8790 error:
8791 isl_basic_map_free(bmap1);
8792 isl_basic_map_free(bmap2);
8793 return NULL;
8796 struct isl_set *isl_basic_set_union(
8797 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8799 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8800 bset_to_bmap(bset2)));
8803 /* Order divs such that any div only depends on previous divs */
8804 __isl_give isl_basic_map *isl_basic_map_order_divs(
8805 __isl_take isl_basic_map *bmap)
8807 int i;
8808 isl_size off;
8810 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8811 if (off < 0)
8812 return isl_basic_map_free(bmap);
8814 for (i = 0; i < bmap->n_div; ++i) {
8815 int pos;
8816 if (isl_int_is_zero(bmap->div[i][0]))
8817 continue;
8818 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8819 bmap->n_div-i);
8820 if (pos == -1)
8821 continue;
8822 if (pos == 0)
8823 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8824 "integer division depends on itself",
8825 return isl_basic_map_free(bmap));
8826 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8827 if (!bmap)
8828 return NULL;
8829 --i;
8831 return bmap;
8834 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8836 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8839 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8841 int i;
8843 if (!map)
8844 return 0;
8846 for (i = 0; i < map->n; ++i) {
8847 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8848 if (!map->p[i])
8849 goto error;
8852 return map;
8853 error:
8854 isl_map_free(map);
8855 return NULL;
8858 /* Sort the local variables of "bset".
8860 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8861 __isl_take isl_basic_set *bset)
8863 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8866 /* Apply the expansion computed by isl_merge_divs.
8867 * The expansion itself is given by "exp" while the resulting
8868 * list of divs is given by "div".
8870 * Move the integer divisions of "bmap" into the right position
8871 * according to "exp" and then introduce the additional integer
8872 * divisions, adding div constraints.
8873 * The moving should be done first to avoid moving coefficients
8874 * in the definitions of the extra integer divisions.
8876 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8877 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8879 int i, j;
8880 int n_div;
8882 bmap = isl_basic_map_cow(bmap);
8883 if (!bmap || !div)
8884 goto error;
8886 if (div->n_row < bmap->n_div)
8887 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8888 "not an expansion", goto error);
8890 n_div = bmap->n_div;
8891 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8892 div->n_row - n_div, 0,
8893 2 * (div->n_row - n_div));
8895 for (i = n_div; i < div->n_row; ++i)
8896 if (isl_basic_map_alloc_div(bmap) < 0)
8897 goto error;
8899 for (j = n_div - 1; j >= 0; --j) {
8900 if (exp[j] == j)
8901 break;
8902 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8903 if (!bmap)
8904 goto error;
8906 j = 0;
8907 for (i = 0; i < div->n_row; ++i) {
8908 if (j < n_div && exp[j] == i) {
8909 j++;
8910 } else {
8911 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8912 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8913 continue;
8914 bmap = isl_basic_map_add_div_constraints(bmap, i);
8915 if (!bmap)
8916 goto error;
8920 isl_mat_free(div);
8921 return bmap;
8922 error:
8923 isl_basic_map_free(bmap);
8924 isl_mat_free(div);
8925 return NULL;
8928 /* Apply the expansion computed by isl_merge_divs.
8929 * The expansion itself is given by "exp" while the resulting
8930 * list of divs is given by "div".
8932 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8933 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8935 return isl_basic_map_expand_divs(bset, div, exp);
8938 /* Look for a div in dst that corresponds to the div "div" in src.
8939 * The divs before "div" in src and dst are assumed to be the same.
8941 * Return the position of the corresponding div in dst
8942 * if there is one. Otherwise, return a position beyond the integer divisions.
8943 * Return -1 on error.
8945 static int find_div(__isl_keep isl_basic_map *dst,
8946 __isl_keep isl_basic_map *src, unsigned div)
8948 int i;
8949 isl_size n_div;
8950 isl_size v_div;
8952 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8953 n_div = isl_basic_map_dim(dst, isl_dim_div);
8954 if (n_div < 0 || v_div < 0)
8955 return -1;
8956 isl_assert(dst->ctx, div <= n_div, return -1);
8957 for (i = div; i < n_div; ++i)
8958 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8959 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8960 n_div - div) == -1)
8961 return i;
8962 return n_div;
8965 /* Align the divs of "dst" to those of "src", adding divs from "src"
8966 * if needed. That is, make sure that the first src->n_div divs
8967 * of the result are equal to those of src.
8969 * The result is not finalized as by design it will have redundant
8970 * divs if any divs from "src" were copied.
8972 __isl_give isl_basic_map *isl_basic_map_align_divs(
8973 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8975 int i;
8976 isl_bool known;
8977 int extended;
8978 isl_size v_div;
8979 isl_size dst_n_div;
8981 if (!dst || !src)
8982 return isl_basic_map_free(dst);
8984 if (src->n_div == 0)
8985 return dst;
8987 known = isl_basic_map_divs_known(src);
8988 if (known < 0)
8989 return isl_basic_map_free(dst);
8990 if (!known)
8991 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8992 "some src divs are unknown",
8993 return isl_basic_map_free(dst));
8995 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8996 if (v_div < 0)
8997 return isl_basic_map_free(dst);
8999 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
9000 if (!src)
9001 return isl_basic_map_free(dst);
9003 extended = 0;
9004 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9005 if (dst_n_div < 0)
9006 dst = isl_basic_map_free(dst);
9007 for (i = 0; i < src->n_div; ++i) {
9008 int j = find_div(dst, src, i);
9009 if (j < 0)
9010 dst = isl_basic_map_free(dst);
9011 if (j == dst_n_div) {
9012 if (!extended) {
9013 int extra = src->n_div - i;
9014 dst = isl_basic_map_cow(dst);
9015 if (!dst)
9016 goto error;
9017 dst = isl_basic_map_extend_space(dst,
9018 isl_space_copy(dst->dim),
9019 extra, 0, 2 * extra);
9020 extended = 1;
9022 j = isl_basic_map_alloc_div(dst);
9023 if (j < 0)
9024 goto error;
9025 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9026 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9027 dst_n_div++;
9028 dst = isl_basic_map_add_div_constraints(dst, j);
9029 if (!dst)
9030 goto error;
9032 if (j != i)
9033 dst = isl_basic_map_swap_div(dst, i, j);
9034 if (!dst)
9035 goto error;
9037 isl_basic_map_free(src);
9038 return dst;
9039 error:
9040 isl_basic_map_free(src);
9041 isl_basic_map_free(dst);
9042 return NULL;
9045 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9047 int i;
9049 if (!map)
9050 return NULL;
9051 if (map->n == 0)
9052 return map;
9053 map = isl_map_compute_divs(map);
9054 map = isl_map_cow(map);
9055 if (!map)
9056 return NULL;
9058 for (i = 1; i < map->n; ++i)
9059 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9060 for (i = 1; i < map->n; ++i) {
9061 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9062 if (!map->p[i])
9063 return isl_map_free(map);
9066 map = isl_map_unmark_normalized(map);
9067 return map;
9070 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9072 return isl_map_align_divs_internal(map);
9075 struct isl_set *isl_set_align_divs(struct isl_set *set)
9077 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9080 /* Align the divs of the basic maps in "map" to those
9081 * of the basic maps in "list", as well as to the other basic maps in "map".
9082 * The elements in "list" are assumed to have known divs.
9084 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9085 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9087 int i;
9088 isl_size n;
9090 n = isl_basic_map_list_n_basic_map(list);
9091 map = isl_map_compute_divs(map);
9092 map = isl_map_cow(map);
9093 if (!map || n < 0)
9094 return isl_map_free(map);
9095 if (map->n == 0)
9096 return map;
9098 for (i = 0; i < n; ++i) {
9099 isl_basic_map *bmap;
9101 bmap = isl_basic_map_list_get_basic_map(list, i);
9102 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9103 isl_basic_map_free(bmap);
9105 if (!map->p[0])
9106 return isl_map_free(map);
9108 return isl_map_align_divs_internal(map);
9111 /* Align the divs of each element of "list" to those of "bmap".
9112 * Both "bmap" and the elements of "list" are assumed to have known divs.
9114 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9115 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9117 int i;
9118 isl_size n;
9120 n = isl_basic_map_list_n_basic_map(list);
9121 if (n < 0 || !bmap)
9122 return isl_basic_map_list_free(list);
9124 for (i = 0; i < n; ++i) {
9125 isl_basic_map *bmap_i;
9127 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9128 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9129 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9132 return list;
9135 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9136 __isl_take isl_map *map)
9138 isl_bool ok;
9140 ok = isl_map_compatible_domain(map, set);
9141 if (ok < 0)
9142 goto error;
9143 if (!ok)
9144 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9145 "incompatible spaces", goto error);
9146 map = isl_map_intersect_domain(map, set);
9147 set = isl_map_range(map);
9148 return set;
9149 error:
9150 isl_set_free(set);
9151 isl_map_free(map);
9152 return NULL;
9155 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9156 __isl_take isl_map *map)
9158 return isl_map_align_params_map_map_and(set, map, &set_apply);
9161 /* There is no need to cow as removing empty parts doesn't change
9162 * the meaning of the set.
9164 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9166 int i;
9168 if (!map)
9169 return NULL;
9171 for (i = map->n - 1; i >= 0; --i)
9172 map = remove_if_empty(map, i);
9174 return map;
9177 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9179 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9182 /* Create a binary relation that maps the shared initial "pos" dimensions
9183 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9185 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9186 __isl_keep isl_basic_set *bset2, int pos)
9188 isl_basic_map *bmap1;
9189 isl_basic_map *bmap2;
9191 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9192 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9193 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9194 isl_dim_out, 0, pos);
9195 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9196 isl_dim_out, 0, pos);
9197 return isl_basic_map_range_product(bmap1, bmap2);
9200 /* Given two basic sets bset1 and bset2, compute the maximal difference
9201 * between the values of dimension pos in bset1 and those in bset2
9202 * for any common value of the parameters and dimensions preceding pos.
9204 static enum isl_lp_result basic_set_maximal_difference_at(
9205 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9206 int pos, isl_int *opt)
9208 isl_basic_map *bmap1;
9209 struct isl_ctx *ctx;
9210 struct isl_vec *obj;
9211 isl_size total;
9212 isl_size nparam;
9213 isl_size dim1;
9214 enum isl_lp_result res;
9216 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9217 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9218 if (nparam < 0 || dim1 < 0 || !bset2)
9219 return isl_lp_error;
9221 bmap1 = join_initial(bset1, bset2, pos);
9222 total = isl_basic_map_dim(bmap1, isl_dim_all);
9223 if (total < 0)
9224 return isl_lp_error;
9226 ctx = bmap1->ctx;
9227 obj = isl_vec_alloc(ctx, 1 + total);
9228 if (!obj)
9229 goto error;
9230 isl_seq_clr(obj->block.data, 1 + total);
9231 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9232 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9233 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9234 opt, NULL, NULL);
9235 isl_basic_map_free(bmap1);
9236 isl_vec_free(obj);
9237 return res;
9238 error:
9239 isl_basic_map_free(bmap1);
9240 return isl_lp_error;
9243 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9244 * for any common value of the parameters and dimensions preceding pos
9245 * in both basic sets, the values of dimension pos in bset1 are
9246 * smaller or larger than those in bset2.
9248 * Returns
9249 * 1 if bset1 follows bset2
9250 * -1 if bset1 precedes bset2
9251 * 0 if bset1 and bset2 are incomparable
9252 * -2 if some error occurred.
9254 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9255 __isl_keep isl_basic_set *bset2, int pos)
9257 isl_int opt;
9258 enum isl_lp_result res;
9259 int cmp;
9261 isl_int_init(opt);
9263 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9265 if (res == isl_lp_empty)
9266 cmp = 0;
9267 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9268 res == isl_lp_unbounded)
9269 cmp = 1;
9270 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9271 cmp = -1;
9272 else
9273 cmp = -2;
9275 isl_int_clear(opt);
9276 return cmp;
9279 /* Given two basic sets bset1 and bset2, check whether
9280 * for any common value of the parameters and dimensions preceding pos
9281 * there is a value of dimension pos in bset1 that is larger
9282 * than a value of the same dimension in bset2.
9284 * Return
9285 * 1 if there exists such a pair
9286 * 0 if there is no such pair, but there is a pair of equal values
9287 * -1 otherwise
9288 * -2 if some error occurred.
9290 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9291 __isl_keep isl_basic_set *bset2, int pos)
9293 isl_bool empty;
9294 isl_basic_map *bmap;
9295 isl_size dim1;
9297 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9298 if (dim1 < 0)
9299 return -2;
9300 bmap = join_initial(bset1, bset2, pos);
9301 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9302 isl_dim_out, dim1 - pos);
9303 empty = isl_basic_map_is_empty(bmap);
9304 if (empty < 0)
9305 goto error;
9306 if (empty) {
9307 isl_basic_map_free(bmap);
9308 return -1;
9310 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9311 isl_dim_out, dim1 - pos);
9312 empty = isl_basic_map_is_empty(bmap);
9313 if (empty < 0)
9314 goto error;
9315 isl_basic_map_free(bmap);
9316 if (empty)
9317 return 0;
9318 return 1;
9319 error:
9320 isl_basic_map_free(bmap);
9321 return -2;
9324 /* Given two sets set1 and set2, check whether
9325 * for any common value of the parameters and dimensions preceding pos
9326 * there is a value of dimension pos in set1 that is larger
9327 * than a value of the same dimension in set2.
9329 * Return
9330 * 1 if there exists such a pair
9331 * 0 if there is no such pair, but there is a pair of equal values
9332 * -1 otherwise
9333 * -2 if some error occurred.
9335 int isl_set_follows_at(__isl_keep isl_set *set1,
9336 __isl_keep isl_set *set2, int pos)
9338 int i, j;
9339 int follows = -1;
9341 if (!set1 || !set2)
9342 return -2;
9344 for (i = 0; i < set1->n; ++i)
9345 for (j = 0; j < set2->n; ++j) {
9346 int f;
9347 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9348 if (f == 1 || f == -2)
9349 return f;
9350 if (f > follows)
9351 follows = f;
9354 return follows;
9357 static isl_bool isl_basic_map_plain_has_fixed_var(
9358 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9360 int i;
9361 int d;
9362 isl_size total;
9364 total = isl_basic_map_dim(bmap, isl_dim_all);
9365 if (total < 0)
9366 return isl_bool_error;
9367 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9368 for (; d+1 > pos; --d)
9369 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9370 break;
9371 if (d != pos)
9372 continue;
9373 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9374 return isl_bool_false;
9375 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9376 return isl_bool_false;
9377 if (!isl_int_is_one(bmap->eq[i][1+d]))
9378 return isl_bool_false;
9379 if (val)
9380 isl_int_neg(*val, bmap->eq[i][0]);
9381 return isl_bool_true;
9383 return isl_bool_false;
9386 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9387 unsigned pos, isl_int *val)
9389 int i;
9390 isl_int v;
9391 isl_int tmp;
9392 isl_bool fixed;
9394 if (!map)
9395 return isl_bool_error;
9396 if (map->n == 0)
9397 return isl_bool_false;
9398 if (map->n == 1)
9399 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9400 isl_int_init(v);
9401 isl_int_init(tmp);
9402 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9403 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9404 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9405 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9406 fixed = isl_bool_false;
9408 if (val)
9409 isl_int_set(*val, v);
9410 isl_int_clear(tmp);
9411 isl_int_clear(v);
9412 return fixed;
9415 static isl_bool isl_basic_set_plain_has_fixed_var(
9416 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9418 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9419 pos, val);
9422 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9423 enum isl_dim_type type, unsigned pos, isl_int *val)
9425 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9426 return isl_bool_error;
9427 return isl_basic_map_plain_has_fixed_var(bmap,
9428 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9431 /* If "bmap" obviously lies on a hyperplane where the given dimension
9432 * has a fixed value, then return that value.
9433 * Otherwise return NaN.
9435 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9436 __isl_keep isl_basic_map *bmap,
9437 enum isl_dim_type type, unsigned pos)
9439 isl_ctx *ctx;
9440 isl_val *v;
9441 isl_bool fixed;
9443 if (!bmap)
9444 return NULL;
9445 ctx = isl_basic_map_get_ctx(bmap);
9446 v = isl_val_alloc(ctx);
9447 if (!v)
9448 return NULL;
9449 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9450 if (fixed < 0)
9451 return isl_val_free(v);
9452 if (fixed) {
9453 isl_int_set_si(v->d, 1);
9454 return v;
9456 isl_val_free(v);
9457 return isl_val_nan(ctx);
9460 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9461 enum isl_dim_type type, unsigned pos, isl_int *val)
9463 if (isl_map_check_range(map, type, pos, 1) < 0)
9464 return isl_bool_error;
9465 return isl_map_plain_has_fixed_var(map,
9466 map_offset(map, type) - 1 + pos, val);
9469 /* If "map" obviously lies on a hyperplane where the given dimension
9470 * has a fixed value, then return that value.
9471 * Otherwise return NaN.
9473 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9474 enum isl_dim_type type, unsigned pos)
9476 isl_ctx *ctx;
9477 isl_val *v;
9478 isl_bool fixed;
9480 if (!map)
9481 return NULL;
9482 ctx = isl_map_get_ctx(map);
9483 v = isl_val_alloc(ctx);
9484 if (!v)
9485 return NULL;
9486 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9487 if (fixed < 0)
9488 return isl_val_free(v);
9489 if (fixed) {
9490 isl_int_set_si(v->d, 1);
9491 return v;
9493 isl_val_free(v);
9494 return isl_val_nan(ctx);
9497 /* If "set" obviously lies on a hyperplane where the given dimension
9498 * has a fixed value, then return that value.
9499 * Otherwise return NaN.
9501 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9502 enum isl_dim_type type, unsigned pos)
9504 return isl_map_plain_get_val_if_fixed(set, type, pos);
9507 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9508 * then return this fixed value in *val.
9510 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9511 unsigned dim, isl_int *val)
9513 isl_size nparam;
9515 nparam = isl_basic_set_dim(bset, isl_dim_param);
9516 if (nparam < 0)
9517 return isl_bool_error;
9518 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
9521 /* Return -1 if the constraint "c1" should be sorted before "c2"
9522 * and 1 if it should be sorted after "c2".
9523 * Return 0 if the two constraints are the same (up to the constant term).
9525 * In particular, if a constraint involves later variables than another
9526 * then it is sorted after this other constraint.
9527 * uset_gist depends on constraints without existentially quantified
9528 * variables sorting first.
9530 * For constraints that have the same latest variable, those
9531 * with the same coefficient for this latest variable (first in absolute value
9532 * and then in actual value) are grouped together.
9533 * This is useful for detecting pairs of constraints that can
9534 * be chained in their printed representation.
9536 * Finally, within a group, constraints are sorted according to
9537 * their coefficients (excluding the constant term).
9539 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9541 isl_int **c1 = (isl_int **) p1;
9542 isl_int **c2 = (isl_int **) p2;
9543 int l1, l2;
9544 unsigned size = *(unsigned *) arg;
9545 int cmp;
9547 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9548 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9550 if (l1 != l2)
9551 return l1 - l2;
9553 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9554 if (cmp != 0)
9555 return cmp;
9556 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9557 if (cmp != 0)
9558 return -cmp;
9560 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9563 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9564 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9565 * and 0 if the two constraints are the same (up to the constant term).
9567 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9568 isl_int *c1, isl_int *c2)
9570 isl_size total;
9571 unsigned size;
9573 total = isl_basic_map_dim(bmap, isl_dim_all);
9574 if (total < 0)
9575 return -2;
9576 size = total;
9577 return sort_constraint_cmp(&c1, &c2, &size);
9580 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9581 __isl_take isl_basic_map *bmap)
9583 isl_size total;
9584 unsigned size;
9586 if (!bmap)
9587 return NULL;
9588 if (bmap->n_ineq == 0)
9589 return bmap;
9590 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9591 return bmap;
9592 total = isl_basic_map_dim(bmap, isl_dim_all);
9593 if (total < 0)
9594 return isl_basic_map_free(bmap);
9595 size = total;
9596 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9597 &sort_constraint_cmp, &size) < 0)
9598 return isl_basic_map_free(bmap);
9599 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9600 return bmap;
9603 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9604 __isl_take isl_basic_set *bset)
9606 isl_basic_map *bmap = bset_to_bmap(bset);
9607 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9610 __isl_give isl_basic_map *isl_basic_map_normalize(
9611 __isl_take isl_basic_map *bmap)
9613 bmap = isl_basic_map_remove_redundancies(bmap);
9614 bmap = isl_basic_map_sort_constraints(bmap);
9615 return bmap;
9617 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9618 __isl_keep isl_basic_map *bmap2)
9620 int i, cmp;
9621 isl_size total;
9622 isl_space *space1, *space2;
9624 if (!bmap1 || !bmap2)
9625 return -1;
9627 if (bmap1 == bmap2)
9628 return 0;
9629 space1 = isl_basic_map_peek_space(bmap1);
9630 space2 = isl_basic_map_peek_space(bmap2);
9631 cmp = isl_space_cmp(space1, space2);
9632 if (cmp)
9633 return cmp;
9634 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9635 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9636 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9637 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9638 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9639 return 0;
9640 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9641 return 1;
9642 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9643 return -1;
9644 if (bmap1->n_eq != bmap2->n_eq)
9645 return bmap1->n_eq - bmap2->n_eq;
9646 if (bmap1->n_ineq != bmap2->n_ineq)
9647 return bmap1->n_ineq - bmap2->n_ineq;
9648 if (bmap1->n_div != bmap2->n_div)
9649 return bmap1->n_div - bmap2->n_div;
9650 total = isl_basic_map_dim(bmap1, isl_dim_all);
9651 if (total < 0)
9652 return -1;
9653 for (i = 0; i < bmap1->n_eq; ++i) {
9654 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9655 if (cmp)
9656 return cmp;
9658 for (i = 0; i < bmap1->n_ineq; ++i) {
9659 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9660 if (cmp)
9661 return cmp;
9663 for (i = 0; i < bmap1->n_div; ++i) {
9664 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9665 if (cmp)
9666 return cmp;
9668 return 0;
9671 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9672 __isl_keep isl_basic_set *bset2)
9674 return isl_basic_map_plain_cmp(bset1, bset2);
9677 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9679 int i, cmp;
9681 if (set1 == set2)
9682 return 0;
9683 if (set1->n != set2->n)
9684 return set1->n - set2->n;
9686 for (i = 0; i < set1->n; ++i) {
9687 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9688 if (cmp)
9689 return cmp;
9692 return 0;
9695 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9696 __isl_keep isl_basic_map *bmap2)
9698 if (!bmap1 || !bmap2)
9699 return isl_bool_error;
9700 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9703 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9704 __isl_keep isl_basic_set *bset2)
9706 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9707 bset_to_bmap(bset2));
9710 static int qsort_bmap_cmp(const void *p1, const void *p2)
9712 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9713 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9715 return isl_basic_map_plain_cmp(bmap1, bmap2);
9718 /* Sort the basic maps of "map" and remove duplicate basic maps.
9720 * While removing basic maps, we make sure that the basic maps remain
9721 * sorted because isl_map_normalize expects the basic maps of the result
9722 * to be sorted.
9724 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9726 int i, j;
9728 map = isl_map_remove_empty_parts(map);
9729 if (!map)
9730 return NULL;
9731 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9732 for (i = map->n - 1; i >= 1; --i) {
9733 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9734 continue;
9735 isl_basic_map_free(map->p[i-1]);
9736 for (j = i; j < map->n; ++j)
9737 map->p[j - 1] = map->p[j];
9738 map->n--;
9741 return map;
9744 /* Remove obvious duplicates among the basic maps of "map".
9746 * Unlike isl_map_normalize, this function does not remove redundant
9747 * constraints and only removes duplicates that have exactly the same
9748 * constraints in the input. It does sort the constraints and
9749 * the basic maps to ease the detection of duplicates.
9751 * If "map" has already been normalized or if the basic maps are
9752 * disjoint, then there can be no duplicates.
9754 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9756 int i;
9757 isl_basic_map *bmap;
9759 if (!map)
9760 return NULL;
9761 if (map->n <= 1)
9762 return map;
9763 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9764 return map;
9765 for (i = 0; i < map->n; ++i) {
9766 bmap = isl_basic_map_copy(map->p[i]);
9767 bmap = isl_basic_map_sort_constraints(bmap);
9768 if (!bmap)
9769 return isl_map_free(map);
9770 isl_basic_map_free(map->p[i]);
9771 map->p[i] = bmap;
9774 map = sort_and_remove_duplicates(map);
9775 return map;
9778 /* We normalize in place, but if anything goes wrong we need
9779 * to return NULL, so we need to make sure we don't change the
9780 * meaning of any possible other copies of map.
9782 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9784 int i;
9785 struct isl_basic_map *bmap;
9787 if (!map)
9788 return NULL;
9789 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9790 return map;
9791 for (i = 0; i < map->n; ++i) {
9792 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9793 if (!bmap)
9794 goto error;
9795 isl_basic_map_free(map->p[i]);
9796 map->p[i] = bmap;
9799 map = sort_and_remove_duplicates(map);
9800 if (map)
9801 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9802 return map;
9803 error:
9804 isl_map_free(map);
9805 return NULL;
9808 struct isl_set *isl_set_normalize(struct isl_set *set)
9810 return set_from_map(isl_map_normalize(set_to_map(set)));
9813 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9814 __isl_keep isl_map *map2)
9816 int i;
9817 isl_bool equal;
9819 if (!map1 || !map2)
9820 return isl_bool_error;
9822 if (map1 == map2)
9823 return isl_bool_true;
9824 if (!isl_space_is_equal(map1->dim, map2->dim))
9825 return isl_bool_false;
9827 map1 = isl_map_copy(map1);
9828 map2 = isl_map_copy(map2);
9829 map1 = isl_map_normalize(map1);
9830 map2 = isl_map_normalize(map2);
9831 if (!map1 || !map2)
9832 goto error;
9833 equal = map1->n == map2->n;
9834 for (i = 0; equal && i < map1->n; ++i) {
9835 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9836 if (equal < 0)
9837 goto error;
9839 isl_map_free(map1);
9840 isl_map_free(map2);
9841 return equal;
9842 error:
9843 isl_map_free(map1);
9844 isl_map_free(map2);
9845 return isl_bool_error;
9848 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9849 __isl_keep isl_set *set2)
9851 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9854 /* Return the basic maps in "map" as a list.
9856 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9857 __isl_keep isl_map *map)
9859 int i;
9860 isl_ctx *ctx;
9861 isl_basic_map_list *list;
9863 if (!map)
9864 return NULL;
9865 ctx = isl_map_get_ctx(map);
9866 list = isl_basic_map_list_alloc(ctx, map->n);
9868 for (i = 0; i < map->n; ++i) {
9869 isl_basic_map *bmap;
9871 bmap = isl_basic_map_copy(map->p[i]);
9872 list = isl_basic_map_list_add(list, bmap);
9875 return list;
9878 /* Return the intersection of the elements in the non-empty list "list".
9879 * All elements are assumed to live in the same space.
9881 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9882 __isl_take isl_basic_map_list *list)
9884 int i;
9885 isl_size n;
9886 isl_basic_map *bmap;
9888 n = isl_basic_map_list_n_basic_map(list);
9889 if (n < 0)
9890 goto error;
9891 if (n < 1)
9892 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9893 "expecting non-empty list", goto error);
9895 bmap = isl_basic_map_list_get_basic_map(list, 0);
9896 for (i = 1; i < n; ++i) {
9897 isl_basic_map *bmap_i;
9899 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9900 bmap = isl_basic_map_intersect(bmap, bmap_i);
9903 isl_basic_map_list_free(list);
9904 return bmap;
9905 error:
9906 isl_basic_map_list_free(list);
9907 return NULL;
9910 /* Return the intersection of the elements in the non-empty list "list".
9911 * All elements are assumed to live in the same space.
9913 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9914 __isl_take isl_basic_set_list *list)
9916 return isl_basic_map_list_intersect(list);
9919 /* Return the union of the elements of "list".
9920 * The list is required to have at least one element.
9922 __isl_give isl_set *isl_basic_set_list_union(
9923 __isl_take isl_basic_set_list *list)
9925 int i;
9926 isl_size n;
9927 isl_space *space;
9928 isl_basic_set *bset;
9929 isl_set *set;
9931 n = isl_basic_set_list_n_basic_set(list);
9932 if (n < 0)
9933 goto error;
9934 if (n < 1)
9935 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9936 "expecting non-empty list", goto error);
9938 bset = isl_basic_set_list_get_basic_set(list, 0);
9939 space = isl_basic_set_get_space(bset);
9940 isl_basic_set_free(bset);
9942 set = isl_set_alloc_space(space, n, 0);
9943 for (i = 0; i < n; ++i) {
9944 bset = isl_basic_set_list_get_basic_set(list, i);
9945 set = isl_set_add_basic_set(set, bset);
9948 isl_basic_set_list_free(list);
9949 return set;
9950 error:
9951 isl_basic_set_list_free(list);
9952 return NULL;
9955 /* Return the union of the elements in the non-empty list "list".
9956 * All elements are assumed to live in the same space.
9958 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9960 int i;
9961 isl_size n;
9962 isl_set *set;
9964 n = isl_set_list_n_set(list);
9965 if (n < 0)
9966 goto error;
9967 if (n < 1)
9968 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9969 "expecting non-empty list", goto error);
9971 set = isl_set_list_get_set(list, 0);
9972 for (i = 1; i < n; ++i) {
9973 isl_set *set_i;
9975 set_i = isl_set_list_get_set(list, i);
9976 set = isl_set_union(set, set_i);
9979 isl_set_list_free(list);
9980 return set;
9981 error:
9982 isl_set_list_free(list);
9983 return NULL;
9986 __isl_give isl_basic_map *isl_basic_map_product(
9987 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9989 isl_space *space_result = NULL;
9990 struct isl_basic_map *bmap;
9991 unsigned in1, in2, out1, out2, nparam, total, pos;
9992 struct isl_dim_map *dim_map1, *dim_map2;
9994 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9995 goto error;
9996 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9997 isl_space_copy(bmap2->dim));
9999 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10000 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10001 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10002 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10003 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10005 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10006 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10007 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10008 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10009 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10010 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10011 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10012 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10013 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10014 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10015 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10017 bmap = isl_basic_map_alloc_space(space_result,
10018 bmap1->n_div + bmap2->n_div,
10019 bmap1->n_eq + bmap2->n_eq,
10020 bmap1->n_ineq + bmap2->n_ineq);
10021 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10022 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10023 bmap = isl_basic_map_simplify(bmap);
10024 return isl_basic_map_finalize(bmap);
10025 error:
10026 isl_basic_map_free(bmap1);
10027 isl_basic_map_free(bmap2);
10028 return NULL;
10031 __isl_give isl_basic_map *isl_basic_map_flat_product(
10032 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10034 isl_basic_map *prod;
10036 prod = isl_basic_map_product(bmap1, bmap2);
10037 prod = isl_basic_map_flatten(prod);
10038 return prod;
10041 __isl_give isl_basic_set *isl_basic_set_flat_product(
10042 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10044 return isl_basic_map_flat_range_product(bset1, bset2);
10047 __isl_give isl_basic_map *isl_basic_map_domain_product(
10048 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10050 isl_space *space1, *space2;
10051 isl_space *space_result = NULL;
10052 isl_basic_map *bmap;
10053 isl_size in1, in2, out, nparam;
10054 unsigned total, pos;
10055 struct isl_dim_map *dim_map1, *dim_map2;
10057 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10058 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10059 out = isl_basic_map_dim(bmap1, isl_dim_out);
10060 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10061 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10062 goto error;
10064 space1 = isl_basic_map_get_space(bmap1);
10065 space2 = isl_basic_map_get_space(bmap2);
10066 space_result = isl_space_domain_product(space1, space2);
10068 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10069 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10070 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10073 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10074 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10075 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10076 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10077 isl_dim_map_div(dim_map1, bmap1, pos += out);
10078 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10080 bmap = isl_basic_map_alloc_space(space_result,
10081 bmap1->n_div + bmap2->n_div,
10082 bmap1->n_eq + bmap2->n_eq,
10083 bmap1->n_ineq + bmap2->n_ineq);
10084 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10085 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10086 bmap = isl_basic_map_simplify(bmap);
10087 return isl_basic_map_finalize(bmap);
10088 error:
10089 isl_basic_map_free(bmap1);
10090 isl_basic_map_free(bmap2);
10091 return NULL;
10094 __isl_give isl_basic_map *isl_basic_map_range_product(
10095 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10097 isl_bool rational;
10098 isl_space *space_result = NULL;
10099 isl_basic_map *bmap;
10100 isl_size in, out1, out2, nparam;
10101 unsigned total, pos;
10102 struct isl_dim_map *dim_map1, *dim_map2;
10104 rational = isl_basic_map_is_rational(bmap1);
10105 if (rational >= 0 && rational)
10106 rational = isl_basic_map_is_rational(bmap2);
10107 in = isl_basic_map_dim(bmap1, isl_dim_in);
10108 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10109 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10110 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10111 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10112 goto error;
10114 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10115 goto error;
10117 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10118 isl_space_copy(bmap2->dim));
10120 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10121 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10122 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10123 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10124 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10125 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10126 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10127 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10128 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10129 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10130 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10132 bmap = isl_basic_map_alloc_space(space_result,
10133 bmap1->n_div + bmap2->n_div,
10134 bmap1->n_eq + bmap2->n_eq,
10135 bmap1->n_ineq + bmap2->n_ineq);
10136 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10137 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10138 if (rational)
10139 bmap = isl_basic_map_set_rational(bmap);
10140 bmap = isl_basic_map_simplify(bmap);
10141 return isl_basic_map_finalize(bmap);
10142 error:
10143 isl_basic_map_free(bmap1);
10144 isl_basic_map_free(bmap2);
10145 return NULL;
10148 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10149 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10151 isl_basic_map *prod;
10153 prod = isl_basic_map_range_product(bmap1, bmap2);
10154 prod = isl_basic_map_flatten_range(prod);
10155 return prod;
10158 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10159 * and collect the results.
10160 * The result live in the space obtained by calling "space_product"
10161 * on the spaces of "map1" and "map2".
10162 * If "remove_duplicates" is set then the result may contain duplicates
10163 * (even if the inputs do not) and so we try and remove the obvious
10164 * duplicates.
10166 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10167 __isl_take isl_map *map2,
10168 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10169 __isl_take isl_space *right),
10170 __isl_give isl_basic_map *(*basic_map_product)(
10171 __isl_take isl_basic_map *left,
10172 __isl_take isl_basic_map *right),
10173 int remove_duplicates)
10175 unsigned flags = 0;
10176 struct isl_map *result;
10177 int i, j;
10178 isl_bool m;
10180 m = isl_map_has_equal_params(map1, map2);
10181 if (m < 0)
10182 goto error;
10183 if (!m)
10184 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10185 "parameters don't match", goto error);
10187 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10188 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10189 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10191 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10192 isl_space_copy(map2->dim)),
10193 map1->n * map2->n, flags);
10194 if (!result)
10195 goto error;
10196 for (i = 0; i < map1->n; ++i)
10197 for (j = 0; j < map2->n; ++j) {
10198 struct isl_basic_map *part;
10199 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10200 isl_basic_map_copy(map2->p[j]));
10201 if (isl_basic_map_is_empty(part))
10202 isl_basic_map_free(part);
10203 else
10204 result = isl_map_add_basic_map(result, part);
10205 if (!result)
10206 goto error;
10208 if (remove_duplicates)
10209 result = isl_map_remove_obvious_duplicates(result);
10210 isl_map_free(map1);
10211 isl_map_free(map2);
10212 return result;
10213 error:
10214 isl_map_free(map1);
10215 isl_map_free(map2);
10216 return NULL;
10219 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10221 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10222 __isl_take isl_map *map2)
10224 return map_product(map1, map2, &isl_space_product,
10225 &isl_basic_map_product, 0);
10228 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10229 __isl_take isl_map *map2)
10231 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10234 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10236 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10237 __isl_take isl_map *map2)
10239 isl_map *prod;
10241 prod = isl_map_product(map1, map2);
10242 prod = isl_map_flatten(prod);
10243 return prod;
10246 /* Given two set A and B, construct its Cartesian product A x B.
10248 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10250 return isl_map_range_product(set1, set2);
10253 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10254 __isl_take isl_set *set2)
10256 return isl_map_flat_range_product(set1, set2);
10259 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10261 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10262 __isl_take isl_map *map2)
10264 return map_product(map1, map2, &isl_space_domain_product,
10265 &isl_basic_map_domain_product, 1);
10268 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10270 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10271 __isl_take isl_map *map2)
10273 return map_product(map1, map2, &isl_space_range_product,
10274 &isl_basic_map_range_product, 1);
10277 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10278 __isl_take isl_map *map2)
10280 return isl_map_align_params_map_map_and(map1, map2,
10281 &map_domain_product_aligned);
10284 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10285 __isl_take isl_map *map2)
10287 return isl_map_align_params_map_map_and(map1, map2,
10288 &map_range_product_aligned);
10291 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10293 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10295 isl_space *space;
10296 isl_size total1, keep1, total2, keep2;
10298 total1 = isl_map_dim(map, isl_dim_in);
10299 total2 = isl_map_dim(map, isl_dim_out);
10300 if (total1 < 0 || total2 < 0)
10301 return isl_map_free(map);
10302 if (!isl_space_domain_is_wrapping(map->dim) ||
10303 !isl_space_range_is_wrapping(map->dim))
10304 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10305 "not a product", return isl_map_free(map));
10307 space = isl_map_get_space(map);
10308 space = isl_space_factor_domain(space);
10309 keep1 = isl_space_dim(space, isl_dim_in);
10310 keep2 = isl_space_dim(space, isl_dim_out);
10311 if (keep1 < 0 || keep2 < 0)
10312 map = isl_map_free(map);
10313 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10314 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10315 map = isl_map_reset_space(map, space);
10317 return map;
10320 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10322 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10324 isl_space *space;
10325 isl_size total1, keep1, total2, keep2;
10327 total1 = isl_map_dim(map, isl_dim_in);
10328 total2 = isl_map_dim(map, isl_dim_out);
10329 if (total1 < 0 || total2 < 0)
10330 return isl_map_free(map);
10331 if (!isl_space_domain_is_wrapping(map->dim) ||
10332 !isl_space_range_is_wrapping(map->dim))
10333 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10334 "not a product", return isl_map_free(map));
10336 space = isl_map_get_space(map);
10337 space = isl_space_factor_range(space);
10338 keep1 = isl_space_dim(space, isl_dim_in);
10339 keep2 = isl_space_dim(space, isl_dim_out);
10340 if (keep1 < 0 || keep2 < 0)
10341 map = isl_map_free(map);
10342 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10343 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10344 map = isl_map_reset_space(map, space);
10346 return map;
10349 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10351 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10353 isl_space *space;
10354 isl_size total, keep;
10356 total = isl_map_dim(map, isl_dim_in);
10357 if (total < 0)
10358 return isl_map_free(map);
10359 if (!isl_space_domain_is_wrapping(map->dim))
10360 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10361 "domain is not a product", return isl_map_free(map));
10363 space = isl_map_get_space(map);
10364 space = isl_space_domain_factor_domain(space);
10365 keep = isl_space_dim(space, isl_dim_in);
10366 if (keep < 0)
10367 map = isl_map_free(map);
10368 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10369 map = isl_map_reset_space(map, space);
10371 return map;
10374 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10376 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10378 isl_space *space;
10379 isl_size total, keep;
10381 total = isl_map_dim(map, isl_dim_in);
10382 if (total < 0)
10383 return isl_map_free(map);
10384 if (!isl_space_domain_is_wrapping(map->dim))
10385 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10386 "domain is not a product", return isl_map_free(map));
10388 space = isl_map_get_space(map);
10389 space = isl_space_domain_factor_range(space);
10390 keep = isl_space_dim(space, isl_dim_in);
10391 if (keep < 0)
10392 map = isl_map_free(map);
10393 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10394 map = isl_map_reset_space(map, space);
10396 return map;
10399 /* Given a map A -> [B -> C], extract the map A -> B.
10401 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10403 isl_space *space;
10404 isl_size total, keep;
10406 total = isl_map_dim(map, isl_dim_out);
10407 if (total < 0)
10408 return isl_map_free(map);
10409 if (!isl_space_range_is_wrapping(map->dim))
10410 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10411 "range is not a product", return isl_map_free(map));
10413 space = isl_map_get_space(map);
10414 space = isl_space_range_factor_domain(space);
10415 keep = isl_space_dim(space, isl_dim_out);
10416 if (keep < 0)
10417 map = isl_map_free(map);
10418 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10419 map = isl_map_reset_space(map, space);
10421 return map;
10424 /* Given a map A -> [B -> C], extract the map A -> C.
10426 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10428 isl_space *space;
10429 isl_size total, keep;
10431 total = isl_map_dim(map, isl_dim_out);
10432 if (total < 0)
10433 return isl_map_free(map);
10434 if (!isl_space_range_is_wrapping(map->dim))
10435 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10436 "range is not a product", return isl_map_free(map));
10438 space = isl_map_get_space(map);
10439 space = isl_space_range_factor_range(space);
10440 keep = isl_space_dim(space, isl_dim_out);
10441 if (keep < 0)
10442 map = isl_map_free(map);
10443 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10444 map = isl_map_reset_space(map, space);
10446 return map;
10449 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10451 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10452 __isl_take isl_map *map2)
10454 isl_map *prod;
10456 prod = isl_map_domain_product(map1, map2);
10457 prod = isl_map_flatten_domain(prod);
10458 return prod;
10461 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10463 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10464 __isl_take isl_map *map2)
10466 isl_map *prod;
10468 prod = isl_map_range_product(map1, map2);
10469 prod = isl_map_flatten_range(prod);
10470 return prod;
10473 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10475 int i;
10476 uint32_t hash = isl_hash_init();
10477 isl_size total;
10479 if (!bmap)
10480 return 0;
10481 bmap = isl_basic_map_copy(bmap);
10482 bmap = isl_basic_map_normalize(bmap);
10483 total = isl_basic_map_dim(bmap, isl_dim_all);
10484 if (total < 0)
10485 return 0;
10486 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10487 for (i = 0; i < bmap->n_eq; ++i) {
10488 uint32_t c_hash;
10489 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10490 isl_hash_hash(hash, c_hash);
10492 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10493 for (i = 0; i < bmap->n_ineq; ++i) {
10494 uint32_t c_hash;
10495 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10496 isl_hash_hash(hash, c_hash);
10498 isl_hash_byte(hash, bmap->n_div & 0xFF);
10499 for (i = 0; i < bmap->n_div; ++i) {
10500 uint32_t c_hash;
10501 if (isl_int_is_zero(bmap->div[i][0]))
10502 continue;
10503 isl_hash_byte(hash, i & 0xFF);
10504 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10505 isl_hash_hash(hash, c_hash);
10507 isl_basic_map_free(bmap);
10508 return hash;
10511 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10513 return isl_basic_map_get_hash(bset_to_bmap(bset));
10516 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10518 int i;
10519 uint32_t hash;
10521 if (!map)
10522 return 0;
10523 map = isl_map_copy(map);
10524 map = isl_map_normalize(map);
10525 if (!map)
10526 return 0;
10528 hash = isl_hash_init();
10529 for (i = 0; i < map->n; ++i) {
10530 uint32_t bmap_hash;
10531 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10532 isl_hash_hash(hash, bmap_hash);
10535 isl_map_free(map);
10537 return hash;
10540 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10542 return isl_map_get_hash(set_to_map(set));
10545 /* Return the number of basic maps in the (current) representation of "map".
10547 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
10549 return map ? map->n : isl_size_error;
10552 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
10554 return set ? set->n : isl_size_error;
10557 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10558 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10560 int i;
10562 if (!map)
10563 return isl_stat_error;
10565 for (i = 0; i < map->n; ++i)
10566 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10567 return isl_stat_error;
10569 return isl_stat_ok;
10572 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10573 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10575 int i;
10577 if (!set)
10578 return isl_stat_error;
10580 for (i = 0; i < set->n; ++i)
10581 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10582 return isl_stat_error;
10584 return isl_stat_ok;
10587 /* Return a list of basic sets, the union of which is equal to "set".
10589 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10590 __isl_keep isl_set *set)
10592 int i;
10593 isl_basic_set_list *list;
10595 if (!set)
10596 return NULL;
10598 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10599 for (i = 0; i < set->n; ++i) {
10600 isl_basic_set *bset;
10602 bset = isl_basic_set_copy(set->p[i]);
10603 list = isl_basic_set_list_add(list, bset);
10606 return list;
10609 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10611 isl_space *space;
10613 if (!bset)
10614 return NULL;
10616 bset = isl_basic_set_cow(bset);
10617 if (!bset)
10618 return NULL;
10620 space = isl_basic_set_get_space(bset);
10621 space = isl_space_lift(space, bset->n_div);
10622 if (!space)
10623 goto error;
10624 isl_space_free(bset->dim);
10625 bset->dim = space;
10626 bset->extra -= bset->n_div;
10627 bset->n_div = 0;
10629 bset = isl_basic_set_finalize(bset);
10631 return bset;
10632 error:
10633 isl_basic_set_free(bset);
10634 return NULL;
10637 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10639 int i;
10640 isl_space *space;
10641 unsigned n_div;
10643 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10645 if (!set)
10646 return NULL;
10648 set = isl_set_cow(set);
10649 if (!set)
10650 return NULL;
10652 n_div = set->p[0]->n_div;
10653 space = isl_set_get_space(set);
10654 space = isl_space_lift(space, n_div);
10655 if (!space)
10656 goto error;
10657 isl_space_free(set->dim);
10658 set->dim = space;
10660 for (i = 0; i < set->n; ++i) {
10661 set->p[i] = isl_basic_set_lift(set->p[i]);
10662 if (!set->p[i])
10663 goto error;
10666 return set;
10667 error:
10668 isl_set_free(set);
10669 return NULL;
10672 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10674 isl_size dim;
10675 int size = 0;
10677 dim = isl_basic_set_dim(bset, isl_dim_all);
10678 if (dim < 0)
10679 return -1;
10680 size += bset->n_eq * (1 + dim);
10681 size += bset->n_ineq * (1 + dim);
10682 size += bset->n_div * (2 + dim);
10684 return size;
10687 int isl_set_size(__isl_keep isl_set *set)
10689 int i;
10690 int size = 0;
10692 if (!set)
10693 return -1;
10695 for (i = 0; i < set->n; ++i)
10696 size += isl_basic_set_size(set->p[i]);
10698 return size;
10701 /* Check if there is any lower bound (if lower == 0) and/or upper
10702 * bound (if upper == 0) on the specified dim.
10704 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10705 enum isl_dim_type type, unsigned pos, int lower, int upper)
10707 int i;
10709 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10710 return isl_bool_error;
10712 pos += isl_basic_map_offset(bmap, type);
10714 for (i = 0; i < bmap->n_div; ++i) {
10715 if (isl_int_is_zero(bmap->div[i][0]))
10716 continue;
10717 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10718 return isl_bool_true;
10721 for (i = 0; i < bmap->n_eq; ++i)
10722 if (!isl_int_is_zero(bmap->eq[i][pos]))
10723 return isl_bool_true;
10725 for (i = 0; i < bmap->n_ineq; ++i) {
10726 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10727 if (sgn > 0)
10728 lower = 1;
10729 if (sgn < 0)
10730 upper = 1;
10733 return lower && upper;
10736 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10737 enum isl_dim_type type, unsigned pos)
10739 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10742 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10743 enum isl_dim_type type, unsigned pos)
10745 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10748 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10749 enum isl_dim_type type, unsigned pos)
10751 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10754 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10755 enum isl_dim_type type, unsigned pos)
10757 int i;
10759 if (!map)
10760 return isl_bool_error;
10762 for (i = 0; i < map->n; ++i) {
10763 isl_bool bounded;
10764 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10765 if (bounded < 0 || !bounded)
10766 return bounded;
10769 return isl_bool_true;
10772 /* Return true if the specified dim is involved in both an upper bound
10773 * and a lower bound.
10775 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10776 enum isl_dim_type type, unsigned pos)
10778 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10781 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10783 static isl_bool has_any_bound(__isl_keep isl_map *map,
10784 enum isl_dim_type type, unsigned pos,
10785 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10786 enum isl_dim_type type, unsigned pos))
10788 int i;
10790 if (!map)
10791 return isl_bool_error;
10793 for (i = 0; i < map->n; ++i) {
10794 isl_bool bounded;
10795 bounded = fn(map->p[i], type, pos);
10796 if (bounded < 0 || bounded)
10797 return bounded;
10800 return isl_bool_false;
10803 /* Return 1 if the specified dim is involved in any lower bound.
10805 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10806 enum isl_dim_type type, unsigned pos)
10808 return has_any_bound(set, type, pos,
10809 &isl_basic_map_dim_has_lower_bound);
10812 /* Return 1 if the specified dim is involved in any upper bound.
10814 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10815 enum isl_dim_type type, unsigned pos)
10817 return has_any_bound(set, type, pos,
10818 &isl_basic_map_dim_has_upper_bound);
10821 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10823 static isl_bool has_bound(__isl_keep isl_map *map,
10824 enum isl_dim_type type, unsigned pos,
10825 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10826 enum isl_dim_type type, unsigned pos))
10828 int i;
10830 if (!map)
10831 return isl_bool_error;
10833 for (i = 0; i < map->n; ++i) {
10834 isl_bool bounded;
10835 bounded = fn(map->p[i], type, pos);
10836 if (bounded < 0 || !bounded)
10837 return bounded;
10840 return isl_bool_true;
10843 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10845 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10846 enum isl_dim_type type, unsigned pos)
10848 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10851 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10853 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10854 enum isl_dim_type type, unsigned pos)
10856 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10859 /* For each of the "n" variables starting at "first", determine
10860 * the sign of the variable and put the results in the first "n"
10861 * elements of the array "signs".
10862 * Sign
10863 * 1 means that the variable is non-negative
10864 * -1 means that the variable is non-positive
10865 * 0 means the variable attains both positive and negative values.
10867 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10868 unsigned first, unsigned n, int *signs)
10870 isl_vec *bound = NULL;
10871 struct isl_tab *tab = NULL;
10872 struct isl_tab_undo *snap;
10873 int i;
10874 isl_size total;
10876 total = isl_basic_set_dim(bset, isl_dim_all);
10877 if (total < 0 || !signs)
10878 return isl_stat_error;
10880 bound = isl_vec_alloc(bset->ctx, 1 + total);
10881 tab = isl_tab_from_basic_set(bset, 0);
10882 if (!bound || !tab)
10883 goto error;
10885 isl_seq_clr(bound->el, bound->size);
10886 isl_int_set_si(bound->el[0], -1);
10888 snap = isl_tab_snap(tab);
10889 for (i = 0; i < n; ++i) {
10890 int empty;
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 if (empty) {
10901 signs[i] = 1;
10902 continue;
10905 isl_int_set_si(bound->el[1 + first + i], 1);
10906 if (isl_tab_add_ineq(tab, bound->el) < 0)
10907 goto error;
10908 empty = tab->empty;
10909 isl_int_set_si(bound->el[1 + first + i], 0);
10910 if (isl_tab_rollback(tab, snap) < 0)
10911 goto error;
10913 signs[i] = empty ? -1 : 0;
10916 isl_tab_free(tab);
10917 isl_vec_free(bound);
10918 return isl_stat_ok;
10919 error:
10920 isl_tab_free(tab);
10921 isl_vec_free(bound);
10922 return isl_stat_error;
10925 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10926 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10928 if (!bset || !signs)
10929 return isl_stat_error;
10930 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10931 return isl_stat_error;
10933 first += pos(bset->dim, type) - 1;
10934 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10937 /* Is it possible for the integer division "div" to depend (possibly
10938 * indirectly) on any output dimensions?
10940 * If the div is undefined, then we conservatively assume that it
10941 * may depend on them.
10942 * Otherwise, we check if it actually depends on them or on any integer
10943 * divisions that may depend on them.
10945 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10947 int i;
10948 isl_size n_out, n_div;
10949 unsigned o_out, o_div;
10951 if (isl_int_is_zero(bmap->div[div][0]))
10952 return isl_bool_true;
10954 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10955 if (n_out < 0)
10956 return isl_bool_error;
10957 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10959 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10960 return isl_bool_true;
10962 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10963 if (n_div < 0)
10964 return isl_bool_error;
10965 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10967 for (i = 0; i < n_div; ++i) {
10968 isl_bool may_involve;
10970 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10971 continue;
10972 may_involve = div_may_involve_output(bmap, i);
10973 if (may_involve < 0 || may_involve)
10974 return may_involve;
10977 return isl_bool_false;
10980 /* Return the first integer division of "bmap" in the range
10981 * [first, first + n[ that may depend on any output dimensions and
10982 * that has a non-zero coefficient in "c" (where the first coefficient
10983 * in "c" corresponds to integer division "first").
10985 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10986 isl_int *c, int first, int n)
10988 int k;
10990 if (!bmap)
10991 return -1;
10993 for (k = first; k < first + n; ++k) {
10994 isl_bool may_involve;
10996 if (isl_int_is_zero(c[k]))
10997 continue;
10998 may_involve = div_may_involve_output(bmap, k);
10999 if (may_involve < 0)
11000 return -1;
11001 if (may_involve)
11002 return k;
11005 return first + n;
11008 /* Look for a pair of inequality constraints in "bmap" of the form
11010 * -l + i >= 0 or i >= l
11011 * and
11012 * n + l - i >= 0 or i <= l + n
11014 * with n < "m" and i the output dimension at position "pos".
11015 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11016 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11017 * and earlier output dimensions, as well as integer divisions that do
11018 * not involve any of the output dimensions.
11020 * Return the index of the first inequality constraint or bmap->n_ineq
11021 * if no such pair can be found.
11023 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11024 int pos, isl_int m)
11026 int i, j;
11027 isl_ctx *ctx;
11028 isl_size total;
11029 isl_size n_div, n_out;
11030 unsigned o_div, o_out;
11031 int less;
11033 total = isl_basic_map_dim(bmap, isl_dim_all);
11034 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11035 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11036 if (total < 0 || n_out < 0 || n_div < 0)
11037 return -1;
11039 ctx = isl_basic_map_get_ctx(bmap);
11040 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11041 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11042 for (i = 0; i < bmap->n_ineq; ++i) {
11043 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11044 continue;
11045 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11046 n_out - (pos + 1)) != -1)
11047 continue;
11048 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11049 0, n_div) < n_div)
11050 continue;
11051 for (j = i + 1; j < bmap->n_ineq; ++j) {
11052 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11053 ctx->one))
11054 continue;
11055 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11056 bmap->ineq[j] + 1, total))
11057 continue;
11058 break;
11060 if (j >= bmap->n_ineq)
11061 continue;
11062 isl_int_add(bmap->ineq[i][0],
11063 bmap->ineq[i][0], bmap->ineq[j][0]);
11064 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11065 isl_int_sub(bmap->ineq[i][0],
11066 bmap->ineq[i][0], bmap->ineq[j][0]);
11067 if (!less)
11068 continue;
11069 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11070 return i;
11071 else
11072 return j;
11075 return bmap->n_ineq;
11078 /* Return the index of the equality of "bmap" that defines
11079 * the output dimension "pos" in terms of earlier dimensions.
11080 * The equality may also involve integer divisions, as long
11081 * as those integer divisions are defined in terms of
11082 * parameters or input dimensions.
11083 * In this case, *div is set to the number of integer divisions and
11084 * *ineq is set to the number of inequality constraints (provided
11085 * div and ineq are not NULL).
11087 * The equality may also involve a single integer division involving
11088 * the output dimensions (typically only output dimension "pos") as
11089 * long as the coefficient of output dimension "pos" is 1 or -1 and
11090 * there is a pair of constraints i >= l and i <= l + n, with i referring
11091 * to output dimension "pos", l an expression involving only earlier
11092 * dimensions and n smaller than the coefficient of the integer division
11093 * in the equality. In this case, the output dimension can be defined
11094 * in terms of a modulo expression that does not involve the integer division.
11095 * *div is then set to this single integer division and
11096 * *ineq is set to the index of constraint i >= l.
11098 * Return bmap->n_eq if there is no such equality.
11099 * Return -1 on error.
11101 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11102 int pos, int *div, int *ineq)
11104 int j, k, l;
11105 isl_size n_div, n_out;
11106 unsigned o_div, o_out;
11108 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11109 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11110 if (n_out < 0 || n_div < 0)
11111 return -1;
11113 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11114 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11116 if (ineq)
11117 *ineq = bmap->n_ineq;
11118 if (div)
11119 *div = n_div;
11120 for (j = 0; j < bmap->n_eq; ++j) {
11121 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11122 continue;
11123 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11124 n_out - (pos + 1)) != -1)
11125 continue;
11126 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11127 0, n_div);
11128 if (k >= n_div)
11129 return j;
11130 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11131 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11132 continue;
11133 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11134 k + 1, n_div - (k+1)) < n_div)
11135 continue;
11136 l = find_modulo_constraint_pair(bmap, pos,
11137 bmap->eq[j][o_div + k]);
11138 if (l < 0)
11139 return -1;
11140 if (l >= bmap->n_ineq)
11141 continue;
11142 if (div)
11143 *div = k;
11144 if (ineq)
11145 *ineq = l;
11146 return j;
11149 return bmap->n_eq;
11152 /* Check if the given basic map is obviously single-valued.
11153 * In particular, for each output dimension, check that there is
11154 * an equality that defines the output dimension in terms of
11155 * earlier dimensions.
11157 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11159 int i;
11160 isl_size n_out;
11162 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11163 if (n_out < 0)
11164 return isl_bool_error;
11166 for (i = 0; i < n_out; ++i) {
11167 int eq;
11169 eq = isl_basic_map_output_defining_equality(bmap, i,
11170 NULL, NULL);
11171 if (eq < 0)
11172 return isl_bool_error;
11173 if (eq >= bmap->n_eq)
11174 return isl_bool_false;
11177 return isl_bool_true;
11180 /* Check if the given basic map is single-valued.
11181 * We simply compute
11183 * M \circ M^-1
11185 * and check if the result is a subset of the identity mapping.
11187 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11189 isl_space *space;
11190 isl_basic_map *test;
11191 isl_basic_map *id;
11192 isl_bool sv;
11194 sv = isl_basic_map_plain_is_single_valued(bmap);
11195 if (sv < 0 || sv)
11196 return sv;
11198 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11199 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11201 space = isl_basic_map_get_space(bmap);
11202 space = isl_space_map_from_set(isl_space_range(space));
11203 id = isl_basic_map_identity(space);
11205 sv = isl_basic_map_is_subset(test, id);
11207 isl_basic_map_free(test);
11208 isl_basic_map_free(id);
11210 return sv;
11213 /* Check if the given map is obviously single-valued.
11215 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11217 if (!map)
11218 return isl_bool_error;
11219 if (map->n == 0)
11220 return isl_bool_true;
11221 if (map->n >= 2)
11222 return isl_bool_false;
11224 return isl_basic_map_plain_is_single_valued(map->p[0]);
11227 /* Check if the given map is single-valued.
11228 * We simply compute
11230 * M \circ M^-1
11232 * and check if the result is a subset of the identity mapping.
11234 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11236 isl_space *dim;
11237 isl_map *test;
11238 isl_map *id;
11239 isl_bool sv;
11241 sv = isl_map_plain_is_single_valued(map);
11242 if (sv < 0 || sv)
11243 return sv;
11245 test = isl_map_reverse(isl_map_copy(map));
11246 test = isl_map_apply_range(test, isl_map_copy(map));
11248 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11249 id = isl_map_identity(dim);
11251 sv = isl_map_is_subset(test, id);
11253 isl_map_free(test);
11254 isl_map_free(id);
11256 return sv;
11259 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11261 isl_bool in;
11263 map = isl_map_copy(map);
11264 map = isl_map_reverse(map);
11265 in = isl_map_is_single_valued(map);
11266 isl_map_free(map);
11268 return in;
11271 /* Check if the given map is obviously injective.
11273 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11275 isl_bool in;
11277 map = isl_map_copy(map);
11278 map = isl_map_reverse(map);
11279 in = isl_map_plain_is_single_valued(map);
11280 isl_map_free(map);
11282 return in;
11285 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11287 isl_bool sv;
11289 sv = isl_map_is_single_valued(map);
11290 if (sv < 0 || !sv)
11291 return sv;
11293 return isl_map_is_injective(map);
11296 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11298 return isl_map_is_single_valued(set_to_map(set));
11301 /* Does "map" only map elements to themselves?
11303 * If the domain and range spaces are different, then "map"
11304 * is considered not to be an identity relation, even if it is empty.
11305 * Otherwise, construct the maximal identity relation and
11306 * check whether "map" is a subset of this relation.
11308 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11310 isl_space *space;
11311 isl_map *id;
11312 isl_bool equal, is_identity;
11314 space = isl_map_get_space(map);
11315 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11316 isl_space_free(space);
11317 if (equal < 0 || !equal)
11318 return equal;
11320 id = isl_map_identity(isl_map_get_space(map));
11321 is_identity = isl_map_is_subset(map, id);
11322 isl_map_free(id);
11324 return is_identity;
11327 int isl_map_is_translation(__isl_keep isl_map *map)
11329 int ok;
11330 isl_set *delta;
11332 delta = isl_map_deltas(isl_map_copy(map));
11333 ok = isl_set_is_singleton(delta);
11334 isl_set_free(delta);
11336 return ok;
11339 static int unique(isl_int *p, unsigned pos, unsigned len)
11341 if (isl_seq_first_non_zero(p, pos) != -1)
11342 return 0;
11343 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11344 return 0;
11345 return 1;
11348 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11350 int i, j;
11351 isl_size nvar, n_div;
11352 unsigned ovar;
11354 n_div = isl_basic_set_dim(bset, isl_dim_div);
11355 if (n_div < 0)
11356 return isl_bool_error;
11357 if (n_div != 0)
11358 return isl_bool_false;
11360 nvar = isl_basic_set_dim(bset, isl_dim_set);
11361 if (nvar < 0)
11362 return isl_bool_error;
11363 ovar = isl_space_offset(bset->dim, isl_dim_set);
11364 for (j = 0; j < nvar; ++j) {
11365 int lower = 0, upper = 0;
11366 for (i = 0; i < bset->n_eq; ++i) {
11367 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11368 continue;
11369 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11370 return isl_bool_false;
11371 break;
11373 if (i < bset->n_eq)
11374 continue;
11375 for (i = 0; i < bset->n_ineq; ++i) {
11376 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11377 continue;
11378 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11379 return isl_bool_false;
11380 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11381 lower = 1;
11382 else
11383 upper = 1;
11385 if (!lower || !upper)
11386 return isl_bool_false;
11389 return isl_bool_true;
11392 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11394 if (!set)
11395 return isl_bool_error;
11396 if (set->n != 1)
11397 return isl_bool_false;
11399 return isl_basic_set_is_box(set->p[0]);
11402 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11404 if (!bset)
11405 return isl_bool_error;
11407 return isl_space_is_wrapping(bset->dim);
11410 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11412 if (!set)
11413 return isl_bool_error;
11415 return isl_space_is_wrapping(set->dim);
11418 /* Modify the space of "map" through a call to "change".
11419 * If "can_change" is set (not NULL), then first call it to check
11420 * if the modification is allowed, printing the error message "cannot_change"
11421 * if it is not.
11423 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11424 isl_bool (*can_change)(__isl_keep isl_map *map),
11425 const char *cannot_change,
11426 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11428 isl_bool ok;
11429 isl_space *space;
11431 if (!map)
11432 return NULL;
11434 ok = can_change ? can_change(map) : isl_bool_true;
11435 if (ok < 0)
11436 return isl_map_free(map);
11437 if (!ok)
11438 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11439 return isl_map_free(map));
11441 space = change(isl_map_get_space(map));
11442 map = isl_map_reset_space(map, space);
11444 return map;
11447 /* Is the domain of "map" a wrapped relation?
11449 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11451 if (!map)
11452 return isl_bool_error;
11454 return isl_space_domain_is_wrapping(map->dim);
11457 /* Does "map" have a wrapped relation in both domain and range?
11459 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11461 return isl_space_is_product(isl_map_peek_space(map));
11464 /* Is the range of "map" a wrapped relation?
11466 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11468 if (!map)
11469 return isl_bool_error;
11471 return isl_space_range_is_wrapping(map->dim);
11474 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11476 bmap = isl_basic_map_cow(bmap);
11477 if (!bmap)
11478 return NULL;
11480 bmap->dim = isl_space_wrap(bmap->dim);
11481 if (!bmap->dim)
11482 goto error;
11484 bmap = isl_basic_map_finalize(bmap);
11486 return bset_from_bmap(bmap);
11487 error:
11488 isl_basic_map_free(bmap);
11489 return NULL;
11492 /* Given a map A -> B, return the set (A -> B).
11494 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11496 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11499 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11501 bset = isl_basic_set_cow(bset);
11502 if (!bset)
11503 return NULL;
11505 bset->dim = isl_space_unwrap(bset->dim);
11506 if (!bset->dim)
11507 goto error;
11509 bset = isl_basic_set_finalize(bset);
11511 return bset_to_bmap(bset);
11512 error:
11513 isl_basic_set_free(bset);
11514 return NULL;
11517 /* Given a set (A -> B), return the map A -> B.
11518 * Error out if "set" is not of the form (A -> B).
11520 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11522 return isl_map_change_space(set, &isl_set_is_wrapping,
11523 "not a wrapping set", &isl_space_unwrap);
11526 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11527 enum isl_dim_type type)
11529 if (!bmap)
11530 return NULL;
11532 if (!isl_space_is_named_or_nested(bmap->dim, type))
11533 return bmap;
11535 bmap = isl_basic_map_cow(bmap);
11536 if (!bmap)
11537 return NULL;
11539 bmap->dim = isl_space_reset(bmap->dim, type);
11540 if (!bmap->dim)
11541 goto error;
11543 bmap = isl_basic_map_finalize(bmap);
11545 return bmap;
11546 error:
11547 isl_basic_map_free(bmap);
11548 return NULL;
11551 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11552 enum isl_dim_type type)
11554 int i;
11556 if (!map)
11557 return NULL;
11559 if (!isl_space_is_named_or_nested(map->dim, type))
11560 return map;
11562 map = isl_map_cow(map);
11563 if (!map)
11564 return NULL;
11566 for (i = 0; i < map->n; ++i) {
11567 map->p[i] = isl_basic_map_reset(map->p[i], type);
11568 if (!map->p[i])
11569 goto error;
11571 map->dim = isl_space_reset(map->dim, type);
11572 if (!map->dim)
11573 goto error;
11575 return map;
11576 error:
11577 isl_map_free(map);
11578 return NULL;
11581 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11583 if (!bmap)
11584 return NULL;
11586 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11587 return bmap;
11589 bmap = isl_basic_map_cow(bmap);
11590 if (!bmap)
11591 return NULL;
11593 bmap->dim = isl_space_flatten(bmap->dim);
11594 if (!bmap->dim)
11595 goto error;
11597 bmap = isl_basic_map_finalize(bmap);
11599 return bmap;
11600 error:
11601 isl_basic_map_free(bmap);
11602 return NULL;
11605 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11607 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11610 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11611 __isl_take isl_basic_map *bmap)
11613 if (!bmap)
11614 return NULL;
11616 if (!bmap->dim->nested[0])
11617 return bmap;
11619 bmap = isl_basic_map_cow(bmap);
11620 if (!bmap)
11621 return NULL;
11623 bmap->dim = isl_space_flatten_domain(bmap->dim);
11624 if (!bmap->dim)
11625 goto error;
11627 bmap = isl_basic_map_finalize(bmap);
11629 return bmap;
11630 error:
11631 isl_basic_map_free(bmap);
11632 return NULL;
11635 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11636 __isl_take isl_basic_map *bmap)
11638 if (!bmap)
11639 return NULL;
11641 if (!bmap->dim->nested[1])
11642 return bmap;
11644 bmap = isl_basic_map_cow(bmap);
11645 if (!bmap)
11646 return NULL;
11648 bmap->dim = isl_space_flatten_range(bmap->dim);
11649 if (!bmap->dim)
11650 goto error;
11652 bmap = isl_basic_map_finalize(bmap);
11654 return bmap;
11655 error:
11656 isl_basic_map_free(bmap);
11657 return NULL;
11660 /* Remove any internal structure from the spaces of domain and range of "map".
11662 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11664 if (!map)
11665 return NULL;
11667 if (!map->dim->nested[0] && !map->dim->nested[1])
11668 return map;
11670 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11673 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11675 return set_from_map(isl_map_flatten(set_to_map(set)));
11678 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11680 isl_space *space, *flat_space;
11681 isl_map *map;
11683 space = isl_set_get_space(set);
11684 flat_space = isl_space_flatten(isl_space_copy(space));
11685 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11686 flat_space));
11687 map = isl_map_intersect_domain(map, set);
11689 return map;
11692 /* Remove any internal structure from the space of the domain of "map".
11694 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11696 if (!map)
11697 return NULL;
11699 if (!map->dim->nested[0])
11700 return map;
11702 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11705 /* Remove any internal structure from the space of the range of "map".
11707 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11709 if (!map)
11710 return NULL;
11712 if (!map->dim->nested[1])
11713 return map;
11715 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11718 /* Reorder the dimensions of "bmap" according to the given dim_map
11719 * and set the dimension specification to "space" and
11720 * perform Gaussian elimination on the result.
11722 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11723 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11725 isl_basic_map *res;
11726 unsigned flags;
11727 isl_size n_div;
11729 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11730 if (n_div < 0 || !space || !dim_map)
11731 goto error;
11733 flags = bmap->flags;
11734 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11735 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11736 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11737 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11738 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11739 if (res)
11740 res->flags = flags;
11741 res = isl_basic_map_gauss(res, NULL);
11742 res = isl_basic_map_finalize(res);
11743 return res;
11744 error:
11745 isl_dim_map_free(dim_map);
11746 isl_basic_map_free(bmap);
11747 isl_space_free(space);
11748 return NULL;
11751 /* Reorder the dimensions of "map" according to given reordering.
11753 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11754 __isl_take isl_reordering *r)
11756 int i;
11757 struct isl_dim_map *dim_map;
11759 map = isl_map_cow(map);
11760 dim_map = isl_dim_map_from_reordering(r);
11761 if (!map || !r || !dim_map)
11762 goto error;
11764 for (i = 0; i < map->n; ++i) {
11765 struct isl_dim_map *dim_map_i;
11766 isl_space *space;
11768 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11770 space = isl_reordering_get_space(r);
11771 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11773 if (!map->p[i])
11774 goto error;
11777 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11778 map = isl_map_unmark_normalized(map);
11780 isl_reordering_free(r);
11781 isl_dim_map_free(dim_map);
11782 return map;
11783 error:
11784 isl_dim_map_free(dim_map);
11785 isl_map_free(map);
11786 isl_reordering_free(r);
11787 return NULL;
11790 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11791 __isl_take isl_reordering *r)
11793 return set_from_map(isl_map_realign(set_to_map(set), r));
11796 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11797 __isl_take isl_space *model)
11799 isl_ctx *ctx;
11800 isl_bool aligned;
11802 if (!map || !model)
11803 goto error;
11805 ctx = isl_space_get_ctx(model);
11806 if (!isl_space_has_named_params(model))
11807 isl_die(ctx, isl_error_invalid,
11808 "model has unnamed parameters", goto error);
11809 if (isl_map_check_named_params(map) < 0)
11810 goto error;
11811 aligned = isl_map_space_has_equal_params(map, model);
11812 if (aligned < 0)
11813 goto error;
11814 if (!aligned) {
11815 isl_reordering *exp;
11817 exp = isl_parameter_alignment_reordering(map->dim, model);
11818 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11819 map = isl_map_realign(map, exp);
11822 isl_space_free(model);
11823 return map;
11824 error:
11825 isl_space_free(model);
11826 isl_map_free(map);
11827 return NULL;
11830 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11831 __isl_take isl_space *model)
11833 return isl_map_align_params(set, model);
11836 /* Align the parameters of "bmap" to those of "model", introducing
11837 * additional parameters if needed.
11839 __isl_give isl_basic_map *isl_basic_map_align_params(
11840 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11842 isl_ctx *ctx;
11843 isl_bool equal_params;
11845 if (!bmap || !model)
11846 goto error;
11848 ctx = isl_space_get_ctx(model);
11849 if (!isl_space_has_named_params(model))
11850 isl_die(ctx, isl_error_invalid,
11851 "model has unnamed parameters", goto error);
11852 if (isl_basic_map_check_named_params(bmap) < 0)
11853 goto error;
11854 equal_params = isl_space_has_equal_params(bmap->dim, model);
11855 if (equal_params < 0)
11856 goto error;
11857 if (!equal_params) {
11858 isl_reordering *exp;
11859 struct isl_dim_map *dim_map;
11861 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11862 exp = isl_reordering_extend_space(exp,
11863 isl_basic_map_get_space(bmap));
11864 dim_map = isl_dim_map_from_reordering(exp);
11865 bmap = isl_basic_map_realign(bmap,
11866 isl_reordering_get_space(exp),
11867 isl_dim_map_extend(dim_map, bmap));
11868 isl_reordering_free(exp);
11869 isl_dim_map_free(dim_map);
11872 isl_space_free(model);
11873 return bmap;
11874 error:
11875 isl_space_free(model);
11876 isl_basic_map_free(bmap);
11877 return NULL;
11880 /* Do "bset" and "space" have the same parameters?
11882 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11883 __isl_keep isl_space *space)
11885 isl_space *bset_space;
11887 bset_space = isl_basic_set_peek_space(bset);
11888 return isl_space_has_equal_params(bset_space, space);
11891 /* Do "map" and "space" have the same parameters?
11893 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11894 __isl_keep isl_space *space)
11896 isl_space *map_space;
11898 map_space = isl_map_peek_space(map);
11899 return isl_space_has_equal_params(map_space, space);
11902 /* Do "set" and "space" have the same parameters?
11904 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11905 __isl_keep isl_space *space)
11907 return isl_map_space_has_equal_params(set_to_map(set), space);
11910 /* Align the parameters of "bset" to those of "model", introducing
11911 * additional parameters if needed.
11913 __isl_give isl_basic_set *isl_basic_set_align_params(
11914 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11916 return isl_basic_map_align_params(bset, model);
11919 /* Drop all parameters not referenced by "map".
11921 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11923 int i;
11924 isl_size n;
11926 n = isl_map_dim(map, isl_dim_param);
11927 if (isl_map_check_named_params(map) < 0 || n < 0)
11928 return isl_map_free(map);
11930 for (i = n - 1; i >= 0; i--) {
11931 isl_bool involves;
11933 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11934 if (involves < 0)
11935 return isl_map_free(map);
11936 if (!involves)
11937 map = isl_map_project_out(map, isl_dim_param, i, 1);
11940 return map;
11943 /* Drop all parameters not referenced by "set".
11945 __isl_give isl_set *isl_set_drop_unused_params(
11946 __isl_take isl_set *set)
11948 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11951 /* Drop all parameters not referenced by "bmap".
11953 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11954 __isl_take isl_basic_map *bmap)
11956 isl_size nparam;
11957 int i;
11959 nparam = isl_basic_map_dim(bmap, isl_dim_param);
11960 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
11961 return isl_basic_map_free(bmap);
11963 for (i = nparam - 1; i >= 0; i--) {
11964 isl_bool involves;
11966 involves = isl_basic_map_involves_dims(bmap,
11967 isl_dim_param, i, 1);
11968 if (involves < 0)
11969 return isl_basic_map_free(bmap);
11970 if (!involves)
11971 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11974 return bmap;
11977 /* Drop all parameters not referenced by "bset".
11979 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11980 __isl_take isl_basic_set *bset)
11982 return bset_from_bmap(isl_basic_map_drop_unused_params(
11983 bset_to_bmap(bset)));
11986 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11987 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11988 enum isl_dim_type c2, enum isl_dim_type c3,
11989 enum isl_dim_type c4, enum isl_dim_type c5)
11991 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11992 struct isl_mat *mat;
11993 int i, j, k;
11994 int pos;
11995 isl_size total;
11997 total = isl_basic_map_dim(bmap, isl_dim_all);
11998 if (total < 0)
11999 return NULL;
12000 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12001 if (!mat)
12002 return NULL;
12003 for (i = 0; i < bmap->n_eq; ++i)
12004 for (j = 0, pos = 0; j < 5; ++j) {
12005 int off = isl_basic_map_offset(bmap, c[j]);
12006 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12007 if (dim < 0)
12008 return isl_mat_free(mat);
12009 for (k = 0; k < dim; ++k) {
12010 isl_int_set(mat->row[i][pos],
12011 bmap->eq[i][off + k]);
12012 ++pos;
12016 return mat;
12019 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12020 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12021 enum isl_dim_type c2, enum isl_dim_type c3,
12022 enum isl_dim_type c4, enum isl_dim_type c5)
12024 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12025 struct isl_mat *mat;
12026 int i, j, k;
12027 int pos;
12028 isl_size total;
12030 total = isl_basic_map_dim(bmap, isl_dim_all);
12031 if (total < 0)
12032 return NULL;
12033 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12034 if (!mat)
12035 return NULL;
12036 for (i = 0; i < bmap->n_ineq; ++i)
12037 for (j = 0, pos = 0; j < 5; ++j) {
12038 int off = isl_basic_map_offset(bmap, c[j]);
12039 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12040 if (dim < 0)
12041 return isl_mat_free(mat);
12042 for (k = 0; k < dim; ++k) {
12043 isl_int_set(mat->row[i][pos],
12044 bmap->ineq[i][off + k]);
12045 ++pos;
12049 return mat;
12052 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12053 __isl_take isl_space *space,
12054 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12055 enum isl_dim_type c2, enum isl_dim_type c3,
12056 enum isl_dim_type c4, enum isl_dim_type c5)
12058 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12059 isl_basic_map *bmap = NULL;
12060 isl_size dim;
12061 unsigned total;
12062 unsigned extra;
12063 int i, j, k, l;
12064 int pos;
12066 dim = isl_space_dim(space, isl_dim_all);
12067 if (dim < 0 || !eq || !ineq)
12068 goto error;
12070 if (eq->n_col != ineq->n_col)
12071 isl_die(space->ctx, isl_error_invalid,
12072 "equalities and inequalities matrices should have "
12073 "same number of columns", goto error);
12075 total = 1 + dim;
12077 if (eq->n_col < total)
12078 isl_die(space->ctx, isl_error_invalid,
12079 "number of columns too small", goto error);
12081 extra = eq->n_col - total;
12083 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12084 eq->n_row, ineq->n_row);
12085 if (!bmap)
12086 goto error;
12087 for (i = 0; i < extra; ++i) {
12088 k = isl_basic_map_alloc_div(bmap);
12089 if (k < 0)
12090 goto error;
12091 isl_int_set_si(bmap->div[k][0], 0);
12093 for (i = 0; i < eq->n_row; ++i) {
12094 l = isl_basic_map_alloc_equality(bmap);
12095 if (l < 0)
12096 goto error;
12097 for (j = 0, pos = 0; j < 5; ++j) {
12098 int off = isl_basic_map_offset(bmap, c[j]);
12099 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12100 if (dim < 0)
12101 goto error;
12102 for (k = 0; k < dim; ++k) {
12103 isl_int_set(bmap->eq[l][off + k],
12104 eq->row[i][pos]);
12105 ++pos;
12109 for (i = 0; i < ineq->n_row; ++i) {
12110 l = isl_basic_map_alloc_inequality(bmap);
12111 if (l < 0)
12112 goto error;
12113 for (j = 0, pos = 0; j < 5; ++j) {
12114 int off = isl_basic_map_offset(bmap, c[j]);
12115 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12116 if (dim < 0)
12117 goto error;
12118 for (k = 0; k < dim; ++k) {
12119 isl_int_set(bmap->ineq[l][off + k],
12120 ineq->row[i][pos]);
12121 ++pos;
12126 isl_space_free(space);
12127 isl_mat_free(eq);
12128 isl_mat_free(ineq);
12130 bmap = isl_basic_map_simplify(bmap);
12131 return isl_basic_map_finalize(bmap);
12132 error:
12133 isl_space_free(space);
12134 isl_mat_free(eq);
12135 isl_mat_free(ineq);
12136 isl_basic_map_free(bmap);
12137 return NULL;
12140 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12141 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12142 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12144 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12145 c1, c2, c3, c4, isl_dim_in);
12148 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12149 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12150 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12152 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12153 c1, c2, c3, c4, isl_dim_in);
12156 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12157 __isl_take isl_space *dim,
12158 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12159 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12161 isl_basic_map *bmap;
12162 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12163 c1, c2, c3, c4, isl_dim_in);
12164 return bset_from_bmap(bmap);
12167 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12169 if (!bmap)
12170 return isl_bool_error;
12172 return isl_space_can_zip(bmap->dim);
12175 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12177 if (!map)
12178 return isl_bool_error;
12180 return isl_space_can_zip(map->dim);
12183 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12184 * (A -> C) -> (B -> D).
12186 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12188 unsigned pos;
12189 isl_size n_in;
12190 isl_size n1;
12191 isl_size n2;
12193 if (!bmap)
12194 return NULL;
12196 if (!isl_basic_map_can_zip(bmap))
12197 isl_die(bmap->ctx, isl_error_invalid,
12198 "basic map cannot be zipped", goto error);
12199 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12200 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12201 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12202 if (n_in < 0 || n1 < 0 || n2 < 0)
12203 return isl_basic_map_free(bmap);
12204 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
12205 bmap = isl_basic_map_cow(bmap);
12206 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12207 if (!bmap)
12208 return NULL;
12209 bmap->dim = isl_space_zip(bmap->dim);
12210 if (!bmap->dim)
12211 goto error;
12212 bmap = isl_basic_map_mark_final(bmap);
12213 return bmap;
12214 error:
12215 isl_basic_map_free(bmap);
12216 return NULL;
12219 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12220 * (A -> C) -> (B -> D).
12222 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12224 int i;
12226 if (!map)
12227 return NULL;
12229 if (!isl_map_can_zip(map))
12230 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12231 goto error);
12233 map = isl_map_cow(map);
12234 if (!map)
12235 return NULL;
12237 for (i = 0; i < map->n; ++i) {
12238 map->p[i] = isl_basic_map_zip(map->p[i]);
12239 if (!map->p[i])
12240 goto error;
12243 map->dim = isl_space_zip(map->dim);
12244 if (!map->dim)
12245 goto error;
12247 return map;
12248 error:
12249 isl_map_free(map);
12250 return NULL;
12253 /* Can we apply isl_basic_map_curry to "bmap"?
12254 * That is, does it have a nested relation in its domain?
12256 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12258 if (!bmap)
12259 return isl_bool_error;
12261 return isl_space_can_curry(bmap->dim);
12264 /* Can we apply isl_map_curry to "map"?
12265 * That is, does it have a nested relation in its domain?
12267 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12269 if (!map)
12270 return isl_bool_error;
12272 return isl_space_can_curry(map->dim);
12275 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12276 * A -> (B -> C).
12278 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12281 if (!bmap)
12282 return NULL;
12284 if (!isl_basic_map_can_curry(bmap))
12285 isl_die(bmap->ctx, isl_error_invalid,
12286 "basic map cannot be curried", goto error);
12287 bmap = isl_basic_map_cow(bmap);
12288 if (!bmap)
12289 return NULL;
12290 bmap->dim = isl_space_curry(bmap->dim);
12291 if (!bmap->dim)
12292 goto error;
12293 bmap = isl_basic_map_mark_final(bmap);
12294 return bmap;
12295 error:
12296 isl_basic_map_free(bmap);
12297 return NULL;
12300 /* Given a map (A -> B) -> C, return the corresponding map
12301 * A -> (B -> C).
12303 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12305 return isl_map_change_space(map, &isl_map_can_curry,
12306 "map cannot be curried", &isl_space_curry);
12309 /* Can isl_map_range_curry be applied to "map"?
12310 * That is, does it have a nested relation in its range,
12311 * the domain of which is itself a nested relation?
12313 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12315 if (!map)
12316 return isl_bool_error;
12318 return isl_space_can_range_curry(map->dim);
12321 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12322 * A -> (B -> (C -> D)).
12324 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12326 return isl_map_change_space(map, &isl_map_can_range_curry,
12327 "map range cannot be curried",
12328 &isl_space_range_curry);
12331 /* Can we apply isl_basic_map_uncurry to "bmap"?
12332 * That is, does it have a nested relation in its domain?
12334 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12336 if (!bmap)
12337 return isl_bool_error;
12339 return isl_space_can_uncurry(bmap->dim);
12342 /* Can we apply isl_map_uncurry to "map"?
12343 * That is, does it have a nested relation in its domain?
12345 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12347 if (!map)
12348 return isl_bool_error;
12350 return isl_space_can_uncurry(map->dim);
12353 /* Given a basic map A -> (B -> C), return the corresponding basic map
12354 * (A -> B) -> C.
12356 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12359 if (!bmap)
12360 return NULL;
12362 if (!isl_basic_map_can_uncurry(bmap))
12363 isl_die(bmap->ctx, isl_error_invalid,
12364 "basic map cannot be uncurried",
12365 return isl_basic_map_free(bmap));
12366 bmap = isl_basic_map_cow(bmap);
12367 if (!bmap)
12368 return NULL;
12369 bmap->dim = isl_space_uncurry(bmap->dim);
12370 if (!bmap->dim)
12371 return isl_basic_map_free(bmap);
12372 bmap = isl_basic_map_mark_final(bmap);
12373 return bmap;
12376 /* Given a map A -> (B -> C), return the corresponding map
12377 * (A -> B) -> C.
12379 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12381 return isl_map_change_space(map, &isl_map_can_uncurry,
12382 "map cannot be uncurried", &isl_space_uncurry);
12385 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12386 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12388 return isl_map_equate(set, type1, pos1, type2, pos2);
12391 /* Construct a basic map where the given dimensions are equal to each other.
12393 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12394 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12396 isl_basic_map *bmap = NULL;
12397 int i;
12398 isl_size total;
12400 total = isl_space_dim(space, isl_dim_all);
12401 if (total < 0 ||
12402 isl_space_check_range(space, type1, pos1, 1) < 0 ||
12403 isl_space_check_range(space, type2, pos2, 1) < 0)
12404 goto error;
12406 if (type1 == type2 && pos1 == pos2)
12407 return isl_basic_map_universe(space);
12409 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12410 i = isl_basic_map_alloc_equality(bmap);
12411 if (i < 0)
12412 goto error;
12413 isl_seq_clr(bmap->eq[i], 1 + total);
12414 pos1 += isl_basic_map_offset(bmap, type1);
12415 pos2 += isl_basic_map_offset(bmap, type2);
12416 isl_int_set_si(bmap->eq[i][pos1], -1);
12417 isl_int_set_si(bmap->eq[i][pos2], 1);
12418 bmap = isl_basic_map_finalize(bmap);
12419 isl_space_free(space);
12420 return bmap;
12421 error:
12422 isl_space_free(space);
12423 isl_basic_map_free(bmap);
12424 return NULL;
12427 /* Add a constraint imposing that the given two dimensions are equal.
12429 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12430 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12432 isl_basic_map *eq;
12434 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12436 bmap = isl_basic_map_intersect(bmap, eq);
12438 return bmap;
12441 /* Add a constraint imposing that the given two dimensions are equal.
12443 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12444 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12446 isl_basic_map *bmap;
12448 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12450 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12452 return map;
12455 /* Add a constraint imposing that the given two dimensions have opposite values.
12457 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12458 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12460 isl_basic_map *bmap = NULL;
12461 int i;
12462 isl_size total;
12464 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12465 return isl_map_free(map);
12466 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12467 return isl_map_free(map);
12469 total = isl_map_dim(map, isl_dim_all);
12470 if (total < 0)
12471 return isl_map_free(map);
12472 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12473 i = isl_basic_map_alloc_equality(bmap);
12474 if (i < 0)
12475 goto error;
12476 isl_seq_clr(bmap->eq[i], 1 + total);
12477 pos1 += isl_basic_map_offset(bmap, type1);
12478 pos2 += isl_basic_map_offset(bmap, type2);
12479 isl_int_set_si(bmap->eq[i][pos1], 1);
12480 isl_int_set_si(bmap->eq[i][pos2], 1);
12481 bmap = isl_basic_map_finalize(bmap);
12483 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12485 return map;
12486 error:
12487 isl_basic_map_free(bmap);
12488 isl_map_free(map);
12489 return NULL;
12492 /* Construct a constraint imposing that the value of the first dimension is
12493 * greater than or equal to that of the second.
12495 static __isl_give isl_constraint *constraint_order_ge(
12496 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12497 enum isl_dim_type type2, int pos2)
12499 isl_constraint *c;
12501 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12502 isl_space_check_range(space, type2, pos2, 1) < 0)
12503 space = isl_space_free(space);
12504 if (!space)
12505 return NULL;
12507 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12509 if (type1 == type2 && pos1 == pos2)
12510 return c;
12512 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12513 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12515 return c;
12518 /* Add a constraint imposing that the value of the first dimension is
12519 * greater than or equal to that of the second.
12521 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12522 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12524 isl_constraint *c;
12525 isl_space *space;
12527 if (type1 == type2 && pos1 == pos2)
12528 return bmap;
12529 space = isl_basic_map_get_space(bmap);
12530 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12531 bmap = isl_basic_map_add_constraint(bmap, c);
12533 return bmap;
12536 /* Add a constraint imposing that the value of the first dimension is
12537 * greater than or equal to that of the second.
12539 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12540 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12542 isl_constraint *c;
12543 isl_space *space;
12545 if (type1 == type2 && pos1 == pos2)
12546 return map;
12547 space = isl_map_get_space(map);
12548 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12549 map = isl_map_add_constraint(map, c);
12551 return map;
12554 /* Add a constraint imposing that the value of the first dimension is
12555 * less than or equal to that of the second.
12557 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12558 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12560 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12563 /* Construct a basic map where the value of the first dimension is
12564 * greater than that of the second.
12566 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12567 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12569 isl_basic_map *bmap = NULL;
12570 int i;
12571 isl_size total;
12573 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12574 isl_space_check_range(space, type2, pos2, 1) < 0)
12575 goto error;
12577 if (type1 == type2 && pos1 == pos2)
12578 return isl_basic_map_empty(space);
12580 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12581 total = isl_basic_map_dim(bmap, isl_dim_all);
12582 i = isl_basic_map_alloc_inequality(bmap);
12583 if (total < 0 || i < 0)
12584 return isl_basic_map_free(bmap);
12585 isl_seq_clr(bmap->ineq[i], 1 + total);
12586 pos1 += isl_basic_map_offset(bmap, type1);
12587 pos2 += isl_basic_map_offset(bmap, type2);
12588 isl_int_set_si(bmap->ineq[i][pos1], 1);
12589 isl_int_set_si(bmap->ineq[i][pos2], -1);
12590 isl_int_set_si(bmap->ineq[i][0], -1);
12591 bmap = isl_basic_map_finalize(bmap);
12593 return bmap;
12594 error:
12595 isl_space_free(space);
12596 isl_basic_map_free(bmap);
12597 return NULL;
12600 /* Add a constraint imposing that the value of the first dimension is
12601 * greater than that of the second.
12603 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12604 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12606 isl_basic_map *gt;
12608 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12610 bmap = isl_basic_map_intersect(bmap, gt);
12612 return bmap;
12615 /* Add a constraint imposing that the value of the first dimension is
12616 * greater than that of the second.
12618 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12619 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12621 isl_basic_map *bmap;
12623 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12625 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12627 return map;
12630 /* Add a constraint imposing that the value of the first dimension is
12631 * smaller than that of the second.
12633 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12634 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12636 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12639 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12640 int pos)
12642 isl_aff *div;
12643 isl_local_space *ls;
12645 if (!bmap)
12646 return NULL;
12648 if (!isl_basic_map_divs_known(bmap))
12649 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12650 "some divs are unknown", return NULL);
12652 ls = isl_basic_map_get_local_space(bmap);
12653 div = isl_local_space_get_div(ls, pos);
12654 isl_local_space_free(ls);
12656 return div;
12659 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12660 int pos)
12662 return isl_basic_map_get_div(bset, pos);
12665 /* Plug in "subs" for dimension "type", "pos" of "bset".
12667 * Let i be the dimension to replace and let "subs" be of the form
12669 * f/d
12671 * Any integer division with a non-zero coefficient for i,
12673 * floor((a i + g)/m)
12675 * is replaced by
12677 * floor((a f + d g)/(m d))
12679 * Constraints of the form
12681 * a i + g
12683 * are replaced by
12685 * a f + d g
12687 * We currently require that "subs" is an integral expression.
12688 * Handling rational expressions may require us to add stride constraints
12689 * as we do in isl_basic_set_preimage_multi_aff.
12691 __isl_give isl_basic_set *isl_basic_set_substitute(
12692 __isl_take isl_basic_set *bset,
12693 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12695 int i;
12696 isl_int v;
12697 isl_ctx *ctx;
12698 isl_size n_div;
12700 if (bset && isl_basic_set_plain_is_empty(bset))
12701 return bset;
12703 bset = isl_basic_set_cow(bset);
12704 if (!bset || !subs)
12705 goto error;
12707 ctx = isl_basic_set_get_ctx(bset);
12708 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12709 isl_die(ctx, isl_error_invalid,
12710 "spaces don't match", goto error);
12711 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
12712 if (n_div < 0)
12713 goto error;
12714 if (n_div != 0)
12715 isl_die(ctx, isl_error_unsupported,
12716 "cannot handle divs yet", goto error);
12717 if (!isl_int_is_one(subs->v->el[0]))
12718 isl_die(ctx, isl_error_invalid,
12719 "can only substitute integer expressions", goto error);
12721 pos += isl_basic_set_offset(bset, type);
12723 isl_int_init(v);
12725 for (i = 0; i < bset->n_eq; ++i) {
12726 if (isl_int_is_zero(bset->eq[i][pos]))
12727 continue;
12728 isl_int_set(v, bset->eq[i][pos]);
12729 isl_int_set_si(bset->eq[i][pos], 0);
12730 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12731 v, subs->v->el + 1, subs->v->size - 1);
12734 for (i = 0; i < bset->n_ineq; ++i) {
12735 if (isl_int_is_zero(bset->ineq[i][pos]))
12736 continue;
12737 isl_int_set(v, bset->ineq[i][pos]);
12738 isl_int_set_si(bset->ineq[i][pos], 0);
12739 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12740 v, subs->v->el + 1, subs->v->size - 1);
12743 for (i = 0; i < bset->n_div; ++i) {
12744 if (isl_int_is_zero(bset->div[i][1 + pos]))
12745 continue;
12746 isl_int_set(v, bset->div[i][1 + pos]);
12747 isl_int_set_si(bset->div[i][1 + pos], 0);
12748 isl_seq_combine(bset->div[i] + 1,
12749 subs->v->el[0], bset->div[i] + 1,
12750 v, subs->v->el + 1, subs->v->size - 1);
12751 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12754 isl_int_clear(v);
12756 bset = isl_basic_set_simplify(bset);
12757 return isl_basic_set_finalize(bset);
12758 error:
12759 isl_basic_set_free(bset);
12760 return NULL;
12763 /* Plug in "subs" for dimension "type", "pos" of "set".
12765 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12766 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12768 int i;
12770 if (set && isl_set_plain_is_empty(set))
12771 return set;
12773 set = isl_set_cow(set);
12774 if (!set || !subs)
12775 goto error;
12777 for (i = set->n - 1; i >= 0; --i) {
12778 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12779 set = set_from_map(remove_if_empty(set_to_map(set), i));
12780 if (!set)
12781 return NULL;
12784 return set;
12785 error:
12786 isl_set_free(set);
12787 return NULL;
12790 /* Check if the range of "ma" is compatible with the domain or range
12791 * (depending on "type") of "bmap".
12793 static isl_stat check_basic_map_compatible_range_multi_aff(
12794 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12795 __isl_keep isl_multi_aff *ma)
12797 isl_bool m;
12798 isl_space *ma_space;
12800 ma_space = isl_multi_aff_get_space(ma);
12802 m = isl_space_has_equal_params(bmap->dim, ma_space);
12803 if (m < 0)
12804 goto error;
12805 if (!m)
12806 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12807 "parameters don't match", goto error);
12808 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12809 if (m < 0)
12810 goto error;
12811 if (!m)
12812 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12813 "spaces don't match", goto error);
12815 isl_space_free(ma_space);
12816 return isl_stat_ok;
12817 error:
12818 isl_space_free(ma_space);
12819 return isl_stat_error;
12822 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12823 * coefficients before the transformed range of dimensions,
12824 * the "n_after" coefficients after the transformed range of dimensions
12825 * and the coefficients of the other divs in "bmap".
12827 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12828 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12830 int i;
12831 isl_size n_param;
12832 isl_size n_set;
12833 isl_local_space *ls;
12835 if (n_div == 0)
12836 return bmap;
12838 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12839 n_param = isl_local_space_dim(ls, isl_dim_param);
12840 n_set = isl_local_space_dim(ls, isl_dim_set);
12841 if (n_param < 0 || n_set < 0)
12842 return isl_basic_map_free(bmap);
12844 for (i = 0; i < n_div; ++i) {
12845 int o_bmap = 0, o_ls = 0;
12847 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12848 o_bmap += 1 + 1 + n_param;
12849 o_ls += 1 + 1 + n_param;
12850 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12851 o_bmap += n_before;
12852 isl_seq_cpy(bmap->div[i] + o_bmap,
12853 ls->div->row[i] + o_ls, n_set);
12854 o_bmap += n_set;
12855 o_ls += n_set;
12856 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12857 o_bmap += n_after;
12858 isl_seq_cpy(bmap->div[i] + o_bmap,
12859 ls->div->row[i] + o_ls, n_div);
12860 o_bmap += n_div;
12861 o_ls += n_div;
12862 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12863 bmap = isl_basic_map_add_div_constraints(bmap, i);
12864 if (!bmap)
12865 goto error;
12868 isl_local_space_free(ls);
12869 return bmap;
12870 error:
12871 isl_local_space_free(ls);
12872 return isl_basic_map_free(bmap);
12875 /* How many stride constraints does "ma" enforce?
12876 * That is, how many of the affine expressions have a denominator
12877 * different from one?
12879 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12881 int i;
12882 int strides = 0;
12884 for (i = 0; i < ma->n; ++i)
12885 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12886 strides++;
12888 return strides;
12891 /* For each affine expression in ma of the form
12893 * x_i = (f_i y + h_i)/m_i
12895 * with m_i different from one, add a constraint to "bmap"
12896 * of the form
12898 * f_i y + h_i = m_i alpha_i
12900 * with alpha_i an additional existentially quantified variable.
12902 * The input variables of "ma" correspond to a subset of the variables
12903 * of "bmap". There are "n_before" variables in "bmap" before this
12904 * subset and "n_after" variables after this subset.
12905 * The integer divisions of the affine expressions in "ma" are assumed
12906 * to have been aligned. There are "n_div_ma" of them and
12907 * they appear first in "bmap", straight after the "n_after" variables.
12909 static __isl_give isl_basic_map *add_ma_strides(
12910 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12911 int n_before, int n_after, int n_div_ma)
12913 int i, k;
12914 int div;
12915 isl_size total;
12916 isl_size n_param;
12917 isl_size n_in;
12919 total = isl_basic_map_dim(bmap, isl_dim_all);
12920 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12921 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12922 if (total < 0 || n_param < 0 || n_in < 0)
12923 return isl_basic_map_free(bmap);
12924 for (i = 0; i < ma->n; ++i) {
12925 int o_bmap = 0, o_ma = 1;
12927 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12928 continue;
12929 div = isl_basic_map_alloc_div(bmap);
12930 k = isl_basic_map_alloc_equality(bmap);
12931 if (div < 0 || k < 0)
12932 goto error;
12933 isl_int_set_si(bmap->div[div][0], 0);
12934 isl_seq_cpy(bmap->eq[k] + o_bmap,
12935 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12936 o_bmap += 1 + n_param;
12937 o_ma += 1 + n_param;
12938 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12939 o_bmap += n_before;
12940 isl_seq_cpy(bmap->eq[k] + o_bmap,
12941 ma->u.p[i]->v->el + o_ma, n_in);
12942 o_bmap += n_in;
12943 o_ma += n_in;
12944 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12945 o_bmap += n_after;
12946 isl_seq_cpy(bmap->eq[k] + o_bmap,
12947 ma->u.p[i]->v->el + o_ma, n_div_ma);
12948 o_bmap += n_div_ma;
12949 o_ma += n_div_ma;
12950 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12951 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12952 total++;
12955 return bmap;
12956 error:
12957 isl_basic_map_free(bmap);
12958 return NULL;
12961 /* Replace the domain or range space (depending on "type) of "space" by "set".
12963 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12964 enum isl_dim_type type, __isl_take isl_space *set)
12966 if (type == isl_dim_in) {
12967 space = isl_space_range(space);
12968 space = isl_space_map_from_domain_and_range(set, space);
12969 } else {
12970 space = isl_space_domain(space);
12971 space = isl_space_map_from_domain_and_range(space, set);
12974 return space;
12977 /* Compute the preimage of the domain or range (depending on "type")
12978 * of "bmap" under the function represented by "ma".
12979 * In other words, plug in "ma" in the domain or range of "bmap".
12980 * The result is a basic map that lives in the same space as "bmap"
12981 * except that the domain or range has been replaced by
12982 * the domain space of "ma".
12984 * If bmap is represented by
12986 * A(p) + S u + B x + T v + C(divs) >= 0,
12988 * where u and x are input and output dimensions if type == isl_dim_out
12989 * while x and v are input and output dimensions if type == isl_dim_in,
12990 * and ma is represented by
12992 * x = D(p) + F(y) + G(divs')
12994 * then the result is
12996 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12998 * The divs in the input set are similarly adjusted.
12999 * In particular
13001 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13003 * becomes
13005 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13006 * B_i G(divs') + c_i(divs))/n_i)
13008 * If bmap is not a rational map and if F(y) involves any denominators
13010 * x_i = (f_i y + h_i)/m_i
13012 * then additional constraints are added to ensure that we only
13013 * map back integer points. That is we enforce
13015 * f_i y + h_i = m_i alpha_i
13017 * with alpha_i an additional existentially quantified variable.
13019 * We first copy over the divs from "ma".
13020 * Then we add the modified constraints and divs from "bmap".
13021 * Finally, we add the stride constraints, if needed.
13023 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13024 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13025 __isl_take isl_multi_aff *ma)
13027 int i, k;
13028 isl_space *space;
13029 isl_basic_map *res = NULL;
13030 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13031 isl_int f, c1, c2, g;
13032 isl_bool rational;
13033 int strides;
13035 isl_int_init(f);
13036 isl_int_init(c1);
13037 isl_int_init(c2);
13038 isl_int_init(g);
13040 ma = isl_multi_aff_align_divs(ma);
13041 if (!bmap || !ma)
13042 goto error;
13043 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13044 goto error;
13046 if (type == isl_dim_in) {
13047 n_before = 0;
13048 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13049 } else {
13050 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13051 n_after = 0;
13053 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13054 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13055 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13056 goto error;
13058 space = isl_multi_aff_get_domain_space(ma);
13059 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13060 rational = isl_basic_map_is_rational(bmap);
13061 strides = rational ? 0 : multi_aff_strides(ma);
13062 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13063 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13064 if (rational)
13065 res = isl_basic_map_set_rational(res);
13067 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13068 if (isl_basic_map_alloc_div(res) < 0)
13069 goto error;
13071 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13072 if (!res)
13073 goto error;
13075 for (i = 0; i < bmap->n_eq; ++i) {
13076 k = isl_basic_map_alloc_equality(res);
13077 if (k < 0)
13078 goto error;
13079 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13080 n_after, n_div_ma, n_div_bmap,
13081 f, c1, c2, g, 0) < 0)
13082 goto error;
13085 for (i = 0; i < bmap->n_ineq; ++i) {
13086 k = isl_basic_map_alloc_inequality(res);
13087 if (k < 0)
13088 goto error;
13089 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13090 n_after, n_div_ma, n_div_bmap,
13091 f, c1, c2, g, 0) < 0)
13092 goto error;
13095 for (i = 0; i < bmap->n_div; ++i) {
13096 if (isl_int_is_zero(bmap->div[i][0])) {
13097 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13098 continue;
13100 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13101 n_before, n_after, n_div_ma, n_div_bmap,
13102 f, c1, c2, g, 1) < 0)
13103 goto error;
13106 if (strides)
13107 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13109 isl_int_clear(f);
13110 isl_int_clear(c1);
13111 isl_int_clear(c2);
13112 isl_int_clear(g);
13113 isl_basic_map_free(bmap);
13114 isl_multi_aff_free(ma);
13115 res = isl_basic_map_simplify(res);
13116 return isl_basic_map_finalize(res);
13117 error:
13118 isl_int_clear(f);
13119 isl_int_clear(c1);
13120 isl_int_clear(c2);
13121 isl_int_clear(g);
13122 isl_basic_map_free(bmap);
13123 isl_multi_aff_free(ma);
13124 isl_basic_map_free(res);
13125 return NULL;
13128 /* Compute the preimage of "bset" under the function represented by "ma".
13129 * In other words, plug in "ma" in "bset". The result is a basic set
13130 * that lives in the domain space of "ma".
13132 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13133 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13135 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13138 /* Compute the preimage of the domain of "bmap" under the function
13139 * represented by "ma".
13140 * In other words, plug in "ma" in the domain of "bmap".
13141 * The result is a basic map that lives in the same space as "bmap"
13142 * except that the domain has been replaced by the domain space of "ma".
13144 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13145 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13147 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13150 /* Compute the preimage of the range of "bmap" under the function
13151 * represented by "ma".
13152 * In other words, plug in "ma" in the range of "bmap".
13153 * The result is a basic map that lives in the same space as "bmap"
13154 * except that the range has been replaced by the domain space of "ma".
13156 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13157 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13159 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13162 /* Check if the range of "ma" is compatible with the domain or range
13163 * (depending on "type") of "map".
13164 * Return isl_stat_error if anything is wrong.
13166 static isl_stat check_map_compatible_range_multi_aff(
13167 __isl_keep isl_map *map, enum isl_dim_type type,
13168 __isl_keep isl_multi_aff *ma)
13170 isl_bool m;
13171 isl_space *ma_space;
13173 ma_space = isl_multi_aff_get_space(ma);
13174 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13175 isl_space_free(ma_space);
13176 if (m < 0)
13177 return isl_stat_error;
13178 if (!m)
13179 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13180 "spaces don't match", return isl_stat_error);
13181 return isl_stat_ok;
13184 /* Compute the preimage of the domain or range (depending on "type")
13185 * of "map" under the function represented by "ma".
13186 * In other words, plug in "ma" in the domain or range of "map".
13187 * The result is a map that lives in the same space as "map"
13188 * except that the domain or range has been replaced by
13189 * the domain space of "ma".
13191 * The parameters are assumed to have been aligned.
13193 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13194 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13196 int i;
13197 isl_space *space;
13199 map = isl_map_cow(map);
13200 ma = isl_multi_aff_align_divs(ma);
13201 if (!map || !ma)
13202 goto error;
13203 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13204 goto error;
13206 for (i = 0; i < map->n; ++i) {
13207 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13208 isl_multi_aff_copy(ma));
13209 if (!map->p[i])
13210 goto error;
13213 space = isl_multi_aff_get_domain_space(ma);
13214 space = isl_space_set(isl_map_get_space(map), type, space);
13216 isl_space_free(map->dim);
13217 map->dim = space;
13218 if (!map->dim)
13219 goto error;
13221 isl_multi_aff_free(ma);
13222 if (map->n > 1)
13223 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13224 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13225 return map;
13226 error:
13227 isl_multi_aff_free(ma);
13228 isl_map_free(map);
13229 return NULL;
13232 /* Compute the preimage of the domain or range (depending on "type")
13233 * of "map" under the function represented by "ma".
13234 * In other words, plug in "ma" in the domain or range of "map".
13235 * The result is a map that lives in the same space as "map"
13236 * except that the domain or range has been replaced by
13237 * the domain space of "ma".
13239 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13240 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13242 isl_bool aligned;
13244 if (!map || !ma)
13245 goto error;
13247 aligned = isl_map_space_has_equal_params(map, ma->space);
13248 if (aligned < 0)
13249 goto error;
13250 if (aligned)
13251 return map_preimage_multi_aff(map, type, ma);
13253 if (isl_map_check_named_params(map) < 0)
13254 goto error;
13255 if (!isl_space_has_named_params(ma->space))
13256 isl_die(map->ctx, isl_error_invalid,
13257 "unaligned unnamed parameters", goto error);
13258 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13259 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13261 return map_preimage_multi_aff(map, type, ma);
13262 error:
13263 isl_multi_aff_free(ma);
13264 return isl_map_free(map);
13267 /* Compute the preimage of "set" under the function represented by "ma".
13268 * In other words, plug in "ma" in "set". The result is a set
13269 * that lives in the domain space of "ma".
13271 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13272 __isl_take isl_multi_aff *ma)
13274 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13277 /* Compute the preimage of the domain of "map" under the function
13278 * represented by "ma".
13279 * In other words, plug in "ma" in the domain of "map".
13280 * The result is a map that lives in the same space as "map"
13281 * except that the domain has been replaced by the domain space of "ma".
13283 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13284 __isl_take isl_multi_aff *ma)
13286 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13289 /* Compute the preimage of the range of "map" under the function
13290 * represented by "ma".
13291 * In other words, plug in "ma" in the range of "map".
13292 * The result is a map that lives in the same space as "map"
13293 * except that the range has been replaced by the domain space of "ma".
13295 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13296 __isl_take isl_multi_aff *ma)
13298 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13301 /* Compute the preimage of "map" under the function represented by "pma".
13302 * In other words, plug in "pma" in the domain or range of "map".
13303 * The result is a map that lives in the same space as "map",
13304 * except that the space of type "type" has been replaced by
13305 * the domain space of "pma".
13307 * The parameters of "map" and "pma" are assumed to have been aligned.
13309 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13310 __isl_take isl_map *map, enum isl_dim_type type,
13311 __isl_take isl_pw_multi_aff *pma)
13313 int i;
13314 isl_map *res;
13316 if (!pma)
13317 goto error;
13319 if (pma->n == 0) {
13320 isl_pw_multi_aff_free(pma);
13321 res = isl_map_empty(isl_map_get_space(map));
13322 isl_map_free(map);
13323 return res;
13326 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13327 isl_multi_aff_copy(pma->p[0].maff));
13328 if (type == isl_dim_in)
13329 res = isl_map_intersect_domain(res,
13330 isl_map_copy(pma->p[0].set));
13331 else
13332 res = isl_map_intersect_range(res,
13333 isl_map_copy(pma->p[0].set));
13335 for (i = 1; i < pma->n; ++i) {
13336 isl_map *res_i;
13338 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13339 isl_multi_aff_copy(pma->p[i].maff));
13340 if (type == isl_dim_in)
13341 res_i = isl_map_intersect_domain(res_i,
13342 isl_map_copy(pma->p[i].set));
13343 else
13344 res_i = isl_map_intersect_range(res_i,
13345 isl_map_copy(pma->p[i].set));
13346 res = isl_map_union(res, res_i);
13349 isl_pw_multi_aff_free(pma);
13350 isl_map_free(map);
13351 return res;
13352 error:
13353 isl_pw_multi_aff_free(pma);
13354 isl_map_free(map);
13355 return NULL;
13358 /* Compute the preimage of "map" under the function represented by "pma".
13359 * In other words, plug in "pma" in the domain or range of "map".
13360 * The result is a map that lives in the same space as "map",
13361 * except that the space of type "type" has been replaced by
13362 * the domain space of "pma".
13364 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13365 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13367 isl_bool aligned;
13369 if (!map || !pma)
13370 goto error;
13372 aligned = isl_map_space_has_equal_params(map, pma->dim);
13373 if (aligned < 0)
13374 goto error;
13375 if (aligned)
13376 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13378 if (isl_map_check_named_params(map) < 0)
13379 goto error;
13380 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13381 goto error;
13382 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13383 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13385 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13386 error:
13387 isl_pw_multi_aff_free(pma);
13388 return isl_map_free(map);
13391 /* Compute the preimage of "set" under the function represented by "pma".
13392 * In other words, plug in "pma" in "set". The result is a set
13393 * that lives in the domain space of "pma".
13395 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13396 __isl_take isl_pw_multi_aff *pma)
13398 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13401 /* Compute the preimage of the domain of "map" under the function
13402 * represented by "pma".
13403 * In other words, plug in "pma" in the domain of "map".
13404 * The result is a map that lives in the same space as "map",
13405 * except that domain space has been replaced by the domain space of "pma".
13407 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13408 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13410 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13413 /* Compute the preimage of the range of "map" under the function
13414 * represented by "pma".
13415 * In other words, plug in "pma" in the range of "map".
13416 * The result is a map that lives in the same space as "map",
13417 * except that range space has been replaced by the domain space of "pma".
13419 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13420 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13422 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13425 /* Compute the preimage of "map" under the function represented by "mpa".
13426 * In other words, plug in "mpa" in the domain or range of "map".
13427 * The result is a map that lives in the same space as "map",
13428 * except that the space of type "type" has been replaced by
13429 * the domain space of "mpa".
13431 * If the map does not involve any constraints that refer to the
13432 * dimensions of the substituted space, then the only possible
13433 * effect of "mpa" on the map is to map the space to a different space.
13434 * We create a separate isl_multi_aff to effectuate this change
13435 * in order to avoid spurious splitting of the map along the pieces
13436 * of "mpa".
13437 * If "mpa" has a non-trivial explicit domain, however,
13438 * then the full substitution should be performed.
13440 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13441 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13443 isl_size n;
13444 isl_bool full;
13445 isl_pw_multi_aff *pma;
13447 n = isl_map_dim(map, type);
13448 if (n < 0 || !mpa)
13449 goto error;
13451 full = isl_map_involves_dims(map, type, 0, n);
13452 if (full >= 0 && !full)
13453 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13454 if (full < 0)
13455 goto error;
13456 if (!full) {
13457 isl_space *space;
13458 isl_multi_aff *ma;
13460 space = isl_multi_pw_aff_get_space(mpa);
13461 isl_multi_pw_aff_free(mpa);
13462 ma = isl_multi_aff_zero(space);
13463 return isl_map_preimage_multi_aff(map, type, ma);
13466 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13467 return isl_map_preimage_pw_multi_aff(map, type, pma);
13468 error:
13469 isl_map_free(map);
13470 isl_multi_pw_aff_free(mpa);
13471 return NULL;
13474 /* Compute the preimage of "map" under the function represented by "mpa".
13475 * In other words, plug in "mpa" in the domain "map".
13476 * The result is a map that lives in the same space as "map",
13477 * except that domain space has been replaced by the domain space of "mpa".
13479 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13480 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13482 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13485 /* Compute the preimage of "set" by the function represented by "mpa".
13486 * In other words, plug in "mpa" in "set".
13488 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13489 __isl_take isl_multi_pw_aff *mpa)
13491 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13494 /* Return a copy of the equality constraints of "bset" as a matrix.
13496 __isl_give isl_mat *isl_basic_set_extract_equalities(
13497 __isl_keep isl_basic_set *bset)
13499 isl_ctx *ctx;
13500 isl_size total;
13502 total = isl_basic_set_dim(bset, isl_dim_all);
13503 if (total < 0)
13504 return NULL;
13506 ctx = isl_basic_set_get_ctx(bset);
13507 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
13510 /* Are the "n" "coefficients" starting at "first" of the integer division
13511 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13512 * to each other?
13513 * The "coefficient" at position 0 is the denominator.
13514 * The "coefficient" at position 1 is the constant term.
13516 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13517 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13518 unsigned first, unsigned n)
13520 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13521 return isl_bool_error;
13522 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13523 return isl_bool_error;
13524 return isl_seq_eq(bmap1->div[pos1] + first,
13525 bmap2->div[pos2] + first, n);
13528 /* Are the integer division expressions at position "pos1" in "bmap1" and
13529 * "pos2" in "bmap2" equal to each other, except that the constant terms
13530 * are different?
13532 isl_bool isl_basic_map_equal_div_expr_except_constant(
13533 __isl_keep isl_basic_map *bmap1, int pos1,
13534 __isl_keep isl_basic_map *bmap2, int pos2)
13536 isl_bool equal;
13537 isl_size total, total2;
13539 total = isl_basic_map_dim(bmap1, isl_dim_all);
13540 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
13541 if (total < 0 || total2 < 0)
13542 return isl_bool_error;
13543 if (total != total2)
13544 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13545 "incomparable div expressions", return isl_bool_error);
13546 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13547 0, 1);
13548 if (equal < 0 || !equal)
13549 return equal;
13550 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13551 1, 1);
13552 if (equal < 0 || equal)
13553 return isl_bool_not(equal);
13554 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13555 2, total);
13558 /* Replace the numerator of the constant term of the integer division
13559 * expression at position "div" in "bmap" by "value".
13560 * The caller guarantees that this does not change the meaning
13561 * of the input.
13563 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13564 __isl_take isl_basic_map *bmap, int div, int value)
13566 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13567 return isl_basic_map_free(bmap);
13569 isl_int_set_si(bmap->div[div][1], value);
13571 return bmap;
13574 /* Is the point "inner" internal to inequality constraint "ineq"
13575 * of "bset"?
13576 * The point is considered to be internal to the inequality constraint,
13577 * if it strictly lies on the positive side of the inequality constraint,
13578 * or if it lies on the constraint and the constraint is lexico-positive.
13580 static isl_bool is_internal(__isl_keep isl_vec *inner,
13581 __isl_keep isl_basic_set *bset, int ineq)
13583 isl_ctx *ctx;
13584 int pos;
13585 isl_size total;
13587 if (!inner || !bset)
13588 return isl_bool_error;
13590 ctx = isl_basic_set_get_ctx(bset);
13591 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13592 &ctx->normalize_gcd);
13593 if (!isl_int_is_zero(ctx->normalize_gcd))
13594 return isl_int_is_nonneg(ctx->normalize_gcd);
13596 total = isl_basic_set_dim(bset, isl_dim_all);
13597 if (total < 0)
13598 return isl_bool_error;
13599 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13600 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13603 /* Tighten the inequality constraints of "bset" that are outward with respect
13604 * to the point "vec".
13605 * That is, tighten the constraints that are not satisfied by "vec".
13607 * "vec" is a point internal to some superset S of "bset" that is used
13608 * to make the subsets of S disjoint, by tightening one half of the constraints
13609 * that separate two subsets. In particular, the constraints of S
13610 * are all satisfied by "vec" and should not be tightened.
13611 * Of the internal constraints, those that have "vec" on the outside
13612 * are tightened. The shared facet is included in the adjacent subset
13613 * with the opposite constraint.
13614 * For constraints that saturate "vec", this criterion cannot be used
13615 * to determine which of the two sides should be tightened.
13616 * Instead, the sign of the first non-zero coefficient is used
13617 * to make this choice. Note that this second criterion is never used
13618 * on the constraints of S since "vec" is interior to "S".
13620 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13621 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13623 int j;
13625 bset = isl_basic_set_cow(bset);
13626 if (!bset)
13627 return NULL;
13628 for (j = 0; j < bset->n_ineq; ++j) {
13629 isl_bool internal;
13631 internal = is_internal(vec, bset, j);
13632 if (internal < 0)
13633 return isl_basic_set_free(bset);
13634 if (internal)
13635 continue;
13636 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13639 return bset;
13642 /* Replace the variables x of type "type" starting at "first" in "bmap"
13643 * by x' with x = M x' with M the matrix trans.
13644 * That is, replace the corresponding coefficients c by c M.
13646 * The transformation matrix should be a square matrix.
13648 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13649 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13650 __isl_take isl_mat *trans)
13652 unsigned pos;
13654 bmap = isl_basic_map_cow(bmap);
13655 if (!bmap || !trans)
13656 goto error;
13658 if (trans->n_row != trans->n_col)
13659 isl_die(trans->ctx, isl_error_invalid,
13660 "expecting square transformation matrix", goto error);
13661 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13662 goto error;
13664 pos = isl_basic_map_offset(bmap, type) + first;
13666 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13667 isl_mat_copy(trans)) < 0)
13668 goto error;
13669 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13670 isl_mat_copy(trans)) < 0)
13671 goto error;
13672 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13673 isl_mat_copy(trans)) < 0)
13674 goto error;
13676 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13677 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13679 isl_mat_free(trans);
13680 return bmap;
13681 error:
13682 isl_mat_free(trans);
13683 isl_basic_map_free(bmap);
13684 return NULL;
13687 /* Replace the variables x of type "type" starting at "first" in "bset"
13688 * by x' with x = M x' with M the matrix trans.
13689 * That is, replace the corresponding coefficients c by c M.
13691 * The transformation matrix should be a square matrix.
13693 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13694 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13695 __isl_take isl_mat *trans)
13697 return isl_basic_map_transform_dims(bset, type, first, trans);