isl_basic_map_swap_div: return modified result
[isl.git] / isl_map.c
blob209b956cc28b78fbc2038a025166ec699e0cb0ce
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 "bset" does not involve any parameters.
1391 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1393 if (!bset)
1394 return isl_stat_error;
1395 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1396 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1397 "basic set should not have any parameters",
1398 return isl_stat_error);
1399 return isl_stat_ok;
1402 /* Check that "bset" does not involve any local variables.
1404 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1406 if (!bset)
1407 return isl_stat_error;
1408 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1409 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1410 "basic set should not have any local variables",
1411 return isl_stat_error);
1412 return isl_stat_ok;
1415 /* Check that "map" has only named parameters, reporting an error
1416 * if it does not.
1418 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1420 return isl_space_check_named_params(isl_map_peek_space(map));
1423 /* Check that "bmap" has only named parameters, reporting an error
1424 * if it does not.
1426 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1428 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1431 /* Check that "bmap1" and "bmap2" have the same parameters,
1432 * reporting an error if they do not.
1434 static isl_stat isl_basic_map_check_equal_params(
1435 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1437 isl_bool match;
1439 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1440 if (match < 0)
1441 return isl_stat_error;
1442 if (!match)
1443 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1444 "parameters don't match", return isl_stat_error);
1445 return isl_stat_ok;
1448 __isl_give isl_map *isl_map_align_params_map_map_and(
1449 __isl_take isl_map *map1, __isl_take isl_map *map2,
1450 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1451 __isl_take isl_map *map2))
1453 if (!map1 || !map2)
1454 goto error;
1455 if (isl_map_has_equal_params(map1, map2))
1456 return fn(map1, map2);
1457 if (isl_map_check_named_params(map1) < 0)
1458 goto error;
1459 if (isl_map_check_named_params(map2) < 0)
1460 goto error;
1461 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1462 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1463 return fn(map1, map2);
1464 error:
1465 isl_map_free(map1);
1466 isl_map_free(map2);
1467 return NULL;
1470 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1471 __isl_keep isl_map *map2,
1472 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1474 isl_bool r;
1476 if (!map1 || !map2)
1477 return isl_bool_error;
1478 if (isl_map_has_equal_params(map1, map2))
1479 return fn(map1, map2);
1480 if (isl_map_check_named_params(map1) < 0)
1481 return isl_bool_error;
1482 if (isl_map_check_named_params(map2) < 0)
1483 return isl_bool_error;
1484 map1 = isl_map_copy(map1);
1485 map2 = isl_map_copy(map2);
1486 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1487 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1488 r = fn(map1, map2);
1489 isl_map_free(map1);
1490 isl_map_free(map2);
1491 return r;
1494 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1496 struct isl_ctx *ctx;
1497 if (!bmap)
1498 return -1;
1499 ctx = bmap->ctx;
1500 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1501 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1502 return -1);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1507 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1508 isl_int *t;
1509 int j = isl_basic_map_alloc_inequality(bmap);
1510 if (j < 0)
1511 return -1;
1512 t = bmap->ineq[j];
1513 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1514 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1515 bmap->eq[-1] = t;
1516 bmap->n_eq++;
1517 bmap->n_ineq--;
1518 bmap->eq--;
1519 return 0;
1521 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1522 bmap->extra - bmap->n_div);
1523 return bmap->n_eq++;
1526 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1528 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1531 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1533 if (!bmap)
1534 return -1;
1535 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1536 bmap->n_eq -= n;
1537 return 0;
1540 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1542 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1545 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1547 isl_int *t;
1548 if (!bmap)
1549 return -1;
1550 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1552 if (pos != bmap->n_eq - 1) {
1553 t = bmap->eq[pos];
1554 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1555 bmap->eq[bmap->n_eq - 1] = t;
1557 bmap->n_eq--;
1558 return 0;
1561 /* Turn inequality "pos" of "bmap" into an equality.
1563 * In particular, we move the inequality in front of the equalities
1564 * and move the last inequality in the position of the moved inequality.
1565 * Note that isl_tab_make_equalities_explicit depends on this particular
1566 * change in the ordering of the constraints.
1568 void isl_basic_map_inequality_to_equality(
1569 struct isl_basic_map *bmap, unsigned pos)
1571 isl_int *t;
1573 t = bmap->ineq[pos];
1574 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1575 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1576 bmap->eq[-1] = t;
1577 bmap->n_eq++;
1578 bmap->n_ineq--;
1579 bmap->eq--;
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1582 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1583 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1586 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1588 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1591 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1593 struct isl_ctx *ctx;
1594 if (!bmap)
1595 return -1;
1596 ctx = bmap->ctx;
1597 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1600 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1601 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1602 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1603 1 + isl_basic_map_total_dim(bmap),
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_ineq++;
1608 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1613 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1615 if (!bmap)
1616 return -1;
1617 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1618 bmap->n_ineq -= n;
1619 return 0;
1622 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1624 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1627 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1629 isl_int *t;
1630 if (!bmap)
1631 return -1;
1632 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1634 if (pos != bmap->n_ineq - 1) {
1635 t = bmap->ineq[pos];
1636 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1637 bmap->ineq[bmap->n_ineq - 1] = t;
1638 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1640 bmap->n_ineq--;
1641 return 0;
1644 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1646 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1649 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1650 isl_int *eq)
1652 isl_bool empty;
1653 int k;
1655 empty = isl_basic_map_plain_is_empty(bmap);
1656 if (empty < 0)
1657 return isl_basic_map_free(bmap);
1658 if (empty)
1659 return bmap;
1661 bmap = isl_basic_map_cow(bmap);
1662 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1663 if (!bmap)
1664 return NULL;
1665 k = isl_basic_map_alloc_equality(bmap);
1666 if (k < 0)
1667 goto error;
1668 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1669 return bmap;
1670 error:
1671 isl_basic_map_free(bmap);
1672 return NULL;
1675 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1676 isl_int *eq)
1678 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1681 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1682 isl_int *ineq)
1684 int k;
1686 bmap = isl_basic_map_cow(bmap);
1687 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1688 if (!bmap)
1689 return NULL;
1690 k = isl_basic_map_alloc_inequality(bmap);
1691 if (k < 0)
1692 goto error;
1693 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1694 return bmap;
1695 error:
1696 isl_basic_map_free(bmap);
1697 return NULL;
1700 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1701 isl_int *ineq)
1703 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1706 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1708 if (!bmap)
1709 return -1;
1710 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1711 isl_seq_clr(bmap->div[bmap->n_div] +
1712 1 + 1 + isl_basic_map_total_dim(bmap),
1713 bmap->extra - bmap->n_div);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1715 return bmap->n_div++;
1718 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1720 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1723 #undef TYPE
1724 #define TYPE isl_basic_map
1725 #include "check_type_range_templ.c"
1727 /* Check that there are "n" dimensions of type "type" starting at "first"
1728 * in "bset".
1730 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1733 return isl_basic_map_check_range(bset_to_bmap(bset),
1734 type, first, n);
1737 /* Insert an extra integer division, prescribed by "div", to "bmap"
1738 * at (integer division) position "pos".
1740 * The integer division is first added at the end and then moved
1741 * into the right position.
1743 __isl_give isl_basic_map *isl_basic_map_insert_div(
1744 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1746 int i, k;
1748 bmap = isl_basic_map_cow(bmap);
1749 if (!bmap || !div)
1750 return isl_basic_map_free(bmap);
1752 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1753 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1754 "unexpected size", return isl_basic_map_free(bmap));
1755 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1756 return isl_basic_map_free(bmap);
1758 bmap = isl_basic_map_extend_space(bmap,
1759 isl_basic_map_get_space(bmap), 1, 0, 2);
1760 k = isl_basic_map_alloc_div(bmap);
1761 if (k < 0)
1762 return isl_basic_map_free(bmap);
1763 isl_seq_cpy(bmap->div[k], div->el, div->size);
1764 isl_int_set_si(bmap->div[k][div->size], 0);
1766 for (i = k; i > pos; --i)
1767 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1769 return bmap;
1772 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1774 if (!bmap)
1775 return isl_stat_error;
1776 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1777 bmap->n_div -= n;
1778 return isl_stat_ok;
1781 static __isl_give isl_basic_map *add_constraints(
1782 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1783 unsigned i_pos, unsigned o_pos)
1785 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1786 isl_ctx *ctx;
1787 isl_space *space;
1788 struct isl_dim_map *dim_map;
1790 space = isl_basic_map_peek_space(bmap2);
1791 if (!bmap1 || !space)
1792 goto error;
1794 total = isl_basic_map_dim(bmap1, isl_dim_all);
1795 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1796 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1797 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1798 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1799 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1800 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1801 ctx = isl_basic_map_get_ctx(bmap1);
1802 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1804 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1805 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1806 isl_dim_map_div(dim_map, bmap2, total);
1808 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1809 error:
1810 isl_basic_map_free(bmap1);
1811 isl_basic_map_free(bmap2);
1812 return NULL;
1815 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1816 struct isl_basic_set *bset2, unsigned pos)
1818 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1819 bset_to_bmap(bset2), 0, pos));
1822 __isl_give isl_basic_map *isl_basic_map_extend_space(
1823 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1824 unsigned extra, unsigned n_eq, unsigned n_ineq)
1826 struct isl_basic_map *ext;
1827 unsigned flags;
1828 int dims_ok;
1830 if (!space)
1831 goto error;
1833 if (!base)
1834 goto error;
1836 dims_ok = isl_space_is_equal(base->dim, space) &&
1837 base->extra >= base->n_div + extra;
1839 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1840 room_for_ineq(base, n_ineq)) {
1841 isl_space_free(space);
1842 return base;
1845 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1846 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1847 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1848 extra += base->extra;
1849 n_eq += base->n_eq;
1850 n_ineq += base->n_ineq;
1852 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1853 space = NULL;
1854 if (!ext)
1855 goto error;
1857 if (dims_ok)
1858 ext->sample = isl_vec_copy(base->sample);
1859 flags = base->flags;
1860 ext = add_constraints(ext, base, 0, 0);
1861 if (ext) {
1862 ext->flags = flags;
1863 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1866 return ext;
1868 error:
1869 isl_space_free(space);
1870 isl_basic_map_free(base);
1871 return NULL;
1874 __isl_give isl_basic_set *isl_basic_set_extend_space(
1875 __isl_take isl_basic_set *base,
1876 __isl_take isl_space *dim, unsigned extra,
1877 unsigned n_eq, unsigned n_ineq)
1879 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1880 dim, extra, n_eq, n_ineq));
1883 struct isl_basic_map *isl_basic_map_extend_constraints(
1884 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1886 if (!base)
1887 return NULL;
1888 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1889 0, n_eq, n_ineq);
1892 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1893 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1896 struct isl_basic_map *bmap;
1897 isl_space *dim;
1899 if (!base)
1900 return NULL;
1901 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1902 if (!dim)
1903 goto error;
1905 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1906 return bmap;
1907 error:
1908 isl_basic_map_free(base);
1909 return NULL;
1912 struct isl_basic_set *isl_basic_set_extend_constraints(
1913 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1915 isl_basic_map *bmap = bset_to_bmap(base);
1916 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1917 return bset_from_bmap(bmap);
1920 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1922 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1925 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1927 if (!bmap)
1928 return NULL;
1930 if (bmap->ref > 1) {
1931 bmap->ref--;
1932 bmap = isl_basic_map_dup(bmap);
1934 if (bmap) {
1935 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1938 return bmap;
1941 /* Clear all cached information in "map", either because it is about
1942 * to be modified or because it is being freed.
1943 * Always return the same pointer that is passed in.
1944 * This is needed for the use in isl_map_free.
1946 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1948 isl_basic_map_free(map->cached_simple_hull[0]);
1949 isl_basic_map_free(map->cached_simple_hull[1]);
1950 map->cached_simple_hull[0] = NULL;
1951 map->cached_simple_hull[1] = NULL;
1952 return map;
1955 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1957 return isl_map_cow(set);
1960 /* Return an isl_map that is equal to "map" and that has only
1961 * a single reference.
1963 * If the original input already has only one reference, then
1964 * simply return it, but clear all cached information, since
1965 * it may be rendered invalid by the operations that will be
1966 * performed on the result.
1968 * Otherwise, create a duplicate (without any cached information).
1970 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1972 if (!map)
1973 return NULL;
1975 if (map->ref == 1)
1976 return clear_caches(map);
1977 map->ref--;
1978 return isl_map_dup(map);
1981 static void swap_vars(struct isl_blk blk, isl_int *a,
1982 unsigned a_len, unsigned b_len)
1984 isl_seq_cpy(blk.data, a+a_len, b_len);
1985 isl_seq_cpy(blk.data+b_len, a, a_len);
1986 isl_seq_cpy(a, blk.data, b_len+a_len);
1989 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1990 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1992 int i;
1993 struct isl_blk blk;
1995 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1996 goto error;
1998 if (n1 == 0 || n2 == 0)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 if (!bmap)
2003 return NULL;
2005 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2006 if (isl_blk_is_error(blk))
2007 goto error;
2009 for (i = 0; i < bmap->n_eq; ++i)
2010 swap_vars(blk,
2011 bmap->eq[i] + pos, n1, n2);
2013 for (i = 0; i < bmap->n_ineq; ++i)
2014 swap_vars(blk,
2015 bmap->ineq[i] + pos, n1, n2);
2017 for (i = 0; i < bmap->n_div; ++i)
2018 swap_vars(blk,
2019 bmap->div[i]+1 + pos, n1, n2);
2021 isl_blk_free(bmap->ctx, blk);
2023 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2024 bmap = isl_basic_map_gauss(bmap, NULL);
2025 return isl_basic_map_finalize(bmap);
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map *bmap)
2034 int i = 0;
2035 unsigned total;
2036 if (!bmap)
2037 goto error;
2038 total = isl_basic_map_total_dim(bmap);
2039 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2040 return isl_basic_map_free(bmap);
2041 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2042 if (bmap->n_eq > 0)
2043 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2044 else {
2045 i = isl_basic_map_alloc_equality(bmap);
2046 if (i < 0)
2047 goto error;
2049 isl_int_set_si(bmap->eq[i][0], 1);
2050 isl_seq_clr(bmap->eq[i]+1, total);
2051 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2052 isl_vec_free(bmap->sample);
2053 bmap->sample = NULL;
2054 return isl_basic_map_finalize(bmap);
2055 error:
2056 isl_basic_map_free(bmap);
2057 return NULL;
2060 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set *bset)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2066 __isl_give isl_basic_map *isl_basic_map_set_rational(
2067 __isl_take isl_basic_map *bmap)
2069 if (!bmap)
2070 return NULL;
2072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2073 return bmap;
2075 bmap = isl_basic_map_cow(bmap);
2076 if (!bmap)
2077 return NULL;
2079 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2081 return isl_basic_map_finalize(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_set_rational(
2085 __isl_take isl_basic_set *bset)
2087 return isl_basic_map_set_rational(bset);
2090 __isl_give isl_basic_set *isl_basic_set_set_integral(
2091 __isl_take isl_basic_set *bset)
2093 if (!bset)
2094 return NULL;
2096 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2097 return bset;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 return NULL;
2103 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2105 return isl_basic_set_finalize(bset);
2108 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2110 int i;
2112 map = isl_map_cow(map);
2113 if (!map)
2114 return NULL;
2115 for (i = 0; i < map->n; ++i) {
2116 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2117 if (!map->p[i])
2118 goto error;
2120 return map;
2121 error:
2122 isl_map_free(map);
2123 return NULL;
2126 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2128 return isl_map_set_rational(set);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 * of "bmap").
2134 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2136 isl_int *t = bmap->div[a];
2137 bmap->div[a] = bmap->div[b];
2138 bmap->div[b] = t;
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2145 int a, int b)
2147 int i;
2148 unsigned off;
2150 if (!bmap)
2151 return NULL;
2153 off = isl_space_dim(bmap->dim, isl_dim_all);
2154 swap_div(bmap, a, b);
2156 for (i = 0; i < bmap->n_eq; ++i)
2157 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2159 for (i = 0; i < bmap->n_ineq; ++i)
2160 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2162 for (i = 0; i < bmap->n_div; ++i)
2163 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2164 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2166 return bmap;
2169 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2171 isl_seq_cpy(c, c + n, rem);
2172 isl_seq_clr(c + rem, n);
2175 /* Drop n dimensions starting at first.
2177 * In principle, this frees up some extra variables as the number
2178 * of columns remains constant, but we would have to extend
2179 * the div array too as the number of rows in this array is assumed
2180 * to be equal to extra.
2182 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2183 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2185 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2188 /* Move "n" divs starting at "first" to the end of the list of divs.
2190 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2191 unsigned first, unsigned n)
2193 isl_int **div;
2194 int i;
2196 if (first + n == bmap->n_div)
2197 return bmap;
2199 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2200 if (!div)
2201 goto error;
2202 for (i = 0; i < n; ++i)
2203 div[i] = bmap->div[first + i];
2204 for (i = 0; i < bmap->n_div - first - n; ++i)
2205 bmap->div[first + i] = bmap->div[first + n + i];
2206 for (i = 0; i < n; ++i)
2207 bmap->div[bmap->n_div - n + i] = div[i];
2208 free(div);
2209 return bmap;
2210 error:
2211 isl_basic_map_free(bmap);
2212 return NULL;
2215 #undef TYPE
2216 #define TYPE isl_map
2217 static
2218 #include "check_type_range_templ.c"
2220 /* Check that there are "n" dimensions of type "type" starting at "first"
2221 * in "set".
2223 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2224 enum isl_dim_type type, unsigned first, unsigned n)
2226 return isl_map_check_range(set_to_map(set), type, first, n);
2229 /* Drop "n" dimensions of type "type" starting at "first".
2230 * Perform the core computation, without cowing or
2231 * simplifying and finalizing the result.
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map *isl_basic_map_drop_core(
2239 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2240 unsigned first, unsigned n)
2242 int i;
2243 unsigned offset;
2244 unsigned left;
2246 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2247 return isl_basic_map_free(bmap);
2249 offset = isl_basic_map_offset(bmap, type) + first;
2250 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2251 for (i = 0; i < bmap->n_eq; ++i)
2252 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2254 for (i = 0; i < bmap->n_ineq; ++i)
2255 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2257 for (i = 0; i < bmap->n_div; ++i)
2258 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2260 if (type == isl_dim_div) {
2261 bmap = move_divs_last(bmap, first, n);
2262 if (!bmap)
2263 return NULL;
2264 if (isl_basic_map_free_div(bmap, n) < 0)
2265 return isl_basic_map_free(bmap);
2266 } else
2267 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2268 if (!bmap->dim)
2269 return isl_basic_map_free(bmap);
2271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2272 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2273 return bmap;
2276 /* Drop "n" dimensions of type "type" starting at "first".
2278 * In principle, this frees up some extra variables as the number
2279 * of columns remains constant, but we would have to extend
2280 * the div array too as the number of rows in this array is assumed
2281 * to be equal to extra.
2283 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 if (!bmap)
2287 return NULL;
2288 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2289 return bmap;
2291 bmap = isl_basic_map_cow(bmap);
2292 if (!bmap)
2293 return NULL;
2295 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2297 bmap = isl_basic_map_simplify(bmap);
2298 return isl_basic_map_finalize(bmap);
2301 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2302 enum isl_dim_type type, unsigned first, unsigned n)
2304 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2305 type, first, n));
2308 /* No longer consider "map" to be normalized.
2310 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2312 if (!map)
2313 return NULL;
2314 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2315 return map;
2318 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2319 enum isl_dim_type type, unsigned first, unsigned n)
2321 int i;
2323 if (isl_map_check_range(map, type, first, n) < 0)
2324 return isl_map_free(map);
2326 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2327 return map;
2328 map = isl_map_cow(map);
2329 if (!map)
2330 goto error;
2331 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2332 if (!map->dim)
2333 goto error;
2335 for (i = 0; i < map->n; ++i) {
2336 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2337 if (!map->p[i])
2338 goto error;
2340 map = isl_map_unmark_normalized(map);
2342 return map;
2343 error:
2344 isl_map_free(map);
2345 return NULL;
2348 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2354 /* Drop the integer division at position "div", which is assumed
2355 * not to appear in any of the constraints or
2356 * in any of the other integer divisions.
2358 * Since the integer division is redundant, there is no need to cow.
2360 __isl_give isl_basic_map *isl_basic_map_drop_div(
2361 __isl_take isl_basic_map *bmap, unsigned div)
2363 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 int i;
2375 if (n == 0)
2376 return map;
2378 if (isl_map_check_range(map, type, first, n) < 0)
2379 return isl_map_free(map);
2381 map = isl_map_cow(map);
2382 if (!map)
2383 return NULL;
2385 for (i = 0; i < map->n; ++i) {
2386 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2387 if (!map->p[i])
2388 goto error;
2390 return map;
2391 error:
2392 isl_map_free(map);
2393 return NULL;
2396 /* Eliminate the specified n dimensions starting at first from the
2397 * constraints, without removing the dimensions from the space.
2398 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2400 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2401 enum isl_dim_type type, unsigned first, unsigned n)
2403 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2406 /* Eliminate the specified n dimensions starting at first from the
2407 * constraints, without removing the dimensions from the space.
2408 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2410 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2411 unsigned first, unsigned n)
2413 return isl_set_eliminate(set, isl_dim_set, first, n);
2416 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2417 __isl_take isl_basic_map *bmap)
2419 if (!bmap)
2420 return NULL;
2421 bmap = isl_basic_map_eliminate_vars(bmap,
2422 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2423 if (!bmap)
2424 return NULL;
2425 bmap->n_div = 0;
2426 return isl_basic_map_finalize(bmap);
2429 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set *bset)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2435 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2437 int i;
2439 if (!map)
2440 return NULL;
2441 if (map->n == 0)
2442 return map;
2444 map = isl_map_cow(map);
2445 if (!map)
2446 return NULL;
2448 for (i = 0; i < map->n; ++i) {
2449 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2450 if (!map->p[i])
2451 goto error;
2453 return map;
2454 error:
2455 isl_map_free(map);
2456 return NULL;
2459 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2461 return isl_map_remove_divs(set);
2464 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2466 unsigned first, unsigned n)
2468 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2469 return isl_basic_map_free(bmap);
2470 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2471 return bmap;
2472 bmap = isl_basic_map_eliminate_vars(bmap,
2473 isl_basic_map_offset(bmap, type) - 1 + first, n);
2474 if (!bmap)
2475 return bmap;
2476 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2477 return bmap;
2478 bmap = isl_basic_map_drop(bmap, type, first, n);
2479 return bmap;
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2486 unsigned first, unsigned n)
2488 int i;
2489 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2491 if (isl_int_is_zero(bmap->div[div][0]))
2492 return isl_bool_false;
2493 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2494 return isl_bool_true;
2496 for (i = bmap->n_div - 1; i >= 0; --i) {
2497 isl_bool involves;
2499 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2500 continue;
2501 involves = div_involves_vars(bmap, i, first, n);
2502 if (involves < 0 || involves)
2503 return involves;
2506 return isl_bool_false;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2527 * b + f(x) >= 0
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2532 * b + g(x) >= 0
2534 * with the condition that the coefficients of g(x) - f(x) are all
2535 * divisible by d.
2536 * Rewriting this constraint as
2538 * 0 >= -b - g(x)
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2548 * or
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map *bmap, int div, int i,
2559 unsigned total, isl_int v, int lb, int ub)
2561 int j;
2563 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2564 if (lb) {
2565 isl_int_sub(v, bmap->ineq[i][1 + j],
2566 bmap->div[div][1 + 1 + j]);
2567 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2569 if (ub) {
2570 isl_int_add(v, bmap->ineq[i][1 + j],
2571 bmap->div[div][1 + 1 + j]);
2572 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2575 if (!lb && !ub)
2576 return bmap;
2578 bmap = isl_basic_map_cow(bmap);
2579 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2580 if (lb) {
2581 int k = isl_basic_map_alloc_inequality(bmap);
2582 if (k < 0)
2583 goto error;
2584 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2585 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2586 bmap->div[div][1 + j]);
2587 isl_int_cdiv_q(bmap->ineq[k][j],
2588 bmap->ineq[k][j], bmap->div[div][0]);
2590 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2592 if (ub) {
2593 int k = isl_basic_map_alloc_inequality(bmap);
2594 if (k < 0)
2595 goto error;
2596 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2597 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2598 bmap->div[div][1 + j]);
2599 isl_int_fdiv_q(bmap->ineq[k][j],
2600 bmap->ineq[k][j], bmap->div[div][0]);
2602 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2605 return bmap;
2606 error:
2607 isl_basic_map_free(bmap);
2608 return NULL;
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2622 * on "div".
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2639 * or
2641 * i + 2j <= 3 + 2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2654 * k >= -2
2656 static __isl_give isl_basic_map *insert_bounds_on_div(
2657 __isl_take isl_basic_map *bmap, int div)
2659 int i;
2660 int check_lb, check_ub;
2661 isl_int v;
2662 unsigned total;
2664 if (!bmap)
2665 return NULL;
2667 if (isl_int_is_zero(bmap->div[div][0]))
2668 return bmap;
2670 total = isl_space_dim(bmap->dim, isl_dim_all);
2672 check_lb = 0;
2673 check_ub = 0;
2674 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2675 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2676 if (s > 0)
2677 check_ub = 1;
2678 if (s < 0)
2679 check_lb = 1;
2682 if (!check_lb && !check_ub)
2683 return bmap;
2685 isl_int_init(v);
2687 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2688 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2689 continue;
2691 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2692 check_lb, check_ub);
2695 isl_int_clear(v);
2697 return bmap;
2700 /* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2703 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map *bmap,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2707 int i;
2709 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2710 return isl_basic_map_free(bmap);
2711 first += isl_basic_map_offset(bmap, type);
2713 for (i = bmap->n_div - 1; i >= 0; --i) {
2714 isl_bool involves;
2716 involves = div_involves_vars(bmap, i, first, n);
2717 if (involves < 0)
2718 return isl_basic_map_free(bmap);
2719 if (!involves)
2720 continue;
2721 bmap = insert_bounds_on_div(bmap, i);
2722 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2723 if (!bmap)
2724 return NULL;
2725 i = bmap->n_div;
2728 return bmap;
2731 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_set *bset,
2733 enum isl_dim_type type, unsigned first, unsigned n)
2735 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2738 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2739 enum isl_dim_type type, unsigned first, unsigned n)
2741 int i;
2743 if (!map)
2744 return NULL;
2745 if (map->n == 0)
2746 return map;
2748 map = isl_map_cow(map);
2749 if (!map)
2750 return NULL;
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2754 type, first, n);
2755 if (!map->p[i])
2756 goto error;
2758 return map;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2765 enum isl_dim_type type, unsigned first, unsigned n)
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2768 type, first, n));
2771 /* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2775 * div.
2777 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2778 enum isl_dim_type type, unsigned first, unsigned n)
2780 int i;
2782 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2783 return isl_bool_error;
2785 first += isl_basic_map_offset(bmap, type);
2786 for (i = 0; i < bmap->n_eq; ++i)
2787 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2788 return isl_bool_true;
2789 for (i = 0; i < bmap->n_ineq; ++i)
2790 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2791 return isl_bool_true;
2792 for (i = 0; i < bmap->n_div; ++i) {
2793 if (isl_int_is_zero(bmap->div[i][0]))
2794 continue;
2795 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2796 return isl_bool_true;
2799 return isl_bool_false;
2802 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2803 enum isl_dim_type type, unsigned first, unsigned n)
2805 int i;
2807 if (isl_map_check_range(map, type, first, n) < 0)
2808 return isl_bool_error;
2810 for (i = 0; i < map->n; ++i) {
2811 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2812 type, first, n);
2813 if (involves < 0 || involves)
2814 return involves;
2817 return isl_bool_false;
2820 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2821 enum isl_dim_type type, unsigned first, unsigned n)
2823 return isl_basic_map_involves_dims(bset, type, first, n);
2826 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2827 enum isl_dim_type type, unsigned first, unsigned n)
2829 return isl_map_involves_dims(set, type, first, n);
2832 /* Drop all constraints in bmap that involve any of the dimensions
2833 * first to first+n-1.
2834 * This function only performs the actual removal of constraints.
2836 * This function should not call finalize since it is used by
2837 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2839 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2842 int i;
2844 if (n == 0)
2845 return bmap;
2847 bmap = isl_basic_map_cow(bmap);
2849 if (!bmap)
2850 return NULL;
2852 for (i = bmap->n_eq - 1; i >= 0; --i) {
2853 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2854 continue;
2855 isl_basic_map_drop_equality(bmap, i);
2858 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2859 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2860 continue;
2861 isl_basic_map_drop_inequality(bmap, i);
2864 return bmap;
2867 /* Drop all constraints in bset that involve any of the dimensions
2868 * first to first+n-1.
2869 * This function only performs the actual removal of constraints.
2871 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2874 return isl_basic_map_drop_constraints_involving(bset, first, n);
2877 /* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map *bmap,
2882 enum isl_dim_type type, unsigned first, unsigned n)
2884 int i;
2886 if (n == 0) {
2887 isl_space *space = isl_basic_map_get_space(bmap);
2888 isl_basic_map_free(bmap);
2889 return isl_basic_map_universe(space);
2891 bmap = isl_basic_map_cow(bmap);
2892 if (!bmap)
2893 return NULL;
2895 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2896 return isl_basic_map_free(bmap);
2898 first += isl_basic_map_offset(bmap, type) - 1;
2900 for (i = bmap->n_eq - 1; i >= 0; --i) {
2901 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2902 continue;
2903 isl_basic_map_drop_equality(bmap, i);
2906 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2907 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2908 continue;
2909 isl_basic_map_drop_inequality(bmap, i);
2912 bmap = isl_basic_map_add_known_div_constraints(bmap);
2913 return bmap;
2916 /* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_set *bset,
2921 enum isl_dim_type type, unsigned first, unsigned n)
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2924 type, first, n);
2927 /* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map *bmap,
2932 enum isl_dim_type type, unsigned first, unsigned n)
2934 if (!bmap)
2935 return NULL;
2936 if (n == 0)
2937 return bmap;
2939 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2940 return isl_basic_map_free(bmap);
2942 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2943 first += isl_basic_map_offset(bmap, type) - 1;
2944 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2945 bmap = isl_basic_map_add_known_div_constraints(bmap);
2946 return bmap;
2949 /* Drop all constraints in bset that involve any of the dimensions
2950 * first to first + n - 1 of the given type.
2952 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2953 __isl_take isl_basic_set *bset,
2954 enum isl_dim_type type, unsigned first, unsigned n)
2956 return isl_basic_map_drop_constraints_involving_dims(bset,
2957 type, first, n);
2960 /* Drop constraints from "map" by applying "drop" to each basic map.
2962 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2963 enum isl_dim_type type, unsigned first, unsigned n,
2964 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2965 enum isl_dim_type type, unsigned first, unsigned n))
2967 int i;
2969 if (isl_map_check_range(map, type, first, n) < 0)
2970 return isl_map_free(map);
2972 map = isl_map_cow(map);
2973 if (!map)
2974 return NULL;
2976 for (i = 0; i < map->n; ++i) {
2977 map->p[i] = drop(map->p[i], type, first, n);
2978 if (!map->p[i])
2979 return isl_map_free(map);
2982 if (map->n > 1)
2983 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2985 return map;
2988 /* Drop all constraints in map that involve any of the dimensions
2989 * first to first + n - 1 of the given type.
2991 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2992 __isl_take isl_map *map,
2993 enum isl_dim_type type, unsigned first, unsigned n)
2995 if (n == 0)
2996 return map;
2997 return drop_constraints(map, type, first, n,
2998 &isl_basic_map_drop_constraints_involving_dims);
3001 /* Drop all constraints in "map" that do not involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3005 __isl_take isl_map *map,
3006 enum isl_dim_type type, unsigned first, unsigned n)
3008 if (n == 0) {
3009 isl_space *space = isl_map_get_space(map);
3010 isl_map_free(map);
3011 return isl_map_universe(space);
3013 return drop_constraints(map, type, first, n,
3014 &isl_basic_map_drop_constraints_not_involving_dims);
3017 /* Drop all constraints in set that involve any of the dimensions
3018 * first to first + n - 1 of the given type.
3020 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3021 __isl_take isl_set *set,
3022 enum isl_dim_type type, unsigned first, unsigned n)
3024 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3027 /* Drop all constraints in "set" that do not involve any of the dimensions
3028 * first to first + n - 1 of the given type.
3030 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3031 __isl_take isl_set *set,
3032 enum isl_dim_type type, unsigned first, unsigned n)
3034 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3037 /* Does local variable "div" of "bmap" have a complete explicit representation?
3038 * Having a complete explicit representation requires not only
3039 * an explicit representation, but also that all local variables
3040 * that appear in this explicit representation in turn have
3041 * a complete explicit representation.
3043 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3045 int i;
3046 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3047 isl_bool marked;
3049 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3050 if (marked < 0 || marked)
3051 return isl_bool_not(marked);
3053 for (i = bmap->n_div - 1; i >= 0; --i) {
3054 isl_bool known;
3056 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3057 continue;
3058 known = isl_basic_map_div_is_known(bmap, i);
3059 if (known < 0 || !known)
3060 return known;
3063 return isl_bool_true;
3066 /* Remove all divs that are unknown or defined in terms of unknown divs.
3068 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3069 __isl_take isl_basic_map *bmap)
3071 int i;
3073 if (!bmap)
3074 return NULL;
3076 for (i = bmap->n_div - 1; i >= 0; --i) {
3077 if (isl_basic_map_div_is_known(bmap, i))
3078 continue;
3079 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3080 if (!bmap)
3081 return NULL;
3082 i = bmap->n_div;
3085 return bmap;
3088 /* Remove all divs that are unknown or defined in terms of unknown divs.
3090 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3091 __isl_take isl_basic_set *bset)
3093 return isl_basic_map_remove_unknown_divs(bset);
3096 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3098 int i;
3100 if (!map)
3101 return NULL;
3102 if (map->n == 0)
3103 return map;
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3109 for (i = 0; i < map->n; ++i) {
3110 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3111 if (!map->p[i])
3112 goto error;
3114 return map;
3115 error:
3116 isl_map_free(map);
3117 return NULL;
3120 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3122 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3125 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3126 __isl_take isl_basic_set *bset,
3127 enum isl_dim_type type, unsigned first, unsigned n)
3129 isl_basic_map *bmap = bset_to_bmap(bset);
3130 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3131 return bset_from_bmap(bmap);
3134 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 int i;
3139 if (n == 0)
3140 return map;
3142 map = isl_map_cow(map);
3143 if (isl_map_check_range(map, type, first, n) < 0)
3144 return isl_map_free(map);
3146 for (i = 0; i < map->n; ++i) {
3147 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3148 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3149 if (!map->p[i])
3150 goto error;
3152 map = isl_map_drop(map, type, first, n);
3153 return map;
3154 error:
3155 isl_map_free(map);
3156 return NULL;
3159 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3160 enum isl_dim_type type, unsigned first, unsigned n)
3162 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3163 type, first, n));
3166 /* Project out n inputs starting at first using Fourier-Motzkin */
3167 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3168 unsigned first, unsigned n)
3170 return isl_map_remove_dims(map, isl_dim_in, first, n);
3173 static void dump_term(struct isl_basic_map *bmap,
3174 isl_int c, int pos, FILE *out)
3176 const char *name;
3177 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3178 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3179 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3180 if (!pos)
3181 isl_int_print(out, c, 0);
3182 else {
3183 if (!isl_int_is_one(c))
3184 isl_int_print(out, c, 0);
3185 if (pos < 1 + nparam) {
3186 name = isl_space_get_dim_name(bmap->dim,
3187 isl_dim_param, pos - 1);
3188 if (name)
3189 fprintf(out, "%s", name);
3190 else
3191 fprintf(out, "p%d", pos - 1);
3192 } else if (pos < 1 + nparam + in)
3193 fprintf(out, "i%d", pos - 1 - nparam);
3194 else if (pos < 1 + nparam + dim)
3195 fprintf(out, "o%d", pos - 1 - nparam - in);
3196 else
3197 fprintf(out, "e%d", pos - 1 - nparam - dim);
3201 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3202 int sign, FILE *out)
3204 int i;
3205 int first;
3206 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3207 isl_int v;
3209 isl_int_init(v);
3210 for (i = 0, first = 1; i < len; ++i) {
3211 if (isl_int_sgn(c[i]) * sign <= 0)
3212 continue;
3213 if (!first)
3214 fprintf(out, " + ");
3215 first = 0;
3216 isl_int_abs(v, c[i]);
3217 dump_term(bmap, v, i, out);
3219 isl_int_clear(v);
3220 if (first)
3221 fprintf(out, "0");
3224 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3225 const char *op, FILE *out, int indent)
3227 int i;
3229 fprintf(out, "%*s", indent, "");
3231 dump_constraint_sign(bmap, c, 1, out);
3232 fprintf(out, " %s ", op);
3233 dump_constraint_sign(bmap, c, -1, out);
3235 fprintf(out, "\n");
3237 for (i = bmap->n_div; i < bmap->extra; ++i) {
3238 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3239 continue;
3240 fprintf(out, "%*s", indent, "");
3241 fprintf(out, "ERROR: unused div coefficient not zero\n");
3242 abort();
3246 static void dump_constraints(struct isl_basic_map *bmap,
3247 isl_int **c, unsigned n,
3248 const char *op, FILE *out, int indent)
3250 int i;
3252 for (i = 0; i < n; ++i)
3253 dump_constraint(bmap, c[i], op, out, indent);
3256 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3258 int j;
3259 int first = 1;
3260 unsigned total = isl_basic_map_total_dim(bmap);
3262 for (j = 0; j < 1 + total; ++j) {
3263 if (isl_int_is_zero(exp[j]))
3264 continue;
3265 if (!first && isl_int_is_pos(exp[j]))
3266 fprintf(out, "+");
3267 dump_term(bmap, exp[j], j, out);
3268 first = 0;
3272 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3274 int i;
3276 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3277 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3279 for (i = 0; i < bmap->n_div; ++i) {
3280 fprintf(out, "%*s", indent, "");
3281 fprintf(out, "e%d = [(", i);
3282 dump_affine(bmap, bmap->div[i]+1, out);
3283 fprintf(out, ")/");
3284 isl_int_print(out, bmap->div[i][0], 0);
3285 fprintf(out, "]\n");
3289 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3290 FILE *out, int indent)
3292 if (!bset) {
3293 fprintf(out, "null basic set\n");
3294 return;
3297 fprintf(out, "%*s", indent, "");
3298 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3299 bset->ref, bset->dim->nparam, bset->dim->n_out,
3300 bset->extra, bset->flags);
3301 dump(bset_to_bmap(bset), out, indent);
3304 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3305 FILE *out, int indent)
3307 if (!bmap) {
3308 fprintf(out, "null basic map\n");
3309 return;
3312 fprintf(out, "%*s", indent, "");
3313 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3314 "flags: %x, n_name: %d\n",
3315 bmap->ref,
3316 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3317 bmap->extra, bmap->flags, bmap->dim->n_id);
3318 dump(bmap, out, indent);
3321 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3323 unsigned total;
3324 if (!bmap)
3325 return -1;
3326 total = isl_basic_map_total_dim(bmap);
3327 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3328 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3329 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3330 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3331 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3332 return 0;
3335 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3336 unsigned flags)
3338 if (isl_space_check_is_set(space) < 0)
3339 goto error;
3340 return isl_map_alloc_space(space, n, flags);
3341 error:
3342 isl_space_free(space);
3343 return NULL;
3346 /* Make sure "map" has room for at least "n" more basic maps.
3348 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3350 int i;
3351 struct isl_map *grown = NULL;
3353 if (!map)
3354 return NULL;
3355 isl_assert(map->ctx, n >= 0, goto error);
3356 if (map->n + n <= map->size)
3357 return map;
3358 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3359 if (!grown)
3360 goto error;
3361 for (i = 0; i < map->n; ++i) {
3362 grown->p[i] = isl_basic_map_copy(map->p[i]);
3363 if (!grown->p[i])
3364 goto error;
3365 grown->n++;
3367 isl_map_free(map);
3368 return grown;
3369 error:
3370 isl_map_free(grown);
3371 isl_map_free(map);
3372 return NULL;
3375 /* Make sure "set" has room for at least "n" more basic sets.
3377 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3379 return set_from_map(isl_map_grow(set_to_map(set), n));
3382 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3384 return isl_map_from_basic_map(bset);
3387 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3389 struct isl_map *map;
3391 if (!bmap)
3392 return NULL;
3394 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3395 return isl_map_add_basic_map(map, bmap);
3398 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3399 __isl_take isl_basic_set *bset)
3401 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3402 bset_to_bmap(bset)));
3405 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3407 return isl_map_free(set);
3410 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3412 int i;
3414 if (!set) {
3415 fprintf(out, "null set\n");
3416 return;
3419 fprintf(out, "%*s", indent, "");
3420 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3421 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3422 set->flags);
3423 for (i = 0; i < set->n; ++i) {
3424 fprintf(out, "%*s", indent, "");
3425 fprintf(out, "basic set %d:\n", i);
3426 isl_basic_set_print_internal(set->p[i], out, indent+4);
3430 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3432 int i;
3434 if (!map) {
3435 fprintf(out, "null map\n");
3436 return;
3439 fprintf(out, "%*s", indent, "");
3440 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3441 "flags: %x, n_name: %d\n",
3442 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3443 map->dim->n_out, map->flags, map->dim->n_id);
3444 for (i = 0; i < map->n; ++i) {
3445 fprintf(out, "%*s", indent, "");
3446 fprintf(out, "basic map %d:\n", i);
3447 isl_basic_map_print_internal(map->p[i], out, indent+4);
3451 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3452 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3454 struct isl_basic_map *bmap_domain;
3456 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3457 goto error;
3459 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3460 isl_assert(bset->ctx,
3461 isl_basic_map_compatible_domain(bmap, bset), goto error);
3463 bmap = isl_basic_map_cow(bmap);
3464 if (!bmap)
3465 goto error;
3466 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3467 bset->n_div, bset->n_eq, bset->n_ineq);
3468 bmap_domain = isl_basic_map_from_domain(bset);
3469 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3471 bmap = isl_basic_map_simplify(bmap);
3472 return isl_basic_map_finalize(bmap);
3473 error:
3474 isl_basic_map_free(bmap);
3475 isl_basic_set_free(bset);
3476 return NULL;
3479 /* Check that the space of "bset" is the same as that of the range of "bmap".
3481 static isl_stat isl_basic_map_check_compatible_range(
3482 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3484 isl_bool ok;
3486 ok = isl_basic_map_compatible_range(bmap, bset);
3487 if (ok < 0)
3488 return isl_stat_error;
3489 if (!ok)
3490 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3491 "incompatible spaces", return isl_stat_error);
3493 return isl_stat_ok;
3496 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3497 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3499 struct isl_basic_map *bmap_range;
3501 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3502 goto error;
3504 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3505 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3506 goto error;
3508 if (isl_basic_set_plain_is_universe(bset)) {
3509 isl_basic_set_free(bset);
3510 return bmap;
3513 bmap = isl_basic_map_cow(bmap);
3514 if (!bmap)
3515 goto error;
3516 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3517 bset->n_div, bset->n_eq, bset->n_ineq);
3518 bmap_range = bset_to_bmap(bset);
3519 bmap = add_constraints(bmap, bmap_range, 0, 0);
3521 bmap = isl_basic_map_simplify(bmap);
3522 return isl_basic_map_finalize(bmap);
3523 error:
3524 isl_basic_map_free(bmap);
3525 isl_basic_set_free(bset);
3526 return NULL;
3529 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3530 __isl_keep isl_vec *vec)
3532 int i;
3533 unsigned total;
3534 isl_int s;
3536 if (!bmap || !vec)
3537 return isl_bool_error;
3539 total = 1 + isl_basic_map_total_dim(bmap);
3540 if (total != vec->size)
3541 return isl_bool_false;
3543 isl_int_init(s);
3545 for (i = 0; i < bmap->n_eq; ++i) {
3546 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3547 if (!isl_int_is_zero(s)) {
3548 isl_int_clear(s);
3549 return isl_bool_false;
3553 for (i = 0; i < bmap->n_ineq; ++i) {
3554 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3555 if (isl_int_is_neg(s)) {
3556 isl_int_clear(s);
3557 return isl_bool_false;
3561 isl_int_clear(s);
3563 return isl_bool_true;
3566 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3567 __isl_keep isl_vec *vec)
3569 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3572 __isl_give isl_basic_map *isl_basic_map_intersect(
3573 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3575 struct isl_vec *sample = NULL;
3577 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3578 goto error;
3579 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3580 isl_space_dim(bmap1->dim, isl_dim_param) &&
3581 isl_space_dim(bmap2->dim, isl_dim_all) !=
3582 isl_space_dim(bmap2->dim, isl_dim_param))
3583 return isl_basic_map_intersect(bmap2, bmap1);
3585 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3586 isl_space_dim(bmap2->dim, isl_dim_param))
3587 isl_assert(bmap1->ctx,
3588 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3590 if (isl_basic_map_plain_is_empty(bmap1)) {
3591 isl_basic_map_free(bmap2);
3592 return bmap1;
3594 if (isl_basic_map_plain_is_empty(bmap2)) {
3595 isl_basic_map_free(bmap1);
3596 return bmap2;
3599 if (bmap1->sample &&
3600 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3601 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3602 sample = isl_vec_copy(bmap1->sample);
3603 else if (bmap2->sample &&
3604 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3605 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3606 sample = isl_vec_copy(bmap2->sample);
3608 bmap1 = isl_basic_map_cow(bmap1);
3609 if (!bmap1)
3610 goto error;
3611 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3612 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3613 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3615 if (!bmap1)
3616 isl_vec_free(sample);
3617 else if (sample) {
3618 isl_vec_free(bmap1->sample);
3619 bmap1->sample = sample;
3622 bmap1 = isl_basic_map_simplify(bmap1);
3623 return isl_basic_map_finalize(bmap1);
3624 error:
3625 if (sample)
3626 isl_vec_free(sample);
3627 isl_basic_map_free(bmap1);
3628 isl_basic_map_free(bmap2);
3629 return NULL;
3632 struct isl_basic_set *isl_basic_set_intersect(
3633 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3635 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3636 bset_to_bmap(bset2)));
3639 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3640 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3642 return isl_basic_set_intersect(bset1, bset2);
3645 /* Special case of isl_map_intersect, where both map1 and map2
3646 * are convex, without any divs and such that either map1 or map2
3647 * contains a single constraint. This constraint is then simply
3648 * added to the other map.
3650 static __isl_give isl_map *map_intersect_add_constraint(
3651 __isl_take isl_map *map1, __isl_take isl_map *map2)
3653 isl_assert(map1->ctx, map1->n == 1, goto error);
3654 isl_assert(map2->ctx, map1->n == 1, goto error);
3655 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3656 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3658 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3659 return isl_map_intersect(map2, map1);
3661 map1 = isl_map_cow(map1);
3662 if (!map1)
3663 goto error;
3664 if (isl_map_plain_is_empty(map1)) {
3665 isl_map_free(map2);
3666 return map1;
3668 if (map2->p[0]->n_eq == 1)
3669 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3670 else
3671 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3672 map2->p[0]->ineq[0]);
3674 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3675 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3676 if (!map1->p[0])
3677 goto error;
3679 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3680 isl_basic_map_free(map1->p[0]);
3681 map1->n = 0;
3684 isl_map_free(map2);
3686 map1 = isl_map_unmark_normalized(map1);
3687 return map1;
3688 error:
3689 isl_map_free(map1);
3690 isl_map_free(map2);
3691 return NULL;
3694 /* map2 may be either a parameter domain or a map living in the same
3695 * space as map1.
3697 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3698 __isl_take isl_map *map2)
3700 unsigned flags = 0;
3701 isl_bool equal;
3702 isl_map *result;
3703 int i, j;
3705 if (!map1 || !map2)
3706 goto error;
3708 if ((isl_map_plain_is_empty(map1) ||
3709 isl_map_plain_is_universe(map2)) &&
3710 isl_space_is_equal(map1->dim, map2->dim)) {
3711 isl_map_free(map2);
3712 return map1;
3714 if ((isl_map_plain_is_empty(map2) ||
3715 isl_map_plain_is_universe(map1)) &&
3716 isl_space_is_equal(map1->dim, map2->dim)) {
3717 isl_map_free(map1);
3718 return map2;
3721 if (map1->n == 1 && map2->n == 1 &&
3722 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3723 isl_space_is_equal(map1->dim, map2->dim) &&
3724 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3725 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3726 return map_intersect_add_constraint(map1, map2);
3728 equal = isl_map_plain_is_equal(map1, map2);
3729 if (equal < 0)
3730 goto error;
3731 if (equal) {
3732 isl_map_free(map2);
3733 return map1;
3736 if (isl_space_dim(map2->dim, isl_dim_all) !=
3737 isl_space_dim(map2->dim, isl_dim_param))
3738 isl_assert(map1->ctx,
3739 isl_space_is_equal(map1->dim, map2->dim), goto error);
3741 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3742 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3743 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3745 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3746 map1->n * map2->n, flags);
3747 if (!result)
3748 goto error;
3749 for (i = 0; i < map1->n; ++i)
3750 for (j = 0; j < map2->n; ++j) {
3751 struct isl_basic_map *part;
3752 part = isl_basic_map_intersect(
3753 isl_basic_map_copy(map1->p[i]),
3754 isl_basic_map_copy(map2->p[j]));
3755 if (isl_basic_map_is_empty(part) < 0)
3756 part = isl_basic_map_free(part);
3757 result = isl_map_add_basic_map(result, part);
3758 if (!result)
3759 goto error;
3761 isl_map_free(map1);
3762 isl_map_free(map2);
3763 return result;
3764 error:
3765 isl_map_free(map1);
3766 isl_map_free(map2);
3767 return NULL;
3770 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3771 __isl_take isl_map *map2)
3773 if (!map1 || !map2)
3774 goto error;
3775 if (!isl_space_is_equal(map1->dim, map2->dim))
3776 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3777 "spaces don't match", goto error);
3778 return map_intersect_internal(map1, map2);
3779 error:
3780 isl_map_free(map1);
3781 isl_map_free(map2);
3782 return NULL;
3785 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3786 __isl_take isl_map *map2)
3788 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3791 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3793 return set_from_map(isl_map_intersect(set_to_map(set1),
3794 set_to_map(set2)));
3797 /* map_intersect_internal accepts intersections
3798 * with parameter domains, so we can just call that function.
3800 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3801 __isl_take isl_set *params)
3803 return map_intersect_internal(map, params);
3806 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3807 __isl_take isl_map *map2)
3809 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3812 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3813 __isl_take isl_set *params)
3815 return isl_map_intersect_params(set, params);
3818 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3820 isl_space *space;
3821 unsigned pos, n1, n2;
3823 if (!bmap)
3824 return NULL;
3825 bmap = isl_basic_map_cow(bmap);
3826 if (!bmap)
3827 return NULL;
3828 space = isl_space_reverse(isl_space_copy(bmap->dim));
3829 pos = isl_basic_map_offset(bmap, isl_dim_in);
3830 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3831 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3832 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3833 return isl_basic_map_reset_space(bmap, space);
3836 static __isl_give isl_basic_map *basic_map_space_reset(
3837 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3839 isl_space *space;
3841 if (!bmap)
3842 return NULL;
3843 if (!isl_space_is_named_or_nested(bmap->dim, type))
3844 return bmap;
3846 space = isl_basic_map_get_space(bmap);
3847 space = isl_space_reset(space, type);
3848 bmap = isl_basic_map_reset_space(bmap, space);
3849 return bmap;
3852 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3853 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3854 unsigned pos, unsigned n)
3856 isl_bool rational, is_empty;
3857 isl_space *res_space;
3858 struct isl_basic_map *res;
3859 struct isl_dim_map *dim_map;
3860 unsigned total, off;
3861 enum isl_dim_type t;
3863 if (n == 0)
3864 return basic_map_space_reset(bmap, type);
3866 is_empty = isl_basic_map_plain_is_empty(bmap);
3867 if (is_empty < 0)
3868 return isl_basic_map_free(bmap);
3869 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3870 type, pos, n);
3871 if (!res_space)
3872 return isl_basic_map_free(bmap);
3873 if (is_empty) {
3874 isl_basic_map_free(bmap);
3875 return isl_basic_map_empty(res_space);
3878 total = isl_basic_map_total_dim(bmap) + n;
3879 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3880 off = 0;
3881 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3882 if (t != type) {
3883 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3884 } else {
3885 unsigned size = isl_basic_map_dim(bmap, t);
3886 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3887 0, pos, off);
3888 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3889 pos, size - pos, off + pos + n);
3891 off += isl_space_dim(res_space, t);
3893 isl_dim_map_div(dim_map, bmap, off);
3895 res = isl_basic_map_alloc_space(res_space,
3896 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3897 rational = isl_basic_map_is_rational(bmap);
3898 if (rational < 0)
3899 res = isl_basic_map_free(res);
3900 if (rational)
3901 res = isl_basic_map_set_rational(res);
3902 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3903 return isl_basic_map_finalize(res);
3906 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3907 __isl_take isl_basic_set *bset,
3908 enum isl_dim_type type, unsigned pos, unsigned n)
3910 return isl_basic_map_insert_dims(bset, type, pos, n);
3913 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3914 enum isl_dim_type type, unsigned n)
3916 if (!bmap)
3917 return NULL;
3918 return isl_basic_map_insert_dims(bmap, type,
3919 isl_basic_map_dim(bmap, type), n);
3922 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3923 enum isl_dim_type type, unsigned n)
3925 if (!bset)
3926 return NULL;
3927 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3928 return isl_basic_map_add_dims(bset, type, n);
3929 error:
3930 isl_basic_set_free(bset);
3931 return NULL;
3934 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3935 enum isl_dim_type type)
3937 isl_space *space;
3939 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3940 return map;
3942 space = isl_map_get_space(map);
3943 space = isl_space_reset(space, type);
3944 map = isl_map_reset_space(map, space);
3945 return map;
3948 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3949 enum isl_dim_type type, unsigned pos, unsigned n)
3951 int i;
3953 if (n == 0)
3954 return map_space_reset(map, type);
3956 map = isl_map_cow(map);
3957 if (!map)
3958 return NULL;
3960 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3961 if (!map->dim)
3962 goto error;
3964 for (i = 0; i < map->n; ++i) {
3965 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3966 if (!map->p[i])
3967 goto error;
3970 return map;
3971 error:
3972 isl_map_free(map);
3973 return NULL;
3976 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3977 enum isl_dim_type type, unsigned pos, unsigned n)
3979 return isl_map_insert_dims(set, type, pos, n);
3982 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3983 enum isl_dim_type type, unsigned n)
3985 if (!map)
3986 return NULL;
3987 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3990 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3991 enum isl_dim_type type, unsigned n)
3993 if (!set)
3994 return NULL;
3995 isl_assert(set->ctx, type != isl_dim_in, goto error);
3996 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3997 error:
3998 isl_set_free(set);
3999 return NULL;
4002 __isl_give isl_basic_map *isl_basic_map_move_dims(
4003 __isl_take isl_basic_map *bmap,
4004 enum isl_dim_type dst_type, unsigned dst_pos,
4005 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4007 struct isl_dim_map *dim_map;
4008 struct isl_basic_map *res;
4009 enum isl_dim_type t;
4010 unsigned total, off;
4012 if (!bmap)
4013 return NULL;
4014 if (n == 0) {
4015 bmap = isl_basic_map_reset(bmap, src_type);
4016 bmap = isl_basic_map_reset(bmap, dst_type);
4017 return bmap;
4020 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4021 return isl_basic_map_free(bmap);
4023 if (dst_type == src_type && dst_pos == src_pos)
4024 return bmap;
4026 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4028 if (pos(bmap->dim, dst_type) + dst_pos ==
4029 pos(bmap->dim, src_type) + src_pos +
4030 ((src_type < dst_type) ? n : 0)) {
4031 bmap = isl_basic_map_cow(bmap);
4032 if (!bmap)
4033 return NULL;
4035 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4036 src_type, src_pos, n);
4037 if (!bmap->dim)
4038 goto error;
4040 bmap = isl_basic_map_finalize(bmap);
4042 return bmap;
4045 total = isl_basic_map_total_dim(bmap);
4046 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4048 off = 0;
4049 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4050 unsigned size = isl_space_dim(bmap->dim, t);
4051 if (t == dst_type) {
4052 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4053 0, dst_pos, off);
4054 off += dst_pos;
4055 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4056 src_pos, n, off);
4057 off += n;
4058 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4059 dst_pos, size - dst_pos, off);
4060 off += size - dst_pos;
4061 } else if (t == src_type) {
4062 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4063 0, src_pos, off);
4064 off += src_pos;
4065 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4066 src_pos + n, size - src_pos - n, off);
4067 off += size - src_pos - n;
4068 } else {
4069 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4070 off += size;
4073 isl_dim_map_div(dim_map, bmap, off);
4075 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4076 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4077 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4078 if (!bmap)
4079 goto error;
4081 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4082 src_type, src_pos, n);
4083 if (!bmap->dim)
4084 goto error;
4086 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4087 bmap = isl_basic_map_gauss(bmap, NULL);
4088 bmap = isl_basic_map_finalize(bmap);
4090 return bmap;
4091 error:
4092 isl_basic_map_free(bmap);
4093 return NULL;
4096 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4097 enum isl_dim_type dst_type, unsigned dst_pos,
4098 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4100 isl_basic_map *bmap = bset_to_bmap(bset);
4101 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4102 src_type, src_pos, n);
4103 return bset_from_bmap(bmap);
4106 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4107 enum isl_dim_type dst_type, unsigned dst_pos,
4108 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4110 if (!set)
4111 return NULL;
4112 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4113 return set_from_map(isl_map_move_dims(set_to_map(set),
4114 dst_type, dst_pos, src_type, src_pos, n));
4115 error:
4116 isl_set_free(set);
4117 return NULL;
4120 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4121 enum isl_dim_type dst_type, unsigned dst_pos,
4122 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4124 int i;
4126 if (n == 0) {
4127 map = isl_map_reset(map, src_type);
4128 map = isl_map_reset(map, dst_type);
4129 return map;
4132 if (isl_map_check_range(map, src_type, src_pos, n))
4133 return isl_map_free(map);
4135 if (dst_type == src_type && dst_pos == src_pos)
4136 return map;
4138 isl_assert(map->ctx, dst_type != src_type, goto error);
4140 map = isl_map_cow(map);
4141 if (!map)
4142 return NULL;
4144 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4145 if (!map->dim)
4146 goto error;
4148 for (i = 0; i < map->n; ++i) {
4149 map->p[i] = isl_basic_map_move_dims(map->p[i],
4150 dst_type, dst_pos,
4151 src_type, src_pos, n);
4152 if (!map->p[i])
4153 goto error;
4156 return map;
4157 error:
4158 isl_map_free(map);
4159 return NULL;
4162 /* Move the specified dimensions to the last columns right before
4163 * the divs. Don't change the dimension specification of bmap.
4164 * That's the responsibility of the caller.
4166 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4167 enum isl_dim_type type, unsigned first, unsigned n)
4169 struct isl_dim_map *dim_map;
4170 struct isl_basic_map *res;
4171 enum isl_dim_type t;
4172 unsigned total, off;
4174 if (!bmap)
4175 return NULL;
4176 if (pos(bmap->dim, type) + first + n ==
4177 1 + isl_space_dim(bmap->dim, isl_dim_all))
4178 return bmap;
4180 total = isl_basic_map_total_dim(bmap);
4181 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4183 off = 0;
4184 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4185 unsigned size = isl_space_dim(bmap->dim, t);
4186 if (t == type) {
4187 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4188 0, first, off);
4189 off += first;
4190 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4191 first, n, total - bmap->n_div - n);
4192 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4193 first + n, size - (first + n), off);
4194 off += size - (first + n);
4195 } else {
4196 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4197 off += size;
4200 isl_dim_map_div(dim_map, bmap, off + n);
4202 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4203 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4204 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4205 return res;
4208 /* Insert "n" rows in the divs of "bmap".
4210 * The number of columns is not changed, which means that the last
4211 * dimensions of "bmap" are being reintepreted as the new divs.
4212 * The space of "bmap" is not adjusted, however, which means
4213 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4214 * from the space of "bmap" is the responsibility of the caller.
4216 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4217 int n)
4219 int i;
4220 size_t row_size;
4221 isl_int **new_div;
4222 isl_int *old;
4224 bmap = isl_basic_map_cow(bmap);
4225 if (!bmap)
4226 return NULL;
4228 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4229 old = bmap->block2.data;
4230 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4231 (bmap->extra + n) * (1 + row_size));
4232 if (!bmap->block2.data)
4233 return isl_basic_map_free(bmap);
4234 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4235 if (!new_div)
4236 return isl_basic_map_free(bmap);
4237 for (i = 0; i < n; ++i) {
4238 new_div[i] = bmap->block2.data +
4239 (bmap->extra + i) * (1 + row_size);
4240 isl_seq_clr(new_div[i], 1 + row_size);
4242 for (i = 0; i < bmap->extra; ++i)
4243 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4244 free(bmap->div);
4245 bmap->div = new_div;
4246 bmap->n_div += n;
4247 bmap->extra += n;
4249 return bmap;
4252 /* Drop constraints from "bmap" that only involve the variables
4253 * of "type" in the range [first, first + n] that are not related
4254 * to any of the variables outside that interval.
4255 * These constraints cannot influence the values for the variables
4256 * outside the interval, except in case they cause "bmap" to be empty.
4257 * Only drop the constraints if "bmap" is known to be non-empty.
4259 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4260 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4261 unsigned first, unsigned n)
4263 int i;
4264 int *groups;
4265 unsigned dim, n_div;
4266 isl_bool non_empty;
4268 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4269 if (non_empty < 0)
4270 return isl_basic_map_free(bmap);
4271 if (!non_empty)
4272 return bmap;
4274 dim = isl_basic_map_dim(bmap, isl_dim_all);
4275 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4276 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4277 if (!groups)
4278 return isl_basic_map_free(bmap);
4279 first += isl_basic_map_offset(bmap, type) - 1;
4280 for (i = 0; i < first; ++i)
4281 groups[i] = -1;
4282 for (i = first + n; i < dim - n_div; ++i)
4283 groups[i] = -1;
4285 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4287 return bmap;
4290 /* Turn the n dimensions of type type, starting at first
4291 * into existentially quantified variables.
4293 * If a subset of the projected out variables are unrelated
4294 * to any of the variables that remain, then the constraints
4295 * involving this subset are simply dropped first.
4297 __isl_give isl_basic_map *isl_basic_map_project_out(
4298 __isl_take isl_basic_map *bmap,
4299 enum isl_dim_type type, unsigned first, unsigned n)
4301 isl_bool empty;
4303 if (n == 0)
4304 return basic_map_space_reset(bmap, type);
4305 if (type == isl_dim_div)
4306 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4307 "cannot project out existentially quantified variables",
4308 return isl_basic_map_free(bmap));
4310 empty = isl_basic_map_plain_is_empty(bmap);
4311 if (empty < 0)
4312 return isl_basic_map_free(bmap);
4313 if (empty)
4314 bmap = isl_basic_map_set_to_empty(bmap);
4316 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4317 if (!bmap)
4318 return NULL;
4320 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4321 return isl_basic_map_remove_dims(bmap, type, first, n);
4323 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4324 return isl_basic_map_free(bmap);
4326 bmap = move_last(bmap, type, first, n);
4327 bmap = isl_basic_map_cow(bmap);
4328 bmap = insert_div_rows(bmap, n);
4329 if (!bmap)
4330 return NULL;
4332 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4333 if (!bmap->dim)
4334 goto error;
4335 bmap = isl_basic_map_simplify(bmap);
4336 bmap = isl_basic_map_drop_redundant_divs(bmap);
4337 return isl_basic_map_finalize(bmap);
4338 error:
4339 isl_basic_map_free(bmap);
4340 return NULL;
4343 /* Turn the n dimensions of type type, starting at first
4344 * into existentially quantified variables.
4346 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4347 enum isl_dim_type type, unsigned first, unsigned n)
4349 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4350 type, first, n));
4353 /* Turn the n dimensions of type type, starting at first
4354 * into existentially quantified variables.
4356 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4357 enum isl_dim_type type, unsigned first, unsigned n)
4359 int i;
4361 if (n == 0)
4362 return map_space_reset(map, type);
4364 if (isl_map_check_range(map, type, first, n) < 0)
4365 return isl_map_free(map);
4367 map = isl_map_cow(map);
4368 if (!map)
4369 return NULL;
4371 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4372 if (!map->dim)
4373 goto error;
4375 for (i = 0; i < map->n; ++i) {
4376 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4377 if (!map->p[i])
4378 goto error;
4381 return map;
4382 error:
4383 isl_map_free(map);
4384 return NULL;
4387 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4388 * into existentially quantified variables.
4390 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4391 enum isl_dim_type type, unsigned first, unsigned n)
4393 unsigned dim;
4395 if (isl_map_check_range(map, type, first, n) < 0)
4396 return isl_map_free(map);
4397 dim = isl_map_dim(map, type);
4398 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4399 map = isl_map_project_out(map, type, 0, first);
4400 return map;
4403 /* Turn the n dimensions of type type, starting at first
4404 * into existentially quantified variables.
4406 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4407 enum isl_dim_type type, unsigned first, unsigned n)
4409 return set_from_map(isl_map_project_out(set_to_map(set),
4410 type, first, n));
4413 /* Return a map that projects the elements in "set" onto their
4414 * "n" set dimensions starting at "first".
4415 * "type" should be equal to isl_dim_set.
4417 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4418 enum isl_dim_type type, unsigned first, unsigned n)
4420 int i;
4421 isl_map *map;
4423 if (type != isl_dim_set)
4424 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4425 "only set dimensions can be projected out", goto error);
4426 if (isl_set_check_range(set, type, first, n) < 0)
4427 return isl_set_free(set);
4429 map = isl_map_from_domain(set);
4430 map = isl_map_add_dims(map, isl_dim_out, n);
4431 for (i = 0; i < n; ++i)
4432 map = isl_map_equate(map, isl_dim_in, first + i,
4433 isl_dim_out, i);
4434 return map;
4435 error:
4436 isl_set_free(set);
4437 return NULL;
4440 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4441 unsigned n)
4443 int i, j;
4445 for (i = 0; i < n; ++i) {
4446 j = isl_basic_map_alloc_div(bmap);
4447 if (j < 0)
4448 goto error;
4449 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4451 return bmap;
4452 error:
4453 isl_basic_map_free(bmap);
4454 return NULL;
4457 struct isl_basic_map *isl_basic_map_apply_range(
4458 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4460 isl_space *space_result = NULL;
4461 struct isl_basic_map *bmap;
4462 unsigned n_in, n_out, n, nparam, total, pos;
4463 struct isl_dim_map *dim_map1, *dim_map2;
4465 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4466 goto error;
4467 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4468 bmap2->dim, isl_dim_in))
4469 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4470 "spaces don't match", goto error);
4472 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4473 isl_space_copy(bmap2->dim));
4475 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4476 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4477 n = isl_basic_map_dim(bmap1, isl_dim_out);
4478 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4480 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4481 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4482 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4483 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4484 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4485 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4486 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4487 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4488 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4492 bmap = isl_basic_map_alloc_space(space_result,
4493 bmap1->n_div + bmap2->n_div + n,
4494 bmap1->n_eq + bmap2->n_eq,
4495 bmap1->n_ineq + bmap2->n_ineq);
4496 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4497 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4498 bmap = add_divs(bmap, n);
4499 bmap = isl_basic_map_simplify(bmap);
4500 bmap = isl_basic_map_drop_redundant_divs(bmap);
4501 return isl_basic_map_finalize(bmap);
4502 error:
4503 isl_basic_map_free(bmap1);
4504 isl_basic_map_free(bmap2);
4505 return NULL;
4508 struct isl_basic_set *isl_basic_set_apply(
4509 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4511 if (!bset || !bmap)
4512 goto error;
4514 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4515 goto error);
4517 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4518 bmap));
4519 error:
4520 isl_basic_set_free(bset);
4521 isl_basic_map_free(bmap);
4522 return NULL;
4525 struct isl_basic_map *isl_basic_map_apply_domain(
4526 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4528 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4529 goto error;
4530 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4531 bmap2->dim, isl_dim_in))
4532 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4533 "spaces don't match", goto error);
4535 bmap1 = isl_basic_map_reverse(bmap1);
4536 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4537 return isl_basic_map_reverse(bmap1);
4538 error:
4539 isl_basic_map_free(bmap1);
4540 isl_basic_map_free(bmap2);
4541 return NULL;
4544 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4545 * A \cap B -> f(A) + f(B)
4547 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4548 __isl_take isl_basic_map *bmap2)
4550 unsigned n_in, n_out, nparam, total, pos;
4551 struct isl_basic_map *bmap = NULL;
4552 struct isl_dim_map *dim_map1, *dim_map2;
4553 int i;
4555 if (!bmap1 || !bmap2)
4556 goto error;
4558 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4559 goto error);
4561 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4562 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4563 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4565 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4566 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4567 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4568 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4569 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4570 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4571 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4572 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4573 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4574 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4575 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4577 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4578 bmap1->n_div + bmap2->n_div + 2 * n_out,
4579 bmap1->n_eq + bmap2->n_eq + n_out,
4580 bmap1->n_ineq + bmap2->n_ineq);
4581 for (i = 0; i < n_out; ++i) {
4582 int j = isl_basic_map_alloc_equality(bmap);
4583 if (j < 0)
4584 goto error;
4585 isl_seq_clr(bmap->eq[j], 1+total);
4586 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4587 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4588 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4590 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4591 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4592 bmap = add_divs(bmap, 2 * n_out);
4594 bmap = isl_basic_map_simplify(bmap);
4595 return isl_basic_map_finalize(bmap);
4596 error:
4597 isl_basic_map_free(bmap);
4598 isl_basic_map_free(bmap1);
4599 isl_basic_map_free(bmap2);
4600 return NULL;
4603 /* Given two maps A -> f(A) and B -> g(B), construct a map
4604 * A \cap B -> f(A) + f(B)
4606 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4607 __isl_take isl_map *map2)
4609 struct isl_map *result;
4610 int i, j;
4612 if (!map1 || !map2)
4613 goto error;
4615 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4617 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4618 map1->n * map2->n, 0);
4619 if (!result)
4620 goto error;
4621 for (i = 0; i < map1->n; ++i)
4622 for (j = 0; j < map2->n; ++j) {
4623 struct isl_basic_map *part;
4624 part = isl_basic_map_sum(
4625 isl_basic_map_copy(map1->p[i]),
4626 isl_basic_map_copy(map2->p[j]));
4627 if (isl_basic_map_is_empty(part))
4628 isl_basic_map_free(part);
4629 else
4630 result = isl_map_add_basic_map(result, part);
4631 if (!result)
4632 goto error;
4634 isl_map_free(map1);
4635 isl_map_free(map2);
4636 return result;
4637 error:
4638 isl_map_free(map1);
4639 isl_map_free(map2);
4640 return NULL;
4643 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4644 __isl_take isl_set *set2)
4646 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4649 /* Given a basic map A -> f(A), construct A -> -f(A).
4651 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4653 int i, j;
4654 unsigned off, n;
4656 bmap = isl_basic_map_cow(bmap);
4657 if (!bmap)
4658 return NULL;
4660 n = isl_basic_map_dim(bmap, isl_dim_out);
4661 off = isl_basic_map_offset(bmap, isl_dim_out);
4662 for (i = 0; i < bmap->n_eq; ++i)
4663 for (j = 0; j < n; ++j)
4664 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4665 for (i = 0; i < bmap->n_ineq; ++i)
4666 for (j = 0; j < n; ++j)
4667 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4668 for (i = 0; i < bmap->n_div; ++i)
4669 for (j = 0; j < n; ++j)
4670 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4671 bmap = isl_basic_map_gauss(bmap, NULL);
4672 return isl_basic_map_finalize(bmap);
4675 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4677 return isl_basic_map_neg(bset);
4680 /* Given a map A -> f(A), construct A -> -f(A).
4682 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4684 int i;
4686 map = isl_map_cow(map);
4687 if (!map)
4688 return NULL;
4690 for (i = 0; i < map->n; ++i) {
4691 map->p[i] = isl_basic_map_neg(map->p[i]);
4692 if (!map->p[i])
4693 goto error;
4696 return map;
4697 error:
4698 isl_map_free(map);
4699 return NULL;
4702 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4704 return set_from_map(isl_map_neg(set_to_map(set)));
4707 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4708 * A -> floor(f(A)/d).
4710 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4711 isl_int d)
4713 unsigned n_in, n_out, nparam, total, pos;
4714 struct isl_basic_map *result = NULL;
4715 struct isl_dim_map *dim_map;
4716 int i;
4718 if (!bmap)
4719 return NULL;
4721 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4722 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4723 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4725 total = nparam + n_in + n_out + bmap->n_div + n_out;
4726 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4727 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4728 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4729 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4730 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4732 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4733 bmap->n_div + n_out,
4734 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4735 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4736 result = add_divs(result, n_out);
4737 for (i = 0; i < n_out; ++i) {
4738 int j;
4739 j = isl_basic_map_alloc_inequality(result);
4740 if (j < 0)
4741 goto error;
4742 isl_seq_clr(result->ineq[j], 1+total);
4743 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4744 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4745 j = isl_basic_map_alloc_inequality(result);
4746 if (j < 0)
4747 goto error;
4748 isl_seq_clr(result->ineq[j], 1+total);
4749 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4750 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4751 isl_int_sub_ui(result->ineq[j][0], d, 1);
4754 result = isl_basic_map_simplify(result);
4755 return isl_basic_map_finalize(result);
4756 error:
4757 isl_basic_map_free(result);
4758 return NULL;
4761 /* Given a map A -> f(A) and an integer d, construct a map
4762 * A -> floor(f(A)/d).
4764 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4766 int i;
4768 map = isl_map_cow(map);
4769 if (!map)
4770 return NULL;
4772 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4773 for (i = 0; i < map->n; ++i) {
4774 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4775 if (!map->p[i])
4776 goto error;
4778 map = isl_map_unmark_normalized(map);
4780 return map;
4781 error:
4782 isl_map_free(map);
4783 return NULL;
4786 /* Given a map A -> f(A) and an integer d, construct a map
4787 * A -> floor(f(A)/d).
4789 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4790 __isl_take isl_val *d)
4792 if (!map || !d)
4793 goto error;
4794 if (!isl_val_is_int(d))
4795 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4796 "expecting integer denominator", goto error);
4797 map = isl_map_floordiv(map, d->n);
4798 isl_val_free(d);
4799 return map;
4800 error:
4801 isl_map_free(map);
4802 isl_val_free(d);
4803 return NULL;
4806 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4807 unsigned pos)
4809 int i;
4810 unsigned nparam;
4811 unsigned n_in;
4813 i = isl_basic_map_alloc_equality(bmap);
4814 if (i < 0)
4815 goto error;
4816 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4817 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4818 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4819 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4820 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4821 return isl_basic_map_finalize(bmap);
4822 error:
4823 isl_basic_map_free(bmap);
4824 return NULL;
4827 /* Add a constraint to "bmap" expressing i_pos < o_pos
4829 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4830 unsigned pos)
4832 int i;
4833 unsigned nparam;
4834 unsigned n_in;
4836 i = isl_basic_map_alloc_inequality(bmap);
4837 if (i < 0)
4838 goto error;
4839 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4840 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4841 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4842 isl_int_set_si(bmap->ineq[i][0], -1);
4843 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4844 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4845 return isl_basic_map_finalize(bmap);
4846 error:
4847 isl_basic_map_free(bmap);
4848 return NULL;
4851 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4853 static __isl_give isl_basic_map *var_less_or_equal(
4854 __isl_take isl_basic_map *bmap, unsigned pos)
4856 int i;
4857 unsigned nparam;
4858 unsigned n_in;
4860 i = isl_basic_map_alloc_inequality(bmap);
4861 if (i < 0)
4862 goto error;
4863 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4864 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4865 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4866 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4867 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4868 return isl_basic_map_finalize(bmap);
4869 error:
4870 isl_basic_map_free(bmap);
4871 return NULL;
4874 /* Add a constraint to "bmap" expressing i_pos > o_pos
4876 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4877 unsigned pos)
4879 int i;
4880 unsigned nparam;
4881 unsigned n_in;
4883 i = isl_basic_map_alloc_inequality(bmap);
4884 if (i < 0)
4885 goto error;
4886 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4887 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4888 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4889 isl_int_set_si(bmap->ineq[i][0], -1);
4890 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4891 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4892 return isl_basic_map_finalize(bmap);
4893 error:
4894 isl_basic_map_free(bmap);
4895 return NULL;
4898 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4900 static __isl_give isl_basic_map *var_more_or_equal(
4901 __isl_take isl_basic_map *bmap, unsigned pos)
4903 int i;
4904 unsigned nparam;
4905 unsigned n_in;
4907 i = isl_basic_map_alloc_inequality(bmap);
4908 if (i < 0)
4909 goto error;
4910 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4911 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4912 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4913 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4914 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4915 return isl_basic_map_finalize(bmap);
4916 error:
4917 isl_basic_map_free(bmap);
4918 return NULL;
4921 __isl_give isl_basic_map *isl_basic_map_equal(
4922 __isl_take isl_space *space, unsigned n_equal)
4924 int i;
4925 struct isl_basic_map *bmap;
4926 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4927 if (!bmap)
4928 return NULL;
4929 for (i = 0; i < n_equal && bmap; ++i)
4930 bmap = var_equal(bmap, i);
4931 return isl_basic_map_finalize(bmap);
4934 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4936 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4937 unsigned pos)
4939 int i;
4940 struct isl_basic_map *bmap;
4941 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4942 if (!bmap)
4943 return NULL;
4944 for (i = 0; i < pos && bmap; ++i)
4945 bmap = var_equal(bmap, i);
4946 if (bmap)
4947 bmap = var_less(bmap, pos);
4948 return isl_basic_map_finalize(bmap);
4951 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4953 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4954 __isl_take isl_space *space, unsigned pos)
4956 int i;
4957 isl_basic_map *bmap;
4959 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4960 for (i = 0; i < pos; ++i)
4961 bmap = var_equal(bmap, i);
4962 bmap = var_less_or_equal(bmap, pos);
4963 return isl_basic_map_finalize(bmap);
4966 /* Return a relation on "space" expressing i_pos > o_pos
4968 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4969 unsigned pos)
4971 int i;
4972 struct isl_basic_map *bmap;
4973 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4974 if (!bmap)
4975 return NULL;
4976 for (i = 0; i < pos && bmap; ++i)
4977 bmap = var_equal(bmap, i);
4978 if (bmap)
4979 bmap = var_more(bmap, pos);
4980 return isl_basic_map_finalize(bmap);
4983 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4985 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4986 __isl_take isl_space *space, unsigned pos)
4988 int i;
4989 isl_basic_map *bmap;
4991 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4992 for (i = 0; i < pos; ++i)
4993 bmap = var_equal(bmap, i);
4994 bmap = var_more_or_equal(bmap, pos);
4995 return isl_basic_map_finalize(bmap);
4998 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4999 unsigned n, int equal)
5001 struct isl_map *map;
5002 int i;
5004 if (n == 0 && equal)
5005 return isl_map_universe(space);
5007 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5009 for (i = 0; i + 1 < n; ++i)
5010 map = isl_map_add_basic_map(map,
5011 isl_basic_map_less_at(isl_space_copy(space), i));
5012 if (n > 0) {
5013 if (equal)
5014 map = isl_map_add_basic_map(map,
5015 isl_basic_map_less_or_equal_at(space, n - 1));
5016 else
5017 map = isl_map_add_basic_map(map,
5018 isl_basic_map_less_at(space, n - 1));
5019 } else
5020 isl_space_free(space);
5022 return map;
5025 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5027 if (!space)
5028 return NULL;
5029 return map_lex_lte_first(space, space->n_out, equal);
5032 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5034 return map_lex_lte_first(dim, n, 0);
5037 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5039 return map_lex_lte_first(dim, n, 1);
5042 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5044 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5047 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5049 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5052 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5053 unsigned n, int equal)
5055 struct isl_map *map;
5056 int i;
5058 if (n == 0 && equal)
5059 return isl_map_universe(space);
5061 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5063 for (i = 0; i + 1 < n; ++i)
5064 map = isl_map_add_basic_map(map,
5065 isl_basic_map_more_at(isl_space_copy(space), i));
5066 if (n > 0) {
5067 if (equal)
5068 map = isl_map_add_basic_map(map,
5069 isl_basic_map_more_or_equal_at(space, n - 1));
5070 else
5071 map = isl_map_add_basic_map(map,
5072 isl_basic_map_more_at(space, n - 1));
5073 } else
5074 isl_space_free(space);
5076 return map;
5079 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5081 if (!space)
5082 return NULL;
5083 return map_lex_gte_first(space, space->n_out, equal);
5086 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5088 return map_lex_gte_first(dim, n, 0);
5091 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5093 return map_lex_gte_first(dim, n, 1);
5096 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5098 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5101 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5103 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5106 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5107 __isl_take isl_set *set2)
5109 isl_map *map;
5110 map = isl_map_lex_le(isl_set_get_space(set1));
5111 map = isl_map_intersect_domain(map, set1);
5112 map = isl_map_intersect_range(map, set2);
5113 return map;
5116 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5117 __isl_take isl_set *set2)
5119 isl_map *map;
5120 map = isl_map_lex_lt(isl_set_get_space(set1));
5121 map = isl_map_intersect_domain(map, set1);
5122 map = isl_map_intersect_range(map, set2);
5123 return map;
5126 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5127 __isl_take isl_set *set2)
5129 isl_map *map;
5130 map = isl_map_lex_ge(isl_set_get_space(set1));
5131 map = isl_map_intersect_domain(map, set1);
5132 map = isl_map_intersect_range(map, set2);
5133 return map;
5136 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5137 __isl_take isl_set *set2)
5139 isl_map *map;
5140 map = isl_map_lex_gt(isl_set_get_space(set1));
5141 map = isl_map_intersect_domain(map, set1);
5142 map = isl_map_intersect_range(map, set2);
5143 return map;
5146 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5147 __isl_take isl_map *map2)
5149 isl_map *map;
5150 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5151 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5152 map = isl_map_apply_range(map, isl_map_reverse(map2));
5153 return map;
5156 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5157 __isl_take isl_map *map2)
5159 isl_map *map;
5160 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5161 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5162 map = isl_map_apply_range(map, isl_map_reverse(map2));
5163 return map;
5166 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5167 __isl_take isl_map *map2)
5169 isl_map *map;
5170 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5171 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5172 map = isl_map_apply_range(map, isl_map_reverse(map2));
5173 return map;
5176 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5177 __isl_take isl_map *map2)
5179 isl_map *map;
5180 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5181 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5182 map = isl_map_apply_range(map, isl_map_reverse(map2));
5183 return map;
5186 /* For a div d = floor(f/m), add the constraint
5188 * f - m d >= 0
5190 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5191 unsigned pos, isl_int *div)
5193 int i;
5194 unsigned total = isl_basic_map_total_dim(bmap);
5196 i = isl_basic_map_alloc_inequality(bmap);
5197 if (i < 0)
5198 return isl_stat_error;
5199 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5200 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5202 return isl_stat_ok;
5205 /* For a div d = floor(f/m), add the constraint
5207 * -(f-(m-1)) + m d >= 0
5209 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5210 unsigned pos, isl_int *div)
5212 int i;
5213 unsigned total = isl_basic_map_total_dim(bmap);
5215 i = isl_basic_map_alloc_inequality(bmap);
5216 if (i < 0)
5217 return isl_stat_error;
5218 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5219 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5220 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5221 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5223 return isl_stat_ok;
5226 /* For a div d = floor(f/m), add the constraints
5228 * f - m d >= 0
5229 * -(f-(m-1)) + m d >= 0
5231 * Note that the second constraint is the negation of
5233 * f - m d >= m
5235 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5236 unsigned pos, isl_int *div)
5238 if (add_upper_div_constraint(bmap, pos, div) < 0)
5239 return -1;
5240 if (add_lower_div_constraint(bmap, pos, div) < 0)
5241 return -1;
5242 return 0;
5245 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5246 unsigned pos, isl_int *div)
5248 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5249 pos, div);
5252 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5253 unsigned div)
5255 unsigned total = isl_basic_map_total_dim(bmap);
5256 unsigned div_pos = total - bmap->n_div + div;
5258 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5259 bmap->div[div]);
5262 /* For each known div d = floor(f/m), add the constraints
5264 * f - m d >= 0
5265 * -(f-(m-1)) + m d >= 0
5267 * Remove duplicate constraints in case of some these div constraints
5268 * already appear in "bmap".
5270 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5271 __isl_take isl_basic_map *bmap)
5273 unsigned n_div;
5275 if (!bmap)
5276 return NULL;
5277 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5278 if (n_div == 0)
5279 return bmap;
5281 bmap = add_known_div_constraints(bmap);
5282 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5283 bmap = isl_basic_map_finalize(bmap);
5284 return bmap;
5287 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5289 * In particular, if this div is of the form d = floor(f/m),
5290 * then add the constraint
5292 * f - m d >= 0
5294 * if sign < 0 or the constraint
5296 * -(f-(m-1)) + m d >= 0
5298 * if sign > 0.
5300 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5301 unsigned div, int sign)
5303 unsigned total;
5304 unsigned div_pos;
5306 if (!bmap)
5307 return -1;
5309 total = isl_basic_map_total_dim(bmap);
5310 div_pos = total - bmap->n_div + div;
5312 if (sign < 0)
5313 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5314 else
5315 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5318 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5319 __isl_take isl_basic_map *bmap)
5321 if (!bmap)
5322 goto error;
5323 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5324 bmap->n_div == 0 &&
5325 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5326 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5327 return bset_from_bmap(bmap);
5328 bmap = isl_basic_map_cow(bmap);
5329 if (!bmap)
5330 goto error;
5331 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5332 if (!bmap->dim)
5333 goto error;
5334 bmap->extra -= bmap->n_div;
5335 bmap->n_div = 0;
5336 bmap = isl_basic_map_finalize(bmap);
5337 return bset_from_bmap(bmap);
5338 error:
5339 isl_basic_map_free(bmap);
5340 return NULL;
5343 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5344 __isl_take isl_basic_set *bset)
5346 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5349 /* Replace each element in "list" by the result of applying
5350 * isl_basic_map_underlying_set to the element.
5352 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5353 __isl_take isl_basic_map_list *list)
5355 int i, n;
5357 if (!list)
5358 return NULL;
5360 n = isl_basic_map_list_n_basic_map(list);
5361 for (i = 0; i < n; ++i) {
5362 isl_basic_map *bmap;
5363 isl_basic_set *bset;
5365 bmap = isl_basic_map_list_get_basic_map(list, i);
5366 bset = isl_basic_set_underlying_set(bmap);
5367 list = isl_basic_set_list_set_basic_set(list, i, bset);
5370 return list;
5373 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5374 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5376 struct isl_basic_map *bmap;
5377 struct isl_ctx *ctx;
5378 unsigned total;
5379 int i;
5381 if (!bset || !like)
5382 goto error;
5383 ctx = bset->ctx;
5384 if (isl_basic_set_check_no_params(bset) < 0 ||
5385 isl_basic_set_check_no_locals(bset) < 0)
5386 goto error;
5387 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5388 goto error);
5389 if (like->n_div == 0) {
5390 isl_space *space = isl_basic_map_get_space(like);
5391 isl_basic_map_free(like);
5392 return isl_basic_map_reset_space(bset, space);
5394 bset = isl_basic_set_cow(bset);
5395 if (!bset)
5396 goto error;
5397 total = bset->dim->n_out + bset->extra;
5398 bmap = bset_to_bmap(bset);
5399 isl_space_free(bmap->dim);
5400 bmap->dim = isl_space_copy(like->dim);
5401 if (!bmap->dim)
5402 goto error;
5403 bmap->n_div = like->n_div;
5404 bmap->extra += like->n_div;
5405 if (bmap->extra) {
5406 unsigned ltotal;
5407 isl_int **div;
5408 ltotal = total - bmap->extra + like->extra;
5409 if (ltotal > total)
5410 ltotal = total;
5411 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5412 bmap->extra * (1 + 1 + total));
5413 if (isl_blk_is_error(bmap->block2))
5414 goto error;
5415 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5416 if (!div)
5417 goto error;
5418 bmap->div = div;
5419 for (i = 0; i < bmap->extra; ++i)
5420 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5421 for (i = 0; i < like->n_div; ++i) {
5422 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5423 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5425 bmap = isl_basic_map_add_known_div_constraints(bmap);
5427 isl_basic_map_free(like);
5428 bmap = isl_basic_map_simplify(bmap);
5429 bmap = isl_basic_map_finalize(bmap);
5430 return bmap;
5431 error:
5432 isl_basic_map_free(like);
5433 isl_basic_set_free(bset);
5434 return NULL;
5437 struct isl_basic_set *isl_basic_set_from_underlying_set(
5438 struct isl_basic_set *bset, struct isl_basic_set *like)
5440 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5441 bset_to_bmap(like)));
5444 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5446 int i;
5448 map = isl_map_cow(map);
5449 if (!map)
5450 return NULL;
5451 map->dim = isl_space_cow(map->dim);
5452 if (!map->dim)
5453 goto error;
5455 for (i = 1; i < map->n; ++i)
5456 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5457 goto error);
5458 for (i = 0; i < map->n; ++i) {
5459 map->p[i] = bset_to_bmap(
5460 isl_basic_map_underlying_set(map->p[i]));
5461 if (!map->p[i])
5462 goto error;
5464 if (map->n == 0)
5465 map->dim = isl_space_underlying(map->dim, 0);
5466 else {
5467 isl_space_free(map->dim);
5468 map->dim = isl_space_copy(map->p[0]->dim);
5470 if (!map->dim)
5471 goto error;
5472 return set_from_map(map);
5473 error:
5474 isl_map_free(map);
5475 return NULL;
5478 /* Replace the space of "bmap" by "space".
5480 * If the space of "bmap" is identical to "space" (including the identifiers
5481 * of the input and output dimensions), then simply return the original input.
5483 __isl_give isl_basic_map *isl_basic_map_reset_space(
5484 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5486 isl_bool equal;
5487 isl_space *bmap_space;
5489 bmap_space = isl_basic_map_peek_space(bmap);
5490 equal = isl_space_is_equal(bmap_space, space);
5491 if (equal >= 0 && equal)
5492 equal = isl_space_has_equal_ids(bmap_space, space);
5493 if (equal < 0)
5494 goto error;
5495 if (equal) {
5496 isl_space_free(space);
5497 return bmap;
5499 bmap = isl_basic_map_cow(bmap);
5500 if (!bmap || !space)
5501 goto error;
5503 isl_space_free(bmap->dim);
5504 bmap->dim = space;
5506 bmap = isl_basic_map_finalize(bmap);
5508 return bmap;
5509 error:
5510 isl_basic_map_free(bmap);
5511 isl_space_free(space);
5512 return NULL;
5515 __isl_give isl_basic_set *isl_basic_set_reset_space(
5516 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5518 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5519 dim));
5522 /* Check that the total dimensions of "map" and "space" are the same.
5524 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5525 __isl_keep isl_space *space)
5527 unsigned dim1, dim2;
5529 if (!map || !space)
5530 return isl_stat_error;
5531 dim1 = isl_map_dim(map, isl_dim_all);
5532 dim2 = isl_space_dim(space, isl_dim_all);
5533 if (dim1 == dim2)
5534 return isl_stat_ok;
5535 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5536 "total dimensions do not match", return isl_stat_error);
5539 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5540 __isl_take isl_space *space)
5542 int i;
5544 map = isl_map_cow(map);
5545 if (!map || !space)
5546 goto error;
5548 for (i = 0; i < map->n; ++i) {
5549 map->p[i] = isl_basic_map_reset_space(map->p[i],
5550 isl_space_copy(space));
5551 if (!map->p[i])
5552 goto error;
5554 isl_space_free(map->dim);
5555 map->dim = space;
5557 return map;
5558 error:
5559 isl_map_free(map);
5560 isl_space_free(space);
5561 return NULL;
5564 /* Replace the space of "map" by "space", without modifying
5565 * the dimension of "map".
5567 * If the space of "map" is identical to "space" (including the identifiers
5568 * of the input and output dimensions), then simply return the original input.
5570 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5571 __isl_take isl_space *space)
5573 isl_bool equal;
5574 isl_space *map_space;
5576 map_space = isl_map_peek_space(map);
5577 equal = isl_space_is_equal(map_space, space);
5578 if (equal >= 0 && equal)
5579 equal = isl_space_has_equal_ids(map_space, space);
5580 if (equal < 0)
5581 goto error;
5582 if (equal) {
5583 isl_space_free(space);
5584 return map;
5586 if (check_map_space_equal_total_dim(map, space) < 0)
5587 goto error;
5588 return isl_map_reset_space(map, space);
5589 error:
5590 isl_map_free(map);
5591 isl_space_free(space);
5592 return NULL;
5595 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5596 __isl_take isl_space *dim)
5598 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5601 /* Compute the parameter domain of the given basic set.
5603 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5605 isl_bool is_params;
5606 isl_space *space;
5607 unsigned n;
5609 is_params = isl_basic_set_is_params(bset);
5610 if (is_params < 0)
5611 return isl_basic_set_free(bset);
5612 if (is_params)
5613 return bset;
5615 n = isl_basic_set_dim(bset, isl_dim_set);
5616 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5617 space = isl_basic_set_get_space(bset);
5618 space = isl_space_params(space);
5619 bset = isl_basic_set_reset_space(bset, space);
5620 return bset;
5623 /* Construct a zero-dimensional basic set with the given parameter domain.
5625 __isl_give isl_basic_set *isl_basic_set_from_params(
5626 __isl_take isl_basic_set *bset)
5628 isl_space *space;
5629 space = isl_basic_set_get_space(bset);
5630 space = isl_space_set_from_params(space);
5631 bset = isl_basic_set_reset_space(bset, space);
5632 return bset;
5635 /* Compute the parameter domain of the given set.
5637 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5639 isl_space *space;
5640 unsigned n;
5642 if (isl_set_is_params(set))
5643 return set;
5645 n = isl_set_dim(set, isl_dim_set);
5646 set = isl_set_project_out(set, isl_dim_set, 0, n);
5647 space = isl_set_get_space(set);
5648 space = isl_space_params(space);
5649 set = isl_set_reset_space(set, space);
5650 return set;
5653 /* Construct a zero-dimensional set with the given parameter domain.
5655 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5657 isl_space *space;
5658 space = isl_set_get_space(set);
5659 space = isl_space_set_from_params(space);
5660 set = isl_set_reset_space(set, space);
5661 return set;
5664 /* Compute the parameter domain of the given map.
5666 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5668 isl_space *space;
5669 unsigned n;
5671 n = isl_map_dim(map, isl_dim_in);
5672 map = isl_map_project_out(map, isl_dim_in, 0, n);
5673 n = isl_map_dim(map, isl_dim_out);
5674 map = isl_map_project_out(map, isl_dim_out, 0, n);
5675 space = isl_map_get_space(map);
5676 space = isl_space_params(space);
5677 map = isl_map_reset_space(map, space);
5678 return map;
5681 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5683 isl_space *space;
5684 unsigned n_out;
5686 if (!bmap)
5687 return NULL;
5688 space = isl_space_domain(isl_basic_map_get_space(bmap));
5690 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5691 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5693 return isl_basic_map_reset_space(bmap, space);
5696 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5698 if (!bmap)
5699 return isl_bool_error;
5700 return isl_space_may_be_set(bmap->dim);
5703 /* Is this basic map actually a set?
5704 * Users should never call this function. Outside of isl,
5705 * the type should indicate whether something is a set or a map.
5707 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5709 if (!bmap)
5710 return isl_bool_error;
5711 return isl_space_is_set(bmap->dim);
5714 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5716 isl_bool is_set;
5718 is_set = isl_basic_map_is_set(bmap);
5719 if (is_set < 0)
5720 goto error;
5721 if (is_set)
5722 return bmap;
5723 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5724 error:
5725 isl_basic_map_free(bmap);
5726 return NULL;
5729 __isl_give isl_basic_map *isl_basic_map_domain_map(
5730 __isl_take isl_basic_map *bmap)
5732 int i;
5733 isl_space *space;
5734 isl_basic_map *domain;
5735 int nparam, n_in, n_out;
5737 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5738 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5739 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5741 space = isl_basic_map_get_space(bmap);
5742 space = isl_space_from_range(isl_space_domain(space));
5743 domain = isl_basic_map_universe(space);
5745 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5746 bmap = isl_basic_map_apply_range(bmap, domain);
5747 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5749 for (i = 0; i < n_in; ++i)
5750 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5751 isl_dim_out, i);
5753 bmap = isl_basic_map_gauss(bmap, NULL);
5754 return isl_basic_map_finalize(bmap);
5757 __isl_give isl_basic_map *isl_basic_map_range_map(
5758 __isl_take isl_basic_map *bmap)
5760 int i;
5761 isl_space *space;
5762 isl_basic_map *range;
5763 int nparam, n_in, n_out;
5765 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5766 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5767 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5769 space = isl_basic_map_get_space(bmap);
5770 space = isl_space_from_range(isl_space_range(space));
5771 range = isl_basic_map_universe(space);
5773 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5774 bmap = isl_basic_map_apply_range(bmap, range);
5775 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5777 for (i = 0; i < n_out; ++i)
5778 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5779 isl_dim_out, i);
5781 bmap = isl_basic_map_gauss(bmap, NULL);
5782 return isl_basic_map_finalize(bmap);
5785 int isl_map_may_be_set(__isl_keep isl_map *map)
5787 if (!map)
5788 return -1;
5789 return isl_space_may_be_set(map->dim);
5792 /* Is this map actually a set?
5793 * Users should never call this function. Outside of isl,
5794 * the type should indicate whether something is a set or a map.
5796 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5798 if (!map)
5799 return isl_bool_error;
5800 return isl_space_is_set(map->dim);
5803 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5805 int i;
5806 isl_bool is_set;
5807 struct isl_set *set;
5809 is_set = isl_map_is_set(map);
5810 if (is_set < 0)
5811 goto error;
5812 if (is_set)
5813 return set_from_map(map);
5815 map = isl_map_cow(map);
5816 if (!map)
5817 goto error;
5819 set = set_from_map(map);
5820 set->dim = isl_space_range(set->dim);
5821 if (!set->dim)
5822 goto error;
5823 for (i = 0; i < map->n; ++i) {
5824 set->p[i] = isl_basic_map_range(map->p[i]);
5825 if (!set->p[i])
5826 goto error;
5828 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5829 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5830 return set;
5831 error:
5832 isl_map_free(map);
5833 return NULL;
5836 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5838 int i;
5840 map = isl_map_cow(map);
5841 if (!map)
5842 return NULL;
5844 map->dim = isl_space_domain_map(map->dim);
5845 if (!map->dim)
5846 goto error;
5847 for (i = 0; i < map->n; ++i) {
5848 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5849 if (!map->p[i])
5850 goto error;
5852 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5853 map = isl_map_unmark_normalized(map);
5854 return map;
5855 error:
5856 isl_map_free(map);
5857 return NULL;
5860 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5862 int i;
5863 isl_space *range_dim;
5865 map = isl_map_cow(map);
5866 if (!map)
5867 return NULL;
5869 range_dim = isl_space_range(isl_map_get_space(map));
5870 range_dim = isl_space_from_range(range_dim);
5871 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5872 map->dim = isl_space_join(map->dim, range_dim);
5873 if (!map->dim)
5874 goto error;
5875 for (i = 0; i < map->n; ++i) {
5876 map->p[i] = isl_basic_map_range_map(map->p[i]);
5877 if (!map->p[i])
5878 goto error;
5880 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5881 map = isl_map_unmark_normalized(map);
5882 return map;
5883 error:
5884 isl_map_free(map);
5885 return NULL;
5888 /* Given a wrapped map of the form A[B -> C],
5889 * return the map A[B -> C] -> B.
5891 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5893 isl_id *id;
5894 isl_map *map;
5896 if (!set)
5897 return NULL;
5898 if (!isl_set_has_tuple_id(set))
5899 return isl_map_domain_map(isl_set_unwrap(set));
5901 id = isl_set_get_tuple_id(set);
5902 map = isl_map_domain_map(isl_set_unwrap(set));
5903 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5905 return map;
5908 __isl_give isl_basic_map *isl_basic_map_from_domain(
5909 __isl_take isl_basic_set *bset)
5911 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5914 __isl_give isl_basic_map *isl_basic_map_from_range(
5915 __isl_take isl_basic_set *bset)
5917 isl_space *space;
5918 space = isl_basic_set_get_space(bset);
5919 space = isl_space_from_range(space);
5920 bset = isl_basic_set_reset_space(bset, space);
5921 return bset_to_bmap(bset);
5924 /* Create a relation with the given set as range.
5925 * The domain of the created relation is a zero-dimensional
5926 * flat anonymous space.
5928 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5930 isl_space *space;
5931 space = isl_set_get_space(set);
5932 space = isl_space_from_range(space);
5933 set = isl_set_reset_space(set, space);
5934 return set_to_map(set);
5937 /* Create a relation with the given set as domain.
5938 * The range of the created relation is a zero-dimensional
5939 * flat anonymous space.
5941 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5943 return isl_map_reverse(isl_map_from_range(set));
5946 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5947 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5949 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5952 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5953 __isl_take isl_set *range)
5955 return isl_map_apply_range(isl_map_reverse(domain), range);
5958 /* Return a newly allocated isl_map with given space and flags and
5959 * room for "n" basic maps.
5960 * Make sure that all cached information is cleared.
5962 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5963 unsigned flags)
5965 struct isl_map *map;
5967 if (!space)
5968 return NULL;
5969 if (n < 0)
5970 isl_die(space->ctx, isl_error_internal,
5971 "negative number of basic maps", goto error);
5972 map = isl_calloc(space->ctx, struct isl_map,
5973 sizeof(struct isl_map) +
5974 (n - 1) * sizeof(struct isl_basic_map *));
5975 if (!map)
5976 goto error;
5978 map->ctx = space->ctx;
5979 isl_ctx_ref(map->ctx);
5980 map->ref = 1;
5981 map->size = n;
5982 map->n = 0;
5983 map->dim = space;
5984 map->flags = flags;
5985 return map;
5986 error:
5987 isl_space_free(space);
5988 return NULL;
5991 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5993 struct isl_basic_map *bmap;
5994 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5995 bmap = isl_basic_map_set_to_empty(bmap);
5996 return bmap;
5999 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6001 struct isl_basic_set *bset;
6002 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6003 bset = isl_basic_set_set_to_empty(bset);
6004 return bset;
6007 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6009 struct isl_basic_map *bmap;
6010 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6011 bmap = isl_basic_map_finalize(bmap);
6012 return bmap;
6015 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6017 struct isl_basic_set *bset;
6018 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6019 bset = isl_basic_set_finalize(bset);
6020 return bset;
6023 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6024 __isl_take isl_space *space)
6026 int i;
6027 unsigned total = isl_space_dim(space, isl_dim_all);
6028 isl_basic_map *bmap;
6030 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6031 for (i = 0; i < total; ++i) {
6032 int k = isl_basic_map_alloc_inequality(bmap);
6033 if (k < 0)
6034 goto error;
6035 isl_seq_clr(bmap->ineq[k], 1 + total);
6036 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6038 return bmap;
6039 error:
6040 isl_basic_map_free(bmap);
6041 return NULL;
6044 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6045 __isl_take isl_space *space)
6047 return isl_basic_map_nat_universe(space);
6050 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6052 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6055 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6057 return isl_map_nat_universe(dim);
6060 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6062 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6065 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6067 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6070 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6072 struct isl_map *map;
6073 if (!space)
6074 return NULL;
6075 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6076 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6077 return map;
6080 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6082 struct isl_set *set;
6083 if (!space)
6084 return NULL;
6085 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6086 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6087 return set;
6090 struct isl_map *isl_map_dup(struct isl_map *map)
6092 int i;
6093 struct isl_map *dup;
6095 if (!map)
6096 return NULL;
6097 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6098 for (i = 0; i < map->n; ++i)
6099 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6100 return dup;
6103 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6104 __isl_take isl_basic_map *bmap)
6106 if (!bmap || !map)
6107 goto error;
6108 if (isl_basic_map_plain_is_empty(bmap)) {
6109 isl_basic_map_free(bmap);
6110 return map;
6112 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6113 isl_assert(map->ctx, map->n < map->size, goto error);
6114 map->p[map->n] = bmap;
6115 map->n++;
6116 map = isl_map_unmark_normalized(map);
6117 return map;
6118 error:
6119 if (map)
6120 isl_map_free(map);
6121 if (bmap)
6122 isl_basic_map_free(bmap);
6123 return NULL;
6126 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6128 int i;
6130 if (!map)
6131 return NULL;
6133 if (--map->ref > 0)
6134 return NULL;
6136 clear_caches(map);
6137 isl_ctx_deref(map->ctx);
6138 for (i = 0; i < map->n; ++i)
6139 isl_basic_map_free(map->p[i]);
6140 isl_space_free(map->dim);
6141 free(map);
6143 return NULL;
6146 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6147 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6149 int j;
6151 bmap = isl_basic_map_cow(bmap);
6152 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6153 j = isl_basic_map_alloc_equality(bmap);
6154 if (j < 0)
6155 goto error;
6156 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6157 isl_int_set_si(bmap->eq[j][pos], -1);
6158 isl_int_set_si(bmap->eq[j][0], value);
6159 bmap = isl_basic_map_simplify(bmap);
6160 return isl_basic_map_finalize(bmap);
6161 error:
6162 isl_basic_map_free(bmap);
6163 return NULL;
6166 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6167 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6169 int j;
6171 bmap = isl_basic_map_cow(bmap);
6172 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6173 j = isl_basic_map_alloc_equality(bmap);
6174 if (j < 0)
6175 goto error;
6176 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6177 isl_int_set_si(bmap->eq[j][pos], -1);
6178 isl_int_set(bmap->eq[j][0], value);
6179 bmap = isl_basic_map_simplify(bmap);
6180 return isl_basic_map_finalize(bmap);
6181 error:
6182 isl_basic_map_free(bmap);
6183 return NULL;
6186 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6187 enum isl_dim_type type, unsigned pos, int value)
6189 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6190 return isl_basic_map_free(bmap);
6191 return isl_basic_map_fix_pos_si(bmap,
6192 isl_basic_map_offset(bmap, type) + pos, value);
6195 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6196 enum isl_dim_type type, unsigned pos, isl_int value)
6198 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6199 return isl_basic_map_free(bmap);
6200 return isl_basic_map_fix_pos(bmap,
6201 isl_basic_map_offset(bmap, type) + pos, value);
6204 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6205 * to be equal to "v".
6207 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6208 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6210 if (!bmap || !v)
6211 goto error;
6212 if (!isl_val_is_int(v))
6213 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6214 "expecting integer value", goto error);
6215 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6216 goto error;
6217 pos += isl_basic_map_offset(bmap, type);
6218 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6219 isl_val_free(v);
6220 return bmap;
6221 error:
6222 isl_basic_map_free(bmap);
6223 isl_val_free(v);
6224 return NULL;
6227 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6228 * to be equal to "v".
6230 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6231 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6233 return isl_basic_map_fix_val(bset, type, pos, v);
6236 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6237 enum isl_dim_type type, unsigned pos, int value)
6239 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6240 type, pos, value));
6243 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6244 enum isl_dim_type type, unsigned pos, isl_int value)
6246 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6247 type, pos, value));
6250 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6251 unsigned input, int value)
6253 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6256 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6257 unsigned dim, int value)
6259 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6260 isl_dim_set, dim, value));
6263 /* Remove the basic map at position "i" from "map" if this basic map
6264 * is (obviously) empty.
6266 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6268 isl_bool empty;
6270 if (!map)
6271 return NULL;
6273 empty = isl_basic_map_plain_is_empty(map->p[i]);
6274 if (empty < 0)
6275 return isl_map_free(map);
6276 if (!empty)
6277 return map;
6279 isl_basic_map_free(map->p[i]);
6280 map->n--;
6281 if (i != map->n) {
6282 map->p[i] = map->p[map->n];
6283 map = isl_map_unmark_normalized(map);
6287 return map;
6290 /* Perform "fn" on each basic map of "map", where we may not be holding
6291 * the only reference to "map".
6292 * In particular, "fn" should be a semantics preserving operation
6293 * that we want to apply to all copies of "map". We therefore need
6294 * to be careful not to modify "map" in a way that breaks "map"
6295 * in case anything goes wrong.
6297 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6298 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6300 struct isl_basic_map *bmap;
6301 int i;
6303 if (!map)
6304 return NULL;
6306 for (i = map->n - 1; i >= 0; --i) {
6307 bmap = isl_basic_map_copy(map->p[i]);
6308 bmap = fn(bmap);
6309 if (!bmap)
6310 goto error;
6311 isl_basic_map_free(map->p[i]);
6312 map->p[i] = bmap;
6313 map = remove_if_empty(map, i);
6314 if (!map)
6315 return NULL;
6318 return map;
6319 error:
6320 isl_map_free(map);
6321 return NULL;
6324 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6325 enum isl_dim_type type, unsigned pos, int value)
6327 int i;
6329 map = isl_map_cow(map);
6330 if (isl_map_check_range(map, type, pos, 1) < 0)
6331 return isl_map_free(map);
6332 for (i = map->n - 1; i >= 0; --i) {
6333 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6334 map = remove_if_empty(map, i);
6335 if (!map)
6336 return NULL;
6338 map = isl_map_unmark_normalized(map);
6339 return map;
6342 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6343 enum isl_dim_type type, unsigned pos, int value)
6345 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6348 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6349 enum isl_dim_type type, unsigned pos, isl_int value)
6351 int i;
6353 map = isl_map_cow(map);
6354 if (isl_map_check_range(map, type, pos, 1) < 0)
6355 return isl_map_free(map);
6356 for (i = 0; i < map->n; ++i) {
6357 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6358 if (!map->p[i])
6359 goto error;
6361 map = isl_map_unmark_normalized(map);
6362 return map;
6363 error:
6364 isl_map_free(map);
6365 return NULL;
6368 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6369 enum isl_dim_type type, unsigned pos, isl_int value)
6371 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6374 /* Fix the value of the variable at position "pos" of type "type" of "map"
6375 * to be equal to "v".
6377 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6378 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6380 int i;
6382 map = isl_map_cow(map);
6383 if (!map || !v)
6384 goto error;
6386 if (!isl_val_is_int(v))
6387 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6388 "expecting integer value", goto error);
6389 if (isl_map_check_range(map, type, pos, 1) < 0)
6390 goto error;
6391 for (i = map->n - 1; i >= 0; --i) {
6392 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6393 isl_val_copy(v));
6394 map = remove_if_empty(map, i);
6395 if (!map)
6396 goto error;
6398 map = isl_map_unmark_normalized(map);
6399 isl_val_free(v);
6400 return map;
6401 error:
6402 isl_map_free(map);
6403 isl_val_free(v);
6404 return NULL;
6407 /* Fix the value of the variable at position "pos" of type "type" of "set"
6408 * to be equal to "v".
6410 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6411 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6413 return isl_map_fix_val(set, type, pos, v);
6416 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6417 unsigned input, int value)
6419 return isl_map_fix_si(map, isl_dim_in, input, value);
6422 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6424 return set_from_map(isl_map_fix_si(set_to_map(set),
6425 isl_dim_set, dim, value));
6428 static __isl_give isl_basic_map *basic_map_bound_si(
6429 __isl_take isl_basic_map *bmap,
6430 enum isl_dim_type type, unsigned pos, int value, int upper)
6432 int j;
6434 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6435 return isl_basic_map_free(bmap);
6436 pos += isl_basic_map_offset(bmap, type);
6437 bmap = isl_basic_map_cow(bmap);
6438 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6439 j = isl_basic_map_alloc_inequality(bmap);
6440 if (j < 0)
6441 goto error;
6442 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6443 if (upper) {
6444 isl_int_set_si(bmap->ineq[j][pos], -1);
6445 isl_int_set_si(bmap->ineq[j][0], value);
6446 } else {
6447 isl_int_set_si(bmap->ineq[j][pos], 1);
6448 isl_int_set_si(bmap->ineq[j][0], -value);
6450 bmap = isl_basic_map_simplify(bmap);
6451 return isl_basic_map_finalize(bmap);
6452 error:
6453 isl_basic_map_free(bmap);
6454 return NULL;
6457 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6458 __isl_take isl_basic_map *bmap,
6459 enum isl_dim_type type, unsigned pos, int value)
6461 return basic_map_bound_si(bmap, type, pos, value, 0);
6464 /* Constrain the values of the given dimension to be no greater than "value".
6466 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6467 __isl_take isl_basic_map *bmap,
6468 enum isl_dim_type type, unsigned pos, int value)
6470 return basic_map_bound_si(bmap, type, pos, value, 1);
6473 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6474 enum isl_dim_type type, unsigned pos, int value, int upper)
6476 int i;
6478 map = isl_map_cow(map);
6479 if (isl_map_check_range(map, type, pos, 1) < 0)
6480 return isl_map_free(map);
6481 for (i = 0; i < map->n; ++i) {
6482 map->p[i] = basic_map_bound_si(map->p[i],
6483 type, pos, value, upper);
6484 if (!map->p[i])
6485 goto error;
6487 map = isl_map_unmark_normalized(map);
6488 return map;
6489 error:
6490 isl_map_free(map);
6491 return NULL;
6494 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6495 enum isl_dim_type type, unsigned pos, int value)
6497 return map_bound_si(map, type, pos, value, 0);
6500 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6501 enum isl_dim_type type, unsigned pos, int value)
6503 return map_bound_si(map, type, pos, value, 1);
6506 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6507 enum isl_dim_type type, unsigned pos, int value)
6509 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6510 type, pos, value));
6513 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6514 enum isl_dim_type type, unsigned pos, int value)
6516 return isl_map_upper_bound_si(set, type, pos, value);
6519 /* Bound the given variable of "bmap" from below (or above is "upper"
6520 * is set) to "value".
6522 static __isl_give isl_basic_map *basic_map_bound(
6523 __isl_take isl_basic_map *bmap,
6524 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6526 int j;
6528 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6529 return isl_basic_map_free(bmap);
6530 pos += isl_basic_map_offset(bmap, type);
6531 bmap = isl_basic_map_cow(bmap);
6532 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6533 j = isl_basic_map_alloc_inequality(bmap);
6534 if (j < 0)
6535 goto error;
6536 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6537 if (upper) {
6538 isl_int_set_si(bmap->ineq[j][pos], -1);
6539 isl_int_set(bmap->ineq[j][0], value);
6540 } else {
6541 isl_int_set_si(bmap->ineq[j][pos], 1);
6542 isl_int_neg(bmap->ineq[j][0], value);
6544 bmap = isl_basic_map_simplify(bmap);
6545 return isl_basic_map_finalize(bmap);
6546 error:
6547 isl_basic_map_free(bmap);
6548 return NULL;
6551 /* Bound the given variable of "map" from below (or above is "upper"
6552 * is set) to "value".
6554 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6555 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6557 int i;
6559 map = isl_map_cow(map);
6560 if (isl_map_check_range(map, type, pos, 1) < 0)
6561 return isl_map_free(map);
6562 for (i = map->n - 1; i >= 0; --i) {
6563 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6564 map = remove_if_empty(map, i);
6565 if (!map)
6566 return NULL;
6568 map = isl_map_unmark_normalized(map);
6569 return map;
6572 __isl_give isl_map *isl_map_lower_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, 0);
6578 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6579 enum isl_dim_type type, unsigned pos, isl_int value)
6581 return map_bound(map, type, pos, value, 1);
6584 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6585 enum isl_dim_type type, unsigned pos, isl_int value)
6587 return isl_map_lower_bound(set, type, pos, value);
6590 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6591 enum isl_dim_type type, unsigned pos, isl_int value)
6593 return isl_map_upper_bound(set, type, pos, value);
6596 /* Force the values of the variable at position "pos" of type "type" of "set"
6597 * to be no smaller than "value".
6599 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6600 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6602 if (!value)
6603 goto error;
6604 if (!isl_val_is_int(value))
6605 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6606 "expecting integer value", goto error);
6607 set = isl_set_lower_bound(set, type, pos, value->n);
6608 isl_val_free(value);
6609 return set;
6610 error:
6611 isl_val_free(value);
6612 isl_set_free(set);
6613 return NULL;
6616 /* Force the values of the variable at position "pos" of type "type" of "set"
6617 * to be no greater than "value".
6619 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6620 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6622 if (!value)
6623 goto error;
6624 if (!isl_val_is_int(value))
6625 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6626 "expecting integer value", goto error);
6627 set = isl_set_upper_bound(set, type, pos, value->n);
6628 isl_val_free(value);
6629 return set;
6630 error:
6631 isl_val_free(value);
6632 isl_set_free(set);
6633 return NULL;
6636 /* Bound the given variable of "bset" from below (or above is "upper"
6637 * is set) to "value".
6639 static __isl_give isl_basic_set *isl_basic_set_bound(
6640 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6641 isl_int value, int upper)
6643 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6644 type, pos, value, upper));
6647 /* Bound the given variable of "bset" from below (or above is "upper"
6648 * is set) to "value".
6650 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6651 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6652 __isl_take isl_val *value, int upper)
6654 if (!value)
6655 goto error;
6656 if (!isl_val_is_int(value))
6657 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6658 "expecting integer value", goto error);
6659 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6660 isl_val_free(value);
6661 return bset;
6662 error:
6663 isl_val_free(value);
6664 isl_basic_set_free(bset);
6665 return NULL;
6668 /* Bound the given variable of "bset" from below to "value".
6670 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6671 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6672 __isl_take isl_val *value)
6674 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6677 /* Bound the given variable of "bset" from above to "value".
6679 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6680 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6681 __isl_take isl_val *value)
6683 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6686 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6688 int i;
6690 map = isl_map_cow(map);
6691 if (!map)
6692 return NULL;
6694 map->dim = isl_space_reverse(map->dim);
6695 if (!map->dim)
6696 goto error;
6697 for (i = 0; i < map->n; ++i) {
6698 map->p[i] = isl_basic_map_reverse(map->p[i]);
6699 if (!map->p[i])
6700 goto error;
6702 map = isl_map_unmark_normalized(map);
6703 return map;
6704 error:
6705 isl_map_free(map);
6706 return NULL;
6709 #undef TYPE
6710 #define TYPE isl_pw_multi_aff
6711 #undef SUFFIX
6712 #define SUFFIX _pw_multi_aff
6713 #undef EMPTY
6714 #define EMPTY isl_pw_multi_aff_empty
6715 #undef ADD
6716 #define ADD isl_pw_multi_aff_union_add
6717 #include "isl_map_lexopt_templ.c"
6719 /* Given a map "map", compute the lexicographically minimal
6720 * (or maximal) image element for each domain element in dom,
6721 * in the form of an isl_pw_multi_aff.
6722 * If "empty" is not NULL, then set *empty to those elements in dom that
6723 * do not have an image element.
6724 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6725 * should be computed over the domain of "map". "empty" is also NULL
6726 * in this case.
6728 * We first compute the lexicographically minimal or maximal element
6729 * in the first basic map. This results in a partial solution "res"
6730 * and a subset "todo" of dom that still need to be handled.
6731 * We then consider each of the remaining maps in "map" and successively
6732 * update both "res" and "todo".
6733 * If "empty" is NULL, then the todo sets are not needed and therefore
6734 * also not computed.
6736 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6737 __isl_take isl_map *map, __isl_take isl_set *dom,
6738 __isl_give isl_set **empty, unsigned flags)
6740 int i;
6741 int full;
6742 isl_pw_multi_aff *res;
6743 isl_set *todo;
6745 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6746 if (!map || (!full && !dom))
6747 goto error;
6749 if (isl_map_plain_is_empty(map)) {
6750 if (empty)
6751 *empty = dom;
6752 else
6753 isl_set_free(dom);
6754 return isl_pw_multi_aff_from_map(map);
6757 res = basic_map_partial_lexopt_pw_multi_aff(
6758 isl_basic_map_copy(map->p[0]),
6759 isl_set_copy(dom), empty, flags);
6761 if (empty)
6762 todo = *empty;
6763 for (i = 1; i < map->n; ++i) {
6764 isl_pw_multi_aff *res_i;
6766 res_i = basic_map_partial_lexopt_pw_multi_aff(
6767 isl_basic_map_copy(map->p[i]),
6768 isl_set_copy(dom), empty, flags);
6770 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6771 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6772 else
6773 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6775 if (empty)
6776 todo = isl_set_intersect(todo, *empty);
6779 isl_set_free(dom);
6780 isl_map_free(map);
6782 if (empty)
6783 *empty = todo;
6785 return res;
6786 error:
6787 if (empty)
6788 *empty = NULL;
6789 isl_set_free(dom);
6790 isl_map_free(map);
6791 return NULL;
6794 #undef TYPE
6795 #define TYPE isl_map
6796 #undef SUFFIX
6797 #define SUFFIX
6798 #undef EMPTY
6799 #define EMPTY isl_map_empty
6800 #undef ADD
6801 #define ADD isl_map_union_disjoint
6802 #include "isl_map_lexopt_templ.c"
6804 /* Given a map "map", compute the lexicographically minimal
6805 * (or maximal) image element for each domain element in "dom",
6806 * in the form of an isl_map.
6807 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6808 * do not have an image element.
6809 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6810 * should be computed over the domain of "map". "empty" is also NULL
6811 * in this case.
6813 * If the input consists of more than one disjunct, then first
6814 * compute the desired result in the form of an isl_pw_multi_aff and
6815 * then convert that into an isl_map.
6817 * This function used to have an explicit implementation in terms
6818 * of isl_maps, but it would continually intersect the domains of
6819 * partial results with the complement of the domain of the next
6820 * partial solution, potentially leading to an explosion in the number
6821 * of disjuncts if there are several disjuncts in the input.
6822 * An even earlier implementation of this function would look for
6823 * better results in the domain of the partial result and for extra
6824 * results in the complement of this domain, which would lead to
6825 * even more splintering.
6827 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6828 __isl_take isl_map *map, __isl_take isl_set *dom,
6829 __isl_give isl_set **empty, unsigned flags)
6831 int full;
6832 struct isl_map *res;
6833 isl_pw_multi_aff *pma;
6835 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6836 if (!map || (!full && !dom))
6837 goto error;
6839 if (isl_map_plain_is_empty(map)) {
6840 if (empty)
6841 *empty = dom;
6842 else
6843 isl_set_free(dom);
6844 return map;
6847 if (map->n == 1) {
6848 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6849 dom, empty, flags);
6850 isl_map_free(map);
6851 return res;
6854 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6855 flags);
6856 return isl_map_from_pw_multi_aff(pma);
6857 error:
6858 if (empty)
6859 *empty = NULL;
6860 isl_set_free(dom);
6861 isl_map_free(map);
6862 return NULL;
6865 __isl_give isl_map *isl_map_partial_lexmax(
6866 __isl_take isl_map *map, __isl_take isl_set *dom,
6867 __isl_give isl_set **empty)
6869 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6872 __isl_give isl_map *isl_map_partial_lexmin(
6873 __isl_take isl_map *map, __isl_take isl_set *dom,
6874 __isl_give isl_set **empty)
6876 return isl_map_partial_lexopt(map, dom, empty, 0);
6879 __isl_give isl_set *isl_set_partial_lexmin(
6880 __isl_take isl_set *set, __isl_take isl_set *dom,
6881 __isl_give isl_set **empty)
6883 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6884 dom, empty));
6887 __isl_give isl_set *isl_set_partial_lexmax(
6888 __isl_take isl_set *set, __isl_take isl_set *dom,
6889 __isl_give isl_set **empty)
6891 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6892 dom, empty));
6895 /* Compute the lexicographic minimum (or maximum if "flags" includes
6896 * ISL_OPT_MAX) of "bset" over its parametric domain.
6898 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6899 unsigned flags)
6901 return isl_basic_map_lexopt(bset, flags);
6904 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6906 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6909 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6911 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6914 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6916 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6919 /* Compute the lexicographic minimum of "bset" over its parametric domain
6920 * for the purpose of quantifier elimination.
6921 * That is, find an explicit representation for all the existentially
6922 * quantified variables in "bset" by computing their lexicographic
6923 * minimum.
6925 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6926 __isl_take isl_basic_set *bset)
6928 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6931 /* Given a basic map with one output dimension, compute the minimum or
6932 * maximum of that dimension as an isl_pw_aff.
6934 * Compute the optimum as a lexicographic optimum over the single
6935 * output dimension and extract the single isl_pw_aff from the result.
6937 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6938 int max)
6940 isl_pw_multi_aff *pma;
6941 isl_pw_aff *pwaff;
6943 bmap = isl_basic_map_copy(bmap);
6944 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6945 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6946 isl_pw_multi_aff_free(pma);
6948 return pwaff;
6951 /* Compute the minimum or maximum of the given output dimension
6952 * as a function of the parameters and the input dimensions,
6953 * but independently of the other output dimensions.
6955 * We first project out the other output dimension and then compute
6956 * the "lexicographic" maximum in each basic map, combining the results
6957 * using isl_pw_aff_union_max.
6959 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6960 int max)
6962 int i;
6963 isl_pw_aff *pwaff;
6964 unsigned n_out;
6966 n_out = isl_map_dim(map, isl_dim_out);
6967 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6968 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6969 if (!map)
6970 return NULL;
6972 if (map->n == 0) {
6973 isl_space *space = isl_map_get_space(map);
6974 isl_map_free(map);
6975 return isl_pw_aff_empty(space);
6978 pwaff = basic_map_dim_opt(map->p[0], max);
6979 for (i = 1; i < map->n; ++i) {
6980 isl_pw_aff *pwaff_i;
6982 pwaff_i = basic_map_dim_opt(map->p[i], max);
6983 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6986 isl_map_free(map);
6988 return pwaff;
6991 /* Compute the minimum of the given output dimension as a function of the
6992 * parameters and input dimensions, but independently of
6993 * the other output dimensions.
6995 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6997 return map_dim_opt(map, pos, 0);
7000 /* Compute the maximum of the given output dimension as a function of the
7001 * parameters and input dimensions, but independently of
7002 * the other output dimensions.
7004 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7006 return map_dim_opt(map, pos, 1);
7009 /* Compute the minimum or maximum of the given set dimension
7010 * as a function of the parameters,
7011 * but independently of the other set dimensions.
7013 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7014 int max)
7016 return map_dim_opt(set, pos, max);
7019 /* Compute the maximum of the given set dimension as a function of the
7020 * parameters, but independently of the other set dimensions.
7022 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7024 return set_dim_opt(set, pos, 1);
7027 /* Compute the minimum of the given set dimension as a function of the
7028 * parameters, but independently of the other set dimensions.
7030 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7032 return set_dim_opt(set, pos, 0);
7035 /* Apply a preimage specified by "mat" on the parameters of "bset".
7036 * bset is assumed to have only parameters and divs.
7038 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7039 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7041 unsigned nparam;
7043 if (!bset || !mat)
7044 goto error;
7046 bset->dim = isl_space_cow(bset->dim);
7047 if (!bset->dim)
7048 goto error;
7050 nparam = isl_basic_set_dim(bset, isl_dim_param);
7052 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7054 bset->dim->nparam = 0;
7055 bset->dim->n_out = nparam;
7056 bset = isl_basic_set_preimage(bset, mat);
7057 if (bset) {
7058 bset->dim->nparam = bset->dim->n_out;
7059 bset->dim->n_out = 0;
7061 return bset;
7062 error:
7063 isl_mat_free(mat);
7064 isl_basic_set_free(bset);
7065 return NULL;
7068 /* Apply a preimage specified by "mat" on the parameters of "set".
7069 * set is assumed to have only parameters and divs.
7071 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7072 __isl_take isl_mat *mat)
7074 isl_space *space;
7075 unsigned nparam;
7077 if (!set || !mat)
7078 goto error;
7080 nparam = isl_set_dim(set, isl_dim_param);
7082 if (mat->n_row != 1 + nparam)
7083 isl_die(isl_set_get_ctx(set), isl_error_internal,
7084 "unexpected number of rows", goto error);
7086 space = isl_set_get_space(set);
7087 space = isl_space_move_dims(space, isl_dim_set, 0,
7088 isl_dim_param, 0, nparam);
7089 set = isl_set_reset_space(set, space);
7090 set = isl_set_preimage(set, mat);
7091 nparam = isl_set_dim(set, isl_dim_out);
7092 space = isl_set_get_space(set);
7093 space = isl_space_move_dims(space, isl_dim_param, 0,
7094 isl_dim_out, 0, nparam);
7095 set = isl_set_reset_space(set, space);
7096 return set;
7097 error:
7098 isl_mat_free(mat);
7099 isl_set_free(set);
7100 return NULL;
7103 /* Intersect the basic set "bset" with the affine space specified by the
7104 * equalities in "eq".
7106 static __isl_give isl_basic_set *basic_set_append_equalities(
7107 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7109 int i, k;
7110 unsigned len;
7112 if (!bset || !eq)
7113 goto error;
7115 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7116 eq->n_row, 0);
7117 if (!bset)
7118 goto error;
7120 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7121 for (i = 0; i < eq->n_row; ++i) {
7122 k = isl_basic_set_alloc_equality(bset);
7123 if (k < 0)
7124 goto error;
7125 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7126 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7128 isl_mat_free(eq);
7130 bset = isl_basic_set_gauss(bset, NULL);
7131 bset = isl_basic_set_finalize(bset);
7133 return bset;
7134 error:
7135 isl_mat_free(eq);
7136 isl_basic_set_free(bset);
7137 return NULL;
7140 /* Intersect the set "set" with the affine space specified by the
7141 * equalities in "eq".
7143 static struct isl_set *set_append_equalities(struct isl_set *set,
7144 struct isl_mat *eq)
7146 int i;
7148 if (!set || !eq)
7149 goto error;
7151 for (i = 0; i < set->n; ++i) {
7152 set->p[i] = basic_set_append_equalities(set->p[i],
7153 isl_mat_copy(eq));
7154 if (!set->p[i])
7155 goto error;
7157 isl_mat_free(eq);
7158 return set;
7159 error:
7160 isl_mat_free(eq);
7161 isl_set_free(set);
7162 return NULL;
7165 /* Given a basic set "bset" that only involves parameters and existentially
7166 * quantified variables, return the index of the first equality
7167 * that only involves parameters. If there is no such equality then
7168 * return bset->n_eq.
7170 * This function assumes that isl_basic_set_gauss has been called on "bset".
7172 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7174 int i, j;
7175 unsigned nparam, n_div;
7177 if (!bset)
7178 return -1;
7180 nparam = isl_basic_set_dim(bset, isl_dim_param);
7181 n_div = isl_basic_set_dim(bset, isl_dim_div);
7183 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7184 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7185 ++i;
7188 return i;
7191 /* Compute an explicit representation for the existentially quantified
7192 * variables in "bset" by computing the "minimal value" of the set
7193 * variables. Since there are no set variables, the computation of
7194 * the minimal value essentially computes an explicit representation
7195 * of the non-empty part(s) of "bset".
7197 * The input only involves parameters and existentially quantified variables.
7198 * All equalities among parameters have been removed.
7200 * Since the existentially quantified variables in the result are in general
7201 * going to be different from those in the input, we first replace
7202 * them by the minimal number of variables based on their equalities.
7203 * This should simplify the parametric integer programming.
7205 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7207 isl_morph *morph1, *morph2;
7208 isl_set *set;
7209 unsigned n;
7211 if (!bset)
7212 return NULL;
7213 if (bset->n_eq == 0)
7214 return isl_basic_set_lexmin_compute_divs(bset);
7216 morph1 = isl_basic_set_parameter_compression(bset);
7217 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7218 bset = isl_basic_set_lift(bset);
7219 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7220 bset = isl_morph_basic_set(morph2, bset);
7221 n = isl_basic_set_dim(bset, isl_dim_set);
7222 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7224 set = isl_basic_set_lexmin_compute_divs(bset);
7226 set = isl_morph_set(isl_morph_inverse(morph1), set);
7228 return set;
7231 /* Project the given basic set onto its parameter domain, possibly introducing
7232 * new, explicit, existential variables in the constraints.
7233 * The input has parameters and (possibly implicit) existential variables.
7234 * The output has the same parameters, but only
7235 * explicit existentially quantified variables.
7237 * The actual projection is performed by pip, but pip doesn't seem
7238 * to like equalities very much, so we first remove the equalities
7239 * among the parameters by performing a variable compression on
7240 * the parameters. Afterward, an inverse transformation is performed
7241 * and the equalities among the parameters are inserted back in.
7243 * The variable compression on the parameters may uncover additional
7244 * equalities that were only implicit before. We therefore check
7245 * if there are any new parameter equalities in the result and
7246 * if so recurse. The removal of parameter equalities is required
7247 * for the parameter compression performed by base_compute_divs.
7249 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7251 int i;
7252 struct isl_mat *eq;
7253 struct isl_mat *T, *T2;
7254 struct isl_set *set;
7255 unsigned nparam;
7257 bset = isl_basic_set_cow(bset);
7258 if (!bset)
7259 return NULL;
7261 if (bset->n_eq == 0)
7262 return base_compute_divs(bset);
7264 bset = isl_basic_set_gauss(bset, NULL);
7265 if (!bset)
7266 return NULL;
7267 if (isl_basic_set_plain_is_empty(bset))
7268 return isl_set_from_basic_set(bset);
7270 i = first_parameter_equality(bset);
7271 if (i == bset->n_eq)
7272 return base_compute_divs(bset);
7274 nparam = isl_basic_set_dim(bset, isl_dim_param);
7275 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7276 0, 1 + nparam);
7277 eq = isl_mat_cow(eq);
7278 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7279 if (T && T->n_col == 0) {
7280 isl_mat_free(T);
7281 isl_mat_free(T2);
7282 isl_mat_free(eq);
7283 bset = isl_basic_set_set_to_empty(bset);
7284 return isl_set_from_basic_set(bset);
7286 bset = basic_set_parameter_preimage(bset, T);
7288 i = first_parameter_equality(bset);
7289 if (!bset)
7290 set = NULL;
7291 else if (i == bset->n_eq)
7292 set = base_compute_divs(bset);
7293 else
7294 set = parameter_compute_divs(bset);
7295 set = set_parameter_preimage(set, T2);
7296 set = set_append_equalities(set, eq);
7297 return set;
7300 /* Insert the divs from "ls" before those of "bmap".
7302 * The number of columns is not changed, which means that the last
7303 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7304 * The caller is responsible for removing the same number of dimensions
7305 * from the space of "bmap".
7307 static __isl_give isl_basic_map *insert_divs_from_local_space(
7308 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7310 int i;
7311 int n_div;
7312 int old_n_div;
7314 n_div = isl_local_space_dim(ls, isl_dim_div);
7315 if (n_div == 0)
7316 return bmap;
7318 old_n_div = bmap->n_div;
7319 bmap = insert_div_rows(bmap, n_div);
7320 if (!bmap)
7321 return NULL;
7323 for (i = 0; i < n_div; ++i) {
7324 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7325 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7328 return bmap;
7331 /* Replace the space of "bmap" by the space and divs of "ls".
7333 * If "ls" has any divs, then we simplify the result since we may
7334 * have discovered some additional equalities that could simplify
7335 * the div expressions.
7337 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7338 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7340 int n_div;
7342 bmap = isl_basic_map_cow(bmap);
7343 if (!bmap || !ls)
7344 goto error;
7346 n_div = isl_local_space_dim(ls, isl_dim_div);
7347 bmap = insert_divs_from_local_space(bmap, ls);
7348 if (!bmap)
7349 goto error;
7351 isl_space_free(bmap->dim);
7352 bmap->dim = isl_local_space_get_space(ls);
7353 if (!bmap->dim)
7354 goto error;
7356 isl_local_space_free(ls);
7357 if (n_div > 0)
7358 bmap = isl_basic_map_simplify(bmap);
7359 bmap = isl_basic_map_finalize(bmap);
7360 return bmap;
7361 error:
7362 isl_basic_map_free(bmap);
7363 isl_local_space_free(ls);
7364 return NULL;
7367 /* Replace the space of "map" by the space and divs of "ls".
7369 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7370 __isl_take isl_local_space *ls)
7372 int i;
7374 map = isl_map_cow(map);
7375 if (!map || !ls)
7376 goto error;
7378 for (i = 0; i < map->n; ++i) {
7379 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7380 isl_local_space_copy(ls));
7381 if (!map->p[i])
7382 goto error;
7384 isl_space_free(map->dim);
7385 map->dim = isl_local_space_get_space(ls);
7386 if (!map->dim)
7387 goto error;
7389 isl_local_space_free(ls);
7390 return map;
7391 error:
7392 isl_local_space_free(ls);
7393 isl_map_free(map);
7394 return NULL;
7397 /* Compute an explicit representation for the existentially
7398 * quantified variables for which do not know any explicit representation yet.
7400 * We first sort the existentially quantified variables so that the
7401 * existentially quantified variables for which we already have an explicit
7402 * representation are placed before those for which we do not.
7403 * The input dimensions, the output dimensions and the existentially
7404 * quantified variables for which we already have an explicit
7405 * representation are then turned into parameters.
7406 * compute_divs returns a map with the same parameters and
7407 * no input or output dimensions and the dimension specification
7408 * is reset to that of the input, including the existentially quantified
7409 * variables for which we already had an explicit representation.
7411 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7413 struct isl_basic_set *bset;
7414 struct isl_set *set;
7415 struct isl_map *map;
7416 isl_space *space;
7417 isl_local_space *ls;
7418 unsigned nparam;
7419 unsigned n_in;
7420 unsigned n_out;
7421 int n_known;
7422 int i;
7424 bmap = isl_basic_map_sort_divs(bmap);
7425 bmap = isl_basic_map_cow(bmap);
7426 if (!bmap)
7427 return NULL;
7429 n_known = isl_basic_map_first_unknown_div(bmap);
7430 if (n_known < 0)
7431 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7433 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7434 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7435 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7436 space = isl_space_set_alloc(bmap->ctx,
7437 nparam + n_in + n_out + n_known, 0);
7438 if (!space)
7439 goto error;
7441 ls = isl_basic_map_get_local_space(bmap);
7442 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7443 n_known, bmap->n_div - n_known);
7444 if (n_known > 0) {
7445 for (i = n_known; i < bmap->n_div; ++i)
7446 swap_div(bmap, i - n_known, i);
7447 bmap->n_div -= n_known;
7448 bmap->extra -= n_known;
7450 bmap = isl_basic_map_reset_space(bmap, space);
7451 bset = bset_from_bmap(bmap);
7453 set = parameter_compute_divs(bset);
7454 map = set_to_map(set);
7455 map = replace_space_by_local_space(map, ls);
7457 return map;
7458 error:
7459 isl_basic_map_free(bmap);
7460 return NULL;
7463 /* Remove the explicit representation of local variable "div",
7464 * if there is any.
7466 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7467 __isl_take isl_basic_map *bmap, int div)
7469 isl_bool unknown;
7471 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7472 if (unknown < 0)
7473 return isl_basic_map_free(bmap);
7474 if (unknown)
7475 return bmap;
7477 bmap = isl_basic_map_cow(bmap);
7478 if (!bmap)
7479 return NULL;
7480 isl_int_set_si(bmap->div[div][0], 0);
7481 return bmap;
7484 /* Is local variable "div" of "bmap" marked as not having an explicit
7485 * representation?
7486 * Note that even if "div" is not marked in this way and therefore
7487 * has an explicit representation, this representation may still
7488 * depend (indirectly) on other local variables that do not
7489 * have an explicit representation.
7491 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7492 int div)
7494 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7495 return isl_bool_error;
7496 return isl_int_is_zero(bmap->div[div][0]);
7499 /* Return the position of the first local variable that does not
7500 * have an explicit representation.
7501 * Return the total number of local variables if they all have
7502 * an explicit representation.
7503 * Return -1 on error.
7505 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7507 int i;
7509 if (!bmap)
7510 return -1;
7512 for (i = 0; i < bmap->n_div; ++i) {
7513 if (!isl_basic_map_div_is_known(bmap, i))
7514 return i;
7516 return bmap->n_div;
7519 /* Return the position of the first local variable that does not
7520 * have an explicit representation.
7521 * Return the total number of local variables if they all have
7522 * an explicit representation.
7523 * Return -1 on error.
7525 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7527 return isl_basic_map_first_unknown_div(bset);
7530 /* Does "bmap" have an explicit representation for all local variables?
7532 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7534 int first, n;
7536 n = isl_basic_map_dim(bmap, isl_dim_div);
7537 first = isl_basic_map_first_unknown_div(bmap);
7538 if (first < 0)
7539 return isl_bool_error;
7540 return first == n;
7543 /* Do all basic maps in "map" have an explicit representation
7544 * for all local variables?
7546 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7548 int i;
7550 if (!map)
7551 return isl_bool_error;
7553 for (i = 0; i < map->n; ++i) {
7554 int known = isl_basic_map_divs_known(map->p[i]);
7555 if (known <= 0)
7556 return known;
7559 return isl_bool_true;
7562 /* If bmap contains any unknown divs, then compute explicit
7563 * expressions for them. However, this computation may be
7564 * quite expensive, so first try to remove divs that aren't
7565 * strictly needed.
7567 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7569 int known;
7570 struct isl_map *map;
7572 known = isl_basic_map_divs_known(bmap);
7573 if (known < 0)
7574 goto error;
7575 if (known)
7576 return isl_map_from_basic_map(bmap);
7578 bmap = isl_basic_map_drop_redundant_divs(bmap);
7580 known = isl_basic_map_divs_known(bmap);
7581 if (known < 0)
7582 goto error;
7583 if (known)
7584 return isl_map_from_basic_map(bmap);
7586 map = compute_divs(bmap);
7587 return map;
7588 error:
7589 isl_basic_map_free(bmap);
7590 return NULL;
7593 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7595 int i;
7596 int known;
7597 struct isl_map *res;
7599 if (!map)
7600 return NULL;
7601 if (map->n == 0)
7602 return map;
7604 known = isl_map_divs_known(map);
7605 if (known < 0) {
7606 isl_map_free(map);
7607 return NULL;
7609 if (known)
7610 return map;
7612 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7613 for (i = 1 ; i < map->n; ++i) {
7614 struct isl_map *r2;
7615 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7616 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7617 res = isl_map_union_disjoint(res, r2);
7618 else
7619 res = isl_map_union(res, r2);
7621 isl_map_free(map);
7623 return res;
7626 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7628 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7631 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7633 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7636 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7638 int i;
7639 struct isl_set *set;
7641 if (!map)
7642 goto error;
7644 map = isl_map_cow(map);
7645 if (!map)
7646 return NULL;
7648 set = set_from_map(map);
7649 set->dim = isl_space_domain(set->dim);
7650 if (!set->dim)
7651 goto error;
7652 for (i = 0; i < map->n; ++i) {
7653 set->p[i] = isl_basic_map_domain(map->p[i]);
7654 if (!set->p[i])
7655 goto error;
7657 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7658 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7659 return set;
7660 error:
7661 isl_map_free(map);
7662 return NULL;
7665 /* Return the union of "map1" and "map2", where we assume for now that
7666 * "map1" and "map2" are disjoint. Note that the basic maps inside
7667 * "map1" or "map2" may not be disjoint from each other.
7668 * Also note that this function is also called from isl_map_union,
7669 * which takes care of handling the situation where "map1" and "map2"
7670 * may not be disjoint.
7672 * If one of the inputs is empty, we can simply return the other input.
7673 * Similarly, if one of the inputs is universal, then it is equal to the union.
7675 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7676 __isl_take isl_map *map2)
7678 int i;
7679 unsigned flags = 0;
7680 struct isl_map *map = NULL;
7681 int is_universe;
7683 if (!map1 || !map2)
7684 goto error;
7686 if (!isl_space_is_equal(map1->dim, map2->dim))
7687 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7688 "spaces don't match", goto error);
7690 if (map1->n == 0) {
7691 isl_map_free(map1);
7692 return map2;
7694 if (map2->n == 0) {
7695 isl_map_free(map2);
7696 return map1;
7699 is_universe = isl_map_plain_is_universe(map1);
7700 if (is_universe < 0)
7701 goto error;
7702 if (is_universe) {
7703 isl_map_free(map2);
7704 return map1;
7707 is_universe = isl_map_plain_is_universe(map2);
7708 if (is_universe < 0)
7709 goto error;
7710 if (is_universe) {
7711 isl_map_free(map1);
7712 return map2;
7715 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7716 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7717 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7719 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7720 map1->n + map2->n, flags);
7721 if (!map)
7722 goto error;
7723 for (i = 0; i < map1->n; ++i) {
7724 map = isl_map_add_basic_map(map,
7725 isl_basic_map_copy(map1->p[i]));
7726 if (!map)
7727 goto error;
7729 for (i = 0; i < map2->n; ++i) {
7730 map = isl_map_add_basic_map(map,
7731 isl_basic_map_copy(map2->p[i]));
7732 if (!map)
7733 goto error;
7735 isl_map_free(map1);
7736 isl_map_free(map2);
7737 return map;
7738 error:
7739 isl_map_free(map);
7740 isl_map_free(map1);
7741 isl_map_free(map2);
7742 return NULL;
7745 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7746 * guaranteed to be disjoint by the caller.
7748 * Note that this functions is called from within isl_map_make_disjoint,
7749 * so we have to be careful not to touch the constraints of the inputs
7750 * in any way.
7752 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7753 __isl_take isl_map *map2)
7755 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7758 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7759 * not be disjoint. The parameters are assumed to have been aligned.
7761 * We currently simply call map_union_disjoint, the internal operation
7762 * of which does not really depend on the inputs being disjoint.
7763 * If the result contains more than one basic map, then we clear
7764 * the disjoint flag since the result may contain basic maps from
7765 * both inputs and these are not guaranteed to be disjoint.
7767 * As a special case, if "map1" and "map2" are obviously equal,
7768 * then we simply return "map1".
7770 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7771 __isl_take isl_map *map2)
7773 int equal;
7775 if (!map1 || !map2)
7776 goto error;
7778 equal = isl_map_plain_is_equal(map1, map2);
7779 if (equal < 0)
7780 goto error;
7781 if (equal) {
7782 isl_map_free(map2);
7783 return map1;
7786 map1 = map_union_disjoint(map1, map2);
7787 if (!map1)
7788 return NULL;
7789 if (map1->n > 1)
7790 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7791 return map1;
7792 error:
7793 isl_map_free(map1);
7794 isl_map_free(map2);
7795 return NULL;
7798 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7799 * not be disjoint.
7801 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7802 __isl_take isl_map *map2)
7804 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7807 __isl_give isl_set *isl_set_union_disjoint(
7808 __isl_take isl_set *set1, __isl_take isl_set *set2)
7810 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7811 set_to_map(set2)));
7814 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7816 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7819 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7820 * the results.
7822 * "map" and "set" are assumed to be compatible and non-NULL.
7824 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7825 __isl_take isl_set *set,
7826 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7827 __isl_take isl_basic_set *bset))
7829 unsigned flags = 0;
7830 struct isl_map *result;
7831 int i, j;
7833 if (isl_set_plain_is_universe(set)) {
7834 isl_set_free(set);
7835 return map;
7838 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7839 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7840 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7842 result = isl_map_alloc_space(isl_space_copy(map->dim),
7843 map->n * set->n, flags);
7844 for (i = 0; result && i < map->n; ++i)
7845 for (j = 0; j < set->n; ++j) {
7846 result = isl_map_add_basic_map(result,
7847 fn(isl_basic_map_copy(map->p[i]),
7848 isl_basic_set_copy(set->p[j])));
7849 if (!result)
7850 break;
7853 isl_map_free(map);
7854 isl_set_free(set);
7855 return result;
7858 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7859 __isl_take isl_set *set)
7861 isl_bool ok;
7863 ok = isl_map_compatible_range(map, set);
7864 if (ok < 0)
7865 goto error;
7866 if (!ok)
7867 isl_die(set->ctx, isl_error_invalid,
7868 "incompatible spaces", goto error);
7870 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7871 error:
7872 isl_map_free(map);
7873 isl_set_free(set);
7874 return NULL;
7877 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7878 __isl_take isl_set *set)
7880 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7883 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7884 __isl_take isl_set *set)
7886 isl_bool ok;
7888 ok = isl_map_compatible_domain(map, set);
7889 if (ok < 0)
7890 goto error;
7891 if (!ok)
7892 isl_die(set->ctx, isl_error_invalid,
7893 "incompatible spaces", goto error);
7895 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7896 error:
7897 isl_map_free(map);
7898 isl_set_free(set);
7899 return NULL;
7902 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7903 __isl_take isl_set *set)
7905 return isl_map_align_params_map_map_and(map, set,
7906 &map_intersect_domain);
7909 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7910 * in the space B -> C, return the intersection.
7911 * The parameters are assumed to have been aligned.
7913 * The map "factor" is first extended to a map living in the space
7914 * [A -> B] -> C and then a regular intersection is computed.
7916 static __isl_give isl_map *map_intersect_domain_factor_range(
7917 __isl_take isl_map *map, __isl_take isl_map *factor)
7919 isl_space *space;
7920 isl_map *ext_factor;
7922 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7923 ext_factor = isl_map_universe(space);
7924 ext_factor = isl_map_domain_product(ext_factor, factor);
7925 return map_intersect(map, ext_factor);
7928 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7929 * in the space B -> C, return the intersection.
7931 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7932 __isl_take isl_map *map, __isl_take isl_map *factor)
7934 return isl_map_align_params_map_map_and(map, factor,
7935 &map_intersect_domain_factor_range);
7938 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7939 * in the space A -> C, return the intersection.
7941 * The map "factor" is first extended to a map living in the space
7942 * A -> [B -> C] and then a regular intersection is computed.
7944 static __isl_give isl_map *map_intersect_range_factor_range(
7945 __isl_take isl_map *map, __isl_take isl_map *factor)
7947 isl_space *space;
7948 isl_map *ext_factor;
7950 space = isl_space_range_factor_domain(isl_map_get_space(map));
7951 ext_factor = isl_map_universe(space);
7952 ext_factor = isl_map_range_product(ext_factor, factor);
7953 return isl_map_intersect(map, ext_factor);
7956 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7957 * in the space A -> C, return the intersection.
7959 __isl_give isl_map *isl_map_intersect_range_factor_range(
7960 __isl_take isl_map *map, __isl_take isl_map *factor)
7962 return isl_map_align_params_map_map_and(map, factor,
7963 &map_intersect_range_factor_range);
7966 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7967 __isl_take isl_map *map2)
7969 if (!map1 || !map2)
7970 goto error;
7971 map1 = isl_map_reverse(map1);
7972 map1 = isl_map_apply_range(map1, map2);
7973 return isl_map_reverse(map1);
7974 error:
7975 isl_map_free(map1);
7976 isl_map_free(map2);
7977 return NULL;
7980 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7981 __isl_take isl_map *map2)
7983 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7986 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7987 __isl_take isl_map *map2)
7989 isl_space *space;
7990 struct isl_map *result;
7991 int i, j;
7993 if (!map1 || !map2)
7994 goto error;
7996 space = isl_space_join(isl_space_copy(map1->dim),
7997 isl_space_copy(map2->dim));
7999 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8000 if (!result)
8001 goto error;
8002 for (i = 0; i < map1->n; ++i)
8003 for (j = 0; j < map2->n; ++j) {
8004 result = isl_map_add_basic_map(result,
8005 isl_basic_map_apply_range(
8006 isl_basic_map_copy(map1->p[i]),
8007 isl_basic_map_copy(map2->p[j])));
8008 if (!result)
8009 goto error;
8011 isl_map_free(map1);
8012 isl_map_free(map2);
8013 if (result && result->n <= 1)
8014 ISL_F_SET(result, ISL_MAP_DISJOINT);
8015 return result;
8016 error:
8017 isl_map_free(map1);
8018 isl_map_free(map2);
8019 return NULL;
8022 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8023 __isl_take isl_map *map2)
8025 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8029 * returns range - domain
8031 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8033 isl_space *target_space;
8034 struct isl_basic_set *bset;
8035 unsigned dim;
8036 unsigned nparam;
8037 int i;
8039 if (!bmap)
8040 goto error;
8041 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8042 bmap->dim, isl_dim_out),
8043 goto error);
8044 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8045 dim = isl_basic_map_dim(bmap, isl_dim_in);
8046 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8047 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8048 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8049 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8050 for (i = 0; i < dim; ++i) {
8051 int j = isl_basic_map_alloc_equality(bmap);
8052 if (j < 0) {
8053 bmap = isl_basic_map_free(bmap);
8054 break;
8056 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8057 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8058 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8059 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8061 bset = isl_basic_map_domain(bmap);
8062 bset = isl_basic_set_reset_space(bset, target_space);
8063 return bset;
8064 error:
8065 isl_basic_map_free(bmap);
8066 return NULL;
8069 /* Check that domain and range of "map" are the same.
8071 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8073 isl_space *space;
8074 isl_bool equal;
8076 space = isl_map_peek_space(map);
8077 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8078 if (equal < 0)
8079 return isl_stat_error;
8080 if (!equal)
8081 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8082 "domain and range don't match", return isl_stat_error);
8083 return isl_stat_ok;
8087 * returns range - domain
8089 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8091 int i;
8092 isl_space *dim;
8093 struct isl_set *result;
8095 if (!map)
8096 return NULL;
8098 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8099 map->dim, isl_dim_out),
8100 goto error);
8101 dim = isl_map_get_space(map);
8102 dim = isl_space_domain(dim);
8103 result = isl_set_alloc_space(dim, map->n, 0);
8104 if (!result)
8105 goto error;
8106 for (i = 0; i < map->n; ++i)
8107 result = isl_set_add_basic_set(result,
8108 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8109 isl_map_free(map);
8110 return result;
8111 error:
8112 isl_map_free(map);
8113 return NULL;
8117 * returns [domain -> range] -> range - domain
8119 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8120 __isl_take isl_basic_map *bmap)
8122 int i, k;
8123 isl_space *space;
8124 isl_basic_map *domain;
8125 int nparam, n;
8126 unsigned total;
8128 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8129 bmap->dim, isl_dim_out))
8130 isl_die(bmap->ctx, isl_error_invalid,
8131 "domain and range don't match", goto error);
8133 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8134 n = isl_basic_map_dim(bmap, isl_dim_in);
8136 space = isl_basic_map_get_space(bmap);
8137 space = isl_space_from_range(isl_space_domain(space));
8138 domain = isl_basic_map_universe(space);
8140 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8141 bmap = isl_basic_map_apply_range(bmap, domain);
8142 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8144 total = isl_basic_map_total_dim(bmap);
8146 for (i = 0; i < n; ++i) {
8147 k = isl_basic_map_alloc_equality(bmap);
8148 if (k < 0)
8149 goto error;
8150 isl_seq_clr(bmap->eq[k], 1 + total);
8151 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8152 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8153 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8156 bmap = isl_basic_map_gauss(bmap, NULL);
8157 return isl_basic_map_finalize(bmap);
8158 error:
8159 isl_basic_map_free(bmap);
8160 return NULL;
8164 * returns [domain -> range] -> range - domain
8166 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8168 int i;
8169 isl_space *domain_space;
8171 if (isl_map_check_equal_tuples(map) < 0)
8172 return isl_map_free(map);
8174 map = isl_map_cow(map);
8175 if (!map)
8176 return NULL;
8178 domain_space = isl_space_domain(isl_map_get_space(map));
8179 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8180 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8181 if (!map->dim)
8182 goto error;
8183 for (i = 0; i < map->n; ++i) {
8184 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8185 if (!map->p[i])
8186 goto error;
8188 map = isl_map_unmark_normalized(map);
8189 return map;
8190 error:
8191 isl_map_free(map);
8192 return NULL;
8195 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8197 unsigned n_in, n_out;
8199 if (!space)
8200 return NULL;
8201 n_in = isl_space_dim(space, isl_dim_in);
8202 n_out = isl_space_dim(space, isl_dim_out);
8203 if (n_in != n_out)
8204 isl_die(space->ctx, isl_error_invalid,
8205 "number of input and output dimensions needs to be "
8206 "the same", goto error);
8207 return isl_basic_map_equal(space, n_in);
8208 error:
8209 isl_space_free(space);
8210 return NULL;
8213 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8215 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8218 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8220 isl_space *dim = isl_set_get_space(set);
8221 isl_map *id;
8222 id = isl_map_identity(isl_space_map_from_set(dim));
8223 return isl_map_intersect_range(id, set);
8226 /* Construct a basic set with all set dimensions having only non-negative
8227 * values.
8229 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8230 __isl_take isl_space *space)
8232 int i;
8233 unsigned nparam;
8234 unsigned dim;
8235 struct isl_basic_set *bset;
8237 if (!space)
8238 return NULL;
8239 nparam = space->nparam;
8240 dim = space->n_out;
8241 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8242 if (!bset)
8243 return NULL;
8244 for (i = 0; i < dim; ++i) {
8245 int k = isl_basic_set_alloc_inequality(bset);
8246 if (k < 0)
8247 goto error;
8248 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8249 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8251 return bset;
8252 error:
8253 isl_basic_set_free(bset);
8254 return NULL;
8257 /* Construct the half-space x_pos >= 0.
8259 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8260 int pos)
8262 int k;
8263 isl_basic_set *nonneg;
8265 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8266 k = isl_basic_set_alloc_inequality(nonneg);
8267 if (k < 0)
8268 goto error;
8269 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8270 isl_int_set_si(nonneg->ineq[k][pos], 1);
8272 return isl_basic_set_finalize(nonneg);
8273 error:
8274 isl_basic_set_free(nonneg);
8275 return NULL;
8278 /* Construct the half-space x_pos <= -1.
8280 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8281 int pos)
8283 int k;
8284 isl_basic_set *neg;
8286 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8287 k = isl_basic_set_alloc_inequality(neg);
8288 if (k < 0)
8289 goto error;
8290 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8291 isl_int_set_si(neg->ineq[k][0], -1);
8292 isl_int_set_si(neg->ineq[k][pos], -1);
8294 return isl_basic_set_finalize(neg);
8295 error:
8296 isl_basic_set_free(neg);
8297 return NULL;
8300 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8301 enum isl_dim_type type, unsigned first, unsigned n)
8303 int i;
8304 unsigned offset;
8305 isl_basic_set *nonneg;
8306 isl_basic_set *neg;
8308 if (n == 0)
8309 return set;
8311 if (isl_set_check_range(set, type, first, n) < 0)
8312 return isl_set_free(set);
8314 offset = pos(set->dim, type);
8315 for (i = 0; i < n; ++i) {
8316 nonneg = nonneg_halfspace(isl_set_get_space(set),
8317 offset + first + i);
8318 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8320 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8323 return set;
8326 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8327 int len,
8328 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8329 void *user)
8331 isl_set *half;
8333 if (!set)
8334 return isl_stat_error;
8335 if (isl_set_plain_is_empty(set)) {
8336 isl_set_free(set);
8337 return isl_stat_ok;
8339 if (first == len)
8340 return fn(set, signs, user);
8342 signs[first] = 1;
8343 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8344 1 + first));
8345 half = isl_set_intersect(half, isl_set_copy(set));
8346 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8347 goto error;
8349 signs[first] = -1;
8350 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8351 1 + first));
8352 half = isl_set_intersect(half, set);
8353 return foreach_orthant(half, signs, first + 1, len, fn, user);
8354 error:
8355 isl_set_free(set);
8356 return isl_stat_error;
8359 /* Call "fn" on the intersections of "set" with each of the orthants
8360 * (except for obviously empty intersections). The orthant is identified
8361 * by the signs array, with each entry having value 1 or -1 according
8362 * to the sign of the corresponding variable.
8364 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8365 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8366 void *user)
8368 unsigned nparam;
8369 unsigned nvar;
8370 int *signs;
8371 isl_stat r;
8373 if (!set)
8374 return isl_stat_error;
8375 if (isl_set_plain_is_empty(set))
8376 return isl_stat_ok;
8378 nparam = isl_set_dim(set, isl_dim_param);
8379 nvar = isl_set_dim(set, isl_dim_set);
8381 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8383 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8384 fn, user);
8386 free(signs);
8388 return r;
8391 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8393 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8396 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8397 __isl_keep isl_basic_map *bmap2)
8399 isl_bool is_subset;
8400 struct isl_map *map1;
8401 struct isl_map *map2;
8403 if (!bmap1 || !bmap2)
8404 return isl_bool_error;
8406 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8407 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8409 is_subset = isl_map_is_subset(map1, map2);
8411 isl_map_free(map1);
8412 isl_map_free(map2);
8414 return is_subset;
8417 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8418 __isl_keep isl_basic_set *bset2)
8420 return isl_basic_map_is_subset(bset1, bset2);
8423 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8424 __isl_keep isl_basic_map *bmap2)
8426 isl_bool is_subset;
8428 if (!bmap1 || !bmap2)
8429 return isl_bool_error;
8430 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8431 if (is_subset != isl_bool_true)
8432 return is_subset;
8433 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8434 return is_subset;
8437 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8438 __isl_keep isl_basic_set *bset2)
8440 return isl_basic_map_is_equal(
8441 bset_to_bmap(bset1), bset_to_bmap(bset2));
8444 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8446 int i;
8447 int is_empty;
8449 if (!map)
8450 return isl_bool_error;
8451 for (i = 0; i < map->n; ++i) {
8452 is_empty = isl_basic_map_is_empty(map->p[i]);
8453 if (is_empty < 0)
8454 return isl_bool_error;
8455 if (!is_empty)
8456 return isl_bool_false;
8458 return isl_bool_true;
8461 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8463 return map ? map->n == 0 : isl_bool_error;
8466 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8468 return set ? set->n == 0 : isl_bool_error;
8471 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8473 return isl_map_is_empty(set_to_map(set));
8476 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8477 __isl_keep isl_map *map2)
8479 if (!map1 || !map2)
8480 return isl_bool_error;
8482 return isl_space_is_equal(map1->dim, map2->dim);
8485 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8486 __isl_keep isl_set *set2)
8488 if (!set1 || !set2)
8489 return isl_bool_error;
8491 return isl_space_is_equal(set1->dim, set2->dim);
8494 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8496 isl_bool is_subset;
8498 if (!map1 || !map2)
8499 return isl_bool_error;
8500 is_subset = isl_map_is_subset(map1, map2);
8501 if (is_subset != isl_bool_true)
8502 return is_subset;
8503 is_subset = isl_map_is_subset(map2, map1);
8504 return is_subset;
8507 /* Is "map1" equal to "map2"?
8509 * First check if they are obviously equal.
8510 * If not, then perform a more detailed analysis.
8512 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8514 isl_bool equal;
8516 equal = isl_map_plain_is_equal(map1, map2);
8517 if (equal < 0 || equal)
8518 return equal;
8519 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8522 isl_bool isl_basic_map_is_strict_subset(
8523 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8525 isl_bool is_subset;
8527 if (!bmap1 || !bmap2)
8528 return isl_bool_error;
8529 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8530 if (is_subset != isl_bool_true)
8531 return is_subset;
8532 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8533 return isl_bool_not(is_subset);
8536 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8537 __isl_keep isl_map *map2)
8539 isl_bool is_subset;
8541 if (!map1 || !map2)
8542 return isl_bool_error;
8543 is_subset = isl_map_is_subset(map1, map2);
8544 if (is_subset != isl_bool_true)
8545 return is_subset;
8546 is_subset = isl_map_is_subset(map2, map1);
8547 return isl_bool_not(is_subset);
8550 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8551 __isl_keep isl_set *set2)
8553 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8556 /* Is "bmap" obviously equal to the universe with the same space?
8558 * That is, does it not have any constraints?
8560 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8562 if (!bmap)
8563 return isl_bool_error;
8564 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8567 /* Is "bset" obviously equal to the universe with the same space?
8569 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8571 return isl_basic_map_plain_is_universe(bset);
8574 /* If "c" does not involve any existentially quantified variables,
8575 * then set *univ to false and abort
8577 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8579 isl_bool *univ = user;
8580 unsigned n;
8582 n = isl_constraint_dim(c, isl_dim_div);
8583 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8584 isl_constraint_free(c);
8585 if (*univ < 0 || !*univ)
8586 return isl_stat_error;
8587 return isl_stat_ok;
8590 /* Is "bmap" equal to the universe with the same space?
8592 * First check if it is obviously equal to the universe.
8593 * If not and if there are any constraints not involving
8594 * existentially quantified variables, then it is certainly
8595 * not equal to the universe.
8596 * Otherwise, check if the universe is a subset of "bmap".
8598 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8600 isl_bool univ;
8601 isl_basic_map *test;
8603 univ = isl_basic_map_plain_is_universe(bmap);
8604 if (univ < 0 || univ)
8605 return univ;
8606 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8607 return isl_bool_false;
8608 univ = isl_bool_true;
8609 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8610 univ)
8611 return isl_bool_error;
8612 if (univ < 0 || !univ)
8613 return univ;
8614 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8615 univ = isl_basic_map_is_subset(test, bmap);
8616 isl_basic_map_free(test);
8617 return univ;
8620 /* Is "bset" equal to the universe with the same space?
8622 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8624 return isl_basic_map_is_universe(bset);
8627 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8629 int i;
8631 if (!map)
8632 return isl_bool_error;
8634 for (i = 0; i < map->n; ++i) {
8635 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8636 if (r < 0 || r)
8637 return r;
8640 return isl_bool_false;
8643 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8645 return isl_map_plain_is_universe(set_to_map(set));
8648 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8650 struct isl_basic_set *bset = NULL;
8651 struct isl_vec *sample = NULL;
8652 isl_bool empty, non_empty;
8654 if (!bmap)
8655 return isl_bool_error;
8657 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8658 return isl_bool_true;
8660 if (isl_basic_map_plain_is_universe(bmap))
8661 return isl_bool_false;
8663 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8664 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8665 copy = isl_basic_map_remove_redundancies(copy);
8666 empty = isl_basic_map_plain_is_empty(copy);
8667 isl_basic_map_free(copy);
8668 return empty;
8671 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8672 if (non_empty < 0)
8673 return isl_bool_error;
8674 if (non_empty)
8675 return isl_bool_false;
8676 isl_vec_free(bmap->sample);
8677 bmap->sample = NULL;
8678 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8679 if (!bset)
8680 return isl_bool_error;
8681 sample = isl_basic_set_sample_vec(bset);
8682 if (!sample)
8683 return isl_bool_error;
8684 empty = sample->size == 0;
8685 isl_vec_free(bmap->sample);
8686 bmap->sample = sample;
8687 if (empty)
8688 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8690 return empty;
8693 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8695 if (!bmap)
8696 return isl_bool_error;
8697 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8700 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8702 if (!bset)
8703 return isl_bool_error;
8704 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8707 /* Is "bmap" known to be non-empty?
8709 * That is, is the cached sample still valid?
8711 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8713 unsigned total;
8715 if (!bmap)
8716 return isl_bool_error;
8717 if (!bmap->sample)
8718 return isl_bool_false;
8719 total = 1 + isl_basic_map_total_dim(bmap);
8720 if (bmap->sample->size != total)
8721 return isl_bool_false;
8722 return isl_basic_map_contains(bmap, bmap->sample);
8725 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8727 return isl_basic_map_is_empty(bset_to_bmap(bset));
8730 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8731 __isl_take isl_basic_map *bmap2)
8733 struct isl_map *map;
8734 if (!bmap1 || !bmap2)
8735 goto error;
8737 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8739 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8740 if (!map)
8741 goto error;
8742 map = isl_map_add_basic_map(map, bmap1);
8743 map = isl_map_add_basic_map(map, bmap2);
8744 return map;
8745 error:
8746 isl_basic_map_free(bmap1);
8747 isl_basic_map_free(bmap2);
8748 return NULL;
8751 struct isl_set *isl_basic_set_union(
8752 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8754 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8755 bset_to_bmap(bset2)));
8758 /* Order divs such that any div only depends on previous divs */
8759 __isl_give isl_basic_map *isl_basic_map_order_divs(
8760 __isl_take isl_basic_map *bmap)
8762 int i;
8763 unsigned off;
8765 if (!bmap)
8766 return NULL;
8768 off = isl_space_dim(bmap->dim, isl_dim_all);
8770 for (i = 0; i < bmap->n_div; ++i) {
8771 int pos;
8772 if (isl_int_is_zero(bmap->div[i][0]))
8773 continue;
8774 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8775 bmap->n_div-i);
8776 if (pos == -1)
8777 continue;
8778 if (pos == 0)
8779 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8780 "integer division depends on itself",
8781 return isl_basic_map_free(bmap));
8782 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8783 if (!bmap)
8784 return NULL;
8785 --i;
8787 return bmap;
8790 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8792 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8795 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8797 int i;
8799 if (!map)
8800 return 0;
8802 for (i = 0; i < map->n; ++i) {
8803 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8804 if (!map->p[i])
8805 goto error;
8808 return map;
8809 error:
8810 isl_map_free(map);
8811 return NULL;
8814 /* Sort the local variables of "bset".
8816 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8817 __isl_take isl_basic_set *bset)
8819 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8822 /* Apply the expansion computed by isl_merge_divs.
8823 * The expansion itself is given by "exp" while the resulting
8824 * list of divs is given by "div".
8826 * Move the integer divisions of "bmap" into the right position
8827 * according to "exp" and then introduce the additional integer
8828 * divisions, adding div constraints.
8829 * The moving should be done first to avoid moving coefficients
8830 * in the definitions of the extra integer divisions.
8832 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8833 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8835 int i, j;
8836 int n_div;
8838 bmap = isl_basic_map_cow(bmap);
8839 if (!bmap || !div)
8840 goto error;
8842 if (div->n_row < bmap->n_div)
8843 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8844 "not an expansion", goto error);
8846 n_div = bmap->n_div;
8847 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8848 div->n_row - n_div, 0,
8849 2 * (div->n_row - n_div));
8851 for (i = n_div; i < div->n_row; ++i)
8852 if (isl_basic_map_alloc_div(bmap) < 0)
8853 goto error;
8855 for (j = n_div - 1; j >= 0; --j) {
8856 if (exp[j] == j)
8857 break;
8858 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8859 if (!bmap)
8860 goto error;
8862 j = 0;
8863 for (i = 0; i < div->n_row; ++i) {
8864 if (j < n_div && exp[j] == i) {
8865 j++;
8866 } else {
8867 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8868 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8869 continue;
8870 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8871 goto error;
8875 isl_mat_free(div);
8876 return bmap;
8877 error:
8878 isl_basic_map_free(bmap);
8879 isl_mat_free(div);
8880 return NULL;
8883 /* Apply the expansion computed by isl_merge_divs.
8884 * The expansion itself is given by "exp" while the resulting
8885 * list of divs is given by "div".
8887 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8888 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8890 return isl_basic_map_expand_divs(bset, div, exp);
8893 /* Look for a div in dst that corresponds to the div "div" in src.
8894 * The divs before "div" in src and dst are assumed to be the same.
8896 * Returns -1 if no corresponding div was found and the position
8897 * of the corresponding div in dst otherwise.
8899 static int find_div(__isl_keep isl_basic_map *dst,
8900 __isl_keep isl_basic_map *src, unsigned div)
8902 int i;
8904 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8906 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8907 for (i = div; i < dst->n_div; ++i)
8908 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8909 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8910 dst->n_div - div) == -1)
8911 return i;
8912 return -1;
8915 /* Align the divs of "dst" to those of "src", adding divs from "src"
8916 * if needed. That is, make sure that the first src->n_div divs
8917 * of the result are equal to those of src.
8919 * The result is not finalized as by design it will have redundant
8920 * divs if any divs from "src" were copied.
8922 __isl_give isl_basic_map *isl_basic_map_align_divs(
8923 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8925 int i;
8926 isl_bool known;
8927 int extended;
8928 unsigned total;
8930 if (!dst || !src)
8931 return isl_basic_map_free(dst);
8933 if (src->n_div == 0)
8934 return dst;
8936 known = isl_basic_map_divs_known(src);
8937 if (known < 0)
8938 return isl_basic_map_free(dst);
8939 if (!known)
8940 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8941 "some src divs are unknown",
8942 return isl_basic_map_free(dst));
8944 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8945 if (!src)
8946 return isl_basic_map_free(dst);
8948 extended = 0;
8949 total = isl_space_dim(src->dim, isl_dim_all);
8950 for (i = 0; i < src->n_div; ++i) {
8951 int j = find_div(dst, src, i);
8952 if (j < 0) {
8953 if (!extended) {
8954 int extra = src->n_div - i;
8955 dst = isl_basic_map_cow(dst);
8956 if (!dst)
8957 goto error;
8958 dst = isl_basic_map_extend_space(dst,
8959 isl_space_copy(dst->dim),
8960 extra, 0, 2 * extra);
8961 extended = 1;
8963 j = isl_basic_map_alloc_div(dst);
8964 if (j < 0)
8965 goto error;
8966 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8967 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8968 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8969 goto error;
8971 if (j != i)
8972 dst = isl_basic_map_swap_div(dst, i, j);
8973 if (!dst)
8974 goto error;
8976 isl_basic_map_free(src);
8977 return dst;
8978 error:
8979 isl_basic_map_free(src);
8980 isl_basic_map_free(dst);
8981 return NULL;
8984 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8986 int i;
8988 if (!map)
8989 return NULL;
8990 if (map->n == 0)
8991 return map;
8992 map = isl_map_compute_divs(map);
8993 map = isl_map_cow(map);
8994 if (!map)
8995 return NULL;
8997 for (i = 1; i < map->n; ++i)
8998 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8999 for (i = 1; i < map->n; ++i) {
9000 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9001 if (!map->p[i])
9002 return isl_map_free(map);
9005 map = isl_map_unmark_normalized(map);
9006 return map;
9009 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9011 return isl_map_align_divs_internal(map);
9014 struct isl_set *isl_set_align_divs(struct isl_set *set)
9016 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9019 /* Align the divs of the basic maps in "map" to those
9020 * of the basic maps in "list", as well as to the other basic maps in "map".
9021 * The elements in "list" are assumed to have known divs.
9023 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9024 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9026 int i, n;
9028 map = isl_map_compute_divs(map);
9029 map = isl_map_cow(map);
9030 if (!map || !list)
9031 return isl_map_free(map);
9032 if (map->n == 0)
9033 return map;
9035 n = isl_basic_map_list_n_basic_map(list);
9036 for (i = 0; i < n; ++i) {
9037 isl_basic_map *bmap;
9039 bmap = isl_basic_map_list_get_basic_map(list, i);
9040 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9041 isl_basic_map_free(bmap);
9043 if (!map->p[0])
9044 return isl_map_free(map);
9046 return isl_map_align_divs_internal(map);
9049 /* Align the divs of each element of "list" to those of "bmap".
9050 * Both "bmap" and the elements of "list" are assumed to have known divs.
9052 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9053 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9055 int i, n;
9057 if (!list || !bmap)
9058 return isl_basic_map_list_free(list);
9060 n = isl_basic_map_list_n_basic_map(list);
9061 for (i = 0; i < n; ++i) {
9062 isl_basic_map *bmap_i;
9064 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9065 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9066 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9069 return list;
9072 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9073 __isl_take isl_map *map)
9075 isl_bool ok;
9077 ok = isl_map_compatible_domain(map, set);
9078 if (ok < 0)
9079 goto error;
9080 if (!ok)
9081 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9082 "incompatible spaces", goto error);
9083 map = isl_map_intersect_domain(map, set);
9084 set = isl_map_range(map);
9085 return set;
9086 error:
9087 isl_set_free(set);
9088 isl_map_free(map);
9089 return NULL;
9092 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9093 __isl_take isl_map *map)
9095 return isl_map_align_params_map_map_and(set, map, &set_apply);
9098 /* There is no need to cow as removing empty parts doesn't change
9099 * the meaning of the set.
9101 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9103 int i;
9105 if (!map)
9106 return NULL;
9108 for (i = map->n - 1; i >= 0; --i)
9109 map = remove_if_empty(map, i);
9111 return map;
9114 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9116 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9119 /* Create a binary relation that maps the shared initial "pos" dimensions
9120 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9122 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9123 __isl_keep isl_basic_set *bset2, int pos)
9125 isl_basic_map *bmap1;
9126 isl_basic_map *bmap2;
9128 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9129 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9130 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9131 isl_dim_out, 0, pos);
9132 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9133 isl_dim_out, 0, pos);
9134 return isl_basic_map_range_product(bmap1, bmap2);
9137 /* Given two basic sets bset1 and bset2, compute the maximal difference
9138 * between the values of dimension pos in bset1 and those in bset2
9139 * for any common value of the parameters and dimensions preceding pos.
9141 static enum isl_lp_result basic_set_maximal_difference_at(
9142 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9143 int pos, isl_int *opt)
9145 isl_basic_map *bmap1;
9146 struct isl_ctx *ctx;
9147 struct isl_vec *obj;
9148 unsigned total;
9149 unsigned nparam;
9150 unsigned dim1;
9151 enum isl_lp_result res;
9153 if (!bset1 || !bset2)
9154 return isl_lp_error;
9156 nparam = isl_basic_set_n_param(bset1);
9157 dim1 = isl_basic_set_n_dim(bset1);
9159 bmap1 = join_initial(bset1, bset2, pos);
9160 if (!bmap1)
9161 return isl_lp_error;
9163 total = isl_basic_map_total_dim(bmap1);
9164 ctx = bmap1->ctx;
9165 obj = isl_vec_alloc(ctx, 1 + total);
9166 if (!obj)
9167 goto error;
9168 isl_seq_clr(obj->block.data, 1 + total);
9169 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9170 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9171 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9172 opt, NULL, NULL);
9173 isl_basic_map_free(bmap1);
9174 isl_vec_free(obj);
9175 return res;
9176 error:
9177 isl_basic_map_free(bmap1);
9178 return isl_lp_error;
9181 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9182 * for any common value of the parameters and dimensions preceding pos
9183 * in both basic sets, the values of dimension pos in bset1 are
9184 * smaller or larger than those in bset2.
9186 * Returns
9187 * 1 if bset1 follows bset2
9188 * -1 if bset1 precedes bset2
9189 * 0 if bset1 and bset2 are incomparable
9190 * -2 if some error occurred.
9192 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9193 __isl_keep isl_basic_set *bset2, int pos)
9195 isl_int opt;
9196 enum isl_lp_result res;
9197 int cmp;
9199 isl_int_init(opt);
9201 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9203 if (res == isl_lp_empty)
9204 cmp = 0;
9205 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9206 res == isl_lp_unbounded)
9207 cmp = 1;
9208 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9209 cmp = -1;
9210 else
9211 cmp = -2;
9213 isl_int_clear(opt);
9214 return cmp;
9217 /* Given two basic sets bset1 and bset2, check whether
9218 * for any common value of the parameters and dimensions preceding pos
9219 * there is a value of dimension pos in bset1 that is larger
9220 * than a value of the same dimension in bset2.
9222 * Return
9223 * 1 if there exists such a pair
9224 * 0 if there is no such pair, but there is a pair of equal values
9225 * -1 otherwise
9226 * -2 if some error occurred.
9228 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9229 __isl_keep isl_basic_set *bset2, int pos)
9231 isl_bool empty;
9232 isl_basic_map *bmap;
9233 unsigned dim1;
9235 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9236 bmap = join_initial(bset1, bset2, pos);
9237 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9238 isl_dim_out, dim1 - pos);
9239 empty = isl_basic_map_is_empty(bmap);
9240 if (empty < 0)
9241 goto error;
9242 if (empty) {
9243 isl_basic_map_free(bmap);
9244 return -1;
9246 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9247 isl_dim_out, dim1 - pos);
9248 empty = isl_basic_map_is_empty(bmap);
9249 if (empty < 0)
9250 goto error;
9251 isl_basic_map_free(bmap);
9252 if (empty)
9253 return 0;
9254 return 1;
9255 error:
9256 isl_basic_map_free(bmap);
9257 return -2;
9260 /* Given two sets set1 and set2, check whether
9261 * for any common value of the parameters and dimensions preceding pos
9262 * there is a value of dimension pos in set1 that is larger
9263 * than a value of the same dimension in set2.
9265 * Return
9266 * 1 if there exists such a pair
9267 * 0 if there is no such pair, but there is a pair of equal values
9268 * -1 otherwise
9269 * -2 if some error occurred.
9271 int isl_set_follows_at(__isl_keep isl_set *set1,
9272 __isl_keep isl_set *set2, int pos)
9274 int i, j;
9275 int follows = -1;
9277 if (!set1 || !set2)
9278 return -2;
9280 for (i = 0; i < set1->n; ++i)
9281 for (j = 0; j < set2->n; ++j) {
9282 int f;
9283 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9284 if (f == 1 || f == -2)
9285 return f;
9286 if (f > follows)
9287 follows = f;
9290 return follows;
9293 static isl_bool isl_basic_map_plain_has_fixed_var(
9294 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9296 int i;
9297 int d;
9298 unsigned total;
9300 if (!bmap)
9301 return isl_bool_error;
9302 total = isl_basic_map_total_dim(bmap);
9303 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9304 for (; d+1 > pos; --d)
9305 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9306 break;
9307 if (d != pos)
9308 continue;
9309 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9310 return isl_bool_false;
9311 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9312 return isl_bool_false;
9313 if (!isl_int_is_one(bmap->eq[i][1+d]))
9314 return isl_bool_false;
9315 if (val)
9316 isl_int_neg(*val, bmap->eq[i][0]);
9317 return isl_bool_true;
9319 return isl_bool_false;
9322 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9323 unsigned pos, isl_int *val)
9325 int i;
9326 isl_int v;
9327 isl_int tmp;
9328 isl_bool fixed;
9330 if (!map)
9331 return isl_bool_error;
9332 if (map->n == 0)
9333 return isl_bool_false;
9334 if (map->n == 1)
9335 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9336 isl_int_init(v);
9337 isl_int_init(tmp);
9338 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9339 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9340 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9341 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9342 fixed = isl_bool_false;
9344 if (val)
9345 isl_int_set(*val, v);
9346 isl_int_clear(tmp);
9347 isl_int_clear(v);
9348 return fixed;
9351 static isl_bool isl_basic_set_plain_has_fixed_var(
9352 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9354 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9355 pos, val);
9358 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9359 enum isl_dim_type type, unsigned pos, isl_int *val)
9361 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9362 return isl_bool_error;
9363 return isl_basic_map_plain_has_fixed_var(bmap,
9364 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9367 /* If "bmap" obviously lies on a hyperplane where the given dimension
9368 * has a fixed value, then return that value.
9369 * Otherwise return NaN.
9371 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9372 __isl_keep isl_basic_map *bmap,
9373 enum isl_dim_type type, unsigned pos)
9375 isl_ctx *ctx;
9376 isl_val *v;
9377 isl_bool fixed;
9379 if (!bmap)
9380 return NULL;
9381 ctx = isl_basic_map_get_ctx(bmap);
9382 v = isl_val_alloc(ctx);
9383 if (!v)
9384 return NULL;
9385 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9386 if (fixed < 0)
9387 return isl_val_free(v);
9388 if (fixed) {
9389 isl_int_set_si(v->d, 1);
9390 return v;
9392 isl_val_free(v);
9393 return isl_val_nan(ctx);
9396 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9397 enum isl_dim_type type, unsigned pos, isl_int *val)
9399 if (isl_map_check_range(map, type, pos, 1) < 0)
9400 return isl_bool_error;
9401 return isl_map_plain_has_fixed_var(map,
9402 map_offset(map, type) - 1 + pos, val);
9405 /* If "map" obviously lies on a hyperplane where the given dimension
9406 * has a fixed value, then return that value.
9407 * Otherwise return NaN.
9409 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9410 enum isl_dim_type type, unsigned pos)
9412 isl_ctx *ctx;
9413 isl_val *v;
9414 isl_bool fixed;
9416 if (!map)
9417 return NULL;
9418 ctx = isl_map_get_ctx(map);
9419 v = isl_val_alloc(ctx);
9420 if (!v)
9421 return NULL;
9422 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9423 if (fixed < 0)
9424 return isl_val_free(v);
9425 if (fixed) {
9426 isl_int_set_si(v->d, 1);
9427 return v;
9429 isl_val_free(v);
9430 return isl_val_nan(ctx);
9433 /* If "set" obviously lies on a hyperplane where the given dimension
9434 * has a fixed value, then return that value.
9435 * Otherwise return NaN.
9437 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9438 enum isl_dim_type type, unsigned pos)
9440 return isl_map_plain_get_val_if_fixed(set, type, pos);
9443 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9444 * then return this fixed value in *val.
9446 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9447 unsigned dim, isl_int *val)
9449 return isl_basic_set_plain_has_fixed_var(bset,
9450 isl_basic_set_n_param(bset) + dim, val);
9453 /* Return -1 if the constraint "c1" should be sorted before "c2"
9454 * and 1 if it should be sorted after "c2".
9455 * Return 0 if the two constraints are the same (up to the constant term).
9457 * In particular, if a constraint involves later variables than another
9458 * then it is sorted after this other constraint.
9459 * uset_gist depends on constraints without existentially quantified
9460 * variables sorting first.
9462 * For constraints that have the same latest variable, those
9463 * with the same coefficient for this latest variable (first in absolute value
9464 * and then in actual value) are grouped together.
9465 * This is useful for detecting pairs of constraints that can
9466 * be chained in their printed representation.
9468 * Finally, within a group, constraints are sorted according to
9469 * their coefficients (excluding the constant term).
9471 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9473 isl_int **c1 = (isl_int **) p1;
9474 isl_int **c2 = (isl_int **) p2;
9475 int l1, l2;
9476 unsigned size = *(unsigned *) arg;
9477 int cmp;
9479 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9480 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9482 if (l1 != l2)
9483 return l1 - l2;
9485 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9486 if (cmp != 0)
9487 return cmp;
9488 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9489 if (cmp != 0)
9490 return -cmp;
9492 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9495 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9496 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9497 * and 0 if the two constraints are the same (up to the constant term).
9499 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9500 isl_int *c1, isl_int *c2)
9502 unsigned total;
9504 if (!bmap)
9505 return -2;
9506 total = isl_basic_map_total_dim(bmap);
9507 return sort_constraint_cmp(&c1, &c2, &total);
9510 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9511 __isl_take isl_basic_map *bmap)
9513 unsigned total;
9515 if (!bmap)
9516 return NULL;
9517 if (bmap->n_ineq == 0)
9518 return bmap;
9519 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9520 return bmap;
9521 total = isl_basic_map_total_dim(bmap);
9522 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9523 &sort_constraint_cmp, &total) < 0)
9524 return isl_basic_map_free(bmap);
9525 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9526 return bmap;
9529 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9530 __isl_take isl_basic_set *bset)
9532 isl_basic_map *bmap = bset_to_bmap(bset);
9533 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9536 __isl_give isl_basic_map *isl_basic_map_normalize(
9537 __isl_take isl_basic_map *bmap)
9539 bmap = isl_basic_map_remove_redundancies(bmap);
9540 bmap = isl_basic_map_sort_constraints(bmap);
9541 return bmap;
9543 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9544 __isl_keep isl_basic_map *bmap2)
9546 int i, cmp;
9547 unsigned total;
9548 isl_space *space1, *space2;
9550 if (!bmap1 || !bmap2)
9551 return -1;
9553 if (bmap1 == bmap2)
9554 return 0;
9555 space1 = isl_basic_map_peek_space(bmap1);
9556 space2 = isl_basic_map_peek_space(bmap2);
9557 cmp = isl_space_cmp(space1, space2);
9558 if (cmp)
9559 return cmp;
9560 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9561 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9562 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9563 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9564 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9565 return 0;
9566 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9567 return 1;
9568 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9569 return -1;
9570 if (bmap1->n_eq != bmap2->n_eq)
9571 return bmap1->n_eq - bmap2->n_eq;
9572 if (bmap1->n_ineq != bmap2->n_ineq)
9573 return bmap1->n_ineq - bmap2->n_ineq;
9574 if (bmap1->n_div != bmap2->n_div)
9575 return bmap1->n_div - bmap2->n_div;
9576 total = isl_basic_map_total_dim(bmap1);
9577 for (i = 0; i < bmap1->n_eq; ++i) {
9578 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9579 if (cmp)
9580 return cmp;
9582 for (i = 0; i < bmap1->n_ineq; ++i) {
9583 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9584 if (cmp)
9585 return cmp;
9587 for (i = 0; i < bmap1->n_div; ++i) {
9588 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9589 if (cmp)
9590 return cmp;
9592 return 0;
9595 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9596 __isl_keep isl_basic_set *bset2)
9598 return isl_basic_map_plain_cmp(bset1, bset2);
9601 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9603 int i, cmp;
9605 if (set1 == set2)
9606 return 0;
9607 if (set1->n != set2->n)
9608 return set1->n - set2->n;
9610 for (i = 0; i < set1->n; ++i) {
9611 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9612 if (cmp)
9613 return cmp;
9616 return 0;
9619 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9620 __isl_keep isl_basic_map *bmap2)
9622 if (!bmap1 || !bmap2)
9623 return isl_bool_error;
9624 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9627 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9628 __isl_keep isl_basic_set *bset2)
9630 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9631 bset_to_bmap(bset2));
9634 static int qsort_bmap_cmp(const void *p1, const void *p2)
9636 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9637 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9639 return isl_basic_map_plain_cmp(bmap1, bmap2);
9642 /* Sort the basic maps of "map" and remove duplicate basic maps.
9644 * While removing basic maps, we make sure that the basic maps remain
9645 * sorted because isl_map_normalize expects the basic maps of the result
9646 * to be sorted.
9648 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9650 int i, j;
9652 map = isl_map_remove_empty_parts(map);
9653 if (!map)
9654 return NULL;
9655 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9656 for (i = map->n - 1; i >= 1; --i) {
9657 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9658 continue;
9659 isl_basic_map_free(map->p[i-1]);
9660 for (j = i; j < map->n; ++j)
9661 map->p[j - 1] = map->p[j];
9662 map->n--;
9665 return map;
9668 /* Remove obvious duplicates among the basic maps of "map".
9670 * Unlike isl_map_normalize, this function does not remove redundant
9671 * constraints and only removes duplicates that have exactly the same
9672 * constraints in the input. It does sort the constraints and
9673 * the basic maps to ease the detection of duplicates.
9675 * If "map" has already been normalized or if the basic maps are
9676 * disjoint, then there can be no duplicates.
9678 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9680 int i;
9681 isl_basic_map *bmap;
9683 if (!map)
9684 return NULL;
9685 if (map->n <= 1)
9686 return map;
9687 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9688 return map;
9689 for (i = 0; i < map->n; ++i) {
9690 bmap = isl_basic_map_copy(map->p[i]);
9691 bmap = isl_basic_map_sort_constraints(bmap);
9692 if (!bmap)
9693 return isl_map_free(map);
9694 isl_basic_map_free(map->p[i]);
9695 map->p[i] = bmap;
9698 map = sort_and_remove_duplicates(map);
9699 return map;
9702 /* We normalize in place, but if anything goes wrong we need
9703 * to return NULL, so we need to make sure we don't change the
9704 * meaning of any possible other copies of map.
9706 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9708 int i;
9709 struct isl_basic_map *bmap;
9711 if (!map)
9712 return NULL;
9713 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9714 return map;
9715 for (i = 0; i < map->n; ++i) {
9716 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9717 if (!bmap)
9718 goto error;
9719 isl_basic_map_free(map->p[i]);
9720 map->p[i] = bmap;
9723 map = sort_and_remove_duplicates(map);
9724 if (map)
9725 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9726 return map;
9727 error:
9728 isl_map_free(map);
9729 return NULL;
9732 struct isl_set *isl_set_normalize(struct isl_set *set)
9734 return set_from_map(isl_map_normalize(set_to_map(set)));
9737 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9738 __isl_keep isl_map *map2)
9740 int i;
9741 isl_bool equal;
9743 if (!map1 || !map2)
9744 return isl_bool_error;
9746 if (map1 == map2)
9747 return isl_bool_true;
9748 if (!isl_space_is_equal(map1->dim, map2->dim))
9749 return isl_bool_false;
9751 map1 = isl_map_copy(map1);
9752 map2 = isl_map_copy(map2);
9753 map1 = isl_map_normalize(map1);
9754 map2 = isl_map_normalize(map2);
9755 if (!map1 || !map2)
9756 goto error;
9757 equal = map1->n == map2->n;
9758 for (i = 0; equal && i < map1->n; ++i) {
9759 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9760 if (equal < 0)
9761 goto error;
9763 isl_map_free(map1);
9764 isl_map_free(map2);
9765 return equal;
9766 error:
9767 isl_map_free(map1);
9768 isl_map_free(map2);
9769 return isl_bool_error;
9772 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9773 __isl_keep isl_set *set2)
9775 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9778 /* Return the basic maps in "map" as a list.
9780 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9781 __isl_keep isl_map *map)
9783 int i;
9784 isl_ctx *ctx;
9785 isl_basic_map_list *list;
9787 if (!map)
9788 return NULL;
9789 ctx = isl_map_get_ctx(map);
9790 list = isl_basic_map_list_alloc(ctx, map->n);
9792 for (i = 0; i < map->n; ++i) {
9793 isl_basic_map *bmap;
9795 bmap = isl_basic_map_copy(map->p[i]);
9796 list = isl_basic_map_list_add(list, bmap);
9799 return list;
9802 /* Return the intersection of the elements in the non-empty list "list".
9803 * All elements are assumed to live in the same space.
9805 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9806 __isl_take isl_basic_map_list *list)
9808 int i, n;
9809 isl_basic_map *bmap;
9811 if (!list)
9812 return NULL;
9813 n = isl_basic_map_list_n_basic_map(list);
9814 if (n < 1)
9815 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9816 "expecting non-empty list", goto error);
9818 bmap = isl_basic_map_list_get_basic_map(list, 0);
9819 for (i = 1; i < n; ++i) {
9820 isl_basic_map *bmap_i;
9822 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9823 bmap = isl_basic_map_intersect(bmap, bmap_i);
9826 isl_basic_map_list_free(list);
9827 return bmap;
9828 error:
9829 isl_basic_map_list_free(list);
9830 return NULL;
9833 /* Return the intersection of the elements in the non-empty list "list".
9834 * All elements are assumed to live in the same space.
9836 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9837 __isl_take isl_basic_set_list *list)
9839 return isl_basic_map_list_intersect(list);
9842 /* Return the union of the elements of "list".
9843 * The list is required to have at least one element.
9845 __isl_give isl_set *isl_basic_set_list_union(
9846 __isl_take isl_basic_set_list *list)
9848 int i, n;
9849 isl_space *space;
9850 isl_basic_set *bset;
9851 isl_set *set;
9853 if (!list)
9854 return NULL;
9855 n = isl_basic_set_list_n_basic_set(list);
9856 if (n < 1)
9857 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9858 "expecting non-empty list", goto error);
9860 bset = isl_basic_set_list_get_basic_set(list, 0);
9861 space = isl_basic_set_get_space(bset);
9862 isl_basic_set_free(bset);
9864 set = isl_set_alloc_space(space, n, 0);
9865 for (i = 0; i < n; ++i) {
9866 bset = isl_basic_set_list_get_basic_set(list, i);
9867 set = isl_set_add_basic_set(set, bset);
9870 isl_basic_set_list_free(list);
9871 return set;
9872 error:
9873 isl_basic_set_list_free(list);
9874 return NULL;
9877 /* Return the union of the elements in the non-empty list "list".
9878 * All elements are assumed to live in the same space.
9880 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9882 int i, n;
9883 isl_set *set;
9885 if (!list)
9886 return NULL;
9887 n = isl_set_list_n_set(list);
9888 if (n < 1)
9889 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9890 "expecting non-empty list", goto error);
9892 set = isl_set_list_get_set(list, 0);
9893 for (i = 1; i < n; ++i) {
9894 isl_set *set_i;
9896 set_i = isl_set_list_get_set(list, i);
9897 set = isl_set_union(set, set_i);
9900 isl_set_list_free(list);
9901 return set;
9902 error:
9903 isl_set_list_free(list);
9904 return NULL;
9907 __isl_give isl_basic_map *isl_basic_map_product(
9908 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9910 isl_space *space_result = NULL;
9911 struct isl_basic_map *bmap;
9912 unsigned in1, in2, out1, out2, nparam, total, pos;
9913 struct isl_dim_map *dim_map1, *dim_map2;
9915 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9916 goto error;
9917 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9918 isl_space_copy(bmap2->dim));
9920 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9921 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9922 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9923 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9924 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9926 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9927 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9928 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9929 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9930 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9931 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9932 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9933 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9934 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9935 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9936 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9938 bmap = isl_basic_map_alloc_space(space_result,
9939 bmap1->n_div + bmap2->n_div,
9940 bmap1->n_eq + bmap2->n_eq,
9941 bmap1->n_ineq + bmap2->n_ineq);
9942 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9943 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9944 bmap = isl_basic_map_simplify(bmap);
9945 return isl_basic_map_finalize(bmap);
9946 error:
9947 isl_basic_map_free(bmap1);
9948 isl_basic_map_free(bmap2);
9949 return NULL;
9952 __isl_give isl_basic_map *isl_basic_map_flat_product(
9953 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9955 isl_basic_map *prod;
9957 prod = isl_basic_map_product(bmap1, bmap2);
9958 prod = isl_basic_map_flatten(prod);
9959 return prod;
9962 __isl_give isl_basic_set *isl_basic_set_flat_product(
9963 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9965 return isl_basic_map_flat_range_product(bset1, bset2);
9968 __isl_give isl_basic_map *isl_basic_map_domain_product(
9969 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9971 isl_space *space_result = NULL;
9972 isl_basic_map *bmap;
9973 unsigned in1, in2, out, nparam, total, pos;
9974 struct isl_dim_map *dim_map1, *dim_map2;
9976 if (!bmap1 || !bmap2)
9977 goto error;
9979 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9980 isl_space_copy(bmap2->dim));
9982 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9983 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9984 out = isl_basic_map_dim(bmap1, isl_dim_out);
9985 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9987 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9988 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9989 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9990 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9991 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9992 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9993 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9994 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9995 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9996 isl_dim_map_div(dim_map1, bmap1, pos += out);
9997 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9999 bmap = isl_basic_map_alloc_space(space_result,
10000 bmap1->n_div + bmap2->n_div,
10001 bmap1->n_eq + bmap2->n_eq,
10002 bmap1->n_ineq + bmap2->n_ineq);
10003 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10004 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10005 bmap = isl_basic_map_simplify(bmap);
10006 return isl_basic_map_finalize(bmap);
10007 error:
10008 isl_basic_map_free(bmap1);
10009 isl_basic_map_free(bmap2);
10010 return NULL;
10013 __isl_give isl_basic_map *isl_basic_map_range_product(
10014 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10016 isl_bool rational;
10017 isl_space *space_result = NULL;
10018 isl_basic_map *bmap;
10019 unsigned in, out1, out2, nparam, total, pos;
10020 struct isl_dim_map *dim_map1, *dim_map2;
10022 rational = isl_basic_map_is_rational(bmap1);
10023 if (rational >= 0 && rational)
10024 rational = isl_basic_map_is_rational(bmap2);
10025 if (!bmap1 || !bmap2 || rational < 0)
10026 goto error;
10028 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10029 goto error;
10031 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10032 isl_space_copy(bmap2->dim));
10034 in = isl_basic_map_dim(bmap1, isl_dim_in);
10035 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10036 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10037 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10039 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10040 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10041 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10042 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10043 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10044 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10045 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10048 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10049 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10051 bmap = isl_basic_map_alloc_space(space_result,
10052 bmap1->n_div + bmap2->n_div,
10053 bmap1->n_eq + bmap2->n_eq,
10054 bmap1->n_ineq + bmap2->n_ineq);
10055 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10056 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10057 if (rational)
10058 bmap = isl_basic_map_set_rational(bmap);
10059 bmap = isl_basic_map_simplify(bmap);
10060 return isl_basic_map_finalize(bmap);
10061 error:
10062 isl_basic_map_free(bmap1);
10063 isl_basic_map_free(bmap2);
10064 return NULL;
10067 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10068 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10070 isl_basic_map *prod;
10072 prod = isl_basic_map_range_product(bmap1, bmap2);
10073 prod = isl_basic_map_flatten_range(prod);
10074 return prod;
10077 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10078 * and collect the results.
10079 * The result live in the space obtained by calling "space_product"
10080 * on the spaces of "map1" and "map2".
10081 * If "remove_duplicates" is set then the result may contain duplicates
10082 * (even if the inputs do not) and so we try and remove the obvious
10083 * duplicates.
10085 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10086 __isl_take isl_map *map2,
10087 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10088 __isl_take isl_space *right),
10089 __isl_give isl_basic_map *(*basic_map_product)(
10090 __isl_take isl_basic_map *left,
10091 __isl_take isl_basic_map *right),
10092 int remove_duplicates)
10094 unsigned flags = 0;
10095 struct isl_map *result;
10096 int i, j;
10097 isl_bool m;
10099 m = isl_map_has_equal_params(map1, map2);
10100 if (m < 0)
10101 goto error;
10102 if (!m)
10103 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10104 "parameters don't match", goto error);
10106 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10107 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10108 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10110 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10111 isl_space_copy(map2->dim)),
10112 map1->n * map2->n, flags);
10113 if (!result)
10114 goto error;
10115 for (i = 0; i < map1->n; ++i)
10116 for (j = 0; j < map2->n; ++j) {
10117 struct isl_basic_map *part;
10118 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10119 isl_basic_map_copy(map2->p[j]));
10120 if (isl_basic_map_is_empty(part))
10121 isl_basic_map_free(part);
10122 else
10123 result = isl_map_add_basic_map(result, part);
10124 if (!result)
10125 goto error;
10127 if (remove_duplicates)
10128 result = isl_map_remove_obvious_duplicates(result);
10129 isl_map_free(map1);
10130 isl_map_free(map2);
10131 return result;
10132 error:
10133 isl_map_free(map1);
10134 isl_map_free(map2);
10135 return NULL;
10138 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10140 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10141 __isl_take isl_map *map2)
10143 return map_product(map1, map2, &isl_space_product,
10144 &isl_basic_map_product, 0);
10147 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10148 __isl_take isl_map *map2)
10150 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10153 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10155 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10156 __isl_take isl_map *map2)
10158 isl_map *prod;
10160 prod = isl_map_product(map1, map2);
10161 prod = isl_map_flatten(prod);
10162 return prod;
10165 /* Given two set A and B, construct its Cartesian product A x B.
10167 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10169 return isl_map_range_product(set1, set2);
10172 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10173 __isl_take isl_set *set2)
10175 return isl_map_flat_range_product(set1, set2);
10178 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10180 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10181 __isl_take isl_map *map2)
10183 return map_product(map1, map2, &isl_space_domain_product,
10184 &isl_basic_map_domain_product, 1);
10187 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10189 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10190 __isl_take isl_map *map2)
10192 return map_product(map1, map2, &isl_space_range_product,
10193 &isl_basic_map_range_product, 1);
10196 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10197 __isl_take isl_map *map2)
10199 return isl_map_align_params_map_map_and(map1, map2,
10200 &map_domain_product_aligned);
10203 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10204 __isl_take isl_map *map2)
10206 return isl_map_align_params_map_map_and(map1, map2,
10207 &map_range_product_aligned);
10210 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10212 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10214 isl_space *space;
10215 int total1, keep1, total2, keep2;
10217 if (!map)
10218 return NULL;
10219 if (!isl_space_domain_is_wrapping(map->dim) ||
10220 !isl_space_range_is_wrapping(map->dim))
10221 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10222 "not a product", return isl_map_free(map));
10224 space = isl_map_get_space(map);
10225 total1 = isl_space_dim(space, isl_dim_in);
10226 total2 = isl_space_dim(space, isl_dim_out);
10227 space = isl_space_factor_domain(space);
10228 keep1 = isl_space_dim(space, isl_dim_in);
10229 keep2 = isl_space_dim(space, isl_dim_out);
10230 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10231 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10232 map = isl_map_reset_space(map, space);
10234 return map;
10237 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10239 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10241 isl_space *space;
10242 int total1, keep1, total2, keep2;
10244 if (!map)
10245 return NULL;
10246 if (!isl_space_domain_is_wrapping(map->dim) ||
10247 !isl_space_range_is_wrapping(map->dim))
10248 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10249 "not a product", return isl_map_free(map));
10251 space = isl_map_get_space(map);
10252 total1 = isl_space_dim(space, isl_dim_in);
10253 total2 = isl_space_dim(space, isl_dim_out);
10254 space = isl_space_factor_range(space);
10255 keep1 = isl_space_dim(space, isl_dim_in);
10256 keep2 = isl_space_dim(space, isl_dim_out);
10257 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10258 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10259 map = isl_map_reset_space(map, space);
10261 return map;
10264 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10266 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10268 isl_space *space;
10269 int total, keep;
10271 if (!map)
10272 return NULL;
10273 if (!isl_space_domain_is_wrapping(map->dim))
10274 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10275 "domain is not a product", return isl_map_free(map));
10277 space = isl_map_get_space(map);
10278 total = isl_space_dim(space, isl_dim_in);
10279 space = isl_space_domain_factor_domain(space);
10280 keep = isl_space_dim(space, isl_dim_in);
10281 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10282 map = isl_map_reset_space(map, space);
10284 return map;
10287 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10289 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10291 isl_space *space;
10292 int total, keep;
10294 if (!map)
10295 return NULL;
10296 if (!isl_space_domain_is_wrapping(map->dim))
10297 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10298 "domain is not a product", return isl_map_free(map));
10300 space = isl_map_get_space(map);
10301 total = isl_space_dim(space, isl_dim_in);
10302 space = isl_space_domain_factor_range(space);
10303 keep = isl_space_dim(space, isl_dim_in);
10304 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10305 map = isl_map_reset_space(map, space);
10307 return map;
10310 /* Given a map A -> [B -> C], extract the map A -> B.
10312 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10314 isl_space *space;
10315 int total, keep;
10317 if (!map)
10318 return NULL;
10319 if (!isl_space_range_is_wrapping(map->dim))
10320 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10321 "range is not a product", return isl_map_free(map));
10323 space = isl_map_get_space(map);
10324 total = isl_space_dim(space, isl_dim_out);
10325 space = isl_space_range_factor_domain(space);
10326 keep = isl_space_dim(space, isl_dim_out);
10327 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10328 map = isl_map_reset_space(map, space);
10330 return map;
10333 /* Given a map A -> [B -> C], extract the map A -> C.
10335 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10337 isl_space *space;
10338 int total, keep;
10340 if (!map)
10341 return NULL;
10342 if (!isl_space_range_is_wrapping(map->dim))
10343 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10344 "range is not a product", return isl_map_free(map));
10346 space = isl_map_get_space(map);
10347 total = isl_space_dim(space, isl_dim_out);
10348 space = isl_space_range_factor_range(space);
10349 keep = isl_space_dim(space, isl_dim_out);
10350 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10351 map = isl_map_reset_space(map, space);
10353 return map;
10356 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10358 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10359 __isl_take isl_map *map2)
10361 isl_map *prod;
10363 prod = isl_map_domain_product(map1, map2);
10364 prod = isl_map_flatten_domain(prod);
10365 return prod;
10368 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10370 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10371 __isl_take isl_map *map2)
10373 isl_map *prod;
10375 prod = isl_map_range_product(map1, map2);
10376 prod = isl_map_flatten_range(prod);
10377 return prod;
10380 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10382 int i;
10383 uint32_t hash = isl_hash_init();
10384 unsigned total;
10386 if (!bmap)
10387 return 0;
10388 bmap = isl_basic_map_copy(bmap);
10389 bmap = isl_basic_map_normalize(bmap);
10390 if (!bmap)
10391 return 0;
10392 total = isl_basic_map_total_dim(bmap);
10393 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10394 for (i = 0; i < bmap->n_eq; ++i) {
10395 uint32_t c_hash;
10396 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10397 isl_hash_hash(hash, c_hash);
10399 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10400 for (i = 0; i < bmap->n_ineq; ++i) {
10401 uint32_t c_hash;
10402 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10403 isl_hash_hash(hash, c_hash);
10405 isl_hash_byte(hash, bmap->n_div & 0xFF);
10406 for (i = 0; i < bmap->n_div; ++i) {
10407 uint32_t c_hash;
10408 if (isl_int_is_zero(bmap->div[i][0]))
10409 continue;
10410 isl_hash_byte(hash, i & 0xFF);
10411 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10412 isl_hash_hash(hash, c_hash);
10414 isl_basic_map_free(bmap);
10415 return hash;
10418 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10420 return isl_basic_map_get_hash(bset_to_bmap(bset));
10423 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10425 int i;
10426 uint32_t hash;
10428 if (!map)
10429 return 0;
10430 map = isl_map_copy(map);
10431 map = isl_map_normalize(map);
10432 if (!map)
10433 return 0;
10435 hash = isl_hash_init();
10436 for (i = 0; i < map->n; ++i) {
10437 uint32_t bmap_hash;
10438 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10439 isl_hash_hash(hash, bmap_hash);
10442 isl_map_free(map);
10444 return hash;
10447 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10449 return isl_map_get_hash(set_to_map(set));
10452 /* Return the number of basic maps in the (current) representation of "map".
10454 int isl_map_n_basic_map(__isl_keep isl_map *map)
10456 return map ? map->n : 0;
10459 int isl_set_n_basic_set(__isl_keep isl_set *set)
10461 return set ? set->n : 0;
10464 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10465 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10467 int i;
10469 if (!map)
10470 return isl_stat_error;
10472 for (i = 0; i < map->n; ++i)
10473 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10474 return isl_stat_error;
10476 return isl_stat_ok;
10479 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10480 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10482 int i;
10484 if (!set)
10485 return isl_stat_error;
10487 for (i = 0; i < set->n; ++i)
10488 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10489 return isl_stat_error;
10491 return isl_stat_ok;
10494 /* Return a list of basic sets, the union of which is equal to "set".
10496 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10497 __isl_keep isl_set *set)
10499 int i;
10500 isl_basic_set_list *list;
10502 if (!set)
10503 return NULL;
10505 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10506 for (i = 0; i < set->n; ++i) {
10507 isl_basic_set *bset;
10509 bset = isl_basic_set_copy(set->p[i]);
10510 list = isl_basic_set_list_add(list, bset);
10513 return list;
10516 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10518 isl_space *space;
10520 if (!bset)
10521 return NULL;
10523 bset = isl_basic_set_cow(bset);
10524 if (!bset)
10525 return NULL;
10527 space = isl_basic_set_get_space(bset);
10528 space = isl_space_lift(space, bset->n_div);
10529 if (!space)
10530 goto error;
10531 isl_space_free(bset->dim);
10532 bset->dim = space;
10533 bset->extra -= bset->n_div;
10534 bset->n_div = 0;
10536 bset = isl_basic_set_finalize(bset);
10538 return bset;
10539 error:
10540 isl_basic_set_free(bset);
10541 return NULL;
10544 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10546 int i;
10547 isl_space *space;
10548 unsigned n_div;
10550 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10552 if (!set)
10553 return NULL;
10555 set = isl_set_cow(set);
10556 if (!set)
10557 return NULL;
10559 n_div = set->p[0]->n_div;
10560 space = isl_set_get_space(set);
10561 space = isl_space_lift(space, n_div);
10562 if (!space)
10563 goto error;
10564 isl_space_free(set->dim);
10565 set->dim = space;
10567 for (i = 0; i < set->n; ++i) {
10568 set->p[i] = isl_basic_set_lift(set->p[i]);
10569 if (!set->p[i])
10570 goto error;
10573 return set;
10574 error:
10575 isl_set_free(set);
10576 return NULL;
10579 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10581 unsigned dim;
10582 int size = 0;
10584 if (!bset)
10585 return -1;
10587 dim = isl_basic_set_total_dim(bset);
10588 size += bset->n_eq * (1 + dim);
10589 size += bset->n_ineq * (1 + dim);
10590 size += bset->n_div * (2 + dim);
10592 return size;
10595 int isl_set_size(__isl_keep isl_set *set)
10597 int i;
10598 int size = 0;
10600 if (!set)
10601 return -1;
10603 for (i = 0; i < set->n; ++i)
10604 size += isl_basic_set_size(set->p[i]);
10606 return size;
10609 /* Check if there is any lower bound (if lower == 0) and/or upper
10610 * bound (if upper == 0) on the specified dim.
10612 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10613 enum isl_dim_type type, unsigned pos, int lower, int upper)
10615 int i;
10617 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10618 return isl_bool_error;
10620 pos += isl_basic_map_offset(bmap, type);
10622 for (i = 0; i < bmap->n_div; ++i) {
10623 if (isl_int_is_zero(bmap->div[i][0]))
10624 continue;
10625 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10626 return isl_bool_true;
10629 for (i = 0; i < bmap->n_eq; ++i)
10630 if (!isl_int_is_zero(bmap->eq[i][pos]))
10631 return isl_bool_true;
10633 for (i = 0; i < bmap->n_ineq; ++i) {
10634 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10635 if (sgn > 0)
10636 lower = 1;
10637 if (sgn < 0)
10638 upper = 1;
10641 return lower && upper;
10644 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10645 enum isl_dim_type type, unsigned pos)
10647 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10650 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10651 enum isl_dim_type type, unsigned pos)
10653 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10656 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10657 enum isl_dim_type type, unsigned pos)
10659 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10662 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10663 enum isl_dim_type type, unsigned pos)
10665 int i;
10667 if (!map)
10668 return isl_bool_error;
10670 for (i = 0; i < map->n; ++i) {
10671 isl_bool bounded;
10672 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10673 if (bounded < 0 || !bounded)
10674 return bounded;
10677 return isl_bool_true;
10680 /* Return true if the specified dim is involved in both an upper bound
10681 * and a lower bound.
10683 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10684 enum isl_dim_type type, unsigned pos)
10686 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10689 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10691 static isl_bool has_any_bound(__isl_keep isl_map *map,
10692 enum isl_dim_type type, unsigned pos,
10693 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10694 enum isl_dim_type type, unsigned pos))
10696 int i;
10698 if (!map)
10699 return isl_bool_error;
10701 for (i = 0; i < map->n; ++i) {
10702 isl_bool bounded;
10703 bounded = fn(map->p[i], type, pos);
10704 if (bounded < 0 || bounded)
10705 return bounded;
10708 return isl_bool_false;
10711 /* Return 1 if the specified dim is involved in any lower bound.
10713 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10714 enum isl_dim_type type, unsigned pos)
10716 return has_any_bound(set, type, pos,
10717 &isl_basic_map_dim_has_lower_bound);
10720 /* Return 1 if the specified dim is involved in any upper bound.
10722 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10723 enum isl_dim_type type, unsigned pos)
10725 return has_any_bound(set, type, pos,
10726 &isl_basic_map_dim_has_upper_bound);
10729 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10731 static isl_bool has_bound(__isl_keep isl_map *map,
10732 enum isl_dim_type type, unsigned pos,
10733 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10734 enum isl_dim_type type, unsigned pos))
10736 int i;
10738 if (!map)
10739 return isl_bool_error;
10741 for (i = 0; i < map->n; ++i) {
10742 isl_bool bounded;
10743 bounded = fn(map->p[i], type, pos);
10744 if (bounded < 0 || !bounded)
10745 return bounded;
10748 return isl_bool_true;
10751 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10753 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10754 enum isl_dim_type type, unsigned pos)
10756 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10759 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10761 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10762 enum isl_dim_type type, unsigned pos)
10764 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10767 /* For each of the "n" variables starting at "first", determine
10768 * the sign of the variable and put the results in the first "n"
10769 * elements of the array "signs".
10770 * Sign
10771 * 1 means that the variable is non-negative
10772 * -1 means that the variable is non-positive
10773 * 0 means the variable attains both positive and negative values.
10775 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10776 unsigned first, unsigned n, int *signs)
10778 isl_vec *bound = NULL;
10779 struct isl_tab *tab = NULL;
10780 struct isl_tab_undo *snap;
10781 int i;
10783 if (!bset || !signs)
10784 return isl_stat_error;
10786 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10787 tab = isl_tab_from_basic_set(bset, 0);
10788 if (!bound || !tab)
10789 goto error;
10791 isl_seq_clr(bound->el, bound->size);
10792 isl_int_set_si(bound->el[0], -1);
10794 snap = isl_tab_snap(tab);
10795 for (i = 0; i < n; ++i) {
10796 int empty;
10798 isl_int_set_si(bound->el[1 + first + i], -1);
10799 if (isl_tab_add_ineq(tab, bound->el) < 0)
10800 goto error;
10801 empty = tab->empty;
10802 isl_int_set_si(bound->el[1 + first + i], 0);
10803 if (isl_tab_rollback(tab, snap) < 0)
10804 goto error;
10806 if (empty) {
10807 signs[i] = 1;
10808 continue;
10811 isl_int_set_si(bound->el[1 + first + i], 1);
10812 if (isl_tab_add_ineq(tab, bound->el) < 0)
10813 goto error;
10814 empty = tab->empty;
10815 isl_int_set_si(bound->el[1 + first + i], 0);
10816 if (isl_tab_rollback(tab, snap) < 0)
10817 goto error;
10819 signs[i] = empty ? -1 : 0;
10822 isl_tab_free(tab);
10823 isl_vec_free(bound);
10824 return isl_stat_ok;
10825 error:
10826 isl_tab_free(tab);
10827 isl_vec_free(bound);
10828 return isl_stat_error;
10831 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10832 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10834 if (!bset || !signs)
10835 return isl_stat_error;
10836 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10837 return isl_stat_error;
10839 first += pos(bset->dim, type) - 1;
10840 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10843 /* Is it possible for the integer division "div" to depend (possibly
10844 * indirectly) on any output dimensions?
10846 * If the div is undefined, then we conservatively assume that it
10847 * may depend on them.
10848 * Otherwise, we check if it actually depends on them or on any integer
10849 * divisions that may depend on them.
10851 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10853 int i;
10854 unsigned n_out, o_out;
10855 unsigned n_div, o_div;
10857 if (isl_int_is_zero(bmap->div[div][0]))
10858 return isl_bool_true;
10860 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10861 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10863 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10864 return isl_bool_true;
10866 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10867 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10869 for (i = 0; i < n_div; ++i) {
10870 isl_bool may_involve;
10872 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10873 continue;
10874 may_involve = div_may_involve_output(bmap, i);
10875 if (may_involve < 0 || may_involve)
10876 return may_involve;
10879 return isl_bool_false;
10882 /* Return the first integer division of "bmap" in the range
10883 * [first, first + n[ that may depend on any output dimensions and
10884 * that has a non-zero coefficient in "c" (where the first coefficient
10885 * in "c" corresponds to integer division "first").
10887 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10888 isl_int *c, int first, int n)
10890 int k;
10892 if (!bmap)
10893 return -1;
10895 for (k = first; k < first + n; ++k) {
10896 isl_bool may_involve;
10898 if (isl_int_is_zero(c[k]))
10899 continue;
10900 may_involve = div_may_involve_output(bmap, k);
10901 if (may_involve < 0)
10902 return -1;
10903 if (may_involve)
10904 return k;
10907 return first + n;
10910 /* Look for a pair of inequality constraints in "bmap" of the form
10912 * -l + i >= 0 or i >= l
10913 * and
10914 * n + l - i >= 0 or i <= l + n
10916 * with n < "m" and i the output dimension at position "pos".
10917 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10918 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10919 * and earlier output dimensions, as well as integer divisions that do
10920 * not involve any of the output dimensions.
10922 * Return the index of the first inequality constraint or bmap->n_ineq
10923 * if no such pair can be found.
10925 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10926 int pos, isl_int m)
10928 int i, j;
10929 isl_ctx *ctx;
10930 unsigned total;
10931 unsigned n_div, o_div;
10932 unsigned n_out, o_out;
10933 int less;
10935 if (!bmap)
10936 return -1;
10938 ctx = isl_basic_map_get_ctx(bmap);
10939 total = isl_basic_map_total_dim(bmap);
10940 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10941 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10942 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10943 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10944 for (i = 0; i < bmap->n_ineq; ++i) {
10945 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10946 continue;
10947 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10948 n_out - (pos + 1)) != -1)
10949 continue;
10950 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10951 0, n_div) < n_div)
10952 continue;
10953 for (j = i + 1; j < bmap->n_ineq; ++j) {
10954 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10955 ctx->one))
10956 continue;
10957 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10958 bmap->ineq[j] + 1, total))
10959 continue;
10960 break;
10962 if (j >= bmap->n_ineq)
10963 continue;
10964 isl_int_add(bmap->ineq[i][0],
10965 bmap->ineq[i][0], bmap->ineq[j][0]);
10966 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10967 isl_int_sub(bmap->ineq[i][0],
10968 bmap->ineq[i][0], bmap->ineq[j][0]);
10969 if (!less)
10970 continue;
10971 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10972 return i;
10973 else
10974 return j;
10977 return bmap->n_ineq;
10980 /* Return the index of the equality of "bmap" that defines
10981 * the output dimension "pos" in terms of earlier dimensions.
10982 * The equality may also involve integer divisions, as long
10983 * as those integer divisions are defined in terms of
10984 * parameters or input dimensions.
10985 * In this case, *div is set to the number of integer divisions and
10986 * *ineq is set to the number of inequality constraints (provided
10987 * div and ineq are not NULL).
10989 * The equality may also involve a single integer division involving
10990 * the output dimensions (typically only output dimension "pos") as
10991 * long as the coefficient of output dimension "pos" is 1 or -1 and
10992 * there is a pair of constraints i >= l and i <= l + n, with i referring
10993 * to output dimension "pos", l an expression involving only earlier
10994 * dimensions and n smaller than the coefficient of the integer division
10995 * in the equality. In this case, the output dimension can be defined
10996 * in terms of a modulo expression that does not involve the integer division.
10997 * *div is then set to this single integer division and
10998 * *ineq is set to the index of constraint i >= l.
11000 * Return bmap->n_eq if there is no such equality.
11001 * Return -1 on error.
11003 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11004 int pos, int *div, int *ineq)
11006 int j, k, l;
11007 unsigned n_out, o_out;
11008 unsigned n_div, o_div;
11010 if (!bmap)
11011 return -1;
11013 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11014 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11015 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11016 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11018 if (ineq)
11019 *ineq = bmap->n_ineq;
11020 if (div)
11021 *div = n_div;
11022 for (j = 0; j < bmap->n_eq; ++j) {
11023 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11024 continue;
11025 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11026 n_out - (pos + 1)) != -1)
11027 continue;
11028 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11029 0, n_div);
11030 if (k >= n_div)
11031 return j;
11032 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11033 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11034 continue;
11035 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11036 k + 1, n_div - (k+1)) < n_div)
11037 continue;
11038 l = find_modulo_constraint_pair(bmap, pos,
11039 bmap->eq[j][o_div + k]);
11040 if (l < 0)
11041 return -1;
11042 if (l >= bmap->n_ineq)
11043 continue;
11044 if (div)
11045 *div = k;
11046 if (ineq)
11047 *ineq = l;
11048 return j;
11051 return bmap->n_eq;
11054 /* Check if the given basic map is obviously single-valued.
11055 * In particular, for each output dimension, check that there is
11056 * an equality that defines the output dimension in terms of
11057 * earlier dimensions.
11059 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11061 int i;
11062 unsigned n_out;
11064 if (!bmap)
11065 return isl_bool_error;
11067 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11069 for (i = 0; i < n_out; ++i) {
11070 int eq;
11072 eq = isl_basic_map_output_defining_equality(bmap, i,
11073 NULL, NULL);
11074 if (eq < 0)
11075 return isl_bool_error;
11076 if (eq >= bmap->n_eq)
11077 return isl_bool_false;
11080 return isl_bool_true;
11083 /* Check if the given basic map is single-valued.
11084 * We simply compute
11086 * M \circ M^-1
11088 * and check if the result is a subset of the identity mapping.
11090 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11092 isl_space *space;
11093 isl_basic_map *test;
11094 isl_basic_map *id;
11095 isl_bool sv;
11097 sv = isl_basic_map_plain_is_single_valued(bmap);
11098 if (sv < 0 || sv)
11099 return sv;
11101 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11102 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11104 space = isl_basic_map_get_space(bmap);
11105 space = isl_space_map_from_set(isl_space_range(space));
11106 id = isl_basic_map_identity(space);
11108 sv = isl_basic_map_is_subset(test, id);
11110 isl_basic_map_free(test);
11111 isl_basic_map_free(id);
11113 return sv;
11116 /* Check if the given map is obviously single-valued.
11118 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11120 if (!map)
11121 return isl_bool_error;
11122 if (map->n == 0)
11123 return isl_bool_true;
11124 if (map->n >= 2)
11125 return isl_bool_false;
11127 return isl_basic_map_plain_is_single_valued(map->p[0]);
11130 /* Check if the given map is single-valued.
11131 * We simply compute
11133 * M \circ M^-1
11135 * and check if the result is a subset of the identity mapping.
11137 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11139 isl_space *dim;
11140 isl_map *test;
11141 isl_map *id;
11142 isl_bool sv;
11144 sv = isl_map_plain_is_single_valued(map);
11145 if (sv < 0 || sv)
11146 return sv;
11148 test = isl_map_reverse(isl_map_copy(map));
11149 test = isl_map_apply_range(test, isl_map_copy(map));
11151 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11152 id = isl_map_identity(dim);
11154 sv = isl_map_is_subset(test, id);
11156 isl_map_free(test);
11157 isl_map_free(id);
11159 return sv;
11162 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11164 isl_bool in;
11166 map = isl_map_copy(map);
11167 map = isl_map_reverse(map);
11168 in = isl_map_is_single_valued(map);
11169 isl_map_free(map);
11171 return in;
11174 /* Check if the given map is obviously injective.
11176 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11178 isl_bool in;
11180 map = isl_map_copy(map);
11181 map = isl_map_reverse(map);
11182 in = isl_map_plain_is_single_valued(map);
11183 isl_map_free(map);
11185 return in;
11188 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11190 isl_bool sv;
11192 sv = isl_map_is_single_valued(map);
11193 if (sv < 0 || !sv)
11194 return sv;
11196 return isl_map_is_injective(map);
11199 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11201 return isl_map_is_single_valued(set_to_map(set));
11204 /* Does "map" only map elements to themselves?
11206 * If the domain and range spaces are different, then "map"
11207 * is considered not to be an identity relation, even if it is empty.
11208 * Otherwise, construct the maximal identity relation and
11209 * check whether "map" is a subset of this relation.
11211 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11213 isl_space *space;
11214 isl_map *id;
11215 isl_bool equal, is_identity;
11217 space = isl_map_get_space(map);
11218 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11219 isl_space_free(space);
11220 if (equal < 0 || !equal)
11221 return equal;
11223 id = isl_map_identity(isl_map_get_space(map));
11224 is_identity = isl_map_is_subset(map, id);
11225 isl_map_free(id);
11227 return is_identity;
11230 int isl_map_is_translation(__isl_keep isl_map *map)
11232 int ok;
11233 isl_set *delta;
11235 delta = isl_map_deltas(isl_map_copy(map));
11236 ok = isl_set_is_singleton(delta);
11237 isl_set_free(delta);
11239 return ok;
11242 static int unique(isl_int *p, unsigned pos, unsigned len)
11244 if (isl_seq_first_non_zero(p, pos) != -1)
11245 return 0;
11246 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11247 return 0;
11248 return 1;
11251 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11253 int i, j;
11254 unsigned nvar;
11255 unsigned ovar;
11257 if (!bset)
11258 return isl_bool_error;
11260 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11261 return isl_bool_false;
11263 nvar = isl_basic_set_dim(bset, isl_dim_set);
11264 ovar = isl_space_offset(bset->dim, isl_dim_set);
11265 for (j = 0; j < nvar; ++j) {
11266 int lower = 0, upper = 0;
11267 for (i = 0; i < bset->n_eq; ++i) {
11268 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11269 continue;
11270 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11271 return isl_bool_false;
11272 break;
11274 if (i < bset->n_eq)
11275 continue;
11276 for (i = 0; i < bset->n_ineq; ++i) {
11277 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11278 continue;
11279 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11280 return isl_bool_false;
11281 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11282 lower = 1;
11283 else
11284 upper = 1;
11286 if (!lower || !upper)
11287 return isl_bool_false;
11290 return isl_bool_true;
11293 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11295 if (!set)
11296 return isl_bool_error;
11297 if (set->n != 1)
11298 return isl_bool_false;
11300 return isl_basic_set_is_box(set->p[0]);
11303 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11305 if (!bset)
11306 return isl_bool_error;
11308 return isl_space_is_wrapping(bset->dim);
11311 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11313 if (!set)
11314 return isl_bool_error;
11316 return isl_space_is_wrapping(set->dim);
11319 /* Modify the space of "map" through a call to "change".
11320 * If "can_change" is set (not NULL), then first call it to check
11321 * if the modification is allowed, printing the error message "cannot_change"
11322 * if it is not.
11324 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11325 isl_bool (*can_change)(__isl_keep isl_map *map),
11326 const char *cannot_change,
11327 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11329 isl_bool ok;
11330 isl_space *space;
11332 if (!map)
11333 return NULL;
11335 ok = can_change ? can_change(map) : isl_bool_true;
11336 if (ok < 0)
11337 return isl_map_free(map);
11338 if (!ok)
11339 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11340 return isl_map_free(map));
11342 space = change(isl_map_get_space(map));
11343 map = isl_map_reset_space(map, space);
11345 return map;
11348 /* Is the domain of "map" a wrapped relation?
11350 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11352 if (!map)
11353 return isl_bool_error;
11355 return isl_space_domain_is_wrapping(map->dim);
11358 /* Does "map" have a wrapped relation in both domain and range?
11360 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11362 return isl_space_is_product(isl_map_peek_space(map));
11365 /* Is the range of "map" a wrapped relation?
11367 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11369 if (!map)
11370 return isl_bool_error;
11372 return isl_space_range_is_wrapping(map->dim);
11375 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11377 bmap = isl_basic_map_cow(bmap);
11378 if (!bmap)
11379 return NULL;
11381 bmap->dim = isl_space_wrap(bmap->dim);
11382 if (!bmap->dim)
11383 goto error;
11385 bmap = isl_basic_map_finalize(bmap);
11387 return bset_from_bmap(bmap);
11388 error:
11389 isl_basic_map_free(bmap);
11390 return NULL;
11393 /* Given a map A -> B, return the set (A -> B).
11395 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11397 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11400 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11402 bset = isl_basic_set_cow(bset);
11403 if (!bset)
11404 return NULL;
11406 bset->dim = isl_space_unwrap(bset->dim);
11407 if (!bset->dim)
11408 goto error;
11410 bset = isl_basic_set_finalize(bset);
11412 return bset_to_bmap(bset);
11413 error:
11414 isl_basic_set_free(bset);
11415 return NULL;
11418 /* Given a set (A -> B), return the map A -> B.
11419 * Error out if "set" is not of the form (A -> B).
11421 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11423 return isl_map_change_space(set, &isl_set_is_wrapping,
11424 "not a wrapping set", &isl_space_unwrap);
11427 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11428 enum isl_dim_type type)
11430 if (!bmap)
11431 return NULL;
11433 if (!isl_space_is_named_or_nested(bmap->dim, type))
11434 return bmap;
11436 bmap = isl_basic_map_cow(bmap);
11437 if (!bmap)
11438 return NULL;
11440 bmap->dim = isl_space_reset(bmap->dim, type);
11441 if (!bmap->dim)
11442 goto error;
11444 bmap = isl_basic_map_finalize(bmap);
11446 return bmap;
11447 error:
11448 isl_basic_map_free(bmap);
11449 return NULL;
11452 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11453 enum isl_dim_type type)
11455 int i;
11457 if (!map)
11458 return NULL;
11460 if (!isl_space_is_named_or_nested(map->dim, type))
11461 return map;
11463 map = isl_map_cow(map);
11464 if (!map)
11465 return NULL;
11467 for (i = 0; i < map->n; ++i) {
11468 map->p[i] = isl_basic_map_reset(map->p[i], type);
11469 if (!map->p[i])
11470 goto error;
11472 map->dim = isl_space_reset(map->dim, type);
11473 if (!map->dim)
11474 goto error;
11476 return map;
11477 error:
11478 isl_map_free(map);
11479 return NULL;
11482 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11484 if (!bmap)
11485 return NULL;
11487 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11488 return bmap;
11490 bmap = isl_basic_map_cow(bmap);
11491 if (!bmap)
11492 return NULL;
11494 bmap->dim = isl_space_flatten(bmap->dim);
11495 if (!bmap->dim)
11496 goto error;
11498 bmap = isl_basic_map_finalize(bmap);
11500 return bmap;
11501 error:
11502 isl_basic_map_free(bmap);
11503 return NULL;
11506 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11508 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11511 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11512 __isl_take isl_basic_map *bmap)
11514 if (!bmap)
11515 return NULL;
11517 if (!bmap->dim->nested[0])
11518 return bmap;
11520 bmap = isl_basic_map_cow(bmap);
11521 if (!bmap)
11522 return NULL;
11524 bmap->dim = isl_space_flatten_domain(bmap->dim);
11525 if (!bmap->dim)
11526 goto error;
11528 bmap = isl_basic_map_finalize(bmap);
11530 return bmap;
11531 error:
11532 isl_basic_map_free(bmap);
11533 return NULL;
11536 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11537 __isl_take isl_basic_map *bmap)
11539 if (!bmap)
11540 return NULL;
11542 if (!bmap->dim->nested[1])
11543 return bmap;
11545 bmap = isl_basic_map_cow(bmap);
11546 if (!bmap)
11547 return NULL;
11549 bmap->dim = isl_space_flatten_range(bmap->dim);
11550 if (!bmap->dim)
11551 goto error;
11553 bmap = isl_basic_map_finalize(bmap);
11555 return bmap;
11556 error:
11557 isl_basic_map_free(bmap);
11558 return NULL;
11561 /* Remove any internal structure from the spaces of domain and range of "map".
11563 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11565 if (!map)
11566 return NULL;
11568 if (!map->dim->nested[0] && !map->dim->nested[1])
11569 return map;
11571 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11574 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11576 return set_from_map(isl_map_flatten(set_to_map(set)));
11579 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11581 isl_space *space, *flat_space;
11582 isl_map *map;
11584 space = isl_set_get_space(set);
11585 flat_space = isl_space_flatten(isl_space_copy(space));
11586 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11587 flat_space));
11588 map = isl_map_intersect_domain(map, set);
11590 return map;
11593 /* Remove any internal structure from the space of the domain of "map".
11595 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11597 if (!map)
11598 return NULL;
11600 if (!map->dim->nested[0])
11601 return map;
11603 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11606 /* Remove any internal structure from the space of the range of "map".
11608 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11610 if (!map)
11611 return NULL;
11613 if (!map->dim->nested[1])
11614 return map;
11616 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11619 /* Reorder the dimensions of "bmap" according to the given dim_map
11620 * and set the dimension specification to "space" and
11621 * perform Gaussian elimination on the result.
11623 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11624 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11626 isl_basic_map *res;
11627 unsigned flags;
11628 unsigned n_div;
11630 if (!bmap || !space || !dim_map)
11631 goto error;
11633 flags = bmap->flags;
11634 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11635 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11636 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11637 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11638 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11639 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11640 if (res)
11641 res->flags = flags;
11642 res = isl_basic_map_gauss(res, NULL);
11643 res = isl_basic_map_finalize(res);
11644 return res;
11645 error:
11646 isl_dim_map_free(dim_map);
11647 isl_basic_map_free(bmap);
11648 isl_space_free(space);
11649 return NULL;
11652 /* Reorder the dimensions of "map" according to given reordering.
11654 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11655 __isl_take isl_reordering *r)
11657 int i;
11658 struct isl_dim_map *dim_map;
11660 map = isl_map_cow(map);
11661 dim_map = isl_dim_map_from_reordering(r);
11662 if (!map || !r || !dim_map)
11663 goto error;
11665 for (i = 0; i < map->n; ++i) {
11666 struct isl_dim_map *dim_map_i;
11667 isl_space *space;
11669 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11671 space = isl_reordering_get_space(r);
11672 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11674 if (!map->p[i])
11675 goto error;
11678 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11679 map = isl_map_unmark_normalized(map);
11681 isl_reordering_free(r);
11682 isl_dim_map_free(dim_map);
11683 return map;
11684 error:
11685 isl_dim_map_free(dim_map);
11686 isl_map_free(map);
11687 isl_reordering_free(r);
11688 return NULL;
11691 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11692 __isl_take isl_reordering *r)
11694 return set_from_map(isl_map_realign(set_to_map(set), r));
11697 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11698 __isl_take isl_space *model)
11700 isl_ctx *ctx;
11701 isl_bool aligned;
11703 if (!map || !model)
11704 goto error;
11706 ctx = isl_space_get_ctx(model);
11707 if (!isl_space_has_named_params(model))
11708 isl_die(ctx, isl_error_invalid,
11709 "model has unnamed parameters", goto error);
11710 if (isl_map_check_named_params(map) < 0)
11711 goto error;
11712 aligned = isl_map_space_has_equal_params(map, model);
11713 if (aligned < 0)
11714 goto error;
11715 if (!aligned) {
11716 isl_reordering *exp;
11718 exp = isl_parameter_alignment_reordering(map->dim, model);
11719 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11720 map = isl_map_realign(map, exp);
11723 isl_space_free(model);
11724 return map;
11725 error:
11726 isl_space_free(model);
11727 isl_map_free(map);
11728 return NULL;
11731 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11732 __isl_take isl_space *model)
11734 return isl_map_align_params(set, model);
11737 /* Align the parameters of "bmap" to those of "model", introducing
11738 * additional parameters if needed.
11740 __isl_give isl_basic_map *isl_basic_map_align_params(
11741 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11743 isl_ctx *ctx;
11744 isl_bool equal_params;
11746 if (!bmap || !model)
11747 goto error;
11749 ctx = isl_space_get_ctx(model);
11750 if (!isl_space_has_named_params(model))
11751 isl_die(ctx, isl_error_invalid,
11752 "model has unnamed parameters", goto error);
11753 if (isl_basic_map_check_named_params(bmap) < 0)
11754 goto error;
11755 equal_params = isl_space_has_equal_params(bmap->dim, model);
11756 if (equal_params < 0)
11757 goto error;
11758 if (!equal_params) {
11759 isl_reordering *exp;
11760 struct isl_dim_map *dim_map;
11762 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11763 exp = isl_reordering_extend_space(exp,
11764 isl_basic_map_get_space(bmap));
11765 dim_map = isl_dim_map_from_reordering(exp);
11766 bmap = isl_basic_map_realign(bmap,
11767 isl_reordering_get_space(exp),
11768 isl_dim_map_extend(dim_map, bmap));
11769 isl_reordering_free(exp);
11770 isl_dim_map_free(dim_map);
11773 isl_space_free(model);
11774 return bmap;
11775 error:
11776 isl_space_free(model);
11777 isl_basic_map_free(bmap);
11778 return NULL;
11781 /* Do "bset" and "space" have the same parameters?
11783 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11784 __isl_keep isl_space *space)
11786 isl_space *bset_space;
11788 bset_space = isl_basic_set_peek_space(bset);
11789 return isl_space_has_equal_params(bset_space, space);
11792 /* Do "map" and "space" have the same parameters?
11794 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11795 __isl_keep isl_space *space)
11797 isl_space *map_space;
11799 map_space = isl_map_peek_space(map);
11800 return isl_space_has_equal_params(map_space, space);
11803 /* Do "set" and "space" have the same parameters?
11805 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11806 __isl_keep isl_space *space)
11808 return isl_map_space_has_equal_params(set_to_map(set), space);
11811 /* Align the parameters of "bset" to those of "model", introducing
11812 * additional parameters if needed.
11814 __isl_give isl_basic_set *isl_basic_set_align_params(
11815 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11817 return isl_basic_map_align_params(bset, model);
11820 /* Drop all parameters not referenced by "map".
11822 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11824 int i;
11826 if (isl_map_check_named_params(map) < 0)
11827 return isl_map_free(map);
11829 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11830 isl_bool involves;
11832 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11833 if (involves < 0)
11834 return isl_map_free(map);
11835 if (!involves)
11836 map = isl_map_project_out(map, isl_dim_param, i, 1);
11839 return map;
11842 /* Drop all parameters not referenced by "set".
11844 __isl_give isl_set *isl_set_drop_unused_params(
11845 __isl_take isl_set *set)
11847 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11850 /* Drop all parameters not referenced by "bmap".
11852 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11853 __isl_take isl_basic_map *bmap)
11855 int i;
11857 if (isl_basic_map_check_named_params(bmap) < 0)
11858 return isl_basic_map_free(bmap);
11860 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11861 isl_bool involves;
11863 involves = isl_basic_map_involves_dims(bmap,
11864 isl_dim_param, i, 1);
11865 if (involves < 0)
11866 return isl_basic_map_free(bmap);
11867 if (!involves)
11868 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11871 return bmap;
11874 /* Drop all parameters not referenced by "bset".
11876 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11877 __isl_take isl_basic_set *bset)
11879 return bset_from_bmap(isl_basic_map_drop_unused_params(
11880 bset_to_bmap(bset)));
11883 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11884 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11885 enum isl_dim_type c2, enum isl_dim_type c3,
11886 enum isl_dim_type c4, enum isl_dim_type c5)
11888 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11889 struct isl_mat *mat;
11890 int i, j, k;
11891 int pos;
11893 if (!bmap)
11894 return NULL;
11895 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11896 isl_basic_map_total_dim(bmap) + 1);
11897 if (!mat)
11898 return NULL;
11899 for (i = 0; i < bmap->n_eq; ++i)
11900 for (j = 0, pos = 0; j < 5; ++j) {
11901 int off = isl_basic_map_offset(bmap, c[j]);
11902 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11903 isl_int_set(mat->row[i][pos],
11904 bmap->eq[i][off + k]);
11905 ++pos;
11909 return mat;
11912 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11913 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11914 enum isl_dim_type c2, enum isl_dim_type c3,
11915 enum isl_dim_type c4, enum isl_dim_type c5)
11917 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11918 struct isl_mat *mat;
11919 int i, j, k;
11920 int pos;
11922 if (!bmap)
11923 return NULL;
11924 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11925 isl_basic_map_total_dim(bmap) + 1);
11926 if (!mat)
11927 return NULL;
11928 for (i = 0; i < bmap->n_ineq; ++i)
11929 for (j = 0, pos = 0; j < 5; ++j) {
11930 int off = isl_basic_map_offset(bmap, c[j]);
11931 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11932 isl_int_set(mat->row[i][pos],
11933 bmap->ineq[i][off + k]);
11934 ++pos;
11938 return mat;
11941 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11942 __isl_take isl_space *space,
11943 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11944 enum isl_dim_type c2, enum isl_dim_type c3,
11945 enum isl_dim_type c4, enum isl_dim_type c5)
11947 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11948 isl_basic_map *bmap = NULL;
11949 unsigned total;
11950 unsigned extra;
11951 int i, j, k, l;
11952 int pos;
11954 if (!space || !eq || !ineq)
11955 goto error;
11957 if (eq->n_col != ineq->n_col)
11958 isl_die(space->ctx, isl_error_invalid,
11959 "equalities and inequalities matrices should have "
11960 "same number of columns", goto error);
11962 total = 1 + isl_space_dim(space, isl_dim_all);
11964 if (eq->n_col < total)
11965 isl_die(space->ctx, isl_error_invalid,
11966 "number of columns too small", goto error);
11968 extra = eq->n_col - total;
11970 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11971 eq->n_row, ineq->n_row);
11972 if (!bmap)
11973 goto error;
11974 for (i = 0; i < extra; ++i) {
11975 k = isl_basic_map_alloc_div(bmap);
11976 if (k < 0)
11977 goto error;
11978 isl_int_set_si(bmap->div[k][0], 0);
11980 for (i = 0; i < eq->n_row; ++i) {
11981 l = isl_basic_map_alloc_equality(bmap);
11982 if (l < 0)
11983 goto error;
11984 for (j = 0, pos = 0; j < 5; ++j) {
11985 int off = isl_basic_map_offset(bmap, c[j]);
11986 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11987 isl_int_set(bmap->eq[l][off + k],
11988 eq->row[i][pos]);
11989 ++pos;
11993 for (i = 0; i < ineq->n_row; ++i) {
11994 l = isl_basic_map_alloc_inequality(bmap);
11995 if (l < 0)
11996 goto error;
11997 for (j = 0, pos = 0; j < 5; ++j) {
11998 int off = isl_basic_map_offset(bmap, c[j]);
11999 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
12000 isl_int_set(bmap->ineq[l][off + k],
12001 ineq->row[i][pos]);
12002 ++pos;
12007 isl_space_free(space);
12008 isl_mat_free(eq);
12009 isl_mat_free(ineq);
12011 bmap = isl_basic_map_simplify(bmap);
12012 return isl_basic_map_finalize(bmap);
12013 error:
12014 isl_space_free(space);
12015 isl_mat_free(eq);
12016 isl_mat_free(ineq);
12017 isl_basic_map_free(bmap);
12018 return NULL;
12021 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12022 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12023 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12025 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12026 c1, c2, c3, c4, isl_dim_in);
12029 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12030 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12031 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12033 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12034 c1, c2, c3, c4, isl_dim_in);
12037 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12038 __isl_take isl_space *dim,
12039 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12040 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12042 isl_basic_map *bmap;
12043 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12044 c1, c2, c3, c4, isl_dim_in);
12045 return bset_from_bmap(bmap);
12048 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12050 if (!bmap)
12051 return isl_bool_error;
12053 return isl_space_can_zip(bmap->dim);
12056 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12058 if (!map)
12059 return isl_bool_error;
12061 return isl_space_can_zip(map->dim);
12064 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12065 * (A -> C) -> (B -> D).
12067 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12069 unsigned pos;
12070 unsigned n1;
12071 unsigned n2;
12073 if (!bmap)
12074 return NULL;
12076 if (!isl_basic_map_can_zip(bmap))
12077 isl_die(bmap->ctx, isl_error_invalid,
12078 "basic map cannot be zipped", goto error);
12079 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12080 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12081 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12082 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12083 bmap = isl_basic_map_cow(bmap);
12084 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12085 if (!bmap)
12086 return NULL;
12087 bmap->dim = isl_space_zip(bmap->dim);
12088 if (!bmap->dim)
12089 goto error;
12090 bmap = isl_basic_map_mark_final(bmap);
12091 return bmap;
12092 error:
12093 isl_basic_map_free(bmap);
12094 return NULL;
12097 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12098 * (A -> C) -> (B -> D).
12100 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12102 int i;
12104 if (!map)
12105 return NULL;
12107 if (!isl_map_can_zip(map))
12108 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12109 goto error);
12111 map = isl_map_cow(map);
12112 if (!map)
12113 return NULL;
12115 for (i = 0; i < map->n; ++i) {
12116 map->p[i] = isl_basic_map_zip(map->p[i]);
12117 if (!map->p[i])
12118 goto error;
12121 map->dim = isl_space_zip(map->dim);
12122 if (!map->dim)
12123 goto error;
12125 return map;
12126 error:
12127 isl_map_free(map);
12128 return NULL;
12131 /* Can we apply isl_basic_map_curry to "bmap"?
12132 * That is, does it have a nested relation in its domain?
12134 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12136 if (!bmap)
12137 return isl_bool_error;
12139 return isl_space_can_curry(bmap->dim);
12142 /* Can we apply isl_map_curry to "map"?
12143 * That is, does it have a nested relation in its domain?
12145 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12147 if (!map)
12148 return isl_bool_error;
12150 return isl_space_can_curry(map->dim);
12153 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12154 * A -> (B -> C).
12156 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12159 if (!bmap)
12160 return NULL;
12162 if (!isl_basic_map_can_curry(bmap))
12163 isl_die(bmap->ctx, isl_error_invalid,
12164 "basic map cannot be curried", goto error);
12165 bmap = isl_basic_map_cow(bmap);
12166 if (!bmap)
12167 return NULL;
12168 bmap->dim = isl_space_curry(bmap->dim);
12169 if (!bmap->dim)
12170 goto error;
12171 bmap = isl_basic_map_mark_final(bmap);
12172 return bmap;
12173 error:
12174 isl_basic_map_free(bmap);
12175 return NULL;
12178 /* Given a map (A -> B) -> C, return the corresponding map
12179 * A -> (B -> C).
12181 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12183 return isl_map_change_space(map, &isl_map_can_curry,
12184 "map cannot be curried", &isl_space_curry);
12187 /* Can isl_map_range_curry be applied to "map"?
12188 * That is, does it have a nested relation in its range,
12189 * the domain of which is itself a nested relation?
12191 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12193 if (!map)
12194 return isl_bool_error;
12196 return isl_space_can_range_curry(map->dim);
12199 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12200 * A -> (B -> (C -> D)).
12202 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12204 return isl_map_change_space(map, &isl_map_can_range_curry,
12205 "map range cannot be curried",
12206 &isl_space_range_curry);
12209 /* Can we apply isl_basic_map_uncurry to "bmap"?
12210 * That is, does it have a nested relation in its domain?
12212 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12214 if (!bmap)
12215 return isl_bool_error;
12217 return isl_space_can_uncurry(bmap->dim);
12220 /* Can we apply isl_map_uncurry to "map"?
12221 * That is, does it have a nested relation in its domain?
12223 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12225 if (!map)
12226 return isl_bool_error;
12228 return isl_space_can_uncurry(map->dim);
12231 /* Given a basic map A -> (B -> C), return the corresponding basic map
12232 * (A -> B) -> C.
12234 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12237 if (!bmap)
12238 return NULL;
12240 if (!isl_basic_map_can_uncurry(bmap))
12241 isl_die(bmap->ctx, isl_error_invalid,
12242 "basic map cannot be uncurried",
12243 return isl_basic_map_free(bmap));
12244 bmap = isl_basic_map_cow(bmap);
12245 if (!bmap)
12246 return NULL;
12247 bmap->dim = isl_space_uncurry(bmap->dim);
12248 if (!bmap->dim)
12249 return isl_basic_map_free(bmap);
12250 bmap = isl_basic_map_mark_final(bmap);
12251 return bmap;
12254 /* Given a map A -> (B -> C), return the corresponding map
12255 * (A -> B) -> C.
12257 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12259 return isl_map_change_space(map, &isl_map_can_uncurry,
12260 "map cannot be uncurried", &isl_space_uncurry);
12263 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12264 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12266 return isl_map_equate(set, type1, pos1, type2, pos2);
12269 /* Construct a basic map where the given dimensions are equal to each other.
12271 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12272 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12274 isl_basic_map *bmap = NULL;
12275 int i;
12277 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12278 isl_space_check_range(space, type2, pos2, 1) < 0)
12279 goto error;
12281 if (type1 == type2 && pos1 == pos2)
12282 return isl_basic_map_universe(space);
12284 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12285 i = isl_basic_map_alloc_equality(bmap);
12286 if (i < 0)
12287 goto error;
12288 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12289 pos1 += isl_basic_map_offset(bmap, type1);
12290 pos2 += isl_basic_map_offset(bmap, type2);
12291 isl_int_set_si(bmap->eq[i][pos1], -1);
12292 isl_int_set_si(bmap->eq[i][pos2], 1);
12293 bmap = isl_basic_map_finalize(bmap);
12294 isl_space_free(space);
12295 return bmap;
12296 error:
12297 isl_space_free(space);
12298 isl_basic_map_free(bmap);
12299 return NULL;
12302 /* Add a constraint imposing that the given two dimensions are equal.
12304 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12305 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12307 isl_basic_map *eq;
12309 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12311 bmap = isl_basic_map_intersect(bmap, eq);
12313 return bmap;
12316 /* Add a constraint imposing that the given two dimensions are equal.
12318 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12319 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12321 isl_basic_map *bmap;
12323 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12325 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12327 return map;
12330 /* Add a constraint imposing that the given two dimensions have opposite values.
12332 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12333 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12335 isl_basic_map *bmap = NULL;
12336 int i;
12338 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12339 return isl_map_free(map);
12340 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12341 return isl_map_free(map);
12343 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12344 i = isl_basic_map_alloc_equality(bmap);
12345 if (i < 0)
12346 goto error;
12347 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12348 pos1 += isl_basic_map_offset(bmap, type1);
12349 pos2 += isl_basic_map_offset(bmap, type2);
12350 isl_int_set_si(bmap->eq[i][pos1], 1);
12351 isl_int_set_si(bmap->eq[i][pos2], 1);
12352 bmap = isl_basic_map_finalize(bmap);
12354 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12356 return map;
12357 error:
12358 isl_basic_map_free(bmap);
12359 isl_map_free(map);
12360 return NULL;
12363 /* Construct a constraint imposing that the value of the first dimension is
12364 * greater than or equal to that of the second.
12366 static __isl_give isl_constraint *constraint_order_ge(
12367 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12368 enum isl_dim_type type2, int pos2)
12370 isl_constraint *c;
12372 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12373 isl_space_check_range(space, type2, pos2, 1) < 0)
12374 space = isl_space_free(space);
12375 if (!space)
12376 return NULL;
12378 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12380 if (type1 == type2 && pos1 == pos2)
12381 return c;
12383 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12384 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12386 return c;
12389 /* Add a constraint imposing that the value of the first dimension is
12390 * greater than or equal to that of the second.
12392 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12393 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12395 isl_constraint *c;
12396 isl_space *space;
12398 if (type1 == type2 && pos1 == pos2)
12399 return bmap;
12400 space = isl_basic_map_get_space(bmap);
12401 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12402 bmap = isl_basic_map_add_constraint(bmap, c);
12404 return bmap;
12407 /* Add a constraint imposing that the value of the first dimension is
12408 * greater than or equal to that of the second.
12410 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12411 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12413 isl_constraint *c;
12414 isl_space *space;
12416 if (type1 == type2 && pos1 == pos2)
12417 return map;
12418 space = isl_map_get_space(map);
12419 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12420 map = isl_map_add_constraint(map, c);
12422 return map;
12425 /* Add a constraint imposing that the value of the first dimension is
12426 * less than or equal to that of the second.
12428 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12429 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12431 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12434 /* Construct a basic map where the value of the first dimension is
12435 * greater than that of the second.
12437 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12438 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12440 isl_basic_map *bmap = NULL;
12441 int i;
12443 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12444 isl_space_check_range(space, type2, pos2, 1) < 0)
12445 goto error;
12447 if (type1 == type2 && pos1 == pos2)
12448 return isl_basic_map_empty(space);
12450 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12451 i = isl_basic_map_alloc_inequality(bmap);
12452 if (i < 0)
12453 return isl_basic_map_free(bmap);
12454 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12455 pos1 += isl_basic_map_offset(bmap, type1);
12456 pos2 += isl_basic_map_offset(bmap, type2);
12457 isl_int_set_si(bmap->ineq[i][pos1], 1);
12458 isl_int_set_si(bmap->ineq[i][pos2], -1);
12459 isl_int_set_si(bmap->ineq[i][0], -1);
12460 bmap = isl_basic_map_finalize(bmap);
12462 return bmap;
12463 error:
12464 isl_space_free(space);
12465 isl_basic_map_free(bmap);
12466 return NULL;
12469 /* Add a constraint imposing that the value of the first dimension is
12470 * greater than that of the second.
12472 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12473 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12475 isl_basic_map *gt;
12477 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12479 bmap = isl_basic_map_intersect(bmap, gt);
12481 return bmap;
12484 /* Add a constraint imposing that the value of the first dimension is
12485 * greater than that of the second.
12487 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12488 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12490 isl_basic_map *bmap;
12492 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12494 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12496 return map;
12499 /* Add a constraint imposing that the value of the first dimension is
12500 * smaller than that of the second.
12502 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12503 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12505 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12508 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12509 int pos)
12511 isl_aff *div;
12512 isl_local_space *ls;
12514 if (!bmap)
12515 return NULL;
12517 if (!isl_basic_map_divs_known(bmap))
12518 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12519 "some divs are unknown", return NULL);
12521 ls = isl_basic_map_get_local_space(bmap);
12522 div = isl_local_space_get_div(ls, pos);
12523 isl_local_space_free(ls);
12525 return div;
12528 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12529 int pos)
12531 return isl_basic_map_get_div(bset, pos);
12534 /* Plug in "subs" for dimension "type", "pos" of "bset".
12536 * Let i be the dimension to replace and let "subs" be of the form
12538 * f/d
12540 * Any integer division with a non-zero coefficient for i,
12542 * floor((a i + g)/m)
12544 * is replaced by
12546 * floor((a f + d g)/(m d))
12548 * Constraints of the form
12550 * a i + g
12552 * are replaced by
12554 * a f + d g
12556 * We currently require that "subs" is an integral expression.
12557 * Handling rational expressions may require us to add stride constraints
12558 * as we do in isl_basic_set_preimage_multi_aff.
12560 __isl_give isl_basic_set *isl_basic_set_substitute(
12561 __isl_take isl_basic_set *bset,
12562 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12564 int i;
12565 isl_int v;
12566 isl_ctx *ctx;
12568 if (bset && isl_basic_set_plain_is_empty(bset))
12569 return bset;
12571 bset = isl_basic_set_cow(bset);
12572 if (!bset || !subs)
12573 goto error;
12575 ctx = isl_basic_set_get_ctx(bset);
12576 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12577 isl_die(ctx, isl_error_invalid,
12578 "spaces don't match", goto error);
12579 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12580 isl_die(ctx, isl_error_unsupported,
12581 "cannot handle divs yet", goto error);
12582 if (!isl_int_is_one(subs->v->el[0]))
12583 isl_die(ctx, isl_error_invalid,
12584 "can only substitute integer expressions", goto error);
12586 pos += isl_basic_set_offset(bset, type);
12588 isl_int_init(v);
12590 for (i = 0; i < bset->n_eq; ++i) {
12591 if (isl_int_is_zero(bset->eq[i][pos]))
12592 continue;
12593 isl_int_set(v, bset->eq[i][pos]);
12594 isl_int_set_si(bset->eq[i][pos], 0);
12595 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12596 v, subs->v->el + 1, subs->v->size - 1);
12599 for (i = 0; i < bset->n_ineq; ++i) {
12600 if (isl_int_is_zero(bset->ineq[i][pos]))
12601 continue;
12602 isl_int_set(v, bset->ineq[i][pos]);
12603 isl_int_set_si(bset->ineq[i][pos], 0);
12604 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12605 v, subs->v->el + 1, subs->v->size - 1);
12608 for (i = 0; i < bset->n_div; ++i) {
12609 if (isl_int_is_zero(bset->div[i][1 + pos]))
12610 continue;
12611 isl_int_set(v, bset->div[i][1 + pos]);
12612 isl_int_set_si(bset->div[i][1 + pos], 0);
12613 isl_seq_combine(bset->div[i] + 1,
12614 subs->v->el[0], bset->div[i] + 1,
12615 v, subs->v->el + 1, subs->v->size - 1);
12616 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12619 isl_int_clear(v);
12621 bset = isl_basic_set_simplify(bset);
12622 return isl_basic_set_finalize(bset);
12623 error:
12624 isl_basic_set_free(bset);
12625 return NULL;
12628 /* Plug in "subs" for dimension "type", "pos" of "set".
12630 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12631 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12633 int i;
12635 if (set && isl_set_plain_is_empty(set))
12636 return set;
12638 set = isl_set_cow(set);
12639 if (!set || !subs)
12640 goto error;
12642 for (i = set->n - 1; i >= 0; --i) {
12643 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12644 set = set_from_map(remove_if_empty(set_to_map(set), i));
12645 if (!set)
12646 return NULL;
12649 return set;
12650 error:
12651 isl_set_free(set);
12652 return NULL;
12655 /* Check if the range of "ma" is compatible with the domain or range
12656 * (depending on "type") of "bmap".
12658 static isl_stat check_basic_map_compatible_range_multi_aff(
12659 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12660 __isl_keep isl_multi_aff *ma)
12662 isl_bool m;
12663 isl_space *ma_space;
12665 ma_space = isl_multi_aff_get_space(ma);
12667 m = isl_space_has_equal_params(bmap->dim, ma_space);
12668 if (m < 0)
12669 goto error;
12670 if (!m)
12671 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12672 "parameters don't match", goto error);
12673 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12674 if (m < 0)
12675 goto error;
12676 if (!m)
12677 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12678 "spaces don't match", goto error);
12680 isl_space_free(ma_space);
12681 return isl_stat_ok;
12682 error:
12683 isl_space_free(ma_space);
12684 return isl_stat_error;
12687 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12688 * coefficients before the transformed range of dimensions,
12689 * the "n_after" coefficients after the transformed range of dimensions
12690 * and the coefficients of the other divs in "bmap".
12692 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12693 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12695 int i;
12696 int n_param;
12697 int n_set;
12698 isl_local_space *ls;
12700 if (n_div == 0)
12701 return 0;
12703 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12704 if (!ls)
12705 return -1;
12707 n_param = isl_local_space_dim(ls, isl_dim_param);
12708 n_set = isl_local_space_dim(ls, isl_dim_set);
12709 for (i = 0; i < n_div; ++i) {
12710 int o_bmap = 0, o_ls = 0;
12712 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12713 o_bmap += 1 + 1 + n_param;
12714 o_ls += 1 + 1 + n_param;
12715 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12716 o_bmap += n_before;
12717 isl_seq_cpy(bmap->div[i] + o_bmap,
12718 ls->div->row[i] + o_ls, n_set);
12719 o_bmap += n_set;
12720 o_ls += n_set;
12721 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12722 o_bmap += n_after;
12723 isl_seq_cpy(bmap->div[i] + o_bmap,
12724 ls->div->row[i] + o_ls, n_div);
12725 o_bmap += n_div;
12726 o_ls += n_div;
12727 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12728 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12729 goto error;
12732 isl_local_space_free(ls);
12733 return 0;
12734 error:
12735 isl_local_space_free(ls);
12736 return -1;
12739 /* How many stride constraints does "ma" enforce?
12740 * That is, how many of the affine expressions have a denominator
12741 * different from one?
12743 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12745 int i;
12746 int strides = 0;
12748 for (i = 0; i < ma->n; ++i)
12749 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12750 strides++;
12752 return strides;
12755 /* For each affine expression in ma of the form
12757 * x_i = (f_i y + h_i)/m_i
12759 * with m_i different from one, add a constraint to "bmap"
12760 * of the form
12762 * f_i y + h_i = m_i alpha_i
12764 * with alpha_i an additional existentially quantified variable.
12766 * The input variables of "ma" correspond to a subset of the variables
12767 * of "bmap". There are "n_before" variables in "bmap" before this
12768 * subset and "n_after" variables after this subset.
12769 * The integer divisions of the affine expressions in "ma" are assumed
12770 * to have been aligned. There are "n_div_ma" of them and
12771 * they appear first in "bmap", straight after the "n_after" variables.
12773 static __isl_give isl_basic_map *add_ma_strides(
12774 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12775 int n_before, int n_after, int n_div_ma)
12777 int i, k;
12778 int div;
12779 int total;
12780 int n_param;
12781 int n_in;
12783 total = isl_basic_map_total_dim(bmap);
12784 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12785 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12786 for (i = 0; i < ma->n; ++i) {
12787 int o_bmap = 0, o_ma = 1;
12789 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12790 continue;
12791 div = isl_basic_map_alloc_div(bmap);
12792 k = isl_basic_map_alloc_equality(bmap);
12793 if (div < 0 || k < 0)
12794 goto error;
12795 isl_int_set_si(bmap->div[div][0], 0);
12796 isl_seq_cpy(bmap->eq[k] + o_bmap,
12797 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12798 o_bmap += 1 + n_param;
12799 o_ma += 1 + n_param;
12800 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12801 o_bmap += n_before;
12802 isl_seq_cpy(bmap->eq[k] + o_bmap,
12803 ma->u.p[i]->v->el + o_ma, n_in);
12804 o_bmap += n_in;
12805 o_ma += n_in;
12806 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12807 o_bmap += n_after;
12808 isl_seq_cpy(bmap->eq[k] + o_bmap,
12809 ma->u.p[i]->v->el + o_ma, n_div_ma);
12810 o_bmap += n_div_ma;
12811 o_ma += n_div_ma;
12812 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12813 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12814 total++;
12817 return bmap;
12818 error:
12819 isl_basic_map_free(bmap);
12820 return NULL;
12823 /* Replace the domain or range space (depending on "type) of "space" by "set".
12825 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12826 enum isl_dim_type type, __isl_take isl_space *set)
12828 if (type == isl_dim_in) {
12829 space = isl_space_range(space);
12830 space = isl_space_map_from_domain_and_range(set, space);
12831 } else {
12832 space = isl_space_domain(space);
12833 space = isl_space_map_from_domain_and_range(space, set);
12836 return space;
12839 /* Compute the preimage of the domain or range (depending on "type")
12840 * of "bmap" under the function represented by "ma".
12841 * In other words, plug in "ma" in the domain or range of "bmap".
12842 * The result is a basic map that lives in the same space as "bmap"
12843 * except that the domain or range has been replaced by
12844 * the domain space of "ma".
12846 * If bmap is represented by
12848 * A(p) + S u + B x + T v + C(divs) >= 0,
12850 * where u and x are input and output dimensions if type == isl_dim_out
12851 * while x and v are input and output dimensions if type == isl_dim_in,
12852 * and ma is represented by
12854 * x = D(p) + F(y) + G(divs')
12856 * then the result is
12858 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12860 * The divs in the input set are similarly adjusted.
12861 * In particular
12863 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12865 * becomes
12867 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12868 * B_i G(divs') + c_i(divs))/n_i)
12870 * If bmap is not a rational map and if F(y) involves any denominators
12872 * x_i = (f_i y + h_i)/m_i
12874 * then additional constraints are added to ensure that we only
12875 * map back integer points. That is we enforce
12877 * f_i y + h_i = m_i alpha_i
12879 * with alpha_i an additional existentially quantified variable.
12881 * We first copy over the divs from "ma".
12882 * Then we add the modified constraints and divs from "bmap".
12883 * Finally, we add the stride constraints, if needed.
12885 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12886 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12887 __isl_take isl_multi_aff *ma)
12889 int i, k;
12890 isl_space *space;
12891 isl_basic_map *res = NULL;
12892 int n_before, n_after, n_div_bmap, n_div_ma;
12893 isl_int f, c1, c2, g;
12894 isl_bool rational;
12895 int strides;
12897 isl_int_init(f);
12898 isl_int_init(c1);
12899 isl_int_init(c2);
12900 isl_int_init(g);
12902 ma = isl_multi_aff_align_divs(ma);
12903 if (!bmap || !ma)
12904 goto error;
12905 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12906 goto error;
12908 if (type == isl_dim_in) {
12909 n_before = 0;
12910 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12911 } else {
12912 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12913 n_after = 0;
12915 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12916 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12918 space = isl_multi_aff_get_domain_space(ma);
12919 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12920 rational = isl_basic_map_is_rational(bmap);
12921 strides = rational ? 0 : multi_aff_strides(ma);
12922 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12923 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12924 if (rational)
12925 res = isl_basic_map_set_rational(res);
12927 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12928 if (isl_basic_map_alloc_div(res) < 0)
12929 goto error;
12931 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12932 goto error;
12934 for (i = 0; i < bmap->n_eq; ++i) {
12935 k = isl_basic_map_alloc_equality(res);
12936 if (k < 0)
12937 goto error;
12938 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12939 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12942 for (i = 0; i < bmap->n_ineq; ++i) {
12943 k = isl_basic_map_alloc_inequality(res);
12944 if (k < 0)
12945 goto error;
12946 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12947 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12950 for (i = 0; i < bmap->n_div; ++i) {
12951 if (isl_int_is_zero(bmap->div[i][0])) {
12952 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12953 continue;
12955 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12956 n_before, n_after, n_div_ma, n_div_bmap,
12957 f, c1, c2, g, 1);
12960 if (strides)
12961 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12963 isl_int_clear(f);
12964 isl_int_clear(c1);
12965 isl_int_clear(c2);
12966 isl_int_clear(g);
12967 isl_basic_map_free(bmap);
12968 isl_multi_aff_free(ma);
12969 res = isl_basic_map_simplify(res);
12970 return isl_basic_map_finalize(res);
12971 error:
12972 isl_int_clear(f);
12973 isl_int_clear(c1);
12974 isl_int_clear(c2);
12975 isl_int_clear(g);
12976 isl_basic_map_free(bmap);
12977 isl_multi_aff_free(ma);
12978 isl_basic_map_free(res);
12979 return NULL;
12982 /* Compute the preimage of "bset" under the function represented by "ma".
12983 * In other words, plug in "ma" in "bset". The result is a basic set
12984 * that lives in the domain space of "ma".
12986 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12987 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12989 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12992 /* Compute the preimage of the domain of "bmap" under the function
12993 * represented by "ma".
12994 * In other words, plug in "ma" in the domain of "bmap".
12995 * The result is a basic map that lives in the same space as "bmap"
12996 * except that the domain has been replaced by the domain space of "ma".
12998 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12999 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13001 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13004 /* Compute the preimage of the range of "bmap" under the function
13005 * represented by "ma".
13006 * In other words, plug in "ma" in the range of "bmap".
13007 * The result is a basic map that lives in the same space as "bmap"
13008 * except that the range has been replaced by the domain space of "ma".
13010 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13011 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13013 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13016 /* Check if the range of "ma" is compatible with the domain or range
13017 * (depending on "type") of "map".
13018 * Return isl_stat_error if anything is wrong.
13020 static isl_stat check_map_compatible_range_multi_aff(
13021 __isl_keep isl_map *map, enum isl_dim_type type,
13022 __isl_keep isl_multi_aff *ma)
13024 isl_bool m;
13025 isl_space *ma_space;
13027 ma_space = isl_multi_aff_get_space(ma);
13028 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13029 isl_space_free(ma_space);
13030 if (m < 0)
13031 return isl_stat_error;
13032 if (!m)
13033 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13034 "spaces don't match", return isl_stat_error);
13035 return isl_stat_ok;
13038 /* Compute the preimage of the domain or range (depending on "type")
13039 * of "map" under the function represented by "ma".
13040 * In other words, plug in "ma" in the domain or range of "map".
13041 * The result is a map that lives in the same space as "map"
13042 * except that the domain or range has been replaced by
13043 * the domain space of "ma".
13045 * The parameters are assumed to have been aligned.
13047 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13048 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13050 int i;
13051 isl_space *space;
13053 map = isl_map_cow(map);
13054 ma = isl_multi_aff_align_divs(ma);
13055 if (!map || !ma)
13056 goto error;
13057 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13058 goto error;
13060 for (i = 0; i < map->n; ++i) {
13061 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13062 isl_multi_aff_copy(ma));
13063 if (!map->p[i])
13064 goto error;
13067 space = isl_multi_aff_get_domain_space(ma);
13068 space = isl_space_set(isl_map_get_space(map), type, space);
13070 isl_space_free(map->dim);
13071 map->dim = space;
13072 if (!map->dim)
13073 goto error;
13075 isl_multi_aff_free(ma);
13076 if (map->n > 1)
13077 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13078 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13079 return map;
13080 error:
13081 isl_multi_aff_free(ma);
13082 isl_map_free(map);
13083 return NULL;
13086 /* Compute the preimage of the domain or range (depending on "type")
13087 * of "map" under the function represented by "ma".
13088 * In other words, plug in "ma" in the domain or range of "map".
13089 * The result is a map that lives in the same space as "map"
13090 * except that the domain or range has been replaced by
13091 * the domain space of "ma".
13093 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13094 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13096 isl_bool aligned;
13098 if (!map || !ma)
13099 goto error;
13101 aligned = isl_map_space_has_equal_params(map, ma->space);
13102 if (aligned < 0)
13103 goto error;
13104 if (aligned)
13105 return map_preimage_multi_aff(map, type, ma);
13107 if (isl_map_check_named_params(map) < 0)
13108 goto error;
13109 if (!isl_space_has_named_params(ma->space))
13110 isl_die(map->ctx, isl_error_invalid,
13111 "unaligned unnamed parameters", goto error);
13112 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13113 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13115 return map_preimage_multi_aff(map, type, ma);
13116 error:
13117 isl_multi_aff_free(ma);
13118 return isl_map_free(map);
13121 /* Compute the preimage of "set" under the function represented by "ma".
13122 * In other words, plug in "ma" in "set". The result is a set
13123 * that lives in the domain space of "ma".
13125 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13126 __isl_take isl_multi_aff *ma)
13128 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13131 /* Compute the preimage of the domain of "map" under the function
13132 * represented by "ma".
13133 * In other words, plug in "ma" in the domain of "map".
13134 * The result is a map that lives in the same space as "map"
13135 * except that the domain has been replaced by the domain space of "ma".
13137 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13138 __isl_take isl_multi_aff *ma)
13140 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13143 /* Compute the preimage of the range of "map" under the function
13144 * represented by "ma".
13145 * In other words, plug in "ma" in the range of "map".
13146 * The result is a map that lives in the same space as "map"
13147 * except that the range has been replaced by the domain space of "ma".
13149 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13150 __isl_take isl_multi_aff *ma)
13152 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13155 /* Compute the preimage of "map" under the function represented by "pma".
13156 * In other words, plug in "pma" in the domain or range of "map".
13157 * The result is a map that lives in the same space as "map",
13158 * except that the space of type "type" has been replaced by
13159 * the domain space of "pma".
13161 * The parameters of "map" and "pma" are assumed to have been aligned.
13163 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13164 __isl_take isl_map *map, enum isl_dim_type type,
13165 __isl_take isl_pw_multi_aff *pma)
13167 int i;
13168 isl_map *res;
13170 if (!pma)
13171 goto error;
13173 if (pma->n == 0) {
13174 isl_pw_multi_aff_free(pma);
13175 res = isl_map_empty(isl_map_get_space(map));
13176 isl_map_free(map);
13177 return res;
13180 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13181 isl_multi_aff_copy(pma->p[0].maff));
13182 if (type == isl_dim_in)
13183 res = isl_map_intersect_domain(res,
13184 isl_map_copy(pma->p[0].set));
13185 else
13186 res = isl_map_intersect_range(res,
13187 isl_map_copy(pma->p[0].set));
13189 for (i = 1; i < pma->n; ++i) {
13190 isl_map *res_i;
13192 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13193 isl_multi_aff_copy(pma->p[i].maff));
13194 if (type == isl_dim_in)
13195 res_i = isl_map_intersect_domain(res_i,
13196 isl_map_copy(pma->p[i].set));
13197 else
13198 res_i = isl_map_intersect_range(res_i,
13199 isl_map_copy(pma->p[i].set));
13200 res = isl_map_union(res, res_i);
13203 isl_pw_multi_aff_free(pma);
13204 isl_map_free(map);
13205 return res;
13206 error:
13207 isl_pw_multi_aff_free(pma);
13208 isl_map_free(map);
13209 return NULL;
13212 /* Compute the preimage of "map" under the function represented by "pma".
13213 * In other words, plug in "pma" in the domain or range of "map".
13214 * The result is a map that lives in the same space as "map",
13215 * except that the space of type "type" has been replaced by
13216 * the domain space of "pma".
13218 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13219 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13221 isl_bool aligned;
13223 if (!map || !pma)
13224 goto error;
13226 aligned = isl_map_space_has_equal_params(map, pma->dim);
13227 if (aligned < 0)
13228 goto error;
13229 if (aligned)
13230 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13232 if (isl_map_check_named_params(map) < 0)
13233 goto error;
13234 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13235 goto error;
13236 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13237 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13239 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13240 error:
13241 isl_pw_multi_aff_free(pma);
13242 return isl_map_free(map);
13245 /* Compute the preimage of "set" under the function represented by "pma".
13246 * In other words, plug in "pma" in "set". The result is a set
13247 * that lives in the domain space of "pma".
13249 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13250 __isl_take isl_pw_multi_aff *pma)
13252 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13255 /* Compute the preimage of the domain of "map" under the function
13256 * represented by "pma".
13257 * In other words, plug in "pma" in the domain of "map".
13258 * The result is a map that lives in the same space as "map",
13259 * except that domain space has been replaced by the domain space of "pma".
13261 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13262 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13264 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13267 /* Compute the preimage of the range of "map" under the function
13268 * represented by "pma".
13269 * In other words, plug in "pma" in the range of "map".
13270 * The result is a map that lives in the same space as "map",
13271 * except that range space has been replaced by the domain space of "pma".
13273 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13274 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13276 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13279 /* Compute the preimage of "map" under the function represented by "mpa".
13280 * In other words, plug in "mpa" in the domain or range of "map".
13281 * The result is a map that lives in the same space as "map",
13282 * except that the space of type "type" has been replaced by
13283 * the domain space of "mpa".
13285 * If the map does not involve any constraints that refer to the
13286 * dimensions of the substituted space, then the only possible
13287 * effect of "mpa" on the map is to map the space to a different space.
13288 * We create a separate isl_multi_aff to effectuate this change
13289 * in order to avoid spurious splitting of the map along the pieces
13290 * of "mpa".
13291 * If "mpa" has a non-trivial explicit domain, however,
13292 * then the full substitution should be performed.
13294 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13295 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13297 int n;
13298 isl_bool full;
13299 isl_pw_multi_aff *pma;
13301 if (!map || !mpa)
13302 goto error;
13304 n = isl_map_dim(map, type);
13305 full = isl_map_involves_dims(map, type, 0, n);
13306 if (full >= 0 && !full)
13307 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13308 if (full < 0)
13309 goto error;
13310 if (!full) {
13311 isl_space *space;
13312 isl_multi_aff *ma;
13314 space = isl_multi_pw_aff_get_space(mpa);
13315 isl_multi_pw_aff_free(mpa);
13316 ma = isl_multi_aff_zero(space);
13317 return isl_map_preimage_multi_aff(map, type, ma);
13320 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13321 return isl_map_preimage_pw_multi_aff(map, type, pma);
13322 error:
13323 isl_map_free(map);
13324 isl_multi_pw_aff_free(mpa);
13325 return NULL;
13328 /* Compute the preimage of "map" under the function represented by "mpa".
13329 * In other words, plug in "mpa" in the domain "map".
13330 * The result is a map that lives in the same space as "map",
13331 * except that domain space has been replaced by the domain space of "mpa".
13333 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13334 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13336 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13339 /* Compute the preimage of "set" by the function represented by "mpa".
13340 * In other words, plug in "mpa" in "set".
13342 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13343 __isl_take isl_multi_pw_aff *mpa)
13345 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13348 /* Return a copy of the equality constraints of "bset" as a matrix.
13350 __isl_give isl_mat *isl_basic_set_extract_equalities(
13351 __isl_keep isl_basic_set *bset)
13353 isl_ctx *ctx;
13354 unsigned total;
13356 if (!bset)
13357 return NULL;
13359 ctx = isl_basic_set_get_ctx(bset);
13360 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13361 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13364 /* Are the "n" "coefficients" starting at "first" of the integer division
13365 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13366 * to each other?
13367 * The "coefficient" at position 0 is the denominator.
13368 * The "coefficient" at position 1 is the constant term.
13370 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13371 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13372 unsigned first, unsigned n)
13374 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13375 return isl_bool_error;
13376 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13377 return isl_bool_error;
13378 return isl_seq_eq(bmap1->div[pos1] + first,
13379 bmap2->div[pos2] + first, n);
13382 /* Are the integer division expressions at position "pos1" in "bmap1" and
13383 * "pos2" in "bmap2" equal to each other, except that the constant terms
13384 * are different?
13386 isl_bool isl_basic_map_equal_div_expr_except_constant(
13387 __isl_keep isl_basic_map *bmap1, int pos1,
13388 __isl_keep isl_basic_map *bmap2, int pos2)
13390 isl_bool equal;
13391 unsigned total;
13393 if (!bmap1 || !bmap2)
13394 return isl_bool_error;
13395 total = isl_basic_map_total_dim(bmap1);
13396 if (total != isl_basic_map_total_dim(bmap2))
13397 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13398 "incomparable div expressions", return isl_bool_error);
13399 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13400 0, 1);
13401 if (equal < 0 || !equal)
13402 return equal;
13403 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13404 1, 1);
13405 if (equal < 0 || equal)
13406 return isl_bool_not(equal);
13407 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13408 2, total);
13411 /* Replace the numerator of the constant term of the integer division
13412 * expression at position "div" in "bmap" by "value".
13413 * The caller guarantees that this does not change the meaning
13414 * of the input.
13416 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13417 __isl_take isl_basic_map *bmap, int div, int value)
13419 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13420 return isl_basic_map_free(bmap);
13422 isl_int_set_si(bmap->div[div][1], value);
13424 return bmap;
13427 /* Is the point "inner" internal to inequality constraint "ineq"
13428 * of "bset"?
13429 * The point is considered to be internal to the inequality constraint,
13430 * if it strictly lies on the positive side of the inequality constraint,
13431 * or if it lies on the constraint and the constraint is lexico-positive.
13433 static isl_bool is_internal(__isl_keep isl_vec *inner,
13434 __isl_keep isl_basic_set *bset, int ineq)
13436 isl_ctx *ctx;
13437 int pos;
13438 unsigned total;
13440 if (!inner || !bset)
13441 return isl_bool_error;
13443 ctx = isl_basic_set_get_ctx(bset);
13444 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13445 &ctx->normalize_gcd);
13446 if (!isl_int_is_zero(ctx->normalize_gcd))
13447 return isl_int_is_nonneg(ctx->normalize_gcd);
13449 total = isl_basic_set_dim(bset, isl_dim_all);
13450 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13451 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13454 /* Tighten the inequality constraints of "bset" that are outward with respect
13455 * to the point "vec".
13456 * That is, tighten the constraints that are not satisfied by "vec".
13458 * "vec" is a point internal to some superset S of "bset" that is used
13459 * to make the subsets of S disjoint, by tightening one half of the constraints
13460 * that separate two subsets. In particular, the constraints of S
13461 * are all satisfied by "vec" and should not be tightened.
13462 * Of the internal constraints, those that have "vec" on the outside
13463 * are tightened. The shared facet is included in the adjacent subset
13464 * with the opposite constraint.
13465 * For constraints that saturate "vec", this criterion cannot be used
13466 * to determine which of the two sides should be tightened.
13467 * Instead, the sign of the first non-zero coefficient is used
13468 * to make this choice. Note that this second criterion is never used
13469 * on the constraints of S since "vec" is interior to "S".
13471 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13472 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13474 int j;
13476 bset = isl_basic_set_cow(bset);
13477 if (!bset)
13478 return NULL;
13479 for (j = 0; j < bset->n_ineq; ++j) {
13480 isl_bool internal;
13482 internal = is_internal(vec, bset, j);
13483 if (internal < 0)
13484 return isl_basic_set_free(bset);
13485 if (internal)
13486 continue;
13487 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13490 return bset;
13493 /* Replace the variables x of type "type" starting at "first" in "bmap"
13494 * by x' with x = M x' with M the matrix trans.
13495 * That is, replace the corresponding coefficients c by c M.
13497 * The transformation matrix should be a square matrix.
13499 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13500 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13501 __isl_take isl_mat *trans)
13503 unsigned pos;
13505 bmap = isl_basic_map_cow(bmap);
13506 if (!bmap || !trans)
13507 goto error;
13509 if (trans->n_row != trans->n_col)
13510 isl_die(trans->ctx, isl_error_invalid,
13511 "expecting square transformation matrix", goto error);
13512 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13513 goto error;
13515 pos = isl_basic_map_offset(bmap, type) + first;
13517 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13518 isl_mat_copy(trans)) < 0)
13519 goto error;
13520 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13521 isl_mat_copy(trans)) < 0)
13522 goto error;
13523 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13524 isl_mat_copy(trans)) < 0)
13525 goto error;
13527 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13528 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13530 isl_mat_free(trans);
13531 return bmap;
13532 error:
13533 isl_mat_free(trans);
13534 isl_basic_map_free(bmap);
13535 return NULL;
13538 /* Replace the variables x of type "type" starting at "first" in "bset"
13539 * by x' with x = M x' with M the matrix trans.
13540 * That is, replace the corresponding coefficients c by c M.
13542 * The transformation matrix should be a square matrix.
13544 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13545 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13546 __isl_take isl_mat *trans)
13548 return isl_basic_map_transform_dims(bset, type, first, trans);