isl_map_simplify.c: has_multiple_var_equality: return isl_bool
[isl.git] / isl_map.c
blob45011df3c763435cb5ff287770fd4adde7b862d9
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>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep const isl_basic_map *const_bset_to_bmap(
58 __isl_keep const isl_basic_set *bset)
60 return (const isl_basic_map *) bset;
63 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return dim->nparam;
67 case isl_dim_in: return dim->n_in;
68 case isl_dim_out: return dim->n_out;
69 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
70 default: return 0;
74 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
76 switch (type) {
77 case isl_dim_param: return 1;
78 case isl_dim_in: return 1 + dim->nparam;
79 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
80 default: return 0;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
85 enum isl_dim_type type)
87 if (!bmap)
88 return 0;
89 switch (type) {
90 case isl_dim_cst: return 1;
91 case isl_dim_param:
92 case isl_dim_in:
93 case isl_dim_out: return isl_space_dim(bmap->dim, type);
94 case isl_dim_div: return bmap->n_div;
95 case isl_dim_all: return isl_basic_map_total_dim(bmap);
96 default: return 0;
100 /* Return the space of "map".
102 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
104 return map ? map->dim : NULL;
107 /* Return the space of "set".
109 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
111 return isl_map_peek_space(set_to_map(set));
114 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
116 return map ? n(map->dim, type) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
121 return set ? n(set->dim, type) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
128 enum isl_dim_type type)
130 isl_space *space;
132 space = isl_basic_map_peek_space(bmap);
133 if (!space)
134 return -1;
136 switch (type) {
137 case isl_dim_param:
138 case isl_dim_in:
139 case isl_dim_out: return isl_space_offset(space, type);
140 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
141 case isl_dim_cst:
142 default:
143 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
152 enum isl_dim_type type)
154 switch (type) {
155 case isl_dim_cst: return 0;
156 case isl_dim_param:
157 case isl_dim_in:
158 case isl_dim_out:
159 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
160 default: return 0;
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
165 enum isl_dim_type type)
167 return isl_basic_map_offset(bset, type);
170 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
172 return pos(map->dim, type);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
176 enum isl_dim_type type)
178 return isl_basic_map_dim(bset, type);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
183 return isl_basic_set_dim(bset, isl_dim_set);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_param);
191 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
196 unsigned isl_set_n_dim(__isl_keep isl_set *set)
198 return isl_set_dim(set, isl_dim_set);
201 unsigned isl_set_n_param(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_param);
206 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
208 return bmap ? bmap->dim->n_in : 0;
211 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
213 return bmap ? bmap->dim->n_out : 0;
216 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->nparam : 0;
221 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->n_div : 0;
226 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
228 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
231 unsigned isl_map_n_in(__isl_keep const isl_map *map)
233 return map ? map->dim->n_in : 0;
236 unsigned isl_map_n_out(__isl_keep const isl_map *map)
238 return map ? map->dim->n_out : 0;
241 unsigned isl_map_n_param(__isl_keep const isl_map *map)
243 return map ? map->dim->nparam : 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return -1;
253 return bmap->n_eq;
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
261 return isl_basic_map_n_equality(bset_to_bmap(bset));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return -1;
271 return bmap->n_ineq;
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
285 __isl_keep isl_basic_map *bmap2)
287 isl_space *space1, *space2;
289 space1 = isl_basic_map_peek_space(bmap1);
290 space2 = isl_basic_map_peek_space(bmap2);
291 return isl_space_has_equal_params(space1, space2);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
297 __isl_keep isl_map *map2)
299 isl_space *space1, *space2;
301 space1 = isl_map_peek_space(map1);
302 space2 = isl_map_peek_space(map2);
303 return isl_space_has_equal_params(space1, space2);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 return isl_map_has_equal_params(map, set_to_map(set));
314 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
315 __isl_keep isl_set *set)
317 isl_bool m;
318 if (!map || !set)
319 return isl_bool_error;
320 m = isl_map_has_equal_params(map, set_to_map(set));
321 if (m < 0 || !m)
322 return m;
323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
324 set->dim, isl_dim_set);
327 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
328 __isl_keep isl_basic_set *bset)
330 isl_bool m;
331 if (!bmap || !bset)
332 return isl_bool_error;
333 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
334 if (m < 0 || !m)
335 return m;
336 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
337 bset->dim, isl_dim_set);
340 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
341 __isl_keep isl_set *set)
343 isl_bool m;
344 if (!map || !set)
345 return isl_bool_error;
346 m = isl_map_has_equal_params(map, set_to_map(set));
347 if (m < 0 || !m)
348 return m;
349 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
350 set->dim, isl_dim_set);
353 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
354 __isl_keep isl_basic_set *bset)
356 isl_bool m;
357 if (!bmap || !bset)
358 return isl_bool_error;
359 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
360 if (m < 0 || !m)
361 return m;
362 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
363 bset->dim, isl_dim_set);
366 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
368 return bmap ? bmap->ctx : NULL;
371 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
373 return bset ? bset->ctx : NULL;
376 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
378 return map ? map->ctx : NULL;
381 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
383 return set ? set->ctx : NULL;
386 /* Return the space of "bmap".
388 __isl_keep isl_space *isl_basic_map_peek_space(
389 __isl_keep const isl_basic_map *bmap)
391 return bmap ? bmap->dim : NULL;
394 /* Return the space of "bset".
396 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
398 return isl_basic_map_peek_space(bset_to_bmap(bset));
401 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
403 return isl_space_copy(isl_basic_map_peek_space(bmap));
406 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_get_space(bset_to_bmap(bset));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
415 int i;
416 isl_ctx *ctx;
417 isl_mat *div;
418 unsigned total;
419 unsigned cols;
421 if (!bmap)
422 return NULL;
424 ctx = isl_basic_map_get_ctx(bmap);
425 total = isl_space_dim(bmap->dim, isl_dim_all);
426 cols = 1 + 1 + total + bmap->n_div;
427 div = isl_mat_alloc(ctx, bmap->n_div, cols);
428 if (!div)
429 return NULL;
431 for (i = 0; i < bmap->n_div; ++i)
432 isl_seq_cpy(div->row[i], bmap->div[i], cols);
434 return div;
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
441 return isl_basic_map_get_divs(bset);
444 __isl_give isl_local_space *isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map *bmap)
447 isl_mat *div;
449 if (!bmap)
450 return NULL;
452 div = isl_basic_map_get_divs(bmap);
453 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
456 __isl_give isl_local_space *isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set *bset)
459 return isl_basic_map_get_local_space(bset);
462 /* For each known div d = floor(f/m), add the constraints
464 * f - m d >= 0
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map *add_known_div_constraints(
470 __isl_take isl_basic_map *bmap)
472 int i;
473 unsigned n_div;
475 if (!bmap)
476 return NULL;
477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
478 if (n_div == 0)
479 return bmap;
480 bmap = isl_basic_map_cow(bmap);
481 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
482 if (!bmap)
483 return NULL;
484 for (i = 0; i < n_div; ++i) {
485 if (isl_int_is_zero(bmap->div[i][0]))
486 continue;
487 bmap = isl_basic_map_add_div_constraints(bmap, i);
490 return bmap;
493 __isl_give isl_basic_map *isl_basic_map_from_local_space(
494 __isl_take isl_local_space *ls)
496 int i;
497 int n_div;
498 isl_basic_map *bmap;
500 if (!ls)
501 return NULL;
503 n_div = isl_local_space_dim(ls, isl_dim_div);
504 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
505 n_div, 0, 2 * n_div);
507 for (i = 0; i < n_div; ++i)
508 if (isl_basic_map_alloc_div(bmap) < 0)
509 goto error;
511 for (i = 0; i < n_div; ++i)
512 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
513 bmap = add_known_div_constraints(bmap);
515 isl_local_space_free(ls);
516 return bmap;
517 error:
518 isl_local_space_free(ls);
519 isl_basic_map_free(bmap);
520 return NULL;
523 __isl_give isl_basic_set *isl_basic_set_from_local_space(
524 __isl_take isl_local_space *ls)
526 return isl_basic_map_from_local_space(ls);
529 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
531 return isl_space_copy(isl_map_peek_space(map));
534 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
536 if (!set)
537 return NULL;
538 return isl_space_copy(set->dim);
541 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
542 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
544 bmap = isl_basic_map_cow(bmap);
545 if (!bmap)
546 return NULL;
547 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
548 if (!bmap->dim)
549 goto error;
550 bmap = isl_basic_map_finalize(bmap);
551 return bmap;
552 error:
553 isl_basic_map_free(bmap);
554 return NULL;
557 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
558 __isl_take isl_basic_set *bset, const char *s)
560 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
563 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
564 enum isl_dim_type type)
566 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
569 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
570 enum isl_dim_type type, const char *s)
572 int i;
574 map = isl_map_cow(map);
575 if (!map)
576 return NULL;
578 map->dim = isl_space_set_tuple_name(map->dim, type, s);
579 if (!map->dim)
580 goto error;
582 for (i = 0; i < map->n; ++i) {
583 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
584 if (!map->p[i])
585 goto error;
588 return map;
589 error:
590 isl_map_free(map);
591 return NULL;
594 /* Replace the identifier of the tuple of type "type" by "id".
596 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
597 __isl_take isl_basic_map *bmap,
598 enum isl_dim_type type, __isl_take isl_id *id)
600 bmap = isl_basic_map_cow(bmap);
601 if (!bmap)
602 goto error;
603 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
604 if (!bmap->dim)
605 return isl_basic_map_free(bmap);
606 bmap = isl_basic_map_finalize(bmap);
607 return bmap;
608 error:
609 isl_id_free(id);
610 return NULL;
613 /* Replace the identifier of the tuple by "id".
615 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
616 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
618 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
621 /* Does the input or output tuple have a name?
623 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
625 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
628 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
629 enum isl_dim_type type)
631 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
634 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
635 const char *s)
637 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
638 isl_dim_set, s));
641 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
642 enum isl_dim_type type, __isl_take isl_id *id)
644 map = isl_map_cow(map);
645 if (!map)
646 goto error;
648 map->dim = isl_space_set_tuple_id(map->dim, type, id);
650 return isl_map_reset_space(map, isl_space_copy(map->dim));
651 error:
652 isl_id_free(id);
653 return NULL;
656 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
657 __isl_take isl_id *id)
659 return isl_map_set_tuple_id(set, isl_dim_set, id);
662 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
663 enum isl_dim_type type)
665 map = isl_map_cow(map);
666 if (!map)
667 return NULL;
669 map->dim = isl_space_reset_tuple_id(map->dim, type);
671 return isl_map_reset_space(map, isl_space_copy(map->dim));
674 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
676 return isl_map_reset_tuple_id(set, isl_dim_set);
679 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
681 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
684 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
685 enum isl_dim_type type)
687 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
690 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
692 return isl_map_has_tuple_id(set, isl_dim_set);
695 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
697 return isl_map_get_tuple_id(set, isl_dim_set);
700 /* Does the set tuple have a name?
702 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
704 if (!set)
705 return isl_bool_error;
706 return isl_space_has_tuple_name(set->dim, isl_dim_set);
710 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
712 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
715 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
717 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
720 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
721 enum isl_dim_type type, unsigned pos)
723 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
726 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
727 enum isl_dim_type type, unsigned pos)
729 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
732 /* Does the given dimension have a name?
734 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
735 enum isl_dim_type type, unsigned pos)
737 if (!map)
738 return isl_bool_error;
739 return isl_space_has_dim_name(map->dim, type, pos);
742 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
743 enum isl_dim_type type, unsigned pos)
745 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
748 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
749 enum isl_dim_type type, unsigned pos)
751 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
754 /* Does the given dimension have a name?
756 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
757 enum isl_dim_type type, unsigned pos)
759 if (!set)
760 return isl_bool_error;
761 return isl_space_has_dim_name(set->dim, type, pos);
764 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
765 __isl_take isl_basic_map *bmap,
766 enum isl_dim_type type, unsigned pos, const char *s)
768 bmap = isl_basic_map_cow(bmap);
769 if (!bmap)
770 return NULL;
771 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
772 if (!bmap->dim)
773 goto error;
774 return isl_basic_map_finalize(bmap);
775 error:
776 isl_basic_map_free(bmap);
777 return NULL;
780 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
781 enum isl_dim_type type, unsigned pos, const char *s)
783 int i;
785 map = isl_map_cow(map);
786 if (!map)
787 return NULL;
789 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
790 if (!map->dim)
791 goto error;
793 for (i = 0; i < map->n; ++i) {
794 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
795 if (!map->p[i])
796 goto error;
799 return map;
800 error:
801 isl_map_free(map);
802 return NULL;
805 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
806 __isl_take isl_basic_set *bset,
807 enum isl_dim_type type, unsigned pos, const char *s)
809 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
810 type, pos, s));
813 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
814 enum isl_dim_type type, unsigned pos, const char *s)
816 return set_from_map(isl_map_set_dim_name(set_to_map(set),
817 type, pos, s));
820 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
821 enum isl_dim_type type, unsigned pos)
823 if (!bmap)
824 return isl_bool_error;
825 return isl_space_has_dim_id(bmap->dim, type, pos);
828 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
829 enum isl_dim_type type, unsigned pos)
831 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
834 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
835 enum isl_dim_type type, unsigned pos)
837 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
840 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
841 enum isl_dim_type type, unsigned pos)
843 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
846 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
847 enum isl_dim_type type, unsigned pos)
849 return isl_map_has_dim_id(set, type, pos);
852 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
853 enum isl_dim_type type, unsigned pos)
855 return isl_map_get_dim_id(set, type, pos);
858 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
859 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
861 map = isl_map_cow(map);
862 if (!map)
863 goto error;
865 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
867 return isl_map_reset_space(map, isl_space_copy(map->dim));
868 error:
869 isl_id_free(id);
870 return NULL;
873 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
874 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
876 return isl_map_set_dim_id(set, type, pos, id);
879 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
880 __isl_keep isl_id *id)
882 if (!map)
883 return -1;
884 return isl_space_find_dim_by_id(map->dim, type, id);
887 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
888 __isl_keep isl_id *id)
890 return isl_map_find_dim_by_id(set, type, id);
893 /* Return the position of the dimension of the given type and name
894 * in "bmap".
895 * Return -1 if no such dimension can be found.
897 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
898 enum isl_dim_type type, const char *name)
900 if (!bmap)
901 return -1;
902 return isl_space_find_dim_by_name(bmap->dim, type, name);
905 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
906 const char *name)
908 if (!map)
909 return -1;
910 return isl_space_find_dim_by_name(map->dim, type, name);
913 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
914 const char *name)
916 return isl_map_find_dim_by_name(set, type, name);
919 /* Check whether equality i of bset is a pure stride constraint
920 * on a single dimension, i.e., of the form
922 * v = k e
924 * with k a constant and e an existentially quantified variable.
926 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
928 unsigned nparam;
929 unsigned d;
930 unsigned n_div;
931 int pos1;
932 int pos2;
934 if (!bset)
935 return isl_bool_error;
937 if (!isl_int_is_zero(bset->eq[i][0]))
938 return isl_bool_false;
940 nparam = isl_basic_set_dim(bset, isl_dim_param);
941 d = isl_basic_set_dim(bset, isl_dim_set);
942 n_div = isl_basic_set_dim(bset, isl_dim_div);
944 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
945 return isl_bool_false;
946 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
947 if (pos1 == -1)
948 return isl_bool_false;
949 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
950 d - pos1 - 1) != -1)
951 return isl_bool_false;
953 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
954 if (pos2 == -1)
955 return isl_bool_false;
956 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
957 n_div - pos2 - 1) != -1)
958 return isl_bool_false;
959 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
960 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
961 return isl_bool_false;
963 return isl_bool_true;
966 /* Reset the user pointer on all identifiers of parameters and tuples
967 * of the space of "map".
969 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
971 isl_space *space;
973 space = isl_map_get_space(map);
974 space = isl_space_reset_user(space);
975 map = isl_map_reset_space(map, space);
977 return map;
980 /* Reset the user pointer on all identifiers of parameters and tuples
981 * of the space of "set".
983 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
985 return isl_map_reset_user(set);
988 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
990 if (!bmap)
991 return isl_bool_error;
992 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
995 /* Has "map" been marked as a rational map?
996 * In particular, have all basic maps in "map" been marked this way?
997 * An empty map is not considered to be rational.
998 * Maps where only some of the basic maps are marked rational
999 * are not allowed.
1001 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1003 int i;
1004 isl_bool rational;
1006 if (!map)
1007 return isl_bool_error;
1008 if (map->n == 0)
1009 return isl_bool_false;
1010 rational = isl_basic_map_is_rational(map->p[0]);
1011 if (rational < 0)
1012 return rational;
1013 for (i = 1; i < map->n; ++i) {
1014 isl_bool rational_i;
1016 rational_i = isl_basic_map_is_rational(map->p[i]);
1017 if (rational_i < 0)
1018 return rational_i;
1019 if (rational != rational_i)
1020 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1021 "mixed rational and integer basic maps "
1022 "not supported", return isl_bool_error);
1025 return rational;
1028 /* Has "set" been marked as a rational set?
1029 * In particular, have all basic set in "set" been marked this way?
1030 * An empty set is not considered to be rational.
1031 * Sets where only some of the basic sets are marked rational
1032 * are not allowed.
1034 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1036 return isl_map_is_rational(set);
1039 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1041 return isl_basic_map_is_rational(bset);
1044 /* Does "bmap" contain any rational points?
1046 * If "bmap" has an equality for each dimension, equating the dimension
1047 * to an integer constant, then it has no rational points, even if it
1048 * is marked as rational.
1050 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1052 isl_bool has_rational = isl_bool_true;
1053 unsigned total;
1055 if (!bmap)
1056 return isl_bool_error;
1057 if (isl_basic_map_plain_is_empty(bmap))
1058 return isl_bool_false;
1059 if (!isl_basic_map_is_rational(bmap))
1060 return isl_bool_false;
1061 bmap = isl_basic_map_copy(bmap);
1062 bmap = isl_basic_map_implicit_equalities(bmap);
1063 if (!bmap)
1064 return isl_bool_error;
1065 total = isl_basic_map_total_dim(bmap);
1066 if (bmap->n_eq == total) {
1067 int i, j;
1068 for (i = 0; i < bmap->n_eq; ++i) {
1069 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1070 if (j < 0)
1071 break;
1072 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1073 !isl_int_is_negone(bmap->eq[i][1 + j]))
1074 break;
1075 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1076 total - j - 1);
1077 if (j >= 0)
1078 break;
1080 if (i == bmap->n_eq)
1081 has_rational = isl_bool_false;
1083 isl_basic_map_free(bmap);
1085 return has_rational;
1088 /* Does "map" contain any rational points?
1090 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1092 int i;
1093 isl_bool has_rational;
1095 if (!map)
1096 return isl_bool_error;
1097 for (i = 0; i < map->n; ++i) {
1098 has_rational = isl_basic_map_has_rational(map->p[i]);
1099 if (has_rational < 0 || has_rational)
1100 return has_rational;
1102 return isl_bool_false;
1105 /* Does "set" contain any rational points?
1107 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1109 return isl_map_has_rational(set);
1112 /* Is this basic set a parameter domain?
1114 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1116 if (!bset)
1117 return isl_bool_error;
1118 return isl_space_is_params(bset->dim);
1121 /* Is this set a parameter domain?
1123 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1125 if (!set)
1126 return isl_bool_error;
1127 return isl_space_is_params(set->dim);
1130 /* Is this map actually a parameter domain?
1131 * Users should never call this function. Outside of isl,
1132 * a map can never be a parameter domain.
1134 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1136 if (!map)
1137 return isl_bool_error;
1138 return isl_space_is_params(map->dim);
1141 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1142 __isl_take isl_basic_map *bmap, unsigned extra,
1143 unsigned n_eq, unsigned n_ineq)
1145 int i;
1146 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1148 bmap->ctx = ctx;
1149 isl_ctx_ref(ctx);
1151 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1152 if (isl_blk_is_error(bmap->block))
1153 goto error;
1155 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1156 if ((n_ineq + n_eq) && !bmap->ineq)
1157 goto error;
1159 if (extra == 0) {
1160 bmap->block2 = isl_blk_empty();
1161 bmap->div = NULL;
1162 } else {
1163 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1164 if (isl_blk_is_error(bmap->block2))
1165 goto error;
1167 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1168 if (!bmap->div)
1169 goto error;
1172 for (i = 0; i < n_ineq + n_eq; ++i)
1173 bmap->ineq[i] = bmap->block.data + i * row_size;
1175 for (i = 0; i < extra; ++i)
1176 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1178 bmap->ref = 1;
1179 bmap->flags = 0;
1180 bmap->c_size = n_eq + n_ineq;
1181 bmap->eq = bmap->ineq + n_ineq;
1182 bmap->extra = extra;
1183 bmap->n_eq = 0;
1184 bmap->n_ineq = 0;
1185 bmap->n_div = 0;
1186 bmap->sample = NULL;
1188 return bmap;
1189 error:
1190 isl_basic_map_free(bmap);
1191 return NULL;
1194 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1195 unsigned nparam, unsigned dim, unsigned extra,
1196 unsigned n_eq, unsigned n_ineq)
1198 struct isl_basic_map *bmap;
1199 isl_space *space;
1201 space = isl_space_set_alloc(ctx, nparam, dim);
1202 if (!space)
1203 return NULL;
1205 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1206 return bset_from_bmap(bmap);
1209 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1210 unsigned extra, unsigned n_eq, unsigned n_ineq)
1212 struct isl_basic_map *bmap;
1213 if (!dim)
1214 return NULL;
1215 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1216 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1217 return bset_from_bmap(bmap);
1218 error:
1219 isl_space_free(dim);
1220 return NULL;
1223 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1224 unsigned extra, unsigned n_eq, unsigned n_ineq)
1226 struct isl_basic_map *bmap;
1228 if (!space)
1229 return NULL;
1230 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1231 if (!bmap)
1232 goto error;
1233 bmap->dim = space;
1235 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1236 error:
1237 isl_space_free(space);
1238 return NULL;
1241 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1242 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1243 unsigned n_eq, unsigned n_ineq)
1245 struct isl_basic_map *bmap;
1246 isl_space *dim;
1248 dim = isl_space_alloc(ctx, nparam, in, out);
1249 if (!dim)
1250 return NULL;
1252 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1253 return bmap;
1256 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1257 __isl_keep isl_basic_map *src)
1259 int i;
1260 unsigned total = isl_basic_map_total_dim(src);
1262 if (!dst)
1263 return NULL;
1265 for (i = 0; i < src->n_eq; ++i) {
1266 int j = isl_basic_map_alloc_equality(dst);
1267 if (j < 0)
1268 return isl_basic_map_free(dst);
1269 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1272 for (i = 0; i < src->n_ineq; ++i) {
1273 int j = isl_basic_map_alloc_inequality(dst);
1274 if (j < 0)
1275 return isl_basic_map_free(dst);
1276 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1279 for (i = 0; i < src->n_div; ++i) {
1280 int j = isl_basic_map_alloc_div(dst);
1281 if (j < 0)
1282 return isl_basic_map_free(dst);
1283 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1285 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1286 return dst;
1289 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1291 struct isl_basic_map *dup;
1293 if (!bmap)
1294 return NULL;
1295 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1296 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1297 dup = dup_constraints(dup, bmap);
1298 if (!dup)
1299 return NULL;
1300 dup->flags = bmap->flags;
1301 dup->sample = isl_vec_copy(bmap->sample);
1302 return dup;
1305 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1307 struct isl_basic_map *dup;
1309 dup = isl_basic_map_dup(bset_to_bmap(bset));
1310 return bset_from_bmap(dup);
1313 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1315 if (!bset)
1316 return NULL;
1318 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1319 bset->ref++;
1320 return bset;
1322 return isl_basic_set_dup(bset);
1325 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1327 if (!set)
1328 return NULL;
1330 set->ref++;
1331 return set;
1334 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1336 if (!bmap)
1337 return NULL;
1339 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1340 bmap->ref++;
1341 return bmap;
1343 bmap = isl_basic_map_dup(bmap);
1344 if (bmap)
1345 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1346 return bmap;
1349 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1351 if (!map)
1352 return NULL;
1354 map->ref++;
1355 return map;
1358 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1360 if (!bmap)
1361 return NULL;
1363 if (--bmap->ref > 0)
1364 return NULL;
1366 isl_ctx_deref(bmap->ctx);
1367 free(bmap->div);
1368 isl_blk_free(bmap->ctx, bmap->block2);
1369 free(bmap->ineq);
1370 isl_blk_free(bmap->ctx, bmap->block);
1371 isl_vec_free(bmap->sample);
1372 isl_space_free(bmap->dim);
1373 free(bmap);
1375 return NULL;
1378 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1380 return isl_basic_map_free(bset_to_bmap(bset));
1383 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1385 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1388 /* Check that "bset" does not involve any parameters.
1390 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1392 if (!bset)
1393 return isl_stat_error;
1394 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1395 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1396 "basic set should not have any parameters",
1397 return isl_stat_error);
1398 return isl_stat_ok;
1401 /* Check that "bset" does not involve any local variables.
1403 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1405 if (!bset)
1406 return isl_stat_error;
1407 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1408 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1409 "basic set should not have any local variables",
1410 return isl_stat_error);
1411 return isl_stat_ok;
1414 /* Check that "map" has only named parameters, reporting an error
1415 * if it does not.
1417 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1419 return isl_space_check_named_params(isl_map_peek_space(map));
1422 /* Check that "bmap" has only named parameters, reporting an error
1423 * if it does not.
1425 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1427 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1430 /* Check that "bmap1" and "bmap2" have the same parameters,
1431 * reporting an error if they do not.
1433 static isl_stat isl_basic_map_check_equal_params(
1434 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1436 isl_bool match;
1438 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1439 if (match < 0)
1440 return isl_stat_error;
1441 if (!match)
1442 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1443 "parameters don't match", return isl_stat_error);
1444 return isl_stat_ok;
1447 __isl_give isl_map *isl_map_align_params_map_map_and(
1448 __isl_take isl_map *map1, __isl_take isl_map *map2,
1449 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1450 __isl_take isl_map *map2))
1452 if (!map1 || !map2)
1453 goto error;
1454 if (isl_map_has_equal_params(map1, map2))
1455 return fn(map1, map2);
1456 if (isl_map_check_named_params(map1) < 0)
1457 goto error;
1458 if (isl_map_check_named_params(map2) < 0)
1459 goto error;
1460 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1461 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1462 return fn(map1, map2);
1463 error:
1464 isl_map_free(map1);
1465 isl_map_free(map2);
1466 return NULL;
1469 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1470 __isl_keep isl_map *map2,
1471 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1473 isl_bool r;
1475 if (!map1 || !map2)
1476 return isl_bool_error;
1477 if (isl_map_has_equal_params(map1, map2))
1478 return fn(map1, map2);
1479 if (isl_map_check_named_params(map1) < 0)
1480 return isl_bool_error;
1481 if (isl_map_check_named_params(map2) < 0)
1482 return isl_bool_error;
1483 map1 = isl_map_copy(map1);
1484 map2 = isl_map_copy(map2);
1485 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1486 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1487 r = fn(map1, map2);
1488 isl_map_free(map1);
1489 isl_map_free(map2);
1490 return r;
1493 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1495 struct isl_ctx *ctx;
1496 if (!bmap)
1497 return -1;
1498 ctx = bmap->ctx;
1499 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1500 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1501 return -1);
1502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1506 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1507 isl_int *t;
1508 int j = isl_basic_map_alloc_inequality(bmap);
1509 if (j < 0)
1510 return -1;
1511 t = bmap->ineq[j];
1512 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1513 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1514 bmap->eq[-1] = t;
1515 bmap->n_eq++;
1516 bmap->n_ineq--;
1517 bmap->eq--;
1518 return 0;
1520 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1521 bmap->extra - bmap->n_div);
1522 return bmap->n_eq++;
1525 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1527 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1530 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1532 if (!bmap)
1533 return -1;
1534 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1535 bmap->n_eq -= n;
1536 return 0;
1539 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1541 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1544 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1546 isl_int *t;
1547 if (!bmap)
1548 return -1;
1549 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1551 if (pos != bmap->n_eq - 1) {
1552 t = bmap->eq[pos];
1553 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1554 bmap->eq[bmap->n_eq - 1] = t;
1556 bmap->n_eq--;
1557 return 0;
1560 /* Turn inequality "pos" of "bmap" into an equality.
1562 * In particular, we move the inequality in front of the equalities
1563 * and move the last inequality in the position of the moved inequality.
1564 * Note that isl_tab_make_equalities_explicit depends on this particular
1565 * change in the ordering of the constraints.
1567 void isl_basic_map_inequality_to_equality(
1568 struct isl_basic_map *bmap, unsigned pos)
1570 isl_int *t;
1572 t = bmap->ineq[pos];
1573 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1574 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1575 bmap->eq[-1] = t;
1576 bmap->n_eq++;
1577 bmap->n_ineq--;
1578 bmap->eq--;
1579 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1582 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1585 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1587 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1590 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1592 struct isl_ctx *ctx;
1593 if (!bmap)
1594 return -1;
1595 ctx = bmap->ctx;
1596 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1597 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1600 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1601 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1602 1 + isl_basic_map_total_dim(bmap),
1603 bmap->extra - bmap->n_div);
1604 return bmap->n_ineq++;
1607 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1609 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1612 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1614 if (!bmap)
1615 return -1;
1616 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1617 bmap->n_ineq -= n;
1618 return 0;
1621 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1623 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1626 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1628 isl_int *t;
1629 if (!bmap)
1630 return -1;
1631 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1633 if (pos != bmap->n_ineq - 1) {
1634 t = bmap->ineq[pos];
1635 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1636 bmap->ineq[bmap->n_ineq - 1] = t;
1637 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1639 bmap->n_ineq--;
1640 return 0;
1643 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1645 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1648 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1649 isl_int *eq)
1651 isl_bool empty;
1652 int k;
1654 empty = isl_basic_map_plain_is_empty(bmap);
1655 if (empty < 0)
1656 return isl_basic_map_free(bmap);
1657 if (empty)
1658 return bmap;
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1662 if (!bmap)
1663 return NULL;
1664 k = isl_basic_map_alloc_equality(bmap);
1665 if (k < 0)
1666 goto error;
1667 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1668 return bmap;
1669 error:
1670 isl_basic_map_free(bmap);
1671 return NULL;
1674 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1675 isl_int *eq)
1677 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1680 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1681 isl_int *ineq)
1683 int k;
1685 bmap = isl_basic_map_cow(bmap);
1686 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1687 if (!bmap)
1688 return NULL;
1689 k = isl_basic_map_alloc_inequality(bmap);
1690 if (k < 0)
1691 goto error;
1692 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1693 return bmap;
1694 error:
1695 isl_basic_map_free(bmap);
1696 return NULL;
1699 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1700 isl_int *ineq)
1702 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1705 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1707 if (!bmap)
1708 return -1;
1709 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1710 isl_seq_clr(bmap->div[bmap->n_div] +
1711 1 + 1 + isl_basic_map_total_dim(bmap),
1712 bmap->extra - bmap->n_div);
1713 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1714 return bmap->n_div++;
1717 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1719 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1722 #undef TYPE
1723 #define TYPE isl_basic_map
1724 #include "check_type_range_templ.c"
1726 /* Check that there are "n" dimensions of type "type" starting at "first"
1727 * in "bset".
1729 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1730 enum isl_dim_type type, unsigned first, unsigned n)
1732 return isl_basic_map_check_range(bset_to_bmap(bset),
1733 type, first, n);
1736 /* Insert an extra integer division, prescribed by "div", to "bmap"
1737 * at (integer division) position "pos".
1739 * The integer division is first added at the end and then moved
1740 * into the right position.
1742 __isl_give isl_basic_map *isl_basic_map_insert_div(
1743 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1745 int i, k;
1747 bmap = isl_basic_map_cow(bmap);
1748 if (!bmap || !div)
1749 return isl_basic_map_free(bmap);
1751 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
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 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1785 isl_ctx *ctx;
1786 isl_space *space;
1787 struct isl_dim_map *dim_map;
1789 space = isl_basic_map_peek_space(bmap2);
1790 if (!bmap1 || !space)
1791 goto error;
1793 total = isl_basic_map_dim(bmap1, isl_dim_all);
1794 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1795 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1796 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1797 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1798 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1799 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1800 ctx = isl_basic_map_get_ctx(bmap1);
1801 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1802 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1804 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1805 isl_dim_map_div(dim_map, bmap2, total);
1807 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1808 error:
1809 isl_basic_map_free(bmap1);
1810 isl_basic_map_free(bmap2);
1811 return NULL;
1814 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1815 struct isl_basic_set *bset2, unsigned pos)
1817 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1818 bset_to_bmap(bset2), 0, pos));
1821 __isl_give isl_basic_map *isl_basic_map_extend_space(
1822 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1823 unsigned extra, unsigned n_eq, unsigned n_ineq)
1825 struct isl_basic_map *ext;
1826 unsigned flags;
1827 int dims_ok;
1829 if (!space)
1830 goto error;
1832 if (!base)
1833 goto error;
1835 dims_ok = isl_space_is_equal(base->dim, space) &&
1836 base->extra >= base->n_div + extra;
1838 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1839 room_for_ineq(base, n_ineq)) {
1840 isl_space_free(space);
1841 return base;
1844 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1845 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1846 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1847 extra += base->extra;
1848 n_eq += base->n_eq;
1849 n_ineq += base->n_ineq;
1851 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1852 space = NULL;
1853 if (!ext)
1854 goto error;
1856 if (dims_ok)
1857 ext->sample = isl_vec_copy(base->sample);
1858 flags = base->flags;
1859 ext = add_constraints(ext, base, 0, 0);
1860 if (ext) {
1861 ext->flags = flags;
1862 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1865 return ext;
1867 error:
1868 isl_space_free(space);
1869 isl_basic_map_free(base);
1870 return NULL;
1873 __isl_give isl_basic_set *isl_basic_set_extend_space(
1874 __isl_take isl_basic_set *base,
1875 __isl_take isl_space *dim, unsigned extra,
1876 unsigned n_eq, unsigned n_ineq)
1878 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1879 dim, extra, n_eq, n_ineq));
1882 struct isl_basic_map *isl_basic_map_extend_constraints(
1883 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1885 if (!base)
1886 return NULL;
1887 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1888 0, n_eq, n_ineq);
1891 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1892 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1893 unsigned n_eq, unsigned n_ineq)
1895 struct isl_basic_map *bmap;
1896 isl_space *dim;
1898 if (!base)
1899 return NULL;
1900 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1901 if (!dim)
1902 goto error;
1904 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1905 return bmap;
1906 error:
1907 isl_basic_map_free(base);
1908 return NULL;
1911 struct isl_basic_set *isl_basic_set_extend_constraints(
1912 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1914 isl_basic_map *bmap = bset_to_bmap(base);
1915 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1916 return bset_from_bmap(bmap);
1919 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1921 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1924 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1926 if (!bmap)
1927 return NULL;
1929 if (bmap->ref > 1) {
1930 bmap->ref--;
1931 bmap = isl_basic_map_dup(bmap);
1933 if (bmap) {
1934 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1935 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1937 return bmap;
1940 /* Clear all cached information in "map", either because it is about
1941 * to be modified or because it is being freed.
1942 * Always return the same pointer that is passed in.
1943 * This is needed for the use in isl_map_free.
1945 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1947 isl_basic_map_free(map->cached_simple_hull[0]);
1948 isl_basic_map_free(map->cached_simple_hull[1]);
1949 map->cached_simple_hull[0] = NULL;
1950 map->cached_simple_hull[1] = NULL;
1951 return map;
1954 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1956 return isl_map_cow(set);
1959 /* Return an isl_map that is equal to "map" and that has only
1960 * a single reference.
1962 * If the original input already has only one reference, then
1963 * simply return it, but clear all cached information, since
1964 * it may be rendered invalid by the operations that will be
1965 * performed on the result.
1967 * Otherwise, create a duplicate (without any cached information).
1969 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1971 if (!map)
1972 return NULL;
1974 if (map->ref == 1)
1975 return clear_caches(map);
1976 map->ref--;
1977 return isl_map_dup(map);
1980 static void swap_vars(struct isl_blk blk, isl_int *a,
1981 unsigned a_len, unsigned b_len)
1983 isl_seq_cpy(blk.data, a+a_len, b_len);
1984 isl_seq_cpy(blk.data+b_len, a, a_len);
1985 isl_seq_cpy(a, blk.data, b_len+a_len);
1988 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1989 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1991 int i;
1992 struct isl_blk blk;
1994 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1995 goto error;
1997 if (n1 == 0 || n2 == 0)
1998 return bmap;
2000 bmap = isl_basic_map_cow(bmap);
2001 if (!bmap)
2002 return NULL;
2004 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2005 if (isl_blk_is_error(blk))
2006 goto error;
2008 for (i = 0; i < bmap->n_eq; ++i)
2009 swap_vars(blk,
2010 bmap->eq[i] + pos, n1, n2);
2012 for (i = 0; i < bmap->n_ineq; ++i)
2013 swap_vars(blk,
2014 bmap->ineq[i] + pos, n1, n2);
2016 for (i = 0; i < bmap->n_div; ++i)
2017 swap_vars(blk,
2018 bmap->div[i]+1 + pos, n1, n2);
2020 isl_blk_free(bmap->ctx, blk);
2022 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2023 bmap = isl_basic_map_gauss(bmap, NULL);
2024 return isl_basic_map_finalize(bmap);
2025 error:
2026 isl_basic_map_free(bmap);
2027 return NULL;
2030 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2031 __isl_take isl_basic_map *bmap)
2033 int i = 0;
2034 unsigned total;
2035 if (!bmap)
2036 goto error;
2037 total = isl_basic_map_total_dim(bmap);
2038 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2039 return isl_basic_map_free(bmap);
2040 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2041 if (bmap->n_eq > 0)
2042 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2043 else {
2044 i = isl_basic_map_alloc_equality(bmap);
2045 if (i < 0)
2046 goto error;
2048 isl_int_set_si(bmap->eq[i][0], 1);
2049 isl_seq_clr(bmap->eq[i]+1, total);
2050 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2051 isl_vec_free(bmap->sample);
2052 bmap->sample = NULL;
2053 return isl_basic_map_finalize(bmap);
2054 error:
2055 isl_basic_map_free(bmap);
2056 return NULL;
2059 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2060 __isl_take isl_basic_set *bset)
2062 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2065 __isl_give isl_basic_map *isl_basic_map_set_rational(
2066 __isl_take isl_basic_map *bmap)
2068 if (!bmap)
2069 return NULL;
2071 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2072 return bmap;
2074 bmap = isl_basic_map_cow(bmap);
2075 if (!bmap)
2076 return NULL;
2078 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2080 return isl_basic_map_finalize(bmap);
2083 __isl_give isl_basic_set *isl_basic_set_set_rational(
2084 __isl_take isl_basic_set *bset)
2086 return isl_basic_map_set_rational(bset);
2089 __isl_give isl_basic_set *isl_basic_set_set_integral(
2090 __isl_take isl_basic_set *bset)
2092 if (!bset)
2093 return NULL;
2095 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2096 return bset;
2098 bset = isl_basic_set_cow(bset);
2099 if (!bset)
2100 return NULL;
2102 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2104 return isl_basic_set_finalize(bset);
2107 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2109 int i;
2111 map = isl_map_cow(map);
2112 if (!map)
2113 return NULL;
2114 for (i = 0; i < map->n; ++i) {
2115 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2116 if (!map->p[i])
2117 goto error;
2119 return map;
2120 error:
2121 isl_map_free(map);
2122 return NULL;
2125 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2127 return isl_map_set_rational(set);
2130 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2131 * of "bmap").
2133 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2135 isl_int *t = bmap->div[a];
2136 bmap->div[a] = bmap->div[b];
2137 bmap->div[b] = t;
2140 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2141 * div definitions accordingly.
2143 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2144 int a, int b)
2146 int i;
2147 unsigned off;
2149 if (!bmap)
2150 return NULL;
2152 off = isl_space_dim(bmap->dim, isl_dim_all);
2153 swap_div(bmap, a, b);
2155 for (i = 0; i < bmap->n_eq; ++i)
2156 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2158 for (i = 0; i < bmap->n_ineq; ++i)
2159 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2161 for (i = 0; i < bmap->n_div; ++i)
2162 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2163 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2165 return bmap;
2168 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2170 isl_seq_cpy(c, c + n, rem);
2171 isl_seq_clr(c + rem, n);
2174 /* Drop n dimensions starting at first.
2176 * In principle, this frees up some extra variables as the number
2177 * of columns remains constant, but we would have to extend
2178 * the div array too as the number of rows in this array is assumed
2179 * to be equal to extra.
2181 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2182 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2184 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2187 /* Move "n" divs starting at "first" to the end of the list of divs.
2189 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2190 unsigned first, unsigned n)
2192 isl_int **div;
2193 int i;
2195 if (first + n == bmap->n_div)
2196 return bmap;
2198 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2199 if (!div)
2200 goto error;
2201 for (i = 0; i < n; ++i)
2202 div[i] = bmap->div[first + i];
2203 for (i = 0; i < bmap->n_div - first - n; ++i)
2204 bmap->div[first + i] = bmap->div[first + n + i];
2205 for (i = 0; i < n; ++i)
2206 bmap->div[bmap->n_div - n + i] = div[i];
2207 free(div);
2208 return bmap;
2209 error:
2210 isl_basic_map_free(bmap);
2211 return NULL;
2214 #undef TYPE
2215 #define TYPE isl_map
2216 static
2217 #include "check_type_range_templ.c"
2219 /* Check that there are "n" dimensions of type "type" starting at "first"
2220 * in "set".
2222 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 return isl_map_check_range(set_to_map(set), type, first, n);
2228 /* Drop "n" dimensions of type "type" starting at "first".
2229 * Perform the core computation, without cowing or
2230 * simplifying and finalizing the result.
2232 * In principle, this frees up some extra variables as the number
2233 * of columns remains constant, but we would have to extend
2234 * the div array too as the number of rows in this array is assumed
2235 * to be equal to extra.
2237 __isl_give isl_basic_map *isl_basic_map_drop_core(
2238 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2239 unsigned first, unsigned n)
2241 int i;
2242 unsigned offset;
2243 unsigned left;
2245 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2246 return isl_basic_map_free(bmap);
2248 offset = isl_basic_map_offset(bmap, type) + first;
2249 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2250 for (i = 0; i < bmap->n_eq; ++i)
2251 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2253 for (i = 0; i < bmap->n_ineq; ++i)
2254 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2256 for (i = 0; i < bmap->n_div; ++i)
2257 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2259 if (type == isl_dim_div) {
2260 bmap = move_divs_last(bmap, first, n);
2261 if (!bmap)
2262 return NULL;
2263 if (isl_basic_map_free_div(bmap, n) < 0)
2264 return isl_basic_map_free(bmap);
2265 } else
2266 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2267 if (!bmap->dim)
2268 return isl_basic_map_free(bmap);
2270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2271 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2272 return bmap;
2275 /* Drop "n" dimensions of type "type" starting at "first".
2277 * In principle, this frees up some extra variables as the number
2278 * of columns remains constant, but we would have to extend
2279 * the div array too as the number of rows in this array is assumed
2280 * to be equal to extra.
2282 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 if (!bmap)
2286 return NULL;
2287 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2288 return bmap;
2290 bmap = isl_basic_map_cow(bmap);
2291 if (!bmap)
2292 return NULL;
2294 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2296 bmap = isl_basic_map_simplify(bmap);
2297 return isl_basic_map_finalize(bmap);
2300 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2301 enum isl_dim_type type, unsigned first, unsigned n)
2303 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2304 type, first, n));
2307 /* No longer consider "map" to be normalized.
2309 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2311 if (!map)
2312 return NULL;
2313 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2314 return map;
2317 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2318 enum isl_dim_type type, unsigned first, unsigned n)
2320 int i;
2322 if (isl_map_check_range(map, type, first, n) < 0)
2323 return isl_map_free(map);
2325 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2326 return map;
2327 map = isl_map_cow(map);
2328 if (!map)
2329 goto error;
2330 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2331 if (!map->dim)
2332 goto error;
2334 for (i = 0; i < map->n; ++i) {
2335 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2336 if (!map->p[i])
2337 goto error;
2339 map = isl_map_unmark_normalized(map);
2341 return map;
2342 error:
2343 isl_map_free(map);
2344 return NULL;
2347 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2348 enum isl_dim_type type, unsigned first, unsigned n)
2350 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2353 /* Drop the integer division at position "div", which is assumed
2354 * not to appear in any of the constraints or
2355 * in any of the other integer divisions.
2357 * Since the integer division is redundant, there is no need to cow.
2359 __isl_give isl_basic_map *isl_basic_map_drop_div(
2360 __isl_take isl_basic_map *bmap, unsigned div)
2362 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2365 /* Eliminate the specified n dimensions starting at first from the
2366 * constraints, without removing the dimensions from the space.
2367 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2369 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 int i;
2374 if (n == 0)
2375 return map;
2377 if (isl_map_check_range(map, type, first, n) < 0)
2378 return isl_map_free(map);
2380 map = isl_map_cow(map);
2381 if (!map)
2382 return NULL;
2384 for (i = 0; i < map->n; ++i) {
2385 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2386 if (!map->p[i])
2387 goto error;
2389 return map;
2390 error:
2391 isl_map_free(map);
2392 return NULL;
2395 /* Eliminate the specified n dimensions starting at first from the
2396 * constraints, without removing the dimensions from the space.
2397 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2399 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2400 enum isl_dim_type type, unsigned first, unsigned n)
2402 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2405 /* Eliminate the specified n dimensions starting at first from the
2406 * constraints, without removing the dimensions from the space.
2407 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2409 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2410 unsigned first, unsigned n)
2412 return isl_set_eliminate(set, isl_dim_set, first, n);
2415 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2416 __isl_take isl_basic_map *bmap)
2418 if (!bmap)
2419 return NULL;
2420 bmap = isl_basic_map_eliminate_vars(bmap,
2421 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2422 if (!bmap)
2423 return NULL;
2424 bmap->n_div = 0;
2425 return isl_basic_map_finalize(bmap);
2428 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2429 __isl_take isl_basic_set *bset)
2431 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2434 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2436 int i;
2438 if (!map)
2439 return NULL;
2440 if (map->n == 0)
2441 return map;
2443 map = isl_map_cow(map);
2444 if (!map)
2445 return NULL;
2447 for (i = 0; i < map->n; ++i) {
2448 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2449 if (!map->p[i])
2450 goto error;
2452 return map;
2453 error:
2454 isl_map_free(map);
2455 return NULL;
2458 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2460 return isl_map_remove_divs(set);
2463 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2464 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2465 unsigned first, unsigned n)
2467 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2468 return isl_basic_map_free(bmap);
2469 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2470 return bmap;
2471 bmap = isl_basic_map_eliminate_vars(bmap,
2472 isl_basic_map_offset(bmap, type) - 1 + first, n);
2473 if (!bmap)
2474 return bmap;
2475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2476 return bmap;
2477 bmap = isl_basic_map_drop(bmap, type, first, n);
2478 return bmap;
2481 /* Return true if the definition of the given div (recursively) involves
2482 * any of the given variables.
2484 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2485 unsigned first, unsigned n)
2487 int i;
2488 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2490 if (isl_int_is_zero(bmap->div[div][0]))
2491 return isl_bool_false;
2492 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2493 return isl_bool_true;
2495 for (i = bmap->n_div - 1; i >= 0; --i) {
2496 isl_bool involves;
2498 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2499 continue;
2500 involves = div_involves_vars(bmap, i, first, n);
2501 if (involves < 0 || involves)
2502 return involves;
2505 return isl_bool_false;
2508 /* Try and add a lower and/or upper bound on "div" to "bmap"
2509 * based on inequality "i".
2510 * "total" is the total number of variables (excluding the divs).
2511 * "v" is a temporary object that can be used during the calculations.
2512 * If "lb" is set, then a lower bound should be constructed.
2513 * If "ub" is set, then an upper bound should be constructed.
2515 * The calling function has already checked that the inequality does not
2516 * reference "div", but we still need to check that the inequality is
2517 * of the right form. We'll consider the case where we want to construct
2518 * a lower bound. The construction of upper bounds is similar.
2520 * Let "div" be of the form
2522 * q = floor((a + f(x))/d)
2524 * We essentially check if constraint "i" is of the form
2526 * b + f(x) >= 0
2528 * so that we can use it to derive a lower bound on "div".
2529 * However, we allow a slightly more general form
2531 * b + g(x) >= 0
2533 * with the condition that the coefficients of g(x) - f(x) are all
2534 * divisible by d.
2535 * Rewriting this constraint as
2537 * 0 >= -b - g(x)
2539 * adding a + f(x) to both sides and dividing by d, we obtain
2541 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2543 * Taking the floor on both sides, we obtain
2545 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2547 * or
2549 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2551 * In the case of an upper bound, we construct the constraint
2553 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2556 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2557 __isl_take isl_basic_map *bmap, int div, int i,
2558 unsigned total, isl_int v, int lb, int ub)
2560 int j;
2562 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2563 if (lb) {
2564 isl_int_sub(v, bmap->ineq[i][1 + j],
2565 bmap->div[div][1 + 1 + j]);
2566 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2568 if (ub) {
2569 isl_int_add(v, bmap->ineq[i][1 + j],
2570 bmap->div[div][1 + 1 + j]);
2571 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2574 if (!lb && !ub)
2575 return bmap;
2577 bmap = isl_basic_map_cow(bmap);
2578 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2579 if (lb) {
2580 int k = isl_basic_map_alloc_inequality(bmap);
2581 if (k < 0)
2582 goto error;
2583 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2584 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2585 bmap->div[div][1 + j]);
2586 isl_int_cdiv_q(bmap->ineq[k][j],
2587 bmap->ineq[k][j], bmap->div[div][0]);
2589 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2591 if (ub) {
2592 int k = isl_basic_map_alloc_inequality(bmap);
2593 if (k < 0)
2594 goto error;
2595 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2596 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2597 bmap->div[div][1 + j]);
2598 isl_int_fdiv_q(bmap->ineq[k][j],
2599 bmap->ineq[k][j], bmap->div[div][0]);
2601 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2604 return bmap;
2605 error:
2606 isl_basic_map_free(bmap);
2607 return NULL;
2610 /* This function is called right before "div" is eliminated from "bmap"
2611 * using Fourier-Motzkin.
2612 * Look through the constraints of "bmap" for constraints on the argument
2613 * of the integer division and use them to construct constraints on the
2614 * integer division itself. These constraints can then be combined
2615 * during the Fourier-Motzkin elimination.
2616 * Note that it is only useful to introduce lower bounds on "div"
2617 * if "bmap" already contains upper bounds on "div" as the newly
2618 * introduce lower bounds can then be combined with the pre-existing
2619 * upper bounds. Similarly for upper bounds.
2620 * We therefore first check if "bmap" contains any lower and/or upper bounds
2621 * on "div".
2623 * It is interesting to note that the introduction of these constraints
2624 * can indeed lead to more accurate results, even when compared to
2625 * deriving constraints on the argument of "div" from constraints on "div".
2626 * Consider, for example, the set
2628 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2630 * The second constraint can be rewritten as
2632 * 2 * [(-i-2j+3)/4] + k >= 0
2634 * from which we can derive
2636 * -i - 2j + 3 >= -2k
2638 * or
2640 * i + 2j <= 3 + 2k
2642 * Combined with the first constraint, we obtain
2644 * -3 <= 3 + 2k or k >= -3
2646 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2647 * the first constraint, we obtain
2649 * [(i + 2j)/4] >= [-3/4] = -1
2651 * Combining this constraint with the second constraint, we obtain
2653 * k >= -2
2655 static __isl_give isl_basic_map *insert_bounds_on_div(
2656 __isl_take isl_basic_map *bmap, int div)
2658 int i;
2659 int check_lb, check_ub;
2660 isl_int v;
2661 unsigned total;
2663 if (!bmap)
2664 return NULL;
2666 if (isl_int_is_zero(bmap->div[div][0]))
2667 return bmap;
2669 total = isl_space_dim(bmap->dim, isl_dim_all);
2671 check_lb = 0;
2672 check_ub = 0;
2673 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2674 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2675 if (s > 0)
2676 check_ub = 1;
2677 if (s < 0)
2678 check_lb = 1;
2681 if (!check_lb && !check_ub)
2682 return bmap;
2684 isl_int_init(v);
2686 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2687 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2688 continue;
2690 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2691 check_lb, check_ub);
2694 isl_int_clear(v);
2696 return bmap;
2699 /* Remove all divs (recursively) involving any of the given dimensions
2700 * in their definitions.
2702 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2703 __isl_take isl_basic_map *bmap,
2704 enum isl_dim_type type, unsigned first, unsigned n)
2706 int i;
2708 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2709 return isl_basic_map_free(bmap);
2710 first += isl_basic_map_offset(bmap, type);
2712 for (i = bmap->n_div - 1; i >= 0; --i) {
2713 isl_bool involves;
2715 involves = div_involves_vars(bmap, i, first, n);
2716 if (involves < 0)
2717 return isl_basic_map_free(bmap);
2718 if (!involves)
2719 continue;
2720 bmap = insert_bounds_on_div(bmap, i);
2721 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2722 if (!bmap)
2723 return NULL;
2724 i = bmap->n_div;
2727 return bmap;
2730 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2731 __isl_take isl_basic_set *bset,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2734 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2737 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2738 enum isl_dim_type type, unsigned first, unsigned n)
2740 int i;
2742 if (!map)
2743 return NULL;
2744 if (map->n == 0)
2745 return map;
2747 map = isl_map_cow(map);
2748 if (!map)
2749 return NULL;
2751 for (i = 0; i < map->n; ++i) {
2752 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2753 type, first, n);
2754 if (!map->p[i])
2755 goto error;
2757 return map;
2758 error:
2759 isl_map_free(map);
2760 return NULL;
2763 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2764 enum isl_dim_type type, unsigned first, unsigned n)
2766 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2767 type, first, n));
2770 /* Does the description of "bmap" depend on the specified dimensions?
2771 * We also check whether the dimensions appear in any of the div definitions.
2772 * In principle there is no need for this check. If the dimensions appear
2773 * in a div definition, they also appear in the defining constraints of that
2774 * div.
2776 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2777 enum isl_dim_type type, unsigned first, unsigned n)
2779 int i;
2781 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2782 return isl_bool_error;
2784 first += isl_basic_map_offset(bmap, type);
2785 for (i = 0; i < bmap->n_eq; ++i)
2786 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2787 return isl_bool_true;
2788 for (i = 0; i < bmap->n_ineq; ++i)
2789 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2790 return isl_bool_true;
2791 for (i = 0; i < bmap->n_div; ++i) {
2792 if (isl_int_is_zero(bmap->div[i][0]))
2793 continue;
2794 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2795 return isl_bool_true;
2798 return isl_bool_false;
2801 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2802 enum isl_dim_type type, unsigned first, unsigned n)
2804 int i;
2806 if (isl_map_check_range(map, type, first, n) < 0)
2807 return isl_bool_error;
2809 for (i = 0; i < map->n; ++i) {
2810 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2811 type, first, n);
2812 if (involves < 0 || involves)
2813 return involves;
2816 return isl_bool_false;
2819 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2820 enum isl_dim_type type, unsigned first, unsigned n)
2822 return isl_basic_map_involves_dims(bset, type, first, n);
2825 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2826 enum isl_dim_type type, unsigned first, unsigned n)
2828 return isl_map_involves_dims(set, type, first, n);
2831 /* Drop all constraints in bmap that involve any of the dimensions
2832 * first to first+n-1.
2833 * This function only performs the actual removal of constraints.
2835 * This function should not call finalize since it is used by
2836 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2838 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2839 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2841 int i;
2843 if (n == 0)
2844 return bmap;
2846 bmap = isl_basic_map_cow(bmap);
2848 if (!bmap)
2849 return NULL;
2851 for (i = bmap->n_eq - 1; i >= 0; --i) {
2852 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2853 continue;
2854 isl_basic_map_drop_equality(bmap, i);
2857 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2858 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2859 continue;
2860 isl_basic_map_drop_inequality(bmap, i);
2863 return bmap;
2866 /* Drop all constraints in bset that involve any of the dimensions
2867 * first to first+n-1.
2868 * This function only performs the actual removal of constraints.
2870 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2871 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2873 return isl_basic_map_drop_constraints_involving(bset, first, n);
2876 /* Drop all constraints in bmap that do not involve any of the dimensions
2877 * first to first + n - 1 of the given type.
2879 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2880 __isl_take isl_basic_map *bmap,
2881 enum isl_dim_type type, unsigned first, unsigned n)
2883 int i;
2885 if (n == 0) {
2886 isl_space *space = isl_basic_map_get_space(bmap);
2887 isl_basic_map_free(bmap);
2888 return isl_basic_map_universe(space);
2890 bmap = isl_basic_map_cow(bmap);
2891 if (!bmap)
2892 return NULL;
2894 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2895 return isl_basic_map_free(bmap);
2897 first += isl_basic_map_offset(bmap, type) - 1;
2899 for (i = bmap->n_eq - 1; i >= 0; --i) {
2900 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2901 continue;
2902 isl_basic_map_drop_equality(bmap, i);
2905 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2906 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2907 continue;
2908 isl_basic_map_drop_inequality(bmap, i);
2911 bmap = isl_basic_map_add_known_div_constraints(bmap);
2912 return bmap;
2915 /* Drop all constraints in bset that do not involve any of the dimensions
2916 * first to first + n - 1 of the given type.
2918 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2919 __isl_take isl_basic_set *bset,
2920 enum isl_dim_type type, unsigned first, unsigned n)
2922 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2923 type, first, n);
2926 /* Drop all constraints in bmap that involve any of the dimensions
2927 * first to first + n - 1 of the given type.
2929 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2930 __isl_take isl_basic_map *bmap,
2931 enum isl_dim_type type, unsigned first, unsigned n)
2933 if (!bmap)
2934 return NULL;
2935 if (n == 0)
2936 return bmap;
2938 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2939 return isl_basic_map_free(bmap);
2941 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2942 first += isl_basic_map_offset(bmap, type) - 1;
2943 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2944 bmap = isl_basic_map_add_known_div_constraints(bmap);
2945 return bmap;
2948 /* Drop all constraints in bset that involve any of the dimensions
2949 * first to first + n - 1 of the given type.
2951 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2952 __isl_take isl_basic_set *bset,
2953 enum isl_dim_type type, unsigned first, unsigned n)
2955 return isl_basic_map_drop_constraints_involving_dims(bset,
2956 type, first, n);
2959 /* Drop constraints from "map" by applying "drop" to each basic map.
2961 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2962 enum isl_dim_type type, unsigned first, unsigned n,
2963 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2964 enum isl_dim_type type, unsigned first, unsigned n))
2966 int i;
2968 if (isl_map_check_range(map, type, first, n) < 0)
2969 return isl_map_free(map);
2971 map = isl_map_cow(map);
2972 if (!map)
2973 return NULL;
2975 for (i = 0; i < map->n; ++i) {
2976 map->p[i] = drop(map->p[i], type, first, n);
2977 if (!map->p[i])
2978 return isl_map_free(map);
2981 if (map->n > 1)
2982 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2984 return map;
2987 /* Drop all constraints in map that involve any of the dimensions
2988 * first to first + n - 1 of the given type.
2990 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2991 __isl_take isl_map *map,
2992 enum isl_dim_type type, unsigned first, unsigned n)
2994 if (n == 0)
2995 return map;
2996 return drop_constraints(map, type, first, n,
2997 &isl_basic_map_drop_constraints_involving_dims);
3000 /* Drop all constraints in "map" that do not involve any of the dimensions
3001 * first to first + n - 1 of the given type.
3003 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3004 __isl_take isl_map *map,
3005 enum isl_dim_type type, unsigned first, unsigned n)
3007 if (n == 0) {
3008 isl_space *space = isl_map_get_space(map);
3009 isl_map_free(map);
3010 return isl_map_universe(space);
3012 return drop_constraints(map, type, first, n,
3013 &isl_basic_map_drop_constraints_not_involving_dims);
3016 /* Drop all constraints in set that involve any of the dimensions
3017 * first to first + n - 1 of the given type.
3019 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3020 __isl_take isl_set *set,
3021 enum isl_dim_type type, unsigned first, unsigned n)
3023 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3026 /* Drop all constraints in "set" that do not involve any of the dimensions
3027 * first to first + n - 1 of the given type.
3029 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3030 __isl_take isl_set *set,
3031 enum isl_dim_type type, unsigned first, unsigned n)
3033 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3036 /* Does local variable "div" of "bmap" have a complete explicit representation?
3037 * Having a complete explicit representation requires not only
3038 * an explicit representation, but also that all local variables
3039 * that appear in this explicit representation in turn have
3040 * a complete explicit representation.
3042 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3044 int i;
3045 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3046 isl_bool marked;
3048 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3049 if (marked < 0 || marked)
3050 return isl_bool_not(marked);
3052 for (i = bmap->n_div - 1; i >= 0; --i) {
3053 isl_bool known;
3055 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3056 continue;
3057 known = isl_basic_map_div_is_known(bmap, i);
3058 if (known < 0 || !known)
3059 return known;
3062 return isl_bool_true;
3065 /* Remove all divs that are unknown or defined in terms of unknown divs.
3067 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3068 __isl_take isl_basic_map *bmap)
3070 int i;
3072 if (!bmap)
3073 return NULL;
3075 for (i = bmap->n_div - 1; i >= 0; --i) {
3076 if (isl_basic_map_div_is_known(bmap, i))
3077 continue;
3078 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3079 if (!bmap)
3080 return NULL;
3081 i = bmap->n_div;
3084 return bmap;
3087 /* Remove all divs that are unknown or defined in terms of unknown divs.
3089 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3090 __isl_take isl_basic_set *bset)
3092 return isl_basic_map_remove_unknown_divs(bset);
3095 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3097 int i;
3099 if (!map)
3100 return NULL;
3101 if (map->n == 0)
3102 return map;
3104 map = isl_map_cow(map);
3105 if (!map)
3106 return NULL;
3108 for (i = 0; i < map->n; ++i) {
3109 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3110 if (!map->p[i])
3111 goto error;
3113 return map;
3114 error:
3115 isl_map_free(map);
3116 return NULL;
3119 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3121 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3124 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3125 __isl_take isl_basic_set *bset,
3126 enum isl_dim_type type, unsigned first, unsigned n)
3128 isl_basic_map *bmap = bset_to_bmap(bset);
3129 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3130 return bset_from_bmap(bmap);
3133 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3134 enum isl_dim_type type, unsigned first, unsigned n)
3136 int i;
3138 if (n == 0)
3139 return map;
3141 map = isl_map_cow(map);
3142 if (isl_map_check_range(map, type, first, n) < 0)
3143 return isl_map_free(map);
3145 for (i = 0; i < map->n; ++i) {
3146 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3147 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3148 if (!map->p[i])
3149 goto error;
3151 map = isl_map_drop(map, type, first, n);
3152 return map;
3153 error:
3154 isl_map_free(map);
3155 return NULL;
3158 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3159 enum isl_dim_type type, unsigned first, unsigned n)
3161 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3162 type, first, n));
3165 /* Project out n inputs starting at first using Fourier-Motzkin */
3166 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3167 unsigned first, unsigned n)
3169 return isl_map_remove_dims(map, isl_dim_in, first, n);
3172 static void dump_term(struct isl_basic_map *bmap,
3173 isl_int c, int pos, FILE *out)
3175 const char *name;
3176 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3177 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3178 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3179 if (!pos)
3180 isl_int_print(out, c, 0);
3181 else {
3182 if (!isl_int_is_one(c))
3183 isl_int_print(out, c, 0);
3184 if (pos < 1 + nparam) {
3185 name = isl_space_get_dim_name(bmap->dim,
3186 isl_dim_param, pos - 1);
3187 if (name)
3188 fprintf(out, "%s", name);
3189 else
3190 fprintf(out, "p%d", pos - 1);
3191 } else if (pos < 1 + nparam + in)
3192 fprintf(out, "i%d", pos - 1 - nparam);
3193 else if (pos < 1 + nparam + dim)
3194 fprintf(out, "o%d", pos - 1 - nparam - in);
3195 else
3196 fprintf(out, "e%d", pos - 1 - nparam - dim);
3200 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3201 int sign, FILE *out)
3203 int i;
3204 int first;
3205 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3206 isl_int v;
3208 isl_int_init(v);
3209 for (i = 0, first = 1; i < len; ++i) {
3210 if (isl_int_sgn(c[i]) * sign <= 0)
3211 continue;
3212 if (!first)
3213 fprintf(out, " + ");
3214 first = 0;
3215 isl_int_abs(v, c[i]);
3216 dump_term(bmap, v, i, out);
3218 isl_int_clear(v);
3219 if (first)
3220 fprintf(out, "0");
3223 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3224 const char *op, FILE *out, int indent)
3226 int i;
3228 fprintf(out, "%*s", indent, "");
3230 dump_constraint_sign(bmap, c, 1, out);
3231 fprintf(out, " %s ", op);
3232 dump_constraint_sign(bmap, c, -1, out);
3234 fprintf(out, "\n");
3236 for (i = bmap->n_div; i < bmap->extra; ++i) {
3237 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3238 continue;
3239 fprintf(out, "%*s", indent, "");
3240 fprintf(out, "ERROR: unused div coefficient not zero\n");
3241 abort();
3245 static void dump_constraints(struct isl_basic_map *bmap,
3246 isl_int **c, unsigned n,
3247 const char *op, FILE *out, int indent)
3249 int i;
3251 for (i = 0; i < n; ++i)
3252 dump_constraint(bmap, c[i], op, out, indent);
3255 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3257 int j;
3258 int first = 1;
3259 unsigned total = isl_basic_map_total_dim(bmap);
3261 for (j = 0; j < 1 + total; ++j) {
3262 if (isl_int_is_zero(exp[j]))
3263 continue;
3264 if (!first && isl_int_is_pos(exp[j]))
3265 fprintf(out, "+");
3266 dump_term(bmap, exp[j], j, out);
3267 first = 0;
3271 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3273 int i;
3275 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3276 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3278 for (i = 0; i < bmap->n_div; ++i) {
3279 fprintf(out, "%*s", indent, "");
3280 fprintf(out, "e%d = [(", i);
3281 dump_affine(bmap, bmap->div[i]+1, out);
3282 fprintf(out, ")/");
3283 isl_int_print(out, bmap->div[i][0], 0);
3284 fprintf(out, "]\n");
3288 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3289 FILE *out, int indent)
3291 if (!bset) {
3292 fprintf(out, "null basic set\n");
3293 return;
3296 fprintf(out, "%*s", indent, "");
3297 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3298 bset->ref, bset->dim->nparam, bset->dim->n_out,
3299 bset->extra, bset->flags);
3300 dump(bset_to_bmap(bset), out, indent);
3303 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3304 FILE *out, int indent)
3306 if (!bmap) {
3307 fprintf(out, "null basic map\n");
3308 return;
3311 fprintf(out, "%*s", indent, "");
3312 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3313 "flags: %x, n_name: %d\n",
3314 bmap->ref,
3315 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3316 bmap->extra, bmap->flags, bmap->dim->n_id);
3317 dump(bmap, out, indent);
3320 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3321 unsigned pos)
3323 unsigned total;
3325 if (!bmap)
3326 return NULL;
3327 total = isl_basic_map_total_dim(bmap);
3328 if (pos >= bmap->n_ineq)
3329 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3330 "invalid position", return isl_basic_map_free(bmap));
3331 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3332 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3333 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3334 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3335 return bmap;
3338 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3339 unsigned flags)
3341 if (isl_space_check_is_set(space) < 0)
3342 goto error;
3343 return isl_map_alloc_space(space, n, flags);
3344 error:
3345 isl_space_free(space);
3346 return NULL;
3349 /* Make sure "map" has room for at least "n" more basic maps.
3351 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3353 int i;
3354 struct isl_map *grown = NULL;
3356 if (!map)
3357 return NULL;
3358 isl_assert(map->ctx, n >= 0, goto error);
3359 if (map->n + n <= map->size)
3360 return map;
3361 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3362 if (!grown)
3363 goto error;
3364 for (i = 0; i < map->n; ++i) {
3365 grown->p[i] = isl_basic_map_copy(map->p[i]);
3366 if (!grown->p[i])
3367 goto error;
3368 grown->n++;
3370 isl_map_free(map);
3371 return grown;
3372 error:
3373 isl_map_free(grown);
3374 isl_map_free(map);
3375 return NULL;
3378 /* Make sure "set" has room for at least "n" more basic sets.
3380 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3382 return set_from_map(isl_map_grow(set_to_map(set), n));
3385 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3387 return isl_map_from_basic_map(bset);
3390 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3392 struct isl_map *map;
3394 if (!bmap)
3395 return NULL;
3397 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3398 return isl_map_add_basic_map(map, bmap);
3401 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3402 __isl_take isl_basic_set *bset)
3404 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3405 bset_to_bmap(bset)));
3408 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3410 return isl_map_free(set);
3413 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3415 int i;
3417 if (!set) {
3418 fprintf(out, "null set\n");
3419 return;
3422 fprintf(out, "%*s", indent, "");
3423 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3424 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3425 set->flags);
3426 for (i = 0; i < set->n; ++i) {
3427 fprintf(out, "%*s", indent, "");
3428 fprintf(out, "basic set %d:\n", i);
3429 isl_basic_set_print_internal(set->p[i], out, indent+4);
3433 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3435 int i;
3437 if (!map) {
3438 fprintf(out, "null map\n");
3439 return;
3442 fprintf(out, "%*s", indent, "");
3443 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3444 "flags: %x, n_name: %d\n",
3445 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3446 map->dim->n_out, map->flags, map->dim->n_id);
3447 for (i = 0; i < map->n; ++i) {
3448 fprintf(out, "%*s", indent, "");
3449 fprintf(out, "basic map %d:\n", i);
3450 isl_basic_map_print_internal(map->p[i], out, indent+4);
3454 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3455 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3457 struct isl_basic_map *bmap_domain;
3459 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3460 goto error;
3462 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3463 isl_assert(bset->ctx,
3464 isl_basic_map_compatible_domain(bmap, bset), goto error);
3466 bmap = isl_basic_map_cow(bmap);
3467 if (!bmap)
3468 goto error;
3469 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3470 bset->n_div, bset->n_eq, bset->n_ineq);
3471 bmap_domain = isl_basic_map_from_domain(bset);
3472 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3474 bmap = isl_basic_map_simplify(bmap);
3475 return isl_basic_map_finalize(bmap);
3476 error:
3477 isl_basic_map_free(bmap);
3478 isl_basic_set_free(bset);
3479 return NULL;
3482 /* Check that the space of "bset" is the same as that of the range of "bmap".
3484 static isl_stat isl_basic_map_check_compatible_range(
3485 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3487 isl_bool ok;
3489 ok = isl_basic_map_compatible_range(bmap, bset);
3490 if (ok < 0)
3491 return isl_stat_error;
3492 if (!ok)
3493 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3494 "incompatible spaces", return isl_stat_error);
3496 return isl_stat_ok;
3499 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3500 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3502 struct isl_basic_map *bmap_range;
3504 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3505 goto error;
3507 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3508 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3509 goto error;
3511 if (isl_basic_set_plain_is_universe(bset)) {
3512 isl_basic_set_free(bset);
3513 return bmap;
3516 bmap = isl_basic_map_cow(bmap);
3517 if (!bmap)
3518 goto error;
3519 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3520 bset->n_div, bset->n_eq, bset->n_ineq);
3521 bmap_range = bset_to_bmap(bset);
3522 bmap = add_constraints(bmap, bmap_range, 0, 0);
3524 bmap = isl_basic_map_simplify(bmap);
3525 return isl_basic_map_finalize(bmap);
3526 error:
3527 isl_basic_map_free(bmap);
3528 isl_basic_set_free(bset);
3529 return NULL;
3532 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3533 __isl_keep isl_vec *vec)
3535 int i;
3536 unsigned total;
3537 isl_int s;
3539 if (!bmap || !vec)
3540 return isl_bool_error;
3542 total = 1 + isl_basic_map_total_dim(bmap);
3543 if (total != vec->size)
3544 return isl_bool_false;
3546 isl_int_init(s);
3548 for (i = 0; i < bmap->n_eq; ++i) {
3549 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3550 if (!isl_int_is_zero(s)) {
3551 isl_int_clear(s);
3552 return isl_bool_false;
3556 for (i = 0; i < bmap->n_ineq; ++i) {
3557 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3558 if (isl_int_is_neg(s)) {
3559 isl_int_clear(s);
3560 return isl_bool_false;
3564 isl_int_clear(s);
3566 return isl_bool_true;
3569 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3570 __isl_keep isl_vec *vec)
3572 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3575 __isl_give isl_basic_map *isl_basic_map_intersect(
3576 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3578 struct isl_vec *sample = NULL;
3580 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3581 goto error;
3582 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3583 isl_space_dim(bmap1->dim, isl_dim_param) &&
3584 isl_space_dim(bmap2->dim, isl_dim_all) !=
3585 isl_space_dim(bmap2->dim, isl_dim_param))
3586 return isl_basic_map_intersect(bmap2, bmap1);
3588 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3589 isl_space_dim(bmap2->dim, isl_dim_param))
3590 isl_assert(bmap1->ctx,
3591 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3593 if (isl_basic_map_plain_is_empty(bmap1)) {
3594 isl_basic_map_free(bmap2);
3595 return bmap1;
3597 if (isl_basic_map_plain_is_empty(bmap2)) {
3598 isl_basic_map_free(bmap1);
3599 return bmap2;
3602 if (bmap1->sample &&
3603 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3604 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3605 sample = isl_vec_copy(bmap1->sample);
3606 else if (bmap2->sample &&
3607 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3608 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3609 sample = isl_vec_copy(bmap2->sample);
3611 bmap1 = isl_basic_map_cow(bmap1);
3612 if (!bmap1)
3613 goto error;
3614 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3615 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3616 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3618 if (!bmap1)
3619 isl_vec_free(sample);
3620 else if (sample) {
3621 isl_vec_free(bmap1->sample);
3622 bmap1->sample = sample;
3625 bmap1 = isl_basic_map_simplify(bmap1);
3626 return isl_basic_map_finalize(bmap1);
3627 error:
3628 if (sample)
3629 isl_vec_free(sample);
3630 isl_basic_map_free(bmap1);
3631 isl_basic_map_free(bmap2);
3632 return NULL;
3635 struct isl_basic_set *isl_basic_set_intersect(
3636 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3638 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3639 bset_to_bmap(bset2)));
3642 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3643 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3645 return isl_basic_set_intersect(bset1, bset2);
3648 /* Special case of isl_map_intersect, where both map1 and map2
3649 * are convex, without any divs and such that either map1 or map2
3650 * contains a single constraint. This constraint is then simply
3651 * added to the other map.
3653 static __isl_give isl_map *map_intersect_add_constraint(
3654 __isl_take isl_map *map1, __isl_take isl_map *map2)
3656 isl_assert(map1->ctx, map1->n == 1, goto error);
3657 isl_assert(map2->ctx, map1->n == 1, goto error);
3658 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3659 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3661 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3662 return isl_map_intersect(map2, map1);
3664 map1 = isl_map_cow(map1);
3665 if (!map1)
3666 goto error;
3667 if (isl_map_plain_is_empty(map1)) {
3668 isl_map_free(map2);
3669 return map1;
3671 if (map2->p[0]->n_eq == 1)
3672 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3673 else
3674 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3675 map2->p[0]->ineq[0]);
3677 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3678 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3679 if (!map1->p[0])
3680 goto error;
3682 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3683 isl_basic_map_free(map1->p[0]);
3684 map1->n = 0;
3687 isl_map_free(map2);
3689 map1 = isl_map_unmark_normalized(map1);
3690 return map1;
3691 error:
3692 isl_map_free(map1);
3693 isl_map_free(map2);
3694 return NULL;
3697 /* map2 may be either a parameter domain or a map living in the same
3698 * space as map1.
3700 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3701 __isl_take isl_map *map2)
3703 unsigned flags = 0;
3704 isl_bool equal;
3705 isl_map *result;
3706 int i, j;
3708 if (!map1 || !map2)
3709 goto error;
3711 if ((isl_map_plain_is_empty(map1) ||
3712 isl_map_plain_is_universe(map2)) &&
3713 isl_space_is_equal(map1->dim, map2->dim)) {
3714 isl_map_free(map2);
3715 return map1;
3717 if ((isl_map_plain_is_empty(map2) ||
3718 isl_map_plain_is_universe(map1)) &&
3719 isl_space_is_equal(map1->dim, map2->dim)) {
3720 isl_map_free(map1);
3721 return map2;
3724 if (map1->n == 1 && map2->n == 1 &&
3725 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3726 isl_space_is_equal(map1->dim, map2->dim) &&
3727 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3728 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3729 return map_intersect_add_constraint(map1, map2);
3731 equal = isl_map_plain_is_equal(map1, map2);
3732 if (equal < 0)
3733 goto error;
3734 if (equal) {
3735 isl_map_free(map2);
3736 return map1;
3739 if (isl_space_dim(map2->dim, isl_dim_all) !=
3740 isl_space_dim(map2->dim, isl_dim_param))
3741 isl_assert(map1->ctx,
3742 isl_space_is_equal(map1->dim, map2->dim), goto error);
3744 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3745 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3746 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3748 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3749 map1->n * map2->n, flags);
3750 if (!result)
3751 goto error;
3752 for (i = 0; i < map1->n; ++i)
3753 for (j = 0; j < map2->n; ++j) {
3754 struct isl_basic_map *part;
3755 part = isl_basic_map_intersect(
3756 isl_basic_map_copy(map1->p[i]),
3757 isl_basic_map_copy(map2->p[j]));
3758 if (isl_basic_map_is_empty(part) < 0)
3759 part = isl_basic_map_free(part);
3760 result = isl_map_add_basic_map(result, part);
3761 if (!result)
3762 goto error;
3764 isl_map_free(map1);
3765 isl_map_free(map2);
3766 return result;
3767 error:
3768 isl_map_free(map1);
3769 isl_map_free(map2);
3770 return NULL;
3773 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3774 __isl_take isl_map *map2)
3776 if (!map1 || !map2)
3777 goto error;
3778 if (!isl_space_is_equal(map1->dim, map2->dim))
3779 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3780 "spaces don't match", goto error);
3781 return map_intersect_internal(map1, map2);
3782 error:
3783 isl_map_free(map1);
3784 isl_map_free(map2);
3785 return NULL;
3788 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3789 __isl_take isl_map *map2)
3791 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3794 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3796 return set_from_map(isl_map_intersect(set_to_map(set1),
3797 set_to_map(set2)));
3800 /* map_intersect_internal accepts intersections
3801 * with parameter domains, so we can just call that function.
3803 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3804 __isl_take isl_set *params)
3806 return map_intersect_internal(map, params);
3809 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3810 __isl_take isl_map *map2)
3812 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3815 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3816 __isl_take isl_set *params)
3818 return isl_map_intersect_params(set, params);
3821 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3823 isl_space *space;
3824 unsigned pos, n1, n2;
3826 if (!bmap)
3827 return NULL;
3828 bmap = isl_basic_map_cow(bmap);
3829 if (!bmap)
3830 return NULL;
3831 space = isl_space_reverse(isl_space_copy(bmap->dim));
3832 pos = isl_basic_map_offset(bmap, isl_dim_in);
3833 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3834 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3835 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3836 return isl_basic_map_reset_space(bmap, space);
3839 static __isl_give isl_basic_map *basic_map_space_reset(
3840 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3842 isl_space *space;
3844 if (!bmap)
3845 return NULL;
3846 if (!isl_space_is_named_or_nested(bmap->dim, type))
3847 return bmap;
3849 space = isl_basic_map_get_space(bmap);
3850 space = isl_space_reset(space, type);
3851 bmap = isl_basic_map_reset_space(bmap, space);
3852 return bmap;
3855 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3856 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3857 unsigned pos, unsigned n)
3859 isl_bool rational, is_empty;
3860 isl_space *res_space;
3861 struct isl_basic_map *res;
3862 struct isl_dim_map *dim_map;
3863 unsigned total, off;
3864 enum isl_dim_type t;
3866 if (n == 0)
3867 return basic_map_space_reset(bmap, type);
3869 is_empty = isl_basic_map_plain_is_empty(bmap);
3870 if (is_empty < 0)
3871 return isl_basic_map_free(bmap);
3872 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3873 type, pos, n);
3874 if (!res_space)
3875 return isl_basic_map_free(bmap);
3876 if (is_empty) {
3877 isl_basic_map_free(bmap);
3878 return isl_basic_map_empty(res_space);
3881 total = isl_basic_map_total_dim(bmap) + n;
3882 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3883 off = 0;
3884 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3885 if (t != type) {
3886 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3887 } else {
3888 unsigned size = isl_basic_map_dim(bmap, t);
3889 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3890 0, pos, off);
3891 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3892 pos, size - pos, off + pos + n);
3894 off += isl_space_dim(res_space, t);
3896 isl_dim_map_div(dim_map, bmap, off);
3898 res = isl_basic_map_alloc_space(res_space,
3899 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3900 rational = isl_basic_map_is_rational(bmap);
3901 if (rational < 0)
3902 res = isl_basic_map_free(res);
3903 if (rational)
3904 res = isl_basic_map_set_rational(res);
3905 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3906 return isl_basic_map_finalize(res);
3909 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3910 __isl_take isl_basic_set *bset,
3911 enum isl_dim_type type, unsigned pos, unsigned n)
3913 return isl_basic_map_insert_dims(bset, type, pos, n);
3916 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3917 enum isl_dim_type type, unsigned n)
3919 if (!bmap)
3920 return NULL;
3921 return isl_basic_map_insert_dims(bmap, type,
3922 isl_basic_map_dim(bmap, type), n);
3925 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3926 enum isl_dim_type type, unsigned n)
3928 if (!bset)
3929 return NULL;
3930 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3931 return isl_basic_map_add_dims(bset, type, n);
3932 error:
3933 isl_basic_set_free(bset);
3934 return NULL;
3937 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3938 enum isl_dim_type type)
3940 isl_space *space;
3942 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3943 return map;
3945 space = isl_map_get_space(map);
3946 space = isl_space_reset(space, type);
3947 map = isl_map_reset_space(map, space);
3948 return map;
3951 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3952 enum isl_dim_type type, unsigned pos, unsigned n)
3954 int i;
3956 if (n == 0)
3957 return map_space_reset(map, type);
3959 map = isl_map_cow(map);
3960 if (!map)
3961 return NULL;
3963 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3964 if (!map->dim)
3965 goto error;
3967 for (i = 0; i < map->n; ++i) {
3968 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3969 if (!map->p[i])
3970 goto error;
3973 return map;
3974 error:
3975 isl_map_free(map);
3976 return NULL;
3979 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3980 enum isl_dim_type type, unsigned pos, unsigned n)
3982 return isl_map_insert_dims(set, type, pos, n);
3985 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3986 enum isl_dim_type type, unsigned n)
3988 if (!map)
3989 return NULL;
3990 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3993 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3994 enum isl_dim_type type, unsigned n)
3996 if (!set)
3997 return NULL;
3998 isl_assert(set->ctx, type != isl_dim_in, goto error);
3999 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4000 error:
4001 isl_set_free(set);
4002 return NULL;
4005 __isl_give isl_basic_map *isl_basic_map_move_dims(
4006 __isl_take isl_basic_map *bmap,
4007 enum isl_dim_type dst_type, unsigned dst_pos,
4008 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4010 struct isl_dim_map *dim_map;
4011 struct isl_basic_map *res;
4012 enum isl_dim_type t;
4013 unsigned total, off;
4015 if (!bmap)
4016 return NULL;
4017 if (n == 0) {
4018 bmap = isl_basic_map_reset(bmap, src_type);
4019 bmap = isl_basic_map_reset(bmap, dst_type);
4020 return bmap;
4023 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4024 return isl_basic_map_free(bmap);
4026 if (dst_type == src_type && dst_pos == src_pos)
4027 return bmap;
4029 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4031 if (pos(bmap->dim, dst_type) + dst_pos ==
4032 pos(bmap->dim, src_type) + src_pos +
4033 ((src_type < dst_type) ? n : 0)) {
4034 bmap = isl_basic_map_cow(bmap);
4035 if (!bmap)
4036 return NULL;
4038 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4039 src_type, src_pos, n);
4040 if (!bmap->dim)
4041 goto error;
4043 bmap = isl_basic_map_finalize(bmap);
4045 return bmap;
4048 total = isl_basic_map_total_dim(bmap);
4049 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4051 off = 0;
4052 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4053 unsigned size = isl_space_dim(bmap->dim, t);
4054 if (t == dst_type) {
4055 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4056 0, dst_pos, off);
4057 off += dst_pos;
4058 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4059 src_pos, n, off);
4060 off += n;
4061 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4062 dst_pos, size - dst_pos, off);
4063 off += size - dst_pos;
4064 } else if (t == src_type) {
4065 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4066 0, src_pos, off);
4067 off += src_pos;
4068 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4069 src_pos + n, size - src_pos - n, off);
4070 off += size - src_pos - n;
4071 } else {
4072 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4073 off += size;
4076 isl_dim_map_div(dim_map, bmap, off);
4078 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4079 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4080 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4081 if (!bmap)
4082 goto error;
4084 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4085 src_type, src_pos, n);
4086 if (!bmap->dim)
4087 goto error;
4089 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4090 bmap = isl_basic_map_gauss(bmap, NULL);
4091 bmap = isl_basic_map_finalize(bmap);
4093 return bmap;
4094 error:
4095 isl_basic_map_free(bmap);
4096 return NULL;
4099 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4100 enum isl_dim_type dst_type, unsigned dst_pos,
4101 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4103 isl_basic_map *bmap = bset_to_bmap(bset);
4104 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4105 src_type, src_pos, n);
4106 return bset_from_bmap(bmap);
4109 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4110 enum isl_dim_type dst_type, unsigned dst_pos,
4111 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4113 if (!set)
4114 return NULL;
4115 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4116 return set_from_map(isl_map_move_dims(set_to_map(set),
4117 dst_type, dst_pos, src_type, src_pos, n));
4118 error:
4119 isl_set_free(set);
4120 return NULL;
4123 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4124 enum isl_dim_type dst_type, unsigned dst_pos,
4125 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4127 int i;
4129 if (n == 0) {
4130 map = isl_map_reset(map, src_type);
4131 map = isl_map_reset(map, dst_type);
4132 return map;
4135 if (isl_map_check_range(map, src_type, src_pos, n))
4136 return isl_map_free(map);
4138 if (dst_type == src_type && dst_pos == src_pos)
4139 return map;
4141 isl_assert(map->ctx, dst_type != src_type, goto error);
4143 map = isl_map_cow(map);
4144 if (!map)
4145 return NULL;
4147 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4148 if (!map->dim)
4149 goto error;
4151 for (i = 0; i < map->n; ++i) {
4152 map->p[i] = isl_basic_map_move_dims(map->p[i],
4153 dst_type, dst_pos,
4154 src_type, src_pos, n);
4155 if (!map->p[i])
4156 goto error;
4159 return map;
4160 error:
4161 isl_map_free(map);
4162 return NULL;
4165 /* Move the specified dimensions to the last columns right before
4166 * the divs. Don't change the dimension specification of bmap.
4167 * That's the responsibility of the caller.
4169 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4170 enum isl_dim_type type, unsigned first, unsigned n)
4172 struct isl_dim_map *dim_map;
4173 struct isl_basic_map *res;
4174 enum isl_dim_type t;
4175 unsigned total, off;
4177 if (!bmap)
4178 return NULL;
4179 if (pos(bmap->dim, type) + first + n ==
4180 1 + isl_space_dim(bmap->dim, isl_dim_all))
4181 return bmap;
4183 total = isl_basic_map_total_dim(bmap);
4184 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4186 off = 0;
4187 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4188 unsigned size = isl_space_dim(bmap->dim, t);
4189 if (t == type) {
4190 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4191 0, first, off);
4192 off += first;
4193 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4194 first, n, total - bmap->n_div - n);
4195 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4196 first + n, size - (first + n), off);
4197 off += size - (first + n);
4198 } else {
4199 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4200 off += size;
4203 isl_dim_map_div(dim_map, bmap, off + n);
4205 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4206 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4207 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4208 return res;
4211 /* Insert "n" rows in the divs of "bmap".
4213 * The number of columns is not changed, which means that the last
4214 * dimensions of "bmap" are being reintepreted as the new divs.
4215 * The space of "bmap" is not adjusted, however, which means
4216 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4217 * from the space of "bmap" is the responsibility of the caller.
4219 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4220 int n)
4222 int i;
4223 size_t row_size;
4224 isl_int **new_div;
4225 isl_int *old;
4227 bmap = isl_basic_map_cow(bmap);
4228 if (!bmap)
4229 return NULL;
4231 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4232 old = bmap->block2.data;
4233 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4234 (bmap->extra + n) * (1 + row_size));
4235 if (!bmap->block2.data)
4236 return isl_basic_map_free(bmap);
4237 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4238 if (!new_div)
4239 return isl_basic_map_free(bmap);
4240 for (i = 0; i < n; ++i) {
4241 new_div[i] = bmap->block2.data +
4242 (bmap->extra + i) * (1 + row_size);
4243 isl_seq_clr(new_div[i], 1 + row_size);
4245 for (i = 0; i < bmap->extra; ++i)
4246 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4247 free(bmap->div);
4248 bmap->div = new_div;
4249 bmap->n_div += n;
4250 bmap->extra += n;
4252 return bmap;
4255 /* Drop constraints from "bmap" that only involve the variables
4256 * of "type" in the range [first, first + n] that are not related
4257 * to any of the variables outside that interval.
4258 * These constraints cannot influence the values for the variables
4259 * outside the interval, except in case they cause "bmap" to be empty.
4260 * Only drop the constraints if "bmap" is known to be non-empty.
4262 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4263 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4264 unsigned first, unsigned n)
4266 int i;
4267 int *groups;
4268 unsigned dim, n_div;
4269 isl_bool non_empty;
4271 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4272 if (non_empty < 0)
4273 return isl_basic_map_free(bmap);
4274 if (!non_empty)
4275 return bmap;
4277 dim = isl_basic_map_dim(bmap, isl_dim_all);
4278 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4279 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4280 if (!groups)
4281 return isl_basic_map_free(bmap);
4282 first += isl_basic_map_offset(bmap, type) - 1;
4283 for (i = 0; i < first; ++i)
4284 groups[i] = -1;
4285 for (i = first + n; i < dim - n_div; ++i)
4286 groups[i] = -1;
4288 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4290 return bmap;
4293 /* Turn the n dimensions of type type, starting at first
4294 * into existentially quantified variables.
4296 * If a subset of the projected out variables are unrelated
4297 * to any of the variables that remain, then the constraints
4298 * involving this subset are simply dropped first.
4300 __isl_give isl_basic_map *isl_basic_map_project_out(
4301 __isl_take isl_basic_map *bmap,
4302 enum isl_dim_type type, unsigned first, unsigned n)
4304 isl_bool empty;
4306 if (n == 0)
4307 return basic_map_space_reset(bmap, type);
4308 if (type == isl_dim_div)
4309 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4310 "cannot project out existentially quantified variables",
4311 return isl_basic_map_free(bmap));
4313 empty = isl_basic_map_plain_is_empty(bmap);
4314 if (empty < 0)
4315 return isl_basic_map_free(bmap);
4316 if (empty)
4317 bmap = isl_basic_map_set_to_empty(bmap);
4319 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4320 if (!bmap)
4321 return NULL;
4323 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4324 return isl_basic_map_remove_dims(bmap, type, first, n);
4326 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4327 return isl_basic_map_free(bmap);
4329 bmap = move_last(bmap, type, first, n);
4330 bmap = isl_basic_map_cow(bmap);
4331 bmap = insert_div_rows(bmap, n);
4332 if (!bmap)
4333 return NULL;
4335 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4336 if (!bmap->dim)
4337 goto error;
4338 bmap = isl_basic_map_simplify(bmap);
4339 bmap = isl_basic_map_drop_redundant_divs(bmap);
4340 return isl_basic_map_finalize(bmap);
4341 error:
4342 isl_basic_map_free(bmap);
4343 return NULL;
4346 /* Turn the n dimensions of type type, starting at first
4347 * into existentially quantified variables.
4349 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4350 enum isl_dim_type type, unsigned first, unsigned n)
4352 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4353 type, first, n));
4356 /* Turn the n dimensions of type type, starting at first
4357 * into existentially quantified variables.
4359 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4360 enum isl_dim_type type, unsigned first, unsigned n)
4362 int i;
4364 if (n == 0)
4365 return map_space_reset(map, type);
4367 if (isl_map_check_range(map, type, first, n) < 0)
4368 return isl_map_free(map);
4370 map = isl_map_cow(map);
4371 if (!map)
4372 return NULL;
4374 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4375 if (!map->dim)
4376 goto error;
4378 for (i = 0; i < map->n; ++i) {
4379 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4380 if (!map->p[i])
4381 goto error;
4384 return map;
4385 error:
4386 isl_map_free(map);
4387 return NULL;
4390 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4391 * into existentially quantified variables.
4393 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4394 enum isl_dim_type type, unsigned first, unsigned n)
4396 unsigned dim;
4398 if (isl_map_check_range(map, type, first, n) < 0)
4399 return isl_map_free(map);
4400 dim = isl_map_dim(map, type);
4401 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4402 map = isl_map_project_out(map, type, 0, first);
4403 return map;
4406 /* Turn the n dimensions of type type, starting at first
4407 * into existentially quantified variables.
4409 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4410 enum isl_dim_type type, unsigned first, unsigned n)
4412 return set_from_map(isl_map_project_out(set_to_map(set),
4413 type, first, n));
4416 /* Return a map that projects the elements in "set" onto their
4417 * "n" set dimensions starting at "first".
4418 * "type" should be equal to isl_dim_set.
4420 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4421 enum isl_dim_type type, unsigned first, unsigned n)
4423 int i;
4424 isl_map *map;
4426 if (type != isl_dim_set)
4427 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4428 "only set dimensions can be projected out", goto error);
4429 if (isl_set_check_range(set, type, first, n) < 0)
4430 return isl_set_free(set);
4432 map = isl_map_from_domain(set);
4433 map = isl_map_add_dims(map, isl_dim_out, n);
4434 for (i = 0; i < n; ++i)
4435 map = isl_map_equate(map, isl_dim_in, first + i,
4436 isl_dim_out, i);
4437 return map;
4438 error:
4439 isl_set_free(set);
4440 return NULL;
4443 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4444 unsigned n)
4446 int i, j;
4448 for (i = 0; i < n; ++i) {
4449 j = isl_basic_map_alloc_div(bmap);
4450 if (j < 0)
4451 goto error;
4452 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4454 return bmap;
4455 error:
4456 isl_basic_map_free(bmap);
4457 return NULL;
4460 struct isl_basic_map *isl_basic_map_apply_range(
4461 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4463 isl_space *space_result = NULL;
4464 struct isl_basic_map *bmap;
4465 unsigned n_in, n_out, n, nparam, total, pos;
4466 struct isl_dim_map *dim_map1, *dim_map2;
4468 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4469 goto error;
4470 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4471 bmap2->dim, isl_dim_in))
4472 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4473 "spaces don't match", goto error);
4475 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4476 isl_space_copy(bmap2->dim));
4478 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4479 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4480 n = isl_basic_map_dim(bmap1, isl_dim_out);
4481 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4483 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4484 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4485 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4486 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4487 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4488 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4489 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4490 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4491 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4492 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4493 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4495 bmap = isl_basic_map_alloc_space(space_result,
4496 bmap1->n_div + bmap2->n_div + n,
4497 bmap1->n_eq + bmap2->n_eq,
4498 bmap1->n_ineq + bmap2->n_ineq);
4499 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4500 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4501 bmap = add_divs(bmap, n);
4502 bmap = isl_basic_map_simplify(bmap);
4503 bmap = isl_basic_map_drop_redundant_divs(bmap);
4504 return isl_basic_map_finalize(bmap);
4505 error:
4506 isl_basic_map_free(bmap1);
4507 isl_basic_map_free(bmap2);
4508 return NULL;
4511 struct isl_basic_set *isl_basic_set_apply(
4512 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4514 if (!bset || !bmap)
4515 goto error;
4517 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4518 goto error);
4520 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4521 bmap));
4522 error:
4523 isl_basic_set_free(bset);
4524 isl_basic_map_free(bmap);
4525 return NULL;
4528 struct isl_basic_map *isl_basic_map_apply_domain(
4529 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4531 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4532 goto error;
4533 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4534 bmap2->dim, isl_dim_in))
4535 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4536 "spaces don't match", goto error);
4538 bmap1 = isl_basic_map_reverse(bmap1);
4539 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4540 return isl_basic_map_reverse(bmap1);
4541 error:
4542 isl_basic_map_free(bmap1);
4543 isl_basic_map_free(bmap2);
4544 return NULL;
4547 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4548 * A \cap B -> f(A) + f(B)
4550 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4551 __isl_take isl_basic_map *bmap2)
4553 unsigned n_in, n_out, nparam, total, pos;
4554 struct isl_basic_map *bmap = NULL;
4555 struct isl_dim_map *dim_map1, *dim_map2;
4556 int i;
4558 if (!bmap1 || !bmap2)
4559 goto error;
4561 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4562 goto error);
4564 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4565 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4566 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4568 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4569 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4570 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4571 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4572 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4573 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4574 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4575 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4576 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4577 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4578 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4580 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4581 bmap1->n_div + bmap2->n_div + 2 * n_out,
4582 bmap1->n_eq + bmap2->n_eq + n_out,
4583 bmap1->n_ineq + bmap2->n_ineq);
4584 for (i = 0; i < n_out; ++i) {
4585 int j = isl_basic_map_alloc_equality(bmap);
4586 if (j < 0)
4587 goto error;
4588 isl_seq_clr(bmap->eq[j], 1+total);
4589 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4590 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4591 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4593 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4594 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4595 bmap = add_divs(bmap, 2 * n_out);
4597 bmap = isl_basic_map_simplify(bmap);
4598 return isl_basic_map_finalize(bmap);
4599 error:
4600 isl_basic_map_free(bmap);
4601 isl_basic_map_free(bmap1);
4602 isl_basic_map_free(bmap2);
4603 return NULL;
4606 /* Given two maps A -> f(A) and B -> g(B), construct a map
4607 * A \cap B -> f(A) + f(B)
4609 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4610 __isl_take isl_map *map2)
4612 struct isl_map *result;
4613 int i, j;
4615 if (!map1 || !map2)
4616 goto error;
4618 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4620 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4621 map1->n * map2->n, 0);
4622 if (!result)
4623 goto error;
4624 for (i = 0; i < map1->n; ++i)
4625 for (j = 0; j < map2->n; ++j) {
4626 struct isl_basic_map *part;
4627 part = isl_basic_map_sum(
4628 isl_basic_map_copy(map1->p[i]),
4629 isl_basic_map_copy(map2->p[j]));
4630 if (isl_basic_map_is_empty(part))
4631 isl_basic_map_free(part);
4632 else
4633 result = isl_map_add_basic_map(result, part);
4634 if (!result)
4635 goto error;
4637 isl_map_free(map1);
4638 isl_map_free(map2);
4639 return result;
4640 error:
4641 isl_map_free(map1);
4642 isl_map_free(map2);
4643 return NULL;
4646 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4647 __isl_take isl_set *set2)
4649 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4652 /* Given a basic map A -> f(A), construct A -> -f(A).
4654 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4656 int i, j;
4657 unsigned off, n;
4659 bmap = isl_basic_map_cow(bmap);
4660 if (!bmap)
4661 return NULL;
4663 n = isl_basic_map_dim(bmap, isl_dim_out);
4664 off = isl_basic_map_offset(bmap, isl_dim_out);
4665 for (i = 0; i < bmap->n_eq; ++i)
4666 for (j = 0; j < n; ++j)
4667 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4668 for (i = 0; i < bmap->n_ineq; ++i)
4669 for (j = 0; j < n; ++j)
4670 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4671 for (i = 0; i < bmap->n_div; ++i)
4672 for (j = 0; j < n; ++j)
4673 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4674 bmap = isl_basic_map_gauss(bmap, NULL);
4675 return isl_basic_map_finalize(bmap);
4678 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4680 return isl_basic_map_neg(bset);
4683 /* Given a map A -> f(A), construct A -> -f(A).
4685 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4687 int i;
4689 map = isl_map_cow(map);
4690 if (!map)
4691 return NULL;
4693 for (i = 0; i < map->n; ++i) {
4694 map->p[i] = isl_basic_map_neg(map->p[i]);
4695 if (!map->p[i])
4696 goto error;
4699 return map;
4700 error:
4701 isl_map_free(map);
4702 return NULL;
4705 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4707 return set_from_map(isl_map_neg(set_to_map(set)));
4710 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4711 * A -> floor(f(A)/d).
4713 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4714 isl_int d)
4716 unsigned n_in, n_out, nparam, total, pos;
4717 struct isl_basic_map *result = NULL;
4718 struct isl_dim_map *dim_map;
4719 int i;
4721 if (!bmap)
4722 return NULL;
4724 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4725 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4726 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4728 total = nparam + n_in + n_out + bmap->n_div + n_out;
4729 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4730 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4731 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4732 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4733 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4735 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4736 bmap->n_div + n_out,
4737 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4738 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4739 result = add_divs(result, n_out);
4740 for (i = 0; i < n_out; ++i) {
4741 int j;
4742 j = isl_basic_map_alloc_inequality(result);
4743 if (j < 0)
4744 goto error;
4745 isl_seq_clr(result->ineq[j], 1+total);
4746 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4747 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4748 j = isl_basic_map_alloc_inequality(result);
4749 if (j < 0)
4750 goto error;
4751 isl_seq_clr(result->ineq[j], 1+total);
4752 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4753 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4754 isl_int_sub_ui(result->ineq[j][0], d, 1);
4757 result = isl_basic_map_simplify(result);
4758 return isl_basic_map_finalize(result);
4759 error:
4760 isl_basic_map_free(result);
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(__isl_take isl_map *map, isl_int d)
4769 int i;
4771 map = isl_map_cow(map);
4772 if (!map)
4773 return NULL;
4775 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4776 for (i = 0; i < map->n; ++i) {
4777 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4778 if (!map->p[i])
4779 goto error;
4781 map = isl_map_unmark_normalized(map);
4783 return map;
4784 error:
4785 isl_map_free(map);
4786 return NULL;
4789 /* Given a map A -> f(A) and an integer d, construct a map
4790 * A -> floor(f(A)/d).
4792 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4793 __isl_take isl_val *d)
4795 if (!map || !d)
4796 goto error;
4797 if (!isl_val_is_int(d))
4798 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4799 "expecting integer denominator", goto error);
4800 map = isl_map_floordiv(map, d->n);
4801 isl_val_free(d);
4802 return map;
4803 error:
4804 isl_map_free(map);
4805 isl_val_free(d);
4806 return NULL;
4809 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4810 unsigned pos)
4812 int i;
4813 unsigned nparam;
4814 unsigned n_in;
4816 i = isl_basic_map_alloc_equality(bmap);
4817 if (i < 0)
4818 goto error;
4819 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4820 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4821 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4822 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4823 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4824 return isl_basic_map_finalize(bmap);
4825 error:
4826 isl_basic_map_free(bmap);
4827 return NULL;
4830 /* Add a constraint to "bmap" expressing i_pos < o_pos
4832 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4833 unsigned pos)
4835 int i;
4836 unsigned nparam;
4837 unsigned n_in;
4839 i = isl_basic_map_alloc_inequality(bmap);
4840 if (i < 0)
4841 goto error;
4842 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4843 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4844 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4845 isl_int_set_si(bmap->ineq[i][0], -1);
4846 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4847 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4848 return isl_basic_map_finalize(bmap);
4849 error:
4850 isl_basic_map_free(bmap);
4851 return NULL;
4854 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4856 static __isl_give isl_basic_map *var_less_or_equal(
4857 __isl_take isl_basic_map *bmap, unsigned pos)
4859 int i;
4860 unsigned nparam;
4861 unsigned n_in;
4863 i = isl_basic_map_alloc_inequality(bmap);
4864 if (i < 0)
4865 goto error;
4866 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4867 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4868 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4869 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4870 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4871 return isl_basic_map_finalize(bmap);
4872 error:
4873 isl_basic_map_free(bmap);
4874 return NULL;
4877 /* Add a constraint to "bmap" expressing i_pos > o_pos
4879 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4880 unsigned pos)
4882 int i;
4883 unsigned nparam;
4884 unsigned n_in;
4886 i = isl_basic_map_alloc_inequality(bmap);
4887 if (i < 0)
4888 goto error;
4889 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4890 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4891 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4892 isl_int_set_si(bmap->ineq[i][0], -1);
4893 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4894 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4895 return isl_basic_map_finalize(bmap);
4896 error:
4897 isl_basic_map_free(bmap);
4898 return NULL;
4901 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4903 static __isl_give isl_basic_map *var_more_or_equal(
4904 __isl_take isl_basic_map *bmap, unsigned pos)
4906 int i;
4907 unsigned nparam;
4908 unsigned n_in;
4910 i = isl_basic_map_alloc_inequality(bmap);
4911 if (i < 0)
4912 goto error;
4913 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4914 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4915 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4916 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4917 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4918 return isl_basic_map_finalize(bmap);
4919 error:
4920 isl_basic_map_free(bmap);
4921 return NULL;
4924 __isl_give isl_basic_map *isl_basic_map_equal(
4925 __isl_take isl_space *space, unsigned n_equal)
4927 int i;
4928 struct isl_basic_map *bmap;
4929 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4930 if (!bmap)
4931 return NULL;
4932 for (i = 0; i < n_equal && bmap; ++i)
4933 bmap = var_equal(bmap, i);
4934 return isl_basic_map_finalize(bmap);
4937 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4939 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4940 unsigned pos)
4942 int i;
4943 struct isl_basic_map *bmap;
4944 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4945 if (!bmap)
4946 return NULL;
4947 for (i = 0; i < pos && bmap; ++i)
4948 bmap = var_equal(bmap, i);
4949 if (bmap)
4950 bmap = var_less(bmap, pos);
4951 return isl_basic_map_finalize(bmap);
4954 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4956 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4957 __isl_take isl_space *space, unsigned pos)
4959 int i;
4960 isl_basic_map *bmap;
4962 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4963 for (i = 0; i < pos; ++i)
4964 bmap = var_equal(bmap, i);
4965 bmap = var_less_or_equal(bmap, pos);
4966 return isl_basic_map_finalize(bmap);
4969 /* Return a relation on "space" expressing i_pos > o_pos
4971 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4972 unsigned pos)
4974 int i;
4975 struct isl_basic_map *bmap;
4976 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4977 if (!bmap)
4978 return NULL;
4979 for (i = 0; i < pos && bmap; ++i)
4980 bmap = var_equal(bmap, i);
4981 if (bmap)
4982 bmap = var_more(bmap, pos);
4983 return isl_basic_map_finalize(bmap);
4986 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4988 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4989 __isl_take isl_space *space, unsigned pos)
4991 int i;
4992 isl_basic_map *bmap;
4994 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4995 for (i = 0; i < pos; ++i)
4996 bmap = var_equal(bmap, i);
4997 bmap = var_more_or_equal(bmap, pos);
4998 return isl_basic_map_finalize(bmap);
5001 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5002 unsigned n, int equal)
5004 struct isl_map *map;
5005 int i;
5007 if (n == 0 && equal)
5008 return isl_map_universe(space);
5010 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5012 for (i = 0; i + 1 < n; ++i)
5013 map = isl_map_add_basic_map(map,
5014 isl_basic_map_less_at(isl_space_copy(space), i));
5015 if (n > 0) {
5016 if (equal)
5017 map = isl_map_add_basic_map(map,
5018 isl_basic_map_less_or_equal_at(space, n - 1));
5019 else
5020 map = isl_map_add_basic_map(map,
5021 isl_basic_map_less_at(space, n - 1));
5022 } else
5023 isl_space_free(space);
5025 return map;
5028 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5030 if (!space)
5031 return NULL;
5032 return map_lex_lte_first(space, space->n_out, equal);
5035 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5037 return map_lex_lte_first(dim, n, 0);
5040 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5042 return map_lex_lte_first(dim, n, 1);
5045 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5047 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5050 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5052 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5055 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5056 unsigned n, int equal)
5058 struct isl_map *map;
5059 int i;
5061 if (n == 0 && equal)
5062 return isl_map_universe(space);
5064 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5066 for (i = 0; i + 1 < n; ++i)
5067 map = isl_map_add_basic_map(map,
5068 isl_basic_map_more_at(isl_space_copy(space), i));
5069 if (n > 0) {
5070 if (equal)
5071 map = isl_map_add_basic_map(map,
5072 isl_basic_map_more_or_equal_at(space, n - 1));
5073 else
5074 map = isl_map_add_basic_map(map,
5075 isl_basic_map_more_at(space, n - 1));
5076 } else
5077 isl_space_free(space);
5079 return map;
5082 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5084 if (!space)
5085 return NULL;
5086 return map_lex_gte_first(space, space->n_out, equal);
5089 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5091 return map_lex_gte_first(dim, n, 0);
5094 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5096 return map_lex_gte_first(dim, n, 1);
5099 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5101 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5104 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5106 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5109 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5110 __isl_take isl_set *set2)
5112 isl_map *map;
5113 map = isl_map_lex_le(isl_set_get_space(set1));
5114 map = isl_map_intersect_domain(map, set1);
5115 map = isl_map_intersect_range(map, set2);
5116 return map;
5119 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5120 __isl_take isl_set *set2)
5122 isl_map *map;
5123 map = isl_map_lex_lt(isl_set_get_space(set1));
5124 map = isl_map_intersect_domain(map, set1);
5125 map = isl_map_intersect_range(map, set2);
5126 return map;
5129 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5130 __isl_take isl_set *set2)
5132 isl_map *map;
5133 map = isl_map_lex_ge(isl_set_get_space(set1));
5134 map = isl_map_intersect_domain(map, set1);
5135 map = isl_map_intersect_range(map, set2);
5136 return map;
5139 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5140 __isl_take isl_set *set2)
5142 isl_map *map;
5143 map = isl_map_lex_gt(isl_set_get_space(set1));
5144 map = isl_map_intersect_domain(map, set1);
5145 map = isl_map_intersect_range(map, set2);
5146 return map;
5149 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5150 __isl_take isl_map *map2)
5152 isl_map *map;
5153 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5154 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5155 map = isl_map_apply_range(map, isl_map_reverse(map2));
5156 return map;
5159 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5160 __isl_take isl_map *map2)
5162 isl_map *map;
5163 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5164 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5165 map = isl_map_apply_range(map, isl_map_reverse(map2));
5166 return map;
5169 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5170 __isl_take isl_map *map2)
5172 isl_map *map;
5173 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5174 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5175 map = isl_map_apply_range(map, isl_map_reverse(map2));
5176 return map;
5179 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5180 __isl_take isl_map *map2)
5182 isl_map *map;
5183 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5184 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5185 map = isl_map_apply_range(map, isl_map_reverse(map2));
5186 return map;
5189 /* For the div d = floor(f/m) at position "div", add the constraint
5191 * f - m d >= 0
5193 static __isl_give isl_basic_map *add_upper_div_constraint(
5194 __isl_take isl_basic_map *bmap, unsigned div)
5196 int i;
5197 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5198 unsigned n_div, pos;
5200 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5201 if (v_div < 0)
5202 return isl_basic_map_free(bmap);
5203 pos = v_div + div;
5204 i = isl_basic_map_alloc_inequality(bmap);
5205 if (i < 0)
5206 return isl_basic_map_free(bmap);
5207 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5208 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5210 return bmap;
5213 /* For the div d = floor(f/m) at position "div", add the constraint
5215 * -(f-(m-1)) + m d >= 0
5217 static __isl_give isl_basic_map *add_lower_div_constraint(
5218 __isl_take isl_basic_map *bmap, unsigned div)
5220 int i;
5221 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5222 unsigned n_div, pos;
5224 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5225 if (v_div < 0)
5226 return isl_basic_map_free(bmap);
5227 pos = v_div + div;
5228 i = isl_basic_map_alloc_inequality(bmap);
5229 if (i < 0)
5230 return isl_basic_map_free(bmap);
5231 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5232 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5233 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5234 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5236 return bmap;
5239 /* For the div d = floor(f/m) at position "pos", add the constraints
5241 * f - m d >= 0
5242 * -(f-(m-1)) + m d >= 0
5244 * Note that the second constraint is the negation of
5246 * f - m d >= m
5248 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5249 __isl_take isl_basic_map *bmap, unsigned pos)
5251 bmap = add_upper_div_constraint(bmap, pos);
5252 bmap = add_lower_div_constraint(bmap, pos);
5253 return bmap;
5256 /* For each known div d = floor(f/m), add the constraints
5258 * f - m d >= 0
5259 * -(f-(m-1)) + m d >= 0
5261 * Remove duplicate constraints in case of some these div constraints
5262 * already appear in "bmap".
5264 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5265 __isl_take isl_basic_map *bmap)
5267 unsigned n_div;
5269 if (!bmap)
5270 return NULL;
5271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5272 if (n_div == 0)
5273 return bmap;
5275 bmap = add_known_div_constraints(bmap);
5276 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5277 bmap = isl_basic_map_finalize(bmap);
5278 return bmap;
5281 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5283 * In particular, if this div is of the form d = floor(f/m),
5284 * then add the constraint
5286 * f - m d >= 0
5288 * if sign < 0 or the constraint
5290 * -(f-(m-1)) + m d >= 0
5292 * if sign > 0.
5294 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5295 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5297 if (sign < 0)
5298 return add_upper_div_constraint(bmap, div);
5299 else
5300 return add_lower_div_constraint(bmap, div);
5303 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5304 __isl_take isl_basic_map *bmap)
5306 if (!bmap)
5307 goto error;
5308 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5309 bmap->n_div == 0 &&
5310 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5311 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5312 return bset_from_bmap(bmap);
5313 bmap = isl_basic_map_cow(bmap);
5314 if (!bmap)
5315 goto error;
5316 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5317 if (!bmap->dim)
5318 goto error;
5319 bmap->extra -= bmap->n_div;
5320 bmap->n_div = 0;
5321 bmap = isl_basic_map_finalize(bmap);
5322 return bset_from_bmap(bmap);
5323 error:
5324 isl_basic_map_free(bmap);
5325 return NULL;
5328 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5329 __isl_take isl_basic_set *bset)
5331 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5334 /* Replace each element in "list" by the result of applying
5335 * isl_basic_map_underlying_set to the element.
5337 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5338 __isl_take isl_basic_map_list *list)
5340 int i, n;
5342 if (!list)
5343 return NULL;
5345 n = isl_basic_map_list_n_basic_map(list);
5346 for (i = 0; i < n; ++i) {
5347 isl_basic_map *bmap;
5348 isl_basic_set *bset;
5350 bmap = isl_basic_map_list_get_basic_map(list, i);
5351 bset = isl_basic_set_underlying_set(bmap);
5352 list = isl_basic_set_list_set_basic_set(list, i, bset);
5355 return list;
5358 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5359 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5361 struct isl_basic_map *bmap;
5362 struct isl_ctx *ctx;
5363 unsigned total;
5364 int i;
5366 if (!bset || !like)
5367 goto error;
5368 ctx = bset->ctx;
5369 if (isl_basic_set_check_no_params(bset) < 0 ||
5370 isl_basic_set_check_no_locals(bset) < 0)
5371 goto error;
5372 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5373 goto error);
5374 if (like->n_div == 0) {
5375 isl_space *space = isl_basic_map_get_space(like);
5376 isl_basic_map_free(like);
5377 return isl_basic_map_reset_space(bset, space);
5379 bset = isl_basic_set_cow(bset);
5380 if (!bset)
5381 goto error;
5382 total = bset->dim->n_out + bset->extra;
5383 bmap = bset_to_bmap(bset);
5384 isl_space_free(bmap->dim);
5385 bmap->dim = isl_space_copy(like->dim);
5386 if (!bmap->dim)
5387 goto error;
5388 bmap->n_div = like->n_div;
5389 bmap->extra += like->n_div;
5390 if (bmap->extra) {
5391 unsigned ltotal;
5392 isl_int **div;
5393 ltotal = total - bmap->extra + like->extra;
5394 if (ltotal > total)
5395 ltotal = total;
5396 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5397 bmap->extra * (1 + 1 + total));
5398 if (isl_blk_is_error(bmap->block2))
5399 goto error;
5400 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5401 if (!div)
5402 goto error;
5403 bmap->div = div;
5404 for (i = 0; i < bmap->extra; ++i)
5405 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5406 for (i = 0; i < like->n_div; ++i) {
5407 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5408 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5410 bmap = isl_basic_map_add_known_div_constraints(bmap);
5412 isl_basic_map_free(like);
5413 bmap = isl_basic_map_simplify(bmap);
5414 bmap = isl_basic_map_finalize(bmap);
5415 return bmap;
5416 error:
5417 isl_basic_map_free(like);
5418 isl_basic_set_free(bset);
5419 return NULL;
5422 struct isl_basic_set *isl_basic_set_from_underlying_set(
5423 struct isl_basic_set *bset, struct isl_basic_set *like)
5425 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5426 bset_to_bmap(like)));
5429 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5431 int i;
5433 map = isl_map_cow(map);
5434 if (!map)
5435 return NULL;
5436 map->dim = isl_space_cow(map->dim);
5437 if (!map->dim)
5438 goto error;
5440 for (i = 1; i < map->n; ++i)
5441 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5442 goto error);
5443 for (i = 0; i < map->n; ++i) {
5444 map->p[i] = bset_to_bmap(
5445 isl_basic_map_underlying_set(map->p[i]));
5446 if (!map->p[i])
5447 goto error;
5449 if (map->n == 0)
5450 map->dim = isl_space_underlying(map->dim, 0);
5451 else {
5452 isl_space_free(map->dim);
5453 map->dim = isl_space_copy(map->p[0]->dim);
5455 if (!map->dim)
5456 goto error;
5457 return set_from_map(map);
5458 error:
5459 isl_map_free(map);
5460 return NULL;
5463 /* Replace the space of "bmap" by "space".
5465 * If the space of "bmap" is identical to "space" (including the identifiers
5466 * of the input and output dimensions), then simply return the original input.
5468 __isl_give isl_basic_map *isl_basic_map_reset_space(
5469 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5471 isl_bool equal;
5472 isl_space *bmap_space;
5474 bmap_space = isl_basic_map_peek_space(bmap);
5475 equal = isl_space_is_equal(bmap_space, space);
5476 if (equal >= 0 && equal)
5477 equal = isl_space_has_equal_ids(bmap_space, space);
5478 if (equal < 0)
5479 goto error;
5480 if (equal) {
5481 isl_space_free(space);
5482 return bmap;
5484 bmap = isl_basic_map_cow(bmap);
5485 if (!bmap || !space)
5486 goto error;
5488 isl_space_free(bmap->dim);
5489 bmap->dim = space;
5491 bmap = isl_basic_map_finalize(bmap);
5493 return bmap;
5494 error:
5495 isl_basic_map_free(bmap);
5496 isl_space_free(space);
5497 return NULL;
5500 __isl_give isl_basic_set *isl_basic_set_reset_space(
5501 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5503 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5504 dim));
5507 /* Check that the total dimensions of "map" and "space" are the same.
5509 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5510 __isl_keep isl_space *space)
5512 unsigned dim1, dim2;
5514 if (!map || !space)
5515 return isl_stat_error;
5516 dim1 = isl_map_dim(map, isl_dim_all);
5517 dim2 = isl_space_dim(space, isl_dim_all);
5518 if (dim1 == dim2)
5519 return isl_stat_ok;
5520 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5521 "total dimensions do not match", return isl_stat_error);
5524 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5525 __isl_take isl_space *space)
5527 int i;
5529 map = isl_map_cow(map);
5530 if (!map || !space)
5531 goto error;
5533 for (i = 0; i < map->n; ++i) {
5534 map->p[i] = isl_basic_map_reset_space(map->p[i],
5535 isl_space_copy(space));
5536 if (!map->p[i])
5537 goto error;
5539 isl_space_free(map->dim);
5540 map->dim = space;
5542 return map;
5543 error:
5544 isl_map_free(map);
5545 isl_space_free(space);
5546 return NULL;
5549 /* Replace the space of "map" by "space", without modifying
5550 * the dimension of "map".
5552 * If the space of "map" is identical to "space" (including the identifiers
5553 * of the input and output dimensions), then simply return the original input.
5555 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5556 __isl_take isl_space *space)
5558 isl_bool equal;
5559 isl_space *map_space;
5561 map_space = isl_map_peek_space(map);
5562 equal = isl_space_is_equal(map_space, space);
5563 if (equal >= 0 && equal)
5564 equal = isl_space_has_equal_ids(map_space, space);
5565 if (equal < 0)
5566 goto error;
5567 if (equal) {
5568 isl_space_free(space);
5569 return map;
5571 if (check_map_space_equal_total_dim(map, space) < 0)
5572 goto error;
5573 return isl_map_reset_space(map, space);
5574 error:
5575 isl_map_free(map);
5576 isl_space_free(space);
5577 return NULL;
5580 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5581 __isl_take isl_space *dim)
5583 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5586 /* Compute the parameter domain of the given basic set.
5588 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5590 isl_bool is_params;
5591 isl_space *space;
5592 unsigned n;
5594 is_params = isl_basic_set_is_params(bset);
5595 if (is_params < 0)
5596 return isl_basic_set_free(bset);
5597 if (is_params)
5598 return bset;
5600 n = isl_basic_set_dim(bset, isl_dim_set);
5601 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5602 space = isl_basic_set_get_space(bset);
5603 space = isl_space_params(space);
5604 bset = isl_basic_set_reset_space(bset, space);
5605 return bset;
5608 /* Construct a zero-dimensional basic set with the given parameter domain.
5610 __isl_give isl_basic_set *isl_basic_set_from_params(
5611 __isl_take isl_basic_set *bset)
5613 isl_space *space;
5614 space = isl_basic_set_get_space(bset);
5615 space = isl_space_set_from_params(space);
5616 bset = isl_basic_set_reset_space(bset, space);
5617 return bset;
5620 /* Compute the parameter domain of the given set.
5622 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5624 isl_space *space;
5625 unsigned n;
5627 if (isl_set_is_params(set))
5628 return set;
5630 n = isl_set_dim(set, isl_dim_set);
5631 set = isl_set_project_out(set, isl_dim_set, 0, n);
5632 space = isl_set_get_space(set);
5633 space = isl_space_params(space);
5634 set = isl_set_reset_space(set, space);
5635 return set;
5638 /* Construct a zero-dimensional set with the given parameter domain.
5640 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5642 isl_space *space;
5643 space = isl_set_get_space(set);
5644 space = isl_space_set_from_params(space);
5645 set = isl_set_reset_space(set, space);
5646 return set;
5649 /* Compute the parameter domain of the given map.
5651 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5653 isl_space *space;
5654 unsigned n;
5656 n = isl_map_dim(map, isl_dim_in);
5657 map = isl_map_project_out(map, isl_dim_in, 0, n);
5658 n = isl_map_dim(map, isl_dim_out);
5659 map = isl_map_project_out(map, isl_dim_out, 0, n);
5660 space = isl_map_get_space(map);
5661 space = isl_space_params(space);
5662 map = isl_map_reset_space(map, space);
5663 return map;
5666 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5668 isl_space *space;
5669 unsigned n_out;
5671 if (!bmap)
5672 return NULL;
5673 space = isl_space_domain(isl_basic_map_get_space(bmap));
5675 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5676 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5678 return isl_basic_map_reset_space(bmap, space);
5681 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5683 if (!bmap)
5684 return isl_bool_error;
5685 return isl_space_may_be_set(bmap->dim);
5688 /* Is this basic map actually a set?
5689 * Users should never call this function. Outside of isl,
5690 * the type should indicate whether something is a set or a map.
5692 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5694 if (!bmap)
5695 return isl_bool_error;
5696 return isl_space_is_set(bmap->dim);
5699 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5701 isl_bool is_set;
5703 is_set = isl_basic_map_is_set(bmap);
5704 if (is_set < 0)
5705 goto error;
5706 if (is_set)
5707 return bmap;
5708 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5709 error:
5710 isl_basic_map_free(bmap);
5711 return NULL;
5714 __isl_give isl_basic_map *isl_basic_map_domain_map(
5715 __isl_take isl_basic_map *bmap)
5717 int i;
5718 isl_space *space;
5719 isl_basic_map *domain;
5720 int nparam, n_in, n_out;
5722 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5723 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5724 n_out = isl_basic_map_dim(bmap, isl_dim_out);
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 int 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);
5754 space = isl_basic_map_get_space(bmap);
5755 space = isl_space_from_range(isl_space_range(space));
5756 range = isl_basic_map_universe(space);
5758 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5759 bmap = isl_basic_map_apply_range(bmap, range);
5760 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5762 for (i = 0; i < n_out; ++i)
5763 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5764 isl_dim_out, i);
5766 bmap = isl_basic_map_gauss(bmap, NULL);
5767 return isl_basic_map_finalize(bmap);
5770 int isl_map_may_be_set(__isl_keep isl_map *map)
5772 if (!map)
5773 return -1;
5774 return isl_space_may_be_set(map->dim);
5777 /* Is this map actually a set?
5778 * Users should never call this function. Outside of isl,
5779 * the type should indicate whether something is a set or a map.
5781 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5783 if (!map)
5784 return isl_bool_error;
5785 return isl_space_is_set(map->dim);
5788 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5790 int i;
5791 isl_bool is_set;
5792 struct isl_set *set;
5794 is_set = isl_map_is_set(map);
5795 if (is_set < 0)
5796 goto error;
5797 if (is_set)
5798 return set_from_map(map);
5800 map = isl_map_cow(map);
5801 if (!map)
5802 goto error;
5804 set = set_from_map(map);
5805 set->dim = isl_space_range(set->dim);
5806 if (!set->dim)
5807 goto error;
5808 for (i = 0; i < map->n; ++i) {
5809 set->p[i] = isl_basic_map_range(map->p[i]);
5810 if (!set->p[i])
5811 goto error;
5813 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5814 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5815 return set;
5816 error:
5817 isl_map_free(map);
5818 return NULL;
5821 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5823 int i;
5825 map = isl_map_cow(map);
5826 if (!map)
5827 return NULL;
5829 map->dim = isl_space_domain_map(map->dim);
5830 if (!map->dim)
5831 goto error;
5832 for (i = 0; i < map->n; ++i) {
5833 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5834 if (!map->p[i])
5835 goto error;
5837 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5838 map = isl_map_unmark_normalized(map);
5839 return map;
5840 error:
5841 isl_map_free(map);
5842 return NULL;
5845 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5847 int i;
5848 isl_space *range_dim;
5850 map = isl_map_cow(map);
5851 if (!map)
5852 return NULL;
5854 range_dim = isl_space_range(isl_map_get_space(map));
5855 range_dim = isl_space_from_range(range_dim);
5856 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5857 map->dim = isl_space_join(map->dim, range_dim);
5858 if (!map->dim)
5859 goto error;
5860 for (i = 0; i < map->n; ++i) {
5861 map->p[i] = isl_basic_map_range_map(map->p[i]);
5862 if (!map->p[i])
5863 goto error;
5865 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5866 map = isl_map_unmark_normalized(map);
5867 return map;
5868 error:
5869 isl_map_free(map);
5870 return NULL;
5873 /* Given a wrapped map of the form A[B -> C],
5874 * return the map A[B -> C] -> B.
5876 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5878 isl_id *id;
5879 isl_map *map;
5881 if (!set)
5882 return NULL;
5883 if (!isl_set_has_tuple_id(set))
5884 return isl_map_domain_map(isl_set_unwrap(set));
5886 id = isl_set_get_tuple_id(set);
5887 map = isl_map_domain_map(isl_set_unwrap(set));
5888 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5890 return map;
5893 __isl_give isl_basic_map *isl_basic_map_from_domain(
5894 __isl_take isl_basic_set *bset)
5896 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5899 __isl_give isl_basic_map *isl_basic_map_from_range(
5900 __isl_take isl_basic_set *bset)
5902 isl_space *space;
5903 space = isl_basic_set_get_space(bset);
5904 space = isl_space_from_range(space);
5905 bset = isl_basic_set_reset_space(bset, space);
5906 return bset_to_bmap(bset);
5909 /* Create a relation with the given set as range.
5910 * The domain of the created relation is a zero-dimensional
5911 * flat anonymous space.
5913 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5915 isl_space *space;
5916 space = isl_set_get_space(set);
5917 space = isl_space_from_range(space);
5918 set = isl_set_reset_space(set, space);
5919 return set_to_map(set);
5922 /* Create a relation with the given set as domain.
5923 * The range of the created relation is a zero-dimensional
5924 * flat anonymous space.
5926 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5928 return isl_map_reverse(isl_map_from_range(set));
5931 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5932 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5934 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5937 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5938 __isl_take isl_set *range)
5940 return isl_map_apply_range(isl_map_reverse(domain), range);
5943 /* Return a newly allocated isl_map with given space and flags and
5944 * room for "n" basic maps.
5945 * Make sure that all cached information is cleared.
5947 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5948 unsigned flags)
5950 struct isl_map *map;
5952 if (!space)
5953 return NULL;
5954 if (n < 0)
5955 isl_die(space->ctx, isl_error_internal,
5956 "negative number of basic maps", goto error);
5957 map = isl_calloc(space->ctx, struct isl_map,
5958 sizeof(struct isl_map) +
5959 (n - 1) * sizeof(struct isl_basic_map *));
5960 if (!map)
5961 goto error;
5963 map->ctx = space->ctx;
5964 isl_ctx_ref(map->ctx);
5965 map->ref = 1;
5966 map->size = n;
5967 map->n = 0;
5968 map->dim = space;
5969 map->flags = flags;
5970 return map;
5971 error:
5972 isl_space_free(space);
5973 return NULL;
5976 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5978 struct isl_basic_map *bmap;
5979 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5980 bmap = isl_basic_map_set_to_empty(bmap);
5981 return bmap;
5984 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5986 struct isl_basic_set *bset;
5987 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5988 bset = isl_basic_set_set_to_empty(bset);
5989 return bset;
5992 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5994 struct isl_basic_map *bmap;
5995 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5996 bmap = isl_basic_map_finalize(bmap);
5997 return bmap;
6000 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6002 struct isl_basic_set *bset;
6003 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6004 bset = isl_basic_set_finalize(bset);
6005 return bset;
6008 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6009 __isl_take isl_space *space)
6011 int i;
6012 unsigned total = isl_space_dim(space, isl_dim_all);
6013 isl_basic_map *bmap;
6015 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6016 for (i = 0; i < total; ++i) {
6017 int k = isl_basic_map_alloc_inequality(bmap);
6018 if (k < 0)
6019 goto error;
6020 isl_seq_clr(bmap->ineq[k], 1 + total);
6021 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6023 return bmap;
6024 error:
6025 isl_basic_map_free(bmap);
6026 return NULL;
6029 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6030 __isl_take isl_space *space)
6032 return isl_basic_map_nat_universe(space);
6035 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6037 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6040 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6042 return isl_map_nat_universe(dim);
6045 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6047 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6050 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6052 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6055 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6057 struct isl_map *map;
6058 if (!space)
6059 return NULL;
6060 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6061 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6062 return map;
6065 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6067 struct isl_set *set;
6068 if (!space)
6069 return NULL;
6070 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6071 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6072 return set;
6075 struct isl_map *isl_map_dup(struct isl_map *map)
6077 int i;
6078 struct isl_map *dup;
6080 if (!map)
6081 return NULL;
6082 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6083 for (i = 0; i < map->n; ++i)
6084 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6085 return dup;
6088 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6089 __isl_take isl_basic_map *bmap)
6091 if (!bmap || !map)
6092 goto error;
6093 if (isl_basic_map_plain_is_empty(bmap)) {
6094 isl_basic_map_free(bmap);
6095 return map;
6097 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6098 isl_assert(map->ctx, map->n < map->size, goto error);
6099 map->p[map->n] = bmap;
6100 map->n++;
6101 map = isl_map_unmark_normalized(map);
6102 return map;
6103 error:
6104 if (map)
6105 isl_map_free(map);
6106 if (bmap)
6107 isl_basic_map_free(bmap);
6108 return NULL;
6111 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6113 int i;
6115 if (!map)
6116 return NULL;
6118 if (--map->ref > 0)
6119 return NULL;
6121 clear_caches(map);
6122 isl_ctx_deref(map->ctx);
6123 for (i = 0; i < map->n; ++i)
6124 isl_basic_map_free(map->p[i]);
6125 isl_space_free(map->dim);
6126 free(map);
6128 return NULL;
6131 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6132 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6134 int j;
6136 bmap = isl_basic_map_cow(bmap);
6137 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6138 j = isl_basic_map_alloc_equality(bmap);
6139 if (j < 0)
6140 goto error;
6141 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6142 isl_int_set_si(bmap->eq[j][pos], -1);
6143 isl_int_set_si(bmap->eq[j][0], value);
6144 bmap = isl_basic_map_simplify(bmap);
6145 return isl_basic_map_finalize(bmap);
6146 error:
6147 isl_basic_map_free(bmap);
6148 return NULL;
6151 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6152 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6154 int j;
6156 bmap = isl_basic_map_cow(bmap);
6157 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6158 j = isl_basic_map_alloc_equality(bmap);
6159 if (j < 0)
6160 goto error;
6161 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6162 isl_int_set_si(bmap->eq[j][pos], -1);
6163 isl_int_set(bmap->eq[j][0], value);
6164 bmap = isl_basic_map_simplify(bmap);
6165 return isl_basic_map_finalize(bmap);
6166 error:
6167 isl_basic_map_free(bmap);
6168 return NULL;
6171 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6172 enum isl_dim_type type, unsigned pos, int value)
6174 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6175 return isl_basic_map_free(bmap);
6176 return isl_basic_map_fix_pos_si(bmap,
6177 isl_basic_map_offset(bmap, type) + pos, value);
6180 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6181 enum isl_dim_type type, unsigned pos, isl_int value)
6183 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6184 return isl_basic_map_free(bmap);
6185 return isl_basic_map_fix_pos(bmap,
6186 isl_basic_map_offset(bmap, type) + pos, value);
6189 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6190 * to be equal to "v".
6192 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6193 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6195 if (!bmap || !v)
6196 goto error;
6197 if (!isl_val_is_int(v))
6198 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6199 "expecting integer value", goto error);
6200 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6201 goto error;
6202 pos += isl_basic_map_offset(bmap, type);
6203 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6204 isl_val_free(v);
6205 return bmap;
6206 error:
6207 isl_basic_map_free(bmap);
6208 isl_val_free(v);
6209 return NULL;
6212 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6213 * to be equal to "v".
6215 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6216 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6218 return isl_basic_map_fix_val(bset, type, pos, v);
6221 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6222 enum isl_dim_type type, unsigned pos, int value)
6224 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6225 type, pos, value));
6228 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6229 enum isl_dim_type type, unsigned pos, isl_int value)
6231 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6232 type, pos, value));
6235 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6236 unsigned input, int value)
6238 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6241 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6242 unsigned dim, int value)
6244 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6245 isl_dim_set, dim, value));
6248 /* Remove the basic map at position "i" from "map" if this basic map
6249 * is (obviously) empty.
6251 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6253 isl_bool empty;
6255 if (!map)
6256 return NULL;
6258 empty = isl_basic_map_plain_is_empty(map->p[i]);
6259 if (empty < 0)
6260 return isl_map_free(map);
6261 if (!empty)
6262 return map;
6264 isl_basic_map_free(map->p[i]);
6265 map->n--;
6266 if (i != map->n) {
6267 map->p[i] = map->p[map->n];
6268 map = isl_map_unmark_normalized(map);
6272 return map;
6275 /* Perform "fn" on each basic map of "map", where we may not be holding
6276 * the only reference to "map".
6277 * In particular, "fn" should be a semantics preserving operation
6278 * that we want to apply to all copies of "map". We therefore need
6279 * to be careful not to modify "map" in a way that breaks "map"
6280 * in case anything goes wrong.
6282 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6283 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6285 struct isl_basic_map *bmap;
6286 int i;
6288 if (!map)
6289 return NULL;
6291 for (i = map->n - 1; i >= 0; --i) {
6292 bmap = isl_basic_map_copy(map->p[i]);
6293 bmap = fn(bmap);
6294 if (!bmap)
6295 goto error;
6296 isl_basic_map_free(map->p[i]);
6297 map->p[i] = bmap;
6298 map = remove_if_empty(map, i);
6299 if (!map)
6300 return NULL;
6303 return map;
6304 error:
6305 isl_map_free(map);
6306 return NULL;
6309 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6310 enum isl_dim_type type, unsigned pos, int value)
6312 int i;
6314 map = isl_map_cow(map);
6315 if (isl_map_check_range(map, type, pos, 1) < 0)
6316 return isl_map_free(map);
6317 for (i = map->n - 1; i >= 0; --i) {
6318 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6319 map = remove_if_empty(map, i);
6320 if (!map)
6321 return NULL;
6323 map = isl_map_unmark_normalized(map);
6324 return map;
6327 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6328 enum isl_dim_type type, unsigned pos, int value)
6330 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6333 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6334 enum isl_dim_type type, unsigned pos, isl_int value)
6336 int i;
6338 map = isl_map_cow(map);
6339 if (isl_map_check_range(map, type, pos, 1) < 0)
6340 return isl_map_free(map);
6341 for (i = 0; i < map->n; ++i) {
6342 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6343 if (!map->p[i])
6344 goto error;
6346 map = isl_map_unmark_normalized(map);
6347 return map;
6348 error:
6349 isl_map_free(map);
6350 return NULL;
6353 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6354 enum isl_dim_type type, unsigned pos, isl_int value)
6356 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6359 /* Fix the value of the variable at position "pos" of type "type" of "map"
6360 * to be equal to "v".
6362 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6363 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6365 int i;
6367 map = isl_map_cow(map);
6368 if (!map || !v)
6369 goto error;
6371 if (!isl_val_is_int(v))
6372 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6373 "expecting integer value", goto error);
6374 if (isl_map_check_range(map, type, pos, 1) < 0)
6375 goto error;
6376 for (i = map->n - 1; i >= 0; --i) {
6377 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6378 isl_val_copy(v));
6379 map = remove_if_empty(map, i);
6380 if (!map)
6381 goto error;
6383 map = isl_map_unmark_normalized(map);
6384 isl_val_free(v);
6385 return map;
6386 error:
6387 isl_map_free(map);
6388 isl_val_free(v);
6389 return NULL;
6392 /* Fix the value of the variable at position "pos" of type "type" of "set"
6393 * to be equal to "v".
6395 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6396 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6398 return isl_map_fix_val(set, type, pos, v);
6401 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6402 unsigned input, int value)
6404 return isl_map_fix_si(map, isl_dim_in, input, value);
6407 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6409 return set_from_map(isl_map_fix_si(set_to_map(set),
6410 isl_dim_set, dim, value));
6413 static __isl_give isl_basic_map *basic_map_bound_si(
6414 __isl_take isl_basic_map *bmap,
6415 enum isl_dim_type type, unsigned pos, int value, int upper)
6417 int j;
6419 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6420 return isl_basic_map_free(bmap);
6421 pos += isl_basic_map_offset(bmap, type);
6422 bmap = isl_basic_map_cow(bmap);
6423 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6424 j = isl_basic_map_alloc_inequality(bmap);
6425 if (j < 0)
6426 goto error;
6427 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6428 if (upper) {
6429 isl_int_set_si(bmap->ineq[j][pos], -1);
6430 isl_int_set_si(bmap->ineq[j][0], value);
6431 } else {
6432 isl_int_set_si(bmap->ineq[j][pos], 1);
6433 isl_int_set_si(bmap->ineq[j][0], -value);
6435 bmap = isl_basic_map_simplify(bmap);
6436 return isl_basic_map_finalize(bmap);
6437 error:
6438 isl_basic_map_free(bmap);
6439 return NULL;
6442 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6443 __isl_take isl_basic_map *bmap,
6444 enum isl_dim_type type, unsigned pos, int value)
6446 return basic_map_bound_si(bmap, type, pos, value, 0);
6449 /* Constrain the values of the given dimension to be no greater than "value".
6451 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6452 __isl_take isl_basic_map *bmap,
6453 enum isl_dim_type type, unsigned pos, int value)
6455 return basic_map_bound_si(bmap, type, pos, value, 1);
6458 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6459 enum isl_dim_type type, unsigned pos, int value, int upper)
6461 int i;
6463 map = isl_map_cow(map);
6464 if (isl_map_check_range(map, type, pos, 1) < 0)
6465 return isl_map_free(map);
6466 for (i = 0; i < map->n; ++i) {
6467 map->p[i] = basic_map_bound_si(map->p[i],
6468 type, pos, value, upper);
6469 if (!map->p[i])
6470 goto error;
6472 map = isl_map_unmark_normalized(map);
6473 return map;
6474 error:
6475 isl_map_free(map);
6476 return NULL;
6479 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6480 enum isl_dim_type type, unsigned pos, int value)
6482 return map_bound_si(map, type, pos, value, 0);
6485 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6486 enum isl_dim_type type, unsigned pos, int value)
6488 return map_bound_si(map, type, pos, value, 1);
6491 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6492 enum isl_dim_type type, unsigned pos, int value)
6494 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6495 type, pos, value));
6498 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6499 enum isl_dim_type type, unsigned pos, int value)
6501 return isl_map_upper_bound_si(set, type, pos, value);
6504 /* Bound the given variable of "bmap" from below (or above is "upper"
6505 * is set) to "value".
6507 static __isl_give isl_basic_map *basic_map_bound(
6508 __isl_take isl_basic_map *bmap,
6509 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6511 int j;
6513 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6514 return isl_basic_map_free(bmap);
6515 pos += isl_basic_map_offset(bmap, type);
6516 bmap = isl_basic_map_cow(bmap);
6517 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6518 j = isl_basic_map_alloc_inequality(bmap);
6519 if (j < 0)
6520 goto error;
6521 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6522 if (upper) {
6523 isl_int_set_si(bmap->ineq[j][pos], -1);
6524 isl_int_set(bmap->ineq[j][0], value);
6525 } else {
6526 isl_int_set_si(bmap->ineq[j][pos], 1);
6527 isl_int_neg(bmap->ineq[j][0], value);
6529 bmap = isl_basic_map_simplify(bmap);
6530 return isl_basic_map_finalize(bmap);
6531 error:
6532 isl_basic_map_free(bmap);
6533 return NULL;
6536 /* Bound the given variable of "map" from below (or above is "upper"
6537 * is set) to "value".
6539 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6540 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6542 int i;
6544 map = isl_map_cow(map);
6545 if (isl_map_check_range(map, type, pos, 1) < 0)
6546 return isl_map_free(map);
6547 for (i = map->n - 1; i >= 0; --i) {
6548 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6549 map = remove_if_empty(map, i);
6550 if (!map)
6551 return NULL;
6553 map = isl_map_unmark_normalized(map);
6554 return map;
6557 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6558 enum isl_dim_type type, unsigned pos, isl_int value)
6560 return map_bound(map, type, pos, value, 0);
6563 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6564 enum isl_dim_type type, unsigned pos, isl_int value)
6566 return map_bound(map, type, pos, value, 1);
6569 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6570 enum isl_dim_type type, unsigned pos, isl_int value)
6572 return isl_map_lower_bound(set, type, pos, value);
6575 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6576 enum isl_dim_type type, unsigned pos, isl_int value)
6578 return isl_map_upper_bound(set, type, pos, value);
6581 /* Force the values of the variable at position "pos" of type "type" of "set"
6582 * to be no smaller than "value".
6584 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6585 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6587 if (!value)
6588 goto error;
6589 if (!isl_val_is_int(value))
6590 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6591 "expecting integer value", goto error);
6592 set = isl_set_lower_bound(set, type, pos, value->n);
6593 isl_val_free(value);
6594 return set;
6595 error:
6596 isl_val_free(value);
6597 isl_set_free(set);
6598 return NULL;
6601 /* Force the values of the variable at position "pos" of type "type" of "set"
6602 * to be no greater than "value".
6604 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6605 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6607 if (!value)
6608 goto error;
6609 if (!isl_val_is_int(value))
6610 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6611 "expecting integer value", goto error);
6612 set = isl_set_upper_bound(set, type, pos, value->n);
6613 isl_val_free(value);
6614 return set;
6615 error:
6616 isl_val_free(value);
6617 isl_set_free(set);
6618 return NULL;
6621 /* Bound the given variable of "bset" from below (or above is "upper"
6622 * is set) to "value".
6624 static __isl_give isl_basic_set *isl_basic_set_bound(
6625 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6626 isl_int value, int upper)
6628 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6629 type, pos, value, upper));
6632 /* Bound the given variable of "bset" from below (or above is "upper"
6633 * is set) to "value".
6635 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6636 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6637 __isl_take isl_val *value, int upper)
6639 if (!value)
6640 goto error;
6641 if (!isl_val_is_int(value))
6642 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6643 "expecting integer value", goto error);
6644 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6645 isl_val_free(value);
6646 return bset;
6647 error:
6648 isl_val_free(value);
6649 isl_basic_set_free(bset);
6650 return NULL;
6653 /* Bound the given variable of "bset" from below to "value".
6655 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6656 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6657 __isl_take isl_val *value)
6659 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6662 /* Bound the given variable of "bset" from above to "value".
6664 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6665 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6666 __isl_take isl_val *value)
6668 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6671 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6673 int i;
6675 map = isl_map_cow(map);
6676 if (!map)
6677 return NULL;
6679 map->dim = isl_space_reverse(map->dim);
6680 if (!map->dim)
6681 goto error;
6682 for (i = 0; i < map->n; ++i) {
6683 map->p[i] = isl_basic_map_reverse(map->p[i]);
6684 if (!map->p[i])
6685 goto error;
6687 map = isl_map_unmark_normalized(map);
6688 return map;
6689 error:
6690 isl_map_free(map);
6691 return NULL;
6694 #undef TYPE
6695 #define TYPE isl_pw_multi_aff
6696 #undef SUFFIX
6697 #define SUFFIX _pw_multi_aff
6698 #undef EMPTY
6699 #define EMPTY isl_pw_multi_aff_empty
6700 #undef ADD
6701 #define ADD isl_pw_multi_aff_union_add
6702 #include "isl_map_lexopt_templ.c"
6704 /* Given a map "map", compute the lexicographically minimal
6705 * (or maximal) image element for each domain element in dom,
6706 * in the form of an isl_pw_multi_aff.
6707 * If "empty" is not NULL, then set *empty to those elements in dom that
6708 * do not have an image element.
6709 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6710 * should be computed over the domain of "map". "empty" is also NULL
6711 * in this case.
6713 * We first compute the lexicographically minimal or maximal element
6714 * in the first basic map. This results in a partial solution "res"
6715 * and a subset "todo" of dom that still need to be handled.
6716 * We then consider each of the remaining maps in "map" and successively
6717 * update both "res" and "todo".
6718 * If "empty" is NULL, then the todo sets are not needed and therefore
6719 * also not computed.
6721 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6722 __isl_take isl_map *map, __isl_take isl_set *dom,
6723 __isl_give isl_set **empty, unsigned flags)
6725 int i;
6726 int full;
6727 isl_pw_multi_aff *res;
6728 isl_set *todo;
6730 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6731 if (!map || (!full && !dom))
6732 goto error;
6734 if (isl_map_plain_is_empty(map)) {
6735 if (empty)
6736 *empty = dom;
6737 else
6738 isl_set_free(dom);
6739 return isl_pw_multi_aff_from_map(map);
6742 res = basic_map_partial_lexopt_pw_multi_aff(
6743 isl_basic_map_copy(map->p[0]),
6744 isl_set_copy(dom), empty, flags);
6746 if (empty)
6747 todo = *empty;
6748 for (i = 1; i < map->n; ++i) {
6749 isl_pw_multi_aff *res_i;
6751 res_i = basic_map_partial_lexopt_pw_multi_aff(
6752 isl_basic_map_copy(map->p[i]),
6753 isl_set_copy(dom), empty, flags);
6755 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6756 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6757 else
6758 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6760 if (empty)
6761 todo = isl_set_intersect(todo, *empty);
6764 isl_set_free(dom);
6765 isl_map_free(map);
6767 if (empty)
6768 *empty = todo;
6770 return res;
6771 error:
6772 if (empty)
6773 *empty = NULL;
6774 isl_set_free(dom);
6775 isl_map_free(map);
6776 return NULL;
6779 #undef TYPE
6780 #define TYPE isl_map
6781 #undef SUFFIX
6782 #define SUFFIX
6783 #undef EMPTY
6784 #define EMPTY isl_map_empty
6785 #undef ADD
6786 #define ADD isl_map_union_disjoint
6787 #include "isl_map_lexopt_templ.c"
6789 /* Given a map "map", compute the lexicographically minimal
6790 * (or maximal) image element for each domain element in "dom",
6791 * in the form of an isl_map.
6792 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6793 * do not have an image element.
6794 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6795 * should be computed over the domain of "map". "empty" is also NULL
6796 * in this case.
6798 * If the input consists of more than one disjunct, then first
6799 * compute the desired result in the form of an isl_pw_multi_aff and
6800 * then convert that into an isl_map.
6802 * This function used to have an explicit implementation in terms
6803 * of isl_maps, but it would continually intersect the domains of
6804 * partial results with the complement of the domain of the next
6805 * partial solution, potentially leading to an explosion in the number
6806 * of disjuncts if there are several disjuncts in the input.
6807 * An even earlier implementation of this function would look for
6808 * better results in the domain of the partial result and for extra
6809 * results in the complement of this domain, which would lead to
6810 * even more splintering.
6812 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6813 __isl_take isl_map *map, __isl_take isl_set *dom,
6814 __isl_give isl_set **empty, unsigned flags)
6816 int full;
6817 struct isl_map *res;
6818 isl_pw_multi_aff *pma;
6820 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6821 if (!map || (!full && !dom))
6822 goto error;
6824 if (isl_map_plain_is_empty(map)) {
6825 if (empty)
6826 *empty = dom;
6827 else
6828 isl_set_free(dom);
6829 return map;
6832 if (map->n == 1) {
6833 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6834 dom, empty, flags);
6835 isl_map_free(map);
6836 return res;
6839 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6840 flags);
6841 return isl_map_from_pw_multi_aff(pma);
6842 error:
6843 if (empty)
6844 *empty = NULL;
6845 isl_set_free(dom);
6846 isl_map_free(map);
6847 return NULL;
6850 __isl_give isl_map *isl_map_partial_lexmax(
6851 __isl_take isl_map *map, __isl_take isl_set *dom,
6852 __isl_give isl_set **empty)
6854 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6857 __isl_give isl_map *isl_map_partial_lexmin(
6858 __isl_take isl_map *map, __isl_take isl_set *dom,
6859 __isl_give isl_set **empty)
6861 return isl_map_partial_lexopt(map, dom, empty, 0);
6864 __isl_give isl_set *isl_set_partial_lexmin(
6865 __isl_take isl_set *set, __isl_take isl_set *dom,
6866 __isl_give isl_set **empty)
6868 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6869 dom, empty));
6872 __isl_give isl_set *isl_set_partial_lexmax(
6873 __isl_take isl_set *set, __isl_take isl_set *dom,
6874 __isl_give isl_set **empty)
6876 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6877 dom, empty));
6880 /* Compute the lexicographic minimum (or maximum if "flags" includes
6881 * ISL_OPT_MAX) of "bset" over its parametric domain.
6883 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6884 unsigned flags)
6886 return isl_basic_map_lexopt(bset, flags);
6889 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6891 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6894 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6896 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6899 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6901 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6904 /* Compute the lexicographic minimum of "bset" over its parametric domain
6905 * for the purpose of quantifier elimination.
6906 * That is, find an explicit representation for all the existentially
6907 * quantified variables in "bset" by computing their lexicographic
6908 * minimum.
6910 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6911 __isl_take isl_basic_set *bset)
6913 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6916 /* Given a basic map with one output dimension, compute the minimum or
6917 * maximum of that dimension as an isl_pw_aff.
6919 * Compute the optimum as a lexicographic optimum over the single
6920 * output dimension and extract the single isl_pw_aff from the result.
6922 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6923 int max)
6925 isl_pw_multi_aff *pma;
6926 isl_pw_aff *pwaff;
6928 bmap = isl_basic_map_copy(bmap);
6929 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6930 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6931 isl_pw_multi_aff_free(pma);
6933 return pwaff;
6936 /* Compute the minimum or maximum of the given output dimension
6937 * as a function of the parameters and the input dimensions,
6938 * but independently of the other output dimensions.
6940 * We first project out the other output dimension and then compute
6941 * the "lexicographic" maximum in each basic map, combining the results
6942 * using isl_pw_aff_union_max.
6944 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6945 int max)
6947 int i;
6948 isl_pw_aff *pwaff;
6949 unsigned n_out;
6951 n_out = isl_map_dim(map, isl_dim_out);
6952 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6953 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6954 if (!map)
6955 return NULL;
6957 if (map->n == 0) {
6958 isl_space *space = isl_map_get_space(map);
6959 isl_map_free(map);
6960 return isl_pw_aff_empty(space);
6963 pwaff = basic_map_dim_opt(map->p[0], max);
6964 for (i = 1; i < map->n; ++i) {
6965 isl_pw_aff *pwaff_i;
6967 pwaff_i = basic_map_dim_opt(map->p[i], max);
6968 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6971 isl_map_free(map);
6973 return pwaff;
6976 /* Compute the minimum of the given output dimension as a function of the
6977 * parameters and input dimensions, but independently of
6978 * the other output dimensions.
6980 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6982 return map_dim_opt(map, pos, 0);
6985 /* Compute the maximum of the given output dimension as a function of the
6986 * parameters and input dimensions, but independently of
6987 * the other output dimensions.
6989 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6991 return map_dim_opt(map, pos, 1);
6994 /* Compute the minimum or maximum of the given set dimension
6995 * as a function of the parameters,
6996 * but independently of the other set dimensions.
6998 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6999 int max)
7001 return map_dim_opt(set, pos, max);
7004 /* Compute the maximum of the given set dimension as a function of the
7005 * parameters, but independently of the other set dimensions.
7007 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7009 return set_dim_opt(set, pos, 1);
7012 /* Compute the minimum of the given set dimension as a function of the
7013 * parameters, but independently of the other set dimensions.
7015 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7017 return set_dim_opt(set, pos, 0);
7020 /* Apply a preimage specified by "mat" on the parameters of "bset".
7021 * bset is assumed to have only parameters and divs.
7023 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7024 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7026 unsigned nparam;
7028 if (!bset || !mat)
7029 goto error;
7031 bset->dim = isl_space_cow(bset->dim);
7032 if (!bset->dim)
7033 goto error;
7035 nparam = isl_basic_set_dim(bset, isl_dim_param);
7037 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7039 bset->dim->nparam = 0;
7040 bset->dim->n_out = nparam;
7041 bset = isl_basic_set_preimage(bset, mat);
7042 if (bset) {
7043 bset->dim->nparam = bset->dim->n_out;
7044 bset->dim->n_out = 0;
7046 return bset;
7047 error:
7048 isl_mat_free(mat);
7049 isl_basic_set_free(bset);
7050 return NULL;
7053 /* Apply a preimage specified by "mat" on the parameters of "set".
7054 * set is assumed to have only parameters and divs.
7056 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7057 __isl_take isl_mat *mat)
7059 isl_space *space;
7060 unsigned nparam;
7062 if (!set || !mat)
7063 goto error;
7065 nparam = isl_set_dim(set, isl_dim_param);
7067 if (mat->n_row != 1 + nparam)
7068 isl_die(isl_set_get_ctx(set), isl_error_internal,
7069 "unexpected number of rows", goto error);
7071 space = isl_set_get_space(set);
7072 space = isl_space_move_dims(space, isl_dim_set, 0,
7073 isl_dim_param, 0, nparam);
7074 set = isl_set_reset_space(set, space);
7075 set = isl_set_preimage(set, mat);
7076 nparam = isl_set_dim(set, isl_dim_out);
7077 space = isl_set_get_space(set);
7078 space = isl_space_move_dims(space, isl_dim_param, 0,
7079 isl_dim_out, 0, nparam);
7080 set = isl_set_reset_space(set, space);
7081 return set;
7082 error:
7083 isl_mat_free(mat);
7084 isl_set_free(set);
7085 return NULL;
7088 /* Intersect the basic set "bset" with the affine space specified by the
7089 * equalities in "eq".
7091 static __isl_give isl_basic_set *basic_set_append_equalities(
7092 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7094 int i, k;
7095 unsigned len;
7097 if (!bset || !eq)
7098 goto error;
7100 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7101 eq->n_row, 0);
7102 if (!bset)
7103 goto error;
7105 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7106 for (i = 0; i < eq->n_row; ++i) {
7107 k = isl_basic_set_alloc_equality(bset);
7108 if (k < 0)
7109 goto error;
7110 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7111 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7113 isl_mat_free(eq);
7115 bset = isl_basic_set_gauss(bset, NULL);
7116 bset = isl_basic_set_finalize(bset);
7118 return bset;
7119 error:
7120 isl_mat_free(eq);
7121 isl_basic_set_free(bset);
7122 return NULL;
7125 /* Intersect the set "set" with the affine space specified by the
7126 * equalities in "eq".
7128 static struct isl_set *set_append_equalities(struct isl_set *set,
7129 struct isl_mat *eq)
7131 int i;
7133 if (!set || !eq)
7134 goto error;
7136 for (i = 0; i < set->n; ++i) {
7137 set->p[i] = basic_set_append_equalities(set->p[i],
7138 isl_mat_copy(eq));
7139 if (!set->p[i])
7140 goto error;
7142 isl_mat_free(eq);
7143 return set;
7144 error:
7145 isl_mat_free(eq);
7146 isl_set_free(set);
7147 return NULL;
7150 /* Given a basic set "bset" that only involves parameters and existentially
7151 * quantified variables, return the index of the first equality
7152 * that only involves parameters. If there is no such equality then
7153 * return bset->n_eq.
7155 * This function assumes that isl_basic_set_gauss has been called on "bset".
7157 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7159 int i, j;
7160 unsigned nparam, n_div;
7162 if (!bset)
7163 return -1;
7165 nparam = isl_basic_set_dim(bset, isl_dim_param);
7166 n_div = isl_basic_set_dim(bset, isl_dim_div);
7168 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7169 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7170 ++i;
7173 return i;
7176 /* Compute an explicit representation for the existentially quantified
7177 * variables in "bset" by computing the "minimal value" of the set
7178 * variables. Since there are no set variables, the computation of
7179 * the minimal value essentially computes an explicit representation
7180 * of the non-empty part(s) of "bset".
7182 * The input only involves parameters and existentially quantified variables.
7183 * All equalities among parameters have been removed.
7185 * Since the existentially quantified variables in the result are in general
7186 * going to be different from those in the input, we first replace
7187 * them by the minimal number of variables based on their equalities.
7188 * This should simplify the parametric integer programming.
7190 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7192 isl_morph *morph1, *morph2;
7193 isl_set *set;
7194 unsigned n;
7196 if (!bset)
7197 return NULL;
7198 if (bset->n_eq == 0)
7199 return isl_basic_set_lexmin_compute_divs(bset);
7201 morph1 = isl_basic_set_parameter_compression(bset);
7202 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7203 bset = isl_basic_set_lift(bset);
7204 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7205 bset = isl_morph_basic_set(morph2, bset);
7206 n = isl_basic_set_dim(bset, isl_dim_set);
7207 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7209 set = isl_basic_set_lexmin_compute_divs(bset);
7211 set = isl_morph_set(isl_morph_inverse(morph1), set);
7213 return set;
7216 /* Project the given basic set onto its parameter domain, possibly introducing
7217 * new, explicit, existential variables in the constraints.
7218 * The input has parameters and (possibly implicit) existential variables.
7219 * The output has the same parameters, but only
7220 * explicit existentially quantified variables.
7222 * The actual projection is performed by pip, but pip doesn't seem
7223 * to like equalities very much, so we first remove the equalities
7224 * among the parameters by performing a variable compression on
7225 * the parameters. Afterward, an inverse transformation is performed
7226 * and the equalities among the parameters are inserted back in.
7228 * The variable compression on the parameters may uncover additional
7229 * equalities that were only implicit before. We therefore check
7230 * if there are any new parameter equalities in the result and
7231 * if so recurse. The removal of parameter equalities is required
7232 * for the parameter compression performed by base_compute_divs.
7234 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7236 int i;
7237 struct isl_mat *eq;
7238 struct isl_mat *T, *T2;
7239 struct isl_set *set;
7240 unsigned nparam;
7242 bset = isl_basic_set_cow(bset);
7243 if (!bset)
7244 return NULL;
7246 if (bset->n_eq == 0)
7247 return base_compute_divs(bset);
7249 bset = isl_basic_set_gauss(bset, NULL);
7250 if (!bset)
7251 return NULL;
7252 if (isl_basic_set_plain_is_empty(bset))
7253 return isl_set_from_basic_set(bset);
7255 i = first_parameter_equality(bset);
7256 if (i == bset->n_eq)
7257 return base_compute_divs(bset);
7259 nparam = isl_basic_set_dim(bset, isl_dim_param);
7260 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7261 0, 1 + nparam);
7262 eq = isl_mat_cow(eq);
7263 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7264 if (T && T->n_col == 0) {
7265 isl_mat_free(T);
7266 isl_mat_free(T2);
7267 isl_mat_free(eq);
7268 bset = isl_basic_set_set_to_empty(bset);
7269 return isl_set_from_basic_set(bset);
7271 bset = basic_set_parameter_preimage(bset, T);
7273 i = first_parameter_equality(bset);
7274 if (!bset)
7275 set = NULL;
7276 else if (i == bset->n_eq)
7277 set = base_compute_divs(bset);
7278 else
7279 set = parameter_compute_divs(bset);
7280 set = set_parameter_preimage(set, T2);
7281 set = set_append_equalities(set, eq);
7282 return set;
7285 /* Insert the divs from "ls" before those of "bmap".
7287 * The number of columns is not changed, which means that the last
7288 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7289 * The caller is responsible for removing the same number of dimensions
7290 * from the space of "bmap".
7292 static __isl_give isl_basic_map *insert_divs_from_local_space(
7293 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7295 int i;
7296 int n_div;
7297 int old_n_div;
7299 n_div = isl_local_space_dim(ls, isl_dim_div);
7300 if (n_div == 0)
7301 return bmap;
7303 old_n_div = bmap->n_div;
7304 bmap = insert_div_rows(bmap, n_div);
7305 if (!bmap)
7306 return NULL;
7308 for (i = 0; i < n_div; ++i) {
7309 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7310 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7313 return bmap;
7316 /* Replace the space of "bmap" by the space and divs of "ls".
7318 * If "ls" has any divs, then we simplify the result since we may
7319 * have discovered some additional equalities that could simplify
7320 * the div expressions.
7322 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7323 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7325 int n_div;
7327 bmap = isl_basic_map_cow(bmap);
7328 if (!bmap || !ls)
7329 goto error;
7331 n_div = isl_local_space_dim(ls, isl_dim_div);
7332 bmap = insert_divs_from_local_space(bmap, ls);
7333 if (!bmap)
7334 goto error;
7336 isl_space_free(bmap->dim);
7337 bmap->dim = isl_local_space_get_space(ls);
7338 if (!bmap->dim)
7339 goto error;
7341 isl_local_space_free(ls);
7342 if (n_div > 0)
7343 bmap = isl_basic_map_simplify(bmap);
7344 bmap = isl_basic_map_finalize(bmap);
7345 return bmap;
7346 error:
7347 isl_basic_map_free(bmap);
7348 isl_local_space_free(ls);
7349 return NULL;
7352 /* Replace the space of "map" by the space and divs of "ls".
7354 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7355 __isl_take isl_local_space *ls)
7357 int i;
7359 map = isl_map_cow(map);
7360 if (!map || !ls)
7361 goto error;
7363 for (i = 0; i < map->n; ++i) {
7364 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7365 isl_local_space_copy(ls));
7366 if (!map->p[i])
7367 goto error;
7369 isl_space_free(map->dim);
7370 map->dim = isl_local_space_get_space(ls);
7371 if (!map->dim)
7372 goto error;
7374 isl_local_space_free(ls);
7375 return map;
7376 error:
7377 isl_local_space_free(ls);
7378 isl_map_free(map);
7379 return NULL;
7382 /* Compute an explicit representation for the existentially
7383 * quantified variables for which do not know any explicit representation yet.
7385 * We first sort the existentially quantified variables so that the
7386 * existentially quantified variables for which we already have an explicit
7387 * representation are placed before those for which we do not.
7388 * The input dimensions, the output dimensions and the existentially
7389 * quantified variables for which we already have an explicit
7390 * representation are then turned into parameters.
7391 * compute_divs returns a map with the same parameters and
7392 * no input or output dimensions and the dimension specification
7393 * is reset to that of the input, including the existentially quantified
7394 * variables for which we already had an explicit representation.
7396 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7398 struct isl_basic_set *bset;
7399 struct isl_set *set;
7400 struct isl_map *map;
7401 isl_space *space;
7402 isl_local_space *ls;
7403 unsigned nparam;
7404 unsigned n_in;
7405 unsigned n_out;
7406 int n_known;
7407 int i;
7409 bmap = isl_basic_map_sort_divs(bmap);
7410 bmap = isl_basic_map_cow(bmap);
7411 if (!bmap)
7412 return NULL;
7414 n_known = isl_basic_map_first_unknown_div(bmap);
7415 if (n_known < 0)
7416 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7418 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7419 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7420 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7421 space = isl_space_set_alloc(bmap->ctx,
7422 nparam + n_in + n_out + n_known, 0);
7423 if (!space)
7424 goto error;
7426 ls = isl_basic_map_get_local_space(bmap);
7427 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7428 n_known, bmap->n_div - n_known);
7429 if (n_known > 0) {
7430 for (i = n_known; i < bmap->n_div; ++i)
7431 swap_div(bmap, i - n_known, i);
7432 bmap->n_div -= n_known;
7433 bmap->extra -= n_known;
7435 bmap = isl_basic_map_reset_space(bmap, space);
7436 bset = bset_from_bmap(bmap);
7438 set = parameter_compute_divs(bset);
7439 map = set_to_map(set);
7440 map = replace_space_by_local_space(map, ls);
7442 return map;
7443 error:
7444 isl_basic_map_free(bmap);
7445 return NULL;
7448 /* Remove the explicit representation of local variable "div",
7449 * if there is any.
7451 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7452 __isl_take isl_basic_map *bmap, int div)
7454 isl_bool unknown;
7456 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7457 if (unknown < 0)
7458 return isl_basic_map_free(bmap);
7459 if (unknown)
7460 return bmap;
7462 bmap = isl_basic_map_cow(bmap);
7463 if (!bmap)
7464 return NULL;
7465 isl_int_set_si(bmap->div[div][0], 0);
7466 return bmap;
7469 /* Is local variable "div" of "bmap" marked as not having an explicit
7470 * representation?
7471 * Note that even if "div" is not marked in this way and therefore
7472 * has an explicit representation, this representation may still
7473 * depend (indirectly) on other local variables that do not
7474 * have an explicit representation.
7476 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7477 int div)
7479 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7480 return isl_bool_error;
7481 return isl_int_is_zero(bmap->div[div][0]);
7484 /* Return the position of the first local variable that does not
7485 * have an explicit representation.
7486 * Return the total number of local variables if they all have
7487 * an explicit representation.
7488 * Return -1 on error.
7490 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7492 int i;
7494 if (!bmap)
7495 return -1;
7497 for (i = 0; i < bmap->n_div; ++i) {
7498 if (!isl_basic_map_div_is_known(bmap, i))
7499 return i;
7501 return bmap->n_div;
7504 /* Return the position of the first local variable that does not
7505 * have an explicit representation.
7506 * Return the total number of local variables if they all have
7507 * an explicit representation.
7508 * Return -1 on error.
7510 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7512 return isl_basic_map_first_unknown_div(bset);
7515 /* Does "bmap" have an explicit representation for all local variables?
7517 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7519 int first, n;
7521 n = isl_basic_map_dim(bmap, isl_dim_div);
7522 first = isl_basic_map_first_unknown_div(bmap);
7523 if (first < 0)
7524 return isl_bool_error;
7525 return first == n;
7528 /* Do all basic maps in "map" have an explicit representation
7529 * for all local variables?
7531 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7533 int i;
7535 if (!map)
7536 return isl_bool_error;
7538 for (i = 0; i < map->n; ++i) {
7539 int known = isl_basic_map_divs_known(map->p[i]);
7540 if (known <= 0)
7541 return known;
7544 return isl_bool_true;
7547 /* If bmap contains any unknown divs, then compute explicit
7548 * expressions for them. However, this computation may be
7549 * quite expensive, so first try to remove divs that aren't
7550 * strictly needed.
7552 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7554 int known;
7555 struct isl_map *map;
7557 known = isl_basic_map_divs_known(bmap);
7558 if (known < 0)
7559 goto error;
7560 if (known)
7561 return isl_map_from_basic_map(bmap);
7563 bmap = isl_basic_map_drop_redundant_divs(bmap);
7565 known = isl_basic_map_divs_known(bmap);
7566 if (known < 0)
7567 goto error;
7568 if (known)
7569 return isl_map_from_basic_map(bmap);
7571 map = compute_divs(bmap);
7572 return map;
7573 error:
7574 isl_basic_map_free(bmap);
7575 return NULL;
7578 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7580 int i;
7581 int known;
7582 struct isl_map *res;
7584 if (!map)
7585 return NULL;
7586 if (map->n == 0)
7587 return map;
7589 known = isl_map_divs_known(map);
7590 if (known < 0) {
7591 isl_map_free(map);
7592 return NULL;
7594 if (known)
7595 return map;
7597 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7598 for (i = 1 ; i < map->n; ++i) {
7599 struct isl_map *r2;
7600 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7601 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7602 res = isl_map_union_disjoint(res, r2);
7603 else
7604 res = isl_map_union(res, r2);
7606 isl_map_free(map);
7608 return res;
7611 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7613 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7616 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7618 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7621 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7623 int i;
7624 struct isl_set *set;
7626 if (!map)
7627 goto error;
7629 map = isl_map_cow(map);
7630 if (!map)
7631 return NULL;
7633 set = set_from_map(map);
7634 set->dim = isl_space_domain(set->dim);
7635 if (!set->dim)
7636 goto error;
7637 for (i = 0; i < map->n; ++i) {
7638 set->p[i] = isl_basic_map_domain(map->p[i]);
7639 if (!set->p[i])
7640 goto error;
7642 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7643 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7644 return set;
7645 error:
7646 isl_map_free(map);
7647 return NULL;
7650 /* Return the union of "map1" and "map2", where we assume for now that
7651 * "map1" and "map2" are disjoint. Note that the basic maps inside
7652 * "map1" or "map2" may not be disjoint from each other.
7653 * Also note that this function is also called from isl_map_union,
7654 * which takes care of handling the situation where "map1" and "map2"
7655 * may not be disjoint.
7657 * If one of the inputs is empty, we can simply return the other input.
7658 * Similarly, if one of the inputs is universal, then it is equal to the union.
7660 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7661 __isl_take isl_map *map2)
7663 int i;
7664 unsigned flags = 0;
7665 struct isl_map *map = NULL;
7666 int is_universe;
7668 if (!map1 || !map2)
7669 goto error;
7671 if (!isl_space_is_equal(map1->dim, map2->dim))
7672 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7673 "spaces don't match", goto error);
7675 if (map1->n == 0) {
7676 isl_map_free(map1);
7677 return map2;
7679 if (map2->n == 0) {
7680 isl_map_free(map2);
7681 return map1;
7684 is_universe = isl_map_plain_is_universe(map1);
7685 if (is_universe < 0)
7686 goto error;
7687 if (is_universe) {
7688 isl_map_free(map2);
7689 return map1;
7692 is_universe = isl_map_plain_is_universe(map2);
7693 if (is_universe < 0)
7694 goto error;
7695 if (is_universe) {
7696 isl_map_free(map1);
7697 return map2;
7700 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7701 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7702 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7704 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7705 map1->n + map2->n, flags);
7706 if (!map)
7707 goto error;
7708 for (i = 0; i < map1->n; ++i) {
7709 map = isl_map_add_basic_map(map,
7710 isl_basic_map_copy(map1->p[i]));
7711 if (!map)
7712 goto error;
7714 for (i = 0; i < map2->n; ++i) {
7715 map = isl_map_add_basic_map(map,
7716 isl_basic_map_copy(map2->p[i]));
7717 if (!map)
7718 goto error;
7720 isl_map_free(map1);
7721 isl_map_free(map2);
7722 return map;
7723 error:
7724 isl_map_free(map);
7725 isl_map_free(map1);
7726 isl_map_free(map2);
7727 return NULL;
7730 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7731 * guaranteed to be disjoint by the caller.
7733 * Note that this functions is called from within isl_map_make_disjoint,
7734 * so we have to be careful not to touch the constraints of the inputs
7735 * in any way.
7737 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7738 __isl_take isl_map *map2)
7740 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7743 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7744 * not be disjoint. The parameters are assumed to have been aligned.
7746 * We currently simply call map_union_disjoint, the internal operation
7747 * of which does not really depend on the inputs being disjoint.
7748 * If the result contains more than one basic map, then we clear
7749 * the disjoint flag since the result may contain basic maps from
7750 * both inputs and these are not guaranteed to be disjoint.
7752 * As a special case, if "map1" and "map2" are obviously equal,
7753 * then we simply return "map1".
7755 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7756 __isl_take isl_map *map2)
7758 int equal;
7760 if (!map1 || !map2)
7761 goto error;
7763 equal = isl_map_plain_is_equal(map1, map2);
7764 if (equal < 0)
7765 goto error;
7766 if (equal) {
7767 isl_map_free(map2);
7768 return map1;
7771 map1 = map_union_disjoint(map1, map2);
7772 if (!map1)
7773 return NULL;
7774 if (map1->n > 1)
7775 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7776 return map1;
7777 error:
7778 isl_map_free(map1);
7779 isl_map_free(map2);
7780 return NULL;
7783 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7784 * not be disjoint.
7786 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7787 __isl_take isl_map *map2)
7789 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7792 __isl_give isl_set *isl_set_union_disjoint(
7793 __isl_take isl_set *set1, __isl_take isl_set *set2)
7795 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7796 set_to_map(set2)));
7799 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7801 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7804 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7805 * the results.
7807 * "map" and "set" are assumed to be compatible and non-NULL.
7809 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7810 __isl_take isl_set *set,
7811 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7812 __isl_take isl_basic_set *bset))
7814 unsigned flags = 0;
7815 struct isl_map *result;
7816 int i, j;
7818 if (isl_set_plain_is_universe(set)) {
7819 isl_set_free(set);
7820 return map;
7823 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7824 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7825 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7827 result = isl_map_alloc_space(isl_space_copy(map->dim),
7828 map->n * set->n, flags);
7829 for (i = 0; result && i < map->n; ++i)
7830 for (j = 0; j < set->n; ++j) {
7831 result = isl_map_add_basic_map(result,
7832 fn(isl_basic_map_copy(map->p[i]),
7833 isl_basic_set_copy(set->p[j])));
7834 if (!result)
7835 break;
7838 isl_map_free(map);
7839 isl_set_free(set);
7840 return result;
7843 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7844 __isl_take isl_set *set)
7846 isl_bool ok;
7848 ok = isl_map_compatible_range(map, set);
7849 if (ok < 0)
7850 goto error;
7851 if (!ok)
7852 isl_die(set->ctx, isl_error_invalid,
7853 "incompatible spaces", goto error);
7855 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7856 error:
7857 isl_map_free(map);
7858 isl_set_free(set);
7859 return NULL;
7862 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7863 __isl_take isl_set *set)
7865 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7868 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7869 __isl_take isl_set *set)
7871 isl_bool ok;
7873 ok = isl_map_compatible_domain(map, set);
7874 if (ok < 0)
7875 goto error;
7876 if (!ok)
7877 isl_die(set->ctx, isl_error_invalid,
7878 "incompatible spaces", goto error);
7880 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7881 error:
7882 isl_map_free(map);
7883 isl_set_free(set);
7884 return NULL;
7887 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7888 __isl_take isl_set *set)
7890 return isl_map_align_params_map_map_and(map, set,
7891 &map_intersect_domain);
7894 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7895 * in the space B -> C, return the intersection.
7896 * The parameters are assumed to have been aligned.
7898 * The map "factor" is first extended to a map living in the space
7899 * [A -> B] -> C and then a regular intersection is computed.
7901 static __isl_give isl_map *map_intersect_domain_factor_range(
7902 __isl_take isl_map *map, __isl_take isl_map *factor)
7904 isl_space *space;
7905 isl_map *ext_factor;
7907 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7908 ext_factor = isl_map_universe(space);
7909 ext_factor = isl_map_domain_product(ext_factor, factor);
7910 return map_intersect(map, ext_factor);
7913 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7914 * in the space B -> C, return the intersection.
7916 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7917 __isl_take isl_map *map, __isl_take isl_map *factor)
7919 return isl_map_align_params_map_map_and(map, factor,
7920 &map_intersect_domain_factor_range);
7923 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7924 * in the space A -> C, return the intersection.
7926 * The map "factor" is first extended to a map living in the space
7927 * A -> [B -> C] and then a regular intersection is computed.
7929 static __isl_give isl_map *map_intersect_range_factor_range(
7930 __isl_take isl_map *map, __isl_take isl_map *factor)
7932 isl_space *space;
7933 isl_map *ext_factor;
7935 space = isl_space_range_factor_domain(isl_map_get_space(map));
7936 ext_factor = isl_map_universe(space);
7937 ext_factor = isl_map_range_product(ext_factor, factor);
7938 return isl_map_intersect(map, ext_factor);
7941 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7942 * in the space A -> C, return the intersection.
7944 __isl_give isl_map *isl_map_intersect_range_factor_range(
7945 __isl_take isl_map *map, __isl_take isl_map *factor)
7947 return isl_map_align_params_map_map_and(map, factor,
7948 &map_intersect_range_factor_range);
7951 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7952 __isl_take isl_map *map2)
7954 if (!map1 || !map2)
7955 goto error;
7956 map1 = isl_map_reverse(map1);
7957 map1 = isl_map_apply_range(map1, map2);
7958 return isl_map_reverse(map1);
7959 error:
7960 isl_map_free(map1);
7961 isl_map_free(map2);
7962 return NULL;
7965 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7966 __isl_take isl_map *map2)
7968 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7971 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7972 __isl_take isl_map *map2)
7974 isl_space *space;
7975 struct isl_map *result;
7976 int i, j;
7978 if (!map1 || !map2)
7979 goto error;
7981 space = isl_space_join(isl_space_copy(map1->dim),
7982 isl_space_copy(map2->dim));
7984 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7985 if (!result)
7986 goto error;
7987 for (i = 0; i < map1->n; ++i)
7988 for (j = 0; j < map2->n; ++j) {
7989 result = isl_map_add_basic_map(result,
7990 isl_basic_map_apply_range(
7991 isl_basic_map_copy(map1->p[i]),
7992 isl_basic_map_copy(map2->p[j])));
7993 if (!result)
7994 goto error;
7996 isl_map_free(map1);
7997 isl_map_free(map2);
7998 if (result && result->n <= 1)
7999 ISL_F_SET(result, ISL_MAP_DISJOINT);
8000 return result;
8001 error:
8002 isl_map_free(map1);
8003 isl_map_free(map2);
8004 return NULL;
8007 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8008 __isl_take isl_map *map2)
8010 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8014 * returns range - domain
8016 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8018 isl_space *target_space;
8019 struct isl_basic_set *bset;
8020 unsigned dim;
8021 unsigned nparam;
8022 int i;
8024 if (!bmap)
8025 goto error;
8026 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8027 bmap->dim, isl_dim_out),
8028 goto error);
8029 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8030 dim = isl_basic_map_dim(bmap, isl_dim_in);
8031 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8032 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8033 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8034 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8035 for (i = 0; i < dim; ++i) {
8036 int j = isl_basic_map_alloc_equality(bmap);
8037 if (j < 0) {
8038 bmap = isl_basic_map_free(bmap);
8039 break;
8041 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8042 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8043 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8044 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8046 bset = isl_basic_map_domain(bmap);
8047 bset = isl_basic_set_reset_space(bset, target_space);
8048 return bset;
8049 error:
8050 isl_basic_map_free(bmap);
8051 return NULL;
8054 /* Check that domain and range of "map" are the same.
8056 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8058 isl_space *space;
8059 isl_bool equal;
8061 space = isl_map_peek_space(map);
8062 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8063 if (equal < 0)
8064 return isl_stat_error;
8065 if (!equal)
8066 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8067 "domain and range don't match", return isl_stat_error);
8068 return isl_stat_ok;
8072 * returns range - domain
8074 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8076 int i;
8077 isl_space *dim;
8078 struct isl_set *result;
8080 if (!map)
8081 return NULL;
8083 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8084 map->dim, isl_dim_out),
8085 goto error);
8086 dim = isl_map_get_space(map);
8087 dim = isl_space_domain(dim);
8088 result = isl_set_alloc_space(dim, map->n, 0);
8089 if (!result)
8090 goto error;
8091 for (i = 0; i < map->n; ++i)
8092 result = isl_set_add_basic_set(result,
8093 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8094 isl_map_free(map);
8095 return result;
8096 error:
8097 isl_map_free(map);
8098 return NULL;
8102 * returns [domain -> range] -> range - domain
8104 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8105 __isl_take isl_basic_map *bmap)
8107 int i, k;
8108 isl_space *space;
8109 isl_basic_map *domain;
8110 int nparam, n;
8111 unsigned total;
8113 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8114 bmap->dim, isl_dim_out))
8115 isl_die(bmap->ctx, isl_error_invalid,
8116 "domain and range don't match", goto error);
8118 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8119 n = isl_basic_map_dim(bmap, isl_dim_in);
8121 space = isl_basic_map_get_space(bmap);
8122 space = isl_space_from_range(isl_space_domain(space));
8123 domain = isl_basic_map_universe(space);
8125 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8126 bmap = isl_basic_map_apply_range(bmap, domain);
8127 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8129 total = isl_basic_map_total_dim(bmap);
8131 for (i = 0; i < n; ++i) {
8132 k = isl_basic_map_alloc_equality(bmap);
8133 if (k < 0)
8134 goto error;
8135 isl_seq_clr(bmap->eq[k], 1 + total);
8136 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8137 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8138 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8141 bmap = isl_basic_map_gauss(bmap, NULL);
8142 return isl_basic_map_finalize(bmap);
8143 error:
8144 isl_basic_map_free(bmap);
8145 return NULL;
8149 * returns [domain -> range] -> range - domain
8151 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8153 int i;
8154 isl_space *domain_space;
8156 if (isl_map_check_equal_tuples(map) < 0)
8157 return isl_map_free(map);
8159 map = isl_map_cow(map);
8160 if (!map)
8161 return NULL;
8163 domain_space = isl_space_domain(isl_map_get_space(map));
8164 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8165 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8166 if (!map->dim)
8167 goto error;
8168 for (i = 0; i < map->n; ++i) {
8169 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8170 if (!map->p[i])
8171 goto error;
8173 map = isl_map_unmark_normalized(map);
8174 return map;
8175 error:
8176 isl_map_free(map);
8177 return NULL;
8180 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8182 unsigned n_in, n_out;
8184 if (!space)
8185 return NULL;
8186 n_in = isl_space_dim(space, isl_dim_in);
8187 n_out = isl_space_dim(space, isl_dim_out);
8188 if (n_in != n_out)
8189 isl_die(space->ctx, isl_error_invalid,
8190 "number of input and output dimensions needs to be "
8191 "the same", goto error);
8192 return isl_basic_map_equal(space, n_in);
8193 error:
8194 isl_space_free(space);
8195 return NULL;
8198 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8200 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8203 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8205 isl_space *dim = isl_set_get_space(set);
8206 isl_map *id;
8207 id = isl_map_identity(isl_space_map_from_set(dim));
8208 return isl_map_intersect_range(id, set);
8211 /* Construct a basic set with all set dimensions having only non-negative
8212 * values.
8214 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8215 __isl_take isl_space *space)
8217 int i;
8218 unsigned nparam;
8219 unsigned dim;
8220 struct isl_basic_set *bset;
8222 if (!space)
8223 return NULL;
8224 nparam = space->nparam;
8225 dim = space->n_out;
8226 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8227 if (!bset)
8228 return NULL;
8229 for (i = 0; i < dim; ++i) {
8230 int k = isl_basic_set_alloc_inequality(bset);
8231 if (k < 0)
8232 goto error;
8233 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8234 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8236 return bset;
8237 error:
8238 isl_basic_set_free(bset);
8239 return NULL;
8242 /* Construct the half-space x_pos >= 0.
8244 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8245 int pos)
8247 int k;
8248 isl_basic_set *nonneg;
8250 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8251 k = isl_basic_set_alloc_inequality(nonneg);
8252 if (k < 0)
8253 goto error;
8254 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8255 isl_int_set_si(nonneg->ineq[k][pos], 1);
8257 return isl_basic_set_finalize(nonneg);
8258 error:
8259 isl_basic_set_free(nonneg);
8260 return NULL;
8263 /* Construct the half-space x_pos <= -1.
8265 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8266 int pos)
8268 int k;
8269 isl_basic_set *neg;
8271 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8272 k = isl_basic_set_alloc_inequality(neg);
8273 if (k < 0)
8274 goto error;
8275 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8276 isl_int_set_si(neg->ineq[k][0], -1);
8277 isl_int_set_si(neg->ineq[k][pos], -1);
8279 return isl_basic_set_finalize(neg);
8280 error:
8281 isl_basic_set_free(neg);
8282 return NULL;
8285 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8286 enum isl_dim_type type, unsigned first, unsigned n)
8288 int i;
8289 unsigned offset;
8290 isl_basic_set *nonneg;
8291 isl_basic_set *neg;
8293 if (n == 0)
8294 return set;
8296 if (isl_set_check_range(set, type, first, n) < 0)
8297 return isl_set_free(set);
8299 offset = pos(set->dim, type);
8300 for (i = 0; i < n; ++i) {
8301 nonneg = nonneg_halfspace(isl_set_get_space(set),
8302 offset + first + i);
8303 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8305 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8308 return set;
8311 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8312 int len,
8313 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8314 void *user)
8316 isl_set *half;
8318 if (!set)
8319 return isl_stat_error;
8320 if (isl_set_plain_is_empty(set)) {
8321 isl_set_free(set);
8322 return isl_stat_ok;
8324 if (first == len)
8325 return fn(set, signs, user);
8327 signs[first] = 1;
8328 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8329 1 + first));
8330 half = isl_set_intersect(half, isl_set_copy(set));
8331 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8332 goto error;
8334 signs[first] = -1;
8335 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8336 1 + first));
8337 half = isl_set_intersect(half, set);
8338 return foreach_orthant(half, signs, first + 1, len, fn, user);
8339 error:
8340 isl_set_free(set);
8341 return isl_stat_error;
8344 /* Call "fn" on the intersections of "set" with each of the orthants
8345 * (except for obviously empty intersections). The orthant is identified
8346 * by the signs array, with each entry having value 1 or -1 according
8347 * to the sign of the corresponding variable.
8349 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8350 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8351 void *user)
8353 unsigned nparam;
8354 unsigned nvar;
8355 int *signs;
8356 isl_stat r;
8358 if (!set)
8359 return isl_stat_error;
8360 if (isl_set_plain_is_empty(set))
8361 return isl_stat_ok;
8363 nparam = isl_set_dim(set, isl_dim_param);
8364 nvar = isl_set_dim(set, isl_dim_set);
8366 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8368 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8369 fn, user);
8371 free(signs);
8373 return r;
8376 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8378 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8381 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8382 __isl_keep isl_basic_map *bmap2)
8384 isl_bool is_subset;
8385 struct isl_map *map1;
8386 struct isl_map *map2;
8388 if (!bmap1 || !bmap2)
8389 return isl_bool_error;
8391 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8392 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8394 is_subset = isl_map_is_subset(map1, map2);
8396 isl_map_free(map1);
8397 isl_map_free(map2);
8399 return is_subset;
8402 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8403 __isl_keep isl_basic_set *bset2)
8405 return isl_basic_map_is_subset(bset1, bset2);
8408 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8409 __isl_keep isl_basic_map *bmap2)
8411 isl_bool is_subset;
8413 if (!bmap1 || !bmap2)
8414 return isl_bool_error;
8415 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8416 if (is_subset != isl_bool_true)
8417 return is_subset;
8418 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8419 return is_subset;
8422 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8423 __isl_keep isl_basic_set *bset2)
8425 return isl_basic_map_is_equal(
8426 bset_to_bmap(bset1), bset_to_bmap(bset2));
8429 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8431 int i;
8432 int is_empty;
8434 if (!map)
8435 return isl_bool_error;
8436 for (i = 0; i < map->n; ++i) {
8437 is_empty = isl_basic_map_is_empty(map->p[i]);
8438 if (is_empty < 0)
8439 return isl_bool_error;
8440 if (!is_empty)
8441 return isl_bool_false;
8443 return isl_bool_true;
8446 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8448 return map ? map->n == 0 : isl_bool_error;
8451 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8453 return set ? set->n == 0 : isl_bool_error;
8456 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8458 return isl_map_is_empty(set_to_map(set));
8461 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8462 __isl_keep isl_map *map2)
8464 if (!map1 || !map2)
8465 return isl_bool_error;
8467 return isl_space_is_equal(map1->dim, map2->dim);
8470 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8471 __isl_keep isl_set *set2)
8473 if (!set1 || !set2)
8474 return isl_bool_error;
8476 return isl_space_is_equal(set1->dim, set2->dim);
8479 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8481 isl_bool is_subset;
8483 if (!map1 || !map2)
8484 return isl_bool_error;
8485 is_subset = isl_map_is_subset(map1, map2);
8486 if (is_subset != isl_bool_true)
8487 return is_subset;
8488 is_subset = isl_map_is_subset(map2, map1);
8489 return is_subset;
8492 /* Is "map1" equal to "map2"?
8494 * First check if they are obviously equal.
8495 * If not, then perform a more detailed analysis.
8497 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8499 isl_bool equal;
8501 equal = isl_map_plain_is_equal(map1, map2);
8502 if (equal < 0 || equal)
8503 return equal;
8504 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8507 isl_bool isl_basic_map_is_strict_subset(
8508 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8510 isl_bool is_subset;
8512 if (!bmap1 || !bmap2)
8513 return isl_bool_error;
8514 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8515 if (is_subset != isl_bool_true)
8516 return is_subset;
8517 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8518 return isl_bool_not(is_subset);
8521 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8522 __isl_keep isl_map *map2)
8524 isl_bool is_subset;
8526 if (!map1 || !map2)
8527 return isl_bool_error;
8528 is_subset = isl_map_is_subset(map1, map2);
8529 if (is_subset != isl_bool_true)
8530 return is_subset;
8531 is_subset = isl_map_is_subset(map2, map1);
8532 return isl_bool_not(is_subset);
8535 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8536 __isl_keep isl_set *set2)
8538 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8541 /* Is "bmap" obviously equal to the universe with the same space?
8543 * That is, does it not have any constraints?
8545 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8547 if (!bmap)
8548 return isl_bool_error;
8549 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8552 /* Is "bset" obviously equal to the universe with the same space?
8554 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8556 return isl_basic_map_plain_is_universe(bset);
8559 /* If "c" does not involve any existentially quantified variables,
8560 * then set *univ to false and abort
8562 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8564 isl_bool *univ = user;
8565 unsigned n;
8567 n = isl_constraint_dim(c, isl_dim_div);
8568 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8569 isl_constraint_free(c);
8570 if (*univ < 0 || !*univ)
8571 return isl_stat_error;
8572 return isl_stat_ok;
8575 /* Is "bmap" equal to the universe with the same space?
8577 * First check if it is obviously equal to the universe.
8578 * If not and if there are any constraints not involving
8579 * existentially quantified variables, then it is certainly
8580 * not equal to the universe.
8581 * Otherwise, check if the universe is a subset of "bmap".
8583 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8585 isl_bool univ;
8586 isl_basic_map *test;
8588 univ = isl_basic_map_plain_is_universe(bmap);
8589 if (univ < 0 || univ)
8590 return univ;
8591 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8592 return isl_bool_false;
8593 univ = isl_bool_true;
8594 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8595 univ)
8596 return isl_bool_error;
8597 if (univ < 0 || !univ)
8598 return univ;
8599 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8600 univ = isl_basic_map_is_subset(test, bmap);
8601 isl_basic_map_free(test);
8602 return univ;
8605 /* Is "bset" equal to the universe with the same space?
8607 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8609 return isl_basic_map_is_universe(bset);
8612 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8614 int i;
8616 if (!map)
8617 return isl_bool_error;
8619 for (i = 0; i < map->n; ++i) {
8620 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8621 if (r < 0 || r)
8622 return r;
8625 return isl_bool_false;
8628 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8630 return isl_map_plain_is_universe(set_to_map(set));
8633 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8635 struct isl_basic_set *bset = NULL;
8636 struct isl_vec *sample = NULL;
8637 isl_bool empty, non_empty;
8639 if (!bmap)
8640 return isl_bool_error;
8642 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8643 return isl_bool_true;
8645 if (isl_basic_map_plain_is_universe(bmap))
8646 return isl_bool_false;
8648 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8649 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8650 copy = isl_basic_map_remove_redundancies(copy);
8651 empty = isl_basic_map_plain_is_empty(copy);
8652 isl_basic_map_free(copy);
8653 return empty;
8656 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8657 if (non_empty < 0)
8658 return isl_bool_error;
8659 if (non_empty)
8660 return isl_bool_false;
8661 isl_vec_free(bmap->sample);
8662 bmap->sample = NULL;
8663 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8664 if (!bset)
8665 return isl_bool_error;
8666 sample = isl_basic_set_sample_vec(bset);
8667 if (!sample)
8668 return isl_bool_error;
8669 empty = sample->size == 0;
8670 isl_vec_free(bmap->sample);
8671 bmap->sample = sample;
8672 if (empty)
8673 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8675 return empty;
8678 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8680 if (!bmap)
8681 return isl_bool_error;
8682 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8685 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8687 if (!bset)
8688 return isl_bool_error;
8689 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8692 /* Is "bmap" known to be non-empty?
8694 * That is, is the cached sample still valid?
8696 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8698 unsigned total;
8700 if (!bmap)
8701 return isl_bool_error;
8702 if (!bmap->sample)
8703 return isl_bool_false;
8704 total = 1 + isl_basic_map_total_dim(bmap);
8705 if (bmap->sample->size != total)
8706 return isl_bool_false;
8707 return isl_basic_map_contains(bmap, bmap->sample);
8710 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8712 return isl_basic_map_is_empty(bset_to_bmap(bset));
8715 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8716 __isl_take isl_basic_map *bmap2)
8718 struct isl_map *map;
8719 if (!bmap1 || !bmap2)
8720 goto error;
8722 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8724 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8725 if (!map)
8726 goto error;
8727 map = isl_map_add_basic_map(map, bmap1);
8728 map = isl_map_add_basic_map(map, bmap2);
8729 return map;
8730 error:
8731 isl_basic_map_free(bmap1);
8732 isl_basic_map_free(bmap2);
8733 return NULL;
8736 struct isl_set *isl_basic_set_union(
8737 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8739 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8740 bset_to_bmap(bset2)));
8743 /* Order divs such that any div only depends on previous divs */
8744 __isl_give isl_basic_map *isl_basic_map_order_divs(
8745 __isl_take isl_basic_map *bmap)
8747 int i;
8748 unsigned off;
8750 if (!bmap)
8751 return NULL;
8753 off = isl_space_dim(bmap->dim, isl_dim_all);
8755 for (i = 0; i < bmap->n_div; ++i) {
8756 int pos;
8757 if (isl_int_is_zero(bmap->div[i][0]))
8758 continue;
8759 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8760 bmap->n_div-i);
8761 if (pos == -1)
8762 continue;
8763 if (pos == 0)
8764 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8765 "integer division depends on itself",
8766 return isl_basic_map_free(bmap));
8767 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8768 if (!bmap)
8769 return NULL;
8770 --i;
8772 return bmap;
8775 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8777 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8780 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8782 int i;
8784 if (!map)
8785 return 0;
8787 for (i = 0; i < map->n; ++i) {
8788 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8789 if (!map->p[i])
8790 goto error;
8793 return map;
8794 error:
8795 isl_map_free(map);
8796 return NULL;
8799 /* Sort the local variables of "bset".
8801 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8802 __isl_take isl_basic_set *bset)
8804 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8807 /* Apply the expansion computed by isl_merge_divs.
8808 * The expansion itself is given by "exp" while the resulting
8809 * list of divs is given by "div".
8811 * Move the integer divisions of "bmap" into the right position
8812 * according to "exp" and then introduce the additional integer
8813 * divisions, adding div constraints.
8814 * The moving should be done first to avoid moving coefficients
8815 * in the definitions of the extra integer divisions.
8817 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8818 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8820 int i, j;
8821 int n_div;
8823 bmap = isl_basic_map_cow(bmap);
8824 if (!bmap || !div)
8825 goto error;
8827 if (div->n_row < bmap->n_div)
8828 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8829 "not an expansion", goto error);
8831 n_div = bmap->n_div;
8832 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8833 div->n_row - n_div, 0,
8834 2 * (div->n_row - n_div));
8836 for (i = n_div; i < div->n_row; ++i)
8837 if (isl_basic_map_alloc_div(bmap) < 0)
8838 goto error;
8840 for (j = n_div - 1; j >= 0; --j) {
8841 if (exp[j] == j)
8842 break;
8843 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8844 if (!bmap)
8845 goto error;
8847 j = 0;
8848 for (i = 0; i < div->n_row; ++i) {
8849 if (j < n_div && exp[j] == i) {
8850 j++;
8851 } else {
8852 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8853 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8854 continue;
8855 bmap = isl_basic_map_add_div_constraints(bmap, i);
8856 if (!bmap)
8857 goto error;
8861 isl_mat_free(div);
8862 return bmap;
8863 error:
8864 isl_basic_map_free(bmap);
8865 isl_mat_free(div);
8866 return NULL;
8869 /* Apply the expansion computed by isl_merge_divs.
8870 * The expansion itself is given by "exp" while the resulting
8871 * list of divs is given by "div".
8873 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8874 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8876 return isl_basic_map_expand_divs(bset, div, exp);
8879 /* Look for a div in dst that corresponds to the div "div" in src.
8880 * The divs before "div" in src and dst are assumed to be the same.
8882 * Returns -1 if no corresponding div was found and the position
8883 * of the corresponding div in dst otherwise.
8885 static int find_div(__isl_keep isl_basic_map *dst,
8886 __isl_keep isl_basic_map *src, unsigned div)
8888 int i;
8890 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8892 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8893 for (i = div; i < dst->n_div; ++i)
8894 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8895 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8896 dst->n_div - div) == -1)
8897 return i;
8898 return -1;
8901 /* Align the divs of "dst" to those of "src", adding divs from "src"
8902 * if needed. That is, make sure that the first src->n_div divs
8903 * of the result are equal to those of src.
8905 * The result is not finalized as by design it will have redundant
8906 * divs if any divs from "src" were copied.
8908 __isl_give isl_basic_map *isl_basic_map_align_divs(
8909 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8911 int i;
8912 isl_bool known;
8913 int extended;
8914 unsigned total;
8916 if (!dst || !src)
8917 return isl_basic_map_free(dst);
8919 if (src->n_div == 0)
8920 return dst;
8922 known = isl_basic_map_divs_known(src);
8923 if (known < 0)
8924 return isl_basic_map_free(dst);
8925 if (!known)
8926 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8927 "some src divs are unknown",
8928 return isl_basic_map_free(dst));
8930 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8931 if (!src)
8932 return isl_basic_map_free(dst);
8934 extended = 0;
8935 total = isl_space_dim(src->dim, isl_dim_all);
8936 for (i = 0; i < src->n_div; ++i) {
8937 int j = find_div(dst, src, i);
8938 if (j < 0) {
8939 if (!extended) {
8940 int extra = src->n_div - i;
8941 dst = isl_basic_map_cow(dst);
8942 if (!dst)
8943 goto error;
8944 dst = isl_basic_map_extend_space(dst,
8945 isl_space_copy(dst->dim),
8946 extra, 0, 2 * extra);
8947 extended = 1;
8949 j = isl_basic_map_alloc_div(dst);
8950 if (j < 0)
8951 goto error;
8952 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8953 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8954 dst = isl_basic_map_add_div_constraints(dst, j);
8955 if (!dst)
8956 goto error;
8958 if (j != i)
8959 dst = isl_basic_map_swap_div(dst, i, j);
8960 if (!dst)
8961 goto error;
8963 isl_basic_map_free(src);
8964 return dst;
8965 error:
8966 isl_basic_map_free(src);
8967 isl_basic_map_free(dst);
8968 return NULL;
8971 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8973 int i;
8975 if (!map)
8976 return NULL;
8977 if (map->n == 0)
8978 return map;
8979 map = isl_map_compute_divs(map);
8980 map = isl_map_cow(map);
8981 if (!map)
8982 return NULL;
8984 for (i = 1; i < map->n; ++i)
8985 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8986 for (i = 1; i < map->n; ++i) {
8987 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8988 if (!map->p[i])
8989 return isl_map_free(map);
8992 map = isl_map_unmark_normalized(map);
8993 return map;
8996 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8998 return isl_map_align_divs_internal(map);
9001 struct isl_set *isl_set_align_divs(struct isl_set *set)
9003 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9006 /* Align the divs of the basic maps in "map" to those
9007 * of the basic maps in "list", as well as to the other basic maps in "map".
9008 * The elements in "list" are assumed to have known divs.
9010 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9011 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9013 int i, n;
9015 map = isl_map_compute_divs(map);
9016 map = isl_map_cow(map);
9017 if (!map || !list)
9018 return isl_map_free(map);
9019 if (map->n == 0)
9020 return map;
9022 n = isl_basic_map_list_n_basic_map(list);
9023 for (i = 0; i < n; ++i) {
9024 isl_basic_map *bmap;
9026 bmap = isl_basic_map_list_get_basic_map(list, i);
9027 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9028 isl_basic_map_free(bmap);
9030 if (!map->p[0])
9031 return isl_map_free(map);
9033 return isl_map_align_divs_internal(map);
9036 /* Align the divs of each element of "list" to those of "bmap".
9037 * Both "bmap" and the elements of "list" are assumed to have known divs.
9039 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9040 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9042 int i, n;
9044 if (!list || !bmap)
9045 return isl_basic_map_list_free(list);
9047 n = isl_basic_map_list_n_basic_map(list);
9048 for (i = 0; i < n; ++i) {
9049 isl_basic_map *bmap_i;
9051 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9052 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9053 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9056 return list;
9059 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9060 __isl_take isl_map *map)
9062 isl_bool ok;
9064 ok = isl_map_compatible_domain(map, set);
9065 if (ok < 0)
9066 goto error;
9067 if (!ok)
9068 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9069 "incompatible spaces", goto error);
9070 map = isl_map_intersect_domain(map, set);
9071 set = isl_map_range(map);
9072 return set;
9073 error:
9074 isl_set_free(set);
9075 isl_map_free(map);
9076 return NULL;
9079 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9080 __isl_take isl_map *map)
9082 return isl_map_align_params_map_map_and(set, map, &set_apply);
9085 /* There is no need to cow as removing empty parts doesn't change
9086 * the meaning of the set.
9088 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9090 int i;
9092 if (!map)
9093 return NULL;
9095 for (i = map->n - 1; i >= 0; --i)
9096 map = remove_if_empty(map, i);
9098 return map;
9101 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9103 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9106 /* Create a binary relation that maps the shared initial "pos" dimensions
9107 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9109 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9110 __isl_keep isl_basic_set *bset2, int pos)
9112 isl_basic_map *bmap1;
9113 isl_basic_map *bmap2;
9115 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9116 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9117 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9118 isl_dim_out, 0, pos);
9119 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9120 isl_dim_out, 0, pos);
9121 return isl_basic_map_range_product(bmap1, bmap2);
9124 /* Given two basic sets bset1 and bset2, compute the maximal difference
9125 * between the values of dimension pos in bset1 and those in bset2
9126 * for any common value of the parameters and dimensions preceding pos.
9128 static enum isl_lp_result basic_set_maximal_difference_at(
9129 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9130 int pos, isl_int *opt)
9132 isl_basic_map *bmap1;
9133 struct isl_ctx *ctx;
9134 struct isl_vec *obj;
9135 unsigned total;
9136 unsigned nparam;
9137 unsigned dim1;
9138 enum isl_lp_result res;
9140 if (!bset1 || !bset2)
9141 return isl_lp_error;
9143 nparam = isl_basic_set_n_param(bset1);
9144 dim1 = isl_basic_set_n_dim(bset1);
9146 bmap1 = join_initial(bset1, bset2, pos);
9147 if (!bmap1)
9148 return isl_lp_error;
9150 total = isl_basic_map_total_dim(bmap1);
9151 ctx = bmap1->ctx;
9152 obj = isl_vec_alloc(ctx, 1 + total);
9153 if (!obj)
9154 goto error;
9155 isl_seq_clr(obj->block.data, 1 + total);
9156 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9157 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9158 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9159 opt, NULL, NULL);
9160 isl_basic_map_free(bmap1);
9161 isl_vec_free(obj);
9162 return res;
9163 error:
9164 isl_basic_map_free(bmap1);
9165 return isl_lp_error;
9168 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9169 * for any common value of the parameters and dimensions preceding pos
9170 * in both basic sets, the values of dimension pos in bset1 are
9171 * smaller or larger than those in bset2.
9173 * Returns
9174 * 1 if bset1 follows bset2
9175 * -1 if bset1 precedes bset2
9176 * 0 if bset1 and bset2 are incomparable
9177 * -2 if some error occurred.
9179 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9180 __isl_keep isl_basic_set *bset2, int pos)
9182 isl_int opt;
9183 enum isl_lp_result res;
9184 int cmp;
9186 isl_int_init(opt);
9188 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9190 if (res == isl_lp_empty)
9191 cmp = 0;
9192 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9193 res == isl_lp_unbounded)
9194 cmp = 1;
9195 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9196 cmp = -1;
9197 else
9198 cmp = -2;
9200 isl_int_clear(opt);
9201 return cmp;
9204 /* Given two basic sets bset1 and bset2, check whether
9205 * for any common value of the parameters and dimensions preceding pos
9206 * there is a value of dimension pos in bset1 that is larger
9207 * than a value of the same dimension in bset2.
9209 * Return
9210 * 1 if there exists such a pair
9211 * 0 if there is no such pair, but there is a pair of equal values
9212 * -1 otherwise
9213 * -2 if some error occurred.
9215 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9216 __isl_keep isl_basic_set *bset2, int pos)
9218 isl_bool empty;
9219 isl_basic_map *bmap;
9220 unsigned dim1;
9222 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9223 bmap = join_initial(bset1, bset2, pos);
9224 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9225 isl_dim_out, dim1 - pos);
9226 empty = isl_basic_map_is_empty(bmap);
9227 if (empty < 0)
9228 goto error;
9229 if (empty) {
9230 isl_basic_map_free(bmap);
9231 return -1;
9233 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9234 isl_dim_out, dim1 - pos);
9235 empty = isl_basic_map_is_empty(bmap);
9236 if (empty < 0)
9237 goto error;
9238 isl_basic_map_free(bmap);
9239 if (empty)
9240 return 0;
9241 return 1;
9242 error:
9243 isl_basic_map_free(bmap);
9244 return -2;
9247 /* Given two sets set1 and set2, check whether
9248 * for any common value of the parameters and dimensions preceding pos
9249 * there is a value of dimension pos in set1 that is larger
9250 * than a value of the same dimension in set2.
9252 * Return
9253 * 1 if there exists such a pair
9254 * 0 if there is no such pair, but there is a pair of equal values
9255 * -1 otherwise
9256 * -2 if some error occurred.
9258 int isl_set_follows_at(__isl_keep isl_set *set1,
9259 __isl_keep isl_set *set2, int pos)
9261 int i, j;
9262 int follows = -1;
9264 if (!set1 || !set2)
9265 return -2;
9267 for (i = 0; i < set1->n; ++i)
9268 for (j = 0; j < set2->n; ++j) {
9269 int f;
9270 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9271 if (f == 1 || f == -2)
9272 return f;
9273 if (f > follows)
9274 follows = f;
9277 return follows;
9280 static isl_bool isl_basic_map_plain_has_fixed_var(
9281 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9283 int i;
9284 int d;
9285 unsigned total;
9287 if (!bmap)
9288 return isl_bool_error;
9289 total = isl_basic_map_total_dim(bmap);
9290 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9291 for (; d+1 > pos; --d)
9292 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9293 break;
9294 if (d != pos)
9295 continue;
9296 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9297 return isl_bool_false;
9298 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9299 return isl_bool_false;
9300 if (!isl_int_is_one(bmap->eq[i][1+d]))
9301 return isl_bool_false;
9302 if (val)
9303 isl_int_neg(*val, bmap->eq[i][0]);
9304 return isl_bool_true;
9306 return isl_bool_false;
9309 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9310 unsigned pos, isl_int *val)
9312 int i;
9313 isl_int v;
9314 isl_int tmp;
9315 isl_bool fixed;
9317 if (!map)
9318 return isl_bool_error;
9319 if (map->n == 0)
9320 return isl_bool_false;
9321 if (map->n == 1)
9322 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9323 isl_int_init(v);
9324 isl_int_init(tmp);
9325 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9326 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9327 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9328 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9329 fixed = isl_bool_false;
9331 if (val)
9332 isl_int_set(*val, v);
9333 isl_int_clear(tmp);
9334 isl_int_clear(v);
9335 return fixed;
9338 static isl_bool isl_basic_set_plain_has_fixed_var(
9339 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9341 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9342 pos, val);
9345 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9346 enum isl_dim_type type, unsigned pos, isl_int *val)
9348 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9349 return isl_bool_error;
9350 return isl_basic_map_plain_has_fixed_var(bmap,
9351 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9354 /* If "bmap" obviously lies on a hyperplane where the given dimension
9355 * has a fixed value, then return that value.
9356 * Otherwise return NaN.
9358 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9359 __isl_keep isl_basic_map *bmap,
9360 enum isl_dim_type type, unsigned pos)
9362 isl_ctx *ctx;
9363 isl_val *v;
9364 isl_bool fixed;
9366 if (!bmap)
9367 return NULL;
9368 ctx = isl_basic_map_get_ctx(bmap);
9369 v = isl_val_alloc(ctx);
9370 if (!v)
9371 return NULL;
9372 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9373 if (fixed < 0)
9374 return isl_val_free(v);
9375 if (fixed) {
9376 isl_int_set_si(v->d, 1);
9377 return v;
9379 isl_val_free(v);
9380 return isl_val_nan(ctx);
9383 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9384 enum isl_dim_type type, unsigned pos, isl_int *val)
9386 if (isl_map_check_range(map, type, pos, 1) < 0)
9387 return isl_bool_error;
9388 return isl_map_plain_has_fixed_var(map,
9389 map_offset(map, type) - 1 + pos, val);
9392 /* If "map" obviously lies on a hyperplane where the given dimension
9393 * has a fixed value, then return that value.
9394 * Otherwise return NaN.
9396 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9397 enum isl_dim_type type, unsigned pos)
9399 isl_ctx *ctx;
9400 isl_val *v;
9401 isl_bool fixed;
9403 if (!map)
9404 return NULL;
9405 ctx = isl_map_get_ctx(map);
9406 v = isl_val_alloc(ctx);
9407 if (!v)
9408 return NULL;
9409 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9410 if (fixed < 0)
9411 return isl_val_free(v);
9412 if (fixed) {
9413 isl_int_set_si(v->d, 1);
9414 return v;
9416 isl_val_free(v);
9417 return isl_val_nan(ctx);
9420 /* If "set" obviously lies on a hyperplane where the given dimension
9421 * has a fixed value, then return that value.
9422 * Otherwise return NaN.
9424 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9425 enum isl_dim_type type, unsigned pos)
9427 return isl_map_plain_get_val_if_fixed(set, type, pos);
9430 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9431 * then return this fixed value in *val.
9433 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9434 unsigned dim, isl_int *val)
9436 return isl_basic_set_plain_has_fixed_var(bset,
9437 isl_basic_set_n_param(bset) + dim, val);
9440 /* Return -1 if the constraint "c1" should be sorted before "c2"
9441 * and 1 if it should be sorted after "c2".
9442 * Return 0 if the two constraints are the same (up to the constant term).
9444 * In particular, if a constraint involves later variables than another
9445 * then it is sorted after this other constraint.
9446 * uset_gist depends on constraints without existentially quantified
9447 * variables sorting first.
9449 * For constraints that have the same latest variable, those
9450 * with the same coefficient for this latest variable (first in absolute value
9451 * and then in actual value) are grouped together.
9452 * This is useful for detecting pairs of constraints that can
9453 * be chained in their printed representation.
9455 * Finally, within a group, constraints are sorted according to
9456 * their coefficients (excluding the constant term).
9458 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9460 isl_int **c1 = (isl_int **) p1;
9461 isl_int **c2 = (isl_int **) p2;
9462 int l1, l2;
9463 unsigned size = *(unsigned *) arg;
9464 int cmp;
9466 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9467 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9469 if (l1 != l2)
9470 return l1 - l2;
9472 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9473 if (cmp != 0)
9474 return cmp;
9475 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9476 if (cmp != 0)
9477 return -cmp;
9479 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9482 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9483 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9484 * and 0 if the two constraints are the same (up to the constant term).
9486 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9487 isl_int *c1, isl_int *c2)
9489 unsigned total;
9491 if (!bmap)
9492 return -2;
9493 total = isl_basic_map_total_dim(bmap);
9494 return sort_constraint_cmp(&c1, &c2, &total);
9497 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9498 __isl_take isl_basic_map *bmap)
9500 unsigned total;
9502 if (!bmap)
9503 return NULL;
9504 if (bmap->n_ineq == 0)
9505 return bmap;
9506 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9507 return bmap;
9508 total = isl_basic_map_total_dim(bmap);
9509 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9510 &sort_constraint_cmp, &total) < 0)
9511 return isl_basic_map_free(bmap);
9512 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9513 return bmap;
9516 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9517 __isl_take isl_basic_set *bset)
9519 isl_basic_map *bmap = bset_to_bmap(bset);
9520 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9523 __isl_give isl_basic_map *isl_basic_map_normalize(
9524 __isl_take isl_basic_map *bmap)
9526 bmap = isl_basic_map_remove_redundancies(bmap);
9527 bmap = isl_basic_map_sort_constraints(bmap);
9528 return bmap;
9530 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9531 __isl_keep isl_basic_map *bmap2)
9533 int i, cmp;
9534 unsigned total;
9535 isl_space *space1, *space2;
9537 if (!bmap1 || !bmap2)
9538 return -1;
9540 if (bmap1 == bmap2)
9541 return 0;
9542 space1 = isl_basic_map_peek_space(bmap1);
9543 space2 = isl_basic_map_peek_space(bmap2);
9544 cmp = isl_space_cmp(space1, space2);
9545 if (cmp)
9546 return cmp;
9547 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9548 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9549 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9550 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9551 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9552 return 0;
9553 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9554 return 1;
9555 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9556 return -1;
9557 if (bmap1->n_eq != bmap2->n_eq)
9558 return bmap1->n_eq - bmap2->n_eq;
9559 if (bmap1->n_ineq != bmap2->n_ineq)
9560 return bmap1->n_ineq - bmap2->n_ineq;
9561 if (bmap1->n_div != bmap2->n_div)
9562 return bmap1->n_div - bmap2->n_div;
9563 total = isl_basic_map_total_dim(bmap1);
9564 for (i = 0; i < bmap1->n_eq; ++i) {
9565 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9566 if (cmp)
9567 return cmp;
9569 for (i = 0; i < bmap1->n_ineq; ++i) {
9570 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9571 if (cmp)
9572 return cmp;
9574 for (i = 0; i < bmap1->n_div; ++i) {
9575 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9576 if (cmp)
9577 return cmp;
9579 return 0;
9582 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9583 __isl_keep isl_basic_set *bset2)
9585 return isl_basic_map_plain_cmp(bset1, bset2);
9588 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9590 int i, cmp;
9592 if (set1 == set2)
9593 return 0;
9594 if (set1->n != set2->n)
9595 return set1->n - set2->n;
9597 for (i = 0; i < set1->n; ++i) {
9598 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9599 if (cmp)
9600 return cmp;
9603 return 0;
9606 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9607 __isl_keep isl_basic_map *bmap2)
9609 if (!bmap1 || !bmap2)
9610 return isl_bool_error;
9611 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9614 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9615 __isl_keep isl_basic_set *bset2)
9617 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9618 bset_to_bmap(bset2));
9621 static int qsort_bmap_cmp(const void *p1, const void *p2)
9623 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9624 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9626 return isl_basic_map_plain_cmp(bmap1, bmap2);
9629 /* Sort the basic maps of "map" and remove duplicate basic maps.
9631 * While removing basic maps, we make sure that the basic maps remain
9632 * sorted because isl_map_normalize expects the basic maps of the result
9633 * to be sorted.
9635 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9637 int i, j;
9639 map = isl_map_remove_empty_parts(map);
9640 if (!map)
9641 return NULL;
9642 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9643 for (i = map->n - 1; i >= 1; --i) {
9644 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9645 continue;
9646 isl_basic_map_free(map->p[i-1]);
9647 for (j = i; j < map->n; ++j)
9648 map->p[j - 1] = map->p[j];
9649 map->n--;
9652 return map;
9655 /* Remove obvious duplicates among the basic maps of "map".
9657 * Unlike isl_map_normalize, this function does not remove redundant
9658 * constraints and only removes duplicates that have exactly the same
9659 * constraints in the input. It does sort the constraints and
9660 * the basic maps to ease the detection of duplicates.
9662 * If "map" has already been normalized or if the basic maps are
9663 * disjoint, then there can be no duplicates.
9665 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9667 int i;
9668 isl_basic_map *bmap;
9670 if (!map)
9671 return NULL;
9672 if (map->n <= 1)
9673 return map;
9674 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9675 return map;
9676 for (i = 0; i < map->n; ++i) {
9677 bmap = isl_basic_map_copy(map->p[i]);
9678 bmap = isl_basic_map_sort_constraints(bmap);
9679 if (!bmap)
9680 return isl_map_free(map);
9681 isl_basic_map_free(map->p[i]);
9682 map->p[i] = bmap;
9685 map = sort_and_remove_duplicates(map);
9686 return map;
9689 /* We normalize in place, but if anything goes wrong we need
9690 * to return NULL, so we need to make sure we don't change the
9691 * meaning of any possible other copies of map.
9693 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9695 int i;
9696 struct isl_basic_map *bmap;
9698 if (!map)
9699 return NULL;
9700 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9701 return map;
9702 for (i = 0; i < map->n; ++i) {
9703 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9704 if (!bmap)
9705 goto error;
9706 isl_basic_map_free(map->p[i]);
9707 map->p[i] = bmap;
9710 map = sort_and_remove_duplicates(map);
9711 if (map)
9712 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9713 return map;
9714 error:
9715 isl_map_free(map);
9716 return NULL;
9719 struct isl_set *isl_set_normalize(struct isl_set *set)
9721 return set_from_map(isl_map_normalize(set_to_map(set)));
9724 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9725 __isl_keep isl_map *map2)
9727 int i;
9728 isl_bool equal;
9730 if (!map1 || !map2)
9731 return isl_bool_error;
9733 if (map1 == map2)
9734 return isl_bool_true;
9735 if (!isl_space_is_equal(map1->dim, map2->dim))
9736 return isl_bool_false;
9738 map1 = isl_map_copy(map1);
9739 map2 = isl_map_copy(map2);
9740 map1 = isl_map_normalize(map1);
9741 map2 = isl_map_normalize(map2);
9742 if (!map1 || !map2)
9743 goto error;
9744 equal = map1->n == map2->n;
9745 for (i = 0; equal && i < map1->n; ++i) {
9746 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9747 if (equal < 0)
9748 goto error;
9750 isl_map_free(map1);
9751 isl_map_free(map2);
9752 return equal;
9753 error:
9754 isl_map_free(map1);
9755 isl_map_free(map2);
9756 return isl_bool_error;
9759 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9760 __isl_keep isl_set *set2)
9762 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9765 /* Return the basic maps in "map" as a list.
9767 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9768 __isl_keep isl_map *map)
9770 int i;
9771 isl_ctx *ctx;
9772 isl_basic_map_list *list;
9774 if (!map)
9775 return NULL;
9776 ctx = isl_map_get_ctx(map);
9777 list = isl_basic_map_list_alloc(ctx, map->n);
9779 for (i = 0; i < map->n; ++i) {
9780 isl_basic_map *bmap;
9782 bmap = isl_basic_map_copy(map->p[i]);
9783 list = isl_basic_map_list_add(list, bmap);
9786 return list;
9789 /* Return the intersection of the elements in the non-empty list "list".
9790 * All elements are assumed to live in the same space.
9792 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9793 __isl_take isl_basic_map_list *list)
9795 int i, n;
9796 isl_basic_map *bmap;
9798 if (!list)
9799 return NULL;
9800 n = isl_basic_map_list_n_basic_map(list);
9801 if (n < 1)
9802 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9803 "expecting non-empty list", goto error);
9805 bmap = isl_basic_map_list_get_basic_map(list, 0);
9806 for (i = 1; i < n; ++i) {
9807 isl_basic_map *bmap_i;
9809 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9810 bmap = isl_basic_map_intersect(bmap, bmap_i);
9813 isl_basic_map_list_free(list);
9814 return bmap;
9815 error:
9816 isl_basic_map_list_free(list);
9817 return NULL;
9820 /* Return the intersection of the elements in the non-empty list "list".
9821 * All elements are assumed to live in the same space.
9823 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9824 __isl_take isl_basic_set_list *list)
9826 return isl_basic_map_list_intersect(list);
9829 /* Return the union of the elements of "list".
9830 * The list is required to have at least one element.
9832 __isl_give isl_set *isl_basic_set_list_union(
9833 __isl_take isl_basic_set_list *list)
9835 int i, n;
9836 isl_space *space;
9837 isl_basic_set *bset;
9838 isl_set *set;
9840 if (!list)
9841 return NULL;
9842 n = isl_basic_set_list_n_basic_set(list);
9843 if (n < 1)
9844 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9845 "expecting non-empty list", goto error);
9847 bset = isl_basic_set_list_get_basic_set(list, 0);
9848 space = isl_basic_set_get_space(bset);
9849 isl_basic_set_free(bset);
9851 set = isl_set_alloc_space(space, n, 0);
9852 for (i = 0; i < n; ++i) {
9853 bset = isl_basic_set_list_get_basic_set(list, i);
9854 set = isl_set_add_basic_set(set, bset);
9857 isl_basic_set_list_free(list);
9858 return set;
9859 error:
9860 isl_basic_set_list_free(list);
9861 return NULL;
9864 /* Return the union of the elements in the non-empty list "list".
9865 * All elements are assumed to live in the same space.
9867 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9869 int i, n;
9870 isl_set *set;
9872 if (!list)
9873 return NULL;
9874 n = isl_set_list_n_set(list);
9875 if (n < 1)
9876 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9877 "expecting non-empty list", goto error);
9879 set = isl_set_list_get_set(list, 0);
9880 for (i = 1; i < n; ++i) {
9881 isl_set *set_i;
9883 set_i = isl_set_list_get_set(list, i);
9884 set = isl_set_union(set, set_i);
9887 isl_set_list_free(list);
9888 return set;
9889 error:
9890 isl_set_list_free(list);
9891 return NULL;
9894 __isl_give isl_basic_map *isl_basic_map_product(
9895 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9897 isl_space *space_result = NULL;
9898 struct isl_basic_map *bmap;
9899 unsigned in1, in2, out1, out2, nparam, total, pos;
9900 struct isl_dim_map *dim_map1, *dim_map2;
9902 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9903 goto error;
9904 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9905 isl_space_copy(bmap2->dim));
9907 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9908 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9909 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9910 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9911 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9913 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9914 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9915 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9916 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9917 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9918 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9919 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9920 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9921 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9922 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9923 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9925 bmap = isl_basic_map_alloc_space(space_result,
9926 bmap1->n_div + bmap2->n_div,
9927 bmap1->n_eq + bmap2->n_eq,
9928 bmap1->n_ineq + bmap2->n_ineq);
9929 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9930 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9931 bmap = isl_basic_map_simplify(bmap);
9932 return isl_basic_map_finalize(bmap);
9933 error:
9934 isl_basic_map_free(bmap1);
9935 isl_basic_map_free(bmap2);
9936 return NULL;
9939 __isl_give isl_basic_map *isl_basic_map_flat_product(
9940 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9942 isl_basic_map *prod;
9944 prod = isl_basic_map_product(bmap1, bmap2);
9945 prod = isl_basic_map_flatten(prod);
9946 return prod;
9949 __isl_give isl_basic_set *isl_basic_set_flat_product(
9950 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9952 return isl_basic_map_flat_range_product(bset1, bset2);
9955 __isl_give isl_basic_map *isl_basic_map_domain_product(
9956 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9958 isl_space *space_result = NULL;
9959 isl_basic_map *bmap;
9960 unsigned in1, in2, out, nparam, total, pos;
9961 struct isl_dim_map *dim_map1, *dim_map2;
9963 if (!bmap1 || !bmap2)
9964 goto error;
9966 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9967 isl_space_copy(bmap2->dim));
9969 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9970 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9971 out = isl_basic_map_dim(bmap1, isl_dim_out);
9972 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9974 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9975 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9976 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9977 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9978 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9979 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9980 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9981 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9982 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9983 isl_dim_map_div(dim_map1, bmap1, pos += out);
9984 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9986 bmap = isl_basic_map_alloc_space(space_result,
9987 bmap1->n_div + bmap2->n_div,
9988 bmap1->n_eq + bmap2->n_eq,
9989 bmap1->n_ineq + bmap2->n_ineq);
9990 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9991 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9992 bmap = isl_basic_map_simplify(bmap);
9993 return isl_basic_map_finalize(bmap);
9994 error:
9995 isl_basic_map_free(bmap1);
9996 isl_basic_map_free(bmap2);
9997 return NULL;
10000 __isl_give isl_basic_map *isl_basic_map_range_product(
10001 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10003 isl_bool rational;
10004 isl_space *space_result = NULL;
10005 isl_basic_map *bmap;
10006 unsigned in, out1, out2, nparam, total, pos;
10007 struct isl_dim_map *dim_map1, *dim_map2;
10009 rational = isl_basic_map_is_rational(bmap1);
10010 if (rational >= 0 && rational)
10011 rational = isl_basic_map_is_rational(bmap2);
10012 if (!bmap1 || !bmap2 || rational < 0)
10013 goto error;
10015 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10016 goto error;
10018 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10019 isl_space_copy(bmap2->dim));
10021 in = isl_basic_map_dim(bmap1, isl_dim_in);
10022 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10023 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10024 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10026 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10027 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10028 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10029 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10030 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10031 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10032 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10033 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10034 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10035 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10036 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10038 bmap = isl_basic_map_alloc_space(space_result,
10039 bmap1->n_div + bmap2->n_div,
10040 bmap1->n_eq + bmap2->n_eq,
10041 bmap1->n_ineq + bmap2->n_ineq);
10042 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10043 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10044 if (rational)
10045 bmap = isl_basic_map_set_rational(bmap);
10046 bmap = isl_basic_map_simplify(bmap);
10047 return isl_basic_map_finalize(bmap);
10048 error:
10049 isl_basic_map_free(bmap1);
10050 isl_basic_map_free(bmap2);
10051 return NULL;
10054 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10055 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10057 isl_basic_map *prod;
10059 prod = isl_basic_map_range_product(bmap1, bmap2);
10060 prod = isl_basic_map_flatten_range(prod);
10061 return prod;
10064 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10065 * and collect the results.
10066 * The result live in the space obtained by calling "space_product"
10067 * on the spaces of "map1" and "map2".
10068 * If "remove_duplicates" is set then the result may contain duplicates
10069 * (even if the inputs do not) and so we try and remove the obvious
10070 * duplicates.
10072 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10073 __isl_take isl_map *map2,
10074 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10075 __isl_take isl_space *right),
10076 __isl_give isl_basic_map *(*basic_map_product)(
10077 __isl_take isl_basic_map *left,
10078 __isl_take isl_basic_map *right),
10079 int remove_duplicates)
10081 unsigned flags = 0;
10082 struct isl_map *result;
10083 int i, j;
10084 isl_bool m;
10086 m = isl_map_has_equal_params(map1, map2);
10087 if (m < 0)
10088 goto error;
10089 if (!m)
10090 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10091 "parameters don't match", goto error);
10093 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10094 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10095 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10097 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10098 isl_space_copy(map2->dim)),
10099 map1->n * map2->n, flags);
10100 if (!result)
10101 goto error;
10102 for (i = 0; i < map1->n; ++i)
10103 for (j = 0; j < map2->n; ++j) {
10104 struct isl_basic_map *part;
10105 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10106 isl_basic_map_copy(map2->p[j]));
10107 if (isl_basic_map_is_empty(part))
10108 isl_basic_map_free(part);
10109 else
10110 result = isl_map_add_basic_map(result, part);
10111 if (!result)
10112 goto error;
10114 if (remove_duplicates)
10115 result = isl_map_remove_obvious_duplicates(result);
10116 isl_map_free(map1);
10117 isl_map_free(map2);
10118 return result;
10119 error:
10120 isl_map_free(map1);
10121 isl_map_free(map2);
10122 return NULL;
10125 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10127 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10128 __isl_take isl_map *map2)
10130 return map_product(map1, map2, &isl_space_product,
10131 &isl_basic_map_product, 0);
10134 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10135 __isl_take isl_map *map2)
10137 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10140 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10142 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10143 __isl_take isl_map *map2)
10145 isl_map *prod;
10147 prod = isl_map_product(map1, map2);
10148 prod = isl_map_flatten(prod);
10149 return prod;
10152 /* Given two set A and B, construct its Cartesian product A x B.
10154 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10156 return isl_map_range_product(set1, set2);
10159 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10160 __isl_take isl_set *set2)
10162 return isl_map_flat_range_product(set1, set2);
10165 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10167 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10168 __isl_take isl_map *map2)
10170 return map_product(map1, map2, &isl_space_domain_product,
10171 &isl_basic_map_domain_product, 1);
10174 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10176 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10177 __isl_take isl_map *map2)
10179 return map_product(map1, map2, &isl_space_range_product,
10180 &isl_basic_map_range_product, 1);
10183 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10184 __isl_take isl_map *map2)
10186 return isl_map_align_params_map_map_and(map1, map2,
10187 &map_domain_product_aligned);
10190 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10191 __isl_take isl_map *map2)
10193 return isl_map_align_params_map_map_and(map1, map2,
10194 &map_range_product_aligned);
10197 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10199 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10201 isl_space *space;
10202 int total1, keep1, total2, keep2;
10204 if (!map)
10205 return NULL;
10206 if (!isl_space_domain_is_wrapping(map->dim) ||
10207 !isl_space_range_is_wrapping(map->dim))
10208 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10209 "not a product", return isl_map_free(map));
10211 space = isl_map_get_space(map);
10212 total1 = isl_space_dim(space, isl_dim_in);
10213 total2 = isl_space_dim(space, isl_dim_out);
10214 space = isl_space_factor_domain(space);
10215 keep1 = isl_space_dim(space, isl_dim_in);
10216 keep2 = isl_space_dim(space, isl_dim_out);
10217 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10218 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10219 map = isl_map_reset_space(map, space);
10221 return map;
10224 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10226 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10228 isl_space *space;
10229 int total1, keep1, total2, keep2;
10231 if (!map)
10232 return NULL;
10233 if (!isl_space_domain_is_wrapping(map->dim) ||
10234 !isl_space_range_is_wrapping(map->dim))
10235 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10236 "not a product", return isl_map_free(map));
10238 space = isl_map_get_space(map);
10239 total1 = isl_space_dim(space, isl_dim_in);
10240 total2 = isl_space_dim(space, isl_dim_out);
10241 space = isl_space_factor_range(space);
10242 keep1 = isl_space_dim(space, isl_dim_in);
10243 keep2 = isl_space_dim(space, isl_dim_out);
10244 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10245 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10246 map = isl_map_reset_space(map, space);
10248 return map;
10251 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10253 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10255 isl_space *space;
10256 int total, keep;
10258 if (!map)
10259 return NULL;
10260 if (!isl_space_domain_is_wrapping(map->dim))
10261 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10262 "domain is not a product", return isl_map_free(map));
10264 space = isl_map_get_space(map);
10265 total = isl_space_dim(space, isl_dim_in);
10266 space = isl_space_domain_factor_domain(space);
10267 keep = isl_space_dim(space, isl_dim_in);
10268 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10269 map = isl_map_reset_space(map, space);
10271 return map;
10274 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10276 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10278 isl_space *space;
10279 int total, keep;
10281 if (!map)
10282 return NULL;
10283 if (!isl_space_domain_is_wrapping(map->dim))
10284 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10285 "domain is not a product", return isl_map_free(map));
10287 space = isl_map_get_space(map);
10288 total = isl_space_dim(space, isl_dim_in);
10289 space = isl_space_domain_factor_range(space);
10290 keep = isl_space_dim(space, isl_dim_in);
10291 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10292 map = isl_map_reset_space(map, space);
10294 return map;
10297 /* Given a map A -> [B -> C], extract the map A -> B.
10299 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10301 isl_space *space;
10302 int total, keep;
10304 if (!map)
10305 return NULL;
10306 if (!isl_space_range_is_wrapping(map->dim))
10307 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10308 "range is not a product", return isl_map_free(map));
10310 space = isl_map_get_space(map);
10311 total = isl_space_dim(space, isl_dim_out);
10312 space = isl_space_range_factor_domain(space);
10313 keep = isl_space_dim(space, isl_dim_out);
10314 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10315 map = isl_map_reset_space(map, space);
10317 return map;
10320 /* Given a map A -> [B -> C], extract the map A -> C.
10322 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10324 isl_space *space;
10325 int total, keep;
10327 if (!map)
10328 return NULL;
10329 if (!isl_space_range_is_wrapping(map->dim))
10330 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10331 "range is not a product", return isl_map_free(map));
10333 space = isl_map_get_space(map);
10334 total = isl_space_dim(space, isl_dim_out);
10335 space = isl_space_range_factor_range(space);
10336 keep = isl_space_dim(space, isl_dim_out);
10337 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10338 map = isl_map_reset_space(map, space);
10340 return map;
10343 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10345 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10346 __isl_take isl_map *map2)
10348 isl_map *prod;
10350 prod = isl_map_domain_product(map1, map2);
10351 prod = isl_map_flatten_domain(prod);
10352 return prod;
10355 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10357 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10358 __isl_take isl_map *map2)
10360 isl_map *prod;
10362 prod = isl_map_range_product(map1, map2);
10363 prod = isl_map_flatten_range(prod);
10364 return prod;
10367 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10369 int i;
10370 uint32_t hash = isl_hash_init();
10371 unsigned total;
10373 if (!bmap)
10374 return 0;
10375 bmap = isl_basic_map_copy(bmap);
10376 bmap = isl_basic_map_normalize(bmap);
10377 if (!bmap)
10378 return 0;
10379 total = isl_basic_map_total_dim(bmap);
10380 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10381 for (i = 0; i < bmap->n_eq; ++i) {
10382 uint32_t c_hash;
10383 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10384 isl_hash_hash(hash, c_hash);
10386 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10387 for (i = 0; i < bmap->n_ineq; ++i) {
10388 uint32_t c_hash;
10389 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10390 isl_hash_hash(hash, c_hash);
10392 isl_hash_byte(hash, bmap->n_div & 0xFF);
10393 for (i = 0; i < bmap->n_div; ++i) {
10394 uint32_t c_hash;
10395 if (isl_int_is_zero(bmap->div[i][0]))
10396 continue;
10397 isl_hash_byte(hash, i & 0xFF);
10398 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10399 isl_hash_hash(hash, c_hash);
10401 isl_basic_map_free(bmap);
10402 return hash;
10405 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10407 return isl_basic_map_get_hash(bset_to_bmap(bset));
10410 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10412 int i;
10413 uint32_t hash;
10415 if (!map)
10416 return 0;
10417 map = isl_map_copy(map);
10418 map = isl_map_normalize(map);
10419 if (!map)
10420 return 0;
10422 hash = isl_hash_init();
10423 for (i = 0; i < map->n; ++i) {
10424 uint32_t bmap_hash;
10425 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10426 isl_hash_hash(hash, bmap_hash);
10429 isl_map_free(map);
10431 return hash;
10434 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10436 return isl_map_get_hash(set_to_map(set));
10439 /* Return the number of basic maps in the (current) representation of "map".
10441 int isl_map_n_basic_map(__isl_keep isl_map *map)
10443 return map ? map->n : 0;
10446 int isl_set_n_basic_set(__isl_keep isl_set *set)
10448 return set ? set->n : 0;
10451 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10452 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10454 int i;
10456 if (!map)
10457 return isl_stat_error;
10459 for (i = 0; i < map->n; ++i)
10460 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10461 return isl_stat_error;
10463 return isl_stat_ok;
10466 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10467 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10469 int i;
10471 if (!set)
10472 return isl_stat_error;
10474 for (i = 0; i < set->n; ++i)
10475 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10476 return isl_stat_error;
10478 return isl_stat_ok;
10481 /* Return a list of basic sets, the union of which is equal to "set".
10483 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10484 __isl_keep isl_set *set)
10486 int i;
10487 isl_basic_set_list *list;
10489 if (!set)
10490 return NULL;
10492 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10493 for (i = 0; i < set->n; ++i) {
10494 isl_basic_set *bset;
10496 bset = isl_basic_set_copy(set->p[i]);
10497 list = isl_basic_set_list_add(list, bset);
10500 return list;
10503 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10505 isl_space *space;
10507 if (!bset)
10508 return NULL;
10510 bset = isl_basic_set_cow(bset);
10511 if (!bset)
10512 return NULL;
10514 space = isl_basic_set_get_space(bset);
10515 space = isl_space_lift(space, bset->n_div);
10516 if (!space)
10517 goto error;
10518 isl_space_free(bset->dim);
10519 bset->dim = space;
10520 bset->extra -= bset->n_div;
10521 bset->n_div = 0;
10523 bset = isl_basic_set_finalize(bset);
10525 return bset;
10526 error:
10527 isl_basic_set_free(bset);
10528 return NULL;
10531 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10533 int i;
10534 isl_space *space;
10535 unsigned n_div;
10537 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10539 if (!set)
10540 return NULL;
10542 set = isl_set_cow(set);
10543 if (!set)
10544 return NULL;
10546 n_div = set->p[0]->n_div;
10547 space = isl_set_get_space(set);
10548 space = isl_space_lift(space, n_div);
10549 if (!space)
10550 goto error;
10551 isl_space_free(set->dim);
10552 set->dim = space;
10554 for (i = 0; i < set->n; ++i) {
10555 set->p[i] = isl_basic_set_lift(set->p[i]);
10556 if (!set->p[i])
10557 goto error;
10560 return set;
10561 error:
10562 isl_set_free(set);
10563 return NULL;
10566 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10568 unsigned dim;
10569 int size = 0;
10571 if (!bset)
10572 return -1;
10574 dim = isl_basic_set_total_dim(bset);
10575 size += bset->n_eq * (1 + dim);
10576 size += bset->n_ineq * (1 + dim);
10577 size += bset->n_div * (2 + dim);
10579 return size;
10582 int isl_set_size(__isl_keep isl_set *set)
10584 int i;
10585 int size = 0;
10587 if (!set)
10588 return -1;
10590 for (i = 0; i < set->n; ++i)
10591 size += isl_basic_set_size(set->p[i]);
10593 return size;
10596 /* Check if there is any lower bound (if lower == 0) and/or upper
10597 * bound (if upper == 0) on the specified dim.
10599 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10600 enum isl_dim_type type, unsigned pos, int lower, int upper)
10602 int i;
10604 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10605 return isl_bool_error;
10607 pos += isl_basic_map_offset(bmap, type);
10609 for (i = 0; i < bmap->n_div; ++i) {
10610 if (isl_int_is_zero(bmap->div[i][0]))
10611 continue;
10612 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10613 return isl_bool_true;
10616 for (i = 0; i < bmap->n_eq; ++i)
10617 if (!isl_int_is_zero(bmap->eq[i][pos]))
10618 return isl_bool_true;
10620 for (i = 0; i < bmap->n_ineq; ++i) {
10621 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10622 if (sgn > 0)
10623 lower = 1;
10624 if (sgn < 0)
10625 upper = 1;
10628 return lower && upper;
10631 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10632 enum isl_dim_type type, unsigned pos)
10634 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10637 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10638 enum isl_dim_type type, unsigned pos)
10640 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10643 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10644 enum isl_dim_type type, unsigned pos)
10646 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10649 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10650 enum isl_dim_type type, unsigned pos)
10652 int i;
10654 if (!map)
10655 return isl_bool_error;
10657 for (i = 0; i < map->n; ++i) {
10658 isl_bool bounded;
10659 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10660 if (bounded < 0 || !bounded)
10661 return bounded;
10664 return isl_bool_true;
10667 /* Return true if the specified dim is involved in both an upper bound
10668 * and a lower bound.
10670 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10671 enum isl_dim_type type, unsigned pos)
10673 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10676 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10678 static isl_bool has_any_bound(__isl_keep isl_map *map,
10679 enum isl_dim_type type, unsigned pos,
10680 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10681 enum isl_dim_type type, unsigned pos))
10683 int i;
10685 if (!map)
10686 return isl_bool_error;
10688 for (i = 0; i < map->n; ++i) {
10689 isl_bool bounded;
10690 bounded = fn(map->p[i], type, pos);
10691 if (bounded < 0 || bounded)
10692 return bounded;
10695 return isl_bool_false;
10698 /* Return 1 if the specified dim is involved in any lower bound.
10700 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10701 enum isl_dim_type type, unsigned pos)
10703 return has_any_bound(set, type, pos,
10704 &isl_basic_map_dim_has_lower_bound);
10707 /* Return 1 if the specified dim is involved in any upper bound.
10709 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10710 enum isl_dim_type type, unsigned pos)
10712 return has_any_bound(set, type, pos,
10713 &isl_basic_map_dim_has_upper_bound);
10716 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10718 static isl_bool has_bound(__isl_keep isl_map *map,
10719 enum isl_dim_type type, unsigned pos,
10720 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10721 enum isl_dim_type type, unsigned pos))
10723 int i;
10725 if (!map)
10726 return isl_bool_error;
10728 for (i = 0; i < map->n; ++i) {
10729 isl_bool bounded;
10730 bounded = fn(map->p[i], type, pos);
10731 if (bounded < 0 || !bounded)
10732 return bounded;
10735 return isl_bool_true;
10738 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10740 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10741 enum isl_dim_type type, unsigned pos)
10743 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10746 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10748 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10749 enum isl_dim_type type, unsigned pos)
10751 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10754 /* For each of the "n" variables starting at "first", determine
10755 * the sign of the variable and put the results in the first "n"
10756 * elements of the array "signs".
10757 * Sign
10758 * 1 means that the variable is non-negative
10759 * -1 means that the variable is non-positive
10760 * 0 means the variable attains both positive and negative values.
10762 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10763 unsigned first, unsigned n, int *signs)
10765 isl_vec *bound = NULL;
10766 struct isl_tab *tab = NULL;
10767 struct isl_tab_undo *snap;
10768 int i;
10770 if (!bset || !signs)
10771 return isl_stat_error;
10773 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10774 tab = isl_tab_from_basic_set(bset, 0);
10775 if (!bound || !tab)
10776 goto error;
10778 isl_seq_clr(bound->el, bound->size);
10779 isl_int_set_si(bound->el[0], -1);
10781 snap = isl_tab_snap(tab);
10782 for (i = 0; i < n; ++i) {
10783 int empty;
10785 isl_int_set_si(bound->el[1 + first + i], -1);
10786 if (isl_tab_add_ineq(tab, bound->el) < 0)
10787 goto error;
10788 empty = tab->empty;
10789 isl_int_set_si(bound->el[1 + first + i], 0);
10790 if (isl_tab_rollback(tab, snap) < 0)
10791 goto error;
10793 if (empty) {
10794 signs[i] = 1;
10795 continue;
10798 isl_int_set_si(bound->el[1 + first + i], 1);
10799 if (isl_tab_add_ineq(tab, bound->el) < 0)
10800 goto error;
10801 empty = tab->empty;
10802 isl_int_set_si(bound->el[1 + first + i], 0);
10803 if (isl_tab_rollback(tab, snap) < 0)
10804 goto error;
10806 signs[i] = empty ? -1 : 0;
10809 isl_tab_free(tab);
10810 isl_vec_free(bound);
10811 return isl_stat_ok;
10812 error:
10813 isl_tab_free(tab);
10814 isl_vec_free(bound);
10815 return isl_stat_error;
10818 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10819 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10821 if (!bset || !signs)
10822 return isl_stat_error;
10823 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10824 return isl_stat_error;
10826 first += pos(bset->dim, type) - 1;
10827 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10830 /* Is it possible for the integer division "div" to depend (possibly
10831 * indirectly) on any output dimensions?
10833 * If the div is undefined, then we conservatively assume that it
10834 * may depend on them.
10835 * Otherwise, we check if it actually depends on them or on any integer
10836 * divisions that may depend on them.
10838 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10840 int i;
10841 unsigned n_out, o_out;
10842 unsigned n_div, o_div;
10844 if (isl_int_is_zero(bmap->div[div][0]))
10845 return isl_bool_true;
10847 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10848 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10850 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10851 return isl_bool_true;
10853 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10854 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10856 for (i = 0; i < n_div; ++i) {
10857 isl_bool may_involve;
10859 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10860 continue;
10861 may_involve = div_may_involve_output(bmap, i);
10862 if (may_involve < 0 || may_involve)
10863 return may_involve;
10866 return isl_bool_false;
10869 /* Return the first integer division of "bmap" in the range
10870 * [first, first + n[ that may depend on any output dimensions and
10871 * that has a non-zero coefficient in "c" (where the first coefficient
10872 * in "c" corresponds to integer division "first").
10874 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10875 isl_int *c, int first, int n)
10877 int k;
10879 if (!bmap)
10880 return -1;
10882 for (k = first; k < first + n; ++k) {
10883 isl_bool may_involve;
10885 if (isl_int_is_zero(c[k]))
10886 continue;
10887 may_involve = div_may_involve_output(bmap, k);
10888 if (may_involve < 0)
10889 return -1;
10890 if (may_involve)
10891 return k;
10894 return first + n;
10897 /* Look for a pair of inequality constraints in "bmap" of the form
10899 * -l + i >= 0 or i >= l
10900 * and
10901 * n + l - i >= 0 or i <= l + n
10903 * with n < "m" and i the output dimension at position "pos".
10904 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10905 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10906 * and earlier output dimensions, as well as integer divisions that do
10907 * not involve any of the output dimensions.
10909 * Return the index of the first inequality constraint or bmap->n_ineq
10910 * if no such pair can be found.
10912 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10913 int pos, isl_int m)
10915 int i, j;
10916 isl_ctx *ctx;
10917 unsigned total;
10918 unsigned n_div, o_div;
10919 unsigned n_out, o_out;
10920 int less;
10922 if (!bmap)
10923 return -1;
10925 ctx = isl_basic_map_get_ctx(bmap);
10926 total = isl_basic_map_total_dim(bmap);
10927 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10928 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10929 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10930 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10931 for (i = 0; i < bmap->n_ineq; ++i) {
10932 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10933 continue;
10934 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10935 n_out - (pos + 1)) != -1)
10936 continue;
10937 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10938 0, n_div) < n_div)
10939 continue;
10940 for (j = i + 1; j < bmap->n_ineq; ++j) {
10941 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10942 ctx->one))
10943 continue;
10944 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10945 bmap->ineq[j] + 1, total))
10946 continue;
10947 break;
10949 if (j >= bmap->n_ineq)
10950 continue;
10951 isl_int_add(bmap->ineq[i][0],
10952 bmap->ineq[i][0], bmap->ineq[j][0]);
10953 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10954 isl_int_sub(bmap->ineq[i][0],
10955 bmap->ineq[i][0], bmap->ineq[j][0]);
10956 if (!less)
10957 continue;
10958 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10959 return i;
10960 else
10961 return j;
10964 return bmap->n_ineq;
10967 /* Return the index of the equality of "bmap" that defines
10968 * the output dimension "pos" in terms of earlier dimensions.
10969 * The equality may also involve integer divisions, as long
10970 * as those integer divisions are defined in terms of
10971 * parameters or input dimensions.
10972 * In this case, *div is set to the number of integer divisions and
10973 * *ineq is set to the number of inequality constraints (provided
10974 * div and ineq are not NULL).
10976 * The equality may also involve a single integer division involving
10977 * the output dimensions (typically only output dimension "pos") as
10978 * long as the coefficient of output dimension "pos" is 1 or -1 and
10979 * there is a pair of constraints i >= l and i <= l + n, with i referring
10980 * to output dimension "pos", l an expression involving only earlier
10981 * dimensions and n smaller than the coefficient of the integer division
10982 * in the equality. In this case, the output dimension can be defined
10983 * in terms of a modulo expression that does not involve the integer division.
10984 * *div is then set to this single integer division and
10985 * *ineq is set to the index of constraint i >= l.
10987 * Return bmap->n_eq if there is no such equality.
10988 * Return -1 on error.
10990 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10991 int pos, int *div, int *ineq)
10993 int j, k, l;
10994 unsigned n_out, o_out;
10995 unsigned n_div, o_div;
10997 if (!bmap)
10998 return -1;
11000 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11001 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11002 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11003 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11005 if (ineq)
11006 *ineq = bmap->n_ineq;
11007 if (div)
11008 *div = n_div;
11009 for (j = 0; j < bmap->n_eq; ++j) {
11010 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11011 continue;
11012 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11013 n_out - (pos + 1)) != -1)
11014 continue;
11015 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11016 0, n_div);
11017 if (k >= n_div)
11018 return j;
11019 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11020 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11021 continue;
11022 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11023 k + 1, n_div - (k+1)) < n_div)
11024 continue;
11025 l = find_modulo_constraint_pair(bmap, pos,
11026 bmap->eq[j][o_div + k]);
11027 if (l < 0)
11028 return -1;
11029 if (l >= bmap->n_ineq)
11030 continue;
11031 if (div)
11032 *div = k;
11033 if (ineq)
11034 *ineq = l;
11035 return j;
11038 return bmap->n_eq;
11041 /* Check if the given basic map is obviously single-valued.
11042 * In particular, for each output dimension, check that there is
11043 * an equality that defines the output dimension in terms of
11044 * earlier dimensions.
11046 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11048 int i;
11049 unsigned n_out;
11051 if (!bmap)
11052 return isl_bool_error;
11054 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11056 for (i = 0; i < n_out; ++i) {
11057 int eq;
11059 eq = isl_basic_map_output_defining_equality(bmap, i,
11060 NULL, NULL);
11061 if (eq < 0)
11062 return isl_bool_error;
11063 if (eq >= bmap->n_eq)
11064 return isl_bool_false;
11067 return isl_bool_true;
11070 /* Check if the given basic map is single-valued.
11071 * We simply compute
11073 * M \circ M^-1
11075 * and check if the result is a subset of the identity mapping.
11077 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11079 isl_space *space;
11080 isl_basic_map *test;
11081 isl_basic_map *id;
11082 isl_bool sv;
11084 sv = isl_basic_map_plain_is_single_valued(bmap);
11085 if (sv < 0 || sv)
11086 return sv;
11088 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11089 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11091 space = isl_basic_map_get_space(bmap);
11092 space = isl_space_map_from_set(isl_space_range(space));
11093 id = isl_basic_map_identity(space);
11095 sv = isl_basic_map_is_subset(test, id);
11097 isl_basic_map_free(test);
11098 isl_basic_map_free(id);
11100 return sv;
11103 /* Check if the given map is obviously single-valued.
11105 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11107 if (!map)
11108 return isl_bool_error;
11109 if (map->n == 0)
11110 return isl_bool_true;
11111 if (map->n >= 2)
11112 return isl_bool_false;
11114 return isl_basic_map_plain_is_single_valued(map->p[0]);
11117 /* Check if the given map is single-valued.
11118 * We simply compute
11120 * M \circ M^-1
11122 * and check if the result is a subset of the identity mapping.
11124 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11126 isl_space *dim;
11127 isl_map *test;
11128 isl_map *id;
11129 isl_bool sv;
11131 sv = isl_map_plain_is_single_valued(map);
11132 if (sv < 0 || sv)
11133 return sv;
11135 test = isl_map_reverse(isl_map_copy(map));
11136 test = isl_map_apply_range(test, isl_map_copy(map));
11138 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11139 id = isl_map_identity(dim);
11141 sv = isl_map_is_subset(test, id);
11143 isl_map_free(test);
11144 isl_map_free(id);
11146 return sv;
11149 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11151 isl_bool in;
11153 map = isl_map_copy(map);
11154 map = isl_map_reverse(map);
11155 in = isl_map_is_single_valued(map);
11156 isl_map_free(map);
11158 return in;
11161 /* Check if the given map is obviously injective.
11163 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11165 isl_bool in;
11167 map = isl_map_copy(map);
11168 map = isl_map_reverse(map);
11169 in = isl_map_plain_is_single_valued(map);
11170 isl_map_free(map);
11172 return in;
11175 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11177 isl_bool sv;
11179 sv = isl_map_is_single_valued(map);
11180 if (sv < 0 || !sv)
11181 return sv;
11183 return isl_map_is_injective(map);
11186 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11188 return isl_map_is_single_valued(set_to_map(set));
11191 /* Does "map" only map elements to themselves?
11193 * If the domain and range spaces are different, then "map"
11194 * is considered not to be an identity relation, even if it is empty.
11195 * Otherwise, construct the maximal identity relation and
11196 * check whether "map" is a subset of this relation.
11198 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11200 isl_space *space;
11201 isl_map *id;
11202 isl_bool equal, is_identity;
11204 space = isl_map_get_space(map);
11205 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11206 isl_space_free(space);
11207 if (equal < 0 || !equal)
11208 return equal;
11210 id = isl_map_identity(isl_map_get_space(map));
11211 is_identity = isl_map_is_subset(map, id);
11212 isl_map_free(id);
11214 return is_identity;
11217 int isl_map_is_translation(__isl_keep isl_map *map)
11219 int ok;
11220 isl_set *delta;
11222 delta = isl_map_deltas(isl_map_copy(map));
11223 ok = isl_set_is_singleton(delta);
11224 isl_set_free(delta);
11226 return ok;
11229 static int unique(isl_int *p, unsigned pos, unsigned len)
11231 if (isl_seq_first_non_zero(p, pos) != -1)
11232 return 0;
11233 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11234 return 0;
11235 return 1;
11238 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11240 int i, j;
11241 unsigned nvar;
11242 unsigned ovar;
11244 if (!bset)
11245 return isl_bool_error;
11247 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11248 return isl_bool_false;
11250 nvar = isl_basic_set_dim(bset, isl_dim_set);
11251 ovar = isl_space_offset(bset->dim, isl_dim_set);
11252 for (j = 0; j < nvar; ++j) {
11253 int lower = 0, upper = 0;
11254 for (i = 0; i < bset->n_eq; ++i) {
11255 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11256 continue;
11257 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11258 return isl_bool_false;
11259 break;
11261 if (i < bset->n_eq)
11262 continue;
11263 for (i = 0; i < bset->n_ineq; ++i) {
11264 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11265 continue;
11266 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11267 return isl_bool_false;
11268 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11269 lower = 1;
11270 else
11271 upper = 1;
11273 if (!lower || !upper)
11274 return isl_bool_false;
11277 return isl_bool_true;
11280 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11282 if (!set)
11283 return isl_bool_error;
11284 if (set->n != 1)
11285 return isl_bool_false;
11287 return isl_basic_set_is_box(set->p[0]);
11290 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11292 if (!bset)
11293 return isl_bool_error;
11295 return isl_space_is_wrapping(bset->dim);
11298 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11300 if (!set)
11301 return isl_bool_error;
11303 return isl_space_is_wrapping(set->dim);
11306 /* Modify the space of "map" through a call to "change".
11307 * If "can_change" is set (not NULL), then first call it to check
11308 * if the modification is allowed, printing the error message "cannot_change"
11309 * if it is not.
11311 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11312 isl_bool (*can_change)(__isl_keep isl_map *map),
11313 const char *cannot_change,
11314 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11316 isl_bool ok;
11317 isl_space *space;
11319 if (!map)
11320 return NULL;
11322 ok = can_change ? can_change(map) : isl_bool_true;
11323 if (ok < 0)
11324 return isl_map_free(map);
11325 if (!ok)
11326 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11327 return isl_map_free(map));
11329 space = change(isl_map_get_space(map));
11330 map = isl_map_reset_space(map, space);
11332 return map;
11335 /* Is the domain of "map" a wrapped relation?
11337 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11339 if (!map)
11340 return isl_bool_error;
11342 return isl_space_domain_is_wrapping(map->dim);
11345 /* Does "map" have a wrapped relation in both domain and range?
11347 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11349 return isl_space_is_product(isl_map_peek_space(map));
11352 /* Is the range of "map" a wrapped relation?
11354 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11356 if (!map)
11357 return isl_bool_error;
11359 return isl_space_range_is_wrapping(map->dim);
11362 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11364 bmap = isl_basic_map_cow(bmap);
11365 if (!bmap)
11366 return NULL;
11368 bmap->dim = isl_space_wrap(bmap->dim);
11369 if (!bmap->dim)
11370 goto error;
11372 bmap = isl_basic_map_finalize(bmap);
11374 return bset_from_bmap(bmap);
11375 error:
11376 isl_basic_map_free(bmap);
11377 return NULL;
11380 /* Given a map A -> B, return the set (A -> B).
11382 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11384 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11387 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11389 bset = isl_basic_set_cow(bset);
11390 if (!bset)
11391 return NULL;
11393 bset->dim = isl_space_unwrap(bset->dim);
11394 if (!bset->dim)
11395 goto error;
11397 bset = isl_basic_set_finalize(bset);
11399 return bset_to_bmap(bset);
11400 error:
11401 isl_basic_set_free(bset);
11402 return NULL;
11405 /* Given a set (A -> B), return the map A -> B.
11406 * Error out if "set" is not of the form (A -> B).
11408 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11410 return isl_map_change_space(set, &isl_set_is_wrapping,
11411 "not a wrapping set", &isl_space_unwrap);
11414 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11415 enum isl_dim_type type)
11417 if (!bmap)
11418 return NULL;
11420 if (!isl_space_is_named_or_nested(bmap->dim, type))
11421 return bmap;
11423 bmap = isl_basic_map_cow(bmap);
11424 if (!bmap)
11425 return NULL;
11427 bmap->dim = isl_space_reset(bmap->dim, type);
11428 if (!bmap->dim)
11429 goto error;
11431 bmap = isl_basic_map_finalize(bmap);
11433 return bmap;
11434 error:
11435 isl_basic_map_free(bmap);
11436 return NULL;
11439 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11440 enum isl_dim_type type)
11442 int i;
11444 if (!map)
11445 return NULL;
11447 if (!isl_space_is_named_or_nested(map->dim, type))
11448 return map;
11450 map = isl_map_cow(map);
11451 if (!map)
11452 return NULL;
11454 for (i = 0; i < map->n; ++i) {
11455 map->p[i] = isl_basic_map_reset(map->p[i], type);
11456 if (!map->p[i])
11457 goto error;
11459 map->dim = isl_space_reset(map->dim, type);
11460 if (!map->dim)
11461 goto error;
11463 return map;
11464 error:
11465 isl_map_free(map);
11466 return NULL;
11469 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11471 if (!bmap)
11472 return NULL;
11474 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11475 return bmap;
11477 bmap = isl_basic_map_cow(bmap);
11478 if (!bmap)
11479 return NULL;
11481 bmap->dim = isl_space_flatten(bmap->dim);
11482 if (!bmap->dim)
11483 goto error;
11485 bmap = isl_basic_map_finalize(bmap);
11487 return bmap;
11488 error:
11489 isl_basic_map_free(bmap);
11490 return NULL;
11493 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11495 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11498 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11499 __isl_take isl_basic_map *bmap)
11501 if (!bmap)
11502 return NULL;
11504 if (!bmap->dim->nested[0])
11505 return bmap;
11507 bmap = isl_basic_map_cow(bmap);
11508 if (!bmap)
11509 return NULL;
11511 bmap->dim = isl_space_flatten_domain(bmap->dim);
11512 if (!bmap->dim)
11513 goto error;
11515 bmap = isl_basic_map_finalize(bmap);
11517 return bmap;
11518 error:
11519 isl_basic_map_free(bmap);
11520 return NULL;
11523 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11524 __isl_take isl_basic_map *bmap)
11526 if (!bmap)
11527 return NULL;
11529 if (!bmap->dim->nested[1])
11530 return bmap;
11532 bmap = isl_basic_map_cow(bmap);
11533 if (!bmap)
11534 return NULL;
11536 bmap->dim = isl_space_flatten_range(bmap->dim);
11537 if (!bmap->dim)
11538 goto error;
11540 bmap = isl_basic_map_finalize(bmap);
11542 return bmap;
11543 error:
11544 isl_basic_map_free(bmap);
11545 return NULL;
11548 /* Remove any internal structure from the spaces of domain and range of "map".
11550 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11552 if (!map)
11553 return NULL;
11555 if (!map->dim->nested[0] && !map->dim->nested[1])
11556 return map;
11558 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11561 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11563 return set_from_map(isl_map_flatten(set_to_map(set)));
11566 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11568 isl_space *space, *flat_space;
11569 isl_map *map;
11571 space = isl_set_get_space(set);
11572 flat_space = isl_space_flatten(isl_space_copy(space));
11573 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11574 flat_space));
11575 map = isl_map_intersect_domain(map, set);
11577 return map;
11580 /* Remove any internal structure from the space of the domain of "map".
11582 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11584 if (!map)
11585 return NULL;
11587 if (!map->dim->nested[0])
11588 return map;
11590 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11593 /* Remove any internal structure from the space of the range of "map".
11595 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11597 if (!map)
11598 return NULL;
11600 if (!map->dim->nested[1])
11601 return map;
11603 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11606 /* Reorder the dimensions of "bmap" according to the given dim_map
11607 * and set the dimension specification to "space" and
11608 * perform Gaussian elimination on the result.
11610 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11611 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11613 isl_basic_map *res;
11614 unsigned flags;
11615 unsigned n_div;
11617 if (!bmap || !space || !dim_map)
11618 goto error;
11620 flags = bmap->flags;
11621 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11622 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11623 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11624 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11625 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11626 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11627 if (res)
11628 res->flags = flags;
11629 res = isl_basic_map_gauss(res, NULL);
11630 res = isl_basic_map_finalize(res);
11631 return res;
11632 error:
11633 isl_dim_map_free(dim_map);
11634 isl_basic_map_free(bmap);
11635 isl_space_free(space);
11636 return NULL;
11639 /* Reorder the dimensions of "map" according to given reordering.
11641 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11642 __isl_take isl_reordering *r)
11644 int i;
11645 struct isl_dim_map *dim_map;
11647 map = isl_map_cow(map);
11648 dim_map = isl_dim_map_from_reordering(r);
11649 if (!map || !r || !dim_map)
11650 goto error;
11652 for (i = 0; i < map->n; ++i) {
11653 struct isl_dim_map *dim_map_i;
11654 isl_space *space;
11656 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11658 space = isl_reordering_get_space(r);
11659 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11661 if (!map->p[i])
11662 goto error;
11665 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11666 map = isl_map_unmark_normalized(map);
11668 isl_reordering_free(r);
11669 isl_dim_map_free(dim_map);
11670 return map;
11671 error:
11672 isl_dim_map_free(dim_map);
11673 isl_map_free(map);
11674 isl_reordering_free(r);
11675 return NULL;
11678 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11679 __isl_take isl_reordering *r)
11681 return set_from_map(isl_map_realign(set_to_map(set), r));
11684 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11685 __isl_take isl_space *model)
11687 isl_ctx *ctx;
11688 isl_bool aligned;
11690 if (!map || !model)
11691 goto error;
11693 ctx = isl_space_get_ctx(model);
11694 if (!isl_space_has_named_params(model))
11695 isl_die(ctx, isl_error_invalid,
11696 "model has unnamed parameters", goto error);
11697 if (isl_map_check_named_params(map) < 0)
11698 goto error;
11699 aligned = isl_map_space_has_equal_params(map, model);
11700 if (aligned < 0)
11701 goto error;
11702 if (!aligned) {
11703 isl_reordering *exp;
11705 exp = isl_parameter_alignment_reordering(map->dim, model);
11706 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11707 map = isl_map_realign(map, exp);
11710 isl_space_free(model);
11711 return map;
11712 error:
11713 isl_space_free(model);
11714 isl_map_free(map);
11715 return NULL;
11718 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11719 __isl_take isl_space *model)
11721 return isl_map_align_params(set, model);
11724 /* Align the parameters of "bmap" to those of "model", introducing
11725 * additional parameters if needed.
11727 __isl_give isl_basic_map *isl_basic_map_align_params(
11728 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11730 isl_ctx *ctx;
11731 isl_bool equal_params;
11733 if (!bmap || !model)
11734 goto error;
11736 ctx = isl_space_get_ctx(model);
11737 if (!isl_space_has_named_params(model))
11738 isl_die(ctx, isl_error_invalid,
11739 "model has unnamed parameters", goto error);
11740 if (isl_basic_map_check_named_params(bmap) < 0)
11741 goto error;
11742 equal_params = isl_space_has_equal_params(bmap->dim, model);
11743 if (equal_params < 0)
11744 goto error;
11745 if (!equal_params) {
11746 isl_reordering *exp;
11747 struct isl_dim_map *dim_map;
11749 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11750 exp = isl_reordering_extend_space(exp,
11751 isl_basic_map_get_space(bmap));
11752 dim_map = isl_dim_map_from_reordering(exp);
11753 bmap = isl_basic_map_realign(bmap,
11754 isl_reordering_get_space(exp),
11755 isl_dim_map_extend(dim_map, bmap));
11756 isl_reordering_free(exp);
11757 isl_dim_map_free(dim_map);
11760 isl_space_free(model);
11761 return bmap;
11762 error:
11763 isl_space_free(model);
11764 isl_basic_map_free(bmap);
11765 return NULL;
11768 /* Do "bset" and "space" have the same parameters?
11770 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11771 __isl_keep isl_space *space)
11773 isl_space *bset_space;
11775 bset_space = isl_basic_set_peek_space(bset);
11776 return isl_space_has_equal_params(bset_space, space);
11779 /* Do "map" and "space" have the same parameters?
11781 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11782 __isl_keep isl_space *space)
11784 isl_space *map_space;
11786 map_space = isl_map_peek_space(map);
11787 return isl_space_has_equal_params(map_space, space);
11790 /* Do "set" and "space" have the same parameters?
11792 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11793 __isl_keep isl_space *space)
11795 return isl_map_space_has_equal_params(set_to_map(set), space);
11798 /* Align the parameters of "bset" to those of "model", introducing
11799 * additional parameters if needed.
11801 __isl_give isl_basic_set *isl_basic_set_align_params(
11802 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11804 return isl_basic_map_align_params(bset, model);
11807 /* Drop all parameters not referenced by "map".
11809 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11811 int i;
11813 if (isl_map_check_named_params(map) < 0)
11814 return isl_map_free(map);
11816 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11817 isl_bool involves;
11819 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11820 if (involves < 0)
11821 return isl_map_free(map);
11822 if (!involves)
11823 map = isl_map_project_out(map, isl_dim_param, i, 1);
11826 return map;
11829 /* Drop all parameters not referenced by "set".
11831 __isl_give isl_set *isl_set_drop_unused_params(
11832 __isl_take isl_set *set)
11834 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11837 /* Drop all parameters not referenced by "bmap".
11839 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11840 __isl_take isl_basic_map *bmap)
11842 int i;
11844 if (isl_basic_map_check_named_params(bmap) < 0)
11845 return isl_basic_map_free(bmap);
11847 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11848 isl_bool involves;
11850 involves = isl_basic_map_involves_dims(bmap,
11851 isl_dim_param, i, 1);
11852 if (involves < 0)
11853 return isl_basic_map_free(bmap);
11854 if (!involves)
11855 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11858 return bmap;
11861 /* Drop all parameters not referenced by "bset".
11863 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11864 __isl_take isl_basic_set *bset)
11866 return bset_from_bmap(isl_basic_map_drop_unused_params(
11867 bset_to_bmap(bset)));
11870 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11871 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11872 enum isl_dim_type c2, enum isl_dim_type c3,
11873 enum isl_dim_type c4, enum isl_dim_type c5)
11875 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11876 struct isl_mat *mat;
11877 int i, j, k;
11878 int pos;
11880 if (!bmap)
11881 return NULL;
11882 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11883 isl_basic_map_total_dim(bmap) + 1);
11884 if (!mat)
11885 return NULL;
11886 for (i = 0; i < bmap->n_eq; ++i)
11887 for (j = 0, pos = 0; j < 5; ++j) {
11888 int off = isl_basic_map_offset(bmap, c[j]);
11889 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11890 isl_int_set(mat->row[i][pos],
11891 bmap->eq[i][off + k]);
11892 ++pos;
11896 return mat;
11899 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11900 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11901 enum isl_dim_type c2, enum isl_dim_type c3,
11902 enum isl_dim_type c4, enum isl_dim_type c5)
11904 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11905 struct isl_mat *mat;
11906 int i, j, k;
11907 int pos;
11909 if (!bmap)
11910 return NULL;
11911 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11912 isl_basic_map_total_dim(bmap) + 1);
11913 if (!mat)
11914 return NULL;
11915 for (i = 0; i < bmap->n_ineq; ++i)
11916 for (j = 0, pos = 0; j < 5; ++j) {
11917 int off = isl_basic_map_offset(bmap, c[j]);
11918 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11919 isl_int_set(mat->row[i][pos],
11920 bmap->ineq[i][off + k]);
11921 ++pos;
11925 return mat;
11928 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11929 __isl_take isl_space *space,
11930 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11931 enum isl_dim_type c2, enum isl_dim_type c3,
11932 enum isl_dim_type c4, enum isl_dim_type c5)
11934 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11935 isl_basic_map *bmap = NULL;
11936 unsigned total;
11937 unsigned extra;
11938 int i, j, k, l;
11939 int pos;
11941 if (!space || !eq || !ineq)
11942 goto error;
11944 if (eq->n_col != ineq->n_col)
11945 isl_die(space->ctx, isl_error_invalid,
11946 "equalities and inequalities matrices should have "
11947 "same number of columns", goto error);
11949 total = 1 + isl_space_dim(space, isl_dim_all);
11951 if (eq->n_col < total)
11952 isl_die(space->ctx, isl_error_invalid,
11953 "number of columns too small", goto error);
11955 extra = eq->n_col - total;
11957 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11958 eq->n_row, ineq->n_row);
11959 if (!bmap)
11960 goto error;
11961 for (i = 0; i < extra; ++i) {
11962 k = isl_basic_map_alloc_div(bmap);
11963 if (k < 0)
11964 goto error;
11965 isl_int_set_si(bmap->div[k][0], 0);
11967 for (i = 0; i < eq->n_row; ++i) {
11968 l = isl_basic_map_alloc_equality(bmap);
11969 if (l < 0)
11970 goto error;
11971 for (j = 0, pos = 0; j < 5; ++j) {
11972 int off = isl_basic_map_offset(bmap, c[j]);
11973 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11974 isl_int_set(bmap->eq[l][off + k],
11975 eq->row[i][pos]);
11976 ++pos;
11980 for (i = 0; i < ineq->n_row; ++i) {
11981 l = isl_basic_map_alloc_inequality(bmap);
11982 if (l < 0)
11983 goto error;
11984 for (j = 0, pos = 0; j < 5; ++j) {
11985 int off = isl_basic_map_offset(bmap, c[j]);
11986 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11987 isl_int_set(bmap->ineq[l][off + k],
11988 ineq->row[i][pos]);
11989 ++pos;
11994 isl_space_free(space);
11995 isl_mat_free(eq);
11996 isl_mat_free(ineq);
11998 bmap = isl_basic_map_simplify(bmap);
11999 return isl_basic_map_finalize(bmap);
12000 error:
12001 isl_space_free(space);
12002 isl_mat_free(eq);
12003 isl_mat_free(ineq);
12004 isl_basic_map_free(bmap);
12005 return NULL;
12008 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12009 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12010 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12012 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12013 c1, c2, c3, c4, isl_dim_in);
12016 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12017 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12018 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12020 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12021 c1, c2, c3, c4, isl_dim_in);
12024 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12025 __isl_take isl_space *dim,
12026 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12027 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12029 isl_basic_map *bmap;
12030 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12031 c1, c2, c3, c4, isl_dim_in);
12032 return bset_from_bmap(bmap);
12035 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12037 if (!bmap)
12038 return isl_bool_error;
12040 return isl_space_can_zip(bmap->dim);
12043 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12045 if (!map)
12046 return isl_bool_error;
12048 return isl_space_can_zip(map->dim);
12051 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12052 * (A -> C) -> (B -> D).
12054 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12056 unsigned pos;
12057 unsigned n1;
12058 unsigned n2;
12060 if (!bmap)
12061 return NULL;
12063 if (!isl_basic_map_can_zip(bmap))
12064 isl_die(bmap->ctx, isl_error_invalid,
12065 "basic map cannot be zipped", goto error);
12066 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12067 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12068 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12069 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12070 bmap = isl_basic_map_cow(bmap);
12071 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12072 if (!bmap)
12073 return NULL;
12074 bmap->dim = isl_space_zip(bmap->dim);
12075 if (!bmap->dim)
12076 goto error;
12077 bmap = isl_basic_map_mark_final(bmap);
12078 return bmap;
12079 error:
12080 isl_basic_map_free(bmap);
12081 return NULL;
12084 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12085 * (A -> C) -> (B -> D).
12087 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12089 int i;
12091 if (!map)
12092 return NULL;
12094 if (!isl_map_can_zip(map))
12095 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12096 goto error);
12098 map = isl_map_cow(map);
12099 if (!map)
12100 return NULL;
12102 for (i = 0; i < map->n; ++i) {
12103 map->p[i] = isl_basic_map_zip(map->p[i]);
12104 if (!map->p[i])
12105 goto error;
12108 map->dim = isl_space_zip(map->dim);
12109 if (!map->dim)
12110 goto error;
12112 return map;
12113 error:
12114 isl_map_free(map);
12115 return NULL;
12118 /* Can we apply isl_basic_map_curry to "bmap"?
12119 * That is, does it have a nested relation in its domain?
12121 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12123 if (!bmap)
12124 return isl_bool_error;
12126 return isl_space_can_curry(bmap->dim);
12129 /* Can we apply isl_map_curry to "map"?
12130 * That is, does it have a nested relation in its domain?
12132 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12134 if (!map)
12135 return isl_bool_error;
12137 return isl_space_can_curry(map->dim);
12140 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12141 * A -> (B -> C).
12143 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12146 if (!bmap)
12147 return NULL;
12149 if (!isl_basic_map_can_curry(bmap))
12150 isl_die(bmap->ctx, isl_error_invalid,
12151 "basic map cannot be curried", goto error);
12152 bmap = isl_basic_map_cow(bmap);
12153 if (!bmap)
12154 return NULL;
12155 bmap->dim = isl_space_curry(bmap->dim);
12156 if (!bmap->dim)
12157 goto error;
12158 bmap = isl_basic_map_mark_final(bmap);
12159 return bmap;
12160 error:
12161 isl_basic_map_free(bmap);
12162 return NULL;
12165 /* Given a map (A -> B) -> C, return the corresponding map
12166 * A -> (B -> C).
12168 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12170 return isl_map_change_space(map, &isl_map_can_curry,
12171 "map cannot be curried", &isl_space_curry);
12174 /* Can isl_map_range_curry be applied to "map"?
12175 * That is, does it have a nested relation in its range,
12176 * the domain of which is itself a nested relation?
12178 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12180 if (!map)
12181 return isl_bool_error;
12183 return isl_space_can_range_curry(map->dim);
12186 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12187 * A -> (B -> (C -> D)).
12189 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12191 return isl_map_change_space(map, &isl_map_can_range_curry,
12192 "map range cannot be curried",
12193 &isl_space_range_curry);
12196 /* Can we apply isl_basic_map_uncurry to "bmap"?
12197 * That is, does it have a nested relation in its domain?
12199 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12201 if (!bmap)
12202 return isl_bool_error;
12204 return isl_space_can_uncurry(bmap->dim);
12207 /* Can we apply isl_map_uncurry to "map"?
12208 * That is, does it have a nested relation in its domain?
12210 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12212 if (!map)
12213 return isl_bool_error;
12215 return isl_space_can_uncurry(map->dim);
12218 /* Given a basic map A -> (B -> C), return the corresponding basic map
12219 * (A -> B) -> C.
12221 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12224 if (!bmap)
12225 return NULL;
12227 if (!isl_basic_map_can_uncurry(bmap))
12228 isl_die(bmap->ctx, isl_error_invalid,
12229 "basic map cannot be uncurried",
12230 return isl_basic_map_free(bmap));
12231 bmap = isl_basic_map_cow(bmap);
12232 if (!bmap)
12233 return NULL;
12234 bmap->dim = isl_space_uncurry(bmap->dim);
12235 if (!bmap->dim)
12236 return isl_basic_map_free(bmap);
12237 bmap = isl_basic_map_mark_final(bmap);
12238 return bmap;
12241 /* Given a map A -> (B -> C), return the corresponding map
12242 * (A -> B) -> C.
12244 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12246 return isl_map_change_space(map, &isl_map_can_uncurry,
12247 "map cannot be uncurried", &isl_space_uncurry);
12250 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12251 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12253 return isl_map_equate(set, type1, pos1, type2, pos2);
12256 /* Construct a basic map where the given dimensions are equal to each other.
12258 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12259 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12261 isl_basic_map *bmap = NULL;
12262 int i;
12264 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12265 isl_space_check_range(space, type2, pos2, 1) < 0)
12266 goto error;
12268 if (type1 == type2 && pos1 == pos2)
12269 return isl_basic_map_universe(space);
12271 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12272 i = isl_basic_map_alloc_equality(bmap);
12273 if (i < 0)
12274 goto error;
12275 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12276 pos1 += isl_basic_map_offset(bmap, type1);
12277 pos2 += isl_basic_map_offset(bmap, type2);
12278 isl_int_set_si(bmap->eq[i][pos1], -1);
12279 isl_int_set_si(bmap->eq[i][pos2], 1);
12280 bmap = isl_basic_map_finalize(bmap);
12281 isl_space_free(space);
12282 return bmap;
12283 error:
12284 isl_space_free(space);
12285 isl_basic_map_free(bmap);
12286 return NULL;
12289 /* Add a constraint imposing that the given two dimensions are equal.
12291 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12292 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12294 isl_basic_map *eq;
12296 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12298 bmap = isl_basic_map_intersect(bmap, eq);
12300 return bmap;
12303 /* Add a constraint imposing that the given two dimensions are equal.
12305 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12306 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12308 isl_basic_map *bmap;
12310 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12312 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12314 return map;
12317 /* Add a constraint imposing that the given two dimensions have opposite values.
12319 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12320 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12322 isl_basic_map *bmap = NULL;
12323 int i;
12325 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12326 return isl_map_free(map);
12327 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12328 return isl_map_free(map);
12330 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12331 i = isl_basic_map_alloc_equality(bmap);
12332 if (i < 0)
12333 goto error;
12334 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12335 pos1 += isl_basic_map_offset(bmap, type1);
12336 pos2 += isl_basic_map_offset(bmap, type2);
12337 isl_int_set_si(bmap->eq[i][pos1], 1);
12338 isl_int_set_si(bmap->eq[i][pos2], 1);
12339 bmap = isl_basic_map_finalize(bmap);
12341 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12343 return map;
12344 error:
12345 isl_basic_map_free(bmap);
12346 isl_map_free(map);
12347 return NULL;
12350 /* Construct a constraint imposing that the value of the first dimension is
12351 * greater than or equal to that of the second.
12353 static __isl_give isl_constraint *constraint_order_ge(
12354 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12355 enum isl_dim_type type2, int pos2)
12357 isl_constraint *c;
12359 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12360 isl_space_check_range(space, type2, pos2, 1) < 0)
12361 space = isl_space_free(space);
12362 if (!space)
12363 return NULL;
12365 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12367 if (type1 == type2 && pos1 == pos2)
12368 return c;
12370 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12371 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12373 return c;
12376 /* Add a constraint imposing that the value of the first dimension is
12377 * greater than or equal to that of the second.
12379 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12380 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12382 isl_constraint *c;
12383 isl_space *space;
12385 if (type1 == type2 && pos1 == pos2)
12386 return bmap;
12387 space = isl_basic_map_get_space(bmap);
12388 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12389 bmap = isl_basic_map_add_constraint(bmap, c);
12391 return bmap;
12394 /* Add a constraint imposing that the value of the first dimension is
12395 * greater than or equal to that of the second.
12397 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12398 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12400 isl_constraint *c;
12401 isl_space *space;
12403 if (type1 == type2 && pos1 == pos2)
12404 return map;
12405 space = isl_map_get_space(map);
12406 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12407 map = isl_map_add_constraint(map, c);
12409 return map;
12412 /* Add a constraint imposing that the value of the first dimension is
12413 * less than or equal to that of the second.
12415 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12416 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12418 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12421 /* Construct a basic map where the value of the first dimension is
12422 * greater than that of the second.
12424 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12425 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12427 isl_basic_map *bmap = NULL;
12428 int i;
12430 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12431 isl_space_check_range(space, type2, pos2, 1) < 0)
12432 goto error;
12434 if (type1 == type2 && pos1 == pos2)
12435 return isl_basic_map_empty(space);
12437 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12438 i = isl_basic_map_alloc_inequality(bmap);
12439 if (i < 0)
12440 return isl_basic_map_free(bmap);
12441 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12442 pos1 += isl_basic_map_offset(bmap, type1);
12443 pos2 += isl_basic_map_offset(bmap, type2);
12444 isl_int_set_si(bmap->ineq[i][pos1], 1);
12445 isl_int_set_si(bmap->ineq[i][pos2], -1);
12446 isl_int_set_si(bmap->ineq[i][0], -1);
12447 bmap = isl_basic_map_finalize(bmap);
12449 return bmap;
12450 error:
12451 isl_space_free(space);
12452 isl_basic_map_free(bmap);
12453 return NULL;
12456 /* Add a constraint imposing that the value of the first dimension is
12457 * greater than that of the second.
12459 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12460 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12462 isl_basic_map *gt;
12464 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12466 bmap = isl_basic_map_intersect(bmap, gt);
12468 return bmap;
12471 /* Add a constraint imposing that the value of the first dimension is
12472 * greater than that of the second.
12474 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12475 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12477 isl_basic_map *bmap;
12479 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12481 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12483 return map;
12486 /* Add a constraint imposing that the value of the first dimension is
12487 * smaller than that of the second.
12489 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12490 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12492 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12495 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12496 int pos)
12498 isl_aff *div;
12499 isl_local_space *ls;
12501 if (!bmap)
12502 return NULL;
12504 if (!isl_basic_map_divs_known(bmap))
12505 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12506 "some divs are unknown", return NULL);
12508 ls = isl_basic_map_get_local_space(bmap);
12509 div = isl_local_space_get_div(ls, pos);
12510 isl_local_space_free(ls);
12512 return div;
12515 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12516 int pos)
12518 return isl_basic_map_get_div(bset, pos);
12521 /* Plug in "subs" for dimension "type", "pos" of "bset".
12523 * Let i be the dimension to replace and let "subs" be of the form
12525 * f/d
12527 * Any integer division with a non-zero coefficient for i,
12529 * floor((a i + g)/m)
12531 * is replaced by
12533 * floor((a f + d g)/(m d))
12535 * Constraints of the form
12537 * a i + g
12539 * are replaced by
12541 * a f + d g
12543 * We currently require that "subs" is an integral expression.
12544 * Handling rational expressions may require us to add stride constraints
12545 * as we do in isl_basic_set_preimage_multi_aff.
12547 __isl_give isl_basic_set *isl_basic_set_substitute(
12548 __isl_take isl_basic_set *bset,
12549 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12551 int i;
12552 isl_int v;
12553 isl_ctx *ctx;
12555 if (bset && isl_basic_set_plain_is_empty(bset))
12556 return bset;
12558 bset = isl_basic_set_cow(bset);
12559 if (!bset || !subs)
12560 goto error;
12562 ctx = isl_basic_set_get_ctx(bset);
12563 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12564 isl_die(ctx, isl_error_invalid,
12565 "spaces don't match", goto error);
12566 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12567 isl_die(ctx, isl_error_unsupported,
12568 "cannot handle divs yet", goto error);
12569 if (!isl_int_is_one(subs->v->el[0]))
12570 isl_die(ctx, isl_error_invalid,
12571 "can only substitute integer expressions", goto error);
12573 pos += isl_basic_set_offset(bset, type);
12575 isl_int_init(v);
12577 for (i = 0; i < bset->n_eq; ++i) {
12578 if (isl_int_is_zero(bset->eq[i][pos]))
12579 continue;
12580 isl_int_set(v, bset->eq[i][pos]);
12581 isl_int_set_si(bset->eq[i][pos], 0);
12582 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12583 v, subs->v->el + 1, subs->v->size - 1);
12586 for (i = 0; i < bset->n_ineq; ++i) {
12587 if (isl_int_is_zero(bset->ineq[i][pos]))
12588 continue;
12589 isl_int_set(v, bset->ineq[i][pos]);
12590 isl_int_set_si(bset->ineq[i][pos], 0);
12591 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12592 v, subs->v->el + 1, subs->v->size - 1);
12595 for (i = 0; i < bset->n_div; ++i) {
12596 if (isl_int_is_zero(bset->div[i][1 + pos]))
12597 continue;
12598 isl_int_set(v, bset->div[i][1 + pos]);
12599 isl_int_set_si(bset->div[i][1 + pos], 0);
12600 isl_seq_combine(bset->div[i] + 1,
12601 subs->v->el[0], bset->div[i] + 1,
12602 v, subs->v->el + 1, subs->v->size - 1);
12603 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12606 isl_int_clear(v);
12608 bset = isl_basic_set_simplify(bset);
12609 return isl_basic_set_finalize(bset);
12610 error:
12611 isl_basic_set_free(bset);
12612 return NULL;
12615 /* Plug in "subs" for dimension "type", "pos" of "set".
12617 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12618 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12620 int i;
12622 if (set && isl_set_plain_is_empty(set))
12623 return set;
12625 set = isl_set_cow(set);
12626 if (!set || !subs)
12627 goto error;
12629 for (i = set->n - 1; i >= 0; --i) {
12630 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12631 set = set_from_map(remove_if_empty(set_to_map(set), i));
12632 if (!set)
12633 return NULL;
12636 return set;
12637 error:
12638 isl_set_free(set);
12639 return NULL;
12642 /* Check if the range of "ma" is compatible with the domain or range
12643 * (depending on "type") of "bmap".
12645 static isl_stat check_basic_map_compatible_range_multi_aff(
12646 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12647 __isl_keep isl_multi_aff *ma)
12649 isl_bool m;
12650 isl_space *ma_space;
12652 ma_space = isl_multi_aff_get_space(ma);
12654 m = isl_space_has_equal_params(bmap->dim, ma_space);
12655 if (m < 0)
12656 goto error;
12657 if (!m)
12658 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12659 "parameters don't match", goto error);
12660 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12661 if (m < 0)
12662 goto error;
12663 if (!m)
12664 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12665 "spaces don't match", goto error);
12667 isl_space_free(ma_space);
12668 return isl_stat_ok;
12669 error:
12670 isl_space_free(ma_space);
12671 return isl_stat_error;
12674 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12675 * coefficients before the transformed range of dimensions,
12676 * the "n_after" coefficients after the transformed range of dimensions
12677 * and the coefficients of the other divs in "bmap".
12679 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12680 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12682 int i;
12683 int n_param;
12684 int n_set;
12685 isl_local_space *ls;
12687 if (n_div == 0)
12688 return bmap;
12690 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12691 if (!ls)
12692 return isl_basic_map_free(bmap);
12694 n_param = isl_local_space_dim(ls, isl_dim_param);
12695 n_set = isl_local_space_dim(ls, isl_dim_set);
12696 for (i = 0; i < n_div; ++i) {
12697 int o_bmap = 0, o_ls = 0;
12699 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12700 o_bmap += 1 + 1 + n_param;
12701 o_ls += 1 + 1 + n_param;
12702 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12703 o_bmap += n_before;
12704 isl_seq_cpy(bmap->div[i] + o_bmap,
12705 ls->div->row[i] + o_ls, n_set);
12706 o_bmap += n_set;
12707 o_ls += n_set;
12708 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12709 o_bmap += n_after;
12710 isl_seq_cpy(bmap->div[i] + o_bmap,
12711 ls->div->row[i] + o_ls, n_div);
12712 o_bmap += n_div;
12713 o_ls += n_div;
12714 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12715 bmap = isl_basic_map_add_div_constraints(bmap, i);
12716 if (!bmap)
12717 goto error;
12720 isl_local_space_free(ls);
12721 return bmap;
12722 error:
12723 isl_local_space_free(ls);
12724 return isl_basic_map_free(bmap);
12727 /* How many stride constraints does "ma" enforce?
12728 * That is, how many of the affine expressions have a denominator
12729 * different from one?
12731 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12733 int i;
12734 int strides = 0;
12736 for (i = 0; i < ma->n; ++i)
12737 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12738 strides++;
12740 return strides;
12743 /* For each affine expression in ma of the form
12745 * x_i = (f_i y + h_i)/m_i
12747 * with m_i different from one, add a constraint to "bmap"
12748 * of the form
12750 * f_i y + h_i = m_i alpha_i
12752 * with alpha_i an additional existentially quantified variable.
12754 * The input variables of "ma" correspond to a subset of the variables
12755 * of "bmap". There are "n_before" variables in "bmap" before this
12756 * subset and "n_after" variables after this subset.
12757 * The integer divisions of the affine expressions in "ma" are assumed
12758 * to have been aligned. There are "n_div_ma" of them and
12759 * they appear first in "bmap", straight after the "n_after" variables.
12761 static __isl_give isl_basic_map *add_ma_strides(
12762 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12763 int n_before, int n_after, int n_div_ma)
12765 int i, k;
12766 int div;
12767 int total;
12768 int n_param;
12769 int n_in;
12771 total = isl_basic_map_total_dim(bmap);
12772 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12773 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12774 for (i = 0; i < ma->n; ++i) {
12775 int o_bmap = 0, o_ma = 1;
12777 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12778 continue;
12779 div = isl_basic_map_alloc_div(bmap);
12780 k = isl_basic_map_alloc_equality(bmap);
12781 if (div < 0 || k < 0)
12782 goto error;
12783 isl_int_set_si(bmap->div[div][0], 0);
12784 isl_seq_cpy(bmap->eq[k] + o_bmap,
12785 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12786 o_bmap += 1 + n_param;
12787 o_ma += 1 + n_param;
12788 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12789 o_bmap += n_before;
12790 isl_seq_cpy(bmap->eq[k] + o_bmap,
12791 ma->u.p[i]->v->el + o_ma, n_in);
12792 o_bmap += n_in;
12793 o_ma += n_in;
12794 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12795 o_bmap += n_after;
12796 isl_seq_cpy(bmap->eq[k] + o_bmap,
12797 ma->u.p[i]->v->el + o_ma, n_div_ma);
12798 o_bmap += n_div_ma;
12799 o_ma += n_div_ma;
12800 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12801 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12802 total++;
12805 return bmap;
12806 error:
12807 isl_basic_map_free(bmap);
12808 return NULL;
12811 /* Replace the domain or range space (depending on "type) of "space" by "set".
12813 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12814 enum isl_dim_type type, __isl_take isl_space *set)
12816 if (type == isl_dim_in) {
12817 space = isl_space_range(space);
12818 space = isl_space_map_from_domain_and_range(set, space);
12819 } else {
12820 space = isl_space_domain(space);
12821 space = isl_space_map_from_domain_and_range(space, set);
12824 return space;
12827 /* Compute the preimage of the domain or range (depending on "type")
12828 * of "bmap" under the function represented by "ma".
12829 * In other words, plug in "ma" in the domain or range of "bmap".
12830 * The result is a basic map that lives in the same space as "bmap"
12831 * except that the domain or range has been replaced by
12832 * the domain space of "ma".
12834 * If bmap is represented by
12836 * A(p) + S u + B x + T v + C(divs) >= 0,
12838 * where u and x are input and output dimensions if type == isl_dim_out
12839 * while x and v are input and output dimensions if type == isl_dim_in,
12840 * and ma is represented by
12842 * x = D(p) + F(y) + G(divs')
12844 * then the result is
12846 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12848 * The divs in the input set are similarly adjusted.
12849 * In particular
12851 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12853 * becomes
12855 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12856 * B_i G(divs') + c_i(divs))/n_i)
12858 * If bmap is not a rational map and if F(y) involves any denominators
12860 * x_i = (f_i y + h_i)/m_i
12862 * then additional constraints are added to ensure that we only
12863 * map back integer points. That is we enforce
12865 * f_i y + h_i = m_i alpha_i
12867 * with alpha_i an additional existentially quantified variable.
12869 * We first copy over the divs from "ma".
12870 * Then we add the modified constraints and divs from "bmap".
12871 * Finally, we add the stride constraints, if needed.
12873 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12874 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12875 __isl_take isl_multi_aff *ma)
12877 int i, k;
12878 isl_space *space;
12879 isl_basic_map *res = NULL;
12880 int n_before, n_after, n_div_bmap, n_div_ma;
12881 isl_int f, c1, c2, g;
12882 isl_bool rational;
12883 int strides;
12885 isl_int_init(f);
12886 isl_int_init(c1);
12887 isl_int_init(c2);
12888 isl_int_init(g);
12890 ma = isl_multi_aff_align_divs(ma);
12891 if (!bmap || !ma)
12892 goto error;
12893 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12894 goto error;
12896 if (type == isl_dim_in) {
12897 n_before = 0;
12898 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12899 } else {
12900 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12901 n_after = 0;
12903 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12904 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12906 space = isl_multi_aff_get_domain_space(ma);
12907 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12908 rational = isl_basic_map_is_rational(bmap);
12909 strides = rational ? 0 : multi_aff_strides(ma);
12910 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12911 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12912 if (rational)
12913 res = isl_basic_map_set_rational(res);
12915 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12916 if (isl_basic_map_alloc_div(res) < 0)
12917 goto error;
12919 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12920 if (!res)
12921 goto error;
12923 for (i = 0; i < bmap->n_eq; ++i) {
12924 k = isl_basic_map_alloc_equality(res);
12925 if (k < 0)
12926 goto error;
12927 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12928 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12931 for (i = 0; i < bmap->n_ineq; ++i) {
12932 k = isl_basic_map_alloc_inequality(res);
12933 if (k < 0)
12934 goto error;
12935 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12936 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12939 for (i = 0; i < bmap->n_div; ++i) {
12940 if (isl_int_is_zero(bmap->div[i][0])) {
12941 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12942 continue;
12944 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12945 n_before, n_after, n_div_ma, n_div_bmap,
12946 f, c1, c2, g, 1);
12949 if (strides)
12950 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12952 isl_int_clear(f);
12953 isl_int_clear(c1);
12954 isl_int_clear(c2);
12955 isl_int_clear(g);
12956 isl_basic_map_free(bmap);
12957 isl_multi_aff_free(ma);
12958 res = isl_basic_map_simplify(res);
12959 return isl_basic_map_finalize(res);
12960 error:
12961 isl_int_clear(f);
12962 isl_int_clear(c1);
12963 isl_int_clear(c2);
12964 isl_int_clear(g);
12965 isl_basic_map_free(bmap);
12966 isl_multi_aff_free(ma);
12967 isl_basic_map_free(res);
12968 return NULL;
12971 /* Compute the preimage of "bset" under the function represented by "ma".
12972 * In other words, plug in "ma" in "bset". The result is a basic set
12973 * that lives in the domain space of "ma".
12975 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12976 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12978 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12981 /* Compute the preimage of the domain of "bmap" under the function
12982 * represented by "ma".
12983 * In other words, plug in "ma" in the domain of "bmap".
12984 * The result is a basic map that lives in the same space as "bmap"
12985 * except that the domain has been replaced by the domain space of "ma".
12987 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12988 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12990 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12993 /* Compute the preimage of the range of "bmap" under the function
12994 * represented by "ma".
12995 * In other words, plug in "ma" in the range of "bmap".
12996 * The result is a basic map that lives in the same space as "bmap"
12997 * except that the range has been replaced by the domain space of "ma".
12999 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13000 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13002 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13005 /* Check if the range of "ma" is compatible with the domain or range
13006 * (depending on "type") of "map".
13007 * Return isl_stat_error if anything is wrong.
13009 static isl_stat check_map_compatible_range_multi_aff(
13010 __isl_keep isl_map *map, enum isl_dim_type type,
13011 __isl_keep isl_multi_aff *ma)
13013 isl_bool m;
13014 isl_space *ma_space;
13016 ma_space = isl_multi_aff_get_space(ma);
13017 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13018 isl_space_free(ma_space);
13019 if (m < 0)
13020 return isl_stat_error;
13021 if (!m)
13022 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13023 "spaces don't match", return isl_stat_error);
13024 return isl_stat_ok;
13027 /* Compute the preimage of the domain or range (depending on "type")
13028 * of "map" under the function represented by "ma".
13029 * In other words, plug in "ma" in the domain or range of "map".
13030 * The result is a map that lives in the same space as "map"
13031 * except that the domain or range has been replaced by
13032 * the domain space of "ma".
13034 * The parameters are assumed to have been aligned.
13036 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13037 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13039 int i;
13040 isl_space *space;
13042 map = isl_map_cow(map);
13043 ma = isl_multi_aff_align_divs(ma);
13044 if (!map || !ma)
13045 goto error;
13046 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13047 goto error;
13049 for (i = 0; i < map->n; ++i) {
13050 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13051 isl_multi_aff_copy(ma));
13052 if (!map->p[i])
13053 goto error;
13056 space = isl_multi_aff_get_domain_space(ma);
13057 space = isl_space_set(isl_map_get_space(map), type, space);
13059 isl_space_free(map->dim);
13060 map->dim = space;
13061 if (!map->dim)
13062 goto error;
13064 isl_multi_aff_free(ma);
13065 if (map->n > 1)
13066 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13067 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13068 return map;
13069 error:
13070 isl_multi_aff_free(ma);
13071 isl_map_free(map);
13072 return NULL;
13075 /* Compute the preimage of the domain or range (depending on "type")
13076 * of "map" under the function represented by "ma".
13077 * In other words, plug in "ma" in the domain or range of "map".
13078 * The result is a map that lives in the same space as "map"
13079 * except that the domain or range has been replaced by
13080 * the domain space of "ma".
13082 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13083 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13085 isl_bool aligned;
13087 if (!map || !ma)
13088 goto error;
13090 aligned = isl_map_space_has_equal_params(map, ma->space);
13091 if (aligned < 0)
13092 goto error;
13093 if (aligned)
13094 return map_preimage_multi_aff(map, type, ma);
13096 if (isl_map_check_named_params(map) < 0)
13097 goto error;
13098 if (!isl_space_has_named_params(ma->space))
13099 isl_die(map->ctx, isl_error_invalid,
13100 "unaligned unnamed parameters", goto error);
13101 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13102 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13104 return map_preimage_multi_aff(map, type, ma);
13105 error:
13106 isl_multi_aff_free(ma);
13107 return isl_map_free(map);
13110 /* Compute the preimage of "set" under the function represented by "ma".
13111 * In other words, plug in "ma" in "set". The result is a set
13112 * that lives in the domain space of "ma".
13114 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13115 __isl_take isl_multi_aff *ma)
13117 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13120 /* Compute the preimage of the domain of "map" under the function
13121 * represented by "ma".
13122 * In other words, plug in "ma" in the domain of "map".
13123 * The result is a map that lives in the same space as "map"
13124 * except that the domain has been replaced by the domain space of "ma".
13126 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13127 __isl_take isl_multi_aff *ma)
13129 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13132 /* Compute the preimage of the range of "map" under the function
13133 * represented by "ma".
13134 * In other words, plug in "ma" in the range of "map".
13135 * The result is a map that lives in the same space as "map"
13136 * except that the range has been replaced by the domain space of "ma".
13138 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13139 __isl_take isl_multi_aff *ma)
13141 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13144 /* Compute the preimage of "map" under the function represented by "pma".
13145 * In other words, plug in "pma" in the domain or range of "map".
13146 * The result is a map that lives in the same space as "map",
13147 * except that the space of type "type" has been replaced by
13148 * the domain space of "pma".
13150 * The parameters of "map" and "pma" are assumed to have been aligned.
13152 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13153 __isl_take isl_map *map, enum isl_dim_type type,
13154 __isl_take isl_pw_multi_aff *pma)
13156 int i;
13157 isl_map *res;
13159 if (!pma)
13160 goto error;
13162 if (pma->n == 0) {
13163 isl_pw_multi_aff_free(pma);
13164 res = isl_map_empty(isl_map_get_space(map));
13165 isl_map_free(map);
13166 return res;
13169 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13170 isl_multi_aff_copy(pma->p[0].maff));
13171 if (type == isl_dim_in)
13172 res = isl_map_intersect_domain(res,
13173 isl_map_copy(pma->p[0].set));
13174 else
13175 res = isl_map_intersect_range(res,
13176 isl_map_copy(pma->p[0].set));
13178 for (i = 1; i < pma->n; ++i) {
13179 isl_map *res_i;
13181 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13182 isl_multi_aff_copy(pma->p[i].maff));
13183 if (type == isl_dim_in)
13184 res_i = isl_map_intersect_domain(res_i,
13185 isl_map_copy(pma->p[i].set));
13186 else
13187 res_i = isl_map_intersect_range(res_i,
13188 isl_map_copy(pma->p[i].set));
13189 res = isl_map_union(res, res_i);
13192 isl_pw_multi_aff_free(pma);
13193 isl_map_free(map);
13194 return res;
13195 error:
13196 isl_pw_multi_aff_free(pma);
13197 isl_map_free(map);
13198 return NULL;
13201 /* Compute the preimage of "map" under the function represented by "pma".
13202 * In other words, plug in "pma" in the domain or range of "map".
13203 * The result is a map that lives in the same space as "map",
13204 * except that the space of type "type" has been replaced by
13205 * the domain space of "pma".
13207 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13208 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13210 isl_bool aligned;
13212 if (!map || !pma)
13213 goto error;
13215 aligned = isl_map_space_has_equal_params(map, pma->dim);
13216 if (aligned < 0)
13217 goto error;
13218 if (aligned)
13219 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13221 if (isl_map_check_named_params(map) < 0)
13222 goto error;
13223 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13224 goto error;
13225 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13226 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13228 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13229 error:
13230 isl_pw_multi_aff_free(pma);
13231 return isl_map_free(map);
13234 /* Compute the preimage of "set" under the function represented by "pma".
13235 * In other words, plug in "pma" in "set". The result is a set
13236 * that lives in the domain space of "pma".
13238 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13239 __isl_take isl_pw_multi_aff *pma)
13241 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13244 /* Compute the preimage of the domain of "map" under the function
13245 * represented by "pma".
13246 * In other words, plug in "pma" in the domain of "map".
13247 * The result is a map that lives in the same space as "map",
13248 * except that domain space has been replaced by the domain space of "pma".
13250 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13251 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13253 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13256 /* Compute the preimage of the range of "map" under the function
13257 * represented by "pma".
13258 * In other words, plug in "pma" in the range of "map".
13259 * The result is a map that lives in the same space as "map",
13260 * except that range space has been replaced by the domain space of "pma".
13262 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13263 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13265 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13268 /* Compute the preimage of "map" under the function represented by "mpa".
13269 * In other words, plug in "mpa" in the domain or range of "map".
13270 * The result is a map that lives in the same space as "map",
13271 * except that the space of type "type" has been replaced by
13272 * the domain space of "mpa".
13274 * If the map does not involve any constraints that refer to the
13275 * dimensions of the substituted space, then the only possible
13276 * effect of "mpa" on the map is to map the space to a different space.
13277 * We create a separate isl_multi_aff to effectuate this change
13278 * in order to avoid spurious splitting of the map along the pieces
13279 * of "mpa".
13280 * If "mpa" has a non-trivial explicit domain, however,
13281 * then the full substitution should be performed.
13283 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13284 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13286 int n;
13287 isl_bool full;
13288 isl_pw_multi_aff *pma;
13290 if (!map || !mpa)
13291 goto error;
13293 n = isl_map_dim(map, type);
13294 full = isl_map_involves_dims(map, type, 0, n);
13295 if (full >= 0 && !full)
13296 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13297 if (full < 0)
13298 goto error;
13299 if (!full) {
13300 isl_space *space;
13301 isl_multi_aff *ma;
13303 space = isl_multi_pw_aff_get_space(mpa);
13304 isl_multi_pw_aff_free(mpa);
13305 ma = isl_multi_aff_zero(space);
13306 return isl_map_preimage_multi_aff(map, type, ma);
13309 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13310 return isl_map_preimage_pw_multi_aff(map, type, pma);
13311 error:
13312 isl_map_free(map);
13313 isl_multi_pw_aff_free(mpa);
13314 return NULL;
13317 /* Compute the preimage of "map" under the function represented by "mpa".
13318 * In other words, plug in "mpa" in the domain "map".
13319 * The result is a map that lives in the same space as "map",
13320 * except that domain space has been replaced by the domain space of "mpa".
13322 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13323 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13325 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13328 /* Compute the preimage of "set" by the function represented by "mpa".
13329 * In other words, plug in "mpa" in "set".
13331 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13332 __isl_take isl_multi_pw_aff *mpa)
13334 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13337 /* Return a copy of the equality constraints of "bset" as a matrix.
13339 __isl_give isl_mat *isl_basic_set_extract_equalities(
13340 __isl_keep isl_basic_set *bset)
13342 isl_ctx *ctx;
13343 unsigned total;
13345 if (!bset)
13346 return NULL;
13348 ctx = isl_basic_set_get_ctx(bset);
13349 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13350 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13353 /* Are the "n" "coefficients" starting at "first" of the integer division
13354 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13355 * to each other?
13356 * The "coefficient" at position 0 is the denominator.
13357 * The "coefficient" at position 1 is the constant term.
13359 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13360 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13361 unsigned first, unsigned n)
13363 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13364 return isl_bool_error;
13365 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13366 return isl_bool_error;
13367 return isl_seq_eq(bmap1->div[pos1] + first,
13368 bmap2->div[pos2] + first, n);
13371 /* Are the integer division expressions at position "pos1" in "bmap1" and
13372 * "pos2" in "bmap2" equal to each other, except that the constant terms
13373 * are different?
13375 isl_bool isl_basic_map_equal_div_expr_except_constant(
13376 __isl_keep isl_basic_map *bmap1, int pos1,
13377 __isl_keep isl_basic_map *bmap2, int pos2)
13379 isl_bool equal;
13380 unsigned total;
13382 if (!bmap1 || !bmap2)
13383 return isl_bool_error;
13384 total = isl_basic_map_total_dim(bmap1);
13385 if (total != isl_basic_map_total_dim(bmap2))
13386 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13387 "incomparable div expressions", return isl_bool_error);
13388 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13389 0, 1);
13390 if (equal < 0 || !equal)
13391 return equal;
13392 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13393 1, 1);
13394 if (equal < 0 || equal)
13395 return isl_bool_not(equal);
13396 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13397 2, total);
13400 /* Replace the numerator of the constant term of the integer division
13401 * expression at position "div" in "bmap" by "value".
13402 * The caller guarantees that this does not change the meaning
13403 * of the input.
13405 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13406 __isl_take isl_basic_map *bmap, int div, int value)
13408 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13409 return isl_basic_map_free(bmap);
13411 isl_int_set_si(bmap->div[div][1], value);
13413 return bmap;
13416 /* Is the point "inner" internal to inequality constraint "ineq"
13417 * of "bset"?
13418 * The point is considered to be internal to the inequality constraint,
13419 * if it strictly lies on the positive side of the inequality constraint,
13420 * or if it lies on the constraint and the constraint is lexico-positive.
13422 static isl_bool is_internal(__isl_keep isl_vec *inner,
13423 __isl_keep isl_basic_set *bset, int ineq)
13425 isl_ctx *ctx;
13426 int pos;
13427 unsigned total;
13429 if (!inner || !bset)
13430 return isl_bool_error;
13432 ctx = isl_basic_set_get_ctx(bset);
13433 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13434 &ctx->normalize_gcd);
13435 if (!isl_int_is_zero(ctx->normalize_gcd))
13436 return isl_int_is_nonneg(ctx->normalize_gcd);
13438 total = isl_basic_set_dim(bset, isl_dim_all);
13439 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13440 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13443 /* Tighten the inequality constraints of "bset" that are outward with respect
13444 * to the point "vec".
13445 * That is, tighten the constraints that are not satisfied by "vec".
13447 * "vec" is a point internal to some superset S of "bset" that is used
13448 * to make the subsets of S disjoint, by tightening one half of the constraints
13449 * that separate two subsets. In particular, the constraints of S
13450 * are all satisfied by "vec" and should not be tightened.
13451 * Of the internal constraints, those that have "vec" on the outside
13452 * are tightened. The shared facet is included in the adjacent subset
13453 * with the opposite constraint.
13454 * For constraints that saturate "vec", this criterion cannot be used
13455 * to determine which of the two sides should be tightened.
13456 * Instead, the sign of the first non-zero coefficient is used
13457 * to make this choice. Note that this second criterion is never used
13458 * on the constraints of S since "vec" is interior to "S".
13460 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13461 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13463 int j;
13465 bset = isl_basic_set_cow(bset);
13466 if (!bset)
13467 return NULL;
13468 for (j = 0; j < bset->n_ineq; ++j) {
13469 isl_bool internal;
13471 internal = is_internal(vec, bset, j);
13472 if (internal < 0)
13473 return isl_basic_set_free(bset);
13474 if (internal)
13475 continue;
13476 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13479 return bset;
13482 /* Replace the variables x of type "type" starting at "first" in "bmap"
13483 * by x' with x = M x' with M the matrix trans.
13484 * That is, replace the corresponding coefficients c by c M.
13486 * The transformation matrix should be a square matrix.
13488 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13489 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13490 __isl_take isl_mat *trans)
13492 unsigned pos;
13494 bmap = isl_basic_map_cow(bmap);
13495 if (!bmap || !trans)
13496 goto error;
13498 if (trans->n_row != trans->n_col)
13499 isl_die(trans->ctx, isl_error_invalid,
13500 "expecting square transformation matrix", goto error);
13501 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13502 goto error;
13504 pos = isl_basic_map_offset(bmap, type) + first;
13506 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13507 isl_mat_copy(trans)) < 0)
13508 goto error;
13509 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13510 isl_mat_copy(trans)) < 0)
13511 goto error;
13512 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13513 isl_mat_copy(trans)) < 0)
13514 goto error;
13516 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13517 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13519 isl_mat_free(trans);
13520 return bmap;
13521 error:
13522 isl_mat_free(trans);
13523 isl_basic_map_free(bmap);
13524 return NULL;
13527 /* Replace the variables x of type "type" starting at "first" in "bset"
13528 * by x' with x = M x' with M the matrix trans.
13529 * That is, replace the corresponding coefficients c by c M.
13531 * The transformation matrix should be a square matrix.
13533 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13534 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13535 __isl_take isl_mat *trans)
13537 return isl_basic_map_transform_dims(bset, type, first, trans);