isl_map_fix_val: reuse isl_map_check_range
[isl.git] / isl_map.c
blob4282c4942de1e7e5f1c66eda4bfd8959bbe36748
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep const isl_basic_map *const_bset_to_bmap(
58 __isl_keep const isl_basic_set *bset)
60 return (const isl_basic_map *) bset;
63 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return dim->nparam;
67 case isl_dim_in: return dim->n_in;
68 case isl_dim_out: return dim->n_out;
69 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
70 default: return 0;
74 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
76 switch (type) {
77 case isl_dim_param: return 1;
78 case isl_dim_in: return 1 + dim->nparam;
79 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
80 default: return 0;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
85 enum isl_dim_type type)
87 if (!bmap)
88 return 0;
89 switch (type) {
90 case isl_dim_cst: return 1;
91 case isl_dim_param:
92 case isl_dim_in:
93 case isl_dim_out: return isl_space_dim(bmap->dim, type);
94 case isl_dim_div: return bmap->n_div;
95 case isl_dim_all: return isl_basic_map_total_dim(bmap);
96 default: return 0;
100 /* Return the space of "map".
102 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
104 return map ? map->dim : NULL;
107 /* Return the space of "set".
109 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
111 return isl_map_peek_space(set_to_map(set));
114 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
116 return map ? n(map->dim, type) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
121 return set ? n(set->dim, type) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
128 enum isl_dim_type type)
130 isl_space *space;
132 space = isl_basic_map_peek_space(bmap);
133 if (!space)
134 return -1;
136 switch (type) {
137 case isl_dim_param:
138 case isl_dim_in:
139 case isl_dim_out: return isl_space_offset(space, type);
140 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
141 case isl_dim_cst:
142 default:
143 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
152 enum isl_dim_type type)
154 switch (type) {
155 case isl_dim_cst: return 0;
156 case isl_dim_param:
157 case isl_dim_in:
158 case isl_dim_out:
159 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
160 default: return 0;
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
165 enum isl_dim_type type)
167 return isl_basic_map_offset(bset, type);
170 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
172 return pos(map->dim, type);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
176 enum isl_dim_type type)
178 return isl_basic_map_dim(bset, type);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
183 return isl_basic_set_dim(bset, isl_dim_set);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_param);
191 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
196 unsigned isl_set_n_dim(__isl_keep isl_set *set)
198 return isl_set_dim(set, isl_dim_set);
201 unsigned isl_set_n_param(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_param);
206 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
208 return bmap ? bmap->dim->n_in : 0;
211 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
213 return bmap ? bmap->dim->n_out : 0;
216 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->nparam : 0;
221 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->n_div : 0;
226 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
228 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
231 unsigned isl_map_n_in(__isl_keep const isl_map *map)
233 return map ? map->dim->n_in : 0;
236 unsigned isl_map_n_out(__isl_keep const isl_map *map)
238 return map ? map->dim->n_out : 0;
241 unsigned isl_map_n_param(__isl_keep const isl_map *map)
243 return map ? map->dim->nparam : 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return -1;
253 return bmap->n_eq;
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
261 return isl_basic_map_n_equality(bset_to_bmap(bset));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return -1;
271 return bmap->n_ineq;
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
285 __isl_keep isl_basic_map *bmap2)
287 isl_space *space1, *space2;
289 space1 = isl_basic_map_peek_space(bmap1);
290 space2 = isl_basic_map_peek_space(bmap2);
291 return isl_space_has_equal_params(space1, space2);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
297 __isl_keep isl_map *map2)
299 isl_space *space1, *space2;
301 space1 = isl_map_peek_space(map1);
302 space2 = isl_map_peek_space(map2);
303 return isl_space_has_equal_params(space1, space2);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 return isl_map_has_equal_params(map, set_to_map(set));
314 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
315 __isl_keep isl_set *set)
317 isl_bool m;
318 if (!map || !set)
319 return isl_bool_error;
320 m = isl_map_has_equal_params(map, set_to_map(set));
321 if (m < 0 || !m)
322 return m;
323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
324 set->dim, isl_dim_set);
327 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
328 __isl_keep isl_basic_set *bset)
330 isl_bool m;
331 if (!bmap || !bset)
332 return isl_bool_error;
333 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
334 if (m < 0 || !m)
335 return m;
336 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
337 bset->dim, isl_dim_set);
340 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
341 __isl_keep isl_set *set)
343 isl_bool m;
344 if (!map || !set)
345 return isl_bool_error;
346 m = isl_map_has_equal_params(map, set_to_map(set));
347 if (m < 0 || !m)
348 return m;
349 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
350 set->dim, isl_dim_set);
353 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
354 __isl_keep isl_basic_set *bset)
356 isl_bool m;
357 if (!bmap || !bset)
358 return isl_bool_error;
359 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
360 if (m < 0 || !m)
361 return m;
362 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
363 bset->dim, isl_dim_set);
366 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
368 return bmap ? bmap->ctx : NULL;
371 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
373 return bset ? bset->ctx : NULL;
376 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
378 return map ? map->ctx : NULL;
381 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
383 return set ? set->ctx : NULL;
386 /* Return the space of "bmap".
388 __isl_keep isl_space *isl_basic_map_peek_space(
389 __isl_keep const isl_basic_map *bmap)
391 return bmap ? bmap->dim : NULL;
394 /* Return the space of "bset".
396 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
398 return isl_basic_map_peek_space(bset_to_bmap(bset));
401 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
403 return isl_space_copy(isl_basic_map_peek_space(bmap));
406 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_get_space(bset_to_bmap(bset));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
415 int i;
416 isl_ctx *ctx;
417 isl_mat *div;
418 unsigned total;
419 unsigned cols;
421 if (!bmap)
422 return NULL;
424 ctx = isl_basic_map_get_ctx(bmap);
425 total = isl_space_dim(bmap->dim, isl_dim_all);
426 cols = 1 + 1 + total + bmap->n_div;
427 div = isl_mat_alloc(ctx, bmap->n_div, cols);
428 if (!div)
429 return NULL;
431 for (i = 0; i < bmap->n_div; ++i)
432 isl_seq_cpy(div->row[i], bmap->div[i], cols);
434 return div;
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
441 return isl_basic_map_get_divs(bset);
444 __isl_give isl_local_space *isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map *bmap)
447 isl_mat *div;
449 if (!bmap)
450 return NULL;
452 div = isl_basic_map_get_divs(bmap);
453 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
456 __isl_give isl_local_space *isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set *bset)
459 return isl_basic_map_get_local_space(bset);
462 /* For each known div d = floor(f/m), add the constraints
464 * f - m d >= 0
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map *add_known_div_constraints(
470 __isl_take isl_basic_map *bmap)
472 int i;
473 unsigned n_div;
475 if (!bmap)
476 return NULL;
477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
478 if (n_div == 0)
479 return bmap;
480 bmap = isl_basic_map_cow(bmap);
481 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
482 if (!bmap)
483 return NULL;
484 for (i = 0; i < n_div; ++i) {
485 if (isl_int_is_zero(bmap->div[i][0]))
486 continue;
487 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
488 return isl_basic_map_free(bmap);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "map" has only named parameters, reporting an error
1390 * if it does not.
1392 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1394 return isl_space_check_named_params(isl_map_peek_space(map));
1397 /* Check that "bmap" has only named parameters, reporting an error
1398 * if it does not.
1400 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1402 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1405 /* Check that "bmap1" and "bmap2" have the same parameters,
1406 * reporting an error if they do not.
1408 static isl_stat isl_basic_map_check_equal_params(
1409 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1411 isl_bool match;
1413 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1414 if (match < 0)
1415 return isl_stat_error;
1416 if (!match)
1417 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1418 "parameters don't match", return isl_stat_error);
1419 return isl_stat_ok;
1422 __isl_give isl_map *isl_map_align_params_map_map_and(
1423 __isl_take isl_map *map1, __isl_take isl_map *map2,
1424 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1425 __isl_take isl_map *map2))
1427 if (!map1 || !map2)
1428 goto error;
1429 if (isl_map_has_equal_params(map1, map2))
1430 return fn(map1, map2);
1431 if (isl_map_check_named_params(map1) < 0)
1432 goto error;
1433 if (isl_map_check_named_params(map2) < 0)
1434 goto error;
1435 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1436 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1437 return fn(map1, map2);
1438 error:
1439 isl_map_free(map1);
1440 isl_map_free(map2);
1441 return NULL;
1444 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1445 __isl_keep isl_map *map2,
1446 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1448 isl_bool r;
1450 if (!map1 || !map2)
1451 return isl_bool_error;
1452 if (isl_map_has_equal_params(map1, map2))
1453 return fn(map1, map2);
1454 if (isl_map_check_named_params(map1) < 0)
1455 return isl_bool_error;
1456 if (isl_map_check_named_params(map2) < 0)
1457 return isl_bool_error;
1458 map1 = isl_map_copy(map1);
1459 map2 = isl_map_copy(map2);
1460 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1461 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1462 r = fn(map1, map2);
1463 isl_map_free(map1);
1464 isl_map_free(map2);
1465 return r;
1468 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1470 struct isl_ctx *ctx;
1471 if (!bmap)
1472 return -1;
1473 ctx = bmap->ctx;
1474 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1475 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1476 return -1);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1479 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1480 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1481 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1482 isl_int *t;
1483 int j = isl_basic_map_alloc_inequality(bmap);
1484 if (j < 0)
1485 return -1;
1486 t = bmap->ineq[j];
1487 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1488 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1489 bmap->eq[-1] = t;
1490 bmap->n_eq++;
1491 bmap->n_ineq--;
1492 bmap->eq--;
1493 return 0;
1495 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1496 bmap->extra - bmap->n_div);
1497 return bmap->n_eq++;
1500 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1502 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1505 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1507 if (!bmap)
1508 return -1;
1509 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1510 bmap->n_eq -= n;
1511 return 0;
1514 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1516 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1519 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1521 isl_int *t;
1522 if (!bmap)
1523 return -1;
1524 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1526 if (pos != bmap->n_eq - 1) {
1527 t = bmap->eq[pos];
1528 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1529 bmap->eq[bmap->n_eq - 1] = t;
1531 bmap->n_eq--;
1532 return 0;
1535 /* Turn inequality "pos" of "bmap" into an equality.
1537 * In particular, we move the inequality in front of the equalities
1538 * and move the last inequality in the position of the moved inequality.
1539 * Note that isl_tab_make_equalities_explicit depends on this particular
1540 * change in the ordering of the constraints.
1542 void isl_basic_map_inequality_to_equality(
1543 struct isl_basic_map *bmap, unsigned pos)
1545 isl_int *t;
1547 t = bmap->ineq[pos];
1548 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1549 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1550 bmap->eq[-1] = t;
1551 bmap->n_eq++;
1552 bmap->n_ineq--;
1553 bmap->eq--;
1554 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1555 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1557 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1560 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1562 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1565 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1567 struct isl_ctx *ctx;
1568 if (!bmap)
1569 return -1;
1570 ctx = bmap->ctx;
1571 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1572 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1574 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1575 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1576 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1577 1 + isl_basic_map_total_dim(bmap),
1578 bmap->extra - bmap->n_div);
1579 return bmap->n_ineq++;
1582 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1584 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1587 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1589 if (!bmap)
1590 return -1;
1591 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1592 bmap->n_ineq -= n;
1593 return 0;
1596 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1598 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1601 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1603 isl_int *t;
1604 if (!bmap)
1605 return -1;
1606 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1608 if (pos != bmap->n_ineq - 1) {
1609 t = bmap->ineq[pos];
1610 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1611 bmap->ineq[bmap->n_ineq - 1] = t;
1612 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1614 bmap->n_ineq--;
1615 return 0;
1618 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1620 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1623 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1624 isl_int *eq)
1626 isl_bool empty;
1627 int k;
1629 empty = isl_basic_map_plain_is_empty(bmap);
1630 if (empty < 0)
1631 return isl_basic_map_free(bmap);
1632 if (empty)
1633 return bmap;
1635 bmap = isl_basic_map_cow(bmap);
1636 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1637 if (!bmap)
1638 return NULL;
1639 k = isl_basic_map_alloc_equality(bmap);
1640 if (k < 0)
1641 goto error;
1642 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1643 return bmap;
1644 error:
1645 isl_basic_map_free(bmap);
1646 return NULL;
1649 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1650 isl_int *eq)
1652 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1655 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1656 isl_int *ineq)
1658 int k;
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1662 if (!bmap)
1663 return NULL;
1664 k = isl_basic_map_alloc_inequality(bmap);
1665 if (k < 0)
1666 goto error;
1667 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1668 return bmap;
1669 error:
1670 isl_basic_map_free(bmap);
1671 return NULL;
1674 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1675 isl_int *ineq)
1677 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1680 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1682 if (!bmap)
1683 return -1;
1684 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1685 isl_seq_clr(bmap->div[bmap->n_div] +
1686 1 + 1 + isl_basic_map_total_dim(bmap),
1687 bmap->extra - bmap->n_div);
1688 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1689 return bmap->n_div++;
1692 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1694 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1697 /* Check that there are "n" dimensions of type "type" starting at "first"
1698 * in "bmap".
1700 isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 unsigned dim;
1705 if (!bmap)
1706 return isl_stat_error;
1707 dim = isl_basic_map_dim(bmap, type);
1708 if (first + n > dim || first + n < first)
1709 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1710 "position or range out of bounds",
1711 return isl_stat_error);
1712 return isl_stat_ok;
1715 /* Insert an extra integer division, prescribed by "div", to "bmap"
1716 * at (integer division) position "pos".
1718 * The integer division is first added at the end and then moved
1719 * into the right position.
1721 __isl_give isl_basic_map *isl_basic_map_insert_div(
1722 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1724 int i, k;
1726 bmap = isl_basic_map_cow(bmap);
1727 if (!bmap || !div)
1728 return isl_basic_map_free(bmap);
1730 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1731 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1732 "unexpected size", return isl_basic_map_free(bmap));
1733 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1734 return isl_basic_map_free(bmap);
1736 bmap = isl_basic_map_extend_space(bmap,
1737 isl_basic_map_get_space(bmap), 1, 0, 2);
1738 k = isl_basic_map_alloc_div(bmap);
1739 if (k < 0)
1740 return isl_basic_map_free(bmap);
1741 isl_seq_cpy(bmap->div[k], div->el, div->size);
1742 isl_int_set_si(bmap->div[k][div->size], 0);
1744 for (i = k; i > pos; --i)
1745 isl_basic_map_swap_div(bmap, i, i - 1);
1747 return bmap;
1750 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1752 if (!bmap)
1753 return isl_stat_error;
1754 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1755 bmap->n_div -= n;
1756 return isl_stat_ok;
1759 static __isl_give isl_basic_map *add_constraints(
1760 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1761 unsigned i_pos, unsigned o_pos)
1763 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1764 isl_ctx *ctx;
1765 isl_space *space;
1766 struct isl_dim_map *dim_map;
1768 space = isl_basic_map_peek_space(bmap2);
1769 if (!bmap1 || !space)
1770 goto error;
1772 total = isl_basic_map_dim(bmap1, isl_dim_all);
1773 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1774 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1775 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1776 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1777 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1778 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1779 ctx = isl_basic_map_get_ctx(bmap1);
1780 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1781 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1782 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1783 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1784 isl_dim_map_div(dim_map, bmap2, total);
1786 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1787 error:
1788 isl_basic_map_free(bmap1);
1789 isl_basic_map_free(bmap2);
1790 return NULL;
1793 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1794 struct isl_basic_set *bset2, unsigned pos)
1796 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1797 bset_to_bmap(bset2), 0, pos));
1800 __isl_give isl_basic_map *isl_basic_map_extend_space(
1801 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1802 unsigned extra, unsigned n_eq, unsigned n_ineq)
1804 struct isl_basic_map *ext;
1805 unsigned flags;
1806 int dims_ok;
1808 if (!space)
1809 goto error;
1811 if (!base)
1812 goto error;
1814 dims_ok = isl_space_is_equal(base->dim, space) &&
1815 base->extra >= base->n_div + extra;
1817 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1818 room_for_ineq(base, n_ineq)) {
1819 isl_space_free(space);
1820 return base;
1823 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1824 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1825 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1826 extra += base->extra;
1827 n_eq += base->n_eq;
1828 n_ineq += base->n_ineq;
1830 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1831 space = NULL;
1832 if (!ext)
1833 goto error;
1835 if (dims_ok)
1836 ext->sample = isl_vec_copy(base->sample);
1837 flags = base->flags;
1838 ext = add_constraints(ext, base, 0, 0);
1839 if (ext) {
1840 ext->flags = flags;
1841 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1844 return ext;
1846 error:
1847 isl_space_free(space);
1848 isl_basic_map_free(base);
1849 return NULL;
1852 __isl_give isl_basic_set *isl_basic_set_extend_space(
1853 __isl_take isl_basic_set *base,
1854 __isl_take isl_space *dim, unsigned extra,
1855 unsigned n_eq, unsigned n_ineq)
1857 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1858 dim, extra, n_eq, n_ineq));
1861 struct isl_basic_map *isl_basic_map_extend_constraints(
1862 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1864 if (!base)
1865 return NULL;
1866 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1867 0, n_eq, n_ineq);
1870 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1871 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1872 unsigned n_eq, unsigned n_ineq)
1874 struct isl_basic_map *bmap;
1875 isl_space *dim;
1877 if (!base)
1878 return NULL;
1879 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1880 if (!dim)
1881 goto error;
1883 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1884 return bmap;
1885 error:
1886 isl_basic_map_free(base);
1887 return NULL;
1890 struct isl_basic_set *isl_basic_set_extend_constraints(
1891 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1893 isl_basic_map *bmap = bset_to_bmap(base);
1894 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1895 return bset_from_bmap(bmap);
1898 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1900 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1903 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1905 if (!bmap)
1906 return NULL;
1908 if (bmap->ref > 1) {
1909 bmap->ref--;
1910 bmap = isl_basic_map_dup(bmap);
1912 if (bmap) {
1913 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1914 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1916 return bmap;
1919 /* Clear all cached information in "map", either because it is about
1920 * to be modified or because it is being freed.
1921 * Always return the same pointer that is passed in.
1922 * This is needed for the use in isl_map_free.
1924 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1926 isl_basic_map_free(map->cached_simple_hull[0]);
1927 isl_basic_map_free(map->cached_simple_hull[1]);
1928 map->cached_simple_hull[0] = NULL;
1929 map->cached_simple_hull[1] = NULL;
1930 return map;
1933 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1935 return isl_map_cow(set);
1938 /* Return an isl_map that is equal to "map" and that has only
1939 * a single reference.
1941 * If the original input already has only one reference, then
1942 * simply return it, but clear all cached information, since
1943 * it may be rendered invalid by the operations that will be
1944 * performed on the result.
1946 * Otherwise, create a duplicate (without any cached information).
1948 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1950 if (!map)
1951 return NULL;
1953 if (map->ref == 1)
1954 return clear_caches(map);
1955 map->ref--;
1956 return isl_map_dup(map);
1959 static void swap_vars(struct isl_blk blk, isl_int *a,
1960 unsigned a_len, unsigned b_len)
1962 isl_seq_cpy(blk.data, a+a_len, b_len);
1963 isl_seq_cpy(blk.data+b_len, a, a_len);
1964 isl_seq_cpy(a, blk.data, b_len+a_len);
1967 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1968 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1970 int i;
1971 struct isl_blk blk;
1973 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1974 goto error;
1976 if (n1 == 0 || n2 == 0)
1977 return bmap;
1979 bmap = isl_basic_map_cow(bmap);
1980 if (!bmap)
1981 return NULL;
1983 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1984 if (isl_blk_is_error(blk))
1985 goto error;
1987 for (i = 0; i < bmap->n_eq; ++i)
1988 swap_vars(blk,
1989 bmap->eq[i] + pos, n1, n2);
1991 for (i = 0; i < bmap->n_ineq; ++i)
1992 swap_vars(blk,
1993 bmap->ineq[i] + pos, n1, n2);
1995 for (i = 0; i < bmap->n_div; ++i)
1996 swap_vars(blk,
1997 bmap->div[i]+1 + pos, n1, n2);
1999 isl_blk_free(bmap->ctx, blk);
2001 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2002 bmap = isl_basic_map_gauss(bmap, NULL);
2003 return isl_basic_map_finalize(bmap);
2004 error:
2005 isl_basic_map_free(bmap);
2006 return NULL;
2009 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2010 __isl_take isl_basic_map *bmap)
2012 int i = 0;
2013 unsigned total;
2014 if (!bmap)
2015 goto error;
2016 total = isl_basic_map_total_dim(bmap);
2017 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2018 return isl_basic_map_free(bmap);
2019 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2020 if (bmap->n_eq > 0)
2021 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2022 else {
2023 i = isl_basic_map_alloc_equality(bmap);
2024 if (i < 0)
2025 goto error;
2027 isl_int_set_si(bmap->eq[i][0], 1);
2028 isl_seq_clr(bmap->eq[i]+1, total);
2029 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2030 isl_vec_free(bmap->sample);
2031 bmap->sample = NULL;
2032 return isl_basic_map_finalize(bmap);
2033 error:
2034 isl_basic_map_free(bmap);
2035 return NULL;
2038 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2039 __isl_take isl_basic_set *bset)
2041 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2044 __isl_give isl_basic_map *isl_basic_map_set_rational(
2045 __isl_take isl_basic_map *bmap)
2047 if (!bmap)
2048 return NULL;
2050 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2051 return bmap;
2053 bmap = isl_basic_map_cow(bmap);
2054 if (!bmap)
2055 return NULL;
2057 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2059 return isl_basic_map_finalize(bmap);
2062 __isl_give isl_basic_set *isl_basic_set_set_rational(
2063 __isl_take isl_basic_set *bset)
2065 return isl_basic_map_set_rational(bset);
2068 __isl_give isl_basic_set *isl_basic_set_set_integral(
2069 __isl_take isl_basic_set *bset)
2071 if (!bset)
2072 return NULL;
2074 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2075 return bset;
2077 bset = isl_basic_set_cow(bset);
2078 if (!bset)
2079 return NULL;
2081 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2083 return isl_basic_set_finalize(bset);
2086 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2088 int i;
2090 map = isl_map_cow(map);
2091 if (!map)
2092 return NULL;
2093 for (i = 0; i < map->n; ++i) {
2094 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2095 if (!map->p[i])
2096 goto error;
2098 return map;
2099 error:
2100 isl_map_free(map);
2101 return NULL;
2104 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2106 return isl_map_set_rational(set);
2109 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2110 * of "bmap").
2112 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2114 isl_int *t = bmap->div[a];
2115 bmap->div[a] = bmap->div[b];
2116 bmap->div[b] = t;
2119 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2120 * div definitions accordingly.
2122 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2124 int i;
2125 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2127 swap_div(bmap, a, b);
2129 for (i = 0; i < bmap->n_eq; ++i)
2130 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2132 for (i = 0; i < bmap->n_ineq; ++i)
2133 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2135 for (i = 0; i < bmap->n_div; ++i)
2136 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2137 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2140 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2142 isl_seq_cpy(c, c + n, rem);
2143 isl_seq_clr(c + rem, n);
2146 /* Drop n dimensions starting at first.
2148 * In principle, this frees up some extra variables as the number
2149 * of columns remains constant, but we would have to extend
2150 * the div array too as the number of rows in this array is assumed
2151 * to be equal to extra.
2153 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2154 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2156 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2159 /* Move "n" divs starting at "first" to the end of the list of divs.
2161 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2162 unsigned first, unsigned n)
2164 isl_int **div;
2165 int i;
2167 if (first + n == bmap->n_div)
2168 return bmap;
2170 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2171 if (!div)
2172 goto error;
2173 for (i = 0; i < n; ++i)
2174 div[i] = bmap->div[first + i];
2175 for (i = 0; i < bmap->n_div - first - n; ++i)
2176 bmap->div[first + i] = bmap->div[first + n + i];
2177 for (i = 0; i < n; ++i)
2178 bmap->div[bmap->n_div - n + i] = div[i];
2179 free(div);
2180 return bmap;
2181 error:
2182 isl_basic_map_free(bmap);
2183 return NULL;
2186 /* Check that there are "n" dimensions of type "type" starting at "first"
2187 * in "map".
2189 static isl_stat isl_map_check_range(__isl_keep isl_map *map,
2190 enum isl_dim_type type, unsigned first, unsigned n)
2192 if (!map)
2193 return isl_stat_error;
2194 if (first + n > isl_map_dim(map, type) || first + n < first)
2195 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2196 "position or range out of bounds",
2197 return isl_stat_error);
2198 return isl_stat_ok;
2201 /* Drop "n" dimensions of type "type" starting at "first".
2202 * Perform the core computation, without cowing or
2203 * simplifying and finalizing the result.
2205 * In principle, this frees up some extra variables as the number
2206 * of columns remains constant, but we would have to extend
2207 * the div array too as the number of rows in this array is assumed
2208 * to be equal to extra.
2210 __isl_give isl_basic_map *isl_basic_map_drop_core(
2211 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2212 unsigned first, unsigned n)
2214 int i;
2215 unsigned offset;
2216 unsigned left;
2218 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2219 return isl_basic_map_free(bmap);
2221 offset = isl_basic_map_offset(bmap, type) + first;
2222 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2223 for (i = 0; i < bmap->n_eq; ++i)
2224 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2226 for (i = 0; i < bmap->n_ineq; ++i)
2227 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2229 for (i = 0; i < bmap->n_div; ++i)
2230 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2232 if (type == isl_dim_div) {
2233 bmap = move_divs_last(bmap, first, n);
2234 if (!bmap)
2235 return NULL;
2236 if (isl_basic_map_free_div(bmap, n) < 0)
2237 return isl_basic_map_free(bmap);
2238 } else
2239 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2240 if (!bmap->dim)
2241 return isl_basic_map_free(bmap);
2243 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2244 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2245 return bmap;
2248 /* Drop "n" dimensions of type "type" starting at "first".
2250 * In principle, this frees up some extra variables as the number
2251 * of columns remains constant, but we would have to extend
2252 * the div array too as the number of rows in this array is assumed
2253 * to be equal to extra.
2255 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2256 enum isl_dim_type type, unsigned first, unsigned n)
2258 if (!bmap)
2259 return NULL;
2260 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2261 return bmap;
2263 bmap = isl_basic_map_cow(bmap);
2264 if (!bmap)
2265 return NULL;
2267 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2269 bmap = isl_basic_map_simplify(bmap);
2270 return isl_basic_map_finalize(bmap);
2273 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2274 enum isl_dim_type type, unsigned first, unsigned n)
2276 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2277 type, first, n));
2280 /* No longer consider "map" to be normalized.
2282 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2284 if (!map)
2285 return NULL;
2286 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2287 return map;
2290 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2291 enum isl_dim_type type, unsigned first, unsigned n)
2293 int i;
2295 if (isl_map_check_range(map, type, first, n) < 0)
2296 return isl_map_free(map);
2298 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2299 return map;
2300 map = isl_map_cow(map);
2301 if (!map)
2302 goto error;
2303 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2304 if (!map->dim)
2305 goto error;
2307 for (i = 0; i < map->n; ++i) {
2308 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2309 if (!map->p[i])
2310 goto error;
2312 map = isl_map_unmark_normalized(map);
2314 return map;
2315 error:
2316 isl_map_free(map);
2317 return NULL;
2320 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2321 enum isl_dim_type type, unsigned first, unsigned n)
2323 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2326 /* Drop the integer division at position "div", which is assumed
2327 * not to appear in any of the constraints or
2328 * in any of the other integer divisions.
2330 * Since the integer division is redundant, there is no need to cow.
2332 __isl_give isl_basic_map *isl_basic_map_drop_div(
2333 __isl_take isl_basic_map *bmap, unsigned div)
2335 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2338 /* Eliminate the specified n dimensions starting at first from the
2339 * constraints, without removing the dimensions from the space.
2340 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2342 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2343 enum isl_dim_type type, unsigned first, unsigned n)
2345 int i;
2347 if (n == 0)
2348 return map;
2350 if (isl_map_check_range(map, type, first, n) < 0)
2351 return isl_map_free(map);
2353 map = isl_map_cow(map);
2354 if (!map)
2355 return NULL;
2357 for (i = 0; i < map->n; ++i) {
2358 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2359 if (!map->p[i])
2360 goto error;
2362 return map;
2363 error:
2364 isl_map_free(map);
2365 return NULL;
2368 /* Eliminate the specified n dimensions starting at first from the
2369 * constraints, without removing the dimensions from the space.
2370 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2372 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2373 enum isl_dim_type type, unsigned first, unsigned n)
2375 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2378 /* Eliminate the specified n dimensions starting at first from the
2379 * constraints, without removing the dimensions from the space.
2380 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2382 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2383 unsigned first, unsigned n)
2385 return isl_set_eliminate(set, isl_dim_set, first, n);
2388 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2389 __isl_take isl_basic_map *bmap)
2391 if (!bmap)
2392 return NULL;
2393 bmap = isl_basic_map_eliminate_vars(bmap,
2394 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2395 if (!bmap)
2396 return NULL;
2397 bmap->n_div = 0;
2398 return isl_basic_map_finalize(bmap);
2401 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2402 __isl_take isl_basic_set *bset)
2404 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2407 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2409 int i;
2411 if (!map)
2412 return NULL;
2413 if (map->n == 0)
2414 return map;
2416 map = isl_map_cow(map);
2417 if (!map)
2418 return NULL;
2420 for (i = 0; i < map->n; ++i) {
2421 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2422 if (!map->p[i])
2423 goto error;
2425 return map;
2426 error:
2427 isl_map_free(map);
2428 return NULL;
2431 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2433 return isl_map_remove_divs(set);
2436 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2437 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2438 unsigned first, unsigned n)
2440 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2441 return isl_basic_map_free(bmap);
2442 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2443 return bmap;
2444 bmap = isl_basic_map_eliminate_vars(bmap,
2445 isl_basic_map_offset(bmap, type) - 1 + first, n);
2446 if (!bmap)
2447 return bmap;
2448 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2449 return bmap;
2450 bmap = isl_basic_map_drop(bmap, type, first, n);
2451 return bmap;
2454 /* Return true if the definition of the given div (recursively) involves
2455 * any of the given variables.
2457 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2458 unsigned first, unsigned n)
2460 int i;
2461 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2463 if (isl_int_is_zero(bmap->div[div][0]))
2464 return isl_bool_false;
2465 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2466 return isl_bool_true;
2468 for (i = bmap->n_div - 1; i >= 0; --i) {
2469 isl_bool involves;
2471 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2472 continue;
2473 involves = div_involves_vars(bmap, i, first, n);
2474 if (involves < 0 || involves)
2475 return involves;
2478 return isl_bool_false;
2481 /* Try and add a lower and/or upper bound on "div" to "bmap"
2482 * based on inequality "i".
2483 * "total" is the total number of variables (excluding the divs).
2484 * "v" is a temporary object that can be used during the calculations.
2485 * If "lb" is set, then a lower bound should be constructed.
2486 * If "ub" is set, then an upper bound should be constructed.
2488 * The calling function has already checked that the inequality does not
2489 * reference "div", but we still need to check that the inequality is
2490 * of the right form. We'll consider the case where we want to construct
2491 * a lower bound. The construction of upper bounds is similar.
2493 * Let "div" be of the form
2495 * q = floor((a + f(x))/d)
2497 * We essentially check if constraint "i" is of the form
2499 * b + f(x) >= 0
2501 * so that we can use it to derive a lower bound on "div".
2502 * However, we allow a slightly more general form
2504 * b + g(x) >= 0
2506 * with the condition that the coefficients of g(x) - f(x) are all
2507 * divisible by d.
2508 * Rewriting this constraint as
2510 * 0 >= -b - g(x)
2512 * adding a + f(x) to both sides and dividing by d, we obtain
2514 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2516 * Taking the floor on both sides, we obtain
2518 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2520 * or
2522 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2524 * In the case of an upper bound, we construct the constraint
2526 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2529 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2530 __isl_take isl_basic_map *bmap, int div, int i,
2531 unsigned total, isl_int v, int lb, int ub)
2533 int j;
2535 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2536 if (lb) {
2537 isl_int_sub(v, bmap->ineq[i][1 + j],
2538 bmap->div[div][1 + 1 + j]);
2539 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2541 if (ub) {
2542 isl_int_add(v, bmap->ineq[i][1 + j],
2543 bmap->div[div][1 + 1 + j]);
2544 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2547 if (!lb && !ub)
2548 return bmap;
2550 bmap = isl_basic_map_cow(bmap);
2551 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2552 if (lb) {
2553 int k = isl_basic_map_alloc_inequality(bmap);
2554 if (k < 0)
2555 goto error;
2556 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2557 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2558 bmap->div[div][1 + j]);
2559 isl_int_cdiv_q(bmap->ineq[k][j],
2560 bmap->ineq[k][j], bmap->div[div][0]);
2562 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2564 if (ub) {
2565 int k = isl_basic_map_alloc_inequality(bmap);
2566 if (k < 0)
2567 goto error;
2568 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2569 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2570 bmap->div[div][1 + j]);
2571 isl_int_fdiv_q(bmap->ineq[k][j],
2572 bmap->ineq[k][j], bmap->div[div][0]);
2574 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2577 return bmap;
2578 error:
2579 isl_basic_map_free(bmap);
2580 return NULL;
2583 /* This function is called right before "div" is eliminated from "bmap"
2584 * using Fourier-Motzkin.
2585 * Look through the constraints of "bmap" for constraints on the argument
2586 * of the integer division and use them to construct constraints on the
2587 * integer division itself. These constraints can then be combined
2588 * during the Fourier-Motzkin elimination.
2589 * Note that it is only useful to introduce lower bounds on "div"
2590 * if "bmap" already contains upper bounds on "div" as the newly
2591 * introduce lower bounds can then be combined with the pre-existing
2592 * upper bounds. Similarly for upper bounds.
2593 * We therefore first check if "bmap" contains any lower and/or upper bounds
2594 * on "div".
2596 * It is interesting to note that the introduction of these constraints
2597 * can indeed lead to more accurate results, even when compared to
2598 * deriving constraints on the argument of "div" from constraints on "div".
2599 * Consider, for example, the set
2601 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2603 * The second constraint can be rewritten as
2605 * 2 * [(-i-2j+3)/4] + k >= 0
2607 * from which we can derive
2609 * -i - 2j + 3 >= -2k
2611 * or
2613 * i + 2j <= 3 + 2k
2615 * Combined with the first constraint, we obtain
2617 * -3 <= 3 + 2k or k >= -3
2619 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2620 * the first constraint, we obtain
2622 * [(i + 2j)/4] >= [-3/4] = -1
2624 * Combining this constraint with the second constraint, we obtain
2626 * k >= -2
2628 static __isl_give isl_basic_map *insert_bounds_on_div(
2629 __isl_take isl_basic_map *bmap, int div)
2631 int i;
2632 int check_lb, check_ub;
2633 isl_int v;
2634 unsigned total;
2636 if (!bmap)
2637 return NULL;
2639 if (isl_int_is_zero(bmap->div[div][0]))
2640 return bmap;
2642 total = isl_space_dim(bmap->dim, isl_dim_all);
2644 check_lb = 0;
2645 check_ub = 0;
2646 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2647 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2648 if (s > 0)
2649 check_ub = 1;
2650 if (s < 0)
2651 check_lb = 1;
2654 if (!check_lb && !check_ub)
2655 return bmap;
2657 isl_int_init(v);
2659 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2660 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2661 continue;
2663 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2664 check_lb, check_ub);
2667 isl_int_clear(v);
2669 return bmap;
2672 /* Remove all divs (recursively) involving any of the given dimensions
2673 * in their definitions.
2675 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2676 __isl_take isl_basic_map *bmap,
2677 enum isl_dim_type type, unsigned first, unsigned n)
2679 int i;
2681 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2682 return isl_basic_map_free(bmap);
2683 first += isl_basic_map_offset(bmap, type);
2685 for (i = bmap->n_div - 1; i >= 0; --i) {
2686 isl_bool involves;
2688 involves = div_involves_vars(bmap, i, first, n);
2689 if (involves < 0)
2690 return isl_basic_map_free(bmap);
2691 if (!involves)
2692 continue;
2693 bmap = insert_bounds_on_div(bmap, i);
2694 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2695 if (!bmap)
2696 return NULL;
2697 i = bmap->n_div;
2700 return bmap;
2703 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2704 __isl_take isl_basic_set *bset,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2707 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2710 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2711 enum isl_dim_type type, unsigned first, unsigned n)
2713 int i;
2715 if (!map)
2716 return NULL;
2717 if (map->n == 0)
2718 return map;
2720 map = isl_map_cow(map);
2721 if (!map)
2722 return NULL;
2724 for (i = 0; i < map->n; ++i) {
2725 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2726 type, first, n);
2727 if (!map->p[i])
2728 goto error;
2730 return map;
2731 error:
2732 isl_map_free(map);
2733 return NULL;
2736 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2737 enum isl_dim_type type, unsigned first, unsigned n)
2739 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2740 type, first, n));
2743 /* Does the description of "bmap" depend on the specified dimensions?
2744 * We also check whether the dimensions appear in any of the div definitions.
2745 * In principle there is no need for this check. If the dimensions appear
2746 * in a div definition, they also appear in the defining constraints of that
2747 * div.
2749 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2750 enum isl_dim_type type, unsigned first, unsigned n)
2752 int i;
2754 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2755 return isl_bool_error;
2757 first += isl_basic_map_offset(bmap, type);
2758 for (i = 0; i < bmap->n_eq; ++i)
2759 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2760 return isl_bool_true;
2761 for (i = 0; i < bmap->n_ineq; ++i)
2762 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2763 return isl_bool_true;
2764 for (i = 0; i < bmap->n_div; ++i) {
2765 if (isl_int_is_zero(bmap->div[i][0]))
2766 continue;
2767 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2768 return isl_bool_true;
2771 return isl_bool_false;
2774 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2775 enum isl_dim_type type, unsigned first, unsigned n)
2777 int i;
2779 if (isl_map_check_range(map, type, first, n) < 0)
2780 return isl_bool_error;
2782 for (i = 0; i < map->n; ++i) {
2783 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2784 type, first, n);
2785 if (involves < 0 || involves)
2786 return involves;
2789 return isl_bool_false;
2792 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2793 enum isl_dim_type type, unsigned first, unsigned n)
2795 return isl_basic_map_involves_dims(bset, type, first, n);
2798 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2799 enum isl_dim_type type, unsigned first, unsigned n)
2801 return isl_map_involves_dims(set, type, first, n);
2804 /* Drop all constraints in bmap that involve any of the dimensions
2805 * first to first+n-1.
2806 * This function only performs the actual removal of constraints.
2808 * This function should not call finalize since it is used by
2809 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2811 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2812 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2814 int i;
2816 if (n == 0)
2817 return bmap;
2819 bmap = isl_basic_map_cow(bmap);
2821 if (!bmap)
2822 return NULL;
2824 for (i = bmap->n_eq - 1; i >= 0; --i) {
2825 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2826 continue;
2827 isl_basic_map_drop_equality(bmap, i);
2830 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2831 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2832 continue;
2833 isl_basic_map_drop_inequality(bmap, i);
2836 return bmap;
2839 /* Drop all constraints in bset that involve any of the dimensions
2840 * first to first+n-1.
2841 * This function only performs the actual removal of constraints.
2843 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2844 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2846 return isl_basic_map_drop_constraints_involving(bset, first, n);
2849 /* Drop all constraints in bmap that do not involve any of the dimensions
2850 * first to first + n - 1 of the given type.
2852 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2853 __isl_take isl_basic_map *bmap,
2854 enum isl_dim_type type, unsigned first, unsigned n)
2856 int i;
2858 if (n == 0) {
2859 isl_space *space = isl_basic_map_get_space(bmap);
2860 isl_basic_map_free(bmap);
2861 return isl_basic_map_universe(space);
2863 bmap = isl_basic_map_cow(bmap);
2864 if (!bmap)
2865 return NULL;
2867 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2868 return isl_basic_map_free(bmap);
2870 first += isl_basic_map_offset(bmap, type) - 1;
2872 for (i = bmap->n_eq - 1; i >= 0; --i) {
2873 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2874 continue;
2875 isl_basic_map_drop_equality(bmap, i);
2878 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2879 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2880 continue;
2881 isl_basic_map_drop_inequality(bmap, i);
2884 bmap = isl_basic_map_add_known_div_constraints(bmap);
2885 return bmap;
2888 /* Drop all constraints in bset that do not involve any of the dimensions
2889 * first to first + n - 1 of the given type.
2891 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2892 __isl_take isl_basic_set *bset,
2893 enum isl_dim_type type, unsigned first, unsigned n)
2895 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2896 type, first, n);
2899 /* Drop all constraints in bmap that involve any of the dimensions
2900 * first to first + n - 1 of the given type.
2902 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2903 __isl_take isl_basic_map *bmap,
2904 enum isl_dim_type type, unsigned first, unsigned n)
2906 if (!bmap)
2907 return NULL;
2908 if (n == 0)
2909 return bmap;
2911 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2912 return isl_basic_map_free(bmap);
2914 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2915 first += isl_basic_map_offset(bmap, type) - 1;
2916 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2917 bmap = isl_basic_map_add_known_div_constraints(bmap);
2918 return bmap;
2921 /* Drop all constraints in bset that involve any of the dimensions
2922 * first to first + n - 1 of the given type.
2924 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2925 __isl_take isl_basic_set *bset,
2926 enum isl_dim_type type, unsigned first, unsigned n)
2928 return isl_basic_map_drop_constraints_involving_dims(bset,
2929 type, first, n);
2932 /* Drop constraints from "map" by applying "drop" to each basic map.
2934 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2935 enum isl_dim_type type, unsigned first, unsigned n,
2936 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2937 enum isl_dim_type type, unsigned first, unsigned n))
2939 int i;
2941 if (isl_map_check_range(map, type, first, n) < 0)
2942 return isl_map_free(map);
2944 map = isl_map_cow(map);
2945 if (!map)
2946 return NULL;
2948 for (i = 0; i < map->n; ++i) {
2949 map->p[i] = drop(map->p[i], type, first, n);
2950 if (!map->p[i])
2951 return isl_map_free(map);
2954 if (map->n > 1)
2955 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2957 return map;
2960 /* Drop all constraints in map that involve any of the dimensions
2961 * first to first + n - 1 of the given type.
2963 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2964 __isl_take isl_map *map,
2965 enum isl_dim_type type, unsigned first, unsigned n)
2967 if (n == 0)
2968 return map;
2969 return drop_constraints(map, type, first, n,
2970 &isl_basic_map_drop_constraints_involving_dims);
2973 /* Drop all constraints in "map" that do not involve any of the dimensions
2974 * first to first + n - 1 of the given type.
2976 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2977 __isl_take isl_map *map,
2978 enum isl_dim_type type, unsigned first, unsigned n)
2980 if (n == 0) {
2981 isl_space *space = isl_map_get_space(map);
2982 isl_map_free(map);
2983 return isl_map_universe(space);
2985 return drop_constraints(map, type, first, n,
2986 &isl_basic_map_drop_constraints_not_involving_dims);
2989 /* Drop all constraints in set that involve any of the dimensions
2990 * first to first + n - 1 of the given type.
2992 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2993 __isl_take isl_set *set,
2994 enum isl_dim_type type, unsigned first, unsigned n)
2996 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2999 /* Drop all constraints in "set" that do not involve any of the dimensions
3000 * first to first + n - 1 of the given type.
3002 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3003 __isl_take isl_set *set,
3004 enum isl_dim_type type, unsigned first, unsigned n)
3006 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3009 /* Does local variable "div" of "bmap" have a complete explicit representation?
3010 * Having a complete explicit representation requires not only
3011 * an explicit representation, but also that all local variables
3012 * that appear in this explicit representation in turn have
3013 * a complete explicit representation.
3015 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3017 int i;
3018 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3019 isl_bool marked;
3021 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3022 if (marked < 0 || marked)
3023 return isl_bool_not(marked);
3025 for (i = bmap->n_div - 1; i >= 0; --i) {
3026 isl_bool known;
3028 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3029 continue;
3030 known = isl_basic_map_div_is_known(bmap, i);
3031 if (known < 0 || !known)
3032 return known;
3035 return isl_bool_true;
3038 /* Remove all divs that are unknown or defined in terms of unknown divs.
3040 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3041 __isl_take isl_basic_map *bmap)
3043 int i;
3045 if (!bmap)
3046 return NULL;
3048 for (i = bmap->n_div - 1; i >= 0; --i) {
3049 if (isl_basic_map_div_is_known(bmap, i))
3050 continue;
3051 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3052 if (!bmap)
3053 return NULL;
3054 i = bmap->n_div;
3057 return bmap;
3060 /* Remove all divs that are unknown or defined in terms of unknown divs.
3062 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3063 __isl_take isl_basic_set *bset)
3065 return isl_basic_map_remove_unknown_divs(bset);
3068 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3070 int i;
3072 if (!map)
3073 return NULL;
3074 if (map->n == 0)
3075 return map;
3077 map = isl_map_cow(map);
3078 if (!map)
3079 return NULL;
3081 for (i = 0; i < map->n; ++i) {
3082 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3083 if (!map->p[i])
3084 goto error;
3086 return map;
3087 error:
3088 isl_map_free(map);
3089 return NULL;
3092 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3094 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3097 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3098 __isl_take isl_basic_set *bset,
3099 enum isl_dim_type type, unsigned first, unsigned n)
3101 isl_basic_map *bmap = bset_to_bmap(bset);
3102 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3103 return bset_from_bmap(bmap);
3106 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3107 enum isl_dim_type type, unsigned first, unsigned n)
3109 int i;
3111 if (n == 0)
3112 return map;
3114 map = isl_map_cow(map);
3115 if (isl_map_check_range(map, type, first, n) < 0)
3116 return isl_map_free(map);
3118 for (i = 0; i < map->n; ++i) {
3119 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3120 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3121 if (!map->p[i])
3122 goto error;
3124 map = isl_map_drop(map, type, first, n);
3125 return map;
3126 error:
3127 isl_map_free(map);
3128 return NULL;
3131 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3132 enum isl_dim_type type, unsigned first, unsigned n)
3134 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3135 type, first, n));
3138 /* Project out n inputs starting at first using Fourier-Motzkin */
3139 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3140 unsigned first, unsigned n)
3142 return isl_map_remove_dims(map, isl_dim_in, first, n);
3145 static void dump_term(struct isl_basic_map *bmap,
3146 isl_int c, int pos, FILE *out)
3148 const char *name;
3149 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3150 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3151 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3152 if (!pos)
3153 isl_int_print(out, c, 0);
3154 else {
3155 if (!isl_int_is_one(c))
3156 isl_int_print(out, c, 0);
3157 if (pos < 1 + nparam) {
3158 name = isl_space_get_dim_name(bmap->dim,
3159 isl_dim_param, pos - 1);
3160 if (name)
3161 fprintf(out, "%s", name);
3162 else
3163 fprintf(out, "p%d", pos - 1);
3164 } else if (pos < 1 + nparam + in)
3165 fprintf(out, "i%d", pos - 1 - nparam);
3166 else if (pos < 1 + nparam + dim)
3167 fprintf(out, "o%d", pos - 1 - nparam - in);
3168 else
3169 fprintf(out, "e%d", pos - 1 - nparam - dim);
3173 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3174 int sign, FILE *out)
3176 int i;
3177 int first;
3178 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3179 isl_int v;
3181 isl_int_init(v);
3182 for (i = 0, first = 1; i < len; ++i) {
3183 if (isl_int_sgn(c[i]) * sign <= 0)
3184 continue;
3185 if (!first)
3186 fprintf(out, " + ");
3187 first = 0;
3188 isl_int_abs(v, c[i]);
3189 dump_term(bmap, v, i, out);
3191 isl_int_clear(v);
3192 if (first)
3193 fprintf(out, "0");
3196 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3197 const char *op, FILE *out, int indent)
3199 int i;
3201 fprintf(out, "%*s", indent, "");
3203 dump_constraint_sign(bmap, c, 1, out);
3204 fprintf(out, " %s ", op);
3205 dump_constraint_sign(bmap, c, -1, out);
3207 fprintf(out, "\n");
3209 for (i = bmap->n_div; i < bmap->extra; ++i) {
3210 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3211 continue;
3212 fprintf(out, "%*s", indent, "");
3213 fprintf(out, "ERROR: unused div coefficient not zero\n");
3214 abort();
3218 static void dump_constraints(struct isl_basic_map *bmap,
3219 isl_int **c, unsigned n,
3220 const char *op, FILE *out, int indent)
3222 int i;
3224 for (i = 0; i < n; ++i)
3225 dump_constraint(bmap, c[i], op, out, indent);
3228 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3230 int j;
3231 int first = 1;
3232 unsigned total = isl_basic_map_total_dim(bmap);
3234 for (j = 0; j < 1 + total; ++j) {
3235 if (isl_int_is_zero(exp[j]))
3236 continue;
3237 if (!first && isl_int_is_pos(exp[j]))
3238 fprintf(out, "+");
3239 dump_term(bmap, exp[j], j, out);
3240 first = 0;
3244 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3246 int i;
3248 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3249 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3251 for (i = 0; i < bmap->n_div; ++i) {
3252 fprintf(out, "%*s", indent, "");
3253 fprintf(out, "e%d = [(", i);
3254 dump_affine(bmap, bmap->div[i]+1, out);
3255 fprintf(out, ")/");
3256 isl_int_print(out, bmap->div[i][0], 0);
3257 fprintf(out, "]\n");
3261 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3262 FILE *out, int indent)
3264 if (!bset) {
3265 fprintf(out, "null basic set\n");
3266 return;
3269 fprintf(out, "%*s", indent, "");
3270 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3271 bset->ref, bset->dim->nparam, bset->dim->n_out,
3272 bset->extra, bset->flags);
3273 dump(bset_to_bmap(bset), out, indent);
3276 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3277 FILE *out, int indent)
3279 if (!bmap) {
3280 fprintf(out, "null basic map\n");
3281 return;
3284 fprintf(out, "%*s", indent, "");
3285 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3286 "flags: %x, n_name: %d\n",
3287 bmap->ref,
3288 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3289 bmap->extra, bmap->flags, bmap->dim->n_id);
3290 dump(bmap, out, indent);
3293 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3295 unsigned total;
3296 if (!bmap)
3297 return -1;
3298 total = isl_basic_map_total_dim(bmap);
3299 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3300 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3301 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3302 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3303 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3304 return 0;
3307 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3308 unsigned flags)
3310 if (isl_space_check_is_set(space) < 0)
3311 goto error;
3312 return isl_map_alloc_space(space, n, flags);
3313 error:
3314 isl_space_free(space);
3315 return NULL;
3318 /* Make sure "map" has room for at least "n" more basic maps.
3320 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3322 int i;
3323 struct isl_map *grown = NULL;
3325 if (!map)
3326 return NULL;
3327 isl_assert(map->ctx, n >= 0, goto error);
3328 if (map->n + n <= map->size)
3329 return map;
3330 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3331 if (!grown)
3332 goto error;
3333 for (i = 0; i < map->n; ++i) {
3334 grown->p[i] = isl_basic_map_copy(map->p[i]);
3335 if (!grown->p[i])
3336 goto error;
3337 grown->n++;
3339 isl_map_free(map);
3340 return grown;
3341 error:
3342 isl_map_free(grown);
3343 isl_map_free(map);
3344 return NULL;
3347 /* Make sure "set" has room for at least "n" more basic sets.
3349 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3351 return set_from_map(isl_map_grow(set_to_map(set), n));
3354 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3356 return isl_map_from_basic_map(bset);
3359 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3361 struct isl_map *map;
3363 if (!bmap)
3364 return NULL;
3366 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3367 return isl_map_add_basic_map(map, bmap);
3370 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3371 __isl_take isl_basic_set *bset)
3373 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3374 bset_to_bmap(bset)));
3377 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3379 return isl_map_free(set);
3382 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3384 int i;
3386 if (!set) {
3387 fprintf(out, "null set\n");
3388 return;
3391 fprintf(out, "%*s", indent, "");
3392 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3393 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3394 set->flags);
3395 for (i = 0; i < set->n; ++i) {
3396 fprintf(out, "%*s", indent, "");
3397 fprintf(out, "basic set %d:\n", i);
3398 isl_basic_set_print_internal(set->p[i], out, indent+4);
3402 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3404 int i;
3406 if (!map) {
3407 fprintf(out, "null map\n");
3408 return;
3411 fprintf(out, "%*s", indent, "");
3412 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3413 "flags: %x, n_name: %d\n",
3414 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3415 map->dim->n_out, map->flags, map->dim->n_id);
3416 for (i = 0; i < map->n; ++i) {
3417 fprintf(out, "%*s", indent, "");
3418 fprintf(out, "basic map %d:\n", i);
3419 isl_basic_map_print_internal(map->p[i], out, indent+4);
3423 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3424 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3426 struct isl_basic_map *bmap_domain;
3428 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3429 goto error;
3431 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3432 isl_assert(bset->ctx,
3433 isl_basic_map_compatible_domain(bmap, bset), goto error);
3435 bmap = isl_basic_map_cow(bmap);
3436 if (!bmap)
3437 goto error;
3438 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3439 bset->n_div, bset->n_eq, bset->n_ineq);
3440 bmap_domain = isl_basic_map_from_domain(bset);
3441 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3443 bmap = isl_basic_map_simplify(bmap);
3444 return isl_basic_map_finalize(bmap);
3445 error:
3446 isl_basic_map_free(bmap);
3447 isl_basic_set_free(bset);
3448 return NULL;
3451 /* Check that the space of "bset" is the same as that of the range of "bmap".
3453 static isl_stat isl_basic_map_check_compatible_range(
3454 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3456 isl_bool ok;
3458 ok = isl_basic_map_compatible_range(bmap, bset);
3459 if (ok < 0)
3460 return isl_stat_error;
3461 if (!ok)
3462 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3463 "incompatible spaces", return isl_stat_error);
3465 return isl_stat_ok;
3468 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3469 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3471 struct isl_basic_map *bmap_range;
3473 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3474 goto error;
3476 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3477 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3478 goto error;
3480 if (isl_basic_set_plain_is_universe(bset)) {
3481 isl_basic_set_free(bset);
3482 return bmap;
3485 bmap = isl_basic_map_cow(bmap);
3486 if (!bmap)
3487 goto error;
3488 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3489 bset->n_div, bset->n_eq, bset->n_ineq);
3490 bmap_range = bset_to_bmap(bset);
3491 bmap = add_constraints(bmap, bmap_range, 0, 0);
3493 bmap = isl_basic_map_simplify(bmap);
3494 return isl_basic_map_finalize(bmap);
3495 error:
3496 isl_basic_map_free(bmap);
3497 isl_basic_set_free(bset);
3498 return NULL;
3501 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3502 __isl_keep isl_vec *vec)
3504 int i;
3505 unsigned total;
3506 isl_int s;
3508 if (!bmap || !vec)
3509 return isl_bool_error;
3511 total = 1 + isl_basic_map_total_dim(bmap);
3512 if (total != vec->size)
3513 return isl_bool_false;
3515 isl_int_init(s);
3517 for (i = 0; i < bmap->n_eq; ++i) {
3518 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3519 if (!isl_int_is_zero(s)) {
3520 isl_int_clear(s);
3521 return isl_bool_false;
3525 for (i = 0; i < bmap->n_ineq; ++i) {
3526 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3527 if (isl_int_is_neg(s)) {
3528 isl_int_clear(s);
3529 return isl_bool_false;
3533 isl_int_clear(s);
3535 return isl_bool_true;
3538 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3539 __isl_keep isl_vec *vec)
3541 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3544 __isl_give isl_basic_map *isl_basic_map_intersect(
3545 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3547 struct isl_vec *sample = NULL;
3549 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3550 goto error;
3551 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3552 isl_space_dim(bmap1->dim, isl_dim_param) &&
3553 isl_space_dim(bmap2->dim, isl_dim_all) !=
3554 isl_space_dim(bmap2->dim, isl_dim_param))
3555 return isl_basic_map_intersect(bmap2, bmap1);
3557 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3558 isl_space_dim(bmap2->dim, isl_dim_param))
3559 isl_assert(bmap1->ctx,
3560 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3562 if (isl_basic_map_plain_is_empty(bmap1)) {
3563 isl_basic_map_free(bmap2);
3564 return bmap1;
3566 if (isl_basic_map_plain_is_empty(bmap2)) {
3567 isl_basic_map_free(bmap1);
3568 return bmap2;
3571 if (bmap1->sample &&
3572 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3573 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3574 sample = isl_vec_copy(bmap1->sample);
3575 else if (bmap2->sample &&
3576 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3577 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3578 sample = isl_vec_copy(bmap2->sample);
3580 bmap1 = isl_basic_map_cow(bmap1);
3581 if (!bmap1)
3582 goto error;
3583 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3584 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3585 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3587 if (!bmap1)
3588 isl_vec_free(sample);
3589 else if (sample) {
3590 isl_vec_free(bmap1->sample);
3591 bmap1->sample = sample;
3594 bmap1 = isl_basic_map_simplify(bmap1);
3595 return isl_basic_map_finalize(bmap1);
3596 error:
3597 if (sample)
3598 isl_vec_free(sample);
3599 isl_basic_map_free(bmap1);
3600 isl_basic_map_free(bmap2);
3601 return NULL;
3604 struct isl_basic_set *isl_basic_set_intersect(
3605 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3607 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3608 bset_to_bmap(bset2)));
3611 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3612 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3614 return isl_basic_set_intersect(bset1, bset2);
3617 /* Special case of isl_map_intersect, where both map1 and map2
3618 * are convex, without any divs and such that either map1 or map2
3619 * contains a single constraint. This constraint is then simply
3620 * added to the other map.
3622 static __isl_give isl_map *map_intersect_add_constraint(
3623 __isl_take isl_map *map1, __isl_take isl_map *map2)
3625 isl_assert(map1->ctx, map1->n == 1, goto error);
3626 isl_assert(map2->ctx, map1->n == 1, goto error);
3627 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3628 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3630 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3631 return isl_map_intersect(map2, map1);
3633 map1 = isl_map_cow(map1);
3634 if (!map1)
3635 goto error;
3636 if (isl_map_plain_is_empty(map1)) {
3637 isl_map_free(map2);
3638 return map1;
3640 if (map2->p[0]->n_eq == 1)
3641 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3642 else
3643 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3644 map2->p[0]->ineq[0]);
3646 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3647 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3648 if (!map1->p[0])
3649 goto error;
3651 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3652 isl_basic_map_free(map1->p[0]);
3653 map1->n = 0;
3656 isl_map_free(map2);
3658 map1 = isl_map_unmark_normalized(map1);
3659 return map1;
3660 error:
3661 isl_map_free(map1);
3662 isl_map_free(map2);
3663 return NULL;
3666 /* map2 may be either a parameter domain or a map living in the same
3667 * space as map1.
3669 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3670 __isl_take isl_map *map2)
3672 unsigned flags = 0;
3673 isl_bool equal;
3674 isl_map *result;
3675 int i, j;
3677 if (!map1 || !map2)
3678 goto error;
3680 if ((isl_map_plain_is_empty(map1) ||
3681 isl_map_plain_is_universe(map2)) &&
3682 isl_space_is_equal(map1->dim, map2->dim)) {
3683 isl_map_free(map2);
3684 return map1;
3686 if ((isl_map_plain_is_empty(map2) ||
3687 isl_map_plain_is_universe(map1)) &&
3688 isl_space_is_equal(map1->dim, map2->dim)) {
3689 isl_map_free(map1);
3690 return map2;
3693 if (map1->n == 1 && map2->n == 1 &&
3694 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3695 isl_space_is_equal(map1->dim, map2->dim) &&
3696 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3697 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3698 return map_intersect_add_constraint(map1, map2);
3700 equal = isl_map_plain_is_equal(map1, map2);
3701 if (equal < 0)
3702 goto error;
3703 if (equal) {
3704 isl_map_free(map2);
3705 return map1;
3708 if (isl_space_dim(map2->dim, isl_dim_all) !=
3709 isl_space_dim(map2->dim, isl_dim_param))
3710 isl_assert(map1->ctx,
3711 isl_space_is_equal(map1->dim, map2->dim), goto error);
3713 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3714 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3715 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3717 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3718 map1->n * map2->n, flags);
3719 if (!result)
3720 goto error;
3721 for (i = 0; i < map1->n; ++i)
3722 for (j = 0; j < map2->n; ++j) {
3723 struct isl_basic_map *part;
3724 part = isl_basic_map_intersect(
3725 isl_basic_map_copy(map1->p[i]),
3726 isl_basic_map_copy(map2->p[j]));
3727 if (isl_basic_map_is_empty(part) < 0)
3728 part = isl_basic_map_free(part);
3729 result = isl_map_add_basic_map(result, part);
3730 if (!result)
3731 goto error;
3733 isl_map_free(map1);
3734 isl_map_free(map2);
3735 return result;
3736 error:
3737 isl_map_free(map1);
3738 isl_map_free(map2);
3739 return NULL;
3742 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3743 __isl_take isl_map *map2)
3745 if (!map1 || !map2)
3746 goto error;
3747 if (!isl_space_is_equal(map1->dim, map2->dim))
3748 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3749 "spaces don't match", goto error);
3750 return map_intersect_internal(map1, map2);
3751 error:
3752 isl_map_free(map1);
3753 isl_map_free(map2);
3754 return NULL;
3757 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3758 __isl_take isl_map *map2)
3760 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3763 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3765 return set_from_map(isl_map_intersect(set_to_map(set1),
3766 set_to_map(set2)));
3769 /* map_intersect_internal accepts intersections
3770 * with parameter domains, so we can just call that function.
3772 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3773 __isl_take isl_set *params)
3775 return map_intersect_internal(map, params);
3778 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3779 __isl_take isl_map *map2)
3781 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3784 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3785 __isl_take isl_set *params)
3787 return isl_map_intersect_params(set, params);
3790 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3792 isl_space *space;
3793 unsigned pos, n1, n2;
3795 if (!bmap)
3796 return NULL;
3797 bmap = isl_basic_map_cow(bmap);
3798 if (!bmap)
3799 return NULL;
3800 space = isl_space_reverse(isl_space_copy(bmap->dim));
3801 pos = isl_basic_map_offset(bmap, isl_dim_in);
3802 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3803 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3804 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3805 return isl_basic_map_reset_space(bmap, space);
3808 static __isl_give isl_basic_map *basic_map_space_reset(
3809 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3811 isl_space *space;
3813 if (!bmap)
3814 return NULL;
3815 if (!isl_space_is_named_or_nested(bmap->dim, type))
3816 return bmap;
3818 space = isl_basic_map_get_space(bmap);
3819 space = isl_space_reset(space, type);
3820 bmap = isl_basic_map_reset_space(bmap, space);
3821 return bmap;
3824 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3825 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3826 unsigned pos, unsigned n)
3828 isl_bool rational;
3829 isl_space *res_space;
3830 struct isl_basic_map *res;
3831 struct isl_dim_map *dim_map;
3832 unsigned total, off;
3833 enum isl_dim_type t;
3835 if (n == 0)
3836 return basic_map_space_reset(bmap, type);
3838 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3839 type, pos, n);
3840 if (!res_space)
3841 return isl_basic_map_free(bmap);
3843 total = isl_basic_map_total_dim(bmap) + n;
3844 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3845 off = 0;
3846 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3847 if (t != type) {
3848 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3849 } else {
3850 unsigned size = isl_basic_map_dim(bmap, t);
3851 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3852 0, pos, off);
3853 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3854 pos, size - pos, off + pos + n);
3856 off += isl_space_dim(res_space, t);
3858 isl_dim_map_div(dim_map, bmap, off);
3860 res = isl_basic_map_alloc_space(res_space,
3861 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3862 rational = isl_basic_map_is_rational(bmap);
3863 if (rational < 0)
3864 res = isl_basic_map_free(res);
3865 if (rational)
3866 res = isl_basic_map_set_rational(res);
3867 if (isl_basic_map_plain_is_empty(bmap)) {
3868 isl_basic_map_free(bmap);
3869 free(dim_map);
3870 return isl_basic_map_set_to_empty(res);
3872 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3873 return isl_basic_map_finalize(res);
3876 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3877 __isl_take isl_basic_set *bset,
3878 enum isl_dim_type type, unsigned pos, unsigned n)
3880 return isl_basic_map_insert_dims(bset, type, pos, n);
3883 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3884 enum isl_dim_type type, unsigned n)
3886 if (!bmap)
3887 return NULL;
3888 return isl_basic_map_insert_dims(bmap, type,
3889 isl_basic_map_dim(bmap, type), n);
3892 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3893 enum isl_dim_type type, unsigned n)
3895 if (!bset)
3896 return NULL;
3897 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3898 return isl_basic_map_add_dims(bset, type, n);
3899 error:
3900 isl_basic_set_free(bset);
3901 return NULL;
3904 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3905 enum isl_dim_type type)
3907 isl_space *space;
3909 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3910 return map;
3912 space = isl_map_get_space(map);
3913 space = isl_space_reset(space, type);
3914 map = isl_map_reset_space(map, space);
3915 return map;
3918 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3919 enum isl_dim_type type, unsigned pos, unsigned n)
3921 int i;
3923 if (n == 0)
3924 return map_space_reset(map, type);
3926 map = isl_map_cow(map);
3927 if (!map)
3928 return NULL;
3930 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3931 if (!map->dim)
3932 goto error;
3934 for (i = 0; i < map->n; ++i) {
3935 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3936 if (!map->p[i])
3937 goto error;
3940 return map;
3941 error:
3942 isl_map_free(map);
3943 return NULL;
3946 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3947 enum isl_dim_type type, unsigned pos, unsigned n)
3949 return isl_map_insert_dims(set, type, pos, n);
3952 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3953 enum isl_dim_type type, unsigned n)
3955 if (!map)
3956 return NULL;
3957 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3960 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3961 enum isl_dim_type type, unsigned n)
3963 if (!set)
3964 return NULL;
3965 isl_assert(set->ctx, type != isl_dim_in, goto error);
3966 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3967 error:
3968 isl_set_free(set);
3969 return NULL;
3972 __isl_give isl_basic_map *isl_basic_map_move_dims(
3973 __isl_take isl_basic_map *bmap,
3974 enum isl_dim_type dst_type, unsigned dst_pos,
3975 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3977 struct isl_dim_map *dim_map;
3978 struct isl_basic_map *res;
3979 enum isl_dim_type t;
3980 unsigned total, off;
3982 if (!bmap)
3983 return NULL;
3984 if (n == 0) {
3985 bmap = isl_basic_map_reset(bmap, src_type);
3986 bmap = isl_basic_map_reset(bmap, dst_type);
3987 return bmap;
3990 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3991 return isl_basic_map_free(bmap);
3993 if (dst_type == src_type && dst_pos == src_pos)
3994 return bmap;
3996 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3998 if (pos(bmap->dim, dst_type) + dst_pos ==
3999 pos(bmap->dim, src_type) + src_pos +
4000 ((src_type < dst_type) ? n : 0)) {
4001 bmap = isl_basic_map_cow(bmap);
4002 if (!bmap)
4003 return NULL;
4005 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4006 src_type, src_pos, n);
4007 if (!bmap->dim)
4008 goto error;
4010 bmap = isl_basic_map_finalize(bmap);
4012 return bmap;
4015 total = isl_basic_map_total_dim(bmap);
4016 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4018 off = 0;
4019 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4020 unsigned size = isl_space_dim(bmap->dim, t);
4021 if (t == dst_type) {
4022 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4023 0, dst_pos, off);
4024 off += dst_pos;
4025 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4026 src_pos, n, off);
4027 off += n;
4028 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4029 dst_pos, size - dst_pos, off);
4030 off += size - dst_pos;
4031 } else if (t == src_type) {
4032 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4033 0, src_pos, off);
4034 off += src_pos;
4035 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4036 src_pos + n, size - src_pos - n, off);
4037 off += size - src_pos - n;
4038 } else {
4039 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4040 off += size;
4043 isl_dim_map_div(dim_map, bmap, off);
4045 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4046 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4047 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4048 if (!bmap)
4049 goto error;
4051 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4052 src_type, src_pos, n);
4053 if (!bmap->dim)
4054 goto error;
4056 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4057 bmap = isl_basic_map_gauss(bmap, NULL);
4058 bmap = isl_basic_map_finalize(bmap);
4060 return bmap;
4061 error:
4062 isl_basic_map_free(bmap);
4063 return NULL;
4066 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4067 enum isl_dim_type dst_type, unsigned dst_pos,
4068 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4070 isl_basic_map *bmap = bset_to_bmap(bset);
4071 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4072 src_type, src_pos, n);
4073 return bset_from_bmap(bmap);
4076 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4077 enum isl_dim_type dst_type, unsigned dst_pos,
4078 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4080 if (!set)
4081 return NULL;
4082 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4083 return set_from_map(isl_map_move_dims(set_to_map(set),
4084 dst_type, dst_pos, src_type, src_pos, n));
4085 error:
4086 isl_set_free(set);
4087 return NULL;
4090 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4091 enum isl_dim_type dst_type, unsigned dst_pos,
4092 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4094 int i;
4096 if (n == 0) {
4097 map = isl_map_reset(map, src_type);
4098 map = isl_map_reset(map, dst_type);
4099 return map;
4102 if (isl_map_check_range(map, src_type, src_pos, n))
4103 return isl_map_free(map);
4105 if (dst_type == src_type && dst_pos == src_pos)
4106 return map;
4108 isl_assert(map->ctx, dst_type != src_type, goto error);
4110 map = isl_map_cow(map);
4111 if (!map)
4112 return NULL;
4114 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4115 if (!map->dim)
4116 goto error;
4118 for (i = 0; i < map->n; ++i) {
4119 map->p[i] = isl_basic_map_move_dims(map->p[i],
4120 dst_type, dst_pos,
4121 src_type, src_pos, n);
4122 if (!map->p[i])
4123 goto error;
4126 return map;
4127 error:
4128 isl_map_free(map);
4129 return NULL;
4132 /* Move the specified dimensions to the last columns right before
4133 * the divs. Don't change the dimension specification of bmap.
4134 * That's the responsibility of the caller.
4136 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4137 enum isl_dim_type type, unsigned first, unsigned n)
4139 struct isl_dim_map *dim_map;
4140 struct isl_basic_map *res;
4141 enum isl_dim_type t;
4142 unsigned total, off;
4144 if (!bmap)
4145 return NULL;
4146 if (pos(bmap->dim, type) + first + n ==
4147 1 + isl_space_dim(bmap->dim, isl_dim_all))
4148 return bmap;
4150 total = isl_basic_map_total_dim(bmap);
4151 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4153 off = 0;
4154 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4155 unsigned size = isl_space_dim(bmap->dim, t);
4156 if (t == type) {
4157 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4158 0, first, off);
4159 off += first;
4160 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4161 first, n, total - bmap->n_div - n);
4162 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4163 first + n, size - (first + n), off);
4164 off += size - (first + n);
4165 } else {
4166 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4167 off += size;
4170 isl_dim_map_div(dim_map, bmap, off + n);
4172 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4173 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4174 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4175 return res;
4178 /* Insert "n" rows in the divs of "bmap".
4180 * The number of columns is not changed, which means that the last
4181 * dimensions of "bmap" are being reintepreted as the new divs.
4182 * The space of "bmap" is not adjusted, however, which means
4183 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4184 * from the space of "bmap" is the responsibility of the caller.
4186 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4187 int n)
4189 int i;
4190 size_t row_size;
4191 isl_int **new_div;
4192 isl_int *old;
4194 bmap = isl_basic_map_cow(bmap);
4195 if (!bmap)
4196 return NULL;
4198 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4199 old = bmap->block2.data;
4200 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4201 (bmap->extra + n) * (1 + row_size));
4202 if (!bmap->block2.data)
4203 return isl_basic_map_free(bmap);
4204 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4205 if (!new_div)
4206 return isl_basic_map_free(bmap);
4207 for (i = 0; i < n; ++i) {
4208 new_div[i] = bmap->block2.data +
4209 (bmap->extra + i) * (1 + row_size);
4210 isl_seq_clr(new_div[i], 1 + row_size);
4212 for (i = 0; i < bmap->extra; ++i)
4213 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4214 free(bmap->div);
4215 bmap->div = new_div;
4216 bmap->n_div += n;
4217 bmap->extra += n;
4219 return bmap;
4222 /* Drop constraints from "bmap" that only involve the variables
4223 * of "type" in the range [first, first + n] that are not related
4224 * to any of the variables outside that interval.
4225 * These constraints cannot influence the values for the variables
4226 * outside the interval, except in case they cause "bmap" to be empty.
4227 * Only drop the constraints if "bmap" is known to be non-empty.
4229 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4230 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4231 unsigned first, unsigned n)
4233 int i;
4234 int *groups;
4235 unsigned dim, n_div;
4236 isl_bool non_empty;
4238 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4239 if (non_empty < 0)
4240 return isl_basic_map_free(bmap);
4241 if (!non_empty)
4242 return bmap;
4244 dim = isl_basic_map_dim(bmap, isl_dim_all);
4245 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4246 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4247 if (!groups)
4248 return isl_basic_map_free(bmap);
4249 first += isl_basic_map_offset(bmap, type) - 1;
4250 for (i = 0; i < first; ++i)
4251 groups[i] = -1;
4252 for (i = first + n; i < dim - n_div; ++i)
4253 groups[i] = -1;
4255 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4257 return bmap;
4260 /* Turn the n dimensions of type type, starting at first
4261 * into existentially quantified variables.
4263 * If a subset of the projected out variables are unrelated
4264 * to any of the variables that remain, then the constraints
4265 * involving this subset are simply dropped first.
4267 __isl_give isl_basic_map *isl_basic_map_project_out(
4268 __isl_take isl_basic_map *bmap,
4269 enum isl_dim_type type, unsigned first, unsigned n)
4271 isl_bool empty;
4273 if (n == 0)
4274 return basic_map_space_reset(bmap, type);
4275 if (type == isl_dim_div)
4276 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4277 "cannot project out existentially quantified variables",
4278 return isl_basic_map_free(bmap));
4280 empty = isl_basic_map_plain_is_empty(bmap);
4281 if (empty < 0)
4282 return isl_basic_map_free(bmap);
4283 if (empty)
4284 bmap = isl_basic_map_set_to_empty(bmap);
4286 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4287 if (!bmap)
4288 return NULL;
4290 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4291 return isl_basic_map_remove_dims(bmap, type, first, n);
4293 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4294 return isl_basic_map_free(bmap);
4296 bmap = move_last(bmap, type, first, n);
4297 bmap = isl_basic_map_cow(bmap);
4298 bmap = insert_div_rows(bmap, n);
4299 if (!bmap)
4300 return NULL;
4302 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4303 if (!bmap->dim)
4304 goto error;
4305 bmap = isl_basic_map_simplify(bmap);
4306 bmap = isl_basic_map_drop_redundant_divs(bmap);
4307 return isl_basic_map_finalize(bmap);
4308 error:
4309 isl_basic_map_free(bmap);
4310 return NULL;
4313 /* Turn the n dimensions of type type, starting at first
4314 * into existentially quantified variables.
4316 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4317 enum isl_dim_type type, unsigned first, unsigned n)
4319 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4320 type, first, n));
4323 /* Turn the n dimensions of type type, starting at first
4324 * into existentially quantified variables.
4326 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4327 enum isl_dim_type type, unsigned first, unsigned n)
4329 int i;
4331 if (n == 0)
4332 return map_space_reset(map, type);
4334 if (isl_map_check_range(map, type, first, n) < 0)
4335 return isl_map_free(map);
4337 map = isl_map_cow(map);
4338 if (!map)
4339 return NULL;
4341 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4342 if (!map->dim)
4343 goto error;
4345 for (i = 0; i < map->n; ++i) {
4346 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4347 if (!map->p[i])
4348 goto error;
4351 return map;
4352 error:
4353 isl_map_free(map);
4354 return NULL;
4357 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4358 * into existentially quantified variables.
4360 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4361 enum isl_dim_type type, unsigned first, unsigned n)
4363 unsigned dim;
4365 if (isl_map_check_range(map, type, first, n) < 0)
4366 return isl_map_free(map);
4367 dim = isl_map_dim(map, type);
4368 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4369 map = isl_map_project_out(map, type, 0, first);
4370 return map;
4373 /* Turn the n dimensions of type type, starting at first
4374 * into existentially quantified variables.
4376 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4377 enum isl_dim_type type, unsigned first, unsigned n)
4379 return set_from_map(isl_map_project_out(set_to_map(set),
4380 type, first, n));
4383 /* Return a map that projects the elements in "set" onto their
4384 * "n" set dimensions starting at "first".
4385 * "type" should be equal to isl_dim_set.
4387 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4388 enum isl_dim_type type, unsigned first, unsigned n)
4390 int i;
4391 int dim;
4392 isl_map *map;
4394 if (!set)
4395 return NULL;
4396 if (type != isl_dim_set)
4397 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4398 "only set dimensions can be projected out", goto error);
4399 dim = isl_set_dim(set, isl_dim_set);
4400 if (first + n > dim || first + n < first)
4401 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4402 "index out of bounds", goto error);
4404 map = isl_map_from_domain(set);
4405 map = isl_map_add_dims(map, isl_dim_out, n);
4406 for (i = 0; i < n; ++i)
4407 map = isl_map_equate(map, isl_dim_in, first + i,
4408 isl_dim_out, i);
4409 return map;
4410 error:
4411 isl_set_free(set);
4412 return NULL;
4415 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4416 unsigned n)
4418 int i, j;
4420 for (i = 0; i < n; ++i) {
4421 j = isl_basic_map_alloc_div(bmap);
4422 if (j < 0)
4423 goto error;
4424 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4426 return bmap;
4427 error:
4428 isl_basic_map_free(bmap);
4429 return NULL;
4432 struct isl_basic_map *isl_basic_map_apply_range(
4433 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4435 isl_space *space_result = NULL;
4436 struct isl_basic_map *bmap;
4437 unsigned n_in, n_out, n, nparam, total, pos;
4438 struct isl_dim_map *dim_map1, *dim_map2;
4440 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4441 goto error;
4442 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4443 bmap2->dim, isl_dim_in))
4444 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4445 "spaces don't match", goto error);
4447 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4448 isl_space_copy(bmap2->dim));
4450 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4451 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4452 n = isl_basic_map_dim(bmap1, isl_dim_out);
4453 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4455 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4456 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4457 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4458 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4459 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4460 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4461 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4462 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4463 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4464 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4465 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4467 bmap = isl_basic_map_alloc_space(space_result,
4468 bmap1->n_div + bmap2->n_div + n,
4469 bmap1->n_eq + bmap2->n_eq,
4470 bmap1->n_ineq + bmap2->n_ineq);
4471 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4472 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4473 bmap = add_divs(bmap, n);
4474 bmap = isl_basic_map_simplify(bmap);
4475 bmap = isl_basic_map_drop_redundant_divs(bmap);
4476 return isl_basic_map_finalize(bmap);
4477 error:
4478 isl_basic_map_free(bmap1);
4479 isl_basic_map_free(bmap2);
4480 return NULL;
4483 struct isl_basic_set *isl_basic_set_apply(
4484 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4486 if (!bset || !bmap)
4487 goto error;
4489 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4490 goto error);
4492 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4493 bmap));
4494 error:
4495 isl_basic_set_free(bset);
4496 isl_basic_map_free(bmap);
4497 return NULL;
4500 struct isl_basic_map *isl_basic_map_apply_domain(
4501 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4503 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4504 goto error;
4505 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4506 bmap2->dim, isl_dim_in))
4507 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4508 "spaces don't match", goto error);
4510 bmap1 = isl_basic_map_reverse(bmap1);
4511 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4512 return isl_basic_map_reverse(bmap1);
4513 error:
4514 isl_basic_map_free(bmap1);
4515 isl_basic_map_free(bmap2);
4516 return NULL;
4519 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4520 * A \cap B -> f(A) + f(B)
4522 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4523 __isl_take isl_basic_map *bmap2)
4525 unsigned n_in, n_out, nparam, total, pos;
4526 struct isl_basic_map *bmap = NULL;
4527 struct isl_dim_map *dim_map1, *dim_map2;
4528 int i;
4530 if (!bmap1 || !bmap2)
4531 goto error;
4533 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4534 goto error);
4536 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4537 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4538 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4540 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4541 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4542 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4543 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4544 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4545 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4546 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4547 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4548 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4549 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4550 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4552 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4553 bmap1->n_div + bmap2->n_div + 2 * n_out,
4554 bmap1->n_eq + bmap2->n_eq + n_out,
4555 bmap1->n_ineq + bmap2->n_ineq);
4556 for (i = 0; i < n_out; ++i) {
4557 int j = isl_basic_map_alloc_equality(bmap);
4558 if (j < 0)
4559 goto error;
4560 isl_seq_clr(bmap->eq[j], 1+total);
4561 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4562 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4563 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4565 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4566 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4567 bmap = add_divs(bmap, 2 * n_out);
4569 bmap = isl_basic_map_simplify(bmap);
4570 return isl_basic_map_finalize(bmap);
4571 error:
4572 isl_basic_map_free(bmap);
4573 isl_basic_map_free(bmap1);
4574 isl_basic_map_free(bmap2);
4575 return NULL;
4578 /* Given two maps A -> f(A) and B -> g(B), construct a map
4579 * A \cap B -> f(A) + f(B)
4581 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4582 __isl_take isl_map *map2)
4584 struct isl_map *result;
4585 int i, j;
4587 if (!map1 || !map2)
4588 goto error;
4590 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4592 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4593 map1->n * map2->n, 0);
4594 if (!result)
4595 goto error;
4596 for (i = 0; i < map1->n; ++i)
4597 for (j = 0; j < map2->n; ++j) {
4598 struct isl_basic_map *part;
4599 part = isl_basic_map_sum(
4600 isl_basic_map_copy(map1->p[i]),
4601 isl_basic_map_copy(map2->p[j]));
4602 if (isl_basic_map_is_empty(part))
4603 isl_basic_map_free(part);
4604 else
4605 result = isl_map_add_basic_map(result, part);
4606 if (!result)
4607 goto error;
4609 isl_map_free(map1);
4610 isl_map_free(map2);
4611 return result;
4612 error:
4613 isl_map_free(map1);
4614 isl_map_free(map2);
4615 return NULL;
4618 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4619 __isl_take isl_set *set2)
4621 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4624 /* Given a basic map A -> f(A), construct A -> -f(A).
4626 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4628 int i, j;
4629 unsigned off, n;
4631 bmap = isl_basic_map_cow(bmap);
4632 if (!bmap)
4633 return NULL;
4635 n = isl_basic_map_dim(bmap, isl_dim_out);
4636 off = isl_basic_map_offset(bmap, isl_dim_out);
4637 for (i = 0; i < bmap->n_eq; ++i)
4638 for (j = 0; j < n; ++j)
4639 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4640 for (i = 0; i < bmap->n_ineq; ++i)
4641 for (j = 0; j < n; ++j)
4642 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4643 for (i = 0; i < bmap->n_div; ++i)
4644 for (j = 0; j < n; ++j)
4645 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4646 bmap = isl_basic_map_gauss(bmap, NULL);
4647 return isl_basic_map_finalize(bmap);
4650 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4652 return isl_basic_map_neg(bset);
4655 /* Given a map A -> f(A), construct A -> -f(A).
4657 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4659 int i;
4661 map = isl_map_cow(map);
4662 if (!map)
4663 return NULL;
4665 for (i = 0; i < map->n; ++i) {
4666 map->p[i] = isl_basic_map_neg(map->p[i]);
4667 if (!map->p[i])
4668 goto error;
4671 return map;
4672 error:
4673 isl_map_free(map);
4674 return NULL;
4677 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4679 return set_from_map(isl_map_neg(set_to_map(set)));
4682 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4683 * A -> floor(f(A)/d).
4685 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4686 isl_int d)
4688 unsigned n_in, n_out, nparam, total, pos;
4689 struct isl_basic_map *result = NULL;
4690 struct isl_dim_map *dim_map;
4691 int i;
4693 if (!bmap)
4694 return NULL;
4696 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4697 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4698 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4700 total = nparam + n_in + n_out + bmap->n_div + n_out;
4701 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4702 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4703 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4704 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4705 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4707 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4708 bmap->n_div + n_out,
4709 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4710 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4711 result = add_divs(result, n_out);
4712 for (i = 0; i < n_out; ++i) {
4713 int j;
4714 j = isl_basic_map_alloc_inequality(result);
4715 if (j < 0)
4716 goto error;
4717 isl_seq_clr(result->ineq[j], 1+total);
4718 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4719 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4720 j = isl_basic_map_alloc_inequality(result);
4721 if (j < 0)
4722 goto error;
4723 isl_seq_clr(result->ineq[j], 1+total);
4724 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4725 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4726 isl_int_sub_ui(result->ineq[j][0], d, 1);
4729 result = isl_basic_map_simplify(result);
4730 return isl_basic_map_finalize(result);
4731 error:
4732 isl_basic_map_free(result);
4733 return NULL;
4736 /* Given a map A -> f(A) and an integer d, construct a map
4737 * A -> floor(f(A)/d).
4739 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4741 int i;
4743 map = isl_map_cow(map);
4744 if (!map)
4745 return NULL;
4747 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4748 for (i = 0; i < map->n; ++i) {
4749 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4750 if (!map->p[i])
4751 goto error;
4753 map = isl_map_unmark_normalized(map);
4755 return map;
4756 error:
4757 isl_map_free(map);
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_val(__isl_take isl_map *map,
4765 __isl_take isl_val *d)
4767 if (!map || !d)
4768 goto error;
4769 if (!isl_val_is_int(d))
4770 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4771 "expecting integer denominator", goto error);
4772 map = isl_map_floordiv(map, d->n);
4773 isl_val_free(d);
4774 return map;
4775 error:
4776 isl_map_free(map);
4777 isl_val_free(d);
4778 return NULL;
4781 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4782 unsigned pos)
4784 int i;
4785 unsigned nparam;
4786 unsigned n_in;
4788 i = isl_basic_map_alloc_equality(bmap);
4789 if (i < 0)
4790 goto error;
4791 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4792 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4793 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4794 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4795 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4796 return isl_basic_map_finalize(bmap);
4797 error:
4798 isl_basic_map_free(bmap);
4799 return NULL;
4802 /* Add a constraint to "bmap" expressing i_pos < o_pos
4804 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4805 unsigned pos)
4807 int i;
4808 unsigned nparam;
4809 unsigned n_in;
4811 i = isl_basic_map_alloc_inequality(bmap);
4812 if (i < 0)
4813 goto error;
4814 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4815 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4816 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4817 isl_int_set_si(bmap->ineq[i][0], -1);
4818 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4819 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4820 return isl_basic_map_finalize(bmap);
4821 error:
4822 isl_basic_map_free(bmap);
4823 return NULL;
4826 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4828 static __isl_give isl_basic_map *var_less_or_equal(
4829 __isl_take isl_basic_map *bmap, unsigned pos)
4831 int i;
4832 unsigned nparam;
4833 unsigned n_in;
4835 i = isl_basic_map_alloc_inequality(bmap);
4836 if (i < 0)
4837 goto error;
4838 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4839 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4840 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4841 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4842 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4843 return isl_basic_map_finalize(bmap);
4844 error:
4845 isl_basic_map_free(bmap);
4846 return NULL;
4849 /* Add a constraint to "bmap" expressing i_pos > o_pos
4851 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4852 unsigned pos)
4854 int i;
4855 unsigned nparam;
4856 unsigned n_in;
4858 i = isl_basic_map_alloc_inequality(bmap);
4859 if (i < 0)
4860 goto error;
4861 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4862 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4863 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4864 isl_int_set_si(bmap->ineq[i][0], -1);
4865 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4866 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4867 return isl_basic_map_finalize(bmap);
4868 error:
4869 isl_basic_map_free(bmap);
4870 return NULL;
4873 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4875 static __isl_give isl_basic_map *var_more_or_equal(
4876 __isl_take isl_basic_map *bmap, unsigned pos)
4878 int i;
4879 unsigned nparam;
4880 unsigned n_in;
4882 i = isl_basic_map_alloc_inequality(bmap);
4883 if (i < 0)
4884 goto error;
4885 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4886 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4887 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4888 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4889 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4890 return isl_basic_map_finalize(bmap);
4891 error:
4892 isl_basic_map_free(bmap);
4893 return NULL;
4896 __isl_give isl_basic_map *isl_basic_map_equal(
4897 __isl_take isl_space *space, unsigned n_equal)
4899 int i;
4900 struct isl_basic_map *bmap;
4901 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4902 if (!bmap)
4903 return NULL;
4904 for (i = 0; i < n_equal && bmap; ++i)
4905 bmap = var_equal(bmap, i);
4906 return isl_basic_map_finalize(bmap);
4909 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4911 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4912 unsigned pos)
4914 int i;
4915 struct isl_basic_map *bmap;
4916 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4917 if (!bmap)
4918 return NULL;
4919 for (i = 0; i < pos && bmap; ++i)
4920 bmap = var_equal(bmap, i);
4921 if (bmap)
4922 bmap = var_less(bmap, pos);
4923 return isl_basic_map_finalize(bmap);
4926 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4928 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4929 __isl_take isl_space *space, unsigned pos)
4931 int i;
4932 isl_basic_map *bmap;
4934 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4935 for (i = 0; i < pos; ++i)
4936 bmap = var_equal(bmap, i);
4937 bmap = var_less_or_equal(bmap, pos);
4938 return isl_basic_map_finalize(bmap);
4941 /* Return a relation on "space" expressing i_pos > o_pos
4943 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4944 unsigned pos)
4946 int i;
4947 struct isl_basic_map *bmap;
4948 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4949 if (!bmap)
4950 return NULL;
4951 for (i = 0; i < pos && bmap; ++i)
4952 bmap = var_equal(bmap, i);
4953 if (bmap)
4954 bmap = var_more(bmap, pos);
4955 return isl_basic_map_finalize(bmap);
4958 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4960 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4961 __isl_take isl_space *space, unsigned pos)
4963 int i;
4964 isl_basic_map *bmap;
4966 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4967 for (i = 0; i < pos; ++i)
4968 bmap = var_equal(bmap, i);
4969 bmap = var_more_or_equal(bmap, pos);
4970 return isl_basic_map_finalize(bmap);
4973 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4974 unsigned n, int equal)
4976 struct isl_map *map;
4977 int i;
4979 if (n == 0 && equal)
4980 return isl_map_universe(space);
4982 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4984 for (i = 0; i + 1 < n; ++i)
4985 map = isl_map_add_basic_map(map,
4986 isl_basic_map_less_at(isl_space_copy(space), i));
4987 if (n > 0) {
4988 if (equal)
4989 map = isl_map_add_basic_map(map,
4990 isl_basic_map_less_or_equal_at(space, n - 1));
4991 else
4992 map = isl_map_add_basic_map(map,
4993 isl_basic_map_less_at(space, n - 1));
4994 } else
4995 isl_space_free(space);
4997 return map;
5000 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5002 if (!space)
5003 return NULL;
5004 return map_lex_lte_first(space, space->n_out, equal);
5007 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5009 return map_lex_lte_first(dim, n, 0);
5012 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5014 return map_lex_lte_first(dim, n, 1);
5017 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5019 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5022 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5024 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5027 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5028 unsigned n, int equal)
5030 struct isl_map *map;
5031 int i;
5033 if (n == 0 && equal)
5034 return isl_map_universe(space);
5036 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5038 for (i = 0; i + 1 < n; ++i)
5039 map = isl_map_add_basic_map(map,
5040 isl_basic_map_more_at(isl_space_copy(space), i));
5041 if (n > 0) {
5042 if (equal)
5043 map = isl_map_add_basic_map(map,
5044 isl_basic_map_more_or_equal_at(space, n - 1));
5045 else
5046 map = isl_map_add_basic_map(map,
5047 isl_basic_map_more_at(space, n - 1));
5048 } else
5049 isl_space_free(space);
5051 return map;
5054 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5056 if (!space)
5057 return NULL;
5058 return map_lex_gte_first(space, space->n_out, equal);
5061 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5063 return map_lex_gte_first(dim, n, 0);
5066 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5068 return map_lex_gte_first(dim, n, 1);
5071 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5073 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5076 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5078 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5081 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5082 __isl_take isl_set *set2)
5084 isl_map *map;
5085 map = isl_map_lex_le(isl_set_get_space(set1));
5086 map = isl_map_intersect_domain(map, set1);
5087 map = isl_map_intersect_range(map, set2);
5088 return map;
5091 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5092 __isl_take isl_set *set2)
5094 isl_map *map;
5095 map = isl_map_lex_lt(isl_set_get_space(set1));
5096 map = isl_map_intersect_domain(map, set1);
5097 map = isl_map_intersect_range(map, set2);
5098 return map;
5101 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5102 __isl_take isl_set *set2)
5104 isl_map *map;
5105 map = isl_map_lex_ge(isl_set_get_space(set1));
5106 map = isl_map_intersect_domain(map, set1);
5107 map = isl_map_intersect_range(map, set2);
5108 return map;
5111 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5112 __isl_take isl_set *set2)
5114 isl_map *map;
5115 map = isl_map_lex_gt(isl_set_get_space(set1));
5116 map = isl_map_intersect_domain(map, set1);
5117 map = isl_map_intersect_range(map, set2);
5118 return map;
5121 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5122 __isl_take isl_map *map2)
5124 isl_map *map;
5125 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5126 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5127 map = isl_map_apply_range(map, isl_map_reverse(map2));
5128 return map;
5131 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5132 __isl_take isl_map *map2)
5134 isl_map *map;
5135 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5136 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5137 map = isl_map_apply_range(map, isl_map_reverse(map2));
5138 return map;
5141 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5142 __isl_take isl_map *map2)
5144 isl_map *map;
5145 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5146 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5147 map = isl_map_apply_range(map, isl_map_reverse(map2));
5148 return map;
5151 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5152 __isl_take isl_map *map2)
5154 isl_map *map;
5155 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5156 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5157 map = isl_map_apply_range(map, isl_map_reverse(map2));
5158 return map;
5161 /* For a div d = floor(f/m), add the constraint
5163 * f - m d >= 0
5165 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5166 unsigned pos, isl_int *div)
5168 int i;
5169 unsigned total = isl_basic_map_total_dim(bmap);
5171 i = isl_basic_map_alloc_inequality(bmap);
5172 if (i < 0)
5173 return isl_stat_error;
5174 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5175 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5177 return isl_stat_ok;
5180 /* For a div d = floor(f/m), add the constraint
5182 * -(f-(m-1)) + m d >= 0
5184 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5185 unsigned pos, isl_int *div)
5187 int i;
5188 unsigned total = isl_basic_map_total_dim(bmap);
5190 i = isl_basic_map_alloc_inequality(bmap);
5191 if (i < 0)
5192 return isl_stat_error;
5193 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5194 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5195 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5196 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5198 return isl_stat_ok;
5201 /* For a div d = floor(f/m), add the constraints
5203 * f - m d >= 0
5204 * -(f-(m-1)) + m d >= 0
5206 * Note that the second constraint is the negation of
5208 * f - m d >= m
5210 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5211 unsigned pos, isl_int *div)
5213 if (add_upper_div_constraint(bmap, pos, div) < 0)
5214 return -1;
5215 if (add_lower_div_constraint(bmap, pos, div) < 0)
5216 return -1;
5217 return 0;
5220 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5221 unsigned pos, isl_int *div)
5223 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5224 pos, div);
5227 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5228 unsigned div)
5230 unsigned total = isl_basic_map_total_dim(bmap);
5231 unsigned div_pos = total - bmap->n_div + div;
5233 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5234 bmap->div[div]);
5237 /* For each known div d = floor(f/m), add the constraints
5239 * f - m d >= 0
5240 * -(f-(m-1)) + m d >= 0
5242 * Remove duplicate constraints in case of some these div constraints
5243 * already appear in "bmap".
5245 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5246 __isl_take isl_basic_map *bmap)
5248 unsigned n_div;
5250 if (!bmap)
5251 return NULL;
5252 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5253 if (n_div == 0)
5254 return bmap;
5256 bmap = add_known_div_constraints(bmap);
5257 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5258 bmap = isl_basic_map_finalize(bmap);
5259 return bmap;
5262 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5264 * In particular, if this div is of the form d = floor(f/m),
5265 * then add the constraint
5267 * f - m d >= 0
5269 * if sign < 0 or the constraint
5271 * -(f-(m-1)) + m d >= 0
5273 * if sign > 0.
5275 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5276 unsigned div, int sign)
5278 unsigned total;
5279 unsigned div_pos;
5281 if (!bmap)
5282 return -1;
5284 total = isl_basic_map_total_dim(bmap);
5285 div_pos = total - bmap->n_div + div;
5287 if (sign < 0)
5288 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5289 else
5290 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5293 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5294 __isl_take isl_basic_map *bmap)
5296 if (!bmap)
5297 goto error;
5298 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5299 bmap->n_div == 0 &&
5300 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5301 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5302 return bset_from_bmap(bmap);
5303 bmap = isl_basic_map_cow(bmap);
5304 if (!bmap)
5305 goto error;
5306 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5307 if (!bmap->dim)
5308 goto error;
5309 bmap->extra -= bmap->n_div;
5310 bmap->n_div = 0;
5311 bmap = isl_basic_map_finalize(bmap);
5312 return bset_from_bmap(bmap);
5313 error:
5314 isl_basic_map_free(bmap);
5315 return NULL;
5318 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5319 __isl_take isl_basic_set *bset)
5321 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5324 /* Replace each element in "list" by the result of applying
5325 * isl_basic_map_underlying_set to the element.
5327 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5328 __isl_take isl_basic_map_list *list)
5330 int i, n;
5332 if (!list)
5333 return NULL;
5335 n = isl_basic_map_list_n_basic_map(list);
5336 for (i = 0; i < n; ++i) {
5337 isl_basic_map *bmap;
5338 isl_basic_set *bset;
5340 bmap = isl_basic_map_list_get_basic_map(list, i);
5341 bset = isl_basic_set_underlying_set(bmap);
5342 list = isl_basic_set_list_set_basic_set(list, i, bset);
5345 return list;
5348 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5349 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5351 struct isl_basic_map *bmap;
5352 struct isl_ctx *ctx;
5353 unsigned total;
5354 int i;
5356 if (!bset || !like)
5357 goto error;
5358 ctx = bset->ctx;
5359 isl_assert(ctx, bset->n_div == 0, goto error);
5360 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5361 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5362 goto error);
5363 if (like->n_div == 0) {
5364 isl_space *space = isl_basic_map_get_space(like);
5365 isl_basic_map_free(like);
5366 return isl_basic_map_reset_space(bset, space);
5368 bset = isl_basic_set_cow(bset);
5369 if (!bset)
5370 goto error;
5371 total = bset->dim->n_out + bset->extra;
5372 bmap = bset_to_bmap(bset);
5373 isl_space_free(bmap->dim);
5374 bmap->dim = isl_space_copy(like->dim);
5375 if (!bmap->dim)
5376 goto error;
5377 bmap->n_div = like->n_div;
5378 bmap->extra += like->n_div;
5379 if (bmap->extra) {
5380 unsigned ltotal;
5381 isl_int **div;
5382 ltotal = total - bmap->extra + like->extra;
5383 if (ltotal > total)
5384 ltotal = total;
5385 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5386 bmap->extra * (1 + 1 + total));
5387 if (isl_blk_is_error(bmap->block2))
5388 goto error;
5389 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5390 if (!div)
5391 goto error;
5392 bmap->div = div;
5393 for (i = 0; i < bmap->extra; ++i)
5394 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5395 for (i = 0; i < like->n_div; ++i) {
5396 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5397 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5399 bmap = isl_basic_map_add_known_div_constraints(bmap);
5401 isl_basic_map_free(like);
5402 bmap = isl_basic_map_simplify(bmap);
5403 bmap = isl_basic_map_finalize(bmap);
5404 return bmap;
5405 error:
5406 isl_basic_map_free(like);
5407 isl_basic_set_free(bset);
5408 return NULL;
5411 struct isl_basic_set *isl_basic_set_from_underlying_set(
5412 struct isl_basic_set *bset, struct isl_basic_set *like)
5414 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5415 bset_to_bmap(like)));
5418 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5420 int i;
5422 map = isl_map_cow(map);
5423 if (!map)
5424 return NULL;
5425 map->dim = isl_space_cow(map->dim);
5426 if (!map->dim)
5427 goto error;
5429 for (i = 1; i < map->n; ++i)
5430 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5431 goto error);
5432 for (i = 0; i < map->n; ++i) {
5433 map->p[i] = bset_to_bmap(
5434 isl_basic_map_underlying_set(map->p[i]));
5435 if (!map->p[i])
5436 goto error;
5438 if (map->n == 0)
5439 map->dim = isl_space_underlying(map->dim, 0);
5440 else {
5441 isl_space_free(map->dim);
5442 map->dim = isl_space_copy(map->p[0]->dim);
5444 if (!map->dim)
5445 goto error;
5446 return set_from_map(map);
5447 error:
5448 isl_map_free(map);
5449 return NULL;
5452 /* Replace the space of "bmap" by "space".
5454 * If the space of "bmap" is identical to "space" (including the identifiers
5455 * of the input and output dimensions), then simply return the original input.
5457 __isl_give isl_basic_map *isl_basic_map_reset_space(
5458 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5460 isl_bool equal;
5461 isl_space *bmap_space;
5463 bmap_space = isl_basic_map_peek_space(bmap);
5464 equal = isl_space_is_equal(bmap_space, space);
5465 if (equal >= 0 && equal)
5466 equal = isl_space_has_equal_ids(bmap_space, space);
5467 if (equal < 0)
5468 goto error;
5469 if (equal) {
5470 isl_space_free(space);
5471 return bmap;
5473 bmap = isl_basic_map_cow(bmap);
5474 if (!bmap || !space)
5475 goto error;
5477 isl_space_free(bmap->dim);
5478 bmap->dim = space;
5480 bmap = isl_basic_map_finalize(bmap);
5482 return bmap;
5483 error:
5484 isl_basic_map_free(bmap);
5485 isl_space_free(space);
5486 return NULL;
5489 __isl_give isl_basic_set *isl_basic_set_reset_space(
5490 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5492 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5493 dim));
5496 /* Check that the total dimensions of "map" and "space" are the same.
5498 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5499 __isl_keep isl_space *space)
5501 unsigned dim1, dim2;
5503 if (!map || !space)
5504 return isl_stat_error;
5505 dim1 = isl_map_dim(map, isl_dim_all);
5506 dim2 = isl_space_dim(space, isl_dim_all);
5507 if (dim1 == dim2)
5508 return isl_stat_ok;
5509 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5510 "total dimensions do not match", return isl_stat_error);
5513 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5514 __isl_take isl_space *space)
5516 int i;
5518 map = isl_map_cow(map);
5519 if (!map || !space)
5520 goto error;
5522 for (i = 0; i < map->n; ++i) {
5523 map->p[i] = isl_basic_map_reset_space(map->p[i],
5524 isl_space_copy(space));
5525 if (!map->p[i])
5526 goto error;
5528 isl_space_free(map->dim);
5529 map->dim = space;
5531 return map;
5532 error:
5533 isl_map_free(map);
5534 isl_space_free(space);
5535 return NULL;
5538 /* Replace the space of "map" by "space", without modifying
5539 * the dimension of "map".
5541 * If the space of "map" is identical to "space" (including the identifiers
5542 * of the input and output dimensions), then simply return the original input.
5544 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5545 __isl_take isl_space *space)
5547 isl_bool equal;
5548 isl_space *map_space;
5550 map_space = isl_map_peek_space(map);
5551 equal = isl_space_is_equal(map_space, space);
5552 if (equal >= 0 && equal)
5553 equal = isl_space_has_equal_ids(map_space, space);
5554 if (equal < 0)
5555 goto error;
5556 if (equal) {
5557 isl_space_free(space);
5558 return map;
5560 if (check_map_space_equal_total_dim(map, space) < 0)
5561 goto error;
5562 return isl_map_reset_space(map, space);
5563 error:
5564 isl_map_free(map);
5565 isl_space_free(space);
5566 return NULL;
5569 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5570 __isl_take isl_space *dim)
5572 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5575 /* Compute the parameter domain of the given basic set.
5577 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5579 isl_bool is_params;
5580 isl_space *space;
5581 unsigned n;
5583 is_params = isl_basic_set_is_params(bset);
5584 if (is_params < 0)
5585 return isl_basic_set_free(bset);
5586 if (is_params)
5587 return bset;
5589 n = isl_basic_set_dim(bset, isl_dim_set);
5590 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5591 space = isl_basic_set_get_space(bset);
5592 space = isl_space_params(space);
5593 bset = isl_basic_set_reset_space(bset, space);
5594 return bset;
5597 /* Construct a zero-dimensional basic set with the given parameter domain.
5599 __isl_give isl_basic_set *isl_basic_set_from_params(
5600 __isl_take isl_basic_set *bset)
5602 isl_space *space;
5603 space = isl_basic_set_get_space(bset);
5604 space = isl_space_set_from_params(space);
5605 bset = isl_basic_set_reset_space(bset, space);
5606 return bset;
5609 /* Compute the parameter domain of the given set.
5611 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5613 isl_space *space;
5614 unsigned n;
5616 if (isl_set_is_params(set))
5617 return set;
5619 n = isl_set_dim(set, isl_dim_set);
5620 set = isl_set_project_out(set, isl_dim_set, 0, n);
5621 space = isl_set_get_space(set);
5622 space = isl_space_params(space);
5623 set = isl_set_reset_space(set, space);
5624 return set;
5627 /* Construct a zero-dimensional set with the given parameter domain.
5629 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5631 isl_space *space;
5632 space = isl_set_get_space(set);
5633 space = isl_space_set_from_params(space);
5634 set = isl_set_reset_space(set, space);
5635 return set;
5638 /* Compute the parameter domain of the given map.
5640 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5642 isl_space *space;
5643 unsigned n;
5645 n = isl_map_dim(map, isl_dim_in);
5646 map = isl_map_project_out(map, isl_dim_in, 0, n);
5647 n = isl_map_dim(map, isl_dim_out);
5648 map = isl_map_project_out(map, isl_dim_out, 0, n);
5649 space = isl_map_get_space(map);
5650 space = isl_space_params(space);
5651 map = isl_map_reset_space(map, space);
5652 return map;
5655 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5657 isl_space *space;
5658 unsigned n_out;
5660 if (!bmap)
5661 return NULL;
5662 space = isl_space_domain(isl_basic_map_get_space(bmap));
5664 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5665 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5667 return isl_basic_map_reset_space(bmap, space);
5670 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5672 if (!bmap)
5673 return isl_bool_error;
5674 return isl_space_may_be_set(bmap->dim);
5677 /* Is this basic map actually a set?
5678 * Users should never call this function. Outside of isl,
5679 * the type should indicate whether something is a set or a map.
5681 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5683 if (!bmap)
5684 return isl_bool_error;
5685 return isl_space_is_set(bmap->dim);
5688 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5690 isl_bool is_set;
5692 is_set = isl_basic_map_is_set(bmap);
5693 if (is_set < 0)
5694 goto error;
5695 if (is_set)
5696 return bmap;
5697 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5698 error:
5699 isl_basic_map_free(bmap);
5700 return NULL;
5703 __isl_give isl_basic_map *isl_basic_map_domain_map(
5704 __isl_take isl_basic_map *bmap)
5706 int i;
5707 isl_space *space;
5708 isl_basic_map *domain;
5709 int nparam, n_in, n_out;
5711 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5712 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5713 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5715 space = isl_basic_map_get_space(bmap);
5716 space = isl_space_from_range(isl_space_domain(space));
5717 domain = isl_basic_map_universe(space);
5719 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5720 bmap = isl_basic_map_apply_range(bmap, domain);
5721 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5723 for (i = 0; i < n_in; ++i)
5724 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5725 isl_dim_out, i);
5727 bmap = isl_basic_map_gauss(bmap, NULL);
5728 return isl_basic_map_finalize(bmap);
5731 __isl_give isl_basic_map *isl_basic_map_range_map(
5732 __isl_take isl_basic_map *bmap)
5734 int i;
5735 isl_space *space;
5736 isl_basic_map *range;
5737 int nparam, n_in, n_out;
5739 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5740 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5741 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5743 space = isl_basic_map_get_space(bmap);
5744 space = isl_space_from_range(isl_space_range(space));
5745 range = isl_basic_map_universe(space);
5747 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5748 bmap = isl_basic_map_apply_range(bmap, range);
5749 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5751 for (i = 0; i < n_out; ++i)
5752 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5753 isl_dim_out, i);
5755 bmap = isl_basic_map_gauss(bmap, NULL);
5756 return isl_basic_map_finalize(bmap);
5759 int isl_map_may_be_set(__isl_keep isl_map *map)
5761 if (!map)
5762 return -1;
5763 return isl_space_may_be_set(map->dim);
5766 /* Is this map actually a set?
5767 * Users should never call this function. Outside of isl,
5768 * the type should indicate whether something is a set or a map.
5770 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5772 if (!map)
5773 return isl_bool_error;
5774 return isl_space_is_set(map->dim);
5777 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5779 int i;
5780 isl_bool is_set;
5781 struct isl_set *set;
5783 is_set = isl_map_is_set(map);
5784 if (is_set < 0)
5785 goto error;
5786 if (is_set)
5787 return set_from_map(map);
5789 map = isl_map_cow(map);
5790 if (!map)
5791 goto error;
5793 set = set_from_map(map);
5794 set->dim = isl_space_range(set->dim);
5795 if (!set->dim)
5796 goto error;
5797 for (i = 0; i < map->n; ++i) {
5798 set->p[i] = isl_basic_map_range(map->p[i]);
5799 if (!set->p[i])
5800 goto error;
5802 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5803 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5804 return set;
5805 error:
5806 isl_map_free(map);
5807 return NULL;
5810 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5812 int i;
5814 map = isl_map_cow(map);
5815 if (!map)
5816 return NULL;
5818 map->dim = isl_space_domain_map(map->dim);
5819 if (!map->dim)
5820 goto error;
5821 for (i = 0; i < map->n; ++i) {
5822 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5823 if (!map->p[i])
5824 goto error;
5826 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5827 map = isl_map_unmark_normalized(map);
5828 return map;
5829 error:
5830 isl_map_free(map);
5831 return NULL;
5834 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5836 int i;
5837 isl_space *range_dim;
5839 map = isl_map_cow(map);
5840 if (!map)
5841 return NULL;
5843 range_dim = isl_space_range(isl_map_get_space(map));
5844 range_dim = isl_space_from_range(range_dim);
5845 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5846 map->dim = isl_space_join(map->dim, range_dim);
5847 if (!map->dim)
5848 goto error;
5849 for (i = 0; i < map->n; ++i) {
5850 map->p[i] = isl_basic_map_range_map(map->p[i]);
5851 if (!map->p[i])
5852 goto error;
5854 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5855 map = isl_map_unmark_normalized(map);
5856 return map;
5857 error:
5858 isl_map_free(map);
5859 return NULL;
5862 /* Given a wrapped map of the form A[B -> C],
5863 * return the map A[B -> C] -> B.
5865 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5867 isl_id *id;
5868 isl_map *map;
5870 if (!set)
5871 return NULL;
5872 if (!isl_set_has_tuple_id(set))
5873 return isl_map_domain_map(isl_set_unwrap(set));
5875 id = isl_set_get_tuple_id(set);
5876 map = isl_map_domain_map(isl_set_unwrap(set));
5877 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5879 return map;
5882 __isl_give isl_basic_map *isl_basic_map_from_domain(
5883 __isl_take isl_basic_set *bset)
5885 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5888 __isl_give isl_basic_map *isl_basic_map_from_range(
5889 __isl_take isl_basic_set *bset)
5891 isl_space *space;
5892 space = isl_basic_set_get_space(bset);
5893 space = isl_space_from_range(space);
5894 bset = isl_basic_set_reset_space(bset, space);
5895 return bset_to_bmap(bset);
5898 /* Create a relation with the given set as range.
5899 * The domain of the created relation is a zero-dimensional
5900 * flat anonymous space.
5902 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5904 isl_space *space;
5905 space = isl_set_get_space(set);
5906 space = isl_space_from_range(space);
5907 set = isl_set_reset_space(set, space);
5908 return set_to_map(set);
5911 /* Create a relation with the given set as domain.
5912 * The range of the created relation is a zero-dimensional
5913 * flat anonymous space.
5915 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5917 return isl_map_reverse(isl_map_from_range(set));
5920 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5921 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5923 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5926 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5927 __isl_take isl_set *range)
5929 return isl_map_apply_range(isl_map_reverse(domain), range);
5932 /* Return a newly allocated isl_map with given space and flags and
5933 * room for "n" basic maps.
5934 * Make sure that all cached information is cleared.
5936 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5937 unsigned flags)
5939 struct isl_map *map;
5941 if (!space)
5942 return NULL;
5943 if (n < 0)
5944 isl_die(space->ctx, isl_error_internal,
5945 "negative number of basic maps", goto error);
5946 map = isl_calloc(space->ctx, struct isl_map,
5947 sizeof(struct isl_map) +
5948 (n - 1) * sizeof(struct isl_basic_map *));
5949 if (!map)
5950 goto error;
5952 map->ctx = space->ctx;
5953 isl_ctx_ref(map->ctx);
5954 map->ref = 1;
5955 map->size = n;
5956 map->n = 0;
5957 map->dim = space;
5958 map->flags = flags;
5959 return map;
5960 error:
5961 isl_space_free(space);
5962 return NULL;
5965 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5967 struct isl_basic_map *bmap;
5968 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5969 bmap = isl_basic_map_set_to_empty(bmap);
5970 return bmap;
5973 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5975 struct isl_basic_set *bset;
5976 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5977 bset = isl_basic_set_set_to_empty(bset);
5978 return bset;
5981 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5983 struct isl_basic_map *bmap;
5984 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5985 bmap = isl_basic_map_finalize(bmap);
5986 return bmap;
5989 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5991 struct isl_basic_set *bset;
5992 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5993 bset = isl_basic_set_finalize(bset);
5994 return bset;
5997 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5998 __isl_take isl_space *space)
6000 int i;
6001 unsigned total = isl_space_dim(space, isl_dim_all);
6002 isl_basic_map *bmap;
6004 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6005 for (i = 0; i < total; ++i) {
6006 int k = isl_basic_map_alloc_inequality(bmap);
6007 if (k < 0)
6008 goto error;
6009 isl_seq_clr(bmap->ineq[k], 1 + total);
6010 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6012 return bmap;
6013 error:
6014 isl_basic_map_free(bmap);
6015 return NULL;
6018 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6019 __isl_take isl_space *space)
6021 return isl_basic_map_nat_universe(space);
6024 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6026 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6029 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6031 return isl_map_nat_universe(dim);
6034 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6036 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6039 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6041 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6044 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6046 struct isl_map *map;
6047 if (!space)
6048 return NULL;
6049 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6050 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6051 return map;
6054 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6056 struct isl_set *set;
6057 if (!space)
6058 return NULL;
6059 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6060 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6061 return set;
6064 struct isl_map *isl_map_dup(struct isl_map *map)
6066 int i;
6067 struct isl_map *dup;
6069 if (!map)
6070 return NULL;
6071 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6072 for (i = 0; i < map->n; ++i)
6073 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6074 return dup;
6077 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6078 __isl_take isl_basic_map *bmap)
6080 if (!bmap || !map)
6081 goto error;
6082 if (isl_basic_map_plain_is_empty(bmap)) {
6083 isl_basic_map_free(bmap);
6084 return map;
6086 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6087 isl_assert(map->ctx, map->n < map->size, goto error);
6088 map->p[map->n] = bmap;
6089 map->n++;
6090 map = isl_map_unmark_normalized(map);
6091 return map;
6092 error:
6093 if (map)
6094 isl_map_free(map);
6095 if (bmap)
6096 isl_basic_map_free(bmap);
6097 return NULL;
6100 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6102 int i;
6104 if (!map)
6105 return NULL;
6107 if (--map->ref > 0)
6108 return NULL;
6110 clear_caches(map);
6111 isl_ctx_deref(map->ctx);
6112 for (i = 0; i < map->n; ++i)
6113 isl_basic_map_free(map->p[i]);
6114 isl_space_free(map->dim);
6115 free(map);
6117 return NULL;
6120 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6121 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6123 int j;
6125 bmap = isl_basic_map_cow(bmap);
6126 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6127 j = isl_basic_map_alloc_equality(bmap);
6128 if (j < 0)
6129 goto error;
6130 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6131 isl_int_set_si(bmap->eq[j][pos], -1);
6132 isl_int_set_si(bmap->eq[j][0], value);
6133 bmap = isl_basic_map_simplify(bmap);
6134 return isl_basic_map_finalize(bmap);
6135 error:
6136 isl_basic_map_free(bmap);
6137 return NULL;
6140 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6141 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6143 int j;
6145 bmap = isl_basic_map_cow(bmap);
6146 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6147 j = isl_basic_map_alloc_equality(bmap);
6148 if (j < 0)
6149 goto error;
6150 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6151 isl_int_set_si(bmap->eq[j][pos], -1);
6152 isl_int_set(bmap->eq[j][0], value);
6153 bmap = isl_basic_map_simplify(bmap);
6154 return isl_basic_map_finalize(bmap);
6155 error:
6156 isl_basic_map_free(bmap);
6157 return NULL;
6160 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6161 enum isl_dim_type type, unsigned pos, int value)
6163 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6164 return isl_basic_map_free(bmap);
6165 return isl_basic_map_fix_pos_si(bmap,
6166 isl_basic_map_offset(bmap, type) + pos, value);
6169 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6170 enum isl_dim_type type, unsigned pos, isl_int value)
6172 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6173 return isl_basic_map_free(bmap);
6174 return isl_basic_map_fix_pos(bmap,
6175 isl_basic_map_offset(bmap, type) + pos, value);
6178 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6179 * to be equal to "v".
6181 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6182 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6184 if (!bmap || !v)
6185 goto error;
6186 if (!isl_val_is_int(v))
6187 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6188 "expecting integer value", goto error);
6189 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6190 goto error;
6191 pos += isl_basic_map_offset(bmap, type);
6192 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6193 isl_val_free(v);
6194 return bmap;
6195 error:
6196 isl_basic_map_free(bmap);
6197 isl_val_free(v);
6198 return NULL;
6201 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6202 * to be equal to "v".
6204 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6205 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6207 return isl_basic_map_fix_val(bset, type, pos, v);
6210 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6211 enum isl_dim_type type, unsigned pos, int value)
6213 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6214 type, pos, value));
6217 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6218 enum isl_dim_type type, unsigned pos, isl_int value)
6220 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6221 type, pos, value));
6224 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6225 unsigned input, int value)
6227 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6230 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6231 unsigned dim, int value)
6233 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6234 isl_dim_set, dim, value));
6237 /* Remove the basic map at position "i" from "map" if this basic map
6238 * is (obviously) empty.
6240 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6242 isl_bool empty;
6244 if (!map)
6245 return NULL;
6247 empty = isl_basic_map_plain_is_empty(map->p[i]);
6248 if (empty < 0)
6249 return isl_map_free(map);
6250 if (!empty)
6251 return map;
6253 isl_basic_map_free(map->p[i]);
6254 map->n--;
6255 if (i != map->n) {
6256 map->p[i] = map->p[map->n];
6257 map = isl_map_unmark_normalized(map);
6261 return map;
6264 /* Perform "fn" on each basic map of "map", where we may not be holding
6265 * the only reference to "map".
6266 * In particular, "fn" should be a semantics preserving operation
6267 * that we want to apply to all copies of "map". We therefore need
6268 * to be careful not to modify "map" in a way that breaks "map"
6269 * in case anything goes wrong.
6271 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6272 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6274 struct isl_basic_map *bmap;
6275 int i;
6277 if (!map)
6278 return NULL;
6280 for (i = map->n - 1; i >= 0; --i) {
6281 bmap = isl_basic_map_copy(map->p[i]);
6282 bmap = fn(bmap);
6283 if (!bmap)
6284 goto error;
6285 isl_basic_map_free(map->p[i]);
6286 map->p[i] = bmap;
6287 map = remove_if_empty(map, i);
6288 if (!map)
6289 return NULL;
6292 return map;
6293 error:
6294 isl_map_free(map);
6295 return NULL;
6298 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6299 enum isl_dim_type type, unsigned pos, int value)
6301 int i;
6303 map = isl_map_cow(map);
6304 if (!map)
6305 return NULL;
6307 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6308 for (i = map->n - 1; i >= 0; --i) {
6309 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6310 map = remove_if_empty(map, i);
6311 if (!map)
6312 return NULL;
6314 map = isl_map_unmark_normalized(map);
6315 return map;
6316 error:
6317 isl_map_free(map);
6318 return NULL;
6321 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6322 enum isl_dim_type type, unsigned pos, int value)
6324 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6327 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6328 enum isl_dim_type type, unsigned pos, isl_int value)
6330 int i;
6332 map = isl_map_cow(map);
6333 if (!map)
6334 return NULL;
6336 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6337 for (i = 0; i < map->n; ++i) {
6338 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6339 if (!map->p[i])
6340 goto error;
6342 map = isl_map_unmark_normalized(map);
6343 return map;
6344 error:
6345 isl_map_free(map);
6346 return NULL;
6349 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6350 enum isl_dim_type type, unsigned pos, isl_int value)
6352 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6355 /* Fix the value of the variable at position "pos" of type "type" of "map"
6356 * to be equal to "v".
6358 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6359 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6361 int i;
6363 map = isl_map_cow(map);
6364 if (!map || !v)
6365 goto error;
6367 if (!isl_val_is_int(v))
6368 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6369 "expecting integer value", goto error);
6370 if (isl_map_check_range(map, type, pos, 1) < 0)
6371 goto error;
6372 for (i = map->n - 1; i >= 0; --i) {
6373 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6374 isl_val_copy(v));
6375 map = remove_if_empty(map, i);
6376 if (!map)
6377 goto error;
6379 map = isl_map_unmark_normalized(map);
6380 isl_val_free(v);
6381 return map;
6382 error:
6383 isl_map_free(map);
6384 isl_val_free(v);
6385 return NULL;
6388 /* Fix the value of the variable at position "pos" of type "type" of "set"
6389 * to be equal to "v".
6391 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6392 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6394 return isl_map_fix_val(set, type, pos, v);
6397 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6398 unsigned input, int value)
6400 return isl_map_fix_si(map, isl_dim_in, input, value);
6403 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6405 return set_from_map(isl_map_fix_si(set_to_map(set),
6406 isl_dim_set, dim, value));
6409 static __isl_give isl_basic_map *basic_map_bound_si(
6410 __isl_take isl_basic_map *bmap,
6411 enum isl_dim_type type, unsigned pos, int value, int upper)
6413 int j;
6415 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6416 return isl_basic_map_free(bmap);
6417 pos += isl_basic_map_offset(bmap, type);
6418 bmap = isl_basic_map_cow(bmap);
6419 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6420 j = isl_basic_map_alloc_inequality(bmap);
6421 if (j < 0)
6422 goto error;
6423 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6424 if (upper) {
6425 isl_int_set_si(bmap->ineq[j][pos], -1);
6426 isl_int_set_si(bmap->ineq[j][0], value);
6427 } else {
6428 isl_int_set_si(bmap->ineq[j][pos], 1);
6429 isl_int_set_si(bmap->ineq[j][0], -value);
6431 bmap = isl_basic_map_simplify(bmap);
6432 return isl_basic_map_finalize(bmap);
6433 error:
6434 isl_basic_map_free(bmap);
6435 return NULL;
6438 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6439 __isl_take isl_basic_map *bmap,
6440 enum isl_dim_type type, unsigned pos, int value)
6442 return basic_map_bound_si(bmap, type, pos, value, 0);
6445 /* Constrain the values of the given dimension to be no greater than "value".
6447 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6448 __isl_take isl_basic_map *bmap,
6449 enum isl_dim_type type, unsigned pos, int value)
6451 return basic_map_bound_si(bmap, type, pos, value, 1);
6454 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6455 enum isl_dim_type type, unsigned pos, int value, int upper)
6457 int i;
6459 map = isl_map_cow(map);
6460 if (!map)
6461 return NULL;
6463 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6464 for (i = 0; i < map->n; ++i) {
6465 map->p[i] = basic_map_bound_si(map->p[i],
6466 type, pos, value, upper);
6467 if (!map->p[i])
6468 goto error;
6470 map = isl_map_unmark_normalized(map);
6471 return map;
6472 error:
6473 isl_map_free(map);
6474 return NULL;
6477 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6478 enum isl_dim_type type, unsigned pos, int value)
6480 return map_bound_si(map, type, pos, value, 0);
6483 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6484 enum isl_dim_type type, unsigned pos, int value)
6486 return map_bound_si(map, type, pos, value, 1);
6489 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6490 enum isl_dim_type type, unsigned pos, int value)
6492 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6493 type, pos, value));
6496 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6497 enum isl_dim_type type, unsigned pos, int value)
6499 return isl_map_upper_bound_si(set, type, pos, value);
6502 /* Bound the given variable of "bmap" from below (or above is "upper"
6503 * is set) to "value".
6505 static __isl_give isl_basic_map *basic_map_bound(
6506 __isl_take isl_basic_map *bmap,
6507 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6509 int j;
6511 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6512 return isl_basic_map_free(bmap);
6513 pos += isl_basic_map_offset(bmap, type);
6514 bmap = isl_basic_map_cow(bmap);
6515 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6516 j = isl_basic_map_alloc_inequality(bmap);
6517 if (j < 0)
6518 goto error;
6519 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6520 if (upper) {
6521 isl_int_set_si(bmap->ineq[j][pos], -1);
6522 isl_int_set(bmap->ineq[j][0], value);
6523 } else {
6524 isl_int_set_si(bmap->ineq[j][pos], 1);
6525 isl_int_neg(bmap->ineq[j][0], value);
6527 bmap = isl_basic_map_simplify(bmap);
6528 return isl_basic_map_finalize(bmap);
6529 error:
6530 isl_basic_map_free(bmap);
6531 return NULL;
6534 /* Bound the given variable of "map" from below (or above is "upper"
6535 * is set) to "value".
6537 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6538 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6540 int i;
6542 map = isl_map_cow(map);
6543 if (!map)
6544 return NULL;
6546 if (pos >= isl_map_dim(map, type))
6547 isl_die(map->ctx, isl_error_invalid,
6548 "index out of bounds", goto error);
6549 for (i = map->n - 1; i >= 0; --i) {
6550 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6551 map = remove_if_empty(map, i);
6552 if (!map)
6553 return NULL;
6555 map = isl_map_unmark_normalized(map);
6556 return map;
6557 error:
6558 isl_map_free(map);
6559 return NULL;
6562 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6563 enum isl_dim_type type, unsigned pos, isl_int value)
6565 return map_bound(map, type, pos, value, 0);
6568 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6569 enum isl_dim_type type, unsigned pos, isl_int value)
6571 return map_bound(map, type, pos, value, 1);
6574 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6575 enum isl_dim_type type, unsigned pos, isl_int value)
6577 return isl_map_lower_bound(set, type, pos, value);
6580 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6581 enum isl_dim_type type, unsigned pos, isl_int value)
6583 return isl_map_upper_bound(set, type, pos, value);
6586 /* Force the values of the variable at position "pos" of type "type" of "set"
6587 * to be no smaller than "value".
6589 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6590 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6592 if (!value)
6593 goto error;
6594 if (!isl_val_is_int(value))
6595 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6596 "expecting integer value", goto error);
6597 set = isl_set_lower_bound(set, type, pos, value->n);
6598 isl_val_free(value);
6599 return set;
6600 error:
6601 isl_val_free(value);
6602 isl_set_free(set);
6603 return NULL;
6606 /* Force the values of the variable at position "pos" of type "type" of "set"
6607 * to be no greater than "value".
6609 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6610 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6612 if (!value)
6613 goto error;
6614 if (!isl_val_is_int(value))
6615 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6616 "expecting integer value", goto error);
6617 set = isl_set_upper_bound(set, type, pos, value->n);
6618 isl_val_free(value);
6619 return set;
6620 error:
6621 isl_val_free(value);
6622 isl_set_free(set);
6623 return NULL;
6626 /* Bound the given variable of "bset" from below (or above is "upper"
6627 * is set) to "value".
6629 static __isl_give isl_basic_set *isl_basic_set_bound(
6630 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6631 isl_int value, int upper)
6633 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6634 type, pos, value, upper));
6637 /* Bound the given variable of "bset" from below (or above is "upper"
6638 * is set) to "value".
6640 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6641 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6642 __isl_take isl_val *value, int upper)
6644 if (!value)
6645 goto error;
6646 if (!isl_val_is_int(value))
6647 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6648 "expecting integer value", goto error);
6649 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6650 isl_val_free(value);
6651 return bset;
6652 error:
6653 isl_val_free(value);
6654 isl_basic_set_free(bset);
6655 return NULL;
6658 /* Bound the given variable of "bset" from below to "value".
6660 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6661 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6662 __isl_take isl_val *value)
6664 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6667 /* Bound the given variable of "bset" from above to "value".
6669 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6670 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6671 __isl_take isl_val *value)
6673 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6676 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6678 int i;
6680 map = isl_map_cow(map);
6681 if (!map)
6682 return NULL;
6684 map->dim = isl_space_reverse(map->dim);
6685 if (!map->dim)
6686 goto error;
6687 for (i = 0; i < map->n; ++i) {
6688 map->p[i] = isl_basic_map_reverse(map->p[i]);
6689 if (!map->p[i])
6690 goto error;
6692 map = isl_map_unmark_normalized(map);
6693 return map;
6694 error:
6695 isl_map_free(map);
6696 return NULL;
6699 #undef TYPE
6700 #define TYPE isl_pw_multi_aff
6701 #undef SUFFIX
6702 #define SUFFIX _pw_multi_aff
6703 #undef EMPTY
6704 #define EMPTY isl_pw_multi_aff_empty
6705 #undef ADD
6706 #define ADD isl_pw_multi_aff_union_add
6707 #include "isl_map_lexopt_templ.c"
6709 /* Given a map "map", compute the lexicographically minimal
6710 * (or maximal) image element for each domain element in dom,
6711 * in the form of an isl_pw_multi_aff.
6712 * If "empty" is not NULL, then set *empty to those elements in dom that
6713 * do not have an image element.
6714 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6715 * should be computed over the domain of "map". "empty" is also NULL
6716 * in this case.
6718 * We first compute the lexicographically minimal or maximal element
6719 * in the first basic map. This results in a partial solution "res"
6720 * and a subset "todo" of dom that still need to be handled.
6721 * We then consider each of the remaining maps in "map" and successively
6722 * update both "res" and "todo".
6723 * If "empty" is NULL, then the todo sets are not needed and therefore
6724 * also not computed.
6726 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6727 __isl_take isl_map *map, __isl_take isl_set *dom,
6728 __isl_give isl_set **empty, unsigned flags)
6730 int i;
6731 int full;
6732 isl_pw_multi_aff *res;
6733 isl_set *todo;
6735 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6736 if (!map || (!full && !dom))
6737 goto error;
6739 if (isl_map_plain_is_empty(map)) {
6740 if (empty)
6741 *empty = dom;
6742 else
6743 isl_set_free(dom);
6744 return isl_pw_multi_aff_from_map(map);
6747 res = basic_map_partial_lexopt_pw_multi_aff(
6748 isl_basic_map_copy(map->p[0]),
6749 isl_set_copy(dom), empty, flags);
6751 if (empty)
6752 todo = *empty;
6753 for (i = 1; i < map->n; ++i) {
6754 isl_pw_multi_aff *res_i;
6756 res_i = basic_map_partial_lexopt_pw_multi_aff(
6757 isl_basic_map_copy(map->p[i]),
6758 isl_set_copy(dom), empty, flags);
6760 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6761 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6762 else
6763 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6765 if (empty)
6766 todo = isl_set_intersect(todo, *empty);
6769 isl_set_free(dom);
6770 isl_map_free(map);
6772 if (empty)
6773 *empty = todo;
6775 return res;
6776 error:
6777 if (empty)
6778 *empty = NULL;
6779 isl_set_free(dom);
6780 isl_map_free(map);
6781 return NULL;
6784 #undef TYPE
6785 #define TYPE isl_map
6786 #undef SUFFIX
6787 #define SUFFIX
6788 #undef EMPTY
6789 #define EMPTY isl_map_empty
6790 #undef ADD
6791 #define ADD isl_map_union_disjoint
6792 #include "isl_map_lexopt_templ.c"
6794 /* Given a map "map", compute the lexicographically minimal
6795 * (or maximal) image element for each domain element in "dom",
6796 * in the form of an isl_map.
6797 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6798 * do not have an image element.
6799 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6800 * should be computed over the domain of "map". "empty" is also NULL
6801 * in this case.
6803 * If the input consists of more than one disjunct, then first
6804 * compute the desired result in the form of an isl_pw_multi_aff and
6805 * then convert that into an isl_map.
6807 * This function used to have an explicit implementation in terms
6808 * of isl_maps, but it would continually intersect the domains of
6809 * partial results with the complement of the domain of the next
6810 * partial solution, potentially leading to an explosion in the number
6811 * of disjuncts if there are several disjuncts in the input.
6812 * An even earlier implementation of this function would look for
6813 * better results in the domain of the partial result and for extra
6814 * results in the complement of this domain, which would lead to
6815 * even more splintering.
6817 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6818 __isl_take isl_map *map, __isl_take isl_set *dom,
6819 __isl_give isl_set **empty, unsigned flags)
6821 int full;
6822 struct isl_map *res;
6823 isl_pw_multi_aff *pma;
6825 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6826 if (!map || (!full && !dom))
6827 goto error;
6829 if (isl_map_plain_is_empty(map)) {
6830 if (empty)
6831 *empty = dom;
6832 else
6833 isl_set_free(dom);
6834 return map;
6837 if (map->n == 1) {
6838 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6839 dom, empty, flags);
6840 isl_map_free(map);
6841 return res;
6844 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6845 flags);
6846 return isl_map_from_pw_multi_aff(pma);
6847 error:
6848 if (empty)
6849 *empty = NULL;
6850 isl_set_free(dom);
6851 isl_map_free(map);
6852 return NULL;
6855 __isl_give isl_map *isl_map_partial_lexmax(
6856 __isl_take isl_map *map, __isl_take isl_set *dom,
6857 __isl_give isl_set **empty)
6859 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6862 __isl_give isl_map *isl_map_partial_lexmin(
6863 __isl_take isl_map *map, __isl_take isl_set *dom,
6864 __isl_give isl_set **empty)
6866 return isl_map_partial_lexopt(map, dom, empty, 0);
6869 __isl_give isl_set *isl_set_partial_lexmin(
6870 __isl_take isl_set *set, __isl_take isl_set *dom,
6871 __isl_give isl_set **empty)
6873 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6874 dom, empty));
6877 __isl_give isl_set *isl_set_partial_lexmax(
6878 __isl_take isl_set *set, __isl_take isl_set *dom,
6879 __isl_give isl_set **empty)
6881 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6882 dom, empty));
6885 /* Compute the lexicographic minimum (or maximum if "flags" includes
6886 * ISL_OPT_MAX) of "bset" over its parametric domain.
6888 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6889 unsigned flags)
6891 return isl_basic_map_lexopt(bset, flags);
6894 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6896 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6899 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6901 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6904 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6906 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6909 /* Compute the lexicographic minimum of "bset" over its parametric domain
6910 * for the purpose of quantifier elimination.
6911 * That is, find an explicit representation for all the existentially
6912 * quantified variables in "bset" by computing their lexicographic
6913 * minimum.
6915 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6916 __isl_take isl_basic_set *bset)
6918 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6921 /* Given a basic map with one output dimension, compute the minimum or
6922 * maximum of that dimension as an isl_pw_aff.
6924 * Compute the optimum as a lexicographic optimum over the single
6925 * output dimension and extract the single isl_pw_aff from the result.
6927 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6928 int max)
6930 isl_pw_multi_aff *pma;
6931 isl_pw_aff *pwaff;
6933 bmap = isl_basic_map_copy(bmap);
6934 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6935 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6936 isl_pw_multi_aff_free(pma);
6938 return pwaff;
6941 /* Compute the minimum or maximum of the given output dimension
6942 * as a function of the parameters and the input dimensions,
6943 * but independently of the other output dimensions.
6945 * We first project out the other output dimension and then compute
6946 * the "lexicographic" maximum in each basic map, combining the results
6947 * using isl_pw_aff_union_max.
6949 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6950 int max)
6952 int i;
6953 isl_pw_aff *pwaff;
6954 unsigned n_out;
6956 n_out = isl_map_dim(map, isl_dim_out);
6957 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6958 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6959 if (!map)
6960 return NULL;
6962 if (map->n == 0) {
6963 isl_space *space = isl_map_get_space(map);
6964 isl_map_free(map);
6965 return isl_pw_aff_empty(space);
6968 pwaff = basic_map_dim_opt(map->p[0], max);
6969 for (i = 1; i < map->n; ++i) {
6970 isl_pw_aff *pwaff_i;
6972 pwaff_i = basic_map_dim_opt(map->p[i], max);
6973 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6976 isl_map_free(map);
6978 return pwaff;
6981 /* Compute the minimum of the given output dimension as a function of the
6982 * parameters and input dimensions, but independently of
6983 * the other output dimensions.
6985 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6987 return map_dim_opt(map, pos, 0);
6990 /* Compute the maximum of the given output dimension as a function of the
6991 * parameters and input dimensions, but independently of
6992 * the other output dimensions.
6994 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6996 return map_dim_opt(map, pos, 1);
6999 /* Compute the minimum or maximum of the given set dimension
7000 * as a function of the parameters,
7001 * but independently of the other set dimensions.
7003 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7004 int max)
7006 return map_dim_opt(set, pos, max);
7009 /* Compute the maximum of the given set dimension as a function of the
7010 * parameters, but independently of the other set dimensions.
7012 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7014 return set_dim_opt(set, pos, 1);
7017 /* Compute the minimum of the given set dimension as a function of the
7018 * parameters, but independently of the other set dimensions.
7020 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7022 return set_dim_opt(set, pos, 0);
7025 /* Apply a preimage specified by "mat" on the parameters of "bset".
7026 * bset is assumed to have only parameters and divs.
7028 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7029 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7031 unsigned nparam;
7033 if (!bset || !mat)
7034 goto error;
7036 bset->dim = isl_space_cow(bset->dim);
7037 if (!bset->dim)
7038 goto error;
7040 nparam = isl_basic_set_dim(bset, isl_dim_param);
7042 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7044 bset->dim->nparam = 0;
7045 bset->dim->n_out = nparam;
7046 bset = isl_basic_set_preimage(bset, mat);
7047 if (bset) {
7048 bset->dim->nparam = bset->dim->n_out;
7049 bset->dim->n_out = 0;
7051 return bset;
7052 error:
7053 isl_mat_free(mat);
7054 isl_basic_set_free(bset);
7055 return NULL;
7058 /* Apply a preimage specified by "mat" on the parameters of "set".
7059 * set is assumed to have only parameters and divs.
7061 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7062 __isl_take isl_mat *mat)
7064 isl_space *space;
7065 unsigned nparam;
7067 if (!set || !mat)
7068 goto error;
7070 nparam = isl_set_dim(set, isl_dim_param);
7072 if (mat->n_row != 1 + nparam)
7073 isl_die(isl_set_get_ctx(set), isl_error_internal,
7074 "unexpected number of rows", goto error);
7076 space = isl_set_get_space(set);
7077 space = isl_space_move_dims(space, isl_dim_set, 0,
7078 isl_dim_param, 0, nparam);
7079 set = isl_set_reset_space(set, space);
7080 set = isl_set_preimage(set, mat);
7081 nparam = isl_set_dim(set, isl_dim_out);
7082 space = isl_set_get_space(set);
7083 space = isl_space_move_dims(space, isl_dim_param, 0,
7084 isl_dim_out, 0, nparam);
7085 set = isl_set_reset_space(set, space);
7086 return set;
7087 error:
7088 isl_mat_free(mat);
7089 isl_set_free(set);
7090 return NULL;
7093 /* Intersect the basic set "bset" with the affine space specified by the
7094 * equalities in "eq".
7096 static __isl_give isl_basic_set *basic_set_append_equalities(
7097 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7099 int i, k;
7100 unsigned len;
7102 if (!bset || !eq)
7103 goto error;
7105 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7106 eq->n_row, 0);
7107 if (!bset)
7108 goto error;
7110 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7111 for (i = 0; i < eq->n_row; ++i) {
7112 k = isl_basic_set_alloc_equality(bset);
7113 if (k < 0)
7114 goto error;
7115 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7116 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7118 isl_mat_free(eq);
7120 bset = isl_basic_set_gauss(bset, NULL);
7121 bset = isl_basic_set_finalize(bset);
7123 return bset;
7124 error:
7125 isl_mat_free(eq);
7126 isl_basic_set_free(bset);
7127 return NULL;
7130 /* Intersect the set "set" with the affine space specified by the
7131 * equalities in "eq".
7133 static struct isl_set *set_append_equalities(struct isl_set *set,
7134 struct isl_mat *eq)
7136 int i;
7138 if (!set || !eq)
7139 goto error;
7141 for (i = 0; i < set->n; ++i) {
7142 set->p[i] = basic_set_append_equalities(set->p[i],
7143 isl_mat_copy(eq));
7144 if (!set->p[i])
7145 goto error;
7147 isl_mat_free(eq);
7148 return set;
7149 error:
7150 isl_mat_free(eq);
7151 isl_set_free(set);
7152 return NULL;
7155 /* Given a basic set "bset" that only involves parameters and existentially
7156 * quantified variables, return the index of the first equality
7157 * that only involves parameters. If there is no such equality then
7158 * return bset->n_eq.
7160 * This function assumes that isl_basic_set_gauss has been called on "bset".
7162 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7164 int i, j;
7165 unsigned nparam, n_div;
7167 if (!bset)
7168 return -1;
7170 nparam = isl_basic_set_dim(bset, isl_dim_param);
7171 n_div = isl_basic_set_dim(bset, isl_dim_div);
7173 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7174 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7175 ++i;
7178 return i;
7181 /* Compute an explicit representation for the existentially quantified
7182 * variables in "bset" by computing the "minimal value" of the set
7183 * variables. Since there are no set variables, the computation of
7184 * the minimal value essentially computes an explicit representation
7185 * of the non-empty part(s) of "bset".
7187 * The input only involves parameters and existentially quantified variables.
7188 * All equalities among parameters have been removed.
7190 * Since the existentially quantified variables in the result are in general
7191 * going to be different from those in the input, we first replace
7192 * them by the minimal number of variables based on their equalities.
7193 * This should simplify the parametric integer programming.
7195 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7197 isl_morph *morph1, *morph2;
7198 isl_set *set;
7199 unsigned n;
7201 if (!bset)
7202 return NULL;
7203 if (bset->n_eq == 0)
7204 return isl_basic_set_lexmin_compute_divs(bset);
7206 morph1 = isl_basic_set_parameter_compression(bset);
7207 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7208 bset = isl_basic_set_lift(bset);
7209 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7210 bset = isl_morph_basic_set(morph2, bset);
7211 n = isl_basic_set_dim(bset, isl_dim_set);
7212 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7214 set = isl_basic_set_lexmin_compute_divs(bset);
7216 set = isl_morph_set(isl_morph_inverse(morph1), set);
7218 return set;
7221 /* Project the given basic set onto its parameter domain, possibly introducing
7222 * new, explicit, existential variables in the constraints.
7223 * The input has parameters and (possibly implicit) existential variables.
7224 * The output has the same parameters, but only
7225 * explicit existentially quantified variables.
7227 * The actual projection is performed by pip, but pip doesn't seem
7228 * to like equalities very much, so we first remove the equalities
7229 * among the parameters by performing a variable compression on
7230 * the parameters. Afterward, an inverse transformation is performed
7231 * and the equalities among the parameters are inserted back in.
7233 * The variable compression on the parameters may uncover additional
7234 * equalities that were only implicit before. We therefore check
7235 * if there are any new parameter equalities in the result and
7236 * if so recurse. The removal of parameter equalities is required
7237 * for the parameter compression performed by base_compute_divs.
7239 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7241 int i;
7242 struct isl_mat *eq;
7243 struct isl_mat *T, *T2;
7244 struct isl_set *set;
7245 unsigned nparam;
7247 bset = isl_basic_set_cow(bset);
7248 if (!bset)
7249 return NULL;
7251 if (bset->n_eq == 0)
7252 return base_compute_divs(bset);
7254 bset = isl_basic_set_gauss(bset, NULL);
7255 if (!bset)
7256 return NULL;
7257 if (isl_basic_set_plain_is_empty(bset))
7258 return isl_set_from_basic_set(bset);
7260 i = first_parameter_equality(bset);
7261 if (i == bset->n_eq)
7262 return base_compute_divs(bset);
7264 nparam = isl_basic_set_dim(bset, isl_dim_param);
7265 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7266 0, 1 + nparam);
7267 eq = isl_mat_cow(eq);
7268 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7269 if (T && T->n_col == 0) {
7270 isl_mat_free(T);
7271 isl_mat_free(T2);
7272 isl_mat_free(eq);
7273 bset = isl_basic_set_set_to_empty(bset);
7274 return isl_set_from_basic_set(bset);
7276 bset = basic_set_parameter_preimage(bset, T);
7278 i = first_parameter_equality(bset);
7279 if (!bset)
7280 set = NULL;
7281 else if (i == bset->n_eq)
7282 set = base_compute_divs(bset);
7283 else
7284 set = parameter_compute_divs(bset);
7285 set = set_parameter_preimage(set, T2);
7286 set = set_append_equalities(set, eq);
7287 return set;
7290 /* Insert the divs from "ls" before those of "bmap".
7292 * The number of columns is not changed, which means that the last
7293 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7294 * The caller is responsible for removing the same number of dimensions
7295 * from the space of "bmap".
7297 static __isl_give isl_basic_map *insert_divs_from_local_space(
7298 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7300 int i;
7301 int n_div;
7302 int old_n_div;
7304 n_div = isl_local_space_dim(ls, isl_dim_div);
7305 if (n_div == 0)
7306 return bmap;
7308 old_n_div = bmap->n_div;
7309 bmap = insert_div_rows(bmap, n_div);
7310 if (!bmap)
7311 return NULL;
7313 for (i = 0; i < n_div; ++i) {
7314 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7315 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7318 return bmap;
7321 /* Replace the space of "bmap" by the space and divs of "ls".
7323 * If "ls" has any divs, then we simplify the result since we may
7324 * have discovered some additional equalities that could simplify
7325 * the div expressions.
7327 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7328 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7330 int n_div;
7332 bmap = isl_basic_map_cow(bmap);
7333 if (!bmap || !ls)
7334 goto error;
7336 n_div = isl_local_space_dim(ls, isl_dim_div);
7337 bmap = insert_divs_from_local_space(bmap, ls);
7338 if (!bmap)
7339 goto error;
7341 isl_space_free(bmap->dim);
7342 bmap->dim = isl_local_space_get_space(ls);
7343 if (!bmap->dim)
7344 goto error;
7346 isl_local_space_free(ls);
7347 if (n_div > 0)
7348 bmap = isl_basic_map_simplify(bmap);
7349 bmap = isl_basic_map_finalize(bmap);
7350 return bmap;
7351 error:
7352 isl_basic_map_free(bmap);
7353 isl_local_space_free(ls);
7354 return NULL;
7357 /* Replace the space of "map" by the space and divs of "ls".
7359 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7360 __isl_take isl_local_space *ls)
7362 int i;
7364 map = isl_map_cow(map);
7365 if (!map || !ls)
7366 goto error;
7368 for (i = 0; i < map->n; ++i) {
7369 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7370 isl_local_space_copy(ls));
7371 if (!map->p[i])
7372 goto error;
7374 isl_space_free(map->dim);
7375 map->dim = isl_local_space_get_space(ls);
7376 if (!map->dim)
7377 goto error;
7379 isl_local_space_free(ls);
7380 return map;
7381 error:
7382 isl_local_space_free(ls);
7383 isl_map_free(map);
7384 return NULL;
7387 /* Compute an explicit representation for the existentially
7388 * quantified variables for which do not know any explicit representation yet.
7390 * We first sort the existentially quantified variables so that the
7391 * existentially quantified variables for which we already have an explicit
7392 * representation are placed before those for which we do not.
7393 * The input dimensions, the output dimensions and the existentially
7394 * quantified variables for which we already have an explicit
7395 * representation are then turned into parameters.
7396 * compute_divs returns a map with the same parameters and
7397 * no input or output dimensions and the dimension specification
7398 * is reset to that of the input, including the existentially quantified
7399 * variables for which we already had an explicit representation.
7401 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7403 struct isl_basic_set *bset;
7404 struct isl_set *set;
7405 struct isl_map *map;
7406 isl_space *space;
7407 isl_local_space *ls;
7408 unsigned nparam;
7409 unsigned n_in;
7410 unsigned n_out;
7411 int n_known;
7412 int i;
7414 bmap = isl_basic_map_sort_divs(bmap);
7415 bmap = isl_basic_map_cow(bmap);
7416 if (!bmap)
7417 return NULL;
7419 n_known = isl_basic_map_first_unknown_div(bmap);
7420 if (n_known < 0)
7421 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7423 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7424 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7425 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7426 space = isl_space_set_alloc(bmap->ctx,
7427 nparam + n_in + n_out + n_known, 0);
7428 if (!space)
7429 goto error;
7431 ls = isl_basic_map_get_local_space(bmap);
7432 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7433 n_known, bmap->n_div - n_known);
7434 if (n_known > 0) {
7435 for (i = n_known; i < bmap->n_div; ++i)
7436 swap_div(bmap, i - n_known, i);
7437 bmap->n_div -= n_known;
7438 bmap->extra -= n_known;
7440 bmap = isl_basic_map_reset_space(bmap, space);
7441 bset = bset_from_bmap(bmap);
7443 set = parameter_compute_divs(bset);
7444 map = set_to_map(set);
7445 map = replace_space_by_local_space(map, ls);
7447 return map;
7448 error:
7449 isl_basic_map_free(bmap);
7450 return NULL;
7453 /* Remove the explicit representation of local variable "div",
7454 * if there is any.
7456 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7457 __isl_take isl_basic_map *bmap, int div)
7459 isl_bool unknown;
7461 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7462 if (unknown < 0)
7463 return isl_basic_map_free(bmap);
7464 if (unknown)
7465 return bmap;
7467 bmap = isl_basic_map_cow(bmap);
7468 if (!bmap)
7469 return NULL;
7470 isl_int_set_si(bmap->div[div][0], 0);
7471 return bmap;
7474 /* Is local variable "div" of "bmap" marked as not having an explicit
7475 * representation?
7476 * Note that even if "div" is not marked in this way and therefore
7477 * has an explicit representation, this representation may still
7478 * depend (indirectly) on other local variables that do not
7479 * have an explicit representation.
7481 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7482 int div)
7484 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7485 return isl_bool_error;
7486 return isl_int_is_zero(bmap->div[div][0]);
7489 /* Return the position of the first local variable that does not
7490 * have an explicit representation.
7491 * Return the total number of local variables if they all have
7492 * an explicit representation.
7493 * Return -1 on error.
7495 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7497 int i;
7499 if (!bmap)
7500 return -1;
7502 for (i = 0; i < bmap->n_div; ++i) {
7503 if (!isl_basic_map_div_is_known(bmap, i))
7504 return i;
7506 return bmap->n_div;
7509 /* Return the position of the first local variable that does not
7510 * have an explicit representation.
7511 * Return the total number of local variables if they all have
7512 * an explicit representation.
7513 * Return -1 on error.
7515 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7517 return isl_basic_map_first_unknown_div(bset);
7520 /* Does "bmap" have an explicit representation for all local variables?
7522 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7524 int first, n;
7526 n = isl_basic_map_dim(bmap, isl_dim_div);
7527 first = isl_basic_map_first_unknown_div(bmap);
7528 if (first < 0)
7529 return isl_bool_error;
7530 return first == n;
7533 /* Do all basic maps in "map" have an explicit representation
7534 * for all local variables?
7536 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7538 int i;
7540 if (!map)
7541 return isl_bool_error;
7543 for (i = 0; i < map->n; ++i) {
7544 int known = isl_basic_map_divs_known(map->p[i]);
7545 if (known <= 0)
7546 return known;
7549 return isl_bool_true;
7552 /* If bmap contains any unknown divs, then compute explicit
7553 * expressions for them. However, this computation may be
7554 * quite expensive, so first try to remove divs that aren't
7555 * strictly needed.
7557 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7559 int known;
7560 struct isl_map *map;
7562 known = isl_basic_map_divs_known(bmap);
7563 if (known < 0)
7564 goto error;
7565 if (known)
7566 return isl_map_from_basic_map(bmap);
7568 bmap = isl_basic_map_drop_redundant_divs(bmap);
7570 known = isl_basic_map_divs_known(bmap);
7571 if (known < 0)
7572 goto error;
7573 if (known)
7574 return isl_map_from_basic_map(bmap);
7576 map = compute_divs(bmap);
7577 return map;
7578 error:
7579 isl_basic_map_free(bmap);
7580 return NULL;
7583 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7585 int i;
7586 int known;
7587 struct isl_map *res;
7589 if (!map)
7590 return NULL;
7591 if (map->n == 0)
7592 return map;
7594 known = isl_map_divs_known(map);
7595 if (known < 0) {
7596 isl_map_free(map);
7597 return NULL;
7599 if (known)
7600 return map;
7602 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7603 for (i = 1 ; i < map->n; ++i) {
7604 struct isl_map *r2;
7605 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7606 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7607 res = isl_map_union_disjoint(res, r2);
7608 else
7609 res = isl_map_union(res, r2);
7611 isl_map_free(map);
7613 return res;
7616 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7618 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7621 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7623 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7626 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7628 int i;
7629 struct isl_set *set;
7631 if (!map)
7632 goto error;
7634 map = isl_map_cow(map);
7635 if (!map)
7636 return NULL;
7638 set = set_from_map(map);
7639 set->dim = isl_space_domain(set->dim);
7640 if (!set->dim)
7641 goto error;
7642 for (i = 0; i < map->n; ++i) {
7643 set->p[i] = isl_basic_map_domain(map->p[i]);
7644 if (!set->p[i])
7645 goto error;
7647 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7648 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7649 return set;
7650 error:
7651 isl_map_free(map);
7652 return NULL;
7655 /* Return the union of "map1" and "map2", where we assume for now that
7656 * "map1" and "map2" are disjoint. Note that the basic maps inside
7657 * "map1" or "map2" may not be disjoint from each other.
7658 * Also note that this function is also called from isl_map_union,
7659 * which takes care of handling the situation where "map1" and "map2"
7660 * may not be disjoint.
7662 * If one of the inputs is empty, we can simply return the other input.
7663 * Similarly, if one of the inputs is universal, then it is equal to the union.
7665 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7666 __isl_take isl_map *map2)
7668 int i;
7669 unsigned flags = 0;
7670 struct isl_map *map = NULL;
7671 int is_universe;
7673 if (!map1 || !map2)
7674 goto error;
7676 if (!isl_space_is_equal(map1->dim, map2->dim))
7677 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7678 "spaces don't match", goto error);
7680 if (map1->n == 0) {
7681 isl_map_free(map1);
7682 return map2;
7684 if (map2->n == 0) {
7685 isl_map_free(map2);
7686 return map1;
7689 is_universe = isl_map_plain_is_universe(map1);
7690 if (is_universe < 0)
7691 goto error;
7692 if (is_universe) {
7693 isl_map_free(map2);
7694 return map1;
7697 is_universe = isl_map_plain_is_universe(map2);
7698 if (is_universe < 0)
7699 goto error;
7700 if (is_universe) {
7701 isl_map_free(map1);
7702 return map2;
7705 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7706 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7707 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7709 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7710 map1->n + map2->n, flags);
7711 if (!map)
7712 goto error;
7713 for (i = 0; i < map1->n; ++i) {
7714 map = isl_map_add_basic_map(map,
7715 isl_basic_map_copy(map1->p[i]));
7716 if (!map)
7717 goto error;
7719 for (i = 0; i < map2->n; ++i) {
7720 map = isl_map_add_basic_map(map,
7721 isl_basic_map_copy(map2->p[i]));
7722 if (!map)
7723 goto error;
7725 isl_map_free(map1);
7726 isl_map_free(map2);
7727 return map;
7728 error:
7729 isl_map_free(map);
7730 isl_map_free(map1);
7731 isl_map_free(map2);
7732 return NULL;
7735 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7736 * guaranteed to be disjoint by the caller.
7738 * Note that this functions is called from within isl_map_make_disjoint,
7739 * so we have to be careful not to touch the constraints of the inputs
7740 * in any way.
7742 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7743 __isl_take isl_map *map2)
7745 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7748 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7749 * not be disjoint. The parameters are assumed to have been aligned.
7751 * We currently simply call map_union_disjoint, the internal operation
7752 * of which does not really depend on the inputs being disjoint.
7753 * If the result contains more than one basic map, then we clear
7754 * the disjoint flag since the result may contain basic maps from
7755 * both inputs and these are not guaranteed to be disjoint.
7757 * As a special case, if "map1" and "map2" are obviously equal,
7758 * then we simply return "map1".
7760 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7761 __isl_take isl_map *map2)
7763 int equal;
7765 if (!map1 || !map2)
7766 goto error;
7768 equal = isl_map_plain_is_equal(map1, map2);
7769 if (equal < 0)
7770 goto error;
7771 if (equal) {
7772 isl_map_free(map2);
7773 return map1;
7776 map1 = map_union_disjoint(map1, map2);
7777 if (!map1)
7778 return NULL;
7779 if (map1->n > 1)
7780 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7781 return map1;
7782 error:
7783 isl_map_free(map1);
7784 isl_map_free(map2);
7785 return NULL;
7788 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7789 * not be disjoint.
7791 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7792 __isl_take isl_map *map2)
7794 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7797 __isl_give isl_set *isl_set_union_disjoint(
7798 __isl_take isl_set *set1, __isl_take isl_set *set2)
7800 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7801 set_to_map(set2)));
7804 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7806 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7809 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7810 * the results.
7812 * "map" and "set" are assumed to be compatible and non-NULL.
7814 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7815 __isl_take isl_set *set,
7816 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7817 __isl_take isl_basic_set *bset))
7819 unsigned flags = 0;
7820 struct isl_map *result;
7821 int i, j;
7823 if (isl_set_plain_is_universe(set)) {
7824 isl_set_free(set);
7825 return map;
7828 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7829 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7830 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7832 result = isl_map_alloc_space(isl_space_copy(map->dim),
7833 map->n * set->n, flags);
7834 for (i = 0; result && i < map->n; ++i)
7835 for (j = 0; j < set->n; ++j) {
7836 result = isl_map_add_basic_map(result,
7837 fn(isl_basic_map_copy(map->p[i]),
7838 isl_basic_set_copy(set->p[j])));
7839 if (!result)
7840 break;
7843 isl_map_free(map);
7844 isl_set_free(set);
7845 return result;
7848 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7849 __isl_take isl_set *set)
7851 isl_bool ok;
7853 ok = isl_map_compatible_range(map, set);
7854 if (ok < 0)
7855 goto error;
7856 if (!ok)
7857 isl_die(set->ctx, isl_error_invalid,
7858 "incompatible spaces", goto error);
7860 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7861 error:
7862 isl_map_free(map);
7863 isl_set_free(set);
7864 return NULL;
7867 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7868 __isl_take isl_set *set)
7870 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7873 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7874 __isl_take isl_set *set)
7876 isl_bool ok;
7878 ok = isl_map_compatible_domain(map, set);
7879 if (ok < 0)
7880 goto error;
7881 if (!ok)
7882 isl_die(set->ctx, isl_error_invalid,
7883 "incompatible spaces", goto error);
7885 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7886 error:
7887 isl_map_free(map);
7888 isl_set_free(set);
7889 return NULL;
7892 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7893 __isl_take isl_set *set)
7895 return isl_map_align_params_map_map_and(map, set,
7896 &map_intersect_domain);
7899 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7900 * in the space B -> C, return the intersection.
7901 * The parameters are assumed to have been aligned.
7903 * The map "factor" is first extended to a map living in the space
7904 * [A -> B] -> C and then a regular intersection is computed.
7906 static __isl_give isl_map *map_intersect_domain_factor_range(
7907 __isl_take isl_map *map, __isl_take isl_map *factor)
7909 isl_space *space;
7910 isl_map *ext_factor;
7912 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7913 ext_factor = isl_map_universe(space);
7914 ext_factor = isl_map_domain_product(ext_factor, factor);
7915 return map_intersect(map, ext_factor);
7918 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7919 * in the space B -> C, return the intersection.
7921 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7922 __isl_take isl_map *map, __isl_take isl_map *factor)
7924 return isl_map_align_params_map_map_and(map, factor,
7925 &map_intersect_domain_factor_range);
7928 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7929 * in the space A -> C, return the intersection.
7931 * The map "factor" is first extended to a map living in the space
7932 * A -> [B -> C] and then a regular intersection is computed.
7934 static __isl_give isl_map *map_intersect_range_factor_range(
7935 __isl_take isl_map *map, __isl_take isl_map *factor)
7937 isl_space *space;
7938 isl_map *ext_factor;
7940 space = isl_space_range_factor_domain(isl_map_get_space(map));
7941 ext_factor = isl_map_universe(space);
7942 ext_factor = isl_map_range_product(ext_factor, factor);
7943 return isl_map_intersect(map, ext_factor);
7946 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7947 * in the space A -> C, return the intersection.
7949 __isl_give isl_map *isl_map_intersect_range_factor_range(
7950 __isl_take isl_map *map, __isl_take isl_map *factor)
7952 return isl_map_align_params_map_map_and(map, factor,
7953 &map_intersect_range_factor_range);
7956 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7957 __isl_take isl_map *map2)
7959 if (!map1 || !map2)
7960 goto error;
7961 map1 = isl_map_reverse(map1);
7962 map1 = isl_map_apply_range(map1, map2);
7963 return isl_map_reverse(map1);
7964 error:
7965 isl_map_free(map1);
7966 isl_map_free(map2);
7967 return NULL;
7970 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7971 __isl_take isl_map *map2)
7973 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7976 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7977 __isl_take isl_map *map2)
7979 isl_space *space;
7980 struct isl_map *result;
7981 int i, j;
7983 if (!map1 || !map2)
7984 goto error;
7986 space = isl_space_join(isl_space_copy(map1->dim),
7987 isl_space_copy(map2->dim));
7989 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7990 if (!result)
7991 goto error;
7992 for (i = 0; i < map1->n; ++i)
7993 for (j = 0; j < map2->n; ++j) {
7994 result = isl_map_add_basic_map(result,
7995 isl_basic_map_apply_range(
7996 isl_basic_map_copy(map1->p[i]),
7997 isl_basic_map_copy(map2->p[j])));
7998 if (!result)
7999 goto error;
8001 isl_map_free(map1);
8002 isl_map_free(map2);
8003 if (result && result->n <= 1)
8004 ISL_F_SET(result, ISL_MAP_DISJOINT);
8005 return result;
8006 error:
8007 isl_map_free(map1);
8008 isl_map_free(map2);
8009 return NULL;
8012 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8013 __isl_take isl_map *map2)
8015 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8019 * returns range - domain
8021 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8023 isl_space *target_space;
8024 struct isl_basic_set *bset;
8025 unsigned dim;
8026 unsigned nparam;
8027 int i;
8029 if (!bmap)
8030 goto error;
8031 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8032 bmap->dim, isl_dim_out),
8033 goto error);
8034 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8035 dim = isl_basic_map_dim(bmap, isl_dim_in);
8036 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8037 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8038 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8039 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8040 for (i = 0; i < dim; ++i) {
8041 int j = isl_basic_map_alloc_equality(bmap);
8042 if (j < 0) {
8043 bmap = isl_basic_map_free(bmap);
8044 break;
8046 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8047 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8048 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8049 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8051 bset = isl_basic_map_domain(bmap);
8052 bset = isl_basic_set_reset_space(bset, target_space);
8053 return bset;
8054 error:
8055 isl_basic_map_free(bmap);
8056 return NULL;
8060 * returns range - domain
8062 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8064 int i;
8065 isl_space *dim;
8066 struct isl_set *result;
8068 if (!map)
8069 return NULL;
8071 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8072 map->dim, isl_dim_out),
8073 goto error);
8074 dim = isl_map_get_space(map);
8075 dim = isl_space_domain(dim);
8076 result = isl_set_alloc_space(dim, map->n, 0);
8077 if (!result)
8078 goto error;
8079 for (i = 0; i < map->n; ++i)
8080 result = isl_set_add_basic_set(result,
8081 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8082 isl_map_free(map);
8083 return result;
8084 error:
8085 isl_map_free(map);
8086 return NULL;
8090 * returns [domain -> range] -> range - domain
8092 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8093 __isl_take isl_basic_map *bmap)
8095 int i, k;
8096 isl_space *space;
8097 isl_basic_map *domain;
8098 int nparam, n;
8099 unsigned total;
8101 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8102 bmap->dim, isl_dim_out))
8103 isl_die(bmap->ctx, isl_error_invalid,
8104 "domain and range don't match", goto error);
8106 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8107 n = isl_basic_map_dim(bmap, isl_dim_in);
8109 space = isl_basic_map_get_space(bmap);
8110 space = isl_space_from_range(isl_space_domain(space));
8111 domain = isl_basic_map_universe(space);
8113 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8114 bmap = isl_basic_map_apply_range(bmap, domain);
8115 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8117 total = isl_basic_map_total_dim(bmap);
8119 for (i = 0; i < n; ++i) {
8120 k = isl_basic_map_alloc_equality(bmap);
8121 if (k < 0)
8122 goto error;
8123 isl_seq_clr(bmap->eq[k], 1 + total);
8124 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8125 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8126 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8129 bmap = isl_basic_map_gauss(bmap, NULL);
8130 return isl_basic_map_finalize(bmap);
8131 error:
8132 isl_basic_map_free(bmap);
8133 return NULL;
8137 * returns [domain -> range] -> range - domain
8139 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8141 int i;
8142 isl_space *domain_space;
8144 if (!map)
8145 return NULL;
8147 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8148 map->dim, isl_dim_out))
8149 isl_die(map->ctx, isl_error_invalid,
8150 "domain and range don't match", goto error);
8152 map = isl_map_cow(map);
8153 if (!map)
8154 return NULL;
8156 domain_space = isl_space_domain(isl_map_get_space(map));
8157 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8158 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8159 if (!map->dim)
8160 goto error;
8161 for (i = 0; i < map->n; ++i) {
8162 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8163 if (!map->p[i])
8164 goto error;
8166 map = isl_map_unmark_normalized(map);
8167 return map;
8168 error:
8169 isl_map_free(map);
8170 return NULL;
8173 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8175 unsigned n_in, n_out;
8177 if (!space)
8178 return NULL;
8179 n_in = isl_space_dim(space, isl_dim_in);
8180 n_out = isl_space_dim(space, isl_dim_out);
8181 if (n_in != n_out)
8182 isl_die(space->ctx, isl_error_invalid,
8183 "number of input and output dimensions needs to be "
8184 "the same", goto error);
8185 return isl_basic_map_equal(space, n_in);
8186 error:
8187 isl_space_free(space);
8188 return NULL;
8191 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8193 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8196 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8198 isl_space *dim = isl_set_get_space(set);
8199 isl_map *id;
8200 id = isl_map_identity(isl_space_map_from_set(dim));
8201 return isl_map_intersect_range(id, set);
8204 /* Construct a basic set with all set dimensions having only non-negative
8205 * values.
8207 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8208 __isl_take isl_space *space)
8210 int i;
8211 unsigned nparam;
8212 unsigned dim;
8213 struct isl_basic_set *bset;
8215 if (!space)
8216 return NULL;
8217 nparam = space->nparam;
8218 dim = space->n_out;
8219 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8220 if (!bset)
8221 return NULL;
8222 for (i = 0; i < dim; ++i) {
8223 int k = isl_basic_set_alloc_inequality(bset);
8224 if (k < 0)
8225 goto error;
8226 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8227 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8229 return bset;
8230 error:
8231 isl_basic_set_free(bset);
8232 return NULL;
8235 /* Construct the half-space x_pos >= 0.
8237 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8238 int pos)
8240 int k;
8241 isl_basic_set *nonneg;
8243 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8244 k = isl_basic_set_alloc_inequality(nonneg);
8245 if (k < 0)
8246 goto error;
8247 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8248 isl_int_set_si(nonneg->ineq[k][pos], 1);
8250 return isl_basic_set_finalize(nonneg);
8251 error:
8252 isl_basic_set_free(nonneg);
8253 return NULL;
8256 /* Construct the half-space x_pos <= -1.
8258 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8259 int pos)
8261 int k;
8262 isl_basic_set *neg;
8264 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8265 k = isl_basic_set_alloc_inequality(neg);
8266 if (k < 0)
8267 goto error;
8268 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8269 isl_int_set_si(neg->ineq[k][0], -1);
8270 isl_int_set_si(neg->ineq[k][pos], -1);
8272 return isl_basic_set_finalize(neg);
8273 error:
8274 isl_basic_set_free(neg);
8275 return NULL;
8278 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8279 enum isl_dim_type type, unsigned first, unsigned n)
8281 int i;
8282 unsigned offset;
8283 isl_basic_set *nonneg;
8284 isl_basic_set *neg;
8286 if (!set)
8287 return NULL;
8288 if (n == 0)
8289 return set;
8291 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8293 offset = pos(set->dim, type);
8294 for (i = 0; i < n; ++i) {
8295 nonneg = nonneg_halfspace(isl_set_get_space(set),
8296 offset + first + i);
8297 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8299 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8302 return set;
8303 error:
8304 isl_set_free(set);
8305 return NULL;
8308 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8309 int len,
8310 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8311 void *user)
8313 isl_set *half;
8315 if (!set)
8316 return isl_stat_error;
8317 if (isl_set_plain_is_empty(set)) {
8318 isl_set_free(set);
8319 return isl_stat_ok;
8321 if (first == len)
8322 return fn(set, signs, user);
8324 signs[first] = 1;
8325 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8326 1 + first));
8327 half = isl_set_intersect(half, isl_set_copy(set));
8328 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8329 goto error;
8331 signs[first] = -1;
8332 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8333 1 + first));
8334 half = isl_set_intersect(half, set);
8335 return foreach_orthant(half, signs, first + 1, len, fn, user);
8336 error:
8337 isl_set_free(set);
8338 return isl_stat_error;
8341 /* Call "fn" on the intersections of "set" with each of the orthants
8342 * (except for obviously empty intersections). The orthant is identified
8343 * by the signs array, with each entry having value 1 or -1 according
8344 * to the sign of the corresponding variable.
8346 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8347 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8348 void *user)
8350 unsigned nparam;
8351 unsigned nvar;
8352 int *signs;
8353 isl_stat r;
8355 if (!set)
8356 return isl_stat_error;
8357 if (isl_set_plain_is_empty(set))
8358 return isl_stat_ok;
8360 nparam = isl_set_dim(set, isl_dim_param);
8361 nvar = isl_set_dim(set, isl_dim_set);
8363 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8365 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8366 fn, user);
8368 free(signs);
8370 return r;
8373 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8375 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8378 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8379 __isl_keep isl_basic_map *bmap2)
8381 isl_bool is_subset;
8382 struct isl_map *map1;
8383 struct isl_map *map2;
8385 if (!bmap1 || !bmap2)
8386 return isl_bool_error;
8388 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8389 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8391 is_subset = isl_map_is_subset(map1, map2);
8393 isl_map_free(map1);
8394 isl_map_free(map2);
8396 return is_subset;
8399 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8400 __isl_keep isl_basic_set *bset2)
8402 return isl_basic_map_is_subset(bset1, bset2);
8405 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8406 __isl_keep isl_basic_map *bmap2)
8408 isl_bool is_subset;
8410 if (!bmap1 || !bmap2)
8411 return isl_bool_error;
8412 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8413 if (is_subset != isl_bool_true)
8414 return is_subset;
8415 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8416 return is_subset;
8419 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8420 __isl_keep isl_basic_set *bset2)
8422 return isl_basic_map_is_equal(
8423 bset_to_bmap(bset1), bset_to_bmap(bset2));
8426 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8428 int i;
8429 int is_empty;
8431 if (!map)
8432 return isl_bool_error;
8433 for (i = 0; i < map->n; ++i) {
8434 is_empty = isl_basic_map_is_empty(map->p[i]);
8435 if (is_empty < 0)
8436 return isl_bool_error;
8437 if (!is_empty)
8438 return isl_bool_false;
8440 return isl_bool_true;
8443 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8445 return map ? map->n == 0 : isl_bool_error;
8448 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8450 return set ? set->n == 0 : isl_bool_error;
8453 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8455 return isl_map_is_empty(set_to_map(set));
8458 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8459 __isl_keep isl_map *map2)
8461 if (!map1 || !map2)
8462 return isl_bool_error;
8464 return isl_space_is_equal(map1->dim, map2->dim);
8467 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8468 __isl_keep isl_set *set2)
8470 if (!set1 || !set2)
8471 return isl_bool_error;
8473 return isl_space_is_equal(set1->dim, set2->dim);
8476 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8478 isl_bool is_subset;
8480 if (!map1 || !map2)
8481 return isl_bool_error;
8482 is_subset = isl_map_is_subset(map1, map2);
8483 if (is_subset != isl_bool_true)
8484 return is_subset;
8485 is_subset = isl_map_is_subset(map2, map1);
8486 return is_subset;
8489 /* Is "map1" equal to "map2"?
8491 * First check if they are obviously equal.
8492 * If not, then perform a more detailed analysis.
8494 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8496 isl_bool equal;
8498 equal = isl_map_plain_is_equal(map1, map2);
8499 if (equal < 0 || equal)
8500 return equal;
8501 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8504 isl_bool isl_basic_map_is_strict_subset(
8505 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8507 isl_bool is_subset;
8509 if (!bmap1 || !bmap2)
8510 return isl_bool_error;
8511 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8512 if (is_subset != isl_bool_true)
8513 return is_subset;
8514 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8515 return isl_bool_not(is_subset);
8518 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8519 __isl_keep isl_map *map2)
8521 isl_bool is_subset;
8523 if (!map1 || !map2)
8524 return isl_bool_error;
8525 is_subset = isl_map_is_subset(map1, map2);
8526 if (is_subset != isl_bool_true)
8527 return is_subset;
8528 is_subset = isl_map_is_subset(map2, map1);
8529 return isl_bool_not(is_subset);
8532 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8533 __isl_keep isl_set *set2)
8535 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8538 /* Is "bmap" obviously equal to the universe with the same space?
8540 * That is, does it not have any constraints?
8542 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8544 if (!bmap)
8545 return isl_bool_error;
8546 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8549 /* Is "bset" obviously equal to the universe with the same space?
8551 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8553 return isl_basic_map_plain_is_universe(bset);
8556 /* If "c" does not involve any existentially quantified variables,
8557 * then set *univ to false and abort
8559 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8561 isl_bool *univ = user;
8562 unsigned n;
8564 n = isl_constraint_dim(c, isl_dim_div);
8565 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8566 isl_constraint_free(c);
8567 if (*univ < 0 || !*univ)
8568 return isl_stat_error;
8569 return isl_stat_ok;
8572 /* Is "bmap" equal to the universe with the same space?
8574 * First check if it is obviously equal to the universe.
8575 * If not and if there are any constraints not involving
8576 * existentially quantified variables, then it is certainly
8577 * not equal to the universe.
8578 * Otherwise, check if the universe is a subset of "bmap".
8580 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8582 isl_bool univ;
8583 isl_basic_map *test;
8585 univ = isl_basic_map_plain_is_universe(bmap);
8586 if (univ < 0 || univ)
8587 return univ;
8588 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8589 return isl_bool_false;
8590 univ = isl_bool_true;
8591 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8592 univ)
8593 return isl_bool_error;
8594 if (univ < 0 || !univ)
8595 return univ;
8596 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8597 univ = isl_basic_map_is_subset(test, bmap);
8598 isl_basic_map_free(test);
8599 return univ;
8602 /* Is "bset" equal to the universe with the same space?
8604 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8606 return isl_basic_map_is_universe(bset);
8609 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8611 int i;
8613 if (!map)
8614 return isl_bool_error;
8616 for (i = 0; i < map->n; ++i) {
8617 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8618 if (r < 0 || r)
8619 return r;
8622 return isl_bool_false;
8625 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8627 return isl_map_plain_is_universe(set_to_map(set));
8630 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8632 struct isl_basic_set *bset = NULL;
8633 struct isl_vec *sample = NULL;
8634 isl_bool empty, non_empty;
8636 if (!bmap)
8637 return isl_bool_error;
8639 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8640 return isl_bool_true;
8642 if (isl_basic_map_plain_is_universe(bmap))
8643 return isl_bool_false;
8645 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8646 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8647 copy = isl_basic_map_remove_redundancies(copy);
8648 empty = isl_basic_map_plain_is_empty(copy);
8649 isl_basic_map_free(copy);
8650 return empty;
8653 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8654 if (non_empty < 0)
8655 return isl_bool_error;
8656 if (non_empty)
8657 return isl_bool_false;
8658 isl_vec_free(bmap->sample);
8659 bmap->sample = NULL;
8660 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8661 if (!bset)
8662 return isl_bool_error;
8663 sample = isl_basic_set_sample_vec(bset);
8664 if (!sample)
8665 return isl_bool_error;
8666 empty = sample->size == 0;
8667 isl_vec_free(bmap->sample);
8668 bmap->sample = sample;
8669 if (empty)
8670 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8672 return empty;
8675 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8677 if (!bmap)
8678 return isl_bool_error;
8679 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8682 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8684 if (!bset)
8685 return isl_bool_error;
8686 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8689 /* Is "bmap" known to be non-empty?
8691 * That is, is the cached sample still valid?
8693 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8695 unsigned total;
8697 if (!bmap)
8698 return isl_bool_error;
8699 if (!bmap->sample)
8700 return isl_bool_false;
8701 total = 1 + isl_basic_map_total_dim(bmap);
8702 if (bmap->sample->size != total)
8703 return isl_bool_false;
8704 return isl_basic_map_contains(bmap, bmap->sample);
8707 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8709 return isl_basic_map_is_empty(bset_to_bmap(bset));
8712 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8713 __isl_take isl_basic_map *bmap2)
8715 struct isl_map *map;
8716 if (!bmap1 || !bmap2)
8717 goto error;
8719 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8721 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8722 if (!map)
8723 goto error;
8724 map = isl_map_add_basic_map(map, bmap1);
8725 map = isl_map_add_basic_map(map, bmap2);
8726 return map;
8727 error:
8728 isl_basic_map_free(bmap1);
8729 isl_basic_map_free(bmap2);
8730 return NULL;
8733 struct isl_set *isl_basic_set_union(
8734 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8736 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8737 bset_to_bmap(bset2)));
8740 /* Order divs such that any div only depends on previous divs */
8741 __isl_give isl_basic_map *isl_basic_map_order_divs(
8742 __isl_take isl_basic_map *bmap)
8744 int i;
8745 unsigned off;
8747 if (!bmap)
8748 return NULL;
8750 off = isl_space_dim(bmap->dim, isl_dim_all);
8752 for (i = 0; i < bmap->n_div; ++i) {
8753 int pos;
8754 if (isl_int_is_zero(bmap->div[i][0]))
8755 continue;
8756 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8757 bmap->n_div-i);
8758 if (pos == -1)
8759 continue;
8760 if (pos == 0)
8761 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8762 "integer division depends on itself",
8763 return isl_basic_map_free(bmap));
8764 isl_basic_map_swap_div(bmap, i, i + pos);
8765 --i;
8767 return bmap;
8770 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8772 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8775 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8777 int i;
8779 if (!map)
8780 return 0;
8782 for (i = 0; i < map->n; ++i) {
8783 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8784 if (!map->p[i])
8785 goto error;
8788 return map;
8789 error:
8790 isl_map_free(map);
8791 return NULL;
8794 /* Sort the local variables of "bset".
8796 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8797 __isl_take isl_basic_set *bset)
8799 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8802 /* Apply the expansion computed by isl_merge_divs.
8803 * The expansion itself is given by "exp" while the resulting
8804 * list of divs is given by "div".
8806 * Move the integer divisions of "bmap" into the right position
8807 * according to "exp" and then introduce the additional integer
8808 * divisions, adding div constraints.
8809 * The moving should be done first to avoid moving coefficients
8810 * in the definitions of the extra integer divisions.
8812 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8813 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8815 int i, j;
8816 int n_div;
8818 bmap = isl_basic_map_cow(bmap);
8819 if (!bmap || !div)
8820 goto error;
8822 if (div->n_row < bmap->n_div)
8823 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8824 "not an expansion", goto error);
8826 n_div = bmap->n_div;
8827 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8828 div->n_row - n_div, 0,
8829 2 * (div->n_row - n_div));
8831 for (i = n_div; i < div->n_row; ++i)
8832 if (isl_basic_map_alloc_div(bmap) < 0)
8833 goto error;
8835 for (j = n_div - 1; j >= 0; --j) {
8836 if (exp[j] == j)
8837 break;
8838 isl_basic_map_swap_div(bmap, j, exp[j]);
8840 j = 0;
8841 for (i = 0; i < div->n_row; ++i) {
8842 if (j < n_div && exp[j] == i) {
8843 j++;
8844 } else {
8845 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8846 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8847 continue;
8848 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8849 goto error;
8853 isl_mat_free(div);
8854 return bmap;
8855 error:
8856 isl_basic_map_free(bmap);
8857 isl_mat_free(div);
8858 return NULL;
8861 /* Apply the expansion computed by isl_merge_divs.
8862 * The expansion itself is given by "exp" while the resulting
8863 * list of divs is given by "div".
8865 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8866 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8868 return isl_basic_map_expand_divs(bset, div, exp);
8871 /* Look for a div in dst that corresponds to the div "div" in src.
8872 * The divs before "div" in src and dst are assumed to be the same.
8874 * Returns -1 if no corresponding div was found and the position
8875 * of the corresponding div in dst otherwise.
8877 static int find_div(__isl_keep isl_basic_map *dst,
8878 __isl_keep isl_basic_map *src, unsigned div)
8880 int i;
8882 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8884 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8885 for (i = div; i < dst->n_div; ++i)
8886 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8887 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8888 dst->n_div - div) == -1)
8889 return i;
8890 return -1;
8893 /* Align the divs of "dst" to those of "src", adding divs from "src"
8894 * if needed. That is, make sure that the first src->n_div divs
8895 * of the result are equal to those of src.
8897 * The result is not finalized as by design it will have redundant
8898 * divs if any divs from "src" were copied.
8900 __isl_give isl_basic_map *isl_basic_map_align_divs(
8901 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8903 int i;
8904 isl_bool known;
8905 int extended;
8906 unsigned total;
8908 if (!dst || !src)
8909 return isl_basic_map_free(dst);
8911 if (src->n_div == 0)
8912 return dst;
8914 known = isl_basic_map_divs_known(src);
8915 if (known < 0)
8916 return isl_basic_map_free(dst);
8917 if (!known)
8918 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8919 "some src divs are unknown",
8920 return isl_basic_map_free(dst));
8922 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8923 if (!src)
8924 return isl_basic_map_free(dst);
8926 extended = 0;
8927 total = isl_space_dim(src->dim, isl_dim_all);
8928 for (i = 0; i < src->n_div; ++i) {
8929 int j = find_div(dst, src, i);
8930 if (j < 0) {
8931 if (!extended) {
8932 int extra = src->n_div - i;
8933 dst = isl_basic_map_cow(dst);
8934 if (!dst)
8935 goto error;
8936 dst = isl_basic_map_extend_space(dst,
8937 isl_space_copy(dst->dim),
8938 extra, 0, 2 * extra);
8939 extended = 1;
8941 j = isl_basic_map_alloc_div(dst);
8942 if (j < 0)
8943 goto error;
8944 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8945 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8946 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8947 goto error;
8949 if (j != i)
8950 isl_basic_map_swap_div(dst, i, j);
8952 isl_basic_map_free(src);
8953 return dst;
8954 error:
8955 isl_basic_map_free(src);
8956 isl_basic_map_free(dst);
8957 return NULL;
8960 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8962 int i;
8964 if (!map)
8965 return NULL;
8966 if (map->n == 0)
8967 return map;
8968 map = isl_map_compute_divs(map);
8969 map = isl_map_cow(map);
8970 if (!map)
8971 return NULL;
8973 for (i = 1; i < map->n; ++i)
8974 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8975 for (i = 1; i < map->n; ++i) {
8976 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8977 if (!map->p[i])
8978 return isl_map_free(map);
8981 map = isl_map_unmark_normalized(map);
8982 return map;
8985 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8987 return isl_map_align_divs_internal(map);
8990 struct isl_set *isl_set_align_divs(struct isl_set *set)
8992 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8995 /* Align the divs of the basic maps in "map" to those
8996 * of the basic maps in "list", as well as to the other basic maps in "map".
8997 * The elements in "list" are assumed to have known divs.
8999 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9000 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9002 int i, n;
9004 map = isl_map_compute_divs(map);
9005 map = isl_map_cow(map);
9006 if (!map || !list)
9007 return isl_map_free(map);
9008 if (map->n == 0)
9009 return map;
9011 n = isl_basic_map_list_n_basic_map(list);
9012 for (i = 0; i < n; ++i) {
9013 isl_basic_map *bmap;
9015 bmap = isl_basic_map_list_get_basic_map(list, i);
9016 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9017 isl_basic_map_free(bmap);
9019 if (!map->p[0])
9020 return isl_map_free(map);
9022 return isl_map_align_divs_internal(map);
9025 /* Align the divs of each element of "list" to those of "bmap".
9026 * Both "bmap" and the elements of "list" are assumed to have known divs.
9028 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9029 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9031 int i, n;
9033 if (!list || !bmap)
9034 return isl_basic_map_list_free(list);
9036 n = isl_basic_map_list_n_basic_map(list);
9037 for (i = 0; i < n; ++i) {
9038 isl_basic_map *bmap_i;
9040 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9041 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9042 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9045 return list;
9048 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9049 __isl_take isl_map *map)
9051 isl_bool ok;
9053 ok = isl_map_compatible_domain(map, set);
9054 if (ok < 0)
9055 goto error;
9056 if (!ok)
9057 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9058 "incompatible spaces", goto error);
9059 map = isl_map_intersect_domain(map, set);
9060 set = isl_map_range(map);
9061 return set;
9062 error:
9063 isl_set_free(set);
9064 isl_map_free(map);
9065 return NULL;
9068 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9069 __isl_take isl_map *map)
9071 return isl_map_align_params_map_map_and(set, map, &set_apply);
9074 /* There is no need to cow as removing empty parts doesn't change
9075 * the meaning of the set.
9077 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9079 int i;
9081 if (!map)
9082 return NULL;
9084 for (i = map->n - 1; i >= 0; --i)
9085 map = remove_if_empty(map, i);
9087 return map;
9090 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9092 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9095 /* Create a binary relation that maps the shared initial "pos" dimensions
9096 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9098 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9099 __isl_keep isl_basic_set *bset2, int pos)
9101 isl_basic_map *bmap1;
9102 isl_basic_map *bmap2;
9104 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9105 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9106 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9107 isl_dim_out, 0, pos);
9108 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9109 isl_dim_out, 0, pos);
9110 return isl_basic_map_range_product(bmap1, bmap2);
9113 /* Given two basic sets bset1 and bset2, compute the maximal difference
9114 * between the values of dimension pos in bset1 and those in bset2
9115 * for any common value of the parameters and dimensions preceding pos.
9117 static enum isl_lp_result basic_set_maximal_difference_at(
9118 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9119 int pos, isl_int *opt)
9121 isl_basic_map *bmap1;
9122 struct isl_ctx *ctx;
9123 struct isl_vec *obj;
9124 unsigned total;
9125 unsigned nparam;
9126 unsigned dim1;
9127 enum isl_lp_result res;
9129 if (!bset1 || !bset2)
9130 return isl_lp_error;
9132 nparam = isl_basic_set_n_param(bset1);
9133 dim1 = isl_basic_set_n_dim(bset1);
9135 bmap1 = join_initial(bset1, bset2, pos);
9136 if (!bmap1)
9137 return isl_lp_error;
9139 total = isl_basic_map_total_dim(bmap1);
9140 ctx = bmap1->ctx;
9141 obj = isl_vec_alloc(ctx, 1 + total);
9142 if (!obj)
9143 goto error;
9144 isl_seq_clr(obj->block.data, 1 + total);
9145 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9146 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9147 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9148 opt, NULL, NULL);
9149 isl_basic_map_free(bmap1);
9150 isl_vec_free(obj);
9151 return res;
9152 error:
9153 isl_basic_map_free(bmap1);
9154 return isl_lp_error;
9157 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9158 * for any common value of the parameters and dimensions preceding pos
9159 * in both basic sets, the values of dimension pos in bset1 are
9160 * smaller or larger than those in bset2.
9162 * Returns
9163 * 1 if bset1 follows bset2
9164 * -1 if bset1 precedes bset2
9165 * 0 if bset1 and bset2 are incomparable
9166 * -2 if some error occurred.
9168 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9169 __isl_keep isl_basic_set *bset2, int pos)
9171 isl_int opt;
9172 enum isl_lp_result res;
9173 int cmp;
9175 isl_int_init(opt);
9177 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9179 if (res == isl_lp_empty)
9180 cmp = 0;
9181 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9182 res == isl_lp_unbounded)
9183 cmp = 1;
9184 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9185 cmp = -1;
9186 else
9187 cmp = -2;
9189 isl_int_clear(opt);
9190 return cmp;
9193 /* Given two basic sets bset1 and bset2, check whether
9194 * for any common value of the parameters and dimensions preceding pos
9195 * there is a value of dimension pos in bset1 that is larger
9196 * than a value of the same dimension in bset2.
9198 * Return
9199 * 1 if there exists such a pair
9200 * 0 if there is no such pair, but there is a pair of equal values
9201 * -1 otherwise
9202 * -2 if some error occurred.
9204 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9205 __isl_keep isl_basic_set *bset2, int pos)
9207 isl_bool empty;
9208 isl_basic_map *bmap;
9209 unsigned dim1;
9211 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9212 bmap = join_initial(bset1, bset2, pos);
9213 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9214 isl_dim_out, dim1 - pos);
9215 empty = isl_basic_map_is_empty(bmap);
9216 if (empty < 0)
9217 goto error;
9218 if (empty) {
9219 isl_basic_map_free(bmap);
9220 return -1;
9222 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9223 isl_dim_out, dim1 - pos);
9224 empty = isl_basic_map_is_empty(bmap);
9225 if (empty < 0)
9226 goto error;
9227 isl_basic_map_free(bmap);
9228 if (empty)
9229 return 0;
9230 return 1;
9231 error:
9232 isl_basic_map_free(bmap);
9233 return -2;
9236 /* Given two sets set1 and set2, check whether
9237 * for any common value of the parameters and dimensions preceding pos
9238 * there is a value of dimension pos in set1 that is larger
9239 * than a value of the same dimension in set2.
9241 * Return
9242 * 1 if there exists such a pair
9243 * 0 if there is no such pair, but there is a pair of equal values
9244 * -1 otherwise
9245 * -2 if some error occurred.
9247 int isl_set_follows_at(__isl_keep isl_set *set1,
9248 __isl_keep isl_set *set2, int pos)
9250 int i, j;
9251 int follows = -1;
9253 if (!set1 || !set2)
9254 return -2;
9256 for (i = 0; i < set1->n; ++i)
9257 for (j = 0; j < set2->n; ++j) {
9258 int f;
9259 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9260 if (f == 1 || f == -2)
9261 return f;
9262 if (f > follows)
9263 follows = f;
9266 return follows;
9269 static isl_bool isl_basic_map_plain_has_fixed_var(
9270 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9272 int i;
9273 int d;
9274 unsigned total;
9276 if (!bmap)
9277 return isl_bool_error;
9278 total = isl_basic_map_total_dim(bmap);
9279 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9280 for (; d+1 > pos; --d)
9281 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9282 break;
9283 if (d != pos)
9284 continue;
9285 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9286 return isl_bool_false;
9287 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9288 return isl_bool_false;
9289 if (!isl_int_is_one(bmap->eq[i][1+d]))
9290 return isl_bool_false;
9291 if (val)
9292 isl_int_neg(*val, bmap->eq[i][0]);
9293 return isl_bool_true;
9295 return isl_bool_false;
9298 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9299 unsigned pos, isl_int *val)
9301 int i;
9302 isl_int v;
9303 isl_int tmp;
9304 isl_bool fixed;
9306 if (!map)
9307 return isl_bool_error;
9308 if (map->n == 0)
9309 return isl_bool_false;
9310 if (map->n == 1)
9311 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9312 isl_int_init(v);
9313 isl_int_init(tmp);
9314 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9315 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9316 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9317 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9318 fixed = isl_bool_false;
9320 if (val)
9321 isl_int_set(*val, v);
9322 isl_int_clear(tmp);
9323 isl_int_clear(v);
9324 return fixed;
9327 static isl_bool isl_basic_set_plain_has_fixed_var(
9328 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9330 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9331 pos, val);
9334 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9335 enum isl_dim_type type, unsigned pos, isl_int *val)
9337 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9338 return isl_bool_error;
9339 return isl_basic_map_plain_has_fixed_var(bmap,
9340 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9343 /* If "bmap" obviously lies on a hyperplane where the given dimension
9344 * has a fixed value, then return that value.
9345 * Otherwise return NaN.
9347 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9348 __isl_keep isl_basic_map *bmap,
9349 enum isl_dim_type type, unsigned pos)
9351 isl_ctx *ctx;
9352 isl_val *v;
9353 isl_bool fixed;
9355 if (!bmap)
9356 return NULL;
9357 ctx = isl_basic_map_get_ctx(bmap);
9358 v = isl_val_alloc(ctx);
9359 if (!v)
9360 return NULL;
9361 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9362 if (fixed < 0)
9363 return isl_val_free(v);
9364 if (fixed) {
9365 isl_int_set_si(v->d, 1);
9366 return v;
9368 isl_val_free(v);
9369 return isl_val_nan(ctx);
9372 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9373 enum isl_dim_type type, unsigned pos, isl_int *val)
9375 if (pos >= isl_map_dim(map, type))
9376 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9377 "position out of bounds", return isl_bool_error);
9378 return isl_map_plain_has_fixed_var(map,
9379 map_offset(map, type) - 1 + pos, val);
9382 /* If "map" obviously lies on a hyperplane where the given dimension
9383 * has a fixed value, then return that value.
9384 * Otherwise return NaN.
9386 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9387 enum isl_dim_type type, unsigned pos)
9389 isl_ctx *ctx;
9390 isl_val *v;
9391 isl_bool fixed;
9393 if (!map)
9394 return NULL;
9395 ctx = isl_map_get_ctx(map);
9396 v = isl_val_alloc(ctx);
9397 if (!v)
9398 return NULL;
9399 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9400 if (fixed < 0)
9401 return isl_val_free(v);
9402 if (fixed) {
9403 isl_int_set_si(v->d, 1);
9404 return v;
9406 isl_val_free(v);
9407 return isl_val_nan(ctx);
9410 /* If "set" obviously lies on a hyperplane where the given dimension
9411 * has a fixed value, then return that value.
9412 * Otherwise return NaN.
9414 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9415 enum isl_dim_type type, unsigned pos)
9417 return isl_map_plain_get_val_if_fixed(set, type, pos);
9420 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9421 * then return this fixed value in *val.
9423 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9424 unsigned dim, isl_int *val)
9426 return isl_basic_set_plain_has_fixed_var(bset,
9427 isl_basic_set_n_param(bset) + dim, val);
9430 /* Return -1 if the constraint "c1" should be sorted before "c2"
9431 * and 1 if it should be sorted after "c2".
9432 * Return 0 if the two constraints are the same (up to the constant term).
9434 * In particular, if a constraint involves later variables than another
9435 * then it is sorted after this other constraint.
9436 * uset_gist depends on constraints without existentially quantified
9437 * variables sorting first.
9439 * For constraints that have the same latest variable, those
9440 * with the same coefficient for this latest variable (first in absolute value
9441 * and then in actual value) are grouped together.
9442 * This is useful for detecting pairs of constraints that can
9443 * be chained in their printed representation.
9445 * Finally, within a group, constraints are sorted according to
9446 * their coefficients (excluding the constant term).
9448 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9450 isl_int **c1 = (isl_int **) p1;
9451 isl_int **c2 = (isl_int **) p2;
9452 int l1, l2;
9453 unsigned size = *(unsigned *) arg;
9454 int cmp;
9456 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9457 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9459 if (l1 != l2)
9460 return l1 - l2;
9462 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9463 if (cmp != 0)
9464 return cmp;
9465 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9466 if (cmp != 0)
9467 return -cmp;
9469 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9472 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9473 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9474 * and 0 if the two constraints are the same (up to the constant term).
9476 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9477 isl_int *c1, isl_int *c2)
9479 unsigned total;
9481 if (!bmap)
9482 return -2;
9483 total = isl_basic_map_total_dim(bmap);
9484 return sort_constraint_cmp(&c1, &c2, &total);
9487 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9488 __isl_take isl_basic_map *bmap)
9490 unsigned total;
9492 if (!bmap)
9493 return NULL;
9494 if (bmap->n_ineq == 0)
9495 return bmap;
9496 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9497 return bmap;
9498 total = isl_basic_map_total_dim(bmap);
9499 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9500 &sort_constraint_cmp, &total) < 0)
9501 return isl_basic_map_free(bmap);
9502 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9503 return bmap;
9506 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9507 __isl_take isl_basic_set *bset)
9509 isl_basic_map *bmap = bset_to_bmap(bset);
9510 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9513 __isl_give isl_basic_map *isl_basic_map_normalize(
9514 __isl_take isl_basic_map *bmap)
9516 bmap = isl_basic_map_remove_redundancies(bmap);
9517 bmap = isl_basic_map_sort_constraints(bmap);
9518 return bmap;
9520 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9521 __isl_keep isl_basic_map *bmap2)
9523 int i, cmp;
9524 unsigned total;
9525 isl_space *space1, *space2;
9527 if (!bmap1 || !bmap2)
9528 return -1;
9530 if (bmap1 == bmap2)
9531 return 0;
9532 space1 = isl_basic_map_peek_space(bmap1);
9533 space2 = isl_basic_map_peek_space(bmap2);
9534 cmp = isl_space_cmp(space1, space2);
9535 if (cmp)
9536 return cmp;
9537 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9538 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9539 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9540 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9541 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9542 return 0;
9543 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9544 return 1;
9545 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9546 return -1;
9547 if (bmap1->n_eq != bmap2->n_eq)
9548 return bmap1->n_eq - bmap2->n_eq;
9549 if (bmap1->n_ineq != bmap2->n_ineq)
9550 return bmap1->n_ineq - bmap2->n_ineq;
9551 if (bmap1->n_div != bmap2->n_div)
9552 return bmap1->n_div - bmap2->n_div;
9553 total = isl_basic_map_total_dim(bmap1);
9554 for (i = 0; i < bmap1->n_eq; ++i) {
9555 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9556 if (cmp)
9557 return cmp;
9559 for (i = 0; i < bmap1->n_ineq; ++i) {
9560 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9561 if (cmp)
9562 return cmp;
9564 for (i = 0; i < bmap1->n_div; ++i) {
9565 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9566 if (cmp)
9567 return cmp;
9569 return 0;
9572 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9573 __isl_keep isl_basic_set *bset2)
9575 return isl_basic_map_plain_cmp(bset1, bset2);
9578 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9580 int i, cmp;
9582 if (set1 == set2)
9583 return 0;
9584 if (set1->n != set2->n)
9585 return set1->n - set2->n;
9587 for (i = 0; i < set1->n; ++i) {
9588 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9589 if (cmp)
9590 return cmp;
9593 return 0;
9596 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9597 __isl_keep isl_basic_map *bmap2)
9599 if (!bmap1 || !bmap2)
9600 return isl_bool_error;
9601 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9604 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9605 __isl_keep isl_basic_set *bset2)
9607 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9608 bset_to_bmap(bset2));
9611 static int qsort_bmap_cmp(const void *p1, const void *p2)
9613 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9614 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9616 return isl_basic_map_plain_cmp(bmap1, bmap2);
9619 /* Sort the basic maps of "map" and remove duplicate basic maps.
9621 * While removing basic maps, we make sure that the basic maps remain
9622 * sorted because isl_map_normalize expects the basic maps of the result
9623 * to be sorted.
9625 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9627 int i, j;
9629 map = isl_map_remove_empty_parts(map);
9630 if (!map)
9631 return NULL;
9632 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9633 for (i = map->n - 1; i >= 1; --i) {
9634 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9635 continue;
9636 isl_basic_map_free(map->p[i-1]);
9637 for (j = i; j < map->n; ++j)
9638 map->p[j - 1] = map->p[j];
9639 map->n--;
9642 return map;
9645 /* Remove obvious duplicates among the basic maps of "map".
9647 * Unlike isl_map_normalize, this function does not remove redundant
9648 * constraints and only removes duplicates that have exactly the same
9649 * constraints in the input. It does sort the constraints and
9650 * the basic maps to ease the detection of duplicates.
9652 * If "map" has already been normalized or if the basic maps are
9653 * disjoint, then there can be no duplicates.
9655 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9657 int i;
9658 isl_basic_map *bmap;
9660 if (!map)
9661 return NULL;
9662 if (map->n <= 1)
9663 return map;
9664 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9665 return map;
9666 for (i = 0; i < map->n; ++i) {
9667 bmap = isl_basic_map_copy(map->p[i]);
9668 bmap = isl_basic_map_sort_constraints(bmap);
9669 if (!bmap)
9670 return isl_map_free(map);
9671 isl_basic_map_free(map->p[i]);
9672 map->p[i] = bmap;
9675 map = sort_and_remove_duplicates(map);
9676 return map;
9679 /* We normalize in place, but if anything goes wrong we need
9680 * to return NULL, so we need to make sure we don't change the
9681 * meaning of any possible other copies of map.
9683 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9685 int i;
9686 struct isl_basic_map *bmap;
9688 if (!map)
9689 return NULL;
9690 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9691 return map;
9692 for (i = 0; i < map->n; ++i) {
9693 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9694 if (!bmap)
9695 goto error;
9696 isl_basic_map_free(map->p[i]);
9697 map->p[i] = bmap;
9700 map = sort_and_remove_duplicates(map);
9701 if (map)
9702 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9703 return map;
9704 error:
9705 isl_map_free(map);
9706 return NULL;
9709 struct isl_set *isl_set_normalize(struct isl_set *set)
9711 return set_from_map(isl_map_normalize(set_to_map(set)));
9714 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9715 __isl_keep isl_map *map2)
9717 int i;
9718 isl_bool equal;
9720 if (!map1 || !map2)
9721 return isl_bool_error;
9723 if (map1 == map2)
9724 return isl_bool_true;
9725 if (!isl_space_is_equal(map1->dim, map2->dim))
9726 return isl_bool_false;
9728 map1 = isl_map_copy(map1);
9729 map2 = isl_map_copy(map2);
9730 map1 = isl_map_normalize(map1);
9731 map2 = isl_map_normalize(map2);
9732 if (!map1 || !map2)
9733 goto error;
9734 equal = map1->n == map2->n;
9735 for (i = 0; equal && i < map1->n; ++i) {
9736 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9737 if (equal < 0)
9738 goto error;
9740 isl_map_free(map1);
9741 isl_map_free(map2);
9742 return equal;
9743 error:
9744 isl_map_free(map1);
9745 isl_map_free(map2);
9746 return isl_bool_error;
9749 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9750 __isl_keep isl_set *set2)
9752 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9755 /* Return the basic maps in "map" as a list.
9757 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9758 __isl_keep isl_map *map)
9760 int i;
9761 isl_ctx *ctx;
9762 isl_basic_map_list *list;
9764 if (!map)
9765 return NULL;
9766 ctx = isl_map_get_ctx(map);
9767 list = isl_basic_map_list_alloc(ctx, map->n);
9769 for (i = 0; i < map->n; ++i) {
9770 isl_basic_map *bmap;
9772 bmap = isl_basic_map_copy(map->p[i]);
9773 list = isl_basic_map_list_add(list, bmap);
9776 return list;
9779 /* Return the intersection of the elements in the non-empty list "list".
9780 * All elements are assumed to live in the same space.
9782 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9783 __isl_take isl_basic_map_list *list)
9785 int i, n;
9786 isl_basic_map *bmap;
9788 if (!list)
9789 return NULL;
9790 n = isl_basic_map_list_n_basic_map(list);
9791 if (n < 1)
9792 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9793 "expecting non-empty list", goto error);
9795 bmap = isl_basic_map_list_get_basic_map(list, 0);
9796 for (i = 1; i < n; ++i) {
9797 isl_basic_map *bmap_i;
9799 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9800 bmap = isl_basic_map_intersect(bmap, bmap_i);
9803 isl_basic_map_list_free(list);
9804 return bmap;
9805 error:
9806 isl_basic_map_list_free(list);
9807 return NULL;
9810 /* Return the intersection of the elements in the non-empty list "list".
9811 * All elements are assumed to live in the same space.
9813 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9814 __isl_take isl_basic_set_list *list)
9816 return isl_basic_map_list_intersect(list);
9819 /* Return the union of the elements of "list".
9820 * The list is required to have at least one element.
9822 __isl_give isl_set *isl_basic_set_list_union(
9823 __isl_take isl_basic_set_list *list)
9825 int i, n;
9826 isl_space *space;
9827 isl_basic_set *bset;
9828 isl_set *set;
9830 if (!list)
9831 return NULL;
9832 n = isl_basic_set_list_n_basic_set(list);
9833 if (n < 1)
9834 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9835 "expecting non-empty list", goto error);
9837 bset = isl_basic_set_list_get_basic_set(list, 0);
9838 space = isl_basic_set_get_space(bset);
9839 isl_basic_set_free(bset);
9841 set = isl_set_alloc_space(space, n, 0);
9842 for (i = 0; i < n; ++i) {
9843 bset = isl_basic_set_list_get_basic_set(list, i);
9844 set = isl_set_add_basic_set(set, bset);
9847 isl_basic_set_list_free(list);
9848 return set;
9849 error:
9850 isl_basic_set_list_free(list);
9851 return NULL;
9854 /* Return the union of the elements in the non-empty list "list".
9855 * All elements are assumed to live in the same space.
9857 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9859 int i, n;
9860 isl_set *set;
9862 if (!list)
9863 return NULL;
9864 n = isl_set_list_n_set(list);
9865 if (n < 1)
9866 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9867 "expecting non-empty list", goto error);
9869 set = isl_set_list_get_set(list, 0);
9870 for (i = 1; i < n; ++i) {
9871 isl_set *set_i;
9873 set_i = isl_set_list_get_set(list, i);
9874 set = isl_set_union(set, set_i);
9877 isl_set_list_free(list);
9878 return set;
9879 error:
9880 isl_set_list_free(list);
9881 return NULL;
9884 __isl_give isl_basic_map *isl_basic_map_product(
9885 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9887 isl_space *space_result = NULL;
9888 struct isl_basic_map *bmap;
9889 unsigned in1, in2, out1, out2, nparam, total, pos;
9890 struct isl_dim_map *dim_map1, *dim_map2;
9892 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9893 goto error;
9894 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9895 isl_space_copy(bmap2->dim));
9897 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9898 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9899 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9900 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9901 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9903 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9904 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9905 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9906 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9907 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9908 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9909 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9912 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9913 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9915 bmap = isl_basic_map_alloc_space(space_result,
9916 bmap1->n_div + bmap2->n_div,
9917 bmap1->n_eq + bmap2->n_eq,
9918 bmap1->n_ineq + bmap2->n_ineq);
9919 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9921 bmap = isl_basic_map_simplify(bmap);
9922 return isl_basic_map_finalize(bmap);
9923 error:
9924 isl_basic_map_free(bmap1);
9925 isl_basic_map_free(bmap2);
9926 return NULL;
9929 __isl_give isl_basic_map *isl_basic_map_flat_product(
9930 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9932 isl_basic_map *prod;
9934 prod = isl_basic_map_product(bmap1, bmap2);
9935 prod = isl_basic_map_flatten(prod);
9936 return prod;
9939 __isl_give isl_basic_set *isl_basic_set_flat_product(
9940 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9942 return isl_basic_map_flat_range_product(bset1, bset2);
9945 __isl_give isl_basic_map *isl_basic_map_domain_product(
9946 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9948 isl_space *space_result = NULL;
9949 isl_basic_map *bmap;
9950 unsigned in1, in2, out, nparam, total, pos;
9951 struct isl_dim_map *dim_map1, *dim_map2;
9953 if (!bmap1 || !bmap2)
9954 goto error;
9956 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9957 isl_space_copy(bmap2->dim));
9959 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9960 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9961 out = isl_basic_map_dim(bmap1, isl_dim_out);
9962 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9964 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9965 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9966 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9969 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9970 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9971 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9972 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9973 isl_dim_map_div(dim_map1, bmap1, pos += out);
9974 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9976 bmap = isl_basic_map_alloc_space(space_result,
9977 bmap1->n_div + bmap2->n_div,
9978 bmap1->n_eq + bmap2->n_eq,
9979 bmap1->n_ineq + bmap2->n_ineq);
9980 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9981 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9982 bmap = isl_basic_map_simplify(bmap);
9983 return isl_basic_map_finalize(bmap);
9984 error:
9985 isl_basic_map_free(bmap1);
9986 isl_basic_map_free(bmap2);
9987 return NULL;
9990 __isl_give isl_basic_map *isl_basic_map_range_product(
9991 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9993 isl_bool rational;
9994 isl_space *space_result = NULL;
9995 isl_basic_map *bmap;
9996 unsigned in, out1, out2, nparam, total, pos;
9997 struct isl_dim_map *dim_map1, *dim_map2;
9999 rational = isl_basic_map_is_rational(bmap1);
10000 if (rational >= 0 && rational)
10001 rational = isl_basic_map_is_rational(bmap2);
10002 if (!bmap1 || !bmap2 || rational < 0)
10003 goto error;
10005 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10006 goto error;
10008 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10009 isl_space_copy(bmap2->dim));
10011 in = isl_basic_map_dim(bmap1, isl_dim_in);
10012 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10013 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10014 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10016 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10017 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10018 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10019 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10020 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10021 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10022 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10023 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10024 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10025 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10026 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10028 bmap = isl_basic_map_alloc_space(space_result,
10029 bmap1->n_div + bmap2->n_div,
10030 bmap1->n_eq + bmap2->n_eq,
10031 bmap1->n_ineq + bmap2->n_ineq);
10032 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10033 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10034 if (rational)
10035 bmap = isl_basic_map_set_rational(bmap);
10036 bmap = isl_basic_map_simplify(bmap);
10037 return isl_basic_map_finalize(bmap);
10038 error:
10039 isl_basic_map_free(bmap1);
10040 isl_basic_map_free(bmap2);
10041 return NULL;
10044 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10045 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10047 isl_basic_map *prod;
10049 prod = isl_basic_map_range_product(bmap1, bmap2);
10050 prod = isl_basic_map_flatten_range(prod);
10051 return prod;
10054 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10055 * and collect the results.
10056 * The result live in the space obtained by calling "space_product"
10057 * on the spaces of "map1" and "map2".
10058 * If "remove_duplicates" is set then the result may contain duplicates
10059 * (even if the inputs do not) and so we try and remove the obvious
10060 * duplicates.
10062 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10063 __isl_take isl_map *map2,
10064 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10065 __isl_take isl_space *right),
10066 __isl_give isl_basic_map *(*basic_map_product)(
10067 __isl_take isl_basic_map *left,
10068 __isl_take isl_basic_map *right),
10069 int remove_duplicates)
10071 unsigned flags = 0;
10072 struct isl_map *result;
10073 int i, j;
10074 isl_bool m;
10076 m = isl_map_has_equal_params(map1, map2);
10077 if (m < 0)
10078 goto error;
10079 if (!m)
10080 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10081 "parameters don't match", goto error);
10083 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10084 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10085 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10087 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10088 isl_space_copy(map2->dim)),
10089 map1->n * map2->n, flags);
10090 if (!result)
10091 goto error;
10092 for (i = 0; i < map1->n; ++i)
10093 for (j = 0; j < map2->n; ++j) {
10094 struct isl_basic_map *part;
10095 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10096 isl_basic_map_copy(map2->p[j]));
10097 if (isl_basic_map_is_empty(part))
10098 isl_basic_map_free(part);
10099 else
10100 result = isl_map_add_basic_map(result, part);
10101 if (!result)
10102 goto error;
10104 if (remove_duplicates)
10105 result = isl_map_remove_obvious_duplicates(result);
10106 isl_map_free(map1);
10107 isl_map_free(map2);
10108 return result;
10109 error:
10110 isl_map_free(map1);
10111 isl_map_free(map2);
10112 return NULL;
10115 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10117 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10118 __isl_take isl_map *map2)
10120 return map_product(map1, map2, &isl_space_product,
10121 &isl_basic_map_product, 0);
10124 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10125 __isl_take isl_map *map2)
10127 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10130 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10132 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10133 __isl_take isl_map *map2)
10135 isl_map *prod;
10137 prod = isl_map_product(map1, map2);
10138 prod = isl_map_flatten(prod);
10139 return prod;
10142 /* Given two set A and B, construct its Cartesian product A x B.
10144 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10146 return isl_map_range_product(set1, set2);
10149 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10150 __isl_take isl_set *set2)
10152 return isl_map_flat_range_product(set1, set2);
10155 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10157 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10158 __isl_take isl_map *map2)
10160 return map_product(map1, map2, &isl_space_domain_product,
10161 &isl_basic_map_domain_product, 1);
10164 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10166 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10167 __isl_take isl_map *map2)
10169 return map_product(map1, map2, &isl_space_range_product,
10170 &isl_basic_map_range_product, 1);
10173 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10174 __isl_take isl_map *map2)
10176 return isl_map_align_params_map_map_and(map1, map2,
10177 &map_domain_product_aligned);
10180 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10181 __isl_take isl_map *map2)
10183 return isl_map_align_params_map_map_and(map1, map2,
10184 &map_range_product_aligned);
10187 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10189 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10191 isl_space *space;
10192 int total1, keep1, total2, keep2;
10194 if (!map)
10195 return NULL;
10196 if (!isl_space_domain_is_wrapping(map->dim) ||
10197 !isl_space_range_is_wrapping(map->dim))
10198 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10199 "not a product", return isl_map_free(map));
10201 space = isl_map_get_space(map);
10202 total1 = isl_space_dim(space, isl_dim_in);
10203 total2 = isl_space_dim(space, isl_dim_out);
10204 space = isl_space_factor_domain(space);
10205 keep1 = isl_space_dim(space, isl_dim_in);
10206 keep2 = isl_space_dim(space, isl_dim_out);
10207 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10208 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10209 map = isl_map_reset_space(map, space);
10211 return map;
10214 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10216 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10218 isl_space *space;
10219 int total1, keep1, total2, keep2;
10221 if (!map)
10222 return NULL;
10223 if (!isl_space_domain_is_wrapping(map->dim) ||
10224 !isl_space_range_is_wrapping(map->dim))
10225 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10226 "not a product", return isl_map_free(map));
10228 space = isl_map_get_space(map);
10229 total1 = isl_space_dim(space, isl_dim_in);
10230 total2 = isl_space_dim(space, isl_dim_out);
10231 space = isl_space_factor_range(space);
10232 keep1 = isl_space_dim(space, isl_dim_in);
10233 keep2 = isl_space_dim(space, isl_dim_out);
10234 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10235 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10236 map = isl_map_reset_space(map, space);
10238 return map;
10241 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10243 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10245 isl_space *space;
10246 int total, keep;
10248 if (!map)
10249 return NULL;
10250 if (!isl_space_domain_is_wrapping(map->dim))
10251 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10252 "domain is not a product", return isl_map_free(map));
10254 space = isl_map_get_space(map);
10255 total = isl_space_dim(space, isl_dim_in);
10256 space = isl_space_domain_factor_domain(space);
10257 keep = isl_space_dim(space, isl_dim_in);
10258 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10259 map = isl_map_reset_space(map, space);
10261 return map;
10264 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10266 __isl_give isl_map *isl_map_domain_factor_range(__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_range(space);
10280 keep = isl_space_dim(space, isl_dim_in);
10281 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10282 map = isl_map_reset_space(map, space);
10284 return map;
10287 /* Given a map A -> [B -> C], extract the map A -> B.
10289 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10291 isl_space *space;
10292 int total, keep;
10294 if (!map)
10295 return NULL;
10296 if (!isl_space_range_is_wrapping(map->dim))
10297 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10298 "range is not a product", return isl_map_free(map));
10300 space = isl_map_get_space(map);
10301 total = isl_space_dim(space, isl_dim_out);
10302 space = isl_space_range_factor_domain(space);
10303 keep = isl_space_dim(space, isl_dim_out);
10304 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10305 map = isl_map_reset_space(map, space);
10307 return map;
10310 /* Given a map A -> [B -> C], extract the map A -> C.
10312 __isl_give isl_map *isl_map_range_factor_range(__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_range(space);
10326 keep = isl_space_dim(space, isl_dim_out);
10327 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10328 map = isl_map_reset_space(map, space);
10330 return map;
10333 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10335 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10336 __isl_take isl_map *map2)
10338 isl_map *prod;
10340 prod = isl_map_domain_product(map1, map2);
10341 prod = isl_map_flatten_domain(prod);
10342 return prod;
10345 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10347 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10348 __isl_take isl_map *map2)
10350 isl_map *prod;
10352 prod = isl_map_range_product(map1, map2);
10353 prod = isl_map_flatten_range(prod);
10354 return prod;
10357 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10359 int i;
10360 uint32_t hash = isl_hash_init();
10361 unsigned total;
10363 if (!bmap)
10364 return 0;
10365 bmap = isl_basic_map_copy(bmap);
10366 bmap = isl_basic_map_normalize(bmap);
10367 if (!bmap)
10368 return 0;
10369 total = isl_basic_map_total_dim(bmap);
10370 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10371 for (i = 0; i < bmap->n_eq; ++i) {
10372 uint32_t c_hash;
10373 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10374 isl_hash_hash(hash, c_hash);
10376 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10377 for (i = 0; i < bmap->n_ineq; ++i) {
10378 uint32_t c_hash;
10379 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10380 isl_hash_hash(hash, c_hash);
10382 isl_hash_byte(hash, bmap->n_div & 0xFF);
10383 for (i = 0; i < bmap->n_div; ++i) {
10384 uint32_t c_hash;
10385 if (isl_int_is_zero(bmap->div[i][0]))
10386 continue;
10387 isl_hash_byte(hash, i & 0xFF);
10388 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10389 isl_hash_hash(hash, c_hash);
10391 isl_basic_map_free(bmap);
10392 return hash;
10395 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10397 return isl_basic_map_get_hash(bset_to_bmap(bset));
10400 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10402 int i;
10403 uint32_t hash;
10405 if (!map)
10406 return 0;
10407 map = isl_map_copy(map);
10408 map = isl_map_normalize(map);
10409 if (!map)
10410 return 0;
10412 hash = isl_hash_init();
10413 for (i = 0; i < map->n; ++i) {
10414 uint32_t bmap_hash;
10415 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10416 isl_hash_hash(hash, bmap_hash);
10419 isl_map_free(map);
10421 return hash;
10424 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10426 return isl_map_get_hash(set_to_map(set));
10429 /* Return the number of basic maps in the (current) representation of "map".
10431 int isl_map_n_basic_map(__isl_keep isl_map *map)
10433 return map ? map->n : 0;
10436 int isl_set_n_basic_set(__isl_keep isl_set *set)
10438 return set ? set->n : 0;
10441 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10442 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10444 int i;
10446 if (!map)
10447 return isl_stat_error;
10449 for (i = 0; i < map->n; ++i)
10450 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10451 return isl_stat_error;
10453 return isl_stat_ok;
10456 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10457 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10459 int i;
10461 if (!set)
10462 return isl_stat_error;
10464 for (i = 0; i < set->n; ++i)
10465 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10466 return isl_stat_error;
10468 return isl_stat_ok;
10471 /* Return a list of basic sets, the union of which is equal to "set".
10473 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10474 __isl_keep isl_set *set)
10476 int i;
10477 isl_basic_set_list *list;
10479 if (!set)
10480 return NULL;
10482 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10483 for (i = 0; i < set->n; ++i) {
10484 isl_basic_set *bset;
10486 bset = isl_basic_set_copy(set->p[i]);
10487 list = isl_basic_set_list_add(list, bset);
10490 return list;
10493 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10495 isl_space *space;
10497 if (!bset)
10498 return NULL;
10500 bset = isl_basic_set_cow(bset);
10501 if (!bset)
10502 return NULL;
10504 space = isl_basic_set_get_space(bset);
10505 space = isl_space_lift(space, bset->n_div);
10506 if (!space)
10507 goto error;
10508 isl_space_free(bset->dim);
10509 bset->dim = space;
10510 bset->extra -= bset->n_div;
10511 bset->n_div = 0;
10513 bset = isl_basic_set_finalize(bset);
10515 return bset;
10516 error:
10517 isl_basic_set_free(bset);
10518 return NULL;
10521 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10523 int i;
10524 isl_space *space;
10525 unsigned n_div;
10527 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10529 if (!set)
10530 return NULL;
10532 set = isl_set_cow(set);
10533 if (!set)
10534 return NULL;
10536 n_div = set->p[0]->n_div;
10537 space = isl_set_get_space(set);
10538 space = isl_space_lift(space, n_div);
10539 if (!space)
10540 goto error;
10541 isl_space_free(set->dim);
10542 set->dim = space;
10544 for (i = 0; i < set->n; ++i) {
10545 set->p[i] = isl_basic_set_lift(set->p[i]);
10546 if (!set->p[i])
10547 goto error;
10550 return set;
10551 error:
10552 isl_set_free(set);
10553 return NULL;
10556 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10558 unsigned dim;
10559 int size = 0;
10561 if (!bset)
10562 return -1;
10564 dim = isl_basic_set_total_dim(bset);
10565 size += bset->n_eq * (1 + dim);
10566 size += bset->n_ineq * (1 + dim);
10567 size += bset->n_div * (2 + dim);
10569 return size;
10572 int isl_set_size(__isl_keep isl_set *set)
10574 int i;
10575 int size = 0;
10577 if (!set)
10578 return -1;
10580 for (i = 0; i < set->n; ++i)
10581 size += isl_basic_set_size(set->p[i]);
10583 return size;
10586 /* Check if there is any lower bound (if lower == 0) and/or upper
10587 * bound (if upper == 0) on the specified dim.
10589 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10590 enum isl_dim_type type, unsigned pos, int lower, int upper)
10592 int i;
10594 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10595 return isl_bool_error;
10597 pos += isl_basic_map_offset(bmap, type);
10599 for (i = 0; i < bmap->n_div; ++i) {
10600 if (isl_int_is_zero(bmap->div[i][0]))
10601 continue;
10602 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10603 return isl_bool_true;
10606 for (i = 0; i < bmap->n_eq; ++i)
10607 if (!isl_int_is_zero(bmap->eq[i][pos]))
10608 return isl_bool_true;
10610 for (i = 0; i < bmap->n_ineq; ++i) {
10611 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10612 if (sgn > 0)
10613 lower = 1;
10614 if (sgn < 0)
10615 upper = 1;
10618 return lower && upper;
10621 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10622 enum isl_dim_type type, unsigned pos)
10624 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10627 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10628 enum isl_dim_type type, unsigned pos)
10630 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10633 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10634 enum isl_dim_type type, unsigned pos)
10636 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10639 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10640 enum isl_dim_type type, unsigned pos)
10642 int i;
10644 if (!map)
10645 return isl_bool_error;
10647 for (i = 0; i < map->n; ++i) {
10648 isl_bool bounded;
10649 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10650 if (bounded < 0 || !bounded)
10651 return bounded;
10654 return isl_bool_true;
10657 /* Return true if the specified dim is involved in both an upper bound
10658 * and a lower bound.
10660 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10661 enum isl_dim_type type, unsigned pos)
10663 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10666 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10668 static isl_bool has_any_bound(__isl_keep isl_map *map,
10669 enum isl_dim_type type, unsigned pos,
10670 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10671 enum isl_dim_type type, unsigned pos))
10673 int i;
10675 if (!map)
10676 return isl_bool_error;
10678 for (i = 0; i < map->n; ++i) {
10679 isl_bool bounded;
10680 bounded = fn(map->p[i], type, pos);
10681 if (bounded < 0 || bounded)
10682 return bounded;
10685 return isl_bool_false;
10688 /* Return 1 if the specified dim is involved in any lower bound.
10690 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10691 enum isl_dim_type type, unsigned pos)
10693 return has_any_bound(set, type, pos,
10694 &isl_basic_map_dim_has_lower_bound);
10697 /* Return 1 if the specified dim is involved in any upper bound.
10699 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10700 enum isl_dim_type type, unsigned pos)
10702 return has_any_bound(set, type, pos,
10703 &isl_basic_map_dim_has_upper_bound);
10706 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10708 static isl_bool has_bound(__isl_keep isl_map *map,
10709 enum isl_dim_type type, unsigned pos,
10710 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10711 enum isl_dim_type type, unsigned pos))
10713 int i;
10715 if (!map)
10716 return isl_bool_error;
10718 for (i = 0; i < map->n; ++i) {
10719 isl_bool bounded;
10720 bounded = fn(map->p[i], type, pos);
10721 if (bounded < 0 || !bounded)
10722 return bounded;
10725 return isl_bool_true;
10728 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10730 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10731 enum isl_dim_type type, unsigned pos)
10733 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10736 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10738 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10739 enum isl_dim_type type, unsigned pos)
10741 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10744 /* For each of the "n" variables starting at "first", determine
10745 * the sign of the variable and put the results in the first "n"
10746 * elements of the array "signs".
10747 * Sign
10748 * 1 means that the variable is non-negative
10749 * -1 means that the variable is non-positive
10750 * 0 means the variable attains both positive and negative values.
10752 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10753 unsigned first, unsigned n, int *signs)
10755 isl_vec *bound = NULL;
10756 struct isl_tab *tab = NULL;
10757 struct isl_tab_undo *snap;
10758 int i;
10760 if (!bset || !signs)
10761 return isl_stat_error;
10763 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10764 tab = isl_tab_from_basic_set(bset, 0);
10765 if (!bound || !tab)
10766 goto error;
10768 isl_seq_clr(bound->el, bound->size);
10769 isl_int_set_si(bound->el[0], -1);
10771 snap = isl_tab_snap(tab);
10772 for (i = 0; i < n; ++i) {
10773 int empty;
10775 isl_int_set_si(bound->el[1 + first + i], -1);
10776 if (isl_tab_add_ineq(tab, bound->el) < 0)
10777 goto error;
10778 empty = tab->empty;
10779 isl_int_set_si(bound->el[1 + first + i], 0);
10780 if (isl_tab_rollback(tab, snap) < 0)
10781 goto error;
10783 if (empty) {
10784 signs[i] = 1;
10785 continue;
10788 isl_int_set_si(bound->el[1 + first + i], 1);
10789 if (isl_tab_add_ineq(tab, bound->el) < 0)
10790 goto error;
10791 empty = tab->empty;
10792 isl_int_set_si(bound->el[1 + first + i], 0);
10793 if (isl_tab_rollback(tab, snap) < 0)
10794 goto error;
10796 signs[i] = empty ? -1 : 0;
10799 isl_tab_free(tab);
10800 isl_vec_free(bound);
10801 return isl_stat_ok;
10802 error:
10803 isl_tab_free(tab);
10804 isl_vec_free(bound);
10805 return isl_stat_error;
10808 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10809 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10811 if (!bset || !signs)
10812 return isl_stat_error;
10813 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10814 return isl_stat_error);
10816 first += pos(bset->dim, type) - 1;
10817 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10820 /* Is it possible for the integer division "div" to depend (possibly
10821 * indirectly) on any output dimensions?
10823 * If the div is undefined, then we conservatively assume that it
10824 * may depend on them.
10825 * Otherwise, we check if it actually depends on them or on any integer
10826 * divisions that may depend on them.
10828 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10830 int i;
10831 unsigned n_out, o_out;
10832 unsigned n_div, o_div;
10834 if (isl_int_is_zero(bmap->div[div][0]))
10835 return isl_bool_true;
10837 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10838 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10840 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10841 return isl_bool_true;
10843 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10844 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10846 for (i = 0; i < n_div; ++i) {
10847 isl_bool may_involve;
10849 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10850 continue;
10851 may_involve = div_may_involve_output(bmap, i);
10852 if (may_involve < 0 || may_involve)
10853 return may_involve;
10856 return isl_bool_false;
10859 /* Return the first integer division of "bmap" in the range
10860 * [first, first + n[ that may depend on any output dimensions and
10861 * that has a non-zero coefficient in "c" (where the first coefficient
10862 * in "c" corresponds to integer division "first").
10864 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10865 isl_int *c, int first, int n)
10867 int k;
10869 if (!bmap)
10870 return -1;
10872 for (k = first; k < first + n; ++k) {
10873 isl_bool may_involve;
10875 if (isl_int_is_zero(c[k]))
10876 continue;
10877 may_involve = div_may_involve_output(bmap, k);
10878 if (may_involve < 0)
10879 return -1;
10880 if (may_involve)
10881 return k;
10884 return first + n;
10887 /* Look for a pair of inequality constraints in "bmap" of the form
10889 * -l + i >= 0 or i >= l
10890 * and
10891 * n + l - i >= 0 or i <= l + n
10893 * with n < "m" and i the output dimension at position "pos".
10894 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10895 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10896 * and earlier output dimensions, as well as integer divisions that do
10897 * not involve any of the output dimensions.
10899 * Return the index of the first inequality constraint or bmap->n_ineq
10900 * if no such pair can be found.
10902 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10903 int pos, isl_int m)
10905 int i, j;
10906 isl_ctx *ctx;
10907 unsigned total;
10908 unsigned n_div, o_div;
10909 unsigned n_out, o_out;
10910 int less;
10912 if (!bmap)
10913 return -1;
10915 ctx = isl_basic_map_get_ctx(bmap);
10916 total = isl_basic_map_total_dim(bmap);
10917 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10918 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10919 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10920 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10921 for (i = 0; i < bmap->n_ineq; ++i) {
10922 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10923 continue;
10924 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10925 n_out - (pos + 1)) != -1)
10926 continue;
10927 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10928 0, n_div) < n_div)
10929 continue;
10930 for (j = i + 1; j < bmap->n_ineq; ++j) {
10931 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10932 ctx->one))
10933 continue;
10934 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10935 bmap->ineq[j] + 1, total))
10936 continue;
10937 break;
10939 if (j >= bmap->n_ineq)
10940 continue;
10941 isl_int_add(bmap->ineq[i][0],
10942 bmap->ineq[i][0], bmap->ineq[j][0]);
10943 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10944 isl_int_sub(bmap->ineq[i][0],
10945 bmap->ineq[i][0], bmap->ineq[j][0]);
10946 if (!less)
10947 continue;
10948 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10949 return i;
10950 else
10951 return j;
10954 return bmap->n_ineq;
10957 /* Return the index of the equality of "bmap" that defines
10958 * the output dimension "pos" in terms of earlier dimensions.
10959 * The equality may also involve integer divisions, as long
10960 * as those integer divisions are defined in terms of
10961 * parameters or input dimensions.
10962 * In this case, *div is set to the number of integer divisions and
10963 * *ineq is set to the number of inequality constraints (provided
10964 * div and ineq are not NULL).
10966 * The equality may also involve a single integer division involving
10967 * the output dimensions (typically only output dimension "pos") as
10968 * long as the coefficient of output dimension "pos" is 1 or -1 and
10969 * there is a pair of constraints i >= l and i <= l + n, with i referring
10970 * to output dimension "pos", l an expression involving only earlier
10971 * dimensions and n smaller than the coefficient of the integer division
10972 * in the equality. In this case, the output dimension can be defined
10973 * in terms of a modulo expression that does not involve the integer division.
10974 * *div is then set to this single integer division and
10975 * *ineq is set to the index of constraint i >= l.
10977 * Return bmap->n_eq if there is no such equality.
10978 * Return -1 on error.
10980 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10981 int pos, int *div, int *ineq)
10983 int j, k, l;
10984 unsigned n_out, o_out;
10985 unsigned n_div, o_div;
10987 if (!bmap)
10988 return -1;
10990 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10991 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10992 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10993 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10995 if (ineq)
10996 *ineq = bmap->n_ineq;
10997 if (div)
10998 *div = n_div;
10999 for (j = 0; j < bmap->n_eq; ++j) {
11000 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11001 continue;
11002 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11003 n_out - (pos + 1)) != -1)
11004 continue;
11005 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11006 0, n_div);
11007 if (k >= n_div)
11008 return j;
11009 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11010 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11011 continue;
11012 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11013 k + 1, n_div - (k+1)) < n_div)
11014 continue;
11015 l = find_modulo_constraint_pair(bmap, pos,
11016 bmap->eq[j][o_div + k]);
11017 if (l < 0)
11018 return -1;
11019 if (l >= bmap->n_ineq)
11020 continue;
11021 if (div)
11022 *div = k;
11023 if (ineq)
11024 *ineq = l;
11025 return j;
11028 return bmap->n_eq;
11031 /* Check if the given basic map is obviously single-valued.
11032 * In particular, for each output dimension, check that there is
11033 * an equality that defines the output dimension in terms of
11034 * earlier dimensions.
11036 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11038 int i;
11039 unsigned n_out;
11041 if (!bmap)
11042 return isl_bool_error;
11044 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11046 for (i = 0; i < n_out; ++i) {
11047 int eq;
11049 eq = isl_basic_map_output_defining_equality(bmap, i,
11050 NULL, NULL);
11051 if (eq < 0)
11052 return isl_bool_error;
11053 if (eq >= bmap->n_eq)
11054 return isl_bool_false;
11057 return isl_bool_true;
11060 /* Check if the given basic map is single-valued.
11061 * We simply compute
11063 * M \circ M^-1
11065 * and check if the result is a subset of the identity mapping.
11067 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11069 isl_space *space;
11070 isl_basic_map *test;
11071 isl_basic_map *id;
11072 isl_bool sv;
11074 sv = isl_basic_map_plain_is_single_valued(bmap);
11075 if (sv < 0 || sv)
11076 return sv;
11078 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11079 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11081 space = isl_basic_map_get_space(bmap);
11082 space = isl_space_map_from_set(isl_space_range(space));
11083 id = isl_basic_map_identity(space);
11085 sv = isl_basic_map_is_subset(test, id);
11087 isl_basic_map_free(test);
11088 isl_basic_map_free(id);
11090 return sv;
11093 /* Check if the given map is obviously single-valued.
11095 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11097 if (!map)
11098 return isl_bool_error;
11099 if (map->n == 0)
11100 return isl_bool_true;
11101 if (map->n >= 2)
11102 return isl_bool_false;
11104 return isl_basic_map_plain_is_single_valued(map->p[0]);
11107 /* Check if the given map is single-valued.
11108 * We simply compute
11110 * M \circ M^-1
11112 * and check if the result is a subset of the identity mapping.
11114 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11116 isl_space *dim;
11117 isl_map *test;
11118 isl_map *id;
11119 isl_bool sv;
11121 sv = isl_map_plain_is_single_valued(map);
11122 if (sv < 0 || sv)
11123 return sv;
11125 test = isl_map_reverse(isl_map_copy(map));
11126 test = isl_map_apply_range(test, isl_map_copy(map));
11128 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11129 id = isl_map_identity(dim);
11131 sv = isl_map_is_subset(test, id);
11133 isl_map_free(test);
11134 isl_map_free(id);
11136 return sv;
11139 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11141 isl_bool in;
11143 map = isl_map_copy(map);
11144 map = isl_map_reverse(map);
11145 in = isl_map_is_single_valued(map);
11146 isl_map_free(map);
11148 return in;
11151 /* Check if the given map is obviously injective.
11153 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11155 isl_bool in;
11157 map = isl_map_copy(map);
11158 map = isl_map_reverse(map);
11159 in = isl_map_plain_is_single_valued(map);
11160 isl_map_free(map);
11162 return in;
11165 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11167 isl_bool sv;
11169 sv = isl_map_is_single_valued(map);
11170 if (sv < 0 || !sv)
11171 return sv;
11173 return isl_map_is_injective(map);
11176 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11178 return isl_map_is_single_valued(set_to_map(set));
11181 /* Does "map" only map elements to themselves?
11183 * If the domain and range spaces are different, then "map"
11184 * is considered not to be an identity relation, even if it is empty.
11185 * Otherwise, construct the maximal identity relation and
11186 * check whether "map" is a subset of this relation.
11188 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11190 isl_space *space;
11191 isl_map *id;
11192 isl_bool equal, is_identity;
11194 space = isl_map_get_space(map);
11195 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11196 isl_space_free(space);
11197 if (equal < 0 || !equal)
11198 return equal;
11200 id = isl_map_identity(isl_map_get_space(map));
11201 is_identity = isl_map_is_subset(map, id);
11202 isl_map_free(id);
11204 return is_identity;
11207 int isl_map_is_translation(__isl_keep isl_map *map)
11209 int ok;
11210 isl_set *delta;
11212 delta = isl_map_deltas(isl_map_copy(map));
11213 ok = isl_set_is_singleton(delta);
11214 isl_set_free(delta);
11216 return ok;
11219 static int unique(isl_int *p, unsigned pos, unsigned len)
11221 if (isl_seq_first_non_zero(p, pos) != -1)
11222 return 0;
11223 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11224 return 0;
11225 return 1;
11228 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11230 int i, j;
11231 unsigned nvar;
11232 unsigned ovar;
11234 if (!bset)
11235 return isl_bool_error;
11237 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11238 return isl_bool_false;
11240 nvar = isl_basic_set_dim(bset, isl_dim_set);
11241 ovar = isl_space_offset(bset->dim, isl_dim_set);
11242 for (j = 0; j < nvar; ++j) {
11243 int lower = 0, upper = 0;
11244 for (i = 0; i < bset->n_eq; ++i) {
11245 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11246 continue;
11247 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11248 return isl_bool_false;
11249 break;
11251 if (i < bset->n_eq)
11252 continue;
11253 for (i = 0; i < bset->n_ineq; ++i) {
11254 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11255 continue;
11256 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11257 return isl_bool_false;
11258 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11259 lower = 1;
11260 else
11261 upper = 1;
11263 if (!lower || !upper)
11264 return isl_bool_false;
11267 return isl_bool_true;
11270 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11272 if (!set)
11273 return isl_bool_error;
11274 if (set->n != 1)
11275 return isl_bool_false;
11277 return isl_basic_set_is_box(set->p[0]);
11280 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11282 if (!bset)
11283 return isl_bool_error;
11285 return isl_space_is_wrapping(bset->dim);
11288 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11290 if (!set)
11291 return isl_bool_error;
11293 return isl_space_is_wrapping(set->dim);
11296 /* Modify the space of "map" through a call to "change".
11297 * If "can_change" is set (not NULL), then first call it to check
11298 * if the modification is allowed, printing the error message "cannot_change"
11299 * if it is not.
11301 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11302 isl_bool (*can_change)(__isl_keep isl_map *map),
11303 const char *cannot_change,
11304 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11306 isl_bool ok;
11307 isl_space *space;
11309 if (!map)
11310 return NULL;
11312 ok = can_change ? can_change(map) : isl_bool_true;
11313 if (ok < 0)
11314 return isl_map_free(map);
11315 if (!ok)
11316 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11317 return isl_map_free(map));
11319 space = change(isl_map_get_space(map));
11320 map = isl_map_reset_space(map, space);
11322 return map;
11325 /* Is the domain of "map" a wrapped relation?
11327 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11329 if (!map)
11330 return isl_bool_error;
11332 return isl_space_domain_is_wrapping(map->dim);
11335 /* Does "map" have a wrapped relation in both domain and range?
11337 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11339 return isl_space_is_product(isl_map_peek_space(map));
11342 /* Is the range of "map" a wrapped relation?
11344 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11346 if (!map)
11347 return isl_bool_error;
11349 return isl_space_range_is_wrapping(map->dim);
11352 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11354 bmap = isl_basic_map_cow(bmap);
11355 if (!bmap)
11356 return NULL;
11358 bmap->dim = isl_space_wrap(bmap->dim);
11359 if (!bmap->dim)
11360 goto error;
11362 bmap = isl_basic_map_finalize(bmap);
11364 return bset_from_bmap(bmap);
11365 error:
11366 isl_basic_map_free(bmap);
11367 return NULL;
11370 /* Given a map A -> B, return the set (A -> B).
11372 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11374 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11377 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11379 bset = isl_basic_set_cow(bset);
11380 if (!bset)
11381 return NULL;
11383 bset->dim = isl_space_unwrap(bset->dim);
11384 if (!bset->dim)
11385 goto error;
11387 bset = isl_basic_set_finalize(bset);
11389 return bset_to_bmap(bset);
11390 error:
11391 isl_basic_set_free(bset);
11392 return NULL;
11395 /* Given a set (A -> B), return the map A -> B.
11396 * Error out if "set" is not of the form (A -> B).
11398 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11400 return isl_map_change_space(set, &isl_set_is_wrapping,
11401 "not a wrapping set", &isl_space_unwrap);
11404 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11405 enum isl_dim_type type)
11407 if (!bmap)
11408 return NULL;
11410 if (!isl_space_is_named_or_nested(bmap->dim, type))
11411 return bmap;
11413 bmap = isl_basic_map_cow(bmap);
11414 if (!bmap)
11415 return NULL;
11417 bmap->dim = isl_space_reset(bmap->dim, type);
11418 if (!bmap->dim)
11419 goto error;
11421 bmap = isl_basic_map_finalize(bmap);
11423 return bmap;
11424 error:
11425 isl_basic_map_free(bmap);
11426 return NULL;
11429 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11430 enum isl_dim_type type)
11432 int i;
11434 if (!map)
11435 return NULL;
11437 if (!isl_space_is_named_or_nested(map->dim, type))
11438 return map;
11440 map = isl_map_cow(map);
11441 if (!map)
11442 return NULL;
11444 for (i = 0; i < map->n; ++i) {
11445 map->p[i] = isl_basic_map_reset(map->p[i], type);
11446 if (!map->p[i])
11447 goto error;
11449 map->dim = isl_space_reset(map->dim, type);
11450 if (!map->dim)
11451 goto error;
11453 return map;
11454 error:
11455 isl_map_free(map);
11456 return NULL;
11459 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11461 if (!bmap)
11462 return NULL;
11464 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11465 return bmap;
11467 bmap = isl_basic_map_cow(bmap);
11468 if (!bmap)
11469 return NULL;
11471 bmap->dim = isl_space_flatten(bmap->dim);
11472 if (!bmap->dim)
11473 goto error;
11475 bmap = isl_basic_map_finalize(bmap);
11477 return bmap;
11478 error:
11479 isl_basic_map_free(bmap);
11480 return NULL;
11483 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11485 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11488 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11489 __isl_take isl_basic_map *bmap)
11491 if (!bmap)
11492 return NULL;
11494 if (!bmap->dim->nested[0])
11495 return bmap;
11497 bmap = isl_basic_map_cow(bmap);
11498 if (!bmap)
11499 return NULL;
11501 bmap->dim = isl_space_flatten_domain(bmap->dim);
11502 if (!bmap->dim)
11503 goto error;
11505 bmap = isl_basic_map_finalize(bmap);
11507 return bmap;
11508 error:
11509 isl_basic_map_free(bmap);
11510 return NULL;
11513 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11514 __isl_take isl_basic_map *bmap)
11516 if (!bmap)
11517 return NULL;
11519 if (!bmap->dim->nested[1])
11520 return bmap;
11522 bmap = isl_basic_map_cow(bmap);
11523 if (!bmap)
11524 return NULL;
11526 bmap->dim = isl_space_flatten_range(bmap->dim);
11527 if (!bmap->dim)
11528 goto error;
11530 bmap = isl_basic_map_finalize(bmap);
11532 return bmap;
11533 error:
11534 isl_basic_map_free(bmap);
11535 return NULL;
11538 /* Remove any internal structure from the spaces of domain and range of "map".
11540 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11542 if (!map)
11543 return NULL;
11545 if (!map->dim->nested[0] && !map->dim->nested[1])
11546 return map;
11548 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11551 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11553 return set_from_map(isl_map_flatten(set_to_map(set)));
11556 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11558 isl_space *space, *flat_space;
11559 isl_map *map;
11561 space = isl_set_get_space(set);
11562 flat_space = isl_space_flatten(isl_space_copy(space));
11563 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11564 flat_space));
11565 map = isl_map_intersect_domain(map, set);
11567 return map;
11570 /* Remove any internal structure from the space of the domain of "map".
11572 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11574 if (!map)
11575 return NULL;
11577 if (!map->dim->nested[0])
11578 return map;
11580 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11583 /* Remove any internal structure from the space of the range of "map".
11585 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11587 if (!map)
11588 return NULL;
11590 if (!map->dim->nested[1])
11591 return map;
11593 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11596 /* Reorder the dimensions of "bmap" according to the given dim_map
11597 * and set the dimension specification to "space" and
11598 * perform Gaussian elimination on the result.
11600 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11601 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11603 isl_basic_map *res;
11604 unsigned flags;
11605 unsigned n_div;
11607 if (!bmap || !space || !dim_map)
11608 goto error;
11610 flags = bmap->flags;
11611 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11612 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11613 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11614 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11615 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11616 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11617 if (res)
11618 res->flags = flags;
11619 res = isl_basic_map_gauss(res, NULL);
11620 res = isl_basic_map_finalize(res);
11621 return res;
11622 error:
11623 free(dim_map);
11624 isl_basic_map_free(bmap);
11625 isl_space_free(space);
11626 return NULL;
11629 /* Reorder the dimensions of "map" according to given reordering.
11631 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11632 __isl_take isl_reordering *r)
11634 int i;
11635 struct isl_dim_map *dim_map;
11637 map = isl_map_cow(map);
11638 dim_map = isl_dim_map_from_reordering(r);
11639 if (!map || !r || !dim_map)
11640 goto error;
11642 for (i = 0; i < map->n; ++i) {
11643 struct isl_dim_map *dim_map_i;
11644 isl_space *space;
11646 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11648 space = isl_reordering_get_space(r);
11649 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11651 if (!map->p[i])
11652 goto error;
11655 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11656 map = isl_map_unmark_normalized(map);
11658 isl_reordering_free(r);
11659 free(dim_map);
11660 return map;
11661 error:
11662 free(dim_map);
11663 isl_map_free(map);
11664 isl_reordering_free(r);
11665 return NULL;
11668 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11669 __isl_take isl_reordering *r)
11671 return set_from_map(isl_map_realign(set_to_map(set), r));
11674 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11675 __isl_take isl_space *model)
11677 isl_ctx *ctx;
11678 isl_bool aligned;
11680 if (!map || !model)
11681 goto error;
11683 ctx = isl_space_get_ctx(model);
11684 if (!isl_space_has_named_params(model))
11685 isl_die(ctx, isl_error_invalid,
11686 "model has unnamed parameters", goto error);
11687 if (isl_map_check_named_params(map) < 0)
11688 goto error;
11689 aligned = isl_map_space_has_equal_params(map, model);
11690 if (aligned < 0)
11691 goto error;
11692 if (!aligned) {
11693 isl_reordering *exp;
11695 exp = isl_parameter_alignment_reordering(map->dim, model);
11696 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11697 map = isl_map_realign(map, exp);
11700 isl_space_free(model);
11701 return map;
11702 error:
11703 isl_space_free(model);
11704 isl_map_free(map);
11705 return NULL;
11708 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11709 __isl_take isl_space *model)
11711 return isl_map_align_params(set, model);
11714 /* Align the parameters of "bmap" to those of "model", introducing
11715 * additional parameters if needed.
11717 __isl_give isl_basic_map *isl_basic_map_align_params(
11718 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11720 isl_ctx *ctx;
11721 isl_bool equal_params;
11723 if (!bmap || !model)
11724 goto error;
11726 ctx = isl_space_get_ctx(model);
11727 if (!isl_space_has_named_params(model))
11728 isl_die(ctx, isl_error_invalid,
11729 "model has unnamed parameters", goto error);
11730 if (isl_basic_map_check_named_params(bmap) < 0)
11731 goto error;
11732 equal_params = isl_space_has_equal_params(bmap->dim, model);
11733 if (equal_params < 0)
11734 goto error;
11735 if (!equal_params) {
11736 isl_reordering *exp;
11737 struct isl_dim_map *dim_map;
11739 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11740 exp = isl_reordering_extend_space(exp,
11741 isl_basic_map_get_space(bmap));
11742 dim_map = isl_dim_map_from_reordering(exp);
11743 bmap = isl_basic_map_realign(bmap,
11744 isl_reordering_get_space(exp),
11745 isl_dim_map_extend(dim_map, bmap));
11746 isl_reordering_free(exp);
11747 free(dim_map);
11750 isl_space_free(model);
11751 return bmap;
11752 error:
11753 isl_space_free(model);
11754 isl_basic_map_free(bmap);
11755 return NULL;
11758 /* Do "bset" and "space" have the same parameters?
11760 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11761 __isl_keep isl_space *space)
11763 isl_space *bset_space;
11765 bset_space = isl_basic_set_peek_space(bset);
11766 return isl_space_has_equal_params(bset_space, space);
11769 /* Do "map" and "space" have the same parameters?
11771 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11772 __isl_keep isl_space *space)
11774 isl_space *map_space;
11776 map_space = isl_map_peek_space(map);
11777 return isl_space_has_equal_params(map_space, space);
11780 /* Do "set" and "space" have the same parameters?
11782 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11783 __isl_keep isl_space *space)
11785 return isl_map_space_has_equal_params(set_to_map(set), space);
11788 /* Align the parameters of "bset" to those of "model", introducing
11789 * additional parameters if needed.
11791 __isl_give isl_basic_set *isl_basic_set_align_params(
11792 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11794 return isl_basic_map_align_params(bset, model);
11797 /* Drop all parameters not referenced by "map".
11799 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11801 int i;
11803 if (isl_map_check_named_params(map) < 0)
11804 return isl_map_free(map);
11806 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11807 isl_bool involves;
11809 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11810 if (involves < 0)
11811 return isl_map_free(map);
11812 if (!involves)
11813 map = isl_map_project_out(map, isl_dim_param, i, 1);
11816 return map;
11819 /* Drop all parameters not referenced by "set".
11821 __isl_give isl_set *isl_set_drop_unused_params(
11822 __isl_take isl_set *set)
11824 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11827 /* Drop all parameters not referenced by "bmap".
11829 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11830 __isl_take isl_basic_map *bmap)
11832 int i;
11834 if (isl_basic_map_check_named_params(bmap) < 0)
11835 return isl_basic_map_free(bmap);
11837 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11838 isl_bool involves;
11840 involves = isl_basic_map_involves_dims(bmap,
11841 isl_dim_param, i, 1);
11842 if (involves < 0)
11843 return isl_basic_map_free(bmap);
11844 if (!involves)
11845 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11848 return bmap;
11851 /* Drop all parameters not referenced by "bset".
11853 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11854 __isl_take isl_basic_set *bset)
11856 return bset_from_bmap(isl_basic_map_drop_unused_params(
11857 bset_to_bmap(bset)));
11860 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11861 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11862 enum isl_dim_type c2, enum isl_dim_type c3,
11863 enum isl_dim_type c4, enum isl_dim_type c5)
11865 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11866 struct isl_mat *mat;
11867 int i, j, k;
11868 int pos;
11870 if (!bmap)
11871 return NULL;
11872 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11873 isl_basic_map_total_dim(bmap) + 1);
11874 if (!mat)
11875 return NULL;
11876 for (i = 0; i < bmap->n_eq; ++i)
11877 for (j = 0, pos = 0; j < 5; ++j) {
11878 int off = isl_basic_map_offset(bmap, c[j]);
11879 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11880 isl_int_set(mat->row[i][pos],
11881 bmap->eq[i][off + k]);
11882 ++pos;
11886 return mat;
11889 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11890 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11891 enum isl_dim_type c2, enum isl_dim_type c3,
11892 enum isl_dim_type c4, enum isl_dim_type c5)
11894 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11895 struct isl_mat *mat;
11896 int i, j, k;
11897 int pos;
11899 if (!bmap)
11900 return NULL;
11901 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11902 isl_basic_map_total_dim(bmap) + 1);
11903 if (!mat)
11904 return NULL;
11905 for (i = 0; i < bmap->n_ineq; ++i)
11906 for (j = 0, pos = 0; j < 5; ++j) {
11907 int off = isl_basic_map_offset(bmap, c[j]);
11908 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11909 isl_int_set(mat->row[i][pos],
11910 bmap->ineq[i][off + k]);
11911 ++pos;
11915 return mat;
11918 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11919 __isl_take isl_space *space,
11920 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11921 enum isl_dim_type c2, enum isl_dim_type c3,
11922 enum isl_dim_type c4, enum isl_dim_type c5)
11924 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11925 isl_basic_map *bmap = NULL;
11926 unsigned total;
11927 unsigned extra;
11928 int i, j, k, l;
11929 int pos;
11931 if (!space || !eq || !ineq)
11932 goto error;
11934 if (eq->n_col != ineq->n_col)
11935 isl_die(space->ctx, isl_error_invalid,
11936 "equalities and inequalities matrices should have "
11937 "same number of columns", goto error);
11939 total = 1 + isl_space_dim(space, isl_dim_all);
11941 if (eq->n_col < total)
11942 isl_die(space->ctx, isl_error_invalid,
11943 "number of columns too small", goto error);
11945 extra = eq->n_col - total;
11947 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11948 eq->n_row, ineq->n_row);
11949 if (!bmap)
11950 goto error;
11951 for (i = 0; i < extra; ++i) {
11952 k = isl_basic_map_alloc_div(bmap);
11953 if (k < 0)
11954 goto error;
11955 isl_int_set_si(bmap->div[k][0], 0);
11957 for (i = 0; i < eq->n_row; ++i) {
11958 l = isl_basic_map_alloc_equality(bmap);
11959 if (l < 0)
11960 goto error;
11961 for (j = 0, pos = 0; j < 5; ++j) {
11962 int off = isl_basic_map_offset(bmap, c[j]);
11963 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11964 isl_int_set(bmap->eq[l][off + k],
11965 eq->row[i][pos]);
11966 ++pos;
11970 for (i = 0; i < ineq->n_row; ++i) {
11971 l = isl_basic_map_alloc_inequality(bmap);
11972 if (l < 0)
11973 goto error;
11974 for (j = 0, pos = 0; j < 5; ++j) {
11975 int off = isl_basic_map_offset(bmap, c[j]);
11976 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11977 isl_int_set(bmap->ineq[l][off + k],
11978 ineq->row[i][pos]);
11979 ++pos;
11984 isl_space_free(space);
11985 isl_mat_free(eq);
11986 isl_mat_free(ineq);
11988 bmap = isl_basic_map_simplify(bmap);
11989 return isl_basic_map_finalize(bmap);
11990 error:
11991 isl_space_free(space);
11992 isl_mat_free(eq);
11993 isl_mat_free(ineq);
11994 isl_basic_map_free(bmap);
11995 return NULL;
11998 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11999 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12000 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12002 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12003 c1, c2, c3, c4, isl_dim_in);
12006 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12007 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12008 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12010 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12011 c1, c2, c3, c4, isl_dim_in);
12014 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12015 __isl_take isl_space *dim,
12016 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12017 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12019 isl_basic_map *bmap;
12020 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12021 c1, c2, c3, c4, isl_dim_in);
12022 return bset_from_bmap(bmap);
12025 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12027 if (!bmap)
12028 return isl_bool_error;
12030 return isl_space_can_zip(bmap->dim);
12033 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12035 if (!map)
12036 return isl_bool_error;
12038 return isl_space_can_zip(map->dim);
12041 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12042 * (A -> C) -> (B -> D).
12044 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12046 unsigned pos;
12047 unsigned n1;
12048 unsigned n2;
12050 if (!bmap)
12051 return NULL;
12053 if (!isl_basic_map_can_zip(bmap))
12054 isl_die(bmap->ctx, isl_error_invalid,
12055 "basic map cannot be zipped", goto error);
12056 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12057 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12058 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12059 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12060 bmap = isl_basic_map_cow(bmap);
12061 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12062 if (!bmap)
12063 return NULL;
12064 bmap->dim = isl_space_zip(bmap->dim);
12065 if (!bmap->dim)
12066 goto error;
12067 bmap = isl_basic_map_mark_final(bmap);
12068 return bmap;
12069 error:
12070 isl_basic_map_free(bmap);
12071 return NULL;
12074 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12075 * (A -> C) -> (B -> D).
12077 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12079 int i;
12081 if (!map)
12082 return NULL;
12084 if (!isl_map_can_zip(map))
12085 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12086 goto error);
12088 map = isl_map_cow(map);
12089 if (!map)
12090 return NULL;
12092 for (i = 0; i < map->n; ++i) {
12093 map->p[i] = isl_basic_map_zip(map->p[i]);
12094 if (!map->p[i])
12095 goto error;
12098 map->dim = isl_space_zip(map->dim);
12099 if (!map->dim)
12100 goto error;
12102 return map;
12103 error:
12104 isl_map_free(map);
12105 return NULL;
12108 /* Can we apply isl_basic_map_curry to "bmap"?
12109 * That is, does it have a nested relation in its domain?
12111 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12113 if (!bmap)
12114 return isl_bool_error;
12116 return isl_space_can_curry(bmap->dim);
12119 /* Can we apply isl_map_curry to "map"?
12120 * That is, does it have a nested relation in its domain?
12122 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12124 if (!map)
12125 return isl_bool_error;
12127 return isl_space_can_curry(map->dim);
12130 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12131 * A -> (B -> C).
12133 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12136 if (!bmap)
12137 return NULL;
12139 if (!isl_basic_map_can_curry(bmap))
12140 isl_die(bmap->ctx, isl_error_invalid,
12141 "basic map cannot be curried", goto error);
12142 bmap = isl_basic_map_cow(bmap);
12143 if (!bmap)
12144 return NULL;
12145 bmap->dim = isl_space_curry(bmap->dim);
12146 if (!bmap->dim)
12147 goto error;
12148 bmap = isl_basic_map_mark_final(bmap);
12149 return bmap;
12150 error:
12151 isl_basic_map_free(bmap);
12152 return NULL;
12155 /* Given a map (A -> B) -> C, return the corresponding map
12156 * A -> (B -> C).
12158 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12160 return isl_map_change_space(map, &isl_map_can_curry,
12161 "map cannot be curried", &isl_space_curry);
12164 /* Can isl_map_range_curry be applied to "map"?
12165 * That is, does it have a nested relation in its range,
12166 * the domain of which is itself a nested relation?
12168 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12170 if (!map)
12171 return isl_bool_error;
12173 return isl_space_can_range_curry(map->dim);
12176 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12177 * A -> (B -> (C -> D)).
12179 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12181 return isl_map_change_space(map, &isl_map_can_range_curry,
12182 "map range cannot be curried",
12183 &isl_space_range_curry);
12186 /* Can we apply isl_basic_map_uncurry to "bmap"?
12187 * That is, does it have a nested relation in its domain?
12189 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12191 if (!bmap)
12192 return isl_bool_error;
12194 return isl_space_can_uncurry(bmap->dim);
12197 /* Can we apply isl_map_uncurry to "map"?
12198 * That is, does it have a nested relation in its domain?
12200 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12202 if (!map)
12203 return isl_bool_error;
12205 return isl_space_can_uncurry(map->dim);
12208 /* Given a basic map A -> (B -> C), return the corresponding basic map
12209 * (A -> B) -> C.
12211 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12214 if (!bmap)
12215 return NULL;
12217 if (!isl_basic_map_can_uncurry(bmap))
12218 isl_die(bmap->ctx, isl_error_invalid,
12219 "basic map cannot be uncurried",
12220 return isl_basic_map_free(bmap));
12221 bmap = isl_basic_map_cow(bmap);
12222 if (!bmap)
12223 return NULL;
12224 bmap->dim = isl_space_uncurry(bmap->dim);
12225 if (!bmap->dim)
12226 return isl_basic_map_free(bmap);
12227 bmap = isl_basic_map_mark_final(bmap);
12228 return bmap;
12231 /* Given a map A -> (B -> C), return the corresponding map
12232 * (A -> B) -> C.
12234 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12236 return isl_map_change_space(map, &isl_map_can_uncurry,
12237 "map cannot be uncurried", &isl_space_uncurry);
12240 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12241 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12243 return isl_map_equate(set, type1, pos1, type2, pos2);
12246 /* Construct a basic map where the given dimensions are equal to each other.
12248 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12249 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12251 isl_basic_map *bmap = NULL;
12252 int i;
12254 if (!space)
12255 return NULL;
12257 if (pos1 >= isl_space_dim(space, type1))
12258 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12259 "index out of bounds", goto error);
12260 if (pos2 >= isl_space_dim(space, type2))
12261 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12262 "index out of bounds", goto error);
12264 if (type1 == type2 && pos1 == pos2)
12265 return isl_basic_map_universe(space);
12267 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12268 i = isl_basic_map_alloc_equality(bmap);
12269 if (i < 0)
12270 goto error;
12271 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12272 pos1 += isl_basic_map_offset(bmap, type1);
12273 pos2 += isl_basic_map_offset(bmap, type2);
12274 isl_int_set_si(bmap->eq[i][pos1], -1);
12275 isl_int_set_si(bmap->eq[i][pos2], 1);
12276 bmap = isl_basic_map_finalize(bmap);
12277 isl_space_free(space);
12278 return bmap;
12279 error:
12280 isl_space_free(space);
12281 isl_basic_map_free(bmap);
12282 return NULL;
12285 /* Add a constraint imposing that the given two dimensions are equal.
12287 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12288 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12290 isl_basic_map *eq;
12292 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12294 bmap = isl_basic_map_intersect(bmap, eq);
12296 return bmap;
12299 /* Add a constraint imposing that the given two dimensions are equal.
12301 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12302 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12304 isl_basic_map *bmap;
12306 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12308 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12310 return map;
12313 /* Add a constraint imposing that the given two dimensions have opposite values.
12315 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12316 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12318 isl_basic_map *bmap = NULL;
12319 int i;
12321 if (!map)
12322 return NULL;
12324 if (pos1 >= isl_map_dim(map, type1))
12325 isl_die(map->ctx, isl_error_invalid,
12326 "index out of bounds", goto error);
12327 if (pos2 >= isl_map_dim(map, type2))
12328 isl_die(map->ctx, isl_error_invalid,
12329 "index out of bounds", goto error);
12331 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12332 i = isl_basic_map_alloc_equality(bmap);
12333 if (i < 0)
12334 goto error;
12335 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12336 pos1 += isl_basic_map_offset(bmap, type1);
12337 pos2 += isl_basic_map_offset(bmap, type2);
12338 isl_int_set_si(bmap->eq[i][pos1], 1);
12339 isl_int_set_si(bmap->eq[i][pos2], 1);
12340 bmap = isl_basic_map_finalize(bmap);
12342 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12344 return map;
12345 error:
12346 isl_basic_map_free(bmap);
12347 isl_map_free(map);
12348 return NULL;
12351 /* Construct a constraint imposing that the value of the first dimension is
12352 * greater than or equal to that of the second.
12354 static __isl_give isl_constraint *constraint_order_ge(
12355 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12356 enum isl_dim_type type2, int pos2)
12358 isl_constraint *c;
12360 if (!space)
12361 return NULL;
12363 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12365 if (pos1 >= isl_constraint_dim(c, type1))
12366 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12367 "index out of bounds", return isl_constraint_free(c));
12368 if (pos2 >= isl_constraint_dim(c, type2))
12369 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12370 "index out of bounds", return isl_constraint_free(c));
12372 if (type1 == type2 && pos1 == pos2)
12373 return c;
12375 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12376 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12378 return c;
12381 /* Add a constraint imposing that the value of the first dimension is
12382 * greater than or equal to that of the second.
12384 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12385 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12387 isl_constraint *c;
12388 isl_space *space;
12390 if (type1 == type2 && pos1 == pos2)
12391 return bmap;
12392 space = isl_basic_map_get_space(bmap);
12393 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12394 bmap = isl_basic_map_add_constraint(bmap, c);
12396 return bmap;
12399 /* Add a constraint imposing that the value of the first dimension is
12400 * greater than or equal to that of the second.
12402 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12403 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12405 isl_constraint *c;
12406 isl_space *space;
12408 if (type1 == type2 && pos1 == pos2)
12409 return map;
12410 space = isl_map_get_space(map);
12411 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12412 map = isl_map_add_constraint(map, c);
12414 return map;
12417 /* Add a constraint imposing that the value of the first dimension is
12418 * less than or equal to that of the second.
12420 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12421 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12423 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12426 /* Construct a basic map where the value of the first dimension is
12427 * greater than that of the second.
12429 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12430 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12432 isl_basic_map *bmap = NULL;
12433 int i;
12435 if (!space)
12436 return NULL;
12438 if (pos1 >= isl_space_dim(space, type1))
12439 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12440 "index out of bounds", goto error);
12441 if (pos2 >= isl_space_dim(space, type2))
12442 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12443 "index out of bounds", goto error);
12445 if (type1 == type2 && pos1 == pos2)
12446 return isl_basic_map_empty(space);
12448 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12449 i = isl_basic_map_alloc_inequality(bmap);
12450 if (i < 0)
12451 return isl_basic_map_free(bmap);
12452 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12453 pos1 += isl_basic_map_offset(bmap, type1);
12454 pos2 += isl_basic_map_offset(bmap, type2);
12455 isl_int_set_si(bmap->ineq[i][pos1], 1);
12456 isl_int_set_si(bmap->ineq[i][pos2], -1);
12457 isl_int_set_si(bmap->ineq[i][0], -1);
12458 bmap = isl_basic_map_finalize(bmap);
12460 return bmap;
12461 error:
12462 isl_space_free(space);
12463 isl_basic_map_free(bmap);
12464 return NULL;
12467 /* Add a constraint imposing that the value of the first dimension is
12468 * greater than that of the second.
12470 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12471 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12473 isl_basic_map *gt;
12475 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12477 bmap = isl_basic_map_intersect(bmap, gt);
12479 return bmap;
12482 /* Add a constraint imposing that the value of the first dimension is
12483 * greater than that of the second.
12485 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12486 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12488 isl_basic_map *bmap;
12490 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12492 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12494 return map;
12497 /* Add a constraint imposing that the value of the first dimension is
12498 * smaller than that of the second.
12500 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12501 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12503 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12506 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12507 int pos)
12509 isl_aff *div;
12510 isl_local_space *ls;
12512 if (!bmap)
12513 return NULL;
12515 if (!isl_basic_map_divs_known(bmap))
12516 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12517 "some divs are unknown", return NULL);
12519 ls = isl_basic_map_get_local_space(bmap);
12520 div = isl_local_space_get_div(ls, pos);
12521 isl_local_space_free(ls);
12523 return div;
12526 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12527 int pos)
12529 return isl_basic_map_get_div(bset, pos);
12532 /* Plug in "subs" for dimension "type", "pos" of "bset".
12534 * Let i be the dimension to replace and let "subs" be of the form
12536 * f/d
12538 * Any integer division with a non-zero coefficient for i,
12540 * floor((a i + g)/m)
12542 * is replaced by
12544 * floor((a f + d g)/(m d))
12546 * Constraints of the form
12548 * a i + g
12550 * are replaced by
12552 * a f + d g
12554 * We currently require that "subs" is an integral expression.
12555 * Handling rational expressions may require us to add stride constraints
12556 * as we do in isl_basic_set_preimage_multi_aff.
12558 __isl_give isl_basic_set *isl_basic_set_substitute(
12559 __isl_take isl_basic_set *bset,
12560 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12562 int i;
12563 isl_int v;
12564 isl_ctx *ctx;
12566 if (bset && isl_basic_set_plain_is_empty(bset))
12567 return bset;
12569 bset = isl_basic_set_cow(bset);
12570 if (!bset || !subs)
12571 goto error;
12573 ctx = isl_basic_set_get_ctx(bset);
12574 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12575 isl_die(ctx, isl_error_invalid,
12576 "spaces don't match", goto error);
12577 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12578 isl_die(ctx, isl_error_unsupported,
12579 "cannot handle divs yet", goto error);
12580 if (!isl_int_is_one(subs->v->el[0]))
12581 isl_die(ctx, isl_error_invalid,
12582 "can only substitute integer expressions", goto error);
12584 pos += isl_basic_set_offset(bset, type);
12586 isl_int_init(v);
12588 for (i = 0; i < bset->n_eq; ++i) {
12589 if (isl_int_is_zero(bset->eq[i][pos]))
12590 continue;
12591 isl_int_set(v, bset->eq[i][pos]);
12592 isl_int_set_si(bset->eq[i][pos], 0);
12593 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12594 v, subs->v->el + 1, subs->v->size - 1);
12597 for (i = 0; i < bset->n_ineq; ++i) {
12598 if (isl_int_is_zero(bset->ineq[i][pos]))
12599 continue;
12600 isl_int_set(v, bset->ineq[i][pos]);
12601 isl_int_set_si(bset->ineq[i][pos], 0);
12602 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12603 v, subs->v->el + 1, subs->v->size - 1);
12606 for (i = 0; i < bset->n_div; ++i) {
12607 if (isl_int_is_zero(bset->div[i][1 + pos]))
12608 continue;
12609 isl_int_set(v, bset->div[i][1 + pos]);
12610 isl_int_set_si(bset->div[i][1 + pos], 0);
12611 isl_seq_combine(bset->div[i] + 1,
12612 subs->v->el[0], bset->div[i] + 1,
12613 v, subs->v->el + 1, subs->v->size - 1);
12614 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12617 isl_int_clear(v);
12619 bset = isl_basic_set_simplify(bset);
12620 return isl_basic_set_finalize(bset);
12621 error:
12622 isl_basic_set_free(bset);
12623 return NULL;
12626 /* Plug in "subs" for dimension "type", "pos" of "set".
12628 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12629 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12631 int i;
12633 if (set && isl_set_plain_is_empty(set))
12634 return set;
12636 set = isl_set_cow(set);
12637 if (!set || !subs)
12638 goto error;
12640 for (i = set->n - 1; i >= 0; --i) {
12641 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12642 set = set_from_map(remove_if_empty(set_to_map(set), i));
12643 if (!set)
12644 return NULL;
12647 return set;
12648 error:
12649 isl_set_free(set);
12650 return NULL;
12653 /* Check if the range of "ma" is compatible with the domain or range
12654 * (depending on "type") of "bmap".
12656 static isl_stat check_basic_map_compatible_range_multi_aff(
12657 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12658 __isl_keep isl_multi_aff *ma)
12660 isl_bool m;
12661 isl_space *ma_space;
12663 ma_space = isl_multi_aff_get_space(ma);
12665 m = isl_space_has_equal_params(bmap->dim, ma_space);
12666 if (m < 0)
12667 goto error;
12668 if (!m)
12669 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12670 "parameters don't match", goto error);
12671 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12672 if (m < 0)
12673 goto error;
12674 if (!m)
12675 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12676 "spaces don't match", goto error);
12678 isl_space_free(ma_space);
12679 return isl_stat_ok;
12680 error:
12681 isl_space_free(ma_space);
12682 return isl_stat_error;
12685 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12686 * coefficients before the transformed range of dimensions,
12687 * the "n_after" coefficients after the transformed range of dimensions
12688 * and the coefficients of the other divs in "bmap".
12690 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12691 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12693 int i;
12694 int n_param;
12695 int n_set;
12696 isl_local_space *ls;
12698 if (n_div == 0)
12699 return 0;
12701 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12702 if (!ls)
12703 return -1;
12705 n_param = isl_local_space_dim(ls, isl_dim_param);
12706 n_set = isl_local_space_dim(ls, isl_dim_set);
12707 for (i = 0; i < n_div; ++i) {
12708 int o_bmap = 0, o_ls = 0;
12710 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12711 o_bmap += 1 + 1 + n_param;
12712 o_ls += 1 + 1 + n_param;
12713 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12714 o_bmap += n_before;
12715 isl_seq_cpy(bmap->div[i] + o_bmap,
12716 ls->div->row[i] + o_ls, n_set);
12717 o_bmap += n_set;
12718 o_ls += n_set;
12719 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12720 o_bmap += n_after;
12721 isl_seq_cpy(bmap->div[i] + o_bmap,
12722 ls->div->row[i] + o_ls, n_div);
12723 o_bmap += n_div;
12724 o_ls += n_div;
12725 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12726 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12727 goto error;
12730 isl_local_space_free(ls);
12731 return 0;
12732 error:
12733 isl_local_space_free(ls);
12734 return -1;
12737 /* How many stride constraints does "ma" enforce?
12738 * That is, how many of the affine expressions have a denominator
12739 * different from one?
12741 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12743 int i;
12744 int strides = 0;
12746 for (i = 0; i < ma->n; ++i)
12747 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12748 strides++;
12750 return strides;
12753 /* For each affine expression in ma of the form
12755 * x_i = (f_i y + h_i)/m_i
12757 * with m_i different from one, add a constraint to "bmap"
12758 * of the form
12760 * f_i y + h_i = m_i alpha_i
12762 * with alpha_i an additional existentially quantified variable.
12764 * The input variables of "ma" correspond to a subset of the variables
12765 * of "bmap". There are "n_before" variables in "bmap" before this
12766 * subset and "n_after" variables after this subset.
12767 * The integer divisions of the affine expressions in "ma" are assumed
12768 * to have been aligned. There are "n_div_ma" of them and
12769 * they appear first in "bmap", straight after the "n_after" variables.
12771 static __isl_give isl_basic_map *add_ma_strides(
12772 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12773 int n_before, int n_after, int n_div_ma)
12775 int i, k;
12776 int div;
12777 int total;
12778 int n_param;
12779 int n_in;
12781 total = isl_basic_map_total_dim(bmap);
12782 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12783 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12784 for (i = 0; i < ma->n; ++i) {
12785 int o_bmap = 0, o_ma = 1;
12787 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12788 continue;
12789 div = isl_basic_map_alloc_div(bmap);
12790 k = isl_basic_map_alloc_equality(bmap);
12791 if (div < 0 || k < 0)
12792 goto error;
12793 isl_int_set_si(bmap->div[div][0], 0);
12794 isl_seq_cpy(bmap->eq[k] + o_bmap,
12795 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12796 o_bmap += 1 + n_param;
12797 o_ma += 1 + n_param;
12798 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12799 o_bmap += n_before;
12800 isl_seq_cpy(bmap->eq[k] + o_bmap,
12801 ma->u.p[i]->v->el + o_ma, n_in);
12802 o_bmap += n_in;
12803 o_ma += n_in;
12804 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12805 o_bmap += n_after;
12806 isl_seq_cpy(bmap->eq[k] + o_bmap,
12807 ma->u.p[i]->v->el + o_ma, n_div_ma);
12808 o_bmap += n_div_ma;
12809 o_ma += n_div_ma;
12810 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12811 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12812 total++;
12815 return bmap;
12816 error:
12817 isl_basic_map_free(bmap);
12818 return NULL;
12821 /* Replace the domain or range space (depending on "type) of "space" by "set".
12823 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12824 enum isl_dim_type type, __isl_take isl_space *set)
12826 if (type == isl_dim_in) {
12827 space = isl_space_range(space);
12828 space = isl_space_map_from_domain_and_range(set, space);
12829 } else {
12830 space = isl_space_domain(space);
12831 space = isl_space_map_from_domain_and_range(space, set);
12834 return space;
12837 /* Compute the preimage of the domain or range (depending on "type")
12838 * of "bmap" under the function represented by "ma".
12839 * In other words, plug in "ma" in the domain or range of "bmap".
12840 * The result is a basic map that lives in the same space as "bmap"
12841 * except that the domain or range has been replaced by
12842 * the domain space of "ma".
12844 * If bmap is represented by
12846 * A(p) + S u + B x + T v + C(divs) >= 0,
12848 * where u and x are input and output dimensions if type == isl_dim_out
12849 * while x and v are input and output dimensions if type == isl_dim_in,
12850 * and ma is represented by
12852 * x = D(p) + F(y) + G(divs')
12854 * then the result is
12856 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12858 * The divs in the input set are similarly adjusted.
12859 * In particular
12861 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12863 * becomes
12865 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12866 * B_i G(divs') + c_i(divs))/n_i)
12868 * If bmap is not a rational map and if F(y) involves any denominators
12870 * x_i = (f_i y + h_i)/m_i
12872 * then additional constraints are added to ensure that we only
12873 * map back integer points. That is we enforce
12875 * f_i y + h_i = m_i alpha_i
12877 * with alpha_i an additional existentially quantified variable.
12879 * We first copy over the divs from "ma".
12880 * Then we add the modified constraints and divs from "bmap".
12881 * Finally, we add the stride constraints, if needed.
12883 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12884 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12885 __isl_take isl_multi_aff *ma)
12887 int i, k;
12888 isl_space *space;
12889 isl_basic_map *res = NULL;
12890 int n_before, n_after, n_div_bmap, n_div_ma;
12891 isl_int f, c1, c2, g;
12892 isl_bool rational;
12893 int strides;
12895 isl_int_init(f);
12896 isl_int_init(c1);
12897 isl_int_init(c2);
12898 isl_int_init(g);
12900 ma = isl_multi_aff_align_divs(ma);
12901 if (!bmap || !ma)
12902 goto error;
12903 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12904 goto error;
12906 if (type == isl_dim_in) {
12907 n_before = 0;
12908 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12909 } else {
12910 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12911 n_after = 0;
12913 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12914 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12916 space = isl_multi_aff_get_domain_space(ma);
12917 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12918 rational = isl_basic_map_is_rational(bmap);
12919 strides = rational ? 0 : multi_aff_strides(ma);
12920 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12921 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12922 if (rational)
12923 res = isl_basic_map_set_rational(res);
12925 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12926 if (isl_basic_map_alloc_div(res) < 0)
12927 goto error;
12929 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12930 goto error;
12932 for (i = 0; i < bmap->n_eq; ++i) {
12933 k = isl_basic_map_alloc_equality(res);
12934 if (k < 0)
12935 goto error;
12936 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12937 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12940 for (i = 0; i < bmap->n_ineq; ++i) {
12941 k = isl_basic_map_alloc_inequality(res);
12942 if (k < 0)
12943 goto error;
12944 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12945 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12948 for (i = 0; i < bmap->n_div; ++i) {
12949 if (isl_int_is_zero(bmap->div[i][0])) {
12950 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12951 continue;
12953 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12954 n_before, n_after, n_div_ma, n_div_bmap,
12955 f, c1, c2, g, 1);
12958 if (strides)
12959 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12961 isl_int_clear(f);
12962 isl_int_clear(c1);
12963 isl_int_clear(c2);
12964 isl_int_clear(g);
12965 isl_basic_map_free(bmap);
12966 isl_multi_aff_free(ma);
12967 res = isl_basic_map_simplify(res);
12968 return isl_basic_map_finalize(res);
12969 error:
12970 isl_int_clear(f);
12971 isl_int_clear(c1);
12972 isl_int_clear(c2);
12973 isl_int_clear(g);
12974 isl_basic_map_free(bmap);
12975 isl_multi_aff_free(ma);
12976 isl_basic_map_free(res);
12977 return NULL;
12980 /* Compute the preimage of "bset" under the function represented by "ma".
12981 * In other words, plug in "ma" in "bset". The result is a basic set
12982 * that lives in the domain space of "ma".
12984 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12985 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12987 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12990 /* Compute the preimage of the domain of "bmap" under the function
12991 * represented by "ma".
12992 * In other words, plug in "ma" in the domain of "bmap".
12993 * The result is a basic map that lives in the same space as "bmap"
12994 * except that the domain has been replaced by the domain space of "ma".
12996 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12997 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12999 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13002 /* Compute the preimage of the range of "bmap" under the function
13003 * represented by "ma".
13004 * In other words, plug in "ma" in the range of "bmap".
13005 * The result is a basic map that lives in the same space as "bmap"
13006 * except that the range has been replaced by the domain space of "ma".
13008 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13009 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13011 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13014 /* Check if the range of "ma" is compatible with the domain or range
13015 * (depending on "type") of "map".
13016 * Return isl_stat_error if anything is wrong.
13018 static isl_stat check_map_compatible_range_multi_aff(
13019 __isl_keep isl_map *map, enum isl_dim_type type,
13020 __isl_keep isl_multi_aff *ma)
13022 isl_bool m;
13023 isl_space *ma_space;
13025 ma_space = isl_multi_aff_get_space(ma);
13026 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13027 isl_space_free(ma_space);
13028 if (m < 0)
13029 return isl_stat_error;
13030 if (!m)
13031 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13032 "spaces don't match", return isl_stat_error);
13033 return isl_stat_ok;
13036 /* Compute the preimage of the domain or range (depending on "type")
13037 * of "map" under the function represented by "ma".
13038 * In other words, plug in "ma" in the domain or range of "map".
13039 * The result is a map that lives in the same space as "map"
13040 * except that the domain or range has been replaced by
13041 * the domain space of "ma".
13043 * The parameters are assumed to have been aligned.
13045 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13046 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13048 int i;
13049 isl_space *space;
13051 map = isl_map_cow(map);
13052 ma = isl_multi_aff_align_divs(ma);
13053 if (!map || !ma)
13054 goto error;
13055 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13056 goto error;
13058 for (i = 0; i < map->n; ++i) {
13059 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13060 isl_multi_aff_copy(ma));
13061 if (!map->p[i])
13062 goto error;
13065 space = isl_multi_aff_get_domain_space(ma);
13066 space = isl_space_set(isl_map_get_space(map), type, space);
13068 isl_space_free(map->dim);
13069 map->dim = space;
13070 if (!map->dim)
13071 goto error;
13073 isl_multi_aff_free(ma);
13074 if (map->n > 1)
13075 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13076 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13077 return map;
13078 error:
13079 isl_multi_aff_free(ma);
13080 isl_map_free(map);
13081 return NULL;
13084 /* Compute the preimage of the domain or range (depending on "type")
13085 * of "map" under the function represented by "ma".
13086 * In other words, plug in "ma" in the domain or range of "map".
13087 * The result is a map that lives in the same space as "map"
13088 * except that the domain or range has been replaced by
13089 * the domain space of "ma".
13091 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13092 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13094 isl_bool aligned;
13096 if (!map || !ma)
13097 goto error;
13099 aligned = isl_map_space_has_equal_params(map, ma->space);
13100 if (aligned < 0)
13101 goto error;
13102 if (aligned)
13103 return map_preimage_multi_aff(map, type, ma);
13105 if (isl_map_check_named_params(map) < 0)
13106 goto error;
13107 if (!isl_space_has_named_params(ma->space))
13108 isl_die(map->ctx, isl_error_invalid,
13109 "unaligned unnamed parameters", goto error);
13110 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13111 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13113 return map_preimage_multi_aff(map, type, ma);
13114 error:
13115 isl_multi_aff_free(ma);
13116 return isl_map_free(map);
13119 /* Compute the preimage of "set" under the function represented by "ma".
13120 * In other words, plug in "ma" in "set". The result is a set
13121 * that lives in the domain space of "ma".
13123 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13124 __isl_take isl_multi_aff *ma)
13126 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13129 /* Compute the preimage of the domain of "map" under the function
13130 * represented by "ma".
13131 * In other words, plug in "ma" in the domain of "map".
13132 * The result is a map that lives in the same space as "map"
13133 * except that the domain has been replaced by the domain space of "ma".
13135 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13136 __isl_take isl_multi_aff *ma)
13138 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13141 /* Compute the preimage of the range of "map" under the function
13142 * represented by "ma".
13143 * In other words, plug in "ma" in the range of "map".
13144 * The result is a map that lives in the same space as "map"
13145 * except that the range has been replaced by the domain space of "ma".
13147 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13148 __isl_take isl_multi_aff *ma)
13150 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13153 /* Compute the preimage of "map" under the function represented by "pma".
13154 * In other words, plug in "pma" in the domain or range of "map".
13155 * The result is a map that lives in the same space as "map",
13156 * except that the space of type "type" has been replaced by
13157 * the domain space of "pma".
13159 * The parameters of "map" and "pma" are assumed to have been aligned.
13161 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13162 __isl_take isl_map *map, enum isl_dim_type type,
13163 __isl_take isl_pw_multi_aff *pma)
13165 int i;
13166 isl_map *res;
13168 if (!pma)
13169 goto error;
13171 if (pma->n == 0) {
13172 isl_pw_multi_aff_free(pma);
13173 res = isl_map_empty(isl_map_get_space(map));
13174 isl_map_free(map);
13175 return res;
13178 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13179 isl_multi_aff_copy(pma->p[0].maff));
13180 if (type == isl_dim_in)
13181 res = isl_map_intersect_domain(res,
13182 isl_map_copy(pma->p[0].set));
13183 else
13184 res = isl_map_intersect_range(res,
13185 isl_map_copy(pma->p[0].set));
13187 for (i = 1; i < pma->n; ++i) {
13188 isl_map *res_i;
13190 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13191 isl_multi_aff_copy(pma->p[i].maff));
13192 if (type == isl_dim_in)
13193 res_i = isl_map_intersect_domain(res_i,
13194 isl_map_copy(pma->p[i].set));
13195 else
13196 res_i = isl_map_intersect_range(res_i,
13197 isl_map_copy(pma->p[i].set));
13198 res = isl_map_union(res, res_i);
13201 isl_pw_multi_aff_free(pma);
13202 isl_map_free(map);
13203 return res;
13204 error:
13205 isl_pw_multi_aff_free(pma);
13206 isl_map_free(map);
13207 return NULL;
13210 /* Compute the preimage of "map" under the function represented by "pma".
13211 * In other words, plug in "pma" in the domain or range of "map".
13212 * The result is a map that lives in the same space as "map",
13213 * except that the space of type "type" has been replaced by
13214 * the domain space of "pma".
13216 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13217 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13219 isl_bool aligned;
13221 if (!map || !pma)
13222 goto error;
13224 aligned = isl_map_space_has_equal_params(map, pma->dim);
13225 if (aligned < 0)
13226 goto error;
13227 if (aligned)
13228 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13230 if (isl_map_check_named_params(map) < 0)
13231 goto error;
13232 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13233 goto error;
13234 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13235 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13237 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13238 error:
13239 isl_pw_multi_aff_free(pma);
13240 return isl_map_free(map);
13243 /* Compute the preimage of "set" under the function represented by "pma".
13244 * In other words, plug in "pma" in "set". The result is a set
13245 * that lives in the domain space of "pma".
13247 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13248 __isl_take isl_pw_multi_aff *pma)
13250 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13253 /* Compute the preimage of the domain of "map" under the function
13254 * represented by "pma".
13255 * In other words, plug in "pma" in the domain of "map".
13256 * The result is a map that lives in the same space as "map",
13257 * except that domain space has been replaced by the domain space of "pma".
13259 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13260 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13262 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13265 /* Compute the preimage of the range of "map" under the function
13266 * represented by "pma".
13267 * In other words, plug in "pma" in the range of "map".
13268 * The result is a map that lives in the same space as "map",
13269 * except that range space has been replaced by the domain space of "pma".
13271 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13272 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13274 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13277 /* Compute the preimage of "map" under the function represented by "mpa".
13278 * In other words, plug in "mpa" in the domain or range of "map".
13279 * The result is a map that lives in the same space as "map",
13280 * except that the space of type "type" has been replaced by
13281 * the domain space of "mpa".
13283 * If the map does not involve any constraints that refer to the
13284 * dimensions of the substituted space, then the only possible
13285 * effect of "mpa" on the map is to map the space to a different space.
13286 * We create a separate isl_multi_aff to effectuate this change
13287 * in order to avoid spurious splitting of the map along the pieces
13288 * of "mpa".
13289 * If "mpa" has a non-trivial explicit domain, however,
13290 * then the full substitution should be performed.
13292 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13293 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13295 int n;
13296 isl_bool full;
13297 isl_pw_multi_aff *pma;
13299 if (!map || !mpa)
13300 goto error;
13302 n = isl_map_dim(map, type);
13303 full = isl_map_involves_dims(map, type, 0, n);
13304 if (full >= 0 && !full)
13305 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13306 if (full < 0)
13307 goto error;
13308 if (!full) {
13309 isl_space *space;
13310 isl_multi_aff *ma;
13312 space = isl_multi_pw_aff_get_space(mpa);
13313 isl_multi_pw_aff_free(mpa);
13314 ma = isl_multi_aff_zero(space);
13315 return isl_map_preimage_multi_aff(map, type, ma);
13318 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13319 return isl_map_preimage_pw_multi_aff(map, type, pma);
13320 error:
13321 isl_map_free(map);
13322 isl_multi_pw_aff_free(mpa);
13323 return NULL;
13326 /* Compute the preimage of "map" under the function represented by "mpa".
13327 * In other words, plug in "mpa" in the domain "map".
13328 * The result is a map that lives in the same space as "map",
13329 * except that domain space has been replaced by the domain space of "mpa".
13331 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13332 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13334 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13337 /* Compute the preimage of "set" by the function represented by "mpa".
13338 * In other words, plug in "mpa" in "set".
13340 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13341 __isl_take isl_multi_pw_aff *mpa)
13343 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13346 /* Return a copy of the equality constraints of "bset" as a matrix.
13348 __isl_give isl_mat *isl_basic_set_extract_equalities(
13349 __isl_keep isl_basic_set *bset)
13351 isl_ctx *ctx;
13352 unsigned total;
13354 if (!bset)
13355 return NULL;
13357 ctx = isl_basic_set_get_ctx(bset);
13358 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13359 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13362 /* Are the "n" "coefficients" starting at "first" of the integer division
13363 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13364 * to each other?
13365 * The "coefficient" at position 0 is the denominator.
13366 * The "coefficient" at position 1 is the constant term.
13368 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13369 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13370 unsigned first, unsigned n)
13372 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13373 return isl_bool_error;
13374 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13375 return isl_bool_error;
13376 return isl_seq_eq(bmap1->div[pos1] + first,
13377 bmap2->div[pos2] + first, n);
13380 /* Are the integer division expressions at position "pos1" in "bmap1" and
13381 * "pos2" in "bmap2" equal to each other, except that the constant terms
13382 * are different?
13384 isl_bool isl_basic_map_equal_div_expr_except_constant(
13385 __isl_keep isl_basic_map *bmap1, int pos1,
13386 __isl_keep isl_basic_map *bmap2, int pos2)
13388 isl_bool equal;
13389 unsigned total;
13391 if (!bmap1 || !bmap2)
13392 return isl_bool_error;
13393 total = isl_basic_map_total_dim(bmap1);
13394 if (total != isl_basic_map_total_dim(bmap2))
13395 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13396 "incomparable div expressions", return isl_bool_error);
13397 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13398 0, 1);
13399 if (equal < 0 || !equal)
13400 return equal;
13401 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13402 1, 1);
13403 if (equal < 0 || equal)
13404 return isl_bool_not(equal);
13405 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13406 2, total);
13409 /* Replace the numerator of the constant term of the integer division
13410 * expression at position "div" in "bmap" by "value".
13411 * The caller guarantees that this does not change the meaning
13412 * of the input.
13414 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13415 __isl_take isl_basic_map *bmap, int div, int value)
13417 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13418 return isl_basic_map_free(bmap);
13420 isl_int_set_si(bmap->div[div][1], value);
13422 return bmap;
13425 /* Is the point "inner" internal to inequality constraint "ineq"
13426 * of "bset"?
13427 * The point is considered to be internal to the inequality constraint,
13428 * if it strictly lies on the positive side of the inequality constraint,
13429 * or if it lies on the constraint and the constraint is lexico-positive.
13431 static isl_bool is_internal(__isl_keep isl_vec *inner,
13432 __isl_keep isl_basic_set *bset, int ineq)
13434 isl_ctx *ctx;
13435 int pos;
13436 unsigned total;
13438 if (!inner || !bset)
13439 return isl_bool_error;
13441 ctx = isl_basic_set_get_ctx(bset);
13442 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13443 &ctx->normalize_gcd);
13444 if (!isl_int_is_zero(ctx->normalize_gcd))
13445 return isl_int_is_nonneg(ctx->normalize_gcd);
13447 total = isl_basic_set_dim(bset, isl_dim_all);
13448 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13449 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13452 /* Tighten the inequality constraints of "bset" that are outward with respect
13453 * to the point "vec".
13454 * That is, tighten the constraints that are not satisfied by "vec".
13456 * "vec" is a point internal to some superset S of "bset" that is used
13457 * to make the subsets of S disjoint, by tightening one half of the constraints
13458 * that separate two subsets. In particular, the constraints of S
13459 * are all satisfied by "vec" and should not be tightened.
13460 * Of the internal constraints, those that have "vec" on the outside
13461 * are tightened. The shared facet is included in the adjacent subset
13462 * with the opposite constraint.
13463 * For constraints that saturate "vec", this criterion cannot be used
13464 * to determine which of the two sides should be tightened.
13465 * Instead, the sign of the first non-zero coefficient is used
13466 * to make this choice. Note that this second criterion is never used
13467 * on the constraints of S since "vec" is interior to "S".
13469 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13470 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13472 int j;
13474 bset = isl_basic_set_cow(bset);
13475 if (!bset)
13476 return NULL;
13477 for (j = 0; j < bset->n_ineq; ++j) {
13478 isl_bool internal;
13480 internal = is_internal(vec, bset, j);
13481 if (internal < 0)
13482 return isl_basic_set_free(bset);
13483 if (internal)
13484 continue;
13485 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13488 return bset;
13491 /* Replace the variables x of type "type" starting at "first" in "bmap"
13492 * by x' with x = M x' with M the matrix trans.
13493 * That is, replace the corresponding coefficients c by c M.
13495 * The transformation matrix should be a square matrix.
13497 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13498 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13499 __isl_take isl_mat *trans)
13501 unsigned pos;
13503 bmap = isl_basic_map_cow(bmap);
13504 if (!bmap || !trans)
13505 goto error;
13507 if (trans->n_row != trans->n_col)
13508 isl_die(trans->ctx, isl_error_invalid,
13509 "expecting square transformation matrix", goto error);
13510 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13511 goto error;
13513 pos = isl_basic_map_offset(bmap, type) + first;
13515 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13516 isl_mat_copy(trans)) < 0)
13517 goto error;
13518 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13519 isl_mat_copy(trans)) < 0)
13520 goto error;
13521 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13522 isl_mat_copy(trans)) < 0)
13523 goto error;
13525 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13526 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13528 isl_mat_free(trans);
13529 return bmap;
13530 error:
13531 isl_mat_free(trans);
13532 isl_basic_map_free(bmap);
13533 return NULL;
13536 /* Replace the variables x of type "type" starting at "first" in "bset"
13537 * by x' with x = M x' with M the matrix trans.
13538 * That is, replace the corresponding coefficients c by c M.
13540 * The transformation matrix should be a square matrix.
13542 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13543 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13544 __isl_take isl_mat *trans)
13546 return isl_basic_map_transform_dims(bset, type, first, trans);