isl_basic_map_transform_dims: reuse isl_basic_map_check_range
[isl.git] / isl_map.c
blob53215dc31aa5a6ff2b624b703c3f839d5ec9813a
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 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
488 return isl_basic_map_free(bmap);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "map" has only named parameters, reporting an error
1390 * if it does not.
1392 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1394 return isl_space_check_named_params(isl_map_peek_space(map));
1397 /* Check that "bmap" has only named parameters, reporting an error
1398 * if it does not.
1400 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1402 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1405 /* Check that "bmap1" and "bmap2" have the same parameters,
1406 * reporting an error if they do not.
1408 static isl_stat isl_basic_map_check_equal_params(
1409 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1411 isl_bool match;
1413 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1414 if (match < 0)
1415 return isl_stat_error;
1416 if (!match)
1417 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1418 "parameters don't match", return isl_stat_error);
1419 return isl_stat_ok;
1422 __isl_give isl_map *isl_map_align_params_map_map_and(
1423 __isl_take isl_map *map1, __isl_take isl_map *map2,
1424 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1425 __isl_take isl_map *map2))
1427 if (!map1 || !map2)
1428 goto error;
1429 if (isl_map_has_equal_params(map1, map2))
1430 return fn(map1, map2);
1431 if (isl_map_check_named_params(map1) < 0)
1432 goto error;
1433 if (isl_map_check_named_params(map2) < 0)
1434 goto error;
1435 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1436 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1437 return fn(map1, map2);
1438 error:
1439 isl_map_free(map1);
1440 isl_map_free(map2);
1441 return NULL;
1444 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1445 __isl_keep isl_map *map2,
1446 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1448 isl_bool r;
1450 if (!map1 || !map2)
1451 return isl_bool_error;
1452 if (isl_map_has_equal_params(map1, map2))
1453 return fn(map1, map2);
1454 if (isl_map_check_named_params(map1) < 0)
1455 return isl_bool_error;
1456 if (isl_map_check_named_params(map2) < 0)
1457 return isl_bool_error;
1458 map1 = isl_map_copy(map1);
1459 map2 = isl_map_copy(map2);
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 r = fn(map1, map2);
1463 isl_map_free(map1);
1464 isl_map_free(map2);
1465 return r;
1468 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1470 struct isl_ctx *ctx;
1471 if (!bmap)
1472 return -1;
1473 ctx = bmap->ctx;
1474 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1475 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1476 return -1);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1479 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1480 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1481 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1482 isl_int *t;
1483 int j = isl_basic_map_alloc_inequality(bmap);
1484 if (j < 0)
1485 return -1;
1486 t = bmap->ineq[j];
1487 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1488 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1489 bmap->eq[-1] = t;
1490 bmap->n_eq++;
1491 bmap->n_ineq--;
1492 bmap->eq--;
1493 return 0;
1495 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1496 bmap->extra - bmap->n_div);
1497 return bmap->n_eq++;
1500 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1502 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1505 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1507 if (!bmap)
1508 return -1;
1509 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1510 bmap->n_eq -= n;
1511 return 0;
1514 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1516 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1519 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1521 isl_int *t;
1522 if (!bmap)
1523 return -1;
1524 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1526 if (pos != bmap->n_eq - 1) {
1527 t = bmap->eq[pos];
1528 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1529 bmap->eq[bmap->n_eq - 1] = t;
1531 bmap->n_eq--;
1532 return 0;
1535 /* Turn inequality "pos" of "bmap" into an equality.
1537 * In particular, we move the inequality in front of the equalities
1538 * and move the last inequality in the position of the moved inequality.
1539 * Note that isl_tab_make_equalities_explicit depends on this particular
1540 * change in the ordering of the constraints.
1542 void isl_basic_map_inequality_to_equality(
1543 struct isl_basic_map *bmap, unsigned pos)
1545 isl_int *t;
1547 t = bmap->ineq[pos];
1548 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1549 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1550 bmap->eq[-1] = t;
1551 bmap->n_eq++;
1552 bmap->n_ineq--;
1553 bmap->eq--;
1554 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1555 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1557 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1560 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1562 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1565 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1567 struct isl_ctx *ctx;
1568 if (!bmap)
1569 return -1;
1570 ctx = bmap->ctx;
1571 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1572 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1574 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1575 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1576 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1577 1 + isl_basic_map_total_dim(bmap),
1578 bmap->extra - bmap->n_div);
1579 return bmap->n_ineq++;
1582 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1584 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1587 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1589 if (!bmap)
1590 return -1;
1591 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1592 bmap->n_ineq -= n;
1593 return 0;
1596 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1598 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1601 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1603 isl_int *t;
1604 if (!bmap)
1605 return -1;
1606 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1608 if (pos != bmap->n_ineq - 1) {
1609 t = bmap->ineq[pos];
1610 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1611 bmap->ineq[bmap->n_ineq - 1] = t;
1612 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1614 bmap->n_ineq--;
1615 return 0;
1618 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1620 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1623 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1624 isl_int *eq)
1626 isl_bool empty;
1627 int k;
1629 empty = isl_basic_map_plain_is_empty(bmap);
1630 if (empty < 0)
1631 return isl_basic_map_free(bmap);
1632 if (empty)
1633 return bmap;
1635 bmap = isl_basic_map_cow(bmap);
1636 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1637 if (!bmap)
1638 return NULL;
1639 k = isl_basic_map_alloc_equality(bmap);
1640 if (k < 0)
1641 goto error;
1642 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1643 return bmap;
1644 error:
1645 isl_basic_map_free(bmap);
1646 return NULL;
1649 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1650 isl_int *eq)
1652 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1655 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1656 isl_int *ineq)
1658 int k;
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1662 if (!bmap)
1663 return NULL;
1664 k = isl_basic_map_alloc_inequality(bmap);
1665 if (k < 0)
1666 goto error;
1667 isl_seq_cpy(bmap->ineq[k], ineq, 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_ineq(__isl_take isl_basic_set *bset,
1675 isl_int *ineq)
1677 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1680 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1682 if (!bmap)
1683 return -1;
1684 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1685 isl_seq_clr(bmap->div[bmap->n_div] +
1686 1 + 1 + isl_basic_map_total_dim(bmap),
1687 bmap->extra - bmap->n_div);
1688 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1689 return bmap->n_div++;
1692 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1694 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1697 /* Check that there are "n" dimensions of type "type" starting at "first"
1698 * in "bmap".
1700 isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 unsigned dim;
1705 if (!bmap)
1706 return isl_stat_error;
1707 dim = isl_basic_map_dim(bmap, type);
1708 if (first + n > dim || first + n < first)
1709 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1710 "position or range out of bounds",
1711 return isl_stat_error);
1712 return isl_stat_ok;
1715 /* Insert an extra integer division, prescribed by "div", to "bmap"
1716 * at (integer division) position "pos".
1718 * The integer division is first added at the end and then moved
1719 * into the right position.
1721 __isl_give isl_basic_map *isl_basic_map_insert_div(
1722 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1724 int i, k;
1726 bmap = isl_basic_map_cow(bmap);
1727 if (!bmap || !div)
1728 return isl_basic_map_free(bmap);
1730 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1731 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1732 "unexpected size", return isl_basic_map_free(bmap));
1733 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1734 return isl_basic_map_free(bmap);
1736 bmap = isl_basic_map_extend_space(bmap,
1737 isl_basic_map_get_space(bmap), 1, 0, 2);
1738 k = isl_basic_map_alloc_div(bmap);
1739 if (k < 0)
1740 return isl_basic_map_free(bmap);
1741 isl_seq_cpy(bmap->div[k], div->el, div->size);
1742 isl_int_set_si(bmap->div[k][div->size], 0);
1744 for (i = k; i > pos; --i)
1745 isl_basic_map_swap_div(bmap, i, i - 1);
1747 return bmap;
1750 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1752 if (!bmap)
1753 return isl_stat_error;
1754 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1755 bmap->n_div -= n;
1756 return isl_stat_ok;
1759 static __isl_give isl_basic_map *add_constraints(
1760 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1761 unsigned i_pos, unsigned o_pos)
1763 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1764 isl_ctx *ctx;
1765 isl_space *space;
1766 struct isl_dim_map *dim_map;
1768 space = isl_basic_map_peek_space(bmap2);
1769 if (!bmap1 || !space)
1770 goto error;
1772 total = isl_basic_map_dim(bmap1, isl_dim_all);
1773 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1774 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1775 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1776 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1777 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1778 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1779 ctx = isl_basic_map_get_ctx(bmap1);
1780 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1781 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1782 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1783 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1784 isl_dim_map_div(dim_map, bmap2, total);
1786 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1787 error:
1788 isl_basic_map_free(bmap1);
1789 isl_basic_map_free(bmap2);
1790 return NULL;
1793 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1794 struct isl_basic_set *bset2, unsigned pos)
1796 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1797 bset_to_bmap(bset2), 0, pos));
1800 __isl_give isl_basic_map *isl_basic_map_extend_space(
1801 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1802 unsigned extra, unsigned n_eq, unsigned n_ineq)
1804 struct isl_basic_map *ext;
1805 unsigned flags;
1806 int dims_ok;
1808 if (!space)
1809 goto error;
1811 if (!base)
1812 goto error;
1814 dims_ok = isl_space_is_equal(base->dim, space) &&
1815 base->extra >= base->n_div + extra;
1817 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1818 room_for_ineq(base, n_ineq)) {
1819 isl_space_free(space);
1820 return base;
1823 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1824 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1825 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1826 extra += base->extra;
1827 n_eq += base->n_eq;
1828 n_ineq += base->n_ineq;
1830 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1831 space = NULL;
1832 if (!ext)
1833 goto error;
1835 if (dims_ok)
1836 ext->sample = isl_vec_copy(base->sample);
1837 flags = base->flags;
1838 ext = add_constraints(ext, base, 0, 0);
1839 if (ext) {
1840 ext->flags = flags;
1841 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1844 return ext;
1846 error:
1847 isl_space_free(space);
1848 isl_basic_map_free(base);
1849 return NULL;
1852 __isl_give isl_basic_set *isl_basic_set_extend_space(
1853 __isl_take isl_basic_set *base,
1854 __isl_take isl_space *dim, unsigned extra,
1855 unsigned n_eq, unsigned n_ineq)
1857 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1858 dim, extra, n_eq, n_ineq));
1861 struct isl_basic_map *isl_basic_map_extend_constraints(
1862 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1864 if (!base)
1865 return NULL;
1866 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1867 0, n_eq, n_ineq);
1870 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1871 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1872 unsigned n_eq, unsigned n_ineq)
1874 struct isl_basic_map *bmap;
1875 isl_space *dim;
1877 if (!base)
1878 return NULL;
1879 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1880 if (!dim)
1881 goto error;
1883 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1884 return bmap;
1885 error:
1886 isl_basic_map_free(base);
1887 return NULL;
1890 struct isl_basic_set *isl_basic_set_extend_constraints(
1891 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1893 isl_basic_map *bmap = bset_to_bmap(base);
1894 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1895 return bset_from_bmap(bmap);
1898 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1900 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1903 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1905 if (!bmap)
1906 return NULL;
1908 if (bmap->ref > 1) {
1909 bmap->ref--;
1910 bmap = isl_basic_map_dup(bmap);
1912 if (bmap) {
1913 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1914 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1916 return bmap;
1919 /* Clear all cached information in "map", either because it is about
1920 * to be modified or because it is being freed.
1921 * Always return the same pointer that is passed in.
1922 * This is needed for the use in isl_map_free.
1924 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1926 isl_basic_map_free(map->cached_simple_hull[0]);
1927 isl_basic_map_free(map->cached_simple_hull[1]);
1928 map->cached_simple_hull[0] = NULL;
1929 map->cached_simple_hull[1] = NULL;
1930 return map;
1933 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1935 return isl_map_cow(set);
1938 /* Return an isl_map that is equal to "map" and that has only
1939 * a single reference.
1941 * If the original input already has only one reference, then
1942 * simply return it, but clear all cached information, since
1943 * it may be rendered invalid by the operations that will be
1944 * performed on the result.
1946 * Otherwise, create a duplicate (without any cached information).
1948 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1950 if (!map)
1951 return NULL;
1953 if (map->ref == 1)
1954 return clear_caches(map);
1955 map->ref--;
1956 return isl_map_dup(map);
1959 static void swap_vars(struct isl_blk blk, isl_int *a,
1960 unsigned a_len, unsigned b_len)
1962 isl_seq_cpy(blk.data, a+a_len, b_len);
1963 isl_seq_cpy(blk.data+b_len, a, a_len);
1964 isl_seq_cpy(a, blk.data, b_len+a_len);
1967 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1968 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1970 int i;
1971 struct isl_blk blk;
1973 if (!bmap)
1974 goto error;
1976 isl_assert(bmap->ctx,
1977 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1979 if (n1 == 0 || n2 == 0)
1980 return bmap;
1982 bmap = isl_basic_map_cow(bmap);
1983 if (!bmap)
1984 return NULL;
1986 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1987 if (isl_blk_is_error(blk))
1988 goto error;
1990 for (i = 0; i < bmap->n_eq; ++i)
1991 swap_vars(blk,
1992 bmap->eq[i] + pos, n1, n2);
1994 for (i = 0; i < bmap->n_ineq; ++i)
1995 swap_vars(blk,
1996 bmap->ineq[i] + pos, n1, n2);
1998 for (i = 0; i < bmap->n_div; ++i)
1999 swap_vars(blk,
2000 bmap->div[i]+1 + pos, n1, n2);
2002 isl_blk_free(bmap->ctx, blk);
2004 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2005 bmap = isl_basic_map_gauss(bmap, NULL);
2006 return isl_basic_map_finalize(bmap);
2007 error:
2008 isl_basic_map_free(bmap);
2009 return NULL;
2012 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2013 __isl_take isl_basic_map *bmap)
2015 int i = 0;
2016 unsigned total;
2017 if (!bmap)
2018 goto error;
2019 total = isl_basic_map_total_dim(bmap);
2020 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2021 return isl_basic_map_free(bmap);
2022 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2023 if (bmap->n_eq > 0)
2024 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2025 else {
2026 i = isl_basic_map_alloc_equality(bmap);
2027 if (i < 0)
2028 goto error;
2030 isl_int_set_si(bmap->eq[i][0], 1);
2031 isl_seq_clr(bmap->eq[i]+1, total);
2032 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2033 isl_vec_free(bmap->sample);
2034 bmap->sample = NULL;
2035 return isl_basic_map_finalize(bmap);
2036 error:
2037 isl_basic_map_free(bmap);
2038 return NULL;
2041 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2042 __isl_take isl_basic_set *bset)
2044 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2047 __isl_give isl_basic_map *isl_basic_map_set_rational(
2048 __isl_take isl_basic_map *bmap)
2050 if (!bmap)
2051 return NULL;
2053 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2054 return bmap;
2056 bmap = isl_basic_map_cow(bmap);
2057 if (!bmap)
2058 return NULL;
2060 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2062 return isl_basic_map_finalize(bmap);
2065 __isl_give isl_basic_set *isl_basic_set_set_rational(
2066 __isl_take isl_basic_set *bset)
2068 return isl_basic_map_set_rational(bset);
2071 __isl_give isl_basic_set *isl_basic_set_set_integral(
2072 __isl_take isl_basic_set *bset)
2074 if (!bset)
2075 return NULL;
2077 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2078 return bset;
2080 bset = isl_basic_set_cow(bset);
2081 if (!bset)
2082 return NULL;
2084 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2086 return isl_basic_set_finalize(bset);
2089 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2091 int i;
2093 map = isl_map_cow(map);
2094 if (!map)
2095 return NULL;
2096 for (i = 0; i < map->n; ++i) {
2097 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2098 if (!map->p[i])
2099 goto error;
2101 return map;
2102 error:
2103 isl_map_free(map);
2104 return NULL;
2107 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2109 return isl_map_set_rational(set);
2112 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2113 * of "bmap").
2115 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2117 isl_int *t = bmap->div[a];
2118 bmap->div[a] = bmap->div[b];
2119 bmap->div[b] = t;
2122 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2123 * div definitions accordingly.
2125 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2127 int i;
2128 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2130 swap_div(bmap, a, b);
2132 for (i = 0; i < bmap->n_eq; ++i)
2133 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2135 for (i = 0; i < bmap->n_ineq; ++i)
2136 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2138 for (i = 0; i < bmap->n_div; ++i)
2139 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2140 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2143 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2145 isl_seq_cpy(c, c + n, rem);
2146 isl_seq_clr(c + rem, n);
2149 /* Drop n dimensions starting at first.
2151 * In principle, this frees up some extra variables as the number
2152 * of columns remains constant, but we would have to extend
2153 * the div array too as the number of rows in this array is assumed
2154 * to be equal to extra.
2156 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2157 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2159 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2162 /* Move "n" divs starting at "first" to the end of the list of divs.
2164 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2165 unsigned first, unsigned n)
2167 isl_int **div;
2168 int i;
2170 if (first + n == bmap->n_div)
2171 return bmap;
2173 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2174 if (!div)
2175 goto error;
2176 for (i = 0; i < n; ++i)
2177 div[i] = bmap->div[first + i];
2178 for (i = 0; i < bmap->n_div - first - n; ++i)
2179 bmap->div[first + i] = bmap->div[first + n + i];
2180 for (i = 0; i < n; ++i)
2181 bmap->div[bmap->n_div - n + i] = div[i];
2182 free(div);
2183 return bmap;
2184 error:
2185 isl_basic_map_free(bmap);
2186 return NULL;
2189 /* Check that there are "n" dimensions of type "type" starting at "first"
2190 * in "map".
2192 static isl_stat isl_map_check_range(__isl_keep isl_map *map,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 if (!map)
2196 return isl_stat_error;
2197 if (first + n > isl_map_dim(map, type) || first + n < first)
2198 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2199 "position or range out of bounds",
2200 return isl_stat_error);
2201 return isl_stat_ok;
2204 /* Drop "n" dimensions of type "type" starting at "first".
2205 * Perform the core computation, without cowing or
2206 * simplifying and finalizing the result.
2208 * In principle, this frees up some extra variables as the number
2209 * of columns remains constant, but we would have to extend
2210 * the div array too as the number of rows in this array is assumed
2211 * to be equal to extra.
2213 __isl_give isl_basic_map *isl_basic_map_drop_core(
2214 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2215 unsigned first, unsigned n)
2217 int i;
2218 unsigned offset;
2219 unsigned left;
2221 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2222 return isl_basic_map_free(bmap);
2224 offset = isl_basic_map_offset(bmap, type) + first;
2225 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2226 for (i = 0; i < bmap->n_eq; ++i)
2227 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2229 for (i = 0; i < bmap->n_ineq; ++i)
2230 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2232 for (i = 0; i < bmap->n_div; ++i)
2233 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2235 if (type == isl_dim_div) {
2236 bmap = move_divs_last(bmap, first, n);
2237 if (!bmap)
2238 return NULL;
2239 if (isl_basic_map_free_div(bmap, n) < 0)
2240 return isl_basic_map_free(bmap);
2241 } else
2242 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2243 if (!bmap->dim)
2244 return isl_basic_map_free(bmap);
2246 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2247 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2248 return bmap;
2251 /* Drop "n" dimensions of type "type" starting at "first".
2253 * In principle, this frees up some extra variables as the number
2254 * of columns remains constant, but we would have to extend
2255 * the div array too as the number of rows in this array is assumed
2256 * to be equal to extra.
2258 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2259 enum isl_dim_type type, unsigned first, unsigned n)
2261 if (!bmap)
2262 return NULL;
2263 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2264 return bmap;
2266 bmap = isl_basic_map_cow(bmap);
2267 if (!bmap)
2268 return NULL;
2270 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2272 bmap = isl_basic_map_simplify(bmap);
2273 return isl_basic_map_finalize(bmap);
2276 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2277 enum isl_dim_type type, unsigned first, unsigned n)
2279 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2280 type, first, n));
2283 /* No longer consider "map" to be normalized.
2285 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2287 if (!map)
2288 return NULL;
2289 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2290 return map;
2293 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2294 enum isl_dim_type type, unsigned first, unsigned n)
2296 int i;
2298 if (isl_map_check_range(map, type, first, n) < 0)
2299 return isl_map_free(map);
2301 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2302 return map;
2303 map = isl_map_cow(map);
2304 if (!map)
2305 goto error;
2306 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2307 if (!map->dim)
2308 goto error;
2310 for (i = 0; i < map->n; ++i) {
2311 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2312 if (!map->p[i])
2313 goto error;
2315 map = isl_map_unmark_normalized(map);
2317 return map;
2318 error:
2319 isl_map_free(map);
2320 return NULL;
2323 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2324 enum isl_dim_type type, unsigned first, unsigned n)
2326 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2329 /* Drop the integer division at position "div", which is assumed
2330 * not to appear in any of the constraints or
2331 * in any of the other integer divisions.
2333 * Since the integer division is redundant, there is no need to cow.
2335 __isl_give isl_basic_map *isl_basic_map_drop_div(
2336 __isl_take isl_basic_map *bmap, unsigned div)
2338 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2341 /* Eliminate the specified n dimensions starting at first from the
2342 * constraints, without removing the dimensions from the space.
2343 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2345 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2346 enum isl_dim_type type, unsigned first, unsigned n)
2348 int i;
2350 if (n == 0)
2351 return map;
2353 if (isl_map_check_range(map, type, first, n) < 0)
2354 return isl_map_free(map);
2356 map = isl_map_cow(map);
2357 if (!map)
2358 return NULL;
2360 for (i = 0; i < map->n; ++i) {
2361 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2362 if (!map->p[i])
2363 goto error;
2365 return map;
2366 error:
2367 isl_map_free(map);
2368 return NULL;
2371 /* Eliminate the specified n dimensions starting at first from the
2372 * constraints, without removing the dimensions from the space.
2373 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2375 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2376 enum isl_dim_type type, unsigned first, unsigned n)
2378 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2381 /* Eliminate the specified n dimensions starting at first from the
2382 * constraints, without removing the dimensions from the space.
2383 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2385 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2386 unsigned first, unsigned n)
2388 return isl_set_eliminate(set, isl_dim_set, first, n);
2391 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2392 __isl_take isl_basic_map *bmap)
2394 if (!bmap)
2395 return NULL;
2396 bmap = isl_basic_map_eliminate_vars(bmap,
2397 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2398 if (!bmap)
2399 return NULL;
2400 bmap->n_div = 0;
2401 return isl_basic_map_finalize(bmap);
2404 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2405 __isl_take isl_basic_set *bset)
2407 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2410 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2412 int i;
2414 if (!map)
2415 return NULL;
2416 if (map->n == 0)
2417 return map;
2419 map = isl_map_cow(map);
2420 if (!map)
2421 return NULL;
2423 for (i = 0; i < map->n; ++i) {
2424 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2425 if (!map->p[i])
2426 goto error;
2428 return map;
2429 error:
2430 isl_map_free(map);
2431 return NULL;
2434 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2436 return isl_map_remove_divs(set);
2439 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2440 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2441 unsigned first, unsigned n)
2443 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2444 return isl_basic_map_free(bmap);
2445 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2446 return bmap;
2447 bmap = isl_basic_map_eliminate_vars(bmap,
2448 isl_basic_map_offset(bmap, type) - 1 + first, n);
2449 if (!bmap)
2450 return bmap;
2451 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2452 return bmap;
2453 bmap = isl_basic_map_drop(bmap, type, first, n);
2454 return bmap;
2457 /* Return true if the definition of the given div (recursively) involves
2458 * any of the given variables.
2460 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2461 unsigned first, unsigned n)
2463 int i;
2464 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2466 if (isl_int_is_zero(bmap->div[div][0]))
2467 return isl_bool_false;
2468 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2469 return isl_bool_true;
2471 for (i = bmap->n_div - 1; i >= 0; --i) {
2472 isl_bool involves;
2474 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2475 continue;
2476 involves = div_involves_vars(bmap, i, first, n);
2477 if (involves < 0 || involves)
2478 return involves;
2481 return isl_bool_false;
2484 /* Try and add a lower and/or upper bound on "div" to "bmap"
2485 * based on inequality "i".
2486 * "total" is the total number of variables (excluding the divs).
2487 * "v" is a temporary object that can be used during the calculations.
2488 * If "lb" is set, then a lower bound should be constructed.
2489 * If "ub" is set, then an upper bound should be constructed.
2491 * The calling function has already checked that the inequality does not
2492 * reference "div", but we still need to check that the inequality is
2493 * of the right form. We'll consider the case where we want to construct
2494 * a lower bound. The construction of upper bounds is similar.
2496 * Let "div" be of the form
2498 * q = floor((a + f(x))/d)
2500 * We essentially check if constraint "i" is of the form
2502 * b + f(x) >= 0
2504 * so that we can use it to derive a lower bound on "div".
2505 * However, we allow a slightly more general form
2507 * b + g(x) >= 0
2509 * with the condition that the coefficients of g(x) - f(x) are all
2510 * divisible by d.
2511 * Rewriting this constraint as
2513 * 0 >= -b - g(x)
2515 * adding a + f(x) to both sides and dividing by d, we obtain
2517 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2519 * Taking the floor on both sides, we obtain
2521 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2523 * or
2525 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2527 * In the case of an upper bound, we construct the constraint
2529 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2532 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2533 __isl_take isl_basic_map *bmap, int div, int i,
2534 unsigned total, isl_int v, int lb, int ub)
2536 int j;
2538 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2539 if (lb) {
2540 isl_int_sub(v, bmap->ineq[i][1 + j],
2541 bmap->div[div][1 + 1 + j]);
2542 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2544 if (ub) {
2545 isl_int_add(v, bmap->ineq[i][1 + j],
2546 bmap->div[div][1 + 1 + j]);
2547 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2550 if (!lb && !ub)
2551 return bmap;
2553 bmap = isl_basic_map_cow(bmap);
2554 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2555 if (lb) {
2556 int k = isl_basic_map_alloc_inequality(bmap);
2557 if (k < 0)
2558 goto error;
2559 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2560 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2561 bmap->div[div][1 + j]);
2562 isl_int_cdiv_q(bmap->ineq[k][j],
2563 bmap->ineq[k][j], bmap->div[div][0]);
2565 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2567 if (ub) {
2568 int k = isl_basic_map_alloc_inequality(bmap);
2569 if (k < 0)
2570 goto error;
2571 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2572 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2573 bmap->div[div][1 + j]);
2574 isl_int_fdiv_q(bmap->ineq[k][j],
2575 bmap->ineq[k][j], bmap->div[div][0]);
2577 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2580 return bmap;
2581 error:
2582 isl_basic_map_free(bmap);
2583 return NULL;
2586 /* This function is called right before "div" is eliminated from "bmap"
2587 * using Fourier-Motzkin.
2588 * Look through the constraints of "bmap" for constraints on the argument
2589 * of the integer division and use them to construct constraints on the
2590 * integer division itself. These constraints can then be combined
2591 * during the Fourier-Motzkin elimination.
2592 * Note that it is only useful to introduce lower bounds on "div"
2593 * if "bmap" already contains upper bounds on "div" as the newly
2594 * introduce lower bounds can then be combined with the pre-existing
2595 * upper bounds. Similarly for upper bounds.
2596 * We therefore first check if "bmap" contains any lower and/or upper bounds
2597 * on "div".
2599 * It is interesting to note that the introduction of these constraints
2600 * can indeed lead to more accurate results, even when compared to
2601 * deriving constraints on the argument of "div" from constraints on "div".
2602 * Consider, for example, the set
2604 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2606 * The second constraint can be rewritten as
2608 * 2 * [(-i-2j+3)/4] + k >= 0
2610 * from which we can derive
2612 * -i - 2j + 3 >= -2k
2614 * or
2616 * i + 2j <= 3 + 2k
2618 * Combined with the first constraint, we obtain
2620 * -3 <= 3 + 2k or k >= -3
2622 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2623 * the first constraint, we obtain
2625 * [(i + 2j)/4] >= [-3/4] = -1
2627 * Combining this constraint with the second constraint, we obtain
2629 * k >= -2
2631 static __isl_give isl_basic_map *insert_bounds_on_div(
2632 __isl_take isl_basic_map *bmap, int div)
2634 int i;
2635 int check_lb, check_ub;
2636 isl_int v;
2637 unsigned total;
2639 if (!bmap)
2640 return NULL;
2642 if (isl_int_is_zero(bmap->div[div][0]))
2643 return bmap;
2645 total = isl_space_dim(bmap->dim, isl_dim_all);
2647 check_lb = 0;
2648 check_ub = 0;
2649 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2650 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2651 if (s > 0)
2652 check_ub = 1;
2653 if (s < 0)
2654 check_lb = 1;
2657 if (!check_lb && !check_ub)
2658 return bmap;
2660 isl_int_init(v);
2662 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2663 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2664 continue;
2666 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2667 check_lb, check_ub);
2670 isl_int_clear(v);
2672 return bmap;
2675 /* Remove all divs (recursively) involving any of the given dimensions
2676 * in their definitions.
2678 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2679 __isl_take isl_basic_map *bmap,
2680 enum isl_dim_type type, unsigned first, unsigned n)
2682 int i;
2684 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2685 return isl_basic_map_free(bmap);
2686 first += isl_basic_map_offset(bmap, type);
2688 for (i = bmap->n_div - 1; i >= 0; --i) {
2689 isl_bool involves;
2691 involves = div_involves_vars(bmap, i, first, n);
2692 if (involves < 0)
2693 return isl_basic_map_free(bmap);
2694 if (!involves)
2695 continue;
2696 bmap = insert_bounds_on_div(bmap, i);
2697 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2698 if (!bmap)
2699 return NULL;
2700 i = bmap->n_div;
2703 return bmap;
2706 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2707 __isl_take isl_basic_set *bset,
2708 enum isl_dim_type type, unsigned first, unsigned n)
2710 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2713 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2714 enum isl_dim_type type, unsigned first, unsigned n)
2716 int i;
2718 if (!map)
2719 return NULL;
2720 if (map->n == 0)
2721 return map;
2723 map = isl_map_cow(map);
2724 if (!map)
2725 return NULL;
2727 for (i = 0; i < map->n; ++i) {
2728 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2729 type, first, n);
2730 if (!map->p[i])
2731 goto error;
2733 return map;
2734 error:
2735 isl_map_free(map);
2736 return NULL;
2739 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2740 enum isl_dim_type type, unsigned first, unsigned n)
2742 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2743 type, first, n));
2746 /* Does the description of "bmap" depend on the specified dimensions?
2747 * We also check whether the dimensions appear in any of the div definitions.
2748 * In principle there is no need for this check. If the dimensions appear
2749 * in a div definition, they also appear in the defining constraints of that
2750 * div.
2752 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2753 enum isl_dim_type type, unsigned first, unsigned n)
2755 int i;
2757 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2758 return isl_bool_error;
2760 first += isl_basic_map_offset(bmap, type);
2761 for (i = 0; i < bmap->n_eq; ++i)
2762 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2763 return isl_bool_true;
2764 for (i = 0; i < bmap->n_ineq; ++i)
2765 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2766 return isl_bool_true;
2767 for (i = 0; i < bmap->n_div; ++i) {
2768 if (isl_int_is_zero(bmap->div[i][0]))
2769 continue;
2770 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2771 return isl_bool_true;
2774 return isl_bool_false;
2777 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2778 enum isl_dim_type type, unsigned first, unsigned n)
2780 int i;
2782 if (isl_map_check_range(map, type, first, n) < 0)
2783 return isl_bool_error;
2785 for (i = 0; i < map->n; ++i) {
2786 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2787 type, first, n);
2788 if (involves < 0 || involves)
2789 return involves;
2792 return isl_bool_false;
2795 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2796 enum isl_dim_type type, unsigned first, unsigned n)
2798 return isl_basic_map_involves_dims(bset, type, first, n);
2801 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2802 enum isl_dim_type type, unsigned first, unsigned n)
2804 return isl_map_involves_dims(set, type, first, n);
2807 /* Drop all constraints in bmap that involve any of the dimensions
2808 * first to first+n-1.
2809 * This function only performs the actual removal of constraints.
2811 * This function should not call finalize since it is used by
2812 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2814 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2815 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2817 int i;
2819 if (n == 0)
2820 return bmap;
2822 bmap = isl_basic_map_cow(bmap);
2824 if (!bmap)
2825 return NULL;
2827 for (i = bmap->n_eq - 1; i >= 0; --i) {
2828 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2829 continue;
2830 isl_basic_map_drop_equality(bmap, i);
2833 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2834 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2835 continue;
2836 isl_basic_map_drop_inequality(bmap, i);
2839 return bmap;
2842 /* Drop all constraints in bset that involve any of the dimensions
2843 * first to first+n-1.
2844 * This function only performs the actual removal of constraints.
2846 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2847 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2849 return isl_basic_map_drop_constraints_involving(bset, first, n);
2852 /* Drop all constraints in bmap that do not involve any of the dimensions
2853 * first to first + n - 1 of the given type.
2855 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2856 __isl_take isl_basic_map *bmap,
2857 enum isl_dim_type type, unsigned first, unsigned n)
2859 int i;
2861 if (n == 0) {
2862 isl_space *space = isl_basic_map_get_space(bmap);
2863 isl_basic_map_free(bmap);
2864 return isl_basic_map_universe(space);
2866 bmap = isl_basic_map_cow(bmap);
2867 if (!bmap)
2868 return NULL;
2870 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2871 return isl_basic_map_free(bmap);
2873 first += isl_basic_map_offset(bmap, type) - 1;
2875 for (i = bmap->n_eq - 1; i >= 0; --i) {
2876 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2877 continue;
2878 isl_basic_map_drop_equality(bmap, i);
2881 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2882 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2883 continue;
2884 isl_basic_map_drop_inequality(bmap, i);
2887 bmap = isl_basic_map_add_known_div_constraints(bmap);
2888 return bmap;
2891 /* Drop all constraints in bset that do not involve any of the dimensions
2892 * first to first + n - 1 of the given type.
2894 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2895 __isl_take isl_basic_set *bset,
2896 enum isl_dim_type type, unsigned first, unsigned n)
2898 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2899 type, first, n);
2902 /* Drop all constraints in bmap that involve any of the dimensions
2903 * first to first + n - 1 of the given type.
2905 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2906 __isl_take isl_basic_map *bmap,
2907 enum isl_dim_type type, unsigned first, unsigned n)
2909 if (!bmap)
2910 return NULL;
2911 if (n == 0)
2912 return bmap;
2914 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2915 return isl_basic_map_free(bmap);
2917 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2918 first += isl_basic_map_offset(bmap, type) - 1;
2919 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2920 bmap = isl_basic_map_add_known_div_constraints(bmap);
2921 return bmap;
2924 /* Drop all constraints in bset that involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2928 __isl_take isl_basic_set *bset,
2929 enum isl_dim_type type, unsigned first, unsigned n)
2931 return isl_basic_map_drop_constraints_involving_dims(bset,
2932 type, first, n);
2935 /* Drop constraints from "map" by applying "drop" to each basic map.
2937 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2938 enum isl_dim_type type, unsigned first, unsigned n,
2939 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2940 enum isl_dim_type type, unsigned first, unsigned n))
2942 int i;
2944 if (isl_map_check_range(map, type, first, n) < 0)
2945 return isl_map_free(map);
2947 map = isl_map_cow(map);
2948 if (!map)
2949 return NULL;
2951 for (i = 0; i < map->n; ++i) {
2952 map->p[i] = drop(map->p[i], type, first, n);
2953 if (!map->p[i])
2954 return isl_map_free(map);
2957 if (map->n > 1)
2958 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2960 return map;
2963 /* Drop all constraints in map that involve any of the dimensions
2964 * first to first + n - 1 of the given type.
2966 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2967 __isl_take isl_map *map,
2968 enum isl_dim_type type, unsigned first, unsigned n)
2970 if (n == 0)
2971 return map;
2972 return drop_constraints(map, type, first, n,
2973 &isl_basic_map_drop_constraints_involving_dims);
2976 /* Drop all constraints in "map" that do not involve any of the dimensions
2977 * first to first + n - 1 of the given type.
2979 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2980 __isl_take isl_map *map,
2981 enum isl_dim_type type, unsigned first, unsigned n)
2983 if (n == 0) {
2984 isl_space *space = isl_map_get_space(map);
2985 isl_map_free(map);
2986 return isl_map_universe(space);
2988 return drop_constraints(map, type, first, n,
2989 &isl_basic_map_drop_constraints_not_involving_dims);
2992 /* Drop all constraints in set that involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2996 __isl_take isl_set *set,
2997 enum isl_dim_type type, unsigned first, unsigned n)
2999 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3002 /* Drop all constraints in "set" that do not involve any of the dimensions
3003 * first to first + n - 1 of the given type.
3005 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3006 __isl_take isl_set *set,
3007 enum isl_dim_type type, unsigned first, unsigned n)
3009 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3012 /* Does local variable "div" of "bmap" have a complete explicit representation?
3013 * Having a complete explicit representation requires not only
3014 * an explicit representation, but also that all local variables
3015 * that appear in this explicit representation in turn have
3016 * a complete explicit representation.
3018 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3020 int i;
3021 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3022 isl_bool marked;
3024 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3025 if (marked < 0 || marked)
3026 return isl_bool_not(marked);
3028 for (i = bmap->n_div - 1; i >= 0; --i) {
3029 isl_bool known;
3031 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3032 continue;
3033 known = isl_basic_map_div_is_known(bmap, i);
3034 if (known < 0 || !known)
3035 return known;
3038 return isl_bool_true;
3041 /* Remove all divs that are unknown or defined in terms of unknown divs.
3043 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3044 __isl_take isl_basic_map *bmap)
3046 int i;
3048 if (!bmap)
3049 return NULL;
3051 for (i = bmap->n_div - 1; i >= 0; --i) {
3052 if (isl_basic_map_div_is_known(bmap, i))
3053 continue;
3054 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3055 if (!bmap)
3056 return NULL;
3057 i = bmap->n_div;
3060 return bmap;
3063 /* Remove all divs that are unknown or defined in terms of unknown divs.
3065 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3066 __isl_take isl_basic_set *bset)
3068 return isl_basic_map_remove_unknown_divs(bset);
3071 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3073 int i;
3075 if (!map)
3076 return NULL;
3077 if (map->n == 0)
3078 return map;
3080 map = isl_map_cow(map);
3081 if (!map)
3082 return NULL;
3084 for (i = 0; i < map->n; ++i) {
3085 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3086 if (!map->p[i])
3087 goto error;
3089 return map;
3090 error:
3091 isl_map_free(map);
3092 return NULL;
3095 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3097 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3100 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3101 __isl_take isl_basic_set *bset,
3102 enum isl_dim_type type, unsigned first, unsigned n)
3104 isl_basic_map *bmap = bset_to_bmap(bset);
3105 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3106 return bset_from_bmap(bmap);
3109 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3110 enum isl_dim_type type, unsigned first, unsigned n)
3112 int i;
3114 if (n == 0)
3115 return map;
3117 map = isl_map_cow(map);
3118 if (isl_map_check_range(map, type, first, n) < 0)
3119 return isl_map_free(map);
3121 for (i = 0; i < map->n; ++i) {
3122 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3123 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3124 if (!map->p[i])
3125 goto error;
3127 map = isl_map_drop(map, type, first, n);
3128 return map;
3129 error:
3130 isl_map_free(map);
3131 return NULL;
3134 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3138 type, first, n));
3141 /* Project out n inputs starting at first using Fourier-Motzkin */
3142 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3143 unsigned first, unsigned n)
3145 return isl_map_remove_dims(map, isl_dim_in, first, n);
3148 static void dump_term(struct isl_basic_map *bmap,
3149 isl_int c, int pos, FILE *out)
3151 const char *name;
3152 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3153 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3154 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3155 if (!pos)
3156 isl_int_print(out, c, 0);
3157 else {
3158 if (!isl_int_is_one(c))
3159 isl_int_print(out, c, 0);
3160 if (pos < 1 + nparam) {
3161 name = isl_space_get_dim_name(bmap->dim,
3162 isl_dim_param, pos - 1);
3163 if (name)
3164 fprintf(out, "%s", name);
3165 else
3166 fprintf(out, "p%d", pos - 1);
3167 } else if (pos < 1 + nparam + in)
3168 fprintf(out, "i%d", pos - 1 - nparam);
3169 else if (pos < 1 + nparam + dim)
3170 fprintf(out, "o%d", pos - 1 - nparam - in);
3171 else
3172 fprintf(out, "e%d", pos - 1 - nparam - dim);
3176 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3177 int sign, FILE *out)
3179 int i;
3180 int first;
3181 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3182 isl_int v;
3184 isl_int_init(v);
3185 for (i = 0, first = 1; i < len; ++i) {
3186 if (isl_int_sgn(c[i]) * sign <= 0)
3187 continue;
3188 if (!first)
3189 fprintf(out, " + ");
3190 first = 0;
3191 isl_int_abs(v, c[i]);
3192 dump_term(bmap, v, i, out);
3194 isl_int_clear(v);
3195 if (first)
3196 fprintf(out, "0");
3199 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3200 const char *op, FILE *out, int indent)
3202 int i;
3204 fprintf(out, "%*s", indent, "");
3206 dump_constraint_sign(bmap, c, 1, out);
3207 fprintf(out, " %s ", op);
3208 dump_constraint_sign(bmap, c, -1, out);
3210 fprintf(out, "\n");
3212 for (i = bmap->n_div; i < bmap->extra; ++i) {
3213 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3214 continue;
3215 fprintf(out, "%*s", indent, "");
3216 fprintf(out, "ERROR: unused div coefficient not zero\n");
3217 abort();
3221 static void dump_constraints(struct isl_basic_map *bmap,
3222 isl_int **c, unsigned n,
3223 const char *op, FILE *out, int indent)
3225 int i;
3227 for (i = 0; i < n; ++i)
3228 dump_constraint(bmap, c[i], op, out, indent);
3231 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3233 int j;
3234 int first = 1;
3235 unsigned total = isl_basic_map_total_dim(bmap);
3237 for (j = 0; j < 1 + total; ++j) {
3238 if (isl_int_is_zero(exp[j]))
3239 continue;
3240 if (!first && isl_int_is_pos(exp[j]))
3241 fprintf(out, "+");
3242 dump_term(bmap, exp[j], j, out);
3243 first = 0;
3247 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3249 int i;
3251 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3252 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3254 for (i = 0; i < bmap->n_div; ++i) {
3255 fprintf(out, "%*s", indent, "");
3256 fprintf(out, "e%d = [(", i);
3257 dump_affine(bmap, bmap->div[i]+1, out);
3258 fprintf(out, ")/");
3259 isl_int_print(out, bmap->div[i][0], 0);
3260 fprintf(out, "]\n");
3264 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3265 FILE *out, int indent)
3267 if (!bset) {
3268 fprintf(out, "null basic set\n");
3269 return;
3272 fprintf(out, "%*s", indent, "");
3273 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3274 bset->ref, bset->dim->nparam, bset->dim->n_out,
3275 bset->extra, bset->flags);
3276 dump(bset_to_bmap(bset), out, indent);
3279 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3280 FILE *out, int indent)
3282 if (!bmap) {
3283 fprintf(out, "null basic map\n");
3284 return;
3287 fprintf(out, "%*s", indent, "");
3288 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3289 "flags: %x, n_name: %d\n",
3290 bmap->ref,
3291 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3292 bmap->extra, bmap->flags, bmap->dim->n_id);
3293 dump(bmap, out, indent);
3296 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3298 unsigned total;
3299 if (!bmap)
3300 return -1;
3301 total = isl_basic_map_total_dim(bmap);
3302 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3303 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3304 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3305 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3306 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3307 return 0;
3310 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3311 unsigned flags)
3313 if (isl_space_check_is_set(space) < 0)
3314 goto error;
3315 return isl_map_alloc_space(space, n, flags);
3316 error:
3317 isl_space_free(space);
3318 return NULL;
3321 /* Make sure "map" has room for at least "n" more basic maps.
3323 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3325 int i;
3326 struct isl_map *grown = NULL;
3328 if (!map)
3329 return NULL;
3330 isl_assert(map->ctx, n >= 0, goto error);
3331 if (map->n + n <= map->size)
3332 return map;
3333 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3334 if (!grown)
3335 goto error;
3336 for (i = 0; i < map->n; ++i) {
3337 grown->p[i] = isl_basic_map_copy(map->p[i]);
3338 if (!grown->p[i])
3339 goto error;
3340 grown->n++;
3342 isl_map_free(map);
3343 return grown;
3344 error:
3345 isl_map_free(grown);
3346 isl_map_free(map);
3347 return NULL;
3350 /* Make sure "set" has room for at least "n" more basic sets.
3352 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3354 return set_from_map(isl_map_grow(set_to_map(set), n));
3357 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3359 return isl_map_from_basic_map(bset);
3362 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3364 struct isl_map *map;
3366 if (!bmap)
3367 return NULL;
3369 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3370 return isl_map_add_basic_map(map, bmap);
3373 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3374 __isl_take isl_basic_set *bset)
3376 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3377 bset_to_bmap(bset)));
3380 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3382 return isl_map_free(set);
3385 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3387 int i;
3389 if (!set) {
3390 fprintf(out, "null set\n");
3391 return;
3394 fprintf(out, "%*s", indent, "");
3395 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3396 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3397 set->flags);
3398 for (i = 0; i < set->n; ++i) {
3399 fprintf(out, "%*s", indent, "");
3400 fprintf(out, "basic set %d:\n", i);
3401 isl_basic_set_print_internal(set->p[i], out, indent+4);
3405 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3407 int i;
3409 if (!map) {
3410 fprintf(out, "null map\n");
3411 return;
3414 fprintf(out, "%*s", indent, "");
3415 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3416 "flags: %x, n_name: %d\n",
3417 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3418 map->dim->n_out, map->flags, map->dim->n_id);
3419 for (i = 0; i < map->n; ++i) {
3420 fprintf(out, "%*s", indent, "");
3421 fprintf(out, "basic map %d:\n", i);
3422 isl_basic_map_print_internal(map->p[i], out, indent+4);
3426 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3427 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3429 struct isl_basic_map *bmap_domain;
3431 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3432 goto error;
3434 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3435 isl_assert(bset->ctx,
3436 isl_basic_map_compatible_domain(bmap, bset), goto error);
3438 bmap = isl_basic_map_cow(bmap);
3439 if (!bmap)
3440 goto error;
3441 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3442 bset->n_div, bset->n_eq, bset->n_ineq);
3443 bmap_domain = isl_basic_map_from_domain(bset);
3444 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3446 bmap = isl_basic_map_simplify(bmap);
3447 return isl_basic_map_finalize(bmap);
3448 error:
3449 isl_basic_map_free(bmap);
3450 isl_basic_set_free(bset);
3451 return NULL;
3454 /* Check that the space of "bset" is the same as that of the range of "bmap".
3456 static isl_stat isl_basic_map_check_compatible_range(
3457 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3459 isl_bool ok;
3461 ok = isl_basic_map_compatible_range(bmap, bset);
3462 if (ok < 0)
3463 return isl_stat_error;
3464 if (!ok)
3465 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3466 "incompatible spaces", return isl_stat_error);
3468 return isl_stat_ok;
3471 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3472 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3474 struct isl_basic_map *bmap_range;
3476 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3477 goto error;
3479 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3480 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3481 goto error;
3483 if (isl_basic_set_plain_is_universe(bset)) {
3484 isl_basic_set_free(bset);
3485 return bmap;
3488 bmap = isl_basic_map_cow(bmap);
3489 if (!bmap)
3490 goto error;
3491 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3492 bset->n_div, bset->n_eq, bset->n_ineq);
3493 bmap_range = bset_to_bmap(bset);
3494 bmap = add_constraints(bmap, bmap_range, 0, 0);
3496 bmap = isl_basic_map_simplify(bmap);
3497 return isl_basic_map_finalize(bmap);
3498 error:
3499 isl_basic_map_free(bmap);
3500 isl_basic_set_free(bset);
3501 return NULL;
3504 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3505 __isl_keep isl_vec *vec)
3507 int i;
3508 unsigned total;
3509 isl_int s;
3511 if (!bmap || !vec)
3512 return isl_bool_error;
3514 total = 1 + isl_basic_map_total_dim(bmap);
3515 if (total != vec->size)
3516 return isl_bool_false;
3518 isl_int_init(s);
3520 for (i = 0; i < bmap->n_eq; ++i) {
3521 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3522 if (!isl_int_is_zero(s)) {
3523 isl_int_clear(s);
3524 return isl_bool_false;
3528 for (i = 0; i < bmap->n_ineq; ++i) {
3529 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3530 if (isl_int_is_neg(s)) {
3531 isl_int_clear(s);
3532 return isl_bool_false;
3536 isl_int_clear(s);
3538 return isl_bool_true;
3541 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3542 __isl_keep isl_vec *vec)
3544 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3547 __isl_give isl_basic_map *isl_basic_map_intersect(
3548 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3550 struct isl_vec *sample = NULL;
3552 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3553 goto error;
3554 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3555 isl_space_dim(bmap1->dim, isl_dim_param) &&
3556 isl_space_dim(bmap2->dim, isl_dim_all) !=
3557 isl_space_dim(bmap2->dim, isl_dim_param))
3558 return isl_basic_map_intersect(bmap2, bmap1);
3560 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3561 isl_space_dim(bmap2->dim, isl_dim_param))
3562 isl_assert(bmap1->ctx,
3563 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3565 if (isl_basic_map_plain_is_empty(bmap1)) {
3566 isl_basic_map_free(bmap2);
3567 return bmap1;
3569 if (isl_basic_map_plain_is_empty(bmap2)) {
3570 isl_basic_map_free(bmap1);
3571 return bmap2;
3574 if (bmap1->sample &&
3575 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3576 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3577 sample = isl_vec_copy(bmap1->sample);
3578 else if (bmap2->sample &&
3579 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3580 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3581 sample = isl_vec_copy(bmap2->sample);
3583 bmap1 = isl_basic_map_cow(bmap1);
3584 if (!bmap1)
3585 goto error;
3586 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3587 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3588 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3590 if (!bmap1)
3591 isl_vec_free(sample);
3592 else if (sample) {
3593 isl_vec_free(bmap1->sample);
3594 bmap1->sample = sample;
3597 bmap1 = isl_basic_map_simplify(bmap1);
3598 return isl_basic_map_finalize(bmap1);
3599 error:
3600 if (sample)
3601 isl_vec_free(sample);
3602 isl_basic_map_free(bmap1);
3603 isl_basic_map_free(bmap2);
3604 return NULL;
3607 struct isl_basic_set *isl_basic_set_intersect(
3608 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3610 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3611 bset_to_bmap(bset2)));
3614 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3615 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3617 return isl_basic_set_intersect(bset1, bset2);
3620 /* Special case of isl_map_intersect, where both map1 and map2
3621 * are convex, without any divs and such that either map1 or map2
3622 * contains a single constraint. This constraint is then simply
3623 * added to the other map.
3625 static __isl_give isl_map *map_intersect_add_constraint(
3626 __isl_take isl_map *map1, __isl_take isl_map *map2)
3628 isl_assert(map1->ctx, map1->n == 1, goto error);
3629 isl_assert(map2->ctx, map1->n == 1, goto error);
3630 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3631 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3633 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3634 return isl_map_intersect(map2, map1);
3636 map1 = isl_map_cow(map1);
3637 if (!map1)
3638 goto error;
3639 if (isl_map_plain_is_empty(map1)) {
3640 isl_map_free(map2);
3641 return map1;
3643 if (map2->p[0]->n_eq == 1)
3644 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3645 else
3646 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3647 map2->p[0]->ineq[0]);
3649 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3650 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3651 if (!map1->p[0])
3652 goto error;
3654 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3655 isl_basic_map_free(map1->p[0]);
3656 map1->n = 0;
3659 isl_map_free(map2);
3661 map1 = isl_map_unmark_normalized(map1);
3662 return map1;
3663 error:
3664 isl_map_free(map1);
3665 isl_map_free(map2);
3666 return NULL;
3669 /* map2 may be either a parameter domain or a map living in the same
3670 * space as map1.
3672 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3673 __isl_take isl_map *map2)
3675 unsigned flags = 0;
3676 isl_bool equal;
3677 isl_map *result;
3678 int i, j;
3680 if (!map1 || !map2)
3681 goto error;
3683 if ((isl_map_plain_is_empty(map1) ||
3684 isl_map_plain_is_universe(map2)) &&
3685 isl_space_is_equal(map1->dim, map2->dim)) {
3686 isl_map_free(map2);
3687 return map1;
3689 if ((isl_map_plain_is_empty(map2) ||
3690 isl_map_plain_is_universe(map1)) &&
3691 isl_space_is_equal(map1->dim, map2->dim)) {
3692 isl_map_free(map1);
3693 return map2;
3696 if (map1->n == 1 && map2->n == 1 &&
3697 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3698 isl_space_is_equal(map1->dim, map2->dim) &&
3699 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3700 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3701 return map_intersect_add_constraint(map1, map2);
3703 equal = isl_map_plain_is_equal(map1, map2);
3704 if (equal < 0)
3705 goto error;
3706 if (equal) {
3707 isl_map_free(map2);
3708 return map1;
3711 if (isl_space_dim(map2->dim, isl_dim_all) !=
3712 isl_space_dim(map2->dim, isl_dim_param))
3713 isl_assert(map1->ctx,
3714 isl_space_is_equal(map1->dim, map2->dim), goto error);
3716 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3717 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3718 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3720 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3721 map1->n * map2->n, flags);
3722 if (!result)
3723 goto error;
3724 for (i = 0; i < map1->n; ++i)
3725 for (j = 0; j < map2->n; ++j) {
3726 struct isl_basic_map *part;
3727 part = isl_basic_map_intersect(
3728 isl_basic_map_copy(map1->p[i]),
3729 isl_basic_map_copy(map2->p[j]));
3730 if (isl_basic_map_is_empty(part) < 0)
3731 part = isl_basic_map_free(part);
3732 result = isl_map_add_basic_map(result, part);
3733 if (!result)
3734 goto error;
3736 isl_map_free(map1);
3737 isl_map_free(map2);
3738 return result;
3739 error:
3740 isl_map_free(map1);
3741 isl_map_free(map2);
3742 return NULL;
3745 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3746 __isl_take isl_map *map2)
3748 if (!map1 || !map2)
3749 goto error;
3750 if (!isl_space_is_equal(map1->dim, map2->dim))
3751 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3752 "spaces don't match", goto error);
3753 return map_intersect_internal(map1, map2);
3754 error:
3755 isl_map_free(map1);
3756 isl_map_free(map2);
3757 return NULL;
3760 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3761 __isl_take isl_map *map2)
3763 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3766 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3768 return set_from_map(isl_map_intersect(set_to_map(set1),
3769 set_to_map(set2)));
3772 /* map_intersect_internal accepts intersections
3773 * with parameter domains, so we can just call that function.
3775 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3776 __isl_take isl_set *params)
3778 return map_intersect_internal(map, params);
3781 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3782 __isl_take isl_map *map2)
3784 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3787 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3788 __isl_take isl_set *params)
3790 return isl_map_intersect_params(set, params);
3793 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3795 isl_space *space;
3796 unsigned pos, n1, n2;
3798 if (!bmap)
3799 return NULL;
3800 bmap = isl_basic_map_cow(bmap);
3801 if (!bmap)
3802 return NULL;
3803 space = isl_space_reverse(isl_space_copy(bmap->dim));
3804 pos = isl_basic_map_offset(bmap, isl_dim_in);
3805 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3806 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3807 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3808 return isl_basic_map_reset_space(bmap, space);
3811 static __isl_give isl_basic_map *basic_map_space_reset(
3812 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3814 isl_space *space;
3816 if (!bmap)
3817 return NULL;
3818 if (!isl_space_is_named_or_nested(bmap->dim, type))
3819 return bmap;
3821 space = isl_basic_map_get_space(bmap);
3822 space = isl_space_reset(space, type);
3823 bmap = isl_basic_map_reset_space(bmap, space);
3824 return bmap;
3827 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3828 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3829 unsigned pos, unsigned n)
3831 isl_bool rational;
3832 isl_space *res_space;
3833 struct isl_basic_map *res;
3834 struct isl_dim_map *dim_map;
3835 unsigned total, off;
3836 enum isl_dim_type t;
3838 if (n == 0)
3839 return basic_map_space_reset(bmap, type);
3841 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3842 type, pos, n);
3843 if (!res_space)
3844 return isl_basic_map_free(bmap);
3846 total = isl_basic_map_total_dim(bmap) + n;
3847 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3848 off = 0;
3849 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3850 if (t != type) {
3851 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3852 } else {
3853 unsigned size = isl_basic_map_dim(bmap, t);
3854 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3855 0, pos, off);
3856 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3857 pos, size - pos, off + pos + n);
3859 off += isl_space_dim(res_space, t);
3861 isl_dim_map_div(dim_map, bmap, off);
3863 res = isl_basic_map_alloc_space(res_space,
3864 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3865 rational = isl_basic_map_is_rational(bmap);
3866 if (rational < 0)
3867 res = isl_basic_map_free(res);
3868 if (rational)
3869 res = isl_basic_map_set_rational(res);
3870 if (isl_basic_map_plain_is_empty(bmap)) {
3871 isl_basic_map_free(bmap);
3872 free(dim_map);
3873 return isl_basic_map_set_to_empty(res);
3875 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3876 return isl_basic_map_finalize(res);
3879 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3880 __isl_take isl_basic_set *bset,
3881 enum isl_dim_type type, unsigned pos, unsigned n)
3883 return isl_basic_map_insert_dims(bset, type, pos, n);
3886 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3887 enum isl_dim_type type, unsigned n)
3889 if (!bmap)
3890 return NULL;
3891 return isl_basic_map_insert_dims(bmap, type,
3892 isl_basic_map_dim(bmap, type), n);
3895 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3896 enum isl_dim_type type, unsigned n)
3898 if (!bset)
3899 return NULL;
3900 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3901 return isl_basic_map_add_dims(bset, type, n);
3902 error:
3903 isl_basic_set_free(bset);
3904 return NULL;
3907 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3908 enum isl_dim_type type)
3910 isl_space *space;
3912 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3913 return map;
3915 space = isl_map_get_space(map);
3916 space = isl_space_reset(space, type);
3917 map = isl_map_reset_space(map, space);
3918 return map;
3921 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3922 enum isl_dim_type type, unsigned pos, unsigned n)
3924 int i;
3926 if (n == 0)
3927 return map_space_reset(map, type);
3929 map = isl_map_cow(map);
3930 if (!map)
3931 return NULL;
3933 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3934 if (!map->dim)
3935 goto error;
3937 for (i = 0; i < map->n; ++i) {
3938 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3939 if (!map->p[i])
3940 goto error;
3943 return map;
3944 error:
3945 isl_map_free(map);
3946 return NULL;
3949 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3950 enum isl_dim_type type, unsigned pos, unsigned n)
3952 return isl_map_insert_dims(set, type, pos, n);
3955 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3956 enum isl_dim_type type, unsigned n)
3958 if (!map)
3959 return NULL;
3960 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3963 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3964 enum isl_dim_type type, unsigned n)
3966 if (!set)
3967 return NULL;
3968 isl_assert(set->ctx, type != isl_dim_in, goto error);
3969 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3970 error:
3971 isl_set_free(set);
3972 return NULL;
3975 __isl_give isl_basic_map *isl_basic_map_move_dims(
3976 __isl_take isl_basic_map *bmap,
3977 enum isl_dim_type dst_type, unsigned dst_pos,
3978 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3980 struct isl_dim_map *dim_map;
3981 struct isl_basic_map *res;
3982 enum isl_dim_type t;
3983 unsigned total, off;
3985 if (!bmap)
3986 return NULL;
3987 if (n == 0) {
3988 bmap = isl_basic_map_reset(bmap, src_type);
3989 bmap = isl_basic_map_reset(bmap, dst_type);
3990 return bmap;
3993 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3994 return isl_basic_map_free(bmap);
3996 if (dst_type == src_type && dst_pos == src_pos)
3997 return bmap;
3999 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4001 if (pos(bmap->dim, dst_type) + dst_pos ==
4002 pos(bmap->dim, src_type) + src_pos +
4003 ((src_type < dst_type) ? n : 0)) {
4004 bmap = isl_basic_map_cow(bmap);
4005 if (!bmap)
4006 return NULL;
4008 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4009 src_type, src_pos, n);
4010 if (!bmap->dim)
4011 goto error;
4013 bmap = isl_basic_map_finalize(bmap);
4015 return bmap;
4018 total = isl_basic_map_total_dim(bmap);
4019 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4021 off = 0;
4022 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4023 unsigned size = isl_space_dim(bmap->dim, t);
4024 if (t == dst_type) {
4025 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4026 0, dst_pos, off);
4027 off += dst_pos;
4028 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4029 src_pos, n, off);
4030 off += n;
4031 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4032 dst_pos, size - dst_pos, off);
4033 off += size - dst_pos;
4034 } else if (t == src_type) {
4035 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4036 0, src_pos, off);
4037 off += src_pos;
4038 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4039 src_pos + n, size - src_pos - n, off);
4040 off += size - src_pos - n;
4041 } else {
4042 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4043 off += size;
4046 isl_dim_map_div(dim_map, bmap, off);
4048 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4049 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4050 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4051 if (!bmap)
4052 goto error;
4054 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4055 src_type, src_pos, n);
4056 if (!bmap->dim)
4057 goto error;
4059 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4060 bmap = isl_basic_map_gauss(bmap, NULL);
4061 bmap = isl_basic_map_finalize(bmap);
4063 return bmap;
4064 error:
4065 isl_basic_map_free(bmap);
4066 return NULL;
4069 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4070 enum isl_dim_type dst_type, unsigned dst_pos,
4071 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4073 isl_basic_map *bmap = bset_to_bmap(bset);
4074 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4075 src_type, src_pos, n);
4076 return bset_from_bmap(bmap);
4079 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4080 enum isl_dim_type dst_type, unsigned dst_pos,
4081 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4083 if (!set)
4084 return NULL;
4085 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4086 return set_from_map(isl_map_move_dims(set_to_map(set),
4087 dst_type, dst_pos, src_type, src_pos, n));
4088 error:
4089 isl_set_free(set);
4090 return NULL;
4093 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4094 enum isl_dim_type dst_type, unsigned dst_pos,
4095 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4097 int i;
4099 if (n == 0) {
4100 map = isl_map_reset(map, src_type);
4101 map = isl_map_reset(map, dst_type);
4102 return map;
4105 if (isl_map_check_range(map, src_type, src_pos, n))
4106 return isl_map_free(map);
4108 if (dst_type == src_type && dst_pos == src_pos)
4109 return map;
4111 isl_assert(map->ctx, dst_type != src_type, goto error);
4113 map = isl_map_cow(map);
4114 if (!map)
4115 return NULL;
4117 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4118 if (!map->dim)
4119 goto error;
4121 for (i = 0; i < map->n; ++i) {
4122 map->p[i] = isl_basic_map_move_dims(map->p[i],
4123 dst_type, dst_pos,
4124 src_type, src_pos, n);
4125 if (!map->p[i])
4126 goto error;
4129 return map;
4130 error:
4131 isl_map_free(map);
4132 return NULL;
4135 /* Move the specified dimensions to the last columns right before
4136 * the divs. Don't change the dimension specification of bmap.
4137 * That's the responsibility of the caller.
4139 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4140 enum isl_dim_type type, unsigned first, unsigned n)
4142 struct isl_dim_map *dim_map;
4143 struct isl_basic_map *res;
4144 enum isl_dim_type t;
4145 unsigned total, off;
4147 if (!bmap)
4148 return NULL;
4149 if (pos(bmap->dim, type) + first + n ==
4150 1 + isl_space_dim(bmap->dim, isl_dim_all))
4151 return bmap;
4153 total = isl_basic_map_total_dim(bmap);
4154 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4156 off = 0;
4157 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4158 unsigned size = isl_space_dim(bmap->dim, t);
4159 if (t == type) {
4160 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4161 0, first, off);
4162 off += first;
4163 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4164 first, n, total - bmap->n_div - n);
4165 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4166 first + n, size - (first + n), off);
4167 off += size - (first + n);
4168 } else {
4169 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4170 off += size;
4173 isl_dim_map_div(dim_map, bmap, off + n);
4175 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4176 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4177 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4178 return res;
4181 /* Insert "n" rows in the divs of "bmap".
4183 * The number of columns is not changed, which means that the last
4184 * dimensions of "bmap" are being reintepreted as the new divs.
4185 * The space of "bmap" is not adjusted, however, which means
4186 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4187 * from the space of "bmap" is the responsibility of the caller.
4189 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4190 int n)
4192 int i;
4193 size_t row_size;
4194 isl_int **new_div;
4195 isl_int *old;
4197 bmap = isl_basic_map_cow(bmap);
4198 if (!bmap)
4199 return NULL;
4201 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4202 old = bmap->block2.data;
4203 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4204 (bmap->extra + n) * (1 + row_size));
4205 if (!bmap->block2.data)
4206 return isl_basic_map_free(bmap);
4207 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4208 if (!new_div)
4209 return isl_basic_map_free(bmap);
4210 for (i = 0; i < n; ++i) {
4211 new_div[i] = bmap->block2.data +
4212 (bmap->extra + i) * (1 + row_size);
4213 isl_seq_clr(new_div[i], 1 + row_size);
4215 for (i = 0; i < bmap->extra; ++i)
4216 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4217 free(bmap->div);
4218 bmap->div = new_div;
4219 bmap->n_div += n;
4220 bmap->extra += n;
4222 return bmap;
4225 /* Drop constraints from "bmap" that only involve the variables
4226 * of "type" in the range [first, first + n] that are not related
4227 * to any of the variables outside that interval.
4228 * These constraints cannot influence the values for the variables
4229 * outside the interval, except in case they cause "bmap" to be empty.
4230 * Only drop the constraints if "bmap" is known to be non-empty.
4232 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4233 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4234 unsigned first, unsigned n)
4236 int i;
4237 int *groups;
4238 unsigned dim, n_div;
4239 isl_bool non_empty;
4241 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4242 if (non_empty < 0)
4243 return isl_basic_map_free(bmap);
4244 if (!non_empty)
4245 return bmap;
4247 dim = isl_basic_map_dim(bmap, isl_dim_all);
4248 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4249 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4250 if (!groups)
4251 return isl_basic_map_free(bmap);
4252 first += isl_basic_map_offset(bmap, type) - 1;
4253 for (i = 0; i < first; ++i)
4254 groups[i] = -1;
4255 for (i = first + n; i < dim - n_div; ++i)
4256 groups[i] = -1;
4258 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4260 return bmap;
4263 /* Turn the n dimensions of type type, starting at first
4264 * into existentially quantified variables.
4266 * If a subset of the projected out variables are unrelated
4267 * to any of the variables that remain, then the constraints
4268 * involving this subset are simply dropped first.
4270 __isl_give isl_basic_map *isl_basic_map_project_out(
4271 __isl_take isl_basic_map *bmap,
4272 enum isl_dim_type type, unsigned first, unsigned n)
4274 isl_bool empty;
4276 if (n == 0)
4277 return basic_map_space_reset(bmap, type);
4278 if (type == isl_dim_div)
4279 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4280 "cannot project out existentially quantified variables",
4281 return isl_basic_map_free(bmap));
4283 empty = isl_basic_map_plain_is_empty(bmap);
4284 if (empty < 0)
4285 return isl_basic_map_free(bmap);
4286 if (empty)
4287 bmap = isl_basic_map_set_to_empty(bmap);
4289 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4290 if (!bmap)
4291 return NULL;
4293 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4294 return isl_basic_map_remove_dims(bmap, type, first, n);
4296 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4297 return isl_basic_map_free(bmap);
4299 bmap = move_last(bmap, type, first, n);
4300 bmap = isl_basic_map_cow(bmap);
4301 bmap = insert_div_rows(bmap, n);
4302 if (!bmap)
4303 return NULL;
4305 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4306 if (!bmap->dim)
4307 goto error;
4308 bmap = isl_basic_map_simplify(bmap);
4309 bmap = isl_basic_map_drop_redundant_divs(bmap);
4310 return isl_basic_map_finalize(bmap);
4311 error:
4312 isl_basic_map_free(bmap);
4313 return NULL;
4316 /* Turn the n dimensions of type type, starting at first
4317 * into existentially quantified variables.
4319 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4320 enum isl_dim_type type, unsigned first, unsigned n)
4322 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4323 type, first, n));
4326 /* Turn the n dimensions of type type, starting at first
4327 * into existentially quantified variables.
4329 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4330 enum isl_dim_type type, unsigned first, unsigned n)
4332 int i;
4334 if (n == 0)
4335 return map_space_reset(map, type);
4337 if (isl_map_check_range(map, type, first, n) < 0)
4338 return isl_map_free(map);
4340 map = isl_map_cow(map);
4341 if (!map)
4342 return NULL;
4344 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4345 if (!map->dim)
4346 goto error;
4348 for (i = 0; i < map->n; ++i) {
4349 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4350 if (!map->p[i])
4351 goto error;
4354 return map;
4355 error:
4356 isl_map_free(map);
4357 return NULL;
4360 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4361 * into existentially quantified variables.
4363 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4364 enum isl_dim_type type, unsigned first, unsigned n)
4366 unsigned dim;
4368 if (isl_map_check_range(map, type, first, n) < 0)
4369 return isl_map_free(map);
4370 dim = isl_map_dim(map, type);
4371 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4372 map = isl_map_project_out(map, type, 0, first);
4373 return map;
4376 /* Turn the n dimensions of type type, starting at first
4377 * into existentially quantified variables.
4379 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4380 enum isl_dim_type type, unsigned first, unsigned n)
4382 return set_from_map(isl_map_project_out(set_to_map(set),
4383 type, first, n));
4386 /* Return a map that projects the elements in "set" onto their
4387 * "n" set dimensions starting at "first".
4388 * "type" should be equal to isl_dim_set.
4390 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4391 enum isl_dim_type type, unsigned first, unsigned n)
4393 int i;
4394 int dim;
4395 isl_map *map;
4397 if (!set)
4398 return NULL;
4399 if (type != isl_dim_set)
4400 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4401 "only set dimensions can be projected out", goto error);
4402 dim = isl_set_dim(set, isl_dim_set);
4403 if (first + n > dim || first + n < first)
4404 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4405 "index out of bounds", goto error);
4407 map = isl_map_from_domain(set);
4408 map = isl_map_add_dims(map, isl_dim_out, n);
4409 for (i = 0; i < n; ++i)
4410 map = isl_map_equate(map, isl_dim_in, first + i,
4411 isl_dim_out, i);
4412 return map;
4413 error:
4414 isl_set_free(set);
4415 return NULL;
4418 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4419 unsigned n)
4421 int i, j;
4423 for (i = 0; i < n; ++i) {
4424 j = isl_basic_map_alloc_div(bmap);
4425 if (j < 0)
4426 goto error;
4427 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4429 return bmap;
4430 error:
4431 isl_basic_map_free(bmap);
4432 return NULL;
4435 struct isl_basic_map *isl_basic_map_apply_range(
4436 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4438 isl_space *space_result = NULL;
4439 struct isl_basic_map *bmap;
4440 unsigned n_in, n_out, n, nparam, total, pos;
4441 struct isl_dim_map *dim_map1, *dim_map2;
4443 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4444 goto error;
4445 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4446 bmap2->dim, isl_dim_in))
4447 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4448 "spaces don't match", goto error);
4450 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4451 isl_space_copy(bmap2->dim));
4453 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4454 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4455 n = isl_basic_map_dim(bmap1, isl_dim_out);
4456 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4458 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4459 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4460 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4461 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4462 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4463 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4464 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4465 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4466 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4467 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4468 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4470 bmap = isl_basic_map_alloc_space(space_result,
4471 bmap1->n_div + bmap2->n_div + n,
4472 bmap1->n_eq + bmap2->n_eq,
4473 bmap1->n_ineq + bmap2->n_ineq);
4474 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4475 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4476 bmap = add_divs(bmap, n);
4477 bmap = isl_basic_map_simplify(bmap);
4478 bmap = isl_basic_map_drop_redundant_divs(bmap);
4479 return isl_basic_map_finalize(bmap);
4480 error:
4481 isl_basic_map_free(bmap1);
4482 isl_basic_map_free(bmap2);
4483 return NULL;
4486 struct isl_basic_set *isl_basic_set_apply(
4487 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4489 if (!bset || !bmap)
4490 goto error;
4492 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4493 goto error);
4495 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4496 bmap));
4497 error:
4498 isl_basic_set_free(bset);
4499 isl_basic_map_free(bmap);
4500 return NULL;
4503 struct isl_basic_map *isl_basic_map_apply_domain(
4504 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4506 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4507 goto error;
4508 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4509 bmap2->dim, isl_dim_in))
4510 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4511 "spaces don't match", goto error);
4513 bmap1 = isl_basic_map_reverse(bmap1);
4514 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4515 return isl_basic_map_reverse(bmap1);
4516 error:
4517 isl_basic_map_free(bmap1);
4518 isl_basic_map_free(bmap2);
4519 return NULL;
4522 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4523 * A \cap B -> f(A) + f(B)
4525 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4526 __isl_take isl_basic_map *bmap2)
4528 unsigned n_in, n_out, nparam, total, pos;
4529 struct isl_basic_map *bmap = NULL;
4530 struct isl_dim_map *dim_map1, *dim_map2;
4531 int i;
4533 if (!bmap1 || !bmap2)
4534 goto error;
4536 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4537 goto error);
4539 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4540 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4541 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4543 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4544 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4545 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4546 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4547 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4548 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4549 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4550 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4551 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4552 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4553 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4555 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4556 bmap1->n_div + bmap2->n_div + 2 * n_out,
4557 bmap1->n_eq + bmap2->n_eq + n_out,
4558 bmap1->n_ineq + bmap2->n_ineq);
4559 for (i = 0; i < n_out; ++i) {
4560 int j = isl_basic_map_alloc_equality(bmap);
4561 if (j < 0)
4562 goto error;
4563 isl_seq_clr(bmap->eq[j], 1+total);
4564 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4565 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4566 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4568 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4569 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4570 bmap = add_divs(bmap, 2 * n_out);
4572 bmap = isl_basic_map_simplify(bmap);
4573 return isl_basic_map_finalize(bmap);
4574 error:
4575 isl_basic_map_free(bmap);
4576 isl_basic_map_free(bmap1);
4577 isl_basic_map_free(bmap2);
4578 return NULL;
4581 /* Given two maps A -> f(A) and B -> g(B), construct a map
4582 * A \cap B -> f(A) + f(B)
4584 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4585 __isl_take isl_map *map2)
4587 struct isl_map *result;
4588 int i, j;
4590 if (!map1 || !map2)
4591 goto error;
4593 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4595 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4596 map1->n * map2->n, 0);
4597 if (!result)
4598 goto error;
4599 for (i = 0; i < map1->n; ++i)
4600 for (j = 0; j < map2->n; ++j) {
4601 struct isl_basic_map *part;
4602 part = isl_basic_map_sum(
4603 isl_basic_map_copy(map1->p[i]),
4604 isl_basic_map_copy(map2->p[j]));
4605 if (isl_basic_map_is_empty(part))
4606 isl_basic_map_free(part);
4607 else
4608 result = isl_map_add_basic_map(result, part);
4609 if (!result)
4610 goto error;
4612 isl_map_free(map1);
4613 isl_map_free(map2);
4614 return result;
4615 error:
4616 isl_map_free(map1);
4617 isl_map_free(map2);
4618 return NULL;
4621 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4622 __isl_take isl_set *set2)
4624 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4627 /* Given a basic map A -> f(A), construct A -> -f(A).
4629 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4631 int i, j;
4632 unsigned off, n;
4634 bmap = isl_basic_map_cow(bmap);
4635 if (!bmap)
4636 return NULL;
4638 n = isl_basic_map_dim(bmap, isl_dim_out);
4639 off = isl_basic_map_offset(bmap, isl_dim_out);
4640 for (i = 0; i < bmap->n_eq; ++i)
4641 for (j = 0; j < n; ++j)
4642 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4643 for (i = 0; i < bmap->n_ineq; ++i)
4644 for (j = 0; j < n; ++j)
4645 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4646 for (i = 0; i < bmap->n_div; ++i)
4647 for (j = 0; j < n; ++j)
4648 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4649 bmap = isl_basic_map_gauss(bmap, NULL);
4650 return isl_basic_map_finalize(bmap);
4653 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4655 return isl_basic_map_neg(bset);
4658 /* Given a map A -> f(A), construct A -> -f(A).
4660 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4662 int i;
4664 map = isl_map_cow(map);
4665 if (!map)
4666 return NULL;
4668 for (i = 0; i < map->n; ++i) {
4669 map->p[i] = isl_basic_map_neg(map->p[i]);
4670 if (!map->p[i])
4671 goto error;
4674 return map;
4675 error:
4676 isl_map_free(map);
4677 return NULL;
4680 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4682 return set_from_map(isl_map_neg(set_to_map(set)));
4685 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4686 * A -> floor(f(A)/d).
4688 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4689 isl_int d)
4691 unsigned n_in, n_out, nparam, total, pos;
4692 struct isl_basic_map *result = NULL;
4693 struct isl_dim_map *dim_map;
4694 int i;
4696 if (!bmap)
4697 return NULL;
4699 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4700 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4701 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4703 total = nparam + n_in + n_out + bmap->n_div + n_out;
4704 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4705 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4706 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4707 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4708 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4710 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4711 bmap->n_div + n_out,
4712 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4713 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4714 result = add_divs(result, n_out);
4715 for (i = 0; i < n_out; ++i) {
4716 int j;
4717 j = isl_basic_map_alloc_inequality(result);
4718 if (j < 0)
4719 goto error;
4720 isl_seq_clr(result->ineq[j], 1+total);
4721 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4722 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4723 j = isl_basic_map_alloc_inequality(result);
4724 if (j < 0)
4725 goto error;
4726 isl_seq_clr(result->ineq[j], 1+total);
4727 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4728 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4729 isl_int_sub_ui(result->ineq[j][0], d, 1);
4732 result = isl_basic_map_simplify(result);
4733 return isl_basic_map_finalize(result);
4734 error:
4735 isl_basic_map_free(result);
4736 return NULL;
4739 /* Given a map A -> f(A) and an integer d, construct a map
4740 * A -> floor(f(A)/d).
4742 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4744 int i;
4746 map = isl_map_cow(map);
4747 if (!map)
4748 return NULL;
4750 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4751 for (i = 0; i < map->n; ++i) {
4752 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4753 if (!map->p[i])
4754 goto error;
4756 map = isl_map_unmark_normalized(map);
4758 return map;
4759 error:
4760 isl_map_free(map);
4761 return NULL;
4764 /* Given a map A -> f(A) and an integer d, construct a map
4765 * A -> floor(f(A)/d).
4767 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4768 __isl_take isl_val *d)
4770 if (!map || !d)
4771 goto error;
4772 if (!isl_val_is_int(d))
4773 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4774 "expecting integer denominator", goto error);
4775 map = isl_map_floordiv(map, d->n);
4776 isl_val_free(d);
4777 return map;
4778 error:
4779 isl_map_free(map);
4780 isl_val_free(d);
4781 return NULL;
4784 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4785 unsigned pos)
4787 int i;
4788 unsigned nparam;
4789 unsigned n_in;
4791 i = isl_basic_map_alloc_equality(bmap);
4792 if (i < 0)
4793 goto error;
4794 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4795 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4796 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4797 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4798 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4799 return isl_basic_map_finalize(bmap);
4800 error:
4801 isl_basic_map_free(bmap);
4802 return NULL;
4805 /* Add a constraint to "bmap" expressing i_pos < o_pos
4807 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4808 unsigned pos)
4810 int i;
4811 unsigned nparam;
4812 unsigned n_in;
4814 i = isl_basic_map_alloc_inequality(bmap);
4815 if (i < 0)
4816 goto error;
4817 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4818 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4819 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4820 isl_int_set_si(bmap->ineq[i][0], -1);
4821 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4822 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4823 return isl_basic_map_finalize(bmap);
4824 error:
4825 isl_basic_map_free(bmap);
4826 return NULL;
4829 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4831 static __isl_give isl_basic_map *var_less_or_equal(
4832 __isl_take isl_basic_map *bmap, unsigned pos)
4834 int i;
4835 unsigned nparam;
4836 unsigned n_in;
4838 i = isl_basic_map_alloc_inequality(bmap);
4839 if (i < 0)
4840 goto error;
4841 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4842 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4843 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4844 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4845 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4846 return isl_basic_map_finalize(bmap);
4847 error:
4848 isl_basic_map_free(bmap);
4849 return NULL;
4852 /* Add a constraint to "bmap" expressing i_pos > o_pos
4854 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4855 unsigned pos)
4857 int i;
4858 unsigned nparam;
4859 unsigned n_in;
4861 i = isl_basic_map_alloc_inequality(bmap);
4862 if (i < 0)
4863 goto error;
4864 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4865 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4866 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4867 isl_int_set_si(bmap->ineq[i][0], -1);
4868 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4869 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4870 return isl_basic_map_finalize(bmap);
4871 error:
4872 isl_basic_map_free(bmap);
4873 return NULL;
4876 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4878 static __isl_give isl_basic_map *var_more_or_equal(
4879 __isl_take isl_basic_map *bmap, unsigned pos)
4881 int i;
4882 unsigned nparam;
4883 unsigned n_in;
4885 i = isl_basic_map_alloc_inequality(bmap);
4886 if (i < 0)
4887 goto error;
4888 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4889 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4890 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4891 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4892 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4893 return isl_basic_map_finalize(bmap);
4894 error:
4895 isl_basic_map_free(bmap);
4896 return NULL;
4899 __isl_give isl_basic_map *isl_basic_map_equal(
4900 __isl_take isl_space *space, unsigned n_equal)
4902 int i;
4903 struct isl_basic_map *bmap;
4904 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4905 if (!bmap)
4906 return NULL;
4907 for (i = 0; i < n_equal && bmap; ++i)
4908 bmap = var_equal(bmap, i);
4909 return isl_basic_map_finalize(bmap);
4912 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4914 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4915 unsigned pos)
4917 int i;
4918 struct isl_basic_map *bmap;
4919 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4920 if (!bmap)
4921 return NULL;
4922 for (i = 0; i < pos && bmap; ++i)
4923 bmap = var_equal(bmap, i);
4924 if (bmap)
4925 bmap = var_less(bmap, pos);
4926 return isl_basic_map_finalize(bmap);
4929 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4931 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4932 __isl_take isl_space *space, unsigned pos)
4934 int i;
4935 isl_basic_map *bmap;
4937 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4938 for (i = 0; i < pos; ++i)
4939 bmap = var_equal(bmap, i);
4940 bmap = var_less_or_equal(bmap, pos);
4941 return isl_basic_map_finalize(bmap);
4944 /* Return a relation on "space" expressing i_pos > o_pos
4946 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4947 unsigned pos)
4949 int i;
4950 struct isl_basic_map *bmap;
4951 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4952 if (!bmap)
4953 return NULL;
4954 for (i = 0; i < pos && bmap; ++i)
4955 bmap = var_equal(bmap, i);
4956 if (bmap)
4957 bmap = var_more(bmap, pos);
4958 return isl_basic_map_finalize(bmap);
4961 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4963 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4964 __isl_take isl_space *space, unsigned pos)
4966 int i;
4967 isl_basic_map *bmap;
4969 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4970 for (i = 0; i < pos; ++i)
4971 bmap = var_equal(bmap, i);
4972 bmap = var_more_or_equal(bmap, pos);
4973 return isl_basic_map_finalize(bmap);
4976 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4977 unsigned n, int equal)
4979 struct isl_map *map;
4980 int i;
4982 if (n == 0 && equal)
4983 return isl_map_universe(space);
4985 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4987 for (i = 0; i + 1 < n; ++i)
4988 map = isl_map_add_basic_map(map,
4989 isl_basic_map_less_at(isl_space_copy(space), i));
4990 if (n > 0) {
4991 if (equal)
4992 map = isl_map_add_basic_map(map,
4993 isl_basic_map_less_or_equal_at(space, n - 1));
4994 else
4995 map = isl_map_add_basic_map(map,
4996 isl_basic_map_less_at(space, n - 1));
4997 } else
4998 isl_space_free(space);
5000 return map;
5003 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5005 if (!space)
5006 return NULL;
5007 return map_lex_lte_first(space, space->n_out, equal);
5010 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5012 return map_lex_lte_first(dim, n, 0);
5015 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5017 return map_lex_lte_first(dim, n, 1);
5020 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5022 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5025 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5027 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5030 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5031 unsigned n, int equal)
5033 struct isl_map *map;
5034 int i;
5036 if (n == 0 && equal)
5037 return isl_map_universe(space);
5039 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5041 for (i = 0; i + 1 < n; ++i)
5042 map = isl_map_add_basic_map(map,
5043 isl_basic_map_more_at(isl_space_copy(space), i));
5044 if (n > 0) {
5045 if (equal)
5046 map = isl_map_add_basic_map(map,
5047 isl_basic_map_more_or_equal_at(space, n - 1));
5048 else
5049 map = isl_map_add_basic_map(map,
5050 isl_basic_map_more_at(space, n - 1));
5051 } else
5052 isl_space_free(space);
5054 return map;
5057 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5059 if (!space)
5060 return NULL;
5061 return map_lex_gte_first(space, space->n_out, equal);
5064 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5066 return map_lex_gte_first(dim, n, 0);
5069 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5071 return map_lex_gte_first(dim, n, 1);
5074 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5076 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5079 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5081 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5084 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5085 __isl_take isl_set *set2)
5087 isl_map *map;
5088 map = isl_map_lex_le(isl_set_get_space(set1));
5089 map = isl_map_intersect_domain(map, set1);
5090 map = isl_map_intersect_range(map, set2);
5091 return map;
5094 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5095 __isl_take isl_set *set2)
5097 isl_map *map;
5098 map = isl_map_lex_lt(isl_set_get_space(set1));
5099 map = isl_map_intersect_domain(map, set1);
5100 map = isl_map_intersect_range(map, set2);
5101 return map;
5104 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5105 __isl_take isl_set *set2)
5107 isl_map *map;
5108 map = isl_map_lex_ge(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5114 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5115 __isl_take isl_set *set2)
5117 isl_map *map;
5118 map = isl_map_lex_gt(isl_set_get_space(set1));
5119 map = isl_map_intersect_domain(map, set1);
5120 map = isl_map_intersect_range(map, set2);
5121 return map;
5124 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5125 __isl_take isl_map *map2)
5127 isl_map *map;
5128 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5129 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5130 map = isl_map_apply_range(map, isl_map_reverse(map2));
5131 return map;
5134 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5135 __isl_take isl_map *map2)
5137 isl_map *map;
5138 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5139 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5140 map = isl_map_apply_range(map, isl_map_reverse(map2));
5141 return map;
5144 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5147 isl_map *map;
5148 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5154 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5155 __isl_take isl_map *map2)
5157 isl_map *map;
5158 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5159 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5160 map = isl_map_apply_range(map, isl_map_reverse(map2));
5161 return map;
5164 /* For a div d = floor(f/m), add the constraint
5166 * f - m d >= 0
5168 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5169 unsigned pos, isl_int *div)
5171 int i;
5172 unsigned total = isl_basic_map_total_dim(bmap);
5174 i = isl_basic_map_alloc_inequality(bmap);
5175 if (i < 0)
5176 return isl_stat_error;
5177 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5178 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5180 return isl_stat_ok;
5183 /* For a div d = floor(f/m), add the constraint
5185 * -(f-(m-1)) + m d >= 0
5187 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5188 unsigned pos, isl_int *div)
5190 int i;
5191 unsigned total = isl_basic_map_total_dim(bmap);
5193 i = isl_basic_map_alloc_inequality(bmap);
5194 if (i < 0)
5195 return isl_stat_error;
5196 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5197 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5198 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5199 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5201 return isl_stat_ok;
5204 /* For a div d = floor(f/m), add the constraints
5206 * f - m d >= 0
5207 * -(f-(m-1)) + m d >= 0
5209 * Note that the second constraint is the negation of
5211 * f - m d >= m
5213 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5214 unsigned pos, isl_int *div)
5216 if (add_upper_div_constraint(bmap, pos, div) < 0)
5217 return -1;
5218 if (add_lower_div_constraint(bmap, pos, div) < 0)
5219 return -1;
5220 return 0;
5223 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5224 unsigned pos, isl_int *div)
5226 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5227 pos, div);
5230 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5231 unsigned div)
5233 unsigned total = isl_basic_map_total_dim(bmap);
5234 unsigned div_pos = total - bmap->n_div + div;
5236 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5237 bmap->div[div]);
5240 /* For each known div d = floor(f/m), add the constraints
5242 * f - m d >= 0
5243 * -(f-(m-1)) + m d >= 0
5245 * Remove duplicate constraints in case of some these div constraints
5246 * already appear in "bmap".
5248 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5249 __isl_take isl_basic_map *bmap)
5251 unsigned n_div;
5253 if (!bmap)
5254 return NULL;
5255 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5256 if (n_div == 0)
5257 return bmap;
5259 bmap = add_known_div_constraints(bmap);
5260 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5261 bmap = isl_basic_map_finalize(bmap);
5262 return bmap;
5265 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5267 * In particular, if this div is of the form d = floor(f/m),
5268 * then add the constraint
5270 * f - m d >= 0
5272 * if sign < 0 or the constraint
5274 * -(f-(m-1)) + m d >= 0
5276 * if sign > 0.
5278 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5279 unsigned div, int sign)
5281 unsigned total;
5282 unsigned div_pos;
5284 if (!bmap)
5285 return -1;
5287 total = isl_basic_map_total_dim(bmap);
5288 div_pos = total - bmap->n_div + div;
5290 if (sign < 0)
5291 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5292 else
5293 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5296 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5297 __isl_take isl_basic_map *bmap)
5299 if (!bmap)
5300 goto error;
5301 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5302 bmap->n_div == 0 &&
5303 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5304 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5305 return bset_from_bmap(bmap);
5306 bmap = isl_basic_map_cow(bmap);
5307 if (!bmap)
5308 goto error;
5309 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5310 if (!bmap->dim)
5311 goto error;
5312 bmap->extra -= bmap->n_div;
5313 bmap->n_div = 0;
5314 bmap = isl_basic_map_finalize(bmap);
5315 return bset_from_bmap(bmap);
5316 error:
5317 isl_basic_map_free(bmap);
5318 return NULL;
5321 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5322 __isl_take isl_basic_set *bset)
5324 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5327 /* Replace each element in "list" by the result of applying
5328 * isl_basic_map_underlying_set to the element.
5330 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5331 __isl_take isl_basic_map_list *list)
5333 int i, n;
5335 if (!list)
5336 return NULL;
5338 n = isl_basic_map_list_n_basic_map(list);
5339 for (i = 0; i < n; ++i) {
5340 isl_basic_map *bmap;
5341 isl_basic_set *bset;
5343 bmap = isl_basic_map_list_get_basic_map(list, i);
5344 bset = isl_basic_set_underlying_set(bmap);
5345 list = isl_basic_set_list_set_basic_set(list, i, bset);
5348 return list;
5351 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5352 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5354 struct isl_basic_map *bmap;
5355 struct isl_ctx *ctx;
5356 unsigned total;
5357 int i;
5359 if (!bset || !like)
5360 goto error;
5361 ctx = bset->ctx;
5362 isl_assert(ctx, bset->n_div == 0, goto error);
5363 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5364 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5365 goto error);
5366 if (like->n_div == 0) {
5367 isl_space *space = isl_basic_map_get_space(like);
5368 isl_basic_map_free(like);
5369 return isl_basic_map_reset_space(bset, space);
5371 bset = isl_basic_set_cow(bset);
5372 if (!bset)
5373 goto error;
5374 total = bset->dim->n_out + bset->extra;
5375 bmap = bset_to_bmap(bset);
5376 isl_space_free(bmap->dim);
5377 bmap->dim = isl_space_copy(like->dim);
5378 if (!bmap->dim)
5379 goto error;
5380 bmap->n_div = like->n_div;
5381 bmap->extra += like->n_div;
5382 if (bmap->extra) {
5383 unsigned ltotal;
5384 isl_int **div;
5385 ltotal = total - bmap->extra + like->extra;
5386 if (ltotal > total)
5387 ltotal = total;
5388 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5389 bmap->extra * (1 + 1 + total));
5390 if (isl_blk_is_error(bmap->block2))
5391 goto error;
5392 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5393 if (!div)
5394 goto error;
5395 bmap->div = div;
5396 for (i = 0; i < bmap->extra; ++i)
5397 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5398 for (i = 0; i < like->n_div; ++i) {
5399 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5400 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5402 bmap = isl_basic_map_add_known_div_constraints(bmap);
5404 isl_basic_map_free(like);
5405 bmap = isl_basic_map_simplify(bmap);
5406 bmap = isl_basic_map_finalize(bmap);
5407 return bmap;
5408 error:
5409 isl_basic_map_free(like);
5410 isl_basic_set_free(bset);
5411 return NULL;
5414 struct isl_basic_set *isl_basic_set_from_underlying_set(
5415 struct isl_basic_set *bset, struct isl_basic_set *like)
5417 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5418 bset_to_bmap(like)));
5421 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5423 int i;
5425 map = isl_map_cow(map);
5426 if (!map)
5427 return NULL;
5428 map->dim = isl_space_cow(map->dim);
5429 if (!map->dim)
5430 goto error;
5432 for (i = 1; i < map->n; ++i)
5433 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5434 goto error);
5435 for (i = 0; i < map->n; ++i) {
5436 map->p[i] = bset_to_bmap(
5437 isl_basic_map_underlying_set(map->p[i]));
5438 if (!map->p[i])
5439 goto error;
5441 if (map->n == 0)
5442 map->dim = isl_space_underlying(map->dim, 0);
5443 else {
5444 isl_space_free(map->dim);
5445 map->dim = isl_space_copy(map->p[0]->dim);
5447 if (!map->dim)
5448 goto error;
5449 return set_from_map(map);
5450 error:
5451 isl_map_free(map);
5452 return NULL;
5455 /* Replace the space of "bmap" by "space".
5457 * If the space of "bmap" is identical to "space" (including the identifiers
5458 * of the input and output dimensions), then simply return the original input.
5460 __isl_give isl_basic_map *isl_basic_map_reset_space(
5461 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5463 isl_bool equal;
5464 isl_space *bmap_space;
5466 bmap_space = isl_basic_map_peek_space(bmap);
5467 equal = isl_space_is_equal(bmap_space, space);
5468 if (equal >= 0 && equal)
5469 equal = isl_space_has_equal_ids(bmap_space, space);
5470 if (equal < 0)
5471 goto error;
5472 if (equal) {
5473 isl_space_free(space);
5474 return bmap;
5476 bmap = isl_basic_map_cow(bmap);
5477 if (!bmap || !space)
5478 goto error;
5480 isl_space_free(bmap->dim);
5481 bmap->dim = space;
5483 bmap = isl_basic_map_finalize(bmap);
5485 return bmap;
5486 error:
5487 isl_basic_map_free(bmap);
5488 isl_space_free(space);
5489 return NULL;
5492 __isl_give isl_basic_set *isl_basic_set_reset_space(
5493 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5495 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5496 dim));
5499 /* Check that the total dimensions of "map" and "space" are the same.
5501 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5502 __isl_keep isl_space *space)
5504 unsigned dim1, dim2;
5506 if (!map || !space)
5507 return isl_stat_error;
5508 dim1 = isl_map_dim(map, isl_dim_all);
5509 dim2 = isl_space_dim(space, isl_dim_all);
5510 if (dim1 == dim2)
5511 return isl_stat_ok;
5512 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5513 "total dimensions do not match", return isl_stat_error);
5516 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5517 __isl_take isl_space *space)
5519 int i;
5521 map = isl_map_cow(map);
5522 if (!map || !space)
5523 goto error;
5525 for (i = 0; i < map->n; ++i) {
5526 map->p[i] = isl_basic_map_reset_space(map->p[i],
5527 isl_space_copy(space));
5528 if (!map->p[i])
5529 goto error;
5531 isl_space_free(map->dim);
5532 map->dim = space;
5534 return map;
5535 error:
5536 isl_map_free(map);
5537 isl_space_free(space);
5538 return NULL;
5541 /* Replace the space of "map" by "space", without modifying
5542 * the dimension of "map".
5544 * If the space of "map" is identical to "space" (including the identifiers
5545 * of the input and output dimensions), then simply return the original input.
5547 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5548 __isl_take isl_space *space)
5550 isl_bool equal;
5551 isl_space *map_space;
5553 map_space = isl_map_peek_space(map);
5554 equal = isl_space_is_equal(map_space, space);
5555 if (equal >= 0 && equal)
5556 equal = isl_space_has_equal_ids(map_space, space);
5557 if (equal < 0)
5558 goto error;
5559 if (equal) {
5560 isl_space_free(space);
5561 return map;
5563 if (check_map_space_equal_total_dim(map, space) < 0)
5564 goto error;
5565 return isl_map_reset_space(map, space);
5566 error:
5567 isl_map_free(map);
5568 isl_space_free(space);
5569 return NULL;
5572 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5573 __isl_take isl_space *dim)
5575 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5578 /* Compute the parameter domain of the given basic set.
5580 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5582 isl_bool is_params;
5583 isl_space *space;
5584 unsigned n;
5586 is_params = isl_basic_set_is_params(bset);
5587 if (is_params < 0)
5588 return isl_basic_set_free(bset);
5589 if (is_params)
5590 return bset;
5592 n = isl_basic_set_dim(bset, isl_dim_set);
5593 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5594 space = isl_basic_set_get_space(bset);
5595 space = isl_space_params(space);
5596 bset = isl_basic_set_reset_space(bset, space);
5597 return bset;
5600 /* Construct a zero-dimensional basic set with the given parameter domain.
5602 __isl_give isl_basic_set *isl_basic_set_from_params(
5603 __isl_take isl_basic_set *bset)
5605 isl_space *space;
5606 space = isl_basic_set_get_space(bset);
5607 space = isl_space_set_from_params(space);
5608 bset = isl_basic_set_reset_space(bset, space);
5609 return bset;
5612 /* Compute the parameter domain of the given set.
5614 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5616 isl_space *space;
5617 unsigned n;
5619 if (isl_set_is_params(set))
5620 return set;
5622 n = isl_set_dim(set, isl_dim_set);
5623 set = isl_set_project_out(set, isl_dim_set, 0, n);
5624 space = isl_set_get_space(set);
5625 space = isl_space_params(space);
5626 set = isl_set_reset_space(set, space);
5627 return set;
5630 /* Construct a zero-dimensional set with the given parameter domain.
5632 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5634 isl_space *space;
5635 space = isl_set_get_space(set);
5636 space = isl_space_set_from_params(space);
5637 set = isl_set_reset_space(set, space);
5638 return set;
5641 /* Compute the parameter domain of the given map.
5643 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5645 isl_space *space;
5646 unsigned n;
5648 n = isl_map_dim(map, isl_dim_in);
5649 map = isl_map_project_out(map, isl_dim_in, 0, n);
5650 n = isl_map_dim(map, isl_dim_out);
5651 map = isl_map_project_out(map, isl_dim_out, 0, n);
5652 space = isl_map_get_space(map);
5653 space = isl_space_params(space);
5654 map = isl_map_reset_space(map, space);
5655 return map;
5658 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5660 isl_space *space;
5661 unsigned n_out;
5663 if (!bmap)
5664 return NULL;
5665 space = isl_space_domain(isl_basic_map_get_space(bmap));
5667 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5668 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5670 return isl_basic_map_reset_space(bmap, space);
5673 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5675 if (!bmap)
5676 return isl_bool_error;
5677 return isl_space_may_be_set(bmap->dim);
5680 /* Is this basic map actually a set?
5681 * Users should never call this function. Outside of isl,
5682 * the type should indicate whether something is a set or a map.
5684 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5686 if (!bmap)
5687 return isl_bool_error;
5688 return isl_space_is_set(bmap->dim);
5691 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5693 isl_bool is_set;
5695 is_set = isl_basic_map_is_set(bmap);
5696 if (is_set < 0)
5697 goto error;
5698 if (is_set)
5699 return bmap;
5700 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5701 error:
5702 isl_basic_map_free(bmap);
5703 return NULL;
5706 __isl_give isl_basic_map *isl_basic_map_domain_map(
5707 __isl_take isl_basic_map *bmap)
5709 int i;
5710 isl_space *space;
5711 isl_basic_map *domain;
5712 int nparam, n_in, n_out;
5714 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5715 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5716 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5718 space = isl_basic_map_get_space(bmap);
5719 space = isl_space_from_range(isl_space_domain(space));
5720 domain = isl_basic_map_universe(space);
5722 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5723 bmap = isl_basic_map_apply_range(bmap, domain);
5724 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5726 for (i = 0; i < n_in; ++i)
5727 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5728 isl_dim_out, i);
5730 bmap = isl_basic_map_gauss(bmap, NULL);
5731 return isl_basic_map_finalize(bmap);
5734 __isl_give isl_basic_map *isl_basic_map_range_map(
5735 __isl_take isl_basic_map *bmap)
5737 int i;
5738 isl_space *space;
5739 isl_basic_map *range;
5740 int nparam, n_in, n_out;
5742 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5743 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5744 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5746 space = isl_basic_map_get_space(bmap);
5747 space = isl_space_from_range(isl_space_range(space));
5748 range = isl_basic_map_universe(space);
5750 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5751 bmap = isl_basic_map_apply_range(bmap, range);
5752 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5754 for (i = 0; i < n_out; ++i)
5755 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5756 isl_dim_out, i);
5758 bmap = isl_basic_map_gauss(bmap, NULL);
5759 return isl_basic_map_finalize(bmap);
5762 int isl_map_may_be_set(__isl_keep isl_map *map)
5764 if (!map)
5765 return -1;
5766 return isl_space_may_be_set(map->dim);
5769 /* Is this map actually a set?
5770 * Users should never call this function. Outside of isl,
5771 * the type should indicate whether something is a set or a map.
5773 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5775 if (!map)
5776 return isl_bool_error;
5777 return isl_space_is_set(map->dim);
5780 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5782 int i;
5783 isl_bool is_set;
5784 struct isl_set *set;
5786 is_set = isl_map_is_set(map);
5787 if (is_set < 0)
5788 goto error;
5789 if (is_set)
5790 return set_from_map(map);
5792 map = isl_map_cow(map);
5793 if (!map)
5794 goto error;
5796 set = set_from_map(map);
5797 set->dim = isl_space_range(set->dim);
5798 if (!set->dim)
5799 goto error;
5800 for (i = 0; i < map->n; ++i) {
5801 set->p[i] = isl_basic_map_range(map->p[i]);
5802 if (!set->p[i])
5803 goto error;
5805 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5806 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5807 return set;
5808 error:
5809 isl_map_free(map);
5810 return NULL;
5813 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5815 int i;
5817 map = isl_map_cow(map);
5818 if (!map)
5819 return NULL;
5821 map->dim = isl_space_domain_map(map->dim);
5822 if (!map->dim)
5823 goto error;
5824 for (i = 0; i < map->n; ++i) {
5825 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5826 if (!map->p[i])
5827 goto error;
5829 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5830 map = isl_map_unmark_normalized(map);
5831 return map;
5832 error:
5833 isl_map_free(map);
5834 return NULL;
5837 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5839 int i;
5840 isl_space *range_dim;
5842 map = isl_map_cow(map);
5843 if (!map)
5844 return NULL;
5846 range_dim = isl_space_range(isl_map_get_space(map));
5847 range_dim = isl_space_from_range(range_dim);
5848 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5849 map->dim = isl_space_join(map->dim, range_dim);
5850 if (!map->dim)
5851 goto error;
5852 for (i = 0; i < map->n; ++i) {
5853 map->p[i] = isl_basic_map_range_map(map->p[i]);
5854 if (!map->p[i])
5855 goto error;
5857 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5858 map = isl_map_unmark_normalized(map);
5859 return map;
5860 error:
5861 isl_map_free(map);
5862 return NULL;
5865 /* Given a wrapped map of the form A[B -> C],
5866 * return the map A[B -> C] -> B.
5868 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5870 isl_id *id;
5871 isl_map *map;
5873 if (!set)
5874 return NULL;
5875 if (!isl_set_has_tuple_id(set))
5876 return isl_map_domain_map(isl_set_unwrap(set));
5878 id = isl_set_get_tuple_id(set);
5879 map = isl_map_domain_map(isl_set_unwrap(set));
5880 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5882 return map;
5885 __isl_give isl_basic_map *isl_basic_map_from_domain(
5886 __isl_take isl_basic_set *bset)
5888 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5891 __isl_give isl_basic_map *isl_basic_map_from_range(
5892 __isl_take isl_basic_set *bset)
5894 isl_space *space;
5895 space = isl_basic_set_get_space(bset);
5896 space = isl_space_from_range(space);
5897 bset = isl_basic_set_reset_space(bset, space);
5898 return bset_to_bmap(bset);
5901 /* Create a relation with the given set as range.
5902 * The domain of the created relation is a zero-dimensional
5903 * flat anonymous space.
5905 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5907 isl_space *space;
5908 space = isl_set_get_space(set);
5909 space = isl_space_from_range(space);
5910 set = isl_set_reset_space(set, space);
5911 return set_to_map(set);
5914 /* Create a relation with the given set as domain.
5915 * The range of the created relation is a zero-dimensional
5916 * flat anonymous space.
5918 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5920 return isl_map_reverse(isl_map_from_range(set));
5923 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5924 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5926 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5929 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5930 __isl_take isl_set *range)
5932 return isl_map_apply_range(isl_map_reverse(domain), range);
5935 /* Return a newly allocated isl_map with given space and flags and
5936 * room for "n" basic maps.
5937 * Make sure that all cached information is cleared.
5939 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5940 unsigned flags)
5942 struct isl_map *map;
5944 if (!space)
5945 return NULL;
5946 if (n < 0)
5947 isl_die(space->ctx, isl_error_internal,
5948 "negative number of basic maps", goto error);
5949 map = isl_calloc(space->ctx, struct isl_map,
5950 sizeof(struct isl_map) +
5951 (n - 1) * sizeof(struct isl_basic_map *));
5952 if (!map)
5953 goto error;
5955 map->ctx = space->ctx;
5956 isl_ctx_ref(map->ctx);
5957 map->ref = 1;
5958 map->size = n;
5959 map->n = 0;
5960 map->dim = space;
5961 map->flags = flags;
5962 return map;
5963 error:
5964 isl_space_free(space);
5965 return NULL;
5968 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5970 struct isl_basic_map *bmap;
5971 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5972 bmap = isl_basic_map_set_to_empty(bmap);
5973 return bmap;
5976 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5978 struct isl_basic_set *bset;
5979 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5980 bset = isl_basic_set_set_to_empty(bset);
5981 return bset;
5984 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5986 struct isl_basic_map *bmap;
5987 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5988 bmap = isl_basic_map_finalize(bmap);
5989 return bmap;
5992 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5994 struct isl_basic_set *bset;
5995 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5996 bset = isl_basic_set_finalize(bset);
5997 return bset;
6000 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6001 __isl_take isl_space *space)
6003 int i;
6004 unsigned total = isl_space_dim(space, isl_dim_all);
6005 isl_basic_map *bmap;
6007 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6008 for (i = 0; i < total; ++i) {
6009 int k = isl_basic_map_alloc_inequality(bmap);
6010 if (k < 0)
6011 goto error;
6012 isl_seq_clr(bmap->ineq[k], 1 + total);
6013 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6015 return bmap;
6016 error:
6017 isl_basic_map_free(bmap);
6018 return NULL;
6021 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6022 __isl_take isl_space *space)
6024 return isl_basic_map_nat_universe(space);
6027 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6029 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6032 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6034 return isl_map_nat_universe(dim);
6037 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6039 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6042 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6044 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6047 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6049 struct isl_map *map;
6050 if (!space)
6051 return NULL;
6052 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6053 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6054 return map;
6057 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6059 struct isl_set *set;
6060 if (!space)
6061 return NULL;
6062 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6063 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6064 return set;
6067 struct isl_map *isl_map_dup(struct isl_map *map)
6069 int i;
6070 struct isl_map *dup;
6072 if (!map)
6073 return NULL;
6074 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6075 for (i = 0; i < map->n; ++i)
6076 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6077 return dup;
6080 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6081 __isl_take isl_basic_map *bmap)
6083 if (!bmap || !map)
6084 goto error;
6085 if (isl_basic_map_plain_is_empty(bmap)) {
6086 isl_basic_map_free(bmap);
6087 return map;
6089 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6090 isl_assert(map->ctx, map->n < map->size, goto error);
6091 map->p[map->n] = bmap;
6092 map->n++;
6093 map = isl_map_unmark_normalized(map);
6094 return map;
6095 error:
6096 if (map)
6097 isl_map_free(map);
6098 if (bmap)
6099 isl_basic_map_free(bmap);
6100 return NULL;
6103 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6105 int i;
6107 if (!map)
6108 return NULL;
6110 if (--map->ref > 0)
6111 return NULL;
6113 clear_caches(map);
6114 isl_ctx_deref(map->ctx);
6115 for (i = 0; i < map->n; ++i)
6116 isl_basic_map_free(map->p[i]);
6117 isl_space_free(map->dim);
6118 free(map);
6120 return NULL;
6123 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6124 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6126 int j;
6128 bmap = isl_basic_map_cow(bmap);
6129 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6130 j = isl_basic_map_alloc_equality(bmap);
6131 if (j < 0)
6132 goto error;
6133 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6134 isl_int_set_si(bmap->eq[j][pos], -1);
6135 isl_int_set_si(bmap->eq[j][0], value);
6136 bmap = isl_basic_map_simplify(bmap);
6137 return isl_basic_map_finalize(bmap);
6138 error:
6139 isl_basic_map_free(bmap);
6140 return NULL;
6143 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6144 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6146 int j;
6148 bmap = isl_basic_map_cow(bmap);
6149 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6150 j = isl_basic_map_alloc_equality(bmap);
6151 if (j < 0)
6152 goto error;
6153 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6154 isl_int_set_si(bmap->eq[j][pos], -1);
6155 isl_int_set(bmap->eq[j][0], value);
6156 bmap = isl_basic_map_simplify(bmap);
6157 return isl_basic_map_finalize(bmap);
6158 error:
6159 isl_basic_map_free(bmap);
6160 return NULL;
6163 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6164 enum isl_dim_type type, unsigned pos, int value)
6166 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6167 return isl_basic_map_free(bmap);
6168 return isl_basic_map_fix_pos_si(bmap,
6169 isl_basic_map_offset(bmap, type) + pos, value);
6172 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6173 enum isl_dim_type type, unsigned pos, isl_int value)
6175 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6176 return isl_basic_map_free(bmap);
6177 return isl_basic_map_fix_pos(bmap,
6178 isl_basic_map_offset(bmap, type) + pos, value);
6181 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6182 * to be equal to "v".
6184 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6185 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6187 if (!bmap || !v)
6188 goto error;
6189 if (!isl_val_is_int(v))
6190 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6191 "expecting integer value", goto error);
6192 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6193 goto error;
6194 pos += isl_basic_map_offset(bmap, type);
6195 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6196 isl_val_free(v);
6197 return bmap;
6198 error:
6199 isl_basic_map_free(bmap);
6200 isl_val_free(v);
6201 return NULL;
6204 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6205 * to be equal to "v".
6207 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6208 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6210 return isl_basic_map_fix_val(bset, type, pos, v);
6213 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6214 enum isl_dim_type type, unsigned pos, int value)
6216 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6217 type, pos, value));
6220 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6221 enum isl_dim_type type, unsigned pos, isl_int value)
6223 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6224 type, pos, value));
6227 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6228 unsigned input, int value)
6230 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6233 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6234 unsigned dim, int value)
6236 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6237 isl_dim_set, dim, value));
6240 /* Remove the basic map at position "i" from "map" if this basic map
6241 * is (obviously) empty.
6243 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6245 isl_bool empty;
6247 if (!map)
6248 return NULL;
6250 empty = isl_basic_map_plain_is_empty(map->p[i]);
6251 if (empty < 0)
6252 return isl_map_free(map);
6253 if (!empty)
6254 return map;
6256 isl_basic_map_free(map->p[i]);
6257 map->n--;
6258 if (i != map->n) {
6259 map->p[i] = map->p[map->n];
6260 map = isl_map_unmark_normalized(map);
6264 return map;
6267 /* Perform "fn" on each basic map of "map", where we may not be holding
6268 * the only reference to "map".
6269 * In particular, "fn" should be a semantics preserving operation
6270 * that we want to apply to all copies of "map". We therefore need
6271 * to be careful not to modify "map" in a way that breaks "map"
6272 * in case anything goes wrong.
6274 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6275 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6277 struct isl_basic_map *bmap;
6278 int i;
6280 if (!map)
6281 return NULL;
6283 for (i = map->n - 1; i >= 0; --i) {
6284 bmap = isl_basic_map_copy(map->p[i]);
6285 bmap = fn(bmap);
6286 if (!bmap)
6287 goto error;
6288 isl_basic_map_free(map->p[i]);
6289 map->p[i] = bmap;
6290 map = remove_if_empty(map, i);
6291 if (!map)
6292 return NULL;
6295 return map;
6296 error:
6297 isl_map_free(map);
6298 return NULL;
6301 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6302 enum isl_dim_type type, unsigned pos, int value)
6304 int i;
6306 map = isl_map_cow(map);
6307 if (!map)
6308 return NULL;
6310 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6311 for (i = map->n - 1; i >= 0; --i) {
6312 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6313 map = remove_if_empty(map, i);
6314 if (!map)
6315 return NULL;
6317 map = isl_map_unmark_normalized(map);
6318 return map;
6319 error:
6320 isl_map_free(map);
6321 return NULL;
6324 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6325 enum isl_dim_type type, unsigned pos, int value)
6327 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6330 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6331 enum isl_dim_type type, unsigned pos, isl_int value)
6333 int i;
6335 map = isl_map_cow(map);
6336 if (!map)
6337 return NULL;
6339 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6340 for (i = 0; i < map->n; ++i) {
6341 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6342 if (!map->p[i])
6343 goto error;
6345 map = isl_map_unmark_normalized(map);
6346 return map;
6347 error:
6348 isl_map_free(map);
6349 return NULL;
6352 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6353 enum isl_dim_type type, unsigned pos, isl_int value)
6355 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6358 /* Fix the value of the variable at position "pos" of type "type" of "map"
6359 * to be equal to "v".
6361 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6362 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6364 int i;
6366 map = isl_map_cow(map);
6367 if (!map || !v)
6368 goto error;
6370 if (!isl_val_is_int(v))
6371 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6372 "expecting integer value", goto error);
6373 if (pos >= isl_map_dim(map, type))
6374 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6375 "index out of bounds", 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 (!map)
6465 return NULL;
6467 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6468 for (i = 0; i < map->n; ++i) {
6469 map->p[i] = basic_map_bound_si(map->p[i],
6470 type, pos, value, upper);
6471 if (!map->p[i])
6472 goto error;
6474 map = isl_map_unmark_normalized(map);
6475 return map;
6476 error:
6477 isl_map_free(map);
6478 return NULL;
6481 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6482 enum isl_dim_type type, unsigned pos, int value)
6484 return map_bound_si(map, type, pos, value, 0);
6487 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6488 enum isl_dim_type type, unsigned pos, int value)
6490 return map_bound_si(map, type, pos, value, 1);
6493 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6494 enum isl_dim_type type, unsigned pos, int value)
6496 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6497 type, pos, value));
6500 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6501 enum isl_dim_type type, unsigned pos, int value)
6503 return isl_map_upper_bound_si(set, type, pos, value);
6506 /* Bound the given variable of "bmap" from below (or above is "upper"
6507 * is set) to "value".
6509 static __isl_give isl_basic_map *basic_map_bound(
6510 __isl_take isl_basic_map *bmap,
6511 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6513 int j;
6515 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6516 return isl_basic_map_free(bmap);
6517 pos += isl_basic_map_offset(bmap, type);
6518 bmap = isl_basic_map_cow(bmap);
6519 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6520 j = isl_basic_map_alloc_inequality(bmap);
6521 if (j < 0)
6522 goto error;
6523 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6524 if (upper) {
6525 isl_int_set_si(bmap->ineq[j][pos], -1);
6526 isl_int_set(bmap->ineq[j][0], value);
6527 } else {
6528 isl_int_set_si(bmap->ineq[j][pos], 1);
6529 isl_int_neg(bmap->ineq[j][0], value);
6531 bmap = isl_basic_map_simplify(bmap);
6532 return isl_basic_map_finalize(bmap);
6533 error:
6534 isl_basic_map_free(bmap);
6535 return NULL;
6538 /* Bound the given variable of "map" from below (or above is "upper"
6539 * is set) to "value".
6541 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6542 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6544 int i;
6546 map = isl_map_cow(map);
6547 if (!map)
6548 return NULL;
6550 if (pos >= isl_map_dim(map, type))
6551 isl_die(map->ctx, isl_error_invalid,
6552 "index out of bounds", goto error);
6553 for (i = map->n - 1; i >= 0; --i) {
6554 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6555 map = remove_if_empty(map, i);
6556 if (!map)
6557 return NULL;
6559 map = isl_map_unmark_normalized(map);
6560 return map;
6561 error:
6562 isl_map_free(map);
6563 return NULL;
6566 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6567 enum isl_dim_type type, unsigned pos, isl_int value)
6569 return map_bound(map, type, pos, value, 0);
6572 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6573 enum isl_dim_type type, unsigned pos, isl_int value)
6575 return map_bound(map, type, pos, value, 1);
6578 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6579 enum isl_dim_type type, unsigned pos, isl_int value)
6581 return isl_map_lower_bound(set, type, pos, value);
6584 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6585 enum isl_dim_type type, unsigned pos, isl_int value)
6587 return isl_map_upper_bound(set, type, pos, value);
6590 /* Force the values of the variable at position "pos" of type "type" of "set"
6591 * to be no smaller than "value".
6593 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6594 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6596 if (!value)
6597 goto error;
6598 if (!isl_val_is_int(value))
6599 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6600 "expecting integer value", goto error);
6601 set = isl_set_lower_bound(set, type, pos, value->n);
6602 isl_val_free(value);
6603 return set;
6604 error:
6605 isl_val_free(value);
6606 isl_set_free(set);
6607 return NULL;
6610 /* Force the values of the variable at position "pos" of type "type" of "set"
6611 * to be no greater than "value".
6613 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6614 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6616 if (!value)
6617 goto error;
6618 if (!isl_val_is_int(value))
6619 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6620 "expecting integer value", goto error);
6621 set = isl_set_upper_bound(set, type, pos, value->n);
6622 isl_val_free(value);
6623 return set;
6624 error:
6625 isl_val_free(value);
6626 isl_set_free(set);
6627 return NULL;
6630 /* Bound the given variable of "bset" from below (or above is "upper"
6631 * is set) to "value".
6633 static __isl_give isl_basic_set *isl_basic_set_bound(
6634 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6635 isl_int value, int upper)
6637 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6638 type, pos, value, upper));
6641 /* Bound the given variable of "bset" from below (or above is "upper"
6642 * is set) to "value".
6644 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6645 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6646 __isl_take isl_val *value, int upper)
6648 if (!value)
6649 goto error;
6650 if (!isl_val_is_int(value))
6651 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6652 "expecting integer value", goto error);
6653 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6654 isl_val_free(value);
6655 return bset;
6656 error:
6657 isl_val_free(value);
6658 isl_basic_set_free(bset);
6659 return NULL;
6662 /* Bound the given variable of "bset" from below to "value".
6664 __isl_give isl_basic_set *isl_basic_set_lower_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, 0);
6671 /* Bound the given variable of "bset" from above to "value".
6673 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6674 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6675 __isl_take isl_val *value)
6677 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6680 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6682 int i;
6684 map = isl_map_cow(map);
6685 if (!map)
6686 return NULL;
6688 map->dim = isl_space_reverse(map->dim);
6689 if (!map->dim)
6690 goto error;
6691 for (i = 0; i < map->n; ++i) {
6692 map->p[i] = isl_basic_map_reverse(map->p[i]);
6693 if (!map->p[i])
6694 goto error;
6696 map = isl_map_unmark_normalized(map);
6697 return map;
6698 error:
6699 isl_map_free(map);
6700 return NULL;
6703 #undef TYPE
6704 #define TYPE isl_pw_multi_aff
6705 #undef SUFFIX
6706 #define SUFFIX _pw_multi_aff
6707 #undef EMPTY
6708 #define EMPTY isl_pw_multi_aff_empty
6709 #undef ADD
6710 #define ADD isl_pw_multi_aff_union_add
6711 #include "isl_map_lexopt_templ.c"
6713 /* Given a map "map", compute the lexicographically minimal
6714 * (or maximal) image element for each domain element in dom,
6715 * in the form of an isl_pw_multi_aff.
6716 * If "empty" is not NULL, then set *empty to those elements in dom that
6717 * do not have an image element.
6718 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6719 * should be computed over the domain of "map". "empty" is also NULL
6720 * in this case.
6722 * We first compute the lexicographically minimal or maximal element
6723 * in the first basic map. This results in a partial solution "res"
6724 * and a subset "todo" of dom that still need to be handled.
6725 * We then consider each of the remaining maps in "map" and successively
6726 * update both "res" and "todo".
6727 * If "empty" is NULL, then the todo sets are not needed and therefore
6728 * also not computed.
6730 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6731 __isl_take isl_map *map, __isl_take isl_set *dom,
6732 __isl_give isl_set **empty, unsigned flags)
6734 int i;
6735 int full;
6736 isl_pw_multi_aff *res;
6737 isl_set *todo;
6739 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6740 if (!map || (!full && !dom))
6741 goto error;
6743 if (isl_map_plain_is_empty(map)) {
6744 if (empty)
6745 *empty = dom;
6746 else
6747 isl_set_free(dom);
6748 return isl_pw_multi_aff_from_map(map);
6751 res = basic_map_partial_lexopt_pw_multi_aff(
6752 isl_basic_map_copy(map->p[0]),
6753 isl_set_copy(dom), empty, flags);
6755 if (empty)
6756 todo = *empty;
6757 for (i = 1; i < map->n; ++i) {
6758 isl_pw_multi_aff *res_i;
6760 res_i = basic_map_partial_lexopt_pw_multi_aff(
6761 isl_basic_map_copy(map->p[i]),
6762 isl_set_copy(dom), empty, flags);
6764 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6765 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6766 else
6767 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6769 if (empty)
6770 todo = isl_set_intersect(todo, *empty);
6773 isl_set_free(dom);
6774 isl_map_free(map);
6776 if (empty)
6777 *empty = todo;
6779 return res;
6780 error:
6781 if (empty)
6782 *empty = NULL;
6783 isl_set_free(dom);
6784 isl_map_free(map);
6785 return NULL;
6788 #undef TYPE
6789 #define TYPE isl_map
6790 #undef SUFFIX
6791 #define SUFFIX
6792 #undef EMPTY
6793 #define EMPTY isl_map_empty
6794 #undef ADD
6795 #define ADD isl_map_union_disjoint
6796 #include "isl_map_lexopt_templ.c"
6798 /* Given a map "map", compute the lexicographically minimal
6799 * (or maximal) image element for each domain element in "dom",
6800 * in the form of an isl_map.
6801 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6802 * do not have an image element.
6803 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6804 * should be computed over the domain of "map". "empty" is also NULL
6805 * in this case.
6807 * If the input consists of more than one disjunct, then first
6808 * compute the desired result in the form of an isl_pw_multi_aff and
6809 * then convert that into an isl_map.
6811 * This function used to have an explicit implementation in terms
6812 * of isl_maps, but it would continually intersect the domains of
6813 * partial results with the complement of the domain of the next
6814 * partial solution, potentially leading to an explosion in the number
6815 * of disjuncts if there are several disjuncts in the input.
6816 * An even earlier implementation of this function would look for
6817 * better results in the domain of the partial result and for extra
6818 * results in the complement of this domain, which would lead to
6819 * even more splintering.
6821 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6822 __isl_take isl_map *map, __isl_take isl_set *dom,
6823 __isl_give isl_set **empty, unsigned flags)
6825 int full;
6826 struct isl_map *res;
6827 isl_pw_multi_aff *pma;
6829 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6830 if (!map || (!full && !dom))
6831 goto error;
6833 if (isl_map_plain_is_empty(map)) {
6834 if (empty)
6835 *empty = dom;
6836 else
6837 isl_set_free(dom);
6838 return map;
6841 if (map->n == 1) {
6842 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6843 dom, empty, flags);
6844 isl_map_free(map);
6845 return res;
6848 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6849 flags);
6850 return isl_map_from_pw_multi_aff(pma);
6851 error:
6852 if (empty)
6853 *empty = NULL;
6854 isl_set_free(dom);
6855 isl_map_free(map);
6856 return NULL;
6859 __isl_give isl_map *isl_map_partial_lexmax(
6860 __isl_take isl_map *map, __isl_take isl_set *dom,
6861 __isl_give isl_set **empty)
6863 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6866 __isl_give isl_map *isl_map_partial_lexmin(
6867 __isl_take isl_map *map, __isl_take isl_set *dom,
6868 __isl_give isl_set **empty)
6870 return isl_map_partial_lexopt(map, dom, empty, 0);
6873 __isl_give isl_set *isl_set_partial_lexmin(
6874 __isl_take isl_set *set, __isl_take isl_set *dom,
6875 __isl_give isl_set **empty)
6877 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6878 dom, empty));
6881 __isl_give isl_set *isl_set_partial_lexmax(
6882 __isl_take isl_set *set, __isl_take isl_set *dom,
6883 __isl_give isl_set **empty)
6885 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6886 dom, empty));
6889 /* Compute the lexicographic minimum (or maximum if "flags" includes
6890 * ISL_OPT_MAX) of "bset" over its parametric domain.
6892 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6893 unsigned flags)
6895 return isl_basic_map_lexopt(bset, flags);
6898 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6900 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6903 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6905 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6908 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6910 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6913 /* Compute the lexicographic minimum of "bset" over its parametric domain
6914 * for the purpose of quantifier elimination.
6915 * That is, find an explicit representation for all the existentially
6916 * quantified variables in "bset" by computing their lexicographic
6917 * minimum.
6919 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6920 __isl_take isl_basic_set *bset)
6922 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6925 /* Given a basic map with one output dimension, compute the minimum or
6926 * maximum of that dimension as an isl_pw_aff.
6928 * Compute the optimum as a lexicographic optimum over the single
6929 * output dimension and extract the single isl_pw_aff from the result.
6931 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6932 int max)
6934 isl_pw_multi_aff *pma;
6935 isl_pw_aff *pwaff;
6937 bmap = isl_basic_map_copy(bmap);
6938 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6939 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6940 isl_pw_multi_aff_free(pma);
6942 return pwaff;
6945 /* Compute the minimum or maximum of the given output dimension
6946 * as a function of the parameters and the input dimensions,
6947 * but independently of the other output dimensions.
6949 * We first project out the other output dimension and then compute
6950 * the "lexicographic" maximum in each basic map, combining the results
6951 * using isl_pw_aff_union_max.
6953 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6954 int max)
6956 int i;
6957 isl_pw_aff *pwaff;
6958 unsigned n_out;
6960 n_out = isl_map_dim(map, isl_dim_out);
6961 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6962 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6963 if (!map)
6964 return NULL;
6966 if (map->n == 0) {
6967 isl_space *space = isl_map_get_space(map);
6968 isl_map_free(map);
6969 return isl_pw_aff_empty(space);
6972 pwaff = basic_map_dim_opt(map->p[0], max);
6973 for (i = 1; i < map->n; ++i) {
6974 isl_pw_aff *pwaff_i;
6976 pwaff_i = basic_map_dim_opt(map->p[i], max);
6977 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6980 isl_map_free(map);
6982 return pwaff;
6985 /* Compute the minimum 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_min(__isl_take isl_map *map, int pos)
6991 return map_dim_opt(map, pos, 0);
6994 /* Compute the maximum of the given output dimension as a function of the
6995 * parameters and input dimensions, but independently of
6996 * the other output dimensions.
6998 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7000 return map_dim_opt(map, pos, 1);
7003 /* Compute the minimum or maximum of the given set dimension
7004 * as a function of the parameters,
7005 * but independently of the other set dimensions.
7007 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7008 int max)
7010 return map_dim_opt(set, pos, max);
7013 /* Compute the maximum of the given set dimension as a function of the
7014 * parameters, but independently of the other set dimensions.
7016 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7018 return set_dim_opt(set, pos, 1);
7021 /* Compute the minimum of the given set dimension as a function of the
7022 * parameters, but independently of the other set dimensions.
7024 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7026 return set_dim_opt(set, pos, 0);
7029 /* Apply a preimage specified by "mat" on the parameters of "bset".
7030 * bset is assumed to have only parameters and divs.
7032 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7033 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7035 unsigned nparam;
7037 if (!bset || !mat)
7038 goto error;
7040 bset->dim = isl_space_cow(bset->dim);
7041 if (!bset->dim)
7042 goto error;
7044 nparam = isl_basic_set_dim(bset, isl_dim_param);
7046 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7048 bset->dim->nparam = 0;
7049 bset->dim->n_out = nparam;
7050 bset = isl_basic_set_preimage(bset, mat);
7051 if (bset) {
7052 bset->dim->nparam = bset->dim->n_out;
7053 bset->dim->n_out = 0;
7055 return bset;
7056 error:
7057 isl_mat_free(mat);
7058 isl_basic_set_free(bset);
7059 return NULL;
7062 /* Apply a preimage specified by "mat" on the parameters of "set".
7063 * set is assumed to have only parameters and divs.
7065 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7066 __isl_take isl_mat *mat)
7068 isl_space *space;
7069 unsigned nparam;
7071 if (!set || !mat)
7072 goto error;
7074 nparam = isl_set_dim(set, isl_dim_param);
7076 if (mat->n_row != 1 + nparam)
7077 isl_die(isl_set_get_ctx(set), isl_error_internal,
7078 "unexpected number of rows", goto error);
7080 space = isl_set_get_space(set);
7081 space = isl_space_move_dims(space, isl_dim_set, 0,
7082 isl_dim_param, 0, nparam);
7083 set = isl_set_reset_space(set, space);
7084 set = isl_set_preimage(set, mat);
7085 nparam = isl_set_dim(set, isl_dim_out);
7086 space = isl_set_get_space(set);
7087 space = isl_space_move_dims(space, isl_dim_param, 0,
7088 isl_dim_out, 0, nparam);
7089 set = isl_set_reset_space(set, space);
7090 return set;
7091 error:
7092 isl_mat_free(mat);
7093 isl_set_free(set);
7094 return NULL;
7097 /* Intersect the basic set "bset" with the affine space specified by the
7098 * equalities in "eq".
7100 static __isl_give isl_basic_set *basic_set_append_equalities(
7101 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7103 int i, k;
7104 unsigned len;
7106 if (!bset || !eq)
7107 goto error;
7109 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7110 eq->n_row, 0);
7111 if (!bset)
7112 goto error;
7114 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7115 for (i = 0; i < eq->n_row; ++i) {
7116 k = isl_basic_set_alloc_equality(bset);
7117 if (k < 0)
7118 goto error;
7119 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7120 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7122 isl_mat_free(eq);
7124 bset = isl_basic_set_gauss(bset, NULL);
7125 bset = isl_basic_set_finalize(bset);
7127 return bset;
7128 error:
7129 isl_mat_free(eq);
7130 isl_basic_set_free(bset);
7131 return NULL;
7134 /* Intersect the set "set" with the affine space specified by the
7135 * equalities in "eq".
7137 static struct isl_set *set_append_equalities(struct isl_set *set,
7138 struct isl_mat *eq)
7140 int i;
7142 if (!set || !eq)
7143 goto error;
7145 for (i = 0; i < set->n; ++i) {
7146 set->p[i] = basic_set_append_equalities(set->p[i],
7147 isl_mat_copy(eq));
7148 if (!set->p[i])
7149 goto error;
7151 isl_mat_free(eq);
7152 return set;
7153 error:
7154 isl_mat_free(eq);
7155 isl_set_free(set);
7156 return NULL;
7159 /* Given a basic set "bset" that only involves parameters and existentially
7160 * quantified variables, return the index of the first equality
7161 * that only involves parameters. If there is no such equality then
7162 * return bset->n_eq.
7164 * This function assumes that isl_basic_set_gauss has been called on "bset".
7166 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7168 int i, j;
7169 unsigned nparam, n_div;
7171 if (!bset)
7172 return -1;
7174 nparam = isl_basic_set_dim(bset, isl_dim_param);
7175 n_div = isl_basic_set_dim(bset, isl_dim_div);
7177 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7178 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7179 ++i;
7182 return i;
7185 /* Compute an explicit representation for the existentially quantified
7186 * variables in "bset" by computing the "minimal value" of the set
7187 * variables. Since there are no set variables, the computation of
7188 * the minimal value essentially computes an explicit representation
7189 * of the non-empty part(s) of "bset".
7191 * The input only involves parameters and existentially quantified variables.
7192 * All equalities among parameters have been removed.
7194 * Since the existentially quantified variables in the result are in general
7195 * going to be different from those in the input, we first replace
7196 * them by the minimal number of variables based on their equalities.
7197 * This should simplify the parametric integer programming.
7199 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7201 isl_morph *morph1, *morph2;
7202 isl_set *set;
7203 unsigned n;
7205 if (!bset)
7206 return NULL;
7207 if (bset->n_eq == 0)
7208 return isl_basic_set_lexmin_compute_divs(bset);
7210 morph1 = isl_basic_set_parameter_compression(bset);
7211 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7212 bset = isl_basic_set_lift(bset);
7213 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7214 bset = isl_morph_basic_set(morph2, bset);
7215 n = isl_basic_set_dim(bset, isl_dim_set);
7216 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7218 set = isl_basic_set_lexmin_compute_divs(bset);
7220 set = isl_morph_set(isl_morph_inverse(morph1), set);
7222 return set;
7225 /* Project the given basic set onto its parameter domain, possibly introducing
7226 * new, explicit, existential variables in the constraints.
7227 * The input has parameters and (possibly implicit) existential variables.
7228 * The output has the same parameters, but only
7229 * explicit existentially quantified variables.
7231 * The actual projection is performed by pip, but pip doesn't seem
7232 * to like equalities very much, so we first remove the equalities
7233 * among the parameters by performing a variable compression on
7234 * the parameters. Afterward, an inverse transformation is performed
7235 * and the equalities among the parameters are inserted back in.
7237 * The variable compression on the parameters may uncover additional
7238 * equalities that were only implicit before. We therefore check
7239 * if there are any new parameter equalities in the result and
7240 * if so recurse. The removal of parameter equalities is required
7241 * for the parameter compression performed by base_compute_divs.
7243 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7245 int i;
7246 struct isl_mat *eq;
7247 struct isl_mat *T, *T2;
7248 struct isl_set *set;
7249 unsigned nparam;
7251 bset = isl_basic_set_cow(bset);
7252 if (!bset)
7253 return NULL;
7255 if (bset->n_eq == 0)
7256 return base_compute_divs(bset);
7258 bset = isl_basic_set_gauss(bset, NULL);
7259 if (!bset)
7260 return NULL;
7261 if (isl_basic_set_plain_is_empty(bset))
7262 return isl_set_from_basic_set(bset);
7264 i = first_parameter_equality(bset);
7265 if (i == bset->n_eq)
7266 return base_compute_divs(bset);
7268 nparam = isl_basic_set_dim(bset, isl_dim_param);
7269 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7270 0, 1 + nparam);
7271 eq = isl_mat_cow(eq);
7272 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7273 if (T && T->n_col == 0) {
7274 isl_mat_free(T);
7275 isl_mat_free(T2);
7276 isl_mat_free(eq);
7277 bset = isl_basic_set_set_to_empty(bset);
7278 return isl_set_from_basic_set(bset);
7280 bset = basic_set_parameter_preimage(bset, T);
7282 i = first_parameter_equality(bset);
7283 if (!bset)
7284 set = NULL;
7285 else if (i == bset->n_eq)
7286 set = base_compute_divs(bset);
7287 else
7288 set = parameter_compute_divs(bset);
7289 set = set_parameter_preimage(set, T2);
7290 set = set_append_equalities(set, eq);
7291 return set;
7294 /* Insert the divs from "ls" before those of "bmap".
7296 * The number of columns is not changed, which means that the last
7297 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7298 * The caller is responsible for removing the same number of dimensions
7299 * from the space of "bmap".
7301 static __isl_give isl_basic_map *insert_divs_from_local_space(
7302 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7304 int i;
7305 int n_div;
7306 int old_n_div;
7308 n_div = isl_local_space_dim(ls, isl_dim_div);
7309 if (n_div == 0)
7310 return bmap;
7312 old_n_div = bmap->n_div;
7313 bmap = insert_div_rows(bmap, n_div);
7314 if (!bmap)
7315 return NULL;
7317 for (i = 0; i < n_div; ++i) {
7318 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7319 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7322 return bmap;
7325 /* Replace the space of "bmap" by the space and divs of "ls".
7327 * If "ls" has any divs, then we simplify the result since we may
7328 * have discovered some additional equalities that could simplify
7329 * the div expressions.
7331 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7332 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7334 int n_div;
7336 bmap = isl_basic_map_cow(bmap);
7337 if (!bmap || !ls)
7338 goto error;
7340 n_div = isl_local_space_dim(ls, isl_dim_div);
7341 bmap = insert_divs_from_local_space(bmap, ls);
7342 if (!bmap)
7343 goto error;
7345 isl_space_free(bmap->dim);
7346 bmap->dim = isl_local_space_get_space(ls);
7347 if (!bmap->dim)
7348 goto error;
7350 isl_local_space_free(ls);
7351 if (n_div > 0)
7352 bmap = isl_basic_map_simplify(bmap);
7353 bmap = isl_basic_map_finalize(bmap);
7354 return bmap;
7355 error:
7356 isl_basic_map_free(bmap);
7357 isl_local_space_free(ls);
7358 return NULL;
7361 /* Replace the space of "map" by the space and divs of "ls".
7363 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7364 __isl_take isl_local_space *ls)
7366 int i;
7368 map = isl_map_cow(map);
7369 if (!map || !ls)
7370 goto error;
7372 for (i = 0; i < map->n; ++i) {
7373 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7374 isl_local_space_copy(ls));
7375 if (!map->p[i])
7376 goto error;
7378 isl_space_free(map->dim);
7379 map->dim = isl_local_space_get_space(ls);
7380 if (!map->dim)
7381 goto error;
7383 isl_local_space_free(ls);
7384 return map;
7385 error:
7386 isl_local_space_free(ls);
7387 isl_map_free(map);
7388 return NULL;
7391 /* Compute an explicit representation for the existentially
7392 * quantified variables for which do not know any explicit representation yet.
7394 * We first sort the existentially quantified variables so that the
7395 * existentially quantified variables for which we already have an explicit
7396 * representation are placed before those for which we do not.
7397 * The input dimensions, the output dimensions and the existentially
7398 * quantified variables for which we already have an explicit
7399 * representation are then turned into parameters.
7400 * compute_divs returns a map with the same parameters and
7401 * no input or output dimensions and the dimension specification
7402 * is reset to that of the input, including the existentially quantified
7403 * variables for which we already had an explicit representation.
7405 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7407 struct isl_basic_set *bset;
7408 struct isl_set *set;
7409 struct isl_map *map;
7410 isl_space *space;
7411 isl_local_space *ls;
7412 unsigned nparam;
7413 unsigned n_in;
7414 unsigned n_out;
7415 int n_known;
7416 int i;
7418 bmap = isl_basic_map_sort_divs(bmap);
7419 bmap = isl_basic_map_cow(bmap);
7420 if (!bmap)
7421 return NULL;
7423 n_known = isl_basic_map_first_unknown_div(bmap);
7424 if (n_known < 0)
7425 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7427 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7428 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7429 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7430 space = isl_space_set_alloc(bmap->ctx,
7431 nparam + n_in + n_out + n_known, 0);
7432 if (!space)
7433 goto error;
7435 ls = isl_basic_map_get_local_space(bmap);
7436 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7437 n_known, bmap->n_div - n_known);
7438 if (n_known > 0) {
7439 for (i = n_known; i < bmap->n_div; ++i)
7440 swap_div(bmap, i - n_known, i);
7441 bmap->n_div -= n_known;
7442 bmap->extra -= n_known;
7444 bmap = isl_basic_map_reset_space(bmap, space);
7445 bset = bset_from_bmap(bmap);
7447 set = parameter_compute_divs(bset);
7448 map = set_to_map(set);
7449 map = replace_space_by_local_space(map, ls);
7451 return map;
7452 error:
7453 isl_basic_map_free(bmap);
7454 return NULL;
7457 /* Remove the explicit representation of local variable "div",
7458 * if there is any.
7460 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7461 __isl_take isl_basic_map *bmap, int div)
7463 isl_bool unknown;
7465 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7466 if (unknown < 0)
7467 return isl_basic_map_free(bmap);
7468 if (unknown)
7469 return bmap;
7471 bmap = isl_basic_map_cow(bmap);
7472 if (!bmap)
7473 return NULL;
7474 isl_int_set_si(bmap->div[div][0], 0);
7475 return bmap;
7478 /* Is local variable "div" of "bmap" marked as not having an explicit
7479 * representation?
7480 * Note that even if "div" is not marked in this way and therefore
7481 * has an explicit representation, this representation may still
7482 * depend (indirectly) on other local variables that do not
7483 * have an explicit representation.
7485 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7486 int div)
7488 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7489 return isl_bool_error;
7490 return isl_int_is_zero(bmap->div[div][0]);
7493 /* Return the position of the first local variable that does not
7494 * have an explicit representation.
7495 * Return the total number of local variables if they all have
7496 * an explicit representation.
7497 * Return -1 on error.
7499 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7501 int i;
7503 if (!bmap)
7504 return -1;
7506 for (i = 0; i < bmap->n_div; ++i) {
7507 if (!isl_basic_map_div_is_known(bmap, i))
7508 return i;
7510 return bmap->n_div;
7513 /* Return the position of the first local variable that does not
7514 * have an explicit representation.
7515 * Return the total number of local variables if they all have
7516 * an explicit representation.
7517 * Return -1 on error.
7519 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7521 return isl_basic_map_first_unknown_div(bset);
7524 /* Does "bmap" have an explicit representation for all local variables?
7526 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7528 int first, n;
7530 n = isl_basic_map_dim(bmap, isl_dim_div);
7531 first = isl_basic_map_first_unknown_div(bmap);
7532 if (first < 0)
7533 return isl_bool_error;
7534 return first == n;
7537 /* Do all basic maps in "map" have an explicit representation
7538 * for all local variables?
7540 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7542 int i;
7544 if (!map)
7545 return isl_bool_error;
7547 for (i = 0; i < map->n; ++i) {
7548 int known = isl_basic_map_divs_known(map->p[i]);
7549 if (known <= 0)
7550 return known;
7553 return isl_bool_true;
7556 /* If bmap contains any unknown divs, then compute explicit
7557 * expressions for them. However, this computation may be
7558 * quite expensive, so first try to remove divs that aren't
7559 * strictly needed.
7561 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7563 int known;
7564 struct isl_map *map;
7566 known = isl_basic_map_divs_known(bmap);
7567 if (known < 0)
7568 goto error;
7569 if (known)
7570 return isl_map_from_basic_map(bmap);
7572 bmap = isl_basic_map_drop_redundant_divs(bmap);
7574 known = isl_basic_map_divs_known(bmap);
7575 if (known < 0)
7576 goto error;
7577 if (known)
7578 return isl_map_from_basic_map(bmap);
7580 map = compute_divs(bmap);
7581 return map;
7582 error:
7583 isl_basic_map_free(bmap);
7584 return NULL;
7587 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7589 int i;
7590 int known;
7591 struct isl_map *res;
7593 if (!map)
7594 return NULL;
7595 if (map->n == 0)
7596 return map;
7598 known = isl_map_divs_known(map);
7599 if (known < 0) {
7600 isl_map_free(map);
7601 return NULL;
7603 if (known)
7604 return map;
7606 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7607 for (i = 1 ; i < map->n; ++i) {
7608 struct isl_map *r2;
7609 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7610 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7611 res = isl_map_union_disjoint(res, r2);
7612 else
7613 res = isl_map_union(res, r2);
7615 isl_map_free(map);
7617 return res;
7620 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7622 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7625 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7627 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7630 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7632 int i;
7633 struct isl_set *set;
7635 if (!map)
7636 goto error;
7638 map = isl_map_cow(map);
7639 if (!map)
7640 return NULL;
7642 set = set_from_map(map);
7643 set->dim = isl_space_domain(set->dim);
7644 if (!set->dim)
7645 goto error;
7646 for (i = 0; i < map->n; ++i) {
7647 set->p[i] = isl_basic_map_domain(map->p[i]);
7648 if (!set->p[i])
7649 goto error;
7651 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7652 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7653 return set;
7654 error:
7655 isl_map_free(map);
7656 return NULL;
7659 /* Return the union of "map1" and "map2", where we assume for now that
7660 * "map1" and "map2" are disjoint. Note that the basic maps inside
7661 * "map1" or "map2" may not be disjoint from each other.
7662 * Also note that this function is also called from isl_map_union,
7663 * which takes care of handling the situation where "map1" and "map2"
7664 * may not be disjoint.
7666 * If one of the inputs is empty, we can simply return the other input.
7667 * Similarly, if one of the inputs is universal, then it is equal to the union.
7669 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7670 __isl_take isl_map *map2)
7672 int i;
7673 unsigned flags = 0;
7674 struct isl_map *map = NULL;
7675 int is_universe;
7677 if (!map1 || !map2)
7678 goto error;
7680 if (!isl_space_is_equal(map1->dim, map2->dim))
7681 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7682 "spaces don't match", goto error);
7684 if (map1->n == 0) {
7685 isl_map_free(map1);
7686 return map2;
7688 if (map2->n == 0) {
7689 isl_map_free(map2);
7690 return map1;
7693 is_universe = isl_map_plain_is_universe(map1);
7694 if (is_universe < 0)
7695 goto error;
7696 if (is_universe) {
7697 isl_map_free(map2);
7698 return map1;
7701 is_universe = isl_map_plain_is_universe(map2);
7702 if (is_universe < 0)
7703 goto error;
7704 if (is_universe) {
7705 isl_map_free(map1);
7706 return map2;
7709 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7710 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7711 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7713 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7714 map1->n + map2->n, flags);
7715 if (!map)
7716 goto error;
7717 for (i = 0; i < map1->n; ++i) {
7718 map = isl_map_add_basic_map(map,
7719 isl_basic_map_copy(map1->p[i]));
7720 if (!map)
7721 goto error;
7723 for (i = 0; i < map2->n; ++i) {
7724 map = isl_map_add_basic_map(map,
7725 isl_basic_map_copy(map2->p[i]));
7726 if (!map)
7727 goto error;
7729 isl_map_free(map1);
7730 isl_map_free(map2);
7731 return map;
7732 error:
7733 isl_map_free(map);
7734 isl_map_free(map1);
7735 isl_map_free(map2);
7736 return NULL;
7739 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7740 * guaranteed to be disjoint by the caller.
7742 * Note that this functions is called from within isl_map_make_disjoint,
7743 * so we have to be careful not to touch the constraints of the inputs
7744 * in any way.
7746 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7747 __isl_take isl_map *map2)
7749 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7752 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7753 * not be disjoint. The parameters are assumed to have been aligned.
7755 * We currently simply call map_union_disjoint, the internal operation
7756 * of which does not really depend on the inputs being disjoint.
7757 * If the result contains more than one basic map, then we clear
7758 * the disjoint flag since the result may contain basic maps from
7759 * both inputs and these are not guaranteed to be disjoint.
7761 * As a special case, if "map1" and "map2" are obviously equal,
7762 * then we simply return "map1".
7764 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7765 __isl_take isl_map *map2)
7767 int equal;
7769 if (!map1 || !map2)
7770 goto error;
7772 equal = isl_map_plain_is_equal(map1, map2);
7773 if (equal < 0)
7774 goto error;
7775 if (equal) {
7776 isl_map_free(map2);
7777 return map1;
7780 map1 = map_union_disjoint(map1, map2);
7781 if (!map1)
7782 return NULL;
7783 if (map1->n > 1)
7784 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7785 return map1;
7786 error:
7787 isl_map_free(map1);
7788 isl_map_free(map2);
7789 return NULL;
7792 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7793 * not be disjoint.
7795 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7796 __isl_take isl_map *map2)
7798 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7801 __isl_give isl_set *isl_set_union_disjoint(
7802 __isl_take isl_set *set1, __isl_take isl_set *set2)
7804 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7805 set_to_map(set2)));
7808 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7810 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7813 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7814 * the results.
7816 * "map" and "set" are assumed to be compatible and non-NULL.
7818 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7819 __isl_take isl_set *set,
7820 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7821 __isl_take isl_basic_set *bset))
7823 unsigned flags = 0;
7824 struct isl_map *result;
7825 int i, j;
7827 if (isl_set_plain_is_universe(set)) {
7828 isl_set_free(set);
7829 return map;
7832 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7833 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7834 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7836 result = isl_map_alloc_space(isl_space_copy(map->dim),
7837 map->n * set->n, flags);
7838 for (i = 0; result && i < map->n; ++i)
7839 for (j = 0; j < set->n; ++j) {
7840 result = isl_map_add_basic_map(result,
7841 fn(isl_basic_map_copy(map->p[i]),
7842 isl_basic_set_copy(set->p[j])));
7843 if (!result)
7844 break;
7847 isl_map_free(map);
7848 isl_set_free(set);
7849 return result;
7852 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7853 __isl_take isl_set *set)
7855 isl_bool ok;
7857 ok = isl_map_compatible_range(map, set);
7858 if (ok < 0)
7859 goto error;
7860 if (!ok)
7861 isl_die(set->ctx, isl_error_invalid,
7862 "incompatible spaces", goto error);
7864 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7865 error:
7866 isl_map_free(map);
7867 isl_set_free(set);
7868 return NULL;
7871 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7872 __isl_take isl_set *set)
7874 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7877 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7878 __isl_take isl_set *set)
7880 isl_bool ok;
7882 ok = isl_map_compatible_domain(map, set);
7883 if (ok < 0)
7884 goto error;
7885 if (!ok)
7886 isl_die(set->ctx, isl_error_invalid,
7887 "incompatible spaces", goto error);
7889 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7890 error:
7891 isl_map_free(map);
7892 isl_set_free(set);
7893 return NULL;
7896 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7897 __isl_take isl_set *set)
7899 return isl_map_align_params_map_map_and(map, set,
7900 &map_intersect_domain);
7903 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7904 * in the space B -> C, return the intersection.
7905 * The parameters are assumed to have been aligned.
7907 * The map "factor" is first extended to a map living in the space
7908 * [A -> B] -> C and then a regular intersection is computed.
7910 static __isl_give isl_map *map_intersect_domain_factor_range(
7911 __isl_take isl_map *map, __isl_take isl_map *factor)
7913 isl_space *space;
7914 isl_map *ext_factor;
7916 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7917 ext_factor = isl_map_universe(space);
7918 ext_factor = isl_map_domain_product(ext_factor, factor);
7919 return map_intersect(map, ext_factor);
7922 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7923 * in the space B -> C, return the intersection.
7925 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7926 __isl_take isl_map *map, __isl_take isl_map *factor)
7928 return isl_map_align_params_map_map_and(map, factor,
7929 &map_intersect_domain_factor_range);
7932 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7933 * in the space A -> C, return the intersection.
7935 * The map "factor" is first extended to a map living in the space
7936 * A -> [B -> C] and then a regular intersection is computed.
7938 static __isl_give isl_map *map_intersect_range_factor_range(
7939 __isl_take isl_map *map, __isl_take isl_map *factor)
7941 isl_space *space;
7942 isl_map *ext_factor;
7944 space = isl_space_range_factor_domain(isl_map_get_space(map));
7945 ext_factor = isl_map_universe(space);
7946 ext_factor = isl_map_range_product(ext_factor, factor);
7947 return isl_map_intersect(map, ext_factor);
7950 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7951 * in the space A -> C, return the intersection.
7953 __isl_give isl_map *isl_map_intersect_range_factor_range(
7954 __isl_take isl_map *map, __isl_take isl_map *factor)
7956 return isl_map_align_params_map_map_and(map, factor,
7957 &map_intersect_range_factor_range);
7960 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7961 __isl_take isl_map *map2)
7963 if (!map1 || !map2)
7964 goto error;
7965 map1 = isl_map_reverse(map1);
7966 map1 = isl_map_apply_range(map1, map2);
7967 return isl_map_reverse(map1);
7968 error:
7969 isl_map_free(map1);
7970 isl_map_free(map2);
7971 return NULL;
7974 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7975 __isl_take isl_map *map2)
7977 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7980 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7981 __isl_take isl_map *map2)
7983 isl_space *space;
7984 struct isl_map *result;
7985 int i, j;
7987 if (!map1 || !map2)
7988 goto error;
7990 space = isl_space_join(isl_space_copy(map1->dim),
7991 isl_space_copy(map2->dim));
7993 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7994 if (!result)
7995 goto error;
7996 for (i = 0; i < map1->n; ++i)
7997 for (j = 0; j < map2->n; ++j) {
7998 result = isl_map_add_basic_map(result,
7999 isl_basic_map_apply_range(
8000 isl_basic_map_copy(map1->p[i]),
8001 isl_basic_map_copy(map2->p[j])));
8002 if (!result)
8003 goto error;
8005 isl_map_free(map1);
8006 isl_map_free(map2);
8007 if (result && result->n <= 1)
8008 ISL_F_SET(result, ISL_MAP_DISJOINT);
8009 return result;
8010 error:
8011 isl_map_free(map1);
8012 isl_map_free(map2);
8013 return NULL;
8016 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8017 __isl_take isl_map *map2)
8019 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8023 * returns range - domain
8025 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8027 isl_space *target_space;
8028 struct isl_basic_set *bset;
8029 unsigned dim;
8030 unsigned nparam;
8031 int i;
8033 if (!bmap)
8034 goto error;
8035 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8036 bmap->dim, isl_dim_out),
8037 goto error);
8038 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8039 dim = isl_basic_map_dim(bmap, isl_dim_in);
8040 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8041 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8042 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8043 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8044 for (i = 0; i < dim; ++i) {
8045 int j = isl_basic_map_alloc_equality(bmap);
8046 if (j < 0) {
8047 bmap = isl_basic_map_free(bmap);
8048 break;
8050 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8051 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8052 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8053 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8055 bset = isl_basic_map_domain(bmap);
8056 bset = isl_basic_set_reset_space(bset, target_space);
8057 return bset;
8058 error:
8059 isl_basic_map_free(bmap);
8060 return NULL;
8064 * returns range - domain
8066 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8068 int i;
8069 isl_space *dim;
8070 struct isl_set *result;
8072 if (!map)
8073 return NULL;
8075 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8076 map->dim, isl_dim_out),
8077 goto error);
8078 dim = isl_map_get_space(map);
8079 dim = isl_space_domain(dim);
8080 result = isl_set_alloc_space(dim, map->n, 0);
8081 if (!result)
8082 goto error;
8083 for (i = 0; i < map->n; ++i)
8084 result = isl_set_add_basic_set(result,
8085 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8086 isl_map_free(map);
8087 return result;
8088 error:
8089 isl_map_free(map);
8090 return NULL;
8094 * returns [domain -> range] -> range - domain
8096 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8097 __isl_take isl_basic_map *bmap)
8099 int i, k;
8100 isl_space *space;
8101 isl_basic_map *domain;
8102 int nparam, n;
8103 unsigned total;
8105 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8106 bmap->dim, isl_dim_out))
8107 isl_die(bmap->ctx, isl_error_invalid,
8108 "domain and range don't match", goto error);
8110 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8111 n = isl_basic_map_dim(bmap, isl_dim_in);
8113 space = isl_basic_map_get_space(bmap);
8114 space = isl_space_from_range(isl_space_domain(space));
8115 domain = isl_basic_map_universe(space);
8117 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8118 bmap = isl_basic_map_apply_range(bmap, domain);
8119 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8121 total = isl_basic_map_total_dim(bmap);
8123 for (i = 0; i < n; ++i) {
8124 k = isl_basic_map_alloc_equality(bmap);
8125 if (k < 0)
8126 goto error;
8127 isl_seq_clr(bmap->eq[k], 1 + total);
8128 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8129 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8130 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8133 bmap = isl_basic_map_gauss(bmap, NULL);
8134 return isl_basic_map_finalize(bmap);
8135 error:
8136 isl_basic_map_free(bmap);
8137 return NULL;
8141 * returns [domain -> range] -> range - domain
8143 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8145 int i;
8146 isl_space *domain_space;
8148 if (!map)
8149 return NULL;
8151 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8152 map->dim, isl_dim_out))
8153 isl_die(map->ctx, isl_error_invalid,
8154 "domain and range don't match", goto error);
8156 map = isl_map_cow(map);
8157 if (!map)
8158 return NULL;
8160 domain_space = isl_space_domain(isl_map_get_space(map));
8161 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8162 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8163 if (!map->dim)
8164 goto error;
8165 for (i = 0; i < map->n; ++i) {
8166 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8167 if (!map->p[i])
8168 goto error;
8170 map = isl_map_unmark_normalized(map);
8171 return map;
8172 error:
8173 isl_map_free(map);
8174 return NULL;
8177 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8179 unsigned n_in, n_out;
8181 if (!space)
8182 return NULL;
8183 n_in = isl_space_dim(space, isl_dim_in);
8184 n_out = isl_space_dim(space, isl_dim_out);
8185 if (n_in != n_out)
8186 isl_die(space->ctx, isl_error_invalid,
8187 "number of input and output dimensions needs to be "
8188 "the same", goto error);
8189 return isl_basic_map_equal(space, n_in);
8190 error:
8191 isl_space_free(space);
8192 return NULL;
8195 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8197 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8200 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8202 isl_space *dim = isl_set_get_space(set);
8203 isl_map *id;
8204 id = isl_map_identity(isl_space_map_from_set(dim));
8205 return isl_map_intersect_range(id, set);
8208 /* Construct a basic set with all set dimensions having only non-negative
8209 * values.
8211 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8212 __isl_take isl_space *space)
8214 int i;
8215 unsigned nparam;
8216 unsigned dim;
8217 struct isl_basic_set *bset;
8219 if (!space)
8220 return NULL;
8221 nparam = space->nparam;
8222 dim = space->n_out;
8223 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8224 if (!bset)
8225 return NULL;
8226 for (i = 0; i < dim; ++i) {
8227 int k = isl_basic_set_alloc_inequality(bset);
8228 if (k < 0)
8229 goto error;
8230 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8231 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8233 return bset;
8234 error:
8235 isl_basic_set_free(bset);
8236 return NULL;
8239 /* Construct the half-space x_pos >= 0.
8241 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8242 int pos)
8244 int k;
8245 isl_basic_set *nonneg;
8247 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8248 k = isl_basic_set_alloc_inequality(nonneg);
8249 if (k < 0)
8250 goto error;
8251 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8252 isl_int_set_si(nonneg->ineq[k][pos], 1);
8254 return isl_basic_set_finalize(nonneg);
8255 error:
8256 isl_basic_set_free(nonneg);
8257 return NULL;
8260 /* Construct the half-space x_pos <= -1.
8262 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8263 int pos)
8265 int k;
8266 isl_basic_set *neg;
8268 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8269 k = isl_basic_set_alloc_inequality(neg);
8270 if (k < 0)
8271 goto error;
8272 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8273 isl_int_set_si(neg->ineq[k][0], -1);
8274 isl_int_set_si(neg->ineq[k][pos], -1);
8276 return isl_basic_set_finalize(neg);
8277 error:
8278 isl_basic_set_free(neg);
8279 return NULL;
8282 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8283 enum isl_dim_type type, unsigned first, unsigned n)
8285 int i;
8286 unsigned offset;
8287 isl_basic_set *nonneg;
8288 isl_basic_set *neg;
8290 if (!set)
8291 return NULL;
8292 if (n == 0)
8293 return set;
8295 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8297 offset = pos(set->dim, type);
8298 for (i = 0; i < n; ++i) {
8299 nonneg = nonneg_halfspace(isl_set_get_space(set),
8300 offset + first + i);
8301 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8303 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8306 return set;
8307 error:
8308 isl_set_free(set);
8309 return NULL;
8312 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8313 int len,
8314 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8315 void *user)
8317 isl_set *half;
8319 if (!set)
8320 return isl_stat_error;
8321 if (isl_set_plain_is_empty(set)) {
8322 isl_set_free(set);
8323 return isl_stat_ok;
8325 if (first == len)
8326 return fn(set, signs, user);
8328 signs[first] = 1;
8329 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8330 1 + first));
8331 half = isl_set_intersect(half, isl_set_copy(set));
8332 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8333 goto error;
8335 signs[first] = -1;
8336 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8337 1 + first));
8338 half = isl_set_intersect(half, set);
8339 return foreach_orthant(half, signs, first + 1, len, fn, user);
8340 error:
8341 isl_set_free(set);
8342 return isl_stat_error;
8345 /* Call "fn" on the intersections of "set" with each of the orthants
8346 * (except for obviously empty intersections). The orthant is identified
8347 * by the signs array, with each entry having value 1 or -1 according
8348 * to the sign of the corresponding variable.
8350 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8351 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8352 void *user)
8354 unsigned nparam;
8355 unsigned nvar;
8356 int *signs;
8357 isl_stat r;
8359 if (!set)
8360 return isl_stat_error;
8361 if (isl_set_plain_is_empty(set))
8362 return isl_stat_ok;
8364 nparam = isl_set_dim(set, isl_dim_param);
8365 nvar = isl_set_dim(set, isl_dim_set);
8367 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8369 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8370 fn, user);
8372 free(signs);
8374 return r;
8377 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8379 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8382 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8383 __isl_keep isl_basic_map *bmap2)
8385 isl_bool is_subset;
8386 struct isl_map *map1;
8387 struct isl_map *map2;
8389 if (!bmap1 || !bmap2)
8390 return isl_bool_error;
8392 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8393 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8395 is_subset = isl_map_is_subset(map1, map2);
8397 isl_map_free(map1);
8398 isl_map_free(map2);
8400 return is_subset;
8403 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8404 __isl_keep isl_basic_set *bset2)
8406 return isl_basic_map_is_subset(bset1, bset2);
8409 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8410 __isl_keep isl_basic_map *bmap2)
8412 isl_bool is_subset;
8414 if (!bmap1 || !bmap2)
8415 return isl_bool_error;
8416 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8417 if (is_subset != isl_bool_true)
8418 return is_subset;
8419 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8420 return is_subset;
8423 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8424 __isl_keep isl_basic_set *bset2)
8426 return isl_basic_map_is_equal(
8427 bset_to_bmap(bset1), bset_to_bmap(bset2));
8430 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8432 int i;
8433 int is_empty;
8435 if (!map)
8436 return isl_bool_error;
8437 for (i = 0; i < map->n; ++i) {
8438 is_empty = isl_basic_map_is_empty(map->p[i]);
8439 if (is_empty < 0)
8440 return isl_bool_error;
8441 if (!is_empty)
8442 return isl_bool_false;
8444 return isl_bool_true;
8447 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8449 return map ? map->n == 0 : isl_bool_error;
8452 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8454 return set ? set->n == 0 : isl_bool_error;
8457 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8459 return isl_map_is_empty(set_to_map(set));
8462 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8463 __isl_keep isl_map *map2)
8465 if (!map1 || !map2)
8466 return isl_bool_error;
8468 return isl_space_is_equal(map1->dim, map2->dim);
8471 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8472 __isl_keep isl_set *set2)
8474 if (!set1 || !set2)
8475 return isl_bool_error;
8477 return isl_space_is_equal(set1->dim, set2->dim);
8480 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8482 isl_bool is_subset;
8484 if (!map1 || !map2)
8485 return isl_bool_error;
8486 is_subset = isl_map_is_subset(map1, map2);
8487 if (is_subset != isl_bool_true)
8488 return is_subset;
8489 is_subset = isl_map_is_subset(map2, map1);
8490 return is_subset;
8493 /* Is "map1" equal to "map2"?
8495 * First check if they are obviously equal.
8496 * If not, then perform a more detailed analysis.
8498 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8500 isl_bool equal;
8502 equal = isl_map_plain_is_equal(map1, map2);
8503 if (equal < 0 || equal)
8504 return equal;
8505 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8508 isl_bool isl_basic_map_is_strict_subset(
8509 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8511 isl_bool is_subset;
8513 if (!bmap1 || !bmap2)
8514 return isl_bool_error;
8515 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8516 if (is_subset != isl_bool_true)
8517 return is_subset;
8518 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8519 return isl_bool_not(is_subset);
8522 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8523 __isl_keep isl_map *map2)
8525 isl_bool is_subset;
8527 if (!map1 || !map2)
8528 return isl_bool_error;
8529 is_subset = isl_map_is_subset(map1, map2);
8530 if (is_subset != isl_bool_true)
8531 return is_subset;
8532 is_subset = isl_map_is_subset(map2, map1);
8533 return isl_bool_not(is_subset);
8536 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8537 __isl_keep isl_set *set2)
8539 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8542 /* Is "bmap" obviously equal to the universe with the same space?
8544 * That is, does it not have any constraints?
8546 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8548 if (!bmap)
8549 return isl_bool_error;
8550 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8553 /* Is "bset" obviously equal to the universe with the same space?
8555 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8557 return isl_basic_map_plain_is_universe(bset);
8560 /* If "c" does not involve any existentially quantified variables,
8561 * then set *univ to false and abort
8563 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8565 isl_bool *univ = user;
8566 unsigned n;
8568 n = isl_constraint_dim(c, isl_dim_div);
8569 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8570 isl_constraint_free(c);
8571 if (*univ < 0 || !*univ)
8572 return isl_stat_error;
8573 return isl_stat_ok;
8576 /* Is "bmap" equal to the universe with the same space?
8578 * First check if it is obviously equal to the universe.
8579 * If not and if there are any constraints not involving
8580 * existentially quantified variables, then it is certainly
8581 * not equal to the universe.
8582 * Otherwise, check if the universe is a subset of "bmap".
8584 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8586 isl_bool univ;
8587 isl_basic_map *test;
8589 univ = isl_basic_map_plain_is_universe(bmap);
8590 if (univ < 0 || univ)
8591 return univ;
8592 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8593 return isl_bool_false;
8594 univ = isl_bool_true;
8595 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8596 univ)
8597 return isl_bool_error;
8598 if (univ < 0 || !univ)
8599 return univ;
8600 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8601 univ = isl_basic_map_is_subset(test, bmap);
8602 isl_basic_map_free(test);
8603 return univ;
8606 /* Is "bset" equal to the universe with the same space?
8608 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8610 return isl_basic_map_is_universe(bset);
8613 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8615 int i;
8617 if (!map)
8618 return isl_bool_error;
8620 for (i = 0; i < map->n; ++i) {
8621 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8622 if (r < 0 || r)
8623 return r;
8626 return isl_bool_false;
8629 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8631 return isl_map_plain_is_universe(set_to_map(set));
8634 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8636 struct isl_basic_set *bset = NULL;
8637 struct isl_vec *sample = NULL;
8638 isl_bool empty, non_empty;
8640 if (!bmap)
8641 return isl_bool_error;
8643 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8644 return isl_bool_true;
8646 if (isl_basic_map_plain_is_universe(bmap))
8647 return isl_bool_false;
8649 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8650 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8651 copy = isl_basic_map_remove_redundancies(copy);
8652 empty = isl_basic_map_plain_is_empty(copy);
8653 isl_basic_map_free(copy);
8654 return empty;
8657 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8658 if (non_empty < 0)
8659 return isl_bool_error;
8660 if (non_empty)
8661 return isl_bool_false;
8662 isl_vec_free(bmap->sample);
8663 bmap->sample = NULL;
8664 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8665 if (!bset)
8666 return isl_bool_error;
8667 sample = isl_basic_set_sample_vec(bset);
8668 if (!sample)
8669 return isl_bool_error;
8670 empty = sample->size == 0;
8671 isl_vec_free(bmap->sample);
8672 bmap->sample = sample;
8673 if (empty)
8674 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8676 return empty;
8679 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8681 if (!bmap)
8682 return isl_bool_error;
8683 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8686 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8688 if (!bset)
8689 return isl_bool_error;
8690 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8693 /* Is "bmap" known to be non-empty?
8695 * That is, is the cached sample still valid?
8697 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8699 unsigned total;
8701 if (!bmap)
8702 return isl_bool_error;
8703 if (!bmap->sample)
8704 return isl_bool_false;
8705 total = 1 + isl_basic_map_total_dim(bmap);
8706 if (bmap->sample->size != total)
8707 return isl_bool_false;
8708 return isl_basic_map_contains(bmap, bmap->sample);
8711 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8713 return isl_basic_map_is_empty(bset_to_bmap(bset));
8716 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8717 __isl_take isl_basic_map *bmap2)
8719 struct isl_map *map;
8720 if (!bmap1 || !bmap2)
8721 goto error;
8723 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8725 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8726 if (!map)
8727 goto error;
8728 map = isl_map_add_basic_map(map, bmap1);
8729 map = isl_map_add_basic_map(map, bmap2);
8730 return map;
8731 error:
8732 isl_basic_map_free(bmap1);
8733 isl_basic_map_free(bmap2);
8734 return NULL;
8737 struct isl_set *isl_basic_set_union(
8738 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8740 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8741 bset_to_bmap(bset2)));
8744 /* Order divs such that any div only depends on previous divs */
8745 __isl_give isl_basic_map *isl_basic_map_order_divs(
8746 __isl_take isl_basic_map *bmap)
8748 int i;
8749 unsigned off;
8751 if (!bmap)
8752 return NULL;
8754 off = isl_space_dim(bmap->dim, isl_dim_all);
8756 for (i = 0; i < bmap->n_div; ++i) {
8757 int pos;
8758 if (isl_int_is_zero(bmap->div[i][0]))
8759 continue;
8760 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8761 bmap->n_div-i);
8762 if (pos == -1)
8763 continue;
8764 if (pos == 0)
8765 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8766 "integer division depends on itself",
8767 return isl_basic_map_free(bmap));
8768 isl_basic_map_swap_div(bmap, i, i + pos);
8769 --i;
8771 return bmap;
8774 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8776 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8779 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8781 int i;
8783 if (!map)
8784 return 0;
8786 for (i = 0; i < map->n; ++i) {
8787 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8788 if (!map->p[i])
8789 goto error;
8792 return map;
8793 error:
8794 isl_map_free(map);
8795 return NULL;
8798 /* Sort the local variables of "bset".
8800 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8801 __isl_take isl_basic_set *bset)
8803 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8806 /* Apply the expansion computed by isl_merge_divs.
8807 * The expansion itself is given by "exp" while the resulting
8808 * list of divs is given by "div".
8810 * Move the integer divisions of "bmap" into the right position
8811 * according to "exp" and then introduce the additional integer
8812 * divisions, adding div constraints.
8813 * The moving should be done first to avoid moving coefficients
8814 * in the definitions of the extra integer divisions.
8816 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8817 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8819 int i, j;
8820 int n_div;
8822 bmap = isl_basic_map_cow(bmap);
8823 if (!bmap || !div)
8824 goto error;
8826 if (div->n_row < bmap->n_div)
8827 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8828 "not an expansion", goto error);
8830 n_div = bmap->n_div;
8831 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8832 div->n_row - n_div, 0,
8833 2 * (div->n_row - n_div));
8835 for (i = n_div; i < div->n_row; ++i)
8836 if (isl_basic_map_alloc_div(bmap) < 0)
8837 goto error;
8839 for (j = n_div - 1; j >= 0; --j) {
8840 if (exp[j] == j)
8841 break;
8842 isl_basic_map_swap_div(bmap, j, exp[j]);
8844 j = 0;
8845 for (i = 0; i < div->n_row; ++i) {
8846 if (j < n_div && exp[j] == i) {
8847 j++;
8848 } else {
8849 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8850 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8851 continue;
8852 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8853 goto error;
8857 isl_mat_free(div);
8858 return bmap;
8859 error:
8860 isl_basic_map_free(bmap);
8861 isl_mat_free(div);
8862 return NULL;
8865 /* Apply the expansion computed by isl_merge_divs.
8866 * The expansion itself is given by "exp" while the resulting
8867 * list of divs is given by "div".
8869 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8870 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8872 return isl_basic_map_expand_divs(bset, div, exp);
8875 /* Look for a div in dst that corresponds to the div "div" in src.
8876 * The divs before "div" in src and dst are assumed to be the same.
8878 * Returns -1 if no corresponding div was found and the position
8879 * of the corresponding div in dst otherwise.
8881 static int find_div(__isl_keep isl_basic_map *dst,
8882 __isl_keep isl_basic_map *src, unsigned div)
8884 int i;
8886 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8888 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8889 for (i = div; i < dst->n_div; ++i)
8890 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8891 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8892 dst->n_div - div) == -1)
8893 return i;
8894 return -1;
8897 /* Align the divs of "dst" to those of "src", adding divs from "src"
8898 * if needed. That is, make sure that the first src->n_div divs
8899 * of the result are equal to those of src.
8901 * The result is not finalized as by design it will have redundant
8902 * divs if any divs from "src" were copied.
8904 __isl_give isl_basic_map *isl_basic_map_align_divs(
8905 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8907 int i;
8908 isl_bool known;
8909 int extended;
8910 unsigned total;
8912 if (!dst || !src)
8913 return isl_basic_map_free(dst);
8915 if (src->n_div == 0)
8916 return dst;
8918 known = isl_basic_map_divs_known(src);
8919 if (known < 0)
8920 return isl_basic_map_free(dst);
8921 if (!known)
8922 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8923 "some src divs are unknown",
8924 return isl_basic_map_free(dst));
8926 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8927 if (!src)
8928 return isl_basic_map_free(dst);
8930 extended = 0;
8931 total = isl_space_dim(src->dim, isl_dim_all);
8932 for (i = 0; i < src->n_div; ++i) {
8933 int j = find_div(dst, src, i);
8934 if (j < 0) {
8935 if (!extended) {
8936 int extra = src->n_div - i;
8937 dst = isl_basic_map_cow(dst);
8938 if (!dst)
8939 goto error;
8940 dst = isl_basic_map_extend_space(dst,
8941 isl_space_copy(dst->dim),
8942 extra, 0, 2 * extra);
8943 extended = 1;
8945 j = isl_basic_map_alloc_div(dst);
8946 if (j < 0)
8947 goto error;
8948 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8949 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8950 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8951 goto error;
8953 if (j != i)
8954 isl_basic_map_swap_div(dst, i, j);
8956 isl_basic_map_free(src);
8957 return dst;
8958 error:
8959 isl_basic_map_free(src);
8960 isl_basic_map_free(dst);
8961 return NULL;
8964 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8966 int i;
8968 if (!map)
8969 return NULL;
8970 if (map->n == 0)
8971 return map;
8972 map = isl_map_compute_divs(map);
8973 map = isl_map_cow(map);
8974 if (!map)
8975 return NULL;
8977 for (i = 1; i < map->n; ++i)
8978 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8979 for (i = 1; i < map->n; ++i) {
8980 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8981 if (!map->p[i])
8982 return isl_map_free(map);
8985 map = isl_map_unmark_normalized(map);
8986 return map;
8989 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8991 return isl_map_align_divs_internal(map);
8994 struct isl_set *isl_set_align_divs(struct isl_set *set)
8996 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8999 /* Align the divs of the basic maps in "map" to those
9000 * of the basic maps in "list", as well as to the other basic maps in "map".
9001 * The elements in "list" are assumed to have known divs.
9003 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9004 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9006 int i, n;
9008 map = isl_map_compute_divs(map);
9009 map = isl_map_cow(map);
9010 if (!map || !list)
9011 return isl_map_free(map);
9012 if (map->n == 0)
9013 return map;
9015 n = isl_basic_map_list_n_basic_map(list);
9016 for (i = 0; i < n; ++i) {
9017 isl_basic_map *bmap;
9019 bmap = isl_basic_map_list_get_basic_map(list, i);
9020 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9021 isl_basic_map_free(bmap);
9023 if (!map->p[0])
9024 return isl_map_free(map);
9026 return isl_map_align_divs_internal(map);
9029 /* Align the divs of each element of "list" to those of "bmap".
9030 * Both "bmap" and the elements of "list" are assumed to have known divs.
9032 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9033 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9035 int i, n;
9037 if (!list || !bmap)
9038 return isl_basic_map_list_free(list);
9040 n = isl_basic_map_list_n_basic_map(list);
9041 for (i = 0; i < n; ++i) {
9042 isl_basic_map *bmap_i;
9044 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9045 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9046 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9049 return list;
9052 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9053 __isl_take isl_map *map)
9055 isl_bool ok;
9057 ok = isl_map_compatible_domain(map, set);
9058 if (ok < 0)
9059 goto error;
9060 if (!ok)
9061 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9062 "incompatible spaces", goto error);
9063 map = isl_map_intersect_domain(map, set);
9064 set = isl_map_range(map);
9065 return set;
9066 error:
9067 isl_set_free(set);
9068 isl_map_free(map);
9069 return NULL;
9072 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9073 __isl_take isl_map *map)
9075 return isl_map_align_params_map_map_and(set, map, &set_apply);
9078 /* There is no need to cow as removing empty parts doesn't change
9079 * the meaning of the set.
9081 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9083 int i;
9085 if (!map)
9086 return NULL;
9088 for (i = map->n - 1; i >= 0; --i)
9089 map = remove_if_empty(map, i);
9091 return map;
9094 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9096 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9099 /* Create a binary relation that maps the shared initial "pos" dimensions
9100 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9102 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9103 __isl_keep isl_basic_set *bset2, int pos)
9105 isl_basic_map *bmap1;
9106 isl_basic_map *bmap2;
9108 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9109 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9110 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9111 isl_dim_out, 0, pos);
9112 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9113 isl_dim_out, 0, pos);
9114 return isl_basic_map_range_product(bmap1, bmap2);
9117 /* Given two basic sets bset1 and bset2, compute the maximal difference
9118 * between the values of dimension pos in bset1 and those in bset2
9119 * for any common value of the parameters and dimensions preceding pos.
9121 static enum isl_lp_result basic_set_maximal_difference_at(
9122 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9123 int pos, isl_int *opt)
9125 isl_basic_map *bmap1;
9126 struct isl_ctx *ctx;
9127 struct isl_vec *obj;
9128 unsigned total;
9129 unsigned nparam;
9130 unsigned dim1;
9131 enum isl_lp_result res;
9133 if (!bset1 || !bset2)
9134 return isl_lp_error;
9136 nparam = isl_basic_set_n_param(bset1);
9137 dim1 = isl_basic_set_n_dim(bset1);
9139 bmap1 = join_initial(bset1, bset2, pos);
9140 if (!bmap1)
9141 return isl_lp_error;
9143 total = isl_basic_map_total_dim(bmap1);
9144 ctx = bmap1->ctx;
9145 obj = isl_vec_alloc(ctx, 1 + total);
9146 if (!obj)
9147 goto error;
9148 isl_seq_clr(obj->block.data, 1 + total);
9149 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9150 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9151 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9152 opt, NULL, NULL);
9153 isl_basic_map_free(bmap1);
9154 isl_vec_free(obj);
9155 return res;
9156 error:
9157 isl_basic_map_free(bmap1);
9158 return isl_lp_error;
9161 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9162 * for any common value of the parameters and dimensions preceding pos
9163 * in both basic sets, the values of dimension pos in bset1 are
9164 * smaller or larger than those in bset2.
9166 * Returns
9167 * 1 if bset1 follows bset2
9168 * -1 if bset1 precedes bset2
9169 * 0 if bset1 and bset2 are incomparable
9170 * -2 if some error occurred.
9172 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9173 __isl_keep isl_basic_set *bset2, int pos)
9175 isl_int opt;
9176 enum isl_lp_result res;
9177 int cmp;
9179 isl_int_init(opt);
9181 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9183 if (res == isl_lp_empty)
9184 cmp = 0;
9185 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9186 res == isl_lp_unbounded)
9187 cmp = 1;
9188 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9189 cmp = -1;
9190 else
9191 cmp = -2;
9193 isl_int_clear(opt);
9194 return cmp;
9197 /* Given two basic sets bset1 and bset2, check whether
9198 * for any common value of the parameters and dimensions preceding pos
9199 * there is a value of dimension pos in bset1 that is larger
9200 * than a value of the same dimension in bset2.
9202 * Return
9203 * 1 if there exists such a pair
9204 * 0 if there is no such pair, but there is a pair of equal values
9205 * -1 otherwise
9206 * -2 if some error occurred.
9208 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9209 __isl_keep isl_basic_set *bset2, int pos)
9211 isl_bool empty;
9212 isl_basic_map *bmap;
9213 unsigned dim1;
9215 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9216 bmap = join_initial(bset1, bset2, pos);
9217 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9218 isl_dim_out, dim1 - pos);
9219 empty = isl_basic_map_is_empty(bmap);
9220 if (empty < 0)
9221 goto error;
9222 if (empty) {
9223 isl_basic_map_free(bmap);
9224 return -1;
9226 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9227 isl_dim_out, dim1 - pos);
9228 empty = isl_basic_map_is_empty(bmap);
9229 if (empty < 0)
9230 goto error;
9231 isl_basic_map_free(bmap);
9232 if (empty)
9233 return 0;
9234 return 1;
9235 error:
9236 isl_basic_map_free(bmap);
9237 return -2;
9240 /* Given two sets set1 and set2, check whether
9241 * for any common value of the parameters and dimensions preceding pos
9242 * there is a value of dimension pos in set1 that is larger
9243 * than a value of the same dimension in set2.
9245 * Return
9246 * 1 if there exists such a pair
9247 * 0 if there is no such pair, but there is a pair of equal values
9248 * -1 otherwise
9249 * -2 if some error occurred.
9251 int isl_set_follows_at(__isl_keep isl_set *set1,
9252 __isl_keep isl_set *set2, int pos)
9254 int i, j;
9255 int follows = -1;
9257 if (!set1 || !set2)
9258 return -2;
9260 for (i = 0; i < set1->n; ++i)
9261 for (j = 0; j < set2->n; ++j) {
9262 int f;
9263 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9264 if (f == 1 || f == -2)
9265 return f;
9266 if (f > follows)
9267 follows = f;
9270 return follows;
9273 static isl_bool isl_basic_map_plain_has_fixed_var(
9274 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9276 int i;
9277 int d;
9278 unsigned total;
9280 if (!bmap)
9281 return isl_bool_error;
9282 total = isl_basic_map_total_dim(bmap);
9283 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9284 for (; d+1 > pos; --d)
9285 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9286 break;
9287 if (d != pos)
9288 continue;
9289 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9290 return isl_bool_false;
9291 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9292 return isl_bool_false;
9293 if (!isl_int_is_one(bmap->eq[i][1+d]))
9294 return isl_bool_false;
9295 if (val)
9296 isl_int_neg(*val, bmap->eq[i][0]);
9297 return isl_bool_true;
9299 return isl_bool_false;
9302 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9303 unsigned pos, isl_int *val)
9305 int i;
9306 isl_int v;
9307 isl_int tmp;
9308 isl_bool fixed;
9310 if (!map)
9311 return isl_bool_error;
9312 if (map->n == 0)
9313 return isl_bool_false;
9314 if (map->n == 1)
9315 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9316 isl_int_init(v);
9317 isl_int_init(tmp);
9318 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9319 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9320 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9321 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9322 fixed = isl_bool_false;
9324 if (val)
9325 isl_int_set(*val, v);
9326 isl_int_clear(tmp);
9327 isl_int_clear(v);
9328 return fixed;
9331 static isl_bool isl_basic_set_plain_has_fixed_var(
9332 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9334 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9335 pos, val);
9338 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9339 enum isl_dim_type type, unsigned pos, isl_int *val)
9341 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9342 return isl_bool_error;
9343 return isl_basic_map_plain_has_fixed_var(bmap,
9344 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9347 /* If "bmap" obviously lies on a hyperplane where the given dimension
9348 * has a fixed value, then return that value.
9349 * Otherwise return NaN.
9351 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9352 __isl_keep isl_basic_map *bmap,
9353 enum isl_dim_type type, unsigned pos)
9355 isl_ctx *ctx;
9356 isl_val *v;
9357 isl_bool fixed;
9359 if (!bmap)
9360 return NULL;
9361 ctx = isl_basic_map_get_ctx(bmap);
9362 v = isl_val_alloc(ctx);
9363 if (!v)
9364 return NULL;
9365 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9366 if (fixed < 0)
9367 return isl_val_free(v);
9368 if (fixed) {
9369 isl_int_set_si(v->d, 1);
9370 return v;
9372 isl_val_free(v);
9373 return isl_val_nan(ctx);
9376 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9377 enum isl_dim_type type, unsigned pos, isl_int *val)
9379 if (pos >= isl_map_dim(map, type))
9380 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9381 "position out of bounds", return isl_bool_error);
9382 return isl_map_plain_has_fixed_var(map,
9383 map_offset(map, type) - 1 + pos, val);
9386 /* If "map" obviously lies on a hyperplane where the given dimension
9387 * has a fixed value, then return that value.
9388 * Otherwise return NaN.
9390 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9391 enum isl_dim_type type, unsigned pos)
9393 isl_ctx *ctx;
9394 isl_val *v;
9395 isl_bool fixed;
9397 if (!map)
9398 return NULL;
9399 ctx = isl_map_get_ctx(map);
9400 v = isl_val_alloc(ctx);
9401 if (!v)
9402 return NULL;
9403 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9404 if (fixed < 0)
9405 return isl_val_free(v);
9406 if (fixed) {
9407 isl_int_set_si(v->d, 1);
9408 return v;
9410 isl_val_free(v);
9411 return isl_val_nan(ctx);
9414 /* If "set" obviously lies on a hyperplane where the given dimension
9415 * has a fixed value, then return that value.
9416 * Otherwise return NaN.
9418 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9419 enum isl_dim_type type, unsigned pos)
9421 return isl_map_plain_get_val_if_fixed(set, type, pos);
9424 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9425 * then return this fixed value in *val.
9427 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9428 unsigned dim, isl_int *val)
9430 return isl_basic_set_plain_has_fixed_var(bset,
9431 isl_basic_set_n_param(bset) + dim, val);
9434 /* Return -1 if the constraint "c1" should be sorted before "c2"
9435 * and 1 if it should be sorted after "c2".
9436 * Return 0 if the two constraints are the same (up to the constant term).
9438 * In particular, if a constraint involves later variables than another
9439 * then it is sorted after this other constraint.
9440 * uset_gist depends on constraints without existentially quantified
9441 * variables sorting first.
9443 * For constraints that have the same latest variable, those
9444 * with the same coefficient for this latest variable (first in absolute value
9445 * and then in actual value) are grouped together.
9446 * This is useful for detecting pairs of constraints that can
9447 * be chained in their printed representation.
9449 * Finally, within a group, constraints are sorted according to
9450 * their coefficients (excluding the constant term).
9452 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9454 isl_int **c1 = (isl_int **) p1;
9455 isl_int **c2 = (isl_int **) p2;
9456 int l1, l2;
9457 unsigned size = *(unsigned *) arg;
9458 int cmp;
9460 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9461 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9463 if (l1 != l2)
9464 return l1 - l2;
9466 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9467 if (cmp != 0)
9468 return cmp;
9469 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9470 if (cmp != 0)
9471 return -cmp;
9473 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9476 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9477 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9478 * and 0 if the two constraints are the same (up to the constant term).
9480 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9481 isl_int *c1, isl_int *c2)
9483 unsigned total;
9485 if (!bmap)
9486 return -2;
9487 total = isl_basic_map_total_dim(bmap);
9488 return sort_constraint_cmp(&c1, &c2, &total);
9491 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9492 __isl_take isl_basic_map *bmap)
9494 unsigned total;
9496 if (!bmap)
9497 return NULL;
9498 if (bmap->n_ineq == 0)
9499 return bmap;
9500 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9501 return bmap;
9502 total = isl_basic_map_total_dim(bmap);
9503 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9504 &sort_constraint_cmp, &total) < 0)
9505 return isl_basic_map_free(bmap);
9506 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9507 return bmap;
9510 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9511 __isl_take isl_basic_set *bset)
9513 isl_basic_map *bmap = bset_to_bmap(bset);
9514 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9517 __isl_give isl_basic_map *isl_basic_map_normalize(
9518 __isl_take isl_basic_map *bmap)
9520 bmap = isl_basic_map_remove_redundancies(bmap);
9521 bmap = isl_basic_map_sort_constraints(bmap);
9522 return bmap;
9524 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9525 __isl_keep isl_basic_map *bmap2)
9527 int i, cmp;
9528 unsigned total;
9529 isl_space *space1, *space2;
9531 if (!bmap1 || !bmap2)
9532 return -1;
9534 if (bmap1 == bmap2)
9535 return 0;
9536 space1 = isl_basic_map_peek_space(bmap1);
9537 space2 = isl_basic_map_peek_space(bmap2);
9538 cmp = isl_space_cmp(space1, space2);
9539 if (cmp)
9540 return cmp;
9541 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9542 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9543 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9544 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9545 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9546 return 0;
9547 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9548 return 1;
9549 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9550 return -1;
9551 if (bmap1->n_eq != bmap2->n_eq)
9552 return bmap1->n_eq - bmap2->n_eq;
9553 if (bmap1->n_ineq != bmap2->n_ineq)
9554 return bmap1->n_ineq - bmap2->n_ineq;
9555 if (bmap1->n_div != bmap2->n_div)
9556 return bmap1->n_div - bmap2->n_div;
9557 total = isl_basic_map_total_dim(bmap1);
9558 for (i = 0; i < bmap1->n_eq; ++i) {
9559 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9560 if (cmp)
9561 return cmp;
9563 for (i = 0; i < bmap1->n_ineq; ++i) {
9564 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9565 if (cmp)
9566 return cmp;
9568 for (i = 0; i < bmap1->n_div; ++i) {
9569 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9570 if (cmp)
9571 return cmp;
9573 return 0;
9576 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9577 __isl_keep isl_basic_set *bset2)
9579 return isl_basic_map_plain_cmp(bset1, bset2);
9582 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9584 int i, cmp;
9586 if (set1 == set2)
9587 return 0;
9588 if (set1->n != set2->n)
9589 return set1->n - set2->n;
9591 for (i = 0; i < set1->n; ++i) {
9592 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9593 if (cmp)
9594 return cmp;
9597 return 0;
9600 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9601 __isl_keep isl_basic_map *bmap2)
9603 if (!bmap1 || !bmap2)
9604 return isl_bool_error;
9605 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9608 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9609 __isl_keep isl_basic_set *bset2)
9611 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9612 bset_to_bmap(bset2));
9615 static int qsort_bmap_cmp(const void *p1, const void *p2)
9617 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9618 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9620 return isl_basic_map_plain_cmp(bmap1, bmap2);
9623 /* Sort the basic maps of "map" and remove duplicate basic maps.
9625 * While removing basic maps, we make sure that the basic maps remain
9626 * sorted because isl_map_normalize expects the basic maps of the result
9627 * to be sorted.
9629 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9631 int i, j;
9633 map = isl_map_remove_empty_parts(map);
9634 if (!map)
9635 return NULL;
9636 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9637 for (i = map->n - 1; i >= 1; --i) {
9638 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9639 continue;
9640 isl_basic_map_free(map->p[i-1]);
9641 for (j = i; j < map->n; ++j)
9642 map->p[j - 1] = map->p[j];
9643 map->n--;
9646 return map;
9649 /* Remove obvious duplicates among the basic maps of "map".
9651 * Unlike isl_map_normalize, this function does not remove redundant
9652 * constraints and only removes duplicates that have exactly the same
9653 * constraints in the input. It does sort the constraints and
9654 * the basic maps to ease the detection of duplicates.
9656 * If "map" has already been normalized or if the basic maps are
9657 * disjoint, then there can be no duplicates.
9659 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9661 int i;
9662 isl_basic_map *bmap;
9664 if (!map)
9665 return NULL;
9666 if (map->n <= 1)
9667 return map;
9668 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9669 return map;
9670 for (i = 0; i < map->n; ++i) {
9671 bmap = isl_basic_map_copy(map->p[i]);
9672 bmap = isl_basic_map_sort_constraints(bmap);
9673 if (!bmap)
9674 return isl_map_free(map);
9675 isl_basic_map_free(map->p[i]);
9676 map->p[i] = bmap;
9679 map = sort_and_remove_duplicates(map);
9680 return map;
9683 /* We normalize in place, but if anything goes wrong we need
9684 * to return NULL, so we need to make sure we don't change the
9685 * meaning of any possible other copies of map.
9687 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9689 int i;
9690 struct isl_basic_map *bmap;
9692 if (!map)
9693 return NULL;
9694 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9695 return map;
9696 for (i = 0; i < map->n; ++i) {
9697 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9698 if (!bmap)
9699 goto error;
9700 isl_basic_map_free(map->p[i]);
9701 map->p[i] = bmap;
9704 map = sort_and_remove_duplicates(map);
9705 if (map)
9706 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9707 return map;
9708 error:
9709 isl_map_free(map);
9710 return NULL;
9713 struct isl_set *isl_set_normalize(struct isl_set *set)
9715 return set_from_map(isl_map_normalize(set_to_map(set)));
9718 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9719 __isl_keep isl_map *map2)
9721 int i;
9722 isl_bool equal;
9724 if (!map1 || !map2)
9725 return isl_bool_error;
9727 if (map1 == map2)
9728 return isl_bool_true;
9729 if (!isl_space_is_equal(map1->dim, map2->dim))
9730 return isl_bool_false;
9732 map1 = isl_map_copy(map1);
9733 map2 = isl_map_copy(map2);
9734 map1 = isl_map_normalize(map1);
9735 map2 = isl_map_normalize(map2);
9736 if (!map1 || !map2)
9737 goto error;
9738 equal = map1->n == map2->n;
9739 for (i = 0; equal && i < map1->n; ++i) {
9740 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9741 if (equal < 0)
9742 goto error;
9744 isl_map_free(map1);
9745 isl_map_free(map2);
9746 return equal;
9747 error:
9748 isl_map_free(map1);
9749 isl_map_free(map2);
9750 return isl_bool_error;
9753 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9754 __isl_keep isl_set *set2)
9756 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9759 /* Return the basic maps in "map" as a list.
9761 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9762 __isl_keep isl_map *map)
9764 int i;
9765 isl_ctx *ctx;
9766 isl_basic_map_list *list;
9768 if (!map)
9769 return NULL;
9770 ctx = isl_map_get_ctx(map);
9771 list = isl_basic_map_list_alloc(ctx, map->n);
9773 for (i = 0; i < map->n; ++i) {
9774 isl_basic_map *bmap;
9776 bmap = isl_basic_map_copy(map->p[i]);
9777 list = isl_basic_map_list_add(list, bmap);
9780 return list;
9783 /* Return the intersection of the elements in the non-empty list "list".
9784 * All elements are assumed to live in the same space.
9786 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9787 __isl_take isl_basic_map_list *list)
9789 int i, n;
9790 isl_basic_map *bmap;
9792 if (!list)
9793 return NULL;
9794 n = isl_basic_map_list_n_basic_map(list);
9795 if (n < 1)
9796 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9797 "expecting non-empty list", goto error);
9799 bmap = isl_basic_map_list_get_basic_map(list, 0);
9800 for (i = 1; i < n; ++i) {
9801 isl_basic_map *bmap_i;
9803 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9804 bmap = isl_basic_map_intersect(bmap, bmap_i);
9807 isl_basic_map_list_free(list);
9808 return bmap;
9809 error:
9810 isl_basic_map_list_free(list);
9811 return NULL;
9814 /* Return the intersection of the elements in the non-empty list "list".
9815 * All elements are assumed to live in the same space.
9817 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9818 __isl_take isl_basic_set_list *list)
9820 return isl_basic_map_list_intersect(list);
9823 /* Return the union of the elements of "list".
9824 * The list is required to have at least one element.
9826 __isl_give isl_set *isl_basic_set_list_union(
9827 __isl_take isl_basic_set_list *list)
9829 int i, n;
9830 isl_space *space;
9831 isl_basic_set *bset;
9832 isl_set *set;
9834 if (!list)
9835 return NULL;
9836 n = isl_basic_set_list_n_basic_set(list);
9837 if (n < 1)
9838 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9839 "expecting non-empty list", goto error);
9841 bset = isl_basic_set_list_get_basic_set(list, 0);
9842 space = isl_basic_set_get_space(bset);
9843 isl_basic_set_free(bset);
9845 set = isl_set_alloc_space(space, n, 0);
9846 for (i = 0; i < n; ++i) {
9847 bset = isl_basic_set_list_get_basic_set(list, i);
9848 set = isl_set_add_basic_set(set, bset);
9851 isl_basic_set_list_free(list);
9852 return set;
9853 error:
9854 isl_basic_set_list_free(list);
9855 return NULL;
9858 /* Return the union of the elements in the non-empty list "list".
9859 * All elements are assumed to live in the same space.
9861 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9863 int i, n;
9864 isl_set *set;
9866 if (!list)
9867 return NULL;
9868 n = isl_set_list_n_set(list);
9869 if (n < 1)
9870 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9871 "expecting non-empty list", goto error);
9873 set = isl_set_list_get_set(list, 0);
9874 for (i = 1; i < n; ++i) {
9875 isl_set *set_i;
9877 set_i = isl_set_list_get_set(list, i);
9878 set = isl_set_union(set, set_i);
9881 isl_set_list_free(list);
9882 return set;
9883 error:
9884 isl_set_list_free(list);
9885 return NULL;
9888 __isl_give isl_basic_map *isl_basic_map_product(
9889 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9891 isl_space *space_result = NULL;
9892 struct isl_basic_map *bmap;
9893 unsigned in1, in2, out1, out2, nparam, total, pos;
9894 struct isl_dim_map *dim_map1, *dim_map2;
9896 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9897 goto error;
9898 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9899 isl_space_copy(bmap2->dim));
9901 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9902 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9903 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9904 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9905 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9907 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9908 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9909 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9914 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9915 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9916 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9917 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9919 bmap = isl_basic_map_alloc_space(space_result,
9920 bmap1->n_div + bmap2->n_div,
9921 bmap1->n_eq + bmap2->n_eq,
9922 bmap1->n_ineq + bmap2->n_ineq);
9923 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9925 bmap = isl_basic_map_simplify(bmap);
9926 return isl_basic_map_finalize(bmap);
9927 error:
9928 isl_basic_map_free(bmap1);
9929 isl_basic_map_free(bmap2);
9930 return NULL;
9933 __isl_give isl_basic_map *isl_basic_map_flat_product(
9934 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9936 isl_basic_map *prod;
9938 prod = isl_basic_map_product(bmap1, bmap2);
9939 prod = isl_basic_map_flatten(prod);
9940 return prod;
9943 __isl_give isl_basic_set *isl_basic_set_flat_product(
9944 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9946 return isl_basic_map_flat_range_product(bset1, bset2);
9949 __isl_give isl_basic_map *isl_basic_map_domain_product(
9950 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9952 isl_space *space_result = NULL;
9953 isl_basic_map *bmap;
9954 unsigned in1, in2, out, nparam, total, pos;
9955 struct isl_dim_map *dim_map1, *dim_map2;
9957 if (!bmap1 || !bmap2)
9958 goto error;
9960 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9961 isl_space_copy(bmap2->dim));
9963 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9964 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9965 out = isl_basic_map_dim(bmap1, isl_dim_out);
9966 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9968 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9969 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9970 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9971 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9972 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9973 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9974 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9975 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9976 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9977 isl_dim_map_div(dim_map1, bmap1, pos += out);
9978 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9980 bmap = isl_basic_map_alloc_space(space_result,
9981 bmap1->n_div + bmap2->n_div,
9982 bmap1->n_eq + bmap2->n_eq,
9983 bmap1->n_ineq + bmap2->n_ineq);
9984 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9985 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9986 bmap = isl_basic_map_simplify(bmap);
9987 return isl_basic_map_finalize(bmap);
9988 error:
9989 isl_basic_map_free(bmap1);
9990 isl_basic_map_free(bmap2);
9991 return NULL;
9994 __isl_give isl_basic_map *isl_basic_map_range_product(
9995 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9997 isl_bool rational;
9998 isl_space *space_result = NULL;
9999 isl_basic_map *bmap;
10000 unsigned in, out1, out2, nparam, total, pos;
10001 struct isl_dim_map *dim_map1, *dim_map2;
10003 rational = isl_basic_map_is_rational(bmap1);
10004 if (rational >= 0 && rational)
10005 rational = isl_basic_map_is_rational(bmap2);
10006 if (!bmap1 || !bmap2 || rational < 0)
10007 goto error;
10009 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10010 goto error;
10012 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10013 isl_space_copy(bmap2->dim));
10015 in = isl_basic_map_dim(bmap1, isl_dim_in);
10016 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10017 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10018 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10020 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10021 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10022 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10023 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10024 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10025 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10026 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10027 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10028 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10029 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10030 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10032 bmap = isl_basic_map_alloc_space(space_result,
10033 bmap1->n_div + bmap2->n_div,
10034 bmap1->n_eq + bmap2->n_eq,
10035 bmap1->n_ineq + bmap2->n_ineq);
10036 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10037 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10038 if (rational)
10039 bmap = isl_basic_map_set_rational(bmap);
10040 bmap = isl_basic_map_simplify(bmap);
10041 return isl_basic_map_finalize(bmap);
10042 error:
10043 isl_basic_map_free(bmap1);
10044 isl_basic_map_free(bmap2);
10045 return NULL;
10048 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10049 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10051 isl_basic_map *prod;
10053 prod = isl_basic_map_range_product(bmap1, bmap2);
10054 prod = isl_basic_map_flatten_range(prod);
10055 return prod;
10058 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10059 * and collect the results.
10060 * The result live in the space obtained by calling "space_product"
10061 * on the spaces of "map1" and "map2".
10062 * If "remove_duplicates" is set then the result may contain duplicates
10063 * (even if the inputs do not) and so we try and remove the obvious
10064 * duplicates.
10066 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10067 __isl_take isl_map *map2,
10068 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10069 __isl_take isl_space *right),
10070 __isl_give isl_basic_map *(*basic_map_product)(
10071 __isl_take isl_basic_map *left,
10072 __isl_take isl_basic_map *right),
10073 int remove_duplicates)
10075 unsigned flags = 0;
10076 struct isl_map *result;
10077 int i, j;
10078 isl_bool m;
10080 m = isl_map_has_equal_params(map1, map2);
10081 if (m < 0)
10082 goto error;
10083 if (!m)
10084 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10085 "parameters don't match", goto error);
10087 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10088 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10089 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10091 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10092 isl_space_copy(map2->dim)),
10093 map1->n * map2->n, flags);
10094 if (!result)
10095 goto error;
10096 for (i = 0; i < map1->n; ++i)
10097 for (j = 0; j < map2->n; ++j) {
10098 struct isl_basic_map *part;
10099 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10100 isl_basic_map_copy(map2->p[j]));
10101 if (isl_basic_map_is_empty(part))
10102 isl_basic_map_free(part);
10103 else
10104 result = isl_map_add_basic_map(result, part);
10105 if (!result)
10106 goto error;
10108 if (remove_duplicates)
10109 result = isl_map_remove_obvious_duplicates(result);
10110 isl_map_free(map1);
10111 isl_map_free(map2);
10112 return result;
10113 error:
10114 isl_map_free(map1);
10115 isl_map_free(map2);
10116 return NULL;
10119 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10121 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10122 __isl_take isl_map *map2)
10124 return map_product(map1, map2, &isl_space_product,
10125 &isl_basic_map_product, 0);
10128 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10129 __isl_take isl_map *map2)
10131 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10134 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10136 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10137 __isl_take isl_map *map2)
10139 isl_map *prod;
10141 prod = isl_map_product(map1, map2);
10142 prod = isl_map_flatten(prod);
10143 return prod;
10146 /* Given two set A and B, construct its Cartesian product A x B.
10148 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10150 return isl_map_range_product(set1, set2);
10153 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10154 __isl_take isl_set *set2)
10156 return isl_map_flat_range_product(set1, set2);
10159 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10161 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10162 __isl_take isl_map *map2)
10164 return map_product(map1, map2, &isl_space_domain_product,
10165 &isl_basic_map_domain_product, 1);
10168 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10170 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10171 __isl_take isl_map *map2)
10173 return map_product(map1, map2, &isl_space_range_product,
10174 &isl_basic_map_range_product, 1);
10177 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10178 __isl_take isl_map *map2)
10180 return isl_map_align_params_map_map_and(map1, map2,
10181 &map_domain_product_aligned);
10184 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10185 __isl_take isl_map *map2)
10187 return isl_map_align_params_map_map_and(map1, map2,
10188 &map_range_product_aligned);
10191 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10193 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10195 isl_space *space;
10196 int total1, keep1, total2, keep2;
10198 if (!map)
10199 return NULL;
10200 if (!isl_space_domain_is_wrapping(map->dim) ||
10201 !isl_space_range_is_wrapping(map->dim))
10202 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10203 "not a product", return isl_map_free(map));
10205 space = isl_map_get_space(map);
10206 total1 = isl_space_dim(space, isl_dim_in);
10207 total2 = isl_space_dim(space, isl_dim_out);
10208 space = isl_space_factor_domain(space);
10209 keep1 = isl_space_dim(space, isl_dim_in);
10210 keep2 = isl_space_dim(space, isl_dim_out);
10211 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10212 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10213 map = isl_map_reset_space(map, space);
10215 return map;
10218 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10220 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10222 isl_space *space;
10223 int total1, keep1, total2, keep2;
10225 if (!map)
10226 return NULL;
10227 if (!isl_space_domain_is_wrapping(map->dim) ||
10228 !isl_space_range_is_wrapping(map->dim))
10229 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10230 "not a product", return isl_map_free(map));
10232 space = isl_map_get_space(map);
10233 total1 = isl_space_dim(space, isl_dim_in);
10234 total2 = isl_space_dim(space, isl_dim_out);
10235 space = isl_space_factor_range(space);
10236 keep1 = isl_space_dim(space, isl_dim_in);
10237 keep2 = isl_space_dim(space, isl_dim_out);
10238 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10239 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10240 map = isl_map_reset_space(map, space);
10242 return map;
10245 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10247 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10249 isl_space *space;
10250 int total, keep;
10252 if (!map)
10253 return NULL;
10254 if (!isl_space_domain_is_wrapping(map->dim))
10255 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10256 "domain is not a product", return isl_map_free(map));
10258 space = isl_map_get_space(map);
10259 total = isl_space_dim(space, isl_dim_in);
10260 space = isl_space_domain_factor_domain(space);
10261 keep = isl_space_dim(space, isl_dim_in);
10262 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10263 map = isl_map_reset_space(map, space);
10265 return map;
10268 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10270 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10272 isl_space *space;
10273 int total, keep;
10275 if (!map)
10276 return NULL;
10277 if (!isl_space_domain_is_wrapping(map->dim))
10278 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10279 "domain is not a product", return isl_map_free(map));
10281 space = isl_map_get_space(map);
10282 total = isl_space_dim(space, isl_dim_in);
10283 space = isl_space_domain_factor_range(space);
10284 keep = isl_space_dim(space, isl_dim_in);
10285 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10286 map = isl_map_reset_space(map, space);
10288 return map;
10291 /* Given a map A -> [B -> C], extract the map A -> B.
10293 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10295 isl_space *space;
10296 int total, keep;
10298 if (!map)
10299 return NULL;
10300 if (!isl_space_range_is_wrapping(map->dim))
10301 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10302 "range is not a product", return isl_map_free(map));
10304 space = isl_map_get_space(map);
10305 total = isl_space_dim(space, isl_dim_out);
10306 space = isl_space_range_factor_domain(space);
10307 keep = isl_space_dim(space, isl_dim_out);
10308 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10309 map = isl_map_reset_space(map, space);
10311 return map;
10314 /* Given a map A -> [B -> C], extract the map A -> C.
10316 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10318 isl_space *space;
10319 int total, keep;
10321 if (!map)
10322 return NULL;
10323 if (!isl_space_range_is_wrapping(map->dim))
10324 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10325 "range is not a product", return isl_map_free(map));
10327 space = isl_map_get_space(map);
10328 total = isl_space_dim(space, isl_dim_out);
10329 space = isl_space_range_factor_range(space);
10330 keep = isl_space_dim(space, isl_dim_out);
10331 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10332 map = isl_map_reset_space(map, space);
10334 return map;
10337 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10339 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10340 __isl_take isl_map *map2)
10342 isl_map *prod;
10344 prod = isl_map_domain_product(map1, map2);
10345 prod = isl_map_flatten_domain(prod);
10346 return prod;
10349 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10351 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10352 __isl_take isl_map *map2)
10354 isl_map *prod;
10356 prod = isl_map_range_product(map1, map2);
10357 prod = isl_map_flatten_range(prod);
10358 return prod;
10361 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10363 int i;
10364 uint32_t hash = isl_hash_init();
10365 unsigned total;
10367 if (!bmap)
10368 return 0;
10369 bmap = isl_basic_map_copy(bmap);
10370 bmap = isl_basic_map_normalize(bmap);
10371 if (!bmap)
10372 return 0;
10373 total = isl_basic_map_total_dim(bmap);
10374 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10375 for (i = 0; i < bmap->n_eq; ++i) {
10376 uint32_t c_hash;
10377 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10378 isl_hash_hash(hash, c_hash);
10380 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10381 for (i = 0; i < bmap->n_ineq; ++i) {
10382 uint32_t c_hash;
10383 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10384 isl_hash_hash(hash, c_hash);
10386 isl_hash_byte(hash, bmap->n_div & 0xFF);
10387 for (i = 0; i < bmap->n_div; ++i) {
10388 uint32_t c_hash;
10389 if (isl_int_is_zero(bmap->div[i][0]))
10390 continue;
10391 isl_hash_byte(hash, i & 0xFF);
10392 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10393 isl_hash_hash(hash, c_hash);
10395 isl_basic_map_free(bmap);
10396 return hash;
10399 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10401 return isl_basic_map_get_hash(bset_to_bmap(bset));
10404 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10406 int i;
10407 uint32_t hash;
10409 if (!map)
10410 return 0;
10411 map = isl_map_copy(map);
10412 map = isl_map_normalize(map);
10413 if (!map)
10414 return 0;
10416 hash = isl_hash_init();
10417 for (i = 0; i < map->n; ++i) {
10418 uint32_t bmap_hash;
10419 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10420 isl_hash_hash(hash, bmap_hash);
10423 isl_map_free(map);
10425 return hash;
10428 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10430 return isl_map_get_hash(set_to_map(set));
10433 /* Return the number of basic maps in the (current) representation of "map".
10435 int isl_map_n_basic_map(__isl_keep isl_map *map)
10437 return map ? map->n : 0;
10440 int isl_set_n_basic_set(__isl_keep isl_set *set)
10442 return set ? set->n : 0;
10445 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10446 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10448 int i;
10450 if (!map)
10451 return isl_stat_error;
10453 for (i = 0; i < map->n; ++i)
10454 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10455 return isl_stat_error;
10457 return isl_stat_ok;
10460 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10461 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10463 int i;
10465 if (!set)
10466 return isl_stat_error;
10468 for (i = 0; i < set->n; ++i)
10469 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10470 return isl_stat_error;
10472 return isl_stat_ok;
10475 /* Return a list of basic sets, the union of which is equal to "set".
10477 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10478 __isl_keep isl_set *set)
10480 int i;
10481 isl_basic_set_list *list;
10483 if (!set)
10484 return NULL;
10486 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10487 for (i = 0; i < set->n; ++i) {
10488 isl_basic_set *bset;
10490 bset = isl_basic_set_copy(set->p[i]);
10491 list = isl_basic_set_list_add(list, bset);
10494 return list;
10497 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10499 isl_space *space;
10501 if (!bset)
10502 return NULL;
10504 bset = isl_basic_set_cow(bset);
10505 if (!bset)
10506 return NULL;
10508 space = isl_basic_set_get_space(bset);
10509 space = isl_space_lift(space, bset->n_div);
10510 if (!space)
10511 goto error;
10512 isl_space_free(bset->dim);
10513 bset->dim = space;
10514 bset->extra -= bset->n_div;
10515 bset->n_div = 0;
10517 bset = isl_basic_set_finalize(bset);
10519 return bset;
10520 error:
10521 isl_basic_set_free(bset);
10522 return NULL;
10525 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10527 int i;
10528 isl_space *space;
10529 unsigned n_div;
10531 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10533 if (!set)
10534 return NULL;
10536 set = isl_set_cow(set);
10537 if (!set)
10538 return NULL;
10540 n_div = set->p[0]->n_div;
10541 space = isl_set_get_space(set);
10542 space = isl_space_lift(space, n_div);
10543 if (!space)
10544 goto error;
10545 isl_space_free(set->dim);
10546 set->dim = space;
10548 for (i = 0; i < set->n; ++i) {
10549 set->p[i] = isl_basic_set_lift(set->p[i]);
10550 if (!set->p[i])
10551 goto error;
10554 return set;
10555 error:
10556 isl_set_free(set);
10557 return NULL;
10560 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10562 unsigned dim;
10563 int size = 0;
10565 if (!bset)
10566 return -1;
10568 dim = isl_basic_set_total_dim(bset);
10569 size += bset->n_eq * (1 + dim);
10570 size += bset->n_ineq * (1 + dim);
10571 size += bset->n_div * (2 + dim);
10573 return size;
10576 int isl_set_size(__isl_keep isl_set *set)
10578 int i;
10579 int size = 0;
10581 if (!set)
10582 return -1;
10584 for (i = 0; i < set->n; ++i)
10585 size += isl_basic_set_size(set->p[i]);
10587 return size;
10590 /* Check if there is any lower bound (if lower == 0) and/or upper
10591 * bound (if upper == 0) on the specified dim.
10593 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10594 enum isl_dim_type type, unsigned pos, int lower, int upper)
10596 int i;
10598 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10599 return isl_bool_error;
10601 pos += isl_basic_map_offset(bmap, type);
10603 for (i = 0; i < bmap->n_div; ++i) {
10604 if (isl_int_is_zero(bmap->div[i][0]))
10605 continue;
10606 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10607 return isl_bool_true;
10610 for (i = 0; i < bmap->n_eq; ++i)
10611 if (!isl_int_is_zero(bmap->eq[i][pos]))
10612 return isl_bool_true;
10614 for (i = 0; i < bmap->n_ineq; ++i) {
10615 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10616 if (sgn > 0)
10617 lower = 1;
10618 if (sgn < 0)
10619 upper = 1;
10622 return lower && upper;
10625 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10626 enum isl_dim_type type, unsigned pos)
10628 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10631 isl_bool isl_basic_map_dim_has_lower_bound(__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, 1);
10637 isl_bool isl_basic_map_dim_has_upper_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, 1, 0);
10643 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10644 enum isl_dim_type type, unsigned pos)
10646 int i;
10648 if (!map)
10649 return isl_bool_error;
10651 for (i = 0; i < map->n; ++i) {
10652 isl_bool bounded;
10653 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10654 if (bounded < 0 || !bounded)
10655 return bounded;
10658 return isl_bool_true;
10661 /* Return true if the specified dim is involved in both an upper bound
10662 * and a lower bound.
10664 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10665 enum isl_dim_type type, unsigned pos)
10667 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10670 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10672 static isl_bool has_any_bound(__isl_keep isl_map *map,
10673 enum isl_dim_type type, unsigned pos,
10674 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10675 enum isl_dim_type type, unsigned pos))
10677 int i;
10679 if (!map)
10680 return isl_bool_error;
10682 for (i = 0; i < map->n; ++i) {
10683 isl_bool bounded;
10684 bounded = fn(map->p[i], type, pos);
10685 if (bounded < 0 || bounded)
10686 return bounded;
10689 return isl_bool_false;
10692 /* Return 1 if the specified dim is involved in any lower bound.
10694 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10695 enum isl_dim_type type, unsigned pos)
10697 return has_any_bound(set, type, pos,
10698 &isl_basic_map_dim_has_lower_bound);
10701 /* Return 1 if the specified dim is involved in any upper bound.
10703 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10704 enum isl_dim_type type, unsigned pos)
10706 return has_any_bound(set, type, pos,
10707 &isl_basic_map_dim_has_upper_bound);
10710 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10712 static isl_bool has_bound(__isl_keep isl_map *map,
10713 enum isl_dim_type type, unsigned pos,
10714 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10715 enum isl_dim_type type, unsigned pos))
10717 int i;
10719 if (!map)
10720 return isl_bool_error;
10722 for (i = 0; i < map->n; ++i) {
10723 isl_bool bounded;
10724 bounded = fn(map->p[i], type, pos);
10725 if (bounded < 0 || !bounded)
10726 return bounded;
10729 return isl_bool_true;
10732 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10734 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10735 enum isl_dim_type type, unsigned pos)
10737 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10740 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10742 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10743 enum isl_dim_type type, unsigned pos)
10745 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10748 /* For each of the "n" variables starting at "first", determine
10749 * the sign of the variable and put the results in the first "n"
10750 * elements of the array "signs".
10751 * Sign
10752 * 1 means that the variable is non-negative
10753 * -1 means that the variable is non-positive
10754 * 0 means the variable attains both positive and negative values.
10756 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10757 unsigned first, unsigned n, int *signs)
10759 isl_vec *bound = NULL;
10760 struct isl_tab *tab = NULL;
10761 struct isl_tab_undo *snap;
10762 int i;
10764 if (!bset || !signs)
10765 return isl_stat_error;
10767 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10768 tab = isl_tab_from_basic_set(bset, 0);
10769 if (!bound || !tab)
10770 goto error;
10772 isl_seq_clr(bound->el, bound->size);
10773 isl_int_set_si(bound->el[0], -1);
10775 snap = isl_tab_snap(tab);
10776 for (i = 0; i < n; ++i) {
10777 int empty;
10779 isl_int_set_si(bound->el[1 + first + i], -1);
10780 if (isl_tab_add_ineq(tab, bound->el) < 0)
10781 goto error;
10782 empty = tab->empty;
10783 isl_int_set_si(bound->el[1 + first + i], 0);
10784 if (isl_tab_rollback(tab, snap) < 0)
10785 goto error;
10787 if (empty) {
10788 signs[i] = 1;
10789 continue;
10792 isl_int_set_si(bound->el[1 + first + i], 1);
10793 if (isl_tab_add_ineq(tab, bound->el) < 0)
10794 goto error;
10795 empty = tab->empty;
10796 isl_int_set_si(bound->el[1 + first + i], 0);
10797 if (isl_tab_rollback(tab, snap) < 0)
10798 goto error;
10800 signs[i] = empty ? -1 : 0;
10803 isl_tab_free(tab);
10804 isl_vec_free(bound);
10805 return isl_stat_ok;
10806 error:
10807 isl_tab_free(tab);
10808 isl_vec_free(bound);
10809 return isl_stat_error;
10812 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10813 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10815 if (!bset || !signs)
10816 return isl_stat_error;
10817 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10818 return isl_stat_error);
10820 first += pos(bset->dim, type) - 1;
10821 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10824 /* Is it possible for the integer division "div" to depend (possibly
10825 * indirectly) on any output dimensions?
10827 * If the div is undefined, then we conservatively assume that it
10828 * may depend on them.
10829 * Otherwise, we check if it actually depends on them or on any integer
10830 * divisions that may depend on them.
10832 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10834 int i;
10835 unsigned n_out, o_out;
10836 unsigned n_div, o_div;
10838 if (isl_int_is_zero(bmap->div[div][0]))
10839 return isl_bool_true;
10841 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10842 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10844 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10845 return isl_bool_true;
10847 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10848 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10850 for (i = 0; i < n_div; ++i) {
10851 isl_bool may_involve;
10853 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10854 continue;
10855 may_involve = div_may_involve_output(bmap, i);
10856 if (may_involve < 0 || may_involve)
10857 return may_involve;
10860 return isl_bool_false;
10863 /* Return the first integer division of "bmap" in the range
10864 * [first, first + n[ that may depend on any output dimensions and
10865 * that has a non-zero coefficient in "c" (where the first coefficient
10866 * in "c" corresponds to integer division "first").
10868 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10869 isl_int *c, int first, int n)
10871 int k;
10873 if (!bmap)
10874 return -1;
10876 for (k = first; k < first + n; ++k) {
10877 isl_bool may_involve;
10879 if (isl_int_is_zero(c[k]))
10880 continue;
10881 may_involve = div_may_involve_output(bmap, k);
10882 if (may_involve < 0)
10883 return -1;
10884 if (may_involve)
10885 return k;
10888 return first + n;
10891 /* Look for a pair of inequality constraints in "bmap" of the form
10893 * -l + i >= 0 or i >= l
10894 * and
10895 * n + l - i >= 0 or i <= l + n
10897 * with n < "m" and i the output dimension at position "pos".
10898 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10899 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10900 * and earlier output dimensions, as well as integer divisions that do
10901 * not involve any of the output dimensions.
10903 * Return the index of the first inequality constraint or bmap->n_ineq
10904 * if no such pair can be found.
10906 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10907 int pos, isl_int m)
10909 int i, j;
10910 isl_ctx *ctx;
10911 unsigned total;
10912 unsigned n_div, o_div;
10913 unsigned n_out, o_out;
10914 int less;
10916 if (!bmap)
10917 return -1;
10919 ctx = isl_basic_map_get_ctx(bmap);
10920 total = isl_basic_map_total_dim(bmap);
10921 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10922 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10923 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10924 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10925 for (i = 0; i < bmap->n_ineq; ++i) {
10926 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10927 continue;
10928 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10929 n_out - (pos + 1)) != -1)
10930 continue;
10931 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10932 0, n_div) < n_div)
10933 continue;
10934 for (j = i + 1; j < bmap->n_ineq; ++j) {
10935 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10936 ctx->one))
10937 continue;
10938 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10939 bmap->ineq[j] + 1, total))
10940 continue;
10941 break;
10943 if (j >= bmap->n_ineq)
10944 continue;
10945 isl_int_add(bmap->ineq[i][0],
10946 bmap->ineq[i][0], bmap->ineq[j][0]);
10947 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10948 isl_int_sub(bmap->ineq[i][0],
10949 bmap->ineq[i][0], bmap->ineq[j][0]);
10950 if (!less)
10951 continue;
10952 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10953 return i;
10954 else
10955 return j;
10958 return bmap->n_ineq;
10961 /* Return the index of the equality of "bmap" that defines
10962 * the output dimension "pos" in terms of earlier dimensions.
10963 * The equality may also involve integer divisions, as long
10964 * as those integer divisions are defined in terms of
10965 * parameters or input dimensions.
10966 * In this case, *div is set to the number of integer divisions and
10967 * *ineq is set to the number of inequality constraints (provided
10968 * div and ineq are not NULL).
10970 * The equality may also involve a single integer division involving
10971 * the output dimensions (typically only output dimension "pos") as
10972 * long as the coefficient of output dimension "pos" is 1 or -1 and
10973 * there is a pair of constraints i >= l and i <= l + n, with i referring
10974 * to output dimension "pos", l an expression involving only earlier
10975 * dimensions and n smaller than the coefficient of the integer division
10976 * in the equality. In this case, the output dimension can be defined
10977 * in terms of a modulo expression that does not involve the integer division.
10978 * *div is then set to this single integer division and
10979 * *ineq is set to the index of constraint i >= l.
10981 * Return bmap->n_eq if there is no such equality.
10982 * Return -1 on error.
10984 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10985 int pos, int *div, int *ineq)
10987 int j, k, l;
10988 unsigned n_out, o_out;
10989 unsigned n_div, o_div;
10991 if (!bmap)
10992 return -1;
10994 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10995 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10996 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10997 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10999 if (ineq)
11000 *ineq = bmap->n_ineq;
11001 if (div)
11002 *div = n_div;
11003 for (j = 0; j < bmap->n_eq; ++j) {
11004 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11005 continue;
11006 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11007 n_out - (pos + 1)) != -1)
11008 continue;
11009 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11010 0, n_div);
11011 if (k >= n_div)
11012 return j;
11013 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11014 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11015 continue;
11016 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11017 k + 1, n_div - (k+1)) < n_div)
11018 continue;
11019 l = find_modulo_constraint_pair(bmap, pos,
11020 bmap->eq[j][o_div + k]);
11021 if (l < 0)
11022 return -1;
11023 if (l >= bmap->n_ineq)
11024 continue;
11025 if (div)
11026 *div = k;
11027 if (ineq)
11028 *ineq = l;
11029 return j;
11032 return bmap->n_eq;
11035 /* Check if the given basic map is obviously single-valued.
11036 * In particular, for each output dimension, check that there is
11037 * an equality that defines the output dimension in terms of
11038 * earlier dimensions.
11040 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11042 int i;
11043 unsigned n_out;
11045 if (!bmap)
11046 return isl_bool_error;
11048 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11050 for (i = 0; i < n_out; ++i) {
11051 int eq;
11053 eq = isl_basic_map_output_defining_equality(bmap, i,
11054 NULL, NULL);
11055 if (eq < 0)
11056 return isl_bool_error;
11057 if (eq >= bmap->n_eq)
11058 return isl_bool_false;
11061 return isl_bool_true;
11064 /* Check if the given basic map is single-valued.
11065 * We simply compute
11067 * M \circ M^-1
11069 * and check if the result is a subset of the identity mapping.
11071 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11073 isl_space *space;
11074 isl_basic_map *test;
11075 isl_basic_map *id;
11076 isl_bool sv;
11078 sv = isl_basic_map_plain_is_single_valued(bmap);
11079 if (sv < 0 || sv)
11080 return sv;
11082 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11083 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11085 space = isl_basic_map_get_space(bmap);
11086 space = isl_space_map_from_set(isl_space_range(space));
11087 id = isl_basic_map_identity(space);
11089 sv = isl_basic_map_is_subset(test, id);
11091 isl_basic_map_free(test);
11092 isl_basic_map_free(id);
11094 return sv;
11097 /* Check if the given map is obviously single-valued.
11099 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11101 if (!map)
11102 return isl_bool_error;
11103 if (map->n == 0)
11104 return isl_bool_true;
11105 if (map->n >= 2)
11106 return isl_bool_false;
11108 return isl_basic_map_plain_is_single_valued(map->p[0]);
11111 /* Check if the given map is single-valued.
11112 * We simply compute
11114 * M \circ M^-1
11116 * and check if the result is a subset of the identity mapping.
11118 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11120 isl_space *dim;
11121 isl_map *test;
11122 isl_map *id;
11123 isl_bool sv;
11125 sv = isl_map_plain_is_single_valued(map);
11126 if (sv < 0 || sv)
11127 return sv;
11129 test = isl_map_reverse(isl_map_copy(map));
11130 test = isl_map_apply_range(test, isl_map_copy(map));
11132 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11133 id = isl_map_identity(dim);
11135 sv = isl_map_is_subset(test, id);
11137 isl_map_free(test);
11138 isl_map_free(id);
11140 return sv;
11143 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11145 isl_bool in;
11147 map = isl_map_copy(map);
11148 map = isl_map_reverse(map);
11149 in = isl_map_is_single_valued(map);
11150 isl_map_free(map);
11152 return in;
11155 /* Check if the given map is obviously injective.
11157 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11159 isl_bool in;
11161 map = isl_map_copy(map);
11162 map = isl_map_reverse(map);
11163 in = isl_map_plain_is_single_valued(map);
11164 isl_map_free(map);
11166 return in;
11169 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11171 isl_bool sv;
11173 sv = isl_map_is_single_valued(map);
11174 if (sv < 0 || !sv)
11175 return sv;
11177 return isl_map_is_injective(map);
11180 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11182 return isl_map_is_single_valued(set_to_map(set));
11185 /* Does "map" only map elements to themselves?
11187 * If the domain and range spaces are different, then "map"
11188 * is considered not to be an identity relation, even if it is empty.
11189 * Otherwise, construct the maximal identity relation and
11190 * check whether "map" is a subset of this relation.
11192 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11194 isl_space *space;
11195 isl_map *id;
11196 isl_bool equal, is_identity;
11198 space = isl_map_get_space(map);
11199 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11200 isl_space_free(space);
11201 if (equal < 0 || !equal)
11202 return equal;
11204 id = isl_map_identity(isl_map_get_space(map));
11205 is_identity = isl_map_is_subset(map, id);
11206 isl_map_free(id);
11208 return is_identity;
11211 int isl_map_is_translation(__isl_keep isl_map *map)
11213 int ok;
11214 isl_set *delta;
11216 delta = isl_map_deltas(isl_map_copy(map));
11217 ok = isl_set_is_singleton(delta);
11218 isl_set_free(delta);
11220 return ok;
11223 static int unique(isl_int *p, unsigned pos, unsigned len)
11225 if (isl_seq_first_non_zero(p, pos) != -1)
11226 return 0;
11227 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11228 return 0;
11229 return 1;
11232 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11234 int i, j;
11235 unsigned nvar;
11236 unsigned ovar;
11238 if (!bset)
11239 return isl_bool_error;
11241 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11242 return isl_bool_false;
11244 nvar = isl_basic_set_dim(bset, isl_dim_set);
11245 ovar = isl_space_offset(bset->dim, isl_dim_set);
11246 for (j = 0; j < nvar; ++j) {
11247 int lower = 0, upper = 0;
11248 for (i = 0; i < bset->n_eq; ++i) {
11249 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11250 continue;
11251 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11252 return isl_bool_false;
11253 break;
11255 if (i < bset->n_eq)
11256 continue;
11257 for (i = 0; i < bset->n_ineq; ++i) {
11258 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11259 continue;
11260 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11261 return isl_bool_false;
11262 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11263 lower = 1;
11264 else
11265 upper = 1;
11267 if (!lower || !upper)
11268 return isl_bool_false;
11271 return isl_bool_true;
11274 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11276 if (!set)
11277 return isl_bool_error;
11278 if (set->n != 1)
11279 return isl_bool_false;
11281 return isl_basic_set_is_box(set->p[0]);
11284 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11286 if (!bset)
11287 return isl_bool_error;
11289 return isl_space_is_wrapping(bset->dim);
11292 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11294 if (!set)
11295 return isl_bool_error;
11297 return isl_space_is_wrapping(set->dim);
11300 /* Modify the space of "map" through a call to "change".
11301 * If "can_change" is set (not NULL), then first call it to check
11302 * if the modification is allowed, printing the error message "cannot_change"
11303 * if it is not.
11305 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11306 isl_bool (*can_change)(__isl_keep isl_map *map),
11307 const char *cannot_change,
11308 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11310 isl_bool ok;
11311 isl_space *space;
11313 if (!map)
11314 return NULL;
11316 ok = can_change ? can_change(map) : isl_bool_true;
11317 if (ok < 0)
11318 return isl_map_free(map);
11319 if (!ok)
11320 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11321 return isl_map_free(map));
11323 space = change(isl_map_get_space(map));
11324 map = isl_map_reset_space(map, space);
11326 return map;
11329 /* Is the domain of "map" a wrapped relation?
11331 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11333 if (!map)
11334 return isl_bool_error;
11336 return isl_space_domain_is_wrapping(map->dim);
11339 /* Does "map" have a wrapped relation in both domain and range?
11341 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11343 return isl_space_is_product(isl_map_peek_space(map));
11346 /* Is the range of "map" a wrapped relation?
11348 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11350 if (!map)
11351 return isl_bool_error;
11353 return isl_space_range_is_wrapping(map->dim);
11356 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11358 bmap = isl_basic_map_cow(bmap);
11359 if (!bmap)
11360 return NULL;
11362 bmap->dim = isl_space_wrap(bmap->dim);
11363 if (!bmap->dim)
11364 goto error;
11366 bmap = isl_basic_map_finalize(bmap);
11368 return bset_from_bmap(bmap);
11369 error:
11370 isl_basic_map_free(bmap);
11371 return NULL;
11374 /* Given a map A -> B, return the set (A -> B).
11376 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11378 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11381 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11383 bset = isl_basic_set_cow(bset);
11384 if (!bset)
11385 return NULL;
11387 bset->dim = isl_space_unwrap(bset->dim);
11388 if (!bset->dim)
11389 goto error;
11391 bset = isl_basic_set_finalize(bset);
11393 return bset_to_bmap(bset);
11394 error:
11395 isl_basic_set_free(bset);
11396 return NULL;
11399 /* Given a set (A -> B), return the map A -> B.
11400 * Error out if "set" is not of the form (A -> B).
11402 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11404 return isl_map_change_space(set, &isl_set_is_wrapping,
11405 "not a wrapping set", &isl_space_unwrap);
11408 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11409 enum isl_dim_type type)
11411 if (!bmap)
11412 return NULL;
11414 if (!isl_space_is_named_or_nested(bmap->dim, type))
11415 return bmap;
11417 bmap = isl_basic_map_cow(bmap);
11418 if (!bmap)
11419 return NULL;
11421 bmap->dim = isl_space_reset(bmap->dim, type);
11422 if (!bmap->dim)
11423 goto error;
11425 bmap = isl_basic_map_finalize(bmap);
11427 return bmap;
11428 error:
11429 isl_basic_map_free(bmap);
11430 return NULL;
11433 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11434 enum isl_dim_type type)
11436 int i;
11438 if (!map)
11439 return NULL;
11441 if (!isl_space_is_named_or_nested(map->dim, type))
11442 return map;
11444 map = isl_map_cow(map);
11445 if (!map)
11446 return NULL;
11448 for (i = 0; i < map->n; ++i) {
11449 map->p[i] = isl_basic_map_reset(map->p[i], type);
11450 if (!map->p[i])
11451 goto error;
11453 map->dim = isl_space_reset(map->dim, type);
11454 if (!map->dim)
11455 goto error;
11457 return map;
11458 error:
11459 isl_map_free(map);
11460 return NULL;
11463 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11465 if (!bmap)
11466 return NULL;
11468 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11469 return bmap;
11471 bmap = isl_basic_map_cow(bmap);
11472 if (!bmap)
11473 return NULL;
11475 bmap->dim = isl_space_flatten(bmap->dim);
11476 if (!bmap->dim)
11477 goto error;
11479 bmap = isl_basic_map_finalize(bmap);
11481 return bmap;
11482 error:
11483 isl_basic_map_free(bmap);
11484 return NULL;
11487 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11489 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11492 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11493 __isl_take isl_basic_map *bmap)
11495 if (!bmap)
11496 return NULL;
11498 if (!bmap->dim->nested[0])
11499 return bmap;
11501 bmap = isl_basic_map_cow(bmap);
11502 if (!bmap)
11503 return NULL;
11505 bmap->dim = isl_space_flatten_domain(bmap->dim);
11506 if (!bmap->dim)
11507 goto error;
11509 bmap = isl_basic_map_finalize(bmap);
11511 return bmap;
11512 error:
11513 isl_basic_map_free(bmap);
11514 return NULL;
11517 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11518 __isl_take isl_basic_map *bmap)
11520 if (!bmap)
11521 return NULL;
11523 if (!bmap->dim->nested[1])
11524 return bmap;
11526 bmap = isl_basic_map_cow(bmap);
11527 if (!bmap)
11528 return NULL;
11530 bmap->dim = isl_space_flatten_range(bmap->dim);
11531 if (!bmap->dim)
11532 goto error;
11534 bmap = isl_basic_map_finalize(bmap);
11536 return bmap;
11537 error:
11538 isl_basic_map_free(bmap);
11539 return NULL;
11542 /* Remove any internal structure from the spaces of domain and range of "map".
11544 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11546 if (!map)
11547 return NULL;
11549 if (!map->dim->nested[0] && !map->dim->nested[1])
11550 return map;
11552 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11555 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11557 return set_from_map(isl_map_flatten(set_to_map(set)));
11560 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11562 isl_space *space, *flat_space;
11563 isl_map *map;
11565 space = isl_set_get_space(set);
11566 flat_space = isl_space_flatten(isl_space_copy(space));
11567 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11568 flat_space));
11569 map = isl_map_intersect_domain(map, set);
11571 return map;
11574 /* Remove any internal structure from the space of the domain of "map".
11576 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11578 if (!map)
11579 return NULL;
11581 if (!map->dim->nested[0])
11582 return map;
11584 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11587 /* Remove any internal structure from the space of the range of "map".
11589 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11591 if (!map)
11592 return NULL;
11594 if (!map->dim->nested[1])
11595 return map;
11597 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11600 /* Reorder the dimensions of "bmap" according to the given dim_map
11601 * and set the dimension specification to "space" and
11602 * perform Gaussian elimination on the result.
11604 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11605 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11607 isl_basic_map *res;
11608 unsigned flags;
11609 unsigned n_div;
11611 if (!bmap || !space || !dim_map)
11612 goto error;
11614 flags = bmap->flags;
11615 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11616 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11617 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11618 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11619 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11620 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11621 if (res)
11622 res->flags = flags;
11623 res = isl_basic_map_gauss(res, NULL);
11624 res = isl_basic_map_finalize(res);
11625 return res;
11626 error:
11627 free(dim_map);
11628 isl_basic_map_free(bmap);
11629 isl_space_free(space);
11630 return NULL;
11633 /* Reorder the dimensions of "map" according to given reordering.
11635 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11636 __isl_take isl_reordering *r)
11638 int i;
11639 struct isl_dim_map *dim_map;
11641 map = isl_map_cow(map);
11642 dim_map = isl_dim_map_from_reordering(r);
11643 if (!map || !r || !dim_map)
11644 goto error;
11646 for (i = 0; i < map->n; ++i) {
11647 struct isl_dim_map *dim_map_i;
11648 isl_space *space;
11650 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11652 space = isl_reordering_get_space(r);
11653 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11655 if (!map->p[i])
11656 goto error;
11659 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11660 map = isl_map_unmark_normalized(map);
11662 isl_reordering_free(r);
11663 free(dim_map);
11664 return map;
11665 error:
11666 free(dim_map);
11667 isl_map_free(map);
11668 isl_reordering_free(r);
11669 return NULL;
11672 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11673 __isl_take isl_reordering *r)
11675 return set_from_map(isl_map_realign(set_to_map(set), r));
11678 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11679 __isl_take isl_space *model)
11681 isl_ctx *ctx;
11682 isl_bool aligned;
11684 if (!map || !model)
11685 goto error;
11687 ctx = isl_space_get_ctx(model);
11688 if (!isl_space_has_named_params(model))
11689 isl_die(ctx, isl_error_invalid,
11690 "model has unnamed parameters", goto error);
11691 if (isl_map_check_named_params(map) < 0)
11692 goto error;
11693 aligned = isl_map_space_has_equal_params(map, model);
11694 if (aligned < 0)
11695 goto error;
11696 if (!aligned) {
11697 isl_reordering *exp;
11699 exp = isl_parameter_alignment_reordering(map->dim, model);
11700 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11701 map = isl_map_realign(map, exp);
11704 isl_space_free(model);
11705 return map;
11706 error:
11707 isl_space_free(model);
11708 isl_map_free(map);
11709 return NULL;
11712 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11713 __isl_take isl_space *model)
11715 return isl_map_align_params(set, model);
11718 /* Align the parameters of "bmap" to those of "model", introducing
11719 * additional parameters if needed.
11721 __isl_give isl_basic_map *isl_basic_map_align_params(
11722 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11724 isl_ctx *ctx;
11725 isl_bool equal_params;
11727 if (!bmap || !model)
11728 goto error;
11730 ctx = isl_space_get_ctx(model);
11731 if (!isl_space_has_named_params(model))
11732 isl_die(ctx, isl_error_invalid,
11733 "model has unnamed parameters", goto error);
11734 if (isl_basic_map_check_named_params(bmap) < 0)
11735 goto error;
11736 equal_params = isl_space_has_equal_params(bmap->dim, model);
11737 if (equal_params < 0)
11738 goto error;
11739 if (!equal_params) {
11740 isl_reordering *exp;
11741 struct isl_dim_map *dim_map;
11743 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11744 exp = isl_reordering_extend_space(exp,
11745 isl_basic_map_get_space(bmap));
11746 dim_map = isl_dim_map_from_reordering(exp);
11747 bmap = isl_basic_map_realign(bmap,
11748 isl_reordering_get_space(exp),
11749 isl_dim_map_extend(dim_map, bmap));
11750 isl_reordering_free(exp);
11751 free(dim_map);
11754 isl_space_free(model);
11755 return bmap;
11756 error:
11757 isl_space_free(model);
11758 isl_basic_map_free(bmap);
11759 return NULL;
11762 /* Do "bset" and "space" have the same parameters?
11764 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11765 __isl_keep isl_space *space)
11767 isl_space *bset_space;
11769 bset_space = isl_basic_set_peek_space(bset);
11770 return isl_space_has_equal_params(bset_space, space);
11773 /* Do "map" and "space" have the same parameters?
11775 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11776 __isl_keep isl_space *space)
11778 isl_space *map_space;
11780 map_space = isl_map_peek_space(map);
11781 return isl_space_has_equal_params(map_space, space);
11784 /* Do "set" and "space" have the same parameters?
11786 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11787 __isl_keep isl_space *space)
11789 return isl_map_space_has_equal_params(set_to_map(set), space);
11792 /* Align the parameters of "bset" to those of "model", introducing
11793 * additional parameters if needed.
11795 __isl_give isl_basic_set *isl_basic_set_align_params(
11796 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11798 return isl_basic_map_align_params(bset, model);
11801 /* Drop all parameters not referenced by "map".
11803 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11805 int i;
11807 if (isl_map_check_named_params(map) < 0)
11808 return isl_map_free(map);
11810 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11811 isl_bool involves;
11813 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11814 if (involves < 0)
11815 return isl_map_free(map);
11816 if (!involves)
11817 map = isl_map_project_out(map, isl_dim_param, i, 1);
11820 return map;
11823 /* Drop all parameters not referenced by "set".
11825 __isl_give isl_set *isl_set_drop_unused_params(
11826 __isl_take isl_set *set)
11828 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11831 /* Drop all parameters not referenced by "bmap".
11833 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11834 __isl_take isl_basic_map *bmap)
11836 int i;
11838 if (isl_basic_map_check_named_params(bmap) < 0)
11839 return isl_basic_map_free(bmap);
11841 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11842 isl_bool involves;
11844 involves = isl_basic_map_involves_dims(bmap,
11845 isl_dim_param, i, 1);
11846 if (involves < 0)
11847 return isl_basic_map_free(bmap);
11848 if (!involves)
11849 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11852 return bmap;
11855 /* Drop all parameters not referenced by "bset".
11857 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11858 __isl_take isl_basic_set *bset)
11860 return bset_from_bmap(isl_basic_map_drop_unused_params(
11861 bset_to_bmap(bset)));
11864 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11865 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11866 enum isl_dim_type c2, enum isl_dim_type c3,
11867 enum isl_dim_type c4, enum isl_dim_type c5)
11869 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11870 struct isl_mat *mat;
11871 int i, j, k;
11872 int pos;
11874 if (!bmap)
11875 return NULL;
11876 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11877 isl_basic_map_total_dim(bmap) + 1);
11878 if (!mat)
11879 return NULL;
11880 for (i = 0; i < bmap->n_eq; ++i)
11881 for (j = 0, pos = 0; j < 5; ++j) {
11882 int off = isl_basic_map_offset(bmap, c[j]);
11883 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11884 isl_int_set(mat->row[i][pos],
11885 bmap->eq[i][off + k]);
11886 ++pos;
11890 return mat;
11893 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11894 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11895 enum isl_dim_type c2, enum isl_dim_type c3,
11896 enum isl_dim_type c4, enum isl_dim_type c5)
11898 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11899 struct isl_mat *mat;
11900 int i, j, k;
11901 int pos;
11903 if (!bmap)
11904 return NULL;
11905 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11906 isl_basic_map_total_dim(bmap) + 1);
11907 if (!mat)
11908 return NULL;
11909 for (i = 0; i < bmap->n_ineq; ++i)
11910 for (j = 0, pos = 0; j < 5; ++j) {
11911 int off = isl_basic_map_offset(bmap, c[j]);
11912 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11913 isl_int_set(mat->row[i][pos],
11914 bmap->ineq[i][off + k]);
11915 ++pos;
11919 return mat;
11922 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11923 __isl_take isl_space *space,
11924 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11925 enum isl_dim_type c2, enum isl_dim_type c3,
11926 enum isl_dim_type c4, enum isl_dim_type c5)
11928 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11929 isl_basic_map *bmap = NULL;
11930 unsigned total;
11931 unsigned extra;
11932 int i, j, k, l;
11933 int pos;
11935 if (!space || !eq || !ineq)
11936 goto error;
11938 if (eq->n_col != ineq->n_col)
11939 isl_die(space->ctx, isl_error_invalid,
11940 "equalities and inequalities matrices should have "
11941 "same number of columns", goto error);
11943 total = 1 + isl_space_dim(space, isl_dim_all);
11945 if (eq->n_col < total)
11946 isl_die(space->ctx, isl_error_invalid,
11947 "number of columns too small", goto error);
11949 extra = eq->n_col - total;
11951 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11952 eq->n_row, ineq->n_row);
11953 if (!bmap)
11954 goto error;
11955 for (i = 0; i < extra; ++i) {
11956 k = isl_basic_map_alloc_div(bmap);
11957 if (k < 0)
11958 goto error;
11959 isl_int_set_si(bmap->div[k][0], 0);
11961 for (i = 0; i < eq->n_row; ++i) {
11962 l = isl_basic_map_alloc_equality(bmap);
11963 if (l < 0)
11964 goto error;
11965 for (j = 0, pos = 0; j < 5; ++j) {
11966 int off = isl_basic_map_offset(bmap, c[j]);
11967 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11968 isl_int_set(bmap->eq[l][off + k],
11969 eq->row[i][pos]);
11970 ++pos;
11974 for (i = 0; i < ineq->n_row; ++i) {
11975 l = isl_basic_map_alloc_inequality(bmap);
11976 if (l < 0)
11977 goto error;
11978 for (j = 0, pos = 0; j < 5; ++j) {
11979 int off = isl_basic_map_offset(bmap, c[j]);
11980 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11981 isl_int_set(bmap->ineq[l][off + k],
11982 ineq->row[i][pos]);
11983 ++pos;
11988 isl_space_free(space);
11989 isl_mat_free(eq);
11990 isl_mat_free(ineq);
11992 bmap = isl_basic_map_simplify(bmap);
11993 return isl_basic_map_finalize(bmap);
11994 error:
11995 isl_space_free(space);
11996 isl_mat_free(eq);
11997 isl_mat_free(ineq);
11998 isl_basic_map_free(bmap);
11999 return NULL;
12002 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12003 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12004 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12006 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12007 c1, c2, c3, c4, isl_dim_in);
12010 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12011 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12012 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12014 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12015 c1, c2, c3, c4, isl_dim_in);
12018 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12019 __isl_take isl_space *dim,
12020 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12021 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12023 isl_basic_map *bmap;
12024 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12025 c1, c2, c3, c4, isl_dim_in);
12026 return bset_from_bmap(bmap);
12029 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12031 if (!bmap)
12032 return isl_bool_error;
12034 return isl_space_can_zip(bmap->dim);
12037 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12039 if (!map)
12040 return isl_bool_error;
12042 return isl_space_can_zip(map->dim);
12045 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12046 * (A -> C) -> (B -> D).
12048 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12050 unsigned pos;
12051 unsigned n1;
12052 unsigned n2;
12054 if (!bmap)
12055 return NULL;
12057 if (!isl_basic_map_can_zip(bmap))
12058 isl_die(bmap->ctx, isl_error_invalid,
12059 "basic map cannot be zipped", goto error);
12060 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12061 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12062 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12063 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12064 bmap = isl_basic_map_cow(bmap);
12065 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12066 if (!bmap)
12067 return NULL;
12068 bmap->dim = isl_space_zip(bmap->dim);
12069 if (!bmap->dim)
12070 goto error;
12071 bmap = isl_basic_map_mark_final(bmap);
12072 return bmap;
12073 error:
12074 isl_basic_map_free(bmap);
12075 return NULL;
12078 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12079 * (A -> C) -> (B -> D).
12081 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12083 int i;
12085 if (!map)
12086 return NULL;
12088 if (!isl_map_can_zip(map))
12089 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12090 goto error);
12092 map = isl_map_cow(map);
12093 if (!map)
12094 return NULL;
12096 for (i = 0; i < map->n; ++i) {
12097 map->p[i] = isl_basic_map_zip(map->p[i]);
12098 if (!map->p[i])
12099 goto error;
12102 map->dim = isl_space_zip(map->dim);
12103 if (!map->dim)
12104 goto error;
12106 return map;
12107 error:
12108 isl_map_free(map);
12109 return NULL;
12112 /* Can we apply isl_basic_map_curry to "bmap"?
12113 * That is, does it have a nested relation in its domain?
12115 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12117 if (!bmap)
12118 return isl_bool_error;
12120 return isl_space_can_curry(bmap->dim);
12123 /* Can we apply isl_map_curry to "map"?
12124 * That is, does it have a nested relation in its domain?
12126 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12128 if (!map)
12129 return isl_bool_error;
12131 return isl_space_can_curry(map->dim);
12134 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12135 * A -> (B -> C).
12137 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12140 if (!bmap)
12141 return NULL;
12143 if (!isl_basic_map_can_curry(bmap))
12144 isl_die(bmap->ctx, isl_error_invalid,
12145 "basic map cannot be curried", goto error);
12146 bmap = isl_basic_map_cow(bmap);
12147 if (!bmap)
12148 return NULL;
12149 bmap->dim = isl_space_curry(bmap->dim);
12150 if (!bmap->dim)
12151 goto error;
12152 bmap = isl_basic_map_mark_final(bmap);
12153 return bmap;
12154 error:
12155 isl_basic_map_free(bmap);
12156 return NULL;
12159 /* Given a map (A -> B) -> C, return the corresponding map
12160 * A -> (B -> C).
12162 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12164 return isl_map_change_space(map, &isl_map_can_curry,
12165 "map cannot be curried", &isl_space_curry);
12168 /* Can isl_map_range_curry be applied to "map"?
12169 * That is, does it have a nested relation in its range,
12170 * the domain of which is itself a nested relation?
12172 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12174 if (!map)
12175 return isl_bool_error;
12177 return isl_space_can_range_curry(map->dim);
12180 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12181 * A -> (B -> (C -> D)).
12183 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12185 return isl_map_change_space(map, &isl_map_can_range_curry,
12186 "map range cannot be curried",
12187 &isl_space_range_curry);
12190 /* Can we apply isl_basic_map_uncurry to "bmap"?
12191 * That is, does it have a nested relation in its domain?
12193 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12195 if (!bmap)
12196 return isl_bool_error;
12198 return isl_space_can_uncurry(bmap->dim);
12201 /* Can we apply isl_map_uncurry to "map"?
12202 * That is, does it have a nested relation in its domain?
12204 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12206 if (!map)
12207 return isl_bool_error;
12209 return isl_space_can_uncurry(map->dim);
12212 /* Given a basic map A -> (B -> C), return the corresponding basic map
12213 * (A -> B) -> C.
12215 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12218 if (!bmap)
12219 return NULL;
12221 if (!isl_basic_map_can_uncurry(bmap))
12222 isl_die(bmap->ctx, isl_error_invalid,
12223 "basic map cannot be uncurried",
12224 return isl_basic_map_free(bmap));
12225 bmap = isl_basic_map_cow(bmap);
12226 if (!bmap)
12227 return NULL;
12228 bmap->dim = isl_space_uncurry(bmap->dim);
12229 if (!bmap->dim)
12230 return isl_basic_map_free(bmap);
12231 bmap = isl_basic_map_mark_final(bmap);
12232 return bmap;
12235 /* Given a map A -> (B -> C), return the corresponding map
12236 * (A -> B) -> C.
12238 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12240 return isl_map_change_space(map, &isl_map_can_uncurry,
12241 "map cannot be uncurried", &isl_space_uncurry);
12244 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12245 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12247 return isl_map_equate(set, type1, pos1, type2, pos2);
12250 /* Construct a basic map where the given dimensions are equal to each other.
12252 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12253 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12255 isl_basic_map *bmap = NULL;
12256 int i;
12258 if (!space)
12259 return NULL;
12261 if (pos1 >= isl_space_dim(space, type1))
12262 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12263 "index out of bounds", goto error);
12264 if (pos2 >= isl_space_dim(space, type2))
12265 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12266 "index out of bounds", 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 (!map)
12326 return NULL;
12328 if (pos1 >= isl_map_dim(map, type1))
12329 isl_die(map->ctx, isl_error_invalid,
12330 "index out of bounds", goto error);
12331 if (pos2 >= isl_map_dim(map, type2))
12332 isl_die(map->ctx, isl_error_invalid,
12333 "index out of bounds", goto error);
12335 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12336 i = isl_basic_map_alloc_equality(bmap);
12337 if (i < 0)
12338 goto error;
12339 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12340 pos1 += isl_basic_map_offset(bmap, type1);
12341 pos2 += isl_basic_map_offset(bmap, type2);
12342 isl_int_set_si(bmap->eq[i][pos1], 1);
12343 isl_int_set_si(bmap->eq[i][pos2], 1);
12344 bmap = isl_basic_map_finalize(bmap);
12346 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12348 return map;
12349 error:
12350 isl_basic_map_free(bmap);
12351 isl_map_free(map);
12352 return NULL;
12355 /* Construct a constraint imposing that the value of the first dimension is
12356 * greater than or equal to that of the second.
12358 static __isl_give isl_constraint *constraint_order_ge(
12359 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12360 enum isl_dim_type type2, int pos2)
12362 isl_constraint *c;
12364 if (!space)
12365 return NULL;
12367 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12369 if (pos1 >= isl_constraint_dim(c, type1))
12370 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12371 "index out of bounds", return isl_constraint_free(c));
12372 if (pos2 >= isl_constraint_dim(c, type2))
12373 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12374 "index out of bounds", return isl_constraint_free(c));
12376 if (type1 == type2 && pos1 == pos2)
12377 return c;
12379 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12380 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12382 return c;
12385 /* Add a constraint imposing that the value of the first dimension is
12386 * greater than or equal to that of the second.
12388 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12389 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12391 isl_constraint *c;
12392 isl_space *space;
12394 if (type1 == type2 && pos1 == pos2)
12395 return bmap;
12396 space = isl_basic_map_get_space(bmap);
12397 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12398 bmap = isl_basic_map_add_constraint(bmap, c);
12400 return bmap;
12403 /* Add a constraint imposing that the value of the first dimension is
12404 * greater than or equal to that of the second.
12406 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12407 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12409 isl_constraint *c;
12410 isl_space *space;
12412 if (type1 == type2 && pos1 == pos2)
12413 return map;
12414 space = isl_map_get_space(map);
12415 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12416 map = isl_map_add_constraint(map, c);
12418 return map;
12421 /* Add a constraint imposing that the value of the first dimension is
12422 * less than or equal to that of the second.
12424 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12425 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12427 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12430 /* Construct a basic map where the value of the first dimension is
12431 * greater than that of the second.
12433 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12434 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12436 isl_basic_map *bmap = NULL;
12437 int i;
12439 if (!space)
12440 return NULL;
12442 if (pos1 >= isl_space_dim(space, type1))
12443 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12444 "index out of bounds", goto error);
12445 if (pos2 >= isl_space_dim(space, type2))
12446 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12447 "index out of bounds", goto error);
12449 if (type1 == type2 && pos1 == pos2)
12450 return isl_basic_map_empty(space);
12452 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12453 i = isl_basic_map_alloc_inequality(bmap);
12454 if (i < 0)
12455 return isl_basic_map_free(bmap);
12456 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12457 pos1 += isl_basic_map_offset(bmap, type1);
12458 pos2 += isl_basic_map_offset(bmap, type2);
12459 isl_int_set_si(bmap->ineq[i][pos1], 1);
12460 isl_int_set_si(bmap->ineq[i][pos2], -1);
12461 isl_int_set_si(bmap->ineq[i][0], -1);
12462 bmap = isl_basic_map_finalize(bmap);
12464 return bmap;
12465 error:
12466 isl_space_free(space);
12467 isl_basic_map_free(bmap);
12468 return NULL;
12471 /* Add a constraint imposing that the value of the first dimension is
12472 * greater than that of the second.
12474 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12475 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12477 isl_basic_map *gt;
12479 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12481 bmap = isl_basic_map_intersect(bmap, gt);
12483 return bmap;
12486 /* Add a constraint imposing that the value of the first dimension is
12487 * greater than that of the second.
12489 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12490 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12492 isl_basic_map *bmap;
12494 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12496 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12498 return map;
12501 /* Add a constraint imposing that the value of the first dimension is
12502 * smaller than that of the second.
12504 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12505 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12507 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12510 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12511 int pos)
12513 isl_aff *div;
12514 isl_local_space *ls;
12516 if (!bmap)
12517 return NULL;
12519 if (!isl_basic_map_divs_known(bmap))
12520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12521 "some divs are unknown", return NULL);
12523 ls = isl_basic_map_get_local_space(bmap);
12524 div = isl_local_space_get_div(ls, pos);
12525 isl_local_space_free(ls);
12527 return div;
12530 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12531 int pos)
12533 return isl_basic_map_get_div(bset, pos);
12536 /* Plug in "subs" for dimension "type", "pos" of "bset".
12538 * Let i be the dimension to replace and let "subs" be of the form
12540 * f/d
12542 * Any integer division with a non-zero coefficient for i,
12544 * floor((a i + g)/m)
12546 * is replaced by
12548 * floor((a f + d g)/(m d))
12550 * Constraints of the form
12552 * a i + g
12554 * are replaced by
12556 * a f + d g
12558 * We currently require that "subs" is an integral expression.
12559 * Handling rational expressions may require us to add stride constraints
12560 * as we do in isl_basic_set_preimage_multi_aff.
12562 __isl_give isl_basic_set *isl_basic_set_substitute(
12563 __isl_take isl_basic_set *bset,
12564 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12566 int i;
12567 isl_int v;
12568 isl_ctx *ctx;
12570 if (bset && isl_basic_set_plain_is_empty(bset))
12571 return bset;
12573 bset = isl_basic_set_cow(bset);
12574 if (!bset || !subs)
12575 goto error;
12577 ctx = isl_basic_set_get_ctx(bset);
12578 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12579 isl_die(ctx, isl_error_invalid,
12580 "spaces don't match", goto error);
12581 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12582 isl_die(ctx, isl_error_unsupported,
12583 "cannot handle divs yet", goto error);
12584 if (!isl_int_is_one(subs->v->el[0]))
12585 isl_die(ctx, isl_error_invalid,
12586 "can only substitute integer expressions", goto error);
12588 pos += isl_basic_set_offset(bset, type);
12590 isl_int_init(v);
12592 for (i = 0; i < bset->n_eq; ++i) {
12593 if (isl_int_is_zero(bset->eq[i][pos]))
12594 continue;
12595 isl_int_set(v, bset->eq[i][pos]);
12596 isl_int_set_si(bset->eq[i][pos], 0);
12597 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12598 v, subs->v->el + 1, subs->v->size - 1);
12601 for (i = 0; i < bset->n_ineq; ++i) {
12602 if (isl_int_is_zero(bset->ineq[i][pos]))
12603 continue;
12604 isl_int_set(v, bset->ineq[i][pos]);
12605 isl_int_set_si(bset->ineq[i][pos], 0);
12606 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12607 v, subs->v->el + 1, subs->v->size - 1);
12610 for (i = 0; i < bset->n_div; ++i) {
12611 if (isl_int_is_zero(bset->div[i][1 + pos]))
12612 continue;
12613 isl_int_set(v, bset->div[i][1 + pos]);
12614 isl_int_set_si(bset->div[i][1 + pos], 0);
12615 isl_seq_combine(bset->div[i] + 1,
12616 subs->v->el[0], bset->div[i] + 1,
12617 v, subs->v->el + 1, subs->v->size - 1);
12618 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12621 isl_int_clear(v);
12623 bset = isl_basic_set_simplify(bset);
12624 return isl_basic_set_finalize(bset);
12625 error:
12626 isl_basic_set_free(bset);
12627 return NULL;
12630 /* Plug in "subs" for dimension "type", "pos" of "set".
12632 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12633 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12635 int i;
12637 if (set && isl_set_plain_is_empty(set))
12638 return set;
12640 set = isl_set_cow(set);
12641 if (!set || !subs)
12642 goto error;
12644 for (i = set->n - 1; i >= 0; --i) {
12645 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12646 set = set_from_map(remove_if_empty(set_to_map(set), i));
12647 if (!set)
12648 return NULL;
12651 return set;
12652 error:
12653 isl_set_free(set);
12654 return NULL;
12657 /* Check if the range of "ma" is compatible with the domain or range
12658 * (depending on "type") of "bmap".
12660 static isl_stat check_basic_map_compatible_range_multi_aff(
12661 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12662 __isl_keep isl_multi_aff *ma)
12664 isl_bool m;
12665 isl_space *ma_space;
12667 ma_space = isl_multi_aff_get_space(ma);
12669 m = isl_space_has_equal_params(bmap->dim, ma_space);
12670 if (m < 0)
12671 goto error;
12672 if (!m)
12673 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12674 "parameters don't match", goto error);
12675 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12676 if (m < 0)
12677 goto error;
12678 if (!m)
12679 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12680 "spaces don't match", goto error);
12682 isl_space_free(ma_space);
12683 return isl_stat_ok;
12684 error:
12685 isl_space_free(ma_space);
12686 return isl_stat_error;
12689 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12690 * coefficients before the transformed range of dimensions,
12691 * the "n_after" coefficients after the transformed range of dimensions
12692 * and the coefficients of the other divs in "bmap".
12694 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12695 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12697 int i;
12698 int n_param;
12699 int n_set;
12700 isl_local_space *ls;
12702 if (n_div == 0)
12703 return 0;
12705 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12706 if (!ls)
12707 return -1;
12709 n_param = isl_local_space_dim(ls, isl_dim_param);
12710 n_set = isl_local_space_dim(ls, isl_dim_set);
12711 for (i = 0; i < n_div; ++i) {
12712 int o_bmap = 0, o_ls = 0;
12714 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12715 o_bmap += 1 + 1 + n_param;
12716 o_ls += 1 + 1 + n_param;
12717 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12718 o_bmap += n_before;
12719 isl_seq_cpy(bmap->div[i] + o_bmap,
12720 ls->div->row[i] + o_ls, n_set);
12721 o_bmap += n_set;
12722 o_ls += n_set;
12723 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12724 o_bmap += n_after;
12725 isl_seq_cpy(bmap->div[i] + o_bmap,
12726 ls->div->row[i] + o_ls, n_div);
12727 o_bmap += n_div;
12728 o_ls += n_div;
12729 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12730 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12731 goto error;
12734 isl_local_space_free(ls);
12735 return 0;
12736 error:
12737 isl_local_space_free(ls);
12738 return -1;
12741 /* How many stride constraints does "ma" enforce?
12742 * That is, how many of the affine expressions have a denominator
12743 * different from one?
12745 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12747 int i;
12748 int strides = 0;
12750 for (i = 0; i < ma->n; ++i)
12751 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12752 strides++;
12754 return strides;
12757 /* For each affine expression in ma of the form
12759 * x_i = (f_i y + h_i)/m_i
12761 * with m_i different from one, add a constraint to "bmap"
12762 * of the form
12764 * f_i y + h_i = m_i alpha_i
12766 * with alpha_i an additional existentially quantified variable.
12768 * The input variables of "ma" correspond to a subset of the variables
12769 * of "bmap". There are "n_before" variables in "bmap" before this
12770 * subset and "n_after" variables after this subset.
12771 * The integer divisions of the affine expressions in "ma" are assumed
12772 * to have been aligned. There are "n_div_ma" of them and
12773 * they appear first in "bmap", straight after the "n_after" variables.
12775 static __isl_give isl_basic_map *add_ma_strides(
12776 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12777 int n_before, int n_after, int n_div_ma)
12779 int i, k;
12780 int div;
12781 int total;
12782 int n_param;
12783 int n_in;
12785 total = isl_basic_map_total_dim(bmap);
12786 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12787 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12788 for (i = 0; i < ma->n; ++i) {
12789 int o_bmap = 0, o_ma = 1;
12791 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12792 continue;
12793 div = isl_basic_map_alloc_div(bmap);
12794 k = isl_basic_map_alloc_equality(bmap);
12795 if (div < 0 || k < 0)
12796 goto error;
12797 isl_int_set_si(bmap->div[div][0], 0);
12798 isl_seq_cpy(bmap->eq[k] + o_bmap,
12799 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12800 o_bmap += 1 + n_param;
12801 o_ma += 1 + n_param;
12802 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12803 o_bmap += n_before;
12804 isl_seq_cpy(bmap->eq[k] + o_bmap,
12805 ma->u.p[i]->v->el + o_ma, n_in);
12806 o_bmap += n_in;
12807 o_ma += n_in;
12808 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12809 o_bmap += n_after;
12810 isl_seq_cpy(bmap->eq[k] + o_bmap,
12811 ma->u.p[i]->v->el + o_ma, n_div_ma);
12812 o_bmap += n_div_ma;
12813 o_ma += n_div_ma;
12814 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12815 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12816 total++;
12819 return bmap;
12820 error:
12821 isl_basic_map_free(bmap);
12822 return NULL;
12825 /* Replace the domain or range space (depending on "type) of "space" by "set".
12827 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12828 enum isl_dim_type type, __isl_take isl_space *set)
12830 if (type == isl_dim_in) {
12831 space = isl_space_range(space);
12832 space = isl_space_map_from_domain_and_range(set, space);
12833 } else {
12834 space = isl_space_domain(space);
12835 space = isl_space_map_from_domain_and_range(space, set);
12838 return space;
12841 /* Compute the preimage of the domain or range (depending on "type")
12842 * of "bmap" under the function represented by "ma".
12843 * In other words, plug in "ma" in the domain or range of "bmap".
12844 * The result is a basic map that lives in the same space as "bmap"
12845 * except that the domain or range has been replaced by
12846 * the domain space of "ma".
12848 * If bmap is represented by
12850 * A(p) + S u + B x + T v + C(divs) >= 0,
12852 * where u and x are input and output dimensions if type == isl_dim_out
12853 * while x and v are input and output dimensions if type == isl_dim_in,
12854 * and ma is represented by
12856 * x = D(p) + F(y) + G(divs')
12858 * then the result is
12860 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12862 * The divs in the input set are similarly adjusted.
12863 * In particular
12865 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12867 * becomes
12869 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12870 * B_i G(divs') + c_i(divs))/n_i)
12872 * If bmap is not a rational map and if F(y) involves any denominators
12874 * x_i = (f_i y + h_i)/m_i
12876 * then additional constraints are added to ensure that we only
12877 * map back integer points. That is we enforce
12879 * f_i y + h_i = m_i alpha_i
12881 * with alpha_i an additional existentially quantified variable.
12883 * We first copy over the divs from "ma".
12884 * Then we add the modified constraints and divs from "bmap".
12885 * Finally, we add the stride constraints, if needed.
12887 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12888 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12889 __isl_take isl_multi_aff *ma)
12891 int i, k;
12892 isl_space *space;
12893 isl_basic_map *res = NULL;
12894 int n_before, n_after, n_div_bmap, n_div_ma;
12895 isl_int f, c1, c2, g;
12896 isl_bool rational;
12897 int strides;
12899 isl_int_init(f);
12900 isl_int_init(c1);
12901 isl_int_init(c2);
12902 isl_int_init(g);
12904 ma = isl_multi_aff_align_divs(ma);
12905 if (!bmap || !ma)
12906 goto error;
12907 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12908 goto error;
12910 if (type == isl_dim_in) {
12911 n_before = 0;
12912 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12913 } else {
12914 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12915 n_after = 0;
12917 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12918 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12920 space = isl_multi_aff_get_domain_space(ma);
12921 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12922 rational = isl_basic_map_is_rational(bmap);
12923 strides = rational ? 0 : multi_aff_strides(ma);
12924 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12925 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12926 if (rational)
12927 res = isl_basic_map_set_rational(res);
12929 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12930 if (isl_basic_map_alloc_div(res) < 0)
12931 goto error;
12933 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12934 goto error;
12936 for (i = 0; i < bmap->n_eq; ++i) {
12937 k = isl_basic_map_alloc_equality(res);
12938 if (k < 0)
12939 goto error;
12940 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12941 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12944 for (i = 0; i < bmap->n_ineq; ++i) {
12945 k = isl_basic_map_alloc_inequality(res);
12946 if (k < 0)
12947 goto error;
12948 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12949 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12952 for (i = 0; i < bmap->n_div; ++i) {
12953 if (isl_int_is_zero(bmap->div[i][0])) {
12954 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12955 continue;
12957 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12958 n_before, n_after, n_div_ma, n_div_bmap,
12959 f, c1, c2, g, 1);
12962 if (strides)
12963 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12965 isl_int_clear(f);
12966 isl_int_clear(c1);
12967 isl_int_clear(c2);
12968 isl_int_clear(g);
12969 isl_basic_map_free(bmap);
12970 isl_multi_aff_free(ma);
12971 res = isl_basic_map_simplify(res);
12972 return isl_basic_map_finalize(res);
12973 error:
12974 isl_int_clear(f);
12975 isl_int_clear(c1);
12976 isl_int_clear(c2);
12977 isl_int_clear(g);
12978 isl_basic_map_free(bmap);
12979 isl_multi_aff_free(ma);
12980 isl_basic_map_free(res);
12981 return NULL;
12984 /* Compute the preimage of "bset" under the function represented by "ma".
12985 * In other words, plug in "ma" in "bset". The result is a basic set
12986 * that lives in the domain space of "ma".
12988 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12989 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12991 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12994 /* Compute the preimage of the domain of "bmap" under the function
12995 * represented by "ma".
12996 * In other words, plug in "ma" in the domain of "bmap".
12997 * The result is a basic map that lives in the same space as "bmap"
12998 * except that the domain has been replaced by the domain space of "ma".
13000 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13001 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13003 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13006 /* Compute the preimage of the range of "bmap" under the function
13007 * represented by "ma".
13008 * In other words, plug in "ma" in the range of "bmap".
13009 * The result is a basic map that lives in the same space as "bmap"
13010 * except that the range has been replaced by the domain space of "ma".
13012 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13013 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13015 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13018 /* Check if the range of "ma" is compatible with the domain or range
13019 * (depending on "type") of "map".
13020 * Return isl_stat_error if anything is wrong.
13022 static isl_stat check_map_compatible_range_multi_aff(
13023 __isl_keep isl_map *map, enum isl_dim_type type,
13024 __isl_keep isl_multi_aff *ma)
13026 isl_bool m;
13027 isl_space *ma_space;
13029 ma_space = isl_multi_aff_get_space(ma);
13030 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13031 isl_space_free(ma_space);
13032 if (m < 0)
13033 return isl_stat_error;
13034 if (!m)
13035 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13036 "spaces don't match", return isl_stat_error);
13037 return isl_stat_ok;
13040 /* Compute the preimage of the domain or range (depending on "type")
13041 * of "map" under the function represented by "ma".
13042 * In other words, plug in "ma" in the domain or range of "map".
13043 * The result is a map that lives in the same space as "map"
13044 * except that the domain or range has been replaced by
13045 * the domain space of "ma".
13047 * The parameters are assumed to have been aligned.
13049 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13050 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13052 int i;
13053 isl_space *space;
13055 map = isl_map_cow(map);
13056 ma = isl_multi_aff_align_divs(ma);
13057 if (!map || !ma)
13058 goto error;
13059 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13060 goto error;
13062 for (i = 0; i < map->n; ++i) {
13063 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13064 isl_multi_aff_copy(ma));
13065 if (!map->p[i])
13066 goto error;
13069 space = isl_multi_aff_get_domain_space(ma);
13070 space = isl_space_set(isl_map_get_space(map), type, space);
13072 isl_space_free(map->dim);
13073 map->dim = space;
13074 if (!map->dim)
13075 goto error;
13077 isl_multi_aff_free(ma);
13078 if (map->n > 1)
13079 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13080 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13081 return map;
13082 error:
13083 isl_multi_aff_free(ma);
13084 isl_map_free(map);
13085 return NULL;
13088 /* Compute the preimage of the domain or range (depending on "type")
13089 * of "map" under the function represented by "ma".
13090 * In other words, plug in "ma" in the domain or range of "map".
13091 * The result is a map that lives in the same space as "map"
13092 * except that the domain or range has been replaced by
13093 * the domain space of "ma".
13095 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13096 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13098 isl_bool aligned;
13100 if (!map || !ma)
13101 goto error;
13103 aligned = isl_map_space_has_equal_params(map, ma->space);
13104 if (aligned < 0)
13105 goto error;
13106 if (aligned)
13107 return map_preimage_multi_aff(map, type, ma);
13109 if (isl_map_check_named_params(map) < 0)
13110 goto error;
13111 if (!isl_space_has_named_params(ma->space))
13112 isl_die(map->ctx, isl_error_invalid,
13113 "unaligned unnamed parameters", goto error);
13114 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13115 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13117 return map_preimage_multi_aff(map, type, ma);
13118 error:
13119 isl_multi_aff_free(ma);
13120 return isl_map_free(map);
13123 /* Compute the preimage of "set" under the function represented by "ma".
13124 * In other words, plug in "ma" in "set". The result is a set
13125 * that lives in the domain space of "ma".
13127 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13128 __isl_take isl_multi_aff *ma)
13130 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13133 /* Compute the preimage of the domain of "map" under the function
13134 * represented by "ma".
13135 * In other words, plug in "ma" in the domain of "map".
13136 * The result is a map that lives in the same space as "map"
13137 * except that the domain has been replaced by the domain space of "ma".
13139 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13140 __isl_take isl_multi_aff *ma)
13142 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13145 /* Compute the preimage of the range of "map" under the function
13146 * represented by "ma".
13147 * In other words, plug in "ma" in the range of "map".
13148 * The result is a map that lives in the same space as "map"
13149 * except that the range has been replaced by the domain space of "ma".
13151 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13152 __isl_take isl_multi_aff *ma)
13154 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13157 /* Compute the preimage of "map" under the function represented by "pma".
13158 * In other words, plug in "pma" in the domain or range of "map".
13159 * The result is a map that lives in the same space as "map",
13160 * except that the space of type "type" has been replaced by
13161 * the domain space of "pma".
13163 * The parameters of "map" and "pma" are assumed to have been aligned.
13165 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13166 __isl_take isl_map *map, enum isl_dim_type type,
13167 __isl_take isl_pw_multi_aff *pma)
13169 int i;
13170 isl_map *res;
13172 if (!pma)
13173 goto error;
13175 if (pma->n == 0) {
13176 isl_pw_multi_aff_free(pma);
13177 res = isl_map_empty(isl_map_get_space(map));
13178 isl_map_free(map);
13179 return res;
13182 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13183 isl_multi_aff_copy(pma->p[0].maff));
13184 if (type == isl_dim_in)
13185 res = isl_map_intersect_domain(res,
13186 isl_map_copy(pma->p[0].set));
13187 else
13188 res = isl_map_intersect_range(res,
13189 isl_map_copy(pma->p[0].set));
13191 for (i = 1; i < pma->n; ++i) {
13192 isl_map *res_i;
13194 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13195 isl_multi_aff_copy(pma->p[i].maff));
13196 if (type == isl_dim_in)
13197 res_i = isl_map_intersect_domain(res_i,
13198 isl_map_copy(pma->p[i].set));
13199 else
13200 res_i = isl_map_intersect_range(res_i,
13201 isl_map_copy(pma->p[i].set));
13202 res = isl_map_union(res, res_i);
13205 isl_pw_multi_aff_free(pma);
13206 isl_map_free(map);
13207 return res;
13208 error:
13209 isl_pw_multi_aff_free(pma);
13210 isl_map_free(map);
13211 return NULL;
13214 /* Compute the preimage of "map" under the function represented by "pma".
13215 * In other words, plug in "pma" in the domain or range of "map".
13216 * The result is a map that lives in the same space as "map",
13217 * except that the space of type "type" has been replaced by
13218 * the domain space of "pma".
13220 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13221 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13223 isl_bool aligned;
13225 if (!map || !pma)
13226 goto error;
13228 aligned = isl_map_space_has_equal_params(map, pma->dim);
13229 if (aligned < 0)
13230 goto error;
13231 if (aligned)
13232 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13234 if (isl_map_check_named_params(map) < 0)
13235 goto error;
13236 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13237 goto error;
13238 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13239 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13241 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13242 error:
13243 isl_pw_multi_aff_free(pma);
13244 return isl_map_free(map);
13247 /* Compute the preimage of "set" under the function represented by "pma".
13248 * In other words, plug in "pma" in "set". The result is a set
13249 * that lives in the domain space of "pma".
13251 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13252 __isl_take isl_pw_multi_aff *pma)
13254 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13257 /* Compute the preimage of the domain of "map" under the function
13258 * represented by "pma".
13259 * In other words, plug in "pma" in the domain of "map".
13260 * The result is a map that lives in the same space as "map",
13261 * except that domain space has been replaced by the domain space of "pma".
13263 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13264 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13266 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13269 /* Compute the preimage of the range of "map" under the function
13270 * represented by "pma".
13271 * In other words, plug in "pma" in the range of "map".
13272 * The result is a map that lives in the same space as "map",
13273 * except that range space has been replaced by the domain space of "pma".
13275 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13276 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13278 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13281 /* Compute the preimage of "map" under the function represented by "mpa".
13282 * In other words, plug in "mpa" in the domain or range of "map".
13283 * The result is a map that lives in the same space as "map",
13284 * except that the space of type "type" has been replaced by
13285 * the domain space of "mpa".
13287 * If the map does not involve any constraints that refer to the
13288 * dimensions of the substituted space, then the only possible
13289 * effect of "mpa" on the map is to map the space to a different space.
13290 * We create a separate isl_multi_aff to effectuate this change
13291 * in order to avoid spurious splitting of the map along the pieces
13292 * of "mpa".
13293 * If "mpa" has a non-trivial explicit domain, however,
13294 * then the full substitution should be performed.
13296 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13297 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13299 int n;
13300 isl_bool full;
13301 isl_pw_multi_aff *pma;
13303 if (!map || !mpa)
13304 goto error;
13306 n = isl_map_dim(map, type);
13307 full = isl_map_involves_dims(map, type, 0, n);
13308 if (full >= 0 && !full)
13309 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13310 if (full < 0)
13311 goto error;
13312 if (!full) {
13313 isl_space *space;
13314 isl_multi_aff *ma;
13316 space = isl_multi_pw_aff_get_space(mpa);
13317 isl_multi_pw_aff_free(mpa);
13318 ma = isl_multi_aff_zero(space);
13319 return isl_map_preimage_multi_aff(map, type, ma);
13322 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13323 return isl_map_preimage_pw_multi_aff(map, type, pma);
13324 error:
13325 isl_map_free(map);
13326 isl_multi_pw_aff_free(mpa);
13327 return NULL;
13330 /* Compute the preimage of "map" under the function represented by "mpa".
13331 * In other words, plug in "mpa" in the domain "map".
13332 * The result is a map that lives in the same space as "map",
13333 * except that domain space has been replaced by the domain space of "mpa".
13335 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13336 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13338 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13341 /* Compute the preimage of "set" by the function represented by "mpa".
13342 * In other words, plug in "mpa" in "set".
13344 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13345 __isl_take isl_multi_pw_aff *mpa)
13347 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13350 /* Return a copy of the equality constraints of "bset" as a matrix.
13352 __isl_give isl_mat *isl_basic_set_extract_equalities(
13353 __isl_keep isl_basic_set *bset)
13355 isl_ctx *ctx;
13356 unsigned total;
13358 if (!bset)
13359 return NULL;
13361 ctx = isl_basic_set_get_ctx(bset);
13362 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13363 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13366 /* Are the "n" "coefficients" starting at "first" of the integer division
13367 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13368 * to each other?
13369 * The "coefficient" at position 0 is the denominator.
13370 * The "coefficient" at position 1 is the constant term.
13372 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13373 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13374 unsigned first, unsigned n)
13376 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13377 return isl_bool_error;
13378 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13379 return isl_bool_error;
13380 return isl_seq_eq(bmap1->div[pos1] + first,
13381 bmap2->div[pos2] + first, n);
13384 /* Are the integer division expressions at position "pos1" in "bmap1" and
13385 * "pos2" in "bmap2" equal to each other, except that the constant terms
13386 * are different?
13388 isl_bool isl_basic_map_equal_div_expr_except_constant(
13389 __isl_keep isl_basic_map *bmap1, int pos1,
13390 __isl_keep isl_basic_map *bmap2, int pos2)
13392 isl_bool equal;
13393 unsigned total;
13395 if (!bmap1 || !bmap2)
13396 return isl_bool_error;
13397 total = isl_basic_map_total_dim(bmap1);
13398 if (total != isl_basic_map_total_dim(bmap2))
13399 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13400 "incomparable div expressions", return isl_bool_error);
13401 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13402 0, 1);
13403 if (equal < 0 || !equal)
13404 return equal;
13405 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13406 1, 1);
13407 if (equal < 0 || equal)
13408 return isl_bool_not(equal);
13409 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13410 2, total);
13413 /* Replace the numerator of the constant term of the integer division
13414 * expression at position "div" in "bmap" by "value".
13415 * The caller guarantees that this does not change the meaning
13416 * of the input.
13418 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13419 __isl_take isl_basic_map *bmap, int div, int value)
13421 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13422 return isl_basic_map_free(bmap);
13424 isl_int_set_si(bmap->div[div][1], value);
13426 return bmap;
13429 /* Is the point "inner" internal to inequality constraint "ineq"
13430 * of "bset"?
13431 * The point is considered to be internal to the inequality constraint,
13432 * if it strictly lies on the positive side of the inequality constraint,
13433 * or if it lies on the constraint and the constraint is lexico-positive.
13435 static isl_bool is_internal(__isl_keep isl_vec *inner,
13436 __isl_keep isl_basic_set *bset, int ineq)
13438 isl_ctx *ctx;
13439 int pos;
13440 unsigned total;
13442 if (!inner || !bset)
13443 return isl_bool_error;
13445 ctx = isl_basic_set_get_ctx(bset);
13446 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13447 &ctx->normalize_gcd);
13448 if (!isl_int_is_zero(ctx->normalize_gcd))
13449 return isl_int_is_nonneg(ctx->normalize_gcd);
13451 total = isl_basic_set_dim(bset, isl_dim_all);
13452 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13453 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13456 /* Tighten the inequality constraints of "bset" that are outward with respect
13457 * to the point "vec".
13458 * That is, tighten the constraints that are not satisfied by "vec".
13460 * "vec" is a point internal to some superset S of "bset" that is used
13461 * to make the subsets of S disjoint, by tightening one half of the constraints
13462 * that separate two subsets. In particular, the constraints of S
13463 * are all satisfied by "vec" and should not be tightened.
13464 * Of the internal constraints, those that have "vec" on the outside
13465 * are tightened. The shared facet is included in the adjacent subset
13466 * with the opposite constraint.
13467 * For constraints that saturate "vec", this criterion cannot be used
13468 * to determine which of the two sides should be tightened.
13469 * Instead, the sign of the first non-zero coefficient is used
13470 * to make this choice. Note that this second criterion is never used
13471 * on the constraints of S since "vec" is interior to "S".
13473 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13474 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13476 int j;
13478 bset = isl_basic_set_cow(bset);
13479 if (!bset)
13480 return NULL;
13481 for (j = 0; j < bset->n_ineq; ++j) {
13482 isl_bool internal;
13484 internal = is_internal(vec, bset, j);
13485 if (internal < 0)
13486 return isl_basic_set_free(bset);
13487 if (internal)
13488 continue;
13489 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13492 return bset;
13495 /* Replace the variables x of type "type" starting at "first" in "bmap"
13496 * by x' with x = M x' with M the matrix trans.
13497 * That is, replace the corresponding coefficients c by c M.
13499 * The transformation matrix should be a square matrix.
13501 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13502 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13503 __isl_take isl_mat *trans)
13505 unsigned pos;
13507 bmap = isl_basic_map_cow(bmap);
13508 if (!bmap || !trans)
13509 goto error;
13511 if (trans->n_row != trans->n_col)
13512 isl_die(trans->ctx, isl_error_invalid,
13513 "expecting square transformation matrix", goto error);
13514 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13515 goto error;
13517 pos = isl_basic_map_offset(bmap, type) + first;
13519 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13520 isl_mat_copy(trans)) < 0)
13521 goto error;
13522 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13523 isl_mat_copy(trans)) < 0)
13524 goto error;
13525 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13526 isl_mat_copy(trans)) < 0)
13527 goto error;
13529 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13530 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13532 isl_mat_free(trans);
13533 return bmap;
13534 error:
13535 isl_mat_free(trans);
13536 isl_basic_map_free(bmap);
13537 return NULL;
13540 /* Replace the variables x of type "type" starting at "first" in "bset"
13541 * by x' with x = M x' with M the matrix trans.
13542 * That is, replace the corresponding coefficients c by c M.
13544 * The transformation matrix should be a square matrix.
13546 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13547 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13548 __isl_take isl_mat *trans)
13550 return isl_basic_map_transform_dims(bset, type, first, trans);